[wgsl] support select()

This commit is contained in:
Dzmitry Malyshau 2021-02-11 15:25:20 -05:00
parent b3d80de9e1
commit 86bb611905
3 changed files with 29 additions and 3 deletions

View File

@ -99,6 +99,7 @@ pub fn get_scalar_type(word: &str) -> Option<(crate::ScalarKind, crate::Bytes)>
"f32" => Some((crate::ScalarKind::Float, 4)),
"i32" => Some((crate::ScalarKind::Sint, 4)),
"u32" => Some((crate::ScalarKind::Uint, 4)),
"bool" => Some((crate::ScalarKind::Bool, 1)),
_ => None,
}
}

View File

@ -443,6 +443,19 @@ impl Parser {
arg1,
arg2,
})
} else if name == "select" {
lexer.expect(Token::Paren('('))?;
let accept = self.parse_general_expression(lexer, ctx.reborrow())?;
lexer.expect(Token::Separator(','))?;
let reject = self.parse_general_expression(lexer, ctx.reborrow())?;
lexer.expect(Token::Separator(','))?;
let condition = self.parse_general_expression(lexer, ctx.reborrow())?;
lexer.expect(Token::Paren(')'))?;
Some(crate::Expression::Select {
condition,
accept,
reject,
})
} else {
// texture sampling
match name {

View File

@ -197,7 +197,19 @@ fn parse_texture_query() {
#[test]
fn parse_postfix() {
parse_str("fn foo() { const x: f32 = vec4<f32>(1.0, 2.0, 3.0, 4.0).xyz.rgbr.aaaa.wz.g; }")
.unwrap();
parse_str("fn foo() { const x: f32 = fract(vec2<f32>(0.5, 1.0)).x; }").unwrap();
parse_str(
"fn foo() {
const x: f32 = vec4<f32>(1.0, 2.0, 3.0, 4.0).xyz.rgbr.aaaa.wz.g;
const y: f32 = fract(vec2<f32>(0.5, x)).x;
}",
)
.unwrap();
}
#[test]
fn parse_expressions() {
parse_str("fn foo() {
const x: f32 = select(0.0, 1.0, true);
const y: vec2<f32> = select(vec2<f32>(1.0, 1.0), vec2<f32>(x, x), vec2<bool>(x < 0.5, x > 0.5));
}").unwrap();
}