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