From 0542f198f54e60b0cb885d3f8d12af66e7805eab Mon Sep 17 00:00:00 2001 From: "Bernhart, Bryan" Date: Wed, 22 Feb 2023 11:16:48 -0800 Subject: [PATCH] Handle device errors for API calls. --- src/gpgmm/d3d12/ErrorD3D12.h | 20 +++++++++++--------- src/gpgmm/d3d12/ResidencyManagerD3D12.cpp | 22 +++++++++++++--------- src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp | 22 ++++++++++++---------- 3 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/gpgmm/d3d12/ErrorD3D12.h b/src/gpgmm/d3d12/ErrorD3D12.h index 8187dc8c4..03bd7dfce 100644 --- a/src/gpgmm/d3d12/ErrorD3D12.h +++ b/src/gpgmm/d3d12/ErrorD3D12.h @@ -33,15 +33,17 @@ namespace gpgmm::d3d12 { for (;;) \ break -#define ReturnIfFailed(expr) \ - { \ - HRESULT hr = expr; \ - if (GPGMM_UNLIKELY(FAILED(hr))) { \ - gpgmm::ErrorLog() << #expr << ": " << GetErrorMessage(hr); \ - return hr; \ - } \ - } \ - for (;;) \ +#define ReturnIfFailed(expr) ReturnIfFailedDevice(expr, nullptr) + +#define ReturnIfFailedDevice(expr, device) \ + { \ + HRESULT hr = expr; \ + if (GPGMM_UNLIKELY(FAILED(hr))) { \ + gpgmm::ErrorLog() << #expr << ": " << GetDeviceErrorMessage(device, hr); \ + return hr; \ + } \ + } \ + for (;;) \ break #define ReturnIfSucceeded(expr) \ diff --git a/src/gpgmm/d3d12/ResidencyManagerD3D12.cpp b/src/gpgmm/d3d12/ResidencyManagerD3D12.cpp index e12a000c1..29afd8134 100644 --- a/src/gpgmm/d3d12/ResidencyManagerD3D12.cpp +++ b/src/gpgmm/d3d12/ResidencyManagerD3D12.cpp @@ -308,8 +308,9 @@ namespace gpgmm::d3d12 { if (!heap->IsInList() && !heap->IsResidencyLocked()) { ComPtr pageable; ReturnIfFailed(heap->QueryInterface(IID_PPV_ARGS(&pageable))); - ReturnIfFailed(MakeResident(heap->GetMemorySegmentGroup(), heap->GetSize(), 1, - pageable.GetAddressOf())); + ReturnIfFailedDevice(MakeResident(heap->GetMemorySegmentGroup(), heap->GetSize(), 1, + pageable.GetAddressOf()), + mDevice); heap->SetResidencyState(RESIDENCY_STATUS_CURRENT_RESIDENT); // Untracked heaps, created not resident, are not already attributed toward residency @@ -573,7 +574,8 @@ namespace gpgmm::d3d12 { const DXGI_MEMORY_SEGMENT_GROUP& memorySegmentGroup) { std::lock_guard lock(mMutex); uint64_t bytesEvicted = bytesInBudget; - ReturnIfFailed(EvictInternal(bytesInBudget, memorySegmentGroup, &bytesEvicted)); + ReturnIfFailedDevice(EvictInternal(bytesInBudget, memorySegmentGroup, &bytesEvicted), + mDevice); return (bytesEvicted >= bytesInBudget) ? S_OK : E_FAIL; } @@ -769,15 +771,17 @@ namespace gpgmm::d3d12 { if (localSizeToMakeResident > 0) { const uint32_t numberOfObjectsToMakeResident = static_cast(localHeapsToMakeResident.size()); - ReturnIfFailed(MakeResident(DXGI_MEMORY_SEGMENT_GROUP_LOCAL, localSizeToMakeResident, - numberOfObjectsToMakeResident, - localHeapsToMakeResident.data())); + ReturnIfFailedDevice( + MakeResident(DXGI_MEMORY_SEGMENT_GROUP_LOCAL, localSizeToMakeResident, + numberOfObjectsToMakeResident, localHeapsToMakeResident.data()), + mDevice); } else if (nonLocalSizeToMakeResident > 0) { const uint32_t numberOfObjectsToMakeResident = static_cast(nonLocalHeapsToMakeResident.size()); - ReturnIfFailed(MakeResident(DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL, - nonLocalSizeToMakeResident, numberOfObjectsToMakeResident, - nonLocalHeapsToMakeResident.data())); + ReturnIfFailedDevice( + MakeResident(DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL, nonLocalSizeToMakeResident, + numberOfObjectsToMakeResident, nonLocalHeapsToMakeResident.data()), + mDevice); } // Once MakeResident succeeds, we must assume the heaps are resident since D3D12 provides diff --git a/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp b/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp index 3718b019a..32c01b538 100644 --- a/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp +++ b/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp @@ -322,8 +322,7 @@ namespace gpgmm::d3d12 { HRESULT hr = createResourceFn(*allocation); if (FAILED(hr)) { InfoEvent(allocator, MessageId::kAllocatorFailed) - << "Failed to create resource using allocation: " + - GetDeviceErrorMessage(device, hr); + << "Failed to create resource using allocation."; allocator->DeallocateMemory(std::move(allocation)); } return hr; @@ -896,8 +895,9 @@ namespace gpgmm::d3d12 { std::lock_guard lock(mMutex); ComPtr allocation; - ReturnIfFailed(CreateResourceInternal(allocationDescriptor, resourceDescriptor, - initialResourceState, pClearValue, &allocation)); + ReturnIfFailedDevice(CreateResourceInternal(allocationDescriptor, resourceDescriptor, + initialResourceState, pClearValue, &allocation), + mDevice); ASSERT(allocation->GetResource() != nullptr); @@ -1338,12 +1338,14 @@ namespace gpgmm::d3d12 { ImportResourceCallbackContext importResourceCallbackContext(pCommittedResource); ComPtr resourceHeap; - ReturnIfFailed(Heap::CreateHeap( - resourceHeapDesc, - (allocationDescriptor.Flags & ALLOCATION_FLAG_DISABLE_RESIDENCY) - ? nullptr - : mResidencyManager.Get(), - ImportResourceCallbackContext::GetHeap, &importResourceCallbackContext, &resourceHeap)); + ReturnIfFailedDevice( + Heap::CreateHeap(resourceHeapDesc, + (allocationDescriptor.Flags & ALLOCATION_FLAG_DISABLE_RESIDENCY) + ? nullptr + : mResidencyManager.Get(), + ImportResourceCallbackContext::GetHeap, &importResourceCallbackContext, + &resourceHeap), + mDevice); const uint64_t& allocationSize = resourceInfo.SizeInBytes; mStats.UsedMemoryUsage += allocationSize;