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
63 changes: 46 additions & 17 deletions src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,24 @@ namespace gpgmm::d3d12 {
enum RESOURCE_HEAP_TYPE {
// Resource heap tier 2
// Resource heaps contain all buffer and textures types.
RESOURCE_HEAP_TYPE_READBACK_ALLOW_ALL_BUFFERS_AND_TEXTURES = 0x0,
RESOURCE_HEAP_TYPE_UPLOAD_ALLOW_ALL_BUFFERS_AND_TEXTURES = 0x1,
RESOURCE_HEAP_TYPE_DEFAULT_ALLOW_ALL_BUFFERS_AND_TEXTURES = 0x2,
RESOURCE_HEAP_TYPE_READBACK_ALLOW_ALL_BUFFERS_AND_TEXTURES = 0,
RESOURCE_HEAP_TYPE_UPLOAD_ALLOW_ALL_BUFFERS_AND_TEXTURES = 1,
RESOURCE_HEAP_TYPE_DEFAULT_ALLOW_ALL_BUFFERS_AND_TEXTURES = 2,

// Resource heap tier 1
// Resource heaps contain buffers or textures but not both.
RESOURCE_HEAP_TYPE_READBACK_ALLOW_ONLY_BUFFERS = 0x3,
RESOURCE_HEAP_TYPE_UPLOAD_ALLOW_ONLY_BUFFERS = 0x4,
RESOURCE_HEAP_TYPE_DEFAULT_ALLOW_ONLY_BUFFERS = 0x5,

RESOURCE_HEAP_TYPE_DEFAULT_ALLOW_ONLY_NON_RT_OR_DS_TEXTURES = 0x6,
RESOURCE_HEAP_TYPE_DEFAULT_ALLOW_ONLY_RT_OR_DS_TEXTURES = 0x7,
RESOURCE_HEAP_TYPE_READBACK_ALLOW_ONLY_BUFFERS = 3,
RESOURCE_HEAP_TYPE_UPLOAD_ALLOW_ONLY_BUFFERS = 4,
RESOURCE_HEAP_TYPE_DEFAULT_ALLOW_ONLY_BUFFERS = 5,

// Further heap attribution is required for tier 1: textures are categorized into render
// target or depth-stencil textures but not both.
RESOURCE_HEAP_TYPE_READBACK_ALLOW_ONLY_NON_RT_OR_DS_TEXTURES = 6,
RESOURCE_HEAP_TYPE_READBACK_ALLOW_ONLY_RT_OR_DS_TEXTURES = 7,
RESOURCE_HEAP_TYPE_UPLOAD_ALLOW_ONLY_NON_RT_OR_DS_TEXTURES = 8,
RESOURCE_HEAP_TYPE_UPLOAD_ALLOW_ONLY_RT_OR_DS_TEXTURES = 9,
RESOURCE_HEAP_TYPE_DEFAULT_ALLOW_ONLY_NON_RT_OR_DS_TEXTURES = 10,
RESOURCE_HEAP_TYPE_DEFAULT_ALLOW_ONLY_RT_OR_DS_TEXTURES = 11,

RESOURCE_HEAP_TYPE_INVALID,
};
Expand Down Expand Up @@ -105,6 +111,8 @@ namespace gpgmm::d3d12 {
switch (resourceHeapType) {
case RESOURCE_HEAP_TYPE_READBACK_ALLOW_ONLY_BUFFERS:
case RESOURCE_HEAP_TYPE_READBACK_ALLOW_ALL_BUFFERS_AND_TEXTURES:
case RESOURCE_HEAP_TYPE_READBACK_ALLOW_ONLY_NON_RT_OR_DS_TEXTURES:
case RESOURCE_HEAP_TYPE_READBACK_ALLOW_ONLY_RT_OR_DS_TEXTURES:
return D3D12_HEAP_TYPE_READBACK;
case RESOURCE_HEAP_TYPE_DEFAULT_ALLOW_ALL_BUFFERS_AND_TEXTURES:
case RESOURCE_HEAP_TYPE_DEFAULT_ALLOW_ONLY_BUFFERS:
Expand All @@ -113,6 +121,8 @@ namespace gpgmm::d3d12 {
return D3D12_HEAP_TYPE_DEFAULT;
case RESOURCE_HEAP_TYPE_UPLOAD_ALLOW_ONLY_BUFFERS:
case RESOURCE_HEAP_TYPE_UPLOAD_ALLOW_ALL_BUFFERS_AND_TEXTURES:
case RESOURCE_HEAP_TYPE_UPLOAD_ALLOW_ONLY_NON_RT_OR_DS_TEXTURES:
case RESOURCE_HEAP_TYPE_UPLOAD_ALLOW_ONLY_RT_OR_DS_TEXTURES:
return D3D12_HEAP_TYPE_UPLOAD;
default:
UNREACHABLE();
Expand All @@ -133,8 +143,12 @@ namespace gpgmm::d3d12 {
case RESOURCE_HEAP_TYPE_UPLOAD_ALLOW_ONLY_BUFFERS:
return createHeapFlags | D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS;
case RESOURCE_HEAP_TYPE_DEFAULT_ALLOW_ONLY_NON_RT_OR_DS_TEXTURES:
case RESOURCE_HEAP_TYPE_READBACK_ALLOW_ONLY_NON_RT_OR_DS_TEXTURES:
case RESOURCE_HEAP_TYPE_UPLOAD_ALLOW_ONLY_NON_RT_OR_DS_TEXTURES:
return createHeapFlags | D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES;
case RESOURCE_HEAP_TYPE_DEFAULT_ALLOW_ONLY_RT_OR_DS_TEXTURES:
case RESOURCE_HEAP_TYPE_READBACK_ALLOW_ONLY_RT_OR_DS_TEXTURES:
case RESOURCE_HEAP_TYPE_UPLOAD_ALLOW_ONLY_RT_OR_DS_TEXTURES:
return createHeapFlags | D3D12_HEAP_FLAG_ALLOW_ONLY_RT_DS_TEXTURES;
default:
UNREACHABLE();
Expand Down Expand Up @@ -193,6 +207,14 @@ namespace gpgmm::d3d12 {
case D3D12_RESOURCE_DIMENSION_TEXTURE2D:
case D3D12_RESOURCE_DIMENSION_TEXTURE3D: {
switch (heapType) {
case D3D12_HEAP_TYPE_UPLOAD: {
if ((flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL) ||
(flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET)) {
return RESOURCE_HEAP_TYPE_UPLOAD_ALLOW_ONLY_RT_OR_DS_TEXTURES;
}
return RESOURCE_HEAP_TYPE_UPLOAD_ALLOW_ONLY_NON_RT_OR_DS_TEXTURES;
}

case D3D12_HEAP_TYPE_DEFAULT: {
if ((flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL) ||
(flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET)) {
Expand All @@ -201,6 +223,15 @@ namespace gpgmm::d3d12 {
return RESOURCE_HEAP_TYPE_DEFAULT_ALLOW_ONLY_NON_RT_OR_DS_TEXTURES;
}

case D3D12_HEAP_TYPE_READBACK: {
if ((flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL) ||
(flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET)) {
return RESOURCE_HEAP_TYPE_READBACK_ALLOW_ONLY_RT_OR_DS_TEXTURES;
}
return RESOURCE_HEAP_TYPE_READBACK_ALLOW_ONLY_NON_RT_OR_DS_TEXTURES;
}

case D3D12_HEAP_TYPE_CUSTOM:
default:
return RESOURCE_HEAP_TYPE_INVALID;
}
Expand Down Expand Up @@ -388,14 +419,12 @@ namespace gpgmm::d3d12 {
caps.reset(ptr);
}

if (allocatorDescriptor.ResourceHeapTier != caps->GetMaxResourceHeapTierSupported()) {
gpgmm::WarningLog()
<< "Resource heap tier does not match capabilities of the adapter "
"(ResourceHeapTier:"
<< allocatorDescriptor.ResourceHeapTier << " vs "
<< caps->GetMaxResourceHeapTierSupported()
<< "). This is probably not what the developer intended. Please use "
"CheckFeatureSupport instead.";
if (allocatorDescriptor.ResourceHeapTier > caps->GetMaxResourceHeapTierSupported()) {
gpgmm::ErrorLog() << "Resource heap tier does not match capabilities of the adapter "
"(ResourceHeapTier:"
<< allocatorDescriptor.ResourceHeapTier << " vs "
<< caps->GetMaxResourceHeapTierSupported() << ").";
return E_FAIL;
}

ALLOCATOR_DESC newDescriptor = allocatorDescriptor;
Expand Down
2 changes: 1 addition & 1 deletion src/gpgmm/d3d12/ResourceAllocatorD3D12.h
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ namespace gpgmm::d3d12 {
const bool mUseDetailedTimingEvents;
const bool mIsCustomHeapsDisabled;

static constexpr uint64_t kNumOfResourceHeapTypes = 8u;
static constexpr uint64_t kNumOfResourceHeapTypes = 12u;

std::array<std::unique_ptr<MemoryAllocator>, kNumOfResourceHeapTypes>
mDedicatedResourceAllocatorOfType;
Expand Down
84 changes: 84 additions & 0 deletions src/tests/end2end/D3D12ResourceAllocatorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ TEST_F(D3D12ResourceAllocatorTests, CreateAllocator) {
EXPECT_EQ(resourceAllocator, nullptr);
}

// Creating an allocator with the wrong resource heap tier should always fail.
{
// Tier 3 doesn't exist in D3D12.
ALLOCATOR_DESC desc = CreateBasicAllocatorDesc();
desc.ResourceHeapTier =
static_cast<D3D12_RESOURCE_HEAP_TIER>(D3D12_RESOURCE_HEAP_TIER_2 + 1);

ComPtr<ResourceAllocator> resourceAllocator;
EXPECT_FAILED(ResourceAllocator::CreateAllocator(desc, &resourceAllocator));
EXPECT_EQ(resourceAllocator, nullptr);
}

// Creating a NULL allocator should always succeed.
EXPECT_SUCCEEDED(ResourceAllocator::CreateAllocator(CreateBasicAllocatorDesc(), nullptr));

Expand Down Expand Up @@ -153,6 +165,78 @@ TEST_F(D3D12ResourceAllocatorTests, CreateBufferNoLeak) {
GPGMM_TEST_MEMORY_LEAK_END();
}

// Exceeding the max resource heap size should always fail.
TEST_F(D3D12ResourceAllocatorTests, CreateBufferAndTextureInSameHeap) {
ALLOCATOR_DESC allocatorDesc = CreateBasicAllocatorDesc();

// Heaps of only the same size can be reused between resource types.
allocatorDesc.PreferredResourceHeapSize = kBufferOf4MBAllocationSize;

// Adapter must support mixing of resource types in same heap.
GPGMM_SKIP_TEST_IF(allocatorDesc.ResourceHeapTier < D3D12_RESOURCE_HEAP_TIER_2);

ComPtr<ResourceAllocator> resourceAllocator;
ASSERT_SUCCEEDED(ResourceAllocator::CreateAllocator(allocatorDesc, &resourceAllocator));

// Create memory for buffer in Heap A.
{
ComPtr<ResourceAllocation> bufferAllocation;
ASSERT_SUCCEEDED(resourceAllocator->CreateResource(
{}, CreateBasicBufferDesc(kBufferOf4MBAllocationSize), D3D12_RESOURCE_STATE_COMMON,
nullptr, &bufferAllocation));
}

EXPECT_EQ(resourceAllocator->GetInfo().FreeMemoryUsage, kBufferOf4MBAllocationSize);

// Reuse memory for texture in Heap A.
{
ComPtr<ResourceAllocation> textureAllocation;
ASSERT_SUCCEEDED(resourceAllocator->CreateResource(
{}, CreateBasicTextureDesc(DXGI_FORMAT_R8G8B8A8_UNORM, 1, 1),
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, &textureAllocation));
}

EXPECT_EQ(resourceAllocator->GetInfo().FreeMemoryUsage, kBufferOf4MBAllocationSize);
}

// Exceeding the max resource heap size should always fail.
TEST_F(D3D12ResourceAllocatorTests, CreateBufferAndTextureInSeperateHeap) {
ALLOCATOR_DESC allocatorDesc = CreateBasicAllocatorDesc();
allocatorDesc.ResourceHeapTier = D3D12_RESOURCE_HEAP_TIER_1;

// Heaps of only the same size can be reused between resource types.
allocatorDesc.PreferredResourceHeapSize = kBufferOf4MBAllocationSize;

ComPtr<ResourceAllocator> resourceAllocator;
ASSERT_SUCCEEDED(ResourceAllocator::CreateAllocator(allocatorDesc, &resourceAllocator));

// Create memory for buffer in Heap A.
{
ComPtr<ResourceAllocation> bufferAllocation;
ASSERT_SUCCEEDED(resourceAllocator->CreateResource(
{}, CreateBasicBufferDesc(kBufferOf4MBAllocationSize), D3D12_RESOURCE_STATE_COMMON,
nullptr, &bufferAllocation));

EXPECT_EQ(bufferAllocation->GetMemory()->GetSize(),
allocatorDesc.PreferredResourceHeapSize);
}

EXPECT_EQ(resourceAllocator->GetInfo().FreeMemoryUsage, kBufferOf4MBAllocationSize);

// Reuse memory for texture in Heap A.
{
ComPtr<ResourceAllocation> textureAllocation;
ASSERT_SUCCEEDED(resourceAllocator->CreateResource(
{}, CreateBasicTextureDesc(DXGI_FORMAT_R8G8B8A8_UNORM, 1, 1),
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, &textureAllocation));

EXPECT_EQ(textureAllocation->GetMemory()->GetSize(),
allocatorDesc.PreferredResourceHeapSize);
}

EXPECT_EQ(resourceAllocator->GetInfo().FreeMemoryUsage, kBufferOf4MBAllocationSize * 2);
}

// Exceeding the max resource heap size should always fail.
TEST_F(D3D12ResourceAllocatorTests, CreateBufferOversized) {
ComPtr<ResourceAllocator> resourceAllocator;
Expand Down