From 60e2802464102539b3e0302aa899e22b43b97c0b Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Mon, 28 May 2018 21:16:33 -0700 Subject: Moved vertex buffer stride definition from IDeviceContext::SetVertexBuffers() to vertex layout description --- .../GraphicsEngine/include/DeviceContextBase.h | 13 ++----- .../GraphicsEngine/include/PipelineStateBase.h | 44 +++++++++++++++++++--- Graphics/GraphicsEngine/interface/DeviceContext.h | 6 --- Graphics/GraphicsEngine/interface/InputLayout.h | 29 +++++++++----- 4 files changed, 60 insertions(+), 32 deletions(-) (limited to 'Graphics/GraphicsEngine') diff --git a/Graphics/GraphicsEngine/include/DeviceContextBase.h b/Graphics/GraphicsEngine/include/DeviceContextBase.h index 29bd2398..8e238bc7 100644 --- a/Graphics/GraphicsEngine/include/DeviceContextBase.h +++ b/Graphics/GraphicsEngine/include/DeviceContextBase.h @@ -49,12 +49,7 @@ struct VertexStreamInfo { /// Strong reference to the buffer object RefCntAutoPtr pBuffer; - Uint32 Stride; ///< Stride in bytes - Uint32 Offset; ///< Offset in bytes - VertexStreamInfo() : - Stride( 0 ), - Offset( 0 ) - {} + Uint32 Offset = 0; ///< Offset in bytes }; /// Base implementation of the device context. @@ -89,7 +84,7 @@ public: /// Base implementation of IDeviceContext::SetVertexBuffers(); validates parameters and /// caches references to the buffers. - inline virtual void SetVertexBuffers( Uint32 StartSlot, Uint32 NumBuffersSet, IBuffer **ppBuffers, Uint32 *pStrides, Uint32 *pOffsets, Uint32 Flags )override = 0; + inline virtual void SetVertexBuffers( Uint32 StartSlot, Uint32 NumBuffersSet, IBuffer **ppBuffers, Uint32 *pOffsets, Uint32 Flags )override = 0; inline virtual void InvalidateState()override = 0; @@ -204,7 +199,7 @@ protected: template -inline void DeviceContextBase :: SetVertexBuffers( Uint32 StartSlot, Uint32 NumBuffersSet, IBuffer **ppBuffers, Uint32 *pStrides, Uint32 *pOffsets, Uint32 Flags ) +inline void DeviceContextBase :: SetVertexBuffers( Uint32 StartSlot, Uint32 NumBuffersSet, IBuffer **ppBuffers, Uint32 *pOffsets, Uint32 Flags ) { if( StartSlot >= MaxBufferSlots ) { @@ -230,7 +225,6 @@ inline void DeviceContextBase :: SetVertexBuffers( Uint32 StartSl { auto &CurrStream = m_VertexStreams[StartSlot + Buff]; CurrStream.pBuffer = RefCntAutoPtr( ppBuffers ? ppBuffers[Buff] : nullptr ); - CurrStream.Stride = pStrides ? pStrides[Buff] : 0; CurrStream.Offset = pOffsets ? pOffsets[Buff] : 0; #ifdef DEBUG_CHECKS if( CurrStream.pBuffer ) @@ -558,7 +552,6 @@ inline void DeviceContextBase :: ClearStateCache() { VERIFY(m_VertexStreams[stream].pBuffer == nullptr, "Unexpected non-null buffer"); VERIFY(m_VertexStreams[stream].Offset == 0, "Unexpected non-zero offset"); - VERIFY(m_VertexStreams[stream].Stride == 0, "Unexpected non-zero stride"); } #endif m_NumVertexStreams = 0; diff --git a/Graphics/GraphicsEngine/include/PipelineStateBase.h b/Graphics/GraphicsEngine/include/PipelineStateBase.h index 7348c32c..22c805f3 100644 --- a/Graphics/GraphicsEngine/include/PipelineStateBase.h +++ b/Graphics/GraphicsEngine/include/PipelineStateBase.h @@ -26,6 +26,7 @@ /// \file /// Implementation of the Diligent::PipelineStateBase template class +#include #include "PipelineState.h" #include "DeviceObjectBase.h" #include "STDAllocator.h" @@ -110,19 +111,21 @@ public: this->m_Desc.GraphicsPipeline.InputLayout.LayoutElements = m_LayoutElements.data(); // Correct description and compute offsets and tight strides + decltype(m_Strides) TightStrides = {}; for( auto It = m_LayoutElements.begin(); It != m_LayoutElements.end(); ++It ) { if( It->ValueType == VT_FLOAT32 || It->ValueType == VT_FLOAT16 ) It->IsNormalized = false; // Floating point values cannot be normalized auto BuffSlot = It->BufferSlot; - if( BuffSlot >= _countof(m_TightStrides) ) + if( BuffSlot >= m_Strides.size() ) { - UNEXPECTED("Buffer slot (", BuffSlot, ") exceeds the limit (", _countof(m_TightStrides), ")"); + UNEXPECTED("Buffer slot (", BuffSlot, ") exceeds the limit (", m_Strides.size(), ")"); continue; } + m_BufferSlotsUsed = std::max(m_BufferSlotsUsed, BuffSlot + 1); - auto &CurrStride = m_TightStrides[BuffSlot]; + auto &CurrStride = TightStrides[BuffSlot]; if( It->RelativeOffset < CurrStride ) { if( It->RelativeOffset == 0 ) @@ -131,8 +134,31 @@ public: UNEXPECTED( "Overlapping layout elements" ); } + if(It->Stride != 0) + { + if(m_Strides[BuffSlot] != 0) + { + VERIFY(m_Strides[BuffSlot] == It->Stride, "Inconsistent strides specified for buffer slot ", BuffSlot, + ". Current value: ", m_Strides[BuffSlot], ". New value: ", It->Stride); + } + m_Strides[BuffSlot] = It->Stride; + } + CurrStride += It->NumComponents * GetValueSize( It->ValueType ); } + + for( auto It = m_LayoutElements.begin(); It != m_LayoutElements.end(); ++It ) + { + auto BuffSlot = It->BufferSlot; + if(m_Strides[BuffSlot] == 0) + { + m_Strides[BuffSlot] = TightStrides[BuffSlot]; + } + else + { + VERIFY(m_Strides[BuffSlot] >= TightStrides[BuffSlot], "Stride (", m_Strides[BuffSlot], ") explicitly specified for slot ", BuffSlot, " is smaller than the total element size (", TightStrides[BuffSlot], ")"); + } + } } ~PipelineStateBase() @@ -158,9 +184,14 @@ public: IMPLEMENT_QUERY_INTERFACE_IN_PLACE( IID_PipelineState, TDeviceObjectBase ) - virtual const Uint32* GetTightStrides()const + virtual const Uint32* GetBufferStrides()const { - return m_TightStrides; + return m_Strides.data(); + } + + Uint32 GetNumBufferSlotsUsed()const + { + return m_BufferSlotsUsed; } IShader* GetVS(){return m_pVS;} @@ -183,10 +214,11 @@ public: protected: std::vector > m_LayoutElements; + Uint32 m_BufferSlotsUsed = 0; // The size of this array must be equal to the // maximum number of buffer slots, because a layout // element can refer to any input slot - Uint32 m_TightStrides[MaxBufferSlots] = {}; + std::array m_Strides = {}; RefCntAutoPtr m_pVS; ///< Strong reference to the vertex shader RefCntAutoPtr m_pPS; ///< Strong reference to the pixel shader diff --git a/Graphics/GraphicsEngine/interface/DeviceContext.h b/Graphics/GraphicsEngine/interface/DeviceContext.h index 0e45b598..460291a5 100644 --- a/Graphics/GraphicsEngine/interface/DeviceContext.h +++ b/Graphics/GraphicsEngine/interface/DeviceContext.h @@ -341,11 +341,6 @@ public: /// \param [in] NumBuffersSet - The number of vertex buffers in the array. /// \param [in] ppBuffers - A pointer to an array of vertex buffers. // The vertex buffers must have been created with the Diligent::BIND_VERTEX_BUFFER flag. - /// \param [in] pStrides - Pointer to an array of stride values; one stride value for each buffer - /// in the vertex-buffer array. Each stride is the size (in bytes) of the - /// elements that are to be used from that vertex buffer. - /// If this parameter is nullptr, tight strides from the input layout - /// will be used for each buffer. See IPipelineState::GetTightStrides(). /// \param [in] pOffsets - Pointer to an array of offset values; one offset value for each buffer /// in the vertex-buffer array. Each offset is the number of bytes between /// the first element of a vertex buffer and the first element that will be @@ -360,7 +355,6 @@ public: virtual void SetVertexBuffers(Uint32 StartSlot, Uint32 NumBuffersSet, IBuffer **ppBuffers, - Uint32 *pStrides, Uint32 *pOffsets, Uint32 Flags) = 0; diff --git a/Graphics/GraphicsEngine/interface/InputLayout.h b/Graphics/GraphicsEngine/interface/InputLayout.h index 598cedd4..7900fa74 100644 --- a/Graphics/GraphicsEngine/interface/InputLayout.h +++ b/Graphics/GraphicsEngine/interface/InputLayout.h @@ -56,11 +56,18 @@ struct LayoutElement Bool IsNormalized; /// Relative offset, in bytes, to the element bits. - /// If this value is zero, the offset will be computed automatically - /// assuming that all previous elements in the same buffer slot a tightly packed. + /// If this value is zero, the offset will be computed automatically assuming + /// that all previous elements in the same buffer slot are tightly packed. /// Overlapping elements are not allowed. Uint32 RelativeOffset; + /// Stride, in bytes, between two elements, for this buffer slot. + /// If this value is zero, stride will be computed automatically assuming + /// that all elements in the same buffer slot are tightly packed. + /// If buffer slot contains multiple layout elements, they all must use + /// the same stride or zero. + Uint32 Stride; + /// Input frequency enum FREQUENCY : Int32 { @@ -82,20 +89,22 @@ struct LayoutElement Uint32 InstanceDataStepRate; /// Initializes the structure - LayoutElement(Uint32 _InputIndex = 0, - Uint32 _BufferSlot = 0, - Uint32 _NumComponents = 0, - VALUE_TYPE _ValueType = VT_FLOAT32, - Bool _IsNormalized = True, - Uint32 _RelativeOffset = 0, - FREQUENCY _Frequency = FREQUENCY_PER_VERTEX, - Uint32 _InstanceDataStepRate = 1) : + LayoutElement(Uint32 _InputIndex = 0, + Uint32 _BufferSlot = 0, + Uint32 _NumComponents = 0, + VALUE_TYPE _ValueType = VT_FLOAT32, + Bool _IsNormalized = True, + Uint32 _RelativeOffset = 0, + Uint32 _Stride = 0, + FREQUENCY _Frequency = FREQUENCY_PER_VERTEX, + Uint32 _InstanceDataStepRate = 1) : InputIndex(_InputIndex), BufferSlot(_BufferSlot), NumComponents(_NumComponents), ValueType(_ValueType), IsNormalized(_IsNormalized), RelativeOffset(_RelativeOffset), + Stride(_Stride), Frequency(_Frequency), InstanceDataStepRate(_InstanceDataStepRate) {} -- cgit v1.2.3