mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-26 01:11:22 +01:00
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:
1 parent
aad2a00f4d
commit
e114d2f0a7
90 files changed
+567
-397
No files matched your search
@@ -732,7 +732,10 @@ impl Global {
|
||||
let bind_group_layouts_guard = hub.bind_group_layouts.read();
|
||||
desc.bind_group_layouts
|
||||
.iter()
|
||||
.map(|bgl_id| bind_group_layouts_guard.get(*bgl_id).get())
|
||||
.map(|bgl_id| match bgl_id {
|
||||
Some(bgl_id) => bind_group_layouts_guard.get(*bgl_id).get().map(Some),
|
||||
None => Ok(None),
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
};
|
||||
|
||||
|
||||
@@ -209,6 +209,7 @@ pub struct Device {
|
||||
pub(crate) adapter: Arc<Adapter>,
|
||||
pub(crate) queue: OnceCellOrLock<Weak<Queue>>,
|
||||
pub(crate) zero_buffer: ManuallyDrop<Box<dyn hal::DynBuffer>>,
|
||||
pub(crate) empty_bgl: ManuallyDrop<Box<dyn hal::DynBindGroupLayout>>,
|
||||
/// The `label` from the descriptor used to create the resource.
|
||||
label: String,
|
||||
|
||||
@@ -298,6 +299,8 @@ impl Drop for Device {
|
||||
|
||||
// SAFETY: We are in the Drop impl and we don't use self.zero_buffer anymore after this point.
|
||||
let zero_buffer = unsafe { ManuallyDrop::take(&mut self.zero_buffer) };
|
||||
// SAFETY: We are in the Drop impl and we don't use self.empty_bgl anymore after this point.
|
||||
let empty_bgl = unsafe { ManuallyDrop::take(&mut self.empty_bgl) };
|
||||
// SAFETY: We are in the Drop impl and we don't use
|
||||
// self.default_external_texture_params_buffer anymore after this point.
|
||||
let default_external_texture_params_buffer =
|
||||
@@ -312,6 +315,7 @@ impl Drop for Device {
|
||||
}
|
||||
unsafe {
|
||||
self.raw.destroy_buffer(zero_buffer);
|
||||
self.raw.destroy_bind_group_layout(empty_bgl);
|
||||
self.raw
|
||||
.destroy_buffer(default_external_texture_params_buffer);
|
||||
self.raw.destroy_fence(fence);
|
||||
@@ -453,6 +457,15 @@ impl Device {
|
||||
}
|
||||
.map_err(DeviceError::from_hal)?;
|
||||
|
||||
let empty_bgl = unsafe {
|
||||
raw_device.create_bind_group_layout(&hal::BindGroupLayoutDescriptor {
|
||||
label: None,
|
||||
flags: hal::BindGroupLayoutFlags::empty(),
|
||||
entries: &[],
|
||||
})
|
||||
}
|
||||
.map_err(DeviceError::from_hal)?;
|
||||
|
||||
let default_external_texture_params_buffer = unsafe {
|
||||
raw_device.create_buffer(&hal::BufferDescriptor {
|
||||
label: hal_label(
|
||||
@@ -495,6 +508,7 @@ impl Device {
|
||||
adapter: adapter.clone(),
|
||||
queue: OnceCellOrLock::new(),
|
||||
zero_buffer: ManuallyDrop::new(zero_buffer),
|
||||
empty_bgl: ManuallyDrop::new(empty_bgl),
|
||||
default_external_texture_params_buffer: ManuallyDrop::new(
|
||||
default_external_texture_params_buffer,
|
||||
),
|
||||
@@ -2471,8 +2485,12 @@ impl Device {
|
||||
.bind_group_layouts
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(group_index, bgl)| pipeline::LateSizedBufferGroup {
|
||||
shader_sizes: bgl
|
||||
.map(|(group_index, bgl)| {
|
||||
let Some(bgl) = bgl else {
|
||||
return pipeline::LateSizedBufferGroup::default();
|
||||
};
|
||||
|
||||
let shader_sizes = bgl
|
||||
.entries
|
||||
.values()
|
||||
.filter_map(|entry| match entry.ty {
|
||||
@@ -2490,7 +2508,8 @@ impl Device {
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.collect(),
|
||||
.collect();
|
||||
pipeline::LateSizedBufferGroup { shader_sizes }
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
@@ -2518,9 +2537,7 @@ impl Device {
|
||||
}
|
||||
}
|
||||
|
||||
/// Internal function exposed for use by `player` crate only.
|
||||
#[doc(hidden)]
|
||||
pub fn create_bind_group_layout_internal(
|
||||
fn create_bind_group_layout_internal(
|
||||
self: &Arc<Self>,
|
||||
label: &crate::Label,
|
||||
entry_map: bgl::EntryMap,
|
||||
@@ -2775,7 +2792,7 @@ impl Device {
|
||||
.map_err(|e| self.handle_hal_error(e))?;
|
||||
|
||||
let bgl = BindGroupLayout {
|
||||
raw: ManuallyDrop::new(raw),
|
||||
raw: binding_model::RawBindGroupLayout::Owning(ManuallyDrop::new(raw)),
|
||||
device: self.clone(),
|
||||
entries: entry_map,
|
||||
origin,
|
||||
@@ -3598,6 +3615,14 @@ impl Device {
|
||||
pub fn create_pipeline_layout(
|
||||
self: &Arc<Self>,
|
||||
desc: &binding_model::ResolvedPipelineLayoutDescriptor,
|
||||
) -> Result<Arc<binding_model::PipelineLayout>, binding_model::CreatePipelineLayoutError> {
|
||||
self.create_pipeline_layout_impl(desc, false)
|
||||
}
|
||||
|
||||
fn create_pipeline_layout_impl(
|
||||
self: &Arc<Self>,
|
||||
desc: &binding_model::ResolvedPipelineLayoutDescriptor,
|
||||
ignore_exclusive_pipeline_check: bool,
|
||||
) -> Result<Arc<binding_model::PipelineLayout>, binding_model::CreatePipelineLayoutError> {
|
||||
use crate::binding_model::CreatePipelineLayoutError as Error;
|
||||
|
||||
@@ -3632,8 +3657,23 @@ impl Device {
|
||||
|
||||
let mut count_validator = binding_model::BindingTypeMaxCountValidator::default();
|
||||
|
||||
for bgl in desc.bind_group_layouts.iter() {
|
||||
for (index, bgl) in desc.bind_group_layouts.iter().enumerate() {
|
||||
let Some(bgl) = bgl else {
|
||||
continue;
|
||||
};
|
||||
|
||||
bgl.same_device(self)?;
|
||||
|
||||
if !ignore_exclusive_pipeline_check {
|
||||
let exclusive_pipeline = bgl.exclusive_pipeline.get().unwrap();
|
||||
if !matches!(exclusive_pipeline, binding_model::ExclusivePipeline::None) {
|
||||
return Err(Error::BglHasExclusivePipeline {
|
||||
index,
|
||||
pipeline: alloc::format!("{exclusive_pipeline}"),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
count_validator.merge(&bgl.binding_count_validator);
|
||||
}
|
||||
|
||||
@@ -3641,16 +3681,18 @@ impl Device {
|
||||
.validate(&self.limits)
|
||||
.map_err(Error::TooManyBindings)?;
|
||||
|
||||
let bind_group_layouts = desc
|
||||
.bind_group_layouts
|
||||
.iter()
|
||||
.cloned()
|
||||
let get_bgl_iter = || {
|
||||
desc.bind_group_layouts
|
||||
.iter()
|
||||
.map(|bgl| bgl.as_ref().filter(|bgl| !bgl.entries.is_empty()))
|
||||
};
|
||||
|
||||
let bind_group_layouts = get_bgl_iter()
|
||||
.map(|bgl| bgl.cloned())
|
||||
.collect::<ArrayVec<_, { hal::MAX_BIND_GROUPS }>>();
|
||||
|
||||
let raw_bind_group_layouts = desc
|
||||
.bind_group_layouts
|
||||
.iter()
|
||||
.map(|bgl| bgl.raw())
|
||||
let raw_bind_group_layouts = get_bgl_iter()
|
||||
.map(|bgl| bgl.map(|bgl| bgl.raw()))
|
||||
.collect::<ArrayVec<_, { hal::MAX_BIND_GROUPS }>>();
|
||||
|
||||
let additional_flags = if self.indirect_validation.is_some() {
|
||||
@@ -3686,7 +3728,7 @@ impl Device {
|
||||
Ok(layout)
|
||||
}
|
||||
|
||||
pub(crate) fn derive_pipeline_layout(
|
||||
fn create_derived_pipeline_layout(
|
||||
self: &Arc<Self>,
|
||||
mut derived_group_layouts: Box<ArrayVec<bgl::EntryMap, { hal::MAX_BIND_GROUPS }>>,
|
||||
) -> Result<Arc<binding_model::PipelineLayout>, pipeline::ImplicitLayoutError> {
|
||||
@@ -3702,9 +3744,13 @@ impl Device {
|
||||
let bind_group_layouts = derived_group_layouts
|
||||
.into_iter()
|
||||
.map(|mut bgl_entry_map| {
|
||||
if bgl_entry_map.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
bgl_entry_map.sort();
|
||||
match unique_bind_group_layouts.entry(bgl_entry_map) {
|
||||
hashbrown::hash_map::Entry::Occupied(v) => Ok(Arc::clone(v.get())),
|
||||
hashbrown::hash_map::Entry::Occupied(v) => Ok(Some(Arc::clone(v.get()))),
|
||||
hashbrown::hash_map::Entry::Vacant(e) => {
|
||||
match self.create_bind_group_layout_internal(
|
||||
&None,
|
||||
@@ -3713,7 +3759,7 @@ impl Device {
|
||||
) {
|
||||
Ok(bgl) => {
|
||||
e.insert(bgl.clone());
|
||||
Ok(bgl)
|
||||
Ok(Some(bgl))
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
@@ -3728,7 +3774,7 @@ impl Device {
|
||||
immediate_size: 0, //TODO?
|
||||
};
|
||||
|
||||
let layout = self.create_pipeline_layout(&layout_desc)?;
|
||||
let layout = self.create_pipeline_layout_impl(&layout_desc, true)?;
|
||||
Ok(layout)
|
||||
}
|
||||
|
||||
@@ -3756,9 +3802,7 @@ impl Device {
|
||||
};
|
||||
|
||||
let mut binding_layout_source = match pipeline_layout {
|
||||
Some(ref pipeline_layout) => {
|
||||
validation::BindingLayoutSource::Provided(pipeline_layout.get_binding_maps())
|
||||
}
|
||||
Some(pipeline_layout) => validation::BindingLayoutSource::Provided(pipeline_layout),
|
||||
None => validation::BindingLayoutSource::new_derived(&self.limits),
|
||||
};
|
||||
let mut shader_binding_sizes = FastHashMap::default();
|
||||
@@ -3786,12 +3830,9 @@ impl Device {
|
||||
}
|
||||
|
||||
let pipeline_layout = match binding_layout_source {
|
||||
validation::BindingLayoutSource::Provided(_) => {
|
||||
drop(binding_layout_source);
|
||||
pipeline_layout.unwrap()
|
||||
}
|
||||
validation::BindingLayoutSource::Provided(pipeline_layout) => pipeline_layout,
|
||||
validation::BindingLayoutSource::Derived(entries) => {
|
||||
self.derive_pipeline_layout(entries)?
|
||||
self.create_derived_pipeline_layout(entries)?
|
||||
}
|
||||
};
|
||||
|
||||
@@ -3852,6 +3893,10 @@ impl Device {
|
||||
|
||||
if is_auto_layout {
|
||||
for bgl in pipeline.layout.bind_group_layouts.iter() {
|
||||
let Some(bgl) = bgl else {
|
||||
continue;
|
||||
};
|
||||
|
||||
// `bind_group_layouts` might contain duplicate entries, so we need to ignore the result.
|
||||
let _ = bgl
|
||||
.exclusive_pipeline
|
||||
@@ -4268,9 +4313,7 @@ impl Device {
|
||||
};
|
||||
|
||||
let mut binding_layout_source = match pipeline_layout {
|
||||
Some(ref pipeline_layout) => {
|
||||
validation::BindingLayoutSource::Provided(pipeline_layout.get_binding_maps())
|
||||
}
|
||||
Some(pipeline_layout) => validation::BindingLayoutSource::Provided(pipeline_layout),
|
||||
None => validation::BindingLayoutSource::new_derived(&self.limits),
|
||||
};
|
||||
|
||||
@@ -4510,12 +4553,9 @@ impl Device {
|
||||
}
|
||||
|
||||
let pipeline_layout = match binding_layout_source {
|
||||
validation::BindingLayoutSource::Provided(_) => {
|
||||
drop(binding_layout_source);
|
||||
pipeline_layout.unwrap()
|
||||
}
|
||||
validation::BindingLayoutSource::Provided(pipeline_layout) => pipeline_layout,
|
||||
validation::BindingLayoutSource::Derived(entries) => {
|
||||
self.derive_pipeline_layout(entries)?
|
||||
self.create_derived_pipeline_layout(entries)?
|
||||
}
|
||||
};
|
||||
|
||||
@@ -4667,6 +4707,10 @@ impl Device {
|
||||
|
||||
if is_auto_layout {
|
||||
for bgl in pipeline.layout.bind_group_layouts.iter() {
|
||||
let Some(bgl) = bgl else {
|
||||
continue;
|
||||
};
|
||||
|
||||
// `bind_group_layouts` might contain duplicate entries, so we need to ignore the result.
|
||||
let _ = bgl
|
||||
.exclusive_pipeline
|
||||
|
||||
Reference in new issue
Block a user