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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/* 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 "LuaBindings.h"
#include "GraphicsAccessories.h"
using namespace std;
namespace Diligent
{
template<typename Type>
Type ReadIntValueFromLua( lua_State *L, int Index )
{
CheckType( L, Index, LUA_TNUMBER );
int isnum;
auto Val = lua_tonumberx( L, Index, &isnum );
if( !isnum )
{
auto Str = lua_tostring( L, Index );
SCRIPT_PARSING_ERROR( L, "Failed to convert parameter ", Str, " to int" );
}
if( static_cast<Type>(Val) != Val )
{
SCRIPT_PARSING_ERROR( L, "Parameter value (", Val, ") is not integer. Truncating to int" );
}
return static_cast<Type>(Val);
}
template<typename Type>
Type ReadFPValueFromLua( lua_State *L, int Index )
{
CheckType( L, Index, LUA_TNUMBER );
int isnum;
auto Val = lua_tonumberx( L, Index, &isnum );
if( !isnum )
{
auto Str = lua_tostring( L, Index );
SCRIPT_PARSING_ERROR( L, "Failed to convert parameter ", Str, " to floating point" );
}
return static_cast<Type>(Val);
}
template<>
int ReadValueFromLua<int>( lua_State *L, int Index )
{
return ReadIntValueFromLua<int>( L, Index );
}
template<>
double ReadValueFromLua<double>( lua_State *L, int Index )
{
return ReadFPValueFromLua<double>( L, Index );
}
template<>
float ReadValueFromLua<float>( lua_State *L, int Index )
{
return ReadFPValueFromLua<float>( L, Index );
}
template<>
String ReadValueFromLua<String>( lua_State *L, int Index )
{
CheckType( L, Index, LUA_TSTRING );
auto Str = lua_tostring( L, Index );
return String( Str );
}
template<>
const Char* ReadValueFromLua<const Char*>( lua_State *L, int Index )
{
CheckType( L, Index, LUA_TSTRING );
auto Str = lua_tostring( L, Index );
return Str;
}
template<>
Bool ReadValueFromLua<Bool>( lua_State *L, int Index )
{
CheckType( L, Index, LUA_TBOOLEAN );
auto Val = lua_toboolean( L, Index );
return Val ? True : False;
}
template<>
Uint32 ReadValueFromLua<Uint32>( lua_State *L, int Index )
{
return ReadIntValueFromLua<Uint32>( L, Index );
}
template<>
Uint8 ReadValueFromLua<Uint8>( lua_State *L, int Index )
{
return ReadIntValueFromLua<Uint8>( L, Index );
}
template<>
void PushValue<double>( lua_State *L, double Val )
{
lua_pushnumber( L, Val );
}
template<>
void PushValue<const float&>( lua_State *L, const float& Val )
{
lua_pushnumber( L, Val );
}
template<>
void PushValue<const Int32&>( lua_State *L, const Int32& Val )
{
lua_pushnumber( L, Val );
}
template<>
void PushValue<const Uint8&>( lua_State *L, const Uint8& Val )
{
lua_pushnumber( L, Val );
}
template<>
void PushValue<const Uint32&>( lua_State *L, const Uint32& Val )
{
lua_pushnumber( L, Val );
}
template<>
void PushValue<const Char*>( lua_State *L, const Char* Val )
{
lua_pushstring( L, Val );
}
template<> void PushValue<const Char* const&>( lua_State *L, const Char* const& Val )
{
lua_pushstring( L, Val );
}
template<>
void PushValue<const String&>( lua_State *L, const String& Val )
{
lua_pushstring( L, Val.c_str() );
}
template<>
void PushValue<bool>( lua_State *L, bool Val )
{
lua_pushboolean( L, Val );
}
template<>
void PushValue<const bool &>( lua_State *L, const bool &Val )
{
lua_pushboolean( L, Val );
}
template<VALUE_TYPE VTType>
void ParseNumericArray( lua_State *L, int StackIndex, std::vector< Uint8 >& RawData )
{
typedef typename VALUE_TYPE2CType<VTType>::CType ElemType;
CheckType( L, StackIndex, LUA_TTABLE );
auto ArraySize = lua_rawlen( L, StackIndex );
auto ElemSize = sizeof( ElemType );
RawData.reserve( ArraySize * ElemSize );
ParseLuaArray( L, StackIndex, &RawData, [ &]( void* pBasePointer, int StackIndex, int NewArrayIndex )
{
VERIFY( pBasePointer == &RawData, "Sanity check failed" );
auto CurrIndex = RawData.size() / ElemSize;
if(static_cast<int>(CurrIndex) != NewArrayIndex - 1 )
SCRIPT_PARSING_ERROR( L, "Explicit array indices are not allowed in array initialization. Provided index ", NewArrayIndex - 1, " conflicts with actual index ", CurrIndex, "." );
RawData.resize( (CurrIndex + 1) * ElemSize );
auto CurrValue = ReadValueFromLua<double>( L, StackIndex );
reinterpret_cast<ElemType&>(RawData[CurrIndex * ElemSize]) = static_cast<ElemType>(CurrValue);
} );
}
NumericArrayLoader::NumericArrayLoader() :
m_ValueTypeBinder( 0, "VALUE_TYPE", m_ValueTypeEnumMapping )
{
DEFINE_ENUM_ELEMENT_MAPPING( m_ValueTypeEnumMapping, VT_INT8 );
DEFINE_ENUM_ELEMENT_MAPPING( m_ValueTypeEnumMapping, VT_INT16 );
DEFINE_ENUM_ELEMENT_MAPPING( m_ValueTypeEnumMapping, VT_INT32 );
DEFINE_ENUM_ELEMENT_MAPPING( m_ValueTypeEnumMapping, VT_UINT8 );
DEFINE_ENUM_ELEMENT_MAPPING( m_ValueTypeEnumMapping, VT_UINT16 );
DEFINE_ENUM_ELEMENT_MAPPING( m_ValueTypeEnumMapping, VT_UINT32 );
DEFINE_ENUM_ELEMENT_MAPPING( m_ValueTypeEnumMapping, VT_FLOAT16 );
DEFINE_ENUM_ELEMENT_MAPPING( m_ValueTypeEnumMapping, VT_FLOAT32 );
VERIFY( m_ValueTypeEnumMapping.m_Str2ValMap.size() == VT_NUM_TYPES - 1,
"Unexpected map size. Did you update VALUE_TYPE enum?" );
VERIFY( m_ValueTypeEnumMapping.m_Val2StrMap.size() == VT_NUM_TYPES - 1,
"Unexpected map size. Did you update VALUE_TYPE enum?" );
m_ParseFuncJumpTbl.insert( make_pair( VT_INT8, ParseNumericArray<VT_INT8> ) );
m_ParseFuncJumpTbl.insert( make_pair( VT_INT16, ParseNumericArray<VT_INT16> ) );
m_ParseFuncJumpTbl.insert( make_pair( VT_INT32, ParseNumericArray<VT_INT32> ) );
m_ParseFuncJumpTbl.insert( make_pair( VT_UINT8, ParseNumericArray<VT_UINT8> ) );
m_ParseFuncJumpTbl.insert( make_pair( VT_UINT16, ParseNumericArray<VT_UINT16> ) );
m_ParseFuncJumpTbl.insert( make_pair( VT_UINT32, ParseNumericArray<VT_UINT32> ) );
//m_ParseFuncJumpTbl.insert( make_pair( VT_FLOAT16, ParseNumericArray<VT_FLOAT16> ) );
m_ParseFuncJumpTbl.insert( make_pair( VT_FLOAT32, ParseNumericArray<VT_FLOAT32> ) );
};
void NumericArrayLoader::LoadArray( lua_State *L, int StackIndex, std::vector< Uint8 >& RawData )
{
VALUE_TYPE ValueType;
m_ValueTypeBinder.SetValue( L, StackIndex-1, &ValueType );
auto it = m_ParseFuncJumpTbl.find( ValueType );
if( it != m_ParseFuncJumpTbl.end() )
{
it->second( L, StackIndex, RawData );
}
else
{
SCRIPT_PARSING_ERROR( L, "No method to parse array of value VALUE_TYPE==", ValueType);
}
}
// Special version of luaL_testudata() which takes an array of allowed metatables
void *luaL_testudata( lua_State *L, int ud, const std::vector<String> &MetatableNames )
{
void *p = lua_touserdata( L, ud );
if( p != nullptr )
{
// value is a userdata?
if( lua_getmetatable( L, ud ) ) // -0 | +(0|1) -> +(0|1)
{
// does it have a metatable?
bool bMatchingMTFound = false;
for( auto mtname = MetatableNames.begin(); mtname != MetatableNames.end() && !bMatchingMTFound; ++mtname )
{
// get correct metatable
luaL_getmetatable( L, mtname->c_str() ); // -0 | +1 -> +1
if( lua_rawequal( L, -1, -2 ) ) // are the same?
bMatchingMTFound = true;
// pop correct metatable
lua_pop( L, 1 ); // -1 | +0 -> -1
}
// pop user metatable
lua_pop( L, 1 ); // -1 | +0 -> -1
return bMatchingMTFound ? p : nullptr;
}
}
return nullptr; // value is not a userdata with a metatable
}
}
|