Skip to content

Commit

Permalink
glsl-in: fix composite constructors (#1631)
Browse files Browse the repository at this point in the history
* glsl-in: Remove unneeded mutability from reference

* glsl-in: Fix composite constructors

In the recent rework of the constructors it seems that the logic for
composite types (arrays and structs) was accidentally removed by me.
  • Loading branch information
JCapucho authored Dec 29, 2021
1 parent 2738ad8 commit 1d0f484
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 13 deletions.
10 changes: 5 additions & 5 deletions src/front/glsl/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ impl Context {

pub fn expr_scalar_components(
&mut self,
parser: &mut Parser,
parser: &Parser,
expr: Handle<Expression>,
meta: Span,
) -> Result<Option<(ScalarKind, crate::Bytes)>> {
Expand All @@ -891,7 +891,7 @@ impl Context {

pub fn expr_power(
&mut self,
parser: &mut Parser,
parser: &Parser,
expr: Handle<Expression>,
meta: Span,
) -> Result<Option<u32>> {
Expand Down Expand Up @@ -921,7 +921,7 @@ impl Context {

pub fn implicit_conversion(
&mut self,
parser: &mut Parser,
parser: &Parser,
expr: &mut Handle<Expression>,
meta: Span,
kind: ScalarKind,
Expand All @@ -941,7 +941,7 @@ impl Context {

pub fn binary_implicit_conversion(
&mut self,
parser: &mut Parser,
parser: &Parser,
left: &mut Handle<Expression>,
left_meta: Span,
right: &mut Handle<Expression>,
Expand Down Expand Up @@ -974,7 +974,7 @@ impl Context {

pub fn implicit_splat(
&mut self,
parser: &mut Parser,
parser: &Parser,
expr: &mut Handle<Expression>,
meta: Span,
vector_size: Option<VectorSize>,
Expand Down
61 changes: 53 additions & 8 deletions src/front/glsl/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,39 @@ impl Parser {
(value, expr_meta),
meta,
)?,
TypeInner::Struct { .. } | TypeInner::Array { .. } => ctx.add_expression(
Expression::Compose {
ty,
components: vec![value],
},
meta,
body,
),
TypeInner::Struct { ref members, .. } => {
let scalar_components = members
.get(0)
.and_then(|member| scalar_components(&self.module.types[member.ty].inner));
if let Some((kind, width)) = scalar_components {
ctx.implicit_conversion(self, &mut value, expr_meta, kind, width)?;
}

ctx.add_expression(
Expression::Compose {
ty,
components: vec![value],
},
meta,
body,
)
}

TypeInner::Array { base, .. } => {
let scalar_components = scalar_components(&self.module.types[base].inner);
if let Some((kind, width)) = scalar_components {
ctx.implicit_conversion(self, &mut value, expr_meta, kind, width)?;
}

ctx.add_expression(
Expression::Compose {
ty,
components: vec![value],
},
meta,
body,
)
}
_ => {
self.errors.push(Error {
kind: ErrorKind::SemanticError("Bad type constructor".into()),
Expand Down Expand Up @@ -535,6 +560,26 @@ impl Parser {
TypeInner::Vector { size, kind, width } => {
return self.vector_constructor(ctx, body, ty, size, kind, width, &args, meta)
}
TypeInner::Array { base, .. } => {
for (mut arg, meta) in args.iter().copied() {
let scalar_components = scalar_components(&self.module.types[base].inner);
if let Some((kind, width)) = scalar_components {
ctx.implicit_conversion(self, &mut arg, meta, kind, width)?;
}

components.push(arg)
}
}
TypeInner::Struct { ref members, .. } => {
for ((mut arg, meta), member) in args.iter().copied().zip(members.iter()) {
let scalar_components = scalar_components(&self.module.types[member.ty].inner);
if let Some((kind, width)) = scalar_components {
ctx.implicit_conversion(self, &mut arg, meta, kind, width)?;
}

components.push(arg)
}
}
_ => {
return Err(Error {
kind: ErrorKind::SemanticError("Constructor: Too many arguments".into()),
Expand Down
11 changes: 11 additions & 0 deletions tests/in/glsl/declarations.vert
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,16 @@ layout(location = 0) out FragmentData {
vec2 a;
} frag;

struct TestStruct {
float a;
float b;
};


void main() {
const vec3 positions[2] = vec3[2](
vec3(-1.0, 1.0, 0.0),
vec3(-1.0, -1.0, 0.0)
);
const TestStruct strct = TestStruct( 1, 2 );
}
8 changes: 8 additions & 0 deletions tests/out/wgsl/declarations-vert.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ struct FragmentData {
a: vec2<f32>;
};

struct TestStruct {
a: f32;
b: f32;
};

struct VertexOutput {
[[location(0)]] position: vec2<f32>;
[[location(1)]] a: vec2<f32>;
Expand All @@ -17,6 +22,9 @@ var<private> vert: VertexData;
var<private> frag: FragmentData;

fn main_1() {
var positions: array<vec3<f32>,2u> = array<vec3<f32>,2u>(vec3<f32>(-1.0, 1.0, 0.0), vec3<f32>(-1.0, -1.0, 0.0));
var strct: TestStruct = TestStruct(1.0, 2.0);

}

[[stage(vertex)]]
Expand Down

0 comments on commit 1d0f484

Please sign in to comment.