Add support for arrayLength to the wgsl frontend (#805)

* Add support for arrayLength to the wgsl frontend

* Fix clippy warning
This commit is contained in:
Lachlan Sneff
2021-04-30 00:20:09 -04:00
committed by GitHub
parent 37a6ff7bfe
commit 960551f952
3 changed files with 40 additions and 1 deletions

View File

@@ -741,6 +741,11 @@ impl Parser {
accept,
reject,
}
} else if name == "arrayLength" {
lexer.open_arguments()?;
let array = self.parse_singular_expression(lexer, ctx.reborrow())?;
lexer.close_arguments()?;
crate::Expression::ArrayLength(array)
} else {
// texture sampling
match name {

View File

@@ -304,3 +304,27 @@ fn parse_struct_instantiation() {
)
.unwrap();
}
#[test]
fn parse_array_length() {
parse_str(
"
[[block]]
struct Foo {
data: [[stride(4)]] array<u32>;
}; // this is used as both input and output for convenience
[[group(0), binding(0)]]
var<storage> foo: [[access(read_write)]] Foo;
[[group(0), binding(1)]]
var<storage> bar: [[access(read)]] array<u32>;
fn foo() {
var x: u32 = arrayLength(foo.data);
var y: u32 = arrayLength(bar);
}
",
)
.unwrap();
}

View File

@@ -1110,7 +1110,17 @@ impl super::Validator {
}
E::Call(function) => other_infos[function.index()].available_stages,
E::ArrayLength(expr) => match *resolver.resolve(expr)? {
Ti::Array { .. } => ShaderStages::all(),
Ti::Pointer { base, .. } => {
if let Some(&Ti::Array {
size: crate::ArraySize::Dynamic,
..
}) = resolver.types.try_get(base).map(|ty| &ty.inner)
{
ShaderStages::all()
} else {
return Err(ExpressionError::InvalidArrayType(expr));
}
}
ref other => {
log::error!("Array length of {:?}", other);
return Err(ExpressionError::InvalidArrayType(expr));