From cda0be9cbf326b147366897dca2ed56afaa7173b Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Fri, 22 Oct 2021 09:45:51 -0400 Subject: [PATCH] hal/egl: don't assume EGL-1.5 if upcast works (#2105) --- wgpu-hal/src/gles/adapter.rs | 2 +- wgpu-hal/src/gles/egl.rs | 101 +++++++++++++++++------------------ wgpu-hal/src/gles/web.rs | 6 ++- 3 files changed, 55 insertions(+), 54 deletions(-) diff --git a/wgpu-hal/src/gles/adapter.rs b/wgpu-hal/src/gles/adapter.rs index 786aca889..fe1ac1ae0 100644 --- a/wgpu-hal/src/gles/adapter.rs +++ b/wgpu-hal/src/gles/adapter.rs @@ -598,7 +598,7 @@ impl crate::Adapter for super::Adapter { ) -> Option { if surface.presentable { Some(crate::SurfaceCapabilities { - formats: if surface.enable_srgb { + formats: if surface.supports_srgb() { vec![ wgt::TextureFormat::Rgba8UnormSrgb, #[cfg(not(target_arch = "wasm32"))] diff --git a/wgpu-hal/src/gles/egl.rs b/wgpu-hal/src/gles/egl.rs index ec5e1a46d..585290763 100644 --- a/wgpu-hal/src/gles/egl.rs +++ b/wgpu-hal/src/gles/egl.rs @@ -656,11 +656,6 @@ impl crate::Instance for Instance { } }; - let enable_srgb = match inner.srgb_kind { - SrgbFrameBufferKind::Core | SrgbFrameBufferKind::Khr => true, - SrgbFrameBufferKind::None => false, - }; - inner .egl .make_current(inner.display, None, None, None) @@ -676,7 +671,7 @@ impl crate::Instance for Instance { pbuffer: inner.pbuffer, raw_window_handle, swapchain: None, - enable_srgb, + srgb_kind: inner.srgb_kind, }) } unsafe fn destroy_surface(&self, _surface: Surface) {} @@ -756,7 +751,7 @@ pub struct Surface { pub(super) presentable: bool, raw_window_handle: RawWindowHandle, swapchain: Option, - pub(super) enable_srgb: bool, + srgb_kind: SrgbFrameBufferKind, } unsafe impl Send for Surface {} @@ -834,6 +829,13 @@ impl Surface { None => None, } } + + pub fn supports_srgb(&self) -> bool { + match self.srgb_kind { + SrgbFrameBufferKind::None => false, + _ => true, + } + } } impl crate::Surface for Surface { @@ -891,58 +893,55 @@ impl crate::Surface for Surface { _ => unreachable!(), }; - let raw = if let Some(egl) = self.egl.upcast::() { - let attributes = [ - egl::RENDER_BUFFER as usize, - if cfg!(target_os = "android") { - egl::BACK_BUFFER as usize - } else { - egl::SINGLE_BUFFER as usize - }, - // Always enable sRGB in EGL 1.5 - egl::GL_COLORSPACE as usize, - egl::GL_COLORSPACE_SRGB as usize, - egl::ATTRIB_NONE, - ]; + let mut attributes = vec![ + egl::RENDER_BUFFER, + if cfg!(target_os = "android") { + egl::BACK_BUFFER + } else { + egl::SINGLE_BUFFER + }, + ]; + match self.srgb_kind { + SrgbFrameBufferKind::None => {} + SrgbFrameBufferKind::Core => { + attributes.push(egl::GL_COLORSPACE); + attributes.push(egl::GL_COLORSPACE_SRGB); + } + SrgbFrameBufferKind::Khr => { + attributes.push(EGL_GL_COLORSPACE_KHR as i32); + attributes.push(EGL_GL_COLORSPACE_SRGB_KHR as i32); + } + } + attributes.push(egl::ATTRIB_NONE as i32); + // Careful, we can still be in 1.4 version even if `upcast` succeeds + let raw_result = if let Some(egl) = self.egl.upcast::() { + let attributes_usize = attributes + .into_iter() + .map(|v| v as usize) + .collect::>(); egl.create_platform_window_surface( self.display, self.config, native_window_ptr, - &attributes, + &attributes_usize, ) - .map_err(|e| { - log::warn!("Error in create_platform_window_surface: {:?}", e); - crate::SurfaceError::Lost - }) } else { - let mut attributes = vec![ - egl::RENDER_BUFFER, - if cfg!(target_os = "android") { - egl::BACK_BUFFER - } else { - egl::SINGLE_BUFFER - }, - ]; - if self.enable_srgb { - attributes.push(EGL_GL_COLORSPACE_KHR as i32); - attributes.push(EGL_GL_COLORSPACE_SRGB_KHR as i32); - } - attributes.push(egl::ATTRIB_NONE as i32); - self.egl - .create_window_surface( - self.display, - self.config, - native_window_ptr, - Some(&attributes), - ) - .map_err(|e| { - log::warn!("Error in create_platform_window_surface: {:?}", e); - crate::SurfaceError::Lost - }) - }?; + self.egl.create_window_surface( + self.display, + self.config, + native_window_ptr, + Some(&attributes), + ) + }; - (raw, wl_window) + match raw_result { + Ok(raw) => (raw, wl_window), + Err(e) => { + log::warn!("Error in create_platform_window_surface: {:?}", e); + return Err(crate::SurfaceError::Lost); + } + } } }; diff --git a/wgpu-hal/src/gles/web.rs b/wgpu-hal/src/gles/web.rs index 6d531dd79..00fdcb528 100644 --- a/wgpu-hal/src/gles/web.rs +++ b/wgpu-hal/src/gles/web.rs @@ -83,7 +83,6 @@ impl crate::Instance for Instance { swapchain: None, texture: None, presentable: true, - enable_srgb: true, // WebGL only supports sRGB }) } else { unreachable!() @@ -107,7 +106,6 @@ pub struct Surface { pub(super) swapchain: Option, texture: Option, pub(super) presentable: bool, - pub(super) enable_srgb: bool, present_program: Option, } @@ -169,6 +167,10 @@ impl Surface { program } + + pub fn supports_srgb(&self) -> bool { + true // WebGL only supports sRGB + } } impl crate::Surface for Surface {