From f586d3e240a3e35f7f74e8da3c41c817b88decb3 Mon Sep 17 00:00:00 2001 From: Igor Shaposhnik Date: Mon, 8 Mar 2021 19:45:04 +0000 Subject: [PATCH] [glsl-out] Workaround lod array shadow as textureGrad --- src/back/glsl/mod.rs | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/back/glsl/mod.rs b/src/back/glsl/mod.rs index e97e5bda48..b9ef8fc276 100644 --- a/src/back/glsl/mod.rs +++ b/src/back/glsl/mod.rs @@ -1340,12 +1340,24 @@ impl<'a, W: Write> Writer<'a, W> { } => { //TODO: handle MS + // textureLod on sampler2DArrayShadow and samplerCubeShadow does not exist in GLSL. + // To emulate this, we will have to use textureGrad with a constant gradient of 0. + let workaround_lod_array_shadow_as_grad = + array_index.is_some() && depth_ref.is_some(); + //Write the function to be used depending on the sample level let fun_name = match level { crate::SampleLevel::Auto | crate::SampleLevel::Bias(_) => "texture", - crate::SampleLevel::Zero | crate::SampleLevel::Exact(_) => "textureLod", + crate::SampleLevel::Zero | crate::SampleLevel::Exact(_) => { + if workaround_lod_array_shadow_as_grad { + "textureGrad" + } else { + "textureLod" + } + } crate::SampleLevel::Gradient { .. } => "textureGrad", }; + write!(self.out, "{}(", fun_name)?; // Write the image that will be used @@ -1385,9 +1397,23 @@ impl<'a, W: Write> Writer<'a, W> { // Auto needs no more arguments crate::SampleLevel::Auto => (), // Zero needs level set to 0 - crate::SampleLevel::Zero => write!(self.out, ", 0")?, + crate::SampleLevel::Zero => { + if workaround_lod_array_shadow_as_grad { + write!(self.out, ", vec2(0, 0), vec2(0,0)")?; + } else { + write!(self.out, ", 0")?; + } + } // Exact and bias require another argument - crate::SampleLevel::Exact(expr) | crate::SampleLevel::Bias(expr) => { + crate::SampleLevel::Exact(expr) => { + if workaround_lod_array_shadow_as_grad { + write!(self.out, ", vec2(0, 0), vec2(0,0)")?; + } else { + write!(self.out, ", ")?; + self.write_expr(expr, ctx)?; + } + } + crate::SampleLevel::Bias(expr) => { write!(self.out, ", ")?; self.write_expr(expr, ctx)?; }