From 0c2265d6359d5b1cc744f0603509fcceafd208df Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Mon, 19 Sep 2022 10:14:32 -0400 Subject: [PATCH] chore: separate new `unsafe` ops into blocks Unsafe operations can be exhausting to validate by themselves. Therefore, it's often interesting to separate these out so we can justify each individual operation, and let a human reader accumulate and drop supporting safety context in the smallest increments necessary. To that end, this commit can pave the way for future work where we may do something like enable [`clippy::undocumented_unsafe_blocks`], which calls out `unsafe` blocks that do not have a `SAFETY` comment immediately above them. This commit only separates the operations in `unsafe` blocks I added in this same PR; I'm deliberately leaving existing `unsafe` blocks alone, ATM. [`clippy::undocumented_unsafe_blocks`]: https://rust-lang.github.io/rust-clippy/stable/index.html#undocumented_unsafe_blocks --- wgpu-core/src/track/mod.rs | 9 +++------ wgpu-core/src/track/texture.rs | 18 ++++++++---------- wgpu-hal/src/vulkan/device.rs | 5 ++++- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/wgpu-core/src/track/mod.rs b/wgpu-core/src/track/mod.rs index bf37b256184..86133bd2b33 100644 --- a/wgpu-core/src/track/mod.rs +++ b/wgpu-core/src/track/mod.rs @@ -420,12 +420,9 @@ impl ResourceMetadataProvider<'_, A> { } ResourceMetadataProvider::Indirect { metadata } => { metadata.tracker_assert_in_bounds(index); - (unsafe { *metadata.epochs.get_unchecked(index) }, unsafe { - metadata - .ref_counts - .get_unchecked(index) - .clone() - .unwrap_unchecked() + (unsafe { *metadata.epochs.get_unchecked(index) }, { + let ref_count = unsafe { metadata.ref_counts.get_unchecked(index) }; + unsafe { ref_count.clone().unwrap_unchecked() } }) } ResourceMetadataProvider::Resource { epoch } => { diff --git a/wgpu-core/src/track/texture.rs b/wgpu-core/src/track/texture.rs index 2e06b1dedec..125eb268434 100644 --- a/wgpu-core/src/track/texture.rs +++ b/wgpu-core/src/track/texture.rs @@ -291,9 +291,10 @@ impl TextureUsageScope { self.tracker_assert_in_bounds(index); scope.tracker_assert_in_bounds(index); + let texture_data = unsafe { texture_data_from_texture(storage, index32) }; unsafe { insert_or_merge( - texture_data_from_texture(storage, index32), + texture_data, &mut self.set, &mut self.metadata, index32, @@ -359,9 +360,10 @@ impl TextureUsageScope { self.tracker_assert_in_bounds(index); + let texture_data = unsafe { texture_data_from_texture(storage, index32) }; unsafe { insert_or_merge( - texture_data_from_texture(storage, index32), + texture_data, &mut self.set, &mut self.metadata, index32, @@ -467,13 +469,8 @@ impl TextureTracker { self.tracker_assert_in_bounds(index); - unsafe { - self.metadata - .ref_counts - .get_unchecked(index) - .as_ref() - .unwrap_unchecked() - } + let ref_count = unsafe { self.metadata.ref_counts.get_unchecked(index) }; + unsafe { ref_count.as_ref().unwrap_unchecked() } } /// Inserts a single texture and a state into the resource tracker. @@ -683,9 +680,10 @@ impl TextureTracker { if unsafe { !scope.metadata.owned.get(index).unwrap_unchecked() } { continue; } + let texture_data = unsafe { texture_data_from_texture(storage, index32) }; unsafe { insert_or_barrier_update( - texture_data_from_texture(storage, index32), + texture_data, Some(&mut self.start_set), &mut self.end_set, &mut self.metadata, diff --git a/wgpu-hal/src/vulkan/device.rs b/wgpu-hal/src/vulkan/device.rs index af22f8e0391..4de2b06a57b 100644 --- a/wgpu-hal/src/vulkan/device.rs +++ b/wgpu-hal/src/vulkan/device.rs @@ -48,13 +48,16 @@ impl super::DeviceShared { .collect(); &buffer_vec }; + + let name = unsafe { CStr::from_bytes_with_nul_unchecked(name_bytes) }; + let _result = unsafe { extension.debug_utils_set_object_name( self.raw.handle(), &vk::DebugUtilsObjectNameInfoEXT::builder() .object_type(object_type) .object_handle(object.as_raw()) - .object_name(CStr::from_bytes_with_nul_unchecked(name_bytes)), + .object_name(name), ) }; }