[wgsl-in] Uses commas to separate struct members instead of semicolons

This commit is contained in:
Igor Shaposhnik
2022-03-13 00:48:10 +03:00
committed by Dzmitry Malyshau
parent 7f4c01784c
commit 816fa347ad
20 changed files with 112 additions and 106 deletions

View File

@@ -2811,7 +2811,14 @@ impl Parser {
let mut members = Vec::new();
lexer.expect(Token::Paren('{'))?;
loop {
let mut ready = true;
while !lexer.skip(Token::Paren('}')) {
if !ready {
return Err(Error::Unexpected(
lexer.next(),
ExpectedToken::Token(Token::Separator(',')),
));
}
let (mut size, mut align) = (None, None);
self.push_scope(Scope::Attribute, lexer);
let mut bind_parser = BindingParser::default();
@@ -2838,10 +2845,6 @@ impl Parser {
let bind_span = self.pop_scope(lexer);
let (name, span) = match lexer.next() {
(Token::Word(word), span) => (word, span),
(Token::Paren('}'), _) => {
let span = Layouter::round_up(alignment, offset);
return Ok((members, span));
}
other => return Err(Error::Unexpected(other, ExpectedToken::FieldName)),
};
if crate::keywords::wgsl::RESERVED.contains(&name) {
@@ -2849,7 +2852,7 @@ impl Parser {
}
lexer.expect(Token::Separator(':'))?;
let (ty, _access) = self.parse_type_decl(lexer, None, type_arena, const_arena)?;
lexer.expect(Token::Separator(';'))?;
ready = lexer.skip(Token::Separator(','));
self.layouter.update(type_arena, const_arena).unwrap();
@@ -2868,6 +2871,9 @@ impl Parser {
offset: range.start,
});
}
let span = Layouter::round_up(alignment, offset);
Ok((members, span))
}
fn parse_type_decl_impl<'a>(

View File

@@ -157,11 +157,11 @@ fn parse_type_cast() {
fn parse_struct() {
parse_str(
"
struct Foo { x: i32; };
struct Foo { x: i32 };
struct Bar {
@size(16) x: vec2<i32>;
@align(16) y: f32;
@size(32) @align(8) z: vec3<f32>;
@size(16) x: vec2<i32>,
@align(16) y: f32,
@size(32) @align(8) z: vec3<f32>,
};
struct Empty {};
var<storage,read_write> s: Foo;
@@ -415,8 +415,8 @@ fn parse_struct_instantiation() {
parse_str(
"
struct Foo {
a: f32;
b: vec3<f32>;
a: f32,
b: vec3<f32>,
};
@stage(fragment)
@@ -433,7 +433,7 @@ fn parse_array_length() {
parse_str(
"
struct Foo {
data: array<u32>;
data: array<u32>
}; // this is used as both input and output for convenience
@group(0) @binding(0)

View File

@@ -1,15 +1,15 @@
// This snapshot tests accessing various containers, dereferencing pointers.
struct AlignedWrapper {
@align(8) value: i32;
@align(8) value: i32
};
struct Bar {
matrix: mat4x4<f32>;
matrix_array: array<mat2x2<f32>, 2>;
atom: atomic<i32>;
arr: array<vec2<u32>, 2>;
data: array<AlignedWrapper>;
matrix: mat4x4<f32>,
matrix_array: array<mat2x2<f32>, 2>,
atom: atomic<i32>,
arr: array<vec2<u32>, 2>,
data: array<AlignedWrapper>,
};
@group(0) @binding(0)

View File

@@ -1,22 +1,22 @@
let NUM_PARTICLES: u32 = 1500u;
struct Particle {
pos : vec2<f32>;
vel : vec2<f32>;
pos : vec2<f32>,
vel : vec2<f32>,
};
struct SimParams {
deltaT : f32;
rule1Distance : f32;
rule2Distance : f32;
rule3Distance : f32;
rule1Scale : f32;
rule2Scale : f32;
rule3Scale : f32;
deltaT : f32,
rule1Distance : f32,
rule2Distance : f32,
rule3Distance : f32,
rule1Scale : f32,
rule2Scale : f32,
rule3Scale : f32,
};
struct Particles {
particles : array<Particle>;
particles : array<Particle>
};
@group(0) @binding(0) var<uniform> params : SimParams;

View File

@@ -1,10 +1,10 @@
// Tests for `naga::back::BoundsCheckPolicy::Restrict`.
struct Globals {
a: array<f32, 10>;
v: vec4<f32>;
m: mat3x4<f32>;
d: array<f32>;
a: array<f32, 10>,
v: vec4<f32>,
m: mat3x4<f32>,
d: array<f32>,
};
@group(0) @binding(0) var<storage, read_write> globals: Globals;

View File

@@ -5,9 +5,9 @@
// be combined.
struct Globals {
a: atomic<u32>;
b: array<atomic<u32>, 10>;
c: array<atomic<u32>>;
a: atomic<u32>,
b: array<atomic<u32>, 10>,
c: array<atomic<u32>>,
};
@group(0) @binding(0) var<storage, read_write> globals: Globals;

View File

@@ -1,10 +1,10 @@
// Tests for `naga::back::BoundsCheckPolicy::ReadZeroSkipWrite`.
struct Globals {
a: array<f32, 10>;
v: vec4<f32>;
m: mat3x4<f32>;
d: array<f32>;
a: array<f32, 10>,
v: vec4<f32>,
m: mat3x4<f32>,
d: array<f32>,
};
@group(0) @binding(0) var<storage, read_write> globals: Globals;

View File

@@ -1,5 +1,5 @@
struct PrimeIndices {
data: array<u32>;
data: array<u32>
}; // this is used as both input and output for convenience
@group(0) @binding(0)

View File

@@ -1,12 +1,12 @@
struct PushConstants {
index: u32;
double: vec2<f64>;
index: u32,
double: vec2<f64>,
};
var<push_constant> pc: PushConstants;
struct FragmentIn {
@location(0) color: vec4<f32>;
@builtin(primitive_index) primitive_index: u32;
@location(0) color: vec4<f32>,
@builtin(primitive_index) primitive_index: u32,
};
@stage(fragment)

View File

@@ -6,9 +6,9 @@ var<workgroup> wg : array<f32, 10u>;
var<workgroup> at: atomic<u32>;
struct Foo {
v3: vec3<f32>;
v3: vec3<f32>,
// test packed vec3
v1: f32;
v1: f32,
};
@group(0) @binding(1)
var<storage, read_write> alignment: Foo;

View File

@@ -1,8 +1,8 @@
// Testing various parts of the pipeline interface: locations, built-ins, and entry points
struct VertexOutput {
@builtin(position) position: vec4<f32>;
@location(1) varying: f32;
@builtin(position) position: vec4<f32>,
@location(1) varying: f32,
};
@stage(vertex)
@@ -16,9 +16,9 @@ fn vertex(
}
struct FragmentOutput {
@builtin(frag_depth) depth: f32;
@builtin(sample_mask) sample_mask: u32;
@location(0) color: f32;
@builtin(frag_depth) depth: f32,
@builtin(sample_mask) sample_mask: u32,
@location(0) color: f32,
};
@stage(fragment)
@@ -47,11 +47,11 @@ fn compute(
}
struct Input1 {
@builtin(vertex_index) index: u32;
@builtin(vertex_index) index: u32,
};
struct Input2 {
@builtin(instance_index) index: u32;
@builtin(instance_index) index: u32,
};
@stage(vertex)

View File

@@ -1,14 +1,14 @@
//TODO: merge with "interface"?
struct FragmentInput {
@builtin(position) position: vec4<f32>;
@location(0) @interpolate(flat) flat : u32;
@location(1) @interpolate(linear) linear : f32;
@location(2) @interpolate(linear, centroid) linear_centroid : vec2<f32>;
@location(3) @interpolate(linear, sample) linear_sample : vec3<f32>;
@location(4) @interpolate(perspective) perspective : vec4<f32>;
@location(5) @interpolate(perspective, centroid) perspective_centroid : f32;
@location(6) @interpolate(perspective, sample) perspective_sample : f32;
@builtin(position) position: vec4<f32>,
@location(0) @interpolate(flat) flat : u32,
@location(1) @interpolate(linear) linear : f32,
@location(2) @interpolate(linear, centroid) linear_centroid : vec2<f32>,
@location(3) @interpolate(linear, sample) linear_sample : vec3<f32>,
@location(4) @interpolate(perspective) perspective : vec4<f32>,
@location(5) @interpolate(perspective, centroid) perspective_centroid : f32,
@location(6) @interpolate(perspective, sample) perspective_sample : f32,
};
@stage(vertex)

View File

@@ -39,8 +39,8 @@ fn bool_cast(x: vec3<f32>) -> vec3<f32> {
}
struct Foo {
a: vec4<f32>;
b: i32;
a: vec4<f32>,
b: i32,
};
fn constructors() -> f32 {

View File

@@ -5,7 +5,7 @@ fn f() {
}
struct DynamicArray {
arr: array<u32>;
arr: array<u32>
};
@group(0) @binding(0)

View File

@@ -3,12 +3,12 @@
// Storage and Uniform storage classes
struct InStorage {
a: array<vec4<f32>, 10>;
a: array<vec4<f32>, 10>
};
@group(0) @binding(0) var<storage> in_storage: InStorage;
struct InUniform {
a: array<vec4<f32>, 20>;
a: array<vec4<f32>, 20>
};
@group(0) @binding(1) var<uniform> in_uniform: InUniform;

View File

@@ -1,10 +1,10 @@
struct PushConstants {
multiplier: f32;
multiplier: f32
};
var<push_constant> pc: PushConstants;
struct FragmentIn {
@location(0) color: vec4<f32>;
@location(0) color: vec4<f32>
};
@stage(fragment)

View File

@@ -2,8 +2,8 @@
let c_scale: f32 = 1.2;
struct VertexOutput {
@location(0) uv : vec2<f32>;
@builtin(position) position : vec4<f32>;
@location(0) uv : vec2<f32>,
@builtin(position) position : vec4<f32>,
};
@stage(vertex)

View File

@@ -1,6 +1,6 @@
struct Globals {
view_proj: mat4x4<f32>;
num_lights: vec4<u32>;
view_proj: mat4x4<f32>,
num_lights: vec4<u32>,
};
@group(0)
@@ -8,8 +8,8 @@ struct Globals {
var<uniform> u_globals: Globals;
struct Entity {
world: mat4x4<f32>;
color: vec4<f32>;
world: mat4x4<f32>,
color: vec4<f32>,
};
@group(1)
@@ -24,9 +24,9 @@ fn vs_bake(@location(0) position: vec4<i32>) -> @builtin(position) vec4<f32> {
*/
struct VertexOutput {
@builtin(position) proj_position: vec4<f32>;
@location(0) world_normal: vec3<f32>;
@location(1) world_position: vec4<f32>;
@builtin(position) proj_position: vec4<f32>,
@location(0) world_normal: vec3<f32>,
@location(1) world_position: vec4<f32>,
};
@stage(vertex)
@@ -46,9 +46,9 @@ fn vs_main(
// fragment shader
struct Light {
proj: mat4x4<f32>;
pos: vec4<f32>;
color: vec4<f32>;
proj: mat4x4<f32>,
pos: vec4<f32>,
color: vec4<f32>,
};
@group(0)

View File

@@ -1,11 +1,11 @@
struct VertexOutput {
@builtin(position) position: vec4<f32>;
@location(0) uv: vec3<f32>;
@builtin(position) position: vec4<f32>,
@location(0) uv: vec3<f32>,
};
struct Data {
proj_inv: mat4x4<f32>;
view: mat4x4<f32>;
proj_inv: mat4x4<f32>,
view: mat4x4<f32>,
};
@group(0) @binding(0)
var<uniform> r_data: Data;

View File

@@ -376,13 +376,13 @@ fn struct_member_zero_size() {
check(
r#"
struct Bar {
@size(0) data: array<f32>;
@size(0) data: array<f32>
};
"#,
r#"error: struct member size or alignment must not be 0
┌─ wgsl:3:23
3 │ @size(0) data: array<f32>;
3 │ @size(0) data: array<f32>
│ ^ struct member size or alignment must not be 0
"#,
@@ -394,13 +394,13 @@ fn struct_member_zero_align() {
check(
r#"
struct Bar {
@align(0) data: array<f32>;
@align(0) data: array<f32>
};
"#,
r#"error: struct member size or alignment must not be 0
┌─ wgsl:3:24
3 │ @align(0) data: array<f32>;
3 │ @align(0) data: array<f32>
│ ^ struct member size or alignment must not be 0
"#,
@@ -542,7 +542,7 @@ fn postfix_pointers() {
check(
r#"
struct S { m: i32; };
struct S { m: i32 };
fn main() {
var s: S = S(42);
let ps = &s;
@@ -655,12 +655,12 @@ fn reserved_keyword() {
// struct member
check(
r#"
struct Foo { sampler: f32; };
struct Foo { sampler: f32 };
"#,
r###"error: name `sampler` is a reserved keyword
┌─ wgsl:2:26
2 │ struct Foo { sampler: f32; };
2 │ struct Foo { sampler: f32 };
│ ^^^^^^^ definition of `sampler`
"###,
@@ -843,8 +843,8 @@ fn invalid_arrays() {
#[test]
fn invalid_structs() {
check_validation_error! {
"struct Bad { data: sampler; };",
"struct Bad { data: texture_2d<f32>; };":
"struct Bad { data: sampler };",
"struct Bad { data: texture_2d<f32> };":
Err(naga::valid::ValidationError::Type {
error: naga::valid::TypeError::InvalidData(_),
..
@@ -852,7 +852,7 @@ fn invalid_structs() {
}
check_validation_error! {
"struct Bad { data: array<f32>; other: f32; };":
"struct Bad { data: array<f32>, other: f32, };":
Err(naga::valid::ValidationError::Type {
error: naga::valid::TypeError::InvalidDynamicArray(_, _),
..
@@ -865,7 +865,7 @@ fn invalid_functions() {
check_validation_error! {
"fn unacceptable_unsized(arg: array<f32>) { }",
"
struct Unsized { data: array<f32>; };
struct Unsized { data: array<f32> };
fn unacceptable_unsized(arg: Unsized) { }
":
Err(naga::valid::ValidationError::Function {
@@ -883,7 +883,7 @@ fn invalid_functions() {
check_validation_error! {
"fn unacceptable_unsized(arg: ptr<workgroup, array<f32>>) { }",
"
struct Unsized { data: array<f32>; };
struct Unsized { data: array<f32> };
fn unacceptable_unsized(arg: ptr<workgroup, Unsized>) { }
":
Err(naga::valid::ValidationError::Type {
@@ -998,8 +998,8 @@ fn missing_bindings() {
check_validation_error! {
"
struct VertexIn {
@location(0) pos: vec4<f32>;
uv: vec2<f32>;
@location(0) pos: vec4<f32>,
uv: vec2<f32>
};
@stage(vertex)
@@ -1088,7 +1088,7 @@ fn valid_access() {
fn invalid_local_vars() {
check_validation_error! {
"
struct Unsized { data: array<f32>; };
struct Unsized { data: array<f32> };
fn local_ptr_dynamic_array(okay: ptr<storage, Unsized>) {
var not_okay: ptr<storage, array<f32>> = &(*okay).data;
}
@@ -1144,12 +1144,12 @@ fn invalid_runtime_sized_arrays() {
check_validation_error! {
"
struct Unsized {
arr: array<f32>;
arr: array<f32>
};
struct Outer {
legit: i32;
unsized: Unsized;
legit: i32,
unsized: Unsized
};
@group(0) @binding(0) var<storage> outer: Outer;
@@ -1187,7 +1187,7 @@ fn select() {
}
",
"
struct S { member: i32; };
struct S { member: i32 };
fn select_structs(which: bool) -> S {
var x: S = S(1);
var y: S = S(2);
@@ -1257,7 +1257,7 @@ fn wrong_access_mode() {
check_validation_error! {
"
struct Globals {
i: i32;
i: i32
};
@group(0) @binding(0)
@@ -1269,7 +1269,7 @@ fn wrong_access_mode() {
",
"
struct Globals {
i: i32;
i: i32
};
@group(0) @binding(0)