blob: 055d5292497f4a8020bd27ea2218af865440eac0 [file] [log] [blame]
Jamie Madillb1a85f42014-08-19 15:23:24 -04001//
2// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7//
8// Build the intermediate representation.
9//
10
11#include <float.h>
12#include <limits.h>
Arun Patole9dea48f2015-04-02 11:45:09 +053013#include <math.h>
Arun Patole97dc22e2015-04-06 17:35:38 +053014#include <stdlib.h>
Jamie Madillb1a85f42014-08-19 15:23:24 -040015#include <algorithm>
Arun Patole274f0702015-05-05 13:33:30 +053016#include <vector>
Jamie Madillb1a85f42014-08-19 15:23:24 -040017
Arun Patole274f0702015-05-05 13:33:30 +053018#include "common/mathutil.h"
Arun Patole7fa33552015-06-10 15:15:18 +053019#include "common/matrix_utils.h"
Olli Etuaho3fdec912016-08-18 15:08:06 +030020#include "compiler/translator/Diagnostics.h"
Jamie Madillb1a85f42014-08-19 15:23:24 -040021#include "compiler/translator/HashNames.h"
22#include "compiler/translator/IntermNode.h"
23#include "compiler/translator/SymbolTable.h"
Corentin Wallez509e4562016-08-25 14:55:44 -040024#include "compiler/translator/util.h"
Jamie Madillb1a85f42014-08-19 15:23:24 -040025
26namespace
27{
28
Arun Patole9dea48f2015-04-02 11:45:09 +053029const float kPi = 3.14159265358979323846f;
30const float kDegreesToRadiansMultiplier = kPi / 180.0f;
31const float kRadiansToDegreesMultiplier = 180.0f / kPi;
32
Jamie Madillb1a85f42014-08-19 15:23:24 -040033TPrecision GetHigherPrecision(TPrecision left, TPrecision right)
34{
35 return left > right ? left : right;
36}
37
Arun Patole274f0702015-05-05 13:33:30 +053038TConstantUnion *Vectorize(const TConstantUnion &constant, size_t size)
39{
40 TConstantUnion *constUnion = new TConstantUnion[size];
41 for (unsigned int i = 0; i < size; ++i)
42 constUnion[i] = constant;
43
44 return constUnion;
45}
46
Olli Etuahof119a262016-08-19 15:54:22 +030047void UndefinedConstantFoldingError(const TSourceLoc &loc,
48 TOperator op,
49 TBasicType basicType,
50 TDiagnostics *diagnostics,
51 TConstantUnion *result)
Arun Patolebf790422015-05-18 17:53:04 +053052{
Olli Etuahof119a262016-08-19 15:54:22 +030053 diagnostics->warning(loc, "operation result is undefined for the values passed in",
54 GetOperatorString(op), "");
Arun Patolebf790422015-05-18 17:53:04 +053055
56 switch (basicType)
57 {
58 case EbtFloat :
59 result->setFConst(0.0f);
60 break;
61 case EbtInt:
62 result->setIConst(0);
63 break;
64 case EbtUInt:
65 result->setUConst(0u);
66 break;
67 case EbtBool:
68 result->setBConst(false);
69 break;
70 default:
71 break;
72 }
73}
74
Olli Etuaho5c0e0232015-11-11 15:55:59 +020075float VectorLength(const TConstantUnion *paramArray, size_t paramArraySize)
Arun Patole1155ddd2015-06-05 18:04:36 +053076{
77 float result = 0.0f;
78 for (size_t i = 0; i < paramArraySize; i++)
79 {
80 float f = paramArray[i].getFConst();
81 result += f * f;
82 }
83 return sqrtf(result);
84}
85
Olli Etuaho5c0e0232015-11-11 15:55:59 +020086float VectorDotProduct(const TConstantUnion *paramArray1,
87 const TConstantUnion *paramArray2,
88 size_t paramArraySize)
Arun Patole1155ddd2015-06-05 18:04:36 +053089{
90 float result = 0.0f;
91 for (size_t i = 0; i < paramArraySize; i++)
92 result += paramArray1[i].getFConst() * paramArray2[i].getFConst();
93 return result;
94}
95
Olli Etuaho3272a6d2016-08-29 17:54:50 +030096TIntermTyped *CreateFoldedNode(const TConstantUnion *constArray,
Olli Etuaho7c3848e2015-11-04 13:19:17 +020097 const TIntermTyped *originalNode,
98 TQualifier qualifier)
Olli Etuahob43846e2015-06-02 18:18:57 +030099{
100 if (constArray == nullptr)
101 {
102 return nullptr;
103 }
104 TIntermTyped *folded = new TIntermConstantUnion(constArray, originalNode->getType());
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200105 folded->getTypePointer()->setQualifier(qualifier);
Olli Etuahob43846e2015-06-02 18:18:57 +0300106 folded->setLine(originalNode->getLine());
107 return folded;
108}
109
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200110angle::Matrix<float> GetMatrix(const TConstantUnion *paramArray,
111 const unsigned int &rows,
112 const unsigned int &cols)
Arun Patole7fa33552015-06-10 15:15:18 +0530113{
114 std::vector<float> elements;
115 for (size_t i = 0; i < rows * cols; i++)
116 elements.push_back(paramArray[i].getFConst());
117 // Transpose is used since the Matrix constructor expects arguments in row-major order,
Olli Etuahod5da5052016-08-29 13:16:55 +0300118 // whereas the paramArray is in column-major order. Rows/cols parameters are also flipped below
119 // so that the created matrix will have the expected dimensions after the transpose.
120 return angle::Matrix<float>(elements, cols, rows).transpose();
Arun Patole7fa33552015-06-10 15:15:18 +0530121}
122
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200123angle::Matrix<float> GetMatrix(const TConstantUnion *paramArray, const unsigned int &size)
Arun Patole7fa33552015-06-10 15:15:18 +0530124{
125 std::vector<float> elements;
126 for (size_t i = 0; i < size * size; i++)
127 elements.push_back(paramArray[i].getFConst());
128 // Transpose is used since the Matrix constructor expects arguments in row-major order,
129 // whereas the paramArray is in column-major order.
130 return angle::Matrix<float>(elements, size).transpose();
131}
132
133void SetUnionArrayFromMatrix(const angle::Matrix<float> &m, TConstantUnion *resultArray)
134{
135 // Transpose is used since the input Matrix is in row-major order,
136 // whereas the actual result should be in column-major order.
137 angle::Matrix<float> result = m.transpose();
138 std::vector<float> resultElements = result.elements();
139 for (size_t i = 0; i < resultElements.size(); i++)
140 resultArray[i].setFConst(resultElements[i]);
141}
142
Jamie Madillb1a85f42014-08-19 15:23:24 -0400143} // namespace anonymous
144
145
146////////////////////////////////////////////////////////////////
147//
148// Member functions of the nodes used for building the tree.
149//
150////////////////////////////////////////////////////////////////
151
Olli Etuahod2a67b92014-10-21 16:42:57 +0300152void TIntermTyped::setTypePreservePrecision(const TType &t)
153{
154 TPrecision precision = getPrecision();
155 mType = t;
156 ASSERT(mType.getBasicType() != EbtBool || precision == EbpUndefined);
157 mType.setPrecision(precision);
158}
159
Jamie Madillb1a85f42014-08-19 15:23:24 -0400160#define REPLACE_IF_IS(node, type, original, replacement) \
161 if (node == original) { \
162 node = static_cast<type *>(replacement); \
163 return true; \
164 }
165
166bool TIntermLoop::replaceChildNode(
167 TIntermNode *original, TIntermNode *replacement)
168{
Olli Etuaho3cbb27a2016-07-14 11:55:48 +0300169 ASSERT(original != nullptr); // This risks replacing multiple children.
Jamie Madillb1a85f42014-08-19 15:23:24 -0400170 REPLACE_IF_IS(mInit, TIntermNode, original, replacement);
171 REPLACE_IF_IS(mCond, TIntermTyped, original, replacement);
172 REPLACE_IF_IS(mExpr, TIntermTyped, original, replacement);
Olli Etuaho3fed4302015-11-02 12:26:02 +0200173 REPLACE_IF_IS(mBody, TIntermAggregate, original, replacement);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400174 return false;
175}
176
Jamie Madillb1a85f42014-08-19 15:23:24 -0400177bool TIntermBranch::replaceChildNode(
178 TIntermNode *original, TIntermNode *replacement)
179{
180 REPLACE_IF_IS(mExpression, TIntermTyped, original, replacement);
181 return false;
182}
183
Jamie Madillb1a85f42014-08-19 15:23:24 -0400184bool TIntermBinary::replaceChildNode(
185 TIntermNode *original, TIntermNode *replacement)
186{
187 REPLACE_IF_IS(mLeft, TIntermTyped, original, replacement);
188 REPLACE_IF_IS(mRight, TIntermTyped, original, replacement);
189 return false;
190}
191
Jamie Madillb1a85f42014-08-19 15:23:24 -0400192bool TIntermUnary::replaceChildNode(
193 TIntermNode *original, TIntermNode *replacement)
194{
Olli Etuahoa2234302016-08-31 12:05:39 +0300195 ASSERT(original->getAsTyped()->getType() == replacement->getAsTyped()->getType());
Jamie Madillb1a85f42014-08-19 15:23:24 -0400196 REPLACE_IF_IS(mOperand, TIntermTyped, original, replacement);
197 return false;
198}
199
Jamie Madillb1a85f42014-08-19 15:23:24 -0400200bool TIntermAggregate::replaceChildNode(
201 TIntermNode *original, TIntermNode *replacement)
202{
203 for (size_t ii = 0; ii < mSequence.size(); ++ii)
204 {
205 REPLACE_IF_IS(mSequence[ii], TIntermNode, original, replacement);
206 }
207 return false;
208}
209
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300210bool TIntermAggregate::replaceChildNodeWithMultiple(TIntermNode *original, TIntermSequence replacements)
211{
212 for (auto it = mSequence.begin(); it < mSequence.end(); ++it)
213 {
214 if (*it == original)
215 {
216 it = mSequence.erase(it);
Olli Etuaho8fee0ab2015-04-23 14:52:46 +0300217 mSequence.insert(it, replacements.begin(), replacements.end());
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300218 return true;
219 }
220 }
221 return false;
222}
223
Olli Etuahoa6f22092015-05-08 18:31:10 +0300224bool TIntermAggregate::insertChildNodes(TIntermSequence::size_type position, TIntermSequence insertions)
225{
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300226 if (position > mSequence.size())
Olli Etuahoa6f22092015-05-08 18:31:10 +0300227 {
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300228 return false;
Olli Etuahoa6f22092015-05-08 18:31:10 +0300229 }
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300230 auto it = mSequence.begin() + position;
231 mSequence.insert(it, insertions.begin(), insertions.end());
232 return true;
Olli Etuahoa6f22092015-05-08 18:31:10 +0300233}
234
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200235bool TIntermAggregate::areChildrenConstQualified()
236{
237 for (TIntermNode *&child : mSequence)
238 {
239 TIntermTyped *typed = child->getAsTyped();
240 if (typed && typed->getQualifier() != EvqConst)
241 {
242 return false;
243 }
244 }
245 return true;
246}
247
Olli Etuahod2a67b92014-10-21 16:42:57 +0300248void TIntermAggregate::setPrecisionFromChildren()
249{
Olli Etuahoa4aa4e32015-06-04 15:54:30 +0300250 mGotPrecisionFromChildren = true;
Olli Etuahod2a67b92014-10-21 16:42:57 +0300251 if (getBasicType() == EbtBool)
252 {
253 mType.setPrecision(EbpUndefined);
254 return;
255 }
256
257 TPrecision precision = EbpUndefined;
258 TIntermSequence::iterator childIter = mSequence.begin();
259 while (childIter != mSequence.end())
260 {
261 TIntermTyped *typed = (*childIter)->getAsTyped();
262 if (typed)
263 precision = GetHigherPrecision(typed->getPrecision(), precision);
264 ++childIter;
265 }
266 mType.setPrecision(precision);
267}
268
269void TIntermAggregate::setBuiltInFunctionPrecision()
270{
271 // All built-ins returning bool should be handled as ops, not functions.
272 ASSERT(getBasicType() != EbtBool);
273
274 TPrecision precision = EbpUndefined;
275 TIntermSequence::iterator childIter = mSequence.begin();
276 while (childIter != mSequence.end())
277 {
278 TIntermTyped *typed = (*childIter)->getAsTyped();
279 // ESSL spec section 8: texture functions get their precision from the sampler.
280 if (typed && IsSampler(typed->getBasicType()))
281 {
282 precision = typed->getPrecision();
283 break;
284 }
285 ++childIter;
286 }
287 // ESSL 3.0 spec section 8: textureSize always gets highp precision.
288 // All other functions that take a sampler are assumed to be texture functions.
Olli Etuaho59f9a642015-08-06 20:38:26 +0300289 if (mName.getString().find("textureSize") == 0)
Olli Etuahod2a67b92014-10-21 16:42:57 +0300290 mType.setPrecision(EbpHigh);
291 else
292 mType.setPrecision(precision);
293}
294
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300295bool TIntermTernary::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
296{
297 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
298 REPLACE_IF_IS(mTrueExpression, TIntermTyped, original, replacement);
299 REPLACE_IF_IS(mFalseExpression, TIntermTyped, original, replacement);
300 return false;
301}
302
Olli Etuaho57961272016-09-14 13:57:46 +0300303bool TIntermIfElse::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400304{
305 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
306 REPLACE_IF_IS(mTrueBlock, TIntermNode, original, replacement);
307 REPLACE_IF_IS(mFalseBlock, TIntermNode, original, replacement);
308 return false;
309}
310
Olli Etuahoa3a36662015-02-17 13:46:51 +0200311bool TIntermSwitch::replaceChildNode(
312 TIntermNode *original, TIntermNode *replacement)
313{
314 REPLACE_IF_IS(mInit, TIntermTyped, original, replacement);
315 REPLACE_IF_IS(mStatementList, TIntermAggregate, original, replacement);
316 return false;
317}
318
319bool TIntermCase::replaceChildNode(
320 TIntermNode *original, TIntermNode *replacement)
321{
322 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
323 return false;
324}
325
Olli Etuahod7a25242015-08-18 13:49:45 +0300326TIntermTyped::TIntermTyped(const TIntermTyped &node) : TIntermNode(), mType(node.mType)
327{
328 // Copy constructor is disallowed for TIntermNode in order to disallow it for subclasses that
329 // don't explicitly allow it, so normal TIntermNode constructor is used to construct the copy.
330 // We need to manually copy any fields of TIntermNode besides handling fields in TIntermTyped.
331 mLine = node.mLine;
332}
333
Olli Etuahod4f4c112016-04-15 15:11:24 +0300334bool TIntermTyped::isConstructorWithOnlyConstantUnionParameters()
335{
336 TIntermAggregate *constructor = getAsAggregate();
337 if (!constructor || !constructor->isConstructor())
338 {
339 return false;
340 }
341 for (TIntermNode *&node : *constructor->getSequence())
342 {
343 if (!node->getAsConstantUnion())
344 return false;
345 }
346 return true;
347}
348
Corentin Wallez509e4562016-08-25 14:55:44 -0400349// static
350TIntermTyped *TIntermTyped::CreateIndexNode(int index)
351{
352 TConstantUnion *u = new TConstantUnion[1];
353 u[0].setIConst(index);
354
355 TType type(EbtInt, EbpUndefined, EvqConst, 1);
356 TIntermConstantUnion *node = new TIntermConstantUnion(u, type);
357 return node;
358}
359
360// static
361TIntermTyped *TIntermTyped::CreateZero(const TType &type)
362{
363 TType constType(type);
364 constType.setQualifier(EvqConst);
365
366 if (!type.isArray() && type.getBasicType() != EbtStruct)
367 {
368 ASSERT(type.isScalar() || type.isVector() || type.isMatrix());
369
370 size_t size = constType.getObjectSize();
371 TConstantUnion *u = new TConstantUnion[size];
372 for (size_t i = 0; i < size; ++i)
373 {
374 switch (type.getBasicType())
375 {
376 case EbtFloat:
377 u[i].setFConst(0.0f);
378 break;
379 case EbtInt:
380 u[i].setIConst(0);
381 break;
382 case EbtUInt:
383 u[i].setUConst(0u);
384 break;
385 case EbtBool:
386 u[i].setBConst(false);
387 break;
388 default:
389 UNREACHABLE();
390 return nullptr;
391 }
392 }
393
394 TIntermConstantUnion *node = new TIntermConstantUnion(u, constType);
395 return node;
396 }
397
398 TIntermAggregate *constructor = new TIntermAggregate(sh::TypeToConstructorOperator(type));
399 constructor->setType(constType);
400
401 if (type.isArray())
402 {
403 TType elementType(type);
404 elementType.clearArrayness();
405
406 size_t arraySize = type.getArraySize();
407 for (size_t i = 0; i < arraySize; ++i)
408 {
409 constructor->getSequence()->push_back(CreateZero(elementType));
410 }
411 }
412 else
413 {
414 ASSERT(type.getBasicType() == EbtStruct);
415
416 TStructure *structure = type.getStruct();
417 for (const auto &field : structure->fields())
418 {
419 constructor->getSequence()->push_back(CreateZero(*field->type()));
420 }
421 }
422
423 return constructor;
424}
425
Olli Etuahod7a25242015-08-18 13:49:45 +0300426TIntermConstantUnion::TIntermConstantUnion(const TIntermConstantUnion &node) : TIntermTyped(node)
427{
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200428 mUnionArrayPointer = node.mUnionArrayPointer;
Olli Etuahod7a25242015-08-18 13:49:45 +0300429}
430
431TIntermAggregate::TIntermAggregate(const TIntermAggregate &node)
432 : TIntermOperator(node),
433 mName(node.mName),
434 mUserDefined(node.mUserDefined),
435 mFunctionId(node.mFunctionId),
Olli Etuahod7a25242015-08-18 13:49:45 +0300436 mUseEmulatedFunction(node.mUseEmulatedFunction),
437 mGotPrecisionFromChildren(node.mGotPrecisionFromChildren)
438{
439 for (TIntermNode *child : node.mSequence)
440 {
441 TIntermTyped *typedChild = child->getAsTyped();
442 ASSERT(typedChild != nullptr);
443 TIntermTyped *childCopy = typedChild->deepCopy();
444 mSequence.push_back(childCopy);
445 }
446}
447
448TIntermBinary::TIntermBinary(const TIntermBinary &node)
449 : TIntermOperator(node), mAddIndexClamp(node.mAddIndexClamp)
450{
451 TIntermTyped *leftCopy = node.mLeft->deepCopy();
452 TIntermTyped *rightCopy = node.mRight->deepCopy();
453 ASSERT(leftCopy != nullptr && rightCopy != nullptr);
454 mLeft = leftCopy;
455 mRight = rightCopy;
456}
457
458TIntermUnary::TIntermUnary(const TIntermUnary &node)
459 : TIntermOperator(node), mUseEmulatedFunction(node.mUseEmulatedFunction)
460{
461 TIntermTyped *operandCopy = node.mOperand->deepCopy();
462 ASSERT(operandCopy != nullptr);
463 mOperand = operandCopy;
464}
465
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300466TIntermTernary::TIntermTernary(const TIntermTernary &node) : TIntermTyped(node)
Olli Etuahod7a25242015-08-18 13:49:45 +0300467{
Olli Etuahod7a25242015-08-18 13:49:45 +0300468 TIntermTyped *conditionCopy = node.mCondition->deepCopy();
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300469 TIntermTyped *trueCopy = node.mTrueExpression->deepCopy();
470 TIntermTyped *falseCopy = node.mFalseExpression->deepCopy();
Olli Etuahod7a25242015-08-18 13:49:45 +0300471 ASSERT(conditionCopy != nullptr && trueCopy != nullptr && falseCopy != nullptr);
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300472 mCondition = conditionCopy;
473 mTrueExpression = trueCopy;
474 mFalseExpression = falseCopy;
Olli Etuahod7a25242015-08-18 13:49:45 +0300475}
476
Jamie Madillb1a85f42014-08-19 15:23:24 -0400477bool TIntermOperator::isAssignment() const
478{
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300479 return IsAssignment(mOp);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400480}
481
Olli Etuaho8f76bcc2015-06-02 13:54:20 +0300482bool TIntermOperator::isMultiplication() const
483{
484 switch (mOp)
485 {
486 case EOpMul:
487 case EOpMatrixTimesMatrix:
488 case EOpMatrixTimesVector:
489 case EOpMatrixTimesScalar:
490 case EOpVectorTimesMatrix:
491 case EOpVectorTimesScalar:
492 return true;
493 default:
494 return false;
495 }
496}
497
Jamie Madillb1a85f42014-08-19 15:23:24 -0400498//
499// returns true if the operator is for one of the constructors
500//
501bool TIntermOperator::isConstructor() const
502{
503 switch (mOp)
504 {
505 case EOpConstructVec2:
506 case EOpConstructVec3:
507 case EOpConstructVec4:
508 case EOpConstructMat2:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400509 case EOpConstructMat2x3:
510 case EOpConstructMat2x4:
511 case EOpConstructMat3x2:
Jamie Madillb1a85f42014-08-19 15:23:24 -0400512 case EOpConstructMat3:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400513 case EOpConstructMat3x4:
514 case EOpConstructMat4x2:
515 case EOpConstructMat4x3:
Jamie Madillb1a85f42014-08-19 15:23:24 -0400516 case EOpConstructMat4:
517 case EOpConstructFloat:
518 case EOpConstructIVec2:
519 case EOpConstructIVec3:
520 case EOpConstructIVec4:
521 case EOpConstructInt:
522 case EOpConstructUVec2:
523 case EOpConstructUVec3:
524 case EOpConstructUVec4:
525 case EOpConstructUInt:
526 case EOpConstructBVec2:
527 case EOpConstructBVec3:
528 case EOpConstructBVec4:
529 case EOpConstructBool:
530 case EOpConstructStruct:
531 return true;
532 default:
533 return false;
534 }
535}
536
Olli Etuaho1dded802016-08-18 18:13:13 +0300537TOperator TIntermBinary::GetMulOpBasedOnOperands(const TType &left, const TType &right)
538{
539 if (left.isMatrix())
540 {
541 if (right.isMatrix())
542 {
543 return EOpMatrixTimesMatrix;
544 }
545 else
546 {
547 if (right.isVector())
548 {
549 return EOpMatrixTimesVector;
550 }
551 else
552 {
553 return EOpMatrixTimesScalar;
554 }
555 }
556 }
557 else
558 {
559 if (right.isMatrix())
560 {
561 if (left.isVector())
562 {
563 return EOpVectorTimesMatrix;
564 }
565 else
566 {
567 return EOpMatrixTimesScalar;
568 }
569 }
570 else
571 {
572 // Neither operand is a matrix.
573 if (left.isVector() == right.isVector())
574 {
575 // Leave as component product.
576 return EOpMul;
577 }
578 else
579 {
580 return EOpVectorTimesScalar;
581 }
582 }
583 }
584}
585
586TOperator TIntermBinary::GetMulAssignOpBasedOnOperands(const TType &left, const TType &right)
587{
588 if (left.isMatrix())
589 {
590 if (right.isMatrix())
591 {
592 return EOpMatrixTimesMatrixAssign;
593 }
594 else
595 {
596 // right should be scalar, but this may not be validated yet.
597 return EOpMatrixTimesScalarAssign;
598 }
599 }
600 else
601 {
602 if (right.isMatrix())
603 {
604 // Left should be a vector, but this may not be validated yet.
605 return EOpVectorTimesMatrixAssign;
606 }
607 else
608 {
609 // Neither operand is a matrix.
610 if (left.isVector() == right.isVector())
611 {
612 // Leave as component product.
613 return EOpMulAssign;
614 }
615 else
616 {
617 // left should be vector and right should be scalar, but this may not be validated
618 // yet.
619 return EOpVectorTimesScalarAssign;
620 }
621 }
622 }
623}
624
Jamie Madillb1a85f42014-08-19 15:23:24 -0400625//
626// Make sure the type of a unary operator is appropriate for its
627// combination of operation and operand type.
628//
Olli Etuahoa2234302016-08-31 12:05:39 +0300629void TIntermUnary::promote()
Jamie Madillb1a85f42014-08-19 15:23:24 -0400630{
Olli Etuahoa2234302016-08-31 12:05:39 +0300631 TQualifier resultQualifier = EvqTemporary;
632 if (mOperand->getQualifier() == EvqConst)
633 resultQualifier = EvqConst;
634
635 unsigned char operandPrimarySize =
636 static_cast<unsigned char>(mOperand->getType().getNominalSize());
Jamie Madillb1a85f42014-08-19 15:23:24 -0400637 switch (mOp)
638 {
Olli Etuahoa2234302016-08-31 12:05:39 +0300639 case EOpFloatBitsToInt:
640 setType(TType(EbtInt, EbpHigh, resultQualifier, operandPrimarySize));
641 break;
642 case EOpFloatBitsToUint:
643 setType(TType(EbtUInt, EbpHigh, resultQualifier, operandPrimarySize));
644 break;
645 case EOpIntBitsToFloat:
646 case EOpUintBitsToFloat:
647 setType(TType(EbtFloat, EbpHigh, resultQualifier, operandPrimarySize));
648 break;
649 case EOpPackSnorm2x16:
650 case EOpPackUnorm2x16:
651 case EOpPackHalf2x16:
652 setType(TType(EbtUInt, EbpHigh, resultQualifier));
653 break;
654 case EOpUnpackSnorm2x16:
655 case EOpUnpackUnorm2x16:
656 setType(TType(EbtFloat, EbpHigh, resultQualifier, 2));
657 break;
658 case EOpUnpackHalf2x16:
659 setType(TType(EbtFloat, EbpMedium, resultQualifier, 2));
660 break;
661 case EOpAny:
662 case EOpAll:
663 setType(TType(EbtBool, EbpUndefined, resultQualifier));
664 break;
665 case EOpLength:
666 case EOpDeterminant:
667 setType(TType(EbtFloat, mOperand->getType().getPrecision(), resultQualifier));
668 break;
669 case EOpTranspose:
670 setType(TType(EbtFloat, mOperand->getType().getPrecision(), resultQualifier,
671 static_cast<unsigned char>(mOperand->getType().getRows()),
672 static_cast<unsigned char>(mOperand->getType().getCols())));
673 break;
674 case EOpIsInf:
675 case EOpIsNan:
676 setType(TType(EbtBool, EbpUndefined, resultQualifier, operandPrimarySize));
677 break;
678 default:
679 setType(mOperand->getType());
680 mType.setQualifier(resultQualifier);
681 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400682 }
Olli Etuahoa2234302016-08-31 12:05:39 +0300683}
Jamie Madillb1a85f42014-08-19 15:23:24 -0400684
Olli Etuahoa2234302016-08-31 12:05:39 +0300685TIntermUnary::TIntermUnary(TOperator op, TIntermTyped *operand)
686 : TIntermOperator(op), mOperand(operand), mUseEmulatedFunction(false)
687{
688 promote();
Jamie Madillb1a85f42014-08-19 15:23:24 -0400689}
690
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300691TIntermBinary::TIntermBinary(TOperator op, TIntermTyped *left, TIntermTyped *right)
692 : TIntermOperator(op), mLeft(left), mRight(right), mAddIndexClamp(false)
693{
694 promote();
695}
696
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300697TIntermTernary::TIntermTernary(TIntermTyped *cond,
698 TIntermTyped *trueExpression,
699 TIntermTyped *falseExpression)
700 : TIntermTyped(trueExpression->getType()),
701 mCondition(cond),
702 mTrueExpression(trueExpression),
703 mFalseExpression(falseExpression)
704{
705 getTypePointer()->setQualifier(
706 TIntermTernary::DetermineQualifier(cond, trueExpression, falseExpression));
707}
708
709// static
710TQualifier TIntermTernary::DetermineQualifier(TIntermTyped *cond,
711 TIntermTyped *trueExpression,
712 TIntermTyped *falseExpression)
713{
714 if (cond->getQualifier() == EvqConst && trueExpression->getQualifier() == EvqConst &&
715 falseExpression->getQualifier() == EvqConst)
716 {
717 return EvqConst;
718 }
719 return EvqTemporary;
720}
721
Jamie Madillb1a85f42014-08-19 15:23:24 -0400722//
723// Establishes the type of the resultant operation, as well as
724// makes the operator the correct one for the operands.
725//
Olli Etuaho47fd36a2015-03-19 14:22:24 +0200726// For lots of operations it should already be established that the operand
727// combination is valid, but returns false if operator can't work on operands.
Jamie Madillb1a85f42014-08-19 15:23:24 -0400728//
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300729void TIntermBinary::promote()
Jamie Madillb1a85f42014-08-19 15:23:24 -0400730{
Olli Etuaho1dded802016-08-18 18:13:13 +0300731 ASSERT(!isMultiplication() ||
732 mOp == GetMulOpBasedOnOperands(mLeft->getType(), mRight->getType()));
733
Jamie Madillb1a85f42014-08-19 15:23:24 -0400734 // Base assumption: just make the type the same as the left
735 // operand. Then only deviations from this need be coded.
Jamie Madillb1a85f42014-08-19 15:23:24 -0400736 setType(mLeft->getType());
737
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200738 TQualifier resultQualifier = EvqConst;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400739 // Binary operations results in temporary variables unless both
740 // operands are const.
741 if (mLeft->getQualifier() != EvqConst || mRight->getQualifier() != EvqConst)
742 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200743 resultQualifier = EvqTemporary;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400744 getTypePointer()->setQualifier(EvqTemporary);
745 }
746
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300747 // Handle indexing ops.
748 switch (mOp)
749 {
750 case EOpIndexDirect:
751 case EOpIndexIndirect:
752 if (mLeft->isArray())
753 {
754 mType.clearArrayness();
755 }
756 else if (mLeft->isMatrix())
757 {
758 setType(TType(mLeft->getBasicType(), mLeft->getPrecision(), resultQualifier,
759 static_cast<unsigned char>(mLeft->getRows())));
760 }
761 else if (mLeft->isVector())
762 {
763 setType(TType(mLeft->getBasicType(), mLeft->getPrecision(), resultQualifier));
764 }
765 else
766 {
767 UNREACHABLE();
768 }
769 return;
770 case EOpIndexDirectStruct:
771 {
772 const TFieldList &fields = mLeft->getType().getStruct()->fields();
773 const int i = mRight->getAsConstantUnion()->getIConst(0);
774 setType(*fields[i]->type());
775 getTypePointer()->setQualifier(resultQualifier);
776 return;
777 }
778 case EOpIndexDirectInterfaceBlock:
779 {
780 const TFieldList &fields = mLeft->getType().getInterfaceBlock()->fields();
781 const int i = mRight->getAsConstantUnion()->getIConst(0);
782 setType(*fields[i]->type());
783 getTypePointer()->setQualifier(resultQualifier);
784 return;
785 }
786 case EOpVectorSwizzle:
787 {
788 auto numFields = mRight->getAsAggregate()->getSequence()->size();
789 setType(TType(mLeft->getBasicType(), mLeft->getPrecision(), resultQualifier,
790 static_cast<unsigned char>(numFields)));
791 return;
792 }
793 default:
794 break;
795 }
796
797 ASSERT(mLeft->isArray() == mRight->isArray());
798
799 // The result gets promoted to the highest precision.
800 TPrecision higherPrecision = GetHigherPrecision(mLeft->getPrecision(), mRight->getPrecision());
801 getTypePointer()->setPrecision(higherPrecision);
802
Jamie Madillb1a85f42014-08-19 15:23:24 -0400803 const int nominalSize =
804 std::max(mLeft->getNominalSize(), mRight->getNominalSize());
805
806 //
807 // All scalars or structs. Code after this test assumes this case is removed!
808 //
809 if (nominalSize == 1)
810 {
811 switch (mOp)
812 {
813 //
814 // Promote to conditional
815 //
816 case EOpEqual:
817 case EOpNotEqual:
818 case EOpLessThan:
819 case EOpGreaterThan:
820 case EOpLessThanEqual:
821 case EOpGreaterThanEqual:
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300822 setType(TType(EbtBool, EbpUndefined, resultQualifier));
823 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400824
825 //
826 // And and Or operate on conditionals
827 //
828 case EOpLogicalAnd:
Olli Etuaho47fd36a2015-03-19 14:22:24 +0200829 case EOpLogicalXor:
Jamie Madillb1a85f42014-08-19 15:23:24 -0400830 case EOpLogicalOr:
Olli Etuaho47fd36a2015-03-19 14:22:24 +0200831 ASSERT(mLeft->getBasicType() == EbtBool && mRight->getBasicType() == EbtBool);
Olli Etuahoc9550582016-08-29 17:56:22 +0300832 setType(TType(EbtBool, EbpUndefined, resultQualifier));
Jamie Madillb1a85f42014-08-19 15:23:24 -0400833 break;
834
835 default:
836 break;
837 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300838 return;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400839 }
840
841 // If we reach here, at least one of the operands is vector or matrix.
842 // The other operand could be a scalar, vector, or matrix.
Jamie Madillb1a85f42014-08-19 15:23:24 -0400843 TBasicType basicType = mLeft->getBasicType();
Olli Etuaho1dded802016-08-18 18:13:13 +0300844
Jamie Madillb1a85f42014-08-19 15:23:24 -0400845 switch (mOp)
846 {
Olli Etuaho1dded802016-08-18 18:13:13 +0300847 case EOpMul:
848 break;
849 case EOpMatrixTimesScalar:
850 if (mRight->isMatrix())
Jamie Madillb1a85f42014-08-19 15:23:24 -0400851 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200852 setType(TType(basicType, higherPrecision, resultQualifier,
853 static_cast<unsigned char>(mRight->getCols()),
854 static_cast<unsigned char>(mRight->getRows())));
Jamie Madillb1a85f42014-08-19 15:23:24 -0400855 }
Olli Etuaho1dded802016-08-18 18:13:13 +0300856 break;
857 case EOpMatrixTimesVector:
858 setType(TType(basicType, higherPrecision, resultQualifier,
859 static_cast<unsigned char>(mLeft->getRows()), 1));
860 break;
861 case EOpMatrixTimesMatrix:
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200862 setType(TType(basicType, higherPrecision, resultQualifier,
863 static_cast<unsigned char>(mRight->getCols()),
864 static_cast<unsigned char>(mLeft->getRows())));
Olli Etuaho1dded802016-08-18 18:13:13 +0300865 break;
866 case EOpVectorTimesScalar:
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200867 setType(TType(basicType, higherPrecision, resultQualifier,
Olli Etuaho1dded802016-08-18 18:13:13 +0300868 static_cast<unsigned char>(nominalSize), 1));
869 break;
870 case EOpVectorTimesMatrix:
871 setType(TType(basicType, higherPrecision, resultQualifier,
872 static_cast<unsigned char>(mRight->getCols()), 1));
873 break;
874 case EOpMulAssign:
875 case EOpVectorTimesScalarAssign:
876 case EOpVectorTimesMatrixAssign:
877 case EOpMatrixTimesScalarAssign:
878 case EOpMatrixTimesMatrixAssign:
879 ASSERT(mOp == GetMulAssignOpBasedOnOperands(mLeft->getType(), mRight->getType()));
880 break;
881 case EOpAssign:
882 case EOpInitialize:
Olli Etuaho1dded802016-08-18 18:13:13 +0300883 ASSERT((mLeft->getNominalSize() == mRight->getNominalSize()) &&
884 (mLeft->getSecondarySize() == mRight->getSecondarySize()));
885 break;
886 case EOpAdd:
887 case EOpSub:
888 case EOpDiv:
889 case EOpIMod:
890 case EOpBitShiftLeft:
891 case EOpBitShiftRight:
892 case EOpBitwiseAnd:
893 case EOpBitwiseXor:
894 case EOpBitwiseOr:
895 case EOpAddAssign:
896 case EOpSubAssign:
897 case EOpDivAssign:
898 case EOpIModAssign:
899 case EOpBitShiftLeftAssign:
900 case EOpBitShiftRightAssign:
901 case EOpBitwiseAndAssign:
902 case EOpBitwiseXorAssign:
903 case EOpBitwiseOrAssign:
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300904 {
905 const int secondarySize =
906 std::max(mLeft->getSecondarySize(), mRight->getSecondarySize());
907 setType(TType(basicType, higherPrecision, resultQualifier,
908 static_cast<unsigned char>(nominalSize),
909 static_cast<unsigned char>(secondarySize)));
910 ASSERT(!mLeft->isArray() && !mRight->isArray());
Olli Etuaho1dded802016-08-18 18:13:13 +0300911 break;
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300912 }
Olli Etuaho1dded802016-08-18 18:13:13 +0300913 case EOpEqual:
914 case EOpNotEqual:
915 case EOpLessThan:
916 case EOpGreaterThan:
917 case EOpLessThanEqual:
918 case EOpGreaterThanEqual:
919 ASSERT((mLeft->getNominalSize() == mRight->getNominalSize()) &&
920 (mLeft->getSecondarySize() == mRight->getSecondarySize()));
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300921 setType(TType(EbtBool, EbpUndefined, resultQualifier));
Olli Etuaho1dded802016-08-18 18:13:13 +0300922 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400923
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300924 case EOpIndexDirect:
925 case EOpIndexIndirect:
926 case EOpIndexDirectInterfaceBlock:
927 case EOpIndexDirectStruct:
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300928 case EOpVectorSwizzle:
929 // These ops should be already fully handled.
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300930 UNREACHABLE();
931 break;
Olli Etuaho1dded802016-08-18 18:13:13 +0300932 default:
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300933 UNREACHABLE();
934 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400935 }
Jamie Madillb1a85f42014-08-19 15:23:24 -0400936}
937
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300938const TConstantUnion *TIntermConstantUnion::foldIndexing(int index)
Olli Etuaho2c4b7462015-06-08 11:30:31 +0300939{
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300940 if (isArray())
Olli Etuaho2c4b7462015-06-08 11:30:31 +0300941 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300942 ASSERT(index < static_cast<int>(getType().getArraySize()));
943 TType arrayElementType = getType();
944 arrayElementType.clearArrayness();
945 size_t arrayElementSize = arrayElementType.getObjectSize();
946 return &mUnionArrayPointer[arrayElementSize * index];
947 }
948 else if (isMatrix())
949 {
950 ASSERT(index < getType().getCols());
951 int size = getType().getRows();
952 return &mUnionArrayPointer[size * index];
953 }
954 else if (isVector())
955 {
956 ASSERT(index < getType().getNominalSize());
957 return &mUnionArrayPointer[index];
958 }
959 else
960 {
961 UNREACHABLE();
Olli Etuaho2c4b7462015-06-08 11:30:31 +0300962 return nullptr;
963 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300964}
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200965
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300966TIntermTyped *TIntermBinary::fold(TDiagnostics *diagnostics)
967{
968 TIntermConstantUnion *leftConstant = mLeft->getAsConstantUnion();
969 TIntermConstantUnion *rightConstant = mRight->getAsConstantUnion();
970 switch (mOp)
971 {
972 case EOpIndexDirect:
973 {
974 if (leftConstant == nullptr || rightConstant == nullptr)
975 {
976 return nullptr;
977 }
978 int index = rightConstant->getIConst(0);
979
980 const TConstantUnion *constArray = leftConstant->foldIndexing(index);
981 return CreateFoldedNode(constArray, this, mType.getQualifier());
982 }
983 case EOpIndexDirectStruct:
984 {
985 if (leftConstant == nullptr || rightConstant == nullptr)
986 {
987 return nullptr;
988 }
989 const TFieldList &fields = mLeft->getType().getStruct()->fields();
990 size_t index = static_cast<size_t>(rightConstant->getIConst(0));
991
992 size_t previousFieldsSize = 0;
993 for (size_t i = 0; i < index; ++i)
994 {
995 previousFieldsSize += fields[i]->type()->getObjectSize();
996 }
997
998 const TConstantUnion *constArray = leftConstant->getUnionArrayPointer();
999 return CreateFoldedNode(constArray + previousFieldsSize, this, mType.getQualifier());
1000 }
1001 case EOpIndexIndirect:
1002 case EOpIndexDirectInterfaceBlock:
1003 // Can never be constant folded.
1004 return nullptr;
1005 case EOpVectorSwizzle:
1006 {
1007 if (leftConstant == nullptr)
1008 {
1009 return nullptr;
1010 }
1011 TIntermAggregate *fieldsAgg = mRight->getAsAggregate();
1012 TIntermSequence *fieldsSequence = fieldsAgg->getSequence();
1013 size_t numFields = fieldsSequence->size();
1014
1015 TConstantUnion *constArray = new TConstantUnion[numFields];
1016 for (size_t i = 0; i < numFields; i++)
1017 {
1018 int fieldOffset = fieldsSequence->at(i)->getAsConstantUnion()->getIConst(0);
1019 constArray[i] = *leftConstant->foldIndexing(fieldOffset);
1020 }
1021 return CreateFoldedNode(constArray, this, mType.getQualifier());
1022 }
1023 default:
1024 {
1025 if (leftConstant == nullptr || rightConstant == nullptr)
1026 {
1027 return nullptr;
1028 }
1029 TConstantUnion *constArray = leftConstant->foldBinary(mOp, rightConstant, diagnostics);
1030
1031 // Nodes may be constant folded without being qualified as constant.
1032 return CreateFoldedNode(constArray, this, mType.getQualifier());
1033 }
1034 }
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001035}
1036
Olli Etuahof119a262016-08-19 15:54:22 +03001037TIntermTyped *TIntermUnary::fold(TDiagnostics *diagnostics)
Olli Etuaho95310b02015-06-02 17:43:38 +03001038{
1039 TIntermConstantUnion *operandConstant = mOperand->getAsConstantUnion();
1040 if (operandConstant == nullptr)
1041 {
1042 return nullptr;
1043 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301044
1045 TConstantUnion *constArray = nullptr;
1046 switch (mOp)
1047 {
1048 case EOpAny:
1049 case EOpAll:
1050 case EOpLength:
1051 case EOpTranspose:
1052 case EOpDeterminant:
1053 case EOpInverse:
1054 case EOpPackSnorm2x16:
1055 case EOpUnpackSnorm2x16:
1056 case EOpPackUnorm2x16:
1057 case EOpUnpackUnorm2x16:
1058 case EOpPackHalf2x16:
1059 case EOpUnpackHalf2x16:
Olli Etuahof119a262016-08-19 15:54:22 +03001060 constArray = operandConstant->foldUnaryNonComponentWise(mOp);
1061 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301062 default:
Olli Etuahof119a262016-08-19 15:54:22 +03001063 constArray = operandConstant->foldUnaryComponentWise(mOp, diagnostics);
1064 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301065 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001066
1067 // Nodes may be constant folded without being qualified as constant.
Olli Etuahoc9550582016-08-29 17:56:22 +03001068 return CreateFoldedNode(constArray, this, mType.getQualifier());
Olli Etuahob43846e2015-06-02 18:18:57 +03001069}
1070
Olli Etuahof119a262016-08-19 15:54:22 +03001071TIntermTyped *TIntermAggregate::fold(TDiagnostics *diagnostics)
Olli Etuahob43846e2015-06-02 18:18:57 +03001072{
1073 // Make sure that all params are constant before actual constant folding.
1074 for (auto *param : *getSequence())
Olli Etuaho95310b02015-06-02 17:43:38 +03001075 {
Olli Etuahob43846e2015-06-02 18:18:57 +03001076 if (param->getAsConstantUnion() == nullptr)
1077 {
1078 return nullptr;
1079 }
Olli Etuaho95310b02015-06-02 17:43:38 +03001080 }
Olli Etuaho1d122782015-11-06 15:35:17 +02001081 TConstantUnion *constArray = nullptr;
1082 if (isConstructor())
Olli Etuahof119a262016-08-19 15:54:22 +03001083 constArray = TIntermConstantUnion::FoldAggregateConstructor(this);
Olli Etuaho1d122782015-11-06 15:35:17 +02001084 else
Olli Etuahof119a262016-08-19 15:54:22 +03001085 constArray = TIntermConstantUnion::FoldAggregateBuiltIn(this, diagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001086
1087 // Nodes may be constant folded without being qualified as constant.
1088 TQualifier resultQualifier = areChildrenConstQualified() ? EvqConst : EvqTemporary;
1089 return CreateFoldedNode(constArray, this, resultQualifier);
Olli Etuaho95310b02015-06-02 17:43:38 +03001090}
1091
Jamie Madillb1a85f42014-08-19 15:23:24 -04001092//
1093// The fold functions see if an operation on a constant can be done in place,
1094// without generating run-time code.
1095//
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001096// Returns the constant value to keep using or nullptr.
Jamie Madillb1a85f42014-08-19 15:23:24 -04001097//
Olli Etuaho3fdec912016-08-18 15:08:06 +03001098TConstantUnion *TIntermConstantUnion::foldBinary(TOperator op,
1099 TIntermConstantUnion *rightNode,
1100 TDiagnostics *diagnostics)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001101{
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001102 const TConstantUnion *leftArray = getUnionArrayPointer();
1103 const TConstantUnion *rightArray = rightNode->getUnionArrayPointer();
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001104
Olli Etuahof119a262016-08-19 15:54:22 +03001105 ASSERT(leftArray && rightArray);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001106
1107 size_t objectSize = getType().getObjectSize();
1108
1109 // for a case like float f = vec4(2, 3, 4, 5) + 1.2;
1110 if (rightNode->getType().getObjectSize() == 1 && objectSize > 1)
1111 {
1112 rightArray = Vectorize(*rightNode->getUnionArrayPointer(), objectSize);
1113 }
1114 else if (rightNode->getType().getObjectSize() > 1 && objectSize == 1)
1115 {
1116 // for a case like float f = 1.2 + vec4(2, 3, 4, 5);
1117 leftArray = Vectorize(*getUnionArrayPointer(), rightNode->getType().getObjectSize());
1118 objectSize = rightNode->getType().getObjectSize();
1119 }
1120
1121 TConstantUnion *resultArray = nullptr;
1122
1123 switch(op)
1124 {
1125 case EOpAdd:
1126 resultArray = new TConstantUnion[objectSize];
1127 for (size_t i = 0; i < objectSize; i++)
Jamie Madill47cb73a2016-09-09 11:41:44 -04001128 resultArray[i] = TConstantUnion::add(leftArray[i], rightArray[i], diagnostics);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001129 break;
1130 case EOpSub:
1131 resultArray = new TConstantUnion[objectSize];
1132 for (size_t i = 0; i < objectSize; i++)
Jamie Madill47cb73a2016-09-09 11:41:44 -04001133 resultArray[i] = TConstantUnion::sub(leftArray[i], rightArray[i], diagnostics);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001134 break;
1135
1136 case EOpMul:
1137 case EOpVectorTimesScalar:
1138 case EOpMatrixTimesScalar:
1139 resultArray = new TConstantUnion[objectSize];
1140 for (size_t i = 0; i < objectSize; i++)
Jamie Madill47cb73a2016-09-09 11:41:44 -04001141 resultArray[i] = TConstantUnion::mul(leftArray[i], rightArray[i], diagnostics);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001142 break;
1143
1144 case EOpMatrixTimesMatrix:
1145 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001146 ASSERT(getType().getBasicType() == EbtFloat && rightNode->getBasicType() == EbtFloat);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001147
1148 const int leftCols = getCols();
1149 const int leftRows = getRows();
1150 const int rightCols = rightNode->getType().getCols();
1151 const int rightRows = rightNode->getType().getRows();
1152 const int resultCols = rightCols;
1153 const int resultRows = leftRows;
1154
1155 resultArray = new TConstantUnion[resultCols * resultRows];
1156 for (int row = 0; row < resultRows; row++)
1157 {
1158 for (int column = 0; column < resultCols; column++)
1159 {
1160 resultArray[resultRows * column + row].setFConst(0.0f);
1161 for (int i = 0; i < leftCols; i++)
1162 {
1163 resultArray[resultRows * column + row].setFConst(
1164 resultArray[resultRows * column + row].getFConst() +
1165 leftArray[i * leftRows + row].getFConst() *
1166 rightArray[column * rightRows + i].getFConst());
1167 }
1168 }
1169 }
1170 }
1171 break;
1172
1173 case EOpDiv:
1174 case EOpIMod:
1175 {
1176 resultArray = new TConstantUnion[objectSize];
1177 for (size_t i = 0; i < objectSize; i++)
1178 {
1179 switch (getType().getBasicType())
1180 {
1181 case EbtFloat:
1182 if (rightArray[i] == 0.0f)
1183 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001184 diagnostics->warning(
1185 getLine(), "Divide by zero error during constant folding", "/", "");
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001186 resultArray[i].setFConst(leftArray[i].getFConst() < 0 ? -FLT_MAX : FLT_MAX);
1187 }
1188 else
1189 {
1190 ASSERT(op == EOpDiv);
1191 resultArray[i].setFConst(leftArray[i].getFConst() / rightArray[i].getFConst());
1192 }
1193 break;
1194
1195 case EbtInt:
1196 if (rightArray[i] == 0)
1197 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001198 diagnostics->warning(
1199 getLine(), "Divide by zero error during constant folding", "/", "");
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001200 resultArray[i].setIConst(INT_MAX);
1201 }
1202 else
1203 {
1204 if (op == EOpDiv)
1205 {
1206 resultArray[i].setIConst(leftArray[i].getIConst() / rightArray[i].getIConst());
1207 }
1208 else
1209 {
1210 ASSERT(op == EOpIMod);
1211 resultArray[i].setIConst(leftArray[i].getIConst() % rightArray[i].getIConst());
1212 }
1213 }
1214 break;
1215
1216 case EbtUInt:
1217 if (rightArray[i] == 0)
1218 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001219 diagnostics->warning(
1220 getLine(), "Divide by zero error during constant folding", "/", "");
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001221 resultArray[i].setUConst(UINT_MAX);
1222 }
1223 else
1224 {
1225 if (op == EOpDiv)
1226 {
1227 resultArray[i].setUConst(leftArray[i].getUConst() / rightArray[i].getUConst());
1228 }
1229 else
1230 {
1231 ASSERT(op == EOpIMod);
1232 resultArray[i].setUConst(leftArray[i].getUConst() % rightArray[i].getUConst());
1233 }
1234 }
1235 break;
1236
1237 default:
Olli Etuaho3fdec912016-08-18 15:08:06 +03001238 UNREACHABLE();
1239 return nullptr;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001240 }
1241 }
1242 }
1243 break;
1244
1245 case EOpMatrixTimesVector:
1246 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001247 ASSERT(rightNode->getBasicType() == EbtFloat);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001248
1249 const int matrixCols = getCols();
1250 const int matrixRows = getRows();
1251
1252 resultArray = new TConstantUnion[matrixRows];
1253
1254 for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++)
1255 {
1256 resultArray[matrixRow].setFConst(0.0f);
1257 for (int col = 0; col < matrixCols; col++)
1258 {
1259 resultArray[matrixRow].setFConst(resultArray[matrixRow].getFConst() +
1260 leftArray[col * matrixRows + matrixRow].getFConst() *
1261 rightArray[col].getFConst());
1262 }
1263 }
1264 }
1265 break;
1266
1267 case EOpVectorTimesMatrix:
1268 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001269 ASSERT(getType().getBasicType() == EbtFloat);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001270
1271 const int matrixCols = rightNode->getType().getCols();
1272 const int matrixRows = rightNode->getType().getRows();
1273
1274 resultArray = new TConstantUnion[matrixCols];
1275
1276 for (int matrixCol = 0; matrixCol < matrixCols; matrixCol++)
1277 {
1278 resultArray[matrixCol].setFConst(0.0f);
1279 for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++)
1280 {
1281 resultArray[matrixCol].setFConst(resultArray[matrixCol].getFConst() +
1282 leftArray[matrixRow].getFConst() *
1283 rightArray[matrixCol * matrixRows + matrixRow].getFConst());
1284 }
1285 }
1286 }
1287 break;
1288
1289 case EOpLogicalAnd:
1290 {
1291 resultArray = new TConstantUnion[objectSize];
1292 for (size_t i = 0; i < objectSize; i++)
1293 {
1294 resultArray[i] = leftArray[i] && rightArray[i];
1295 }
1296 }
1297 break;
1298
1299 case EOpLogicalOr:
1300 {
1301 resultArray = new TConstantUnion[objectSize];
1302 for (size_t i = 0; i < objectSize; i++)
1303 {
1304 resultArray[i] = leftArray[i] || rightArray[i];
1305 }
1306 }
1307 break;
1308
1309 case EOpLogicalXor:
1310 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001311 ASSERT(getType().getBasicType() == EbtBool);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001312 resultArray = new TConstantUnion[objectSize];
1313 for (size_t i = 0; i < objectSize; i++)
1314 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001315 resultArray[i].setBConst(leftArray[i] != rightArray[i]);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001316 }
1317 }
1318 break;
1319
1320 case EOpBitwiseAnd:
1321 resultArray = new TConstantUnion[objectSize];
1322 for (size_t i = 0; i < objectSize; i++)
1323 resultArray[i] = leftArray[i] & rightArray[i];
1324 break;
1325 case EOpBitwiseXor:
1326 resultArray = new TConstantUnion[objectSize];
1327 for (size_t i = 0; i < objectSize; i++)
1328 resultArray[i] = leftArray[i] ^ rightArray[i];
1329 break;
1330 case EOpBitwiseOr:
1331 resultArray = new TConstantUnion[objectSize];
1332 for (size_t i = 0; i < objectSize; i++)
1333 resultArray[i] = leftArray[i] | rightArray[i];
1334 break;
1335 case EOpBitShiftLeft:
1336 resultArray = new TConstantUnion[objectSize];
1337 for (size_t i = 0; i < objectSize; i++)
1338 resultArray[i] = leftArray[i] << rightArray[i];
1339 break;
1340 case EOpBitShiftRight:
1341 resultArray = new TConstantUnion[objectSize];
1342 for (size_t i = 0; i < objectSize; i++)
1343 resultArray[i] = leftArray[i] >> rightArray[i];
1344 break;
1345
1346 case EOpLessThan:
1347 ASSERT(objectSize == 1);
1348 resultArray = new TConstantUnion[1];
1349 resultArray->setBConst(*leftArray < *rightArray);
1350 break;
1351
1352 case EOpGreaterThan:
1353 ASSERT(objectSize == 1);
1354 resultArray = new TConstantUnion[1];
1355 resultArray->setBConst(*leftArray > *rightArray);
1356 break;
1357
1358 case EOpLessThanEqual:
1359 ASSERT(objectSize == 1);
1360 resultArray = new TConstantUnion[1];
1361 resultArray->setBConst(!(*leftArray > *rightArray));
1362 break;
1363
1364 case EOpGreaterThanEqual:
1365 ASSERT(objectSize == 1);
1366 resultArray = new TConstantUnion[1];
1367 resultArray->setBConst(!(*leftArray < *rightArray));
1368 break;
1369
1370 case EOpEqual:
1371 case EOpNotEqual:
1372 {
1373 resultArray = new TConstantUnion[1];
1374 bool equal = true;
Olli Etuaho40d9edf2015-11-12 17:30:34 +02001375 for (size_t i = 0; i < objectSize; i++)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001376 {
Olli Etuaho40d9edf2015-11-12 17:30:34 +02001377 if (leftArray[i] != rightArray[i])
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001378 {
Olli Etuaho40d9edf2015-11-12 17:30:34 +02001379 equal = false;
1380 break; // break out of for loop
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001381 }
1382 }
1383 if (op == EOpEqual)
1384 {
1385 resultArray->setBConst(equal);
1386 }
1387 else
1388 {
1389 resultArray->setBConst(!equal);
1390 }
1391 }
1392 break;
1393
1394 default:
Olli Etuaho3fdec912016-08-18 15:08:06 +03001395 UNREACHABLE();
1396 return nullptr;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001397 }
1398 return resultArray;
1399}
1400
Olli Etuahof119a262016-08-19 15:54:22 +03001401// The fold functions do operations on a constant at GLSL compile time, without generating run-time
1402// code. Returns the constant value to keep using. Nullptr should not be returned.
1403TConstantUnion *TIntermConstantUnion::foldUnaryNonComponentWise(TOperator op)
Jamie Madillb1a85f42014-08-19 15:23:24 -04001404{
Olli Etuahof119a262016-08-19 15:54:22 +03001405 // Do operations where the return type may have a different number of components compared to the
1406 // operand type.
Jamie Madillb1a85f42014-08-19 15:23:24 -04001407
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001408 const TConstantUnion *operandArray = getUnionArrayPointer();
Olli Etuahof119a262016-08-19 15:54:22 +03001409 ASSERT(operandArray);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301410
1411 size_t objectSize = getType().getObjectSize();
1412 TConstantUnion *resultArray = nullptr;
1413 switch (op)
1414 {
Olli Etuahof119a262016-08-19 15:54:22 +03001415 case EOpAny:
1416 ASSERT(getType().getBasicType() == EbtBool);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301417 resultArray = new TConstantUnion();
1418 resultArray->setBConst(false);
1419 for (size_t i = 0; i < objectSize; i++)
1420 {
1421 if (operandArray[i].getBConst())
1422 {
1423 resultArray->setBConst(true);
1424 break;
1425 }
1426 }
1427 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301428
Olli Etuahof119a262016-08-19 15:54:22 +03001429 case EOpAll:
1430 ASSERT(getType().getBasicType() == EbtBool);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301431 resultArray = new TConstantUnion();
1432 resultArray->setBConst(true);
1433 for (size_t i = 0; i < objectSize; i++)
1434 {
1435 if (!operandArray[i].getBConst())
1436 {
1437 resultArray->setBConst(false);
1438 break;
1439 }
1440 }
1441 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301442
Olli Etuahof119a262016-08-19 15:54:22 +03001443 case EOpLength:
1444 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301445 resultArray = new TConstantUnion();
1446 resultArray->setFConst(VectorLength(operandArray, objectSize));
1447 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301448
Olli Etuahof119a262016-08-19 15:54:22 +03001449 case EOpTranspose:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301450 {
Olli Etuahof119a262016-08-19 15:54:22 +03001451 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301452 resultArray = new TConstantUnion[objectSize];
1453 angle::Matrix<float> result =
Olli Etuahod5da5052016-08-29 13:16:55 +03001454 GetMatrix(operandArray, getType().getRows(), getType().getCols()).transpose();
Arun Patoleab2b9a22015-07-06 18:27:56 +05301455 SetUnionArrayFromMatrix(result, resultArray);
1456 break;
1457 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301458
Olli Etuahof119a262016-08-19 15:54:22 +03001459 case EOpDeterminant:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301460 {
Olli Etuahof119a262016-08-19 15:54:22 +03001461 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301462 unsigned int size = getType().getNominalSize();
1463 ASSERT(size >= 2 && size <= 4);
1464 resultArray = new TConstantUnion();
1465 resultArray->setFConst(GetMatrix(operandArray, size).determinant());
1466 break;
1467 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301468
Olli Etuahof119a262016-08-19 15:54:22 +03001469 case EOpInverse:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301470 {
Olli Etuahof119a262016-08-19 15:54:22 +03001471 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301472 unsigned int size = getType().getNominalSize();
1473 ASSERT(size >= 2 && size <= 4);
Olli Etuahof119a262016-08-19 15:54:22 +03001474 resultArray = new TConstantUnion[objectSize];
Arun Patoleab2b9a22015-07-06 18:27:56 +05301475 angle::Matrix<float> result = GetMatrix(operandArray, size).inverse();
1476 SetUnionArrayFromMatrix(result, resultArray);
1477 break;
1478 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301479
Olli Etuahof119a262016-08-19 15:54:22 +03001480 case EOpPackSnorm2x16:
1481 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301482 ASSERT(getType().getNominalSize() == 2);
1483 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03001484 resultArray->setUConst(
1485 gl::packSnorm2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
Arun Patoleab2b9a22015-07-06 18:27:56 +05301486 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301487
Olli Etuahof119a262016-08-19 15:54:22 +03001488 case EOpUnpackSnorm2x16:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301489 {
Olli Etuahof119a262016-08-19 15:54:22 +03001490 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301491 resultArray = new TConstantUnion[2];
1492 float f1, f2;
1493 gl::unpackSnorm2x16(operandArray[0].getUConst(), &f1, &f2);
1494 resultArray[0].setFConst(f1);
1495 resultArray[1].setFConst(f2);
1496 break;
1497 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301498
Olli Etuahof119a262016-08-19 15:54:22 +03001499 case EOpPackUnorm2x16:
1500 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301501 ASSERT(getType().getNominalSize() == 2);
1502 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03001503 resultArray->setUConst(
1504 gl::packUnorm2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
Arun Patoleab2b9a22015-07-06 18:27:56 +05301505 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301506
Olli Etuahof119a262016-08-19 15:54:22 +03001507 case EOpUnpackUnorm2x16:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301508 {
Olli Etuahof119a262016-08-19 15:54:22 +03001509 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301510 resultArray = new TConstantUnion[2];
1511 float f1, f2;
1512 gl::unpackUnorm2x16(operandArray[0].getUConst(), &f1, &f2);
1513 resultArray[0].setFConst(f1);
1514 resultArray[1].setFConst(f2);
1515 break;
1516 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301517
Olli Etuahof119a262016-08-19 15:54:22 +03001518 case EOpPackHalf2x16:
1519 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301520 ASSERT(getType().getNominalSize() == 2);
1521 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03001522 resultArray->setUConst(
1523 gl::packHalf2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
Arun Patoleab2b9a22015-07-06 18:27:56 +05301524 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301525
Olli Etuahof119a262016-08-19 15:54:22 +03001526 case EOpUnpackHalf2x16:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301527 {
Olli Etuahof119a262016-08-19 15:54:22 +03001528 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301529 resultArray = new TConstantUnion[2];
1530 float f1, f2;
1531 gl::unpackHalf2x16(operandArray[0].getUConst(), &f1, &f2);
1532 resultArray[0].setFConst(f1);
1533 resultArray[1].setFConst(f2);
1534 break;
1535 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301536
Olli Etuahof119a262016-08-19 15:54:22 +03001537 default:
1538 UNREACHABLE();
1539 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301540 }
1541
1542 return resultArray;
1543}
1544
Olli Etuahof119a262016-08-19 15:54:22 +03001545TConstantUnion *TIntermConstantUnion::foldUnaryComponentWise(TOperator op,
1546 TDiagnostics *diagnostics)
Arun Patoleab2b9a22015-07-06 18:27:56 +05301547{
Olli Etuahof119a262016-08-19 15:54:22 +03001548 // Do unary operations where each component of the result is computed based on the corresponding
1549 // component of the operand. Also folds normalize, though the divisor in that case takes all
1550 // components into account.
Arun Patoleab2b9a22015-07-06 18:27:56 +05301551
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001552 const TConstantUnion *operandArray = getUnionArrayPointer();
Olli Etuahof119a262016-08-19 15:54:22 +03001553 ASSERT(operandArray);
Jamie Madillb1a85f42014-08-19 15:23:24 -04001554
1555 size_t objectSize = getType().getObjectSize();
1556
Arun Patoleab2b9a22015-07-06 18:27:56 +05301557 TConstantUnion *resultArray = new TConstantUnion[objectSize];
1558 for (size_t i = 0; i < objectSize; i++)
Arun Patole9d0b1f92015-05-20 14:27:17 +05301559 {
Arun Patoleab2b9a22015-07-06 18:27:56 +05301560 switch(op)
Arun Patole9d0b1f92015-05-20 14:27:17 +05301561 {
Olli Etuahof119a262016-08-19 15:54:22 +03001562 case EOpNegative:
1563 switch (getType().getBasicType())
1564 {
1565 case EbtFloat:
1566 resultArray[i].setFConst(-operandArray[i].getFConst());
1567 break;
1568 case EbtInt:
1569 resultArray[i].setIConst(-operandArray[i].getIConst());
1570 break;
1571 case EbtUInt:
1572 resultArray[i].setUConst(static_cast<unsigned int>(
1573 -static_cast<int>(operandArray[i].getUConst())));
1574 break;
1575 default:
1576 UNREACHABLE();
1577 return nullptr;
1578 }
Arun Patole1155ddd2015-06-05 18:04:36 +05301579 break;
Arun Patolecdfa8f52015-06-30 17:48:25 +05301580
Olli Etuahof119a262016-08-19 15:54:22 +03001581 case EOpPositive:
1582 switch (getType().getBasicType())
1583 {
1584 case EbtFloat:
1585 resultArray[i].setFConst(operandArray[i].getFConst());
1586 break;
1587 case EbtInt:
1588 resultArray[i].setIConst(operandArray[i].getIConst());
1589 break;
1590 case EbtUInt:
1591 resultArray[i].setUConst(static_cast<unsigned int>(
1592 static_cast<int>(operandArray[i].getUConst())));
1593 break;
1594 default:
1595 UNREACHABLE();
1596 return nullptr;
1597 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301598 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301599
Olli Etuahof119a262016-08-19 15:54:22 +03001600 case EOpLogicalNot:
1601 switch (getType().getBasicType())
1602 {
1603 case EbtBool:
1604 resultArray[i].setBConst(!operandArray[i].getBConst());
1605 break;
1606 default:
1607 UNREACHABLE();
1608 return nullptr;
1609 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301610 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301611
Olli Etuahof119a262016-08-19 15:54:22 +03001612 case EOpBitwiseNot:
1613 switch (getType().getBasicType())
1614 {
1615 case EbtInt:
1616 resultArray[i].setIConst(~operandArray[i].getIConst());
1617 break;
1618 case EbtUInt:
1619 resultArray[i].setUConst(~operandArray[i].getUConst());
1620 break;
1621 default:
1622 UNREACHABLE();
1623 return nullptr;
1624 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301625 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301626
Olli Etuahof119a262016-08-19 15:54:22 +03001627 case EOpRadians:
1628 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301629 resultArray[i].setFConst(kDegreesToRadiansMultiplier * operandArray[i].getFConst());
1630 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301631
Olli Etuahof119a262016-08-19 15:54:22 +03001632 case EOpDegrees:
1633 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301634 resultArray[i].setFConst(kRadiansToDegreesMultiplier * operandArray[i].getFConst());
1635 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301636
Olli Etuahof119a262016-08-19 15:54:22 +03001637 case EOpSin:
1638 foldFloatTypeUnary(operandArray[i], &sinf, &resultArray[i]);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301639 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301640
Olli Etuahof119a262016-08-19 15:54:22 +03001641 case EOpCos:
1642 foldFloatTypeUnary(operandArray[i], &cosf, &resultArray[i]);
1643 break;
1644
1645 case EOpTan:
1646 foldFloatTypeUnary(operandArray[i], &tanf, &resultArray[i]);
1647 break;
1648
1649 case EOpAsin:
1650 // For asin(x), results are undefined if |x| > 1, we are choosing to set result to
1651 // 0.
1652 if (fabsf(operandArray[i].getFConst()) > 1.0f)
1653 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1654 diagnostics, &resultArray[i]);
1655 else
1656 foldFloatTypeUnary(operandArray[i], &asinf, &resultArray[i]);
1657 break;
1658
1659 case EOpAcos:
1660 // For acos(x), results are undefined if |x| > 1, we are choosing to set result to
1661 // 0.
1662 if (fabsf(operandArray[i].getFConst()) > 1.0f)
1663 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1664 diagnostics, &resultArray[i]);
1665 else
1666 foldFloatTypeUnary(operandArray[i], &acosf, &resultArray[i]);
1667 break;
1668
1669 case EOpAtan:
1670 foldFloatTypeUnary(operandArray[i], &atanf, &resultArray[i]);
1671 break;
1672
1673 case EOpSinh:
1674 foldFloatTypeUnary(operandArray[i], &sinhf, &resultArray[i]);
1675 break;
1676
1677 case EOpCosh:
1678 foldFloatTypeUnary(operandArray[i], &coshf, &resultArray[i]);
1679 break;
1680
1681 case EOpTanh:
1682 foldFloatTypeUnary(operandArray[i], &tanhf, &resultArray[i]);
1683 break;
1684
1685 case EOpAsinh:
1686 foldFloatTypeUnary(operandArray[i], &asinhf, &resultArray[i]);
1687 break;
1688
1689 case EOpAcosh:
1690 // For acosh(x), results are undefined if x < 1, we are choosing to set result to 0.
1691 if (operandArray[i].getFConst() < 1.0f)
1692 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1693 diagnostics, &resultArray[i]);
1694 else
1695 foldFloatTypeUnary(operandArray[i], &acoshf, &resultArray[i]);
1696 break;
1697
1698 case EOpAtanh:
1699 // For atanh(x), results are undefined if |x| >= 1, we are choosing to set result to
1700 // 0.
1701 if (fabsf(operandArray[i].getFConst()) >= 1.0f)
1702 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1703 diagnostics, &resultArray[i]);
1704 else
1705 foldFloatTypeUnary(operandArray[i], &atanhf, &resultArray[i]);
1706 break;
1707
1708 case EOpAbs:
1709 switch (getType().getBasicType())
Arun Patoleab2b9a22015-07-06 18:27:56 +05301710 {
Olli Etuahof119a262016-08-19 15:54:22 +03001711 case EbtFloat:
1712 resultArray[i].setFConst(fabsf(operandArray[i].getFConst()));
1713 break;
1714 case EbtInt:
1715 resultArray[i].setIConst(abs(operandArray[i].getIConst()));
1716 break;
1717 default:
1718 UNREACHABLE();
1719 return nullptr;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301720 }
1721 break;
Olli Etuahof119a262016-08-19 15:54:22 +03001722
1723 case EOpSign:
1724 switch (getType().getBasicType())
Arun Patoleab2b9a22015-07-06 18:27:56 +05301725 {
Olli Etuahof119a262016-08-19 15:54:22 +03001726 case EbtFloat:
1727 {
1728 float fConst = operandArray[i].getFConst();
1729 float fResult = 0.0f;
1730 if (fConst > 0.0f)
1731 fResult = 1.0f;
1732 else if (fConst < 0.0f)
1733 fResult = -1.0f;
1734 resultArray[i].setFConst(fResult);
1735 break;
1736 }
1737 case EbtInt:
1738 {
1739 int iConst = operandArray[i].getIConst();
1740 int iResult = 0;
1741 if (iConst > 0)
1742 iResult = 1;
1743 else if (iConst < 0)
1744 iResult = -1;
1745 resultArray[i].setIConst(iResult);
1746 break;
1747 }
1748 default:
1749 UNREACHABLE();
1750 return nullptr;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301751 }
1752 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301753
Olli Etuahof119a262016-08-19 15:54:22 +03001754 case EOpFloor:
1755 foldFloatTypeUnary(operandArray[i], &floorf, &resultArray[i]);
1756 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301757
Olli Etuahof119a262016-08-19 15:54:22 +03001758 case EOpTrunc:
1759 foldFloatTypeUnary(operandArray[i], &truncf, &resultArray[i]);
1760 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301761
Olli Etuahof119a262016-08-19 15:54:22 +03001762 case EOpRound:
1763 foldFloatTypeUnary(operandArray[i], &roundf, &resultArray[i]);
1764 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301765
Olli Etuahof119a262016-08-19 15:54:22 +03001766 case EOpRoundEven:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301767 {
Olli Etuahof119a262016-08-19 15:54:22 +03001768 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301769 float x = operandArray[i].getFConst();
1770 float result;
1771 float fractPart = modff(x, &result);
1772 if (fabsf(fractPart) == 0.5f)
1773 result = 2.0f * roundf(x / 2.0f);
1774 else
1775 result = roundf(x);
1776 resultArray[i].setFConst(result);
1777 break;
1778 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301779
Olli Etuahof119a262016-08-19 15:54:22 +03001780 case EOpCeil:
1781 foldFloatTypeUnary(operandArray[i], &ceilf, &resultArray[i]);
1782 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301783
Olli Etuahof119a262016-08-19 15:54:22 +03001784 case EOpFract:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301785 {
Olli Etuahof119a262016-08-19 15:54:22 +03001786 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301787 float x = operandArray[i].getFConst();
1788 resultArray[i].setFConst(x - floorf(x));
1789 break;
1790 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301791
Olli Etuahof119a262016-08-19 15:54:22 +03001792 case EOpIsNan:
1793 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05301794 resultArray[i].setBConst(gl::isNaN(operandArray[0].getFConst()));
1795 break;
Arun Patole551279e2015-07-07 18:18:23 +05301796
Olli Etuahof119a262016-08-19 15:54:22 +03001797 case EOpIsInf:
1798 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05301799 resultArray[i].setBConst(gl::isInf(operandArray[0].getFConst()));
1800 break;
Arun Patole551279e2015-07-07 18:18:23 +05301801
Olli Etuahof119a262016-08-19 15:54:22 +03001802 case EOpFloatBitsToInt:
1803 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05301804 resultArray[i].setIConst(gl::bitCast<int32_t>(operandArray[0].getFConst()));
1805 break;
Arun Patole551279e2015-07-07 18:18:23 +05301806
Olli Etuahof119a262016-08-19 15:54:22 +03001807 case EOpFloatBitsToUint:
1808 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05301809 resultArray[i].setUConst(gl::bitCast<uint32_t>(operandArray[0].getFConst()));
1810 break;
Arun Patole551279e2015-07-07 18:18:23 +05301811
Olli Etuahof119a262016-08-19 15:54:22 +03001812 case EOpIntBitsToFloat:
1813 ASSERT(getType().getBasicType() == EbtInt);
Arun Patole551279e2015-07-07 18:18:23 +05301814 resultArray[i].setFConst(gl::bitCast<float>(operandArray[0].getIConst()));
1815 break;
Arun Patole551279e2015-07-07 18:18:23 +05301816
Olli Etuahof119a262016-08-19 15:54:22 +03001817 case EOpUintBitsToFloat:
1818 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patole551279e2015-07-07 18:18:23 +05301819 resultArray[i].setFConst(gl::bitCast<float>(operandArray[0].getUConst()));
1820 break;
Arun Patole551279e2015-07-07 18:18:23 +05301821
Olli Etuahof119a262016-08-19 15:54:22 +03001822 case EOpExp:
1823 foldFloatTypeUnary(operandArray[i], &expf, &resultArray[i]);
1824 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301825
Olli Etuahof119a262016-08-19 15:54:22 +03001826 case EOpLog:
1827 // For log(x), results are undefined if x <= 0, we are choosing to set result to 0.
1828 if (operandArray[i].getFConst() <= 0.0f)
1829 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1830 diagnostics, &resultArray[i]);
1831 else
1832 foldFloatTypeUnary(operandArray[i], &logf, &resultArray[i]);
1833 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301834
Olli Etuahof119a262016-08-19 15:54:22 +03001835 case EOpExp2:
1836 foldFloatTypeUnary(operandArray[i], &exp2f, &resultArray[i]);
1837 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301838
Olli Etuahof119a262016-08-19 15:54:22 +03001839 case EOpLog2:
1840 // For log2(x), results are undefined if x <= 0, we are choosing to set result to 0.
1841 // And log2f is not available on some plarforms like old android, so just using
1842 // log(x)/log(2) here.
1843 if (operandArray[i].getFConst() <= 0.0f)
1844 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1845 diagnostics, &resultArray[i]);
1846 else
1847 {
1848 foldFloatTypeUnary(operandArray[i], &logf, &resultArray[i]);
1849 resultArray[i].setFConst(resultArray[i].getFConst() / logf(2.0f));
1850 }
1851 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301852
Olli Etuahof119a262016-08-19 15:54:22 +03001853 case EOpSqrt:
1854 // For sqrt(x), results are undefined if x < 0, we are choosing to set result to 0.
1855 if (operandArray[i].getFConst() < 0.0f)
1856 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1857 diagnostics, &resultArray[i]);
1858 else
1859 foldFloatTypeUnary(operandArray[i], &sqrtf, &resultArray[i]);
1860 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301861
Olli Etuahof119a262016-08-19 15:54:22 +03001862 case EOpInverseSqrt:
1863 // There is no stdlib built-in function equavalent for GLES built-in inversesqrt(),
1864 // so getting the square root first using builtin function sqrt() and then taking
1865 // its inverse.
1866 // Also, for inversesqrt(x), results are undefined if x <= 0, we are choosing to set
1867 // result to 0.
1868 if (operandArray[i].getFConst() <= 0.0f)
1869 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1870 diagnostics, &resultArray[i]);
1871 else
1872 {
1873 foldFloatTypeUnary(operandArray[i], &sqrtf, &resultArray[i]);
1874 resultArray[i].setFConst(1.0f / resultArray[i].getFConst());
1875 }
1876 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301877
Olli Etuahof119a262016-08-19 15:54:22 +03001878 case EOpVectorLogicalNot:
1879 ASSERT(getType().getBasicType() == EbtBool);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301880 resultArray[i].setBConst(!operandArray[i].getBConst());
1881 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301882
Olli Etuahof119a262016-08-19 15:54:22 +03001883 case EOpNormalize:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301884 {
Olli Etuahof119a262016-08-19 15:54:22 +03001885 ASSERT(getType().getBasicType() == EbtFloat);
1886 float x = operandArray[i].getFConst();
Arun Patoleab2b9a22015-07-06 18:27:56 +05301887 float length = VectorLength(operandArray, objectSize);
1888 if (length)
1889 resultArray[i].setFConst(x / length);
1890 else
Olli Etuahof119a262016-08-19 15:54:22 +03001891 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1892 diagnostics, &resultArray[i]);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301893 break;
1894 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301895
Olli Etuahof119a262016-08-19 15:54:22 +03001896 case EOpDFdx:
1897 case EOpDFdy:
1898 case EOpFwidth:
1899 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole0c5409f2015-07-08 15:17:53 +05301900 // Derivatives of constant arguments should be 0.
1901 resultArray[i].setFConst(0.0f);
1902 break;
Arun Patole0c5409f2015-07-08 15:17:53 +05301903
Olli Etuahof119a262016-08-19 15:54:22 +03001904 default:
1905 return nullptr;
Arun Patole9d0b1f92015-05-20 14:27:17 +05301906 }
Arun Patole9d0b1f92015-05-20 14:27:17 +05301907 }
Jamie Madillb1a85f42014-08-19 15:23:24 -04001908
Arun Patoleab2b9a22015-07-06 18:27:56 +05301909 return resultArray;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001910}
1911
Olli Etuahof119a262016-08-19 15:54:22 +03001912void TIntermConstantUnion::foldFloatTypeUnary(const TConstantUnion &parameter,
1913 FloatTypeUnaryFunc builtinFunc,
1914 TConstantUnion *result) const
Arun Patole9dea48f2015-04-02 11:45:09 +05301915{
1916 ASSERT(builtinFunc);
1917
Olli Etuahof119a262016-08-19 15:54:22 +03001918 ASSERT(getType().getBasicType() == EbtFloat);
1919 result->setFConst(builtinFunc(parameter.getFConst()));
Arun Patole9dea48f2015-04-02 11:45:09 +05301920}
1921
Jamie Madillb1a85f42014-08-19 15:23:24 -04001922// static
Olli Etuahof119a262016-08-19 15:54:22 +03001923TConstantUnion *TIntermConstantUnion::FoldAggregateConstructor(TIntermAggregate *aggregate)
Olli Etuaho1d122782015-11-06 15:35:17 +02001924{
1925 ASSERT(aggregate->getSequence()->size() > 0u);
1926 size_t resultSize = aggregate->getType().getObjectSize();
1927 TConstantUnion *resultArray = new TConstantUnion[resultSize];
1928 TBasicType basicType = aggregate->getBasicType();
1929
1930 size_t resultIndex = 0u;
1931
1932 if (aggregate->getSequence()->size() == 1u)
1933 {
1934 TIntermNode *argument = aggregate->getSequence()->front();
1935 TIntermConstantUnion *argumentConstant = argument->getAsConstantUnion();
1936 const TConstantUnion *argumentUnionArray = argumentConstant->getUnionArrayPointer();
1937 // Check the special case of constructing a matrix diagonal from a single scalar,
1938 // or a vector from a single scalar.
1939 if (argumentConstant->getType().getObjectSize() == 1u)
1940 {
1941 if (aggregate->isMatrix())
1942 {
1943 int resultCols = aggregate->getType().getCols();
1944 int resultRows = aggregate->getType().getRows();
1945 for (int col = 0; col < resultCols; ++col)
1946 {
1947 for (int row = 0; row < resultRows; ++row)
1948 {
1949 if (col == row)
1950 {
1951 resultArray[resultIndex].cast(basicType, argumentUnionArray[0]);
1952 }
1953 else
1954 {
1955 resultArray[resultIndex].setFConst(0.0f);
1956 }
1957 ++resultIndex;
1958 }
1959 }
1960 }
1961 else
1962 {
1963 while (resultIndex < resultSize)
1964 {
1965 resultArray[resultIndex].cast(basicType, argumentUnionArray[0]);
1966 ++resultIndex;
1967 }
1968 }
1969 ASSERT(resultIndex == resultSize);
1970 return resultArray;
1971 }
1972 else if (aggregate->isMatrix() && argumentConstant->isMatrix())
1973 {
1974 // The special case of constructing a matrix from a matrix.
1975 int argumentCols = argumentConstant->getType().getCols();
1976 int argumentRows = argumentConstant->getType().getRows();
1977 int resultCols = aggregate->getType().getCols();
1978 int resultRows = aggregate->getType().getRows();
1979 for (int col = 0; col < resultCols; ++col)
1980 {
1981 for (int row = 0; row < resultRows; ++row)
1982 {
1983 if (col < argumentCols && row < argumentRows)
1984 {
1985 resultArray[resultIndex].cast(basicType,
1986 argumentUnionArray[col * argumentRows + row]);
1987 }
1988 else if (col == row)
1989 {
1990 resultArray[resultIndex].setFConst(1.0f);
1991 }
1992 else
1993 {
1994 resultArray[resultIndex].setFConst(0.0f);
1995 }
1996 ++resultIndex;
1997 }
1998 }
1999 ASSERT(resultIndex == resultSize);
2000 return resultArray;
2001 }
2002 }
2003
2004 for (TIntermNode *&argument : *aggregate->getSequence())
2005 {
2006 TIntermConstantUnion *argumentConstant = argument->getAsConstantUnion();
2007 size_t argumentSize = argumentConstant->getType().getObjectSize();
2008 const TConstantUnion *argumentUnionArray = argumentConstant->getUnionArrayPointer();
2009 for (size_t i = 0u; i < argumentSize; ++i)
2010 {
2011 if (resultIndex >= resultSize)
2012 break;
2013 resultArray[resultIndex].cast(basicType, argumentUnionArray[i]);
2014 ++resultIndex;
2015 }
2016 }
2017 ASSERT(resultIndex == resultSize);
2018 return resultArray;
2019}
2020
2021// static
Olli Etuahof119a262016-08-19 15:54:22 +03002022TConstantUnion *TIntermConstantUnion::FoldAggregateBuiltIn(TIntermAggregate *aggregate,
2023 TDiagnostics *diagnostics)
Arun Patole274f0702015-05-05 13:33:30 +05302024{
Olli Etuahob43846e2015-06-02 18:18:57 +03002025 TOperator op = aggregate->getOp();
Arun Patole274f0702015-05-05 13:33:30 +05302026 TIntermSequence *sequence = aggregate->getSequence();
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002027 unsigned int paramsCount = static_cast<unsigned int>(sequence->size());
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002028 std::vector<const TConstantUnion *> unionArrays(paramsCount);
Arun Patole274f0702015-05-05 13:33:30 +05302029 std::vector<size_t> objectSizes(paramsCount);
Olli Etuahob43846e2015-06-02 18:18:57 +03002030 size_t maxObjectSize = 0;
Arun Patole274f0702015-05-05 13:33:30 +05302031 TBasicType basicType = EbtVoid;
2032 TSourceLoc loc;
2033 for (unsigned int i = 0; i < paramsCount; i++)
2034 {
2035 TIntermConstantUnion *paramConstant = (*sequence)[i]->getAsConstantUnion();
Olli Etuahob43846e2015-06-02 18:18:57 +03002036 ASSERT(paramConstant != nullptr); // Should be checked already.
Arun Patole274f0702015-05-05 13:33:30 +05302037
2038 if (i == 0)
2039 {
2040 basicType = paramConstant->getType().getBasicType();
2041 loc = paramConstant->getLine();
2042 }
2043 unionArrays[i] = paramConstant->getUnionArrayPointer();
2044 objectSizes[i] = paramConstant->getType().getObjectSize();
Olli Etuahob43846e2015-06-02 18:18:57 +03002045 if (objectSizes[i] > maxObjectSize)
2046 maxObjectSize = objectSizes[i];
Arun Patole274f0702015-05-05 13:33:30 +05302047 }
2048
Olli Etuahod5da5052016-08-29 13:16:55 +03002049 if (!(*sequence)[0]->getAsTyped()->isMatrix() && aggregate->getOp() != EOpOuterProduct)
Arun Patole7fa33552015-06-10 15:15:18 +05302050 {
2051 for (unsigned int i = 0; i < paramsCount; i++)
2052 if (objectSizes[i] != maxObjectSize)
2053 unionArrays[i] = Vectorize(*unionArrays[i], maxObjectSize);
2054 }
Arun Patole274f0702015-05-05 13:33:30 +05302055
Olli Etuahob43846e2015-06-02 18:18:57 +03002056 TConstantUnion *resultArray = nullptr;
Arun Patole274f0702015-05-05 13:33:30 +05302057 if (paramsCount == 2)
2058 {
2059 //
2060 // Binary built-in
2061 //
2062 switch (op)
2063 {
Olli Etuahof119a262016-08-19 15:54:22 +03002064 case EOpAtan:
Arun Patolebf790422015-05-18 17:53:04 +05302065 {
Olli Etuahof119a262016-08-19 15:54:22 +03002066 ASSERT(basicType == EbtFloat);
2067 resultArray = new TConstantUnion[maxObjectSize];
2068 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302069 {
Olli Etuahof119a262016-08-19 15:54:22 +03002070 float y = unionArrays[0][i].getFConst();
2071 float x = unionArrays[1][i].getFConst();
2072 // Results are undefined if x and y are both 0.
2073 if (x == 0.0f && y == 0.0f)
2074 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
2075 &resultArray[i]);
2076 else
2077 resultArray[i].setFConst(atan2f(y, x));
Arun Patolebf790422015-05-18 17:53:04 +05302078 }
Olli Etuahof119a262016-08-19 15:54:22 +03002079 break;
Arun Patolebf790422015-05-18 17:53:04 +05302080 }
Arun Patolebf790422015-05-18 17:53:04 +05302081
Olli Etuahof119a262016-08-19 15:54:22 +03002082 case EOpPow:
Arun Patolebf790422015-05-18 17:53:04 +05302083 {
Olli Etuahof119a262016-08-19 15:54:22 +03002084 ASSERT(basicType == EbtFloat);
2085 resultArray = new TConstantUnion[maxObjectSize];
2086 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302087 {
Olli Etuahof119a262016-08-19 15:54:22 +03002088 float x = unionArrays[0][i].getFConst();
2089 float y = unionArrays[1][i].getFConst();
2090 // Results are undefined if x < 0.
2091 // Results are undefined if x = 0 and y <= 0.
2092 if (x < 0.0f)
2093 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
2094 &resultArray[i]);
2095 else if (x == 0.0f && y <= 0.0f)
2096 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
2097 &resultArray[i]);
2098 else
2099 resultArray[i].setFConst(powf(x, y));
Arun Patolebf790422015-05-18 17:53:04 +05302100 }
Olli Etuahof119a262016-08-19 15:54:22 +03002101 break;
Arun Patolebf790422015-05-18 17:53:04 +05302102 }
Arun Patolebf790422015-05-18 17:53:04 +05302103
Olli Etuahof119a262016-08-19 15:54:22 +03002104 case EOpMod:
Arun Patolebf790422015-05-18 17:53:04 +05302105 {
Olli Etuahof119a262016-08-19 15:54:22 +03002106 ASSERT(basicType == EbtFloat);
2107 resultArray = new TConstantUnion[maxObjectSize];
2108 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302109 {
Olli Etuahof119a262016-08-19 15:54:22 +03002110 float x = unionArrays[0][i].getFConst();
2111 float y = unionArrays[1][i].getFConst();
2112 resultArray[i].setFConst(x - y * floorf(x / y));
Arun Patolebf790422015-05-18 17:53:04 +05302113 }
Olli Etuahof119a262016-08-19 15:54:22 +03002114 break;
Arun Patolebf790422015-05-18 17:53:04 +05302115 }
Arun Patolebf790422015-05-18 17:53:04 +05302116
Olli Etuahof119a262016-08-19 15:54:22 +03002117 case EOpMin:
Arun Patole274f0702015-05-05 13:33:30 +05302118 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002119 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole274f0702015-05-05 13:33:30 +05302120 for (size_t i = 0; i < maxObjectSize; i++)
2121 {
2122 switch (basicType)
2123 {
Olli Etuahof119a262016-08-19 15:54:22 +03002124 case EbtFloat:
2125 resultArray[i].setFConst(std::min(unionArrays[0][i].getFConst(),
2126 unionArrays[1][i].getFConst()));
2127 break;
2128 case EbtInt:
2129 resultArray[i].setIConst(std::min(unionArrays[0][i].getIConst(),
2130 unionArrays[1][i].getIConst()));
2131 break;
2132 case EbtUInt:
2133 resultArray[i].setUConst(std::min(unionArrays[0][i].getUConst(),
2134 unionArrays[1][i].getUConst()));
2135 break;
2136 default:
2137 UNREACHABLE();
2138 break;
Arun Patole274f0702015-05-05 13:33:30 +05302139 }
2140 }
Olli Etuahof119a262016-08-19 15:54:22 +03002141 break;
Arun Patole274f0702015-05-05 13:33:30 +05302142 }
Arun Patole274f0702015-05-05 13:33:30 +05302143
Olli Etuahof119a262016-08-19 15:54:22 +03002144 case EOpMax:
Arun Patole274f0702015-05-05 13:33:30 +05302145 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002146 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole274f0702015-05-05 13:33:30 +05302147 for (size_t i = 0; i < maxObjectSize; i++)
2148 {
2149 switch (basicType)
2150 {
Olli Etuahof119a262016-08-19 15:54:22 +03002151 case EbtFloat:
2152 resultArray[i].setFConst(std::max(unionArrays[0][i].getFConst(),
2153 unionArrays[1][i].getFConst()));
2154 break;
2155 case EbtInt:
2156 resultArray[i].setIConst(std::max(unionArrays[0][i].getIConst(),
2157 unionArrays[1][i].getIConst()));
2158 break;
2159 case EbtUInt:
2160 resultArray[i].setUConst(std::max(unionArrays[0][i].getUConst(),
2161 unionArrays[1][i].getUConst()));
2162 break;
2163 default:
2164 UNREACHABLE();
2165 break;
Arun Patole274f0702015-05-05 13:33:30 +05302166 }
2167 }
Olli Etuahof119a262016-08-19 15:54:22 +03002168 break;
Arun Patole274f0702015-05-05 13:33:30 +05302169 }
Arun Patole274f0702015-05-05 13:33:30 +05302170
Olli Etuahof119a262016-08-19 15:54:22 +03002171 case EOpStep:
Arun Patolebf790422015-05-18 17:53:04 +05302172 {
Olli Etuahof119a262016-08-19 15:54:22 +03002173 ASSERT(basicType == EbtFloat);
2174 resultArray = new TConstantUnion[maxObjectSize];
2175 for (size_t i = 0; i < maxObjectSize; i++)
2176 resultArray[i].setFConst(
2177 unionArrays[1][i].getFConst() < unionArrays[0][i].getFConst() ? 0.0f
2178 : 1.0f);
2179 break;
Arun Patolebf790422015-05-18 17:53:04 +05302180 }
Arun Patolebf790422015-05-18 17:53:04 +05302181
Olli Etuahof119a262016-08-19 15:54:22 +03002182 case EOpLessThan:
Arun Patole9d0b1f92015-05-20 14:27:17 +05302183 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002184 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole9d0b1f92015-05-20 14:27:17 +05302185 for (size_t i = 0; i < maxObjectSize; i++)
2186 {
2187 switch (basicType)
2188 {
Olli Etuahof119a262016-08-19 15:54:22 +03002189 case EbtFloat:
2190 resultArray[i].setBConst(unionArrays[0][i].getFConst() <
2191 unionArrays[1][i].getFConst());
2192 break;
2193 case EbtInt:
2194 resultArray[i].setBConst(unionArrays[0][i].getIConst() <
2195 unionArrays[1][i].getIConst());
2196 break;
2197 case EbtUInt:
2198 resultArray[i].setBConst(unionArrays[0][i].getUConst() <
2199 unionArrays[1][i].getUConst());
2200 break;
2201 default:
2202 UNREACHABLE();
2203 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302204 }
2205 }
Olli Etuahof119a262016-08-19 15:54:22 +03002206 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302207 }
Arun Patole9d0b1f92015-05-20 14:27:17 +05302208
Olli Etuahof119a262016-08-19 15:54:22 +03002209 case EOpLessThanEqual:
Arun Patole9d0b1f92015-05-20 14:27:17 +05302210 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002211 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole9d0b1f92015-05-20 14:27:17 +05302212 for (size_t i = 0; i < maxObjectSize; i++)
2213 {
2214 switch (basicType)
2215 {
Olli Etuahof119a262016-08-19 15:54:22 +03002216 case EbtFloat:
2217 resultArray[i].setBConst(unionArrays[0][i].getFConst() <=
2218 unionArrays[1][i].getFConst());
2219 break;
2220 case EbtInt:
2221 resultArray[i].setBConst(unionArrays[0][i].getIConst() <=
2222 unionArrays[1][i].getIConst());
2223 break;
2224 case EbtUInt:
2225 resultArray[i].setBConst(unionArrays[0][i].getUConst() <=
2226 unionArrays[1][i].getUConst());
2227 break;
2228 default:
2229 UNREACHABLE();
2230 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302231 }
2232 }
Olli Etuahof119a262016-08-19 15:54:22 +03002233 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302234 }
Arun Patole9d0b1f92015-05-20 14:27:17 +05302235
Olli Etuahof119a262016-08-19 15:54:22 +03002236 case EOpGreaterThan:
Arun Patole9d0b1f92015-05-20 14:27:17 +05302237 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002238 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole9d0b1f92015-05-20 14:27:17 +05302239 for (size_t i = 0; i < maxObjectSize; i++)
2240 {
2241 switch (basicType)
2242 {
Olli Etuahof119a262016-08-19 15:54:22 +03002243 case EbtFloat:
2244 resultArray[i].setBConst(unionArrays[0][i].getFConst() >
2245 unionArrays[1][i].getFConst());
2246 break;
2247 case EbtInt:
2248 resultArray[i].setBConst(unionArrays[0][i].getIConst() >
2249 unionArrays[1][i].getIConst());
2250 break;
2251 case EbtUInt:
2252 resultArray[i].setBConst(unionArrays[0][i].getUConst() >
2253 unionArrays[1][i].getUConst());
2254 break;
2255 default:
2256 UNREACHABLE();
2257 break;
Olli Etuahob43846e2015-06-02 18:18:57 +03002258 }
2259 }
Olli Etuahof119a262016-08-19 15:54:22 +03002260 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302261 }
Olli Etuahof119a262016-08-19 15:54:22 +03002262 case EOpGreaterThanEqual:
Arun Patole9d0b1f92015-05-20 14:27:17 +05302263 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002264 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole9d0b1f92015-05-20 14:27:17 +05302265 for (size_t i = 0; i < maxObjectSize; i++)
2266 {
2267 switch (basicType)
2268 {
Olli Etuahof119a262016-08-19 15:54:22 +03002269 case EbtFloat:
2270 resultArray[i].setBConst(unionArrays[0][i].getFConst() >=
2271 unionArrays[1][i].getFConst());
2272 break;
2273 case EbtInt:
2274 resultArray[i].setBConst(unionArrays[0][i].getIConst() >=
2275 unionArrays[1][i].getIConst());
2276 break;
2277 case EbtUInt:
2278 resultArray[i].setBConst(unionArrays[0][i].getUConst() >=
2279 unionArrays[1][i].getUConst());
2280 break;
2281 default:
2282 UNREACHABLE();
2283 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302284 }
2285 }
2286 }
2287 break;
2288
Olli Etuahof119a262016-08-19 15:54:22 +03002289 case EOpVectorEqual:
Arun Patole9d0b1f92015-05-20 14:27:17 +05302290 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002291 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole9d0b1f92015-05-20 14:27:17 +05302292 for (size_t i = 0; i < maxObjectSize; i++)
2293 {
2294 switch (basicType)
2295 {
Olli Etuahof119a262016-08-19 15:54:22 +03002296 case EbtFloat:
2297 resultArray[i].setBConst(unionArrays[0][i].getFConst() ==
2298 unionArrays[1][i].getFConst());
2299 break;
2300 case EbtInt:
2301 resultArray[i].setBConst(unionArrays[0][i].getIConst() ==
2302 unionArrays[1][i].getIConst());
2303 break;
2304 case EbtUInt:
2305 resultArray[i].setBConst(unionArrays[0][i].getUConst() ==
2306 unionArrays[1][i].getUConst());
2307 break;
2308 case EbtBool:
2309 resultArray[i].setBConst(unionArrays[0][i].getBConst() ==
2310 unionArrays[1][i].getBConst());
2311 break;
2312 default:
2313 UNREACHABLE();
2314 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302315 }
2316 }
Olli Etuahof119a262016-08-19 15:54:22 +03002317 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302318 }
Arun Patole9d0b1f92015-05-20 14:27:17 +05302319
Olli Etuahof119a262016-08-19 15:54:22 +03002320 case EOpVectorNotEqual:
Arun Patole9d0b1f92015-05-20 14:27:17 +05302321 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002322 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole9d0b1f92015-05-20 14:27:17 +05302323 for (size_t i = 0; i < maxObjectSize; i++)
2324 {
2325 switch (basicType)
2326 {
Olli Etuahof119a262016-08-19 15:54:22 +03002327 case EbtFloat:
2328 resultArray[i].setBConst(unionArrays[0][i].getFConst() !=
2329 unionArrays[1][i].getFConst());
2330 break;
2331 case EbtInt:
2332 resultArray[i].setBConst(unionArrays[0][i].getIConst() !=
2333 unionArrays[1][i].getIConst());
2334 break;
2335 case EbtUInt:
2336 resultArray[i].setBConst(unionArrays[0][i].getUConst() !=
2337 unionArrays[1][i].getUConst());
2338 break;
2339 case EbtBool:
2340 resultArray[i].setBConst(unionArrays[0][i].getBConst() !=
2341 unionArrays[1][i].getBConst());
2342 break;
2343 default:
2344 UNREACHABLE();
2345 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302346 }
2347 }
Olli Etuahof119a262016-08-19 15:54:22 +03002348 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302349 }
Arun Patole9d0b1f92015-05-20 14:27:17 +05302350
Olli Etuahof119a262016-08-19 15:54:22 +03002351 case EOpDistance:
Arun Patole1155ddd2015-06-05 18:04:36 +05302352 {
Olli Etuahof119a262016-08-19 15:54:22 +03002353 ASSERT(basicType == EbtFloat);
Arun Patole1155ddd2015-06-05 18:04:36 +05302354 TConstantUnion *distanceArray = new TConstantUnion[maxObjectSize];
Olli Etuahof119a262016-08-19 15:54:22 +03002355 resultArray = new TConstantUnion();
Arun Patole1155ddd2015-06-05 18:04:36 +05302356 for (size_t i = 0; i < maxObjectSize; i++)
2357 {
2358 float x = unionArrays[0][i].getFConst();
2359 float y = unionArrays[1][i].getFConst();
2360 distanceArray[i].setFConst(x - y);
2361 }
Olli Etuahob43846e2015-06-02 18:18:57 +03002362 resultArray->setFConst(VectorLength(distanceArray, maxObjectSize));
Olli Etuahof119a262016-08-19 15:54:22 +03002363 break;
Arun Patole1155ddd2015-06-05 18:04:36 +05302364 }
Arun Patole1155ddd2015-06-05 18:04:36 +05302365
Olli Etuahof119a262016-08-19 15:54:22 +03002366 case EOpDot:
2367 ASSERT(basicType == EbtFloat);
Olli Etuahob43846e2015-06-02 18:18:57 +03002368 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03002369 resultArray->setFConst(
2370 VectorDotProduct(unionArrays[0], unionArrays[1], maxObjectSize));
2371 break;
Arun Patole1155ddd2015-06-05 18:04:36 +05302372
Olli Etuahof119a262016-08-19 15:54:22 +03002373 case EOpCross:
Arun Patole1155ddd2015-06-05 18:04:36 +05302374 {
Olli Etuahof119a262016-08-19 15:54:22 +03002375 ASSERT(basicType == EbtFloat && maxObjectSize == 3);
Olli Etuahob43846e2015-06-02 18:18:57 +03002376 resultArray = new TConstantUnion[maxObjectSize];
Olli Etuahof119a262016-08-19 15:54:22 +03002377 float x0 = unionArrays[0][0].getFConst();
2378 float x1 = unionArrays[0][1].getFConst();
2379 float x2 = unionArrays[0][2].getFConst();
2380 float y0 = unionArrays[1][0].getFConst();
2381 float y1 = unionArrays[1][1].getFConst();
2382 float y2 = unionArrays[1][2].getFConst();
Olli Etuahob43846e2015-06-02 18:18:57 +03002383 resultArray[0].setFConst(x1 * y2 - y1 * x2);
2384 resultArray[1].setFConst(x2 * y0 - y2 * x0);
2385 resultArray[2].setFConst(x0 * y1 - y0 * x1);
Olli Etuahof119a262016-08-19 15:54:22 +03002386 break;
Arun Patole1155ddd2015-06-05 18:04:36 +05302387 }
Arun Patole1155ddd2015-06-05 18:04:36 +05302388
Olli Etuahof119a262016-08-19 15:54:22 +03002389 case EOpReflect:
Arun Patole1155ddd2015-06-05 18:04:36 +05302390 {
Olli Etuahof119a262016-08-19 15:54:22 +03002391 ASSERT(basicType == EbtFloat);
Arun Patole1155ddd2015-06-05 18:04:36 +05302392 // genType reflect (genType I, genType N) :
Olli Etuahof119a262016-08-19 15:54:22 +03002393 // For the incident vector I and surface orientation N, returns the reflection
2394 // direction:
Arun Patole1155ddd2015-06-05 18:04:36 +05302395 // I - 2 * dot(N, I) * N.
Olli Etuahof119a262016-08-19 15:54:22 +03002396 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole1155ddd2015-06-05 18:04:36 +05302397 float dotProduct = VectorDotProduct(unionArrays[1], unionArrays[0], maxObjectSize);
2398 for (size_t i = 0; i < maxObjectSize; i++)
2399 {
2400 float result = unionArrays[0][i].getFConst() -
2401 2.0f * dotProduct * unionArrays[1][i].getFConst();
Olli Etuahob43846e2015-06-02 18:18:57 +03002402 resultArray[i].setFConst(result);
Arun Patole1155ddd2015-06-05 18:04:36 +05302403 }
Olli Etuahof119a262016-08-19 15:54:22 +03002404 break;
Arun Patole1155ddd2015-06-05 18:04:36 +05302405 }
Arun Patole1155ddd2015-06-05 18:04:36 +05302406
Olli Etuahof119a262016-08-19 15:54:22 +03002407 case EOpMul:
Arun Patole7fa33552015-06-10 15:15:18 +05302408 {
Olli Etuahof119a262016-08-19 15:54:22 +03002409 ASSERT(basicType == EbtFloat && (*sequence)[0]->getAsTyped()->isMatrix() &&
2410 (*sequence)[1]->getAsTyped()->isMatrix());
Arun Patole7fa33552015-06-10 15:15:18 +05302411 // Perform component-wise matrix multiplication.
2412 resultArray = new TConstantUnion[maxObjectSize];
Olli Etuahof119a262016-08-19 15:54:22 +03002413 int size = (*sequence)[0]->getAsTyped()->getNominalSize();
Arun Patole7fa33552015-06-10 15:15:18 +05302414 angle::Matrix<float> result =
2415 GetMatrix(unionArrays[0], size).compMult(GetMatrix(unionArrays[1], size));
2416 SetUnionArrayFromMatrix(result, resultArray);
Olli Etuahof119a262016-08-19 15:54:22 +03002417 break;
Arun Patole7fa33552015-06-10 15:15:18 +05302418 }
Arun Patole7fa33552015-06-10 15:15:18 +05302419
Olli Etuahof119a262016-08-19 15:54:22 +03002420 case EOpOuterProduct:
Arun Patole7fa33552015-06-10 15:15:18 +05302421 {
Olli Etuahof119a262016-08-19 15:54:22 +03002422 ASSERT(basicType == EbtFloat);
Arun Patole7fa33552015-06-10 15:15:18 +05302423 size_t numRows = (*sequence)[0]->getAsTyped()->getType().getObjectSize();
2424 size_t numCols = (*sequence)[1]->getAsTyped()->getType().getObjectSize();
Olli Etuahof119a262016-08-19 15:54:22 +03002425 resultArray = new TConstantUnion[numRows * numCols];
Arun Patole7fa33552015-06-10 15:15:18 +05302426 angle::Matrix<float> result =
Olli Etuahod5da5052016-08-29 13:16:55 +03002427 GetMatrix(unionArrays[0], static_cast<int>(numRows), 1)
2428 .outerProduct(GetMatrix(unionArrays[1], 1, static_cast<int>(numCols)));
Arun Patole7fa33552015-06-10 15:15:18 +05302429 SetUnionArrayFromMatrix(result, resultArray);
Olli Etuahof119a262016-08-19 15:54:22 +03002430 break;
Arun Patole7fa33552015-06-10 15:15:18 +05302431 }
Arun Patole7fa33552015-06-10 15:15:18 +05302432
Olli Etuahof119a262016-08-19 15:54:22 +03002433 default:
2434 UNREACHABLE();
2435 // TODO: Add constant folding support for other built-in operations that take 2
2436 // parameters and not handled above.
2437 return nullptr;
Arun Patole274f0702015-05-05 13:33:30 +05302438 }
2439 }
2440 else if (paramsCount == 3)
2441 {
2442 //
2443 // Ternary built-in
2444 //
2445 switch (op)
2446 {
Olli Etuahof119a262016-08-19 15:54:22 +03002447 case EOpClamp:
Arun Patole274f0702015-05-05 13:33:30 +05302448 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002449 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole274f0702015-05-05 13:33:30 +05302450 for (size_t i = 0; i < maxObjectSize; i++)
2451 {
2452 switch (basicType)
2453 {
Olli Etuahof119a262016-08-19 15:54:22 +03002454 case EbtFloat:
Arun Patole274f0702015-05-05 13:33:30 +05302455 {
Olli Etuahof119a262016-08-19 15:54:22 +03002456 float x = unionArrays[0][i].getFConst();
Arun Patole274f0702015-05-05 13:33:30 +05302457 float min = unionArrays[1][i].getFConst();
2458 float max = unionArrays[2][i].getFConst();
2459 // Results are undefined if min > max.
2460 if (min > max)
Olli Etuahof119a262016-08-19 15:54:22 +03002461 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
2462 &resultArray[i]);
Arun Patole274f0702015-05-05 13:33:30 +05302463 else
Olli Etuahob43846e2015-06-02 18:18:57 +03002464 resultArray[i].setFConst(gl::clamp(x, min, max));
Olli Etuahof119a262016-08-19 15:54:22 +03002465 break;
Arun Patole274f0702015-05-05 13:33:30 +05302466 }
Olli Etuahof119a262016-08-19 15:54:22 +03002467
2468 case EbtInt:
Arun Patole274f0702015-05-05 13:33:30 +05302469 {
Olli Etuahof119a262016-08-19 15:54:22 +03002470 int x = unionArrays[0][i].getIConst();
Arun Patole274f0702015-05-05 13:33:30 +05302471 int min = unionArrays[1][i].getIConst();
2472 int max = unionArrays[2][i].getIConst();
2473 // Results are undefined if min > max.
2474 if (min > max)
Olli Etuahof119a262016-08-19 15:54:22 +03002475 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
2476 &resultArray[i]);
Arun Patole274f0702015-05-05 13:33:30 +05302477 else
Olli Etuahob43846e2015-06-02 18:18:57 +03002478 resultArray[i].setIConst(gl::clamp(x, min, max));
Olli Etuahof119a262016-08-19 15:54:22 +03002479 break;
Arun Patole274f0702015-05-05 13:33:30 +05302480 }
Olli Etuahof119a262016-08-19 15:54:22 +03002481 case EbtUInt:
Arun Patole274f0702015-05-05 13:33:30 +05302482 {
Olli Etuahof119a262016-08-19 15:54:22 +03002483 unsigned int x = unionArrays[0][i].getUConst();
Arun Patole274f0702015-05-05 13:33:30 +05302484 unsigned int min = unionArrays[1][i].getUConst();
2485 unsigned int max = unionArrays[2][i].getUConst();
2486 // Results are undefined if min > max.
2487 if (min > max)
Olli Etuahof119a262016-08-19 15:54:22 +03002488 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
2489 &resultArray[i]);
Arun Patole274f0702015-05-05 13:33:30 +05302490 else
Olli Etuahob43846e2015-06-02 18:18:57 +03002491 resultArray[i].setUConst(gl::clamp(x, min, max));
Olli Etuahof119a262016-08-19 15:54:22 +03002492 break;
Arun Patole274f0702015-05-05 13:33:30 +05302493 }
Olli Etuahof119a262016-08-19 15:54:22 +03002494 default:
2495 UNREACHABLE();
2496 break;
Arun Patole274f0702015-05-05 13:33:30 +05302497 }
2498 }
Olli Etuahof119a262016-08-19 15:54:22 +03002499 break;
Arun Patole274f0702015-05-05 13:33:30 +05302500 }
Arun Patole274f0702015-05-05 13:33:30 +05302501
Olli Etuahof119a262016-08-19 15:54:22 +03002502 case EOpMix:
Arun Patolebf790422015-05-18 17:53:04 +05302503 {
Olli Etuahof119a262016-08-19 15:54:22 +03002504 ASSERT(basicType == EbtFloat);
2505 resultArray = new TConstantUnion[maxObjectSize];
2506 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302507 {
Olli Etuahof119a262016-08-19 15:54:22 +03002508 float x = unionArrays[0][i].getFConst();
2509 float y = unionArrays[1][i].getFConst();
2510 TBasicType type = (*sequence)[2]->getAsTyped()->getType().getBasicType();
2511 if (type == EbtFloat)
Arun Patolebf790422015-05-18 17:53:04 +05302512 {
Olli Etuahof119a262016-08-19 15:54:22 +03002513 // Returns the linear blend of x and y, i.e., x * (1 - a) + y * a.
2514 float a = unionArrays[2][i].getFConst();
2515 resultArray[i].setFConst(x * (1.0f - a) + y * a);
2516 }
2517 else // 3rd parameter is EbtBool
2518 {
2519 ASSERT(type == EbtBool);
2520 // Selects which vector each returned component comes from.
2521 // For a component of a that is false, the corresponding component of x is
2522 // returned.
2523 // For a component of a that is true, the corresponding component of y is
2524 // returned.
2525 bool a = unionArrays[2][i].getBConst();
2526 resultArray[i].setFConst(a ? y : x);
Arun Patolebf790422015-05-18 17:53:04 +05302527 }
2528 }
Olli Etuahof119a262016-08-19 15:54:22 +03002529 break;
Arun Patolebf790422015-05-18 17:53:04 +05302530 }
Arun Patolebf790422015-05-18 17:53:04 +05302531
Olli Etuahof119a262016-08-19 15:54:22 +03002532 case EOpSmoothStep:
Arun Patolebf790422015-05-18 17:53:04 +05302533 {
Olli Etuahof119a262016-08-19 15:54:22 +03002534 ASSERT(basicType == EbtFloat);
2535 resultArray = new TConstantUnion[maxObjectSize];
2536 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302537 {
Olli Etuahof119a262016-08-19 15:54:22 +03002538 float edge0 = unionArrays[0][i].getFConst();
2539 float edge1 = unionArrays[1][i].getFConst();
2540 float x = unionArrays[2][i].getFConst();
2541 // Results are undefined if edge0 >= edge1.
2542 if (edge0 >= edge1)
Arun Patolebf790422015-05-18 17:53:04 +05302543 {
Olli Etuahof119a262016-08-19 15:54:22 +03002544 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
2545 &resultArray[i]);
2546 }
2547 else
2548 {
2549 // Returns 0.0 if x <= edge0 and 1.0 if x >= edge1 and performs smooth
2550 // Hermite interpolation between 0 and 1 when edge0 < x < edge1.
2551 float t = gl::clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
2552 resultArray[i].setFConst(t * t * (3.0f - 2.0f * t));
Arun Patolebf790422015-05-18 17:53:04 +05302553 }
2554 }
Olli Etuahof119a262016-08-19 15:54:22 +03002555 break;
Arun Patolebf790422015-05-18 17:53:04 +05302556 }
Arun Patolebf790422015-05-18 17:53:04 +05302557
Olli Etuahof119a262016-08-19 15:54:22 +03002558 case EOpFaceForward:
Arun Patole1155ddd2015-06-05 18:04:36 +05302559 {
Olli Etuahof119a262016-08-19 15:54:22 +03002560 ASSERT(basicType == EbtFloat);
Arun Patole1155ddd2015-06-05 18:04:36 +05302561 // genType faceforward(genType N, genType I, genType Nref) :
2562 // If dot(Nref, I) < 0 return N, otherwise return -N.
Olli Etuahof119a262016-08-19 15:54:22 +03002563 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole1155ddd2015-06-05 18:04:36 +05302564 float dotProduct = VectorDotProduct(unionArrays[2], unionArrays[1], maxObjectSize);
2565 for (size_t i = 0; i < maxObjectSize; i++)
2566 {
2567 if (dotProduct < 0)
Olli Etuahob43846e2015-06-02 18:18:57 +03002568 resultArray[i].setFConst(unionArrays[0][i].getFConst());
Arun Patole1155ddd2015-06-05 18:04:36 +05302569 else
Olli Etuahob43846e2015-06-02 18:18:57 +03002570 resultArray[i].setFConst(-unionArrays[0][i].getFConst());
Arun Patole1155ddd2015-06-05 18:04:36 +05302571 }
Olli Etuahof119a262016-08-19 15:54:22 +03002572 break;
Arun Patole1155ddd2015-06-05 18:04:36 +05302573 }
Arun Patole1155ddd2015-06-05 18:04:36 +05302574
Olli Etuahof119a262016-08-19 15:54:22 +03002575 case EOpRefract:
Arun Patole1155ddd2015-06-05 18:04:36 +05302576 {
Olli Etuahof119a262016-08-19 15:54:22 +03002577 ASSERT(basicType == EbtFloat);
Arun Patole1155ddd2015-06-05 18:04:36 +05302578 // genType refract(genType I, genType N, float eta) :
Olli Etuahof119a262016-08-19 15:54:22 +03002579 // For the incident vector I and surface normal N, and the ratio of indices of
2580 // refraction eta,
Arun Patole1155ddd2015-06-05 18:04:36 +05302581 // return the refraction vector. The result is computed by
2582 // k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I))
2583 // if (k < 0.0)
2584 // return genType(0.0)
2585 // else
2586 // return eta * I - (eta * dot(N, I) + sqrt(k)) * N
Olli Etuahof119a262016-08-19 15:54:22 +03002587 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole1155ddd2015-06-05 18:04:36 +05302588 float dotProduct = VectorDotProduct(unionArrays[1], unionArrays[0], maxObjectSize);
2589 for (size_t i = 0; i < maxObjectSize; i++)
2590 {
2591 float eta = unionArrays[2][i].getFConst();
Olli Etuahof119a262016-08-19 15:54:22 +03002592 float k = 1.0f - eta * eta * (1.0f - dotProduct * dotProduct);
Arun Patole1155ddd2015-06-05 18:04:36 +05302593 if (k < 0.0f)
Olli Etuahob43846e2015-06-02 18:18:57 +03002594 resultArray[i].setFConst(0.0f);
Arun Patole1155ddd2015-06-05 18:04:36 +05302595 else
Olli Etuahob43846e2015-06-02 18:18:57 +03002596 resultArray[i].setFConst(eta * unionArrays[0][i].getFConst() -
Olli Etuahof119a262016-08-19 15:54:22 +03002597 (eta * dotProduct + sqrtf(k)) *
2598 unionArrays[1][i].getFConst());
Arun Patole1155ddd2015-06-05 18:04:36 +05302599 }
Olli Etuahof119a262016-08-19 15:54:22 +03002600 break;
Arun Patole1155ddd2015-06-05 18:04:36 +05302601 }
Arun Patole1155ddd2015-06-05 18:04:36 +05302602
Olli Etuahof119a262016-08-19 15:54:22 +03002603 default:
2604 UNREACHABLE();
2605 // TODO: Add constant folding support for other built-in operations that take 3
2606 // parameters and not handled above.
2607 return nullptr;
Arun Patole274f0702015-05-05 13:33:30 +05302608 }
2609 }
Olli Etuahob43846e2015-06-02 18:18:57 +03002610 return resultArray;
Arun Patole274f0702015-05-05 13:33:30 +05302611}
2612
2613// static
Jamie Madillb1a85f42014-08-19 15:23:24 -04002614TString TIntermTraverser::hash(const TString &name, ShHashFunction64 hashFunction)
2615{
2616 if (hashFunction == NULL || name.empty())
2617 return name;
2618 khronos_uint64_t number = (*hashFunction)(name.c_str(), name.length());
2619 TStringStream stream;
2620 stream << HASHED_NAME_PREFIX << std::hex << number;
2621 TString hashedName = stream.str();
2622 return hashedName;
2623}
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002624
2625void TIntermTraverser::updateTree()
2626{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002627 for (size_t ii = 0; ii < mInsertions.size(); ++ii)
2628 {
2629 const NodeInsertMultipleEntry &insertion = mInsertions[ii];
2630 ASSERT(insertion.parent);
Olli Etuaho5d91dda2015-06-18 15:47:46 +03002631 if (!insertion.insertionsAfter.empty())
2632 {
2633 bool inserted = insertion.parent->insertChildNodes(insertion.position + 1,
2634 insertion.insertionsAfter);
2635 ASSERT(inserted);
2636 UNUSED_ASSERTION_VARIABLE(inserted);
2637 }
2638 if (!insertion.insertionsBefore.empty())
2639 {
2640 bool inserted =
2641 insertion.parent->insertChildNodes(insertion.position, insertion.insertionsBefore);
2642 ASSERT(inserted);
2643 UNUSED_ASSERTION_VARIABLE(inserted);
2644 }
Olli Etuahoa6f22092015-05-08 18:31:10 +03002645 }
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002646 for (size_t ii = 0; ii < mReplacements.size(); ++ii)
2647 {
Olli Etuahocd94ef92015-04-16 19:18:10 +03002648 const NodeUpdateEntry &replacement = mReplacements[ii];
2649 ASSERT(replacement.parent);
2650 bool replaced = replacement.parent->replaceChildNode(
2651 replacement.original, replacement.replacement);
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002652 ASSERT(replaced);
Olli Etuahod57e0db2015-04-24 15:05:08 +03002653 UNUSED_ASSERTION_VARIABLE(replaced);
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002654
Olli Etuahocd94ef92015-04-16 19:18:10 +03002655 if (!replacement.originalBecomesChildOfReplacement)
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002656 {
2657 // In AST traversing, a parent is visited before its children.
Olli Etuahocd94ef92015-04-16 19:18:10 +03002658 // After we replace a node, if its immediate child is to
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002659 // be replaced, we need to make sure we don't update the replaced
2660 // node; instead, we update the replacement node.
2661 for (size_t jj = ii + 1; jj < mReplacements.size(); ++jj)
2662 {
Olli Etuahocd94ef92015-04-16 19:18:10 +03002663 NodeUpdateEntry &replacement2 = mReplacements[jj];
2664 if (replacement2.parent == replacement.original)
2665 replacement2.parent = replacement.replacement;
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002666 }
2667 }
2668 }
Olli Etuahofc0e2bc2015-04-16 13:39:56 +03002669 for (size_t ii = 0; ii < mMultiReplacements.size(); ++ii)
2670 {
2671 const NodeReplaceWithMultipleEntry &replacement = mMultiReplacements[ii];
2672 ASSERT(replacement.parent);
2673 bool replaced = replacement.parent->replaceChildNodeWithMultiple(
2674 replacement.original, replacement.replacements);
2675 ASSERT(replaced);
Olli Etuahod57e0db2015-04-24 15:05:08 +03002676 UNUSED_ASSERTION_VARIABLE(replaced);
Olli Etuahofc0e2bc2015-04-16 13:39:56 +03002677 }
Olli Etuahod4f303e2015-05-20 17:09:06 +03002678
Jamie Madill03d863c2016-07-27 18:15:53 -04002679 clearReplacementQueue();
2680}
2681
2682void TIntermTraverser::clearReplacementQueue()
2683{
Olli Etuahod4f303e2015-05-20 17:09:06 +03002684 mReplacements.clear();
2685 mMultiReplacements.clear();
Jamie Madill03d863c2016-07-27 18:15:53 -04002686 mInsertions.clear();
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002687}
Jamie Madill1048e432016-07-23 18:51:28 -04002688
Jamie Madill03d863c2016-07-27 18:15:53 -04002689void TIntermTraverser::queueReplacement(TIntermNode *original,
2690 TIntermNode *replacement,
2691 OriginalNode originalStatus)
Jamie Madill1048e432016-07-23 18:51:28 -04002692{
Jamie Madill03d863c2016-07-27 18:15:53 -04002693 queueReplacementWithParent(getParentNode(), original, replacement, originalStatus);
Jamie Madill1048e432016-07-23 18:51:28 -04002694}
2695
Jamie Madill03d863c2016-07-27 18:15:53 -04002696void TIntermTraverser::queueReplacementWithParent(TIntermNode *parent,
2697 TIntermNode *original,
2698 TIntermNode *replacement,
2699 OriginalNode originalStatus)
Jamie Madill1048e432016-07-23 18:51:28 -04002700{
Jamie Madill03d863c2016-07-27 18:15:53 -04002701 bool originalBecomesChild = (originalStatus == OriginalNode::BECOMES_CHILD);
2702 mReplacements.push_back(NodeUpdateEntry(parent, original, replacement, originalBecomesChild));
Jamie Madill1048e432016-07-23 18:51:28 -04002703}