Implement bindless buffers on metal (#9081)

This commit is contained in:
Máté Homolya authored and GitHub committed 2026-06-29 17:21:43 -04:00
1 parent 2374e16ab9
commit bf5c6cff4e
18 files changed
+1059 -277

No files matched your search

+2
View File
@@ -173,6 +173,8 @@ By @stuartparmenter in [#9658](https://github.com/gfx-rs/wgpu/pull/9658).
- Add `metal::Queue::add_wait_event` / `add_signal_event` (with `remove_*` companions) to stage `MTLSharedEvent` waits/signals on the next `Queue::submit`, for GPU-side interop with foreign APIs. Waits run on an internal CB committed before user CBs. By @AdrianEddy in [#9483](https://github.com/gfx-rs/wgpu/pull/9483).
- Unconditionally enable `Features::CLIP_DISTANCES`. By @ErichDonGubler in [#9270](https://github.com/gfx-rs/wgpu/pull/9270).
- Added full support for mesh shaders, including in WGSL shaders. By @inner-daemons in [#8739](https://github.com/gfx-rs/wgpu/pull/8739).
- Fixed structure field names incorrectly ignoring reserved keywords in the Metal (MSL) backend. By @39ali [#9379](https://github.com/gfx-rs/wgpu/pull/9379).
- Added support for bindless storage buffers (buffer binding arrays) on Metal. By @mate-h in [#9081](https://github.com/gfx-rs/wgpu/pull/9081).
- Added `DropCallback`s to Metal textures. By @jerzywilczek in [#9634](https://github.com/gfx-rs/wgpu/pull/9634).
#### GLES
+33
View File
@@ -424,6 +424,39 @@ pub struct PipelineOptions {
/// vertex_buffer_mappings are used during shader translation to
/// support vertex pulling.
pub vertex_buffer_mappings: Vec<VertexBufferMapping>,
/// For each storage `binding_array` in the pipeline layout, the number of
/// elements that layout declares, keyed by `ResourceBinding`.
/// The MSL writer uses this to report the number of elements in an unbounded `binding_array`.
#[cfg_attr(
feature = "deserialize",
serde(deserialize_with = "deserialize_binding_array_length_map")
)]
pub binding_array_length_map: crate::FastHashMap<crate::ResourceBinding, u32>,
}
#[cfg(feature = "deserialize")]
#[derive(serde::Deserialize)]
struct BindingArrayLengthMapSerialization {
resource_binding: crate::ResourceBinding,
count: u32,
}
#[cfg(feature = "deserialize")]
fn deserialize_binding_array_length_map<'de, D>(
deserializer: D,
) -> Result<crate::FastHashMap<crate::ResourceBinding, u32>, D::Error>
where
D: serde::Deserializer<'de>,
{
use serde::Deserialize;
let vec = Vec::<BindingArrayLengthMapSerialization>::deserialize(deserializer)?;
let mut map = crate::FastHashMap::default();
for item in vec {
map.insert(item.resource_binding, item.count);
}
Ok(map)
}
impl Options {
+264 -84
View File
@@ -370,16 +370,28 @@ impl Display for TypeContext<'_> {
write!(out, "{}", super::ray::metal_intersector_ty())
}
crate::TypeInner::BindingArray { base, .. } => {
let base_inner = &self.gctx.types[base].inner;
let base_tyname = Self {
handle: base,
first_time: false,
..*self
};
write!(
out,
"constant {ARGUMENT_BUFFER_WRAPPER_STRUCT}<{base_tyname}>*"
)
match *base_inner {
crate::TypeInner::Struct { .. } => {
// Buffers in a binding array are pointers declared as `device T*`, so members use `->`.
// Textures and samplers stay as plain values inside the wrapper.
write!(
out,
"device {ARGUMENT_BUFFER_WRAPPER_STRUCT}<device {base_tyname}*>*"
)
}
_ => {
write!(
out,
"constant {ARGUMENT_BUFFER_WRAPPER_STRUCT}<{base_tyname}>*"
)
}
}
}
}
}
@@ -430,29 +442,36 @@ impl TypedGlobalVariable<'_> {
first_time: false,
};
let access = if var.space.needs_access_qualifier()
&& !self.usage.intersects(valid::GlobalUse::WRITE)
{
"const"
let (coherent, space, access, reference) = if matches!(
self.module.types[var.ty].inner,
crate::TypeInner::BindingArray { .. }
) {
("", "", "", "")
} else {
""
};
let (coherent, space, access, reference) = match (var.space.to_msl_name(), var.space) {
(Some(space), crate::AddressSpace::WorkGroup) => {
("", space, access, if self.reference { "&" } else { "" })
let access = if var.space.needs_access_qualifier()
&& !self.usage.intersects(valid::GlobalUse::WRITE)
{
"const"
} else {
""
};
match (var.space.to_msl_name(), var.space) {
(Some(space), crate::AddressSpace::WorkGroup) => {
("", space, access, if self.reference { "&" } else { "" })
}
(Some(space), _) if self.reference => {
let coherent = if var
.memory_decorations
.contains(crate::MemoryDecorations::COHERENT)
{
"coherent "
} else {
""
};
(coherent, space, access, "&")
}
_ => ("", "", "", ""),
}
(Some(space), _) if self.reference => {
let coherent = if var
.memory_decorations
.contains(crate::MemoryDecorations::COHERENT)
{
"coherent "
} else {
""
};
(coherent, space, access, "&")
}
_ => ("", "", "", ""),
};
let ty = format!(
@@ -620,28 +639,6 @@ fn should_pack_struct_member(
}
}
fn needs_array_length(ty: Handle<crate::Type>, arena: &crate::UniqueArena<crate::Type>) -> bool {
match arena[ty].inner {
crate::TypeInner::Struct { ref members, .. } => {
if let Some(member) = members.last() {
if let crate::TypeInner::Array {
size: crate::ArraySize::Dynamic,
..
} = arena[member.ty].inner
{
return true;
}
}
false
}
crate::TypeInner::Array {
size: crate::ArraySize::Dynamic,
..
} => true,
_ => false,
}
}
impl crate::AddressSpace {
/// Returns true if global variables in this address space are
/// passed in function arguments. These arguments need to be
@@ -805,6 +802,71 @@ impl<'a> ExpressionContext<'a> {
self.info[handle].ty.inner_with(&self.module.types)
}
/// Walks from an inner pointer toward a storage binding array global and
/// returns the element index at that global for MSL runtime buffer sizing.
fn binding_array_index_from_chain(
&self,
mut expr: Handle<crate::Expression>,
global: Handle<crate::GlobalVariable>,
) -> Option<index::GuardedIndex> {
let expressions = &self.function.expressions;
loop {
match expressions[expr] {
crate::Expression::Load { pointer } => expr = pointer,
crate::Expression::Access { base, index } => {
if matches!(
expressions[base],
crate::Expression::GlobalVariable(g) if g == global
) {
return Some(index::GuardedIndex::Expression(index));
}
expr = base;
}
crate::Expression::AccessIndex { base, index } => {
if matches!(
expressions[base],
crate::Expression::GlobalVariable(g) if g == global
) {
return Some(index::GuardedIndex::Known(index));
}
expr = base;
}
crate::Expression::GlobalVariable(_) => return None,
_ => return None,
}
}
}
/// Whether `expr` is directly indexing a global in the outer `Access`/`AccessIndex` shape.
fn is_global_access_chain(&self, expr: Handle<crate::Expression>) -> bool {
let expressions = &self.function.expressions;
match expressions[expr] {
crate::Expression::Access { base, .. } => match expressions[base] {
crate::Expression::GlobalVariable(_) => true,
crate::Expression::Access { .. } => self.is_global_access_chain(base),
_ => false,
},
crate::Expression::AccessIndex { base, .. } => {
matches!(expressions[base], crate::Expression::GlobalVariable(_))
}
_ => false,
}
}
fn struct_member_needs_arrow(
&self,
base: Handle<crate::Expression>,
originating_global_ty: impl FnOnce(&crate::TypeInner) -> bool,
) -> bool {
let originating_matches = match self.function.originating_global(base) {
Some(gv) => {
originating_global_ty(&self.module.types[self.module.global_variables[gv].ty].inner)
}
None => false,
};
originating_matches && self.is_global_access_chain(base)
}
/// Return true if calls to `image`'s `read` and `write` methods should supply a level of detail.
///
/// Only mipmapped images need to specify a level of detail. Since 1D
@@ -1579,9 +1641,51 @@ impl<W: Write> Writer<W> {
///
/// `handle` must be the handle of a global variable whose final member is a
/// dynamically sized array.
///
/// `chain_expr` sits on the pointer path from an inner access, such as the
/// value passed to array length, back toward this global. For storage binding
/// arrays, that path tells us which element index to use in `_buffer_sizes`.
fn binding_array_layout_count(
module: &crate::Module,
pipeline_options: &PipelineOptions,
global: Handle<crate::GlobalVariable>,
) -> u32 {
let var = &module.global_variables[global];
let crate::TypeInner::BindingArray { size, .. } = module.types[var.ty].inner else {
unreachable!("binding_array_layout_count called on non-binding-array global");
};
let from_shader = match size {
crate::ArraySize::Constant(n) => n.get(),
crate::ArraySize::Pending(_) | crate::ArraySize::Dynamic => 0,
};
let from_layout = var
.binding
.and_then(|br| pipeline_options.binding_array_length_map.get(&br))
.copied()
.unwrap_or(0);
from_shader.max(from_layout).max(1)
}
fn put_binding_array_size_member_index(
&mut self,
index: index::GuardedIndex,
context: &ExpressionContext,
) -> BackendResult {
match index {
index::GuardedIndex::Expression(expr) => {
write!(self.out, "unsigned(")?;
self.put_expression(expr, context, true)?;
write!(self.out, ")")?;
}
index::GuardedIndex::Known(value) => write!(self.out, "{value}u")?,
}
Ok(())
}
fn put_dynamic_array_max_index(
&mut self,
handle: Handle<crate::GlobalVariable>,
chain_expr: Handle<crate::Expression>,
context: &ExpressionContext,
) -> BackendResult {
let global = &context.module.global_variables[handle];
@@ -1590,6 +1694,18 @@ impl<W: Write> Writer<W> {
Some(&crate::StructMember { offset, ty, .. }) => (offset, ty),
None => return Err(Error::GenericValidation("Struct has no members".into())),
},
crate::TypeInner::BindingArray { base, .. } => match context.module.types[base].inner {
crate::TypeInner::Struct { ref members, .. } => match members.last() {
Some(&crate::StructMember { offset, ty, .. }) => (offset, ty),
None => return Err(Error::GenericValidation("Struct has no members".into())),
},
_ => {
return Err(Error::GenericValidation(
"binding_array element must be a struct with a runtime-sized array field"
.into(),
))
}
},
crate::TypeInner::Array {
size: crate::ArraySize::Dynamic,
..
@@ -1629,8 +1745,32 @@ impl<W: Write> Writer<W> {
// prevent that.
write!(
self.out,
"(_buffer_sizes.{member} - {offset} - {size}) / {stride}",
"(_buffer_sizes.{member}",
member = ArraySizeMember(handle),
)?;
if let crate::TypeInner::BindingArray { .. } = context.module.types[global.ty].inner {
let Some(array_index) = context.binding_array_index_from_chain(chain_expr, handle)
else {
return Err(Error::GenericValidation(
"Could not find binding_array index for buffer size".into(),
));
};
write!(self.out, "[")?;
match array_index {
index::GuardedIndex::Expression(expr) => {
write!(self.out, "unsigned(")?;
self.put_expression(expr, context, true)?;
write!(self.out, ")")?;
}
index::GuardedIndex::Known(i) => {
write!(self.out, "{i}u")?;
}
}
write!(self.out, "]")?;
}
write!(
self.out,
" - {offset} - {size}) / {stride}",
offset = offset,
size = size,
stride = stride,
@@ -2869,31 +3009,18 @@ impl<W: Write> Writer<W> {
unreachable!()
}
crate::Expression::ArrayLength(expr) => {
// Find the global to which the array belongs.
let global = match context.function.expressions[expr] {
crate::Expression::AccessIndex { base, .. } => {
match context.function.expressions[base] {
crate::Expression::GlobalVariable(handle) => handle,
ref ex => {
return Err(Error::GenericValidation(format!(
"Expected global variable in AccessIndex, got {ex:?}"
)))
}
}
}
crate::Expression::GlobalVariable(handle) => handle,
ref ex => {
return Err(Error::GenericValidation(format!(
"Unexpected expression in ArrayLength, got {ex:?}"
)))
}
};
let global = context.function.originating_global(expr).ok_or_else(|| {
Error::GenericValidation(format!(
"Could not find global variable for ArrayLength operand {:?}",
context.function.expressions[expr]
))
})?;
if !is_scoped {
write!(self.out, "(")?;
}
write!(self.out, "1 + ")?;
self.put_dynamic_array_max_index(global, context)?;
self.put_dynamic_array_max_index(global, expr, context)?;
if !is_scoped {
write!(self.out, ")")?;
}
@@ -3186,8 +3313,26 @@ impl<W: Write> Writer<W> {
let global = context.function.originating_global(base).ok_or_else(|| {
Error::GenericValidation("Could not find originating global".into())
})?;
write!(self.out, "1 + ")?;
self.put_dynamic_array_max_index(global, context)?
if matches!(
context.module.types[context.module.global_variables[global].ty].inner,
crate::TypeInner::BindingArray { .. }
) {
write!(
self.out,
"{} && _buffer_sizes.{}[",
Self::binding_array_layout_count(
context.module,
context.pipeline_options,
global,
),
ArraySizeMember(global),
)?;
self.put_binding_array_size_member_index(index, context)?;
write!(self.out, "] != 0u")?;
} else {
write!(self.out, "1 + ")?;
self.put_dynamic_array_max_index(global, base, context)?
}
}
}
}
@@ -3256,7 +3401,17 @@ impl<W: Write> Writer<W> {
let base_ty = base_ty_handle.unwrap();
self.put_access_chain(base, policy, context)?;
let name = &self.names[&NameKey::StructMember(base_ty, index)];
write!(self.out, ".{name}")?;
write!(
self.out,
"{}{name}",
if context.struct_member_needs_arrow(base, |ty| {
matches!(ty, crate::TypeInner::BindingArray { .. })
}) {
"->"
} else {
"."
},
)?;
}
crate::TypeInner::ValuePointer { .. } | crate::TypeInner::Vector { .. } => {
self.put_access_chain(base, policy, context)?;
@@ -3343,7 +3498,7 @@ impl<W: Write> Writer<W> {
let global = context.function.originating_global(base).ok_or_else(|| {
Error::GenericValidation("Could not find originating global".into())
})?;
self.put_dynamic_array_max_index(global, context)?;
self.put_dynamic_array_max_index(global, base, context)?;
}
}
write!(self.out, ")")?;
@@ -4003,8 +4158,9 @@ impl<W: Write> Writer<W> {
}
write!(self.out, "{name}")?;
}
needs_buffer_sizes |=
needs_array_length(var.ty, &context.expression.module.types);
needs_buffer_sizes |= context.expression.module.types[var.ty]
.inner
.needs_host_buffer_byte_size(&context.expression.module.types);
}
if needs_buffer_sizes {
if separate {
@@ -4408,7 +4564,11 @@ impl<W: Write> Writer<W> {
let globals: Vec<Handle<crate::GlobalVariable>> = module
.global_variables
.iter()
.filter(|&(_, var)| needs_array_length(var.ty, &module.types))
.filter(|&(_, var)| {
module.types[var.ty]
.inner
.needs_host_buffer_byte_size(&module.types)
})
.map(|(handle, _)| handle)
.collect();
@@ -4421,12 +4581,26 @@ impl<W: Write> Writer<W> {
writeln!(self.out, "struct _mslBufferSizes {{")?;
for global in globals {
writeln!(
self.out,
"{}uint {};",
back::INDENT,
ArraySizeMember(global)
)?;
let var = &module.global_variables[global];
let var_ty = var.ty;
match module.types[var_ty].inner {
crate::TypeInner::BindingArray { .. } => {
let n =
Self::binding_array_layout_count(module, pipeline_options, global);
writeln!(
self.out,
"{}uint {}[{n}];",
back::INDENT,
ArraySizeMember(global),
)?;
}
_ => writeln!(
self.out,
"{}uint {};",
back::INDENT,
ArraySizeMember(global)
)?,
}
}
for idx in buffer_indices {
@@ -6744,7 +6918,9 @@ template <typename A>
if var.space.needs_pass_through() {
pass_through_globals.push(handle);
}
needs_buffer_sizes |= needs_array_length(var.ty, &module.types);
needs_buffer_sizes |= module.types[var.ty]
.inner
.needs_host_buffer_byte_size(&module.types);
}
}
@@ -6917,7 +7093,11 @@ template <typename A>
.global_variables
.iter()
.filter(|&(handle, _)| !fun_info[handle].is_empty())
.any(|(_, var)| needs_array_length(var.ty, &module.types));
.any(|(_, var)| {
module.types[var.ty]
.inner
.needs_host_buffer_byte_size(&module.types)
});
// skip this entry point if any global bindings are missing,
// or their types are incompatible.
+19
View File
@@ -253,6 +253,22 @@ impl BlockContext<'_> {
Ok(length_id)
}
/// If `sequence` refers to an unbounded binding array global, return its
/// layout size from the SPIR-V binding map.
fn binding_array_layout_size(&self, sequence: Handle<crate::Expression>) -> Option<u32> {
let global_handle = match self.ir_function.expressions[sequence] {
crate::Expression::GlobalVariable(handle) => handle,
_ => self.ir_function.originating_global(sequence)?,
};
let global = &self.ir_module.global_variables[global_handle];
let crate::TypeInner::BindingArray { .. } = self.ir_module.types[global.ty].inner else {
return None;
};
let binding = global.binding?;
let bind_target = self.writer.resolve_resource_binding(&binding).ok()?;
bind_target.binding_array_size
}
/// Compute the length of a subscriptable value.
///
/// Given `sequence`, an expression referring to some indexable type, return
@@ -273,6 +289,9 @@ impl BlockContext<'_> {
Ok(MaybeKnown::Known(known_length))
}
Ok(crate::proc::IndexableLength::Dynamic) => {
if let Some(size) = self.binding_array_layout_size(sequence) {
return Ok(MaybeKnown::Known(size));
}
let length_id = self.write_runtime_array_length(sequence, block)?;
Ok(MaybeKnown::Computed(length_id))
}
+1 -1
View File
@@ -524,7 +524,7 @@ impl Writer {
///
/// If the specified resource is not present in the binding map this will
/// return an error, unless [`Writer::fake_missing_bindings`] is set.
fn resolve_resource_binding(
pub(super) fn resolve_resource_binding(
&self,
res_binding: &crate::ResourceBinding,
) -> Result<BindingInfo, Error> {
+2 -4
View File
@@ -391,10 +391,8 @@ impl crate::Function {
crate::Expression::Access { base, .. } => base,
crate::Expression::AccessIndex { base, .. } => base,
crate::Expression::GlobalVariable(handle) => return Some(handle),
crate::Expression::LocalVariable(_) => return None,
crate::Expression::FunctionArgument(_) => return None,
// There are no other expressions that produce pointer values.
_ => unreachable!(),
// Other expressions are not on this path to a global.
_ => return None,
}
}
}
+30
View File
@@ -374,6 +374,36 @@ impl crate::TypeInner {
}
}
/// True when host code may need a storage buffer byte size for this type, for bounds checks
/// or WGSL `arrayLength`. Includes runtime-sized arrays and `binding_array` of element types
/// that qualify.
///
/// The Metal backend treats this like “needs array length metadata”: it decides which globals
/// get entries in the synthesized `_mslBufferSizes` struct that those entry points take.
///
/// A struct matches only if its last field is a runtime-sized array. This method does not open
/// a trailing struct to look at its members. [`Self::is_dynamically_sized`] does.
pub fn needs_host_buffer_byte_size(&self, types: &crate::UniqueArena<crate::Type>) -> bool {
use crate::TypeInner as Ti;
match *self {
Ti::Struct { ref members, .. } => members.last().is_some_and(|m| {
matches!(
types[m.ty].inner,
Ti::Array {
size: crate::ArraySize::Dynamic,
..
}
)
}),
Ti::Array {
size: crate::ArraySize::Dynamic,
..
} => true,
Ti::BindingArray { base, .. } => types[base].inner.needs_host_buffer_byte_size(types),
_ => false,
}
}
/// Returns true if `self` is a constructible type.
pub fn is_constructible(&self, types: &crate::UniqueArena<crate::Type>) -> bool {
use crate::TypeInner as Ti;
+23 -1
View File
@@ -1,4 +1,4 @@
targets = "WGSL | SPIRV"
targets = "WGSL | METAL | SPIRV"
capabilities = """
STORAGE_BUFFER_BINDING_ARRAY
| STORAGE_BUFFER_BINDING_ARRAY_NON_UNIFORM_INDEXING
@@ -8,6 +8,24 @@ capabilities = """
index = "ReadZeroSkipWrite"
buffer = "ReadZeroSkipWrite"
image = "ReadZeroSkipWrite"
binding_array = "ReadZeroSkipWrite"
[msl]
fake_missing_bindings = true
lang_version = [3, 0]
spirv_cross_compatibility = false
zero_initialize_workgroup_memory = true
[msl.per_entry_point_map.main]
resources = [
{ bind_target = { buffer = 0, mutable = false }, resource_binding = { group = 0, binding = 0 } },
{ bind_target = { buffer = 1, mutable = false }, resource_binding = { group = 0, binding = 1 } },
{ bind_target = { buffer = 2, mutable = false }, resource_binding = { group = 0, binding = 10 } },
]
[[msl_pipeline.binding_array_length_map]]
resource_binding = { group = 0, binding = 0 }
count = 10
[spv]
version = [1, 1]
@@ -15,3 +33,7 @@ version = [1, 1]
[[spv.binding_map]]
bind_target = { descriptor_set = 0, binding = 0, binding_array_size = 10 }
resource_binding = { group = 0, binding = 0 }
[[spv.binding_map]]
bind_target = { descriptor_set = 0, binding = 1 }
resource_binding = { group = 0, binding = 1 }
+27 -2
View File
@@ -3,9 +3,19 @@ struct UniformIndex {
index: u32
}
struct Foo { x: u32, far: array<i32> }
struct Inner {
y: u32,
}
struct Foo { x: u32, nested: Inner, far: array<i32> }
struct PlainData {
values: array<u32>,
}
@group(0) @binding(0)
var<storage, read> storage_array: binding_array<Foo, 1>;
var<storage, read> storage_array: binding_array<Foo>;
@group(0) @binding(1)
var<storage, read> plain_storage: PlainData;
@group(0) @binding(10)
var<uniform> uni: UniformIndex;
@@ -23,10 +33,25 @@ fn main(fragment_in: FragmentIn) -> @location(0) u32 {
u1 += storage_array[0].x;
u1 += storage_array[uniform_index].x;
u1 += storage_array[non_uniform_index].x;
u1 += storage_array[7].x;
u1 += storage_array[0].nested.y;
u1 += storage_array[uniform_index].nested.y;
u1 += storage_array[non_uniform_index].nested.y;
u1 += storage_array[7].nested.y;
u1 += arrayLength(&storage_array[0].far);
u1 += arrayLength(&storage_array[uniform_index].far);
u1 += arrayLength(&storage_array[non_uniform_index].far);
u1 += arrayLength(&storage_array[7].far);
u1 += bitcast<u32>(storage_array[0].far[0]);
u1 += bitcast<u32>(storage_array[uniform_index].far[0]);
u1 += bitcast<u32>(storage_array[non_uniform_index].far[0]);
u1 += bitcast<u32>(storage_array[7].far[0]);
u1 += plain_storage.values[0];
u1 += arrayLength(&plain_storage.values);
return u1;
}
@@ -0,0 +1,110 @@
// language: metal3.0
#include <metal_stdlib>
#include <simd/simd.h>
using metal::uint;
struct DefaultConstructible {
template<typename T>
operator T() && {
return T {};
}
};
struct _mslBufferSizes {
uint size0[10];
uint size1;
};
struct UniformIndex {
uint index;
};
struct Inner {
uint y;
};
typedef int type_2[1];
struct Foo {
uint x;
Inner nested;
type_2 far;
};
typedef uint type_3[1];
struct PlainData {
type_3 values;
};
template <typename T>
struct NagaArgumentBufferWrapper {
T inner;
};
struct FragmentIn {
uint index;
};
struct main_Input {
uint index [[user(loc0), flat]];
};
struct main_Output {
uint member [[color(0)]];
};
fragment main_Output main_(
main_Input varyings [[stage_in]]
, device NagaArgumentBufferWrapper<device Foo*>* storage_array [[buffer(0)]]
, device PlainData const& plain_storage [[buffer(1)]]
, constant UniformIndex& uni [[buffer(2)]]
, constant _mslBufferSizes& _buffer_sizes [[user(fake0)]]
) {
const FragmentIn fragment_in = { varyings.index };
uint u1_ = 0u;
uint uniform_index = uni.index;
uint non_uniform_index = fragment_in.index;
uint _e7 = u1_;
uint _e11 = uint(0) < 10 && _buffer_sizes.size0[0u] != 0u ? storage_array[0].inner->x : DefaultConstructible();
u1_ = _e7 + _e11;
uint _e13 = u1_;
uint _e17 = uint(uniform_index) < 10 && _buffer_sizes.size0[unsigned(uniform_index)] != 0u ? storage_array[uniform_index].inner->x : DefaultConstructible();
u1_ = _e13 + _e17;
uint _e19 = u1_;
uint _e23 = uint(non_uniform_index) < 10 && _buffer_sizes.size0[unsigned(non_uniform_index)] != 0u ? storage_array[non_uniform_index].inner->x : DefaultConstructible();
u1_ = _e19 + _e23;
uint _e25 = u1_;
uint _e29 = uint(7) < 10 && _buffer_sizes.size0[7u] != 0u ? storage_array[7].inner->x : DefaultConstructible();
u1_ = _e25 + _e29;
uint _e31 = u1_;
uint _e36 = uint(0) < 10 && _buffer_sizes.size0[0u] != 0u ? storage_array[0].inner->nested.y : DefaultConstructible();
u1_ = _e31 + _e36;
uint _e38 = u1_;
uint _e43 = uint(uniform_index) < 10 && _buffer_sizes.size0[unsigned(uniform_index)] != 0u ? storage_array[uniform_index].inner->nested.y : DefaultConstructible();
u1_ = _e38 + _e43;
uint _e45 = u1_;
uint _e50 = uint(non_uniform_index) < 10 && _buffer_sizes.size0[unsigned(non_uniform_index)] != 0u ? storage_array[non_uniform_index].inner->nested.y : DefaultConstructible();
u1_ = _e45 + _e50;
uint _e52 = u1_;
uint _e57 = uint(7) < 10 && _buffer_sizes.size0[7u] != 0u ? storage_array[7].inner->nested.y : DefaultConstructible();
u1_ = _e52 + _e57;
uint _e59 = u1_;
u1_ = _e59 + (1 + (_buffer_sizes.size0[0u] - 8 - 4) / 4);
uint _e65 = u1_;
u1_ = _e65 + (1 + (_buffer_sizes.size0[unsigned(uniform_index)] - 8 - 4) / 4);
uint _e71 = u1_;
u1_ = _e71 + (1 + (_buffer_sizes.size0[unsigned(non_uniform_index)] - 8 - 4) / 4);
uint _e77 = u1_;
u1_ = _e77 + (1 + (_buffer_sizes.size0[7u] - 8 - 4) / 4);
uint _e83 = u1_;
int _e88 = uint(0) < 10 && _buffer_sizes.size0[0u] != 0u && uint(0) < 10 && _buffer_sizes.size0[0u] != 0u ? storage_array[0].inner->far[0] : DefaultConstructible();
u1_ = _e83 + as_type<uint>(_e88);
uint _e91 = u1_;
int _e96 = uint(0) < 10 && _buffer_sizes.size0[0u] != 0u && uint(uniform_index) < 10 && _buffer_sizes.size0[unsigned(uniform_index)] != 0u ? storage_array[uniform_index].inner->far[0] : DefaultConstructible();
u1_ = _e91 + as_type<uint>(_e96);
uint _e99 = u1_;
int _e104 = uint(0) < 10 && _buffer_sizes.size0[0u] != 0u && uint(non_uniform_index) < 10 && _buffer_sizes.size0[unsigned(non_uniform_index)] != 0u ? storage_array[non_uniform_index].inner->far[0] : DefaultConstructible();
u1_ = _e99 + as_type<uint>(_e104);
uint _e107 = u1_;
int _e112 = uint(0) < 10 && _buffer_sizes.size0[0u] != 0u && uint(7) < 10 && _buffer_sizes.size0[7u] != 0u ? storage_array[7].inner->far[0] : DefaultConstructible();
u1_ = _e107 + as_type<uint>(_e112);
uint _e115 = u1_;
uint _e119 = uint(0) < 1 + (_buffer_sizes.size1 - 0 - 4) / 4 ? plain_storage.values[0] : DefaultConstructible();
u1_ = _e115 + _e119;
uint _e121 = u1_;
u1_ = _e121 + (1 + (_buffer_sizes.size1 - 0 - 4) / 4);
uint _e126 = u1_;
return main_Output { _e126 };
}
@@ -1,119 +1,239 @@
; SPIR-V
; Version: 1.1
; Generator: rspirv
; Bound: 76
; Bound: 159
OpCapability Shader
OpCapability ShaderNonUniform
OpExtension "SPV_KHR_storage_buffer_storage_class"
OpExtension "SPV_EXT_descriptor_indexing"
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %25 "main" %20 %23
OpExecutionMode %25 OriginUpperLeft
OpEntryPoint Fragment %29 "main" %24 %27
OpExecutionMode %29 OriginUpperLeft
OpMemberDecorate %4 0 Offset 0
OpDecorate %6 ArrayStride 4
OpMemberDecorate %7 0 Offset 0
OpMemberDecorate %7 1 Offset 4
OpDecorate %7 Block
OpMemberDecorate %5 0 Offset 0
OpDecorate %7 ArrayStride 4
OpMemberDecorate %8 0 Offset 0
OpMemberDecorate %8 1 Offset 4
OpMemberDecorate %8 2 Offset 8
OpDecorate %8 Block
OpDecorate %9 ArrayStride 4
OpMemberDecorate %10 0 Offset 0
OpDecorate %11 NonWritable
OpDecorate %11 DescriptorSet 0
OpDecorate %11 Binding 0
OpDecorate %15 DescriptorSet 0
OpDecorate %15 Binding 10
OpDecorate %16 Block
OpMemberDecorate %16 0 Offset 0
OpDecorate %20 Location 0
OpDecorate %20 Flat
OpDecorate %23 Location 0
OpDecorate %56 NonUniform
OpDecorate %10 Block
OpMemberDecorate %12 0 Offset 0
OpDecorate %13 NonWritable
OpDecorate %13 DescriptorSet 0
OpDecorate %13 Binding 0
OpDecorate %17 NonWritable
OpDecorate %17 DescriptorSet 0
OpDecorate %17 Binding 1
OpDecorate %19 DescriptorSet 0
OpDecorate %19 Binding 10
OpDecorate %20 Block
OpMemberDecorate %20 0 Offset 0
OpDecorate %24 Location 0
OpDecorate %24 Flat
OpDecorate %27 Location 0
OpDecorate %60 NonUniform
OpDecorate %87 NonUniform
OpDecorate %133 NonUniform
%2 = OpTypeVoid
%3 = OpTypeInt 32 0
%4 = OpTypeStruct %3
%5 = OpTypeInt 32 1
%6 = OpTypeRuntimeArray %5
%7 = OpTypeStruct %3 %6
%9 = OpConstant %3 1
%8 = OpTypeArray %7 %9
%10 = OpTypeStruct %3
%13 = OpConstant %3 10
%12 = OpTypeArray %7 %13
%14 = OpTypePointer StorageBuffer %12
%11 = OpVariable %14 StorageBuffer
%16 = OpTypeStruct %4
%17 = OpTypePointer Uniform %16
%15 = OpVariable %17 Uniform
%21 = OpTypePointer Input %3
%20 = OpVariable %21 Input
%24 = OpTypePointer Output %3
%23 = OpVariable %24 Output
%26 = OpTypeFunction %2
%27 = OpTypePointer Uniform %4
%28 = OpConstant %3 0
%30 = OpTypePointer StorageBuffer %8
%32 = OpTypePointer Function %3
%34 = OpTypePointer Uniform %3
%39 = OpTypePointer StorageBuffer %7
%40 = OpTypePointer StorageBuffer %3
%46 = OpTypeBool
%48 = OpConstantNull %3
%63 = OpTypePointer StorageBuffer %6
%25 = OpFunction %2 None %26
%18 = OpLabel
%31 = OpVariable %32 Function %28
%22 = OpLoad %3 %20
%19 = OpCompositeConstruct %10 %22
%29 = OpAccessChain %27 %15 %28
OpBranch %33
%33 = OpLabel
%35 = OpAccessChain %34 %29 %28
%36 = OpLoad %3 %35
%37 = OpCompositeExtract %3 %19 0
%38 = OpLoad %3 %31
%41 = OpAccessChain %40 %11 %28 %28
%42 = OpLoad %3 %41
%43 = OpIAdd %3 %38 %42
OpStore %31 %43
%44 = OpLoad %3 %31
%45 = OpULessThan %46 %36 %9
OpSelectionMerge %49 None
OpBranchConditional %45 %50 %49
%50 = OpLabel
%47 = OpAccessChain %40 %11 %36 %28
%51 = OpLoad %3 %47
OpBranch %49
%49 = OpLabel
%52 = OpPhi %3 %48 %33 %51 %50
%53 = OpIAdd %3 %44 %52
OpStore %31 %53
%54 = OpLoad %3 %31
%55 = OpULessThan %46 %37 %9
OpSelectionMerge %57 None
OpBranchConditional %55 %58 %57
%58 = OpLabel
%56 = OpAccessChain %40 %11 %37 %28
%59 = OpLoad %3 %56
OpBranch %57
%57 = OpLabel
%60 = OpPhi %3 %48 %49 %59 %58
%61 = OpIAdd %3 %54 %60
OpStore %31 %61
%62 = OpLoad %3 %31
%64 = OpAccessChain %39 %11 %28
%65 = OpArrayLength %3 %64 1
%66 = OpIAdd %3 %62 %65
OpStore %31 %66
%67 = OpLoad %3 %31
%68 = OpAccessChain %39 %11 %36
%69 = OpArrayLength %3 %68 1
%70 = OpIAdd %3 %67 %69
OpStore %31 %70
%71 = OpLoad %3 %31
%72 = OpAccessChain %39 %11 %37
%73 = OpArrayLength %3 %72 1
%74 = OpIAdd %3 %71 %73
OpStore %31 %74
%75 = OpLoad %3 %31
OpStore %23 %75
%5 = OpTypeStruct %3
%6 = OpTypeInt 32 1
%7 = OpTypeRuntimeArray %6
%8 = OpTypeStruct %3 %5 %7
%9 = OpTypeRuntimeArray %3
%10 = OpTypeStruct %9
%11 = OpTypeRuntimeArray %8
%12 = OpTypeStruct %3
%15 = OpConstant %3 10
%14 = OpTypeArray %8 %15
%16 = OpTypePointer StorageBuffer %14
%13 = OpVariable %16 StorageBuffer
%18 = OpTypePointer StorageBuffer %10
%17 = OpVariable %18 StorageBuffer
%20 = OpTypeStruct %4
%21 = OpTypePointer Uniform %20
%19 = OpVariable %21 Uniform
%25 = OpTypePointer Input %3
%24 = OpVariable %25 Input
%28 = OpTypePointer Output %3
%27 = OpVariable %28 Output
%30 = OpTypeFunction %2
%31 = OpTypePointer Uniform %4
%32 = OpConstant %3 0
%34 = OpTypePointer StorageBuffer %11
%36 = OpTypePointer Function %3
%38 = OpTypePointer Uniform %3
%43 = OpTypePointer StorageBuffer %8
%44 = OpTypePointer StorageBuffer %3
%50 = OpTypeBool
%52 = OpConstantNull %3
%67 = OpConstant %3 7
%72 = OpTypePointer StorageBuffer %5
%73 = OpConstant %3 1
%98 = OpTypePointer StorageBuffer %7
%115 = OpTypePointer StorageBuffer %6
%116 = OpConstant %3 2
%124 = OpConstantNull %6
%146 = OpTypePointer StorageBuffer %9
%29 = OpFunction %2 None %30
%22 = OpLabel
%35 = OpVariable %36 Function %32
%26 = OpLoad %3 %24
%23 = OpCompositeConstruct %12 %26
%33 = OpAccessChain %31 %19 %32
OpBranch %37
%37 = OpLabel
%39 = OpAccessChain %38 %33 %32
%40 = OpLoad %3 %39
%41 = OpCompositeExtract %3 %23 0
%42 = OpLoad %3 %35
%45 = OpAccessChain %44 %13 %32 %32
%46 = OpLoad %3 %45
%47 = OpIAdd %3 %42 %46
OpStore %35 %47
%48 = OpLoad %3 %35
%49 = OpULessThan %50 %40 %15
OpSelectionMerge %53 None
OpBranchConditional %49 %54 %53
%54 = OpLabel
%51 = OpAccessChain %44 %13 %40 %32
%55 = OpLoad %3 %51
OpBranch %53
%53 = OpLabel
%56 = OpPhi %3 %52 %37 %55 %54
%57 = OpIAdd %3 %48 %56
OpStore %35 %57
%58 = OpLoad %3 %35
%59 = OpULessThan %50 %41 %15
OpSelectionMerge %61 None
OpBranchConditional %59 %62 %61
%62 = OpLabel
%60 = OpAccessChain %44 %13 %41 %32
%63 = OpLoad %3 %60
OpBranch %61
%61 = OpLabel
%64 = OpPhi %3 %52 %53 %63 %62
%65 = OpIAdd %3 %58 %64
OpStore %35 %65
%66 = OpLoad %3 %35
%68 = OpAccessChain %44 %13 %67 %32
%69 = OpLoad %3 %68
%70 = OpIAdd %3 %66 %69
OpStore %35 %70
%71 = OpLoad %3 %35
%74 = OpAccessChain %44 %13 %32 %73 %32
%75 = OpLoad %3 %74
%76 = OpIAdd %3 %71 %75
OpStore %35 %76
%77 = OpLoad %3 %35
%78 = OpULessThan %50 %40 %15
OpSelectionMerge %80 None
OpBranchConditional %78 %81 %80
%81 = OpLabel
%79 = OpAccessChain %44 %13 %40 %73 %32
%82 = OpLoad %3 %79
OpBranch %80
%80 = OpLabel
%83 = OpPhi %3 %52 %61 %82 %81
%84 = OpIAdd %3 %77 %83
OpStore %35 %84
%85 = OpLoad %3 %35
%86 = OpULessThan %50 %41 %15
OpSelectionMerge %88 None
OpBranchConditional %86 %89 %88
%89 = OpLabel
%87 = OpAccessChain %44 %13 %41 %73 %32
%90 = OpLoad %3 %87
OpBranch %88
%88 = OpLabel
%91 = OpPhi %3 %52 %80 %90 %89
%92 = OpIAdd %3 %85 %91
OpStore %35 %92
%93 = OpLoad %3 %35
%94 = OpAccessChain %44 %13 %67 %73 %32
%95 = OpLoad %3 %94
%96 = OpIAdd %3 %93 %95
OpStore %35 %96
%97 = OpLoad %3 %35
%99 = OpAccessChain %43 %13 %32
%100 = OpArrayLength %3 %99 2
%101 = OpIAdd %3 %97 %100
OpStore %35 %101
%102 = OpLoad %3 %35
%103 = OpAccessChain %43 %13 %40
%104 = OpArrayLength %3 %103 2
%105 = OpIAdd %3 %102 %104
OpStore %35 %105
%106 = OpLoad %3 %35
%107 = OpAccessChain %43 %13 %41
%108 = OpArrayLength %3 %107 2
%109 = OpIAdd %3 %106 %108
OpStore %35 %109
%110 = OpLoad %3 %35
%111 = OpAccessChain %43 %13 %67
%112 = OpArrayLength %3 %111 2
%113 = OpIAdd %3 %110 %112
OpStore %35 %113
%114 = OpLoad %3 %35
%117 = OpAccessChain %115 %13 %32 %116 %32
%118 = OpLoad %6 %117
%119 = OpBitcast %3 %118
%120 = OpIAdd %3 %114 %119
OpStore %35 %120
%121 = OpLoad %3 %35
%122 = OpULessThan %50 %40 %15
OpSelectionMerge %125 None
OpBranchConditional %122 %126 %125
%126 = OpLabel
%123 = OpAccessChain %115 %13 %40 %116 %32
%127 = OpLoad %6 %123
OpBranch %125
%125 = OpLabel
%128 = OpPhi %6 %124 %88 %127 %126
%129 = OpBitcast %3 %128
%130 = OpIAdd %3 %121 %129
OpStore %35 %130
%131 = OpLoad %3 %35
%132 = OpULessThan %50 %41 %15
OpSelectionMerge %134 None
OpBranchConditional %132 %135 %134
%135 = OpLabel
%133 = OpAccessChain %115 %13 %41 %116 %32
%136 = OpLoad %6 %133
OpBranch %134
%134 = OpLabel
%137 = OpPhi %6 %124 %125 %136 %135
%138 = OpBitcast %3 %137
%139 = OpIAdd %3 %131 %138
OpStore %35 %139
%140 = OpLoad %3 %35
%141 = OpAccessChain %115 %13 %67 %116 %32
%142 = OpLoad %6 %141
%143 = OpBitcast %3 %142
%144 = OpIAdd %3 %140 %143
OpStore %35 %144
%145 = OpLoad %3 %35
%147 = OpArrayLength %3 %17 0
%148 = OpULessThan %50 %32 %147
OpSelectionMerge %150 None
OpBranchConditional %148 %151 %150
%151 = OpLabel
%149 = OpAccessChain %44 %17 %32 %32
%152 = OpLoad %3 %149
OpBranch %150
%150 = OpLabel
%153 = OpPhi %3 %52 %134 %152 %151
%154 = OpIAdd %3 %145 %153
OpStore %35 %154
%155 = OpLoad %3 %35
%156 = OpArrayLength %3 %17 0
%157 = OpIAdd %3 %155 %156
OpStore %35 %157
%158 = OpLoad %3 %35
OpStore %27 %158
OpReturn
OpFunctionEnd
@@ -6,53 +6,121 @@ struct _4
uint _m0;
};
struct _10
struct _5
{
uint _m0;
};
layout(set = 0, binding = 0, std430) readonly buffer _7_11
struct _12
{
uint _m0;
int _m1[];
} _11[10];
};
layout(set = 0, binding = 10, std140) uniform _16_15
layout(set = 0, binding = 0, std430) readonly buffer _8_13
{
uint _m0;
_5 _m1;
int _m2[];
} _13[10];
layout(set = 0, binding = 1, std430) readonly buffer _10_17
{
uint _m0[];
} _17;
layout(set = 0, binding = 10, std140) uniform _20_19
{
_4 _m0;
} _15;
} _19;
layout(location = 0) flat in uint _20;
layout(location = 0) out uint _23;
layout(location = 0) flat in uint _24;
layout(location = 0) out uint _27;
void main()
{
uint _31 = 0u;
uint _37 = _10(_20)._m0;
_31 += _11[0u]._m0;
uint _52;
if (_15._m0._m0 < 1u)
uint _35 = 0u;
uint _41 = _12(_24)._m0;
_35 += _13[0u]._m0;
uint _56;
if (_19._m0._m0 < 10u)
{
_52 = _11[_15._m0._m0]._m0;
_56 = _13[_19._m0._m0]._m0;
}
else
{
_52 = 0u;
_56 = 0u;
}
_31 += _52;
uint _60;
if (_37 < 1u)
_35 += _56;
uint _64;
if (_41 < 10u)
{
_60 = _11[nonuniformEXT(_37)]._m0;
_64 = _13[nonuniformEXT(_41)]._m0;
}
else
{
_60 = 0u;
_64 = 0u;
}
_31 += _60;
_31 += uint(_11[0u]._m1.length());
_31 += uint(_11[_15._m0._m0]._m1.length());
_31 += uint(_11[_37]._m1.length());
_23 = _31;
_35 += _64;
_35 += _13[7u]._m0;
_35 += _13[0u]._m1._m0;
uint _83;
if (_19._m0._m0 < 10u)
{
_83 = _13[_19._m0._m0]._m1._m0;
}
else
{
_83 = 0u;
}
_35 += _83;
uint _91;
if (_41 < 10u)
{
_91 = _13[nonuniformEXT(_41)]._m1._m0;
}
else
{
_91 = 0u;
}
_35 += _91;
_35 += _13[7u]._m1._m0;
_35 += uint(_13[0u]._m2.length());
_35 += uint(_13[_19._m0._m0]._m2.length());
_35 += uint(_13[_41]._m2.length());
_35 += uint(_13[7u]._m2.length());
_35 += uint(_13[0u]._m2[0u]);
int _128;
if (_19._m0._m0 < 10u)
{
_128 = _13[_19._m0._m0]._m2[0u];
}
else
{
_128 = 0;
}
_35 += uint(_128);
int _137;
if (_41 < 10u)
{
_137 = _13[nonuniformEXT(_41)]._m2[0u];
}
else
{
_137 = 0;
}
_35 += uint(_137);
_35 += uint(_13[7u]._m2[0u]);
uint _153;
if (0u < uint(_17._m0.length()))
{
_153 = _17._m0[0u];
}
else
{
_153 = 0u;
}
_35 += _153;
_35 += uint(_17._m0.length());
_27 = _35;
}
@@ -4,17 +4,28 @@ struct UniformIndex {
index: u32,
}
struct Inner {
y: u32,
}
struct Foo {
x: u32,
nested: Inner,
far: array<i32>,
}
struct PlainData {
values: array<u32>,
}
struct FragmentIn {
@location(0) @interpolate(flat) index: u32,
}
@group(0) @binding(0)
var<storage> storage_array: binding_array<Foo, 1>;
var<storage> storage_array: binding_array<Foo>;
@group(0) @binding(1)
var<storage> plain_storage: PlainData;
@group(0) @binding(10)
var<uniform> uni: UniformIndex;
@@ -34,11 +45,45 @@ fn main(fragment_in: FragmentIn) -> @location(0) u32 {
let _e23 = storage_array[non_uniform_index].x;
u1_ = (_e19 + _e23);
let _e25 = u1_;
u1_ = (_e25 + arrayLength((&storage_array[0].far)));
let _e29 = storage_array[7].x;
u1_ = (_e25 + _e29);
let _e31 = u1_;
u1_ = (_e31 + arrayLength((&storage_array[uniform_index].far)));
let _e37 = u1_;
u1_ = (_e37 + arrayLength((&storage_array[non_uniform_index].far)));
let _e43 = u1_;
return _e43;
let _e36 = storage_array[0].nested.y;
u1_ = (_e31 + _e36);
let _e38 = u1_;
let _e43 = storage_array[uniform_index].nested.y;
u1_ = (_e38 + _e43);
let _e45 = u1_;
let _e50 = storage_array[non_uniform_index].nested.y;
u1_ = (_e45 + _e50);
let _e52 = u1_;
let _e57 = storage_array[7].nested.y;
u1_ = (_e52 + _e57);
let _e59 = u1_;
u1_ = (_e59 + arrayLength((&storage_array[0].far)));
let _e65 = u1_;
u1_ = (_e65 + arrayLength((&storage_array[uniform_index].far)));
let _e71 = u1_;
u1_ = (_e71 + arrayLength((&storage_array[non_uniform_index].far)));
let _e77 = u1_;
u1_ = (_e77 + arrayLength((&storage_array[7].far)));
let _e83 = u1_;
let _e88 = storage_array[0].far[0];
u1_ = (_e83 + bitcast<u32>(_e88));
let _e91 = u1_;
let _e96 = storage_array[uniform_index].far[0];
u1_ = (_e91 + bitcast<u32>(_e96));
let _e99 = u1_;
let _e104 = storage_array[non_uniform_index].far[0];
u1_ = (_e99 + bitcast<u32>(_e104));
let _e107 = u1_;
let _e112 = storage_array[7].far[0];
u1_ = (_e107 + bitcast<u32>(_e112));
let _e115 = u1_;
let _e119 = plain_storage.values[0];
u1_ = (_e115 + _e119);
let _e121 = u1_;
u1_ = (_e121 + arrayLength((&plain_storage.values)));
let _e126 = u1_;
return _e126;
}
+8
View File
@@ -1246,6 +1246,14 @@ impl super::CapabilitiesQuery {
.unwrap_or(MTLArgumentBuffersTier::Tier1)
>= MTLArgumentBuffersTier::Tier2,
);
features.set(
F::BUFFER_BINDING_ARRAY,
self.msl_version >= MTLLanguageVersion::Version2_0
&& self
.argument_buffers
.unwrap_or(MTLArgumentBuffersTier::Tier1)
>= MTLArgumentBuffersTier::Tier2,
);
features.set(
F::STORAGE_RESOURCE_BINDING_ARRAY,
self.msl_version >= MTLLanguageVersion::Version3_0
+26 -7
View File
@@ -350,12 +350,31 @@ impl super::CommandEncoder {
}
let index = (resource_indices.buffers + index) as usize;
encoder.set_buffer(buffer, offset as usize, index);
let br = naga::ResourceBinding {
group: group_index,
binding: *binding_location,
};
if let Some(size) = binding_size {
let br = naga::ResourceBinding {
group: group_index,
binding: *binding_location,
};
self.state.storage_buffer_length_map.insert(br, *size);
self.state.storage_buffer_length_map.insert((br, 0), *size);
changes_sizes_buffer = true;
}
}
super::BufferLikeResource::StorageBindingArray {
ptr,
array_element_sizes,
binding_location,
} => {
let buffer = Some(unsafe { ptr.as_ref() });
let index = (resource_indices.buffers + index) as usize;
encoder.set_buffer(buffer, 0, index);
let br = naga::ResourceBinding {
group: group_index,
binding: *binding_location,
};
for &(array_idx, size) in array_element_sizes {
self.state
.storage_buffer_length_map
.insert((br, array_idx), size);
changes_sizes_buffer = true;
}
}
@@ -430,9 +449,9 @@ impl super::CommandState {
let slot = stage_info.sizes_slot?;
result_sizes.clear();
result_sizes.extend(stage_info.sized_bindings.iter().map(|br| {
result_sizes.extend(stage_info.sized_bindings.iter().map(|(br, array_idx)| {
self.storage_buffer_length_map
.get(br)
.get(&(*br, *array_idx))
.map(|size| u32::try_from(size.get()).unwrap_or(u32::MAX))
.unwrap_or_default()
}));
+7
View File
@@ -365,6 +365,13 @@ pub fn map_resource_usage(ty: &wgt::BindingType) -> MTLResourceUsage {
}
},
wgt::BindingType::Sampler(..) => MTLResourceUsage::empty(),
wgt::BindingType::Buffer { ty, .. } => match ty {
wgt::BufferBindingType::Uniform
| wgt::BufferBindingType::Storage { read_only: true } => MTLResourceUsage::Read,
wgt::BufferBindingType::Storage { read_only: false } => {
MTLResourceUsage::Read | MTLResourceUsage::Write
}
},
_ => unreachable!(),
}
}
+126 -41
View File
@@ -1,5 +1,5 @@
use alloc::{borrow::ToOwned as _, sync::Arc, vec::Vec};
use core::ptr::NonNull;
use core::{mem::align_of, ptr::NonNull};
use bytemuck::TransparentWrapper;
use objc2::{
@@ -43,15 +43,16 @@ struct CompiledShader {
wg_size: MTLSize,
wg_memory_sizes: Vec<u32>,
/// Bindings of WGSL `storage` globals that contain variable-sized arrays.
/// Bindings of WGSL `storage` globals that contain variable-sized arrays,
/// paired with a binding-array element index when applicable.
///
/// In order to implement bounds checks and the `arrayLength` function for
/// WGSL runtime-sized arrays, we pass the entry point a struct with a
/// member for each global variable that contains such an array. That member
/// is a `u32` holding the variable's total size in bytes---which is simply
/// the size of the `Buffer` supplying that variable's contents for the
/// draw call.
sized_bindings: Vec<naga::ResourceBinding>,
/// `u32` member for each such binding. For ordinary storage buffers the
/// element index is zero; for storage binding arrays there is one entry per
/// layout element. Each member holds that element's total size in bytes---
/// the size of the `Buffer` supplying its contents for the draw or dispatch.
sized_bindings: Vec<(naga::ResourceBinding, u32)>,
immutable_buffer_mask: usize,
}
@@ -89,6 +90,21 @@ fn create_depth_stencil_desc(
desc
}
#[allow(
clippy::mut_from_ref,
reason = "MTLBuffer shared contents are writable through `&MTLBuffer`"
)]
fn bindless_id_table_mut(
buffer: &ProtocolObject<dyn MTLBuffer>,
count: u32,
) -> &mut [MTLResourceID] {
let ptr = buffer.contents().cast::<u8>().as_ptr();
// SAFETY: The buffer is aligned to the size of `MTLResourceID`.
assert_eq!(ptr as usize % align_of::<MTLResourceID>(), 0);
let ptr = ptr.cast::<MTLResourceID>();
unsafe { core::slice::from_raw_parts_mut(ptr, count as usize) }
}
const fn convert_vertex_format_to_naga(format: wgt::VertexFormat) -> nt::VertexFormat {
match format {
wgt::VertexFormat::Uint8 => nt::VertexFormat::Uint8,
@@ -231,6 +247,7 @@ impl super::Device {
},
vertex_pulling_transform: true,
vertex_buffer_mappings: vertex_buffer_mappings.to_vec(),
binding_array_length_map: layout.binding_array_length_map.clone(),
};
let (source, info) = naga::back::msl::write_string(
@@ -322,18 +339,27 @@ impl super::Device {
immutable_buffer_mask |= 1 << slot;
}
let mut dynamic_array_container_ty = var.ty;
if let naga::TypeInner::Struct { ref members, .. } =
module.types[var.ty].inner
if module.types[var.ty]
.inner
.needs_host_buffer_byte_size(&module.types)
{
dynamic_array_container_ty = members.last().unwrap().ty;
}
if let naga::TypeInner::Array {
size: naga::ArraySize::Dynamic,
..
} = module.types[dynamic_array_container_ty].inner
{
sized_bindings.push(br);
let n = match module.types[var.ty].inner {
naga::TypeInner::BindingArray { size, .. } => {
let from_shader = match size {
naga::ArraySize::Constant(n) => n.get(),
naga::ArraySize::Pending(_)
| naga::ArraySize::Dynamic => 0,
};
let from_layout = layout
.binding_array_length_map
.get(&br)
.copied()
.unwrap_or(0);
from_shader.max(from_layout).max(1)
}
_ => 1,
};
sized_bindings.extend((0..n).map(|i| (br, i)));
}
}
_ => {}
@@ -789,6 +815,7 @@ impl crate::Device for super::Device {
resources: Default::default(),
});
let mut bind_group_infos = [const { None }; crate::MAX_BIND_GROUPS];
let mut binding_array_length_map = naga::FastHashMap::default();
// First, place the immediates
for info in stage_data.iter_mut() {
@@ -811,6 +838,22 @@ impl crate::Device for super::Device {
let base_resource_indices = stage_data.map_ref(|info| info.counters.clone());
for entry in bgl.entries.iter() {
let br = naga::ResourceBinding {
group: group_index as u32,
binding: entry.binding,
};
if let Some(count) = entry.count {
if matches!(
entry.ty,
wgt::BindingType::Buffer {
ty: wgt::BufferBindingType::Storage { .. },
..
}
) {
binding_array_length_map.insert(br, count.get());
}
}
if let wgt::BindingType::Buffer {
ty: wgt::BufferBindingType::Storage { .. },
..
@@ -883,10 +926,6 @@ impl crate::Device for super::Device {
}
}
let br = naga::ResourceBinding {
group: group_index as u32,
binding: entry.binding,
};
info.resources.insert(br, target);
}
}
@@ -930,6 +969,7 @@ impl crate::Device for super::Device {
immediates_infos,
total_immediates: desc.immediate_size,
per_stage_map,
binding_array_length_map,
})
}
@@ -974,7 +1014,7 @@ impl crate::Device for super::Device {
let uses = conv::map_resource_usage(&layout.ty);
// Create argument buffer for this array
let buffer = self
let argument_buffer = self
.shared
.device
.newBufferWithLength_options(
@@ -984,22 +1024,18 @@ impl crate::Device for super::Device {
)
.unwrap();
let contents: &mut [MTLResourceID] = unsafe {
core::slice::from_raw_parts_mut(
buffer.contents().cast().as_ptr(),
count as usize,
)
};
let mut array_element_sizes = Vec::new();
match layout.ty {
wgt::BindingType::Texture { .. }
| wgt::BindingType::StorageTexture { .. } => {
let resource_ids =
bindless_id_table_mut(argument_buffer.as_ref(), count);
let start = entry.resource_index as usize;
let end = start + count as usize;
let textures = &desc.textures[start..end];
for (idx, tex) in textures.iter().enumerate() {
contents[idx] = tex.view.raw.gpuResourceID();
resource_ids[idx] = tex.view.raw.gpuResourceID();
let use_info = bg
.resources_to_use
@@ -1012,17 +1048,51 @@ impl crate::Device for super::Device {
}
}
wgt::BindingType::Sampler { .. } => {
let resource_ids =
bindless_id_table_mut(argument_buffer.as_ref(), count);
let start = entry.resource_index as usize;
let end = start + count as usize;
let samplers = &desc.samplers[start..end];
for (idx, &sampler) in samplers.iter().enumerate() {
contents[idx] = sampler.raw.gpuResourceID();
resource_ids[idx] = sampler.raw.gpuResourceID();
// Samplers aren't resources like buffers and textures, so don't
// need to be passed to useResource
}
}
wgt::BindingType::Buffer { ty, .. } => {
let start = entry.resource_index as usize;
let end = start + count as usize;
let buffers = &desc.buffers[start..end];
let pointers = argument_buffer.contents().cast::<u64>();
for (idx, source) in buffers.iter().enumerate() {
let addr = source.buffer.raw.gpuAddress() + source.offset;
unsafe {
pointers.add(idx).write(addr);
}
if let wgt::BufferBindingType::Storage { .. } = ty {
let remaining_size = wgt::BufferSize::new(
source.buffer.size - source.offset,
);
if let Some(binding_size) = source.size.or(remaining_size) {
array_element_sizes.push((idx as u32, binding_size));
}
}
let use_info = bg
.resources_to_use
.entry(source.buffer.as_raw().cast())
.or_default();
use_info.stages |= stages;
use_info.uses |= uses;
use_info.visible_in_compute |=
layout.visibility.contains(wgt::ShaderStages::COMPUTE);
}
}
wgt::BindingType::AccelerationStructure { .. } => {
let resource_ids =
bindless_id_table_mut(argument_buffer.as_ref(), count);
let start = entry.resource_index as usize;
let end = start + count as usize;
let acceleration_structures =
@@ -1031,7 +1101,7 @@ impl crate::Device for super::Device {
for (idx, &acceleration_structure) in
acceleration_structures.iter().enumerate()
{
contents[idx] = acceleration_structure.raw.gpuResourceID();
resource_ids[idx] = acceleration_structure.raw.gpuResourceID();
let use_info = bg
.resources_to_use
@@ -1048,16 +1118,31 @@ impl crate::Device for super::Device {
}
}
bg.buffers.push(super::BufferLikeResource::Buffer {
ptr: NonNull::from(&*buffer),
offset: 0,
dynamic_index: None,
binding_size: None,
binding_location: layout.binding,
});
if matches!(
layout.ty,
wgt::BindingType::Buffer {
ty: wgt::BufferBindingType::Storage { .. },
..
}
) {
bg.buffers
.push(super::BufferLikeResource::StorageBindingArray {
ptr: NonNull::from(&*argument_buffer),
array_element_sizes,
binding_location: layout.binding,
});
} else {
bg.buffers.push(super::BufferLikeResource::Buffer {
ptr: NonNull::from(&*argument_buffer),
offset: 0,
dynamic_index: None,
binding_size: None,
binding_location: layout.binding,
});
}
counter.buffers += 1;
bg.argument_buffers.push(buffer)
bg.argument_buffers.push(argument_buffer)
}
// Bindfull path
else {
+13 -2
View File
@@ -1020,6 +1020,7 @@ pub struct PipelineLayout {
immediates_infos: MultiStageData<Option<ImmediateDataInfo>>,
total_immediates: u32,
per_stage_map: MultiStageResources,
binding_array_length_map: FastHashMap<naga::ResourceBinding, u32>,
}
impl crate::DynPipelineLayout for PipelineLayout {}
@@ -1043,6 +1044,16 @@ enum BufferLikeResource {
binding_location: u32,
},
/// Bindless storage `binding_array`: one argument [`MTLBuffer`] (pointer table) plus element
/// byte sizes `(array index, size)` for `_buffer_sizes` / runtime-sized arrays.
///
/// [`MTLBuffer`]: objc2_metal::MTLBuffer
StorageBindingArray {
ptr: NonNull<ProtocolObject<dyn MTLBuffer>>,
array_element_sizes: Vec<(u32, wgt::BufferSize)>,
binding_location: u32,
},
AccelerationStructure(NonNull<ProtocolObject<dyn MTLAccelerationStructure>>),
}
@@ -1116,7 +1127,7 @@ struct PipelineStageInfo {
/// Bindings of all WGSL `storage` globals that contain runtime-sized arrays.
///
/// See `device::CompiledShader::sized_bindings` for more details.
sized_bindings: Vec<naga::ResourceBinding>,
sized_bindings: Vec<(naga::ResourceBinding, u32)>,
/// Info on all bound vertex buffers.
vertex_buffer_mappings: Vec<naga::back::msl::VertexBufferMapping>,
@@ -1317,7 +1328,7 @@ struct CommandState {
/// See `device::CompiledShader::sized_bindings` for more details.
///
/// [`ResourceBinding`]: naga::ResourceBinding
storage_buffer_length_map: FastHashMap<naga::ResourceBinding, wgt::BufferSize>,
storage_buffer_length_map: FastHashMap<(naga::ResourceBinding, u32), wgt::BufferSize>,
vertex_buffer_size_map: FastHashMap<u32, wgt::BufferSize>,