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" |
Olli Etuaho | d57e0db | 2015-04-24 15:05:08 +0300 | [diff] [blame] | 8 | |
| 9 | #include "common/debug.h" |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 10 | |
daniel@transgaming.com | 773ff74 | 2013-01-11 04:12:51 +0000 | [diff] [blame] | 11 | #include <cfloat> |
daniel@transgaming.com | 6c1203f | 2013-01-11 04:12:43 +0000 | [diff] [blame] | 12 | |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 13 | namespace |
| 14 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 15 | TString arrayBrackets(const TType &type) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 16 | { |
| 17 | ASSERT(type.isArray()); |
| 18 | TInfoSinkBase out; |
| 19 | out << "[" << type.getArraySize() << "]"; |
| 20 | return TString(out.c_str()); |
| 21 | } |
| 22 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 23 | bool isSingleStatement(TIntermNode *node) |
| 24 | { |
| 25 | if (const TIntermAggregate *aggregate = node->getAsAggregate()) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 26 | { |
| 27 | return (aggregate->getOp() != EOpFunction) && |
| 28 | (aggregate->getOp() != EOpSequence); |
| 29 | } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 30 | else if (const TIntermSelection *selection = node->getAsSelectionNode()) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 31 | { |
| 32 | // Ternary operators are usually part of an assignment operator. |
| 33 | // This handles those rare cases in which they are all by themselves. |
| 34 | return selection->usesTernaryOperator(); |
| 35 | } |
| 36 | else if (node->getAsLoopNode()) |
| 37 | { |
| 38 | return false; |
| 39 | } |
Olli Etuaho | 01cd8af | 2015-02-20 10:39:20 +0200 | [diff] [blame] | 40 | else if (node->getAsSwitchNode()) |
| 41 | { |
| 42 | return false; |
| 43 | } |
| 44 | else if (node->getAsCaseNode()) |
| 45 | { |
| 46 | return false; |
| 47 | } |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 48 | return true; |
| 49 | } |
Zhenyao Mo | 05b6b7f | 2015-03-02 17:08:09 -0800 | [diff] [blame] | 50 | |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 51 | } // namespace |
| 52 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 53 | TOutputGLSLBase::TOutputGLSLBase(TInfoSinkBase &objSink, |
shannon.woods@transgaming.com | 1d432bb | 2013-01-25 21:57:28 +0000 | [diff] [blame] | 54 | ShArrayIndexClampingStrategy clampingStrategy, |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 55 | ShHashFunction64 hashFunction, |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 56 | NameMap &nameMap, |
| 57 | TSymbolTable &symbolTable, |
Zhenyao Mo | 05b6b7f | 2015-03-02 17:08:09 -0800 | [diff] [blame] | 58 | int shaderVersion, |
| 59 | ShShaderOutput output) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 60 | : TIntermTraverser(true, true, true), |
| 61 | mObjSink(objSink), |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 62 | mDeclaringVariables(false), |
shannon.woods@transgaming.com | 1d432bb | 2013-01-25 21:57:28 +0000 | [diff] [blame] | 63 | mClampingStrategy(clampingStrategy), |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 64 | mHashFunction(hashFunction), |
| 65 | mNameMap(nameMap), |
Jamie Madill | 02f20dd | 2013-09-12 12:07:42 -0400 | [diff] [blame] | 66 | mSymbolTable(symbolTable), |
Zhenyao Mo | 05b6b7f | 2015-03-02 17:08:09 -0800 | [diff] [blame] | 67 | mShaderVersion(shaderVersion), |
| 68 | mOutput(output) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 69 | { |
| 70 | } |
| 71 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 72 | void TOutputGLSLBase::writeTriplet( |
| 73 | Visit visit, const char *preStr, const char *inStr, const char *postStr) |
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 | TInfoSinkBase &out = objSink(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 76 | if (visit == PreVisit && preStr) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 77 | out << preStr; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 78 | else if (visit == InVisit && inStr) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 79 | out << inStr; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 80 | else if (visit == PostVisit && postStr) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 81 | out << postStr; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 84 | void TOutputGLSLBase::writeBuiltInFunctionTriplet( |
| 85 | Visit visit, const char *preStr, bool useEmulatedFunction) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 86 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 87 | TString preString = useEmulatedFunction ? |
| 88 | BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr) : preStr; |
| 89 | writeTriplet(visit, preString.c_str(), ", ", ")"); |
| 90 | } |
| 91 | |
| 92 | void TOutputGLSLBase::writeVariableType(const TType &type) |
| 93 | { |
| 94 | TInfoSinkBase &out = objSink(); |
Olli Etuaho | 214c2d8 | 2015-04-27 14:49:13 +0300 | [diff] [blame] | 95 | if (type.isInvariant()) |
| 96 | { |
| 97 | out << "invariant "; |
| 98 | } |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 99 | TQualifier qualifier = type.getQualifier(); |
Jamie Madill | 3b5c2da | 2014-08-19 15:23:32 -0400 | [diff] [blame] | 100 | if (qualifier != EvqTemporary && qualifier != EvqGlobal) |
Jamie Madill | 1c28e1f | 2014-08-04 11:37:54 -0400 | [diff] [blame] | 101 | { |
Qingqing Deng | ad0d079 | 2015-04-08 14:25:06 -0700 | [diff] [blame] | 102 | if (IsGLSL130OrNewer(mOutput)) |
Zhenyao Mo | 05b6b7f | 2015-03-02 17:08:09 -0800 | [diff] [blame] | 103 | { |
| 104 | switch (qualifier) |
| 105 | { |
| 106 | case EvqAttribute: |
Olli Etuaho | 214c2d8 | 2015-04-27 14:49:13 +0300 | [diff] [blame] | 107 | out << "in "; |
Zhenyao Mo | 05b6b7f | 2015-03-02 17:08:09 -0800 | [diff] [blame] | 108 | break; |
| 109 | case EvqVaryingIn: |
Olli Etuaho | 214c2d8 | 2015-04-27 14:49:13 +0300 | [diff] [blame] | 110 | out << "in "; |
Zhenyao Mo | 05b6b7f | 2015-03-02 17:08:09 -0800 | [diff] [blame] | 111 | break; |
| 112 | case EvqVaryingOut: |
Olli Etuaho | 214c2d8 | 2015-04-27 14:49:13 +0300 | [diff] [blame] | 113 | out << "out "; |
Zhenyao Mo | 05b6b7f | 2015-03-02 17:08:09 -0800 | [diff] [blame] | 114 | break; |
| 115 | default: |
| 116 | out << type.getQualifierString() << " "; |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | out << type.getQualifierString() << " "; |
| 123 | } |
Jamie Madill | 1c28e1f | 2014-08-04 11:37:54 -0400 | [diff] [blame] | 124 | } |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 125 | // Declare the struct if we have not done so already. |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 126 | if (type.getBasicType() == EbtStruct && !structDeclared(type.getStruct())) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 127 | { |
Jamie Madill | 01f85ac | 2014-06-06 11:55:04 -0400 | [diff] [blame] | 128 | TStructure *structure = type.getStruct(); |
| 129 | |
| 130 | declareStruct(structure); |
| 131 | |
| 132 | if (!structure->name().empty()) |
| 133 | { |
| 134 | mDeclaredStructs.insert(structure->uniqueId()); |
| 135 | } |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 136 | } |
| 137 | else |
| 138 | { |
| 139 | if (writeVariablePrecision(type.getPrecision())) |
| 140 | out << " "; |
| 141 | out << getTypeName(type); |
| 142 | } |
| 143 | } |
| 144 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 145 | void TOutputGLSLBase::writeFunctionParameters(const TIntermSequence &args) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 146 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 147 | TInfoSinkBase &out = objSink(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 148 | for (TIntermSequence::const_iterator iter = args.begin(); |
| 149 | iter != args.end(); ++iter) |
| 150 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 151 | const TIntermSymbol *arg = (*iter)->getAsSymbolNode(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 152 | ASSERT(arg != NULL); |
| 153 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 154 | const TType &type = arg->getType(); |
zmo@google.com | 189be2f | 2011-06-16 18:28:53 +0000 | [diff] [blame] | 155 | writeVariableType(type); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 156 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 157 | const TString &name = arg->getSymbol(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 158 | if (!name.empty()) |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 159 | out << " " << hashName(name); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 160 | if (type.isArray()) |
| 161 | out << arrayBrackets(type); |
| 162 | |
| 163 | // Put a comma if this is not the last argument. |
| 164 | if (iter != args.end() - 1) |
| 165 | out << ", "; |
| 166 | } |
| 167 | } |
| 168 | |
Jamie Madill | 6ba6ead | 2015-05-04 14:21:21 -0400 | [diff] [blame] | 169 | const TConstantUnion *TOutputGLSLBase::writeConstantUnion( |
| 170 | const TType &type, const TConstantUnion *pConstUnion) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 171 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 172 | TInfoSinkBase &out = objSink(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 173 | |
| 174 | if (type.getBasicType() == EbtStruct) |
| 175 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 176 | const TStructure *structure = type.getStruct(); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 177 | out << hashName(structure->name()) << "("; |
| 178 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 179 | const TFieldList &fields = structure->fields(); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 180 | for (size_t i = 0; i < fields.size(); ++i) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 181 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 182 | const TType *fieldType = fields[i]->type(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 183 | ASSERT(fieldType != NULL); |
| 184 | pConstUnion = writeConstantUnion(*fieldType, pConstUnion); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 185 | if (i != fields.size() - 1) |
| 186 | out << ", "; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 187 | } |
| 188 | out << ")"; |
| 189 | } |
| 190 | else |
| 191 | { |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame] | 192 | size_t size = type.getObjectSize(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 193 | bool writeType = size > 1; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 194 | if (writeType) |
| 195 | out << getTypeName(type) << "("; |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame] | 196 | for (size_t i = 0; i < size; ++i, ++pConstUnion) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 197 | { |
| 198 | switch (pConstUnion->getType()) |
| 199 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 200 | case EbtFloat: |
| 201 | out << std::min(FLT_MAX, std::max(-FLT_MAX, pConstUnion->getFConst())); |
| 202 | break; |
| 203 | case EbtInt: |
| 204 | out << pConstUnion->getIConst(); |
| 205 | break; |
Olli Etuaho | 5321c80 | 2015-04-02 17:08:16 +0300 | [diff] [blame] | 206 | case EbtUInt: |
| 207 | out << pConstUnion->getUConst() << "u"; |
| 208 | break; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 209 | case EbtBool: |
| 210 | out << pConstUnion->getBConst(); |
| 211 | break; |
| 212 | default: UNREACHABLE(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 213 | } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 214 | if (i != size - 1) |
| 215 | out << ", "; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 216 | } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 217 | if (writeType) |
| 218 | out << ")"; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 219 | } |
| 220 | return pConstUnion; |
| 221 | } |
| 222 | |
Olli Etuaho | f40319e | 2015-03-10 14:33:00 +0200 | [diff] [blame] | 223 | void TOutputGLSLBase::writeConstructorTriplet(Visit visit, const TType &type, const char *constructorBaseType) |
| 224 | { |
| 225 | TInfoSinkBase &out = objSink(); |
| 226 | if (visit == PreVisit) |
| 227 | { |
| 228 | if (type.isArray()) |
| 229 | { |
| 230 | out << constructorBaseType; |
| 231 | out << arrayBrackets(type); |
| 232 | out << "("; |
| 233 | } |
| 234 | else |
| 235 | { |
| 236 | out << constructorBaseType << "("; |
| 237 | } |
| 238 | } |
| 239 | else |
| 240 | { |
| 241 | writeTriplet(visit, nullptr, ", ", ")"); |
| 242 | } |
| 243 | } |
| 244 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 245 | void TOutputGLSLBase::visitSymbol(TIntermSymbol *node) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 246 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 247 | TInfoSinkBase &out = objSink(); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 248 | if (mLoopUnrollStack.needsToReplaceSymbolWithValue(node)) |
| 249 | out << mLoopUnrollStack.getLoopIndexValue(node); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 250 | else |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 251 | out << hashVariableName(node->getSymbol()); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 252 | |
| 253 | if (mDeclaringVariables && node->getType().isArray()) |
| 254 | out << arrayBrackets(node->getType()); |
| 255 | } |
| 256 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 257 | void TOutputGLSLBase::visitConstantUnion(TIntermConstantUnion *node) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 258 | { |
| 259 | writeConstantUnion(node->getType(), node->getUnionArrayPointer()); |
| 260 | } |
| 261 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 262 | bool TOutputGLSLBase::visitBinary(Visit visit, TIntermBinary *node) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 263 | { |
| 264 | bool visitChildren = true; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 265 | TInfoSinkBase &out = objSink(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 266 | switch (node->getOp()) |
| 267 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 268 | case EOpInitialize: |
| 269 | if (visit == InVisit) |
| 270 | { |
| 271 | out << " = "; |
| 272 | // RHS of initialize is not being declared. |
| 273 | mDeclaringVariables = false; |
| 274 | } |
| 275 | break; |
| 276 | case EOpAssign: |
| 277 | writeTriplet(visit, "(", " = ", ")"); |
| 278 | break; |
| 279 | case EOpAddAssign: |
| 280 | writeTriplet(visit, "(", " += ", ")"); |
| 281 | break; |
| 282 | case EOpSubAssign: |
| 283 | writeTriplet(visit, "(", " -= ", ")"); |
| 284 | break; |
| 285 | case EOpDivAssign: |
| 286 | writeTriplet(visit, "(", " /= ", ")"); |
| 287 | break; |
Olli Etuaho | ff805cc | 2015-02-13 10:59:34 +0200 | [diff] [blame] | 288 | case EOpIModAssign: |
Gregoire Payen de La Garanderie | be954a2 | 2014-12-23 00:05:28 +0000 | [diff] [blame] | 289 | writeTriplet(visit, "(", " %= ", ")"); |
| 290 | break; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 291 | // Notice the fall-through. |
| 292 | case EOpMulAssign: |
| 293 | case EOpVectorTimesMatrixAssign: |
| 294 | case EOpVectorTimesScalarAssign: |
| 295 | case EOpMatrixTimesScalarAssign: |
| 296 | case EOpMatrixTimesMatrixAssign: |
| 297 | writeTriplet(visit, "(", " *= ", ")"); |
| 298 | break; |
Olli Etuaho | 31b5fc6 | 2015-01-16 12:13:36 +0200 | [diff] [blame] | 299 | case EOpBitShiftLeftAssign: |
| 300 | writeTriplet(visit, "(", " <<= ", ")"); |
| 301 | break; |
| 302 | case EOpBitShiftRightAssign: |
| 303 | writeTriplet(visit, "(", " >>= ", ")"); |
| 304 | break; |
| 305 | case EOpBitwiseAndAssign: |
| 306 | writeTriplet(visit, "(", " &= ", ")"); |
| 307 | break; |
| 308 | case EOpBitwiseXorAssign: |
| 309 | writeTriplet(visit, "(", " ^= ", ")"); |
| 310 | break; |
| 311 | case EOpBitwiseOrAssign: |
| 312 | writeTriplet(visit, "(", " |= ", ")"); |
| 313 | break; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 314 | |
| 315 | case EOpIndexDirect: |
| 316 | writeTriplet(visit, NULL, "[", "]"); |
| 317 | break; |
| 318 | case EOpIndexIndirect: |
| 319 | if (node->getAddIndexClamp()) |
| 320 | { |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 321 | if (visit == InVisit) |
| 322 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 323 | if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC) |
| 324 | out << "[int(clamp(float("; |
| 325 | else |
| 326 | out << "[webgl_int_clamp("; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 327 | } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 328 | else if (visit == PostVisit) |
| 329 | { |
| 330 | int maxSize; |
| 331 | TIntermTyped *left = node->getLeft(); |
| 332 | TType leftType = left->getType(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 333 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 334 | if (left->isArray()) |
| 335 | { |
| 336 | // The shader will fail validation if the array length is not > 0. |
| 337 | maxSize = leftType.getArraySize() - 1; |
| 338 | } |
| 339 | else |
| 340 | { |
| 341 | maxSize = leftType.getNominalSize() - 1; |
| 342 | } |
| 343 | |
| 344 | if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC) |
| 345 | out << "), 0.0, float(" << maxSize << ")))]"; |
| 346 | else |
| 347 | out << ", 0, " << maxSize << ")]"; |
| 348 | } |
| 349 | } |
| 350 | else |
| 351 | { |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 352 | writeTriplet(visit, NULL, "[", "]"); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 353 | } |
| 354 | break; |
| 355 | case EOpIndexDirectStruct: |
| 356 | if (visit == InVisit) |
| 357 | { |
| 358 | // Here we are writing out "foo.bar", where "foo" is struct |
| 359 | // and "bar" is field. In AST, it is represented as a binary |
| 360 | // node, where left child represents "foo" and right child "bar". |
| 361 | // The node itself represents ".". The struct field "bar" is |
| 362 | // actually stored as an index into TStructure::fields. |
| 363 | out << "."; |
| 364 | const TStructure *structure = node->getLeft()->getType().getStruct(); |
| 365 | const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion(); |
| 366 | const TField *field = structure->fields()[index->getIConst(0)]; |
| 367 | |
| 368 | TString fieldName = field->name(); |
| 369 | if (!mSymbolTable.findBuiltIn(structure->name(), mShaderVersion)) |
| 370 | fieldName = hashName(fieldName); |
| 371 | |
| 372 | out << fieldName; |
| 373 | visitChildren = false; |
| 374 | } |
| 375 | break; |
Geoff Lang | 6e36042 | 2015-09-02 15:54:36 -0400 | [diff] [blame] | 376 | case EOpIndexDirectInterfaceBlock: |
| 377 | if (visit == InVisit) |
| 378 | { |
| 379 | out << "."; |
| 380 | const TInterfaceBlock *interfaceBlock = node->getLeft()->getType().getInterfaceBlock(); |
| 381 | const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion(); |
| 382 | const TField *field = interfaceBlock->fields()[index->getIConst(0)]; |
| 383 | |
| 384 | TString fieldName = field->name(); |
| 385 | ASSERT(!mSymbolTable.findBuiltIn(interfaceBlock->name(), mShaderVersion)); |
| 386 | fieldName = hashName(fieldName); |
| 387 | |
| 388 | out << fieldName; |
| 389 | visitChildren = false; |
| 390 | } |
| 391 | break; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 392 | case EOpVectorSwizzle: |
| 393 | if (visit == InVisit) |
| 394 | { |
| 395 | out << "."; |
| 396 | TIntermAggregate *rightChild = node->getRight()->getAsAggregate(); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 397 | TIntermSequence *sequence = rightChild->getSequence(); |
| 398 | for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); ++sit) |
daniel@transgaming.com | 4167cc9 | 2013-01-11 04:11:53 +0000 | [diff] [blame] | 399 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 400 | TIntermConstantUnion *element = (*sit)->getAsConstantUnion(); |
| 401 | ASSERT(element->getBasicType() == EbtInt); |
| 402 | ASSERT(element->getNominalSize() == 1); |
Jamie Madill | 6ba6ead | 2015-05-04 14:21:21 -0400 | [diff] [blame] | 403 | const TConstantUnion& data = element->getUnionArrayPointer()[0]; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 404 | ASSERT(data.getType() == EbtInt); |
| 405 | switch (data.getIConst()) |
daniel@transgaming.com | 4167cc9 | 2013-01-11 04:11:53 +0000 | [diff] [blame] | 406 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 407 | case 0: |
| 408 | out << "x"; |
| 409 | break; |
| 410 | case 1: |
| 411 | out << "y"; |
| 412 | break; |
| 413 | case 2: |
| 414 | out << "z"; |
| 415 | break; |
| 416 | case 3: |
| 417 | out << "w"; |
| 418 | break; |
| 419 | default: |
| 420 | UNREACHABLE(); |
daniel@transgaming.com | 4167cc9 | 2013-01-11 04:11:53 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 423 | visitChildren = false; |
| 424 | } |
| 425 | break; |
daniel@transgaming.com | 97b16d1 | 2013-02-01 03:20:42 +0000 | [diff] [blame] | 426 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 427 | case EOpAdd: |
| 428 | writeTriplet(visit, "(", " + ", ")"); |
| 429 | break; |
| 430 | case EOpSub: |
| 431 | writeTriplet(visit, "(", " - ", ")"); |
| 432 | break; |
| 433 | case EOpMul: |
| 434 | writeTriplet(visit, "(", " * ", ")"); |
| 435 | break; |
| 436 | case EOpDiv: |
| 437 | writeTriplet(visit, "(", " / ", ")"); |
| 438 | break; |
Olli Etuaho | ff805cc | 2015-02-13 10:59:34 +0200 | [diff] [blame] | 439 | case EOpIMod: |
Gregoire Payen de La Garanderie | be954a2 | 2014-12-23 00:05:28 +0000 | [diff] [blame] | 440 | writeTriplet(visit, "(", " % ", ")"); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 441 | break; |
Olli Etuaho | 31b5fc6 | 2015-01-16 12:13:36 +0200 | [diff] [blame] | 442 | case EOpBitShiftLeft: |
| 443 | writeTriplet(visit, "(", " << ", ")"); |
| 444 | break; |
| 445 | case EOpBitShiftRight: |
| 446 | writeTriplet(visit, "(", " >> ", ")"); |
| 447 | break; |
| 448 | case EOpBitwiseAnd: |
| 449 | writeTriplet(visit, "(", " & ", ")"); |
| 450 | break; |
| 451 | case EOpBitwiseXor: |
| 452 | writeTriplet(visit, "(", " ^ ", ")"); |
| 453 | break; |
| 454 | case EOpBitwiseOr: |
| 455 | writeTriplet(visit, "(", " | ", ")"); |
| 456 | break; |
| 457 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 458 | case EOpEqual: |
| 459 | writeTriplet(visit, "(", " == ", ")"); |
| 460 | break; |
| 461 | case EOpNotEqual: |
| 462 | writeTriplet(visit, "(", " != ", ")"); |
| 463 | break; |
| 464 | case EOpLessThan: |
| 465 | writeTriplet(visit, "(", " < ", ")"); |
| 466 | break; |
| 467 | case EOpGreaterThan: |
| 468 | writeTriplet(visit, "(", " > ", ")"); |
| 469 | break; |
| 470 | case EOpLessThanEqual: |
| 471 | writeTriplet(visit, "(", " <= ", ")"); |
| 472 | break; |
| 473 | case EOpGreaterThanEqual: |
| 474 | writeTriplet(visit, "(", " >= ", ")"); |
| 475 | break; |
daniel@transgaming.com | 97b16d1 | 2013-02-01 03:20:42 +0000 | [diff] [blame] | 476 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 477 | // Notice the fall-through. |
| 478 | case EOpVectorTimesScalar: |
| 479 | case EOpVectorTimesMatrix: |
| 480 | case EOpMatrixTimesVector: |
| 481 | case EOpMatrixTimesScalar: |
| 482 | case EOpMatrixTimesMatrix: |
| 483 | writeTriplet(visit, "(", " * ", ")"); |
| 484 | break; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 485 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 486 | case EOpLogicalOr: |
| 487 | writeTriplet(visit, "(", " || ", ")"); |
| 488 | break; |
| 489 | case EOpLogicalXor: |
| 490 | writeTriplet(visit, "(", " ^^ ", ")"); |
| 491 | break; |
| 492 | case EOpLogicalAnd: |
| 493 | writeTriplet(visit, "(", " && ", ")"); |
| 494 | break; |
| 495 | default: |
| 496 | UNREACHABLE(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | return visitChildren; |
| 500 | } |
| 501 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 502 | bool TOutputGLSLBase::visitUnary(Visit visit, TIntermUnary *node) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 503 | { |
zmo@google.com | 32e9731 | 2011-08-24 01:03:11 +0000 | [diff] [blame] | 504 | TString preString; |
| 505 | TString postString = ")"; |
| 506 | |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 507 | switch (node->getOp()) |
| 508 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 509 | case EOpNegative: preString = "(-"; break; |
Zhenyao Mo | de1e00e | 2014-10-09 16:55:32 -0700 | [diff] [blame] | 510 | case EOpPositive: preString = "(+"; break; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 511 | case EOpVectorLogicalNot: preString = "not("; break; |
| 512 | case EOpLogicalNot: preString = "(!"; break; |
Olli Etuaho | 31b5fc6 | 2015-01-16 12:13:36 +0200 | [diff] [blame] | 513 | case EOpBitwiseNot: preString = "(~"; break; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 514 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 515 | case EOpPostIncrement: preString = "("; postString = "++)"; break; |
| 516 | case EOpPostDecrement: preString = "("; postString = "--)"; break; |
| 517 | case EOpPreIncrement: preString = "(++"; break; |
| 518 | case EOpPreDecrement: preString = "(--"; break; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 519 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 520 | case EOpRadians: |
| 521 | preString = "radians("; |
| 522 | break; |
| 523 | case EOpDegrees: |
| 524 | preString = "degrees("; |
| 525 | break; |
| 526 | case EOpSin: |
| 527 | preString = "sin("; |
| 528 | break; |
| 529 | case EOpCos: |
| 530 | preString = "cos("; |
| 531 | break; |
| 532 | case EOpTan: |
| 533 | preString = "tan("; |
| 534 | break; |
| 535 | case EOpAsin: |
| 536 | preString = "asin("; |
| 537 | break; |
| 538 | case EOpAcos: |
| 539 | preString = "acos("; |
| 540 | break; |
| 541 | case EOpAtan: |
| 542 | preString = "atan("; |
| 543 | break; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 544 | |
Olli Etuaho | 5c9cd3d | 2014-12-18 13:04:25 +0200 | [diff] [blame] | 545 | case EOpSinh: |
| 546 | preString = "sinh("; |
| 547 | break; |
| 548 | case EOpCosh: |
| 549 | preString = "cosh("; |
| 550 | break; |
| 551 | case EOpTanh: |
| 552 | preString = "tanh("; |
| 553 | break; |
| 554 | case EOpAsinh: |
| 555 | preString = "asinh("; |
| 556 | break; |
| 557 | case EOpAcosh: |
| 558 | preString = "acosh("; |
| 559 | break; |
| 560 | case EOpAtanh: |
| 561 | preString = "atanh("; |
| 562 | break; |
| 563 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 564 | case EOpExp: |
| 565 | preString = "exp("; |
| 566 | break; |
| 567 | case EOpLog: |
| 568 | preString = "log("; |
| 569 | break; |
| 570 | case EOpExp2: |
| 571 | preString = "exp2("; |
| 572 | break; |
| 573 | case EOpLog2: |
| 574 | preString = "log2("; |
| 575 | break; |
| 576 | case EOpSqrt: |
| 577 | preString = "sqrt("; |
| 578 | break; |
| 579 | case EOpInverseSqrt: |
| 580 | preString = "inversesqrt("; |
| 581 | break; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 582 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 583 | case EOpAbs: |
| 584 | preString = "abs("; |
| 585 | break; |
| 586 | case EOpSign: |
| 587 | preString = "sign("; |
| 588 | break; |
| 589 | case EOpFloor: |
| 590 | preString = "floor("; |
| 591 | break; |
Qingqing Deng | 5dbece5 | 2015-02-27 20:35:38 -0800 | [diff] [blame] | 592 | case EOpTrunc: |
| 593 | preString = "trunc("; |
| 594 | break; |
| 595 | case EOpRound: |
| 596 | preString = "round("; |
| 597 | break; |
| 598 | case EOpRoundEven: |
| 599 | preString = "roundEven("; |
| 600 | break; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 601 | case EOpCeil: |
| 602 | preString = "ceil("; |
| 603 | break; |
| 604 | case EOpFract: |
| 605 | preString = "fract("; |
| 606 | break; |
Arun Patole | 0c1726e | 2015-02-18 14:35:02 +0530 | [diff] [blame] | 607 | case EOpIsNan: |
| 608 | preString = "isnan("; |
| 609 | break; |
| 610 | case EOpIsInf: |
| 611 | preString = "isinf("; |
| 612 | break; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 613 | |
Olli Etuaho | e8d2c07 | 2015-01-08 16:33:54 +0200 | [diff] [blame] | 614 | case EOpFloatBitsToInt: |
| 615 | preString = "floatBitsToInt("; |
| 616 | break; |
| 617 | case EOpFloatBitsToUint: |
| 618 | preString = "floatBitsToUint("; |
| 619 | break; |
| 620 | case EOpIntBitsToFloat: |
| 621 | preString = "intBitsToFloat("; |
| 622 | break; |
| 623 | case EOpUintBitsToFloat: |
| 624 | preString = "uintBitsToFloat("; |
| 625 | break; |
| 626 | |
Olli Etuaho | 7700ff6 | 2015-01-15 12:16:29 +0200 | [diff] [blame] | 627 | case EOpPackSnorm2x16: |
| 628 | preString = "packSnorm2x16("; |
| 629 | break; |
| 630 | case EOpPackUnorm2x16: |
| 631 | preString = "packUnorm2x16("; |
| 632 | break; |
| 633 | case EOpPackHalf2x16: |
| 634 | preString = "packHalf2x16("; |
| 635 | break; |
| 636 | case EOpUnpackSnorm2x16: |
| 637 | preString = "unpackSnorm2x16("; |
| 638 | break; |
| 639 | case EOpUnpackUnorm2x16: |
| 640 | preString = "unpackUnorm2x16("; |
| 641 | break; |
| 642 | case EOpUnpackHalf2x16: |
| 643 | preString = "unpackHalf2x16("; |
| 644 | break; |
| 645 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 646 | case EOpLength: |
| 647 | preString = "length("; |
| 648 | break; |
| 649 | case EOpNormalize: |
| 650 | preString = "normalize("; |
| 651 | break; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 652 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 653 | case EOpDFdx: |
| 654 | preString = "dFdx("; |
| 655 | break; |
| 656 | case EOpDFdy: |
| 657 | preString = "dFdy("; |
| 658 | break; |
| 659 | case EOpFwidth: |
| 660 | preString = "fwidth("; |
| 661 | break; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 662 | |
Olli Etuaho | e39706d | 2014-12-30 16:40:36 +0200 | [diff] [blame] | 663 | case EOpTranspose: |
| 664 | preString = "transpose("; |
| 665 | break; |
| 666 | case EOpDeterminant: |
| 667 | preString = "determinant("; |
| 668 | break; |
Olli Etuaho | abf6dad | 2015-01-14 14:45:16 +0200 | [diff] [blame] | 669 | case EOpInverse: |
| 670 | preString = "inverse("; |
| 671 | break; |
Olli Etuaho | e39706d | 2014-12-30 16:40:36 +0200 | [diff] [blame] | 672 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 673 | case EOpAny: |
| 674 | preString = "any("; |
| 675 | break; |
| 676 | case EOpAll: |
| 677 | preString = "all("; |
| 678 | break; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 679 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 680 | default: |
| 681 | UNREACHABLE(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 682 | } |
| 683 | |
zmo@google.com | 32e9731 | 2011-08-24 01:03:11 +0000 | [diff] [blame] | 684 | if (visit == PreVisit && node->getUseEmulatedFunction()) |
| 685 | preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preString); |
| 686 | writeTriplet(visit, preString.c_str(), NULL, postString.c_str()); |
| 687 | |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 688 | return true; |
| 689 | } |
| 690 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 691 | bool TOutputGLSLBase::visitSelection(Visit visit, TIntermSelection *node) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 692 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 693 | TInfoSinkBase &out = objSink(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 694 | |
| 695 | if (node->usesTernaryOperator()) |
| 696 | { |
| 697 | // Notice two brackets at the beginning and end. The outer ones |
| 698 | // encapsulate the whole ternary expression. This preserves the |
| 699 | // order of precedence when ternary expressions are used in a |
| 700 | // compound expression, i.e., c = 2 * (a < b ? 1 : 2). |
| 701 | out << "(("; |
| 702 | node->getCondition()->traverse(this); |
| 703 | out << ") ? ("; |
| 704 | node->getTrueBlock()->traverse(this); |
| 705 | out << ") : ("; |
| 706 | node->getFalseBlock()->traverse(this); |
| 707 | out << "))"; |
| 708 | } |
| 709 | else |
| 710 | { |
| 711 | out << "if ("; |
| 712 | node->getCondition()->traverse(this); |
| 713 | out << ")\n"; |
| 714 | |
Zhenyao Mo | 7cab38b | 2013-10-15 12:59:30 -0700 | [diff] [blame] | 715 | incrementDepth(node); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 716 | visitCodeBlock(node->getTrueBlock()); |
| 717 | |
| 718 | if (node->getFalseBlock()) |
| 719 | { |
| 720 | out << "else\n"; |
| 721 | visitCodeBlock(node->getFalseBlock()); |
| 722 | } |
| 723 | decrementDepth(); |
| 724 | } |
| 725 | return false; |
| 726 | } |
| 727 | |
Olli Etuaho | 01cd8af | 2015-02-20 10:39:20 +0200 | [diff] [blame] | 728 | bool TOutputGLSLBase::visitSwitch(Visit visit, TIntermSwitch *node) |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 729 | { |
Olli Etuaho | 01cd8af | 2015-02-20 10:39:20 +0200 | [diff] [blame] | 730 | if (node->getStatementList()) |
| 731 | { |
| 732 | writeTriplet(visit, "switch (", ") ", nullptr); |
| 733 | // The curly braces get written when visiting the statementList aggregate |
| 734 | } |
| 735 | else |
| 736 | { |
| 737 | // No statementList, so it won't output curly braces |
| 738 | writeTriplet(visit, "switch (", ") {", "}\n"); |
| 739 | } |
| 740 | return true; |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 741 | } |
| 742 | |
Olli Etuaho | 01cd8af | 2015-02-20 10:39:20 +0200 | [diff] [blame] | 743 | bool TOutputGLSLBase::visitCase(Visit visit, TIntermCase *node) |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 744 | { |
Olli Etuaho | 01cd8af | 2015-02-20 10:39:20 +0200 | [diff] [blame] | 745 | if (node->hasCondition()) |
| 746 | { |
| 747 | writeTriplet(visit, "case (", nullptr, "):\n"); |
| 748 | return true; |
| 749 | } |
| 750 | else |
| 751 | { |
| 752 | TInfoSinkBase &out = objSink(); |
| 753 | out << "default:\n"; |
| 754 | return false; |
| 755 | } |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 756 | } |
| 757 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 758 | bool TOutputGLSLBase::visitAggregate(Visit visit, TIntermAggregate *node) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 759 | { |
| 760 | bool visitChildren = true; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 761 | TInfoSinkBase &out = objSink(); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 762 | bool useEmulatedFunction = (visit == PreVisit && node->getUseEmulatedFunction()); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 763 | switch (node->getOp()) |
| 764 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 765 | case EOpSequence: |
| 766 | // Scope the sequences except when at the global scope. |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 767 | if (mDepth > 0) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 768 | { |
| 769 | out << "{\n"; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 770 | } |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 771 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 772 | incrementDepth(node); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 773 | for (TIntermSequence::const_iterator iter = node->getSequence()->begin(); |
| 774 | iter != node->getSequence()->end(); ++iter) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 775 | { |
Austin Kinross | 3ae6465 | 2015-01-26 15:51:39 -0800 | [diff] [blame] | 776 | TIntermNode *curNode = *iter; |
| 777 | ASSERT(curNode != NULL); |
| 778 | curNode->traverse(this); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 779 | |
Austin Kinross | 3ae6465 | 2015-01-26 15:51:39 -0800 | [diff] [blame] | 780 | if (isSingleStatement(curNode)) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 781 | out << ";\n"; |
| 782 | } |
| 783 | decrementDepth(); |
| 784 | |
| 785 | // Scope the sequences except when at the global scope. |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 786 | if (mDepth > 0) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 787 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 788 | out << "}\n"; |
| 789 | } |
| 790 | visitChildren = false; |
| 791 | break; |
| 792 | case EOpPrototype: |
| 793 | // Function declaration. |
| 794 | ASSERT(visit == PreVisit); |
Olli Etuaho | ab6fc6a | 2015-04-13 12:10:20 +0300 | [diff] [blame] | 795 | { |
| 796 | const TType &type = node->getType(); |
| 797 | writeVariableType(type); |
| 798 | if (type.isArray()) |
| 799 | out << arrayBrackets(type); |
| 800 | } |
| 801 | |
Olli Etuaho | 59f9a64 | 2015-08-06 20:38:26 +0300 | [diff] [blame] | 802 | out << " " << hashFunctionNameIfNeeded(node->getNameObj()); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 803 | |
| 804 | out << "("; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 805 | writeFunctionParameters(*(node->getSequence())); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 806 | out << ")"; |
| 807 | |
| 808 | visitChildren = false; |
| 809 | break; |
| 810 | case EOpFunction: { |
| 811 | // Function definition. |
| 812 | ASSERT(visit == PreVisit); |
Olli Etuaho | ab6fc6a | 2015-04-13 12:10:20 +0300 | [diff] [blame] | 813 | { |
| 814 | const TType &type = node->getType(); |
| 815 | writeVariableType(type); |
| 816 | if (type.isArray()) |
| 817 | out << arrayBrackets(type); |
| 818 | } |
| 819 | |
Olli Etuaho | 59f9a64 | 2015-08-06 20:38:26 +0300 | [diff] [blame] | 820 | out << " " << hashFunctionNameIfNeeded(node->getNameObj()); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 821 | |
| 822 | incrementDepth(node); |
| 823 | // Function definition node contains one or two children nodes |
| 824 | // representing function parameters and function body. The latter |
| 825 | // is not present in case of empty function bodies. |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 826 | const TIntermSequence &sequence = *(node->getSequence()); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 827 | ASSERT((sequence.size() == 1) || (sequence.size() == 2)); |
| 828 | TIntermSequence::const_iterator seqIter = sequence.begin(); |
| 829 | |
| 830 | // Traverse function parameters. |
| 831 | TIntermAggregate *params = (*seqIter)->getAsAggregate(); |
| 832 | ASSERT(params != NULL); |
| 833 | ASSERT(params->getOp() == EOpParameters); |
| 834 | params->traverse(this); |
| 835 | |
| 836 | // Traverse function body. |
| 837 | TIntermAggregate *body = ++seqIter != sequence.end() ? |
| 838 | (*seqIter)->getAsAggregate() : NULL; |
| 839 | visitCodeBlock(body); |
| 840 | decrementDepth(); |
| 841 | |
| 842 | // Fully processed; no need to visit children. |
| 843 | visitChildren = false; |
| 844 | break; |
| 845 | } |
| 846 | case EOpFunctionCall: |
| 847 | // Function call. |
| 848 | if (visit == PreVisit) |
Olli Etuaho | 59f9a64 | 2015-08-06 20:38:26 +0300 | [diff] [blame] | 849 | out << hashFunctionNameIfNeeded(node->getNameObj()) << "("; |
Olli Etuaho | 853dc1a | 2014-11-06 17:25:48 +0200 | [diff] [blame] | 850 | else if (visit == InVisit) |
| 851 | out << ", "; |
| 852 | else |
| 853 | out << ")"; |
| 854 | break; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 855 | case EOpParameters: |
| 856 | // Function parameters. |
| 857 | ASSERT(visit == PreVisit); |
| 858 | out << "("; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 859 | writeFunctionParameters(*(node->getSequence())); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 860 | out << ")"; |
| 861 | visitChildren = false; |
| 862 | break; |
| 863 | case EOpDeclaration: |
| 864 | // Variable declaration. |
| 865 | if (visit == PreVisit) |
| 866 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 867 | const TIntermSequence &sequence = *(node->getSequence()); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 868 | const TIntermTyped *variable = sequence.front()->getAsTyped(); |
| 869 | writeVariableType(variable->getType()); |
| 870 | out << " "; |
| 871 | mDeclaringVariables = true; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 872 | } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 873 | else if (visit == InVisit) |
| 874 | { |
| 875 | out << ", "; |
| 876 | mDeclaringVariables = true; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 877 | } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 878 | else |
| 879 | { |
| 880 | mDeclaringVariables = false; |
| 881 | } |
| 882 | break; |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 883 | case EOpInvariantDeclaration: |
| 884 | // Invariant declaration. |
| 885 | ASSERT(visit == PreVisit); |
| 886 | { |
Jamie Madill | 3b5c2da | 2014-08-19 15:23:32 -0400 | [diff] [blame] | 887 | const TIntermSequence *sequence = node->getSequence(); |
| 888 | ASSERT(sequence && sequence->size() == 1); |
| 889 | const TIntermSymbol *symbol = sequence->front()->getAsSymbolNode(); |
| 890 | ASSERT(symbol); |
Zhenyao Mo | 2a51727 | 2014-10-27 16:09:57 -0700 | [diff] [blame] | 891 | out << "invariant " << hashVariableName(symbol->getSymbol()); |
Jamie Madill | 3b5c2da | 2014-08-19 15:23:32 -0400 | [diff] [blame] | 892 | } |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 893 | visitChildren = false; |
| 894 | break; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 895 | case EOpConstructFloat: |
Olli Etuaho | f40319e | 2015-03-10 14:33:00 +0200 | [diff] [blame] | 896 | writeConstructorTriplet(visit, node->getType(), "float"); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 897 | break; |
| 898 | case EOpConstructVec2: |
Olli Etuaho | f40319e | 2015-03-10 14:33:00 +0200 | [diff] [blame] | 899 | writeConstructorTriplet(visit, node->getType(), "vec2"); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 900 | break; |
| 901 | case EOpConstructVec3: |
Olli Etuaho | f40319e | 2015-03-10 14:33:00 +0200 | [diff] [blame] | 902 | writeConstructorTriplet(visit, node->getType(), "vec3"); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 903 | break; |
| 904 | case EOpConstructVec4: |
Olli Etuaho | f40319e | 2015-03-10 14:33:00 +0200 | [diff] [blame] | 905 | writeConstructorTriplet(visit, node->getType(), "vec4"); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 906 | break; |
| 907 | case EOpConstructBool: |
Olli Etuaho | f40319e | 2015-03-10 14:33:00 +0200 | [diff] [blame] | 908 | writeConstructorTriplet(visit, node->getType(), "bool"); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 909 | break; |
| 910 | case EOpConstructBVec2: |
Olli Etuaho | f40319e | 2015-03-10 14:33:00 +0200 | [diff] [blame] | 911 | writeConstructorTriplet(visit, node->getType(), "bvec2"); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 912 | break; |
| 913 | case EOpConstructBVec3: |
Olli Etuaho | f40319e | 2015-03-10 14:33:00 +0200 | [diff] [blame] | 914 | writeConstructorTriplet(visit, node->getType(), "bvec3"); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 915 | break; |
| 916 | case EOpConstructBVec4: |
Olli Etuaho | f40319e | 2015-03-10 14:33:00 +0200 | [diff] [blame] | 917 | writeConstructorTriplet(visit, node->getType(), "bvec4"); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 918 | break; |
| 919 | case EOpConstructInt: |
Olli Etuaho | f40319e | 2015-03-10 14:33:00 +0200 | [diff] [blame] | 920 | writeConstructorTriplet(visit, node->getType(), "int"); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 921 | break; |
| 922 | case EOpConstructIVec2: |
Olli Etuaho | f40319e | 2015-03-10 14:33:00 +0200 | [diff] [blame] | 923 | writeConstructorTriplet(visit, node->getType(), "ivec2"); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 924 | break; |
| 925 | case EOpConstructIVec3: |
Olli Etuaho | f40319e | 2015-03-10 14:33:00 +0200 | [diff] [blame] | 926 | writeConstructorTriplet(visit, node->getType(), "ivec3"); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 927 | break; |
| 928 | case EOpConstructIVec4: |
Olli Etuaho | f40319e | 2015-03-10 14:33:00 +0200 | [diff] [blame] | 929 | writeConstructorTriplet(visit, node->getType(), "ivec4"); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 930 | break; |
Geoff Lang | c4c7442d | 2015-07-20 13:09:26 -0400 | [diff] [blame] | 931 | case EOpConstructUInt: |
| 932 | writeConstructorTriplet(visit, node->getType(), "uint"); |
| 933 | break; |
| 934 | case EOpConstructUVec2: |
| 935 | writeConstructorTriplet(visit, node->getType(), "uvec2"); |
| 936 | break; |
| 937 | case EOpConstructUVec3: |
| 938 | writeConstructorTriplet(visit, node->getType(), "uvec3"); |
| 939 | break; |
| 940 | case EOpConstructUVec4: |
| 941 | writeConstructorTriplet(visit, node->getType(), "uvec4"); |
| 942 | break; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 943 | case EOpConstructMat2: |
Olli Etuaho | f40319e | 2015-03-10 14:33:00 +0200 | [diff] [blame] | 944 | writeConstructorTriplet(visit, node->getType(), "mat2"); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 945 | break; |
Alexis Hetu | 07e57df | 2015-06-16 16:55:52 -0400 | [diff] [blame] | 946 | case EOpConstructMat2x3: |
| 947 | writeConstructorTriplet(visit, node->getType(), "mat2x3"); |
| 948 | break; |
| 949 | case EOpConstructMat2x4: |
| 950 | writeConstructorTriplet(visit, node->getType(), "mat2x4"); |
| 951 | break; |
| 952 | case EOpConstructMat3x2: |
| 953 | writeConstructorTriplet(visit, node->getType(), "mat3x2"); |
| 954 | break; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 955 | case EOpConstructMat3: |
Olli Etuaho | f40319e | 2015-03-10 14:33:00 +0200 | [diff] [blame] | 956 | writeConstructorTriplet(visit, node->getType(), "mat3"); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 957 | break; |
Alexis Hetu | 07e57df | 2015-06-16 16:55:52 -0400 | [diff] [blame] | 958 | case EOpConstructMat3x4: |
| 959 | writeConstructorTriplet(visit, node->getType(), "mat3x4"); |
| 960 | break; |
| 961 | case EOpConstructMat4x2: |
| 962 | writeConstructorTriplet(visit, node->getType(), "mat4x2"); |
| 963 | break; |
| 964 | case EOpConstructMat4x3: |
| 965 | writeConstructorTriplet(visit, node->getType(), "mat4x3"); |
| 966 | break; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 967 | case EOpConstructMat4: |
Olli Etuaho | f40319e | 2015-03-10 14:33:00 +0200 | [diff] [blame] | 968 | writeConstructorTriplet(visit, node->getType(), "mat4"); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 969 | break; |
| 970 | case EOpConstructStruct: |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 971 | { |
| 972 | const TType &type = node->getType(); |
| 973 | ASSERT(type.getBasicType() == EbtStruct); |
Olli Etuaho | f40319e | 2015-03-10 14:33:00 +0200 | [diff] [blame] | 974 | TString constructorName = hashName(type.getStruct()->name()); |
| 975 | writeConstructorTriplet(visit, node->getType(), constructorName.c_str()); |
| 976 | break; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 977 | } |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 978 | |
Olli Etuaho | e39706d | 2014-12-30 16:40:36 +0200 | [diff] [blame] | 979 | case EOpOuterProduct: |
| 980 | writeBuiltInFunctionTriplet(visit, "outerProduct(", useEmulatedFunction); |
| 981 | break; |
| 982 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 983 | case EOpLessThan: |
| 984 | writeBuiltInFunctionTriplet(visit, "lessThan(", useEmulatedFunction); |
| 985 | break; |
| 986 | case EOpGreaterThan: |
| 987 | writeBuiltInFunctionTriplet(visit, "greaterThan(", useEmulatedFunction); |
| 988 | break; |
| 989 | case EOpLessThanEqual: |
| 990 | writeBuiltInFunctionTriplet(visit, "lessThanEqual(", useEmulatedFunction); |
| 991 | break; |
| 992 | case EOpGreaterThanEqual: |
| 993 | writeBuiltInFunctionTriplet(visit, "greaterThanEqual(", useEmulatedFunction); |
| 994 | break; |
| 995 | case EOpVectorEqual: |
| 996 | writeBuiltInFunctionTriplet(visit, "equal(", useEmulatedFunction); |
| 997 | break; |
| 998 | case EOpVectorNotEqual: |
| 999 | writeBuiltInFunctionTriplet(visit, "notEqual(", useEmulatedFunction); |
| 1000 | break; |
| 1001 | case EOpComma: |
Zhenyao Mo | 0783efd | 2014-10-21 15:51:59 -0700 | [diff] [blame] | 1002 | writeTriplet(visit, "(", ", ", ")"); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 1003 | break; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 1004 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 1005 | case EOpMod: |
| 1006 | writeBuiltInFunctionTriplet(visit, "mod(", useEmulatedFunction); |
| 1007 | break; |
Olli Etuaho | b6e07a6 | 2015-02-16 12:22:10 +0200 | [diff] [blame] | 1008 | case EOpModf: |
| 1009 | writeBuiltInFunctionTriplet(visit, "modf(", useEmulatedFunction); |
| 1010 | break; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 1011 | case EOpPow: |
| 1012 | writeBuiltInFunctionTriplet(visit, "pow(", useEmulatedFunction); |
| 1013 | break; |
| 1014 | case EOpAtan: |
| 1015 | writeBuiltInFunctionTriplet(visit, "atan(", useEmulatedFunction); |
| 1016 | break; |
| 1017 | case EOpMin: |
| 1018 | writeBuiltInFunctionTriplet(visit, "min(", useEmulatedFunction); |
| 1019 | break; |
| 1020 | case EOpMax: |
| 1021 | writeBuiltInFunctionTriplet(visit, "max(", useEmulatedFunction); |
| 1022 | break; |
| 1023 | case EOpClamp: |
| 1024 | writeBuiltInFunctionTriplet(visit, "clamp(", useEmulatedFunction); |
| 1025 | break; |
| 1026 | case EOpMix: |
| 1027 | writeBuiltInFunctionTriplet(visit, "mix(", useEmulatedFunction); |
| 1028 | break; |
| 1029 | case EOpStep: |
| 1030 | writeBuiltInFunctionTriplet(visit, "step(", useEmulatedFunction); |
| 1031 | break; |
| 1032 | case EOpSmoothStep: |
| 1033 | writeBuiltInFunctionTriplet(visit, "smoothstep(", useEmulatedFunction); |
| 1034 | break; |
| 1035 | case EOpDistance: |
| 1036 | writeBuiltInFunctionTriplet(visit, "distance(", useEmulatedFunction); |
| 1037 | break; |
| 1038 | case EOpDot: |
| 1039 | writeBuiltInFunctionTriplet(visit, "dot(", useEmulatedFunction); |
| 1040 | break; |
| 1041 | case EOpCross: |
| 1042 | writeBuiltInFunctionTriplet(visit, "cross(", useEmulatedFunction); |
| 1043 | break; |
| 1044 | case EOpFaceForward: |
| 1045 | writeBuiltInFunctionTriplet(visit, "faceforward(", useEmulatedFunction); |
| 1046 | break; |
| 1047 | case EOpReflect: |
| 1048 | writeBuiltInFunctionTriplet(visit, "reflect(", useEmulatedFunction); |
| 1049 | break; |
| 1050 | case EOpRefract: |
| 1051 | writeBuiltInFunctionTriplet(visit, "refract(", useEmulatedFunction); |
| 1052 | break; |
| 1053 | case EOpMul: |
| 1054 | writeBuiltInFunctionTriplet(visit, "matrixCompMult(", useEmulatedFunction); |
| 1055 | break; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 1056 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 1057 | default: |
| 1058 | UNREACHABLE(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 1059 | } |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 1060 | return visitChildren; |
| 1061 | } |
| 1062 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 1063 | bool TOutputGLSLBase::visitLoop(Visit visit, TIntermLoop *node) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 1064 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 1065 | TInfoSinkBase &out = objSink(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 1066 | |
Zhenyao Mo | 7cab38b | 2013-10-15 12:59:30 -0700 | [diff] [blame] | 1067 | incrementDepth(node); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 1068 | // Loop header. |
| 1069 | TLoopType loopType = node->getType(); |
| 1070 | if (loopType == ELoopFor) // for loop |
| 1071 | { |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 1072 | if (!node->getUnrollFlag()) |
| 1073 | { |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 1074 | out << "for ("; |
| 1075 | if (node->getInit()) |
| 1076 | node->getInit()->traverse(this); |
| 1077 | out << "; "; |
| 1078 | |
| 1079 | if (node->getCondition()) |
| 1080 | node->getCondition()->traverse(this); |
| 1081 | out << "; "; |
| 1082 | |
| 1083 | if (node->getExpression()) |
| 1084 | node->getExpression()->traverse(this); |
| 1085 | out << ")\n"; |
| 1086 | } |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 1087 | else |
| 1088 | { |
| 1089 | // Need to put a one-iteration loop here to handle break. |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1090 | TIntermSequence *declSeq = |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 1091 | node->getInit()->getAsAggregate()->getSequence(); |
| 1092 | TIntermSymbol *indexSymbol = |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1093 | (*declSeq)[0]->getAsBinaryNode()->getLeft()->getAsSymbolNode(); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 1094 | TString name = hashVariableName(indexSymbol->getSymbol()); |
| 1095 | out << "for (int " << name << " = 0; " |
| 1096 | << name << " < 1; " |
| 1097 | << "++" << name << ")\n"; |
| 1098 | } |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 1099 | } |
| 1100 | else if (loopType == ELoopWhile) // while loop |
| 1101 | { |
| 1102 | out << "while ("; |
| 1103 | ASSERT(node->getCondition() != NULL); |
| 1104 | node->getCondition()->traverse(this); |
| 1105 | out << ")\n"; |
| 1106 | } |
| 1107 | else // do-while loop |
| 1108 | { |
| 1109 | ASSERT(loopType == ELoopDoWhile); |
| 1110 | out << "do\n"; |
| 1111 | } |
| 1112 | |
| 1113 | // Loop body. |
| 1114 | if (node->getUnrollFlag()) |
| 1115 | { |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 1116 | out << "{\n"; |
| 1117 | mLoopUnrollStack.push(node); |
| 1118 | while (mLoopUnrollStack.satisfiesLoopCondition()) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 1119 | { |
| 1120 | visitCodeBlock(node->getBody()); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 1121 | mLoopUnrollStack.step(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 1122 | } |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 1123 | mLoopUnrollStack.pop(); |
| 1124 | out << "}\n"; |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 1125 | } |
| 1126 | else |
| 1127 | { |
| 1128 | visitCodeBlock(node->getBody()); |
| 1129 | } |
| 1130 | |
| 1131 | // Loop footer. |
| 1132 | if (loopType == ELoopDoWhile) // do-while loop |
| 1133 | { |
| 1134 | out << "while ("; |
| 1135 | ASSERT(node->getCondition() != NULL); |
| 1136 | node->getCondition()->traverse(this); |
| 1137 | out << ");\n"; |
| 1138 | } |
| 1139 | decrementDepth(); |
| 1140 | |
| 1141 | // No need to visit children. They have been already processed in |
| 1142 | // this function. |
| 1143 | return false; |
| 1144 | } |
| 1145 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 1146 | bool TOutputGLSLBase::visitBranch(Visit visit, TIntermBranch *node) |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 1147 | { |
| 1148 | switch (node->getFlowOp()) |
| 1149 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 1150 | case EOpKill: |
| 1151 | writeTriplet(visit, "discard", NULL, NULL); |
| 1152 | break; |
| 1153 | case EOpBreak: |
| 1154 | writeTriplet(visit, "break", NULL, NULL); |
| 1155 | break; |
| 1156 | case EOpContinue: |
| 1157 | writeTriplet(visit, "continue", NULL, NULL); |
| 1158 | break; |
| 1159 | case EOpReturn: |
| 1160 | writeTriplet(visit, "return ", NULL, NULL); |
| 1161 | break; |
| 1162 | default: |
| 1163 | UNREACHABLE(); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | return true; |
| 1167 | } |
| 1168 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 1169 | void TOutputGLSLBase::visitCodeBlock(TIntermNode *node) |
| 1170 | { |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 1171 | TInfoSinkBase &out = objSink(); |
| 1172 | if (node != NULL) |
| 1173 | { |
| 1174 | node->traverse(this); |
| 1175 | // Single statements not part of a sequence need to be terminated |
| 1176 | // with semi-colon. |
| 1177 | if (isSingleStatement(node)) |
| 1178 | out << ";\n"; |
| 1179 | } |
| 1180 | else |
| 1181 | { |
| 1182 | out << "{\n}\n"; // Empty code block. |
| 1183 | } |
| 1184 | } |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 1185 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 1186 | TString TOutputGLSLBase::getTypeName(const TType &type) |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 1187 | { |
| 1188 | TInfoSinkBase out; |
| 1189 | if (type.isMatrix()) |
| 1190 | { |
| 1191 | out << "mat"; |
| 1192 | out << type.getNominalSize(); |
Geoff Lang | d8edb51 | 2015-09-02 11:52:56 -0400 | [diff] [blame^] | 1193 | if (type.getSecondarySize() != type.getNominalSize()) |
| 1194 | { |
| 1195 | out << "x" << type.getSecondarySize(); |
| 1196 | } |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 1197 | } |
| 1198 | else if (type.isVector()) |
| 1199 | { |
| 1200 | switch (type.getBasicType()) |
| 1201 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 1202 | case EbtFloat: |
| 1203 | out << "vec"; |
| 1204 | break; |
| 1205 | case EbtInt: |
| 1206 | out << "ivec"; |
| 1207 | break; |
| 1208 | case EbtBool: |
| 1209 | out << "bvec"; |
| 1210 | break; |
Geoff Lang | c4c7442d | 2015-07-20 13:09:26 -0400 | [diff] [blame] | 1211 | case EbtUInt: |
| 1212 | out << "uvec"; |
| 1213 | break; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 1214 | default: |
| 1215 | UNREACHABLE(); |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 1216 | } |
| 1217 | out << type.getNominalSize(); |
| 1218 | } |
| 1219 | else |
| 1220 | { |
| 1221 | if (type.getBasicType() == EbtStruct) |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1222 | out << hashName(type.getStruct()->name()); |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 1223 | else |
| 1224 | out << type.getBasicString(); |
| 1225 | } |
| 1226 | return TString(out.c_str()); |
| 1227 | } |
| 1228 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 1229 | TString TOutputGLSLBase::hashName(const TString &name) |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 1230 | { |
| 1231 | if (mHashFunction == NULL || name.empty()) |
| 1232 | return name; |
| 1233 | NameMap::const_iterator it = mNameMap.find(name.c_str()); |
| 1234 | if (it != mNameMap.end()) |
| 1235 | return it->second.c_str(); |
| 1236 | TString hashedName = TIntermTraverser::hash(name, mHashFunction); |
| 1237 | mNameMap[name.c_str()] = hashedName.c_str(); |
| 1238 | return hashedName; |
| 1239 | } |
| 1240 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 1241 | TString TOutputGLSLBase::hashVariableName(const TString &name) |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 1242 | { |
Jamie Madill | 02f20dd | 2013-09-12 12:07:42 -0400 | [diff] [blame] | 1243 | if (mSymbolTable.findBuiltIn(name, mShaderVersion) != NULL) |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 1244 | return name; |
| 1245 | return hashName(name); |
| 1246 | } |
| 1247 | |
Olli Etuaho | 59f9a64 | 2015-08-06 20:38:26 +0300 | [diff] [blame] | 1248 | TString TOutputGLSLBase::hashFunctionNameIfNeeded(const TName &mangledName) |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 1249 | { |
Olli Etuaho | 59f9a64 | 2015-08-06 20:38:26 +0300 | [diff] [blame] | 1250 | TString mangledStr = mangledName.getString(); |
| 1251 | TString name = TFunction::unmangleName(mangledStr); |
| 1252 | if (mSymbolTable.findBuiltIn(mangledStr, mShaderVersion) != nullptr || name == "main") |
Nicolas Capens | 4648508 | 2014-04-15 13:12:50 -0400 | [diff] [blame] | 1253 | return translateTextureFunction(name); |
Olli Etuaho | 59f9a64 | 2015-08-06 20:38:26 +0300 | [diff] [blame] | 1254 | if (mangledName.isInternal()) |
| 1255 | return name; |
| 1256 | else |
| 1257 | return hashName(name); |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 1258 | } |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1259 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 1260 | bool TOutputGLSLBase::structDeclared(const TStructure *structure) const |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1261 | { |
Zhenyao Mo | 904a916 | 2014-05-09 14:07:45 -0700 | [diff] [blame] | 1262 | ASSERT(structure); |
Jamie Madill | 01f85ac | 2014-06-06 11:55:04 -0400 | [diff] [blame] | 1263 | if (structure->name().empty()) |
Zhenyao Mo | 904a916 | 2014-05-09 14:07:45 -0700 | [diff] [blame] | 1264 | { |
Jamie Madill | 01f85ac | 2014-06-06 11:55:04 -0400 | [diff] [blame] | 1265 | return false; |
Zhenyao Mo | 904a916 | 2014-05-09 14:07:45 -0700 | [diff] [blame] | 1266 | } |
Jamie Madill | 01f85ac | 2014-06-06 11:55:04 -0400 | [diff] [blame] | 1267 | |
| 1268 | return (mDeclaredStructs.count(structure->uniqueId()) > 0); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1269 | } |
| 1270 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 1271 | void TOutputGLSLBase::declareStruct(const TStructure *structure) |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1272 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 1273 | TInfoSinkBase &out = objSink(); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1274 | |
| 1275 | out << "struct " << hashName(structure->name()) << "{\n"; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 1276 | const TFieldList &fields = structure->fields(); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1277 | for (size_t i = 0; i < fields.size(); ++i) |
| 1278 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 1279 | const TField *field = fields[i]; |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1280 | if (writeVariablePrecision(field->type()->getPrecision())) |
| 1281 | out << " "; |
| 1282 | out << getTypeName(*field->type()) << " " << hashName(field->name()); |
| 1283 | if (field->type()->isArray()) |
| 1284 | out << arrayBrackets(*field->type()); |
| 1285 | out << ";\n"; |
| 1286 | } |
| 1287 | out << "}"; |
Zhenyao Mo | 904a916 | 2014-05-09 14:07:45 -0700 | [diff] [blame] | 1288 | } |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1289 | |