Skip to content

Commit

Permalink
[wgpu-hal] return None in Adapter.surface_capabilities() if the `…
Browse files Browse the repository at this point in the history
…Surface` and the `Adapter` don't share the same WebGL2 context
  • Loading branch information
teoxoy committed Jul 4, 2024
1 parent 7910fd8 commit 9f34acd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ By @atlv24 in [#5383](https://github.com/gfx-rs/wgpu/pull/5383)
When targeting WebGL2, it has always been the case that a surface had to be created before calling `request_adapter()`.
We now make this requirement explicit.

Validation was also added to prevent configuring the surface with a device that doesn't share the same underlying
WebGL2 context since this has never worked.

Calling `enumerate_adapters()` when targeting WebGPU used to return an empty `Vec` and since we now require users
to pass a compatible surface when targeting WebGL2, having `enumerate_adapters()` doesn't make sense.

Expand Down
5 changes: 5 additions & 0 deletions wgpu-hal/src/gles/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,11 @@ impl crate::Adapter for super::Adapter {
&self,
surface: &super::Surface,
) -> Option<crate::SurfaceCapabilities> {
#[cfg(webgl)]
if self.shared.context.webgl2_context != surface.webgl2_context {
return None;
}

if surface.presentable {
let mut formats = vec![
wgt::TextureFormat::Rgba8Unorm,
Expand Down
14 changes: 10 additions & 4 deletions wgpu-hal/src/gles/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use super::TextureFormatDesc;
/// with the `AdapterContext` API from the EGL implementation.
pub struct AdapterContext {
pub glow_context: glow::Context,
pub webgl2_context: web_sys::WebGl2RenderingContext,
}

impl AdapterContext {
Expand Down Expand Up @@ -124,9 +125,14 @@ impl crate::Instance for Instance {
if let Some(surface_hint) = surface_hint {
let gl = glow::Context::from_webgl2_context(surface_hint.webgl2_context.clone());

unsafe { super::Adapter::expose(AdapterContext { glow_context: gl }) }
.into_iter()
.collect()
unsafe {
super::Adapter::expose(AdapterContext {
glow_context: gl,
webgl2_context: surface_hint.webgl2_context.clone(),
})
}
.into_iter()
.collect()
} else {
Vec::new()
}
Expand Down Expand Up @@ -172,7 +178,7 @@ impl crate::Instance for Instance {
#[derive(Debug)]
pub struct Surface {
canvas: Canvas,
webgl2_context: web_sys::WebGl2RenderingContext,
pub(super) webgl2_context: web_sys::WebGl2RenderingContext,
pub(super) swapchain: RwLock<Option<Swapchain>>,
texture: Mutex<Option<glow::Texture>>,
pub(super) presentable: bool,
Expand Down

0 comments on commit 9f34acd

Please sign in to comment.