From 1ef9d5389bf547e4e47e38082e5e88aec9d858e0 Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Wed, 3 Aug 2022 20:27:33 +0200 Subject: [PATCH] [d3d11] Move ClearState and RestoreState to D3D11CommonContext --- src/d3d11/d3d11_context.cpp | 333 ---------------------------- src/d3d11/d3d11_context.h | 22 -- src/d3d11/d3d11_context_common.cpp | 340 +++++++++++++++++++++++++++++ src/d3d11/d3d11_context_common.h | 22 ++ 4 files changed, 362 insertions(+), 355 deletions(-) diff --git a/src/d3d11/d3d11_context.cpp b/src/d3d11/d3d11_context.cpp index 39f2b197..b79506c1 100644 --- a/src/d3d11/d3d11_context.cpp +++ b/src/d3d11/d3d11_context.cpp @@ -104,122 +104,6 @@ namespace dxvk { } - void STDMETHODCALLTYPE D3D11DeviceContext::ClearState() { - D3D10DeviceLock lock = LockContext(); - - // Default shaders - m_state.vs.shader = nullptr; - m_state.hs.shader = nullptr; - m_state.ds.shader = nullptr; - m_state.gs.shader = nullptr; - m_state.ps.shader = nullptr; - m_state.cs.shader = nullptr; - - // Default constant buffers - for (uint32_t i = 0; i < D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT; i++) { - m_state.vs.constantBuffers[i] = { nullptr, 0, 0 }; - m_state.hs.constantBuffers[i] = { nullptr, 0, 0 }; - m_state.ds.constantBuffers[i] = { nullptr, 0, 0 }; - m_state.gs.constantBuffers[i] = { nullptr, 0, 0 }; - m_state.ps.constantBuffers[i] = { nullptr, 0, 0 }; - m_state.cs.constantBuffers[i] = { nullptr, 0, 0 }; - } - - // Default samplers - for (uint32_t i = 0; i < D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT; i++) { - m_state.vs.samplers[i] = nullptr; - m_state.hs.samplers[i] = nullptr; - m_state.ds.samplers[i] = nullptr; - m_state.gs.samplers[i] = nullptr; - m_state.ps.samplers[i] = nullptr; - m_state.cs.samplers[i] = nullptr; - } - - // Default shader resources - for (uint32_t i = 0; i < D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT; i++) { - m_state.vs.shaderResources.views[i] = nullptr; - m_state.hs.shaderResources.views[i] = nullptr; - m_state.ds.shaderResources.views[i] = nullptr; - m_state.gs.shaderResources.views[i] = nullptr; - m_state.ps.shaderResources.views[i] = nullptr; - m_state.cs.shaderResources.views[i] = nullptr; - } - - m_state.vs.shaderResources.hazardous.clear(); - m_state.hs.shaderResources.hazardous.clear(); - m_state.ds.shaderResources.hazardous.clear(); - m_state.gs.shaderResources.hazardous.clear(); - m_state.ps.shaderResources.hazardous.clear(); - m_state.cs.shaderResources.hazardous.clear(); - - // Default UAVs - for (uint32_t i = 0; i < D3D11_1_UAV_SLOT_COUNT; i++) { - m_state.ps.unorderedAccessViews[i] = nullptr; - m_state.cs.unorderedAccessViews[i] = nullptr; - } - - m_state.cs.uavMask.clear(); - - // Default ID state - m_state.id.argBuffer = nullptr; - - // Default IA state - m_state.ia.inputLayout = nullptr; - m_state.ia.primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED; - - for (uint32_t i = 0; i < D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT; i++) { - m_state.ia.vertexBuffers[i].buffer = nullptr; - m_state.ia.vertexBuffers[i].offset = 0; - m_state.ia.vertexBuffers[i].stride = 0; - } - - m_state.ia.indexBuffer.buffer = nullptr; - m_state.ia.indexBuffer.offset = 0; - m_state.ia.indexBuffer.format = DXGI_FORMAT_UNKNOWN; - - // Default OM State - for (uint32_t i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++) - m_state.om.renderTargetViews[i] = nullptr; - m_state.om.depthStencilView = nullptr; - - m_state.om.cbState = nullptr; - m_state.om.dsState = nullptr; - - for (uint32_t i = 0; i < 4; i++) - m_state.om.blendFactor[i] = 1.0f; - - m_state.om.sampleCount = 0; - m_state.om.sampleMask = D3D11_DEFAULT_SAMPLE_MASK; - m_state.om.stencilRef = D3D11_DEFAULT_STENCIL_REFERENCE; - - m_state.om.maxRtv = 0; - m_state.om.maxUav = 0; - - // Default RS state - m_state.rs.state = nullptr; - m_state.rs.numViewports = 0; - m_state.rs.numScissors = 0; - - for (uint32_t i = 0; i < D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE; i++) { - m_state.rs.viewports[i] = D3D11_VIEWPORT { }; - m_state.rs.scissors [i] = D3D11_RECT { }; - } - - // Default SO state - for (uint32_t i = 0; i < D3D11_SO_BUFFER_SLOT_COUNT; i++) { - m_state.so.targets[i].buffer = nullptr; - m_state.so.targets[i].offset = 0; - } - - // Default predication - m_state.pr.predicateObject = nullptr; - m_state.pr.predicateValue = FALSE; - - // Make sure to apply all state - ResetState(); - } - - void STDMETHODCALLTYPE D3D11DeviceContext::SetPredication( ID3D11Predicate* pPredicate, BOOL PredicateValue) { @@ -2580,223 +2464,6 @@ namespace dxvk { } - void D3D11DeviceContext::ResetState() { - EmitCs([] (DxvkContext* ctx) { - // Reset render targets - ctx->bindRenderTargets(DxvkRenderTargets()); - - // Reset vertex input state - ctx->setInputLayout(0, nullptr, 0, nullptr); - - // Reset render states - DxvkInputAssemblyState iaState; - InitDefaultPrimitiveTopology(&iaState); - - DxvkDepthStencilState dsState; - InitDefaultDepthStencilState(&dsState); - - DxvkRasterizerState rsState; - InitDefaultRasterizerState(&rsState); - - DxvkBlendMode cbState; - DxvkLogicOpState loState; - DxvkMultisampleState msState; - InitDefaultBlendState(&cbState, &loState, &msState, D3D11_DEFAULT_SAMPLE_MASK); - - ctx->setInputAssemblyState(iaState); - ctx->setDepthStencilState(dsState); - ctx->setRasterizerState(rsState); - ctx->setLogicOpState(loState); - ctx->setMultisampleState(msState); - - for (uint32_t i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++) - ctx->setBlendMode(i, cbState); - - // Reset dynamic states - ctx->setBlendConstants(DxvkBlendConstants { 1.0f, 1.0f, 1.0f, 1.0f }); - ctx->setStencilReference(D3D11_DEFAULT_STENCIL_REFERENCE); - - // Reset viewports - auto viewport = VkViewport(); - auto scissor = VkRect2D(); - - ctx->setViewports(1, &viewport, &scissor); - - // Unbind indirect draw buffer - ctx->bindDrawBuffers(DxvkBufferSlice(), DxvkBufferSlice()); - - // Unbind index and vertex buffers - ctx->bindIndexBuffer(DxvkBufferSlice(), VK_INDEX_TYPE_UINT32); - - for (uint32_t i = 0; i < D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT; i++) - ctx->bindVertexBuffer(i, DxvkBufferSlice(), 0); - - // Unbind transform feedback buffers - for (uint32_t i = 0; i < D3D11_SO_BUFFER_SLOT_COUNT; i++) - ctx->bindXfbBuffer(i, DxvkBufferSlice(), DxvkBufferSlice()); - - // Unbind per-shader stage resources - for (uint32_t i = 0; i < 6; i++) { - auto programType = DxbcProgramType(i); - auto stage = GetShaderStage(programType); - - ctx->bindShader(stage, nullptr); - - // Unbind constant buffers, including the shader's ICB - auto cbSlotId = computeConstantBufferBinding(programType, 0); - - for (uint32_t j = 0; j <= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT; j++) - ctx->bindResourceBuffer(stage, cbSlotId + j, DxvkBufferSlice()); - - // Unbind shader resource views - auto srvSlotId = computeSrvBinding(programType, 0); - - for (uint32_t j = 0; j < D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT; j++) - ctx->bindResourceView(stage, srvSlotId + j, nullptr, nullptr); - - // Unbind texture samplers - auto samplerSlotId = computeSamplerBinding(programType, 0); - - for (uint32_t j = 0; j < D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT; j++) - ctx->bindResourceSampler(stage, samplerSlotId + j, nullptr); - - // Unbind UAVs for supported stages - if (programType == DxbcProgramType::PixelShader - || programType == DxbcProgramType::ComputeShader) { - VkShaderStageFlags stages = programType == DxbcProgramType::PixelShader - ? VK_SHADER_STAGE_ALL_GRAPHICS - : VK_SHADER_STAGE_COMPUTE_BIT; - - auto uavSlotId = computeUavBinding(programType, 0); - auto ctrSlotId = computeUavCounterBinding(programType, 0); - - for (uint32_t j = 0; j < D3D11_1_UAV_SLOT_COUNT; j++) { - ctx->bindResourceView (stages, uavSlotId, nullptr, nullptr); - ctx->bindResourceBuffer (stages, ctrSlotId, DxvkBufferSlice()); - } - } - } - - // Initialize push constants - DxbcPushConstants pc; - pc.rasterizerSampleCount = 1; - ctx->pushConstants(0, sizeof(pc), &pc); - }); - } - - - void D3D11DeviceContext::RestoreState() { - BindFramebuffer(); - - BindShader (GetCommonShader(m_state.vs.shader.ptr())); - BindShader (GetCommonShader(m_state.hs.shader.ptr())); - BindShader (GetCommonShader(m_state.ds.shader.ptr())); - BindShader (GetCommonShader(m_state.gs.shader.ptr())); - BindShader (GetCommonShader(m_state.ps.shader.ptr())); - BindShader (GetCommonShader(m_state.cs.shader.ptr())); - - ApplyInputLayout(); - ApplyPrimitiveTopology(); - ApplyBlendState(); - ApplyBlendFactor(); - ApplyDepthStencilState(); - ApplyStencilRef(); - ApplyRasterizerState(); - ApplyRasterizerSampleCount(); - ApplyViewportState(); - - BindDrawBuffers( - m_state.id.argBuffer.ptr(), - m_state.id.cntBuffer.ptr()); - - BindIndexBuffer( - m_state.ia.indexBuffer.buffer.ptr(), - m_state.ia.indexBuffer.offset, - m_state.ia.indexBuffer.format); - - for (uint32_t i = 0; i < m_state.ia.vertexBuffers.size(); i++) { - BindVertexBuffer(i, - m_state.ia.vertexBuffers[i].buffer.ptr(), - m_state.ia.vertexBuffers[i].offset, - m_state.ia.vertexBuffers[i].stride); - } - - for (uint32_t i = 0; i < m_state.so.targets.size(); i++) - BindXfbBuffer(i, m_state.so.targets[i].buffer.ptr(), ~0u); - - RestoreConstantBuffers (m_state.vs.constantBuffers); - RestoreConstantBuffers (m_state.hs.constantBuffers); - RestoreConstantBuffers (m_state.ds.constantBuffers); - RestoreConstantBuffers (m_state.gs.constantBuffers); - RestoreConstantBuffers (m_state.ps.constantBuffers); - RestoreConstantBuffers (m_state.cs.constantBuffers); - - RestoreSamplers (m_state.vs.samplers); - RestoreSamplers (m_state.hs.samplers); - RestoreSamplers (m_state.ds.samplers); - RestoreSamplers(m_state.gs.samplers); - RestoreSamplers (m_state.ps.samplers); - RestoreSamplers (m_state.cs.samplers); - - RestoreShaderResources (m_state.vs.shaderResources); - RestoreShaderResources (m_state.hs.shaderResources); - RestoreShaderResources (m_state.ds.shaderResources); - RestoreShaderResources (m_state.gs.shaderResources); - RestoreShaderResources (m_state.ps.shaderResources); - RestoreShaderResources (m_state.cs.shaderResources); - - RestoreUnorderedAccessViews (m_state.ps.unorderedAccessViews); - RestoreUnorderedAccessViews (m_state.cs.unorderedAccessViews); - } - - - template - void D3D11DeviceContext::RestoreConstantBuffers( - D3D11ConstantBufferBindings& Bindings) { - uint32_t slotId = computeConstantBufferBinding(Stage, 0); - - for (uint32_t i = 0; i < Bindings.size(); i++) { - BindConstantBuffer(slotId + i, Bindings[i].buffer.ptr(), - Bindings[i].constantOffset, Bindings[i].constantBound); - } - } - - - template - void D3D11DeviceContext::RestoreSamplers( - D3D11SamplerBindings& Bindings) { - uint32_t slotId = computeSamplerBinding(Stage, 0); - - for (uint32_t i = 0; i < Bindings.size(); i++) - BindSampler(slotId + i, Bindings[i]); - } - - - template - void D3D11DeviceContext::RestoreShaderResources( - D3D11ShaderResourceBindings& Bindings) { - uint32_t slotId = computeSrvBinding(Stage, 0); - - for (uint32_t i = 0; i < Bindings.views.size(); i++) - BindShaderResource(slotId + i, Bindings.views[i].ptr()); - } - - - template - void D3D11DeviceContext::RestoreUnorderedAccessViews( - D3D11UnorderedAccessBindings& Bindings) { - uint32_t uavSlotId = computeUavBinding (Stage, 0); - uint32_t ctrSlotId = computeUavCounterBinding(Stage, 0); - - for (uint32_t i = 0; i < Bindings.size(); i++) { - BindUnorderedAccessView( - uavSlotId + i, - Bindings[i].ptr(), - ctrSlotId + i, ~0u); - } - } - - bool D3D11DeviceContext::TestRtvUavHazards( UINT NumRTVs, ID3D11RenderTargetView* const* ppRTVs, diff --git a/src/d3d11/d3d11_context.h b/src/d3d11/d3d11_context.h index 4cb0ebcb..e82faee4 100644 --- a/src/d3d11/d3d11_context.h +++ b/src/d3d11/d3d11_context.h @@ -43,8 +43,6 @@ namespace dxvk { const D3D11_RECT* pRects, UINT NumRects); - void STDMETHODCALLTYPE ClearState(); - void STDMETHODCALLTYPE SetPredication( ID3D11Predicate* pPredicate, BOOL PredicateValue); @@ -407,26 +405,6 @@ namespace dxvk { ID3D11Buffer* pBufferForArgs, ID3D11Buffer* pBufferForCount); - void ResetState(); - - void RestoreState(); - - template - void RestoreConstantBuffers( - D3D11ConstantBufferBindings& Bindings); - - template - void RestoreSamplers( - D3D11SamplerBindings& Bindings); - - template - void RestoreShaderResources( - D3D11ShaderResourceBindings& Bindings); - - template - void RestoreUnorderedAccessViews( - D3D11UnorderedAccessBindings& Bindings); - bool TestRtvUavHazards( UINT NumRTVs, ID3D11RenderTargetView* const* ppRTVs, diff --git a/src/d3d11/d3d11_context_common.cpp b/src/d3d11/d3d11_context_common.cpp index 996ad3f7..c8ea5cf0 100644 --- a/src/d3d11/d3d11_context_common.cpp +++ b/src/d3d11/d3d11_context_common.cpp @@ -63,6 +63,123 @@ namespace dxvk { } + template + void STDMETHODCALLTYPE D3D11CommonContext::ClearState() { + D3D10DeviceLock lock = LockContext(); + + // Default shaders + m_state.vs.shader = nullptr; + m_state.hs.shader = nullptr; + m_state.ds.shader = nullptr; + m_state.gs.shader = nullptr; + m_state.ps.shader = nullptr; + m_state.cs.shader = nullptr; + + // Default constant buffers + for (uint32_t i = 0; i < D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT; i++) { + m_state.vs.constantBuffers[i] = { nullptr, 0, 0 }; + m_state.hs.constantBuffers[i] = { nullptr, 0, 0 }; + m_state.ds.constantBuffers[i] = { nullptr, 0, 0 }; + m_state.gs.constantBuffers[i] = { nullptr, 0, 0 }; + m_state.ps.constantBuffers[i] = { nullptr, 0, 0 }; + m_state.cs.constantBuffers[i] = { nullptr, 0, 0 }; + } + + // Default samplers + for (uint32_t i = 0; i < D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT; i++) { + m_state.vs.samplers[i] = nullptr; + m_state.hs.samplers[i] = nullptr; + m_state.ds.samplers[i] = nullptr; + m_state.gs.samplers[i] = nullptr; + m_state.ps.samplers[i] = nullptr; + m_state.cs.samplers[i] = nullptr; + } + + // Default shader resources + for (uint32_t i = 0; i < D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT; i++) { + m_state.vs.shaderResources.views[i] = nullptr; + m_state.hs.shaderResources.views[i] = nullptr; + m_state.ds.shaderResources.views[i] = nullptr; + m_state.gs.shaderResources.views[i] = nullptr; + m_state.ps.shaderResources.views[i] = nullptr; + m_state.cs.shaderResources.views[i] = nullptr; + } + + m_state.vs.shaderResources.hazardous.clear(); + m_state.hs.shaderResources.hazardous.clear(); + m_state.ds.shaderResources.hazardous.clear(); + m_state.gs.shaderResources.hazardous.clear(); + m_state.ps.shaderResources.hazardous.clear(); + m_state.cs.shaderResources.hazardous.clear(); + + // Default UAVs + for (uint32_t i = 0; i < D3D11_1_UAV_SLOT_COUNT; i++) { + m_state.ps.unorderedAccessViews[i] = nullptr; + m_state.cs.unorderedAccessViews[i] = nullptr; + } + + m_state.cs.uavMask.clear(); + + // Default ID state + m_state.id.argBuffer = nullptr; + + // Default IA state + m_state.ia.inputLayout = nullptr; + m_state.ia.primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED; + + for (uint32_t i = 0; i < D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT; i++) { + m_state.ia.vertexBuffers[i].buffer = nullptr; + m_state.ia.vertexBuffers[i].offset = 0; + m_state.ia.vertexBuffers[i].stride = 0; + } + + m_state.ia.indexBuffer.buffer = nullptr; + m_state.ia.indexBuffer.offset = 0; + m_state.ia.indexBuffer.format = DXGI_FORMAT_UNKNOWN; + + // Default OM State + for (uint32_t i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++) + m_state.om.renderTargetViews[i] = nullptr; + m_state.om.depthStencilView = nullptr; + + m_state.om.cbState = nullptr; + m_state.om.dsState = nullptr; + + for (uint32_t i = 0; i < 4; i++) + m_state.om.blendFactor[i] = 1.0f; + + m_state.om.sampleCount = 0; + m_state.om.sampleMask = D3D11_DEFAULT_SAMPLE_MASK; + m_state.om.stencilRef = D3D11_DEFAULT_STENCIL_REFERENCE; + + m_state.om.maxRtv = 0; + m_state.om.maxUav = 0; + + // Default RS state + m_state.rs.state = nullptr; + m_state.rs.numViewports = 0; + m_state.rs.numScissors = 0; + + for (uint32_t i = 0; i < D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE; i++) { + m_state.rs.viewports[i] = D3D11_VIEWPORT { }; + m_state.rs.scissors [i] = D3D11_RECT { }; + } + + // Default SO state + for (uint32_t i = 0; i < D3D11_SO_BUFFER_SLOT_COUNT; i++) { + m_state.so.targets[i].buffer = nullptr; + m_state.so.targets[i].offset = 0; + } + + // Default predication + m_state.pr.predicateObject = nullptr; + m_state.pr.predicateValue = FALSE; + + // Make sure to apply all state + ResetState(); + } + + template void STDMETHODCALLTYPE D3D11CommonContext::UpdateSubresource( ID3D11Resource* pDstResource, @@ -1717,6 +1834,112 @@ namespace dxvk { } + template + void D3D11CommonContext::ResetState() { + EmitCs([] (DxvkContext* ctx) { + // Reset render targets + ctx->bindRenderTargets(DxvkRenderTargets()); + + // Reset vertex input state + ctx->setInputLayout(0, nullptr, 0, nullptr); + + // Reset render states + DxvkInputAssemblyState iaState; + InitDefaultPrimitiveTopology(&iaState); + + DxvkDepthStencilState dsState; + InitDefaultDepthStencilState(&dsState); + + DxvkRasterizerState rsState; + InitDefaultRasterizerState(&rsState); + + DxvkBlendMode cbState; + DxvkLogicOpState loState; + DxvkMultisampleState msState; + InitDefaultBlendState(&cbState, &loState, &msState, D3D11_DEFAULT_SAMPLE_MASK); + + ctx->setInputAssemblyState(iaState); + ctx->setDepthStencilState(dsState); + ctx->setRasterizerState(rsState); + ctx->setLogicOpState(loState); + ctx->setMultisampleState(msState); + + for (uint32_t i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++) + ctx->setBlendMode(i, cbState); + + // Reset dynamic states + ctx->setBlendConstants(DxvkBlendConstants { 1.0f, 1.0f, 1.0f, 1.0f }); + ctx->setStencilReference(D3D11_DEFAULT_STENCIL_REFERENCE); + + // Reset viewports + auto viewport = VkViewport(); + auto scissor = VkRect2D(); + + ctx->setViewports(1, &viewport, &scissor); + + // Unbind indirect draw buffer + ctx->bindDrawBuffers(DxvkBufferSlice(), DxvkBufferSlice()); + + // Unbind index and vertex buffers + ctx->bindIndexBuffer(DxvkBufferSlice(), VK_INDEX_TYPE_UINT32); + + for (uint32_t i = 0; i < D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT; i++) + ctx->bindVertexBuffer(i, DxvkBufferSlice(), 0); + + // Unbind transform feedback buffers + for (uint32_t i = 0; i < D3D11_SO_BUFFER_SLOT_COUNT; i++) + ctx->bindXfbBuffer(i, DxvkBufferSlice(), DxvkBufferSlice()); + + // Unbind per-shader stage resources + for (uint32_t i = 0; i < 6; i++) { + auto programType = DxbcProgramType(i); + auto stage = GetShaderStage(programType); + + ctx->bindShader(stage, nullptr); + + // Unbind constant buffers, including the shader's ICB + auto cbSlotId = computeConstantBufferBinding(programType, 0); + + for (uint32_t j = 0; j <= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT; j++) + ctx->bindResourceBuffer(stage, cbSlotId + j, DxvkBufferSlice()); + + // Unbind shader resource views + auto srvSlotId = computeSrvBinding(programType, 0); + + for (uint32_t j = 0; j < D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT; j++) + ctx->bindResourceView(stage, srvSlotId + j, nullptr, nullptr); + + // Unbind texture samplers + auto samplerSlotId = computeSamplerBinding(programType, 0); + + for (uint32_t j = 0; j < D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT; j++) + ctx->bindResourceSampler(stage, samplerSlotId + j, nullptr); + + // Unbind UAVs for supported stages + if (programType == DxbcProgramType::PixelShader + || programType == DxbcProgramType::ComputeShader) { + VkShaderStageFlags stages = programType == DxbcProgramType::PixelShader + ? VK_SHADER_STAGE_ALL_GRAPHICS + : VK_SHADER_STAGE_COMPUTE_BIT; + + auto uavSlotId = computeUavBinding(programType, 0); + auto ctrSlotId = computeUavCounterBinding(programType, 0); + + for (uint32_t j = 0; j < D3D11_1_UAV_SLOT_COUNT; j++) { + ctx->bindResourceView (stages, uavSlotId, nullptr, nullptr); + ctx->bindResourceBuffer (stages, ctrSlotId, DxvkBufferSlice()); + } + } + } + + // Initialize push constants + DxbcPushConstants pc; + pc.rasterizerSampleCount = 1; + ctx->pushConstants(0, sizeof(pc), &pc); + }); + } + + template template void D3D11CommonContext::ResolveSrvHazards( @@ -2049,6 +2272,123 @@ namespace dxvk { } + template + void D3D11CommonContext::RestoreState() { + BindFramebuffer(); + + BindShader (GetCommonShader(m_state.vs.shader.ptr())); + BindShader (GetCommonShader(m_state.hs.shader.ptr())); + BindShader (GetCommonShader(m_state.ds.shader.ptr())); + BindShader (GetCommonShader(m_state.gs.shader.ptr())); + BindShader (GetCommonShader(m_state.ps.shader.ptr())); + BindShader (GetCommonShader(m_state.cs.shader.ptr())); + + ApplyInputLayout(); + ApplyPrimitiveTopology(); + ApplyBlendState(); + ApplyBlendFactor(); + ApplyDepthStencilState(); + ApplyStencilRef(); + ApplyRasterizerState(); + ApplyRasterizerSampleCount(); + ApplyViewportState(); + + BindDrawBuffers( + m_state.id.argBuffer.ptr(), + m_state.id.cntBuffer.ptr()); + + BindIndexBuffer( + m_state.ia.indexBuffer.buffer.ptr(), + m_state.ia.indexBuffer.offset, + m_state.ia.indexBuffer.format); + + for (uint32_t i = 0; i < m_state.ia.vertexBuffers.size(); i++) { + BindVertexBuffer(i, + m_state.ia.vertexBuffers[i].buffer.ptr(), + m_state.ia.vertexBuffers[i].offset, + m_state.ia.vertexBuffers[i].stride); + } + + for (uint32_t i = 0; i < m_state.so.targets.size(); i++) + BindXfbBuffer(i, m_state.so.targets[i].buffer.ptr(), ~0u); + + RestoreConstantBuffers (m_state.vs.constantBuffers); + RestoreConstantBuffers (m_state.hs.constantBuffers); + RestoreConstantBuffers (m_state.ds.constantBuffers); + RestoreConstantBuffers (m_state.gs.constantBuffers); + RestoreConstantBuffers (m_state.ps.constantBuffers); + RestoreConstantBuffers (m_state.cs.constantBuffers); + + RestoreSamplers (m_state.vs.samplers); + RestoreSamplers (m_state.hs.samplers); + RestoreSamplers (m_state.ds.samplers); + RestoreSamplers(m_state.gs.samplers); + RestoreSamplers (m_state.ps.samplers); + RestoreSamplers (m_state.cs.samplers); + + RestoreShaderResources (m_state.vs.shaderResources); + RestoreShaderResources (m_state.hs.shaderResources); + RestoreShaderResources (m_state.ds.shaderResources); + RestoreShaderResources (m_state.gs.shaderResources); + RestoreShaderResources (m_state.ps.shaderResources); + RestoreShaderResources (m_state.cs.shaderResources); + + RestoreUnorderedAccessViews (m_state.ps.unorderedAccessViews); + RestoreUnorderedAccessViews (m_state.cs.unorderedAccessViews); + } + + + template + template + void D3D11CommonContext::RestoreConstantBuffers( + D3D11ConstantBufferBindings& Bindings) { + uint32_t slotId = computeConstantBufferBinding(Stage, 0); + + for (uint32_t i = 0; i < Bindings.size(); i++) { + BindConstantBuffer(slotId + i, Bindings[i].buffer.ptr(), + Bindings[i].constantOffset, Bindings[i].constantBound); + } + } + + + template + template + void D3D11CommonContext::RestoreSamplers( + D3D11SamplerBindings& Bindings) { + uint32_t slotId = computeSamplerBinding(Stage, 0); + + for (uint32_t i = 0; i < Bindings.size(); i++) + BindSampler(slotId + i, Bindings[i]); + } + + + template + template + void D3D11CommonContext::RestoreShaderResources( + D3D11ShaderResourceBindings& Bindings) { + uint32_t slotId = computeSrvBinding(Stage, 0); + + for (uint32_t i = 0; i < Bindings.views.size(); i++) + BindShaderResource(slotId + i, Bindings.views[i].ptr()); + } + + + template + template + void D3D11CommonContext::RestoreUnorderedAccessViews( + D3D11UnorderedAccessBindings& Bindings) { + uint32_t uavSlotId = computeUavBinding (Stage, 0); + uint32_t ctrSlotId = computeUavCounterBinding(Stage, 0); + + for (uint32_t i = 0; i < Bindings.size(); i++) { + BindUnorderedAccessView( + uavSlotId + i, + Bindings[i].ptr(), + ctrSlotId + i, ~0u); + } + } + + template bool D3D11CommonContext::TestRtvUavHazards( UINT NumRTVs, diff --git a/src/d3d11/d3d11_context_common.h b/src/d3d11/d3d11_context_common.h index ca7d8de5..d13fa179 100644 --- a/src/d3d11/d3d11_context_common.h +++ b/src/d3d11/d3d11_context_common.h @@ -68,6 +68,8 @@ namespace dxvk { REFIID riid, void** ppvObject); + void STDMETHODCALLTYPE ClearState(); + void STDMETHODCALLTYPE UpdateSubresource( ID3D11Resource* pDstResource, UINT DstSubresource, @@ -584,6 +586,8 @@ namespace dxvk { UINT NumSamplers, ID3D11SamplerState** ppSamplers); + void ResetState(); + template void ResolveSrvHazards( T* pView, @@ -603,6 +607,24 @@ namespace dxvk { void ResolveOmUavHazards( D3D11RenderTargetView* pView); + void RestoreState(); + + template + void RestoreConstantBuffers( + D3D11ConstantBufferBindings& Bindings); + + template + void RestoreSamplers( + D3D11SamplerBindings& Bindings); + + template + void RestoreShaderResources( + D3D11ShaderResourceBindings& Bindings); + + template + void RestoreUnorderedAccessViews( + D3D11UnorderedAccessBindings& Bindings); + template void SetConstantBuffers( D3D11ConstantBufferBindings& Bindings,