blob: 9f49430676e0d67fe0238f66f26c1954e5b043dd [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 Etuaho76acee82014-11-04 13:44:03 +0200786 out << " " << hashFunctionName(node->getName());
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
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700804 out << " " << hashFunctionName(node->getName());
805
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)
833 out << hashFunctionName(node->getName()) << "(";
834 else if (visit == InVisit)
835 out << ", ";
836 else
zmo@google.com5601ea02011-06-10 18:23:25 +0000837 out << ")";
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700838 break;
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200839 case EOpInternalFunctionCall:
840 // Function call to an internal helper function.
841 if (visit == PreVisit)
842 out << node->getName() << "(";
843 else if (visit == InVisit)
844 out << ", ";
845 else
846 out << ")";
847 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700848 case EOpParameters:
849 // Function parameters.
850 ASSERT(visit == PreVisit);
851 out << "(";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700852 writeFunctionParameters(*(node->getSequence()));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700853 out << ")";
854 visitChildren = false;
855 break;
856 case EOpDeclaration:
857 // Variable declaration.
858 if (visit == PreVisit)
859 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700860 const TIntermSequence &sequence = *(node->getSequence());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700861 const TIntermTyped *variable = sequence.front()->getAsTyped();
862 writeVariableType(variable->getType());
863 out << " ";
864 mDeclaringVariables = true;
zmo@google.com5601ea02011-06-10 18:23:25 +0000865 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700866 else if (visit == InVisit)
867 {
868 out << ", ";
869 mDeclaringVariables = true;
zmo@google.com5601ea02011-06-10 18:23:25 +0000870 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700871 else
872 {
873 mDeclaringVariables = false;
874 }
875 break;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700876 case EOpInvariantDeclaration:
877 // Invariant declaration.
878 ASSERT(visit == PreVisit);
879 {
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400880 const TIntermSequence *sequence = node->getSequence();
881 ASSERT(sequence && sequence->size() == 1);
882 const TIntermSymbol *symbol = sequence->front()->getAsSymbolNode();
883 ASSERT(symbol);
Zhenyao Mo2a517272014-10-27 16:09:57 -0700884 out << "invariant " << hashVariableName(symbol->getSymbol());
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400885 }
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700886 visitChildren = false;
887 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700888 case EOpConstructFloat:
Olli Etuahof40319e2015-03-10 14:33:00 +0200889 writeConstructorTriplet(visit, node->getType(), "float");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700890 break;
891 case EOpConstructVec2:
Olli Etuahof40319e2015-03-10 14:33:00 +0200892 writeConstructorTriplet(visit, node->getType(), "vec2");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700893 break;
894 case EOpConstructVec3:
Olli Etuahof40319e2015-03-10 14:33:00 +0200895 writeConstructorTriplet(visit, node->getType(), "vec3");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700896 break;
897 case EOpConstructVec4:
Olli Etuahof40319e2015-03-10 14:33:00 +0200898 writeConstructorTriplet(visit, node->getType(), "vec4");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700899 break;
900 case EOpConstructBool:
Olli Etuahof40319e2015-03-10 14:33:00 +0200901 writeConstructorTriplet(visit, node->getType(), "bool");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700902 break;
903 case EOpConstructBVec2:
Olli Etuahof40319e2015-03-10 14:33:00 +0200904 writeConstructorTriplet(visit, node->getType(), "bvec2");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700905 break;
906 case EOpConstructBVec3:
Olli Etuahof40319e2015-03-10 14:33:00 +0200907 writeConstructorTriplet(visit, node->getType(), "bvec3");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700908 break;
909 case EOpConstructBVec4:
Olli Etuahof40319e2015-03-10 14:33:00 +0200910 writeConstructorTriplet(visit, node->getType(), "bvec4");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700911 break;
912 case EOpConstructInt:
Olli Etuahof40319e2015-03-10 14:33:00 +0200913 writeConstructorTriplet(visit, node->getType(), "int");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700914 break;
915 case EOpConstructIVec2:
Olli Etuahof40319e2015-03-10 14:33:00 +0200916 writeConstructorTriplet(visit, node->getType(), "ivec2");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700917 break;
918 case EOpConstructIVec3:
Olli Etuahof40319e2015-03-10 14:33:00 +0200919 writeConstructorTriplet(visit, node->getType(), "ivec3");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700920 break;
921 case EOpConstructIVec4:
Olli Etuahof40319e2015-03-10 14:33:00 +0200922 writeConstructorTriplet(visit, node->getType(), "ivec4");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700923 break;
924 case EOpConstructMat2:
Olli Etuahof40319e2015-03-10 14:33:00 +0200925 writeConstructorTriplet(visit, node->getType(), "mat2");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700926 break;
Alexis Hetu07e57df2015-06-16 16:55:52 -0400927 case EOpConstructMat2x3:
928 writeConstructorTriplet(visit, node->getType(), "mat2x3");
929 break;
930 case EOpConstructMat2x4:
931 writeConstructorTriplet(visit, node->getType(), "mat2x4");
932 break;
933 case EOpConstructMat3x2:
934 writeConstructorTriplet(visit, node->getType(), "mat3x2");
935 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700936 case EOpConstructMat3:
Olli Etuahof40319e2015-03-10 14:33:00 +0200937 writeConstructorTriplet(visit, node->getType(), "mat3");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700938 break;
Alexis Hetu07e57df2015-06-16 16:55:52 -0400939 case EOpConstructMat3x4:
940 writeConstructorTriplet(visit, node->getType(), "mat3x4");
941 break;
942 case EOpConstructMat4x2:
943 writeConstructorTriplet(visit, node->getType(), "mat4x2");
944 break;
945 case EOpConstructMat4x3:
946 writeConstructorTriplet(visit, node->getType(), "mat4x3");
947 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700948 case EOpConstructMat4:
Olli Etuahof40319e2015-03-10 14:33:00 +0200949 writeConstructorTriplet(visit, node->getType(), "mat4");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700950 break;
951 case EOpConstructStruct:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700952 {
953 const TType &type = node->getType();
954 ASSERT(type.getBasicType() == EbtStruct);
Olli Etuahof40319e2015-03-10 14:33:00 +0200955 TString constructorName = hashName(type.getStruct()->name());
956 writeConstructorTriplet(visit, node->getType(), constructorName.c_str());
957 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700958 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000959
Olli Etuahoe39706d2014-12-30 16:40:36 +0200960 case EOpOuterProduct:
961 writeBuiltInFunctionTriplet(visit, "outerProduct(", useEmulatedFunction);
962 break;
963
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700964 case EOpLessThan:
965 writeBuiltInFunctionTriplet(visit, "lessThan(", useEmulatedFunction);
966 break;
967 case EOpGreaterThan:
968 writeBuiltInFunctionTriplet(visit, "greaterThan(", useEmulatedFunction);
969 break;
970 case EOpLessThanEqual:
971 writeBuiltInFunctionTriplet(visit, "lessThanEqual(", useEmulatedFunction);
972 break;
973 case EOpGreaterThanEqual:
974 writeBuiltInFunctionTriplet(visit, "greaterThanEqual(", useEmulatedFunction);
975 break;
976 case EOpVectorEqual:
977 writeBuiltInFunctionTriplet(visit, "equal(", useEmulatedFunction);
978 break;
979 case EOpVectorNotEqual:
980 writeBuiltInFunctionTriplet(visit, "notEqual(", useEmulatedFunction);
981 break;
982 case EOpComma:
Zhenyao Mo0783efd2014-10-21 15:51:59 -0700983 writeTriplet(visit, "(", ", ", ")");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700984 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000985
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700986 case EOpMod:
987 writeBuiltInFunctionTriplet(visit, "mod(", useEmulatedFunction);
988 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +0200989 case EOpModf:
990 writeBuiltInFunctionTriplet(visit, "modf(", useEmulatedFunction);
991 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700992 case EOpPow:
993 writeBuiltInFunctionTriplet(visit, "pow(", useEmulatedFunction);
994 break;
995 case EOpAtan:
996 writeBuiltInFunctionTriplet(visit, "atan(", useEmulatedFunction);
997 break;
998 case EOpMin:
999 writeBuiltInFunctionTriplet(visit, "min(", useEmulatedFunction);
1000 break;
1001 case EOpMax:
1002 writeBuiltInFunctionTriplet(visit, "max(", useEmulatedFunction);
1003 break;
1004 case EOpClamp:
1005 writeBuiltInFunctionTriplet(visit, "clamp(", useEmulatedFunction);
1006 break;
1007 case EOpMix:
1008 writeBuiltInFunctionTriplet(visit, "mix(", useEmulatedFunction);
1009 break;
1010 case EOpStep:
1011 writeBuiltInFunctionTriplet(visit, "step(", useEmulatedFunction);
1012 break;
1013 case EOpSmoothStep:
1014 writeBuiltInFunctionTriplet(visit, "smoothstep(", useEmulatedFunction);
1015 break;
1016 case EOpDistance:
1017 writeBuiltInFunctionTriplet(visit, "distance(", useEmulatedFunction);
1018 break;
1019 case EOpDot:
1020 writeBuiltInFunctionTriplet(visit, "dot(", useEmulatedFunction);
1021 break;
1022 case EOpCross:
1023 writeBuiltInFunctionTriplet(visit, "cross(", useEmulatedFunction);
1024 break;
1025 case EOpFaceForward:
1026 writeBuiltInFunctionTriplet(visit, "faceforward(", useEmulatedFunction);
1027 break;
1028 case EOpReflect:
1029 writeBuiltInFunctionTriplet(visit, "reflect(", useEmulatedFunction);
1030 break;
1031 case EOpRefract:
1032 writeBuiltInFunctionTriplet(visit, "refract(", useEmulatedFunction);
1033 break;
1034 case EOpMul:
1035 writeBuiltInFunctionTriplet(visit, "matrixCompMult(", useEmulatedFunction);
1036 break;
zmo@google.com5601ea02011-06-10 18:23:25 +00001037
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001038 default:
1039 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +00001040 }
zmo@google.com5601ea02011-06-10 18:23:25 +00001041 return visitChildren;
1042}
1043
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001044bool TOutputGLSLBase::visitLoop(Visit visit, TIntermLoop *node)
zmo@google.com5601ea02011-06-10 18:23:25 +00001045{
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001046 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +00001047
Zhenyao Mo7cab38b2013-10-15 12:59:30 -07001048 incrementDepth(node);
zmo@google.com5601ea02011-06-10 18:23:25 +00001049 // Loop header.
1050 TLoopType loopType = node->getType();
1051 if (loopType == ELoopFor) // for loop
1052 {
Zhenyao Mo550c6002014-02-26 15:40:48 -08001053 if (!node->getUnrollFlag())
1054 {
zmo@google.com5601ea02011-06-10 18:23:25 +00001055 out << "for (";
1056 if (node->getInit())
1057 node->getInit()->traverse(this);
1058 out << "; ";
1059
1060 if (node->getCondition())
1061 node->getCondition()->traverse(this);
1062 out << "; ";
1063
1064 if (node->getExpression())
1065 node->getExpression()->traverse(this);
1066 out << ")\n";
1067 }
Zhenyao Mo550c6002014-02-26 15:40:48 -08001068 else
1069 {
1070 // Need to put a one-iteration loop here to handle break.
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001071 TIntermSequence *declSeq =
Zhenyao Mo550c6002014-02-26 15:40:48 -08001072 node->getInit()->getAsAggregate()->getSequence();
1073 TIntermSymbol *indexSymbol =
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001074 (*declSeq)[0]->getAsBinaryNode()->getLeft()->getAsSymbolNode();
Zhenyao Mo550c6002014-02-26 15:40:48 -08001075 TString name = hashVariableName(indexSymbol->getSymbol());
1076 out << "for (int " << name << " = 0; "
1077 << name << " < 1; "
1078 << "++" << name << ")\n";
1079 }
zmo@google.com5601ea02011-06-10 18:23:25 +00001080 }
1081 else if (loopType == ELoopWhile) // while loop
1082 {
1083 out << "while (";
1084 ASSERT(node->getCondition() != NULL);
1085 node->getCondition()->traverse(this);
1086 out << ")\n";
1087 }
1088 else // do-while loop
1089 {
1090 ASSERT(loopType == ELoopDoWhile);
1091 out << "do\n";
1092 }
1093
1094 // Loop body.
1095 if (node->getUnrollFlag())
1096 {
Zhenyao Mo550c6002014-02-26 15:40:48 -08001097 out << "{\n";
1098 mLoopUnrollStack.push(node);
1099 while (mLoopUnrollStack.satisfiesLoopCondition())
zmo@google.com5601ea02011-06-10 18:23:25 +00001100 {
1101 visitCodeBlock(node->getBody());
Zhenyao Mo550c6002014-02-26 15:40:48 -08001102 mLoopUnrollStack.step();
zmo@google.com5601ea02011-06-10 18:23:25 +00001103 }
Zhenyao Mo550c6002014-02-26 15:40:48 -08001104 mLoopUnrollStack.pop();
1105 out << "}\n";
zmo@google.com5601ea02011-06-10 18:23:25 +00001106 }
1107 else
1108 {
1109 visitCodeBlock(node->getBody());
1110 }
1111
1112 // Loop footer.
1113 if (loopType == ELoopDoWhile) // do-while loop
1114 {
1115 out << "while (";
1116 ASSERT(node->getCondition() != NULL);
1117 node->getCondition()->traverse(this);
1118 out << ");\n";
1119 }
1120 decrementDepth();
1121
1122 // No need to visit children. They have been already processed in
1123 // this function.
1124 return false;
1125}
1126
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001127bool TOutputGLSLBase::visitBranch(Visit visit, TIntermBranch *node)
zmo@google.com5601ea02011-06-10 18:23:25 +00001128{
1129 switch (node->getFlowOp())
1130 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001131 case EOpKill:
1132 writeTriplet(visit, "discard", NULL, NULL);
1133 break;
1134 case EOpBreak:
1135 writeTriplet(visit, "break", NULL, NULL);
1136 break;
1137 case EOpContinue:
1138 writeTriplet(visit, "continue", NULL, NULL);
1139 break;
1140 case EOpReturn:
1141 writeTriplet(visit, "return ", NULL, NULL);
1142 break;
1143 default:
1144 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +00001145 }
1146
1147 return true;
1148}
1149
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001150void TOutputGLSLBase::visitCodeBlock(TIntermNode *node)
1151{
zmo@google.com5601ea02011-06-10 18:23:25 +00001152 TInfoSinkBase &out = objSink();
1153 if (node != NULL)
1154 {
1155 node->traverse(this);
1156 // Single statements not part of a sequence need to be terminated
1157 // with semi-colon.
1158 if (isSingleStatement(node))
1159 out << ";\n";
1160 }
1161 else
1162 {
1163 out << "{\n}\n"; // Empty code block.
1164 }
1165}
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001166
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001167TString TOutputGLSLBase::getTypeName(const TType &type)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001168{
1169 TInfoSinkBase out;
1170 if (type.isMatrix())
1171 {
1172 out << "mat";
1173 out << type.getNominalSize();
1174 }
1175 else if (type.isVector())
1176 {
1177 switch (type.getBasicType())
1178 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001179 case EbtFloat:
1180 out << "vec";
1181 break;
1182 case EbtInt:
1183 out << "ivec";
1184 break;
1185 case EbtBool:
1186 out << "bvec";
1187 break;
1188 default:
1189 UNREACHABLE();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001190 }
1191 out << type.getNominalSize();
1192 }
1193 else
1194 {
1195 if (type.getBasicType() == EbtStruct)
Jamie Madill98493dd2013-07-08 14:39:03 -04001196 out << hashName(type.getStruct()->name());
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001197 else
1198 out << type.getBasicString();
1199 }
1200 return TString(out.c_str());
1201}
1202
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001203TString TOutputGLSLBase::hashName(const TString &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001204{
1205 if (mHashFunction == NULL || name.empty())
1206 return name;
1207 NameMap::const_iterator it = mNameMap.find(name.c_str());
1208 if (it != mNameMap.end())
1209 return it->second.c_str();
1210 TString hashedName = TIntermTraverser::hash(name, mHashFunction);
1211 mNameMap[name.c_str()] = hashedName.c_str();
1212 return hashedName;
1213}
1214
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001215TString TOutputGLSLBase::hashVariableName(const TString &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001216{
Jamie Madill02f20dd2013-09-12 12:07:42 -04001217 if (mSymbolTable.findBuiltIn(name, mShaderVersion) != NULL)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001218 return name;
1219 return hashName(name);
1220}
1221
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001222TString TOutputGLSLBase::hashFunctionName(const TString &mangled_name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001223{
1224 TString name = TFunction::unmangleName(mangled_name);
Jamie Madill02f20dd2013-09-12 12:07:42 -04001225 if (mSymbolTable.findBuiltIn(mangled_name, mShaderVersion) != NULL || name == "main")
Nicolas Capens46485082014-04-15 13:12:50 -04001226 return translateTextureFunction(name);
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001227 return hashName(name);
1228}
Jamie Madill98493dd2013-07-08 14:39:03 -04001229
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001230bool TOutputGLSLBase::structDeclared(const TStructure *structure) const
Jamie Madill98493dd2013-07-08 14:39:03 -04001231{
Zhenyao Mo904a9162014-05-09 14:07:45 -07001232 ASSERT(structure);
Jamie Madill01f85ac2014-06-06 11:55:04 -04001233 if (structure->name().empty())
Zhenyao Mo904a9162014-05-09 14:07:45 -07001234 {
Jamie Madill01f85ac2014-06-06 11:55:04 -04001235 return false;
Zhenyao Mo904a9162014-05-09 14:07:45 -07001236 }
Jamie Madill01f85ac2014-06-06 11:55:04 -04001237
1238 return (mDeclaredStructs.count(structure->uniqueId()) > 0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001239}
1240
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001241void TOutputGLSLBase::declareStruct(const TStructure *structure)
Jamie Madill98493dd2013-07-08 14:39:03 -04001242{
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001243 TInfoSinkBase &out = objSink();
Jamie Madill98493dd2013-07-08 14:39:03 -04001244
1245 out << "struct " << hashName(structure->name()) << "{\n";
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001246 const TFieldList &fields = structure->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04001247 for (size_t i = 0; i < fields.size(); ++i)
1248 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001249 const TField *field = fields[i];
Jamie Madill98493dd2013-07-08 14:39:03 -04001250 if (writeVariablePrecision(field->type()->getPrecision()))
1251 out << " ";
1252 out << getTypeName(*field->type()) << " " << hashName(field->name());
1253 if (field->type()->isArray())
1254 out << arrayBrackets(*field->type());
1255 out << ";\n";
1256 }
1257 out << "}";
Zhenyao Mo904a9162014-05-09 14:07:45 -07001258}
Jamie Madill98493dd2013-07-08 14:39:03 -04001259