diff --git a/src/gpgmm/d3d12/BufferAllocatorD3D12.cpp b/src/gpgmm/d3d12/BufferAllocatorD3D12.cpp index e526e3a0..aea6a7d9 100644 --- a/src/gpgmm/d3d12/BufferAllocatorD3D12.cpp +++ b/src/gpgmm/d3d12/BufferAllocatorD3D12.cpp @@ -70,8 +70,7 @@ namespace gpgmm::d3d12 { ComPtr resourceHeap; HRESULT hr = mResourceAllocator->CreateCommittedResource( mHeapProperties, mHeapFlags, info, &resourceDescriptor, - /*pOptimizedClearValue*/ nullptr, mInitialResourceState, /*resourceOut*/ nullptr, - &resourceHeap); + /*pOptimizedClearValue*/ nullptr, mInitialResourceState, &resourceHeap); if (FAILED(hr)) { return GetErrorCode(hr); diff --git a/src/gpgmm/d3d12/ResourceAllocationD3D12.cpp b/src/gpgmm/d3d12/ResourceAllocationD3D12.cpp index 6db9a53f..5dc21821 100644 --- a/src/gpgmm/d3d12/ResourceAllocationD3D12.cpp +++ b/src/gpgmm/d3d12/ResourceAllocationD3D12.cpp @@ -43,6 +43,31 @@ namespace gpgmm::d3d12 { } // namespace + // static + HRESULT ResourceAllocation::CreateResourceAllocation( + const RESOURCE_RESOURCE_ALLOCATION_DESC& descriptor, + ResourceAllocator* pResourceAllocator, + MemoryAllocatorBase* pHeapAllocator, + ResidencyHeap* pResourceHeap, + MemoryBlock* pMemoryBlock, + ComPtr resource, + ResourceAllocation** ppResourceAllocationOut) { + // If no resource was specified it must be implicitly created. + if (resource == nullptr) { + pResourceHeap->QueryInterface(IID_PPV_ARGS(&resource)); + } + + if (ppResourceAllocationOut != nullptr) { + *ppResourceAllocationOut = + new ResourceAllocation(descriptor, pResourceAllocator, pHeapAllocator, + pResourceHeap, pMemoryBlock, resource); + } else { + return S_FALSE; + } + + return S_OK; + } + ResourceAllocation::ResourceAllocation(const RESOURCE_RESOURCE_ALLOCATION_DESC& desc, ResourceAllocator* resourceAllocator, MemoryAllocatorBase* allocator, @@ -58,6 +83,8 @@ namespace gpgmm::d3d12 { mResourceAllocator(std::move(resourceAllocator)), mResource(std::move(resource)), mOffsetFromResource(desc.OffsetFromResource) { + ASSERT(allocator != nullptr); + ASSERT(mResourceAllocator != nullptr); ASSERT(resourceHeap != nullptr); GPGMM_TRACE_EVENT_OBJECT_NEW(this); } diff --git a/src/gpgmm/d3d12/ResourceAllocationD3D12.h b/src/gpgmm/d3d12/ResourceAllocationD3D12.h index e4b64cf6..d20a2c11 100644 --- a/src/gpgmm/d3d12/ResourceAllocationD3D12.h +++ b/src/gpgmm/d3d12/ResourceAllocationD3D12.h @@ -40,6 +40,14 @@ namespace gpgmm::d3d12 { public DebugObject, public IResourceAllocation { public: + static HRESULT CreateResourceAllocation(const RESOURCE_RESOURCE_ALLOCATION_DESC& descriptor, + ResourceAllocator* pResourceAllocator, + MemoryAllocatorBase* pHeapAllocator, + ResidencyHeap* pResourceHeap, + MemoryBlock* pMemoryBlock, + ComPtr resource, + ResourceAllocation** ppResourceAllocationOut); + ~ResourceAllocation() override; // IResourceAllocation interface diff --git a/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp b/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp index cfa8ce3f..b805c4a9 100644 --- a/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp +++ b/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp @@ -1325,14 +1325,6 @@ namespace gpgmm::d3d12 { GPGMM_RETURN_IF_NOT_FATAL(TryAllocateResource( allocator, subAllocWithinRequest, [&](const auto& subAllocation) -> HRESULT { - // Committed resource implicitly creates a resource heap which can be - // used for sub-allocation. - ComPtr committedResource; - ResidencyHeap* resourceHeap = - static_cast(subAllocation.GetMemory()); - GPGMM_RETURN_IF_FAILED( - resourceHeap->QueryInterface(IID_PPV_ARGS(&committedResource)), mDevice); - RESOURCE_RESOURCE_ALLOCATION_DESC allocationDesc = {}; allocationDesc.SizeInBytes = newResourceDesc.Width; allocationDesc.HeapOffset = kInvalidOffset; @@ -1340,9 +1332,15 @@ namespace gpgmm::d3d12 { allocationDesc.OffsetFromResource = subAllocation.GetOffset(); allocationDesc.DebugName = allocationDescriptor.DebugName; - *ppResourceAllocationOut = new ResourceAllocation( - allocationDesc, /*resourceAllocator*/ this, subAllocation.GetAllocator(), - resourceHeap, subAllocation.GetBlock(), std::move(committedResource)); + ResidencyHeap* resourceHeap = + static_cast(subAllocation.GetMemory()); + + GPGMM_RETURN_IF_FAILED( + ResourceAllocation::CreateResourceAllocation( + allocationDesc, /*resourceAllocator*/ this, + subAllocation.GetAllocator(), resourceHeap, subAllocation.GetBlock(), + nullptr, ppResourceAllocationOut), + mDevice); return S_OK; })); @@ -1381,9 +1379,12 @@ namespace gpgmm::d3d12 { allocationDesc.OffsetFromResource = 0; allocationDesc.DebugName = allocationDescriptor.DebugName; - *ppResourceAllocationOut = new ResourceAllocation( - allocationDesc, /*resourceAllocator*/ this, subAllocation.GetAllocator(), - resourceHeap, subAllocation.GetBlock(), std::move(placedResource)); + GPGMM_RETURN_IF_FAILED( + ResourceAllocation::CreateResourceAllocation( + allocationDesc, /*resourceAllocator*/ this, + subAllocation.GetAllocator(), resourceHeap, subAllocation.GetBlock(), + std::move(placedResource), ppResourceAllocationOut), + mDevice); return S_OK; })); @@ -1425,9 +1426,12 @@ namespace gpgmm::d3d12 { allocationDesc.OffsetFromResource = 0; allocationDesc.DebugName = allocationDescriptor.DebugName; - *ppResourceAllocationOut = new ResourceAllocation( - allocationDesc, /*resourceAllocator*/ this, allocation.GetAllocator(), - resourceHeap, allocation.GetBlock(), std::move(placedResource)); + GPGMM_RETURN_IF_FAILED( + ResourceAllocation::CreateResourceAllocation( + allocationDesc, /*resourceAllocator*/ this, allocation.GetAllocator(), + resourceHeap, allocation.GetBlock(), std::move(placedResource), + ppResourceAllocationOut), + mDevice); return S_OK; })); @@ -1457,11 +1461,10 @@ namespace gpgmm::d3d12 { "be created and RESOURCE_ALLOCATION_FLAG_NEVER_FALLBACK was specified.", ErrorCode::kAllocationFailed); - ComPtr committedResource; ComPtr resourceHeap; if (FAILED(CreateCommittedResource(heapProperties, heapFlags, resourceInfo, &newResourceDesc, clearValue, initialResourceState, - &committedResource, &resourceHeap))) { + &resourceHeap))) { return ErrorCode::kAllocationFailed; } @@ -1485,10 +1488,10 @@ namespace gpgmm::d3d12 { allocationDesc.Type = RESOURCE_ALLOCATION_TYPE_STANDALONE; allocationDesc.DebugName = allocationDescriptor.DebugName; - if (ppResourceAllocationOut != nullptr) { - *ppResourceAllocationOut = - new ResourceAllocation(allocationDesc, this, this, resourceHeap.Detach(), nullptr, - std::move(committedResource)); + if (FAILED(ResourceAllocation::CreateResourceAllocation( + allocationDesc, this, this, resourceHeap.Detach(), nullptr, nullptr, + ppResourceAllocationOut))) { + return ErrorCode::kAllocationFailed; } return ErrorCode::kNone; @@ -1618,7 +1621,6 @@ namespace gpgmm::d3d12 { const D3D12_RESOURCE_DESC* resourceDescriptor, const D3D12_CLEAR_VALUE* clearValue, D3D12_RESOURCE_STATES initialResourceState, - ID3D12Resource** committedResourceOut, ResidencyHeap** resourceHeapOut) { GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, "ResourceAllocator.CreateCommittedResource"); @@ -1646,13 +1648,6 @@ namespace gpgmm::d3d12 { &callbackContext, &resourceHeap), mDevice); - if (committedResourceOut != nullptr) { - ComPtr committedResource; - GPGMM_RETURN_IF_FAILED(resourceHeap.As(&committedResource), mDevice); - - *committedResourceOut = committedResource.Detach(); - } - if (resourceHeapOut != nullptr) { *resourceHeapOut = static_cast(resourceHeap.Detach()); } diff --git a/src/gpgmm/d3d12/ResourceAllocatorD3D12.h b/src/gpgmm/d3d12/ResourceAllocatorD3D12.h index 911b563d..973b2a04 100644 --- a/src/gpgmm/d3d12/ResourceAllocatorD3D12.h +++ b/src/gpgmm/d3d12/ResourceAllocatorD3D12.h @@ -131,7 +131,6 @@ namespace gpgmm::d3d12 { const D3D12_RESOURCE_DESC* resourceDescriptor, const D3D12_CLEAR_VALUE* clearValue, D3D12_RESOURCE_STATES initialResourceState, - ID3D12Resource** committedResourceOut, ResidencyHeap** resourceHeapOut); HRESULT ReleaseResourceHeapsInternal(uint64_t bytesToRelease, uint64_t* pBytesReleased);