blob: bf8649c27821763988eeec7734bbf19807b694f1 [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>
13#include <algorithm>
14
15#include "compiler/translator/HashNames.h"
16#include "compiler/translator/IntermNode.h"
17#include "compiler/translator/SymbolTable.h"
18
19namespace
20{
21
22TPrecision GetHigherPrecision(TPrecision left, TPrecision right)
23{
24 return left > right ? left : right;
25}
26
27bool ValidateMultiplication(TOperator op, const TType &left, const TType &right)
28{
29 switch (op)
30 {
31 case EOpMul:
32 case EOpMulAssign:
33 return left.getNominalSize() == right.getNominalSize() &&
34 left.getSecondarySize() == right.getSecondarySize();
35 case EOpVectorTimesScalar:
36 case EOpVectorTimesScalarAssign:
37 return true;
38 case EOpVectorTimesMatrix:
39 return left.getNominalSize() == right.getRows();
40 case EOpVectorTimesMatrixAssign:
41 return left.getNominalSize() == right.getRows() &&
42 left.getNominalSize() == right.getCols();
43 case EOpMatrixTimesVector:
44 return left.getCols() == right.getNominalSize();
45 case EOpMatrixTimesScalar:
46 case EOpMatrixTimesScalarAssign:
47 return true;
48 case EOpMatrixTimesMatrix:
49 return left.getCols() == right.getRows();
50 case EOpMatrixTimesMatrixAssign:
51 return left.getCols() == right.getCols() &&
52 left.getRows() == right.getRows();
53
54 default:
55 UNREACHABLE();
56 return false;
57 }
58}
59
60bool CompareStructure(const TType& leftNodeType,
61 ConstantUnion *rightUnionArray,
62 ConstantUnion *leftUnionArray);
63
64bool CompareStruct(const TType &leftNodeType,
65 ConstantUnion *rightUnionArray,
66 ConstantUnion *leftUnionArray)
67{
68 const TFieldList &fields = leftNodeType.getStruct()->fields();
69
70 size_t structSize = fields.size();
71 size_t index = 0;
72
73 for (size_t j = 0; j < structSize; j++)
74 {
75 size_t size = fields[j]->type()->getObjectSize();
76 for (size_t i = 0; i < size; i++)
77 {
78 if (fields[j]->type()->getBasicType() == EbtStruct)
79 {
80 if (!CompareStructure(*fields[j]->type(),
81 &rightUnionArray[index],
82 &leftUnionArray[index]))
83 {
84 return false;
85 }
86 }
87 else
88 {
89 if (leftUnionArray[index] != rightUnionArray[index])
90 return false;
91 index++;
92 }
93 }
94 }
95 return true;
96}
97
98bool CompareStructure(const TType &leftNodeType,
99 ConstantUnion *rightUnionArray,
100 ConstantUnion *leftUnionArray)
101{
102 if (leftNodeType.isArray())
103 {
104 TType typeWithoutArrayness = leftNodeType;
105 typeWithoutArrayness.clearArrayness();
106
107 size_t arraySize = leftNodeType.getArraySize();
108
109 for (size_t i = 0; i < arraySize; ++i)
110 {
111 size_t offset = typeWithoutArrayness.getObjectSize() * i;
112 if (!CompareStruct(typeWithoutArrayness,
113 &rightUnionArray[offset],
114 &leftUnionArray[offset]))
115 {
116 return false;
117 }
118 }
119 }
120 else
121 {
122 return CompareStruct(leftNodeType, rightUnionArray, leftUnionArray);
123 }
124 return true;
125}
126
127} // namespace anonymous
128
129
130////////////////////////////////////////////////////////////////
131//
132// Member functions of the nodes used for building the tree.
133//
134////////////////////////////////////////////////////////////////
135
136#define REPLACE_IF_IS(node, type, original, replacement) \
137 if (node == original) { \
138 node = static_cast<type *>(replacement); \
139 return true; \
140 }
141
142bool TIntermLoop::replaceChildNode(
143 TIntermNode *original, TIntermNode *replacement)
144{
145 REPLACE_IF_IS(mInit, TIntermNode, original, replacement);
146 REPLACE_IF_IS(mCond, TIntermTyped, original, replacement);
147 REPLACE_IF_IS(mExpr, TIntermTyped, original, replacement);
148 REPLACE_IF_IS(mBody, TIntermNode, original, replacement);
149 return false;
150}
151
152void TIntermLoop::enqueueChildren(std::queue<TIntermNode *> *nodeQueue) const
153{
154 if (mInit)
155 {
156 nodeQueue->push(mInit);
157 }
158 if (mCond)
159 {
160 nodeQueue->push(mCond);
161 }
162 if (mExpr)
163 {
164 nodeQueue->push(mExpr);
165 }
166 if (mBody)
167 {
168 nodeQueue->push(mBody);
169 }
170}
171
172bool TIntermBranch::replaceChildNode(
173 TIntermNode *original, TIntermNode *replacement)
174{
175 REPLACE_IF_IS(mExpression, TIntermTyped, original, replacement);
176 return false;
177}
178
179void TIntermBranch::enqueueChildren(std::queue<TIntermNode *> *nodeQueue) const
180{
181 if (mExpression)
182 {
183 nodeQueue->push(mExpression);
184 }
185}
186
187bool TIntermBinary::replaceChildNode(
188 TIntermNode *original, TIntermNode *replacement)
189{
190 REPLACE_IF_IS(mLeft, TIntermTyped, original, replacement);
191 REPLACE_IF_IS(mRight, TIntermTyped, original, replacement);
192 return false;
193}
194
195void TIntermBinary::enqueueChildren(std::queue<TIntermNode *> *nodeQueue) const
196{
197 if (mLeft)
198 {
199 nodeQueue->push(mLeft);
200 }
201 if (mRight)
202 {
203 nodeQueue->push(mRight);
204 }
205}
206
207bool TIntermUnary::replaceChildNode(
208 TIntermNode *original, TIntermNode *replacement)
209{
210 REPLACE_IF_IS(mOperand, TIntermTyped, original, replacement);
211 return false;
212}
213
214void TIntermUnary::enqueueChildren(std::queue<TIntermNode *> *nodeQueue) const
215{
216 if (mOperand)
217 {
218 nodeQueue->push(mOperand);
219 }
220}
221
222bool TIntermAggregate::replaceChildNode(
223 TIntermNode *original, TIntermNode *replacement)
224{
225 for (size_t ii = 0; ii < mSequence.size(); ++ii)
226 {
227 REPLACE_IF_IS(mSequence[ii], TIntermNode, original, replacement);
228 }
229 return false;
230}
231
232void TIntermAggregate::enqueueChildren(std::queue<TIntermNode *> *nodeQueue) const
233{
234 for (size_t childIndex = 0; childIndex < mSequence.size(); childIndex++)
235 {
236 nodeQueue->push(mSequence[childIndex]);
237 }
238}
239
240bool TIntermSelection::replaceChildNode(
241 TIntermNode *original, TIntermNode *replacement)
242{
243 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
244 REPLACE_IF_IS(mTrueBlock, TIntermNode, original, replacement);
245 REPLACE_IF_IS(mFalseBlock, TIntermNode, original, replacement);
246 return false;
247}
248
249void TIntermSelection::enqueueChildren(std::queue<TIntermNode *> *nodeQueue) const
250{
251 if (mCondition)
252 {
253 nodeQueue->push(mCondition);
254 }
255 if (mTrueBlock)
256 {
257 nodeQueue->push(mTrueBlock);
258 }
259 if (mFalseBlock)
260 {
261 nodeQueue->push(mFalseBlock);
262 }
263}
264
265//
266// Say whether or not an operation node changes the value of a variable.
267//
268bool TIntermOperator::isAssignment() const
269{
270 switch (mOp)
271 {
272 case EOpPostIncrement:
273 case EOpPostDecrement:
274 case EOpPreIncrement:
275 case EOpPreDecrement:
276 case EOpAssign:
277 case EOpAddAssign:
278 case EOpSubAssign:
279 case EOpMulAssign:
280 case EOpVectorTimesMatrixAssign:
281 case EOpVectorTimesScalarAssign:
282 case EOpMatrixTimesScalarAssign:
283 case EOpMatrixTimesMatrixAssign:
284 case EOpDivAssign:
285 return true;
286 default:
287 return false;
288 }
289}
290
291//
292// returns true if the operator is for one of the constructors
293//
294bool TIntermOperator::isConstructor() const
295{
296 switch (mOp)
297 {
298 case EOpConstructVec2:
299 case EOpConstructVec3:
300 case EOpConstructVec4:
301 case EOpConstructMat2:
302 case EOpConstructMat3:
303 case EOpConstructMat4:
304 case EOpConstructFloat:
305 case EOpConstructIVec2:
306 case EOpConstructIVec3:
307 case EOpConstructIVec4:
308 case EOpConstructInt:
309 case EOpConstructUVec2:
310 case EOpConstructUVec3:
311 case EOpConstructUVec4:
312 case EOpConstructUInt:
313 case EOpConstructBVec2:
314 case EOpConstructBVec3:
315 case EOpConstructBVec4:
316 case EOpConstructBool:
317 case EOpConstructStruct:
318 return true;
319 default:
320 return false;
321 }
322}
323
324//
325// Make sure the type of a unary operator is appropriate for its
326// combination of operation and operand type.
327//
328// Returns false in nothing makes sense.
329//
330bool TIntermUnary::promote(TInfoSink &)
331{
332 switch (mOp)
333 {
334 case EOpLogicalNot:
335 if (mOperand->getBasicType() != EbtBool)
336 return false;
337 break;
338 case EOpNegative:
Zhenyao Mode1e00e2014-10-09 16:55:32 -0700339 case EOpPositive:
Jamie Madillb1a85f42014-08-19 15:23:24 -0400340 case EOpPostIncrement:
341 case EOpPostDecrement:
342 case EOpPreIncrement:
343 case EOpPreDecrement:
344 if (mOperand->getBasicType() == EbtBool)
345 return false;
346 break;
347
348 // operators for built-ins are already type checked against their prototype
349 case EOpAny:
350 case EOpAll:
351 case EOpVectorLogicalNot:
352 return true;
353
354 default:
355 if (mOperand->getBasicType() != EbtFloat)
356 return false;
357 }
358
359 setType(mOperand->getType());
360 mType.setQualifier(EvqTemporary);
361
362 return true;
363}
364
365//
366// Establishes the type of the resultant operation, as well as
367// makes the operator the correct one for the operands.
368//
369// Returns false if operator can't work on operands.
370//
371bool TIntermBinary::promote(TInfoSink &infoSink)
372{
373 // This function only handles scalars, vectors, and matrices.
374 if (mLeft->isArray() || mRight->isArray())
375 {
376 infoSink.info.message(EPrefixInternalError, getLine(),
377 "Invalid operation for arrays");
378 return false;
379 }
380
381 // GLSL ES 2.0 does not support implicit type casting.
382 // So the basic type should always match.
383 if (mLeft->getBasicType() != mRight->getBasicType())
384 {
385 return false;
386 }
387
388 //
389 // Base assumption: just make the type the same as the left
390 // operand. Then only deviations from this need be coded.
391 //
392 setType(mLeft->getType());
393
394 // The result gets promoted to the highest precision.
395 TPrecision higherPrecision = GetHigherPrecision(
396 mLeft->getPrecision(), mRight->getPrecision());
397 getTypePointer()->setPrecision(higherPrecision);
398
399 // Binary operations results in temporary variables unless both
400 // operands are const.
401 if (mLeft->getQualifier() != EvqConst || mRight->getQualifier() != EvqConst)
402 {
403 getTypePointer()->setQualifier(EvqTemporary);
404 }
405
406 const int nominalSize =
407 std::max(mLeft->getNominalSize(), mRight->getNominalSize());
408
409 //
410 // All scalars or structs. Code after this test assumes this case is removed!
411 //
412 if (nominalSize == 1)
413 {
414 switch (mOp)
415 {
416 //
417 // Promote to conditional
418 //
419 case EOpEqual:
420 case EOpNotEqual:
421 case EOpLessThan:
422 case EOpGreaterThan:
423 case EOpLessThanEqual:
424 case EOpGreaterThanEqual:
425 setType(TType(EbtBool, EbpUndefined));
426 break;
427
428 //
429 // And and Or operate on conditionals
430 //
431 case EOpLogicalAnd:
432 case EOpLogicalOr:
433 // Both operands must be of type bool.
434 if (mLeft->getBasicType() != EbtBool || mRight->getBasicType() != EbtBool)
435 {
436 return false;
437 }
438 setType(TType(EbtBool, EbpUndefined));
439 break;
440
441 default:
442 break;
443 }
444 return true;
445 }
446
447 // If we reach here, at least one of the operands is vector or matrix.
448 // The other operand could be a scalar, vector, or matrix.
449 // Can these two operands be combined?
450 //
451 TBasicType basicType = mLeft->getBasicType();
452 switch (mOp)
453 {
454 case EOpMul:
455 if (!mLeft->isMatrix() && mRight->isMatrix())
456 {
457 if (mLeft->isVector())
458 {
459 mOp = EOpVectorTimesMatrix;
460 setType(TType(basicType, higherPrecision, EvqTemporary,
461 mRight->getCols(), 1));
462 }
463 else
464 {
465 mOp = EOpMatrixTimesScalar;
466 setType(TType(basicType, higherPrecision, EvqTemporary,
467 mRight->getCols(), mRight->getRows()));
468 }
469 }
470 else if (mLeft->isMatrix() && !mRight->isMatrix())
471 {
472 if (mRight->isVector())
473 {
474 mOp = EOpMatrixTimesVector;
475 setType(TType(basicType, higherPrecision, EvqTemporary,
476 mLeft->getRows(), 1));
477 }
478 else
479 {
480 mOp = EOpMatrixTimesScalar;
481 }
482 }
483 else if (mLeft->isMatrix() && mRight->isMatrix())
484 {
485 mOp = EOpMatrixTimesMatrix;
486 setType(TType(basicType, higherPrecision, EvqTemporary,
487 mRight->getCols(), mLeft->getRows()));
488 }
489 else if (!mLeft->isMatrix() && !mRight->isMatrix())
490 {
491 if (mLeft->isVector() && mRight->isVector())
492 {
493 // leave as component product
494 }
495 else if (mLeft->isVector() || mRight->isVector())
496 {
497 mOp = EOpVectorTimesScalar;
498 setType(TType(basicType, higherPrecision, EvqTemporary,
499 nominalSize, 1));
500 }
501 }
502 else
503 {
504 infoSink.info.message(EPrefixInternalError, getLine(),
505 "Missing elses");
506 return false;
507 }
508
509 if (!ValidateMultiplication(mOp, mLeft->getType(), mRight->getType()))
510 {
511 return false;
512 }
513 break;
514
515 case EOpMulAssign:
516 if (!mLeft->isMatrix() && mRight->isMatrix())
517 {
518 if (mLeft->isVector())
519 {
520 mOp = EOpVectorTimesMatrixAssign;
521 }
522 else
523 {
524 return false;
525 }
526 }
527 else if (mLeft->isMatrix() && !mRight->isMatrix())
528 {
529 if (mRight->isVector())
530 {
531 return false;
532 }
533 else
534 {
535 mOp = EOpMatrixTimesScalarAssign;
536 }
537 }
538 else if (mLeft->isMatrix() && mRight->isMatrix())
539 {
540 mOp = EOpMatrixTimesMatrixAssign;
541 setType(TType(basicType, higherPrecision, EvqTemporary,
542 mRight->getCols(), mLeft->getRows()));
543 }
544 else if (!mLeft->isMatrix() && !mRight->isMatrix())
545 {
546 if (mLeft->isVector() && mRight->isVector())
547 {
548 // leave as component product
549 }
550 else if (mLeft->isVector() || mRight->isVector())
551 {
552 if (!mLeft->isVector())
553 return false;
554 mOp = EOpVectorTimesScalarAssign;
555 setType(TType(basicType, higherPrecision, EvqTemporary,
556 mLeft->getNominalSize(), 1));
557 }
558 }
559 else
560 {
561 infoSink.info.message(EPrefixInternalError, getLine(),
562 "Missing elses");
563 return false;
564 }
565
566 if (!ValidateMultiplication(mOp, mLeft->getType(), mRight->getType()))
567 {
568 return false;
569 }
570 break;
571
572 case EOpAssign:
573 case EOpInitialize:
574 case EOpAdd:
575 case EOpSub:
576 case EOpDiv:
577 case EOpAddAssign:
578 case EOpSubAssign:
579 case EOpDivAssign:
580 if ((mLeft->isMatrix() && mRight->isVector()) ||
581 (mLeft->isVector() && mRight->isMatrix()))
582 {
583 return false;
584 }
585
586 // Are the sizes compatible?
587 if (mLeft->getNominalSize() != mRight->getNominalSize() ||
588 mLeft->getSecondarySize() != mRight->getSecondarySize())
589 {
590 // If the nominal size of operands do not match:
591 // One of them must be scalar.
592 if (!mLeft->isScalar() && !mRight->isScalar())
593 return false;
594
595 // Operator cannot be of type pure assignment.
596 if (mOp == EOpAssign || mOp == EOpInitialize)
597 return false;
598 }
599
600 {
601 const int secondarySize = std::max(
602 mLeft->getSecondarySize(), mRight->getSecondarySize());
603 setType(TType(basicType, higherPrecision, EvqTemporary,
604 nominalSize, secondarySize));
605 }
606 break;
607
608 case EOpEqual:
609 case EOpNotEqual:
610 case EOpLessThan:
611 case EOpGreaterThan:
612 case EOpLessThanEqual:
613 case EOpGreaterThanEqual:
614 if ((mLeft->getNominalSize() != mRight->getNominalSize()) ||
615 (mLeft->getSecondarySize() != mRight->getSecondarySize()))
616 {
617 return false;
618 }
619 setType(TType(EbtBool, EbpUndefined));
620 break;
621
622 default:
623 return false;
624 }
625 return true;
626}
627
628//
629// The fold functions see if an operation on a constant can be done in place,
630// without generating run-time code.
631//
632// Returns the node to keep using, which may or may not be the node passed in.
633//
634TIntermTyped *TIntermConstantUnion::fold(
635 TOperator op, TIntermTyped *constantNode, TInfoSink &infoSink)
636{
637 ConstantUnion *unionArray = getUnionArrayPointer();
638
639 if (!unionArray)
640 return NULL;
641
642 size_t objectSize = getType().getObjectSize();
643
644 if (constantNode)
645 {
646 // binary operations
647 TIntermConstantUnion *node = constantNode->getAsConstantUnion();
648 ConstantUnion *rightUnionArray = node->getUnionArrayPointer();
649 TType returnType = getType();
650
651 if (!rightUnionArray)
652 return NULL;
653
654 // for a case like float f = 1.2 + vec4(2,3,4,5);
655 if (constantNode->getType().getObjectSize() == 1 && objectSize > 1)
656 {
657 rightUnionArray = new ConstantUnion[objectSize];
658 for (size_t i = 0; i < objectSize; ++i)
659 {
660 rightUnionArray[i] = *node->getUnionArrayPointer();
661 }
662 returnType = getType();
663 }
664 else if (constantNode->getType().getObjectSize() > 1 && objectSize == 1)
665 {
666 // for a case like float f = vec4(2,3,4,5) + 1.2;
667 unionArray = new ConstantUnion[constantNode->getType().getObjectSize()];
668 for (size_t i = 0; i < constantNode->getType().getObjectSize(); ++i)
669 {
670 unionArray[i] = *getUnionArrayPointer();
671 }
672 returnType = node->getType();
673 objectSize = constantNode->getType().getObjectSize();
674 }
675
676 ConstantUnion *tempConstArray = NULL;
677 TIntermConstantUnion *tempNode;
678
679 bool boolNodeFlag = false;
680 switch(op)
681 {
682 case EOpAdd:
683 tempConstArray = new ConstantUnion[objectSize];
684 for (size_t i = 0; i < objectSize; i++)
685 tempConstArray[i] = unionArray[i] + rightUnionArray[i];
686 break;
687 case EOpSub:
688 tempConstArray = new ConstantUnion[objectSize];
689 for (size_t i = 0; i < objectSize; i++)
690 tempConstArray[i] = unionArray[i] - rightUnionArray[i];
691 break;
692
693 case EOpMul:
694 case EOpVectorTimesScalar:
695 case EOpMatrixTimesScalar:
696 tempConstArray = new ConstantUnion[objectSize];
697 for (size_t i = 0; i < objectSize; i++)
698 tempConstArray[i] = unionArray[i] * rightUnionArray[i];
699 break;
700
701 case EOpMatrixTimesMatrix:
702 {
703 if (getType().getBasicType() != EbtFloat ||
704 node->getBasicType() != EbtFloat)
705 {
706 infoSink.info.message(
707 EPrefixInternalError, getLine(),
708 "Constant Folding cannot be done for matrix multiply");
709 return NULL;
710 }
711
712 const int leftCols = getCols();
713 const int leftRows = getRows();
714 const int rightCols = constantNode->getType().getCols();
715 const int rightRows = constantNode->getType().getRows();
716 const int resultCols = rightCols;
717 const int resultRows = leftRows;
718
719 tempConstArray = new ConstantUnion[resultCols*resultRows];
720 for (int row = 0; row < resultRows; row++)
721 {
722 for (int column = 0; column < resultCols; column++)
723 {
724 tempConstArray[resultRows * column + row].setFConst(0.0f);
725 for (int i = 0; i < leftCols; i++)
726 {
727 tempConstArray[resultRows * column + row].setFConst(
728 tempConstArray[resultRows * column + row].getFConst() +
729 unionArray[i * leftRows + row].getFConst() *
730 rightUnionArray[column * rightRows + i].getFConst());
731 }
732 }
733 }
734
735 // update return type for matrix product
736 returnType.setPrimarySize(resultCols);
737 returnType.setSecondarySize(resultRows);
738 }
739 break;
740
741 case EOpDiv:
742 {
743 tempConstArray = new ConstantUnion[objectSize];
744 for (size_t i = 0; i < objectSize; i++)
745 {
746 switch (getType().getBasicType())
747 {
748 case EbtFloat:
749 if (rightUnionArray[i] == 0.0f)
750 {
751 infoSink.info.message(
752 EPrefixWarning, getLine(),
753 "Divide by zero error during constant folding");
754 tempConstArray[i].setFConst(
755 unionArray[i].getFConst() < 0 ? -FLT_MAX : FLT_MAX);
756 }
757 else
758 {
759 tempConstArray[i].setFConst(
760 unionArray[i].getFConst() /
761 rightUnionArray[i].getFConst());
762 }
763 break;
764
765 case EbtInt:
766 if (rightUnionArray[i] == 0)
767 {
768 infoSink.info.message(
769 EPrefixWarning, getLine(),
770 "Divide by zero error during constant folding");
771 tempConstArray[i].setIConst(INT_MAX);
772 }
773 else
774 {
775 tempConstArray[i].setIConst(
776 unionArray[i].getIConst() /
777 rightUnionArray[i].getIConst());
778 }
779 break;
780
781 case EbtUInt:
782 if (rightUnionArray[i] == 0)
783 {
784 infoSink.info.message(
785 EPrefixWarning, getLine(),
786 "Divide by zero error during constant folding");
787 tempConstArray[i].setUConst(UINT_MAX);
788 }
789 else
790 {
791 tempConstArray[i].setUConst(
792 unionArray[i].getUConst() /
793 rightUnionArray[i].getUConst());
794 }
795 break;
796
797 default:
798 infoSink.info.message(
799 EPrefixInternalError, getLine(),
800 "Constant folding cannot be done for \"/\"");
801 return NULL;
802 }
803 }
804 }
805 break;
806
807 case EOpMatrixTimesVector:
808 {
809 if (node->getBasicType() != EbtFloat)
810 {
811 infoSink.info.message(
812 EPrefixInternalError, getLine(),
813 "Constant Folding cannot be done for matrix times vector");
814 return NULL;
815 }
816
817 const int matrixCols = getCols();
818 const int matrixRows = getRows();
819
820 tempConstArray = new ConstantUnion[matrixRows];
821
822 for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++)
823 {
824 tempConstArray[matrixRow].setFConst(0.0f);
825 for (int col = 0; col < matrixCols; col++)
826 {
827 tempConstArray[matrixRow].setFConst(
828 tempConstArray[matrixRow].getFConst() +
829 unionArray[col * matrixRows + matrixRow].getFConst() *
830 rightUnionArray[col].getFConst());
831 }
832 }
833
834 returnType = node->getType();
835 returnType.setPrimarySize(matrixRows);
836
837 tempNode = new TIntermConstantUnion(tempConstArray, returnType);
838 tempNode->setLine(getLine());
839
840 return tempNode;
841 }
842
843 case EOpVectorTimesMatrix:
844 {
845 if (getType().getBasicType() != EbtFloat)
846 {
847 infoSink.info.message(
848 EPrefixInternalError, getLine(),
849 "Constant Folding cannot be done for vector times matrix");
850 return NULL;
851 }
852
853 const int matrixCols = constantNode->getType().getCols();
854 const int matrixRows = constantNode->getType().getRows();
855
856 tempConstArray = new ConstantUnion[matrixCols];
857
858 for (int matrixCol = 0; matrixCol < matrixCols; matrixCol++)
859 {
860 tempConstArray[matrixCol].setFConst(0.0f);
861 for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++)
862 {
863 tempConstArray[matrixCol].setFConst(
864 tempConstArray[matrixCol].getFConst() +
865 unionArray[matrixRow].getFConst() *
866 rightUnionArray[matrixCol * matrixRows + matrixRow].getFConst());
867 }
868 }
869
870 returnType.setPrimarySize(matrixCols);
871 }
872 break;
873
874 case EOpLogicalAnd:
875 // this code is written for possible future use,
876 // will not get executed currently
877 {
878 tempConstArray = new ConstantUnion[objectSize];
879 for (size_t i = 0; i < objectSize; i++)
880 {
881 tempConstArray[i] = unionArray[i] && rightUnionArray[i];
882 }
883 }
884 break;
885
886 case EOpLogicalOr:
887 // this code is written for possible future use,
888 // will not get executed currently
889 {
890 tempConstArray = new ConstantUnion[objectSize];
891 for (size_t i = 0; i < objectSize; i++)
892 {
893 tempConstArray[i] = unionArray[i] || rightUnionArray[i];
894 }
895 }
896 break;
897
898 case EOpLogicalXor:
899 {
900 tempConstArray = new ConstantUnion[objectSize];
901 for (size_t i = 0; i < objectSize; i++)
902 {
903 switch (getType().getBasicType())
904 {
905 case EbtBool:
906 tempConstArray[i].setBConst(
907 unionArray[i] == rightUnionArray[i] ? false : true);
908 break;
909 default:
910 UNREACHABLE();
911 break;
912 }
913 }
914 }
915 break;
916
917 case EOpLessThan:
918 ASSERT(objectSize == 1);
919 tempConstArray = new ConstantUnion[1];
920 tempConstArray->setBConst(*unionArray < *rightUnionArray);
921 returnType = TType(EbtBool, EbpUndefined, EvqConst);
922 break;
923
924 case EOpGreaterThan:
925 ASSERT(objectSize == 1);
926 tempConstArray = new ConstantUnion[1];
927 tempConstArray->setBConst(*unionArray > *rightUnionArray);
928 returnType = TType(EbtBool, EbpUndefined, EvqConst);
929 break;
930
931 case EOpLessThanEqual:
932 {
933 ASSERT(objectSize == 1);
934 ConstantUnion constant;
935 constant.setBConst(*unionArray > *rightUnionArray);
936 tempConstArray = new ConstantUnion[1];
937 tempConstArray->setBConst(!constant.getBConst());
938 returnType = TType(EbtBool, EbpUndefined, EvqConst);
939 break;
940 }
941
942 case EOpGreaterThanEqual:
943 {
944 ASSERT(objectSize == 1);
945 ConstantUnion constant;
946 constant.setBConst(*unionArray < *rightUnionArray);
947 tempConstArray = new ConstantUnion[1];
948 tempConstArray->setBConst(!constant.getBConst());
949 returnType = TType(EbtBool, EbpUndefined, EvqConst);
950 break;
951 }
952
953 case EOpEqual:
954 if (getType().getBasicType() == EbtStruct)
955 {
956 if (!CompareStructure(node->getType(),
957 node->getUnionArrayPointer(),
958 unionArray))
959 {
960 boolNodeFlag = true;
961 }
962 }
963 else
964 {
965 for (size_t i = 0; i < objectSize; i++)
966 {
967 if (unionArray[i] != rightUnionArray[i])
968 {
969 boolNodeFlag = true;
970 break; // break out of for loop
971 }
972 }
973 }
974
975 tempConstArray = new ConstantUnion[1];
976 if (!boolNodeFlag)
977 {
978 tempConstArray->setBConst(true);
979 }
980 else
981 {
982 tempConstArray->setBConst(false);
983 }
984
985 tempNode = new TIntermConstantUnion(
986 tempConstArray, TType(EbtBool, EbpUndefined, EvqConst));
987 tempNode->setLine(getLine());
988
989 return tempNode;
990
991 case EOpNotEqual:
992 if (getType().getBasicType() == EbtStruct)
993 {
994 if (CompareStructure(node->getType(),
995 node->getUnionArrayPointer(),
996 unionArray))
997 {
998 boolNodeFlag = true;
999 }
1000 }
1001 else
1002 {
1003 for (size_t i = 0; i < objectSize; i++)
1004 {
1005 if (unionArray[i] == rightUnionArray[i])
1006 {
1007 boolNodeFlag = true;
1008 break; // break out of for loop
1009 }
1010 }
1011 }
1012
1013 tempConstArray = new ConstantUnion[1];
1014 if (!boolNodeFlag)
1015 {
1016 tempConstArray->setBConst(true);
1017 }
1018 else
1019 {
1020 tempConstArray->setBConst(false);
1021 }
1022
1023 tempNode = new TIntermConstantUnion(
1024 tempConstArray, TType(EbtBool, EbpUndefined, EvqConst));
1025 tempNode->setLine(getLine());
1026
1027 return tempNode;
1028
1029 default:
1030 infoSink.info.message(
1031 EPrefixInternalError, getLine(),
1032 "Invalid operator for constant folding");
1033 return NULL;
1034 }
1035 tempNode = new TIntermConstantUnion(tempConstArray, returnType);
1036 tempNode->setLine(getLine());
1037
1038 return tempNode;
1039 }
1040 else
1041 {
1042 //
1043 // Do unary operations
1044 //
1045 TIntermConstantUnion *newNode = 0;
1046 ConstantUnion* tempConstArray = new ConstantUnion[objectSize];
1047 for (size_t i = 0; i < objectSize; i++)
1048 {
1049 switch(op)
1050 {
1051 case EOpNegative:
1052 switch (getType().getBasicType())
1053 {
1054 case EbtFloat:
1055 tempConstArray[i].setFConst(-unionArray[i].getFConst());
1056 break;
1057 case EbtInt:
1058 tempConstArray[i].setIConst(-unionArray[i].getIConst());
1059 break;
1060 case EbtUInt:
1061 tempConstArray[i].setUConst(static_cast<unsigned int>(
1062 -static_cast<int>(unionArray[i].getUConst())));
1063 break;
1064 default:
1065 infoSink.info.message(
1066 EPrefixInternalError, getLine(),
1067 "Unary operation not folded into constant");
1068 return NULL;
1069 }
1070 break;
1071
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001072 case EOpPositive:
1073 switch (getType().getBasicType())
1074 {
1075 case EbtFloat:
1076 tempConstArray[i].setFConst(unionArray[i].getFConst());
1077 break;
1078 case EbtInt:
1079 tempConstArray[i].setIConst(unionArray[i].getIConst());
1080 break;
1081 case EbtUInt:
1082 tempConstArray[i].setUConst(static_cast<unsigned int>(
1083 static_cast<int>(unionArray[i].getUConst())));
1084 break;
1085 default:
1086 infoSink.info.message(
1087 EPrefixInternalError, getLine(),
1088 "Unary operation not folded into constant");
1089 return NULL;
1090 }
1091 break;
1092
Jamie Madillb1a85f42014-08-19 15:23:24 -04001093 case EOpLogicalNot:
1094 // this code is written for possible future use,
1095 // will not get executed currently
1096 switch (getType().getBasicType())
1097 {
1098 case EbtBool:
1099 tempConstArray[i].setBConst(!unionArray[i].getBConst());
1100 break;
1101 default:
1102 infoSink.info.message(
1103 EPrefixInternalError, getLine(),
1104 "Unary operation not folded into constant");
1105 return NULL;
1106 }
1107 break;
1108
1109 default:
1110 return NULL;
1111 }
1112 }
1113 newNode = new TIntermConstantUnion(tempConstArray, getType());
1114 newNode->setLine(getLine());
1115 return newNode;
1116 }
1117}
1118
1119// static
1120TString TIntermTraverser::hash(const TString &name, ShHashFunction64 hashFunction)
1121{
1122 if (hashFunction == NULL || name.empty())
1123 return name;
1124 khronos_uint64_t number = (*hashFunction)(name.c_str(), name.length());
1125 TStringStream stream;
1126 stream << HASHED_NAME_PREFIX << std::hex << number;
1127 TString hashedName = stream.str();
1128 return hashedName;
1129}