From 3cd2b47a5dfb4319d9db029ef4f82c59103e9ec3 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Wed, 22 Aug 2018 07:35:02 -0700 Subject: Implemented shader variable access by Index in GL backend --- Graphics/GraphicsEngineOpenGL/include/GLProgram.h | 3 +- .../include/GLProgramResources.h | 122 +++++++++++++-------- Graphics/GraphicsEngineOpenGL/src/GLProgram.cpp | 6 +- .../src/GLProgramResources.cpp | 59 +++++----- Graphics/GraphicsEngineOpenGL/src/ShaderGLImpl.cpp | 25 +++-- .../src/ShaderResourceBindingGLImpl.cpp | 8 +- 6 files changed, 131 insertions(+), 92 deletions(-) (limited to 'Graphics/GraphicsEngineOpenGL') diff --git a/Graphics/GraphicsEngineOpenGL/include/GLProgram.h b/Graphics/GraphicsEngineOpenGL/include/GLProgram.h index 1d72f416..2d29b8fb 100644 --- a/Graphics/GraphicsEngineOpenGL/include/GLProgram.h +++ b/Graphics/GraphicsEngineOpenGL/include/GLProgram.h @@ -44,7 +44,8 @@ namespace Diligent void BindConstantResources(IResourceMapping *pResourceMapping, Uint32 Flags); const GLProgramResources& GetAllResources()const{return m_AllResources;} - GLProgramResources& GetConstantResources(){return m_ConstantResources;} + GLProgramResources& GetConstantResources() {return m_ConstantResources;} + const GLProgramResources& GetConstantResources()const{return m_ConstantResources;} #ifdef VERIFY_RESOURCE_BINDINGS diff --git a/Graphics/GraphicsEngineOpenGL/include/GLProgramResources.h b/Graphics/GraphicsEngineOpenGL/include/GLProgramResources.h index 2d01c261..e27a1c79 100644 --- a/Graphics/GraphicsEngineOpenGL/include/GLProgramResources.h +++ b/Graphics/GraphicsEngineOpenGL/include/GLProgramResources.h @@ -37,28 +37,34 @@ namespace Diligent class GLProgramResources { public: - GLProgramResources(); - GLProgramResources( GLProgramResources&& Program ); + GLProgramResources(){} + GLProgramResources (GLProgramResources&& Program)noexcept; - void LoadUniforms(class RenderDeviceGLImpl *pDeviceGLImpl, - GLuint GLProgram, + GLProgramResources (const GLProgramResources&) = delete; + GLProgramResources& operator = (const GLProgramResources&) = delete; + GLProgramResources& operator = ( GLProgramResources&&) = delete; + + void LoadUniforms(class RenderDeviceGLImpl* pDeviceGLImpl, + GLuint GLProgram, const SHADER_VARIABLE_TYPE DefaultVariableType, - const ShaderVariableDesc *VariableDesc, - Uint32 NumVars, - const StaticSamplerDesc *StaticSamplers, - Uint32 NumStaticSamplers); + const ShaderVariableDesc* VariableDesc, + Uint32 NumVars, + const StaticSamplerDesc* StaticSamplers, + Uint32 NumStaticSamplers); void Clone(const GLProgramResources& SrcLayout, - SHADER_VARIABLE_TYPE *VarTypes, - Uint32 NumVarTypes, - IObject &Owner); + SHADER_VARIABLE_TYPE* VarTypes, + Uint32 NumVarTypes, + IObject& Owner); struct GLProgramVariableBase { - GLProgramVariableBase( const Char* _Name, size_t _ArraySize, SHADER_VARIABLE_TYPE _VarType) : - Name( _Name ), + GLProgramVariableBase(String _Name, + size_t _ArraySize, + SHADER_VARIABLE_TYPE _VarType) : + Name ( std::move(_Name) ), pResources(_ArraySize), - VarType(_VarType) + VarType (_VarType) { VERIFY_EXPR(_ArraySize >= 1); } @@ -74,15 +80,18 @@ namespace Diligent return ComputeHash(static_cast(VarType), pResources.size()); } - String Name; + String Name; std::vector< RefCntAutoPtr > pResources; - SHADER_VARIABLE_TYPE VarType; + const SHADER_VARIABLE_TYPE VarType; }; struct UniformBufferInfo : GLProgramVariableBase { - UniformBufferInfo(const Char* _Name, size_t _ArraySize, SHADER_VARIABLE_TYPE _VarType, GLint _Index) : - GLProgramVariableBase(_Name, _ArraySize, _VarType), + UniformBufferInfo(String _Name, + size_t _ArraySize, + SHADER_VARIABLE_TYPE _VarType, + GLint _Index) : + GLProgramVariableBase(std::move(_Name), _ArraySize, _VarType), Index(_Index) {} @@ -97,16 +106,21 @@ namespace Diligent return ComputeHash(Index, GLProgramVariableBase::GetHash()); } - GLuint Index; + const GLuint Index; }; std::vector& GetUniformBlocks(){ return m_UniformBlocks; } struct SamplerInfo : GLProgramVariableBase { - SamplerInfo(const Char* _Name, size_t _ArraySize, SHADER_VARIABLE_TYPE _VarType, GLint _Location, GLenum _Type, class SamplerGLImpl *_pStaticSampler) : - GLProgramVariableBase(_Name, _ArraySize, _VarType), - Location(_Location), - Type(_Type), + SamplerInfo(String _Name, + size_t _ArraySize, + SHADER_VARIABLE_TYPE _VarType, + GLint _Location, + GLenum _Type, + class SamplerGLImpl* _pStaticSampler) : + GLProgramVariableBase(std::move(_Name), _ArraySize, _VarType), + Location (_Location), + Type (_Type), pStaticSampler(_pStaticSampler) {} @@ -122,18 +136,22 @@ namespace Diligent return ComputeHash(Location, Type, GLProgramVariableBase::GetHash()); } - GLint Location; - GLenum Type; + const GLint Location; + const GLenum Type; RefCntAutoPtr pStaticSampler; }; std::vector& GetSamplers(){ return m_Samplers; } struct ImageInfo : GLProgramVariableBase { - ImageInfo(const Char* _Name, size_t _ArraySize, SHADER_VARIABLE_TYPE _VarType, GLint _BindingPoint, GLenum _Type) : - GLProgramVariableBase(_Name, _ArraySize, _VarType), + ImageInfo(String _Name, + size_t _ArraySize, + SHADER_VARIABLE_TYPE _VarType, + GLint _BindingPoint, + GLenum _Type) : + GLProgramVariableBase(std::move(_Name), _ArraySize, _VarType), BindingPoint(_BindingPoint), - Type(_Type) + Type (_Type) {} bool IsCompatibleWith(const ImageInfo& II)const @@ -148,15 +166,18 @@ namespace Diligent return ComputeHash(BindingPoint, Type, GLProgramVariableBase::GetHash()); } - GLint BindingPoint; - GLenum Type; + const GLint BindingPoint; + const GLenum Type; }; std::vector& GetImages(){ return m_Images; } struct StorageBlockInfo : GLProgramVariableBase { - StorageBlockInfo(const Char* _Name, size_t _ArraySize, SHADER_VARIABLE_TYPE _VarType, GLint _Binding) : - GLProgramVariableBase(_Name, _ArraySize, _VarType), + StorageBlockInfo(String _Name, + size_t _ArraySize, + SHADER_VARIABLE_TYPE _VarType, + GLint _Binding) : + GLProgramVariableBase(std::move(_Name), _ArraySize, _VarType), Binding(_Binding) {} @@ -171,19 +192,20 @@ namespace Diligent return ComputeHash(Binding, GLProgramVariableBase::GetHash()); } - GLint Binding; + const GLint Binding; }; std::vector& GetStorageBlocks(){ return m_StorageBlocks; } struct CGLShaderVariable : ShaderVariableBase { - CGLShaderVariable( IObject &Owner, GLProgramResources::GLProgramVariableBase &ProgVar ) : - ShaderVariableBase( Owner ), - ProgramVar(ProgVar) + CGLShaderVariable(IObject& Owner, GLProgramResources::GLProgramVariableBase& ProgVar, Uint32 _Index) : + ShaderVariableBase(Owner), + ProgramVar (ProgVar), + VariableIndex (_Index) {} - virtual void Set( IDeviceObject *pObject )override final + virtual void Set(IDeviceObject *pObject)override final { ProgramVar.pResources[0] = pObject; } @@ -211,12 +233,12 @@ namespace Diligent virtual Uint32 GetIndex()const override final { - UNSUPPORTED("Not yet implemented"); - return 0; + return VariableIndex; } private: - GLProgramVariableBase &ProgramVar; + GLProgramVariableBase& ProgramVar; + const Uint32 VariableIndex; }; void BindResources(IResourceMapping *pResourceMapping, Uint32 Flags); @@ -225,25 +247,33 @@ namespace Diligent void dbgVerifyResourceBindings(); #endif - IShaderVariable* GetShaderVariable( const Char* Name ); + IShaderVariable* GetShaderVariable(const Char* Name); + IShaderVariable* GetShaderVariable(Uint32 Index) + { + return Index < m_VariablesByIndex.size() ? m_VariablesByIndex[Index] : nullptr; + } const std::unordered_map& GetVariables(){return m_VariableHash;} + + Uint32 GetVariableCount()const + { + return static_cast(m_VariableHash.size()); + } bool IsCompatibleWith(const GLProgramResources& Res)const; size_t GetHash()const; private: - const GLProgramResources& operator = (const GLProgramResources& Program); - void InitVariables(IObject &Owner); std::vector m_UniformBlocks; - std::vector< SamplerInfo > m_Samplers; - std::vector< ImageInfo > m_Images; - std::vector< StorageBlockInfo > m_StorageBlocks; + std::vector m_Samplers; + std::vector m_Images; + std::vector m_StorageBlocks; /// Hash map to look up shader variables by name. std::unordered_map m_VariableHash; + std::vector m_VariablesByIndex; // When adding new member DO NOT FORGET TO UPDATE GLProgramResources( GLProgramResources&& ProgramResources )!!! }; } diff --git a/Graphics/GraphicsEngineOpenGL/src/GLProgram.cpp b/Graphics/GraphicsEngineOpenGL/src/GLProgram.cpp index 487ebcf9..dbf9914d 100644 --- a/Graphics/GraphicsEngineOpenGL/src/GLProgram.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/GLProgram.cpp @@ -33,9 +33,9 @@ namespace Diligent {} GLProgram::GLProgram( GLProgram&& Program ): - GLObjectWrappers::GLProgramObj( std::move( Program ) ), - m_AllResources( std::move( Program.m_AllResources) ), - m_ConstantResources( std::move( Program.m_ConstantResources) ) + GLObjectWrappers::GLProgramObj(std::move(Program ) ), + m_AllResources (std::move(Program.m_AllResources) ), + m_ConstantResources (std::move(Program.m_ConstantResources)) {} void GLProgram::InitResources(RenderDeviceGLImpl* pDeviceGLImpl, diff --git a/Graphics/GraphicsEngineOpenGL/src/GLProgramResources.cpp b/Graphics/GraphicsEngineOpenGL/src/GLProgramResources.cpp index 90230428..0dac8ea1 100644 --- a/Graphics/GraphicsEngineOpenGL/src/GLProgramResources.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/GLProgramResources.cpp @@ -27,16 +27,13 @@ namespace Diligent { - GLProgramResources::GLProgramResources() - { - } - - GLProgramResources::GLProgramResources( GLProgramResources&& Program ): - m_UniformBlocks( std::move( Program.m_UniformBlocks ) ), - m_Samplers( std::move( Program.m_Samplers ) ), - m_Images( std::move( Program.m_Images ) ), - m_StorageBlocks( std::move( Program.m_StorageBlocks ) ), - m_VariableHash(std::move( Program.m_VariableHash)) + GLProgramResources::GLProgramResources( GLProgramResources&& Program )noexcept : + m_UniformBlocks (std::move(Program.m_UniformBlocks)), + m_Samplers (std::move(Program.m_Samplers) ), + m_Images (std::move(Program.m_Images) ), + m_StorageBlocks (std::move(Program.m_StorageBlocks)), + m_VariableHash (std::move(Program.m_VariableHash) ), + m_VariablesByIndex(std::move(Program.m_VariablesByIndex) ) { } @@ -361,32 +358,32 @@ namespace Diligent } void GLProgramResources::Clone(const GLProgramResources& SrcLayout, - SHADER_VARIABLE_TYPE *VarTypes, - Uint32 NumVarTypes, - IObject &Owner) + SHADER_VARIABLE_TYPE* VarTypes, + Uint32 NumVarTypes, + IObject& Owner) { - for (auto ub = SrcLayout.m_UniformBlocks.begin(); ub != SrcLayout.m_UniformBlocks.end(); ++ub) + for (auto& ub : SrcLayout.m_UniformBlocks) { - if(CheckType(ub->VarType, VarTypes, NumVarTypes)) - m_UniformBlocks.emplace_back( ub->Name.c_str(), ub->pResources.size(), ub->VarType, ub->Index ); + if(CheckType(ub.VarType, VarTypes, NumVarTypes)) + m_UniformBlocks.emplace_back( ub.Name, ub.pResources.size(), ub.VarType, ub.Index ); } - for (auto sam = SrcLayout.m_Samplers.begin(); sam != SrcLayout.m_Samplers.end(); ++sam) + for (auto& sam : SrcLayout.m_Samplers) { - if(CheckType(sam->VarType, VarTypes, NumVarTypes)) - m_Samplers.emplace_back( sam->Name.c_str(), sam->pResources.size(), sam->VarType, sam->Location, sam->Type, const_cast(sam->pStaticSampler.RawPtr()) ); + if(CheckType(sam.VarType, VarTypes, NumVarTypes)) + m_Samplers.emplace_back( sam.Name, sam.pResources.size(), sam.VarType, sam.Location, sam.Type, const_cast(sam.pStaticSampler.RawPtr()) ); } - for (auto img = SrcLayout.m_Images.begin(); img != SrcLayout.m_Images.end(); ++img) + for (auto& img : SrcLayout.m_Images) { - if(CheckType(img->VarType, VarTypes, NumVarTypes)) - m_Images.emplace_back( img->Name.c_str(), img->pResources.size(), img->VarType, img->BindingPoint, img->Type ); + if(CheckType(img.VarType, VarTypes, NumVarTypes)) + m_Images.emplace_back( img.Name, img.pResources.size(), img.VarType, img.BindingPoint, img.Type ); } - for (auto sb = SrcLayout.m_StorageBlocks.begin(); sb != SrcLayout.m_StorageBlocks.end(); ++sb) + for (auto& sb : SrcLayout.m_StorageBlocks) { - if(CheckType(sb->VarType, VarTypes, NumVarTypes)) - m_StorageBlocks.emplace_back( sb->Name.c_str(), sb->pResources.size(), sb->VarType, sb->Binding ); + if(CheckType(sb.VarType, VarTypes, NumVarTypes)) + m_StorageBlocks.emplace_back( sb.Name, sb.pResources.size(), sb.VarType, sb.Binding ); } InitVariables(Owner); @@ -396,12 +393,18 @@ namespace Diligent { // After all program resources are loaded, we can populate shader variable hash map. // The map contains raw pointers, but none of the arrays will ever change. + auto TotalVars = m_UniformBlocks.size() + m_Samplers.size() + m_Images.size() + m_StorageBlocks.size(); + m_VariablesByIndex.reserve(TotalVars); + m_VariableHash.reserve(TotalVars); #define STORE_SHADER_VARIABLES(ResArr)\ { \ - auto& Arr = ResArr; \ - for( auto it = Arr.begin(); it != Arr.end(); ++it ) \ + for( auto& ProgVar : ResArr) \ + { \ /* HashMapStringKey will make a copy of the string*/ \ - m_VariableHash.insert( std::make_pair( Diligent::HashMapStringKey(it->Name), CGLShaderVariable(Owner, *it) ) ); \ + auto it = m_VariableHash.insert( std::make_pair( Diligent::HashMapStringKey(ProgVar.Name), CGLShaderVariable(Owner, ProgVar, static_cast(m_VariablesByIndex.size())) ) ); \ + VERIFY_EXPR(it.second); \ + m_VariablesByIndex.push_back(&it.first->second); \ + } \ } STORE_SHADER_VARIABLES(m_UniformBlocks) diff --git a/Graphics/GraphicsEngineOpenGL/src/ShaderGLImpl.cpp b/Graphics/GraphicsEngineOpenGL/src/ShaderGLImpl.cpp index 1a3ebfda..bb190b49 100644 --- a/Graphics/GraphicsEngineOpenGL/src/ShaderGLImpl.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/ShaderGLImpl.cpp @@ -189,24 +189,29 @@ void ShaderGLImpl::BindResources( IResourceMapping* pResourceMapping, Uint32 Fla IShaderVariable* ShaderGLImpl::GetShaderVariable( const Char* Name ) { - if( !m_GlProgObj ) - { - UNSUPPORTED( "Shader variable queries are currently supported for separable programs only" ); - } - - return m_GlProgObj.GetConstantResources().GetShaderVariable(Name); + DEV_CHECK_ERR(m_GlProgObj, "Shader variable queries are currently supported for separable programs only"); + if( m_GlProgObj ) + return m_GlProgObj.GetConstantResources().GetShaderVariable(Name); + else + return nullptr; } Uint32 ShaderGLImpl::GetVariableCount() const { - UNSUPPORTED("Not yet implemented"); - return 0; + DEV_CHECK_ERR(m_GlProgObj, "Shader variable queries are currently supported for separable programs only"); + if( m_GlProgObj ) + return m_GlProgObj.GetConstantResources().GetVariableCount(); + else + return 0; } IShaderVariable* ShaderGLImpl::GetShaderVariable(Uint32 Index) { - UNSUPPORTED("Not yet implemented"); - return nullptr; + DEV_CHECK_ERR(m_GlProgObj, "Shader variable queries are currently supported for separable programs only"); + if( m_GlProgObj ) + return m_GlProgObj.GetConstantResources().GetShaderVariable(Index); + else + return 0; } } diff --git a/Graphics/GraphicsEngineOpenGL/src/ShaderResourceBindingGLImpl.cpp b/Graphics/GraphicsEngineOpenGL/src/ShaderResourceBindingGLImpl.cpp index 3d2b7198..4041afed 100644 --- a/Graphics/GraphicsEngineOpenGL/src/ShaderResourceBindingGLImpl.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/ShaderResourceBindingGLImpl.cpp @@ -89,14 +89,14 @@ IShaderVariable *ShaderResourceBindingGLImpl::GetVariable(SHADER_TYPE ShaderType Uint32 ShaderResourceBindingGLImpl::GetVariableCount(SHADER_TYPE ShaderType) const { - UNSUPPORTED("Not yet implemented"); - return 0; + auto ShaderInd = GetShaderTypeIndex(ShaderType); + return m_DynamicProgResources[ShaderInd].GetVariableCount(); } IShaderVariable* ShaderResourceBindingGLImpl::GetVariable(SHADER_TYPE ShaderType, Uint32 Index) { - UNSUPPORTED("Not yet implemented"); - return 0; + auto ShaderInd = GetShaderTypeIndex(ShaderType); + return m_DynamicProgResources[ShaderInd].GetShaderVariable(Index); } static GLProgramResources NullProgramResources; -- cgit v1.2.3