Added drop_callback to dx12 hal types (#10303)

This commit is contained in:
Alex Andreba authored and GitHub committed 2026-09-15 14:43:19 +00:00
1 parent cddb2f5d80
commit 1b3473981b
4 files changed
+49 -5

No files matched your search

+1
View File
@@ -481,6 +481,7 @@ impl super::CommandEncoder {
resource,
allocated_size: size.get(),
allocation,
_drop_guard: None,
}
};
+32
View File
@@ -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<crate::DropCallback>,
) -> 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<crate::DropCallback>,
) -> 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,
})
}
+11
View File
@@ -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<crate::DropGuard>,
}
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<u32>,
// 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<crate::DropGuard>,
}
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,
+5 -5
View File
@@ -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<dyn FnOnce() + Send + Sync + 'static>;
/// A callback to signal that wgpu is no longer using a resource.
@@ -349,7 +349,7 @@ pub type DropCallback = Box<dyn FnOnce() + Send + Sync + 'static>;
#[cfg(webgl)]
pub type DropCallback = Box<dyn FnOnce() + 'static>;
#[cfg(any(gles, vulkan, metal))]
#[cfg(any(gles, vulkan, metal, dx12))]
pub struct DropGuard {
callback: Option<DropCallback>,
}
@@ -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<DropCallback>) -> Option<Self> {
@@ -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()