2 // Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3 // All rights reserved.
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 // Traverse 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 bool flag = node->getSequence().size() == 1 && node->getSequence()[0]->getAsTyped()->getAsConstantUnion();
81 singleConstantParam = true;
82 constructorType = node->getOp();
83 size = node->getType().computeNumComponents();
85 if (node->getType().isMatrix()) {
87 matrixCols = node->getType().getMatrixCols();
88 matrixRows = node->getType().getMatrixRows();
92 for (TIntermSequence::iterator p = node->getSequence().begin();
93 p != node->getSequence().end(); p++) {
95 if (node->getOp() == EOpComma)
102 singleConstantParam = false;
103 constructorType = EOpNull;
113 void TConstTraverser::visitConstantUnion(TIntermConstantUnion* node)
115 TConstUnionArray leftUnionArray(unionArray);
116 int instanceSize = type.computeNumComponents();
118 if (index >= instanceSize)
121 if (! singleConstantParam) {
122 int rightUnionSize = node->getType().computeNumComponents();
124 const TConstUnionArray& rightUnionArray = node->getConstArray();
125 for (int i = 0; i < rightUnionSize; i++) {
126 if (index >= instanceSize)
128 leftUnionArray[index] = rightUnionArray[i];
133 int endIndex = index + size;
134 const TConstUnionArray& rightUnionArray = node->getConstArray();
137 int nodeComps = node->getType().computeNumComponents();
138 for (int i = index; i < endIndex; i++) {
139 if (i >= instanceSize)
142 leftUnionArray[i] = rightUnionArray[count];
150 // constructing a matrix, but from what?
151 if (node->isMatrix()) {
152 // Matrix from a matrix; this has the outer matrix, node is the argument matrix.
153 // Traverse the outer, potentially bigger matrix, fill in missing pieces with the
155 for (int c = 0; c < matrixCols; ++c) {
156 for (int r = 0; r < matrixRows; ++r) {
157 int targetOffset = index + c * matrixRows + r;
158 if (r < node->getType().getMatrixRows() && c < node->getType().getMatrixCols()) {
159 int srcOffset = c * node->getType().getMatrixRows() + r;
160 leftUnionArray[targetOffset] = rightUnionArray[srcOffset];
162 leftUnionArray[targetOffset].setDConst(1.0);
164 leftUnionArray[targetOffset].setDConst(0.0);
168 // matrix from vector or scalar
169 int nodeComps = node->getType().computeNumComponents();
170 if (nodeComps == 1) {
171 for (int c = 0; c < matrixCols; ++c) {
172 for (int r = 0; r < matrixRows; ++r) {
174 leftUnionArray[index] = rightUnionArray[0];
176 leftUnionArray[index].setDConst(0.0);
182 for (int i = index; i < endIndex; i++) {
183 if (i >= instanceSize)
186 // construct the matrix in column-major order, from
187 // the components provided, in order
188 leftUnionArray[i] = rightUnionArray[count];
199 bool TIntermediate::parseConstTree(TIntermNode* root, TConstUnionArray unionArray, TOperator constructorType, const TType& t, bool singleConstantParam)
204 TConstTraverser it(unionArray, singleConstantParam, constructorType, t);
213 } // end namespace glslang