2 //Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3 //Copyright (C) 2013 LunarG, Inc.
7 //Redistribution and use in source and binary forms, with or without
8 //modification, are permitted provided that the following conditions
11 // Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
14 // Redistributions in binary form must reproduce the above
15 // copyright notice, this list of conditions and the following
16 // disclaimer in the documentation and/or other materials provided
17 // with the distribution.
19 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
20 // contributors may be used to endorse or promote products derived
21 // from this software without specific prior written permission.
23 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 //FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 //COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 //INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 //CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 //ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 //POSSIBILITY OF SUCH DAMAGE.
37 #ifndef _CONSTANT_UNION_INCLUDED_
38 #define _CONSTANT_UNION_INCLUDED_
44 POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator())
52 void setUConst(unsigned int u)
58 void setDConst(double d)
64 void setBConst(bool b)
70 int getIConst() const { return iConst; }
71 unsigned int getUConst() const { return uConst; }
72 double getDConst() const { return dConst; }
73 bool getBConst() const { return bConst; }
75 bool operator==(const int i) const
83 bool operator==(unsigned const int u) const
91 bool operator==(const double d) const
99 bool operator==(const bool b) const
107 bool operator==(const TConstUnion& constant) const
109 if (constant.type != type)
114 if (constant.iConst == iConst)
119 if (constant.uConst == uConst)
124 if (constant.dConst == dConst)
129 if (constant.bConst == bConst)
134 assert(false && "Default missing");
140 bool operator!=(const int i) const
142 return !operator==(i);
145 bool operator!=(const unsigned int u) const
147 return !operator==(u);
150 bool operator!=(const float f) const
152 return !operator==(f);
155 bool operator!=(const bool b) const
157 return !operator==(b);
160 bool operator!=(const TConstUnion& constant) const
162 return !operator==(constant);
165 bool operator>(const TConstUnion& constant) const
167 assert(type == constant.type);
170 if (iConst > constant.iConst)
175 if (uConst > constant.uConst)
180 if (dConst > constant.dConst)
185 assert(false && "Default missing");
190 bool operator<(const TConstUnion& constant) const
192 assert(type == constant.type);
195 if (iConst < constant.iConst)
200 if (uConst < constant.uConst)
205 if (dConst < constant.dConst)
210 assert(false && "Default missing");
215 TConstUnion operator+(const TConstUnion& constant) const
217 TConstUnion returnValue;
218 assert(type == constant.type);
220 case EbtInt: returnValue.setIConst(iConst + constant.iConst); break;
221 case EbtUint: returnValue.setUConst(uConst + constant.uConst); break;
222 case EbtDouble: returnValue.setDConst(dConst + constant.dConst); break;
223 default: assert(false && "Default missing");
229 TConstUnion operator-(const TConstUnion& constant) const
231 TConstUnion returnValue;
232 assert(type == constant.type);
234 case EbtInt: returnValue.setIConst(iConst - constant.iConst); break;
235 case EbtUint: returnValue.setUConst(uConst - constant.uConst); break;
236 case EbtDouble: returnValue.setDConst(dConst - constant.dConst); break;
237 default: assert(false && "Default missing");
243 TConstUnion operator*(const TConstUnion& constant) const
245 TConstUnion returnValue;
246 assert(type == constant.type);
248 case EbtInt: returnValue.setIConst(iConst * constant.iConst); break;
249 case EbtUint: returnValue.setUConst(uConst * constant.uConst); break;
250 case EbtDouble: returnValue.setDConst(dConst * constant.dConst); break;
251 default: assert(false && "Default missing");
257 TConstUnion operator%(const TConstUnion& constant) const
259 TConstUnion returnValue;
260 assert(type == constant.type);
262 case EbtInt: returnValue.setIConst(iConst % constant.iConst); break;
263 case EbtUint: returnValue.setUConst(uConst % constant.uConst); break;
264 default: assert(false && "Default missing");
270 TConstUnion operator>>(const TConstUnion& constant) const
272 TConstUnion returnValue;
275 switch (constant.type) {
276 case EbtInt: returnValue.setIConst(iConst >> constant.iConst); break;
277 case EbtUint: returnValue.setIConst(iConst >> constant.uConst); break;
278 default: assert(false && "Default missing");
282 switch (constant.type) {
283 case EbtInt: returnValue.setUConst(uConst >> constant.iConst); break;
284 case EbtUint: returnValue.setUConst(uConst >> constant.uConst); break;
285 default: assert(false && "Default missing");
288 default: assert(false && "Default missing");
294 TConstUnion operator<<(const TConstUnion& constant) const
296 TConstUnion returnValue;
299 switch (constant.type) {
300 case EbtInt: returnValue.setIConst(iConst << constant.iConst); break;
301 case EbtUint: returnValue.setIConst(iConst << constant.uConst); break;
302 default: assert(false && "Default missing");
306 switch (constant.type) {
307 case EbtInt: returnValue.setUConst(uConst << constant.iConst); break;
308 case EbtUint: returnValue.setUConst(uConst << constant.uConst); break;
309 default: assert(false && "Default missing");
312 default: assert(false && "Default missing");
318 TConstUnion operator&(const TConstUnion& constant) const
320 TConstUnion returnValue;
321 assert(type == constant.type);
323 case EbtInt: returnValue.setIConst(iConst & constant.iConst); break;
324 case EbtUint: returnValue.setUConst(uConst & constant.uConst); break;
325 default: assert(false && "Default missing");
331 TConstUnion operator|(const TConstUnion& constant) const
333 TConstUnion returnValue;
334 assert(type == constant.type);
336 case EbtInt: returnValue.setIConst(iConst | constant.iConst); break;
337 case EbtUint: returnValue.setUConst(uConst | constant.uConst); break;
338 default: assert(false && "Default missing");
344 TConstUnion operator^(const TConstUnion& constant) const
346 TConstUnion returnValue;
347 assert(type == constant.type);
349 case EbtInt: returnValue.setIConst(iConst ^ constant.iConst); break;
350 case EbtUint: returnValue.setUConst(uConst ^ constant.uConst); break;
351 default: assert(false && "Default missing");
357 TConstUnion operator~() const
359 TConstUnion returnValue;
361 case EbtInt: returnValue.setIConst(~iConst); break;
362 case EbtUint: returnValue.setUConst(~uConst); break;
363 default: assert(false && "Default missing");
369 TConstUnion operator&&(const TConstUnion& constant) const
371 TConstUnion returnValue;
372 assert(type == constant.type);
374 case EbtBool: returnValue.setBConst(bConst && constant.bConst); break;
375 default: assert(false && "Default missing");
381 TConstUnion operator||(const TConstUnion& constant) const
383 TConstUnion returnValue;
384 assert(type == constant.type);
386 case EbtBool: returnValue.setBConst(bConst || constant.bConst); break;
387 default: assert(false && "Default missing");
393 TBasicType getType() const { return type; }
397 int iConst; // used for ivec, scalar ints
398 unsigned int uConst; // used for uvec, scalar uints
399 bool bConst; // used for bvec, scalar bools
400 double dConst; // used for vec, dvec, mat, dmat, scalar floats and doubles
406 // Encapsulate having a pointer to an array of TConstUnion,
407 // which only needs to be allocated if it's size is going to be
410 // One convenience is being able to use [] to go inside the array, instead
411 // of C++ assuming it as an array of pointers to vectors.
413 // General usage is that the size is known up front, and it is
414 // created once with the proper size.
416 class TConstUnionArray {
418 POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator())
420 TConstUnionArray() : unionArray(0) { }
421 virtual ~TConstUnionArray() { }
423 explicit TConstUnionArray(int size)
428 unionArray = new TConstUnionVector(size);
430 TConstUnionArray(const TConstUnionArray& a) : unionArray(a.unionArray) { }
431 TConstUnionArray(const TConstUnionArray& a, int start, int size)
433 unionArray = new TConstUnionVector(size);
434 for (int i = 0; i < size; ++i)
435 (*unionArray)[i] = a[start + i];
438 // Use this constructor for a smear operation
439 TConstUnionArray(int size, const TConstUnion& val)
441 unionArray = new TConstUnionVector(size, val);
444 int size() const { return unionArray ? (int)unionArray->size() : 0; }
445 TConstUnion& operator[](size_t index) { return (*unionArray)[index]; }
446 const TConstUnion& operator[](size_t index) const { return (*unionArray)[index]; }
447 bool operator==(const TConstUnionArray& rhs) const
449 // this includes the case that both are unallocated
450 if (unionArray == rhs.unionArray)
453 if (! unionArray || ! rhs.unionArray)
456 if (! unionArray || ! rhs.unionArray)
459 return *unionArray == *rhs.unionArray;
461 bool operator!=(const TConstUnionArray& rhs) const { return ! operator==(rhs); }
463 double dot(const TConstUnionArray& rhs)
465 assert(rhs.unionArray->size() == unionArray->size());
468 for (size_t comp = 0; comp < unionArray->size(); ++comp)
469 sum += (*this)[comp].getDConst() * rhs[comp].getDConst();
474 bool empty() const { return unionArray == 0; }
477 typedef TVector<TConstUnion> TConstUnionVector;
478 TConstUnionVector* unionArray;
481 } // end namespace glslang
483 #endif // _CONSTANT_UNION_INCLUDED_