blob: 4deeceb22b22e8b83d90f4516c117602db6f8542 [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"
Olli Etuahod57e0db2015-04-24 15:05:08 +03008
9#include "common/debug.h"
zmo@google.com5601ea02011-06-10 18:23:25 +000010
daniel@transgaming.com773ff742013-01-11 04:12:51 +000011#include <cfloat>
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +000012
zmo@google.com5601ea02011-06-10 18:23:25 +000013namespace
14{
Zhenyao Mo9eedea02014-05-12 16:02:35 -070015TString arrayBrackets(const TType &type)
zmo@google.com5601ea02011-06-10 18:23:25 +000016{
17 ASSERT(type.isArray());
18 TInfoSinkBase out;
19 out << "[" << type.getArraySize() << "]";
20 return TString(out.c_str());
21}
22
Zhenyao Mo9eedea02014-05-12 16:02:35 -070023bool isSingleStatement(TIntermNode *node)
24{
25 if (const TIntermAggregate *aggregate = node->getAsAggregate())
zmo@google.com5601ea02011-06-10 18:23:25 +000026 {
27 return (aggregate->getOp() != EOpFunction) &&
28 (aggregate->getOp() != EOpSequence);
29 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -070030 else if (const TIntermSelection *selection = node->getAsSelectionNode())
zmo@google.com5601ea02011-06-10 18:23:25 +000031 {
32 // Ternary operators are usually part of an assignment operator.
33 // This handles those rare cases in which they are all by themselves.
34 return selection->usesTernaryOperator();
35 }
36 else if (node->getAsLoopNode())
37 {
38 return false;
39 }
Olli Etuaho01cd8af2015-02-20 10:39:20 +020040 else if (node->getAsSwitchNode())
41 {
42 return false;
43 }
44 else if (node->getAsCaseNode())
45 {
46 return false;
47 }
zmo@google.com5601ea02011-06-10 18:23:25 +000048 return true;
49}
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080050
zmo@google.com5601ea02011-06-10 18:23:25 +000051} // namespace
52
Zhenyao Mo9eedea02014-05-12 16:02:35 -070053TOutputGLSLBase::TOutputGLSLBase(TInfoSinkBase &objSink,
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +000054 ShArrayIndexClampingStrategy clampingStrategy,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000055 ShHashFunction64 hashFunction,
Zhenyao Mo9eedea02014-05-12 16:02:35 -070056 NameMap &nameMap,
57 TSymbolTable &symbolTable,
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080058 int shaderVersion,
59 ShShaderOutput output)
zmo@google.com5601ea02011-06-10 18:23:25 +000060 : TIntermTraverser(true, true, true),
61 mObjSink(objSink),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000062 mDeclaringVariables(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +000063 mClampingStrategy(clampingStrategy),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000064 mHashFunction(hashFunction),
65 mNameMap(nameMap),
Jamie Madill02f20dd2013-09-12 12:07:42 -040066 mSymbolTable(symbolTable),
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080067 mShaderVersion(shaderVersion),
68 mOutput(output)
zmo@google.com5601ea02011-06-10 18:23:25 +000069{
70}
71
Zhenyao Mo9eedea02014-05-12 16:02:35 -070072void TOutputGLSLBase::writeTriplet(
73 Visit visit, const char *preStr, const char *inStr, const char *postStr)
zmo@google.com5601ea02011-06-10 18:23:25 +000074{
Zhenyao Mo9eedea02014-05-12 16:02:35 -070075 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +000076 if (visit == PreVisit && preStr)
zmo@google.com5601ea02011-06-10 18:23:25 +000077 out << preStr;
zmo@google.com5601ea02011-06-10 18:23:25 +000078 else if (visit == InVisit && inStr)
zmo@google.com5601ea02011-06-10 18:23:25 +000079 out << inStr;
zmo@google.com5601ea02011-06-10 18:23:25 +000080 else if (visit == PostVisit && postStr)
zmo@google.com5601ea02011-06-10 18:23:25 +000081 out << postStr;
zmo@google.com5601ea02011-06-10 18:23:25 +000082}
83
Zhenyao Mo9eedea02014-05-12 16:02:35 -070084void TOutputGLSLBase::writeBuiltInFunctionTriplet(
85 Visit visit, const char *preStr, bool useEmulatedFunction)
zmo@google.com5601ea02011-06-10 18:23:25 +000086{
Zhenyao Mo9eedea02014-05-12 16:02:35 -070087 TString preString = useEmulatedFunction ?
88 BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr) : preStr;
89 writeTriplet(visit, preString.c_str(), ", ", ")");
90}
91
92void TOutputGLSLBase::writeVariableType(const TType &type)
93{
94 TInfoSinkBase &out = objSink();
Olli Etuaho214c2d82015-04-27 14:49:13 +030095 if (type.isInvariant())
96 {
97 out << "invariant ";
98 }
zmo@google.com5601ea02011-06-10 18:23:25 +000099 TQualifier qualifier = type.getQualifier();
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400100 if (qualifier != EvqTemporary && qualifier != EvqGlobal)
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400101 {
Qingqing Dengad0d0792015-04-08 14:25:06 -0700102 if (IsGLSL130OrNewer(mOutput))
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800103 {
104 switch (qualifier)
105 {
106 case EvqAttribute:
Olli Etuaho214c2d82015-04-27 14:49:13 +0300107 out << "in ";
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800108 break;
109 case EvqVaryingIn:
Olli Etuaho214c2d82015-04-27 14:49:13 +0300110 out << "in ";
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800111 break;
112 case EvqVaryingOut:
Olli Etuaho214c2d82015-04-27 14:49:13 +0300113 out << "out ";
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800114 break;
115 default:
116 out << type.getQualifierString() << " ";
117 break;
118 }
119 }
120 else
121 {
122 out << type.getQualifierString() << " ";
123 }
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400124 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000125 // Declare the struct if we have not done so already.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700126 if (type.getBasicType() == EbtStruct && !structDeclared(type.getStruct()))
zmo@google.com5601ea02011-06-10 18:23:25 +0000127 {
Jamie Madill01f85ac2014-06-06 11:55:04 -0400128 TStructure *structure = type.getStruct();
129
130 declareStruct(structure);
131
132 if (!structure->name().empty())
133 {
134 mDeclaredStructs.insert(structure->uniqueId());
135 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000136 }
137 else
138 {
139 if (writeVariablePrecision(type.getPrecision()))
140 out << " ";
141 out << getTypeName(type);
142 }
143}
144
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700145void TOutputGLSLBase::writeFunctionParameters(const TIntermSequence &args)
zmo@google.com5601ea02011-06-10 18:23:25 +0000146{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700147 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000148 for (TIntermSequence::const_iterator iter = args.begin();
149 iter != args.end(); ++iter)
150 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700151 const TIntermSymbol *arg = (*iter)->getAsSymbolNode();
zmo@google.com5601ea02011-06-10 18:23:25 +0000152 ASSERT(arg != NULL);
153
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700154 const TType &type = arg->getType();
zmo@google.com189be2f2011-06-16 18:28:53 +0000155 writeVariableType(type);
zmo@google.com5601ea02011-06-10 18:23:25 +0000156
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700157 const TString &name = arg->getSymbol();
zmo@google.com5601ea02011-06-10 18:23:25 +0000158 if (!name.empty())
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000159 out << " " << hashName(name);
zmo@google.com5601ea02011-06-10 18:23:25 +0000160 if (type.isArray())
161 out << arrayBrackets(type);
162
163 // Put a comma if this is not the last argument.
164 if (iter != args.end() - 1)
165 out << ", ";
166 }
167}
168
Jamie Madill6ba6ead2015-05-04 14:21:21 -0400169const TConstantUnion *TOutputGLSLBase::writeConstantUnion(
170 const TType &type, const TConstantUnion *pConstUnion)
zmo@google.com5601ea02011-06-10 18:23:25 +0000171{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700172 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000173
174 if (type.getBasicType() == EbtStruct)
175 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700176 const TStructure *structure = type.getStruct();
Jamie Madill98493dd2013-07-08 14:39:03 -0400177 out << hashName(structure->name()) << "(";
178
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700179 const TFieldList &fields = structure->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -0400180 for (size_t i = 0; i < fields.size(); ++i)
zmo@google.com5601ea02011-06-10 18:23:25 +0000181 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700182 const TType *fieldType = fields[i]->type();
zmo@google.com5601ea02011-06-10 18:23:25 +0000183 ASSERT(fieldType != NULL);
184 pConstUnion = writeConstantUnion(*fieldType, pConstUnion);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700185 if (i != fields.size() - 1)
186 out << ", ";
zmo@google.com5601ea02011-06-10 18:23:25 +0000187 }
188 out << ")";
189 }
190 else
191 {
Jamie Madill94bf7f22013-07-08 13:31:15 -0400192 size_t size = type.getObjectSize();
zmo@google.com5601ea02011-06-10 18:23:25 +0000193 bool writeType = size > 1;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700194 if (writeType)
195 out << getTypeName(type) << "(";
Jamie Madill94bf7f22013-07-08 13:31:15 -0400196 for (size_t i = 0; i < size; ++i, ++pConstUnion)
zmo@google.com5601ea02011-06-10 18:23:25 +0000197 {
198 switch (pConstUnion->getType())
199 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700200 case EbtFloat:
201 out << std::min(FLT_MAX, std::max(-FLT_MAX, pConstUnion->getFConst()));
202 break;
203 case EbtInt:
204 out << pConstUnion->getIConst();
205 break;
Olli Etuaho5321c802015-04-02 17:08:16 +0300206 case EbtUInt:
207 out << pConstUnion->getUConst() << "u";
208 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700209 case EbtBool:
210 out << pConstUnion->getBConst();
211 break;
212 default: UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000213 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700214 if (i != size - 1)
215 out << ", ";
zmo@google.com5601ea02011-06-10 18:23:25 +0000216 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700217 if (writeType)
218 out << ")";
zmo@google.com5601ea02011-06-10 18:23:25 +0000219 }
220 return pConstUnion;
221}
222
Olli Etuahof40319e2015-03-10 14:33:00 +0200223void TOutputGLSLBase::writeConstructorTriplet(Visit visit, const TType &type, const char *constructorBaseType)
224{
225 TInfoSinkBase &out = objSink();
226 if (visit == PreVisit)
227 {
228 if (type.isArray())
229 {
230 out << constructorBaseType;
231 out << arrayBrackets(type);
232 out << "(";
233 }
234 else
235 {
236 out << constructorBaseType << "(";
237 }
238 }
239 else
240 {
241 writeTriplet(visit, nullptr, ", ", ")");
242 }
243}
244
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700245void TOutputGLSLBase::visitSymbol(TIntermSymbol *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000246{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700247 TInfoSinkBase &out = objSink();
Zhenyao Mo550c6002014-02-26 15:40:48 -0800248 if (mLoopUnrollStack.needsToReplaceSymbolWithValue(node))
249 out << mLoopUnrollStack.getLoopIndexValue(node);
zmo@google.com5601ea02011-06-10 18:23:25 +0000250 else
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000251 out << hashVariableName(node->getSymbol());
zmo@google.com5601ea02011-06-10 18:23:25 +0000252
253 if (mDeclaringVariables && node->getType().isArray())
254 out << arrayBrackets(node->getType());
255}
256
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700257void TOutputGLSLBase::visitConstantUnion(TIntermConstantUnion *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000258{
259 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
260}
261
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700262bool TOutputGLSLBase::visitBinary(Visit visit, TIntermBinary *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000263{
264 bool visitChildren = true;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700265 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000266 switch (node->getOp())
267 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700268 case EOpInitialize:
269 if (visit == InVisit)
270 {
271 out << " = ";
272 // RHS of initialize is not being declared.
273 mDeclaringVariables = false;
274 }
275 break;
276 case EOpAssign:
277 writeTriplet(visit, "(", " = ", ")");
278 break;
279 case EOpAddAssign:
280 writeTriplet(visit, "(", " += ", ")");
281 break;
282 case EOpSubAssign:
283 writeTriplet(visit, "(", " -= ", ")");
284 break;
285 case EOpDivAssign:
286 writeTriplet(visit, "(", " /= ", ")");
287 break;
Olli Etuahoff805cc2015-02-13 10:59:34 +0200288 case EOpIModAssign:
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +0000289 writeTriplet(visit, "(", " %= ", ")");
290 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700291 // Notice the fall-through.
292 case EOpMulAssign:
293 case EOpVectorTimesMatrixAssign:
294 case EOpVectorTimesScalarAssign:
295 case EOpMatrixTimesScalarAssign:
296 case EOpMatrixTimesMatrixAssign:
297 writeTriplet(visit, "(", " *= ", ")");
298 break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200299 case EOpBitShiftLeftAssign:
300 writeTriplet(visit, "(", " <<= ", ")");
301 break;
302 case EOpBitShiftRightAssign:
303 writeTriplet(visit, "(", " >>= ", ")");
304 break;
305 case EOpBitwiseAndAssign:
306 writeTriplet(visit, "(", " &= ", ")");
307 break;
308 case EOpBitwiseXorAssign:
309 writeTriplet(visit, "(", " ^= ", ")");
310 break;
311 case EOpBitwiseOrAssign:
312 writeTriplet(visit, "(", " |= ", ")");
313 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700314
315 case EOpIndexDirect:
316 writeTriplet(visit, NULL, "[", "]");
317 break;
318 case EOpIndexIndirect:
319 if (node->getAddIndexClamp())
320 {
zmo@google.com5601ea02011-06-10 18:23:25 +0000321 if (visit == InVisit)
322 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700323 if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC)
324 out << "[int(clamp(float(";
325 else
326 out << "[webgl_int_clamp(";
zmo@google.com5601ea02011-06-10 18:23:25 +0000327 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700328 else if (visit == PostVisit)
329 {
330 int maxSize;
331 TIntermTyped *left = node->getLeft();
332 TType leftType = left->getType();
zmo@google.com5601ea02011-06-10 18:23:25 +0000333
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700334 if (left->isArray())
335 {
336 // The shader will fail validation if the array length is not > 0.
337 maxSize = leftType.getArraySize() - 1;
338 }
339 else
340 {
341 maxSize = leftType.getNominalSize() - 1;
342 }
343
344 if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC)
345 out << "), 0.0, float(" << maxSize << ")))]";
346 else
347 out << ", 0, " << maxSize << ")]";
348 }
349 }
350 else
351 {
zmo@google.com5601ea02011-06-10 18:23:25 +0000352 writeTriplet(visit, NULL, "[", "]");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700353 }
354 break;
355 case EOpIndexDirectStruct:
356 if (visit == InVisit)
357 {
358 // Here we are writing out "foo.bar", where "foo" is struct
359 // and "bar" is field. In AST, it is represented as a binary
360 // node, where left child represents "foo" and right child "bar".
361 // The node itself represents ".". The struct field "bar" is
362 // actually stored as an index into TStructure::fields.
363 out << ".";
364 const TStructure *structure = node->getLeft()->getType().getStruct();
365 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
366 const TField *field = structure->fields()[index->getIConst(0)];
367
368 TString fieldName = field->name();
369 if (!mSymbolTable.findBuiltIn(structure->name(), mShaderVersion))
370 fieldName = hashName(fieldName);
371
372 out << fieldName;
373 visitChildren = false;
374 }
375 break;
376 case EOpVectorSwizzle:
377 if (visit == InVisit)
378 {
379 out << ".";
380 TIntermAggregate *rightChild = node->getRight()->getAsAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700381 TIntermSequence *sequence = rightChild->getSequence();
382 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); ++sit)
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000383 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700384 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
385 ASSERT(element->getBasicType() == EbtInt);
386 ASSERT(element->getNominalSize() == 1);
Jamie Madill6ba6ead2015-05-04 14:21:21 -0400387 const TConstantUnion& data = element->getUnionArrayPointer()[0];
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700388 ASSERT(data.getType() == EbtInt);
389 switch (data.getIConst())
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000390 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700391 case 0:
392 out << "x";
393 break;
394 case 1:
395 out << "y";
396 break;
397 case 2:
398 out << "z";
399 break;
400 case 3:
401 out << "w";
402 break;
403 default:
404 UNREACHABLE();
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000405 }
406 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700407 visitChildren = false;
408 }
409 break;
daniel@transgaming.com97b16d12013-02-01 03:20:42 +0000410
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700411 case EOpAdd:
412 writeTriplet(visit, "(", " + ", ")");
413 break;
414 case EOpSub:
415 writeTriplet(visit, "(", " - ", ")");
416 break;
417 case EOpMul:
418 writeTriplet(visit, "(", " * ", ")");
419 break;
420 case EOpDiv:
421 writeTriplet(visit, "(", " / ", ")");
422 break;
Olli Etuahoff805cc2015-02-13 10:59:34 +0200423 case EOpIMod:
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +0000424 writeTriplet(visit, "(", " % ", ")");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700425 break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200426 case EOpBitShiftLeft:
427 writeTriplet(visit, "(", " << ", ")");
428 break;
429 case EOpBitShiftRight:
430 writeTriplet(visit, "(", " >> ", ")");
431 break;
432 case EOpBitwiseAnd:
433 writeTriplet(visit, "(", " & ", ")");
434 break;
435 case EOpBitwiseXor:
436 writeTriplet(visit, "(", " ^ ", ")");
437 break;
438 case EOpBitwiseOr:
439 writeTriplet(visit, "(", " | ", ")");
440 break;
441
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700442 case EOpEqual:
443 writeTriplet(visit, "(", " == ", ")");
444 break;
445 case EOpNotEqual:
446 writeTriplet(visit, "(", " != ", ")");
447 break;
448 case EOpLessThan:
449 writeTriplet(visit, "(", " < ", ")");
450 break;
451 case EOpGreaterThan:
452 writeTriplet(visit, "(", " > ", ")");
453 break;
454 case EOpLessThanEqual:
455 writeTriplet(visit, "(", " <= ", ")");
456 break;
457 case EOpGreaterThanEqual:
458 writeTriplet(visit, "(", " >= ", ")");
459 break;
daniel@transgaming.com97b16d12013-02-01 03:20:42 +0000460
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700461 // Notice the fall-through.
462 case EOpVectorTimesScalar:
463 case EOpVectorTimesMatrix:
464 case EOpMatrixTimesVector:
465 case EOpMatrixTimesScalar:
466 case EOpMatrixTimesMatrix:
467 writeTriplet(visit, "(", " * ", ")");
468 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000469
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700470 case EOpLogicalOr:
471 writeTriplet(visit, "(", " || ", ")");
472 break;
473 case EOpLogicalXor:
474 writeTriplet(visit, "(", " ^^ ", ")");
475 break;
476 case EOpLogicalAnd:
477 writeTriplet(visit, "(", " && ", ")");
478 break;
479 default:
480 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000481 }
482
483 return visitChildren;
484}
485
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700486bool TOutputGLSLBase::visitUnary(Visit visit, TIntermUnary *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000487{
zmo@google.com32e97312011-08-24 01:03:11 +0000488 TString preString;
489 TString postString = ")";
490
zmo@google.com5601ea02011-06-10 18:23:25 +0000491 switch (node->getOp())
492 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700493 case EOpNegative: preString = "(-"; break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -0700494 case EOpPositive: preString = "(+"; break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700495 case EOpVectorLogicalNot: preString = "not("; break;
496 case EOpLogicalNot: preString = "(!"; break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200497 case EOpBitwiseNot: preString = "(~"; break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000498
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700499 case EOpPostIncrement: preString = "("; postString = "++)"; break;
500 case EOpPostDecrement: preString = "("; postString = "--)"; break;
501 case EOpPreIncrement: preString = "(++"; break;
502 case EOpPreDecrement: preString = "(--"; break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000503
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700504 case EOpRadians:
505 preString = "radians(";
506 break;
507 case EOpDegrees:
508 preString = "degrees(";
509 break;
510 case EOpSin:
511 preString = "sin(";
512 break;
513 case EOpCos:
514 preString = "cos(";
515 break;
516 case EOpTan:
517 preString = "tan(";
518 break;
519 case EOpAsin:
520 preString = "asin(";
521 break;
522 case EOpAcos:
523 preString = "acos(";
524 break;
525 case EOpAtan:
526 preString = "atan(";
527 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000528
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200529 case EOpSinh:
530 preString = "sinh(";
531 break;
532 case EOpCosh:
533 preString = "cosh(";
534 break;
535 case EOpTanh:
536 preString = "tanh(";
537 break;
538 case EOpAsinh:
539 preString = "asinh(";
540 break;
541 case EOpAcosh:
542 preString = "acosh(";
543 break;
544 case EOpAtanh:
545 preString = "atanh(";
546 break;
547
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700548 case EOpExp:
549 preString = "exp(";
550 break;
551 case EOpLog:
552 preString = "log(";
553 break;
554 case EOpExp2:
555 preString = "exp2(";
556 break;
557 case EOpLog2:
558 preString = "log2(";
559 break;
560 case EOpSqrt:
561 preString = "sqrt(";
562 break;
563 case EOpInverseSqrt:
564 preString = "inversesqrt(";
565 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000566
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700567 case EOpAbs:
568 preString = "abs(";
569 break;
570 case EOpSign:
571 preString = "sign(";
572 break;
573 case EOpFloor:
574 preString = "floor(";
575 break;
Qingqing Deng5dbece52015-02-27 20:35:38 -0800576 case EOpTrunc:
577 preString = "trunc(";
578 break;
579 case EOpRound:
580 preString = "round(";
581 break;
582 case EOpRoundEven:
583 preString = "roundEven(";
584 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700585 case EOpCeil:
586 preString = "ceil(";
587 break;
588 case EOpFract:
589 preString = "fract(";
590 break;
Arun Patole0c1726e2015-02-18 14:35:02 +0530591 case EOpIsNan:
592 preString = "isnan(";
593 break;
594 case EOpIsInf:
595 preString = "isinf(";
596 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000597
Olli Etuahoe8d2c072015-01-08 16:33:54 +0200598 case EOpFloatBitsToInt:
599 preString = "floatBitsToInt(";
600 break;
601 case EOpFloatBitsToUint:
602 preString = "floatBitsToUint(";
603 break;
604 case EOpIntBitsToFloat:
605 preString = "intBitsToFloat(";
606 break;
607 case EOpUintBitsToFloat:
608 preString = "uintBitsToFloat(";
609 break;
610
Olli Etuaho7700ff62015-01-15 12:16:29 +0200611 case EOpPackSnorm2x16:
612 preString = "packSnorm2x16(";
613 break;
614 case EOpPackUnorm2x16:
615 preString = "packUnorm2x16(";
616 break;
617 case EOpPackHalf2x16:
618 preString = "packHalf2x16(";
619 break;
620 case EOpUnpackSnorm2x16:
621 preString = "unpackSnorm2x16(";
622 break;
623 case EOpUnpackUnorm2x16:
624 preString = "unpackUnorm2x16(";
625 break;
626 case EOpUnpackHalf2x16:
627 preString = "unpackHalf2x16(";
628 break;
629
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700630 case EOpLength:
631 preString = "length(";
632 break;
633 case EOpNormalize:
634 preString = "normalize(";
635 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000636
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700637 case EOpDFdx:
638 preString = "dFdx(";
639 break;
640 case EOpDFdy:
641 preString = "dFdy(";
642 break;
643 case EOpFwidth:
644 preString = "fwidth(";
645 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000646
Olli Etuahoe39706d2014-12-30 16:40:36 +0200647 case EOpTranspose:
648 preString = "transpose(";
649 break;
650 case EOpDeterminant:
651 preString = "determinant(";
652 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +0200653 case EOpInverse:
654 preString = "inverse(";
655 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +0200656
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700657 case EOpAny:
658 preString = "any(";
659 break;
660 case EOpAll:
661 preString = "all(";
662 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000663
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700664 default:
665 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000666 }
667
zmo@google.com32e97312011-08-24 01:03:11 +0000668 if (visit == PreVisit && node->getUseEmulatedFunction())
669 preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preString);
670 writeTriplet(visit, preString.c_str(), NULL, postString.c_str());
671
zmo@google.com5601ea02011-06-10 18:23:25 +0000672 return true;
673}
674
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700675bool TOutputGLSLBase::visitSelection(Visit visit, TIntermSelection *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000676{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700677 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000678
679 if (node->usesTernaryOperator())
680 {
681 // Notice two brackets at the beginning and end. The outer ones
682 // encapsulate the whole ternary expression. This preserves the
683 // order of precedence when ternary expressions are used in a
684 // compound expression, i.e., c = 2 * (a < b ? 1 : 2).
685 out << "((";
686 node->getCondition()->traverse(this);
687 out << ") ? (";
688 node->getTrueBlock()->traverse(this);
689 out << ") : (";
690 node->getFalseBlock()->traverse(this);
691 out << "))";
692 }
693 else
694 {
695 out << "if (";
696 node->getCondition()->traverse(this);
697 out << ")\n";
698
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700699 incrementDepth(node);
zmo@google.com5601ea02011-06-10 18:23:25 +0000700 visitCodeBlock(node->getTrueBlock());
701
702 if (node->getFalseBlock())
703 {
704 out << "else\n";
705 visitCodeBlock(node->getFalseBlock());
706 }
707 decrementDepth();
708 }
709 return false;
710}
711
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200712bool TOutputGLSLBase::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200713{
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200714 if (node->getStatementList())
715 {
716 writeTriplet(visit, "switch (", ") ", nullptr);
717 // The curly braces get written when visiting the statementList aggregate
718 }
719 else
720 {
721 // No statementList, so it won't output curly braces
722 writeTriplet(visit, "switch (", ") {", "}\n");
723 }
724 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200725}
726
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200727bool TOutputGLSLBase::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200728{
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200729 if (node->hasCondition())
730 {
731 writeTriplet(visit, "case (", nullptr, "):\n");
732 return true;
733 }
734 else
735 {
736 TInfoSinkBase &out = objSink();
737 out << "default:\n";
738 return false;
739 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200740}
741
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700742bool TOutputGLSLBase::visitAggregate(Visit visit, TIntermAggregate *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000743{
744 bool visitChildren = true;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700745 TInfoSinkBase &out = objSink();
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700746 bool useEmulatedFunction = (visit == PreVisit && node->getUseEmulatedFunction());
zmo@google.com5601ea02011-06-10 18:23:25 +0000747 switch (node->getOp())
748 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700749 case EOpSequence:
750 // Scope the sequences except when at the global scope.
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700751 if (mDepth > 0)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700752 {
753 out << "{\n";
zmo@google.com5601ea02011-06-10 18:23:25 +0000754 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000755
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700756 incrementDepth(node);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700757 for (TIntermSequence::const_iterator iter = node->getSequence()->begin();
758 iter != node->getSequence()->end(); ++iter)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700759 {
Austin Kinross3ae64652015-01-26 15:51:39 -0800760 TIntermNode *curNode = *iter;
761 ASSERT(curNode != NULL);
762 curNode->traverse(this);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700763
Austin Kinross3ae64652015-01-26 15:51:39 -0800764 if (isSingleStatement(curNode))
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700765 out << ";\n";
766 }
767 decrementDepth();
768
769 // Scope the sequences except when at the global scope.
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700770 if (mDepth > 0)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700771 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700772 out << "}\n";
773 }
774 visitChildren = false;
775 break;
776 case EOpPrototype:
777 // Function declaration.
778 ASSERT(visit == PreVisit);
Olli Etuahoab6fc6a2015-04-13 12:10:20 +0300779 {
780 const TType &type = node->getType();
781 writeVariableType(type);
782 if (type.isArray())
783 out << arrayBrackets(type);
784 }
785
Olli Etuaho59f9a642015-08-06 20:38:26 +0300786 out << " " << hashFunctionNameIfNeeded(node->getNameObj());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700787
788 out << "(";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700789 writeFunctionParameters(*(node->getSequence()));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700790 out << ")";
791
792 visitChildren = false;
793 break;
794 case EOpFunction: {
795 // Function definition.
796 ASSERT(visit == PreVisit);
Olli Etuahoab6fc6a2015-04-13 12:10:20 +0300797 {
798 const TType &type = node->getType();
799 writeVariableType(type);
800 if (type.isArray())
801 out << arrayBrackets(type);
802 }
803
Olli Etuaho59f9a642015-08-06 20:38:26 +0300804 out << " " << hashFunctionNameIfNeeded(node->getNameObj());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700805
806 incrementDepth(node);
807 // Function definition node contains one or two children nodes
808 // representing function parameters and function body. The latter
809 // is not present in case of empty function bodies.
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700810 const TIntermSequence &sequence = *(node->getSequence());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700811 ASSERT((sequence.size() == 1) || (sequence.size() == 2));
812 TIntermSequence::const_iterator seqIter = sequence.begin();
813
814 // Traverse function parameters.
815 TIntermAggregate *params = (*seqIter)->getAsAggregate();
816 ASSERT(params != NULL);
817 ASSERT(params->getOp() == EOpParameters);
818 params->traverse(this);
819
820 // Traverse function body.
821 TIntermAggregate *body = ++seqIter != sequence.end() ?
822 (*seqIter)->getAsAggregate() : NULL;
823 visitCodeBlock(body);
824 decrementDepth();
825
826 // Fully processed; no need to visit children.
827 visitChildren = false;
828 break;
829 }
830 case EOpFunctionCall:
831 // Function call.
832 if (visit == PreVisit)
Olli Etuaho59f9a642015-08-06 20:38:26 +0300833 out << hashFunctionNameIfNeeded(node->getNameObj()) << "(";
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200834 else if (visit == InVisit)
835 out << ", ";
836 else
837 out << ")";
838 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700839 case EOpParameters:
840 // Function parameters.
841 ASSERT(visit == PreVisit);
842 out << "(";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700843 writeFunctionParameters(*(node->getSequence()));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700844 out << ")";
845 visitChildren = false;
846 break;
847 case EOpDeclaration:
848 // Variable declaration.
849 if (visit == PreVisit)
850 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700851 const TIntermSequence &sequence = *(node->getSequence());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700852 const TIntermTyped *variable = sequence.front()->getAsTyped();
853 writeVariableType(variable->getType());
854 out << " ";
855 mDeclaringVariables = true;
zmo@google.com5601ea02011-06-10 18:23:25 +0000856 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700857 else if (visit == InVisit)
858 {
859 out << ", ";
860 mDeclaringVariables = true;
zmo@google.com5601ea02011-06-10 18:23:25 +0000861 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700862 else
863 {
864 mDeclaringVariables = false;
865 }
866 break;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700867 case EOpInvariantDeclaration:
868 // Invariant declaration.
869 ASSERT(visit == PreVisit);
870 {
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400871 const TIntermSequence *sequence = node->getSequence();
872 ASSERT(sequence && sequence->size() == 1);
873 const TIntermSymbol *symbol = sequence->front()->getAsSymbolNode();
874 ASSERT(symbol);
Zhenyao Mo2a517272014-10-27 16:09:57 -0700875 out << "invariant " << hashVariableName(symbol->getSymbol());
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400876 }
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700877 visitChildren = false;
878 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700879 case EOpConstructFloat:
Olli Etuahof40319e2015-03-10 14:33:00 +0200880 writeConstructorTriplet(visit, node->getType(), "float");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700881 break;
882 case EOpConstructVec2:
Olli Etuahof40319e2015-03-10 14:33:00 +0200883 writeConstructorTriplet(visit, node->getType(), "vec2");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700884 break;
885 case EOpConstructVec3:
Olli Etuahof40319e2015-03-10 14:33:00 +0200886 writeConstructorTriplet(visit, node->getType(), "vec3");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700887 break;
888 case EOpConstructVec4:
Olli Etuahof40319e2015-03-10 14:33:00 +0200889 writeConstructorTriplet(visit, node->getType(), "vec4");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700890 break;
891 case EOpConstructBool:
Olli Etuahof40319e2015-03-10 14:33:00 +0200892 writeConstructorTriplet(visit, node->getType(), "bool");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700893 break;
894 case EOpConstructBVec2:
Olli Etuahof40319e2015-03-10 14:33:00 +0200895 writeConstructorTriplet(visit, node->getType(), "bvec2");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700896 break;
897 case EOpConstructBVec3:
Olli Etuahof40319e2015-03-10 14:33:00 +0200898 writeConstructorTriplet(visit, node->getType(), "bvec3");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700899 break;
900 case EOpConstructBVec4:
Olli Etuahof40319e2015-03-10 14:33:00 +0200901 writeConstructorTriplet(visit, node->getType(), "bvec4");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700902 break;
903 case EOpConstructInt:
Olli Etuahof40319e2015-03-10 14:33:00 +0200904 writeConstructorTriplet(visit, node->getType(), "int");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700905 break;
906 case EOpConstructIVec2:
Olli Etuahof40319e2015-03-10 14:33:00 +0200907 writeConstructorTriplet(visit, node->getType(), "ivec2");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700908 break;
909 case EOpConstructIVec3:
Olli Etuahof40319e2015-03-10 14:33:00 +0200910 writeConstructorTriplet(visit, node->getType(), "ivec3");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700911 break;
912 case EOpConstructIVec4:
Olli Etuahof40319e2015-03-10 14:33:00 +0200913 writeConstructorTriplet(visit, node->getType(), "ivec4");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700914 break;
Geoff Langc4c7442d2015-07-20 13:09:26 -0400915 case EOpConstructUInt:
916 writeConstructorTriplet(visit, node->getType(), "uint");
917 break;
918 case EOpConstructUVec2:
919 writeConstructorTriplet(visit, node->getType(), "uvec2");
920 break;
921 case EOpConstructUVec3:
922 writeConstructorTriplet(visit, node->getType(), "uvec3");
923 break;
924 case EOpConstructUVec4:
925 writeConstructorTriplet(visit, node->getType(), "uvec4");
926 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700927 case EOpConstructMat2:
Olli Etuahof40319e2015-03-10 14:33:00 +0200928 writeConstructorTriplet(visit, node->getType(), "mat2");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700929 break;
Alexis Hetu07e57df2015-06-16 16:55:52 -0400930 case EOpConstructMat2x3:
931 writeConstructorTriplet(visit, node->getType(), "mat2x3");
932 break;
933 case EOpConstructMat2x4:
934 writeConstructorTriplet(visit, node->getType(), "mat2x4");
935 break;
936 case EOpConstructMat3x2:
937 writeConstructorTriplet(visit, node->getType(), "mat3x2");
938 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700939 case EOpConstructMat3:
Olli Etuahof40319e2015-03-10 14:33:00 +0200940 writeConstructorTriplet(visit, node->getType(), "mat3");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700941 break;
Alexis Hetu07e57df2015-06-16 16:55:52 -0400942 case EOpConstructMat3x4:
943 writeConstructorTriplet(visit, node->getType(), "mat3x4");
944 break;
945 case EOpConstructMat4x2:
946 writeConstructorTriplet(visit, node->getType(), "mat4x2");
947 break;
948 case EOpConstructMat4x3:
949 writeConstructorTriplet(visit, node->getType(), "mat4x3");
950 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700951 case EOpConstructMat4:
Olli Etuahof40319e2015-03-10 14:33:00 +0200952 writeConstructorTriplet(visit, node->getType(), "mat4");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700953 break;
954 case EOpConstructStruct:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700955 {
956 const TType &type = node->getType();
957 ASSERT(type.getBasicType() == EbtStruct);
Olli Etuahof40319e2015-03-10 14:33:00 +0200958 TString constructorName = hashName(type.getStruct()->name());
959 writeConstructorTriplet(visit, node->getType(), constructorName.c_str());
960 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700961 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000962
Olli Etuahoe39706d2014-12-30 16:40:36 +0200963 case EOpOuterProduct:
964 writeBuiltInFunctionTriplet(visit, "outerProduct(", useEmulatedFunction);
965 break;
966
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700967 case EOpLessThan:
968 writeBuiltInFunctionTriplet(visit, "lessThan(", useEmulatedFunction);
969 break;
970 case EOpGreaterThan:
971 writeBuiltInFunctionTriplet(visit, "greaterThan(", useEmulatedFunction);
972 break;
973 case EOpLessThanEqual:
974 writeBuiltInFunctionTriplet(visit, "lessThanEqual(", useEmulatedFunction);
975 break;
976 case EOpGreaterThanEqual:
977 writeBuiltInFunctionTriplet(visit, "greaterThanEqual(", useEmulatedFunction);
978 break;
979 case EOpVectorEqual:
980 writeBuiltInFunctionTriplet(visit, "equal(", useEmulatedFunction);
981 break;
982 case EOpVectorNotEqual:
983 writeBuiltInFunctionTriplet(visit, "notEqual(", useEmulatedFunction);
984 break;
985 case EOpComma:
Zhenyao Mo0783efd2014-10-21 15:51:59 -0700986 writeTriplet(visit, "(", ", ", ")");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700987 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000988
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700989 case EOpMod:
990 writeBuiltInFunctionTriplet(visit, "mod(", useEmulatedFunction);
991 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +0200992 case EOpModf:
993 writeBuiltInFunctionTriplet(visit, "modf(", useEmulatedFunction);
994 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700995 case EOpPow:
996 writeBuiltInFunctionTriplet(visit, "pow(", useEmulatedFunction);
997 break;
998 case EOpAtan:
999 writeBuiltInFunctionTriplet(visit, "atan(", useEmulatedFunction);
1000 break;
1001 case EOpMin:
1002 writeBuiltInFunctionTriplet(visit, "min(", useEmulatedFunction);
1003 break;
1004 case EOpMax:
1005 writeBuiltInFunctionTriplet(visit, "max(", useEmulatedFunction);
1006 break;
1007 case EOpClamp:
1008 writeBuiltInFunctionTriplet(visit, "clamp(", useEmulatedFunction);
1009 break;
1010 case EOpMix:
1011 writeBuiltInFunctionTriplet(visit, "mix(", useEmulatedFunction);
1012 break;
1013 case EOpStep:
1014 writeBuiltInFunctionTriplet(visit, "step(", useEmulatedFunction);
1015 break;
1016 case EOpSmoothStep:
1017 writeBuiltInFunctionTriplet(visit, "smoothstep(", useEmulatedFunction);
1018 break;
1019 case EOpDistance:
1020 writeBuiltInFunctionTriplet(visit, "distance(", useEmulatedFunction);
1021 break;
1022 case EOpDot:
1023 writeBuiltInFunctionTriplet(visit, "dot(", useEmulatedFunction);
1024 break;
1025 case EOpCross:
1026 writeBuiltInFunctionTriplet(visit, "cross(", useEmulatedFunction);
1027 break;
1028 case EOpFaceForward:
1029 writeBuiltInFunctionTriplet(visit, "faceforward(", useEmulatedFunction);
1030 break;
1031 case EOpReflect:
1032 writeBuiltInFunctionTriplet(visit, "reflect(", useEmulatedFunction);
1033 break;
1034 case EOpRefract:
1035 writeBuiltInFunctionTriplet(visit, "refract(", useEmulatedFunction);
1036 break;
1037 case EOpMul:
1038 writeBuiltInFunctionTriplet(visit, "matrixCompMult(", useEmulatedFunction);
1039 break;
zmo@google.com5601ea02011-06-10 18:23:25 +00001040
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001041 default:
1042 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +00001043 }
zmo@google.com5601ea02011-06-10 18:23:25 +00001044 return visitChildren;
1045}
1046
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001047bool TOutputGLSLBase::visitLoop(Visit visit, TIntermLoop *node)
zmo@google.com5601ea02011-06-10 18:23:25 +00001048{
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001049 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +00001050
Zhenyao Mo7cab38b2013-10-15 12:59:30 -07001051 incrementDepth(node);
zmo@google.com5601ea02011-06-10 18:23:25 +00001052 // Loop header.
1053 TLoopType loopType = node->getType();
1054 if (loopType == ELoopFor) // for loop
1055 {
Zhenyao Mo550c6002014-02-26 15:40:48 -08001056 if (!node->getUnrollFlag())
1057 {
zmo@google.com5601ea02011-06-10 18:23:25 +00001058 out << "for (";
1059 if (node->getInit())
1060 node->getInit()->traverse(this);
1061 out << "; ";
1062
1063 if (node->getCondition())
1064 node->getCondition()->traverse(this);
1065 out << "; ";
1066
1067 if (node->getExpression())
1068 node->getExpression()->traverse(this);
1069 out << ")\n";
1070 }
Zhenyao Mo550c6002014-02-26 15:40:48 -08001071 else
1072 {
1073 // Need to put a one-iteration loop here to handle break.
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001074 TIntermSequence *declSeq =
Zhenyao Mo550c6002014-02-26 15:40:48 -08001075 node->getInit()->getAsAggregate()->getSequence();
1076 TIntermSymbol *indexSymbol =
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001077 (*declSeq)[0]->getAsBinaryNode()->getLeft()->getAsSymbolNode();
Zhenyao Mo550c6002014-02-26 15:40:48 -08001078 TString name = hashVariableName(indexSymbol->getSymbol());
1079 out << "for (int " << name << " = 0; "
1080 << name << " < 1; "
1081 << "++" << name << ")\n";
1082 }
zmo@google.com5601ea02011-06-10 18:23:25 +00001083 }
1084 else if (loopType == ELoopWhile) // while loop
1085 {
1086 out << "while (";
1087 ASSERT(node->getCondition() != NULL);
1088 node->getCondition()->traverse(this);
1089 out << ")\n";
1090 }
1091 else // do-while loop
1092 {
1093 ASSERT(loopType == ELoopDoWhile);
1094 out << "do\n";
1095 }
1096
1097 // Loop body.
1098 if (node->getUnrollFlag())
1099 {
Zhenyao Mo550c6002014-02-26 15:40:48 -08001100 out << "{\n";
1101 mLoopUnrollStack.push(node);
1102 while (mLoopUnrollStack.satisfiesLoopCondition())
zmo@google.com5601ea02011-06-10 18:23:25 +00001103 {
1104 visitCodeBlock(node->getBody());
Zhenyao Mo550c6002014-02-26 15:40:48 -08001105 mLoopUnrollStack.step();
zmo@google.com5601ea02011-06-10 18:23:25 +00001106 }
Zhenyao Mo550c6002014-02-26 15:40:48 -08001107 mLoopUnrollStack.pop();
1108 out << "}\n";
zmo@google.com5601ea02011-06-10 18:23:25 +00001109 }
1110 else
1111 {
1112 visitCodeBlock(node->getBody());
1113 }
1114
1115 // Loop footer.
1116 if (loopType == ELoopDoWhile) // do-while loop
1117 {
1118 out << "while (";
1119 ASSERT(node->getCondition() != NULL);
1120 node->getCondition()->traverse(this);
1121 out << ");\n";
1122 }
1123 decrementDepth();
1124
1125 // No need to visit children. They have been already processed in
1126 // this function.
1127 return false;
1128}
1129
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001130bool TOutputGLSLBase::visitBranch(Visit visit, TIntermBranch *node)
zmo@google.com5601ea02011-06-10 18:23:25 +00001131{
1132 switch (node->getFlowOp())
1133 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001134 case EOpKill:
1135 writeTriplet(visit, "discard", NULL, NULL);
1136 break;
1137 case EOpBreak:
1138 writeTriplet(visit, "break", NULL, NULL);
1139 break;
1140 case EOpContinue:
1141 writeTriplet(visit, "continue", NULL, NULL);
1142 break;
1143 case EOpReturn:
1144 writeTriplet(visit, "return ", NULL, NULL);
1145 break;
1146 default:
1147 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +00001148 }
1149
1150 return true;
1151}
1152
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001153void TOutputGLSLBase::visitCodeBlock(TIntermNode *node)
1154{
zmo@google.com5601ea02011-06-10 18:23:25 +00001155 TInfoSinkBase &out = objSink();
1156 if (node != NULL)
1157 {
1158 node->traverse(this);
1159 // Single statements not part of a sequence need to be terminated
1160 // with semi-colon.
1161 if (isSingleStatement(node))
1162 out << ";\n";
1163 }
1164 else
1165 {
1166 out << "{\n}\n"; // Empty code block.
1167 }
1168}
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001169
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001170TString TOutputGLSLBase::getTypeName(const TType &type)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001171{
1172 TInfoSinkBase out;
1173 if (type.isMatrix())
1174 {
1175 out << "mat";
1176 out << type.getNominalSize();
1177 }
1178 else if (type.isVector())
1179 {
1180 switch (type.getBasicType())
1181 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001182 case EbtFloat:
1183 out << "vec";
1184 break;
1185 case EbtInt:
1186 out << "ivec";
1187 break;
1188 case EbtBool:
1189 out << "bvec";
1190 break;
Geoff Langc4c7442d2015-07-20 13:09:26 -04001191 case EbtUInt:
1192 out << "uvec";
1193 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001194 default:
1195 UNREACHABLE();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001196 }
1197 out << type.getNominalSize();
1198 }
1199 else
1200 {
1201 if (type.getBasicType() == EbtStruct)
Jamie Madill98493dd2013-07-08 14:39:03 -04001202 out << hashName(type.getStruct()->name());
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001203 else
1204 out << type.getBasicString();
1205 }
1206 return TString(out.c_str());
1207}
1208
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001209TString TOutputGLSLBase::hashName(const TString &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001210{
1211 if (mHashFunction == NULL || name.empty())
1212 return name;
1213 NameMap::const_iterator it = mNameMap.find(name.c_str());
1214 if (it != mNameMap.end())
1215 return it->second.c_str();
1216 TString hashedName = TIntermTraverser::hash(name, mHashFunction);
1217 mNameMap[name.c_str()] = hashedName.c_str();
1218 return hashedName;
1219}
1220
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001221TString TOutputGLSLBase::hashVariableName(const TString &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001222{
Jamie Madill02f20dd2013-09-12 12:07:42 -04001223 if (mSymbolTable.findBuiltIn(name, mShaderVersion) != NULL)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001224 return name;
1225 return hashName(name);
1226}
1227
Olli Etuaho59f9a642015-08-06 20:38:26 +03001228TString TOutputGLSLBase::hashFunctionNameIfNeeded(const TName &mangledName)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001229{
Olli Etuaho59f9a642015-08-06 20:38:26 +03001230 TString mangledStr = mangledName.getString();
1231 TString name = TFunction::unmangleName(mangledStr);
1232 if (mSymbolTable.findBuiltIn(mangledStr, mShaderVersion) != nullptr || name == "main")
Nicolas Capens46485082014-04-15 13:12:50 -04001233 return translateTextureFunction(name);
Olli Etuaho59f9a642015-08-06 20:38:26 +03001234 if (mangledName.isInternal())
1235 return name;
1236 else
1237 return hashName(name);
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001238}
Jamie Madill98493dd2013-07-08 14:39:03 -04001239
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001240bool TOutputGLSLBase::structDeclared(const TStructure *structure) const
Jamie Madill98493dd2013-07-08 14:39:03 -04001241{
Zhenyao Mo904a9162014-05-09 14:07:45 -07001242 ASSERT(structure);
Jamie Madill01f85ac2014-06-06 11:55:04 -04001243 if (structure->name().empty())
Zhenyao Mo904a9162014-05-09 14:07:45 -07001244 {
Jamie Madill01f85ac2014-06-06 11:55:04 -04001245 return false;
Zhenyao Mo904a9162014-05-09 14:07:45 -07001246 }
Jamie Madill01f85ac2014-06-06 11:55:04 -04001247
1248 return (mDeclaredStructs.count(structure->uniqueId()) > 0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001249}
1250
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001251void TOutputGLSLBase::declareStruct(const TStructure *structure)
Jamie Madill98493dd2013-07-08 14:39:03 -04001252{
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001253 TInfoSinkBase &out = objSink();
Jamie Madill98493dd2013-07-08 14:39:03 -04001254
1255 out << "struct " << hashName(structure->name()) << "{\n";
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001256 const TFieldList &fields = structure->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04001257 for (size_t i = 0; i < fields.size(); ++i)
1258 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001259 const TField *field = fields[i];
Jamie Madill98493dd2013-07-08 14:39:03 -04001260 if (writeVariablePrecision(field->type()->getPrecision()))
1261 out << " ";
1262 out << getTypeName(*field->type()) << " " << hashName(field->name());
1263 if (field->type()->isArray())
1264 out << arrayBrackets(*field->type());
1265 out << ";\n";
1266 }
1267 out << "}";
Zhenyao Mo904a9162014-05-09 14:07:45 -07001268}
Jamie Madill98493dd2013-07-08 14:39:03 -04001269