blob: 888d336a3d2255401d1e34beb495690cde895771 [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
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800430TIntermAggregate *createInternalFunctionCallNode(const TType &type,
431 TString name,
432 TIntermSequence *arguments)
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200433{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800434 TName nameObj(TFunction::GetMangledNameFromCall(name, *arguments));
Olli Etuaho59f9a642015-08-06 20:38:26 +0300435 nameObj.setInternal(true);
Olli Etuahofe486322017-03-21 09:30:54 +0000436 TIntermAggregate *callNode =
437 TIntermAggregate::Create(type, EOpCallInternalRawFunction, arguments);
Olli Etuahobd674552016-10-06 13:28:42 +0100438 callNode->getFunctionSymbolInfo()->setNameObj(nameObj);
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200439 return callNode;
440}
441
442TIntermAggregate *createRoundingFunctionCallNode(TIntermTyped *roundedChild)
443{
444 TString roundFunctionName;
445 if (roundedChild->getPrecision() == EbpMedium)
446 roundFunctionName = "angle_frm";
447 else
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500448 roundFunctionName = "angle_frl";
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800449 TIntermSequence *arguments = new TIntermSequence();
450 arguments->push_back(roundedChild);
451 return createInternalFunctionCallNode(roundedChild->getType(), roundFunctionName, arguments);
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200452}
453
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500454TIntermAggregate *createCompoundAssignmentFunctionCallNode(TIntermTyped *left,
455 TIntermTyped *right,
456 const char *opNameStr)
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200457{
458 std::stringstream strstr;
459 if (left->getPrecision() == EbpMedium)
460 strstr << "angle_compound_" << opNameStr << "_frm";
461 else
462 strstr << "angle_compound_" << opNameStr << "_frl";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500463 TString functionName = strstr.str().c_str();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800464 TIntermSequence *arguments = new TIntermSequence();
465 arguments->push_back(left);
466 arguments->push_back(right);
467 return createInternalFunctionCallNode(TType(EbtVoid), functionName, arguments);
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200468}
469
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500470bool parentUsesResult(TIntermNode *parent, TIntermNode *node)
Olli Etuaho1be88702015-01-19 16:56:44 +0200471{
472 if (!parent)
473 {
474 return false;
475 }
476
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100477 TIntermBlock *blockParent = parent->getAsBlock();
478 // If the parent is a block, the result is not assigned anywhere,
Olli Etuaho1be88702015-01-19 16:56:44 +0200479 // so rounding it is not needed. In particular, this can avoid a lot of
480 // unnecessary rounding of unused return values of assignment.
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100481 if (blockParent)
Olli Etuaho1be88702015-01-19 16:56:44 +0200482 {
483 return false;
484 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100485 TIntermBinary *binaryParent = parent->getAsBinaryNode();
486 if (binaryParent && binaryParent->getOp() == EOpComma && (binaryParent->getRight() != node))
Olli Etuaho1be88702015-01-19 16:56:44 +0200487 {
488 return false;
489 }
490 return true;
491}
492
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200493} // namespace anonymous
494
Olli Etuaho217fe6e2015-08-05 13:25:08 +0300495EmulatePrecision::EmulatePrecision(const TSymbolTable &symbolTable, int shaderVersion)
496 : TLValueTrackingTraverser(true, true, true, symbolTable, shaderVersion),
497 mDeclaringVariables(false)
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500498{
499}
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200500
501void EmulatePrecision::visitSymbol(TIntermSymbol *node)
502{
Olli Etuahoa26ad582015-08-04 13:51:47 +0300503 if (canRoundFloat(node->getType()) && !mDeclaringVariables && !isLValueRequiredHere())
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200504 {
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200505 TIntermNode *replacement = createRoundingFunctionCallNode(node);
Jamie Madill03d863c2016-07-27 18:15:53 -0400506 queueReplacement(node, replacement, OriginalNode::BECOMES_CHILD);
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200507 }
508}
509
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200510bool EmulatePrecision::visitBinary(Visit visit, TIntermBinary *node)
511{
512 bool visitChildren = true;
513
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200514 TOperator op = node->getOp();
515
516 // RHS of initialize is not being declared.
517 if (op == EOpInitialize && visit == InVisit)
518 mDeclaringVariables = false;
519
Olli Etuahob6fa0432016-09-28 16:28:05 +0100520 if ((op == EOpIndexDirectStruct) && visit == InVisit)
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200521 visitChildren = false;
522
523 if (visit != PreVisit)
524 return visitChildren;
525
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500526 const TType &type = node->getType();
527 bool roundFloat = canRoundFloat(type);
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200528
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500529 if (roundFloat)
530 {
531 switch (op)
532 {
533 // Math operators that can result in a float may need to apply rounding to the return
534 // value. Note that in the case of assignment, the rounding is applied to its return
535 // value here, not the value being assigned.
536 case EOpAssign:
537 case EOpAdd:
538 case EOpSub:
539 case EOpMul:
540 case EOpDiv:
541 case EOpVectorTimesScalar:
542 case EOpVectorTimesMatrix:
543 case EOpMatrixTimesVector:
544 case EOpMatrixTimesScalar:
545 case EOpMatrixTimesMatrix:
Olli Etuaho1be88702015-01-19 16:56:44 +0200546 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500547 TIntermNode *parent = getParentNode();
548 if (!parentUsesResult(parent, node))
549 {
550 break;
551 }
552 TIntermNode *replacement = createRoundingFunctionCallNode(node);
553 queueReplacement(node, replacement, OriginalNode::BECOMES_CHILD);
Olli Etuaho1be88702015-01-19 16:56:44 +0200554 break;
555 }
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200556
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500557 // Compound assignment cases need to replace the operator with a function call.
558 case EOpAddAssign:
559 {
560 mEmulateCompoundAdd.insert(
561 TypePair(type.getBuiltInTypeNameString(),
562 node->getRight()->getType().getBuiltInTypeNameString()));
563 TIntermNode *replacement = createCompoundAssignmentFunctionCallNode(
564 node->getLeft(), node->getRight(), "add");
565 queueReplacement(node, replacement, OriginalNode::IS_DROPPED);
566 break;
567 }
568 case EOpSubAssign:
569 {
570 mEmulateCompoundSub.insert(
571 TypePair(type.getBuiltInTypeNameString(),
572 node->getRight()->getType().getBuiltInTypeNameString()));
573 TIntermNode *replacement = createCompoundAssignmentFunctionCallNode(
574 node->getLeft(), node->getRight(), "sub");
575 queueReplacement(node, replacement, OriginalNode::IS_DROPPED);
576 break;
577 }
578 case EOpMulAssign:
579 case EOpVectorTimesMatrixAssign:
580 case EOpVectorTimesScalarAssign:
581 case EOpMatrixTimesScalarAssign:
582 case EOpMatrixTimesMatrixAssign:
583 {
584 mEmulateCompoundMul.insert(
585 TypePair(type.getBuiltInTypeNameString(),
586 node->getRight()->getType().getBuiltInTypeNameString()));
587 TIntermNode *replacement = createCompoundAssignmentFunctionCallNode(
588 node->getLeft(), node->getRight(), "mul");
589 queueReplacement(node, replacement, OriginalNode::IS_DROPPED);
590 break;
591 }
592 case EOpDivAssign:
593 {
594 mEmulateCompoundDiv.insert(
595 TypePair(type.getBuiltInTypeNameString(),
596 node->getRight()->getType().getBuiltInTypeNameString()));
597 TIntermNode *replacement = createCompoundAssignmentFunctionCallNode(
598 node->getLeft(), node->getRight(), "div");
599 queueReplacement(node, replacement, OriginalNode::IS_DROPPED);
600 break;
601 }
602 default:
603 // The rest of the binary operations should not need precision emulation.
604 break;
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200605 }
606 }
607 return visitChildren;
608}
609
Olli Etuaho13389b62016-10-16 11:48:18 +0100610bool EmulatePrecision::visitDeclaration(Visit visit, TIntermDeclaration *node)
611{
612 // Variable or interface block declaration.
613 if (visit == PreVisit)
614 {
615 mDeclaringVariables = true;
616 }
617 else if (visit == InVisit)
618 {
619 mDeclaringVariables = true;
620 }
621 else
622 {
623 mDeclaringVariables = false;
624 }
625 return true;
626}
627
Olli Etuahobf4e1b72016-12-09 11:30:15 +0000628bool EmulatePrecision::visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node)
629{
630 return false;
631}
632
Olli Etuaho16c745a2017-01-16 17:02:27 +0000633bool EmulatePrecision::visitFunctionPrototype(Visit visit, TIntermFunctionPrototype *node)
634{
635 return false;
636}
637
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200638bool EmulatePrecision::visitAggregate(Visit visit, TIntermAggregate *node)
639{
640 bool visitChildren = true;
641 switch (node->getOp())
642 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500643 case EOpConstructStruct:
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800644 case EOpCallInternalRawFunction:
645 case EOpCallFunctionInAST:
646 // User-defined function return values are not rounded. The calculations that produced
647 // the value inside the function definition should have been rounded.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500648 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500649 default:
Olli Etuaho1be88702015-01-19 16:56:44 +0200650 TIntermNode *parent = getParentNode();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500651 if (canRoundFloat(node->getType()) && visit == PreVisit &&
Olli Etuahoa26ad582015-08-04 13:51:47 +0300652 parentUsesResult(parent, node))
Olli Etuaho1be88702015-01-19 16:56:44 +0200653 {
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200654 TIntermNode *replacement = createRoundingFunctionCallNode(node);
Jamie Madill03d863c2016-07-27 18:15:53 -0400655 queueReplacement(node, replacement, OriginalNode::BECOMES_CHILD);
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200656 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500657 break;
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200658 }
659 return visitChildren;
660}
661
662bool EmulatePrecision::visitUnary(Visit visit, TIntermUnary *node)
663{
664 switch (node->getOp())
665 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500666 case EOpNegative:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500667 case EOpLogicalNot:
668 case EOpPostIncrement:
669 case EOpPostDecrement:
670 case EOpPreIncrement:
671 case EOpPreDecrement:
Olli Etuahod68924e2017-01-02 17:34:40 +0000672 case EOpLogicalNotComponentWise:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500673 break;
674 default:
675 if (canRoundFloat(node->getType()) && visit == PreVisit)
676 {
677 TIntermNode *replacement = createRoundingFunctionCallNode(node);
678 queueReplacement(node, replacement, OriginalNode::BECOMES_CHILD);
679 }
680 break;
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200681 }
682
683 return true;
684}
685
Olli Etuaho61b81ac2016-06-28 14:15:20 +0300686void EmulatePrecision::writeEmulationHelpers(TInfoSinkBase &sink,
687 const int shaderVersion,
688 const ShShaderOutput outputLanguage)
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200689{
Olli Etuahob741c762016-06-29 15:49:22 +0300690 std::unique_ptr<RoundingHelperWriter> roundingHelperWriter(
691 RoundingHelperWriter::createHelperWriter(outputLanguage));
692
693 roundingHelperWriter->writeCommonRoundingHelpers(sink, shaderVersion);
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200694
695 EmulationSet::const_iterator it;
696 for (it = mEmulateCompoundAdd.begin(); it != mEmulateCompoundAdd.end(); it++)
Olli Etuahob741c762016-06-29 15:49:22 +0300697 roundingHelperWriter->writeCompoundAssignmentHelper(sink, it->lType, it->rType, "+", "add");
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200698 for (it = mEmulateCompoundSub.begin(); it != mEmulateCompoundSub.end(); it++)
Olli Etuahob741c762016-06-29 15:49:22 +0300699 roundingHelperWriter->writeCompoundAssignmentHelper(sink, it->lType, it->rType, "-", "sub");
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200700 for (it = mEmulateCompoundDiv.begin(); it != mEmulateCompoundDiv.end(); it++)
Olli Etuahob741c762016-06-29 15:49:22 +0300701 roundingHelperWriter->writeCompoundAssignmentHelper(sink, it->lType, it->rType, "/", "div");
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200702 for (it = mEmulateCompoundMul.begin(); it != mEmulateCompoundMul.end(); it++)
Olli Etuahob741c762016-06-29 15:49:22 +0300703 roundingHelperWriter->writeCompoundAssignmentHelper(sink, it->lType, it->rType, "*", "mul");
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200704}
705
Jamie Madilld5696192016-10-06 11:09:24 -0400706// static
707bool EmulatePrecision::SupportedInLanguage(const ShShaderOutput outputLanguage)
708{
709 switch (outputLanguage)
710 {
711 case SH_HLSL_4_1_OUTPUT:
712 case SH_ESSL_OUTPUT:
713 return true;
714 default:
715 // Other languages not yet supported
716 return (outputLanguage == SH_GLSL_COMPATIBILITY_OUTPUT ||
Jamie Madillacb4b812016-11-07 13:50:29 -0500717 sh::IsGLSL130OrNewer(outputLanguage));
Jamie Madilld5696192016-10-06 11:09:24 -0400718 }
719}
Jamie Madill45bcc782016-11-07 13:58:48 -0500720
721} // namespace sh