huangcm
2025-02-24 69ed55dec4b2116a19e4cca4393cbc014fce5fb2
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
#ifndef _RSGVARIABLEVALUE_HPP
#define _RSGVARIABLEVALUE_HPP
/*-------------------------------------------------------------------------
 * drawElements Quality Program Random Shader Generator
 * ----------------------------------------------------
 *
 * 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 Variable Value class.
 *//*--------------------------------------------------------------------*/
 
#include "rsgDefs.hpp"
#include "rsgVariableType.hpp"
#include "rsgVariable.hpp"
#include "tcuVector.hpp"
 
#include <algorithm>
 
namespace rsg
{
 
union Scalar
{
   int        intVal;
   float    floatVal;
   bool    boolVal;
 
   Scalar (void)        : intVal    (0) {}
   Scalar (float v)    : floatVal    (v) {}
   Scalar (int v)        : intVal    (v) {}
   Scalar (bool v)        : boolVal    (v) {}
 
   // Bit-exact compare
   bool operator== (Scalar other) const { return intVal == other.intVal; }
   bool operator!= (Scalar other) const { return intVal != other.intVal; }
 
   template <typename T> static Scalar min    (void);
   template <typename T> static Scalar max    (void);
 
   template <typename T> T        as (void) const;
   template <typename T> T&    as (void);
};
DE_STATIC_ASSERT(sizeof(Scalar) == sizeof(deUint32));
 
template <> inline Scalar Scalar::min<float>    (void)    { return Scalar((int)0xff800000);    }
template <> inline Scalar Scalar::max<float>    (void)    { return Scalar((int)0x7f800000);    }
template <> inline Scalar Scalar::min<int>        (void)    { return Scalar((int)0x80000000);    }
template <> inline Scalar Scalar::max<int>        (void)    { return Scalar((int)0x7fffffff);    }
template <> inline Scalar Scalar::min<bool>        (void)    { return Scalar(false);                }
template <> inline Scalar Scalar::max<bool>        (void)    { return Scalar(true);                }
 
template <> inline float    Scalar::as<float>    (void) const    { return floatVal;    }
template <> inline float&    Scalar::as<float>    (void)            { return floatVal;    }
template <> inline int        Scalar::as<int>        (void) const    { return intVal;    }
template <> inline int&        Scalar::as<int>        (void)            { return intVal;    }
template <> inline bool        Scalar::as<bool>    (void) const    { return boolVal;    }
template <> inline bool&    Scalar::as<bool>    (void)            { return boolVal;    }
 
template <int Stride>
class StridedValueRead
{
public:
                               StridedValueRead        (const VariableType& type, const Scalar* value) : m_type(type), m_value(value) {}
 
   const VariableType&            getType                    (void) const            { return m_type;    }
   const Scalar*                getValuePtr                (void) const            { return m_value;    }
 
private:
   const VariableType&            m_type;
   const Scalar*                m_value;
};
 
template <int Stride>
class ConstStridedValueAccess
{
public:
                               ConstStridedValueAccess    (void) : m_type(DE_NULL), m_value(DE_NULL) {}
                               ConstStridedValueAccess    (const VariableType& type, const Scalar* valuePtr) : m_type(&type), m_value(const_cast<Scalar*>(valuePtr)) {}
 
   const VariableType&            getType                    (void) const            { return *m_type;            }
 
   // Read-only access
   ConstStridedValueAccess        component                (int compNdx) const        { return ConstStridedValueAccess(getType().getElementType(), m_value + Stride*compNdx);                                                    }
   ConstStridedValueAccess        arrayElement            (int elementNdx) const    { return ConstStridedValueAccess(getType().getElementType(), m_value + Stride*getType().getElementScalarOffset(elementNdx));                }
   ConstStridedValueAccess        member                    (int memberNdx) const    { return ConstStridedValueAccess(getType().getMembers()[memberNdx].getType(), m_value + Stride*getType().getMemberScalarOffset(memberNdx));    }
 
   float                        asFloat                    (void) const            { DE_STATIC_ASSERT(Stride == 1); return m_value->floatVal;    }
   int                            asInt                    (void) const            { DE_STATIC_ASSERT(Stride == 1); return m_value->intVal;    }
   bool                        asBool                    (void) const            { DE_STATIC_ASSERT(Stride == 1); return m_value->boolVal;    }
   Scalar                        asScalar                (void) const            { DE_STATIC_ASSERT(Stride == 1); return *m_value;            }
 
   float                        asFloat                    (int ndx) const            { DE_ASSERT(de::inBounds(ndx, 0, Stride)); return m_value[ndx].floatVal;    }
   int                            asInt                    (int ndx) const            { DE_ASSERT(de::inBounds(ndx, 0, Stride)); return m_value[ndx].intVal;    }
   bool                        asBool                    (int ndx) const            { DE_ASSERT(de::inBounds(ndx, 0, Stride)); return m_value[ndx].boolVal;    }
   Scalar                        asScalar                (int ndx) const            { DE_ASSERT(de::inBounds(ndx, 0, Stride)); return m_value[ndx];            }
 
   template <typename T>
   T                            as                        (int ndx) const            { DE_ASSERT(de::inBounds(ndx, 0, Stride)); return this->m_value[ndx].template as<T>();    }
 
   // For assignment: b = a.value()
   StridedValueRead<Stride>    value                    (void) const            { return StridedValueRead<Stride>(getType(), m_value);        }
 
protected:
   const VariableType*            m_type;
   Scalar*                        m_value;    // \note Non-const internal pointer is used so that ValueAccess can extend this class with RW access
};
 
template <int Stride>
class StridedValueAccess : public ConstStridedValueAccess<Stride>
{
public:
                               StridedValueAccess    (void) {}
                               StridedValueAccess    (const VariableType& type, Scalar* valuePtr) : ConstStridedValueAccess<Stride>(type, valuePtr) {}
 
   // Read-write access
   StridedValueAccess            component            (int compNdx)        { return StridedValueAccess(this->getType().getElementType(), this->m_value + Stride*compNdx);                                                    }
   StridedValueAccess            arrayElement        (int elementNdx)    { return StridedValueAccess(this->getType().getElementType(), this->m_value + Stride*this->getType().getElementScalarOffset(elementNdx));                    }
   StridedValueAccess            member                (int memberNdx)        { return StridedValueAccess(this->getType().getMembers()[memberNdx].getType(), this->m_value + Stride*this->getType().getMemberScalarOffset(memberNdx));    }
 
   float&                        asFloat                (void)                { DE_STATIC_ASSERT(Stride == 1); return this->m_value->floatVal;    }
   int&                        asInt                (void)                { DE_STATIC_ASSERT(Stride == 1); return this->m_value->intVal;        }
   bool&                        asBool                (void)                { DE_STATIC_ASSERT(Stride == 1); return this->m_value->boolVal;        }
   Scalar&                        asScalar            (void)                { DE_STATIC_ASSERT(Stride == 1); return *this->m_value;                }
 
   float&                        asFloat                (int ndx)            { DE_ASSERT(de::inBounds(ndx, 0, Stride)); return this->m_value[ndx].floatVal;    }
   int&                        asInt                (int ndx)            { DE_ASSERT(de::inBounds(ndx, 0, Stride)); return this->m_value[ndx].intVal;        }
   bool&                        asBool                (int ndx)            { DE_ASSERT(de::inBounds(ndx, 0, Stride)); return this->m_value[ndx].boolVal;        }
   Scalar&                        asScalar            (int ndx)            { DE_ASSERT(de::inBounds(ndx, 0, Stride)); return this->m_value[ndx];                }
 
   template <typename T>
   T&                            as                    (int ndx)            { DE_ASSERT(de::inBounds(ndx, 0, Stride)); return this->m_value[ndx].template as<T>();        }
 
   template <int SrcStride>
   StridedValueAccess&            operator=            (const StridedValueRead<SrcStride>& value);
 
   // Helpers, work only in Stride == 1 case
   template <int Size>
   StridedValueAccess&            operator=            (const tcu::Vector<float, Size>& vec);
   StridedValueAccess&            operator=            (float floatVal)    { asFloat()        = floatVal;    return *this;    }
   StridedValueAccess&            operator=            (int intVal)        { asInt()        = intVal;    return *this;    }
   StridedValueAccess&            operator=            (bool boolVal)        { asBool()        = boolVal;    return *this;    }
   StridedValueAccess&            operator=            (Scalar val)        { asScalar()    = val;        return *this;    }
};
 
template <int Stride>
template <int SrcStride>
StridedValueAccess<Stride>& StridedValueAccess<Stride>::operator= (const StridedValueRead<SrcStride>& valueRead)
{
   DE_STATIC_ASSERT(SrcStride == Stride || SrcStride == 1);
   DE_ASSERT(this->getType() == valueRead.getType());
 
   int scalarSize = this->getType().getScalarSize();
 
   if (scalarSize == 0)
       return *this; // Happens when void value range is copied
 
   if (Stride == SrcStride)
       std::copy(valueRead.getValuePtr(), valueRead.getValuePtr() + scalarSize*Stride, this->m_value);
   else
   {
       for (int scalarNdx = 0; scalarNdx < scalarSize; scalarNdx++)
           std::fill(this->m_value + scalarNdx*Stride, this->m_value + (scalarNdx+1)*Stride, valueRead.getValuePtr()[scalarNdx]);
   }
 
   return *this;
}
 
template <int Stride>
template <int Size>
StridedValueAccess<Stride>& StridedValueAccess<Stride>::operator= (const tcu::Vector<float, Size>& vec)
{
   DE_ASSERT(this->getType() == VariableType(VariableType::TYPE_FLOAT, Size));
   for (int comp = 0; comp < 4; comp++)
       component(comp).asFloat() = vec.getPtr()[comp];
 
   return *this;
}
 
// Typedefs for stride == 1 case
typedef ConstStridedValueAccess<1>    ConstValueAccess;
typedef StridedValueAccess<1>        ValueAccess;
 
class ConstValueRangeAccess
{
public:
                               ConstValueRangeAccess    (void) : m_type(DE_NULL), m_min(DE_NULL), m_max(DE_NULL) {}
                               ConstValueRangeAccess    (const VariableType& type, const Scalar* minVal, const Scalar* maxVal) : m_type(&type), m_min(const_cast<Scalar*>(minVal)), m_max(const_cast<Scalar*>(maxVal)) {}
 
   const VariableType&            getType                    (void) const    { return *m_type;                            }
   ConstValueAccess            getMin                    (void) const    { return ConstValueAccess(*m_type, m_min);    }
   ConstValueAccess            getMax                    (void) const    { return ConstValueAccess(*m_type, m_max);    }
 
   // Read-only access
   ConstValueRangeAccess        component                (int compNdx) const;
   ConstValueRangeAccess        arrayElement            (int elementNdx) const;
   ConstValueRangeAccess        member                    (int memberNdx) const;
 
   // Set operations - tests condition for all elements
   bool                        intersects                (const ConstValueRangeAccess& other) const;
   bool                        isSupersetOf            (const ConstValueRangeAccess& other) const;
   bool                        isSubsetOf                (const ConstValueRangeAccess& other) const;
 
protected:
   const VariableType*            m_type;
   Scalar*                        m_min;    // \note See note in ConstValueAccess
   Scalar*                        m_max;
};
 
inline ConstValueRangeAccess ConstValueRangeAccess::component (int compNdx) const
{
   return ConstValueRangeAccess(m_type->getElementType(), m_min + compNdx, m_max + compNdx);
}
 
inline ConstValueRangeAccess ConstValueRangeAccess::arrayElement (int elementNdx) const
{
   int offset = m_type->getElementScalarOffset(elementNdx);
   return ConstValueRangeAccess(m_type->getElementType(), m_min + offset, m_max + offset);
}
 
inline ConstValueRangeAccess ConstValueRangeAccess::member (int memberNdx) const
{
   int offset = m_type->getMemberScalarOffset(memberNdx);
   return ConstValueRangeAccess(m_type->getMembers()[memberNdx].getType(), m_min + offset, m_max + offset);
}
 
class ValueRangeAccess : public ConstValueRangeAccess
{
public:
                               ValueRangeAccess        (const VariableType& type, Scalar* minVal, Scalar* maxVal) : ConstValueRangeAccess(type, minVal, maxVal) {}
 
   // Read-write access
   ValueAccess                    getMin                    (void) { return ValueAccess(*m_type, m_min);    }
   ValueAccess                    getMax                    (void) { return ValueAccess(*m_type, m_max);    }
 
   ValueRangeAccess            component                (int compNdx);
   ValueRangeAccess            arrayElement            (int elementNdx);
   ValueRangeAccess            member                    (int memberNdx);
};
 
inline ValueRangeAccess ValueRangeAccess::component (int compNdx)
{
   return ValueRangeAccess(m_type->getElementType(), m_min + compNdx, m_max + compNdx);
}
 
inline ValueRangeAccess ValueRangeAccess::arrayElement (int elementNdx)
{
   int offset = m_type->getElementScalarOffset(elementNdx);
   return ValueRangeAccess(m_type->getElementType(), m_min + offset, m_max + offset);
}
 
inline ValueRangeAccess ValueRangeAccess::member (int memberNdx)
{
   int offset = m_type->getMemberScalarOffset(memberNdx);
   return ValueRangeAccess(m_type->getMembers()[memberNdx].getType(), m_min + offset, m_max + offset);
}
 
class ValueRange
{
public:
                               ValueRange            (const VariableType& type);
                               ValueRange            (const VariableType& type, const ConstValueAccess& minVal, const ConstValueAccess& maxVal);
                               ValueRange            (const VariableType& type, const Scalar* minVal, const Scalar* maxVal);
                               ValueRange            (ConstValueRangeAccess other);
                               ~ValueRange            (void);
 
   const VariableType&            getType                (void) const    { return m_type;                                }
 
   ValueAccess                    getMin                (void)            { return ValueAccess(m_type, getMinPtr());        }
   ValueAccess                    getMax                (void)            { return ValueAccess(m_type, getMaxPtr());        }
 
   ConstValueAccess            getMin                (void) const    { return ConstValueAccess(m_type, getMinPtr());    }
   ConstValueAccess            getMax                (void) const    { return ConstValueAccess(m_type, getMaxPtr());    }
 
   ValueRangeAccess            asAccess            (void)            { return ValueRangeAccess(m_type, getMinPtr(), getMaxPtr());        }
   ConstValueRangeAccess        asAccess            (void) const    { return ConstValueRangeAccess(m_type, getMinPtr(), getMaxPtr());    }
 
   operator ConstValueRangeAccess                    (void) const    { return asAccess();                            }
   operator ValueRangeAccess                        (void)            { return asAccess();                            }
 
   static void                    computeIntersection    (ValueRangeAccess dst, const ConstValueRangeAccess& a, const ConstValueRangeAccess& b);
   static void                    computeIntersection    (ValueRange& dst, const ConstValueRangeAccess& a, const ConstValueRangeAccess& b);
 
private:
   const Scalar*                getMinPtr            (void) const    { return m_min.empty() ? DE_NULL : &m_min[0];    }
   const Scalar*                getMaxPtr            (void) const    { return m_max.empty() ? DE_NULL : &m_max[0];    }
 
   Scalar*                        getMinPtr            (void)            { return m_min.empty() ? DE_NULL : &m_min[0];    }
   Scalar*                        getMaxPtr            (void)            { return m_max.empty() ? DE_NULL : &m_max[0];    }
 
   VariableType                m_type;
   std::vector<Scalar>            m_min;
   std::vector<Scalar>            m_max;
};
 
template <int Stride>
class ValueStorage
{
public:
                                       ValueStorage        (void);
                                       ValueStorage        (const VariableType& type);
 
   void                                setStorage            (const VariableType& type);
 
   StridedValueAccess<Stride>            getValue            (const VariableType& type)            { return StridedValueAccess<Stride>(type, &m_value[0]);            }
   ConstStridedValueAccess<Stride>        getValue            (const VariableType& type) const    { return ConstStridedValueAccess<Stride>(type, &m_value[0]);    }
 
private:
                                       ValueStorage        (const ValueStorage& other);
   ValueStorage                        operator=            (const ValueStorage& other);
 
   std::vector<Scalar>                    m_value;
};
 
template <int Stride>
ValueStorage<Stride>::ValueStorage (void)
{
}
 
template <int Stride>
ValueStorage<Stride>::ValueStorage (const VariableType& type)
{
   setStorage(type);
}
 
template <int Stride>
void ValueStorage<Stride>::setStorage (const VariableType& type)
{
   m_value.resize(type.getScalarSize() * Stride);
}
 
class VariableValue
{
public:
                           VariableValue        (const Variable* variable) : m_variable(variable), m_storage(m_variable->getType()) {}
                           ~VariableValue        (void) {}
 
   const Variable*            getVariable            (void) const    { return m_variable;                                }
   ValueAccess                getValue            (void)            { return m_storage.getValue(m_variable->getType());    }
   ConstValueAccess        getValue            (void) const    { return m_storage.getValue(m_variable->getType());    }
 
                           VariableValue        (const VariableValue& other);
   VariableValue&            operator=            (const VariableValue& other);
 
private:
   const VariableType&        getType                (void) const    { return m_variable->getType();                        }
 
   const Variable*            m_variable;
   ValueStorage<1>            m_storage;
};
 
} // rsg
 
#endif // _RSGVARIABLEVALUE_HPP