-
Notifications
You must be signed in to change notification settings - Fork 969
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
Support for OffscreenCanvas in Web Backend #1837
Comments
This is how raw-window-handle works on the web in general. See https://docs.rs/raw-window-handle/0.3.3/raw_window_handle/web/struct.WebHandle.html#structfield.id It's really useful in general because it allows native and web applications to run in the same way (e.g. using winit+wgpu to run anywhere). We should support offscreen canvas or canvases that haven't been added to the DOM somehow though. We could expose a wasm-only extension that accepts those web-sys types directly instead like you mentioned. The downsides are:
Maybe there are some other options? |
Your solution sounds good! It could be optional behind a feature flag. web-sys doesn't have a lot of major version updates, since it's just an autogenerated API wrapper. I can see how this workaround is a good solution when your main platform is native and you just want to get it to work on the web somehow, but when the web is the main target, it causes problems, especially when you want to integrate it with existing web frameworks. |
I've been hitting this too, needing to do the dance of adding the canvas to the DOM with the proper ID. My case is a further complicated because I am using the Shadow DOM, which I think adding web-only methods that are compiled out on native is fine. Ideally the surface creation methods can directly take a |
Add specialized methods on web for creating a wgpu Surface directly from an web_sys::HTMLCanvasElement and OffscreenCanvas. * Instance::create_surface_from_canvas * Instance::create_surface_from_offscreen_canvas Fixes gfx-rs#1837.
Add specialized methods on web for creating a wgpu Surface directly from an web_sys::HTMLCanvasElement and OffscreenCanvas. * Instance::create_surface_from_canvas * Instance::create_surface_from_offscreen_canvas Fixes #1837.
Is your feature request related to a problem? Please describe.
I've looked into the way the web backend creates the rendering context:
wgpu/wgpu/src/backend/web.rs
Lines 934 to 957 in 62f2636
This seems like a hack, since I can't pass in a canvas directly, I have to create it elsewhere, insert it into the DOM, assign a random id and then pass in the id.
This is fine for demo applications, but when you have an existing web application you want to integrate into, this doesn't seem like a great idea, since the developer has to be careful not to re-use the same id multiple times if there are multiple canvases in the DOM.
Also, eventually it'll be interesting to use wgpu-rs to render in a Web Worker using OffscreenCanvas. Right now, OffscreenCanvas doesn't support webgpu anyways, but as far as my research has unearthed, this will change at some point.
Describe the solution you'd like
Not passing in a
raw_handle
, but aHtmlCanvasElement
orOffscreenCanvas
directly. Both supply theget_context
API needed for the backend. Alternatively, the backend could also receive the context directly, but that would require the application to set it up with the right parameters, which might not be trivial (though it looks like it is right now).Describe alternatives you've considered
I've looked into whether I can skip the function shown above and create this context directly, but all of the types needed aren't
pub
.Additional context
This is more relevant for the WebGL2 Backend #1686 right now, where it would really work.
The text was updated successfully, but these errors were encountered: