From 6f194b0e7b52f0999f301b16041b8cb83231fb85 Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Thu, 12 Jan 2023 18:07:52 +0100 Subject: [PATCH] [d3d11] Bind UAV counter buffers as views --- src/d3d11/d3d11_context.cpp | 27 +++++++++++++++------------ src/d3d11/d3d11_initializer.cpp | 11 +++++++---- src/d3d11/d3d11_view_uav.cpp | 14 +++++++++++--- src/d3d11/d3d11_view_uav.h | 10 ++++------ 4 files changed, 37 insertions(+), 25 deletions(-) diff --git a/src/d3d11/d3d11_context.cpp b/src/d3d11/d3d11_context.cpp index 51c15489..5a4734fc 100644 --- a/src/d3d11/d3d11_context.cpp +++ b/src/d3d11/d3d11_context.cpp @@ -329,13 +329,14 @@ namespace dxvk { if (!buf || !uav) return; - auto counterSlice = uav->GetCounterSlice(); - if (!counterSlice.defined()) + auto counterView = uav->GetCounterView(); + + if (counterView == nullptr) return; EmitCs([ cDstSlice = buf->GetBufferSlice(DstAlignedByteOffset), - cSrcSlice = std::move(counterSlice) + cSrcSlice = counterView->slice() ] (DxvkContext* ctx) { ctx->copyBuffer( cDstSlice.buffer(), @@ -3702,25 +3703,27 @@ namespace dxvk { cUavSlotId = UavSlot, cCtrSlotId = CtrSlot, cBufferView = pUav->GetBufferView(), - cCounterSlice = pUav->GetCounterSlice(), + cCounterView = pUav->GetCounterView(), cCounterValue = Counter ] (DxvkContext* ctx) mutable { VkShaderStageFlags stages = ShaderStage == DxbcProgramType::ComputeShader ? VK_SHADER_STAGE_COMPUTE_BIT : VK_SHADER_STAGE_ALL_GRAPHICS; - if (cCounterSlice.defined() && cCounterValue != ~0u) { + if (cCounterView != nullptr && cCounterValue != ~0u) { + auto counterSlice = cCounterView->slice(); + ctx->updateBuffer( - cCounterSlice.buffer(), - cCounterSlice.offset(), + counterSlice.buffer(), + counterSlice.offset(), sizeof(uint32_t), &cCounterValue); } ctx->bindResourceBufferView(stages, cUavSlotId, Forwarder::move(cBufferView)); - ctx->bindResourceBuffer(stages, cCtrSlotId, - Forwarder::move(cCounterSlice)); + ctx->bindResourceBufferView(stages, cCtrSlotId, + Forwarder::move(cCounterView)); }); } else { EmitCs([ @@ -3734,7 +3737,7 @@ namespace dxvk { ctx->bindResourceImageView(stages, cUavSlotId, Forwarder::move(cImageView)); - ctx->bindResourceBuffer(stages, cCtrSlotId, DxvkBufferSlice()); + ctx->bindResourceBufferView(stages, cCtrSlotId, nullptr); }); } } else { @@ -3747,7 +3750,7 @@ namespace dxvk { : VK_SHADER_STAGE_ALL_GRAPHICS; ctx->bindResourceImageView(stages, cUavSlotId, nullptr); - ctx->bindResourceBuffer(stages, cCtrSlotId, DxvkBufferSlice()); + ctx->bindResourceBufferView(stages, cCtrSlotId, nullptr); }); } } @@ -4416,7 +4419,7 @@ namespace dxvk { for (uint32_t j = 0; j < cUsedBindings.stages[i].uavCount; j++) { ctx->bindResourceImageView(stages, uavSlotId, nullptr); - ctx->bindResourceBuffer(stages, ctrSlotId, DxvkBufferSlice()); + ctx->bindResourceBufferView(stages, ctrSlotId, nullptr); } } } diff --git a/src/d3d11/d3d11_initializer.cpp b/src/d3d11/d3d11_initializer.cpp index a5391adb..44c16e56 100644 --- a/src/d3d11/d3d11_initializer.cpp +++ b/src/d3d11/d3d11_initializer.cpp @@ -54,18 +54,21 @@ namespace dxvk { void D3D11Initializer::InitUavCounter( D3D11UnorderedAccessView* pUav) { - auto counterBuffer = pUav->GetCounterSlice(); + auto counterView = pUav->GetCounterView(); - if (!counterBuffer.defined()) + if (counterView == nullptr) return; + auto counterSlice = counterView->slice(); + std::lock_guard lock(m_mutex); m_transferCommands += 1; const uint32_t zero = 0; m_context->updateBuffer( - counterBuffer.buffer(), - 0, sizeof(zero), &zero); + counterSlice.buffer(), + counterSlice.offset(), + sizeof(zero), &zero); FlushImplicit(); } diff --git a/src/d3d11/d3d11_view_uav.cpp b/src/d3d11/d3d11_view_uav.cpp index 51a7cedd..f21fe053 100644 --- a/src/d3d11/d3d11_view_uav.cpp +++ b/src/d3d11/d3d11_view_uav.cpp @@ -44,7 +44,7 @@ namespace dxvk { } if (pDesc->Buffer.Flags & (D3D11_BUFFER_UAV_FLAG_APPEND | D3D11_BUFFER_UAV_FLAG_COUNTER)) - m_counterBuffer = CreateCounterBuffer(); + m_counterView = CreateCounterBufferView(); // Populate view info struct m_info.Buffer.Offset = viewInfo.rangeOffset; @@ -429,7 +429,7 @@ namespace dxvk { } - Rc D3D11UnorderedAccessView::CreateCounterBuffer() { + Rc D3D11UnorderedAccessView::CreateCounterBufferView() { Rc device = m_parent->GetDXVKDevice(); DxvkBufferCreateInfo info; @@ -443,7 +443,15 @@ namespace dxvk { | VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT | VK_ACCESS_SHADER_READ_BIT; - return device->createBuffer(info, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); + + Rc buffer = device->createBuffer(info, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); + + DxvkBufferViewCreateInfo viewInfo; + viewInfo.format = VK_FORMAT_UNDEFINED; + viewInfo.rangeOffset = 0; + viewInfo.rangeLength = sizeof(uint32_t); + + return device->createBufferView(buffer, viewInfo); } } diff --git a/src/d3d11/d3d11_view_uav.h b/src/d3d11/d3d11_view_uav.h index 3076fe09..c86eba5f 100644 --- a/src/d3d11/d3d11_view_uav.h +++ b/src/d3d11/d3d11_view_uav.h @@ -57,10 +57,8 @@ namespace dxvk { return m_imageView; } - DxvkBufferSlice GetCounterSlice() const { - return m_counterBuffer != nullptr - ? DxvkBufferSlice(m_counterBuffer) - : DxvkBufferSlice(); + Rc GetCounterView() const { + return m_counterView; } static HRESULT GetDescFromResource( @@ -85,9 +83,9 @@ namespace dxvk { D3D11_VK_VIEW_INFO m_info; Rc m_bufferView; Rc m_imageView; - Rc m_counterBuffer; + Rc m_counterView; - Rc CreateCounterBuffer(); + Rc CreateCounterBufferView(); };