blob: d71c85737cac0ed929e43dcd30bb9cf3f9c9946a [file] [log] [blame]
zmo@google.com5601ea02011-06-10 18:23:25 +00001//
Nicolas Capens16004fc2014-06-11 11:29:11 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
zmo@google.com5601ea02011-06-10 18:23:25 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/OutputGLSLBase.h"
Olli Etuahod57e0db2015-04-24 15:05:08 +03008
9#include "common/debug.h"
Olli Etuaho56a2f952016-12-08 12:16:27 +000010#include "common/mathutil.h"
zmo@google.com5601ea02011-06-10 18:23:25 +000011
daniel@transgaming.com773ff742013-01-11 04:12:51 +000012#include <cfloat>
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +000013
Jamie Madill45bcc782016-11-07 13:58:48 -050014namespace sh
15{
16
zmo@google.com5601ea02011-06-10 18:23:25 +000017namespace
18{
Zhenyao Mo9eedea02014-05-12 16:02:35 -070019TString arrayBrackets(const TType &type)
zmo@google.com5601ea02011-06-10 18:23:25 +000020{
21 ASSERT(type.isArray());
22 TInfoSinkBase out;
23 out << "[" << type.getArraySize() << "]";
24 return TString(out.c_str());
25}
26
Zhenyao Mo9eedea02014-05-12 16:02:35 -070027bool isSingleStatement(TIntermNode *node)
28{
Olli Etuaho336b1472016-10-05 16:37:55 +010029 if (node->getAsFunctionDefinition())
zmo@google.com5601ea02011-06-10 18:23:25 +000030 {
Olli Etuaho336b1472016-10-05 16:37:55 +010031 return false;
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010032 }
33 else if (node->getAsBlock())
34 {
35 return false;
zmo@google.com5601ea02011-06-10 18:23:25 +000036 }
Olli Etuaho57961272016-09-14 13:57:46 +030037 else if (node->getAsIfElseNode())
zmo@google.com5601ea02011-06-10 18:23:25 +000038 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +030039 return false;
zmo@google.com5601ea02011-06-10 18:23:25 +000040 }
41 else if (node->getAsLoopNode())
42 {
43 return false;
44 }
Olli Etuaho01cd8af2015-02-20 10:39:20 +020045 else if (node->getAsSwitchNode())
46 {
47 return false;
48 }
49 else if (node->getAsCaseNode())
50 {
51 return false;
52 }
zmo@google.com5601ea02011-06-10 18:23:25 +000053 return true;
54}
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080055
Martin Radev2cc85b32016-08-05 16:22:53 +030056// If SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS is enabled, layout qualifiers are spilled whenever
57// variables with specified layout qualifiers are copied. Additional checks are needed against the
58// type and storage qualifier of the variable to verify that layout qualifiers have to be outputted.
59// TODO (mradev): Fix layout qualifier spilling in ScalarizeVecAndMatConstructorArgs and remove
60// NeedsToWriteLayoutQualifier.
61bool NeedsToWriteLayoutQualifier(const TType &type)
62{
63 if (type.getBasicType() == EbtInterfaceBlock)
64 {
65 return false;
66 }
67
68 const TLayoutQualifier &layoutQualifier = type.getLayoutQualifier();
69
70 if ((type.getQualifier() == EvqFragmentOut || type.getQualifier() == EvqVertexIn) &&
71 layoutQualifier.location >= 0)
72 {
73 return true;
74 }
75 if (IsImage(type.getBasicType()) && layoutQualifier.imageInternalFormat != EiifUnspecified)
76 {
77 return true;
78 }
79 return false;
80}
81
zmo@google.com5601ea02011-06-10 18:23:25 +000082} // namespace
83
Zhenyao Mo9eedea02014-05-12 16:02:35 -070084TOutputGLSLBase::TOutputGLSLBase(TInfoSinkBase &objSink,
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +000085 ShArrayIndexClampingStrategy clampingStrategy,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000086 ShHashFunction64 hashFunction,
Zhenyao Mo9eedea02014-05-12 16:02:35 -070087 NameMap &nameMap,
88 TSymbolTable &symbolTable,
Qiankun Miao89dd8f32016-11-09 12:59:30 +000089 sh::GLenum shaderType,
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080090 int shaderVersion,
Qiankun Miao705a9192016-08-29 10:05:27 +080091 ShShaderOutput output,
92 ShCompileOptions compileOptions)
zmo@google.com5601ea02011-06-10 18:23:25 +000093 : TIntermTraverser(true, true, true),
94 mObjSink(objSink),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000095 mDeclaringVariables(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +000096 mClampingStrategy(clampingStrategy),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000097 mHashFunction(hashFunction),
98 mNameMap(nameMap),
Jamie Madill02f20dd2013-09-12 12:07:42 -040099 mSymbolTable(symbolTable),
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000100 mShaderType(shaderType),
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800101 mShaderVersion(shaderVersion),
Qiankun Miao705a9192016-08-29 10:05:27 +0800102 mOutput(output),
103 mCompileOptions(compileOptions)
zmo@google.com5601ea02011-06-10 18:23:25 +0000104{
105}
106
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800107void TOutputGLSLBase::writeInvariantQualifier(const TType &type)
108{
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000109 if (!sh::RemoveInvariant(mShaderType, mShaderVersion, mOutput, mCompileOptions))
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800110 {
111 TInfoSinkBase &out = objSink();
112 out << "invariant ";
113 }
114}
115
Olli Etuaho56a2f952016-12-08 12:16:27 +0000116void TOutputGLSLBase::writeFloat(TInfoSinkBase &out, float f)
117{
118 if ((gl::isInf(f) || gl::isNaN(f)) && mShaderVersion >= 300)
119 {
120 out << "uintBitsToFloat(" << gl::bitCast<uint32_t>(f) << "u)";
121 }
122 else
123 {
124 out << std::min(FLT_MAX, std::max(-FLT_MAX, f));
125 }
126}
127
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500128void TOutputGLSLBase::writeTriplet(Visit visit,
129 const char *preStr,
130 const char *inStr,
131 const char *postStr)
zmo@google.com5601ea02011-06-10 18:23:25 +0000132{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700133 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000134 if (visit == PreVisit && preStr)
zmo@google.com5601ea02011-06-10 18:23:25 +0000135 out << preStr;
zmo@google.com5601ea02011-06-10 18:23:25 +0000136 else if (visit == InVisit && inStr)
zmo@google.com5601ea02011-06-10 18:23:25 +0000137 out << inStr;
zmo@google.com5601ea02011-06-10 18:23:25 +0000138 else if (visit == PostVisit && postStr)
zmo@google.com5601ea02011-06-10 18:23:25 +0000139 out << postStr;
zmo@google.com5601ea02011-06-10 18:23:25 +0000140}
141
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500142void TOutputGLSLBase::writeBuiltInFunctionTriplet(Visit visit,
Olli Etuahoe1805592017-01-02 16:41:20 +0000143 TOperator op,
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500144 bool useEmulatedFunction)
zmo@google.com5601ea02011-06-10 18:23:25 +0000145{
Olli Etuahod68924e2017-01-02 17:34:40 +0000146 TInfoSinkBase &out = objSink();
147 if (visit == PreVisit)
Olli Etuahoe1805592017-01-02 16:41:20 +0000148 {
Olli Etuahod68924e2017-01-02 17:34:40 +0000149 const char *opStr(GetOperatorString(op));
150 if (useEmulatedFunction)
151 {
152 BuiltInFunctionEmulator::WriteEmulatedFunctionName(out, opStr);
153 }
154 else
155 {
156 out << opStr;
157 }
158 out << "(";
Olli Etuahoe1805592017-01-02 16:41:20 +0000159 }
Olli Etuahod68924e2017-01-02 17:34:40 +0000160 else
161 {
162 writeTriplet(visit, nullptr, ", ", ")");
163 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700164}
165
Olli Etuahoae69d7e2015-10-07 17:19:50 +0300166void TOutputGLSLBase::writeLayoutQualifier(const TType &type)
167{
Martin Radev2cc85b32016-08-05 16:22:53 +0300168 if (!NeedsToWriteLayoutQualifier(type))
169 {
170 return;
171 }
172
173 TInfoSinkBase &out = objSink();
174 const TLayoutQualifier &layoutQualifier = type.getLayoutQualifier();
175 out << "layout(";
176
Olli Etuahoae69d7e2015-10-07 17:19:50 +0300177 if (type.getQualifier() == EvqFragmentOut || type.getQualifier() == EvqVertexIn)
178 {
Olli Etuahoae69d7e2015-10-07 17:19:50 +0300179 if (layoutQualifier.location >= 0)
180 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300181 out << "location = " << layoutQualifier.location;
Olli Etuahoae69d7e2015-10-07 17:19:50 +0300182 }
183 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300184
185 if (IsImage(type.getBasicType()) && layoutQualifier.imageInternalFormat != EiifUnspecified)
186 {
187 ASSERT(type.getQualifier() == EvqTemporary || type.getQualifier() == EvqUniform);
188 out << getImageInternalFormatString(layoutQualifier.imageInternalFormat);
189 }
190
191 out << ") ";
Olli Etuahoae69d7e2015-10-07 17:19:50 +0300192}
193
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800194const char *TOutputGLSLBase::mapQualifierToString(TQualifier qualifier)
195{
196 if (sh::IsGLSL410OrOlder(mOutput) && mShaderVersion >= 300 &&
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000197 (mCompileOptions & SH_REMOVE_INVARIANT_AND_CENTROID_FOR_ESSL3) != 0)
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800198 {
199 switch (qualifier)
200 {
201 // The return string is consistent with sh::getQualifierString() from
202 // BaseTypes.h minus the "centroid" keyword.
203 case EvqCentroid:
204 return "";
205 case EvqCentroidIn:
206 return "smooth in";
207 case EvqCentroidOut:
208 return "smooth out";
209 default:
210 break;
211 }
212 }
213 if (sh::IsGLSL130OrNewer(mOutput))
214 {
215 switch (qualifier)
216 {
217 case EvqAttribute:
218 return "in";
219 case EvqVaryingIn:
220 return "in";
221 case EvqVaryingOut:
222 return "out";
223 default:
224 break;
225 }
226 }
227 return sh::getQualifierString(qualifier);
228}
229
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700230void TOutputGLSLBase::writeVariableType(const TType &type)
231{
Qiankun Miao705a9192016-08-29 10:05:27 +0800232 TQualifier qualifier = type.getQualifier();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500233 TInfoSinkBase &out = objSink();
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800234 if (type.isInvariant())
Olli Etuaho214c2d82015-04-27 14:49:13 +0300235 {
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800236 writeInvariantQualifier(type);
Olli Etuaho214c2d82015-04-27 14:49:13 +0300237 }
Geoff Langbdcc54a2015-09-02 13:09:48 -0400238 if (type.getBasicType() == EbtInterfaceBlock)
239 {
240 TInterfaceBlock *interfaceBlock = type.getInterfaceBlock();
241 declareInterfaceBlockLayout(interfaceBlock);
242 }
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400243 if (qualifier != EvqTemporary && qualifier != EvqGlobal)
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400244 {
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800245 const char *qualifierString = mapQualifierToString(qualifier);
246 if (qualifierString && qualifierString[0] != '\0')
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800247 {
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800248 out << qualifierString << " ";
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800249 }
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400250 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300251
252 const TMemoryQualifier &memoryQualifier = type.getMemoryQualifier();
253 if (memoryQualifier.readonly)
254 {
255 ASSERT(IsImage(type.getBasicType()));
256 out << "readonly ";
257 }
258
259 if (memoryQualifier.writeonly)
260 {
261 ASSERT(IsImage(type.getBasicType()));
262 out << "writeonly ";
263 }
264
Martin Radev049edfa2016-11-11 14:35:37 +0200265 if (memoryQualifier.coherent)
266 {
267 ASSERT(IsImage(type.getBasicType()));
268 out << "coherent ";
269 }
270
271 if (memoryQualifier.restrictQualifier)
272 {
273 ASSERT(IsImage(type.getBasicType()));
274 out << "restrict ";
275 }
276
277 if (memoryQualifier.volatileQualifier)
278 {
279 ASSERT(IsImage(type.getBasicType()));
280 out << "volatile ";
281 }
282
zmo@google.com5601ea02011-06-10 18:23:25 +0000283 // Declare the struct if we have not done so already.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700284 if (type.getBasicType() == EbtStruct && !structDeclared(type.getStruct()))
zmo@google.com5601ea02011-06-10 18:23:25 +0000285 {
Jamie Madill01f85ac2014-06-06 11:55:04 -0400286 TStructure *structure = type.getStruct();
287
288 declareStruct(structure);
289
290 if (!structure->name().empty())
291 {
292 mDeclaredStructs.insert(structure->uniqueId());
293 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000294 }
Geoff Langbdcc54a2015-09-02 13:09:48 -0400295 else if (type.getBasicType() == EbtInterfaceBlock)
296 {
297 TInterfaceBlock *interfaceBlock = type.getInterfaceBlock();
298 declareInterfaceBlock(interfaceBlock);
299 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000300 else
301 {
302 if (writeVariablePrecision(type.getPrecision()))
303 out << " ";
304 out << getTypeName(type);
305 }
306}
307
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700308void TOutputGLSLBase::writeFunctionParameters(const TIntermSequence &args)
zmo@google.com5601ea02011-06-10 18:23:25 +0000309{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700310 TInfoSinkBase &out = objSink();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500311 for (TIntermSequence::const_iterator iter = args.begin(); iter != args.end(); ++iter)
zmo@google.com5601ea02011-06-10 18:23:25 +0000312 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700313 const TIntermSymbol *arg = (*iter)->getAsSymbolNode();
zmo@google.com5601ea02011-06-10 18:23:25 +0000314 ASSERT(arg != NULL);
315
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700316 const TType &type = arg->getType();
zmo@google.com189be2f2011-06-16 18:28:53 +0000317 writeVariableType(type);
zmo@google.com5601ea02011-06-10 18:23:25 +0000318
Olli Etuaho0982a2b2016-11-01 15:13:46 +0000319 if (!arg->getName().getString().empty())
320 out << " " << hashName(arg->getName());
zmo@google.com5601ea02011-06-10 18:23:25 +0000321 if (type.isArray())
322 out << arrayBrackets(type);
323
324 // Put a comma if this is not the last argument.
325 if (iter != args.end() - 1)
326 out << ", ";
327 }
328}
329
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500330const TConstantUnion *TOutputGLSLBase::writeConstantUnion(const TType &type,
331 const TConstantUnion *pConstUnion)
zmo@google.com5601ea02011-06-10 18:23:25 +0000332{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700333 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000334
335 if (type.getBasicType() == EbtStruct)
336 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700337 const TStructure *structure = type.getStruct();
Olli Etuaho0982a2b2016-11-01 15:13:46 +0000338 out << hashName(TName(structure->name())) << "(";
Jamie Madill98493dd2013-07-08 14:39:03 -0400339
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700340 const TFieldList &fields = structure->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -0400341 for (size_t i = 0; i < fields.size(); ++i)
zmo@google.com5601ea02011-06-10 18:23:25 +0000342 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700343 const TType *fieldType = fields[i]->type();
zmo@google.com5601ea02011-06-10 18:23:25 +0000344 ASSERT(fieldType != NULL);
345 pConstUnion = writeConstantUnion(*fieldType, pConstUnion);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700346 if (i != fields.size() - 1)
347 out << ", ";
zmo@google.com5601ea02011-06-10 18:23:25 +0000348 }
349 out << ")";
350 }
351 else
352 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500353 size_t size = type.getObjectSize();
zmo@google.com5601ea02011-06-10 18:23:25 +0000354 bool writeType = size > 1;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700355 if (writeType)
356 out << getTypeName(type) << "(";
Jamie Madill94bf7f22013-07-08 13:31:15 -0400357 for (size_t i = 0; i < size; ++i, ++pConstUnion)
zmo@google.com5601ea02011-06-10 18:23:25 +0000358 {
359 switch (pConstUnion->getType())
360 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500361 case EbtFloat:
362 writeFloat(out, pConstUnion->getFConst());
363 break;
364 case EbtInt:
365 out << pConstUnion->getIConst();
366 break;
367 case EbtUInt:
368 out << pConstUnion->getUConst() << "u";
369 break;
370 case EbtBool:
371 out << pConstUnion->getBConst();
372 break;
373 default:
374 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000375 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700376 if (i != size - 1)
377 out << ", ";
zmo@google.com5601ea02011-06-10 18:23:25 +0000378 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700379 if (writeType)
380 out << ")";
zmo@google.com5601ea02011-06-10 18:23:25 +0000381 }
382 return pConstUnion;
383}
384
Olli Etuahoe92507b2016-07-04 11:20:10 +0300385void TOutputGLSLBase::writeConstructorTriplet(Visit visit, const TType &type)
Olli Etuahof40319e2015-03-10 14:33:00 +0200386{
387 TInfoSinkBase &out = objSink();
388 if (visit == PreVisit)
389 {
390 if (type.isArray())
391 {
Olli Etuahoe92507b2016-07-04 11:20:10 +0300392 out << getTypeName(type);
Olli Etuahof40319e2015-03-10 14:33:00 +0200393 out << arrayBrackets(type);
394 out << "(";
395 }
396 else
397 {
Olli Etuahoe92507b2016-07-04 11:20:10 +0300398 out << getTypeName(type) << "(";
Olli Etuahof40319e2015-03-10 14:33:00 +0200399 }
400 }
401 else
402 {
403 writeTriplet(visit, nullptr, ", ", ")");
404 }
405}
406
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700407void TOutputGLSLBase::visitSymbol(TIntermSymbol *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000408{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700409 TInfoSinkBase &out = objSink();
Corentin Wallez1b896c62016-11-16 13:10:44 -0500410 out << hashVariableName(node->getName());
zmo@google.com5601ea02011-06-10 18:23:25 +0000411
412 if (mDeclaringVariables && node->getType().isArray())
413 out << arrayBrackets(node->getType());
414}
415
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700416void TOutputGLSLBase::visitConstantUnion(TIntermConstantUnion *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000417{
418 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
419}
420
Olli Etuahob6fa0432016-09-28 16:28:05 +0100421bool TOutputGLSLBase::visitSwizzle(Visit visit, TIntermSwizzle *node)
422{
423 TInfoSinkBase &out = objSink();
424 if (visit == PostVisit)
425 {
426 out << ".";
427 node->writeOffsetsAsXYZW(&out);
428 }
429 return true;
430}
431
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700432bool TOutputGLSLBase::visitBinary(Visit visit, TIntermBinary *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000433{
434 bool visitChildren = true;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700435 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000436 switch (node->getOp())
437 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100438 case EOpComma:
439 writeTriplet(visit, "(", ", ", ")");
440 break;
441 case EOpInitialize:
zmo@google.com5601ea02011-06-10 18:23:25 +0000442 if (visit == InVisit)
443 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100444 out << " = ";
445 // RHS of initialize is not being declared.
446 mDeclaringVariables = false;
zmo@google.com5601ea02011-06-10 18:23:25 +0000447 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100448 break;
449 case EOpAssign:
450 writeTriplet(visit, "(", " = ", ")");
451 break;
452 case EOpAddAssign:
453 writeTriplet(visit, "(", " += ", ")");
454 break;
455 case EOpSubAssign:
456 writeTriplet(visit, "(", " -= ", ")");
457 break;
458 case EOpDivAssign:
459 writeTriplet(visit, "(", " /= ", ")");
460 break;
461 case EOpIModAssign:
462 writeTriplet(visit, "(", " %= ", ")");
463 break;
464 // Notice the fall-through.
465 case EOpMulAssign:
466 case EOpVectorTimesMatrixAssign:
467 case EOpVectorTimesScalarAssign:
468 case EOpMatrixTimesScalarAssign:
469 case EOpMatrixTimesMatrixAssign:
470 writeTriplet(visit, "(", " *= ", ")");
471 break;
472 case EOpBitShiftLeftAssign:
473 writeTriplet(visit, "(", " <<= ", ")");
474 break;
475 case EOpBitShiftRightAssign:
476 writeTriplet(visit, "(", " >>= ", ")");
477 break;
478 case EOpBitwiseAndAssign:
479 writeTriplet(visit, "(", " &= ", ")");
480 break;
481 case EOpBitwiseXorAssign:
482 writeTriplet(visit, "(", " ^= ", ")");
483 break;
484 case EOpBitwiseOrAssign:
485 writeTriplet(visit, "(", " |= ", ")");
486 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000487
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100488 case EOpIndexDirect:
zmo@google.com5601ea02011-06-10 18:23:25 +0000489 writeTriplet(visit, NULL, "[", "]");
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100490 break;
491 case EOpIndexIndirect:
492 if (node->getAddIndexClamp())
493 {
494 if (visit == InVisit)
495 {
496 if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC)
497 out << "[int(clamp(float(";
498 else
499 out << "[webgl_int_clamp(";
500 }
501 else if (visit == PostVisit)
502 {
503 int maxSize;
504 TIntermTyped *left = node->getLeft();
505 TType leftType = left->getType();
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700506
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100507 if (left->isArray())
508 {
509 // The shader will fail validation if the array length is not > 0.
510 maxSize = static_cast<int>(leftType.getArraySize()) - 1;
511 }
512 else
513 {
514 maxSize = leftType.getNominalSize() - 1;
515 }
516
517 if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC)
518 out << "), 0.0, float(" << maxSize << ")))]";
519 else
520 out << ", 0, " << maxSize << ")]";
521 }
522 }
523 else
524 {
525 writeTriplet(visit, NULL, "[", "]");
526 }
527 break;
528 case EOpIndexDirectStruct:
529 if (visit == InVisit)
530 {
531 // Here we are writing out "foo.bar", where "foo" is struct
532 // and "bar" is field. In AST, it is represented as a binary
533 // node, where left child represents "foo" and right child "bar".
534 // The node itself represents ".". The struct field "bar" is
535 // actually stored as an index into TStructure::fields.
536 out << ".";
537 const TStructure *structure = node->getLeft()->getType().getStruct();
538 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
539 const TField *field = structure->fields()[index->getIConst(0)];
540
541 TString fieldName = field->name();
542 if (!mSymbolTable.findBuiltIn(structure->name(), mShaderVersion))
Olli Etuaho0982a2b2016-11-01 15:13:46 +0000543 fieldName = hashName(TName(fieldName));
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100544
545 out << fieldName;
546 visitChildren = false;
547 }
548 break;
549 case EOpIndexDirectInterfaceBlock:
550 if (visit == InVisit)
551 {
552 out << ".";
553 const TInterfaceBlock *interfaceBlock =
554 node->getLeft()->getType().getInterfaceBlock();
555 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
556 const TField *field = interfaceBlock->fields()[index->getIConst(0)];
557
558 TString fieldName = field->name();
559 ASSERT(!mSymbolTable.findBuiltIn(interfaceBlock->name(), mShaderVersion));
Olli Etuaho0982a2b2016-11-01 15:13:46 +0000560 fieldName = hashName(TName(fieldName));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700561
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100562 out << fieldName;
563 visitChildren = false;
564 }
565 break;
Geoff Lang6e360422015-09-02 15:54:36 -0400566
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100567 case EOpAdd:
568 writeTriplet(visit, "(", " + ", ")");
569 break;
570 case EOpSub:
571 writeTriplet(visit, "(", " - ", ")");
572 break;
573 case EOpMul:
574 writeTriplet(visit, "(", " * ", ")");
575 break;
576 case EOpDiv:
577 writeTriplet(visit, "(", " / ", ")");
578 break;
579 case EOpIMod:
580 writeTriplet(visit, "(", " % ", ")");
581 break;
582 case EOpBitShiftLeft:
583 writeTriplet(visit, "(", " << ", ")");
584 break;
585 case EOpBitShiftRight:
586 writeTriplet(visit, "(", " >> ", ")");
587 break;
588 case EOpBitwiseAnd:
589 writeTriplet(visit, "(", " & ", ")");
590 break;
591 case EOpBitwiseXor:
592 writeTriplet(visit, "(", " ^ ", ")");
593 break;
594 case EOpBitwiseOr:
595 writeTriplet(visit, "(", " | ", ")");
596 break;
Geoff Lang6e360422015-09-02 15:54:36 -0400597
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100598 case EOpEqual:
599 writeTriplet(visit, "(", " == ", ")");
600 break;
601 case EOpNotEqual:
602 writeTriplet(visit, "(", " != ", ")");
603 break;
604 case EOpLessThan:
605 writeTriplet(visit, "(", " < ", ")");
606 break;
607 case EOpGreaterThan:
608 writeTriplet(visit, "(", " > ", ")");
609 break;
610 case EOpLessThanEqual:
611 writeTriplet(visit, "(", " <= ", ")");
612 break;
613 case EOpGreaterThanEqual:
614 writeTriplet(visit, "(", " >= ", ")");
615 break;
daniel@transgaming.com97b16d12013-02-01 03:20:42 +0000616
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100617 // Notice the fall-through.
618 case EOpVectorTimesScalar:
619 case EOpVectorTimesMatrix:
620 case EOpMatrixTimesVector:
621 case EOpMatrixTimesScalar:
622 case EOpMatrixTimesMatrix:
623 writeTriplet(visit, "(", " * ", ")");
624 break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200625
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100626 case EOpLogicalOr:
627 writeTriplet(visit, "(", " || ", ")");
628 break;
629 case EOpLogicalXor:
630 writeTriplet(visit, "(", " ^^ ", ")");
631 break;
632 case EOpLogicalAnd:
633 writeTriplet(visit, "(", " && ", ")");
634 break;
635 default:
636 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000637 }
638
639 return visitChildren;
640}
641
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700642bool TOutputGLSLBase::visitUnary(Visit visit, TIntermUnary *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000643{
zmo@google.com32e97312011-08-24 01:03:11 +0000644 TString preString;
645 TString postString = ")";
646
zmo@google.com5601ea02011-06-10 18:23:25 +0000647 switch (node->getOp())
648 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500649 case EOpNegative:
650 preString = "(-";
651 break;
652 case EOpPositive:
653 preString = "(+";
654 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500655 case EOpLogicalNot:
656 preString = "(!";
657 break;
658 case EOpBitwiseNot:
659 preString = "(~";
660 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000661
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500662 case EOpPostIncrement:
663 preString = "(";
664 postString = "++)";
665 break;
666 case EOpPostDecrement:
667 preString = "(";
668 postString = "--)";
669 break;
670 case EOpPreIncrement:
671 preString = "(++";
672 break;
673 case EOpPreDecrement:
674 preString = "(--";
675 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000676
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500677 case EOpRadians:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500678 case EOpDegrees:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500679 case EOpSin:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500680 case EOpCos:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500681 case EOpTan:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500682 case EOpAsin:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500683 case EOpAcos:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500684 case EOpAtan:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500685 case EOpSinh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500686 case EOpCosh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500687 case EOpTanh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500688 case EOpAsinh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500689 case EOpAcosh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500690 case EOpAtanh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500691 case EOpExp:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500692 case EOpLog:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500693 case EOpExp2:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500694 case EOpLog2:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500695 case EOpSqrt:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500696 case EOpInverseSqrt:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500697 case EOpAbs:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500698 case EOpSign:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500699 case EOpFloor:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500700 case EOpTrunc:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500701 case EOpRound:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500702 case EOpRoundEven:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500703 case EOpCeil:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500704 case EOpFract:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500705 case EOpIsNan:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500706 case EOpIsInf:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500707 case EOpFloatBitsToInt:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500708 case EOpFloatBitsToUint:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500709 case EOpIntBitsToFloat:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500710 case EOpUintBitsToFloat:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500711 case EOpPackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500712 case EOpPackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500713 case EOpPackHalf2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500714 case EOpUnpackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500715 case EOpUnpackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500716 case EOpUnpackHalf2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500717 case EOpLength:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500718 case EOpNormalize:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500719 case EOpDFdx:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500720 case EOpDFdy:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500721 case EOpFwidth:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500722 case EOpTranspose:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500723 case EOpDeterminant:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500724 case EOpInverse:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500725 case EOpAny:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500726 case EOpAll:
Olli Etuahod68924e2017-01-02 17:34:40 +0000727 case EOpLogicalNotComponentWise:
728 writeBuiltInFunctionTriplet(visit, node->getOp(), node->getUseEmulatedFunction());
729 return true;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500730 default:
731 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000732 }
733
zmo@google.com32e97312011-08-24 01:03:11 +0000734 writeTriplet(visit, preString.c_str(), NULL, postString.c_str());
735
zmo@google.com5601ea02011-06-10 18:23:25 +0000736 return true;
737}
738
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300739bool TOutputGLSLBase::visitTernary(Visit visit, TIntermTernary *node)
740{
741 TInfoSinkBase &out = objSink();
742 // Notice two brackets at the beginning and end. The outer ones
743 // encapsulate the whole ternary expression. This preserves the
744 // order of precedence when ternary expressions are used in a
745 // compound expression, i.e., c = 2 * (a < b ? 1 : 2).
746 out << "((";
747 node->getCondition()->traverse(this);
748 out << ") ? (";
749 node->getTrueExpression()->traverse(this);
750 out << ") : (";
751 node->getFalseExpression()->traverse(this);
752 out << "))";
753 return false;
754}
755
Olli Etuaho57961272016-09-14 13:57:46 +0300756bool TOutputGLSLBase::visitIfElse(Visit visit, TIntermIfElse *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000757{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700758 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000759
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300760 out << "if (";
761 node->getCondition()->traverse(this);
762 out << ")\n";
zmo@google.com5601ea02011-06-10 18:23:25 +0000763
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300764 incrementDepth(node);
765 visitCodeBlock(node->getTrueBlock());
zmo@google.com5601ea02011-06-10 18:23:25 +0000766
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300767 if (node->getFalseBlock())
768 {
769 out << "else\n";
770 visitCodeBlock(node->getFalseBlock());
zmo@google.com5601ea02011-06-10 18:23:25 +0000771 }
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300772 decrementDepth();
zmo@google.com5601ea02011-06-10 18:23:25 +0000773 return false;
774}
775
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200776bool TOutputGLSLBase::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200777{
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200778 if (node->getStatementList())
779 {
780 writeTriplet(visit, "switch (", ") ", nullptr);
781 // The curly braces get written when visiting the statementList aggregate
782 }
783 else
784 {
785 // No statementList, so it won't output curly braces
786 writeTriplet(visit, "switch (", ") {", "}\n");
787 }
788 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200789}
790
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200791bool TOutputGLSLBase::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200792{
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200793 if (node->hasCondition())
794 {
795 writeTriplet(visit, "case (", nullptr, "):\n");
796 return true;
797 }
798 else
799 {
800 TInfoSinkBase &out = objSink();
801 out << "default:\n";
802 return false;
803 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200804}
805
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100806bool TOutputGLSLBase::visitBlock(Visit visit, TIntermBlock *node)
807{
808 TInfoSinkBase &out = objSink();
809 // Scope the blocks except when at the global scope.
810 if (mDepth > 0)
811 {
812 out << "{\n";
813 }
814
815 incrementDepth(node);
816 for (TIntermSequence::const_iterator iter = node->getSequence()->begin();
817 iter != node->getSequence()->end(); ++iter)
818 {
819 TIntermNode *curNode = *iter;
820 ASSERT(curNode != nullptr);
821 curNode->traverse(this);
822
823 if (isSingleStatement(curNode))
824 out << ";\n";
825 }
826 decrementDepth();
827
828 // Scope the blocks except when at the global scope.
829 if (mDepth > 0)
830 {
831 out << "}\n";
832 }
833 return false;
834}
835
Olli Etuaho336b1472016-10-05 16:37:55 +0100836bool TOutputGLSLBase::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
837{
Olli Etuaho336b1472016-10-05 16:37:55 +0100838 incrementDepth(node);
Olli Etuaho8ad9e752017-01-16 19:55:20 +0000839 TIntermFunctionPrototype *prototype = node->getFunctionPrototype();
840 prototype->traverse(this);
Olli Etuaho336b1472016-10-05 16:37:55 +0100841 visitCodeBlock(node->getBody());
842 decrementDepth();
843
844 // Fully processed; no need to visit children.
845 return false;
846}
847
Olli Etuahobf4e1b72016-12-09 11:30:15 +0000848bool TOutputGLSLBase::visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node)
849{
850 TInfoSinkBase &out = objSink();
851 ASSERT(visit == PreVisit);
852 const TIntermSymbol *symbol = node->getSymbol();
853 out << "invariant " << hashVariableName(symbol->getName());
854 return false;
855}
856
Olli Etuaho16c745a2017-01-16 17:02:27 +0000857bool TOutputGLSLBase::visitFunctionPrototype(Visit visit, TIntermFunctionPrototype *node)
858{
859 TInfoSinkBase &out = objSink();
860 ASSERT(visit == PreVisit);
861
862 const TType &type = node->getType();
863 writeVariableType(type);
864 if (type.isArray())
865 out << arrayBrackets(type);
866
867 out << " " << hashFunctionNameIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
868
869 out << "(";
870 writeFunctionParameters(*(node->getSequence()));
871 out << ")";
872
873 return false;
874}
875
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700876bool TOutputGLSLBase::visitAggregate(Visit visit, TIntermAggregate *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000877{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500878 bool visitChildren = true;
879 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000880 switch (node->getOp())
881 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500882 case EOpFunctionCall:
883 // Function call.
884 if (visit == PreVisit)
885 out << hashFunctionNameIfNeeded(node->getFunctionSymbolInfo()->getNameObj()) << "(";
886 else if (visit == InVisit)
887 out << ", ";
888 else
889 out << ")";
890 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500891 case EOpConstructFloat:
892 case EOpConstructVec2:
893 case EOpConstructVec3:
894 case EOpConstructVec4:
895 case EOpConstructBool:
896 case EOpConstructBVec2:
897 case EOpConstructBVec3:
898 case EOpConstructBVec4:
899 case EOpConstructInt:
900 case EOpConstructIVec2:
901 case EOpConstructIVec3:
902 case EOpConstructIVec4:
903 case EOpConstructUInt:
904 case EOpConstructUVec2:
905 case EOpConstructUVec3:
906 case EOpConstructUVec4:
907 case EOpConstructMat2:
908 case EOpConstructMat2x3:
909 case EOpConstructMat2x4:
910 case EOpConstructMat3x2:
911 case EOpConstructMat3:
912 case EOpConstructMat3x4:
913 case EOpConstructMat4x2:
914 case EOpConstructMat4x3:
915 case EOpConstructMat4:
916 case EOpConstructStruct:
917 writeConstructorTriplet(visit, node->getType());
918 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +0200919
Olli Etuahoe1805592017-01-02 16:41:20 +0000920 case EOpEqualComponentWise:
921 case EOpNotEqualComponentWise:
922 case EOpLessThanComponentWise:
923 case EOpGreaterThanComponentWise:
924 case EOpLessThanEqualComponentWise:
925 case EOpGreaterThanEqualComponentWise:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500926 case EOpMod:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500927 case EOpModf:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500928 case EOpPow:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500929 case EOpAtan:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500930 case EOpMin:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500931 case EOpMax:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500932 case EOpClamp:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500933 case EOpMix:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500934 case EOpStep:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500935 case EOpSmoothStep:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500936 case EOpDistance:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500937 case EOpDot:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500938 case EOpCross:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500939 case EOpFaceForward:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500940 case EOpReflect:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500941 case EOpRefract:
Olli Etuahoe1805592017-01-02 16:41:20 +0000942 case EOpMulMatrixComponentWise:
943 case EOpOuterProduct:
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300944 case EOpBarrier:
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300945 case EOpMemoryBarrier:
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300946 case EOpMemoryBarrierAtomicCounter:
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300947 case EOpMemoryBarrierBuffer:
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300948 case EOpMemoryBarrierImage:
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300949 case EOpMemoryBarrierShared:
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300950 case EOpGroupMemoryBarrier:
Olli Etuahod68924e2017-01-02 17:34:40 +0000951 writeBuiltInFunctionTriplet(visit, node->getOp(), node->getUseEmulatedFunction());
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300952 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500953 default:
954 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000955 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000956 return visitChildren;
957}
958
Olli Etuaho13389b62016-10-16 11:48:18 +0100959bool TOutputGLSLBase::visitDeclaration(Visit visit, TIntermDeclaration *node)
960{
961 TInfoSinkBase &out = objSink();
962
963 // Variable declaration.
964 if (visit == PreVisit)
965 {
966 const TIntermSequence &sequence = *(node->getSequence());
967 const TIntermTyped *variable = sequence.front()->getAsTyped();
968 writeLayoutQualifier(variable->getType());
969 writeVariableType(variable->getType());
970 out << " ";
971 mDeclaringVariables = true;
972 }
973 else if (visit == InVisit)
974 {
975 out << ", ";
976 mDeclaringVariables = true;
977 }
978 else
979 {
980 mDeclaringVariables = false;
981 }
982 return true;
983}
984
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700985bool TOutputGLSLBase::visitLoop(Visit visit, TIntermLoop *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000986{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700987 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000988
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700989 incrementDepth(node);
Corentin Wallez7258e302015-09-22 10:40:24 -0700990
zmo@google.com5601ea02011-06-10 18:23:25 +0000991 TLoopType loopType = node->getType();
Corentin Wallez7258e302015-09-22 10:40:24 -0700992
zmo@google.com5601ea02011-06-10 18:23:25 +0000993 if (loopType == ELoopFor) // for loop
994 {
Corentin Wallez1b896c62016-11-16 13:10:44 -0500995 out << "for (";
996 if (node->getInit())
997 node->getInit()->traverse(this);
998 out << "; ";
zmo@google.com5601ea02011-06-10 18:23:25 +0000999
Corentin Wallez1b896c62016-11-16 13:10:44 -05001000 if (node->getCondition())
1001 node->getCondition()->traverse(this);
1002 out << "; ";
zmo@google.com5601ea02011-06-10 18:23:25 +00001003
Corentin Wallez1b896c62016-11-16 13:10:44 -05001004 if (node->getExpression())
1005 node->getExpression()->traverse(this);
1006 out << ")\n";
Corentin Wallez7258e302015-09-22 10:40:24 -07001007
Corentin Wallez1b896c62016-11-16 13:10:44 -05001008 visitCodeBlock(node->getBody());
zmo@google.com5601ea02011-06-10 18:23:25 +00001009 }
1010 else if (loopType == ELoopWhile) // while loop
1011 {
1012 out << "while (";
1013 ASSERT(node->getCondition() != NULL);
1014 node->getCondition()->traverse(this);
1015 out << ")\n";
Corentin Wallez7258e302015-09-22 10:40:24 -07001016
1017 visitCodeBlock(node->getBody());
zmo@google.com5601ea02011-06-10 18:23:25 +00001018 }
1019 else // do-while loop
1020 {
1021 ASSERT(loopType == ELoopDoWhile);
1022 out << "do\n";
zmo@google.com5601ea02011-06-10 18:23:25 +00001023
zmo@google.com5601ea02011-06-10 18:23:25 +00001024 visitCodeBlock(node->getBody());
zmo@google.com5601ea02011-06-10 18:23:25 +00001025
zmo@google.com5601ea02011-06-10 18:23:25 +00001026 out << "while (";
1027 ASSERT(node->getCondition() != NULL);
1028 node->getCondition()->traverse(this);
1029 out << ");\n";
1030 }
Corentin Wallez7258e302015-09-22 10:40:24 -07001031
zmo@google.com5601ea02011-06-10 18:23:25 +00001032 decrementDepth();
1033
1034 // No need to visit children. They have been already processed in
1035 // this function.
1036 return false;
1037}
1038
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001039bool TOutputGLSLBase::visitBranch(Visit visit, TIntermBranch *node)
zmo@google.com5601ea02011-06-10 18:23:25 +00001040{
1041 switch (node->getFlowOp())
1042 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001043 case EOpKill:
1044 writeTriplet(visit, "discard", NULL, NULL);
1045 break;
1046 case EOpBreak:
1047 writeTriplet(visit, "break", NULL, NULL);
1048 break;
1049 case EOpContinue:
1050 writeTriplet(visit, "continue", NULL, NULL);
1051 break;
1052 case EOpReturn:
1053 writeTriplet(visit, "return ", NULL, NULL);
1054 break;
1055 default:
1056 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +00001057 }
1058
1059 return true;
1060}
1061
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001062void TOutputGLSLBase::visitCodeBlock(TIntermBlock *node)
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001063{
zmo@google.com5601ea02011-06-10 18:23:25 +00001064 TInfoSinkBase &out = objSink();
1065 if (node != NULL)
1066 {
1067 node->traverse(this);
1068 // Single statements not part of a sequence need to be terminated
1069 // with semi-colon.
1070 if (isSingleStatement(node))
1071 out << ";\n";
1072 }
1073 else
1074 {
1075 out << "{\n}\n"; // Empty code block.
1076 }
1077}
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001078
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001079TString TOutputGLSLBase::getTypeName(const TType &type)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001080{
Olli Etuahoe92507b2016-07-04 11:20:10 +03001081 if (type.getBasicType() == EbtStruct)
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001082 return hashName(TName(type.getStruct()->name()));
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001083 else
Olli Etuahoe92507b2016-07-04 11:20:10 +03001084 return type.getBuiltInTypeNameString();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001085}
1086
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001087TString TOutputGLSLBase::hashName(const TName &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001088{
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001089 if (name.getString().empty())
1090 {
1091 ASSERT(!name.isInternal());
1092 return name.getString();
1093 }
1094 if (name.isInternal())
1095 {
1096 // TODO(oetuaho): Would be nicer to prefix non-internal names with "_" instead, like is
1097 // done in the HLSL output, but that requires fairly complex changes elsewhere in the code
1098 // as well.
1099 // We need to use a prefix that is reserved in WebGL in order to guarantee that the internal
1100 // names don't conflict with user-defined names from WebGL.
1101 return "webgl_angle_" + name.getString();
1102 }
1103 if (mHashFunction == nullptr)
1104 {
1105 return name.getString();
1106 }
1107 NameMap::const_iterator it = mNameMap.find(name.getString().c_str());
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001108 if (it != mNameMap.end())
1109 return it->second.c_str();
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001110 TString hashedName = TIntermTraverser::hash(name.getString(), mHashFunction);
1111 mNameMap[name.getString().c_str()] = hashedName.c_str();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001112 return hashedName;
1113}
1114
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001115TString TOutputGLSLBase::hashVariableName(const TName &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001116{
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001117 if (mSymbolTable.findBuiltIn(name.getString(), mShaderVersion) != NULL)
Olli Etuaho09b04a22016-12-15 13:30:26 +00001118 {
1119 if (mCompileOptions & SH_TRANSLATE_VIEWID_OVR_TO_UNIFORM &&
1120 name.getString() == "gl_ViewID_OVR")
1121 {
Olli Etuaho2f90a9b2017-01-10 12:27:56 +00001122 TName uniformName(TString("ViewID_OVR"));
Olli Etuaho09b04a22016-12-15 13:30:26 +00001123 uniformName.setInternal(true);
1124 return hashName(uniformName);
1125 }
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001126 return name.getString();
Olli Etuaho09b04a22016-12-15 13:30:26 +00001127 }
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001128 return hashName(name);
1129}
1130
Olli Etuaho59f9a642015-08-06 20:38:26 +03001131TString TOutputGLSLBase::hashFunctionNameIfNeeded(const TName &mangledName)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001132{
Olli Etuaho59f9a642015-08-06 20:38:26 +03001133 TString mangledStr = mangledName.getString();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001134 TString name = TFunction::unmangleName(mangledStr);
Olli Etuaho59f9a642015-08-06 20:38:26 +03001135 if (mSymbolTable.findBuiltIn(mangledStr, mShaderVersion) != nullptr || name == "main")
Nicolas Capens46485082014-04-15 13:12:50 -04001136 return translateTextureFunction(name);
Olli Etuaho59f9a642015-08-06 20:38:26 +03001137 if (mangledName.isInternal())
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001138 {
1139 // Internal function names are outputted as-is - they may refer to functions manually added
1140 // to the output shader source that are not included in the AST at all.
Olli Etuaho59f9a642015-08-06 20:38:26 +03001141 return name;
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001142 }
Olli Etuaho59f9a642015-08-06 20:38:26 +03001143 else
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001144 {
1145 TName nameObj(name);
1146 return hashName(nameObj);
1147 }
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001148}
Jamie Madill98493dd2013-07-08 14:39:03 -04001149
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001150bool TOutputGLSLBase::structDeclared(const TStructure *structure) const
Jamie Madill98493dd2013-07-08 14:39:03 -04001151{
Zhenyao Mo904a9162014-05-09 14:07:45 -07001152 ASSERT(structure);
Jamie Madill01f85ac2014-06-06 11:55:04 -04001153 if (structure->name().empty())
Zhenyao Mo904a9162014-05-09 14:07:45 -07001154 {
Jamie Madill01f85ac2014-06-06 11:55:04 -04001155 return false;
Zhenyao Mo904a9162014-05-09 14:07:45 -07001156 }
Jamie Madill01f85ac2014-06-06 11:55:04 -04001157
1158 return (mDeclaredStructs.count(structure->uniqueId()) > 0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001159}
1160
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001161void TOutputGLSLBase::declareStruct(const TStructure *structure)
Jamie Madill98493dd2013-07-08 14:39:03 -04001162{
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001163 TInfoSinkBase &out = objSink();
Jamie Madill98493dd2013-07-08 14:39:03 -04001164
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001165 out << "struct " << hashName(TName(structure->name())) << "{\n";
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001166 const TFieldList &fields = structure->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04001167 for (size_t i = 0; i < fields.size(); ++i)
1168 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001169 const TField *field = fields[i];
Jamie Madill98493dd2013-07-08 14:39:03 -04001170 if (writeVariablePrecision(field->type()->getPrecision()))
1171 out << " ";
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001172 out << getTypeName(*field->type()) << " " << hashName(TName(field->name()));
Jamie Madill98493dd2013-07-08 14:39:03 -04001173 if (field->type()->isArray())
1174 out << arrayBrackets(*field->type());
1175 out << ";\n";
1176 }
1177 out << "}";
Zhenyao Mo904a9162014-05-09 14:07:45 -07001178}
Jamie Madill98493dd2013-07-08 14:39:03 -04001179
Geoff Langbdcc54a2015-09-02 13:09:48 -04001180void TOutputGLSLBase::declareInterfaceBlockLayout(const TInterfaceBlock *interfaceBlock)
1181{
1182 TInfoSinkBase &out = objSink();
1183
1184 out << "layout(";
1185
1186 switch (interfaceBlock->blockStorage())
1187 {
1188 case EbsUnspecified:
1189 case EbsShared:
1190 // Default block storage is shared.
1191 out << "shared";
1192 break;
1193
1194 case EbsPacked:
1195 out << "packed";
1196 break;
1197
1198 case EbsStd140:
1199 out << "std140";
1200 break;
1201
1202 default:
1203 UNREACHABLE();
1204 break;
1205 }
1206
1207 out << ", ";
1208
1209 switch (interfaceBlock->matrixPacking())
1210 {
1211 case EmpUnspecified:
1212 case EmpColumnMajor:
1213 // Default matrix packing is column major.
1214 out << "column_major";
1215 break;
1216
1217 case EmpRowMajor:
1218 out << "row_major";
1219 break;
1220
1221 default:
1222 UNREACHABLE();
1223 break;
1224 }
1225
1226 out << ") ";
1227}
1228
1229void TOutputGLSLBase::declareInterfaceBlock(const TInterfaceBlock *interfaceBlock)
1230{
1231 TInfoSinkBase &out = objSink();
1232
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001233 out << hashName(TName(interfaceBlock->name())) << "{\n";
Geoff Langbdcc54a2015-09-02 13:09:48 -04001234 const TFieldList &fields = interfaceBlock->fields();
1235 for (size_t i = 0; i < fields.size(); ++i)
1236 {
1237 const TField *field = fields[i];
1238 if (writeVariablePrecision(field->type()->getPrecision()))
1239 out << " ";
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001240 out << getTypeName(*field->type()) << " " << hashName(TName(field->name()));
Geoff Langbdcc54a2015-09-02 13:09:48 -04001241 if (field->type()->isArray())
1242 out << arrayBrackets(*field->type());
1243 out << ";\n";
1244 }
1245 out << "}";
1246}
Jamie Madill45bcc782016-11-07 13:58:48 -05001247
1248} // namespace sh