Skip to content

Commit 8b51b3a

Browse files
authored
Fix MaxResourceHeapSize not being honored. (#803)
1 parent 5465ad2 commit 8b51b3a

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,8 @@ namespace gpgmm::d3d12 {
616616
EventRecordScope::kPerInstance),
617617
mUseDetailedTimingEvents(descriptor.RecordOptions.UseDetailedTimingEvents),
618618
mIsCustomHeapsDisabled(descriptor.Flags & ALLOCATOR_FLAG_DISABLE_UNIFIED_MEMORY),
619-
mIsAlwaysCreateResident(descriptor.Flags & ALLOCATOR_FLAG_ALWAYS_RESIDENT) {
619+
mIsAlwaysCreateResident(descriptor.Flags & ALLOCATOR_FLAG_ALWAYS_RESIDENT),
620+
mMaxResourceHeapSize(descriptor.MaxResourceHeapSize) {
620621
ASSERT(mDevice != nullptr);
621622

622623
GPGMM_TRACE_EVENT_OBJECT_NEW(this);
@@ -745,12 +746,11 @@ namespace gpgmm::d3d12 {
745746
double memoryGrowthFactor,
746747
bool isPrefetchAllowed,
747748
std::unique_ptr<MemoryAllocator> underlyingAllocator) {
748-
const uint64_t maxResourceHeapSize = mCaps->GetMaxResourceHeapSize();
749749
switch (algorithm) {
750750
case ALLOCATOR_ALGORITHM_BUDDY_SYSTEM: {
751751
// System and memory size must be aligned at creation-time.
752752
return std::make_unique<BuddyMemoryAllocator>(
753-
/*systemSize*/ PrevPowerOfTwo(maxResourceHeapSize),
753+
/*systemSize*/ PrevPowerOfTwo(mMaxResourceHeapSize),
754754
/*memorySize*/ NextPowerOfTwo(memorySize),
755755
/*memoryAlignment*/ memoryAlignment,
756756
/*memoryAllocator*/ std::move(underlyingAllocator));
@@ -759,7 +759,7 @@ namespace gpgmm::d3d12 {
759759
// Min slab size is always equal to the memory size because the
760760
// slab allocator aligns the slab size at allocate-time.
761761
return std::make_unique<SlabCacheAllocator>(
762-
/*maxSlabSize*/ PrevPowerOfTwo(maxResourceHeapSize),
762+
/*maxSlabSize*/ PrevPowerOfTwo(mMaxResourceHeapSize),
763763
/*minSlabSize*/ memorySize,
764764
/*slabAlignment*/ memoryAlignment,
765765
/*slabFragmentationLimit*/ memoryFragmentationLimit,
@@ -975,12 +975,12 @@ namespace gpgmm::d3d12 {
975975
D3D12_RESOURCE_DESC newResourceDesc = resourceDescriptor;
976976
const D3D12_RESOURCE_ALLOCATION_INFO resourceInfo =
977977
GetResourceAllocationInfo(mDevice, newResourceDesc);
978-
if (resourceInfo.SizeInBytes > mCaps->GetMaxResourceSize()) {
978+
if (resourceInfo.SizeInBytes > mMaxResourceHeapSize) {
979979
ErrorLog(MessageId::kSizeExceeded)
980980
<< "Unable to create resource allocation because the resource size exceeded "
981981
"the capabilities of the device: "
982982
<< GPGMM_BYTES_TO_GB(resourceInfo.SizeInBytes) << " vs "
983-
<< GPGMM_BYTES_TO_GB(mCaps->GetMaxResourceSize()) << " GBs.";
983+
<< GPGMM_BYTES_TO_GB(mMaxResourceHeapSize) << " GBs.";
984984
return E_OUTOFMEMORY;
985985
}
986986

@@ -1069,7 +1069,7 @@ namespace gpgmm::d3d12 {
10691069
request.AlwaysPrefetch =
10701070
(allocationDescriptor.Flags & ALLOCATION_FLAG_ALWAYS_PREFETCH_MEMORY);
10711071
request.AlwaysCacheSize = (allocationDescriptor.Flags & ALLOCATION_FLAG_ALWAYS_CACHE_SIZE);
1072-
request.AvailableForAllocation = mCaps->GetMaxResourceHeapSize();
1072+
request.AvailableForAllocation = mMaxResourceHeapSize;
10731073

10741074
// Apply extra padding to the resource heap size, if specified.
10751075
// Padding can only be applied to standalone non-committed resources.

src/gpgmm/d3d12/ResourceAllocatorD3D12.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ namespace gpgmm::d3d12 {
159159
const bool mUseDetailedTimingEvents;
160160
const bool mIsCustomHeapsDisabled;
161161
const bool mIsAlwaysCreateResident;
162+
const uint64_t mMaxResourceHeapSize;
162163

163164
static constexpr uint64_t kNumOfResourceHeapTypes = 12u;
164165

src/tests/end2end/D3D12ResourceAllocatorTests.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,20 @@ TEST_F(D3D12ResourceAllocatorTests, CreateBuffer) {
689689
{}, CreateBasicBufferDesc(kBufferOf4MBAllocationSize),
690690
D3D12_RESOURCE_STATE_UNORDERED_ACCESS, nullptr, &allocation));
691691
}
692+
693+
// Create a buffer that exceeds the max size should always fail.
694+
{
695+
ALLOCATOR_DESC allocatorDesc = CreateBasicAllocatorDesc();
696+
allocatorDesc.MaxResourceHeapSize = kBufferOf4MBAllocationSize;
697+
698+
ComPtr<IResourceAllocator> resourceAllocatorLimitedTo4MB;
699+
ASSERT_SUCCEEDED(CreateResourceAllocator(allocatorDesc, mDevice.Get(), mAdapter.Get(),
700+
&resourceAllocatorLimitedTo4MB, nullptr));
701+
702+
ASSERT_FAILED(resourceAllocatorLimitedTo4MB->CreateResource(
703+
{}, CreateBasicBufferDesc(kBufferOf4MBAllocationSize + 1),
704+
D3D12_RESOURCE_STATE_UNORDERED_ACCESS, nullptr, nullptr));
705+
}
692706
}
693707

694708
TEST_F(D3D12ResourceAllocatorTests, CreateBufferLeaked) {

0 commit comments

Comments
 (0)