diff options
Diffstat (limited to 'RenderScript/src/PSODescParser.cpp')
| -rw-r--r-- | RenderScript/src/PSODescParser.cpp | 352 |
1 files changed, 350 insertions, 2 deletions
diff --git a/RenderScript/src/PSODescParser.cpp b/RenderScript/src/PSODescParser.cpp index b1da543..4d2b143 100644 --- a/RenderScript/src/PSODescParser.cpp +++ b/RenderScript/src/PSODescParser.cpp @@ -27,9 +27,248 @@ #include "RasterizerStateDescParser.h" #include "DepthStencilStateDescParser.h" #include "InputLayoutDescParser.h" +#include "SamplerParser.h" namespace Diligent { + class ShaderVariableTypeEnumMapping : public EnumMapping < Diligent::SHADER_RESOURCE_VARIABLE_TYPE > + { + public: + ShaderVariableTypeEnumMapping() + { + DEFINE_ENUM_ELEMENT_MAPPING( (*this), SHADER_RESOURCE_VARIABLE_TYPE_STATIC ); + DEFINE_ENUM_ELEMENT_MAPPING( (*this), SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE ); + DEFINE_ENUM_ELEMENT_MAPPING( (*this), SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC ); + } + }; + + class ShaderResourceVariableDescArrayParser; + template<> + class MemberBinder<ShaderResourceVariableDescArrayParser> : public MemberBinderBase + { + public: + MemberBinder( size_t VariableDescOffset, + size_t NumVariablesOffset, + size_t VarDescBufferOffset, + size_t VarNamesBufferOffset ) : + MemberBinderBase( VariableDescOffset ), + m_NumVariablesOffset(NumVariablesOffset), + m_VarDescBufferOffset(VarDescBufferOffset), + m_VarNamesBufferOffset(VarNamesBufferOffset) + { + DEFINE_ENUM_BINDER( m_Bindings, ShaderResourceVariableDesc, Type, m_ShaderVarTypeEnumMapping ); + DEFINE_FLAGS_BINDER( m_Bindings, ShaderResourceVariableDesc, ShaderStages, SHADER_TYPE, m_ShaderTypeEnumMapping ); + } + + virtual void GetValue( lua_State *L, const void* pBasePointer )override + { + // Use raw pointer to push the value to Lua because the buffer + // most likely does not exist + const auto &VarDesc = GetMemberByOffest<ShaderResourceVariableDesc*>( pBasePointer, m_MemberOffset); + const auto &NumVars = GetMemberByOffest<Uint32>( pBasePointer, m_NumVariablesOffset); + + PushLuaArray( L, VarDesc, VarDesc + NumVars, [&]( const ShaderResourceVariableDesc &VarDesc ) + { + // Push variable type. The function will leave the new table on top + // of the stack + PushLuaTable( L, &VarDesc, m_Bindings ); // Stack: +1 + + // Push name into the same table + lua_pushstring( L, "Name" ); // Stack: +2 + lua_pushstring( L, VarDesc.Name); // Stack: +3 + lua_settable( L, -3 ); // Stack: +1 + } + ); + } + + virtual void SetValue( lua_State *L, int Index, void* pBasePointer )override + { + auto &ShaderVarDescBuffer = GetMemberByOffest<std::vector<ShaderResourceVariableDesc>>( pBasePointer, m_VarDescBufferOffset); + auto &ShaderNamesBuffer = GetMemberByOffest<std::vector<String>>( pBasePointer, m_VarNamesBufferOffset); + + ParseLuaArray( L, Index, pBasePointer, + [&]( void* _pBasePointer, int StackIndex, int NewArrayIndex ) + { + VERIFY_EXPR( pBasePointer == _pBasePointer ); + + auto CurrIndex = ShaderVarDescBuffer.size(); + if( static_cast<int>(CurrIndex) != NewArrayIndex - 1 ) + SCRIPT_PARSING_ERROR( L, "Explicit array indices are not allowed in shader name description. Provided index ", NewArrayIndex - 1, " conflicts with actual index ", CurrIndex, "." ); + ShaderVarDescBuffer.resize( CurrIndex + 1 ); + ShaderNamesBuffer.resize( CurrIndex + 1 ); + ParseLuaTable( L, StackIndex, &(ShaderVarDescBuffer)[CurrIndex], + [&](int TblStackInd, void* __pBasePointer, const char *Key) + { + auto Binding = m_Bindings.find( Key ); + if( Binding != m_Bindings.end() ) + { + Binding->second->SetValue( L, TblStackInd, __pBasePointer ); + } + else if (strcmp(Key, "Name") == 0) + { + auto Name = ReadValueFromLua<const Char*>(L, TblStackInd); + ShaderNamesBuffer[CurrIndex] = Name; + } + else + { + SCRIPT_PARSING_ERROR( L, "Unknown Member \"", Key, '\"' ); + } + } + ); + + if( ShaderNamesBuffer[CurrIndex].length() == 0 ) + SCRIPT_PARSING_ERROR(L, "Missing shader variable name") + } + ); + + for(size_t v=0; v < ShaderVarDescBuffer.size(); ++v) + { + ShaderVarDescBuffer[v].Name = ShaderNamesBuffer[v].c_str(); + } + + auto &VarDesc = GetMemberByOffest<ShaderResourceVariableDesc*>( pBasePointer, m_MemberOffset); + auto &NumVars = GetMemberByOffest<Uint32>( pBasePointer, m_NumVariablesOffset); + NumVars = static_cast<Uint32>(ShaderVarDescBuffer.size()); + VarDesc = NumVars ? ShaderVarDescBuffer.data() : nullptr; + } + private: + BindingsMapType m_Bindings; + ShaderVariableTypeEnumMapping m_ShaderVarTypeEnumMapping; + ShaderTypeEnumMapping m_ShaderTypeEnumMapping; + size_t m_NumVariablesOffset; + size_t m_VarDescBufferOffset; + size_t m_VarNamesBufferOffset; + }; + + + class StaticSamplerDescArrayParser; + template<> + class MemberBinder<StaticSamplerDescArrayParser> : public MemberBinderBase + { + public: + MemberBinder( size_t StaticSamplerDescOffset, + size_t NumStaticSamplersOffset, + size_t StaticSamplersBufferOffset, + size_t StaticSamplerTexNamesBufferOffset ) : + MemberBinderBase( StaticSamplerDescOffset ), + m_NumStaticSamplersOffset(NumStaticSamplersOffset), + m_StaticSamplersBufferOffset(StaticSamplersBufferOffset), + m_StaticSamplerTexNamesBufferOffset(StaticSamplerTexNamesBufferOffset) + { + DEFINE_FLAGS_BINDER( m_Bindings, StaticSamplerDesc, ShaderStages, SHADER_TYPE, m_ShaderTypeEnumMapping ); + InitSamplerParserBindings<SamplerDesc>(m_Bindings, m_FilterTypeEnumMapping, m_TexAddrModeEnumMapping, m_CmpFuncEnumMapping); + } + + virtual void GetValue( lua_State *L, const void* pBasePointer )override + { + // Use raw pointer to push the value to Lua because the buffer + // most likely does not exist + const auto &StaticSamplers = GetMemberByOffest<StaticSamplerDesc*>( pBasePointer, m_MemberOffset); + const auto &NumStaticSamplers = GetMemberByOffest<Uint32>( pBasePointer, m_NumStaticSamplersOffset); + + PushLuaArray( L, StaticSamplers, StaticSamplers + NumStaticSamplers, [&]( const StaticSamplerDesc &SamDesc ) + { + // Push new table to hold variables of StaticSamplerDesc struct + lua_newtable(L); // Stack: +1 + + // Push "Desc" field + lua_pushstring( L, "Desc" ); // Stack: +2 + // Push members of StaticSamplerDesc::Desc. The function will leave new table on top + // of the stack + PushLuaTable( L, &SamDesc.Desc, m_Bindings ); // Stack: +3 + // Push the table from the top into the parent table + lua_settable( L, -3 ); // Stack: +1 + + // Push "SamplerOrTextureName" field + lua_pushstring( L, "SamplerOrTextureName" ); // Stack: +2 + lua_pushstring( L, SamDesc.SamplerOrTextureName); // Stack: +3 + lua_settable( L, -3 ); // Stack: +1 + } + ); + } + + virtual void SetValue( lua_State *L, int Index, void* pBasePointer )override + { + auto &StaticSamplersBuffer = GetMemberByOffest<std::vector<StaticSamplerDesc>>( pBasePointer, m_StaticSamplersBufferOffset); + auto &StaticSamplerTexNamesBuffer = GetMemberByOffest<std::vector<String>>( pBasePointer, m_StaticSamplerTexNamesBufferOffset); + + ParseLuaArray( L, Index, pBasePointer, + [&]( void* _pBasePointer, int StackIndex, int NewArrayIndex ) + { + VERIFY_EXPR( pBasePointer == _pBasePointer ); + + auto CurrIndex = StaticSamplersBuffer.size(); + if(static_cast<int>(CurrIndex) != NewArrayIndex - 1 ) + SCRIPT_PARSING_ERROR( L, "Explicit array indices are not allowed in static sampler description. Provided index ", NewArrayIndex - 1, " conflicts with actual index ", CurrIndex, "." ); + StaticSamplersBuffer.resize( CurrIndex + 1 ); + StaticSamplerTexNamesBuffer.resize( CurrIndex + 1 ); + ParseLuaTable( L, StackIndex, &(StaticSamplersBuffer)[CurrIndex], + [&](int TblStackInd, void* __pBasePointer, const char *Key) + { + if (strcmp(Key, "Desc") == 0) + { + ParseLuaTable( L, StackIndex, &(StaticSamplersBuffer)[CurrIndex].Desc, + [&](int TblStackInd, void* __pBasePointer, const char *Key) + { + if (strcmp(Key, "Name") == 0) + { + UNSUPPORTED("Parsing of the static sampler name is not implemented"); + } + else + { + auto Binding = m_Bindings.find( Key ); + if (Binding != m_Bindings.end()) + { + Binding->second->SetValue( L, TblStackInd, __pBasePointer ); + } + else + { + SCRIPT_PARSING_ERROR( L, "Unknown Member \"", Key, '\"' ); + } + } + } + ); + } + else if (strcmp(Key, "SamplerOrTextureName") == 0) + { + auto Name = ReadValueFromLua<const Char*>(L, TblStackInd); + StaticSamplerTexNamesBuffer[CurrIndex] = Name; + } + else + { + SCRIPT_PARSING_ERROR( L, "Unknown Member \"", Key, '\"' ); + } + } + ); + + if( StaticSamplerTexNamesBuffer[CurrIndex].length() == 0 ) + SCRIPT_PARSING_ERROR(L, "Missing static sampler texture name") + } + ); + + for(size_t v=0; v < StaticSamplersBuffer.size(); ++v) + { + StaticSamplersBuffer[v].SamplerOrTextureName = StaticSamplerTexNamesBuffer[v].c_str(); + } + + auto &StaticSamplers = GetMemberByOffest<StaticSamplerDesc*>( pBasePointer, m_MemberOffset); + auto &NumStaticSamplers = GetMemberByOffest<Uint32>( pBasePointer, m_NumStaticSamplersOffset); + NumStaticSamplers = static_cast<Uint32>(StaticSamplersBuffer.size()); + StaticSamplers = NumStaticSamplers ? StaticSamplersBuffer.data() : nullptr; + } + private: + BindingsMapType m_Bindings; + EnumMapping<FILTER_TYPE> m_FilterTypeEnumMapping; + EnumMapping<TEXTURE_ADDRESS_MODE> m_TexAddrModeEnumMapping; + ComparisonFuncEnumMapping m_CmpFuncEnumMapping; + ShaderTypeEnumMapping m_ShaderTypeEnumMapping; + + size_t m_NumStaticSamplersOffset; + size_t m_StaticSamplersBufferOffset; + size_t m_StaticSamplerTexNamesBufferOffset; + }; + + class RTVFormatsParser; template<> @@ -115,6 +354,52 @@ namespace Diligent BindingsMapType m_Bindings; }; + + template<> + class MemberBinder<PipelineResourceLayoutDesc> : public MemberBinderBase + { + public: + MemberBinder( size_t MemberOffset, size_t VarDescBufferOffset, size_t VarNamesBufferOffset, size_t StaticSamplersBufferOffset, size_t StaticSamplerTexNamesBufferOffset ) : + MemberBinderBase( MemberOffset ) + { + DEFINE_ENUM_BINDER( m_Bindings, PipelineResourceLayoutDesc, DefaultVariableType, m_ShaderVarTypeEnumMapping ); + + auto *pShaderDescBinder = + new MemberBinder<ShaderResourceVariableDescArrayParser>( + offsetof(PipelineResourceLayoutDesc, Variables), + offsetof(PipelineResourceLayoutDesc, NumVariables), + VarDescBufferOffset, + VarNamesBufferOffset + ); + m_Bindings.insert( std::make_pair( "VariableDesc", std::unique_ptr<MemberBinderBase>(pShaderDescBinder) ) ); + + auto *pStaticSamplerDescBinder = + new MemberBinder<StaticSamplerDescArrayParser>( + offsetof(PipelineResourceLayoutDesc, StaticSamplers), + offsetof(PipelineResourceLayoutDesc, NumStaticSamplers), + StaticSamplersBufferOffset, + StaticSamplerTexNamesBufferOffset + ); + m_Bindings.insert( std::make_pair( "StaticSamplers", std::unique_ptr<MemberBinderBase>(pStaticSamplerDescBinder) ) ); + } + + virtual void GetValue( lua_State *L, const void* pBasePointer ) + { + const auto *pShaderDesc = &GetMemberByOffest<ShaderDesc>( pBasePointer, m_MemberOffset ); + PushLuaTable( L, pShaderDesc, m_Bindings ); + } + + virtual void SetValue( lua_State *L, int Index, void* pBasePointer ) + { + auto *pShaderDesc = &GetMemberByOffest<ShaderDesc>(pBasePointer, m_MemberOffset); + ParseLuaTable( L, Index, pShaderDesc, m_Bindings ); + } + + private: + BindingsMapType m_Bindings; + ShaderVariableTypeEnumMapping m_ShaderVarTypeEnumMapping; + }; + template<> class MemberBinder<GraphicsPipelineDesc> : public MemberBinderBase { @@ -242,10 +527,12 @@ namespace Diligent const Char* PSODescParser::PSODescLibName = "PipelineState"; - PSODescParser::PSODescParser( IRenderDevice *pRenderDevice, lua_State *L ) : + PSODescParser::PSODescParser( IRenderDevice *pRenderDevice, lua_State *L, const String& ResMappingMetatableName ) : EngineObjectParserCommon<IPipelineState>( pRenderDevice, L, PSODescLibName ), m_SetPSOBinding( this, L, "Context", "SetPipelineState", &PSODescParser::SetPSO ), - m_IsCompatibleWithBinding(this, L, m_MetatableRegistryName.c_str(), "IsCompatibleWith", &PSODescParser::IsCompatibleWith) + m_IsCompatibleWithBinding(this, L, m_MetatableRegistryName.c_str(), "IsCompatibleWith", &PSODescParser::IsCompatibleWith), + m_ResMappingMetatableName(ResMappingMetatableName), + m_BindStaticResourcesBinding( this, L, m_MetatableRegistryName.c_str(), "BindStaticResources", &PSODescParser::BindStaticResources ) { DEFINE_BUFFERED_STRING_BINDER( m_Bindings, PSODescWrapper, Name, NameBuffer ) @@ -254,6 +541,16 @@ namespace Diligent Validator<SRBAllocationGranularityType> SRBValidator("SRBAllocationGranularity", 1, 65536); DEFINE_BINDER_EX( m_Bindings, PSODescWrapper, SRBAllocationGranularity, SRBAllocationGranularityType, SRBValidator); + auto *pShaderDescBinder = + new MemberBinder<PipelineResourceLayoutDesc>( + offsetof(PSODescWrapper, ResourceLayout), + offsetof(PSODescWrapper, m_VarDescBuffer) - offsetof(PSODescWrapper, ResourceLayout), + offsetof(PSODescWrapper, m_VarNamesBuffer) - offsetof(PSODescWrapper, ResourceLayout), + offsetof(PSODescWrapper, m_StaticSamplersBuffer) - offsetof(PSODescWrapper, ResourceLayout), + offsetof(PSODescWrapper, m_StaticSamplerTexNamesBuffer) - offsetof(PSODescWrapper, ResourceLayout) + ); + m_Bindings.insert( std::make_pair( "ResourceLayout", std::unique_ptr<MemberBinderBase>(pShaderDescBinder) ) ); + DEFINE_BINDER_EX( m_Bindings, PSODescWrapper, GraphicsPipeline, GraphicsPipelineDesc, 0 ); DEFINE_BINDER_EX( m_Bindings, PSODescWrapper, ComputePipeline, ComputePipelineDesc, 0 ); }; @@ -303,4 +600,55 @@ namespace Diligent // Returning one value to Lua return 1; } + + int PSODescParser::BindStaticResources( lua_State *L ) + { + // In order to communicate properly with Lua, a C function must use the following protocol, + // which defines the way parameters and results are passed : a C function receives its arguments + // from Lua in its PRIVATE stack in direct order( the first argument is pushed first ).So, when the + // function starts, lua_gettop( L ) returns the number of arguments received by the function.The first + // argument( if any ) is at index 1 and its last argument is at index lua_gettop( L ). + // To return values to Lua, a C function just pushes them onto the stack, in direct order + // ( the first result is pushed first ), and returns the number of results. + // Any other value in the stack below the results will be properly discarded by Lua. + // Like a Lua function, a C function called by Lua can also return many results. + + try + { + auto NumArgs = lua_gettop( L ); + if( NumArgs < 2 ) + { + SCRIPT_PARSING_ERROR( L, "At least 1 argument (resource mapping) is expected" ); + } + + auto *pPSO = *GetUserData<IPipelineState**>( L, 1, m_MetatableRegistryName.c_str() ); + VERIFY( pPSO, "PSO pointer is null" ); + if( !pPSO )return 0; + + auto *pResourceMapping = *GetUserData<IResourceMapping**>( L, 2, m_ResMappingMetatableName.c_str() ); + if( !pResourceMapping ) + { + SCRIPT_PARSING_ERROR( L, "Incorrect 1st argument type: resource mapping is expected" ); + } + + Uint32 Flags = 0; + // The last argument may be flags + const int FlagsArgInd = 3; + if( NumArgs >= FlagsArgInd && + (lua_type( L, FlagsArgInd ) == LUA_TSTRING || lua_type( L, FlagsArgInd ) == LUA_TTABLE ) ) + { + FlagsLoader<BIND_SHADER_RESOURCES_FLAGS> FlagsLoader(0, "BindShaderResourceFlags", m_BindShaderResFlagEnumMapping); + FlagsLoader.SetValue( L, FlagsArgInd, &Flags ); + } + + + pPSO->BindStaticResources( pResourceMapping, Flags ); + } + catch( const std::runtime_error& ) + { + + } + + return 0; + } } |
