diff --git a/src/dawn_native/d3d12/BindGroupD3D12.cpp b/src/dawn_native/d3d12/BindGroupD3D12.cpp index e4591ac883..4d27b521bf 100644 --- a/src/dawn_native/d3d12/BindGroupD3D12.cpp +++ b/src/dawn_native/d3d12/BindGroupD3D12.cpp @@ -180,10 +180,10 @@ namespace dawn_native { namespace d3d12 { } d3d12Device->CopyDescriptorsSimple( - viewDescriptorCount, viewDescriptorHeapAllocation.GetCPUHandle(0), - mCPUViewAllocation.OffsetFrom(0, 0), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); + viewDescriptorCount, viewDescriptorHeapAllocation.GetBaseCPUDescriptor(), + mCPUViewAllocation.GetBaseDescriptor(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); - mBaseViewDescriptor = viewDescriptorHeapAllocation.GetGPUHandle(0); + mBaseViewDescriptor = viewDescriptorHeapAllocation.GetBaseGPUDescriptor(); } const uint32_t samplerDescriptorCount = bgl->GetSamplerDescriptorCount(); @@ -197,10 +197,10 @@ namespace dawn_native { namespace d3d12 { } d3d12Device->CopyDescriptorsSimple( - samplerDescriptorCount, samplerDescriptorHeapAllocation.GetCPUHandle(0), - mCPUSamplerAllocation.OffsetFrom(0, 0), D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER); + samplerDescriptorCount, samplerDescriptorHeapAllocation.GetBaseCPUDescriptor(), + mCPUSamplerAllocation.GetBaseDescriptor(), D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER); - mBaseSamplerDescriptor = samplerDescriptorHeapAllocation.GetGPUHandle(0); + mBaseSamplerDescriptor = samplerDescriptorHeapAllocation.GetBaseGPUDescriptor(); } // Record both the device and heap serials to determine later if the allocations are still diff --git a/src/dawn_native/d3d12/CPUDescriptorHeapAllocationD3D12.cpp b/src/dawn_native/d3d12/CPUDescriptorHeapAllocationD3D12.cpp index 635b25ebc7..021c47ab87 100644 --- a/src/dawn_native/d3d12/CPUDescriptorHeapAllocationD3D12.cpp +++ b/src/dawn_native/d3d12/CPUDescriptorHeapAllocationD3D12.cpp @@ -23,6 +23,11 @@ namespace dawn_native { namespace d3d12 { : mBaseDescriptor(baseDescriptor), mHeapIndex(heapIndex) { } + D3D12_CPU_DESCRIPTOR_HANDLE CPUDescriptorHeapAllocation::GetBaseDescriptor() const { + ASSERT(IsValid()); + return mBaseDescriptor; + } + D3D12_CPU_DESCRIPTOR_HANDLE CPUDescriptorHeapAllocation::OffsetFrom( uint32_t sizeIncrementInBytes, uint32_t offsetInDescriptorCount) const { diff --git a/src/dawn_native/d3d12/CPUDescriptorHeapAllocationD3D12.h b/src/dawn_native/d3d12/CPUDescriptorHeapAllocationD3D12.h index 560c99871e..05aaf5185b 100644 --- a/src/dawn_native/d3d12/CPUDescriptorHeapAllocationD3D12.h +++ b/src/dawn_native/d3d12/CPUDescriptorHeapAllocationD3D12.h @@ -27,6 +27,8 @@ namespace dawn_native { namespace d3d12 { CPUDescriptorHeapAllocation() = default; CPUDescriptorHeapAllocation(D3D12_CPU_DESCRIPTOR_HANDLE baseDescriptor, uint32_t heapIndex); + D3D12_CPU_DESCRIPTOR_HANDLE GetBaseDescriptor() const; + D3D12_CPU_DESCRIPTOR_HANDLE OffsetFrom(uint32_t sizeIncrementInBytes, uint32_t offsetInDescriptorCount) const; uint32_t GetHeapIndex() const; diff --git a/src/dawn_native/d3d12/DescriptorHeapAllocationD3D12.cpp b/src/dawn_native/d3d12/DescriptorHeapAllocationD3D12.cpp index fd16f1ef58..2ca7885299 100644 --- a/src/dawn_native/d3d12/DescriptorHeapAllocationD3D12.cpp +++ b/src/dawn_native/d3d12/DescriptorHeapAllocationD3D12.cpp @@ -17,30 +17,21 @@ namespace dawn_native { namespace d3d12 { - DescriptorHeapAllocation::DescriptorHeapAllocation() : mSizeIncrement(0) { - } - DescriptorHeapAllocation::DescriptorHeapAllocation( - uint32_t sizeIncrement, D3D12_CPU_DESCRIPTOR_HANDLE baseCPUDescriptorHandle, D3D12_GPU_DESCRIPTOR_HANDLE baseGPUDescriptorHandle) - : mSizeIncrement(sizeIncrement), - mBaseCPUDescriptorHandle(baseCPUDescriptorHandle), + : mBaseCPUDescriptorHandle(baseCPUDescriptorHandle), mBaseGPUDescriptorHandle(baseGPUDescriptorHandle) { } - D3D12_CPU_DESCRIPTOR_HANDLE DescriptorHeapAllocation::GetCPUHandle(uint32_t offset) const { + D3D12_GPU_DESCRIPTOR_HANDLE DescriptorHeapAllocation::GetBaseGPUDescriptor() const { ASSERT(!IsInvalid()); - D3D12_CPU_DESCRIPTOR_HANDLE cpuHandle = mBaseCPUDescriptorHandle; - cpuHandle.ptr += mSizeIncrement * offset; - return cpuHandle; + return mBaseGPUDescriptorHandle; } - D3D12_GPU_DESCRIPTOR_HANDLE DescriptorHeapAllocation::GetGPUHandle(uint32_t offset) const { + D3D12_CPU_DESCRIPTOR_HANDLE DescriptorHeapAllocation::GetBaseCPUDescriptor() const { ASSERT(!IsInvalid()); - D3D12_GPU_DESCRIPTOR_HANDLE gpuHandle = mBaseGPUDescriptorHandle; - gpuHandle.ptr += mSizeIncrement * offset; - return gpuHandle; + return mBaseCPUDescriptorHandle; } bool DescriptorHeapAllocation::IsInvalid() const { diff --git a/src/dawn_native/d3d12/DescriptorHeapAllocationD3D12.h b/src/dawn_native/d3d12/DescriptorHeapAllocationD3D12.h index 30a034c9cc..e63d415b31 100644 --- a/src/dawn_native/d3d12/DescriptorHeapAllocationD3D12.h +++ b/src/dawn_native/d3d12/DescriptorHeapAllocationD3D12.h @@ -21,23 +21,20 @@ namespace dawn_native { namespace d3d12 { - // Wrapper for a handle into a descriptor heap. + // Wrapper for a handle into a GPU-only descriptor heap. class DescriptorHeapAllocation { public: - DescriptorHeapAllocation(); - DescriptorHeapAllocation(uint32_t sizeIncrement, - D3D12_CPU_DESCRIPTOR_HANDLE baseCPUDescriptorHandle, + DescriptorHeapAllocation() = default; + DescriptorHeapAllocation(D3D12_CPU_DESCRIPTOR_HANDLE baseCPUDescriptorHandle, D3D12_GPU_DESCRIPTOR_HANDLE baseGPUDescriptorHandle); ~DescriptorHeapAllocation() = default; - D3D12_CPU_DESCRIPTOR_HANDLE GetCPUHandle(uint32_t offset) const; - D3D12_GPU_DESCRIPTOR_HANDLE GetGPUHandle(uint32_t offset) const; + D3D12_GPU_DESCRIPTOR_HANDLE GetBaseGPUDescriptor() const; + D3D12_CPU_DESCRIPTOR_HANDLE GetBaseCPUDescriptor() const; bool IsInvalid() const; private: - uint32_t mSizeIncrement; - D3D12_CPU_DESCRIPTOR_HANDLE mBaseCPUDescriptorHandle = {0}; D3D12_GPU_DESCRIPTOR_HANDLE mBaseGPUDescriptorHandle = {0}; }; diff --git a/src/dawn_native/d3d12/ShaderVisibleDescriptorAllocatorD3D12.cpp b/src/dawn_native/d3d12/ShaderVisibleDescriptorAllocatorD3D12.cpp index 101ca4b190..d881f26fce 100644 --- a/src/dawn_native/d3d12/ShaderVisibleDescriptorAllocatorD3D12.cpp +++ b/src/dawn_native/d3d12/ShaderVisibleDescriptorAllocatorD3D12.cpp @@ -101,16 +101,20 @@ namespace dawn_native { namespace d3d12 { ID3D12DescriptorHeap* descriptorHeap = mShaderVisibleBuffers[heapType].heap.Get(); - D3D12_CPU_DESCRIPTOR_HANDLE baseCPUDescriptor = - descriptorHeap->GetCPUDescriptorHandleForHeapStart(); - baseCPUDescriptor.ptr += mSizeIncrements[heapType] * startOffset; + const uint64_t heapOffset = mSizeIncrements[heapType] * startOffset; - D3D12_GPU_DESCRIPTOR_HANDLE baseGPUDescriptor = - descriptorHeap->GetGPUDescriptorHandleForHeapStart(); - baseGPUDescriptor.ptr += mSizeIncrements[heapType] * startOffset; + // Check for 32-bit overflow since CPU heap start handle uses size_t. + const size_t cpuHeapStartPtr = descriptorHeap->GetCPUDescriptorHandleForHeapStart().ptr; - return DescriptorHeapAllocation{mSizeIncrements[heapType], baseCPUDescriptor, - baseGPUDescriptor}; + ASSERT(heapOffset <= std::numeric_limits::max() - cpuHeapStartPtr); + + const D3D12_CPU_DESCRIPTOR_HANDLE baseCPUDescriptor = {cpuHeapStartPtr + + static_cast(heapOffset)}; + + const D3D12_GPU_DESCRIPTOR_HANDLE baseGPUDescriptor = { + descriptorHeap->GetGPUDescriptorHandleForHeapStart().ptr + heapOffset}; + + return DescriptorHeapAllocation{baseCPUDescriptor, baseGPUDescriptor}; } std::array ShaderVisibleDescriptorAllocator::GetShaderVisibleHeaps()