From a3c83f6dbf1287174007f29af1cd3cf333139940 Mon Sep 17 00:00:00 2001 From: David Lenaerts Date: Sat, 4 Oct 2025 20:35:56 +0200 Subject: [PATCH 1/4] fix(gles): Do not use comparison sampler if no comparison samplers are used in original --- naga/src/back/glsl/mod.rs | 54 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/naga/src/back/glsl/mod.rs b/naga/src/back/glsl/mod.rs index 4c5a9d8cbc..52a71a9a0d 100644 --- a/naga/src/back/glsl/mod.rs +++ b/naga/src/back/glsl/mod.rs @@ -911,7 +911,7 @@ impl<'a, W: Write> Writer<'a, W> { TypeInner::Image { mut dim, arrayed, - class, + mut class, } => { // Gather the storage format if needed let storage_format_access = match self.module.types[global.ty].inner { @@ -959,6 +959,10 @@ impl<'a, W: Write> Writer<'a, W> { // The trailing space is important write!(self.out, "uniform ")?; + if self.needs_depth_fix(ep_info, handle, &mut class) { + class = crate::ImageClass::Sampled { kind: crate::ScalarKind::Float, multi: false }; + } + // write the type // // This is way we need the leading space because `write_image_type` doesn't add @@ -1034,6 +1038,27 @@ impl<'a, W: Write> Writer<'a, W> { self.collect_reflection_info() } + fn needs_depth_fix(&mut self, ep_info: &valid::FunctionInfo, handle: Handle, class: &crate::ImageClass) -> bool { + if let crate::ImageClass::Depth { multi: false } = class { + let has_shadow_sampler = ep_info.sampling_set.iter().all(|key| { + let data = &self.module.global_variables[key.sampler]; + if key.image != handle { + return false; + } + return if let TypeInner::Sampler { comparison: true } = &self.module.types[data.ty].inner { + true + } else { + false + } + }); + + !has_shadow_sampler + } + else { + false + } + } + fn write_array_size( &mut self, base: Handle, @@ -3162,6 +3187,14 @@ impl<'a, W: Write> Writer<'a, W> { self.write_expr(expr, ctx)?; } + let needs_depth_fix = if let Expression::GlobalVariable(global_handle) = ctx.expressions[image] { + let ep_info = self.info.get_entry_point(self.entry_point_idx as usize); + self.needs_depth_fix(ep_info, global_handle, &class) + } + else { + false + }; + match level { // Auto needs no more arguments crate::SampleLevel::Auto => (), @@ -3180,7 +3213,16 @@ impl<'a, W: Write> Writer<'a, W> { // Exact and bias require another argument crate::SampleLevel::Exact(expr) => { write!(self.out, ", ")?; + + if needs_depth_fix { + write!(self.out, "float(")?; + } + self.write_expr(expr, ctx)?; + + if needs_depth_fix { + write!(self.out, ")")?; + } } crate::SampleLevel::Bias(_) => { // This needs to be done after the offset writing @@ -3226,7 +3268,15 @@ impl<'a, W: Write> Writer<'a, W> { } // End the function - write!(self.out, ")")? + write!(self.out, ")")?; + + if needs_depth_fix { + // parser thinks it will yield f32, but in reality it yields vec4f + write!(self.out, ".x")?; + } + + + // TODO: if depth texture but not comparison sampler, add ".x" } Expression::ImageLoad { image, From 5ce1ca96fd287de6ec18287f3ba163e28f502c50 Mon Sep 17 00:00:00 2001 From: David Lenaerts Date: Sat, 4 Oct 2025 20:55:06 +0200 Subject: [PATCH 2/4] fix(gles): Cleanups --- naga/src/back/glsl/mod.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/naga/src/back/glsl/mod.rs b/naga/src/back/glsl/mod.rs index 52a71a9a0d..8e2b430ce6 100644 --- a/naga/src/back/glsl/mod.rs +++ b/naga/src/back/glsl/mod.rs @@ -1040,19 +1040,15 @@ impl<'a, W: Write> Writer<'a, W> { fn needs_depth_fix(&mut self, ep_info: &valid::FunctionInfo, handle: Handle, class: &crate::ImageClass) -> bool { if let crate::ImageClass::Depth { multi: false } = class { - let has_shadow_sampler = ep_info.sampling_set.iter().all(|key| { + // if any sampler uses the comparison sampler, a fix is not needed. Otherwise, we need to actually sample a regular texture as far as glsl is concerned + !ep_info.sampling_set.iter().all(|key| { let data = &self.module.global_variables[key.sampler]; if key.image != handle { return false; } - return if let TypeInner::Sampler { comparison: true } = &self.module.types[data.ty].inner { - true - } else { - false - } - }); - !has_shadow_sampler + matches!(self.module.types[data.ty].inner, TypeInner::Sampler { comparison: true }) + }) } else { false From e13a19beeccfffc051c0e6346a1e9b375ee66133 Mon Sep 17 00:00:00 2001 From: David Lenaerts Date: Sat, 4 Oct 2025 22:44:34 +0200 Subject: [PATCH 3/4] fix(gles): Make cargo fmt happy --- naga/src/back/glsl/mod.rs | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/naga/src/back/glsl/mod.rs b/naga/src/back/glsl/mod.rs index 8e2b430ce6..0ffcb3659f 100644 --- a/naga/src/back/glsl/mod.rs +++ b/naga/src/back/glsl/mod.rs @@ -960,7 +960,10 @@ impl<'a, W: Write> Writer<'a, W> { write!(self.out, "uniform ")?; if self.needs_depth_fix(ep_info, handle, &mut class) { - class = crate::ImageClass::Sampled { kind: crate::ScalarKind::Float, multi: false }; + class = crate::ImageClass::Sampled { + kind: crate::ScalarKind::Float, + multi: false, + }; } // write the type @@ -1038,7 +1041,12 @@ impl<'a, W: Write> Writer<'a, W> { self.collect_reflection_info() } - fn needs_depth_fix(&mut self, ep_info: &valid::FunctionInfo, handle: Handle, class: &crate::ImageClass) -> bool { + fn needs_depth_fix( + &mut self, + ep_info: &valid::FunctionInfo, + handle: Handle, + class: &crate::ImageClass, + ) -> bool { if let crate::ImageClass::Depth { multi: false } = class { // if any sampler uses the comparison sampler, a fix is not needed. Otherwise, we need to actually sample a regular texture as far as glsl is concerned !ep_info.sampling_set.iter().all(|key| { @@ -1047,10 +1055,12 @@ impl<'a, W: Write> Writer<'a, W> { return false; } - matches!(self.module.types[data.ty].inner, TypeInner::Sampler { comparison: true }) + matches!( + self.module.types[data.ty].inner, + TypeInner::Sampler { comparison: true } + ) }) - } - else { + } else { false } } @@ -3183,13 +3193,13 @@ impl<'a, W: Write> Writer<'a, W> { self.write_expr(expr, ctx)?; } - let needs_depth_fix = if let Expression::GlobalVariable(global_handle) = ctx.expressions[image] { - let ep_info = self.info.get_entry_point(self.entry_point_idx as usize); - self.needs_depth_fix(ep_info, global_handle, &class) - } - else { - false - }; + let needs_depth_fix = + if let Expression::GlobalVariable(global_handle) = ctx.expressions[image] { + let ep_info = self.info.get_entry_point(self.entry_point_idx as usize); + self.needs_depth_fix(ep_info, global_handle, &class) + } else { + false + }; match level { // Auto needs no more arguments @@ -3270,9 +3280,6 @@ impl<'a, W: Write> Writer<'a, W> { // parser thinks it will yield f32, but in reality it yields vec4f write!(self.out, ".x")?; } - - - // TODO: if depth texture but not comparison sampler, add ".x" } Expression::ImageLoad { image, From 248d622b2b19d09a44997bb9b9bfa07174f8f6f9 Mon Sep 17 00:00:00 2001 From: David Lenaerts Date: Sat, 4 Oct 2025 22:49:41 +0200 Subject: [PATCH 4/4] fix(gles): Fix some validations --- naga/src/back/glsl/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/naga/src/back/glsl/mod.rs b/naga/src/back/glsl/mod.rs index 0ffcb3659f..d39ae5153d 100644 --- a/naga/src/back/glsl/mod.rs +++ b/naga/src/back/glsl/mod.rs @@ -959,7 +959,7 @@ impl<'a, W: Write> Writer<'a, W> { // The trailing space is important write!(self.out, "uniform ")?; - if self.needs_depth_fix(ep_info, handle, &mut class) { + if self.needs_depth_fix(ep_info, handle, &class) { class = crate::ImageClass::Sampled { kind: crate::ScalarKind::Float, multi: false, @@ -1047,7 +1047,7 @@ impl<'a, W: Write> Writer<'a, W> { handle: Handle, class: &crate::ImageClass, ) -> bool { - if let crate::ImageClass::Depth { multi: false } = class { + if let crate::ImageClass::Depth { multi: false } = *class { // if any sampler uses the comparison sampler, a fix is not needed. Otherwise, we need to actually sample a regular texture as far as glsl is concerned !ep_info.sampling_set.iter().all(|key| { let data = &self.module.global_variables[key.sampler];