git.s-ol.nu ~forks/DiligentCore / 1a3c66a
PipelineStateD3D11Impl: improved D3D11 shader storage assiduous 6 months ago
2 changed file(s) with 65 addition(s) and 62 deletion(s). Raw diff Collapse all Expand all
7070 virtual ID3D11InputLayout* DILIGENT_CALL_TYPE GetD3D11InputLayout() override final { return m_pd3d11InputLayout; }
7171
7272 /// Implementation of IPipelineStateD3D11::GetD3D11VertexShader() method.
73 virtual ID3D11VertexShader* DILIGENT_CALL_TYPE GetD3D11VertexShader() override final { return m_pVS; }
73 virtual ID3D11VertexShader* DILIGENT_CALL_TYPE GetD3D11VertexShader() override final { return GetD3D11Shader<ID3D11VertexShader>(VSInd); }
7474
7575 /// Implementation of IPipelineStateD3D11::GetD3D11PixelShader() method.
76 virtual ID3D11PixelShader* DILIGENT_CALL_TYPE GetD3D11PixelShader() override final { return m_pPS; }
76 virtual ID3D11PixelShader* DILIGENT_CALL_TYPE GetD3D11PixelShader() override final { return GetD3D11Shader<ID3D11PixelShader>(PSInd); }
7777
7878 /// Implementation of IPipelineStateD3D11::GetD3D11GeometryShader() method.
79 virtual ID3D11GeometryShader* DILIGENT_CALL_TYPE GetD3D11GeometryShader() override final { return m_pGS; }
79 virtual ID3D11GeometryShader* DILIGENT_CALL_TYPE GetD3D11GeometryShader() override final { return GetD3D11Shader<ID3D11GeometryShader>(GSInd); }
8080
8181 /// Implementation of IPipelineStateD3D11::GetD3D11DomainShader() method.
82 virtual ID3D11DomainShader* DILIGENT_CALL_TYPE GetD3D11DomainShader() override final { return m_pDS; }
82 virtual ID3D11DomainShader* DILIGENT_CALL_TYPE GetD3D11DomainShader() override final { return GetD3D11Shader<ID3D11DomainShader>(DSInd); }
8383
8484 /// Implementation of IPipelineStateD3D11::GetD3D11HullShader() method.
85 virtual ID3D11HullShader* DILIGENT_CALL_TYPE GetD3D11HullShader() override final { return m_pHS; }
85 virtual ID3D11HullShader* DILIGENT_CALL_TYPE GetD3D11HullShader() override final { return GetD3D11Shader<ID3D11HullShader>(HSInd); }
8686
8787 /// Implementation of IPipelineStateD3D11::GetD3D11ComputeShader() method.
88 virtual ID3D11ComputeShader* DILIGENT_CALL_TYPE GetD3D11ComputeShader() override final { return m_pCS; }
88 virtual ID3D11ComputeShader* DILIGENT_CALL_TYPE GetD3D11ComputeShader() override final { return GetD3D11Shader<ID3D11ComputeShader>(CSInd); }
8989
90 Uint32 GetNumShaders() const { return m_NumShaders; }
91 SHADER_TYPE GetShaderStageType(Uint32 Index) const;
90 Uint32 GetNumShaders() const { return m_NumShaders; }
9291
9392 #ifdef DILIGENT_DEVELOPMENT
9493 void DvpVerifySRBResources(class ShaderResourceBindingD3D11Impl* pSRBs[], const D3D11ShaderResourceCounters BaseBindings[], Uint32 NumSRBs) const;
111110
112111 void ValidateShaderResources(const ShaderD3D11Impl* pShader);
113112
113 template <typename D3D11ShaderType>
114 D3D11ShaderType* GetD3D11Shader(Uint32 ShaderInd)
115 {
116 auto idx = m_ShaderIndices[ShaderInd];
117 return idx >= 0 ? static_cast<D3D11ShaderType*>(m_ppd3d11Shaders[idx].p) : nullptr;
118 }
119
114120 private:
115 std::array<Uint8, 5> m_ShaderTypes = {};
116 Uint8 m_NumShaders = 0;
121 // ShaderTypeIndex -> index in m_ppd3d11Shaders array
122 std::array<Int8, D3D11ResourceBindPoints::NumShaderTypes> m_ShaderIndices = {-1, -1, -1, -1, -1, -1};
123
124 // The number of shader stages in this pipeline
125 Uint8 m_NumShaders = 0;
117126
118127 CComPtr<ID3D11BlendState> m_pd3d11BlendState;
119128 CComPtr<ID3D11RasterizerState> m_pd3d11RasterizerState;
120129 CComPtr<ID3D11DepthStencilState> m_pd3d11DepthStencilState;
121130 CComPtr<ID3D11InputLayout> m_pd3d11InputLayout;
122 CComPtr<ID3D11VertexShader> m_pVS;
123 CComPtr<ID3D11PixelShader> m_pPS;
124 CComPtr<ID3D11GeometryShader> m_pGS;
125 CComPtr<ID3D11DomainShader> m_pDS;
126 CComPtr<ID3D11HullShader> m_pHS;
127 CComPtr<ID3D11ComputeShader> m_pCS;
131
132 using D3D11ShaderAutoPtrType = CComPtr<ID3D11DeviceChild>;
133 D3D11ShaderAutoPtrType* m_ppd3d11Shaders = nullptr; // Shader array indexed by m_ShaderIndices[]
128134
129135 #ifdef DILIGENT_DEVELOPMENT
130136 // Shader resources for all shaders in all shader stages in the pipeline.
135141 #endif
136142 };
137143
138 __forceinline SHADER_TYPE GetShaderStageType(const ShaderD3D11Impl* pShader)
139 {
140 return pShader->GetDesc().ShaderType;
141 }
142
143144 } // namespace Diligent
3636
3737 namespace Diligent
3838 {
39
3940 namespace
4041 {
42
4143 void VerifyResourceMerge(const PipelineStateDesc& PSODesc,
4244 const D3DShaderResourceAttribs& ExistingRes,
4345 const D3DShaderResourceAttribs& NewResAttribs)
6163 if (ExistingRes.IsMultisample() != NewResAttribs.IsMultisample())
6264 LOG_RESOURCE_MERGE_ERROR_AND_THROW("mutlisample state");
6365 #undef LOG_RESOURCE_MERGE_ERROR_AND_THROW
66 }
67
68 __forceinline SHADER_TYPE GetShaderStageType(const ShaderD3D11Impl* pShader)
69 {
70 return pShader->GetDesc().ShaderType;
6471 }
6572
6673 } // namespace
262269 ExtractShaders<ShaderD3D11Impl>(CreateInfo, Shaders);
263270
264271 m_NumShaders = static_cast<Uint8>(Shaders.size());
265
266 for (Uint32 s = 0; s < Shaders.size(); ++s)
267 {
268 auto ShaderType = Shaders[s]->GetDesc().ShaderType;
269 m_ShaderTypes[s] = static_cast<Uint8>(GetShaderTypeIndex(ShaderType));
270 VERIFY_EXPR(ShaderType == GetShaderTypeFromIndex(m_ShaderTypes[s]));
272 for (Uint32 s = 0; s < m_NumShaders; ++s)
273 {
274 const auto ShaderType = Shaders[s]->GetDesc().ShaderType;
275 const auto ShaderTypeIdx = GetShaderTypeIndex(ShaderType);
276 VERIFY_EXPR(m_ShaderIndices[ShaderTypeIdx] < 0);
277 m_ShaderIndices[ShaderTypeIdx] = static_cast<Int8>(s);
271278 }
272279
273280 FixedLinearAllocator MemPool{GetRawAllocator()};
274281
275282 ReserveSpaceForPipelineDesc(CreateInfo, MemPool);
283 MemPool.AddSpace<D3D11ShaderAutoPtrType>(m_NumShaders);
276284
277285 MemPool.Reserve();
278286
279287 InitializePipelineDesc(CreateInfo, MemPool);
288 m_ppd3d11Shaders = MemPool.ConstructArray<D3D11ShaderAutoPtrType>(m_NumShaders);
280289
281290 InitResourceLayouts(CreateInfo, Shaders, ByteCodes);
282291
283292 auto* pDeviceD3D11 = GetDevice()->GetD3D11Device();
284 for (Uint32 s = 0; s < Shaders.size(); ++s)
285 {
286 auto ShaderType = Shaders[s]->GetDesc().ShaderType;
293 for (Uint32 s = 0; s < m_NumShaders; ++s)
294 {
295 const auto ShaderType = Shaders[s]->GetDesc().ShaderType;
287296 const auto& pByteCode = ByteCodes[s];
288297 switch (ShaderType)
289298 {
290 #define CREATE_SHADER(SHADER_NAME, ShaderName, pShader) \
291 case SHADER_TYPE_##SHADER_NAME: \
292 { \
293 HRESULT hr = pDeviceD3D11->Create##ShaderName##Shader(pByteCode->GetBufferPointer(), pByteCode->GetBufferSize(), NULL, &pShader); \
294 CHECK_D3D_RESULT_THROW(hr, "Failed to create D3D11 shader"); \
295 break; \
299 #define CREATE_SHADER(SHADER_NAME, ShaderName) \
300 case SHADER_TYPE_##SHADER_NAME: \
301 { \
302 CComPtr<ID3D11##ShaderName##Shader> pShader; \
303 \
304 auto hr = pDeviceD3D11->Create##ShaderName##Shader(pByteCode->GetBufferPointer(), pByteCode->GetBufferSize(), NULL, &pShader); \
305 CHECK_D3D_RESULT_THROW(hr, "Failed to create D3D11 shader"); \
306 m_ppd3d11Shaders[s] = pShader; \
307 break; \
296308 }
297309 // clang-format off
298 CREATE_SHADER(VERTEX, Vertex, m_pVS)
299 CREATE_SHADER(PIXEL, Pixel, m_pPS)
300 CREATE_SHADER(GEOMETRY, Geometry, m_pGS)
301 CREATE_SHADER(DOMAIN, Domain, m_pDS)
302 CREATE_SHADER(HULL, Hull, m_pHS)
303 CREATE_SHADER(COMPUTE, Compute, m_pCS)
310 CREATE_SHADER(VERTEX, Vertex)
311 CREATE_SHADER(PIXEL, Pixel)
312 CREATE_SHADER(GEOMETRY, Geometry)
313 CREATE_SHADER(DOMAIN, Domain)
314 CREATE_SHADER(HULL, Hull)
315 CREATE_SHADER(COMPUTE, Compute)
304316 // clang-format on
305317 default: LOG_ERROR_AND_THROW("Unknown shader type");
306318 }
319331 std::vector<CComPtr<ID3DBlob>> ByteCodes;
320332 InitInternalObjects(CreateInfo, ByteCodes);
321333
322 if (m_pVS == nullptr)
334 if (GetD3D11VertexShader() == nullptr)
323335 LOG_ERROR_AND_THROW("Vertex shader is null");
324336
325337 auto& GraphicsPipeline = GetGraphicsPipelineDesc();
383395
384396 void PipelineStateD3D11Impl::Destruct()
385397 {
386 m_ShaderTypes = {};
387 m_NumShaders = 0;
388
389 m_pd3d11BlendState = nullptr;
390 m_pd3d11RasterizerState = nullptr;
391 m_pd3d11DepthStencilState = nullptr;
392 m_pd3d11InputLayout = nullptr;
393 m_pVS = nullptr;
394 m_pPS = nullptr;
395 m_pGS = nullptr;
396 m_pDS = nullptr;
397 m_pHS = nullptr;
398 m_pCS = nullptr;
398 m_pd3d11BlendState.Release();
399 m_pd3d11RasterizerState.Release();
400 m_pd3d11DepthStencilState.Release();
401 m_pd3d11InputLayout.Release();
402 for (Uint32 s = 0; s < m_NumShaders; ++s)
403 m_ppd3d11Shaders[s].~D3D11ShaderAutoPtrType();
404 m_ppd3d11Shaders = nullptr;
405 m_ShaderIndices.fill(-1);
399406
400407 TPipelineStateBase::Destruct();
401408 }
409416 return false;
410417
411418 const auto& rhs = *ValidatedCast<const PipelineStateD3D11Impl>(pPSO);
412 if (m_NumShaders != rhs.m_NumShaders || m_ShaderTypes != rhs.m_ShaderTypes)
419 if (m_ActiveShaderStages != rhs.m_ActiveShaderStages)
413420 return false;
414421
415422 return true;
416 }
417
418 SHADER_TYPE PipelineStateD3D11Impl::GetShaderStageType(Uint32 Index) const
419 {
420 return GetShaderTypeFromIndex(m_ShaderTypes[Index]);
421423 }
422424
423425 void PipelineStateD3D11Impl::ValidateShaderResources(const ShaderD3D11Impl* pShader)