blob: d03abb86c957de2d7cb52e38932efbd49ba1d1a2 [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;
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400653 case EOpInvariantDeclaration: {
654 // Invariant declaration.
655 ASSERT(visit == PreVisit);
656 const TIntermSequence *sequence = node->getSequence();
657 ASSERT(sequence && sequence->size() == 1);
658 const TIntermSymbol *symbol = sequence->front()->getAsSymbolNode();
659 ASSERT(symbol);
660 out << "invariant " << symbol->getSymbol() << ";";
661 visitChildren = false;
662 break;
663 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700664 case EOpConstructFloat:
665 writeTriplet(visit, "float(", NULL, ")");
666 break;
667 case EOpConstructVec2:
668 writeBuiltInFunctionTriplet(visit, "vec2(", false);
669 break;
670 case EOpConstructVec3:
671 writeBuiltInFunctionTriplet(visit, "vec3(", false);
672 break;
673 case EOpConstructVec4:
674 writeBuiltInFunctionTriplet(visit, "vec4(", false);
675 break;
676 case EOpConstructBool:
677 writeTriplet(visit, "bool(", NULL, ")");
678 break;
679 case EOpConstructBVec2:
680 writeBuiltInFunctionTriplet(visit, "bvec2(", false);
681 break;
682 case EOpConstructBVec3:
683 writeBuiltInFunctionTriplet(visit, "bvec3(", false);
684 break;
685 case EOpConstructBVec4:
686 writeBuiltInFunctionTriplet(visit, "bvec4(", false);
687 break;
688 case EOpConstructInt:
689 writeTriplet(visit, "int(", NULL, ")");
690 break;
691 case EOpConstructIVec2:
692 writeBuiltInFunctionTriplet(visit, "ivec2(", false);
693 break;
694 case EOpConstructIVec3:
695 writeBuiltInFunctionTriplet(visit, "ivec3(", false);
696 break;
697 case EOpConstructIVec4:
698 writeBuiltInFunctionTriplet(visit, "ivec4(", false);
699 break;
700 case EOpConstructMat2:
701 writeBuiltInFunctionTriplet(visit, "mat2(", false);
702 break;
703 case EOpConstructMat3:
704 writeBuiltInFunctionTriplet(visit, "mat3(", false);
705 break;
706 case EOpConstructMat4:
707 writeBuiltInFunctionTriplet(visit, "mat4(", false);
708 break;
709 case EOpConstructStruct:
710 if (visit == PreVisit)
711 {
712 const TType &type = node->getType();
713 ASSERT(type.getBasicType() == EbtStruct);
714 out << hashName(type.getStruct()->name()) << "(";
715 }
716 else if (visit == InVisit)
717 {
718 out << ", ";
719 }
720 else
721 {
zmo@google.com5601ea02011-06-10 18:23:25 +0000722 out << ")";
zmo@google.com5601ea02011-06-10 18:23:25 +0000723 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700724 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000725
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700726 case EOpLessThan:
727 writeBuiltInFunctionTriplet(visit, "lessThan(", useEmulatedFunction);
728 break;
729 case EOpGreaterThan:
730 writeBuiltInFunctionTriplet(visit, "greaterThan(", useEmulatedFunction);
731 break;
732 case EOpLessThanEqual:
733 writeBuiltInFunctionTriplet(visit, "lessThanEqual(", useEmulatedFunction);
734 break;
735 case EOpGreaterThanEqual:
736 writeBuiltInFunctionTriplet(visit, "greaterThanEqual(", useEmulatedFunction);
737 break;
738 case EOpVectorEqual:
739 writeBuiltInFunctionTriplet(visit, "equal(", useEmulatedFunction);
740 break;
741 case EOpVectorNotEqual:
742 writeBuiltInFunctionTriplet(visit, "notEqual(", useEmulatedFunction);
743 break;
744 case EOpComma:
745 writeTriplet(visit, NULL, ", ", NULL);
746 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000747
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700748 case EOpMod:
749 writeBuiltInFunctionTriplet(visit, "mod(", useEmulatedFunction);
750 break;
751 case EOpPow:
752 writeBuiltInFunctionTriplet(visit, "pow(", useEmulatedFunction);
753 break;
754 case EOpAtan:
755 writeBuiltInFunctionTriplet(visit, "atan(", useEmulatedFunction);
756 break;
757 case EOpMin:
758 writeBuiltInFunctionTriplet(visit, "min(", useEmulatedFunction);
759 break;
760 case EOpMax:
761 writeBuiltInFunctionTriplet(visit, "max(", useEmulatedFunction);
762 break;
763 case EOpClamp:
764 writeBuiltInFunctionTriplet(visit, "clamp(", useEmulatedFunction);
765 break;
766 case EOpMix:
767 writeBuiltInFunctionTriplet(visit, "mix(", useEmulatedFunction);
768 break;
769 case EOpStep:
770 writeBuiltInFunctionTriplet(visit, "step(", useEmulatedFunction);
771 break;
772 case EOpSmoothStep:
773 writeBuiltInFunctionTriplet(visit, "smoothstep(", useEmulatedFunction);
774 break;
775 case EOpDistance:
776 writeBuiltInFunctionTriplet(visit, "distance(", useEmulatedFunction);
777 break;
778 case EOpDot:
779 writeBuiltInFunctionTriplet(visit, "dot(", useEmulatedFunction);
780 break;
781 case EOpCross:
782 writeBuiltInFunctionTriplet(visit, "cross(", useEmulatedFunction);
783 break;
784 case EOpFaceForward:
785 writeBuiltInFunctionTriplet(visit, "faceforward(", useEmulatedFunction);
786 break;
787 case EOpReflect:
788 writeBuiltInFunctionTriplet(visit, "reflect(", useEmulatedFunction);
789 break;
790 case EOpRefract:
791 writeBuiltInFunctionTriplet(visit, "refract(", useEmulatedFunction);
792 break;
793 case EOpMul:
794 writeBuiltInFunctionTriplet(visit, "matrixCompMult(", useEmulatedFunction);
795 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000796
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700797 default:
798 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000799 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000800 return visitChildren;
801}
802
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700803bool TOutputGLSLBase::visitLoop(Visit visit, TIntermLoop *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000804{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700805 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000806
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700807 incrementDepth(node);
zmo@google.com5601ea02011-06-10 18:23:25 +0000808 // Loop header.
809 TLoopType loopType = node->getType();
810 if (loopType == ELoopFor) // for loop
811 {
Zhenyao Mo550c6002014-02-26 15:40:48 -0800812 if (!node->getUnrollFlag())
813 {
zmo@google.com5601ea02011-06-10 18:23:25 +0000814 out << "for (";
815 if (node->getInit())
816 node->getInit()->traverse(this);
817 out << "; ";
818
819 if (node->getCondition())
820 node->getCondition()->traverse(this);
821 out << "; ";
822
823 if (node->getExpression())
824 node->getExpression()->traverse(this);
825 out << ")\n";
826 }
Zhenyao Mo550c6002014-02-26 15:40:48 -0800827 else
828 {
829 // Need to put a one-iteration loop here to handle break.
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700830 TIntermSequence *declSeq =
Zhenyao Mo550c6002014-02-26 15:40:48 -0800831 node->getInit()->getAsAggregate()->getSequence();
832 TIntermSymbol *indexSymbol =
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700833 (*declSeq)[0]->getAsBinaryNode()->getLeft()->getAsSymbolNode();
Zhenyao Mo550c6002014-02-26 15:40:48 -0800834 TString name = hashVariableName(indexSymbol->getSymbol());
835 out << "for (int " << name << " = 0; "
836 << name << " < 1; "
837 << "++" << name << ")\n";
838 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000839 }
840 else if (loopType == ELoopWhile) // while loop
841 {
842 out << "while (";
843 ASSERT(node->getCondition() != NULL);
844 node->getCondition()->traverse(this);
845 out << ")\n";
846 }
847 else // do-while loop
848 {
849 ASSERT(loopType == ELoopDoWhile);
850 out << "do\n";
851 }
852
853 // Loop body.
854 if (node->getUnrollFlag())
855 {
Zhenyao Mo550c6002014-02-26 15:40:48 -0800856 out << "{\n";
857 mLoopUnrollStack.push(node);
858 while (mLoopUnrollStack.satisfiesLoopCondition())
zmo@google.com5601ea02011-06-10 18:23:25 +0000859 {
860 visitCodeBlock(node->getBody());
Zhenyao Mo550c6002014-02-26 15:40:48 -0800861 mLoopUnrollStack.step();
zmo@google.com5601ea02011-06-10 18:23:25 +0000862 }
Zhenyao Mo550c6002014-02-26 15:40:48 -0800863 mLoopUnrollStack.pop();
864 out << "}\n";
zmo@google.com5601ea02011-06-10 18:23:25 +0000865 }
866 else
867 {
868 visitCodeBlock(node->getBody());
869 }
870
871 // Loop footer.
872 if (loopType == ELoopDoWhile) // do-while loop
873 {
874 out << "while (";
875 ASSERT(node->getCondition() != NULL);
876 node->getCondition()->traverse(this);
877 out << ");\n";
878 }
879 decrementDepth();
880
881 // No need to visit children. They have been already processed in
882 // this function.
883 return false;
884}
885
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700886bool TOutputGLSLBase::visitBranch(Visit visit, TIntermBranch *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000887{
888 switch (node->getFlowOp())
889 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700890 case EOpKill:
891 writeTriplet(visit, "discard", NULL, NULL);
892 break;
893 case EOpBreak:
894 writeTriplet(visit, "break", NULL, NULL);
895 break;
896 case EOpContinue:
897 writeTriplet(visit, "continue", NULL, NULL);
898 break;
899 case EOpReturn:
900 writeTriplet(visit, "return ", NULL, NULL);
901 break;
902 default:
903 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000904 }
905
906 return true;
907}
908
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700909void TOutputGLSLBase::visitCodeBlock(TIntermNode *node)
910{
zmo@google.com5601ea02011-06-10 18:23:25 +0000911 TInfoSinkBase &out = objSink();
912 if (node != NULL)
913 {
914 node->traverse(this);
915 // Single statements not part of a sequence need to be terminated
916 // with semi-colon.
917 if (isSingleStatement(node))
918 out << ";\n";
919 }
920 else
921 {
922 out << "{\n}\n"; // Empty code block.
923 }
924}
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000925
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700926TString TOutputGLSLBase::getTypeName(const TType &type)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000927{
928 TInfoSinkBase out;
929 if (type.isMatrix())
930 {
931 out << "mat";
932 out << type.getNominalSize();
933 }
934 else if (type.isVector())
935 {
936 switch (type.getBasicType())
937 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700938 case EbtFloat:
939 out << "vec";
940 break;
941 case EbtInt:
942 out << "ivec";
943 break;
944 case EbtBool:
945 out << "bvec";
946 break;
947 default:
948 UNREACHABLE();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000949 }
950 out << type.getNominalSize();
951 }
952 else
953 {
954 if (type.getBasicType() == EbtStruct)
Jamie Madill98493dd2013-07-08 14:39:03 -0400955 out << hashName(type.getStruct()->name());
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000956 else
957 out << type.getBasicString();
958 }
959 return TString(out.c_str());
960}
961
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700962TString TOutputGLSLBase::hashName(const TString &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000963{
964 if (mHashFunction == NULL || name.empty())
965 return name;
966 NameMap::const_iterator it = mNameMap.find(name.c_str());
967 if (it != mNameMap.end())
968 return it->second.c_str();
969 TString hashedName = TIntermTraverser::hash(name, mHashFunction);
970 mNameMap[name.c_str()] = hashedName.c_str();
971 return hashedName;
972}
973
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700974TString TOutputGLSLBase::hashVariableName(const TString &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000975{
Jamie Madill02f20dd2013-09-12 12:07:42 -0400976 if (mSymbolTable.findBuiltIn(name, mShaderVersion) != NULL)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000977 return name;
978 return hashName(name);
979}
980
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700981TString TOutputGLSLBase::hashFunctionName(const TString &mangled_name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000982{
983 TString name = TFunction::unmangleName(mangled_name);
Jamie Madill02f20dd2013-09-12 12:07:42 -0400984 if (mSymbolTable.findBuiltIn(mangled_name, mShaderVersion) != NULL || name == "main")
Nicolas Capens46485082014-04-15 13:12:50 -0400985 return translateTextureFunction(name);
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000986 return hashName(name);
987}
Jamie Madill98493dd2013-07-08 14:39:03 -0400988
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700989bool TOutputGLSLBase::structDeclared(const TStructure *structure) const
Jamie Madill98493dd2013-07-08 14:39:03 -0400990{
Zhenyao Mo904a9162014-05-09 14:07:45 -0700991 ASSERT(structure);
Jamie Madill01f85ac2014-06-06 11:55:04 -0400992 if (structure->name().empty())
Zhenyao Mo904a9162014-05-09 14:07:45 -0700993 {
Jamie Madill01f85ac2014-06-06 11:55:04 -0400994 return false;
Zhenyao Mo904a9162014-05-09 14:07:45 -0700995 }
Jamie Madill01f85ac2014-06-06 11:55:04 -0400996
997 return (mDeclaredStructs.count(structure->uniqueId()) > 0);
Jamie Madill98493dd2013-07-08 14:39:03 -0400998}
999
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001000void TOutputGLSLBase::declareStruct(const TStructure *structure)
Jamie Madill98493dd2013-07-08 14:39:03 -04001001{
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001002 TInfoSinkBase &out = objSink();
Jamie Madill98493dd2013-07-08 14:39:03 -04001003
1004 out << "struct " << hashName(structure->name()) << "{\n";
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001005 const TFieldList &fields = structure->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04001006 for (size_t i = 0; i < fields.size(); ++i)
1007 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001008 const TField *field = fields[i];
Jamie Madill98493dd2013-07-08 14:39:03 -04001009 if (writeVariablePrecision(field->type()->getPrecision()))
1010 out << " ";
1011 out << getTypeName(*field->type()) << " " << hashName(field->name());
1012 if (field->type()->isArray())
1013 out << arrayBrackets(*field->type());
1014 out << ";\n";
1015 }
1016 out << "}";
Zhenyao Mo904a9162014-05-09 14:07:45 -07001017}
Jamie Madill98493dd2013-07-08 14:39:03 -04001018