blob: 3729b0ed140c1286970eb46b00c0750d038e182f [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:
182 return type.getSecondarySize() > 1 ? "mat2" : "vec2";
183 case 3:
184 return type.getSecondarySize() > 1 ? "mat3" : "vec3";
185 case 4:
186 return type.getSecondarySize() > 1 ? "mat4" : "vec4";
187 default:
188 UNREACHABLE();
189 return NULL;
190 }
191}
192
193bool canRoundFloat(const TType &type)
194{
195 return type.getBasicType() == EbtFloat && !type.isNonSquareMatrix() && !type.isArray() &&
196 (type.getPrecision() == EbpLow || type.getPrecision() == EbpMedium);
197}
198
199TIntermAggregate *createInternalFunctionCallNode(TString name, TIntermNode *child)
200{
201 TIntermAggregate *callNode = new TIntermAggregate();
202 callNode->setOp(EOpInternalFunctionCall);
203 callNode->setName(name);
204 callNode->getSequence()->push_back(child);
205 return callNode;
206}
207
208TIntermAggregate *createRoundingFunctionCallNode(TIntermTyped *roundedChild)
209{
210 TString roundFunctionName;
211 if (roundedChild->getPrecision() == EbpMedium)
212 roundFunctionName = "angle_frm";
213 else
214 roundFunctionName = "angle_frl";
215 return createInternalFunctionCallNode(roundFunctionName, roundedChild);
216}
217
218TIntermAggregate *createCompoundAssignmentFunctionCallNode(TIntermTyped *left, TIntermTyped *right, const char *opNameStr)
219{
220 std::stringstream strstr;
221 if (left->getPrecision() == EbpMedium)
222 strstr << "angle_compound_" << opNameStr << "_frm";
223 else
224 strstr << "angle_compound_" << opNameStr << "_frl";
225 TString functionName = strstr.str().c_str();
226 TIntermAggregate *callNode = createInternalFunctionCallNode(functionName, left);
227 callNode->getSequence()->push_back(right);
228 return callNode;
229}
230
Olli Etuaho1be88702015-01-19 16:56:44 +0200231bool parentUsesResult(TIntermNode* parent, TIntermNode* node)
232{
233 if (!parent)
234 {
235 return false;
236 }
237
238 TIntermAggregate *aggParent = parent->getAsAggregate();
239 // If the parent's op is EOpSequence, the result is not assigned anywhere,
240 // so rounding it is not needed. In particular, this can avoid a lot of
241 // unnecessary rounding of unused return values of assignment.
242 if (aggParent && aggParent->getOp() == EOpSequence)
243 {
244 return false;
245 }
246 if (aggParent && aggParent->getOp() == EOpComma && (aggParent->getSequence()->back() != node))
247 {
248 return false;
249 }
250 return true;
251}
252
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200253} // namespace anonymous
254
255EmulatePrecision::EmulatePrecision()
256 : TIntermTraverser(true, true, true),
257 mDeclaringVariables(false),
258 mInLValue(false),
259 mInFunctionCallOutParameter(false)
260{}
261
262void EmulatePrecision::visitSymbol(TIntermSymbol *node)
263{
264 if (canRoundFloat(node->getType()) &&
265 !mDeclaringVariables && !mInLValue && !mInFunctionCallOutParameter)
266 {
267 TIntermNode *parent = getParentNode();
268 TIntermNode *replacement = createRoundingFunctionCallNode(node);
269 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, true));
270 }
271}
272
273
274bool EmulatePrecision::visitBinary(Visit visit, TIntermBinary *node)
275{
276 bool visitChildren = true;
277
278 if (node->isAssignment())
279 {
280 if (visit == PreVisit)
281 mInLValue = true;
282 else if (visit == InVisit)
283 mInLValue = false;
284 }
285
286 TOperator op = node->getOp();
287
288 // RHS of initialize is not being declared.
289 if (op == EOpInitialize && visit == InVisit)
290 mDeclaringVariables = false;
291
292 if ((op == EOpIndexDirectStruct || op == EOpVectorSwizzle) && visit == InVisit)
293 visitChildren = false;
294
295 if (visit != PreVisit)
296 return visitChildren;
297
298 const TType& type = node->getType();
299 bool roundFloat = canRoundFloat(type);
300
301 if (roundFloat) {
302 switch (op) {
303 // Math operators that can result in a float may need to apply rounding to the return
304 // value. Note that in the case of assignment, the rounding is applied to its return
305 // value here, not the value being assigned.
306 case EOpAssign:
307 case EOpAdd:
308 case EOpSub:
309 case EOpMul:
310 case EOpDiv:
311 case EOpVectorTimesScalar:
312 case EOpVectorTimesMatrix:
313 case EOpMatrixTimesVector:
314 case EOpMatrixTimesScalar:
315 case EOpMatrixTimesMatrix:
316 {
317 TIntermNode *parent = getParentNode();
Olli Etuaho1be88702015-01-19 16:56:44 +0200318 if (!parentUsesResult(parent, node))
319 {
320 break;
321 }
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200322 TIntermNode *replacement = createRoundingFunctionCallNode(node);
323 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, true));
324 break;
325 }
326
327 // Compound assignment cases need to replace the operator with a function call.
328 case EOpAddAssign:
329 {
330 mEmulateCompoundAdd.insert(TypePair(getFloatTypeStr(type), getFloatTypeStr(node->getRight()->getType())));
331 TIntermNode *parent = getParentNode();
332 TIntermNode *replacement = createCompoundAssignmentFunctionCallNode(node->getLeft(), node->getRight(), "add");
333 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, false));
334 break;
335 }
336 case EOpSubAssign:
337 {
338 mEmulateCompoundSub.insert(TypePair(getFloatTypeStr(type), getFloatTypeStr(node->getRight()->getType())));
339 TIntermNode *parent = getParentNode();
340 TIntermNode *replacement = createCompoundAssignmentFunctionCallNode(node->getLeft(), node->getRight(), "sub");
341 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, false));
342 break;
343 }
344 case EOpMulAssign:
345 case EOpVectorTimesMatrixAssign:
346 case EOpVectorTimesScalarAssign:
347 case EOpMatrixTimesScalarAssign:
348 case EOpMatrixTimesMatrixAssign:
349 {
350 mEmulateCompoundMul.insert(TypePair(getFloatTypeStr(type), getFloatTypeStr(node->getRight()->getType())));
351 TIntermNode *parent = getParentNode();
352 TIntermNode *replacement = createCompoundAssignmentFunctionCallNode(node->getLeft(), node->getRight(), "mul");
353 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, false));
354 break;
355 }
356 case EOpDivAssign:
357 {
358 mEmulateCompoundDiv.insert(TypePair(getFloatTypeStr(type), getFloatTypeStr(node->getRight()->getType())));
359 TIntermNode *parent = getParentNode();
360 TIntermNode *replacement = createCompoundAssignmentFunctionCallNode(node->getLeft(), node->getRight(), "div");
361 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, false));
362 break;
363 }
364 default:
365 // The rest of the binary operations should not need precision emulation.
366 break;
367 }
368 }
369 return visitChildren;
370}
371
372bool EmulatePrecision::visitAggregate(Visit visit, TIntermAggregate *node)
373{
374 bool visitChildren = true;
375 switch (node->getOp())
376 {
377 case EOpSequence:
378 case EOpConstructStruct:
379 // No special handling
380 break;
381 case EOpFunction:
382 if (visit == PreVisit)
383 {
384 const TIntermSequence &sequence = *(node->getSequence());
385 TIntermSequence::const_iterator seqIter = sequence.begin();
386 TIntermAggregate *params = (*seqIter)->getAsAggregate();
387 ASSERT(params != NULL);
388 ASSERT(params->getOp() == EOpParameters);
389 mFunctionMap[node->getName()] = params->getSequence();
390 }
391 break;
392 case EOpPrototype:
393 if (visit == PreVisit)
394 mFunctionMap[node->getName()] = node->getSequence();
395 visitChildren = false;
396 break;
397 case EOpParameters:
398 visitChildren = false;
399 break;
400 case EOpInvariantDeclaration:
401 visitChildren = false;
402 break;
403 case EOpDeclaration:
404 // Variable declaration.
405 if (visit == PreVisit)
406 {
407 mDeclaringVariables = true;
408 }
409 else if (visit == InVisit)
410 {
411 mDeclaringVariables = true;
412 }
413 else
414 {
415 mDeclaringVariables = false;
416 }
417 break;
418 case EOpFunctionCall:
419 {
420 // Function call.
421 bool inFunctionMap = (mFunctionMap.find(node->getName()) != mFunctionMap.end());
422 if (visit == PreVisit)
423 {
Olli Etuaho1be88702015-01-19 16:56:44 +0200424 // User-defined function return values are not rounded, this relies on that
425 // calculations producing the value were rounded.
426 TIntermNode *parent = getParentNode();
427 if (canRoundFloat(node->getType()) && !inFunctionMap && parentUsesResult(parent, node))
428 {
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200429 TIntermNode *replacement = createRoundingFunctionCallNode(node);
430 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, true));
431 }
432
433 if (inFunctionMap)
434 {
435 mSeqIterStack.push_back(mFunctionMap[node->getName()]->begin());
436 if (mSeqIterStack.back() != mFunctionMap[node->getName()]->end())
437 {
438 TQualifier qualifier = (*mSeqIterStack.back())->getAsTyped()->getQualifier();
439 mInFunctionCallOutParameter = (qualifier == EvqOut || qualifier == EvqInOut);
440 }
441 }
442 else
443 {
444 // The function is not user-defined - it is likely built-in texture function.
445 // Assume that those do not have out parameters.
446 mInFunctionCallOutParameter = false;
447 }
448 }
449 else if (visit == InVisit)
450 {
451 if (inFunctionMap)
452 {
453 ++mSeqIterStack.back();
454 TQualifier qualifier = (*mSeqIterStack.back())->getAsTyped()->getQualifier();
455 mInFunctionCallOutParameter = (qualifier == EvqOut || qualifier == EvqInOut);
456 }
457 }
458 else
459 {
460 if (inFunctionMap)
461 {
462 mSeqIterStack.pop_back();
463 mInFunctionCallOutParameter = false;
464 }
465 }
466 break;
467 }
468 default:
Olli Etuaho1be88702015-01-19 16:56:44 +0200469 TIntermNode *parent = getParentNode();
470 if (canRoundFloat(node->getType()) && visit == PreVisit && parentUsesResult(parent, node))
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200471 {
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200472 TIntermNode *replacement = createRoundingFunctionCallNode(node);
473 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, true));
474 }
475 break;
476 }
477 return visitChildren;
478}
479
480bool EmulatePrecision::visitUnary(Visit visit, TIntermUnary *node)
481{
482 switch (node->getOp())
483 {
484 case EOpNegative:
485 case EOpVectorLogicalNot:
486 case EOpLogicalNot:
487 break;
488 case EOpPostIncrement:
489 case EOpPostDecrement:
490 case EOpPreIncrement:
491 case EOpPreDecrement:
492 if (visit == PreVisit)
493 mInLValue = true;
494 else if (visit == PostVisit)
495 mInLValue = false;
496 break;
497 default:
498 if (canRoundFloat(node->getType()) && visit == PreVisit)
499 {
500 TIntermNode *parent = getParentNode();
501 TIntermNode *replacement = createRoundingFunctionCallNode(node);
502 mReplacements.push_back(NodeUpdateEntry(parent, node, replacement, true));
503 }
504 break;
505 }
506
507 return true;
508}
509
510void EmulatePrecision::writeEmulationHelpers(TInfoSinkBase& sink, ShShaderOutput outputLanguage)
511{
512 // Other languages not yet supported
513 ASSERT(outputLanguage == SH_GLSL_OUTPUT || outputLanguage == SH_ESSL_OUTPUT);
514 writeCommonPrecisionEmulationHelpers(sink, outputLanguage);
515
516 EmulationSet::const_iterator it;
517 for (it = mEmulateCompoundAdd.begin(); it != mEmulateCompoundAdd.end(); it++)
518 writeCompoundAssignmentPrecisionEmulation(sink, outputLanguage, it->lType, it->rType, "+", "add");
519 for (it = mEmulateCompoundSub.begin(); it != mEmulateCompoundSub.end(); it++)
520 writeCompoundAssignmentPrecisionEmulation(sink, outputLanguage, it->lType, it->rType, "-", "sub");
521 for (it = mEmulateCompoundDiv.begin(); it != mEmulateCompoundDiv.end(); it++)
522 writeCompoundAssignmentPrecisionEmulation(sink, outputLanguage, it->lType, it->rType, "/", "div");
523 for (it = mEmulateCompoundMul.begin(); it != mEmulateCompoundMul.end(); it++)
524 writeCompoundAssignmentPrecisionEmulation(sink, outputLanguage, it->lType, it->rType, "*", "mul");
525}
526