blob: 1c9dc39d94c19ff3f7ee0ad522229a268c394ab8 [file] [log] [blame]
Olli Etuaho853dc1a2014-11-06 17:25:48 +02001//
2// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7#include "compiler/translator/EmulatePrecision.h"
8
Olli Etuahob741c762016-06-29 15:49:22 +03009#include <memory>
10
Jamie Madill45bcc782016-11-07 13:58:48 -050011namespace sh
12{
13
Olli Etuaho853dc1a2014-11-06 17:25:48 +020014namespace
15{
16
Olli Etuahob741c762016-06-29 15:49:22 +030017class RoundingHelperWriter : angle::NonCopyable
Olli Etuaho853dc1a2014-11-06 17:25:48 +020018{
Olli Etuahob741c762016-06-29 15:49:22 +030019 public:
20 static RoundingHelperWriter *createHelperWriter(const ShShaderOutput outputLanguage);
Olli Etuaho853dc1a2014-11-06 17:25:48 +020021
Olli Etuahob741c762016-06-29 15:49:22 +030022 void writeCommonRoundingHelpers(TInfoSinkBase &sink, const int shaderVersion);
23 void writeCompoundAssignmentHelper(TInfoSinkBase &sink,
24 const char *lType,
25 const char *rType,
26 const char *opStr,
27 const char *opNameStr);
Olli Etuaho853dc1a2014-11-06 17:25:48 +020028
Olli Etuahob741c762016-06-29 15:49:22 +030029 virtual ~RoundingHelperWriter() {}
Olli Etuaho853dc1a2014-11-06 17:25:48 +020030
Olli Etuahob741c762016-06-29 15:49:22 +030031 protected:
32 RoundingHelperWriter(const ShShaderOutput outputLanguage) : mOutputLanguage(outputLanguage) {}
33 RoundingHelperWriter() = delete;
34
35 const ShShaderOutput mOutputLanguage;
36
37 private:
38 virtual std::string getTypeString(const char *glslType) = 0;
39 virtual void writeFloatRoundingHelpers(TInfoSinkBase &sink) = 0;
40 virtual void writeVectorRoundingHelpers(TInfoSinkBase &sink, const unsigned int size) = 0;
41 virtual void writeMatrixRoundingHelper(TInfoSinkBase &sink,
42 const unsigned int columns,
43 const unsigned int rows,
44 const char *functionName) = 0;
45};
46
47class RoundingHelperWriterGLSL : public RoundingHelperWriter
Olli Etuaho3fdaf6f2016-07-13 15:07:41 +030048{
Olli Etuahob741c762016-06-29 15:49:22 +030049 public:
50 RoundingHelperWriterGLSL(const ShShaderOutput outputLanguage)
51 : RoundingHelperWriter(outputLanguage)
Olli Etuaho3fdaf6f2016-07-13 15:07:41 +030052 {
Olli Etuaho3fdaf6f2016-07-13 15:07:41 +030053 }
54
Olli Etuahob741c762016-06-29 15:49:22 +030055 private:
56 std::string getTypeString(const char *glslType) override;
57 void writeFloatRoundingHelpers(TInfoSinkBase &sink) override;
58 void writeVectorRoundingHelpers(TInfoSinkBase &sink, const unsigned int size) override;
59 void writeMatrixRoundingHelper(TInfoSinkBase &sink,
60 const unsigned int columns,
61 const unsigned int rows,
62 const char *functionName) override;
63};
Olli Etuaho3fdaf6f2016-07-13 15:07:41 +030064
Olli Etuahob741c762016-06-29 15:49:22 +030065class RoundingHelperWriterESSL : public RoundingHelperWriterGLSL
66{
67 public:
68 RoundingHelperWriterESSL(const ShShaderOutput outputLanguage)
69 : RoundingHelperWriterGLSL(outputLanguage)
Olli Etuaho3fdaf6f2016-07-13 15:07:41 +030070 {
Olli Etuaho3fdaf6f2016-07-13 15:07:41 +030071 }
72
Olli Etuahob741c762016-06-29 15:49:22 +030073 private:
74 std::string getTypeString(const char *glslType) override;
75};
76
77class RoundingHelperWriterHLSL : public RoundingHelperWriter
78{
79 public:
80 RoundingHelperWriterHLSL(const ShShaderOutput outputLanguage)
81 : RoundingHelperWriter(outputLanguage)
82 {
83 }
84
85 private:
86 std::string getTypeString(const char *glslType) override;
87 void writeFloatRoundingHelpers(TInfoSinkBase &sink) override;
88 void writeVectorRoundingHelpers(TInfoSinkBase &sink, const unsigned int size) override;
89 void writeMatrixRoundingHelper(TInfoSinkBase &sink,
90 const unsigned int columns,
91 const unsigned int rows,
92 const char *functionName) override;
93};
94
95RoundingHelperWriter *RoundingHelperWriter::createHelperWriter(const ShShaderOutput outputLanguage)
96{
Jamie Madilld5696192016-10-06 11:09:24 -040097 ASSERT(EmulatePrecision::SupportedInLanguage(outputLanguage));
Olli Etuahob741c762016-06-29 15:49:22 +030098 switch (outputLanguage)
99 {
100 case SH_HLSL_4_1_OUTPUT:
101 return new RoundingHelperWriterHLSL(outputLanguage);
102 case SH_ESSL_OUTPUT:
103 return new RoundingHelperWriterESSL(outputLanguage);
104 default:
Olli Etuahob741c762016-06-29 15:49:22 +0300105 return new RoundingHelperWriterGLSL(outputLanguage);
106 }
Olli Etuaho3fdaf6f2016-07-13 15:07:41 +0300107}
108
Olli Etuahob741c762016-06-29 15:49:22 +0300109void RoundingHelperWriter::writeCommonRoundingHelpers(TInfoSinkBase &sink, const int shaderVersion)
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200110{
111 // Write the angle_frm functions that round floating point numbers to
112 // half precision, and angle_frl functions that round them to minimum lowp
113 // precision.
114
Olli Etuahob741c762016-06-29 15:49:22 +0300115 writeFloatRoundingHelpers(sink);
116 writeVectorRoundingHelpers(sink, 2);
117 writeVectorRoundingHelpers(sink, 3);
118 writeVectorRoundingHelpers(sink, 4);
119 if (shaderVersion > 100)
120 {
121 for (unsigned int columns = 2; columns <= 4; ++columns)
122 {
123 for (unsigned int rows = 2; rows <= 4; ++rows)
124 {
125 writeMatrixRoundingHelper(sink, columns, rows, "angle_frm");
126 writeMatrixRoundingHelper(sink, columns, rows, "angle_frl");
127 }
128 }
129 }
130 else
131 {
132 for (unsigned int size = 2; size <= 4; ++size)
133 {
134 writeMatrixRoundingHelper(sink, size, size, "angle_frm");
135 writeMatrixRoundingHelper(sink, size, size, "angle_frl");
136 }
137 }
138}
139
140void RoundingHelperWriter::writeCompoundAssignmentHelper(TInfoSinkBase &sink,
141 const char *lType,
142 const char *rType,
143 const char *opStr,
144 const char *opNameStr)
145{
146 std::string lTypeStr = getTypeString(lType);
147 std::string rTypeStr = getTypeString(rType);
148
149 // Note that y should be passed through angle_frm at the function call site,
150 // but x can't be passed through angle_frm there since it is an inout parameter.
151 // So only pass x and the result through angle_frm here.
152 // clang-format off
153 sink <<
154 lTypeStr << " angle_compound_" << opNameStr << "_frm(inout " << lTypeStr << " x, in " << rTypeStr << " y) {\n"
155 " x = angle_frm(angle_frm(x) " << opStr << " y);\n"
156 " return x;\n"
157 "}\n";
158 sink <<
159 lTypeStr << " angle_compound_" << opNameStr << "_frl(inout " << lTypeStr << " x, in " << rTypeStr << " y) {\n"
160 " x = angle_frl(angle_frm(x) " << opStr << " y);\n"
161 " return x;\n"
162 "}\n";
163 // clang-format on
164}
165
166std::string RoundingHelperWriterGLSL::getTypeString(const char *glslType)
167{
168 return glslType;
169}
170
171std::string RoundingHelperWriterESSL::getTypeString(const char *glslType)
172{
173 std::stringstream typeStrStr;
174 typeStrStr << "highp " << glslType;
175 return typeStrStr.str();
176}
177
178void RoundingHelperWriterGLSL::writeFloatRoundingHelpers(TInfoSinkBase &sink)
179{
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200180 // Unoptimized version of angle_frm for single floats:
181 //
Olli Etuahob741c762016-06-29 15:49:22 +0300182 // int webgl_maxNormalExponent(in int exponentBits)
183 // {
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200184 // int possibleExponents = int(exp2(float(exponentBits)));
185 // int exponentBias = possibleExponents / 2 - 1;
186 // int allExponentBitsOne = possibleExponents - 1;
187 // return (allExponentBitsOne - 1) - exponentBias;
188 // }
189 //
Olli Etuahob741c762016-06-29 15:49:22 +0300190 // float angle_frm(in float x)
191 // {
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200192 // int mantissaBits = 10;
193 // int exponentBits = 5;
194 // float possibleMantissas = exp2(float(mantissaBits));
195 // float mantissaMax = 2.0 - 1.0 / possibleMantissas;
196 // int maxNE = webgl_maxNormalExponent(exponentBits);
197 // float max = exp2(float(maxNE)) * mantissaMax;
Olli Etuahob741c762016-06-29 15:49:22 +0300198 // if (x > max)
199 // {
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200200 // return max;
201 // }
Olli Etuahob741c762016-06-29 15:49:22 +0300202 // if (x < -max)
203 // {
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200204 // return -max;
205 // }
206 // float exponent = floor(log2(abs(x)));
Olli Etuahob741c762016-06-29 15:49:22 +0300207 // if (abs(x) == 0.0 || exponent < -float(maxNE))
208 // {
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200209 // return 0.0 * sign(x)
210 // }
211 // x = x * exp2(-(exponent - float(mantissaBits)));
212 // x = sign(x) * floor(abs(x));
213 // return x * exp2(exponent - float(mantissaBits));
214 // }
215
216 // All numbers with a magnitude less than 2^-15 are subnormal, and are
217 // flushed to zero.
218
219 // Note the constant numbers below:
220 // a) 65504 is the maximum possible mantissa (1.1111111111 in binary) times
221 // 2^15, the maximum normal exponent.
222 // b) 10.0 is the number of mantissa bits.
223 // c) -25.0 is the minimum normal half-float exponent -15.0 minus the number
224 // of mantissa bits.
225 // d) + 1e-30 is to make sure the argument of log2() won't be zero. It can
226 // only affect the result of log2 on x where abs(x) < 1e-22. Since these
227 // numbers will be flushed to zero either way (2^-15 is the smallest
228 // normal positive number), this does not introduce any error.
229
Olli Etuahob741c762016-06-29 15:49:22 +0300230 std::string floatType = getTypeString("float");
231
232 // clang-format off
233 sink <<
234 floatType << " angle_frm(in " << floatType << " x) {\n"
235 " x = clamp(x, -65504.0, 65504.0);\n"
236 " " << floatType << " exponent = floor(log2(abs(x) + 1e-30)) - 10.0;\n"
237 " bool isNonZero = (exponent >= -25.0);\n"
238 " x = x * exp2(-exponent);\n"
239 " x = sign(x) * floor(abs(x));\n"
240 " return x * exp2(exponent) * float(isNonZero);\n"
241 "}\n";
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200242
243 sink <<
Olli Etuahob741c762016-06-29 15:49:22 +0300244 floatType << " angle_frl(in " << floatType << " x) {\n"
245 " x = clamp(x, -2.0, 2.0);\n"
246 " x = x * 256.0;\n"
247 " x = sign(x) * floor(abs(x));\n"
248 " return x * 0.00390625;\n"
249 "}\n";
250 // clang-format on
Olli Etuahoa42e8b22016-06-29 15:49:22 +0300251}
252
Olli Etuahob741c762016-06-29 15:49:22 +0300253void RoundingHelperWriterGLSL::writeVectorRoundingHelpers(TInfoSinkBase &sink,
254 const unsigned int size)
Olli Etuahoa42e8b22016-06-29 15:49:22 +0300255{
Olli Etuahob741c762016-06-29 15:49:22 +0300256 std::stringstream vecTypeStrStr;
257 vecTypeStrStr << "vec" << size;
258 std::string vecType = getTypeString(vecTypeStrStr.str().c_str());
259
260 // clang-format off
261 sink <<
262 vecType << " angle_frm(in " << vecType << " v) {\n"
263 " v = clamp(v, -65504.0, 65504.0);\n"
264 " " << vecType << " exponent = floor(log2(abs(v) + 1e-30)) - 10.0;\n"
265 " bvec" << size << " isNonZero = greaterThanEqual(exponent, vec" << size << "(-25.0));\n"
266 " v = v * exp2(-exponent);\n"
267 " v = sign(v) * floor(abs(v));\n"
268 " return v * exp2(exponent) * vec" << size << "(isNonZero);\n"
269 "}\n";
270
271 sink <<
272 vecType << " angle_frl(in " << vecType << " v) {\n"
273 " v = clamp(v, -2.0, 2.0);\n"
274 " v = v * 256.0;\n"
275 " v = sign(v) * floor(abs(v));\n"
276 " return v * 0.00390625;\n"
277 "}\n";
278 // clang-format on
279}
280
281void RoundingHelperWriterGLSL::writeMatrixRoundingHelper(TInfoSinkBase &sink,
282 const unsigned int columns,
283 const unsigned int rows,
284 const char *functionName)
285{
286 std::stringstream matTypeStrStr;
287 matTypeStrStr << "mat" << columns;
288 if (rows != columns)
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200289 {
Olli Etuahob741c762016-06-29 15:49:22 +0300290 matTypeStrStr << "x" << rows;
291 }
292 std::string matType = getTypeString(matTypeStrStr.str().c_str());
293
294 sink << matType << " " << functionName << "(in " << matType << " m) {\n"
295 << " " << matType << " rounded;\n";
296
297 for (unsigned int i = 0; i < columns; ++i)
298 {
299 sink << " rounded[" << i << "] = " << functionName << "(m[" << i << "]);\n";
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200300 }
301
Olli Etuahob741c762016-06-29 15:49:22 +0300302 sink << " return rounded;\n"
303 "}\n";
304}
305
306static const char *GetHLSLTypeStr(const char *floatTypeStr)
307{
308 if (strcmp(floatTypeStr, "float") == 0)
309 {
310 return "float";
311 }
312 if (strcmp(floatTypeStr, "vec2") == 0)
313 {
314 return "float2";
315 }
316 if (strcmp(floatTypeStr, "vec3") == 0)
317 {
318 return "float3";
319 }
320 if (strcmp(floatTypeStr, "vec4") == 0)
321 {
322 return "float4";
323 }
324 if (strcmp(floatTypeStr, "mat2") == 0)
325 {
326 return "float2x2";
327 }
328 if (strcmp(floatTypeStr, "mat3") == 0)
329 {
330 return "float3x3";
331 }
332 if (strcmp(floatTypeStr, "mat4") == 0)
333 {
334 return "float4x4";
335 }
336 if (strcmp(floatTypeStr, "mat2x3") == 0)
337 {
338 return "float2x3";
339 }
340 if (strcmp(floatTypeStr, "mat2x4") == 0)
341 {
342 return "float2x4";
343 }
344 if (strcmp(floatTypeStr, "mat3x2") == 0)
345 {
346 return "float3x2";
347 }
348 if (strcmp(floatTypeStr, "mat3x4") == 0)
349 {
350 return "float3x4";
351 }
352 if (strcmp(floatTypeStr, "mat4x2") == 0)
353 {
354 return "float4x2";
355 }
356 if (strcmp(floatTypeStr, "mat4x3") == 0)
357 {
358 return "float4x3";
359 }
360 UNREACHABLE();
361 return nullptr;
362}
363
364std::string RoundingHelperWriterHLSL::getTypeString(const char *glslType)
365{
366 return GetHLSLTypeStr(glslType);
367}
368
369void RoundingHelperWriterHLSL::writeFloatRoundingHelpers(TInfoSinkBase &sink)
370{
371 // In HLSL scalars are the same as 1-vectors.
372 writeVectorRoundingHelpers(sink, 1);
373}
374
375void RoundingHelperWriterHLSL::writeVectorRoundingHelpers(TInfoSinkBase &sink,
376 const unsigned int size)
377{
378 std::stringstream vecTypeStrStr;
379 vecTypeStrStr << "float" << size;
380 std::string vecType = vecTypeStrStr.str();
381
382 // clang-format off
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200383 sink <<
Olli Etuahob741c762016-06-29 15:49:22 +0300384 vecType << " angle_frm(" << vecType << " v) {\n"
385 " v = clamp(v, -65504.0, 65504.0);\n"
386 " " << vecType << " exponent = floor(log2(abs(v) + 1e-30)) - 10.0;\n"
387 " bool" << size << " isNonZero = exponent < -25.0;\n"
388 " v = v * exp2(-exponent);\n"
389 " v = sign(v) * floor(abs(v));\n"
390 " return v * exp2(exponent) * (float" << size << ")(isNonZero);\n"
391 "}\n";
392
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200393 sink <<
Olli Etuahob741c762016-06-29 15:49:22 +0300394 vecType << " angle_frl(" << vecType << " v) {\n"
395 " v = clamp(v, -2.0, 2.0);\n"
396 " v = v * 256.0;\n"
397 " v = sign(v) * floor(abs(v));\n"
398 " return v * 0.00390625;\n"
399 "}\n";
400 // clang-format on
401}
402
403void RoundingHelperWriterHLSL::writeMatrixRoundingHelper(TInfoSinkBase &sink,
404 const unsigned int columns,
405 const unsigned int rows,
406 const char *functionName)
407{
408 std::stringstream matTypeStrStr;
409 matTypeStrStr << "float" << columns << "x" << rows;
410 std::string matType = matTypeStrStr.str();
411
412 sink << matType << " " << functionName << "(" << matType << " m) {\n"
413 << " " << matType << " rounded;\n";
414
415 for (unsigned int i = 0; i < columns; ++i)
416 {
417 sink << " rounded[" << i << "] = " << functionName << "(m[" << i << "]);\n";
418 }
419
420 sink << " return rounded;\n"
421 "}\n";
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200422}
423
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200424bool canRoundFloat(const TType &type)
425{
Olli Etuaho61b81ac2016-06-28 14:15:20 +0300426 return type.getBasicType() == EbtFloat && !type.isArray() &&
427 (type.getPrecision() == EbpLow || type.getPrecision() == EbpMedium);
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200428}
429
430TIntermAggregate *createInternalFunctionCallNode(TString name, TIntermNode *child)
431{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800432 TIntermAggregate *callNode = new TIntermAggregate(EOpCallInternalRawFunction);
Olli Etuaho59f9a642015-08-06 20:38:26 +0300433 TName nameObj(TFunction::mangleName(name));
434 nameObj.setInternal(true);
Olli Etuahobd674552016-10-06 13:28:42 +0100435 callNode->getFunctionSymbolInfo()->setNameObj(nameObj);
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200436 callNode->getSequence()->push_back(child);
437 return callNode;
438}
439
440TIntermAggregate *createRoundingFunctionCallNode(TIntermTyped *roundedChild)
441{
442 TString roundFunctionName;
443 if (roundedChild->getPrecision() == EbpMedium)
444 roundFunctionName = "angle_frm";
445 else
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500446 roundFunctionName = "angle_frl";
Olli Etuaho3820e9c2016-07-04 16:01:15 +0300447 TIntermAggregate *callNode = createInternalFunctionCallNode(roundFunctionName, roundedChild);
448 callNode->setType(roundedChild->getType());
449 return callNode;
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200450}
451
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500452TIntermAggregate *createCompoundAssignmentFunctionCallNode(TIntermTyped *left,
453 TIntermTyped *right,
454 const char *opNameStr)
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200455{
456 std::stringstream strstr;
457 if (left->getPrecision() == EbpMedium)
458 strstr << "angle_compound_" << opNameStr << "_frm";
459 else
460 strstr << "angle_compound_" << opNameStr << "_frl";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500461 TString functionName = strstr.str().c_str();
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200462 TIntermAggregate *callNode = createInternalFunctionCallNode(functionName, left);
463 callNode->getSequence()->push_back(right);
464 return callNode;
465}
466
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500467bool parentUsesResult(TIntermNode *parent, TIntermNode *node)
Olli Etuaho1be88702015-01-19 16:56:44 +0200468{
469 if (!parent)
470 {
471 return false;
472 }
473
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100474 TIntermBlock *blockParent = parent->getAsBlock();
475 // If the parent is a block, the result is not assigned anywhere,
Olli Etuaho1be88702015-01-19 16:56:44 +0200476 // so rounding it is not needed. In particular, this can avoid a lot of
477 // unnecessary rounding of unused return values of assignment.
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100478 if (blockParent)
Olli Etuaho1be88702015-01-19 16:56:44 +0200479 {
480 return false;
481 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100482 TIntermBinary *binaryParent = parent->getAsBinaryNode();
483 if (binaryParent && binaryParent->getOp() == EOpComma && (binaryParent->getRight() != node))
Olli Etuaho1be88702015-01-19 16:56:44 +0200484 {
485 return false;
486 }
487 return true;
488}
489
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200490} // namespace anonymous
491
Olli Etuaho217fe6e2015-08-05 13:25:08 +0300492EmulatePrecision::EmulatePrecision(const TSymbolTable &symbolTable, int shaderVersion)
493 : TLValueTrackingTraverser(true, true, true, symbolTable, shaderVersion),
494 mDeclaringVariables(false)
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500495{
496}
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200497
498void EmulatePrecision::visitSymbol(TIntermSymbol *node)
499{
Olli Etuahoa26ad582015-08-04 13:51:47 +0300500 if (canRoundFloat(node->getType()) && !mDeclaringVariables && !isLValueRequiredHere())
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200501 {
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200502 TIntermNode *replacement = createRoundingFunctionCallNode(node);
Jamie Madill03d863c2016-07-27 18:15:53 -0400503 queueReplacement(node, replacement, OriginalNode::BECOMES_CHILD);
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200504 }
505}
506
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200507bool EmulatePrecision::visitBinary(Visit visit, TIntermBinary *node)
508{
509 bool visitChildren = true;
510
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200511 TOperator op = node->getOp();
512
513 // RHS of initialize is not being declared.
514 if (op == EOpInitialize && visit == InVisit)
515 mDeclaringVariables = false;
516
Olli Etuahob6fa0432016-09-28 16:28:05 +0100517 if ((op == EOpIndexDirectStruct) && visit == InVisit)
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200518 visitChildren = false;
519
520 if (visit != PreVisit)
521 return visitChildren;
522
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500523 const TType &type = node->getType();
524 bool roundFloat = canRoundFloat(type);
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200525
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500526 if (roundFloat)
527 {
528 switch (op)
529 {
530 // Math operators that can result in a float may need to apply rounding to the return
531 // value. Note that in the case of assignment, the rounding is applied to its return
532 // value here, not the value being assigned.
533 case EOpAssign:
534 case EOpAdd:
535 case EOpSub:
536 case EOpMul:
537 case EOpDiv:
538 case EOpVectorTimesScalar:
539 case EOpVectorTimesMatrix:
540 case EOpMatrixTimesVector:
541 case EOpMatrixTimesScalar:
542 case EOpMatrixTimesMatrix:
Olli Etuaho1be88702015-01-19 16:56:44 +0200543 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500544 TIntermNode *parent = getParentNode();
545 if (!parentUsesResult(parent, node))
546 {
547 break;
548 }
549 TIntermNode *replacement = createRoundingFunctionCallNode(node);
550 queueReplacement(node, replacement, OriginalNode::BECOMES_CHILD);
Olli Etuaho1be88702015-01-19 16:56:44 +0200551 break;
552 }
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200553
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500554 // Compound assignment cases need to replace the operator with a function call.
555 case EOpAddAssign:
556 {
557 mEmulateCompoundAdd.insert(
558 TypePair(type.getBuiltInTypeNameString(),
559 node->getRight()->getType().getBuiltInTypeNameString()));
560 TIntermNode *replacement = createCompoundAssignmentFunctionCallNode(
561 node->getLeft(), node->getRight(), "add");
562 queueReplacement(node, replacement, OriginalNode::IS_DROPPED);
563 break;
564 }
565 case EOpSubAssign:
566 {
567 mEmulateCompoundSub.insert(
568 TypePair(type.getBuiltInTypeNameString(),
569 node->getRight()->getType().getBuiltInTypeNameString()));
570 TIntermNode *replacement = createCompoundAssignmentFunctionCallNode(
571 node->getLeft(), node->getRight(), "sub");
572 queueReplacement(node, replacement, OriginalNode::IS_DROPPED);
573 break;
574 }
575 case EOpMulAssign:
576 case EOpVectorTimesMatrixAssign:
577 case EOpVectorTimesScalarAssign:
578 case EOpMatrixTimesScalarAssign:
579 case EOpMatrixTimesMatrixAssign:
580 {
581 mEmulateCompoundMul.insert(
582 TypePair(type.getBuiltInTypeNameString(),
583 node->getRight()->getType().getBuiltInTypeNameString()));
584 TIntermNode *replacement = createCompoundAssignmentFunctionCallNode(
585 node->getLeft(), node->getRight(), "mul");
586 queueReplacement(node, replacement, OriginalNode::IS_DROPPED);
587 break;
588 }
589 case EOpDivAssign:
590 {
591 mEmulateCompoundDiv.insert(
592 TypePair(type.getBuiltInTypeNameString(),
593 node->getRight()->getType().getBuiltInTypeNameString()));
594 TIntermNode *replacement = createCompoundAssignmentFunctionCallNode(
595 node->getLeft(), node->getRight(), "div");
596 queueReplacement(node, replacement, OriginalNode::IS_DROPPED);
597 break;
598 }
599 default:
600 // The rest of the binary operations should not need precision emulation.
601 break;
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200602 }
603 }
604 return visitChildren;
605}
606
Olli Etuaho13389b62016-10-16 11:48:18 +0100607bool EmulatePrecision::visitDeclaration(Visit visit, TIntermDeclaration *node)
608{
609 // Variable or interface block declaration.
610 if (visit == PreVisit)
611 {
612 mDeclaringVariables = true;
613 }
614 else if (visit == InVisit)
615 {
616 mDeclaringVariables = true;
617 }
618 else
619 {
620 mDeclaringVariables = false;
621 }
622 return true;
623}
624
Olli Etuahobf4e1b72016-12-09 11:30:15 +0000625bool EmulatePrecision::visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node)
626{
627 return false;
628}
629
Olli Etuaho16c745a2017-01-16 17:02:27 +0000630bool EmulatePrecision::visitFunctionPrototype(Visit visit, TIntermFunctionPrototype *node)
631{
632 return false;
633}
634
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200635bool EmulatePrecision::visitAggregate(Visit visit, TIntermAggregate *node)
636{
637 bool visitChildren = true;
638 switch (node->getOp())
639 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500640 case EOpConstructStruct:
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800641 case EOpCallInternalRawFunction:
642 case EOpCallFunctionInAST:
643 // User-defined function return values are not rounded. The calculations that produced
644 // the value inside the function definition should have been rounded.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500645 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500646 default:
Olli Etuaho1be88702015-01-19 16:56:44 +0200647 TIntermNode *parent = getParentNode();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500648 if (canRoundFloat(node->getType()) && visit == PreVisit &&
Olli Etuahoa26ad582015-08-04 13:51:47 +0300649 parentUsesResult(parent, node))
Olli Etuaho1be88702015-01-19 16:56:44 +0200650 {
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200651 TIntermNode *replacement = createRoundingFunctionCallNode(node);
Jamie Madill03d863c2016-07-27 18:15:53 -0400652 queueReplacement(node, replacement, OriginalNode::BECOMES_CHILD);
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200653 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500654 break;
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200655 }
656 return visitChildren;
657}
658
659bool EmulatePrecision::visitUnary(Visit visit, TIntermUnary *node)
660{
661 switch (node->getOp())
662 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500663 case EOpNegative:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500664 case EOpLogicalNot:
665 case EOpPostIncrement:
666 case EOpPostDecrement:
667 case EOpPreIncrement:
668 case EOpPreDecrement:
Olli Etuahod68924e2017-01-02 17:34:40 +0000669 case EOpLogicalNotComponentWise:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500670 break;
671 default:
672 if (canRoundFloat(node->getType()) && visit == PreVisit)
673 {
674 TIntermNode *replacement = createRoundingFunctionCallNode(node);
675 queueReplacement(node, replacement, OriginalNode::BECOMES_CHILD);
676 }
677 break;
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200678 }
679
680 return true;
681}
682
Olli Etuaho61b81ac2016-06-28 14:15:20 +0300683void EmulatePrecision::writeEmulationHelpers(TInfoSinkBase &sink,
684 const int shaderVersion,
685 const ShShaderOutput outputLanguage)
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200686{
Olli Etuahob741c762016-06-29 15:49:22 +0300687 std::unique_ptr<RoundingHelperWriter> roundingHelperWriter(
688 RoundingHelperWriter::createHelperWriter(outputLanguage));
689
690 roundingHelperWriter->writeCommonRoundingHelpers(sink, shaderVersion);
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200691
692 EmulationSet::const_iterator it;
693 for (it = mEmulateCompoundAdd.begin(); it != mEmulateCompoundAdd.end(); it++)
Olli Etuahob741c762016-06-29 15:49:22 +0300694 roundingHelperWriter->writeCompoundAssignmentHelper(sink, it->lType, it->rType, "+", "add");
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200695 for (it = mEmulateCompoundSub.begin(); it != mEmulateCompoundSub.end(); it++)
Olli Etuahob741c762016-06-29 15:49:22 +0300696 roundingHelperWriter->writeCompoundAssignmentHelper(sink, it->lType, it->rType, "-", "sub");
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200697 for (it = mEmulateCompoundDiv.begin(); it != mEmulateCompoundDiv.end(); it++)
Olli Etuahob741c762016-06-29 15:49:22 +0300698 roundingHelperWriter->writeCompoundAssignmentHelper(sink, it->lType, it->rType, "/", "div");
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200699 for (it = mEmulateCompoundMul.begin(); it != mEmulateCompoundMul.end(); it++)
Olli Etuahob741c762016-06-29 15:49:22 +0300700 roundingHelperWriter->writeCompoundAssignmentHelper(sink, it->lType, it->rType, "*", "mul");
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200701}
702
Jamie Madilld5696192016-10-06 11:09:24 -0400703// static
704bool EmulatePrecision::SupportedInLanguage(const ShShaderOutput outputLanguage)
705{
706 switch (outputLanguage)
707 {
708 case SH_HLSL_4_1_OUTPUT:
709 case SH_ESSL_OUTPUT:
710 return true;
711 default:
712 // Other languages not yet supported
713 return (outputLanguage == SH_GLSL_COMPATIBILITY_OUTPUT ||
Jamie Madillacb4b812016-11-07 13:50:29 -0500714 sh::IsGLSL130OrNewer(outputLanguage));
Jamie Madilld5696192016-10-06 11:09:24 -0400715 }
716}
Jamie Madill45bcc782016-11-07 13:58:48 -0500717
718} // namespace sh