From 4ef1ee5bcc3162323c48b59e01b4c3fc19e2fb7e Mon Sep 17 00:00:00 2001 From: baldurk Date: Fri, 7 Jun 2024 10:36:23 +0100 Subject: [PATCH] Remove unnecessary and problematic allocator singleton --- .../driver/d3d12/d3d12_command_list4_wrap.cpp | 8 +-- renderdoc/driver/d3d12/d3d12_manager.cpp | 65 +++++++++--------- renderdoc/driver/d3d12/d3d12_manager.h | 67 ++++++------------- 3 files changed, 58 insertions(+), 82 deletions(-) diff --git a/renderdoc/driver/d3d12/d3d12_command_list4_wrap.cpp b/renderdoc/driver/d3d12/d3d12_command_list4_wrap.cpp index d6ea68752..837dcfd18 100644 --- a/renderdoc/driver/d3d12/d3d12_command_list4_wrap.cpp +++ b/renderdoc/driver/d3d12/d3d12_command_list4_wrap.cpp @@ -1016,7 +1016,7 @@ bool WrappedID3D12GraphicsCommandList::Serialise_BuildRaytracingAccelerationStru totalInstancesSize = AlignUp(totalInstancesSize, D3D12_RAYTRACING_INSTANCE_DESCS_BYTE_ALIGNMENT); - if(D3D12GpuBufferAllocator::Inst()->Alloc( + if(GetResourceManager()->GetGPUBufferAllocator().Alloc( D3D12GpuBufferHeapType::DefaultHeapWithUav, D3D12GpuBufferHeapMemoryFlag::Default, totalInstancesSize, D3D12_RAYTRACING_INSTANCE_DESCS_BYTE_ALIGNMENT, &patchInfo.m_patchedInstanceBuffer)) @@ -1297,9 +1297,9 @@ void WrappedID3D12GraphicsCommandList::CopyRaytracingAccelerationStructure( // after the copy operation for simplicity. D3D12GpuBuffer *sizeBuffer = NULL; - D3D12GpuBufferAllocator::Inst()->Alloc(D3D12GpuBufferHeapType::CustomHeapWithUavCpuAccess, - D3D12GpuBufferHeapMemoryFlag::Default, 8, 8, - &sizeBuffer); + GetResourceManager()->GetGPUBufferAllocator().Alloc( + D3D12GpuBufferHeapType::CustomHeapWithUavCpuAccess, D3D12GpuBufferHeapMemoryFlag::Default, + 8, 8, &sizeBuffer); D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_DESC desc = {}; desc.DestBuffer = sizeBuffer->Address(); diff --git a/renderdoc/driver/d3d12/d3d12_manager.cpp b/renderdoc/driver/d3d12/d3d12_manager.cpp index 136650733..c60d8104e 100644 --- a/renderdoc/driver/d3d12/d3d12_manager.cpp +++ b/renderdoc/driver/d3d12/d3d12_manager.cpp @@ -686,14 +686,16 @@ D3D12Descriptor *DescriptorFromPortableHandle(D3D12ResourceManager *manager, Por #define BARRIER_ASSERT(...) #endif -D3D12RaytracingResourceAndUtilHandler::D3D12RaytracingResourceAndUtilHandler(WrappedID3D12Device *device) +D3D12RaytracingResourceAndUtilHandler::D3D12RaytracingResourceAndUtilHandler( + WrappedID3D12Device *device, D3D12GpuBufferAllocator &gpuBufferAllocator) : m_wrappedDevice(device), m_cmdList(NULL), m_cmdAlloc(NULL), m_cmdQueue(NULL), m_gpuFence(NULL), m_gpuSyncHandle(NULL), - m_gpuSyncCounter(0u) + m_gpuSyncCounter(0u), + m_GPUBufferAllocator(gpuBufferAllocator) { } @@ -744,9 +746,8 @@ void D3D12RaytracingResourceAndUtilHandler::CreateInternalResources() m_gpuSyncHandle = ::CreateEvent(NULL, FALSE, FALSE, NULL); } - D3D12GpuBufferAllocator::Inst()->Alloc(D3D12GpuBufferHeapType::CustomHeapWithUavCpuAccess, - D3D12GpuBufferHeapMemoryFlag::Default, 16, 256, - &ASQueryBuffer); + m_GPUBufferAllocator.Alloc(D3D12GpuBufferHeapType::CustomHeapWithUavCpuAccess, + D3D12GpuBufferHeapMemoryFlag::Default, 16, 256, &ASQueryBuffer); } } @@ -780,9 +781,8 @@ void D3D12RaytracingResourceAndUtilHandler::ResizeSerialisationBuffer(UINT64 siz { SAFE_RELEASE(ASSerialiseBuffer); - D3D12GpuBufferAllocator::Inst()->Alloc(D3D12GpuBufferHeapType::DefaultHeapWithUav, - D3D12GpuBufferHeapMemoryFlag::Default, size, 256, - &ASSerialiseBuffer); + m_GPUBufferAllocator.Alloc(D3D12GpuBufferHeapType::DefaultHeapWithUav, + D3D12GpuBufferHeapMemoryFlag::Default, size, 256, &ASSerialiseBuffer); } } @@ -880,9 +880,9 @@ PatchedRayDispatch D3D12RaytracingResourceAndUtilHandler::PatchRayDispatch( ? 1 : uint32_t(desc.CallableShaderTable.SizeInBytes / desc.CallableShaderTable.StrideInBytes); - D3D12GpuBufferAllocator::Inst()->Alloc( - D3D12GpuBufferHeapType::DefaultHeapWithUav, D3D12GpuBufferHeapMemoryFlag::Default, - patchDataSize, D3D12_RAYTRACING_SHADER_TABLE_BYTE_ALIGNMENT, &scratchBuffer); + m_GPUBufferAllocator.Alloc(D3D12GpuBufferHeapType::DefaultHeapWithUav, + D3D12GpuBufferHeapMemoryFlag::Default, patchDataSize, + D3D12_RAYTRACING_SHADER_TABLE_BYTE_ALIGNMENT, &scratchBuffer); ResourceId id; uint64_t offs = 0; @@ -1146,9 +1146,9 @@ void D3D12RaytracingResourceAndUtilHandler::PrepareRayDispatchBuffer( } } - D3D12GpuBufferAllocator::Inst()->Alloc(D3D12GpuBufferHeapType::UploadHeap, - D3D12GpuBufferHeapMemoryFlag::Default, lookupData.size(), - 256, &m_LookupBuffer); + m_GPUBufferAllocator.Alloc(D3D12GpuBufferHeapType::UploadHeap, + D3D12GpuBufferHeapMemoryFlag::Default, lookupData.size(), 256, + &m_LookupBuffer); memcpy(m_LookupBuffer->Map(), lookupData.data(), lookupData.size()); m_LookupBuffer->Unmap(); @@ -1425,8 +1425,6 @@ void D3D12RaytracingResourceAndUtilHandler::UnregisterExportDatabase(D3D12Shader // memory use - next time we need to add data we'll reclaim that. } -D3D12GpuBufferAllocator *D3D12GpuBufferAllocator::m_bufferAllocator = NULL; - bool D3D12GpuBufferAllocator::D3D12GpuBufferResource::CreateCommittedResourceBuffer( ID3D12Device *device, const D3D12_HEAP_PROPERTIES &heapProperty, D3D12_RESOURCE_STATES initState, uint64_t size, bool allowUav, D3D12GpuBufferResource **bufferResource) @@ -1500,6 +1498,7 @@ bool D3D12GpuBufferAllocator::D3D12GpuBufferPool::Alloc(WrappedID3D12Device *wra uint64_t size, uint64_t alignment, D3D12GpuBuffer **gpuBuffer) { + D3D12GpuBufferAllocator &allocator = wrappedDevice->GetResourceManager()->GetGPUBufferAllocator(); if(heapMem == D3D12GpuBufferHeapMemoryFlag::Default) { if(size > m_bufferInitSize) @@ -1512,21 +1511,23 @@ bool D3D12GpuBufferAllocator::D3D12GpuBufferPool::Alloc(WrappedID3D12Device *wra { if(bufferRes->SubAlloc(size, alignment, gpuAddress)) { - *gpuBuffer = new D3D12GpuBuffer(m_bufferPoolHeapType, D3D12GpuBufferHeapMemoryFlag::Default, - size, alignment, gpuAddress, bufferRes->Resource()); + *gpuBuffer = new D3D12GpuBuffer(allocator, m_bufferPoolHeapType, + D3D12GpuBufferHeapMemoryFlag::Default, size, alignment, + gpuAddress, bufferRes->Resource()); return true; } } D3D12GpuBufferResource *newBufferResource = NULL; - if(D3D12GpuBufferAllocator::CreateBufferResource(wrappedDevice, m_bufferPoolHeapType, - m_bufferInitSize, &newBufferResource)) + if(D3D12GpuBufferAllocator::D3D12GpuBufferResource::CreateBufferResource( + wrappedDevice, m_bufferPoolHeapType, m_bufferInitSize, &newBufferResource)) { m_bufferResourceList.push_back(newBufferResource); if(newBufferResource->SubAlloc(size, alignment, gpuAddress)) { - *gpuBuffer = new D3D12GpuBuffer(m_bufferPoolHeapType, D3D12GpuBufferHeapMemoryFlag::Default, - size, alignment, gpuAddress, newBufferResource->Resource()); + *gpuBuffer = new D3D12GpuBuffer(allocator, m_bufferPoolHeapType, + D3D12GpuBufferHeapMemoryFlag::Default, size, alignment, + gpuAddress, newBufferResource->Resource()); return true; } } @@ -1534,14 +1535,14 @@ bool D3D12GpuBufferAllocator::D3D12GpuBufferPool::Alloc(WrappedID3D12Device *wra else { D3D12GpuBufferResource *newBufferResource = NULL; - if(CreateBufferResource(m_bufferAllocator->m_wrappedDevice, m_bufferPoolHeapType, size, - &newBufferResource)) + if(D3D12GpuBufferAllocator::D3D12GpuBufferResource::CreateBufferResource( + wrappedDevice, m_bufferPoolHeapType, size, &newBufferResource)) { m_bufferResourceList.push_back(newBufferResource); - *gpuBuffer = new D3D12GpuBuffer(m_bufferPoolHeapType, D3D12GpuBufferHeapMemoryFlag::Dedicated, - size, D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT, - newBufferResource->Resource()->GetGPUVirtualAddress(), - newBufferResource->Resource()); + *gpuBuffer = new D3D12GpuBuffer( + allocator, m_bufferPoolHeapType, D3D12GpuBufferHeapMemoryFlag::Dedicated, size, + D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT, + newBufferResource->Resource()->GetGPUVirtualAddress(), newBufferResource->Resource()); return true; } } @@ -1633,9 +1634,9 @@ void D3D12GpuBufferAllocator::Release(const D3D12GpuBuffer &gpuBuffer) RDCERR("Couldn't identify buffer heap type %zu", heap); } -bool D3D12GpuBufferAllocator::CreateBufferResource(WrappedID3D12Device *wrappedDevice, - D3D12GpuBufferHeapType heapType, uint64_t size, - D3D12GpuBufferResource **bufferResource) +bool D3D12GpuBufferAllocator::D3D12GpuBufferResource::CreateBufferResource( + WrappedID3D12Device *wrappedDevice, D3D12GpuBufferHeapType heapType, uint64_t size, + D3D12GpuBufferResource **bufferResource) { D3D12_HEAP_PROPERTIES heapProperty; heapProperty.CreationNodeMask = 0; @@ -2130,7 +2131,7 @@ void D3D12GpuBuffer::Release() unsigned int ret = InterlockedDecrement(&m_RefCount); if(ret == 0) { - D3D12GpuBufferAllocator::Inst()->Release(*this); + m_Allocator.Release(*this); delete this; } diff --git a/renderdoc/driver/d3d12/d3d12_manager.h b/renderdoc/driver/d3d12/d3d12_manager.h index 2f9ca0cdd..4ebdbb1f8 100644 --- a/renderdoc/driver/d3d12/d3d12_manager.h +++ b/renderdoc/driver/d3d12/d3d12_manager.h @@ -576,16 +576,17 @@ class D3D12GpuBufferAllocator; struct D3D12GpuBuffer { - D3D12GpuBuffer(D3D12GpuBufferHeapType heapType, D3D12GpuBufferHeapMemoryFlag heapMemory, - uint64_t size, uint64_t alignment, D3D12_GPU_VIRTUAL_ADDRESS alignedAddress, - ID3D12Resource *resource) + D3D12GpuBuffer(D3D12GpuBufferAllocator &alloc, D3D12GpuBufferHeapType heapType, + D3D12GpuBufferHeapMemoryFlag heapMemory, uint64_t size, uint64_t alignment, + D3D12_GPU_VIRTUAL_ADDRESS alignedAddress, ID3D12Resource *resource) : m_alignedAddress(alignedAddress), m_offset(0), m_alignment(alignment), m_addressContentSize(size), m_heapType(heapType), m_heapMemory(heapMemory), - m_resource(resource) + m_resource(resource), + m_Allocator(alloc) { m_RefCount = 1; if(m_resource) @@ -654,6 +655,7 @@ private: uint64_t m_offset; uint64_t m_alignment; uint64_t m_addressContentSize; + D3D12GpuBufferAllocator &m_Allocator; D3D12GpuBufferHeapType m_heapType; D3D12GpuBufferHeapMemoryFlag m_heapMemory; ID3D12Resource *m_resource; @@ -840,31 +842,11 @@ class WrappedID3D12GraphicsCommandList; class D3D12GpuBufferAllocator { public: - static bool Initialize(WrappedID3D12Device *wrappedDevice) + D3D12GpuBufferAllocator(WrappedID3D12Device *wrappedDevice) : m_wrappedDevice(wrappedDevice) { - if(m_bufferAllocator == NULL && wrappedDevice) - { - m_bufferAllocator = new D3D12GpuBufferAllocator(wrappedDevice); - } - - return m_bufferAllocator != NULL; + m_totalAllocatedMemoryInUse = 0; } - static D3D12GpuBufferAllocator *Inst() { return m_bufferAllocator; } - static bool Destroy() - { - SAFE_DELETE(m_bufferAllocator); - return true; - } - - static bool CopyBufferRegion(WrappedID3D12GraphicsCommandList *wrappedCmd, - const D3D12GpuBuffer &destBuffer, - D3D12_GPU_VIRTUAL_ADDRESS srcAddress, uint64_t dataSize); - - static bool CopyBufferRegion(WrappedID3D12GraphicsCommandList *wrappedCmd, - const D3D12GpuBuffer &destBuffer, const D3D12GpuBuffer &sourceBuffer, - uint64_t dataSize); - bool Alloc(D3D12GpuBufferHeapType heapType, D3D12GpuBufferHeapMemoryFlag heapMem, uint64_t size, D3D12GpuBuffer **gpuBuffer) { @@ -886,15 +868,13 @@ public: } private: - D3D12GpuBufferAllocator(WrappedID3D12Device *wrappedDevice) : m_wrappedDevice(wrappedDevice) - { - m_totalAllocatedMemoryInUse = 0; - } - // Class for handling buffer resources class D3D12GpuBufferResource { public: + static bool CreateBufferResource(WrappedID3D12Device *wrappedDevice, + D3D12GpuBufferHeapType heapType, uint64_t size, + D3D12GpuBufferResource **bufferResource); static bool CreateCommittedResourceBuffer(ID3D12Device *device, const D3D12_HEAP_PROPERTIES &heapProperty, D3D12_RESOURCE_STATES initState, uint64_t size, @@ -1015,12 +995,6 @@ private: uint64_t m_bufferInitSize; }; - static D3D12GpuBufferAllocator *m_bufferAllocator; - - static bool CreateBufferResource(WrappedID3D12Device *wrappedDevice, - D3D12GpuBufferHeapType heapType, uint64_t size, - D3D12GpuBufferResource **bufferResource); - Threading::CriticalSection m_bufferAllocLock; D3D12GpuBufferPool *m_bufferPoolList[(size_t)D3D12GpuBufferHeapType::Count] = {}; @@ -1082,7 +1056,8 @@ struct D3D12ShaderExportDatabase; class D3D12RaytracingResourceAndUtilHandler { public: - D3D12RaytracingResourceAndUtilHandler(WrappedID3D12Device *device); + D3D12RaytracingResourceAndUtilHandler(WrappedID3D12Device *device, + D3D12GpuBufferAllocator &gpuBufferAllocator); void CreateInternalResources(); @@ -1135,6 +1110,7 @@ private: void InitReplayBlasPatchingResources(); WrappedID3D12Device *m_wrappedDevice; + D3D12GpuBufferAllocator &m_GPUBufferAllocator; ID3D12GraphicsCommandListX *m_cmdList; ID3D12CommandAllocator *m_cmdAlloc; @@ -1190,17 +1166,13 @@ class D3D12ResourceManager : public ResourceManager T *GetLiveAs(ResourceId id, bool optional = false) @@ -1221,6 +1193,8 @@ public: return m_raytracingResourceManager; } + D3D12GpuBufferAllocator &GetGPUBufferAllocator() { return m_GPUBufferAllocator; } + template void SerialiseResourceStates(SerialiserType &ser, BarrierSet &barriers, std::map &states, @@ -1249,4 +1223,5 @@ private: WrappedID3D12Device *m_Device; D3D12RaytracingResourceAndUtilHandler *m_raytracingResourceManager; + D3D12GpuBufferAllocator m_GPUBufferAllocator; };