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