blob: 4a7fa54155832f4f55b15fae24e0735d270c2fb3 [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
9namespace
10{
11
12static void writeVectorPrecisionEmulationHelpers(
13 TInfoSinkBase& sink, ShShaderOutput outputLanguage, unsigned int size)
14{
15 std::stringstream vecTypeStrStr;
16 if (outputLanguage == SH_ESSL_OUTPUT)
17 vecTypeStrStr << "highp ";
18 vecTypeStrStr << "vec" << size;
19 std::string vecType = vecTypeStrStr.str();
20
21 sink <<
22 vecType << " angle_frm(in " << vecType << " v) {\n"
23 " v = clamp(v, -65504.0, 65504.0);\n"
24 " " << vecType << " exponent = floor(log2(abs(v) + 1e-30)) - 10.0;\n"
25 " bvec" << size << " isNonZero = greaterThanEqual(exponent, vec" << size << "(-25.0));\n"
26 " v = v * exp2(-exponent);\n"
27 " v = sign(v) * floor(abs(v));\n"
28 " return v * exp2(exponent) * vec" << size << "(isNonZero);\n"
29 "}\n";
30
31 sink <<
32 vecType << " angle_frl(in " << vecType << " v) {\n"
33 " v = clamp(v, -2.0, 2.0);\n"
34 " v = v * 256.0;\n"
35 " v = sign(v) * floor(abs(v));\n"
36 " return v * 0.00390625;\n"
37 "}\n";
38}
39
40static void writeMatrixPrecisionEmulationHelper(
41 TInfoSinkBase& sink, ShShaderOutput outputLanguage, unsigned int size, const char *functionName)
42{
43 std::stringstream matTypeStrStr;
44 if (outputLanguage == SH_ESSL_OUTPUT)
45 matTypeStrStr << "highp ";
46 matTypeStrStr << "mat" << size;
47 std::string matType = matTypeStrStr.str();
48
49 sink << matType << " " << functionName << "(in " << matType << " m) {\n"
50 " " << matType << " rounded;\n";
51
52 for (unsigned int i = 0; i < size; ++i)
53 {
54 sink << " rounded[" << i << "] = " << functionName << "(m[" << i << "]);\n";
55 }
56
57 sink << " return rounded;\n"
58 "}\n";
59}
60
61static void writeCommonPrecisionEmulationHelpers(TInfoSinkBase& sink, ShShaderOutput outputLanguage)
62{
63 // Write the angle_frm functions that round floating point numbers to
64 // half precision, and angle_frl functions that round them to minimum lowp
65 // precision.
66
67 // Unoptimized version of angle_frm for single floats:
68 //
69 // int webgl_maxNormalExponent(in int exponentBits) {
70 // int possibleExponents = int(exp2(float(exponentBits)));
71 // int exponentBias = possibleExponents / 2 - 1;
72 // int allExponentBitsOne = possibleExponents - 1;
73 // return (allExponentBitsOne - 1) - exponentBias;
74 // }
75 //
76 // float angle_frm(in float x) {
77 // int mantissaBits = 10;
78 // int exponentBits = 5;
79 // float possibleMantissas = exp2(float(mantissaBits));
80 // float mantissaMax = 2.0 - 1.0 / possibleMantissas;
81 // int maxNE = webgl_maxNormalExponent(exponentBits);
82 // float max = exp2(float(maxNE)) * mantissaMax;
83 // if (x > max) {
84 // return max;
85 // }
86 // if (x < -max) {
87 // return -max;
88 // }
89 // float exponent = floor(log2(abs(x)));
90 // if (abs(x) == 0.0 || exponent < -float(maxNE)) {
91 // return 0.0 * sign(x)
92 // }
93 // x = x * exp2(-(exponent - float(mantissaBits)));
94 // x = sign(x) * floor(abs(x));
95 // return x * exp2(exponent - float(mantissaBits));
96 // }
97
98 // All numbers with a magnitude less than 2^-15 are subnormal, and are
99 // flushed to zero.
100
101 // Note the constant numbers below:
102 // a) 65504 is the maximum possible mantissa (1.1111111111 in binary) times
103 // 2^15, the maximum normal exponent.
104 // b) 10.0 is the number of mantissa bits.
105 // c) -25.0 is the minimum normal half-float exponent -15.0 minus the number
106 // of mantissa bits.
107 // d) + 1e-30 is to make sure the argument of log2() won't be zero. It can
108 // only affect the result of log2 on x where abs(x) < 1e-22. Since these
109 // numbers will be flushed to zero either way (2^-15 is the smallest
110 // normal positive number), this does not introduce any error.
111
112 std::string floatType = "float";
113 if (outputLanguage == SH_ESSL_OUTPUT)
114 floatType = "highp float";
115
116 sink <<
117 floatType << " angle_frm(in " << floatType << " x) {\n"
118 " x = clamp(x, -65504.0, 65504.0);\n"
119 " " << floatType << " exponent = floor(log2(abs(x) + 1e-30)) - 10.0;\n"
120 " bool isNonZero = (exponent >= -25.0);\n"
121 " x = x * exp2(-exponent);\n"
122 " x = sign(x) * floor(abs(x));\n"
123 " return x * exp2(exponent) * float(isNonZero);\n"
124 "}\n";
125
126 sink <<
127 floatType << " angle_frl(in " << floatType << " x) {\n"
128 " x = clamp(x, -2.0, 2.0);\n"
129 " x = x * 256.0;\n"
130 " x = sign(x) * floor(abs(x));\n"
131 " return x * 0.00390625;\n"
132 "}\n";
133
134 writeVectorPrecisionEmulationHelpers(sink, outputLanguage, 2);
135 writeVectorPrecisionEmulationHelpers(sink, outputLanguage, 3);
136 writeVectorPrecisionEmulationHelpers(sink, outputLanguage, 4);
137 for (unsigned int size = 2; size <= 4; ++size)
138 {
139 writeMatrixPrecisionEmulationHelper(sink, outputLanguage, size, "angle_frm");
140 writeMatrixPrecisionEmulationHelper(sink, outputLanguage, size, "angle_frl");
141 }
142}
143
144static void writeCompoundAssignmentPrecisionEmulation(
145 TInfoSinkBase& sink, ShShaderOutput outputLanguage,
146 const char *lType, const char *rType, const char *opStr, const char *opNameStr)
147{
148 std::string lTypeStr = lType;
149 std::string rTypeStr = rType;
150 if (outputLanguage == SH_ESSL_OUTPUT)
151 {
152 std::stringstream lTypeStrStr;
153 lTypeStrStr << "highp " << lType;
154 lTypeStr = lTypeStrStr.str();
155 std::stringstream rTypeStrStr;
156 rTypeStrStr << "highp " << rType;
157 rTypeStr = rTypeStrStr.str();
158 }
159
160 // Note that y should be passed through angle_frm at the function call site,
161 // but x can't be passed through angle_frm there since it is an inout parameter.
162 // So only pass x and the result through angle_frm here.
163 sink <<
164 lTypeStr << " angle_compound_" << opNameStr << "_frm(inout " << lTypeStr << " x, in " << rTypeStr << " y) {\n"
165 " x = angle_frm(angle_frm(x) " << opStr << " y);\n"
166 " return x;\n"
167 "}\n";
168 sink <<
169 lTypeStr << " angle_compound_" << opNameStr << "_frl(inout " << lTypeStr << " x, in " << rTypeStr << " y) {\n"
170 " x = angle_frl(angle_frm(x) " << opStr << " y);\n"
171 " return x;\n"
172 "}\n";
173}
174
175const char *getFloatTypeStr(const TType& type)
176{
177 switch (type.getNominalSize())
178 {
179 case 1:
180 return "float";
181 case 2:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400182 switch(type.getSecondarySize())
183 {
184 case 1:
185 return "vec2";
186 case 2:
187 return "mat2";
188 case 3:
189 return "mat2x3";
190 case 4:
191 return "mat2x4";
192 default:
193 UNREACHABLE();
194 return NULL;
195 }
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200196 case 3:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400197 switch(type.getSecondarySize())
198 {
199 case 1:
200 return "vec3";
201 case 2:
202 return "mat3x2";
203 case 3:
204 return "mat3";
205 case 4:
206 return "mat3x4";
207 default:
208 UNREACHABLE();
209 return NULL;
210 }
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200211 case 4:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400212 switch(type.getSecondarySize())
213 {
214 case 1:
215 return "vec4";
216 case 2:
217 return "mat4x2";
218 case 3:
219 return "mat4x3";
220 case 4:
221 return "mat4";
222 default:
223 UNREACHABLE();
224 return NULL;
225 }
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200226 default:
227 UNREACHABLE();
228 return NULL;
229 }
230}
231
232bool canRoundFloat(const TType &type)
233{
234 return type.getBasicType() == EbtFloat && !type.isNonSquareMatrix() && !type.isArray() &&
235 (type.getPrecision() == EbpLow || type.getPrecision() == EbpMedium);
236}
237
238TIntermAggregate *createInternalFunctionCallNode(TString name, TIntermNode *child)
239{
240 TIntermAggregate *callNode = new TIntermAggregate();
Olli Etuaho59f9a642015-08-06 20:38:26 +0300241 callNode->setOp(EOpFunctionCall);
242 TName nameObj(TFunction::mangleName(name));
243 nameObj.setInternal(true);
244 callNode->setNameObj(nameObj);
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200245 callNode->getSequence()->push_back(child);
246 return callNode;
247}
248
249TIntermAggregate *createRoundingFunctionCallNode(TIntermTyped *roundedChild)
250{
251 TString roundFunctionName;
252 if (roundedChild->getPrecision() == EbpMedium)
253 roundFunctionName = "angle_frm";
254 else
255 roundFunctionName = "angle_frl";
256 return createInternalFunctionCallNode(roundFunctionName, roundedChild);
257}
258
259TIntermAggregate *createCompoundAssignmentFunctionCallNode(TIntermTyped *left, TIntermTyped *right, const char *opNameStr)
260{
261 std::stringstream strstr;
262 if (left->getPrecision() == EbpMedium)
263 strstr << "angle_compound_" << opNameStr << "_frm";
264 else
265 strstr << "angle_compound_" << opNameStr << "_frl";
266 TString functionName = strstr.str().c_str();
267 TIntermAggregate *callNode = createInternalFunctionCallNode(functionName, left);
268 callNode->getSequence()->push_back(right);
269 return callNode;
270}
271
Olli Etuaho1be88702015-01-19 16:56:44 +0200272bool parentUsesResult(TIntermNode* parent, TIntermNode* node)
273{
274 if (!parent)
275 {
276 return false;
277 }
278
279 TIntermAggregate *aggParent = parent->getAsAggregate();
280 // If the parent's op is EOpSequence, the result is not assigned anywhere,
281 // so rounding it is not needed. In particular, this can avoid a lot of
282 // unnecessary rounding of unused return values of assignment.
283 if (aggParent && aggParent->getOp() == EOpSequence)
284 {
285 return false;
286 }
287 if (aggParent && aggParent->getOp() == EOpComma && (aggParent->getSequence()->back() != node))
288 {
289 return false;
290 }
291 return true;
292}
293
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200294} // namespace anonymous
295
Olli Etuaho217fe6e2015-08-05 13:25:08 +0300296EmulatePrecision::EmulatePrecision(const TSymbolTable &symbolTable, int shaderVersion)
297 : TLValueTrackingTraverser(true, true, true, symbolTable, shaderVersion),
298 mDeclaringVariables(false)
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200299{}
300
301void EmulatePrecision::visitSymbol(TIntermSymbol *node)
302{
Olli Etuahoa26ad582015-08-04 13:51:47 +0300303 if (canRoundFloat(node->getType()) && !mDeclaringVariables && !isLValueRequiredHere())
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200304 {
305 TIntermNode *parent = getParentNode();
306 TIntermNode *replacement = createRoundingFunctionCallNode(node);
307 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, true));
308 }
309}
310
311
312bool EmulatePrecision::visitBinary(Visit visit, TIntermBinary *node)
313{
314 bool visitChildren = true;
315
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200316 TOperator op = node->getOp();
317
318 // RHS of initialize is not being declared.
319 if (op == EOpInitialize && visit == InVisit)
320 mDeclaringVariables = false;
321
322 if ((op == EOpIndexDirectStruct || op == EOpVectorSwizzle) && visit == InVisit)
323 visitChildren = false;
324
325 if (visit != PreVisit)
326 return visitChildren;
327
328 const TType& type = node->getType();
329 bool roundFloat = canRoundFloat(type);
330
331 if (roundFloat) {
332 switch (op) {
333 // Math operators that can result in a float may need to apply rounding to the return
334 // value. Note that in the case of assignment, the rounding is applied to its return
335 // value here, not the value being assigned.
336 case EOpAssign:
337 case EOpAdd:
338 case EOpSub:
339 case EOpMul:
340 case EOpDiv:
341 case EOpVectorTimesScalar:
342 case EOpVectorTimesMatrix:
343 case EOpMatrixTimesVector:
344 case EOpMatrixTimesScalar:
345 case EOpMatrixTimesMatrix:
346 {
347 TIntermNode *parent = getParentNode();
Olli Etuaho1be88702015-01-19 16:56:44 +0200348 if (!parentUsesResult(parent, node))
349 {
350 break;
351 }
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200352 TIntermNode *replacement = createRoundingFunctionCallNode(node);
353 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, true));
354 break;
355 }
356
357 // Compound assignment cases need to replace the operator with a function call.
358 case EOpAddAssign:
359 {
360 mEmulateCompoundAdd.insert(TypePair(getFloatTypeStr(type), getFloatTypeStr(node->getRight()->getType())));
361 TIntermNode *parent = getParentNode();
362 TIntermNode *replacement = createCompoundAssignmentFunctionCallNode(node->getLeft(), node->getRight(), "add");
363 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, false));
364 break;
365 }
366 case EOpSubAssign:
367 {
368 mEmulateCompoundSub.insert(TypePair(getFloatTypeStr(type), getFloatTypeStr(node->getRight()->getType())));
369 TIntermNode *parent = getParentNode();
370 TIntermNode *replacement = createCompoundAssignmentFunctionCallNode(node->getLeft(), node->getRight(), "sub");
371 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, false));
372 break;
373 }
374 case EOpMulAssign:
375 case EOpVectorTimesMatrixAssign:
376 case EOpVectorTimesScalarAssign:
377 case EOpMatrixTimesScalarAssign:
378 case EOpMatrixTimesMatrixAssign:
379 {
380 mEmulateCompoundMul.insert(TypePair(getFloatTypeStr(type), getFloatTypeStr(node->getRight()->getType())));
381 TIntermNode *parent = getParentNode();
382 TIntermNode *replacement = createCompoundAssignmentFunctionCallNode(node->getLeft(), node->getRight(), "mul");
383 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, false));
384 break;
385 }
386 case EOpDivAssign:
387 {
388 mEmulateCompoundDiv.insert(TypePair(getFloatTypeStr(type), getFloatTypeStr(node->getRight()->getType())));
389 TIntermNode *parent = getParentNode();
390 TIntermNode *replacement = createCompoundAssignmentFunctionCallNode(node->getLeft(), node->getRight(), "div");
391 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, false));
392 break;
393 }
394 default:
395 // The rest of the binary operations should not need precision emulation.
396 break;
397 }
398 }
399 return visitChildren;
400}
401
402bool EmulatePrecision::visitAggregate(Visit visit, TIntermAggregate *node)
403{
404 bool visitChildren = true;
405 switch (node->getOp())
406 {
407 case EOpSequence:
408 case EOpConstructStruct:
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200409 case EOpFunction:
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200410 break;
411 case EOpPrototype:
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200412 visitChildren = false;
413 break;
414 case EOpParameters:
415 visitChildren = false;
416 break;
417 case EOpInvariantDeclaration:
418 visitChildren = false;
419 break;
420 case EOpDeclaration:
421 // Variable declaration.
422 if (visit == PreVisit)
423 {
424 mDeclaringVariables = true;
425 }
426 else if (visit == InVisit)
427 {
428 mDeclaringVariables = true;
429 }
430 else
431 {
432 mDeclaringVariables = false;
433 }
434 break;
435 case EOpFunctionCall:
436 {
437 // Function call.
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200438 if (visit == PreVisit)
439 {
Olli Etuaho1be88702015-01-19 16:56:44 +0200440 // User-defined function return values are not rounded, this relies on that
441 // calculations producing the value were rounded.
442 TIntermNode *parent = getParentNode();
Olli Etuahoa26ad582015-08-04 13:51:47 +0300443 if (canRoundFloat(node->getType()) && !isInFunctionMap(node) &&
444 parentUsesResult(parent, node))
Olli Etuaho1be88702015-01-19 16:56:44 +0200445 {
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200446 TIntermNode *replacement = createRoundingFunctionCallNode(node);
447 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, true));
448 }
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200449 }
450 break;
451 }
452 default:
Olli Etuaho1be88702015-01-19 16:56:44 +0200453 TIntermNode *parent = getParentNode();
454 if (canRoundFloat(node->getType()) && visit == PreVisit && parentUsesResult(parent, node))
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200455 {
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200456 TIntermNode *replacement = createRoundingFunctionCallNode(node);
457 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, true));
458 }
459 break;
460 }
461 return visitChildren;
462}
463
464bool EmulatePrecision::visitUnary(Visit visit, TIntermUnary *node)
465{
466 switch (node->getOp())
467 {
468 case EOpNegative:
469 case EOpVectorLogicalNot:
470 case EOpLogicalNot:
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200471 case EOpPostIncrement:
472 case EOpPostDecrement:
473 case EOpPreIncrement:
474 case EOpPreDecrement:
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200475 break;
476 default:
477 if (canRoundFloat(node->getType()) && visit == PreVisit)
478 {
479 TIntermNode *parent = getParentNode();
480 TIntermNode *replacement = createRoundingFunctionCallNode(node);
481 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, true));
482 }
483 break;
484 }
485
486 return true;
487}
488
489void EmulatePrecision::writeEmulationHelpers(TInfoSinkBase& sink, ShShaderOutput outputLanguage)
490{
491 // Other languages not yet supported
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800492 ASSERT(outputLanguage == SH_GLSL_COMPATIBILITY_OUTPUT ||
Qingqing Dengad0d0792015-04-08 14:25:06 -0700493 IsGLSL130OrNewer(outputLanguage) ||
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800494 outputLanguage == SH_ESSL_OUTPUT);
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200495 writeCommonPrecisionEmulationHelpers(sink, outputLanguage);
496
497 EmulationSet::const_iterator it;
498 for (it = mEmulateCompoundAdd.begin(); it != mEmulateCompoundAdd.end(); it++)
499 writeCompoundAssignmentPrecisionEmulation(sink, outputLanguage, it->lType, it->rType, "+", "add");
500 for (it = mEmulateCompoundSub.begin(); it != mEmulateCompoundSub.end(); it++)
501 writeCompoundAssignmentPrecisionEmulation(sink, outputLanguage, it->lType, it->rType, "-", "sub");
502 for (it = mEmulateCompoundDiv.begin(); it != mEmulateCompoundDiv.end(); it++)
503 writeCompoundAssignmentPrecisionEmulation(sink, outputLanguage, it->lType, it->rType, "/", "div");
504 for (it = mEmulateCompoundMul.begin(); it != mEmulateCompoundMul.end(); it++)
505 writeCompoundAssignmentPrecisionEmulation(sink, outputLanguage, it->lType, it->rType, "*", "mul");
506}
507