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

web: Add Instance::create_surface_from_canvas (fix #1837) #1932

Merged
merged 1 commit into from
Sep 10, 2021

Conversation

Herschel
Copy link
Contributor

@Herschel Herschel commented Sep 9, 2021

Add specialized methods on web for creating a wgpu Surface directly
from a web_sys::HTMLCanvasElement and web_sys::OffscreenCanvas.

  • Instance::create_surface_from_canvas
  • Instance::create_surface_from_offscreen_canvas

Connections
Fixes #1837.

Description
Currently Instance::create_surface requires a HasRawWindowHandle. The web backend uses this handle to search for a canvas with a data-raw-handle attribute.

This is handy for full-screen demos or games, or quickly compiling winit apps to both desktop and web. However, this isn't ideal for apps that are part of a larger site, or usable as a JS library. For example, the user may want to instantiate wgpu without attaching the canvas to the DOM, or the canvas may be buried inside a shadow DOM. Additionally, it'd be nice to use OffscreenCanvas. See #1837 for more info.

This adds web-specific Instance::create_surface_from_canvas and Instance::create_surface_from_offscreen_canvas methods that directly accept web_sys::HtmlCanvasElement and web_sys::OffscreenCanvas. These methods are only compiled for the wasm32 architecture. This was modeled after the Instance::create_surface_from_core_animation_layer method for macOS.

This does add web_sys types to the externally visible API. However, web_sys is already a dependency of the project, and as WebGPU is a web-based API, wgpu should work directly with standard web types.

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.
@kvark kvark requested a review from grovesNL September 10, 2021 00:49
Copy link
Collaborator

@grovesNL grovesNL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thank you!

This seems good to me assuming we're ok with having web-sys types/version as part of the public API. I'm not sure of a way around this besides having a common canvas trait that web-sys implements (like we do with raw-window-handle)

@kvark kvark enabled auto-merge (rebase) September 10, 2021 01:07
@kvark kvark merged commit 8080a3c into gfx-rs:master Sep 10, 2021
@Herschel Herschel deleted the create-surface-canvas branch September 10, 2021 01:53
@zicklag zicklag mentioned this pull request Apr 8, 2022
7 tasks
@DemiKal DemiKal mentioned this pull request Oct 5, 2022
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for OffscreenCanvas in Web Backend
3 participants