Skip to content

Commit

Permalink
Allow SUBALLOCATED_WITHIN to share only pools.
Browse files Browse the repository at this point in the history
Fixes a possible issue when nested allocators could override the allocation type.
  • Loading branch information
bbernhar committed Jan 9, 2024
1 parent 1dc9f23 commit cf21bf5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/gpgmm/common/MemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ namespace gpgmm {
std::lock_guard<std::mutex> lock(mMutex);
ASSERT(next != nullptr);
next->mParent = this->value();
SetNextInChain(std::move(next));
}

void MemoryAllocatorBase::SetNextInChain(ScopedRef<MemoryAllocatorBase> next) {
mNext = next;
}

Expand Down
1 change: 1 addition & 0 deletions src/gpgmm/common/MemoryAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ namespace gpgmm {

// Return the next MemoryAllocatorBase.
MemoryAllocatorBase* GetNextInChain() const;
void SetNextInChain(ScopedRef<MemoryAllocatorBase> next);

// Return the previous MemoryAllocatorBase.
MemoryAllocatorBase* GetParent() const;
Expand Down
6 changes: 4 additions & 2 deletions src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,10 @@ namespace gpgmm::d3d12 {
allocator->mPooledOrNonPooledHeapAllocator[resourceHeapTypeIndex];
mMSAAPooledOrNonPooledHeapAllocator[resourceHeapTypeIndex] =
allocator->mMSAAPooledOrNonPooledHeapAllocator[resourceHeapTypeIndex];
mSmallBufferAllocatorOfType[resourceHeapTypeIndex] =
allocator->mSmallBufferAllocatorOfType[resourceHeapTypeIndex];

// Small buffers have seperate pools within because they cannot use managed heaps.
mSmallBufferAllocatorOfType[resourceHeapTypeIndex]->SetNextInChain(
allocator->mSmallBufferAllocatorOfType[resourceHeapTypeIndex]->GetNextInChain());
}
}

Expand Down
40 changes: 40 additions & 0 deletions src/tests/end2end/D3D12ResourceAllocatorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2102,3 +2102,43 @@ TEST_F(D3D12ResourceAllocatorTests, NestedAllocators) {
RESOURCE_ALLOCATOR_STATS afterStats = GetStats(parentAllocator);
EXPECT_EQ(beforeStats.FreeHeapUsage, afterStats.FreeHeapUsage);
}

// Create two resource allocations using different methods using the same heaps.
TEST_F(D3D12ResourceAllocatorTests, NestedAllocatorsWithin) {
RESOURCE_ALLOCATOR_DESC desc = CreateBasicAllocatorDesc();
desc.SubAllocationAlgorithm = RESOURCE_ALLOCATION_ALGORITHM_BUDDY_SYSTEM;

RESOURCE_ALLOCATION_DESC allocationDesc = {};
allocationDesc.Flags |= RESOURCE_ALLOCATION_FLAG_ALLOW_SUBALLOCATE_WITHIN_RESOURCE;

ComPtr<IResourceAllocator> parentAllocator;
ASSERT_SUCCEEDED(
CreateResourceAllocator(desc, mDevice.Get(), mAdapter.Get(), &parentAllocator, nullptr));

{
ComPtr<IResourceAllocation> subAllocation;
ASSERT_SUCCEEDED(parentAllocator->CreateResource(allocationDesc, CreateBasicBufferDesc(1),
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr,
&subAllocation));
EXPECT_EQ(subAllocation->GetInfo().Type, RESOURCE_ALLOCATION_TYPE_SUBALLOCATED_WITHIN);
}

RESOURCE_ALLOCATOR_STATS beforeStats = GetStats(parentAllocator);
EXPECT_GT(beforeStats.FreeHeapUsage, 0u);

desc.SubAllocationAlgorithm = RESOURCE_ALLOCATION_ALGORITHM_SLAB;

ComPtr<IResourceAllocator> childAllocator;
ASSERT_SUCCEEDED(CreateResourceAllocator(desc, parentAllocator.Get(), &childAllocator));

{
ComPtr<IResourceAllocation> subAllocation;
ASSERT_SUCCEEDED(childAllocator->CreateResource(allocationDesc, CreateBasicBufferDesc(1),
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr,
&subAllocation));
EXPECT_EQ(subAllocation->GetInfo().Type, RESOURCE_ALLOCATION_TYPE_SUBALLOCATED_WITHIN);
}

RESOURCE_ALLOCATOR_STATS afterStats = GetStats(parentAllocator);
EXPECT_EQ(beforeStats.FreeHeapUsage, afterStats.FreeHeapUsage);
}

0 comments on commit cf21bf5

Please sign in to comment.