From 25f93d7940ea32f8f0361482bc11b70b25e4f30e Mon Sep 17 00:00:00 2001 From: "Bernhart, Bryan" Date: Mon, 27 Mar 2023 15:10:10 -0700 Subject: [PATCH] Fix dedicated allocations from using resource alignments for heaps. --- src/gpgmm/common/DedicatedMemoryAllocator.cpp | 13 ++++--- src/gpgmm/common/DedicatedMemoryAllocator.h | 5 ++- src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp | 2 +- .../end2end/D3D12ResourceAllocatorTests.cpp | 35 ++++++++++++++++--- .../perftests/MemoryAllocatorPerfTests.cpp | 11 +++--- 5 files changed, 49 insertions(+), 17 deletions(-) diff --git a/src/gpgmm/common/DedicatedMemoryAllocator.cpp b/src/gpgmm/common/DedicatedMemoryAllocator.cpp index fa97da786..3e238b9e7 100644 --- a/src/gpgmm/common/DedicatedMemoryAllocator.cpp +++ b/src/gpgmm/common/DedicatedMemoryAllocator.cpp @@ -16,12 +16,14 @@ #include "gpgmm/common/MemoryBlock.h" #include "gpgmm/common/TraceEvent.h" +#include "gpgmm/utils/Math.h" namespace gpgmm { DedicatedMemoryAllocator::DedicatedMemoryAllocator( - std::unique_ptr memoryAllocator) - : MemoryAllocator(std::move(memoryAllocator)) { + std::unique_ptr memoryAllocator, + uint64_t memoryAlignment) + : MemoryAllocator(std::move(memoryAllocator)), mMemoryAlignment(memoryAlignment) { } ResultOrError> DedicatedMemoryAllocator::TryAllocateMemory( @@ -32,8 +34,11 @@ namespace gpgmm { GPGMM_INVALID_IF(!ValidateRequest(request)); + MemoryAllocationRequest memoryRequest = request; + memoryRequest.Alignment = mMemoryAlignment; + std::unique_ptr allocation; - GPGMM_TRY_ASSIGN(GetNextInChain()->TryAllocateMemory(request), allocation); + GPGMM_TRY_ASSIGN(GetNextInChain()->TryAllocateMemory(memoryRequest), allocation); mStats.UsedBlockCount++; mStats.UsedBlockUsage += allocation->GetSize(); @@ -64,7 +69,7 @@ namespace gpgmm { } uint64_t DedicatedMemoryAllocator::GetMemoryAlignment() const { - return GetNextInChain()->GetMemoryAlignment(); + return mMemoryAlignment; } const char* DedicatedMemoryAllocator::GetTypename() const { diff --git a/src/gpgmm/common/DedicatedMemoryAllocator.h b/src/gpgmm/common/DedicatedMemoryAllocator.h index 08250d9ab..30b100451 100644 --- a/src/gpgmm/common/DedicatedMemoryAllocator.h +++ b/src/gpgmm/common/DedicatedMemoryAllocator.h @@ -25,7 +25,8 @@ namespace gpgmm { // memory to be tracked. class DedicatedMemoryAllocator final : public MemoryAllocator { public: - DedicatedMemoryAllocator(std::unique_ptr memoryAllocator); + DedicatedMemoryAllocator(std::unique_ptr memoryAllocator, + uint64_t memoryAlignment); // MemoryAllocator interface ResultOrError> TryAllocateMemory( @@ -38,6 +39,8 @@ namespace gpgmm { private: // ObjectBase interface const char* GetTypename() const override; + + const uint64_t mMemoryAlignment; }; } // namespace gpgmm diff --git a/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp b/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp index 0961ad425..67b7f508e 100644 --- a/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp +++ b/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp @@ -758,7 +758,7 @@ namespace gpgmm::d3d12 { } case ALLOCATOR_ALGORITHM_DEDICATED: { return std::make_unique( - /*memoryAllocator*/ std::move(underlyingAllocator)); + /*memoryAllocator*/ std::move(underlyingAllocator), memoryAlignment); } default: { UNREACHABLE(); diff --git a/src/tests/end2end/D3D12ResourceAllocatorTests.cpp b/src/tests/end2end/D3D12ResourceAllocatorTests.cpp index b0e08060a..0b8d8a747 100644 --- a/src/tests/end2end/D3D12ResourceAllocatorTests.cpp +++ b/src/tests/end2end/D3D12ResourceAllocatorTests.cpp @@ -777,13 +777,13 @@ TEST_F(D3D12ResourceAllocatorTests, CreateBufferDisableUMA) { } TEST_F(D3D12ResourceAllocatorTests, CreateSmallTexture) { - ComPtr resourceAllocator; - ASSERT_SUCCEEDED( - CreateResourceAllocator(CreateBasicAllocatorDesc(), &resourceAllocator, nullptr)); - ASSERT_NE(resourceAllocator, nullptr); - // DXGI_FORMAT_R8G8B8A8_UNORM { + ComPtr resourceAllocator; + ASSERT_SUCCEEDED( + CreateResourceAllocator(CreateBasicAllocatorDesc(), &resourceAllocator, nullptr)); + ASSERT_NE(resourceAllocator, nullptr); + ALLOCATION_DESC allocationDesc = {}; allocationDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT; @@ -796,6 +796,31 @@ TEST_F(D3D12ResourceAllocatorTests, CreateSmallTexture) { gpgmm::IsAligned(allocation->GetInfo().SizeInBytes, static_cast(D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT))); } + + { + ALLOCATOR_DESC allocatorDesc = CreateBasicAllocatorDesc(); + allocatorDesc.Flags = ALLOCATOR_FLAG_ALWAYS_ON_DEMAND; + allocatorDesc.SubAllocationAlgorithm = ALLOCATOR_ALGORITHM_DEDICATED; + + ComPtr resourceAllocator; + ASSERT_SUCCEEDED(CreateResourceAllocator(allocatorDesc, &resourceAllocator, nullptr)); + + ALLOCATION_DESC allocationDesc = {}; + allocationDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT; + + ComPtr allocation; + ASSERT_SUCCEEDED(resourceAllocator->CreateResource( + allocationDesc, CreateBasicTextureDesc(DXGI_FORMAT_R8G8B8A8_UNORM, 1, 1), + D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, &allocation)); + + ASSERT_NE(allocation, nullptr); + EXPECT_TRUE( + gpgmm::IsAligned(allocation->GetInfo().SizeInBytes, + static_cast(D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT))); + EXPECT_TRUE( + gpgmm::IsAligned(allocation->GetMemory()->GetInfo().SizeInBytes, + static_cast(D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT))); + } } TEST_F(D3D12ResourceAllocatorTests, CreateMultisampledTexture) { diff --git a/src/tests/perftests/MemoryAllocatorPerfTests.cpp b/src/tests/perftests/MemoryAllocatorPerfTests.cpp index 1af62ba60..307db9ec4 100644 --- a/src/tests/perftests/MemoryAllocatorPerfTests.cpp +++ b/src/tests/perftests/MemoryAllocatorPerfTests.cpp @@ -79,9 +79,9 @@ class SingleSizeAllocationPerfTests : public MemoryAllocatorPerfTests { }; BENCHMARK_DEFINE_F(SingleSizeAllocationPerfTests, SlabCache_Warm)(benchmark::State& state) { - SlabCacheAllocator allocator( - state.range(1), state.range(0), kMemoryAlignment, kMemoryAlignment, /*allowPrefetch*/ false, - kDisableSlabGrowth, std::make_unique()); + SlabCacheAllocator allocator(state.range(1), state.range(0), kMemoryAlignment, kMemoryAlignment, + /*allowPrefetch*/ false, kDisableSlabGrowth, + std::make_unique()); // Below is effectively equivelent to STL's reserve(size=1). { @@ -99,8 +99,7 @@ BENCHMARK_DEFINE_F(SingleSizeAllocationPerfTests, SlabCache_Warm)(benchmark::Sta BENCHMARK_DEFINE_F(SingleSizeAllocationPerfTests, SlabCache_Cold)(benchmark::State& state) { SlabCacheAllocator allocator(state.range(1), state.range(0), kMemoryAlignment, /*slabFragmentationLimit*/ 1, /*allowPrefetch*/ false, - kDisableSlabGrowth, - std::make_unique()); + kDisableSlabGrowth, std::make_unique()); for (auto _ : state) { SingleStep(state, &allocator, CreateBasicRequest(state.range(2))); @@ -129,7 +128,7 @@ BENCHMARK_DEFINE_F(SingleSizeAllocationPerfTests, BuddySystem)(benchmark::State& } BENCHMARK_DEFINE_F(SingleSizeAllocationPerfTests, Standalone)(benchmark::State& state) { - DedicatedMemoryAllocator allocator(std::make_unique()); + DedicatedMemoryAllocator allocator(std::make_unique(), kMemoryAlignment); for (auto _ : state) { SingleStep(state, &allocator, CreateBasicRequest(state.range(2)));