diff --git a/src/dawn_native/d3d12/D3D12Error.cpp b/src/dawn_native/d3d12/D3D12Error.cpp index 38bde463f1..2cd46273c5 100644 --- a/src/dawn_native/d3d12/D3D12Error.cpp +++ b/src/dawn_native/d3d12/D3D12Error.cpp @@ -26,4 +26,11 @@ namespace dawn_native { namespace d3d12 { return DAWN_DEVICE_LOST_ERROR(message); } + MaybeError CheckOutOfMemoryHRESULT(HRESULT result, const char* context) { + if (result == E_OUTOFMEMORY) { + return DAWN_OUT_OF_MEMORY_ERROR(context); + } + return CheckHRESULT(result, context); + } + }} // namespace dawn_native::d3d12 \ No newline at end of file diff --git a/src/dawn_native/d3d12/D3D12Error.h b/src/dawn_native/d3d12/D3D12Error.h index b5f5eb7380..ed11a8c7c5 100644 --- a/src/dawn_native/d3d12/D3D12Error.h +++ b/src/dawn_native/d3d12/D3D12Error.h @@ -23,6 +23,9 @@ namespace dawn_native { namespace d3d12 { // Returns a success only if result of HResult is success MaybeError CheckHRESULT(HRESULT result, const char* context); + // Uses CheckRESULT but returns OOM specific error when recoverable. + MaybeError CheckOutOfMemoryHRESULT(HRESULT result, const char* context); + }} // namespace dawn_native::d3d12 #endif // DAWNNATIVE_D3D12_D3D12ERROR_H_ \ No newline at end of file diff --git a/src/dawn_native/d3d12/DescriptorHeapAllocator.cpp b/src/dawn_native/d3d12/DescriptorHeapAllocator.cpp index 19804529cb..facc307b26 100644 --- a/src/dawn_native/d3d12/DescriptorHeapAllocator.cpp +++ b/src/dawn_native/d3d12/DescriptorHeapAllocator.cpp @@ -15,6 +15,7 @@ #include "dawn_native/d3d12/DescriptorHeapAllocator.h" #include "common/Assert.h" +#include "dawn_native/d3d12/D3D12Error.h" #include "dawn_native/d3d12/DeviceD3D12.h" namespace dawn_native { namespace d3d12 { @@ -83,10 +84,9 @@ namespace dawn_native { namespace d3d12 { heapDescriptor.Flags = flags; heapDescriptor.NodeMask = 0; ComPtr heap; - if (FAILED(mDevice->GetD3D12Device()->CreateDescriptorHeap(&heapDescriptor, - IID_PPV_ARGS(&heap)))) { - return DAWN_OUT_OF_MEMORY_ERROR("Unable to allocate heap"); - } + DAWN_TRY(CheckHRESULT( + mDevice->GetD3D12Device()->CreateDescriptorHeap(&heapDescriptor, IID_PPV_ARGS(&heap)), + "ID3D12Device::CreateDescriptorHeap")); mDevice->ReferenceUntilUnused(heap); diff --git a/src/dawn_native/d3d12/HeapAllocatorD3D12.cpp b/src/dawn_native/d3d12/HeapAllocatorD3D12.cpp index 0c89086b11..e16f380110 100644 --- a/src/dawn_native/d3d12/HeapAllocatorD3D12.cpp +++ b/src/dawn_native/d3d12/HeapAllocatorD3D12.cpp @@ -13,6 +13,7 @@ // limitations under the License. #include "dawn_native/d3d12/HeapAllocatorD3D12.h" +#include "dawn_native/d3d12/D3D12Error.h" #include "dawn_native/d3d12/DeviceD3D12.h" #include "dawn_native/d3d12/HeapD3D12.h" @@ -41,9 +42,9 @@ namespace dawn_native { namespace d3d12 { heapDesc.Flags = mHeapFlags; ComPtr heap; - if (FAILED(mDevice->GetD3D12Device()->CreateHeap(&heapDesc, IID_PPV_ARGS(&heap)))) { - return DAWN_OUT_OF_MEMORY_ERROR("Unable to allocate heap"); - } + DAWN_TRY(CheckOutOfMemoryHRESULT( + mDevice->GetD3D12Device()->CreateHeap(&heapDesc, IID_PPV_ARGS(&heap)), + "ID3D12Device::CreateHeap")); return {std::make_unique(std::move(heap))}; } diff --git a/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.cpp b/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.cpp index c7c4fd8da7..15d8d3516f 100644 --- a/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.cpp +++ b/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.cpp @@ -14,6 +14,7 @@ #include "dawn_native/d3d12/ResourceAllocatorManagerD3D12.h" +#include "dawn_native/d3d12/D3D12Error.h" #include "dawn_native/d3d12/DeviceD3D12.h" #include "dawn_native/d3d12/HeapAllocatorD3D12.h" #include "dawn_native/d3d12/HeapD3D12.h" @@ -188,13 +189,10 @@ namespace dawn_native { namespace d3d12 { // barrier). // https://docs.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-id3d12device-createplacedresource ComPtr placedResource; - if (FAILED(mDevice->GetD3D12Device()->CreatePlacedResource( - heap, allocation.GetOffset(), &resourceDescriptor, initialUsage, nullptr, - IID_PPV_ARGS(&placedResource)))) { - // Note: Heap must already exist before the resource is created. If CreatePlacedResource - // fails, it's unlikely to be OOM. - return DAWN_DEVICE_LOST_ERROR("Unable to allocate resource"); - } + DAWN_TRY(CheckOutOfMemoryHRESULT(mDevice->GetD3D12Device()->CreatePlacedResource( + heap, allocation.GetOffset(), &resourceDescriptor, + initialUsage, nullptr, IID_PPV_ARGS(&placedResource)), + "ID3D12Device::CreatePlacedResource")); return ResourceHeapAllocation{allocation.GetInfo(), allocation.GetOffset(), std::move(placedResource)}; @@ -214,11 +212,11 @@ namespace dawn_native { namespace d3d12 { // Note: Heap flags are inferred by the resource descriptor and do not need to be explicitly // provided to CreateCommittedResource. ComPtr committedResource; - if (FAILED(mDevice->GetD3D12Device()->CreateCommittedResource( - &heapProperties, D3D12_HEAP_FLAG_NONE, &resourceDescriptor, initialUsage, nullptr, - IID_PPV_ARGS(&committedResource)))) { - return DAWN_OUT_OF_MEMORY_ERROR("Unable to allocate resource"); - } + DAWN_TRY( + CheckOutOfMemoryHRESULT(mDevice->GetD3D12Device()->CreateCommittedResource( + &heapProperties, D3D12_HEAP_FLAG_NONE, &resourceDescriptor, + initialUsage, nullptr, IID_PPV_ARGS(&committedResource)), + "ID3D12Device::CreateCommittedResource")); AllocationInfo info; info.mMethod = AllocationMethod::kDirect; diff --git a/src/dawn_native/d3d12/StagingBufferD3D12.cpp b/src/dawn_native/d3d12/StagingBufferD3D12.cpp index 8d21484012..c2b2cc1f3c 100644 --- a/src/dawn_native/d3d12/StagingBufferD3D12.cpp +++ b/src/dawn_native/d3d12/StagingBufferD3D12.cpp @@ -13,6 +13,7 @@ // limitations under the License. #include "dawn_native/d3d12/StagingBufferD3D12.h" +#include "dawn_native/d3d12/D3D12Error.h" #include "dawn_native/d3d12/DeviceD3D12.h" namespace dawn_native { namespace d3d12 { @@ -39,11 +40,7 @@ namespace dawn_native { namespace d3d12 { mDevice->AllocateMemory(D3D12_HEAP_TYPE_UPLOAD, resourceDescriptor, D3D12_RESOURCE_STATE_GENERIC_READ)); - if (FAILED(GetResource()->Map(0, nullptr, &mMappedPointer))) { - return DAWN_DEVICE_LOST_ERROR("Unable to map staging buffer."); - } - - return {}; + return CheckHRESULT(GetResource()->Map(0, nullptr, &mMappedPointer), "ID3D12Resource::Map"); } StagingBuffer::~StagingBuffer() {