blob: b62ce4f7f402c0d2dc2829adca1cdfca71ea237a [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"
Jamie Madillb1a85f42014-08-19 15:23:24 -040020#include "compiler/translator/HashNames.h"
21#include "compiler/translator/IntermNode.h"
22#include "compiler/translator/SymbolTable.h"
23
24namespace
25{
26
Arun Patole9dea48f2015-04-02 11:45:09 +053027const float kPi = 3.14159265358979323846f;
28const float kDegreesToRadiansMultiplier = kPi / 180.0f;
29const float kRadiansToDegreesMultiplier = 180.0f / kPi;
30
Jamie Madillb1a85f42014-08-19 15:23:24 -040031TPrecision GetHigherPrecision(TPrecision left, TPrecision right)
32{
33 return left > right ? left : right;
34}
35
36bool ValidateMultiplication(TOperator op, const TType &left, const TType &right)
37{
38 switch (op)
39 {
40 case EOpMul:
41 case EOpMulAssign:
42 return left.getNominalSize() == right.getNominalSize() &&
43 left.getSecondarySize() == right.getSecondarySize();
44 case EOpVectorTimesScalar:
45 case EOpVectorTimesScalarAssign:
46 return true;
47 case EOpVectorTimesMatrix:
48 return left.getNominalSize() == right.getRows();
49 case EOpVectorTimesMatrixAssign:
50 return left.getNominalSize() == right.getRows() &&
51 left.getNominalSize() == right.getCols();
52 case EOpMatrixTimesVector:
53 return left.getCols() == right.getNominalSize();
54 case EOpMatrixTimesScalar:
55 case EOpMatrixTimesScalarAssign:
56 return true;
57 case EOpMatrixTimesMatrix:
58 return left.getCols() == right.getRows();
59 case EOpMatrixTimesMatrixAssign:
60 return left.getCols() == right.getCols() &&
61 left.getRows() == right.getRows();
62
63 default:
64 UNREACHABLE();
65 return false;
66 }
67}
68
69bool CompareStructure(const TType& leftNodeType,
Jamie Madillb11e2482015-05-04 14:21:22 -040070 const TConstantUnion *rightUnionArray,
71 const TConstantUnion *leftUnionArray);
Jamie Madillb1a85f42014-08-19 15:23:24 -040072
73bool CompareStruct(const TType &leftNodeType,
Jamie Madillb11e2482015-05-04 14:21:22 -040074 const TConstantUnion *rightUnionArray,
75 const TConstantUnion *leftUnionArray)
Jamie Madillb1a85f42014-08-19 15:23:24 -040076{
77 const TFieldList &fields = leftNodeType.getStruct()->fields();
78
79 size_t structSize = fields.size();
80 size_t index = 0;
81
82 for (size_t j = 0; j < structSize; j++)
83 {
84 size_t size = fields[j]->type()->getObjectSize();
85 for (size_t i = 0; i < size; i++)
86 {
87 if (fields[j]->type()->getBasicType() == EbtStruct)
88 {
89 if (!CompareStructure(*fields[j]->type(),
90 &rightUnionArray[index],
91 &leftUnionArray[index]))
92 {
93 return false;
94 }
95 }
96 else
97 {
98 if (leftUnionArray[index] != rightUnionArray[index])
99 return false;
100 index++;
101 }
102 }
103 }
104 return true;
105}
106
107bool CompareStructure(const TType &leftNodeType,
Jamie Madillb11e2482015-05-04 14:21:22 -0400108 const TConstantUnion *rightUnionArray,
109 const TConstantUnion *leftUnionArray)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400110{
111 if (leftNodeType.isArray())
112 {
113 TType typeWithoutArrayness = leftNodeType;
114 typeWithoutArrayness.clearArrayness();
115
116 size_t arraySize = leftNodeType.getArraySize();
117
118 for (size_t i = 0; i < arraySize; ++i)
119 {
120 size_t offset = typeWithoutArrayness.getObjectSize() * i;
121 if (!CompareStruct(typeWithoutArrayness,
122 &rightUnionArray[offset],
123 &leftUnionArray[offset]))
124 {
125 return false;
126 }
127 }
128 }
129 else
130 {
131 return CompareStruct(leftNodeType, rightUnionArray, leftUnionArray);
132 }
133 return true;
134}
135
Arun Patole274f0702015-05-05 13:33:30 +0530136TConstantUnion *Vectorize(const TConstantUnion &constant, size_t size)
137{
138 TConstantUnion *constUnion = new TConstantUnion[size];
139 for (unsigned int i = 0; i < size; ++i)
140 constUnion[i] = constant;
141
142 return constUnion;
143}
144
Arun Patolebf790422015-05-18 17:53:04 +0530145void UndefinedConstantFoldingError(const TSourceLoc &loc, TOperator op, TBasicType basicType,
146 TInfoSink &infoSink, TConstantUnion *result)
147{
148 std::stringstream constantFoldingErrorStream;
149 constantFoldingErrorStream << "'" << GetOperatorString(op)
150 << "' operation result is undefined for the values passed in";
151 infoSink.info.message(EPrefixWarning, loc, constantFoldingErrorStream.str().c_str());
152
153 switch (basicType)
154 {
155 case EbtFloat :
156 result->setFConst(0.0f);
157 break;
158 case EbtInt:
159 result->setIConst(0);
160 break;
161 case EbtUInt:
162 result->setUConst(0u);
163 break;
164 case EbtBool:
165 result->setBConst(false);
166 break;
167 default:
168 break;
169 }
170}
171
Arun Patole1155ddd2015-06-05 18:04:36 +0530172float VectorLength(TConstantUnion *paramArray, size_t paramArraySize)
173{
174 float result = 0.0f;
175 for (size_t i = 0; i < paramArraySize; i++)
176 {
177 float f = paramArray[i].getFConst();
178 result += f * f;
179 }
180 return sqrtf(result);
181}
182
183float VectorDotProduct(TConstantUnion *paramArray1, TConstantUnion *paramArray2, size_t paramArraySize)
184{
185 float result = 0.0f;
186 for (size_t i = 0; i < paramArraySize; i++)
187 result += paramArray1[i].getFConst() * paramArray2[i].getFConst();
188 return result;
189}
190
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200191TIntermTyped *CreateFoldedNode(TConstantUnion *constArray,
192 const TIntermTyped *originalNode,
193 TQualifier qualifier)
Olli Etuahob43846e2015-06-02 18:18:57 +0300194{
195 if (constArray == nullptr)
196 {
197 return nullptr;
198 }
199 TIntermTyped *folded = new TIntermConstantUnion(constArray, originalNode->getType());
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200200 folded->getTypePointer()->setQualifier(qualifier);
Olli Etuahob43846e2015-06-02 18:18:57 +0300201 folded->setLine(originalNode->getLine());
202 return folded;
203}
204
Arun Patole7fa33552015-06-10 15:15:18 +0530205angle::Matrix<float> GetMatrix(TConstantUnion *paramArray, const unsigned int &rows, const unsigned int &cols)
206{
207 std::vector<float> elements;
208 for (size_t i = 0; i < rows * cols; i++)
209 elements.push_back(paramArray[i].getFConst());
210 // Transpose is used since the Matrix constructor expects arguments in row-major order,
211 // whereas the paramArray is in column-major order.
212 return angle::Matrix<float>(elements, rows, cols).transpose();
213}
214
215angle::Matrix<float> GetMatrix(TConstantUnion *paramArray, const unsigned int &size)
216{
217 std::vector<float> elements;
218 for (size_t i = 0; i < size * size; i++)
219 elements.push_back(paramArray[i].getFConst());
220 // Transpose is used since the Matrix constructor expects arguments in row-major order,
221 // whereas the paramArray is in column-major order.
222 return angle::Matrix<float>(elements, size).transpose();
223}
224
225void SetUnionArrayFromMatrix(const angle::Matrix<float> &m, TConstantUnion *resultArray)
226{
227 // Transpose is used since the input Matrix is in row-major order,
228 // whereas the actual result should be in column-major order.
229 angle::Matrix<float> result = m.transpose();
230 std::vector<float> resultElements = result.elements();
231 for (size_t i = 0; i < resultElements.size(); i++)
232 resultArray[i].setFConst(resultElements[i]);
233}
234
Jamie Madillb1a85f42014-08-19 15:23:24 -0400235} // namespace anonymous
236
237
238////////////////////////////////////////////////////////////////
239//
240// Member functions of the nodes used for building the tree.
241//
242////////////////////////////////////////////////////////////////
243
Olli Etuahod2a67b92014-10-21 16:42:57 +0300244void TIntermTyped::setTypePreservePrecision(const TType &t)
245{
246 TPrecision precision = getPrecision();
247 mType = t;
248 ASSERT(mType.getBasicType() != EbtBool || precision == EbpUndefined);
249 mType.setPrecision(precision);
250}
251
Jamie Madillb1a85f42014-08-19 15:23:24 -0400252#define REPLACE_IF_IS(node, type, original, replacement) \
253 if (node == original) { \
254 node = static_cast<type *>(replacement); \
255 return true; \
256 }
257
258bool TIntermLoop::replaceChildNode(
259 TIntermNode *original, TIntermNode *replacement)
260{
261 REPLACE_IF_IS(mInit, TIntermNode, original, replacement);
262 REPLACE_IF_IS(mCond, TIntermTyped, original, replacement);
263 REPLACE_IF_IS(mExpr, TIntermTyped, original, replacement);
264 REPLACE_IF_IS(mBody, TIntermNode, original, replacement);
265 return false;
266}
267
Jamie Madillb1a85f42014-08-19 15:23:24 -0400268bool TIntermBranch::replaceChildNode(
269 TIntermNode *original, TIntermNode *replacement)
270{
271 REPLACE_IF_IS(mExpression, TIntermTyped, original, replacement);
272 return false;
273}
274
Jamie Madillb1a85f42014-08-19 15:23:24 -0400275bool TIntermBinary::replaceChildNode(
276 TIntermNode *original, TIntermNode *replacement)
277{
278 REPLACE_IF_IS(mLeft, TIntermTyped, original, replacement);
279 REPLACE_IF_IS(mRight, TIntermTyped, original, replacement);
280 return false;
281}
282
Jamie Madillb1a85f42014-08-19 15:23:24 -0400283bool TIntermUnary::replaceChildNode(
284 TIntermNode *original, TIntermNode *replacement)
285{
286 REPLACE_IF_IS(mOperand, TIntermTyped, original, replacement);
287 return false;
288}
289
Jamie Madillb1a85f42014-08-19 15:23:24 -0400290bool TIntermAggregate::replaceChildNode(
291 TIntermNode *original, TIntermNode *replacement)
292{
293 for (size_t ii = 0; ii < mSequence.size(); ++ii)
294 {
295 REPLACE_IF_IS(mSequence[ii], TIntermNode, original, replacement);
296 }
297 return false;
298}
299
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300300bool TIntermAggregate::replaceChildNodeWithMultiple(TIntermNode *original, TIntermSequence replacements)
301{
302 for (auto it = mSequence.begin(); it < mSequence.end(); ++it)
303 {
304 if (*it == original)
305 {
306 it = mSequence.erase(it);
Olli Etuaho8fee0ab2015-04-23 14:52:46 +0300307 mSequence.insert(it, replacements.begin(), replacements.end());
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300308 return true;
309 }
310 }
311 return false;
312}
313
Olli Etuahoa6f22092015-05-08 18:31:10 +0300314bool TIntermAggregate::insertChildNodes(TIntermSequence::size_type position, TIntermSequence insertions)
315{
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300316 if (position > mSequence.size())
Olli Etuahoa6f22092015-05-08 18:31:10 +0300317 {
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300318 return false;
Olli Etuahoa6f22092015-05-08 18:31:10 +0300319 }
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300320 auto it = mSequence.begin() + position;
321 mSequence.insert(it, insertions.begin(), insertions.end());
322 return true;
Olli Etuahoa6f22092015-05-08 18:31:10 +0300323}
324
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200325bool TIntermAggregate::areChildrenConstQualified()
326{
327 for (TIntermNode *&child : mSequence)
328 {
329 TIntermTyped *typed = child->getAsTyped();
330 if (typed && typed->getQualifier() != EvqConst)
331 {
332 return false;
333 }
334 }
335 return true;
336}
337
Olli Etuahod2a67b92014-10-21 16:42:57 +0300338void TIntermAggregate::setPrecisionFromChildren()
339{
Olli Etuahoa4aa4e32015-06-04 15:54:30 +0300340 mGotPrecisionFromChildren = true;
Olli Etuahod2a67b92014-10-21 16:42:57 +0300341 if (getBasicType() == EbtBool)
342 {
343 mType.setPrecision(EbpUndefined);
344 return;
345 }
346
347 TPrecision precision = EbpUndefined;
348 TIntermSequence::iterator childIter = mSequence.begin();
349 while (childIter != mSequence.end())
350 {
351 TIntermTyped *typed = (*childIter)->getAsTyped();
352 if (typed)
353 precision = GetHigherPrecision(typed->getPrecision(), precision);
354 ++childIter;
355 }
356 mType.setPrecision(precision);
357}
358
359void TIntermAggregate::setBuiltInFunctionPrecision()
360{
361 // All built-ins returning bool should be handled as ops, not functions.
362 ASSERT(getBasicType() != EbtBool);
363
364 TPrecision precision = EbpUndefined;
365 TIntermSequence::iterator childIter = mSequence.begin();
366 while (childIter != mSequence.end())
367 {
368 TIntermTyped *typed = (*childIter)->getAsTyped();
369 // ESSL spec section 8: texture functions get their precision from the sampler.
370 if (typed && IsSampler(typed->getBasicType()))
371 {
372 precision = typed->getPrecision();
373 break;
374 }
375 ++childIter;
376 }
377 // ESSL 3.0 spec section 8: textureSize always gets highp precision.
378 // All other functions that take a sampler are assumed to be texture functions.
Olli Etuaho59f9a642015-08-06 20:38:26 +0300379 if (mName.getString().find("textureSize") == 0)
Olli Etuahod2a67b92014-10-21 16:42:57 +0300380 mType.setPrecision(EbpHigh);
381 else
382 mType.setPrecision(precision);
383}
384
Jamie Madillb1a85f42014-08-19 15:23:24 -0400385bool TIntermSelection::replaceChildNode(
386 TIntermNode *original, TIntermNode *replacement)
387{
388 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
389 REPLACE_IF_IS(mTrueBlock, TIntermNode, original, replacement);
390 REPLACE_IF_IS(mFalseBlock, TIntermNode, original, replacement);
391 return false;
392}
393
Olli Etuahoa3a36662015-02-17 13:46:51 +0200394bool TIntermSwitch::replaceChildNode(
395 TIntermNode *original, TIntermNode *replacement)
396{
397 REPLACE_IF_IS(mInit, TIntermTyped, original, replacement);
398 REPLACE_IF_IS(mStatementList, TIntermAggregate, original, replacement);
399 return false;
400}
401
402bool TIntermCase::replaceChildNode(
403 TIntermNode *original, TIntermNode *replacement)
404{
405 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
406 return false;
407}
408
Olli Etuahod7a25242015-08-18 13:49:45 +0300409TIntermTyped::TIntermTyped(const TIntermTyped &node) : TIntermNode(), mType(node.mType)
410{
411 // Copy constructor is disallowed for TIntermNode in order to disallow it for subclasses that
412 // don't explicitly allow it, so normal TIntermNode constructor is used to construct the copy.
413 // We need to manually copy any fields of TIntermNode besides handling fields in TIntermTyped.
414 mLine = node.mLine;
415}
416
417TIntermConstantUnion::TIntermConstantUnion(const TIntermConstantUnion &node) : TIntermTyped(node)
418{
419 size_t arraySize = mType.getObjectSize();
420 mUnionArrayPointer = new TConstantUnion[arraySize];
421 for (size_t i = 0u; i < arraySize; ++i)
422 {
423 mUnionArrayPointer[i] = node.mUnionArrayPointer[i];
424 }
425}
426
427TIntermAggregate::TIntermAggregate(const TIntermAggregate &node)
428 : TIntermOperator(node),
429 mName(node.mName),
430 mUserDefined(node.mUserDefined),
431 mFunctionId(node.mFunctionId),
Olli Etuahod7a25242015-08-18 13:49:45 +0300432 mUseEmulatedFunction(node.mUseEmulatedFunction),
433 mGotPrecisionFromChildren(node.mGotPrecisionFromChildren)
434{
435 for (TIntermNode *child : node.mSequence)
436 {
437 TIntermTyped *typedChild = child->getAsTyped();
438 ASSERT(typedChild != nullptr);
439 TIntermTyped *childCopy = typedChild->deepCopy();
440 mSequence.push_back(childCopy);
441 }
442}
443
444TIntermBinary::TIntermBinary(const TIntermBinary &node)
445 : TIntermOperator(node), mAddIndexClamp(node.mAddIndexClamp)
446{
447 TIntermTyped *leftCopy = node.mLeft->deepCopy();
448 TIntermTyped *rightCopy = node.mRight->deepCopy();
449 ASSERT(leftCopy != nullptr && rightCopy != nullptr);
450 mLeft = leftCopy;
451 mRight = rightCopy;
452}
453
454TIntermUnary::TIntermUnary(const TIntermUnary &node)
455 : TIntermOperator(node), mUseEmulatedFunction(node.mUseEmulatedFunction)
456{
457 TIntermTyped *operandCopy = node.mOperand->deepCopy();
458 ASSERT(operandCopy != nullptr);
459 mOperand = operandCopy;
460}
461
462TIntermSelection::TIntermSelection(const TIntermSelection &node) : TIntermTyped(node)
463{
464 // Only supported for ternary nodes, not if statements.
465 TIntermTyped *trueTyped = node.mTrueBlock->getAsTyped();
466 TIntermTyped *falseTyped = node.mFalseBlock->getAsTyped();
467 ASSERT(trueTyped != nullptr);
468 ASSERT(falseTyped != nullptr);
469 TIntermTyped *conditionCopy = node.mCondition->deepCopy();
470 TIntermTyped *trueCopy = trueTyped->deepCopy();
471 TIntermTyped *falseCopy = falseTyped->deepCopy();
472 ASSERT(conditionCopy != nullptr && trueCopy != nullptr && falseCopy != nullptr);
473 mCondition = conditionCopy;
474 mTrueBlock = trueCopy;
475 mFalseBlock = falseCopy;
476}
477
Jamie Madillb1a85f42014-08-19 15:23:24 -0400478//
479// Say whether or not an operation node changes the value of a variable.
480//
481bool TIntermOperator::isAssignment() const
482{
483 switch (mOp)
484 {
485 case EOpPostIncrement:
486 case EOpPostDecrement:
487 case EOpPreIncrement:
488 case EOpPreDecrement:
489 case EOpAssign:
490 case EOpAddAssign:
491 case EOpSubAssign:
492 case EOpMulAssign:
493 case EOpVectorTimesMatrixAssign:
494 case EOpVectorTimesScalarAssign:
495 case EOpMatrixTimesScalarAssign:
496 case EOpMatrixTimesMatrixAssign:
497 case EOpDivAssign:
Olli Etuahoff805cc2015-02-13 10:59:34 +0200498 case EOpIModAssign:
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200499 case EOpBitShiftLeftAssign:
500 case EOpBitShiftRightAssign:
501 case EOpBitwiseAndAssign:
502 case EOpBitwiseXorAssign:
503 case EOpBitwiseOrAssign:
Jamie Madillb1a85f42014-08-19 15:23:24 -0400504 return true;
505 default:
506 return false;
507 }
508}
509
Olli Etuaho8f76bcc2015-06-02 13:54:20 +0300510bool TIntermOperator::isMultiplication() const
511{
512 switch (mOp)
513 {
514 case EOpMul:
515 case EOpMatrixTimesMatrix:
516 case EOpMatrixTimesVector:
517 case EOpMatrixTimesScalar:
518 case EOpVectorTimesMatrix:
519 case EOpVectorTimesScalar:
520 return true;
521 default:
522 return false;
523 }
524}
525
Jamie Madillb1a85f42014-08-19 15:23:24 -0400526//
527// returns true if the operator is for one of the constructors
528//
529bool TIntermOperator::isConstructor() const
530{
531 switch (mOp)
532 {
533 case EOpConstructVec2:
534 case EOpConstructVec3:
535 case EOpConstructVec4:
536 case EOpConstructMat2:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400537 case EOpConstructMat2x3:
538 case EOpConstructMat2x4:
539 case EOpConstructMat3x2:
Jamie Madillb1a85f42014-08-19 15:23:24 -0400540 case EOpConstructMat3:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400541 case EOpConstructMat3x4:
542 case EOpConstructMat4x2:
543 case EOpConstructMat4x3:
Jamie Madillb1a85f42014-08-19 15:23:24 -0400544 case EOpConstructMat4:
545 case EOpConstructFloat:
546 case EOpConstructIVec2:
547 case EOpConstructIVec3:
548 case EOpConstructIVec4:
549 case EOpConstructInt:
550 case EOpConstructUVec2:
551 case EOpConstructUVec3:
552 case EOpConstructUVec4:
553 case EOpConstructUInt:
554 case EOpConstructBVec2:
555 case EOpConstructBVec3:
556 case EOpConstructBVec4:
557 case EOpConstructBool:
558 case EOpConstructStruct:
559 return true;
560 default:
561 return false;
562 }
563}
564
565//
566// Make sure the type of a unary operator is appropriate for its
567// combination of operation and operand type.
568//
Olli Etuahof6c694b2015-03-26 14:50:53 +0200569void TIntermUnary::promote(const TType *funcReturnType)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400570{
571 switch (mOp)
572 {
Olli Etuahodca3e792015-03-26 13:24:04 +0200573 case EOpFloatBitsToInt:
574 case EOpFloatBitsToUint:
Olli Etuahoe8d2c072015-01-08 16:33:54 +0200575 case EOpIntBitsToFloat:
576 case EOpUintBitsToFloat:
Olli Etuahodca3e792015-03-26 13:24:04 +0200577 case EOpPackSnorm2x16:
578 case EOpPackUnorm2x16:
579 case EOpPackHalf2x16:
Olli Etuaho7700ff62015-01-15 12:16:29 +0200580 case EOpUnpackSnorm2x16:
581 case EOpUnpackUnorm2x16:
Olli Etuahodca3e792015-03-26 13:24:04 +0200582 mType.setPrecision(EbpHigh);
Arun Patole6b19d762015-02-19 09:40:39 +0530583 break;
Olli Etuahodca3e792015-03-26 13:24:04 +0200584 case EOpUnpackHalf2x16:
585 mType.setPrecision(EbpMedium);
586 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400587 default:
Olli Etuahodca3e792015-03-26 13:24:04 +0200588 setType(mOperand->getType());
Jamie Madillb1a85f42014-08-19 15:23:24 -0400589 }
590
Olli Etuahof6c694b2015-03-26 14:50:53 +0200591 if (funcReturnType != nullptr)
592 {
593 if (funcReturnType->getBasicType() == EbtBool)
594 {
595 // Bool types should not have precision.
596 setType(*funcReturnType);
597 }
598 else
599 {
600 // Precision of the node has been set based on the operand.
601 setTypePreservePrecision(*funcReturnType);
602 }
603 }
604
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200605 if (mOperand->getQualifier() == EvqConst)
606 mType.setQualifier(EvqConst);
607 else
608 mType.setQualifier(EvqTemporary);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400609}
610
611//
612// Establishes the type of the resultant operation, as well as
613// makes the operator the correct one for the operands.
614//
Olli Etuaho47fd36a2015-03-19 14:22:24 +0200615// For lots of operations it should already be established that the operand
616// combination is valid, but returns false if operator can't work on operands.
Jamie Madillb1a85f42014-08-19 15:23:24 -0400617//
618bool TIntermBinary::promote(TInfoSink &infoSink)
619{
Olli Etuahoe79904c2015-03-18 16:56:42 +0200620 ASSERT(mLeft->isArray() == mRight->isArray());
Jamie Madillb1a85f42014-08-19 15:23:24 -0400621
Jamie Madillb1a85f42014-08-19 15:23:24 -0400622 //
623 // Base assumption: just make the type the same as the left
624 // operand. Then only deviations from this need be coded.
625 //
626 setType(mLeft->getType());
627
628 // The result gets promoted to the highest precision.
629 TPrecision higherPrecision = GetHigherPrecision(
630 mLeft->getPrecision(), mRight->getPrecision());
631 getTypePointer()->setPrecision(higherPrecision);
632
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200633 TQualifier resultQualifier = EvqConst;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400634 // Binary operations results in temporary variables unless both
635 // operands are const.
636 if (mLeft->getQualifier() != EvqConst || mRight->getQualifier() != EvqConst)
637 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200638 resultQualifier = EvqTemporary;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400639 getTypePointer()->setQualifier(EvqTemporary);
640 }
641
642 const int nominalSize =
643 std::max(mLeft->getNominalSize(), mRight->getNominalSize());
644
645 //
646 // All scalars or structs. Code after this test assumes this case is removed!
647 //
648 if (nominalSize == 1)
649 {
650 switch (mOp)
651 {
652 //
653 // Promote to conditional
654 //
655 case EOpEqual:
656 case EOpNotEqual:
657 case EOpLessThan:
658 case EOpGreaterThan:
659 case EOpLessThanEqual:
660 case EOpGreaterThanEqual:
661 setType(TType(EbtBool, EbpUndefined));
662 break;
663
664 //
665 // And and Or operate on conditionals
666 //
667 case EOpLogicalAnd:
Olli Etuaho47fd36a2015-03-19 14:22:24 +0200668 case EOpLogicalXor:
Jamie Madillb1a85f42014-08-19 15:23:24 -0400669 case EOpLogicalOr:
Olli Etuaho47fd36a2015-03-19 14:22:24 +0200670 ASSERT(mLeft->getBasicType() == EbtBool && mRight->getBasicType() == EbtBool);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400671 setType(TType(EbtBool, EbpUndefined));
672 break;
673
674 default:
675 break;
676 }
677 return true;
678 }
679
680 // If we reach here, at least one of the operands is vector or matrix.
681 // The other operand could be a scalar, vector, or matrix.
682 // Can these two operands be combined?
683 //
684 TBasicType basicType = mLeft->getBasicType();
685 switch (mOp)
686 {
687 case EOpMul:
688 if (!mLeft->isMatrix() && mRight->isMatrix())
689 {
690 if (mLeft->isVector())
691 {
692 mOp = EOpVectorTimesMatrix;
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200693 setType(TType(basicType, higherPrecision, resultQualifier,
Minmin Gong794e0002015-04-07 18:31:54 -0700694 static_cast<unsigned char>(mRight->getCols()), 1));
Jamie Madillb1a85f42014-08-19 15:23:24 -0400695 }
696 else
697 {
698 mOp = EOpMatrixTimesScalar;
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200699 setType(TType(basicType, higherPrecision, resultQualifier,
700 static_cast<unsigned char>(mRight->getCols()),
701 static_cast<unsigned char>(mRight->getRows())));
Jamie Madillb1a85f42014-08-19 15:23:24 -0400702 }
703 }
704 else if (mLeft->isMatrix() && !mRight->isMatrix())
705 {
706 if (mRight->isVector())
707 {
708 mOp = EOpMatrixTimesVector;
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200709 setType(TType(basicType, higherPrecision, resultQualifier,
Minmin Gong794e0002015-04-07 18:31:54 -0700710 static_cast<unsigned char>(mLeft->getRows()), 1));
Jamie Madillb1a85f42014-08-19 15:23:24 -0400711 }
712 else
713 {
714 mOp = EOpMatrixTimesScalar;
715 }
716 }
717 else if (mLeft->isMatrix() && mRight->isMatrix())
718 {
719 mOp = EOpMatrixTimesMatrix;
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200720 setType(TType(basicType, higherPrecision, resultQualifier,
721 static_cast<unsigned char>(mRight->getCols()),
722 static_cast<unsigned char>(mLeft->getRows())));
Jamie Madillb1a85f42014-08-19 15:23:24 -0400723 }
724 else if (!mLeft->isMatrix() && !mRight->isMatrix())
725 {
726 if (mLeft->isVector() && mRight->isVector())
727 {
728 // leave as component product
729 }
730 else if (mLeft->isVector() || mRight->isVector())
731 {
732 mOp = EOpVectorTimesScalar;
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200733 setType(TType(basicType, higherPrecision, resultQualifier,
Minmin Gong794e0002015-04-07 18:31:54 -0700734 static_cast<unsigned char>(nominalSize), 1));
Jamie Madillb1a85f42014-08-19 15:23:24 -0400735 }
736 }
737 else
738 {
739 infoSink.info.message(EPrefixInternalError, getLine(),
740 "Missing elses");
741 return false;
742 }
743
744 if (!ValidateMultiplication(mOp, mLeft->getType(), mRight->getType()))
745 {
746 return false;
747 }
748 break;
749
750 case EOpMulAssign:
751 if (!mLeft->isMatrix() && mRight->isMatrix())
752 {
753 if (mLeft->isVector())
754 {
755 mOp = EOpVectorTimesMatrixAssign;
756 }
757 else
758 {
759 return false;
760 }
761 }
762 else if (mLeft->isMatrix() && !mRight->isMatrix())
763 {
764 if (mRight->isVector())
765 {
766 return false;
767 }
768 else
769 {
770 mOp = EOpMatrixTimesScalarAssign;
771 }
772 }
773 else if (mLeft->isMatrix() && mRight->isMatrix())
774 {
775 mOp = EOpMatrixTimesMatrixAssign;
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200776 setType(TType(basicType, higherPrecision, resultQualifier,
777 static_cast<unsigned char>(mRight->getCols()),
778 static_cast<unsigned char>(mLeft->getRows())));
Jamie Madillb1a85f42014-08-19 15:23:24 -0400779 }
780 else if (!mLeft->isMatrix() && !mRight->isMatrix())
781 {
782 if (mLeft->isVector() && mRight->isVector())
783 {
784 // leave as component product
785 }
786 else if (mLeft->isVector() || mRight->isVector())
787 {
788 if (!mLeft->isVector())
789 return false;
790 mOp = EOpVectorTimesScalarAssign;
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200791 setType(TType(basicType, higherPrecision, resultQualifier,
Minmin Gong794e0002015-04-07 18:31:54 -0700792 static_cast<unsigned char>(mLeft->getNominalSize()), 1));
Jamie Madillb1a85f42014-08-19 15:23:24 -0400793 }
794 }
795 else
796 {
797 infoSink.info.message(EPrefixInternalError, getLine(),
798 "Missing elses");
799 return false;
800 }
801
802 if (!ValidateMultiplication(mOp, mLeft->getType(), mRight->getType()))
803 {
804 return false;
805 }
806 break;
807
808 case EOpAssign:
809 case EOpInitialize:
Olli Etuaho47fd36a2015-03-19 14:22:24 +0200810 // No more additional checks are needed.
811 ASSERT((mLeft->getNominalSize() == mRight->getNominalSize()) &&
812 (mLeft->getSecondarySize() == mRight->getSecondarySize()));
813 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400814 case EOpAdd:
815 case EOpSub:
816 case EOpDiv:
Olli Etuahoff805cc2015-02-13 10:59:34 +0200817 case EOpIMod:
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200818 case EOpBitShiftLeft:
819 case EOpBitShiftRight:
820 case EOpBitwiseAnd:
821 case EOpBitwiseXor:
822 case EOpBitwiseOr:
Jamie Madillb1a85f42014-08-19 15:23:24 -0400823 case EOpAddAssign:
824 case EOpSubAssign:
825 case EOpDivAssign:
Olli Etuahoff805cc2015-02-13 10:59:34 +0200826 case EOpIModAssign:
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200827 case EOpBitShiftLeftAssign:
828 case EOpBitShiftRightAssign:
829 case EOpBitwiseAndAssign:
830 case EOpBitwiseXorAssign:
831 case EOpBitwiseOrAssign:
Jamie Madillb1a85f42014-08-19 15:23:24 -0400832 if ((mLeft->isMatrix() && mRight->isVector()) ||
833 (mLeft->isVector() && mRight->isMatrix()))
834 {
835 return false;
836 }
837
838 // Are the sizes compatible?
839 if (mLeft->getNominalSize() != mRight->getNominalSize() ||
840 mLeft->getSecondarySize() != mRight->getSecondarySize())
841 {
Olli Etuaho6c850472014-12-02 16:23:17 +0200842 // If the nominal sizes of operands do not match:
843 // One of them must be a scalar.
Jamie Madillb1a85f42014-08-19 15:23:24 -0400844 if (!mLeft->isScalar() && !mRight->isScalar())
845 return false;
846
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200847 // In the case of compound assignment other than multiply-assign,
848 // the right side needs to be a scalar. Otherwise a vector/matrix
849 // would be assigned to a scalar. A scalar can't be shifted by a
850 // vector either.
Olli Etuaho6c850472014-12-02 16:23:17 +0200851 if (!mRight->isScalar() &&
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200852 (isAssignment() ||
853 mOp == EOpBitShiftLeft ||
854 mOp == EOpBitShiftRight))
Olli Etuaho6c850472014-12-02 16:23:17 +0200855 return false;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400856 }
857
858 {
859 const int secondarySize = std::max(
860 mLeft->getSecondarySize(), mRight->getSecondarySize());
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200861 setType(TType(basicType, higherPrecision, resultQualifier,
862 static_cast<unsigned char>(nominalSize),
863 static_cast<unsigned char>(secondarySize)));
Olli Etuahoe79904c2015-03-18 16:56:42 +0200864 if (mLeft->isArray())
865 {
866 ASSERT(mLeft->getArraySize() == mRight->getArraySize());
867 mType.setArraySize(mLeft->getArraySize());
868 }
Jamie Madillb1a85f42014-08-19 15:23:24 -0400869 }
870 break;
871
872 case EOpEqual:
873 case EOpNotEqual:
874 case EOpLessThan:
875 case EOpGreaterThan:
876 case EOpLessThanEqual:
877 case EOpGreaterThanEqual:
Olli Etuaho47fd36a2015-03-19 14:22:24 +0200878 ASSERT((mLeft->getNominalSize() == mRight->getNominalSize()) &&
879 (mLeft->getSecondarySize() == mRight->getSecondarySize()));
Jamie Madillb1a85f42014-08-19 15:23:24 -0400880 setType(TType(EbtBool, EbpUndefined));
881 break;
882
883 default:
884 return false;
885 }
886 return true;
887}
888
Olli Etuaho2c4b7462015-06-08 11:30:31 +0300889TIntermTyped *TIntermBinary::fold(TInfoSink &infoSink)
890{
891 TIntermConstantUnion *leftConstant = mLeft->getAsConstantUnion();
892 TIntermConstantUnion *rightConstant = mRight->getAsConstantUnion();
893 if (leftConstant == nullptr || rightConstant == nullptr)
894 {
895 return nullptr;
896 }
897 TConstantUnion *constArray = leftConstant->foldBinary(mOp, rightConstant, infoSink);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200898
899 // Nodes may be constant folded without being qualified as constant.
900 TQualifier resultQualifier = EvqConst;
901 if (mLeft->getQualifier() != EvqConst || mRight->getQualifier() != EvqConst)
902 {
903 resultQualifier = EvqTemporary;
904 }
905 return CreateFoldedNode(constArray, this, resultQualifier);
Olli Etuaho2c4b7462015-06-08 11:30:31 +0300906}
907
Olli Etuaho95310b02015-06-02 17:43:38 +0300908TIntermTyped *TIntermUnary::fold(TInfoSink &infoSink)
909{
910 TIntermConstantUnion *operandConstant = mOperand->getAsConstantUnion();
911 if (operandConstant == nullptr)
912 {
913 return nullptr;
914 }
Arun Patoleab2b9a22015-07-06 18:27:56 +0530915
916 TConstantUnion *constArray = nullptr;
917 switch (mOp)
918 {
919 case EOpAny:
920 case EOpAll:
921 case EOpLength:
922 case EOpTranspose:
923 case EOpDeterminant:
924 case EOpInverse:
925 case EOpPackSnorm2x16:
926 case EOpUnpackSnorm2x16:
927 case EOpPackUnorm2x16:
928 case EOpUnpackUnorm2x16:
929 case EOpPackHalf2x16:
930 case EOpUnpackHalf2x16:
931 constArray = operandConstant->foldUnaryWithDifferentReturnType(mOp, infoSink);
932 break;
933 default:
934 constArray = operandConstant->foldUnaryWithSameReturnType(mOp, infoSink);
935 break;
936 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200937
938 // Nodes may be constant folded without being qualified as constant.
939 TQualifier resultQualifier = mOperand->getQualifier() == EvqConst ? EvqConst : EvqTemporary;
940 return CreateFoldedNode(constArray, this, resultQualifier);
Olli Etuahob43846e2015-06-02 18:18:57 +0300941}
942
943TIntermTyped *TIntermAggregate::fold(TInfoSink &infoSink)
944{
945 // Make sure that all params are constant before actual constant folding.
946 for (auto *param : *getSequence())
Olli Etuaho95310b02015-06-02 17:43:38 +0300947 {
Olli Etuahob43846e2015-06-02 18:18:57 +0300948 if (param->getAsConstantUnion() == nullptr)
949 {
950 return nullptr;
951 }
Olli Etuaho95310b02015-06-02 17:43:38 +0300952 }
Olli Etuahob43846e2015-06-02 18:18:57 +0300953 TConstantUnion *constArray = TIntermConstantUnion::FoldAggregateBuiltIn(this, infoSink);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200954
955 // Nodes may be constant folded without being qualified as constant.
956 TQualifier resultQualifier = areChildrenConstQualified() ? EvqConst : EvqTemporary;
957 return CreateFoldedNode(constArray, this, resultQualifier);
Olli Etuaho95310b02015-06-02 17:43:38 +0300958}
959
Jamie Madillb1a85f42014-08-19 15:23:24 -0400960//
961// The fold functions see if an operation on a constant can be done in place,
962// without generating run-time code.
963//
Olli Etuaho2c4b7462015-06-08 11:30:31 +0300964// Returns the constant value to keep using or nullptr.
Jamie Madillb1a85f42014-08-19 15:23:24 -0400965//
Olli Etuaho2c4b7462015-06-08 11:30:31 +0300966TConstantUnion *TIntermConstantUnion::foldBinary(TOperator op, TIntermConstantUnion *rightNode, TInfoSink &infoSink)
967{
968 TConstantUnion *leftArray = getUnionArrayPointer();
969 TConstantUnion *rightArray = rightNode->getUnionArrayPointer();
970
971 if (!leftArray)
972 return nullptr;
973 if (!rightArray)
974 return nullptr;
975
976 size_t objectSize = getType().getObjectSize();
977
978 // for a case like float f = vec4(2, 3, 4, 5) + 1.2;
979 if (rightNode->getType().getObjectSize() == 1 && objectSize > 1)
980 {
981 rightArray = Vectorize(*rightNode->getUnionArrayPointer(), objectSize);
982 }
983 else if (rightNode->getType().getObjectSize() > 1 && objectSize == 1)
984 {
985 // for a case like float f = 1.2 + vec4(2, 3, 4, 5);
986 leftArray = Vectorize(*getUnionArrayPointer(), rightNode->getType().getObjectSize());
987 objectSize = rightNode->getType().getObjectSize();
988 }
989
990 TConstantUnion *resultArray = nullptr;
991
992 switch(op)
993 {
994 case EOpAdd:
995 resultArray = new TConstantUnion[objectSize];
996 for (size_t i = 0; i < objectSize; i++)
997 resultArray[i] = leftArray[i] + rightArray[i];
998 break;
999 case EOpSub:
1000 resultArray = new TConstantUnion[objectSize];
1001 for (size_t i = 0; i < objectSize; i++)
1002 resultArray[i] = leftArray[i] - rightArray[i];
1003 break;
1004
1005 case EOpMul:
1006 case EOpVectorTimesScalar:
1007 case EOpMatrixTimesScalar:
1008 resultArray = new TConstantUnion[objectSize];
1009 for (size_t i = 0; i < objectSize; i++)
1010 resultArray[i] = leftArray[i] * rightArray[i];
1011 break;
1012
1013 case EOpMatrixTimesMatrix:
1014 {
1015 if (getType().getBasicType() != EbtFloat ||
1016 rightNode->getBasicType() != EbtFloat)
1017 {
1018 infoSink.info.message(
1019 EPrefixInternalError, getLine(),
1020 "Constant Folding cannot be done for matrix multiply");
1021 return nullptr;
1022 }
1023
1024 const int leftCols = getCols();
1025 const int leftRows = getRows();
1026 const int rightCols = rightNode->getType().getCols();
1027 const int rightRows = rightNode->getType().getRows();
1028 const int resultCols = rightCols;
1029 const int resultRows = leftRows;
1030
1031 resultArray = new TConstantUnion[resultCols * resultRows];
1032 for (int row = 0; row < resultRows; row++)
1033 {
1034 for (int column = 0; column < resultCols; column++)
1035 {
1036 resultArray[resultRows * column + row].setFConst(0.0f);
1037 for (int i = 0; i < leftCols; i++)
1038 {
1039 resultArray[resultRows * column + row].setFConst(
1040 resultArray[resultRows * column + row].getFConst() +
1041 leftArray[i * leftRows + row].getFConst() *
1042 rightArray[column * rightRows + i].getFConst());
1043 }
1044 }
1045 }
1046 }
1047 break;
1048
1049 case EOpDiv:
1050 case EOpIMod:
1051 {
1052 resultArray = new TConstantUnion[objectSize];
1053 for (size_t i = 0; i < objectSize; i++)
1054 {
1055 switch (getType().getBasicType())
1056 {
1057 case EbtFloat:
1058 if (rightArray[i] == 0.0f)
1059 {
1060 infoSink.info.message(EPrefixWarning, getLine(),
1061 "Divide by zero error during constant folding");
1062 resultArray[i].setFConst(leftArray[i].getFConst() < 0 ? -FLT_MAX : FLT_MAX);
1063 }
1064 else
1065 {
1066 ASSERT(op == EOpDiv);
1067 resultArray[i].setFConst(leftArray[i].getFConst() / rightArray[i].getFConst());
1068 }
1069 break;
1070
1071 case EbtInt:
1072 if (rightArray[i] == 0)
1073 {
1074 infoSink.info.message(EPrefixWarning, getLine(),
1075 "Divide by zero error during constant folding");
1076 resultArray[i].setIConst(INT_MAX);
1077 }
1078 else
1079 {
1080 if (op == EOpDiv)
1081 {
1082 resultArray[i].setIConst(leftArray[i].getIConst() / rightArray[i].getIConst());
1083 }
1084 else
1085 {
1086 ASSERT(op == EOpIMod);
1087 resultArray[i].setIConst(leftArray[i].getIConst() % rightArray[i].getIConst());
1088 }
1089 }
1090 break;
1091
1092 case EbtUInt:
1093 if (rightArray[i] == 0)
1094 {
1095 infoSink.info.message(EPrefixWarning, getLine(),
1096 "Divide by zero error during constant folding");
1097 resultArray[i].setUConst(UINT_MAX);
1098 }
1099 else
1100 {
1101 if (op == EOpDiv)
1102 {
1103 resultArray[i].setUConst(leftArray[i].getUConst() / rightArray[i].getUConst());
1104 }
1105 else
1106 {
1107 ASSERT(op == EOpIMod);
1108 resultArray[i].setUConst(leftArray[i].getUConst() % rightArray[i].getUConst());
1109 }
1110 }
1111 break;
1112
1113 default:
1114 infoSink.info.message(EPrefixInternalError, getLine(),
1115 "Constant folding cannot be done for \"/\"");
1116 return nullptr;
1117 }
1118 }
1119 }
1120 break;
1121
1122 case EOpMatrixTimesVector:
1123 {
1124 if (rightNode->getBasicType() != EbtFloat)
1125 {
1126 infoSink.info.message(EPrefixInternalError, getLine(),
1127 "Constant Folding cannot be done for matrix times vector");
1128 return nullptr;
1129 }
1130
1131 const int matrixCols = getCols();
1132 const int matrixRows = getRows();
1133
1134 resultArray = new TConstantUnion[matrixRows];
1135
1136 for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++)
1137 {
1138 resultArray[matrixRow].setFConst(0.0f);
1139 for (int col = 0; col < matrixCols; col++)
1140 {
1141 resultArray[matrixRow].setFConst(resultArray[matrixRow].getFConst() +
1142 leftArray[col * matrixRows + matrixRow].getFConst() *
1143 rightArray[col].getFConst());
1144 }
1145 }
1146 }
1147 break;
1148
1149 case EOpVectorTimesMatrix:
1150 {
1151 if (getType().getBasicType() != EbtFloat)
1152 {
1153 infoSink.info.message(EPrefixInternalError, getLine(),
1154 "Constant Folding cannot be done for vector times matrix");
1155 return nullptr;
1156 }
1157
1158 const int matrixCols = rightNode->getType().getCols();
1159 const int matrixRows = rightNode->getType().getRows();
1160
1161 resultArray = new TConstantUnion[matrixCols];
1162
1163 for (int matrixCol = 0; matrixCol < matrixCols; matrixCol++)
1164 {
1165 resultArray[matrixCol].setFConst(0.0f);
1166 for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++)
1167 {
1168 resultArray[matrixCol].setFConst(resultArray[matrixCol].getFConst() +
1169 leftArray[matrixRow].getFConst() *
1170 rightArray[matrixCol * matrixRows + matrixRow].getFConst());
1171 }
1172 }
1173 }
1174 break;
1175
1176 case EOpLogicalAnd:
1177 {
1178 resultArray = new TConstantUnion[objectSize];
1179 for (size_t i = 0; i < objectSize; i++)
1180 {
1181 resultArray[i] = leftArray[i] && rightArray[i];
1182 }
1183 }
1184 break;
1185
1186 case EOpLogicalOr:
1187 {
1188 resultArray = new TConstantUnion[objectSize];
1189 for (size_t i = 0; i < objectSize; i++)
1190 {
1191 resultArray[i] = leftArray[i] || rightArray[i];
1192 }
1193 }
1194 break;
1195
1196 case EOpLogicalXor:
1197 {
1198 resultArray = new TConstantUnion[objectSize];
1199 for (size_t i = 0; i < objectSize; i++)
1200 {
1201 switch (getType().getBasicType())
1202 {
1203 case EbtBool:
1204 resultArray[i].setBConst(leftArray[i] != rightArray[i]);
1205 break;
1206 default:
1207 UNREACHABLE();
1208 break;
1209 }
1210 }
1211 }
1212 break;
1213
1214 case EOpBitwiseAnd:
1215 resultArray = new TConstantUnion[objectSize];
1216 for (size_t i = 0; i < objectSize; i++)
1217 resultArray[i] = leftArray[i] & rightArray[i];
1218 break;
1219 case EOpBitwiseXor:
1220 resultArray = new TConstantUnion[objectSize];
1221 for (size_t i = 0; i < objectSize; i++)
1222 resultArray[i] = leftArray[i] ^ rightArray[i];
1223 break;
1224 case EOpBitwiseOr:
1225 resultArray = new TConstantUnion[objectSize];
1226 for (size_t i = 0; i < objectSize; i++)
1227 resultArray[i] = leftArray[i] | rightArray[i];
1228 break;
1229 case EOpBitShiftLeft:
1230 resultArray = new TConstantUnion[objectSize];
1231 for (size_t i = 0; i < objectSize; i++)
1232 resultArray[i] = leftArray[i] << rightArray[i];
1233 break;
1234 case EOpBitShiftRight:
1235 resultArray = new TConstantUnion[objectSize];
1236 for (size_t i = 0; i < objectSize; i++)
1237 resultArray[i] = leftArray[i] >> rightArray[i];
1238 break;
1239
1240 case EOpLessThan:
1241 ASSERT(objectSize == 1);
1242 resultArray = new TConstantUnion[1];
1243 resultArray->setBConst(*leftArray < *rightArray);
1244 break;
1245
1246 case EOpGreaterThan:
1247 ASSERT(objectSize == 1);
1248 resultArray = new TConstantUnion[1];
1249 resultArray->setBConst(*leftArray > *rightArray);
1250 break;
1251
1252 case EOpLessThanEqual:
1253 ASSERT(objectSize == 1);
1254 resultArray = new TConstantUnion[1];
1255 resultArray->setBConst(!(*leftArray > *rightArray));
1256 break;
1257
1258 case EOpGreaterThanEqual:
1259 ASSERT(objectSize == 1);
1260 resultArray = new TConstantUnion[1];
1261 resultArray->setBConst(!(*leftArray < *rightArray));
1262 break;
1263
1264 case EOpEqual:
1265 case EOpNotEqual:
1266 {
1267 resultArray = new TConstantUnion[1];
1268 bool equal = true;
1269 if (getType().getBasicType() == EbtStruct)
1270 {
1271 equal = CompareStructure(getType(), rightArray, leftArray);
1272 }
1273 else
1274 {
1275 for (size_t i = 0; i < objectSize; i++)
1276 {
1277 if (leftArray[i] != rightArray[i])
1278 {
1279 equal = false;
1280 break; // break out of for loop
1281 }
1282 }
1283 }
1284 if (op == EOpEqual)
1285 {
1286 resultArray->setBConst(equal);
1287 }
1288 else
1289 {
1290 resultArray->setBConst(!equal);
1291 }
1292 }
1293 break;
1294
1295 default:
1296 infoSink.info.message(
1297 EPrefixInternalError, getLine(),
1298 "Invalid operator for constant folding");
1299 return nullptr;
1300 }
1301 return resultArray;
1302}
1303
1304//
1305// The fold functions see if an operation on a constant can be done in place,
1306// without generating run-time code.
1307//
Olli Etuaho95310b02015-06-02 17:43:38 +03001308// Returns the constant value to keep using or nullptr.
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001309//
Arun Patoleab2b9a22015-07-06 18:27:56 +05301310TConstantUnion *TIntermConstantUnion::foldUnaryWithDifferentReturnType(TOperator op, TInfoSink &infoSink)
Jamie Madillb1a85f42014-08-19 15:23:24 -04001311{
Arun Patoleab2b9a22015-07-06 18:27:56 +05301312 //
1313 // Do operations where the return type has a different number of components compared to the operand type.
1314 //
Jamie Madillb1a85f42014-08-19 15:23:24 -04001315
Arun Patoleab2b9a22015-07-06 18:27:56 +05301316 TConstantUnion *operandArray = getUnionArrayPointer();
1317 if (!operandArray)
1318 return nullptr;
1319
1320 size_t objectSize = getType().getObjectSize();
1321 TConstantUnion *resultArray = nullptr;
1322 switch (op)
1323 {
1324 case EOpAny:
1325 if (getType().getBasicType() == EbtBool)
1326 {
1327 resultArray = new TConstantUnion();
1328 resultArray->setBConst(false);
1329 for (size_t i = 0; i < objectSize; i++)
1330 {
1331 if (operandArray[i].getBConst())
1332 {
1333 resultArray->setBConst(true);
1334 break;
1335 }
1336 }
1337 break;
1338 }
1339 else
1340 {
1341 infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
1342 return nullptr;
1343 }
1344
1345 case EOpAll:
1346 if (getType().getBasicType() == EbtBool)
1347 {
1348 resultArray = new TConstantUnion();
1349 resultArray->setBConst(true);
1350 for (size_t i = 0; i < objectSize; i++)
1351 {
1352 if (!operandArray[i].getBConst())
1353 {
1354 resultArray->setBConst(false);
1355 break;
1356 }
1357 }
1358 break;
1359 }
1360 else
1361 {
1362 infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
1363 return nullptr;
1364 }
1365
1366 case EOpLength:
1367 if (getType().getBasicType() == EbtFloat)
1368 {
1369 resultArray = new TConstantUnion();
1370 resultArray->setFConst(VectorLength(operandArray, objectSize));
1371 break;
1372 }
1373 else
1374 {
1375 infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
1376 return nullptr;
1377 }
1378
1379 case EOpTranspose:
1380 if (getType().getBasicType() == EbtFloat)
1381 {
1382 resultArray = new TConstantUnion[objectSize];
1383 angle::Matrix<float> result =
1384 GetMatrix(operandArray, getType().getNominalSize(), getType().getSecondarySize()).transpose();
1385 SetUnionArrayFromMatrix(result, resultArray);
1386 break;
1387 }
1388 else
1389 {
1390 infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
1391 return nullptr;
1392 }
1393
1394 case EOpDeterminant:
1395 if (getType().getBasicType() == EbtFloat)
1396 {
1397 unsigned int size = getType().getNominalSize();
1398 ASSERT(size >= 2 && size <= 4);
1399 resultArray = new TConstantUnion();
1400 resultArray->setFConst(GetMatrix(operandArray, size).determinant());
1401 break;
1402 }
1403 else
1404 {
1405 infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
1406 return nullptr;
1407 }
1408
1409 case EOpInverse:
1410 if (getType().getBasicType() == EbtFloat)
1411 {
1412 unsigned int size = getType().getNominalSize();
1413 ASSERT(size >= 2 && size <= 4);
1414 resultArray = new TConstantUnion[objectSize];
1415 angle::Matrix<float> result = GetMatrix(operandArray, size).inverse();
1416 SetUnionArrayFromMatrix(result, resultArray);
1417 break;
1418 }
1419 else
1420 {
1421 infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
1422 return nullptr;
1423 }
1424
1425 case EOpPackSnorm2x16:
1426 if (getType().getBasicType() == EbtFloat)
1427 {
1428 ASSERT(getType().getNominalSize() == 2);
1429 resultArray = new TConstantUnion();
1430 resultArray->setUConst(gl::packSnorm2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
1431 break;
1432 }
1433 else
1434 {
1435 infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
1436 return nullptr;
1437 }
1438
1439 case EOpUnpackSnorm2x16:
1440 if (getType().getBasicType() == EbtUInt)
1441 {
1442 resultArray = new TConstantUnion[2];
1443 float f1, f2;
1444 gl::unpackSnorm2x16(operandArray[0].getUConst(), &f1, &f2);
1445 resultArray[0].setFConst(f1);
1446 resultArray[1].setFConst(f2);
1447 break;
1448 }
1449 else
1450 {
1451 infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
1452 return nullptr;
1453 }
1454
1455 case EOpPackUnorm2x16:
1456 if (getType().getBasicType() == EbtFloat)
1457 {
1458 ASSERT(getType().getNominalSize() == 2);
1459 resultArray = new TConstantUnion();
1460 resultArray->setUConst(gl::packUnorm2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
1461 break;
1462 }
1463 else
1464 {
1465 infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
1466 return nullptr;
1467 }
1468
1469 case EOpUnpackUnorm2x16:
1470 if (getType().getBasicType() == EbtUInt)
1471 {
1472 resultArray = new TConstantUnion[2];
1473 float f1, f2;
1474 gl::unpackUnorm2x16(operandArray[0].getUConst(), &f1, &f2);
1475 resultArray[0].setFConst(f1);
1476 resultArray[1].setFConst(f2);
1477 break;
1478 }
1479 else
1480 {
1481 infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
1482 return nullptr;
1483 }
1484
1485 case EOpPackHalf2x16:
1486 if (getType().getBasicType() == EbtFloat)
1487 {
1488 ASSERT(getType().getNominalSize() == 2);
1489 resultArray = new TConstantUnion();
1490 resultArray->setUConst(gl::packHalf2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
1491 break;
1492 }
1493 else
1494 {
1495 infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
1496 return nullptr;
1497 }
1498
1499 case EOpUnpackHalf2x16:
1500 if (getType().getBasicType() == EbtUInt)
1501 {
1502 resultArray = new TConstantUnion[2];
1503 float f1, f2;
1504 gl::unpackHalf2x16(operandArray[0].getUConst(), &f1, &f2);
1505 resultArray[0].setFConst(f1);
1506 resultArray[1].setFConst(f2);
1507 break;
1508 }
1509 else
1510 {
1511 infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
1512 return nullptr;
1513 }
1514 break;
1515
1516 default:
1517 break;
1518 }
1519
1520 return resultArray;
1521}
1522
1523TConstantUnion *TIntermConstantUnion::foldUnaryWithSameReturnType(TOperator op, TInfoSink &infoSink)
1524{
1525 //
1526 // Do unary operations where the return type is the same as operand type.
1527 //
1528
1529 TConstantUnion *operandArray = getUnionArrayPointer();
Olli Etuaho95310b02015-06-02 17:43:38 +03001530 if (!operandArray)
Arun Patolefddc2112015-04-22 13:28:10 +05301531 return nullptr;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001532
1533 size_t objectSize = getType().getObjectSize();
1534
Arun Patoleab2b9a22015-07-06 18:27:56 +05301535 TConstantUnion *resultArray = new TConstantUnion[objectSize];
1536 for (size_t i = 0; i < objectSize; i++)
Arun Patole9d0b1f92015-05-20 14:27:17 +05301537 {
Arun Patoleab2b9a22015-07-06 18:27:56 +05301538 switch(op)
Arun Patole9d0b1f92015-05-20 14:27:17 +05301539 {
Arun Patoleab2b9a22015-07-06 18:27:56 +05301540 case EOpNegative:
1541 switch (getType().getBasicType())
Arun Patole9d0b1f92015-05-20 14:27:17 +05301542 {
Arun Patoleab2b9a22015-07-06 18:27:56 +05301543 case EbtFloat:
1544 resultArray[i].setFConst(-operandArray[i].getFConst());
Arun Patole1155ddd2015-06-05 18:04:36 +05301545 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301546 case EbtInt:
1547 resultArray[i].setIConst(-operandArray[i].getIConst());
Arun Patole1155ddd2015-06-05 18:04:36 +05301548 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301549 case EbtUInt:
1550 resultArray[i].setUConst(static_cast<unsigned int>(
1551 -static_cast<int>(operandArray[i].getUConst())));
Arun Patole1155ddd2015-06-05 18:04:36 +05301552 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301553 default:
1554 infoSink.info.message(
1555 EPrefixInternalError, getLine(),
1556 "Unary operation not folded into constant");
Arun Patolecdfa8f52015-06-30 17:48:25 +05301557 return nullptr;
1558 }
1559 break;
1560
Arun Patoleab2b9a22015-07-06 18:27:56 +05301561 case EOpPositive:
1562 switch (getType().getBasicType())
1563 {
1564 case EbtFloat:
1565 resultArray[i].setFConst(operandArray[i].getFConst());
1566 break;
1567 case EbtInt:
1568 resultArray[i].setIConst(operandArray[i].getIConst());
1569 break;
1570 case EbtUInt:
1571 resultArray[i].setUConst(static_cast<unsigned int>(
1572 static_cast<int>(operandArray[i].getUConst())));
1573 break;
1574 default:
1575 infoSink.info.message(
1576 EPrefixInternalError, getLine(),
1577 "Unary operation not folded into constant");
1578 return nullptr;
1579 }
1580 break;
1581
1582 case EOpLogicalNot:
1583 // this code is written for possible future use,
1584 // will not get executed currently
1585 switch (getType().getBasicType())
1586 {
1587 case EbtBool:
1588 resultArray[i].setBConst(!operandArray[i].getBConst());
1589 break;
1590 default:
1591 infoSink.info.message(
1592 EPrefixInternalError, getLine(),
1593 "Unary operation not folded into constant");
1594 return nullptr;
1595 }
1596 break;
1597
1598 case EOpBitwiseNot:
1599 switch (getType().getBasicType())
1600 {
1601 case EbtInt:
1602 resultArray[i].setIConst(~operandArray[i].getIConst());
1603 break;
1604 case EbtUInt:
1605 resultArray[i].setUConst(~operandArray[i].getUConst());
1606 break;
1607 default:
1608 infoSink.info.message(
1609 EPrefixInternalError, getLine(),
1610 "Unary operation not folded into constant");
1611 return nullptr;
1612 }
1613 break;
1614
1615 case EOpRadians:
1616 if (getType().getBasicType() == EbtFloat)
1617 {
1618 resultArray[i].setFConst(kDegreesToRadiansMultiplier * operandArray[i].getFConst());
1619 break;
1620 }
1621 infoSink.info.message(
1622 EPrefixInternalError, getLine(),
1623 "Unary operation not folded into constant");
1624 return nullptr;
1625
1626 case EOpDegrees:
1627 if (getType().getBasicType() == EbtFloat)
1628 {
1629 resultArray[i].setFConst(kRadiansToDegreesMultiplier * operandArray[i].getFConst());
1630 break;
1631 }
1632 infoSink.info.message(
1633 EPrefixInternalError, getLine(),
1634 "Unary operation not folded into constant");
1635 return nullptr;
1636
1637 case EOpSin:
1638 if (!foldFloatTypeUnary(operandArray[i], &sinf, infoSink, &resultArray[i]))
1639 return nullptr;
1640 break;
1641
1642 case EOpCos:
1643 if (!foldFloatTypeUnary(operandArray[i], &cosf, infoSink, &resultArray[i]))
1644 return nullptr;
1645 break;
1646
1647 case EOpTan:
1648 if (!foldFloatTypeUnary(operandArray[i], &tanf, infoSink, &resultArray[i]))
1649 return nullptr;
1650 break;
1651
1652 case EOpAsin:
1653 // For asin(x), results are undefined if |x| > 1, we are choosing to set result to 0.
1654 if (getType().getBasicType() == EbtFloat && fabsf(operandArray[i].getFConst()) > 1.0f)
1655 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(), infoSink, &resultArray[i]);
1656 else if (!foldFloatTypeUnary(operandArray[i], &asinf, infoSink, &resultArray[i]))
1657 return nullptr;
1658 break;
1659
1660 case EOpAcos:
1661 // For acos(x), results are undefined if |x| > 1, we are choosing to set result to 0.
1662 if (getType().getBasicType() == EbtFloat && fabsf(operandArray[i].getFConst()) > 1.0f)
1663 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(), infoSink, &resultArray[i]);
1664 else if (!foldFloatTypeUnary(operandArray[i], &acosf, infoSink, &resultArray[i]))
1665 return nullptr;
1666 break;
1667
1668 case EOpAtan:
1669 if (!foldFloatTypeUnary(operandArray[i], &atanf, infoSink, &resultArray[i]))
1670 return nullptr;
1671 break;
1672
1673 case EOpSinh:
1674 if (!foldFloatTypeUnary(operandArray[i], &sinhf, infoSink, &resultArray[i]))
1675 return nullptr;
1676 break;
1677
1678 case EOpCosh:
1679 if (!foldFloatTypeUnary(operandArray[i], &coshf, infoSink, &resultArray[i]))
1680 return nullptr;
1681 break;
1682
1683 case EOpTanh:
1684 if (!foldFloatTypeUnary(operandArray[i], &tanhf, infoSink, &resultArray[i]))
1685 return nullptr;
1686 break;
1687
1688 case EOpAsinh:
1689 if (!foldFloatTypeUnary(operandArray[i], &asinhf, infoSink, &resultArray[i]))
1690 return nullptr;
1691 break;
1692
1693 case EOpAcosh:
1694 // For acosh(x), results are undefined if x < 1, we are choosing to set result to 0.
1695 if (getType().getBasicType() == EbtFloat && operandArray[i].getFConst() < 1.0f)
1696 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(), infoSink, &resultArray[i]);
1697 else if (!foldFloatTypeUnary(operandArray[i], &acoshf, infoSink, &resultArray[i]))
1698 return nullptr;
1699 break;
1700
1701 case EOpAtanh:
1702 // For atanh(x), results are undefined if |x| >= 1, we are choosing to set result to 0.
1703 if (getType().getBasicType() == EbtFloat && fabsf(operandArray[i].getFConst()) >= 1.0f)
1704 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(), infoSink, &resultArray[i]);
1705 else if (!foldFloatTypeUnary(operandArray[i], &atanhf, infoSink, &resultArray[i]))
1706 return nullptr;
1707 break;
1708
1709 case EOpAbs:
1710 switch (getType().getBasicType())
1711 {
1712 case EbtFloat:
1713 resultArray[i].setFConst(fabsf(operandArray[i].getFConst()));
1714 break;
1715 case EbtInt:
1716 resultArray[i].setIConst(abs(operandArray[i].getIConst()));
1717 break;
1718 default:
1719 infoSink.info.message(
1720 EPrefixInternalError, getLine(),
1721 "Unary operation not folded into constant");
1722 return nullptr;
1723 }
1724 break;
1725
1726 case EOpSign:
1727 switch (getType().getBasicType())
1728 {
1729 case EbtFloat:
1730 {
1731 float fConst = operandArray[i].getFConst();
1732 float fResult = 0.0f;
1733 if (fConst > 0.0f)
1734 fResult = 1.0f;
1735 else if (fConst < 0.0f)
1736 fResult = -1.0f;
1737 resultArray[i].setFConst(fResult);
1738 }
1739 break;
1740 case EbtInt:
1741 {
1742 int iConst = operandArray[i].getIConst();
1743 int iResult = 0;
1744 if (iConst > 0)
1745 iResult = 1;
1746 else if (iConst < 0)
1747 iResult = -1;
1748 resultArray[i].setIConst(iResult);
1749 }
1750 break;
1751 default:
1752 infoSink.info.message(
1753 EPrefixInternalError, getLine(),
1754 "Unary operation not folded into constant");
1755 return nullptr;
1756 }
1757 break;
1758
1759 case EOpFloor:
1760 if (!foldFloatTypeUnary(operandArray[i], &floorf, infoSink, &resultArray[i]))
1761 return nullptr;
1762 break;
1763
1764 case EOpTrunc:
1765 if (!foldFloatTypeUnary(operandArray[i], &truncf, infoSink, &resultArray[i]))
1766 return nullptr;
1767 break;
1768
1769 case EOpRound:
1770 if (!foldFloatTypeUnary(operandArray[i], &roundf, infoSink, &resultArray[i]))
1771 return nullptr;
1772 break;
1773
1774 case EOpRoundEven:
1775 if (getType().getBasicType() == EbtFloat)
1776 {
1777 float x = operandArray[i].getFConst();
1778 float result;
1779 float fractPart = modff(x, &result);
1780 if (fabsf(fractPart) == 0.5f)
1781 result = 2.0f * roundf(x / 2.0f);
1782 else
1783 result = roundf(x);
1784 resultArray[i].setFConst(result);
1785 break;
1786 }
1787 infoSink.info.message(
1788 EPrefixInternalError, getLine(),
1789 "Unary operation not folded into constant");
1790 return nullptr;
1791
1792 case EOpCeil:
1793 if (!foldFloatTypeUnary(operandArray[i], &ceilf, infoSink, &resultArray[i]))
1794 return nullptr;
1795 break;
1796
1797 case EOpFract:
1798 if (getType().getBasicType() == EbtFloat)
1799 {
1800 float x = operandArray[i].getFConst();
1801 resultArray[i].setFConst(x - floorf(x));
1802 break;
1803 }
1804 infoSink.info.message(
1805 EPrefixInternalError, getLine(),
1806 "Unary operation not folded into constant");
1807 return nullptr;
1808
Arun Patole551279e2015-07-07 18:18:23 +05301809 case EOpIsNan:
1810 if (getType().getBasicType() == EbtFloat)
1811 {
1812 resultArray[i].setBConst(gl::isNaN(operandArray[0].getFConst()));
1813 break;
1814 }
1815 infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
1816 return nullptr;
1817
1818 case EOpIsInf:
1819 if (getType().getBasicType() == EbtFloat)
1820 {
1821 resultArray[i].setBConst(gl::isInf(operandArray[0].getFConst()));
1822 break;
1823 }
1824 infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
1825 return nullptr;
1826
1827 case EOpFloatBitsToInt:
1828 if (getType().getBasicType() == EbtFloat)
1829 {
1830 resultArray[i].setIConst(gl::bitCast<int32_t>(operandArray[0].getFConst()));
1831 break;
1832 }
1833 infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
1834 return nullptr;
1835
1836 case EOpFloatBitsToUint:
1837 if (getType().getBasicType() == EbtFloat)
1838 {
1839 resultArray[i].setUConst(gl::bitCast<uint32_t>(operandArray[0].getFConst()));
1840 break;
1841 }
1842 infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
1843 return nullptr;
1844
1845 case EOpIntBitsToFloat:
1846 if (getType().getBasicType() == EbtInt)
1847 {
1848 resultArray[i].setFConst(gl::bitCast<float>(operandArray[0].getIConst()));
1849 break;
1850 }
1851 infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
1852 return nullptr;
1853
1854 case EOpUintBitsToFloat:
1855 if (getType().getBasicType() == EbtUInt)
1856 {
1857 resultArray[i].setFConst(gl::bitCast<float>(operandArray[0].getUConst()));
1858 break;
1859 }
1860 infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
1861 return nullptr;
1862
Arun Patoleab2b9a22015-07-06 18:27:56 +05301863 case EOpExp:
1864 if (!foldFloatTypeUnary(operandArray[i], &expf, infoSink, &resultArray[i]))
1865 return nullptr;
1866 break;
1867
1868 case EOpLog:
1869 // For log(x), results are undefined if x <= 0, we are choosing to set result to 0.
1870 if (getType().getBasicType() == EbtFloat && operandArray[i].getFConst() <= 0.0f)
1871 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(), infoSink, &resultArray[i]);
1872 else if (!foldFloatTypeUnary(operandArray[i], &logf, infoSink, &resultArray[i]))
1873 return nullptr;
1874 break;
1875
1876 case EOpExp2:
1877 if (!foldFloatTypeUnary(operandArray[i], &exp2f, infoSink, &resultArray[i]))
1878 return nullptr;
1879 break;
1880
1881 case EOpLog2:
1882 // For log2(x), results are undefined if x <= 0, we are choosing to set result to 0.
1883 // And log2f is not available on some plarforms like old android, so just using log(x)/log(2) here.
1884 if (getType().getBasicType() == EbtFloat && operandArray[i].getFConst() <= 0.0f)
1885 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(), infoSink, &resultArray[i]);
1886 else if (!foldFloatTypeUnary(operandArray[i], &logf, infoSink, &resultArray[i]))
1887 return nullptr;
1888 else
1889 resultArray[i].setFConst(resultArray[i].getFConst() / logf(2.0f));
1890 break;
1891
1892 case EOpSqrt:
1893 // For sqrt(x), results are undefined if x < 0, we are choosing to set result to 0.
1894 if (getType().getBasicType() == EbtFloat && operandArray[i].getFConst() < 0.0f)
1895 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(), infoSink, &resultArray[i]);
1896 else if (!foldFloatTypeUnary(operandArray[i], &sqrtf, infoSink, &resultArray[i]))
1897 return nullptr;
1898 break;
1899
1900 case EOpInverseSqrt:
1901 // There is no stdlib built-in function equavalent for GLES built-in inversesqrt(),
1902 // so getting the square root first using builtin function sqrt() and then taking its inverse.
1903 // Also, for inversesqrt(x), results are undefined if x <= 0, we are choosing to set result to 0.
1904 if (getType().getBasicType() == EbtFloat && operandArray[i].getFConst() <= 0.0f)
1905 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(), infoSink, &resultArray[i]);
1906 else if (!foldFloatTypeUnary(operandArray[i], &sqrtf, infoSink, &resultArray[i]))
1907 return nullptr;
1908 else
1909 resultArray[i].setFConst(1.0f / resultArray[i].getFConst());
1910 break;
1911
1912 case EOpVectorLogicalNot:
1913 if (getType().getBasicType() == EbtBool)
1914 {
1915 resultArray[i].setBConst(!operandArray[i].getBConst());
1916 break;
1917 }
1918 infoSink.info.message(
1919 EPrefixInternalError, getLine(),
1920 "Unary operation not folded into constant");
1921 return nullptr;
1922
1923 case EOpNormalize:
1924 if (getType().getBasicType() == EbtFloat)
1925 {
1926 float x = operandArray[i].getFConst();
1927 float length = VectorLength(operandArray, objectSize);
1928 if (length)
1929 resultArray[i].setFConst(x / length);
1930 else
1931 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(), infoSink,
1932 &resultArray[i]);
1933 break;
1934 }
1935 infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
1936 return nullptr;
1937
Arun Patole0c5409f2015-07-08 15:17:53 +05301938 case EOpDFdx:
1939 case EOpDFdy:
1940 case EOpFwidth:
1941 if (getType().getBasicType() == EbtFloat)
1942 {
1943 // Derivatives of constant arguments should be 0.
1944 resultArray[i].setFConst(0.0f);
1945 break;
1946 }
1947 infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
1948 return nullptr;
1949
Arun Patole1155ddd2015-06-05 18:04:36 +05301950 default:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301951 return nullptr;
Arun Patole9d0b1f92015-05-20 14:27:17 +05301952 }
Arun Patole9d0b1f92015-05-20 14:27:17 +05301953 }
Jamie Madillb1a85f42014-08-19 15:23:24 -04001954
Arun Patoleab2b9a22015-07-06 18:27:56 +05301955 return resultArray;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001956}
1957
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001958bool TIntermConstantUnion::foldFloatTypeUnary(const TConstantUnion &parameter, FloatTypeUnaryFunc builtinFunc,
1959 TInfoSink &infoSink, TConstantUnion *result) const
Arun Patole9dea48f2015-04-02 11:45:09 +05301960{
1961 ASSERT(builtinFunc);
1962
1963 if (getType().getBasicType() == EbtFloat)
1964 {
1965 result->setFConst(builtinFunc(parameter.getFConst()));
1966 return true;
1967 }
1968
1969 infoSink.info.message(
1970 EPrefixInternalError, getLine(),
1971 "Unary operation not folded into constant");
1972 return false;
1973}
1974
Jamie Madillb1a85f42014-08-19 15:23:24 -04001975// static
Olli Etuahob43846e2015-06-02 18:18:57 +03001976TConstantUnion *TIntermConstantUnion::FoldAggregateBuiltIn(TIntermAggregate *aggregate, TInfoSink &infoSink)
Arun Patole274f0702015-05-05 13:33:30 +05301977{
Olli Etuahob43846e2015-06-02 18:18:57 +03001978 TOperator op = aggregate->getOp();
Arun Patole274f0702015-05-05 13:33:30 +05301979 TIntermSequence *sequence = aggregate->getSequence();
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001980 unsigned int paramsCount = static_cast<unsigned int>(sequence->size());
Arun Patole274f0702015-05-05 13:33:30 +05301981 std::vector<TConstantUnion *> unionArrays(paramsCount);
1982 std::vector<size_t> objectSizes(paramsCount);
Olli Etuahob43846e2015-06-02 18:18:57 +03001983 size_t maxObjectSize = 0;
Arun Patole274f0702015-05-05 13:33:30 +05301984 TBasicType basicType = EbtVoid;
1985 TSourceLoc loc;
1986 for (unsigned int i = 0; i < paramsCount; i++)
1987 {
1988 TIntermConstantUnion *paramConstant = (*sequence)[i]->getAsConstantUnion();
Olli Etuahob43846e2015-06-02 18:18:57 +03001989 ASSERT(paramConstant != nullptr); // Should be checked already.
Arun Patole274f0702015-05-05 13:33:30 +05301990
1991 if (i == 0)
1992 {
1993 basicType = paramConstant->getType().getBasicType();
1994 loc = paramConstant->getLine();
1995 }
1996 unionArrays[i] = paramConstant->getUnionArrayPointer();
1997 objectSizes[i] = paramConstant->getType().getObjectSize();
Olli Etuahob43846e2015-06-02 18:18:57 +03001998 if (objectSizes[i] > maxObjectSize)
1999 maxObjectSize = objectSizes[i];
Arun Patole274f0702015-05-05 13:33:30 +05302000 }
2001
Arun Patole7fa33552015-06-10 15:15:18 +05302002 if (!(*sequence)[0]->getAsTyped()->isMatrix())
2003 {
2004 for (unsigned int i = 0; i < paramsCount; i++)
2005 if (objectSizes[i] != maxObjectSize)
2006 unionArrays[i] = Vectorize(*unionArrays[i], maxObjectSize);
2007 }
Arun Patole274f0702015-05-05 13:33:30 +05302008
Olli Etuahob43846e2015-06-02 18:18:57 +03002009 TConstantUnion *resultArray = nullptr;
Arun Patole274f0702015-05-05 13:33:30 +05302010 if (paramsCount == 2)
2011 {
2012 //
2013 // Binary built-in
2014 //
2015 switch (op)
2016 {
Arun Patolebf790422015-05-18 17:53:04 +05302017 case EOpAtan:
2018 {
2019 if (basicType == EbtFloat)
2020 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002021 resultArray = new TConstantUnion[maxObjectSize];
Arun Patolebf790422015-05-18 17:53:04 +05302022 for (size_t i = 0; i < maxObjectSize; i++)
2023 {
2024 float y = unionArrays[0][i].getFConst();
2025 float x = unionArrays[1][i].getFConst();
2026 // Results are undefined if x and y are both 0.
2027 if (x == 0.0f && y == 0.0f)
Olli Etuahob43846e2015-06-02 18:18:57 +03002028 UndefinedConstantFoldingError(loc, op, basicType, infoSink, &resultArray[i]);
Arun Patolebf790422015-05-18 17:53:04 +05302029 else
Olli Etuahob43846e2015-06-02 18:18:57 +03002030 resultArray[i].setFConst(atan2f(y, x));
Arun Patolebf790422015-05-18 17:53:04 +05302031 }
2032 }
2033 else
2034 UNREACHABLE();
2035 }
2036 break;
2037
2038 case EOpPow:
2039 {
2040 if (basicType == EbtFloat)
2041 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002042 resultArray = new TConstantUnion[maxObjectSize];
Arun Patolebf790422015-05-18 17:53:04 +05302043 for (size_t i = 0; i < maxObjectSize; i++)
2044 {
2045 float x = unionArrays[0][i].getFConst();
2046 float y = unionArrays[1][i].getFConst();
2047 // Results are undefined if x < 0.
2048 // Results are undefined if x = 0 and y <= 0.
2049 if (x < 0.0f)
Olli Etuahob43846e2015-06-02 18:18:57 +03002050 UndefinedConstantFoldingError(loc, op, basicType, infoSink, &resultArray[i]);
Arun Patolebf790422015-05-18 17:53:04 +05302051 else if (x == 0.0f && y <= 0.0f)
Olli Etuahob43846e2015-06-02 18:18:57 +03002052 UndefinedConstantFoldingError(loc, op, basicType, infoSink, &resultArray[i]);
Arun Patolebf790422015-05-18 17:53:04 +05302053 else
Olli Etuahob43846e2015-06-02 18:18:57 +03002054 resultArray[i].setFConst(powf(x, y));
Arun Patolebf790422015-05-18 17:53:04 +05302055 }
2056 }
2057 else
2058 UNREACHABLE();
2059 }
2060 break;
2061
2062 case EOpMod:
2063 {
2064 if (basicType == EbtFloat)
2065 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002066 resultArray = new TConstantUnion[maxObjectSize];
Arun Patolebf790422015-05-18 17:53:04 +05302067 for (size_t i = 0; i < maxObjectSize; i++)
2068 {
2069 float x = unionArrays[0][i].getFConst();
2070 float y = unionArrays[1][i].getFConst();
Olli Etuahob43846e2015-06-02 18:18:57 +03002071 resultArray[i].setFConst(x - y * floorf(x / y));
Arun Patolebf790422015-05-18 17:53:04 +05302072 }
2073 }
2074 else
2075 UNREACHABLE();
2076 }
2077 break;
2078
Arun Patole274f0702015-05-05 13:33:30 +05302079 case EOpMin:
2080 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002081 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole274f0702015-05-05 13:33:30 +05302082 for (size_t i = 0; i < maxObjectSize; i++)
2083 {
2084 switch (basicType)
2085 {
2086 case EbtFloat:
Olli Etuahob43846e2015-06-02 18:18:57 +03002087 resultArray[i].setFConst(std::min(unionArrays[0][i].getFConst(), unionArrays[1][i].getFConst()));
Arun Patole274f0702015-05-05 13:33:30 +05302088 break;
2089 case EbtInt:
Olli Etuahob43846e2015-06-02 18:18:57 +03002090 resultArray[i].setIConst(std::min(unionArrays[0][i].getIConst(), unionArrays[1][i].getIConst()));
Arun Patole274f0702015-05-05 13:33:30 +05302091 break;
2092 case EbtUInt:
Olli Etuahob43846e2015-06-02 18:18:57 +03002093 resultArray[i].setUConst(std::min(unionArrays[0][i].getUConst(), unionArrays[1][i].getUConst()));
Arun Patole274f0702015-05-05 13:33:30 +05302094 break;
2095 default:
2096 UNREACHABLE();
2097 break;
2098 }
2099 }
2100 }
2101 break;
2102
2103 case EOpMax:
2104 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002105 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole274f0702015-05-05 13:33:30 +05302106 for (size_t i = 0; i < maxObjectSize; i++)
2107 {
2108 switch (basicType)
2109 {
2110 case EbtFloat:
Olli Etuahob43846e2015-06-02 18:18:57 +03002111 resultArray[i].setFConst(std::max(unionArrays[0][i].getFConst(), unionArrays[1][i].getFConst()));
Arun Patole274f0702015-05-05 13:33:30 +05302112 break;
2113 case EbtInt:
Olli Etuahob43846e2015-06-02 18:18:57 +03002114 resultArray[i].setIConst(std::max(unionArrays[0][i].getIConst(), unionArrays[1][i].getIConst()));
Arun Patole274f0702015-05-05 13:33:30 +05302115 break;
2116 case EbtUInt:
Olli Etuahob43846e2015-06-02 18:18:57 +03002117 resultArray[i].setUConst(std::max(unionArrays[0][i].getUConst(), unionArrays[1][i].getUConst()));
Arun Patole274f0702015-05-05 13:33:30 +05302118 break;
2119 default:
2120 UNREACHABLE();
2121 break;
2122 }
2123 }
2124 }
2125 break;
2126
Arun Patolebf790422015-05-18 17:53:04 +05302127 case EOpStep:
2128 {
2129 if (basicType == EbtFloat)
2130 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002131 resultArray = new TConstantUnion[maxObjectSize];
Arun Patolebf790422015-05-18 17:53:04 +05302132 for (size_t i = 0; i < maxObjectSize; i++)
Olli Etuahob43846e2015-06-02 18:18:57 +03002133 resultArray[i].setFConst(unionArrays[1][i].getFConst() < unionArrays[0][i].getFConst() ? 0.0f : 1.0f);
Arun Patolebf790422015-05-18 17:53:04 +05302134 }
2135 else
2136 UNREACHABLE();
2137 }
2138 break;
2139
Arun Patole9d0b1f92015-05-20 14:27:17 +05302140 case EOpLessThan:
2141 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002142 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole9d0b1f92015-05-20 14:27:17 +05302143 for (size_t i = 0; i < maxObjectSize; i++)
2144 {
2145 switch (basicType)
2146 {
2147 case EbtFloat:
Olli Etuahob43846e2015-06-02 18:18:57 +03002148 resultArray[i].setBConst(unionArrays[0][i].getFConst() < unionArrays[1][i].getFConst());
Arun Patole9d0b1f92015-05-20 14:27:17 +05302149 break;
2150 case EbtInt:
Olli Etuahob43846e2015-06-02 18:18:57 +03002151 resultArray[i].setBConst(unionArrays[0][i].getIConst() < unionArrays[1][i].getIConst());
Arun Patole9d0b1f92015-05-20 14:27:17 +05302152 break;
2153 case EbtUInt:
Olli Etuahob43846e2015-06-02 18:18:57 +03002154 resultArray[i].setBConst(unionArrays[0][i].getUConst() < unionArrays[1][i].getUConst());
Arun Patole9d0b1f92015-05-20 14:27:17 +05302155 break;
2156 default:
2157 UNREACHABLE();
2158 break;
2159 }
2160 }
2161 }
2162 break;
2163
2164 case EOpLessThanEqual:
2165 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002166 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole9d0b1f92015-05-20 14:27:17 +05302167 for (size_t i = 0; i < maxObjectSize; i++)
2168 {
2169 switch (basicType)
2170 {
2171 case EbtFloat:
Olli Etuahob43846e2015-06-02 18:18:57 +03002172 resultArray[i].setBConst(unionArrays[0][i].getFConst() <= unionArrays[1][i].getFConst());
Arun Patole9d0b1f92015-05-20 14:27:17 +05302173 break;
2174 case EbtInt:
Olli Etuahob43846e2015-06-02 18:18:57 +03002175 resultArray[i].setBConst(unionArrays[0][i].getIConst() <= unionArrays[1][i].getIConst());
Arun Patole9d0b1f92015-05-20 14:27:17 +05302176 break;
2177 case EbtUInt:
Olli Etuahob43846e2015-06-02 18:18:57 +03002178 resultArray[i].setBConst(unionArrays[0][i].getUConst() <= unionArrays[1][i].getUConst());
Arun Patole9d0b1f92015-05-20 14:27:17 +05302179 break;
2180 default:
2181 UNREACHABLE();
2182 break;
2183 }
2184 }
2185 }
2186 break;
2187
2188 case EOpGreaterThan:
2189 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002190 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole9d0b1f92015-05-20 14:27:17 +05302191 for (size_t i = 0; i < maxObjectSize; i++)
2192 {
2193 switch (basicType)
2194 {
2195 case EbtFloat:
Olli Etuahob43846e2015-06-02 18:18:57 +03002196 resultArray[i].setBConst(unionArrays[0][i].getFConst() > unionArrays[1][i].getFConst());
Arun Patole9d0b1f92015-05-20 14:27:17 +05302197 break;
2198 case EbtInt:
Olli Etuahob43846e2015-06-02 18:18:57 +03002199 resultArray[i].setBConst(unionArrays[0][i].getIConst() > unionArrays[1][i].getIConst());
Arun Patole9d0b1f92015-05-20 14:27:17 +05302200 break;
2201 case EbtUInt:
Olli Etuahob43846e2015-06-02 18:18:57 +03002202 resultArray[i].setBConst(unionArrays[0][i].getUConst() > unionArrays[1][i].getUConst());
Arun Patole9d0b1f92015-05-20 14:27:17 +05302203 break;
2204 default:
2205 UNREACHABLE();
2206 break;
Olli Etuahob43846e2015-06-02 18:18:57 +03002207 }
2208 }
Arun Patole9d0b1f92015-05-20 14:27:17 +05302209 }
2210 break;
2211
2212 case EOpGreaterThanEqual:
2213 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002214 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole9d0b1f92015-05-20 14:27:17 +05302215 for (size_t i = 0; i < maxObjectSize; i++)
2216 {
2217 switch (basicType)
2218 {
2219 case EbtFloat:
Olli Etuahob43846e2015-06-02 18:18:57 +03002220 resultArray[i].setBConst(unionArrays[0][i].getFConst() >= unionArrays[1][i].getFConst());
Arun Patole9d0b1f92015-05-20 14:27:17 +05302221 break;
2222 case EbtInt:
Olli Etuahob43846e2015-06-02 18:18:57 +03002223 resultArray[i].setBConst(unionArrays[0][i].getIConst() >= unionArrays[1][i].getIConst());
Arun Patole9d0b1f92015-05-20 14:27:17 +05302224 break;
2225 case EbtUInt:
Olli Etuahob43846e2015-06-02 18:18:57 +03002226 resultArray[i].setBConst(unionArrays[0][i].getUConst() >= unionArrays[1][i].getUConst());
Arun Patole9d0b1f92015-05-20 14:27:17 +05302227 break;
2228 default:
2229 UNREACHABLE();
2230 break;
2231 }
2232 }
2233 }
2234 break;
2235
2236 case EOpVectorEqual:
2237 {
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 {
2243 case EbtFloat:
Olli Etuahob43846e2015-06-02 18:18:57 +03002244 resultArray[i].setBConst(unionArrays[0][i].getFConst() == unionArrays[1][i].getFConst());
Arun Patole9d0b1f92015-05-20 14:27:17 +05302245 break;
2246 case EbtInt:
Olli Etuahob43846e2015-06-02 18:18:57 +03002247 resultArray[i].setBConst(unionArrays[0][i].getIConst() == unionArrays[1][i].getIConst());
Arun Patole9d0b1f92015-05-20 14:27:17 +05302248 break;
2249 case EbtUInt:
Olli Etuahob43846e2015-06-02 18:18:57 +03002250 resultArray[i].setBConst(unionArrays[0][i].getUConst() == unionArrays[1][i].getUConst());
Arun Patole9d0b1f92015-05-20 14:27:17 +05302251 break;
2252 case EbtBool:
Olli Etuahob43846e2015-06-02 18:18:57 +03002253 resultArray[i].setBConst(unionArrays[0][i].getBConst() == unionArrays[1][i].getBConst());
Arun Patole9d0b1f92015-05-20 14:27:17 +05302254 break;
2255 default:
2256 UNREACHABLE();
2257 break;
2258 }
2259 }
2260 }
2261 break;
2262
2263 case EOpVectorNotEqual:
2264 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002265 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole9d0b1f92015-05-20 14:27:17 +05302266 for (size_t i = 0; i < maxObjectSize; i++)
2267 {
2268 switch (basicType)
2269 {
2270 case EbtFloat:
Olli Etuahob43846e2015-06-02 18:18:57 +03002271 resultArray[i].setBConst(unionArrays[0][i].getFConst() != unionArrays[1][i].getFConst());
Arun Patole9d0b1f92015-05-20 14:27:17 +05302272 break;
2273 case EbtInt:
Olli Etuahob43846e2015-06-02 18:18:57 +03002274 resultArray[i].setBConst(unionArrays[0][i].getIConst() != unionArrays[1][i].getIConst());
Arun Patole9d0b1f92015-05-20 14:27:17 +05302275 break;
2276 case EbtUInt:
Olli Etuahob43846e2015-06-02 18:18:57 +03002277 resultArray[i].setBConst(unionArrays[0][i].getUConst() != unionArrays[1][i].getUConst());
Arun Patole9d0b1f92015-05-20 14:27:17 +05302278 break;
2279 case EbtBool:
Olli Etuahob43846e2015-06-02 18:18:57 +03002280 resultArray[i].setBConst(unionArrays[0][i].getBConst() != unionArrays[1][i].getBConst());
Arun Patole9d0b1f92015-05-20 14:27:17 +05302281 break;
2282 default:
2283 UNREACHABLE();
2284 break;
2285 }
2286 }
2287 }
2288 break;
2289
Arun Patole1155ddd2015-06-05 18:04:36 +05302290 case EOpDistance:
2291 if (basicType == EbtFloat)
2292 {
2293 TConstantUnion *distanceArray = new TConstantUnion[maxObjectSize];
Olli Etuahob43846e2015-06-02 18:18:57 +03002294 resultArray = new TConstantUnion();
Arun Patole1155ddd2015-06-05 18:04:36 +05302295 for (size_t i = 0; i < maxObjectSize; i++)
2296 {
2297 float x = unionArrays[0][i].getFConst();
2298 float y = unionArrays[1][i].getFConst();
2299 distanceArray[i].setFConst(x - y);
2300 }
Olli Etuahob43846e2015-06-02 18:18:57 +03002301 resultArray->setFConst(VectorLength(distanceArray, maxObjectSize));
Arun Patole1155ddd2015-06-05 18:04:36 +05302302 }
2303 else
2304 UNREACHABLE();
2305 break;
2306
2307 case EOpDot:
Olli Etuahob43846e2015-06-02 18:18:57 +03002308
Arun Patole1155ddd2015-06-05 18:04:36 +05302309 if (basicType == EbtFloat)
2310 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002311 resultArray = new TConstantUnion();
2312 resultArray->setFConst(VectorDotProduct(unionArrays[0], unionArrays[1], maxObjectSize));
Arun Patole1155ddd2015-06-05 18:04:36 +05302313 }
2314 else
2315 UNREACHABLE();
2316 break;
2317
2318 case EOpCross:
2319 if (basicType == EbtFloat && maxObjectSize == 3)
2320 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002321 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole1155ddd2015-06-05 18:04:36 +05302322 float x0 = unionArrays[0][0].getFConst();
2323 float x1 = unionArrays[0][1].getFConst();
2324 float x2 = unionArrays[0][2].getFConst();
2325 float y0 = unionArrays[1][0].getFConst();
2326 float y1 = unionArrays[1][1].getFConst();
2327 float y2 = unionArrays[1][2].getFConst();
Olli Etuahob43846e2015-06-02 18:18:57 +03002328 resultArray[0].setFConst(x1 * y2 - y1 * x2);
2329 resultArray[1].setFConst(x2 * y0 - y2 * x0);
2330 resultArray[2].setFConst(x0 * y1 - y0 * x1);
Arun Patole1155ddd2015-06-05 18:04:36 +05302331 }
2332 else
2333 UNREACHABLE();
2334 break;
2335
2336 case EOpReflect:
2337 if (basicType == EbtFloat)
2338 {
2339 // genType reflect (genType I, genType N) :
2340 // For the incident vector I and surface orientation N, returns the reflection direction:
2341 // I - 2 * dot(N, I) * N.
Olli Etuahob43846e2015-06-02 18:18:57 +03002342 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole1155ddd2015-06-05 18:04:36 +05302343 float dotProduct = VectorDotProduct(unionArrays[1], unionArrays[0], maxObjectSize);
2344 for (size_t i = 0; i < maxObjectSize; i++)
2345 {
2346 float result = unionArrays[0][i].getFConst() -
2347 2.0f * dotProduct * unionArrays[1][i].getFConst();
Olli Etuahob43846e2015-06-02 18:18:57 +03002348 resultArray[i].setFConst(result);
Arun Patole1155ddd2015-06-05 18:04:36 +05302349 }
2350 }
2351 else
2352 UNREACHABLE();
2353 break;
2354
Arun Patole7fa33552015-06-10 15:15:18 +05302355 case EOpMul:
2356 if (basicType == EbtFloat && (*sequence)[0]->getAsTyped()->isMatrix() &&
2357 (*sequence)[1]->getAsTyped()->isMatrix())
2358 {
2359 // Perform component-wise matrix multiplication.
2360 resultArray = new TConstantUnion[maxObjectSize];
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002361 int size = (*sequence)[0]->getAsTyped()->getNominalSize();
Arun Patole7fa33552015-06-10 15:15:18 +05302362 angle::Matrix<float> result =
2363 GetMatrix(unionArrays[0], size).compMult(GetMatrix(unionArrays[1], size));
2364 SetUnionArrayFromMatrix(result, resultArray);
2365 }
2366 else
2367 UNREACHABLE();
2368 break;
2369
2370 case EOpOuterProduct:
2371 if (basicType == EbtFloat)
2372 {
2373 size_t numRows = (*sequence)[0]->getAsTyped()->getType().getObjectSize();
2374 size_t numCols = (*sequence)[1]->getAsTyped()->getType().getObjectSize();
2375 resultArray = new TConstantUnion[numRows * numCols];
2376 angle::Matrix<float> result =
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002377 GetMatrix(unionArrays[0], 1, static_cast<int>(numCols))
2378 .outerProduct(GetMatrix(unionArrays[1], static_cast<int>(numRows), 1));
Arun Patole7fa33552015-06-10 15:15:18 +05302379 SetUnionArrayFromMatrix(result, resultArray);
2380 }
2381 else
2382 UNREACHABLE();
2383 break;
2384
Arun Patole274f0702015-05-05 13:33:30 +05302385 default:
2386 UNREACHABLE();
2387 // TODO: Add constant folding support for other built-in operations that take 2 parameters and not handled above.
2388 return nullptr;
2389 }
2390 }
2391 else if (paramsCount == 3)
2392 {
2393 //
2394 // Ternary built-in
2395 //
2396 switch (op)
2397 {
2398 case EOpClamp:
2399 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002400 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole274f0702015-05-05 13:33:30 +05302401 for (size_t i = 0; i < maxObjectSize; i++)
2402 {
2403 switch (basicType)
2404 {
2405 case EbtFloat:
2406 {
2407 float x = unionArrays[0][i].getFConst();
2408 float min = unionArrays[1][i].getFConst();
2409 float max = unionArrays[2][i].getFConst();
2410 // Results are undefined if min > max.
2411 if (min > max)
Olli Etuahob43846e2015-06-02 18:18:57 +03002412 UndefinedConstantFoldingError(loc, op, basicType, infoSink, &resultArray[i]);
Arun Patole274f0702015-05-05 13:33:30 +05302413 else
Olli Etuahob43846e2015-06-02 18:18:57 +03002414 resultArray[i].setFConst(gl::clamp(x, min, max));
Arun Patole274f0702015-05-05 13:33:30 +05302415 }
2416 break;
2417 case EbtInt:
2418 {
2419 int x = unionArrays[0][i].getIConst();
2420 int min = unionArrays[1][i].getIConst();
2421 int max = unionArrays[2][i].getIConst();
2422 // Results are undefined if min > max.
2423 if (min > max)
Olli Etuahob43846e2015-06-02 18:18:57 +03002424 UndefinedConstantFoldingError(loc, op, basicType, infoSink, &resultArray[i]);
Arun Patole274f0702015-05-05 13:33:30 +05302425 else
Olli Etuahob43846e2015-06-02 18:18:57 +03002426 resultArray[i].setIConst(gl::clamp(x, min, max));
Arun Patole274f0702015-05-05 13:33:30 +05302427 }
2428 break;
2429 case EbtUInt:
2430 {
2431 unsigned int x = unionArrays[0][i].getUConst();
2432 unsigned int min = unionArrays[1][i].getUConst();
2433 unsigned int max = unionArrays[2][i].getUConst();
2434 // Results are undefined if min > max.
2435 if (min > max)
Olli Etuahob43846e2015-06-02 18:18:57 +03002436 UndefinedConstantFoldingError(loc, op, basicType, infoSink, &resultArray[i]);
Arun Patole274f0702015-05-05 13:33:30 +05302437 else
Olli Etuahob43846e2015-06-02 18:18:57 +03002438 resultArray[i].setUConst(gl::clamp(x, min, max));
Arun Patole274f0702015-05-05 13:33:30 +05302439 }
2440 break;
2441 default:
2442 UNREACHABLE();
2443 break;
2444 }
2445 }
2446 }
2447 break;
2448
Arun Patolebf790422015-05-18 17:53:04 +05302449 case EOpMix:
2450 {
2451 if (basicType == EbtFloat)
2452 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002453 resultArray = new TConstantUnion[maxObjectSize];
Arun Patolebf790422015-05-18 17:53:04 +05302454 for (size_t i = 0; i < maxObjectSize; i++)
2455 {
2456 float x = unionArrays[0][i].getFConst();
2457 float y = unionArrays[1][i].getFConst();
2458 TBasicType type = (*sequence)[2]->getAsTyped()->getType().getBasicType();
2459 if (type == EbtFloat)
2460 {
2461 // Returns the linear blend of x and y, i.e., x * (1 - a) + y * a.
2462 float a = unionArrays[2][i].getFConst();
Olli Etuahob43846e2015-06-02 18:18:57 +03002463 resultArray[i].setFConst(x * (1.0f - a) + y * a);
Arun Patolebf790422015-05-18 17:53:04 +05302464 }
2465 else // 3rd parameter is EbtBool
2466 {
2467 ASSERT(type == EbtBool);
2468 // Selects which vector each returned component comes from.
2469 // For a component of a that is false, the corresponding component of x is returned.
2470 // For a component of a that is true, the corresponding component of y is returned.
2471 bool a = unionArrays[2][i].getBConst();
Olli Etuahob43846e2015-06-02 18:18:57 +03002472 resultArray[i].setFConst(a ? y : x);
Arun Patolebf790422015-05-18 17:53:04 +05302473 }
2474 }
2475 }
2476 else
2477 UNREACHABLE();
2478 }
2479 break;
2480
2481 case EOpSmoothStep:
2482 {
2483 if (basicType == EbtFloat)
2484 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002485 resultArray = new TConstantUnion[maxObjectSize];
Arun Patolebf790422015-05-18 17:53:04 +05302486 for (size_t i = 0; i < maxObjectSize; i++)
2487 {
2488 float edge0 = unionArrays[0][i].getFConst();
2489 float edge1 = unionArrays[1][i].getFConst();
2490 float x = unionArrays[2][i].getFConst();
2491 // Results are undefined if edge0 >= edge1.
2492 if (edge0 >= edge1)
2493 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002494 UndefinedConstantFoldingError(loc, op, basicType, infoSink, &resultArray[i]);
Arun Patolebf790422015-05-18 17:53:04 +05302495 }
2496 else
2497 {
2498 // Returns 0.0 if x <= edge0 and 1.0 if x >= edge1 and performs smooth
2499 // Hermite interpolation between 0 and 1 when edge0 < x < edge1.
2500 float t = gl::clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
Olli Etuahob43846e2015-06-02 18:18:57 +03002501 resultArray[i].setFConst(t * t * (3.0f - 2.0f * t));
Arun Patolebf790422015-05-18 17:53:04 +05302502 }
2503 }
2504 }
2505 else
2506 UNREACHABLE();
2507 }
2508 break;
2509
Arun Patole1155ddd2015-06-05 18:04:36 +05302510 case EOpFaceForward:
2511 if (basicType == EbtFloat)
2512 {
2513 // genType faceforward(genType N, genType I, genType Nref) :
2514 // If dot(Nref, I) < 0 return N, otherwise return -N.
Olli Etuahob43846e2015-06-02 18:18:57 +03002515 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole1155ddd2015-06-05 18:04:36 +05302516 float dotProduct = VectorDotProduct(unionArrays[2], unionArrays[1], maxObjectSize);
2517 for (size_t i = 0; i < maxObjectSize; i++)
2518 {
2519 if (dotProduct < 0)
Olli Etuahob43846e2015-06-02 18:18:57 +03002520 resultArray[i].setFConst(unionArrays[0][i].getFConst());
Arun Patole1155ddd2015-06-05 18:04:36 +05302521 else
Olli Etuahob43846e2015-06-02 18:18:57 +03002522 resultArray[i].setFConst(-unionArrays[0][i].getFConst());
Arun Patole1155ddd2015-06-05 18:04:36 +05302523 }
2524 }
2525 else
2526 UNREACHABLE();
2527 break;
2528
2529 case EOpRefract:
2530 if (basicType == EbtFloat)
2531 {
2532 // genType refract(genType I, genType N, float eta) :
2533 // For the incident vector I and surface normal N, and the ratio of indices of refraction eta,
2534 // return the refraction vector. The result is computed by
2535 // k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I))
2536 // if (k < 0.0)
2537 // return genType(0.0)
2538 // else
2539 // return eta * I - (eta * dot(N, I) + sqrt(k)) * N
Olli Etuahob43846e2015-06-02 18:18:57 +03002540 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole1155ddd2015-06-05 18:04:36 +05302541 float dotProduct = VectorDotProduct(unionArrays[1], unionArrays[0], maxObjectSize);
2542 for (size_t i = 0; i < maxObjectSize; i++)
2543 {
2544 float eta = unionArrays[2][i].getFConst();
2545 float k = 1.0f - eta * eta * (1.0f - dotProduct * dotProduct);
2546 if (k < 0.0f)
Olli Etuahob43846e2015-06-02 18:18:57 +03002547 resultArray[i].setFConst(0.0f);
Arun Patole1155ddd2015-06-05 18:04:36 +05302548 else
Olli Etuahob43846e2015-06-02 18:18:57 +03002549 resultArray[i].setFConst(eta * unionArrays[0][i].getFConst() -
Arun Patole1155ddd2015-06-05 18:04:36 +05302550 (eta * dotProduct + sqrtf(k)) * unionArrays[1][i].getFConst());
2551 }
2552 }
2553 else
2554 UNREACHABLE();
2555 break;
2556
Arun Patole274f0702015-05-05 13:33:30 +05302557 default:
2558 UNREACHABLE();
2559 // TODO: Add constant folding support for other built-in operations that take 3 parameters and not handled above.
2560 return nullptr;
2561 }
2562 }
Olli Etuahob43846e2015-06-02 18:18:57 +03002563 return resultArray;
Arun Patole274f0702015-05-05 13:33:30 +05302564}
2565
2566// static
Jamie Madillb1a85f42014-08-19 15:23:24 -04002567TString TIntermTraverser::hash(const TString &name, ShHashFunction64 hashFunction)
2568{
2569 if (hashFunction == NULL || name.empty())
2570 return name;
2571 khronos_uint64_t number = (*hashFunction)(name.c_str(), name.length());
2572 TStringStream stream;
2573 stream << HASHED_NAME_PREFIX << std::hex << number;
2574 TString hashedName = stream.str();
2575 return hashedName;
2576}
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002577
2578void TIntermTraverser::updateTree()
2579{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002580 for (size_t ii = 0; ii < mInsertions.size(); ++ii)
2581 {
2582 const NodeInsertMultipleEntry &insertion = mInsertions[ii];
2583 ASSERT(insertion.parent);
Olli Etuaho5d91dda2015-06-18 15:47:46 +03002584 if (!insertion.insertionsAfter.empty())
2585 {
2586 bool inserted = insertion.parent->insertChildNodes(insertion.position + 1,
2587 insertion.insertionsAfter);
2588 ASSERT(inserted);
2589 UNUSED_ASSERTION_VARIABLE(inserted);
2590 }
2591 if (!insertion.insertionsBefore.empty())
2592 {
2593 bool inserted =
2594 insertion.parent->insertChildNodes(insertion.position, insertion.insertionsBefore);
2595 ASSERT(inserted);
2596 UNUSED_ASSERTION_VARIABLE(inserted);
2597 }
Olli Etuahoa6f22092015-05-08 18:31:10 +03002598 }
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002599 for (size_t ii = 0; ii < mReplacements.size(); ++ii)
2600 {
Olli Etuahocd94ef92015-04-16 19:18:10 +03002601 const NodeUpdateEntry &replacement = mReplacements[ii];
2602 ASSERT(replacement.parent);
2603 bool replaced = replacement.parent->replaceChildNode(
2604 replacement.original, replacement.replacement);
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002605 ASSERT(replaced);
Olli Etuahod57e0db2015-04-24 15:05:08 +03002606 UNUSED_ASSERTION_VARIABLE(replaced);
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002607
Olli Etuahocd94ef92015-04-16 19:18:10 +03002608 if (!replacement.originalBecomesChildOfReplacement)
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002609 {
2610 // In AST traversing, a parent is visited before its children.
Olli Etuahocd94ef92015-04-16 19:18:10 +03002611 // After we replace a node, if its immediate child is to
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002612 // be replaced, we need to make sure we don't update the replaced
2613 // node; instead, we update the replacement node.
2614 for (size_t jj = ii + 1; jj < mReplacements.size(); ++jj)
2615 {
Olli Etuahocd94ef92015-04-16 19:18:10 +03002616 NodeUpdateEntry &replacement2 = mReplacements[jj];
2617 if (replacement2.parent == replacement.original)
2618 replacement2.parent = replacement.replacement;
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002619 }
2620 }
2621 }
Olli Etuahofc0e2bc2015-04-16 13:39:56 +03002622 for (size_t ii = 0; ii < mMultiReplacements.size(); ++ii)
2623 {
2624 const NodeReplaceWithMultipleEntry &replacement = mMultiReplacements[ii];
2625 ASSERT(replacement.parent);
2626 bool replaced = replacement.parent->replaceChildNodeWithMultiple(
2627 replacement.original, replacement.replacements);
2628 ASSERT(replaced);
Olli Etuahod57e0db2015-04-24 15:05:08 +03002629 UNUSED_ASSERTION_VARIABLE(replaced);
Olli Etuahofc0e2bc2015-04-16 13:39:56 +03002630 }
Olli Etuahod4f303e2015-05-20 17:09:06 +03002631
2632 mInsertions.clear();
2633 mReplacements.clear();
2634 mMultiReplacements.clear();
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002635}