blob: 97a6076bcd2d9573cc2ea65a5d8e98b5a9d9aa48 [file] [log] [blame]
Jamie Madillb1a85f42014-08-19 15:23:24 -04001//
2// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7//
8// Build the intermediate representation.
9//
10
11#include <float.h>
12#include <limits.h>
Arun Patole9dea48f2015-04-02 11:45:09 +053013#include <math.h>
Arun Patole97dc22e2015-04-06 17:35:38 +053014#include <stdlib.h>
Jamie Madillb1a85f42014-08-19 15:23:24 -040015#include <algorithm>
Arun Patole274f0702015-05-05 13:33:30 +053016#include <vector>
Jamie Madillb1a85f42014-08-19 15:23:24 -040017
Arun Patole274f0702015-05-05 13:33:30 +053018#include "common/mathutil.h"
Arun Patole7fa33552015-06-10 15:15:18 +053019#include "common/matrix_utils.h"
Olli Etuaho3fdec912016-08-18 15:08:06 +030020#include "compiler/translator/Diagnostics.h"
Jamie Madillb1a85f42014-08-19 15:23:24 -040021#include "compiler/translator/HashNames.h"
22#include "compiler/translator/IntermNode.h"
23#include "compiler/translator/SymbolTable.h"
Corentin Wallez509e4562016-08-25 14:55:44 -040024#include "compiler/translator/util.h"
Jamie Madillb1a85f42014-08-19 15:23:24 -040025
26namespace
27{
28
Arun Patole9dea48f2015-04-02 11:45:09 +053029const float kPi = 3.14159265358979323846f;
30const float kDegreesToRadiansMultiplier = kPi / 180.0f;
31const float kRadiansToDegreesMultiplier = 180.0f / kPi;
32
Jamie Madillb1a85f42014-08-19 15:23:24 -040033TPrecision GetHigherPrecision(TPrecision left, TPrecision right)
34{
35 return left > right ? left : right;
36}
37
Arun Patole274f0702015-05-05 13:33:30 +053038TConstantUnion *Vectorize(const TConstantUnion &constant, size_t size)
39{
40 TConstantUnion *constUnion = new TConstantUnion[size];
41 for (unsigned int i = 0; i < size; ++i)
42 constUnion[i] = constant;
43
44 return constUnion;
45}
46
Olli Etuahof119a262016-08-19 15:54:22 +030047void UndefinedConstantFoldingError(const TSourceLoc &loc,
48 TOperator op,
49 TBasicType basicType,
50 TDiagnostics *diagnostics,
51 TConstantUnion *result)
Arun Patolebf790422015-05-18 17:53:04 +053052{
Olli Etuahof119a262016-08-19 15:54:22 +030053 diagnostics->warning(loc, "operation result is undefined for the values passed in",
54 GetOperatorString(op), "");
Arun Patolebf790422015-05-18 17:53:04 +053055
56 switch (basicType)
57 {
58 case EbtFloat :
59 result->setFConst(0.0f);
60 break;
61 case EbtInt:
62 result->setIConst(0);
63 break;
64 case EbtUInt:
65 result->setUConst(0u);
66 break;
67 case EbtBool:
68 result->setBConst(false);
69 break;
70 default:
71 break;
72 }
73}
74
Olli Etuaho5c0e0232015-11-11 15:55:59 +020075float VectorLength(const TConstantUnion *paramArray, size_t paramArraySize)
Arun Patole1155ddd2015-06-05 18:04:36 +053076{
77 float result = 0.0f;
78 for (size_t i = 0; i < paramArraySize; i++)
79 {
80 float f = paramArray[i].getFConst();
81 result += f * f;
82 }
83 return sqrtf(result);
84}
85
Olli Etuaho5c0e0232015-11-11 15:55:59 +020086float VectorDotProduct(const TConstantUnion *paramArray1,
87 const TConstantUnion *paramArray2,
88 size_t paramArraySize)
Arun Patole1155ddd2015-06-05 18:04:36 +053089{
90 float result = 0.0f;
91 for (size_t i = 0; i < paramArraySize; i++)
92 result += paramArray1[i].getFConst() * paramArray2[i].getFConst();
93 return result;
94}
95
Olli Etuaho3272a6d2016-08-29 17:54:50 +030096TIntermTyped *CreateFoldedNode(const TConstantUnion *constArray,
Olli Etuaho7c3848e2015-11-04 13:19:17 +020097 const TIntermTyped *originalNode,
98 TQualifier qualifier)
Olli Etuahob43846e2015-06-02 18:18:57 +030099{
100 if (constArray == nullptr)
101 {
102 return nullptr;
103 }
104 TIntermTyped *folded = new TIntermConstantUnion(constArray, originalNode->getType());
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200105 folded->getTypePointer()->setQualifier(qualifier);
Olli Etuahob43846e2015-06-02 18:18:57 +0300106 folded->setLine(originalNode->getLine());
107 return folded;
108}
109
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200110angle::Matrix<float> GetMatrix(const TConstantUnion *paramArray,
111 const unsigned int &rows,
112 const unsigned int &cols)
Arun Patole7fa33552015-06-10 15:15:18 +0530113{
114 std::vector<float> elements;
115 for (size_t i = 0; i < rows * cols; i++)
116 elements.push_back(paramArray[i].getFConst());
117 // Transpose is used since the Matrix constructor expects arguments in row-major order,
Olli Etuahod5da5052016-08-29 13:16:55 +0300118 // whereas the paramArray is in column-major order. Rows/cols parameters are also flipped below
119 // so that the created matrix will have the expected dimensions after the transpose.
120 return angle::Matrix<float>(elements, cols, rows).transpose();
Arun Patole7fa33552015-06-10 15:15:18 +0530121}
122
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200123angle::Matrix<float> GetMatrix(const TConstantUnion *paramArray, const unsigned int &size)
Arun Patole7fa33552015-06-10 15:15:18 +0530124{
125 std::vector<float> elements;
126 for (size_t i = 0; i < size * size; i++)
127 elements.push_back(paramArray[i].getFConst());
128 // Transpose is used since the Matrix constructor expects arguments in row-major order,
129 // whereas the paramArray is in column-major order.
130 return angle::Matrix<float>(elements, size).transpose();
131}
132
133void SetUnionArrayFromMatrix(const angle::Matrix<float> &m, TConstantUnion *resultArray)
134{
135 // Transpose is used since the input Matrix is in row-major order,
136 // whereas the actual result should be in column-major order.
137 angle::Matrix<float> result = m.transpose();
138 std::vector<float> resultElements = result.elements();
139 for (size_t i = 0; i < resultElements.size(); i++)
140 resultArray[i].setFConst(resultElements[i]);
141}
142
Jamie Madillb1a85f42014-08-19 15:23:24 -0400143} // namespace anonymous
144
145
146////////////////////////////////////////////////////////////////
147//
148// Member functions of the nodes used for building the tree.
149//
150////////////////////////////////////////////////////////////////
151
Olli Etuahod2a67b92014-10-21 16:42:57 +0300152void TIntermTyped::setTypePreservePrecision(const TType &t)
153{
154 TPrecision precision = getPrecision();
155 mType = t;
156 ASSERT(mType.getBasicType() != EbtBool || precision == EbpUndefined);
157 mType.setPrecision(precision);
158}
159
Jamie Madillb1a85f42014-08-19 15:23:24 -0400160#define REPLACE_IF_IS(node, type, original, replacement) \
161 if (node == original) { \
162 node = static_cast<type *>(replacement); \
163 return true; \
164 }
165
166bool TIntermLoop::replaceChildNode(
167 TIntermNode *original, TIntermNode *replacement)
168{
Olli Etuaho3cbb27a2016-07-14 11:55:48 +0300169 ASSERT(original != nullptr); // This risks replacing multiple children.
Jamie Madillb1a85f42014-08-19 15:23:24 -0400170 REPLACE_IF_IS(mInit, TIntermNode, original, replacement);
171 REPLACE_IF_IS(mCond, TIntermTyped, original, replacement);
172 REPLACE_IF_IS(mExpr, TIntermTyped, original, replacement);
Olli Etuaho3fed4302015-11-02 12:26:02 +0200173 REPLACE_IF_IS(mBody, TIntermAggregate, original, replacement);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400174 return false;
175}
176
Jamie Madillb1a85f42014-08-19 15:23:24 -0400177bool TIntermBranch::replaceChildNode(
178 TIntermNode *original, TIntermNode *replacement)
179{
180 REPLACE_IF_IS(mExpression, TIntermTyped, original, replacement);
181 return false;
182}
183
Jamie Madillb1a85f42014-08-19 15:23:24 -0400184bool TIntermBinary::replaceChildNode(
185 TIntermNode *original, TIntermNode *replacement)
186{
187 REPLACE_IF_IS(mLeft, TIntermTyped, original, replacement);
188 REPLACE_IF_IS(mRight, TIntermTyped, original, replacement);
189 return false;
190}
191
Jamie Madillb1a85f42014-08-19 15:23:24 -0400192bool TIntermUnary::replaceChildNode(
193 TIntermNode *original, TIntermNode *replacement)
194{
Olli Etuahoa2234302016-08-31 12:05:39 +0300195 ASSERT(original->getAsTyped()->getType() == replacement->getAsTyped()->getType());
Jamie Madillb1a85f42014-08-19 15:23:24 -0400196 REPLACE_IF_IS(mOperand, TIntermTyped, original, replacement);
197 return false;
198}
199
Jamie Madillb1a85f42014-08-19 15:23:24 -0400200bool TIntermAggregate::replaceChildNode(
201 TIntermNode *original, TIntermNode *replacement)
202{
203 for (size_t ii = 0; ii < mSequence.size(); ++ii)
204 {
205 REPLACE_IF_IS(mSequence[ii], TIntermNode, original, replacement);
206 }
207 return false;
208}
209
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300210bool TIntermAggregate::replaceChildNodeWithMultiple(TIntermNode *original, TIntermSequence replacements)
211{
212 for (auto it = mSequence.begin(); it < mSequence.end(); ++it)
213 {
214 if (*it == original)
215 {
216 it = mSequence.erase(it);
Olli Etuaho8fee0ab2015-04-23 14:52:46 +0300217 mSequence.insert(it, replacements.begin(), replacements.end());
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300218 return true;
219 }
220 }
221 return false;
222}
223
Olli Etuahoa6f22092015-05-08 18:31:10 +0300224bool TIntermAggregate::insertChildNodes(TIntermSequence::size_type position, TIntermSequence insertions)
225{
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300226 if (position > mSequence.size())
Olli Etuahoa6f22092015-05-08 18:31:10 +0300227 {
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300228 return false;
Olli Etuahoa6f22092015-05-08 18:31:10 +0300229 }
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300230 auto it = mSequence.begin() + position;
231 mSequence.insert(it, insertions.begin(), insertions.end());
232 return true;
Olli Etuahoa6f22092015-05-08 18:31:10 +0300233}
234
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200235bool TIntermAggregate::areChildrenConstQualified()
236{
237 for (TIntermNode *&child : mSequence)
238 {
239 TIntermTyped *typed = child->getAsTyped();
240 if (typed && typed->getQualifier() != EvqConst)
241 {
242 return false;
243 }
244 }
245 return true;
246}
247
Olli Etuahod2a67b92014-10-21 16:42:57 +0300248void TIntermAggregate::setPrecisionFromChildren()
249{
Olli Etuahoa4aa4e32015-06-04 15:54:30 +0300250 mGotPrecisionFromChildren = true;
Olli Etuahod2a67b92014-10-21 16:42:57 +0300251 if (getBasicType() == EbtBool)
252 {
253 mType.setPrecision(EbpUndefined);
254 return;
255 }
256
257 TPrecision precision = EbpUndefined;
258 TIntermSequence::iterator childIter = mSequence.begin();
259 while (childIter != mSequence.end())
260 {
261 TIntermTyped *typed = (*childIter)->getAsTyped();
262 if (typed)
263 precision = GetHigherPrecision(typed->getPrecision(), precision);
264 ++childIter;
265 }
266 mType.setPrecision(precision);
267}
268
269void TIntermAggregate::setBuiltInFunctionPrecision()
270{
271 // All built-ins returning bool should be handled as ops, not functions.
272 ASSERT(getBasicType() != EbtBool);
273
274 TPrecision precision = EbpUndefined;
275 TIntermSequence::iterator childIter = mSequence.begin();
276 while (childIter != mSequence.end())
277 {
278 TIntermTyped *typed = (*childIter)->getAsTyped();
279 // ESSL spec section 8: texture functions get their precision from the sampler.
280 if (typed && IsSampler(typed->getBasicType()))
281 {
282 precision = typed->getPrecision();
283 break;
284 }
285 ++childIter;
286 }
287 // ESSL 3.0 spec section 8: textureSize always gets highp precision.
288 // All other functions that take a sampler are assumed to be texture functions.
Olli Etuaho59f9a642015-08-06 20:38:26 +0300289 if (mName.getString().find("textureSize") == 0)
Olli Etuahod2a67b92014-10-21 16:42:57 +0300290 mType.setPrecision(EbpHigh);
291 else
292 mType.setPrecision(precision);
293}
294
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300295bool TIntermTernary::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
296{
297 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
298 REPLACE_IF_IS(mTrueExpression, TIntermTyped, original, replacement);
299 REPLACE_IF_IS(mFalseExpression, TIntermTyped, original, replacement);
300 return false;
301}
302
Olli Etuaho57961272016-09-14 13:57:46 +0300303bool TIntermIfElse::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400304{
305 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
306 REPLACE_IF_IS(mTrueBlock, TIntermNode, original, replacement);
307 REPLACE_IF_IS(mFalseBlock, TIntermNode, original, replacement);
308 return false;
309}
310
Olli Etuahoa3a36662015-02-17 13:46:51 +0200311bool TIntermSwitch::replaceChildNode(
312 TIntermNode *original, TIntermNode *replacement)
313{
314 REPLACE_IF_IS(mInit, TIntermTyped, original, replacement);
315 REPLACE_IF_IS(mStatementList, TIntermAggregate, original, replacement);
316 return false;
317}
318
319bool TIntermCase::replaceChildNode(
320 TIntermNode *original, TIntermNode *replacement)
321{
322 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
323 return false;
324}
325
Olli Etuahod7a25242015-08-18 13:49:45 +0300326TIntermTyped::TIntermTyped(const TIntermTyped &node) : TIntermNode(), mType(node.mType)
327{
328 // Copy constructor is disallowed for TIntermNode in order to disallow it for subclasses that
329 // don't explicitly allow it, so normal TIntermNode constructor is used to construct the copy.
330 // We need to manually copy any fields of TIntermNode besides handling fields in TIntermTyped.
331 mLine = node.mLine;
332}
333
Olli Etuahod4f4c112016-04-15 15:11:24 +0300334bool TIntermTyped::isConstructorWithOnlyConstantUnionParameters()
335{
336 TIntermAggregate *constructor = getAsAggregate();
337 if (!constructor || !constructor->isConstructor())
338 {
339 return false;
340 }
341 for (TIntermNode *&node : *constructor->getSequence())
342 {
343 if (!node->getAsConstantUnion())
344 return false;
345 }
346 return true;
347}
348
Corentin Wallez509e4562016-08-25 14:55:44 -0400349// static
350TIntermTyped *TIntermTyped::CreateIndexNode(int index)
351{
352 TConstantUnion *u = new TConstantUnion[1];
353 u[0].setIConst(index);
354
355 TType type(EbtInt, EbpUndefined, EvqConst, 1);
356 TIntermConstantUnion *node = new TIntermConstantUnion(u, type);
357 return node;
358}
359
360// static
361TIntermTyped *TIntermTyped::CreateZero(const TType &type)
362{
363 TType constType(type);
364 constType.setQualifier(EvqConst);
365
366 if (!type.isArray() && type.getBasicType() != EbtStruct)
367 {
368 ASSERT(type.isScalar() || type.isVector() || type.isMatrix());
369
370 size_t size = constType.getObjectSize();
371 TConstantUnion *u = new TConstantUnion[size];
372 for (size_t i = 0; i < size; ++i)
373 {
374 switch (type.getBasicType())
375 {
376 case EbtFloat:
377 u[i].setFConst(0.0f);
378 break;
379 case EbtInt:
380 u[i].setIConst(0);
381 break;
382 case EbtUInt:
383 u[i].setUConst(0u);
384 break;
385 case EbtBool:
386 u[i].setBConst(false);
387 break;
388 default:
389 UNREACHABLE();
390 return nullptr;
391 }
392 }
393
394 TIntermConstantUnion *node = new TIntermConstantUnion(u, constType);
395 return node;
396 }
397
398 TIntermAggregate *constructor = new TIntermAggregate(sh::TypeToConstructorOperator(type));
399 constructor->setType(constType);
400
401 if (type.isArray())
402 {
403 TType elementType(type);
404 elementType.clearArrayness();
405
406 size_t arraySize = type.getArraySize();
407 for (size_t i = 0; i < arraySize; ++i)
408 {
409 constructor->getSequence()->push_back(CreateZero(elementType));
410 }
411 }
412 else
413 {
414 ASSERT(type.getBasicType() == EbtStruct);
415
416 TStructure *structure = type.getStruct();
417 for (const auto &field : structure->fields())
418 {
419 constructor->getSequence()->push_back(CreateZero(*field->type()));
420 }
421 }
422
423 return constructor;
424}
425
Olli Etuahod7a25242015-08-18 13:49:45 +0300426TIntermConstantUnion::TIntermConstantUnion(const TIntermConstantUnion &node) : TIntermTyped(node)
427{
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200428 mUnionArrayPointer = node.mUnionArrayPointer;
Olli Etuahod7a25242015-08-18 13:49:45 +0300429}
430
431TIntermAggregate::TIntermAggregate(const TIntermAggregate &node)
432 : TIntermOperator(node),
433 mName(node.mName),
434 mUserDefined(node.mUserDefined),
435 mFunctionId(node.mFunctionId),
Olli Etuahod7a25242015-08-18 13:49:45 +0300436 mUseEmulatedFunction(node.mUseEmulatedFunction),
437 mGotPrecisionFromChildren(node.mGotPrecisionFromChildren)
438{
439 for (TIntermNode *child : node.mSequence)
440 {
441 TIntermTyped *typedChild = child->getAsTyped();
442 ASSERT(typedChild != nullptr);
443 TIntermTyped *childCopy = typedChild->deepCopy();
444 mSequence.push_back(childCopy);
445 }
446}
447
448TIntermBinary::TIntermBinary(const TIntermBinary &node)
449 : TIntermOperator(node), mAddIndexClamp(node.mAddIndexClamp)
450{
451 TIntermTyped *leftCopy = node.mLeft->deepCopy();
452 TIntermTyped *rightCopy = node.mRight->deepCopy();
453 ASSERT(leftCopy != nullptr && rightCopy != nullptr);
454 mLeft = leftCopy;
455 mRight = rightCopy;
456}
457
458TIntermUnary::TIntermUnary(const TIntermUnary &node)
459 : TIntermOperator(node), mUseEmulatedFunction(node.mUseEmulatedFunction)
460{
461 TIntermTyped *operandCopy = node.mOperand->deepCopy();
462 ASSERT(operandCopy != nullptr);
463 mOperand = operandCopy;
464}
465
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300466TIntermTernary::TIntermTernary(const TIntermTernary &node) : TIntermTyped(node)
Olli Etuahod7a25242015-08-18 13:49:45 +0300467{
Olli Etuahod7a25242015-08-18 13:49:45 +0300468 TIntermTyped *conditionCopy = node.mCondition->deepCopy();
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300469 TIntermTyped *trueCopy = node.mTrueExpression->deepCopy();
470 TIntermTyped *falseCopy = node.mFalseExpression->deepCopy();
Olli Etuahod7a25242015-08-18 13:49:45 +0300471 ASSERT(conditionCopy != nullptr && trueCopy != nullptr && falseCopy != nullptr);
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300472 mCondition = conditionCopy;
473 mTrueExpression = trueCopy;
474 mFalseExpression = falseCopy;
Olli Etuahod7a25242015-08-18 13:49:45 +0300475}
476
Jamie Madillb1a85f42014-08-19 15:23:24 -0400477bool TIntermOperator::isAssignment() const
478{
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300479 return IsAssignment(mOp);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400480}
481
Olli Etuaho8f76bcc2015-06-02 13:54:20 +0300482bool TIntermOperator::isMultiplication() const
483{
484 switch (mOp)
485 {
486 case EOpMul:
487 case EOpMatrixTimesMatrix:
488 case EOpMatrixTimesVector:
489 case EOpMatrixTimesScalar:
490 case EOpVectorTimesMatrix:
491 case EOpVectorTimesScalar:
492 return true;
493 default:
494 return false;
495 }
496}
497
Jamie Madillb1a85f42014-08-19 15:23:24 -0400498//
499// returns true if the operator is for one of the constructors
500//
501bool TIntermOperator::isConstructor() const
502{
503 switch (mOp)
504 {
505 case EOpConstructVec2:
506 case EOpConstructVec3:
507 case EOpConstructVec4:
508 case EOpConstructMat2:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400509 case EOpConstructMat2x3:
510 case EOpConstructMat2x4:
511 case EOpConstructMat3x2:
Jamie Madillb1a85f42014-08-19 15:23:24 -0400512 case EOpConstructMat3:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400513 case EOpConstructMat3x4:
514 case EOpConstructMat4x2:
515 case EOpConstructMat4x3:
Jamie Madillb1a85f42014-08-19 15:23:24 -0400516 case EOpConstructMat4:
517 case EOpConstructFloat:
518 case EOpConstructIVec2:
519 case EOpConstructIVec3:
520 case EOpConstructIVec4:
521 case EOpConstructInt:
522 case EOpConstructUVec2:
523 case EOpConstructUVec3:
524 case EOpConstructUVec4:
525 case EOpConstructUInt:
526 case EOpConstructBVec2:
527 case EOpConstructBVec3:
528 case EOpConstructBVec4:
529 case EOpConstructBool:
530 case EOpConstructStruct:
531 return true;
532 default:
533 return false;
534 }
535}
536
Olli Etuaho1dded802016-08-18 18:13:13 +0300537TOperator TIntermBinary::GetMulOpBasedOnOperands(const TType &left, const TType &right)
538{
539 if (left.isMatrix())
540 {
541 if (right.isMatrix())
542 {
543 return EOpMatrixTimesMatrix;
544 }
545 else
546 {
547 if (right.isVector())
548 {
549 return EOpMatrixTimesVector;
550 }
551 else
552 {
553 return EOpMatrixTimesScalar;
554 }
555 }
556 }
557 else
558 {
559 if (right.isMatrix())
560 {
561 if (left.isVector())
562 {
563 return EOpVectorTimesMatrix;
564 }
565 else
566 {
567 return EOpMatrixTimesScalar;
568 }
569 }
570 else
571 {
572 // Neither operand is a matrix.
573 if (left.isVector() == right.isVector())
574 {
575 // Leave as component product.
576 return EOpMul;
577 }
578 else
579 {
580 return EOpVectorTimesScalar;
581 }
582 }
583 }
584}
585
586TOperator TIntermBinary::GetMulAssignOpBasedOnOperands(const TType &left, const TType &right)
587{
588 if (left.isMatrix())
589 {
590 if (right.isMatrix())
591 {
592 return EOpMatrixTimesMatrixAssign;
593 }
594 else
595 {
596 // right should be scalar, but this may not be validated yet.
597 return EOpMatrixTimesScalarAssign;
598 }
599 }
600 else
601 {
602 if (right.isMatrix())
603 {
604 // Left should be a vector, but this may not be validated yet.
605 return EOpVectorTimesMatrixAssign;
606 }
607 else
608 {
609 // Neither operand is a matrix.
610 if (left.isVector() == right.isVector())
611 {
612 // Leave as component product.
613 return EOpMulAssign;
614 }
615 else
616 {
617 // left should be vector and right should be scalar, but this may not be validated
618 // yet.
619 return EOpVectorTimesScalarAssign;
620 }
621 }
622 }
623}
624
Jamie Madillb1a85f42014-08-19 15:23:24 -0400625//
626// Make sure the type of a unary operator is appropriate for its
627// combination of operation and operand type.
628//
Olli Etuahoa2234302016-08-31 12:05:39 +0300629void TIntermUnary::promote()
Jamie Madillb1a85f42014-08-19 15:23:24 -0400630{
Olli Etuahoa2234302016-08-31 12:05:39 +0300631 TQualifier resultQualifier = EvqTemporary;
632 if (mOperand->getQualifier() == EvqConst)
633 resultQualifier = EvqConst;
634
635 unsigned char operandPrimarySize =
636 static_cast<unsigned char>(mOperand->getType().getNominalSize());
Jamie Madillb1a85f42014-08-19 15:23:24 -0400637 switch (mOp)
638 {
Olli Etuahoa2234302016-08-31 12:05:39 +0300639 case EOpFloatBitsToInt:
640 setType(TType(EbtInt, EbpHigh, resultQualifier, operandPrimarySize));
641 break;
642 case EOpFloatBitsToUint:
643 setType(TType(EbtUInt, EbpHigh, resultQualifier, operandPrimarySize));
644 break;
645 case EOpIntBitsToFloat:
646 case EOpUintBitsToFloat:
647 setType(TType(EbtFloat, EbpHigh, resultQualifier, operandPrimarySize));
648 break;
649 case EOpPackSnorm2x16:
650 case EOpPackUnorm2x16:
651 case EOpPackHalf2x16:
652 setType(TType(EbtUInt, EbpHigh, resultQualifier));
653 break;
654 case EOpUnpackSnorm2x16:
655 case EOpUnpackUnorm2x16:
656 setType(TType(EbtFloat, EbpHigh, resultQualifier, 2));
657 break;
658 case EOpUnpackHalf2x16:
659 setType(TType(EbtFloat, EbpMedium, resultQualifier, 2));
660 break;
661 case EOpAny:
662 case EOpAll:
663 setType(TType(EbtBool, EbpUndefined, resultQualifier));
664 break;
665 case EOpLength:
666 case EOpDeterminant:
667 setType(TType(EbtFloat, mOperand->getType().getPrecision(), resultQualifier));
668 break;
669 case EOpTranspose:
670 setType(TType(EbtFloat, mOperand->getType().getPrecision(), resultQualifier,
671 static_cast<unsigned char>(mOperand->getType().getRows()),
672 static_cast<unsigned char>(mOperand->getType().getCols())));
673 break;
674 case EOpIsInf:
675 case EOpIsNan:
676 setType(TType(EbtBool, EbpUndefined, resultQualifier, operandPrimarySize));
677 break;
678 default:
679 setType(mOperand->getType());
680 mType.setQualifier(resultQualifier);
681 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400682 }
Olli Etuahoa2234302016-08-31 12:05:39 +0300683}
Jamie Madillb1a85f42014-08-19 15:23:24 -0400684
Olli Etuahoa2234302016-08-31 12:05:39 +0300685TIntermUnary::TIntermUnary(TOperator op, TIntermTyped *operand)
686 : TIntermOperator(op), mOperand(operand), mUseEmulatedFunction(false)
687{
688 promote();
Jamie Madillb1a85f42014-08-19 15:23:24 -0400689}
690
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300691TIntermBinary::TIntermBinary(TOperator op, TIntermTyped *left, TIntermTyped *right)
692 : TIntermOperator(op), mLeft(left), mRight(right), mAddIndexClamp(false)
693{
694 promote();
695}
696
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300697TIntermTernary::TIntermTernary(TIntermTyped *cond,
698 TIntermTyped *trueExpression,
699 TIntermTyped *falseExpression)
700 : TIntermTyped(trueExpression->getType()),
701 mCondition(cond),
702 mTrueExpression(trueExpression),
703 mFalseExpression(falseExpression)
704{
705 getTypePointer()->setQualifier(
706 TIntermTernary::DetermineQualifier(cond, trueExpression, falseExpression));
707}
708
709// static
710TQualifier TIntermTernary::DetermineQualifier(TIntermTyped *cond,
711 TIntermTyped *trueExpression,
712 TIntermTyped *falseExpression)
713{
714 if (cond->getQualifier() == EvqConst && trueExpression->getQualifier() == EvqConst &&
715 falseExpression->getQualifier() == EvqConst)
716 {
717 return EvqConst;
718 }
719 return EvqTemporary;
720}
721
Jamie Madillb1a85f42014-08-19 15:23:24 -0400722//
723// Establishes the type of the resultant operation, as well as
724// makes the operator the correct one for the operands.
725//
Olli Etuaho47fd36a2015-03-19 14:22:24 +0200726// For lots of operations it should already be established that the operand
727// combination is valid, but returns false if operator can't work on operands.
Jamie Madillb1a85f42014-08-19 15:23:24 -0400728//
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300729void TIntermBinary::promote()
Jamie Madillb1a85f42014-08-19 15:23:24 -0400730{
Olli Etuaho1dded802016-08-18 18:13:13 +0300731 ASSERT(!isMultiplication() ||
732 mOp == GetMulOpBasedOnOperands(mLeft->getType(), mRight->getType()));
733
Jamie Madillb1a85f42014-08-19 15:23:24 -0400734 // Base assumption: just make the type the same as the left
735 // operand. Then only deviations from this need be coded.
Jamie Madillb1a85f42014-08-19 15:23:24 -0400736 setType(mLeft->getType());
737
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200738 TQualifier resultQualifier = EvqConst;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400739 // Binary operations results in temporary variables unless both
740 // operands are const.
741 if (mLeft->getQualifier() != EvqConst || mRight->getQualifier() != EvqConst)
742 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200743 resultQualifier = EvqTemporary;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400744 getTypePointer()->setQualifier(EvqTemporary);
745 }
746
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300747 // Handle indexing ops.
748 switch (mOp)
749 {
750 case EOpIndexDirect:
751 case EOpIndexIndirect:
752 if (mLeft->isArray())
753 {
754 mType.clearArrayness();
755 }
756 else if (mLeft->isMatrix())
757 {
758 setType(TType(mLeft->getBasicType(), mLeft->getPrecision(), resultQualifier,
759 static_cast<unsigned char>(mLeft->getRows())));
760 }
761 else if (mLeft->isVector())
762 {
763 setType(TType(mLeft->getBasicType(), mLeft->getPrecision(), resultQualifier));
764 }
765 else
766 {
767 UNREACHABLE();
768 }
769 return;
770 case EOpIndexDirectStruct:
771 {
772 const TFieldList &fields = mLeft->getType().getStruct()->fields();
773 const int i = mRight->getAsConstantUnion()->getIConst(0);
774 setType(*fields[i]->type());
775 getTypePointer()->setQualifier(resultQualifier);
776 return;
777 }
778 case EOpIndexDirectInterfaceBlock:
779 {
780 const TFieldList &fields = mLeft->getType().getInterfaceBlock()->fields();
781 const int i = mRight->getAsConstantUnion()->getIConst(0);
782 setType(*fields[i]->type());
783 getTypePointer()->setQualifier(resultQualifier);
784 return;
785 }
786 case EOpVectorSwizzle:
787 {
788 auto numFields = mRight->getAsAggregate()->getSequence()->size();
789 setType(TType(mLeft->getBasicType(), mLeft->getPrecision(), resultQualifier,
790 static_cast<unsigned char>(numFields)));
791 return;
792 }
793 default:
794 break;
795 }
796
797 ASSERT(mLeft->isArray() == mRight->isArray());
798
799 // The result gets promoted to the highest precision.
800 TPrecision higherPrecision = GetHigherPrecision(mLeft->getPrecision(), mRight->getPrecision());
801 getTypePointer()->setPrecision(higherPrecision);
802
Jamie Madillb1a85f42014-08-19 15:23:24 -0400803 const int nominalSize =
804 std::max(mLeft->getNominalSize(), mRight->getNominalSize());
805
806 //
807 // All scalars or structs. Code after this test assumes this case is removed!
808 //
809 if (nominalSize == 1)
810 {
811 switch (mOp)
812 {
813 //
814 // Promote to conditional
815 //
816 case EOpEqual:
817 case EOpNotEqual:
818 case EOpLessThan:
819 case EOpGreaterThan:
820 case EOpLessThanEqual:
821 case EOpGreaterThanEqual:
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300822 setType(TType(EbtBool, EbpUndefined, resultQualifier));
823 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400824
825 //
826 // And and Or operate on conditionals
827 //
828 case EOpLogicalAnd:
Olli Etuaho47fd36a2015-03-19 14:22:24 +0200829 case EOpLogicalXor:
Jamie Madillb1a85f42014-08-19 15:23:24 -0400830 case EOpLogicalOr:
Olli Etuaho47fd36a2015-03-19 14:22:24 +0200831 ASSERT(mLeft->getBasicType() == EbtBool && mRight->getBasicType() == EbtBool);
Olli Etuahoc9550582016-08-29 17:56:22 +0300832 setType(TType(EbtBool, EbpUndefined, resultQualifier));
Jamie Madillb1a85f42014-08-19 15:23:24 -0400833 break;
834
835 default:
836 break;
837 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300838 return;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400839 }
840
841 // If we reach here, at least one of the operands is vector or matrix.
842 // The other operand could be a scalar, vector, or matrix.
Jamie Madillb1a85f42014-08-19 15:23:24 -0400843 TBasicType basicType = mLeft->getBasicType();
Olli Etuaho1dded802016-08-18 18:13:13 +0300844
Jamie Madillb1a85f42014-08-19 15:23:24 -0400845 switch (mOp)
846 {
Olli Etuaho1dded802016-08-18 18:13:13 +0300847 case EOpMul:
848 break;
849 case EOpMatrixTimesScalar:
850 if (mRight->isMatrix())
Jamie Madillb1a85f42014-08-19 15:23:24 -0400851 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200852 setType(TType(basicType, higherPrecision, resultQualifier,
853 static_cast<unsigned char>(mRight->getCols()),
854 static_cast<unsigned char>(mRight->getRows())));
Jamie Madillb1a85f42014-08-19 15:23:24 -0400855 }
Olli Etuaho1dded802016-08-18 18:13:13 +0300856 break;
857 case EOpMatrixTimesVector:
858 setType(TType(basicType, higherPrecision, resultQualifier,
859 static_cast<unsigned char>(mLeft->getRows()), 1));
860 break;
861 case EOpMatrixTimesMatrix:
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200862 setType(TType(basicType, higherPrecision, resultQualifier,
863 static_cast<unsigned char>(mRight->getCols()),
864 static_cast<unsigned char>(mLeft->getRows())));
Olli Etuaho1dded802016-08-18 18:13:13 +0300865 break;
866 case EOpVectorTimesScalar:
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200867 setType(TType(basicType, higherPrecision, resultQualifier,
Olli Etuaho1dded802016-08-18 18:13:13 +0300868 static_cast<unsigned char>(nominalSize), 1));
869 break;
870 case EOpVectorTimesMatrix:
871 setType(TType(basicType, higherPrecision, resultQualifier,
872 static_cast<unsigned char>(mRight->getCols()), 1));
873 break;
874 case EOpMulAssign:
875 case EOpVectorTimesScalarAssign:
876 case EOpVectorTimesMatrixAssign:
877 case EOpMatrixTimesScalarAssign:
878 case EOpMatrixTimesMatrixAssign:
879 ASSERT(mOp == GetMulAssignOpBasedOnOperands(mLeft->getType(), mRight->getType()));
880 break;
881 case EOpAssign:
882 case EOpInitialize:
Olli Etuaho1dded802016-08-18 18:13:13 +0300883 ASSERT((mLeft->getNominalSize() == mRight->getNominalSize()) &&
884 (mLeft->getSecondarySize() == mRight->getSecondarySize()));
885 break;
886 case EOpAdd:
887 case EOpSub:
888 case EOpDiv:
889 case EOpIMod:
890 case EOpBitShiftLeft:
891 case EOpBitShiftRight:
892 case EOpBitwiseAnd:
893 case EOpBitwiseXor:
894 case EOpBitwiseOr:
895 case EOpAddAssign:
896 case EOpSubAssign:
897 case EOpDivAssign:
898 case EOpIModAssign:
899 case EOpBitShiftLeftAssign:
900 case EOpBitShiftRightAssign:
901 case EOpBitwiseAndAssign:
902 case EOpBitwiseXorAssign:
903 case EOpBitwiseOrAssign:
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300904 {
905 const int secondarySize =
906 std::max(mLeft->getSecondarySize(), mRight->getSecondarySize());
907 setType(TType(basicType, higherPrecision, resultQualifier,
908 static_cast<unsigned char>(nominalSize),
909 static_cast<unsigned char>(secondarySize)));
910 ASSERT(!mLeft->isArray() && !mRight->isArray());
Olli Etuaho1dded802016-08-18 18:13:13 +0300911 break;
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300912 }
Olli Etuaho1dded802016-08-18 18:13:13 +0300913 case EOpEqual:
914 case EOpNotEqual:
915 case EOpLessThan:
916 case EOpGreaterThan:
917 case EOpLessThanEqual:
918 case EOpGreaterThanEqual:
919 ASSERT((mLeft->getNominalSize() == mRight->getNominalSize()) &&
920 (mLeft->getSecondarySize() == mRight->getSecondarySize()));
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300921 setType(TType(EbtBool, EbpUndefined, resultQualifier));
Olli Etuaho1dded802016-08-18 18:13:13 +0300922 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400923
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300924 case EOpIndexDirect:
925 case EOpIndexIndirect:
926 case EOpIndexDirectInterfaceBlock:
927 case EOpIndexDirectStruct:
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300928 case EOpVectorSwizzle:
929 // These ops should be already fully handled.
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300930 UNREACHABLE();
931 break;
Olli Etuaho1dded802016-08-18 18:13:13 +0300932 default:
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300933 UNREACHABLE();
934 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400935 }
Jamie Madillb1a85f42014-08-19 15:23:24 -0400936}
937
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300938const TConstantUnion *TIntermConstantUnion::foldIndexing(int index)
Olli Etuaho2c4b7462015-06-08 11:30:31 +0300939{
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300940 if (isArray())
Olli Etuaho2c4b7462015-06-08 11:30:31 +0300941 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300942 ASSERT(index < static_cast<int>(getType().getArraySize()));
943 TType arrayElementType = getType();
944 arrayElementType.clearArrayness();
945 size_t arrayElementSize = arrayElementType.getObjectSize();
946 return &mUnionArrayPointer[arrayElementSize * index];
947 }
948 else if (isMatrix())
949 {
950 ASSERT(index < getType().getCols());
951 int size = getType().getRows();
952 return &mUnionArrayPointer[size * index];
953 }
954 else if (isVector())
955 {
956 ASSERT(index < getType().getNominalSize());
957 return &mUnionArrayPointer[index];
958 }
959 else
960 {
961 UNREACHABLE();
Olli Etuaho2c4b7462015-06-08 11:30:31 +0300962 return nullptr;
963 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300964}
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200965
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300966TIntermTyped *TIntermBinary::fold(TDiagnostics *diagnostics)
967{
968 TIntermConstantUnion *leftConstant = mLeft->getAsConstantUnion();
969 TIntermConstantUnion *rightConstant = mRight->getAsConstantUnion();
970 switch (mOp)
971 {
972 case EOpIndexDirect:
973 {
974 if (leftConstant == nullptr || rightConstant == nullptr)
975 {
976 return nullptr;
977 }
978 int index = rightConstant->getIConst(0);
979
980 const TConstantUnion *constArray = leftConstant->foldIndexing(index);
981 return CreateFoldedNode(constArray, this, mType.getQualifier());
982 }
983 case EOpIndexDirectStruct:
984 {
985 if (leftConstant == nullptr || rightConstant == nullptr)
986 {
987 return nullptr;
988 }
989 const TFieldList &fields = mLeft->getType().getStruct()->fields();
990 size_t index = static_cast<size_t>(rightConstant->getIConst(0));
991
992 size_t previousFieldsSize = 0;
993 for (size_t i = 0; i < index; ++i)
994 {
995 previousFieldsSize += fields[i]->type()->getObjectSize();
996 }
997
998 const TConstantUnion *constArray = leftConstant->getUnionArrayPointer();
999 return CreateFoldedNode(constArray + previousFieldsSize, this, mType.getQualifier());
1000 }
1001 case EOpIndexIndirect:
1002 case EOpIndexDirectInterfaceBlock:
1003 // Can never be constant folded.
1004 return nullptr;
1005 case EOpVectorSwizzle:
1006 {
1007 if (leftConstant == nullptr)
1008 {
1009 return nullptr;
1010 }
1011 TIntermAggregate *fieldsAgg = mRight->getAsAggregate();
1012 TIntermSequence *fieldsSequence = fieldsAgg->getSequence();
1013 size_t numFields = fieldsSequence->size();
1014
1015 TConstantUnion *constArray = new TConstantUnion[numFields];
1016 for (size_t i = 0; i < numFields; i++)
1017 {
1018 int fieldOffset = fieldsSequence->at(i)->getAsConstantUnion()->getIConst(0);
1019 constArray[i] = *leftConstant->foldIndexing(fieldOffset);
1020 }
1021 return CreateFoldedNode(constArray, this, mType.getQualifier());
1022 }
1023 default:
1024 {
1025 if (leftConstant == nullptr || rightConstant == nullptr)
1026 {
1027 return nullptr;
1028 }
Jamie Madill5db69f52016-09-15 12:47:32 -04001029 TConstantUnion *constArray =
1030 leftConstant->foldBinary(mOp, rightConstant, diagnostics, mLeft->getLine());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001031
1032 // Nodes may be constant folded without being qualified as constant.
1033 return CreateFoldedNode(constArray, this, mType.getQualifier());
1034 }
1035 }
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001036}
1037
Olli Etuahof119a262016-08-19 15:54:22 +03001038TIntermTyped *TIntermUnary::fold(TDiagnostics *diagnostics)
Olli Etuaho95310b02015-06-02 17:43:38 +03001039{
1040 TIntermConstantUnion *operandConstant = mOperand->getAsConstantUnion();
1041 if (operandConstant == nullptr)
1042 {
1043 return nullptr;
1044 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301045
1046 TConstantUnion *constArray = nullptr;
1047 switch (mOp)
1048 {
1049 case EOpAny:
1050 case EOpAll:
1051 case EOpLength:
1052 case EOpTranspose:
1053 case EOpDeterminant:
1054 case EOpInverse:
1055 case EOpPackSnorm2x16:
1056 case EOpUnpackSnorm2x16:
1057 case EOpPackUnorm2x16:
1058 case EOpUnpackUnorm2x16:
1059 case EOpPackHalf2x16:
1060 case EOpUnpackHalf2x16:
Olli Etuahof119a262016-08-19 15:54:22 +03001061 constArray = operandConstant->foldUnaryNonComponentWise(mOp);
1062 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301063 default:
Olli Etuahof119a262016-08-19 15:54:22 +03001064 constArray = operandConstant->foldUnaryComponentWise(mOp, diagnostics);
1065 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301066 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001067
1068 // Nodes may be constant folded without being qualified as constant.
Olli Etuahoc9550582016-08-29 17:56:22 +03001069 return CreateFoldedNode(constArray, this, mType.getQualifier());
Olli Etuahob43846e2015-06-02 18:18:57 +03001070}
1071
Olli Etuahof119a262016-08-19 15:54:22 +03001072TIntermTyped *TIntermAggregate::fold(TDiagnostics *diagnostics)
Olli Etuahob43846e2015-06-02 18:18:57 +03001073{
1074 // Make sure that all params are constant before actual constant folding.
1075 for (auto *param : *getSequence())
Olli Etuaho95310b02015-06-02 17:43:38 +03001076 {
Olli Etuahob43846e2015-06-02 18:18:57 +03001077 if (param->getAsConstantUnion() == nullptr)
1078 {
1079 return nullptr;
1080 }
Olli Etuaho95310b02015-06-02 17:43:38 +03001081 }
Olli Etuaho1d122782015-11-06 15:35:17 +02001082 TConstantUnion *constArray = nullptr;
1083 if (isConstructor())
Olli Etuahof119a262016-08-19 15:54:22 +03001084 constArray = TIntermConstantUnion::FoldAggregateConstructor(this);
Olli Etuaho1d122782015-11-06 15:35:17 +02001085 else
Olli Etuahof119a262016-08-19 15:54:22 +03001086 constArray = TIntermConstantUnion::FoldAggregateBuiltIn(this, diagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001087
1088 // Nodes may be constant folded without being qualified as constant.
1089 TQualifier resultQualifier = areChildrenConstQualified() ? EvqConst : EvqTemporary;
1090 return CreateFoldedNode(constArray, this, resultQualifier);
Olli Etuaho95310b02015-06-02 17:43:38 +03001091}
1092
Jamie Madillb1a85f42014-08-19 15:23:24 -04001093//
1094// The fold functions see if an operation on a constant can be done in place,
1095// without generating run-time code.
1096//
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001097// Returns the constant value to keep using or nullptr.
Jamie Madillb1a85f42014-08-19 15:23:24 -04001098//
Olli Etuaho3fdec912016-08-18 15:08:06 +03001099TConstantUnion *TIntermConstantUnion::foldBinary(TOperator op,
1100 TIntermConstantUnion *rightNode,
Jamie Madill5db69f52016-09-15 12:47:32 -04001101 TDiagnostics *diagnostics,
1102 const TSourceLoc &line)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001103{
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001104 const TConstantUnion *leftArray = getUnionArrayPointer();
1105 const TConstantUnion *rightArray = rightNode->getUnionArrayPointer();
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001106
Olli Etuahof119a262016-08-19 15:54:22 +03001107 ASSERT(leftArray && rightArray);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001108
1109 size_t objectSize = getType().getObjectSize();
1110
1111 // for a case like float f = vec4(2, 3, 4, 5) + 1.2;
1112 if (rightNode->getType().getObjectSize() == 1 && objectSize > 1)
1113 {
1114 rightArray = Vectorize(*rightNode->getUnionArrayPointer(), objectSize);
1115 }
1116 else if (rightNode->getType().getObjectSize() > 1 && objectSize == 1)
1117 {
1118 // for a case like float f = 1.2 + vec4(2, 3, 4, 5);
1119 leftArray = Vectorize(*getUnionArrayPointer(), rightNode->getType().getObjectSize());
1120 objectSize = rightNode->getType().getObjectSize();
1121 }
1122
1123 TConstantUnion *resultArray = nullptr;
1124
1125 switch(op)
1126 {
1127 case EOpAdd:
1128 resultArray = new TConstantUnion[objectSize];
1129 for (size_t i = 0; i < objectSize; i++)
Jamie Madill5db69f52016-09-15 12:47:32 -04001130 resultArray[i] = TConstantUnion::add(leftArray[i], rightArray[i], diagnostics, line);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001131 break;
1132 case EOpSub:
1133 resultArray = new TConstantUnion[objectSize];
1134 for (size_t i = 0; i < objectSize; i++)
Jamie Madill5db69f52016-09-15 12:47:32 -04001135 resultArray[i] = TConstantUnion::sub(leftArray[i], rightArray[i], diagnostics, line);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001136 break;
1137
1138 case EOpMul:
1139 case EOpVectorTimesScalar:
1140 case EOpMatrixTimesScalar:
1141 resultArray = new TConstantUnion[objectSize];
1142 for (size_t i = 0; i < objectSize; i++)
Jamie Madill5db69f52016-09-15 12:47:32 -04001143 resultArray[i] = TConstantUnion::mul(leftArray[i], rightArray[i], diagnostics, line);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001144 break;
1145
1146 case EOpMatrixTimesMatrix:
1147 {
Jamie Madill5db69f52016-09-15 12:47:32 -04001148 // TODO(jmadll): This code should check for overflows.
Olli Etuaho3fdec912016-08-18 15:08:06 +03001149 ASSERT(getType().getBasicType() == EbtFloat && rightNode->getBasicType() == EbtFloat);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001150
1151 const int leftCols = getCols();
1152 const int leftRows = getRows();
1153 const int rightCols = rightNode->getType().getCols();
1154 const int rightRows = rightNode->getType().getRows();
1155 const int resultCols = rightCols;
1156 const int resultRows = leftRows;
1157
1158 resultArray = new TConstantUnion[resultCols * resultRows];
1159 for (int row = 0; row < resultRows; row++)
1160 {
1161 for (int column = 0; column < resultCols; column++)
1162 {
1163 resultArray[resultRows * column + row].setFConst(0.0f);
1164 for (int i = 0; i < leftCols; i++)
1165 {
1166 resultArray[resultRows * column + row].setFConst(
1167 resultArray[resultRows * column + row].getFConst() +
1168 leftArray[i * leftRows + row].getFConst() *
1169 rightArray[column * rightRows + i].getFConst());
1170 }
1171 }
1172 }
1173 }
1174 break;
1175
1176 case EOpDiv:
1177 case EOpIMod:
1178 {
1179 resultArray = new TConstantUnion[objectSize];
1180 for (size_t i = 0; i < objectSize; i++)
1181 {
1182 switch (getType().getBasicType())
1183 {
1184 case EbtFloat:
1185 if (rightArray[i] == 0.0f)
1186 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001187 diagnostics->warning(
1188 getLine(), "Divide by zero error during constant folding", "/", "");
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001189 resultArray[i].setFConst(leftArray[i].getFConst() < 0 ? -FLT_MAX : FLT_MAX);
1190 }
1191 else
1192 {
1193 ASSERT(op == EOpDiv);
1194 resultArray[i].setFConst(leftArray[i].getFConst() / rightArray[i].getFConst());
1195 }
1196 break;
1197
1198 case EbtInt:
1199 if (rightArray[i] == 0)
1200 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001201 diagnostics->warning(
1202 getLine(), "Divide by zero error during constant folding", "/", "");
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001203 resultArray[i].setIConst(INT_MAX);
1204 }
1205 else
1206 {
1207 if (op == EOpDiv)
1208 {
1209 resultArray[i].setIConst(leftArray[i].getIConst() / rightArray[i].getIConst());
1210 }
1211 else
1212 {
1213 ASSERT(op == EOpIMod);
1214 resultArray[i].setIConst(leftArray[i].getIConst() % rightArray[i].getIConst());
1215 }
1216 }
1217 break;
1218
1219 case EbtUInt:
1220 if (rightArray[i] == 0)
1221 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001222 diagnostics->warning(
1223 getLine(), "Divide by zero error during constant folding", "/", "");
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001224 resultArray[i].setUConst(UINT_MAX);
1225 }
1226 else
1227 {
1228 if (op == EOpDiv)
1229 {
1230 resultArray[i].setUConst(leftArray[i].getUConst() / rightArray[i].getUConst());
1231 }
1232 else
1233 {
1234 ASSERT(op == EOpIMod);
1235 resultArray[i].setUConst(leftArray[i].getUConst() % rightArray[i].getUConst());
1236 }
1237 }
1238 break;
1239
1240 default:
Olli Etuaho3fdec912016-08-18 15:08:06 +03001241 UNREACHABLE();
1242 return nullptr;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001243 }
1244 }
1245 }
1246 break;
1247
1248 case EOpMatrixTimesVector:
1249 {
Jamie Madill5db69f52016-09-15 12:47:32 -04001250 // TODO(jmadll): This code should check for overflows.
Olli Etuaho3fdec912016-08-18 15:08:06 +03001251 ASSERT(rightNode->getBasicType() == EbtFloat);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001252
1253 const int matrixCols = getCols();
1254 const int matrixRows = getRows();
1255
1256 resultArray = new TConstantUnion[matrixRows];
1257
1258 for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++)
1259 {
1260 resultArray[matrixRow].setFConst(0.0f);
1261 for (int col = 0; col < matrixCols; col++)
1262 {
1263 resultArray[matrixRow].setFConst(resultArray[matrixRow].getFConst() +
1264 leftArray[col * matrixRows + matrixRow].getFConst() *
1265 rightArray[col].getFConst());
1266 }
1267 }
1268 }
1269 break;
1270
1271 case EOpVectorTimesMatrix:
1272 {
Jamie Madill5db69f52016-09-15 12:47:32 -04001273 // TODO(jmadll): This code should check for overflows.
Olli Etuaho3fdec912016-08-18 15:08:06 +03001274 ASSERT(getType().getBasicType() == EbtFloat);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001275
1276 const int matrixCols = rightNode->getType().getCols();
1277 const int matrixRows = rightNode->getType().getRows();
1278
1279 resultArray = new TConstantUnion[matrixCols];
1280
1281 for (int matrixCol = 0; matrixCol < matrixCols; matrixCol++)
1282 {
1283 resultArray[matrixCol].setFConst(0.0f);
1284 for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++)
1285 {
1286 resultArray[matrixCol].setFConst(resultArray[matrixCol].getFConst() +
1287 leftArray[matrixRow].getFConst() *
1288 rightArray[matrixCol * matrixRows + matrixRow].getFConst());
1289 }
1290 }
1291 }
1292 break;
1293
1294 case EOpLogicalAnd:
1295 {
1296 resultArray = new TConstantUnion[objectSize];
1297 for (size_t i = 0; i < objectSize; i++)
1298 {
1299 resultArray[i] = leftArray[i] && rightArray[i];
1300 }
1301 }
1302 break;
1303
1304 case EOpLogicalOr:
1305 {
1306 resultArray = new TConstantUnion[objectSize];
1307 for (size_t i = 0; i < objectSize; i++)
1308 {
1309 resultArray[i] = leftArray[i] || rightArray[i];
1310 }
1311 }
1312 break;
1313
1314 case EOpLogicalXor:
1315 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001316 ASSERT(getType().getBasicType() == EbtBool);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001317 resultArray = new TConstantUnion[objectSize];
1318 for (size_t i = 0; i < objectSize; i++)
1319 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001320 resultArray[i].setBConst(leftArray[i] != rightArray[i]);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001321 }
1322 }
1323 break;
1324
1325 case EOpBitwiseAnd:
1326 resultArray = new TConstantUnion[objectSize];
1327 for (size_t i = 0; i < objectSize; i++)
1328 resultArray[i] = leftArray[i] & rightArray[i];
1329 break;
1330 case EOpBitwiseXor:
1331 resultArray = new TConstantUnion[objectSize];
1332 for (size_t i = 0; i < objectSize; i++)
1333 resultArray[i] = leftArray[i] ^ rightArray[i];
1334 break;
1335 case EOpBitwiseOr:
1336 resultArray = new TConstantUnion[objectSize];
1337 for (size_t i = 0; i < objectSize; i++)
1338 resultArray[i] = leftArray[i] | rightArray[i];
1339 break;
1340 case EOpBitShiftLeft:
1341 resultArray = new TConstantUnion[objectSize];
1342 for (size_t i = 0; i < objectSize; i++)
1343 resultArray[i] = leftArray[i] << rightArray[i];
1344 break;
1345 case EOpBitShiftRight:
1346 resultArray = new TConstantUnion[objectSize];
1347 for (size_t i = 0; i < objectSize; i++)
1348 resultArray[i] = leftArray[i] >> rightArray[i];
1349 break;
1350
1351 case EOpLessThan:
1352 ASSERT(objectSize == 1);
1353 resultArray = new TConstantUnion[1];
1354 resultArray->setBConst(*leftArray < *rightArray);
1355 break;
1356
1357 case EOpGreaterThan:
1358 ASSERT(objectSize == 1);
1359 resultArray = new TConstantUnion[1];
1360 resultArray->setBConst(*leftArray > *rightArray);
1361 break;
1362
1363 case EOpLessThanEqual:
1364 ASSERT(objectSize == 1);
1365 resultArray = new TConstantUnion[1];
1366 resultArray->setBConst(!(*leftArray > *rightArray));
1367 break;
1368
1369 case EOpGreaterThanEqual:
1370 ASSERT(objectSize == 1);
1371 resultArray = new TConstantUnion[1];
1372 resultArray->setBConst(!(*leftArray < *rightArray));
1373 break;
1374
1375 case EOpEqual:
1376 case EOpNotEqual:
1377 {
1378 resultArray = new TConstantUnion[1];
1379 bool equal = true;
Olli Etuaho40d9edf2015-11-12 17:30:34 +02001380 for (size_t i = 0; i < objectSize; i++)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001381 {
Olli Etuaho40d9edf2015-11-12 17:30:34 +02001382 if (leftArray[i] != rightArray[i])
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001383 {
Olli Etuaho40d9edf2015-11-12 17:30:34 +02001384 equal = false;
1385 break; // break out of for loop
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001386 }
1387 }
1388 if (op == EOpEqual)
1389 {
1390 resultArray->setBConst(equal);
1391 }
1392 else
1393 {
1394 resultArray->setBConst(!equal);
1395 }
1396 }
1397 break;
1398
1399 default:
Olli Etuaho3fdec912016-08-18 15:08:06 +03001400 UNREACHABLE();
1401 return nullptr;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001402 }
1403 return resultArray;
1404}
1405
Olli Etuahof119a262016-08-19 15:54:22 +03001406// The fold functions do operations on a constant at GLSL compile time, without generating run-time
1407// code. Returns the constant value to keep using. Nullptr should not be returned.
1408TConstantUnion *TIntermConstantUnion::foldUnaryNonComponentWise(TOperator op)
Jamie Madillb1a85f42014-08-19 15:23:24 -04001409{
Olli Etuahof119a262016-08-19 15:54:22 +03001410 // Do operations where the return type may have a different number of components compared to the
1411 // operand type.
Jamie Madillb1a85f42014-08-19 15:23:24 -04001412
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001413 const TConstantUnion *operandArray = getUnionArrayPointer();
Olli Etuahof119a262016-08-19 15:54:22 +03001414 ASSERT(operandArray);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301415
1416 size_t objectSize = getType().getObjectSize();
1417 TConstantUnion *resultArray = nullptr;
1418 switch (op)
1419 {
Olli Etuahof119a262016-08-19 15:54:22 +03001420 case EOpAny:
1421 ASSERT(getType().getBasicType() == EbtBool);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301422 resultArray = new TConstantUnion();
1423 resultArray->setBConst(false);
1424 for (size_t i = 0; i < objectSize; i++)
1425 {
1426 if (operandArray[i].getBConst())
1427 {
1428 resultArray->setBConst(true);
1429 break;
1430 }
1431 }
1432 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301433
Olli Etuahof119a262016-08-19 15:54:22 +03001434 case EOpAll:
1435 ASSERT(getType().getBasicType() == EbtBool);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301436 resultArray = new TConstantUnion();
1437 resultArray->setBConst(true);
1438 for (size_t i = 0; i < objectSize; i++)
1439 {
1440 if (!operandArray[i].getBConst())
1441 {
1442 resultArray->setBConst(false);
1443 break;
1444 }
1445 }
1446 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301447
Olli Etuahof119a262016-08-19 15:54:22 +03001448 case EOpLength:
1449 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301450 resultArray = new TConstantUnion();
1451 resultArray->setFConst(VectorLength(operandArray, objectSize));
1452 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301453
Olli Etuahof119a262016-08-19 15:54:22 +03001454 case EOpTranspose:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301455 {
Olli Etuahof119a262016-08-19 15:54:22 +03001456 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301457 resultArray = new TConstantUnion[objectSize];
1458 angle::Matrix<float> result =
Olli Etuahod5da5052016-08-29 13:16:55 +03001459 GetMatrix(operandArray, getType().getRows(), getType().getCols()).transpose();
Arun Patoleab2b9a22015-07-06 18:27:56 +05301460 SetUnionArrayFromMatrix(result, resultArray);
1461 break;
1462 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301463
Olli Etuahof119a262016-08-19 15:54:22 +03001464 case EOpDeterminant:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301465 {
Olli Etuahof119a262016-08-19 15:54:22 +03001466 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301467 unsigned int size = getType().getNominalSize();
1468 ASSERT(size >= 2 && size <= 4);
1469 resultArray = new TConstantUnion();
1470 resultArray->setFConst(GetMatrix(operandArray, size).determinant());
1471 break;
1472 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301473
Olli Etuahof119a262016-08-19 15:54:22 +03001474 case EOpInverse:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301475 {
Olli Etuahof119a262016-08-19 15:54:22 +03001476 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301477 unsigned int size = getType().getNominalSize();
1478 ASSERT(size >= 2 && size <= 4);
Olli Etuahof119a262016-08-19 15:54:22 +03001479 resultArray = new TConstantUnion[objectSize];
Arun Patoleab2b9a22015-07-06 18:27:56 +05301480 angle::Matrix<float> result = GetMatrix(operandArray, size).inverse();
1481 SetUnionArrayFromMatrix(result, resultArray);
1482 break;
1483 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301484
Olli Etuahof119a262016-08-19 15:54:22 +03001485 case EOpPackSnorm2x16:
1486 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301487 ASSERT(getType().getNominalSize() == 2);
1488 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03001489 resultArray->setUConst(
1490 gl::packSnorm2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
Arun Patoleab2b9a22015-07-06 18:27:56 +05301491 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301492
Olli Etuahof119a262016-08-19 15:54:22 +03001493 case EOpUnpackSnorm2x16:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301494 {
Olli Etuahof119a262016-08-19 15:54:22 +03001495 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301496 resultArray = new TConstantUnion[2];
1497 float f1, f2;
1498 gl::unpackSnorm2x16(operandArray[0].getUConst(), &f1, &f2);
1499 resultArray[0].setFConst(f1);
1500 resultArray[1].setFConst(f2);
1501 break;
1502 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301503
Olli Etuahof119a262016-08-19 15:54:22 +03001504 case EOpPackUnorm2x16:
1505 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301506 ASSERT(getType().getNominalSize() == 2);
1507 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03001508 resultArray->setUConst(
1509 gl::packUnorm2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
Arun Patoleab2b9a22015-07-06 18:27:56 +05301510 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301511
Olli Etuahof119a262016-08-19 15:54:22 +03001512 case EOpUnpackUnorm2x16:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301513 {
Olli Etuahof119a262016-08-19 15:54:22 +03001514 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301515 resultArray = new TConstantUnion[2];
1516 float f1, f2;
1517 gl::unpackUnorm2x16(operandArray[0].getUConst(), &f1, &f2);
1518 resultArray[0].setFConst(f1);
1519 resultArray[1].setFConst(f2);
1520 break;
1521 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301522
Olli Etuahof119a262016-08-19 15:54:22 +03001523 case EOpPackHalf2x16:
1524 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301525 ASSERT(getType().getNominalSize() == 2);
1526 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03001527 resultArray->setUConst(
1528 gl::packHalf2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
Arun Patoleab2b9a22015-07-06 18:27:56 +05301529 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301530
Olli Etuahof119a262016-08-19 15:54:22 +03001531 case EOpUnpackHalf2x16:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301532 {
Olli Etuahof119a262016-08-19 15:54:22 +03001533 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301534 resultArray = new TConstantUnion[2];
1535 float f1, f2;
1536 gl::unpackHalf2x16(operandArray[0].getUConst(), &f1, &f2);
1537 resultArray[0].setFConst(f1);
1538 resultArray[1].setFConst(f2);
1539 break;
1540 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301541
Olli Etuahof119a262016-08-19 15:54:22 +03001542 default:
1543 UNREACHABLE();
1544 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301545 }
1546
1547 return resultArray;
1548}
1549
Olli Etuahof119a262016-08-19 15:54:22 +03001550TConstantUnion *TIntermConstantUnion::foldUnaryComponentWise(TOperator op,
1551 TDiagnostics *diagnostics)
Arun Patoleab2b9a22015-07-06 18:27:56 +05301552{
Olli Etuahof119a262016-08-19 15:54:22 +03001553 // Do unary operations where each component of the result is computed based on the corresponding
1554 // component of the operand. Also folds normalize, though the divisor in that case takes all
1555 // components into account.
Arun Patoleab2b9a22015-07-06 18:27:56 +05301556
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001557 const TConstantUnion *operandArray = getUnionArrayPointer();
Olli Etuahof119a262016-08-19 15:54:22 +03001558 ASSERT(operandArray);
Jamie Madillb1a85f42014-08-19 15:23:24 -04001559
1560 size_t objectSize = getType().getObjectSize();
1561
Arun Patoleab2b9a22015-07-06 18:27:56 +05301562 TConstantUnion *resultArray = new TConstantUnion[objectSize];
1563 for (size_t i = 0; i < objectSize; i++)
Arun Patole9d0b1f92015-05-20 14:27:17 +05301564 {
Arun Patoleab2b9a22015-07-06 18:27:56 +05301565 switch(op)
Arun Patole9d0b1f92015-05-20 14:27:17 +05301566 {
Olli Etuahof119a262016-08-19 15:54:22 +03001567 case EOpNegative:
1568 switch (getType().getBasicType())
1569 {
1570 case EbtFloat:
1571 resultArray[i].setFConst(-operandArray[i].getFConst());
1572 break;
1573 case EbtInt:
1574 resultArray[i].setIConst(-operandArray[i].getIConst());
1575 break;
1576 case EbtUInt:
1577 resultArray[i].setUConst(static_cast<unsigned int>(
1578 -static_cast<int>(operandArray[i].getUConst())));
1579 break;
1580 default:
1581 UNREACHABLE();
1582 return nullptr;
1583 }
Arun Patole1155ddd2015-06-05 18:04:36 +05301584 break;
Arun Patolecdfa8f52015-06-30 17:48:25 +05301585
Olli Etuahof119a262016-08-19 15:54:22 +03001586 case EOpPositive:
1587 switch (getType().getBasicType())
1588 {
1589 case EbtFloat:
1590 resultArray[i].setFConst(operandArray[i].getFConst());
1591 break;
1592 case EbtInt:
1593 resultArray[i].setIConst(operandArray[i].getIConst());
1594 break;
1595 case EbtUInt:
1596 resultArray[i].setUConst(static_cast<unsigned int>(
1597 static_cast<int>(operandArray[i].getUConst())));
1598 break;
1599 default:
1600 UNREACHABLE();
1601 return nullptr;
1602 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301603 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301604
Olli Etuahof119a262016-08-19 15:54:22 +03001605 case EOpLogicalNot:
1606 switch (getType().getBasicType())
1607 {
1608 case EbtBool:
1609 resultArray[i].setBConst(!operandArray[i].getBConst());
1610 break;
1611 default:
1612 UNREACHABLE();
1613 return nullptr;
1614 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301615 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301616
Olli Etuahof119a262016-08-19 15:54:22 +03001617 case EOpBitwiseNot:
1618 switch (getType().getBasicType())
1619 {
1620 case EbtInt:
1621 resultArray[i].setIConst(~operandArray[i].getIConst());
1622 break;
1623 case EbtUInt:
1624 resultArray[i].setUConst(~operandArray[i].getUConst());
1625 break;
1626 default:
1627 UNREACHABLE();
1628 return nullptr;
1629 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301630 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301631
Olli Etuahof119a262016-08-19 15:54:22 +03001632 case EOpRadians:
1633 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301634 resultArray[i].setFConst(kDegreesToRadiansMultiplier * operandArray[i].getFConst());
1635 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301636
Olli Etuahof119a262016-08-19 15:54:22 +03001637 case EOpDegrees:
1638 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301639 resultArray[i].setFConst(kRadiansToDegreesMultiplier * operandArray[i].getFConst());
1640 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301641
Olli Etuahof119a262016-08-19 15:54:22 +03001642 case EOpSin:
1643 foldFloatTypeUnary(operandArray[i], &sinf, &resultArray[i]);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301644 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301645
Olli Etuahof119a262016-08-19 15:54:22 +03001646 case EOpCos:
1647 foldFloatTypeUnary(operandArray[i], &cosf, &resultArray[i]);
1648 break;
1649
1650 case EOpTan:
1651 foldFloatTypeUnary(operandArray[i], &tanf, &resultArray[i]);
1652 break;
1653
1654 case EOpAsin:
1655 // For asin(x), results are undefined if |x| > 1, we are choosing to set result to
1656 // 0.
1657 if (fabsf(operandArray[i].getFConst()) > 1.0f)
1658 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1659 diagnostics, &resultArray[i]);
1660 else
1661 foldFloatTypeUnary(operandArray[i], &asinf, &resultArray[i]);
1662 break;
1663
1664 case EOpAcos:
1665 // For acos(x), results are undefined if |x| > 1, we are choosing to set result to
1666 // 0.
1667 if (fabsf(operandArray[i].getFConst()) > 1.0f)
1668 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1669 diagnostics, &resultArray[i]);
1670 else
1671 foldFloatTypeUnary(operandArray[i], &acosf, &resultArray[i]);
1672 break;
1673
1674 case EOpAtan:
1675 foldFloatTypeUnary(operandArray[i], &atanf, &resultArray[i]);
1676 break;
1677
1678 case EOpSinh:
1679 foldFloatTypeUnary(operandArray[i], &sinhf, &resultArray[i]);
1680 break;
1681
1682 case EOpCosh:
1683 foldFloatTypeUnary(operandArray[i], &coshf, &resultArray[i]);
1684 break;
1685
1686 case EOpTanh:
1687 foldFloatTypeUnary(operandArray[i], &tanhf, &resultArray[i]);
1688 break;
1689
1690 case EOpAsinh:
1691 foldFloatTypeUnary(operandArray[i], &asinhf, &resultArray[i]);
1692 break;
1693
1694 case EOpAcosh:
1695 // For acosh(x), results are undefined if x < 1, we are choosing to set result to 0.
1696 if (operandArray[i].getFConst() < 1.0f)
1697 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1698 diagnostics, &resultArray[i]);
1699 else
1700 foldFloatTypeUnary(operandArray[i], &acoshf, &resultArray[i]);
1701 break;
1702
1703 case EOpAtanh:
1704 // For atanh(x), results are undefined if |x| >= 1, we are choosing to set result to
1705 // 0.
1706 if (fabsf(operandArray[i].getFConst()) >= 1.0f)
1707 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1708 diagnostics, &resultArray[i]);
1709 else
1710 foldFloatTypeUnary(operandArray[i], &atanhf, &resultArray[i]);
1711 break;
1712
1713 case EOpAbs:
1714 switch (getType().getBasicType())
Arun Patoleab2b9a22015-07-06 18:27:56 +05301715 {
Olli Etuahof119a262016-08-19 15:54:22 +03001716 case EbtFloat:
1717 resultArray[i].setFConst(fabsf(operandArray[i].getFConst()));
1718 break;
1719 case EbtInt:
1720 resultArray[i].setIConst(abs(operandArray[i].getIConst()));
1721 break;
1722 default:
1723 UNREACHABLE();
1724 return nullptr;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301725 }
1726 break;
Olli Etuahof119a262016-08-19 15:54:22 +03001727
1728 case EOpSign:
1729 switch (getType().getBasicType())
Arun Patoleab2b9a22015-07-06 18:27:56 +05301730 {
Olli Etuahof119a262016-08-19 15:54:22 +03001731 case EbtFloat:
1732 {
1733 float fConst = operandArray[i].getFConst();
1734 float fResult = 0.0f;
1735 if (fConst > 0.0f)
1736 fResult = 1.0f;
1737 else if (fConst < 0.0f)
1738 fResult = -1.0f;
1739 resultArray[i].setFConst(fResult);
1740 break;
1741 }
1742 case EbtInt:
1743 {
1744 int iConst = operandArray[i].getIConst();
1745 int iResult = 0;
1746 if (iConst > 0)
1747 iResult = 1;
1748 else if (iConst < 0)
1749 iResult = -1;
1750 resultArray[i].setIConst(iResult);
1751 break;
1752 }
1753 default:
1754 UNREACHABLE();
1755 return nullptr;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301756 }
1757 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301758
Olli Etuahof119a262016-08-19 15:54:22 +03001759 case EOpFloor:
1760 foldFloatTypeUnary(operandArray[i], &floorf, &resultArray[i]);
1761 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301762
Olli Etuahof119a262016-08-19 15:54:22 +03001763 case EOpTrunc:
1764 foldFloatTypeUnary(operandArray[i], &truncf, &resultArray[i]);
1765 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301766
Olli Etuahof119a262016-08-19 15:54:22 +03001767 case EOpRound:
1768 foldFloatTypeUnary(operandArray[i], &roundf, &resultArray[i]);
1769 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301770
Olli Etuahof119a262016-08-19 15:54:22 +03001771 case EOpRoundEven:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301772 {
Olli Etuahof119a262016-08-19 15:54:22 +03001773 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301774 float x = operandArray[i].getFConst();
1775 float result;
1776 float fractPart = modff(x, &result);
1777 if (fabsf(fractPart) == 0.5f)
1778 result = 2.0f * roundf(x / 2.0f);
1779 else
1780 result = roundf(x);
1781 resultArray[i].setFConst(result);
1782 break;
1783 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301784
Olli Etuahof119a262016-08-19 15:54:22 +03001785 case EOpCeil:
1786 foldFloatTypeUnary(operandArray[i], &ceilf, &resultArray[i]);
1787 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301788
Olli Etuahof119a262016-08-19 15:54:22 +03001789 case EOpFract:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301790 {
Olli Etuahof119a262016-08-19 15:54:22 +03001791 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301792 float x = operandArray[i].getFConst();
1793 resultArray[i].setFConst(x - floorf(x));
1794 break;
1795 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301796
Olli Etuahof119a262016-08-19 15:54:22 +03001797 case EOpIsNan:
1798 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05301799 resultArray[i].setBConst(gl::isNaN(operandArray[0].getFConst()));
1800 break;
Arun Patole551279e2015-07-07 18:18:23 +05301801
Olli Etuahof119a262016-08-19 15:54:22 +03001802 case EOpIsInf:
1803 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05301804 resultArray[i].setBConst(gl::isInf(operandArray[0].getFConst()));
1805 break;
Arun Patole551279e2015-07-07 18:18:23 +05301806
Olli Etuahof119a262016-08-19 15:54:22 +03001807 case EOpFloatBitsToInt:
1808 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05301809 resultArray[i].setIConst(gl::bitCast<int32_t>(operandArray[0].getFConst()));
1810 break;
Arun Patole551279e2015-07-07 18:18:23 +05301811
Olli Etuahof119a262016-08-19 15:54:22 +03001812 case EOpFloatBitsToUint:
1813 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05301814 resultArray[i].setUConst(gl::bitCast<uint32_t>(operandArray[0].getFConst()));
1815 break;
Arun Patole551279e2015-07-07 18:18:23 +05301816
Olli Etuahof119a262016-08-19 15:54:22 +03001817 case EOpIntBitsToFloat:
1818 ASSERT(getType().getBasicType() == EbtInt);
Arun Patole551279e2015-07-07 18:18:23 +05301819 resultArray[i].setFConst(gl::bitCast<float>(operandArray[0].getIConst()));
1820 break;
Arun Patole551279e2015-07-07 18:18:23 +05301821
Olli Etuahof119a262016-08-19 15:54:22 +03001822 case EOpUintBitsToFloat:
1823 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patole551279e2015-07-07 18:18:23 +05301824 resultArray[i].setFConst(gl::bitCast<float>(operandArray[0].getUConst()));
1825 break;
Arun Patole551279e2015-07-07 18:18:23 +05301826
Olli Etuahof119a262016-08-19 15:54:22 +03001827 case EOpExp:
1828 foldFloatTypeUnary(operandArray[i], &expf, &resultArray[i]);
1829 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301830
Olli Etuahof119a262016-08-19 15:54:22 +03001831 case EOpLog:
1832 // For log(x), results are undefined if x <= 0, we are choosing to set result to 0.
1833 if (operandArray[i].getFConst() <= 0.0f)
1834 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1835 diagnostics, &resultArray[i]);
1836 else
1837 foldFloatTypeUnary(operandArray[i], &logf, &resultArray[i]);
1838 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301839
Olli Etuahof119a262016-08-19 15:54:22 +03001840 case EOpExp2:
1841 foldFloatTypeUnary(operandArray[i], &exp2f, &resultArray[i]);
1842 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301843
Olli Etuahof119a262016-08-19 15:54:22 +03001844 case EOpLog2:
1845 // For log2(x), results are undefined if x <= 0, we are choosing to set result to 0.
1846 // And log2f is not available on some plarforms like old android, so just using
1847 // log(x)/log(2) here.
1848 if (operandArray[i].getFConst() <= 0.0f)
1849 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1850 diagnostics, &resultArray[i]);
1851 else
1852 {
1853 foldFloatTypeUnary(operandArray[i], &logf, &resultArray[i]);
1854 resultArray[i].setFConst(resultArray[i].getFConst() / logf(2.0f));
1855 }
1856 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301857
Olli Etuahof119a262016-08-19 15:54:22 +03001858 case EOpSqrt:
1859 // For sqrt(x), results are undefined if x < 0, we are choosing to set result to 0.
1860 if (operandArray[i].getFConst() < 0.0f)
1861 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1862 diagnostics, &resultArray[i]);
1863 else
1864 foldFloatTypeUnary(operandArray[i], &sqrtf, &resultArray[i]);
1865 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301866
Olli Etuahof119a262016-08-19 15:54:22 +03001867 case EOpInverseSqrt:
1868 // There is no stdlib built-in function equavalent for GLES built-in inversesqrt(),
1869 // so getting the square root first using builtin function sqrt() and then taking
1870 // its inverse.
1871 // Also, for inversesqrt(x), results are undefined if x <= 0, we are choosing to set
1872 // result to 0.
1873 if (operandArray[i].getFConst() <= 0.0f)
1874 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1875 diagnostics, &resultArray[i]);
1876 else
1877 {
1878 foldFloatTypeUnary(operandArray[i], &sqrtf, &resultArray[i]);
1879 resultArray[i].setFConst(1.0f / resultArray[i].getFConst());
1880 }
1881 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301882
Olli Etuahof119a262016-08-19 15:54:22 +03001883 case EOpVectorLogicalNot:
1884 ASSERT(getType().getBasicType() == EbtBool);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301885 resultArray[i].setBConst(!operandArray[i].getBConst());
1886 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301887
Olli Etuahof119a262016-08-19 15:54:22 +03001888 case EOpNormalize:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301889 {
Olli Etuahof119a262016-08-19 15:54:22 +03001890 ASSERT(getType().getBasicType() == EbtFloat);
1891 float x = operandArray[i].getFConst();
Arun Patoleab2b9a22015-07-06 18:27:56 +05301892 float length = VectorLength(operandArray, objectSize);
1893 if (length)
1894 resultArray[i].setFConst(x / length);
1895 else
Olli Etuahof119a262016-08-19 15:54:22 +03001896 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
1897 diagnostics, &resultArray[i]);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301898 break;
1899 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301900
Olli Etuahof119a262016-08-19 15:54:22 +03001901 case EOpDFdx:
1902 case EOpDFdy:
1903 case EOpFwidth:
1904 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole0c5409f2015-07-08 15:17:53 +05301905 // Derivatives of constant arguments should be 0.
1906 resultArray[i].setFConst(0.0f);
1907 break;
Arun Patole0c5409f2015-07-08 15:17:53 +05301908
Olli Etuahof119a262016-08-19 15:54:22 +03001909 default:
1910 return nullptr;
Arun Patole9d0b1f92015-05-20 14:27:17 +05301911 }
Arun Patole9d0b1f92015-05-20 14:27:17 +05301912 }
Jamie Madillb1a85f42014-08-19 15:23:24 -04001913
Arun Patoleab2b9a22015-07-06 18:27:56 +05301914 return resultArray;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001915}
1916
Olli Etuahof119a262016-08-19 15:54:22 +03001917void TIntermConstantUnion::foldFloatTypeUnary(const TConstantUnion &parameter,
1918 FloatTypeUnaryFunc builtinFunc,
1919 TConstantUnion *result) const
Arun Patole9dea48f2015-04-02 11:45:09 +05301920{
1921 ASSERT(builtinFunc);
1922
Olli Etuahof119a262016-08-19 15:54:22 +03001923 ASSERT(getType().getBasicType() == EbtFloat);
1924 result->setFConst(builtinFunc(parameter.getFConst()));
Arun Patole9dea48f2015-04-02 11:45:09 +05301925}
1926
Jamie Madillb1a85f42014-08-19 15:23:24 -04001927// static
Olli Etuahof119a262016-08-19 15:54:22 +03001928TConstantUnion *TIntermConstantUnion::FoldAggregateConstructor(TIntermAggregate *aggregate)
Olli Etuaho1d122782015-11-06 15:35:17 +02001929{
1930 ASSERT(aggregate->getSequence()->size() > 0u);
1931 size_t resultSize = aggregate->getType().getObjectSize();
1932 TConstantUnion *resultArray = new TConstantUnion[resultSize];
1933 TBasicType basicType = aggregate->getBasicType();
1934
1935 size_t resultIndex = 0u;
1936
1937 if (aggregate->getSequence()->size() == 1u)
1938 {
1939 TIntermNode *argument = aggregate->getSequence()->front();
1940 TIntermConstantUnion *argumentConstant = argument->getAsConstantUnion();
1941 const TConstantUnion *argumentUnionArray = argumentConstant->getUnionArrayPointer();
1942 // Check the special case of constructing a matrix diagonal from a single scalar,
1943 // or a vector from a single scalar.
1944 if (argumentConstant->getType().getObjectSize() == 1u)
1945 {
1946 if (aggregate->isMatrix())
1947 {
1948 int resultCols = aggregate->getType().getCols();
1949 int resultRows = aggregate->getType().getRows();
1950 for (int col = 0; col < resultCols; ++col)
1951 {
1952 for (int row = 0; row < resultRows; ++row)
1953 {
1954 if (col == row)
1955 {
1956 resultArray[resultIndex].cast(basicType, argumentUnionArray[0]);
1957 }
1958 else
1959 {
1960 resultArray[resultIndex].setFConst(0.0f);
1961 }
1962 ++resultIndex;
1963 }
1964 }
1965 }
1966 else
1967 {
1968 while (resultIndex < resultSize)
1969 {
1970 resultArray[resultIndex].cast(basicType, argumentUnionArray[0]);
1971 ++resultIndex;
1972 }
1973 }
1974 ASSERT(resultIndex == resultSize);
1975 return resultArray;
1976 }
1977 else if (aggregate->isMatrix() && argumentConstant->isMatrix())
1978 {
1979 // The special case of constructing a matrix from a matrix.
1980 int argumentCols = argumentConstant->getType().getCols();
1981 int argumentRows = argumentConstant->getType().getRows();
1982 int resultCols = aggregate->getType().getCols();
1983 int resultRows = aggregate->getType().getRows();
1984 for (int col = 0; col < resultCols; ++col)
1985 {
1986 for (int row = 0; row < resultRows; ++row)
1987 {
1988 if (col < argumentCols && row < argumentRows)
1989 {
1990 resultArray[resultIndex].cast(basicType,
1991 argumentUnionArray[col * argumentRows + row]);
1992 }
1993 else if (col == row)
1994 {
1995 resultArray[resultIndex].setFConst(1.0f);
1996 }
1997 else
1998 {
1999 resultArray[resultIndex].setFConst(0.0f);
2000 }
2001 ++resultIndex;
2002 }
2003 }
2004 ASSERT(resultIndex == resultSize);
2005 return resultArray;
2006 }
2007 }
2008
2009 for (TIntermNode *&argument : *aggregate->getSequence())
2010 {
2011 TIntermConstantUnion *argumentConstant = argument->getAsConstantUnion();
2012 size_t argumentSize = argumentConstant->getType().getObjectSize();
2013 const TConstantUnion *argumentUnionArray = argumentConstant->getUnionArrayPointer();
2014 for (size_t i = 0u; i < argumentSize; ++i)
2015 {
2016 if (resultIndex >= resultSize)
2017 break;
2018 resultArray[resultIndex].cast(basicType, argumentUnionArray[i]);
2019 ++resultIndex;
2020 }
2021 }
2022 ASSERT(resultIndex == resultSize);
2023 return resultArray;
2024}
2025
2026// static
Olli Etuahof119a262016-08-19 15:54:22 +03002027TConstantUnion *TIntermConstantUnion::FoldAggregateBuiltIn(TIntermAggregate *aggregate,
2028 TDiagnostics *diagnostics)
Arun Patole274f0702015-05-05 13:33:30 +05302029{
Olli Etuahob43846e2015-06-02 18:18:57 +03002030 TOperator op = aggregate->getOp();
Arun Patole274f0702015-05-05 13:33:30 +05302031 TIntermSequence *sequence = aggregate->getSequence();
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002032 unsigned int paramsCount = static_cast<unsigned int>(sequence->size());
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002033 std::vector<const TConstantUnion *> unionArrays(paramsCount);
Arun Patole274f0702015-05-05 13:33:30 +05302034 std::vector<size_t> objectSizes(paramsCount);
Olli Etuahob43846e2015-06-02 18:18:57 +03002035 size_t maxObjectSize = 0;
Arun Patole274f0702015-05-05 13:33:30 +05302036 TBasicType basicType = EbtVoid;
2037 TSourceLoc loc;
2038 for (unsigned int i = 0; i < paramsCount; i++)
2039 {
2040 TIntermConstantUnion *paramConstant = (*sequence)[i]->getAsConstantUnion();
Olli Etuahob43846e2015-06-02 18:18:57 +03002041 ASSERT(paramConstant != nullptr); // Should be checked already.
Arun Patole274f0702015-05-05 13:33:30 +05302042
2043 if (i == 0)
2044 {
2045 basicType = paramConstant->getType().getBasicType();
2046 loc = paramConstant->getLine();
2047 }
2048 unionArrays[i] = paramConstant->getUnionArrayPointer();
2049 objectSizes[i] = paramConstant->getType().getObjectSize();
Olli Etuahob43846e2015-06-02 18:18:57 +03002050 if (objectSizes[i] > maxObjectSize)
2051 maxObjectSize = objectSizes[i];
Arun Patole274f0702015-05-05 13:33:30 +05302052 }
2053
Olli Etuahod5da5052016-08-29 13:16:55 +03002054 if (!(*sequence)[0]->getAsTyped()->isMatrix() && aggregate->getOp() != EOpOuterProduct)
Arun Patole7fa33552015-06-10 15:15:18 +05302055 {
2056 for (unsigned int i = 0; i < paramsCount; i++)
2057 if (objectSizes[i] != maxObjectSize)
2058 unionArrays[i] = Vectorize(*unionArrays[i], maxObjectSize);
2059 }
Arun Patole274f0702015-05-05 13:33:30 +05302060
Olli Etuahob43846e2015-06-02 18:18:57 +03002061 TConstantUnion *resultArray = nullptr;
Arun Patole274f0702015-05-05 13:33:30 +05302062 if (paramsCount == 2)
2063 {
2064 //
2065 // Binary built-in
2066 //
2067 switch (op)
2068 {
Olli Etuahof119a262016-08-19 15:54:22 +03002069 case EOpAtan:
Arun Patolebf790422015-05-18 17:53:04 +05302070 {
Olli Etuahof119a262016-08-19 15:54:22 +03002071 ASSERT(basicType == EbtFloat);
2072 resultArray = new TConstantUnion[maxObjectSize];
2073 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302074 {
Olli Etuahof119a262016-08-19 15:54:22 +03002075 float y = unionArrays[0][i].getFConst();
2076 float x = unionArrays[1][i].getFConst();
2077 // Results are undefined if x and y are both 0.
2078 if (x == 0.0f && y == 0.0f)
2079 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
2080 &resultArray[i]);
2081 else
2082 resultArray[i].setFConst(atan2f(y, x));
Arun Patolebf790422015-05-18 17:53:04 +05302083 }
Olli Etuahof119a262016-08-19 15:54:22 +03002084 break;
Arun Patolebf790422015-05-18 17:53:04 +05302085 }
Arun Patolebf790422015-05-18 17:53:04 +05302086
Olli Etuahof119a262016-08-19 15:54:22 +03002087 case EOpPow:
Arun Patolebf790422015-05-18 17:53:04 +05302088 {
Olli Etuahof119a262016-08-19 15:54:22 +03002089 ASSERT(basicType == EbtFloat);
2090 resultArray = new TConstantUnion[maxObjectSize];
2091 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302092 {
Olli Etuahof119a262016-08-19 15:54:22 +03002093 float x = unionArrays[0][i].getFConst();
2094 float y = unionArrays[1][i].getFConst();
2095 // Results are undefined if x < 0.
2096 // Results are undefined if x = 0 and y <= 0.
2097 if (x < 0.0f)
2098 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
2099 &resultArray[i]);
2100 else if (x == 0.0f && y <= 0.0f)
2101 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
2102 &resultArray[i]);
2103 else
2104 resultArray[i].setFConst(powf(x, y));
Arun Patolebf790422015-05-18 17:53:04 +05302105 }
Olli Etuahof119a262016-08-19 15:54:22 +03002106 break;
Arun Patolebf790422015-05-18 17:53:04 +05302107 }
Arun Patolebf790422015-05-18 17:53:04 +05302108
Olli Etuahof119a262016-08-19 15:54:22 +03002109 case EOpMod:
Arun Patolebf790422015-05-18 17:53:04 +05302110 {
Olli Etuahof119a262016-08-19 15:54:22 +03002111 ASSERT(basicType == EbtFloat);
2112 resultArray = new TConstantUnion[maxObjectSize];
2113 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302114 {
Olli Etuahof119a262016-08-19 15:54:22 +03002115 float x = unionArrays[0][i].getFConst();
2116 float y = unionArrays[1][i].getFConst();
2117 resultArray[i].setFConst(x - y * floorf(x / y));
Arun Patolebf790422015-05-18 17:53:04 +05302118 }
Olli Etuahof119a262016-08-19 15:54:22 +03002119 break;
Arun Patolebf790422015-05-18 17:53:04 +05302120 }
Arun Patolebf790422015-05-18 17:53:04 +05302121
Olli Etuahof119a262016-08-19 15:54:22 +03002122 case EOpMin:
Arun Patole274f0702015-05-05 13:33:30 +05302123 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002124 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole274f0702015-05-05 13:33:30 +05302125 for (size_t i = 0; i < maxObjectSize; i++)
2126 {
2127 switch (basicType)
2128 {
Olli Etuahof119a262016-08-19 15:54:22 +03002129 case EbtFloat:
2130 resultArray[i].setFConst(std::min(unionArrays[0][i].getFConst(),
2131 unionArrays[1][i].getFConst()));
2132 break;
2133 case EbtInt:
2134 resultArray[i].setIConst(std::min(unionArrays[0][i].getIConst(),
2135 unionArrays[1][i].getIConst()));
2136 break;
2137 case EbtUInt:
2138 resultArray[i].setUConst(std::min(unionArrays[0][i].getUConst(),
2139 unionArrays[1][i].getUConst()));
2140 break;
2141 default:
2142 UNREACHABLE();
2143 break;
Arun Patole274f0702015-05-05 13:33:30 +05302144 }
2145 }
Olli Etuahof119a262016-08-19 15:54:22 +03002146 break;
Arun Patole274f0702015-05-05 13:33:30 +05302147 }
Arun Patole274f0702015-05-05 13:33:30 +05302148
Olli Etuahof119a262016-08-19 15:54:22 +03002149 case EOpMax:
Arun Patole274f0702015-05-05 13:33:30 +05302150 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002151 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole274f0702015-05-05 13:33:30 +05302152 for (size_t i = 0; i < maxObjectSize; i++)
2153 {
2154 switch (basicType)
2155 {
Olli Etuahof119a262016-08-19 15:54:22 +03002156 case EbtFloat:
2157 resultArray[i].setFConst(std::max(unionArrays[0][i].getFConst(),
2158 unionArrays[1][i].getFConst()));
2159 break;
2160 case EbtInt:
2161 resultArray[i].setIConst(std::max(unionArrays[0][i].getIConst(),
2162 unionArrays[1][i].getIConst()));
2163 break;
2164 case EbtUInt:
2165 resultArray[i].setUConst(std::max(unionArrays[0][i].getUConst(),
2166 unionArrays[1][i].getUConst()));
2167 break;
2168 default:
2169 UNREACHABLE();
2170 break;
Arun Patole274f0702015-05-05 13:33:30 +05302171 }
2172 }
Olli Etuahof119a262016-08-19 15:54:22 +03002173 break;
Arun Patole274f0702015-05-05 13:33:30 +05302174 }
Arun Patole274f0702015-05-05 13:33:30 +05302175
Olli Etuahof119a262016-08-19 15:54:22 +03002176 case EOpStep:
Arun Patolebf790422015-05-18 17:53:04 +05302177 {
Olli Etuahof119a262016-08-19 15:54:22 +03002178 ASSERT(basicType == EbtFloat);
2179 resultArray = new TConstantUnion[maxObjectSize];
2180 for (size_t i = 0; i < maxObjectSize; i++)
2181 resultArray[i].setFConst(
2182 unionArrays[1][i].getFConst() < unionArrays[0][i].getFConst() ? 0.0f
2183 : 1.0f);
2184 break;
Arun Patolebf790422015-05-18 17:53:04 +05302185 }
Arun Patolebf790422015-05-18 17:53:04 +05302186
Olli Etuahof119a262016-08-19 15:54:22 +03002187 case EOpLessThan:
Arun Patole9d0b1f92015-05-20 14:27:17 +05302188 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002189 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole9d0b1f92015-05-20 14:27:17 +05302190 for (size_t i = 0; i < maxObjectSize; i++)
2191 {
2192 switch (basicType)
2193 {
Olli Etuahof119a262016-08-19 15:54:22 +03002194 case EbtFloat:
2195 resultArray[i].setBConst(unionArrays[0][i].getFConst() <
2196 unionArrays[1][i].getFConst());
2197 break;
2198 case EbtInt:
2199 resultArray[i].setBConst(unionArrays[0][i].getIConst() <
2200 unionArrays[1][i].getIConst());
2201 break;
2202 case EbtUInt:
2203 resultArray[i].setBConst(unionArrays[0][i].getUConst() <
2204 unionArrays[1][i].getUConst());
2205 break;
2206 default:
2207 UNREACHABLE();
2208 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302209 }
2210 }
Olli Etuahof119a262016-08-19 15:54:22 +03002211 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302212 }
Arun Patole9d0b1f92015-05-20 14:27:17 +05302213
Olli Etuahof119a262016-08-19 15:54:22 +03002214 case EOpLessThanEqual:
Arun Patole9d0b1f92015-05-20 14:27:17 +05302215 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002216 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole9d0b1f92015-05-20 14:27:17 +05302217 for (size_t i = 0; i < maxObjectSize; i++)
2218 {
2219 switch (basicType)
2220 {
Olli Etuahof119a262016-08-19 15:54:22 +03002221 case EbtFloat:
2222 resultArray[i].setBConst(unionArrays[0][i].getFConst() <=
2223 unionArrays[1][i].getFConst());
2224 break;
2225 case EbtInt:
2226 resultArray[i].setBConst(unionArrays[0][i].getIConst() <=
2227 unionArrays[1][i].getIConst());
2228 break;
2229 case EbtUInt:
2230 resultArray[i].setBConst(unionArrays[0][i].getUConst() <=
2231 unionArrays[1][i].getUConst());
2232 break;
2233 default:
2234 UNREACHABLE();
2235 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302236 }
2237 }
Olli Etuahof119a262016-08-19 15:54:22 +03002238 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302239 }
Arun Patole9d0b1f92015-05-20 14:27:17 +05302240
Olli Etuahof119a262016-08-19 15:54:22 +03002241 case EOpGreaterThan:
Arun Patole9d0b1f92015-05-20 14:27:17 +05302242 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002243 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole9d0b1f92015-05-20 14:27:17 +05302244 for (size_t i = 0; i < maxObjectSize; i++)
2245 {
2246 switch (basicType)
2247 {
Olli Etuahof119a262016-08-19 15:54:22 +03002248 case EbtFloat:
2249 resultArray[i].setBConst(unionArrays[0][i].getFConst() >
2250 unionArrays[1][i].getFConst());
2251 break;
2252 case EbtInt:
2253 resultArray[i].setBConst(unionArrays[0][i].getIConst() >
2254 unionArrays[1][i].getIConst());
2255 break;
2256 case EbtUInt:
2257 resultArray[i].setBConst(unionArrays[0][i].getUConst() >
2258 unionArrays[1][i].getUConst());
2259 break;
2260 default:
2261 UNREACHABLE();
2262 break;
Olli Etuahob43846e2015-06-02 18:18:57 +03002263 }
2264 }
Olli Etuahof119a262016-08-19 15:54:22 +03002265 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302266 }
Olli Etuahof119a262016-08-19 15:54:22 +03002267 case EOpGreaterThanEqual:
Arun Patole9d0b1f92015-05-20 14:27:17 +05302268 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002269 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole9d0b1f92015-05-20 14:27:17 +05302270 for (size_t i = 0; i < maxObjectSize; i++)
2271 {
2272 switch (basicType)
2273 {
Olli Etuahof119a262016-08-19 15:54:22 +03002274 case EbtFloat:
2275 resultArray[i].setBConst(unionArrays[0][i].getFConst() >=
2276 unionArrays[1][i].getFConst());
2277 break;
2278 case EbtInt:
2279 resultArray[i].setBConst(unionArrays[0][i].getIConst() >=
2280 unionArrays[1][i].getIConst());
2281 break;
2282 case EbtUInt:
2283 resultArray[i].setBConst(unionArrays[0][i].getUConst() >=
2284 unionArrays[1][i].getUConst());
2285 break;
2286 default:
2287 UNREACHABLE();
2288 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302289 }
2290 }
2291 }
2292 break;
2293
Olli Etuahof119a262016-08-19 15:54:22 +03002294 case EOpVectorEqual:
Arun Patole9d0b1f92015-05-20 14:27:17 +05302295 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002296 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole9d0b1f92015-05-20 14:27:17 +05302297 for (size_t i = 0; i < maxObjectSize; i++)
2298 {
2299 switch (basicType)
2300 {
Olli Etuahof119a262016-08-19 15:54:22 +03002301 case EbtFloat:
2302 resultArray[i].setBConst(unionArrays[0][i].getFConst() ==
2303 unionArrays[1][i].getFConst());
2304 break;
2305 case EbtInt:
2306 resultArray[i].setBConst(unionArrays[0][i].getIConst() ==
2307 unionArrays[1][i].getIConst());
2308 break;
2309 case EbtUInt:
2310 resultArray[i].setBConst(unionArrays[0][i].getUConst() ==
2311 unionArrays[1][i].getUConst());
2312 break;
2313 case EbtBool:
2314 resultArray[i].setBConst(unionArrays[0][i].getBConst() ==
2315 unionArrays[1][i].getBConst());
2316 break;
2317 default:
2318 UNREACHABLE();
2319 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302320 }
2321 }
Olli Etuahof119a262016-08-19 15:54:22 +03002322 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302323 }
Arun Patole9d0b1f92015-05-20 14:27:17 +05302324
Olli Etuahof119a262016-08-19 15:54:22 +03002325 case EOpVectorNotEqual:
Arun Patole9d0b1f92015-05-20 14:27:17 +05302326 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002327 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole9d0b1f92015-05-20 14:27:17 +05302328 for (size_t i = 0; i < maxObjectSize; i++)
2329 {
2330 switch (basicType)
2331 {
Olli Etuahof119a262016-08-19 15:54:22 +03002332 case EbtFloat:
2333 resultArray[i].setBConst(unionArrays[0][i].getFConst() !=
2334 unionArrays[1][i].getFConst());
2335 break;
2336 case EbtInt:
2337 resultArray[i].setBConst(unionArrays[0][i].getIConst() !=
2338 unionArrays[1][i].getIConst());
2339 break;
2340 case EbtUInt:
2341 resultArray[i].setBConst(unionArrays[0][i].getUConst() !=
2342 unionArrays[1][i].getUConst());
2343 break;
2344 case EbtBool:
2345 resultArray[i].setBConst(unionArrays[0][i].getBConst() !=
2346 unionArrays[1][i].getBConst());
2347 break;
2348 default:
2349 UNREACHABLE();
2350 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302351 }
2352 }
Olli Etuahof119a262016-08-19 15:54:22 +03002353 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302354 }
Arun Patole9d0b1f92015-05-20 14:27:17 +05302355
Olli Etuahof119a262016-08-19 15:54:22 +03002356 case EOpDistance:
Arun Patole1155ddd2015-06-05 18:04:36 +05302357 {
Olli Etuahof119a262016-08-19 15:54:22 +03002358 ASSERT(basicType == EbtFloat);
Arun Patole1155ddd2015-06-05 18:04:36 +05302359 TConstantUnion *distanceArray = new TConstantUnion[maxObjectSize];
Olli Etuahof119a262016-08-19 15:54:22 +03002360 resultArray = new TConstantUnion();
Arun Patole1155ddd2015-06-05 18:04:36 +05302361 for (size_t i = 0; i < maxObjectSize; i++)
2362 {
2363 float x = unionArrays[0][i].getFConst();
2364 float y = unionArrays[1][i].getFConst();
2365 distanceArray[i].setFConst(x - y);
2366 }
Olli Etuahob43846e2015-06-02 18:18:57 +03002367 resultArray->setFConst(VectorLength(distanceArray, maxObjectSize));
Olli Etuahof119a262016-08-19 15:54:22 +03002368 break;
Arun Patole1155ddd2015-06-05 18:04:36 +05302369 }
Arun Patole1155ddd2015-06-05 18:04:36 +05302370
Olli Etuahof119a262016-08-19 15:54:22 +03002371 case EOpDot:
2372 ASSERT(basicType == EbtFloat);
Olli Etuahob43846e2015-06-02 18:18:57 +03002373 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03002374 resultArray->setFConst(
2375 VectorDotProduct(unionArrays[0], unionArrays[1], maxObjectSize));
2376 break;
Arun Patole1155ddd2015-06-05 18:04:36 +05302377
Olli Etuahof119a262016-08-19 15:54:22 +03002378 case EOpCross:
Arun Patole1155ddd2015-06-05 18:04:36 +05302379 {
Olli Etuahof119a262016-08-19 15:54:22 +03002380 ASSERT(basicType == EbtFloat && maxObjectSize == 3);
Olli Etuahob43846e2015-06-02 18:18:57 +03002381 resultArray = new TConstantUnion[maxObjectSize];
Olli Etuahof119a262016-08-19 15:54:22 +03002382 float x0 = unionArrays[0][0].getFConst();
2383 float x1 = unionArrays[0][1].getFConst();
2384 float x2 = unionArrays[0][2].getFConst();
2385 float y0 = unionArrays[1][0].getFConst();
2386 float y1 = unionArrays[1][1].getFConst();
2387 float y2 = unionArrays[1][2].getFConst();
Olli Etuahob43846e2015-06-02 18:18:57 +03002388 resultArray[0].setFConst(x1 * y2 - y1 * x2);
2389 resultArray[1].setFConst(x2 * y0 - y2 * x0);
2390 resultArray[2].setFConst(x0 * y1 - y0 * x1);
Olli Etuahof119a262016-08-19 15:54:22 +03002391 break;
Arun Patole1155ddd2015-06-05 18:04:36 +05302392 }
Arun Patole1155ddd2015-06-05 18:04:36 +05302393
Olli Etuahof119a262016-08-19 15:54:22 +03002394 case EOpReflect:
Arun Patole1155ddd2015-06-05 18:04:36 +05302395 {
Olli Etuahof119a262016-08-19 15:54:22 +03002396 ASSERT(basicType == EbtFloat);
Arun Patole1155ddd2015-06-05 18:04:36 +05302397 // genType reflect (genType I, genType N) :
Olli Etuahof119a262016-08-19 15:54:22 +03002398 // For the incident vector I and surface orientation N, returns the reflection
2399 // direction:
Arun Patole1155ddd2015-06-05 18:04:36 +05302400 // I - 2 * dot(N, I) * N.
Olli Etuahof119a262016-08-19 15:54:22 +03002401 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole1155ddd2015-06-05 18:04:36 +05302402 float dotProduct = VectorDotProduct(unionArrays[1], unionArrays[0], maxObjectSize);
2403 for (size_t i = 0; i < maxObjectSize; i++)
2404 {
2405 float result = unionArrays[0][i].getFConst() -
2406 2.0f * dotProduct * unionArrays[1][i].getFConst();
Olli Etuahob43846e2015-06-02 18:18:57 +03002407 resultArray[i].setFConst(result);
Arun Patole1155ddd2015-06-05 18:04:36 +05302408 }
Olli Etuahof119a262016-08-19 15:54:22 +03002409 break;
Arun Patole1155ddd2015-06-05 18:04:36 +05302410 }
Arun Patole1155ddd2015-06-05 18:04:36 +05302411
Olli Etuahof119a262016-08-19 15:54:22 +03002412 case EOpMul:
Arun Patole7fa33552015-06-10 15:15:18 +05302413 {
Olli Etuahof119a262016-08-19 15:54:22 +03002414 ASSERT(basicType == EbtFloat && (*sequence)[0]->getAsTyped()->isMatrix() &&
2415 (*sequence)[1]->getAsTyped()->isMatrix());
Arun Patole7fa33552015-06-10 15:15:18 +05302416 // Perform component-wise matrix multiplication.
2417 resultArray = new TConstantUnion[maxObjectSize];
Olli Etuahof119a262016-08-19 15:54:22 +03002418 int size = (*sequence)[0]->getAsTyped()->getNominalSize();
Arun Patole7fa33552015-06-10 15:15:18 +05302419 angle::Matrix<float> result =
2420 GetMatrix(unionArrays[0], size).compMult(GetMatrix(unionArrays[1], size));
2421 SetUnionArrayFromMatrix(result, resultArray);
Olli Etuahof119a262016-08-19 15:54:22 +03002422 break;
Arun Patole7fa33552015-06-10 15:15:18 +05302423 }
Arun Patole7fa33552015-06-10 15:15:18 +05302424
Olli Etuahof119a262016-08-19 15:54:22 +03002425 case EOpOuterProduct:
Arun Patole7fa33552015-06-10 15:15:18 +05302426 {
Olli Etuahof119a262016-08-19 15:54:22 +03002427 ASSERT(basicType == EbtFloat);
Arun Patole7fa33552015-06-10 15:15:18 +05302428 size_t numRows = (*sequence)[0]->getAsTyped()->getType().getObjectSize();
2429 size_t numCols = (*sequence)[1]->getAsTyped()->getType().getObjectSize();
Olli Etuahof119a262016-08-19 15:54:22 +03002430 resultArray = new TConstantUnion[numRows * numCols];
Arun Patole7fa33552015-06-10 15:15:18 +05302431 angle::Matrix<float> result =
Olli Etuahod5da5052016-08-29 13:16:55 +03002432 GetMatrix(unionArrays[0], static_cast<int>(numRows), 1)
2433 .outerProduct(GetMatrix(unionArrays[1], 1, static_cast<int>(numCols)));
Arun Patole7fa33552015-06-10 15:15:18 +05302434 SetUnionArrayFromMatrix(result, resultArray);
Olli Etuahof119a262016-08-19 15:54:22 +03002435 break;
Arun Patole7fa33552015-06-10 15:15:18 +05302436 }
Arun Patole7fa33552015-06-10 15:15:18 +05302437
Olli Etuahof119a262016-08-19 15:54:22 +03002438 default:
2439 UNREACHABLE();
2440 // TODO: Add constant folding support for other built-in operations that take 2
2441 // parameters and not handled above.
2442 return nullptr;
Arun Patole274f0702015-05-05 13:33:30 +05302443 }
2444 }
2445 else if (paramsCount == 3)
2446 {
2447 //
2448 // Ternary built-in
2449 //
2450 switch (op)
2451 {
Olli Etuahof119a262016-08-19 15:54:22 +03002452 case EOpClamp:
Arun Patole274f0702015-05-05 13:33:30 +05302453 {
Olli Etuahob43846e2015-06-02 18:18:57 +03002454 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole274f0702015-05-05 13:33:30 +05302455 for (size_t i = 0; i < maxObjectSize; i++)
2456 {
2457 switch (basicType)
2458 {
Olli Etuahof119a262016-08-19 15:54:22 +03002459 case EbtFloat:
Arun Patole274f0702015-05-05 13:33:30 +05302460 {
Olli Etuahof119a262016-08-19 15:54:22 +03002461 float x = unionArrays[0][i].getFConst();
Arun Patole274f0702015-05-05 13:33:30 +05302462 float min = unionArrays[1][i].getFConst();
2463 float max = unionArrays[2][i].getFConst();
2464 // Results are undefined if min > max.
2465 if (min > max)
Olli Etuahof119a262016-08-19 15:54:22 +03002466 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
2467 &resultArray[i]);
Arun Patole274f0702015-05-05 13:33:30 +05302468 else
Olli Etuahob43846e2015-06-02 18:18:57 +03002469 resultArray[i].setFConst(gl::clamp(x, min, max));
Olli Etuahof119a262016-08-19 15:54:22 +03002470 break;
Arun Patole274f0702015-05-05 13:33:30 +05302471 }
Olli Etuahof119a262016-08-19 15:54:22 +03002472
2473 case EbtInt:
Arun Patole274f0702015-05-05 13:33:30 +05302474 {
Olli Etuahof119a262016-08-19 15:54:22 +03002475 int x = unionArrays[0][i].getIConst();
Arun Patole274f0702015-05-05 13:33:30 +05302476 int min = unionArrays[1][i].getIConst();
2477 int max = unionArrays[2][i].getIConst();
2478 // Results are undefined if min > max.
2479 if (min > max)
Olli Etuahof119a262016-08-19 15:54:22 +03002480 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
2481 &resultArray[i]);
Arun Patole274f0702015-05-05 13:33:30 +05302482 else
Olli Etuahob43846e2015-06-02 18:18:57 +03002483 resultArray[i].setIConst(gl::clamp(x, min, max));
Olli Etuahof119a262016-08-19 15:54:22 +03002484 break;
Arun Patole274f0702015-05-05 13:33:30 +05302485 }
Olli Etuahof119a262016-08-19 15:54:22 +03002486 case EbtUInt:
Arun Patole274f0702015-05-05 13:33:30 +05302487 {
Olli Etuahof119a262016-08-19 15:54:22 +03002488 unsigned int x = unionArrays[0][i].getUConst();
Arun Patole274f0702015-05-05 13:33:30 +05302489 unsigned int min = unionArrays[1][i].getUConst();
2490 unsigned int max = unionArrays[2][i].getUConst();
2491 // Results are undefined if min > max.
2492 if (min > max)
Olli Etuahof119a262016-08-19 15:54:22 +03002493 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
2494 &resultArray[i]);
Arun Patole274f0702015-05-05 13:33:30 +05302495 else
Olli Etuahob43846e2015-06-02 18:18:57 +03002496 resultArray[i].setUConst(gl::clamp(x, min, max));
Olli Etuahof119a262016-08-19 15:54:22 +03002497 break;
Arun Patole274f0702015-05-05 13:33:30 +05302498 }
Olli Etuahof119a262016-08-19 15:54:22 +03002499 default:
2500 UNREACHABLE();
2501 break;
Arun Patole274f0702015-05-05 13:33:30 +05302502 }
2503 }
Olli Etuahof119a262016-08-19 15:54:22 +03002504 break;
Arun Patole274f0702015-05-05 13:33:30 +05302505 }
Arun Patole274f0702015-05-05 13:33:30 +05302506
Olli Etuahof119a262016-08-19 15:54:22 +03002507 case EOpMix:
Arun Patolebf790422015-05-18 17:53:04 +05302508 {
Olli Etuahof119a262016-08-19 15:54:22 +03002509 ASSERT(basicType == EbtFloat);
2510 resultArray = new TConstantUnion[maxObjectSize];
2511 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302512 {
Olli Etuahof119a262016-08-19 15:54:22 +03002513 float x = unionArrays[0][i].getFConst();
2514 float y = unionArrays[1][i].getFConst();
2515 TBasicType type = (*sequence)[2]->getAsTyped()->getType().getBasicType();
2516 if (type == EbtFloat)
Arun Patolebf790422015-05-18 17:53:04 +05302517 {
Olli Etuahof119a262016-08-19 15:54:22 +03002518 // Returns the linear blend of x and y, i.e., x * (1 - a) + y * a.
2519 float a = unionArrays[2][i].getFConst();
2520 resultArray[i].setFConst(x * (1.0f - a) + y * a);
2521 }
2522 else // 3rd parameter is EbtBool
2523 {
2524 ASSERT(type == EbtBool);
2525 // Selects which vector each returned component comes from.
2526 // For a component of a that is false, the corresponding component of x is
2527 // returned.
2528 // For a component of a that is true, the corresponding component of y is
2529 // returned.
2530 bool a = unionArrays[2][i].getBConst();
2531 resultArray[i].setFConst(a ? y : x);
Arun Patolebf790422015-05-18 17:53:04 +05302532 }
2533 }
Olli Etuahof119a262016-08-19 15:54:22 +03002534 break;
Arun Patolebf790422015-05-18 17:53:04 +05302535 }
Arun Patolebf790422015-05-18 17:53:04 +05302536
Olli Etuahof119a262016-08-19 15:54:22 +03002537 case EOpSmoothStep:
Arun Patolebf790422015-05-18 17:53:04 +05302538 {
Olli Etuahof119a262016-08-19 15:54:22 +03002539 ASSERT(basicType == EbtFloat);
2540 resultArray = new TConstantUnion[maxObjectSize];
2541 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302542 {
Olli Etuahof119a262016-08-19 15:54:22 +03002543 float edge0 = unionArrays[0][i].getFConst();
2544 float edge1 = unionArrays[1][i].getFConst();
2545 float x = unionArrays[2][i].getFConst();
2546 // Results are undefined if edge0 >= edge1.
2547 if (edge0 >= edge1)
Arun Patolebf790422015-05-18 17:53:04 +05302548 {
Olli Etuahof119a262016-08-19 15:54:22 +03002549 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
2550 &resultArray[i]);
2551 }
2552 else
2553 {
2554 // Returns 0.0 if x <= edge0 and 1.0 if x >= edge1 and performs smooth
2555 // Hermite interpolation between 0 and 1 when edge0 < x < edge1.
2556 float t = gl::clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
2557 resultArray[i].setFConst(t * t * (3.0f - 2.0f * t));
Arun Patolebf790422015-05-18 17:53:04 +05302558 }
2559 }
Olli Etuahof119a262016-08-19 15:54:22 +03002560 break;
Arun Patolebf790422015-05-18 17:53:04 +05302561 }
Arun Patolebf790422015-05-18 17:53:04 +05302562
Olli Etuahof119a262016-08-19 15:54:22 +03002563 case EOpFaceForward:
Arun Patole1155ddd2015-06-05 18:04:36 +05302564 {
Olli Etuahof119a262016-08-19 15:54:22 +03002565 ASSERT(basicType == EbtFloat);
Arun Patole1155ddd2015-06-05 18:04:36 +05302566 // genType faceforward(genType N, genType I, genType Nref) :
2567 // If dot(Nref, I) < 0 return N, otherwise return -N.
Olli Etuahof119a262016-08-19 15:54:22 +03002568 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole1155ddd2015-06-05 18:04:36 +05302569 float dotProduct = VectorDotProduct(unionArrays[2], unionArrays[1], maxObjectSize);
2570 for (size_t i = 0; i < maxObjectSize; i++)
2571 {
2572 if (dotProduct < 0)
Olli Etuahob43846e2015-06-02 18:18:57 +03002573 resultArray[i].setFConst(unionArrays[0][i].getFConst());
Arun Patole1155ddd2015-06-05 18:04:36 +05302574 else
Olli Etuahob43846e2015-06-02 18:18:57 +03002575 resultArray[i].setFConst(-unionArrays[0][i].getFConst());
Arun Patole1155ddd2015-06-05 18:04:36 +05302576 }
Olli Etuahof119a262016-08-19 15:54:22 +03002577 break;
Arun Patole1155ddd2015-06-05 18:04:36 +05302578 }
Arun Patole1155ddd2015-06-05 18:04:36 +05302579
Olli Etuahof119a262016-08-19 15:54:22 +03002580 case EOpRefract:
Arun Patole1155ddd2015-06-05 18:04:36 +05302581 {
Olli Etuahof119a262016-08-19 15:54:22 +03002582 ASSERT(basicType == EbtFloat);
Arun Patole1155ddd2015-06-05 18:04:36 +05302583 // genType refract(genType I, genType N, float eta) :
Olli Etuahof119a262016-08-19 15:54:22 +03002584 // For the incident vector I and surface normal N, and the ratio of indices of
2585 // refraction eta,
Arun Patole1155ddd2015-06-05 18:04:36 +05302586 // return the refraction vector. The result is computed by
2587 // k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I))
2588 // if (k < 0.0)
2589 // return genType(0.0)
2590 // else
2591 // return eta * I - (eta * dot(N, I) + sqrt(k)) * N
Olli Etuahof119a262016-08-19 15:54:22 +03002592 resultArray = new TConstantUnion[maxObjectSize];
Arun Patole1155ddd2015-06-05 18:04:36 +05302593 float dotProduct = VectorDotProduct(unionArrays[1], unionArrays[0], maxObjectSize);
2594 for (size_t i = 0; i < maxObjectSize; i++)
2595 {
2596 float eta = unionArrays[2][i].getFConst();
Olli Etuahof119a262016-08-19 15:54:22 +03002597 float k = 1.0f - eta * eta * (1.0f - dotProduct * dotProduct);
Arun Patole1155ddd2015-06-05 18:04:36 +05302598 if (k < 0.0f)
Olli Etuahob43846e2015-06-02 18:18:57 +03002599 resultArray[i].setFConst(0.0f);
Arun Patole1155ddd2015-06-05 18:04:36 +05302600 else
Olli Etuahob43846e2015-06-02 18:18:57 +03002601 resultArray[i].setFConst(eta * unionArrays[0][i].getFConst() -
Olli Etuahof119a262016-08-19 15:54:22 +03002602 (eta * dotProduct + sqrtf(k)) *
2603 unionArrays[1][i].getFConst());
Arun Patole1155ddd2015-06-05 18:04:36 +05302604 }
Olli Etuahof119a262016-08-19 15:54:22 +03002605 break;
Arun Patole1155ddd2015-06-05 18:04:36 +05302606 }
Arun Patole1155ddd2015-06-05 18:04:36 +05302607
Olli Etuahof119a262016-08-19 15:54:22 +03002608 default:
2609 UNREACHABLE();
2610 // TODO: Add constant folding support for other built-in operations that take 3
2611 // parameters and not handled above.
2612 return nullptr;
Arun Patole274f0702015-05-05 13:33:30 +05302613 }
2614 }
Olli Etuahob43846e2015-06-02 18:18:57 +03002615 return resultArray;
Arun Patole274f0702015-05-05 13:33:30 +05302616}
2617
2618// static
Jamie Madillb1a85f42014-08-19 15:23:24 -04002619TString TIntermTraverser::hash(const TString &name, ShHashFunction64 hashFunction)
2620{
2621 if (hashFunction == NULL || name.empty())
2622 return name;
2623 khronos_uint64_t number = (*hashFunction)(name.c_str(), name.length());
2624 TStringStream stream;
2625 stream << HASHED_NAME_PREFIX << std::hex << number;
2626 TString hashedName = stream.str();
2627 return hashedName;
2628}
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002629
2630void TIntermTraverser::updateTree()
2631{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002632 for (size_t ii = 0; ii < mInsertions.size(); ++ii)
2633 {
2634 const NodeInsertMultipleEntry &insertion = mInsertions[ii];
2635 ASSERT(insertion.parent);
Olli Etuaho5d91dda2015-06-18 15:47:46 +03002636 if (!insertion.insertionsAfter.empty())
2637 {
2638 bool inserted = insertion.parent->insertChildNodes(insertion.position + 1,
2639 insertion.insertionsAfter);
2640 ASSERT(inserted);
2641 UNUSED_ASSERTION_VARIABLE(inserted);
2642 }
2643 if (!insertion.insertionsBefore.empty())
2644 {
2645 bool inserted =
2646 insertion.parent->insertChildNodes(insertion.position, insertion.insertionsBefore);
2647 ASSERT(inserted);
2648 UNUSED_ASSERTION_VARIABLE(inserted);
2649 }
Olli Etuahoa6f22092015-05-08 18:31:10 +03002650 }
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002651 for (size_t ii = 0; ii < mReplacements.size(); ++ii)
2652 {
Olli Etuahocd94ef92015-04-16 19:18:10 +03002653 const NodeUpdateEntry &replacement = mReplacements[ii];
2654 ASSERT(replacement.parent);
2655 bool replaced = replacement.parent->replaceChildNode(
2656 replacement.original, replacement.replacement);
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002657 ASSERT(replaced);
Olli Etuahod57e0db2015-04-24 15:05:08 +03002658 UNUSED_ASSERTION_VARIABLE(replaced);
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002659
Olli Etuahocd94ef92015-04-16 19:18:10 +03002660 if (!replacement.originalBecomesChildOfReplacement)
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002661 {
2662 // In AST traversing, a parent is visited before its children.
Olli Etuahocd94ef92015-04-16 19:18:10 +03002663 // After we replace a node, if its immediate child is to
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002664 // be replaced, we need to make sure we don't update the replaced
2665 // node; instead, we update the replacement node.
2666 for (size_t jj = ii + 1; jj < mReplacements.size(); ++jj)
2667 {
Olli Etuahocd94ef92015-04-16 19:18:10 +03002668 NodeUpdateEntry &replacement2 = mReplacements[jj];
2669 if (replacement2.parent == replacement.original)
2670 replacement2.parent = replacement.replacement;
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002671 }
2672 }
2673 }
Olli Etuahofc0e2bc2015-04-16 13:39:56 +03002674 for (size_t ii = 0; ii < mMultiReplacements.size(); ++ii)
2675 {
2676 const NodeReplaceWithMultipleEntry &replacement = mMultiReplacements[ii];
2677 ASSERT(replacement.parent);
2678 bool replaced = replacement.parent->replaceChildNodeWithMultiple(
2679 replacement.original, replacement.replacements);
2680 ASSERT(replaced);
Olli Etuahod57e0db2015-04-24 15:05:08 +03002681 UNUSED_ASSERTION_VARIABLE(replaced);
Olli Etuahofc0e2bc2015-04-16 13:39:56 +03002682 }
Olli Etuahod4f303e2015-05-20 17:09:06 +03002683
Jamie Madill03d863c2016-07-27 18:15:53 -04002684 clearReplacementQueue();
2685}
2686
2687void TIntermTraverser::clearReplacementQueue()
2688{
Olli Etuahod4f303e2015-05-20 17:09:06 +03002689 mReplacements.clear();
2690 mMultiReplacements.clear();
Jamie Madill03d863c2016-07-27 18:15:53 -04002691 mInsertions.clear();
Olli Etuaho853dc1a2014-11-06 17:25:48 +02002692}
Jamie Madill1048e432016-07-23 18:51:28 -04002693
Jamie Madill03d863c2016-07-27 18:15:53 -04002694void TIntermTraverser::queueReplacement(TIntermNode *original,
2695 TIntermNode *replacement,
2696 OriginalNode originalStatus)
Jamie Madill1048e432016-07-23 18:51:28 -04002697{
Jamie Madill03d863c2016-07-27 18:15:53 -04002698 queueReplacementWithParent(getParentNode(), original, replacement, originalStatus);
Jamie Madill1048e432016-07-23 18:51:28 -04002699}
2700
Jamie Madill03d863c2016-07-27 18:15:53 -04002701void TIntermTraverser::queueReplacementWithParent(TIntermNode *parent,
2702 TIntermNode *original,
2703 TIntermNode *replacement,
2704 OriginalNode originalStatus)
Jamie Madill1048e432016-07-23 18:51:28 -04002705{
Jamie Madill03d863c2016-07-27 18:15:53 -04002706 bool originalBecomesChild = (originalStatus == OriginalNode::BECOMES_CHILD);
2707 mReplacements.push_back(NodeUpdateEntry(parent, original, replacement, originalBecomesChild));
Jamie Madill1048e432016-07-23 18:51:28 -04002708}