Add support for gaps in bind group layouts & update relevant validation (#9034)

* [wgpu-core] add support for optional BG & BGL entries

* [wgpu] allow gaps in bind group layouts

And allow unbinding in the web backend.

* adapt tests

* add changelog entry

* [wgpu] remove generic from `PipelineLayoutDescriptor`
This commit is contained in:
Teodor Tanasoaia authored and GitHub committed 2026-02-25 19:47:36 +01:00
1 parent aad2a00f4d
commit e114d2f0a7
90 files changed
+567 -397

No files matched your search

+81 -62
View File
@@ -1,5 +1,3 @@
use core::{iter::zip, ops::Range};
use alloc::{boxed::Box, sync::Arc, vec::Vec};
use thiserror::Error;
@@ -16,9 +14,8 @@ mod compat {
sync::{Arc, Weak},
vec::Vec,
};
use core::{num::NonZeroU32, ops::Range};
use core::num::NonZeroU32;
use arrayvec::ArrayVec;
use thiserror::Error;
use wgt::{BindingType, ShaderStages};
@@ -44,12 +41,13 @@ mod compat {
}
impl Entry {
fn empty() -> Self {
const fn empty() -> Self {
Self {
assigned: None,
expected: None,
}
}
fn is_active(&self) -> bool {
self.assigned.is_some() && self.expected.is_some()
}
@@ -62,14 +60,10 @@ mod compat {
false
}
} else {
true
false
}
}
fn is_incompatible(&self) -> bool {
self.expected.is_none() || !self.is_valid()
}
fn check(&self) -> Result<(), Error> {
if let Some(expected_bgl) = self.expected.as_ref() {
if let Some(assigned_bgl) = self.assigned.as_ref() {
@@ -191,64 +185,69 @@ mod compat {
}
}
#[derive(Debug, Default)]
#[derive(Debug)]
pub(super) struct BoundBindGroupLayouts {
entries: ArrayVec<Entry, { hal::MAX_BIND_GROUPS }>,
entries: [Entry; hal::MAX_BIND_GROUPS],
rebind_start: usize,
}
impl BoundBindGroupLayouts {
pub fn new() -> Self {
Self {
entries: (0..hal::MAX_BIND_GROUPS).map(|_| Entry::empty()).collect(),
entries: [const { Entry::empty() }; hal::MAX_BIND_GROUPS],
rebind_start: 0,
}
}
pub fn num_valid_entries(&self) -> usize {
// find first incompatible entry
self.entries
.iter()
.position(|e| e.is_incompatible())
.unwrap_or(self.entries.len())
}
/// Get the range of entries that needs to be rebound, and clears it.
pub fn take_rebind_range(&mut self) -> Range<usize> {
let end = self.num_valid_entries();
/// Takes the start index of the bind group range to be rebound, and clears it.
pub fn take_rebind_start_index(&mut self) -> usize {
let start = self.rebind_start;
self.rebind_start = end;
start..end.max(start)
self.rebind_start = self.entries.len();
start
}
pub fn update_start_index(&mut self, start_index: usize) {
pub fn update_rebind_start_index(&mut self, start_index: usize) {
self.rebind_start = self.rebind_start.min(start_index);
}
pub fn update_expectations(&mut self, expectations: &[Arc<BindGroupLayout>]) {
let start_index = self
pub fn update_expectations(&mut self, expectations: &[Option<Arc<BindGroupLayout>>]) {
let mut rebind_start_index = None;
for (i, (e, new_expected_bgl)) in self
.entries
.iter()
.zip(expectations)
.position(|(e, expect)| {
e.expected.is_none() || !e.expected.as_ref().unwrap().is_equal(expect)
})
.unwrap_or(expectations.len());
for (e, expect) in self.entries[start_index..]
.iter_mut()
.zip(expectations[start_index..].iter())
.zip(expectations.iter().chain(core::iter::repeat(&None)))
.enumerate()
{
e.expected = Some(expect.clone());
let (must_set, must_rebind) = match (&mut e.expected, new_expected_bgl) {
(None, None) => (false, false),
(None, Some(_)) => (true, true),
(Some(_), None) => (true, false),
(Some(old_expected_bgl), Some(new_expected_bgl)) => {
let is_different = !old_expected_bgl.is_equal(new_expected_bgl);
(is_different, is_different)
}
};
if must_set {
e.expected = new_expected_bgl.clone();
}
if must_rebind && rebind_start_index.is_none() {
rebind_start_index = Some(i);
}
}
for e in self.entries[expectations.len()..].iter_mut() {
e.expected = None;
if let Some(rebind_start_index) = rebind_start_index {
self.update_rebind_start_index(rebind_start_index);
}
self.update_start_index(start_index);
}
pub fn assign(&mut self, index: usize, value: Arc<BindGroupLayout>) {
self.entries[index].assigned = Some(value);
self.update_start_index(index);
self.update_rebind_start_index(index);
}
pub fn clear(&mut self, index: usize) {
self.entries[index].assigned = None;
}
pub fn list_active(&self) -> impl Iterator<Item = usize> + '_ {
@@ -258,6 +257,13 @@ mod compat {
.filter_map(|(i, e)| if e.is_active() { Some(i) } else { None })
}
pub fn list_valid(&self) -> impl Iterator<Item = usize> + '_ {
self.entries
.iter()
.enumerate()
.filter_map(|(i, e)| if e.is_valid() { Some(i) } else { None })
}
#[allow(clippy::result_large_err)]
pub fn get_invalid(&self) -> Result<(), (usize, Error)> {
for (index, entry) in self.entries.iter().enumerate() {
@@ -295,13 +301,13 @@ struct LateBufferBinding {
}
#[derive(Debug, Default)]
pub(super) struct EntryPayload {
pub(super) group: Option<Arc<BindGroup>>,
pub(super) dynamic_offsets: Vec<wgt::DynamicOffset>,
struct EntryPayload {
group: Option<Arc<BindGroup>>,
dynamic_offsets: Vec<wgt::DynamicOffset>,
late_buffer_bindings: Vec<LateBufferBinding>,
/// Since `LateBufferBinding` may contain information about the bindings
/// not used by the pipeline, we need to know when to stop validating.
pub(super) late_bindings_effective_count: usize,
late_bindings_effective_count: usize,
}
impl EntryPayload {
@@ -313,7 +319,7 @@ impl EntryPayload {
}
}
#[derive(Debug, Default)]
#[derive(Debug)]
pub(super) struct Binder {
pub(super) pipeline_layout: Option<Arc<PipelineLayout>>,
manager: compat::BoundBindGroupLayouts,
@@ -383,7 +389,7 @@ impl Binder {
if let Some(old) = old {
// root constants are the base compatibility property
if old.immediate_size != new.immediate_size {
self.manager.update_start_index(0);
self.manager.update_rebind_start_index(0);
}
}
@@ -431,31 +437,44 @@ impl Binder {
self.manager.assign(index, bind_group.layout.clone());
}
/// Get the range of entries that needs to be rebound, and clears it.
pub(super) fn take_rebind_range(&mut self) -> Range<usize> {
self.manager.take_rebind_range()
pub(super) fn clear_group(&mut self, index: usize) {
self.payloads[index].reset();
self.manager.clear(index);
}
pub(super) fn entries(
/// Takes the start index of the bind group range to be rebound, and clears it.
pub(super) fn take_rebind_start_index(&mut self) -> usize {
self.manager.take_rebind_start_index()
}
pub(super) fn list_valid_with_start(
&self,
range: Range<usize>,
) -> impl ExactSizeIterator<Item = (usize, &'_ EntryPayload)> + Clone + '_ {
let payloads = &self.payloads[range.clone()];
zip(range, payloads)
start: usize,
) -> impl Iterator<Item = (usize, &Arc<BindGroup>, &[wgt::DynamicOffset])> + '_ {
let payloads = &self.payloads;
self.manager
.list_valid()
.filter(move |i| *i >= start)
.map(move |index| {
(
index,
payloads[index].group.as_ref().unwrap(),
payloads[index].dynamic_offsets.as_slice(),
)
})
}
pub(super) fn list_active<'a>(&'a self) -> impl Iterator<Item = &'a Arc<BindGroup>> + 'a {
pub(super) fn list_active(&self) -> impl Iterator<Item = &Arc<BindGroup>> + '_ {
let payloads = &self.payloads;
self.manager
.list_active()
.map(move |index| payloads[index].group.as_ref().unwrap())
}
pub(super) fn list_valid<'a>(&'a self) -> impl Iterator<Item = (usize, &'a EntryPayload)> + 'a {
self.payloads
.iter()
.take(self.manager.num_valid_entries())
.enumerate()
pub(super) fn list_valid(
&self,
) -> impl Iterator<Item = (usize, &Arc<BindGroup>, &[wgt::DynamicOffset])> + '_ {
self.list_valid_with_start(0)
}
pub(super) fn check_compatibility<T: Labeled>(
+40 -41
View File
@@ -522,17 +522,6 @@ fn set_bind_group(
num_dynamic_offsets: usize,
bind_group_id: Option<id::Id<id::markers::BindGroup>>,
) -> Result<(), RenderBundleErrorInner> {
if bind_group_id.is_none() {
// TODO: do appropriate cleanup for null bind_group.
return Ok(());
}
let bind_group_id = bind_group_id.unwrap();
let bind_group = bind_group_guard.get(bind_group_id).get()?;
bind_group.same_device(&state.device)?;
let max_bind_groups = state.device.limits.max_bind_groups;
if index >= max_bind_groups {
return Err(
@@ -549,14 +538,31 @@ fn set_bind_group(
state.next_dynamic_offset = offsets_range.end;
let offsets = &dynamic_offsets[offsets_range.clone()];
bind_group.validate_dynamic_bindings(index, offsets)?;
let bind_group = bind_group_id.map(|id| bind_group_guard.get(id));
unsafe { state.trackers.merge_bind_group(&bind_group.used)? };
let bind_group = state.trackers.bind_groups.insert_single(bind_group);
if let Some(bind_group) = bind_group {
let bind_group = bind_group.get()?;
bind_group.same_device(&state.device)?;
bind_group.validate_dynamic_bindings(index, offsets)?;
state
.binder
.assign_group(index as usize, bind_group, offsets);
unsafe { state.trackers.merge_bind_group(&bind_group.used)? };
let bind_group = state.trackers.bind_groups.insert_single(bind_group);
state
.binder
.assign_group(index as usize, bind_group, offsets);
} else {
if !offsets.is_empty() {
return Err(RenderBundleErrorInner::Bind(
BindError::DynamicOffsetCountNotZero {
group: index,
actual: offsets.len(),
},
));
}
state.binder.clear_group(index as usize);
}
Ok(())
}
@@ -977,17 +983,12 @@ impl RenderBundle {
num_dynamic_offsets,
bind_group,
} => {
let mut bg = None;
if bind_group.is_some() {
let bind_group = bind_group.as_ref().unwrap();
let raw_bg = bind_group.try_raw(snatch_guard)?;
bg = Some(raw_bg);
}
let raw_bg = bind_group.as_ref().unwrap().try_raw(snatch_guard)?;
unsafe {
raw.set_bind_group(
pipeline_layout.as_ref().unwrap().raw(),
*index,
bg,
raw_bg,
&offsets[..*num_dynamic_offsets],
)
};
@@ -1435,26 +1436,24 @@ impl State {
///
/// This should be further deduplicated with similar code on render/compute passes.
fn flush_bindings(&mut self) {
let range = self.binder.take_rebind_range();
let entries = self.binder.entries(range);
let start = self.binder.take_rebind_start_index();
let entries = self.binder.list_valid_with_start(start);
self.commands.extend(entries.map(|(i, entry)| {
let bind_group = entry.group.as_ref().unwrap();
self.commands
.extend(entries.map(|(i, bind_group, dynamic_offsets)| {
self.buffer_memory_init_actions
.extend_from_slice(&bind_group.used_buffer_ranges);
self.texture_memory_init_actions
.extend_from_slice(&bind_group.used_texture_ranges);
self.buffer_memory_init_actions
.extend_from_slice(&bind_group.used_buffer_ranges);
self.texture_memory_init_actions
.extend_from_slice(&bind_group.used_texture_ranges);
self.flat_dynamic_offsets.extend_from_slice(dynamic_offsets);
self.flat_dynamic_offsets
.extend_from_slice(&entry.dynamic_offsets);
ArcRenderCommand::SetBindGroup {
index: i.try_into().unwrap(),
bind_group: Some(bind_group.clone()),
num_dynamic_offsets: entry.dynamic_offsets.len(),
}
}));
ArcRenderCommand::SetBindGroup {
index: i.try_into().unwrap(),
bind_group: Some(bind_group.clone()),
num_dynamic_offsets: dynamic_offsets.len(),
}
}));
}
fn vertex_buffer_sizes(&self) -> impl Iterator<Item = Option<wgt::BufferAddress>> + '_ {
+10 -13
View File
@@ -933,7 +933,7 @@ fn dispatch_indirect(
state.pass.base.raw_encoder.set_bind_group(
params.pipeline_layout,
0,
Some(params.dst_bind_group),
params.dst_bind_group,
&[],
);
}
@@ -941,14 +941,12 @@ fn dispatch_indirect(
state.pass.base.raw_encoder.set_bind_group(
params.pipeline_layout,
1,
Some(
buffer
.indirect_validation_bind_groups
.get(state.pass.base.snatch_guard)
.unwrap()
.dispatch
.as_ref(),
),
buffer
.indirect_validation_bind_groups
.get(state.pass.base.snatch_guard)
.unwrap()
.dispatch
.as_ref(),
&[params.aligned_offset as u32],
);
}
@@ -1007,15 +1005,14 @@ fn dispatch_indirect(
}
}
for (i, e) in state.pass.binder.list_valid() {
let group = e.group.as_ref().unwrap();
for (i, group, dynamic_offsets) in state.pass.binder.list_valid() {
let raw_bg = group.try_raw(state.pass.base.snatch_guard)?;
unsafe {
state.pass.base.raw_encoder.set_bind_group(
pipeline.layout.raw(),
i as u32,
Some(raw_bg),
&e.dynamic_offsets,
raw_bg,
dynamic_offsets,
);
}
}
+44 -48
View File
@@ -20,7 +20,7 @@ use wgt::DynamicOffset;
#[derive(Clone, Debug, Error)]
#[error(
"Bind group index {index} is greater than the device's requested `max_bind_group` limit {max}"
"Bind group index {index} is greater than the device's configured `max_bind_groups` limit {max}"
)]
pub struct BindGroupIndexOutOfRange {
pub index: u32,
@@ -97,35 +97,42 @@ where
);
state.dynamic_offset_count += num_dynamic_offsets;
let Some(bind_group) = bind_group else {
// TODO: Handle bind_group None.
return Ok(());
};
if let Some(bind_group) = bind_group {
// Add the bind group to the tracker. This is done for both compute and
// render passes, and is used to fail submission of the command buffer if
// any resource in any of the bind groups has been destroyed, whether or
// not the bind group is actually used by the pipeline.
let bind_group = state.base.tracker.bind_groups.insert_single(bind_group);
// Add the bind group to the tracker. This is done for both compute and
// render passes, and is used to fail submission of the command buffer if
// any resource in any of the bind groups has been destroyed, whether or
// not the bind group is actually used by the pipeline.
let bind_group = state.base.tracker.bind_groups.insert_single(bind_group);
bind_group.same_device(device)?;
bind_group.same_device(device)?;
bind_group.validate_dynamic_bindings(index, &state.temp_offsets)?;
bind_group.validate_dynamic_bindings(index, &state.temp_offsets)?;
if merge_bind_groups {
// Merge the bind group's resources into the tracker. We only do this
// for render passes. For compute passes it is done per dispatch in
// [`flush_bindings`].
unsafe {
state.scope.merge_bind_group(&bind_group.used)?;
if merge_bind_groups {
// Merge the bind group's resources into the tracker. We only do this
// for render passes. For compute passes it is done per dispatch in
// [`flush_bindings`].
unsafe {
state.scope.merge_bind_group(&bind_group.used)?;
}
}
}
//Note: stateless trackers are not merged: the lifetime reference
// is held to the bind group itself.
//Note: stateless trackers are not merged: the lifetime reference
// is held to the bind group itself.
state
.binder
.assign_group(index as usize, bind_group, &state.temp_offsets);
state
.binder
.assign_group(index as usize, bind_group, &state.temp_offsets);
} else {
if !state.temp_offsets.is_empty() {
return Err(BindError::DynamicOffsetCountNotZero {
group: index,
actual: state.temp_offsets.len(),
}
.into());
}
state.binder.clear_group(index as usize);
};
Ok(())
}
@@ -135,16 +142,11 @@ where
/// See the compute pass version of `State::flush_bindings` for an explanation
/// of some differences in handling the two types of passes.
pub(super) fn flush_bindings_helper(state: &mut PassState) -> Result<(), DestroyedResourceError> {
let range = state.binder.take_rebind_range();
if range.is_empty() {
return Ok(());
}
let entries = state.binder.entries(range);
for (_, entry) in entries.clone() {
let bind_group = entry.group.as_ref().unwrap();
let start = state.binder.take_rebind_start_index();
let entries = state.binder.list_valid_with_start(start);
let pipeline_layout = state.binder.pipeline_layout.as_ref().unwrap();
for (i, bind_group, dynamic_offsets) in entries {
state.base.buffer_memory_init_actions.extend(
bind_group.used_buffer_ranges.iter().filter_map(|action| {
action
@@ -170,21 +172,15 @@ pub(super) fn flush_bindings_helper(state: &mut PassState) -> Result<(), Destroy
.map(|tlas| crate::ray_tracing::AsAction::UseTlas(tlas.clone()));
state.base.as_actions.extend(used_resource);
}
if let Some(pipeline_layout) = state.binder.pipeline_layout.as_ref() {
for (i, e) in entries {
if let Some(group) = e.group.as_ref() {
let raw_bg = group.try_raw(state.base.snatch_guard)?;
unsafe {
state.base.raw_encoder.set_bind_group(
pipeline_layout.raw(),
i as u32,
Some(raw_bg),
&e.dynamic_offsets,
);
}
}
let raw_bg = bind_group.try_raw(state.base.snatch_guard)?;
unsafe {
state.base.raw_encoder.set_bind_group(
pipeline_layout.raw(),
i as u32,
raw_bg,
dynamic_offsets,
);
}
}