blob: 296eef7d1e134a89ea8f86c45a51ea11661e6e3e [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
Olli Etuahoae69d7e2015-10-07 17:19:50 +030092void TOutputGLSLBase::writeLayoutQualifier(const TType &type)
93{
94 if (type.getQualifier() == EvqFragmentOut || type.getQualifier() == EvqVertexIn)
95 {
96 const TLayoutQualifier &layoutQualifier = type.getLayoutQualifier();
97 if (layoutQualifier.location >= 0)
98 {
99 TInfoSinkBase &out = objSink();
100 out << "layout(location = " << layoutQualifier.location << ") ";
101 }
102 }
103}
104
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700105void TOutputGLSLBase::writeVariableType(const TType &type)
106{
107 TInfoSinkBase &out = objSink();
Olli Etuaho214c2d82015-04-27 14:49:13 +0300108 if (type.isInvariant())
109 {
110 out << "invariant ";
111 }
Geoff Langbdcc54a2015-09-02 13:09:48 -0400112 if (type.getBasicType() == EbtInterfaceBlock)
113 {
114 TInterfaceBlock *interfaceBlock = type.getInterfaceBlock();
115 declareInterfaceBlockLayout(interfaceBlock);
116 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000117 TQualifier qualifier = type.getQualifier();
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400118 if (qualifier != EvqTemporary && qualifier != EvqGlobal)
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400119 {
Qingqing Dengad0d0792015-04-08 14:25:06 -0700120 if (IsGLSL130OrNewer(mOutput))
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800121 {
122 switch (qualifier)
123 {
124 case EvqAttribute:
Olli Etuaho214c2d82015-04-27 14:49:13 +0300125 out << "in ";
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800126 break;
127 case EvqVaryingIn:
Olli Etuaho214c2d82015-04-27 14:49:13 +0300128 out << "in ";
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800129 break;
130 case EvqVaryingOut:
Olli Etuaho214c2d82015-04-27 14:49:13 +0300131 out << "out ";
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800132 break;
133 default:
134 out << type.getQualifierString() << " ";
135 break;
136 }
137 }
138 else
139 {
140 out << type.getQualifierString() << " ";
141 }
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400142 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000143 // Declare the struct if we have not done so already.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700144 if (type.getBasicType() == EbtStruct && !structDeclared(type.getStruct()))
zmo@google.com5601ea02011-06-10 18:23:25 +0000145 {
Jamie Madill01f85ac2014-06-06 11:55:04 -0400146 TStructure *structure = type.getStruct();
147
148 declareStruct(structure);
149
150 if (!structure->name().empty())
151 {
152 mDeclaredStructs.insert(structure->uniqueId());
153 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000154 }
Geoff Langbdcc54a2015-09-02 13:09:48 -0400155 else if (type.getBasicType() == EbtInterfaceBlock)
156 {
157 TInterfaceBlock *interfaceBlock = type.getInterfaceBlock();
158 declareInterfaceBlock(interfaceBlock);
159 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000160 else
161 {
162 if (writeVariablePrecision(type.getPrecision()))
163 out << " ";
164 out << getTypeName(type);
165 }
166}
167
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700168void TOutputGLSLBase::writeFunctionParameters(const TIntermSequence &args)
zmo@google.com5601ea02011-06-10 18:23:25 +0000169{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700170 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000171 for (TIntermSequence::const_iterator iter = args.begin();
172 iter != args.end(); ++iter)
173 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700174 const TIntermSymbol *arg = (*iter)->getAsSymbolNode();
zmo@google.com5601ea02011-06-10 18:23:25 +0000175 ASSERT(arg != NULL);
176
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700177 const TType &type = arg->getType();
zmo@google.com189be2f2011-06-16 18:28:53 +0000178 writeVariableType(type);
zmo@google.com5601ea02011-06-10 18:23:25 +0000179
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700180 const TString &name = arg->getSymbol();
zmo@google.com5601ea02011-06-10 18:23:25 +0000181 if (!name.empty())
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000182 out << " " << hashName(name);
zmo@google.com5601ea02011-06-10 18:23:25 +0000183 if (type.isArray())
184 out << arrayBrackets(type);
185
186 // Put a comma if this is not the last argument.
187 if (iter != args.end() - 1)
188 out << ", ";
189 }
190}
191
Jamie Madill6ba6ead2015-05-04 14:21:21 -0400192const TConstantUnion *TOutputGLSLBase::writeConstantUnion(
193 const TType &type, const TConstantUnion *pConstUnion)
zmo@google.com5601ea02011-06-10 18:23:25 +0000194{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700195 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000196
197 if (type.getBasicType() == EbtStruct)
198 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700199 const TStructure *structure = type.getStruct();
Jamie Madill98493dd2013-07-08 14:39:03 -0400200 out << hashName(structure->name()) << "(";
201
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700202 const TFieldList &fields = structure->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -0400203 for (size_t i = 0; i < fields.size(); ++i)
zmo@google.com5601ea02011-06-10 18:23:25 +0000204 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700205 const TType *fieldType = fields[i]->type();
zmo@google.com5601ea02011-06-10 18:23:25 +0000206 ASSERT(fieldType != NULL);
207 pConstUnion = writeConstantUnion(*fieldType, pConstUnion);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700208 if (i != fields.size() - 1)
209 out << ", ";
zmo@google.com5601ea02011-06-10 18:23:25 +0000210 }
211 out << ")";
212 }
213 else
214 {
Jamie Madill94bf7f22013-07-08 13:31:15 -0400215 size_t size = type.getObjectSize();
zmo@google.com5601ea02011-06-10 18:23:25 +0000216 bool writeType = size > 1;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700217 if (writeType)
218 out << getTypeName(type) << "(";
Jamie Madill94bf7f22013-07-08 13:31:15 -0400219 for (size_t i = 0; i < size; ++i, ++pConstUnion)
zmo@google.com5601ea02011-06-10 18:23:25 +0000220 {
221 switch (pConstUnion->getType())
222 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700223 case EbtFloat:
224 out << std::min(FLT_MAX, std::max(-FLT_MAX, pConstUnion->getFConst()));
225 break;
226 case EbtInt:
227 out << pConstUnion->getIConst();
228 break;
Olli Etuaho5321c802015-04-02 17:08:16 +0300229 case EbtUInt:
230 out << pConstUnion->getUConst() << "u";
231 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700232 case EbtBool:
233 out << pConstUnion->getBConst();
234 break;
235 default: UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000236 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700237 if (i != size - 1)
238 out << ", ";
zmo@google.com5601ea02011-06-10 18:23:25 +0000239 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700240 if (writeType)
241 out << ")";
zmo@google.com5601ea02011-06-10 18:23:25 +0000242 }
243 return pConstUnion;
244}
245
Olli Etuahoe92507b2016-07-04 11:20:10 +0300246void TOutputGLSLBase::writeConstructorTriplet(Visit visit, const TType &type)
Olli Etuahof40319e2015-03-10 14:33:00 +0200247{
248 TInfoSinkBase &out = objSink();
249 if (visit == PreVisit)
250 {
251 if (type.isArray())
252 {
Olli Etuahoe92507b2016-07-04 11:20:10 +0300253 out << getTypeName(type);
Olli Etuahof40319e2015-03-10 14:33:00 +0200254 out << arrayBrackets(type);
255 out << "(";
256 }
257 else
258 {
Olli Etuahoe92507b2016-07-04 11:20:10 +0300259 out << getTypeName(type) << "(";
Olli Etuahof40319e2015-03-10 14:33:00 +0200260 }
261 }
262 else
263 {
264 writeTriplet(visit, nullptr, ", ", ")");
265 }
266}
267
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700268void TOutputGLSLBase::visitSymbol(TIntermSymbol *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000269{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700270 TInfoSinkBase &out = objSink();
Zhenyao Mo550c6002014-02-26 15:40:48 -0800271 if (mLoopUnrollStack.needsToReplaceSymbolWithValue(node))
272 out << mLoopUnrollStack.getLoopIndexValue(node);
zmo@google.com5601ea02011-06-10 18:23:25 +0000273 else
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000274 out << hashVariableName(node->getSymbol());
zmo@google.com5601ea02011-06-10 18:23:25 +0000275
276 if (mDeclaringVariables && node->getType().isArray())
277 out << arrayBrackets(node->getType());
278}
279
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700280void TOutputGLSLBase::visitConstantUnion(TIntermConstantUnion *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000281{
282 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
283}
284
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700285bool TOutputGLSLBase::visitBinary(Visit visit, TIntermBinary *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000286{
287 bool visitChildren = true;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700288 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000289 switch (node->getOp())
290 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700291 case EOpInitialize:
292 if (visit == InVisit)
293 {
294 out << " = ";
295 // RHS of initialize is not being declared.
296 mDeclaringVariables = false;
297 }
298 break;
299 case EOpAssign:
300 writeTriplet(visit, "(", " = ", ")");
301 break;
302 case EOpAddAssign:
303 writeTriplet(visit, "(", " += ", ")");
304 break;
305 case EOpSubAssign:
306 writeTriplet(visit, "(", " -= ", ")");
307 break;
308 case EOpDivAssign:
309 writeTriplet(visit, "(", " /= ", ")");
310 break;
Olli Etuahoff805cc2015-02-13 10:59:34 +0200311 case EOpIModAssign:
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +0000312 writeTriplet(visit, "(", " %= ", ")");
313 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700314 // Notice the fall-through.
315 case EOpMulAssign:
316 case EOpVectorTimesMatrixAssign:
317 case EOpVectorTimesScalarAssign:
318 case EOpMatrixTimesScalarAssign:
319 case EOpMatrixTimesMatrixAssign:
320 writeTriplet(visit, "(", " *= ", ")");
321 break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200322 case EOpBitShiftLeftAssign:
323 writeTriplet(visit, "(", " <<= ", ")");
324 break;
325 case EOpBitShiftRightAssign:
326 writeTriplet(visit, "(", " >>= ", ")");
327 break;
328 case EOpBitwiseAndAssign:
329 writeTriplet(visit, "(", " &= ", ")");
330 break;
331 case EOpBitwiseXorAssign:
332 writeTriplet(visit, "(", " ^= ", ")");
333 break;
334 case EOpBitwiseOrAssign:
335 writeTriplet(visit, "(", " |= ", ")");
336 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700337
338 case EOpIndexDirect:
339 writeTriplet(visit, NULL, "[", "]");
340 break;
341 case EOpIndexIndirect:
342 if (node->getAddIndexClamp())
343 {
zmo@google.com5601ea02011-06-10 18:23:25 +0000344 if (visit == InVisit)
345 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700346 if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC)
347 out << "[int(clamp(float(";
348 else
349 out << "[webgl_int_clamp(";
zmo@google.com5601ea02011-06-10 18:23:25 +0000350 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700351 else if (visit == PostVisit)
352 {
353 int maxSize;
354 TIntermTyped *left = node->getLeft();
355 TType leftType = left->getType();
zmo@google.com5601ea02011-06-10 18:23:25 +0000356
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700357 if (left->isArray())
358 {
359 // The shader will fail validation if the array length is not > 0.
Olli Etuaho856c4972016-08-08 11:38:39 +0300360 maxSize = static_cast<int>(leftType.getArraySize()) - 1;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700361 }
362 else
363 {
364 maxSize = leftType.getNominalSize() - 1;
365 }
366
367 if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC)
368 out << "), 0.0, float(" << maxSize << ")))]";
369 else
370 out << ", 0, " << maxSize << ")]";
371 }
372 }
373 else
374 {
zmo@google.com5601ea02011-06-10 18:23:25 +0000375 writeTriplet(visit, NULL, "[", "]");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700376 }
377 break;
378 case EOpIndexDirectStruct:
379 if (visit == InVisit)
380 {
381 // Here we are writing out "foo.bar", where "foo" is struct
382 // and "bar" is field. In AST, it is represented as a binary
383 // node, where left child represents "foo" and right child "bar".
384 // The node itself represents ".". The struct field "bar" is
385 // actually stored as an index into TStructure::fields.
386 out << ".";
387 const TStructure *structure = node->getLeft()->getType().getStruct();
388 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
389 const TField *field = structure->fields()[index->getIConst(0)];
390
391 TString fieldName = field->name();
392 if (!mSymbolTable.findBuiltIn(structure->name(), mShaderVersion))
393 fieldName = hashName(fieldName);
394
395 out << fieldName;
396 visitChildren = false;
397 }
398 break;
Geoff Lang6e360422015-09-02 15:54:36 -0400399 case EOpIndexDirectInterfaceBlock:
400 if (visit == InVisit)
401 {
402 out << ".";
403 const TInterfaceBlock *interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
404 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
405 const TField *field = interfaceBlock->fields()[index->getIConst(0)];
406
407 TString fieldName = field->name();
408 ASSERT(!mSymbolTable.findBuiltIn(interfaceBlock->name(), mShaderVersion));
409 fieldName = hashName(fieldName);
410
411 out << fieldName;
412 visitChildren = false;
413 }
414 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700415 case EOpVectorSwizzle:
416 if (visit == InVisit)
417 {
418 out << ".";
419 TIntermAggregate *rightChild = node->getRight()->getAsAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700420 TIntermSequence *sequence = rightChild->getSequence();
421 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); ++sit)
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000422 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700423 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
424 ASSERT(element->getBasicType() == EbtInt);
425 ASSERT(element->getNominalSize() == 1);
Jamie Madill6ba6ead2015-05-04 14:21:21 -0400426 const TConstantUnion& data = element->getUnionArrayPointer()[0];
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700427 ASSERT(data.getType() == EbtInt);
428 switch (data.getIConst())
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000429 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700430 case 0:
431 out << "x";
432 break;
433 case 1:
434 out << "y";
435 break;
436 case 2:
437 out << "z";
438 break;
439 case 3:
440 out << "w";
441 break;
442 default:
443 UNREACHABLE();
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000444 }
445 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700446 visitChildren = false;
447 }
448 break;
daniel@transgaming.com97b16d12013-02-01 03:20:42 +0000449
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700450 case EOpAdd:
451 writeTriplet(visit, "(", " + ", ")");
452 break;
453 case EOpSub:
454 writeTriplet(visit, "(", " - ", ")");
455 break;
456 case EOpMul:
457 writeTriplet(visit, "(", " * ", ")");
458 break;
459 case EOpDiv:
460 writeTriplet(visit, "(", " / ", ")");
461 break;
Olli Etuahoff805cc2015-02-13 10:59:34 +0200462 case EOpIMod:
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +0000463 writeTriplet(visit, "(", " % ", ")");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700464 break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200465 case EOpBitShiftLeft:
466 writeTriplet(visit, "(", " << ", ")");
467 break;
468 case EOpBitShiftRight:
469 writeTriplet(visit, "(", " >> ", ")");
470 break;
471 case EOpBitwiseAnd:
472 writeTriplet(visit, "(", " & ", ")");
473 break;
474 case EOpBitwiseXor:
475 writeTriplet(visit, "(", " ^ ", ")");
476 break;
477 case EOpBitwiseOr:
478 writeTriplet(visit, "(", " | ", ")");
479 break;
480
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700481 case EOpEqual:
482 writeTriplet(visit, "(", " == ", ")");
483 break;
484 case EOpNotEqual:
485 writeTriplet(visit, "(", " != ", ")");
486 break;
487 case EOpLessThan:
488 writeTriplet(visit, "(", " < ", ")");
489 break;
490 case EOpGreaterThan:
491 writeTriplet(visit, "(", " > ", ")");
492 break;
493 case EOpLessThanEqual:
494 writeTriplet(visit, "(", " <= ", ")");
495 break;
496 case EOpGreaterThanEqual:
497 writeTriplet(visit, "(", " >= ", ")");
498 break;
daniel@transgaming.com97b16d12013-02-01 03:20:42 +0000499
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700500 // Notice the fall-through.
501 case EOpVectorTimesScalar:
502 case EOpVectorTimesMatrix:
503 case EOpMatrixTimesVector:
504 case EOpMatrixTimesScalar:
505 case EOpMatrixTimesMatrix:
506 writeTriplet(visit, "(", " * ", ")");
507 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000508
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700509 case EOpLogicalOr:
510 writeTriplet(visit, "(", " || ", ")");
511 break;
512 case EOpLogicalXor:
513 writeTriplet(visit, "(", " ^^ ", ")");
514 break;
515 case EOpLogicalAnd:
516 writeTriplet(visit, "(", " && ", ")");
517 break;
518 default:
519 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000520 }
521
522 return visitChildren;
523}
524
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700525bool TOutputGLSLBase::visitUnary(Visit visit, TIntermUnary *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000526{
zmo@google.com32e97312011-08-24 01:03:11 +0000527 TString preString;
528 TString postString = ")";
529
zmo@google.com5601ea02011-06-10 18:23:25 +0000530 switch (node->getOp())
531 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700532 case EOpNegative: preString = "(-"; break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -0700533 case EOpPositive: preString = "(+"; break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700534 case EOpVectorLogicalNot: preString = "not("; break;
535 case EOpLogicalNot: preString = "(!"; break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200536 case EOpBitwiseNot: preString = "(~"; break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000537
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700538 case EOpPostIncrement: preString = "("; postString = "++)"; break;
539 case EOpPostDecrement: preString = "("; postString = "--)"; break;
540 case EOpPreIncrement: preString = "(++"; break;
541 case EOpPreDecrement: preString = "(--"; break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000542
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700543 case EOpRadians:
544 preString = "radians(";
545 break;
546 case EOpDegrees:
547 preString = "degrees(";
548 break;
549 case EOpSin:
550 preString = "sin(";
551 break;
552 case EOpCos:
553 preString = "cos(";
554 break;
555 case EOpTan:
556 preString = "tan(";
557 break;
558 case EOpAsin:
559 preString = "asin(";
560 break;
561 case EOpAcos:
562 preString = "acos(";
563 break;
564 case EOpAtan:
565 preString = "atan(";
566 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000567
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200568 case EOpSinh:
569 preString = "sinh(";
570 break;
571 case EOpCosh:
572 preString = "cosh(";
573 break;
574 case EOpTanh:
575 preString = "tanh(";
576 break;
577 case EOpAsinh:
578 preString = "asinh(";
579 break;
580 case EOpAcosh:
581 preString = "acosh(";
582 break;
583 case EOpAtanh:
584 preString = "atanh(";
585 break;
586
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700587 case EOpExp:
588 preString = "exp(";
589 break;
590 case EOpLog:
591 preString = "log(";
592 break;
593 case EOpExp2:
594 preString = "exp2(";
595 break;
596 case EOpLog2:
597 preString = "log2(";
598 break;
599 case EOpSqrt:
600 preString = "sqrt(";
601 break;
602 case EOpInverseSqrt:
603 preString = "inversesqrt(";
604 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000605
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700606 case EOpAbs:
607 preString = "abs(";
608 break;
609 case EOpSign:
610 preString = "sign(";
611 break;
612 case EOpFloor:
613 preString = "floor(";
614 break;
Qingqing Deng5dbece52015-02-27 20:35:38 -0800615 case EOpTrunc:
616 preString = "trunc(";
617 break;
618 case EOpRound:
619 preString = "round(";
620 break;
621 case EOpRoundEven:
622 preString = "roundEven(";
623 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700624 case EOpCeil:
625 preString = "ceil(";
626 break;
627 case EOpFract:
628 preString = "fract(";
629 break;
Arun Patole0c1726e2015-02-18 14:35:02 +0530630 case EOpIsNan:
631 preString = "isnan(";
632 break;
633 case EOpIsInf:
634 preString = "isinf(";
635 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000636
Olli Etuahoe8d2c072015-01-08 16:33:54 +0200637 case EOpFloatBitsToInt:
638 preString = "floatBitsToInt(";
639 break;
640 case EOpFloatBitsToUint:
641 preString = "floatBitsToUint(";
642 break;
643 case EOpIntBitsToFloat:
644 preString = "intBitsToFloat(";
645 break;
646 case EOpUintBitsToFloat:
647 preString = "uintBitsToFloat(";
648 break;
649
Olli Etuaho7700ff62015-01-15 12:16:29 +0200650 case EOpPackSnorm2x16:
651 preString = "packSnorm2x16(";
652 break;
653 case EOpPackUnorm2x16:
654 preString = "packUnorm2x16(";
655 break;
656 case EOpPackHalf2x16:
657 preString = "packHalf2x16(";
658 break;
659 case EOpUnpackSnorm2x16:
660 preString = "unpackSnorm2x16(";
661 break;
662 case EOpUnpackUnorm2x16:
663 preString = "unpackUnorm2x16(";
664 break;
665 case EOpUnpackHalf2x16:
666 preString = "unpackHalf2x16(";
667 break;
668
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700669 case EOpLength:
670 preString = "length(";
671 break;
672 case EOpNormalize:
673 preString = "normalize(";
674 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000675
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700676 case EOpDFdx:
677 preString = "dFdx(";
678 break;
679 case EOpDFdy:
680 preString = "dFdy(";
681 break;
682 case EOpFwidth:
683 preString = "fwidth(";
684 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000685
Olli Etuahoe39706d2014-12-30 16:40:36 +0200686 case EOpTranspose:
687 preString = "transpose(";
688 break;
689 case EOpDeterminant:
690 preString = "determinant(";
691 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +0200692 case EOpInverse:
693 preString = "inverse(";
694 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +0200695
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700696 case EOpAny:
697 preString = "any(";
698 break;
699 case EOpAll:
700 preString = "all(";
701 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000702
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700703 default:
704 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000705 }
706
zmo@google.com32e97312011-08-24 01:03:11 +0000707 if (visit == PreVisit && node->getUseEmulatedFunction())
708 preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preString);
709 writeTriplet(visit, preString.c_str(), NULL, postString.c_str());
710
zmo@google.com5601ea02011-06-10 18:23:25 +0000711 return true;
712}
713
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700714bool TOutputGLSLBase::visitSelection(Visit visit, TIntermSelection *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000715{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700716 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000717
718 if (node->usesTernaryOperator())
719 {
720 // Notice two brackets at the beginning and end. The outer ones
721 // encapsulate the whole ternary expression. This preserves the
722 // order of precedence when ternary expressions are used in a
723 // compound expression, i.e., c = 2 * (a < b ? 1 : 2).
724 out << "((";
725 node->getCondition()->traverse(this);
726 out << ") ? (";
727 node->getTrueBlock()->traverse(this);
728 out << ") : (";
729 node->getFalseBlock()->traverse(this);
730 out << "))";
731 }
732 else
733 {
734 out << "if (";
735 node->getCondition()->traverse(this);
736 out << ")\n";
737
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700738 incrementDepth(node);
zmo@google.com5601ea02011-06-10 18:23:25 +0000739 visitCodeBlock(node->getTrueBlock());
740
741 if (node->getFalseBlock())
742 {
743 out << "else\n";
744 visitCodeBlock(node->getFalseBlock());
745 }
746 decrementDepth();
747 }
748 return false;
749}
750
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200751bool TOutputGLSLBase::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200752{
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200753 if (node->getStatementList())
754 {
755 writeTriplet(visit, "switch (", ") ", nullptr);
756 // The curly braces get written when visiting the statementList aggregate
757 }
758 else
759 {
760 // No statementList, so it won't output curly braces
761 writeTriplet(visit, "switch (", ") {", "}\n");
762 }
763 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200764}
765
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200766bool TOutputGLSLBase::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200767{
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200768 if (node->hasCondition())
769 {
770 writeTriplet(visit, "case (", nullptr, "):\n");
771 return true;
772 }
773 else
774 {
775 TInfoSinkBase &out = objSink();
776 out << "default:\n";
777 return false;
778 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200779}
780
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700781bool TOutputGLSLBase::visitAggregate(Visit visit, TIntermAggregate *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000782{
783 bool visitChildren = true;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700784 TInfoSinkBase &out = objSink();
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700785 bool useEmulatedFunction = (visit == PreVisit && node->getUseEmulatedFunction());
zmo@google.com5601ea02011-06-10 18:23:25 +0000786 switch (node->getOp())
787 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700788 case EOpSequence:
789 // Scope the sequences except when at the global scope.
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700790 if (mDepth > 0)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700791 {
792 out << "{\n";
zmo@google.com5601ea02011-06-10 18:23:25 +0000793 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000794
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700795 incrementDepth(node);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700796 for (TIntermSequence::const_iterator iter = node->getSequence()->begin();
797 iter != node->getSequence()->end(); ++iter)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700798 {
Austin Kinross3ae64652015-01-26 15:51:39 -0800799 TIntermNode *curNode = *iter;
800 ASSERT(curNode != NULL);
801 curNode->traverse(this);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700802
Austin Kinross3ae64652015-01-26 15:51:39 -0800803 if (isSingleStatement(curNode))
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700804 out << ";\n";
805 }
806 decrementDepth();
807
808 // Scope the sequences except when at the global scope.
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700809 if (mDepth > 0)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700810 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700811 out << "}\n";
812 }
813 visitChildren = false;
814 break;
815 case EOpPrototype:
816 // Function declaration.
817 ASSERT(visit == PreVisit);
Olli Etuahoab6fc6a2015-04-13 12:10:20 +0300818 {
819 const TType &type = node->getType();
820 writeVariableType(type);
821 if (type.isArray())
822 out << arrayBrackets(type);
823 }
824
Olli Etuaho59f9a642015-08-06 20:38:26 +0300825 out << " " << hashFunctionNameIfNeeded(node->getNameObj());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700826
827 out << "(";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700828 writeFunctionParameters(*(node->getSequence()));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700829 out << ")";
830
831 visitChildren = false;
832 break;
833 case EOpFunction: {
834 // Function definition.
835 ASSERT(visit == PreVisit);
Olli Etuahoab6fc6a2015-04-13 12:10:20 +0300836 {
837 const TType &type = node->getType();
838 writeVariableType(type);
839 if (type.isArray())
840 out << arrayBrackets(type);
841 }
842
Olli Etuaho59f9a642015-08-06 20:38:26 +0300843 out << " " << hashFunctionNameIfNeeded(node->getNameObj());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700844
845 incrementDepth(node);
846 // Function definition node contains one or two children nodes
847 // representing function parameters and function body. The latter
848 // is not present in case of empty function bodies.
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700849 const TIntermSequence &sequence = *(node->getSequence());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700850 ASSERT((sequence.size() == 1) || (sequence.size() == 2));
851 TIntermSequence::const_iterator seqIter = sequence.begin();
852
853 // Traverse function parameters.
854 TIntermAggregate *params = (*seqIter)->getAsAggregate();
855 ASSERT(params != NULL);
856 ASSERT(params->getOp() == EOpParameters);
857 params->traverse(this);
858
859 // Traverse function body.
860 TIntermAggregate *body = ++seqIter != sequence.end() ?
861 (*seqIter)->getAsAggregate() : NULL;
862 visitCodeBlock(body);
863 decrementDepth();
864
865 // Fully processed; no need to visit children.
866 visitChildren = false;
867 break;
868 }
869 case EOpFunctionCall:
870 // Function call.
871 if (visit == PreVisit)
Olli Etuaho59f9a642015-08-06 20:38:26 +0300872 out << hashFunctionNameIfNeeded(node->getNameObj()) << "(";
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200873 else if (visit == InVisit)
874 out << ", ";
875 else
876 out << ")";
877 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700878 case EOpParameters:
879 // Function parameters.
880 ASSERT(visit == PreVisit);
881 out << "(";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700882 writeFunctionParameters(*(node->getSequence()));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700883 out << ")";
884 visitChildren = false;
885 break;
886 case EOpDeclaration:
887 // Variable declaration.
888 if (visit == PreVisit)
889 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700890 const TIntermSequence &sequence = *(node->getSequence());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700891 const TIntermTyped *variable = sequence.front()->getAsTyped();
Olli Etuahoae69d7e2015-10-07 17:19:50 +0300892 writeLayoutQualifier(variable->getType());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700893 writeVariableType(variable->getType());
894 out << " ";
895 mDeclaringVariables = true;
zmo@google.com5601ea02011-06-10 18:23:25 +0000896 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700897 else if (visit == InVisit)
898 {
899 out << ", ";
900 mDeclaringVariables = true;
zmo@google.com5601ea02011-06-10 18:23:25 +0000901 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700902 else
903 {
904 mDeclaringVariables = false;
905 }
906 break;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700907 case EOpInvariantDeclaration:
908 // Invariant declaration.
909 ASSERT(visit == PreVisit);
910 {
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400911 const TIntermSequence *sequence = node->getSequence();
912 ASSERT(sequence && sequence->size() == 1);
913 const TIntermSymbol *symbol = sequence->front()->getAsSymbolNode();
914 ASSERT(symbol);
Zhenyao Mo2a517272014-10-27 16:09:57 -0700915 out << "invariant " << hashVariableName(symbol->getSymbol());
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400916 }
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700917 visitChildren = false;
918 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700919 case EOpConstructFloat:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700920 case EOpConstructVec2:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700921 case EOpConstructVec3:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700922 case EOpConstructVec4:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700923 case EOpConstructBool:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700924 case EOpConstructBVec2:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700925 case EOpConstructBVec3:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700926 case EOpConstructBVec4:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700927 case EOpConstructInt:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700928 case EOpConstructIVec2:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700929 case EOpConstructIVec3:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700930 case EOpConstructIVec4:
Geoff Langc4c7442d2015-07-20 13:09:26 -0400931 case EOpConstructUInt:
Geoff Langc4c7442d2015-07-20 13:09:26 -0400932 case EOpConstructUVec2:
Geoff Langc4c7442d2015-07-20 13:09:26 -0400933 case EOpConstructUVec3:
Geoff Langc4c7442d2015-07-20 13:09:26 -0400934 case EOpConstructUVec4:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700935 case EOpConstructMat2:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400936 case EOpConstructMat2x3:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400937 case EOpConstructMat2x4:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400938 case EOpConstructMat3x2:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700939 case EOpConstructMat3:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400940 case EOpConstructMat3x4:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400941 case EOpConstructMat4x2:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400942 case EOpConstructMat4x3:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700943 case EOpConstructMat4:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700944 case EOpConstructStruct:
Olli Etuahoe92507b2016-07-04 11:20:10 +0300945 writeConstructorTriplet(visit, node->getType());
946 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000947
Olli Etuahoe39706d2014-12-30 16:40:36 +0200948 case EOpOuterProduct:
949 writeBuiltInFunctionTriplet(visit, "outerProduct(", useEmulatedFunction);
950 break;
951
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700952 case EOpLessThan:
953 writeBuiltInFunctionTriplet(visit, "lessThan(", useEmulatedFunction);
954 break;
955 case EOpGreaterThan:
956 writeBuiltInFunctionTriplet(visit, "greaterThan(", useEmulatedFunction);
957 break;
958 case EOpLessThanEqual:
959 writeBuiltInFunctionTriplet(visit, "lessThanEqual(", useEmulatedFunction);
960 break;
961 case EOpGreaterThanEqual:
962 writeBuiltInFunctionTriplet(visit, "greaterThanEqual(", useEmulatedFunction);
963 break;
964 case EOpVectorEqual:
965 writeBuiltInFunctionTriplet(visit, "equal(", useEmulatedFunction);
966 break;
967 case EOpVectorNotEqual:
968 writeBuiltInFunctionTriplet(visit, "notEqual(", useEmulatedFunction);
969 break;
970 case EOpComma:
Zhenyao Mo0783efd2014-10-21 15:51:59 -0700971 writeTriplet(visit, "(", ", ", ")");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700972 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000973
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700974 case EOpMod:
975 writeBuiltInFunctionTriplet(visit, "mod(", useEmulatedFunction);
976 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +0200977 case EOpModf:
978 writeBuiltInFunctionTriplet(visit, "modf(", useEmulatedFunction);
979 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700980 case EOpPow:
981 writeBuiltInFunctionTriplet(visit, "pow(", useEmulatedFunction);
982 break;
983 case EOpAtan:
984 writeBuiltInFunctionTriplet(visit, "atan(", useEmulatedFunction);
985 break;
986 case EOpMin:
987 writeBuiltInFunctionTriplet(visit, "min(", useEmulatedFunction);
988 break;
989 case EOpMax:
990 writeBuiltInFunctionTriplet(visit, "max(", useEmulatedFunction);
991 break;
992 case EOpClamp:
993 writeBuiltInFunctionTriplet(visit, "clamp(", useEmulatedFunction);
994 break;
995 case EOpMix:
996 writeBuiltInFunctionTriplet(visit, "mix(", useEmulatedFunction);
997 break;
998 case EOpStep:
999 writeBuiltInFunctionTriplet(visit, "step(", useEmulatedFunction);
1000 break;
1001 case EOpSmoothStep:
1002 writeBuiltInFunctionTriplet(visit, "smoothstep(", useEmulatedFunction);
1003 break;
1004 case EOpDistance:
1005 writeBuiltInFunctionTriplet(visit, "distance(", useEmulatedFunction);
1006 break;
1007 case EOpDot:
1008 writeBuiltInFunctionTriplet(visit, "dot(", useEmulatedFunction);
1009 break;
1010 case EOpCross:
1011 writeBuiltInFunctionTriplet(visit, "cross(", useEmulatedFunction);
1012 break;
1013 case EOpFaceForward:
1014 writeBuiltInFunctionTriplet(visit, "faceforward(", useEmulatedFunction);
1015 break;
1016 case EOpReflect:
1017 writeBuiltInFunctionTriplet(visit, "reflect(", useEmulatedFunction);
1018 break;
1019 case EOpRefract:
1020 writeBuiltInFunctionTriplet(visit, "refract(", useEmulatedFunction);
1021 break;
1022 case EOpMul:
1023 writeBuiltInFunctionTriplet(visit, "matrixCompMult(", useEmulatedFunction);
1024 break;
zmo@google.com5601ea02011-06-10 18:23:25 +00001025
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001026 default:
1027 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +00001028 }
zmo@google.com5601ea02011-06-10 18:23:25 +00001029 return visitChildren;
1030}
1031
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001032bool TOutputGLSLBase::visitLoop(Visit visit, TIntermLoop *node)
zmo@google.com5601ea02011-06-10 18:23:25 +00001033{
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001034 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +00001035
Zhenyao Mo7cab38b2013-10-15 12:59:30 -07001036 incrementDepth(node);
Corentin Wallez7258e302015-09-22 10:40:24 -07001037
zmo@google.com5601ea02011-06-10 18:23:25 +00001038 TLoopType loopType = node->getType();
Corentin Wallez7258e302015-09-22 10:40:24 -07001039
1040 // Only for loops can be unrolled
1041 ASSERT(!node->getUnrollFlag() || loopType == ELoopFor);
1042
zmo@google.com5601ea02011-06-10 18:23:25 +00001043 if (loopType == ELoopFor) // for loop
1044 {
Zhenyao Mo550c6002014-02-26 15:40:48 -08001045 if (!node->getUnrollFlag())
1046 {
zmo@google.com5601ea02011-06-10 18:23:25 +00001047 out << "for (";
1048 if (node->getInit())
1049 node->getInit()->traverse(this);
1050 out << "; ";
1051
1052 if (node->getCondition())
1053 node->getCondition()->traverse(this);
1054 out << "; ";
1055
1056 if (node->getExpression())
1057 node->getExpression()->traverse(this);
1058 out << ")\n";
Corentin Wallez7258e302015-09-22 10:40:24 -07001059
1060 visitCodeBlock(node->getBody());
zmo@google.com5601ea02011-06-10 18:23:25 +00001061 }
Zhenyao Mo550c6002014-02-26 15:40:48 -08001062 else
1063 {
1064 // Need to put a one-iteration loop here to handle break.
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001065 TIntermSequence *declSeq =
Zhenyao Mo550c6002014-02-26 15:40:48 -08001066 node->getInit()->getAsAggregate()->getSequence();
1067 TIntermSymbol *indexSymbol =
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001068 (*declSeq)[0]->getAsBinaryNode()->getLeft()->getAsSymbolNode();
Zhenyao Mo550c6002014-02-26 15:40:48 -08001069 TString name = hashVariableName(indexSymbol->getSymbol());
1070 out << "for (int " << name << " = 0; "
1071 << name << " < 1; "
1072 << "++" << name << ")\n";
Corentin Wallez7258e302015-09-22 10:40:24 -07001073
1074 out << "{\n";
1075 mLoopUnrollStack.push(node);
1076 while (mLoopUnrollStack.satisfiesLoopCondition())
1077 {
1078 visitCodeBlock(node->getBody());
1079 mLoopUnrollStack.step();
1080 }
1081 mLoopUnrollStack.pop();
1082 out << "}\n";
Zhenyao Mo550c6002014-02-26 15:40:48 -08001083 }
zmo@google.com5601ea02011-06-10 18:23:25 +00001084 }
1085 else if (loopType == ELoopWhile) // while loop
1086 {
1087 out << "while (";
1088 ASSERT(node->getCondition() != NULL);
1089 node->getCondition()->traverse(this);
1090 out << ")\n";
Corentin Wallez7258e302015-09-22 10:40:24 -07001091
1092 visitCodeBlock(node->getBody());
zmo@google.com5601ea02011-06-10 18:23:25 +00001093 }
1094 else // do-while loop
1095 {
1096 ASSERT(loopType == ELoopDoWhile);
1097 out << "do\n";
zmo@google.com5601ea02011-06-10 18:23:25 +00001098
zmo@google.com5601ea02011-06-10 18:23:25 +00001099 visitCodeBlock(node->getBody());
zmo@google.com5601ea02011-06-10 18:23:25 +00001100
zmo@google.com5601ea02011-06-10 18:23:25 +00001101 out << "while (";
1102 ASSERT(node->getCondition() != NULL);
1103 node->getCondition()->traverse(this);
1104 out << ");\n";
1105 }
Corentin Wallez7258e302015-09-22 10:40:24 -07001106
zmo@google.com5601ea02011-06-10 18:23:25 +00001107 decrementDepth();
1108
1109 // No need to visit children. They have been already processed in
1110 // this function.
1111 return false;
1112}
1113
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001114bool TOutputGLSLBase::visitBranch(Visit visit, TIntermBranch *node)
zmo@google.com5601ea02011-06-10 18:23:25 +00001115{
1116 switch (node->getFlowOp())
1117 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001118 case EOpKill:
1119 writeTriplet(visit, "discard", NULL, NULL);
1120 break;
1121 case EOpBreak:
1122 writeTriplet(visit, "break", NULL, NULL);
1123 break;
1124 case EOpContinue:
1125 writeTriplet(visit, "continue", NULL, NULL);
1126 break;
1127 case EOpReturn:
1128 writeTriplet(visit, "return ", NULL, NULL);
1129 break;
1130 default:
1131 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +00001132 }
1133
1134 return true;
1135}
1136
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001137void TOutputGLSLBase::visitCodeBlock(TIntermNode *node)
1138{
zmo@google.com5601ea02011-06-10 18:23:25 +00001139 TInfoSinkBase &out = objSink();
1140 if (node != NULL)
1141 {
1142 node->traverse(this);
1143 // Single statements not part of a sequence need to be terminated
1144 // with semi-colon.
1145 if (isSingleStatement(node))
1146 out << ";\n";
1147 }
1148 else
1149 {
1150 out << "{\n}\n"; // Empty code block.
1151 }
1152}
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001153
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001154TString TOutputGLSLBase::getTypeName(const TType &type)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001155{
Olli Etuahoe92507b2016-07-04 11:20:10 +03001156 if (type.getBasicType() == EbtStruct)
1157 return hashName(type.getStruct()->name());
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001158 else
Olli Etuahoe92507b2016-07-04 11:20:10 +03001159 return type.getBuiltInTypeNameString();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001160}
1161
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001162TString TOutputGLSLBase::hashName(const TString &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001163{
1164 if (mHashFunction == NULL || name.empty())
1165 return name;
1166 NameMap::const_iterator it = mNameMap.find(name.c_str());
1167 if (it != mNameMap.end())
1168 return it->second.c_str();
1169 TString hashedName = TIntermTraverser::hash(name, mHashFunction);
1170 mNameMap[name.c_str()] = hashedName.c_str();
1171 return hashedName;
1172}
1173
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001174TString TOutputGLSLBase::hashVariableName(const TString &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001175{
Jamie Madill02f20dd2013-09-12 12:07:42 -04001176 if (mSymbolTable.findBuiltIn(name, mShaderVersion) != NULL)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001177 return name;
1178 return hashName(name);
1179}
1180
Olli Etuaho59f9a642015-08-06 20:38:26 +03001181TString TOutputGLSLBase::hashFunctionNameIfNeeded(const TName &mangledName)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001182{
Olli Etuaho59f9a642015-08-06 20:38:26 +03001183 TString mangledStr = mangledName.getString();
1184 TString name = TFunction::unmangleName(mangledStr);
1185 if (mSymbolTable.findBuiltIn(mangledStr, mShaderVersion) != nullptr || name == "main")
Nicolas Capens46485082014-04-15 13:12:50 -04001186 return translateTextureFunction(name);
Olli Etuaho59f9a642015-08-06 20:38:26 +03001187 if (mangledName.isInternal())
1188 return name;
1189 else
1190 return hashName(name);
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001191}
Jamie Madill98493dd2013-07-08 14:39:03 -04001192
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001193bool TOutputGLSLBase::structDeclared(const TStructure *structure) const
Jamie Madill98493dd2013-07-08 14:39:03 -04001194{
Zhenyao Mo904a9162014-05-09 14:07:45 -07001195 ASSERT(structure);
Jamie Madill01f85ac2014-06-06 11:55:04 -04001196 if (structure->name().empty())
Zhenyao Mo904a9162014-05-09 14:07:45 -07001197 {
Jamie Madill01f85ac2014-06-06 11:55:04 -04001198 return false;
Zhenyao Mo904a9162014-05-09 14:07:45 -07001199 }
Jamie Madill01f85ac2014-06-06 11:55:04 -04001200
1201 return (mDeclaredStructs.count(structure->uniqueId()) > 0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001202}
1203
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001204void TOutputGLSLBase::declareStruct(const TStructure *structure)
Jamie Madill98493dd2013-07-08 14:39:03 -04001205{
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001206 TInfoSinkBase &out = objSink();
Jamie Madill98493dd2013-07-08 14:39:03 -04001207
1208 out << "struct " << hashName(structure->name()) << "{\n";
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001209 const TFieldList &fields = structure->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04001210 for (size_t i = 0; i < fields.size(); ++i)
1211 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001212 const TField *field = fields[i];
Jamie Madill98493dd2013-07-08 14:39:03 -04001213 if (writeVariablePrecision(field->type()->getPrecision()))
1214 out << " ";
1215 out << getTypeName(*field->type()) << " " << hashName(field->name());
1216 if (field->type()->isArray())
1217 out << arrayBrackets(*field->type());
1218 out << ";\n";
1219 }
1220 out << "}";
Zhenyao Mo904a9162014-05-09 14:07:45 -07001221}
Jamie Madill98493dd2013-07-08 14:39:03 -04001222
Geoff Langbdcc54a2015-09-02 13:09:48 -04001223void TOutputGLSLBase::declareInterfaceBlockLayout(const TInterfaceBlock *interfaceBlock)
1224{
1225 TInfoSinkBase &out = objSink();
1226
1227 out << "layout(";
1228
1229 switch (interfaceBlock->blockStorage())
1230 {
1231 case EbsUnspecified:
1232 case EbsShared:
1233 // Default block storage is shared.
1234 out << "shared";
1235 break;
1236
1237 case EbsPacked:
1238 out << "packed";
1239 break;
1240
1241 case EbsStd140:
1242 out << "std140";
1243 break;
1244
1245 default:
1246 UNREACHABLE();
1247 break;
1248 }
1249
1250 out << ", ";
1251
1252 switch (interfaceBlock->matrixPacking())
1253 {
1254 case EmpUnspecified:
1255 case EmpColumnMajor:
1256 // Default matrix packing is column major.
1257 out << "column_major";
1258 break;
1259
1260 case EmpRowMajor:
1261 out << "row_major";
1262 break;
1263
1264 default:
1265 UNREACHABLE();
1266 break;
1267 }
1268
1269 out << ") ";
1270}
1271
1272void TOutputGLSLBase::declareInterfaceBlock(const TInterfaceBlock *interfaceBlock)
1273{
1274 TInfoSinkBase &out = objSink();
1275
1276 out << hashName(interfaceBlock->name()) << "{\n";
1277 const TFieldList &fields = interfaceBlock->fields();
1278 for (size_t i = 0; i < fields.size(); ++i)
1279 {
1280 const TField *field = fields[i];
1281 if (writeVariablePrecision(field->type()->getPrecision()))
1282 out << " ";
1283 out << getTypeName(*field->type()) << " " << hashName(field->name());
1284 if (field->type()->isArray())
1285 out << arrayBrackets(*field->type());
1286 out << ";\n";
1287 }
1288 out << "}";
1289}