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] Optional parentheses for if and switch #1725

Merged
merged 2 commits into from
Feb 15, 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
8 changes: 4 additions & 4 deletions src/back/wgsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,9 +691,9 @@ impl<W: Write> Writer<W> {
ref reject,
} => {
write!(self.out, "{}", level)?;
write!(self.out, "if (")?;
write!(self.out, "if ")?;
self.write_expr(module, condition, func_ctx)?;
writeln!(self.out, ") {{")?;
writeln!(self.out, " {{")?;

let l2 = level.next();
for sta in accept {
Expand Down Expand Up @@ -845,9 +845,9 @@ impl<W: Write> Writer<W> {
} => {
// Start the switch
write!(self.out, "{}", level)?;
write!(self.out, "switch(")?;
write!(self.out, "switch ")?;
self.write_expr(module, selector, func_ctx)?;
writeln!(self.out, ") {{")?;
writeln!(self.out, " {{")?;

let type_postfix = match *func_ctx.info[selector].ty.inner_with(&module.types) {
crate::TypeInner::Scalar {
Expand Down
6 changes: 0 additions & 6 deletions src/front/wgsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3602,14 +3602,12 @@ impl Parser {
}
"if" => {
let _ = lexer.next();
lexer.expect(Token::Paren('('))?;
emitter.start(context.expressions);
let condition = self.parse_general_expression(
lexer,
context.as_expression(block, &mut emitter),
)?;
block.extend(emitter.finish(context.expressions));
lexer.expect(Token::Paren(')'))?;

let accept = self.parse_block(lexer, context.reborrow(), false)?;

Expand All @@ -3628,14 +3626,12 @@ impl Parser {
// ... else if (...) { ... }
let mut sub_emitter = super::Emitter::default();

lexer.expect(Token::Paren('('))?;
sub_emitter.start(context.expressions);
let other_condition = self.parse_general_expression(
lexer,
context.as_expression(block, &mut sub_emitter),
)?;
let other_emit = sub_emitter.finish(context.expressions);
lexer.expect(Token::Paren(')'))?;
let other_block = self.parse_block(lexer, context.reborrow(), false)?;
elsif_stack.push((
elseif_span_start,
Expand Down Expand Up @@ -3671,7 +3667,6 @@ impl Parser {
"switch" => {
let _ = lexer.next();
emitter.start(context.expressions);
lexer.expect(Token::Paren('('))?;
let selector = self.parse_general_expression(
lexer,
context.as_expression(block, &mut emitter),
Expand All @@ -3681,7 +3676,6 @@ impl Parser {
.as_expression(block, &mut emitter)
.resolve_type(selector)?
.scalar_kind();
lexer.expect(Token::Paren(')'))?;
block.extend(emitter.finish(context.expressions));
lexer.expect(Token::Paren('{'))?;
let mut cases = Vec::new();
Expand Down
39 changes: 37 additions & 2 deletions src/front/wgsl/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,26 @@ fn parse_statement() {

#[test]
fn parse_if() {
parse_str(
"
fn main() {
if true {
discard;
} else {}
if 0 != 1 {}
if false {
return;
} else if true {
return;
} else {}
}
",
)
.unwrap();
}

#[test]
fn parse_parentheses_if() {
parse_str(
"
fn main() {
Expand All @@ -231,11 +251,11 @@ fn parse_loop() {
fn main() {
var i: i32 = 0;
loop {
if (i == 1) { break; }
if i == 1 { break; }
continuing { i = 1; }
}
loop {
if (i == 0) { continue; }
if i == 0 { continue; }
break;
}
}
Expand Down Expand Up @@ -283,6 +303,21 @@ fn parse_switch() {
.unwrap();
}

#[test]
fn parse_parentheses_switch() {
parse_str(
"
fn main() {
var pos: f32;
switch pos > 1.0 {
default: { pos = 3.0; }
}
}
",
)
.unwrap();
}

#[test]
fn parse_texture_load() {
parse_str(
Expand Down
24 changes: 12 additions & 12 deletions tests/in/boids.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct Particles {
@stage(compute) @workgroup_size(64)
fn main(@builtin(global_invocation_id) global_invocation_id : vec3<u32>) {
let index : u32 = global_invocation_id.x;
if (index >= NUM_PARTICLES) {
if index >= NUM_PARTICLES {
return;
}

Expand All @@ -44,24 +44,24 @@ fn main(@builtin(global_invocation_id) global_invocation_id : vec3<u32>) {
var vel : vec2<f32>;
var i : u32 = 0u;
loop {
if (i >= NUM_PARTICLES) {
if i >= NUM_PARTICLES {
break;
}
if (i == index) {
if i == index {
continue;
}

pos = particlesSrc.particles[i].pos;
vel = particlesSrc.particles[i].vel;

if (distance(pos, vPos) < params.rule1Distance) {
if distance(pos, vPos) < params.rule1Distance {
cMass = cMass + pos;
cMassCount = cMassCount + 1;
}
if (distance(pos, vPos) < params.rule2Distance) {
if distance(pos, vPos) < params.rule2Distance {
colVel = colVel - (pos - vPos);
}
if (distance(pos, vPos) < params.rule3Distance) {
if distance(pos, vPos) < params.rule3Distance {
cVel = cVel + vel;
cVelCount = cVelCount + 1;
}
Expand All @@ -70,10 +70,10 @@ fn main(@builtin(global_invocation_id) global_invocation_id : vec3<u32>) {
i = i + 1u;
}
}
if (cMassCount > 0) {
if cMassCount > 0 {
cMass = cMass / f32(cMassCount) - vPos;
}
if (cVelCount > 0) {
if cVelCount > 0 {
cVel = cVel / f32(cVelCount);
}

Expand All @@ -88,16 +88,16 @@ fn main(@builtin(global_invocation_id) global_invocation_id : vec3<u32>) {
vPos = vPos + (vVel * params.deltaT);

// Wrap around boundary
if (vPos.x < -1.0) {
if vPos.x < -1.0 {
vPos.x = 1.0;
}
if (vPos.x > 1.0) {
if vPos.x > 1.0 {
vPos.x = -1.0;
}
if (vPos.y < -1.0) {
if vPos.y < -1.0 {
vPos.y = 1.0;
}
if (vPos.y > 1.0) {
if vPos.y > 1.0 {
vPos.y = -1.0;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/in/collatz.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ fn collatz_iterations(n_base: u32) -> u32 {
var n = n_base;
var i: u32 = 0u;
loop {
if (n <= 1u) {
if n <= 1u {
break;
}
if (n % 2u == 0u) {
if n % 2u == 0u {
n = n / 2u;
}
else {
Expand Down
10 changes: 5 additions & 5 deletions tests/in/control-flow.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {

var pos: i32;
// switch without cases
switch (1) {
switch 1 {
default: {
pos = 1;
}
}

// non-empty switch *not* in last-statement-in-function position
// (return statements might be inserted into the switch cases otherwise)
switch (pos) {
switch pos {
case 1: {
pos = 0;
break;
Expand All @@ -41,7 +41,7 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
}

// non-empty switch in last-statement-in-function position
switch (pos) {
switch pos {
case 1: {
pos = 0;
break;
Expand All @@ -61,7 +61,7 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
}

fn switch_default_break(i: i32) {
switch (i) {
switch i {
default: {
break;
}
Expand All @@ -80,7 +80,7 @@ fn switch_case_break() {

fn loop_switch_continue(x: i32) {
loop {
switch (x) {
switch x {
case 1: {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/in/extra.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct FragmentIn {

@stage(fragment)
fn main(in: FragmentIn) -> @location(0) vec4<f32> {
if (in.primitive_index % 2u == 0u) {
if in.primitive_index % 2u == 0u {
return in.color;
} else {
return vec4<f32>(vec3<f32>(1.0) - in.color.rgb, in.color.a);
Expand Down
2 changes: 1 addition & 1 deletion tests/in/operators.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn splat() -> vec4<f32> {

fn unary() -> i32 {
let a = 1;
if (!true) { return a; } else { return ~a; };
if !true { return a; } else { return ~a; };
}

fn bool_cast(x: vec3<f32>) -> vec3<f32> {
Expand Down
2 changes: 1 addition & 1 deletion tests/in/quad.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn vert_main(
@stage(fragment)
fn frag_main(@location(0) uv : vec2<f32>) -> @location(0) vec4<f32> {
let color = textureSample(u_texture, u_sampler, uv);
if (color.a == 0.0) {
if color.a == 0.0 {
discard;
}
// forcing the expression here to be emitted in order to check the
Expand Down
4 changes: 2 additions & 2 deletions tests/in/shadow.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var t_shadow: texture_depth_2d_array;
var sampler_shadow: sampler_comparison;

fn fetch_shadow(light_id: u32, homogeneous_coords: vec4<f32>) -> f32 {
if (homogeneous_coords.w <= 0.0) {
if homogeneous_coords.w <= 0.0 {
return 1.0;
}
let flip_correction = vec2<f32>(0.5, -0.5);
Expand All @@ -44,7 +44,7 @@ fn fs_main(
var color = c_ambient;
var i: u32 = 0u;
loop {
if (i >= min(u_globals.num_lights.x, c_max_lights)) {
if i >= min(u_globals.num_lights.x, c_max_lights) {
break;
}
let light = s_lights.data[i];
Expand Down
4 changes: 2 additions & 2 deletions tests/out/wgsl/246-collatz-comp.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ fn collatz_iterations(n: u32) -> u32 {
n_1 = n;
loop {
let _e7 = n_1;
if (!((_e7 != u32(1)))) {
if !((_e7 != u32(1))) {
break;
}
{
let _e14 = n_1;
if (((f32(_e14) % f32(2)) == f32(0))) {
if ((f32(_e14) % f32(2)) == f32(0)) {
{
let _e22 = n_1;
n_1 = (_e22 / u32(2));
Expand Down
2 changes: 1 addition & 1 deletion tests/out/wgsl/932-for-loop-if-vert.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fn main_1() {

loop {
let _e2 = i;
if (!((_e2 < 1))) {
if !((_e2 < 1)) {
break;
}
{
Expand Down
4 changes: 2 additions & 2 deletions tests/out/wgsl/bevy-pbr-frag.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ fn main_1() {
let _e224 = i;
let _e225 = global_2.NumLights;
let _e229 = i;
if (!(((_e224 < i32(_e225.x)) && (_e229 < 10)))) {
if !(((_e224 < i32(_e225.x)) && (_e229 < 10))) {
break;
}
{
Expand All @@ -828,7 +828,7 @@ fn main_1() {
let _e261 = i_1;
let _e262 = global_2.NumLights;
let _e266 = i_1;
if (!(((_e261 < i32(_e262.y)) && (_e266 < 1)))) {
if !(((_e261 < i32(_e262.y)) && (_e266 < 1))) {
break;
}
{
Expand Down
Loading