Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/gpgmm/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ source_set("gpgmm_sources") {
"d3d12/D3D12Platform.h",
"d3d12/DebugObjectD3D12.cpp",
"d3d12/DebugObjectD3D12.h",
"d3d12/DebugResourceAllocatorD3D12.cpp",
"d3d12/DebugResourceAllocatorD3D12.h",
"d3d12/ErrorD3D12.cpp",
"d3d12/ErrorD3D12.h",
"d3d12/FenceD3D12.cpp",
Expand All @@ -137,6 +135,8 @@ source_set("gpgmm_sources") {
"d3d12/ResidencyManagerD3D12.h",
"d3d12/ResourceAllocationD3D12.cpp",
"d3d12/ResourceAllocationD3D12.h",
"d3d12/ResourceAllocationTrackingAllocatorD3D12.cpp",
"d3d12/ResourceAllocationTrackingAllocatorD3D12.h",
"d3d12/ResourceAllocatorD3D12.cpp",
"d3d12/ResourceAllocatorD3D12.h",
"d3d12/ResourceHeapAllocatorD3D12.cpp",
Expand Down
4 changes: 2 additions & 2 deletions src/gpgmm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ if (GPGMM_ENABLE_D3D12)
"d3d12/BufferAllocatorD3D12.h"
"d3d12/DebugObjectD3D12.cpp"
"d3d12/DebugObjectD3D12.h"
"d3d12/DebugResourceAllocatorD3D12.cpp"
"d3d12/DebugResourceAllocatorD3D12.h"
"d3d12/ResourceAllocationTrackingAllocatorD3D12.cpp"
"d3d12/ResourceAllocationTrackingAllocatorD3D12.h"
"d3d12/CapsD3D12.cpp"
"d3d12/CapsD3D12.h"
"d3d12/ErrorD3D12.cpp"
Expand Down
6 changes: 3 additions & 3 deletions src/gpgmm/d3d12/ResourceAllocationD3D12.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

namespace gpgmm::d3d12 {

class DebugResourceAllocator;
class ResourceAllocationTrackingAllocator;
class ResidencyManager;
class ResourceAllocator;
class ResidencyHeap;
Expand Down Expand Up @@ -67,8 +67,8 @@ namespace gpgmm::d3d12 {
MemoryBlock* block,
ComPtr<ID3D12Resource> resource);

// Only DebugResourceAllocator may inject itself to ensure |this| cannot leak.
friend DebugResourceAllocator;
// Only ResourceAllocationTrackingAllocator may inject itself to ensure |this| cannot leak.
friend ResourceAllocationTrackingAllocator;
void SetDebugAllocator(MemoryAllocator* allocator);

HRESULT SetDebugNameImpl(LPCWSTR name) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "gpgmm/d3d12/DebugResourceAllocatorD3D12.h"
#include "gpgmm/d3d12/ResourceAllocationTrackingAllocatorD3D12.h"

#include "gpgmm/common/EventMessage.h"
#include "gpgmm/d3d12/BackendD3D12.h"
Expand All @@ -23,30 +23,32 @@

namespace gpgmm::d3d12 {

DebugResourceAllocator::ResourceAllocationEntry::ResourceAllocationEntry(
ResourceAllocationTrackingAllocator::ResourceAllocationEntry::ResourceAllocationEntry(
ResourceAllocation* allocation)
: mAllocation(allocation) {
}

DebugResourceAllocator::ResourceAllocationEntry::ResourceAllocationEntry(
ResourceAllocationTrackingAllocator::ResourceAllocationEntry::ResourceAllocationEntry(
ResourceAllocation* allocation,
MemoryAllocator* allocator)
: mAllocation(allocation), mAllocator(allocator) {
}

MemoryAllocator* DebugResourceAllocator::ResourceAllocationEntry::GetAllocator() const {
MemoryAllocator* ResourceAllocationTrackingAllocator::ResourceAllocationEntry::GetAllocator()
const {
return mAllocator;
}

ResourceAllocation* DebugResourceAllocator::ResourceAllocationEntry::GetAllocation() const {
ResourceAllocation*
ResourceAllocationTrackingAllocator::ResourceAllocationEntry::GetAllocation() const {
return mAllocation;
}

size_t DebugResourceAllocator::ResourceAllocationEntry::GetKey() const {
size_t ResourceAllocationTrackingAllocator::ResourceAllocationEntry::GetKey() const {
return reinterpret_cast<uintptr_t>(mAllocation);
}

void DebugResourceAllocator::ReportLiveAllocations() const {
void ResourceAllocationTrackingAllocator::ReportLiveAllocations() const {
std::lock_guard<std::mutex> lock(mMutex);
for (auto allocationEntry : mLiveAllocations) {
const ResourceAllocation* allocation = allocationEntry->GetValue().GetAllocation();
Expand All @@ -55,7 +57,7 @@ namespace gpgmm::d3d12 {
}
}

void DebugResourceAllocator::ReleaseLiveAllocationsForTesting() {
void ResourceAllocationTrackingAllocator::ReleaseLiveAllocationsForTesting() {
std::lock_guard<std::mutex> lock(mMutex);
for (auto allocationEntry : mLiveAllocations) {
allocationEntry->GetValue().GetAllocator()->DeallocateMemory(
Expand All @@ -65,7 +67,7 @@ namespace gpgmm::d3d12 {
mLiveAllocations.clear();
}

void DebugResourceAllocator::AddLiveAllocation(ResourceAllocation* allocation) {
void ResourceAllocationTrackingAllocator::TrackAllocation(ResourceAllocation* allocation) {
std::lock_guard<std::mutex> lock(mMutex);

mLiveAllocations.GetOrCreate(
Expand All @@ -75,11 +77,12 @@ namespace gpgmm::d3d12 {
allocation->SetDebugAllocator(this);
}

void DebugResourceAllocator::DeallocateMemory(std::unique_ptr<MemoryAllocation> allocation) {
void ResourceAllocationTrackingAllocator::DeallocateMemory(
std::unique_ptr<MemoryAllocation> allocation) {
std::lock_guard<std::mutex> lock(mMutex);

// KeepAlive must be false so |mLiveAllocations| cache will shrink by 1 entry once |entry|
// falls out of scope below since AddLiveAllocation() adds one (and only one) ref.
// falls out of scope below since TrackAllocation() adds one (and only one) ref.
auto entry = mLiveAllocations.GetOrCreate(
ResourceAllocationEntry(ToBackend(allocation.get())), false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef GPGMM_D3D12_DEBUGRESOURCEALLOCATORD3D12_H_
#define GPGMM_D3D12_DEBUGRESOURCEALLOCATORD3D12_H_
#ifndef GPGMM_D3D12_RESOURCEALLOCATIONTRACKINGALLOCATORD3D12_H_
#define GPGMM_D3D12_RESOURCEALLOCATIONTRACKINGALLOCATORD3D12_H_

#include "gpgmm/common/MemoryAllocator.h"
#include "gpgmm/common/MemoryCache.h"
Expand All @@ -22,14 +22,14 @@ namespace gpgmm::d3d12 {

class ResourceAllocation;

// DebugResourceAllocator tracks "live" allocations so they can be reported if leaked.
// A "live" allocation means the allocation was created (allocated) but not released
// ResourceAllocationTrackingAllocator tracks "live" allocations so they can be reported if
// leaked. A "live" allocation means the allocation was created (allocated) but not released
// (de-allocated).
class DebugResourceAllocator final : public MemoryAllocator {
class ResourceAllocationTrackingAllocator final : public MemoryAllocator {
public:
DebugResourceAllocator() = default;
ResourceAllocationTrackingAllocator() = default;

void AddLiveAllocation(ResourceAllocation* allocation);
void TrackAllocation(ResourceAllocation* allocation);
void ReportLiveAllocations() const;
void ReleaseLiveAllocationsForTesting();

Expand Down Expand Up @@ -58,4 +58,4 @@ namespace gpgmm::d3d12 {

} // namespace gpgmm::d3d12

#endif // GPGMM_D3D12_DEBUGRESOURCEALLOCATORD3D12_H_
#endif // GPGMM_D3D12_RESOURCEALLOCATIONTRACKINGALLOCATORD3D12_H_
14 changes: 7 additions & 7 deletions src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
#include "gpgmm/d3d12/BackendD3D12.h"
#include "gpgmm/d3d12/BufferAllocatorD3D12.h"
#include "gpgmm/d3d12/CapsD3D12.h"
#include "gpgmm/d3d12/DebugResourceAllocatorD3D12.h"
#include "gpgmm/d3d12/ErrorD3D12.h"
#include "gpgmm/d3d12/JSONSerializerD3D12.h"
#include "gpgmm/d3d12/LogD3D12.h"
#include "gpgmm/d3d12/ResidencyHeapD3D12.h"
#include "gpgmm/d3d12/ResidencyManagerD3D12.h"
#include "gpgmm/d3d12/ResourceAllocationD3D12.h"
#include "gpgmm/d3d12/ResourceAllocationTrackingAllocatorD3D12.h"
#include "gpgmm/d3d12/ResourceHeapAllocatorD3D12.h"
#include "gpgmm/d3d12/ResourceSizeD3D12.h"
#include "gpgmm/d3d12/UtilsD3D12.h"
Expand Down Expand Up @@ -600,7 +600,7 @@ namespace gpgmm::d3d12 {
GPGMM_TRACE_EVENT_OBJECT_NEW(this);

if (descriptor.Flags & RESOURCE_ALLOCATOR_FLAG_NEVER_LEAK) {
mDebugAllocator = std::make_unique<DebugResourceAllocator>();
mTrackingAllocator = std::make_unique<ResourceAllocationTrackingAllocator>();
}

const bool isUMA =
Expand Down Expand Up @@ -813,9 +813,9 @@ namespace gpgmm::d3d12 {
// Give the debug allocator the first chance to report allocation leaks.
// If allocation leak exists, report then release them immediately to prevent another leak
// check from re-reporting the leaked allocation.
if (mDebugAllocator) {
mDebugAllocator->ReportLiveAllocations();
mDebugAllocator->ReleaseLiveAllocationsForTesting();
if (mTrackingAllocator) {
mTrackingAllocator->ReportLiveAllocations();
mTrackingAllocator->ReleaseLiveAllocationsForTesting();
}

// Destroy allocators in the reverse order they were created so we can record delete events
Expand Down Expand Up @@ -956,10 +956,10 @@ namespace gpgmm::d3d12 {

ASSERT(allocation->GetResource() != nullptr);

if (GPGMM_UNLIKELY(mDebugAllocator)) {
if (GPGMM_UNLIKELY(mTrackingAllocator)) {
GPGMM_RETURN_IF_FAILED(allocation->SetDebugName(allocationDescriptor.DebugName),
mDevice);
mDebugAllocator->AddLiveAllocation(static_cast<ResourceAllocation*>(allocation.Get()));
mTrackingAllocator->TrackAllocation(static_cast<ResourceAllocation*>(allocation.Get()));
}

// Update the current usage counters.
Expand Down
4 changes: 2 additions & 2 deletions src/gpgmm/d3d12/ResourceAllocatorD3D12.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace gpgmm::d3d12 {
class BufferAllocator;
class Caps;
class ResidencyHeap;
class DebugResourceAllocator;
class ResourceAllocationTrackingAllocator;
class ResidencyManager;
class ResourceAllocation;

Expand Down Expand Up @@ -179,7 +179,7 @@ namespace gpgmm::d3d12 {
std::array<std::unique_ptr<MemoryAllocator>, kNumOfResourceHeapTypes>
mSmallBufferAllocatorOfType;

std::unique_ptr<DebugResourceAllocator> mDebugAllocator;
std::unique_ptr<ResourceAllocationTrackingAllocator> mTrackingAllocator;
};

} // namespace gpgmm::d3d12
Expand Down