@@ -305,7 +305,8 @@ ResourceAllocatorManager::ResourceAllocatorManager(Device* device) : mDevice(dev
305305 const ResourceHeapKind resourceHeapKind = static_cast <ResourceHeapKind>(i);
306306 D3D12_HEAP_FLAGS heapFlags = GetD3D12HeapFlags (resourceHeapKind) | createNotZeroedHeapFlag;
307307 mHeapAllocators [i] = std::make_unique<HeapAllocator>(
308- mDevice , resourceHeapKind, heapFlags, GetMemorySegment (mDevice , resourceHeapKind));
308+ mDevice , resourceHeapKind, heapFlags, GetMemorySegment (mDevice , resourceHeapKind),
309+ &mAllocatedMemory );
309310 mPooledHeapAllocators [i] =
310311 std::make_unique<PooledResourceMemoryAllocator>(mHeapAllocators [i].get ());
311312 mSubAllocatedResourceAllocators [i] = std::make_unique<BuddyMemoryAllocator>(
@@ -403,6 +404,9 @@ void ResourceAllocatorManager::Tick(ExecutionSerial completedSerial) {
403404 }
404405 mAllocationsToDelete .ClearUpTo (completedSerial);
405406 mHeapsToDelete .ClearUpTo (completedSerial);
407+
408+ mAllocatedMemory .Tick (completedSerial);
409+ mUsedMemory .Tick (completedSerial);
406410}
407411
408412void ResourceAllocatorManager::DeallocateMemory (ResourceHeapAllocation& allocation) {
@@ -420,6 +424,14 @@ void ResourceAllocatorManager::DeallocateMemory(ResourceHeapAllocation& allocati
420424 if (allocation.GetInfo ().mMethod == AllocationMethod::kDirect ) {
421425 mHeapsToDelete .Enqueue (std::unique_ptr<ResourceHeapBase>(allocation.GetResourceHeap ()),
422426 mDevice ->GetQueue ()->GetPendingCommandSerial ());
427+
428+ mUsedMemory .Decrement (mDevice ->GetQueue ()->GetPendingCommandSerial (),
429+ allocation.GetInfo ().mRequestedSize );
430+ mAllocatedMemory .Decrement (mDevice ->GetQueue ()->GetPendingCommandSerial (),
431+ allocation.GetInfo ().mRequestedSize );
432+ } else if (allocation.GetInfo ().mMethod == AllocationMethod::kSubAllocated ) {
433+ mUsedMemory .Decrement (mDevice ->GetQueue ()->GetPendingCommandSerial (),
434+ allocation.GetInfo ().mRequestedSize );
423435 }
424436
425437 // Invalidate the allocation immediately in case one accidentally
@@ -502,6 +514,8 @@ ResultOrError<ResourceHeapAllocation> ResourceAllocatorManager::CreatePlacedReso
502514 optimizedClearValue, IID_PPV_ARGS (&placedResource)),
503515 " ID3D12Device::CreatePlacedResource" ));
504516
517+ mUsedMemory .Increment (resourceInfo.SizeInBytes );
518+
505519 // After CreatePlacedResource has finished, the heap can be unlocked from residency. This
506520 // will insert it into the residency LRU.
507521 mDevice ->GetResidencyManager ()->UnlockAllocation (heap);
@@ -556,6 +570,9 @@ ResultOrError<ResourceHeapAllocation> ResourceAllocatorManager::CreateCommittedR
556570 optimizedClearValue, IID_PPV_ARGS (&committedResource)),
557571 " ID3D12Device::CreateCommittedResource" ));
558572
573+ mAllocatedMemory .Increment (resourceInfo.SizeInBytes );
574+ mUsedMemory .Increment (resourceInfo.SizeInBytes );
575+
559576 // When using CreateCommittedResource, D3D12 creates an implicit heap that contains the
560577 // resource allocation. Because Dawn's memory residency management occurs at the resource
561578 // heap granularity, every directly allocated ResourceHeapAllocation also stores a Heap
@@ -583,4 +600,12 @@ void ResourceAllocatorManager::FreeRecycledAllocations() {
583600 }
584601}
585602
603+ uint64_t ResourceAllocatorManager::GetTotalAllocatedMemory () const {
604+ return mAllocatedMemory .GetSize ();
605+ }
606+
607+ uint64_t ResourceAllocatorManager::GetTotalUsedMemory () const {
608+ return mUsedMemory .GetSize ();
609+ }
610+
586611} // namespace dawn::native::d3d12
0 commit comments