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
3 changes: 1 addition & 2 deletions src/gpgmm/d3d12/BufferAllocatorD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ namespace gpgmm::d3d12 {
ComPtr<ResidencyHeap> 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);
Expand Down
27 changes: 27 additions & 0 deletions src/gpgmm/d3d12/ResourceAllocationD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ID3D12Resource> 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,
Expand All @@ -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);
}
Expand Down
8 changes: 8 additions & 0 deletions src/gpgmm/d3d12/ResourceAllocationD3D12.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<ID3D12Resource> resource,
ResourceAllocation** ppResourceAllocationOut);

~ResourceAllocation() override;

// IResourceAllocation interface
Expand Down
57 changes: 26 additions & 31 deletions src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1325,24 +1325,22 @@ 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<ID3D12Resource> committedResource;
ResidencyHeap* resourceHeap =
static_cast<ResidencyHeap*>(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;
allocationDesc.Type = RESOURCE_ALLOCATION_TYPE_SUBALLOCATED_WITHIN;
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<ResidencyHeap*>(subAllocation.GetMemory());

GPGMM_RETURN_IF_FAILED(
ResourceAllocation::CreateResourceAllocation(
allocationDesc, /*resourceAllocator*/ this,
subAllocation.GetAllocator(), resourceHeap, subAllocation.GetBlock(),
nullptr, ppResourceAllocationOut),
mDevice);

return S_OK;
}));
Expand Down Expand Up @@ -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;
}));
Expand Down Expand Up @@ -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;
}));
Expand Down Expand Up @@ -1457,11 +1461,10 @@ namespace gpgmm::d3d12 {
"be created and RESOURCE_ALLOCATION_FLAG_NEVER_FALLBACK was specified.",
ErrorCode::kAllocationFailed);

ComPtr<ID3D12Resource> committedResource;
ComPtr<ResidencyHeap> resourceHeap;
if (FAILED(CreateCommittedResource(heapProperties, heapFlags, resourceInfo,
&newResourceDesc, clearValue, initialResourceState,
&committedResource, &resourceHeap))) {
&resourceHeap))) {
return ErrorCode::kAllocationFailed;
}

Expand All @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -1646,13 +1648,6 @@ namespace gpgmm::d3d12 {
&callbackContext, &resourceHeap),
mDevice);

if (committedResourceOut != nullptr) {
ComPtr<ID3D12Resource> committedResource;
GPGMM_RETURN_IF_FAILED(resourceHeap.As(&committedResource), mDevice);

*committedResourceOut = committedResource.Detach();
}

if (resourceHeapOut != nullptr) {
*resourceHeapOut = static_cast<ResidencyHeap*>(resourceHeap.Detach());
}
Expand Down
1 change: 0 additions & 1 deletion src/gpgmm/d3d12/ResourceAllocatorD3D12.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down