1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
/* Copyright 2015-2019 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
*
* In no event and under no legal theory, whether in tort (including negligence),
* contract, or otherwise, unless required by applicable law (such as deliberate
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
* liable for any damages, including any direct, indirect, special, incidental,
* or consequential damages of any character arising as a result of this License or
* out of the use or inability to use the software (including but not limited to damages
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
* all other commercial damages or losses), even if such Contributor has been advised
* of the possibility of such damages.
*/
#include "pch.h"
#include "ShaderResourceBindingParser.h"
namespace Diligent
{
const Char* ShaderResourceBindingParser::ShaderResourceBindingLibName = "ShaderResourceBinding";
ShaderResourceBindingParser::ShaderResourceBindingParser( IRenderDevice *pRenderDevice, lua_State *L,
const String &PSOLibMetatableName,
const String &ResMappingMetatableName,
const String &ShaderVarMetatableRegistryName ) :
EngineObjectParserBase( pRenderDevice, L, ShaderResourceBindingLibName ),
m_PSOLibMetatableName(PSOLibMetatableName),
m_ResMappingMetatableName(ResMappingMetatableName),
m_ShaderVarMetatableRegistryName(ShaderVarMetatableRegistryName),
m_BindResourcesBinding( this, L, m_MetatableRegistryName.c_str(), "BindResources", &ShaderResourceBindingParser::BindResources ),
m_GetVariableBinding( this, L, m_MetatableRegistryName.c_str(), "GetVariable", &ShaderResourceBindingParser::GetVariable ),
m_CreateShaderResourceBinding( this, L, PSOLibMetatableName.c_str(), "CreateShaderResourceBinding", &ShaderResourceBindingParser::CreateShaderResourceBinding ),
m_InitializeStaticResourcesBinding( this, L, m_MetatableRegistryName.c_str(), "InitializeStaticResources", &ShaderResourceBindingParser::InitializeStaticResources )
{
}
void ShaderResourceBindingParser::CreateObj( lua_State *L )
{
INIT_LUA_STACK_TRACKING(L);
// PSO should be the first argument
auto *pPSO = *GetUserData<IPipelineState**>( L, 1, m_PSOLibMetatableName.c_str() );
bool InitStaticResources = false;
auto NumArgs = lua_gettop( L );
if( NumArgs >= 2 )
{
InitStaticResources = ReadValueFromLua<Bool>( L, 2 );
}
auto pNewShaderResBndngLuaObj = reinterpret_cast<IShaderResourceBinding**>(lua_newuserdata( L, sizeof( IShaderResourceBinding* ) ));
pPSO->CreateShaderResourceBinding(pNewShaderResBndngLuaObj, InitStaticResources);
CHECK_LUA_STACK_HEIGHT( +1 );
}
void ShaderResourceBindingParser::DestroyObj( void *pData )
{
if( pData != nullptr )
{
auto ppShaderResBinding = reinterpret_cast<IShaderResourceBinding**>(pData);
if( *ppShaderResBinding != nullptr )
(*ppShaderResBinding)->Release();
}
}
void ShaderResourceBindingParser::ReadField( lua_State *L, void *pData, const Char *Field )
{
SCRIPT_PARSING_ERROR(L, "Shader resource binding have no fields that can be read")
}
void ShaderResourceBindingParser::UpdateField( lua_State *L, void *pData, const Char *Field )
{
SCRIPT_PARSING_ERROR(L, "Shader resource binding have no fields that can be updated")
}
void ShaderResourceBindingParser::PushExistingObject( lua_State *L, const void *pObject )
{
auto pShaderResourceBinding = reinterpret_cast<IShaderResourceBinding**>(lua_newuserdata( L, sizeof( IShaderResourceBinding* ) ));
*pShaderResourceBinding = reinterpret_cast<IShaderResourceBinding*>( const_cast<void*>(pObject) );
(*pShaderResourceBinding)->AddRef();
}
int ShaderResourceBindingParser::CreateShaderResourceBinding( lua_State *L )
{
return LuaCreate(L);
}
int ShaderResourceBindingParser::BindResources( lua_State *L )
{
try
{
auto NumArgs = lua_gettop( L );
if( NumArgs < 3 )
{
SCRIPT_PARSING_ERROR( L, "At least 2 arguments (shader flags and resource mapping) are expected" );
}
int ArgStackInd = 1;
auto *pResBinding = *GetUserData<IShaderResourceBinding**>( L, ArgStackInd, m_MetatableRegistryName.c_str() );
VERIFY( pResBinding, "Resource mapping pointer is null" );
if( !pResBinding )return 0;
++ArgStackInd;
Uint32 ShaderFlags = 0;
{
FlagsLoader<SHADER_TYPE> FlagsLoader(0, "BindShaderResourceFlags", m_ShaderTypeEnumMapping);
FlagsLoader.SetValue( L, ArgStackInd, &ShaderFlags );
}
++ArgStackInd;
auto *pResourceMapping = *GetUserData<IResourceMapping**>( L, ArgStackInd, m_ResMappingMetatableName.c_str() );
if( !pResourceMapping )
{
SCRIPT_PARSING_ERROR( L, "Incorrect 2nd argument type: resource mapping is expected" );
}
++ArgStackInd;
Uint32 Flags = 0;
// The last argument may be flags
if( NumArgs >= ArgStackInd &&
(lua_type( L, ArgStackInd ) == LUA_TSTRING || lua_type( L, ArgStackInd ) == LUA_TTABLE ) )
{
FlagsLoader<BIND_SHADER_RESOURCES_FLAGS> FlagsLoader(0, "BindShaderResourceFlags", m_BindShaderResFlagEnumMapping);
FlagsLoader.SetValue( L, ArgStackInd, &Flags );
}
pResBinding->BindResources( ShaderFlags, pResourceMapping, Flags );
}
catch( const std::runtime_error& )
{
}
return 0;
}
int ShaderResourceBindingParser::GetVariable( lua_State *L )
{
auto NumArgs = lua_gettop( L );
if( NumArgs < 3 )
{
SCRIPT_PARSING_ERROR( L, "2 arguments (shader type and variable name) are expected" );
}
INIT_LUA_STACK_TRACKING(L);
int ArgStackInd = 1;
// The object itself goes first
auto *pShaderResBinding = *GetUserData<IShaderResourceBinding**>( L, ArgStackInd, m_MetatableRegistryName.c_str() );
// Shader type should be the first argument
++ArgStackInd;
SHADER_TYPE ShaderType = SHADER_TYPE_UNKNOWN;
EnumMemberBinder<SHADER_TYPE> ShaderTypeParser(0, "ShaderType", m_ShaderTypeEnumMapping);
ShaderTypeParser.SetValue(L, ArgStackInd, &ShaderType);
++ArgStackInd;
// Variable name should be the second argument
auto VarName = ReadValueFromLua<String>( L, ArgStackInd );
auto pVar = pShaderResBinding->GetVariable(ShaderType, VarName.c_str());
auto pNewShaderVarLuaObj = reinterpret_cast<IShaderVariable**>(lua_newuserdata( L, sizeof( IShaderVariable* ) ));
*pNewShaderVarLuaObj = pVar;
pVar->AddRef();
// Push onto the stack the metatable associated with name given in the registry
luaL_getmetatable( L, m_ShaderVarMetatableRegistryName.c_str() ); // -0 | +1 -> +1
// Pop a table from the top of the stack and set it as the new metatable
// for the value at the given index (which is where the new user datum is)
lua_setmetatable( L, -2 ); // -1 | +0 -> -1
CHECK_LUA_STACK_HEIGHT( +1 );
return 1;
}
int ShaderResourceBindingParser::InitializeStaticResources( lua_State *L )
{
auto NumArgs = lua_gettop( L );
// The object itself goes first
auto *pShaderResBinding = *GetUserData<IShaderResourceBinding**>( L, 1, m_MetatableRegistryName.c_str() );
IPipelineState* pPSO = nullptr;
if (NumArgs >= 2)
{
pPSO = *GetUserData<IPipelineState**>( L, 2, m_PSOLibMetatableName.c_str() );
}
pShaderResBinding->InitializeStaticResources(pPSO);
return 0;
}
void ShaderResourceBindingParser::GetObjectByName( lua_State *L, const Char *ShaderName, IShaderResourceBinding** ppObject )
{
auto pObject = *GetGlobalObject<IShaderResourceBinding**>( L, ShaderName, m_MetatableRegistryName.c_str() );
*ppObject = pObject;
pObject->AddRef();
}
}
|