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

[wgsl] Uses commas to separate struct members instead of semicolons #1773

Merged
merged 3 commits into from
Mar 13, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## v0.9 (TBD)
- WGSL:
- commas are used to separate struct members intead of semicolons
- attributes are declared with `@attrib` instead of `[[attrib]]`
- `stride` attribute is removed
- block comments are supported
Expand Down
2 changes: 1 addition & 1 deletion src/back/wgsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ impl<W: Write> Writer<W> {
let member_name = &self.names[&NameKey::StructMember(handle, index as u32)];
write!(self.out, "{}: ", member_name)?;
self.write_type(module, member.ty)?;
write!(self.out, ";")?;
write!(self.out, ",")?;
writeln!(self.out)?;
}

Expand Down
18 changes: 12 additions & 6 deletions src/front/wgsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -2838,18 +2845,14 @@ 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) {
return Err(Error::ReservedKeyword(span));
}
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();

Expand All @@ -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>(
Expand Down
14 changes: 7 additions & 7 deletions src/front/wgsl/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -415,8 +415,8 @@ fn parse_struct_instantiation() {
parse_str(
"
struct Foo {
a: f32;
b: vec3<f32>;
a: f32,
b: vec3<f32>,
};

@stage(fragment)
Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions tests/in/access.wgsl
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
20 changes: 10 additions & 10 deletions tests/in/boids.wgsl
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
8 changes: 4 additions & 4 deletions tests/in/bounds-check-restrict.wgsl
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
6 changes: 3 additions & 3 deletions tests/in/bounds-check-zero-atomic.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions tests/in/bounds-check-zero.wgsl
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/in/collatz.wgsl
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
8 changes: 4 additions & 4 deletions tests/in/extra.wgsl
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tests/in/globals.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions tests/in/interface.wgsl
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions tests/in/interpolate.wgsl
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tests/in/operators.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion tests/in/pointers.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn f() {
}

struct DynamicArray {
arr: array<u32>;
arr: array<u32>
};

@group(0) @binding(0)
Expand Down
4 changes: 2 additions & 2 deletions tests/in/policy-mix.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions tests/in/push-constants.wgsl
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tests/in/quad.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions tests/in/shadow.wgsl
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
struct Globals {
view_proj: mat4x4<f32>;
num_lights: vec4<u32>;
view_proj: mat4x4<f32>,
num_lights: vec4<u32>,
};

@group(0)
@binding(0)
var<uniform> u_globals: Globals;

struct Entity {
world: mat4x4<f32>;
color: vec4<f32>;
world: mat4x4<f32>,
color: vec4<f32>,
};

@group(1)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Loading