From 46ef1fc0615a7ad0f9a11c698f2a9e2d47b6ed64 Mon Sep 17 00:00:00 2001 From: Connor Fitzgerald Date: Thu, 3 Apr 2025 18:39:49 -0400 Subject: [PATCH] [d3d12] Use `DeviceAllocationContext` for deallocation --- wgpu-hal/src/dx12/device.rs | 13 ++++--------- wgpu-hal/src/dx12/suballocation.rs | 11 +++++------ 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index 9f7044947..1cd108b6c 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -451,10 +451,9 @@ impl crate::Device for super::Device { unsafe fn destroy_buffer(&self, buffer: super::Buffer) { suballocation::free_resource( - self, + suballocation::DeviceAllocationContext::from(self), buffer.resource, buffer.allocation, - &self.mem_allocator, ); self.counters.buffers.sub(1); @@ -542,11 +541,9 @@ impl crate::Device for super::Device { unsafe fn destroy_texture(&self, texture: super::Texture) { suballocation::free_resource( - self, + suballocation::DeviceAllocationContext::from(self), texture.resource, texture.allocation, - // SAFETY: for allocations to exist, the allocator must exist - &self.mem_allocator, ); self.counters.textures.sub(1); @@ -1673,10 +1670,9 @@ impl crate::Device for super::Device { if let Some(sampler_buffer) = group.sampler_index_buffer { suballocation::free_resource( - self, + suballocation::DeviceAllocationContext::from(self), sampler_buffer.buffer, sampler_buffer.allocation, - &self.mem_allocator, ); } @@ -2320,10 +2316,9 @@ impl crate::Device for super::Device { acceleration_structure: super::AccelerationStructure, ) { suballocation::free_resource( - self, + suballocation::DeviceAllocationContext::from(self), acceleration_structure.resource, acceleration_structure.allocation, - &self.mem_allocator, ); } diff --git a/wgpu-hal/src/dx12/suballocation.rs b/wgpu-hal/src/dx12/suballocation.rs index 92ce95020..d045820ca 100644 --- a/wgpu-hal/src/dx12/suballocation.rs +++ b/wgpu-hal/src/dx12/suballocation.rs @@ -258,10 +258,9 @@ pub(crate) fn create_acceleration_structure( } pub(crate) fn free_resource( - device: &crate::dx12::Device, + ctx: DeviceAllocationContext, resource: Direct3D12::ID3D12Resource, allocation: Allocation, - allocator: &Mutex, ) { // Make sure the resource is released before we free the allocation. drop(resource); @@ -271,13 +270,13 @@ pub(crate) fn free_resource( }; let counter = match allocation.ty { - AllocationType::Buffer => &device.counters.buffer_memory, - AllocationType::Texture => &device.counters.texture_memory, - AllocationType::AccelerationStructure => &device.counters.acceleration_structure_memory, + AllocationType::Buffer => &ctx.counters.buffer_memory, + AllocationType::Texture => &ctx.counters.texture_memory, + AllocationType::AccelerationStructure => &ctx.counters.acceleration_structure_memory, }; counter.sub(inner.size() as isize); - match allocator.lock().free(inner) { + match ctx.mem_allocator.lock().free(inner) { Ok(_) => (), // TODO: Don't panic here Err(e) => panic!("Failed to destroy dx12 {:?}, {e}", allocation.ty),