Skip to content

Commit

Permalink
glsl-in: Splat inputs for smoothstep if needed
Browse files Browse the repository at this point in the history
Glsl defines two overloads for smoothstep that accept `min` and `max` as
scalars and the value as a vector, naga's IR is stricter and only allows
operators with the same dimensions, so this inputs must be splatted.
  • Loading branch information
JCapucho committed Jun 7, 2022
1 parent 89bed99 commit d071e0c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/front/glsl/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,7 @@ fn inject_common_builtin(
};
declaration.overloads.push(module.add_builtin(
vec![ty(), ty(), base_ty()],
MacroCall::MathFunction(MathFunction::SmoothStep),
MacroCall::SmoothStep { splatted: size },
))
}
}
Expand Down Expand Up @@ -1604,6 +1604,12 @@ pub enum MacroCall {
BitCast(Sk),
Derivate(DerivativeAxis),
Barrier,
/// SmoothStep needs a separate variant because it might need it's inputs
/// to be splatted depending on the overload
SmoothStep {
/// The size of the splat operation if some
splatted: Option<VectorSize>,
},
}

impl MacroCall {
Expand Down Expand Up @@ -2072,6 +2078,22 @@ impl MacroCall {
body.push(crate::Statement::Barrier(crate::Barrier::all()), meta);
return Ok(None);
}
MacroCall::SmoothStep { splatted } => {
ctx.implicit_splat(parser, &mut args[0], meta, splatted)?;
ctx.implicit_splat(parser, &mut args[1], meta, splatted)?;

ctx.add_expression(
Expression::Math {
fun: MathFunction::SmoothStep,
arg: args[0],
arg1: args.get(1).copied(),
arg2: args.get(2).copied(),
arg3: None,
},
Span::default(),
body,
)
}
}))
}
}
Expand Down
3 changes: 3 additions & 0 deletions tests/in/glsl/math-functions.vert
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,7 @@ void main() {
// float ldexpOut = ldexp(a.x, i);
vec4 rad = radians(a);
float deg = degrees(a.x);
float smoothStepScalar = smoothstep(0.0, 1.0, 0.5);
vec4 smoothStepVector = smoothstep(vec4(0.0), vec4(1.0), vec4(0.5));
vec4 smoothStepMixed = smoothstep(0.0, 1.0, vec4(0.5));
}

0 comments on commit d071e0c

Please sign in to comment.