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