diff --git a/player/src/lib.rs b/player/src/lib.rs index 05c58933d..0f9c8842d 100644 --- a/player/src/lib.rs +++ b/player/src/lib.rs @@ -223,9 +223,7 @@ impl Player { immediate_size: desc.immediate_size, }; - let pipeline_layout = device - .create_pipeline_layout(&resolved_desc) - .expect("create_pipeline_layout error"); + let (pipeline_layout, _error) = device.create_pipeline_layout(&resolved_desc); self.pipeline_layouts.insert(id, pipeline_layout); } Action::DropPipelineLayout(id) => { diff --git a/wgpu-core/src/binding_model.rs b/wgpu-core/src/binding_model.rs index 15aa0f1b8..127d8677c 100644 --- a/wgpu-core/src/binding_model.rs +++ b/wgpu-core/src/binding_model.rs @@ -999,7 +999,7 @@ pub type ResolvedPipelineLayoutDescriptor<'a, BGL = Arc> = #[derive(Debug)] pub struct PipelineLayout { - pub(crate) raw: ManuallyDrop>, + pub(crate) raw: ResourceState>, pub(crate) device: Arc, /// The `label` from the descriptor used to create the resource. pub(crate) label: String, @@ -1010,17 +1010,38 @@ pub struct PipelineLayout { impl Drop for PipelineLayout { fn drop(&mut self) { resource_log!("Destroy raw {}", self.error_ident()); - // SAFETY: We are in the Drop impl and we don't use self.raw anymore after this point. - let raw = unsafe { ManuallyDrop::take(&mut self.raw) }; - unsafe { - self.device.raw().destroy_pipeline_layout(raw); + if let ResourceState::Valid(raw) = core::mem::replace(&mut self.raw, ResourceState::Invalid) + { + unsafe { + self.device.raw().destroy_pipeline_layout(raw); + } + } + #[cfg(feature = "trace")] + { + if let Some(t) = self.device.trace.lock().as_mut() { + t.add(crate::device::trace::Action::DropPipelineLayout(unsafe { + crate::device::trace::to_trace(self) + })); + } } } } impl PipelineLayout { - pub(crate) fn raw(&self) -> &dyn hal::DynPipelineLayout { - self.raw.as_ref() + pub(crate) fn raw(&self) -> Result<&dyn hal::DynPipelineLayout, InvalidResourceError> { + self.raw + .as_ref() + .valid() + .map(|r| r.as_ref()) + .ok_or_else(|| InvalidResourceError(self.error_ident())) + } + + pub(crate) fn check_valid(&self) -> Result<(), InvalidResourceError> { + self.raw + .as_ref() + .valid() + .map(|_| ()) + .ok_or_else(|| InvalidResourceError(self.error_ident())) } pub(crate) fn get_bind_group_layout( @@ -1082,6 +1103,16 @@ impl PipelineLayout { Ok(()) } + + pub(crate) fn invalid(device: Arc, label: String) -> Arc { + Arc::new(Self { + raw: ResourceState::Invalid, + device, + label, + bind_group_layouts: ArrayVec::new(), + immediate_size: 0, + }) + } } crate::impl_resource_type!(PipelineLayout); diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index d422ab3a1..cf2ffcfcb 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -1250,7 +1250,11 @@ impl RenderBundle { let raw_bg = bind_group.as_ref().unwrap().try_raw(snatch_guard)?; unsafe { raw.set_bind_group( - pipeline_layout.as_ref().unwrap().raw(), + pipeline_layout + .as_ref() + .unwrap() + .raw() + .expect("PipelineLayout should be valid at this point"), *index, raw_bg, &offsets[..*num_dynamic_offsets], @@ -1311,7 +1315,15 @@ impl RenderBundle { let data_slice = &self.base.immediates_data[(values_offset as usize)..values_end_offset]; - unsafe { raw.set_immediates(pipeline_layout.raw(), *offset, data_slice) } + unsafe { + raw.set_immediates( + pipeline_layout + .raw() + .expect("PipelineLayout should be valid at this point"), + *offset, + data_slice, + ) + } } else { super::immediates_clear( *offset, @@ -1319,7 +1331,9 @@ impl RenderBundle { |clear_offset, clear_data| { unsafe { raw.set_immediates( - pipeline_layout.raw(), + pipeline_layout + .raw() + .expect("PipelineLayout should be valid at this point"), clear_offset, clear_data, ) diff --git a/wgpu-core/src/command/compute.rs b/wgpu-core/src/command/compute.rs index d4099debf..20d3b17b5 100644 --- a/wgpu-core/src/command/compute.rs +++ b/wgpu-core/src/command/compute.rs @@ -1106,7 +1106,7 @@ fn dispatch_workgroups_indirect( if !state.immediates.is_empty() { unsafe { state.pass.base.raw_encoder.set_immediates( - pipeline_layout.raw(), + pipeline_layout.raw()?, 0, &state.immediates, ); @@ -1117,7 +1117,7 @@ fn dispatch_workgroups_indirect( let raw_bg = group.try_raw(state.pass.base.snatch_guard)?; unsafe { state.pass.base.raw_encoder.set_bind_group( - pipeline_layout.raw(), + pipeline_layout.raw()?, i as u32, raw_bg, dynamic_offsets, diff --git a/wgpu-core/src/command/pass.rs b/wgpu-core/src/command/pass.rs index e73f78618..fd90492b1 100644 --- a/wgpu-core/src/command/pass.rs +++ b/wgpu-core/src/command/pass.rs @@ -176,7 +176,9 @@ pub(super) fn flush_bindings_helper(state: &mut PassState) -> Result<(), Destroy let raw_bg = bind_group.try_raw(state.base.snatch_guard)?; unsafe { state.base.raw_encoder.set_bind_group( - pipeline_layout.raw(), + pipeline_layout + .raw() + .expect("Pipeline layout should be valid at this point"), i as u32, raw_bg, dynamic_offsets, @@ -207,7 +209,9 @@ where pipeline_layout.immediate_size, |clear_offset, clear_data| unsafe { state.base.raw_encoder.set_immediates( - pipeline_layout.raw(), + pipeline_layout + .raw() + .expect("Pipeline layout should be valid at this point"), clear_offset, clear_data, ); @@ -288,10 +292,13 @@ where f(data_slice); unsafe { - state - .base - .raw_encoder - .set_immediates(pipeline_layout.raw(), offset, data_slice) + state.base.raw_encoder.set_immediates( + pipeline_layout + .raw() + .expect("Pipeline layout should be valid at this point"), + offset, + data_slice, + ) } Ok(()) } diff --git a/wgpu-core/src/device/global.rs b/wgpu-core/src/device/global.rs index 982ec7c02..ee41f7326 100644 --- a/wgpu-core/src/device/global.rs +++ b/wgpu-core/src/device/global.rs @@ -711,47 +711,25 @@ impl Global { let hub = &self.hub; let fid = hub.pipeline_layouts.prepare(id_in); - let error = 'error: { - let device = self.hub.devices.get(device_id); + let device = self.hub.devices.get(device_id); - if let Err(e) = device.check_is_valid() { - break 'error e.into(); - } - - let bind_group_layouts = { - let bind_group_layouts_guard = hub.bind_group_layouts.read(); - desc.bind_group_layouts - .iter() - .map(|bgl_id| bgl_id.map(|bgl_id| bind_group_layouts_guard.get(bgl_id))) - .collect::>() - }; - - let desc = binding_model::ResolvedPipelineLayoutDescriptor { - label: desc.label.clone(), - bind_group_layouts: Cow::Owned(bind_group_layouts), - immediate_size: desc.immediate_size, - }; - - let layout = match device.create_pipeline_layout(&desc) { - Ok(layout) => layout, - Err(e) => break 'error e, - }; - - #[cfg(feature = "trace")] - if let Some(ref mut trace) = *device.trace.lock() { - trace.add(trace::Action::CreatePipelineLayout( - layout.to_trace(), - desc.to_trace(), - )); - } - - let id = fid.assign(Fallible::Valid(layout)); - api_log!("Device::create_pipeline_layout -> {id:?}"); - return (id, None); + let bind_group_layouts = { + let bind_group_layouts_guard = hub.bind_group_layouts.read(); + desc.bind_group_layouts + .iter() + .map(|bgl_id| bgl_id.map(|bgl_id| bind_group_layouts_guard.get(bgl_id))) + .collect::>() }; - let id = fid.assign(Fallible::Invalid(Arc::new(desc.label.to_string()))); - (id, Some(error)) + let desc = binding_model::ResolvedPipelineLayoutDescriptor { + label: desc.label.clone(), + bind_group_layouts: Cow::Owned(bind_group_layouts), + immediate_size: desc.immediate_size, + }; + + let (layout, error) = device.create_pipeline_layout(&desc); + let id = fid.assign(layout); + (id, error) } pub fn pipeline_layout_drop(&self, pipeline_layout_id: id::PipelineLayoutId) { @@ -761,13 +739,6 @@ impl Global { let hub = &self.hub; let _layout = hub.pipeline_layouts.remove(pipeline_layout_id); - - #[cfg(feature = "trace")] - if let Ok(layout) = _layout.get() { - if let Some(t) = layout.device.trace.lock().as_mut() { - t.add(trace::Action::DropPipelineLayout(layout.to_trace())); - } - } } pub fn device_create_bind_group( @@ -1385,14 +1356,7 @@ impl Global { break 'error e.into(); } - let layout = desc - .layout - .map(|layout| hub.pipeline_layouts.get(layout).get()) - .transpose(); - let layout = match layout { - Ok(layout) => layout, - Err(e) => break 'error e.into(), - }; + let layout = desc.layout.map(|layout| hub.pipeline_layouts.get(layout)); let cache = desc .cache @@ -1614,14 +1578,7 @@ impl Global { break 'error e.into(); } - let layout = desc - .layout - .map(|layout| hub.pipeline_layouts.get(layout).get()) - .transpose(); - let layout = match layout { - Ok(layout) => layout, - Err(e) => break 'error e.into(), - }; + let layout = desc.layout.map(|layout| hub.pipeline_layouts.get(layout)); let cache = desc .cache diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 9778392c1..e577b4ea8 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -3817,8 +3817,30 @@ impl Device { pub fn create_pipeline_layout( self: &Arc, desc: &binding_model::ResolvedPipelineLayoutDescriptor, - ) -> Result, binding_model::CreatePipelineLayoutError> { - self.create_pipeline_layout_impl(desc, false) + ) -> ( + Arc, + Option, + ) { + let (layout, error) = match self.create_pipeline_layout_impl(desc, false) { + Ok(layout) => (layout, None), + Err(e) => ( + binding_model::PipelineLayout::invalid(Arc::clone(self), desc.label.to_string()), + Some(e), + ), + }; + #[cfg(feature = "trace")] + if let Some(ref mut trace) = *self.trace.lock() { + use crate::device::trace::IntoTrace; + trace.add(trace::Action::CreatePipelineLayout( + layout.to_trace(), + desc.to_trace(), + )); + } + api_log!( + "Device::create_pipeline_layout -> {:?}", + Arc::as_ptr(&layout) + ); + (layout, error) } fn create_pipeline_layout_impl( @@ -3918,7 +3940,7 @@ impl Device { drop(raw_bind_group_layouts); let layout = binding_model::PipelineLayout { - raw: ManuallyDrop::new(raw), + raw: ResourceState::Valid(raw), device: self.clone(), label: desc.label.to_string(), bind_group_layouts, @@ -4025,6 +4047,7 @@ impl Device { let pipeline_layout = match desc.layout { Some(pipeline_layout) => { pipeline_layout.same_device(self)?; + pipeline_layout.check_valid()?; Some(pipeline_layout) } None => None, @@ -4083,7 +4106,7 @@ impl Device { let pipeline_desc = hal::ComputePipelineDescriptor { label: desc.label.to_hal(self.instance_flags), - layout: pipeline_layout.raw(), + layout: pipeline_layout.raw()?, stage: hal::ProgrammableStage { module: shader_module.raw(), entry_point: final_entry_point_name.as_ref(), @@ -4580,6 +4603,7 @@ impl Device { let pipeline_layout = match desc.layout { Some(pipeline_layout) => { pipeline_layout.same_device(self)?; + pipeline_layout.check_valid()?; Some(pipeline_layout) } None => None, @@ -4918,7 +4942,7 @@ impl Device { let raw = { let pipeline_desc = hal::RenderPipelineDescriptor { label: desc.label.to_hal(self.instance_flags), - layout: pipeline_layout.raw(), + layout: pipeline_layout.raw()?, vertex_processor: match vertex_stage { Some(vertex_stage) => hal::VertexProcessor::Standard { vertex_buffers: &hal_vertex_buffer_layouts, diff --git a/wgpu-core/src/hub.rs b/wgpu-core/src/hub.rs index 347d08da9..9233191cf 100644 --- a/wgpu-core/src/hub.rs +++ b/wgpu-core/src/hub.rs @@ -190,7 +190,7 @@ pub struct Hub { pub(crate) adapters: Registry>, pub(crate) devices: Registry>, pub(crate) queues: Registry>, - pub(crate) pipeline_layouts: Registry>, + pub(crate) pipeline_layouts: Registry>, pub(crate) shader_modules: Registry>, pub(crate) bind_group_layouts: Registry>, pub(crate) bind_groups: Registry>,