wgpu_core/
scratch.rs

1use crate::device::{Device, DeviceError};
2use crate::resource_log;
3use hal::BufferUses;
4use std::mem::ManuallyDrop;
5use std::sync::Arc;
6
7#[derive(Debug)]
8pub struct ScratchBuffer {
9    raw: ManuallyDrop<Box<dyn hal::DynBuffer>>,
10    device: Arc<Device>,
11}
12
13impl ScratchBuffer {
14    pub(crate) fn new(device: &Arc<Device>, size: wgt::BufferSize) -> Result<Self, DeviceError> {
15        let raw = unsafe {
16            device
17                .raw()
18                .create_buffer(&hal::BufferDescriptor {
19                    label: Some("(wgpu) scratch buffer"),
20                    size: size.get(),
21                    usage: BufferUses::ACCELERATION_STRUCTURE_SCRATCH,
22                    memory_flags: hal::MemoryFlags::empty(),
23                })
24                .map_err(DeviceError::from_hal)?
25        };
26        Ok(Self {
27            raw: ManuallyDrop::new(raw),
28            device: device.clone(),
29        })
30    }
31    pub(crate) fn raw(&self) -> &dyn hal::DynBuffer {
32        self.raw.as_ref()
33    }
34}
35
36impl Drop for ScratchBuffer {
37    fn drop(&mut self) {
38        resource_log!("Destroy raw ScratchBuffer");
39        // SAFETY: We are in the Drop impl and we don't use self.raw anymore after this point.
40        let raw = unsafe { ManuallyDrop::take(&mut self.raw) };
41        unsafe { self.device.raw().destroy_buffer(raw) };
42    }
43}