Skip to content

Commit

Permalink
chore: separate new unsafe ops into blocks
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ErichDonGubler committed Sep 21, 2022
1 parent 1f984c3 commit 0c2265d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
9 changes: 3 additions & 6 deletions wgpu-core/src/track/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,12 +420,9 @@ impl<A: hub::HalApi> 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 } => {
Expand Down
18 changes: 8 additions & 10 deletions wgpu-core/src/track/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,10 @@ impl<A: hub::HalApi> TextureUsageScope<A> {
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,
Expand Down Expand Up @@ -359,9 +360,10 @@ impl<A: hub::HalApi> TextureUsageScope<A> {

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,
Expand Down Expand Up @@ -467,13 +469,8 @@ impl<A: hub::HalApi> TextureTracker<A> {

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.
Expand Down Expand Up @@ -683,9 +680,10 @@ impl<A: hub::HalApi> TextureTracker<A> {
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,
Expand Down
5 changes: 4 additions & 1 deletion wgpu-hal/src/vulkan/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
};
}
Expand Down

0 comments on commit 0c2265d

Please sign in to comment.