blob: 7588fbe227fc829a2512eee8a7c8f7a9b70831dc [file] [log] [blame]
Jamie Madillb1a85f42014-08-19 15:23:24 -04001//
2// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7//
8// Build the intermediate representation.
9//
10
11#include <float.h>
12#include <limits.h>
Arun Patole9dea48f2015-04-02 11:45:09 +053013#include <math.h>
Arun Patole97dc22e2015-04-06 17:35:38 +053014#include <stdlib.h>
Jamie Madillb1a85f42014-08-19 15:23:24 -040015#include <algorithm>
Arun Patole274f0702015-05-05 13:33:30 +053016#include <vector>
Jamie Madillb1a85f42014-08-19 15:23:24 -040017
Arun Patole274f0702015-05-05 13:33:30 +053018#include "common/mathutil.h"
Arun Patole7fa33552015-06-10 15:15:18 +053019#include "common/matrix_utils.h"
Olli Etuaho3fdec912016-08-18 15:08:06 +030020#include "compiler/translator/Diagnostics.h"
Jamie Madillb1a85f42014-08-19 15:23:24 -040021#include "compiler/translator/HashNames.h"
22#include "compiler/translator/IntermNode.h"
23#include "compiler/translator/SymbolTable.h"
Corentin Wallez509e4562016-08-25 14:55:44 -040024#include "compiler/translator/util.h"
Jamie Madillb1a85f42014-08-19 15:23:24 -040025
26namespace
27{
28
Arun Patole9dea48f2015-04-02 11:45:09 +053029const float kPi = 3.14159265358979323846f;
30const float kDegreesToRadiansMultiplier = kPi / 180.0f;
31const float kRadiansToDegreesMultiplier = 180.0f / kPi;
32
Jamie Madillb1a85f42014-08-19 15:23:24 -040033TPrecision GetHigherPrecision(TPrecision left, TPrecision right)
34{
35 return left > right ? left : right;
36}
37
Arun Patole274f0702015-05-05 13:33:30 +053038TConstantUnion *Vectorize(const TConstantUnion &constant, size_t size)
39{
40 TConstantUnion *constUnion = new TConstantUnion[size];
41 for (unsigned int i = 0; i < size; ++i)
42 constUnion[i] = constant;
43
44 return constUnion;
45}
46
Olli Etuahof119a262016-08-19 15:54:22 +030047void UndefinedConstantFoldingError(const TSourceLoc &loc,
48 TOperator op,
49 TBasicType basicType,
50 TDiagnostics *diagnostics,
51 TConstantUnion *result)
Arun Patolebf790422015-05-18 17:53:04 +053052{
Olli Etuahof119a262016-08-19 15:54:22 +030053 diagnostics->warning(loc, "operation result is undefined for the values passed in",
54 GetOperatorString(op), "");
Arun Patolebf790422015-05-18 17:53:04 +053055
56 switch (basicType)
57 {
58 case EbtFloat :
59 result->setFConst(0.0f);
60 break;
61 case EbtInt:
62 result->setIConst(0);
63 break;
64 case EbtUInt:
65 result->setUConst(0u);
66 break;
67 case EbtBool:
68 result->setBConst(false);
69 break;
70 default:
71 break;
72 }
73}
74
Olli Etuaho5c0e0232015-11-11 15:55:59 +020075float VectorLength(const TConstantUnion *paramArray, size_t paramArraySize)
Arun Patole1155ddd2015-06-05 18:04:36 +053076{
77 float result = 0.0f;
78 for (size_t i = 0; i < paramArraySize; i++)
79 {
80 float f = paramArray[i].getFConst();
81 result += f * f;
82 }
83 return sqrtf(result);
84}
85
Olli Etuaho5c0e0232015-11-11 15:55:59 +020086float VectorDotProduct(const TConstantUnion *paramArray1,
87 const TConstantUnion *paramArray2,
88 size_t paramArraySize)
Arun Patole1155ddd2015-06-05 18:04:36 +053089{
90 float result = 0.0f;
91 for (size_t i = 0; i < paramArraySize; i++)
92 result += paramArray1[i].getFConst() * paramArray2[i].getFConst();
93 return result;
94}
95
Olli Etuaho7c3848e2015-11-04 13:19:17 +020096TIntermTyped *CreateFoldedNode(TConstantUnion *constArray,
97 const TIntermTyped *originalNode,
98 TQualifier qualifier)
Olli Etuahob43846e2015-06-02 18:18:57 +030099{
100 if (constArray == nullptr)
101 {
102 return nullptr;
103 }
104 TIntermTyped *folded = new TIntermConstantUnion(constArray, originalNode->getType());
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200105 folded->getTypePointer()->setQualifier(qualifier);
Olli Etuahob43846e2015-06-02 18:18:57 +0300106 folded->setLine(originalNode->getLine());
107 return folded;
108}
109
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200110angle::Matrix<float> GetMatrix(const TConstantUnion *paramArray,
111 const unsigned int &rows,
112 const unsigned int &cols)
Arun Patole7fa33552015-06-10 15:15:18 +0530113{
114 std::vector<float> elements;
115 for (size_t i = 0; i < rows * cols; i++)
116 elements.push_back(paramArray[i].getFConst());
117 // Transpose is used since the Matrix constructor expects arguments in row-major order,
Olli Etuahod5da5052016-08-29 13:16:55 +0300118 // whereas the paramArray is in column-major order. Rows/cols parameters are also flipped below
119 // so that the created matrix will have the expected dimensions after the transpose.
120 return angle::Matrix<float>(elements, cols, rows).transpose();
Arun Patole7fa33552015-06-10 15:15:18 +0530121}
122
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200123angle::Matrix<float> GetMatrix(const TConstantUnion *paramArray, const unsigned int &size)
Arun Patole7fa33552015-06-10 15:15:18 +0530124{
125 std::vector<float> elements;
126 for (size_t i = 0; i < size * size; i++)
127 elements.push_back(paramArray[i].getFConst());
128 // Transpose is used since the Matrix constructor expects arguments in row-major order,
129 // whereas the paramArray is in column-major order.
130 return angle::Matrix<float>(elements, size).transpose();
131}
132
133void SetUnionArrayFromMatrix(const angle::Matrix<float> &m, TConstantUnion *resultArray)
134{
135 // Transpose is used since the input Matrix is in row-major order,
136 // whereas the actual result should be in column-major order.
137 angle::Matrix<float> result = m.transpose();
138 std::vector<float> resultElements = result.elements();
139 for (size_t i = 0; i < resultElements.size(); i++)
140 resultArray[i].setFConst(resultElements[i]);
141}
142
Jamie Madillb1a85f42014-08-19 15:23:24 -0400143} // namespace anonymous
144
145
146////////////////////////////////////////////////////////////////
147//
148// Member functions of the nodes used for building the tree.
149//
150////////////////////////////////////////////////////////////////
151
Olli Etuahod2a67b92014-10-21 16:42:57 +0300152void TIntermTyped::setTypePreservePrecision(const TType &t)
153{
154 TPrecision precision = getPrecision();
155 mType = t;
156 ASSERT(mType.getBasicType() != EbtBool || precision == EbpUndefined);
157 mType.setPrecision(precision);
158}
159
Jamie Madillb1a85f42014-08-19 15:23:24 -0400160#define REPLACE_IF_IS(node, type, original, replacement) \
161 if (node == original) { \
162 node = static_cast<type *>(replacement); \
163 return true; \
164 }
165
166bool TIntermLoop::replaceChildNode(
167 TIntermNode *original, TIntermNode *replacement)
168{
Olli Etuaho3cbb27a2016-07-14 11:55:48 +0300169 ASSERT(original != nullptr); // This risks replacing multiple children.
Jamie Madillb1a85f42014-08-19 15:23:24 -0400170 REPLACE_IF_IS(mInit, TIntermNode, original, replacement);
171 REPLACE_IF_IS(mCond, TIntermTyped, original, replacement);
172 REPLACE_IF_IS(mExpr, TIntermTyped, original, replacement);
Olli Etuaho3fed4302015-11-02 12:26:02 +0200173 REPLACE_IF_IS(mBody, TIntermAggregate, original, replacement);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400174 return false;
175}
176
Jamie Madillb1a85f42014-08-19 15:23:24 -0400177bool TIntermBranch::replaceChildNode(
178 TIntermNode *original, TIntermNode *replacement)
179{
180 REPLACE_IF_IS(mExpression, TIntermTyped, original, replacement);
181 return false;
182}
183
Jamie Madillb1a85f42014-08-19 15:23:24 -0400184bool TIntermBinary::replaceChildNode(
185 TIntermNode *original, TIntermNode *replacement)
186{
187 REPLACE_IF_IS(mLeft, TIntermTyped, original, replacement);
188 REPLACE_IF_IS(mRight, TIntermTyped, original, replacement);
189 return false;
190}
191
Jamie Madillb1a85f42014-08-19 15:23:24 -0400192bool TIntermUnary::replaceChildNode(
193 TIntermNode *original, TIntermNode *replacement)
194{
195 REPLACE_IF_IS(mOperand, TIntermTyped, original, replacement);
196 return false;
197}
198
Jamie Madillb1a85f42014-08-19 15:23:24 -0400199bool TIntermAggregate::replaceChildNode(
200 TIntermNode *original, TIntermNode *replacement)
201{
202 for (size_t ii = 0; ii < mSequence.size(); ++ii)
203 {
204 REPLACE_IF_IS(mSequence[ii], TIntermNode, original, replacement);
205 }
206 return false;
207}
208
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300209bool TIntermAggregate::replaceChildNodeWithMultiple(TIntermNode *original, TIntermSequence replacements)
210{
211 for (auto it = mSequence.begin(); it < mSequence.end(); ++it)
212 {
213 if (*it == original)
214 {
215 it = mSequence.erase(it);
Olli Etuaho8fee0ab2015-04-23 14:52:46 +0300216 mSequence.insert(it, replacements.begin(), replacements.end());
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300217 return true;
218 }
219 }
220 return false;
221}
222
Olli Etuahoa6f22092015-05-08 18:31:10 +0300223bool TIntermAggregate::insertChildNodes(TIntermSequence::size_type position, TIntermSequence insertions)
224{
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300225 if (position > mSequence.size())
Olli Etuahoa6f22092015-05-08 18:31:10 +0300226 {
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300227 return false;
Olli Etuahoa6f22092015-05-08 18:31:10 +0300228 }
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300229 auto it = mSequence.begin() + position;
230 mSequence.insert(it, insertions.begin(), insertions.end());
231 return true;
Olli Etuahoa6f22092015-05-08 18:31:10 +0300232}
233
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200234bool TIntermAggregate::areChildrenConstQualified()
235{
236 for (TIntermNode *&child : mSequence)
237 {
238 TIntermTyped *typed = child->getAsTyped();
239 if (typed && typed->getQualifier() != EvqConst)
240 {
241 return false;
242 }
243 }
244 return true;
245}
246
Olli Etuahod2a67b92014-10-21 16:42:57 +0300247void TIntermAggregate::setPrecisionFromChildren()
248{
Olli Etuahoa4aa4e32015-06-04 15:54:30 +0300249 mGotPrecisionFromChildren = true;
Olli Etuahod2a67b92014-10-21 16:42:57 +0300250 if (getBasicType() == EbtBool)
251 {
252 mType.setPrecision(EbpUndefined);
253 return;
254 }
255
256 TPrecision precision = EbpUndefined;
257 TIntermSequence::iterator childIter = mSequence.begin();
258 while (childIter != mSequence.end())
259 {
260 TIntermTyped *typed = (*childIter)->getAsTyped();
261 if (typed)
262 precision = GetHigherPrecision(typed->getPrecision(), precision);
263 ++childIter;
264 }
265 mType.setPrecision(precision);
266}
267
268void TIntermAggregate::setBuiltInFunctionPrecision()
269{
270 // All built-ins returning bool should be handled as ops, not functions.
271 ASSERT(getBasicType() != EbtBool);
272
273 TPrecision precision = EbpUndefined;
274 TIntermSequence::iterator childIter = mSequence.begin();
275 while (childIter != mSequence.end())
276 {
277 TIntermTyped *typed = (*childIter)->getAsTyped();
278 // ESSL spec section 8: texture functions get their precision from the sampler.
279 if (typed && IsSampler(typed->getBasicType()))
280 {
281 precision = typed->getPrecision();
282 break;
283 }
284 ++childIter;
285 }
286 // ESSL 3.0 spec section 8: textureSize always gets highp precision.
287 // All other functions that take a sampler are assumed to be texture functions.
Olli Etuaho59f9a642015-08-06 20:38:26 +0300288 if (mName.getString().find("textureSize") == 0)
Olli Etuahod2a67b92014-10-21 16:42:57 +0300289 mType.setPrecision(EbpHigh);
290 else
291 mType.setPrecision(precision);
292}
293
Jamie Madillb1a85f42014-08-19 15:23:24 -0400294bool TIntermSelection::replaceChildNode(
295 TIntermNode *original, TIntermNode *replacement)
296{
297 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
298 REPLACE_IF_IS(mTrueBlock, TIntermNode, original, replacement);
299 REPLACE_IF_IS(mFalseBlock, TIntermNode, original, replacement);
300 return false;
301}
302
Olli Etuahoa3a36662015-02-17 13:46:51 +0200303bool TIntermSwitch::replaceChildNode(
304 TIntermNode *original, TIntermNode *replacement)
305{
306 REPLACE_IF_IS(mInit, TIntermTyped, original, replacement);
307 REPLACE_IF_IS(mStatementList, TIntermAggregate, original, replacement);
308 return false;
309}
310
311bool TIntermCase::replaceChildNode(
312 TIntermNode *original, TIntermNode *replacement)
313{
314 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
315 return false;
316}
317
Olli Etuahod7a25242015-08-18 13:49:45 +0300318TIntermTyped::TIntermTyped(const TIntermTyped &node) : TIntermNode(), mType(node.mType)
319{
320 // Copy constructor is disallowed for TIntermNode in order to disallow it for subclasses that
321 // don't explicitly allow it, so normal TIntermNode constructor is used to construct the copy.
322 // We need to manually copy any fields of TIntermNode besides handling fields in TIntermTyped.
323 mLine = node.mLine;
324}
325
Olli Etuahod4f4c112016-04-15 15:11:24 +0300326bool TIntermTyped::isConstructorWithOnlyConstantUnionParameters()
327{
328 TIntermAggregate *constructor = getAsAggregate();
329 if (!constructor || !constructor->isConstructor())
330 {
331 return false;
332 }
333 for (TIntermNode *&node : *constructor->getSequence())
334 {
335 if (!node->getAsConstantUnion())
336 return false;
337 }
338 return true;
339}
340
Corentin Wallez509e4562016-08-25 14:55:44 -0400341// static
342TIntermTyped *TIntermTyped::CreateIndexNode(int index)
343{
344 TConstantUnion *u = new TConstantUnion[1];
345 u[0].setIConst(index);
346
347 TType type(EbtInt, EbpUndefined, EvqConst, 1);
348 TIntermConstantUnion *node = new TIntermConstantUnion(u, type);
349 return node;
350}
351
352// static
353TIntermTyped *TIntermTyped::CreateZero(const TType &type)
354{
355 TType constType(type);
356 constType.setQualifier(EvqConst);
357
358 if (!type.isArray() && type.getBasicType() != EbtStruct)
359 {
360 ASSERT(type.isScalar() || type.isVector() || type.isMatrix());
361
362 size_t size = constType.getObjectSize();
363 TConstantUnion *u = new TConstantUnion[size];
364 for (size_t i = 0; i < size; ++i)
365 {
366 switch (type.getBasicType())
367 {
368 case EbtFloat:
369 u[i].setFConst(0.0f);
370 break;
371 case EbtInt:
372 u[i].setIConst(0);
373 break;
374 case EbtUInt:
375 u[i].setUConst(0u);
376 break;
377 case EbtBool:
378 u[i].setBConst(false);
379 break;
380 default:
381 UNREACHABLE();
382 return nullptr;
383 }
384 }
385
386 TIntermConstantUnion *node = new TIntermConstantUnion(u, constType);
387 return node;
388 }
389
390 TIntermAggregate *constructor = new TIntermAggregate(sh::TypeToConstructorOperator(type));
391 constructor->setType(constType);
392
393 if (type.isArray())
394 {
395 TType elementType(type);
396 elementType.clearArrayness();
397
398 size_t arraySize = type.getArraySize();
399 for (size_t i = 0; i < arraySize; ++i)
400 {
401 constructor->getSequence()->push_back(CreateZero(elementType));
402 }
403 }
404 else
405 {
406 ASSERT(type.getBasicType() == EbtStruct);
407
408 TStructure *structure = type.getStruct();
409 for (const auto &field : structure->fields())
410 {
411 constructor->getSequence()->push_back(CreateZero(*field->type()));
412 }
413 }
414
415 return constructor;
416}
417
Olli Etuahod7a25242015-08-18 13:49:45 +0300418TIntermConstantUnion::TIntermConstantUnion(const TIntermConstantUnion &node) : TIntermTyped(node)
419{
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200420 mUnionArrayPointer = node.mUnionArrayPointer;
Olli Etuahod7a25242015-08-18 13:49:45 +0300421}
422
423TIntermAggregate::TIntermAggregate(const TIntermAggregate &node)
424 : TIntermOperator(node),
425 mName(node.mName),
426 mUserDefined(node.mUserDefined),
427 mFunctionId(node.mFunctionId),
Olli Etuahod7a25242015-08-18 13:49:45 +0300428 mUseEmulatedFunction(node.mUseEmulatedFunction),
429 mGotPrecisionFromChildren(node.mGotPrecisionFromChildren)
430{
431 for (TIntermNode *child : node.mSequence)
432 {
433 TIntermTyped *typedChild = child->getAsTyped();
434 ASSERT(typedChild != nullptr);
435 TIntermTyped *childCopy = typedChild->deepCopy();
436 mSequence.push_back(childCopy);
437 }
438}
439
440TIntermBinary::TIntermBinary(const TIntermBinary &node)
441 : TIntermOperator(node), mAddIndexClamp(node.mAddIndexClamp)
442{
443 TIntermTyped *leftCopy = node.mLeft->deepCopy();
444 TIntermTyped *rightCopy = node.mRight->deepCopy();
445 ASSERT(leftCopy != nullptr && rightCopy != nullptr);
446 mLeft = leftCopy;
447 mRight = rightCopy;
448}
449
450TIntermUnary::TIntermUnary(const TIntermUnary &node)
451 : TIntermOperator(node), mUseEmulatedFunction(node.mUseEmulatedFunction)
452{
453 TIntermTyped *operandCopy = node.mOperand->deepCopy();
454 ASSERT(operandCopy != nullptr);
455 mOperand = operandCopy;
456}
457
458TIntermSelection::TIntermSelection(const TIntermSelection &node) : TIntermTyped(node)
459{
460 // Only supported for ternary nodes, not if statements.
461 TIntermTyped *trueTyped = node.mTrueBlock->getAsTyped();
462 TIntermTyped *falseTyped = node.mFalseBlock->getAsTyped();
463 ASSERT(trueTyped != nullptr);
464 ASSERT(falseTyped != nullptr);
465 TIntermTyped *conditionCopy = node.mCondition->deepCopy();
466 TIntermTyped *trueCopy = trueTyped->deepCopy();
467 TIntermTyped *falseCopy = falseTyped->deepCopy();
468 ASSERT(conditionCopy != nullptr && trueCopy != nullptr && falseCopy != nullptr);
469 mCondition = conditionCopy;
470 mTrueBlock = trueCopy;
471 mFalseBlock = falseCopy;
472}
473
Jamie Madillb1a85f42014-08-19 15:23:24 -0400474bool TIntermOperator::isAssignment() const
475{
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300476 return IsAssignment(mOp);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400477}
478
Olli Etuaho8f76bcc2015-06-02 13:54:20 +0300479bool TIntermOperator::isMultiplication() const
480{
481 switch (mOp)
482 {
483 case EOpMul:
484 case EOpMatrixTimesMatrix:
485 case EOpMatrixTimesVector:
486 case EOpMatrixTimesScalar:
487 case EOpVectorTimesMatrix:
488 case EOpVectorTimesScalar:
489 return true;
490 default:
491 return false;
492 }
493}
494
Jamie Madillb1a85f42014-08-19 15:23:24 -0400495//
496// returns true if the operator is for one of the constructors
497//
498bool TIntermOperator::isConstructor() const
499{
500 switch (mOp)
501 {
502 case EOpConstructVec2:
503 case EOpConstructVec3:
504 case EOpConstructVec4:
505 case EOpConstructMat2:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400506 case EOpConstructMat2x3:
507 case EOpConstructMat2x4:
508 case EOpConstructMat3x2:
Jamie Madillb1a85f42014-08-19 15:23:24 -0400509 case EOpConstructMat3:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400510 case EOpConstructMat3x4:
511 case EOpConstructMat4x2:
512 case EOpConstructMat4x3:
Jamie Madillb1a85f42014-08-19 15:23:24 -0400513 case EOpConstructMat4:
514 case EOpConstructFloat:
515 case EOpConstructIVec2:
516 case EOpConstructIVec3:
517 case EOpConstructIVec4:
518 case EOpConstructInt:
519 case EOpConstructUVec2:
520 case EOpConstructUVec3:
521 case EOpConstructUVec4:
522 case EOpConstructUInt:
523 case EOpConstructBVec2:
524 case EOpConstructBVec3:
525 case EOpConstructBVec4:
526 case EOpConstructBool:
527 case EOpConstructStruct:
528 return true;
529 default:
530 return false;
531 }
532}
533
Olli Etuaho1dded802016-08-18 18:13:13 +0300534TOperator TIntermBinary::GetMulOpBasedOnOperands(const TType &left, const TType &right)
535{
536 if (left.isMatrix())
537 {
538 if (right.isMatrix())
539 {
540 return EOpMatrixTimesMatrix;
541 }
542 else
543 {
544 if (right.isVector())
545 {
546 return EOpMatrixTimesVector;
547 }
548 else
549 {
550 return EOpMatrixTimesScalar;
551 }
552 }
553 }
554 else
555 {
556 if (right.isMatrix())
557 {
558 if (left.isVector())
559 {
560 return EOpVectorTimesMatrix;
561 }
562 else
563 {
564 return EOpMatrixTimesScalar;
565 }
566 }
567 else
568 {
569 // Neither operand is a matrix.
570 if (left.isVector() == right.isVector())
571 {
572 // Leave as component product.
573 return EOpMul;
574 }
575 else
576 {
577 return EOpVectorTimesScalar;
578 }
579 }
580 }
581}
582
583TOperator TIntermBinary::GetMulAssignOpBasedOnOperands(const TType &left, const TType &right)
584{
585 if (left.isMatrix())
586 {
587 if (right.isMatrix())
588 {
589 return EOpMatrixTimesMatrixAssign;
590 }
591 else
592 {
593 // right should be scalar, but this may not be validated yet.
594 return EOpMatrixTimesScalarAssign;
595 }
596 }
597 else
598 {
599 if (right.isMatrix())
600 {
601 // Left should be a vector, but this may not be validated yet.
602 return EOpVectorTimesMatrixAssign;
603 }
604 else
605 {
606 // Neither operand is a matrix.
607 if (left.isVector() == right.isVector())
608 {
609 // Leave as component product.
610 return EOpMulAssign;
611 }
612 else
613 {
614 // left should be vector and right should be scalar, but this may not be validated
615 // yet.
616 return EOpVectorTimesScalarAssign;
617 }
618 }
619 }
620}
621
Jamie Madillb1a85f42014-08-19 15:23:24 -0400622//
623// Make sure the type of a unary operator is appropriate for its
624// combination of operation and operand type.
625//
Olli Etuahof6c694b2015-03-26 14:50:53 +0200626void TIntermUnary::promote(const TType *funcReturnType)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400627{
628 switch (mOp)
629 {
Olli Etuahodca3e792015-03-26 13:24:04 +0200630 case EOpFloatBitsToInt:
631 case EOpFloatBitsToUint:
Olli Etuahoe8d2c072015-01-08 16:33:54 +0200632 case EOpIntBitsToFloat:
633 case EOpUintBitsToFloat:
Olli Etuahodca3e792015-03-26 13:24:04 +0200634 case EOpPackSnorm2x16:
635 case EOpPackUnorm2x16:
636 case EOpPackHalf2x16:
Olli Etuaho7700ff62015-01-15 12:16:29 +0200637 case EOpUnpackSnorm2x16:
638 case EOpUnpackUnorm2x16:
Olli Etuahodca3e792015-03-26 13:24:04 +0200639 mType.setPrecision(EbpHigh);
Arun Patole6b19d762015-02-19 09:40:39 +0530640 break;
Olli Etuahodca3e792015-03-26 13:24:04 +0200641 case EOpUnpackHalf2x16:
642 mType.setPrecision(EbpMedium);
643 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400644 default:
Olli Etuahodca3e792015-03-26 13:24:04 +0200645 setType(mOperand->getType());
Jamie Madillb1a85f42014-08-19 15:23:24 -0400646 }
647
Olli Etuahof6c694b2015-03-26 14:50:53 +0200648 if (funcReturnType != nullptr)
649 {
650 if (funcReturnType->getBasicType() == EbtBool)
651 {
652 // Bool types should not have precision.
653 setType(*funcReturnType);
654 }
655 else
656 {
657 // Precision of the node has been set based on the operand.
658 setTypePreservePrecision(*funcReturnType);
659 }
660 }
661
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200662 if (mOperand->getQualifier() == EvqConst)
663 mType.setQualifier(EvqConst);
664 else
665 mType.setQualifier(EvqTemporary);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400666}
667
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300668TIntermBinary::TIntermBinary(TOperator op, TIntermTyped *left, TIntermTyped *right)
669 : TIntermOperator(op), mLeft(left), mRight(right), mAddIndexClamp(false)
670{
671 promote();
672}
673
Jamie Madillb1a85f42014-08-19 15:23:24 -0400674//
675// Establishes the type of the resultant operation, as well as
676// makes the operator the correct one for the operands.
677//
Olli Etuaho47fd36a2015-03-19 14:22:24 +0200678// For lots of operations it should already be established that the operand
679// combination is valid, but returns false if operator can't work on operands.
Jamie Madillb1a85f42014-08-19 15:23:24 -0400680//
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300681void TIntermBinary::promote()
Jamie Madillb1a85f42014-08-19 15:23:24 -0400682{
Olli Etuahoe79904c2015-03-18 16:56:42 +0200683 ASSERT(mLeft->isArray() == mRight->isArray());
Jamie Madillb1a85f42014-08-19 15:23:24 -0400684
Olli Etuaho1dded802016-08-18 18:13:13 +0300685 ASSERT(!isMultiplication() ||
686 mOp == GetMulOpBasedOnOperands(mLeft->getType(), mRight->getType()));
687
Jamie Madillb1a85f42014-08-19 15:23:24 -0400688 // Base assumption: just make the type the same as the left
689 // operand. Then only deviations from this need be coded.
Jamie Madillb1a85f42014-08-19 15:23:24 -0400690 setType(mLeft->getType());
691
692 // The result gets promoted to the highest precision.
693 TPrecision higherPrecision = GetHigherPrecision(
694 mLeft->getPrecision(), mRight->getPrecision());
695 getTypePointer()->setPrecision(higherPrecision);
696
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200697 TQualifier resultQualifier = EvqConst;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400698 // Binary operations results in temporary variables unless both
699 // operands are const.
700 if (mLeft->getQualifier() != EvqConst || mRight->getQualifier() != EvqConst)
701 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200702 resultQualifier = EvqTemporary;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400703 getTypePointer()->setQualifier(EvqTemporary);
704 }
705
706 const int nominalSize =
707 std::max(mLeft->getNominalSize(), mRight->getNominalSize());
708
709 //
710 // All scalars or structs. Code after this test assumes this case is removed!
711 //
712 if (nominalSize == 1)
713 {
714 switch (mOp)
715 {
716 //
717 // Promote to conditional
718 //
719 case EOpEqual:
720 case EOpNotEqual:
721 case EOpLessThan:
722 case EOpGreaterThan:
723 case EOpLessThanEqual:
724 case EOpGreaterThanEqual:
725 setType(TType(EbtBool, EbpUndefined));
726 break;
727
728 //
729 // And and Or operate on conditionals
730 //
731 case EOpLogicalAnd:
Olli Etuaho47fd36a2015-03-19 14:22:24 +0200732 case EOpLogicalXor:
Jamie Madillb1a85f42014-08-19 15:23:24 -0400733 case EOpLogicalOr:
Olli Etuaho47fd36a2015-03-19 14:22:24 +0200734 ASSERT(mLeft->getBasicType() == EbtBool && mRight->getBasicType() == EbtBool);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400735 setType(TType(EbtBool, EbpUndefined));
736 break;
737
738 default:
739 break;
740 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300741 return;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400742 }
743
744 // If we reach here, at least one of the operands is vector or matrix.
745 // The other operand could be a scalar, vector, or matrix.
Jamie Madillb1a85f42014-08-19 15:23:24 -0400746 TBasicType basicType = mLeft->getBasicType();
Olli Etuaho1dded802016-08-18 18:13:13 +0300747
Jamie Madillb1a85f42014-08-19 15:23:24 -0400748 switch (mOp)
749 {
Olli Etuaho1dded802016-08-18 18:13:13 +0300750 case EOpMul:
751 break;
752 case EOpMatrixTimesScalar:
753 if (mRight->isMatrix())
Jamie Madillb1a85f42014-08-19 15:23:24 -0400754 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200755 setType(TType(basicType, higherPrecision, resultQualifier,
756 static_cast<unsigned char>(mRight->getCols()),
757 static_cast<unsigned char>(mRight->getRows())));
Jamie Madillb1a85f42014-08-19 15:23:24 -0400758 }
Olli Etuaho1dded802016-08-18 18:13:13 +0300759 break;
760 case EOpMatrixTimesVector:
761 setType(TType(basicType, higherPrecision, resultQualifier,
762 static_cast<unsigned char>(mLeft->getRows()), 1));
763 break;
764 case EOpMatrixTimesMatrix:
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200765 setType(TType(basicType, higherPrecision, resultQualifier,
766 static_cast<unsigned char>(mRight->getCols()),
767 static_cast<unsigned char>(mLeft->getRows())));
Olli Etuaho1dded802016-08-18 18:13:13 +0300768 break;
769 case EOpVectorTimesScalar:
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200770 setType(TType(basicType, higherPrecision, resultQualifier,
Olli Etuaho1dded802016-08-18 18:13:13 +0300771 static_cast<unsigned char>(nominalSize), 1));
772 break;
773 case EOpVectorTimesMatrix:
774 setType(TType(basicType, higherPrecision, resultQualifier,
775 static_cast<unsigned char>(mRight->getCols()), 1));
776 break;
777 case EOpMulAssign:
778 case EOpVectorTimesScalarAssign:
779 case EOpVectorTimesMatrixAssign:
780 case EOpMatrixTimesScalarAssign:
781 case EOpMatrixTimesMatrixAssign:
782 ASSERT(mOp == GetMulAssignOpBasedOnOperands(mLeft->getType(), mRight->getType()));
783 break;
784 case EOpAssign:
785 case EOpInitialize:
Olli Etuaho1dded802016-08-18 18:13:13 +0300786 ASSERT((mLeft->getNominalSize() == mRight->getNominalSize()) &&
787 (mLeft->getSecondarySize() == mRight->getSecondarySize()));
788 break;
789 case EOpAdd:
790 case EOpSub:
791 case EOpDiv:
792 case EOpIMod:
793 case EOpBitShiftLeft:
794 case EOpBitShiftRight:
795 case EOpBitwiseAnd:
796 case EOpBitwiseXor:
797 case EOpBitwiseOr:
798 case EOpAddAssign:
799 case EOpSubAssign:
800 case EOpDivAssign:
801 case EOpIModAssign:
802 case EOpBitShiftLeftAssign:
803 case EOpBitShiftRightAssign:
804 case EOpBitwiseAndAssign:
805 case EOpBitwiseXorAssign:
806 case EOpBitwiseOrAssign:
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300807 {
808 const int secondarySize =
809 std::max(mLeft->getSecondarySize(), mRight->getSecondarySize());
810 setType(TType(basicType, higherPrecision, resultQualifier,
811 static_cast<unsigned char>(nominalSize),
812 static_cast<unsigned char>(secondarySize)));
813 ASSERT(!mLeft->isArray() && !mRight->isArray());
Olli Etuaho1dded802016-08-18 18:13:13 +0300814 break;
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300815 }
Olli Etuaho1dded802016-08-18 18:13:13 +0300816 case EOpEqual:
817 case EOpNotEqual:
818 case EOpLessThan:
819 case EOpGreaterThan:
820 case EOpLessThanEqual:
821 case EOpGreaterThanEqual:
822 ASSERT((mLeft->getNominalSize() == mRight->getNominalSize()) &&
823 (mLeft->getSecondarySize() == mRight->getSecondarySize()));
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300824 setType(TType(EbtBool, EbpUndefined, resultQualifier));
Olli Etuaho1dded802016-08-18 18:13:13 +0300825 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400826
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300827 case EOpIndexDirect:
828 case EOpIndexIndirect:
829 case EOpIndexDirectInterfaceBlock:
830 case EOpIndexDirectStruct:
831 // TODO (oetuaho): These ops could be handled here as well (should be done closer to the
832 // top of the function).
833 UNREACHABLE();
834 break;
Olli Etuaho1dded802016-08-18 18:13:13 +0300835 default:
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300836 UNREACHABLE();
837 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400838 }
Jamie Madillb1a85f42014-08-19 15:23:24 -0400839}
840
Olli Etuaho3fdec912016-08-18 15:08:06 +0300841TIntermTyped *TIntermBinary::fold(TDiagnostics *diagnostics)
Olli Etuaho2c4b7462015-06-08 11:30:31 +0300842{
843 TIntermConstantUnion *leftConstant = mLeft->getAsConstantUnion();
844 TIntermConstantUnion *rightConstant = mRight->getAsConstantUnion();
845 if (leftConstant == nullptr || rightConstant == nullptr)
846 {
847 return nullptr;
848 }
Olli Etuaho3fdec912016-08-18 15:08:06 +0300849 TConstantUnion *constArray = leftConstant->foldBinary(mOp, rightConstant, diagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200850
851 // Nodes may be constant folded without being qualified as constant.
852 TQualifier resultQualifier = EvqConst;
853 if (mLeft->getQualifier() != EvqConst || mRight->getQualifier() != EvqConst)
854 {
855 resultQualifier = EvqTemporary;
856 }
857 return CreateFoldedNode(constArray, this, resultQualifier);
Olli Etuaho2c4b7462015-06-08 11:30:31 +0300858}
859
Olli Etuahof119a262016-08-19 15:54:22 +0300860TIntermTyped *TIntermUnary::fold(TDiagnostics *diagnostics)
Olli Etuaho95310b02015-06-02 17:43:38 +0300861{
862 TIntermConstantUnion *operandConstant = mOperand->getAsConstantUnion();
863 if (operandConstant == nullptr)
864 {
865 return nullptr;
866 }
Arun Patoleab2b9a22015-07-06 18:27:56 +0530867
868 TConstantUnion *constArray = nullptr;
869 switch (mOp)
870 {
871 case EOpAny:
872 case EOpAll:
873 case EOpLength:
874 case EOpTranspose:
875 case EOpDeterminant:
876 case EOpInverse:
877 case EOpPackSnorm2x16:
878 case EOpUnpackSnorm2x16:
879 case EOpPackUnorm2x16:
880 case EOpUnpackUnorm2x16:
881 case EOpPackHalf2x16:
882 case EOpUnpackHalf2x16:
Olli Etuahof119a262016-08-19 15:54:22 +0300883 constArray = operandConstant->foldUnaryNonComponentWise(mOp);
884 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +0530885 default:
Olli Etuahof119a262016-08-19 15:54:22 +0300886 constArray = operandConstant->foldUnaryComponentWise(mOp, diagnostics);
887 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +0530888 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200889
890 // Nodes may be constant folded without being qualified as constant.
891 TQualifier resultQualifier = mOperand->getQualifier() == EvqConst ? EvqConst : EvqTemporary;
892 return CreateFoldedNode(constArray, this, resultQualifier);
Olli Etuahob43846e2015-06-02 18:18:57 +0300893}
894
Olli Etuahof119a262016-08-19 15:54:22 +0300895TIntermTyped *TIntermAggregate::fold(TDiagnostics *diagnostics)
Olli Etuahob43846e2015-06-02 18:18:57 +0300896{
897 // Make sure that all params are constant before actual constant folding.
898 for (auto *param : *getSequence())
Olli Etuaho95310b02015-06-02 17:43:38 +0300899 {
Olli Etuahob43846e2015-06-02 18:18:57 +0300900 if (param->getAsConstantUnion() == nullptr)
901 {
902 return nullptr;
903 }
Olli Etuaho95310b02015-06-02 17:43:38 +0300904 }
Olli Etuaho1d122782015-11-06 15:35:17 +0200905 TConstantUnion *constArray = nullptr;
906 if (isConstructor())
Olli Etuahof119a262016-08-19 15:54:22 +0300907 constArray = TIntermConstantUnion::FoldAggregateConstructor(this);
Olli Etuaho1d122782015-11-06 15:35:17 +0200908 else
Olli Etuahof119a262016-08-19 15:54:22 +0300909 constArray = TIntermConstantUnion::FoldAggregateBuiltIn(this, diagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200910
911 // Nodes may be constant folded without being qualified as constant.
912 TQualifier resultQualifier = areChildrenConstQualified() ? EvqConst : EvqTemporary;
913 return CreateFoldedNode(constArray, this, resultQualifier);
Olli Etuaho95310b02015-06-02 17:43:38 +0300914}
915
Jamie Madillb1a85f42014-08-19 15:23:24 -0400916//
917// The fold functions see if an operation on a constant can be done in place,
918// without generating run-time code.
919//
Olli Etuaho2c4b7462015-06-08 11:30:31 +0300920// Returns the constant value to keep using or nullptr.
Jamie Madillb1a85f42014-08-19 15:23:24 -0400921//
Olli Etuaho3fdec912016-08-18 15:08:06 +0300922TConstantUnion *TIntermConstantUnion::foldBinary(TOperator op,
923 TIntermConstantUnion *rightNode,
924 TDiagnostics *diagnostics)
Olli Etuaho2c4b7462015-06-08 11:30:31 +0300925{
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200926 const TConstantUnion *leftArray = getUnionArrayPointer();
927 const TConstantUnion *rightArray = rightNode->getUnionArrayPointer();
Olli Etuaho2c4b7462015-06-08 11:30:31 +0300928
Olli Etuahof119a262016-08-19 15:54:22 +0300929 ASSERT(leftArray && rightArray);
Olli Etuaho2c4b7462015-06-08 11:30:31 +0300930
931 size_t objectSize = getType().getObjectSize();
932
933 // for a case like float f = vec4(2, 3, 4, 5) + 1.2;
934 if (rightNode->getType().getObjectSize() == 1 && objectSize > 1)
935 {
936 rightArray = Vectorize(*rightNode->getUnionArrayPointer(), objectSize);
937 }
938 else if (rightNode->getType().getObjectSize() > 1 && objectSize == 1)
939 {
940 // for a case like float f = 1.2 + vec4(2, 3, 4, 5);
941 leftArray = Vectorize(*getUnionArrayPointer(), rightNode->getType().getObjectSize());
942 objectSize = rightNode->getType().getObjectSize();
943 }
944
945 TConstantUnion *resultArray = nullptr;
946
947 switch(op)
948 {
949 case EOpAdd:
950 resultArray = new TConstantUnion[objectSize];
951 for (size_t i = 0; i < objectSize; i++)
952 resultArray[i] = leftArray[i] + rightArray[i];
953 break;
954 case EOpSub:
955 resultArray = new TConstantUnion[objectSize];
956 for (size_t i = 0; i < objectSize; i++)
957 resultArray[i] = leftArray[i] - rightArray[i];
958 break;
959
960 case EOpMul:
961 case EOpVectorTimesScalar:
962 case EOpMatrixTimesScalar:
963 resultArray = new TConstantUnion[objectSize];
964 for (size_t i = 0; i < objectSize; i++)
965 resultArray[i] = leftArray[i] * rightArray[i];
966 break;
967
968 case EOpMatrixTimesMatrix:
969 {
Olli Etuaho3fdec912016-08-18 15:08:06 +0300970 ASSERT(getType().getBasicType() == EbtFloat && rightNode->getBasicType() == EbtFloat);
Olli Etuaho2c4b7462015-06-08 11:30:31 +0300971
972 const int leftCols = getCols();
973 const int leftRows = getRows();
974 const int rightCols = rightNode->getType().getCols();
975 const int rightRows = rightNode->getType().getRows();
976 const int resultCols = rightCols;
977 const int resultRows = leftRows;
978
979 resultArray = new TConstantUnion[resultCols * resultRows];
980 for (int row = 0; row < resultRows; row++)
981 {
982 for (int column = 0; column < resultCols; column++)
983 {
984 resultArray[resultRows * column + row].setFConst(0.0f);
985 for (int i = 0; i < leftCols; i++)
986 {
987 resultArray[resultRows * column + row].setFConst(
988 resultArray[resultRows * column + row].getFConst() +
989 leftArray[i * leftRows + row].getFConst() *
990 rightArray[column * rightRows + i].getFConst());
991 }
992 }
993 }
994 }
995 break;
996
997 case EOpDiv:
998 case EOpIMod:
999 {
1000 resultArray = new TConstantUnion[objectSize];
1001 for (size_t i = 0; i < objectSize; i++)
1002 {
1003 switch (getType().getBasicType())
1004 {
1005 case EbtFloat:
1006 if (rightArray[i] == 0.0f)
1007 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001008 diagnostics->warning(
1009 getLine(), "Divide by zero error during constant folding", "/", "");
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001010 resultArray[i].setFConst(leftArray[i].getFConst() < 0 ? -FLT_MAX : FLT_MAX);
1011 }
1012 else
1013 {
1014 ASSERT(op == EOpDiv);
1015 resultArray[i].setFConst(leftArray[i].getFConst() / rightArray[i].getFConst());
1016 }
1017 break;
1018
1019 case EbtInt:
1020 if (rightArray[i] == 0)
1021 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001022 diagnostics->warning(
1023 getLine(), "Divide by zero error during constant folding", "/", "");
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001024 resultArray[i].setIConst(INT_MAX);
1025 }
1026 else
1027 {
1028 if (op == EOpDiv)
1029 {
1030 resultArray[i].setIConst(leftArray[i].getIConst() / rightArray[i].getIConst());
1031 }
1032 else
1033 {
1034 ASSERT(op == EOpIMod);
1035 resultArray[i].setIConst(leftArray[i].getIConst() % rightArray[i].getIConst());
1036 }
1037 }
1038 break;
1039
1040 case EbtUInt:
1041 if (rightArray[i] == 0)
1042 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001043 diagnostics->warning(
1044 getLine(), "Divide by zero error during constant folding", "/", "");
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001045 resultArray[i].setUConst(UINT_MAX);
1046 }
1047 else
1048 {
1049 if (op == EOpDiv)
1050 {
1051 resultArray[i].setUConst(leftArray[i].getUConst() / rightArray[i].getUConst());
1052 }
1053 else
1054 {
1055 ASSERT(op == EOpIMod);
1056 resultArray[i].setUConst(leftArray[i].getUConst() % rightArray[i].getUConst());
1057 }
1058 }
1059 break;
1060
1061 default:
Olli Etuaho3fdec912016-08-18 15:08:06 +03001062 UNREACHABLE();
1063 return nullptr;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001064 }
1065 }
1066 }
1067 break;
1068
1069 case EOpMatrixTimesVector:
1070 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001071 ASSERT(rightNode->getBasicType() == EbtFloat);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001072
1073 const int matrixCols = getCols();
1074 const int matrixRows = getRows();
1075
1076 resultArray = new TConstantUnion[matrixRows];
1077
1078 for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++)
1079 {
1080 resultArray[matrixRow].setFConst(0.0f);
1081 for (int col = 0; col < matrixCols; col++)
1082 {
1083 resultArray[matrixRow].setFConst(resultArray[matrixRow].getFConst() +
1084 leftArray[col * matrixRows + matrixRow].getFConst() *
1085 rightArray[col].getFConst());
1086 }
1087 }
1088 }
1089 break;
1090
1091 case EOpVectorTimesMatrix:
1092 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001093 ASSERT(getType().getBasicType() == EbtFloat);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001094
1095 const int matrixCols = rightNode->getType().getCols();
1096 const int matrixRows = rightNode->getType().getRows();
1097
1098 resultArray = new TConstantUnion[matrixCols];
1099
1100 for (int matrixCol = 0; matrixCol < matrixCols; matrixCol++)
1101 {
1102 resultArray[matrixCol].setFConst(0.0f);
1103 for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++)
1104 {
1105 resultArray[matrixCol].setFConst(resultArray[matrixCol].getFConst() +
1106 leftArray[matrixRow].getFConst() *
1107 rightArray[matrixCol * matrixRows + matrixRow].getFConst());
1108 }
1109 }
1110 }
1111 break;
1112
1113 case EOpLogicalAnd:
1114 {
1115 resultArray = new TConstantUnion[objectSize];
1116 for (size_t i = 0; i < objectSize; i++)
1117 {
1118 resultArray[i] = leftArray[i] && rightArray[i];
1119 }
1120 }
1121 break;
1122
1123 case EOpLogicalOr:
1124 {
1125 resultArray = new TConstantUnion[objectSize];
1126 for (size_t i = 0; i < objectSize; i++)
1127 {
1128 resultArray[i] = leftArray[i] || rightArray[i];
1129 }
1130 }
1131 break;
1132
1133 case EOpLogicalXor:
1134 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001135 ASSERT(getType().getBasicType() == EbtBool);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001136 resultArray = new TConstantUnion[objectSize];
1137 for (size_t i = 0; i < objectSize; i++)
1138 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001139 resultArray[i].setBConst(leftArray[i] != rightArray[i]);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001140 }
1141 }
1142 break;
1143
1144 case EOpBitwiseAnd:
1145 resultArray = new TConstantUnion[objectSize];
1146 for (size_t i = 0; i < objectSize; i++)
1147 resultArray[i] = leftArray[i] & rightArray[i];
1148 break;
1149 case EOpBitwiseXor:
1150 resultArray = new TConstantUnion[objectSize];
1151 for (size_t i = 0; i < objectSize; i++)
1152 resultArray[i] = leftArray[i] ^ rightArray[i];
1153 break;
1154 case EOpBitwiseOr:
1155 resultArray = new TConstantUnion[objectSize];
1156 for (size_t i = 0; i < objectSize; i++)
1157 resultArray[i] = leftArray[i] | rightArray[i];
1158 break;
1159 case EOpBitShiftLeft:
1160 resultArray = new TConstantUnion[objectSize];
1161 for (size_t i = 0; i < objectSize; i++)
1162 resultArray[i] = leftArray[i] << rightArray[i];
1163 break;
1164 case EOpBitShiftRight:
1165 resultArray = new TConstantUnion[objectSize];
1166 for (size_t i = 0; i < objectSize; i++)
1167 resultArray[i] = leftArray[i] >> rightArray[i];
1168 break;
1169
1170 case EOpLessThan:
1171 ASSERT(objectSize == 1);
1172 resultArray = new TConstantUnion[1];
1173 resultArray->setBConst(*leftArray < *rightArray);
1174 break;
1175
1176 case EOpGreaterThan:
1177 ASSERT(objectSize == 1);
1178 resultArray = new TConstantUnion[1];
1179 resultArray->setBConst(*leftArray > *rightArray);
1180 break;
1181
1182 case EOpLessThanEqual:
1183 ASSERT(objectSize == 1);
1184 resultArray = new TConstantUnion[1];
1185 resultArray->setBConst(!(*leftArray > *rightArray));
1186 break;
1187
1188 case EOpGreaterThanEqual:
1189 ASSERT(objectSize == 1);
1190 resultArray = new TConstantUnion[1];
1191 resultArray->setBConst(!(*leftArray < *rightArray));
1192 break;
1193
1194 case EOpEqual:
1195 case EOpNotEqual:
1196 {
1197 resultArray = new TConstantUnion[1];
1198 bool equal = true;
Olli Etuaho40d9edf2015-11-12 17:30:34 +02001199 for (size_t i = 0; i < objectSize; i++)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001200 {
Olli Etuaho40d9edf2015-11-12 17:30:34 +02001201 if (leftArray[i] != rightArray[i])
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001202 {
Olli Etuaho40d9edf2015-11-12 17:30:34 +02001203 equal = false;
1204 break; // break out of for loop
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001205 }
1206 }
1207 if (op == EOpEqual)
1208 {
1209 resultArray->setBConst(equal);
1210 }
1211 else
1212 {
1213 resultArray->setBConst(!equal);
1214 }
1215 }
1216 break;
1217
1218 default:
Olli Etuaho3fdec912016-08-18 15:08:06 +03001219 UNREACHABLE();
1220 return nullptr;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001221 }
1222 return resultArray;
1223}
1224
Olli Etuahof119a262016-08-19 15:54:22 +03001225// The fold functions do operations on a constant at GLSL compile time, without generating run-time
1226// code. Returns the constant value to keep using. Nullptr should not be returned.
1227TConstantUnion *TIntermConstantUnion::foldUnaryNonComponentWise(TOperator op)
Jamie Madillb1a85f42014-08-19 15:23:24 -04001228{
Olli Etuahof119a262016-08-19 15:54:22 +03001229 // Do operations where the return type may have a different number of components compared to the
1230 // operand type.
Jamie Madillb1a85f42014-08-19 15:23:24 -04001231
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001232 const TConstantUnion *operandArray = getUnionArrayPointer();
Olli Etuahof119a262016-08-19 15:54:22 +03001233 ASSERT(operandArray);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301234
1235 size_t objectSize = getType().getObjectSize();
1236 TConstantUnion *resultArray = nullptr;
1237 switch (op)
1238 {
Olli Etuahof119a262016-08-19 15:54:22 +03001239 case EOpAny:
1240 ASSERT(getType().getBasicType() == EbtBool);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301241 resultArray = new TConstantUnion();
1242 resultArray->setBConst(false);
1243 for (size_t i = 0; i < objectSize; i++)
1244 {
1245 if (operandArray[i].getBConst())
1246 {
1247 resultArray->setBConst(true);
1248 break;
1249 }
1250 }
1251 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301252
Olli Etuahof119a262016-08-19 15:54:22 +03001253 case EOpAll:
1254 ASSERT(getType().getBasicType() == EbtBool);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301255 resultArray = new TConstantUnion();
1256 resultArray->setBConst(true);
1257 for (size_t i = 0; i < objectSize; i++)
1258 {
1259 if (!operandArray[i].getBConst())
1260 {
1261 resultArray->setBConst(false);
1262 break;
1263 }
1264 }
1265 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301266
Olli Etuahof119a262016-08-19 15:54:22 +03001267 case EOpLength:
1268 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301269 resultArray = new TConstantUnion();
1270 resultArray->setFConst(VectorLength(operandArray, objectSize));
1271 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301272
Olli Etuahof119a262016-08-19 15:54:22 +03001273 case EOpTranspose:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301274 {
Olli Etuahof119a262016-08-19 15:54:22 +03001275 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301276 resultArray = new TConstantUnion[objectSize];
1277 angle::Matrix<float> result =
Olli Etuahod5da5052016-08-29 13:16:55 +03001278 GetMatrix(operandArray, getType().getRows(), getType().getCols()).transpose();
Arun Patoleab2b9a22015-07-06 18:27:56 +05301279 SetUnionArrayFromMatrix(result, resultArray);
1280 break;
1281 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301282
Olli Etuahof119a262016-08-19 15:54:22 +03001283 case EOpDeterminant:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301284 {
Olli Etuahof119a262016-08-19 15:54:22 +03001285 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301286 unsigned int size = getType().getNominalSize();
1287 ASSERT(size >= 2 && size <= 4);
1288 resultArray = new TConstantUnion();
1289 resultArray->setFConst(GetMatrix(operandArray, size).determinant());
1290 break;
1291 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301292
Olli Etuahof119a262016-08-19 15:54:22 +03001293 case EOpInverse:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301294 {
Olli Etuahof119a262016-08-19 15:54:22 +03001295 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301296 unsigned int size = getType().getNominalSize();
1297 ASSERT(size >= 2 && size <= 4);
Olli Etuahof119a262016-08-19 15:54:22 +03001298 resultArray = new TConstantUnion[objectSize];
Arun Patoleab2b9a22015-07-06 18:27:56 +05301299 angle::Matrix<float> result = GetMatrix(operandArray, size).inverse();
1300 SetUnionArrayFromMatrix(result, resultArray);
1301 break;
1302 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301303
Olli Etuahof119a262016-08-19 15:54:22 +03001304 case EOpPackSnorm2x16:
1305 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301306 ASSERT(getType().getNominalSize() == 2);
1307 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03001308 resultArray->setUConst(
1309 gl::packSnorm2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
Arun Patoleab2b9a22015-07-06 18:27:56 +05301310 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301311
Olli Etuahof119a262016-08-19 15:54:22 +03001312 case EOpUnpackSnorm2x16:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301313 {
Olli Etuahof119a262016-08-19 15:54:22 +03001314 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301315 resultArray = new TConstantUnion[2];
1316 float f1, f2;
1317 gl::unpackSnorm2x16(operandArray[0].getUConst(), &f1, &f2);
1318 resultArray[0].setFConst(f1);
1319 resultArray[1].setFConst(f2);
1320 break;
1321 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301322
Olli Etuahof119a262016-08-19 15:54:22 +03001323 case EOpPackUnorm2x16:
1324 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301325 ASSERT(getType().getNominalSize() == 2);
1326 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03001327 resultArray->setUConst(
1328 gl::packUnorm2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
Arun Patoleab2b9a22015-07-06 18:27:56 +05301329 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301330
Olli Etuahof119a262016-08-19 15:54:22 +03001331 case EOpUnpackUnorm2x16:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301332 {
Olli Etuahof119a262016-08-19 15:54:22 +03001333 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301334 resultArray = new TConstantUnion[2];
1335 float f1, f2;
1336 gl::unpackUnorm2x16(operandArray[0].getUConst(), &f1, &f2);
1337 resultArray[0].setFConst(f1);
1338 resultArray[1].setFConst(f2);
1339 break;
1340 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301341
Olli Etuahof119a262016-08-19 15:54:22 +03001342 case EOpPackHalf2x16:
1343 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301344 ASSERT(getType().getNominalSize() == 2);
1345 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03001346 resultArray->setUConst(
1347 gl::packHalf2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
Arun Patoleab2b9a22015-07-06 18:27:56 +05301348 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301349
Olli Etuahof119a262016-08-19 15:54:22 +03001350 case EOpUnpackHalf2x16:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301351 {
Olli Etuahof119a262016-08-19 15:54:22 +03001352 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301353 resultArray = new TConstantUnion[2];
1354 float f1, f2;
1355 gl::unpackHalf2x16(operandArray[0].getUConst(), &f1, &f2);
1356 resultArray[0].setFConst(f1);
1357 resultArray[1].setFConst(f2);
1358 break;
1359 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301360
Olli Etuahof119a262016-08-19 15:54:22 +03001361 default:
1362 UNREACHABLE();
1363 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301364 }
1365
1366 return resultArray;
1367}
1368
Olli Etuahof119a262016-08-19 15:54:22 +03001369TConstantUnion *TIntermConstantUnion::foldUnaryComponentWise(TOperator op,
1370 TDiagnostics *diagnostics)
Arun Patoleab2b9a22015-07-06 18:27:56 +05301371{
Olli Etuahof119a262016-08-19 15:54:22 +03001372 // Do unary operations where each component of the result is computed based on the corresponding
1373 // component of the operand. Also folds normalize, though the divisor in that case takes all
1374 // components into account.
Arun Patoleab2b9a22015-07-06 18:27:56 +05301375
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001376 const TConstantUnion *operandArray = getUnionArrayPointer();
Olli Etuahof119a262016-08-19 15:54:22 +03001377 ASSERT(operandArray);
Jamie Madillb1a85f42014-08-19 15:23:24 -04001378
1379 size_t objectSize = getType().getObjectSize();
1380
Arun Patoleab2b9a22015-07-06 18:27:56 +05301381 TConstantUnion *resultArray = new TConstantUnion[objectSize];
1382 for (size_t i = 0; i < objectSize; i++)
Arun Patole9d0b1f92015-05-20 14:27:17 +05301383 {
Arun Patoleab2b9a22015-07-06 18:27:56 +05301384 switch(op)
Arun Patole9d0b1f92015-05-20 14:27:17 +05301385 {
Olli Etuahof119a262016-08-19 15:54:22 +03001386 case EOpNegative:
1387 switch (getType().getBasicType())
1388 {
1389 case EbtFloat:
1390 resultArray[i].setFConst(-operandArray[i].getFConst());
1391 break;
1392 case EbtInt:
1393 resultArray[i].setIConst(-operandArray[i].getIConst());
1394 break;
1395 case EbtUInt:
1396 resultArray[i].setUConst(static_cast<unsigned int>(
1397 -static_cast<int>(operandArray[i].getUConst())));
1398 break;
1399 default:
1400 UNREACHABLE();
1401 return nullptr;
1402 }
Arun Patole1155ddd2015-06-05 18:04:36 +05301403 break;
Arun Patolecdfa8f52015-06-30 17:48:25 +05301404
Olli Etuahof119a262016-08-19 15:54:22 +03001405 case EOpPositive:
1406 switch (getType().getBasicType())
1407 {
1408 case EbtFloat:
1409 resultArray[i].setFConst(operandArray[i].getFConst());
1410 break;
1411 case EbtInt:
1412 resultArray[i].setIConst(operandArray[i].getIConst());
1413 break;
1414 case EbtUInt:
1415 resultArray[i].setUConst(static_cast<unsigned int>(
1416 static_cast<int>(operandArray[i].getUConst())));
1417 break;
1418 default:
1419 UNREACHABLE();
1420 return nullptr;
1421 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301422 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301423
Olli Etuahof119a262016-08-19 15:54:22 +03001424 case EOpLogicalNot:
1425 switch (getType().getBasicType())
1426 {
1427 case EbtBool:
1428 resultArray[i].setBConst(!operandArray[i].getBConst());
1429 break;
1430 default:
1431 UNREACHABLE();
1432 return nullptr;
1433 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301434 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301435
Olli Etuahof119a262016-08-19 15:54:22 +03001436 case EOpBitwiseNot:
1437 switch (getType().getBasicType())
1438 {
1439 case EbtInt:
1440 resultArray[i].setIConst(~operandArray[i].getIConst());
1441 break;
1442 case EbtUInt:
1443 resultArray[i].setUConst(~operandArray[i].getUConst());
1444 break;
1445 default:
1446 UNREACHABLE();
1447 return nullptr;
1448 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301449 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301450
Olli Etuahof119a262016-08-19 15:54:22 +03001451 case EOpRadians:
1452 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301453 resultArray[i].setFConst(kDegreesToRadiansMultiplier * operandArray[i].getFConst());
1454 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301455
Olli Etuahof119a262016-08-19 15:54:22 +03001456 case EOpDegrees:
1457 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301458 resultArray[i].setFConst(kRadiansToDegreesMultiplier * operandArray[i].getFConst());
1459 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301460
Olli Etuahof119a262016-08-19 15:54:22 +03001461 case EOpSin:
1462 foldFloatTypeUnary(operandArray[i], &sinf, &resultArray[i]);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301463 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301464
Olli Etuahof119a262016-08-19 15:54:22 +03001465 case EOpCos:
1466 foldFloatTypeUnary(operandArray[i], &cosf, &resultArray[i]);
1467 break;
1468
1469 case EOpTan:
1470 foldFloatTypeUnary(operandArray[i], &tanf, &resultArray[i]);
1471 break;
1472
1473 case EOpAsin:
1474 // For asin(x), results are undefined if |x| > 1, we are choosing to set result to
1475 // 0.
1476 if (fabsf(operandArray[i].getFConst()) > 1.0f)
1477 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1478 diagnostics, &resultArray[i]);
1479 else
1480 foldFloatTypeUnary(operandArray[i], &asinf, &resultArray[i]);
1481 break;
1482
1483 case EOpAcos:
1484 // For acos(x), results are undefined if |x| > 1, we are choosing to set result to
1485 // 0.
1486 if (fabsf(operandArray[i].getFConst()) > 1.0f)
1487 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1488 diagnostics, &resultArray[i]);
1489 else
1490 foldFloatTypeUnary(operandArray[i], &acosf, &resultArray[i]);
1491 break;
1492
1493 case EOpAtan:
1494 foldFloatTypeUnary(operandArray[i], &atanf, &resultArray[i]);
1495 break;
1496
1497 case EOpSinh:
1498 foldFloatTypeUnary(operandArray[i], &sinhf, &resultArray[i]);
1499 break;
1500
1501 case EOpCosh:
1502 foldFloatTypeUnary(operandArray[i], &coshf, &resultArray[i]);
1503 break;
1504
1505 case EOpTanh:
1506 foldFloatTypeUnary(operandArray[i], &tanhf, &resultArray[i]);
1507 break;
1508
1509 case EOpAsinh:
1510 foldFloatTypeUnary(operandArray[i], &asinhf, &resultArray[i]);
1511 break;
1512
1513 case EOpAcosh:
1514 // For acosh(x), results are undefined if x < 1, we are choosing to set result to 0.
1515 if (operandArray[i].getFConst() < 1.0f)
1516 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1517 diagnostics, &resultArray[i]);
1518 else
1519 foldFloatTypeUnary(operandArray[i], &acoshf, &resultArray[i]);
1520 break;
1521
1522 case EOpAtanh:
1523 // For atanh(x), results are undefined if |x| >= 1, we are choosing to set result to
1524 // 0.
1525 if (fabsf(operandArray[i].getFConst()) >= 1.0f)
1526 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1527 diagnostics, &resultArray[i]);
1528 else
1529 foldFloatTypeUnary(operandArray[i], &atanhf, &resultArray[i]);
1530 break;
1531
1532 case EOpAbs:
1533 switch (getType().getBasicType())
Arun Patoleab2b9a22015-07-06 18:27:56 +05301534 {
Olli Etuahof119a262016-08-19 15:54:22 +03001535 case EbtFloat:
1536 resultArray[i].setFConst(fabsf(operandArray[i].getFConst()));
1537 break;
1538 case EbtInt:
1539 resultArray[i].setIConst(abs(operandArray[i].getIConst()));
1540 break;
1541 default:
1542 UNREACHABLE();
1543 return nullptr;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301544 }
1545 break;
Olli Etuahof119a262016-08-19 15:54:22 +03001546
1547 case EOpSign:
1548 switch (getType().getBasicType())
Arun Patoleab2b9a22015-07-06 18:27:56 +05301549 {
Olli Etuahof119a262016-08-19 15:54:22 +03001550 case EbtFloat:
1551 {
1552 float fConst = operandArray[i].getFConst();
1553 float fResult = 0.0f;
1554 if (fConst > 0.0f)
1555 fResult = 1.0f;
1556 else if (fConst < 0.0f)
1557 fResult = -1.0f;
1558 resultArray[i].setFConst(fResult);
1559 break;
1560 }
1561 case EbtInt:
1562 {
1563 int iConst = operandArray[i].getIConst();
1564 int iResult = 0;
1565 if (iConst > 0)
1566 iResult = 1;
1567 else if (iConst < 0)
1568 iResult = -1;
1569 resultArray[i].setIConst(iResult);
1570 break;
1571 }
1572 default:
1573 UNREACHABLE();
1574 return nullptr;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301575 }
1576 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301577
Olli Etuahof119a262016-08-19 15:54:22 +03001578 case EOpFloor:
1579 foldFloatTypeUnary(operandArray[i], &floorf, &resultArray[i]);
1580 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301581
Olli Etuahof119a262016-08-19 15:54:22 +03001582 case EOpTrunc:
1583 foldFloatTypeUnary(operandArray[i], &truncf, &resultArray[i]);
1584 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301585
Olli Etuahof119a262016-08-19 15:54:22 +03001586 case EOpRound:
1587 foldFloatTypeUnary(operandArray[i], &roundf, &resultArray[i]);
1588 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301589
Olli Etuahof119a262016-08-19 15:54:22 +03001590 case EOpRoundEven:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301591 {
Olli Etuahof119a262016-08-19 15:54:22 +03001592 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301593 float x = operandArray[i].getFConst();
1594 float result;
1595 float fractPart = modff(x, &result);
1596 if (fabsf(fractPart) == 0.5f)
1597 result = 2.0f * roundf(x / 2.0f);
1598 else
1599 result = roundf(x);
1600 resultArray[i].setFConst(result);
1601 break;
1602 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301603
Olli Etuahof119a262016-08-19 15:54:22 +03001604 case EOpCeil:
1605 foldFloatTypeUnary(operandArray[i], &ceilf, &resultArray[i]);
1606 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301607
Olli Etuahof119a262016-08-19 15:54:22 +03001608 case EOpFract:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301609 {
Olli Etuahof119a262016-08-19 15:54:22 +03001610 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301611 float x = operandArray[i].getFConst();
1612 resultArray[i].setFConst(x - floorf(x));
1613 break;
1614 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301615
Olli Etuahof119a262016-08-19 15:54:22 +03001616 case EOpIsNan:
1617 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05301618 resultArray[i].setBConst(gl::isNaN(operandArray[0].getFConst()));
1619 break;
Arun Patole551279e2015-07-07 18:18:23 +05301620
Olli Etuahof119a262016-08-19 15:54:22 +03001621 case EOpIsInf:
1622 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05301623 resultArray[i].setBConst(gl::isInf(operandArray[0].getFConst()));
1624 break;
Arun Patole551279e2015-07-07 18:18:23 +05301625
Olli Etuahof119a262016-08-19 15:54:22 +03001626 case EOpFloatBitsToInt:
1627 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05301628 resultArray[i].setIConst(gl::bitCast<int32_t>(operandArray[0].getFConst()));
1629 break;
Arun Patole551279e2015-07-07 18:18:23 +05301630
Olli Etuahof119a262016-08-19 15:54:22 +03001631 case EOpFloatBitsToUint:
1632 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05301633 resultArray[i].setUConst(gl::bitCast<uint32_t>(operandArray[0].getFConst()));
1634 break;
Arun Patole551279e2015-07-07 18:18:23 +05301635
Olli Etuahof119a262016-08-19 15:54:22 +03001636 case EOpIntBitsToFloat:
1637 ASSERT(getType().getBasicType() == EbtInt);
Arun Patole551279e2015-07-07 18:18:23 +05301638 resultArray[i].setFConst(gl::bitCast<float>(operandArray[0].getIConst()));
1639 break;
Arun Patole551279e2015-07-07 18:18:23 +05301640
Olli Etuahof119a262016-08-19 15:54:22 +03001641 case EOpUintBitsToFloat:
1642 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patole551279e2015-07-07 18:18:23 +05301643 resultArray[i].setFConst(gl::bitCast<float>(operandArray[0].getUConst()));
1644 break;
Arun Patole551279e2015-07-07 18:18:23 +05301645
Olli Etuahof119a262016-08-19 15:54:22 +03001646 case EOpExp:
1647 foldFloatTypeUnary(operandArray[i], &expf, &resultArray[i]);
1648 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301649
Olli Etuahof119a262016-08-19 15:54:22 +03001650 case EOpLog:
1651 // For log(x), results are undefined if x <= 0, we are choosing to set result to 0.
1652 if (operandArray[i].getFConst() <= 0.0f)
1653 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1654 diagnostics, &resultArray[i]);
1655 else
1656 foldFloatTypeUnary(operandArray[i], &logf, &resultArray[i]);
1657 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301658
Olli Etuahof119a262016-08-19 15:54:22 +03001659 case EOpExp2:
1660 foldFloatTypeUnary(operandArray[i], &exp2f, &resultArray[i]);
1661 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301662
Olli Etuahof119a262016-08-19 15:54:22 +03001663 case EOpLog2:
1664 // For log2(x), results are undefined if x <= 0, we are choosing to set result to 0.
1665 // And log2f is not available on some plarforms like old android, so just using
1666 // log(x)/log(2) here.
1667 if (operandArray[i].getFConst() <= 0.0f)
1668 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1669 diagnostics, &resultArray[i]);
1670 else
1671 {
1672 foldFloatTypeUnary(operandArray[i], &logf, &resultArray[i]);
1673 resultArray[i].setFConst(resultArray[i].getFConst() / logf(2.0f));
1674 }
1675 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301676
Olli Etuahof119a262016-08-19 15:54:22 +03001677 case EOpSqrt:
1678 // For sqrt(x), results are undefined if x < 0, we are choosing to set result to 0.
1679 if (operandArray[i].getFConst() < 0.0f)
1680 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1681 diagnostics, &resultArray[i]);
1682 else
1683 foldFloatTypeUnary(operandArray[i], &sqrtf, &resultArray[i]);
1684 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301685
Olli Etuahof119a262016-08-19 15:54:22 +03001686 case EOpInverseSqrt:
1687 // There is no stdlib built-in function equavalent for GLES built-in inversesqrt(),
1688 // so getting the square root first using builtin function sqrt() and then taking
1689 // its inverse.
1690 // Also, for inversesqrt(x), results are undefined if x <= 0, we are choosing to set
1691 // result to 0.
1692 if (operandArray[i].getFConst() <= 0.0f)
1693 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1694 diagnostics, &resultArray[i]);
1695 else
1696 {
1697 foldFloatTypeUnary(operandArray[i], &sqrtf, &resultArray[i]);
1698 resultArray[i].setFConst(1.0f / resultArray[i].getFConst());
1699 }
1700 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301701
Olli Etuahof119a262016-08-19 15:54:22 +03001702 case EOpVectorLogicalNot:
1703 ASSERT(getType().getBasicType() == EbtBool);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301704 resultArray[i].setBConst(!operandArray[i].getBConst());
1705 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301706
Olli Etuahof119a262016-08-19 15:54:22 +03001707 case EOpNormalize:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301708 {
Olli Etuahof119a262016-08-19 15:54:22 +03001709 ASSERT(getType().getBasicType() == EbtFloat);
1710 float x = operandArray[i].getFConst();
Arun Patoleab2b9a22015-07-06 18:27:56 +05301711 float length = VectorLength(operandArray, objectSize);
1712 if (length)
1713 resultArray[i].setFConst(x / length);
1714 else
Olli Etuahof119a262016-08-19 15:54:22 +03001715 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1716 diagnostics, &resultArray[i]);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301717 break;
1718 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301719
Olli Etuahof119a262016-08-19 15:54:22 +03001720 case EOpDFdx:
1721 case EOpDFdy:
1722 case EOpFwidth:
1723 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole0c5409f2015-07-08 15:17:53 +05301724 // Derivatives of constant arguments should be 0.
1725 resultArray[i].setFConst(0.0f);
1726 break;
Arun Patole0c5409f2015-07-08 15:17:53 +05301727
Olli Etuahof119a262016-08-19 15:54:22 +03001728 default:
1729 return nullptr;
Arun Patole9d0b1f92015-05-20 14:27:17 +05301730 }
Arun Patole9d0b1f92015-05-20 14:27:17 +05301731 }
Jamie Madillb1a85f42014-08-19 15:23:24 -04001732
Arun Patoleab2b9a22015-07-06 18:27:56 +05301733 return resultArray;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001734}
1735
Olli Etuahof119a262016-08-19 15:54:22 +03001736void TIntermConstantUnion::foldFloatTypeUnary(const TConstantUnion &parameter,
1737 FloatTypeUnaryFunc builtinFunc,
1738 TConstantUnion *result) const
Arun Patole9dea48f2015-04-02 11:45:09 +05301739{
1740 ASSERT(builtinFunc);
1741
Olli Etuahof119a262016-08-19 15:54:22 +03001742 ASSERT(getType().getBasicType() == EbtFloat);
1743 result->setFConst(builtinFunc(parameter.getFConst()));
Arun Patole9dea48f2015-04-02 11:45:09 +05301744}
1745
Jamie Madillb1a85f42014-08-19 15:23:24 -04001746// static
Olli Etuahof119a262016-08-19 15:54:22 +03001747TConstantUnion *TIntermConstantUnion::FoldAggregateConstructor(TIntermAggregate *aggregate)
Olli Etuaho1d122782015-11-06 15:35:17 +02001748{
1749 ASSERT(aggregate->getSequence()->size() > 0u);
1750 size_t resultSize = aggregate->getType().getObjectSize();
1751 TConstantUnion *resultArray = new TConstantUnion[resultSize];
1752 TBasicType basicType = aggregate->getBasicType();
1753
1754 size_t resultIndex = 0u;
1755
1756 if (aggregate->getSequence()->size() == 1u)
1757 {
1758 TIntermNode *argument = aggregate->getSequence()->front();
1759 TIntermConstantUnion *argumentConstant = argument->getAsConstantUnion();
1760 const TConstantUnion *argumentUnionArray = argumentConstant->getUnionArrayPointer();
1761 // Check the special case of constructing a matrix diagonal from a single scalar,
1762 // or a vector from a single scalar.
1763 if (argumentConstant->getType().getObjectSize() == 1u)
1764 {
1765 if (aggregate->isMatrix())
1766 {
1767 int resultCols = aggregate->getType().getCols();
1768 int resultRows = aggregate->getType().getRows();
1769 for (int col = 0; col < resultCols; ++col)
1770 {
1771 for (int row = 0; row < resultRows; ++row)
1772 {
1773 if (col == row)
1774 {
1775 resultArray[resultIndex].cast(basicType, argumentUnionArray[0]);
1776 }
1777 else
1778 {
1779 resultArray[resultIndex].setFConst(0.0f);
1780 }
1781 ++resultIndex;
1782 }
1783 }
1784 }
1785 else
1786 {
1787 while (resultIndex < resultSize)
1788 {
1789 resultArray[resultIndex].cast(basicType, argumentUnionArray[0]);
1790 ++resultIndex;
1791 }
1792 }
1793 ASSERT(resultIndex == resultSize);
1794 return resultArray;
1795 }
1796 else if (aggregate->isMatrix() && argumentConstant->isMatrix())
1797 {
1798 // The special case of constructing a matrix from a matrix.
1799 int argumentCols = argumentConstant->getType().getCols();
1800 int argumentRows = argumentConstant->getType().getRows();
1801 int resultCols = aggregate->getType().getCols();
1802 int resultRows = aggregate->getType().getRows();
1803 for (int col = 0; col < resultCols; ++col)
1804 {
1805 for (int row = 0; row < resultRows; ++row)
1806 {
1807 if (col < argumentCols && row < argumentRows)
1808 {
1809 resultArray[resultIndex].cast(basicType,
1810 argumentUnionArray[col * argumentRows + row]);
1811 }
1812 else if (col == row)
1813 {
1814 resultArray[resultIndex].setFConst(1.0f);
1815 }
1816 else
1817 {
1818 resultArray[resultIndex].setFConst(0.0f);
1819 }
1820 ++resultIndex;
1821 }
1822 }
1823 ASSERT(resultIndex == resultSize);
1824 return resultArray;
1825 }
1826 }
1827
1828 for (TIntermNode *&argument : *aggregate->getSequence())
1829 {
1830 TIntermConstantUnion *argumentConstant = argument->getAsConstantUnion();
1831 size_t argumentSize = argumentConstant->getType().getObjectSize();
1832 const TConstantUnion *argumentUnionArray = argumentConstant->getUnionArrayPointer();
1833 for (size_t i = 0u; i < argumentSize; ++i)
1834 {
1835 if (resultIndex >= resultSize)
1836 break;
1837 resultArray[resultIndex].cast(basicType, argumentUnionArray[i]);
1838 ++resultIndex;
1839 }
1840 }
1841 ASSERT(resultIndex == resultSize);
1842 return resultArray;
1843}
1844
1845// static
Olli Etuahof119a262016-08-19 15:54:22 +03001846TConstantUnion *TIntermConstantUnion::FoldAggregateBuiltIn(TIntermAggregate *aggregate,
1847 TDiagnostics *diagnostics)
Arun Patole274f0702015-05-05 13:33:30 +05301848{
Olli Etuahob43846e2015-06-02 18:18:57 +03001849 TOperator op = aggregate->getOp();
Arun Patole274f0702015-05-05 13:33:30 +05301850 TIntermSequence *sequence = aggregate->getSequence();
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001851 unsigned int paramsCount = static_cast<unsigned int>(sequence->size());
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001852 std::vector<const TConstantUnion *> unionArrays(paramsCount);
Arun Patole274f0702015-05-05 13:33:30 +05301853 std::vector<size_t> objectSizes(paramsCount);
Olli Etuahob43846e2015-06-02 18:18:57 +03001854 size_t maxObjectSize = 0;
Arun Patole274f0702015-05-05 13:33:30 +05301855 TBasicType basicType = EbtVoid;
1856 TSourceLoc loc;
1857 for (unsigned int i = 0; i < paramsCount; i++)
1858 {
1859 TIntermConstantUnion *paramConstant = (*sequence)[i]->getAsConstantUnion();
Olli Etuahob43846e2015-06-02 18:18:57 +03001860 ASSERT(paramConstant != nullptr); // Should be checked already.
Arun Patole274f0702015-05-05 13:33:30 +05301861
1862 if (i == 0)
1863 {
1864 basicType = paramConstant->getType().getBasicType();
1865 loc = paramConstant->getLine();
1866 }
1867 unionArrays[i] = paramConstant->getUnionArrayPointer();
1868 objectSizes[i] = paramConstant->getType().getObjectSize();
Olli Etuahob43846e2015-06-02 18:18:57 +03001869 if (objectSizes[i] > maxObjectSize)
1870 maxObjectSize = objectSizes[i];
Arun Patole274f0702015-05-05 13:33:30 +05301871 }
1872
Olli Etuahod5da5052016-08-29 13:16:55 +03001873 if (!(*sequence)[0]->getAsTyped()->isMatrix() && aggregate->getOp() != EOpOuterProduct)
Arun Patole7fa33552015-06-10 15:15:18 +05301874 {
1875 for (unsigned int i = 0; i < paramsCount; i++)
1876 if (objectSizes[i] != maxObjectSize)
1877 unionArrays[i] = Vectorize(*unionArrays[i], maxObjectSize);
1878 }
Arun Patole274f0702015-05-05 13:33:30 +05301879
Olli Etuahob43846e2015-06-02 18:18:57 +03001880 TConstantUnion *resultArray = nullptr;
Arun Patole274f0702015-05-05 13:33:30 +05301881 if (paramsCount == 2)
1882 {
1883 //
1884 // Binary built-in
1885 //
1886 switch (op)
1887 {
Olli Etuahof119a262016-08-19 15:54:22 +03001888 case EOpAtan:
Arun Patolebf790422015-05-18 17:53:04 +05301889 {
Olli Etuahof119a262016-08-19 15:54:22 +03001890 ASSERT(basicType == EbtFloat);
1891 resultArray = new TConstantUnion[maxObjectSize];
1892 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05301893 {
Olli Etuahof119a262016-08-19 15:54:22 +03001894 float y = unionArrays[0][i].getFConst();
1895 float x = unionArrays[1][i].getFConst();
1896 // Results are undefined if x and y are both 0.
1897 if (x == 0.0f && y == 0.0f)
1898 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
1899 &resultArray[i]);
1900 else
1901 resultArray[i].setFConst(atan2f(y, x));
Arun Patolebf790422015-05-18 17:53:04 +05301902 }
Olli Etuahof119a262016-08-19 15:54:22 +03001903 break;
Arun Patolebf790422015-05-18 17:53:04 +05301904 }
Arun Patolebf790422015-05-18 17:53:04 +05301905
Olli Etuahof119a262016-08-19 15:54:22 +03001906 case EOpPow:
Arun Patolebf790422015-05-18 17:53:04 +05301907 {
Olli Etuahof119a262016-08-19 15:54:22 +03001908 ASSERT(basicType == EbtFloat);
1909 resultArray = new TConstantUnion[maxObjectSize];
1910 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05301911 {
Olli Etuahof119a262016-08-19 15:54:22 +03001912 float x = unionArrays[0][i].getFConst();
1913 float y = unionArrays[1][i].getFConst();
1914 // Results are undefined if x < 0.
1915 // Results are undefined if x = 0 and y <= 0.
1916 if (x < 0.0f)
1917 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
1918 &resultArray[i]);
1919 else if (x == 0.0f && y <= 0.0f)
1920 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
1921 &resultArray[i]);
1922 else
1923 resultArray[i].setFConst(powf(x, y));
Arun Patolebf790422015-05-18 17:53:04 +05301924 }
Olli Etuahof119a262016-08-19 15:54:22 +03001925 break;
Arun Patolebf790422015-05-18 17:53:04 +05301926 }
Arun Patolebf790422015-05-18 17:53:04 +05301927
Olli Etuahof119a262016-08-19 15:54:22 +03001928 case EOpMod:
Arun Patolebf790422015-05-18 17:53:04 +05301929 {
Olli Etuahof119a262016-08-19 15:54:22 +03001930 ASSERT(basicType == EbtFloat);
1931 resultArray = new TConstantUnion[maxObjectSize];
1932 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05301933 {
Olli Etuahof119a262016-08-19 15:54:22 +03001934 float x = unionArrays[0][i].getFConst();
1935 float y = unionArrays[1][i].getFConst();
1936 resultArray[i].setFConst(x - y * floorf(x / y));
Arun Patolebf790422015-05-18 17:53:04 +05301937 }
Olli Etuahof119a262016-08-19 15:54:22 +03001938 break;
Arun Patolebf790422015-05-18 17:53:04 +05301939 }
Arun Patolebf790422015-05-18 17:53:04 +05301940
Olli Etuahof119a262016-08-19 15:54:22 +03001941 case EOpMin:
Arun Patole274f0702015-05-05 13:33:30 +05301942 {
Olli Etuahob43846e2015-06-02 18:18:57 +03001943 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole274f0702015-05-05 13:33:30 +05301944 for (size_t i = 0; i < maxObjectSize; i++)
1945 {
1946 switch (basicType)
1947 {
Olli Etuahof119a262016-08-19 15:54:22 +03001948 case EbtFloat:
1949 resultArray[i].setFConst(std::min(unionArrays[0][i].getFConst(),
1950 unionArrays[1][i].getFConst()));
1951 break;
1952 case EbtInt:
1953 resultArray[i].setIConst(std::min(unionArrays[0][i].getIConst(),
1954 unionArrays[1][i].getIConst()));
1955 break;
1956 case EbtUInt:
1957 resultArray[i].setUConst(std::min(unionArrays[0][i].getUConst(),
1958 unionArrays[1][i].getUConst()));
1959 break;
1960 default:
1961 UNREACHABLE();
1962 break;
Arun Patole274f0702015-05-05 13:33:30 +05301963 }
1964 }
Olli Etuahof119a262016-08-19 15:54:22 +03001965 break;
Arun Patole274f0702015-05-05 13:33:30 +05301966 }
Arun Patole274f0702015-05-05 13:33:30 +05301967
Olli Etuahof119a262016-08-19 15:54:22 +03001968 case EOpMax:
Arun Patole274f0702015-05-05 13:33:30 +05301969 {
Olli Etuahob43846e2015-06-02 18:18:57 +03001970 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole274f0702015-05-05 13:33:30 +05301971 for (size_t i = 0; i < maxObjectSize; i++)
1972 {
1973 switch (basicType)
1974 {
Olli Etuahof119a262016-08-19 15:54:22 +03001975 case EbtFloat:
1976 resultArray[i].setFConst(std::max(unionArrays[0][i].getFConst(),
1977 unionArrays[1][i].getFConst()));
1978 break;
1979 case EbtInt:
1980 resultArray[i].setIConst(std::max(unionArrays[0][i].getIConst(),
1981 unionArrays[1][i].getIConst()));
1982 break;
1983 case EbtUInt:
1984 resultArray[i].setUConst(std::max(unionArrays[0][i].getUConst(),
1985 unionArrays[1][i].getUConst()));
1986 break;
1987 default:
1988 UNREACHABLE();
1989 break;
Arun Patole274f0702015-05-05 13:33:30 +05301990 }
1991 }
Olli Etuahof119a262016-08-19 15:54:22 +03001992 break;
Arun Patole274f0702015-05-05 13:33:30 +05301993 }
Arun Patole274f0702015-05-05 13:33:30 +05301994
Olli Etuahof119a262016-08-19 15:54:22 +03001995 case EOpStep:
Arun Patolebf790422015-05-18 17:53:04 +05301996 {
Olli Etuahof119a262016-08-19 15:54:22 +03001997 ASSERT(basicType == EbtFloat);
1998 resultArray = new TConstantUnion[maxObjectSize];
1999 for (size_t i = 0; i < maxObjectSize; i++)
2000 resultArray[i].setFConst(
2001 unionArrays[1][i].getFConst() < unionArrays[0][i].getFConst() ? 0.0f
2002 : 1.0f);
2003 break;
Arun Patolebf790422015-05-18 17:53:04 +05302004 }
Arun Patolebf790422015-05-18 17:53:04 +05302005
Olli Etuahof119a262016-08-19 15:54:22 +03002006 case EOpLessThan:
Arun Patole9d0b1f92015-05-20 14:27:17 +05302007 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002008 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole9d0b1f92015-05-20 14:27:17 +05302009 for (size_t i = 0; i < maxObjectSize; i++)
2010 {
2011 switch (basicType)
2012 {
Olli Etuahof119a262016-08-19 15:54:22 +03002013 case EbtFloat:
2014 resultArray[i].setBConst(unionArrays[0][i].getFConst() <
2015 unionArrays[1][i].getFConst());
2016 break;
2017 case EbtInt:
2018 resultArray[i].setBConst(unionArrays[0][i].getIConst() <
2019 unionArrays[1][i].getIConst());
2020 break;
2021 case EbtUInt:
2022 resultArray[i].setBConst(unionArrays[0][i].getUConst() <
2023 unionArrays[1][i].getUConst());
2024 break;
2025 default:
2026 UNREACHABLE();
2027 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302028 }
2029 }
Olli Etuahof119a262016-08-19 15:54:22 +03002030 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302031 }
Arun Patole9d0b1f92015-05-20 14:27:17 +05302032
Olli Etuahof119a262016-08-19 15:54:22 +03002033 case EOpLessThanEqual:
Arun Patole9d0b1f92015-05-20 14:27:17 +05302034 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002035 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole9d0b1f92015-05-20 14:27:17 +05302036 for (size_t i = 0; i < maxObjectSize; i++)
2037 {
2038 switch (basicType)
2039 {
Olli Etuahof119a262016-08-19 15:54:22 +03002040 case EbtFloat:
2041 resultArray[i].setBConst(unionArrays[0][i].getFConst() <=
2042 unionArrays[1][i].getFConst());
2043 break;
2044 case EbtInt:
2045 resultArray[i].setBConst(unionArrays[0][i].getIConst() <=
2046 unionArrays[1][i].getIConst());
2047 break;
2048 case EbtUInt:
2049 resultArray[i].setBConst(unionArrays[0][i].getUConst() <=
2050 unionArrays[1][i].getUConst());
2051 break;
2052 default:
2053 UNREACHABLE();
2054 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302055 }
2056 }
Olli Etuahof119a262016-08-19 15:54:22 +03002057 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302058 }
Arun Patole9d0b1f92015-05-20 14:27:17 +05302059
Olli Etuahof119a262016-08-19 15:54:22 +03002060 case EOpGreaterThan:
Arun Patole9d0b1f92015-05-20 14:27:17 +05302061 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002062 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole9d0b1f92015-05-20 14:27:17 +05302063 for (size_t i = 0; i < maxObjectSize; i++)
2064 {
2065 switch (basicType)
2066 {
Olli Etuahof119a262016-08-19 15:54:22 +03002067 case EbtFloat:
2068 resultArray[i].setBConst(unionArrays[0][i].getFConst() >
2069 unionArrays[1][i].getFConst());
2070 break;
2071 case EbtInt:
2072 resultArray[i].setBConst(unionArrays[0][i].getIConst() >
2073 unionArrays[1][i].getIConst());
2074 break;
2075 case EbtUInt:
2076 resultArray[i].setBConst(unionArrays[0][i].getUConst() >
2077 unionArrays[1][i].getUConst());
2078 break;
2079 default:
2080 UNREACHABLE();
2081 break;
Olli Etuahob43846e2015-06-02 18:18:57 +03002082 }
2083 }
Olli Etuahof119a262016-08-19 15:54:22 +03002084 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302085 }
Olli Etuahof119a262016-08-19 15:54:22 +03002086 case EOpGreaterThanEqual:
Arun Patole9d0b1f92015-05-20 14:27:17 +05302087 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002088 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole9d0b1f92015-05-20 14:27:17 +05302089 for (size_t i = 0; i < maxObjectSize; i++)
2090 {
2091 switch (basicType)
2092 {
Olli Etuahof119a262016-08-19 15:54:22 +03002093 case EbtFloat:
2094 resultArray[i].setBConst(unionArrays[0][i].getFConst() >=
2095 unionArrays[1][i].getFConst());
2096 break;
2097 case EbtInt:
2098 resultArray[i].setBConst(unionArrays[0][i].getIConst() >=
2099 unionArrays[1][i].getIConst());
2100 break;
2101 case EbtUInt:
2102 resultArray[i].setBConst(unionArrays[0][i].getUConst() >=
2103 unionArrays[1][i].getUConst());
2104 break;
2105 default:
2106 UNREACHABLE();
2107 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302108 }
2109 }
2110 }
2111 break;
2112
Olli Etuahof119a262016-08-19 15:54:22 +03002113 case EOpVectorEqual:
Arun Patole9d0b1f92015-05-20 14:27:17 +05302114 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002115 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole9d0b1f92015-05-20 14:27:17 +05302116 for (size_t i = 0; i < maxObjectSize; i++)
2117 {
2118 switch (basicType)
2119 {
Olli Etuahof119a262016-08-19 15:54:22 +03002120 case EbtFloat:
2121 resultArray[i].setBConst(unionArrays[0][i].getFConst() ==
2122 unionArrays[1][i].getFConst());
2123 break;
2124 case EbtInt:
2125 resultArray[i].setBConst(unionArrays[0][i].getIConst() ==
2126 unionArrays[1][i].getIConst());
2127 break;
2128 case EbtUInt:
2129 resultArray[i].setBConst(unionArrays[0][i].getUConst() ==
2130 unionArrays[1][i].getUConst());
2131 break;
2132 case EbtBool:
2133 resultArray[i].setBConst(unionArrays[0][i].getBConst() ==
2134 unionArrays[1][i].getBConst());
2135 break;
2136 default:
2137 UNREACHABLE();
2138 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302139 }
2140 }
Olli Etuahof119a262016-08-19 15:54:22 +03002141 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302142 }
Arun Patole9d0b1f92015-05-20 14:27:17 +05302143
Olli Etuahof119a262016-08-19 15:54:22 +03002144 case EOpVectorNotEqual:
Arun Patole9d0b1f92015-05-20 14:27:17 +05302145 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002146 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole9d0b1f92015-05-20 14:27:17 +05302147 for (size_t i = 0; i < maxObjectSize; i++)
2148 {
2149 switch (basicType)
2150 {
Olli Etuahof119a262016-08-19 15:54:22 +03002151 case EbtFloat:
2152 resultArray[i].setBConst(unionArrays[0][i].getFConst() !=
2153 unionArrays[1][i].getFConst());
2154 break;
2155 case EbtInt:
2156 resultArray[i].setBConst(unionArrays[0][i].getIConst() !=
2157 unionArrays[1][i].getIConst());
2158 break;
2159 case EbtUInt:
2160 resultArray[i].setBConst(unionArrays[0][i].getUConst() !=
2161 unionArrays[1][i].getUConst());
2162 break;
2163 case EbtBool:
2164 resultArray[i].setBConst(unionArrays[0][i].getBConst() !=
2165 unionArrays[1][i].getBConst());
2166 break;
2167 default:
2168 UNREACHABLE();
2169 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302170 }
2171 }
Olli Etuahof119a262016-08-19 15:54:22 +03002172 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302173 }
Arun Patole9d0b1f92015-05-20 14:27:17 +05302174
Olli Etuahof119a262016-08-19 15:54:22 +03002175 case EOpDistance:
Arun Patole1155ddd2015-06-05 18:04:36 +05302176 {
Olli Etuahof119a262016-08-19 15:54:22 +03002177 ASSERT(basicType == EbtFloat);
Arun Patole1155ddd2015-06-05 18:04:36 +05302178 TConstantUnion *distanceArray = new TConstantUnion[maxObjectSize];
Olli Etuahof119a262016-08-19 15:54:22 +03002179 resultArray = new TConstantUnion();
Arun Patole1155ddd2015-06-05 18:04:36 +05302180 for (size_t i = 0; i < maxObjectSize; i++)
2181 {
2182 float x = unionArrays[0][i].getFConst();
2183 float y = unionArrays[1][i].getFConst();
2184 distanceArray[i].setFConst(x - y);
2185 }
Olli Etuahob43846e2015-06-02 18:18:57 +03002186 resultArray->setFConst(VectorLength(distanceArray, maxObjectSize));
Olli Etuahof119a262016-08-19 15:54:22 +03002187 break;
Arun Patole1155ddd2015-06-05 18:04:36 +05302188 }
Arun Patole1155ddd2015-06-05 18:04:36 +05302189
Olli Etuahof119a262016-08-19 15:54:22 +03002190 case EOpDot:
2191 ASSERT(basicType == EbtFloat);
Olli Etuahob43846e2015-06-02 18:18:57 +03002192 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03002193 resultArray->setFConst(
2194 VectorDotProduct(unionArrays[0], unionArrays[1], maxObjectSize));
2195 break;
Arun Patole1155ddd2015-06-05 18:04:36 +05302196
Olli Etuahof119a262016-08-19 15:54:22 +03002197 case EOpCross:
Arun Patole1155ddd2015-06-05 18:04:36 +05302198 {
Olli Etuahof119a262016-08-19 15:54:22 +03002199 ASSERT(basicType == EbtFloat && maxObjectSize == 3);
Olli Etuahob43846e2015-06-02 18:18:57 +03002200 resultArray = new TConstantUnion[maxObjectSize];
Olli Etuahof119a262016-08-19 15:54:22 +03002201 float x0 = unionArrays[0][0].getFConst();
2202 float x1 = unionArrays[0][1].getFConst();
2203 float x2 = unionArrays[0][2].getFConst();
2204 float y0 = unionArrays[1][0].getFConst();
2205 float y1 = unionArrays[1][1].getFConst();
2206 float y2 = unionArrays[1][2].getFConst();
Olli Etuahob43846e2015-06-02 18:18:57 +03002207 resultArray[0].setFConst(x1 * y2 - y1 * x2);
2208 resultArray[1].setFConst(x2 * y0 - y2 * x0);
2209 resultArray[2].setFConst(x0 * y1 - y0 * x1);
Olli Etuahof119a262016-08-19 15:54:22 +03002210 break;
Arun Patole1155ddd2015-06-05 18:04:36 +05302211 }
Arun Patole1155ddd2015-06-05 18:04:36 +05302212
Olli Etuahof119a262016-08-19 15:54:22 +03002213 case EOpReflect:
Arun Patole1155ddd2015-06-05 18:04:36 +05302214 {
Olli Etuahof119a262016-08-19 15:54:22 +03002215 ASSERT(basicType == EbtFloat);
Arun Patole1155ddd2015-06-05 18:04:36 +05302216 // genType reflect (genType I, genType N) :
Olli Etuahof119a262016-08-19 15:54:22 +03002217 // For the incident vector I and surface orientation N, returns the reflection
2218 // direction:
Arun Patole1155ddd2015-06-05 18:04:36 +05302219 // I - 2 * dot(N, I) * N.
Olli Etuahof119a262016-08-19 15:54:22 +03002220 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole1155ddd2015-06-05 18:04:36 +05302221 float dotProduct = VectorDotProduct(unionArrays[1], unionArrays[0], maxObjectSize);
2222 for (size_t i = 0; i < maxObjectSize; i++)
2223 {
2224 float result = unionArrays[0][i].getFConst() -
2225 2.0f * dotProduct * unionArrays[1][i].getFConst();
Olli Etuahob43846e2015-06-02 18:18:57 +03002226 resultArray[i].setFConst(result);
Arun Patole1155ddd2015-06-05 18:04:36 +05302227 }
Olli Etuahof119a262016-08-19 15:54:22 +03002228 break;
Arun Patole1155ddd2015-06-05 18:04:36 +05302229 }
Arun Patole1155ddd2015-06-05 18:04:36 +05302230
Olli Etuahof119a262016-08-19 15:54:22 +03002231 case EOpMul:
Arun Patole7fa33552015-06-10 15:15:18 +05302232 {
Olli Etuahof119a262016-08-19 15:54:22 +03002233 ASSERT(basicType == EbtFloat && (*sequence)[0]->getAsTyped()->isMatrix() &&
2234 (*sequence)[1]->getAsTyped()->isMatrix());
Arun Patole7fa33552015-06-10 15:15:18 +05302235 // Perform component-wise matrix multiplication.
2236 resultArray = new TConstantUnion[maxObjectSize];
Olli Etuahof119a262016-08-19 15:54:22 +03002237 int size = (*sequence)[0]->getAsTyped()->getNominalSize();
Arun Patole7fa33552015-06-10 15:15:18 +05302238 angle::Matrix<float> result =
2239 GetMatrix(unionArrays[0], size).compMult(GetMatrix(unionArrays[1], size));
2240 SetUnionArrayFromMatrix(result, resultArray);
Olli Etuahof119a262016-08-19 15:54:22 +03002241 break;
Arun Patole7fa33552015-06-10 15:15:18 +05302242 }
Arun Patole7fa33552015-06-10 15:15:18 +05302243
Olli Etuahof119a262016-08-19 15:54:22 +03002244 case EOpOuterProduct:
Arun Patole7fa33552015-06-10 15:15:18 +05302245 {
Olli Etuahof119a262016-08-19 15:54:22 +03002246 ASSERT(basicType == EbtFloat);
Arun Patole7fa33552015-06-10 15:15:18 +05302247 size_t numRows = (*sequence)[0]->getAsTyped()->getType().getObjectSize();
2248 size_t numCols = (*sequence)[1]->getAsTyped()->getType().getObjectSize();
Olli Etuahof119a262016-08-19 15:54:22 +03002249 resultArray = new TConstantUnion[numRows * numCols];
Arun Patole7fa33552015-06-10 15:15:18 +05302250 angle::Matrix<float> result =
Olli Etuahod5da5052016-08-29 13:16:55 +03002251 GetMatrix(unionArrays[0], static_cast<int>(numRows), 1)
2252 .outerProduct(GetMatrix(unionArrays[1], 1, static_cast<int>(numCols)));
Arun Patole7fa33552015-06-10 15:15:18 +05302253 SetUnionArrayFromMatrix(result, resultArray);
Olli Etuahof119a262016-08-19 15:54:22 +03002254 break;
Arun Patole7fa33552015-06-10 15:15:18 +05302255 }
Arun Patole7fa33552015-06-10 15:15:18 +05302256
Olli Etuahof119a262016-08-19 15:54:22 +03002257 default:
2258 UNREACHABLE();
2259 // TODO: Add constant folding support for other built-in operations that take 2
2260 // parameters and not handled above.
2261 return nullptr;
Arun Patole274f0702015-05-05 13:33:30 +05302262 }
2263 }
2264 else if (paramsCount == 3)
2265 {
2266 //
2267 // Ternary built-in
2268 //
2269 switch (op)
2270 {
Olli Etuahof119a262016-08-19 15:54:22 +03002271 case EOpClamp:
Arun Patole274f0702015-05-05 13:33:30 +05302272 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002273 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole274f0702015-05-05 13:33:30 +05302274 for (size_t i = 0; i < maxObjectSize; i++)
2275 {
2276 switch (basicType)
2277 {
Olli Etuahof119a262016-08-19 15:54:22 +03002278 case EbtFloat:
Arun Patole274f0702015-05-05 13:33:30 +05302279 {
Olli Etuahof119a262016-08-19 15:54:22 +03002280 float x = unionArrays[0][i].getFConst();
Arun Patole274f0702015-05-05 13:33:30 +05302281 float min = unionArrays[1][i].getFConst();
2282 float max = unionArrays[2][i].getFConst();
2283 // Results are undefined if min > max.
2284 if (min > max)
Olli Etuahof119a262016-08-19 15:54:22 +03002285 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
2286 &resultArray[i]);
Arun Patole274f0702015-05-05 13:33:30 +05302287 else
Olli Etuahob43846e2015-06-02 18:18:57 +03002288 resultArray[i].setFConst(gl::clamp(x, min, max));
Olli Etuahof119a262016-08-19 15:54:22 +03002289 break;
Arun Patole274f0702015-05-05 13:33:30 +05302290 }
Olli Etuahof119a262016-08-19 15:54:22 +03002291
2292 case EbtInt:
Arun Patole274f0702015-05-05 13:33:30 +05302293 {
Olli Etuahof119a262016-08-19 15:54:22 +03002294 int x = unionArrays[0][i].getIConst();
Arun Patole274f0702015-05-05 13:33:30 +05302295 int min = unionArrays[1][i].getIConst();
2296 int max = unionArrays[2][i].getIConst();
2297 // Results are undefined if min > max.
2298 if (min > max)
Olli Etuahof119a262016-08-19 15:54:22 +03002299 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
2300 &resultArray[i]);
Arun Patole274f0702015-05-05 13:33:30 +05302301 else
Olli Etuahob43846e2015-06-02 18:18:57 +03002302 resultArray[i].setIConst(gl::clamp(x, min, max));
Olli Etuahof119a262016-08-19 15:54:22 +03002303 break;
Arun Patole274f0702015-05-05 13:33:30 +05302304 }
Olli Etuahof119a262016-08-19 15:54:22 +03002305 case EbtUInt:
Arun Patole274f0702015-05-05 13:33:30 +05302306 {
Olli Etuahof119a262016-08-19 15:54:22 +03002307 unsigned int x = unionArrays[0][i].getUConst();
Arun Patole274f0702015-05-05 13:33:30 +05302308 unsigned int min = unionArrays[1][i].getUConst();
2309 unsigned int max = unionArrays[2][i].getUConst();
2310 // Results are undefined if min > max.
2311 if (min > max)
Olli Etuahof119a262016-08-19 15:54:22 +03002312 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
2313 &resultArray[i]);
Arun Patole274f0702015-05-05 13:33:30 +05302314 else
Olli Etuahob43846e2015-06-02 18:18:57 +03002315 resultArray[i].setUConst(gl::clamp(x, min, max));
Olli Etuahof119a262016-08-19 15:54:22 +03002316 break;
Arun Patole274f0702015-05-05 13:33:30 +05302317 }
Olli Etuahof119a262016-08-19 15:54:22 +03002318 default:
2319 UNREACHABLE();
2320 break;
Arun Patole274f0702015-05-05 13:33:30 +05302321 }
2322 }
Olli Etuahof119a262016-08-19 15:54:22 +03002323 break;
Arun Patole274f0702015-05-05 13:33:30 +05302324 }
Arun Patole274f0702015-05-05 13:33:30 +05302325
Olli Etuahof119a262016-08-19 15:54:22 +03002326 case EOpMix:
Arun Patolebf790422015-05-18 17:53:04 +05302327 {
Olli Etuahof119a262016-08-19 15:54:22 +03002328 ASSERT(basicType == EbtFloat);
2329 resultArray = new TConstantUnion[maxObjectSize];
2330 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302331 {
Olli Etuahof119a262016-08-19 15:54:22 +03002332 float x = unionArrays[0][i].getFConst();
2333 float y = unionArrays[1][i].getFConst();
2334 TBasicType type = (*sequence)[2]->getAsTyped()->getType().getBasicType();
2335 if (type == EbtFloat)
Arun Patolebf790422015-05-18 17:53:04 +05302336 {
Olli Etuahof119a262016-08-19 15:54:22 +03002337 // Returns the linear blend of x and y, i.e., x * (1 - a) + y * a.
2338 float a = unionArrays[2][i].getFConst();
2339 resultArray[i].setFConst(x * (1.0f - a) + y * a);
2340 }
2341 else // 3rd parameter is EbtBool
2342 {
2343 ASSERT(type == EbtBool);
2344 // Selects which vector each returned component comes from.
2345 // For a component of a that is false, the corresponding component of x is
2346 // returned.
2347 // For a component of a that is true, the corresponding component of y is
2348 // returned.
2349 bool a = unionArrays[2][i].getBConst();
2350 resultArray[i].setFConst(a ? y : x);
Arun Patolebf790422015-05-18 17:53:04 +05302351 }
2352 }
Olli Etuahof119a262016-08-19 15:54:22 +03002353 break;
Arun Patolebf790422015-05-18 17:53:04 +05302354 }
Arun Patolebf790422015-05-18 17:53:04 +05302355
Olli Etuahof119a262016-08-19 15:54:22 +03002356 case EOpSmoothStep:
Arun Patolebf790422015-05-18 17:53:04 +05302357 {
Olli Etuahof119a262016-08-19 15:54:22 +03002358 ASSERT(basicType == EbtFloat);
2359 resultArray = new TConstantUnion[maxObjectSize];
2360 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302361 {
Olli Etuahof119a262016-08-19 15:54:22 +03002362 float edge0 = unionArrays[0][i].getFConst();
2363 float edge1 = unionArrays[1][i].getFConst();
2364 float x = unionArrays[2][i].getFConst();
2365 // Results are undefined if edge0 >= edge1.
2366 if (edge0 >= edge1)
Arun Patolebf790422015-05-18 17:53:04 +05302367 {
Olli Etuahof119a262016-08-19 15:54:22 +03002368 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
2369 &resultArray[i]);
2370 }
2371 else
2372 {
2373 // Returns 0.0 if x <= edge0 and 1.0 if x >= edge1 and performs smooth
2374 // Hermite interpolation between 0 and 1 when edge0 < x < edge1.
2375 float t = gl::clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
2376 resultArray[i].setFConst(t * t * (3.0f - 2.0f * t));
Arun Patolebf790422015-05-18 17:53:04 +05302377 }
2378 }
Olli Etuahof119a262016-08-19 15:54:22 +03002379 break;
Arun Patolebf790422015-05-18 17:53:04 +05302380 }
Arun Patolebf790422015-05-18 17:53:04 +05302381
Olli Etuahof119a262016-08-19 15:54:22 +03002382 case EOpFaceForward:
Arun Patole1155ddd2015-06-05 18:04:36 +05302383 {
Olli Etuahof119a262016-08-19 15:54:22 +03002384 ASSERT(basicType == EbtFloat);
Arun Patole1155ddd2015-06-05 18:04:36 +05302385 // genType faceforward(genType N, genType I, genType Nref) :
2386 // If dot(Nref, I) < 0 return N, otherwise return -N.
Olli Etuahof119a262016-08-19 15:54:22 +03002387 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole1155ddd2015-06-05 18:04:36 +05302388 float dotProduct = VectorDotProduct(unionArrays[2], unionArrays[1], maxObjectSize);
2389 for (size_t i = 0; i < maxObjectSize; i++)
2390 {
2391 if (dotProduct < 0)
Olli Etuahob43846e2015-06-02 18:18:57 +03002392 resultArray[i].setFConst(unionArrays[0][i].getFConst());
Arun Patole1155ddd2015-06-05 18:04:36 +05302393 else
Olli Etuahob43846e2015-06-02 18:18:57 +03002394 resultArray[i].setFConst(-unionArrays[0][i].getFConst());
Arun Patole1155ddd2015-06-05 18:04:36 +05302395 }
Olli Etuahof119a262016-08-19 15:54:22 +03002396 break;
Arun Patole1155ddd2015-06-05 18:04:36 +05302397 }
Arun Patole1155ddd2015-06-05 18:04:36 +05302398
Olli Etuahof119a262016-08-19 15:54:22 +03002399 case EOpRefract:
Arun Patole1155ddd2015-06-05 18:04:36 +05302400 {
Olli Etuahof119a262016-08-19 15:54:22 +03002401 ASSERT(basicType == EbtFloat);
Arun Patole1155ddd2015-06-05 18:04:36 +05302402 // genType refract(genType I, genType N, float eta) :
Olli Etuahof119a262016-08-19 15:54:22 +03002403 // For the incident vector I and surface normal N, and the ratio of indices of
2404 // refraction eta,
Arun Patole1155ddd2015-06-05 18:04:36 +05302405 // return the refraction vector. The result is computed by
2406 // k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I))
2407 // if (k < 0.0)
2408 // return genType(0.0)
2409 // else
2410 // return eta * I - (eta * dot(N, I) + sqrt(k)) * N
Olli Etuahof119a262016-08-19 15:54:22 +03002411 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole1155ddd2015-06-05 18:04:36 +05302412 float dotProduct = VectorDotProduct(unionArrays[1], unionArrays[0], maxObjectSize);
2413 for (size_t i = 0; i < maxObjectSize; i++)
2414 {
2415 float eta = unionArrays[2][i].getFConst();
Olli Etuahof119a262016-08-19 15:54:22 +03002416 float k = 1.0f - eta * eta * (1.0f - dotProduct * dotProduct);
Arun Patole1155ddd2015-06-05 18:04:36 +05302417 if (k < 0.0f)
Olli Etuahob43846e2015-06-02 18:18:57 +03002418 resultArray[i].setFConst(0.0f);
Arun Patole1155ddd2015-06-05 18:04:36 +05302419 else
Olli Etuahob43846e2015-06-02 18:18:57 +03002420 resultArray[i].setFConst(eta * unionArrays[0][i].getFConst() -
Olli Etuahof119a262016-08-19 15:54:22 +03002421 (eta * dotProduct + sqrtf(k)) *
2422 unionArrays[1][i].getFConst());
Arun Patole1155ddd2015-06-05 18:04:36 +05302423 }
Olli Etuahof119a262016-08-19 15:54:22 +03002424 break;
Arun Patole1155ddd2015-06-05 18:04:36 +05302425 }
Arun Patole1155ddd2015-06-05 18:04:36 +05302426
Olli Etuahof119a262016-08-19 15:54:22 +03002427 default:
2428 UNREACHABLE();
2429 // TODO: Add constant folding support for other built-in operations that take 3
2430 // parameters and not handled above.
2431 return nullptr;
Arun Patole274f0702015-05-05 13:33:30 +05302432 }
2433 }
Olli Etuahob43846e2015-06-02 18:18:57 +03002434 return resultArray;
Arun Patole274f0702015-05-05 13:33:30 +05302435}
2436
2437// static
Jamie Madillb1a85f42014-08-19 15:23:24 -04002438TString TIntermTraverser::hash(const TString &name, ShHashFunction64 hashFunction)
2439{
2440 if (hashFunction == NULL || name.empty())
2441 return name;
2442 khronos_uint64_t number = (*hashFunction)(name.c_str(), name.length());
2443 TStringStream stream;
2444 stream << HASHED_NAME_PREFIX << std::hex << number;
2445 TString hashedName = stream.str();
2446 return hashedName;
2447}
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002448
2449void TIntermTraverser::updateTree()
2450{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002451 for (size_t ii = 0; ii < mInsertions.size(); ++ii)
2452 {
2453 const NodeInsertMultipleEntry &insertion = mInsertions[ii];
2454 ASSERT(insertion.parent);
Olli Etuaho5d91dda2015-06-18 15:47:46 +03002455 if (!insertion.insertionsAfter.empty())
2456 {
2457 bool inserted = insertion.parent->insertChildNodes(insertion.position + 1,
2458 insertion.insertionsAfter);
2459 ASSERT(inserted);
2460 UNUSED_ASSERTION_VARIABLE(inserted);
2461 }
2462 if (!insertion.insertionsBefore.empty())
2463 {
2464 bool inserted =
2465 insertion.parent->insertChildNodes(insertion.position, insertion.insertionsBefore);
2466 ASSERT(inserted);
2467 UNUSED_ASSERTION_VARIABLE(inserted);
2468 }
Olli Etuahoa6f22092015-05-08 18:31:10 +03002469 }
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002470 for (size_t ii = 0; ii < mReplacements.size(); ++ii)
2471 {
Olli Etuahocd94ef92015-04-16 19:18:10 +03002472 const NodeUpdateEntry &replacement = mReplacements[ii];
2473 ASSERT(replacement.parent);
2474 bool replaced = replacement.parent->replaceChildNode(
2475 replacement.original, replacement.replacement);
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002476 ASSERT(replaced);
Olli Etuahod57e0db2015-04-24 15:05:08 +03002477 UNUSED_ASSERTION_VARIABLE(replaced);
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002478
Olli Etuahocd94ef92015-04-16 19:18:10 +03002479 if (!replacement.originalBecomesChildOfReplacement)
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002480 {
2481 // In AST traversing, a parent is visited before its children.
Olli Etuahocd94ef92015-04-16 19:18:10 +03002482 // After we replace a node, if its immediate child is to
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002483 // be replaced, we need to make sure we don't update the replaced
2484 // node; instead, we update the replacement node.
2485 for (size_t jj = ii + 1; jj < mReplacements.size(); ++jj)
2486 {
Olli Etuahocd94ef92015-04-16 19:18:10 +03002487 NodeUpdateEntry &replacement2 = mReplacements[jj];
2488 if (replacement2.parent == replacement.original)
2489 replacement2.parent = replacement.replacement;
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002490 }
2491 }
2492 }
Olli Etuahofc0e2bc2015-04-16 13:39:56 +03002493 for (size_t ii = 0; ii < mMultiReplacements.size(); ++ii)
2494 {
2495 const NodeReplaceWithMultipleEntry &replacement = mMultiReplacements[ii];
2496 ASSERT(replacement.parent);
2497 bool replaced = replacement.parent->replaceChildNodeWithMultiple(
2498 replacement.original, replacement.replacements);
2499 ASSERT(replaced);
Olli Etuahod57e0db2015-04-24 15:05:08 +03002500 UNUSED_ASSERTION_VARIABLE(replaced);
Olli Etuahofc0e2bc2015-04-16 13:39:56 +03002501 }
Olli Etuahod4f303e2015-05-20 17:09:06 +03002502
Jamie Madill03d863c2016-07-27 18:15:53 -04002503 clearReplacementQueue();
2504}
2505
2506void TIntermTraverser::clearReplacementQueue()
2507{
Olli Etuahod4f303e2015-05-20 17:09:06 +03002508 mReplacements.clear();
2509 mMultiReplacements.clear();
Jamie Madill03d863c2016-07-27 18:15:53 -04002510 mInsertions.clear();
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002511}
Jamie Madill1048e432016-07-23 18:51:28 -04002512
Jamie Madill03d863c2016-07-27 18:15:53 -04002513void TIntermTraverser::queueReplacement(TIntermNode *original,
2514 TIntermNode *replacement,
2515 OriginalNode originalStatus)
Jamie Madill1048e432016-07-23 18:51:28 -04002516{
Jamie Madill03d863c2016-07-27 18:15:53 -04002517 queueReplacementWithParent(getParentNode(), original, replacement, originalStatus);
Jamie Madill1048e432016-07-23 18:51:28 -04002518}
2519
Jamie Madill03d863c2016-07-27 18:15:53 -04002520void TIntermTraverser::queueReplacementWithParent(TIntermNode *parent,
2521 TIntermNode *original,
2522 TIntermNode *replacement,
2523 OriginalNode originalStatus)
Jamie Madill1048e432016-07-23 18:51:28 -04002524{
Jamie Madill03d863c2016-07-27 18:15:53 -04002525 bool originalBecomesChild = (originalStatus == OriginalNode::BECOMES_CHILD);
2526 mReplacements.push_back(NodeUpdateEntry(parent, original, replacement, originalBecomesChild));
Jamie Madill1048e432016-07-23 18:51:28 -04002527}