blob: c0c043733dedbd67e8a498ccd051e58068ce77b0 [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
Martin Radev2cc85b32016-08-05 16:22:53 +030052// If SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS is enabled, layout qualifiers are spilled whenever
53// variables with specified layout qualifiers are copied. Additional checks are needed against the
54// type and storage qualifier of the variable to verify that layout qualifiers have to be outputted.
55// TODO (mradev): Fix layout qualifier spilling in ScalarizeVecAndMatConstructorArgs and remove
56// NeedsToWriteLayoutQualifier.
57bool NeedsToWriteLayoutQualifier(const TType &type)
58{
59 if (type.getBasicType() == EbtInterfaceBlock)
60 {
61 return false;
62 }
63
64 const TLayoutQualifier &layoutQualifier = type.getLayoutQualifier();
65
66 if ((type.getQualifier() == EvqFragmentOut || type.getQualifier() == EvqVertexIn) &&
67 layoutQualifier.location >= 0)
68 {
69 return true;
70 }
Olli Etuaho43364892017-02-13 16:00:12 +000071
Andrei Volykhina5527072017-03-22 16:46:30 +030072 if (type.getQualifier() == EvqFragmentOut && layoutQualifier.yuv == true)
73 {
74 return true;
75 }
76
Olli Etuaho43364892017-02-13 16:00:12 +000077 if (IsOpaqueType(type.getBasicType()) && layoutQualifier.binding != -1)
78 {
79 return true;
80 }
81
Martin Radev2cc85b32016-08-05 16:22:53 +030082 if (IsImage(type.getBasicType()) && layoutQualifier.imageInternalFormat != EiifUnspecified)
83 {
84 return true;
85 }
86 return false;
87}
88
Olli Etuaho43364892017-02-13 16:00:12 +000089class CommaSeparatedListItemPrefixGenerator
90{
91 public:
92 CommaSeparatedListItemPrefixGenerator() : mFirst(true) {}
93 private:
94 bool mFirst;
95
96 friend TInfoSinkBase &operator<<(TInfoSinkBase &out,
97 CommaSeparatedListItemPrefixGenerator &gen);
98};
99
100TInfoSinkBase &operator<<(TInfoSinkBase &out, CommaSeparatedListItemPrefixGenerator &gen)
101{
102 if (gen.mFirst)
103 {
104 gen.mFirst = false;
105 }
106 else
107 {
108 out << ", ";
109 }
110 return out;
111}
112
zmo@google.com5601ea02011-06-10 18:23:25 +0000113} // namespace
114
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700115TOutputGLSLBase::TOutputGLSLBase(TInfoSinkBase &objSink,
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000116 ShArrayIndexClampingStrategy clampingStrategy,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000117 ShHashFunction64 hashFunction,
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700118 NameMap &nameMap,
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300119 TSymbolTable *symbolTable,
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000120 sh::GLenum shaderType,
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800121 int shaderVersion,
Qiankun Miao705a9192016-08-29 10:05:27 +0800122 ShShaderOutput output,
123 ShCompileOptions compileOptions)
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300124 : TIntermTraverser(true, true, true, symbolTable),
zmo@google.com5601ea02011-06-10 18:23:25 +0000125 mObjSink(objSink),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000126 mDeclaringVariables(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000127 mClampingStrategy(clampingStrategy),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000128 mHashFunction(hashFunction),
129 mNameMap(nameMap),
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000130 mShaderType(shaderType),
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800131 mShaderVersion(shaderVersion),
Qiankun Miao705a9192016-08-29 10:05:27 +0800132 mOutput(output),
133 mCompileOptions(compileOptions)
zmo@google.com5601ea02011-06-10 18:23:25 +0000134{
135}
136
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800137void TOutputGLSLBase::writeInvariantQualifier(const TType &type)
138{
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000139 if (!sh::RemoveInvariant(mShaderType, mShaderVersion, mOutput, mCompileOptions))
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800140 {
141 TInfoSinkBase &out = objSink();
142 out << "invariant ";
143 }
144}
145
Olli Etuaho56a2f952016-12-08 12:16:27 +0000146void TOutputGLSLBase::writeFloat(TInfoSinkBase &out, float f)
147{
148 if ((gl::isInf(f) || gl::isNaN(f)) && mShaderVersion >= 300)
149 {
150 out << "uintBitsToFloat(" << gl::bitCast<uint32_t>(f) << "u)";
151 }
152 else
153 {
154 out << std::min(FLT_MAX, std::max(-FLT_MAX, f));
155 }
156}
157
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500158void TOutputGLSLBase::writeTriplet(Visit visit,
159 const char *preStr,
160 const char *inStr,
161 const char *postStr)
zmo@google.com5601ea02011-06-10 18:23:25 +0000162{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700163 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000164 if (visit == PreVisit && preStr)
zmo@google.com5601ea02011-06-10 18:23:25 +0000165 out << preStr;
zmo@google.com5601ea02011-06-10 18:23:25 +0000166 else if (visit == InVisit && inStr)
zmo@google.com5601ea02011-06-10 18:23:25 +0000167 out << inStr;
zmo@google.com5601ea02011-06-10 18:23:25 +0000168 else if (visit == PostVisit && postStr)
zmo@google.com5601ea02011-06-10 18:23:25 +0000169 out << postStr;
zmo@google.com5601ea02011-06-10 18:23:25 +0000170}
171
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500172void TOutputGLSLBase::writeBuiltInFunctionTriplet(Visit visit,
Olli Etuahoe1805592017-01-02 16:41:20 +0000173 TOperator op,
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500174 bool useEmulatedFunction)
zmo@google.com5601ea02011-06-10 18:23:25 +0000175{
Olli Etuahod68924e2017-01-02 17:34:40 +0000176 TInfoSinkBase &out = objSink();
177 if (visit == PreVisit)
Olli Etuahoe1805592017-01-02 16:41:20 +0000178 {
Olli Etuahod68924e2017-01-02 17:34:40 +0000179 const char *opStr(GetOperatorString(op));
180 if (useEmulatedFunction)
181 {
182 BuiltInFunctionEmulator::WriteEmulatedFunctionName(out, opStr);
183 }
184 else
185 {
186 out << opStr;
187 }
188 out << "(";
Olli Etuahoe1805592017-01-02 16:41:20 +0000189 }
Olli Etuahod68924e2017-01-02 17:34:40 +0000190 else
191 {
192 writeTriplet(visit, nullptr, ", ", ")");
193 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700194}
195
Olli Etuahoae69d7e2015-10-07 17:19:50 +0300196void TOutputGLSLBase::writeLayoutQualifier(const TType &type)
197{
Martin Radev2cc85b32016-08-05 16:22:53 +0300198 if (!NeedsToWriteLayoutQualifier(type))
199 {
200 return;
201 }
202
203 TInfoSinkBase &out = objSink();
204 const TLayoutQualifier &layoutQualifier = type.getLayoutQualifier();
205 out << "layout(";
206
Olli Etuaho43364892017-02-13 16:00:12 +0000207 CommaSeparatedListItemPrefixGenerator listItemPrefix;
208
Olli Etuahoae69d7e2015-10-07 17:19:50 +0300209 if (type.getQualifier() == EvqFragmentOut || type.getQualifier() == EvqVertexIn)
210 {
Olli Etuahoae69d7e2015-10-07 17:19:50 +0300211 if (layoutQualifier.location >= 0)
212 {
Olli Etuaho43364892017-02-13 16:00:12 +0000213 out << listItemPrefix << "location = " << layoutQualifier.location;
Olli Etuahoae69d7e2015-10-07 17:19:50 +0300214 }
215 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300216
Andrei Volykhina5527072017-03-22 16:46:30 +0300217 if (type.getQualifier() == EvqFragmentOut)
218 {
219 if (layoutQualifier.yuv == true)
220 {
221 out << listItemPrefix << "yuv";
222 }
223 }
224
Olli Etuaho43364892017-02-13 16:00:12 +0000225 if (IsOpaqueType(type.getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +0300226 {
Olli Etuaho43364892017-02-13 16:00:12 +0000227 if (layoutQualifier.binding >= 0)
228 {
229 out << listItemPrefix << "binding = " << layoutQualifier.binding;
230 }
231 }
232
233 if (IsImage(type.getBasicType()))
234 {
235 if (layoutQualifier.imageInternalFormat != EiifUnspecified)
236 {
237 ASSERT(type.getQualifier() == EvqTemporary || type.getQualifier() == EvqUniform);
238 out << listItemPrefix
239 << getImageInternalFormatString(layoutQualifier.imageInternalFormat);
240 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300241 }
242
jchen1005c31da2017-07-18 16:11:39 +0800243 if (IsAtomicCounter(type.getBasicType()))
244 {
245 out << listItemPrefix << "offset = " << layoutQualifier.offset;
246 }
247
Martin Radev2cc85b32016-08-05 16:22:53 +0300248 out << ") ";
Olli Etuahoae69d7e2015-10-07 17:19:50 +0300249}
250
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800251const char *TOutputGLSLBase::mapQualifierToString(TQualifier qualifier)
252{
253 if (sh::IsGLSL410OrOlder(mOutput) && mShaderVersion >= 300 &&
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000254 (mCompileOptions & SH_REMOVE_INVARIANT_AND_CENTROID_FOR_ESSL3) != 0)
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800255 {
256 switch (qualifier)
257 {
258 // The return string is consistent with sh::getQualifierString() from
259 // BaseTypes.h minus the "centroid" keyword.
260 case EvqCentroid:
261 return "";
262 case EvqCentroidIn:
263 return "smooth in";
264 case EvqCentroidOut:
265 return "smooth out";
266 default:
267 break;
268 }
269 }
270 if (sh::IsGLSL130OrNewer(mOutput))
271 {
272 switch (qualifier)
273 {
274 case EvqAttribute:
275 return "in";
276 case EvqVaryingIn:
277 return "in";
278 case EvqVaryingOut:
279 return "out";
280 default:
281 break;
282 }
283 }
284 return sh::getQualifierString(qualifier);
285}
286
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700287void TOutputGLSLBase::writeVariableType(const TType &type)
288{
Qiankun Miao705a9192016-08-29 10:05:27 +0800289 TQualifier qualifier = type.getQualifier();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500290 TInfoSinkBase &out = objSink();
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800291 if (type.isInvariant())
Olli Etuaho214c2d82015-04-27 14:49:13 +0300292 {
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800293 writeInvariantQualifier(type);
Olli Etuaho214c2d82015-04-27 14:49:13 +0300294 }
Geoff Langbdcc54a2015-09-02 13:09:48 -0400295 if (type.getBasicType() == EbtInterfaceBlock)
296 {
297 TInterfaceBlock *interfaceBlock = type.getInterfaceBlock();
298 declareInterfaceBlockLayout(interfaceBlock);
299 }
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400300 if (qualifier != EvqTemporary && qualifier != EvqGlobal)
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400301 {
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800302 const char *qualifierString = mapQualifierToString(qualifier);
303 if (qualifierString && qualifierString[0] != '\0')
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800304 {
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800305 out << qualifierString << " ";
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800306 }
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400307 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300308
309 const TMemoryQualifier &memoryQualifier = type.getMemoryQualifier();
310 if (memoryQualifier.readonly)
311 {
312 ASSERT(IsImage(type.getBasicType()));
313 out << "readonly ";
314 }
315
316 if (memoryQualifier.writeonly)
317 {
318 ASSERT(IsImage(type.getBasicType()));
319 out << "writeonly ";
320 }
321
Martin Radev049edfa2016-11-11 14:35:37 +0200322 if (memoryQualifier.coherent)
323 {
324 ASSERT(IsImage(type.getBasicType()));
325 out << "coherent ";
326 }
327
328 if (memoryQualifier.restrictQualifier)
329 {
330 ASSERT(IsImage(type.getBasicType()));
331 out << "restrict ";
332 }
333
334 if (memoryQualifier.volatileQualifier)
335 {
336 ASSERT(IsImage(type.getBasicType()));
337 out << "volatile ";
338 }
339
zmo@google.com5601ea02011-06-10 18:23:25 +0000340 // Declare the struct if we have not done so already.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700341 if (type.getBasicType() == EbtStruct && !structDeclared(type.getStruct()))
zmo@google.com5601ea02011-06-10 18:23:25 +0000342 {
Jamie Madill01f85ac2014-06-06 11:55:04 -0400343 TStructure *structure = type.getStruct();
344
345 declareStruct(structure);
346
347 if (!structure->name().empty())
348 {
349 mDeclaredStructs.insert(structure->uniqueId());
350 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000351 }
Geoff Langbdcc54a2015-09-02 13:09:48 -0400352 else if (type.getBasicType() == EbtInterfaceBlock)
353 {
354 TInterfaceBlock *interfaceBlock = type.getInterfaceBlock();
355 declareInterfaceBlock(interfaceBlock);
356 }
zmo@google.com5601ea02011-06-10 18:23:25 +0000357 else
358 {
359 if (writeVariablePrecision(type.getPrecision()))
360 out << " ";
361 out << getTypeName(type);
362 }
363}
364
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700365void TOutputGLSLBase::writeFunctionParameters(const TIntermSequence &args)
zmo@google.com5601ea02011-06-10 18:23:25 +0000366{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700367 TInfoSinkBase &out = objSink();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500368 for (TIntermSequence::const_iterator iter = args.begin(); iter != args.end(); ++iter)
zmo@google.com5601ea02011-06-10 18:23:25 +0000369 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700370 const TIntermSymbol *arg = (*iter)->getAsSymbolNode();
Yunchao He4f285442017-04-21 12:15:49 +0800371 ASSERT(arg != nullptr);
zmo@google.com5601ea02011-06-10 18:23:25 +0000372
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700373 const TType &type = arg->getType();
zmo@google.com189be2f2011-06-16 18:28:53 +0000374 writeVariableType(type);
zmo@google.com5601ea02011-06-10 18:23:25 +0000375
Olli Etuaho0982a2b2016-11-01 15:13:46 +0000376 if (!arg->getName().getString().empty())
377 out << " " << hashName(arg->getName());
zmo@google.com5601ea02011-06-10 18:23:25 +0000378 if (type.isArray())
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300379 out << ArrayString(type);
zmo@google.com5601ea02011-06-10 18:23:25 +0000380
381 // Put a comma if this is not the last argument.
382 if (iter != args.end() - 1)
383 out << ", ";
384 }
385}
386
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500387const TConstantUnion *TOutputGLSLBase::writeConstantUnion(const TType &type,
388 const TConstantUnion *pConstUnion)
zmo@google.com5601ea02011-06-10 18:23:25 +0000389{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700390 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000391
392 if (type.getBasicType() == EbtStruct)
393 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700394 const TStructure *structure = type.getStruct();
Olli Etuaho0982a2b2016-11-01 15:13:46 +0000395 out << hashName(TName(structure->name())) << "(";
Jamie Madill98493dd2013-07-08 14:39:03 -0400396
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700397 const TFieldList &fields = structure->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -0400398 for (size_t i = 0; i < fields.size(); ++i)
zmo@google.com5601ea02011-06-10 18:23:25 +0000399 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700400 const TType *fieldType = fields[i]->type();
Yunchao He4f285442017-04-21 12:15:49 +0800401 ASSERT(fieldType != nullptr);
zmo@google.com5601ea02011-06-10 18:23:25 +0000402 pConstUnion = writeConstantUnion(*fieldType, pConstUnion);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700403 if (i != fields.size() - 1)
404 out << ", ";
zmo@google.com5601ea02011-06-10 18:23:25 +0000405 }
406 out << ")";
407 }
408 else
409 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500410 size_t size = type.getObjectSize();
zmo@google.com5601ea02011-06-10 18:23:25 +0000411 bool writeType = size > 1;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700412 if (writeType)
413 out << getTypeName(type) << "(";
Jamie Madill94bf7f22013-07-08 13:31:15 -0400414 for (size_t i = 0; i < size; ++i, ++pConstUnion)
zmo@google.com5601ea02011-06-10 18:23:25 +0000415 {
416 switch (pConstUnion->getType())
417 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500418 case EbtFloat:
419 writeFloat(out, pConstUnion->getFConst());
420 break;
421 case EbtInt:
422 out << pConstUnion->getIConst();
423 break;
424 case EbtUInt:
425 out << pConstUnion->getUConst() << "u";
426 break;
427 case EbtBool:
428 out << pConstUnion->getBConst();
429 break;
Andrei Volykhina5527072017-03-22 16:46:30 +0300430 case EbtYuvCscStandardEXT:
431 out << getYuvCscStandardEXTString(pConstUnion->getYuvCscStandardEXTConst());
432 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500433 default:
434 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000435 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700436 if (i != size - 1)
437 out << ", ";
zmo@google.com5601ea02011-06-10 18:23:25 +0000438 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700439 if (writeType)
440 out << ")";
zmo@google.com5601ea02011-06-10 18:23:25 +0000441 }
442 return pConstUnion;
443}
444
Olli Etuahoe92507b2016-07-04 11:20:10 +0300445void TOutputGLSLBase::writeConstructorTriplet(Visit visit, const TType &type)
Olli Etuahof40319e2015-03-10 14:33:00 +0200446{
447 TInfoSinkBase &out = objSink();
448 if (visit == PreVisit)
449 {
450 if (type.isArray())
451 {
Olli Etuahoe92507b2016-07-04 11:20:10 +0300452 out << getTypeName(type);
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300453 out << ArrayString(type);
Olli Etuahof40319e2015-03-10 14:33:00 +0200454 out << "(";
455 }
456 else
457 {
Olli Etuahoe92507b2016-07-04 11:20:10 +0300458 out << getTypeName(type) << "(";
Olli Etuahof40319e2015-03-10 14:33:00 +0200459 }
460 }
461 else
462 {
463 writeTriplet(visit, nullptr, ", ", ")");
464 }
465}
466
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700467void TOutputGLSLBase::visitSymbol(TIntermSymbol *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000468{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700469 TInfoSinkBase &out = objSink();
Corentin Wallez1b896c62016-11-16 13:10:44 -0500470 out << hashVariableName(node->getName());
zmo@google.com5601ea02011-06-10 18:23:25 +0000471
472 if (mDeclaringVariables && node->getType().isArray())
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300473 out << ArrayString(node->getType());
zmo@google.com5601ea02011-06-10 18:23:25 +0000474}
475
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700476void TOutputGLSLBase::visitConstantUnion(TIntermConstantUnion *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000477{
478 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
479}
480
Olli Etuahob6fa0432016-09-28 16:28:05 +0100481bool TOutputGLSLBase::visitSwizzle(Visit visit, TIntermSwizzle *node)
482{
483 TInfoSinkBase &out = objSink();
484 if (visit == PostVisit)
485 {
486 out << ".";
487 node->writeOffsetsAsXYZW(&out);
488 }
489 return true;
490}
491
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700492bool TOutputGLSLBase::visitBinary(Visit visit, TIntermBinary *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000493{
494 bool visitChildren = true;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700495 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000496 switch (node->getOp())
497 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100498 case EOpComma:
499 writeTriplet(visit, "(", ", ", ")");
500 break;
501 case EOpInitialize:
zmo@google.com5601ea02011-06-10 18:23:25 +0000502 if (visit == InVisit)
503 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100504 out << " = ";
505 // RHS of initialize is not being declared.
506 mDeclaringVariables = false;
zmo@google.com5601ea02011-06-10 18:23:25 +0000507 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100508 break;
509 case EOpAssign:
510 writeTriplet(visit, "(", " = ", ")");
511 break;
512 case EOpAddAssign:
513 writeTriplet(visit, "(", " += ", ")");
514 break;
515 case EOpSubAssign:
516 writeTriplet(visit, "(", " -= ", ")");
517 break;
518 case EOpDivAssign:
519 writeTriplet(visit, "(", " /= ", ")");
520 break;
521 case EOpIModAssign:
522 writeTriplet(visit, "(", " %= ", ")");
523 break;
524 // Notice the fall-through.
525 case EOpMulAssign:
526 case EOpVectorTimesMatrixAssign:
527 case EOpVectorTimesScalarAssign:
528 case EOpMatrixTimesScalarAssign:
529 case EOpMatrixTimesMatrixAssign:
530 writeTriplet(visit, "(", " *= ", ")");
531 break;
532 case EOpBitShiftLeftAssign:
533 writeTriplet(visit, "(", " <<= ", ")");
534 break;
535 case EOpBitShiftRightAssign:
536 writeTriplet(visit, "(", " >>= ", ")");
537 break;
538 case EOpBitwiseAndAssign:
539 writeTriplet(visit, "(", " &= ", ")");
540 break;
541 case EOpBitwiseXorAssign:
542 writeTriplet(visit, "(", " ^= ", ")");
543 break;
544 case EOpBitwiseOrAssign:
545 writeTriplet(visit, "(", " |= ", ")");
546 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000547
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100548 case EOpIndexDirect:
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800549 writeTriplet(visit, nullptr, "[", "]");
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100550 break;
551 case EOpIndexIndirect:
552 if (node->getAddIndexClamp())
553 {
554 if (visit == InVisit)
555 {
556 if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC)
557 out << "[int(clamp(float(";
558 else
559 out << "[webgl_int_clamp(";
560 }
561 else if (visit == PostVisit)
562 {
563 int maxSize;
564 TIntermTyped *left = node->getLeft();
565 TType leftType = left->getType();
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700566
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100567 if (left->isArray())
568 {
569 // The shader will fail validation if the array length is not > 0.
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300570 maxSize = static_cast<int>(leftType.getOutermostArraySize()) - 1;
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100571 }
572 else
573 {
574 maxSize = leftType.getNominalSize() - 1;
575 }
576
577 if (mClampingStrategy == SH_CLAMP_WITH_CLAMP_INTRINSIC)
578 out << "), 0.0, float(" << maxSize << ")))]";
579 else
580 out << ", 0, " << maxSize << ")]";
581 }
582 }
583 else
584 {
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800585 writeTriplet(visit, nullptr, "[", "]");
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100586 }
587 break;
588 case EOpIndexDirectStruct:
589 if (visit == InVisit)
590 {
591 // Here we are writing out "foo.bar", where "foo" is struct
592 // and "bar" is field. In AST, it is represented as a binary
593 // node, where left child represents "foo" and right child "bar".
594 // The node itself represents ".". The struct field "bar" is
595 // actually stored as an index into TStructure::fields.
596 out << ".";
597 const TStructure *structure = node->getLeft()->getType().getStruct();
598 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
599 const TField *field = structure->fields()[index->getIConst(0)];
600
601 TString fieldName = field->name();
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300602 if (!mSymbolTable->findBuiltIn(structure->name(), mShaderVersion))
Olli Etuaho0982a2b2016-11-01 15:13:46 +0000603 fieldName = hashName(TName(fieldName));
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100604
605 out << fieldName;
606 visitChildren = false;
607 }
608 break;
609 case EOpIndexDirectInterfaceBlock:
610 if (visit == InVisit)
611 {
612 out << ".";
613 const TInterfaceBlock *interfaceBlock =
614 node->getLeft()->getType().getInterfaceBlock();
615 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
616 const TField *field = interfaceBlock->fields()[index->getIConst(0)];
617
618 TString fieldName = field->name();
Jiawei Shaod8105a02017-08-08 09:54:36 +0800619 ASSERT(!mSymbolTable->findBuiltIn(interfaceBlock->name(), mShaderVersion) ||
620 interfaceBlock->name() == "gl_PerVertex");
Olli Etuaho0982a2b2016-11-01 15:13:46 +0000621 fieldName = hashName(TName(fieldName));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700622
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100623 out << fieldName;
624 visitChildren = false;
625 }
626 break;
Geoff Lang6e360422015-09-02 15:54:36 -0400627
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100628 case EOpAdd:
629 writeTriplet(visit, "(", " + ", ")");
630 break;
631 case EOpSub:
632 writeTriplet(visit, "(", " - ", ")");
633 break;
634 case EOpMul:
635 writeTriplet(visit, "(", " * ", ")");
636 break;
637 case EOpDiv:
638 writeTriplet(visit, "(", " / ", ")");
639 break;
640 case EOpIMod:
641 writeTriplet(visit, "(", " % ", ")");
642 break;
643 case EOpBitShiftLeft:
644 writeTriplet(visit, "(", " << ", ")");
645 break;
646 case EOpBitShiftRight:
647 writeTriplet(visit, "(", " >> ", ")");
648 break;
649 case EOpBitwiseAnd:
650 writeTriplet(visit, "(", " & ", ")");
651 break;
652 case EOpBitwiseXor:
653 writeTriplet(visit, "(", " ^ ", ")");
654 break;
655 case EOpBitwiseOr:
656 writeTriplet(visit, "(", " | ", ")");
657 break;
Geoff Lang6e360422015-09-02 15:54:36 -0400658
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100659 case EOpEqual:
660 writeTriplet(visit, "(", " == ", ")");
661 break;
662 case EOpNotEqual:
663 writeTriplet(visit, "(", " != ", ")");
664 break;
665 case EOpLessThan:
666 writeTriplet(visit, "(", " < ", ")");
667 break;
668 case EOpGreaterThan:
669 writeTriplet(visit, "(", " > ", ")");
670 break;
671 case EOpLessThanEqual:
672 writeTriplet(visit, "(", " <= ", ")");
673 break;
674 case EOpGreaterThanEqual:
675 writeTriplet(visit, "(", " >= ", ")");
676 break;
daniel@transgaming.com97b16d12013-02-01 03:20:42 +0000677
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100678 // Notice the fall-through.
679 case EOpVectorTimesScalar:
680 case EOpVectorTimesMatrix:
681 case EOpMatrixTimesVector:
682 case EOpMatrixTimesScalar:
683 case EOpMatrixTimesMatrix:
684 writeTriplet(visit, "(", " * ", ")");
685 break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200686
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100687 case EOpLogicalOr:
688 writeTriplet(visit, "(", " || ", ")");
689 break;
690 case EOpLogicalXor:
691 writeTriplet(visit, "(", " ^^ ", ")");
692 break;
693 case EOpLogicalAnd:
694 writeTriplet(visit, "(", " && ", ")");
695 break;
696 default:
697 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000698 }
699
700 return visitChildren;
701}
702
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700703bool TOutputGLSLBase::visitUnary(Visit visit, TIntermUnary *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000704{
zmo@google.com32e97312011-08-24 01:03:11 +0000705 TString preString;
706 TString postString = ")";
707
zmo@google.com5601ea02011-06-10 18:23:25 +0000708 switch (node->getOp())
709 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500710 case EOpNegative:
711 preString = "(-";
712 break;
713 case EOpPositive:
714 preString = "(+";
715 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500716 case EOpLogicalNot:
717 preString = "(!";
718 break;
719 case EOpBitwiseNot:
720 preString = "(~";
721 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000722
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500723 case EOpPostIncrement:
724 preString = "(";
725 postString = "++)";
726 break;
727 case EOpPostDecrement:
728 preString = "(";
729 postString = "--)";
730 break;
731 case EOpPreIncrement:
732 preString = "(++";
733 break;
734 case EOpPreDecrement:
735 preString = "(--";
736 break;
zmo@google.com5601ea02011-06-10 18:23:25 +0000737
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500738 case EOpRadians:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500739 case EOpDegrees:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500740 case EOpSin:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500741 case EOpCos:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500742 case EOpTan:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500743 case EOpAsin:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500744 case EOpAcos:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500745 case EOpAtan:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500746 case EOpSinh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500747 case EOpCosh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500748 case EOpTanh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500749 case EOpAsinh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500750 case EOpAcosh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500751 case EOpAtanh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500752 case EOpExp:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500753 case EOpLog:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500754 case EOpExp2:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500755 case EOpLog2:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500756 case EOpSqrt:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500757 case EOpInverseSqrt:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500758 case EOpAbs:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500759 case EOpSign:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500760 case EOpFloor:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500761 case EOpTrunc:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500762 case EOpRound:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500763 case EOpRoundEven:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500764 case EOpCeil:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500765 case EOpFract:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500766 case EOpIsNan:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500767 case EOpIsInf:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500768 case EOpFloatBitsToInt:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500769 case EOpFloatBitsToUint:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500770 case EOpIntBitsToFloat:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500771 case EOpUintBitsToFloat:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500772 case EOpPackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500773 case EOpPackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500774 case EOpPackHalf2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500775 case EOpUnpackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500776 case EOpUnpackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500777 case EOpUnpackHalf2x16:
Olli Etuaho25aef452017-01-29 16:15:44 -0800778 case EOpPackUnorm4x8:
779 case EOpPackSnorm4x8:
780 case EOpUnpackUnorm4x8:
781 case EOpUnpackSnorm4x8:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500782 case EOpLength:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500783 case EOpNormalize:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500784 case EOpDFdx:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500785 case EOpDFdy:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500786 case EOpFwidth:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500787 case EOpTranspose:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500788 case EOpDeterminant:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500789 case EOpInverse:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500790 case EOpAny:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500791 case EOpAll:
Olli Etuahod68924e2017-01-02 17:34:40 +0000792 case EOpLogicalNotComponentWise:
Olli Etuaho9250cb22017-01-21 10:51:27 +0000793 case EOpBitfieldReverse:
794 case EOpBitCount:
795 case EOpFindLSB:
796 case EOpFindMSB:
Olli Etuahod68924e2017-01-02 17:34:40 +0000797 writeBuiltInFunctionTriplet(visit, node->getOp(), node->getUseEmulatedFunction());
798 return true;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500799 default:
800 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +0000801 }
802
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800803 writeTriplet(visit, preString.c_str(), nullptr, postString.c_str());
zmo@google.com32e97312011-08-24 01:03:11 +0000804
zmo@google.com5601ea02011-06-10 18:23:25 +0000805 return true;
806}
807
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300808bool TOutputGLSLBase::visitTernary(Visit visit, TIntermTernary *node)
809{
810 TInfoSinkBase &out = objSink();
811 // Notice two brackets at the beginning and end. The outer ones
812 // encapsulate the whole ternary expression. This preserves the
813 // order of precedence when ternary expressions are used in a
814 // compound expression, i.e., c = 2 * (a < b ? 1 : 2).
815 out << "((";
816 node->getCondition()->traverse(this);
817 out << ") ? (";
818 node->getTrueExpression()->traverse(this);
819 out << ") : (";
820 node->getFalseExpression()->traverse(this);
821 out << "))";
822 return false;
823}
824
Olli Etuaho57961272016-09-14 13:57:46 +0300825bool TOutputGLSLBase::visitIfElse(Visit visit, TIntermIfElse *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000826{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700827 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000828
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300829 out << "if (";
830 node->getCondition()->traverse(this);
831 out << ")\n";
zmo@google.com5601ea02011-06-10 18:23:25 +0000832
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300833 visitCodeBlock(node->getTrueBlock());
zmo@google.com5601ea02011-06-10 18:23:25 +0000834
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300835 if (node->getFalseBlock())
836 {
837 out << "else\n";
838 visitCodeBlock(node->getFalseBlock());
zmo@google.com5601ea02011-06-10 18:23:25 +0000839 }
840 return false;
841}
842
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200843bool TOutputGLSLBase::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200844{
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200845 if (node->getStatementList())
846 {
847 writeTriplet(visit, "switch (", ") ", nullptr);
848 // The curly braces get written when visiting the statementList aggregate
849 }
850 else
851 {
852 // No statementList, so it won't output curly braces
853 writeTriplet(visit, "switch (", ") {", "}\n");
854 }
855 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200856}
857
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200858bool TOutputGLSLBase::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200859{
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200860 if (node->hasCondition())
861 {
862 writeTriplet(visit, "case (", nullptr, "):\n");
863 return true;
864 }
865 else
866 {
867 TInfoSinkBase &out = objSink();
868 out << "default:\n";
869 return false;
870 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200871}
872
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100873bool TOutputGLSLBase::visitBlock(Visit visit, TIntermBlock *node)
874{
875 TInfoSinkBase &out = objSink();
876 // Scope the blocks except when at the global scope.
877 if (mDepth > 0)
878 {
879 out << "{\n";
880 }
881
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100882 for (TIntermSequence::const_iterator iter = node->getSequence()->begin();
883 iter != node->getSequence()->end(); ++iter)
884 {
885 TIntermNode *curNode = *iter;
886 ASSERT(curNode != nullptr);
887 curNode->traverse(this);
888
889 if (isSingleStatement(curNode))
890 out << ";\n";
891 }
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100892
893 // Scope the blocks except when at the global scope.
894 if (mDepth > 0)
895 {
896 out << "}\n";
897 }
898 return false;
899}
900
Olli Etuaho336b1472016-10-05 16:37:55 +0100901bool TOutputGLSLBase::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
902{
Olli Etuaho8ad9e752017-01-16 19:55:20 +0000903 TIntermFunctionPrototype *prototype = node->getFunctionPrototype();
904 prototype->traverse(this);
Olli Etuaho336b1472016-10-05 16:37:55 +0100905 visitCodeBlock(node->getBody());
Olli Etuaho336b1472016-10-05 16:37:55 +0100906
907 // Fully processed; no need to visit children.
908 return false;
909}
910
Olli Etuahobf4e1b72016-12-09 11:30:15 +0000911bool TOutputGLSLBase::visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node)
912{
913 TInfoSinkBase &out = objSink();
914 ASSERT(visit == PreVisit);
915 const TIntermSymbol *symbol = node->getSymbol();
916 out << "invariant " << hashVariableName(symbol->getName());
917 return false;
918}
919
Olli Etuaho16c745a2017-01-16 17:02:27 +0000920bool TOutputGLSLBase::visitFunctionPrototype(Visit visit, TIntermFunctionPrototype *node)
921{
922 TInfoSinkBase &out = objSink();
923 ASSERT(visit == PreVisit);
924
925 const TType &type = node->getType();
926 writeVariableType(type);
927 if (type.isArray())
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300928 out << ArrayString(type);
Olli Etuaho16c745a2017-01-16 17:02:27 +0000929
Olli Etuahoec9232b2017-03-27 17:01:37 +0300930 out << " " << hashFunctionNameIfNeeded(*node->getFunctionSymbolInfo());
Olli Etuaho16c745a2017-01-16 17:02:27 +0000931
932 out << "(";
933 writeFunctionParameters(*(node->getSequence()));
934 out << ")";
935
936 return false;
937}
938
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700939bool TOutputGLSLBase::visitAggregate(Visit visit, TIntermAggregate *node)
zmo@google.com5601ea02011-06-10 18:23:25 +0000940{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500941 bool visitChildren = true;
942 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +0000943 switch (node->getOp())
944 {
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800945 case EOpCallFunctionInAST:
946 case EOpCallInternalRawFunction:
947 case EOpCallBuiltInFunction:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500948 // Function call.
949 if (visit == PreVisit)
Olli Etuahoec9232b2017-03-27 17:01:37 +0300950 {
951 if (node->getOp() == EOpCallBuiltInFunction)
952 {
953 out << translateTextureFunction(node->getFunctionSymbolInfo()->getName());
954 }
955 else
956 {
957 out << hashFunctionNameIfNeeded(*node->getFunctionSymbolInfo());
958 }
959 out << "(";
960 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500961 else if (visit == InVisit)
962 out << ", ";
963 else
964 out << ")";
965 break;
Olli Etuaho8fab3202017-05-08 18:22:22 +0300966 case EOpConstruct:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500967 writeConstructorTriplet(visit, node->getType());
968 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +0200969
Olli Etuahoe1805592017-01-02 16:41:20 +0000970 case EOpEqualComponentWise:
971 case EOpNotEqualComponentWise:
972 case EOpLessThanComponentWise:
973 case EOpGreaterThanComponentWise:
974 case EOpLessThanEqualComponentWise:
975 case EOpGreaterThanEqualComponentWise:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500976 case EOpMod:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500977 case EOpModf:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500978 case EOpPow:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500979 case EOpAtan:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500980 case EOpMin:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500981 case EOpMax:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500982 case EOpClamp:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500983 case EOpMix:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500984 case EOpStep:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500985 case EOpSmoothStep:
Olli Etuaho74da73f2017-02-01 15:37:48 +0000986 case EOpFrexp:
987 case EOpLdexp:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500988 case EOpDistance:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500989 case EOpDot:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500990 case EOpCross:
Jamie Madille72595b2017-06-06 15:12:26 -0400991 case EOpFaceforward:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500992 case EOpReflect:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500993 case EOpRefract:
Olli Etuahoe1805592017-01-02 16:41:20 +0000994 case EOpMulMatrixComponentWise:
995 case EOpOuterProduct:
Olli Etuaho9250cb22017-01-21 10:51:27 +0000996 case EOpBitfieldExtract:
997 case EOpBitfieldInsert:
998 case EOpUaddCarry:
999 case EOpUsubBorrow:
1000 case EOpUmulExtended:
1001 case EOpImulExtended:
Martin Radevd7c5b0a2016-07-27 14:04:43 +03001002 case EOpBarrier:
Martin Radevd7c5b0a2016-07-27 14:04:43 +03001003 case EOpMemoryBarrier:
Martin Radevd7c5b0a2016-07-27 14:04:43 +03001004 case EOpMemoryBarrierAtomicCounter:
Martin Radevd7c5b0a2016-07-27 14:04:43 +03001005 case EOpMemoryBarrierBuffer:
Martin Radevd7c5b0a2016-07-27 14:04:43 +03001006 case EOpMemoryBarrierImage:
Martin Radevd7c5b0a2016-07-27 14:04:43 +03001007 case EOpMemoryBarrierShared:
Martin Radevd7c5b0a2016-07-27 14:04:43 +03001008 case EOpGroupMemoryBarrier:
Jiawei Shaod27f5c82017-08-23 09:38:08 +08001009 case EOpEmitVertex:
1010 case EOpEndPrimitive:
Olli Etuahod68924e2017-01-02 17:34:40 +00001011 writeBuiltInFunctionTriplet(visit, node->getOp(), node->getUseEmulatedFunction());
Martin Radevd7c5b0a2016-07-27 14:04:43 +03001012 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001013 default:
1014 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +00001015 }
zmo@google.com5601ea02011-06-10 18:23:25 +00001016 return visitChildren;
1017}
1018
Olli Etuaho13389b62016-10-16 11:48:18 +01001019bool TOutputGLSLBase::visitDeclaration(Visit visit, TIntermDeclaration *node)
1020{
1021 TInfoSinkBase &out = objSink();
1022
1023 // Variable declaration.
1024 if (visit == PreVisit)
1025 {
1026 const TIntermSequence &sequence = *(node->getSequence());
1027 const TIntermTyped *variable = sequence.front()->getAsTyped();
1028 writeLayoutQualifier(variable->getType());
1029 writeVariableType(variable->getType());
1030 out << " ";
1031 mDeclaringVariables = true;
1032 }
1033 else if (visit == InVisit)
1034 {
1035 out << ", ";
1036 mDeclaringVariables = true;
1037 }
1038 else
1039 {
1040 mDeclaringVariables = false;
1041 }
1042 return true;
1043}
1044
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001045bool TOutputGLSLBase::visitLoop(Visit visit, TIntermLoop *node)
zmo@google.com5601ea02011-06-10 18:23:25 +00001046{
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001047 TInfoSinkBase &out = objSink();
zmo@google.com5601ea02011-06-10 18:23:25 +00001048
zmo@google.com5601ea02011-06-10 18:23:25 +00001049 TLoopType loopType = node->getType();
Corentin Wallez7258e302015-09-22 10:40:24 -07001050
zmo@google.com5601ea02011-06-10 18:23:25 +00001051 if (loopType == ELoopFor) // for loop
1052 {
Corentin Wallez1b896c62016-11-16 13:10:44 -05001053 out << "for (";
1054 if (node->getInit())
1055 node->getInit()->traverse(this);
1056 out << "; ";
zmo@google.com5601ea02011-06-10 18:23:25 +00001057
Corentin Wallez1b896c62016-11-16 13:10:44 -05001058 if (node->getCondition())
1059 node->getCondition()->traverse(this);
1060 out << "; ";
zmo@google.com5601ea02011-06-10 18:23:25 +00001061
Corentin Wallez1b896c62016-11-16 13:10:44 -05001062 if (node->getExpression())
1063 node->getExpression()->traverse(this);
1064 out << ")\n";
Corentin Wallez7258e302015-09-22 10:40:24 -07001065
Corentin Wallez1b896c62016-11-16 13:10:44 -05001066 visitCodeBlock(node->getBody());
zmo@google.com5601ea02011-06-10 18:23:25 +00001067 }
1068 else if (loopType == ELoopWhile) // while loop
1069 {
1070 out << "while (";
Yunchao He4f285442017-04-21 12:15:49 +08001071 ASSERT(node->getCondition() != nullptr);
zmo@google.com5601ea02011-06-10 18:23:25 +00001072 node->getCondition()->traverse(this);
1073 out << ")\n";
Corentin Wallez7258e302015-09-22 10:40:24 -07001074
1075 visitCodeBlock(node->getBody());
zmo@google.com5601ea02011-06-10 18:23:25 +00001076 }
1077 else // do-while loop
1078 {
1079 ASSERT(loopType == ELoopDoWhile);
1080 out << "do\n";
zmo@google.com5601ea02011-06-10 18:23:25 +00001081
zmo@google.com5601ea02011-06-10 18:23:25 +00001082 visitCodeBlock(node->getBody());
zmo@google.com5601ea02011-06-10 18:23:25 +00001083
zmo@google.com5601ea02011-06-10 18:23:25 +00001084 out << "while (";
Yunchao He4f285442017-04-21 12:15:49 +08001085 ASSERT(node->getCondition() != nullptr);
zmo@google.com5601ea02011-06-10 18:23:25 +00001086 node->getCondition()->traverse(this);
1087 out << ");\n";
1088 }
Corentin Wallez7258e302015-09-22 10:40:24 -07001089
zmo@google.com5601ea02011-06-10 18:23:25 +00001090 // No need to visit children. They have been already processed in
1091 // this function.
1092 return false;
1093}
1094
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001095bool TOutputGLSLBase::visitBranch(Visit visit, TIntermBranch *node)
zmo@google.com5601ea02011-06-10 18:23:25 +00001096{
1097 switch (node->getFlowOp())
1098 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001099 case EOpKill:
Yunchao Hef81ce4a2017-04-24 10:49:17 +08001100 writeTriplet(visit, "discard", nullptr, nullptr);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001101 break;
1102 case EOpBreak:
Yunchao Hef81ce4a2017-04-24 10:49:17 +08001103 writeTriplet(visit, "break", nullptr, nullptr);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001104 break;
1105 case EOpContinue:
Yunchao Hef81ce4a2017-04-24 10:49:17 +08001106 writeTriplet(visit, "continue", nullptr, nullptr);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001107 break;
1108 case EOpReturn:
Yunchao Hef81ce4a2017-04-24 10:49:17 +08001109 writeTriplet(visit, "return ", nullptr, nullptr);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001110 break;
1111 default:
1112 UNREACHABLE();
zmo@google.com5601ea02011-06-10 18:23:25 +00001113 }
1114
1115 return true;
1116}
1117
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001118void TOutputGLSLBase::visitCodeBlock(TIntermBlock *node)
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001119{
zmo@google.com5601ea02011-06-10 18:23:25 +00001120 TInfoSinkBase &out = objSink();
Yunchao He4f285442017-04-21 12:15:49 +08001121 if (node != nullptr)
zmo@google.com5601ea02011-06-10 18:23:25 +00001122 {
1123 node->traverse(this);
1124 // Single statements not part of a sequence need to be terminated
1125 // with semi-colon.
1126 if (isSingleStatement(node))
1127 out << ";\n";
1128 }
1129 else
1130 {
1131 out << "{\n}\n"; // Empty code block.
1132 }
1133}
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001134
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001135TString TOutputGLSLBase::getTypeName(const TType &type)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001136{
Olli Etuahoe92507b2016-07-04 11:20:10 +03001137 if (type.getBasicType() == EbtStruct)
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001138 return hashName(TName(type.getStruct()->name()));
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001139 else
Olli Etuahoe92507b2016-07-04 11:20:10 +03001140 return type.getBuiltInTypeNameString();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001141}
1142
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001143TString TOutputGLSLBase::hashName(const TName &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001144{
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001145 if (name.getString().empty())
1146 {
1147 ASSERT(!name.isInternal());
1148 return name.getString();
1149 }
1150 if (name.isInternal())
1151 {
1152 // TODO(oetuaho): Would be nicer to prefix non-internal names with "_" instead, like is
1153 // done in the HLSL output, but that requires fairly complex changes elsewhere in the code
1154 // as well.
1155 // We need to use a prefix that is reserved in WebGL in order to guarantee that the internal
1156 // names don't conflict with user-defined names from WebGL.
1157 return "webgl_angle_" + name.getString();
1158 }
1159 if (mHashFunction == nullptr)
1160 {
1161 return name.getString();
1162 }
1163 NameMap::const_iterator it = mNameMap.find(name.getString().c_str());
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001164 if (it != mNameMap.end())
1165 return it->second.c_str();
Olli Etuahocccf2b02017-07-05 14:50:54 +03001166 TString hashedName = HashName(name.getString(), mHashFunction);
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001167 mNameMap[name.getString().c_str()] = hashedName.c_str();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001168 return hashedName;
1169}
1170
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001171TString TOutputGLSLBase::hashVariableName(const TName &name)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001172{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03001173 if (mSymbolTable->findBuiltIn(name.getString(), mShaderVersion) != nullptr)
Olli Etuaho09b04a22016-12-15 13:30:26 +00001174 {
1175 if (mCompileOptions & SH_TRANSLATE_VIEWID_OVR_TO_UNIFORM &&
1176 name.getString() == "gl_ViewID_OVR")
1177 {
Olli Etuaho2f90a9b2017-01-10 12:27:56 +00001178 TName uniformName(TString("ViewID_OVR"));
Olli Etuaho09b04a22016-12-15 13:30:26 +00001179 uniformName.setInternal(true);
1180 return hashName(uniformName);
1181 }
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001182 return name.getString();
Olli Etuaho09b04a22016-12-15 13:30:26 +00001183 }
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001184 return hashName(name);
1185}
1186
Olli Etuahoec9232b2017-03-27 17:01:37 +03001187TString TOutputGLSLBase::hashFunctionNameIfNeeded(const TFunctionSymbolInfo &info)
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001188{
Olli Etuahoec9232b2017-03-27 17:01:37 +03001189 if (info.isMain() || info.getNameObj().isInternal())
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001190 {
1191 // Internal function names are outputted as-is - they may refer to functions manually added
1192 // to the output shader source that are not included in the AST at all.
Olli Etuahoec9232b2017-03-27 17:01:37 +03001193 return info.getName();
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001194 }
Olli Etuaho59f9a642015-08-06 20:38:26 +03001195 else
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001196 {
Olli Etuahoec9232b2017-03-27 17:01:37 +03001197 return hashName(info.getNameObj());
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001198 }
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001199}
Jamie Madill98493dd2013-07-08 14:39:03 -04001200
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001201bool TOutputGLSLBase::structDeclared(const TStructure *structure) const
Jamie Madill98493dd2013-07-08 14:39:03 -04001202{
Zhenyao Mo904a9162014-05-09 14:07:45 -07001203 ASSERT(structure);
Jamie Madill01f85ac2014-06-06 11:55:04 -04001204 if (structure->name().empty())
Zhenyao Mo904a9162014-05-09 14:07:45 -07001205 {
Jamie Madill01f85ac2014-06-06 11:55:04 -04001206 return false;
Zhenyao Mo904a9162014-05-09 14:07:45 -07001207 }
Jamie Madill01f85ac2014-06-06 11:55:04 -04001208
1209 return (mDeclaredStructs.count(structure->uniqueId()) > 0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001210}
1211
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001212void TOutputGLSLBase::declareStruct(const TStructure *structure)
Jamie Madill98493dd2013-07-08 14:39:03 -04001213{
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001214 TInfoSinkBase &out = objSink();
Jamie Madill98493dd2013-07-08 14:39:03 -04001215
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001216 out << "struct " << hashName(TName(structure->name())) << "{\n";
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001217 const TFieldList &fields = structure->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04001218 for (size_t i = 0; i < fields.size(); ++i)
1219 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -07001220 const TField *field = fields[i];
Jamie Madill98493dd2013-07-08 14:39:03 -04001221 if (writeVariablePrecision(field->type()->getPrecision()))
1222 out << " ";
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001223 out << getTypeName(*field->type()) << " " << hashName(TName(field->name()));
Jamie Madill98493dd2013-07-08 14:39:03 -04001224 if (field->type()->isArray())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001225 out << ArrayString(*field->type());
Jamie Madill98493dd2013-07-08 14:39:03 -04001226 out << ";\n";
1227 }
1228 out << "}";
Zhenyao Mo904a9162014-05-09 14:07:45 -07001229}
Jamie Madill98493dd2013-07-08 14:39:03 -04001230
Geoff Langbdcc54a2015-09-02 13:09:48 -04001231void TOutputGLSLBase::declareInterfaceBlockLayout(const TInterfaceBlock *interfaceBlock)
1232{
1233 TInfoSinkBase &out = objSink();
1234
1235 out << "layout(";
1236
1237 switch (interfaceBlock->blockStorage())
1238 {
1239 case EbsUnspecified:
1240 case EbsShared:
1241 // Default block storage is shared.
1242 out << "shared";
1243 break;
1244
1245 case EbsPacked:
1246 out << "packed";
1247 break;
1248
1249 case EbsStd140:
1250 out << "std140";
1251 break;
1252
1253 default:
1254 UNREACHABLE();
1255 break;
1256 }
1257
1258 out << ", ";
1259
1260 switch (interfaceBlock->matrixPacking())
1261 {
1262 case EmpUnspecified:
1263 case EmpColumnMajor:
1264 // Default matrix packing is column major.
1265 out << "column_major";
1266 break;
1267
1268 case EmpRowMajor:
1269 out << "row_major";
1270 break;
1271
1272 default:
1273 UNREACHABLE();
1274 break;
1275 }
1276
1277 out << ") ";
1278}
1279
1280void TOutputGLSLBase::declareInterfaceBlock(const TInterfaceBlock *interfaceBlock)
1281{
1282 TInfoSinkBase &out = objSink();
1283
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001284 out << hashName(TName(interfaceBlock->name())) << "{\n";
Geoff Langbdcc54a2015-09-02 13:09:48 -04001285 const TFieldList &fields = interfaceBlock->fields();
1286 for (size_t i = 0; i < fields.size(); ++i)
1287 {
1288 const TField *field = fields[i];
1289 if (writeVariablePrecision(field->type()->getPrecision()))
1290 out << " ";
Olli Etuaho0982a2b2016-11-01 15:13:46 +00001291 out << getTypeName(*field->type()) << " " << hashName(TName(field->name()));
Geoff Langbdcc54a2015-09-02 13:09:48 -04001292 if (field->type()->isArray())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001293 out << ArrayString(*field->type());
Geoff Langbdcc54a2015-09-02 13:09:48 -04001294 out << ";\n";
1295 }
1296 out << "}";
1297}
Jamie Madill45bcc782016-11-07 13:58:48 -05001298
Shaob5cc1192017-07-06 10:47:20 +08001299void WriteGeometryShaderLayoutQualifiers(TInfoSinkBase &out,
1300 sh::TLayoutPrimitiveType inputPrimitive,
1301 int invocations,
1302 sh::TLayoutPrimitiveType outputPrimitive,
1303 int maxVertices)
1304{
1305 // Omit 'invocations = 1'
1306 if (inputPrimitive != EptUndefined || invocations > 1)
1307 {
1308 out << "layout (";
1309
1310 if (inputPrimitive != EptUndefined)
1311 {
1312 out << getGeometryShaderPrimitiveTypeString(inputPrimitive);
1313 }
1314
1315 if (invocations > 1)
1316 {
1317 if (inputPrimitive != EptUndefined)
1318 {
1319 out << ", ";
1320 }
1321 out << "invocations = " << invocations;
1322 }
1323 out << ") in;\n";
1324 }
1325
1326 if (outputPrimitive != EptUndefined || maxVertices != -1)
1327 {
1328 out << "layout (";
1329
1330 if (outputPrimitive != EptUndefined)
1331 {
1332 out << getGeometryShaderPrimitiveTypeString(outputPrimitive);
1333 }
1334
1335 if (maxVertices != -1)
1336 {
1337 if (outputPrimitive != EptUndefined)
1338 {
1339 out << ", ";
1340 }
1341 out << "max_vertices = " << maxVertices;
1342 }
1343 out << ") out;\n";
1344 }
1345}
1346
Jamie Madill45bcc782016-11-07 13:58:48 -05001347} // namespace sh