ronnie
2022-10-14 1504bb53e29d3d46222c0b3ea994fc494b48e153
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
409
410
411
412
413
414
415
416
417
418
419
420
#ifndef _GLUVARTYPEUTIL_HPP
#define _GLUVARTYPEUTIL_HPP
/*-------------------------------------------------------------------------
 * drawElements Quality Program OpenGL ES Utilities
 * ------------------------------------------------
 *
 * Copyright 2014 The Android Open Source Project
 *
 * 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
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *//*!
 * \file
 * \brief Shader variable type utilities.
 *//*--------------------------------------------------------------------*/
 
#include "tcuDefs.hpp"
#include "gluVarType.hpp"
 
#include <vector>
#include <string>
#include <iterator>
 
namespace glu
{
 
// Variable path tokenizer
 
class VarTokenizer
{
public:
   enum Token
   {
       TOKEN_IDENTIFIER = 0,
       TOKEN_LEFT_BRACKET,
       TOKEN_RIGHT_BRACKET,
       TOKEN_PERIOD,
       TOKEN_NUMBER,
       TOKEN_END,
 
       TOKEN_LAST
   };
 
                   VarTokenizer                    (const char* str);
                   ~VarTokenizer                    (void) {}
 
   Token            getToken                        (void) const { return m_token;                                                            }
   std::string        getIdentifier                    (void) const { return std::string(m_str+m_tokenStart, m_str+m_tokenStart+m_tokenLen);    }
   int                getNumber                        (void) const;
   int                getCurrentTokenStartLocation    (void) const { return m_tokenStart;                                                        }
   int                getCurrentTokenEndLocation        (void) const { return m_tokenStart + m_tokenLen;                                        }
   void            advance                            (void);
 
private:
   const char*        m_str;
 
   Token            m_token;
   int                m_tokenStart;
   int                m_tokenLen;
};
 
// VarType subtype path utilities.
 
struct VarTypeComponent
{
   enum Type
   {
       STRUCT_MEMBER = 0,
       ARRAY_ELEMENT,
       MATRIX_COLUMN,
       VECTOR_COMPONENT,
 
       TYPE_LAST
   };
 
               VarTypeComponent    (Type type_, int index_)    : type(type_), index(index_) {}
               VarTypeComponent    (void)                        : type(TYPE_LAST), index(0) {}
 
   bool        operator==            (const VarTypeComponent& other) const { return type == other.type && index == other.index; }
   bool        operator!=            (const VarTypeComponent& other) const { return type != other.type || index != other.index; }
 
   Type        type;
   int            index;
};
 
typedef std::vector<VarTypeComponent> TypeComponentVector;
 
// TypeComponentVector utilties.
 
template <typename Iterator>
bool            isValidTypePath        (const VarType& type, Iterator begin, Iterator end);
 
template <typename Iterator>
VarType            getVarType            (const VarType& type, Iterator begin, Iterator end);
 
inline bool        isValidTypePath        (const VarType& type, const TypeComponentVector& path) { return isValidTypePath(type, path.begin(), path.end()); }
inline VarType    getVarType            (const VarType& type, const TypeComponentVector& path) { return getVarType(type, path.begin(), path.end()); }
 
std::string        parseVariableName    (const char* nameWithPath);
void            parseTypePath        (const char* nameWithPath, const VarType& type, TypeComponentVector& path);
 
// Type path formatter.
 
struct TypeAccessFormat
{
   TypeAccessFormat (const VarType& type_, const TypeComponentVector& path_) : type(type_), path(path_) {}
 
   const VarType&                    type;
   const TypeComponentVector&        path;
};
 
std::ostream&        operator<<        (std::ostream& str, const TypeAccessFormat& format);
 
// Subtype path builder.
 
class SubTypeAccess
{
public:
                               SubTypeAccess        (const VarType& type);
 
   SubTypeAccess&                member                (int ndx)    { m_path.push_back(VarTypeComponent(VarTypeComponent::STRUCT_MEMBER,    ndx)); DE_ASSERT(isValid()); return *this; } //!< Access struct element.
   SubTypeAccess&                element                (int ndx)    { m_path.push_back(VarTypeComponent(VarTypeComponent::ARRAY_ELEMENT,    ndx)); DE_ASSERT(isValid()); return *this; } //!< Access array element.
   SubTypeAccess&                column                (int ndx)    { m_path.push_back(VarTypeComponent(VarTypeComponent::MATRIX_COLUMN,    ndx)); DE_ASSERT(isValid()); return *this; } //!< Access column.
   SubTypeAccess&                component            (int ndx)    { m_path.push_back(VarTypeComponent(VarTypeComponent::VECTOR_COMPONENT,    ndx)); DE_ASSERT(isValid()); return *this; } //!< Access component.
   SubTypeAccess&                parent                (void)        { DE_ASSERT(!m_path.empty()); m_path.pop_back(); return *this; }
 
   SubTypeAccess                member                (int ndx) const { return SubTypeAccess(*this).member(ndx);        }
   SubTypeAccess                element                (int ndx) const { return SubTypeAccess(*this).element(ndx);        }
   SubTypeAccess                column                (int ndx) const { return SubTypeAccess(*this).column(ndx);        }
   SubTypeAccess                component            (int ndx) const { return SubTypeAccess(*this).component(ndx);    }
   SubTypeAccess                parent                (void) const    { return SubTypeAccess(*this).parent();            }
 
   bool                        isValid                (void) const    { return isValidTypePath(m_type, m_path);        }
   VarType                        getType                (void) const    { return getVarType(m_type, m_path);            }
   const TypeComponentVector&    getPath                (void) const    { return m_path;                                }
 
   bool                        empty                (void) const { return m_path.empty(); }
 
   bool                        operator==            (const SubTypeAccess& other) const { return m_path == other.m_path && m_type == other.m_type; }
   bool                        operator!=            (const SubTypeAccess& other) const { return m_path != other.m_path || m_type != other.m_type; }
 
private:
   VarType                        m_type;
   TypeComponentVector            m_path;
};
 
// Subtype iterator.
 
// \note VarType must be live during iterator usage.
template <class IsExpanded>
class SubTypeIterator : public std::iterator<std::forward_iterator_tag, VarType>
{
public:
   static SubTypeIterator<IsExpanded>    begin                (const VarType* type) { return SubTypeIterator(type);                        }
   static SubTypeIterator<IsExpanded>    end                    (const VarType* type) { DE_UNREF(type); return SubTypeIterator(DE_NULL);    }
 
   bool                                operator==            (const SubTypeIterator<IsExpanded>& other) const { return m_type == other.m_type && m_path == other.m_path; }
   bool                                operator!=            (const SubTypeIterator<IsExpanded>& other) const { return m_type != other.m_type || m_path != other.m_path; }
 
   SubTypeIterator<IsExpanded>&        operator++            (void);
   SubTypeIterator<IsExpanded>            operator++            (int)    { SubTypeIterator<IsExpanded> copy(*this); ++(*this); return copy; }
 
   void                                toStream            (std::ostream& str) const { str << TypeAccessFormat(*m_type, m_path); }
   VarType                                getType                (void) const { return getVarType(*m_type, m_path.begin(), m_path.end()); }
   const TypeComponentVector&            getPath                (void) const { return m_path; }
 
   VarType                                operator*            (void) const { return getType(); }
 
private:
                                       SubTypeIterator        (const VarType* type);
 
   void                                removeTraversed        (void);
   void                                findNext            (void);
 
   const VarType*                        m_type;
   TypeComponentVector                    m_path;
};
 
struct IsBasicType            { bool operator() (const VarType& type) const { return type.isBasicType(); } };
struct IsScalarType            { bool operator() (const VarType& type) const { return type.isBasicType() && isDataTypeScalar(type.getBasicType()); } };
struct IsVectorOrScalarType    { bool operator() (const VarType& type) const { return type.isBasicType() && isDataTypeScalarOrVector(type.getBasicType()); } };
 
typedef SubTypeIterator<IsBasicType>            BasicTypeIterator;
typedef SubTypeIterator<IsVectorOrScalarType>    VectorTypeIterator;
typedef SubTypeIterator<IsScalarType>            ScalarTypeIterator;
 
template <class IsExpanded>
std::ostream& operator<< (std::ostream& str, const SubTypeIterator<IsExpanded>& iter)
{
   iter.toStream(str);
   return str;
}
 
template <class IsExpanded>
SubTypeIterator<IsExpanded>::SubTypeIterator (const VarType* type)
   : m_type(type)
{
   if (m_type)
       findNext();
}
 
template <class IsExpanded>
SubTypeIterator<IsExpanded>& SubTypeIterator<IsExpanded>::operator++ (void)
{
   if (!m_path.empty())
   {
       // Remove traversed nodes.
       removeTraversed();
 
       if (!m_path.empty())
           findNext();
       else
           m_type = DE_NULL; // Unset type to signal end.
   }
   else
   {
       // First type was already expanded.
       DE_ASSERT(IsExpanded()(getVarType(*m_type, m_path)));
       m_type = DE_NULL;
   }
 
   return *this;
}
 
template <class IsExpanded>
void SubTypeIterator<IsExpanded>::removeTraversed (void)
{
   DE_ASSERT(m_type && !m_path.empty());
 
   // Pop traversed nodes.
   while (!m_path.empty())
   {
       VarTypeComponent&    curComp        = m_path.back();
       VarType                parentType    = getVarType(*m_type, m_path.begin(), m_path.end()-1);
 
       if (curComp.type == VarTypeComponent::MATRIX_COLUMN)
       {
           DE_ASSERT(isDataTypeMatrix(parentType.getBasicType()));
           if (curComp.index+1 < getDataTypeMatrixNumColumns(parentType.getBasicType()))
               break;
       }
       else if (curComp.type == VarTypeComponent::VECTOR_COMPONENT)
       {
           DE_ASSERT(isDataTypeVector(parentType.getBasicType()));
           if (curComp.index+1 < getDataTypeScalarSize(parentType.getBasicType()))
               break;
       }
       else if (curComp.type == VarTypeComponent::ARRAY_ELEMENT)
       {
           DE_ASSERT(parentType.isArrayType());
           if (curComp.index+1 < parentType.getArraySize())
               break;
       }
       else if (curComp.type == VarTypeComponent::STRUCT_MEMBER)
       {
           DE_ASSERT(parentType.isStructType());
           if (curComp.index+1 < parentType.getStructPtr()->getNumMembers())
               break;
       }
 
       m_path.pop_back();
   }
}
 
template <class IsExpanded>
void SubTypeIterator<IsExpanded>::findNext (void)
{
   if (!m_path.empty())
   {
       // Increment child counter in current level.
       VarTypeComponent& curComp = m_path.back();
       curComp.index += 1;
   }
 
   for (;;)
   {
       VarType curType = getVarType(*m_type, m_path);
 
       if (IsExpanded()(curType))
           break;
 
       // Recurse into child type.
       if (curType.isBasicType())
       {
           DataType basicType = curType.getBasicType();
 
           if (isDataTypeMatrix(basicType))
               m_path.push_back(VarTypeComponent(VarTypeComponent::MATRIX_COLUMN, 0));
           else if (isDataTypeVector(basicType))
               m_path.push_back(VarTypeComponent(VarTypeComponent::VECTOR_COMPONENT, 0));
           else
               DE_ASSERT(false); // Can't expand scalars - IsExpanded() is buggy.
       }
       else if (curType.isArrayType())
           m_path.push_back(VarTypeComponent(VarTypeComponent::ARRAY_ELEMENT, 0));
       else if (curType.isStructType())
           m_path.push_back(VarTypeComponent(VarTypeComponent::STRUCT_MEMBER, 0));
       else
           DE_ASSERT(false);
   }
}
 
template <typename Iterator>
bool isValidTypePath (const VarType& type, Iterator begin, Iterator end)
{
   const VarType*    curType        = &type;
   Iterator        pathIter    = begin;
 
   // Process struct member and array element parts of path.
   while (pathIter != end)
   {
       if (pathIter->type == VarTypeComponent::STRUCT_MEMBER)
       {
           if (!curType->isStructType() || !de::inBounds(pathIter->index, 0, curType->getStructPtr()->getNumMembers()))
               return false;
 
           curType = &curType->getStructPtr()->getMember(pathIter->index).getType();
       }
       else if (pathIter->type == VarTypeComponent::ARRAY_ELEMENT)
       {
           if (!curType->isArrayType() || (curType->getArraySize() != VarType::UNSIZED_ARRAY && !de::inBounds(pathIter->index, 0, curType->getArraySize())))
               return false;
 
           curType = &curType->getElementType();
       }
       else
           break;
 
       ++pathIter;
   }
 
   if (pathIter != end)
   {
       DE_ASSERT(pathIter->type == VarTypeComponent::MATRIX_COLUMN || pathIter->type == VarTypeComponent::VECTOR_COMPONENT);
 
       // Current type should be basic type.
       if (!curType->isBasicType())
           return false;
 
       DataType basicType = curType->getBasicType();
 
       if (pathIter->type == VarTypeComponent::MATRIX_COLUMN)
       {
           if (!isDataTypeMatrix(basicType))
               return false;
 
           basicType = getDataTypeFloatVec(getDataTypeMatrixNumRows(basicType));
           ++pathIter;
       }
 
       if (pathIter != end && pathIter->type == VarTypeComponent::VECTOR_COMPONENT)
       {
           if (!isDataTypeVector(basicType))
               return false;
 
           basicType = getDataTypeScalarType(basicType);
           ++pathIter;
       }
   }
 
   return pathIter == end;
}
 
template <typename Iterator>
VarType getVarType (const VarType& type, Iterator begin, Iterator end)
{
   TCU_CHECK(isValidTypePath(type, begin, end));
 
   const VarType*    curType        = &type;
   Iterator        pathIter    = begin;
 
   // Process struct member and array element parts of path.
   while (pathIter != end)
   {
       if (pathIter->type == VarTypeComponent::STRUCT_MEMBER)
           curType = &curType->getStructPtr()->getMember(pathIter->index).getType();
       else if (pathIter->type == VarTypeComponent::ARRAY_ELEMENT)
           curType = &curType->getElementType();
       else
           break;
 
       ++pathIter;
   }
 
   if (pathIter != end)
   {
       DataType    basicType    = curType->getBasicType();
       Precision    precision    = curType->getPrecision();
 
       if (pathIter->type == VarTypeComponent::MATRIX_COLUMN)
       {
           basicType = getDataTypeFloatVec(getDataTypeMatrixNumRows(basicType));
           ++pathIter;
       }
 
       if (pathIter != end && pathIter->type == VarTypeComponent::VECTOR_COMPONENT)
       {
           basicType = getDataTypeScalarType(basicType);
           ++pathIter;
       }
 
       DE_ASSERT(pathIter == end);
       return VarType(basicType, precision);
   }
   else
       return VarType(*curType);
}
 
} // glu
 
#endif // _GLUVARTYPEUTIL_HPP