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

+1 -1
View File
@@ -1127,7 +1127,7 @@ impl crate::CommandEncoder for super::CommandEncoder {
group: &super::BindGroup,
dynamic_offsets: &[wgt::DynamicOffset],
) {
let info = &layout.bind_group_infos[index as usize];
let info = layout.bind_group_infos[index as usize].as_ref().unwrap();
let mut root_index = info.base_root_index as usize;
// Bind CBV/SRC/UAV descriptor tables
+10 -3
View File
@@ -950,6 +950,10 @@ impl crate::Device for super::Device {
let mut total_non_dynamic_entries = 0_usize;
let mut sampler_in_any_bind_group = false;
for bgl in desc.bind_group_layouts {
let Some(bgl) = bgl else {
continue;
};
let mut sampler_in_bind_group = false;
for entry in &bgl.entries {
@@ -980,9 +984,12 @@ impl crate::Device for super::Device {
let mut ranges = Vec::with_capacity(total_non_dynamic_entries);
let mut bind_group_infos =
ArrayVec::<super::BindGroupInfo, { crate::MAX_BIND_GROUPS }>::default();
let mut bind_group_infos = [const { None }; crate::MAX_BIND_GROUPS];
for (index, bgl) in desc.bind_group_layouts.iter().enumerate() {
let Some(bgl) = bgl else {
continue;
};
let mut info = super::BindGroupInfo {
tables: super::TableTypes::empty(),
base_root_index: parameters.len() as u32,
@@ -1247,7 +1254,7 @@ impl crate::Device for super::Device {
total_dynamic_storage_buffers += dynamic_storage_buffers;
}
bind_group_infos.push(info);
bind_group_infos[index] = Some(info);
}
let sampler_heap_target = hlsl::SamplerHeapBindTargets {
+1 -1
View File
@@ -1119,7 +1119,7 @@ pub struct PipelineLayout {
shared: PipelineLayoutShared,
// Storing for each associated bind group, which tables we created
// in the root signature. This is required for binding descriptor sets.
bind_group_infos: ArrayVec<BindGroupInfo, { crate::MAX_BIND_GROUPS }>,
bind_group_infos: [Option<BindGroupInfo>; crate::MAX_BIND_GROUPS],
naga_options: naga::back::hlsl::Options,
}
+2 -8
View File
@@ -62,7 +62,7 @@ pub trait DynCommandEncoder: DynResource + core::fmt::Debug {
&mut self,
layout: &dyn DynPipelineLayout,
index: u32,
group: Option<&dyn DynBindGroup>,
group: &dyn DynBindGroup,
dynamic_offsets: &[wgt::DynamicOffset],
);
@@ -317,15 +317,9 @@ impl<C: CommandEncoder + DynResource> DynCommandEncoder for C {
&mut self,
layout: &dyn DynPipelineLayout,
index: u32,
group: Option<&dyn DynBindGroup>,
group: &dyn DynBindGroup,
dynamic_offsets: &[wgt::DynamicOffset],
) {
if group.is_none() {
// TODO: Handle group None correctly.
return;
}
let group = group.unwrap();
let layout = layout.expect_downcast_ref();
let group = group.expect_downcast_ref();
unsafe { C::set_bind_group(self, layout, index, group, dynamic_offsets) };
+1 -1
View File
@@ -290,7 +290,7 @@ impl<D: Device + DynResource> DynDevice for D {
let bind_group_layouts: Vec<_> = desc
.bind_group_layouts
.iter()
.map(|bgl| bgl.expect_downcast_ref())
.map(|bgl| bgl.map(|bgl| bgl.expect_downcast_ref()))
.collect();
let desc = PipelineLayoutDescriptor {
label: desc.label,
+1 -1
View File
@@ -718,7 +718,7 @@ impl crate::CommandEncoder for super::CommandEncoder {
let mut do_index = 0;
let mut dirty_textures = 0u32;
let mut dirty_samplers = 0u32;
let group_info = &layout.group_infos[index as usize];
let group_info = layout.group_infos[index as usize].as_ref().unwrap();
for (binding_layout, raw_binding) in group_info.entries.iter().zip(group.contents.iter()) {
let slot = group_info.binding_to_slot[binding_layout.binding as usize] as u32;
+12 -6
View File
@@ -321,10 +321,11 @@ impl super::Device {
multiview_mask: Option<NonZeroU32>,
) -> Result<Arc<super::PipelineInner>, crate::PipelineError> {
let mut program_stages = ArrayVec::new();
let mut group_to_binding_to_slot = Vec::with_capacity(layout.group_infos.len());
for group in &*layout.group_infos {
group_to_binding_to_slot.push(group.binding_to_slot.clone());
}
let group_to_binding_to_slot = layout
.group_infos
.iter()
.map(|group| group.as_ref().map(|group| group.binding_to_slot.clone()))
.collect::<Vec<_>>();
for &(naga_stage, stage) in &shaders {
program_stages.push(super::ProgramStage {
naga_stage: naga_stage.to_owned(),
@@ -1198,6 +1199,11 @@ impl crate::Device for super::Device {
let mut binding_map = glsl::BindingMap::default();
for (group_index, bg_layout) in desc.bind_group_layouts.iter().enumerate() {
let Some(bg_layout) = bg_layout else {
group_infos.push(None);
continue;
};
// create a vector with the size enough to hold all the bindings, filled with `!0`
let mut binding_to_slot = vec![
!0;
@@ -1236,10 +1242,10 @@ impl crate::Device for super::Device {
*counter += entry.count.map_or(1, |c| c.get() as u8);
}
group_infos.push(super::BindGroupLayoutInfo {
group_infos.push(Some(super::BindGroupLayoutInfo {
entries: Arc::clone(&bg_layout.entries),
binding_to_slot,
});
}));
}
self.counters.pipeline_layouts.add(1);
+6 -3
View File
@@ -553,15 +553,18 @@ struct BindGroupLayoutInfo {
#[derive(Debug)]
pub struct PipelineLayout {
group_infos: Box<[BindGroupLayoutInfo]>,
group_infos: Box<[Option<BindGroupLayoutInfo>]>,
naga_options: naga::back::glsl::Options,
}
impl crate::DynPipelineLayout for PipelineLayout {}
impl PipelineLayout {
/// # Panics
/// If the pipeline layout does not contain a bind group layout used by
/// the resource binding.
fn get_slot(&self, br: &naga::ResourceBinding) -> u8 {
let group_info = &self.group_infos[br.group as usize];
let group_info = self.group_infos[br.group as usize].as_ref().unwrap();
group_info.binding_to_slot[br.binding as usize]
}
}
@@ -709,7 +712,7 @@ struct ProgramStage {
#[derive(PartialEq, Eq, Hash)]
struct ProgramCacheKey {
stages: ArrayVec<ProgramStage, 3>,
group_to_binding_to_slot: Box<[Box<[u8]>]>,
group_to_binding_to_slot: Box<[Option<Box<[u8]>>]>,
}
type ProgramCache = FastHashMap<ProgramCacheKey, Result<Arc<PipelineInner>, crate::PipelineError>>;
+1 -1
View File
@@ -2094,7 +2094,7 @@ pub struct BindGroupLayoutDescriptor<'a> {
pub struct PipelineLayoutDescriptor<'a, B: DynBindGroupLayout + ?Sized> {
pub label: Label<'a>,
pub flags: PipelineLayoutFlags,
pub bind_group_layouts: &'a [&'a B],
pub bind_group_layouts: &'a [Option<&'a B>],
pub immediate_size: u32,
}
+3 -1
View File
@@ -1025,7 +1025,9 @@ impl crate::CommandEncoder for super::CommandEncoder {
group: &super::BindGroup,
dynamic_offsets: &[wgt::DynamicOffset],
) {
let bg_info = &layout.bind_group_infos[group_index as usize];
let bg_info = layout.bind_group_infos[group_index as usize]
.as_ref()
.unwrap();
let render_encoder = self.state.render.clone();
let compute_encoder = self.state.compute.clone();
if let Some(encoder) = render_encoder {
+7 -3
View File
@@ -728,7 +728,7 @@ impl crate::Device for super::Device {
need_sizes_buffer: false,
resources: Default::default(),
});
let mut bind_group_infos = arrayvec::ArrayVec::new();
let mut bind_group_infos = [const { None }; crate::MAX_BIND_GROUPS];
// First, place the immediates
for info in stage_data.iter_mut() {
@@ -742,7 +742,11 @@ impl crate::Device for super::Device {
}
// Second, place the described resources
for (group_index, &bgl) in desc.bind_group_layouts.iter().enumerate() {
for (group_index, bgl) in desc.bind_group_layouts.iter().enumerate() {
let Some(bgl) = bgl else {
continue;
};
// remember where the resources for this set start at each shader stage
let base_resource_indices = stage_data.map_ref(|info| info.counters.clone());
@@ -827,7 +831,7 @@ impl crate::Device for super::Device {
}
}
bind_group_infos.push(super::BindGroupLayoutInfo {
bind_group_infos[group_index] = Some(super::BindGroupLayoutInfo {
base_resource_indices,
});
}
+1 -2
View File
@@ -33,7 +33,6 @@ use alloc::{
};
use core::{fmt, iter, ops, ptr::NonNull, sync::atomic};
use arrayvec::ArrayVec;
use bitflags::bitflags;
use hashbrown::HashMap;
use naga::FastHashMap;
@@ -734,7 +733,7 @@ struct ImmediateDataInfo {
#[derive(Debug)]
pub struct PipelineLayout {
bind_group_infos: ArrayVec<BindGroupLayoutInfo, { crate::MAX_BIND_GROUPS }>,
bind_group_infos: [Option<BindGroupLayoutInfo>; crate::MAX_BIND_GROUPS],
immediates_infos: MultiStageData<Option<ImmediateDataInfo>>,
total_counters: MultiStageResourceCounters,
total_immediates: u32,
+7
View File
@@ -2514,6 +2514,12 @@ impl super::Adapter {
let drop_guard = crate::DropGuard::from_option(drop_callback);
let empty_descriptor_set_layout = unsafe {
raw_device
.create_descriptor_set_layout(&vk::DescriptorSetLayoutCreateInfo::default(), None)
.map_err(super::map_host_device_oom_err)?
};
let shared = Arc::new(super::DeviceShared {
raw: raw_device,
family_index,
@@ -2544,6 +2550,7 @@ impl super::Adapter {
texture_identity_factory: super::ResourceIdentityFactory::new(),
texture_view_identity_factory: super::ResourceIdentityFactory::new(),
empty_descriptor_set_layout,
});
let relay_semaphores = super::RelaySemaphores::new(&shared)?;
+18 -2
View File
@@ -1420,7 +1420,19 @@ impl crate::Device for super::Device {
let vk_set_layouts = desc
.bind_group_layouts
.iter()
.map(|bgl| bgl.raw)
.map(|bgl| match bgl {
Some(bgl) => bgl.raw,
None => {
// `VUID-VkPipelineLayoutCreateInfo-pSetLayouts-parameter`
// says `VK_NULL_HANDLE` is allowed but
// `VUID-VkPipelineLayoutCreateInfo-graphicsPipelineLibrary-06753`
// says it's not, unless the `graphicsPipelineLibrary`
// feature is enabled.
//
// We use an empty descriptor set layout to work around this.
self.shared.empty_descriptor_set_layout
}
})
.collect::<Vec<_>>();
let vk_immediates_ranges: Option<vk::PushConstantRange> = if desc.immediate_size != 0 {
Some(vk::PushConstantRange {
@@ -1452,7 +1464,11 @@ impl crate::Device for super::Device {
}
let mut binding_map = BTreeMap::new();
for (group, &layout) in desc.bind_group_layouts.iter().enumerate() {
for (group, layout) in desc.bind_group_layouts.iter().enumerate() {
let Some(layout) = layout else {
continue;
};
for &(binding, binding_info) in &layout.binding_map {
binding_map.insert(
naga::ResourceBinding {
+6
View File
@@ -495,6 +495,8 @@ struct DeviceShared {
/// As above, for texture views.
texture_view_identity_factory: ResourceIdentityFactory<vk::ImageView>,
empty_descriptor_set_layout: vk::DescriptorSetLayout,
// The `drop_guard` field must be the last field of this struct so it is dropped last.
// Do not add new fields after it.
drop_guard: Option<crate::DropGuard>,
@@ -505,6 +507,10 @@ impl Drop for DeviceShared {
for &raw in self.render_passes.lock().values() {
unsafe { self.raw.destroy_render_pass(raw, None) };
}
unsafe {
self.raw
.destroy_descriptor_set_layout(self.empty_descriptor_set_layout, None)
};
if self.drop_guard.is_none() {
unsafe { self.raw.destroy_device(None) };
}