From c1bc0864c58e794b68d58305fb828da9ebf0f0cd Mon Sep 17 00:00:00 2001 From: teoxoy <28601907+teoxoy@users.noreply.github.com> Date: Wed, 7 Aug 2024 14:24:52 +0200 Subject: [PATCH] use `ManuallyDrop` for remaining resources --- wgpu-core/src/binding_model.rs | 33 +++++++-------- wgpu-core/src/device/global.rs | 26 ++++++------ wgpu-core/src/device/resource.rs | 33 ++++++++------- wgpu-core/src/pipeline.rs | 70 +++++++++++++++++--------------- wgpu-core/src/resource.rs | 32 +++++++-------- 5 files changed, 101 insertions(+), 93 deletions(-) diff --git a/wgpu-core/src/binding_model.rs b/wgpu-core/src/binding_model.rs index 08d9cda56..0687e6e0f 100644 --- a/wgpu-core/src/binding_model.rs +++ b/wgpu-core/src/binding_model.rs @@ -26,6 +26,7 @@ use serde::Serialize; use std::{ borrow::Cow, + mem::ManuallyDrop, ops::Range, sync::{Arc, Weak}, }; @@ -498,7 +499,7 @@ impl std::fmt::Display for ExclusivePipeline { /// Bind group layout. #[derive(Debug)] pub struct BindGroupLayout { - pub(crate) raw: Option, + pub(crate) raw: ManuallyDrop, pub(crate) device: Arc>, pub(crate) entries: bgl::EntryMap, /// It is very important that we know if the bind group comes from the BGL pool. @@ -517,15 +518,15 @@ pub struct BindGroupLayout { impl Drop for BindGroupLayout { fn drop(&mut self) { + resource_log!("Destroy raw {}", self.error_ident()); if matches!(self.origin, bgl::Origin::Pool) { self.device.bgl_pool.remove(&self.entries); } - if let Some(raw) = self.raw.take() { - resource_log!("Destroy raw {}", self.error_ident()); - unsafe { - use hal::Device; - self.device.raw().destroy_bind_group_layout(raw); - } + // 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 { + use hal::Device; + self.device.raw().destroy_bind_group_layout(raw); } } } @@ -537,7 +538,7 @@ crate::impl_storage_item!(BindGroupLayout); impl BindGroupLayout { pub(crate) fn raw(&self) -> &A::BindGroupLayout { - self.raw.as_ref().unwrap() + &self.raw } } @@ -651,7 +652,7 @@ pub struct ResolvedPipelineLayoutDescriptor<'a, A: HalApi> { #[derive(Debug)] pub struct PipelineLayout { - pub(crate) raw: Option, + pub(crate) raw: ManuallyDrop, pub(crate) device: Arc>, /// The `label` from the descriptor used to create the resource. pub(crate) label: String, @@ -661,19 +662,19 @@ pub struct PipelineLayout { impl Drop for PipelineLayout { fn drop(&mut self) { - if let Some(raw) = self.raw.take() { - resource_log!("Destroy raw {}", self.error_ident()); - unsafe { - use hal::Device; - self.device.raw().destroy_pipeline_layout(raw); - } + 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 { + use hal::Device; + self.device.raw().destroy_pipeline_layout(raw); } } } impl PipelineLayout { pub(crate) fn raw(&self) -> &A::PipelineLayout { - self.raw.as_ref().unwrap() + &self.raw } pub(crate) fn get_binding_maps(&self) -> ArrayVec<&bgl::EntryMap, { hal::MAX_BIND_GROUPS }> { diff --git a/wgpu-core/src/device/global.rs b/wgpu-core/src/device/global.rs index 1f70ee09e..7fd82e8ce 100644 --- a/wgpu-core/src/device/global.rs +++ b/wgpu-core/src/device/global.rs @@ -2191,23 +2191,21 @@ impl Global { if !cache.device.is_valid() { return None; } - if let Some(raw_cache) = cache.raw.as_ref() { - let mut vec = unsafe { cache.device.raw().pipeline_cache_get_data(raw_cache) }?; - let validation_key = cache.device.raw().pipeline_cache_validation_key()?; + let mut vec = unsafe { cache.device.raw().pipeline_cache_get_data(cache.raw()) }?; + let validation_key = cache.device.raw().pipeline_cache_validation_key()?; - let mut header_contents = [0; pipeline_cache::HEADER_LENGTH]; - pipeline_cache::add_cache_header( - &mut header_contents, - &vec, - &cache.device.adapter.raw.info, - validation_key, - ); + let mut header_contents = [0; pipeline_cache::HEADER_LENGTH]; + pipeline_cache::add_cache_header( + &mut header_contents, + &vec, + &cache.device.adapter.raw.info, + validation_key, + ); - let deleted = vec.splice(..0, header_contents).collect::>(); - debug_assert!(deleted.is_empty()); + let deleted = vec.splice(..0, header_contents).collect::>(); + debug_assert!(deleted.is_empty()); - return Some(vec); - } + return Some(vec); } None } diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 4a7582fd6..63c09831d 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -80,7 +80,7 @@ use super::{ /// When locking pending_writes please check that trackers is not locked /// trackers should be locked only when needed for the shortest time possible pub struct Device { - raw: Option, + raw: ManuallyDrop, pub(crate) adapter: Arc>, pub(crate) queue: OnceCell>>, queue_to_drop: OnceCell, @@ -169,7 +169,8 @@ impl std::fmt::Debug for Device { impl Drop for Device { fn drop(&mut self) { resource_log!("Drop {}", self.error_ident()); - let raw = self.raw.take().unwrap(); + // 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) }; // SAFETY: We are in the Drop impl and we don't use self.pending_writes anymore after this point. let pending_writes = unsafe { ManuallyDrop::take(&mut self.pending_writes.lock()) }; pending_writes.dispose(&raw); @@ -193,7 +194,7 @@ pub enum CreateDeviceError { impl Device { pub(crate) fn raw(&self) -> &A::Device { - self.raw.as_ref().unwrap() + &self.raw } pub(crate) fn require_features(&self, feature: wgt::Features) -> Result<(), MissingFeatures> { if self.features.contains(feature) { @@ -271,7 +272,7 @@ impl Device { let downlevel = adapter.raw.capabilities.downlevel.clone(); Ok(Self { - raw: Some(raw_device), + raw: ManuallyDrop::new(raw_device), adapter: adapter.clone(), queue: OnceCell::new(), queue_to_drop: OnceCell::new(), @@ -1418,7 +1419,7 @@ impl Device { }; let sampler = Sampler { - raw: Some(raw), + raw: ManuallyDrop::new(raw), device: self.clone(), label: desc.label.to_string(), tracking_data: TrackingData::new(self.tracker_indices.samplers.clone()), @@ -1550,7 +1551,7 @@ impl Device { }; let module = pipeline::ShaderModule { - raw: Some(raw), + raw: ManuallyDrop::new(raw), device: self.clone(), interface: Some(interface), label: desc.label.to_string(), @@ -1591,7 +1592,7 @@ impl Device { }; let module = pipeline::ShaderModule { - raw: Some(raw), + raw: ManuallyDrop::new(raw), device: self.clone(), interface: None, label: desc.label.to_string(), @@ -1861,7 +1862,7 @@ impl Device { .map_err(binding_model::CreateBindGroupLayoutError::TooManyBindings)?; let bgl = BindGroupLayout { - raw: Some(raw), + raw: ManuallyDrop::new(raw), device: self.clone(), entries: entry_map, origin, @@ -2576,7 +2577,7 @@ impl Device { drop(raw_bind_group_layouts); let layout = binding_model::PipelineLayout { - raw: Some(raw), + raw: ManuallyDrop::new(raw), device: self.clone(), label: desc.label.to_string(), bind_group_layouts, @@ -2718,7 +2719,7 @@ impl Device { constants: desc.stage.constants.as_ref(), zero_initialize_workgroup_memory: desc.stage.zero_initialize_workgroup_memory, }, - cache: cache.as_ref().and_then(|it| it.raw.as_ref()), + cache: cache.as_ref().map(|it| it.raw()), }; let raw = @@ -2742,7 +2743,7 @@ impl Device { )?; let pipeline = pipeline::ComputePipeline { - raw: Some(raw), + raw: ManuallyDrop::new(raw), layout: pipeline_layout, device: self.clone(), _shader_module: shader_module, @@ -3299,7 +3300,7 @@ impl Device { fragment_stage, color_targets, multiview: desc.multiview, - cache: cache.as_ref().and_then(|it| it.raw.as_ref()), + cache: cache.as_ref().map(|it| it.raw()), }; let raw = unsafe { self.raw().create_render_pipeline(&pipeline_desc) }.map_err( @@ -3363,7 +3364,7 @@ impl Device { }; let pipeline = pipeline::RenderPipeline { - raw: Some(raw), + raw: ManuallyDrop::new(raw), layout: pipeline_layout, device: self.clone(), pass_context, @@ -3434,7 +3435,7 @@ impl Device { device: self.clone(), label: desc.label.to_string(), // This would be none in the error condition, which we don't implement yet - raw: Some(raw), + raw: ManuallyDrop::new(raw), }; let cache = Arc::new(cache); @@ -3535,8 +3536,10 @@ impl Device { let hal_desc = desc.map_label(|label| label.to_hal(self.instance_flags)); + let raw = unsafe { self.raw().create_query_set(&hal_desc).unwrap() }; + let query_set = QuerySet { - raw: Some(unsafe { self.raw().create_query_set(&hal_desc).unwrap() }), + raw: ManuallyDrop::new(raw), device: self.clone(), label: desc.label.to_string(), tracking_data: TrackingData::new(self.tracker_indices.query_sets.clone()), diff --git a/wgpu-core/src/pipeline.rs b/wgpu-core/src/pipeline.rs index 68e92ca4b..59226051e 100644 --- a/wgpu-core/src/pipeline.rs +++ b/wgpu-core/src/pipeline.rs @@ -10,7 +10,7 @@ use crate::{ }; use arrayvec::ArrayVec; use naga::error::ShaderError; -use std::{borrow::Cow, marker::PhantomData, num::NonZeroU32, sync::Arc}; +use std::{borrow::Cow, marker::PhantomData, mem::ManuallyDrop, num::NonZeroU32, sync::Arc}; use thiserror::Error; /// Information about buffer bindings, which @@ -47,7 +47,7 @@ pub struct ShaderModuleDescriptor<'a> { #[derive(Debug)] pub struct ShaderModule { - pub(crate) raw: Option, + pub(crate) raw: ManuallyDrop, pub(crate) device: Arc>, pub(crate) interface: Option, /// The `label` from the descriptor used to create the resource. @@ -56,12 +56,12 @@ pub struct ShaderModule { impl Drop for ShaderModule { fn drop(&mut self) { - if let Some(raw) = self.raw.take() { - resource_log!("Destroy raw {}", self.error_ident()); - unsafe { - use hal::Device; - self.device.raw().destroy_shader_module(raw); - } + 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 { + use hal::Device; + self.device.raw().destroy_shader_module(raw); } } } @@ -73,7 +73,7 @@ crate::impl_storage_item!(ShaderModule); impl ShaderModule { pub(crate) fn raw(&self) -> &A::ShaderModule { - self.raw.as_ref().unwrap() + &self.raw } pub(crate) fn finalize_entry_point_name( @@ -242,7 +242,7 @@ pub enum CreateComputePipelineError { #[derive(Debug)] pub struct ComputePipeline { - pub(crate) raw: Option, + pub(crate) raw: ManuallyDrop, pub(crate) layout: Arc>, pub(crate) device: Arc>, pub(crate) _shader_module: Arc>, @@ -254,12 +254,12 @@ pub struct ComputePipeline { impl Drop for ComputePipeline { fn drop(&mut self) { - if let Some(raw) = self.raw.take() { - resource_log!("Destroy raw {}", self.error_ident()); - unsafe { - use hal::Device; - self.device.raw().destroy_compute_pipeline(raw); - } + 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 { + use hal::Device; + self.device.raw().destroy_compute_pipeline(raw); } } } @@ -272,7 +272,7 @@ crate::impl_trackable!(ComputePipeline); impl ComputePipeline { pub(crate) fn raw(&self) -> &A::ComputePipeline { - self.raw.as_ref().unwrap() + &self.raw } } @@ -301,7 +301,7 @@ impl From for CreatePipelineCacheError { #[derive(Debug)] pub struct PipelineCache { - pub(crate) raw: Option, + pub(crate) raw: ManuallyDrop, pub(crate) device: Arc>, /// The `label` from the descriptor used to create the resource. pub(crate) label: String, @@ -309,12 +309,12 @@ pub struct PipelineCache { impl Drop for PipelineCache { fn drop(&mut self) { - if let Some(raw) = self.raw.take() { - resource_log!("Destroy raw {}", self.error_ident()); - unsafe { - use hal::Device; - self.device.raw().destroy_pipeline_cache(raw); - } + 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 { + use hal::Device; + self.device.raw().destroy_pipeline_cache(raw); } } } @@ -324,6 +324,12 @@ crate::impl_labeled!(PipelineCache); crate::impl_parent_device!(PipelineCache); crate::impl_storage_item!(PipelineCache); +impl PipelineCache { + pub(crate) fn raw(&self) -> &A::PipelineCache { + &self.raw + } +} + /// Describes how the vertex buffer is interpreted. #[derive(Clone, Debug)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] @@ -586,7 +592,7 @@ impl Default for VertexStep { #[derive(Debug)] pub struct RenderPipeline { - pub(crate) raw: Option, + pub(crate) raw: ManuallyDrop, pub(crate) device: Arc>, pub(crate) layout: Arc>, pub(crate) _shader_modules: @@ -603,12 +609,12 @@ pub struct RenderPipeline { impl Drop for RenderPipeline { fn drop(&mut self) { - if let Some(raw) = self.raw.take() { - resource_log!("Destroy raw {}", self.error_ident()); - unsafe { - use hal::Device; - self.device.raw().destroy_render_pipeline(raw); - } + 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 { + use hal::Device; + self.device.raw().destroy_render_pipeline(raw); } } } @@ -621,6 +627,6 @@ crate::impl_trackable!(RenderPipeline); impl RenderPipeline { pub(crate) fn raw(&self) -> &A::RenderPipeline { - self.raw.as_ref().unwrap() + &self.raw } } diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index b9d35a601..f125fdfb3 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -1697,7 +1697,7 @@ pub struct SamplerDescriptor<'a> { #[derive(Debug)] pub struct Sampler { - pub(crate) raw: Option, + pub(crate) raw: ManuallyDrop, pub(crate) device: Arc>, /// The `label` from the descriptor used to create the resource. pub(crate) label: String, @@ -1710,19 +1710,19 @@ pub struct Sampler { impl Drop for Sampler { fn drop(&mut self) { - if let Some(raw) = self.raw.take() { - resource_log!("Destroy raw {}", self.error_ident()); - unsafe { - use hal::Device; - self.device.raw().destroy_sampler(raw); - } + 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 { + use hal::Device; + self.device.raw().destroy_sampler(raw); } } } impl Sampler { pub(crate) fn raw(&self) -> &A::Sampler { - self.raw.as_ref().unwrap() + &self.raw } } @@ -1793,7 +1793,7 @@ pub type QuerySetDescriptor<'a> = wgt::QuerySetDescriptor>; #[derive(Debug)] pub struct QuerySet { - pub(crate) raw: Option, + pub(crate) raw: ManuallyDrop, pub(crate) device: Arc>, /// The `label` from the descriptor used to create the resource. pub(crate) label: String, @@ -1803,12 +1803,12 @@ pub struct QuerySet { impl Drop for QuerySet { fn drop(&mut self) { - if let Some(raw) = self.raw.take() { - resource_log!("Destroy raw {}", self.error_ident()); - unsafe { - use hal::Device; - self.device.raw().destroy_query_set(raw); - } + 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 { + use hal::Device; + self.device.raw().destroy_query_set(raw); } } } @@ -1821,7 +1821,7 @@ crate::impl_trackable!(QuerySet); impl QuerySet { pub(crate) fn raw(&self) -> &A::QuerySet { - self.raw.as_ref().unwrap() + &self.raw } }