blob: 4c6db975ca2b6e40db54f9d52b383cd0a56064e9 [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/IntermNode.h"
22#include "compiler/translator/SymbolTable.h"
Corentin Wallez509e4562016-08-25 14:55:44 -040023#include "compiler/translator/util.h"
Jamie Madillb1a85f42014-08-19 15:23:24 -040024
Jamie Madill45bcc782016-11-07 13:58:48 -050025namespace sh
26{
27
Jamie Madillb1a85f42014-08-19 15:23:24 -040028namespace
29{
30
Jamie Madilld7b1ab52016-12-12 14:42:19 -050031const float kPi = 3.14159265358979323846f;
Arun Patole9dea48f2015-04-02 11:45:09 +053032const float kDegreesToRadiansMultiplier = kPi / 180.0f;
33const float kRadiansToDegreesMultiplier = 180.0f / kPi;
34
Jamie Madillb1a85f42014-08-19 15:23:24 -040035TPrecision GetHigherPrecision(TPrecision left, TPrecision right)
36{
37 return left > right ? left : right;
38}
39
Arun Patole274f0702015-05-05 13:33:30 +053040TConstantUnion *Vectorize(const TConstantUnion &constant, size_t size)
41{
42 TConstantUnion *constUnion = new TConstantUnion[size];
43 for (unsigned int i = 0; i < size; ++i)
Jamie Madilld7b1ab52016-12-12 14:42:19 -050044 constUnion[i] = constant;
Arun Patole274f0702015-05-05 13:33:30 +053045
46 return constUnion;
47}
48
Olli Etuahof119a262016-08-19 15:54:22 +030049void UndefinedConstantFoldingError(const TSourceLoc &loc,
50 TOperator op,
51 TBasicType basicType,
52 TDiagnostics *diagnostics,
53 TConstantUnion *result)
Arun Patolebf790422015-05-18 17:53:04 +053054{
Olli Etuahof119a262016-08-19 15:54:22 +030055 diagnostics->warning(loc, "operation result is undefined for the values passed in",
Olli Etuaho4de340a2016-12-16 09:32:03 +000056 GetOperatorString(op));
Arun Patolebf790422015-05-18 17:53:04 +053057
58 switch (basicType)
59 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -050060 case EbtFloat:
61 result->setFConst(0.0f);
62 break;
63 case EbtInt:
64 result->setIConst(0);
65 break;
66 case EbtUInt:
67 result->setUConst(0u);
68 break;
69 case EbtBool:
70 result->setBConst(false);
71 break;
72 default:
73 break;
Arun Patolebf790422015-05-18 17:53:04 +053074 }
75}
76
Olli Etuaho5c0e0232015-11-11 15:55:59 +020077float VectorLength(const TConstantUnion *paramArray, size_t paramArraySize)
Arun Patole1155ddd2015-06-05 18:04:36 +053078{
79 float result = 0.0f;
80 for (size_t i = 0; i < paramArraySize; i++)
81 {
82 float f = paramArray[i].getFConst();
83 result += f * f;
84 }
85 return sqrtf(result);
86}
87
Olli Etuaho5c0e0232015-11-11 15:55:59 +020088float VectorDotProduct(const TConstantUnion *paramArray1,
89 const TConstantUnion *paramArray2,
90 size_t paramArraySize)
Arun Patole1155ddd2015-06-05 18:04:36 +053091{
92 float result = 0.0f;
93 for (size_t i = 0; i < paramArraySize; i++)
94 result += paramArray1[i].getFConst() * paramArray2[i].getFConst();
95 return result;
96}
97
Olli Etuaho3272a6d2016-08-29 17:54:50 +030098TIntermTyped *CreateFoldedNode(const TConstantUnion *constArray,
Olli Etuaho7c3848e2015-11-04 13:19:17 +020099 const TIntermTyped *originalNode,
100 TQualifier qualifier)
Olli Etuahob43846e2015-06-02 18:18:57 +0300101{
102 if (constArray == nullptr)
103 {
104 return nullptr;
105 }
106 TIntermTyped *folded = new TIntermConstantUnion(constArray, originalNode->getType());
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200107 folded->getTypePointer()->setQualifier(qualifier);
Olli Etuahob43846e2015-06-02 18:18:57 +0300108 folded->setLine(originalNode->getLine());
109 return folded;
110}
111
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200112angle::Matrix<float> GetMatrix(const TConstantUnion *paramArray,
113 const unsigned int &rows,
114 const unsigned int &cols)
Arun Patole7fa33552015-06-10 15:15:18 +0530115{
116 std::vector<float> elements;
117 for (size_t i = 0; i < rows * cols; i++)
118 elements.push_back(paramArray[i].getFConst());
119 // Transpose is used since the Matrix constructor expects arguments in row-major order,
Olli Etuahod5da5052016-08-29 13:16:55 +0300120 // whereas the paramArray is in column-major order. Rows/cols parameters are also flipped below
121 // so that the created matrix will have the expected dimensions after the transpose.
122 return angle::Matrix<float>(elements, cols, rows).transpose();
Arun Patole7fa33552015-06-10 15:15:18 +0530123}
124
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200125angle::Matrix<float> GetMatrix(const TConstantUnion *paramArray, const unsigned int &size)
Arun Patole7fa33552015-06-10 15:15:18 +0530126{
127 std::vector<float> elements;
128 for (size_t i = 0; i < size * size; i++)
129 elements.push_back(paramArray[i].getFConst());
130 // Transpose is used since the Matrix constructor expects arguments in row-major order,
131 // whereas the paramArray is in column-major order.
132 return angle::Matrix<float>(elements, size).transpose();
133}
134
135void SetUnionArrayFromMatrix(const angle::Matrix<float> &m, TConstantUnion *resultArray)
136{
137 // Transpose is used since the input Matrix is in row-major order,
138 // whereas the actual result should be in column-major order.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500139 angle::Matrix<float> result = m.transpose();
Arun Patole7fa33552015-06-10 15:15:18 +0530140 std::vector<float> resultElements = result.elements();
141 for (size_t i = 0; i < resultElements.size(); i++)
142 resultArray[i].setFConst(resultElements[i]);
143}
144
Jamie Madillb1a85f42014-08-19 15:23:24 -0400145} // namespace anonymous
146
Jamie Madillb1a85f42014-08-19 15:23:24 -0400147////////////////////////////////////////////////////////////////
148//
149// Member functions of the nodes used for building the tree.
150//
151////////////////////////////////////////////////////////////////
152
Olli Etuahod2a67b92014-10-21 16:42:57 +0300153void TIntermTyped::setTypePreservePrecision(const TType &t)
154{
155 TPrecision precision = getPrecision();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500156 mType = t;
Olli Etuahod2a67b92014-10-21 16:42:57 +0300157 ASSERT(mType.getBasicType() != EbtBool || precision == EbpUndefined);
158 mType.setPrecision(precision);
159}
160
Jamie Madillb1a85f42014-08-19 15:23:24 -0400161#define REPLACE_IF_IS(node, type, original, replacement) \
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500162 if (node == original) \
163 { \
164 node = static_cast<type *>(replacement); \
165 return true; \
Jamie Madillb1a85f42014-08-19 15:23:24 -0400166 }
167
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500168bool TIntermLoop::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400169{
Olli Etuaho3cbb27a2016-07-14 11:55:48 +0300170 ASSERT(original != nullptr); // This risks replacing multiple children.
Jamie Madillb1a85f42014-08-19 15:23:24 -0400171 REPLACE_IF_IS(mInit, TIntermNode, original, replacement);
172 REPLACE_IF_IS(mCond, TIntermTyped, original, replacement);
173 REPLACE_IF_IS(mExpr, TIntermTyped, original, replacement);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100174 REPLACE_IF_IS(mBody, TIntermBlock, original, replacement);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400175 return false;
176}
177
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500178bool TIntermBranch::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400179{
180 REPLACE_IF_IS(mExpression, TIntermTyped, original, replacement);
181 return false;
182}
183
Olli Etuahob6fa0432016-09-28 16:28:05 +0100184bool TIntermSwizzle::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
185{
186 ASSERT(original->getAsTyped()->getType() == replacement->getAsTyped()->getType());
187 REPLACE_IF_IS(mOperand, TIntermTyped, original, replacement);
188 return false;
189}
190
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500191bool TIntermBinary::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400192{
193 REPLACE_IF_IS(mLeft, TIntermTyped, original, replacement);
194 REPLACE_IF_IS(mRight, TIntermTyped, original, replacement);
195 return false;
196}
197
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500198bool TIntermUnary::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400199{
Olli Etuahoa2234302016-08-31 12:05:39 +0300200 ASSERT(original->getAsTyped()->getType() == replacement->getAsTyped()->getType());
Jamie Madillb1a85f42014-08-19 15:23:24 -0400201 REPLACE_IF_IS(mOperand, TIntermTyped, original, replacement);
202 return false;
203}
204
Olli Etuahobf4e1b72016-12-09 11:30:15 +0000205bool TIntermInvariantDeclaration::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
206{
207 REPLACE_IF_IS(mSymbol, TIntermSymbol, original, replacement);
208 return false;
209}
210
Olli Etuaho336b1472016-10-05 16:37:55 +0100211bool TIntermFunctionDefinition::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
212{
Olli Etuaho8ad9e752017-01-16 19:55:20 +0000213 REPLACE_IF_IS(mPrototype, TIntermFunctionPrototype, original, replacement);
Olli Etuaho336b1472016-10-05 16:37:55 +0100214 REPLACE_IF_IS(mBody, TIntermBlock, original, replacement);
215 return false;
216}
217
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500218bool TIntermAggregate::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400219{
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100220 return replaceChildNodeInternal(original, replacement);
221}
222
223bool TIntermBlock::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
224{
225 return replaceChildNodeInternal(original, replacement);
226}
227
Olli Etuaho16c745a2017-01-16 17:02:27 +0000228bool TIntermFunctionPrototype::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
229{
230 return replaceChildNodeInternal(original, replacement);
231}
232
Olli Etuaho13389b62016-10-16 11:48:18 +0100233bool TIntermDeclaration::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
234{
235 return replaceChildNodeInternal(original, replacement);
236}
237
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100238bool TIntermAggregateBase::replaceChildNodeInternal(TIntermNode *original, TIntermNode *replacement)
239{
240 for (size_t ii = 0; ii < getSequence()->size(); ++ii)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400241 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100242 REPLACE_IF_IS((*getSequence())[ii], TIntermNode, original, replacement);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400243 }
244 return false;
245}
246
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100247bool TIntermAggregateBase::replaceChildNodeWithMultiple(TIntermNode *original,
248 const TIntermSequence &replacements)
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300249{
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100250 for (auto it = getSequence()->begin(); it < getSequence()->end(); ++it)
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300251 {
252 if (*it == original)
253 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100254 it = getSequence()->erase(it);
255 getSequence()->insert(it, replacements.begin(), replacements.end());
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300256 return true;
257 }
258 }
259 return false;
260}
261
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100262bool TIntermAggregateBase::insertChildNodes(TIntermSequence::size_type position,
263 const TIntermSequence &insertions)
Olli Etuahoa6f22092015-05-08 18:31:10 +0300264{
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100265 if (position > getSequence()->size())
Olli Etuahoa6f22092015-05-08 18:31:10 +0300266 {
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300267 return false;
Olli Etuahoa6f22092015-05-08 18:31:10 +0300268 }
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100269 auto it = getSequence()->begin() + position;
270 getSequence()->insert(it, insertions.begin(), insertions.end());
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300271 return true;
Olli Etuahoa6f22092015-05-08 18:31:10 +0300272}
273
Olli Etuahofe486322017-03-21 09:30:54 +0000274TIntermAggregate *TIntermAggregate::CreateFunctionCall(const TFunction &func,
275 TIntermSequence *arguments)
276{
277 TIntermAggregate *callNode =
278 new TIntermAggregate(func.getReturnType(), EOpCallFunctionInAST, arguments);
279 callNode->getFunctionSymbolInfo()->setFromFunction(func);
280 return callNode;
281}
282
283TIntermAggregate *TIntermAggregate::CreateFunctionCall(const TType &type,
284 const TSymbolUniqueId &id,
285 const TName &name,
286 TIntermSequence *arguments)
287{
288 TIntermAggregate *callNode = new TIntermAggregate(type, EOpCallFunctionInAST, arguments);
289 callNode->getFunctionSymbolInfo()->setId(id);
290 callNode->getFunctionSymbolInfo()->setNameObj(name);
291 return callNode;
292}
293
294TIntermAggregate *TIntermAggregate::CreateBuiltInFunctionCall(const TFunction &func,
295 TIntermSequence *arguments)
296{
297 TIntermAggregate *callNode =
298 new TIntermAggregate(func.getReturnType(), EOpCallBuiltInFunction, arguments);
299 callNode->getFunctionSymbolInfo()->setFromFunction(func);
300 // Note that name needs to be set before texture function type is determined.
301 callNode->setBuiltInFunctionPrecision();
302 return callNode;
303}
304
305TIntermAggregate *TIntermAggregate::CreateConstructor(const TType &type,
Olli Etuahofe486322017-03-21 09:30:54 +0000306 TIntermSequence *arguments)
307{
Olli Etuaho8fab3202017-05-08 18:22:22 +0300308 return new TIntermAggregate(type, EOpConstruct, arguments);
Olli Etuahofe486322017-03-21 09:30:54 +0000309}
310
311TIntermAggregate *TIntermAggregate::Create(const TType &type,
312 TOperator op,
313 TIntermSequence *arguments)
314{
315 TIntermAggregate *node = new TIntermAggregate(type, op, arguments);
316 ASSERT(op != EOpCallFunctionInAST); // Should use CreateFunctionCall
317 ASSERT(op != EOpCallBuiltInFunction); // Should use CreateBuiltInFunctionCall
318 ASSERT(!node->isConstructor()); // Should use CreateConstructor
319 return node;
320}
321
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800322TIntermAggregate::TIntermAggregate(const TType &type, TOperator op, TIntermSequence *arguments)
323 : TIntermOperator(op), mUseEmulatedFunction(false), mGotPrecisionFromChildren(false)
324{
325 if (arguments != nullptr)
326 {
327 mArguments.swap(*arguments);
328 }
329 setTypePrecisionAndQualifier(type);
330}
331
332void TIntermAggregate::setTypePrecisionAndQualifier(const TType &type)
333{
334 setType(type);
335 mType.setQualifier(EvqTemporary);
336 if (!isFunctionCall())
337 {
338 if (isConstructor())
339 {
340 // Structs should not be precision qualified, the individual members may be.
341 // Built-in types on the other hand should be precision qualified.
Olli Etuaho8fab3202017-05-08 18:22:22 +0300342 if (getBasicType() != EbtStruct)
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800343 {
344 setPrecisionFromChildren();
345 }
346 }
347 else
348 {
349 setPrecisionForBuiltInOp();
350 }
351 if (areChildrenConstQualified())
352 {
353 mType.setQualifier(EvqConst);
354 }
355 }
356}
357
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200358bool TIntermAggregate::areChildrenConstQualified()
359{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800360 for (TIntermNode *&arg : mArguments)
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200361 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800362 TIntermTyped *typedArg = arg->getAsTyped();
363 if (typedArg && typedArg->getQualifier() != EvqConst)
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200364 {
365 return false;
366 }
367 }
368 return true;
369}
370
Olli Etuahod2a67b92014-10-21 16:42:57 +0300371void TIntermAggregate::setPrecisionFromChildren()
372{
Olli Etuahoa4aa4e32015-06-04 15:54:30 +0300373 mGotPrecisionFromChildren = true;
Olli Etuahod2a67b92014-10-21 16:42:57 +0300374 if (getBasicType() == EbtBool)
375 {
376 mType.setPrecision(EbpUndefined);
377 return;
378 }
379
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500380 TPrecision precision = EbpUndefined;
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800381 TIntermSequence::iterator childIter = mArguments.begin();
382 while (childIter != mArguments.end())
Olli Etuahod2a67b92014-10-21 16:42:57 +0300383 {
384 TIntermTyped *typed = (*childIter)->getAsTyped();
385 if (typed)
386 precision = GetHigherPrecision(typed->getPrecision(), precision);
387 ++childIter;
388 }
389 mType.setPrecision(precision);
390}
391
Olli Etuaho9250cb22017-01-21 10:51:27 +0000392void TIntermAggregate::setPrecisionForBuiltInOp()
393{
394 ASSERT(!isConstructor());
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800395 ASSERT(!isFunctionCall());
Olli Etuaho9250cb22017-01-21 10:51:27 +0000396 if (!setPrecisionForSpecialBuiltInOp())
397 {
398 setPrecisionFromChildren();
399 }
400}
401
402bool TIntermAggregate::setPrecisionForSpecialBuiltInOp()
403{
404 switch (mOp)
405 {
406 case EOpBitfieldExtract:
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800407 mType.setPrecision(mArguments[0]->getAsTyped()->getPrecision());
408 mGotPrecisionFromChildren = true;
Olli Etuaho9250cb22017-01-21 10:51:27 +0000409 return true;
410 case EOpBitfieldInsert:
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800411 mType.setPrecision(GetHigherPrecision(mArguments[0]->getAsTyped()->getPrecision(),
412 mArguments[1]->getAsTyped()->getPrecision()));
413 mGotPrecisionFromChildren = true;
Olli Etuaho9250cb22017-01-21 10:51:27 +0000414 return true;
415 case EOpUaddCarry:
416 case EOpUsubBorrow:
417 mType.setPrecision(EbpHigh);
418 return true;
419 default:
420 return false;
421 }
422}
423
Olli Etuahod2a67b92014-10-21 16:42:57 +0300424void TIntermAggregate::setBuiltInFunctionPrecision()
425{
426 // All built-ins returning bool should be handled as ops, not functions.
427 ASSERT(getBasicType() != EbtBool);
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800428 ASSERT(mOp == EOpCallBuiltInFunction);
Olli Etuahod2a67b92014-10-21 16:42:57 +0300429
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800430 TPrecision precision = EbpUndefined;
431 for (TIntermNode *arg : mArguments)
Olli Etuahod2a67b92014-10-21 16:42:57 +0300432 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800433 TIntermTyped *typed = arg->getAsTyped();
Olli Etuahod2a67b92014-10-21 16:42:57 +0300434 // ESSL spec section 8: texture functions get their precision from the sampler.
435 if (typed && IsSampler(typed->getBasicType()))
436 {
437 precision = typed->getPrecision();
438 break;
439 }
Olli Etuahod2a67b92014-10-21 16:42:57 +0300440 }
441 // ESSL 3.0 spec section 8: textureSize always gets highp precision.
442 // All other functions that take a sampler are assumed to be texture functions.
Olli Etuahobd674552016-10-06 13:28:42 +0100443 if (mFunctionInfo.getName().find("textureSize") == 0)
Olli Etuahod2a67b92014-10-21 16:42:57 +0300444 mType.setPrecision(EbpHigh);
445 else
446 mType.setPrecision(precision);
447}
448
Olli Etuahof2209f72017-04-01 12:45:55 +0300449TString TIntermAggregate::getSymbolTableMangledName() const
450{
451 ASSERT(!isConstructor());
452 switch (mOp)
453 {
454 case EOpCallInternalRawFunction:
455 case EOpCallBuiltInFunction:
456 case EOpCallFunctionInAST:
457 return TFunction::GetMangledNameFromCall(mFunctionInfo.getName(), mArguments);
458 default:
459 TString opString = GetOperatorString(mOp);
460 return TFunction::GetMangledNameFromCall(opString, mArguments);
461 }
462}
463
Olli Etuahoa22aa4e2017-05-24 18:17:23 +0300464bool TIntermAggregate::hasSideEffects() const
465{
466 if (isFunctionCall() && mFunctionInfo.isKnownToNotHaveSideEffects())
467 {
468 for (TIntermNode *arg : mArguments)
469 {
470 if (arg->getAsTyped()->hasSideEffects())
471 {
472 return true;
473 }
474 }
475 return false;
476 }
477 // Conservatively assume most aggregate operators have side-effects
478 return true;
479}
480
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100481void TIntermBlock::appendStatement(TIntermNode *statement)
482{
Olli Etuaho13389b62016-10-16 11:48:18 +0100483 // Declaration nodes with no children can appear if all the declarators just added constants to
484 // the symbol table instead of generating code. They're no-ops so they aren't added to blocks.
485 if (statement != nullptr && (statement->getAsDeclarationNode() == nullptr ||
486 !statement->getAsDeclarationNode()->getSequence()->empty()))
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100487 {
488 mStatements.push_back(statement);
489 }
490}
491
Olli Etuaho16c745a2017-01-16 17:02:27 +0000492void TIntermFunctionPrototype::appendParameter(TIntermSymbol *parameter)
493{
494 ASSERT(parameter != nullptr);
495 mParameters.push_back(parameter);
496}
497
Olli Etuaho13389b62016-10-16 11:48:18 +0100498void TIntermDeclaration::appendDeclarator(TIntermTyped *declarator)
499{
500 ASSERT(declarator != nullptr);
501 ASSERT(declarator->getAsSymbolNode() != nullptr ||
502 (declarator->getAsBinaryNode() != nullptr &&
503 declarator->getAsBinaryNode()->getOp() == EOpInitialize));
504 ASSERT(mDeclarators.empty() ||
505 declarator->getType().sameElementType(mDeclarators.back()->getAsTyped()->getType()));
506 mDeclarators.push_back(declarator);
507}
508
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300509bool TIntermTernary::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
510{
511 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
512 REPLACE_IF_IS(mTrueExpression, TIntermTyped, original, replacement);
513 REPLACE_IF_IS(mFalseExpression, TIntermTyped, original, replacement);
514 return false;
515}
516
Olli Etuaho57961272016-09-14 13:57:46 +0300517bool TIntermIfElse::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400518{
519 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100520 REPLACE_IF_IS(mTrueBlock, TIntermBlock, original, replacement);
521 REPLACE_IF_IS(mFalseBlock, TIntermBlock, original, replacement);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400522 return false;
523}
524
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500525bool TIntermSwitch::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Olli Etuahoa3a36662015-02-17 13:46:51 +0200526{
527 REPLACE_IF_IS(mInit, TIntermTyped, original, replacement);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100528 REPLACE_IF_IS(mStatementList, TIntermBlock, original, replacement);
Olli Etuahoa3a36662015-02-17 13:46:51 +0200529 return false;
530}
531
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500532bool TIntermCase::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Olli Etuahoa3a36662015-02-17 13:46:51 +0200533{
534 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
535 return false;
536}
537
Olli Etuahod7a25242015-08-18 13:49:45 +0300538TIntermTyped::TIntermTyped(const TIntermTyped &node) : TIntermNode(), mType(node.mType)
539{
540 // Copy constructor is disallowed for TIntermNode in order to disallow it for subclasses that
541 // don't explicitly allow it, so normal TIntermNode constructor is used to construct the copy.
542 // We need to manually copy any fields of TIntermNode besides handling fields in TIntermTyped.
543 mLine = node.mLine;
544}
545
Olli Etuahod4f4c112016-04-15 15:11:24 +0300546bool TIntermTyped::isConstructorWithOnlyConstantUnionParameters()
547{
548 TIntermAggregate *constructor = getAsAggregate();
549 if (!constructor || !constructor->isConstructor())
550 {
551 return false;
552 }
553 for (TIntermNode *&node : *constructor->getSequence())
554 {
555 if (!node->getAsConstantUnion())
556 return false;
557 }
558 return true;
559}
560
Corentin Wallez509e4562016-08-25 14:55:44 -0400561// static
562TIntermTyped *TIntermTyped::CreateIndexNode(int index)
563{
564 TConstantUnion *u = new TConstantUnion[1];
565 u[0].setIConst(index);
566
567 TType type(EbtInt, EbpUndefined, EvqConst, 1);
568 TIntermConstantUnion *node = new TIntermConstantUnion(u, type);
569 return node;
570}
571
572// static
573TIntermTyped *TIntermTyped::CreateZero(const TType &type)
574{
575 TType constType(type);
576 constType.setQualifier(EvqConst);
577
578 if (!type.isArray() && type.getBasicType() != EbtStruct)
579 {
Corentin Wallez509e4562016-08-25 14:55:44 -0400580 size_t size = constType.getObjectSize();
581 TConstantUnion *u = new TConstantUnion[size];
582 for (size_t i = 0; i < size; ++i)
583 {
584 switch (type.getBasicType())
585 {
586 case EbtFloat:
587 u[i].setFConst(0.0f);
588 break;
589 case EbtInt:
590 u[i].setIConst(0);
591 break;
592 case EbtUInt:
593 u[i].setUConst(0u);
594 break;
595 case EbtBool:
596 u[i].setBConst(false);
597 break;
598 default:
Corentin Wallez17a5c062017-01-22 15:20:53 -0500599 // CreateZero is called by ParseContext that keeps parsing even when an error
600 // occurs, so it is possible for CreateZero to be called with non-basic types.
601 // This happens only on error condition but CreateZero needs to return a value
602 // with the correct type to continue the typecheck. That's why we handle
603 // non-basic type by setting whatever value, we just need the type to be right.
604 u[i].setIConst(42);
605 break;
Corentin Wallez509e4562016-08-25 14:55:44 -0400606 }
607 }
608
609 TIntermConstantUnion *node = new TIntermConstantUnion(u, constType);
610 return node;
611 }
612
Olli Etuaho193c0952017-05-02 15:51:47 +0300613 if (type.getBasicType() == EbtVoid)
614 {
615 // Void array. This happens only on error condition, similarly to the case above. We don't
616 // have a constructor operator for void, so this needs special handling. We'll end up with a
617 // value without the array type, but that should not be a problem.
618 constType.clearArrayness();
619 return CreateZero(constType);
620 }
621
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800622 TIntermSequence *arguments = new TIntermSequence();
Corentin Wallez509e4562016-08-25 14:55:44 -0400623
624 if (type.isArray())
625 {
626 TType elementType(type);
627 elementType.clearArrayness();
628
629 size_t arraySize = type.getArraySize();
630 for (size_t i = 0; i < arraySize; ++i)
631 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800632 arguments->push_back(CreateZero(elementType));
Corentin Wallez509e4562016-08-25 14:55:44 -0400633 }
634 }
635 else
636 {
637 ASSERT(type.getBasicType() == EbtStruct);
638
639 TStructure *structure = type.getStruct();
640 for (const auto &field : structure->fields())
641 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800642 arguments->push_back(CreateZero(*field->type()));
Corentin Wallez509e4562016-08-25 14:55:44 -0400643 }
644 }
645
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300646 return TIntermAggregate::CreateConstructor(constType, arguments);
Corentin Wallez509e4562016-08-25 14:55:44 -0400647}
648
Corentin Wallez36fd1002016-12-08 11:30:44 -0500649// static
650TIntermTyped *TIntermTyped::CreateBool(bool value)
651{
652 TConstantUnion *u = new TConstantUnion[1];
653 u[0].setBConst(value);
654
655 TType type(EbtBool, EbpUndefined, EvqConst, 1);
656 TIntermConstantUnion *node = new TIntermConstantUnion(u, type);
657 return node;
658}
659
Olli Etuahod7a25242015-08-18 13:49:45 +0300660TIntermConstantUnion::TIntermConstantUnion(const TIntermConstantUnion &node) : TIntermTyped(node)
661{
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200662 mUnionArrayPointer = node.mUnionArrayPointer;
Olli Etuahod7a25242015-08-18 13:49:45 +0300663}
664
Olli Etuahobd674552016-10-06 13:28:42 +0100665void TFunctionSymbolInfo::setFromFunction(const TFunction &function)
666{
Olli Etuahoec9232b2017-03-27 17:01:37 +0300667 setName(function.getName());
Olli Etuahofe486322017-03-21 09:30:54 +0000668 setId(TSymbolUniqueId(function));
669}
670
Olli Etuahoa22aa4e2017-05-24 18:17:23 +0300671TFunctionSymbolInfo::TFunctionSymbolInfo(const TSymbolUniqueId &id)
672 : mId(new TSymbolUniqueId(id)), mKnownToNotHaveSideEffects(false)
Olli Etuahofe486322017-03-21 09:30:54 +0000673{
674}
675
676TFunctionSymbolInfo::TFunctionSymbolInfo(const TFunctionSymbolInfo &info)
Olli Etuahoa22aa4e2017-05-24 18:17:23 +0300677 : mName(info.mName), mId(nullptr), mKnownToNotHaveSideEffects(info.mKnownToNotHaveSideEffects)
Olli Etuahofe486322017-03-21 09:30:54 +0000678{
679 if (info.mId)
680 {
681 mId = new TSymbolUniqueId(*info.mId);
682 }
683}
684
685TFunctionSymbolInfo &TFunctionSymbolInfo::operator=(const TFunctionSymbolInfo &info)
686{
687 mName = info.mName;
688 if (info.mId)
689 {
690 mId = new TSymbolUniqueId(*info.mId);
691 }
692 else
693 {
694 mId = nullptr;
695 }
696 return *this;
697}
698
699void TFunctionSymbolInfo::setId(const TSymbolUniqueId &id)
700{
701 mId = new TSymbolUniqueId(id);
702}
703
704const TSymbolUniqueId &TFunctionSymbolInfo::getId() const
705{
706 ASSERT(mId);
707 return *mId;
Olli Etuahobd674552016-10-06 13:28:42 +0100708}
709
Olli Etuahod7a25242015-08-18 13:49:45 +0300710TIntermAggregate::TIntermAggregate(const TIntermAggregate &node)
711 : TIntermOperator(node),
Olli Etuahod7a25242015-08-18 13:49:45 +0300712 mUseEmulatedFunction(node.mUseEmulatedFunction),
Olli Etuahobd674552016-10-06 13:28:42 +0100713 mGotPrecisionFromChildren(node.mGotPrecisionFromChildren),
714 mFunctionInfo(node.mFunctionInfo)
Olli Etuahod7a25242015-08-18 13:49:45 +0300715{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800716 for (TIntermNode *arg : node.mArguments)
Olli Etuahod7a25242015-08-18 13:49:45 +0300717 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800718 TIntermTyped *typedArg = arg->getAsTyped();
719 ASSERT(typedArg != nullptr);
720 TIntermTyped *argCopy = typedArg->deepCopy();
721 mArguments.push_back(argCopy);
Olli Etuahod7a25242015-08-18 13:49:45 +0300722 }
723}
724
Olli Etuahofe486322017-03-21 09:30:54 +0000725TIntermAggregate *TIntermAggregate::shallowCopy() const
726{
727 TIntermSequence *copySeq = new TIntermSequence();
728 copySeq->insert(copySeq->begin(), getSequence()->begin(), getSequence()->end());
729 TIntermAggregate *copyNode = new TIntermAggregate(mType, mOp, copySeq);
730 *copyNode->getFunctionSymbolInfo() = mFunctionInfo;
731 copyNode->setLine(mLine);
732 return copyNode;
733}
734
Olli Etuahob6fa0432016-09-28 16:28:05 +0100735TIntermSwizzle::TIntermSwizzle(const TIntermSwizzle &node) : TIntermTyped(node)
736{
737 TIntermTyped *operandCopy = node.mOperand->deepCopy();
738 ASSERT(operandCopy != nullptr);
739 mOperand = operandCopy;
Olli Etuahoc9da71f2017-03-06 16:28:54 +0000740 mSwizzleOffsets = node.mSwizzleOffsets;
Olli Etuahob6fa0432016-09-28 16:28:05 +0100741}
742
Olli Etuahod7a25242015-08-18 13:49:45 +0300743TIntermBinary::TIntermBinary(const TIntermBinary &node)
744 : TIntermOperator(node), mAddIndexClamp(node.mAddIndexClamp)
745{
746 TIntermTyped *leftCopy = node.mLeft->deepCopy();
747 TIntermTyped *rightCopy = node.mRight->deepCopy();
748 ASSERT(leftCopy != nullptr && rightCopy != nullptr);
749 mLeft = leftCopy;
750 mRight = rightCopy;
751}
752
753TIntermUnary::TIntermUnary(const TIntermUnary &node)
754 : TIntermOperator(node), mUseEmulatedFunction(node.mUseEmulatedFunction)
755{
756 TIntermTyped *operandCopy = node.mOperand->deepCopy();
757 ASSERT(operandCopy != nullptr);
758 mOperand = operandCopy;
759}
760
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300761TIntermTernary::TIntermTernary(const TIntermTernary &node) : TIntermTyped(node)
Olli Etuahod7a25242015-08-18 13:49:45 +0300762{
Olli Etuahod7a25242015-08-18 13:49:45 +0300763 TIntermTyped *conditionCopy = node.mCondition->deepCopy();
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300764 TIntermTyped *trueCopy = node.mTrueExpression->deepCopy();
765 TIntermTyped *falseCopy = node.mFalseExpression->deepCopy();
Olli Etuahod7a25242015-08-18 13:49:45 +0300766 ASSERT(conditionCopy != nullptr && trueCopy != nullptr && falseCopy != nullptr);
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300767 mCondition = conditionCopy;
768 mTrueExpression = trueCopy;
769 mFalseExpression = falseCopy;
Olli Etuahod7a25242015-08-18 13:49:45 +0300770}
771
Jamie Madillb1a85f42014-08-19 15:23:24 -0400772bool TIntermOperator::isAssignment() const
773{
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300774 return IsAssignment(mOp);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400775}
776
Olli Etuaho8f76bcc2015-06-02 13:54:20 +0300777bool TIntermOperator::isMultiplication() const
778{
779 switch (mOp)
780 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500781 case EOpMul:
782 case EOpMatrixTimesMatrix:
783 case EOpMatrixTimesVector:
784 case EOpMatrixTimesScalar:
785 case EOpVectorTimesMatrix:
786 case EOpVectorTimesScalar:
787 return true;
788 default:
789 return false;
Olli Etuaho8f76bcc2015-06-02 13:54:20 +0300790 }
791}
792
Jamie Madillb1a85f42014-08-19 15:23:24 -0400793bool TIntermOperator::isConstructor() const
794{
Olli Etuaho8fab3202017-05-08 18:22:22 +0300795 return (mOp == EOpConstruct);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400796}
797
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800798bool TIntermOperator::isFunctionCall() const
799{
800 switch (mOp)
801 {
802 case EOpCallFunctionInAST:
803 case EOpCallBuiltInFunction:
804 case EOpCallInternalRawFunction:
805 return true;
806 default:
807 return false;
808 }
809}
810
Olli Etuaho1dded802016-08-18 18:13:13 +0300811TOperator TIntermBinary::GetMulOpBasedOnOperands(const TType &left, const TType &right)
812{
813 if (left.isMatrix())
814 {
815 if (right.isMatrix())
816 {
817 return EOpMatrixTimesMatrix;
818 }
819 else
820 {
821 if (right.isVector())
822 {
823 return EOpMatrixTimesVector;
824 }
825 else
826 {
827 return EOpMatrixTimesScalar;
828 }
829 }
830 }
831 else
832 {
833 if (right.isMatrix())
834 {
835 if (left.isVector())
836 {
837 return EOpVectorTimesMatrix;
838 }
839 else
840 {
841 return EOpMatrixTimesScalar;
842 }
843 }
844 else
845 {
846 // Neither operand is a matrix.
847 if (left.isVector() == right.isVector())
848 {
849 // Leave as component product.
850 return EOpMul;
851 }
852 else
853 {
854 return EOpVectorTimesScalar;
855 }
856 }
857 }
858}
859
860TOperator TIntermBinary::GetMulAssignOpBasedOnOperands(const TType &left, const TType &right)
861{
862 if (left.isMatrix())
863 {
864 if (right.isMatrix())
865 {
866 return EOpMatrixTimesMatrixAssign;
867 }
868 else
869 {
870 // right should be scalar, but this may not be validated yet.
871 return EOpMatrixTimesScalarAssign;
872 }
873 }
874 else
875 {
876 if (right.isMatrix())
877 {
878 // Left should be a vector, but this may not be validated yet.
879 return EOpVectorTimesMatrixAssign;
880 }
881 else
882 {
883 // Neither operand is a matrix.
884 if (left.isVector() == right.isVector())
885 {
886 // Leave as component product.
887 return EOpMulAssign;
888 }
889 else
890 {
891 // left should be vector and right should be scalar, but this may not be validated
892 // yet.
893 return EOpVectorTimesScalarAssign;
894 }
895 }
896 }
897}
898
Jamie Madillb1a85f42014-08-19 15:23:24 -0400899//
900// Make sure the type of a unary operator is appropriate for its
901// combination of operation and operand type.
902//
Olli Etuahoa2234302016-08-31 12:05:39 +0300903void TIntermUnary::promote()
Jamie Madillb1a85f42014-08-19 15:23:24 -0400904{
Olli Etuahoa2234302016-08-31 12:05:39 +0300905 TQualifier resultQualifier = EvqTemporary;
906 if (mOperand->getQualifier() == EvqConst)
907 resultQualifier = EvqConst;
908
909 unsigned char operandPrimarySize =
910 static_cast<unsigned char>(mOperand->getType().getNominalSize());
Jamie Madillb1a85f42014-08-19 15:23:24 -0400911 switch (mOp)
912 {
Olli Etuahoa2234302016-08-31 12:05:39 +0300913 case EOpFloatBitsToInt:
914 setType(TType(EbtInt, EbpHigh, resultQualifier, operandPrimarySize));
915 break;
916 case EOpFloatBitsToUint:
917 setType(TType(EbtUInt, EbpHigh, resultQualifier, operandPrimarySize));
918 break;
919 case EOpIntBitsToFloat:
920 case EOpUintBitsToFloat:
921 setType(TType(EbtFloat, EbpHigh, resultQualifier, operandPrimarySize));
922 break;
923 case EOpPackSnorm2x16:
924 case EOpPackUnorm2x16:
925 case EOpPackHalf2x16:
Olli Etuaho25aef452017-01-29 16:15:44 -0800926 case EOpPackUnorm4x8:
927 case EOpPackSnorm4x8:
Olli Etuahoa2234302016-08-31 12:05:39 +0300928 setType(TType(EbtUInt, EbpHigh, resultQualifier));
929 break;
930 case EOpUnpackSnorm2x16:
931 case EOpUnpackUnorm2x16:
932 setType(TType(EbtFloat, EbpHigh, resultQualifier, 2));
933 break;
934 case EOpUnpackHalf2x16:
935 setType(TType(EbtFloat, EbpMedium, resultQualifier, 2));
936 break;
Olli Etuaho25aef452017-01-29 16:15:44 -0800937 case EOpUnpackUnorm4x8:
938 case EOpUnpackSnorm4x8:
939 setType(TType(EbtFloat, EbpMedium, resultQualifier, 4));
940 break;
Olli Etuahoa2234302016-08-31 12:05:39 +0300941 case EOpAny:
942 case EOpAll:
943 setType(TType(EbtBool, EbpUndefined, resultQualifier));
944 break;
945 case EOpLength:
946 case EOpDeterminant:
947 setType(TType(EbtFloat, mOperand->getType().getPrecision(), resultQualifier));
948 break;
949 case EOpTranspose:
950 setType(TType(EbtFloat, mOperand->getType().getPrecision(), resultQualifier,
951 static_cast<unsigned char>(mOperand->getType().getRows()),
952 static_cast<unsigned char>(mOperand->getType().getCols())));
953 break;
954 case EOpIsInf:
955 case EOpIsNan:
956 setType(TType(EbtBool, EbpUndefined, resultQualifier, operandPrimarySize));
957 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +0000958 case EOpBitfieldReverse:
959 setType(TType(mOperand->getBasicType(), EbpHigh, resultQualifier, operandPrimarySize));
960 break;
961 case EOpBitCount:
962 setType(TType(EbtInt, EbpLow, resultQualifier, operandPrimarySize));
963 break;
964 case EOpFindLSB:
965 setType(TType(EbtInt, EbpLow, resultQualifier, operandPrimarySize));
966 break;
967 case EOpFindMSB:
968 setType(TType(EbtInt, EbpLow, resultQualifier, operandPrimarySize));
969 break;
Olli Etuahoa2234302016-08-31 12:05:39 +0300970 default:
971 setType(mOperand->getType());
972 mType.setQualifier(resultQualifier);
973 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400974 }
Olli Etuahoa2234302016-08-31 12:05:39 +0300975}
Jamie Madillb1a85f42014-08-19 15:23:24 -0400976
Olli Etuahob6fa0432016-09-28 16:28:05 +0100977TIntermSwizzle::TIntermSwizzle(TIntermTyped *operand, const TVector<int> &swizzleOffsets)
978 : TIntermTyped(TType(EbtFloat, EbpUndefined)),
979 mOperand(operand),
980 mSwizzleOffsets(swizzleOffsets)
981{
982 ASSERT(mSwizzleOffsets.size() <= 4);
983 promote();
984}
985
Olli Etuahoa2234302016-08-31 12:05:39 +0300986TIntermUnary::TIntermUnary(TOperator op, TIntermTyped *operand)
987 : TIntermOperator(op), mOperand(operand), mUseEmulatedFunction(false)
988{
989 promote();
Jamie Madillb1a85f42014-08-19 15:23:24 -0400990}
991
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300992TIntermBinary::TIntermBinary(TOperator op, TIntermTyped *left, TIntermTyped *right)
993 : TIntermOperator(op), mLeft(left), mRight(right), mAddIndexClamp(false)
994{
995 promote();
996}
997
Olli Etuahobf4e1b72016-12-09 11:30:15 +0000998TIntermInvariantDeclaration::TIntermInvariantDeclaration(TIntermSymbol *symbol, const TSourceLoc &line)
999 : TIntermNode(), mSymbol(symbol)
1000{
1001 ASSERT(symbol);
1002 setLine(line);
1003}
1004
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001005TIntermTernary::TIntermTernary(TIntermTyped *cond,
1006 TIntermTyped *trueExpression,
1007 TIntermTyped *falseExpression)
1008 : TIntermTyped(trueExpression->getType()),
1009 mCondition(cond),
1010 mTrueExpression(trueExpression),
1011 mFalseExpression(falseExpression)
1012{
1013 getTypePointer()->setQualifier(
1014 TIntermTernary::DetermineQualifier(cond, trueExpression, falseExpression));
1015}
1016
Olli Etuaho81629262017-04-19 11:56:01 +03001017TIntermLoop::TIntermLoop(TLoopType type,
1018 TIntermNode *init,
1019 TIntermTyped *cond,
1020 TIntermTyped *expr,
1021 TIntermBlock *body)
1022 : mType(type), mInit(init), mCond(cond), mExpr(expr), mBody(body)
1023{
1024 // Declaration nodes with no children can appear if all the declarators just added constants to
1025 // the symbol table instead of generating code. They're no-ops so don't add them to the tree.
1026 if (mInit && mInit->getAsDeclarationNode() &&
1027 mInit->getAsDeclarationNode()->getSequence()->empty())
1028 {
1029 mInit = nullptr;
1030 }
1031}
1032
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001033// static
1034TQualifier TIntermTernary::DetermineQualifier(TIntermTyped *cond,
1035 TIntermTyped *trueExpression,
1036 TIntermTyped *falseExpression)
1037{
1038 if (cond->getQualifier() == EvqConst && trueExpression->getQualifier() == EvqConst &&
1039 falseExpression->getQualifier() == EvqConst)
1040 {
1041 return EvqConst;
1042 }
1043 return EvqTemporary;
1044}
1045
Olli Etuahob6fa0432016-09-28 16:28:05 +01001046void TIntermSwizzle::promote()
1047{
1048 TQualifier resultQualifier = EvqTemporary;
1049 if (mOperand->getQualifier() == EvqConst)
1050 resultQualifier = EvqConst;
1051
1052 auto numFields = mSwizzleOffsets.size();
1053 setType(TType(mOperand->getBasicType(), mOperand->getPrecision(), resultQualifier,
1054 static_cast<unsigned char>(numFields)));
1055}
1056
1057bool TIntermSwizzle::hasDuplicateOffsets() const
1058{
1059 int offsetCount[4] = {0u, 0u, 0u, 0u};
1060 for (const auto offset : mSwizzleOffsets)
1061 {
1062 offsetCount[offset]++;
1063 if (offsetCount[offset] > 1)
1064 {
1065 return true;
1066 }
1067 }
1068 return false;
1069}
1070
Olli Etuaho09b04a22016-12-15 13:30:26 +00001071bool TIntermSwizzle::offsetsMatch(int offset) const
1072{
1073 return mSwizzleOffsets.size() == 1 && mSwizzleOffsets[0] == offset;
1074}
1075
Olli Etuahob6fa0432016-09-28 16:28:05 +01001076void TIntermSwizzle::writeOffsetsAsXYZW(TInfoSinkBase *out) const
1077{
1078 for (const int offset : mSwizzleOffsets)
1079 {
1080 switch (offset)
1081 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001082 case 0:
1083 *out << "x";
1084 break;
1085 case 1:
1086 *out << "y";
1087 break;
1088 case 2:
1089 *out << "z";
1090 break;
1091 case 3:
1092 *out << "w";
1093 break;
1094 default:
1095 UNREACHABLE();
Olli Etuahob6fa0432016-09-28 16:28:05 +01001096 }
1097 }
1098}
1099
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001100TQualifier TIntermBinary::GetCommaQualifier(int shaderVersion,
1101 const TIntermTyped *left,
1102 const TIntermTyped *right)
1103{
1104 // ESSL3.00 section 12.43: The result of a sequence operator is not a constant-expression.
1105 if (shaderVersion >= 300 || left->getQualifier() != EvqConst ||
1106 right->getQualifier() != EvqConst)
1107 {
1108 return EvqTemporary;
1109 }
1110 return EvqConst;
1111}
Olli Etuahob6fa0432016-09-28 16:28:05 +01001112
1113// Establishes the type of the result of the binary operation.
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001114void TIntermBinary::promote()
Jamie Madillb1a85f42014-08-19 15:23:24 -04001115{
Olli Etuaho1dded802016-08-18 18:13:13 +03001116 ASSERT(!isMultiplication() ||
1117 mOp == GetMulOpBasedOnOperands(mLeft->getType(), mRight->getType()));
1118
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001119 // Comma is handled as a special case.
1120 if (mOp == EOpComma)
1121 {
1122 setType(mRight->getType());
1123 return;
1124 }
1125
Jamie Madillb1a85f42014-08-19 15:23:24 -04001126 // Base assumption: just make the type the same as the left
1127 // operand. Then only deviations from this need be coded.
Jamie Madillb1a85f42014-08-19 15:23:24 -04001128 setType(mLeft->getType());
1129
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001130 TQualifier resultQualifier = EvqConst;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001131 // Binary operations results in temporary variables unless both
1132 // operands are const.
1133 if (mLeft->getQualifier() != EvqConst || mRight->getQualifier() != EvqConst)
1134 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001135 resultQualifier = EvqTemporary;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001136 getTypePointer()->setQualifier(EvqTemporary);
1137 }
1138
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001139 // Handle indexing ops.
1140 switch (mOp)
1141 {
1142 case EOpIndexDirect:
1143 case EOpIndexIndirect:
1144 if (mLeft->isArray())
1145 {
1146 mType.clearArrayness();
1147 }
1148 else if (mLeft->isMatrix())
1149 {
1150 setType(TType(mLeft->getBasicType(), mLeft->getPrecision(), resultQualifier,
1151 static_cast<unsigned char>(mLeft->getRows())));
1152 }
1153 else if (mLeft->isVector())
1154 {
1155 setType(TType(mLeft->getBasicType(), mLeft->getPrecision(), resultQualifier));
1156 }
1157 else
1158 {
1159 UNREACHABLE();
1160 }
1161 return;
1162 case EOpIndexDirectStruct:
1163 {
1164 const TFieldList &fields = mLeft->getType().getStruct()->fields();
1165 const int i = mRight->getAsConstantUnion()->getIConst(0);
1166 setType(*fields[i]->type());
1167 getTypePointer()->setQualifier(resultQualifier);
1168 return;
1169 }
1170 case EOpIndexDirectInterfaceBlock:
1171 {
1172 const TFieldList &fields = mLeft->getType().getInterfaceBlock()->fields();
1173 const int i = mRight->getAsConstantUnion()->getIConst(0);
1174 setType(*fields[i]->type());
1175 getTypePointer()->setQualifier(resultQualifier);
1176 return;
1177 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001178 default:
1179 break;
1180 }
1181
1182 ASSERT(mLeft->isArray() == mRight->isArray());
1183
1184 // The result gets promoted to the highest precision.
1185 TPrecision higherPrecision = GetHigherPrecision(mLeft->getPrecision(), mRight->getPrecision());
1186 getTypePointer()->setPrecision(higherPrecision);
1187
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001188 const int nominalSize = std::max(mLeft->getNominalSize(), mRight->getNominalSize());
Jamie Madillb1a85f42014-08-19 15:23:24 -04001189
1190 //
1191 // All scalars or structs. Code after this test assumes this case is removed!
1192 //
1193 if (nominalSize == 1)
1194 {
1195 switch (mOp)
1196 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001197 //
1198 // Promote to conditional
1199 //
1200 case EOpEqual:
1201 case EOpNotEqual:
1202 case EOpLessThan:
1203 case EOpGreaterThan:
1204 case EOpLessThanEqual:
1205 case EOpGreaterThanEqual:
1206 setType(TType(EbtBool, EbpUndefined, resultQualifier));
1207 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001208
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001209 //
1210 // And and Or operate on conditionals
1211 //
1212 case EOpLogicalAnd:
1213 case EOpLogicalXor:
1214 case EOpLogicalOr:
1215 ASSERT(mLeft->getBasicType() == EbtBool && mRight->getBasicType() == EbtBool);
1216 setType(TType(EbtBool, EbpUndefined, resultQualifier));
1217 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001218
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001219 default:
1220 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001221 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001222 return;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001223 }
1224
1225 // If we reach here, at least one of the operands is vector or matrix.
1226 // The other operand could be a scalar, vector, or matrix.
Jamie Madillb1a85f42014-08-19 15:23:24 -04001227 TBasicType basicType = mLeft->getBasicType();
Olli Etuaho1dded802016-08-18 18:13:13 +03001228
Jamie Madillb1a85f42014-08-19 15:23:24 -04001229 switch (mOp)
1230 {
Olli Etuaho1dded802016-08-18 18:13:13 +03001231 case EOpMul:
1232 break;
1233 case EOpMatrixTimesScalar:
1234 if (mRight->isMatrix())
Jamie Madillb1a85f42014-08-19 15:23:24 -04001235 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001236 setType(TType(basicType, higherPrecision, resultQualifier,
1237 static_cast<unsigned char>(mRight->getCols()),
1238 static_cast<unsigned char>(mRight->getRows())));
Jamie Madillb1a85f42014-08-19 15:23:24 -04001239 }
Olli Etuaho1dded802016-08-18 18:13:13 +03001240 break;
1241 case EOpMatrixTimesVector:
1242 setType(TType(basicType, higherPrecision, resultQualifier,
1243 static_cast<unsigned char>(mLeft->getRows()), 1));
1244 break;
1245 case EOpMatrixTimesMatrix:
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001246 setType(TType(basicType, higherPrecision, resultQualifier,
1247 static_cast<unsigned char>(mRight->getCols()),
1248 static_cast<unsigned char>(mLeft->getRows())));
Olli Etuaho1dded802016-08-18 18:13:13 +03001249 break;
1250 case EOpVectorTimesScalar:
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001251 setType(TType(basicType, higherPrecision, resultQualifier,
Olli Etuaho1dded802016-08-18 18:13:13 +03001252 static_cast<unsigned char>(nominalSize), 1));
1253 break;
1254 case EOpVectorTimesMatrix:
1255 setType(TType(basicType, higherPrecision, resultQualifier,
1256 static_cast<unsigned char>(mRight->getCols()), 1));
1257 break;
1258 case EOpMulAssign:
1259 case EOpVectorTimesScalarAssign:
1260 case EOpVectorTimesMatrixAssign:
1261 case EOpMatrixTimesScalarAssign:
1262 case EOpMatrixTimesMatrixAssign:
1263 ASSERT(mOp == GetMulAssignOpBasedOnOperands(mLeft->getType(), mRight->getType()));
1264 break;
1265 case EOpAssign:
1266 case EOpInitialize:
Olli Etuaho1dded802016-08-18 18:13:13 +03001267 ASSERT((mLeft->getNominalSize() == mRight->getNominalSize()) &&
1268 (mLeft->getSecondarySize() == mRight->getSecondarySize()));
1269 break;
1270 case EOpAdd:
1271 case EOpSub:
1272 case EOpDiv:
1273 case EOpIMod:
1274 case EOpBitShiftLeft:
1275 case EOpBitShiftRight:
1276 case EOpBitwiseAnd:
1277 case EOpBitwiseXor:
1278 case EOpBitwiseOr:
1279 case EOpAddAssign:
1280 case EOpSubAssign:
1281 case EOpDivAssign:
1282 case EOpIModAssign:
1283 case EOpBitShiftLeftAssign:
1284 case EOpBitShiftRightAssign:
1285 case EOpBitwiseAndAssign:
1286 case EOpBitwiseXorAssign:
1287 case EOpBitwiseOrAssign:
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001288 {
1289 const int secondarySize =
1290 std::max(mLeft->getSecondarySize(), mRight->getSecondarySize());
1291 setType(TType(basicType, higherPrecision, resultQualifier,
1292 static_cast<unsigned char>(nominalSize),
1293 static_cast<unsigned char>(secondarySize)));
1294 ASSERT(!mLeft->isArray() && !mRight->isArray());
Olli Etuaho1dded802016-08-18 18:13:13 +03001295 break;
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001296 }
Olli Etuaho1dded802016-08-18 18:13:13 +03001297 case EOpEqual:
1298 case EOpNotEqual:
1299 case EOpLessThan:
1300 case EOpGreaterThan:
1301 case EOpLessThanEqual:
1302 case EOpGreaterThanEqual:
1303 ASSERT((mLeft->getNominalSize() == mRight->getNominalSize()) &&
1304 (mLeft->getSecondarySize() == mRight->getSecondarySize()));
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001305 setType(TType(EbtBool, EbpUndefined, resultQualifier));
Olli Etuaho1dded802016-08-18 18:13:13 +03001306 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001307
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001308 case EOpIndexDirect:
1309 case EOpIndexIndirect:
1310 case EOpIndexDirectInterfaceBlock:
1311 case EOpIndexDirectStruct:
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001312 // These ops should be already fully handled.
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001313 UNREACHABLE();
1314 break;
Olli Etuaho1dded802016-08-18 18:13:13 +03001315 default:
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001316 UNREACHABLE();
1317 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001318 }
Jamie Madillb1a85f42014-08-19 15:23:24 -04001319}
1320
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001321const TConstantUnion *TIntermConstantUnion::foldIndexing(int index)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001322{
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001323 if (isArray())
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001324 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001325 ASSERT(index < static_cast<int>(getType().getArraySize()));
1326 TType arrayElementType = getType();
1327 arrayElementType.clearArrayness();
1328 size_t arrayElementSize = arrayElementType.getObjectSize();
1329 return &mUnionArrayPointer[arrayElementSize * index];
1330 }
1331 else if (isMatrix())
1332 {
1333 ASSERT(index < getType().getCols());
1334 int size = getType().getRows();
1335 return &mUnionArrayPointer[size * index];
1336 }
1337 else if (isVector())
1338 {
1339 ASSERT(index < getType().getNominalSize());
1340 return &mUnionArrayPointer[index];
1341 }
1342 else
1343 {
1344 UNREACHABLE();
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001345 return nullptr;
1346 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001347}
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001348
Olli Etuahob6fa0432016-09-28 16:28:05 +01001349TIntermTyped *TIntermSwizzle::fold()
1350{
1351 TIntermConstantUnion *operandConstant = mOperand->getAsConstantUnion();
1352 if (operandConstant == nullptr)
1353 {
1354 return nullptr;
1355 }
1356
1357 TConstantUnion *constArray = new TConstantUnion[mSwizzleOffsets.size()];
1358 for (size_t i = 0; i < mSwizzleOffsets.size(); ++i)
1359 {
1360 constArray[i] = *operandConstant->foldIndexing(mSwizzleOffsets.at(i));
1361 }
1362 return CreateFoldedNode(constArray, this, mType.getQualifier());
1363}
1364
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001365TIntermTyped *TIntermBinary::fold(TDiagnostics *diagnostics)
1366{
1367 TIntermConstantUnion *leftConstant = mLeft->getAsConstantUnion();
1368 TIntermConstantUnion *rightConstant = mRight->getAsConstantUnion();
1369 switch (mOp)
1370 {
1371 case EOpIndexDirect:
1372 {
1373 if (leftConstant == nullptr || rightConstant == nullptr)
1374 {
1375 return nullptr;
1376 }
1377 int index = rightConstant->getIConst(0);
1378
1379 const TConstantUnion *constArray = leftConstant->foldIndexing(index);
1380 return CreateFoldedNode(constArray, this, mType.getQualifier());
1381 }
1382 case EOpIndexDirectStruct:
1383 {
1384 if (leftConstant == nullptr || rightConstant == nullptr)
1385 {
1386 return nullptr;
1387 }
1388 const TFieldList &fields = mLeft->getType().getStruct()->fields();
1389 size_t index = static_cast<size_t>(rightConstant->getIConst(0));
1390
1391 size_t previousFieldsSize = 0;
1392 for (size_t i = 0; i < index; ++i)
1393 {
1394 previousFieldsSize += fields[i]->type()->getObjectSize();
1395 }
1396
1397 const TConstantUnion *constArray = leftConstant->getUnionArrayPointer();
1398 return CreateFoldedNode(constArray + previousFieldsSize, this, mType.getQualifier());
1399 }
1400 case EOpIndexIndirect:
1401 case EOpIndexDirectInterfaceBlock:
1402 // Can never be constant folded.
1403 return nullptr;
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001404 default:
1405 {
1406 if (leftConstant == nullptr || rightConstant == nullptr)
1407 {
1408 return nullptr;
1409 }
Jamie Madill5db69f52016-09-15 12:47:32 -04001410 TConstantUnion *constArray =
1411 leftConstant->foldBinary(mOp, rightConstant, diagnostics, mLeft->getLine());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001412
1413 // Nodes may be constant folded without being qualified as constant.
1414 return CreateFoldedNode(constArray, this, mType.getQualifier());
1415 }
1416 }
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001417}
1418
Olli Etuahof119a262016-08-19 15:54:22 +03001419TIntermTyped *TIntermUnary::fold(TDiagnostics *diagnostics)
Olli Etuaho95310b02015-06-02 17:43:38 +03001420{
1421 TIntermConstantUnion *operandConstant = mOperand->getAsConstantUnion();
1422 if (operandConstant == nullptr)
1423 {
1424 return nullptr;
1425 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301426
1427 TConstantUnion *constArray = nullptr;
1428 switch (mOp)
1429 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001430 case EOpAny:
1431 case EOpAll:
1432 case EOpLength:
1433 case EOpTranspose:
1434 case EOpDeterminant:
1435 case EOpInverse:
1436 case EOpPackSnorm2x16:
1437 case EOpUnpackSnorm2x16:
1438 case EOpPackUnorm2x16:
1439 case EOpUnpackUnorm2x16:
1440 case EOpPackHalf2x16:
1441 case EOpUnpackHalf2x16:
Olli Etuaho25aef452017-01-29 16:15:44 -08001442 case EOpPackUnorm4x8:
1443 case EOpPackSnorm4x8:
1444 case EOpUnpackUnorm4x8:
1445 case EOpUnpackSnorm4x8:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001446 constArray = operandConstant->foldUnaryNonComponentWise(mOp);
1447 break;
1448 default:
1449 constArray = operandConstant->foldUnaryComponentWise(mOp, diagnostics);
1450 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301451 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001452
1453 // Nodes may be constant folded without being qualified as constant.
Olli Etuahoc9550582016-08-29 17:56:22 +03001454 return CreateFoldedNode(constArray, this, mType.getQualifier());
Olli Etuahob43846e2015-06-02 18:18:57 +03001455}
1456
Olli Etuahof119a262016-08-19 15:54:22 +03001457TIntermTyped *TIntermAggregate::fold(TDiagnostics *diagnostics)
Olli Etuahob43846e2015-06-02 18:18:57 +03001458{
1459 // Make sure that all params are constant before actual constant folding.
1460 for (auto *param : *getSequence())
Olli Etuaho95310b02015-06-02 17:43:38 +03001461 {
Olli Etuahob43846e2015-06-02 18:18:57 +03001462 if (param->getAsConstantUnion() == nullptr)
1463 {
1464 return nullptr;
1465 }
Olli Etuaho95310b02015-06-02 17:43:38 +03001466 }
Olli Etuaho1d122782015-11-06 15:35:17 +02001467 TConstantUnion *constArray = nullptr;
1468 if (isConstructor())
Olli Etuahof119a262016-08-19 15:54:22 +03001469 constArray = TIntermConstantUnion::FoldAggregateConstructor(this);
Olli Etuaho1d122782015-11-06 15:35:17 +02001470 else
Olli Etuahof119a262016-08-19 15:54:22 +03001471 constArray = TIntermConstantUnion::FoldAggregateBuiltIn(this, diagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001472
1473 // Nodes may be constant folded without being qualified as constant.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08001474 return CreateFoldedNode(constArray, this, getQualifier());
Olli Etuaho95310b02015-06-02 17:43:38 +03001475}
1476
Jamie Madillb1a85f42014-08-19 15:23:24 -04001477//
1478// The fold functions see if an operation on a constant can be done in place,
1479// without generating run-time code.
1480//
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001481// Returns the constant value to keep using or nullptr.
Jamie Madillb1a85f42014-08-19 15:23:24 -04001482//
Olli Etuaho3fdec912016-08-18 15:08:06 +03001483TConstantUnion *TIntermConstantUnion::foldBinary(TOperator op,
1484 TIntermConstantUnion *rightNode,
Jamie Madill5db69f52016-09-15 12:47:32 -04001485 TDiagnostics *diagnostics,
1486 const TSourceLoc &line)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001487{
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001488 const TConstantUnion *leftArray = getUnionArrayPointer();
1489 const TConstantUnion *rightArray = rightNode->getUnionArrayPointer();
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001490
Olli Etuahof119a262016-08-19 15:54:22 +03001491 ASSERT(leftArray && rightArray);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001492
1493 size_t objectSize = getType().getObjectSize();
1494
1495 // for a case like float f = vec4(2, 3, 4, 5) + 1.2;
1496 if (rightNode->getType().getObjectSize() == 1 && objectSize > 1)
1497 {
1498 rightArray = Vectorize(*rightNode->getUnionArrayPointer(), objectSize);
1499 }
1500 else if (rightNode->getType().getObjectSize() > 1 && objectSize == 1)
1501 {
1502 // for a case like float f = 1.2 + vec4(2, 3, 4, 5);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001503 leftArray = Vectorize(*getUnionArrayPointer(), rightNode->getType().getObjectSize());
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001504 objectSize = rightNode->getType().getObjectSize();
1505 }
1506
1507 TConstantUnion *resultArray = nullptr;
1508
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001509 switch (op)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001510 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001511 case EOpAdd:
1512 resultArray = new TConstantUnion[objectSize];
1513 for (size_t i = 0; i < objectSize; i++)
1514 resultArray[i] =
1515 TConstantUnion::add(leftArray[i], rightArray[i], diagnostics, line);
1516 break;
1517 case EOpSub:
1518 resultArray = new TConstantUnion[objectSize];
1519 for (size_t i = 0; i < objectSize; i++)
1520 resultArray[i] =
1521 TConstantUnion::sub(leftArray[i], rightArray[i], diagnostics, line);
1522 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001523
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001524 case EOpMul:
1525 case EOpVectorTimesScalar:
1526 case EOpMatrixTimesScalar:
1527 resultArray = new TConstantUnion[objectSize];
1528 for (size_t i = 0; i < objectSize; i++)
1529 resultArray[i] =
1530 TConstantUnion::mul(leftArray[i], rightArray[i], diagnostics, line);
1531 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001532
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001533 case EOpMatrixTimesMatrix:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001534 {
Jamie Madill5db69f52016-09-15 12:47:32 -04001535 // TODO(jmadll): This code should check for overflows.
Olli Etuaho3fdec912016-08-18 15:08:06 +03001536 ASSERT(getType().getBasicType() == EbtFloat && rightNode->getBasicType() == EbtFloat);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001537
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001538 const int leftCols = getCols();
1539 const int leftRows = getRows();
1540 const int rightCols = rightNode->getType().getCols();
1541 const int rightRows = rightNode->getType().getRows();
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001542 const int resultCols = rightCols;
1543 const int resultRows = leftRows;
1544
1545 resultArray = new TConstantUnion[resultCols * resultRows];
1546 for (int row = 0; row < resultRows; row++)
1547 {
1548 for (int column = 0; column < resultCols; column++)
1549 {
1550 resultArray[resultRows * column + row].setFConst(0.0f);
1551 for (int i = 0; i < leftCols; i++)
1552 {
1553 resultArray[resultRows * column + row].setFConst(
1554 resultArray[resultRows * column + row].getFConst() +
1555 leftArray[i * leftRows + row].getFConst() *
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001556 rightArray[column * rightRows + i].getFConst());
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001557 }
1558 }
1559 }
1560 }
1561 break;
1562
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001563 case EOpDiv:
1564 case EOpIMod:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001565 {
1566 resultArray = new TConstantUnion[objectSize];
1567 for (size_t i = 0; i < objectSize; i++)
1568 {
1569 switch (getType().getBasicType())
1570 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001571 case EbtFloat:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001572 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001573 ASSERT(op == EOpDiv);
1574 float dividend = leftArray[i].getFConst();
1575 float divisor = rightArray[i].getFConst();
1576 if (divisor == 0.0f)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001577 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001578 if (dividend == 0.0f)
Olli Etuahod4453572016-09-27 13:21:46 +01001579 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001580 diagnostics->warning(
1581 getLine(),
1582 "Zero divided by zero during constant folding generated NaN",
Olli Etuaho4de340a2016-12-16 09:32:03 +00001583 "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001584 resultArray[i].setFConst(std::numeric_limits<float>::quiet_NaN());
Olli Etuahod4453572016-09-27 13:21:46 +01001585 }
1586 else
1587 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001588 diagnostics->warning(getLine(),
1589 "Divide by zero during constant folding", "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001590 bool negativeResult =
1591 std::signbit(dividend) != std::signbit(divisor);
1592 resultArray[i].setFConst(
1593 negativeResult ? -std::numeric_limits<float>::infinity()
1594 : std::numeric_limits<float>::infinity());
Olli Etuahod4453572016-09-27 13:21:46 +01001595 }
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001596 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001597 else if (gl::isInf(dividend) && gl::isInf(divisor))
1598 {
1599 diagnostics->warning(getLine(),
1600 "Infinity divided by infinity during constant "
1601 "folding generated NaN",
Olli Etuaho4de340a2016-12-16 09:32:03 +00001602 "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001603 resultArray[i].setFConst(std::numeric_limits<float>::quiet_NaN());
1604 }
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001605 else
1606 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001607 float result = dividend / divisor;
1608 if (!gl::isInf(dividend) && gl::isInf(result))
Olli Etuahod4453572016-09-27 13:21:46 +01001609 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001610 diagnostics->warning(
1611 getLine(), "Constant folded division overflowed to infinity",
Olli Etuaho4de340a2016-12-16 09:32:03 +00001612 "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001613 }
1614 resultArray[i].setFConst(result);
1615 }
1616 break;
1617 }
1618 case EbtInt:
1619 if (rightArray[i] == 0)
1620 {
1621 diagnostics->warning(
Olli Etuaho4de340a2016-12-16 09:32:03 +00001622 getLine(), "Divide by zero error during constant folding", "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001623 resultArray[i].setIConst(INT_MAX);
1624 }
1625 else
1626 {
1627 int lhs = leftArray[i].getIConst();
1628 int divisor = rightArray[i].getIConst();
1629 if (op == EOpDiv)
1630 {
1631 // Check for the special case where the minimum representable number
1632 // is
1633 // divided by -1. If left alone this leads to integer overflow in
1634 // C++.
1635 // ESSL 3.00.6 section 4.1.3 Integers:
1636 // "However, for the case where the minimum representable value is
1637 // divided by -1, it is allowed to return either the minimum
1638 // representable value or the maximum representable value."
1639 if (lhs == -0x7fffffff - 1 && divisor == -1)
1640 {
1641 resultArray[i].setIConst(0x7fffffff);
1642 }
1643 else
1644 {
1645 resultArray[i].setIConst(lhs / divisor);
1646 }
Olli Etuahod4453572016-09-27 13:21:46 +01001647 }
1648 else
1649 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001650 ASSERT(op == EOpIMod);
1651 if (lhs < 0 || divisor < 0)
1652 {
1653 // ESSL 3.00.6 section 5.9: Results of modulus are undefined
1654 // when
1655 // either one of the operands is negative.
1656 diagnostics->warning(getLine(),
1657 "Negative modulus operator operand "
1658 "encountered during constant folding",
Olli Etuaho4de340a2016-12-16 09:32:03 +00001659 "%");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001660 resultArray[i].setIConst(0);
1661 }
1662 else
1663 {
1664 resultArray[i].setIConst(lhs % divisor);
1665 }
Olli Etuahod4453572016-09-27 13:21:46 +01001666 }
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001667 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001668 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001669
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001670 case EbtUInt:
1671 if (rightArray[i] == 0)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001672 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001673 diagnostics->warning(
Olli Etuaho4de340a2016-12-16 09:32:03 +00001674 getLine(), "Divide by zero error during constant folding", "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001675 resultArray[i].setUConst(UINT_MAX);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001676 }
1677 else
1678 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001679 if (op == EOpDiv)
1680 {
1681 resultArray[i].setUConst(leftArray[i].getUConst() /
1682 rightArray[i].getUConst());
1683 }
1684 else
1685 {
1686 ASSERT(op == EOpIMod);
1687 resultArray[i].setUConst(leftArray[i].getUConst() %
1688 rightArray[i].getUConst());
1689 }
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001690 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001691 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001692
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001693 default:
1694 UNREACHABLE();
1695 return nullptr;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001696 }
1697 }
1698 }
1699 break;
1700
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001701 case EOpMatrixTimesVector:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001702 {
Jamie Madill5db69f52016-09-15 12:47:32 -04001703 // TODO(jmadll): This code should check for overflows.
Olli Etuaho3fdec912016-08-18 15:08:06 +03001704 ASSERT(rightNode->getBasicType() == EbtFloat);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001705
1706 const int matrixCols = getCols();
1707 const int matrixRows = getRows();
1708
1709 resultArray = new TConstantUnion[matrixRows];
1710
1711 for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++)
1712 {
1713 resultArray[matrixRow].setFConst(0.0f);
1714 for (int col = 0; col < matrixCols; col++)
1715 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001716 resultArray[matrixRow].setFConst(
1717 resultArray[matrixRow].getFConst() +
1718 leftArray[col * matrixRows + matrixRow].getFConst() *
1719 rightArray[col].getFConst());
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001720 }
1721 }
1722 }
1723 break;
1724
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001725 case EOpVectorTimesMatrix:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001726 {
Jamie Madill5db69f52016-09-15 12:47:32 -04001727 // TODO(jmadll): This code should check for overflows.
Olli Etuaho3fdec912016-08-18 15:08:06 +03001728 ASSERT(getType().getBasicType() == EbtFloat);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001729
1730 const int matrixCols = rightNode->getType().getCols();
1731 const int matrixRows = rightNode->getType().getRows();
1732
1733 resultArray = new TConstantUnion[matrixCols];
1734
1735 for (int matrixCol = 0; matrixCol < matrixCols; matrixCol++)
1736 {
1737 resultArray[matrixCol].setFConst(0.0f);
1738 for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++)
1739 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001740 resultArray[matrixCol].setFConst(
1741 resultArray[matrixCol].getFConst() +
1742 leftArray[matrixRow].getFConst() *
1743 rightArray[matrixCol * matrixRows + matrixRow].getFConst());
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001744 }
1745 }
1746 }
1747 break;
1748
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001749 case EOpLogicalAnd:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001750 {
1751 resultArray = new TConstantUnion[objectSize];
1752 for (size_t i = 0; i < objectSize; i++)
1753 {
1754 resultArray[i] = leftArray[i] && rightArray[i];
1755 }
1756 }
1757 break;
1758
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001759 case EOpLogicalOr:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001760 {
1761 resultArray = new TConstantUnion[objectSize];
1762 for (size_t i = 0; i < objectSize; i++)
1763 {
1764 resultArray[i] = leftArray[i] || rightArray[i];
1765 }
1766 }
1767 break;
1768
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001769 case EOpLogicalXor:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001770 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001771 ASSERT(getType().getBasicType() == EbtBool);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001772 resultArray = new TConstantUnion[objectSize];
1773 for (size_t i = 0; i < objectSize; i++)
1774 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001775 resultArray[i].setBConst(leftArray[i] != rightArray[i]);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001776 }
1777 }
1778 break;
1779
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001780 case EOpBitwiseAnd:
1781 resultArray = new TConstantUnion[objectSize];
1782 for (size_t i = 0; i < objectSize; i++)
1783 resultArray[i] = leftArray[i] & rightArray[i];
1784 break;
1785 case EOpBitwiseXor:
1786 resultArray = new TConstantUnion[objectSize];
1787 for (size_t i = 0; i < objectSize; i++)
1788 resultArray[i] = leftArray[i] ^ rightArray[i];
1789 break;
1790 case EOpBitwiseOr:
1791 resultArray = new TConstantUnion[objectSize];
1792 for (size_t i = 0; i < objectSize; i++)
1793 resultArray[i] = leftArray[i] | rightArray[i];
1794 break;
1795 case EOpBitShiftLeft:
1796 resultArray = new TConstantUnion[objectSize];
1797 for (size_t i = 0; i < objectSize; i++)
1798 resultArray[i] =
1799 TConstantUnion::lshift(leftArray[i], rightArray[i], diagnostics, line);
1800 break;
1801 case EOpBitShiftRight:
1802 resultArray = new TConstantUnion[objectSize];
1803 for (size_t i = 0; i < objectSize; i++)
1804 resultArray[i] =
1805 TConstantUnion::rshift(leftArray[i], rightArray[i], diagnostics, line);
1806 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001807
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001808 case EOpLessThan:
1809 ASSERT(objectSize == 1);
1810 resultArray = new TConstantUnion[1];
1811 resultArray->setBConst(*leftArray < *rightArray);
1812 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001813
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001814 case EOpGreaterThan:
1815 ASSERT(objectSize == 1);
1816 resultArray = new TConstantUnion[1];
1817 resultArray->setBConst(*leftArray > *rightArray);
1818 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001819
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001820 case EOpLessThanEqual:
1821 ASSERT(objectSize == 1);
1822 resultArray = new TConstantUnion[1];
1823 resultArray->setBConst(!(*leftArray > *rightArray));
1824 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001825
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001826 case EOpGreaterThanEqual:
1827 ASSERT(objectSize == 1);
1828 resultArray = new TConstantUnion[1];
1829 resultArray->setBConst(!(*leftArray < *rightArray));
1830 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001831
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001832 case EOpEqual:
1833 case EOpNotEqual:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001834 {
1835 resultArray = new TConstantUnion[1];
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001836 bool equal = true;
Olli Etuaho40d9edf2015-11-12 17:30:34 +02001837 for (size_t i = 0; i < objectSize; i++)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001838 {
Olli Etuaho40d9edf2015-11-12 17:30:34 +02001839 if (leftArray[i] != rightArray[i])
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001840 {
Olli Etuaho40d9edf2015-11-12 17:30:34 +02001841 equal = false;
1842 break; // break out of for loop
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001843 }
1844 }
1845 if (op == EOpEqual)
1846 {
1847 resultArray->setBConst(equal);
1848 }
1849 else
1850 {
1851 resultArray->setBConst(!equal);
1852 }
1853 }
1854 break;
1855
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001856 default:
1857 UNREACHABLE();
1858 return nullptr;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001859 }
1860 return resultArray;
1861}
1862
Olli Etuahof119a262016-08-19 15:54:22 +03001863// The fold functions do operations on a constant at GLSL compile time, without generating run-time
1864// code. Returns the constant value to keep using. Nullptr should not be returned.
1865TConstantUnion *TIntermConstantUnion::foldUnaryNonComponentWise(TOperator op)
Jamie Madillb1a85f42014-08-19 15:23:24 -04001866{
Olli Etuahof119a262016-08-19 15:54:22 +03001867 // Do operations where the return type may have a different number of components compared to the
1868 // operand type.
Jamie Madillb1a85f42014-08-19 15:23:24 -04001869
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001870 const TConstantUnion *operandArray = getUnionArrayPointer();
Olli Etuahof119a262016-08-19 15:54:22 +03001871 ASSERT(operandArray);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301872
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001873 size_t objectSize = getType().getObjectSize();
Arun Patoleab2b9a22015-07-06 18:27:56 +05301874 TConstantUnion *resultArray = nullptr;
1875 switch (op)
1876 {
Olli Etuahof119a262016-08-19 15:54:22 +03001877 case EOpAny:
1878 ASSERT(getType().getBasicType() == EbtBool);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301879 resultArray = new TConstantUnion();
1880 resultArray->setBConst(false);
1881 for (size_t i = 0; i < objectSize; i++)
1882 {
1883 if (operandArray[i].getBConst())
1884 {
1885 resultArray->setBConst(true);
1886 break;
1887 }
1888 }
1889 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301890
Olli Etuahof119a262016-08-19 15:54:22 +03001891 case EOpAll:
1892 ASSERT(getType().getBasicType() == EbtBool);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301893 resultArray = new TConstantUnion();
1894 resultArray->setBConst(true);
1895 for (size_t i = 0; i < objectSize; i++)
1896 {
1897 if (!operandArray[i].getBConst())
1898 {
1899 resultArray->setBConst(false);
1900 break;
1901 }
1902 }
1903 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301904
Olli Etuahof119a262016-08-19 15:54:22 +03001905 case EOpLength:
1906 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301907 resultArray = new TConstantUnion();
1908 resultArray->setFConst(VectorLength(operandArray, objectSize));
1909 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301910
Olli Etuahof119a262016-08-19 15:54:22 +03001911 case EOpTranspose:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301912 {
Olli Etuahof119a262016-08-19 15:54:22 +03001913 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301914 resultArray = new TConstantUnion[objectSize];
1915 angle::Matrix<float> result =
Olli Etuahod5da5052016-08-29 13:16:55 +03001916 GetMatrix(operandArray, getType().getRows(), getType().getCols()).transpose();
Arun Patoleab2b9a22015-07-06 18:27:56 +05301917 SetUnionArrayFromMatrix(result, resultArray);
1918 break;
1919 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301920
Olli Etuahof119a262016-08-19 15:54:22 +03001921 case EOpDeterminant:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301922 {
Olli Etuahof119a262016-08-19 15:54:22 +03001923 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301924 unsigned int size = getType().getNominalSize();
1925 ASSERT(size >= 2 && size <= 4);
1926 resultArray = new TConstantUnion();
1927 resultArray->setFConst(GetMatrix(operandArray, size).determinant());
1928 break;
1929 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301930
Olli Etuahof119a262016-08-19 15:54:22 +03001931 case EOpInverse:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301932 {
Olli Etuahof119a262016-08-19 15:54:22 +03001933 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301934 unsigned int size = getType().getNominalSize();
1935 ASSERT(size >= 2 && size <= 4);
Olli Etuahof119a262016-08-19 15:54:22 +03001936 resultArray = new TConstantUnion[objectSize];
Arun Patoleab2b9a22015-07-06 18:27:56 +05301937 angle::Matrix<float> result = GetMatrix(operandArray, size).inverse();
1938 SetUnionArrayFromMatrix(result, resultArray);
1939 break;
1940 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301941
Olli Etuahof119a262016-08-19 15:54:22 +03001942 case EOpPackSnorm2x16:
1943 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301944 ASSERT(getType().getNominalSize() == 2);
1945 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03001946 resultArray->setUConst(
1947 gl::packSnorm2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
Arun Patoleab2b9a22015-07-06 18:27:56 +05301948 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301949
Olli Etuahof119a262016-08-19 15:54:22 +03001950 case EOpUnpackSnorm2x16:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301951 {
Olli Etuahof119a262016-08-19 15:54:22 +03001952 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301953 resultArray = new TConstantUnion[2];
1954 float f1, f2;
1955 gl::unpackSnorm2x16(operandArray[0].getUConst(), &f1, &f2);
1956 resultArray[0].setFConst(f1);
1957 resultArray[1].setFConst(f2);
1958 break;
1959 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301960
Olli Etuahof119a262016-08-19 15:54:22 +03001961 case EOpPackUnorm2x16:
1962 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301963 ASSERT(getType().getNominalSize() == 2);
1964 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03001965 resultArray->setUConst(
1966 gl::packUnorm2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
Arun Patoleab2b9a22015-07-06 18:27:56 +05301967 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301968
Olli Etuahof119a262016-08-19 15:54:22 +03001969 case EOpUnpackUnorm2x16:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301970 {
Olli Etuahof119a262016-08-19 15:54:22 +03001971 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301972 resultArray = new TConstantUnion[2];
1973 float f1, f2;
1974 gl::unpackUnorm2x16(operandArray[0].getUConst(), &f1, &f2);
1975 resultArray[0].setFConst(f1);
1976 resultArray[1].setFConst(f2);
1977 break;
1978 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301979
Olli Etuahof119a262016-08-19 15:54:22 +03001980 case EOpPackHalf2x16:
1981 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301982 ASSERT(getType().getNominalSize() == 2);
1983 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03001984 resultArray->setUConst(
1985 gl::packHalf2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
Arun Patoleab2b9a22015-07-06 18:27:56 +05301986 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05301987
Olli Etuahof119a262016-08-19 15:54:22 +03001988 case EOpUnpackHalf2x16:
Arun Patoleab2b9a22015-07-06 18:27:56 +05301989 {
Olli Etuahof119a262016-08-19 15:54:22 +03001990 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patoleab2b9a22015-07-06 18:27:56 +05301991 resultArray = new TConstantUnion[2];
1992 float f1, f2;
1993 gl::unpackHalf2x16(operandArray[0].getUConst(), &f1, &f2);
1994 resultArray[0].setFConst(f1);
1995 resultArray[1].setFConst(f2);
1996 break;
1997 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301998
Olli Etuaho25aef452017-01-29 16:15:44 -08001999 case EOpPackUnorm4x8:
2000 {
2001 ASSERT(getType().getBasicType() == EbtFloat);
2002 resultArray = new TConstantUnion();
2003 resultArray->setUConst(
2004 gl::PackUnorm4x8(operandArray[0].getFConst(), operandArray[1].getFConst(),
2005 operandArray[2].getFConst(), operandArray[3].getFConst()));
2006 break;
2007 }
2008 case EOpPackSnorm4x8:
2009 {
2010 ASSERT(getType().getBasicType() == EbtFloat);
2011 resultArray = new TConstantUnion();
2012 resultArray->setUConst(
2013 gl::PackSnorm4x8(operandArray[0].getFConst(), operandArray[1].getFConst(),
2014 operandArray[2].getFConst(), operandArray[3].getFConst()));
2015 break;
2016 }
2017 case EOpUnpackUnorm4x8:
2018 {
2019 ASSERT(getType().getBasicType() == EbtUInt);
2020 resultArray = new TConstantUnion[4];
2021 float f[4];
2022 gl::UnpackUnorm4x8(operandArray[0].getUConst(), f);
2023 for (size_t i = 0; i < 4; ++i)
2024 {
2025 resultArray[i].setFConst(f[i]);
2026 }
2027 break;
2028 }
2029 case EOpUnpackSnorm4x8:
2030 {
2031 ASSERT(getType().getBasicType() == EbtUInt);
2032 resultArray = new TConstantUnion[4];
2033 float f[4];
2034 gl::UnpackSnorm4x8(operandArray[0].getUConst(), f);
2035 for (size_t i = 0; i < 4; ++i)
2036 {
2037 resultArray[i].setFConst(f[i]);
2038 }
2039 break;
2040 }
2041
Olli Etuahof119a262016-08-19 15:54:22 +03002042 default:
2043 UNREACHABLE();
2044 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302045 }
2046
2047 return resultArray;
2048}
2049
Olli Etuahof119a262016-08-19 15:54:22 +03002050TConstantUnion *TIntermConstantUnion::foldUnaryComponentWise(TOperator op,
2051 TDiagnostics *diagnostics)
Arun Patoleab2b9a22015-07-06 18:27:56 +05302052{
Olli Etuahof119a262016-08-19 15:54:22 +03002053 // Do unary operations where each component of the result is computed based on the corresponding
2054 // component of the operand. Also folds normalize, though the divisor in that case takes all
2055 // components into account.
Arun Patoleab2b9a22015-07-06 18:27:56 +05302056
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002057 const TConstantUnion *operandArray = getUnionArrayPointer();
Olli Etuahof119a262016-08-19 15:54:22 +03002058 ASSERT(operandArray);
Jamie Madillb1a85f42014-08-19 15:23:24 -04002059
2060 size_t objectSize = getType().getObjectSize();
2061
Arun Patoleab2b9a22015-07-06 18:27:56 +05302062 TConstantUnion *resultArray = new TConstantUnion[objectSize];
2063 for (size_t i = 0; i < objectSize; i++)
Arun Patole9d0b1f92015-05-20 14:27:17 +05302064 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002065 switch (op)
Arun Patole9d0b1f92015-05-20 14:27:17 +05302066 {
Olli Etuahof119a262016-08-19 15:54:22 +03002067 case EOpNegative:
2068 switch (getType().getBasicType())
2069 {
2070 case EbtFloat:
2071 resultArray[i].setFConst(-operandArray[i].getFConst());
2072 break;
2073 case EbtInt:
Olli Etuaho42fad762016-09-28 10:06:29 +01002074 if (operandArray[i] == std::numeric_limits<int>::min())
2075 {
2076 // The minimum representable integer doesn't have a positive
2077 // counterpart, rather the negation overflows and in ESSL is supposed to
2078 // wrap back to the minimum representable integer. Make sure that we
2079 // don't actually let the negation overflow, which has undefined
2080 // behavior in C++.
2081 resultArray[i].setIConst(std::numeric_limits<int>::min());
2082 }
2083 else
2084 {
2085 resultArray[i].setIConst(-operandArray[i].getIConst());
2086 }
Olli Etuahof119a262016-08-19 15:54:22 +03002087 break;
2088 case EbtUInt:
Olli Etuaho42fad762016-09-28 10:06:29 +01002089 if (operandArray[i] == 0x80000000u)
2090 {
2091 resultArray[i].setUConst(0x80000000u);
2092 }
2093 else
2094 {
2095 resultArray[i].setUConst(static_cast<unsigned int>(
2096 -static_cast<int>(operandArray[i].getUConst())));
2097 }
Olli Etuahof119a262016-08-19 15:54:22 +03002098 break;
2099 default:
2100 UNREACHABLE();
2101 return nullptr;
2102 }
Arun Patole1155ddd2015-06-05 18:04:36 +05302103 break;
Arun Patolecdfa8f52015-06-30 17:48:25 +05302104
Olli Etuahof119a262016-08-19 15:54:22 +03002105 case EOpPositive:
2106 switch (getType().getBasicType())
2107 {
2108 case EbtFloat:
2109 resultArray[i].setFConst(operandArray[i].getFConst());
2110 break;
2111 case EbtInt:
2112 resultArray[i].setIConst(operandArray[i].getIConst());
2113 break;
2114 case EbtUInt:
2115 resultArray[i].setUConst(static_cast<unsigned int>(
2116 static_cast<int>(operandArray[i].getUConst())));
2117 break;
2118 default:
2119 UNREACHABLE();
2120 return nullptr;
2121 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302122 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302123
Olli Etuahof119a262016-08-19 15:54:22 +03002124 case EOpLogicalNot:
2125 switch (getType().getBasicType())
2126 {
2127 case EbtBool:
2128 resultArray[i].setBConst(!operandArray[i].getBConst());
2129 break;
2130 default:
2131 UNREACHABLE();
2132 return nullptr;
2133 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302134 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302135
Olli Etuahof119a262016-08-19 15:54:22 +03002136 case EOpBitwiseNot:
2137 switch (getType().getBasicType())
2138 {
2139 case EbtInt:
2140 resultArray[i].setIConst(~operandArray[i].getIConst());
2141 break;
2142 case EbtUInt:
2143 resultArray[i].setUConst(~operandArray[i].getUConst());
2144 break;
2145 default:
2146 UNREACHABLE();
2147 return nullptr;
2148 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302149 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302150
Olli Etuahof119a262016-08-19 15:54:22 +03002151 case EOpRadians:
2152 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302153 resultArray[i].setFConst(kDegreesToRadiansMultiplier * operandArray[i].getFConst());
2154 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302155
Olli Etuahof119a262016-08-19 15:54:22 +03002156 case EOpDegrees:
2157 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302158 resultArray[i].setFConst(kRadiansToDegreesMultiplier * operandArray[i].getFConst());
2159 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302160
Olli Etuahof119a262016-08-19 15:54:22 +03002161 case EOpSin:
2162 foldFloatTypeUnary(operandArray[i], &sinf, &resultArray[i]);
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 EOpCos:
2166 foldFloatTypeUnary(operandArray[i], &cosf, &resultArray[i]);
2167 break;
2168
2169 case EOpTan:
2170 foldFloatTypeUnary(operandArray[i], &tanf, &resultArray[i]);
2171 break;
2172
2173 case EOpAsin:
2174 // For asin(x), results are undefined if |x| > 1, we are choosing to set result to
2175 // 0.
2176 if (fabsf(operandArray[i].getFConst()) > 1.0f)
2177 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2178 diagnostics, &resultArray[i]);
2179 else
2180 foldFloatTypeUnary(operandArray[i], &asinf, &resultArray[i]);
2181 break;
2182
2183 case EOpAcos:
2184 // For acos(x), results are undefined if |x| > 1, we are choosing to set result to
2185 // 0.
2186 if (fabsf(operandArray[i].getFConst()) > 1.0f)
2187 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2188 diagnostics, &resultArray[i]);
2189 else
2190 foldFloatTypeUnary(operandArray[i], &acosf, &resultArray[i]);
2191 break;
2192
2193 case EOpAtan:
2194 foldFloatTypeUnary(operandArray[i], &atanf, &resultArray[i]);
2195 break;
2196
2197 case EOpSinh:
2198 foldFloatTypeUnary(operandArray[i], &sinhf, &resultArray[i]);
2199 break;
2200
2201 case EOpCosh:
2202 foldFloatTypeUnary(operandArray[i], &coshf, &resultArray[i]);
2203 break;
2204
2205 case EOpTanh:
2206 foldFloatTypeUnary(operandArray[i], &tanhf, &resultArray[i]);
2207 break;
2208
2209 case EOpAsinh:
2210 foldFloatTypeUnary(operandArray[i], &asinhf, &resultArray[i]);
2211 break;
2212
2213 case EOpAcosh:
2214 // For acosh(x), results are undefined if x < 1, we are choosing to set result to 0.
2215 if (operandArray[i].getFConst() < 1.0f)
2216 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2217 diagnostics, &resultArray[i]);
2218 else
2219 foldFloatTypeUnary(operandArray[i], &acoshf, &resultArray[i]);
2220 break;
2221
2222 case EOpAtanh:
2223 // For atanh(x), results are undefined if |x| >= 1, we are choosing to set result to
2224 // 0.
2225 if (fabsf(operandArray[i].getFConst()) >= 1.0f)
2226 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2227 diagnostics, &resultArray[i]);
2228 else
2229 foldFloatTypeUnary(operandArray[i], &atanhf, &resultArray[i]);
2230 break;
2231
2232 case EOpAbs:
2233 switch (getType().getBasicType())
Arun Patoleab2b9a22015-07-06 18:27:56 +05302234 {
Olli Etuahof119a262016-08-19 15:54:22 +03002235 case EbtFloat:
2236 resultArray[i].setFConst(fabsf(operandArray[i].getFConst()));
2237 break;
2238 case EbtInt:
2239 resultArray[i].setIConst(abs(operandArray[i].getIConst()));
2240 break;
2241 default:
2242 UNREACHABLE();
2243 return nullptr;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302244 }
2245 break;
Olli Etuahof119a262016-08-19 15:54:22 +03002246
2247 case EOpSign:
2248 switch (getType().getBasicType())
Arun Patoleab2b9a22015-07-06 18:27:56 +05302249 {
Olli Etuahof119a262016-08-19 15:54:22 +03002250 case EbtFloat:
2251 {
2252 float fConst = operandArray[i].getFConst();
2253 float fResult = 0.0f;
2254 if (fConst > 0.0f)
2255 fResult = 1.0f;
2256 else if (fConst < 0.0f)
2257 fResult = -1.0f;
2258 resultArray[i].setFConst(fResult);
2259 break;
2260 }
2261 case EbtInt:
2262 {
2263 int iConst = operandArray[i].getIConst();
2264 int iResult = 0;
2265 if (iConst > 0)
2266 iResult = 1;
2267 else if (iConst < 0)
2268 iResult = -1;
2269 resultArray[i].setIConst(iResult);
2270 break;
2271 }
2272 default:
2273 UNREACHABLE();
2274 return nullptr;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302275 }
2276 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302277
Olli Etuahof119a262016-08-19 15:54:22 +03002278 case EOpFloor:
2279 foldFloatTypeUnary(operandArray[i], &floorf, &resultArray[i]);
2280 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302281
Olli Etuahof119a262016-08-19 15:54:22 +03002282 case EOpTrunc:
2283 foldFloatTypeUnary(operandArray[i], &truncf, &resultArray[i]);
2284 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302285
Olli Etuahof119a262016-08-19 15:54:22 +03002286 case EOpRound:
2287 foldFloatTypeUnary(operandArray[i], &roundf, &resultArray[i]);
2288 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302289
Olli Etuahof119a262016-08-19 15:54:22 +03002290 case EOpRoundEven:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302291 {
Olli Etuahof119a262016-08-19 15:54:22 +03002292 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302293 float x = operandArray[i].getFConst();
2294 float result;
2295 float fractPart = modff(x, &result);
2296 if (fabsf(fractPart) == 0.5f)
2297 result = 2.0f * roundf(x / 2.0f);
2298 else
2299 result = roundf(x);
2300 resultArray[i].setFConst(result);
2301 break;
2302 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302303
Olli Etuahof119a262016-08-19 15:54:22 +03002304 case EOpCeil:
2305 foldFloatTypeUnary(operandArray[i], &ceilf, &resultArray[i]);
2306 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302307
Olli Etuahof119a262016-08-19 15:54:22 +03002308 case EOpFract:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302309 {
Olli Etuahof119a262016-08-19 15:54:22 +03002310 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302311 float x = operandArray[i].getFConst();
2312 resultArray[i].setFConst(x - floorf(x));
2313 break;
2314 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302315
Olli Etuahof119a262016-08-19 15:54:22 +03002316 case EOpIsNan:
2317 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05302318 resultArray[i].setBConst(gl::isNaN(operandArray[0].getFConst()));
2319 break;
Arun Patole551279e2015-07-07 18:18:23 +05302320
Olli Etuahof119a262016-08-19 15:54:22 +03002321 case EOpIsInf:
2322 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05302323 resultArray[i].setBConst(gl::isInf(operandArray[0].getFConst()));
2324 break;
Arun Patole551279e2015-07-07 18:18:23 +05302325
Olli Etuahof119a262016-08-19 15:54:22 +03002326 case EOpFloatBitsToInt:
2327 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05302328 resultArray[i].setIConst(gl::bitCast<int32_t>(operandArray[0].getFConst()));
2329 break;
Arun Patole551279e2015-07-07 18:18:23 +05302330
Olli Etuahof119a262016-08-19 15:54:22 +03002331 case EOpFloatBitsToUint:
2332 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05302333 resultArray[i].setUConst(gl::bitCast<uint32_t>(operandArray[0].getFConst()));
2334 break;
Arun Patole551279e2015-07-07 18:18:23 +05302335
Olli Etuahof119a262016-08-19 15:54:22 +03002336 case EOpIntBitsToFloat:
2337 ASSERT(getType().getBasicType() == EbtInt);
Arun Patole551279e2015-07-07 18:18:23 +05302338 resultArray[i].setFConst(gl::bitCast<float>(operandArray[0].getIConst()));
2339 break;
Arun Patole551279e2015-07-07 18:18:23 +05302340
Olli Etuahof119a262016-08-19 15:54:22 +03002341 case EOpUintBitsToFloat:
2342 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patole551279e2015-07-07 18:18:23 +05302343 resultArray[i].setFConst(gl::bitCast<float>(operandArray[0].getUConst()));
2344 break;
Arun Patole551279e2015-07-07 18:18:23 +05302345
Olli Etuahof119a262016-08-19 15:54:22 +03002346 case EOpExp:
2347 foldFloatTypeUnary(operandArray[i], &expf, &resultArray[i]);
2348 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302349
Olli Etuahof119a262016-08-19 15:54:22 +03002350 case EOpLog:
2351 // For log(x), results are undefined if x <= 0, we are choosing to set result to 0.
2352 if (operandArray[i].getFConst() <= 0.0f)
2353 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2354 diagnostics, &resultArray[i]);
2355 else
2356 foldFloatTypeUnary(operandArray[i], &logf, &resultArray[i]);
2357 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302358
Olli Etuahof119a262016-08-19 15:54:22 +03002359 case EOpExp2:
2360 foldFloatTypeUnary(operandArray[i], &exp2f, &resultArray[i]);
2361 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302362
Olli Etuahof119a262016-08-19 15:54:22 +03002363 case EOpLog2:
2364 // For log2(x), results are undefined if x <= 0, we are choosing to set result to 0.
2365 // And log2f is not available on some plarforms like old android, so just using
2366 // log(x)/log(2) here.
2367 if (operandArray[i].getFConst() <= 0.0f)
2368 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2369 diagnostics, &resultArray[i]);
2370 else
2371 {
2372 foldFloatTypeUnary(operandArray[i], &logf, &resultArray[i]);
2373 resultArray[i].setFConst(resultArray[i].getFConst() / logf(2.0f));
2374 }
2375 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302376
Olli Etuahof119a262016-08-19 15:54:22 +03002377 case EOpSqrt:
2378 // For sqrt(x), results are undefined if x < 0, we are choosing to set result to 0.
2379 if (operandArray[i].getFConst() < 0.0f)
2380 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2381 diagnostics, &resultArray[i]);
2382 else
2383 foldFloatTypeUnary(operandArray[i], &sqrtf, &resultArray[i]);
2384 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302385
Olli Etuahof119a262016-08-19 15:54:22 +03002386 case EOpInverseSqrt:
2387 // There is no stdlib built-in function equavalent for GLES built-in inversesqrt(),
2388 // so getting the square root first using builtin function sqrt() and then taking
2389 // its inverse.
2390 // Also, for inversesqrt(x), results are undefined if x <= 0, we are choosing to set
2391 // result to 0.
2392 if (operandArray[i].getFConst() <= 0.0f)
2393 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2394 diagnostics, &resultArray[i]);
2395 else
2396 {
2397 foldFloatTypeUnary(operandArray[i], &sqrtf, &resultArray[i]);
2398 resultArray[i].setFConst(1.0f / resultArray[i].getFConst());
2399 }
2400 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302401
Olli Etuahod68924e2017-01-02 17:34:40 +00002402 case EOpLogicalNotComponentWise:
Olli Etuahof119a262016-08-19 15:54:22 +03002403 ASSERT(getType().getBasicType() == EbtBool);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302404 resultArray[i].setBConst(!operandArray[i].getBConst());
2405 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302406
Olli Etuahof119a262016-08-19 15:54:22 +03002407 case EOpNormalize:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302408 {
Olli Etuahof119a262016-08-19 15:54:22 +03002409 ASSERT(getType().getBasicType() == EbtFloat);
2410 float x = operandArray[i].getFConst();
Arun Patoleab2b9a22015-07-06 18:27:56 +05302411 float length = VectorLength(operandArray, objectSize);
2412 if (length)
2413 resultArray[i].setFConst(x / length);
2414 else
Olli Etuahof119a262016-08-19 15:54:22 +03002415 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2416 diagnostics, &resultArray[i]);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302417 break;
2418 }
Olli Etuaho9250cb22017-01-21 10:51:27 +00002419 case EOpBitfieldReverse:
2420 {
2421 uint32_t value;
2422 if (getType().getBasicType() == EbtInt)
2423 {
2424 value = static_cast<uint32_t>(operandArray[i].getIConst());
2425 }
2426 else
2427 {
2428 ASSERT(getType().getBasicType() == EbtUInt);
2429 value = operandArray[i].getUConst();
2430 }
2431 uint32_t result = gl::BitfieldReverse(value);
2432 if (getType().getBasicType() == EbtInt)
2433 {
2434 resultArray[i].setIConst(static_cast<int32_t>(result));
2435 }
2436 else
2437 {
2438 resultArray[i].setUConst(result);
2439 }
2440 break;
2441 }
2442 case EOpBitCount:
2443 {
2444 uint32_t value;
2445 if (getType().getBasicType() == EbtInt)
2446 {
2447 value = static_cast<uint32_t>(operandArray[i].getIConst());
2448 }
2449 else
2450 {
2451 ASSERT(getType().getBasicType() == EbtUInt);
2452 value = operandArray[i].getUConst();
2453 }
2454 int result = gl::BitCount(value);
2455 resultArray[i].setIConst(result);
2456 break;
2457 }
2458 case EOpFindLSB:
2459 {
2460 uint32_t value;
2461 if (getType().getBasicType() == EbtInt)
2462 {
2463 value = static_cast<uint32_t>(operandArray[i].getIConst());
2464 }
2465 else
2466 {
2467 ASSERT(getType().getBasicType() == EbtUInt);
2468 value = operandArray[i].getUConst();
2469 }
2470 resultArray[i].setIConst(gl::FindLSB(value));
2471 break;
2472 }
2473 case EOpFindMSB:
2474 {
2475 uint32_t value;
2476 if (getType().getBasicType() == EbtInt)
2477 {
2478 int intValue = operandArray[i].getIConst();
2479 value = static_cast<uint32_t>(intValue);
2480 if (intValue < 0)
2481 {
2482 // Look for zero instead of one in value. This also handles the intValue ==
2483 // -1 special case, where the return value needs to be -1.
2484 value = ~value;
2485 }
2486 }
2487 else
2488 {
2489 ASSERT(getType().getBasicType() == EbtUInt);
2490 value = operandArray[i].getUConst();
2491 }
2492 resultArray[i].setIConst(gl::FindMSB(value));
2493 break;
2494 }
Olli Etuahof119a262016-08-19 15:54:22 +03002495 case EOpDFdx:
2496 case EOpDFdy:
2497 case EOpFwidth:
2498 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole0c5409f2015-07-08 15:17:53 +05302499 // Derivatives of constant arguments should be 0.
2500 resultArray[i].setFConst(0.0f);
2501 break;
Arun Patole0c5409f2015-07-08 15:17:53 +05302502
Olli Etuahof119a262016-08-19 15:54:22 +03002503 default:
2504 return nullptr;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302505 }
Arun Patole9d0b1f92015-05-20 14:27:17 +05302506 }
Jamie Madillb1a85f42014-08-19 15:23:24 -04002507
Arun Patoleab2b9a22015-07-06 18:27:56 +05302508 return resultArray;
Jamie Madillb1a85f42014-08-19 15:23:24 -04002509}
2510
Olli Etuahof119a262016-08-19 15:54:22 +03002511void TIntermConstantUnion::foldFloatTypeUnary(const TConstantUnion &parameter,
2512 FloatTypeUnaryFunc builtinFunc,
2513 TConstantUnion *result) const
Arun Patole9dea48f2015-04-02 11:45:09 +05302514{
2515 ASSERT(builtinFunc);
2516
Olli Etuahof119a262016-08-19 15:54:22 +03002517 ASSERT(getType().getBasicType() == EbtFloat);
2518 result->setFConst(builtinFunc(parameter.getFConst()));
Arun Patole9dea48f2015-04-02 11:45:09 +05302519}
2520
Jamie Madillb1a85f42014-08-19 15:23:24 -04002521// static
Olli Etuahof119a262016-08-19 15:54:22 +03002522TConstantUnion *TIntermConstantUnion::FoldAggregateConstructor(TIntermAggregate *aggregate)
Olli Etuaho1d122782015-11-06 15:35:17 +02002523{
2524 ASSERT(aggregate->getSequence()->size() > 0u);
2525 size_t resultSize = aggregate->getType().getObjectSize();
2526 TConstantUnion *resultArray = new TConstantUnion[resultSize];
2527 TBasicType basicType = aggregate->getBasicType();
2528
2529 size_t resultIndex = 0u;
2530
2531 if (aggregate->getSequence()->size() == 1u)
2532 {
2533 TIntermNode *argument = aggregate->getSequence()->front();
2534 TIntermConstantUnion *argumentConstant = argument->getAsConstantUnion();
2535 const TConstantUnion *argumentUnionArray = argumentConstant->getUnionArrayPointer();
2536 // Check the special case of constructing a matrix diagonal from a single scalar,
2537 // or a vector from a single scalar.
2538 if (argumentConstant->getType().getObjectSize() == 1u)
2539 {
2540 if (aggregate->isMatrix())
2541 {
2542 int resultCols = aggregate->getType().getCols();
2543 int resultRows = aggregate->getType().getRows();
2544 for (int col = 0; col < resultCols; ++col)
2545 {
2546 for (int row = 0; row < resultRows; ++row)
2547 {
2548 if (col == row)
2549 {
2550 resultArray[resultIndex].cast(basicType, argumentUnionArray[0]);
2551 }
2552 else
2553 {
2554 resultArray[resultIndex].setFConst(0.0f);
2555 }
2556 ++resultIndex;
2557 }
2558 }
2559 }
2560 else
2561 {
2562 while (resultIndex < resultSize)
2563 {
2564 resultArray[resultIndex].cast(basicType, argumentUnionArray[0]);
2565 ++resultIndex;
2566 }
2567 }
2568 ASSERT(resultIndex == resultSize);
2569 return resultArray;
2570 }
2571 else if (aggregate->isMatrix() && argumentConstant->isMatrix())
2572 {
2573 // The special case of constructing a matrix from a matrix.
2574 int argumentCols = argumentConstant->getType().getCols();
2575 int argumentRows = argumentConstant->getType().getRows();
2576 int resultCols = aggregate->getType().getCols();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002577 int resultRows = aggregate->getType().getRows();
Olli Etuaho1d122782015-11-06 15:35:17 +02002578 for (int col = 0; col < resultCols; ++col)
2579 {
2580 for (int row = 0; row < resultRows; ++row)
2581 {
2582 if (col < argumentCols && row < argumentRows)
2583 {
2584 resultArray[resultIndex].cast(basicType,
2585 argumentUnionArray[col * argumentRows + row]);
2586 }
2587 else if (col == row)
2588 {
2589 resultArray[resultIndex].setFConst(1.0f);
2590 }
2591 else
2592 {
2593 resultArray[resultIndex].setFConst(0.0f);
2594 }
2595 ++resultIndex;
2596 }
2597 }
2598 ASSERT(resultIndex == resultSize);
2599 return resultArray;
2600 }
2601 }
2602
2603 for (TIntermNode *&argument : *aggregate->getSequence())
2604 {
2605 TIntermConstantUnion *argumentConstant = argument->getAsConstantUnion();
2606 size_t argumentSize = argumentConstant->getType().getObjectSize();
2607 const TConstantUnion *argumentUnionArray = argumentConstant->getUnionArrayPointer();
2608 for (size_t i = 0u; i < argumentSize; ++i)
2609 {
2610 if (resultIndex >= resultSize)
2611 break;
2612 resultArray[resultIndex].cast(basicType, argumentUnionArray[i]);
2613 ++resultIndex;
2614 }
2615 }
2616 ASSERT(resultIndex == resultSize);
2617 return resultArray;
2618}
2619
2620// static
Olli Etuahof119a262016-08-19 15:54:22 +03002621TConstantUnion *TIntermConstantUnion::FoldAggregateBuiltIn(TIntermAggregate *aggregate,
2622 TDiagnostics *diagnostics)
Arun Patole274f0702015-05-05 13:33:30 +05302623{
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002624 TOperator op = aggregate->getOp();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002625 TIntermSequence *arguments = aggregate->getSequence();
2626 unsigned int argsCount = static_cast<unsigned int>(arguments->size());
2627 std::vector<const TConstantUnion *> unionArrays(argsCount);
2628 std::vector<size_t> objectSizes(argsCount);
Olli Etuahob43846e2015-06-02 18:18:57 +03002629 size_t maxObjectSize = 0;
Arun Patole274f0702015-05-05 13:33:30 +05302630 TBasicType basicType = EbtVoid;
2631 TSourceLoc loc;
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002632 for (unsigned int i = 0; i < argsCount; i++)
Arun Patole274f0702015-05-05 13:33:30 +05302633 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002634 TIntermConstantUnion *argConstant = (*arguments)[i]->getAsConstantUnion();
2635 ASSERT(argConstant != nullptr); // Should be checked already.
Arun Patole274f0702015-05-05 13:33:30 +05302636
2637 if (i == 0)
2638 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002639 basicType = argConstant->getType().getBasicType();
2640 loc = argConstant->getLine();
Arun Patole274f0702015-05-05 13:33:30 +05302641 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002642 unionArrays[i] = argConstant->getUnionArrayPointer();
2643 objectSizes[i] = argConstant->getType().getObjectSize();
Olli Etuahob43846e2015-06-02 18:18:57 +03002644 if (objectSizes[i] > maxObjectSize)
2645 maxObjectSize = objectSizes[i];
Arun Patole274f0702015-05-05 13:33:30 +05302646 }
2647
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002648 if (!(*arguments)[0]->getAsTyped()->isMatrix() && aggregate->getOp() != EOpOuterProduct)
Arun Patole7fa33552015-06-10 15:15:18 +05302649 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002650 for (unsigned int i = 0; i < argsCount; i++)
Arun Patole7fa33552015-06-10 15:15:18 +05302651 if (objectSizes[i] != maxObjectSize)
2652 unionArrays[i] = Vectorize(*unionArrays[i], maxObjectSize);
2653 }
Arun Patole274f0702015-05-05 13:33:30 +05302654
Olli Etuahob43846e2015-06-02 18:18:57 +03002655 TConstantUnion *resultArray = nullptr;
Olli Etuaho51182ab2017-01-22 00:12:29 +00002656
2657 switch (op)
Arun Patole274f0702015-05-05 13:33:30 +05302658 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002659 case EOpAtan:
Arun Patole274f0702015-05-05 13:33:30 +05302660 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002661 ASSERT(basicType == EbtFloat);
2662 resultArray = new TConstantUnion[maxObjectSize];
2663 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302664 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002665 float y = unionArrays[0][i].getFConst();
2666 float x = unionArrays[1][i].getFConst();
2667 // Results are undefined if x and y are both 0.
2668 if (x == 0.0f && y == 0.0f)
2669 UndefinedConstantFoldingError(loc, op, basicType, diagnostics, &resultArray[i]);
2670 else
2671 resultArray[i].setFConst(atan2f(y, x));
Arun Patolebf790422015-05-18 17:53:04 +05302672 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002673 break;
2674 }
Arun Patolebf790422015-05-18 17:53:04 +05302675
Olli Etuaho51182ab2017-01-22 00:12:29 +00002676 case EOpPow:
2677 {
2678 ASSERT(basicType == EbtFloat);
2679 resultArray = new TConstantUnion[maxObjectSize];
2680 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302681 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002682 float x = unionArrays[0][i].getFConst();
2683 float y = unionArrays[1][i].getFConst();
2684 // Results are undefined if x < 0.
2685 // Results are undefined if x = 0 and y <= 0.
2686 if (x < 0.0f)
2687 UndefinedConstantFoldingError(loc, op, basicType, diagnostics, &resultArray[i]);
2688 else if (x == 0.0f && y <= 0.0f)
2689 UndefinedConstantFoldingError(loc, op, basicType, diagnostics, &resultArray[i]);
2690 else
2691 resultArray[i].setFConst(powf(x, y));
Arun Patolebf790422015-05-18 17:53:04 +05302692 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002693 break;
2694 }
Arun Patolebf790422015-05-18 17:53:04 +05302695
Olli Etuaho51182ab2017-01-22 00:12:29 +00002696 case EOpMod:
2697 {
2698 ASSERT(basicType == EbtFloat);
2699 resultArray = new TConstantUnion[maxObjectSize];
2700 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302701 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002702 float x = unionArrays[0][i].getFConst();
2703 float y = unionArrays[1][i].getFConst();
2704 resultArray[i].setFConst(x - y * floorf(x / y));
Arun Patolebf790422015-05-18 17:53:04 +05302705 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002706 break;
2707 }
Arun Patolebf790422015-05-18 17:53:04 +05302708
Olli Etuaho51182ab2017-01-22 00:12:29 +00002709 case EOpMin:
2710 {
2711 resultArray = new TConstantUnion[maxObjectSize];
2712 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patole274f0702015-05-05 13:33:30 +05302713 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002714 switch (basicType)
Arun Patole274f0702015-05-05 13:33:30 +05302715 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002716 case EbtFloat:
2717 resultArray[i].setFConst(
2718 std::min(unionArrays[0][i].getFConst(), unionArrays[1][i].getFConst()));
2719 break;
2720 case EbtInt:
2721 resultArray[i].setIConst(
2722 std::min(unionArrays[0][i].getIConst(), unionArrays[1][i].getIConst()));
2723 break;
2724 case EbtUInt:
2725 resultArray[i].setUConst(
2726 std::min(unionArrays[0][i].getUConst(), unionArrays[1][i].getUConst()));
2727 break;
2728 default:
2729 UNREACHABLE();
2730 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302731 }
2732 }
2733 break;
Arun Patole274f0702015-05-05 13:33:30 +05302734 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002735
2736 case EOpMax:
Arun Patole274f0702015-05-05 13:33:30 +05302737 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002738 resultArray = new TConstantUnion[maxObjectSize];
2739 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patole274f0702015-05-05 13:33:30 +05302740 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002741 switch (basicType)
Arun Patole274f0702015-05-05 13:33:30 +05302742 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002743 case EbtFloat:
2744 resultArray[i].setFConst(
2745 std::max(unionArrays[0][i].getFConst(), unionArrays[1][i].getFConst()));
2746 break;
2747 case EbtInt:
2748 resultArray[i].setIConst(
2749 std::max(unionArrays[0][i].getIConst(), unionArrays[1][i].getIConst()));
2750 break;
2751 case EbtUInt:
2752 resultArray[i].setUConst(
2753 std::max(unionArrays[0][i].getUConst(), unionArrays[1][i].getUConst()));
2754 break;
2755 default:
2756 UNREACHABLE();
2757 break;
Arun Patole274f0702015-05-05 13:33:30 +05302758 }
2759 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002760 break;
Arun Patole274f0702015-05-05 13:33:30 +05302761 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002762
2763 case EOpStep:
2764 {
2765 ASSERT(basicType == EbtFloat);
2766 resultArray = new TConstantUnion[maxObjectSize];
2767 for (size_t i = 0; i < maxObjectSize; i++)
2768 resultArray[i].setFConst(
2769 unionArrays[1][i].getFConst() < unionArrays[0][i].getFConst() ? 0.0f : 1.0f);
2770 break;
2771 }
2772
2773 case EOpLessThanComponentWise:
2774 {
2775 resultArray = new TConstantUnion[maxObjectSize];
2776 for (size_t i = 0; i < maxObjectSize; i++)
2777 {
2778 switch (basicType)
2779 {
2780 case EbtFloat:
2781 resultArray[i].setBConst(unionArrays[0][i].getFConst() <
2782 unionArrays[1][i].getFConst());
2783 break;
2784 case EbtInt:
2785 resultArray[i].setBConst(unionArrays[0][i].getIConst() <
2786 unionArrays[1][i].getIConst());
2787 break;
2788 case EbtUInt:
2789 resultArray[i].setBConst(unionArrays[0][i].getUConst() <
2790 unionArrays[1][i].getUConst());
2791 break;
2792 default:
2793 UNREACHABLE();
2794 break;
2795 }
2796 }
2797 break;
2798 }
2799
2800 case EOpLessThanEqualComponentWise:
2801 {
2802 resultArray = new TConstantUnion[maxObjectSize];
2803 for (size_t i = 0; i < maxObjectSize; i++)
2804 {
2805 switch (basicType)
2806 {
2807 case EbtFloat:
2808 resultArray[i].setBConst(unionArrays[0][i].getFConst() <=
2809 unionArrays[1][i].getFConst());
2810 break;
2811 case EbtInt:
2812 resultArray[i].setBConst(unionArrays[0][i].getIConst() <=
2813 unionArrays[1][i].getIConst());
2814 break;
2815 case EbtUInt:
2816 resultArray[i].setBConst(unionArrays[0][i].getUConst() <=
2817 unionArrays[1][i].getUConst());
2818 break;
2819 default:
2820 UNREACHABLE();
2821 break;
2822 }
2823 }
2824 break;
2825 }
2826
2827 case EOpGreaterThanComponentWise:
2828 {
2829 resultArray = new TConstantUnion[maxObjectSize];
2830 for (size_t i = 0; i < maxObjectSize; i++)
2831 {
2832 switch (basicType)
2833 {
2834 case EbtFloat:
2835 resultArray[i].setBConst(unionArrays[0][i].getFConst() >
2836 unionArrays[1][i].getFConst());
2837 break;
2838 case EbtInt:
2839 resultArray[i].setBConst(unionArrays[0][i].getIConst() >
2840 unionArrays[1][i].getIConst());
2841 break;
2842 case EbtUInt:
2843 resultArray[i].setBConst(unionArrays[0][i].getUConst() >
2844 unionArrays[1][i].getUConst());
2845 break;
2846 default:
2847 UNREACHABLE();
2848 break;
2849 }
2850 }
2851 break;
2852 }
2853 case EOpGreaterThanEqualComponentWise:
2854 {
2855 resultArray = new TConstantUnion[maxObjectSize];
2856 for (size_t i = 0; i < maxObjectSize; i++)
2857 {
2858 switch (basicType)
2859 {
2860 case EbtFloat:
2861 resultArray[i].setBConst(unionArrays[0][i].getFConst() >=
2862 unionArrays[1][i].getFConst());
2863 break;
2864 case EbtInt:
2865 resultArray[i].setBConst(unionArrays[0][i].getIConst() >=
2866 unionArrays[1][i].getIConst());
2867 break;
2868 case EbtUInt:
2869 resultArray[i].setBConst(unionArrays[0][i].getUConst() >=
2870 unionArrays[1][i].getUConst());
2871 break;
2872 default:
2873 UNREACHABLE();
2874 break;
2875 }
2876 }
2877 }
2878 break;
2879
2880 case EOpEqualComponentWise:
2881 {
2882 resultArray = new TConstantUnion[maxObjectSize];
2883 for (size_t i = 0; i < maxObjectSize; i++)
2884 {
2885 switch (basicType)
2886 {
2887 case EbtFloat:
2888 resultArray[i].setBConst(unionArrays[0][i].getFConst() ==
2889 unionArrays[1][i].getFConst());
2890 break;
2891 case EbtInt:
2892 resultArray[i].setBConst(unionArrays[0][i].getIConst() ==
2893 unionArrays[1][i].getIConst());
2894 break;
2895 case EbtUInt:
2896 resultArray[i].setBConst(unionArrays[0][i].getUConst() ==
2897 unionArrays[1][i].getUConst());
2898 break;
2899 case EbtBool:
2900 resultArray[i].setBConst(unionArrays[0][i].getBConst() ==
2901 unionArrays[1][i].getBConst());
2902 break;
2903 default:
2904 UNREACHABLE();
2905 break;
2906 }
2907 }
2908 break;
2909 }
2910
2911 case EOpNotEqualComponentWise:
2912 {
2913 resultArray = new TConstantUnion[maxObjectSize];
2914 for (size_t i = 0; i < maxObjectSize; i++)
2915 {
2916 switch (basicType)
2917 {
2918 case EbtFloat:
2919 resultArray[i].setBConst(unionArrays[0][i].getFConst() !=
2920 unionArrays[1][i].getFConst());
2921 break;
2922 case EbtInt:
2923 resultArray[i].setBConst(unionArrays[0][i].getIConst() !=
2924 unionArrays[1][i].getIConst());
2925 break;
2926 case EbtUInt:
2927 resultArray[i].setBConst(unionArrays[0][i].getUConst() !=
2928 unionArrays[1][i].getUConst());
2929 break;
2930 case EbtBool:
2931 resultArray[i].setBConst(unionArrays[0][i].getBConst() !=
2932 unionArrays[1][i].getBConst());
2933 break;
2934 default:
2935 UNREACHABLE();
2936 break;
2937 }
2938 }
2939 break;
2940 }
2941
2942 case EOpDistance:
2943 {
2944 ASSERT(basicType == EbtFloat);
2945 TConstantUnion *distanceArray = new TConstantUnion[maxObjectSize];
2946 resultArray = new TConstantUnion();
2947 for (size_t i = 0; i < maxObjectSize; i++)
2948 {
2949 float x = unionArrays[0][i].getFConst();
2950 float y = unionArrays[1][i].getFConst();
2951 distanceArray[i].setFConst(x - y);
2952 }
2953 resultArray->setFConst(VectorLength(distanceArray, maxObjectSize));
2954 break;
2955 }
2956
2957 case EOpDot:
2958 ASSERT(basicType == EbtFloat);
2959 resultArray = new TConstantUnion();
2960 resultArray->setFConst(VectorDotProduct(unionArrays[0], unionArrays[1], maxObjectSize));
2961 break;
2962
2963 case EOpCross:
2964 {
2965 ASSERT(basicType == EbtFloat && maxObjectSize == 3);
2966 resultArray = new TConstantUnion[maxObjectSize];
2967 float x0 = unionArrays[0][0].getFConst();
2968 float x1 = unionArrays[0][1].getFConst();
2969 float x2 = unionArrays[0][2].getFConst();
2970 float y0 = unionArrays[1][0].getFConst();
2971 float y1 = unionArrays[1][1].getFConst();
2972 float y2 = unionArrays[1][2].getFConst();
2973 resultArray[0].setFConst(x1 * y2 - y1 * x2);
2974 resultArray[1].setFConst(x2 * y0 - y2 * x0);
2975 resultArray[2].setFConst(x0 * y1 - y0 * x1);
2976 break;
2977 }
2978
2979 case EOpReflect:
2980 {
2981 ASSERT(basicType == EbtFloat);
2982 // genType reflect (genType I, genType N) :
2983 // For the incident vector I and surface orientation N, returns the reflection
2984 // direction:
2985 // I - 2 * dot(N, I) * N.
2986 resultArray = new TConstantUnion[maxObjectSize];
2987 float dotProduct = VectorDotProduct(unionArrays[1], unionArrays[0], maxObjectSize);
2988 for (size_t i = 0; i < maxObjectSize; i++)
2989 {
2990 float result = unionArrays[0][i].getFConst() -
2991 2.0f * dotProduct * unionArrays[1][i].getFConst();
2992 resultArray[i].setFConst(result);
2993 }
2994 break;
2995 }
2996
2997 case EOpMulMatrixComponentWise:
2998 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002999 ASSERT(basicType == EbtFloat && (*arguments)[0]->getAsTyped()->isMatrix() &&
3000 (*arguments)[1]->getAsTyped()->isMatrix());
Olli Etuaho51182ab2017-01-22 00:12:29 +00003001 // Perform component-wise matrix multiplication.
3002 resultArray = new TConstantUnion[maxObjectSize];
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003003 int size = (*arguments)[0]->getAsTyped()->getNominalSize();
Olli Etuaho51182ab2017-01-22 00:12:29 +00003004 angle::Matrix<float> result =
3005 GetMatrix(unionArrays[0], size).compMult(GetMatrix(unionArrays[1], size));
3006 SetUnionArrayFromMatrix(result, resultArray);
3007 break;
3008 }
3009
3010 case EOpOuterProduct:
3011 {
3012 ASSERT(basicType == EbtFloat);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003013 size_t numRows = (*arguments)[0]->getAsTyped()->getType().getObjectSize();
3014 size_t numCols = (*arguments)[1]->getAsTyped()->getType().getObjectSize();
Olli Etuaho51182ab2017-01-22 00:12:29 +00003015 resultArray = new TConstantUnion[numRows * numCols];
3016 angle::Matrix<float> result =
3017 GetMatrix(unionArrays[0], static_cast<int>(numRows), 1)
3018 .outerProduct(GetMatrix(unionArrays[1], 1, static_cast<int>(numCols)));
3019 SetUnionArrayFromMatrix(result, resultArray);
3020 break;
3021 }
3022
3023 case EOpClamp:
3024 {
3025 resultArray = new TConstantUnion[maxObjectSize];
3026 for (size_t i = 0; i < maxObjectSize; i++)
3027 {
3028 switch (basicType)
3029 {
3030 case EbtFloat:
3031 {
3032 float x = unionArrays[0][i].getFConst();
3033 float min = unionArrays[1][i].getFConst();
3034 float max = unionArrays[2][i].getFConst();
3035 // Results are undefined if min > max.
3036 if (min > max)
3037 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
3038 &resultArray[i]);
3039 else
3040 resultArray[i].setFConst(gl::clamp(x, min, max));
3041 break;
3042 }
3043
3044 case EbtInt:
3045 {
3046 int x = unionArrays[0][i].getIConst();
3047 int min = unionArrays[1][i].getIConst();
3048 int max = unionArrays[2][i].getIConst();
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].setIConst(gl::clamp(x, min, max));
3055 break;
3056 }
3057 case EbtUInt:
3058 {
3059 unsigned int x = unionArrays[0][i].getUConst();
3060 unsigned int min = unionArrays[1][i].getUConst();
3061 unsigned int max = unionArrays[2][i].getUConst();
3062 // Results are undefined if min > max.
3063 if (min > max)
3064 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
3065 &resultArray[i]);
3066 else
3067 resultArray[i].setUConst(gl::clamp(x, min, max));
3068 break;
3069 }
3070 default:
3071 UNREACHABLE();
3072 break;
3073 }
3074 }
3075 break;
3076 }
3077
3078 case EOpMix:
3079 {
3080 ASSERT(basicType == EbtFloat);
3081 resultArray = new TConstantUnion[maxObjectSize];
3082 for (size_t i = 0; i < maxObjectSize; i++)
3083 {
3084 float x = unionArrays[0][i].getFConst();
3085 float y = unionArrays[1][i].getFConst();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003086 TBasicType type = (*arguments)[2]->getAsTyped()->getType().getBasicType();
Olli Etuaho51182ab2017-01-22 00:12:29 +00003087 if (type == EbtFloat)
3088 {
3089 // Returns the linear blend of x and y, i.e., x * (1 - a) + y * a.
3090 float a = unionArrays[2][i].getFConst();
3091 resultArray[i].setFConst(x * (1.0f - a) + y * a);
3092 }
3093 else // 3rd parameter is EbtBool
3094 {
3095 ASSERT(type == EbtBool);
3096 // Selects which vector each returned component comes from.
3097 // For a component of a that is false, the corresponding component of x is
3098 // returned.
3099 // For a component of a that is true, the corresponding component of y is
3100 // returned.
3101 bool a = unionArrays[2][i].getBConst();
3102 resultArray[i].setFConst(a ? y : x);
3103 }
3104 }
3105 break;
3106 }
3107
3108 case EOpSmoothStep:
3109 {
3110 ASSERT(basicType == EbtFloat);
3111 resultArray = new TConstantUnion[maxObjectSize];
3112 for (size_t i = 0; i < maxObjectSize; i++)
3113 {
3114 float edge0 = unionArrays[0][i].getFConst();
3115 float edge1 = unionArrays[1][i].getFConst();
3116 float x = unionArrays[2][i].getFConst();
3117 // Results are undefined if edge0 >= edge1.
3118 if (edge0 >= edge1)
3119 {
3120 UndefinedConstantFoldingError(loc, op, basicType, diagnostics, &resultArray[i]);
3121 }
3122 else
3123 {
3124 // Returns 0.0 if x <= edge0 and 1.0 if x >= edge1 and performs smooth
3125 // Hermite interpolation between 0 and 1 when edge0 < x < edge1.
3126 float t = gl::clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
3127 resultArray[i].setFConst(t * t * (3.0f - 2.0f * t));
3128 }
3129 }
3130 break;
3131 }
3132
Olli Etuaho74da73f2017-02-01 15:37:48 +00003133 case EOpLdexp:
3134 {
3135 resultArray = new TConstantUnion[maxObjectSize];
3136 for (size_t i = 0; i < maxObjectSize; i++)
3137 {
3138 float x = unionArrays[0][i].getFConst();
3139 int exp = unionArrays[1][i].getIConst();
3140 if (exp > 128)
3141 {
3142 UndefinedConstantFoldingError(loc, op, basicType, diagnostics, &resultArray[i]);
3143 }
3144 else
3145 {
3146 resultArray[i].setFConst(gl::Ldexp(x, exp));
3147 }
3148 }
3149 break;
3150 }
3151
Jamie Madille72595b2017-06-06 15:12:26 -04003152 case EOpFaceforward:
Olli Etuaho51182ab2017-01-22 00:12:29 +00003153 {
3154 ASSERT(basicType == EbtFloat);
3155 // genType faceforward(genType N, genType I, genType Nref) :
3156 // If dot(Nref, I) < 0 return N, otherwise return -N.
3157 resultArray = new TConstantUnion[maxObjectSize];
3158 float dotProduct = VectorDotProduct(unionArrays[2], unionArrays[1], maxObjectSize);
3159 for (size_t i = 0; i < maxObjectSize; i++)
3160 {
3161 if (dotProduct < 0)
3162 resultArray[i].setFConst(unionArrays[0][i].getFConst());
3163 else
3164 resultArray[i].setFConst(-unionArrays[0][i].getFConst());
3165 }
3166 break;
3167 }
3168
3169 case EOpRefract:
3170 {
3171 ASSERT(basicType == EbtFloat);
3172 // genType refract(genType I, genType N, float eta) :
3173 // For the incident vector I and surface normal N, and the ratio of indices of
3174 // refraction eta,
3175 // return the refraction vector. The result is computed by
3176 // k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I))
3177 // if (k < 0.0)
3178 // return genType(0.0)
3179 // else
3180 // return eta * I - (eta * dot(N, I) + sqrt(k)) * N
3181 resultArray = new TConstantUnion[maxObjectSize];
3182 float dotProduct = VectorDotProduct(unionArrays[1], unionArrays[0], maxObjectSize);
3183 for (size_t i = 0; i < maxObjectSize; i++)
3184 {
3185 float eta = unionArrays[2][i].getFConst();
3186 float k = 1.0f - eta * eta * (1.0f - dotProduct * dotProduct);
3187 if (k < 0.0f)
3188 resultArray[i].setFConst(0.0f);
3189 else
3190 resultArray[i].setFConst(eta * unionArrays[0][i].getFConst() -
3191 (eta * dotProduct + sqrtf(k)) *
3192 unionArrays[1][i].getFConst());
3193 }
3194 break;
3195 }
Olli Etuaho9250cb22017-01-21 10:51:27 +00003196 case EOpBitfieldExtract:
3197 {
3198 resultArray = new TConstantUnion[maxObjectSize];
3199 for (size_t i = 0; i < maxObjectSize; ++i)
3200 {
3201 int offset = unionArrays[1][0].getIConst();
3202 int bits = unionArrays[2][0].getIConst();
3203 if (bits == 0)
3204 {
3205 if (aggregate->getBasicType() == EbtInt)
3206 {
3207 resultArray[i].setIConst(0);
3208 }
3209 else
3210 {
3211 ASSERT(aggregate->getBasicType() == EbtUInt);
3212 resultArray[i].setUConst(0);
3213 }
3214 }
3215 else if (offset < 0 || bits < 0 || offset >= 32 || bits > 32 || offset + bits > 32)
3216 {
3217 UndefinedConstantFoldingError(loc, op, aggregate->getBasicType(), diagnostics,
3218 &resultArray[i]);
3219 }
3220 else
3221 {
3222 // bits can be 32 here, so we need to avoid bit shift overflow.
3223 uint32_t maskMsb = 1u << (bits - 1);
3224 uint32_t mask = ((maskMsb - 1u) | maskMsb) << offset;
3225 if (aggregate->getBasicType() == EbtInt)
3226 {
3227 uint32_t value = static_cast<uint32_t>(unionArrays[0][i].getIConst());
3228 uint32_t resultUnsigned = (value & mask) >> offset;
3229 if ((resultUnsigned & maskMsb) != 0)
3230 {
3231 // The most significant bits (from bits+1 to the most significant bit)
3232 // should be set to 1.
3233 uint32_t higherBitsMask = ((1u << (32 - bits)) - 1u) << bits;
3234 resultUnsigned |= higherBitsMask;
3235 }
3236 resultArray[i].setIConst(static_cast<int32_t>(resultUnsigned));
3237 }
3238 else
3239 {
3240 ASSERT(aggregate->getBasicType() == EbtUInt);
3241 uint32_t value = unionArrays[0][i].getUConst();
3242 resultArray[i].setUConst((value & mask) >> offset);
3243 }
3244 }
3245 }
3246 break;
3247 }
3248 case EOpBitfieldInsert:
3249 {
3250 resultArray = new TConstantUnion[maxObjectSize];
3251 for (size_t i = 0; i < maxObjectSize; ++i)
3252 {
3253 int offset = unionArrays[2][0].getIConst();
3254 int bits = unionArrays[3][0].getIConst();
3255 if (bits == 0)
3256 {
3257 if (aggregate->getBasicType() == EbtInt)
3258 {
3259 int32_t base = unionArrays[0][i].getIConst();
3260 resultArray[i].setIConst(base);
3261 }
3262 else
3263 {
3264 ASSERT(aggregate->getBasicType() == EbtUInt);
3265 uint32_t base = unionArrays[0][i].getUConst();
3266 resultArray[i].setUConst(base);
3267 }
3268 }
3269 else if (offset < 0 || bits < 0 || offset >= 32 || bits > 32 || offset + bits > 32)
3270 {
3271 UndefinedConstantFoldingError(loc, op, aggregate->getBasicType(), diagnostics,
3272 &resultArray[i]);
3273 }
3274 else
3275 {
3276 // bits can be 32 here, so we need to avoid bit shift overflow.
3277 uint32_t maskMsb = 1u << (bits - 1);
3278 uint32_t insertMask = ((maskMsb - 1u) | maskMsb) << offset;
3279 uint32_t baseMask = ~insertMask;
3280 if (aggregate->getBasicType() == EbtInt)
3281 {
3282 uint32_t base = static_cast<uint32_t>(unionArrays[0][i].getIConst());
3283 uint32_t insert = static_cast<uint32_t>(unionArrays[1][i].getIConst());
3284 uint32_t resultUnsigned =
3285 (base & baseMask) | ((insert << offset) & insertMask);
3286 resultArray[i].setIConst(static_cast<int32_t>(resultUnsigned));
3287 }
3288 else
3289 {
3290 ASSERT(aggregate->getBasicType() == EbtUInt);
3291 uint32_t base = unionArrays[0][i].getUConst();
3292 uint32_t insert = unionArrays[1][i].getUConst();
3293 resultArray[i].setUConst((base & baseMask) |
3294 ((insert << offset) & insertMask));
3295 }
3296 }
3297 }
3298 break;
3299 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00003300
3301 default:
3302 UNREACHABLE();
3303 return nullptr;
Arun Patole274f0702015-05-05 13:33:30 +05303304 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003305 return resultArray;
Arun Patole274f0702015-05-05 13:33:30 +05303306}
3307
Jamie Madill45bcc782016-11-07 13:58:48 -05003308} // namespace sh