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
39 changes: 30 additions & 9 deletions src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,22 @@ namespace gpgmm::d3d12 {
}
}

HRESULT GetHeapType(D3D12_RESOURCE_STATES initialResourceState, D3D12_HEAP_TYPE* heapType) {
if (HasAllFlags(GetInitialResourceState(D3D12_HEAP_TYPE_UPLOAD),
initialResourceState)) {
*heapType = D3D12_HEAP_TYPE_UPLOAD;
return S_OK;
}

if (HasAllFlags(GetInitialResourceState(D3D12_HEAP_TYPE_READBACK),
initialResourceState)) {
*heapType = D3D12_HEAP_TYPE_READBACK;
return S_OK;
}

return E_UNEXPECTED;
}

// RAII wrapper to lock/unlock heap from the residency cache.
class ScopedResidencyLock final : public NonCopyable {
public:
Expand Down Expand Up @@ -761,9 +777,14 @@ namespace gpgmm::d3d12 {
return E_OUTOFMEMORY;
}

const RESOURCE_HEAP_TYPE resourceHeapType =
GetResourceHeapType(newResourceDesc.Dimension, allocationDescriptor.HeapType,
newResourceDesc.Flags, mResourceHeapTier);
// If the heap type was not specified, infer it using the initial resource state.
D3D12_HEAP_TYPE heapType = allocationDescriptor.HeapType;
if (heapType == 0) {
ReturnIfFailed(GetHeapType(initialResourceState, &heapType));
}

const RESOURCE_HEAP_TYPE resourceHeapType = GetResourceHeapType(
newResourceDesc.Dimension, heapType, newResourceDesc.Flags, mResourceHeapTier);
if (resourceHeapType == RESOURCE_HEAP_TYPE_INVALID) {
return E_INVALIDARG;
}
Expand Down Expand Up @@ -824,7 +845,7 @@ namespace gpgmm::d3d12 {
// Limit available memory to unused budget when residency is enabled.
if (mResidencyManager != nullptr) {
const DXGI_MEMORY_SEGMENT_GROUP segment =
mResidencyManager->GetMemorySegmentGroup(allocationDescriptor.HeapType);
mResidencyManager->GetMemorySegmentGroup(heapType);
DXGI_QUERY_VIDEO_MEMORY_INFO* currentVideoInfo =
mResidencyManager->GetVideoMemoryInfo(segment);

Expand Down Expand Up @@ -853,8 +874,8 @@ namespace gpgmm::d3d12 {
if (allocationDescriptor.Flags & ALLOCATION_FLAG_ALLOW_SUBALLOCATE_WITHIN_RESOURCE &&
resourceInfo.Alignment > newResourceDesc.Width &&
newResourceDesc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER &&
GetInitialResourceState(allocationDescriptor.HeapType) == initialResourceState &&
!isAlwaysCommitted && !neverSubAllocate) {
GetInitialResourceState(heapType) == initialResourceState && !isAlwaysCommitted &&
!neverSubAllocate) {
allocator = mSmallBufferAllocatorOfType[static_cast<size_t>(resourceHeapType)].get();

// GetResourceAllocationInfo() always rejects alignments smaller than 64KB. So if the
Expand Down Expand Up @@ -995,9 +1016,9 @@ namespace gpgmm::d3d12 {

ComPtr<ID3D12Resource> committedResource;
Heap* resourceHeap = nullptr;
ReturnIfFailed(CreateCommittedResource(
allocationDescriptor.HeapType, heapFlags, resourceInfo, &newResourceDesc, clearValue,
initialResourceState, &committedResource, &resourceHeap));
ReturnIfFailed(CreateCommittedResource(heapType, heapFlags, resourceInfo, &newResourceDesc,
clearValue, initialResourceState, &committedResource,
&resourceHeap));

// Using committed resources will create a tightly allocated resource allocations.
// This means the block and heap size should be equal (modulo driver padding).
Expand Down
3 changes: 2 additions & 1 deletion src/gpgmm/d3d12/ResourceAllocatorD3D12.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ namespace gpgmm::d3d12 {

/** \brief Heap type that the resource to be allocated requires.

Required parameter.
Optional parameter. If the heap type is not provided, the heap type will be inferred by the
parameters used to call CreateResource.
*/
D3D12_HEAP_TYPE HeapType;

Expand Down
22 changes: 22 additions & 0 deletions src/tests/end2end/D3D12ResourceAllocatorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,28 @@ TEST_F(D3D12ResourceAllocatorTests, CreateBuffer) {
ASSERT_NE(allocation, nullptr);
EXPECT_EQ(allocation->GetDebugName(), allocationDesc.DebugName);
}

// Creating a buffer without a heap type should be inferred based on the resource state.
{
ComPtr<ResourceAllocation> allocation;
ASSERT_SUCCEEDED(resourceAllocator->CreateResource(
{}, CreateBasicBufferDesc(kDefaultBufferSize), D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
&allocation));
ASSERT_NE(allocation, nullptr);
}
{
ComPtr<ResourceAllocation> allocation;
ASSERT_SUCCEEDED(resourceAllocator->CreateResource(
{}, CreateBasicBufferDesc(kDefaultBufferSize), D3D12_RESOURCE_STATE_GENERIC_READ,
nullptr, &allocation));
ASSERT_NE(allocation, nullptr);
}
{
ComPtr<ResourceAllocation> allocation;
ASSERT_FAILED(resourceAllocator->CreateResource(
{}, CreateBasicBufferDesc(kDefaultBufferSize), D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
nullptr, &allocation));
}
}

TEST_F(D3D12ResourceAllocatorTests, CreateSmallTexture) {
Expand Down