blob: 4931c59162fdf9ef840c2790d6d3d71623fa574c [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
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200175bool canRoundFloat(const TType &type)
176{
177 return type.getBasicType() == EbtFloat && !type.isNonSquareMatrix() && !type.isArray() &&
178 (type.getPrecision() == EbpLow || type.getPrecision() == EbpMedium);
179}
180
181TIntermAggregate *createInternalFunctionCallNode(TString name, TIntermNode *child)
182{
183 TIntermAggregate *callNode = new TIntermAggregate();
Olli Etuaho59f9a642015-08-06 20:38:26 +0300184 callNode->setOp(EOpFunctionCall);
185 TName nameObj(TFunction::mangleName(name));
186 nameObj.setInternal(true);
187 callNode->setNameObj(nameObj);
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200188 callNode->getSequence()->push_back(child);
189 return callNode;
190}
191
192TIntermAggregate *createRoundingFunctionCallNode(TIntermTyped *roundedChild)
193{
194 TString roundFunctionName;
195 if (roundedChild->getPrecision() == EbpMedium)
196 roundFunctionName = "angle_frm";
197 else
198 roundFunctionName = "angle_frl";
Olli Etuaho3820e9c2016-07-04 16:01:15 +0300199 TIntermAggregate *callNode = createInternalFunctionCallNode(roundFunctionName, roundedChild);
200 callNode->setType(roundedChild->getType());
201 return callNode;
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200202}
203
204TIntermAggregate *createCompoundAssignmentFunctionCallNode(TIntermTyped *left, TIntermTyped *right, const char *opNameStr)
205{
206 std::stringstream strstr;
207 if (left->getPrecision() == EbpMedium)
208 strstr << "angle_compound_" << opNameStr << "_frm";
209 else
210 strstr << "angle_compound_" << opNameStr << "_frl";
211 TString functionName = strstr.str().c_str();
212 TIntermAggregate *callNode = createInternalFunctionCallNode(functionName, left);
213 callNode->getSequence()->push_back(right);
214 return callNode;
215}
216
Olli Etuaho1be88702015-01-19 16:56:44 +0200217bool parentUsesResult(TIntermNode* parent, TIntermNode* node)
218{
219 if (!parent)
220 {
221 return false;
222 }
223
224 TIntermAggregate *aggParent = parent->getAsAggregate();
225 // If the parent's op is EOpSequence, the result is not assigned anywhere,
226 // so rounding it is not needed. In particular, this can avoid a lot of
227 // unnecessary rounding of unused return values of assignment.
228 if (aggParent && aggParent->getOp() == EOpSequence)
229 {
230 return false;
231 }
232 if (aggParent && aggParent->getOp() == EOpComma && (aggParent->getSequence()->back() != node))
233 {
234 return false;
235 }
236 return true;
237}
238
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200239} // namespace anonymous
240
Olli Etuaho217fe6e2015-08-05 13:25:08 +0300241EmulatePrecision::EmulatePrecision(const TSymbolTable &symbolTable, int shaderVersion)
242 : TLValueTrackingTraverser(true, true, true, symbolTable, shaderVersion),
243 mDeclaringVariables(false)
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200244{}
245
246void EmulatePrecision::visitSymbol(TIntermSymbol *node)
247{
Olli Etuahoa26ad582015-08-04 13:51:47 +0300248 if (canRoundFloat(node->getType()) && !mDeclaringVariables && !isLValueRequiredHere())
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200249 {
250 TIntermNode *parent = getParentNode();
251 TIntermNode *replacement = createRoundingFunctionCallNode(node);
252 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, true));
253 }
254}
255
256
257bool EmulatePrecision::visitBinary(Visit visit, TIntermBinary *node)
258{
259 bool visitChildren = true;
260
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200261 TOperator op = node->getOp();
262
263 // RHS of initialize is not being declared.
264 if (op == EOpInitialize && visit == InVisit)
265 mDeclaringVariables = false;
266
267 if ((op == EOpIndexDirectStruct || op == EOpVectorSwizzle) && visit == InVisit)
268 visitChildren = false;
269
270 if (visit != PreVisit)
271 return visitChildren;
272
273 const TType& type = node->getType();
274 bool roundFloat = canRoundFloat(type);
275
276 if (roundFloat) {
277 switch (op) {
278 // Math operators that can result in a float may need to apply rounding to the return
279 // value. Note that in the case of assignment, the rounding is applied to its return
280 // value here, not the value being assigned.
281 case EOpAssign:
282 case EOpAdd:
283 case EOpSub:
284 case EOpMul:
285 case EOpDiv:
286 case EOpVectorTimesScalar:
287 case EOpVectorTimesMatrix:
288 case EOpMatrixTimesVector:
289 case EOpMatrixTimesScalar:
290 case EOpMatrixTimesMatrix:
291 {
292 TIntermNode *parent = getParentNode();
Olli Etuaho1be88702015-01-19 16:56:44 +0200293 if (!parentUsesResult(parent, node))
294 {
295 break;
296 }
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200297 TIntermNode *replacement = createRoundingFunctionCallNode(node);
298 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, true));
299 break;
300 }
301
302 // Compound assignment cases need to replace the operator with a function call.
303 case EOpAddAssign:
304 {
Olli Etuahoe92507b2016-07-04 11:20:10 +0300305 mEmulateCompoundAdd.insert(
306 TypePair(type.getBuiltInTypeNameString(),
307 node->getRight()->getType().getBuiltInTypeNameString()));
308 TIntermNode *parent = getParentNode();
309 TIntermNode *replacement = createCompoundAssignmentFunctionCallNode(
310 node->getLeft(), node->getRight(), "add");
311 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, false));
312 break;
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200313 }
314 case EOpSubAssign:
315 {
Olli Etuahoe92507b2016-07-04 11:20:10 +0300316 mEmulateCompoundSub.insert(
317 TypePair(type.getBuiltInTypeNameString(),
318 node->getRight()->getType().getBuiltInTypeNameString()));
319 TIntermNode *parent = getParentNode();
320 TIntermNode *replacement = createCompoundAssignmentFunctionCallNode(
321 node->getLeft(), node->getRight(), "sub");
322 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, false));
323 break;
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200324 }
325 case EOpMulAssign:
326 case EOpVectorTimesMatrixAssign:
327 case EOpVectorTimesScalarAssign:
328 case EOpMatrixTimesScalarAssign:
329 case EOpMatrixTimesMatrixAssign:
330 {
Olli Etuahoe92507b2016-07-04 11:20:10 +0300331 mEmulateCompoundMul.insert(
332 TypePair(type.getBuiltInTypeNameString(),
333 node->getRight()->getType().getBuiltInTypeNameString()));
334 TIntermNode *parent = getParentNode();
335 TIntermNode *replacement = createCompoundAssignmentFunctionCallNode(
336 node->getLeft(), node->getRight(), "mul");
337 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, false));
338 break;
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200339 }
340 case EOpDivAssign:
341 {
Olli Etuahoe92507b2016-07-04 11:20:10 +0300342 mEmulateCompoundDiv.insert(
343 TypePair(type.getBuiltInTypeNameString(),
344 node->getRight()->getType().getBuiltInTypeNameString()));
345 TIntermNode *parent = getParentNode();
346 TIntermNode *replacement = createCompoundAssignmentFunctionCallNode(
347 node->getLeft(), node->getRight(), "div");
348 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, false));
349 break;
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200350 }
351 default:
352 // The rest of the binary operations should not need precision emulation.
353 break;
354 }
355 }
356 return visitChildren;
357}
358
359bool EmulatePrecision::visitAggregate(Visit visit, TIntermAggregate *node)
360{
361 bool visitChildren = true;
362 switch (node->getOp())
363 {
364 case EOpSequence:
365 case EOpConstructStruct:
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200366 case EOpFunction:
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200367 break;
368 case EOpPrototype:
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200369 visitChildren = false;
370 break;
371 case EOpParameters:
372 visitChildren = false;
373 break;
374 case EOpInvariantDeclaration:
375 visitChildren = false;
376 break;
377 case EOpDeclaration:
378 // Variable declaration.
379 if (visit == PreVisit)
380 {
381 mDeclaringVariables = true;
382 }
383 else if (visit == InVisit)
384 {
385 mDeclaringVariables = true;
386 }
387 else
388 {
389 mDeclaringVariables = false;
390 }
391 break;
392 case EOpFunctionCall:
393 {
394 // Function call.
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200395 if (visit == PreVisit)
396 {
Olli Etuaho1be88702015-01-19 16:56:44 +0200397 // User-defined function return values are not rounded, this relies on that
398 // calculations producing the value were rounded.
399 TIntermNode *parent = getParentNode();
Olli Etuahoa26ad582015-08-04 13:51:47 +0300400 if (canRoundFloat(node->getType()) && !isInFunctionMap(node) &&
401 parentUsesResult(parent, node))
Olli Etuaho1be88702015-01-19 16:56:44 +0200402 {
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200403 TIntermNode *replacement = createRoundingFunctionCallNode(node);
404 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, true));
405 }
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200406 }
407 break;
408 }
409 default:
Olli Etuaho1be88702015-01-19 16:56:44 +0200410 TIntermNode *parent = getParentNode();
411 if (canRoundFloat(node->getType()) && visit == PreVisit && parentUsesResult(parent, node))
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200412 {
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200413 TIntermNode *replacement = createRoundingFunctionCallNode(node);
414 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, true));
415 }
416 break;
417 }
418 return visitChildren;
419}
420
421bool EmulatePrecision::visitUnary(Visit visit, TIntermUnary *node)
422{
423 switch (node->getOp())
424 {
425 case EOpNegative:
426 case EOpVectorLogicalNot:
427 case EOpLogicalNot:
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200428 case EOpPostIncrement:
429 case EOpPostDecrement:
430 case EOpPreIncrement:
431 case EOpPreDecrement:
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200432 break;
433 default:
434 if (canRoundFloat(node->getType()) && visit == PreVisit)
435 {
436 TIntermNode *parent = getParentNode();
437 TIntermNode *replacement = createRoundingFunctionCallNode(node);
438 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, true));
439 }
440 break;
441 }
442
443 return true;
444}
445
446void EmulatePrecision::writeEmulationHelpers(TInfoSinkBase& sink, ShShaderOutput outputLanguage)
447{
448 // Other languages not yet supported
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800449 ASSERT(outputLanguage == SH_GLSL_COMPATIBILITY_OUTPUT ||
Qingqing Dengad0d0792015-04-08 14:25:06 -0700450 IsGLSL130OrNewer(outputLanguage) ||
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800451 outputLanguage == SH_ESSL_OUTPUT);
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200452 writeCommonPrecisionEmulationHelpers(sink, outputLanguage);
453
454 EmulationSet::const_iterator it;
455 for (it = mEmulateCompoundAdd.begin(); it != mEmulateCompoundAdd.end(); it++)
456 writeCompoundAssignmentPrecisionEmulation(sink, outputLanguage, it->lType, it->rType, "+", "add");
457 for (it = mEmulateCompoundSub.begin(); it != mEmulateCompoundSub.end(); it++)
458 writeCompoundAssignmentPrecisionEmulation(sink, outputLanguage, it->lType, it->rType, "-", "sub");
459 for (it = mEmulateCompoundDiv.begin(); it != mEmulateCompoundDiv.end(); it++)
460 writeCompoundAssignmentPrecisionEmulation(sink, outputLanguage, it->lType, it->rType, "/", "div");
461 for (it = mEmulateCompoundMul.begin(); it != mEmulateCompoundMul.end(); it++)
462 writeCompoundAssignmentPrecisionEmulation(sink, outputLanguage, it->lType, it->rType, "*", "mul");
463}
464