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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
/* Copyright 2015-2017 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 "ScriptParser.h"
#include "LuaBindings.h"
#include "SamplerParser.h"
#include "ShaderParser.h"
#include "BufferParser.h"
#include "TextureParser.h"
#include "DrawAttribsParser.h"
#include "FileSystem.h"
#include "ResourceMappingParser.h"
#include "TextureViewParser.h"
#include "BufferViewParser.h"
#include "PSODescParser.h"
#include "DeviceContextFuncBindings.h"
#include "ViewportParser.h"
#include "ScissorRectParser.h"
#include "ShaderVariableParser.h"
#include "ShaderResourceBindingParser.h"
using namespace Diligent;
using namespace Diligent;
namespace Diligent
{
const Char* ScriptParser::DeviceContextRegistryKey = "DeviceContext";
#define IMPLEMENT_PUSH_FUNC_STUBS(ObjectType, ParserName) \
void ScriptParser::SpecialPushFuncs::PushFuncStub( lua_State *L, const ObjectType* pObject) \
{ \
m_pScriptParser->ParserName->PushObject( L, pObject ); \
} \
\
void ScriptParser::SpecialPushFuncs::PushFuncStub( lua_State *L, const RefCntAutoPtr<ObjectType> &pObject ) \
{ \
PushFuncStub( L, pObject.RawPtr() ); \
}
IMPLEMENT_PUSH_FUNC_STUBS( ISampler, m_pSamplerParser)
IMPLEMENT_PUSH_FUNC_STUBS( IShader, m_pShaderParser )
IMPLEMENT_PUSH_FUNC_STUBS( IBuffer, m_pBufferParser )
IMPLEMENT_PUSH_FUNC_STUBS( ITexture, m_pTextureParser )
IMPLEMENT_PUSH_FUNC_STUBS( IResourceMapping, m_pResourceMappingParser )
IMPLEMENT_PUSH_FUNC_STUBS( ITextureView, m_pTextureViewParser )
IMPLEMENT_PUSH_FUNC_STUBS( IBufferView, m_pBufferViewParser )
IMPLEMENT_PUSH_FUNC_STUBS( IPipelineState, m_pPSOParser )
IMPLEMENT_PUSH_FUNC_STUBS( IShaderVariable, m_pShaderVariableParser )
IMPLEMENT_PUSH_FUNC_STUBS( IShaderResourceBinding, m_pShaderResBindingParser )
void ScriptParser::SpecialPushFuncs::PushFuncStub( lua_State *L, const DrawAttribs &DrawAttribs )
{
m_pScriptParser->m_pDrawAttribsParser->PushObject( L, &DrawAttribs );
}
void ScriptParser::SpecialPushFuncs::PushFuncStub( lua_State *L, const Viewport &Viewport )
{
m_pScriptParser->m_pViewportParser->PushObject( L, &Viewport );
}
void ScriptParser::SpecialPushFuncs::PushFuncStub( lua_State *L, const Rect &Rect )
{
m_pScriptParser->m_pScissorRectParser->PushObject( L, &Rect );
}
ScriptParser::ScriptParser( IReferenceCounters *pRefCounters, IRenderDevice *pRenderDevice ) :
TBase(pRefCounters),
m_pRenderDevice( pRenderDevice ),
m_LuaState( LuaState::LUA_LIB_BASE | LuaState::LUA_LIB_COROUTINE | LuaState::LUA_LIB_TABLE |
LuaState::LUA_LIB_STRING | LuaState::LUA_LIB_BIT32 | LuaState::LUA_LIB_MATH )
{
// Run's ctor is called BEFORE the m_LuaState's ctor
// We need to explicitly init the object:
m_RunFunctionCaller.SetLuaState( m_LuaState );
m_RunFunctionCaller.SetScriptParser( this );
// We need to define global constants before initializing parsers
DefineGlobalConstants(m_LuaState);
m_pSamplerParser.reset( new SamplerParser( pRenderDevice, m_LuaState ) );
m_pBufferParser.reset( new BufferParser( pRenderDevice, m_LuaState ) );
m_pTextureParser.reset( new TextureParser( pRenderDevice, m_LuaState ) );
m_pDrawAttribsParser.reset( new DrawAttribsParser( m_pBufferParser.get(), pRenderDevice, m_LuaState ) );
// Texture view parser must be create AFTER texture parser, because it
// registers CreateView function in Metatables.Texture table
m_pTextureViewParser.reset( new TextureViewParser( m_pTextureParser.get(), m_pSamplerParser.get(), pRenderDevice, m_LuaState ) );
m_pBufferViewParser.reset( new BufferViewParser( m_pBufferParser.get(), pRenderDevice, m_LuaState ) );
m_pResourceMappingParser.reset( new ResourceMappingParser( pRenderDevice, m_LuaState, m_pTextureViewParser.get(), m_pBufferParser.get(), m_pBufferViewParser.get() ) );
m_pShaderParser.reset( new ShaderParser( pRenderDevice, m_LuaState, m_pResourceMappingParser->GetMetatableName() ) );
m_pPSOParser.reset( new PSODescParser( pRenderDevice, m_LuaState ) );
m_pViewportParser.reset( new ViewportParser( pRenderDevice, m_LuaState ) );
m_pScissorRectParser.reset( new ScissorRectParser( pRenderDevice, m_LuaState ) );
m_pShaderVariableParser.reset( new ShaderVariableParser( pRenderDevice, m_LuaState, m_pShaderParser->GetMetatableName(), m_pBufferParser->GetMetatableName(), m_pBufferViewParser->GetMetatableName(), m_pTextureViewParser->GetMetatableName() ) );
m_pShaderResBindingParser.reset( new ShaderResourceBindingParser( pRenderDevice, m_LuaState, m_pPSOParser->GetMetatableName(), m_pResourceMappingParser->GetMetatableName(), m_pShaderVariableParser->GetMetatableName() ) );
m_pDeviceCtxFuncBindings.reset( new DeviceContextFuncBindings( pRenderDevice, m_LuaState, m_pTextureViewParser.get(), m_pShaderResBindingParser.get(), m_pPSOParser.get() ) );
}
ScriptParser::~ScriptParser()
{
// It is essentially important to close Lua first, because we need to release
// all user data
m_LuaState.Close();
}
void ScriptParser::DefineGlobalConstants( lua_State *L )
{
INIT_LUA_STACK_TRACKING( L );
// Create a new empty table and push it onto the stack
lua_newtable( L ); // -0 | +1 -> +1
const auto &DeviceCaps = m_pRenderDevice->GetDeviceCaps();
const Char *pDeviceStr = nullptr;
switch(DeviceCaps.DevType)
{
case DeviceType::OpenGL:
pDeviceStr = "OpenGL";
break;
case DeviceType::OpenGLES:
pDeviceStr = "OpenGLES";
break;
case DeviceType::D3D11:
pDeviceStr = "D3D11";
break;
case DeviceType::D3D12:
pDeviceStr = "D3D12";
break;
default:
UNEXPECTED( "Unknown device type" );
}
SetTableField( L, "DeviceType", -1, pDeviceStr ); // -0 | +0 -> 0
// lua_setglobal() pops a value from the stack and sets it as the new value of global name.
lua_setglobal( L, "Constants" ); // -1 | +0 -> -1
CHECK_LUA_STACK_HEIGHT();
lua_newtable( L ); // -0 | +1 -> +1
lua_setglobal( L, "Context" ); // -1 | +0 -> -1
CHECK_LUA_STACK_HEIGHT();
}
// Every C-function called by Lua sees only its own private stack, with its first argument at index 1
// Return the number of results pushed onto the stack
void ScriptParser::Parse( const Char *pScript )
{
// Load the chunck and push it onto the top of the stack.
// In case of errors, the error message is pushed onto the top of the stack
if( luaL_loadstring( m_LuaState, pScript ) )
{
// Get the error message from the top of the stack
// NOTE: the lua_tostring function returns a pointer to an internal copy of the string.
// The string is always zero-terminated and Lua ensures that this pointer is valid as long
// as the corresponding value is in the stack.
auto ErrorMsg = lua_tostring( m_LuaState, -1 );
if( ErrorMsg )
{
LOG_ERROR_AND_THROW( "Failed to parse the script file:\n", ErrorMsg );
}
else
{
LOG_ERROR_AND_THROW( "Failed to parse the script file." );
}
lua_pop( m_LuaState, 1 ); // pop error message from the stack
}
}
void ScriptParser::GetSamplerByName( const Char *SamplerName, ISampler** ppSampler )
{
m_pSamplerParser->GetObjectByName( m_LuaState, SamplerName, ppSampler );
}
void ScriptParser::GetShaderByName( const Char *ShaderName, IShader** ppShader )
{
m_pShaderParser->GetObjectByName( m_LuaState, ShaderName, ppShader );
}
void ScriptParser::GetBufferByName( const Char *BufferName, IBuffer** ppBuffer )
{
m_pBufferParser->GetObjectByName( m_LuaState, BufferName, ppBuffer );
}
void ScriptParser::GetTextureByName( const Char *TextureName, ITexture** ppTexture )
{
m_pTextureParser->GetObjectByName( m_LuaState, TextureName, ppTexture );
}
void ScriptParser::GetResourceMappingByName( const Char *ResourceMappingName, IResourceMapping** ppResourceMapping )
{
m_pResourceMappingParser->GetObjectByName( m_LuaState, ResourceMappingName, ppResourceMapping );
}
void ScriptParser::GetTextureViewByName( const Char *TextureViewName, ITextureView** ppTextureView )
{
m_pTextureViewParser->GetObjectByName( m_LuaState, TextureViewName, ppTextureView );
}
void ScriptParser::GetBufferViewByName( const Char *BufferViewName, IBufferView** ppBufferView )
{
m_pBufferViewParser->GetObjectByName( m_LuaState, BufferViewName, ppBufferView );
}
void ScriptParser::GetPipelineStateByName( const Char *PSOName, IPipelineState** ppPSO )
{
m_pPSOParser->GetObjectByName( m_LuaState, PSOName, ppPSO );
}
void ScriptParser::GetShaderVariableByName( const Char *ShaderVarName, IShaderVariable** ppShaderVar )
{
m_pShaderVariableParser->GetObjectByName( m_LuaState, ShaderVarName, ppShaderVar );
}
void ScriptParser::QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )
{
UNSUPPORTED( "Not implemented" );
}
}
|