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
13 changes: 9 additions & 4 deletions src/gpgmm/common/DedicatedMemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
: MemoryAllocator(std::move(memoryAllocator)) {
std::unique_ptr<MemoryAllocator> memoryAllocator,
uint64_t memoryAlignment)
: MemoryAllocator(std::move(memoryAllocator)), mMemoryAlignment(memoryAlignment) {
}

ResultOrError<std::unique_ptr<MemoryAllocation>> DedicatedMemoryAllocator::TryAllocateMemory(
Expand All @@ -32,8 +34,11 @@ namespace gpgmm {

GPGMM_INVALID_IF(!ValidateRequest(request));

MemoryAllocationRequest memoryRequest = request;
memoryRequest.Alignment = mMemoryAlignment;

std::unique_ptr<MemoryAllocation> allocation;
GPGMM_TRY_ASSIGN(GetNextInChain()->TryAllocateMemory(request), allocation);
GPGMM_TRY_ASSIGN(GetNextInChain()->TryAllocateMemory(memoryRequest), allocation);

mStats.UsedBlockCount++;
mStats.UsedBlockUsage += allocation->GetSize();
Expand Down Expand Up @@ -64,7 +69,7 @@ namespace gpgmm {
}

uint64_t DedicatedMemoryAllocator::GetMemoryAlignment() const {
return GetNextInChain()->GetMemoryAlignment();
return mMemoryAlignment;
}

const char* DedicatedMemoryAllocator::GetTypename() const {
Expand Down
5 changes: 4 additions & 1 deletion src/gpgmm/common/DedicatedMemoryAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ namespace gpgmm {
// memory to be tracked.
class DedicatedMemoryAllocator final : public MemoryAllocator {
public:
DedicatedMemoryAllocator(std::unique_ptr<MemoryAllocator> memoryAllocator);
DedicatedMemoryAllocator(std::unique_ptr<MemoryAllocator> memoryAllocator,
uint64_t memoryAlignment);

// MemoryAllocator interface
ResultOrError<std::unique_ptr<MemoryAllocation>> TryAllocateMemory(
Expand All @@ -38,6 +39,8 @@ namespace gpgmm {
private:
// ObjectBase interface
const char* GetTypename() const override;

const uint64_t mMemoryAlignment;
};

} // namespace gpgmm
Expand Down
2 changes: 1 addition & 1 deletion src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ namespace gpgmm::d3d12 {
}
case ALLOCATOR_ALGORITHM_DEDICATED: {
return std::make_unique<DedicatedMemoryAllocator>(
/*memoryAllocator*/ std::move(underlyingAllocator));
/*memoryAllocator*/ std::move(underlyingAllocator), memoryAlignment);
}
default: {
UNREACHABLE();
Expand Down
35 changes: 30 additions & 5 deletions src/tests/end2end/D3D12ResourceAllocatorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,13 +777,13 @@ TEST_F(D3D12ResourceAllocatorTests, CreateBufferDisableUMA) {
}

TEST_F(D3D12ResourceAllocatorTests, CreateSmallTexture) {
ComPtr<IResourceAllocator> resourceAllocator;
ASSERT_SUCCEEDED(
CreateResourceAllocator(CreateBasicAllocatorDesc(), &resourceAllocator, nullptr));
ASSERT_NE(resourceAllocator, nullptr);

// DXGI_FORMAT_R8G8B8A8_UNORM
{
ComPtr<IResourceAllocator> resourceAllocator;
ASSERT_SUCCEEDED(
CreateResourceAllocator(CreateBasicAllocatorDesc(), &resourceAllocator, nullptr));
ASSERT_NE(resourceAllocator, nullptr);

ALLOCATION_DESC allocationDesc = {};
allocationDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT;

Expand All @@ -796,6 +796,31 @@ TEST_F(D3D12ResourceAllocatorTests, CreateSmallTexture) {
gpgmm::IsAligned(allocation->GetInfo().SizeInBytes,
static_cast<uint32_t>(D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT)));
}

{
ALLOCATOR_DESC allocatorDesc = CreateBasicAllocatorDesc();
allocatorDesc.Flags = ALLOCATOR_FLAG_ALWAYS_ON_DEMAND;
allocatorDesc.SubAllocationAlgorithm = ALLOCATOR_ALGORITHM_DEDICATED;

ComPtr<IResourceAllocator> resourceAllocator;
ASSERT_SUCCEEDED(CreateResourceAllocator(allocatorDesc, &resourceAllocator, nullptr));

ALLOCATION_DESC allocationDesc = {};
allocationDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT;

ComPtr<IResourceAllocation> 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<uint32_t>(D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT)));
EXPECT_TRUE(
gpgmm::IsAligned(allocation->GetMemory()->GetInfo().SizeInBytes,
static_cast<uint32_t>(D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT)));
}
}

TEST_F(D3D12ResourceAllocatorTests, CreateMultisampledTexture) {
Expand Down
11 changes: 5 additions & 6 deletions src/tests/perftests/MemoryAllocatorPerfTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<DummyMemoryAllocator>());
SlabCacheAllocator allocator(state.range(1), state.range(0), kMemoryAlignment, kMemoryAlignment,
/*allowPrefetch*/ false, kDisableSlabGrowth,
std::make_unique<DummyMemoryAllocator>());

// Below is effectively equivelent to STL's reserve(size=1).
{
Expand All @@ -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<DummyMemoryAllocator>());
kDisableSlabGrowth, std::make_unique<DummyMemoryAllocator>());

for (auto _ : state) {
SingleStep(state, &allocator, CreateBasicRequest(state.range(2)));
Expand Down Expand Up @@ -129,7 +128,7 @@ BENCHMARK_DEFINE_F(SingleSizeAllocationPerfTests, BuddySystem)(benchmark::State&
}

BENCHMARK_DEFINE_F(SingleSizeAllocationPerfTests, Standalone)(benchmark::State& state) {
DedicatedMemoryAllocator allocator(std::make_unique<DummyMemoryAllocator>());
DedicatedMemoryAllocator allocator(std::make_unique<DummyMemoryAllocator>(), kMemoryAlignment);

for (auto _ : state) {
SingleStep(state, &allocator, CreateBasicRequest(state.range(2)));
Expand Down