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