From 9c997120e15f6d57b02958fce662e1a3cec295aa Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Sun, 10 Dec 2017 01:56:07 +0100 Subject: [PATCH] [d3d11] Implemented shader resource binding --- src/d3d11/d3d11_context.cpp | 71 +++++++++++++++++++++++++++++---- src/d3d11/d3d11_context.h | 7 ++++ src/d3d11/d3d11_context_state.h | 12 +++++- src/d3d11/d3d11_view.h | 6 +++ 4 files changed, 87 insertions(+), 9 deletions(-) diff --git a/src/d3d11/d3d11_context.cpp b/src/d3d11/d3d11_context.cpp index 42510ca10..3bbccd029 100644 --- a/src/d3d11/d3d11_context.cpp +++ b/src/d3d11/d3d11_context.cpp @@ -61,8 +61,8 @@ namespace dxvk { this->VSSetShader(nullptr, nullptr, 0); this->VSSetConstantBuffers(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, nullptr); -// this->VSSetShaderResources(0, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, nullptr); -// this->VSSetSamplers (0, D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, nullptr); + this->VSSetShaderResources(0, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, nullptr); + this->VSSetSamplers (0, D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, nullptr); // this->HSSetShader(nullptr, nullptr, 0); // this->HSSetConstantBuffers(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, nullptr); @@ -81,8 +81,8 @@ namespace dxvk { this->PSSetShader(nullptr, nullptr, 0); this->PSSetConstantBuffers(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, nullptr); -// this->PSSetShaderResources(0, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, nullptr); -// this->PSSetSamplers (0, D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, nullptr); + this->PSSetShaderResources(0, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, nullptr); + this->PSSetSamplers (0, D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, nullptr); // this->CSSetShader(nullptr, nullptr, 0); // this->CSSetConstantBuffers(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, nullptr); @@ -90,8 +90,8 @@ namespace dxvk { // this->CSSetSamplers (0, D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, nullptr); this->OMSetRenderTargets(0, nullptr, nullptr); -// this->OMSetBlendState(nullptr, nullptr, D3D11_DEFAULT_SAMPLE_MASK); -// this->OMSetDepthStencilState(nullptr, 0); + this->OMSetBlendState(nullptr, nullptr, D3D11_DEFAULT_SAMPLE_MASK); + this->OMSetDepthStencilState(nullptr, 0); this->RSSetState(nullptr); this->RSSetViewports(0, nullptr); @@ -699,7 +699,11 @@ namespace dxvk { UINT StartSlot, UINT NumViews, ID3D11ShaderResourceView* const* ppShaderResourceViews) { - Logger::err("D3D11DeviceContext::VSSetShaderResources: Not implemented"); + this->BindShaderResources( + DxbcProgramType::VertexShader, + &m_state.vs.shaderResources, + StartSlot, NumViews, + ppShaderResourceViews); } @@ -973,7 +977,11 @@ namespace dxvk { UINT StartSlot, UINT NumViews, ID3D11ShaderResourceView* const* ppShaderResourceViews) { - Logger::err("D3D11DeviceContext::PSSetShaderResources: Not implemented"); + this->BindShaderResources( + DxbcProgramType::PixelShader, + &m_state.ps.shaderResources, + StartSlot, NumViews, + ppShaderResourceViews); } @@ -1408,6 +1416,53 @@ namespace dxvk { } + void D3D11DeviceContext::BindShaderResources( + DxbcProgramType ShaderStage, + D3D11ShaderResourceBindings* pBindings, + UINT StartSlot, + UINT NumResources, + ID3D11ShaderResourceView* const* ppResources) { + for (uint32_t i = 0; i < NumResources; i++) { + D3D11ShaderResourceView* resView = nullptr; + + if (ppResources != nullptr) + resView = static_cast(ppResources[i]); + + if (pBindings->at(StartSlot + i) != resView) { + pBindings->at(StartSlot + i) = resView; + + // Bind sampler to the DXVK resource slot + const VkPipelineBindPoint bindPoint + = ShaderStage == DxbcProgramType::ComputeShader + ? VK_PIPELINE_BIND_POINT_COMPUTE + : VK_PIPELINE_BIND_POINT_GRAPHICS; + + const uint32_t slotId = computeResourceSlotId( + ShaderStage, DxbcBindingType::ImageSampler, + StartSlot + i); + + if (resView != nullptr) { + // Figure out what we have to bind based on the resource type + if (resView->GetResourceType() == D3D11_RESOURCE_DIMENSION_BUFFER) { + Logger::warn("D3D11: Texel buffers not yet supported"); + m_context->bindResourceTexelBuffer( + bindPoint, slotId, nullptr); + } else { + m_context->bindResourceImage(bindPoint, + slotId, resView->GetDXVKImageView()); + } + } else { + // When unbinding a resource, it doesn't really matter if + // the resource type is correct, so we'll just bind a null + // image to the given resource slot + m_context->bindResourceImage( + bindPoint, slotId, nullptr); + } + } + } + } + + void D3D11DeviceContext::ApplyViewportState() { // We cannot set less than one viewport in Vulkan, and // rendering with no active viewport is illegal anyway. diff --git a/src/d3d11/d3d11_context.h b/src/d3d11/d3d11_context.h index 74cc534c4..add5a5ad8 100644 --- a/src/d3d11/d3d11_context.h +++ b/src/d3d11/d3d11_context.h @@ -563,6 +563,13 @@ namespace dxvk { UINT NumSamplers, ID3D11SamplerState* const* ppSamplers); + void BindShaderResources( + DxbcProgramType ShaderStage, + D3D11ShaderResourceBindings* pBindings, + UINT StartSlot, + UINT NumResources, + ID3D11ShaderResourceView* const* ppResources); + void ApplyViewportState(); void SetupIAStateObjects(); diff --git a/src/d3d11/d3d11_context_state.h b/src/d3d11/d3d11_context_state.h index 26cb81d4e..226f8341d 100644 --- a/src/d3d11/d3d11_context_state.h +++ b/src/d3d11/d3d11_context_state.h @@ -14,15 +14,20 @@ namespace dxvk { using D3D11ConstantBufferBindings = std::array< Com, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT>; - + using D3D11SamplerBindings = std::array< Com, D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT>; + using D3D11ShaderResourceBindings = std::array< + Com, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT>; + + struct D3D11ContextStateVS { Com shader; D3D11ConstantBufferBindings constantBuffers; D3D11SamplerBindings samplers; + D3D11ShaderResourceBindings shaderResources; }; @@ -30,6 +35,7 @@ namespace dxvk { Com shader; D3D11ConstantBufferBindings constantBuffers; D3D11SamplerBindings samplers; + D3D11ShaderResourceBindings shaderResources; }; @@ -37,6 +43,7 @@ namespace dxvk { Com shader; D3D11ConstantBufferBindings constantBuffers; D3D11SamplerBindings samplers; + D3D11ShaderResourceBindings shaderResources; }; @@ -44,6 +51,7 @@ namespace dxvk { Com shader; D3D11ConstantBufferBindings constantBuffers; D3D11SamplerBindings samplers; + D3D11ShaderResourceBindings shaderResources; }; @@ -51,6 +59,7 @@ namespace dxvk { Com shader; D3D11ConstantBufferBindings constantBuffers; D3D11SamplerBindings samplers; + D3D11ShaderResourceBindings shaderResources; }; @@ -58,6 +67,7 @@ namespace dxvk { Com shader; D3D11ConstantBufferBindings constantBuffers; D3D11SamplerBindings samplers; + D3D11ShaderResourceBindings shaderResources; }; diff --git a/src/d3d11/d3d11_view.h b/src/d3d11/d3d11_view.h index de845ede8..c9a6ca7ab 100644 --- a/src/d3d11/d3d11_view.h +++ b/src/d3d11/d3d11_view.h @@ -53,6 +53,12 @@ namespace dxvk { *pDesc = m_desc; } + D3D11_RESOURCE_DIMENSION GetResourceType() { + D3D11_RESOURCE_DIMENSION type; + m_resource->GetType(&type); + return type; + } + Rc GetDXVKBufferView() { return m_bufferView; }