blob: 43015596134bb0adf28b8095289a8e336939da9e [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{
Olli Etuaho336b1472016-10-05 16:37:55 +010025 if (node->getAsFunctionDefinition())
zmo@google.com5601ea02011-06-10 18:23:25 +000026 {
Olli Etuaho336b1472016-10-05 16:37:55 +010027 return false;
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010028 }
29 else if (node->getAsBlock())
30 {
31 return false;
zmo@google.com5601ea02011-06-10 18:23:25 +000032 }
Olli Etuaho57961272016-09-14 13:57:46 +030033 else if (node->getAsIfElseNode())
zmo@google.com5601ea02011-06-10 18:23:25 +000034 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +030035 return false;
zmo@google.com5601ea02011-06-10 18:23:25 +000036 }
37 else if (node->getAsLoopNode())
38 {
39 return false;
40 }
Olli Etuaho01cd8af2015-02-20 10:39:20 +020041 else if (node->getAsSwitchNode())
42 {
43 return false;
44 }
45 else if (node->getAsCaseNode())
46 {
47 return false;
48 }
zmo@google.com5601ea02011-06-10 18:23:25 +000049 return true;
50}
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080051
zmo@google.com5601ea02011-06-10 18:23:25 +000052} // namespace
53
Zhenyao Mo9eedea02014-05-12 16:02:35 -070054TOutputGLSLBase::TOutputGLSLBase(TInfoSinkBase &objSink,
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +000055 ShArrayIndexClampingStrategy clampingStrategy,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000056 ShHashFunction64 hashFunction,
Zhenyao Mo9eedea02014-05-12 16:02:35 -070057 NameMap &nameMap,
58 TSymbolTable &symbolTable,
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080059 int shaderVersion,
60 ShShaderOutput output)
zmo@google.com5601ea02011-06-10 18:23:25 +000061 : TIntermTraverser(true, true, true),
62 mObjSink(objSink),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000063 mDeclaringVariables(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +000064 mClampingStrategy(clampingStrategy),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000065 mHashFunction(hashFunction),
66 mNameMap(nameMap),
Jamie Madill02f20dd2013-09-12 12:07:42 -040067 mSymbolTable(symbolTable),
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080068 mShaderVersion(shaderVersion),
69 mOutput(output)
zmo@google.com5601ea02011-06-10 18:23:25 +000070{
71}
72
Zhenyao Mo9eedea02014-05-12 16:02:35 -070073void TOutputGLSLBase::writeTriplet(
74 Visit visit, const char *preStr, const char *inStr, const char *postStr)
zmo@google.com5601ea02011-06-10 18:23:25 +000075{
Zhenyao Mo9eedea02014-05-12 16:02:35 -070076 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +000077 if (visit == PreVisit && preStr)
zmo@google.com5601ea02011-06-10 18:23:25 +000078 out << preStr;
zmo@google.com5601ea02011-06-10 18:23:25 +000079 else if (visit == InVisit && inStr)
zmo@google.com5601ea02011-06-10 18:23:25 +000080 out << inStr;
zmo@google.com5601ea02011-06-10 18:23:25 +000081 else if (visit == PostVisit && postStr)
zmo@google.com5601ea02011-06-10 18:23:25 +000082 out << postStr;
zmo@google.com5601ea02011-06-10 18:23:25 +000083}
84
Zhenyao Mo9eedea02014-05-12 16:02:35 -070085void TOutputGLSLBase::writeBuiltInFunctionTriplet(
86 Visit visit, const char *preStr, bool useEmulatedFunction)
zmo@google.com5601ea02011-06-10 18:23:25 +000087{
Zhenyao Mo9eedea02014-05-12 16:02:35 -070088 TString preString = useEmulatedFunction ?
89 BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr) : preStr;
90 writeTriplet(visit, preString.c_str(), ", ", ")");
91}
92
Olli Etuahoae69d7e2015-10-07 17:19:50 +030093void TOutputGLSLBase::writeLayoutQualifier(const TType &type)
94{
95 if (type.getQualifier() == EvqFragmentOut || type.getQualifier() == EvqVertexIn)
96 {
97 const TLayoutQualifier &layoutQualifier = type.getLayoutQualifier();
98 if (layoutQualifier.location >= 0)
99 {
100 TInfoSinkBase &out = objSink();
101 out << "layout(location = " << layoutQualifier.location << ") ";
102 }
103 }
104}
105
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700106void TOutputGLSLBase::writeVariableType(const TType &type)
107{
108 TInfoSinkBase &out = objSink();
Olli Etuaho214c2d82015-04-27 14:49:13 +0300109 if (type.isInvariant())
110 {
111 out << "invariant ";
112 }
Geoff Langbdcc54a2015-09-02 13:09:48 -0400113 if (type.getBasicType() == EbtInterfaceBlock)
114 {
115 TInterfaceBlock *interfaceBlock = type.getInterfaceBlock();
116 declareInterfaceBlockLayout(interfaceBlock);
117 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000118 TQualifier qualifier = type.getQualifier();
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400119 if (qualifier != EvqTemporary && qualifier != EvqGlobal)
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400120 {
Qingqing Dengad0d0792015-04-08 14:25:06 -0700121 if (IsGLSL130OrNewer(mOutput))
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800122 {
123 switch (qualifier)
124 {
125 case EvqAttribute:
Olli Etuaho214c2d82015-04-27 14:49:13 +0300126 out << "in ";
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800127 break;
128 case EvqVaryingIn:
Olli Etuaho214c2d82015-04-27 14:49:13 +0300129 out << "in ";
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800130 break;
131 case EvqVaryingOut:
Olli Etuaho214c2d82015-04-27 14:49:13 +0300132 out << "out ";
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800133 break;
134 default:
135 out << type.getQualifierString() << " ";
136 break;
137 }
138 }
139 else
140 {
141 out << type.getQualifierString() << " ";
142 }
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400143 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000144 // Declare the struct if we have not done so already.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700145 if (type.getBasicType() == EbtStruct && !structDeclared(type.getStruct()))
zmo@google.com5601ea02011-06-10 18:23:25 +0000146 {
Jamie Madill01f85ac2014-06-06 11:55:04 -0400147 TStructure *structure = type.getStruct();
148
149 declareStruct(structure);
150
151 if (!structure->name().empty())
152 {
153 mDeclaredStructs.insert(structure->uniqueId());
154 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000155 }
Geoff Langbdcc54a2015-09-02 13:09:48 -0400156 else if (type.getBasicType() == EbtInterfaceBlock)
157 {
158 TInterfaceBlock *interfaceBlock = type.getInterfaceBlock();
159 declareInterfaceBlock(interfaceBlock);
160 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000161 else
162 {
163 if (writeVariablePrecision(type.getPrecision()))
164 out << " ";
165 out << getTypeName(type);
166 }
167}
168
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700169void TOutputGLSLBase::writeFunctionParameters(const TIntermSequence &args)
zmo@google.com5601ea02011-06-10 18:23:25 +0000170{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700171 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000172 for (TIntermSequence::const_iterator iter = args.begin();
173 iter != args.end(); ++iter)
174 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700175 const TIntermSymbol *arg = (*iter)->getAsSymbolNode();
zmo@google.com5601ea02011-06-10 18:23:25 +0000176 ASSERT(arg != NULL);
177
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700178 const TType &type = arg->getType();
zmo@google.com189be2f2011-06-16 18:28:53 +0000179 writeVariableType(type);
zmo@google.com5601ea02011-06-10 18:23:25 +0000180
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700181 const TString &name = arg->getSymbol();
zmo@google.com5601ea02011-06-10 18:23:25 +0000182 if (!name.empty())
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000183 out << " " << hashName(name);
zmo@google.com5601ea02011-06-10 18:23:25 +0000184 if (type.isArray())
185 out << arrayBrackets(type);
186
187 // Put a comma if this is not the last argument.
188 if (iter != args.end() - 1)
189 out << ", ";
190 }
191}
192
Jamie Madill6ba6ead2015-05-04 14:21:21 -0400193const TConstantUnion *TOutputGLSLBase::writeConstantUnion(
194 const TType &type, const TConstantUnion *pConstUnion)
zmo@google.com5601ea02011-06-10 18:23:25 +0000195{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700196 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000197
198 if (type.getBasicType() == EbtStruct)
199 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700200 const TStructure *structure = type.getStruct();
Jamie Madill98493dd2013-07-08 14:39:03 -0400201 out << hashName(structure->name()) << "(";
202
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700203 const TFieldList &fields = structure->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -0400204 for (size_t i = 0; i < fields.size(); ++i)
zmo@google.com5601ea02011-06-10 18:23:25 +0000205 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700206 const TType *fieldType = fields[i]->type();
zmo@google.com5601ea02011-06-10 18:23:25 +0000207 ASSERT(fieldType != NULL);
208 pConstUnion = writeConstantUnion(*fieldType, pConstUnion);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700209 if (i != fields.size() - 1)
210 out << ", ";
zmo@google.com5601ea02011-06-10 18:23:25 +0000211 }
212 out << ")";
213 }
214 else
215 {
Jamie Madill94bf7f22013-07-08 13:31:15 -0400216 size_t size = type.getObjectSize();
zmo@google.com5601ea02011-06-10 18:23:25 +0000217 bool writeType = size > 1;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700218 if (writeType)
219 out << getTypeName(type) << "(";
Jamie Madill94bf7f22013-07-08 13:31:15 -0400220 for (size_t i = 0; i < size; ++i, ++pConstUnion)
zmo@google.com5601ea02011-06-10 18:23:25 +0000221 {
222 switch (pConstUnion->getType())
223 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700224 case EbtFloat:
225 out << std::min(FLT_MAX, std::max(-FLT_MAX, pConstUnion->getFConst()));
226 break;
227 case EbtInt:
228 out << pConstUnion->getIConst();
229 break;
Olli Etuaho5321c802015-04-02 17:08:16 +0300230 case EbtUInt:
231 out << pConstUnion->getUConst() << "u";
232 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700233 case EbtBool:
234 out << pConstUnion->getBConst();
235 break;
236 default: UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000237 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700238 if (i != size - 1)
239 out << ", ";
zmo@google.com5601ea02011-06-10 18:23:25 +0000240 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700241 if (writeType)
242 out << ")";
zmo@google.com5601ea02011-06-10 18:23:25 +0000243 }
244 return pConstUnion;
245}
246
Olli Etuahoe92507b2016-07-04 11:20:10 +0300247void TOutputGLSLBase::writeConstructorTriplet(Visit visit, const TType &type)
Olli Etuahof40319e2015-03-10 14:33:00 +0200248{
249 TInfoSinkBase &out = objSink();
250 if (visit == PreVisit)
251 {
252 if (type.isArray())
253 {
Olli Etuahoe92507b2016-07-04 11:20:10 +0300254 out << getTypeName(type);
Olli Etuahof40319e2015-03-10 14:33:00 +0200255 out << arrayBrackets(type);
256 out << "(";
257 }
258 else
259 {
Olli Etuahoe92507b2016-07-04 11:20:10 +0300260 out << getTypeName(type) << "(";
Olli Etuahof40319e2015-03-10 14:33:00 +0200261 }
262 }
263 else
264 {
265 writeTriplet(visit, nullptr, ", ", ")");
266 }
267}
268
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700269void TOutputGLSLBase::visitSymbol(TIntermSymbol *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000270{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700271 TInfoSinkBase &out = objSink();
Zhenyao Mo550c6002014-02-26 15:40:48 -0800272 if (mLoopUnrollStack.needsToReplaceSymbolWithValue(node))
273 out << mLoopUnrollStack.getLoopIndexValue(node);
zmo@google.com5601ea02011-06-10 18:23:25 +0000274 else
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000275 out << hashVariableName(node->getSymbol());
zmo@google.com5601ea02011-06-10 18:23:25 +0000276
277 if (mDeclaringVariables && node->getType().isArray())
278 out << arrayBrackets(node->getType());
279}
280
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700281void TOutputGLSLBase::visitConstantUnion(TIntermConstantUnion *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000282{
283 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
284}
285
Olli Etuahob6fa0432016-09-28 16:28:05 +0100286bool TOutputGLSLBase::visitSwizzle(Visit visit, TIntermSwizzle *node)
287{
288 TInfoSinkBase &out = objSink();
289 if (visit == PostVisit)
290 {
291 out << ".";
292 node->writeOffsetsAsXYZW(&out);
293 }
294 return true;
295}
296
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700297bool TOutputGLSLBase::visitBinary(Visit visit, TIntermBinary *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000298{
299 bool visitChildren = true;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700300 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000301 switch (node->getOp())
302 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100303 case EOpComma:
304 writeTriplet(visit, "(", ", ", ")");
305 break;
306 case EOpInitialize:
zmo@google.com5601ea02011-06-10 18:23:25 +0000307 if (visit == InVisit)
308 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100309 out << " = ";
310 // RHS of initialize is not being declared.
311 mDeclaringVariables = false;
zmo@google.com5601ea02011-06-10 18:23:25 +0000312 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100313 break;
314 case EOpAssign:
315 writeTriplet(visit, "(", " = ", ")");
316 break;
317 case EOpAddAssign:
318 writeTriplet(visit, "(", " += ", ")");
319 break;
320 case EOpSubAssign:
321 writeTriplet(visit, "(", " -= ", ")");
322 break;
323 case EOpDivAssign:
324 writeTriplet(visit, "(", " /= ", ")");
325 break;
326 case EOpIModAssign:
327 writeTriplet(visit, "(", " %= ", ")");
328 break;
329 // Notice the fall-through.
330 case EOpMulAssign:
331 case EOpVectorTimesMatrixAssign:
332 case EOpVectorTimesScalarAssign:
333 case EOpMatrixTimesScalarAssign:
334 case EOpMatrixTimesMatrixAssign:
335 writeTriplet(visit, "(", " *= ", ")");
336 break;
337 case EOpBitShiftLeftAssign:
338 writeTriplet(visit, "(", " <<= ", ")");
339 break;
340 case EOpBitShiftRightAssign:
341 writeTriplet(visit, "(", " >>= ", ")");
342 break;
343 case EOpBitwiseAndAssign:
344 writeTriplet(visit, "(", " &= ", ")");
345 break;
346 case EOpBitwiseXorAssign:
347 writeTriplet(visit, "(", " ^= ", ")");
348 break;
349 case EOpBitwiseOrAssign:
350 writeTriplet(visit, "(", " |= ", ")");
351 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000352
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100353 case EOpIndexDirect:
zmo@google.com5601ea02011-06-10 18:23:25 +0000354 writeTriplet(visit, NULL, "[", "]");
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100355 break;
356 case EOpIndexIndirect:
357 if (node->getAddIndexClamp())
358 {
359 if (visit == InVisit)
360 {
361 if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC)
362 out << "[int(clamp(float(";
363 else
364 out << "[webgl_int_clamp(";
365 }
366 else if (visit == PostVisit)
367 {
368 int maxSize;
369 TIntermTyped *left = node->getLeft();
370 TType leftType = left->getType();
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700371
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100372 if (left->isArray())
373 {
374 // The shader will fail validation if the array length is not > 0.
375 maxSize = static_cast<int>(leftType.getArraySize()) - 1;
376 }
377 else
378 {
379 maxSize = leftType.getNominalSize() - 1;
380 }
381
382 if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC)
383 out << "), 0.0, float(" << maxSize << ")))]";
384 else
385 out << ", 0, " << maxSize << ")]";
386 }
387 }
388 else
389 {
390 writeTriplet(visit, NULL, "[", "]");
391 }
392 break;
393 case EOpIndexDirectStruct:
394 if (visit == InVisit)
395 {
396 // Here we are writing out "foo.bar", where "foo" is struct
397 // and "bar" is field. In AST, it is represented as a binary
398 // node, where left child represents "foo" and right child "bar".
399 // The node itself represents ".". The struct field "bar" is
400 // actually stored as an index into TStructure::fields.
401 out << ".";
402 const TStructure *structure = node->getLeft()->getType().getStruct();
403 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
404 const TField *field = structure->fields()[index->getIConst(0)];
405
406 TString fieldName = field->name();
407 if (!mSymbolTable.findBuiltIn(structure->name(), mShaderVersion))
408 fieldName = hashName(fieldName);
409
410 out << fieldName;
411 visitChildren = false;
412 }
413 break;
414 case EOpIndexDirectInterfaceBlock:
415 if (visit == InVisit)
416 {
417 out << ".";
418 const TInterfaceBlock *interfaceBlock =
419 node->getLeft()->getType().getInterfaceBlock();
420 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
421 const TField *field = interfaceBlock->fields()[index->getIConst(0)];
422
423 TString fieldName = field->name();
424 ASSERT(!mSymbolTable.findBuiltIn(interfaceBlock->name(), mShaderVersion));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700425 fieldName = hashName(fieldName);
426
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100427 out << fieldName;
428 visitChildren = false;
429 }
430 break;
Geoff Lang6e360422015-09-02 15:54:36 -0400431
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100432 case EOpAdd:
433 writeTriplet(visit, "(", " + ", ")");
434 break;
435 case EOpSub:
436 writeTriplet(visit, "(", " - ", ")");
437 break;
438 case EOpMul:
439 writeTriplet(visit, "(", " * ", ")");
440 break;
441 case EOpDiv:
442 writeTriplet(visit, "(", " / ", ")");
443 break;
444 case EOpIMod:
445 writeTriplet(visit, "(", " % ", ")");
446 break;
447 case EOpBitShiftLeft:
448 writeTriplet(visit, "(", " << ", ")");
449 break;
450 case EOpBitShiftRight:
451 writeTriplet(visit, "(", " >> ", ")");
452 break;
453 case EOpBitwiseAnd:
454 writeTriplet(visit, "(", " & ", ")");
455 break;
456 case EOpBitwiseXor:
457 writeTriplet(visit, "(", " ^ ", ")");
458 break;
459 case EOpBitwiseOr:
460 writeTriplet(visit, "(", " | ", ")");
461 break;
Geoff Lang6e360422015-09-02 15:54:36 -0400462
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100463 case EOpEqual:
464 writeTriplet(visit, "(", " == ", ")");
465 break;
466 case EOpNotEqual:
467 writeTriplet(visit, "(", " != ", ")");
468 break;
469 case EOpLessThan:
470 writeTriplet(visit, "(", " < ", ")");
471 break;
472 case EOpGreaterThan:
473 writeTriplet(visit, "(", " > ", ")");
474 break;
475 case EOpLessThanEqual:
476 writeTriplet(visit, "(", " <= ", ")");
477 break;
478 case EOpGreaterThanEqual:
479 writeTriplet(visit, "(", " >= ", ")");
480 break;
daniel@transgaming.com97b16d12013-02-01 03:20:42 +0000481
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100482 // Notice the fall-through.
483 case EOpVectorTimesScalar:
484 case EOpVectorTimesMatrix:
485 case EOpMatrixTimesVector:
486 case EOpMatrixTimesScalar:
487 case EOpMatrixTimesMatrix:
488 writeTriplet(visit, "(", " * ", ")");
489 break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200490
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100491 case EOpLogicalOr:
492 writeTriplet(visit, "(", " || ", ")");
493 break;
494 case EOpLogicalXor:
495 writeTriplet(visit, "(", " ^^ ", ")");
496 break;
497 case EOpLogicalAnd:
498 writeTriplet(visit, "(", " && ", ")");
499 break;
500 default:
501 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000502 }
503
504 return visitChildren;
505}
506
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700507bool TOutputGLSLBase::visitUnary(Visit visit, TIntermUnary *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000508{
zmo@google.com32e97312011-08-24 01:03:11 +0000509 TString preString;
510 TString postString = ")";
511
zmo@google.com5601ea02011-06-10 18:23:25 +0000512 switch (node->getOp())
513 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700514 case EOpNegative: preString = "(-"; break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -0700515 case EOpPositive: preString = "(+"; break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700516 case EOpVectorLogicalNot: preString = "not("; break;
517 case EOpLogicalNot: preString = "(!"; break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200518 case EOpBitwiseNot: preString = "(~"; break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000519
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700520 case EOpPostIncrement: preString = "("; postString = "++)"; break;
521 case EOpPostDecrement: preString = "("; postString = "--)"; break;
522 case EOpPreIncrement: preString = "(++"; break;
523 case EOpPreDecrement: preString = "(--"; break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000524
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700525 case EOpRadians:
526 preString = "radians(";
527 break;
528 case EOpDegrees:
529 preString = "degrees(";
530 break;
531 case EOpSin:
532 preString = "sin(";
533 break;
534 case EOpCos:
535 preString = "cos(";
536 break;
537 case EOpTan:
538 preString = "tan(";
539 break;
540 case EOpAsin:
541 preString = "asin(";
542 break;
543 case EOpAcos:
544 preString = "acos(";
545 break;
546 case EOpAtan:
547 preString = "atan(";
548 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000549
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200550 case EOpSinh:
551 preString = "sinh(";
552 break;
553 case EOpCosh:
554 preString = "cosh(";
555 break;
556 case EOpTanh:
557 preString = "tanh(";
558 break;
559 case EOpAsinh:
560 preString = "asinh(";
561 break;
562 case EOpAcosh:
563 preString = "acosh(";
564 break;
565 case EOpAtanh:
566 preString = "atanh(";
567 break;
568
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700569 case EOpExp:
570 preString = "exp(";
571 break;
572 case EOpLog:
573 preString = "log(";
574 break;
575 case EOpExp2:
576 preString = "exp2(";
577 break;
578 case EOpLog2:
579 preString = "log2(";
580 break;
581 case EOpSqrt:
582 preString = "sqrt(";
583 break;
584 case EOpInverseSqrt:
585 preString = "inversesqrt(";
586 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000587
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700588 case EOpAbs:
589 preString = "abs(";
590 break;
591 case EOpSign:
592 preString = "sign(";
593 break;
594 case EOpFloor:
595 preString = "floor(";
596 break;
Qingqing Deng5dbece52015-02-27 20:35:38 -0800597 case EOpTrunc:
598 preString = "trunc(";
599 break;
600 case EOpRound:
601 preString = "round(";
602 break;
603 case EOpRoundEven:
604 preString = "roundEven(";
605 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700606 case EOpCeil:
607 preString = "ceil(";
608 break;
609 case EOpFract:
610 preString = "fract(";
611 break;
Arun Patole0c1726e2015-02-18 14:35:02 +0530612 case EOpIsNan:
613 preString = "isnan(";
614 break;
615 case EOpIsInf:
616 preString = "isinf(";
617 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000618
Olli Etuahoe8d2c072015-01-08 16:33:54 +0200619 case EOpFloatBitsToInt:
620 preString = "floatBitsToInt(";
621 break;
622 case EOpFloatBitsToUint:
623 preString = "floatBitsToUint(";
624 break;
625 case EOpIntBitsToFloat:
626 preString = "intBitsToFloat(";
627 break;
628 case EOpUintBitsToFloat:
629 preString = "uintBitsToFloat(";
630 break;
631
Olli Etuaho7700ff62015-01-15 12:16:29 +0200632 case EOpPackSnorm2x16:
633 preString = "packSnorm2x16(";
634 break;
635 case EOpPackUnorm2x16:
636 preString = "packUnorm2x16(";
637 break;
638 case EOpPackHalf2x16:
639 preString = "packHalf2x16(";
640 break;
641 case EOpUnpackSnorm2x16:
642 preString = "unpackSnorm2x16(";
643 break;
644 case EOpUnpackUnorm2x16:
645 preString = "unpackUnorm2x16(";
646 break;
647 case EOpUnpackHalf2x16:
648 preString = "unpackHalf2x16(";
649 break;
650
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700651 case EOpLength:
652 preString = "length(";
653 break;
654 case EOpNormalize:
655 preString = "normalize(";
656 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000657
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700658 case EOpDFdx:
659 preString = "dFdx(";
660 break;
661 case EOpDFdy:
662 preString = "dFdy(";
663 break;
664 case EOpFwidth:
665 preString = "fwidth(";
666 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000667
Olli Etuahoe39706d2014-12-30 16:40:36 +0200668 case EOpTranspose:
669 preString = "transpose(";
670 break;
671 case EOpDeterminant:
672 preString = "determinant(";
673 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +0200674 case EOpInverse:
675 preString = "inverse(";
676 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +0200677
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700678 case EOpAny:
679 preString = "any(";
680 break;
681 case EOpAll:
682 preString = "all(";
683 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000684
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700685 default:
686 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000687 }
688
zmo@google.com32e97312011-08-24 01:03:11 +0000689 if (visit == PreVisit && node->getUseEmulatedFunction())
690 preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preString);
691 writeTriplet(visit, preString.c_str(), NULL, postString.c_str());
692
zmo@google.com5601ea02011-06-10 18:23:25 +0000693 return true;
694}
695
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300696bool TOutputGLSLBase::visitTernary(Visit visit, TIntermTernary *node)
697{
698 TInfoSinkBase &out = objSink();
699 // Notice two brackets at the beginning and end. The outer ones
700 // encapsulate the whole ternary expression. This preserves the
701 // order of precedence when ternary expressions are used in a
702 // compound expression, i.e., c = 2 * (a < b ? 1 : 2).
703 out << "((";
704 node->getCondition()->traverse(this);
705 out << ") ? (";
706 node->getTrueExpression()->traverse(this);
707 out << ") : (";
708 node->getFalseExpression()->traverse(this);
709 out << "))";
710 return false;
711}
712
Olli Etuaho57961272016-09-14 13:57:46 +0300713bool TOutputGLSLBase::visitIfElse(Visit visit, TIntermIfElse *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000714{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700715 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000716
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300717 out << "if (";
718 node->getCondition()->traverse(this);
719 out << ")\n";
zmo@google.com5601ea02011-06-10 18:23:25 +0000720
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300721 incrementDepth(node);
722 visitCodeBlock(node->getTrueBlock());
zmo@google.com5601ea02011-06-10 18:23:25 +0000723
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300724 if (node->getFalseBlock())
725 {
726 out << "else\n";
727 visitCodeBlock(node->getFalseBlock());
zmo@google.com5601ea02011-06-10 18:23:25 +0000728 }
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300729 decrementDepth();
zmo@google.com5601ea02011-06-10 18:23:25 +0000730 return false;
731}
732
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200733bool TOutputGLSLBase::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200734{
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200735 if (node->getStatementList())
736 {
737 writeTriplet(visit, "switch (", ") ", nullptr);
738 // The curly braces get written when visiting the statementList aggregate
739 }
740 else
741 {
742 // No statementList, so it won't output curly braces
743 writeTriplet(visit, "switch (", ") {", "}\n");
744 }
745 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200746}
747
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200748bool TOutputGLSLBase::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200749{
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200750 if (node->hasCondition())
751 {
752 writeTriplet(visit, "case (", nullptr, "):\n");
753 return true;
754 }
755 else
756 {
757 TInfoSinkBase &out = objSink();
758 out << "default:\n";
759 return false;
760 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200761}
762
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100763bool TOutputGLSLBase::visitBlock(Visit visit, TIntermBlock *node)
764{
765 TInfoSinkBase &out = objSink();
766 // Scope the blocks except when at the global scope.
767 if (mDepth > 0)
768 {
769 out << "{\n";
770 }
771
772 incrementDepth(node);
773 for (TIntermSequence::const_iterator iter = node->getSequence()->begin();
774 iter != node->getSequence()->end(); ++iter)
775 {
776 TIntermNode *curNode = *iter;
777 ASSERT(curNode != nullptr);
778 curNode->traverse(this);
779
780 if (isSingleStatement(curNode))
781 out << ";\n";
782 }
783 decrementDepth();
784
785 // Scope the blocks except when at the global scope.
786 if (mDepth > 0)
787 {
788 out << "}\n";
789 }
790 return false;
791}
792
Olli Etuaho336b1472016-10-05 16:37:55 +0100793bool TOutputGLSLBase::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
794{
795 TInfoSinkBase &out = objSink();
796
797 ASSERT(visit == PreVisit);
798 {
799 const TType &type = node->getType();
800 writeVariableType(type);
801 if (type.isArray())
802 out << arrayBrackets(type);
803 }
804
805 out << " " << hashFunctionNameIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
806
807 incrementDepth(node);
808
809 // Traverse function parameters.
810 TIntermAggregate *params = node->getFunctionParameters()->getAsAggregate();
811 ASSERT(params->getOp() == EOpParameters);
812 params->traverse(this);
813
814 // Traverse function body.
815 visitCodeBlock(node->getBody());
816 decrementDepth();
817
818 // Fully processed; no need to visit children.
819 return false;
820}
821
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700822bool TOutputGLSLBase::visitAggregate(Visit visit, TIntermAggregate *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000823{
824 bool visitChildren = true;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700825 TInfoSinkBase &out = objSink();
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700826 bool useEmulatedFunction = (visit == PreVisit && node->getUseEmulatedFunction());
zmo@google.com5601ea02011-06-10 18:23:25 +0000827 switch (node->getOp())
828 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700829 case EOpPrototype:
830 // Function declaration.
831 ASSERT(visit == PreVisit);
Olli Etuahoab6fc6a2015-04-13 12:10:20 +0300832 {
833 const TType &type = node->getType();
834 writeVariableType(type);
835 if (type.isArray())
836 out << arrayBrackets(type);
837 }
838
Olli Etuahobd674552016-10-06 13:28:42 +0100839 out << " " << hashFunctionNameIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700840
841 out << "(";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700842 writeFunctionParameters(*(node->getSequence()));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700843 out << ")";
844
845 visitChildren = false;
846 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700847 case EOpFunctionCall:
848 // Function call.
849 if (visit == PreVisit)
Olli Etuahobd674552016-10-06 13:28:42 +0100850 out << hashFunctionNameIfNeeded(node->getFunctionSymbolInfo()->getNameObj()) << "(";
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200851 else if (visit == InVisit)
852 out << ", ";
853 else
854 out << ")";
855 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700856 case EOpParameters:
857 // Function parameters.
858 ASSERT(visit == PreVisit);
859 out << "(";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700860 writeFunctionParameters(*(node->getSequence()));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700861 out << ")";
862 visitChildren = false;
863 break;
864 case EOpDeclaration:
865 // Variable declaration.
866 if (visit == PreVisit)
867 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700868 const TIntermSequence &sequence = *(node->getSequence());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700869 const TIntermTyped *variable = sequence.front()->getAsTyped();
Olli Etuahoae69d7e2015-10-07 17:19:50 +0300870 writeLayoutQualifier(variable->getType());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700871 writeVariableType(variable->getType());
872 out << " ";
873 mDeclaringVariables = true;
zmo@google.com5601ea02011-06-10 18:23:25 +0000874 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700875 else if (visit == InVisit)
876 {
877 out << ", ";
878 mDeclaringVariables = true;
zmo@google.com5601ea02011-06-10 18:23:25 +0000879 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700880 else
881 {
882 mDeclaringVariables = false;
883 }
884 break;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700885 case EOpInvariantDeclaration:
886 // Invariant declaration.
887 ASSERT(visit == PreVisit);
888 {
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400889 const TIntermSequence *sequence = node->getSequence();
890 ASSERT(sequence && sequence->size() == 1);
891 const TIntermSymbol *symbol = sequence->front()->getAsSymbolNode();
892 ASSERT(symbol);
Zhenyao Mo2a517272014-10-27 16:09:57 -0700893 out << "invariant " << hashVariableName(symbol->getSymbol());
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400894 }
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700895 visitChildren = false;
896 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700897 case EOpConstructFloat:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700898 case EOpConstructVec2:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700899 case EOpConstructVec3:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700900 case EOpConstructVec4:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700901 case EOpConstructBool:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700902 case EOpConstructBVec2:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700903 case EOpConstructBVec3:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700904 case EOpConstructBVec4:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700905 case EOpConstructInt:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700906 case EOpConstructIVec2:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700907 case EOpConstructIVec3:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700908 case EOpConstructIVec4:
Geoff Langc4c7442d2015-07-20 13:09:26 -0400909 case EOpConstructUInt:
Geoff Langc4c7442d2015-07-20 13:09:26 -0400910 case EOpConstructUVec2:
Geoff Langc4c7442d2015-07-20 13:09:26 -0400911 case EOpConstructUVec3:
Geoff Langc4c7442d2015-07-20 13:09:26 -0400912 case EOpConstructUVec4:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700913 case EOpConstructMat2:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400914 case EOpConstructMat2x3:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400915 case EOpConstructMat2x4:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400916 case EOpConstructMat3x2:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700917 case EOpConstructMat3:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400918 case EOpConstructMat3x4:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400919 case EOpConstructMat4x2:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400920 case EOpConstructMat4x3:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700921 case EOpConstructMat4:
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700922 case EOpConstructStruct:
Olli Etuahoe92507b2016-07-04 11:20:10 +0300923 writeConstructorTriplet(visit, node->getType());
924 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000925
Olli Etuahoe39706d2014-12-30 16:40:36 +0200926 case EOpOuterProduct:
927 writeBuiltInFunctionTriplet(visit, "outerProduct(", useEmulatedFunction);
928 break;
929
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700930 case EOpLessThan:
931 writeBuiltInFunctionTriplet(visit, "lessThan(", useEmulatedFunction);
932 break;
933 case EOpGreaterThan:
934 writeBuiltInFunctionTriplet(visit, "greaterThan(", useEmulatedFunction);
935 break;
936 case EOpLessThanEqual:
937 writeBuiltInFunctionTriplet(visit, "lessThanEqual(", useEmulatedFunction);
938 break;
939 case EOpGreaterThanEqual:
940 writeBuiltInFunctionTriplet(visit, "greaterThanEqual(", useEmulatedFunction);
941 break;
942 case EOpVectorEqual:
943 writeBuiltInFunctionTriplet(visit, "equal(", useEmulatedFunction);
944 break;
945 case EOpVectorNotEqual:
946 writeBuiltInFunctionTriplet(visit, "notEqual(", useEmulatedFunction);
947 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000948
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700949 case EOpMod:
950 writeBuiltInFunctionTriplet(visit, "mod(", useEmulatedFunction);
951 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +0200952 case EOpModf:
953 writeBuiltInFunctionTriplet(visit, "modf(", useEmulatedFunction);
954 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700955 case EOpPow:
956 writeBuiltInFunctionTriplet(visit, "pow(", useEmulatedFunction);
957 break;
958 case EOpAtan:
959 writeBuiltInFunctionTriplet(visit, "atan(", useEmulatedFunction);
960 break;
961 case EOpMin:
962 writeBuiltInFunctionTriplet(visit, "min(", useEmulatedFunction);
963 break;
964 case EOpMax:
965 writeBuiltInFunctionTriplet(visit, "max(", useEmulatedFunction);
966 break;
967 case EOpClamp:
968 writeBuiltInFunctionTriplet(visit, "clamp(", useEmulatedFunction);
969 break;
970 case EOpMix:
971 writeBuiltInFunctionTriplet(visit, "mix(", useEmulatedFunction);
972 break;
973 case EOpStep:
974 writeBuiltInFunctionTriplet(visit, "step(", useEmulatedFunction);
975 break;
976 case EOpSmoothStep:
977 writeBuiltInFunctionTriplet(visit, "smoothstep(", useEmulatedFunction);
978 break;
979 case EOpDistance:
980 writeBuiltInFunctionTriplet(visit, "distance(", useEmulatedFunction);
981 break;
982 case EOpDot:
983 writeBuiltInFunctionTriplet(visit, "dot(", useEmulatedFunction);
984 break;
985 case EOpCross:
986 writeBuiltInFunctionTriplet(visit, "cross(", useEmulatedFunction);
987 break;
988 case EOpFaceForward:
989 writeBuiltInFunctionTriplet(visit, "faceforward(", useEmulatedFunction);
990 break;
991 case EOpReflect:
992 writeBuiltInFunctionTriplet(visit, "reflect(", useEmulatedFunction);
993 break;
994 case EOpRefract:
995 writeBuiltInFunctionTriplet(visit, "refract(", useEmulatedFunction);
996 break;
997 case EOpMul:
998 writeBuiltInFunctionTriplet(visit, "matrixCompMult(", useEmulatedFunction);
999 break;
zmo@google.com5601ea02011-06-10 18:23:25 +00001000
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001001 default:
1002 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +00001003 }
zmo@google.com5601ea02011-06-10 18:23:25 +00001004 return visitChildren;
1005}
1006
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001007bool TOutputGLSLBase::visitLoop(Visit visit, TIntermLoop *node)
zmo@google.com5601ea02011-06-10 18:23:25 +00001008{
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001009 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +00001010
Zhenyao Mo7cab38b2013-10-15 12:59:30 -07001011 incrementDepth(node);
Corentin Wallez7258e302015-09-22 10:40:24 -07001012
zmo@google.com5601ea02011-06-10 18:23:25 +00001013 TLoopType loopType = node->getType();
Corentin Wallez7258e302015-09-22 10:40:24 -07001014
1015 // Only for loops can be unrolled
1016 ASSERT(!node->getUnrollFlag() || loopType == ELoopFor);
1017
zmo@google.com5601ea02011-06-10 18:23:25 +00001018 if (loopType == ELoopFor) // for loop
1019 {
Zhenyao Mo550c6002014-02-26 15:40:48 -08001020 if (!node->getUnrollFlag())
1021 {
zmo@google.com5601ea02011-06-10 18:23:25 +00001022 out << "for (";
1023 if (node->getInit())
1024 node->getInit()->traverse(this);
1025 out << "; ";
1026
1027 if (node->getCondition())
1028 node->getCondition()->traverse(this);
1029 out << "; ";
1030
1031 if (node->getExpression())
1032 node->getExpression()->traverse(this);
1033 out << ")\n";
Corentin Wallez7258e302015-09-22 10:40:24 -07001034
1035 visitCodeBlock(node->getBody());
zmo@google.com5601ea02011-06-10 18:23:25 +00001036 }
Zhenyao Mo550c6002014-02-26 15:40:48 -08001037 else
1038 {
1039 // Need to put a one-iteration loop here to handle break.
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001040 TIntermSequence *declSeq =
Zhenyao Mo550c6002014-02-26 15:40:48 -08001041 node->getInit()->getAsAggregate()->getSequence();
1042 TIntermSymbol *indexSymbol =
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001043 (*declSeq)[0]->getAsBinaryNode()->getLeft()->getAsSymbolNode();
Zhenyao Mo550c6002014-02-26 15:40:48 -08001044 TString name = hashVariableName(indexSymbol->getSymbol());
1045 out << "for (int " << name << " = 0; "
1046 << name << " < 1; "
1047 << "++" << name << ")\n";
Corentin Wallez7258e302015-09-22 10:40:24 -07001048
1049 out << "{\n";
1050 mLoopUnrollStack.push(node);
1051 while (mLoopUnrollStack.satisfiesLoopCondition())
1052 {
1053 visitCodeBlock(node->getBody());
1054 mLoopUnrollStack.step();
1055 }
1056 mLoopUnrollStack.pop();
1057 out << "}\n";
Zhenyao Mo550c6002014-02-26 15:40:48 -08001058 }
zmo@google.com5601ea02011-06-10 18:23:25 +00001059 }
1060 else if (loopType == ELoopWhile) // while loop
1061 {
1062 out << "while (";
1063 ASSERT(node->getCondition() != NULL);
1064 node->getCondition()->traverse(this);
1065 out << ")\n";
Corentin Wallez7258e302015-09-22 10:40:24 -07001066
1067 visitCodeBlock(node->getBody());
zmo@google.com5601ea02011-06-10 18:23:25 +00001068 }
1069 else // do-while loop
1070 {
1071 ASSERT(loopType == ELoopDoWhile);
1072 out << "do\n";
zmo@google.com5601ea02011-06-10 18:23:25 +00001073
zmo@google.com5601ea02011-06-10 18:23:25 +00001074 visitCodeBlock(node->getBody());
zmo@google.com5601ea02011-06-10 18:23:25 +00001075
zmo@google.com5601ea02011-06-10 18:23:25 +00001076 out << "while (";
1077 ASSERT(node->getCondition() != NULL);
1078 node->getCondition()->traverse(this);
1079 out << ");\n";
1080 }
Corentin Wallez7258e302015-09-22 10:40:24 -07001081
zmo@google.com5601ea02011-06-10 18:23:25 +00001082 decrementDepth();
1083
1084 // No need to visit children. They have been already processed in
1085 // this function.
1086 return false;
1087}
1088
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001089bool TOutputGLSLBase::visitBranch(Visit visit, TIntermBranch *node)
zmo@google.com5601ea02011-06-10 18:23:25 +00001090{
1091 switch (node->getFlowOp())
1092 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001093 case EOpKill:
1094 writeTriplet(visit, "discard", NULL, NULL);
1095 break;
1096 case EOpBreak:
1097 writeTriplet(visit, "break", NULL, NULL);
1098 break;
1099 case EOpContinue:
1100 writeTriplet(visit, "continue", NULL, NULL);
1101 break;
1102 case EOpReturn:
1103 writeTriplet(visit, "return ", NULL, NULL);
1104 break;
1105 default:
1106 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +00001107 }
1108
1109 return true;
1110}
1111
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001112void TOutputGLSLBase::visitCodeBlock(TIntermBlock *node)
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001113{
zmo@google.com5601ea02011-06-10 18:23:25 +00001114 TInfoSinkBase &out = objSink();
1115 if (node != NULL)
1116 {
1117 node->traverse(this);
1118 // Single statements not part of a sequence need to be terminated
1119 // with semi-colon.
1120 if (isSingleStatement(node))
1121 out << ";\n";
1122 }
1123 else
1124 {
1125 out << "{\n}\n"; // Empty code block.
1126 }
1127}
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001128
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001129TString TOutputGLSLBase::getTypeName(const TType &type)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001130{
Olli Etuahoe92507b2016-07-04 11:20:10 +03001131 if (type.getBasicType() == EbtStruct)
1132 return hashName(type.getStruct()->name());
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001133 else
Olli Etuahoe92507b2016-07-04 11:20:10 +03001134 return type.getBuiltInTypeNameString();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001135}
1136
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001137TString TOutputGLSLBase::hashName(const TString &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001138{
1139 if (mHashFunction == NULL || name.empty())
1140 return name;
1141 NameMap::const_iterator it = mNameMap.find(name.c_str());
1142 if (it != mNameMap.end())
1143 return it->second.c_str();
1144 TString hashedName = TIntermTraverser::hash(name, mHashFunction);
1145 mNameMap[name.c_str()] = hashedName.c_str();
1146 return hashedName;
1147}
1148
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001149TString TOutputGLSLBase::hashVariableName(const TString &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001150{
Jamie Madill02f20dd2013-09-12 12:07:42 -04001151 if (mSymbolTable.findBuiltIn(name, mShaderVersion) != NULL)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001152 return name;
1153 return hashName(name);
1154}
1155
Olli Etuaho59f9a642015-08-06 20:38:26 +03001156TString TOutputGLSLBase::hashFunctionNameIfNeeded(const TName &mangledName)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001157{
Olli Etuaho59f9a642015-08-06 20:38:26 +03001158 TString mangledStr = mangledName.getString();
1159 TString name = TFunction::unmangleName(mangledStr);
1160 if (mSymbolTable.findBuiltIn(mangledStr, mShaderVersion) != nullptr || name == "main")
Nicolas Capens46485082014-04-15 13:12:50 -04001161 return translateTextureFunction(name);
Olli Etuaho59f9a642015-08-06 20:38:26 +03001162 if (mangledName.isInternal())
1163 return name;
1164 else
1165 return hashName(name);
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001166}
Jamie Madill98493dd2013-07-08 14:39:03 -04001167
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001168bool TOutputGLSLBase::structDeclared(const TStructure *structure) const
Jamie Madill98493dd2013-07-08 14:39:03 -04001169{
Zhenyao Mo904a9162014-05-09 14:07:45 -07001170 ASSERT(structure);
Jamie Madill01f85ac2014-06-06 11:55:04 -04001171 if (structure->name().empty())
Zhenyao Mo904a9162014-05-09 14:07:45 -07001172 {
Jamie Madill01f85ac2014-06-06 11:55:04 -04001173 return false;
Zhenyao Mo904a9162014-05-09 14:07:45 -07001174 }
Jamie Madill01f85ac2014-06-06 11:55:04 -04001175
1176 return (mDeclaredStructs.count(structure->uniqueId()) > 0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001177}
1178
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001179void TOutputGLSLBase::declareStruct(const TStructure *structure)
Jamie Madill98493dd2013-07-08 14:39:03 -04001180{
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001181 TInfoSinkBase &out = objSink();
Jamie Madill98493dd2013-07-08 14:39:03 -04001182
1183 out << "struct " << hashName(structure->name()) << "{\n";
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001184 const TFieldList &fields = structure->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04001185 for (size_t i = 0; i < fields.size(); ++i)
1186 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001187 const TField *field = fields[i];
Jamie Madill98493dd2013-07-08 14:39:03 -04001188 if (writeVariablePrecision(field->type()->getPrecision()))
1189 out << " ";
1190 out << getTypeName(*field->type()) << " " << hashName(field->name());
1191 if (field->type()->isArray())
1192 out << arrayBrackets(*field->type());
1193 out << ";\n";
1194 }
1195 out << "}";
Zhenyao Mo904a9162014-05-09 14:07:45 -07001196}
Jamie Madill98493dd2013-07-08 14:39:03 -04001197
Geoff Langbdcc54a2015-09-02 13:09:48 -04001198void TOutputGLSLBase::declareInterfaceBlockLayout(const TInterfaceBlock *interfaceBlock)
1199{
1200 TInfoSinkBase &out = objSink();
1201
1202 out << "layout(";
1203
1204 switch (interfaceBlock->blockStorage())
1205 {
1206 case EbsUnspecified:
1207 case EbsShared:
1208 // Default block storage is shared.
1209 out << "shared";
1210 break;
1211
1212 case EbsPacked:
1213 out << "packed";
1214 break;
1215
1216 case EbsStd140:
1217 out << "std140";
1218 break;
1219
1220 default:
1221 UNREACHABLE();
1222 break;
1223 }
1224
1225 out << ", ";
1226
1227 switch (interfaceBlock->matrixPacking())
1228 {
1229 case EmpUnspecified:
1230 case EmpColumnMajor:
1231 // Default matrix packing is column major.
1232 out << "column_major";
1233 break;
1234
1235 case EmpRowMajor:
1236 out << "row_major";
1237 break;
1238
1239 default:
1240 UNREACHABLE();
1241 break;
1242 }
1243
1244 out << ") ";
1245}
1246
1247void TOutputGLSLBase::declareInterfaceBlock(const TInterfaceBlock *interfaceBlock)
1248{
1249 TInfoSinkBase &out = objSink();
1250
1251 out << hashName(interfaceBlock->name()) << "{\n";
1252 const TFieldList &fields = interfaceBlock->fields();
1253 for (size_t i = 0; i < fields.size(); ++i)
1254 {
1255 const TField *field = fields[i];
1256 if (writeVariablePrecision(field->type()->getPrecision()))
1257 out << " ";
1258 out << getTypeName(*field->type()) << " " << hashName(field->name());
1259 if (field->type()->isArray())
1260 out << arrayBrackets(*field->type());
1261 out << ";\n";
1262 }
1263 out << "}";
1264}