blob: 9a47287d767dd72a38e86f709fe0c809809c0e24 [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"
Olli Etuaho96f6adf2017-08-16 11:18:54 +030013#include "compiler/translator/util.h"
zmo@google.com5601ea02011-06-10 18:23:25 +000014
daniel@transgaming.com773ff742013-01-11 04:12:51 +000015#include <cfloat>
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +000016
Jamie Madill45bcc782016-11-07 13:58:48 -050017namespace sh
18{
19
zmo@google.com5601ea02011-06-10 18:23:25 +000020namespace
21{
zmo@google.com5601ea02011-06-10 18:23:25 +000022
Zhenyao Mo9eedea02014-05-12 16:02:35 -070023bool isSingleStatement(TIntermNode *node)
24{
Olli Etuaho336b1472016-10-05 16:37:55 +010025 if (node->getAsFunctionDefinition())
zmo@google.com5601ea02011-06-10 18:23:25 +000026 {
Olli Etuaho336b1472016-10-05 16:37:55 +010027 return false;
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010028 }
29 else if (node->getAsBlock())
30 {
31 return false;
zmo@google.com5601ea02011-06-10 18:23:25 +000032 }
Olli Etuaho57961272016-09-14 13:57:46 +030033 else if (node->getAsIfElseNode())
zmo@google.com5601ea02011-06-10 18:23:25 +000034 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +030035 return false;
zmo@google.com5601ea02011-06-10 18:23:25 +000036 }
37 else if (node->getAsLoopNode())
38 {
39 return false;
40 }
Olli Etuaho01cd8af2015-02-20 10:39:20 +020041 else if (node->getAsSwitchNode())
42 {
43 return false;
44 }
45 else if (node->getAsCaseNode())
46 {
47 return false;
48 }
zmo@google.com5601ea02011-06-10 18:23:25 +000049 return true;
50}
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080051
Olli Etuaho43364892017-02-13 16:00:12 +000052class CommaSeparatedListItemPrefixGenerator
53{
54 public:
55 CommaSeparatedListItemPrefixGenerator() : mFirst(true) {}
56 private:
57 bool mFirst;
58
59 friend TInfoSinkBase &operator<<(TInfoSinkBase &out,
60 CommaSeparatedListItemPrefixGenerator &gen);
61};
62
63TInfoSinkBase &operator<<(TInfoSinkBase &out, CommaSeparatedListItemPrefixGenerator &gen)
64{
65 if (gen.mFirst)
66 {
67 gen.mFirst = false;
68 }
69 else
70 {
71 out << ", ";
72 }
73 return out;
74}
75
zmo@google.com5601ea02011-06-10 18:23:25 +000076} // namespace
77
Zhenyao Mo9eedea02014-05-12 16:02:35 -070078TOutputGLSLBase::TOutputGLSLBase(TInfoSinkBase &objSink,
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +000079 ShArrayIndexClampingStrategy clampingStrategy,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000080 ShHashFunction64 hashFunction,
Zhenyao Mo9eedea02014-05-12 16:02:35 -070081 NameMap &nameMap,
Olli Etuahoa5e693a2017-07-13 16:07:26 +030082 TSymbolTable *symbolTable,
Qiankun Miao89dd8f32016-11-09 12:59:30 +000083 sh::GLenum shaderType,
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080084 int shaderVersion,
Qiankun Miao705a9192016-08-29 10:05:27 +080085 ShShaderOutput output,
86 ShCompileOptions compileOptions)
Olli Etuahoa5e693a2017-07-13 16:07:26 +030087 : TIntermTraverser(true, true, true, symbolTable),
zmo@google.com5601ea02011-06-10 18:23:25 +000088 mObjSink(objSink),
Olli Etuaho39f74df2017-11-20 16:09:57 +020089 mDeclaringVariable(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +000090 mClampingStrategy(clampingStrategy),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000091 mHashFunction(hashFunction),
92 mNameMap(nameMap),
Qiankun Miao89dd8f32016-11-09 12:59:30 +000093 mShaderType(shaderType),
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080094 mShaderVersion(shaderVersion),
Qiankun Miao705a9192016-08-29 10:05:27 +080095 mOutput(output),
96 mCompileOptions(compileOptions)
zmo@google.com5601ea02011-06-10 18:23:25 +000097{
98}
99
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800100void TOutputGLSLBase::writeInvariantQualifier(const TType &type)
101{
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000102 if (!sh::RemoveInvariant(mShaderType, mShaderVersion, mOutput, mCompileOptions))
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800103 {
104 TInfoSinkBase &out = objSink();
105 out << "invariant ";
106 }
107}
108
Olli Etuaho56a2f952016-12-08 12:16:27 +0000109void TOutputGLSLBase::writeFloat(TInfoSinkBase &out, float f)
110{
111 if ((gl::isInf(f) || gl::isNaN(f)) && mShaderVersion >= 300)
112 {
113 out << "uintBitsToFloat(" << gl::bitCast<uint32_t>(f) << "u)";
114 }
115 else
116 {
117 out << std::min(FLT_MAX, std::max(-FLT_MAX, f));
118 }
119}
120
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500121void TOutputGLSLBase::writeTriplet(Visit visit,
122 const char *preStr,
123 const char *inStr,
124 const char *postStr)
zmo@google.com5601ea02011-06-10 18:23:25 +0000125{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700126 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000127 if (visit == PreVisit && preStr)
zmo@google.com5601ea02011-06-10 18:23:25 +0000128 out << preStr;
zmo@google.com5601ea02011-06-10 18:23:25 +0000129 else if (visit == InVisit && inStr)
zmo@google.com5601ea02011-06-10 18:23:25 +0000130 out << inStr;
zmo@google.com5601ea02011-06-10 18:23:25 +0000131 else if (visit == PostVisit && postStr)
zmo@google.com5601ea02011-06-10 18:23:25 +0000132 out << postStr;
zmo@google.com5601ea02011-06-10 18:23:25 +0000133}
134
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500135void TOutputGLSLBase::writeBuiltInFunctionTriplet(Visit visit,
Olli Etuahoe1805592017-01-02 16:41:20 +0000136 TOperator op,
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500137 bool useEmulatedFunction)
zmo@google.com5601ea02011-06-10 18:23:25 +0000138{
Olli Etuahod68924e2017-01-02 17:34:40 +0000139 TInfoSinkBase &out = objSink();
140 if (visit == PreVisit)
Olli Etuahoe1805592017-01-02 16:41:20 +0000141 {
Olli Etuahod68924e2017-01-02 17:34:40 +0000142 const char *opStr(GetOperatorString(op));
143 if (useEmulatedFunction)
144 {
145 BuiltInFunctionEmulator::WriteEmulatedFunctionName(out, opStr);
146 }
147 else
148 {
149 out << opStr;
150 }
151 out << "(";
Olli Etuahoe1805592017-01-02 16:41:20 +0000152 }
Olli Etuahod68924e2017-01-02 17:34:40 +0000153 else
154 {
155 writeTriplet(visit, nullptr, ", ", ")");
156 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700157}
158
Jamie Madill2a9e1072017-09-22 11:31:57 -0400159void TOutputGLSLBase::writeLayoutQualifier(TIntermTyped *variable)
Olli Etuahoae69d7e2015-10-07 17:19:50 +0300160{
Jamie Madill2a9e1072017-09-22 11:31:57 -0400161 const TType &type = variable->getType();
162
Martin Radev2cc85b32016-08-05 16:22:53 +0300163 if (!NeedsToWriteLayoutQualifier(type))
164 {
165 return;
166 }
167
168 TInfoSinkBase &out = objSink();
169 const TLayoutQualifier &layoutQualifier = type.getLayoutQualifier();
170 out << "layout(";
171
Olli Etuaho43364892017-02-13 16:00:12 +0000172 CommaSeparatedListItemPrefixGenerator listItemPrefix;
173
Jiawei Shao4cc89e22017-08-31 14:25:54 +0800174 if (type.getQualifier() == EvqFragmentOut || type.getQualifier() == EvqVertexIn ||
175 IsVarying(type.getQualifier()))
Olli Etuahoae69d7e2015-10-07 17:19:50 +0300176 {
Olli Etuahoae69d7e2015-10-07 17:19:50 +0300177 if (layoutQualifier.location >= 0)
178 {
Olli Etuaho43364892017-02-13 16:00:12 +0000179 out << listItemPrefix << "location = " << layoutQualifier.location;
Olli Etuahoae69d7e2015-10-07 17:19:50 +0300180 }
181 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300182
Andrei Volykhina5527072017-03-22 16:46:30 +0300183 if (type.getQualifier() == EvqFragmentOut)
184 {
185 if (layoutQualifier.yuv == true)
186 {
187 out << listItemPrefix << "yuv";
188 }
189 }
190
Olli Etuaho43364892017-02-13 16:00:12 +0000191 if (IsOpaqueType(type.getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +0300192 {
Olli Etuaho43364892017-02-13 16:00:12 +0000193 if (layoutQualifier.binding >= 0)
194 {
195 out << listItemPrefix << "binding = " << layoutQualifier.binding;
196 }
197 }
198
199 if (IsImage(type.getBasicType()))
200 {
201 if (layoutQualifier.imageInternalFormat != EiifUnspecified)
202 {
203 ASSERT(type.getQualifier() == EvqTemporary || type.getQualifier() == EvqUniform);
204 out << listItemPrefix
205 << getImageInternalFormatString(layoutQualifier.imageInternalFormat);
206 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300207 }
208
jchen1005c31da2017-07-18 16:11:39 +0800209 if (IsAtomicCounter(type.getBasicType()))
210 {
211 out << listItemPrefix << "offset = " << layoutQualifier.offset;
212 }
213
Martin Radev2cc85b32016-08-05 16:22:53 +0300214 out << ") ";
Olli Etuahoae69d7e2015-10-07 17:19:50 +0300215}
216
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800217const char *TOutputGLSLBase::mapQualifierToString(TQualifier qualifier)
218{
219 if (sh::IsGLSL410OrOlder(mOutput) && mShaderVersion >= 300 &&
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000220 (mCompileOptions & SH_REMOVE_INVARIANT_AND_CENTROID_FOR_ESSL3) != 0)
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800221 {
222 switch (qualifier)
223 {
224 // The return string is consistent with sh::getQualifierString() from
225 // BaseTypes.h minus the "centroid" keyword.
226 case EvqCentroid:
227 return "";
228 case EvqCentroidIn:
229 return "smooth in";
230 case EvqCentroidOut:
231 return "smooth out";
232 default:
233 break;
234 }
235 }
236 if (sh::IsGLSL130OrNewer(mOutput))
237 {
238 switch (qualifier)
239 {
240 case EvqAttribute:
241 return "in";
242 case EvqVaryingIn:
243 return "in";
244 case EvqVaryingOut:
245 return "out";
246 default:
247 break;
248 }
249 }
250 return sh::getQualifierString(qualifier);
251}
252
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700253void TOutputGLSLBase::writeVariableType(const TType &type)
254{
Qiankun Miao705a9192016-08-29 10:05:27 +0800255 TQualifier qualifier = type.getQualifier();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500256 TInfoSinkBase &out = objSink();
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800257 if (type.isInvariant())
Olli Etuaho214c2d82015-04-27 14:49:13 +0300258 {
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800259 writeInvariantQualifier(type);
Olli Etuaho214c2d82015-04-27 14:49:13 +0300260 }
Geoff Langbdcc54a2015-09-02 13:09:48 -0400261 if (type.getBasicType() == EbtInterfaceBlock)
262 {
263 TInterfaceBlock *interfaceBlock = type.getInterfaceBlock();
264 declareInterfaceBlockLayout(interfaceBlock);
265 }
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400266 if (qualifier != EvqTemporary && qualifier != EvqGlobal)
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400267 {
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800268 const char *qualifierString = mapQualifierToString(qualifier);
269 if (qualifierString && qualifierString[0] != '\0')
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800270 {
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800271 out << qualifierString << " ";
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800272 }
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400273 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300274
275 const TMemoryQualifier &memoryQualifier = type.getMemoryQualifier();
276 if (memoryQualifier.readonly)
277 {
278 ASSERT(IsImage(type.getBasicType()));
279 out << "readonly ";
280 }
281
282 if (memoryQualifier.writeonly)
283 {
284 ASSERT(IsImage(type.getBasicType()));
285 out << "writeonly ";
286 }
287
Martin Radev049edfa2016-11-11 14:35:37 +0200288 if (memoryQualifier.coherent)
289 {
290 ASSERT(IsImage(type.getBasicType()));
291 out << "coherent ";
292 }
293
294 if (memoryQualifier.restrictQualifier)
295 {
296 ASSERT(IsImage(type.getBasicType()));
297 out << "restrict ";
298 }
299
300 if (memoryQualifier.volatileQualifier)
301 {
302 ASSERT(IsImage(type.getBasicType()));
303 out << "volatile ";
304 }
305
zmo@google.com5601ea02011-06-10 18:23:25 +0000306 // Declare the struct if we have not done so already.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700307 if (type.getBasicType() == EbtStruct && !structDeclared(type.getStruct()))
zmo@google.com5601ea02011-06-10 18:23:25 +0000308 {
Olli Etuahobd3cd502017-11-03 15:48:52 +0200309 const TStructure *structure = type.getStruct();
Jamie Madill01f85ac2014-06-06 11:55:04 -0400310
311 declareStruct(structure);
312
313 if (!structure->name().empty())
314 {
315 mDeclaredStructs.insert(structure->uniqueId());
316 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000317 }
Geoff Langbdcc54a2015-09-02 13:09:48 -0400318 else if (type.getBasicType() == EbtInterfaceBlock)
319 {
320 TInterfaceBlock *interfaceBlock = type.getInterfaceBlock();
321 declareInterfaceBlock(interfaceBlock);
322 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000323 else
324 {
325 if (writeVariablePrecision(type.getPrecision()))
326 out << " ";
327 out << getTypeName(type);
328 }
329}
330
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700331void TOutputGLSLBase::writeFunctionParameters(const TIntermSequence &args)
zmo@google.com5601ea02011-06-10 18:23:25 +0000332{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700333 TInfoSinkBase &out = objSink();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500334 for (TIntermSequence::const_iterator iter = args.begin(); iter != args.end(); ++iter)
zmo@google.com5601ea02011-06-10 18:23:25 +0000335 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700336 const TIntermSymbol *arg = (*iter)->getAsSymbolNode();
Yunchao He4f285442017-04-21 12:15:49 +0800337 ASSERT(arg != nullptr);
zmo@google.com5601ea02011-06-10 18:23:25 +0000338
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700339 const TType &type = arg->getType();
zmo@google.com189be2f2011-06-16 18:28:53 +0000340 writeVariableType(type);
zmo@google.com5601ea02011-06-10 18:23:25 +0000341
Olli Etuaho0982a2b2016-11-01 15:13:46 +0000342 if (!arg->getName().getString().empty())
343 out << " " << hashName(arg->getName());
zmo@google.com5601ea02011-06-10 18:23:25 +0000344 if (type.isArray())
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300345 out << ArrayString(type);
zmo@google.com5601ea02011-06-10 18:23:25 +0000346
347 // Put a comma if this is not the last argument.
348 if (iter != args.end() - 1)
349 out << ", ";
350 }
351}
352
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500353const TConstantUnion *TOutputGLSLBase::writeConstantUnion(const TType &type,
354 const TConstantUnion *pConstUnion)
zmo@google.com5601ea02011-06-10 18:23:25 +0000355{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700356 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000357
358 if (type.getBasicType() == EbtStruct)
359 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700360 const TStructure *structure = type.getStruct();
Olli Etuaho0982a2b2016-11-01 15:13:46 +0000361 out << hashName(TName(structure->name())) << "(";
Jamie Madill98493dd2013-07-08 14:39:03 -0400362
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700363 const TFieldList &fields = structure->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -0400364 for (size_t i = 0; i < fields.size(); ++i)
zmo@google.com5601ea02011-06-10 18:23:25 +0000365 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700366 const TType *fieldType = fields[i]->type();
Yunchao He4f285442017-04-21 12:15:49 +0800367 ASSERT(fieldType != nullptr);
zmo@google.com5601ea02011-06-10 18:23:25 +0000368 pConstUnion = writeConstantUnion(*fieldType, pConstUnion);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700369 if (i != fields.size() - 1)
370 out << ", ";
zmo@google.com5601ea02011-06-10 18:23:25 +0000371 }
372 out << ")";
373 }
374 else
375 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500376 size_t size = type.getObjectSize();
zmo@google.com5601ea02011-06-10 18:23:25 +0000377 bool writeType = size > 1;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700378 if (writeType)
379 out << getTypeName(type) << "(";
Jamie Madill94bf7f22013-07-08 13:31:15 -0400380 for (size_t i = 0; i < size; ++i, ++pConstUnion)
zmo@google.com5601ea02011-06-10 18:23:25 +0000381 {
382 switch (pConstUnion->getType())
383 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500384 case EbtFloat:
385 writeFloat(out, pConstUnion->getFConst());
386 break;
387 case EbtInt:
388 out << pConstUnion->getIConst();
389 break;
390 case EbtUInt:
391 out << pConstUnion->getUConst() << "u";
392 break;
393 case EbtBool:
394 out << pConstUnion->getBConst();
395 break;
Andrei Volykhina5527072017-03-22 16:46:30 +0300396 case EbtYuvCscStandardEXT:
397 out << getYuvCscStandardEXTString(pConstUnion->getYuvCscStandardEXTConst());
398 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500399 default:
400 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000401 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700402 if (i != size - 1)
403 out << ", ";
zmo@google.com5601ea02011-06-10 18:23:25 +0000404 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700405 if (writeType)
406 out << ")";
zmo@google.com5601ea02011-06-10 18:23:25 +0000407 }
408 return pConstUnion;
409}
410
Olli Etuahoe92507b2016-07-04 11:20:10 +0300411void TOutputGLSLBase::writeConstructorTriplet(Visit visit, const TType &type)
Olli Etuahof40319e2015-03-10 14:33:00 +0200412{
413 TInfoSinkBase &out = objSink();
414 if (visit == PreVisit)
415 {
416 if (type.isArray())
417 {
Olli Etuahoe92507b2016-07-04 11:20:10 +0300418 out << getTypeName(type);
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300419 out << ArrayString(type);
Olli Etuahof40319e2015-03-10 14:33:00 +0200420 out << "(";
421 }
422 else
423 {
Olli Etuahoe92507b2016-07-04 11:20:10 +0300424 out << getTypeName(type) << "(";
Olli Etuahof40319e2015-03-10 14:33:00 +0200425 }
426 }
427 else
428 {
429 writeTriplet(visit, nullptr, ", ", ")");
430 }
431}
432
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700433void TOutputGLSLBase::visitSymbol(TIntermSymbol *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000434{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700435 TInfoSinkBase &out = objSink();
Corentin Wallez1b896c62016-11-16 13:10:44 -0500436 out << hashVariableName(node->getName());
zmo@google.com5601ea02011-06-10 18:23:25 +0000437
Olli Etuaho39f74df2017-11-20 16:09:57 +0200438 if (mDeclaringVariable && node->getType().isArray())
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300439 out << ArrayString(node->getType());
zmo@google.com5601ea02011-06-10 18:23:25 +0000440}
441
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700442void TOutputGLSLBase::visitConstantUnion(TIntermConstantUnion *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000443{
444 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
445}
446
Olli Etuahob6fa0432016-09-28 16:28:05 +0100447bool TOutputGLSLBase::visitSwizzle(Visit visit, TIntermSwizzle *node)
448{
449 TInfoSinkBase &out = objSink();
450 if (visit == PostVisit)
451 {
452 out << ".";
453 node->writeOffsetsAsXYZW(&out);
454 }
455 return true;
456}
457
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700458bool TOutputGLSLBase::visitBinary(Visit visit, TIntermBinary *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000459{
460 bool visitChildren = true;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700461 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000462 switch (node->getOp())
463 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100464 case EOpComma:
465 writeTriplet(visit, "(", ", ", ")");
466 break;
467 case EOpInitialize:
zmo@google.com5601ea02011-06-10 18:23:25 +0000468 if (visit == InVisit)
469 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100470 out << " = ";
471 // RHS of initialize is not being declared.
Olli Etuaho39f74df2017-11-20 16:09:57 +0200472 mDeclaringVariable = false;
zmo@google.com5601ea02011-06-10 18:23:25 +0000473 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100474 break;
475 case EOpAssign:
476 writeTriplet(visit, "(", " = ", ")");
477 break;
478 case EOpAddAssign:
479 writeTriplet(visit, "(", " += ", ")");
480 break;
481 case EOpSubAssign:
482 writeTriplet(visit, "(", " -= ", ")");
483 break;
484 case EOpDivAssign:
485 writeTriplet(visit, "(", " /= ", ")");
486 break;
487 case EOpIModAssign:
488 writeTriplet(visit, "(", " %= ", ")");
489 break;
490 // Notice the fall-through.
491 case EOpMulAssign:
492 case EOpVectorTimesMatrixAssign:
493 case EOpVectorTimesScalarAssign:
494 case EOpMatrixTimesScalarAssign:
495 case EOpMatrixTimesMatrixAssign:
496 writeTriplet(visit, "(", " *= ", ")");
497 break;
498 case EOpBitShiftLeftAssign:
499 writeTriplet(visit, "(", " <<= ", ")");
500 break;
501 case EOpBitShiftRightAssign:
502 writeTriplet(visit, "(", " >>= ", ")");
503 break;
504 case EOpBitwiseAndAssign:
505 writeTriplet(visit, "(", " &= ", ")");
506 break;
507 case EOpBitwiseXorAssign:
508 writeTriplet(visit, "(", " ^= ", ")");
509 break;
510 case EOpBitwiseOrAssign:
511 writeTriplet(visit, "(", " |= ", ")");
512 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000513
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100514 case EOpIndexDirect:
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800515 writeTriplet(visit, nullptr, "[", "]");
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100516 break;
517 case EOpIndexIndirect:
518 if (node->getAddIndexClamp())
519 {
520 if (visit == InVisit)
521 {
522 if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC)
523 out << "[int(clamp(float(";
524 else
525 out << "[webgl_int_clamp(";
526 }
527 else if (visit == PostVisit)
528 {
529 int maxSize;
530 TIntermTyped *left = node->getLeft();
531 TType leftType = left->getType();
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700532
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100533 if (left->isArray())
534 {
535 // The shader will fail validation if the array length is not > 0.
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300536 maxSize = static_cast<int>(leftType.getOutermostArraySize()) - 1;
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100537 }
538 else
539 {
540 maxSize = leftType.getNominalSize() - 1;
541 }
542
543 if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC)
544 out << "), 0.0, float(" << maxSize << ")))]";
545 else
546 out << ", 0, " << maxSize << ")]";
547 }
548 }
549 else
550 {
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800551 writeTriplet(visit, nullptr, "[", "]");
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100552 }
553 break;
554 case EOpIndexDirectStruct:
555 if (visit == InVisit)
556 {
557 // Here we are writing out "foo.bar", where "foo" is struct
558 // and "bar" is field. In AST, it is represented as a binary
559 // node, where left child represents "foo" and right child "bar".
560 // The node itself represents ".". The struct field "bar" is
561 // actually stored as an index into TStructure::fields.
562 out << ".";
563 const TStructure *structure = node->getLeft()->getType().getStruct();
564 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
565 const TField *field = structure->fields()[index->getIConst(0)];
566
567 TString fieldName = field->name();
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300568 if (!mSymbolTable->findBuiltIn(structure->name(), mShaderVersion))
Olli Etuaho0982a2b2016-11-01 15:13:46 +0000569 fieldName = hashName(TName(fieldName));
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100570
571 out << fieldName;
572 visitChildren = false;
573 }
574 break;
575 case EOpIndexDirectInterfaceBlock:
576 if (visit == InVisit)
577 {
578 out << ".";
579 const TInterfaceBlock *interfaceBlock =
580 node->getLeft()->getType().getInterfaceBlock();
581 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
582 const TField *field = interfaceBlock->fields()[index->getIConst(0)];
583
584 TString fieldName = field->name();
Jiawei Shao65c56dd2017-10-13 16:18:57 +0800585 if (!mSymbolTable->findBuiltIn(interfaceBlock->name(), mShaderVersion))
586 {
587 fieldName = hashName(TName(fieldName));
588 }
589 else
590 {
591 ASSERT(interfaceBlock->name() == "gl_PerVertex");
592 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700593
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100594 out << fieldName;
595 visitChildren = false;
596 }
597 break;
Geoff Lang6e360422015-09-02 15:54:36 -0400598
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100599 case EOpAdd:
600 writeTriplet(visit, "(", " + ", ")");
601 break;
602 case EOpSub:
603 writeTriplet(visit, "(", " - ", ")");
604 break;
605 case EOpMul:
606 writeTriplet(visit, "(", " * ", ")");
607 break;
608 case EOpDiv:
609 writeTriplet(visit, "(", " / ", ")");
610 break;
611 case EOpIMod:
612 writeTriplet(visit, "(", " % ", ")");
613 break;
614 case EOpBitShiftLeft:
615 writeTriplet(visit, "(", " << ", ")");
616 break;
617 case EOpBitShiftRight:
618 writeTriplet(visit, "(", " >> ", ")");
619 break;
620 case EOpBitwiseAnd:
621 writeTriplet(visit, "(", " & ", ")");
622 break;
623 case EOpBitwiseXor:
624 writeTriplet(visit, "(", " ^ ", ")");
625 break;
626 case EOpBitwiseOr:
627 writeTriplet(visit, "(", " | ", ")");
628 break;
Geoff Lang6e360422015-09-02 15:54:36 -0400629
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100630 case EOpEqual:
631 writeTriplet(visit, "(", " == ", ")");
632 break;
633 case EOpNotEqual:
634 writeTriplet(visit, "(", " != ", ")");
635 break;
636 case EOpLessThan:
637 writeTriplet(visit, "(", " < ", ")");
638 break;
639 case EOpGreaterThan:
640 writeTriplet(visit, "(", " > ", ")");
641 break;
642 case EOpLessThanEqual:
643 writeTriplet(visit, "(", " <= ", ")");
644 break;
645 case EOpGreaterThanEqual:
646 writeTriplet(visit, "(", " >= ", ")");
647 break;
daniel@transgaming.com97b16d12013-02-01 03:20:42 +0000648
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100649 // Notice the fall-through.
650 case EOpVectorTimesScalar:
651 case EOpVectorTimesMatrix:
652 case EOpMatrixTimesVector:
653 case EOpMatrixTimesScalar:
654 case EOpMatrixTimesMatrix:
655 writeTriplet(visit, "(", " * ", ")");
656 break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200657
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100658 case EOpLogicalOr:
659 writeTriplet(visit, "(", " || ", ")");
660 break;
661 case EOpLogicalXor:
662 writeTriplet(visit, "(", " ^^ ", ")");
663 break;
664 case EOpLogicalAnd:
665 writeTriplet(visit, "(", " && ", ")");
666 break;
667 default:
668 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000669 }
670
671 return visitChildren;
672}
673
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700674bool TOutputGLSLBase::visitUnary(Visit visit, TIntermUnary *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000675{
zmo@google.com32e97312011-08-24 01:03:11 +0000676 TString preString;
677 TString postString = ")";
678
zmo@google.com5601ea02011-06-10 18:23:25 +0000679 switch (node->getOp())
680 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500681 case EOpNegative:
682 preString = "(-";
683 break;
684 case EOpPositive:
685 preString = "(+";
686 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500687 case EOpLogicalNot:
688 preString = "(!";
689 break;
690 case EOpBitwiseNot:
691 preString = "(~";
692 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000693
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500694 case EOpPostIncrement:
695 preString = "(";
696 postString = "++)";
697 break;
698 case EOpPostDecrement:
699 preString = "(";
700 postString = "--)";
701 break;
702 case EOpPreIncrement:
703 preString = "(++";
704 break;
705 case EOpPreDecrement:
706 preString = "(--";
707 break;
Olli Etuahobb5a7e22017-08-30 13:03:12 +0300708 case EOpArrayLength:
709 preString = "((";
710 postString = ").length())";
711 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000712
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500713 case EOpRadians:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500714 case EOpDegrees:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500715 case EOpSin:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500716 case EOpCos:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500717 case EOpTan:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500718 case EOpAsin:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500719 case EOpAcos:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500720 case EOpAtan:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500721 case EOpSinh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500722 case EOpCosh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500723 case EOpTanh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500724 case EOpAsinh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500725 case EOpAcosh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500726 case EOpAtanh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500727 case EOpExp:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500728 case EOpLog:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500729 case EOpExp2:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500730 case EOpLog2:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500731 case EOpSqrt:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500732 case EOpInverseSqrt:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500733 case EOpAbs:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500734 case EOpSign:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500735 case EOpFloor:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500736 case EOpTrunc:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500737 case EOpRound:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500738 case EOpRoundEven:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500739 case EOpCeil:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500740 case EOpFract:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500741 case EOpIsNan:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500742 case EOpIsInf:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500743 case EOpFloatBitsToInt:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500744 case EOpFloatBitsToUint:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500745 case EOpIntBitsToFloat:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500746 case EOpUintBitsToFloat:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500747 case EOpPackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500748 case EOpPackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500749 case EOpPackHalf2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500750 case EOpUnpackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500751 case EOpUnpackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500752 case EOpUnpackHalf2x16:
Olli Etuaho25aef452017-01-29 16:15:44 -0800753 case EOpPackUnorm4x8:
754 case EOpPackSnorm4x8:
755 case EOpUnpackUnorm4x8:
756 case EOpUnpackSnorm4x8:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500757 case EOpLength:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500758 case EOpNormalize:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500759 case EOpDFdx:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500760 case EOpDFdy:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500761 case EOpFwidth:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500762 case EOpTranspose:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500763 case EOpDeterminant:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500764 case EOpInverse:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500765 case EOpAny:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500766 case EOpAll:
Olli Etuahod68924e2017-01-02 17:34:40 +0000767 case EOpLogicalNotComponentWise:
Olli Etuaho9250cb22017-01-21 10:51:27 +0000768 case EOpBitfieldReverse:
769 case EOpBitCount:
770 case EOpFindLSB:
771 case EOpFindMSB:
Olli Etuahod68924e2017-01-02 17:34:40 +0000772 writeBuiltInFunctionTriplet(visit, node->getOp(), node->getUseEmulatedFunction());
773 return true;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500774 default:
775 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000776 }
777
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800778 writeTriplet(visit, preString.c_str(), nullptr, postString.c_str());
zmo@google.com32e97312011-08-24 01:03:11 +0000779
zmo@google.com5601ea02011-06-10 18:23:25 +0000780 return true;
781}
782
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300783bool TOutputGLSLBase::visitTernary(Visit visit, TIntermTernary *node)
784{
785 TInfoSinkBase &out = objSink();
786 // Notice two brackets at the beginning and end. The outer ones
787 // encapsulate the whole ternary expression. This preserves the
788 // order of precedence when ternary expressions are used in a
789 // compound expression, i.e., c = 2 * (a < b ? 1 : 2).
790 out << "((";
791 node->getCondition()->traverse(this);
792 out << ") ? (";
793 node->getTrueExpression()->traverse(this);
794 out << ") : (";
795 node->getFalseExpression()->traverse(this);
796 out << "))";
797 return false;
798}
799
Olli Etuaho57961272016-09-14 13:57:46 +0300800bool TOutputGLSLBase::visitIfElse(Visit visit, TIntermIfElse *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000801{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700802 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000803
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300804 out << "if (";
805 node->getCondition()->traverse(this);
806 out << ")\n";
zmo@google.com5601ea02011-06-10 18:23:25 +0000807
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300808 visitCodeBlock(node->getTrueBlock());
zmo@google.com5601ea02011-06-10 18:23:25 +0000809
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300810 if (node->getFalseBlock())
811 {
812 out << "else\n";
813 visitCodeBlock(node->getFalseBlock());
zmo@google.com5601ea02011-06-10 18:23:25 +0000814 }
815 return false;
816}
817
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200818bool TOutputGLSLBase::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200819{
Olli Etuaho923ecef2017-10-11 12:01:38 +0300820 ASSERT(node->getStatementList());
821 writeTriplet(visit, "switch (", ") ", nullptr);
822 // The curly braces get written when visiting the statementList aggregate
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200823 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200824}
825
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200826bool TOutputGLSLBase::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200827{
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200828 if (node->hasCondition())
829 {
830 writeTriplet(visit, "case (", nullptr, "):\n");
831 return true;
832 }
833 else
834 {
835 TInfoSinkBase &out = objSink();
836 out << "default:\n";
837 return false;
838 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200839}
840
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100841bool TOutputGLSLBase::visitBlock(Visit visit, TIntermBlock *node)
842{
843 TInfoSinkBase &out = objSink();
844 // Scope the blocks except when at the global scope.
845 if (mDepth > 0)
846 {
847 out << "{\n";
848 }
849
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100850 for (TIntermSequence::const_iterator iter = node->getSequence()->begin();
851 iter != node->getSequence()->end(); ++iter)
852 {
853 TIntermNode *curNode = *iter;
854 ASSERT(curNode != nullptr);
855 curNode->traverse(this);
856
857 if (isSingleStatement(curNode))
858 out << ";\n";
859 }
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100860
861 // Scope the blocks except when at the global scope.
862 if (mDepth > 0)
863 {
864 out << "}\n";
865 }
866 return false;
867}
868
Olli Etuaho336b1472016-10-05 16:37:55 +0100869bool TOutputGLSLBase::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
870{
Olli Etuaho8ad9e752017-01-16 19:55:20 +0000871 TIntermFunctionPrototype *prototype = node->getFunctionPrototype();
872 prototype->traverse(this);
Olli Etuaho336b1472016-10-05 16:37:55 +0100873 visitCodeBlock(node->getBody());
Olli Etuaho336b1472016-10-05 16:37:55 +0100874
875 // Fully processed; no need to visit children.
876 return false;
877}
878
Olli Etuahobf4e1b72016-12-09 11:30:15 +0000879bool TOutputGLSLBase::visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node)
880{
881 TInfoSinkBase &out = objSink();
882 ASSERT(visit == PreVisit);
883 const TIntermSymbol *symbol = node->getSymbol();
884 out << "invariant " << hashVariableName(symbol->getName());
885 return false;
886}
887
Olli Etuaho16c745a2017-01-16 17:02:27 +0000888bool TOutputGLSLBase::visitFunctionPrototype(Visit visit, TIntermFunctionPrototype *node)
889{
890 TInfoSinkBase &out = objSink();
891 ASSERT(visit == PreVisit);
892
893 const TType &type = node->getType();
894 writeVariableType(type);
895 if (type.isArray())
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300896 out << ArrayString(type);
Olli Etuaho16c745a2017-01-16 17:02:27 +0000897
Olli Etuahoec9232b2017-03-27 17:01:37 +0300898 out << " " << hashFunctionNameIfNeeded(*node->getFunctionSymbolInfo());
Olli Etuaho16c745a2017-01-16 17:02:27 +0000899
900 out << "(";
901 writeFunctionParameters(*(node->getSequence()));
902 out << ")";
903
904 return false;
905}
906
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700907bool TOutputGLSLBase::visitAggregate(Visit visit, TIntermAggregate *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000908{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500909 bool visitChildren = true;
910 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000911 switch (node->getOp())
912 {
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800913 case EOpCallFunctionInAST:
914 case EOpCallInternalRawFunction:
915 case EOpCallBuiltInFunction:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500916 // Function call.
917 if (visit == PreVisit)
Olli Etuahoec9232b2017-03-27 17:01:37 +0300918 {
919 if (node->getOp() == EOpCallBuiltInFunction)
920 {
921 out << translateTextureFunction(node->getFunctionSymbolInfo()->getName());
922 }
923 else
924 {
925 out << hashFunctionNameIfNeeded(*node->getFunctionSymbolInfo());
926 }
927 out << "(";
928 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500929 else if (visit == InVisit)
930 out << ", ";
931 else
932 out << ")";
933 break;
Olli Etuaho8fab3202017-05-08 18:22:22 +0300934 case EOpConstruct:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500935 writeConstructorTriplet(visit, node->getType());
936 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +0200937
Olli Etuahoe1805592017-01-02 16:41:20 +0000938 case EOpEqualComponentWise:
939 case EOpNotEqualComponentWise:
940 case EOpLessThanComponentWise:
941 case EOpGreaterThanComponentWise:
942 case EOpLessThanEqualComponentWise:
943 case EOpGreaterThanEqualComponentWise:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500944 case EOpMod:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500945 case EOpModf:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500946 case EOpPow:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500947 case EOpAtan:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500948 case EOpMin:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500949 case EOpMax:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500950 case EOpClamp:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500951 case EOpMix:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500952 case EOpStep:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500953 case EOpSmoothStep:
Olli Etuaho74da73f2017-02-01 15:37:48 +0000954 case EOpFrexp:
955 case EOpLdexp:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500956 case EOpDistance:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500957 case EOpDot:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500958 case EOpCross:
Jamie Madille72595b2017-06-06 15:12:26 -0400959 case EOpFaceforward:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500960 case EOpReflect:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500961 case EOpRefract:
Olli Etuahoe1805592017-01-02 16:41:20 +0000962 case EOpMulMatrixComponentWise:
963 case EOpOuterProduct:
Olli Etuaho9250cb22017-01-21 10:51:27 +0000964 case EOpBitfieldExtract:
965 case EOpBitfieldInsert:
966 case EOpUaddCarry:
967 case EOpUsubBorrow:
968 case EOpUmulExtended:
969 case EOpImulExtended:
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300970 case EOpBarrier:
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300971 case EOpMemoryBarrier:
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300972 case EOpMemoryBarrierAtomicCounter:
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300973 case EOpMemoryBarrierBuffer:
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300974 case EOpMemoryBarrierImage:
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300975 case EOpMemoryBarrierShared:
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300976 case EOpGroupMemoryBarrier:
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800977 case EOpEmitVertex:
978 case EOpEndPrimitive:
Olli Etuahod68924e2017-01-02 17:34:40 +0000979 writeBuiltInFunctionTriplet(visit, node->getOp(), node->getUseEmulatedFunction());
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300980 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500981 default:
982 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000983 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000984 return visitChildren;
985}
986
Olli Etuaho13389b62016-10-16 11:48:18 +0100987bool TOutputGLSLBase::visitDeclaration(Visit visit, TIntermDeclaration *node)
988{
989 TInfoSinkBase &out = objSink();
990
991 // Variable declaration.
992 if (visit == PreVisit)
993 {
994 const TIntermSequence &sequence = *(node->getSequence());
Jamie Madill2a9e1072017-09-22 11:31:57 -0400995 TIntermTyped *variable = sequence.front()->getAsTyped();
996 writeLayoutQualifier(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +0100997 writeVariableType(variable->getType());
Olli Etuaho39f74df2017-11-20 16:09:57 +0200998 if (variable->getAsSymbolNode() == nullptr ||
999 !variable->getAsSymbolNode()->getSymbol().empty())
1000 {
1001 out << " ";
1002 }
1003 mDeclaringVariable = true;
Olli Etuaho13389b62016-10-16 11:48:18 +01001004 }
1005 else if (visit == InVisit)
1006 {
Olli Etuaho39f74df2017-11-20 16:09:57 +02001007 UNREACHABLE();
Olli Etuaho13389b62016-10-16 11:48:18 +01001008 }
1009 else
1010 {
Olli Etuaho39f74df2017-11-20 16:09:57 +02001011 mDeclaringVariable = false;
Olli Etuaho13389b62016-10-16 11:48:18 +01001012 }
1013 return true;
1014}
1015
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001016bool TOutputGLSLBase::visitLoop(Visit visit, TIntermLoop *node)
zmo@google.com5601ea02011-06-10 18:23:25 +00001017{
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001018 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +00001019
zmo@google.com5601ea02011-06-10 18:23:25 +00001020 TLoopType loopType = node->getType();
Corentin Wallez7258e302015-09-22 10:40:24 -07001021
zmo@google.com5601ea02011-06-10 18:23:25 +00001022 if (loopType == ELoopFor) // for loop
1023 {
Corentin Wallez1b896c62016-11-16 13:10:44 -05001024 out << "for (";
1025 if (node->getInit())
1026 node->getInit()->traverse(this);
1027 out << "; ";
zmo@google.com5601ea02011-06-10 18:23:25 +00001028
Corentin Wallez1b896c62016-11-16 13:10:44 -05001029 if (node->getCondition())
1030 node->getCondition()->traverse(this);
1031 out << "; ";
zmo@google.com5601ea02011-06-10 18:23:25 +00001032
Corentin Wallez1b896c62016-11-16 13:10:44 -05001033 if (node->getExpression())
1034 node->getExpression()->traverse(this);
1035 out << ")\n";
Corentin Wallez7258e302015-09-22 10:40:24 -07001036
Corentin Wallez1b896c62016-11-16 13:10:44 -05001037 visitCodeBlock(node->getBody());
zmo@google.com5601ea02011-06-10 18:23:25 +00001038 }
1039 else if (loopType == ELoopWhile) // while loop
1040 {
1041 out << "while (";
Yunchao He4f285442017-04-21 12:15:49 +08001042 ASSERT(node->getCondition() != nullptr);
zmo@google.com5601ea02011-06-10 18:23:25 +00001043 node->getCondition()->traverse(this);
1044 out << ")\n";
Corentin Wallez7258e302015-09-22 10:40:24 -07001045
1046 visitCodeBlock(node->getBody());
zmo@google.com5601ea02011-06-10 18:23:25 +00001047 }
1048 else // do-while loop
1049 {
1050 ASSERT(loopType == ELoopDoWhile);
1051 out << "do\n";
zmo@google.com5601ea02011-06-10 18:23:25 +00001052
zmo@google.com5601ea02011-06-10 18:23:25 +00001053 visitCodeBlock(node->getBody());
zmo@google.com5601ea02011-06-10 18:23:25 +00001054
zmo@google.com5601ea02011-06-10 18:23:25 +00001055 out << "while (";
Yunchao He4f285442017-04-21 12:15:49 +08001056 ASSERT(node->getCondition() != nullptr);
zmo@google.com5601ea02011-06-10 18:23:25 +00001057 node->getCondition()->traverse(this);
1058 out << ");\n";
1059 }
Corentin Wallez7258e302015-09-22 10:40:24 -07001060
zmo@google.com5601ea02011-06-10 18:23:25 +00001061 // No need to visit children. They have been already processed in
1062 // this function.
1063 return false;
1064}
1065
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001066bool TOutputGLSLBase::visitBranch(Visit visit, TIntermBranch *node)
zmo@google.com5601ea02011-06-10 18:23:25 +00001067{
1068 switch (node->getFlowOp())
1069 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001070 case EOpKill:
Yunchao Hef81ce4a2017-04-24 10:49:17 +08001071 writeTriplet(visit, "discard", nullptr, nullptr);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001072 break;
1073 case EOpBreak:
Yunchao Hef81ce4a2017-04-24 10:49:17 +08001074 writeTriplet(visit, "break", nullptr, nullptr);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001075 break;
1076 case EOpContinue:
Yunchao Hef81ce4a2017-04-24 10:49:17 +08001077 writeTriplet(visit, "continue", nullptr, nullptr);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001078 break;
1079 case EOpReturn:
Yunchao Hef81ce4a2017-04-24 10:49:17 +08001080 writeTriplet(visit, "return ", nullptr, nullptr);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001081 break;
1082 default:
1083 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +00001084 }
1085
1086 return true;
1087}
1088
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001089void TOutputGLSLBase::visitCodeBlock(TIntermBlock *node)
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001090{
zmo@google.com5601ea02011-06-10 18:23:25 +00001091 TInfoSinkBase &out = objSink();
Yunchao He4f285442017-04-21 12:15:49 +08001092 if (node != nullptr)
zmo@google.com5601ea02011-06-10 18:23:25 +00001093 {
1094 node->traverse(this);
1095 // Single statements not part of a sequence need to be terminated
1096 // with semi-colon.
1097 if (isSingleStatement(node))
1098 out << ";\n";
1099 }
1100 else
1101 {
1102 out << "{\n}\n"; // Empty code block.
1103 }
1104}
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001105
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001106TString TOutputGLSLBase::getTypeName(const TType &type)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001107{
Jamie Madill6276b922017-09-25 02:35:57 -04001108 return GetTypeName(type, mHashFunction, &mNameMap);
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001109}
1110
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001111TString TOutputGLSLBase::hashName(const TName &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001112{
Olli Etuaho855d9642017-05-17 14:05:06 +03001113 return HashName(name, mHashFunction, &mNameMap);
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001114}
1115
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001116TString TOutputGLSLBase::hashVariableName(const TName &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001117{
Olli Etuaho855d9642017-05-17 14:05:06 +03001118 if (mSymbolTable->findBuiltIn(name.getString(), mShaderVersion) != nullptr ||
1119 name.getString().substr(0, 3) == "gl_")
Olli Etuaho09b04a22016-12-15 13:30:26 +00001120 {
1121 if (mCompileOptions & SH_TRANSLATE_VIEWID_OVR_TO_UNIFORM &&
1122 name.getString() == "gl_ViewID_OVR")
1123 {
Olli Etuaho2f90a9b2017-01-10 12:27:56 +00001124 TName uniformName(TString("ViewID_OVR"));
Olli Etuaho09b04a22016-12-15 13:30:26 +00001125 uniformName.setInternal(true);
1126 return hashName(uniformName);
1127 }
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001128 return name.getString();
Olli Etuaho09b04a22016-12-15 13:30:26 +00001129 }
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001130 return hashName(name);
1131}
1132
Olli Etuahoec9232b2017-03-27 17:01:37 +03001133TString TOutputGLSLBase::hashFunctionNameIfNeeded(const TFunctionSymbolInfo &info)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001134{
Olli Etuaho855d9642017-05-17 14:05:06 +03001135 if (info.isMain())
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001136 {
Olli Etuahoec9232b2017-03-27 17:01:37 +03001137 return info.getName();
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001138 }
Olli Etuaho59f9a642015-08-06 20:38:26 +03001139 else
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001140 {
Olli Etuahoec9232b2017-03-27 17:01:37 +03001141 return hashName(info.getNameObj());
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001142 }
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001143}
Jamie Madill98493dd2013-07-08 14:39:03 -04001144
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001145bool TOutputGLSLBase::structDeclared(const TStructure *structure) const
Jamie Madill98493dd2013-07-08 14:39:03 -04001146{
Zhenyao Mo904a9162014-05-09 14:07:45 -07001147 ASSERT(structure);
Jamie Madill01f85ac2014-06-06 11:55:04 -04001148 if (structure->name().empty())
Zhenyao Mo904a9162014-05-09 14:07:45 -07001149 {
Jamie Madill01f85ac2014-06-06 11:55:04 -04001150 return false;
Zhenyao Mo904a9162014-05-09 14:07:45 -07001151 }
Jamie Madill01f85ac2014-06-06 11:55:04 -04001152
1153 return (mDeclaredStructs.count(structure->uniqueId()) > 0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001154}
1155
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001156void TOutputGLSLBase::declareStruct(const TStructure *structure)
Jamie Madill98493dd2013-07-08 14:39:03 -04001157{
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001158 TInfoSinkBase &out = objSink();
Jamie Madill98493dd2013-07-08 14:39:03 -04001159
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001160 out << "struct " << hashName(TName(structure->name())) << "{\n";
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001161 const TFieldList &fields = structure->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04001162 for (size_t i = 0; i < fields.size(); ++i)
1163 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001164 const TField *field = fields[i];
Jamie Madill98493dd2013-07-08 14:39:03 -04001165 if (writeVariablePrecision(field->type()->getPrecision()))
1166 out << " ";
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001167 out << getTypeName(*field->type()) << " " << hashName(TName(field->name()));
Jamie Madill98493dd2013-07-08 14:39:03 -04001168 if (field->type()->isArray())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001169 out << ArrayString(*field->type());
Jamie Madill98493dd2013-07-08 14:39:03 -04001170 out << ";\n";
1171 }
1172 out << "}";
Zhenyao Mo904a9162014-05-09 14:07:45 -07001173}
Jamie Madill98493dd2013-07-08 14:39:03 -04001174
Geoff Langbdcc54a2015-09-02 13:09:48 -04001175void TOutputGLSLBase::declareInterfaceBlockLayout(const TInterfaceBlock *interfaceBlock)
1176{
1177 TInfoSinkBase &out = objSink();
1178
1179 out << "layout(";
1180
1181 switch (interfaceBlock->blockStorage())
1182 {
1183 case EbsUnspecified:
1184 case EbsShared:
1185 // Default block storage is shared.
1186 out << "shared";
1187 break;
1188
1189 case EbsPacked:
1190 out << "packed";
1191 break;
1192
1193 case EbsStd140:
1194 out << "std140";
1195 break;
1196
Qin Jiajiaca68d982017-09-18 16:41:56 +08001197 case EbsStd430:
1198 out << "std430";
1199 break;
1200
Geoff Langbdcc54a2015-09-02 13:09:48 -04001201 default:
1202 UNREACHABLE();
1203 break;
1204 }
1205
1206 out << ", ";
1207
Jiajia Qin729b2c62017-08-14 09:36:11 +08001208 if (interfaceBlock->blockBinding() > 0)
1209 {
1210 out << "binding = " << interfaceBlock->blockBinding();
1211 out << ", ";
1212 }
1213
Geoff Langbdcc54a2015-09-02 13:09:48 -04001214 switch (interfaceBlock->matrixPacking())
1215 {
1216 case EmpUnspecified:
1217 case EmpColumnMajor:
1218 // Default matrix packing is column major.
1219 out << "column_major";
1220 break;
1221
1222 case EmpRowMajor:
1223 out << "row_major";
1224 break;
1225
1226 default:
1227 UNREACHABLE();
1228 break;
1229 }
1230
1231 out << ") ";
1232}
1233
1234void TOutputGLSLBase::declareInterfaceBlock(const TInterfaceBlock *interfaceBlock)
1235{
1236 TInfoSinkBase &out = objSink();
1237
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001238 out << hashName(TName(interfaceBlock->name())) << "{\n";
Geoff Langbdcc54a2015-09-02 13:09:48 -04001239 const TFieldList &fields = interfaceBlock->fields();
1240 for (size_t i = 0; i < fields.size(); ++i)
1241 {
1242 const TField *field = fields[i];
1243 if (writeVariablePrecision(field->type()->getPrecision()))
1244 out << " ";
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001245 out << getTypeName(*field->type()) << " " << hashName(TName(field->name()));
Geoff Langbdcc54a2015-09-02 13:09:48 -04001246 if (field->type()->isArray())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001247 out << ArrayString(*field->type());
Geoff Langbdcc54a2015-09-02 13:09:48 -04001248 out << ";\n";
1249 }
1250 out << "}";
1251}
Jamie Madill45bcc782016-11-07 13:58:48 -05001252
Shaob5cc1192017-07-06 10:47:20 +08001253void WriteGeometryShaderLayoutQualifiers(TInfoSinkBase &out,
1254 sh::TLayoutPrimitiveType inputPrimitive,
1255 int invocations,
1256 sh::TLayoutPrimitiveType outputPrimitive,
1257 int maxVertices)
1258{
1259 // Omit 'invocations = 1'
1260 if (inputPrimitive != EptUndefined || invocations > 1)
1261 {
1262 out << "layout (";
1263
1264 if (inputPrimitive != EptUndefined)
1265 {
1266 out << getGeometryShaderPrimitiveTypeString(inputPrimitive);
1267 }
1268
1269 if (invocations > 1)
1270 {
1271 if (inputPrimitive != EptUndefined)
1272 {
1273 out << ", ";
1274 }
1275 out << "invocations = " << invocations;
1276 }
1277 out << ") in;\n";
1278 }
1279
1280 if (outputPrimitive != EptUndefined || maxVertices != -1)
1281 {
1282 out << "layout (";
1283
1284 if (outputPrimitive != EptUndefined)
1285 {
1286 out << getGeometryShaderPrimitiveTypeString(outputPrimitive);
1287 }
1288
1289 if (maxVertices != -1)
1290 {
1291 if (outputPrimitive != EptUndefined)
1292 {
1293 out << ", ";
1294 }
1295 out << "max_vertices = " << maxVertices;
1296 }
1297 out << ") out;\n";
1298 }
1299}
1300
Jamie Madill2a9e1072017-09-22 11:31:57 -04001301// If SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS is enabled, layout qualifiers are spilled whenever
1302// variables with specified layout qualifiers are copied. Additional checks are needed against the
1303// type and storage qualifier of the variable to verify that layout qualifiers have to be outputted.
1304// TODO (mradev): Fix layout qualifier spilling in ScalarizeVecAndMatConstructorArgs and remove
1305// NeedsToWriteLayoutQualifier.
1306bool NeedsToWriteLayoutQualifier(const TType &type)
1307{
1308 if (type.getBasicType() == EbtInterfaceBlock)
1309 {
1310 return false;
1311 }
1312
1313 const TLayoutQualifier &layoutQualifier = type.getLayoutQualifier();
1314
1315 if ((type.getQualifier() == EvqFragmentOut || type.getQualifier() == EvqVertexIn ||
1316 IsVarying(type.getQualifier())) &&
1317 layoutQualifier.location >= 0)
1318 {
1319 return true;
1320 }
1321
1322 if (type.getQualifier() == EvqFragmentOut && layoutQualifier.yuv == true)
1323 {
1324 return true;
1325 }
1326
1327 if (IsOpaqueType(type.getBasicType()) && layoutQualifier.binding != -1)
1328 {
1329 return true;
1330 }
1331
1332 if (IsImage(type.getBasicType()) && layoutQualifier.imageInternalFormat != EiifUnspecified)
1333 {
1334 return true;
1335 }
1336 return false;
1337}
1338
Jamie Madill45bcc782016-11-07 13:58:48 -05001339} // namespace sh