diff options
| author | Egor Yusov <egor.yusov@gmail.com> | 2019-03-07 16:44:55 +0000 |
|---|---|---|
| committer | Egor Yusov <egor.yusov@gmail.com> | 2019-03-07 16:44:55 +0000 |
| commit | 3f579a5ff60f5e54959abe2c85fc612e674ef91c (patch) | |
| tree | 92e4aabb6997d86698baeaab8c216c8fbdf0caab /Graphics | |
| parent | Few minor improvements to shader resource management in Vk backend (diff) | |
| download | DiligentCore-3f579a5ff60f5e54959abe2c85fc612e674ef91c.tar.gz DiligentCore-3f579a5ff60f5e54959abe2c85fc612e674ef91c.zip | |
Improved handling input layout elements in the pipeline state
Diffstat (limited to 'Graphics')
8 files changed, 48 insertions, 37 deletions
diff --git a/Graphics/GraphicsEngine/include/PipelineStateBase.h b/Graphics/GraphicsEngine/include/PipelineStateBase.h index ddc682ef..fc5d8dce 100644 --- a/Graphics/GraphicsEngine/include/PipelineStateBase.h +++ b/Graphics/GraphicsEngine/include/PipelineStateBase.h @@ -63,7 +63,6 @@ public: const PipelineStateDesc& PSODesc, bool bIsDeviceInternal = false ) : TDeviceObjectBase (pRefCounters, pDevice, PSODesc, bIsDeviceInternal), - m_LayoutElements (PSODesc.GraphicsPipeline.InputLayout.NumElements, LayoutElement{}, STD_ALLOCATOR_RAW_MEM(LayoutElement, GetRawAllocator(), "Allocator for vector<LayoutElement>")), m_NumShaders(0) { const auto& SrcLayout = PSODesc.ResourceLayout; @@ -167,9 +166,15 @@ public: VERIFY(m_NumShaders > 0, "There must be at least one shader in the Pipeline State"); const auto& InputLayout = PSODesc.GraphicsPipeline.InputLayout; + LayoutElement* pLayoutElements = nullptr; + if (InputLayout.NumElements > 0) + { + auto* pLayoutElementsRawMem = ALLOCATE(GetRawAllocator(), "Raw memory for input layout elements", sizeof(LayoutElement) * InputLayout.NumElements); + pLayoutElements = reinterpret_cast<LayoutElement*>(pLayoutElementsRawMem); + } + this->m_Desc.GraphicsPipeline.InputLayout.LayoutElements = pLayoutElements; for (size_t Elem = 0; Elem < InputLayout.NumElements; ++Elem) - m_LayoutElements[Elem] = InputLayout.LayoutElements[Elem]; - this->m_Desc.GraphicsPipeline.InputLayout.LayoutElements = m_LayoutElements.data(); + pLayoutElements[Elem] = InputLayout.LayoutElements[Elem]; // Set all strides to an invalid value because an application may want to use 0 stride for (auto& Stride : m_Strides) @@ -177,12 +182,14 @@ public: // Correct description and compute offsets and tight strides decltype(m_Strides) TightStrides = {}; - for (auto It = m_LayoutElements.begin(); It != m_LayoutElements.end(); ++It) + for (Uint32 i=0; i < InputLayout.NumElements; ++i) { - if( It->ValueType == VT_FLOAT32 || It->ValueType == VT_FLOAT16 ) - It->IsNormalized = false; // Floating point values cannot be normalized + auto& LayoutElem = pLayoutElements[i]; - auto BuffSlot = It->BufferSlot; + if( LayoutElem.ValueType == VT_FLOAT32 || LayoutElem.ValueType == VT_FLOAT16 ) + LayoutElem.IsNormalized = false; // Floating point values cannot be normalized + + auto BuffSlot = LayoutElem.BufferSlot; if (BuffSlot >= m_Strides.size()) { UNEXPECTED("Buffer slot (", BuffSlot, ") exceeds maximum allowed value (", m_Strides.size()-1, ")"); @@ -192,31 +199,33 @@ public: auto& CurrAutoStride = TightStrides[BuffSlot]; // If offset is not explicitly specified, use current auto stride value - if (It->RelativeOffset == LayoutElement::AutoOffset) + if (LayoutElem.RelativeOffset == LayoutElement::AutoOffset) { - It->RelativeOffset = CurrAutoStride; + LayoutElem.RelativeOffset = CurrAutoStride; } // If stride is explicitly specified, use it for the current buffer slot - if (It->Stride != LayoutElement::AutoStride) + if (LayoutElem.Stride != LayoutElement::AutoStride) { // Verify that the value is consistent with the previously specified stride, if any - if (m_Strides[BuffSlot] != LayoutElement::AutoStride && m_Strides[BuffSlot] != It->Stride) + if (m_Strides[BuffSlot] != LayoutElement::AutoStride && m_Strides[BuffSlot] != LayoutElem.Stride) { LOG_ERROR_MESSAGE("Inconsistent strides are specified for buffer slot ", BuffSlot, ". " - "Input element at index ", It->InputIndex, " explicitly specifies stride ", It->Stride, ", " + "Input element at index ", LayoutElem.InputIndex, " explicitly specifies stride ", LayoutElem.Stride, ", " "while current value is ", m_Strides[BuffSlot], ". Specify consistent strides or use " "LayoutElement::AutoStride to allow the engine compute strides automatically."); } - m_Strides[BuffSlot] = It->Stride; + m_Strides[BuffSlot] = LayoutElem.Stride; } - CurrAutoStride = std::max(CurrAutoStride, It->RelativeOffset + It->NumComponents * GetValueSize(It->ValueType)); + CurrAutoStride = std::max(CurrAutoStride, LayoutElem.RelativeOffset + LayoutElem.NumComponents * GetValueSize(LayoutElem.ValueType)); } - for (auto It = m_LayoutElements.begin(); It != m_LayoutElements.end(); ++It) + for (Uint32 i=0; i < InputLayout.NumElements; ++i) { - auto BuffSlot = It->BufferSlot; + auto& LayoutElem = pLayoutElements[i]; + + auto BuffSlot = LayoutElem.BufferSlot; // If no input elements explicitly specified stride for this buffer slot, use automatic stride if (m_Strides[BuffSlot] == LayoutElement::AutoStride) { @@ -230,8 +239,8 @@ public: "minimum stride ", TightStrides[BuffSlot], " required to accomodate all input elements."); } } - if (It->Stride == LayoutElement::AutoStride) - It->Stride = m_Strides[BuffSlot]; + if (LayoutElem.Stride == LayoutElement::AutoStride) + LayoutElem.Stride = m_Strides[BuffSlot]; } // Set strides for all unused slots to 0 for (auto& Stride : m_Strides) @@ -270,6 +279,8 @@ public: RawAllocator.Free(const_cast<ShaderResourceVariableDesc*>(this->m_Desc.ResourceLayout.Variables)); if (this->m_Desc.ResourceLayout.StaticSamplers != nullptr) RawAllocator.Free(const_cast<StaticSamplerDesc*>(this->m_Desc.ResourceLayout.StaticSamplers)); + if (this->m_Desc.GraphicsPipeline.InputLayout.LayoutElements != nullptr) + RawAllocator.Free(const_cast<LayoutElement*>(this->m_Desc.GraphicsPipeline.InputLayout.LayoutElements)); } IMPLEMENT_QUERY_INTERFACE_IN_PLACE( IID_PipelineState, TDeviceObjectBase ) @@ -315,8 +326,6 @@ public: } protected: - // TODO: rework this - std::vector<LayoutElement, STDAllocatorRawMem<LayoutElement> > m_LayoutElements; Uint32 m_BufferSlotsUsed = 0; Uint32 m_NumShaders = 0; ///< Number of shaders that this PSO uses diff --git a/Graphics/GraphicsEngineD3D11/include/D3D11TypeConversions.h b/Graphics/GraphicsEngineD3D11/include/D3D11TypeConversions.h index 16e76c9c..3041c8f4 100644 --- a/Graphics/GraphicsEngineD3D11/include/D3D11TypeConversions.h +++ b/Graphics/GraphicsEngineD3D11/include/D3D11TypeConversions.h @@ -158,8 +158,8 @@ D3D11_COMPARISON_FUNC ComparisonFuncToD3D11ComparisonFunc(COMPARISON_FUNCTION Fu void DepthStencilStateDesc_To_D3D11_DEPTH_STENCIL_DESC(const DepthStencilStateDesc &DepthStencilDesc, D3D11_DEPTH_STENCIL_DESC &d3d11DSSDesc); void RasterizerStateDesc_To_D3D11_RASTERIZER_DESC(const RasterizerStateDesc &RasterizerDesc, D3D11_RASTERIZER_DESC &d3d11RSDesc); void BlendStateDesc_To_D3D11_BLEND_DESC(const BlendStateDesc &BSDesc, D3D11_BLEND_DESC &D3D11BSDesc); -void LayoutElements_To_D3D11_INPUT_ELEMENT_DESCs(const std::vector<LayoutElement, STDAllocatorRawMem<LayoutElement> > &LayoutElements, - std::vector<D3D11_INPUT_ELEMENT_DESC, STDAllocatorRawMem<D3D11_INPUT_ELEMENT_DESC> > &D3D11InputElements); +void LayoutElements_To_D3D11_INPUT_ELEMENT_DESCs(const InputLayoutDesc& InputLayout, + std::vector<D3D11_INPUT_ELEMENT_DESC, STDAllocatorRawMem<D3D11_INPUT_ELEMENT_DESC> >& D3D11InputElements); void TextureViewDesc_to_D3D11_SRV_DESC(const TextureViewDesc& TexViewDesc, D3D11_SHADER_RESOURCE_VIEW_DESC &D3D11SRVDesc, Uint32 SampleCount); void TextureViewDesc_to_D3D11_RTV_DESC(const TextureViewDesc& TexViewDesc, D3D11_RENDER_TARGET_VIEW_DESC &D3D11RTVDesc, Uint32 SampleCount); diff --git a/Graphics/GraphicsEngineD3D11/src/D3D11TypeConversions.cpp b/Graphics/GraphicsEngineD3D11/src/D3D11TypeConversions.cpp index 5cade5d1..c730a5e0 100644 --- a/Graphics/GraphicsEngineD3D11/src/D3D11TypeConversions.cpp +++ b/Graphics/GraphicsEngineD3D11/src/D3D11TypeConversions.cpp @@ -72,10 +72,10 @@ void BlendStateDesc_To_D3D11_BLEND_DESC(const BlendStateDesc& BSDesc, D3D11_BLEN } } -void LayoutElements_To_D3D11_INPUT_ELEMENT_DESCs(const std::vector<LayoutElement, STDAllocatorRawMem<LayoutElement> >& LayoutElements, +void LayoutElements_To_D3D11_INPUT_ELEMENT_DESCs(const InputLayoutDesc& InputLayout, std::vector<D3D11_INPUT_ELEMENT_DESC, STDAllocatorRawMem<D3D11_INPUT_ELEMENT_DESC> >& D3D11InputElements) { - LayoutElements_To_D3D_INPUT_ELEMENT_DESCs<D3D11_INPUT_ELEMENT_DESC>(LayoutElements, D3D11InputElements); + LayoutElements_To_D3D_INPUT_ELEMENT_DESCs<D3D11_INPUT_ELEMENT_DESC>(InputLayout, D3D11InputElements); } D3D11_PRIMITIVE_TOPOLOGY TopologyToD3D11Topology(PRIMITIVE_TOPOLOGY Topology) diff --git a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp index d9cc1c21..ff165df6 100644 --- a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp @@ -98,10 +98,11 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pRefCoun "Failed to create D3D11 depth stencil state" ); // Create input layout - if( m_LayoutElements.size() > 0 ) + const auto& InputLayout = m_Desc.GraphicsPipeline.InputLayout; + if (InputLayout.NumElements > 0) { std::vector<D3D11_INPUT_ELEMENT_DESC, STDAllocatorRawMem<D3D11_INPUT_ELEMENT_DESC> > d311InputElements(STD_ALLOCATOR_RAW_MEM(D3D11_INPUT_ELEMENT_DESC, GetRawAllocator(), "Allocator for vector<D3D11_INPUT_ELEMENT_DESC>") ); - LayoutElements_To_D3D11_INPUT_ELEMENT_DESCs(m_LayoutElements, d311InputElements); + LayoutElements_To_D3D11_INPUT_ELEMENT_DESCs(InputLayout, d311InputElements); ID3DBlob* pVSByteCode = m_pVS.RawPtr<ShaderD3D11Impl>()->GetBytecode(); if( !pVSByteCode ) diff --git a/Graphics/GraphicsEngineD3D12/include/D3D12TypeConversions.h b/Graphics/GraphicsEngineD3D12/include/D3D12TypeConversions.h index aab656eb..16c3a3e8 100644 --- a/Graphics/GraphicsEngineD3D12/include/D3D12TypeConversions.h +++ b/Graphics/GraphicsEngineD3D12/include/D3D12TypeConversions.h @@ -40,7 +40,7 @@ void DepthStencilStateDesc_To_D3D12_DEPTH_STENCIL_DESC(const DepthStencilStateDe void RasterizerStateDesc_To_D3D12_RASTERIZER_DESC(const RasterizerStateDesc &RasterizerDesc, D3D12_RASTERIZER_DESC &d3d11RSDesc); void BlendStateDesc_To_D3D12_BLEND_DESC(const BlendStateDesc &BSDesc, D3D12_BLEND_DESC &d3d12BlendDesc); -void LayoutElements_To_D3D12_INPUT_ELEMENT_DESCs(const std::vector<LayoutElement, STDAllocatorRawMem<LayoutElement>> &LayoutElements, +void LayoutElements_To_D3D12_INPUT_ELEMENT_DESCs(const InputLayoutDesc& InputLayout, std::vector<D3D12_INPUT_ELEMENT_DESC, STDAllocatorRawMem<D3D12_INPUT_ELEMENT_DESC> > &d3d12InputElements); void TextureViewDesc_to_D3D12_SRV_DESC(const TextureViewDesc& SRVDesc, D3D12_SHADER_RESOURCE_VIEW_DESC &D3D12SRVDesc, Uint32 SampleCount); diff --git a/Graphics/GraphicsEngineD3D12/src/D3D12TypeConversions.cpp b/Graphics/GraphicsEngineD3D12/src/D3D12TypeConversions.cpp index b533aec9..55d5bfed 100644 --- a/Graphics/GraphicsEngineD3D12/src/D3D12TypeConversions.cpp +++ b/Graphics/GraphicsEngineD3D12/src/D3D12TypeConversions.cpp @@ -128,10 +128,10 @@ void BlendStateDesc_To_D3D12_BLEND_DESC(const BlendStateDesc& BSDesc, D3D12_BLEN } } -void LayoutElements_To_D3D12_INPUT_ELEMENT_DESCs(const std::vector<LayoutElement, STDAllocatorRawMem<LayoutElement>> &LayoutElements, - std::vector<D3D12_INPUT_ELEMENT_DESC, STDAllocatorRawMem<D3D12_INPUT_ELEMENT_DESC> > &d3d12InputElements) +void LayoutElements_To_D3D12_INPUT_ELEMENT_DESCs(const InputLayoutDesc& InputLayout, + std::vector<D3D12_INPUT_ELEMENT_DESC, STDAllocatorRawMem<D3D12_INPUT_ELEMENT_DESC> >& d3d12InputElements) { - LayoutElements_To_D3D_INPUT_ELEMENT_DESCs<D3D12_INPUT_ELEMENT_DESC>(LayoutElements, d3d12InputElements); + LayoutElements_To_D3D_INPUT_ELEMENT_DESCs<D3D12_INPUT_ELEMENT_DESC>(InputLayout, d3d12InputElements); } D3D12_PRIMITIVE_TOPOLOGY TopologyToD3D12Topology(PRIMITIVE_TOPOLOGY Topology) diff --git a/Graphics/GraphicsEngineD3D12/src/PipelineStateD3D12Impl.cpp b/Graphics/GraphicsEngineD3D12/src/PipelineStateD3D12Impl.cpp index 46530860..d1f015f3 100644 --- a/Graphics/GraphicsEngineD3D12/src/PipelineStateD3D12Impl.cpp +++ b/Graphics/GraphicsEngineD3D12/src/PipelineStateD3D12Impl.cpp @@ -218,9 +218,10 @@ PipelineStateD3D12Impl :: PipelineStateD3D12Impl(IReferenceCounters* pRefCo DepthStencilStateDesc_To_D3D12_DEPTH_STENCIL_DESC(GraphicsPipeline.DepthStencilDesc, d3d12PSODesc.DepthStencilState); std::vector<D3D12_INPUT_ELEMENT_DESC, STDAllocatorRawMem<D3D12_INPUT_ELEMENT_DESC>> d312InputElements( STD_ALLOCATOR_RAW_MEM(D3D12_INPUT_ELEMENT_DESC, GetRawAllocator(), "Allocator for vector<D3D12_INPUT_ELEMENT_DESC>") ); - if (m_LayoutElements.size() > 0) + const auto& InputLayout = m_Desc.GraphicsPipeline.InputLayout; + if (InputLayout.NumElements > 0) { - LayoutElements_To_D3D12_INPUT_ELEMENT_DESCs(m_LayoutElements, d312InputElements); + LayoutElements_To_D3D12_INPUT_ELEMENT_DESCs(InputLayout, d312InputElements); d3d12PSODesc.InputLayout.NumElements = static_cast<UINT>(d312InputElements.size()); d3d12PSODesc.InputLayout.pInputElementDescs = d312InputElements.data(); } diff --git a/Graphics/GraphicsEngineD3DBase/include/D3DTypeConversionImpl.h b/Graphics/GraphicsEngineD3DBase/include/D3DTypeConversionImpl.h index 3494c317..75233364 100644 --- a/Graphics/GraphicsEngineD3DBase/include/D3DTypeConversionImpl.h +++ b/Graphics/GraphicsEngineD3DBase/include/D3DTypeConversionImpl.h @@ -370,14 +370,14 @@ namespace Diligent template<typename D3D_INPUT_ELEMENT_DESC> - void LayoutElements_To_D3D_INPUT_ELEMENT_DESCs(const std::vector<LayoutElement, STDAllocatorRawMem<LayoutElement> > &LayoutElements, std::vector<D3D_INPUT_ELEMENT_DESC, STDAllocatorRawMem<D3D_INPUT_ELEMENT_DESC>> &D3DInputElements) + void LayoutElements_To_D3D_INPUT_ELEMENT_DESCs(const InputLayoutDesc& InputLayout, + std::vector<D3D_INPUT_ELEMENT_DESC, STDAllocatorRawMem<D3D_INPUT_ELEMENT_DESC>> &D3DInputElements) { // D3D12_INPUT_ELEMENT_DESC and D3D11_INPUT_ELEMENT_DESC are identical - auto NumElements = LayoutElements.size(); - D3DInputElements.resize(NumElements); - for(Uint32 iElem=0; iElem < NumElements; ++iElem) + D3DInputElements.resize(InputLayout.NumElements); + for(Uint32 iElem=0; iElem < InputLayout.NumElements; ++iElem) { - const auto &CurrElem = LayoutElements[iElem]; + const auto &CurrElem = InputLayout.LayoutElements[iElem]; auto &D3DElem = D3DInputElements[iElem]; D3DElem.SemanticName = "ATTRIB"; D3DElem.SemanticIndex = CurrElem.InputIndex; |
