[glsl-in] Implicitly splat the inputs of clamp()

This commit is contained in:
Jasper St. Pierre authored and João Capucho committed 2021-07-01 18:32:33 +01:00
1 parent 8880faf360
commit cb8e9f2cad
4 files changed
+73 -2

No files matched your search

+19
View File
@@ -883,6 +883,25 @@ impl<'function> Context<'function> {
Ok(())
}
pub fn implicit_splat(
&mut self,
program: &mut Program,
expr: &mut Handle<Expression>,
meta: SourceMetadata,
vector_size: Option<VectorSize>,
) -> Result<(), ErrorKind> {
let expr_type = program.resolve_type(self, *expr, meta)?;
if let (&TypeInner::Scalar { .. }, Some(size)) = (expr_type, vector_size) {
*expr = self.expressions.append(Expression::Splat {
size,
value: *expr,
})
}
Ok(())
}
}
pub fn type_power(kind: ScalarKind) -> Option<u32> {
+28 -2
View File
@@ -444,14 +444,40 @@ impl Program<'_> {
},
))
}
"clamp" | "faceforward" | "refract" | "fma" | "smoothstep" => {
"clamp" => {
if args.len() != 3 {
return Err(ErrorKind::wrong_function_args(name, 3, args.len(), meta));
}
let (arg0, arg0_meta) = args[0];
let vector_size = match *(self.resolve_type(ctx, arg0, arg0_meta)?) {
TypeInner::Vector{ size, .. } => Some(size),
_ => None,
};
let (mut arg1, arg1_meta) = args[1];
ctx.implicit_splat(self, &mut arg1, arg1_meta, vector_size)?;
let (mut arg2, arg2_meta) = args[2];
ctx.implicit_splat(self, &mut arg2, arg2_meta, vector_size)?;
Ok(Some(ctx.add_expression(
Expression::Math {
fun: MathFunction::Clamp,
arg: arg0,
arg1: Some(arg1),
arg2: Some(arg2),
},
body,
)))
}
"faceforward" | "refract" | "fma" | "smoothstep" => {
if args.len() != 3 {
return Err(ErrorKind::wrong_function_args(name, 3, args.len(), meta));
}
Ok(Some(ctx.add_expression(
Expression::Math {
fun: match name.as_str() {
"clamp" => MathFunction::Clamp,
"faceforward" => MathFunction::FaceForward,
"refract" => MathFunction::Refract,
"fma" => MathFunction::Fma,
+6
View File
@@ -0,0 +1,6 @@
#version 450
layout(location = 0) in vec2 a_pos;
void main() {
gl_Position = vec4(clamp(a_pos, 0.0, 1.0), 0.0, 1.0);
}
+20
View File
@@ -0,0 +1,20 @@
struct VertexOutput {
[[builtin(position)]] member: vec4<f32>;
};
var<private> a_pos1: vec2<f32>;
var<private> gl_Position: vec4<f32>;
fn main1() {
let _e2: vec2<f32> = a_pos1;
gl_Position = vec4<f32>(clamp(_e2, vec2<f32>(0.0), vec2<f32>(1.0)), 0.0, 1.0);
return;
}
[[stage(vertex)]]
fn main([[location(0), interpolate(perspective)]] a_pos: vec2<f32>) -> VertexOutput {
a_pos1 = a_pos;
main1();
let _e3: vec4<f32> = gl_Position;
return VertexOutput(_e3);
}