2 //Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
5 //Redistribution and use in source and binary forms, with or without
6 //modification, are permitted provided that the following conditions
9 // Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
12 // Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following
14 // disclaimer in the documentation and/or other materials provided
15 // with the distribution.
17 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
18 // contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
21 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 //FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 //COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 //INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 //CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 //ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 //POSSIBILITY OF SUCH DAMAGE.
36 // Travarse a tree of constants to create a single folded constant.
37 // It should only be used when the whole tree is known to be constant.
40 #include "ParseHelper.h"
44 class TConstTraverser : public TIntermTraverser {
46 TConstTraverser(const TConstUnionArray& cUnion, bool singleConstParam, TOperator constructType, const TType& t)
47 : unionArray(cUnion), type(t),
48 constructorType(constructType), singleConstantParam(singleConstParam), error(false), isMatrix(false),
49 matrixCols(0), matrixRows(0) { index = 0; tOp = EOpNull; }
51 virtual void visitConstantUnion(TIntermConstantUnion* node);
52 virtual bool visitAggregate(TVisit, TIntermAggregate* node);
55 TConstUnionArray unionArray;
58 TOperator constructorType;
59 bool singleConstantParam;
61 int size; // size of the constructor ( 4 for vec4)
67 TConstTraverser(TConstTraverser&);
68 TConstTraverser& operator=(TConstTraverser&);
71 bool TConstTraverser::visitAggregate(TVisit /* visit */, TIntermAggregate* node)
73 if (! node->isConstructor() && node->getOp() != EOpComma) {
79 if (node->getSequence().size() == 0) {
85 bool flag = node->getSequence().size() == 1 && node->getSequence()[0]->getAsTyped()->getAsConstantUnion();
87 singleConstantParam = true;
88 constructorType = node->getOp();
89 size = node->getType().computeNumComponents();
91 if (node->getType().isMatrix()) {
93 matrixCols = node->getType().getMatrixCols();
94 matrixRows = node->getType().getMatrixRows();
98 for (TIntermSequence::iterator p = node->getSequence().begin();
99 p != node->getSequence().end(); p++) {
101 if (node->getOp() == EOpComma)
104 (*p)->traverse(this);
108 singleConstantParam = false;
109 constructorType = EOpNull;
119 void TConstTraverser::visitConstantUnion(TIntermConstantUnion* node)
121 TConstUnionArray leftUnionArray(unionArray);
122 int instanceSize = type.computeNumComponents();
124 if (index >= instanceSize)
127 if (! singleConstantParam) {
128 int rightUnionSize = node->getType().computeNumComponents();
130 const TConstUnionArray& rightUnionArray = node->getConstArray();
131 for (int i = 0; i < rightUnionSize; i++) {
132 if (index >= instanceSize)
134 leftUnionArray[index] = rightUnionArray[i];
139 int endIndex = index + size;
140 const TConstUnionArray& rightUnionArray = node->getConstArray();
143 int nodeComps = node->getType().computeNumComponents();
144 for (int i = index; i < endIndex; i++) {
145 if (i >= instanceSize)
148 leftUnionArray[i] = rightUnionArray[count];
156 // constructing a matrix, but from what?
157 if (node->isMatrix()) {
158 // Matrix from a matrix; this has the outer matrix, node is the argument matrix.
159 // Traverse the outer, potentially bigger matrix, fill in missing pieces with the
161 for (int c = 0; c < matrixCols; ++c) {
162 for (int r = 0; r < matrixRows; ++r) {
163 int targetOffset = index + c * matrixRows + r;
164 if (r < node->getType().getMatrixRows() && c < node->getType().getMatrixCols()) {
165 int srcOffset = c * node->getType().getMatrixRows() + r;
166 leftUnionArray[targetOffset] = rightUnionArray[srcOffset];
168 leftUnionArray[targetOffset].setDConst(1.0);
170 leftUnionArray[targetOffset].setDConst(0.0);
174 // matrix from vector
176 const int startIndex = index;
177 int nodeComps = node->getType().computeNumComponents();
178 for (int i = startIndex; i < endIndex; i++) {
179 if (i >= instanceSize)
181 if (i == startIndex || (i - startIndex) % (matrixRows + 1) == 0 )
182 leftUnionArray[i] = rightUnionArray[count];
184 leftUnionArray[i].setDConst(0.0);
196 bool TIntermediate::parseConstTree(TIntermNode* root, TConstUnionArray unionArray, TOperator constructorType, const TType& t, bool singleConstantParam)
201 TConstTraverser it(unionArray, singleConstantParam, constructorType, t);
210 } // end namespace glslang