diff --git a/wgpu-hal/src/dx12/command.rs b/wgpu-hal/src/dx12/command.rs index fc2096ccb..0d83ffece 100644 --- a/wgpu-hal/src/dx12/command.rs +++ b/wgpu-hal/src/dx12/command.rs @@ -481,6 +481,7 @@ impl super::CommandEncoder { resource, allocated_size: size.get(), allocation, + _drop_guard: None, } }; diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index 1448cfc00..34428c595 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -462,6 +462,22 @@ impl super::Device { &self.present_queue } + /// Wraps an existing D3D12 resource as a texture. + /// + /// D3D12 resources are reference counted, so this is not a hand-off of + /// ownership: the reference passed in belongs to the returned texture and + /// is released when wgpu-hal destroys it, while any reference the caller + /// kept stays valid. `drop_callback` is therefore a notification rather + /// than a destructor -- it runs after that release, once wgpu is done with + /// the texture, which is the point at which an importer can let whoever + /// produced the resource reuse it. + /// + /// # Safety + /// + /// - `resource` must be a texture whose dimensions, format, mip level + /// count and sample count are the ones described here. + /// - If `drop_callback` is [`Some`], the callback must be safe to run from + /// whichever thread drops the texture. pub unsafe fn texture_from_raw( resource: Direct3D12::ID3D12Resource, format: wgt::TextureFormat, @@ -469,6 +485,7 @@ impl super::Device { size: wgt::Extent3d, mip_level_count: u32, sample_count: u32, + drop_callback: Option, ) -> super::Texture { super::Texture { resource, @@ -482,12 +499,24 @@ impl super::Device { format.theoretical_memory_footprint(size), ), plane_slice_override: None, + _drop_guard: crate::DropGuard::from_option(drop_callback), } } + /// Wraps an existing D3D12 resource as a buffer. + /// + /// Ownership and `drop_callback` work exactly as they do for + /// [`texture_from_raw`](Self::texture_from_raw). + /// + /// # Safety + /// + /// - `resource` must be a buffer at least `size` bytes long. + /// - If `drop_callback` is [`Some`], the callback must be safe to run from + /// whichever thread drops the buffer. pub unsafe fn buffer_from_raw( resource: Direct3D12::ID3D12Resource, size: wgt::BufferAddress, + drop_callback: Option, ) -> super::Buffer { super::Buffer { resource, @@ -496,6 +525,7 @@ impl super::Device { suballocation::AllocationType::Buffer, size, ), + _drop_guard: crate::DropGuard::from_option(drop_callback), } } } @@ -525,6 +555,7 @@ impl crate::Device for super::Device { resource, allocated_size: desc.size, allocation, + _drop_guard: None, }, desc.size, )) @@ -607,6 +638,7 @@ impl crate::Device for super::Device { sample_count: desc.sample_count, allocation, plane_slice_override: None, + _drop_guard: None, }) } diff --git a/wgpu-hal/src/dx12/mod.rs b/wgpu-hal/src/dx12/mod.rs index 7fe591b73..a56df2eea 100644 --- a/wgpu-hal/src/dx12/mod.rs +++ b/wgpu-hal/src/dx12/mod.rs @@ -926,6 +926,11 @@ pub struct Buffer { // things from the allocated buffer size is usually incorrect. allocated_size: wgt::BufferAddress, allocation: suballocation::Allocation, + + // The `_drop_guard` field must be the last field of this struct so it is + // dropped last, after this buffer's reference to `resource` is released. + // Do not add new fields after it. + _drop_guard: Option, } impl Buffer { @@ -960,6 +965,11 @@ pub struct Texture { /// importers wrapping one plane of a multi-plane DXGI resource as a /// single-plane wgpu texture. plane_slice_override: Option, + + // The `_drop_guard` field must be the last field of this struct so it is + // dropped last, after this texture's reference to `resource` is released. + // Do not add new fields after it. + _drop_guard: Option, } impl Texture { @@ -1647,6 +1657,7 @@ impl crate::Surface for Surface { sc.format.theoretical_memory_footprint(sc.size), ), plane_slice_override: None, + _drop_guard: None, }; Ok(crate::AcquiredSurfaceTexture { texture, diff --git a/wgpu-hal/src/lib.rs b/wgpu-hal/src/lib.rs index 78be382bc..1695eacc3 100644 --- a/wgpu-hal/src/lib.rs +++ b/wgpu-hal/src/lib.rs @@ -338,7 +338,7 @@ pub type FenceValue = u64; pub type AtomicFenceValue = wgpu_sync::atomic::AtomicU64; /// A callback to signal that wgpu is no longer using a resource. -#[cfg(all(any(gles, vulkan, metal), not(webgl)))] +#[cfg(all(any(gles, vulkan, metal, dx12), not(webgl)))] pub type DropCallback = Box; /// A callback to signal that wgpu is no longer using a resource. @@ -349,7 +349,7 @@ pub type DropCallback = Box; #[cfg(webgl)] pub type DropCallback = Box; -#[cfg(any(gles, vulkan, metal))] +#[cfg(any(gles, vulkan, metal, dx12))] pub struct DropGuard { callback: Option, } @@ -363,7 +363,7 @@ unsafe impl Send for DropGuard {} #[cfg(all(webgl, send_sync))] unsafe impl Sync for DropGuard {} -#[cfg(any(gles, vulkan, metal))] +#[cfg(any(gles, vulkan, metal, dx12))] impl DropGuard { #[cfg(any(native, Emscripten))] fn from_option(callback: Option) -> Option { @@ -386,7 +386,7 @@ impl DropGuard { } } -#[cfg(any(gles, vulkan, metal))] +#[cfg(any(gles, vulkan, metal, dx12))] impl Drop for DropGuard { fn drop(&mut self) { if let Some(cb) = self.callback.take() { @@ -395,7 +395,7 @@ impl Drop for DropGuard { } } -#[cfg(any(gles, vulkan, metal))] +#[cfg(any(gles, vulkan, metal, dx12))] impl fmt::Debug for DropGuard { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("DropGuard").finish()