From d9df69ec290eafa36dc2b77465690968acd50573 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Fri, 15 Oct 2021 14:14:39 -0400 Subject: [PATCH] hal/egl: request ALPHA_SIZE for srgb, move shaders into a folder --- wgpu-hal/src/gles/adapter.rs | 4 +- wgpu-hal/src/gles/egl.rs | 58 +++++++++++-------- .../{shader_clear.frag => shaders/clear.frag} | 0 .../{shader_clear.vert => shaders/clear.vert} | 0 .../src/gles/{web => shaders}/present.frag | 0 .../src/gles/{web => shaders}/present.vert | 0 wgpu-hal/src/gles/web.rs | 4 +- 7 files changed, 38 insertions(+), 28 deletions(-) rename wgpu-hal/src/gles/{shader_clear.frag => shaders/clear.frag} (100%) rename wgpu-hal/src/gles/{shader_clear.vert => shaders/clear.vert} (100%) rename wgpu-hal/src/gles/{web => shaders}/present.frag (100%) rename wgpu-hal/src/gles/{web => shaders}/present.vert (100%) diff --git a/wgpu-hal/src/gles/adapter.rs b/wgpu-hal/src/gles/adapter.rs index 648935fd2f..deb10d9aca 100644 --- a/wgpu-hal/src/gles/adapter.rs +++ b/wgpu-hal/src/gles/adapter.rs @@ -431,12 +431,12 @@ impl super::Adapter { let vertex = gl .create_shader(glow::VERTEX_SHADER) .expect("Could not create shader"); - gl.shader_source(vertex, include_str!("./shader_clear.vert")); + gl.shader_source(vertex, include_str!("./shaders/clear.vert")); gl.compile_shader(vertex); let fragment = gl .create_shader(glow::FRAGMENT_SHADER) .expect("Could not create shader"); - gl.shader_source(fragment, include_str!("./shader_clear.frag")); + gl.shader_source(fragment, include_str!("./shaders/clear.frag")); gl.compile_shader(fragment); gl.attach_shader(program, vertex); gl.attach_shader(program, fragment); diff --git a/wgpu-hal/src/gles/egl.rs b/wgpu-hal/src/gles/egl.rs index 0b14fd6895..0ab5bef602 100644 --- a/wgpu-hal/src/gles/egl.rs +++ b/wgpu-hal/src/gles/egl.rs @@ -131,10 +131,21 @@ fn test_wayland_display() -> Option { Some(library) } +#[derive(Clone, Copy, Debug)] +enum SrgbFrameBufferKind { + /// No support for SRGB surface + None, + /// Using EGL 1.5's support for colorspaces + Core, + /// Using EGL_KHR_gl_colorspace + Khr, +} + /// Choose GLES framebuffer configuration. fn choose_config( egl: &egl::DynamicInstance, display: egl::Display, + srgb_kind: SrgbFrameBufferKind, ) -> Result<(egl::Config, bool), crate::InstanceError> { //TODO: EGL_SLOW_CONFIG let tiers = [ @@ -147,7 +158,7 @@ fn choose_config( ("native-render", &[egl::NATIVE_RENDERABLE, egl::TRUE as _]), ]; - let mut attributes = Vec::with_capacity(7); + let mut attributes = Vec::with_capacity(9); for tier_max in (0..tiers.len()).rev() { let name = tiers[tier_max].0; log::info!("\tTrying {}", name); @@ -156,6 +167,14 @@ fn choose_config( for &(_, tier_attr) in tiers[..=tier_max].iter() { attributes.extend_from_slice(tier_attr); } + // make sure the Alpha is enough to support sRGB + match srgb_kind { + SrgbFrameBufferKind::None => {} + _ => { + attributes.push(egl::ALPHA_SIZE); + attributes.push(8); + } + } attributes.push(egl::NONE); match egl.choose_first_config(display, &attributes) { @@ -227,16 +246,6 @@ fn gl_debug_message_callback(source: u32, gltype: u32, id: u32, severity: u32, m } } -#[derive(Debug)] -enum SrgbFrameBufferKind { - /// No support for SRGB surface - None, - /// Using EGL 1.5's support for colorspaces - Core, - /// Using EGL_KHR_gl_colorspace - Khr, -} - /// A wrapper around a [`glow::Context`] and the required EGL context that uses locking to guarantee /// exclusive access when shared with multiple threads. pub struct AdapterContext { @@ -356,22 +365,34 @@ impl Inner { display_extensions.split_whitespace().collect::>() ); + let srgb_kind = if version >= (1, 5) { + log::info!("\tEGL surface: +srgb"); + SrgbFrameBufferKind::Core + } else if display_extensions.contains("EGL_KHR_gl_colorspace") { + log::info!("\tEGL surface: +srgb khr"); + SrgbFrameBufferKind::Khr + } else { + log::warn!("\tEGL surface: -srgb"); + SrgbFrameBufferKind::None + }; + if log::max_level() >= log::LevelFilter::Trace { log::trace!("Configurations:"); let config_count = egl.get_config_count(display).unwrap(); let mut configurations = Vec::with_capacity(config_count); egl.get_configs(display, &mut configurations).unwrap(); for &config in configurations.iter() { - log::trace!("\tCONFORMANT=0x{:X}, RENDERABLE=0x{:X}, NATIVE_RENDERABLE=0x{:X}, SURFACE_TYPE=0x{:X}", + log::trace!("\tCONFORMANT=0x{:X}, RENDERABLE=0x{:X}, NATIVE_RENDERABLE=0x{:X}, SURFACE_TYPE=0x{:X}, ALPHA_SIZE={}", egl.get_config_attrib(display, config, egl::CONFORMANT).unwrap(), egl.get_config_attrib(display, config, egl::RENDERABLE_TYPE).unwrap(), egl.get_config_attrib(display, config, egl::NATIVE_RENDERABLE).unwrap(), egl.get_config_attrib(display, config, egl::SURFACE_TYPE).unwrap(), + egl.get_config_attrib(display, config, egl::ALPHA_SIZE).unwrap(), ); } } - let (config, supports_native_window) = choose_config(&egl, display)?; + let (config, supports_native_window) = choose_config(&egl, display, srgb_kind)?; egl.bind_api(egl::OPENGL_ES_API).unwrap(); let needs_robustness = true; @@ -439,17 +460,6 @@ impl Inner { })? }; - let srgb_kind = if version >= (1, 5) { - log::info!("\tEGL surface: +srgb"); - SrgbFrameBufferKind::Core - } else if display_extensions.contains("EGL_KHR_gl_colorspace") { - log::info!("\tEGL surface: +srgb khr"); - SrgbFrameBufferKind::Khr - } else { - log::warn!("\tEGL surface: -srgb"); - SrgbFrameBufferKind::None - }; - Ok(Self { egl, display, diff --git a/wgpu-hal/src/gles/shader_clear.frag b/wgpu-hal/src/gles/shaders/clear.frag similarity index 100% rename from wgpu-hal/src/gles/shader_clear.frag rename to wgpu-hal/src/gles/shaders/clear.frag diff --git a/wgpu-hal/src/gles/shader_clear.vert b/wgpu-hal/src/gles/shaders/clear.vert similarity index 100% rename from wgpu-hal/src/gles/shader_clear.vert rename to wgpu-hal/src/gles/shaders/clear.vert diff --git a/wgpu-hal/src/gles/web/present.frag b/wgpu-hal/src/gles/shaders/present.frag similarity index 100% rename from wgpu-hal/src/gles/web/present.frag rename to wgpu-hal/src/gles/shaders/present.frag diff --git a/wgpu-hal/src/gles/web/present.vert b/wgpu-hal/src/gles/shaders/present.vert similarity index 100% rename from wgpu-hal/src/gles/web/present.vert rename to wgpu-hal/src/gles/shaders/present.vert diff --git a/wgpu-hal/src/gles/web.rs b/wgpu-hal/src/gles/web.rs index 5f26029e68..2fd0351c29 100644 --- a/wgpu-hal/src/gles/web.rs +++ b/wgpu-hal/src/gles/web.rs @@ -153,12 +153,12 @@ impl Surface { let vertex = gl .create_shader(glow::VERTEX_SHADER) .expect("Could not create shader"); - gl.shader_source(vertex, include_str!("./web/present.vert")); + gl.shader_source(vertex, include_str!("./shaders/present.vert")); gl.compile_shader(vertex); let fragment = gl .create_shader(glow::FRAGMENT_SHADER) .expect("Could not create shader"); - gl.shader_source(fragment, include_str!("./web/present.frag")); + gl.shader_source(fragment, include_str!("./shaders/present.frag")); gl.compile_shader(fragment); gl.attach_shader(program, vertex); gl.attach_shader(program, fragment);