michael@0: // michael@0: // Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: #include "compiler/OutputGLSLBase.h" michael@0: #include "compiler/compiler_debug.h" michael@0: michael@0: #include michael@0: michael@0: namespace michael@0: { michael@0: TString arrayBrackets(const TType& type) michael@0: { michael@0: ASSERT(type.isArray()); michael@0: TInfoSinkBase out; michael@0: out << "[" << type.getArraySize() << "]"; michael@0: return TString(out.c_str()); michael@0: } michael@0: michael@0: bool isSingleStatement(TIntermNode* node) { michael@0: if (const TIntermAggregate* aggregate = node->getAsAggregate()) michael@0: { michael@0: return (aggregate->getOp() != EOpFunction) && michael@0: (aggregate->getOp() != EOpSequence); michael@0: } michael@0: else if (const TIntermSelection* selection = node->getAsSelectionNode()) michael@0: { michael@0: // Ternary operators are usually part of an assignment operator. michael@0: // This handles those rare cases in which they are all by themselves. michael@0: return selection->usesTernaryOperator(); michael@0: } michael@0: else if (node->getAsLoopNode()) michael@0: { michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: } // namespace michael@0: michael@0: TOutputGLSLBase::TOutputGLSLBase(TInfoSinkBase& objSink, michael@0: ShArrayIndexClampingStrategy clampingStrategy, michael@0: ShHashFunction64 hashFunction, michael@0: NameMap& nameMap, michael@0: TSymbolTable& symbolTable) michael@0: : TIntermTraverser(true, true, true), michael@0: mObjSink(objSink), michael@0: mDeclaringVariables(false), michael@0: mClampingStrategy(clampingStrategy), michael@0: mHashFunction(hashFunction), michael@0: mNameMap(nameMap), michael@0: mSymbolTable(symbolTable) michael@0: { michael@0: } michael@0: michael@0: void TOutputGLSLBase::writeTriplet(Visit visit, const char* preStr, const char* inStr, const char* postStr) michael@0: { michael@0: TInfoSinkBase& out = objSink(); michael@0: if (visit == PreVisit && preStr) michael@0: { michael@0: out << preStr; michael@0: } michael@0: else if (visit == InVisit && inStr) michael@0: { michael@0: out << inStr; michael@0: } michael@0: else if (visit == PostVisit && postStr) michael@0: { michael@0: out << postStr; michael@0: } michael@0: } michael@0: michael@0: void TOutputGLSLBase::writeVariableType(const TType& type) michael@0: { michael@0: TInfoSinkBase& out = objSink(); michael@0: TQualifier qualifier = type.getQualifier(); michael@0: // TODO(alokp): Validate qualifier for variable declarations. michael@0: if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal)) michael@0: out << type.getQualifierString() << " "; michael@0: // Declare the struct if we have not done so already. michael@0: if ((type.getBasicType() == EbtStruct) && !structDeclared(type.getStruct())) michael@0: { michael@0: declareStruct(type.getStruct()); michael@0: } michael@0: else michael@0: { michael@0: if (writeVariablePrecision(type.getPrecision())) michael@0: out << " "; michael@0: out << getTypeName(type); michael@0: } michael@0: } michael@0: michael@0: void TOutputGLSLBase::writeFunctionParameters(const TIntermSequence& args) michael@0: { michael@0: TInfoSinkBase& out = objSink(); michael@0: for (TIntermSequence::const_iterator iter = args.begin(); michael@0: iter != args.end(); ++iter) michael@0: { michael@0: const TIntermSymbol* arg = (*iter)->getAsSymbolNode(); michael@0: ASSERT(arg != NULL); michael@0: michael@0: const TType& type = arg->getType(); michael@0: writeVariableType(type); michael@0: michael@0: const TString& name = arg->getSymbol(); michael@0: if (!name.empty()) michael@0: out << " " << hashName(name); michael@0: if (type.isArray()) michael@0: out << arrayBrackets(type); michael@0: michael@0: // Put a comma if this is not the last argument. michael@0: if (iter != args.end() - 1) michael@0: out << ", "; michael@0: } michael@0: } michael@0: michael@0: const ConstantUnion* TOutputGLSLBase::writeConstantUnion(const TType& type, michael@0: const ConstantUnion* pConstUnion) michael@0: { michael@0: TInfoSinkBase& out = objSink(); michael@0: michael@0: if (type.getBasicType() == EbtStruct) michael@0: { michael@0: const TStructure* structure = type.getStruct(); michael@0: out << hashName(structure->name()) << "("; michael@0: michael@0: const TFieldList& fields = structure->fields(); michael@0: for (size_t i = 0; i < fields.size(); ++i) michael@0: { michael@0: const TType* fieldType = fields[i]->type(); michael@0: ASSERT(fieldType != NULL); michael@0: pConstUnion = writeConstantUnion(*fieldType, pConstUnion); michael@0: if (i != fields.size() - 1) out << ", "; michael@0: } michael@0: out << ")"; michael@0: } michael@0: else michael@0: { michael@0: size_t size = type.getObjectSize(); michael@0: bool writeType = size > 1; michael@0: if (writeType) out << getTypeName(type) << "("; michael@0: for (size_t i = 0; i < size; ++i, ++pConstUnion) michael@0: { michael@0: switch (pConstUnion->getType()) michael@0: { michael@0: case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, pConstUnion->getFConst())); break; michael@0: case EbtInt: out << pConstUnion->getIConst(); break; michael@0: case EbtBool: out << pConstUnion->getBConst(); break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: if (i != size - 1) out << ", "; michael@0: } michael@0: if (writeType) out << ")"; michael@0: } michael@0: return pConstUnion; michael@0: } michael@0: michael@0: void TOutputGLSLBase::visitSymbol(TIntermSymbol* node) michael@0: { michael@0: TInfoSinkBase& out = objSink(); michael@0: if (mLoopUnroll.NeedsToReplaceSymbolWithValue(node)) michael@0: out << mLoopUnroll.GetLoopIndexValue(node); michael@0: else michael@0: out << hashVariableName(node->getSymbol()); michael@0: michael@0: if (mDeclaringVariables && node->getType().isArray()) michael@0: out << arrayBrackets(node->getType()); michael@0: } michael@0: michael@0: void TOutputGLSLBase::visitConstantUnion(TIntermConstantUnion* node) michael@0: { michael@0: writeConstantUnion(node->getType(), node->getUnionArrayPointer()); michael@0: } michael@0: michael@0: bool TOutputGLSLBase::visitBinary(Visit visit, TIntermBinary* node) michael@0: { michael@0: bool visitChildren = true; michael@0: TInfoSinkBase& out = objSink(); michael@0: switch (node->getOp()) michael@0: { michael@0: case EOpInitialize: michael@0: if (visit == InVisit) michael@0: { michael@0: out << " = "; michael@0: // RHS of initialize is not being declared. michael@0: mDeclaringVariables = false; michael@0: } michael@0: break; michael@0: case EOpAssign: writeTriplet(visit, "(", " = ", ")"); break; michael@0: case EOpAddAssign: writeTriplet(visit, "(", " += ", ")"); break; michael@0: case EOpSubAssign: writeTriplet(visit, "(", " -= ", ")"); break; michael@0: case EOpDivAssign: writeTriplet(visit, "(", " /= ", ")"); break; michael@0: // Notice the fall-through. michael@0: case EOpMulAssign: michael@0: case EOpVectorTimesMatrixAssign: michael@0: case EOpVectorTimesScalarAssign: michael@0: case EOpMatrixTimesScalarAssign: michael@0: case EOpMatrixTimesMatrixAssign: michael@0: writeTriplet(visit, "(", " *= ", ")"); michael@0: break; michael@0: michael@0: case EOpIndexDirect: michael@0: writeTriplet(visit, NULL, "[", "]"); michael@0: break; michael@0: case EOpIndexIndirect: michael@0: if (node->getAddIndexClamp()) michael@0: { michael@0: if (visit == InVisit) michael@0: { michael@0: if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC) { michael@0: out << "[int(clamp(float("; michael@0: } else { michael@0: out << "[webgl_int_clamp("; michael@0: } michael@0: } michael@0: else if (visit == PostVisit) michael@0: { michael@0: int maxSize; michael@0: TIntermTyped *left = node->getLeft(); michael@0: TType leftType = left->getType(); michael@0: michael@0: if (left->isArray()) michael@0: { michael@0: // The shader will fail validation if the array length is not > 0. michael@0: maxSize = leftType.getArraySize() - 1; michael@0: } michael@0: else michael@0: { michael@0: maxSize = leftType.getNominalSize() - 1; michael@0: } michael@0: michael@0: if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC) { michael@0: out << "), 0.0, float(" << maxSize << ")))]"; michael@0: } else { michael@0: out << ", 0, " << maxSize << ")]"; michael@0: } michael@0: } michael@0: } michael@0: else michael@0: { michael@0: writeTriplet(visit, NULL, "[", "]"); michael@0: } michael@0: break; michael@0: case EOpIndexDirectStruct: michael@0: if (visit == InVisit) michael@0: { michael@0: // Here we are writing out "foo.bar", where "foo" is struct michael@0: // and "bar" is field. In AST, it is represented as a binary michael@0: // node, where left child represents "foo" and right child "bar". michael@0: // The node itself represents ".". The struct field "bar" is michael@0: // actually stored as an index into TStructure::fields. michael@0: out << "."; michael@0: const TStructure* structure = node->getLeft()->getType().getStruct(); michael@0: const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion(); michael@0: const TField* field = structure->fields()[index->getIConst(0)]; michael@0: michael@0: TString fieldName = field->name(); michael@0: if (!mSymbolTable.findBuiltIn(structure->name())) michael@0: fieldName = hashName(fieldName); michael@0: michael@0: out << fieldName; michael@0: visitChildren = false; michael@0: } michael@0: break; michael@0: case EOpVectorSwizzle: michael@0: if (visit == InVisit) michael@0: { michael@0: out << "."; michael@0: TIntermAggregate* rightChild = node->getRight()->getAsAggregate(); michael@0: TIntermSequence& sequence = rightChild->getSequence(); michael@0: for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); ++sit) michael@0: { michael@0: TIntermConstantUnion* element = (*sit)->getAsConstantUnion(); michael@0: ASSERT(element->getBasicType() == EbtInt); michael@0: ASSERT(element->getNominalSize() == 1); michael@0: const ConstantUnion& data = element->getUnionArrayPointer()[0]; michael@0: ASSERT(data.getType() == EbtInt); michael@0: switch (data.getIConst()) michael@0: { michael@0: case 0: out << "x"; break; michael@0: case 1: out << "y"; break; michael@0: case 2: out << "z"; break; michael@0: case 3: out << "w"; break; michael@0: default: UNREACHABLE(); break; michael@0: } michael@0: } michael@0: visitChildren = false; michael@0: } michael@0: break; michael@0: michael@0: case EOpAdd: writeTriplet(visit, "(", " + ", ")"); break; michael@0: case EOpSub: writeTriplet(visit, "(", " - ", ")"); break; michael@0: case EOpMul: writeTriplet(visit, "(", " * ", ")"); break; michael@0: case EOpDiv: writeTriplet(visit, "(", " / ", ")"); break; michael@0: case EOpMod: UNIMPLEMENTED(); break; michael@0: case EOpEqual: writeTriplet(visit, "(", " == ", ")"); break; michael@0: case EOpNotEqual: writeTriplet(visit, "(", " != ", ")"); break; michael@0: case EOpLessThan: writeTriplet(visit, "(", " < ", ")"); break; michael@0: case EOpGreaterThan: writeTriplet(visit, "(", " > ", ")"); break; michael@0: case EOpLessThanEqual: writeTriplet(visit, "(", " <= ", ")"); break; michael@0: case EOpGreaterThanEqual: writeTriplet(visit, "(", " >= ", ")"); break; michael@0: michael@0: // Notice the fall-through. michael@0: case EOpVectorTimesScalar: michael@0: case EOpVectorTimesMatrix: michael@0: case EOpMatrixTimesVector: michael@0: case EOpMatrixTimesScalar: michael@0: case EOpMatrixTimesMatrix: michael@0: writeTriplet(visit, "(", " * ", ")"); michael@0: break; michael@0: michael@0: case EOpLogicalOr: writeTriplet(visit, "(", " || ", ")"); break; michael@0: case EOpLogicalXor: writeTriplet(visit, "(", " ^^ ", ")"); break; michael@0: case EOpLogicalAnd: writeTriplet(visit, "(", " && ", ")"); break; michael@0: default: UNREACHABLE(); break; michael@0: } michael@0: michael@0: return visitChildren; michael@0: } michael@0: michael@0: bool TOutputGLSLBase::visitUnary(Visit visit, TIntermUnary* node) michael@0: { michael@0: TString preString; michael@0: TString postString = ")"; michael@0: michael@0: switch (node->getOp()) michael@0: { michael@0: case EOpNegative: preString = "(-"; break; michael@0: case EOpVectorLogicalNot: preString = "not("; break; michael@0: case EOpLogicalNot: preString = "(!"; break; michael@0: michael@0: case EOpPostIncrement: preString = "("; postString = "++)"; break; michael@0: case EOpPostDecrement: preString = "("; postString = "--)"; break; michael@0: case EOpPreIncrement: preString = "(++"; break; michael@0: case EOpPreDecrement: preString = "(--"; break; michael@0: michael@0: case EOpConvIntToBool: michael@0: case EOpConvFloatToBool: michael@0: switch (node->getOperand()->getType().getNominalSize()) michael@0: { michael@0: case 1: preString = "bool("; break; michael@0: case 2: preString = "bvec2("; break; michael@0: case 3: preString = "bvec3("; break; michael@0: case 4: preString = "bvec4("; break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: break; michael@0: case EOpConvBoolToFloat: michael@0: case EOpConvIntToFloat: michael@0: switch (node->getOperand()->getType().getNominalSize()) michael@0: { michael@0: case 1: preString = "float("; break; michael@0: case 2: preString = "vec2("; break; michael@0: case 3: preString = "vec3("; break; michael@0: case 4: preString = "vec4("; break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: break; michael@0: case EOpConvFloatToInt: michael@0: case EOpConvBoolToInt: michael@0: switch (node->getOperand()->getType().getNominalSize()) michael@0: { michael@0: case 1: preString = "int("; break; michael@0: case 2: preString = "ivec2("; break; michael@0: case 3: preString = "ivec3("; break; michael@0: case 4: preString = "ivec4("; break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: break; michael@0: michael@0: case EOpRadians: preString = "radians("; break; michael@0: case EOpDegrees: preString = "degrees("; break; michael@0: case EOpSin: preString = "sin("; break; michael@0: case EOpCos: preString = "cos("; break; michael@0: case EOpTan: preString = "tan("; break; michael@0: case EOpAsin: preString = "asin("; break; michael@0: case EOpAcos: preString = "acos("; break; michael@0: case EOpAtan: preString = "atan("; break; michael@0: michael@0: case EOpExp: preString = "exp("; break; michael@0: case EOpLog: preString = "log("; break; michael@0: case EOpExp2: preString = "exp2("; break; michael@0: case EOpLog2: preString = "log2("; break; michael@0: case EOpSqrt: preString = "sqrt("; break; michael@0: case EOpInverseSqrt: preString = "inversesqrt("; break; michael@0: michael@0: case EOpAbs: preString = "abs("; break; michael@0: case EOpSign: preString = "sign("; break; michael@0: case EOpFloor: preString = "floor("; break; michael@0: case EOpCeil: preString = "ceil("; break; michael@0: case EOpFract: preString = "fract("; break; michael@0: michael@0: case EOpLength: preString = "length("; break; michael@0: case EOpNormalize: preString = "normalize("; break; michael@0: michael@0: case EOpDFdx: preString = "dFdx("; break; michael@0: case EOpDFdy: preString = "dFdy("; break; michael@0: case EOpFwidth: preString = "fwidth("; break; michael@0: michael@0: case EOpAny: preString = "any("; break; michael@0: case EOpAll: preString = "all("; break; michael@0: michael@0: default: UNREACHABLE(); break; michael@0: } michael@0: michael@0: if (visit == PreVisit && node->getUseEmulatedFunction()) michael@0: preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preString); michael@0: writeTriplet(visit, preString.c_str(), NULL, postString.c_str()); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool TOutputGLSLBase::visitSelection(Visit visit, TIntermSelection* node) michael@0: { michael@0: TInfoSinkBase& out = objSink(); michael@0: michael@0: if (node->usesTernaryOperator()) michael@0: { michael@0: // Notice two brackets at the beginning and end. The outer ones michael@0: // encapsulate the whole ternary expression. This preserves the michael@0: // order of precedence when ternary expressions are used in a michael@0: // compound expression, i.e., c = 2 * (a < b ? 1 : 2). michael@0: out << "(("; michael@0: node->getCondition()->traverse(this); michael@0: out << ") ? ("; michael@0: node->getTrueBlock()->traverse(this); michael@0: out << ") : ("; michael@0: node->getFalseBlock()->traverse(this); michael@0: out << "))"; michael@0: } michael@0: else michael@0: { michael@0: out << "if ("; michael@0: node->getCondition()->traverse(this); michael@0: out << ")\n"; michael@0: michael@0: incrementDepth(); michael@0: visitCodeBlock(node->getTrueBlock()); michael@0: michael@0: if (node->getFalseBlock()) michael@0: { michael@0: out << "else\n"; michael@0: visitCodeBlock(node->getFalseBlock()); michael@0: } michael@0: decrementDepth(); michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: bool TOutputGLSLBase::visitAggregate(Visit visit, TIntermAggregate* node) michael@0: { michael@0: bool visitChildren = true; michael@0: TInfoSinkBase& out = objSink(); michael@0: TString preString; michael@0: bool delayedWrite = false; michael@0: switch (node->getOp()) michael@0: { michael@0: case EOpSequence: { michael@0: // Scope the sequences except when at the global scope. michael@0: if (depth > 0) out << "{\n"; michael@0: michael@0: incrementDepth(); michael@0: const TIntermSequence& sequence = node->getSequence(); michael@0: for (TIntermSequence::const_iterator iter = sequence.begin(); michael@0: iter != sequence.end(); ++iter) michael@0: { michael@0: TIntermNode* node = *iter; michael@0: ASSERT(node != NULL); michael@0: node->traverse(this); michael@0: michael@0: if (isSingleStatement(node)) michael@0: out << ";\n"; michael@0: } michael@0: decrementDepth(); michael@0: michael@0: // Scope the sequences except when at the global scope. michael@0: if (depth > 0) out << "}\n"; michael@0: visitChildren = false; michael@0: break; michael@0: } michael@0: case EOpPrototype: { michael@0: // Function declaration. michael@0: ASSERT(visit == PreVisit); michael@0: writeVariableType(node->getType()); michael@0: out << " " << hashName(node->getName()); michael@0: michael@0: out << "("; michael@0: writeFunctionParameters(node->getSequence()); michael@0: out << ")"; michael@0: michael@0: visitChildren = false; michael@0: break; michael@0: } michael@0: case EOpFunction: { michael@0: // Function definition. michael@0: ASSERT(visit == PreVisit); michael@0: writeVariableType(node->getType()); michael@0: out << " " << hashFunctionName(node->getName()); michael@0: michael@0: incrementDepth(); michael@0: // Function definition node contains one or two children nodes michael@0: // representing function parameters and function body. The latter michael@0: // is not present in case of empty function bodies. michael@0: const TIntermSequence& sequence = node->getSequence(); michael@0: ASSERT((sequence.size() == 1) || (sequence.size() == 2)); michael@0: TIntermSequence::const_iterator seqIter = sequence.begin(); michael@0: michael@0: // Traverse function parameters. michael@0: TIntermAggregate* params = (*seqIter)->getAsAggregate(); michael@0: ASSERT(params != NULL); michael@0: ASSERT(params->getOp() == EOpParameters); michael@0: params->traverse(this); michael@0: michael@0: // Traverse function body. michael@0: TIntermAggregate* body = ++seqIter != sequence.end() ? michael@0: (*seqIter)->getAsAggregate() : NULL; michael@0: visitCodeBlock(body); michael@0: decrementDepth(); michael@0: michael@0: // Fully processed; no need to visit children. michael@0: visitChildren = false; michael@0: break; michael@0: } michael@0: case EOpFunctionCall: michael@0: // Function call. michael@0: if (visit == PreVisit) michael@0: { michael@0: out << hashFunctionName(node->getName()) << "("; michael@0: } michael@0: else if (visit == InVisit) michael@0: { michael@0: out << ", "; michael@0: } michael@0: else michael@0: { michael@0: out << ")"; michael@0: } michael@0: break; michael@0: case EOpParameters: { michael@0: // Function parameters. michael@0: ASSERT(visit == PreVisit); michael@0: out << "("; michael@0: writeFunctionParameters(node->getSequence()); michael@0: out << ")"; michael@0: visitChildren = false; michael@0: break; michael@0: } michael@0: case EOpDeclaration: { michael@0: // Variable declaration. michael@0: if (visit == PreVisit) michael@0: { michael@0: const TIntermSequence& sequence = node->getSequence(); michael@0: const TIntermTyped* variable = sequence.front()->getAsTyped(); michael@0: writeVariableType(variable->getType()); michael@0: out << " "; michael@0: mDeclaringVariables = true; michael@0: } michael@0: else if (visit == InVisit) michael@0: { michael@0: out << ", "; michael@0: mDeclaringVariables = true; michael@0: } michael@0: else michael@0: { michael@0: mDeclaringVariables = false; michael@0: } michael@0: break; michael@0: } michael@0: case EOpConstructFloat: writeTriplet(visit, "float(", NULL, ")"); break; michael@0: case EOpConstructVec2: writeTriplet(visit, "vec2(", ", ", ")"); break; michael@0: case EOpConstructVec3: writeTriplet(visit, "vec3(", ", ", ")"); break; michael@0: case EOpConstructVec4: writeTriplet(visit, "vec4(", ", ", ")"); break; michael@0: case EOpConstructBool: writeTriplet(visit, "bool(", NULL, ")"); break; michael@0: case EOpConstructBVec2: writeTriplet(visit, "bvec2(", ", ", ")"); break; michael@0: case EOpConstructBVec3: writeTriplet(visit, "bvec3(", ", ", ")"); break; michael@0: case EOpConstructBVec4: writeTriplet(visit, "bvec4(", ", ", ")"); break; michael@0: case EOpConstructInt: writeTriplet(visit, "int(", NULL, ")"); break; michael@0: case EOpConstructIVec2: writeTriplet(visit, "ivec2(", ", ", ")"); break; michael@0: case EOpConstructIVec3: writeTriplet(visit, "ivec3(", ", ", ")"); break; michael@0: case EOpConstructIVec4: writeTriplet(visit, "ivec4(", ", ", ")"); break; michael@0: case EOpConstructMat2: writeTriplet(visit, "mat2(", ", ", ")"); break; michael@0: case EOpConstructMat3: writeTriplet(visit, "mat3(", ", ", ")"); break; michael@0: case EOpConstructMat4: writeTriplet(visit, "mat4(", ", ", ")"); break; michael@0: case EOpConstructStruct: michael@0: if (visit == PreVisit) michael@0: { michael@0: const TType& type = node->getType(); michael@0: ASSERT(type.getBasicType() == EbtStruct); michael@0: out << hashName(type.getStruct()->name()) << "("; michael@0: } michael@0: else if (visit == InVisit) michael@0: { michael@0: out << ", "; michael@0: } michael@0: else michael@0: { michael@0: out << ")"; michael@0: } michael@0: break; michael@0: michael@0: case EOpLessThan: preString = "lessThan("; delayedWrite = true; break; michael@0: case EOpGreaterThan: preString = "greaterThan("; delayedWrite = true; break; michael@0: case EOpLessThanEqual: preString = "lessThanEqual("; delayedWrite = true; break; michael@0: case EOpGreaterThanEqual: preString = "greaterThanEqual("; delayedWrite = true; break; michael@0: case EOpVectorEqual: preString = "equal("; delayedWrite = true; break; michael@0: case EOpVectorNotEqual: preString = "notEqual("; delayedWrite = true; break; michael@0: case EOpComma: writeTriplet(visit, NULL, ", ", NULL); break; michael@0: michael@0: case EOpMod: preString = "mod("; delayedWrite = true; break; michael@0: case EOpPow: preString = "pow("; delayedWrite = true; break; michael@0: case EOpAtan: preString = "atan("; delayedWrite = true; break; michael@0: case EOpMin: preString = "min("; delayedWrite = true; break; michael@0: case EOpMax: preString = "max("; delayedWrite = true; break; michael@0: case EOpClamp: preString = "clamp("; delayedWrite = true; break; michael@0: case EOpMix: preString = "mix("; delayedWrite = true; break; michael@0: case EOpStep: preString = "step("; delayedWrite = true; break; michael@0: case EOpSmoothStep: preString = "smoothstep("; delayedWrite = true; break; michael@0: michael@0: case EOpDistance: preString = "distance("; delayedWrite = true; break; michael@0: case EOpDot: preString = "dot("; delayedWrite = true; break; michael@0: case EOpCross: preString = "cross("; delayedWrite = true; break; michael@0: case EOpFaceForward: preString = "faceforward("; delayedWrite = true; break; michael@0: case EOpReflect: preString = "reflect("; delayedWrite = true; break; michael@0: case EOpRefract: preString = "refract("; delayedWrite = true; break; michael@0: case EOpMul: preString = "matrixCompMult("; delayedWrite = true; break; michael@0: michael@0: default: UNREACHABLE(); break; michael@0: } michael@0: if (delayedWrite && visit == PreVisit && node->getUseEmulatedFunction()) michael@0: preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preString); michael@0: if (delayedWrite) michael@0: writeTriplet(visit, preString.c_str(), ", ", ")"); michael@0: return visitChildren; michael@0: } michael@0: michael@0: bool TOutputGLSLBase::visitLoop(Visit visit, TIntermLoop* node) michael@0: { michael@0: TInfoSinkBase& out = objSink(); michael@0: michael@0: incrementDepth(); michael@0: // Loop header. michael@0: TLoopType loopType = node->getType(); michael@0: if (loopType == ELoopFor) // for loop michael@0: { michael@0: if (!node->getUnrollFlag()) { michael@0: out << "for ("; michael@0: if (node->getInit()) michael@0: node->getInit()->traverse(this); michael@0: out << "; "; michael@0: michael@0: if (node->getCondition()) michael@0: node->getCondition()->traverse(this); michael@0: out << "; "; michael@0: michael@0: if (node->getExpression()) michael@0: node->getExpression()->traverse(this); michael@0: out << ")\n"; michael@0: } michael@0: } michael@0: else if (loopType == ELoopWhile) // while loop michael@0: { michael@0: out << "while ("; michael@0: ASSERT(node->getCondition() != NULL); michael@0: node->getCondition()->traverse(this); michael@0: out << ")\n"; michael@0: } michael@0: else // do-while loop michael@0: { michael@0: ASSERT(loopType == ELoopDoWhile); michael@0: out << "do\n"; michael@0: } michael@0: michael@0: // Loop body. michael@0: if (node->getUnrollFlag()) michael@0: { michael@0: TLoopIndexInfo indexInfo; michael@0: mLoopUnroll.FillLoopIndexInfo(node, indexInfo); michael@0: mLoopUnroll.Push(indexInfo); michael@0: while (mLoopUnroll.SatisfiesLoopCondition()) michael@0: { michael@0: visitCodeBlock(node->getBody()); michael@0: mLoopUnroll.Step(); michael@0: } michael@0: mLoopUnroll.Pop(); michael@0: } michael@0: else michael@0: { michael@0: visitCodeBlock(node->getBody()); michael@0: } michael@0: michael@0: // Loop footer. michael@0: if (loopType == ELoopDoWhile) // do-while loop michael@0: { michael@0: out << "while ("; michael@0: ASSERT(node->getCondition() != NULL); michael@0: node->getCondition()->traverse(this); michael@0: out << ");\n"; michael@0: } michael@0: decrementDepth(); michael@0: michael@0: // No need to visit children. They have been already processed in michael@0: // this function. michael@0: return false; michael@0: } michael@0: michael@0: bool TOutputGLSLBase::visitBranch(Visit visit, TIntermBranch* node) michael@0: { michael@0: switch (node->getFlowOp()) michael@0: { michael@0: case EOpKill: writeTriplet(visit, "discard", NULL, NULL); break; michael@0: case EOpBreak: writeTriplet(visit, "break", NULL, NULL); break; michael@0: case EOpContinue: writeTriplet(visit, "continue", NULL, NULL); break; michael@0: case EOpReturn: writeTriplet(visit, "return ", NULL, NULL); break; michael@0: default: UNREACHABLE(); break; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void TOutputGLSLBase::visitCodeBlock(TIntermNode* node) { michael@0: TInfoSinkBase &out = objSink(); michael@0: if (node != NULL) michael@0: { michael@0: node->traverse(this); michael@0: // Single statements not part of a sequence need to be terminated michael@0: // with semi-colon. michael@0: if (isSingleStatement(node)) michael@0: out << ";\n"; michael@0: } michael@0: else michael@0: { michael@0: out << "{\n}\n"; // Empty code block. michael@0: } michael@0: } michael@0: michael@0: TString TOutputGLSLBase::getTypeName(const TType& type) michael@0: { michael@0: TInfoSinkBase out; michael@0: if (type.isMatrix()) michael@0: { michael@0: out << "mat"; michael@0: out << type.getNominalSize(); michael@0: } michael@0: else if (type.isVector()) michael@0: { michael@0: switch (type.getBasicType()) michael@0: { michael@0: case EbtFloat: out << "vec"; break; michael@0: case EbtInt: out << "ivec"; break; michael@0: case EbtBool: out << "bvec"; break; michael@0: default: UNREACHABLE(); break; michael@0: } michael@0: out << type.getNominalSize(); michael@0: } michael@0: else michael@0: { michael@0: if (type.getBasicType() == EbtStruct) michael@0: out << hashName(type.getStruct()->name()); michael@0: else michael@0: out << type.getBasicString(); michael@0: } michael@0: return TString(out.c_str()); michael@0: } michael@0: michael@0: TString TOutputGLSLBase::hashName(const TString& name) michael@0: { michael@0: if (mHashFunction == NULL || name.empty()) michael@0: return name; michael@0: NameMap::const_iterator it = mNameMap.find(name.c_str()); michael@0: if (it != mNameMap.end()) michael@0: return it->second.c_str(); michael@0: TString hashedName = TIntermTraverser::hash(name, mHashFunction); michael@0: mNameMap[name.c_str()] = hashedName.c_str(); michael@0: return hashedName; michael@0: } michael@0: michael@0: TString TOutputGLSLBase::hashVariableName(const TString& name) michael@0: { michael@0: if (mSymbolTable.findBuiltIn(name) != NULL) michael@0: return name; michael@0: return hashName(name); michael@0: } michael@0: michael@0: TString TOutputGLSLBase::hashFunctionName(const TString& mangled_name) michael@0: { michael@0: TString name = TFunction::unmangleName(mangled_name); michael@0: if (mSymbolTable.findBuiltIn(mangled_name) != NULL || name == "main") michael@0: return name; michael@0: return hashName(name); michael@0: } michael@0: michael@0: bool TOutputGLSLBase::structDeclared(const TStructure* structure) const michael@0: { michael@0: return mDeclaredStructs.find(structure->name()) != mDeclaredStructs.end(); michael@0: } michael@0: michael@0: void TOutputGLSLBase::declareStruct(const TStructure* structure) michael@0: { michael@0: TInfoSinkBase& out = objSink(); michael@0: michael@0: out << "struct " << hashName(structure->name()) << "{\n"; michael@0: const TFieldList& fields = structure->fields(); michael@0: for (size_t i = 0; i < fields.size(); ++i) michael@0: { michael@0: const TField* field = fields[i]; michael@0: if (writeVariablePrecision(field->type()->getPrecision())) michael@0: out << " "; michael@0: out << getTypeName(*field->type()) << " " << hashName(field->name()); michael@0: if (field->type()->isArray()) michael@0: out << arrayBrackets(*field->type()); michael@0: out << ";\n"; michael@0: } michael@0: out << "}"; michael@0: michael@0: mDeclaredStructs.insert(structure->name()); michael@0: }