Skip to content

Commit

Permalink
Include suggested documentation, more binding array tests, enforce st…
Browse files Browse the repository at this point in the history
…ructs
  • Loading branch information
kvark committed Apr 24, 2023
1 parent 26c6e14 commit 74dbb64
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/back/spv/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1405,8 +1405,8 @@ impl<'w> BlockContext<'w> {
/// Emit any needed bounds-checking expressions to `block`.
///
/// Some cases we need to generate a different return type than what the IR gives us.
/// This is because pointers to binding arrays don't exist in the IR, but we need to
/// create them to create an access chain in SPIRV.
/// This is because pointers to binding arrays of handles (such as images or samplers)
/// don't exist in the IR, but we need to create them to create an access chain in SPIRV.
///
/// On success, the return value is an [`ExpressionPointer`] value; see the
/// documentation for that type.
Expand Down
5 changes: 5 additions & 0 deletions src/back/spv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@ enum LocalType {
image_type_id: Word,
},
Sampler,
/// Equivalent to a [`LocalType::Pointer`] whose `base` is a Naga IR [`BindingArray`]. SPIR-V
/// permits duplicated `OpTypePointer` ids, so it's fine to have two different [`LocalType`]
/// representations for pointer types.
///
/// [`BindingArray`]: crate::TypeInner::BindingArray
PointerToBindingArray {
base: Handle<crate::Type>,
size: u64,
Expand Down
3 changes: 3 additions & 0 deletions src/valid/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,9 @@ impl super::Validator {

log::debug!("var {:?}", var);
let inner_ty = match types[var.ty].inner {
// A binding array is (mostly) supposed to behave the same as a
// series of individually bound resources, so we can (mostly)
// validate a `binding_array<T>` as if it were just a plain `T`.
crate::TypeInner::BindingArray { base, .. } => base,
_ => var.ty,
};
Expand Down
12 changes: 11 additions & 1 deletion src/valid/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ pub enum TypeError {
InvalidArrayStride { stride: u32, expected: u32 },
#[error("Field '{0}' can't be dynamically-sized, has type {1:?}")]
InvalidDynamicArray(String, Handle<crate::Type>),
#[error("The base handle {0:?} has to be a struct")]
BindingArrayBaseTypeNotStruct(Handle<crate::Type>),
#[error("Structure member[{index}] at {offset} overlaps the previous member")]
MemberOverlap { index: u32, offset: u32 },
#[error(
Expand Down Expand Up @@ -612,7 +614,7 @@ impl super::Validator {
}
Ti::AccelerationStructure => {
self.require_type_capability(Capabilities::RAY_QUERY)?;
TypeInfo::new(TypeFlags::empty(), Alignment::ONE)
TypeInfo::new(TypeFlags::ARGUMENT, Alignment::ONE)
}
Ti::RayQuery => {
self.require_type_capability(Capabilities::RAY_QUERY)?;
Expand All @@ -631,6 +633,14 @@ impl super::Validator {
};
let base_info = &self.types[base.index()];

if base_info.flags.contains(TypeFlags::DATA) {
// Currently Naga only supports binding arrays of structs for non-handle types.
match types[base].inner {
crate::TypeInner::Struct { .. } => {}
_ => return Err(TypeError::BindingArrayBaseTypeNotStruct(base)),
};
}

TypeInfo::new(base_info.flags & type_info_mask, Alignment::ONE)
}
})
Expand Down
21 changes: 20 additions & 1 deletion tests/wgsl-errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1860,7 +1860,26 @@ fn function_returns_void() {
#[test]
fn binding_array_local() {
check_validation! {
"fn f() { var x: binding_array<i32, 4>; }":
"fn f() { var x: binding_array<sampler, 4>; }":
Err(_)
}
}

#[test]
fn binding_array_private() {
check_validation! {
"var<private> x: binding_array<sampler, 4>;":
Err(_)
}
}

#[test]
fn binding_array_non_struct() {
check_validation! {
"var<storage> x: binding_array<i32, 4>;":
Err(naga::valid::ValidationError::Type {
source: naga::valid::TypeError::BindingArrayBaseTypeNotStruct(_),
..
})
}
}

0 comments on commit 74dbb64

Please sign in to comment.