blob: 8d8830c36d50cef03f141b39dbdec23a6c491e55 [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;
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +0000226 case EOpModAssign:
227 writeTriplet(visit, "(", " %= ", ")");
228 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700229 // Notice the fall-through.
230 case EOpMulAssign:
231 case EOpVectorTimesMatrixAssign:
232 case EOpVectorTimesScalarAssign:
233 case EOpMatrixTimesScalarAssign:
234 case EOpMatrixTimesMatrixAssign:
235 writeTriplet(visit, "(", " *= ", ")");
236 break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200237 case EOpBitShiftLeftAssign:
238 writeTriplet(visit, "(", " <<= ", ")");
239 break;
240 case EOpBitShiftRightAssign:
241 writeTriplet(visit, "(", " >>= ", ")");
242 break;
243 case EOpBitwiseAndAssign:
244 writeTriplet(visit, "(", " &= ", ")");
245 break;
246 case EOpBitwiseXorAssign:
247 writeTriplet(visit, "(", " ^= ", ")");
248 break;
249 case EOpBitwiseOrAssign:
250 writeTriplet(visit, "(", " |= ", ")");
251 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700252
253 case EOpIndexDirect:
254 writeTriplet(visit, NULL, "[", "]");
255 break;
256 case EOpIndexIndirect:
257 if (node->getAddIndexClamp())
258 {
zmo@google.com5601ea02011-06-10 18:23:25 +0000259 if (visit == InVisit)
260 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700261 if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC)
262 out << "[int(clamp(float(";
263 else
264 out << "[webgl_int_clamp(";
zmo@google.com5601ea02011-06-10 18:23:25 +0000265 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700266 else if (visit == PostVisit)
267 {
268 int maxSize;
269 TIntermTyped *left = node->getLeft();
270 TType leftType = left->getType();
zmo@google.com5601ea02011-06-10 18:23:25 +0000271
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700272 if (left->isArray())
273 {
274 // The shader will fail validation if the array length is not > 0.
275 maxSize = leftType.getArraySize() - 1;
276 }
277 else
278 {
279 maxSize = leftType.getNominalSize() - 1;
280 }
281
282 if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC)
283 out << "), 0.0, float(" << maxSize << ")))]";
284 else
285 out << ", 0, " << maxSize << ")]";
286 }
287 }
288 else
289 {
zmo@google.com5601ea02011-06-10 18:23:25 +0000290 writeTriplet(visit, NULL, "[", "]");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700291 }
292 break;
293 case EOpIndexDirectStruct:
294 if (visit == InVisit)
295 {
296 // Here we are writing out "foo.bar", where "foo" is struct
297 // and "bar" is field. In AST, it is represented as a binary
298 // node, where left child represents "foo" and right child "bar".
299 // The node itself represents ".". The struct field "bar" is
300 // actually stored as an index into TStructure::fields.
301 out << ".";
302 const TStructure *structure = node->getLeft()->getType().getStruct();
303 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
304 const TField *field = structure->fields()[index->getIConst(0)];
305
306 TString fieldName = field->name();
307 if (!mSymbolTable.findBuiltIn(structure->name(), mShaderVersion))
308 fieldName = hashName(fieldName);
309
310 out << fieldName;
311 visitChildren = false;
312 }
313 break;
314 case EOpVectorSwizzle:
315 if (visit == InVisit)
316 {
317 out << ".";
318 TIntermAggregate *rightChild = node->getRight()->getAsAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700319 TIntermSequence *sequence = rightChild->getSequence();
320 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); ++sit)
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000321 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700322 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
323 ASSERT(element->getBasicType() == EbtInt);
324 ASSERT(element->getNominalSize() == 1);
325 const ConstantUnion& data = element->getUnionArrayPointer()[0];
326 ASSERT(data.getType() == EbtInt);
327 switch (data.getIConst())
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000328 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700329 case 0:
330 out << "x";
331 break;
332 case 1:
333 out << "y";
334 break;
335 case 2:
336 out << "z";
337 break;
338 case 3:
339 out << "w";
340 break;
341 default:
342 UNREACHABLE();
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000343 }
344 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700345 visitChildren = false;
346 }
347 break;
daniel@transgaming.com97b16d12013-02-01 03:20:42 +0000348
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700349 case EOpAdd:
350 writeTriplet(visit, "(", " + ", ")");
351 break;
352 case EOpSub:
353 writeTriplet(visit, "(", " - ", ")");
354 break;
355 case EOpMul:
356 writeTriplet(visit, "(", " * ", ")");
357 break;
358 case EOpDiv:
359 writeTriplet(visit, "(", " / ", ")");
360 break;
361 case EOpMod:
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +0000362 writeTriplet(visit, "(", " % ", ")");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700363 break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200364 case EOpBitShiftLeft:
365 writeTriplet(visit, "(", " << ", ")");
366 break;
367 case EOpBitShiftRight:
368 writeTriplet(visit, "(", " >> ", ")");
369 break;
370 case EOpBitwiseAnd:
371 writeTriplet(visit, "(", " & ", ")");
372 break;
373 case EOpBitwiseXor:
374 writeTriplet(visit, "(", " ^ ", ")");
375 break;
376 case EOpBitwiseOr:
377 writeTriplet(visit, "(", " | ", ")");
378 break;
379
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700380 case EOpEqual:
381 writeTriplet(visit, "(", " == ", ")");
382 break;
383 case EOpNotEqual:
384 writeTriplet(visit, "(", " != ", ")");
385 break;
386 case EOpLessThan:
387 writeTriplet(visit, "(", " < ", ")");
388 break;
389 case EOpGreaterThan:
390 writeTriplet(visit, "(", " > ", ")");
391 break;
392 case EOpLessThanEqual:
393 writeTriplet(visit, "(", " <= ", ")");
394 break;
395 case EOpGreaterThanEqual:
396 writeTriplet(visit, "(", " >= ", ")");
397 break;
daniel@transgaming.com97b16d12013-02-01 03:20:42 +0000398
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700399 // Notice the fall-through.
400 case EOpVectorTimesScalar:
401 case EOpVectorTimesMatrix:
402 case EOpMatrixTimesVector:
403 case EOpMatrixTimesScalar:
404 case EOpMatrixTimesMatrix:
405 writeTriplet(visit, "(", " * ", ")");
406 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000407
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700408 case EOpLogicalOr:
409 writeTriplet(visit, "(", " || ", ")");
410 break;
411 case EOpLogicalXor:
412 writeTriplet(visit, "(", " ^^ ", ")");
413 break;
414 case EOpLogicalAnd:
415 writeTriplet(visit, "(", " && ", ")");
416 break;
417 default:
418 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000419 }
420
421 return visitChildren;
422}
423
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700424bool TOutputGLSLBase::visitUnary(Visit visit, TIntermUnary *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000425{
zmo@google.com32e97312011-08-24 01:03:11 +0000426 TString preString;
427 TString postString = ")";
428
zmo@google.com5601ea02011-06-10 18:23:25 +0000429 switch (node->getOp())
430 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700431 case EOpNegative: preString = "(-"; break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -0700432 case EOpPositive: preString = "(+"; break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700433 case EOpVectorLogicalNot: preString = "not("; break;
434 case EOpLogicalNot: preString = "(!"; break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200435 case EOpBitwiseNot: preString = "(~"; break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000436
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700437 case EOpPostIncrement: preString = "("; postString = "++)"; break;
438 case EOpPostDecrement: preString = "("; postString = "--)"; break;
439 case EOpPreIncrement: preString = "(++"; break;
440 case EOpPreDecrement: preString = "(--"; break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000441
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700442 case EOpRadians:
443 preString = "radians(";
444 break;
445 case EOpDegrees:
446 preString = "degrees(";
447 break;
448 case EOpSin:
449 preString = "sin(";
450 break;
451 case EOpCos:
452 preString = "cos(";
453 break;
454 case EOpTan:
455 preString = "tan(";
456 break;
457 case EOpAsin:
458 preString = "asin(";
459 break;
460 case EOpAcos:
461 preString = "acos(";
462 break;
463 case EOpAtan:
464 preString = "atan(";
465 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000466
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200467 case EOpSinh:
468 preString = "sinh(";
469 break;
470 case EOpCosh:
471 preString = "cosh(";
472 break;
473 case EOpTanh:
474 preString = "tanh(";
475 break;
476 case EOpAsinh:
477 preString = "asinh(";
478 break;
479 case EOpAcosh:
480 preString = "acosh(";
481 break;
482 case EOpAtanh:
483 preString = "atanh(";
484 break;
485
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700486 case EOpExp:
487 preString = "exp(";
488 break;
489 case EOpLog:
490 preString = "log(";
491 break;
492 case EOpExp2:
493 preString = "exp2(";
494 break;
495 case EOpLog2:
496 preString = "log2(";
497 break;
498 case EOpSqrt:
499 preString = "sqrt(";
500 break;
501 case EOpInverseSqrt:
502 preString = "inversesqrt(";
503 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000504
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700505 case EOpAbs:
506 preString = "abs(";
507 break;
508 case EOpSign:
509 preString = "sign(";
510 break;
511 case EOpFloor:
512 preString = "floor(";
513 break;
514 case EOpCeil:
515 preString = "ceil(";
516 break;
517 case EOpFract:
518 preString = "fract(";
519 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000520
Olli Etuahoe8d2c072015-01-08 16:33:54 +0200521 case EOpFloatBitsToInt:
522 preString = "floatBitsToInt(";
523 break;
524 case EOpFloatBitsToUint:
525 preString = "floatBitsToUint(";
526 break;
527 case EOpIntBitsToFloat:
528 preString = "intBitsToFloat(";
529 break;
530 case EOpUintBitsToFloat:
531 preString = "uintBitsToFloat(";
532 break;
533
Olli Etuaho7700ff62015-01-15 12:16:29 +0200534 case EOpPackSnorm2x16:
535 preString = "packSnorm2x16(";
536 break;
537 case EOpPackUnorm2x16:
538 preString = "packUnorm2x16(";
539 break;
540 case EOpPackHalf2x16:
541 preString = "packHalf2x16(";
542 break;
543 case EOpUnpackSnorm2x16:
544 preString = "unpackSnorm2x16(";
545 break;
546 case EOpUnpackUnorm2x16:
547 preString = "unpackUnorm2x16(";
548 break;
549 case EOpUnpackHalf2x16:
550 preString = "unpackHalf2x16(";
551 break;
552
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700553 case EOpLength:
554 preString = "length(";
555 break;
556 case EOpNormalize:
557 preString = "normalize(";
558 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000559
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700560 case EOpDFdx:
561 preString = "dFdx(";
562 break;
563 case EOpDFdy:
564 preString = "dFdy(";
565 break;
566 case EOpFwidth:
567 preString = "fwidth(";
568 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000569
Olli Etuahoe39706d2014-12-30 16:40:36 +0200570 case EOpTranspose:
571 preString = "transpose(";
572 break;
573 case EOpDeterminant:
574 preString = "determinant(";
575 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +0200576 case EOpInverse:
577 preString = "inverse(";
578 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +0200579
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700580 case EOpAny:
581 preString = "any(";
582 break;
583 case EOpAll:
584 preString = "all(";
585 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000586
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700587 default:
588 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000589 }
590
zmo@google.com32e97312011-08-24 01:03:11 +0000591 if (visit == PreVisit && node->getUseEmulatedFunction())
592 preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preString);
593 writeTriplet(visit, preString.c_str(), NULL, postString.c_str());
594
zmo@google.com5601ea02011-06-10 18:23:25 +0000595 return true;
596}
597
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700598bool TOutputGLSLBase::visitSelection(Visit visit, TIntermSelection *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000599{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700600 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000601
602 if (node->usesTernaryOperator())
603 {
604 // Notice two brackets at the beginning and end. The outer ones
605 // encapsulate the whole ternary expression. This preserves the
606 // order of precedence when ternary expressions are used in a
607 // compound expression, i.e., c = 2 * (a < b ? 1 : 2).
608 out << "((";
609 node->getCondition()->traverse(this);
610 out << ") ? (";
611 node->getTrueBlock()->traverse(this);
612 out << ") : (";
613 node->getFalseBlock()->traverse(this);
614 out << "))";
615 }
616 else
617 {
618 out << "if (";
619 node->getCondition()->traverse(this);
620 out << ")\n";
621
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700622 incrementDepth(node);
zmo@google.com5601ea02011-06-10 18:23:25 +0000623 visitCodeBlock(node->getTrueBlock());
624
625 if (node->getFalseBlock())
626 {
627 out << "else\n";
628 visitCodeBlock(node->getFalseBlock());
629 }
630 decrementDepth();
631 }
632 return false;
633}
634
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700635bool TOutputGLSLBase::visitAggregate(Visit visit, TIntermAggregate *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000636{
637 bool visitChildren = true;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700638 TInfoSinkBase &out = objSink();
zmo@google.comf420c422011-09-12 18:27:59 +0000639 TString preString;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700640 bool useEmulatedFunction = (visit == PreVisit && node->getUseEmulatedFunction());
zmo@google.com5601ea02011-06-10 18:23:25 +0000641 switch (node->getOp())
642 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700643 case EOpSequence:
644 // Scope the sequences except when at the global scope.
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700645 if (mDepth > 0)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700646 {
647 out << "{\n";
zmo@google.com5601ea02011-06-10 18:23:25 +0000648 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000649
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700650 incrementDepth(node);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700651 for (TIntermSequence::const_iterator iter = node->getSequence()->begin();
652 iter != node->getSequence()->end(); ++iter)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700653 {
Austin Kinross3ae64652015-01-26 15:51:39 -0800654 TIntermNode *curNode = *iter;
655 ASSERT(curNode != NULL);
656 curNode->traverse(this);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700657
Austin Kinross3ae64652015-01-26 15:51:39 -0800658 if (isSingleStatement(curNode))
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700659 out << ";\n";
660 }
661 decrementDepth();
662
663 // Scope the sequences except when at the global scope.
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700664 if (mDepth > 0)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700665 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700666 out << "}\n";
667 }
668 visitChildren = false;
669 break;
670 case EOpPrototype:
671 // Function declaration.
672 ASSERT(visit == PreVisit);
673 writeVariableType(node->getType());
Olli Etuaho76acee82014-11-04 13:44:03 +0200674 out << " " << hashFunctionName(node->getName());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700675
676 out << "(";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700677 writeFunctionParameters(*(node->getSequence()));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700678 out << ")";
679
680 visitChildren = false;
681 break;
682 case EOpFunction: {
683 // Function definition.
684 ASSERT(visit == PreVisit);
685 writeVariableType(node->getType());
686 out << " " << hashFunctionName(node->getName());
687
688 incrementDepth(node);
689 // Function definition node contains one or two children nodes
690 // representing function parameters and function body. The latter
691 // is not present in case of empty function bodies.
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700692 const TIntermSequence &sequence = *(node->getSequence());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700693 ASSERT((sequence.size() == 1) || (sequence.size() == 2));
694 TIntermSequence::const_iterator seqIter = sequence.begin();
695
696 // Traverse function parameters.
697 TIntermAggregate *params = (*seqIter)->getAsAggregate();
698 ASSERT(params != NULL);
699 ASSERT(params->getOp() == EOpParameters);
700 params->traverse(this);
701
702 // Traverse function body.
703 TIntermAggregate *body = ++seqIter != sequence.end() ?
704 (*seqIter)->getAsAggregate() : NULL;
705 visitCodeBlock(body);
706 decrementDepth();
707
708 // Fully processed; no need to visit children.
709 visitChildren = false;
710 break;
711 }
712 case EOpFunctionCall:
713 // Function call.
714 if (visit == PreVisit)
715 out << hashFunctionName(node->getName()) << "(";
716 else if (visit == InVisit)
717 out << ", ";
718 else
zmo@google.com5601ea02011-06-10 18:23:25 +0000719 out << ")";
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700720 break;
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200721 case EOpInternalFunctionCall:
722 // Function call to an internal helper function.
723 if (visit == PreVisit)
724 out << node->getName() << "(";
725 else if (visit == InVisit)
726 out << ", ";
727 else
728 out << ")";
729 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700730 case EOpParameters:
731 // Function parameters.
732 ASSERT(visit == PreVisit);
733 out << "(";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700734 writeFunctionParameters(*(node->getSequence()));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700735 out << ")";
736 visitChildren = false;
737 break;
738 case EOpDeclaration:
739 // Variable declaration.
740 if (visit == PreVisit)
741 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700742 const TIntermSequence &sequence = *(node->getSequence());
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700743 const TIntermTyped *variable = sequence.front()->getAsTyped();
744 writeVariableType(variable->getType());
745 out << " ";
746 mDeclaringVariables = true;
zmo@google.com5601ea02011-06-10 18:23:25 +0000747 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700748 else if (visit == InVisit)
749 {
750 out << ", ";
751 mDeclaringVariables = true;
zmo@google.com5601ea02011-06-10 18:23:25 +0000752 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700753 else
754 {
755 mDeclaringVariables = false;
756 }
757 break;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700758 case EOpInvariantDeclaration:
759 // Invariant declaration.
760 ASSERT(visit == PreVisit);
761 {
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400762 const TIntermSequence *sequence = node->getSequence();
763 ASSERT(sequence && sequence->size() == 1);
764 const TIntermSymbol *symbol = sequence->front()->getAsSymbolNode();
765 ASSERT(symbol);
Zhenyao Mo2a517272014-10-27 16:09:57 -0700766 out << "invariant " << hashVariableName(symbol->getSymbol());
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400767 }
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700768 visitChildren = false;
769 break;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700770 case EOpConstructFloat:
771 writeTriplet(visit, "float(", NULL, ")");
772 break;
773 case EOpConstructVec2:
774 writeBuiltInFunctionTriplet(visit, "vec2(", false);
775 break;
776 case EOpConstructVec3:
777 writeBuiltInFunctionTriplet(visit, "vec3(", false);
778 break;
779 case EOpConstructVec4:
780 writeBuiltInFunctionTriplet(visit, "vec4(", false);
781 break;
782 case EOpConstructBool:
783 writeTriplet(visit, "bool(", NULL, ")");
784 break;
785 case EOpConstructBVec2:
786 writeBuiltInFunctionTriplet(visit, "bvec2(", false);
787 break;
788 case EOpConstructBVec3:
789 writeBuiltInFunctionTriplet(visit, "bvec3(", false);
790 break;
791 case EOpConstructBVec4:
792 writeBuiltInFunctionTriplet(visit, "bvec4(", false);
793 break;
794 case EOpConstructInt:
795 writeTriplet(visit, "int(", NULL, ")");
796 break;
797 case EOpConstructIVec2:
798 writeBuiltInFunctionTriplet(visit, "ivec2(", false);
799 break;
800 case EOpConstructIVec3:
801 writeBuiltInFunctionTriplet(visit, "ivec3(", false);
802 break;
803 case EOpConstructIVec4:
804 writeBuiltInFunctionTriplet(visit, "ivec4(", false);
805 break;
806 case EOpConstructMat2:
807 writeBuiltInFunctionTriplet(visit, "mat2(", false);
808 break;
809 case EOpConstructMat3:
810 writeBuiltInFunctionTriplet(visit, "mat3(", false);
811 break;
812 case EOpConstructMat4:
813 writeBuiltInFunctionTriplet(visit, "mat4(", false);
814 break;
815 case EOpConstructStruct:
816 if (visit == PreVisit)
817 {
818 const TType &type = node->getType();
819 ASSERT(type.getBasicType() == EbtStruct);
820 out << hashName(type.getStruct()->name()) << "(";
821 }
822 else if (visit == InVisit)
823 {
824 out << ", ";
825 }
826 else
827 {
zmo@google.com5601ea02011-06-10 18:23:25 +0000828 out << ")";
zmo@google.com5601ea02011-06-10 18:23:25 +0000829 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700830 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000831
Olli Etuahoe39706d2014-12-30 16:40:36 +0200832 case EOpOuterProduct:
833 writeBuiltInFunctionTriplet(visit, "outerProduct(", useEmulatedFunction);
834 break;
835
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700836 case EOpLessThan:
837 writeBuiltInFunctionTriplet(visit, "lessThan(", useEmulatedFunction);
838 break;
839 case EOpGreaterThan:
840 writeBuiltInFunctionTriplet(visit, "greaterThan(", useEmulatedFunction);
841 break;
842 case EOpLessThanEqual:
843 writeBuiltInFunctionTriplet(visit, "lessThanEqual(", useEmulatedFunction);
844 break;
845 case EOpGreaterThanEqual:
846 writeBuiltInFunctionTriplet(visit, "greaterThanEqual(", useEmulatedFunction);
847 break;
848 case EOpVectorEqual:
849 writeBuiltInFunctionTriplet(visit, "equal(", useEmulatedFunction);
850 break;
851 case EOpVectorNotEqual:
852 writeBuiltInFunctionTriplet(visit, "notEqual(", useEmulatedFunction);
853 break;
854 case EOpComma:
Zhenyao Mo0783efd2014-10-21 15:51:59 -0700855 writeTriplet(visit, "(", ", ", ")");
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700856 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000857
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700858 case EOpMod:
859 writeBuiltInFunctionTriplet(visit, "mod(", useEmulatedFunction);
860 break;
861 case EOpPow:
862 writeBuiltInFunctionTriplet(visit, "pow(", useEmulatedFunction);
863 break;
864 case EOpAtan:
865 writeBuiltInFunctionTriplet(visit, "atan(", useEmulatedFunction);
866 break;
867 case EOpMin:
868 writeBuiltInFunctionTriplet(visit, "min(", useEmulatedFunction);
869 break;
870 case EOpMax:
871 writeBuiltInFunctionTriplet(visit, "max(", useEmulatedFunction);
872 break;
873 case EOpClamp:
874 writeBuiltInFunctionTriplet(visit, "clamp(", useEmulatedFunction);
875 break;
876 case EOpMix:
877 writeBuiltInFunctionTriplet(visit, "mix(", useEmulatedFunction);
878 break;
879 case EOpStep:
880 writeBuiltInFunctionTriplet(visit, "step(", useEmulatedFunction);
881 break;
882 case EOpSmoothStep:
883 writeBuiltInFunctionTriplet(visit, "smoothstep(", useEmulatedFunction);
884 break;
885 case EOpDistance:
886 writeBuiltInFunctionTriplet(visit, "distance(", useEmulatedFunction);
887 break;
888 case EOpDot:
889 writeBuiltInFunctionTriplet(visit, "dot(", useEmulatedFunction);
890 break;
891 case EOpCross:
892 writeBuiltInFunctionTriplet(visit, "cross(", useEmulatedFunction);
893 break;
894 case EOpFaceForward:
895 writeBuiltInFunctionTriplet(visit, "faceforward(", useEmulatedFunction);
896 break;
897 case EOpReflect:
898 writeBuiltInFunctionTriplet(visit, "reflect(", useEmulatedFunction);
899 break;
900 case EOpRefract:
901 writeBuiltInFunctionTriplet(visit, "refract(", useEmulatedFunction);
902 break;
903 case EOpMul:
904 writeBuiltInFunctionTriplet(visit, "matrixCompMult(", useEmulatedFunction);
905 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000906
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700907 default:
908 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000909 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000910 return visitChildren;
911}
912
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700913bool TOutputGLSLBase::visitLoop(Visit visit, TIntermLoop *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000914{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700915 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000916
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700917 incrementDepth(node);
zmo@google.com5601ea02011-06-10 18:23:25 +0000918 // Loop header.
919 TLoopType loopType = node->getType();
920 if (loopType == ELoopFor) // for loop
921 {
Zhenyao Mo550c6002014-02-26 15:40:48 -0800922 if (!node->getUnrollFlag())
923 {
zmo@google.com5601ea02011-06-10 18:23:25 +0000924 out << "for (";
925 if (node->getInit())
926 node->getInit()->traverse(this);
927 out << "; ";
928
929 if (node->getCondition())
930 node->getCondition()->traverse(this);
931 out << "; ";
932
933 if (node->getExpression())
934 node->getExpression()->traverse(this);
935 out << ")\n";
936 }
Zhenyao Mo550c6002014-02-26 15:40:48 -0800937 else
938 {
939 // Need to put a one-iteration loop here to handle break.
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700940 TIntermSequence *declSeq =
Zhenyao Mo550c6002014-02-26 15:40:48 -0800941 node->getInit()->getAsAggregate()->getSequence();
942 TIntermSymbol *indexSymbol =
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700943 (*declSeq)[0]->getAsBinaryNode()->getLeft()->getAsSymbolNode();
Zhenyao Mo550c6002014-02-26 15:40:48 -0800944 TString name = hashVariableName(indexSymbol->getSymbol());
945 out << "for (int " << name << " = 0; "
946 << name << " < 1; "
947 << "++" << name << ")\n";
948 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000949 }
950 else if (loopType == ELoopWhile) // while loop
951 {
952 out << "while (";
953 ASSERT(node->getCondition() != NULL);
954 node->getCondition()->traverse(this);
955 out << ")\n";
956 }
957 else // do-while loop
958 {
959 ASSERT(loopType == ELoopDoWhile);
960 out << "do\n";
961 }
962
963 // Loop body.
964 if (node->getUnrollFlag())
965 {
Zhenyao Mo550c6002014-02-26 15:40:48 -0800966 out << "{\n";
967 mLoopUnrollStack.push(node);
968 while (mLoopUnrollStack.satisfiesLoopCondition())
zmo@google.com5601ea02011-06-10 18:23:25 +0000969 {
970 visitCodeBlock(node->getBody());
Zhenyao Mo550c6002014-02-26 15:40:48 -0800971 mLoopUnrollStack.step();
zmo@google.com5601ea02011-06-10 18:23:25 +0000972 }
Zhenyao Mo550c6002014-02-26 15:40:48 -0800973 mLoopUnrollStack.pop();
974 out << "}\n";
zmo@google.com5601ea02011-06-10 18:23:25 +0000975 }
976 else
977 {
978 visitCodeBlock(node->getBody());
979 }
980
981 // Loop footer.
982 if (loopType == ELoopDoWhile) // do-while loop
983 {
984 out << "while (";
985 ASSERT(node->getCondition() != NULL);
986 node->getCondition()->traverse(this);
987 out << ");\n";
988 }
989 decrementDepth();
990
991 // No need to visit children. They have been already processed in
992 // this function.
993 return false;
994}
995
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700996bool TOutputGLSLBase::visitBranch(Visit visit, TIntermBranch *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000997{
998 switch (node->getFlowOp())
999 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001000 case EOpKill:
1001 writeTriplet(visit, "discard", NULL, NULL);
1002 break;
1003 case EOpBreak:
1004 writeTriplet(visit, "break", NULL, NULL);
1005 break;
1006 case EOpContinue:
1007 writeTriplet(visit, "continue", NULL, NULL);
1008 break;
1009 case EOpReturn:
1010 writeTriplet(visit, "return ", NULL, NULL);
1011 break;
1012 default:
1013 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +00001014 }
1015
1016 return true;
1017}
1018
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001019void TOutputGLSLBase::visitCodeBlock(TIntermNode *node)
1020{
zmo@google.com5601ea02011-06-10 18:23:25 +00001021 TInfoSinkBase &out = objSink();
1022 if (node != NULL)
1023 {
1024 node->traverse(this);
1025 // Single statements not part of a sequence need to be terminated
1026 // with semi-colon.
1027 if (isSingleStatement(node))
1028 out << ";\n";
1029 }
1030 else
1031 {
1032 out << "{\n}\n"; // Empty code block.
1033 }
1034}
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001035
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001036TString TOutputGLSLBase::getTypeName(const TType &type)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001037{
1038 TInfoSinkBase out;
1039 if (type.isMatrix())
1040 {
1041 out << "mat";
1042 out << type.getNominalSize();
1043 }
1044 else if (type.isVector())
1045 {
1046 switch (type.getBasicType())
1047 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001048 case EbtFloat:
1049 out << "vec";
1050 break;
1051 case EbtInt:
1052 out << "ivec";
1053 break;
1054 case EbtBool:
1055 out << "bvec";
1056 break;
1057 default:
1058 UNREACHABLE();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001059 }
1060 out << type.getNominalSize();
1061 }
1062 else
1063 {
1064 if (type.getBasicType() == EbtStruct)
Jamie Madill98493dd2013-07-08 14:39:03 -04001065 out << hashName(type.getStruct()->name());
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001066 else
1067 out << type.getBasicString();
1068 }
1069 return TString(out.c_str());
1070}
1071
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001072TString TOutputGLSLBase::hashName(const TString &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001073{
1074 if (mHashFunction == NULL || name.empty())
1075 return name;
1076 NameMap::const_iterator it = mNameMap.find(name.c_str());
1077 if (it != mNameMap.end())
1078 return it->second.c_str();
1079 TString hashedName = TIntermTraverser::hash(name, mHashFunction);
1080 mNameMap[name.c_str()] = hashedName.c_str();
1081 return hashedName;
1082}
1083
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001084TString TOutputGLSLBase::hashVariableName(const TString &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001085{
Jamie Madill02f20dd2013-09-12 12:07:42 -04001086 if (mSymbolTable.findBuiltIn(name, mShaderVersion) != NULL)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001087 return name;
1088 return hashName(name);
1089}
1090
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001091TString TOutputGLSLBase::hashFunctionName(const TString &mangled_name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001092{
1093 TString name = TFunction::unmangleName(mangled_name);
Jamie Madill02f20dd2013-09-12 12:07:42 -04001094 if (mSymbolTable.findBuiltIn(mangled_name, mShaderVersion) != NULL || name == "main")
Nicolas Capens46485082014-04-15 13:12:50 -04001095 return translateTextureFunction(name);
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001096 return hashName(name);
1097}
Jamie Madill98493dd2013-07-08 14:39:03 -04001098
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001099bool TOutputGLSLBase::structDeclared(const TStructure *structure) const
Jamie Madill98493dd2013-07-08 14:39:03 -04001100{
Zhenyao Mo904a9162014-05-09 14:07:45 -07001101 ASSERT(structure);
Jamie Madill01f85ac2014-06-06 11:55:04 -04001102 if (structure->name().empty())
Zhenyao Mo904a9162014-05-09 14:07:45 -07001103 {
Jamie Madill01f85ac2014-06-06 11:55:04 -04001104 return false;
Zhenyao Mo904a9162014-05-09 14:07:45 -07001105 }
Jamie Madill01f85ac2014-06-06 11:55:04 -04001106
1107 return (mDeclaredStructs.count(structure->uniqueId()) > 0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001108}
1109
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001110void TOutputGLSLBase::declareStruct(const TStructure *structure)
Jamie Madill98493dd2013-07-08 14:39:03 -04001111{
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001112 TInfoSinkBase &out = objSink();
Jamie Madill98493dd2013-07-08 14:39:03 -04001113
1114 out << "struct " << hashName(structure->name()) << "{\n";
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001115 const TFieldList &fields = structure->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04001116 for (size_t i = 0; i < fields.size(); ++i)
1117 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001118 const TField *field = fields[i];
Jamie Madill98493dd2013-07-08 14:39:03 -04001119 if (writeVariablePrecision(field->type()->getPrecision()))
1120 out << " ";
1121 out << getTypeName(*field->type()) << " " << hashName(field->name());
1122 if (field->type()->isArray())
1123 out << arrayBrackets(*field->type());
1124 out << ";\n";
1125 }
1126 out << "}";
Zhenyao Mo904a9162014-05-09 14:07:45 -07001127}
Jamie Madill98493dd2013-07-08 14:39:03 -04001128