blob: cea2b4ad7d468d85fc50e24629f38da168c2ecc4 [file] [log] [blame]
zmo@google.com5601ea02011-06-10 18:23:25 +00001//
Nicolas Capens16004fc2014-06-11 11:29:11 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
zmo@google.com5601ea02011-06-10 18:23:25 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/OutputGLSLBase.h"
8#include "compiler/translator/compilerdebug.h"
zmo@google.com5601ea02011-06-10 18:23:25 +00009
daniel@transgaming.com773ff742013-01-11 04:12:51 +000010#include <cfloat>
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +000011
zmo@google.com5601ea02011-06-10 18:23:25 +000012namespace
13{
Zhenyao Mo9eedea02014-05-12 16:02:35 -070014TString arrayBrackets(const TType &type)
zmo@google.com5601ea02011-06-10 18:23:25 +000015{
16 ASSERT(type.isArray());
17 TInfoSinkBase out;
18 out << "[" << type.getArraySize() << "]";
19 return TString(out.c_str());
20}
21
Zhenyao Mo9eedea02014-05-12 16:02:35 -070022bool isSingleStatement(TIntermNode *node)
23{
24 if (const TIntermAggregate *aggregate = node->getAsAggregate())
zmo@google.com5601ea02011-06-10 18:23:25 +000025 {
26 return (aggregate->getOp() != EOpFunction) &&
27 (aggregate->getOp() != EOpSequence);
28 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -070029 else if (const TIntermSelection *selection = node->getAsSelectionNode())
zmo@google.com5601ea02011-06-10 18:23:25 +000030 {
31 // Ternary operators are usually part of an assignment operator.
32 // This handles those rare cases in which they are all by themselves.
33 return selection->usesTernaryOperator();
34 }
35 else if (node->getAsLoopNode())
36 {
37 return false;
38 }
Olli Etuaho01cd8af2015-02-20 10:39:20 +020039 else if (node->getAsSwitchNode())
40 {
41 return false;
42 }
43 else if (node->getAsCaseNode())
44 {
45 return false;
46 }
zmo@google.com5601ea02011-06-10 18:23:25 +000047 return true;
48}
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080049
zmo@google.com5601ea02011-06-10 18:23:25 +000050} // namespace
51
Zhenyao Mo9eedea02014-05-12 16:02:35 -070052TOutputGLSLBase::TOutputGLSLBase(TInfoSinkBase &objSink,
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +000053 ShArrayIndexClampingStrategy clampingStrategy,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000054 ShHashFunction64 hashFunction,
Zhenyao Mo9eedea02014-05-12 16:02:35 -070055 NameMap &nameMap,
56 TSymbolTable &symbolTable,
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080057 int shaderVersion,
58 ShShaderOutput output)
zmo@google.com5601ea02011-06-10 18:23:25 +000059 : TIntermTraverser(true, true, true),
60 mObjSink(objSink),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000061 mDeclaringVariables(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +000062 mClampingStrategy(clampingStrategy),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000063 mHashFunction(hashFunction),
64 mNameMap(nameMap),
Jamie Madill02f20dd2013-09-12 12:07:42 -040065 mSymbolTable(symbolTable),
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080066 mShaderVersion(shaderVersion),
67 mOutput(output)
zmo@google.com5601ea02011-06-10 18:23:25 +000068{
69}
70
Zhenyao Mo9eedea02014-05-12 16:02:35 -070071void TOutputGLSLBase::writeTriplet(
72 Visit visit, const char *preStr, const char *inStr, const char *postStr)
zmo@google.com5601ea02011-06-10 18:23:25 +000073{
Zhenyao Mo9eedea02014-05-12 16:02:35 -070074 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +000075 if (visit == PreVisit && preStr)
zmo@google.com5601ea02011-06-10 18:23:25 +000076 out << preStr;
zmo@google.com5601ea02011-06-10 18:23:25 +000077 else if (visit == InVisit && inStr)
zmo@google.com5601ea02011-06-10 18:23:25 +000078 out << inStr;
zmo@google.com5601ea02011-06-10 18:23:25 +000079 else if (visit == PostVisit && postStr)
zmo@google.com5601ea02011-06-10 18:23:25 +000080 out << postStr;
zmo@google.com5601ea02011-06-10 18:23:25 +000081}
82
Zhenyao Mo9eedea02014-05-12 16:02:35 -070083void TOutputGLSLBase::writeBuiltInFunctionTriplet(
84 Visit visit, const char *preStr, bool useEmulatedFunction)
zmo@google.com5601ea02011-06-10 18:23:25 +000085{
Zhenyao Mo9eedea02014-05-12 16:02:35 -070086 TString preString = useEmulatedFunction ?
87 BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr) : preStr;
88 writeTriplet(visit, preString.c_str(), ", ", ")");
89}
90
91void TOutputGLSLBase::writeVariableType(const TType &type)
92{
93 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +000094 TQualifier qualifier = type.getQualifier();
Jamie Madill3b5c2da2014-08-19 15:23:32 -040095 if (qualifier != EvqTemporary && qualifier != EvqGlobal)
Jamie Madill1c28e1f2014-08-04 11:37:54 -040096 {
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080097 if (mOutput == SH_GLSL_CORE_OUTPUT)
98 {
99 switch (qualifier)
100 {
101 case EvqAttribute:
102 out << "in" << " ";
103 break;
104 case EvqVaryingIn:
105 out << "in" << " ";
106 break;
107 case EvqVaryingOut:
108 out << "out" << " ";
109 break;
110 case EvqInvariantVaryingIn:
111 out << "invariant in" << " ";
112 break;
113 case EvqInvariantVaryingOut:
114 out << "invariant out" << " ";
115 break;
116 default:
117 out << type.getQualifierString() << " ";
118 break;
119 }
120 }
121 else
122 {
123 out << type.getQualifierString() << " ";
124 }
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400125 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000126 // Declare the struct if we have not done so already.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700127 if (type.getBasicType() == EbtStruct && !structDeclared(type.getStruct()))
zmo@google.com5601ea02011-06-10 18:23:25 +0000128 {
Jamie Madill01f85ac2014-06-06 11:55:04 -0400129 TStructure *structure = type.getStruct();
130
131 declareStruct(structure);
132
133 if (!structure->name().empty())
134 {
135 mDeclaredStructs.insert(structure->uniqueId());
136 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000137 }
138 else
139 {
140 if (writeVariablePrecision(type.getPrecision()))
141 out << " ";
142 out << getTypeName(type);
143 }
144}
145
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700146void TOutputGLSLBase::writeFunctionParameters(const TIntermSequence &args)
zmo@google.com5601ea02011-06-10 18:23:25 +0000147{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700148 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000149 for (TIntermSequence::const_iterator iter = args.begin();
150 iter != args.end(); ++iter)
151 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700152 const TIntermSymbol *arg = (*iter)->getAsSymbolNode();
zmo@google.com5601ea02011-06-10 18:23:25 +0000153 ASSERT(arg != NULL);
154
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700155 const TType &type = arg->getType();
zmo@google.com189be2f2011-06-16 18:28:53 +0000156 writeVariableType(type);
zmo@google.com5601ea02011-06-10 18:23:25 +0000157
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700158 const TString &name = arg->getSymbol();
zmo@google.com5601ea02011-06-10 18:23:25 +0000159 if (!name.empty())
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000160 out << " " << hashName(name);
zmo@google.com5601ea02011-06-10 18:23:25 +0000161 if (type.isArray())
162 out << arrayBrackets(type);
163
164 // Put a comma if this is not the last argument.
165 if (iter != args.end() - 1)
166 out << ", ";
167 }
168}
169
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700170const ConstantUnion *TOutputGLSLBase::writeConstantUnion(
171 const TType &type, const ConstantUnion *pConstUnion)
zmo@google.com5601ea02011-06-10 18:23:25 +0000172{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700173 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000174
175 if (type.getBasicType() == EbtStruct)
176 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700177 const TStructure *structure = type.getStruct();
Jamie Madill98493dd2013-07-08 14:39:03 -0400178 out << hashName(structure->name()) << "(";
179
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700180 const TFieldList &fields = structure->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -0400181 for (size_t i = 0; i < fields.size(); ++i)
zmo@google.com5601ea02011-06-10 18:23:25 +0000182 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700183 const TType *fieldType = fields[i]->type();
zmo@google.com5601ea02011-06-10 18:23:25 +0000184 ASSERT(fieldType != NULL);
185 pConstUnion = writeConstantUnion(*fieldType, pConstUnion);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700186 if (i != fields.size() - 1)
187 out << ", ";
zmo@google.com5601ea02011-06-10 18:23:25 +0000188 }
189 out << ")";
190 }
191 else
192 {
Jamie Madill94bf7f22013-07-08 13:31:15 -0400193 size_t size = type.getObjectSize();
zmo@google.com5601ea02011-06-10 18:23:25 +0000194 bool writeType = size > 1;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700195 if (writeType)
196 out << getTypeName(type) << "(";
Jamie Madill94bf7f22013-07-08 13:31:15 -0400197 for (size_t i = 0; i < size; ++i, ++pConstUnion)
zmo@google.com5601ea02011-06-10 18:23:25 +0000198 {
199 switch (pConstUnion->getType())
200 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700201 case EbtFloat:
202 out << std::min(FLT_MAX, std::max(-FLT_MAX, pConstUnion->getFConst()));
203 break;
204 case EbtInt:
205 out << pConstUnion->getIConst();
206 break;
207 case EbtBool:
208 out << pConstUnion->getBConst();
209 break;
210 default: UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000211 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700212 if (i != size - 1)
213 out << ", ";
zmo@google.com5601ea02011-06-10 18:23:25 +0000214 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700215 if (writeType)
216 out << ")";
zmo@google.com5601ea02011-06-10 18:23:25 +0000217 }
218 return pConstUnion;
219}
220
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700221void TOutputGLSLBase::visitSymbol(TIntermSymbol *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000222{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700223 TInfoSinkBase &out = objSink();
Zhenyao Mo550c6002014-02-26 15:40:48 -0800224 if (mLoopUnrollStack.needsToReplaceSymbolWithValue(node))
225 out << mLoopUnrollStack.getLoopIndexValue(node);
zmo@google.com5601ea02011-06-10 18:23:25 +0000226 else
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000227 out << hashVariableName(node->getSymbol());
zmo@google.com5601ea02011-06-10 18:23:25 +0000228
229 if (mDeclaringVariables && node->getType().isArray())
230 out << arrayBrackets(node->getType());
231}
232
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700233void TOutputGLSLBase::visitConstantUnion(TIntermConstantUnion *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000234{
235 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
236}
237
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700238bool TOutputGLSLBase::visitBinary(Visit visit, TIntermBinary *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000239{
240 bool visitChildren = true;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700241 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000242 switch (node->getOp())
243 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700244 case EOpInitialize:
245 if (visit == InVisit)
246 {
247 out << " = ";
248 // RHS of initialize is not being declared.
249 mDeclaringVariables = false;
250 }
251 break;
252 case EOpAssign:
253 writeTriplet(visit, "(", " = ", ")");
254 break;
255 case EOpAddAssign:
256 writeTriplet(visit, "(", " += ", ")");
257 break;
258 case EOpSubAssign:
259 writeTriplet(visit, "(", " -= ", ")");
260 break;
261 case EOpDivAssign:
262 writeTriplet(visit, "(", " /= ", ")");
263 break;
Olli Etuahoff805cc2015-02-13 10:59:34 +0200264 case EOpIModAssign:
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +0000265 writeTriplet(visit, "(", " %= ", ")");
266 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700267 // Notice the fall-through.
268 case EOpMulAssign:
269 case EOpVectorTimesMatrixAssign:
270 case EOpVectorTimesScalarAssign:
271 case EOpMatrixTimesScalarAssign:
272 case EOpMatrixTimesMatrixAssign:
273 writeTriplet(visit, "(", " *= ", ")");
274 break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200275 case EOpBitShiftLeftAssign:
276 writeTriplet(visit, "(", " <<= ", ")");
277 break;
278 case EOpBitShiftRightAssign:
279 writeTriplet(visit, "(", " >>= ", ")");
280 break;
281 case EOpBitwiseAndAssign:
282 writeTriplet(visit, "(", " &= ", ")");
283 break;
284 case EOpBitwiseXorAssign:
285 writeTriplet(visit, "(", " ^= ", ")");
286 break;
287 case EOpBitwiseOrAssign:
288 writeTriplet(visit, "(", " |= ", ")");
289 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700290
291 case EOpIndexDirect:
292 writeTriplet(visit, NULL, "[", "]");
293 break;
294 case EOpIndexIndirect:
295 if (node->getAddIndexClamp())
296 {
zmo@google.com5601ea02011-06-10 18:23:25 +0000297 if (visit == InVisit)
298 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700299 if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC)
300 out << "[int(clamp(float(";
301 else
302 out << "[webgl_int_clamp(";
zmo@google.com5601ea02011-06-10 18:23:25 +0000303 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700304 else if (visit == PostVisit)
305 {
306 int maxSize;
307 TIntermTyped *left = node->getLeft();
308 TType leftType = left->getType();
zmo@google.com5601ea02011-06-10 18:23:25 +0000309
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700310 if (left->isArray())
311 {
312 // The shader will fail validation if the array length is not > 0.
313 maxSize = leftType.getArraySize() - 1;
314 }
315 else
316 {
317 maxSize = leftType.getNominalSize() - 1;
318 }
319
320 if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC)
321 out << "), 0.0, float(" << maxSize << ")))]";
322 else
323 out << ", 0, " << maxSize << ")]";
324 }
325 }
326 else
327 {
zmo@google.com5601ea02011-06-10 18:23:25 +0000328 writeTriplet(visit, NULL, "[", "]");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700329 }
330 break;
331 case EOpIndexDirectStruct:
332 if (visit == InVisit)
333 {
334 // Here we are writing out "foo.bar", where "foo" is struct
335 // and "bar" is field. In AST, it is represented as a binary
336 // node, where left child represents "foo" and right child "bar".
337 // The node itself represents ".". The struct field "bar" is
338 // actually stored as an index into TStructure::fields.
339 out << ".";
340 const TStructure *structure = node->getLeft()->getType().getStruct();
341 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
342 const TField *field = structure->fields()[index->getIConst(0)];
343
344 TString fieldName = field->name();
345 if (!mSymbolTable.findBuiltIn(structure->name(), mShaderVersion))
346 fieldName = hashName(fieldName);
347
348 out << fieldName;
349 visitChildren = false;
350 }
351 break;
352 case EOpVectorSwizzle:
353 if (visit == InVisit)
354 {
355 out << ".";
356 TIntermAggregate *rightChild = node->getRight()->getAsAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700357 TIntermSequence *sequence = rightChild->getSequence();
358 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); ++sit)
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000359 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700360 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
361 ASSERT(element->getBasicType() == EbtInt);
362 ASSERT(element->getNominalSize() == 1);
363 const ConstantUnion& data = element->getUnionArrayPointer()[0];
364 ASSERT(data.getType() == EbtInt);
365 switch (data.getIConst())
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000366 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700367 case 0:
368 out << "x";
369 break;
370 case 1:
371 out << "y";
372 break;
373 case 2:
374 out << "z";
375 break;
376 case 3:
377 out << "w";
378 break;
379 default:
380 UNREACHABLE();
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000381 }
382 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700383 visitChildren = false;
384 }
385 break;
daniel@transgaming.com97b16d12013-02-01 03:20:42 +0000386
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700387 case EOpAdd:
388 writeTriplet(visit, "(", " + ", ")");
389 break;
390 case EOpSub:
391 writeTriplet(visit, "(", " - ", ")");
392 break;
393 case EOpMul:
394 writeTriplet(visit, "(", " * ", ")");
395 break;
396 case EOpDiv:
397 writeTriplet(visit, "(", " / ", ")");
398 break;
Olli Etuahoff805cc2015-02-13 10:59:34 +0200399 case EOpIMod:
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +0000400 writeTriplet(visit, "(", " % ", ")");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700401 break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200402 case EOpBitShiftLeft:
403 writeTriplet(visit, "(", " << ", ")");
404 break;
405 case EOpBitShiftRight:
406 writeTriplet(visit, "(", " >> ", ")");
407 break;
408 case EOpBitwiseAnd:
409 writeTriplet(visit, "(", " & ", ")");
410 break;
411 case EOpBitwiseXor:
412 writeTriplet(visit, "(", " ^ ", ")");
413 break;
414 case EOpBitwiseOr:
415 writeTriplet(visit, "(", " | ", ")");
416 break;
417
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700418 case EOpEqual:
419 writeTriplet(visit, "(", " == ", ")");
420 break;
421 case EOpNotEqual:
422 writeTriplet(visit, "(", " != ", ")");
423 break;
424 case EOpLessThan:
425 writeTriplet(visit, "(", " < ", ")");
426 break;
427 case EOpGreaterThan:
428 writeTriplet(visit, "(", " > ", ")");
429 break;
430 case EOpLessThanEqual:
431 writeTriplet(visit, "(", " <= ", ")");
432 break;
433 case EOpGreaterThanEqual:
434 writeTriplet(visit, "(", " >= ", ")");
435 break;
daniel@transgaming.com97b16d12013-02-01 03:20:42 +0000436
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700437 // Notice the fall-through.
438 case EOpVectorTimesScalar:
439 case EOpVectorTimesMatrix:
440 case EOpMatrixTimesVector:
441 case EOpMatrixTimesScalar:
442 case EOpMatrixTimesMatrix:
443 writeTriplet(visit, "(", " * ", ")");
444 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000445
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700446 case EOpLogicalOr:
447 writeTriplet(visit, "(", " || ", ")");
448 break;
449 case EOpLogicalXor:
450 writeTriplet(visit, "(", " ^^ ", ")");
451 break;
452 case EOpLogicalAnd:
453 writeTriplet(visit, "(", " && ", ")");
454 break;
455 default:
456 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000457 }
458
459 return visitChildren;
460}
461
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700462bool TOutputGLSLBase::visitUnary(Visit visit, TIntermUnary *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000463{
zmo@google.com32e97312011-08-24 01:03:11 +0000464 TString preString;
465 TString postString = ")";
466
zmo@google.com5601ea02011-06-10 18:23:25 +0000467 switch (node->getOp())
468 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700469 case EOpNegative: preString = "(-"; break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -0700470 case EOpPositive: preString = "(+"; break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700471 case EOpVectorLogicalNot: preString = "not("; break;
472 case EOpLogicalNot: preString = "(!"; break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200473 case EOpBitwiseNot: preString = "(~"; break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000474
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700475 case EOpPostIncrement: preString = "("; postString = "++)"; break;
476 case EOpPostDecrement: preString = "("; postString = "--)"; break;
477 case EOpPreIncrement: preString = "(++"; break;
478 case EOpPreDecrement: preString = "(--"; break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000479
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700480 case EOpRadians:
481 preString = "radians(";
482 break;
483 case EOpDegrees:
484 preString = "degrees(";
485 break;
486 case EOpSin:
487 preString = "sin(";
488 break;
489 case EOpCos:
490 preString = "cos(";
491 break;
492 case EOpTan:
493 preString = "tan(";
494 break;
495 case EOpAsin:
496 preString = "asin(";
497 break;
498 case EOpAcos:
499 preString = "acos(";
500 break;
501 case EOpAtan:
502 preString = "atan(";
503 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000504
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200505 case EOpSinh:
506 preString = "sinh(";
507 break;
508 case EOpCosh:
509 preString = "cosh(";
510 break;
511 case EOpTanh:
512 preString = "tanh(";
513 break;
514 case EOpAsinh:
515 preString = "asinh(";
516 break;
517 case EOpAcosh:
518 preString = "acosh(";
519 break;
520 case EOpAtanh:
521 preString = "atanh(";
522 break;
523
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700524 case EOpExp:
525 preString = "exp(";
526 break;
527 case EOpLog:
528 preString = "log(";
529 break;
530 case EOpExp2:
531 preString = "exp2(";
532 break;
533 case EOpLog2:
534 preString = "log2(";
535 break;
536 case EOpSqrt:
537 preString = "sqrt(";
538 break;
539 case EOpInverseSqrt:
540 preString = "inversesqrt(";
541 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000542
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700543 case EOpAbs:
544 preString = "abs(";
545 break;
546 case EOpSign:
547 preString = "sign(";
548 break;
549 case EOpFloor:
550 preString = "floor(";
551 break;
552 case EOpCeil:
553 preString = "ceil(";
554 break;
555 case EOpFract:
556 preString = "fract(";
557 break;
Arun Patole0c1726e2015-02-18 14:35:02 +0530558 case EOpIsNan:
559 preString = "isnan(";
560 break;
561 case EOpIsInf:
562 preString = "isinf(";
563 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000564
Olli Etuahoe8d2c072015-01-08 16:33:54 +0200565 case EOpFloatBitsToInt:
566 preString = "floatBitsToInt(";
567 break;
568 case EOpFloatBitsToUint:
569 preString = "floatBitsToUint(";
570 break;
571 case EOpIntBitsToFloat:
572 preString = "intBitsToFloat(";
573 break;
574 case EOpUintBitsToFloat:
575 preString = "uintBitsToFloat(";
576 break;
577
Olli Etuaho7700ff62015-01-15 12:16:29 +0200578 case EOpPackSnorm2x16:
579 preString = "packSnorm2x16(";
580 break;
581 case EOpPackUnorm2x16:
582 preString = "packUnorm2x16(";
583 break;
584 case EOpPackHalf2x16:
585 preString = "packHalf2x16(";
586 break;
587 case EOpUnpackSnorm2x16:
588 preString = "unpackSnorm2x16(";
589 break;
590 case EOpUnpackUnorm2x16:
591 preString = "unpackUnorm2x16(";
592 break;
593 case EOpUnpackHalf2x16:
594 preString = "unpackHalf2x16(";
595 break;
596
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700597 case EOpLength:
598 preString = "length(";
599 break;
600 case EOpNormalize:
601 preString = "normalize(";
602 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000603
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700604 case EOpDFdx:
605 preString = "dFdx(";
606 break;
607 case EOpDFdy:
608 preString = "dFdy(";
609 break;
610 case EOpFwidth:
611 preString = "fwidth(";
612 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000613
Olli Etuahoe39706d2014-12-30 16:40:36 +0200614 case EOpTranspose:
615 preString = "transpose(";
616 break;
617 case EOpDeterminant:
618 preString = "determinant(";
619 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +0200620 case EOpInverse:
621 preString = "inverse(";
622 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +0200623
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700624 case EOpAny:
625 preString = "any(";
626 break;
627 case EOpAll:
628 preString = "all(";
629 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000630
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700631 default:
632 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000633 }
634
zmo@google.com32e97312011-08-24 01:03:11 +0000635 if (visit == PreVisit && node->getUseEmulatedFunction())
636 preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preString);
637 writeTriplet(visit, preString.c_str(), NULL, postString.c_str());
638
zmo@google.com5601ea02011-06-10 18:23:25 +0000639 return true;
640}
641
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700642bool TOutputGLSLBase::visitSelection(Visit visit, TIntermSelection *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000643{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700644 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000645
646 if (node->usesTernaryOperator())
647 {
648 // Notice two brackets at the beginning and end. The outer ones
649 // encapsulate the whole ternary expression. This preserves the
650 // order of precedence when ternary expressions are used in a
651 // compound expression, i.e., c = 2 * (a < b ? 1 : 2).
652 out << "((";
653 node->getCondition()->traverse(this);
654 out << ") ? (";
655 node->getTrueBlock()->traverse(this);
656 out << ") : (";
657 node->getFalseBlock()->traverse(this);
658 out << "))";
659 }
660 else
661 {
662 out << "if (";
663 node->getCondition()->traverse(this);
664 out << ")\n";
665
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700666 incrementDepth(node);
zmo@google.com5601ea02011-06-10 18:23:25 +0000667 visitCodeBlock(node->getTrueBlock());
668
669 if (node->getFalseBlock())
670 {
671 out << "else\n";
672 visitCodeBlock(node->getFalseBlock());
673 }
674 decrementDepth();
675 }
676 return false;
677}
678
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200679bool TOutputGLSLBase::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200680{
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200681 if (node->getStatementList())
682 {
683 writeTriplet(visit, "switch (", ") ", nullptr);
684 // The curly braces get written when visiting the statementList aggregate
685 }
686 else
687 {
688 // No statementList, so it won't output curly braces
689 writeTriplet(visit, "switch (", ") {", "}\n");
690 }
691 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200692}
693
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200694bool TOutputGLSLBase::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200695{
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200696 if (node->hasCondition())
697 {
698 writeTriplet(visit, "case (", nullptr, "):\n");
699 return true;
700 }
701 else
702 {
703 TInfoSinkBase &out = objSink();
704 out << "default:\n";
705 return false;
706 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200707}
708
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700709bool TOutputGLSLBase::visitAggregate(Visit visit, TIntermAggregate *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000710{
711 bool visitChildren = true;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700712 TInfoSinkBase &out = objSink();
zmo@google.comf420c422011-09-12 18:27:59 +0000713 TString preString;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700714 bool useEmulatedFunction = (visit == PreVisit && node->getUseEmulatedFunction());
zmo@google.com5601ea02011-06-10 18:23:25 +0000715 switch (node->getOp())
716 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700717 case EOpSequence:
718 // Scope the sequences except when at the global scope.
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700719 if (mDepth > 0)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700720 {
721 out << "{\n";
zmo@google.com5601ea02011-06-10 18:23:25 +0000722 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000723
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700724 incrementDepth(node);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700725 for (TIntermSequence::const_iterator iter = node->getSequence()->begin();
726 iter != node->getSequence()->end(); ++iter)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700727 {
Austin Kinross3ae64652015-01-26 15:51:39 -0800728 TIntermNode *curNode = *iter;
729 ASSERT(curNode != NULL);
730 curNode->traverse(this);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700731
Austin Kinross3ae64652015-01-26 15:51:39 -0800732 if (isSingleStatement(curNode))
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700733 out << ";\n";
734 }
735 decrementDepth();
736
737 // Scope the sequences except when at the global scope.
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700738 if (mDepth > 0)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700739 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700740 out << "}\n";
741 }
742 visitChildren = false;
743 break;
744 case EOpPrototype:
745 // Function declaration.
746 ASSERT(visit == PreVisit);
747 writeVariableType(node->getType());
Olli Etuaho76acee82014-11-04 13:44:03 +0200748 out << " " << hashFunctionName(node->getName());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700749
750 out << "(";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700751 writeFunctionParameters(*(node->getSequence()));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700752 out << ")";
753
754 visitChildren = false;
755 break;
756 case EOpFunction: {
757 // Function definition.
758 ASSERT(visit == PreVisit);
759 writeVariableType(node->getType());
760 out << " " << hashFunctionName(node->getName());
761
762 incrementDepth(node);
763 // Function definition node contains one or two children nodes
764 // representing function parameters and function body. The latter
765 // is not present in case of empty function bodies.
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700766 const TIntermSequence &sequence = *(node->getSequence());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700767 ASSERT((sequence.size() == 1) || (sequence.size() == 2));
768 TIntermSequence::const_iterator seqIter = sequence.begin();
769
770 // Traverse function parameters.
771 TIntermAggregate *params = (*seqIter)->getAsAggregate();
772 ASSERT(params != NULL);
773 ASSERT(params->getOp() == EOpParameters);
774 params->traverse(this);
775
776 // Traverse function body.
777 TIntermAggregate *body = ++seqIter != sequence.end() ?
778 (*seqIter)->getAsAggregate() : NULL;
779 visitCodeBlock(body);
780 decrementDepth();
781
782 // Fully processed; no need to visit children.
783 visitChildren = false;
784 break;
785 }
786 case EOpFunctionCall:
787 // Function call.
788 if (visit == PreVisit)
789 out << hashFunctionName(node->getName()) << "(";
790 else if (visit == InVisit)
791 out << ", ";
792 else
zmo@google.com5601ea02011-06-10 18:23:25 +0000793 out << ")";
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700794 break;
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200795 case EOpInternalFunctionCall:
796 // Function call to an internal helper function.
797 if (visit == PreVisit)
798 out << node->getName() << "(";
799 else if (visit == InVisit)
800 out << ", ";
801 else
802 out << ")";
803 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700804 case EOpParameters:
805 // Function parameters.
806 ASSERT(visit == PreVisit);
807 out << "(";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700808 writeFunctionParameters(*(node->getSequence()));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700809 out << ")";
810 visitChildren = false;
811 break;
812 case EOpDeclaration:
813 // Variable declaration.
814 if (visit == PreVisit)
815 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700816 const TIntermSequence &sequence = *(node->getSequence());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700817 const TIntermTyped *variable = sequence.front()->getAsTyped();
818 writeVariableType(variable->getType());
819 out << " ";
820 mDeclaringVariables = true;
zmo@google.com5601ea02011-06-10 18:23:25 +0000821 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700822 else if (visit == InVisit)
823 {
824 out << ", ";
825 mDeclaringVariables = true;
zmo@google.com5601ea02011-06-10 18:23:25 +0000826 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700827 else
828 {
829 mDeclaringVariables = false;
830 }
831 break;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700832 case EOpInvariantDeclaration:
833 // Invariant declaration.
834 ASSERT(visit == PreVisit);
835 {
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400836 const TIntermSequence *sequence = node->getSequence();
837 ASSERT(sequence && sequence->size() == 1);
838 const TIntermSymbol *symbol = sequence->front()->getAsSymbolNode();
839 ASSERT(symbol);
Zhenyao Mo2a517272014-10-27 16:09:57 -0700840 out << "invariant " << hashVariableName(symbol->getSymbol());
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400841 }
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700842 visitChildren = false;
843 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700844 case EOpConstructFloat:
845 writeTriplet(visit, "float(", NULL, ")");
846 break;
847 case EOpConstructVec2:
848 writeBuiltInFunctionTriplet(visit, "vec2(", false);
849 break;
850 case EOpConstructVec3:
851 writeBuiltInFunctionTriplet(visit, "vec3(", false);
852 break;
853 case EOpConstructVec4:
854 writeBuiltInFunctionTriplet(visit, "vec4(", false);
855 break;
856 case EOpConstructBool:
857 writeTriplet(visit, "bool(", NULL, ")");
858 break;
859 case EOpConstructBVec2:
860 writeBuiltInFunctionTriplet(visit, "bvec2(", false);
861 break;
862 case EOpConstructBVec3:
863 writeBuiltInFunctionTriplet(visit, "bvec3(", false);
864 break;
865 case EOpConstructBVec4:
866 writeBuiltInFunctionTriplet(visit, "bvec4(", false);
867 break;
868 case EOpConstructInt:
869 writeTriplet(visit, "int(", NULL, ")");
870 break;
871 case EOpConstructIVec2:
872 writeBuiltInFunctionTriplet(visit, "ivec2(", false);
873 break;
874 case EOpConstructIVec3:
875 writeBuiltInFunctionTriplet(visit, "ivec3(", false);
876 break;
877 case EOpConstructIVec4:
878 writeBuiltInFunctionTriplet(visit, "ivec4(", false);
879 break;
880 case EOpConstructMat2:
881 writeBuiltInFunctionTriplet(visit, "mat2(", false);
882 break;
883 case EOpConstructMat3:
884 writeBuiltInFunctionTriplet(visit, "mat3(", false);
885 break;
886 case EOpConstructMat4:
887 writeBuiltInFunctionTriplet(visit, "mat4(", false);
888 break;
889 case EOpConstructStruct:
890 if (visit == PreVisit)
891 {
892 const TType &type = node->getType();
893 ASSERT(type.getBasicType() == EbtStruct);
894 out << hashName(type.getStruct()->name()) << "(";
895 }
896 else if (visit == InVisit)
897 {
898 out << ", ";
899 }
900 else
901 {
zmo@google.com5601ea02011-06-10 18:23:25 +0000902 out << ")";
zmo@google.com5601ea02011-06-10 18:23:25 +0000903 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700904 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000905
Olli Etuahoe39706d2014-12-30 16:40:36 +0200906 case EOpOuterProduct:
907 writeBuiltInFunctionTriplet(visit, "outerProduct(", useEmulatedFunction);
908 break;
909
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700910 case EOpLessThan:
911 writeBuiltInFunctionTriplet(visit, "lessThan(", useEmulatedFunction);
912 break;
913 case EOpGreaterThan:
914 writeBuiltInFunctionTriplet(visit, "greaterThan(", useEmulatedFunction);
915 break;
916 case EOpLessThanEqual:
917 writeBuiltInFunctionTriplet(visit, "lessThanEqual(", useEmulatedFunction);
918 break;
919 case EOpGreaterThanEqual:
920 writeBuiltInFunctionTriplet(visit, "greaterThanEqual(", useEmulatedFunction);
921 break;
922 case EOpVectorEqual:
923 writeBuiltInFunctionTriplet(visit, "equal(", useEmulatedFunction);
924 break;
925 case EOpVectorNotEqual:
926 writeBuiltInFunctionTriplet(visit, "notEqual(", useEmulatedFunction);
927 break;
928 case EOpComma:
Zhenyao Mo0783efd2014-10-21 15:51:59 -0700929 writeTriplet(visit, "(", ", ", ")");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700930 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000931
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700932 case EOpMod:
933 writeBuiltInFunctionTriplet(visit, "mod(", useEmulatedFunction);
934 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +0200935 case EOpModf:
936 writeBuiltInFunctionTriplet(visit, "modf(", useEmulatedFunction);
937 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700938 case EOpPow:
939 writeBuiltInFunctionTriplet(visit, "pow(", useEmulatedFunction);
940 break;
941 case EOpAtan:
942 writeBuiltInFunctionTriplet(visit, "atan(", useEmulatedFunction);
943 break;
944 case EOpMin:
945 writeBuiltInFunctionTriplet(visit, "min(", useEmulatedFunction);
946 break;
947 case EOpMax:
948 writeBuiltInFunctionTriplet(visit, "max(", useEmulatedFunction);
949 break;
950 case EOpClamp:
951 writeBuiltInFunctionTriplet(visit, "clamp(", useEmulatedFunction);
952 break;
953 case EOpMix:
954 writeBuiltInFunctionTriplet(visit, "mix(", useEmulatedFunction);
955 break;
956 case EOpStep:
957 writeBuiltInFunctionTriplet(visit, "step(", useEmulatedFunction);
958 break;
959 case EOpSmoothStep:
960 writeBuiltInFunctionTriplet(visit, "smoothstep(", useEmulatedFunction);
961 break;
962 case EOpDistance:
963 writeBuiltInFunctionTriplet(visit, "distance(", useEmulatedFunction);
964 break;
965 case EOpDot:
966 writeBuiltInFunctionTriplet(visit, "dot(", useEmulatedFunction);
967 break;
968 case EOpCross:
969 writeBuiltInFunctionTriplet(visit, "cross(", useEmulatedFunction);
970 break;
971 case EOpFaceForward:
972 writeBuiltInFunctionTriplet(visit, "faceforward(", useEmulatedFunction);
973 break;
974 case EOpReflect:
975 writeBuiltInFunctionTriplet(visit, "reflect(", useEmulatedFunction);
976 break;
977 case EOpRefract:
978 writeBuiltInFunctionTriplet(visit, "refract(", useEmulatedFunction);
979 break;
980 case EOpMul:
981 writeBuiltInFunctionTriplet(visit, "matrixCompMult(", useEmulatedFunction);
982 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000983
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700984 default:
985 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000986 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000987 return visitChildren;
988}
989
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700990bool TOutputGLSLBase::visitLoop(Visit visit, TIntermLoop *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000991{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700992 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000993
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700994 incrementDepth(node);
zmo@google.com5601ea02011-06-10 18:23:25 +0000995 // Loop header.
996 TLoopType loopType = node->getType();
997 if (loopType == ELoopFor) // for loop
998 {
Zhenyao Mo550c6002014-02-26 15:40:48 -0800999 if (!node->getUnrollFlag())
1000 {
zmo@google.com5601ea02011-06-10 18:23:25 +00001001 out << "for (";
1002 if (node->getInit())
1003 node->getInit()->traverse(this);
1004 out << "; ";
1005
1006 if (node->getCondition())
1007 node->getCondition()->traverse(this);
1008 out << "; ";
1009
1010 if (node->getExpression())
1011 node->getExpression()->traverse(this);
1012 out << ")\n";
1013 }
Zhenyao Mo550c6002014-02-26 15:40:48 -08001014 else
1015 {
1016 // Need to put a one-iteration loop here to handle break.
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001017 TIntermSequence *declSeq =
Zhenyao Mo550c6002014-02-26 15:40:48 -08001018 node->getInit()->getAsAggregate()->getSequence();
1019 TIntermSymbol *indexSymbol =
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001020 (*declSeq)[0]->getAsBinaryNode()->getLeft()->getAsSymbolNode();
Zhenyao Mo550c6002014-02-26 15:40:48 -08001021 TString name = hashVariableName(indexSymbol->getSymbol());
1022 out << "for (int " << name << " = 0; "
1023 << name << " < 1; "
1024 << "++" << name << ")\n";
1025 }
zmo@google.com5601ea02011-06-10 18:23:25 +00001026 }
1027 else if (loopType == ELoopWhile) // while loop
1028 {
1029 out << "while (";
1030 ASSERT(node->getCondition() != NULL);
1031 node->getCondition()->traverse(this);
1032 out << ")\n";
1033 }
1034 else // do-while loop
1035 {
1036 ASSERT(loopType == ELoopDoWhile);
1037 out << "do\n";
1038 }
1039
1040 // Loop body.
1041 if (node->getUnrollFlag())
1042 {
Zhenyao Mo550c6002014-02-26 15:40:48 -08001043 out << "{\n";
1044 mLoopUnrollStack.push(node);
1045 while (mLoopUnrollStack.satisfiesLoopCondition())
zmo@google.com5601ea02011-06-10 18:23:25 +00001046 {
1047 visitCodeBlock(node->getBody());
Zhenyao Mo550c6002014-02-26 15:40:48 -08001048 mLoopUnrollStack.step();
zmo@google.com5601ea02011-06-10 18:23:25 +00001049 }
Zhenyao Mo550c6002014-02-26 15:40:48 -08001050 mLoopUnrollStack.pop();
1051 out << "}\n";
zmo@google.com5601ea02011-06-10 18:23:25 +00001052 }
1053 else
1054 {
1055 visitCodeBlock(node->getBody());
1056 }
1057
1058 // Loop footer.
1059 if (loopType == ELoopDoWhile) // do-while loop
1060 {
1061 out << "while (";
1062 ASSERT(node->getCondition() != NULL);
1063 node->getCondition()->traverse(this);
1064 out << ");\n";
1065 }
1066 decrementDepth();
1067
1068 // No need to visit children. They have been already processed in
1069 // this function.
1070 return false;
1071}
1072
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001073bool TOutputGLSLBase::visitBranch(Visit visit, TIntermBranch *node)
zmo@google.com5601ea02011-06-10 18:23:25 +00001074{
1075 switch (node->getFlowOp())
1076 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001077 case EOpKill:
1078 writeTriplet(visit, "discard", NULL, NULL);
1079 break;
1080 case EOpBreak:
1081 writeTriplet(visit, "break", NULL, NULL);
1082 break;
1083 case EOpContinue:
1084 writeTriplet(visit, "continue", NULL, NULL);
1085 break;
1086 case EOpReturn:
1087 writeTriplet(visit, "return ", NULL, NULL);
1088 break;
1089 default:
1090 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +00001091 }
1092
1093 return true;
1094}
1095
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001096void TOutputGLSLBase::visitCodeBlock(TIntermNode *node)
1097{
zmo@google.com5601ea02011-06-10 18:23:25 +00001098 TInfoSinkBase &out = objSink();
1099 if (node != NULL)
1100 {
1101 node->traverse(this);
1102 // Single statements not part of a sequence need to be terminated
1103 // with semi-colon.
1104 if (isSingleStatement(node))
1105 out << ";\n";
1106 }
1107 else
1108 {
1109 out << "{\n}\n"; // Empty code block.
1110 }
1111}
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001112
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001113TString TOutputGLSLBase::getTypeName(const TType &type)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001114{
1115 TInfoSinkBase out;
1116 if (type.isMatrix())
1117 {
1118 out << "mat";
1119 out << type.getNominalSize();
1120 }
1121 else if (type.isVector())
1122 {
1123 switch (type.getBasicType())
1124 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001125 case EbtFloat:
1126 out << "vec";
1127 break;
1128 case EbtInt:
1129 out << "ivec";
1130 break;
1131 case EbtBool:
1132 out << "bvec";
1133 break;
1134 default:
1135 UNREACHABLE();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001136 }
1137 out << type.getNominalSize();
1138 }
1139 else
1140 {
1141 if (type.getBasicType() == EbtStruct)
Jamie Madill98493dd2013-07-08 14:39:03 -04001142 out << hashName(type.getStruct()->name());
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001143 else
1144 out << type.getBasicString();
1145 }
1146 return TString(out.c_str());
1147}
1148
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001149TString TOutputGLSLBase::hashName(const TString &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001150{
1151 if (mHashFunction == NULL || name.empty())
1152 return name;
1153 NameMap::const_iterator it = mNameMap.find(name.c_str());
1154 if (it != mNameMap.end())
1155 return it->second.c_str();
1156 TString hashedName = TIntermTraverser::hash(name, mHashFunction);
1157 mNameMap[name.c_str()] = hashedName.c_str();
1158 return hashedName;
1159}
1160
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001161TString TOutputGLSLBase::hashVariableName(const TString &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001162{
Jamie Madill02f20dd2013-09-12 12:07:42 -04001163 if (mSymbolTable.findBuiltIn(name, mShaderVersion) != NULL)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001164 return name;
1165 return hashName(name);
1166}
1167
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001168TString TOutputGLSLBase::hashFunctionName(const TString &mangled_name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001169{
1170 TString name = TFunction::unmangleName(mangled_name);
Jamie Madill02f20dd2013-09-12 12:07:42 -04001171 if (mSymbolTable.findBuiltIn(mangled_name, mShaderVersion) != NULL || name == "main")
Nicolas Capens46485082014-04-15 13:12:50 -04001172 return translateTextureFunction(name);
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001173 return hashName(name);
1174}
Jamie Madill98493dd2013-07-08 14:39:03 -04001175
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001176bool TOutputGLSLBase::structDeclared(const TStructure *structure) const
Jamie Madill98493dd2013-07-08 14:39:03 -04001177{
Zhenyao Mo904a9162014-05-09 14:07:45 -07001178 ASSERT(structure);
Jamie Madill01f85ac2014-06-06 11:55:04 -04001179 if (structure->name().empty())
Zhenyao Mo904a9162014-05-09 14:07:45 -07001180 {
Jamie Madill01f85ac2014-06-06 11:55:04 -04001181 return false;
Zhenyao Mo904a9162014-05-09 14:07:45 -07001182 }
Jamie Madill01f85ac2014-06-06 11:55:04 -04001183
1184 return (mDeclaredStructs.count(structure->uniqueId()) > 0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001185}
1186
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001187void TOutputGLSLBase::declareStruct(const TStructure *structure)
Jamie Madill98493dd2013-07-08 14:39:03 -04001188{
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001189 TInfoSinkBase &out = objSink();
Jamie Madill98493dd2013-07-08 14:39:03 -04001190
1191 out << "struct " << hashName(structure->name()) << "{\n";
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001192 const TFieldList &fields = structure->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04001193 for (size_t i = 0; i < fields.size(); ++i)
1194 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001195 const TField *field = fields[i];
Jamie Madill98493dd2013-07-08 14:39:03 -04001196 if (writeVariablePrecision(field->type()->getPrecision()))
1197 out << " ";
1198 out << getTypeName(*field->type()) << " " << hashName(field->name());
1199 if (field->type()->isArray())
1200 out << arrayBrackets(*field->type());
1201 out << ";\n";
1202 }
1203 out << "}";
Zhenyao Mo904a9162014-05-09 14:07:45 -07001204}
Jamie Madill98493dd2013-07-08 14:39:03 -04001205