// // Do constant folding for an aggregate node that has all its children // as constants and an operator that requires constant folding. // TIntermTyped* TIntermediate::fold(TIntermAggregate* aggrNode) { if (! areAllChildConst(aggrNode)) return aggrNode; if (aggrNode->isConstructor()) return foldConstructor(aggrNode); TIntermSequence& children = aggrNode->getSequence(); // First, see if this is an operation to constant fold, kick out if not, // see what size the result is if so. bool componentwise = false; // will also say componentwise if a scalar argument gets repeated to make per-component results int objectSize; switch (aggrNode->getOp()) { case EOpAtan: case EOpPow: case EOpMin: case EOpMax: case EOpMix: case EOpClamp: componentwise = true; objectSize = children[0]->getAsConstantUnion()->getType().getObjectSize(); break; case EOpCross: case EOpReflect: case EOpRefract: case EOpFaceForward: objectSize = children[0]->getAsConstantUnion()->getType().getObjectSize(); break; case EOpDistance: case EOpDot: objectSize = 1; break; case EOpOuterProduct: objectSize = children[0]->getAsTyped()->getType().getVectorSize() * children[1]->getAsTyped()->getType().getVectorSize(); break; case EOpStep: componentwise = true; objectSize = std::max(children[0]->getAsTyped()->getType().getVectorSize(), children[1]->getAsTyped()->getType().getVectorSize()); break; case EOpSmoothStep: componentwise = true; objectSize = std::max(children[0]->getAsTyped()->getType().getVectorSize(), children[2]->getAsTyped()->getType().getVectorSize()); break; default: return aggrNode; } TConstUnion* newConstArray = new TConstUnion[objectSize]; TVector<TConstUnion*> childConstUnions; for (unsigned int arg = 0; arg < children.size(); ++arg) childConstUnions.push_back(children[arg]->getAsConstantUnion()->getUnionArrayPointer()); // Second, do the actual folding bool isFloatingPoint = children[0]->getAsTyped()->getBasicType() == EbtFloat || children[0]->getAsTyped()->getBasicType() == EbtDouble; bool isSigned = children[0]->getAsTyped()->getBasicType() == EbtInt; if (componentwise) { for (int comp = 0; comp < objectSize; comp++) { // some arguments are scalars instead of matching vectors; simulate a smear int arg0comp = std::min(comp, children[0]->getAsTyped()->getType().getVectorSize() - 1); int arg1comp; if (children.size() > 1) arg1comp = std::min(comp, children[1]->getAsTyped()->getType().getVectorSize() - 1); int arg2comp; if (children.size() > 2) arg2comp = std::min(comp, children[2]->getAsTyped()->getType().getVectorSize() - 1); switch (aggrNode->getOp()) { case EOpAtan: newConstArray[comp].setDConst(atan2(childConstUnions[0][arg0comp].getDConst(), childConstUnions[1][arg1comp].getDConst())); break; case EOpPow: newConstArray[comp].setDConst(pow(childConstUnions[0][arg0comp].getDConst(), childConstUnions[1][arg1comp].getDConst())); break; case EOpMin: if (isFloatingPoint) newConstArray[comp].setDConst(std::min(childConstUnions[0][arg0comp].getDConst(), childConstUnions[1][arg1comp].getDConst())); else if (isSigned) newConstArray[comp].setIConst(std::min(childConstUnions[0][arg0comp].getIConst(), childConstUnions[1][arg1comp].getIConst())); else newConstArray[comp].setUConst(std::min(childConstUnions[0][arg0comp].getUConst(), childConstUnions[1][arg1comp].getUConst())); break; case EOpMax: if (isFloatingPoint) newConstArray[comp].setDConst(std::max(childConstUnions[0][arg0comp].getDConst(), childConstUnions[1][arg1comp].getDConst())); else if (isSigned) newConstArray[comp].setIConst(std::max(childConstUnions[0][arg0comp].getIConst(), childConstUnions[1][arg1comp].getIConst())); else newConstArray[comp].setUConst(std::max(childConstUnions[0][arg0comp].getUConst(), childConstUnions[1][arg1comp].getUConst())); break; case EOpClamp: if (isFloatingPoint) newConstArray[comp].setDConst(std::min(std::max(childConstUnions[0][arg0comp].getDConst(), childConstUnions[1][arg1comp].getDConst()), childConstUnions[2][arg2comp].getDConst())); else if (isSigned) newConstArray[comp].setIConst(std::min(std::max(childConstUnions[0][arg0comp].getIConst(), childConstUnions[1][arg1comp].getIConst()), childConstUnions[2][arg2comp].getIConst())); else newConstArray[comp].setUConst(std::min(std::max(childConstUnions[0][arg0comp].getUConst(), childConstUnions[1][arg1comp].getUConst()), childConstUnions[2][arg2comp].getUConst())); break; case EOpMix: if (children[2]->getAsTyped()->getBasicType() == EbtBool) newConstArray[comp].setDConst(childConstUnions[2][arg2comp].getBConst() ? childConstUnions[1][arg1comp].getDConst() : childConstUnions[0][arg0comp].getDConst()); else newConstArray[comp].setDConst(childConstUnions[0][arg0comp].getDConst() * (1.0 - childConstUnions[2][arg2comp].getDConst()) + childConstUnions[1][arg1comp].getDConst() * childConstUnions[2][arg2comp].getDConst()); break; case EOpStep: newConstArray[comp].setDConst(childConstUnions[1][arg1comp].getDConst() < childConstUnions[0][arg0comp].getDConst() ? 0.0 : 1.0); break; case EOpSmoothStep: { double t = (childConstUnions[2][arg2comp].getDConst() - childConstUnions[0][arg0comp].getDConst()) / (childConstUnions[1][arg1comp].getDConst() - childConstUnions[0][arg0comp].getDConst()); if (t < 0.0) t = 0.0; if (t > 1.0) t = 1.0; newConstArray[comp].setDConst(t * t * (3.0 - 2.0 * t)); break; } default: return aggrNode; } } } else { // Non-componentwise... switch (aggrNode->getOp()) { // TODO: Functionality: constant folding: the rest of the ops have to be fleshed out case EOpModf: case EOpDistance: case EOpDot: case EOpCross: case EOpFaceForward: case EOpReflect: case EOpRefract: case EOpOuterProduct: return aggrNode; default: return aggrNode; } } TIntermConstantUnion *newNode = new TIntermConstantUnion(newConstArray, aggrNode->getType()); newNode->getWritableType().getQualifier().storage = EvqConst; newNode->setLoc(aggrNode->getLoc()); return newNode; }
// // Do constant folding for an aggregate node that has all its children // as constants and an operator that requires constant folding. // TIntermTyped* TIntermediate::fold(TIntermAggregate* aggrNode) { if (! areAllChildConst(aggrNode)) return aggrNode; if (aggrNode->isConstructor()) return foldConstructor(aggrNode); TIntermSequence& children = aggrNode->getSequence(); // First, see if this is an operation to constant fold, kick out if not, // see what size the result is if so. bool componentwise = false; // will also say componentwise if a scalar argument gets repeated to make per-component results int objectSize; switch (aggrNode->getOp()) { case EOpAtan: case EOpPow: case EOpMin: case EOpMax: case EOpMix: case EOpClamp: case EOpLessThan: case EOpGreaterThan: case EOpLessThanEqual: case EOpGreaterThanEqual: case EOpVectorEqual: case EOpVectorNotEqual: componentwise = true; objectSize = children[0]->getAsConstantUnion()->getType().computeNumComponents(); break; case EOpCross: case EOpReflect: case EOpRefract: case EOpFaceForward: objectSize = children[0]->getAsConstantUnion()->getType().computeNumComponents(); break; case EOpDistance: case EOpDot: objectSize = 1; break; case EOpOuterProduct: objectSize = children[0]->getAsTyped()->getType().getVectorSize() * children[1]->getAsTyped()->getType().getVectorSize(); break; case EOpStep: componentwise = true; objectSize = std::max(children[0]->getAsTyped()->getType().getVectorSize(), children[1]->getAsTyped()->getType().getVectorSize()); break; case EOpSmoothStep: componentwise = true; objectSize = std::max(children[0]->getAsTyped()->getType().getVectorSize(), children[2]->getAsTyped()->getType().getVectorSize()); break; default: return aggrNode; } TConstUnionArray newConstArray(objectSize); TVector<TConstUnionArray> childConstUnions; for (unsigned int arg = 0; arg < children.size(); ++arg) childConstUnions.push_back(children[arg]->getAsConstantUnion()->getConstArray()); // Second, do the actual folding bool isFloatingPoint = children[0]->getAsTyped()->getBasicType() == EbtFloat || children[0]->getAsTyped()->getBasicType() == EbtDouble; bool isSigned = children[0]->getAsTyped()->getBasicType() == EbtInt; if (componentwise) { for (int comp = 0; comp < objectSize; comp++) { // some arguments are scalars instead of matching vectors; simulate a smear int arg0comp = std::min(comp, children[0]->getAsTyped()->getType().getVectorSize() - 1); int arg1comp = 0; if (children.size() > 1) arg1comp = std::min(comp, children[1]->getAsTyped()->getType().getVectorSize() - 1); int arg2comp = 0; if (children.size() > 2) arg2comp = std::min(comp, children[2]->getAsTyped()->getType().getVectorSize() - 1); switch (aggrNode->getOp()) { case EOpAtan: newConstArray[comp].setDConst(atan2(childConstUnions[0][arg0comp].getDConst(), childConstUnions[1][arg1comp].getDConst())); break; case EOpPow: newConstArray[comp].setDConst(pow(childConstUnions[0][arg0comp].getDConst(), childConstUnions[1][arg1comp].getDConst())); break; case EOpMin: if (isFloatingPoint) newConstArray[comp].setDConst(std::min(childConstUnions[0][arg0comp].getDConst(), childConstUnions[1][arg1comp].getDConst())); else if (isSigned) newConstArray[comp].setIConst(std::min(childConstUnions[0][arg0comp].getIConst(), childConstUnions[1][arg1comp].getIConst())); else newConstArray[comp].setUConst(std::min(childConstUnions[0][arg0comp].getUConst(), childConstUnions[1][arg1comp].getUConst())); break; case EOpMax: if (isFloatingPoint) newConstArray[comp].setDConst(std::max(childConstUnions[0][arg0comp].getDConst(), childConstUnions[1][arg1comp].getDConst())); else if (isSigned) newConstArray[comp].setIConst(std::max(childConstUnions[0][arg0comp].getIConst(), childConstUnions[1][arg1comp].getIConst())); else newConstArray[comp].setUConst(std::max(childConstUnions[0][arg0comp].getUConst(), childConstUnions[1][arg1comp].getUConst())); break; case EOpClamp: if (isFloatingPoint) newConstArray[comp].setDConst(std::min(std::max(childConstUnions[0][arg0comp].getDConst(), childConstUnions[1][arg1comp].getDConst()), childConstUnions[2][arg2comp].getDConst())); else if (isSigned) newConstArray[comp].setIConst(std::min(std::max(childConstUnions[0][arg0comp].getIConst(), childConstUnions[1][arg1comp].getIConst()), childConstUnions[2][arg2comp].getIConst())); else newConstArray[comp].setUConst(std::min(std::max(childConstUnions[0][arg0comp].getUConst(), childConstUnions[1][arg1comp].getUConst()), childConstUnions[2][arg2comp].getUConst())); break; case EOpLessThan: newConstArray[comp].setBConst(childConstUnions[0][arg0comp] < childConstUnions[1][arg1comp]); break; case EOpGreaterThan: newConstArray[comp].setBConst(childConstUnions[0][arg0comp] > childConstUnions[1][arg1comp]); break; case EOpLessThanEqual: newConstArray[comp].setBConst(! (childConstUnions[0][arg0comp] > childConstUnions[1][arg1comp])); break; case EOpGreaterThanEqual: newConstArray[comp].setBConst(! (childConstUnions[0][arg0comp] < childConstUnions[1][arg1comp])); break; case EOpVectorEqual: newConstArray[comp].setBConst(childConstUnions[0][arg0comp] == childConstUnions[1][arg1comp]); break; case EOpVectorNotEqual: newConstArray[comp].setBConst(childConstUnions[0][arg0comp] != childConstUnions[1][arg1comp]); break; case EOpMix: if (children[2]->getAsTyped()->getBasicType() == EbtBool) newConstArray[comp].setDConst(childConstUnions[2][arg2comp].getBConst() ? childConstUnions[1][arg1comp].getDConst() : childConstUnions[0][arg0comp].getDConst()); else newConstArray[comp].setDConst(childConstUnions[0][arg0comp].getDConst() * (1.0 - childConstUnions[2][arg2comp].getDConst()) + childConstUnions[1][arg1comp].getDConst() * childConstUnions[2][arg2comp].getDConst()); break; case EOpStep: newConstArray[comp].setDConst(childConstUnions[1][arg1comp].getDConst() < childConstUnions[0][arg0comp].getDConst() ? 0.0 : 1.0); break; case EOpSmoothStep: { double t = (childConstUnions[2][arg2comp].getDConst() - childConstUnions[0][arg0comp].getDConst()) / (childConstUnions[1][arg1comp].getDConst() - childConstUnions[0][arg0comp].getDConst()); if (t < 0.0) t = 0.0; if (t > 1.0) t = 1.0; newConstArray[comp].setDConst(t * t * (3.0 - 2.0 * t)); break; } default: return aggrNode; } } } else { // Non-componentwise... int numComps = children[0]->getAsConstantUnion()->getType().computeNumComponents(); double dot; switch (aggrNode->getOp()) { case EOpDistance: { double sum = 0.0; for (int comp = 0; comp < numComps; ++comp) { double diff = childConstUnions[1][comp].getDConst() - childConstUnions[0][comp].getDConst(); sum += diff * diff; } newConstArray[0].setDConst(sqrt(sum)); break; } case EOpDot: newConstArray[0].setDConst(childConstUnions[0].dot(childConstUnions[1])); break; case EOpCross: newConstArray[0] = childConstUnions[0][1] * childConstUnions[1][2] - childConstUnions[0][2] * childConstUnions[1][1]; newConstArray[1] = childConstUnions[0][2] * childConstUnions[1][0] - childConstUnions[0][0] * childConstUnions[1][2]; newConstArray[2] = childConstUnions[0][0] * childConstUnions[1][1] - childConstUnions[0][1] * childConstUnions[1][0]; break; case EOpFaceForward: // If dot(Nref, I) < 0 return N, otherwise return –N: Arguments are (N, I, Nref). dot = childConstUnions[1].dot(childConstUnions[2]); for (int comp = 0; comp < numComps; ++comp) { if (dot < 0.0) newConstArray[comp] = childConstUnions[0][comp]; else newConstArray[comp].setDConst(-childConstUnions[0][comp].getDConst()); } break; case EOpReflect: // I – 2 * dot(N, I) * N: Arguments are (I, N). dot = childConstUnions[0].dot(childConstUnions[1]); dot *= 2.0; for (int comp = 0; comp < numComps; ++comp) newConstArray[comp].setDConst(childConstUnions[0][comp].getDConst() - dot * childConstUnions[1][comp].getDConst()); break; case EOpRefract: { // Arguments are (I, N, eta). // k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I)) // if (k < 0.0) // return dvec(0.0) // else // return eta * I - (eta * dot(N, I) + sqrt(k)) * N dot = childConstUnions[0].dot(childConstUnions[1]); double eta = childConstUnions[2][0].getDConst(); double k = 1.0 - eta * eta * (1.0 - dot * dot); if (k < 0.0) { for (int comp = 0; comp < numComps; ++comp) newConstArray[comp].setDConst(0.0); } else { for (int comp = 0; comp < numComps; ++comp) newConstArray[comp].setDConst(eta * childConstUnions[0][comp].getDConst() - (eta * dot + sqrt(k)) * childConstUnions[1][comp].getDConst()); } break; } case EOpOuterProduct: { int numRows = numComps; int numCols = children[1]->getAsConstantUnion()->getType().computeNumComponents(); for (int row = 0; row < numRows; ++row) for (int col = 0; col < numCols; ++col) newConstArray[col * numRows + row] = childConstUnions[0][row] * childConstUnions[1][col]; break; } default: return aggrNode; } } TIntermConstantUnion *newNode = new TIntermConstantUnion(newConstArray, aggrNode->getType()); newNode->getWritableType().getQualifier().storage = EvqConst; newNode->setLoc(aggrNode->getLoc()); return newNode; }