zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 1 | // |
Nicolas Capens | 16004fc | 2014-06-11 11:29:11 -0400 | [diff] [blame] | 2 | // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved. |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 7 | #include "compiler/translator/OutputGLSLBase.h" |
| 8 | #include "compiler/translator/compilerdebug.h" |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 9 | |
daniel@transgaming.com | 773ff74 | 2013-01-11 04:12:51 +0000 | [diff] [blame] | 10 | #include <cfloat> |
daniel@transgaming.com | 6c1203f | 2013-01-11 04:12:43 +0000 | [diff] [blame] | 11 | |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 12 | namespace |
| 13 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 14 | TString arrayBrackets(const TType &type) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 15 | { |
| 16 | ASSERT(type.isArray()); |
| 17 | TInfoSinkBase out; |
| 18 | out << "[" << type.getArraySize() << "]"; |
| 19 | return TString(out.c_str()); |
| 20 | } |
| 21 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 22 | bool isSingleStatement(TIntermNode *node) |
| 23 | { |
| 24 | if (const TIntermAggregate *aggregate = node->getAsAggregate()) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 25 | { |
| 26 | return (aggregate->getOp() != EOpFunction) && |
| 27 | (aggregate->getOp() != EOpSequence); |
| 28 | } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 29 | else if (const TIntermSelection *selection = node->getAsSelectionNode()) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 30 | { |
| 31 | // Ternary operators are usually part of an assignment operator. |
| 32 | // This handles those rare cases in which they are all by themselves. |
| 33 | return selection->usesTernaryOperator(); |
| 34 | } |
| 35 | else if (node->getAsLoopNode()) |
| 36 | { |
| 37 | return false; |
| 38 | } |
| 39 | return true; |
| 40 | } |
| 41 | } // namespace |
| 42 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 43 | TOutputGLSLBase::TOutputGLSLBase(TInfoSinkBase &objSink, |
shannon.woods@transgaming.com | 1d432bb | 2013-01-25 21:57:28 +0000 | [diff] [blame] | 44 | ShArrayIndexClampingStrategy clampingStrategy, |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 45 | ShHashFunction64 hashFunction, |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 46 | NameMap &nameMap, |
| 47 | TSymbolTable &symbolTable, |
Jamie Madill | 02f20dd | 2013-09-12 12:07:42 -0400 | [diff] [blame] | 48 | int shaderVersion) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 49 | : TIntermTraverser(true, true, true), |
| 50 | mObjSink(objSink), |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 51 | mDeclaringVariables(false), |
shannon.woods@transgaming.com | 1d432bb | 2013-01-25 21:57:28 +0000 | [diff] [blame] | 52 | mClampingStrategy(clampingStrategy), |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 53 | mHashFunction(hashFunction), |
| 54 | mNameMap(nameMap), |
Jamie Madill | 02f20dd | 2013-09-12 12:07:42 -0400 | [diff] [blame] | 55 | mSymbolTable(symbolTable), |
| 56 | mShaderVersion(shaderVersion) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 57 | { |
| 58 | } |
| 59 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 60 | void TOutputGLSLBase::writeTriplet( |
| 61 | Visit visit, const char *preStr, const char *inStr, const char *postStr) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 62 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 63 | TInfoSinkBase &out = objSink(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 64 | if (visit == PreVisit && preStr) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 65 | out << preStr; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 66 | else if (visit == InVisit && inStr) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 67 | out << inStr; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 68 | else if (visit == PostVisit && postStr) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 69 | out << postStr; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 72 | void TOutputGLSLBase::writeBuiltInFunctionTriplet( |
| 73 | Visit visit, const char *preStr, bool useEmulatedFunction) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 74 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 75 | TString preString = useEmulatedFunction ? |
| 76 | BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr) : preStr; |
| 77 | writeTriplet(visit, preString.c_str(), ", ", ")"); |
| 78 | } |
| 79 | |
| 80 | void TOutputGLSLBase::writeVariableType(const TType &type) |
| 81 | { |
| 82 | TInfoSinkBase &out = objSink(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 83 | TQualifier qualifier = type.getQualifier(); |
| 84 | // TODO(alokp): Validate qualifier for variable declarations. |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 85 | if (qualifier != EvqTemporary && qualifier != EvqGlobal) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 86 | out << type.getQualifierString() << " "; |
| 87 | // Declare the struct if we have not done so already. |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 88 | if (type.getBasicType() == EbtStruct && !structDeclared(type.getStruct())) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 89 | { |
Jamie Madill | 01f85ac | 2014-06-06 11:55:04 -0400 | [diff] [blame] | 90 | TStructure *structure = type.getStruct(); |
| 91 | |
| 92 | declareStruct(structure); |
| 93 | |
| 94 | if (!structure->name().empty()) |
| 95 | { |
| 96 | mDeclaredStructs.insert(structure->uniqueId()); |
| 97 | } |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 98 | } |
| 99 | else |
| 100 | { |
| 101 | if (writeVariablePrecision(type.getPrecision())) |
| 102 | out << " "; |
| 103 | out << getTypeName(type); |
| 104 | } |
| 105 | } |
| 106 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 107 | void TOutputGLSLBase::writeFunctionParameters(const TIntermSequence &args) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 108 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 109 | TInfoSinkBase &out = objSink(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 110 | for (TIntermSequence::const_iterator iter = args.begin(); |
| 111 | iter != args.end(); ++iter) |
| 112 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 113 | const TIntermSymbol *arg = (*iter)->getAsSymbolNode(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 114 | ASSERT(arg != NULL); |
| 115 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 116 | const TType &type = arg->getType(); |
zmo@google.com | 189be2f | 2011-06-16 18:28:53 +0000 | [diff] [blame] | 117 | writeVariableType(type); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 118 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 119 | const TString &name = arg->getSymbol(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 120 | if (!name.empty()) |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 121 | out << " " << hashName(name); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 122 | if (type.isArray()) |
| 123 | out << arrayBrackets(type); |
| 124 | |
| 125 | // Put a comma if this is not the last argument. |
| 126 | if (iter != args.end() - 1) |
| 127 | out << ", "; |
| 128 | } |
| 129 | } |
| 130 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 131 | const ConstantUnion *TOutputGLSLBase::writeConstantUnion( |
| 132 | const TType &type, const ConstantUnion *pConstUnion) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 133 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 134 | TInfoSinkBase &out = objSink(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 135 | |
| 136 | if (type.getBasicType() == EbtStruct) |
| 137 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 138 | const TStructure *structure = type.getStruct(); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 139 | out << hashName(structure->name()) << "("; |
| 140 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 141 | const TFieldList &fields = structure->fields(); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 142 | for (size_t i = 0; i < fields.size(); ++i) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 143 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 144 | const TType *fieldType = fields[i]->type(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 145 | ASSERT(fieldType != NULL); |
| 146 | pConstUnion = writeConstantUnion(*fieldType, pConstUnion); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 147 | if (i != fields.size() - 1) |
| 148 | out << ", "; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 149 | } |
| 150 | out << ")"; |
| 151 | } |
| 152 | else |
| 153 | { |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame] | 154 | size_t size = type.getObjectSize(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 155 | bool writeType = size > 1; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 156 | if (writeType) |
| 157 | out << getTypeName(type) << "("; |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame] | 158 | for (size_t i = 0; i < size; ++i, ++pConstUnion) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 159 | { |
| 160 | switch (pConstUnion->getType()) |
| 161 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 162 | case EbtFloat: |
| 163 | out << std::min(FLT_MAX, std::max(-FLT_MAX, pConstUnion->getFConst())); |
| 164 | break; |
| 165 | case EbtInt: |
| 166 | out << pConstUnion->getIConst(); |
| 167 | break; |
| 168 | case EbtBool: |
| 169 | out << pConstUnion->getBConst(); |
| 170 | break; |
| 171 | default: UNREACHABLE(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 172 | } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 173 | if (i != size - 1) |
| 174 | out << ", "; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 175 | } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 176 | if (writeType) |
| 177 | out << ")"; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 178 | } |
| 179 | return pConstUnion; |
| 180 | } |
| 181 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 182 | void TOutputGLSLBase::visitSymbol(TIntermSymbol *node) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 183 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 184 | TInfoSinkBase &out = objSink(); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 185 | if (mLoopUnrollStack.needsToReplaceSymbolWithValue(node)) |
| 186 | out << mLoopUnrollStack.getLoopIndexValue(node); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 187 | else |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 188 | out << hashVariableName(node->getSymbol()); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 189 | |
| 190 | if (mDeclaringVariables && node->getType().isArray()) |
| 191 | out << arrayBrackets(node->getType()); |
| 192 | } |
| 193 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 194 | void TOutputGLSLBase::visitConstantUnion(TIntermConstantUnion *node) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 195 | { |
| 196 | writeConstantUnion(node->getType(), node->getUnionArrayPointer()); |
| 197 | } |
| 198 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 199 | bool TOutputGLSLBase::visitBinary(Visit visit, TIntermBinary *node) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 200 | { |
| 201 | bool visitChildren = true; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 202 | TInfoSinkBase &out = objSink(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 203 | switch (node->getOp()) |
| 204 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 205 | case EOpInitialize: |
| 206 | if (visit == InVisit) |
| 207 | { |
| 208 | out << " = "; |
| 209 | // RHS of initialize is not being declared. |
| 210 | mDeclaringVariables = false; |
| 211 | } |
| 212 | break; |
| 213 | case EOpAssign: |
| 214 | writeTriplet(visit, "(", " = ", ")"); |
| 215 | break; |
| 216 | case EOpAddAssign: |
| 217 | writeTriplet(visit, "(", " += ", ")"); |
| 218 | break; |
| 219 | case EOpSubAssign: |
| 220 | writeTriplet(visit, "(", " -= ", ")"); |
| 221 | break; |
| 222 | case EOpDivAssign: |
| 223 | writeTriplet(visit, "(", " /= ", ")"); |
| 224 | break; |
| 225 | // Notice the fall-through. |
| 226 | case EOpMulAssign: |
| 227 | case EOpVectorTimesMatrixAssign: |
| 228 | case EOpVectorTimesScalarAssign: |
| 229 | case EOpMatrixTimesScalarAssign: |
| 230 | case EOpMatrixTimesMatrixAssign: |
| 231 | writeTriplet(visit, "(", " *= ", ")"); |
| 232 | break; |
| 233 | |
| 234 | case EOpIndexDirect: |
| 235 | writeTriplet(visit, NULL, "[", "]"); |
| 236 | break; |
| 237 | case EOpIndexIndirect: |
| 238 | if (node->getAddIndexClamp()) |
| 239 | { |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 240 | if (visit == InVisit) |
| 241 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 242 | if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC) |
| 243 | out << "[int(clamp(float("; |
| 244 | else |
| 245 | out << "[webgl_int_clamp("; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 246 | } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 247 | else if (visit == PostVisit) |
| 248 | { |
| 249 | int maxSize; |
| 250 | TIntermTyped *left = node->getLeft(); |
| 251 | TType leftType = left->getType(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 252 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 253 | if (left->isArray()) |
| 254 | { |
| 255 | // The shader will fail validation if the array length is not > 0. |
| 256 | maxSize = leftType.getArraySize() - 1; |
| 257 | } |
| 258 | else |
| 259 | { |
| 260 | maxSize = leftType.getNominalSize() - 1; |
| 261 | } |
| 262 | |
| 263 | if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC) |
| 264 | out << "), 0.0, float(" << maxSize << ")))]"; |
| 265 | else |
| 266 | out << ", 0, " << maxSize << ")]"; |
| 267 | } |
| 268 | } |
| 269 | else |
| 270 | { |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 271 | writeTriplet(visit, NULL, "[", "]"); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 272 | } |
| 273 | break; |
| 274 | case EOpIndexDirectStruct: |
| 275 | if (visit == InVisit) |
| 276 | { |
| 277 | // Here we are writing out "foo.bar", where "foo" is struct |
| 278 | // and "bar" is field. In AST, it is represented as a binary |
| 279 | // node, where left child represents "foo" and right child "bar". |
| 280 | // The node itself represents ".". The struct field "bar" is |
| 281 | // actually stored as an index into TStructure::fields. |
| 282 | out << "."; |
| 283 | const TStructure *structure = node->getLeft()->getType().getStruct(); |
| 284 | const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion(); |
| 285 | const TField *field = structure->fields()[index->getIConst(0)]; |
| 286 | |
| 287 | TString fieldName = field->name(); |
| 288 | if (!mSymbolTable.findBuiltIn(structure->name(), mShaderVersion)) |
| 289 | fieldName = hashName(fieldName); |
| 290 | |
| 291 | out << fieldName; |
| 292 | visitChildren = false; |
| 293 | } |
| 294 | break; |
| 295 | case EOpVectorSwizzle: |
| 296 | if (visit == InVisit) |
| 297 | { |
| 298 | out << "."; |
| 299 | TIntermAggregate *rightChild = node->getRight()->getAsAggregate(); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 300 | TIntermSequence *sequence = rightChild->getSequence(); |
| 301 | for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); ++sit) |
daniel@transgaming.com | 4167cc9 | 2013-01-11 04:11:53 +0000 | [diff] [blame] | 302 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 303 | TIntermConstantUnion *element = (*sit)->getAsConstantUnion(); |
| 304 | ASSERT(element->getBasicType() == EbtInt); |
| 305 | ASSERT(element->getNominalSize() == 1); |
| 306 | const ConstantUnion& data = element->getUnionArrayPointer()[0]; |
| 307 | ASSERT(data.getType() == EbtInt); |
| 308 | switch (data.getIConst()) |
daniel@transgaming.com | 4167cc9 | 2013-01-11 04:11:53 +0000 | [diff] [blame] | 309 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 310 | case 0: |
| 311 | out << "x"; |
| 312 | break; |
| 313 | case 1: |
| 314 | out << "y"; |
| 315 | break; |
| 316 | case 2: |
| 317 | out << "z"; |
| 318 | break; |
| 319 | case 3: |
| 320 | out << "w"; |
| 321 | break; |
| 322 | default: |
| 323 | UNREACHABLE(); |
daniel@transgaming.com | 4167cc9 | 2013-01-11 04:11:53 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 326 | visitChildren = false; |
| 327 | } |
| 328 | break; |
daniel@transgaming.com | 97b16d1 | 2013-02-01 03:20:42 +0000 | [diff] [blame] | 329 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 330 | case EOpAdd: |
| 331 | writeTriplet(visit, "(", " + ", ")"); |
| 332 | break; |
| 333 | case EOpSub: |
| 334 | writeTriplet(visit, "(", " - ", ")"); |
| 335 | break; |
| 336 | case EOpMul: |
| 337 | writeTriplet(visit, "(", " * ", ")"); |
| 338 | break; |
| 339 | case EOpDiv: |
| 340 | writeTriplet(visit, "(", " / ", ")"); |
| 341 | break; |
| 342 | case EOpMod: |
| 343 | UNIMPLEMENTED(); |
| 344 | break; |
| 345 | case EOpEqual: |
| 346 | writeTriplet(visit, "(", " == ", ")"); |
| 347 | break; |
| 348 | case EOpNotEqual: |
| 349 | writeTriplet(visit, "(", " != ", ")"); |
| 350 | break; |
| 351 | case EOpLessThan: |
| 352 | writeTriplet(visit, "(", " < ", ")"); |
| 353 | break; |
| 354 | case EOpGreaterThan: |
| 355 | writeTriplet(visit, "(", " > ", ")"); |
| 356 | break; |
| 357 | case EOpLessThanEqual: |
| 358 | writeTriplet(visit, "(", " <= ", ")"); |
| 359 | break; |
| 360 | case EOpGreaterThanEqual: |
| 361 | writeTriplet(visit, "(", " >= ", ")"); |
| 362 | break; |
daniel@transgaming.com | 97b16d1 | 2013-02-01 03:20:42 +0000 | [diff] [blame] | 363 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 364 | // Notice the fall-through. |
| 365 | case EOpVectorTimesScalar: |
| 366 | case EOpVectorTimesMatrix: |
| 367 | case EOpMatrixTimesVector: |
| 368 | case EOpMatrixTimesScalar: |
| 369 | case EOpMatrixTimesMatrix: |
| 370 | writeTriplet(visit, "(", " * ", ")"); |
| 371 | break; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 372 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 373 | case EOpLogicalOr: |
| 374 | writeTriplet(visit, "(", " || ", ")"); |
| 375 | break; |
| 376 | case EOpLogicalXor: |
| 377 | writeTriplet(visit, "(", " ^^ ", ")"); |
| 378 | break; |
| 379 | case EOpLogicalAnd: |
| 380 | writeTriplet(visit, "(", " && ", ")"); |
| 381 | break; |
| 382 | default: |
| 383 | UNREACHABLE(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | return visitChildren; |
| 387 | } |
| 388 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 389 | bool TOutputGLSLBase::visitUnary(Visit visit, TIntermUnary *node) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 390 | { |
zmo@google.com | 32e9731 | 2011-08-24 01:03:11 +0000 | [diff] [blame] | 391 | TString preString; |
| 392 | TString postString = ")"; |
| 393 | |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 394 | switch (node->getOp()) |
| 395 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 396 | case EOpNegative: preString = "(-"; break; |
| 397 | case EOpVectorLogicalNot: preString = "not("; break; |
| 398 | case EOpLogicalNot: preString = "(!"; break; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 399 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 400 | case EOpPostIncrement: preString = "("; postString = "++)"; break; |
| 401 | case EOpPostDecrement: preString = "("; postString = "--)"; break; |
| 402 | case EOpPreIncrement: preString = "(++"; break; |
| 403 | case EOpPreDecrement: preString = "(--"; break; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 404 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 405 | case EOpRadians: |
| 406 | preString = "radians("; |
| 407 | break; |
| 408 | case EOpDegrees: |
| 409 | preString = "degrees("; |
| 410 | break; |
| 411 | case EOpSin: |
| 412 | preString = "sin("; |
| 413 | break; |
| 414 | case EOpCos: |
| 415 | preString = "cos("; |
| 416 | break; |
| 417 | case EOpTan: |
| 418 | preString = "tan("; |
| 419 | break; |
| 420 | case EOpAsin: |
| 421 | preString = "asin("; |
| 422 | break; |
| 423 | case EOpAcos: |
| 424 | preString = "acos("; |
| 425 | break; |
| 426 | case EOpAtan: |
| 427 | preString = "atan("; |
| 428 | break; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 429 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 430 | case EOpExp: |
| 431 | preString = "exp("; |
| 432 | break; |
| 433 | case EOpLog: |
| 434 | preString = "log("; |
| 435 | break; |
| 436 | case EOpExp2: |
| 437 | preString = "exp2("; |
| 438 | break; |
| 439 | case EOpLog2: |
| 440 | preString = "log2("; |
| 441 | break; |
| 442 | case EOpSqrt: |
| 443 | preString = "sqrt("; |
| 444 | break; |
| 445 | case EOpInverseSqrt: |
| 446 | preString = "inversesqrt("; |
| 447 | break; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 448 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 449 | case EOpAbs: |
| 450 | preString = "abs("; |
| 451 | break; |
| 452 | case EOpSign: |
| 453 | preString = "sign("; |
| 454 | break; |
| 455 | case EOpFloor: |
| 456 | preString = "floor("; |
| 457 | break; |
| 458 | case EOpCeil: |
| 459 | preString = "ceil("; |
| 460 | break; |
| 461 | case EOpFract: |
| 462 | preString = "fract("; |
| 463 | break; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 464 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 465 | case EOpLength: |
| 466 | preString = "length("; |
| 467 | break; |
| 468 | case EOpNormalize: |
| 469 | preString = "normalize("; |
| 470 | break; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 471 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 472 | case EOpDFdx: |
| 473 | preString = "dFdx("; |
| 474 | break; |
| 475 | case EOpDFdy: |
| 476 | preString = "dFdy("; |
| 477 | break; |
| 478 | case EOpFwidth: |
| 479 | preString = "fwidth("; |
| 480 | break; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 481 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 482 | case EOpAny: |
| 483 | preString = "any("; |
| 484 | break; |
| 485 | case EOpAll: |
| 486 | preString = "all("; |
| 487 | break; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 488 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 489 | default: |
| 490 | UNREACHABLE(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 491 | } |
| 492 | |
zmo@google.com | 32e9731 | 2011-08-24 01:03:11 +0000 | [diff] [blame] | 493 | if (visit == PreVisit && node->getUseEmulatedFunction()) |
| 494 | preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preString); |
| 495 | writeTriplet(visit, preString.c_str(), NULL, postString.c_str()); |
| 496 | |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 497 | return true; |
| 498 | } |
| 499 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 500 | bool TOutputGLSLBase::visitSelection(Visit visit, TIntermSelection *node) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 501 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 502 | TInfoSinkBase &out = objSink(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 503 | |
| 504 | if (node->usesTernaryOperator()) |
| 505 | { |
| 506 | // Notice two brackets at the beginning and end. The outer ones |
| 507 | // encapsulate the whole ternary expression. This preserves the |
| 508 | // order of precedence when ternary expressions are used in a |
| 509 | // compound expression, i.e., c = 2 * (a < b ? 1 : 2). |
| 510 | out << "(("; |
| 511 | node->getCondition()->traverse(this); |
| 512 | out << ") ? ("; |
| 513 | node->getTrueBlock()->traverse(this); |
| 514 | out << ") : ("; |
| 515 | node->getFalseBlock()->traverse(this); |
| 516 | out << "))"; |
| 517 | } |
| 518 | else |
| 519 | { |
| 520 | out << "if ("; |
| 521 | node->getCondition()->traverse(this); |
| 522 | out << ")\n"; |
| 523 | |
Zhenyao Mo | 7cab38b | 2013-10-15 12:59:30 -0700 | [diff] [blame] | 524 | incrementDepth(node); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 525 | visitCodeBlock(node->getTrueBlock()); |
| 526 | |
| 527 | if (node->getFalseBlock()) |
| 528 | { |
| 529 | out << "else\n"; |
| 530 | visitCodeBlock(node->getFalseBlock()); |
| 531 | } |
| 532 | decrementDepth(); |
| 533 | } |
| 534 | return false; |
| 535 | } |
| 536 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 537 | bool TOutputGLSLBase::visitAggregate(Visit visit, TIntermAggregate *node) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 538 | { |
| 539 | bool visitChildren = true; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 540 | TInfoSinkBase &out = objSink(); |
zmo@google.com | f420c42 | 2011-09-12 18:27:59 +0000 | [diff] [blame] | 541 | TString preString; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 542 | bool useEmulatedFunction = (visit == PreVisit && node->getUseEmulatedFunction()); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 543 | switch (node->getOp()) |
| 544 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 545 | case EOpSequence: |
| 546 | // Scope the sequences except when at the global scope. |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 547 | if (mDepth > 0) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 548 | { |
| 549 | out << "{\n"; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 550 | } |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 551 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 552 | incrementDepth(node); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 553 | for (TIntermSequence::const_iterator iter = node->getSequence()->begin(); |
| 554 | iter != node->getSequence()->end(); ++iter) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 555 | { |
| 556 | TIntermNode *node = *iter; |
| 557 | ASSERT(node != NULL); |
| 558 | node->traverse(this); |
| 559 | |
| 560 | if (isSingleStatement(node)) |
| 561 | out << ";\n"; |
| 562 | } |
| 563 | decrementDepth(); |
| 564 | |
| 565 | // Scope the sequences except when at the global scope. |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 566 | if (mDepth > 0) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 567 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 568 | out << "}\n"; |
| 569 | } |
| 570 | visitChildren = false; |
| 571 | break; |
| 572 | case EOpPrototype: |
| 573 | // Function declaration. |
| 574 | ASSERT(visit == PreVisit); |
| 575 | writeVariableType(node->getType()); |
| 576 | out << " " << hashName(node->getName()); |
| 577 | |
| 578 | out << "("; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 579 | writeFunctionParameters(*(node->getSequence())); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 580 | out << ")"; |
| 581 | |
| 582 | visitChildren = false; |
| 583 | break; |
| 584 | case EOpFunction: { |
| 585 | // Function definition. |
| 586 | ASSERT(visit == PreVisit); |
| 587 | writeVariableType(node->getType()); |
| 588 | out << " " << hashFunctionName(node->getName()); |
| 589 | |
| 590 | incrementDepth(node); |
| 591 | // Function definition node contains one or two children nodes |
| 592 | // representing function parameters and function body. The latter |
| 593 | // is not present in case of empty function bodies. |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 594 | const TIntermSequence &sequence = *(node->getSequence()); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 595 | ASSERT((sequence.size() == 1) || (sequence.size() == 2)); |
| 596 | TIntermSequence::const_iterator seqIter = sequence.begin(); |
| 597 | |
| 598 | // Traverse function parameters. |
| 599 | TIntermAggregate *params = (*seqIter)->getAsAggregate(); |
| 600 | ASSERT(params != NULL); |
| 601 | ASSERT(params->getOp() == EOpParameters); |
| 602 | params->traverse(this); |
| 603 | |
| 604 | // Traverse function body. |
| 605 | TIntermAggregate *body = ++seqIter != sequence.end() ? |
| 606 | (*seqIter)->getAsAggregate() : NULL; |
| 607 | visitCodeBlock(body); |
| 608 | decrementDepth(); |
| 609 | |
| 610 | // Fully processed; no need to visit children. |
| 611 | visitChildren = false; |
| 612 | break; |
| 613 | } |
| 614 | case EOpFunctionCall: |
| 615 | // Function call. |
| 616 | if (visit == PreVisit) |
| 617 | out << hashFunctionName(node->getName()) << "("; |
| 618 | else if (visit == InVisit) |
| 619 | out << ", "; |
| 620 | else |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 621 | out << ")"; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 622 | break; |
| 623 | case EOpParameters: |
| 624 | // Function parameters. |
| 625 | ASSERT(visit == PreVisit); |
| 626 | out << "("; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 627 | writeFunctionParameters(*(node->getSequence())); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 628 | out << ")"; |
| 629 | visitChildren = false; |
| 630 | break; |
| 631 | case EOpDeclaration: |
| 632 | // Variable declaration. |
| 633 | if (visit == PreVisit) |
| 634 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 635 | const TIntermSequence &sequence = *(node->getSequence()); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 636 | const TIntermTyped *variable = sequence.front()->getAsTyped(); |
| 637 | writeVariableType(variable->getType()); |
| 638 | out << " "; |
| 639 | mDeclaringVariables = true; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 640 | } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 641 | else if (visit == InVisit) |
| 642 | { |
| 643 | out << ", "; |
| 644 | mDeclaringVariables = true; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 645 | } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 646 | else |
| 647 | { |
| 648 | mDeclaringVariables = false; |
| 649 | } |
| 650 | break; |
| 651 | case EOpConstructFloat: |
| 652 | writeTriplet(visit, "float(", NULL, ")"); |
| 653 | break; |
| 654 | case EOpConstructVec2: |
| 655 | writeBuiltInFunctionTriplet(visit, "vec2(", false); |
| 656 | break; |
| 657 | case EOpConstructVec3: |
| 658 | writeBuiltInFunctionTriplet(visit, "vec3(", false); |
| 659 | break; |
| 660 | case EOpConstructVec4: |
| 661 | writeBuiltInFunctionTriplet(visit, "vec4(", false); |
| 662 | break; |
| 663 | case EOpConstructBool: |
| 664 | writeTriplet(visit, "bool(", NULL, ")"); |
| 665 | break; |
| 666 | case EOpConstructBVec2: |
| 667 | writeBuiltInFunctionTriplet(visit, "bvec2(", false); |
| 668 | break; |
| 669 | case EOpConstructBVec3: |
| 670 | writeBuiltInFunctionTriplet(visit, "bvec3(", false); |
| 671 | break; |
| 672 | case EOpConstructBVec4: |
| 673 | writeBuiltInFunctionTriplet(visit, "bvec4(", false); |
| 674 | break; |
| 675 | case EOpConstructInt: |
| 676 | writeTriplet(visit, "int(", NULL, ")"); |
| 677 | break; |
| 678 | case EOpConstructIVec2: |
| 679 | writeBuiltInFunctionTriplet(visit, "ivec2(", false); |
| 680 | break; |
| 681 | case EOpConstructIVec3: |
| 682 | writeBuiltInFunctionTriplet(visit, "ivec3(", false); |
| 683 | break; |
| 684 | case EOpConstructIVec4: |
| 685 | writeBuiltInFunctionTriplet(visit, "ivec4(", false); |
| 686 | break; |
| 687 | case EOpConstructMat2: |
| 688 | writeBuiltInFunctionTriplet(visit, "mat2(", false); |
| 689 | break; |
| 690 | case EOpConstructMat3: |
| 691 | writeBuiltInFunctionTriplet(visit, "mat3(", false); |
| 692 | break; |
| 693 | case EOpConstructMat4: |
| 694 | writeBuiltInFunctionTriplet(visit, "mat4(", false); |
| 695 | break; |
| 696 | case EOpConstructStruct: |
| 697 | if (visit == PreVisit) |
| 698 | { |
| 699 | const TType &type = node->getType(); |
| 700 | ASSERT(type.getBasicType() == EbtStruct); |
| 701 | out << hashName(type.getStruct()->name()) << "("; |
| 702 | } |
| 703 | else if (visit == InVisit) |
| 704 | { |
| 705 | out << ", "; |
| 706 | } |
| 707 | else |
| 708 | { |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 709 | out << ")"; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 710 | } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 711 | break; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 712 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 713 | case EOpLessThan: |
| 714 | writeBuiltInFunctionTriplet(visit, "lessThan(", useEmulatedFunction); |
| 715 | break; |
| 716 | case EOpGreaterThan: |
| 717 | writeBuiltInFunctionTriplet(visit, "greaterThan(", useEmulatedFunction); |
| 718 | break; |
| 719 | case EOpLessThanEqual: |
| 720 | writeBuiltInFunctionTriplet(visit, "lessThanEqual(", useEmulatedFunction); |
| 721 | break; |
| 722 | case EOpGreaterThanEqual: |
| 723 | writeBuiltInFunctionTriplet(visit, "greaterThanEqual(", useEmulatedFunction); |
| 724 | break; |
| 725 | case EOpVectorEqual: |
| 726 | writeBuiltInFunctionTriplet(visit, "equal(", useEmulatedFunction); |
| 727 | break; |
| 728 | case EOpVectorNotEqual: |
| 729 | writeBuiltInFunctionTriplet(visit, "notEqual(", useEmulatedFunction); |
| 730 | break; |
| 731 | case EOpComma: |
| 732 | writeTriplet(visit, NULL, ", ", NULL); |
| 733 | break; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 734 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 735 | case EOpMod: |
| 736 | writeBuiltInFunctionTriplet(visit, "mod(", useEmulatedFunction); |
| 737 | break; |
| 738 | case EOpPow: |
| 739 | writeBuiltInFunctionTriplet(visit, "pow(", useEmulatedFunction); |
| 740 | break; |
| 741 | case EOpAtan: |
| 742 | writeBuiltInFunctionTriplet(visit, "atan(", useEmulatedFunction); |
| 743 | break; |
| 744 | case EOpMin: |
| 745 | writeBuiltInFunctionTriplet(visit, "min(", useEmulatedFunction); |
| 746 | break; |
| 747 | case EOpMax: |
| 748 | writeBuiltInFunctionTriplet(visit, "max(", useEmulatedFunction); |
| 749 | break; |
| 750 | case EOpClamp: |
| 751 | writeBuiltInFunctionTriplet(visit, "clamp(", useEmulatedFunction); |
| 752 | break; |
| 753 | case EOpMix: |
| 754 | writeBuiltInFunctionTriplet(visit, "mix(", useEmulatedFunction); |
| 755 | break; |
| 756 | case EOpStep: |
| 757 | writeBuiltInFunctionTriplet(visit, "step(", useEmulatedFunction); |
| 758 | break; |
| 759 | case EOpSmoothStep: |
| 760 | writeBuiltInFunctionTriplet(visit, "smoothstep(", useEmulatedFunction); |
| 761 | break; |
| 762 | case EOpDistance: |
| 763 | writeBuiltInFunctionTriplet(visit, "distance(", useEmulatedFunction); |
| 764 | break; |
| 765 | case EOpDot: |
| 766 | writeBuiltInFunctionTriplet(visit, "dot(", useEmulatedFunction); |
| 767 | break; |
| 768 | case EOpCross: |
| 769 | writeBuiltInFunctionTriplet(visit, "cross(", useEmulatedFunction); |
| 770 | break; |
| 771 | case EOpFaceForward: |
| 772 | writeBuiltInFunctionTriplet(visit, "faceforward(", useEmulatedFunction); |
| 773 | break; |
| 774 | case EOpReflect: |
| 775 | writeBuiltInFunctionTriplet(visit, "reflect(", useEmulatedFunction); |
| 776 | break; |
| 777 | case EOpRefract: |
| 778 | writeBuiltInFunctionTriplet(visit, "refract(", useEmulatedFunction); |
| 779 | break; |
| 780 | case EOpMul: |
| 781 | writeBuiltInFunctionTriplet(visit, "matrixCompMult(", useEmulatedFunction); |
| 782 | break; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 783 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 784 | default: |
| 785 | UNREACHABLE(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 786 | } |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 787 | return visitChildren; |
| 788 | } |
| 789 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 790 | bool TOutputGLSLBase::visitLoop(Visit visit, TIntermLoop *node) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 791 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 792 | TInfoSinkBase &out = objSink(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 793 | |
Zhenyao Mo | 7cab38b | 2013-10-15 12:59:30 -0700 | [diff] [blame] | 794 | incrementDepth(node); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 795 | // Loop header. |
| 796 | TLoopType loopType = node->getType(); |
| 797 | if (loopType == ELoopFor) // for loop |
| 798 | { |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 799 | if (!node->getUnrollFlag()) |
| 800 | { |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 801 | out << "for ("; |
| 802 | if (node->getInit()) |
| 803 | node->getInit()->traverse(this); |
| 804 | out << "; "; |
| 805 | |
| 806 | if (node->getCondition()) |
| 807 | node->getCondition()->traverse(this); |
| 808 | out << "; "; |
| 809 | |
| 810 | if (node->getExpression()) |
| 811 | node->getExpression()->traverse(this); |
| 812 | out << ")\n"; |
| 813 | } |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 814 | else |
| 815 | { |
| 816 | // Need to put a one-iteration loop here to handle break. |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 817 | TIntermSequence *declSeq = |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 818 | node->getInit()->getAsAggregate()->getSequence(); |
| 819 | TIntermSymbol *indexSymbol = |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 820 | (*declSeq)[0]->getAsBinaryNode()->getLeft()->getAsSymbolNode(); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 821 | TString name = hashVariableName(indexSymbol->getSymbol()); |
| 822 | out << "for (int " << name << " = 0; " |
| 823 | << name << " < 1; " |
| 824 | << "++" << name << ")\n"; |
| 825 | } |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 826 | } |
| 827 | else if (loopType == ELoopWhile) // while loop |
| 828 | { |
| 829 | out << "while ("; |
| 830 | ASSERT(node->getCondition() != NULL); |
| 831 | node->getCondition()->traverse(this); |
| 832 | out << ")\n"; |
| 833 | } |
| 834 | else // do-while loop |
| 835 | { |
| 836 | ASSERT(loopType == ELoopDoWhile); |
| 837 | out << "do\n"; |
| 838 | } |
| 839 | |
| 840 | // Loop body. |
| 841 | if (node->getUnrollFlag()) |
| 842 | { |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 843 | out << "{\n"; |
| 844 | mLoopUnrollStack.push(node); |
| 845 | while (mLoopUnrollStack.satisfiesLoopCondition()) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 846 | { |
| 847 | visitCodeBlock(node->getBody()); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 848 | mLoopUnrollStack.step(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 849 | } |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 850 | mLoopUnrollStack.pop(); |
| 851 | out << "}\n"; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 852 | } |
| 853 | else |
| 854 | { |
| 855 | visitCodeBlock(node->getBody()); |
| 856 | } |
| 857 | |
| 858 | // Loop footer. |
| 859 | if (loopType == ELoopDoWhile) // do-while loop |
| 860 | { |
| 861 | out << "while ("; |
| 862 | ASSERT(node->getCondition() != NULL); |
| 863 | node->getCondition()->traverse(this); |
| 864 | out << ");\n"; |
| 865 | } |
| 866 | decrementDepth(); |
| 867 | |
| 868 | // No need to visit children. They have been already processed in |
| 869 | // this function. |
| 870 | return false; |
| 871 | } |
| 872 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 873 | bool TOutputGLSLBase::visitBranch(Visit visit, TIntermBranch *node) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 874 | { |
| 875 | switch (node->getFlowOp()) |
| 876 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 877 | case EOpKill: |
| 878 | writeTriplet(visit, "discard", NULL, NULL); |
| 879 | break; |
| 880 | case EOpBreak: |
| 881 | writeTriplet(visit, "break", NULL, NULL); |
| 882 | break; |
| 883 | case EOpContinue: |
| 884 | writeTriplet(visit, "continue", NULL, NULL); |
| 885 | break; |
| 886 | case EOpReturn: |
| 887 | writeTriplet(visit, "return ", NULL, NULL); |
| 888 | break; |
| 889 | default: |
| 890 | UNREACHABLE(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | return true; |
| 894 | } |
| 895 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 896 | void TOutputGLSLBase::visitCodeBlock(TIntermNode *node) |
| 897 | { |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 898 | TInfoSinkBase &out = objSink(); |
| 899 | if (node != NULL) |
| 900 | { |
| 901 | node->traverse(this); |
| 902 | // Single statements not part of a sequence need to be terminated |
| 903 | // with semi-colon. |
| 904 | if (isSingleStatement(node)) |
| 905 | out << ";\n"; |
| 906 | } |
| 907 | else |
| 908 | { |
| 909 | out << "{\n}\n"; // Empty code block. |
| 910 | } |
| 911 | } |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 912 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 913 | TString TOutputGLSLBase::getTypeName(const TType &type) |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 914 | { |
| 915 | TInfoSinkBase out; |
| 916 | if (type.isMatrix()) |
| 917 | { |
| 918 | out << "mat"; |
| 919 | out << type.getNominalSize(); |
| 920 | } |
| 921 | else if (type.isVector()) |
| 922 | { |
| 923 | switch (type.getBasicType()) |
| 924 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 925 | case EbtFloat: |
| 926 | out << "vec"; |
| 927 | break; |
| 928 | case EbtInt: |
| 929 | out << "ivec"; |
| 930 | break; |
| 931 | case EbtBool: |
| 932 | out << "bvec"; |
| 933 | break; |
| 934 | default: |
| 935 | UNREACHABLE(); |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 936 | } |
| 937 | out << type.getNominalSize(); |
| 938 | } |
| 939 | else |
| 940 | { |
| 941 | if (type.getBasicType() == EbtStruct) |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 942 | out << hashName(type.getStruct()->name()); |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 943 | else |
| 944 | out << type.getBasicString(); |
| 945 | } |
| 946 | return TString(out.c_str()); |
| 947 | } |
| 948 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 949 | TString TOutputGLSLBase::hashName(const TString &name) |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 950 | { |
| 951 | if (mHashFunction == NULL || name.empty()) |
| 952 | return name; |
| 953 | NameMap::const_iterator it = mNameMap.find(name.c_str()); |
| 954 | if (it != mNameMap.end()) |
| 955 | return it->second.c_str(); |
| 956 | TString hashedName = TIntermTraverser::hash(name, mHashFunction); |
| 957 | mNameMap[name.c_str()] = hashedName.c_str(); |
| 958 | return hashedName; |
| 959 | } |
| 960 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 961 | TString TOutputGLSLBase::hashVariableName(const TString &name) |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 962 | { |
Jamie Madill | 02f20dd | 2013-09-12 12:07:42 -0400 | [diff] [blame] | 963 | if (mSymbolTable.findBuiltIn(name, mShaderVersion) != NULL) |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 964 | return name; |
| 965 | return hashName(name); |
| 966 | } |
| 967 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 968 | TString TOutputGLSLBase::hashFunctionName(const TString &mangled_name) |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 969 | { |
| 970 | TString name = TFunction::unmangleName(mangled_name); |
Jamie Madill | 02f20dd | 2013-09-12 12:07:42 -0400 | [diff] [blame] | 971 | if (mSymbolTable.findBuiltIn(mangled_name, mShaderVersion) != NULL || name == "main") |
Nicolas Capens | 4648508 | 2014-04-15 13:12:50 -0400 | [diff] [blame] | 972 | return translateTextureFunction(name); |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 973 | return hashName(name); |
| 974 | } |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 975 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 976 | bool TOutputGLSLBase::structDeclared(const TStructure *structure) const |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 977 | { |
Zhenyao Mo | 904a916 | 2014-05-09 14:07:45 -0700 | [diff] [blame] | 978 | ASSERT(structure); |
Jamie Madill | 01f85ac | 2014-06-06 11:55:04 -0400 | [diff] [blame] | 979 | if (structure->name().empty()) |
Zhenyao Mo | 904a916 | 2014-05-09 14:07:45 -0700 | [diff] [blame] | 980 | { |
Jamie Madill | 01f85ac | 2014-06-06 11:55:04 -0400 | [diff] [blame] | 981 | return false; |
Zhenyao Mo | 904a916 | 2014-05-09 14:07:45 -0700 | [diff] [blame] | 982 | } |
Jamie Madill | 01f85ac | 2014-06-06 11:55:04 -0400 | [diff] [blame] | 983 | |
| 984 | return (mDeclaredStructs.count(structure->uniqueId()) > 0); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 985 | } |
| 986 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 987 | void TOutputGLSLBase::declareStruct(const TStructure *structure) |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 988 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 989 | TInfoSinkBase &out = objSink(); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 990 | |
| 991 | out << "struct " << hashName(structure->name()) << "{\n"; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 992 | const TFieldList &fields = structure->fields(); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 993 | for (size_t i = 0; i < fields.size(); ++i) |
| 994 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 995 | const TField *field = fields[i]; |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 996 | if (writeVariablePrecision(field->type()->getPrecision())) |
| 997 | out << " "; |
| 998 | out << getTypeName(*field->type()) << " " << hashName(field->name()); |
| 999 | if (field->type()->isArray()) |
| 1000 | out << arrayBrackets(*field->type()); |
| 1001 | out << ";\n"; |
| 1002 | } |
| 1003 | out << "}"; |
Zhenyao Mo | 904a916 | 2014-05-09 14:07:45 -0700 | [diff] [blame] | 1004 | } |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1005 | |