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) : unionArray(cUnion), type(t),
47 constructorType(constructType), singleConstantParam(singleConstParam), error(false), isMatrix(false),
48 matrixCols(0), matrixRows(0) { index = 0; tOp = EOpNull; }
50 TConstUnionArray unionArray;
53 TOperator constructorType;
54 bool singleConstantParam;
56 int size; // size of the constructor ( 4 for vec4)
62 bool ParseAggregate(bool /* preVisit */, TIntermAggregate* node, TIntermTraverser* it)
64 TConstTraverser* oit = static_cast<TConstTraverser*>(it);
66 if (! node->isConstructor() && node->getOp() != EOpComma) {
72 if (node->getSequence().size() == 0) {
78 bool flag = node->getSequence().size() == 1 && node->getSequence()[0]->getAsTyped()->getAsConstantUnion();
80 oit->singleConstantParam = true;
81 oit->constructorType = node->getOp();
82 oit->size = node->getType().getObjectSize();
84 if (node->getType().isMatrix()) {
86 oit->matrixCols = node->getType().getMatrixCols();
87 oit->matrixRows = node->getType().getMatrixRows();
91 for (TIntermSequence::iterator p = node->getSequence().begin();
92 p != node->getSequence().end(); p++) {
94 if (node->getOp() == EOpComma)
101 oit->singleConstantParam = false;
102 oit->constructorType = EOpNull;
104 oit->isMatrix = false;
112 void ParseConstantUnion(TIntermConstantUnion* node, TIntermTraverser* it)
114 TConstTraverser* oit = static_cast<TConstTraverser*>(it);
115 TConstUnionArray leftUnionArray(oit->unionArray);
116 int instanceSize = oit->type.getObjectSize();
118 if (oit->index >= instanceSize)
121 if (! oit->singleConstantParam) {
122 int size = node->getType().getObjectSize();
124 const TConstUnionArray& rightUnionArray = node->getConstArray();
125 for (int i = 0; i < size; i++) {
126 if (oit->index >= instanceSize)
128 leftUnionArray[oit->index] = rightUnionArray[i];
133 int endIndex = oit->index + oit->size;
134 const TConstUnionArray& rightUnionArray = node->getConstArray();
135 if (! oit->isMatrix) {
137 for (int i = oit->index; i < endIndex; i++) {
138 if (i >= instanceSize)
141 leftUnionArray[i] = rightUnionArray[count];
145 if (node->getType().getObjectSize() > 1)
149 // constructing a matrix, but from what?
150 if (node->isMatrix()) {
151 // Matrix from a matrix; oit has the outer matrix, node is the argument matrix.
152 // Traverse the outer, potentially bigger matrix, fill in missing pieces with the
154 for (int c = 0; c < oit->matrixCols; ++c) {
155 for (int r = 0; r < oit->matrixRows; ++r) {
156 int targetOffset = oit->index + c * oit->matrixRows + r;
157 if (r < node->getType().getMatrixRows() && c < node->getType().getMatrixCols()) {
158 int srcOffset = c * node->getType().getMatrixRows() + r;
159 leftUnionArray[targetOffset] = rightUnionArray[srcOffset];
161 leftUnionArray[targetOffset].setDConst(1.0);
163 leftUnionArray[targetOffset].setDConst(0.0);
167 // matrix from vector
169 int index = oit->index;
170 for (int i = index; i < endIndex; i++) {
171 if (i >= instanceSize)
173 if (i == index || (i - index) % (oit->matrixRows + 1) == 0 )
174 leftUnionArray[i] = rightUnionArray[count];
176 leftUnionArray[i].setDConst(0.0);
180 if (node->getType().getObjectSize() > 1)
188 bool TIntermediate::parseConstTree(TIntermNode* root, TConstUnionArray unionArray, TOperator constructorType, const TType& t, bool singleConstantParam)
193 TConstTraverser it(unionArray, singleConstantParam, constructorType, t);
195 it.visitAggregate = ParseAggregate;
196 it.visitConstantUnion = ParseConstantUnion;
205 } // end namespace glslang