blob: 28004524f4186df158f0b7be62a76f04161dc414 [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
Jamie Madill45bcc782016-11-07 13:58:48 -050026namespace sh
27{
28
Jamie Madillb1a85f42014-08-19 15:23:24 -040029namespace
30{
31
Jamie Madilld7b1ab52016-12-12 14:42:19 -050032const float kPi = 3.14159265358979323846f;
Arun Patole9dea48f2015-04-02 11:45:09 +053033const float kDegreesToRadiansMultiplier = kPi / 180.0f;
34const float kRadiansToDegreesMultiplier = 180.0f / kPi;
35
Jamie Madillb1a85f42014-08-19 15:23:24 -040036TPrecision GetHigherPrecision(TPrecision left, TPrecision right)
37{
38 return left > right ? left : right;
39}
40
Arun Patole274f0702015-05-05 13:33:30 +053041TConstantUnion *Vectorize(const TConstantUnion &constant, size_t size)
42{
43 TConstantUnion *constUnion = new TConstantUnion[size];
44 for (unsigned int i = 0; i < size; ++i)
Jamie Madilld7b1ab52016-12-12 14:42:19 -050045 constUnion[i] = constant;
Arun Patole274f0702015-05-05 13:33:30 +053046
47 return constUnion;
48}
49
Olli Etuahof119a262016-08-19 15:54:22 +030050void UndefinedConstantFoldingError(const TSourceLoc &loc,
51 TOperator op,
52 TBasicType basicType,
53 TDiagnostics *diagnostics,
54 TConstantUnion *result)
Arun Patolebf790422015-05-18 17:53:04 +053055{
Olli Etuahof119a262016-08-19 15:54:22 +030056 diagnostics->warning(loc, "operation result is undefined for the values passed in",
Olli Etuaho4de340a2016-12-16 09:32:03 +000057 GetOperatorString(op));
Arun Patolebf790422015-05-18 17:53:04 +053058
59 switch (basicType)
60 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -050061 case EbtFloat:
62 result->setFConst(0.0f);
63 break;
64 case EbtInt:
65 result->setIConst(0);
66 break;
67 case EbtUInt:
68 result->setUConst(0u);
69 break;
70 case EbtBool:
71 result->setBConst(false);
72 break;
73 default:
74 break;
Arun Patolebf790422015-05-18 17:53:04 +053075 }
76}
77
Olli Etuaho5c0e0232015-11-11 15:55:59 +020078float VectorLength(const TConstantUnion *paramArray, size_t paramArraySize)
Arun Patole1155ddd2015-06-05 18:04:36 +053079{
80 float result = 0.0f;
81 for (size_t i = 0; i < paramArraySize; i++)
82 {
83 float f = paramArray[i].getFConst();
84 result += f * f;
85 }
86 return sqrtf(result);
87}
88
Olli Etuaho5c0e0232015-11-11 15:55:59 +020089float VectorDotProduct(const TConstantUnion *paramArray1,
90 const TConstantUnion *paramArray2,
91 size_t paramArraySize)
Arun Patole1155ddd2015-06-05 18:04:36 +053092{
93 float result = 0.0f;
94 for (size_t i = 0; i < paramArraySize; i++)
95 result += paramArray1[i].getFConst() * paramArray2[i].getFConst();
96 return result;
97}
98
Olli Etuaho3272a6d2016-08-29 17:54:50 +030099TIntermTyped *CreateFoldedNode(const TConstantUnion *constArray,
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200100 const TIntermTyped *originalNode,
101 TQualifier qualifier)
Olli Etuahob43846e2015-06-02 18:18:57 +0300102{
103 if (constArray == nullptr)
104 {
105 return nullptr;
106 }
107 TIntermTyped *folded = new TIntermConstantUnion(constArray, originalNode->getType());
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200108 folded->getTypePointer()->setQualifier(qualifier);
Olli Etuahob43846e2015-06-02 18:18:57 +0300109 folded->setLine(originalNode->getLine());
110 return folded;
111}
112
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200113angle::Matrix<float> GetMatrix(const TConstantUnion *paramArray,
114 const unsigned int &rows,
115 const unsigned int &cols)
Arun Patole7fa33552015-06-10 15:15:18 +0530116{
117 std::vector<float> elements;
118 for (size_t i = 0; i < rows * cols; i++)
119 elements.push_back(paramArray[i].getFConst());
120 // Transpose is used since the Matrix constructor expects arguments in row-major order,
Olli Etuahod5da5052016-08-29 13:16:55 +0300121 // whereas the paramArray is in column-major order. Rows/cols parameters are also flipped below
122 // so that the created matrix will have the expected dimensions after the transpose.
123 return angle::Matrix<float>(elements, cols, rows).transpose();
Arun Patole7fa33552015-06-10 15:15:18 +0530124}
125
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200126angle::Matrix<float> GetMatrix(const TConstantUnion *paramArray, const unsigned int &size)
Arun Patole7fa33552015-06-10 15:15:18 +0530127{
128 std::vector<float> elements;
129 for (size_t i = 0; i < size * size; i++)
130 elements.push_back(paramArray[i].getFConst());
131 // Transpose is used since the Matrix constructor expects arguments in row-major order,
132 // whereas the paramArray is in column-major order.
133 return angle::Matrix<float>(elements, size).transpose();
134}
135
136void SetUnionArrayFromMatrix(const angle::Matrix<float> &m, TConstantUnion *resultArray)
137{
138 // Transpose is used since the input Matrix is in row-major order,
139 // whereas the actual result should be in column-major order.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500140 angle::Matrix<float> result = m.transpose();
Arun Patole7fa33552015-06-10 15:15:18 +0530141 std::vector<float> resultElements = result.elements();
142 for (size_t i = 0; i < resultElements.size(); i++)
143 resultArray[i].setFConst(resultElements[i]);
144}
145
Jamie Madillb1a85f42014-08-19 15:23:24 -0400146} // namespace anonymous
147
Jamie Madillb1a85f42014-08-19 15:23:24 -0400148////////////////////////////////////////////////////////////////
149//
150// Member functions of the nodes used for building the tree.
151//
152////////////////////////////////////////////////////////////////
153
Olli Etuahod2a67b92014-10-21 16:42:57 +0300154void TIntermTyped::setTypePreservePrecision(const TType &t)
155{
156 TPrecision precision = getPrecision();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500157 mType = t;
Olli Etuahod2a67b92014-10-21 16:42:57 +0300158 ASSERT(mType.getBasicType() != EbtBool || precision == EbpUndefined);
159 mType.setPrecision(precision);
160}
161
Jamie Madillb1a85f42014-08-19 15:23:24 -0400162#define REPLACE_IF_IS(node, type, original, replacement) \
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500163 if (node == original) \
164 { \
165 node = static_cast<type *>(replacement); \
166 return true; \
Jamie Madillb1a85f42014-08-19 15:23:24 -0400167 }
168
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500169bool TIntermLoop::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400170{
Olli Etuaho3cbb27a2016-07-14 11:55:48 +0300171 ASSERT(original != nullptr); // This risks replacing multiple children.
Jamie Madillb1a85f42014-08-19 15:23:24 -0400172 REPLACE_IF_IS(mInit, TIntermNode, original, replacement);
173 REPLACE_IF_IS(mCond, TIntermTyped, original, replacement);
174 REPLACE_IF_IS(mExpr, TIntermTyped, original, replacement);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100175 REPLACE_IF_IS(mBody, TIntermBlock, original, replacement);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400176 return false;
177}
178
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500179bool TIntermBranch::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400180{
181 REPLACE_IF_IS(mExpression, TIntermTyped, original, replacement);
182 return false;
183}
184
Olli Etuahob6fa0432016-09-28 16:28:05 +0100185bool TIntermSwizzle::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
186{
187 ASSERT(original->getAsTyped()->getType() == replacement->getAsTyped()->getType());
188 REPLACE_IF_IS(mOperand, TIntermTyped, original, replacement);
189 return false;
190}
191
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500192bool TIntermBinary::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400193{
194 REPLACE_IF_IS(mLeft, TIntermTyped, original, replacement);
195 REPLACE_IF_IS(mRight, TIntermTyped, original, replacement);
196 return false;
197}
198
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500199bool TIntermUnary::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400200{
Olli Etuahoa2234302016-08-31 12:05:39 +0300201 ASSERT(original->getAsTyped()->getType() == replacement->getAsTyped()->getType());
Jamie Madillb1a85f42014-08-19 15:23:24 -0400202 REPLACE_IF_IS(mOperand, TIntermTyped, original, replacement);
203 return false;
204}
205
Olli Etuahobf4e1b72016-12-09 11:30:15 +0000206bool TIntermInvariantDeclaration::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
207{
208 REPLACE_IF_IS(mSymbol, TIntermSymbol, original, replacement);
209 return false;
210}
211
Olli Etuaho336b1472016-10-05 16:37:55 +0100212bool TIntermFunctionDefinition::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
213{
Olli Etuaho8ad9e752017-01-16 19:55:20 +0000214 REPLACE_IF_IS(mPrototype, TIntermFunctionPrototype, original, replacement);
Olli Etuaho336b1472016-10-05 16:37:55 +0100215 REPLACE_IF_IS(mBody, TIntermBlock, original, replacement);
216 return false;
217}
218
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500219bool TIntermAggregate::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400220{
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100221 return replaceChildNodeInternal(original, replacement);
222}
223
224bool TIntermBlock::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
225{
226 return replaceChildNodeInternal(original, replacement);
227}
228
Olli Etuaho16c745a2017-01-16 17:02:27 +0000229bool TIntermFunctionPrototype::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
230{
231 return replaceChildNodeInternal(original, replacement);
232}
233
Olli Etuaho13389b62016-10-16 11:48:18 +0100234bool TIntermDeclaration::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
235{
236 return replaceChildNodeInternal(original, replacement);
237}
238
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100239bool TIntermAggregateBase::replaceChildNodeInternal(TIntermNode *original, TIntermNode *replacement)
240{
241 for (size_t ii = 0; ii < getSequence()->size(); ++ii)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400242 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100243 REPLACE_IF_IS((*getSequence())[ii], TIntermNode, original, replacement);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400244 }
245 return false;
246}
247
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100248bool TIntermAggregateBase::replaceChildNodeWithMultiple(TIntermNode *original,
249 const TIntermSequence &replacements)
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300250{
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100251 for (auto it = getSequence()->begin(); it < getSequence()->end(); ++it)
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300252 {
253 if (*it == original)
254 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100255 it = getSequence()->erase(it);
256 getSequence()->insert(it, replacements.begin(), replacements.end());
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300257 return true;
258 }
259 }
260 return false;
261}
262
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100263bool TIntermAggregateBase::insertChildNodes(TIntermSequence::size_type position,
264 const TIntermSequence &insertions)
Olli Etuahoa6f22092015-05-08 18:31:10 +0300265{
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100266 if (position > getSequence()->size())
Olli Etuahoa6f22092015-05-08 18:31:10 +0300267 {
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300268 return false;
Olli Etuahoa6f22092015-05-08 18:31:10 +0300269 }
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100270 auto it = getSequence()->begin() + position;
271 getSequence()->insert(it, insertions.begin(), insertions.end());
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300272 return true;
Olli Etuahoa6f22092015-05-08 18:31:10 +0300273}
274
Olli Etuahofe486322017-03-21 09:30:54 +0000275TIntermAggregate *TIntermAggregate::CreateFunctionCall(const TFunction &func,
276 TIntermSequence *arguments)
277{
278 TIntermAggregate *callNode =
279 new TIntermAggregate(func.getReturnType(), EOpCallFunctionInAST, arguments);
280 callNode->getFunctionSymbolInfo()->setFromFunction(func);
281 return callNode;
282}
283
284TIntermAggregate *TIntermAggregate::CreateFunctionCall(const TType &type,
285 const TSymbolUniqueId &id,
286 const TName &name,
287 TIntermSequence *arguments)
288{
289 TIntermAggregate *callNode = new TIntermAggregate(type, EOpCallFunctionInAST, arguments);
290 callNode->getFunctionSymbolInfo()->setId(id);
291 callNode->getFunctionSymbolInfo()->setNameObj(name);
292 return callNode;
293}
294
295TIntermAggregate *TIntermAggregate::CreateBuiltInFunctionCall(const TFunction &func,
296 TIntermSequence *arguments)
297{
298 TIntermAggregate *callNode =
299 new TIntermAggregate(func.getReturnType(), EOpCallBuiltInFunction, arguments);
300 callNode->getFunctionSymbolInfo()->setFromFunction(func);
301 // Note that name needs to be set before texture function type is determined.
302 callNode->setBuiltInFunctionPrecision();
303 return callNode;
304}
305
306TIntermAggregate *TIntermAggregate::CreateConstructor(const TType &type,
307 TOperator op,
308 TIntermSequence *arguments)
309{
310 TIntermAggregate *constructorNode = new TIntermAggregate(type, op, arguments);
311 ASSERT(constructorNode->isConstructor());
312 return constructorNode;
313}
314
315TIntermAggregate *TIntermAggregate::Create(const TType &type,
316 TOperator op,
317 TIntermSequence *arguments)
318{
319 TIntermAggregate *node = new TIntermAggregate(type, op, arguments);
320 ASSERT(op != EOpCallFunctionInAST); // Should use CreateFunctionCall
321 ASSERT(op != EOpCallBuiltInFunction); // Should use CreateBuiltInFunctionCall
322 ASSERT(!node->isConstructor()); // Should use CreateConstructor
323 return node;
324}
325
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800326TIntermAggregate::TIntermAggregate(const TType &type, TOperator op, TIntermSequence *arguments)
327 : TIntermOperator(op), mUseEmulatedFunction(false), mGotPrecisionFromChildren(false)
328{
329 if (arguments != nullptr)
330 {
331 mArguments.swap(*arguments);
332 }
333 setTypePrecisionAndQualifier(type);
334}
335
336void TIntermAggregate::setTypePrecisionAndQualifier(const TType &type)
337{
338 setType(type);
339 mType.setQualifier(EvqTemporary);
340 if (!isFunctionCall())
341 {
342 if (isConstructor())
343 {
344 // Structs should not be precision qualified, the individual members may be.
345 // Built-in types on the other hand should be precision qualified.
346 if (mOp != EOpConstructStruct)
347 {
348 setPrecisionFromChildren();
349 }
350 }
351 else
352 {
353 setPrecisionForBuiltInOp();
354 }
355 if (areChildrenConstQualified())
356 {
357 mType.setQualifier(EvqConst);
358 }
359 }
360}
361
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200362bool TIntermAggregate::areChildrenConstQualified()
363{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800364 for (TIntermNode *&arg : mArguments)
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200365 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800366 TIntermTyped *typedArg = arg->getAsTyped();
367 if (typedArg && typedArg->getQualifier() != EvqConst)
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200368 {
369 return false;
370 }
371 }
372 return true;
373}
374
Olli Etuahod2a67b92014-10-21 16:42:57 +0300375void TIntermAggregate::setPrecisionFromChildren()
376{
Olli Etuahoa4aa4e32015-06-04 15:54:30 +0300377 mGotPrecisionFromChildren = true;
Olli Etuahod2a67b92014-10-21 16:42:57 +0300378 if (getBasicType() == EbtBool)
379 {
380 mType.setPrecision(EbpUndefined);
381 return;
382 }
383
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500384 TPrecision precision = EbpUndefined;
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800385 TIntermSequence::iterator childIter = mArguments.begin();
386 while (childIter != mArguments.end())
Olli Etuahod2a67b92014-10-21 16:42:57 +0300387 {
388 TIntermTyped *typed = (*childIter)->getAsTyped();
389 if (typed)
390 precision = GetHigherPrecision(typed->getPrecision(), precision);
391 ++childIter;
392 }
393 mType.setPrecision(precision);
394}
395
Olli Etuaho9250cb22017-01-21 10:51:27 +0000396void TIntermAggregate::setPrecisionForBuiltInOp()
397{
398 ASSERT(!isConstructor());
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800399 ASSERT(!isFunctionCall());
Olli Etuaho9250cb22017-01-21 10:51:27 +0000400 if (!setPrecisionForSpecialBuiltInOp())
401 {
402 setPrecisionFromChildren();
403 }
404}
405
406bool TIntermAggregate::setPrecisionForSpecialBuiltInOp()
407{
408 switch (mOp)
409 {
410 case EOpBitfieldExtract:
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800411 mType.setPrecision(mArguments[0]->getAsTyped()->getPrecision());
412 mGotPrecisionFromChildren = true;
Olli Etuaho9250cb22017-01-21 10:51:27 +0000413 return true;
414 case EOpBitfieldInsert:
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800415 mType.setPrecision(GetHigherPrecision(mArguments[0]->getAsTyped()->getPrecision(),
416 mArguments[1]->getAsTyped()->getPrecision()));
417 mGotPrecisionFromChildren = true;
Olli Etuaho9250cb22017-01-21 10:51:27 +0000418 return true;
419 case EOpUaddCarry:
420 case EOpUsubBorrow:
421 mType.setPrecision(EbpHigh);
422 return true;
423 default:
424 return false;
425 }
426}
427
Olli Etuahod2a67b92014-10-21 16:42:57 +0300428void TIntermAggregate::setBuiltInFunctionPrecision()
429{
430 // All built-ins returning bool should be handled as ops, not functions.
431 ASSERT(getBasicType() != EbtBool);
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800432 ASSERT(mOp == EOpCallBuiltInFunction);
Olli Etuahod2a67b92014-10-21 16:42:57 +0300433
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800434 TPrecision precision = EbpUndefined;
435 for (TIntermNode *arg : mArguments)
Olli Etuahod2a67b92014-10-21 16:42:57 +0300436 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800437 TIntermTyped *typed = arg->getAsTyped();
Olli Etuahod2a67b92014-10-21 16:42:57 +0300438 // ESSL spec section 8: texture functions get their precision from the sampler.
439 if (typed && IsSampler(typed->getBasicType()))
440 {
441 precision = typed->getPrecision();
442 break;
443 }
Olli Etuahod2a67b92014-10-21 16:42:57 +0300444 }
445 // ESSL 3.0 spec section 8: textureSize always gets highp precision.
446 // All other functions that take a sampler are assumed to be texture functions.
Olli Etuahobd674552016-10-06 13:28:42 +0100447 if (mFunctionInfo.getName().find("textureSize") == 0)
Olli Etuahod2a67b92014-10-21 16:42:57 +0300448 mType.setPrecision(EbpHigh);
449 else
450 mType.setPrecision(precision);
451}
452
Olli Etuahof2209f72017-04-01 12:45:55 +0300453TString TIntermAggregate::getSymbolTableMangledName() const
454{
455 ASSERT(!isConstructor());
456 switch (mOp)
457 {
458 case EOpCallInternalRawFunction:
459 case EOpCallBuiltInFunction:
460 case EOpCallFunctionInAST:
461 return TFunction::GetMangledNameFromCall(mFunctionInfo.getName(), mArguments);
462 default:
463 TString opString = GetOperatorString(mOp);
464 return TFunction::GetMangledNameFromCall(opString, mArguments);
465 }
466}
467
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100468void TIntermBlock::appendStatement(TIntermNode *statement)
469{
Olli Etuaho13389b62016-10-16 11:48:18 +0100470 // Declaration nodes with no children can appear if all the declarators just added constants to
471 // the symbol table instead of generating code. They're no-ops so they aren't added to blocks.
472 if (statement != nullptr && (statement->getAsDeclarationNode() == nullptr ||
473 !statement->getAsDeclarationNode()->getSequence()->empty()))
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100474 {
475 mStatements.push_back(statement);
476 }
477}
478
Olli Etuaho16c745a2017-01-16 17:02:27 +0000479void TIntermFunctionPrototype::appendParameter(TIntermSymbol *parameter)
480{
481 ASSERT(parameter != nullptr);
482 mParameters.push_back(parameter);
483}
484
Olli Etuaho13389b62016-10-16 11:48:18 +0100485void TIntermDeclaration::appendDeclarator(TIntermTyped *declarator)
486{
487 ASSERT(declarator != nullptr);
488 ASSERT(declarator->getAsSymbolNode() != nullptr ||
489 (declarator->getAsBinaryNode() != nullptr &&
490 declarator->getAsBinaryNode()->getOp() == EOpInitialize));
491 ASSERT(mDeclarators.empty() ||
492 declarator->getType().sameElementType(mDeclarators.back()->getAsTyped()->getType()));
493 mDeclarators.push_back(declarator);
494}
495
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300496bool TIntermTernary::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
497{
498 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
499 REPLACE_IF_IS(mTrueExpression, TIntermTyped, original, replacement);
500 REPLACE_IF_IS(mFalseExpression, TIntermTyped, original, replacement);
501 return false;
502}
503
Olli Etuaho57961272016-09-14 13:57:46 +0300504bool TIntermIfElse::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400505{
506 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100507 REPLACE_IF_IS(mTrueBlock, TIntermBlock, original, replacement);
508 REPLACE_IF_IS(mFalseBlock, TIntermBlock, original, replacement);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400509 return false;
510}
511
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500512bool TIntermSwitch::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Olli Etuahoa3a36662015-02-17 13:46:51 +0200513{
514 REPLACE_IF_IS(mInit, TIntermTyped, original, replacement);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100515 REPLACE_IF_IS(mStatementList, TIntermBlock, original, replacement);
Olli Etuahoa3a36662015-02-17 13:46:51 +0200516 return false;
517}
518
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500519bool TIntermCase::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Olli Etuahoa3a36662015-02-17 13:46:51 +0200520{
521 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
522 return false;
523}
524
Olli Etuahod7a25242015-08-18 13:49:45 +0300525TIntermTyped::TIntermTyped(const TIntermTyped &node) : TIntermNode(), mType(node.mType)
526{
527 // Copy constructor is disallowed for TIntermNode in order to disallow it for subclasses that
528 // don't explicitly allow it, so normal TIntermNode constructor is used to construct the copy.
529 // We need to manually copy any fields of TIntermNode besides handling fields in TIntermTyped.
530 mLine = node.mLine;
531}
532
Olli Etuahod4f4c112016-04-15 15:11:24 +0300533bool TIntermTyped::isConstructorWithOnlyConstantUnionParameters()
534{
535 TIntermAggregate *constructor = getAsAggregate();
536 if (!constructor || !constructor->isConstructor())
537 {
538 return false;
539 }
540 for (TIntermNode *&node : *constructor->getSequence())
541 {
542 if (!node->getAsConstantUnion())
543 return false;
544 }
545 return true;
546}
547
Corentin Wallez509e4562016-08-25 14:55:44 -0400548// static
549TIntermTyped *TIntermTyped::CreateIndexNode(int index)
550{
551 TConstantUnion *u = new TConstantUnion[1];
552 u[0].setIConst(index);
553
554 TType type(EbtInt, EbpUndefined, EvqConst, 1);
555 TIntermConstantUnion *node = new TIntermConstantUnion(u, type);
556 return node;
557}
558
559// static
560TIntermTyped *TIntermTyped::CreateZero(const TType &type)
561{
562 TType constType(type);
563 constType.setQualifier(EvqConst);
564
565 if (!type.isArray() && type.getBasicType() != EbtStruct)
566 {
567 ASSERT(type.isScalar() || type.isVector() || type.isMatrix());
568
569 size_t size = constType.getObjectSize();
570 TConstantUnion *u = new TConstantUnion[size];
571 for (size_t i = 0; i < size; ++i)
572 {
573 switch (type.getBasicType())
574 {
575 case EbtFloat:
576 u[i].setFConst(0.0f);
577 break;
578 case EbtInt:
579 u[i].setIConst(0);
580 break;
581 case EbtUInt:
582 u[i].setUConst(0u);
583 break;
584 case EbtBool:
585 u[i].setBConst(false);
586 break;
587 default:
Corentin Wallez17a5c062017-01-22 15:20:53 -0500588 // CreateZero is called by ParseContext that keeps parsing even when an error
589 // occurs, so it is possible for CreateZero to be called with non-basic types.
590 // This happens only on error condition but CreateZero needs to return a value
591 // with the correct type to continue the typecheck. That's why we handle
592 // non-basic type by setting whatever value, we just need the type to be right.
593 u[i].setIConst(42);
594 break;
Corentin Wallez509e4562016-08-25 14:55:44 -0400595 }
596 }
597
598 TIntermConstantUnion *node = new TIntermConstantUnion(u, constType);
599 return node;
600 }
601
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800602 TIntermSequence *arguments = new TIntermSequence();
Corentin Wallez509e4562016-08-25 14:55:44 -0400603
604 if (type.isArray())
605 {
606 TType elementType(type);
607 elementType.clearArrayness();
608
609 size_t arraySize = type.getArraySize();
610 for (size_t i = 0; i < arraySize; ++i)
611 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800612 arguments->push_back(CreateZero(elementType));
Corentin Wallez509e4562016-08-25 14:55:44 -0400613 }
614 }
615 else
616 {
617 ASSERT(type.getBasicType() == EbtStruct);
618
619 TStructure *structure = type.getStruct();
620 for (const auto &field : structure->fields())
621 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800622 arguments->push_back(CreateZero(*field->type()));
Corentin Wallez509e4562016-08-25 14:55:44 -0400623 }
624 }
625
Olli Etuahofe486322017-03-21 09:30:54 +0000626 return TIntermAggregate::CreateConstructor(constType, sh::TypeToConstructorOperator(type),
627 arguments);
Corentin Wallez509e4562016-08-25 14:55:44 -0400628}
629
Corentin Wallez36fd1002016-12-08 11:30:44 -0500630// static
631TIntermTyped *TIntermTyped::CreateBool(bool value)
632{
633 TConstantUnion *u = new TConstantUnion[1];
634 u[0].setBConst(value);
635
636 TType type(EbtBool, EbpUndefined, EvqConst, 1);
637 TIntermConstantUnion *node = new TIntermConstantUnion(u, type);
638 return node;
639}
640
Olli Etuahod7a25242015-08-18 13:49:45 +0300641TIntermConstantUnion::TIntermConstantUnion(const TIntermConstantUnion &node) : TIntermTyped(node)
642{
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200643 mUnionArrayPointer = node.mUnionArrayPointer;
Olli Etuahod7a25242015-08-18 13:49:45 +0300644}
645
Olli Etuahobd674552016-10-06 13:28:42 +0100646void TFunctionSymbolInfo::setFromFunction(const TFunction &function)
647{
Olli Etuahoec9232b2017-03-27 17:01:37 +0300648 setName(function.getName());
Olli Etuahofe486322017-03-21 09:30:54 +0000649 setId(TSymbolUniqueId(function));
650}
651
652TFunctionSymbolInfo::TFunctionSymbolInfo(const TSymbolUniqueId &id) : mId(new TSymbolUniqueId(id))
653{
654}
655
656TFunctionSymbolInfo::TFunctionSymbolInfo(const TFunctionSymbolInfo &info)
657 : mName(info.mName), mId(nullptr)
658{
659 if (info.mId)
660 {
661 mId = new TSymbolUniqueId(*info.mId);
662 }
663}
664
665TFunctionSymbolInfo &TFunctionSymbolInfo::operator=(const TFunctionSymbolInfo &info)
666{
667 mName = info.mName;
668 if (info.mId)
669 {
670 mId = new TSymbolUniqueId(*info.mId);
671 }
672 else
673 {
674 mId = nullptr;
675 }
676 return *this;
677}
678
679void TFunctionSymbolInfo::setId(const TSymbolUniqueId &id)
680{
681 mId = new TSymbolUniqueId(id);
682}
683
684const TSymbolUniqueId &TFunctionSymbolInfo::getId() const
685{
686 ASSERT(mId);
687 return *mId;
Olli Etuahobd674552016-10-06 13:28:42 +0100688}
689
Olli Etuahod7a25242015-08-18 13:49:45 +0300690TIntermAggregate::TIntermAggregate(const TIntermAggregate &node)
691 : TIntermOperator(node),
Olli Etuahod7a25242015-08-18 13:49:45 +0300692 mUseEmulatedFunction(node.mUseEmulatedFunction),
Olli Etuahobd674552016-10-06 13:28:42 +0100693 mGotPrecisionFromChildren(node.mGotPrecisionFromChildren),
694 mFunctionInfo(node.mFunctionInfo)
Olli Etuahod7a25242015-08-18 13:49:45 +0300695{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800696 for (TIntermNode *arg : node.mArguments)
Olli Etuahod7a25242015-08-18 13:49:45 +0300697 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800698 TIntermTyped *typedArg = arg->getAsTyped();
699 ASSERT(typedArg != nullptr);
700 TIntermTyped *argCopy = typedArg->deepCopy();
701 mArguments.push_back(argCopy);
Olli Etuahod7a25242015-08-18 13:49:45 +0300702 }
703}
704
Olli Etuahofe486322017-03-21 09:30:54 +0000705TIntermAggregate *TIntermAggregate::shallowCopy() const
706{
707 TIntermSequence *copySeq = new TIntermSequence();
708 copySeq->insert(copySeq->begin(), getSequence()->begin(), getSequence()->end());
709 TIntermAggregate *copyNode = new TIntermAggregate(mType, mOp, copySeq);
710 *copyNode->getFunctionSymbolInfo() = mFunctionInfo;
711 copyNode->setLine(mLine);
712 return copyNode;
713}
714
Olli Etuahob6fa0432016-09-28 16:28:05 +0100715TIntermSwizzle::TIntermSwizzle(const TIntermSwizzle &node) : TIntermTyped(node)
716{
717 TIntermTyped *operandCopy = node.mOperand->deepCopy();
718 ASSERT(operandCopy != nullptr);
719 mOperand = operandCopy;
Olli Etuahoc9da71f2017-03-06 16:28:54 +0000720 mSwizzleOffsets = node.mSwizzleOffsets;
Olli Etuahob6fa0432016-09-28 16:28:05 +0100721}
722
Olli Etuahod7a25242015-08-18 13:49:45 +0300723TIntermBinary::TIntermBinary(const TIntermBinary &node)
724 : TIntermOperator(node), mAddIndexClamp(node.mAddIndexClamp)
725{
726 TIntermTyped *leftCopy = node.mLeft->deepCopy();
727 TIntermTyped *rightCopy = node.mRight->deepCopy();
728 ASSERT(leftCopy != nullptr && rightCopy != nullptr);
729 mLeft = leftCopy;
730 mRight = rightCopy;
731}
732
733TIntermUnary::TIntermUnary(const TIntermUnary &node)
734 : TIntermOperator(node), mUseEmulatedFunction(node.mUseEmulatedFunction)
735{
736 TIntermTyped *operandCopy = node.mOperand->deepCopy();
737 ASSERT(operandCopy != nullptr);
738 mOperand = operandCopy;
739}
740
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300741TIntermTernary::TIntermTernary(const TIntermTernary &node) : TIntermTyped(node)
Olli Etuahod7a25242015-08-18 13:49:45 +0300742{
Olli Etuahod7a25242015-08-18 13:49:45 +0300743 TIntermTyped *conditionCopy = node.mCondition->deepCopy();
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300744 TIntermTyped *trueCopy = node.mTrueExpression->deepCopy();
745 TIntermTyped *falseCopy = node.mFalseExpression->deepCopy();
Olli Etuahod7a25242015-08-18 13:49:45 +0300746 ASSERT(conditionCopy != nullptr && trueCopy != nullptr && falseCopy != nullptr);
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300747 mCondition = conditionCopy;
748 mTrueExpression = trueCopy;
749 mFalseExpression = falseCopy;
Olli Etuahod7a25242015-08-18 13:49:45 +0300750}
751
Jamie Madillb1a85f42014-08-19 15:23:24 -0400752bool TIntermOperator::isAssignment() const
753{
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300754 return IsAssignment(mOp);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400755}
756
Olli Etuaho8f76bcc2015-06-02 13:54:20 +0300757bool TIntermOperator::isMultiplication() const
758{
759 switch (mOp)
760 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500761 case EOpMul:
762 case EOpMatrixTimesMatrix:
763 case EOpMatrixTimesVector:
764 case EOpMatrixTimesScalar:
765 case EOpVectorTimesMatrix:
766 case EOpVectorTimesScalar:
767 return true;
768 default:
769 return false;
Olli Etuaho8f76bcc2015-06-02 13:54:20 +0300770 }
771}
772
Jamie Madillb1a85f42014-08-19 15:23:24 -0400773//
774// returns true if the operator is for one of the constructors
775//
776bool TIntermOperator::isConstructor() const
777{
778 switch (mOp)
779 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500780 case EOpConstructVec2:
781 case EOpConstructVec3:
782 case EOpConstructVec4:
783 case EOpConstructMat2:
784 case EOpConstructMat2x3:
785 case EOpConstructMat2x4:
786 case EOpConstructMat3x2:
787 case EOpConstructMat3:
788 case EOpConstructMat3x4:
789 case EOpConstructMat4x2:
790 case EOpConstructMat4x3:
791 case EOpConstructMat4:
792 case EOpConstructFloat:
793 case EOpConstructIVec2:
794 case EOpConstructIVec3:
795 case EOpConstructIVec4:
796 case EOpConstructInt:
797 case EOpConstructUVec2:
798 case EOpConstructUVec3:
799 case EOpConstructUVec4:
800 case EOpConstructUInt:
801 case EOpConstructBVec2:
802 case EOpConstructBVec3:
803 case EOpConstructBVec4:
804 case EOpConstructBool:
805 case EOpConstructStruct:
806 return true;
807 default:
808 return false;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400809 }
810}
811
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800812bool TIntermOperator::isFunctionCall() const
813{
814 switch (mOp)
815 {
816 case EOpCallFunctionInAST:
817 case EOpCallBuiltInFunction:
818 case EOpCallInternalRawFunction:
819 return true;
820 default:
821 return false;
822 }
823}
824
Olli Etuaho1dded802016-08-18 18:13:13 +0300825TOperator TIntermBinary::GetMulOpBasedOnOperands(const TType &left, const TType &right)
826{
827 if (left.isMatrix())
828 {
829 if (right.isMatrix())
830 {
831 return EOpMatrixTimesMatrix;
832 }
833 else
834 {
835 if (right.isVector())
836 {
837 return EOpMatrixTimesVector;
838 }
839 else
840 {
841 return EOpMatrixTimesScalar;
842 }
843 }
844 }
845 else
846 {
847 if (right.isMatrix())
848 {
849 if (left.isVector())
850 {
851 return EOpVectorTimesMatrix;
852 }
853 else
854 {
855 return EOpMatrixTimesScalar;
856 }
857 }
858 else
859 {
860 // Neither operand is a matrix.
861 if (left.isVector() == right.isVector())
862 {
863 // Leave as component product.
864 return EOpMul;
865 }
866 else
867 {
868 return EOpVectorTimesScalar;
869 }
870 }
871 }
872}
873
874TOperator TIntermBinary::GetMulAssignOpBasedOnOperands(const TType &left, const TType &right)
875{
876 if (left.isMatrix())
877 {
878 if (right.isMatrix())
879 {
880 return EOpMatrixTimesMatrixAssign;
881 }
882 else
883 {
884 // right should be scalar, but this may not be validated yet.
885 return EOpMatrixTimesScalarAssign;
886 }
887 }
888 else
889 {
890 if (right.isMatrix())
891 {
892 // Left should be a vector, but this may not be validated yet.
893 return EOpVectorTimesMatrixAssign;
894 }
895 else
896 {
897 // Neither operand is a matrix.
898 if (left.isVector() == right.isVector())
899 {
900 // Leave as component product.
901 return EOpMulAssign;
902 }
903 else
904 {
905 // left should be vector and right should be scalar, but this may not be validated
906 // yet.
907 return EOpVectorTimesScalarAssign;
908 }
909 }
910 }
911}
912
Jamie Madillb1a85f42014-08-19 15:23:24 -0400913//
914// Make sure the type of a unary operator is appropriate for its
915// combination of operation and operand type.
916//
Olli Etuahoa2234302016-08-31 12:05:39 +0300917void TIntermUnary::promote()
Jamie Madillb1a85f42014-08-19 15:23:24 -0400918{
Olli Etuahoa2234302016-08-31 12:05:39 +0300919 TQualifier resultQualifier = EvqTemporary;
920 if (mOperand->getQualifier() == EvqConst)
921 resultQualifier = EvqConst;
922
923 unsigned char operandPrimarySize =
924 static_cast<unsigned char>(mOperand->getType().getNominalSize());
Jamie Madillb1a85f42014-08-19 15:23:24 -0400925 switch (mOp)
926 {
Olli Etuahoa2234302016-08-31 12:05:39 +0300927 case EOpFloatBitsToInt:
928 setType(TType(EbtInt, EbpHigh, resultQualifier, operandPrimarySize));
929 break;
930 case EOpFloatBitsToUint:
931 setType(TType(EbtUInt, EbpHigh, resultQualifier, operandPrimarySize));
932 break;
933 case EOpIntBitsToFloat:
934 case EOpUintBitsToFloat:
935 setType(TType(EbtFloat, EbpHigh, resultQualifier, operandPrimarySize));
936 break;
937 case EOpPackSnorm2x16:
938 case EOpPackUnorm2x16:
939 case EOpPackHalf2x16:
Olli Etuaho25aef452017-01-29 16:15:44 -0800940 case EOpPackUnorm4x8:
941 case EOpPackSnorm4x8:
Olli Etuahoa2234302016-08-31 12:05:39 +0300942 setType(TType(EbtUInt, EbpHigh, resultQualifier));
943 break;
944 case EOpUnpackSnorm2x16:
945 case EOpUnpackUnorm2x16:
946 setType(TType(EbtFloat, EbpHigh, resultQualifier, 2));
947 break;
948 case EOpUnpackHalf2x16:
949 setType(TType(EbtFloat, EbpMedium, resultQualifier, 2));
950 break;
Olli Etuaho25aef452017-01-29 16:15:44 -0800951 case EOpUnpackUnorm4x8:
952 case EOpUnpackSnorm4x8:
953 setType(TType(EbtFloat, EbpMedium, resultQualifier, 4));
954 break;
Olli Etuahoa2234302016-08-31 12:05:39 +0300955 case EOpAny:
956 case EOpAll:
957 setType(TType(EbtBool, EbpUndefined, resultQualifier));
958 break;
959 case EOpLength:
960 case EOpDeterminant:
961 setType(TType(EbtFloat, mOperand->getType().getPrecision(), resultQualifier));
962 break;
963 case EOpTranspose:
964 setType(TType(EbtFloat, mOperand->getType().getPrecision(), resultQualifier,
965 static_cast<unsigned char>(mOperand->getType().getRows()),
966 static_cast<unsigned char>(mOperand->getType().getCols())));
967 break;
968 case EOpIsInf:
969 case EOpIsNan:
970 setType(TType(EbtBool, EbpUndefined, resultQualifier, operandPrimarySize));
971 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +0000972 case EOpBitfieldReverse:
973 setType(TType(mOperand->getBasicType(), EbpHigh, resultQualifier, operandPrimarySize));
974 break;
975 case EOpBitCount:
976 setType(TType(EbtInt, EbpLow, resultQualifier, operandPrimarySize));
977 break;
978 case EOpFindLSB:
979 setType(TType(EbtInt, EbpLow, resultQualifier, operandPrimarySize));
980 break;
981 case EOpFindMSB:
982 setType(TType(EbtInt, EbpLow, resultQualifier, operandPrimarySize));
983 break;
Olli Etuahoa2234302016-08-31 12:05:39 +0300984 default:
985 setType(mOperand->getType());
986 mType.setQualifier(resultQualifier);
987 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400988 }
Olli Etuahoa2234302016-08-31 12:05:39 +0300989}
Jamie Madillb1a85f42014-08-19 15:23:24 -0400990
Olli Etuahob6fa0432016-09-28 16:28:05 +0100991TIntermSwizzle::TIntermSwizzle(TIntermTyped *operand, const TVector<int> &swizzleOffsets)
992 : TIntermTyped(TType(EbtFloat, EbpUndefined)),
993 mOperand(operand),
994 mSwizzleOffsets(swizzleOffsets)
995{
996 ASSERT(mSwizzleOffsets.size() <= 4);
997 promote();
998}
999
Olli Etuahoa2234302016-08-31 12:05:39 +03001000TIntermUnary::TIntermUnary(TOperator op, TIntermTyped *operand)
1001 : TIntermOperator(op), mOperand(operand), mUseEmulatedFunction(false)
1002{
1003 promote();
Jamie Madillb1a85f42014-08-19 15:23:24 -04001004}
1005
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001006TIntermBinary::TIntermBinary(TOperator op, TIntermTyped *left, TIntermTyped *right)
1007 : TIntermOperator(op), mLeft(left), mRight(right), mAddIndexClamp(false)
1008{
1009 promote();
1010}
1011
Olli Etuahobf4e1b72016-12-09 11:30:15 +00001012TIntermInvariantDeclaration::TIntermInvariantDeclaration(TIntermSymbol *symbol, const TSourceLoc &line)
1013 : TIntermNode(), mSymbol(symbol)
1014{
1015 ASSERT(symbol);
1016 setLine(line);
1017}
1018
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001019TIntermTernary::TIntermTernary(TIntermTyped *cond,
1020 TIntermTyped *trueExpression,
1021 TIntermTyped *falseExpression)
1022 : TIntermTyped(trueExpression->getType()),
1023 mCondition(cond),
1024 mTrueExpression(trueExpression),
1025 mFalseExpression(falseExpression)
1026{
1027 getTypePointer()->setQualifier(
1028 TIntermTernary::DetermineQualifier(cond, trueExpression, falseExpression));
1029}
1030
Olli Etuaho81629262017-04-19 11:56:01 +03001031TIntermLoop::TIntermLoop(TLoopType type,
1032 TIntermNode *init,
1033 TIntermTyped *cond,
1034 TIntermTyped *expr,
1035 TIntermBlock *body)
1036 : mType(type), mInit(init), mCond(cond), mExpr(expr), mBody(body)
1037{
1038 // Declaration nodes with no children can appear if all the declarators just added constants to
1039 // the symbol table instead of generating code. They're no-ops so don't add them to the tree.
1040 if (mInit && mInit->getAsDeclarationNode() &&
1041 mInit->getAsDeclarationNode()->getSequence()->empty())
1042 {
1043 mInit = nullptr;
1044 }
1045}
1046
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001047// static
1048TQualifier TIntermTernary::DetermineQualifier(TIntermTyped *cond,
1049 TIntermTyped *trueExpression,
1050 TIntermTyped *falseExpression)
1051{
1052 if (cond->getQualifier() == EvqConst && trueExpression->getQualifier() == EvqConst &&
1053 falseExpression->getQualifier() == EvqConst)
1054 {
1055 return EvqConst;
1056 }
1057 return EvqTemporary;
1058}
1059
Olli Etuahob6fa0432016-09-28 16:28:05 +01001060void TIntermSwizzle::promote()
1061{
1062 TQualifier resultQualifier = EvqTemporary;
1063 if (mOperand->getQualifier() == EvqConst)
1064 resultQualifier = EvqConst;
1065
1066 auto numFields = mSwizzleOffsets.size();
1067 setType(TType(mOperand->getBasicType(), mOperand->getPrecision(), resultQualifier,
1068 static_cast<unsigned char>(numFields)));
1069}
1070
1071bool TIntermSwizzle::hasDuplicateOffsets() const
1072{
1073 int offsetCount[4] = {0u, 0u, 0u, 0u};
1074 for (const auto offset : mSwizzleOffsets)
1075 {
1076 offsetCount[offset]++;
1077 if (offsetCount[offset] > 1)
1078 {
1079 return true;
1080 }
1081 }
1082 return false;
1083}
1084
Olli Etuaho09b04a22016-12-15 13:30:26 +00001085bool TIntermSwizzle::offsetsMatch(int offset) const
1086{
1087 return mSwizzleOffsets.size() == 1 && mSwizzleOffsets[0] == offset;
1088}
1089
Olli Etuahob6fa0432016-09-28 16:28:05 +01001090void TIntermSwizzle::writeOffsetsAsXYZW(TInfoSinkBase *out) const
1091{
1092 for (const int offset : mSwizzleOffsets)
1093 {
1094 switch (offset)
1095 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001096 case 0:
1097 *out << "x";
1098 break;
1099 case 1:
1100 *out << "y";
1101 break;
1102 case 2:
1103 *out << "z";
1104 break;
1105 case 3:
1106 *out << "w";
1107 break;
1108 default:
1109 UNREACHABLE();
Olli Etuahob6fa0432016-09-28 16:28:05 +01001110 }
1111 }
1112}
1113
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001114TQualifier TIntermBinary::GetCommaQualifier(int shaderVersion,
1115 const TIntermTyped *left,
1116 const TIntermTyped *right)
1117{
1118 // ESSL3.00 section 12.43: The result of a sequence operator is not a constant-expression.
1119 if (shaderVersion >= 300 || left->getQualifier() != EvqConst ||
1120 right->getQualifier() != EvqConst)
1121 {
1122 return EvqTemporary;
1123 }
1124 return EvqConst;
1125}
Olli Etuahob6fa0432016-09-28 16:28:05 +01001126
1127// Establishes the type of the result of the binary operation.
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001128void TIntermBinary::promote()
Jamie Madillb1a85f42014-08-19 15:23:24 -04001129{
Olli Etuaho1dded802016-08-18 18:13:13 +03001130 ASSERT(!isMultiplication() ||
1131 mOp == GetMulOpBasedOnOperands(mLeft->getType(), mRight->getType()));
1132
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001133 // Comma is handled as a special case.
1134 if (mOp == EOpComma)
1135 {
1136 setType(mRight->getType());
1137 return;
1138 }
1139
Jamie Madillb1a85f42014-08-19 15:23:24 -04001140 // Base assumption: just make the type the same as the left
1141 // operand. Then only deviations from this need be coded.
Jamie Madillb1a85f42014-08-19 15:23:24 -04001142 setType(mLeft->getType());
1143
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001144 TQualifier resultQualifier = EvqConst;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001145 // Binary operations results in temporary variables unless both
1146 // operands are const.
1147 if (mLeft->getQualifier() != EvqConst || mRight->getQualifier() != EvqConst)
1148 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001149 resultQualifier = EvqTemporary;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001150 getTypePointer()->setQualifier(EvqTemporary);
1151 }
1152
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001153 // Handle indexing ops.
1154 switch (mOp)
1155 {
1156 case EOpIndexDirect:
1157 case EOpIndexIndirect:
1158 if (mLeft->isArray())
1159 {
1160 mType.clearArrayness();
1161 }
1162 else if (mLeft->isMatrix())
1163 {
1164 setType(TType(mLeft->getBasicType(), mLeft->getPrecision(), resultQualifier,
1165 static_cast<unsigned char>(mLeft->getRows())));
1166 }
1167 else if (mLeft->isVector())
1168 {
1169 setType(TType(mLeft->getBasicType(), mLeft->getPrecision(), resultQualifier));
1170 }
1171 else
1172 {
1173 UNREACHABLE();
1174 }
1175 return;
1176 case EOpIndexDirectStruct:
1177 {
1178 const TFieldList &fields = mLeft->getType().getStruct()->fields();
1179 const int i = mRight->getAsConstantUnion()->getIConst(0);
1180 setType(*fields[i]->type());
1181 getTypePointer()->setQualifier(resultQualifier);
1182 return;
1183 }
1184 case EOpIndexDirectInterfaceBlock:
1185 {
1186 const TFieldList &fields = mLeft->getType().getInterfaceBlock()->fields();
1187 const int i = mRight->getAsConstantUnion()->getIConst(0);
1188 setType(*fields[i]->type());
1189 getTypePointer()->setQualifier(resultQualifier);
1190 return;
1191 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001192 default:
1193 break;
1194 }
1195
1196 ASSERT(mLeft->isArray() == mRight->isArray());
1197
1198 // The result gets promoted to the highest precision.
1199 TPrecision higherPrecision = GetHigherPrecision(mLeft->getPrecision(), mRight->getPrecision());
1200 getTypePointer()->setPrecision(higherPrecision);
1201
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001202 const int nominalSize = std::max(mLeft->getNominalSize(), mRight->getNominalSize());
Jamie Madillb1a85f42014-08-19 15:23:24 -04001203
1204 //
1205 // All scalars or structs. Code after this test assumes this case is removed!
1206 //
1207 if (nominalSize == 1)
1208 {
1209 switch (mOp)
1210 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001211 //
1212 // Promote to conditional
1213 //
1214 case EOpEqual:
1215 case EOpNotEqual:
1216 case EOpLessThan:
1217 case EOpGreaterThan:
1218 case EOpLessThanEqual:
1219 case EOpGreaterThanEqual:
1220 setType(TType(EbtBool, EbpUndefined, resultQualifier));
1221 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001222
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001223 //
1224 // And and Or operate on conditionals
1225 //
1226 case EOpLogicalAnd:
1227 case EOpLogicalXor:
1228 case EOpLogicalOr:
1229 ASSERT(mLeft->getBasicType() == EbtBool && mRight->getBasicType() == EbtBool);
1230 setType(TType(EbtBool, EbpUndefined, resultQualifier));
1231 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001232
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001233 default:
1234 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001235 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001236 return;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001237 }
1238
1239 // If we reach here, at least one of the operands is vector or matrix.
1240 // The other operand could be a scalar, vector, or matrix.
Jamie Madillb1a85f42014-08-19 15:23:24 -04001241 TBasicType basicType = mLeft->getBasicType();
Olli Etuaho1dded802016-08-18 18:13:13 +03001242
Jamie Madillb1a85f42014-08-19 15:23:24 -04001243 switch (mOp)
1244 {
Olli Etuaho1dded802016-08-18 18:13:13 +03001245 case EOpMul:
1246 break;
1247 case EOpMatrixTimesScalar:
1248 if (mRight->isMatrix())
Jamie Madillb1a85f42014-08-19 15:23:24 -04001249 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001250 setType(TType(basicType, higherPrecision, resultQualifier,
1251 static_cast<unsigned char>(mRight->getCols()),
1252 static_cast<unsigned char>(mRight->getRows())));
Jamie Madillb1a85f42014-08-19 15:23:24 -04001253 }
Olli Etuaho1dded802016-08-18 18:13:13 +03001254 break;
1255 case EOpMatrixTimesVector:
1256 setType(TType(basicType, higherPrecision, resultQualifier,
1257 static_cast<unsigned char>(mLeft->getRows()), 1));
1258 break;
1259 case EOpMatrixTimesMatrix:
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001260 setType(TType(basicType, higherPrecision, resultQualifier,
1261 static_cast<unsigned char>(mRight->getCols()),
1262 static_cast<unsigned char>(mLeft->getRows())));
Olli Etuaho1dded802016-08-18 18:13:13 +03001263 break;
1264 case EOpVectorTimesScalar:
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001265 setType(TType(basicType, higherPrecision, resultQualifier,
Olli Etuaho1dded802016-08-18 18:13:13 +03001266 static_cast<unsigned char>(nominalSize), 1));
1267 break;
1268 case EOpVectorTimesMatrix:
1269 setType(TType(basicType, higherPrecision, resultQualifier,
1270 static_cast<unsigned char>(mRight->getCols()), 1));
1271 break;
1272 case EOpMulAssign:
1273 case EOpVectorTimesScalarAssign:
1274 case EOpVectorTimesMatrixAssign:
1275 case EOpMatrixTimesScalarAssign:
1276 case EOpMatrixTimesMatrixAssign:
1277 ASSERT(mOp == GetMulAssignOpBasedOnOperands(mLeft->getType(), mRight->getType()));
1278 break;
1279 case EOpAssign:
1280 case EOpInitialize:
Olli Etuaho1dded802016-08-18 18:13:13 +03001281 ASSERT((mLeft->getNominalSize() == mRight->getNominalSize()) &&
1282 (mLeft->getSecondarySize() == mRight->getSecondarySize()));
1283 break;
1284 case EOpAdd:
1285 case EOpSub:
1286 case EOpDiv:
1287 case EOpIMod:
1288 case EOpBitShiftLeft:
1289 case EOpBitShiftRight:
1290 case EOpBitwiseAnd:
1291 case EOpBitwiseXor:
1292 case EOpBitwiseOr:
1293 case EOpAddAssign:
1294 case EOpSubAssign:
1295 case EOpDivAssign:
1296 case EOpIModAssign:
1297 case EOpBitShiftLeftAssign:
1298 case EOpBitShiftRightAssign:
1299 case EOpBitwiseAndAssign:
1300 case EOpBitwiseXorAssign:
1301 case EOpBitwiseOrAssign:
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001302 {
1303 const int secondarySize =
1304 std::max(mLeft->getSecondarySize(), mRight->getSecondarySize());
1305 setType(TType(basicType, higherPrecision, resultQualifier,
1306 static_cast<unsigned char>(nominalSize),
1307 static_cast<unsigned char>(secondarySize)));
1308 ASSERT(!mLeft->isArray() && !mRight->isArray());
Olli Etuaho1dded802016-08-18 18:13:13 +03001309 break;
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001310 }
Olli Etuaho1dded802016-08-18 18:13:13 +03001311 case EOpEqual:
1312 case EOpNotEqual:
1313 case EOpLessThan:
1314 case EOpGreaterThan:
1315 case EOpLessThanEqual:
1316 case EOpGreaterThanEqual:
1317 ASSERT((mLeft->getNominalSize() == mRight->getNominalSize()) &&
1318 (mLeft->getSecondarySize() == mRight->getSecondarySize()));
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001319 setType(TType(EbtBool, EbpUndefined, resultQualifier));
Olli Etuaho1dded802016-08-18 18:13:13 +03001320 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001321
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001322 case EOpIndexDirect:
1323 case EOpIndexIndirect:
1324 case EOpIndexDirectInterfaceBlock:
1325 case EOpIndexDirectStruct:
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001326 // These ops should be already fully handled.
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001327 UNREACHABLE();
1328 break;
Olli Etuaho1dded802016-08-18 18:13:13 +03001329 default:
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001330 UNREACHABLE();
1331 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001332 }
Jamie Madillb1a85f42014-08-19 15:23:24 -04001333}
1334
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001335const TConstantUnion *TIntermConstantUnion::foldIndexing(int index)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001336{
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001337 if (isArray())
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001338 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001339 ASSERT(index < static_cast<int>(getType().getArraySize()));
1340 TType arrayElementType = getType();
1341 arrayElementType.clearArrayness();
1342 size_t arrayElementSize = arrayElementType.getObjectSize();
1343 return &mUnionArrayPointer[arrayElementSize * index];
1344 }
1345 else if (isMatrix())
1346 {
1347 ASSERT(index < getType().getCols());
1348 int size = getType().getRows();
1349 return &mUnionArrayPointer[size * index];
1350 }
1351 else if (isVector())
1352 {
1353 ASSERT(index < getType().getNominalSize());
1354 return &mUnionArrayPointer[index];
1355 }
1356 else
1357 {
1358 UNREACHABLE();
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001359 return nullptr;
1360 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001361}
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001362
Olli Etuahob6fa0432016-09-28 16:28:05 +01001363TIntermTyped *TIntermSwizzle::fold()
1364{
1365 TIntermConstantUnion *operandConstant = mOperand->getAsConstantUnion();
1366 if (operandConstant == nullptr)
1367 {
1368 return nullptr;
1369 }
1370
1371 TConstantUnion *constArray = new TConstantUnion[mSwizzleOffsets.size()];
1372 for (size_t i = 0; i < mSwizzleOffsets.size(); ++i)
1373 {
1374 constArray[i] = *operandConstant->foldIndexing(mSwizzleOffsets.at(i));
1375 }
1376 return CreateFoldedNode(constArray, this, mType.getQualifier());
1377}
1378
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001379TIntermTyped *TIntermBinary::fold(TDiagnostics *diagnostics)
1380{
1381 TIntermConstantUnion *leftConstant = mLeft->getAsConstantUnion();
1382 TIntermConstantUnion *rightConstant = mRight->getAsConstantUnion();
1383 switch (mOp)
1384 {
1385 case EOpIndexDirect:
1386 {
1387 if (leftConstant == nullptr || rightConstant == nullptr)
1388 {
1389 return nullptr;
1390 }
1391 int index = rightConstant->getIConst(0);
1392
1393 const TConstantUnion *constArray = leftConstant->foldIndexing(index);
1394 return CreateFoldedNode(constArray, this, mType.getQualifier());
1395 }
1396 case EOpIndexDirectStruct:
1397 {
1398 if (leftConstant == nullptr || rightConstant == nullptr)
1399 {
1400 return nullptr;
1401 }
1402 const TFieldList &fields = mLeft->getType().getStruct()->fields();
1403 size_t index = static_cast<size_t>(rightConstant->getIConst(0));
1404
1405 size_t previousFieldsSize = 0;
1406 for (size_t i = 0; i < index; ++i)
1407 {
1408 previousFieldsSize += fields[i]->type()->getObjectSize();
1409 }
1410
1411 const TConstantUnion *constArray = leftConstant->getUnionArrayPointer();
1412 return CreateFoldedNode(constArray + previousFieldsSize, this, mType.getQualifier());
1413 }
1414 case EOpIndexIndirect:
1415 case EOpIndexDirectInterfaceBlock:
1416 // Can never be constant folded.
1417 return nullptr;
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001418 default:
1419 {
1420 if (leftConstant == nullptr || rightConstant == nullptr)
1421 {
1422 return nullptr;
1423 }
Jamie Madill5db69f52016-09-15 12:47:32 -04001424 TConstantUnion *constArray =
1425 leftConstant->foldBinary(mOp, rightConstant, diagnostics, mLeft->getLine());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001426
1427 // Nodes may be constant folded without being qualified as constant.
1428 return CreateFoldedNode(constArray, this, mType.getQualifier());
1429 }
1430 }
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001431}
1432
Olli Etuahof119a262016-08-19 15:54:22 +03001433TIntermTyped *TIntermUnary::fold(TDiagnostics *diagnostics)
Olli Etuaho95310b02015-06-02 17:43:38 +03001434{
1435 TIntermConstantUnion *operandConstant = mOperand->getAsConstantUnion();
1436 if (operandConstant == nullptr)
1437 {
1438 return nullptr;
1439 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301440
1441 TConstantUnion *constArray = nullptr;
1442 switch (mOp)
1443 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001444 case EOpAny:
1445 case EOpAll:
1446 case EOpLength:
1447 case EOpTranspose:
1448 case EOpDeterminant:
1449 case EOpInverse:
1450 case EOpPackSnorm2x16:
1451 case EOpUnpackSnorm2x16:
1452 case EOpPackUnorm2x16:
1453 case EOpUnpackUnorm2x16:
1454 case EOpPackHalf2x16:
1455 case EOpUnpackHalf2x16:
Olli Etuaho25aef452017-01-29 16:15:44 -08001456 case EOpPackUnorm4x8:
1457 case EOpPackSnorm4x8:
1458 case EOpUnpackUnorm4x8:
1459 case EOpUnpackSnorm4x8:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001460 constArray = operandConstant->foldUnaryNonComponentWise(mOp);
1461 break;
1462 default:
1463 constArray = operandConstant->foldUnaryComponentWise(mOp, diagnostics);
1464 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301465 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001466
1467 // Nodes may be constant folded without being qualified as constant.
Olli Etuahoc9550582016-08-29 17:56:22 +03001468 return CreateFoldedNode(constArray, this, mType.getQualifier());
Olli Etuahob43846e2015-06-02 18:18:57 +03001469}
1470
Olli Etuahof119a262016-08-19 15:54:22 +03001471TIntermTyped *TIntermAggregate::fold(TDiagnostics *diagnostics)
Olli Etuahob43846e2015-06-02 18:18:57 +03001472{
1473 // Make sure that all params are constant before actual constant folding.
1474 for (auto *param : *getSequence())
Olli Etuaho95310b02015-06-02 17:43:38 +03001475 {
Olli Etuahob43846e2015-06-02 18:18:57 +03001476 if (param->getAsConstantUnion() == nullptr)
1477 {
1478 return nullptr;
1479 }
Olli Etuaho95310b02015-06-02 17:43:38 +03001480 }
Olli Etuaho1d122782015-11-06 15:35:17 +02001481 TConstantUnion *constArray = nullptr;
1482 if (isConstructor())
Olli Etuahof119a262016-08-19 15:54:22 +03001483 constArray = TIntermConstantUnion::FoldAggregateConstructor(this);
Olli Etuaho1d122782015-11-06 15:35:17 +02001484 else
Olli Etuahof119a262016-08-19 15:54:22 +03001485 constArray = TIntermConstantUnion::FoldAggregateBuiltIn(this, diagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001486
1487 // Nodes may be constant folded without being qualified as constant.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08001488 return CreateFoldedNode(constArray, this, getQualifier());
Olli Etuaho95310b02015-06-02 17:43:38 +03001489}
1490
Jamie Madillb1a85f42014-08-19 15:23:24 -04001491//
1492// The fold functions see if an operation on a constant can be done in place,
1493// without generating run-time code.
1494//
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001495// Returns the constant value to keep using or nullptr.
Jamie Madillb1a85f42014-08-19 15:23:24 -04001496//
Olli Etuaho3fdec912016-08-18 15:08:06 +03001497TConstantUnion *TIntermConstantUnion::foldBinary(TOperator op,
1498 TIntermConstantUnion *rightNode,
Jamie Madill5db69f52016-09-15 12:47:32 -04001499 TDiagnostics *diagnostics,
1500 const TSourceLoc &line)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001501{
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001502 const TConstantUnion *leftArray = getUnionArrayPointer();
1503 const TConstantUnion *rightArray = rightNode->getUnionArrayPointer();
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001504
Olli Etuahof119a262016-08-19 15:54:22 +03001505 ASSERT(leftArray && rightArray);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001506
1507 size_t objectSize = getType().getObjectSize();
1508
1509 // for a case like float f = vec4(2, 3, 4, 5) + 1.2;
1510 if (rightNode->getType().getObjectSize() == 1 && objectSize > 1)
1511 {
1512 rightArray = Vectorize(*rightNode->getUnionArrayPointer(), objectSize);
1513 }
1514 else if (rightNode->getType().getObjectSize() > 1 && objectSize == 1)
1515 {
1516 // for a case like float f = 1.2 + vec4(2, 3, 4, 5);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001517 leftArray = Vectorize(*getUnionArrayPointer(), rightNode->getType().getObjectSize());
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001518 objectSize = rightNode->getType().getObjectSize();
1519 }
1520
1521 TConstantUnion *resultArray = nullptr;
1522
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001523 switch (op)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001524 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001525 case EOpAdd:
1526 resultArray = new TConstantUnion[objectSize];
1527 for (size_t i = 0; i < objectSize; i++)
1528 resultArray[i] =
1529 TConstantUnion::add(leftArray[i], rightArray[i], diagnostics, line);
1530 break;
1531 case EOpSub:
1532 resultArray = new TConstantUnion[objectSize];
1533 for (size_t i = 0; i < objectSize; i++)
1534 resultArray[i] =
1535 TConstantUnion::sub(leftArray[i], rightArray[i], diagnostics, line);
1536 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001537
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001538 case EOpMul:
1539 case EOpVectorTimesScalar:
1540 case EOpMatrixTimesScalar:
1541 resultArray = new TConstantUnion[objectSize];
1542 for (size_t i = 0; i < objectSize; i++)
1543 resultArray[i] =
1544 TConstantUnion::mul(leftArray[i], rightArray[i], diagnostics, line);
1545 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001546
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001547 case EOpMatrixTimesMatrix:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001548 {
Jamie Madill5db69f52016-09-15 12:47:32 -04001549 // TODO(jmadll): This code should check for overflows.
Olli Etuaho3fdec912016-08-18 15:08:06 +03001550 ASSERT(getType().getBasicType() == EbtFloat && rightNode->getBasicType() == EbtFloat);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001551
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001552 const int leftCols = getCols();
1553 const int leftRows = getRows();
1554 const int rightCols = rightNode->getType().getCols();
1555 const int rightRows = rightNode->getType().getRows();
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001556 const int resultCols = rightCols;
1557 const int resultRows = leftRows;
1558
1559 resultArray = new TConstantUnion[resultCols * resultRows];
1560 for (int row = 0; row < resultRows; row++)
1561 {
1562 for (int column = 0; column < resultCols; column++)
1563 {
1564 resultArray[resultRows * column + row].setFConst(0.0f);
1565 for (int i = 0; i < leftCols; i++)
1566 {
1567 resultArray[resultRows * column + row].setFConst(
1568 resultArray[resultRows * column + row].getFConst() +
1569 leftArray[i * leftRows + row].getFConst() *
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001570 rightArray[column * rightRows + i].getFConst());
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001571 }
1572 }
1573 }
1574 }
1575 break;
1576
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001577 case EOpDiv:
1578 case EOpIMod:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001579 {
1580 resultArray = new TConstantUnion[objectSize];
1581 for (size_t i = 0; i < objectSize; i++)
1582 {
1583 switch (getType().getBasicType())
1584 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001585 case EbtFloat:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001586 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001587 ASSERT(op == EOpDiv);
1588 float dividend = leftArray[i].getFConst();
1589 float divisor = rightArray[i].getFConst();
1590 if (divisor == 0.0f)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001591 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001592 if (dividend == 0.0f)
Olli Etuahod4453572016-09-27 13:21:46 +01001593 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001594 diagnostics->warning(
1595 getLine(),
1596 "Zero divided by zero during constant folding generated NaN",
Olli Etuaho4de340a2016-12-16 09:32:03 +00001597 "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001598 resultArray[i].setFConst(std::numeric_limits<float>::quiet_NaN());
Olli Etuahod4453572016-09-27 13:21:46 +01001599 }
1600 else
1601 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001602 diagnostics->warning(getLine(),
1603 "Divide by zero during constant folding", "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001604 bool negativeResult =
1605 std::signbit(dividend) != std::signbit(divisor);
1606 resultArray[i].setFConst(
1607 negativeResult ? -std::numeric_limits<float>::infinity()
1608 : std::numeric_limits<float>::infinity());
Olli Etuahod4453572016-09-27 13:21:46 +01001609 }
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001610 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001611 else if (gl::isInf(dividend) && gl::isInf(divisor))
1612 {
1613 diagnostics->warning(getLine(),
1614 "Infinity divided by infinity during constant "
1615 "folding generated NaN",
Olli Etuaho4de340a2016-12-16 09:32:03 +00001616 "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001617 resultArray[i].setFConst(std::numeric_limits<float>::quiet_NaN());
1618 }
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001619 else
1620 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001621 float result = dividend / divisor;
1622 if (!gl::isInf(dividend) && gl::isInf(result))
Olli Etuahod4453572016-09-27 13:21:46 +01001623 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001624 diagnostics->warning(
1625 getLine(), "Constant folded division overflowed to infinity",
Olli Etuaho4de340a2016-12-16 09:32:03 +00001626 "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001627 }
1628 resultArray[i].setFConst(result);
1629 }
1630 break;
1631 }
1632 case EbtInt:
1633 if (rightArray[i] == 0)
1634 {
1635 diagnostics->warning(
Olli Etuaho4de340a2016-12-16 09:32:03 +00001636 getLine(), "Divide by zero error during constant folding", "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001637 resultArray[i].setIConst(INT_MAX);
1638 }
1639 else
1640 {
1641 int lhs = leftArray[i].getIConst();
1642 int divisor = rightArray[i].getIConst();
1643 if (op == EOpDiv)
1644 {
1645 // Check for the special case where the minimum representable number
1646 // is
1647 // divided by -1. If left alone this leads to integer overflow in
1648 // C++.
1649 // ESSL 3.00.6 section 4.1.3 Integers:
1650 // "However, for the case where the minimum representable value is
1651 // divided by -1, it is allowed to return either the minimum
1652 // representable value or the maximum representable value."
1653 if (lhs == -0x7fffffff - 1 && divisor == -1)
1654 {
1655 resultArray[i].setIConst(0x7fffffff);
1656 }
1657 else
1658 {
1659 resultArray[i].setIConst(lhs / divisor);
1660 }
Olli Etuahod4453572016-09-27 13:21:46 +01001661 }
1662 else
1663 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001664 ASSERT(op == EOpIMod);
1665 if (lhs < 0 || divisor < 0)
1666 {
1667 // ESSL 3.00.6 section 5.9: Results of modulus are undefined
1668 // when
1669 // either one of the operands is negative.
1670 diagnostics->warning(getLine(),
1671 "Negative modulus operator operand "
1672 "encountered during constant folding",
Olli Etuaho4de340a2016-12-16 09:32:03 +00001673 "%");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001674 resultArray[i].setIConst(0);
1675 }
1676 else
1677 {
1678 resultArray[i].setIConst(lhs % divisor);
1679 }
Olli Etuahod4453572016-09-27 13:21:46 +01001680 }
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001681 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001682 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001683
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001684 case EbtUInt:
1685 if (rightArray[i] == 0)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001686 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001687 diagnostics->warning(
Olli Etuaho4de340a2016-12-16 09:32:03 +00001688 getLine(), "Divide by zero error during constant folding", "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001689 resultArray[i].setUConst(UINT_MAX);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001690 }
1691 else
1692 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001693 if (op == EOpDiv)
1694 {
1695 resultArray[i].setUConst(leftArray[i].getUConst() /
1696 rightArray[i].getUConst());
1697 }
1698 else
1699 {
1700 ASSERT(op == EOpIMod);
1701 resultArray[i].setUConst(leftArray[i].getUConst() %
1702 rightArray[i].getUConst());
1703 }
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001704 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001705 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001706
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001707 default:
1708 UNREACHABLE();
1709 return nullptr;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001710 }
1711 }
1712 }
1713 break;
1714
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001715 case EOpMatrixTimesVector:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001716 {
Jamie Madill5db69f52016-09-15 12:47:32 -04001717 // TODO(jmadll): This code should check for overflows.
Olli Etuaho3fdec912016-08-18 15:08:06 +03001718 ASSERT(rightNode->getBasicType() == EbtFloat);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001719
1720 const int matrixCols = getCols();
1721 const int matrixRows = getRows();
1722
1723 resultArray = new TConstantUnion[matrixRows];
1724
1725 for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++)
1726 {
1727 resultArray[matrixRow].setFConst(0.0f);
1728 for (int col = 0; col < matrixCols; col++)
1729 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001730 resultArray[matrixRow].setFConst(
1731 resultArray[matrixRow].getFConst() +
1732 leftArray[col * matrixRows + matrixRow].getFConst() *
1733 rightArray[col].getFConst());
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001734 }
1735 }
1736 }
1737 break;
1738
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001739 case EOpVectorTimesMatrix:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001740 {
Jamie Madill5db69f52016-09-15 12:47:32 -04001741 // TODO(jmadll): This code should check for overflows.
Olli Etuaho3fdec912016-08-18 15:08:06 +03001742 ASSERT(getType().getBasicType() == EbtFloat);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001743
1744 const int matrixCols = rightNode->getType().getCols();
1745 const int matrixRows = rightNode->getType().getRows();
1746
1747 resultArray = new TConstantUnion[matrixCols];
1748
1749 for (int matrixCol = 0; matrixCol < matrixCols; matrixCol++)
1750 {
1751 resultArray[matrixCol].setFConst(0.0f);
1752 for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++)
1753 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001754 resultArray[matrixCol].setFConst(
1755 resultArray[matrixCol].getFConst() +
1756 leftArray[matrixRow].getFConst() *
1757 rightArray[matrixCol * matrixRows + matrixRow].getFConst());
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001758 }
1759 }
1760 }
1761 break;
1762
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001763 case EOpLogicalAnd:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001764 {
1765 resultArray = new TConstantUnion[objectSize];
1766 for (size_t i = 0; i < objectSize; i++)
1767 {
1768 resultArray[i] = leftArray[i] && rightArray[i];
1769 }
1770 }
1771 break;
1772
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001773 case EOpLogicalOr:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001774 {
1775 resultArray = new TConstantUnion[objectSize];
1776 for (size_t i = 0; i < objectSize; i++)
1777 {
1778 resultArray[i] = leftArray[i] || rightArray[i];
1779 }
1780 }
1781 break;
1782
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001783 case EOpLogicalXor:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001784 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001785 ASSERT(getType().getBasicType() == EbtBool);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001786 resultArray = new TConstantUnion[objectSize];
1787 for (size_t i = 0; i < objectSize; i++)
1788 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001789 resultArray[i].setBConst(leftArray[i] != rightArray[i]);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001790 }
1791 }
1792 break;
1793
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001794 case EOpBitwiseAnd:
1795 resultArray = new TConstantUnion[objectSize];
1796 for (size_t i = 0; i < objectSize; i++)
1797 resultArray[i] = leftArray[i] & rightArray[i];
1798 break;
1799 case EOpBitwiseXor:
1800 resultArray = new TConstantUnion[objectSize];
1801 for (size_t i = 0; i < objectSize; i++)
1802 resultArray[i] = leftArray[i] ^ rightArray[i];
1803 break;
1804 case EOpBitwiseOr:
1805 resultArray = new TConstantUnion[objectSize];
1806 for (size_t i = 0; i < objectSize; i++)
1807 resultArray[i] = leftArray[i] | rightArray[i];
1808 break;
1809 case EOpBitShiftLeft:
1810 resultArray = new TConstantUnion[objectSize];
1811 for (size_t i = 0; i < objectSize; i++)
1812 resultArray[i] =
1813 TConstantUnion::lshift(leftArray[i], rightArray[i], diagnostics, line);
1814 break;
1815 case EOpBitShiftRight:
1816 resultArray = new TConstantUnion[objectSize];
1817 for (size_t i = 0; i < objectSize; i++)
1818 resultArray[i] =
1819 TConstantUnion::rshift(leftArray[i], rightArray[i], diagnostics, line);
1820 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001821
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001822 case EOpLessThan:
1823 ASSERT(objectSize == 1);
1824 resultArray = new TConstantUnion[1];
1825 resultArray->setBConst(*leftArray < *rightArray);
1826 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001827
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001828 case EOpGreaterThan:
1829 ASSERT(objectSize == 1);
1830 resultArray = new TConstantUnion[1];
1831 resultArray->setBConst(*leftArray > *rightArray);
1832 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001833
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001834 case EOpLessThanEqual:
1835 ASSERT(objectSize == 1);
1836 resultArray = new TConstantUnion[1];
1837 resultArray->setBConst(!(*leftArray > *rightArray));
1838 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001839
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001840 case EOpGreaterThanEqual:
1841 ASSERT(objectSize == 1);
1842 resultArray = new TConstantUnion[1];
1843 resultArray->setBConst(!(*leftArray < *rightArray));
1844 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001845
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001846 case EOpEqual:
1847 case EOpNotEqual:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001848 {
1849 resultArray = new TConstantUnion[1];
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001850 bool equal = true;
Olli Etuaho40d9edf2015-11-12 17:30:34 +02001851 for (size_t i = 0; i < objectSize; i++)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001852 {
Olli Etuaho40d9edf2015-11-12 17:30:34 +02001853 if (leftArray[i] != rightArray[i])
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001854 {
Olli Etuaho40d9edf2015-11-12 17:30:34 +02001855 equal = false;
1856 break; // break out of for loop
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001857 }
1858 }
1859 if (op == EOpEqual)
1860 {
1861 resultArray->setBConst(equal);
1862 }
1863 else
1864 {
1865 resultArray->setBConst(!equal);
1866 }
1867 }
1868 break;
1869
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001870 default:
1871 UNREACHABLE();
1872 return nullptr;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001873 }
1874 return resultArray;
1875}
1876
Olli Etuahof119a262016-08-19 15:54:22 +03001877// The fold functions do operations on a constant at GLSL compile time, without generating run-time
1878// code. Returns the constant value to keep using. Nullptr should not be returned.
1879TConstantUnion *TIntermConstantUnion::foldUnaryNonComponentWise(TOperator op)
Jamie Madillb1a85f42014-08-19 15:23:24 -04001880{
Olli Etuahof119a262016-08-19 15:54:22 +03001881 // Do operations where the return type may have a different number of components compared to the
1882 // operand type.
Jamie Madillb1a85f42014-08-19 15:23:24 -04001883
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001884 const TConstantUnion *operandArray = getUnionArrayPointer();
Olli Etuahof119a262016-08-19 15:54:22 +03001885 ASSERT(operandArray);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301886
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001887 size_t objectSize = getType().getObjectSize();
Arun Patoleab2b9a22015-07-06 18:27:56 +05301888 TConstantUnion *resultArray = nullptr;
1889 switch (op)
1890 {
Olli Etuahof119a262016-08-19 15:54:22 +03001891 case EOpAny:
1892 ASSERT(getType().getBasicType() == EbtBool);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301893 resultArray = new TConstantUnion();
1894 resultArray->setBConst(false);
1895 for (size_t i = 0; i < objectSize; i++)
1896 {
1897 if (operandArray[i].getBConst())
1898 {
1899 resultArray->setBConst(true);
1900 break;
1901 }
1902 }
1903 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301904
Olli Etuahof119a262016-08-19 15:54:22 +03001905 case EOpAll:
1906 ASSERT(getType().getBasicType() == EbtBool);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301907 resultArray = new TConstantUnion();
1908 resultArray->setBConst(true);
1909 for (size_t i = 0; i < objectSize; i++)
1910 {
1911 if (!operandArray[i].getBConst())
1912 {
1913 resultArray->setBConst(false);
1914 break;
1915 }
1916 }
1917 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301918
Olli Etuahof119a262016-08-19 15:54:22 +03001919 case EOpLength:
1920 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301921 resultArray = new TConstantUnion();
1922 resultArray->setFConst(VectorLength(operandArray, objectSize));
1923 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301924
Olli Etuahof119a262016-08-19 15:54:22 +03001925 case EOpTranspose:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301926 {
Olli Etuahof119a262016-08-19 15:54:22 +03001927 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301928 resultArray = new TConstantUnion[objectSize];
1929 angle::Matrix<float> result =
Olli Etuahod5da5052016-08-29 13:16:55 +03001930 GetMatrix(operandArray, getType().getRows(), getType().getCols()).transpose();
Arun Patoleab2b9a22015-07-06 18:27:56 +05301931 SetUnionArrayFromMatrix(result, resultArray);
1932 break;
1933 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301934
Olli Etuahof119a262016-08-19 15:54:22 +03001935 case EOpDeterminant:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301936 {
Olli Etuahof119a262016-08-19 15:54:22 +03001937 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301938 unsigned int size = getType().getNominalSize();
1939 ASSERT(size >= 2 && size <= 4);
1940 resultArray = new TConstantUnion();
1941 resultArray->setFConst(GetMatrix(operandArray, size).determinant());
1942 break;
1943 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301944
Olli Etuahof119a262016-08-19 15:54:22 +03001945 case EOpInverse:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301946 {
Olli Etuahof119a262016-08-19 15:54:22 +03001947 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301948 unsigned int size = getType().getNominalSize();
1949 ASSERT(size >= 2 && size <= 4);
Olli Etuahof119a262016-08-19 15:54:22 +03001950 resultArray = new TConstantUnion[objectSize];
Arun Patoleab2b9a22015-07-06 18:27:56 +05301951 angle::Matrix<float> result = GetMatrix(operandArray, size).inverse();
1952 SetUnionArrayFromMatrix(result, resultArray);
1953 break;
1954 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301955
Olli Etuahof119a262016-08-19 15:54:22 +03001956 case EOpPackSnorm2x16:
1957 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301958 ASSERT(getType().getNominalSize() == 2);
1959 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03001960 resultArray->setUConst(
1961 gl::packSnorm2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
Arun Patoleab2b9a22015-07-06 18:27:56 +05301962 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301963
Olli Etuahof119a262016-08-19 15:54:22 +03001964 case EOpUnpackSnorm2x16:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301965 {
Olli Etuahof119a262016-08-19 15:54:22 +03001966 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301967 resultArray = new TConstantUnion[2];
1968 float f1, f2;
1969 gl::unpackSnorm2x16(operandArray[0].getUConst(), &f1, &f2);
1970 resultArray[0].setFConst(f1);
1971 resultArray[1].setFConst(f2);
1972 break;
1973 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301974
Olli Etuahof119a262016-08-19 15:54:22 +03001975 case EOpPackUnorm2x16:
1976 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301977 ASSERT(getType().getNominalSize() == 2);
1978 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03001979 resultArray->setUConst(
1980 gl::packUnorm2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
Arun Patoleab2b9a22015-07-06 18:27:56 +05301981 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301982
Olli Etuahof119a262016-08-19 15:54:22 +03001983 case EOpUnpackUnorm2x16:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301984 {
Olli Etuahof119a262016-08-19 15:54:22 +03001985 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301986 resultArray = new TConstantUnion[2];
1987 float f1, f2;
1988 gl::unpackUnorm2x16(operandArray[0].getUConst(), &f1, &f2);
1989 resultArray[0].setFConst(f1);
1990 resultArray[1].setFConst(f2);
1991 break;
1992 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301993
Olli Etuahof119a262016-08-19 15:54:22 +03001994 case EOpPackHalf2x16:
1995 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301996 ASSERT(getType().getNominalSize() == 2);
1997 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03001998 resultArray->setUConst(
1999 gl::packHalf2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
Arun Patoleab2b9a22015-07-06 18:27:56 +05302000 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302001
Olli Etuahof119a262016-08-19 15:54:22 +03002002 case EOpUnpackHalf2x16:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302003 {
Olli Etuahof119a262016-08-19 15:54:22 +03002004 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302005 resultArray = new TConstantUnion[2];
2006 float f1, f2;
2007 gl::unpackHalf2x16(operandArray[0].getUConst(), &f1, &f2);
2008 resultArray[0].setFConst(f1);
2009 resultArray[1].setFConst(f2);
2010 break;
2011 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302012
Olli Etuaho25aef452017-01-29 16:15:44 -08002013 case EOpPackUnorm4x8:
2014 {
2015 ASSERT(getType().getBasicType() == EbtFloat);
2016 resultArray = new TConstantUnion();
2017 resultArray->setUConst(
2018 gl::PackUnorm4x8(operandArray[0].getFConst(), operandArray[1].getFConst(),
2019 operandArray[2].getFConst(), operandArray[3].getFConst()));
2020 break;
2021 }
2022 case EOpPackSnorm4x8:
2023 {
2024 ASSERT(getType().getBasicType() == EbtFloat);
2025 resultArray = new TConstantUnion();
2026 resultArray->setUConst(
2027 gl::PackSnorm4x8(operandArray[0].getFConst(), operandArray[1].getFConst(),
2028 operandArray[2].getFConst(), operandArray[3].getFConst()));
2029 break;
2030 }
2031 case EOpUnpackUnorm4x8:
2032 {
2033 ASSERT(getType().getBasicType() == EbtUInt);
2034 resultArray = new TConstantUnion[4];
2035 float f[4];
2036 gl::UnpackUnorm4x8(operandArray[0].getUConst(), f);
2037 for (size_t i = 0; i < 4; ++i)
2038 {
2039 resultArray[i].setFConst(f[i]);
2040 }
2041 break;
2042 }
2043 case EOpUnpackSnorm4x8:
2044 {
2045 ASSERT(getType().getBasicType() == EbtUInt);
2046 resultArray = new TConstantUnion[4];
2047 float f[4];
2048 gl::UnpackSnorm4x8(operandArray[0].getUConst(), f);
2049 for (size_t i = 0; i < 4; ++i)
2050 {
2051 resultArray[i].setFConst(f[i]);
2052 }
2053 break;
2054 }
2055
Olli Etuahof119a262016-08-19 15:54:22 +03002056 default:
2057 UNREACHABLE();
2058 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302059 }
2060
2061 return resultArray;
2062}
2063
Olli Etuahof119a262016-08-19 15:54:22 +03002064TConstantUnion *TIntermConstantUnion::foldUnaryComponentWise(TOperator op,
2065 TDiagnostics *diagnostics)
Arun Patoleab2b9a22015-07-06 18:27:56 +05302066{
Olli Etuahof119a262016-08-19 15:54:22 +03002067 // Do unary operations where each component of the result is computed based on the corresponding
2068 // component of the operand. Also folds normalize, though the divisor in that case takes all
2069 // components into account.
Arun Patoleab2b9a22015-07-06 18:27:56 +05302070
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002071 const TConstantUnion *operandArray = getUnionArrayPointer();
Olli Etuahof119a262016-08-19 15:54:22 +03002072 ASSERT(operandArray);
Jamie Madillb1a85f42014-08-19 15:23:24 -04002073
2074 size_t objectSize = getType().getObjectSize();
2075
Arun Patoleab2b9a22015-07-06 18:27:56 +05302076 TConstantUnion *resultArray = new TConstantUnion[objectSize];
2077 for (size_t i = 0; i < objectSize; i++)
Arun Patole9d0b1f92015-05-20 14:27:17 +05302078 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002079 switch (op)
Arun Patole9d0b1f92015-05-20 14:27:17 +05302080 {
Olli Etuahof119a262016-08-19 15:54:22 +03002081 case EOpNegative:
2082 switch (getType().getBasicType())
2083 {
2084 case EbtFloat:
2085 resultArray[i].setFConst(-operandArray[i].getFConst());
2086 break;
2087 case EbtInt:
Olli Etuaho42fad762016-09-28 10:06:29 +01002088 if (operandArray[i] == std::numeric_limits<int>::min())
2089 {
2090 // The minimum representable integer doesn't have a positive
2091 // counterpart, rather the negation overflows and in ESSL is supposed to
2092 // wrap back to the minimum representable integer. Make sure that we
2093 // don't actually let the negation overflow, which has undefined
2094 // behavior in C++.
2095 resultArray[i].setIConst(std::numeric_limits<int>::min());
2096 }
2097 else
2098 {
2099 resultArray[i].setIConst(-operandArray[i].getIConst());
2100 }
Olli Etuahof119a262016-08-19 15:54:22 +03002101 break;
2102 case EbtUInt:
Olli Etuaho42fad762016-09-28 10:06:29 +01002103 if (operandArray[i] == 0x80000000u)
2104 {
2105 resultArray[i].setUConst(0x80000000u);
2106 }
2107 else
2108 {
2109 resultArray[i].setUConst(static_cast<unsigned int>(
2110 -static_cast<int>(operandArray[i].getUConst())));
2111 }
Olli Etuahof119a262016-08-19 15:54:22 +03002112 break;
2113 default:
2114 UNREACHABLE();
2115 return nullptr;
2116 }
Arun Patole1155ddd2015-06-05 18:04:36 +05302117 break;
Arun Patolecdfa8f52015-06-30 17:48:25 +05302118
Olli Etuahof119a262016-08-19 15:54:22 +03002119 case EOpPositive:
2120 switch (getType().getBasicType())
2121 {
2122 case EbtFloat:
2123 resultArray[i].setFConst(operandArray[i].getFConst());
2124 break;
2125 case EbtInt:
2126 resultArray[i].setIConst(operandArray[i].getIConst());
2127 break;
2128 case EbtUInt:
2129 resultArray[i].setUConst(static_cast<unsigned int>(
2130 static_cast<int>(operandArray[i].getUConst())));
2131 break;
2132 default:
2133 UNREACHABLE();
2134 return nullptr;
2135 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302136 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302137
Olli Etuahof119a262016-08-19 15:54:22 +03002138 case EOpLogicalNot:
2139 switch (getType().getBasicType())
2140 {
2141 case EbtBool:
2142 resultArray[i].setBConst(!operandArray[i].getBConst());
2143 break;
2144 default:
2145 UNREACHABLE();
2146 return nullptr;
2147 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302148 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302149
Olli Etuahof119a262016-08-19 15:54:22 +03002150 case EOpBitwiseNot:
2151 switch (getType().getBasicType())
2152 {
2153 case EbtInt:
2154 resultArray[i].setIConst(~operandArray[i].getIConst());
2155 break;
2156 case EbtUInt:
2157 resultArray[i].setUConst(~operandArray[i].getUConst());
2158 break;
2159 default:
2160 UNREACHABLE();
2161 return nullptr;
2162 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302163 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302164
Olli Etuahof119a262016-08-19 15:54:22 +03002165 case EOpRadians:
2166 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302167 resultArray[i].setFConst(kDegreesToRadiansMultiplier * operandArray[i].getFConst());
2168 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302169
Olli Etuahof119a262016-08-19 15:54:22 +03002170 case EOpDegrees:
2171 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302172 resultArray[i].setFConst(kRadiansToDegreesMultiplier * operandArray[i].getFConst());
2173 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302174
Olli Etuahof119a262016-08-19 15:54:22 +03002175 case EOpSin:
2176 foldFloatTypeUnary(operandArray[i], &sinf, &resultArray[i]);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302177 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302178
Olli Etuahof119a262016-08-19 15:54:22 +03002179 case EOpCos:
2180 foldFloatTypeUnary(operandArray[i], &cosf, &resultArray[i]);
2181 break;
2182
2183 case EOpTan:
2184 foldFloatTypeUnary(operandArray[i], &tanf, &resultArray[i]);
2185 break;
2186
2187 case EOpAsin:
2188 // For asin(x), results are undefined if |x| > 1, we are choosing to set result to
2189 // 0.
2190 if (fabsf(operandArray[i].getFConst()) > 1.0f)
2191 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2192 diagnostics, &resultArray[i]);
2193 else
2194 foldFloatTypeUnary(operandArray[i], &asinf, &resultArray[i]);
2195 break;
2196
2197 case EOpAcos:
2198 // For acos(x), results are undefined if |x| > 1, we are choosing to set result to
2199 // 0.
2200 if (fabsf(operandArray[i].getFConst()) > 1.0f)
2201 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2202 diagnostics, &resultArray[i]);
2203 else
2204 foldFloatTypeUnary(operandArray[i], &acosf, &resultArray[i]);
2205 break;
2206
2207 case EOpAtan:
2208 foldFloatTypeUnary(operandArray[i], &atanf, &resultArray[i]);
2209 break;
2210
2211 case EOpSinh:
2212 foldFloatTypeUnary(operandArray[i], &sinhf, &resultArray[i]);
2213 break;
2214
2215 case EOpCosh:
2216 foldFloatTypeUnary(operandArray[i], &coshf, &resultArray[i]);
2217 break;
2218
2219 case EOpTanh:
2220 foldFloatTypeUnary(operandArray[i], &tanhf, &resultArray[i]);
2221 break;
2222
2223 case EOpAsinh:
2224 foldFloatTypeUnary(operandArray[i], &asinhf, &resultArray[i]);
2225 break;
2226
2227 case EOpAcosh:
2228 // For acosh(x), results are undefined if x < 1, we are choosing to set result to 0.
2229 if (operandArray[i].getFConst() < 1.0f)
2230 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2231 diagnostics, &resultArray[i]);
2232 else
2233 foldFloatTypeUnary(operandArray[i], &acoshf, &resultArray[i]);
2234 break;
2235
2236 case EOpAtanh:
2237 // For atanh(x), results are undefined if |x| >= 1, we are choosing to set result to
2238 // 0.
2239 if (fabsf(operandArray[i].getFConst()) >= 1.0f)
2240 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2241 diagnostics, &resultArray[i]);
2242 else
2243 foldFloatTypeUnary(operandArray[i], &atanhf, &resultArray[i]);
2244 break;
2245
2246 case EOpAbs:
2247 switch (getType().getBasicType())
Arun Patoleab2b9a22015-07-06 18:27:56 +05302248 {
Olli Etuahof119a262016-08-19 15:54:22 +03002249 case EbtFloat:
2250 resultArray[i].setFConst(fabsf(operandArray[i].getFConst()));
2251 break;
2252 case EbtInt:
2253 resultArray[i].setIConst(abs(operandArray[i].getIConst()));
2254 break;
2255 default:
2256 UNREACHABLE();
2257 return nullptr;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302258 }
2259 break;
Olli Etuahof119a262016-08-19 15:54:22 +03002260
2261 case EOpSign:
2262 switch (getType().getBasicType())
Arun Patoleab2b9a22015-07-06 18:27:56 +05302263 {
Olli Etuahof119a262016-08-19 15:54:22 +03002264 case EbtFloat:
2265 {
2266 float fConst = operandArray[i].getFConst();
2267 float fResult = 0.0f;
2268 if (fConst > 0.0f)
2269 fResult = 1.0f;
2270 else if (fConst < 0.0f)
2271 fResult = -1.0f;
2272 resultArray[i].setFConst(fResult);
2273 break;
2274 }
2275 case EbtInt:
2276 {
2277 int iConst = operandArray[i].getIConst();
2278 int iResult = 0;
2279 if (iConst > 0)
2280 iResult = 1;
2281 else if (iConst < 0)
2282 iResult = -1;
2283 resultArray[i].setIConst(iResult);
2284 break;
2285 }
2286 default:
2287 UNREACHABLE();
2288 return nullptr;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302289 }
2290 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302291
Olli Etuahof119a262016-08-19 15:54:22 +03002292 case EOpFloor:
2293 foldFloatTypeUnary(operandArray[i], &floorf, &resultArray[i]);
2294 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302295
Olli Etuahof119a262016-08-19 15:54:22 +03002296 case EOpTrunc:
2297 foldFloatTypeUnary(operandArray[i], &truncf, &resultArray[i]);
2298 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302299
Olli Etuahof119a262016-08-19 15:54:22 +03002300 case EOpRound:
2301 foldFloatTypeUnary(operandArray[i], &roundf, &resultArray[i]);
2302 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302303
Olli Etuahof119a262016-08-19 15:54:22 +03002304 case EOpRoundEven:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302305 {
Olli Etuahof119a262016-08-19 15:54:22 +03002306 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302307 float x = operandArray[i].getFConst();
2308 float result;
2309 float fractPart = modff(x, &result);
2310 if (fabsf(fractPart) == 0.5f)
2311 result = 2.0f * roundf(x / 2.0f);
2312 else
2313 result = roundf(x);
2314 resultArray[i].setFConst(result);
2315 break;
2316 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302317
Olli Etuahof119a262016-08-19 15:54:22 +03002318 case EOpCeil:
2319 foldFloatTypeUnary(operandArray[i], &ceilf, &resultArray[i]);
2320 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302321
Olli Etuahof119a262016-08-19 15:54:22 +03002322 case EOpFract:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302323 {
Olli Etuahof119a262016-08-19 15:54:22 +03002324 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302325 float x = operandArray[i].getFConst();
2326 resultArray[i].setFConst(x - floorf(x));
2327 break;
2328 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302329
Olli Etuahof119a262016-08-19 15:54:22 +03002330 case EOpIsNan:
2331 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05302332 resultArray[i].setBConst(gl::isNaN(operandArray[0].getFConst()));
2333 break;
Arun Patole551279e2015-07-07 18:18:23 +05302334
Olli Etuahof119a262016-08-19 15:54:22 +03002335 case EOpIsInf:
2336 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05302337 resultArray[i].setBConst(gl::isInf(operandArray[0].getFConst()));
2338 break;
Arun Patole551279e2015-07-07 18:18:23 +05302339
Olli Etuahof119a262016-08-19 15:54:22 +03002340 case EOpFloatBitsToInt:
2341 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05302342 resultArray[i].setIConst(gl::bitCast<int32_t>(operandArray[0].getFConst()));
2343 break;
Arun Patole551279e2015-07-07 18:18:23 +05302344
Olli Etuahof119a262016-08-19 15:54:22 +03002345 case EOpFloatBitsToUint:
2346 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05302347 resultArray[i].setUConst(gl::bitCast<uint32_t>(operandArray[0].getFConst()));
2348 break;
Arun Patole551279e2015-07-07 18:18:23 +05302349
Olli Etuahof119a262016-08-19 15:54:22 +03002350 case EOpIntBitsToFloat:
2351 ASSERT(getType().getBasicType() == EbtInt);
Arun Patole551279e2015-07-07 18:18:23 +05302352 resultArray[i].setFConst(gl::bitCast<float>(operandArray[0].getIConst()));
2353 break;
Arun Patole551279e2015-07-07 18:18:23 +05302354
Olli Etuahof119a262016-08-19 15:54:22 +03002355 case EOpUintBitsToFloat:
2356 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patole551279e2015-07-07 18:18:23 +05302357 resultArray[i].setFConst(gl::bitCast<float>(operandArray[0].getUConst()));
2358 break;
Arun Patole551279e2015-07-07 18:18:23 +05302359
Olli Etuahof119a262016-08-19 15:54:22 +03002360 case EOpExp:
2361 foldFloatTypeUnary(operandArray[i], &expf, &resultArray[i]);
2362 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302363
Olli Etuahof119a262016-08-19 15:54:22 +03002364 case EOpLog:
2365 // For log(x), results are undefined if x <= 0, we are choosing to set result to 0.
2366 if (operandArray[i].getFConst() <= 0.0f)
2367 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2368 diagnostics, &resultArray[i]);
2369 else
2370 foldFloatTypeUnary(operandArray[i], &logf, &resultArray[i]);
2371 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302372
Olli Etuahof119a262016-08-19 15:54:22 +03002373 case EOpExp2:
2374 foldFloatTypeUnary(operandArray[i], &exp2f, &resultArray[i]);
2375 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302376
Olli Etuahof119a262016-08-19 15:54:22 +03002377 case EOpLog2:
2378 // For log2(x), results are undefined if x <= 0, we are choosing to set result to 0.
2379 // And log2f is not available on some plarforms like old android, so just using
2380 // log(x)/log(2) here.
2381 if (operandArray[i].getFConst() <= 0.0f)
2382 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2383 diagnostics, &resultArray[i]);
2384 else
2385 {
2386 foldFloatTypeUnary(operandArray[i], &logf, &resultArray[i]);
2387 resultArray[i].setFConst(resultArray[i].getFConst() / logf(2.0f));
2388 }
2389 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302390
Olli Etuahof119a262016-08-19 15:54:22 +03002391 case EOpSqrt:
2392 // For sqrt(x), results are undefined if x < 0, we are choosing to set result to 0.
2393 if (operandArray[i].getFConst() < 0.0f)
2394 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2395 diagnostics, &resultArray[i]);
2396 else
2397 foldFloatTypeUnary(operandArray[i], &sqrtf, &resultArray[i]);
2398 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302399
Olli Etuahof119a262016-08-19 15:54:22 +03002400 case EOpInverseSqrt:
2401 // There is no stdlib built-in function equavalent for GLES built-in inversesqrt(),
2402 // so getting the square root first using builtin function sqrt() and then taking
2403 // its inverse.
2404 // Also, for inversesqrt(x), results are undefined if x <= 0, we are choosing to set
2405 // result to 0.
2406 if (operandArray[i].getFConst() <= 0.0f)
2407 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2408 diagnostics, &resultArray[i]);
2409 else
2410 {
2411 foldFloatTypeUnary(operandArray[i], &sqrtf, &resultArray[i]);
2412 resultArray[i].setFConst(1.0f / resultArray[i].getFConst());
2413 }
2414 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302415
Olli Etuahod68924e2017-01-02 17:34:40 +00002416 case EOpLogicalNotComponentWise:
Olli Etuahof119a262016-08-19 15:54:22 +03002417 ASSERT(getType().getBasicType() == EbtBool);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302418 resultArray[i].setBConst(!operandArray[i].getBConst());
2419 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302420
Olli Etuahof119a262016-08-19 15:54:22 +03002421 case EOpNormalize:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302422 {
Olli Etuahof119a262016-08-19 15:54:22 +03002423 ASSERT(getType().getBasicType() == EbtFloat);
2424 float x = operandArray[i].getFConst();
Arun Patoleab2b9a22015-07-06 18:27:56 +05302425 float length = VectorLength(operandArray, objectSize);
2426 if (length)
2427 resultArray[i].setFConst(x / length);
2428 else
Olli Etuahof119a262016-08-19 15:54:22 +03002429 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2430 diagnostics, &resultArray[i]);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302431 break;
2432 }
Olli Etuaho9250cb22017-01-21 10:51:27 +00002433 case EOpBitfieldReverse:
2434 {
2435 uint32_t value;
2436 if (getType().getBasicType() == EbtInt)
2437 {
2438 value = static_cast<uint32_t>(operandArray[i].getIConst());
2439 }
2440 else
2441 {
2442 ASSERT(getType().getBasicType() == EbtUInt);
2443 value = operandArray[i].getUConst();
2444 }
2445 uint32_t result = gl::BitfieldReverse(value);
2446 if (getType().getBasicType() == EbtInt)
2447 {
2448 resultArray[i].setIConst(static_cast<int32_t>(result));
2449 }
2450 else
2451 {
2452 resultArray[i].setUConst(result);
2453 }
2454 break;
2455 }
2456 case EOpBitCount:
2457 {
2458 uint32_t value;
2459 if (getType().getBasicType() == EbtInt)
2460 {
2461 value = static_cast<uint32_t>(operandArray[i].getIConst());
2462 }
2463 else
2464 {
2465 ASSERT(getType().getBasicType() == EbtUInt);
2466 value = operandArray[i].getUConst();
2467 }
2468 int result = gl::BitCount(value);
2469 resultArray[i].setIConst(result);
2470 break;
2471 }
2472 case EOpFindLSB:
2473 {
2474 uint32_t value;
2475 if (getType().getBasicType() == EbtInt)
2476 {
2477 value = static_cast<uint32_t>(operandArray[i].getIConst());
2478 }
2479 else
2480 {
2481 ASSERT(getType().getBasicType() == EbtUInt);
2482 value = operandArray[i].getUConst();
2483 }
2484 resultArray[i].setIConst(gl::FindLSB(value));
2485 break;
2486 }
2487 case EOpFindMSB:
2488 {
2489 uint32_t value;
2490 if (getType().getBasicType() == EbtInt)
2491 {
2492 int intValue = operandArray[i].getIConst();
2493 value = static_cast<uint32_t>(intValue);
2494 if (intValue < 0)
2495 {
2496 // Look for zero instead of one in value. This also handles the intValue ==
2497 // -1 special case, where the return value needs to be -1.
2498 value = ~value;
2499 }
2500 }
2501 else
2502 {
2503 ASSERT(getType().getBasicType() == EbtUInt);
2504 value = operandArray[i].getUConst();
2505 }
2506 resultArray[i].setIConst(gl::FindMSB(value));
2507 break;
2508 }
Olli Etuahof119a262016-08-19 15:54:22 +03002509 case EOpDFdx:
2510 case EOpDFdy:
2511 case EOpFwidth:
2512 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole0c5409f2015-07-08 15:17:53 +05302513 // Derivatives of constant arguments should be 0.
2514 resultArray[i].setFConst(0.0f);
2515 break;
Arun Patole0c5409f2015-07-08 15:17:53 +05302516
Olli Etuahof119a262016-08-19 15:54:22 +03002517 default:
2518 return nullptr;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302519 }
Arun Patole9d0b1f92015-05-20 14:27:17 +05302520 }
Jamie Madillb1a85f42014-08-19 15:23:24 -04002521
Arun Patoleab2b9a22015-07-06 18:27:56 +05302522 return resultArray;
Jamie Madillb1a85f42014-08-19 15:23:24 -04002523}
2524
Olli Etuahof119a262016-08-19 15:54:22 +03002525void TIntermConstantUnion::foldFloatTypeUnary(const TConstantUnion &parameter,
2526 FloatTypeUnaryFunc builtinFunc,
2527 TConstantUnion *result) const
Arun Patole9dea48f2015-04-02 11:45:09 +05302528{
2529 ASSERT(builtinFunc);
2530
Olli Etuahof119a262016-08-19 15:54:22 +03002531 ASSERT(getType().getBasicType() == EbtFloat);
2532 result->setFConst(builtinFunc(parameter.getFConst()));
Arun Patole9dea48f2015-04-02 11:45:09 +05302533}
2534
Jamie Madillb1a85f42014-08-19 15:23:24 -04002535// static
Olli Etuahof119a262016-08-19 15:54:22 +03002536TConstantUnion *TIntermConstantUnion::FoldAggregateConstructor(TIntermAggregate *aggregate)
Olli Etuaho1d122782015-11-06 15:35:17 +02002537{
2538 ASSERT(aggregate->getSequence()->size() > 0u);
2539 size_t resultSize = aggregate->getType().getObjectSize();
2540 TConstantUnion *resultArray = new TConstantUnion[resultSize];
2541 TBasicType basicType = aggregate->getBasicType();
2542
2543 size_t resultIndex = 0u;
2544
2545 if (aggregate->getSequence()->size() == 1u)
2546 {
2547 TIntermNode *argument = aggregate->getSequence()->front();
2548 TIntermConstantUnion *argumentConstant = argument->getAsConstantUnion();
2549 const TConstantUnion *argumentUnionArray = argumentConstant->getUnionArrayPointer();
2550 // Check the special case of constructing a matrix diagonal from a single scalar,
2551 // or a vector from a single scalar.
2552 if (argumentConstant->getType().getObjectSize() == 1u)
2553 {
2554 if (aggregate->isMatrix())
2555 {
2556 int resultCols = aggregate->getType().getCols();
2557 int resultRows = aggregate->getType().getRows();
2558 for (int col = 0; col < resultCols; ++col)
2559 {
2560 for (int row = 0; row < resultRows; ++row)
2561 {
2562 if (col == row)
2563 {
2564 resultArray[resultIndex].cast(basicType, argumentUnionArray[0]);
2565 }
2566 else
2567 {
2568 resultArray[resultIndex].setFConst(0.0f);
2569 }
2570 ++resultIndex;
2571 }
2572 }
2573 }
2574 else
2575 {
2576 while (resultIndex < resultSize)
2577 {
2578 resultArray[resultIndex].cast(basicType, argumentUnionArray[0]);
2579 ++resultIndex;
2580 }
2581 }
2582 ASSERT(resultIndex == resultSize);
2583 return resultArray;
2584 }
2585 else if (aggregate->isMatrix() && argumentConstant->isMatrix())
2586 {
2587 // The special case of constructing a matrix from a matrix.
2588 int argumentCols = argumentConstant->getType().getCols();
2589 int argumentRows = argumentConstant->getType().getRows();
2590 int resultCols = aggregate->getType().getCols();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002591 int resultRows = aggregate->getType().getRows();
Olli Etuaho1d122782015-11-06 15:35:17 +02002592 for (int col = 0; col < resultCols; ++col)
2593 {
2594 for (int row = 0; row < resultRows; ++row)
2595 {
2596 if (col < argumentCols && row < argumentRows)
2597 {
2598 resultArray[resultIndex].cast(basicType,
2599 argumentUnionArray[col * argumentRows + row]);
2600 }
2601 else if (col == row)
2602 {
2603 resultArray[resultIndex].setFConst(1.0f);
2604 }
2605 else
2606 {
2607 resultArray[resultIndex].setFConst(0.0f);
2608 }
2609 ++resultIndex;
2610 }
2611 }
2612 ASSERT(resultIndex == resultSize);
2613 return resultArray;
2614 }
2615 }
2616
2617 for (TIntermNode *&argument : *aggregate->getSequence())
2618 {
2619 TIntermConstantUnion *argumentConstant = argument->getAsConstantUnion();
2620 size_t argumentSize = argumentConstant->getType().getObjectSize();
2621 const TConstantUnion *argumentUnionArray = argumentConstant->getUnionArrayPointer();
2622 for (size_t i = 0u; i < argumentSize; ++i)
2623 {
2624 if (resultIndex >= resultSize)
2625 break;
2626 resultArray[resultIndex].cast(basicType, argumentUnionArray[i]);
2627 ++resultIndex;
2628 }
2629 }
2630 ASSERT(resultIndex == resultSize);
2631 return resultArray;
2632}
2633
2634// static
Olli Etuahof119a262016-08-19 15:54:22 +03002635TConstantUnion *TIntermConstantUnion::FoldAggregateBuiltIn(TIntermAggregate *aggregate,
2636 TDiagnostics *diagnostics)
Arun Patole274f0702015-05-05 13:33:30 +05302637{
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002638 TOperator op = aggregate->getOp();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002639 TIntermSequence *arguments = aggregate->getSequence();
2640 unsigned int argsCount = static_cast<unsigned int>(arguments->size());
2641 std::vector<const TConstantUnion *> unionArrays(argsCount);
2642 std::vector<size_t> objectSizes(argsCount);
Olli Etuahob43846e2015-06-02 18:18:57 +03002643 size_t maxObjectSize = 0;
Arun Patole274f0702015-05-05 13:33:30 +05302644 TBasicType basicType = EbtVoid;
2645 TSourceLoc loc;
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002646 for (unsigned int i = 0; i < argsCount; i++)
Arun Patole274f0702015-05-05 13:33:30 +05302647 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002648 TIntermConstantUnion *argConstant = (*arguments)[i]->getAsConstantUnion();
2649 ASSERT(argConstant != nullptr); // Should be checked already.
Arun Patole274f0702015-05-05 13:33:30 +05302650
2651 if (i == 0)
2652 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002653 basicType = argConstant->getType().getBasicType();
2654 loc = argConstant->getLine();
Arun Patole274f0702015-05-05 13:33:30 +05302655 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002656 unionArrays[i] = argConstant->getUnionArrayPointer();
2657 objectSizes[i] = argConstant->getType().getObjectSize();
Olli Etuahob43846e2015-06-02 18:18:57 +03002658 if (objectSizes[i] > maxObjectSize)
2659 maxObjectSize = objectSizes[i];
Arun Patole274f0702015-05-05 13:33:30 +05302660 }
2661
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002662 if (!(*arguments)[0]->getAsTyped()->isMatrix() && aggregate->getOp() != EOpOuterProduct)
Arun Patole7fa33552015-06-10 15:15:18 +05302663 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002664 for (unsigned int i = 0; i < argsCount; i++)
Arun Patole7fa33552015-06-10 15:15:18 +05302665 if (objectSizes[i] != maxObjectSize)
2666 unionArrays[i] = Vectorize(*unionArrays[i], maxObjectSize);
2667 }
Arun Patole274f0702015-05-05 13:33:30 +05302668
Olli Etuahob43846e2015-06-02 18:18:57 +03002669 TConstantUnion *resultArray = nullptr;
Olli Etuaho51182ab2017-01-22 00:12:29 +00002670
2671 switch (op)
Arun Patole274f0702015-05-05 13:33:30 +05302672 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002673 case EOpAtan:
Arun Patole274f0702015-05-05 13:33:30 +05302674 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002675 ASSERT(basicType == EbtFloat);
2676 resultArray = new TConstantUnion[maxObjectSize];
2677 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302678 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002679 float y = unionArrays[0][i].getFConst();
2680 float x = unionArrays[1][i].getFConst();
2681 // Results are undefined if x and y are both 0.
2682 if (x == 0.0f && y == 0.0f)
2683 UndefinedConstantFoldingError(loc, op, basicType, diagnostics, &resultArray[i]);
2684 else
2685 resultArray[i].setFConst(atan2f(y, x));
Arun Patolebf790422015-05-18 17:53:04 +05302686 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002687 break;
2688 }
Arun Patolebf790422015-05-18 17:53:04 +05302689
Olli Etuaho51182ab2017-01-22 00:12:29 +00002690 case EOpPow:
2691 {
2692 ASSERT(basicType == EbtFloat);
2693 resultArray = new TConstantUnion[maxObjectSize];
2694 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302695 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002696 float x = unionArrays[0][i].getFConst();
2697 float y = unionArrays[1][i].getFConst();
2698 // Results are undefined if x < 0.
2699 // Results are undefined if x = 0 and y <= 0.
2700 if (x < 0.0f)
2701 UndefinedConstantFoldingError(loc, op, basicType, diagnostics, &resultArray[i]);
2702 else if (x == 0.0f && y <= 0.0f)
2703 UndefinedConstantFoldingError(loc, op, basicType, diagnostics, &resultArray[i]);
2704 else
2705 resultArray[i].setFConst(powf(x, y));
Arun Patolebf790422015-05-18 17:53:04 +05302706 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002707 break;
2708 }
Arun Patolebf790422015-05-18 17:53:04 +05302709
Olli Etuaho51182ab2017-01-22 00:12:29 +00002710 case EOpMod:
2711 {
2712 ASSERT(basicType == EbtFloat);
2713 resultArray = new TConstantUnion[maxObjectSize];
2714 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302715 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002716 float x = unionArrays[0][i].getFConst();
2717 float y = unionArrays[1][i].getFConst();
2718 resultArray[i].setFConst(x - y * floorf(x / y));
Arun Patolebf790422015-05-18 17:53:04 +05302719 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002720 break;
2721 }
Arun Patolebf790422015-05-18 17:53:04 +05302722
Olli Etuaho51182ab2017-01-22 00:12:29 +00002723 case EOpMin:
2724 {
2725 resultArray = new TConstantUnion[maxObjectSize];
2726 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patole274f0702015-05-05 13:33:30 +05302727 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002728 switch (basicType)
Arun Patole274f0702015-05-05 13:33:30 +05302729 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002730 case EbtFloat:
2731 resultArray[i].setFConst(
2732 std::min(unionArrays[0][i].getFConst(), unionArrays[1][i].getFConst()));
2733 break;
2734 case EbtInt:
2735 resultArray[i].setIConst(
2736 std::min(unionArrays[0][i].getIConst(), unionArrays[1][i].getIConst()));
2737 break;
2738 case EbtUInt:
2739 resultArray[i].setUConst(
2740 std::min(unionArrays[0][i].getUConst(), unionArrays[1][i].getUConst()));
2741 break;
2742 default:
2743 UNREACHABLE();
2744 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302745 }
2746 }
2747 break;
Arun Patole274f0702015-05-05 13:33:30 +05302748 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002749
2750 case EOpMax:
Arun Patole274f0702015-05-05 13:33:30 +05302751 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002752 resultArray = new TConstantUnion[maxObjectSize];
2753 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patole274f0702015-05-05 13:33:30 +05302754 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002755 switch (basicType)
Arun Patole274f0702015-05-05 13:33:30 +05302756 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002757 case EbtFloat:
2758 resultArray[i].setFConst(
2759 std::max(unionArrays[0][i].getFConst(), unionArrays[1][i].getFConst()));
2760 break;
2761 case EbtInt:
2762 resultArray[i].setIConst(
2763 std::max(unionArrays[0][i].getIConst(), unionArrays[1][i].getIConst()));
2764 break;
2765 case EbtUInt:
2766 resultArray[i].setUConst(
2767 std::max(unionArrays[0][i].getUConst(), unionArrays[1][i].getUConst()));
2768 break;
2769 default:
2770 UNREACHABLE();
2771 break;
Arun Patole274f0702015-05-05 13:33:30 +05302772 }
2773 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002774 break;
Arun Patole274f0702015-05-05 13:33:30 +05302775 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002776
2777 case EOpStep:
2778 {
2779 ASSERT(basicType == EbtFloat);
2780 resultArray = new TConstantUnion[maxObjectSize];
2781 for (size_t i = 0; i < maxObjectSize; i++)
2782 resultArray[i].setFConst(
2783 unionArrays[1][i].getFConst() < unionArrays[0][i].getFConst() ? 0.0f : 1.0f);
2784 break;
2785 }
2786
2787 case EOpLessThanComponentWise:
2788 {
2789 resultArray = new TConstantUnion[maxObjectSize];
2790 for (size_t i = 0; i < maxObjectSize; i++)
2791 {
2792 switch (basicType)
2793 {
2794 case EbtFloat:
2795 resultArray[i].setBConst(unionArrays[0][i].getFConst() <
2796 unionArrays[1][i].getFConst());
2797 break;
2798 case EbtInt:
2799 resultArray[i].setBConst(unionArrays[0][i].getIConst() <
2800 unionArrays[1][i].getIConst());
2801 break;
2802 case EbtUInt:
2803 resultArray[i].setBConst(unionArrays[0][i].getUConst() <
2804 unionArrays[1][i].getUConst());
2805 break;
2806 default:
2807 UNREACHABLE();
2808 break;
2809 }
2810 }
2811 break;
2812 }
2813
2814 case EOpLessThanEqualComponentWise:
2815 {
2816 resultArray = new TConstantUnion[maxObjectSize];
2817 for (size_t i = 0; i < maxObjectSize; i++)
2818 {
2819 switch (basicType)
2820 {
2821 case EbtFloat:
2822 resultArray[i].setBConst(unionArrays[0][i].getFConst() <=
2823 unionArrays[1][i].getFConst());
2824 break;
2825 case EbtInt:
2826 resultArray[i].setBConst(unionArrays[0][i].getIConst() <=
2827 unionArrays[1][i].getIConst());
2828 break;
2829 case EbtUInt:
2830 resultArray[i].setBConst(unionArrays[0][i].getUConst() <=
2831 unionArrays[1][i].getUConst());
2832 break;
2833 default:
2834 UNREACHABLE();
2835 break;
2836 }
2837 }
2838 break;
2839 }
2840
2841 case EOpGreaterThanComponentWise:
2842 {
2843 resultArray = new TConstantUnion[maxObjectSize];
2844 for (size_t i = 0; i < maxObjectSize; i++)
2845 {
2846 switch (basicType)
2847 {
2848 case EbtFloat:
2849 resultArray[i].setBConst(unionArrays[0][i].getFConst() >
2850 unionArrays[1][i].getFConst());
2851 break;
2852 case EbtInt:
2853 resultArray[i].setBConst(unionArrays[0][i].getIConst() >
2854 unionArrays[1][i].getIConst());
2855 break;
2856 case EbtUInt:
2857 resultArray[i].setBConst(unionArrays[0][i].getUConst() >
2858 unionArrays[1][i].getUConst());
2859 break;
2860 default:
2861 UNREACHABLE();
2862 break;
2863 }
2864 }
2865 break;
2866 }
2867 case EOpGreaterThanEqualComponentWise:
2868 {
2869 resultArray = new TConstantUnion[maxObjectSize];
2870 for (size_t i = 0; i < maxObjectSize; i++)
2871 {
2872 switch (basicType)
2873 {
2874 case EbtFloat:
2875 resultArray[i].setBConst(unionArrays[0][i].getFConst() >=
2876 unionArrays[1][i].getFConst());
2877 break;
2878 case EbtInt:
2879 resultArray[i].setBConst(unionArrays[0][i].getIConst() >=
2880 unionArrays[1][i].getIConst());
2881 break;
2882 case EbtUInt:
2883 resultArray[i].setBConst(unionArrays[0][i].getUConst() >=
2884 unionArrays[1][i].getUConst());
2885 break;
2886 default:
2887 UNREACHABLE();
2888 break;
2889 }
2890 }
2891 }
2892 break;
2893
2894 case EOpEqualComponentWise:
2895 {
2896 resultArray = new TConstantUnion[maxObjectSize];
2897 for (size_t i = 0; i < maxObjectSize; i++)
2898 {
2899 switch (basicType)
2900 {
2901 case EbtFloat:
2902 resultArray[i].setBConst(unionArrays[0][i].getFConst() ==
2903 unionArrays[1][i].getFConst());
2904 break;
2905 case EbtInt:
2906 resultArray[i].setBConst(unionArrays[0][i].getIConst() ==
2907 unionArrays[1][i].getIConst());
2908 break;
2909 case EbtUInt:
2910 resultArray[i].setBConst(unionArrays[0][i].getUConst() ==
2911 unionArrays[1][i].getUConst());
2912 break;
2913 case EbtBool:
2914 resultArray[i].setBConst(unionArrays[0][i].getBConst() ==
2915 unionArrays[1][i].getBConst());
2916 break;
2917 default:
2918 UNREACHABLE();
2919 break;
2920 }
2921 }
2922 break;
2923 }
2924
2925 case EOpNotEqualComponentWise:
2926 {
2927 resultArray = new TConstantUnion[maxObjectSize];
2928 for (size_t i = 0; i < maxObjectSize; i++)
2929 {
2930 switch (basicType)
2931 {
2932 case EbtFloat:
2933 resultArray[i].setBConst(unionArrays[0][i].getFConst() !=
2934 unionArrays[1][i].getFConst());
2935 break;
2936 case EbtInt:
2937 resultArray[i].setBConst(unionArrays[0][i].getIConst() !=
2938 unionArrays[1][i].getIConst());
2939 break;
2940 case EbtUInt:
2941 resultArray[i].setBConst(unionArrays[0][i].getUConst() !=
2942 unionArrays[1][i].getUConst());
2943 break;
2944 case EbtBool:
2945 resultArray[i].setBConst(unionArrays[0][i].getBConst() !=
2946 unionArrays[1][i].getBConst());
2947 break;
2948 default:
2949 UNREACHABLE();
2950 break;
2951 }
2952 }
2953 break;
2954 }
2955
2956 case EOpDistance:
2957 {
2958 ASSERT(basicType == EbtFloat);
2959 TConstantUnion *distanceArray = new TConstantUnion[maxObjectSize];
2960 resultArray = new TConstantUnion();
2961 for (size_t i = 0; i < maxObjectSize; i++)
2962 {
2963 float x = unionArrays[0][i].getFConst();
2964 float y = unionArrays[1][i].getFConst();
2965 distanceArray[i].setFConst(x - y);
2966 }
2967 resultArray->setFConst(VectorLength(distanceArray, maxObjectSize));
2968 break;
2969 }
2970
2971 case EOpDot:
2972 ASSERT(basicType == EbtFloat);
2973 resultArray = new TConstantUnion();
2974 resultArray->setFConst(VectorDotProduct(unionArrays[0], unionArrays[1], maxObjectSize));
2975 break;
2976
2977 case EOpCross:
2978 {
2979 ASSERT(basicType == EbtFloat && maxObjectSize == 3);
2980 resultArray = new TConstantUnion[maxObjectSize];
2981 float x0 = unionArrays[0][0].getFConst();
2982 float x1 = unionArrays[0][1].getFConst();
2983 float x2 = unionArrays[0][2].getFConst();
2984 float y0 = unionArrays[1][0].getFConst();
2985 float y1 = unionArrays[1][1].getFConst();
2986 float y2 = unionArrays[1][2].getFConst();
2987 resultArray[0].setFConst(x1 * y2 - y1 * x2);
2988 resultArray[1].setFConst(x2 * y0 - y2 * x0);
2989 resultArray[2].setFConst(x0 * y1 - y0 * x1);
2990 break;
2991 }
2992
2993 case EOpReflect:
2994 {
2995 ASSERT(basicType == EbtFloat);
2996 // genType reflect (genType I, genType N) :
2997 // For the incident vector I and surface orientation N, returns the reflection
2998 // direction:
2999 // I - 2 * dot(N, I) * N.
3000 resultArray = new TConstantUnion[maxObjectSize];
3001 float dotProduct = VectorDotProduct(unionArrays[1], unionArrays[0], maxObjectSize);
3002 for (size_t i = 0; i < maxObjectSize; i++)
3003 {
3004 float result = unionArrays[0][i].getFConst() -
3005 2.0f * dotProduct * unionArrays[1][i].getFConst();
3006 resultArray[i].setFConst(result);
3007 }
3008 break;
3009 }
3010
3011 case EOpMulMatrixComponentWise:
3012 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003013 ASSERT(basicType == EbtFloat && (*arguments)[0]->getAsTyped()->isMatrix() &&
3014 (*arguments)[1]->getAsTyped()->isMatrix());
Olli Etuaho51182ab2017-01-22 00:12:29 +00003015 // Perform component-wise matrix multiplication.
3016 resultArray = new TConstantUnion[maxObjectSize];
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003017 int size = (*arguments)[0]->getAsTyped()->getNominalSize();
Olli Etuaho51182ab2017-01-22 00:12:29 +00003018 angle::Matrix<float> result =
3019 GetMatrix(unionArrays[0], size).compMult(GetMatrix(unionArrays[1], size));
3020 SetUnionArrayFromMatrix(result, resultArray);
3021 break;
3022 }
3023
3024 case EOpOuterProduct:
3025 {
3026 ASSERT(basicType == EbtFloat);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003027 size_t numRows = (*arguments)[0]->getAsTyped()->getType().getObjectSize();
3028 size_t numCols = (*arguments)[1]->getAsTyped()->getType().getObjectSize();
Olli Etuaho51182ab2017-01-22 00:12:29 +00003029 resultArray = new TConstantUnion[numRows * numCols];
3030 angle::Matrix<float> result =
3031 GetMatrix(unionArrays[0], static_cast<int>(numRows), 1)
3032 .outerProduct(GetMatrix(unionArrays[1], 1, static_cast<int>(numCols)));
3033 SetUnionArrayFromMatrix(result, resultArray);
3034 break;
3035 }
3036
3037 case EOpClamp:
3038 {
3039 resultArray = new TConstantUnion[maxObjectSize];
3040 for (size_t i = 0; i < maxObjectSize; i++)
3041 {
3042 switch (basicType)
3043 {
3044 case EbtFloat:
3045 {
3046 float x = unionArrays[0][i].getFConst();
3047 float min = unionArrays[1][i].getFConst();
3048 float max = unionArrays[2][i].getFConst();
3049 // Results are undefined if min > max.
3050 if (min > max)
3051 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
3052 &resultArray[i]);
3053 else
3054 resultArray[i].setFConst(gl::clamp(x, min, max));
3055 break;
3056 }
3057
3058 case EbtInt:
3059 {
3060 int x = unionArrays[0][i].getIConst();
3061 int min = unionArrays[1][i].getIConst();
3062 int max = unionArrays[2][i].getIConst();
3063 // Results are undefined if min > max.
3064 if (min > max)
3065 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
3066 &resultArray[i]);
3067 else
3068 resultArray[i].setIConst(gl::clamp(x, min, max));
3069 break;
3070 }
3071 case EbtUInt:
3072 {
3073 unsigned int x = unionArrays[0][i].getUConst();
3074 unsigned int min = unionArrays[1][i].getUConst();
3075 unsigned int max = unionArrays[2][i].getUConst();
3076 // Results are undefined if min > max.
3077 if (min > max)
3078 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
3079 &resultArray[i]);
3080 else
3081 resultArray[i].setUConst(gl::clamp(x, min, max));
3082 break;
3083 }
3084 default:
3085 UNREACHABLE();
3086 break;
3087 }
3088 }
3089 break;
3090 }
3091
3092 case EOpMix:
3093 {
3094 ASSERT(basicType == EbtFloat);
3095 resultArray = new TConstantUnion[maxObjectSize];
3096 for (size_t i = 0; i < maxObjectSize; i++)
3097 {
3098 float x = unionArrays[0][i].getFConst();
3099 float y = unionArrays[1][i].getFConst();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003100 TBasicType type = (*arguments)[2]->getAsTyped()->getType().getBasicType();
Olli Etuaho51182ab2017-01-22 00:12:29 +00003101 if (type == EbtFloat)
3102 {
3103 // Returns the linear blend of x and y, i.e., x * (1 - a) + y * a.
3104 float a = unionArrays[2][i].getFConst();
3105 resultArray[i].setFConst(x * (1.0f - a) + y * a);
3106 }
3107 else // 3rd parameter is EbtBool
3108 {
3109 ASSERT(type == EbtBool);
3110 // Selects which vector each returned component comes from.
3111 // For a component of a that is false, the corresponding component of x is
3112 // returned.
3113 // For a component of a that is true, the corresponding component of y is
3114 // returned.
3115 bool a = unionArrays[2][i].getBConst();
3116 resultArray[i].setFConst(a ? y : x);
3117 }
3118 }
3119 break;
3120 }
3121
3122 case EOpSmoothStep:
3123 {
3124 ASSERT(basicType == EbtFloat);
3125 resultArray = new TConstantUnion[maxObjectSize];
3126 for (size_t i = 0; i < maxObjectSize; i++)
3127 {
3128 float edge0 = unionArrays[0][i].getFConst();
3129 float edge1 = unionArrays[1][i].getFConst();
3130 float x = unionArrays[2][i].getFConst();
3131 // Results are undefined if edge0 >= edge1.
3132 if (edge0 >= edge1)
3133 {
3134 UndefinedConstantFoldingError(loc, op, basicType, diagnostics, &resultArray[i]);
3135 }
3136 else
3137 {
3138 // Returns 0.0 if x <= edge0 and 1.0 if x >= edge1 and performs smooth
3139 // Hermite interpolation between 0 and 1 when edge0 < x < edge1.
3140 float t = gl::clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
3141 resultArray[i].setFConst(t * t * (3.0f - 2.0f * t));
3142 }
3143 }
3144 break;
3145 }
3146
Olli Etuaho74da73f2017-02-01 15:37:48 +00003147 case EOpLdexp:
3148 {
3149 resultArray = new TConstantUnion[maxObjectSize];
3150 for (size_t i = 0; i < maxObjectSize; i++)
3151 {
3152 float x = unionArrays[0][i].getFConst();
3153 int exp = unionArrays[1][i].getIConst();
3154 if (exp > 128)
3155 {
3156 UndefinedConstantFoldingError(loc, op, basicType, diagnostics, &resultArray[i]);
3157 }
3158 else
3159 {
3160 resultArray[i].setFConst(gl::Ldexp(x, exp));
3161 }
3162 }
3163 break;
3164 }
3165
Olli Etuaho51182ab2017-01-22 00:12:29 +00003166 case EOpFaceForward:
3167 {
3168 ASSERT(basicType == EbtFloat);
3169 // genType faceforward(genType N, genType I, genType Nref) :
3170 // If dot(Nref, I) < 0 return N, otherwise return -N.
3171 resultArray = new TConstantUnion[maxObjectSize];
3172 float dotProduct = VectorDotProduct(unionArrays[2], unionArrays[1], maxObjectSize);
3173 for (size_t i = 0; i < maxObjectSize; i++)
3174 {
3175 if (dotProduct < 0)
3176 resultArray[i].setFConst(unionArrays[0][i].getFConst());
3177 else
3178 resultArray[i].setFConst(-unionArrays[0][i].getFConst());
3179 }
3180 break;
3181 }
3182
3183 case EOpRefract:
3184 {
3185 ASSERT(basicType == EbtFloat);
3186 // genType refract(genType I, genType N, float eta) :
3187 // For the incident vector I and surface normal N, and the ratio of indices of
3188 // refraction eta,
3189 // return the refraction vector. The result is computed by
3190 // k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I))
3191 // if (k < 0.0)
3192 // return genType(0.0)
3193 // else
3194 // return eta * I - (eta * dot(N, I) + sqrt(k)) * N
3195 resultArray = new TConstantUnion[maxObjectSize];
3196 float dotProduct = VectorDotProduct(unionArrays[1], unionArrays[0], maxObjectSize);
3197 for (size_t i = 0; i < maxObjectSize; i++)
3198 {
3199 float eta = unionArrays[2][i].getFConst();
3200 float k = 1.0f - eta * eta * (1.0f - dotProduct * dotProduct);
3201 if (k < 0.0f)
3202 resultArray[i].setFConst(0.0f);
3203 else
3204 resultArray[i].setFConst(eta * unionArrays[0][i].getFConst() -
3205 (eta * dotProduct + sqrtf(k)) *
3206 unionArrays[1][i].getFConst());
3207 }
3208 break;
3209 }
Olli Etuaho9250cb22017-01-21 10:51:27 +00003210 case EOpBitfieldExtract:
3211 {
3212 resultArray = new TConstantUnion[maxObjectSize];
3213 for (size_t i = 0; i < maxObjectSize; ++i)
3214 {
3215 int offset = unionArrays[1][0].getIConst();
3216 int bits = unionArrays[2][0].getIConst();
3217 if (bits == 0)
3218 {
3219 if (aggregate->getBasicType() == EbtInt)
3220 {
3221 resultArray[i].setIConst(0);
3222 }
3223 else
3224 {
3225 ASSERT(aggregate->getBasicType() == EbtUInt);
3226 resultArray[i].setUConst(0);
3227 }
3228 }
3229 else if (offset < 0 || bits < 0 || offset >= 32 || bits > 32 || offset + bits > 32)
3230 {
3231 UndefinedConstantFoldingError(loc, op, aggregate->getBasicType(), diagnostics,
3232 &resultArray[i]);
3233 }
3234 else
3235 {
3236 // bits can be 32 here, so we need to avoid bit shift overflow.
3237 uint32_t maskMsb = 1u << (bits - 1);
3238 uint32_t mask = ((maskMsb - 1u) | maskMsb) << offset;
3239 if (aggregate->getBasicType() == EbtInt)
3240 {
3241 uint32_t value = static_cast<uint32_t>(unionArrays[0][i].getIConst());
3242 uint32_t resultUnsigned = (value & mask) >> offset;
3243 if ((resultUnsigned & maskMsb) != 0)
3244 {
3245 // The most significant bits (from bits+1 to the most significant bit)
3246 // should be set to 1.
3247 uint32_t higherBitsMask = ((1u << (32 - bits)) - 1u) << bits;
3248 resultUnsigned |= higherBitsMask;
3249 }
3250 resultArray[i].setIConst(static_cast<int32_t>(resultUnsigned));
3251 }
3252 else
3253 {
3254 ASSERT(aggregate->getBasicType() == EbtUInt);
3255 uint32_t value = unionArrays[0][i].getUConst();
3256 resultArray[i].setUConst((value & mask) >> offset);
3257 }
3258 }
3259 }
3260 break;
3261 }
3262 case EOpBitfieldInsert:
3263 {
3264 resultArray = new TConstantUnion[maxObjectSize];
3265 for (size_t i = 0; i < maxObjectSize; ++i)
3266 {
3267 int offset = unionArrays[2][0].getIConst();
3268 int bits = unionArrays[3][0].getIConst();
3269 if (bits == 0)
3270 {
3271 if (aggregate->getBasicType() == EbtInt)
3272 {
3273 int32_t base = unionArrays[0][i].getIConst();
3274 resultArray[i].setIConst(base);
3275 }
3276 else
3277 {
3278 ASSERT(aggregate->getBasicType() == EbtUInt);
3279 uint32_t base = unionArrays[0][i].getUConst();
3280 resultArray[i].setUConst(base);
3281 }
3282 }
3283 else if (offset < 0 || bits < 0 || offset >= 32 || bits > 32 || offset + bits > 32)
3284 {
3285 UndefinedConstantFoldingError(loc, op, aggregate->getBasicType(), diagnostics,
3286 &resultArray[i]);
3287 }
3288 else
3289 {
3290 // bits can be 32 here, so we need to avoid bit shift overflow.
3291 uint32_t maskMsb = 1u << (bits - 1);
3292 uint32_t insertMask = ((maskMsb - 1u) | maskMsb) << offset;
3293 uint32_t baseMask = ~insertMask;
3294 if (aggregate->getBasicType() == EbtInt)
3295 {
3296 uint32_t base = static_cast<uint32_t>(unionArrays[0][i].getIConst());
3297 uint32_t insert = static_cast<uint32_t>(unionArrays[1][i].getIConst());
3298 uint32_t resultUnsigned =
3299 (base & baseMask) | ((insert << offset) & insertMask);
3300 resultArray[i].setIConst(static_cast<int32_t>(resultUnsigned));
3301 }
3302 else
3303 {
3304 ASSERT(aggregate->getBasicType() == EbtUInt);
3305 uint32_t base = unionArrays[0][i].getUConst();
3306 uint32_t insert = unionArrays[1][i].getUConst();
3307 resultArray[i].setUConst((base & baseMask) |
3308 ((insert << offset) & insertMask));
3309 }
3310 }
3311 }
3312 break;
3313 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00003314
3315 default:
3316 UNREACHABLE();
3317 return nullptr;
Arun Patole274f0702015-05-05 13:33:30 +05303318 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003319 return resultArray;
Arun Patole274f0702015-05-05 13:33:30 +05303320}
3321
3322// static
Jamie Madillb1a85f42014-08-19 15:23:24 -04003323TString TIntermTraverser::hash(const TString &name, ShHashFunction64 hashFunction)
3324{
3325 if (hashFunction == NULL || name.empty())
3326 return name;
3327 khronos_uint64_t number = (*hashFunction)(name.c_str(), name.length());
3328 TStringStream stream;
3329 stream << HASHED_NAME_PREFIX << std::hex << number;
3330 TString hashedName = stream.str();
3331 return hashedName;
3332}
Olli Etuaho853dc1a2014-11-06 17:25:48 +02003333
3334void TIntermTraverser::updateTree()
3335{
Olli Etuahoa6f22092015-05-08 18:31:10 +03003336 for (size_t ii = 0; ii < mInsertions.size(); ++ii)
3337 {
3338 const NodeInsertMultipleEntry &insertion = mInsertions[ii];
3339 ASSERT(insertion.parent);
Olli Etuaho5d91dda2015-06-18 15:47:46 +03003340 if (!insertion.insertionsAfter.empty())
3341 {
3342 bool inserted = insertion.parent->insertChildNodes(insertion.position + 1,
3343 insertion.insertionsAfter);
3344 ASSERT(inserted);
Olli Etuaho5d91dda2015-06-18 15:47:46 +03003345 }
3346 if (!insertion.insertionsBefore.empty())
3347 {
3348 bool inserted =
3349 insertion.parent->insertChildNodes(insertion.position, insertion.insertionsBefore);
3350 ASSERT(inserted);
Olli Etuaho5d91dda2015-06-18 15:47:46 +03003351 }
Olli Etuahoa6f22092015-05-08 18:31:10 +03003352 }
Olli Etuaho853dc1a2014-11-06 17:25:48 +02003353 for (size_t ii = 0; ii < mReplacements.size(); ++ii)
3354 {
Olli Etuahocd94ef92015-04-16 19:18:10 +03003355 const NodeUpdateEntry &replacement = mReplacements[ii];
3356 ASSERT(replacement.parent);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003357 bool replaced =
3358 replacement.parent->replaceChildNode(replacement.original, replacement.replacement);
Olli Etuaho853dc1a2014-11-06 17:25:48 +02003359 ASSERT(replaced);
3360
Olli Etuahocd94ef92015-04-16 19:18:10 +03003361 if (!replacement.originalBecomesChildOfReplacement)
Olli Etuaho853dc1a2014-11-06 17:25:48 +02003362 {
3363 // In AST traversing, a parent is visited before its children.
Olli Etuahocd94ef92015-04-16 19:18:10 +03003364 // After we replace a node, if its immediate child is to
Olli Etuaho853dc1a2014-11-06 17:25:48 +02003365 // be replaced, we need to make sure we don't update the replaced
3366 // node; instead, we update the replacement node.
3367 for (size_t jj = ii + 1; jj < mReplacements.size(); ++jj)
3368 {
Olli Etuahocd94ef92015-04-16 19:18:10 +03003369 NodeUpdateEntry &replacement2 = mReplacements[jj];
3370 if (replacement2.parent == replacement.original)
3371 replacement2.parent = replacement.replacement;
Olli Etuaho853dc1a2014-11-06 17:25:48 +02003372 }
3373 }
3374 }
Olli Etuahofc0e2bc2015-04-16 13:39:56 +03003375 for (size_t ii = 0; ii < mMultiReplacements.size(); ++ii)
3376 {
3377 const NodeReplaceWithMultipleEntry &replacement = mMultiReplacements[ii];
3378 ASSERT(replacement.parent);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003379 bool replaced = replacement.parent->replaceChildNodeWithMultiple(replacement.original,
3380 replacement.replacements);
Olli Etuahofc0e2bc2015-04-16 13:39:56 +03003381 ASSERT(replaced);
3382 }
Olli Etuahod4f303e2015-05-20 17:09:06 +03003383
Jamie Madill03d863c2016-07-27 18:15:53 -04003384 clearReplacementQueue();
3385}
3386
3387void TIntermTraverser::clearReplacementQueue()
3388{
Olli Etuahod4f303e2015-05-20 17:09:06 +03003389 mReplacements.clear();
3390 mMultiReplacements.clear();
Jamie Madill03d863c2016-07-27 18:15:53 -04003391 mInsertions.clear();
Olli Etuaho853dc1a2014-11-06 17:25:48 +02003392}
Jamie Madill1048e432016-07-23 18:51:28 -04003393
Jamie Madill03d863c2016-07-27 18:15:53 -04003394void TIntermTraverser::queueReplacement(TIntermNode *original,
3395 TIntermNode *replacement,
3396 OriginalNode originalStatus)
Jamie Madill1048e432016-07-23 18:51:28 -04003397{
Jamie Madill03d863c2016-07-27 18:15:53 -04003398 queueReplacementWithParent(getParentNode(), original, replacement, originalStatus);
Jamie Madill1048e432016-07-23 18:51:28 -04003399}
3400
Jamie Madill03d863c2016-07-27 18:15:53 -04003401void TIntermTraverser::queueReplacementWithParent(TIntermNode *parent,
3402 TIntermNode *original,
3403 TIntermNode *replacement,
3404 OriginalNode originalStatus)
Jamie Madill1048e432016-07-23 18:51:28 -04003405{
Jamie Madill03d863c2016-07-27 18:15:53 -04003406 bool originalBecomesChild = (originalStatus == OriginalNode::BECOMES_CHILD);
3407 mReplacements.push_back(NodeUpdateEntry(parent, original, replacement, originalBecomesChild));
Jamie Madill1048e432016-07-23 18:51:28 -04003408}
Jamie Madill45bcc782016-11-07 13:58:48 -05003409
Olli Etuahofe486322017-03-21 09:30:54 +00003410TName TIntermTraverser::GetInternalFunctionName(const char *name)
3411{
3412 TString nameStr(name);
Olli Etuahofe486322017-03-21 09:30:54 +00003413 TName nameObj(nameStr);
3414 nameObj.setInternal(true);
3415 return nameObj;
3416}
3417
3418TIntermFunctionPrototype *TIntermTraverser::CreateInternalFunctionPrototypeNode(
3419 const TType &returnType,
3420 const char *name,
3421 const TSymbolUniqueId &functionId)
3422{
3423 TIntermFunctionPrototype *functionNode = new TIntermFunctionPrototype(returnType, functionId);
3424 functionNode->getFunctionSymbolInfo()->setNameObj(GetInternalFunctionName(name));
3425 return functionNode;
3426}
3427
3428TIntermFunctionDefinition *TIntermTraverser::CreateInternalFunctionDefinitionNode(
3429 const TType &returnType,
3430 const char *name,
3431 TIntermBlock *functionBody,
3432 const TSymbolUniqueId &functionId)
3433{
3434 TIntermFunctionPrototype *prototypeNode =
3435 CreateInternalFunctionPrototypeNode(returnType, name, functionId);
3436 return new TIntermFunctionDefinition(prototypeNode, functionBody);
3437}
3438
3439TIntermAggregate *TIntermTraverser::CreateInternalFunctionCallNode(
3440 const TType &returnType,
3441 const char *name,
3442 const TSymbolUniqueId &functionId,
3443 TIntermSequence *arguments)
3444{
3445 TIntermAggregate *functionNode = TIntermAggregate::CreateFunctionCall(
3446 returnType, functionId, GetInternalFunctionName(name), arguments);
3447 return functionNode;
3448}
3449
Jamie Madill45bcc782016-11-07 13:58:48 -05003450} // namespace sh