Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

glsl-in: Fix parameter not changing to depth #1967

Merged
merged 2 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/front/glsl/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2237,20 +2237,26 @@ pub fn sampled_to_depth(
meta: Span,
errors: &mut Vec<Error>,
) {
// Get the a mutable type handle of the underlying image storage
let ty = match ctx[image] {
Expression::GlobalVariable(handle) => &mut module.global_variables.get_mut(handle).ty,
Expression::FunctionArgument(i) => {
// Mark the function argument as carrying a depth texture
ctx.parameters_info[i as usize].depth = true;
// NOTE: We need to later also change the parameter type
&mut ctx.arguments[i as usize].ty
}
_ => {
// Only globals and function arguments are allowed to carry an image
return errors.push(Error {
kind: ErrorKind::SemanticError("Not a valid texture expression".into()),
meta,
})
});
}
};

match module.types[*ty].inner {
// Update the image class to depth in case it already isn't
TypeInner::Image {
class,
dim,
Expand All @@ -2270,6 +2276,7 @@ pub fn sampled_to_depth(
)
}
ImageClass::Depth { .. } => {}
// Other image classes aren't allowed to be transformed to depth
_ => errors.push(Error {
kind: ErrorKind::SemanticError("Not a texture".into()),
meta,
Expand All @@ -2280,6 +2287,15 @@ pub fn sampled_to_depth(
meta,
}),
};

// Copy the handle to allow borrowing the `ctx` again
let ty = *ty;

// If the image was passed trough a function argument we also need to change
// the corresponding parameter
if let Expression::FunctionArgument(i) = ctx[image] {
ctx.parameters[i as usize] = ty;
}
}

bitflags::bitflags! {
Expand Down
7 changes: 7 additions & 0 deletions src/front/glsl/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,13 @@ impl Parser {
let overload_param_ty = &self.module.types[*overload_parameter].inner;
let call_arg_ty = self.resolve_type(ctx, call_argument.0, call_argument.1)?;

log::trace!(
"Testing parameter {}\n\tOverload = {:?}\n\tCall = {:?}",
i,
overload_param_ty,
call_arg_ty
);

// Storage images cannot be directly compared since while the access is part of the
// type in naga's IR, in glsl they are a qualifier and don't enter in the match as
// long as the access needed is satisfied.
Expand Down
16 changes: 16 additions & 0 deletions tests/in/glsl/sampler-functions.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#version 440
precision mediump float;

float CalcShadowPCF1(texture2D T_P_t_TextureDepth, samplerShadow S_P_t_TextureDepth, in vec3 t_ProjCoord) {
float t_Res = 0.0f;
t_Res += texture(sampler2DShadow(T_P_t_TextureDepth, S_P_t_TextureDepth), t_ProjCoord.xyz) * (1.0 / 5.0);
return t_Res;
}

float CalcShadowPCF(texture2D T_P_t_TextureDepth, samplerShadow S_P_t_TextureDepth, in vec3 t_ProjCoord, in float t_Bias) {
t_ProjCoord.z += t_Bias;
return CalcShadowPCF1(T_P_t_TextureDepth, S_P_t_TextureDepth, t_ProjCoord.xyz);
}

void main() {
}
41 changes: 41 additions & 0 deletions tests/out/wgsl/sampler-functions-frag.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
fn CalcShadowPCF1_(T_P_t_TextureDepth: texture_depth_2d, S_P_t_TextureDepth: sampler_comparison, t_ProjCoord: vec3<f32>) -> f32 {
var t_ProjCoord_1: vec3<f32>;
var t_Res: f32 = 0.0;

t_ProjCoord_1 = t_ProjCoord;
let _e6 = t_Res;
let _e7 = t_ProjCoord_1;
_ = _e7.xyz;
let _e9 = t_ProjCoord_1;
let _e10 = _e9.xyz;
let _e13 = textureSampleCompare(T_P_t_TextureDepth, S_P_t_TextureDepth, _e10.xy, _e10.z);
t_Res = (_e6 + (_e13 * (1.0 / 5.0)));
let _e19 = t_Res;
return _e19;
}

fn CalcShadowPCF(T_P_t_TextureDepth_1: texture_depth_2d, S_P_t_TextureDepth_1: sampler_comparison, t_ProjCoord_2: vec3<f32>, t_Bias: f32) -> f32 {
var t_ProjCoord_3: vec3<f32>;
var t_Bias_1: f32;

t_ProjCoord_3 = t_ProjCoord_2;
t_Bias_1 = t_Bias;
let _e7 = t_ProjCoord_3;
let _e9 = t_Bias_1;
t_ProjCoord_3.z = (_e7.z + _e9);
let _e11 = t_ProjCoord_3;
_ = _e11.xyz;
let _e13 = t_ProjCoord_3;
let _e15 = CalcShadowPCF1_(T_P_t_TextureDepth_1, S_P_t_TextureDepth_1, _e13.xyz);
return _e15;
}

fn main_1() {
return;
}

@fragment
fn main() {
main_1();
return;
}