diff --git a/CHANGELOG.md b/CHANGELOG.md index d7c2c19a823..f2df60d0d28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -105,6 +105,7 @@ By @andyleiserson in [#9321](https://github.com/gfx-rs/wgpu/pull/9321). #### GLES - Added support for GLSL passthrough. By @inner-daemons in [#9064](https://github.com/gfx-rs/wgpu/pull/9064). +- Implement `Adapter::new_external()` for WebGL2 (just like EGL/WGL) to import an external WebGL2 rendering context, and expose the imported context back through `Adapter::adapter_context()` / `Device::context()`. By @pepperoni505 in [#9438](https://github.com/gfx-rs/wgpu/pull/9438). #### DX12 diff --git a/wgpu-hal/src/gles/web.rs b/wgpu-hal/src/gles/web.rs index 8e404d6b50a..c9c65a04bda 100644 --- a/wgpu-hal/src/gles/web.rs +++ b/wgpu-hal/src/gles/web.rs @@ -179,6 +179,44 @@ impl crate::Instance for Instance { } } +impl super::Adapter { + /// Creates a new external adapter from an existing WebGL2 rendering context. + /// + /// # Safety + /// + /// - The underlying WebGL2 context must be valid (not lost). + /// - The underlying WebGL2 context must be valid when interfacing with any objects returned by + /// wgpu-hal from this adapter. + /// - The underlying WebGL2 context must be valid when dropping this adapter and when + /// dropping any objects returned from this adapter. + pub unsafe fn new_external( + webgl2_context: web_sys::WebGl2RenderingContext, + options: wgt::GlBackendOptions, + ) -> Option> { + let glow_context = glow::Context::from_webgl2_context(webgl2_context.clone()); + unsafe { + Self::expose( + AdapterContext { + glow_context, + webgl2_context, + }, + options, + ) + } + } + + pub fn adapter_context(&self) -> &AdapterContext { + &self.shared.context + } +} + +impl super::Device { + /// Returns the underlying WebGL2 `AdapterContext`. + pub fn context(&self) -> &AdapterContext { + &self.shared.context + } +} + #[derive(Debug)] pub struct Surface { canvas: Canvas,