diff --git a/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp b/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp index 1d81ab130..e1d55d55c 100644 --- a/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp +++ b/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp @@ -826,8 +826,12 @@ namespace gpgmm { namespace d3d12 { allocationDescriptor.HeapType, heapFlags, resourceInfo.SizeInBytes, &newResourceDesc, clearValue, initialResourceState, &committedResource, &resourceHeap)); - mInfo.UsedMemoryUsage += resourceHeap->GetSize(); + // Using committed resources will create a tightly allocated resource allocations. + // This means the block and heap size should be equal (modulo driver padding). + const uint64_t allocationSize = resourceHeap->GetSize(); + mInfo.UsedMemoryUsage += allocationSize; mInfo.UsedMemoryCount++; + mInfo.UsedBlockUsage += allocationSize; *resourceAllocationOut = new ResourceAllocation{mResidencyManager.Get(), /*allocator*/ this, @@ -1017,8 +1021,11 @@ namespace gpgmm { namespace d3d12 { std::lock_guard lock(mMutex); - mInfo.UsedMemoryUsage -= allocation->GetSize(); + const uint64_t allocationSize = allocation->GetSize(); + mInfo.UsedMemoryUsage -= allocationSize; mInfo.UsedMemoryCount--; + mInfo.UsedBlockUsage -= allocationSize; + SafeRelease(allocation); } diff --git a/src/tests/end2end/D3D12ResourceAllocatorTests.cpp b/src/tests/end2end/D3D12ResourceAllocatorTests.cpp index 82c8c2faf..6267754ff 100644 --- a/src/tests/end2end/D3D12ResourceAllocatorTests.cpp +++ b/src/tests/end2end/D3D12ResourceAllocatorTests.cpp @@ -384,6 +384,10 @@ TEST_F(D3D12ResourceAllocatorTests, CreateBufferAlwaysCommitted) { Heap* resourceHeap = ToBackend(allocation->GetMemory()); ASSERT_NE(resourceHeap, nullptr); ASSERT_EQ(resourceHeap->GetHeap(), nullptr); + + // Commited resources must use all the memory allocated. + EXPECT_EQ(allocator->GetInfo().UsedMemoryUsage, kDefaultPreferredResourceHeapSize); + EXPECT_EQ(allocator->GetInfo().UsedBlockUsage, allocator->GetInfo().UsedMemoryUsage); } TEST_F(D3D12ResourceAllocatorTests, CreateBufferNeverAllocate) {