From 421190935f58f2dff0761c588494cb8ce788a27d Mon Sep 17 00:00:00 2001 From: Jack Lavigne Date: Fri, 17 Apr 2026 18:29:48 +0200 Subject: [PATCH 1/3] hal/gles: add Adapter::new_external() for WebGL2 Mirrors the existing Adapter::new_external in gles/egl.rs and gles/wgl.rs for importing an externally-created WebGL2 rendering context. The caller retains ownership of the canvas and the context; no Surface is created. --- wgpu-hal/src/gles/web.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/wgpu-hal/src/gles/web.rs b/wgpu-hal/src/gles/web.rs index 8e404d6b50a..04f49fc384a 100644 --- a/wgpu-hal/src/gles/web.rs +++ b/wgpu-hal/src/gles/web.rs @@ -179,6 +179,33 @@ 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, + ) + } + } +} + #[derive(Debug)] pub struct Surface { canvas: Canvas, From 7442f3079163b9e543808a69e571d4df9b8a60e7 Mon Sep 17 00:00:00 2001 From: Jack Lavigne Date: Fri, 17 Apr 2026 18:39:03 +0200 Subject: [PATCH 2/3] CHANGELOG: add entry for WebGL2 Adapter::new_external (#9438) --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 529a5def093..7433f217d77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -82,6 +82,7 @@ By @teoxoy in [#9351](https://github.com/gfx-rs/wgpu/pull/9351). #### 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. By @pepperoni505 in [#9438](https://github.com/gfx-rs/wgpu/pull/9438). #### DX12 From 1c2d0f0ffe2e1fadfa55e24f272ae3d2f8e6d8e3 Mon Sep 17 00:00:00 2001 From: Jack Lavigne Date: Sat, 18 Apr 2026 01:19:11 +0200 Subject: [PATCH 3/3] hal/gles: expose imported context back through Adapter/Device Pair with Adapter::new_external: embedders that bring their own WebGL2 context need a way to reach it back for interop. Add Adapter::adapter_context() and Device::context(), both returning the AdapterContext so callers can drive raw-GL work on the shared glow::Context or web_sys::WebGl2RenderingContext. --- CHANGELOG.md | 2 +- wgpu-hal/src/gles/web.rs | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7433f217d77..cf206a7e850 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -82,7 +82,7 @@ By @teoxoy in [#9351](https://github.com/gfx-rs/wgpu/pull/9351). #### 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. By @pepperoni505 in [#9438](https://github.com/gfx-rs/wgpu/pull/9438). +- 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 04f49fc384a..c9c65a04bda 100644 --- a/wgpu-hal/src/gles/web.rs +++ b/wgpu-hal/src/gles/web.rs @@ -204,6 +204,17 @@ impl super::Adapter { ) } } + + 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)]