blob: b7aaceed889284946029a3e38319ea14c4d21381 [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
Shaob5cc1192017-07-06 10:47:20 +08009#include "angle_gl.h"
Olli Etuahod57e0db2015-04-24 15:05:08 +030010#include "common/debug.h"
Olli Etuaho56a2f952016-12-08 12:16:27 +000011#include "common/mathutil.h"
Olli Etuahocccf2b02017-07-05 14:50:54 +030012#include "compiler/translator/Compiler.h"
zmo@google.com5601ea02011-06-10 18:23:25 +000013
daniel@transgaming.com773ff742013-01-11 04:12:51 +000014#include <cfloat>
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +000015
Jamie Madill45bcc782016-11-07 13:58:48 -050016namespace sh
17{
18
zmo@google.com5601ea02011-06-10 18:23:25 +000019namespace
20{
Zhenyao Mo9eedea02014-05-12 16:02:35 -070021TString arrayBrackets(const TType &type)
zmo@google.com5601ea02011-06-10 18:23:25 +000022{
23 ASSERT(type.isArray());
24 TInfoSinkBase out;
25 out << "[" << type.getArraySize() << "]";
26 return TString(out.c_str());
27}
28
Zhenyao Mo9eedea02014-05-12 16:02:35 -070029bool isSingleStatement(TIntermNode *node)
30{
Olli Etuaho336b1472016-10-05 16:37:55 +010031 if (node->getAsFunctionDefinition())
zmo@google.com5601ea02011-06-10 18:23:25 +000032 {
Olli Etuaho336b1472016-10-05 16:37:55 +010033 return false;
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010034 }
35 else if (node->getAsBlock())
36 {
37 return false;
zmo@google.com5601ea02011-06-10 18:23:25 +000038 }
Olli Etuaho57961272016-09-14 13:57:46 +030039 else if (node->getAsIfElseNode())
zmo@google.com5601ea02011-06-10 18:23:25 +000040 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +030041 return false;
zmo@google.com5601ea02011-06-10 18:23:25 +000042 }
43 else if (node->getAsLoopNode())
44 {
45 return false;
46 }
Olli Etuaho01cd8af2015-02-20 10:39:20 +020047 else if (node->getAsSwitchNode())
48 {
49 return false;
50 }
51 else if (node->getAsCaseNode())
52 {
53 return false;
54 }
zmo@google.com5601ea02011-06-10 18:23:25 +000055 return true;
56}
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080057
Martin Radev2cc85b32016-08-05 16:22:53 +030058// If SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS is enabled, layout qualifiers are spilled whenever
59// variables with specified layout qualifiers are copied. Additional checks are needed against the
60// type and storage qualifier of the variable to verify that layout qualifiers have to be outputted.
61// TODO (mradev): Fix layout qualifier spilling in ScalarizeVecAndMatConstructorArgs and remove
62// NeedsToWriteLayoutQualifier.
63bool NeedsToWriteLayoutQualifier(const TType &type)
64{
65 if (type.getBasicType() == EbtInterfaceBlock)
66 {
67 return false;
68 }
69
70 const TLayoutQualifier &layoutQualifier = type.getLayoutQualifier();
71
72 if ((type.getQualifier() == EvqFragmentOut || type.getQualifier() == EvqVertexIn) &&
73 layoutQualifier.location >= 0)
74 {
75 return true;
76 }
Olli Etuaho43364892017-02-13 16:00:12 +000077
Andrei Volykhina5527072017-03-22 16:46:30 +030078 if (type.getQualifier() == EvqFragmentOut && layoutQualifier.yuv == true)
79 {
80 return true;
81 }
82
Olli Etuaho43364892017-02-13 16:00:12 +000083 if (IsOpaqueType(type.getBasicType()) && layoutQualifier.binding != -1)
84 {
85 return true;
86 }
87
Martin Radev2cc85b32016-08-05 16:22:53 +030088 if (IsImage(type.getBasicType()) && layoutQualifier.imageInternalFormat != EiifUnspecified)
89 {
90 return true;
91 }
92 return false;
93}
94
Olli Etuaho43364892017-02-13 16:00:12 +000095class CommaSeparatedListItemPrefixGenerator
96{
97 public:
98 CommaSeparatedListItemPrefixGenerator() : mFirst(true) {}
99 private:
100 bool mFirst;
101
102 friend TInfoSinkBase &operator<<(TInfoSinkBase &out,
103 CommaSeparatedListItemPrefixGenerator &gen);
104};
105
106TInfoSinkBase &operator<<(TInfoSinkBase &out, CommaSeparatedListItemPrefixGenerator &gen)
107{
108 if (gen.mFirst)
109 {
110 gen.mFirst = false;
111 }
112 else
113 {
114 out << ", ";
115 }
116 return out;
117}
118
zmo@google.com5601ea02011-06-10 18:23:25 +0000119} // namespace
120
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700121TOutputGLSLBase::TOutputGLSLBase(TInfoSinkBase &objSink,
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000122 ShArrayIndexClampingStrategy clampingStrategy,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000123 ShHashFunction64 hashFunction,
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700124 NameMap &nameMap,
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300125 TSymbolTable *symbolTable,
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000126 sh::GLenum shaderType,
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800127 int shaderVersion,
Qiankun Miao705a9192016-08-29 10:05:27 +0800128 ShShaderOutput output,
129 ShCompileOptions compileOptions)
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300130 : TIntermTraverser(true, true, true, symbolTable),
zmo@google.com5601ea02011-06-10 18:23:25 +0000131 mObjSink(objSink),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000132 mDeclaringVariables(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000133 mClampingStrategy(clampingStrategy),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000134 mHashFunction(hashFunction),
135 mNameMap(nameMap),
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000136 mShaderType(shaderType),
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800137 mShaderVersion(shaderVersion),
Qiankun Miao705a9192016-08-29 10:05:27 +0800138 mOutput(output),
139 mCompileOptions(compileOptions)
zmo@google.com5601ea02011-06-10 18:23:25 +0000140{
141}
142
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800143void TOutputGLSLBase::writeInvariantQualifier(const TType &type)
144{
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000145 if (!sh::RemoveInvariant(mShaderType, mShaderVersion, mOutput, mCompileOptions))
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800146 {
147 TInfoSinkBase &out = objSink();
148 out << "invariant ";
149 }
150}
151
Olli Etuaho56a2f952016-12-08 12:16:27 +0000152void TOutputGLSLBase::writeFloat(TInfoSinkBase &out, float f)
153{
154 if ((gl::isInf(f) || gl::isNaN(f)) && mShaderVersion >= 300)
155 {
156 out << "uintBitsToFloat(" << gl::bitCast<uint32_t>(f) << "u)";
157 }
158 else
159 {
160 out << std::min(FLT_MAX, std::max(-FLT_MAX, f));
161 }
162}
163
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500164void TOutputGLSLBase::writeTriplet(Visit visit,
165 const char *preStr,
166 const char *inStr,
167 const char *postStr)
zmo@google.com5601ea02011-06-10 18:23:25 +0000168{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700169 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000170 if (visit == PreVisit && preStr)
zmo@google.com5601ea02011-06-10 18:23:25 +0000171 out << preStr;
zmo@google.com5601ea02011-06-10 18:23:25 +0000172 else if (visit == InVisit && inStr)
zmo@google.com5601ea02011-06-10 18:23:25 +0000173 out << inStr;
zmo@google.com5601ea02011-06-10 18:23:25 +0000174 else if (visit == PostVisit && postStr)
zmo@google.com5601ea02011-06-10 18:23:25 +0000175 out << postStr;
zmo@google.com5601ea02011-06-10 18:23:25 +0000176}
177
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500178void TOutputGLSLBase::writeBuiltInFunctionTriplet(Visit visit,
Olli Etuahoe1805592017-01-02 16:41:20 +0000179 TOperator op,
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500180 bool useEmulatedFunction)
zmo@google.com5601ea02011-06-10 18:23:25 +0000181{
Olli Etuahod68924e2017-01-02 17:34:40 +0000182 TInfoSinkBase &out = objSink();
183 if (visit == PreVisit)
Olli Etuahoe1805592017-01-02 16:41:20 +0000184 {
Olli Etuahod68924e2017-01-02 17:34:40 +0000185 const char *opStr(GetOperatorString(op));
186 if (useEmulatedFunction)
187 {
188 BuiltInFunctionEmulator::WriteEmulatedFunctionName(out, opStr);
189 }
190 else
191 {
192 out << opStr;
193 }
194 out << "(";
Olli Etuahoe1805592017-01-02 16:41:20 +0000195 }
Olli Etuahod68924e2017-01-02 17:34:40 +0000196 else
197 {
198 writeTriplet(visit, nullptr, ", ", ")");
199 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700200}
201
Olli Etuahoae69d7e2015-10-07 17:19:50 +0300202void TOutputGLSLBase::writeLayoutQualifier(const TType &type)
203{
Martin Radev2cc85b32016-08-05 16:22:53 +0300204 if (!NeedsToWriteLayoutQualifier(type))
205 {
206 return;
207 }
208
209 TInfoSinkBase &out = objSink();
210 const TLayoutQualifier &layoutQualifier = type.getLayoutQualifier();
211 out << "layout(";
212
Olli Etuaho43364892017-02-13 16:00:12 +0000213 CommaSeparatedListItemPrefixGenerator listItemPrefix;
214
Olli Etuahoae69d7e2015-10-07 17:19:50 +0300215 if (type.getQualifier() == EvqFragmentOut || type.getQualifier() == EvqVertexIn)
216 {
Olli Etuahoae69d7e2015-10-07 17:19:50 +0300217 if (layoutQualifier.location >= 0)
218 {
Olli Etuaho43364892017-02-13 16:00:12 +0000219 out << listItemPrefix << "location = " << layoutQualifier.location;
Olli Etuahoae69d7e2015-10-07 17:19:50 +0300220 }
221 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300222
Andrei Volykhina5527072017-03-22 16:46:30 +0300223 if (type.getQualifier() == EvqFragmentOut)
224 {
225 if (layoutQualifier.yuv == true)
226 {
227 out << listItemPrefix << "yuv";
228 }
229 }
230
Olli Etuaho43364892017-02-13 16:00:12 +0000231 if (IsOpaqueType(type.getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +0300232 {
Olli Etuaho43364892017-02-13 16:00:12 +0000233 if (layoutQualifier.binding >= 0)
234 {
235 out << listItemPrefix << "binding = " << layoutQualifier.binding;
236 }
237 }
238
239 if (IsImage(type.getBasicType()))
240 {
241 if (layoutQualifier.imageInternalFormat != EiifUnspecified)
242 {
243 ASSERT(type.getQualifier() == EvqTemporary || type.getQualifier() == EvqUniform);
244 out << listItemPrefix
245 << getImageInternalFormatString(layoutQualifier.imageInternalFormat);
246 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300247 }
248
jchen1005c31da2017-07-18 16:11:39 +0800249 if (IsAtomicCounter(type.getBasicType()))
250 {
251 out << listItemPrefix << "offset = " << layoutQualifier.offset;
252 }
253
Martin Radev2cc85b32016-08-05 16:22:53 +0300254 out << ") ";
Olli Etuahoae69d7e2015-10-07 17:19:50 +0300255}
256
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800257const char *TOutputGLSLBase::mapQualifierToString(TQualifier qualifier)
258{
259 if (sh::IsGLSL410OrOlder(mOutput) && mShaderVersion >= 300 &&
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000260 (mCompileOptions & SH_REMOVE_INVARIANT_AND_CENTROID_FOR_ESSL3) != 0)
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800261 {
262 switch (qualifier)
263 {
264 // The return string is consistent with sh::getQualifierString() from
265 // BaseTypes.h minus the "centroid" keyword.
266 case EvqCentroid:
267 return "";
268 case EvqCentroidIn:
269 return "smooth in";
270 case EvqCentroidOut:
271 return "smooth out";
272 default:
273 break;
274 }
275 }
276 if (sh::IsGLSL130OrNewer(mOutput))
277 {
278 switch (qualifier)
279 {
280 case EvqAttribute:
281 return "in";
282 case EvqVaryingIn:
283 return "in";
284 case EvqVaryingOut:
285 return "out";
286 default:
287 break;
288 }
289 }
290 return sh::getQualifierString(qualifier);
291}
292
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700293void TOutputGLSLBase::writeVariableType(const TType &type)
294{
Qiankun Miao705a9192016-08-29 10:05:27 +0800295 TQualifier qualifier = type.getQualifier();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500296 TInfoSinkBase &out = objSink();
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800297 if (type.isInvariant())
Olli Etuaho214c2d82015-04-27 14:49:13 +0300298 {
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800299 writeInvariantQualifier(type);
Olli Etuaho214c2d82015-04-27 14:49:13 +0300300 }
Geoff Langbdcc54a2015-09-02 13:09:48 -0400301 if (type.getBasicType() == EbtInterfaceBlock)
302 {
303 TInterfaceBlock *interfaceBlock = type.getInterfaceBlock();
304 declareInterfaceBlockLayout(interfaceBlock);
305 }
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400306 if (qualifier != EvqTemporary && qualifier != EvqGlobal)
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400307 {
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800308 const char *qualifierString = mapQualifierToString(qualifier);
309 if (qualifierString && qualifierString[0] != '\0')
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800310 {
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800311 out << qualifierString << " ";
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800312 }
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400313 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300314
315 const TMemoryQualifier &memoryQualifier = type.getMemoryQualifier();
316 if (memoryQualifier.readonly)
317 {
318 ASSERT(IsImage(type.getBasicType()));
319 out << "readonly ";
320 }
321
322 if (memoryQualifier.writeonly)
323 {
324 ASSERT(IsImage(type.getBasicType()));
325 out << "writeonly ";
326 }
327
Martin Radev049edfa2016-11-11 14:35:37 +0200328 if (memoryQualifier.coherent)
329 {
330 ASSERT(IsImage(type.getBasicType()));
331 out << "coherent ";
332 }
333
334 if (memoryQualifier.restrictQualifier)
335 {
336 ASSERT(IsImage(type.getBasicType()));
337 out << "restrict ";
338 }
339
340 if (memoryQualifier.volatileQualifier)
341 {
342 ASSERT(IsImage(type.getBasicType()));
343 out << "volatile ";
344 }
345
zmo@google.com5601ea02011-06-10 18:23:25 +0000346 // Declare the struct if we have not done so already.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700347 if (type.getBasicType() == EbtStruct && !structDeclared(type.getStruct()))
zmo@google.com5601ea02011-06-10 18:23:25 +0000348 {
Jamie Madill01f85ac2014-06-06 11:55:04 -0400349 TStructure *structure = type.getStruct();
350
351 declareStruct(structure);
352
353 if (!structure->name().empty())
354 {
355 mDeclaredStructs.insert(structure->uniqueId());
356 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000357 }
Geoff Langbdcc54a2015-09-02 13:09:48 -0400358 else if (type.getBasicType() == EbtInterfaceBlock)
359 {
360 TInterfaceBlock *interfaceBlock = type.getInterfaceBlock();
361 declareInterfaceBlock(interfaceBlock);
362 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000363 else
364 {
365 if (writeVariablePrecision(type.getPrecision()))
366 out << " ";
367 out << getTypeName(type);
368 }
369}
370
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700371void TOutputGLSLBase::writeFunctionParameters(const TIntermSequence &args)
zmo@google.com5601ea02011-06-10 18:23:25 +0000372{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700373 TInfoSinkBase &out = objSink();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500374 for (TIntermSequence::const_iterator iter = args.begin(); iter != args.end(); ++iter)
zmo@google.com5601ea02011-06-10 18:23:25 +0000375 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700376 const TIntermSymbol *arg = (*iter)->getAsSymbolNode();
Yunchao He4f285442017-04-21 12:15:49 +0800377 ASSERT(arg != nullptr);
zmo@google.com5601ea02011-06-10 18:23:25 +0000378
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700379 const TType &type = arg->getType();
zmo@google.com189be2f2011-06-16 18:28:53 +0000380 writeVariableType(type);
zmo@google.com5601ea02011-06-10 18:23:25 +0000381
Olli Etuaho0982a2b2016-11-01 15:13:46 +0000382 if (!arg->getName().getString().empty())
383 out << " " << hashName(arg->getName());
zmo@google.com5601ea02011-06-10 18:23:25 +0000384 if (type.isArray())
385 out << arrayBrackets(type);
386
387 // Put a comma if this is not the last argument.
388 if (iter != args.end() - 1)
389 out << ", ";
390 }
391}
392
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500393const TConstantUnion *TOutputGLSLBase::writeConstantUnion(const TType &type,
394 const TConstantUnion *pConstUnion)
zmo@google.com5601ea02011-06-10 18:23:25 +0000395{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700396 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000397
398 if (type.getBasicType() == EbtStruct)
399 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700400 const TStructure *structure = type.getStruct();
Olli Etuaho0982a2b2016-11-01 15:13:46 +0000401 out << hashName(TName(structure->name())) << "(";
Jamie Madill98493dd2013-07-08 14:39:03 -0400402
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700403 const TFieldList &fields = structure->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -0400404 for (size_t i = 0; i < fields.size(); ++i)
zmo@google.com5601ea02011-06-10 18:23:25 +0000405 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700406 const TType *fieldType = fields[i]->type();
Yunchao He4f285442017-04-21 12:15:49 +0800407 ASSERT(fieldType != nullptr);
zmo@google.com5601ea02011-06-10 18:23:25 +0000408 pConstUnion = writeConstantUnion(*fieldType, pConstUnion);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700409 if (i != fields.size() - 1)
410 out << ", ";
zmo@google.com5601ea02011-06-10 18:23:25 +0000411 }
412 out << ")";
413 }
414 else
415 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500416 size_t size = type.getObjectSize();
zmo@google.com5601ea02011-06-10 18:23:25 +0000417 bool writeType = size > 1;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700418 if (writeType)
419 out << getTypeName(type) << "(";
Jamie Madill94bf7f22013-07-08 13:31:15 -0400420 for (size_t i = 0; i < size; ++i, ++pConstUnion)
zmo@google.com5601ea02011-06-10 18:23:25 +0000421 {
422 switch (pConstUnion->getType())
423 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500424 case EbtFloat:
425 writeFloat(out, pConstUnion->getFConst());
426 break;
427 case EbtInt:
428 out << pConstUnion->getIConst();
429 break;
430 case EbtUInt:
431 out << pConstUnion->getUConst() << "u";
432 break;
433 case EbtBool:
434 out << pConstUnion->getBConst();
435 break;
Andrei Volykhina5527072017-03-22 16:46:30 +0300436 case EbtYuvCscStandardEXT:
437 out << getYuvCscStandardEXTString(pConstUnion->getYuvCscStandardEXTConst());
438 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500439 default:
440 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000441 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700442 if (i != size - 1)
443 out << ", ";
zmo@google.com5601ea02011-06-10 18:23:25 +0000444 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700445 if (writeType)
446 out << ")";
zmo@google.com5601ea02011-06-10 18:23:25 +0000447 }
448 return pConstUnion;
449}
450
Olli Etuahoe92507b2016-07-04 11:20:10 +0300451void TOutputGLSLBase::writeConstructorTriplet(Visit visit, const TType &type)
Olli Etuahof40319e2015-03-10 14:33:00 +0200452{
453 TInfoSinkBase &out = objSink();
454 if (visit == PreVisit)
455 {
456 if (type.isArray())
457 {
Olli Etuahoe92507b2016-07-04 11:20:10 +0300458 out << getTypeName(type);
Olli Etuahof40319e2015-03-10 14:33:00 +0200459 out << arrayBrackets(type);
460 out << "(";
461 }
462 else
463 {
Olli Etuahoe92507b2016-07-04 11:20:10 +0300464 out << getTypeName(type) << "(";
Olli Etuahof40319e2015-03-10 14:33:00 +0200465 }
466 }
467 else
468 {
469 writeTriplet(visit, nullptr, ", ", ")");
470 }
471}
472
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700473void TOutputGLSLBase::visitSymbol(TIntermSymbol *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000474{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700475 TInfoSinkBase &out = objSink();
Corentin Wallez1b896c62016-11-16 13:10:44 -0500476 out << hashVariableName(node->getName());
zmo@google.com5601ea02011-06-10 18:23:25 +0000477
478 if (mDeclaringVariables && node->getType().isArray())
479 out << arrayBrackets(node->getType());
480}
481
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700482void TOutputGLSLBase::visitConstantUnion(TIntermConstantUnion *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000483{
484 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
485}
486
Olli Etuahob6fa0432016-09-28 16:28:05 +0100487bool TOutputGLSLBase::visitSwizzle(Visit visit, TIntermSwizzle *node)
488{
489 TInfoSinkBase &out = objSink();
490 if (visit == PostVisit)
491 {
492 out << ".";
493 node->writeOffsetsAsXYZW(&out);
494 }
495 return true;
496}
497
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700498bool TOutputGLSLBase::visitBinary(Visit visit, TIntermBinary *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000499{
500 bool visitChildren = true;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700501 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000502 switch (node->getOp())
503 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100504 case EOpComma:
505 writeTriplet(visit, "(", ", ", ")");
506 break;
507 case EOpInitialize:
zmo@google.com5601ea02011-06-10 18:23:25 +0000508 if (visit == InVisit)
509 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100510 out << " = ";
511 // RHS of initialize is not being declared.
512 mDeclaringVariables = false;
zmo@google.com5601ea02011-06-10 18:23:25 +0000513 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100514 break;
515 case EOpAssign:
516 writeTriplet(visit, "(", " = ", ")");
517 break;
518 case EOpAddAssign:
519 writeTriplet(visit, "(", " += ", ")");
520 break;
521 case EOpSubAssign:
522 writeTriplet(visit, "(", " -= ", ")");
523 break;
524 case EOpDivAssign:
525 writeTriplet(visit, "(", " /= ", ")");
526 break;
527 case EOpIModAssign:
528 writeTriplet(visit, "(", " %= ", ")");
529 break;
530 // Notice the fall-through.
531 case EOpMulAssign:
532 case EOpVectorTimesMatrixAssign:
533 case EOpVectorTimesScalarAssign:
534 case EOpMatrixTimesScalarAssign:
535 case EOpMatrixTimesMatrixAssign:
536 writeTriplet(visit, "(", " *= ", ")");
537 break;
538 case EOpBitShiftLeftAssign:
539 writeTriplet(visit, "(", " <<= ", ")");
540 break;
541 case EOpBitShiftRightAssign:
542 writeTriplet(visit, "(", " >>= ", ")");
543 break;
544 case EOpBitwiseAndAssign:
545 writeTriplet(visit, "(", " &= ", ")");
546 break;
547 case EOpBitwiseXorAssign:
548 writeTriplet(visit, "(", " ^= ", ")");
549 break;
550 case EOpBitwiseOrAssign:
551 writeTriplet(visit, "(", " |= ", ")");
552 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000553
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100554 case EOpIndexDirect:
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800555 writeTriplet(visit, nullptr, "[", "]");
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100556 break;
557 case EOpIndexIndirect:
558 if (node->getAddIndexClamp())
559 {
560 if (visit == InVisit)
561 {
562 if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC)
563 out << "[int(clamp(float(";
564 else
565 out << "[webgl_int_clamp(";
566 }
567 else if (visit == PostVisit)
568 {
569 int maxSize;
570 TIntermTyped *left = node->getLeft();
571 TType leftType = left->getType();
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700572
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100573 if (left->isArray())
574 {
575 // The shader will fail validation if the array length is not > 0.
576 maxSize = static_cast<int>(leftType.getArraySize()) - 1;
577 }
578 else
579 {
580 maxSize = leftType.getNominalSize() - 1;
581 }
582
583 if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC)
584 out << "), 0.0, float(" << maxSize << ")))]";
585 else
586 out << ", 0, " << maxSize << ")]";
587 }
588 }
589 else
590 {
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800591 writeTriplet(visit, nullptr, "[", "]");
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100592 }
593 break;
594 case EOpIndexDirectStruct:
595 if (visit == InVisit)
596 {
597 // Here we are writing out "foo.bar", where "foo" is struct
598 // and "bar" is field. In AST, it is represented as a binary
599 // node, where left child represents "foo" and right child "bar".
600 // The node itself represents ".". The struct field "bar" is
601 // actually stored as an index into TStructure::fields.
602 out << ".";
603 const TStructure *structure = node->getLeft()->getType().getStruct();
604 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
605 const TField *field = structure->fields()[index->getIConst(0)];
606
607 TString fieldName = field->name();
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300608 if (!mSymbolTable->findBuiltIn(structure->name(), mShaderVersion))
Olli Etuaho0982a2b2016-11-01 15:13:46 +0000609 fieldName = hashName(TName(fieldName));
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100610
611 out << fieldName;
612 visitChildren = false;
613 }
614 break;
615 case EOpIndexDirectInterfaceBlock:
616 if (visit == InVisit)
617 {
618 out << ".";
619 const TInterfaceBlock *interfaceBlock =
620 node->getLeft()->getType().getInterfaceBlock();
621 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
622 const TField *field = interfaceBlock->fields()[index->getIConst(0)];
623
624 TString fieldName = field->name();
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300625 ASSERT(!mSymbolTable->findBuiltIn(interfaceBlock->name(), mShaderVersion));
Olli Etuaho0982a2b2016-11-01 15:13:46 +0000626 fieldName = hashName(TName(fieldName));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700627
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100628 out << fieldName;
629 visitChildren = false;
630 }
631 break;
Geoff Lang6e360422015-09-02 15:54:36 -0400632
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100633 case EOpAdd:
634 writeTriplet(visit, "(", " + ", ")");
635 break;
636 case EOpSub:
637 writeTriplet(visit, "(", " - ", ")");
638 break;
639 case EOpMul:
640 writeTriplet(visit, "(", " * ", ")");
641 break;
642 case EOpDiv:
643 writeTriplet(visit, "(", " / ", ")");
644 break;
645 case EOpIMod:
646 writeTriplet(visit, "(", " % ", ")");
647 break;
648 case EOpBitShiftLeft:
649 writeTriplet(visit, "(", " << ", ")");
650 break;
651 case EOpBitShiftRight:
652 writeTriplet(visit, "(", " >> ", ")");
653 break;
654 case EOpBitwiseAnd:
655 writeTriplet(visit, "(", " & ", ")");
656 break;
657 case EOpBitwiseXor:
658 writeTriplet(visit, "(", " ^ ", ")");
659 break;
660 case EOpBitwiseOr:
661 writeTriplet(visit, "(", " | ", ")");
662 break;
Geoff Lang6e360422015-09-02 15:54:36 -0400663
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100664 case EOpEqual:
665 writeTriplet(visit, "(", " == ", ")");
666 break;
667 case EOpNotEqual:
668 writeTriplet(visit, "(", " != ", ")");
669 break;
670 case EOpLessThan:
671 writeTriplet(visit, "(", " < ", ")");
672 break;
673 case EOpGreaterThan:
674 writeTriplet(visit, "(", " > ", ")");
675 break;
676 case EOpLessThanEqual:
677 writeTriplet(visit, "(", " <= ", ")");
678 break;
679 case EOpGreaterThanEqual:
680 writeTriplet(visit, "(", " >= ", ")");
681 break;
daniel@transgaming.com97b16d12013-02-01 03:20:42 +0000682
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100683 // Notice the fall-through.
684 case EOpVectorTimesScalar:
685 case EOpVectorTimesMatrix:
686 case EOpMatrixTimesVector:
687 case EOpMatrixTimesScalar:
688 case EOpMatrixTimesMatrix:
689 writeTriplet(visit, "(", " * ", ")");
690 break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200691
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100692 case EOpLogicalOr:
693 writeTriplet(visit, "(", " || ", ")");
694 break;
695 case EOpLogicalXor:
696 writeTriplet(visit, "(", " ^^ ", ")");
697 break;
698 case EOpLogicalAnd:
699 writeTriplet(visit, "(", " && ", ")");
700 break;
701 default:
702 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000703 }
704
705 return visitChildren;
706}
707
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700708bool TOutputGLSLBase::visitUnary(Visit visit, TIntermUnary *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000709{
zmo@google.com32e97312011-08-24 01:03:11 +0000710 TString preString;
711 TString postString = ")";
712
zmo@google.com5601ea02011-06-10 18:23:25 +0000713 switch (node->getOp())
714 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500715 case EOpNegative:
716 preString = "(-";
717 break;
718 case EOpPositive:
719 preString = "(+";
720 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500721 case EOpLogicalNot:
722 preString = "(!";
723 break;
724 case EOpBitwiseNot:
725 preString = "(~";
726 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000727
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500728 case EOpPostIncrement:
729 preString = "(";
730 postString = "++)";
731 break;
732 case EOpPostDecrement:
733 preString = "(";
734 postString = "--)";
735 break;
736 case EOpPreIncrement:
737 preString = "(++";
738 break;
739 case EOpPreDecrement:
740 preString = "(--";
741 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000742
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500743 case EOpRadians:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500744 case EOpDegrees:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500745 case EOpSin:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500746 case EOpCos:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500747 case EOpTan:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500748 case EOpAsin:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500749 case EOpAcos:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500750 case EOpAtan:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500751 case EOpSinh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500752 case EOpCosh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500753 case EOpTanh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500754 case EOpAsinh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500755 case EOpAcosh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500756 case EOpAtanh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500757 case EOpExp:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500758 case EOpLog:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500759 case EOpExp2:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500760 case EOpLog2:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500761 case EOpSqrt:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500762 case EOpInverseSqrt:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500763 case EOpAbs:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500764 case EOpSign:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500765 case EOpFloor:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500766 case EOpTrunc:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500767 case EOpRound:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500768 case EOpRoundEven:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500769 case EOpCeil:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500770 case EOpFract:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500771 case EOpIsNan:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500772 case EOpIsInf:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500773 case EOpFloatBitsToInt:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500774 case EOpFloatBitsToUint:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500775 case EOpIntBitsToFloat:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500776 case EOpUintBitsToFloat:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500777 case EOpPackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500778 case EOpPackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500779 case EOpPackHalf2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500780 case EOpUnpackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500781 case EOpUnpackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500782 case EOpUnpackHalf2x16:
Olli Etuaho25aef452017-01-29 16:15:44 -0800783 case EOpPackUnorm4x8:
784 case EOpPackSnorm4x8:
785 case EOpUnpackUnorm4x8:
786 case EOpUnpackSnorm4x8:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500787 case EOpLength:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500788 case EOpNormalize:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500789 case EOpDFdx:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500790 case EOpDFdy:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500791 case EOpFwidth:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500792 case EOpTranspose:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500793 case EOpDeterminant:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500794 case EOpInverse:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500795 case EOpAny:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500796 case EOpAll:
Olli Etuahod68924e2017-01-02 17:34:40 +0000797 case EOpLogicalNotComponentWise:
Olli Etuaho9250cb22017-01-21 10:51:27 +0000798 case EOpBitfieldReverse:
799 case EOpBitCount:
800 case EOpFindLSB:
801 case EOpFindMSB:
Olli Etuahod68924e2017-01-02 17:34:40 +0000802 writeBuiltInFunctionTriplet(visit, node->getOp(), node->getUseEmulatedFunction());
803 return true;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500804 default:
805 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000806 }
807
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800808 writeTriplet(visit, preString.c_str(), nullptr, postString.c_str());
zmo@google.com32e97312011-08-24 01:03:11 +0000809
zmo@google.com5601ea02011-06-10 18:23:25 +0000810 return true;
811}
812
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300813bool TOutputGLSLBase::visitTernary(Visit visit, TIntermTernary *node)
814{
815 TInfoSinkBase &out = objSink();
816 // Notice two brackets at the beginning and end. The outer ones
817 // encapsulate the whole ternary expression. This preserves the
818 // order of precedence when ternary expressions are used in a
819 // compound expression, i.e., c = 2 * (a < b ? 1 : 2).
820 out << "((";
821 node->getCondition()->traverse(this);
822 out << ") ? (";
823 node->getTrueExpression()->traverse(this);
824 out << ") : (";
825 node->getFalseExpression()->traverse(this);
826 out << "))";
827 return false;
828}
829
Olli Etuaho57961272016-09-14 13:57:46 +0300830bool TOutputGLSLBase::visitIfElse(Visit visit, TIntermIfElse *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000831{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700832 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000833
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300834 out << "if (";
835 node->getCondition()->traverse(this);
836 out << ")\n";
zmo@google.com5601ea02011-06-10 18:23:25 +0000837
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300838 visitCodeBlock(node->getTrueBlock());
zmo@google.com5601ea02011-06-10 18:23:25 +0000839
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300840 if (node->getFalseBlock())
841 {
842 out << "else\n";
843 visitCodeBlock(node->getFalseBlock());
zmo@google.com5601ea02011-06-10 18:23:25 +0000844 }
845 return false;
846}
847
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200848bool TOutputGLSLBase::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200849{
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200850 if (node->getStatementList())
851 {
852 writeTriplet(visit, "switch (", ") ", nullptr);
853 // The curly braces get written when visiting the statementList aggregate
854 }
855 else
856 {
857 // No statementList, so it won't output curly braces
858 writeTriplet(visit, "switch (", ") {", "}\n");
859 }
860 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200861}
862
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200863bool TOutputGLSLBase::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200864{
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200865 if (node->hasCondition())
866 {
867 writeTriplet(visit, "case (", nullptr, "):\n");
868 return true;
869 }
870 else
871 {
872 TInfoSinkBase &out = objSink();
873 out << "default:\n";
874 return false;
875 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200876}
877
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100878bool TOutputGLSLBase::visitBlock(Visit visit, TIntermBlock *node)
879{
880 TInfoSinkBase &out = objSink();
881 // Scope the blocks except when at the global scope.
882 if (mDepth > 0)
883 {
884 out << "{\n";
885 }
886
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100887 for (TIntermSequence::const_iterator iter = node->getSequence()->begin();
888 iter != node->getSequence()->end(); ++iter)
889 {
890 TIntermNode *curNode = *iter;
891 ASSERT(curNode != nullptr);
892 curNode->traverse(this);
893
894 if (isSingleStatement(curNode))
895 out << ";\n";
896 }
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100897
898 // Scope the blocks except when at the global scope.
899 if (mDepth > 0)
900 {
901 out << "}\n";
902 }
903 return false;
904}
905
Olli Etuaho336b1472016-10-05 16:37:55 +0100906bool TOutputGLSLBase::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
907{
Olli Etuaho8ad9e752017-01-16 19:55:20 +0000908 TIntermFunctionPrototype *prototype = node->getFunctionPrototype();
909 prototype->traverse(this);
Olli Etuaho336b1472016-10-05 16:37:55 +0100910 visitCodeBlock(node->getBody());
Olli Etuaho336b1472016-10-05 16:37:55 +0100911
912 // Fully processed; no need to visit children.
913 return false;
914}
915
Olli Etuahobf4e1b72016-12-09 11:30:15 +0000916bool TOutputGLSLBase::visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node)
917{
918 TInfoSinkBase &out = objSink();
919 ASSERT(visit == PreVisit);
920 const TIntermSymbol *symbol = node->getSymbol();
921 out << "invariant " << hashVariableName(symbol->getName());
922 return false;
923}
924
Olli Etuaho16c745a2017-01-16 17:02:27 +0000925bool TOutputGLSLBase::visitFunctionPrototype(Visit visit, TIntermFunctionPrototype *node)
926{
927 TInfoSinkBase &out = objSink();
928 ASSERT(visit == PreVisit);
929
930 const TType &type = node->getType();
931 writeVariableType(type);
932 if (type.isArray())
933 out << arrayBrackets(type);
934
Olli Etuahoec9232b2017-03-27 17:01:37 +0300935 out << " " << hashFunctionNameIfNeeded(*node->getFunctionSymbolInfo());
Olli Etuaho16c745a2017-01-16 17:02:27 +0000936
937 out << "(";
938 writeFunctionParameters(*(node->getSequence()));
939 out << ")";
940
941 return false;
942}
943
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700944bool TOutputGLSLBase::visitAggregate(Visit visit, TIntermAggregate *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000945{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500946 bool visitChildren = true;
947 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000948 switch (node->getOp())
949 {
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800950 case EOpCallFunctionInAST:
951 case EOpCallInternalRawFunction:
952 case EOpCallBuiltInFunction:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500953 // Function call.
954 if (visit == PreVisit)
Olli Etuahoec9232b2017-03-27 17:01:37 +0300955 {
956 if (node->getOp() == EOpCallBuiltInFunction)
957 {
958 out << translateTextureFunction(node->getFunctionSymbolInfo()->getName());
959 }
960 else
961 {
962 out << hashFunctionNameIfNeeded(*node->getFunctionSymbolInfo());
963 }
964 out << "(";
965 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500966 else if (visit == InVisit)
967 out << ", ";
968 else
969 out << ")";
970 break;
Olli Etuaho8fab3202017-05-08 18:22:22 +0300971 case EOpConstruct:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500972 writeConstructorTriplet(visit, node->getType());
973 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +0200974
Olli Etuahoe1805592017-01-02 16:41:20 +0000975 case EOpEqualComponentWise:
976 case EOpNotEqualComponentWise:
977 case EOpLessThanComponentWise:
978 case EOpGreaterThanComponentWise:
979 case EOpLessThanEqualComponentWise:
980 case EOpGreaterThanEqualComponentWise:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500981 case EOpMod:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500982 case EOpModf:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500983 case EOpPow:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500984 case EOpAtan:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500985 case EOpMin:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500986 case EOpMax:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500987 case EOpClamp:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500988 case EOpMix:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500989 case EOpStep:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500990 case EOpSmoothStep:
Olli Etuaho74da73f2017-02-01 15:37:48 +0000991 case EOpFrexp:
992 case EOpLdexp:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500993 case EOpDistance:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500994 case EOpDot:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500995 case EOpCross:
Jamie Madille72595b2017-06-06 15:12:26 -0400996 case EOpFaceforward:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500997 case EOpReflect:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500998 case EOpRefract:
Olli Etuahoe1805592017-01-02 16:41:20 +0000999 case EOpMulMatrixComponentWise:
1000 case EOpOuterProduct:
Olli Etuaho9250cb22017-01-21 10:51:27 +00001001 case EOpBitfieldExtract:
1002 case EOpBitfieldInsert:
1003 case EOpUaddCarry:
1004 case EOpUsubBorrow:
1005 case EOpUmulExtended:
1006 case EOpImulExtended:
Martin Radevd7c5b0a2016-07-27 14:04:43 +03001007 case EOpBarrier:
Martin Radevd7c5b0a2016-07-27 14:04:43 +03001008 case EOpMemoryBarrier:
Martin Radevd7c5b0a2016-07-27 14:04:43 +03001009 case EOpMemoryBarrierAtomicCounter:
Martin Radevd7c5b0a2016-07-27 14:04:43 +03001010 case EOpMemoryBarrierBuffer:
Martin Radevd7c5b0a2016-07-27 14:04:43 +03001011 case EOpMemoryBarrierImage:
Martin Radevd7c5b0a2016-07-27 14:04:43 +03001012 case EOpMemoryBarrierShared:
Martin Radevd7c5b0a2016-07-27 14:04:43 +03001013 case EOpGroupMemoryBarrier:
Olli Etuahod68924e2017-01-02 17:34:40 +00001014 writeBuiltInFunctionTriplet(visit, node->getOp(), node->getUseEmulatedFunction());
Martin Radevd7c5b0a2016-07-27 14:04:43 +03001015 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001016 default:
1017 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +00001018 }
zmo@google.com5601ea02011-06-10 18:23:25 +00001019 return visitChildren;
1020}
1021
Olli Etuaho13389b62016-10-16 11:48:18 +01001022bool TOutputGLSLBase::visitDeclaration(Visit visit, TIntermDeclaration *node)
1023{
1024 TInfoSinkBase &out = objSink();
1025
1026 // Variable declaration.
1027 if (visit == PreVisit)
1028 {
1029 const TIntermSequence &sequence = *(node->getSequence());
1030 const TIntermTyped *variable = sequence.front()->getAsTyped();
1031 writeLayoutQualifier(variable->getType());
1032 writeVariableType(variable->getType());
1033 out << " ";
1034 mDeclaringVariables = true;
1035 }
1036 else if (visit == InVisit)
1037 {
1038 out << ", ";
1039 mDeclaringVariables = true;
1040 }
1041 else
1042 {
1043 mDeclaringVariables = false;
1044 }
1045 return true;
1046}
1047
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001048bool TOutputGLSLBase::visitLoop(Visit visit, TIntermLoop *node)
zmo@google.com5601ea02011-06-10 18:23:25 +00001049{
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001050 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +00001051
zmo@google.com5601ea02011-06-10 18:23:25 +00001052 TLoopType loopType = node->getType();
Corentin Wallez7258e302015-09-22 10:40:24 -07001053
zmo@google.com5601ea02011-06-10 18:23:25 +00001054 if (loopType == ELoopFor) // for loop
1055 {
Corentin Wallez1b896c62016-11-16 13:10:44 -05001056 out << "for (";
1057 if (node->getInit())
1058 node->getInit()->traverse(this);
1059 out << "; ";
zmo@google.com5601ea02011-06-10 18:23:25 +00001060
Corentin Wallez1b896c62016-11-16 13:10:44 -05001061 if (node->getCondition())
1062 node->getCondition()->traverse(this);
1063 out << "; ";
zmo@google.com5601ea02011-06-10 18:23:25 +00001064
Corentin Wallez1b896c62016-11-16 13:10:44 -05001065 if (node->getExpression())
1066 node->getExpression()->traverse(this);
1067 out << ")\n";
Corentin Wallez7258e302015-09-22 10:40:24 -07001068
Corentin Wallez1b896c62016-11-16 13:10:44 -05001069 visitCodeBlock(node->getBody());
zmo@google.com5601ea02011-06-10 18:23:25 +00001070 }
1071 else if (loopType == ELoopWhile) // while loop
1072 {
1073 out << "while (";
Yunchao He4f285442017-04-21 12:15:49 +08001074 ASSERT(node->getCondition() != nullptr);
zmo@google.com5601ea02011-06-10 18:23:25 +00001075 node->getCondition()->traverse(this);
1076 out << ")\n";
Corentin Wallez7258e302015-09-22 10:40:24 -07001077
1078 visitCodeBlock(node->getBody());
zmo@google.com5601ea02011-06-10 18:23:25 +00001079 }
1080 else // do-while loop
1081 {
1082 ASSERT(loopType == ELoopDoWhile);
1083 out << "do\n";
zmo@google.com5601ea02011-06-10 18:23:25 +00001084
zmo@google.com5601ea02011-06-10 18:23:25 +00001085 visitCodeBlock(node->getBody());
zmo@google.com5601ea02011-06-10 18:23:25 +00001086
zmo@google.com5601ea02011-06-10 18:23:25 +00001087 out << "while (";
Yunchao He4f285442017-04-21 12:15:49 +08001088 ASSERT(node->getCondition() != nullptr);
zmo@google.com5601ea02011-06-10 18:23:25 +00001089 node->getCondition()->traverse(this);
1090 out << ");\n";
1091 }
Corentin Wallez7258e302015-09-22 10:40:24 -07001092
zmo@google.com5601ea02011-06-10 18:23:25 +00001093 // No need to visit children. They have been already processed in
1094 // this function.
1095 return false;
1096}
1097
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001098bool TOutputGLSLBase::visitBranch(Visit visit, TIntermBranch *node)
zmo@google.com5601ea02011-06-10 18:23:25 +00001099{
1100 switch (node->getFlowOp())
1101 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001102 case EOpKill:
Yunchao Hef81ce4a2017-04-24 10:49:17 +08001103 writeTriplet(visit, "discard", nullptr, nullptr);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001104 break;
1105 case EOpBreak:
Yunchao Hef81ce4a2017-04-24 10:49:17 +08001106 writeTriplet(visit, "break", nullptr, nullptr);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001107 break;
1108 case EOpContinue:
Yunchao Hef81ce4a2017-04-24 10:49:17 +08001109 writeTriplet(visit, "continue", nullptr, nullptr);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001110 break;
1111 case EOpReturn:
Yunchao Hef81ce4a2017-04-24 10:49:17 +08001112 writeTriplet(visit, "return ", nullptr, nullptr);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001113 break;
1114 default:
1115 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +00001116 }
1117
1118 return true;
1119}
1120
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001121void TOutputGLSLBase::visitCodeBlock(TIntermBlock *node)
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001122{
zmo@google.com5601ea02011-06-10 18:23:25 +00001123 TInfoSinkBase &out = objSink();
Yunchao He4f285442017-04-21 12:15:49 +08001124 if (node != nullptr)
zmo@google.com5601ea02011-06-10 18:23:25 +00001125 {
1126 node->traverse(this);
1127 // Single statements not part of a sequence need to be terminated
1128 // with semi-colon.
1129 if (isSingleStatement(node))
1130 out << ";\n";
1131 }
1132 else
1133 {
1134 out << "{\n}\n"; // Empty code block.
1135 }
1136}
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001137
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001138TString TOutputGLSLBase::getTypeName(const TType &type)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001139{
Olli Etuahoe92507b2016-07-04 11:20:10 +03001140 if (type.getBasicType() == EbtStruct)
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001141 return hashName(TName(type.getStruct()->name()));
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001142 else
Olli Etuahoe92507b2016-07-04 11:20:10 +03001143 return type.getBuiltInTypeNameString();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001144}
1145
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001146TString TOutputGLSLBase::hashName(const TName &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001147{
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001148 if (name.getString().empty())
1149 {
1150 ASSERT(!name.isInternal());
1151 return name.getString();
1152 }
1153 if (name.isInternal())
1154 {
1155 // TODO(oetuaho): Would be nicer to prefix non-internal names with "_" instead, like is
1156 // done in the HLSL output, but that requires fairly complex changes elsewhere in the code
1157 // as well.
1158 // We need to use a prefix that is reserved in WebGL in order to guarantee that the internal
1159 // names don't conflict with user-defined names from WebGL.
1160 return "webgl_angle_" + name.getString();
1161 }
1162 if (mHashFunction == nullptr)
1163 {
1164 return name.getString();
1165 }
1166 NameMap::const_iterator it = mNameMap.find(name.getString().c_str());
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001167 if (it != mNameMap.end())
1168 return it->second.c_str();
Olli Etuahocccf2b02017-07-05 14:50:54 +03001169 TString hashedName = HashName(name.getString(), mHashFunction);
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001170 mNameMap[name.getString().c_str()] = hashedName.c_str();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001171 return hashedName;
1172}
1173
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001174TString TOutputGLSLBase::hashVariableName(const TName &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001175{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03001176 if (mSymbolTable->findBuiltIn(name.getString(), mShaderVersion) != nullptr)
Olli Etuaho09b04a22016-12-15 13:30:26 +00001177 {
1178 if (mCompileOptions & SH_TRANSLATE_VIEWID_OVR_TO_UNIFORM &&
1179 name.getString() == "gl_ViewID_OVR")
1180 {
Olli Etuaho2f90a9b2017-01-10 12:27:56 +00001181 TName uniformName(TString("ViewID_OVR"));
Olli Etuaho09b04a22016-12-15 13:30:26 +00001182 uniformName.setInternal(true);
1183 return hashName(uniformName);
1184 }
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001185 return name.getString();
Olli Etuaho09b04a22016-12-15 13:30:26 +00001186 }
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001187 return hashName(name);
1188}
1189
Olli Etuahoec9232b2017-03-27 17:01:37 +03001190TString TOutputGLSLBase::hashFunctionNameIfNeeded(const TFunctionSymbolInfo &info)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001191{
Olli Etuahoec9232b2017-03-27 17:01:37 +03001192 if (info.isMain() || info.getNameObj().isInternal())
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001193 {
1194 // Internal function names are outputted as-is - they may refer to functions manually added
1195 // to the output shader source that are not included in the AST at all.
Olli Etuahoec9232b2017-03-27 17:01:37 +03001196 return info.getName();
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001197 }
Olli Etuaho59f9a642015-08-06 20:38:26 +03001198 else
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001199 {
Olli Etuahoec9232b2017-03-27 17:01:37 +03001200 return hashName(info.getNameObj());
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001201 }
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001202}
Jamie Madill98493dd2013-07-08 14:39:03 -04001203
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001204bool TOutputGLSLBase::structDeclared(const TStructure *structure) const
Jamie Madill98493dd2013-07-08 14:39:03 -04001205{
Zhenyao Mo904a9162014-05-09 14:07:45 -07001206 ASSERT(structure);
Jamie Madill01f85ac2014-06-06 11:55:04 -04001207 if (structure->name().empty())
Zhenyao Mo904a9162014-05-09 14:07:45 -07001208 {
Jamie Madill01f85ac2014-06-06 11:55:04 -04001209 return false;
Zhenyao Mo904a9162014-05-09 14:07:45 -07001210 }
Jamie Madill01f85ac2014-06-06 11:55:04 -04001211
1212 return (mDeclaredStructs.count(structure->uniqueId()) > 0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001213}
1214
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001215void TOutputGLSLBase::declareStruct(const TStructure *structure)
Jamie Madill98493dd2013-07-08 14:39:03 -04001216{
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001217 TInfoSinkBase &out = objSink();
Jamie Madill98493dd2013-07-08 14:39:03 -04001218
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001219 out << "struct " << hashName(TName(structure->name())) << "{\n";
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001220 const TFieldList &fields = structure->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04001221 for (size_t i = 0; i < fields.size(); ++i)
1222 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001223 const TField *field = fields[i];
Jamie Madill98493dd2013-07-08 14:39:03 -04001224 if (writeVariablePrecision(field->type()->getPrecision()))
1225 out << " ";
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001226 out << getTypeName(*field->type()) << " " << hashName(TName(field->name()));
Jamie Madill98493dd2013-07-08 14:39:03 -04001227 if (field->type()->isArray())
1228 out << arrayBrackets(*field->type());
1229 out << ";\n";
1230 }
1231 out << "}";
Zhenyao Mo904a9162014-05-09 14:07:45 -07001232}
Jamie Madill98493dd2013-07-08 14:39:03 -04001233
Geoff Langbdcc54a2015-09-02 13:09:48 -04001234void TOutputGLSLBase::declareInterfaceBlockLayout(const TInterfaceBlock *interfaceBlock)
1235{
1236 TInfoSinkBase &out = objSink();
1237
1238 out << "layout(";
1239
1240 switch (interfaceBlock->blockStorage())
1241 {
1242 case EbsUnspecified:
1243 case EbsShared:
1244 // Default block storage is shared.
1245 out << "shared";
1246 break;
1247
1248 case EbsPacked:
1249 out << "packed";
1250 break;
1251
1252 case EbsStd140:
1253 out << "std140";
1254 break;
1255
1256 default:
1257 UNREACHABLE();
1258 break;
1259 }
1260
1261 out << ", ";
1262
1263 switch (interfaceBlock->matrixPacking())
1264 {
1265 case EmpUnspecified:
1266 case EmpColumnMajor:
1267 // Default matrix packing is column major.
1268 out << "column_major";
1269 break;
1270
1271 case EmpRowMajor:
1272 out << "row_major";
1273 break;
1274
1275 default:
1276 UNREACHABLE();
1277 break;
1278 }
1279
1280 out << ") ";
1281}
1282
1283void TOutputGLSLBase::declareInterfaceBlock(const TInterfaceBlock *interfaceBlock)
1284{
1285 TInfoSinkBase &out = objSink();
1286
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001287 out << hashName(TName(interfaceBlock->name())) << "{\n";
Geoff Langbdcc54a2015-09-02 13:09:48 -04001288 const TFieldList &fields = interfaceBlock->fields();
1289 for (size_t i = 0; i < fields.size(); ++i)
1290 {
1291 const TField *field = fields[i];
1292 if (writeVariablePrecision(field->type()->getPrecision()))
1293 out << " ";
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001294 out << getTypeName(*field->type()) << " " << hashName(TName(field->name()));
Geoff Langbdcc54a2015-09-02 13:09:48 -04001295 if (field->type()->isArray())
1296 out << arrayBrackets(*field->type());
1297 out << ";\n";
1298 }
1299 out << "}";
1300}
Jamie Madill45bcc782016-11-07 13:58:48 -05001301
Shaob5cc1192017-07-06 10:47:20 +08001302void WriteGeometryShaderLayoutQualifiers(TInfoSinkBase &out,
1303 sh::TLayoutPrimitiveType inputPrimitive,
1304 int invocations,
1305 sh::TLayoutPrimitiveType outputPrimitive,
1306 int maxVertices)
1307{
1308 // Omit 'invocations = 1'
1309 if (inputPrimitive != EptUndefined || invocations > 1)
1310 {
1311 out << "layout (";
1312
1313 if (inputPrimitive != EptUndefined)
1314 {
1315 out << getGeometryShaderPrimitiveTypeString(inputPrimitive);
1316 }
1317
1318 if (invocations > 1)
1319 {
1320 if (inputPrimitive != EptUndefined)
1321 {
1322 out << ", ";
1323 }
1324 out << "invocations = " << invocations;
1325 }
1326 out << ") in;\n";
1327 }
1328
1329 if (outputPrimitive != EptUndefined || maxVertices != -1)
1330 {
1331 out << "layout (";
1332
1333 if (outputPrimitive != EptUndefined)
1334 {
1335 out << getGeometryShaderPrimitiveTypeString(outputPrimitive);
1336 }
1337
1338 if (maxVertices != -1)
1339 {
1340 if (outputPrimitive != EptUndefined)
1341 {
1342 out << ", ";
1343 }
1344 out << "max_vertices = " << maxVertices;
1345 }
1346 out << ") out;\n";
1347 }
1348}
1349
Jamie Madill45bcc782016-11-07 13:58:48 -05001350} // namespace sh