Skip to content

Commit

Permalink
glsl-in: use forced conversions for vector/matrix constructors
Browse files Browse the repository at this point in the history
The spec defines that for vector and matrix constructors all arguments
should use the same conversions as scalar constructors (forced conversions)
  • Loading branch information
JCapucho authored and kvark committed Mar 28, 2022
1 parent 26537c1 commit 21f89b6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
19 changes: 19 additions & 0 deletions src/front/glsl/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,25 @@ impl Context {
Ok(())
}

pub fn forced_conversion(
&mut self,
parser: &Parser,
expr: &mut Handle<Expression>,
meta: Span,
kind: ScalarKind,
width: crate::Bytes,
) -> Result<()> {
if let Some((expr_scalar_kind, expr_width)) =
self.expr_scalar_components(parser, *expr, meta)?
{
if expr_scalar_kind != kind || expr_width != width {
self.conversion(expr, meta, kind, width)?;
}
}

Ok(())
}

pub fn binary_implicit_conversion(
&mut self,
parser: &Parser,
Expand Down
11 changes: 4 additions & 7 deletions src/front/glsl/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl Parser {

Ok(match self.module.types[ty].inner {
TypeInner::Vector { size, kind, width } if vector_size.is_none() => {
ctx.implicit_conversion(self, &mut value, meta, kind, width)?;
ctx.forced_conversion(self, &mut value, expr_meta, kind, width)?;

if let TypeInner::Scalar { .. } = *self.resolve_type(ctx, value, expr_meta)? {
ctx.add_expression(Expression::Splat { size, value }, meta, body)
Expand Down Expand Up @@ -256,7 +256,7 @@ impl Parser {
// `Expression::As` doesn't support matrix width
// casts so we need to do some extra work for casts

ctx.implicit_conversion(self, &mut value, expr_meta, ScalarKind::Float, width)?;
ctx.forced_conversion(self, &mut value, expr_meta, ScalarKind::Float, width)?;
match *self.resolve_type(ctx, value, expr_meta)? {
TypeInner::Scalar { .. } => {
// If a matrix is constructed with a single scalar value, then that
Expand Down Expand Up @@ -446,7 +446,7 @@ impl Parser {
let mut components = Vec::with_capacity(size as usize);

for (mut arg, expr_meta) in args.iter().copied() {
ctx.implicit_conversion(self, &mut arg, expr_meta, kind, width)?;
ctx.forced_conversion(self, &mut arg, expr_meta, kind, width)?;

if components.len() >= size as usize {
break;
Expand Down Expand Up @@ -512,10 +512,7 @@ impl Parser {
let mut flattened = Vec::with_capacity(columns as usize * rows as usize);

for (mut arg, meta) in args.iter().copied() {
let scalar_components = scalar_components(&self.module.types[ty].inner);
if let Some((kind, width)) = scalar_components {
ctx.implicit_conversion(self, &mut arg, meta, kind, width)?;
}
ctx.forced_conversion(self, &mut arg, meta, ScalarKind::Float, width)?;

match *self.resolve_type(ctx, arg, meta)? {
TypeInner::Vector { size, .. } => {
Expand Down
5 changes: 5 additions & 0 deletions tests/in/glsl/expressions.frag
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ void testFreestandingConstructor() {
vec4(1.0);
}

void testNonImplicitCastVectorCast() {
uint a = 1;
ivec4 b = ivec4(a);
}

float global;
void privatePointer(inout float a) {}

Expand Down
11 changes: 10 additions & 1 deletion tests/out/wgsl/expressions-frag.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,16 @@ fn testFreestandingConstructor() {
return;
}

fn privatePointer(a_18: ptr<function, f32>) {
fn testNonImplicitCastVectorCast() {
var a_18: u32 = 1u;
var b_16: vec4<i32>;

let _e3 = a_18;
b_16 = vec4<i32>(i32(_e3));
return;
}

fn privatePointer(a_19: ptr<function, f32>) {
return;
}

Expand Down

0 comments on commit 21f89b6

Please sign in to comment.