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