Skip to content

Commit

Permalink
use ManuallyDrop for Destroyed{Buffer,Texture}
Browse files Browse the repository at this point in the history
  • Loading branch information
teoxoy committed Jul 29, 2024
1 parent 650054b commit 76f9b2f
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions wgpu-core/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ impl<A: HalApi> Buffer<A> {
};

queue::TempResource::DestroyedBuffer(DestroyedBuffer {
raw: Some(raw),
raw: ManuallyDrop::new(raw),
device: Arc::clone(&self.device),
label: self.label().to_owned(),
bind_groups,
Expand Down Expand Up @@ -761,7 +761,7 @@ crate::impl_trackable!(Buffer);
/// A buffer that has been marked as destroyed and is staged for actual deletion soon.
#[derive(Debug)]
pub struct DestroyedBuffer<A: HalApi> {
raw: Option<A::Buffer>,
raw: ManuallyDrop<A::Buffer>,
device: Arc<Device<A>>,
label: String,
bind_groups: Vec<Weak<BindGroup<A>>>,
Expand All @@ -781,13 +781,12 @@ impl<A: HalApi> Drop for DestroyedBuffer<A> {
}
drop(deferred);

if let Some(raw) = self.raw.take() {
resource_log!("Destroy raw Buffer (destroyed) {:?}", self.label());

unsafe {
use hal::Device;
self.device.raw().destroy_buffer(raw);
}
resource_log!("Destroy raw Buffer (destroyed) {:?}", self.label());
// 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_buffer(raw);
}
}
}
Expand Down Expand Up @@ -1174,7 +1173,7 @@ impl<A: HalApi> Texture<A> {
};

queue::TempResource::DestroyedTexture(DestroyedTexture {
raw: Some(raw),
raw: ManuallyDrop::new(raw),
views,
bind_groups,
device: Arc::clone(&self.device),
Expand Down Expand Up @@ -1363,7 +1362,7 @@ impl Global {
/// A texture that has been marked as destroyed and is staged for actual deletion soon.
#[derive(Debug)]
pub struct DestroyedTexture<A: HalApi> {
raw: Option<A::Texture>,
raw: ManuallyDrop<A::Texture>,
views: Vec<Weak<TextureView<A>>>,
bind_groups: Vec<Weak<BindGroup<A>>>,
device: Arc<Device<A>>,
Expand All @@ -1389,13 +1388,12 @@ impl<A: HalApi> Drop for DestroyedTexture<A> {
}
drop(deferred);

if let Some(raw) = self.raw.take() {
resource_log!("Destroy raw Texture (destroyed) {:?}", self.label());

unsafe {
use hal::Device;
self.device.raw().destroy_texture(raw);
}
resource_log!("Destroy raw Texture (destroyed) {:?}", self.label());
// 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_texture(raw);
}
}
}
Expand Down

0 comments on commit 76f9b2f

Please sign in to comment.