blob: 5f5e05822865ee5be02b922049a7e84481e676a7 [file] [log] [blame]
zmo@google.com5601ea02011-06-10 18:23:25 +00001//
Nicolas Capens16004fc2014-06-11 11:29:11 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
zmo@google.com5601ea02011-06-10 18:23:25 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/OutputGLSLBase.h"
8#include "compiler/translator/compilerdebug.h"
zmo@google.com5601ea02011-06-10 18:23:25 +00009
daniel@transgaming.com773ff742013-01-11 04:12:51 +000010#include <cfloat>
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +000011
zmo@google.com5601ea02011-06-10 18:23:25 +000012namespace
13{
Zhenyao Mo9eedea02014-05-12 16:02:35 -070014TString arrayBrackets(const TType &type)
zmo@google.com5601ea02011-06-10 18:23:25 +000015{
16 ASSERT(type.isArray());
17 TInfoSinkBase out;
18 out << "[" << type.getArraySize() << "]";
19 return TString(out.c_str());
20}
21
Zhenyao Mo9eedea02014-05-12 16:02:35 -070022bool isSingleStatement(TIntermNode *node)
23{
24 if (const TIntermAggregate *aggregate = node->getAsAggregate())
zmo@google.com5601ea02011-06-10 18:23:25 +000025 {
26 return (aggregate->getOp() != EOpFunction) &&
27 (aggregate->getOp() != EOpSequence);
28 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -070029 else if (const TIntermSelection *selection = node->getAsSelectionNode())
zmo@google.com5601ea02011-06-10 18:23:25 +000030 {
31 // Ternary operators are usually part of an assignment operator.
32 // This handles those rare cases in which they are all by themselves.
33 return selection->usesTernaryOperator();
34 }
35 else if (node->getAsLoopNode())
36 {
37 return false;
38 }
39 return true;
40}
41} // namespace
42
Zhenyao Mo9eedea02014-05-12 16:02:35 -070043TOutputGLSLBase::TOutputGLSLBase(TInfoSinkBase &objSink,
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +000044 ShArrayIndexClampingStrategy clampingStrategy,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000045 ShHashFunction64 hashFunction,
Zhenyao Mo9eedea02014-05-12 16:02:35 -070046 NameMap &nameMap,
47 TSymbolTable &symbolTable,
Jamie Madill02f20dd2013-09-12 12:07:42 -040048 int shaderVersion)
zmo@google.com5601ea02011-06-10 18:23:25 +000049 : TIntermTraverser(true, true, true),
50 mObjSink(objSink),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000051 mDeclaringVariables(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +000052 mClampingStrategy(clampingStrategy),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000053 mHashFunction(hashFunction),
54 mNameMap(nameMap),
Jamie Madill02f20dd2013-09-12 12:07:42 -040055 mSymbolTable(symbolTable),
56 mShaderVersion(shaderVersion)
zmo@google.com5601ea02011-06-10 18:23:25 +000057{
58}
59
Zhenyao Mo9eedea02014-05-12 16:02:35 -070060void TOutputGLSLBase::writeTriplet(
61 Visit visit, const char *preStr, const char *inStr, const char *postStr)
zmo@google.com5601ea02011-06-10 18:23:25 +000062{
Zhenyao Mo9eedea02014-05-12 16:02:35 -070063 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +000064 if (visit == PreVisit && preStr)
zmo@google.com5601ea02011-06-10 18:23:25 +000065 out << preStr;
zmo@google.com5601ea02011-06-10 18:23:25 +000066 else if (visit == InVisit && inStr)
zmo@google.com5601ea02011-06-10 18:23:25 +000067 out << inStr;
zmo@google.com5601ea02011-06-10 18:23:25 +000068 else if (visit == PostVisit && postStr)
zmo@google.com5601ea02011-06-10 18:23:25 +000069 out << postStr;
zmo@google.com5601ea02011-06-10 18:23:25 +000070}
71
Zhenyao Mo9eedea02014-05-12 16:02:35 -070072void TOutputGLSLBase::writeBuiltInFunctionTriplet(
73 Visit visit, const char *preStr, bool useEmulatedFunction)
zmo@google.com5601ea02011-06-10 18:23:25 +000074{
Zhenyao Mo9eedea02014-05-12 16:02:35 -070075 TString preString = useEmulatedFunction ?
76 BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr) : preStr;
77 writeTriplet(visit, preString.c_str(), ", ", ")");
78}
79
80void TOutputGLSLBase::writeVariableType(const TType &type)
81{
82 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +000083 TQualifier qualifier = type.getQualifier();
Jamie Madill3b5c2da2014-08-19 15:23:32 -040084 if (qualifier != EvqTemporary && qualifier != EvqGlobal)
Jamie Madill1c28e1f2014-08-04 11:37:54 -040085 {
zmo@google.com5601ea02011-06-10 18:23:25 +000086 out << type.getQualifierString() << " ";
Jamie Madill1c28e1f2014-08-04 11:37:54 -040087 }
zmo@google.com5601ea02011-06-10 18:23:25 +000088 // Declare the struct if we have not done so already.
Zhenyao Mo9eedea02014-05-12 16:02:35 -070089 if (type.getBasicType() == EbtStruct && !structDeclared(type.getStruct()))
zmo@google.com5601ea02011-06-10 18:23:25 +000090 {
Jamie Madill01f85ac2014-06-06 11:55:04 -040091 TStructure *structure = type.getStruct();
92
93 declareStruct(structure);
94
95 if (!structure->name().empty())
96 {
97 mDeclaredStructs.insert(structure->uniqueId());
98 }
zmo@google.com5601ea02011-06-10 18:23:25 +000099 }
100 else
101 {
102 if (writeVariablePrecision(type.getPrecision()))
103 out << " ";
104 out << getTypeName(type);
105 }
106}
107
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700108void TOutputGLSLBase::writeFunctionParameters(const TIntermSequence &args)
zmo@google.com5601ea02011-06-10 18:23:25 +0000109{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700110 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000111 for (TIntermSequence::const_iterator iter = args.begin();
112 iter != args.end(); ++iter)
113 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700114 const TIntermSymbol *arg = (*iter)->getAsSymbolNode();
zmo@google.com5601ea02011-06-10 18:23:25 +0000115 ASSERT(arg != NULL);
116
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700117 const TType &type = arg->getType();
zmo@google.com189be2f2011-06-16 18:28:53 +0000118 writeVariableType(type);
zmo@google.com5601ea02011-06-10 18:23:25 +0000119
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700120 const TString &name = arg->getSymbol();
zmo@google.com5601ea02011-06-10 18:23:25 +0000121 if (!name.empty())
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000122 out << " " << hashName(name);
zmo@google.com5601ea02011-06-10 18:23:25 +0000123 if (type.isArray())
124 out << arrayBrackets(type);
125
126 // Put a comma if this is not the last argument.
127 if (iter != args.end() - 1)
128 out << ", ";
129 }
130}
131
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700132const ConstantUnion *TOutputGLSLBase::writeConstantUnion(
133 const TType &type, const ConstantUnion *pConstUnion)
zmo@google.com5601ea02011-06-10 18:23:25 +0000134{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700135 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000136
137 if (type.getBasicType() == EbtStruct)
138 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700139 const TStructure *structure = type.getStruct();
Jamie Madill98493dd2013-07-08 14:39:03 -0400140 out << hashName(structure->name()) << "(";
141
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700142 const TFieldList &fields = structure->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -0400143 for (size_t i = 0; i < fields.size(); ++i)
zmo@google.com5601ea02011-06-10 18:23:25 +0000144 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700145 const TType *fieldType = fields[i]->type();
zmo@google.com5601ea02011-06-10 18:23:25 +0000146 ASSERT(fieldType != NULL);
147 pConstUnion = writeConstantUnion(*fieldType, pConstUnion);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700148 if (i != fields.size() - 1)
149 out << ", ";
zmo@google.com5601ea02011-06-10 18:23:25 +0000150 }
151 out << ")";
152 }
153 else
154 {
Jamie Madill94bf7f22013-07-08 13:31:15 -0400155 size_t size = type.getObjectSize();
zmo@google.com5601ea02011-06-10 18:23:25 +0000156 bool writeType = size > 1;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700157 if (writeType)
158 out << getTypeName(type) << "(";
Jamie Madill94bf7f22013-07-08 13:31:15 -0400159 for (size_t i = 0; i < size; ++i, ++pConstUnion)
zmo@google.com5601ea02011-06-10 18:23:25 +0000160 {
161 switch (pConstUnion->getType())
162 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700163 case EbtFloat:
164 out << std::min(FLT_MAX, std::max(-FLT_MAX, pConstUnion->getFConst()));
165 break;
166 case EbtInt:
167 out << pConstUnion->getIConst();
168 break;
169 case EbtBool:
170 out << pConstUnion->getBConst();
171 break;
172 default: UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000173 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700174 if (i != size - 1)
175 out << ", ";
zmo@google.com5601ea02011-06-10 18:23:25 +0000176 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700177 if (writeType)
178 out << ")";
zmo@google.com5601ea02011-06-10 18:23:25 +0000179 }
180 return pConstUnion;
181}
182
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700183void TOutputGLSLBase::visitSymbol(TIntermSymbol *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000184{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700185 TInfoSinkBase &out = objSink();
Zhenyao Mo550c6002014-02-26 15:40:48 -0800186 if (mLoopUnrollStack.needsToReplaceSymbolWithValue(node))
187 out << mLoopUnrollStack.getLoopIndexValue(node);
zmo@google.com5601ea02011-06-10 18:23:25 +0000188 else
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000189 out << hashVariableName(node->getSymbol());
zmo@google.com5601ea02011-06-10 18:23:25 +0000190
191 if (mDeclaringVariables && node->getType().isArray())
192 out << arrayBrackets(node->getType());
193}
194
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700195void TOutputGLSLBase::visitConstantUnion(TIntermConstantUnion *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000196{
197 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
198}
199
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700200bool TOutputGLSLBase::visitBinary(Visit visit, TIntermBinary *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000201{
202 bool visitChildren = true;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700203 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000204 switch (node->getOp())
205 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700206 case EOpInitialize:
207 if (visit == InVisit)
208 {
209 out << " = ";
210 // RHS of initialize is not being declared.
211 mDeclaringVariables = false;
212 }
213 break;
214 case EOpAssign:
215 writeTriplet(visit, "(", " = ", ")");
216 break;
217 case EOpAddAssign:
218 writeTriplet(visit, "(", " += ", ")");
219 break;
220 case EOpSubAssign:
221 writeTriplet(visit, "(", " -= ", ")");
222 break;
223 case EOpDivAssign:
224 writeTriplet(visit, "(", " /= ", ")");
225 break;
226 // Notice the fall-through.
227 case EOpMulAssign:
228 case EOpVectorTimesMatrixAssign:
229 case EOpVectorTimesScalarAssign:
230 case EOpMatrixTimesScalarAssign:
231 case EOpMatrixTimesMatrixAssign:
232 writeTriplet(visit, "(", " *= ", ")");
233 break;
234
235 case EOpIndexDirect:
236 writeTriplet(visit, NULL, "[", "]");
237 break;
238 case EOpIndexIndirect:
239 if (node->getAddIndexClamp())
240 {
zmo@google.com5601ea02011-06-10 18:23:25 +0000241 if (visit == InVisit)
242 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700243 if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC)
244 out << "[int(clamp(float(";
245 else
246 out << "[webgl_int_clamp(";
zmo@google.com5601ea02011-06-10 18:23:25 +0000247 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700248 else if (visit == PostVisit)
249 {
250 int maxSize;
251 TIntermTyped *left = node->getLeft();
252 TType leftType = left->getType();
zmo@google.com5601ea02011-06-10 18:23:25 +0000253
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700254 if (left->isArray())
255 {
256 // The shader will fail validation if the array length is not > 0.
257 maxSize = leftType.getArraySize() - 1;
258 }
259 else
260 {
261 maxSize = leftType.getNominalSize() - 1;
262 }
263
264 if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC)
265 out << "), 0.0, float(" << maxSize << ")))]";
266 else
267 out << ", 0, " << maxSize << ")]";
268 }
269 }
270 else
271 {
zmo@google.com5601ea02011-06-10 18:23:25 +0000272 writeTriplet(visit, NULL, "[", "]");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700273 }
274 break;
275 case EOpIndexDirectStruct:
276 if (visit == InVisit)
277 {
278 // Here we are writing out "foo.bar", where "foo" is struct
279 // and "bar" is field. In AST, it is represented as a binary
280 // node, where left child represents "foo" and right child "bar".
281 // The node itself represents ".". The struct field "bar" is
282 // actually stored as an index into TStructure::fields.
283 out << ".";
284 const TStructure *structure = node->getLeft()->getType().getStruct();
285 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
286 const TField *field = structure->fields()[index->getIConst(0)];
287
288 TString fieldName = field->name();
289 if (!mSymbolTable.findBuiltIn(structure->name(), mShaderVersion))
290 fieldName = hashName(fieldName);
291
292 out << fieldName;
293 visitChildren = false;
294 }
295 break;
296 case EOpVectorSwizzle:
297 if (visit == InVisit)
298 {
299 out << ".";
300 TIntermAggregate *rightChild = node->getRight()->getAsAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700301 TIntermSequence *sequence = rightChild->getSequence();
302 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); ++sit)
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000303 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700304 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
305 ASSERT(element->getBasicType() == EbtInt);
306 ASSERT(element->getNominalSize() == 1);
307 const ConstantUnion& data = element->getUnionArrayPointer()[0];
308 ASSERT(data.getType() == EbtInt);
309 switch (data.getIConst())
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000310 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700311 case 0:
312 out << "x";
313 break;
314 case 1:
315 out << "y";
316 break;
317 case 2:
318 out << "z";
319 break;
320 case 3:
321 out << "w";
322 break;
323 default:
324 UNREACHABLE();
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000325 }
326 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700327 visitChildren = false;
328 }
329 break;
daniel@transgaming.com97b16d12013-02-01 03:20:42 +0000330
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700331 case EOpAdd:
332 writeTriplet(visit, "(", " + ", ")");
333 break;
334 case EOpSub:
335 writeTriplet(visit, "(", " - ", ")");
336 break;
337 case EOpMul:
338 writeTriplet(visit, "(", " * ", ")");
339 break;
340 case EOpDiv:
341 writeTriplet(visit, "(", " / ", ")");
342 break;
343 case EOpMod:
344 UNIMPLEMENTED();
345 break;
346 case EOpEqual:
347 writeTriplet(visit, "(", " == ", ")");
348 break;
349 case EOpNotEqual:
350 writeTriplet(visit, "(", " != ", ")");
351 break;
352 case EOpLessThan:
353 writeTriplet(visit, "(", " < ", ")");
354 break;
355 case EOpGreaterThan:
356 writeTriplet(visit, "(", " > ", ")");
357 break;
358 case EOpLessThanEqual:
359 writeTriplet(visit, "(", " <= ", ")");
360 break;
361 case EOpGreaterThanEqual:
362 writeTriplet(visit, "(", " >= ", ")");
363 break;
daniel@transgaming.com97b16d12013-02-01 03:20:42 +0000364
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700365 // Notice the fall-through.
366 case EOpVectorTimesScalar:
367 case EOpVectorTimesMatrix:
368 case EOpMatrixTimesVector:
369 case EOpMatrixTimesScalar:
370 case EOpMatrixTimesMatrix:
371 writeTriplet(visit, "(", " * ", ")");
372 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000373
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700374 case EOpLogicalOr:
375 writeTriplet(visit, "(", " || ", ")");
376 break;
377 case EOpLogicalXor:
378 writeTriplet(visit, "(", " ^^ ", ")");
379 break;
380 case EOpLogicalAnd:
381 writeTriplet(visit, "(", " && ", ")");
382 break;
383 default:
384 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000385 }
386
387 return visitChildren;
388}
389
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700390bool TOutputGLSLBase::visitUnary(Visit visit, TIntermUnary *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000391{
zmo@google.com32e97312011-08-24 01:03:11 +0000392 TString preString;
393 TString postString = ")";
394
zmo@google.com5601ea02011-06-10 18:23:25 +0000395 switch (node->getOp())
396 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700397 case EOpNegative: preString = "(-"; break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -0700398 case EOpPositive: preString = "(+"; break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700399 case EOpVectorLogicalNot: preString = "not("; break;
400 case EOpLogicalNot: preString = "(!"; break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000401
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700402 case EOpPostIncrement: preString = "("; postString = "++)"; break;
403 case EOpPostDecrement: preString = "("; postString = "--)"; break;
404 case EOpPreIncrement: preString = "(++"; break;
405 case EOpPreDecrement: preString = "(--"; break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000406
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700407 case EOpRadians:
408 preString = "radians(";
409 break;
410 case EOpDegrees:
411 preString = "degrees(";
412 break;
413 case EOpSin:
414 preString = "sin(";
415 break;
416 case EOpCos:
417 preString = "cos(";
418 break;
419 case EOpTan:
420 preString = "tan(";
421 break;
422 case EOpAsin:
423 preString = "asin(";
424 break;
425 case EOpAcos:
426 preString = "acos(";
427 break;
428 case EOpAtan:
429 preString = "atan(";
430 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000431
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700432 case EOpExp:
433 preString = "exp(";
434 break;
435 case EOpLog:
436 preString = "log(";
437 break;
438 case EOpExp2:
439 preString = "exp2(";
440 break;
441 case EOpLog2:
442 preString = "log2(";
443 break;
444 case EOpSqrt:
445 preString = "sqrt(";
446 break;
447 case EOpInverseSqrt:
448 preString = "inversesqrt(";
449 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000450
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700451 case EOpAbs:
452 preString = "abs(";
453 break;
454 case EOpSign:
455 preString = "sign(";
456 break;
457 case EOpFloor:
458 preString = "floor(";
459 break;
460 case EOpCeil:
461 preString = "ceil(";
462 break;
463 case EOpFract:
464 preString = "fract(";
465 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000466
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700467 case EOpLength:
468 preString = "length(";
469 break;
470 case EOpNormalize:
471 preString = "normalize(";
472 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000473
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700474 case EOpDFdx:
475 preString = "dFdx(";
476 break;
477 case EOpDFdy:
478 preString = "dFdy(";
479 break;
480 case EOpFwidth:
481 preString = "fwidth(";
482 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000483
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700484 case EOpAny:
485 preString = "any(";
486 break;
487 case EOpAll:
488 preString = "all(";
489 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000490
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700491 default:
492 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000493 }
494
zmo@google.com32e97312011-08-24 01:03:11 +0000495 if (visit == PreVisit && node->getUseEmulatedFunction())
496 preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preString);
497 writeTriplet(visit, preString.c_str(), NULL, postString.c_str());
498
zmo@google.com5601ea02011-06-10 18:23:25 +0000499 return true;
500}
501
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700502bool TOutputGLSLBase::visitSelection(Visit visit, TIntermSelection *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000503{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700504 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000505
506 if (node->usesTernaryOperator())
507 {
508 // Notice two brackets at the beginning and end. The outer ones
509 // encapsulate the whole ternary expression. This preserves the
510 // order of precedence when ternary expressions are used in a
511 // compound expression, i.e., c = 2 * (a < b ? 1 : 2).
512 out << "((";
513 node->getCondition()->traverse(this);
514 out << ") ? (";
515 node->getTrueBlock()->traverse(this);
516 out << ") : (";
517 node->getFalseBlock()->traverse(this);
518 out << "))";
519 }
520 else
521 {
522 out << "if (";
523 node->getCondition()->traverse(this);
524 out << ")\n";
525
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700526 incrementDepth(node);
zmo@google.com5601ea02011-06-10 18:23:25 +0000527 visitCodeBlock(node->getTrueBlock());
528
529 if (node->getFalseBlock())
530 {
531 out << "else\n";
532 visitCodeBlock(node->getFalseBlock());
533 }
534 decrementDepth();
535 }
536 return false;
537}
538
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700539bool TOutputGLSLBase::visitAggregate(Visit visit, TIntermAggregate *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000540{
541 bool visitChildren = true;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700542 TInfoSinkBase &out = objSink();
zmo@google.comf420c422011-09-12 18:27:59 +0000543 TString preString;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700544 bool useEmulatedFunction = (visit == PreVisit && node->getUseEmulatedFunction());
zmo@google.com5601ea02011-06-10 18:23:25 +0000545 switch (node->getOp())
546 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700547 case EOpSequence:
548 // Scope the sequences except when at the global scope.
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700549 if (mDepth > 0)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700550 {
551 out << "{\n";
zmo@google.com5601ea02011-06-10 18:23:25 +0000552 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000553
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700554 incrementDepth(node);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700555 for (TIntermSequence::const_iterator iter = node->getSequence()->begin();
556 iter != node->getSequence()->end(); ++iter)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700557 {
558 TIntermNode *node = *iter;
559 ASSERT(node != NULL);
560 node->traverse(this);
561
562 if (isSingleStatement(node))
563 out << ";\n";
564 }
565 decrementDepth();
566
567 // Scope the sequences except when at the global scope.
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700568 if (mDepth > 0)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700569 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700570 out << "}\n";
571 }
572 visitChildren = false;
573 break;
574 case EOpPrototype:
575 // Function declaration.
576 ASSERT(visit == PreVisit);
577 writeVariableType(node->getType());
578 out << " " << hashName(node->getName());
579
580 out << "(";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700581 writeFunctionParameters(*(node->getSequence()));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700582 out << ")";
583
584 visitChildren = false;
585 break;
586 case EOpFunction: {
587 // Function definition.
588 ASSERT(visit == PreVisit);
589 writeVariableType(node->getType());
590 out << " " << hashFunctionName(node->getName());
591
592 incrementDepth(node);
593 // Function definition node contains one or two children nodes
594 // representing function parameters and function body. The latter
595 // is not present in case of empty function bodies.
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700596 const TIntermSequence &sequence = *(node->getSequence());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700597 ASSERT((sequence.size() == 1) || (sequence.size() == 2));
598 TIntermSequence::const_iterator seqIter = sequence.begin();
599
600 // Traverse function parameters.
601 TIntermAggregate *params = (*seqIter)->getAsAggregate();
602 ASSERT(params != NULL);
603 ASSERT(params->getOp() == EOpParameters);
604 params->traverse(this);
605
606 // Traverse function body.
607 TIntermAggregate *body = ++seqIter != sequence.end() ?
608 (*seqIter)->getAsAggregate() : NULL;
609 visitCodeBlock(body);
610 decrementDepth();
611
612 // Fully processed; no need to visit children.
613 visitChildren = false;
614 break;
615 }
616 case EOpFunctionCall:
617 // Function call.
618 if (visit == PreVisit)
619 out << hashFunctionName(node->getName()) << "(";
620 else if (visit == InVisit)
621 out << ", ";
622 else
zmo@google.com5601ea02011-06-10 18:23:25 +0000623 out << ")";
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700624 break;
625 case EOpParameters:
626 // Function parameters.
627 ASSERT(visit == PreVisit);
628 out << "(";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700629 writeFunctionParameters(*(node->getSequence()));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700630 out << ")";
631 visitChildren = false;
632 break;
633 case EOpDeclaration:
634 // Variable declaration.
635 if (visit == PreVisit)
636 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700637 const TIntermSequence &sequence = *(node->getSequence());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700638 const TIntermTyped *variable = sequence.front()->getAsTyped();
639 writeVariableType(variable->getType());
640 out << " ";
641 mDeclaringVariables = true;
zmo@google.com5601ea02011-06-10 18:23:25 +0000642 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700643 else if (visit == InVisit)
644 {
645 out << ", ";
646 mDeclaringVariables = true;
zmo@google.com5601ea02011-06-10 18:23:25 +0000647 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700648 else
649 {
650 mDeclaringVariables = false;
651 }
652 break;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700653 case EOpInvariantDeclaration:
654 // Invariant declaration.
655 ASSERT(visit == PreVisit);
656 {
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400657 const TIntermSequence *sequence = node->getSequence();
658 ASSERT(sequence && sequence->size() == 1);
659 const TIntermSymbol *symbol = sequence->front()->getAsSymbolNode();
660 ASSERT(symbol);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700661 out << "invariant " << hashVariableName(symbol->getSymbol()) << ";";
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400662 }
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700663 visitChildren = false;
664 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700665 case EOpConstructFloat:
666 writeTriplet(visit, "float(", NULL, ")");
667 break;
668 case EOpConstructVec2:
669 writeBuiltInFunctionTriplet(visit, "vec2(", false);
670 break;
671 case EOpConstructVec3:
672 writeBuiltInFunctionTriplet(visit, "vec3(", false);
673 break;
674 case EOpConstructVec4:
675 writeBuiltInFunctionTriplet(visit, "vec4(", false);
676 break;
677 case EOpConstructBool:
678 writeTriplet(visit, "bool(", NULL, ")");
679 break;
680 case EOpConstructBVec2:
681 writeBuiltInFunctionTriplet(visit, "bvec2(", false);
682 break;
683 case EOpConstructBVec3:
684 writeBuiltInFunctionTriplet(visit, "bvec3(", false);
685 break;
686 case EOpConstructBVec4:
687 writeBuiltInFunctionTriplet(visit, "bvec4(", false);
688 break;
689 case EOpConstructInt:
690 writeTriplet(visit, "int(", NULL, ")");
691 break;
692 case EOpConstructIVec2:
693 writeBuiltInFunctionTriplet(visit, "ivec2(", false);
694 break;
695 case EOpConstructIVec3:
696 writeBuiltInFunctionTriplet(visit, "ivec3(", false);
697 break;
698 case EOpConstructIVec4:
699 writeBuiltInFunctionTriplet(visit, "ivec4(", false);
700 break;
701 case EOpConstructMat2:
702 writeBuiltInFunctionTriplet(visit, "mat2(", false);
703 break;
704 case EOpConstructMat3:
705 writeBuiltInFunctionTriplet(visit, "mat3(", false);
706 break;
707 case EOpConstructMat4:
708 writeBuiltInFunctionTriplet(visit, "mat4(", false);
709 break;
710 case EOpConstructStruct:
711 if (visit == PreVisit)
712 {
713 const TType &type = node->getType();
714 ASSERT(type.getBasicType() == EbtStruct);
715 out << hashName(type.getStruct()->name()) << "(";
716 }
717 else if (visit == InVisit)
718 {
719 out << ", ";
720 }
721 else
722 {
zmo@google.com5601ea02011-06-10 18:23:25 +0000723 out << ")";
zmo@google.com5601ea02011-06-10 18:23:25 +0000724 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700725 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000726
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700727 case EOpLessThan:
728 writeBuiltInFunctionTriplet(visit, "lessThan(", useEmulatedFunction);
729 break;
730 case EOpGreaterThan:
731 writeBuiltInFunctionTriplet(visit, "greaterThan(", useEmulatedFunction);
732 break;
733 case EOpLessThanEqual:
734 writeBuiltInFunctionTriplet(visit, "lessThanEqual(", useEmulatedFunction);
735 break;
736 case EOpGreaterThanEqual:
737 writeBuiltInFunctionTriplet(visit, "greaterThanEqual(", useEmulatedFunction);
738 break;
739 case EOpVectorEqual:
740 writeBuiltInFunctionTriplet(visit, "equal(", useEmulatedFunction);
741 break;
742 case EOpVectorNotEqual:
743 writeBuiltInFunctionTriplet(visit, "notEqual(", useEmulatedFunction);
744 break;
745 case EOpComma:
746 writeTriplet(visit, NULL, ", ", NULL);
747 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000748
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700749 case EOpMod:
750 writeBuiltInFunctionTriplet(visit, "mod(", useEmulatedFunction);
751 break;
752 case EOpPow:
753 writeBuiltInFunctionTriplet(visit, "pow(", useEmulatedFunction);
754 break;
755 case EOpAtan:
756 writeBuiltInFunctionTriplet(visit, "atan(", useEmulatedFunction);
757 break;
758 case EOpMin:
759 writeBuiltInFunctionTriplet(visit, "min(", useEmulatedFunction);
760 break;
761 case EOpMax:
762 writeBuiltInFunctionTriplet(visit, "max(", useEmulatedFunction);
763 break;
764 case EOpClamp:
765 writeBuiltInFunctionTriplet(visit, "clamp(", useEmulatedFunction);
766 break;
767 case EOpMix:
768 writeBuiltInFunctionTriplet(visit, "mix(", useEmulatedFunction);
769 break;
770 case EOpStep:
771 writeBuiltInFunctionTriplet(visit, "step(", useEmulatedFunction);
772 break;
773 case EOpSmoothStep:
774 writeBuiltInFunctionTriplet(visit, "smoothstep(", useEmulatedFunction);
775 break;
776 case EOpDistance:
777 writeBuiltInFunctionTriplet(visit, "distance(", useEmulatedFunction);
778 break;
779 case EOpDot:
780 writeBuiltInFunctionTriplet(visit, "dot(", useEmulatedFunction);
781 break;
782 case EOpCross:
783 writeBuiltInFunctionTriplet(visit, "cross(", useEmulatedFunction);
784 break;
785 case EOpFaceForward:
786 writeBuiltInFunctionTriplet(visit, "faceforward(", useEmulatedFunction);
787 break;
788 case EOpReflect:
789 writeBuiltInFunctionTriplet(visit, "reflect(", useEmulatedFunction);
790 break;
791 case EOpRefract:
792 writeBuiltInFunctionTriplet(visit, "refract(", useEmulatedFunction);
793 break;
794 case EOpMul:
795 writeBuiltInFunctionTriplet(visit, "matrixCompMult(", useEmulatedFunction);
796 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000797
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700798 default:
799 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000800 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000801 return visitChildren;
802}
803
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700804bool TOutputGLSLBase::visitLoop(Visit visit, TIntermLoop *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000805{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700806 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000807
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700808 incrementDepth(node);
zmo@google.com5601ea02011-06-10 18:23:25 +0000809 // Loop header.
810 TLoopType loopType = node->getType();
811 if (loopType == ELoopFor) // for loop
812 {
Zhenyao Mo550c6002014-02-26 15:40:48 -0800813 if (!node->getUnrollFlag())
814 {
zmo@google.com5601ea02011-06-10 18:23:25 +0000815 out << "for (";
816 if (node->getInit())
817 node->getInit()->traverse(this);
818 out << "; ";
819
820 if (node->getCondition())
821 node->getCondition()->traverse(this);
822 out << "; ";
823
824 if (node->getExpression())
825 node->getExpression()->traverse(this);
826 out << ")\n";
827 }
Zhenyao Mo550c6002014-02-26 15:40:48 -0800828 else
829 {
830 // Need to put a one-iteration loop here to handle break.
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700831 TIntermSequence *declSeq =
Zhenyao Mo550c6002014-02-26 15:40:48 -0800832 node->getInit()->getAsAggregate()->getSequence();
833 TIntermSymbol *indexSymbol =
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700834 (*declSeq)[0]->getAsBinaryNode()->getLeft()->getAsSymbolNode();
Zhenyao Mo550c6002014-02-26 15:40:48 -0800835 TString name = hashVariableName(indexSymbol->getSymbol());
836 out << "for (int " << name << " = 0; "
837 << name << " < 1; "
838 << "++" << name << ")\n";
839 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000840 }
841 else if (loopType == ELoopWhile) // while loop
842 {
843 out << "while (";
844 ASSERT(node->getCondition() != NULL);
845 node->getCondition()->traverse(this);
846 out << ")\n";
847 }
848 else // do-while loop
849 {
850 ASSERT(loopType == ELoopDoWhile);
851 out << "do\n";
852 }
853
854 // Loop body.
855 if (node->getUnrollFlag())
856 {
Zhenyao Mo550c6002014-02-26 15:40:48 -0800857 out << "{\n";
858 mLoopUnrollStack.push(node);
859 while (mLoopUnrollStack.satisfiesLoopCondition())
zmo@google.com5601ea02011-06-10 18:23:25 +0000860 {
861 visitCodeBlock(node->getBody());
Zhenyao Mo550c6002014-02-26 15:40:48 -0800862 mLoopUnrollStack.step();
zmo@google.com5601ea02011-06-10 18:23:25 +0000863 }
Zhenyao Mo550c6002014-02-26 15:40:48 -0800864 mLoopUnrollStack.pop();
865 out << "}\n";
zmo@google.com5601ea02011-06-10 18:23:25 +0000866 }
867 else
868 {
869 visitCodeBlock(node->getBody());
870 }
871
872 // Loop footer.
873 if (loopType == ELoopDoWhile) // do-while loop
874 {
875 out << "while (";
876 ASSERT(node->getCondition() != NULL);
877 node->getCondition()->traverse(this);
878 out << ");\n";
879 }
880 decrementDepth();
881
882 // No need to visit children. They have been already processed in
883 // this function.
884 return false;
885}
886
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700887bool TOutputGLSLBase::visitBranch(Visit visit, TIntermBranch *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000888{
889 switch (node->getFlowOp())
890 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700891 case EOpKill:
892 writeTriplet(visit, "discard", NULL, NULL);
893 break;
894 case EOpBreak:
895 writeTriplet(visit, "break", NULL, NULL);
896 break;
897 case EOpContinue:
898 writeTriplet(visit, "continue", NULL, NULL);
899 break;
900 case EOpReturn:
901 writeTriplet(visit, "return ", NULL, NULL);
902 break;
903 default:
904 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000905 }
906
907 return true;
908}
909
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700910void TOutputGLSLBase::visitCodeBlock(TIntermNode *node)
911{
zmo@google.com5601ea02011-06-10 18:23:25 +0000912 TInfoSinkBase &out = objSink();
913 if (node != NULL)
914 {
915 node->traverse(this);
916 // Single statements not part of a sequence need to be terminated
917 // with semi-colon.
918 if (isSingleStatement(node))
919 out << ";\n";
920 }
921 else
922 {
923 out << "{\n}\n"; // Empty code block.
924 }
925}
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000926
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700927TString TOutputGLSLBase::getTypeName(const TType &type)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000928{
929 TInfoSinkBase out;
930 if (type.isMatrix())
931 {
932 out << "mat";
933 out << type.getNominalSize();
934 }
935 else if (type.isVector())
936 {
937 switch (type.getBasicType())
938 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700939 case EbtFloat:
940 out << "vec";
941 break;
942 case EbtInt:
943 out << "ivec";
944 break;
945 case EbtBool:
946 out << "bvec";
947 break;
948 default:
949 UNREACHABLE();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000950 }
951 out << type.getNominalSize();
952 }
953 else
954 {
955 if (type.getBasicType() == EbtStruct)
Jamie Madill98493dd2013-07-08 14:39:03 -0400956 out << hashName(type.getStruct()->name());
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000957 else
958 out << type.getBasicString();
959 }
960 return TString(out.c_str());
961}
962
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700963TString TOutputGLSLBase::hashName(const TString &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000964{
965 if (mHashFunction == NULL || name.empty())
966 return name;
967 NameMap::const_iterator it = mNameMap.find(name.c_str());
968 if (it != mNameMap.end())
969 return it->second.c_str();
970 TString hashedName = TIntermTraverser::hash(name, mHashFunction);
971 mNameMap[name.c_str()] = hashedName.c_str();
972 return hashedName;
973}
974
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700975TString TOutputGLSLBase::hashVariableName(const TString &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000976{
Jamie Madill02f20dd2013-09-12 12:07:42 -0400977 if (mSymbolTable.findBuiltIn(name, mShaderVersion) != NULL)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000978 return name;
979 return hashName(name);
980}
981
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700982TString TOutputGLSLBase::hashFunctionName(const TString &mangled_name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000983{
984 TString name = TFunction::unmangleName(mangled_name);
Jamie Madill02f20dd2013-09-12 12:07:42 -0400985 if (mSymbolTable.findBuiltIn(mangled_name, mShaderVersion) != NULL || name == "main")
Nicolas Capens46485082014-04-15 13:12:50 -0400986 return translateTextureFunction(name);
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000987 return hashName(name);
988}
Jamie Madill98493dd2013-07-08 14:39:03 -0400989
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700990bool TOutputGLSLBase::structDeclared(const TStructure *structure) const
Jamie Madill98493dd2013-07-08 14:39:03 -0400991{
Zhenyao Mo904a9162014-05-09 14:07:45 -0700992 ASSERT(structure);
Jamie Madill01f85ac2014-06-06 11:55:04 -0400993 if (structure->name().empty())
Zhenyao Mo904a9162014-05-09 14:07:45 -0700994 {
Jamie Madill01f85ac2014-06-06 11:55:04 -0400995 return false;
Zhenyao Mo904a9162014-05-09 14:07:45 -0700996 }
Jamie Madill01f85ac2014-06-06 11:55:04 -0400997
998 return (mDeclaredStructs.count(structure->uniqueId()) > 0);
Jamie Madill98493dd2013-07-08 14:39:03 -0400999}
1000
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001001void TOutputGLSLBase::declareStruct(const TStructure *structure)
Jamie Madill98493dd2013-07-08 14:39:03 -04001002{
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001003 TInfoSinkBase &out = objSink();
Jamie Madill98493dd2013-07-08 14:39:03 -04001004
1005 out << "struct " << hashName(structure->name()) << "{\n";
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001006 const TFieldList &fields = structure->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04001007 for (size_t i = 0; i < fields.size(); ++i)
1008 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001009 const TField *field = fields[i];
Jamie Madill98493dd2013-07-08 14:39:03 -04001010 if (writeVariablePrecision(field->type()->getPrecision()))
1011 out << " ";
1012 out << getTypeName(*field->type()) << " " << hashName(field->name());
1013 if (field->type()->isArray())
1014 out << arrayBrackets(*field->type());
1015 out << ";\n";
1016 }
1017 out << "}";
Zhenyao Mo904a9162014-05-09 14:07:45 -07001018}
Jamie Madill98493dd2013-07-08 14:39:03 -04001019