blob: 4827ee1440900535836cbdcbd7222994831673ca [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;
Qingqing Deng5dbece52015-02-27 20:35:38 -0800552 case EOpTrunc:
553 preString = "trunc(";
554 break;
555 case EOpRound:
556 preString = "round(";
557 break;
558 case EOpRoundEven:
559 preString = "roundEven(";
560 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700561 case EOpCeil:
562 preString = "ceil(";
563 break;
564 case EOpFract:
565 preString = "fract(";
566 break;
Arun Patole0c1726e2015-02-18 14:35:02 +0530567 case EOpIsNan:
568 preString = "isnan(";
569 break;
570 case EOpIsInf:
571 preString = "isinf(";
572 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000573
Olli Etuahoe8d2c072015-01-08 16:33:54 +0200574 case EOpFloatBitsToInt:
575 preString = "floatBitsToInt(";
576 break;
577 case EOpFloatBitsToUint:
578 preString = "floatBitsToUint(";
579 break;
580 case EOpIntBitsToFloat:
581 preString = "intBitsToFloat(";
582 break;
583 case EOpUintBitsToFloat:
584 preString = "uintBitsToFloat(";
585 break;
586
Olli Etuaho7700ff62015-01-15 12:16:29 +0200587 case EOpPackSnorm2x16:
588 preString = "packSnorm2x16(";
589 break;
590 case EOpPackUnorm2x16:
591 preString = "packUnorm2x16(";
592 break;
593 case EOpPackHalf2x16:
594 preString = "packHalf2x16(";
595 break;
596 case EOpUnpackSnorm2x16:
597 preString = "unpackSnorm2x16(";
598 break;
599 case EOpUnpackUnorm2x16:
600 preString = "unpackUnorm2x16(";
601 break;
602 case EOpUnpackHalf2x16:
603 preString = "unpackHalf2x16(";
604 break;
605
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700606 case EOpLength:
607 preString = "length(";
608 break;
609 case EOpNormalize:
610 preString = "normalize(";
611 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000612
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700613 case EOpDFdx:
614 preString = "dFdx(";
615 break;
616 case EOpDFdy:
617 preString = "dFdy(";
618 break;
619 case EOpFwidth:
620 preString = "fwidth(";
621 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000622
Olli Etuahoe39706d2014-12-30 16:40:36 +0200623 case EOpTranspose:
624 preString = "transpose(";
625 break;
626 case EOpDeterminant:
627 preString = "determinant(";
628 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +0200629 case EOpInverse:
630 preString = "inverse(";
631 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +0200632
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700633 case EOpAny:
634 preString = "any(";
635 break;
636 case EOpAll:
637 preString = "all(";
638 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000639
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700640 default:
641 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000642 }
643
zmo@google.com32e97312011-08-24 01:03:11 +0000644 if (visit == PreVisit && node->getUseEmulatedFunction())
645 preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preString);
646 writeTriplet(visit, preString.c_str(), NULL, postString.c_str());
647
zmo@google.com5601ea02011-06-10 18:23:25 +0000648 return true;
649}
650
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700651bool TOutputGLSLBase::visitSelection(Visit visit, TIntermSelection *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000652{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700653 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000654
655 if (node->usesTernaryOperator())
656 {
657 // Notice two brackets at the beginning and end. The outer ones
658 // encapsulate the whole ternary expression. This preserves the
659 // order of precedence when ternary expressions are used in a
660 // compound expression, i.e., c = 2 * (a < b ? 1 : 2).
661 out << "((";
662 node->getCondition()->traverse(this);
663 out << ") ? (";
664 node->getTrueBlock()->traverse(this);
665 out << ") : (";
666 node->getFalseBlock()->traverse(this);
667 out << "))";
668 }
669 else
670 {
671 out << "if (";
672 node->getCondition()->traverse(this);
673 out << ")\n";
674
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700675 incrementDepth(node);
zmo@google.com5601ea02011-06-10 18:23:25 +0000676 visitCodeBlock(node->getTrueBlock());
677
678 if (node->getFalseBlock())
679 {
680 out << "else\n";
681 visitCodeBlock(node->getFalseBlock());
682 }
683 decrementDepth();
684 }
685 return false;
686}
687
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200688bool TOutputGLSLBase::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200689{
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200690 if (node->getStatementList())
691 {
692 writeTriplet(visit, "switch (", ") ", nullptr);
693 // The curly braces get written when visiting the statementList aggregate
694 }
695 else
696 {
697 // No statementList, so it won't output curly braces
698 writeTriplet(visit, "switch (", ") {", "}\n");
699 }
700 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200701}
702
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200703bool TOutputGLSLBase::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200704{
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200705 if (node->hasCondition())
706 {
707 writeTriplet(visit, "case (", nullptr, "):\n");
708 return true;
709 }
710 else
711 {
712 TInfoSinkBase &out = objSink();
713 out << "default:\n";
714 return false;
715 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200716}
717
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700718bool TOutputGLSLBase::visitAggregate(Visit visit, TIntermAggregate *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000719{
720 bool visitChildren = true;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700721 TInfoSinkBase &out = objSink();
zmo@google.comf420c422011-09-12 18:27:59 +0000722 TString preString;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700723 bool useEmulatedFunction = (visit == PreVisit && node->getUseEmulatedFunction());
zmo@google.com5601ea02011-06-10 18:23:25 +0000724 switch (node->getOp())
725 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700726 case EOpSequence:
727 // Scope the sequences except when at the global scope.
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700728 if (mDepth > 0)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700729 {
730 out << "{\n";
zmo@google.com5601ea02011-06-10 18:23:25 +0000731 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000732
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700733 incrementDepth(node);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700734 for (TIntermSequence::const_iterator iter = node->getSequence()->begin();
735 iter != node->getSequence()->end(); ++iter)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700736 {
Austin Kinross3ae64652015-01-26 15:51:39 -0800737 TIntermNode *curNode = *iter;
738 ASSERT(curNode != NULL);
739 curNode->traverse(this);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700740
Austin Kinross3ae64652015-01-26 15:51:39 -0800741 if (isSingleStatement(curNode))
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700742 out << ";\n";
743 }
744 decrementDepth();
745
746 // Scope the sequences except when at the global scope.
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700747 if (mDepth > 0)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700748 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700749 out << "}\n";
750 }
751 visitChildren = false;
752 break;
753 case EOpPrototype:
754 // Function declaration.
755 ASSERT(visit == PreVisit);
756 writeVariableType(node->getType());
Olli Etuaho76acee82014-11-04 13:44:03 +0200757 out << " " << hashFunctionName(node->getName());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700758
759 out << "(";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700760 writeFunctionParameters(*(node->getSequence()));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700761 out << ")";
762
763 visitChildren = false;
764 break;
765 case EOpFunction: {
766 // Function definition.
767 ASSERT(visit == PreVisit);
768 writeVariableType(node->getType());
769 out << " " << hashFunctionName(node->getName());
770
771 incrementDepth(node);
772 // Function definition node contains one or two children nodes
773 // representing function parameters and function body. The latter
774 // is not present in case of empty function bodies.
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700775 const TIntermSequence &sequence = *(node->getSequence());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700776 ASSERT((sequence.size() == 1) || (sequence.size() == 2));
777 TIntermSequence::const_iterator seqIter = sequence.begin();
778
779 // Traverse function parameters.
780 TIntermAggregate *params = (*seqIter)->getAsAggregate();
781 ASSERT(params != NULL);
782 ASSERT(params->getOp() == EOpParameters);
783 params->traverse(this);
784
785 // Traverse function body.
786 TIntermAggregate *body = ++seqIter != sequence.end() ?
787 (*seqIter)->getAsAggregate() : NULL;
788 visitCodeBlock(body);
789 decrementDepth();
790
791 // Fully processed; no need to visit children.
792 visitChildren = false;
793 break;
794 }
795 case EOpFunctionCall:
796 // Function call.
797 if (visit == PreVisit)
798 out << hashFunctionName(node->getName()) << "(";
799 else if (visit == InVisit)
800 out << ", ";
801 else
zmo@google.com5601ea02011-06-10 18:23:25 +0000802 out << ")";
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700803 break;
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200804 case EOpInternalFunctionCall:
805 // Function call to an internal helper function.
806 if (visit == PreVisit)
807 out << node->getName() << "(";
808 else if (visit == InVisit)
809 out << ", ";
810 else
811 out << ")";
812 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700813 case EOpParameters:
814 // Function parameters.
815 ASSERT(visit == PreVisit);
816 out << "(";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700817 writeFunctionParameters(*(node->getSequence()));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700818 out << ")";
819 visitChildren = false;
820 break;
821 case EOpDeclaration:
822 // Variable declaration.
823 if (visit == PreVisit)
824 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700825 const TIntermSequence &sequence = *(node->getSequence());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700826 const TIntermTyped *variable = sequence.front()->getAsTyped();
827 writeVariableType(variable->getType());
828 out << " ";
829 mDeclaringVariables = true;
zmo@google.com5601ea02011-06-10 18:23:25 +0000830 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700831 else if (visit == InVisit)
832 {
833 out << ", ";
834 mDeclaringVariables = true;
zmo@google.com5601ea02011-06-10 18:23:25 +0000835 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700836 else
837 {
838 mDeclaringVariables = false;
839 }
840 break;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700841 case EOpInvariantDeclaration:
842 // Invariant declaration.
843 ASSERT(visit == PreVisit);
844 {
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400845 const TIntermSequence *sequence = node->getSequence();
846 ASSERT(sequence && sequence->size() == 1);
847 const TIntermSymbol *symbol = sequence->front()->getAsSymbolNode();
848 ASSERT(symbol);
Zhenyao Mo2a517272014-10-27 16:09:57 -0700849 out << "invariant " << hashVariableName(symbol->getSymbol());
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400850 }
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700851 visitChildren = false;
852 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700853 case EOpConstructFloat:
854 writeTriplet(visit, "float(", NULL, ")");
855 break;
856 case EOpConstructVec2:
857 writeBuiltInFunctionTriplet(visit, "vec2(", false);
858 break;
859 case EOpConstructVec3:
860 writeBuiltInFunctionTriplet(visit, "vec3(", false);
861 break;
862 case EOpConstructVec4:
863 writeBuiltInFunctionTriplet(visit, "vec4(", false);
864 break;
865 case EOpConstructBool:
866 writeTriplet(visit, "bool(", NULL, ")");
867 break;
868 case EOpConstructBVec2:
869 writeBuiltInFunctionTriplet(visit, "bvec2(", false);
870 break;
871 case EOpConstructBVec3:
872 writeBuiltInFunctionTriplet(visit, "bvec3(", false);
873 break;
874 case EOpConstructBVec4:
875 writeBuiltInFunctionTriplet(visit, "bvec4(", false);
876 break;
877 case EOpConstructInt:
878 writeTriplet(visit, "int(", NULL, ")");
879 break;
880 case EOpConstructIVec2:
881 writeBuiltInFunctionTriplet(visit, "ivec2(", false);
882 break;
883 case EOpConstructIVec3:
884 writeBuiltInFunctionTriplet(visit, "ivec3(", false);
885 break;
886 case EOpConstructIVec4:
887 writeBuiltInFunctionTriplet(visit, "ivec4(", false);
888 break;
889 case EOpConstructMat2:
890 writeBuiltInFunctionTriplet(visit, "mat2(", false);
891 break;
892 case EOpConstructMat3:
893 writeBuiltInFunctionTriplet(visit, "mat3(", false);
894 break;
895 case EOpConstructMat4:
896 writeBuiltInFunctionTriplet(visit, "mat4(", false);
897 break;
898 case EOpConstructStruct:
899 if (visit == PreVisit)
900 {
901 const TType &type = node->getType();
902 ASSERT(type.getBasicType() == EbtStruct);
903 out << hashName(type.getStruct()->name()) << "(";
904 }
905 else if (visit == InVisit)
906 {
907 out << ", ";
908 }
909 else
910 {
zmo@google.com5601ea02011-06-10 18:23:25 +0000911 out << ")";
zmo@google.com5601ea02011-06-10 18:23:25 +0000912 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700913 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000914
Olli Etuahoe39706d2014-12-30 16:40:36 +0200915 case EOpOuterProduct:
916 writeBuiltInFunctionTriplet(visit, "outerProduct(", useEmulatedFunction);
917 break;
918
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700919 case EOpLessThan:
920 writeBuiltInFunctionTriplet(visit, "lessThan(", useEmulatedFunction);
921 break;
922 case EOpGreaterThan:
923 writeBuiltInFunctionTriplet(visit, "greaterThan(", useEmulatedFunction);
924 break;
925 case EOpLessThanEqual:
926 writeBuiltInFunctionTriplet(visit, "lessThanEqual(", useEmulatedFunction);
927 break;
928 case EOpGreaterThanEqual:
929 writeBuiltInFunctionTriplet(visit, "greaterThanEqual(", useEmulatedFunction);
930 break;
931 case EOpVectorEqual:
932 writeBuiltInFunctionTriplet(visit, "equal(", useEmulatedFunction);
933 break;
934 case EOpVectorNotEqual:
935 writeBuiltInFunctionTriplet(visit, "notEqual(", useEmulatedFunction);
936 break;
937 case EOpComma:
Zhenyao Mo0783efd2014-10-21 15:51:59 -0700938 writeTriplet(visit, "(", ", ", ")");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700939 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000940
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700941 case EOpMod:
942 writeBuiltInFunctionTriplet(visit, "mod(", useEmulatedFunction);
943 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +0200944 case EOpModf:
945 writeBuiltInFunctionTriplet(visit, "modf(", useEmulatedFunction);
946 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700947 case EOpPow:
948 writeBuiltInFunctionTriplet(visit, "pow(", useEmulatedFunction);
949 break;
950 case EOpAtan:
951 writeBuiltInFunctionTriplet(visit, "atan(", useEmulatedFunction);
952 break;
953 case EOpMin:
954 writeBuiltInFunctionTriplet(visit, "min(", useEmulatedFunction);
955 break;
956 case EOpMax:
957 writeBuiltInFunctionTriplet(visit, "max(", useEmulatedFunction);
958 break;
959 case EOpClamp:
960 writeBuiltInFunctionTriplet(visit, "clamp(", useEmulatedFunction);
961 break;
962 case EOpMix:
963 writeBuiltInFunctionTriplet(visit, "mix(", useEmulatedFunction);
964 break;
965 case EOpStep:
966 writeBuiltInFunctionTriplet(visit, "step(", useEmulatedFunction);
967 break;
968 case EOpSmoothStep:
969 writeBuiltInFunctionTriplet(visit, "smoothstep(", useEmulatedFunction);
970 break;
971 case EOpDistance:
972 writeBuiltInFunctionTriplet(visit, "distance(", useEmulatedFunction);
973 break;
974 case EOpDot:
975 writeBuiltInFunctionTriplet(visit, "dot(", useEmulatedFunction);
976 break;
977 case EOpCross:
978 writeBuiltInFunctionTriplet(visit, "cross(", useEmulatedFunction);
979 break;
980 case EOpFaceForward:
981 writeBuiltInFunctionTriplet(visit, "faceforward(", useEmulatedFunction);
982 break;
983 case EOpReflect:
984 writeBuiltInFunctionTriplet(visit, "reflect(", useEmulatedFunction);
985 break;
986 case EOpRefract:
987 writeBuiltInFunctionTriplet(visit, "refract(", useEmulatedFunction);
988 break;
989 case EOpMul:
990 writeBuiltInFunctionTriplet(visit, "matrixCompMult(", useEmulatedFunction);
991 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000992
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700993 default:
994 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000995 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000996 return visitChildren;
997}
998
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700999bool TOutputGLSLBase::visitLoop(Visit visit, TIntermLoop *node)
zmo@google.com5601ea02011-06-10 18:23:25 +00001000{
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001001 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +00001002
Zhenyao Mo7cab38b2013-10-15 12:59:30 -07001003 incrementDepth(node);
zmo@google.com5601ea02011-06-10 18:23:25 +00001004 // Loop header.
1005 TLoopType loopType = node->getType();
1006 if (loopType == ELoopFor) // for loop
1007 {
Zhenyao Mo550c6002014-02-26 15:40:48 -08001008 if (!node->getUnrollFlag())
1009 {
zmo@google.com5601ea02011-06-10 18:23:25 +00001010 out << "for (";
1011 if (node->getInit())
1012 node->getInit()->traverse(this);
1013 out << "; ";
1014
1015 if (node->getCondition())
1016 node->getCondition()->traverse(this);
1017 out << "; ";
1018
1019 if (node->getExpression())
1020 node->getExpression()->traverse(this);
1021 out << ")\n";
1022 }
Zhenyao Mo550c6002014-02-26 15:40:48 -08001023 else
1024 {
1025 // Need to put a one-iteration loop here to handle break.
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001026 TIntermSequence *declSeq =
Zhenyao Mo550c6002014-02-26 15:40:48 -08001027 node->getInit()->getAsAggregate()->getSequence();
1028 TIntermSymbol *indexSymbol =
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001029 (*declSeq)[0]->getAsBinaryNode()->getLeft()->getAsSymbolNode();
Zhenyao Mo550c6002014-02-26 15:40:48 -08001030 TString name = hashVariableName(indexSymbol->getSymbol());
1031 out << "for (int " << name << " = 0; "
1032 << name << " < 1; "
1033 << "++" << name << ")\n";
1034 }
zmo@google.com5601ea02011-06-10 18:23:25 +00001035 }
1036 else if (loopType == ELoopWhile) // while loop
1037 {
1038 out << "while (";
1039 ASSERT(node->getCondition() != NULL);
1040 node->getCondition()->traverse(this);
1041 out << ")\n";
1042 }
1043 else // do-while loop
1044 {
1045 ASSERT(loopType == ELoopDoWhile);
1046 out << "do\n";
1047 }
1048
1049 // Loop body.
1050 if (node->getUnrollFlag())
1051 {
Zhenyao Mo550c6002014-02-26 15:40:48 -08001052 out << "{\n";
1053 mLoopUnrollStack.push(node);
1054 while (mLoopUnrollStack.satisfiesLoopCondition())
zmo@google.com5601ea02011-06-10 18:23:25 +00001055 {
1056 visitCodeBlock(node->getBody());
Zhenyao Mo550c6002014-02-26 15:40:48 -08001057 mLoopUnrollStack.step();
zmo@google.com5601ea02011-06-10 18:23:25 +00001058 }
Zhenyao Mo550c6002014-02-26 15:40:48 -08001059 mLoopUnrollStack.pop();
1060 out << "}\n";
zmo@google.com5601ea02011-06-10 18:23:25 +00001061 }
1062 else
1063 {
1064 visitCodeBlock(node->getBody());
1065 }
1066
1067 // Loop footer.
1068 if (loopType == ELoopDoWhile) // do-while loop
1069 {
1070 out << "while (";
1071 ASSERT(node->getCondition() != NULL);
1072 node->getCondition()->traverse(this);
1073 out << ");\n";
1074 }
1075 decrementDepth();
1076
1077 // No need to visit children. They have been already processed in
1078 // this function.
1079 return false;
1080}
1081
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001082bool TOutputGLSLBase::visitBranch(Visit visit, TIntermBranch *node)
zmo@google.com5601ea02011-06-10 18:23:25 +00001083{
1084 switch (node->getFlowOp())
1085 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001086 case EOpKill:
1087 writeTriplet(visit, "discard", NULL, NULL);
1088 break;
1089 case EOpBreak:
1090 writeTriplet(visit, "break", NULL, NULL);
1091 break;
1092 case EOpContinue:
1093 writeTriplet(visit, "continue", NULL, NULL);
1094 break;
1095 case EOpReturn:
1096 writeTriplet(visit, "return ", NULL, NULL);
1097 break;
1098 default:
1099 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +00001100 }
1101
1102 return true;
1103}
1104
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001105void TOutputGLSLBase::visitCodeBlock(TIntermNode *node)
1106{
zmo@google.com5601ea02011-06-10 18:23:25 +00001107 TInfoSinkBase &out = objSink();
1108 if (node != NULL)
1109 {
1110 node->traverse(this);
1111 // Single statements not part of a sequence need to be terminated
1112 // with semi-colon.
1113 if (isSingleStatement(node))
1114 out << ";\n";
1115 }
1116 else
1117 {
1118 out << "{\n}\n"; // Empty code block.
1119 }
1120}
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001121
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001122TString TOutputGLSLBase::getTypeName(const TType &type)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001123{
1124 TInfoSinkBase out;
1125 if (type.isMatrix())
1126 {
1127 out << "mat";
1128 out << type.getNominalSize();
1129 }
1130 else if (type.isVector())
1131 {
1132 switch (type.getBasicType())
1133 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001134 case EbtFloat:
1135 out << "vec";
1136 break;
1137 case EbtInt:
1138 out << "ivec";
1139 break;
1140 case EbtBool:
1141 out << "bvec";
1142 break;
1143 default:
1144 UNREACHABLE();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001145 }
1146 out << type.getNominalSize();
1147 }
1148 else
1149 {
1150 if (type.getBasicType() == EbtStruct)
Jamie Madill98493dd2013-07-08 14:39:03 -04001151 out << hashName(type.getStruct()->name());
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001152 else
1153 out << type.getBasicString();
1154 }
1155 return TString(out.c_str());
1156}
1157
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001158TString TOutputGLSLBase::hashName(const TString &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001159{
1160 if (mHashFunction == NULL || name.empty())
1161 return name;
1162 NameMap::const_iterator it = mNameMap.find(name.c_str());
1163 if (it != mNameMap.end())
1164 return it->second.c_str();
1165 TString hashedName = TIntermTraverser::hash(name, mHashFunction);
1166 mNameMap[name.c_str()] = hashedName.c_str();
1167 return hashedName;
1168}
1169
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001170TString TOutputGLSLBase::hashVariableName(const TString &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001171{
Jamie Madill02f20dd2013-09-12 12:07:42 -04001172 if (mSymbolTable.findBuiltIn(name, mShaderVersion) != NULL)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001173 return name;
1174 return hashName(name);
1175}
1176
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001177TString TOutputGLSLBase::hashFunctionName(const TString &mangled_name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001178{
1179 TString name = TFunction::unmangleName(mangled_name);
Jamie Madill02f20dd2013-09-12 12:07:42 -04001180 if (mSymbolTable.findBuiltIn(mangled_name, mShaderVersion) != NULL || name == "main")
Nicolas Capens46485082014-04-15 13:12:50 -04001181 return translateTextureFunction(name);
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001182 return hashName(name);
1183}
Jamie Madill98493dd2013-07-08 14:39:03 -04001184
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001185bool TOutputGLSLBase::structDeclared(const TStructure *structure) const
Jamie Madill98493dd2013-07-08 14:39:03 -04001186{
Zhenyao Mo904a9162014-05-09 14:07:45 -07001187 ASSERT(structure);
Jamie Madill01f85ac2014-06-06 11:55:04 -04001188 if (structure->name().empty())
Zhenyao Mo904a9162014-05-09 14:07:45 -07001189 {
Jamie Madill01f85ac2014-06-06 11:55:04 -04001190 return false;
Zhenyao Mo904a9162014-05-09 14:07:45 -07001191 }
Jamie Madill01f85ac2014-06-06 11:55:04 -04001192
1193 return (mDeclaredStructs.count(structure->uniqueId()) > 0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001194}
1195
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001196void TOutputGLSLBase::declareStruct(const TStructure *structure)
Jamie Madill98493dd2013-07-08 14:39:03 -04001197{
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001198 TInfoSinkBase &out = objSink();
Jamie Madill98493dd2013-07-08 14:39:03 -04001199
1200 out << "struct " << hashName(structure->name()) << "{\n";
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001201 const TFieldList &fields = structure->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04001202 for (size_t i = 0; i < fields.size(); ++i)
1203 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001204 const TField *field = fields[i];
Jamie Madill98493dd2013-07-08 14:39:03 -04001205 if (writeVariablePrecision(field->type()->getPrecision()))
1206 out << " ";
1207 out << getTypeName(*field->type()) << " " << hashName(field->name());
1208 if (field->type()->isArray())
1209 out << arrayBrackets(*field->type());
1210 out << ";\n";
1211 }
1212 out << "}";
Zhenyao Mo904a9162014-05-09 14:07:45 -07001213}
Jamie Madill98493dd2013-07-08 14:39:03 -04001214