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
38 changes: 24 additions & 14 deletions src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,11 @@ namespace gpgmm::d3d12 {
? std::min(allocatorDescriptor.MaxResourceHeapSize, caps->GetMaxResourceHeapSize())
: caps->GetMaxResourceHeapSize();

newDescriptor.PreferredResourceHeapSize =
(allocatorDescriptor.PreferredResourceHeapSize == 0)
? kNoRequiredAlignment
: allocatorDescriptor.PreferredResourceHeapSize;

newDescriptor.MemoryFragmentationLimit = (allocatorDescriptor.MemoryFragmentationLimit > 0)
? allocatorDescriptor.MemoryFragmentationLimit
: kDefaultFragmentationLimit;
Expand Down Expand Up @@ -611,16 +616,19 @@ namespace gpgmm::d3d12 {
const uint64_t maxResourceHeapSize = mCaps->GetMaxResourceHeapSize();
switch (algorithm) {
case ALLOCATOR_ALGORITHM_BUDDY_SYSTEM: {
// System and memory size must be aligned at creation-time.
return std::make_unique<BuddyMemoryAllocator>(
/*systemSize*/ PrevPowerOfTwo(maxResourceHeapSize),
/*memorySize*/ std::max(memoryAlignment, memorySize),
/*memorySize*/ NextPowerOfTwo(memorySize),
/*memoryAlignment*/ memoryAlignment,
/*memoryAllocator*/ std::move(underlyingAllocator));
}
case ALLOCATOR_ALGORITHM_SLAB: {
// Min slab size is always equal to the memory size because the
// slab allocator aligns the slab size at allocate-time.
return std::make_unique<SlabCacheAllocator>(
/*maxSlabSize*/ PrevPowerOfTwo(maxResourceHeapSize),
/*minSlabSize*/ std::max(memoryAlignment, memorySize),
/*minSlabSize*/ memorySize,
/*slabAlignment*/ memoryAlignment,
/*slabFragmentationLimit*/ memoryFragmentationLimit,
/*allowSlabPrefetch*/ isPrefetchAllowed,
Expand All @@ -647,15 +655,16 @@ namespace gpgmm::d3d12 {
std::make_unique<ResourceHeapAllocator>(mResidencyManager.Get(), mDevice.Get(),
heapProperties, heapFlags, mIsAlwaysInBudget);

const uint64_t heapSize =
std::max(heapAlignment, AlignTo(descriptor.PreferredResourceHeapSize, heapAlignment));

std::unique_ptr<MemoryAllocator> pooledOrNonPooledAllocator = CreatePoolAllocator(
descriptor.PoolAlgorithm, /*memorySize*/ descriptor.PreferredResourceHeapSize,
heapAlignment, (descriptor.Flags & ALLOCATOR_FLAG_ALWAYS_ON_DEMAND),
std::move(resourceHeapAllocator));
descriptor.PoolAlgorithm, heapSize, heapAlignment,
(descriptor.Flags & ALLOCATOR_FLAG_ALWAYS_ON_DEMAND), std::move(resourceHeapAllocator));

return CreateSubAllocator(
descriptor.SubAllocationAlgorithm,
/*memorySize*/ std::max(heapAlignment, descriptor.PreferredResourceHeapSize),
heapAlignment, descriptor.MemoryFragmentationLimit, descriptor.MemoryGrowthFactor,
descriptor.SubAllocationAlgorithm, heapSize, heapAlignment,
descriptor.MemoryFragmentationLimit, descriptor.MemoryGrowthFactor,
/*allowSlabPrefetch*/ !(descriptor.Flags & ALLOCATOR_FLAG_DISABLE_MEMORY_PREFETCH),
std::move(pooledOrNonPooledAllocator));
}
Expand All @@ -677,14 +686,15 @@ namespace gpgmm::d3d12 {
(descriptor.Flags & ALLOCATOR_FLAG_ALWAYS_ON_DEMAND),
std::move(smallBufferOnlyAllocator));

const uint64_t heapSize =
std::max(heapAlignment, AlignTo(descriptor.PreferredResourceHeapSize, heapAlignment));

// Any amount of fragmentation must be allowed for small buffers since the allocation can
// be smaller then the resource heap alignment.
return CreateSubAllocator(
descriptor.SubAllocationAlgorithm,
/*memorySize*/ std::max(heapAlignment, descriptor.PreferredResourceHeapSize),
heapAlignment,
/*memoryFragmentationLimit*/ 1, descriptor.MemoryGrowthFactor,
/*allowSlabPrefetch*/ false, std::move(pooledOrNonPooledAllocator));
return CreateSubAllocator(descriptor.SubAllocationAlgorithm, heapSize, heapAlignment,
/*memoryFragmentationLimit*/ 1, descriptor.MemoryGrowthFactor,
/*allowSlabPrefetch*/ false,
std::move(pooledOrNonPooledAllocator));
}

ResourceAllocator::~ResourceAllocator() {
Expand Down
62 changes: 62 additions & 0 deletions src/tests/end2end/D3D12ResourceAllocatorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,68 @@ TEST_F(D3D12ResourceAllocatorTests, CreateBufferSubAllocated) {
}
}

TEST_F(D3D12ResourceAllocatorTests, CreateBufferWithPreferredHeapSize) {
ALLOCATOR_DESC allocatorDesc = CreateBasicAllocatorDesc();

// ALLOCATOR_ALGORITHM_SLAB
{
ALLOCATOR_DESC newAllocatorDesc = allocatorDesc;
newAllocatorDesc.SubAllocationAlgorithm = ALLOCATOR_ALGORITHM_SLAB;
newAllocatorDesc.PreferredResourceHeapSize = GPGMM_MB_TO_BYTES(12);

ComPtr<ResourceAllocator> resourceAllocator;
ASSERT_SUCCEEDED(ResourceAllocator::CreateAllocator(newAllocatorDesc, &resourceAllocator));
ASSERT_NE(resourceAllocator, nullptr);

ComPtr<ResourceAllocation> allocation;
ASSERT_SUCCEEDED(
resourceAllocator->CreateResource({}, CreateBasicBufferDesc(kDefaultBufferSize),
D3D12_RESOURCE_STATE_COMMON, nullptr, &allocation));

// Slab allocator requires heaps to be in aligned in powers-of-two sizes.
EXPECT_EQ(allocation->GetMemory()->GetSize(), GPGMM_MB_TO_BYTES(16));
}

// ALLOCATOR_ALGORITHM_BUDDY_SYSTEM
{
ALLOCATOR_DESC newAllocatorDesc = allocatorDesc;
newAllocatorDesc.SubAllocationAlgorithm = ALLOCATOR_ALGORITHM_BUDDY_SYSTEM;
newAllocatorDesc.PreferredResourceHeapSize = GPGMM_MB_TO_BYTES(12);

ComPtr<ResourceAllocator> resourceAllocator;
ASSERT_SUCCEEDED(ResourceAllocator::CreateAllocator(newAllocatorDesc, &resourceAllocator));
ASSERT_NE(resourceAllocator, nullptr);

ComPtr<ResourceAllocation> allocation;
ASSERT_SUCCEEDED(
resourceAllocator->CreateResource({}, CreateBasicBufferDesc(kDefaultBufferSize),
D3D12_RESOURCE_STATE_COMMON, nullptr, &allocation));

// Buddy allocator requires heaps to be in aligned in powers-of-two sizes.
EXPECT_EQ(allocation->GetMemory()->GetSize(), GPGMM_MB_TO_BYTES(16));
}

// ALLOCATOR_ALGORITHM_DEDICATED
{
ALLOCATOR_DESC newAllocatorDesc = allocatorDesc;
newAllocatorDesc.SubAllocationAlgorithm = ALLOCATOR_ALGORITHM_DEDICATED;
newAllocatorDesc.PreferredResourceHeapSize = GPGMM_MB_TO_BYTES(12);

ComPtr<ResourceAllocator> resourceAllocator;
ASSERT_SUCCEEDED(ResourceAllocator::CreateAllocator(newAllocatorDesc, &resourceAllocator));
ASSERT_NE(resourceAllocator, nullptr);

ComPtr<ResourceAllocation> allocation;
ASSERT_SUCCEEDED(
resourceAllocator->CreateResource({}, CreateBasicBufferDesc(kDefaultBufferSize),
D3D12_RESOURCE_STATE_COMMON, nullptr, &allocation));

// Dedicated allocator ignores the preferred resource heap size and allocates exactly what
// is needed.
EXPECT_EQ(allocation->GetMemory()->GetSize(), kDefaultBufferSize);
}
}

TEST_F(D3D12ResourceAllocatorTests, CreateBufferManyDeallocateAtEnd) {
ComPtr<ResourceAllocator> resourceAllocator;
ASSERT_SUCCEEDED(
Expand Down