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: use forced conversions for vector/matrix constructors #1796

Merged
merged 1 commit into from
Mar 28, 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
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