Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebGPU: Add a preinitializedWebGPUAdapter and emscripten_get_webgpu_adapter() #21073

Open
austinEng opened this issue Jan 12, 2024 · 3 comments
Labels

Comments

@austinEng
Copy link
Contributor

Currently, WebGPU applications can set Module['preinitializedWebGPUDevice'] and use emscripten_get_webgpu_device().

This makes it convenient to get the device into your C++ application, without needing any async in C++.

It would be nice to likewise add preinitializedWebGPUAdapter and emscripten_get_webgpu_adapter() so that applications can easily get the WGPUAdapter and query adapter properties in C++. Otherwise, this information needs to be queried in Javascript and passed into C++ separately.
(#21072 is about actually filling those adapter properties with useful information)

@kainino0x

@kainino0x
Copy link
Collaborator

I was actually hoping to remove preinitializedWebGPUDevice once we have:

  • a stable way to get webgpu objects in and out of wasm (like jsvalstore)
  • futures-based initialization

Though maybe you can evaluate whether this would work for whatever code you're looking at that currently uses preinitializedWebGPUDevice?

@austinEng
Copy link
Contributor Author

austinEng commented Jan 13, 2024

futures-based initialization would work except it has the usual problems of WASM apps that don't want to build with ASYNCIFY, and aren't structured to yield to the event loop. Both of those are true in the code I was looking at.

JsValStore works, except for the fact we don't define emscripten_webgpu_import_adapter / emscripten_webgpu_export_adapter - though they could be added.

I'm not sure if emscripten_webgpu_import/export* are really all that useful though because it's generally easier to just use WebGPU.mgrAdapter directly. Maybe the WebGPU.mgr* are not supposed to be exposed though..?

Consider:

EM_JS(int, get_adapter, (), {
  return JsValStore.add(myGlobalObject.propertyWhereISavedTheAdapter);
});

int adapter_handle = get_adapter();
wgpu::Adapter adapter = wgpu::Adapter::Acquire(emscripten_webgpu_import_adapter(adapter_handle));
emscripten_webgpu_release_js_handle(adapter_handle);

vs.

EM_JS(int, get_adapter, (), {
  return WebGPU.mgrAdapter.create(myGlobalObject.propertyWhereISavedTheAdapter);
});

wgpu::Adapter adapter = wgpu::Adapter::Acquire(reinterpret_cast<WGPUAdapter>(get_adapter()));

@kainino0x
Copy link
Collaborator

WebGPU.mgr* is almost certainly going to change (soon) when we put objects in WASM tables to use with externref. I'm wondering if we can replace JsValStore with a stable API to directly access those tables but I'm not sure exactly what this would look like yet. For EM_JS/EM_ASM I think we can do better somehow with externref, like for example having a wgpu::Adapter overload taking an externref directly (though this would be type unsafe):

externref_t jsAdapter = EM_ASM_EXTERNREF({
    return myGlobalObject.propertyWhereISavedTheAdapter;
});
wgpu::Adapter adapter{jsAdapter}; // constructor stores the adapter into the table

TBD though as this is going to require a lot more externref support than Emscripten currently has (which is none).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants