This repository was archived by the owner on Sep 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathGLSLFunctionSignature.cxx
408 lines (348 loc) · 13.7 KB
/
GLSLFunctionSignature.cxx
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//--------------------------------------------------------------
//
// Microsoft Edge Implementation
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files(the ""Software""),
// to deal in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// 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.IN NO EVENT SHALL THE AUTHORS
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
// OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//--------------------------------------------------------------
#include "PreComp.hxx"
#include "GLSLFunctionSignature.hxx"
#include "FunctionCallHeaderWithParametersNode.hxx"
#include "TypeHelpers.hxx"
#include "GLSL.tab.h"
//+----------------------------------------------------------------------------
//
// Function: Constructor
//
//-----------------------------------------------------------------------------
CGLSLFunctionSignature::ParamInfo::ParamInfo()
{
_paramQual = EMPTY_TOK;
}
//+----------------------------------------------------------------------------
//
// Function: Constructor
//
//-----------------------------------------------------------------------------
CGLSLFunctionSignature::ParamInfo::ParamInfo(__in GLSLType* pType, int paramQual)
{
_spType = pType;
_paramQual = paramQual;
}
//+----------------------------------------------------------------------------
//
// Function: Constructor
//
//-----------------------------------------------------------------------------
CGLSLFunctionSignature::CGLSLFunctionSignature() :
_signatureType(GLSLSignatureType::Normal)
{
}
//+----------------------------------------------------------------------------
//
// Function: AddBasicType
//
// Synopsis: Called by code that is building up a signature.
//
//-----------------------------------------------------------------------------
HRESULT CGLSLFunctionSignature::AddBasicType(int type, int paramQual)
{
CHK_START;
Assert(_signatureType == GLSLSignatureType::Normal || _signatureType == GLSLSignatureType::GenType);
if (type == GENTYPE)
{
_signatureType = GLSLSignatureType::GenType;
}
TSmartPointer<GLSLType> spType;
CHK(GLSLType::CreateFromBasicTypeToken(type, &spType));
CHK(AddType(spType, paramQual));
CHK_RETURN;
}
//+----------------------------------------------------------------------------
//
// Function: AddType
//
// Synopsis: Called by code that is building up a signature.
//
//-----------------------------------------------------------------------------
HRESULT CGLSLFunctionSignature::AddType(__in GLSLType* pType, int paramQual)
{
ParamInfo info(pType, paramQual);
return _rgArgTypes.Add(info);
}
//+----------------------------------------------------------------------------
//
// Function: InitFromKnown
//
// Synopsis: Initialize from the information for a known function. All
// known functions are assumed to lack parameter qualifiers.
//
//-----------------------------------------------------------------------------
HRESULT CGLSLFunctionSignature::InitFromKnown(const GLSLFunctionInfo &info)
{
CHK_START;
if (info._sigType == GLSLSignatureType::Normal)
{
// Add the return type
CHK(AddBasicType(info._type, EMPTY_TOK));
// Add the argument types
for (int i = 0; i < info._numArgs; i++)
{
CHK(AddBasicType(info._rgArgTypes[i], EMPTY_TOK));
}
}
else
{
_signatureType = info._sigType;
}
CHK_RETURN;
}
//+----------------------------------------------------------------------------
//
// Function: SignatureMatchesArgumentTypes
//
// Synopsis: Match the parameters and determine the return type, along
// with the computed genType.
//
// In GLSL, some builtin function signatures have
// gentype arguments, which means that it could take a vector
// of any length.
//
// In this case, the arguments need to match in count, and all
// of the gentype arguments need to be the same length. If the
// return type is a gentype then this length determines the
// return type.
//
//-----------------------------------------------------------------------------
HRESULT CGLSLFunctionSignature::SignatureMatchesArgumentTypes(
const CModernArray<TSmartPointer<GLSLType>>& aryArgumentTypes, // Arguments types to match
__out int* pGenType, // Determined gentype
__deref_out GLSLType** ppReturnType // Return type if signature matched args
) const
{
CHK_START;
// Only MatchNormalSignature will potentially set a meaningful gentype, so
// init the out param for all paths here.
*pGenType = NO_TYPE;
switch (GetSignatureType())
{
case GLSLSignatureType::Normal:
case GLSLSignatureType::GenType:
CHK(MatchNormalSignature(aryArgumentTypes, pGenType, ppReturnType));
break;
case GLSLSignatureType::CompareFloatVector:
case GLSLSignatureType::CompareIntVector:
case GLSLSignatureType::CompareBoolVector:
CHK(MatchCompareVectorSignature(aryArgumentTypes, ppReturnType));
break;
case GLSLSignatureType::TestBoolVector:
CHK(MatchTestVectorSignature(aryArgumentTypes, ppReturnType));
break;
default:
Assert(false);
CHK(E_UNEXPECTED);
}
CHK_RETURN;
}
//+----------------------------------------------------------------------------
//
// Function: MatchCompareVectorSignature
//
// Synopsis: Match the parameters and determine the return type when this
// signature is a vector relational function.
//
// The function must take two vectors the same size, and return
// a boolean of this same size.
//
//-----------------------------------------------------------------------------
HRESULT CGLSLFunctionSignature::MatchCompareVectorSignature(
const CModernArray<TSmartPointer<GLSLType>>& aryArgumentTypes, // Arguments types to match
__deref_out GLSLType** ppReturnType // Return type if signature matched args
) const
{
CHK_START;
// Comparison functions have 2 arguments
CHKB(aryArgumentTypes.GetCount() == 2);
// Figure out the component type we need
int compType = NO_TYPE;
switch (GetSignatureType())
{
case GLSLSignatureType::CompareFloatVector:
compType = FLOAT_TOK;
break;
case GLSLSignatureType::CompareIntVector:
compType = INT_TOK;
break;
case GLSLSignatureType::CompareBoolVector:
compType = BOOL_TOK;
break;
default:
Assert(false);
CHK(E_UNEXPECTED);
}
int vecLength = -1;
// Loop through all of the arguments, ensuring that they match
for (UINT i = 0; i < aryArgumentTypes.GetCount(); i++)
{
// Get the arg basic type
const GLSLType* pArgType = aryArgumentTypes[i];
int argBasicType;
CHK(pArgType->GetBasicType(&argBasicType));
// Get the arg component type and check it
int argCompType;
CHK(TypeHelpers::GetComponentType(argBasicType, &argCompType));
CHKB(argCompType == compType);
// Get the arg length and make sure it is consistent across args
int argLength = TypeHelpers::GetVectorLength(argBasicType);
if (vecLength == -1)
{
vecLength = argLength;
}
else
{
CHKB(argLength == vecLength);
}
// Must be a vector
CHKB(argLength >=2 && argLength <= 4);
}
int retTypes[3] = { BVEC2_TOK, BVEC3_TOK, BVEC4_TOK };
// The conditions above should force this
Assert(vecLength >= 2 && vecLength <= 4);
// Create a return type from the matched basic type
TSmartPointer<GLSLType> spReturnType;
CHK(GLSLType::CreateFromBasicTypeToken(retTypes[vecLength - 2], &spReturnType));
*ppReturnType = spReturnType.Extract();
CHK_RETURN;
}
//+----------------------------------------------------------------------------
//
// Function: MatchTestVectorSignature
//
// Synopsis: Match the parameters and determine the return type when this
// signature is a vector test function.
//
// The function must take a boolean vector and return a boolean.
//
//-----------------------------------------------------------------------------
HRESULT CGLSLFunctionSignature::MatchTestVectorSignature(
const CModernArray<TSmartPointer<GLSLType>>& aryArgumentTypes, // Arguments types to match
__deref_out GLSLType** ppReturnType // Return type if signature matched args
) const
{
CHK_START;
// Test functions have 1 argument
CHKB(aryArgumentTypes.GetCount() == 1);
// Get the arg basic type
const GLSLType* pArgType = aryArgumentTypes[0];
int argBasicType;
CHK(pArgType->GetBasicType(&argBasicType));
// Get the arg component type and check it
int argCompType;
CHK(TypeHelpers::GetComponentType(argBasicType, &argCompType));
CHKB(argCompType == BOOL_TOK);
// Create a return type
CHK(GLSLType::CreateFromBasicTypeToken(BOOL_TOK, ppReturnType));
CHK_RETURN;
}
//+----------------------------------------------------------------------------
//
// Function: MatchNormalSignature
//
// Synopsis: Match the parameters and determine the return type when
// this signature is for a normal or gentype function.
//
// In GLSL, some builtin function signatures have
// gentype arguments, which means that it could take a vector
// of any length.
//
// In this case, the arguments need to match in count, and all
// of the gentype arguments need to be the same length. If the
// return type is a gentype then this length determines the
// return type.
//
//-----------------------------------------------------------------------------
HRESULT CGLSLFunctionSignature::MatchNormalSignature(
const CModernArray<TSmartPointer<GLSLType>>& aryArgumentTypes, // Arguments types to match
__out int* pGenType, // Determined gentype
__deref_out GLSLType** ppReturnType // Return type if signature matched args
) const
{
CHK_START;
int genType = NO_TYPE;
// Need to have the same argument count
CHKB(aryArgumentTypes.GetCount() == GetArgumentCount());
// Loop through all of the arguments, ensuring that they match
for (UINT i = 0; i < aryArgumentTypes.GetCount(); i++)
{
const GLSLType* pArgType = aryArgumentTypes[i];
// Check that the argument passed matches the argument type of the template
int templateBasicType;
if (SUCCEEDED(UseArgumentType(i)->GetBasicType(&templateBasicType)) && templateBasicType == GENTYPE)
{
// We either create a new gentype match, or verify the current one. Either way
// we need to have a basic type now.
int argBasicType;
CHK(pArgType->GetBasicType(&argBasicType));
if (genType == NO_TYPE)
{
// We have not matched the gentype yet, so do so now
genType = argBasicType;
}
else
{
// We have decided what the gentype represents, all others have to fall in line
CHKB(argBasicType == genType);
}
}
else
{
// Specifically check for equal types - no implicit casting when calling functions is allowed
CHKB(UseArgumentType(i)->IsEqualType(pArgType));
}
}
if (GetSignatureType() == GLSLSignatureType::GenType)
{
// We have a gentype function but we were unable to infer what the gentype
// mapped to. This could mean the metadata is wrong.
CHKB(genType != NO_TYPE);
// gentype is only allowed to be be certain types
CHKB(TypeHelpers::IsValidGenType(genType));
}
// By now, we know that the arguments match. So we can determine the return type
// that is requested, transforming it to a real type if need be. We get
// the return type from the template return type.
TSmartPointer<GLSLType> spReturnType = UseReturnType();
int returnBasicType;
if (SUCCEEDED(spReturnType->GetBasicType(&returnBasicType)) && returnBasicType == GENTYPE)
{
// If we have a gentype in the type list then this flag should be set
Assert(GetSignatureType() == GLSLSignatureType::GenType);
// Create a return type from the matched basic type
CHK(GLSLType::CreateFromBasicTypeToken(genType, &spReturnType));
// If this assert fires then the pTemplate had a gentype in it, which
// makes no sense. This needs to be investigated and fixed.
Assert(genType != GENTYPE);
}
*ppReturnType = spReturnType.Extract();
*pGenType = genType;
CHK_RETURN;
}