ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 7 | |
John Stiles | 3738ef5 | 2021-04-13 10:41:57 -0400 | [diff] [blame] | 8 | #include "src/sksl/codegen/SkSLGLSLCodeGenerator.h" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 9 | |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 10 | #include <memory> |
| 11 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/sksl/SkSLCompiler.h" |
John Stiles | e3ae968 | 2021-08-05 10:35:01 -0400 | [diff] [blame] | 13 | #include "src/sksl/ir/SkSLConstructorArrayCast.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 15 | #include "src/sksl/ir/SkSLExtension.h" |
| 16 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 17 | #include "src/sksl/ir/SkSLModifiersDeclaration.h" |
| 18 | #include "src/sksl/ir/SkSLNop.h" |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 19 | #include "src/sksl/ir/SkSLStructDefinition.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/sksl/ir/SkSLVariableReference.h" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 21 | |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 22 | #ifndef SKSL_STANDALONE |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "include/private/SkOnce.h" |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 24 | #endif |
| 25 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 26 | namespace SkSL { |
| 27 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 28 | void GLSLCodeGenerator::write(skstd::string_view s) { |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 29 | if (!s.length()) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 30 | return; |
| 31 | } |
| 32 | if (fAtLineStart) { |
| 33 | for (int i = 0; i < fIndentation; i++) { |
| 34 | fOut->writeText(" "); |
| 35 | } |
| 36 | } |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 37 | fOut->write(s.data(), s.length()); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 38 | fAtLineStart = false; |
| 39 | } |
| 40 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 41 | void GLSLCodeGenerator::writeLine(skstd::string_view s) { |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 42 | this->write(s); |
John Stiles | e8b5a73 | 2021-03-12 13:25:52 -0500 | [diff] [blame] | 43 | fOut->writeText(fLineEnding); |
| 44 | fAtLineStart = true; |
| 45 | } |
| 46 | |
| 47 | void GLSLCodeGenerator::finishLine() { |
| 48 | if (!fAtLineStart) { |
| 49 | this->writeLine(); |
| 50 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 53 | void GLSLCodeGenerator::writeExtension(skstd::string_view name, bool require) { |
Ethan Nicholas | 88f6d37 | 2018-07-27 10:03:46 -0400 | [diff] [blame] | 54 | fExtensions.writeText("#extension "); |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 55 | fExtensions.write(name.data(), name.length()); |
Ethan Nicholas | 88f6d37 | 2018-07-27 10:03:46 -0400 | [diff] [blame] | 56 | fExtensions.writeText(require ? " : require\n" : " : enable\n"); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 59 | bool GLSLCodeGenerator::usesPrecisionModifiers() const { |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 60 | return this->caps().usesPrecisionModifiers(); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 61 | } |
| 62 | |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 63 | // Returns the name of the type with array dimensions, e.g. `float[2]`. |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 64 | String GLSLCodeGenerator::getTypeName(const Type& type) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 65 | switch (type.typeKind()) { |
| 66 | case Type::TypeKind::kVector: { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 67 | const Type& component = type.componentType(); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 68 | String result; |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 69 | if (component == *fContext.fTypes.fFloat || component == *fContext.fTypes.fHalf) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 70 | result = "vec"; |
| 71 | } |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 72 | else if (component.isSigned()) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 73 | result = "ivec"; |
| 74 | } |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 75 | else if (component.isUnsigned()) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 76 | result = "uvec"; |
| 77 | } |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 78 | else if (component == *fContext.fTypes.fBool) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 79 | result = "bvec"; |
| 80 | } |
| 81 | else { |
John Stiles | f57207b | 2021-02-02 17:50:34 -0500 | [diff] [blame] | 82 | SK_ABORT("unsupported vector type"); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 83 | } |
| 84 | result += to_string(type.columns()); |
| 85 | return result; |
| 86 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 87 | case Type::TypeKind::kMatrix: { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 88 | String result; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 89 | const Type& component = type.componentType(); |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 90 | if (component == *fContext.fTypes.fFloat || component == *fContext.fTypes.fHalf) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 91 | result = "mat"; |
| 92 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 93 | else { |
John Stiles | f57207b | 2021-02-02 17:50:34 -0500 | [diff] [blame] | 94 | SK_ABORT("unsupported matrix type"); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 95 | } |
| 96 | result += to_string(type.columns()); |
| 97 | if (type.columns() != type.rows()) { |
| 98 | result += "x"; |
| 99 | result += to_string(type.rows()); |
| 100 | } |
| 101 | return result; |
| 102 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 103 | case Type::TypeKind::kArray: { |
John Stiles | bb43b7e | 2020-12-03 17:36:32 -0500 | [diff] [blame] | 104 | String baseTypeName = this->getTypeName(type.componentType()); |
Brian Osman | b42c383 | 2021-08-27 15:19:03 -0400 | [diff] [blame] | 105 | return String::printf("%s[%d]", baseTypeName.c_str(), type.columns()); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 106 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 107 | case Type::TypeKind::kScalar: { |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 108 | if (type == *fContext.fTypes.fHalf) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 109 | return "float"; |
| 110 | } |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 111 | else if (type == *fContext.fTypes.fShort) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 112 | return "int"; |
| 113 | } |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 114 | else if (type == *fContext.fTypes.fUShort) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 115 | return "uint"; |
| 116 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 117 | else { |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 118 | return String(type.name()); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 119 | } |
| 120 | break; |
| 121 | } |
| 122 | default: |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 123 | return String(type.name()); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Brian Osman | 02bc522 | 2021-01-28 11:00:20 -0500 | [diff] [blame] | 127 | void GLSLCodeGenerator::writeStructDefinition(const StructDefinition& s) { |
| 128 | const Type& type = s.type(); |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 129 | this->write("struct "); |
| 130 | this->write(type.name()); |
| 131 | this->writeLine(" {"); |
| 132 | fIndentation++; |
| 133 | for (const auto& f : type.fields()) { |
| 134 | this->writeModifiers(f.fModifiers, false); |
| 135 | this->writeTypePrecision(*f.fType); |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 136 | const Type& baseType = f.fType->isArray() ? f.fType->componentType() : *f.fType; |
| 137 | this->writeType(baseType); |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 138 | this->write(" "); |
| 139 | this->write(f.fName); |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 140 | if (f.fType->isArray()) { |
| 141 | this->write("[" + to_string(f.fType->columns()) + "]"); |
| 142 | } |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 143 | this->writeLine(";"); |
| 144 | } |
| 145 | fIndentation--; |
Brian Osman | 02bc522 | 2021-01-28 11:00:20 -0500 | [diff] [blame] | 146 | this->writeLine("};"); |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 147 | } |
| 148 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 149 | void GLSLCodeGenerator::writeType(const Type& type) { |
Brian Osman | 02bc522 | 2021-01-28 11:00:20 -0500 | [diff] [blame] | 150 | this->write(this->getTypeName(type)); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void GLSLCodeGenerator::writeExpression(const Expression& expr, Precedence parentPrecedence) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 154 | switch (expr.kind()) { |
| 155 | case Expression::Kind::kBinary: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 156 | this->writeBinaryExpression(expr.as<BinaryExpression>(), parentPrecedence); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 157 | break; |
John Stiles | 3c74a53 | 2021-05-18 17:31:48 -0400 | [diff] [blame] | 158 | case Expression::Kind::kConstructorDiagonalMatrix: |
| 159 | this->writeConstructorDiagonalMatrix(expr.as<ConstructorDiagonalMatrix>(), |
| 160 | parentPrecedence); |
| 161 | break; |
John Stiles | e3ae968 | 2021-08-05 10:35:01 -0400 | [diff] [blame] | 162 | case Expression::Kind::kConstructorArrayCast: |
| 163 | this->writeExpression(*expr.as<ConstructorArrayCast>().argument(), parentPrecedence); |
| 164 | break; |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 165 | case Expression::Kind::kConstructorArray: |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 166 | case Expression::Kind::kConstructorCompound: |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 167 | case Expression::Kind::kConstructorMatrixResize: |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 168 | case Expression::Kind::kConstructorSplat: |
John Stiles | d47330f | 2021-04-08 23:25:52 -0400 | [diff] [blame] | 169 | case Expression::Kind::kConstructorStruct: |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 170 | this->writeAnyConstructor(expr.asAnyConstructor(), parentPrecedence); |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 171 | break; |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 172 | case Expression::Kind::kConstructorScalarCast: |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 173 | case Expression::Kind::kConstructorCompoundCast: |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 174 | this->writeCastConstructor(expr.asAnyConstructor(), parentPrecedence); |
| 175 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 176 | case Expression::Kind::kFieldAccess: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 177 | this->writeFieldAccess(expr.as<FieldAccess>()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 178 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 179 | case Expression::Kind::kFunctionCall: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 180 | this->writeFunctionCall(expr.as<FunctionCall>()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 181 | break; |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 182 | case Expression::Kind::kLiteral: |
| 183 | this->writeLiteral(expr.as<Literal>()); |
| 184 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 185 | case Expression::Kind::kPrefix: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 186 | this->writePrefixExpression(expr.as<PrefixExpression>(), parentPrecedence); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 187 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 188 | case Expression::Kind::kPostfix: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 189 | this->writePostfixExpression(expr.as<PostfixExpression>(), parentPrecedence); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 190 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 191 | case Expression::Kind::kSetting: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 192 | this->writeSetting(expr.as<Setting>()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 193 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 194 | case Expression::Kind::kSwizzle: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 195 | this->writeSwizzle(expr.as<Swizzle>()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 196 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 197 | case Expression::Kind::kVariableReference: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 198 | this->writeVariableReference(expr.as<VariableReference>()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 199 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 200 | case Expression::Kind::kTernary: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 201 | this->writeTernaryExpression(expr.as<TernaryExpression>(), parentPrecedence); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 202 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 203 | case Expression::Kind::kIndex: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 204 | this->writeIndexExpression(expr.as<IndexExpression>()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 205 | break; |
| 206 | default: |
John Stiles | eada7bc | 2021-02-02 16:29:32 -0500 | [diff] [blame] | 207 | SkDEBUGFAILF("unsupported expression: %s", expr.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 208 | break; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 212 | static bool is_abs(Expression& expr) { |
John Stiles | a5569ac | 2021-05-18 17:06:33 -0400 | [diff] [blame] | 213 | return expr.is<FunctionCall>() && |
| 214 | expr.as<FunctionCall>().function().intrinsicKind() == k_abs_IntrinsicKind; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 217 | // turns min(abs(x), y) into ((tmpVar1 = abs(x)) < (tmpVar2 = y) ? tmpVar1 : tmpVar2) to avoid a |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 218 | // Tegra3 compiler bug. |
| 219 | void GLSLCodeGenerator::writeMinAbsHack(Expression& absExpr, Expression& otherExpr) { |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 220 | SkASSERT(!this->caps().canUseMinAndAbsTogether()); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 221 | String tmpVar1 = "minAbsHackVar" + to_string(fVarCount++); |
| 222 | String tmpVar2 = "minAbsHackVar" + to_string(fVarCount++); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 223 | this->fFunctionHeader += String(" ") + this->getTypePrecision(absExpr.type()) + |
| 224 | this->getTypeName(absExpr.type()) + " " + tmpVar1 + ";\n"; |
| 225 | this->fFunctionHeader += String(" ") + this->getTypePrecision(otherExpr.type()) + |
| 226 | this->getTypeName(otherExpr.type()) + " " + tmpVar2 + ";\n"; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 227 | this->write("((" + tmpVar1 + " = "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 228 | this->writeExpression(absExpr, Precedence::kTopLevel); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 229 | this->write(") < (" + tmpVar2 + " = "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 230 | this->writeExpression(otherExpr, Precedence::kAssignment); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 231 | this->write(") ? " + tmpVar1 + " : " + tmpVar2 + ")"); |
| 232 | } |
| 233 | |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 234 | void GLSLCodeGenerator::writeInverseSqrtHack(const Expression& x) { |
| 235 | this->write("(1.0 / sqrt("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 236 | this->writeExpression(x, Precedence::kTopLevel); |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 237 | this->write("))"); |
| 238 | } |
| 239 | |
| 240 | void GLSLCodeGenerator::writeDeterminantHack(const Expression& mat) { |
| 241 | String name; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 242 | const Type& type = mat.type(); |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 243 | if (type == *fContext.fTypes.fFloat2x2 || type == *fContext.fTypes.fHalf2x2) { |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 244 | name = "_determinant2"; |
| 245 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 246 | fWrittenIntrinsics.insert(name); |
| 247 | fExtraFunctions.writeText(( |
| 248 | "float " + name + "(mat2 m) {" |
| 249 | " return m[0][0] * m[1][1] - m[0][1] * m[1][0];" |
| 250 | "}" |
| 251 | ).c_str()); |
| 252 | } |
| 253 | } |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 254 | else if (type == *fContext.fTypes.fFloat3x3 || type == *fContext.fTypes.fHalf3x3) { |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 255 | name = "_determinant3"; |
| 256 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 257 | fWrittenIntrinsics.insert(name); |
| 258 | fExtraFunctions.writeText(( |
| 259 | "float " + name + "(mat3 m) {" |
| 260 | " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];" |
| 261 | " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];" |
| 262 | " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];" |
| 263 | " float b01 = a22 * a11 - a12 * a21;" |
| 264 | " float b11 = -a22 * a10 + a12 * a20;" |
| 265 | " float b21 = a21 * a10 - a11 * a20;" |
| 266 | " return a00 * b01 + a01 * b11 + a02 * b21;" |
| 267 | "}" |
| 268 | ).c_str()); |
| 269 | } |
| 270 | } |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 271 | else if (type == *fContext.fTypes.fFloat4x4 || type == *fContext.fTypes.fHalf4x4) { |
John Stiles | 6f3015a | 2020-10-08 14:55:36 -0400 | [diff] [blame] | 272 | name = "_determinant4"; |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 273 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 274 | fWrittenIntrinsics.insert(name); |
| 275 | fExtraFunctions.writeText(( |
| 276 | "mat4 " + name + "(mat4 m) {" |
| 277 | " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3];" |
| 278 | " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3];" |
| 279 | " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2], a23 = m[2][3];" |
| 280 | " float a30 = m[3][0], a31 = m[3][1], a32 = m[3][2], a33 = m[3][3];" |
| 281 | " float b00 = a00 * a11 - a01 * a10;" |
| 282 | " float b01 = a00 * a12 - a02 * a10;" |
| 283 | " float b02 = a00 * a13 - a03 * a10;" |
| 284 | " float b03 = a01 * a12 - a02 * a11;" |
| 285 | " float b04 = a01 * a13 - a03 * a11;" |
| 286 | " float b05 = a02 * a13 - a03 * a12;" |
| 287 | " float b06 = a20 * a31 - a21 * a30;" |
| 288 | " float b07 = a20 * a32 - a22 * a30;" |
| 289 | " float b08 = a20 * a33 - a23 * a30;" |
| 290 | " float b09 = a21 * a32 - a22 * a31;" |
| 291 | " float b10 = a21 * a33 - a23 * a31;" |
| 292 | " float b11 = a22 * a33 - a23 * a32;" |
| 293 | " return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;" |
| 294 | "}" |
| 295 | ).c_str()); |
| 296 | } |
| 297 | } |
| 298 | else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 299 | SkASSERT(false); |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 300 | } |
| 301 | this->write(name + "("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 302 | this->writeExpression(mat, Precedence::kTopLevel); |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 303 | this->write(")"); |
| 304 | } |
| 305 | |
| 306 | void GLSLCodeGenerator::writeInverseHack(const Expression& mat) { |
| 307 | String name; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 308 | const Type& type = mat.type(); |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 309 | if (type == *fContext.fTypes.fFloat2x2 || type == *fContext.fTypes.fHalf2x2) { |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 310 | name = "_inverse2"; |
| 311 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 312 | fWrittenIntrinsics.insert(name); |
| 313 | fExtraFunctions.writeText(( |
| 314 | "mat2 " + name + "(mat2 m) {" |
| 315 | " return mat2(m[1][1], -m[0][1], -m[1][0], m[0][0]) / " |
| 316 | "(m[0][0] * m[1][1] - m[0][1] * m[1][0]);" |
| 317 | "}" |
| 318 | ).c_str()); |
| 319 | } |
| 320 | } |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 321 | else if (type == *fContext.fTypes.fFloat3x3 || type == *fContext.fTypes.fHalf3x3) { |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 322 | name = "_inverse3"; |
| 323 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 324 | fWrittenIntrinsics.insert(name); |
| 325 | fExtraFunctions.writeText(( |
| 326 | "mat3 " + name + "(mat3 m) {" |
| 327 | " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];" |
| 328 | " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];" |
| 329 | " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];" |
| 330 | " float b01 = a22 * a11 - a12 * a21;" |
| 331 | " float b11 = -a22 * a10 + a12 * a20;" |
| 332 | " float b21 = a21 * a10 - a11 * a20;" |
| 333 | " float det = a00 * b01 + a01 * b11 + a02 * b21;" |
| 334 | " return mat3(b01, (-a22 * a01 + a02 * a21), (a12 * a01 - a02 * a11)," |
| 335 | " b11, (a22 * a00 - a02 * a20), (-a12 * a00 + a02 * a10)," |
| 336 | " b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10)) / det;" |
| 337 | "}" |
| 338 | ).c_str()); |
| 339 | } |
| 340 | } |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 341 | else if (type == *fContext.fTypes.fFloat4x4 || type == *fContext.fTypes.fHalf4x4) { |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 342 | name = "_inverse4"; |
| 343 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 344 | fWrittenIntrinsics.insert(name); |
| 345 | fExtraFunctions.writeText(( |
| 346 | "mat4 " + name + "(mat4 m) {" |
| 347 | " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3];" |
| 348 | " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3];" |
| 349 | " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2], a23 = m[2][3];" |
| 350 | " float a30 = m[3][0], a31 = m[3][1], a32 = m[3][2], a33 = m[3][3];" |
| 351 | " float b00 = a00 * a11 - a01 * a10;" |
| 352 | " float b01 = a00 * a12 - a02 * a10;" |
| 353 | " float b02 = a00 * a13 - a03 * a10;" |
| 354 | " float b03 = a01 * a12 - a02 * a11;" |
| 355 | " float b04 = a01 * a13 - a03 * a11;" |
| 356 | " float b05 = a02 * a13 - a03 * a12;" |
| 357 | " float b06 = a20 * a31 - a21 * a30;" |
| 358 | " float b07 = a20 * a32 - a22 * a30;" |
| 359 | " float b08 = a20 * a33 - a23 * a30;" |
| 360 | " float b09 = a21 * a32 - a22 * a31;" |
| 361 | " float b10 = a21 * a33 - a23 * a31;" |
| 362 | " float b11 = a22 * a33 - a23 * a32;" |
| 363 | " float det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - " |
| 364 | " b04 * b07 + b05 * b06;" |
| 365 | " return mat4(" |
| 366 | " a11 * b11 - a12 * b10 + a13 * b09," |
| 367 | " a02 * b10 - a01 * b11 - a03 * b09," |
| 368 | " a31 * b05 - a32 * b04 + a33 * b03," |
| 369 | " a22 * b04 - a21 * b05 - a23 * b03," |
| 370 | " a12 * b08 - a10 * b11 - a13 * b07," |
| 371 | " a00 * b11 - a02 * b08 + a03 * b07," |
| 372 | " a32 * b02 - a30 * b05 - a33 * b01," |
| 373 | " a20 * b05 - a22 * b02 + a23 * b01," |
| 374 | " a10 * b10 - a11 * b08 + a13 * b06," |
| 375 | " a01 * b08 - a00 * b10 - a03 * b06," |
| 376 | " a30 * b04 - a31 * b02 + a33 * b00," |
| 377 | " a21 * b02 - a20 * b04 - a23 * b00," |
| 378 | " a11 * b07 - a10 * b09 - a12 * b06," |
| 379 | " a00 * b09 - a01 * b07 + a02 * b06," |
| 380 | " a31 * b01 - a30 * b03 - a32 * b00," |
| 381 | " a20 * b03 - a21 * b01 + a22 * b00) / det;" |
| 382 | "}" |
| 383 | ).c_str()); |
| 384 | } |
| 385 | } |
| 386 | else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 387 | SkASSERT(false); |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 388 | } |
| 389 | this->write(name + "("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 390 | this->writeExpression(mat, Precedence::kTopLevel); |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 391 | this->write(")"); |
| 392 | } |
| 393 | |
| 394 | void GLSLCodeGenerator::writeTransposeHack(const Expression& mat) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 395 | const Type& type = mat.type(); |
| 396 | String name = "transpose" + to_string(type.columns()) + to_string(type.rows()); |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 397 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 398 | fWrittenIntrinsics.insert(name); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 399 | String typeName = this->getTypeName(type); |
| 400 | const Type& base = type.componentType(); |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 401 | String transposed = this->getTypeName(base.toCompound(fContext, |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 402 | type.rows(), |
| 403 | type.columns())); |
| 404 | fExtraFunctions.writeText((transposed + " " + name + "(" + typeName + " m) {\nreturn " + |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 405 | transposed + "(").c_str()); |
| 406 | const char* separator = ""; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 407 | for (int row = 0; row < type.rows(); ++row) { |
| 408 | for (int column = 0; column < type.columns(); ++column) { |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 409 | fExtraFunctions.writeText(separator); |
| 410 | fExtraFunctions.writeText(("m[" + to_string(column) + "][" + to_string(row) + |
| 411 | "]").c_str()); |
| 412 | separator = ", "; |
| 413 | } |
| 414 | } |
| 415 | fExtraFunctions.writeText("); }"); |
| 416 | } |
| 417 | this->write(name + "("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 418 | this->writeExpression(mat, Precedence::kTopLevel); |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 419 | this->write(")"); |
| 420 | } |
| 421 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 422 | void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 423 | const FunctionDeclaration& function = c.function(); |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 424 | const ExpressionArray& arguments = c.arguments(); |
Brian Osman | 8a83ca4 | 2018-02-12 14:32:17 -0500 | [diff] [blame] | 425 | bool isTextureFunctionWithBias = false; |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 426 | bool nameWritten = false; |
John Stiles | f96cb71 | 2021-05-05 22:17:04 -0400 | [diff] [blame] | 427 | switch (c.function().intrinsicKind()) { |
| 428 | case k_abs_IntrinsicKind: { |
| 429 | if (!this->caps().emulateAbsIntFunction()) |
| 430 | break; |
| 431 | SkASSERT(arguments.size() == 1); |
| 432 | if (arguments[0]->type() != *fContext.fTypes.fInt) { |
Adrienne Walker | 92b161f | 2018-08-22 10:41:52 -0700 | [diff] [blame] | 433 | break; |
| 434 | } |
John Stiles | f96cb71 | 2021-05-05 22:17:04 -0400 | [diff] [blame] | 435 | // abs(int) on Intel OSX is incorrect, so emulate it: |
| 436 | String name = "_absemulation"; |
| 437 | this->write(name); |
| 438 | nameWritten = true; |
| 439 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 440 | fWrittenIntrinsics.insert(name); |
| 441 | fExtraFunctions.writeText(( |
| 442 | "int " + name + "(int x) {\n" |
| 443 | " return x * sign(x);\n" |
| 444 | "}\n" |
| 445 | ).c_str()); |
| 446 | } |
| 447 | break; |
| 448 | } |
| 449 | case k_atan_IntrinsicKind: |
| 450 | if (this->caps().mustForceNegatedAtanParamToFloat() && |
| 451 | arguments.size() == 2 && |
| 452 | arguments[1]->kind() == Expression::Kind::kPrefix) { |
| 453 | const PrefixExpression& p = (PrefixExpression&) *arguments[1]; |
| 454 | if (p.getOperator().kind() == Token::Kind::TK_MINUS) { |
| 455 | this->write("atan("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 456 | this->writeExpression(*arguments[0], Precedence::kSequence); |
John Stiles | f96cb71 | 2021-05-05 22:17:04 -0400 | [diff] [blame] | 457 | this->write(", -1.0 * "); |
| 458 | this->writeExpression(*p.operand(), Precedence::kMultiplicative); |
| 459 | this->write(")"); |
Chris Dalton | a708618 | 2018-11-16 09:33:43 -0500 | [diff] [blame] | 460 | return; |
| 461 | } |
John Stiles | f96cb71 | 2021-05-05 22:17:04 -0400 | [diff] [blame] | 462 | } |
| 463 | break; |
Chris Dalton | 155c33c | 2021-06-07 18:51:42 -0600 | [diff] [blame] | 464 | case k_ldexp_IntrinsicKind: |
| 465 | if (this->caps().mustForceNegatedLdexpParamToMultiply() && |
| 466 | arguments.size() == 2 && |
| 467 | arguments[1]->is<PrefixExpression>()) { |
| 468 | const PrefixExpression& p = arguments[1]->as<PrefixExpression>(); |
| 469 | if (p.getOperator().kind() == Token::Kind::TK_MINUS) { |
| 470 | this->write("ldexp("); |
| 471 | this->writeExpression(*arguments[0], Precedence::kSequence); |
| 472 | this->write(", "); |
| 473 | this->writeExpression(*p.operand(), Precedence::kMultiplicative); |
| 474 | this->write(" * -1)"); |
| 475 | return; |
| 476 | } |
| 477 | } |
| 478 | break; |
John Stiles | f96cb71 | 2021-05-05 22:17:04 -0400 | [diff] [blame] | 479 | case k_dFdy_IntrinsicKind: |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 480 | // Flipping Y also negates the Y derivatives. |
| 481 | this->write(SKSL_RTFLIP_NAME ".y * dFdy"); |
| 482 | nameWritten = true; |
John Stiles | f96cb71 | 2021-05-05 22:17:04 -0400 | [diff] [blame] | 483 | [[fallthrough]]; |
| 484 | case k_dFdx_IntrinsicKind: |
| 485 | case k_fwidth_IntrinsicKind: |
| 486 | if (!fFoundDerivatives && |
| 487 | this->caps().shaderDerivativeExtensionString()) { |
| 488 | this->writeExtension(this->caps().shaderDerivativeExtensionString()); |
| 489 | fFoundDerivatives = true; |
| 490 | } |
| 491 | break; |
| 492 | case k_determinant_IntrinsicKind: |
| 493 | if (!this->caps().builtinDeterminantSupport()) { |
| 494 | SkASSERT(arguments.size() == 1); |
| 495 | this->writeDeterminantHack(*arguments[0]); |
| 496 | return; |
| 497 | } |
| 498 | break; |
| 499 | case k_fma_IntrinsicKind: |
| 500 | if (!this->caps().builtinFMASupport()) { |
| 501 | SkASSERT(arguments.size() == 3); |
| 502 | this->write("(("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 503 | this->writeExpression(*arguments[0], Precedence::kSequence); |
John Stiles | f96cb71 | 2021-05-05 22:17:04 -0400 | [diff] [blame] | 504 | this->write(") * ("); |
| 505 | this->writeExpression(*arguments[1], Precedence::kSequence); |
| 506 | this->write(") + ("); |
| 507 | this->writeExpression(*arguments[2], Precedence::kSequence); |
Adrienne Walker | 2f4c09b | 2018-08-22 16:04:57 -0700 | [diff] [blame] | 508 | this->write("))"); |
| 509 | return; |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 510 | } |
John Stiles | f96cb71 | 2021-05-05 22:17:04 -0400 | [diff] [blame] | 511 | break; |
| 512 | case k_fract_IntrinsicKind: |
| 513 | if (!this->caps().canUseFractForNegativeValues()) { |
| 514 | SkASSERT(arguments.size() == 1); |
| 515 | this->write("(0.5 - sign("); |
| 516 | this->writeExpression(*arguments[0], Precedence::kSequence); |
| 517 | this->write(") * (0.5 - fract(abs("); |
| 518 | this->writeExpression(*arguments[0], Precedence::kSequence); |
| 519 | this->write("))))"); |
| 520 | return; |
| 521 | } |
| 522 | break; |
| 523 | case k_inverse_IntrinsicKind: |
| 524 | if (this->caps().generation() < k140_GrGLSLGeneration) { |
| 525 | SkASSERT(arguments.size() == 1); |
| 526 | this->writeInverseHack(*arguments[0]); |
| 527 | return; |
| 528 | } |
| 529 | break; |
| 530 | case k_inversesqrt_IntrinsicKind: |
| 531 | if (this->caps().generation() < k130_GrGLSLGeneration) { |
| 532 | SkASSERT(arguments.size() == 1); |
| 533 | this->writeInverseSqrtHack(*arguments[0]); |
| 534 | return; |
| 535 | } |
| 536 | break; |
| 537 | case k_min_IntrinsicKind: |
| 538 | if (!this->caps().canUseMinAndAbsTogether()) { |
| 539 | SkASSERT(arguments.size() == 2); |
| 540 | if (is_abs(*arguments[0])) { |
| 541 | this->writeMinAbsHack(*arguments[0], *arguments[1]); |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 542 | return; |
| 543 | } |
John Stiles | f96cb71 | 2021-05-05 22:17:04 -0400 | [diff] [blame] | 544 | if (is_abs(*arguments[1])) { |
| 545 | // note that this violates the GLSL left-to-right evaluation semantics. |
| 546 | // I doubt it will ever end up mattering, but it's worth calling out. |
| 547 | this->writeMinAbsHack(*arguments[1], *arguments[0]); |
| 548 | return; |
| 549 | } |
| 550 | } |
| 551 | break; |
| 552 | case k_pow_IntrinsicKind: |
| 553 | if (!this->caps().removePowWithConstantExponent()) { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 554 | break; |
John Stiles | f96cb71 | 2021-05-05 22:17:04 -0400 | [diff] [blame] | 555 | } |
| 556 | // pow(x, y) on some NVIDIA drivers causes crashes if y is a |
| 557 | // constant. It's hard to tell what constitutes "constant" here |
| 558 | // so just replace in all cases. |
| 559 | |
| 560 | // Change pow(x, y) into exp2(y * log2(x)) |
| 561 | this->write("exp2("); |
| 562 | this->writeExpression(*arguments[1], Precedence::kMultiplicative); |
| 563 | this->write(" * log2("); |
| 564 | this->writeExpression(*arguments[0], Precedence::kSequence); |
| 565 | this->write("))"); |
| 566 | return; |
| 567 | case k_saturate_IntrinsicKind: |
| 568 | SkASSERT(arguments.size() == 1); |
| 569 | this->write("clamp("); |
| 570 | this->writeExpression(*arguments[0], Precedence::kSequence); |
| 571 | this->write(", 0.0, 1.0)"); |
| 572 | return; |
| 573 | case k_sample_IntrinsicKind: { |
| 574 | const char* dim = ""; |
| 575 | bool proj = false; |
| 576 | const Type& arg0Type = arguments[0]->type(); |
| 577 | const Type& arg1Type = arguments[1]->type(); |
| 578 | switch (arg0Type.dimensions()) { |
| 579 | case SpvDim1D: |
| 580 | dim = "1D"; |
| 581 | isTextureFunctionWithBias = true; |
| 582 | if (arg1Type == *fContext.fTypes.fFloat) { |
| 583 | proj = false; |
| 584 | } else { |
| 585 | SkASSERT(arg1Type == *fContext.fTypes.fFloat2); |
| 586 | proj = true; |
| 587 | } |
| 588 | break; |
| 589 | case SpvDim2D: |
| 590 | dim = "2D"; |
| 591 | if (arg0Type != *fContext.fTypes.fSamplerExternalOES) { |
| 592 | isTextureFunctionWithBias = true; |
| 593 | } |
| 594 | if (arg1Type == *fContext.fTypes.fFloat2) { |
| 595 | proj = false; |
| 596 | } else { |
| 597 | SkASSERT(arg1Type == *fContext.fTypes.fFloat3); |
| 598 | proj = true; |
| 599 | } |
| 600 | break; |
| 601 | case SpvDim3D: |
| 602 | dim = "3D"; |
| 603 | isTextureFunctionWithBias = true; |
| 604 | if (arg1Type == *fContext.fTypes.fFloat3) { |
| 605 | proj = false; |
| 606 | } else { |
| 607 | SkASSERT(arg1Type == *fContext.fTypes.fFloat4); |
| 608 | proj = true; |
| 609 | } |
| 610 | break; |
| 611 | case SpvDimCube: |
| 612 | dim = "Cube"; |
| 613 | isTextureFunctionWithBias = true; |
| 614 | proj = false; |
| 615 | break; |
| 616 | case SpvDimRect: |
| 617 | dim = "2DRect"; |
| 618 | proj = false; |
| 619 | break; |
| 620 | case SpvDimBuffer: |
| 621 | SkASSERT(false); // doesn't exist |
| 622 | dim = "Buffer"; |
| 623 | proj = false; |
| 624 | break; |
| 625 | case SpvDimSubpassData: |
| 626 | SkASSERT(false); // doesn't exist |
| 627 | dim = "SubpassData"; |
| 628 | proj = false; |
| 629 | break; |
| 630 | } |
| 631 | if (!fTextureFunctionOverride.empty()) { |
| 632 | this->write(fTextureFunctionOverride.c_str()); |
| 633 | } else { |
| 634 | this->write("texture"); |
| 635 | if (this->caps().generation() < k130_GrGLSLGeneration) { |
| 636 | this->write(dim); |
| 637 | } |
| 638 | if (proj) { |
| 639 | this->write("Proj"); |
| 640 | } |
| 641 | } |
| 642 | nameWritten = true; |
| 643 | break; |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 644 | } |
John Stiles | f96cb71 | 2021-05-05 22:17:04 -0400 | [diff] [blame] | 645 | case k_transpose_IntrinsicKind: |
| 646 | if (this->caps().generation() < k130_GrGLSLGeneration) { |
| 647 | SkASSERT(arguments.size() == 1); |
| 648 | this->writeTransposeHack(*arguments[0]); |
| 649 | return; |
| 650 | } |
| 651 | break; |
| 652 | default: |
| 653 | break; |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 654 | } |
John Stiles | f96cb71 | 2021-05-05 22:17:04 -0400 | [diff] [blame] | 655 | |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 656 | if (!nameWritten) { |
John Stiles | ece1d79 | 2021-03-22 11:43:11 -0400 | [diff] [blame] | 657 | this->write(function.mangledName()); |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 658 | } |
| 659 | this->write("("); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 660 | const char* separator = ""; |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 661 | for (const auto& arg : arguments) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 662 | this->write(separator); |
| 663 | separator = ", "; |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 664 | this->writeExpression(*arg, Precedence::kSequence); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 665 | } |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 666 | if (fProgram.fConfig->fSettings.fSharpenTextures && isTextureFunctionWithBias) { |
Brian Osman | 8a83ca4 | 2018-02-12 14:32:17 -0500 | [diff] [blame] | 667 | this->write(", -0.5"); |
| 668 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 669 | this->write(")"); |
| 670 | } |
| 671 | |
John Stiles | 3c74a53 | 2021-05-18 17:31:48 -0400 | [diff] [blame] | 672 | void GLSLCodeGenerator::writeConstructorDiagonalMatrix(const ConstructorDiagonalMatrix& c, |
| 673 | Precedence parentPrecedence) { |
| 674 | if (c.type().columns() == 4 && c.type().rows() == 2) { |
| 675 | // Due to a longstanding bug in glslang and Mesa, several GPU drivers generate diagonal 4x2 |
| 676 | // matrices incorrectly. (skia:12003, https://github.com/KhronosGroup/glslang/pull/2646) |
| 677 | // We can work around this issue by multiplying a scalar by the identity matrix. |
| 678 | // In practice, this doesn't come up naturally in real code and we don't know every affected |
| 679 | // driver, so we just apply this workaround everywhere. |
| 680 | this->write("("); |
| 681 | this->writeType(c.type()); |
| 682 | this->write("(1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0) * "); |
| 683 | this->writeExpression(*c.argument(), Precedence::kMultiplicative); |
| 684 | this->write(")"); |
| 685 | return; |
| 686 | } |
| 687 | this->writeAnyConstructor(c, parentPrecedence); |
| 688 | } |
| 689 | |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 690 | void GLSLCodeGenerator::writeCastConstructor(const AnyConstructor& c, Precedence parentPrecedence) { |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 691 | const auto arguments = c.argumentSpan(); |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 692 | SkASSERT(arguments.size() == 1); |
| 693 | |
| 694 | const Expression& argument = *arguments.front(); |
| 695 | if ((this->getTypeName(c.type()) == this->getTypeName(argument.type()) || |
| 696 | (argument.type() == *fContext.fTypes.fFloatLiteral))) { |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 697 | // In cases like half(float), they're different types as far as SkSL is concerned but |
| 698 | // the same type as far as GLSL is concerned. We avoid a redundant float(float) by just |
| 699 | // writing out the inner expression here. |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 700 | this->writeExpression(argument, parentPrecedence); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 701 | return; |
| 702 | } |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 703 | |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 704 | // This cast should be emitted as-is. |
John Stiles | d8eb875 | 2021-04-01 11:49:10 -0400 | [diff] [blame] | 705 | return this->writeAnyConstructor(c, parentPrecedence); |
John Stiles | 626b62e | 2021-03-31 22:06:07 -0400 | [diff] [blame] | 706 | } |
| 707 | |
John Stiles | d8eb875 | 2021-04-01 11:49:10 -0400 | [diff] [blame] | 708 | void GLSLCodeGenerator::writeAnyConstructor(const AnyConstructor& c, Precedence parentPrecedence) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 709 | this->writeType(c.type()); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 710 | this->write("("); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 711 | const char* separator = ""; |
John Stiles | d8eb875 | 2021-04-01 11:49:10 -0400 | [diff] [blame] | 712 | for (const auto& arg : c.argumentSpan()) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 713 | this->write(separator); |
| 714 | separator = ", "; |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 715 | this->writeExpression(*arg, Precedence::kSequence); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 716 | } |
| 717 | this->write(")"); |
| 718 | } |
| 719 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 720 | void GLSLCodeGenerator::writeFragCoord() { |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 721 | if (!this->caps().canUseFragCoord()) { |
Brian Salomon | dba65f9 | 2018-01-22 08:43:38 -0500 | [diff] [blame] | 722 | if (!fSetupFragCoordWorkaround) { |
| 723 | const char* precision = usesPrecisionModifiers() ? "highp " : ""; |
| 724 | fFunctionHeader += precision; |
| 725 | fFunctionHeader += " float sk_FragCoord_InvW = 1. / sk_FragCoord_Workaround.w;\n"; |
| 726 | fFunctionHeader += precision; |
| 727 | fFunctionHeader += " vec4 sk_FragCoord_Resolved = " |
| 728 | "vec4(sk_FragCoord_Workaround.xyz * sk_FragCoord_InvW, sk_FragCoord_InvW);\n"; |
| 729 | // Ensure that we get exact .5 values for x and y. |
| 730 | fFunctionHeader += " sk_FragCoord_Resolved.xy = floor(sk_FragCoord_Resolved.xy) + " |
| 731 | "vec2(.5);\n"; |
| 732 | fSetupFragCoordWorkaround = true; |
| 733 | } |
| 734 | this->write("sk_FragCoord_Resolved"); |
Brian Osman | cd3261a | 2018-01-16 13:52:29 +0000 | [diff] [blame] | 735 | return; |
| 736 | } |
| 737 | |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 738 | if (!fSetupFragPosition) { |
| 739 | fFunctionHeader += usesPrecisionModifiers() ? "highp " : ""; |
| 740 | fFunctionHeader += " vec4 sk_FragCoord = vec4(" |
| 741 | "gl_FragCoord.x, " |
| 742 | SKSL_RTFLIP_NAME ".x + " SKSL_RTFLIP_NAME ".y * gl_FragCoord.y, " |
| 743 | "gl_FragCoord.z, " |
| 744 | "gl_FragCoord.w);\n"; |
| 745 | fSetupFragPosition = true; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 746 | } |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 747 | this->write("sk_FragCoord"); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 748 | } |
| 749 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 750 | void GLSLCodeGenerator::writeVariableReference(const VariableReference& ref) { |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 751 | switch (ref.variable()->modifiers().fLayout.fBuiltin) { |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 752 | case SK_FRAGCOLOR_BUILTIN: |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 753 | if (this->caps().mustDeclareFragmentShaderOutput()) { |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 754 | this->write("sk_FragColor"); |
| 755 | } else { |
| 756 | this->write("gl_FragColor"); |
| 757 | } |
| 758 | break; |
| 759 | case SK_FRAGCOORD_BUILTIN: |
| 760 | this->writeFragCoord(); |
| 761 | break; |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 762 | case SK_CLOCKWISE_BUILTIN: |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 763 | if (!fSetupClockwise) { |
| 764 | fFunctionHeader += |
| 765 | " bool sk_Clockwise = gl_FrontFacing;\n" |
| 766 | " if (" SKSL_RTFLIP_NAME ".y < 0.0) {\n" |
| 767 | " sk_Clockwise = !sk_Clockwise;\n" |
| 768 | " }\n"; |
| 769 | fSetupClockwise = true; |
| 770 | } |
| 771 | this->write("sk_Clockwise"); |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 772 | break; |
Ethan Nicholas | a51740c | 2017-02-07 14:53:32 -0500 | [diff] [blame] | 773 | case SK_VERTEXID_BUILTIN: |
| 774 | this->write("gl_VertexID"); |
| 775 | break; |
Chris Dalton | 8580d51 | 2017-10-14 22:12:33 -0600 | [diff] [blame] | 776 | case SK_INSTANCEID_BUILTIN: |
| 777 | this->write("gl_InstanceID"); |
| 778 | break; |
Ethan Nicholas | eab2baa | 2018-04-13 15:16:27 -0400 | [diff] [blame] | 779 | case SK_LASTFRAGCOLOR_BUILTIN: |
Brian Osman | a81e7e2 | 2021-09-15 10:28:27 -0400 | [diff] [blame^] | 780 | if (this->caps().fbFetchSupport()) { |
| 781 | this->write(this->caps().fbFetchColorName()); |
| 782 | } else { |
| 783 | fContext.fErrors->error(ref.fOffset, |
| 784 | "sk_LastFragColor requires framebuffer fetch support"); |
| 785 | } |
Ethan Nicholas | eab2baa | 2018-04-13 15:16:27 -0400 | [diff] [blame] | 786 | break; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 787 | default: |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 788 | this->write(ref.variable()->name()); |
Ethan Nicholas | b13f369 | 2021-09-10 16:49:42 -0400 | [diff] [blame] | 789 | break; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 790 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | void GLSLCodeGenerator::writeIndexExpression(const IndexExpression& expr) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 794 | this->writeExpression(*expr.base(), Precedence::kPostfix); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 795 | this->write("["); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 796 | this->writeExpression(*expr.index(), Precedence::kTopLevel); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 797 | this->write("]"); |
| 798 | } |
| 799 | |
Brian Osman | cd3261a | 2018-01-16 13:52:29 +0000 | [diff] [blame] | 800 | bool is_sk_position(const FieldAccess& f) { |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 801 | return "sk_Position" == f.base()->type().fields()[f.fieldIndex()].fName; |
Brian Osman | cd3261a | 2018-01-16 13:52:29 +0000 | [diff] [blame] | 802 | } |
| 803 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 804 | void GLSLCodeGenerator::writeFieldAccess(const FieldAccess& f) { |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 805 | if (f.ownerKind() == FieldAccess::OwnerKind::kDefault) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 806 | this->writeExpression(*f.base(), Precedence::kPostfix); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 807 | this->write("."); |
| 808 | } |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 809 | const Type& baseType = f.base()->type(); |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 810 | skstd::string_view name = baseType.fields()[f.fieldIndex()].fName; |
John Stiles | ba38588 | 2020-09-25 14:49:37 -0400 | [diff] [blame] | 811 | if (name == "sk_Position") { |
| 812 | this->write("gl_Position"); |
| 813 | } else if (name == "sk_PointSize") { |
| 814 | this->write("gl_PointSize"); |
| 815 | } else { |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 816 | this->write(baseType.fields()[f.fieldIndex()].fName); |
Ethan Nicholas | 67d6460 | 2017-02-09 10:15:25 -0500 | [diff] [blame] | 817 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 818 | } |
| 819 | |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 820 | void GLSLCodeGenerator::writeSwizzle(const Swizzle& swizzle) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 821 | this->writeExpression(*swizzle.base(), Precedence::kPostfix); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 822 | this->write("."); |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 823 | for (int c : swizzle.components()) { |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 824 | SkASSERT(c >= 0 && c <= 3); |
| 825 | this->write(&("x\0y\0z\0w\0"[c * 2])); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | |
John Stiles | f061c59 | 2021-05-21 23:34:42 -0400 | [diff] [blame] | 829 | void GLSLCodeGenerator::writeMatrixComparisonWorkaround(const BinaryExpression& b) { |
| 830 | const Expression& left = *b.left(); |
| 831 | const Expression& right = *b.right(); |
| 832 | Operator op = b.getOperator(); |
| 833 | |
| 834 | SkASSERT(op.kind() == Token::Kind::TK_EQEQ || op.kind() == Token::Kind::TK_NEQ); |
| 835 | SkASSERT(left.type().isMatrix()); |
| 836 | SkASSERT(right.type().isMatrix()); |
| 837 | |
| 838 | String tempMatrix1 = "_tempMatrix" + to_string(fVarCount++); |
| 839 | String tempMatrix2 = "_tempMatrix" + to_string(fVarCount++); |
| 840 | |
| 841 | this->fFunctionHeader += String(" ") + this->getTypePrecision(left.type()) + |
| 842 | this->getTypeName(left.type()) + " " + tempMatrix1 + ";\n " + |
| 843 | this->getTypePrecision(right.type()) + |
| 844 | this->getTypeName(right.type()) + " " + tempMatrix2 + ";\n"; |
| 845 | this->write("((" + tempMatrix1 + " = "); |
| 846 | this->writeExpression(left, Precedence::kAssignment); |
| 847 | this->write("), (" + tempMatrix2 + " = "); |
| 848 | this->writeExpression(right, Precedence::kAssignment); |
| 849 | this->write("), (" + tempMatrix1 + " "); |
| 850 | this->write(op.operatorName()); |
| 851 | this->write(" " + tempMatrix2 + "))"); |
| 852 | } |
| 853 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 854 | void GLSLCodeGenerator::writeBinaryExpression(const BinaryExpression& b, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 855 | Precedence parentPrecedence) { |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 856 | const Expression& left = *b.left(); |
| 857 | const Expression& right = *b.right(); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 858 | Operator op = b.getOperator(); |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 859 | if (this->caps().unfoldShortCircuitAsTernary() && |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 860 | (op.kind() == Token::Kind::TK_LOGICALAND || op.kind() == Token::Kind::TK_LOGICALOR)) { |
Adrienne Walker | c02165f | 2018-08-21 11:08:11 -0700 | [diff] [blame] | 861 | this->writeShortCircuitWorkaroundExpression(b, parentPrecedence); |
| 862 | return; |
| 863 | } |
| 864 | |
John Stiles | f061c59 | 2021-05-21 23:34:42 -0400 | [diff] [blame] | 865 | if (this->caps().rewriteMatrixComparisons() && |
| 866 | left.type().isMatrix() && right.type().isMatrix() && |
| 867 | (op.kind() == Token::Kind::TK_EQEQ || op.kind() == Token::Kind::TK_NEQ)) { |
| 868 | this->writeMatrixComparisonWorkaround(b); |
| 869 | return; |
| 870 | } |
| 871 | |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 872 | Precedence precedence = op.getBinaryPrecedence(); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 873 | if (precedence >= parentPrecedence) { |
| 874 | this->write("("); |
| 875 | } |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 876 | bool positionWorkaround = fProgram.fConfig->fKind == ProgramKind::kVertex && |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 877 | op.isAssignment() && |
| 878 | left.is<FieldAccess>() && |
| 879 | is_sk_position(left.as<FieldAccess>()) && |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 880 | !right.containsRTAdjust() && |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 881 | !this->caps().canUseFragCoord(); |
Brian Osman | cd3261a | 2018-01-16 13:52:29 +0000 | [diff] [blame] | 882 | if (positionWorkaround) { |
| 883 | this->write("sk_FragCoord_Workaround = ("); |
| 884 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 885 | this->writeExpression(left, precedence); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 886 | this->write(" "); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 887 | this->write(op.operatorName()); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 888 | this->write(" "); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 889 | this->writeExpression(right, precedence); |
Brian Osman | cd3261a | 2018-01-16 13:52:29 +0000 | [diff] [blame] | 890 | if (positionWorkaround) { |
| 891 | this->write(")"); |
| 892 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 893 | if (precedence >= parentPrecedence) { |
| 894 | this->write(")"); |
| 895 | } |
| 896 | } |
| 897 | |
Adrienne Walker | c02165f | 2018-08-21 11:08:11 -0700 | [diff] [blame] | 898 | void GLSLCodeGenerator::writeShortCircuitWorkaroundExpression(const BinaryExpression& b, |
| 899 | Precedence parentPrecedence) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 900 | if (Precedence::kTernary >= parentPrecedence) { |
Adrienne Walker | c02165f | 2018-08-21 11:08:11 -0700 | [diff] [blame] | 901 | this->write("("); |
| 902 | } |
| 903 | |
| 904 | // Transform: |
| 905 | // a && b => a ? b : false |
| 906 | // a || b => a ? true : b |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 907 | this->writeExpression(*b.left(), Precedence::kTernary); |
Adrienne Walker | c02165f | 2018-08-21 11:08:11 -0700 | [diff] [blame] | 908 | this->write(" ? "); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 909 | if (b.getOperator().kind() == Token::Kind::TK_LOGICALAND) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 910 | this->writeExpression(*b.right(), Precedence::kTernary); |
Adrienne Walker | c02165f | 2018-08-21 11:08:11 -0700 | [diff] [blame] | 911 | } else { |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 912 | Literal boolTrue(/*offset=*/-1, /*value=*/1, fContext.fTypes.fBool.get()); |
| 913 | this->writeLiteral(boolTrue); |
Adrienne Walker | c02165f | 2018-08-21 11:08:11 -0700 | [diff] [blame] | 914 | } |
| 915 | this->write(" : "); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 916 | if (b.getOperator().kind() == Token::Kind::TK_LOGICALAND) { |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 917 | Literal boolFalse(/*offset=*/-1, /*value=*/0, fContext.fTypes.fBool.get()); |
| 918 | this->writeLiteral(boolFalse); |
Adrienne Walker | c02165f | 2018-08-21 11:08:11 -0700 | [diff] [blame] | 919 | } else { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 920 | this->writeExpression(*b.right(), Precedence::kTernary); |
Adrienne Walker | c02165f | 2018-08-21 11:08:11 -0700 | [diff] [blame] | 921 | } |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 922 | if (Precedence::kTernary >= parentPrecedence) { |
Adrienne Walker | c02165f | 2018-08-21 11:08:11 -0700 | [diff] [blame] | 923 | this->write(")"); |
| 924 | } |
| 925 | } |
| 926 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 927 | void GLSLCodeGenerator::writeTernaryExpression(const TernaryExpression& t, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 928 | Precedence parentPrecedence) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 929 | if (Precedence::kTernary >= parentPrecedence) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 930 | this->write("("); |
| 931 | } |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 932 | this->writeExpression(*t.test(), Precedence::kTernary); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 933 | this->write(" ? "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 934 | this->writeExpression(*t.ifTrue(), Precedence::kTernary); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 935 | this->write(" : "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 936 | this->writeExpression(*t.ifFalse(), Precedence::kTernary); |
| 937 | if (Precedence::kTernary >= parentPrecedence) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 938 | this->write(")"); |
| 939 | } |
| 940 | } |
| 941 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 942 | void GLSLCodeGenerator::writePrefixExpression(const PrefixExpression& p, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 943 | Precedence parentPrecedence) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 944 | if (Precedence::kPrefix >= parentPrecedence) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 945 | this->write("("); |
| 946 | } |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 947 | this->write(p.getOperator().operatorName()); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 948 | this->writeExpression(*p.operand(), Precedence::kPrefix); |
| 949 | if (Precedence::kPrefix >= parentPrecedence) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 950 | this->write(")"); |
| 951 | } |
| 952 | } |
| 953 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 954 | void GLSLCodeGenerator::writePostfixExpression(const PostfixExpression& p, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 955 | Precedence parentPrecedence) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 956 | if (Precedence::kPostfix >= parentPrecedence) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 957 | this->write("("); |
| 958 | } |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 959 | this->writeExpression(*p.operand(), Precedence::kPostfix); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 960 | this->write(p.getOperator().operatorName()); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 961 | if (Precedence::kPostfix >= parentPrecedence) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 962 | this->write(")"); |
| 963 | } |
| 964 | } |
| 965 | |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 966 | void GLSLCodeGenerator::writeLiteral(const Literal& l) { |
| 967 | const Type& type = l.type(); |
| 968 | if (type.isFloat()) { |
| 969 | this->write(to_string(l.floatValue())); |
| 970 | return; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 971 | } |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 972 | if (type.isInteger()) { |
| 973 | if (type == *fContext.fTypes.fUInt) { |
| 974 | this->write(to_string(l.intValue() & 0xffffffff) + "u"); |
| 975 | } else if (type == *fContext.fTypes.fUShort) { |
| 976 | this->write(to_string(l.intValue() & 0xffff) + "u"); |
| 977 | } else { |
| 978 | this->write(to_string(l.intValue())); |
| 979 | } |
| 980 | return; |
| 981 | } |
| 982 | SkASSERT(type.isBoolean()); |
| 983 | this->write(l.boolValue() ? "true" : "false"); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 984 | } |
| 985 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 986 | void GLSLCodeGenerator::writeSetting(const Setting& s) { |
John Stiles | f57207b | 2021-02-02 17:50:34 -0500 | [diff] [blame] | 987 | SK_ABORT("internal error; setting was not folded to a constant during compilation\n"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 988 | } |
| 989 | |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 990 | void GLSLCodeGenerator::writeFunctionDeclaration(const FunctionDeclaration& f) { |
| 991 | this->writeTypePrecision(f.returnType()); |
| 992 | this->writeType(f.returnType()); |
John Stiles | ece1d79 | 2021-03-22 11:43:11 -0400 | [diff] [blame] | 993 | this->write(" " + f.mangledName() + "("); |
John Stiles | fffe384 | 2020-09-02 15:44:18 -0400 | [diff] [blame] | 994 | const char* separator = ""; |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 995 | for (const auto& param : f.parameters()) { |
Brian Osman | 716aeb9 | 2021-04-21 13:20:00 -0400 | [diff] [blame] | 996 | // This is a workaround for our test files. They use the runtime effect signature, so main |
| 997 | // takes a coords parameter. The IR generator tags those with a builtin ID (sk_FragCoord), |
| 998 | // and we omit them from the declaration here, so the function is valid GLSL. |
| 999 | if (f.isMain() && param->modifiers().fLayout.fBuiltin != -1) { |
| 1000 | continue; |
| 1001 | } |
John Stiles | fffe384 | 2020-09-02 15:44:18 -0400 | [diff] [blame] | 1002 | this->write(separator); |
| 1003 | separator = ", "; |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1004 | this->writeModifiers(param->modifiers(), false); |
John Stiles | fffe384 | 2020-09-02 15:44:18 -0400 | [diff] [blame] | 1005 | std::vector<int> sizes; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1006 | const Type* type = ¶m->type(); |
John Stiles | bb43b7e | 2020-12-03 17:36:32 -0500 | [diff] [blame] | 1007 | if (type->isArray()) { |
John Stiles | fffe384 | 2020-09-02 15:44:18 -0400 | [diff] [blame] | 1008 | sizes.push_back(type->columns()); |
| 1009 | type = &type->componentType(); |
| 1010 | } |
| 1011 | this->writeTypePrecision(*type); |
| 1012 | this->writeType(*type); |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 1013 | this->write(" " + param->name()); |
John Stiles | fffe384 | 2020-09-02 15:44:18 -0400 | [diff] [blame] | 1014 | for (int s : sizes) { |
Brian Osman | b42c383 | 2021-08-27 15:19:03 -0400 | [diff] [blame] | 1015 | this->write("[" + to_string(s) + "]"); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1016 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1017 | } |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1018 | this->write(")"); |
| 1019 | } |
| 1020 | |
| 1021 | void GLSLCodeGenerator::writeFunction(const FunctionDefinition& f) { |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 1022 | fSetupFragPosition = false; |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1023 | fSetupFragCoordWorkaround = false; |
| 1024 | |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1025 | this->writeFunctionDeclaration(f.declaration()); |
| 1026 | this->writeLine(" {"); |
John Stiles | fffe384 | 2020-09-02 15:44:18 -0400 | [diff] [blame] | 1027 | fIndentation++; |
| 1028 | |
John Stiles | 9568023 | 2021-04-22 15:05:20 -0400 | [diff] [blame] | 1029 | fFunctionHeader.clear(); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1030 | OutputStream* oldOut = fOut; |
| 1031 | StringStream buffer; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1032 | fOut = &buffer; |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 1033 | for (const std::unique_ptr<Statement>& stmt : f.body()->as<Block>().children()) { |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1034 | if (!stmt->isEmpty()) { |
| 1035 | this->writeStatement(*stmt); |
John Stiles | e8b5a73 | 2021-03-12 13:25:52 -0500 | [diff] [blame] | 1036 | this->finishLine(); |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1037 | } |
| 1038 | } |
John Stiles | fffe384 | 2020-09-02 15:44:18 -0400 | [diff] [blame] | 1039 | |
| 1040 | fIndentation--; |
| 1041 | this->writeLine("}"); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1042 | |
| 1043 | fOut = oldOut; |
| 1044 | this->write(fFunctionHeader); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1045 | this->write(buffer.str()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1048 | void GLSLCodeGenerator::writeFunctionPrototype(const FunctionPrototype& f) { |
| 1049 | this->writeFunctionDeclaration(f.declaration()); |
| 1050 | this->writeLine(";"); |
| 1051 | } |
| 1052 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1053 | void GLSLCodeGenerator::writeModifiers(const Modifiers& modifiers, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1054 | bool globalContext) { |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 1055 | if (modifiers.fFlags & Modifiers::kFlat_Flag) { |
| 1056 | this->write("flat "); |
| 1057 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1058 | if (modifiers.fFlags & Modifiers::kNoPerspective_Flag) { |
| 1059 | this->write("noperspective "); |
| 1060 | } |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1061 | String layout = modifiers.fLayout.description(); |
Ethan Nicholas | 8da9e94 | 2017-03-09 16:35:09 -0500 | [diff] [blame] | 1062 | if (layout.size()) { |
| 1063 | this->write(layout + " "); |
| 1064 | } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1065 | if ((modifiers.fFlags & Modifiers::kIn_Flag) && |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1066 | (modifiers.fFlags & Modifiers::kOut_Flag)) { |
| 1067 | this->write("inout "); |
| 1068 | } else if (modifiers.fFlags & Modifiers::kIn_Flag) { |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1069 | if (globalContext && |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 1070 | this->caps().generation() < GrGLSLGeneration::k130_GrGLSLGeneration) { |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1071 | this->write(fProgram.fConfig->fKind == ProgramKind::kVertex ? "attribute " |
| 1072 | : "varying "); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1073 | } else { |
| 1074 | this->write("in "); |
| 1075 | } |
| 1076 | } else if (modifiers.fFlags & Modifiers::kOut_Flag) { |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1077 | if (globalContext && |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 1078 | this->caps().generation() < GrGLSLGeneration::k130_GrGLSLGeneration) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1079 | this->write("varying "); |
| 1080 | } else { |
| 1081 | this->write("out "); |
| 1082 | } |
| 1083 | } |
| 1084 | if (modifiers.fFlags & Modifiers::kUniform_Flag) { |
| 1085 | this->write("uniform "); |
| 1086 | } |
| 1087 | if (modifiers.fFlags & Modifiers::kConst_Flag) { |
| 1088 | this->write("const "); |
| 1089 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | void GLSLCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) { |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1093 | if (intf.typeName() == "sk_PerVertex") { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1094 | return; |
| 1095 | } |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1096 | this->writeModifiers(intf.variable().modifiers(), true); |
| 1097 | this->writeLine(intf.typeName() + " {"); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1098 | fIndentation++; |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1099 | const Type* structType = &intf.variable().type(); |
John Stiles | bb43b7e | 2020-12-03 17:36:32 -0500 | [diff] [blame] | 1100 | if (structType->isArray()) { |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1101 | structType = &structType->componentType(); |
| 1102 | } |
| 1103 | for (const auto& f : structType->fields()) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1104 | this->writeModifiers(f.fModifiers, false); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1105 | this->writeTypePrecision(*f.fType); |
ethannicholas | 0730be7 | 2016-09-01 07:59:02 -0700 | [diff] [blame] | 1106 | this->writeType(*f.fType); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1107 | this->writeLine(" " + f.fName + ";"); |
| 1108 | } |
| 1109 | fIndentation--; |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1110 | this->write("}"); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1111 | if (intf.instanceName().size()) { |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1112 | this->write(" "); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1113 | this->write(intf.instanceName()); |
John Stiles | d39aec0 | 2020-12-03 10:42:26 -0500 | [diff] [blame] | 1114 | if (intf.arraySize() > 0) { |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1115 | this->write("["); |
John Stiles | d39aec0 | 2020-12-03 10:42:26 -0500 | [diff] [blame] | 1116 | this->write(to_string(intf.arraySize())); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1117 | this->write("]"); |
| 1118 | } |
| 1119 | } |
| 1120 | this->writeLine(";"); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1123 | void GLSLCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1124 | this->writeExpression(value, Precedence::kTopLevel); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1125 | } |
| 1126 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1127 | const char* GLSLCodeGenerator::getTypePrecision(const Type& type) { |
| 1128 | if (usesPrecisionModifiers()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1129 | switch (type.typeKind()) { |
| 1130 | case Type::TypeKind::kScalar: |
Ethan Nicholas | 722c83e | 2021-05-04 11:39:30 -0400 | [diff] [blame] | 1131 | if (type == *fContext.fTypes.fShort || type == *fContext.fTypes.fUShort) { |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1132 | if (fProgram.fConfig->fSettings.fForceHighPrecision || |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 1133 | this->caps().incompleteShortIntPrecision()) { |
Chris Dalton | c2d0dd6 | 2018-03-07 07:46:10 -0700 | [diff] [blame] | 1134 | return "highp "; |
| 1135 | } |
| 1136 | return "mediump "; |
| 1137 | } |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 1138 | if (type == *fContext.fTypes.fHalf) { |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1139 | return fProgram.fConfig->fSettings.fForceHighPrecision ? "highp " : "mediump "; |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1140 | } |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 1141 | if (type == *fContext.fTypes.fFloat || type == *fContext.fTypes.fInt || |
| 1142 | type == *fContext.fTypes.fUInt) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1143 | return "highp "; |
| 1144 | } |
| 1145 | return ""; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1146 | case Type::TypeKind::kVector: // fall through |
| 1147 | case Type::TypeKind::kMatrix: |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 1148 | case Type::TypeKind::kArray: |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1149 | return this->getTypePrecision(type.componentType()); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1150 | default: |
| 1151 | break; |
| 1152 | } |
| 1153 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1154 | return ""; |
| 1155 | } |
| 1156 | |
| 1157 | void GLSLCodeGenerator::writeTypePrecision(const Type& type) { |
| 1158 | this->write(this->getTypePrecision(type)); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1159 | } |
| 1160 | |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1161 | void GLSLCodeGenerator::writeVarDeclaration(const VarDeclaration& var, bool global) { |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1162 | this->writeModifiers(var.var().modifiers(), global); |
| 1163 | this->writeTypePrecision(var.baseType()); |
| 1164 | this->writeType(var.baseType()); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1165 | this->write(" "); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1166 | this->write(var.var().name()); |
John Stiles | 62a5646 | 2020-12-03 10:41:58 -0500 | [diff] [blame] | 1167 | if (var.arraySize() > 0) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1168 | this->write("["); |
John Stiles | 62a5646 | 2020-12-03 10:41:58 -0500 | [diff] [blame] | 1169 | this->write(to_string(var.arraySize())); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1170 | this->write("]"); |
Ethan Nicholas | 14efcbf | 2017-11-07 09:23:38 -0500 | [diff] [blame] | 1171 | } |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1172 | if (var.value()) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1173 | this->write(" = "); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1174 | this->writeVarInitializer(var.var(), *var.value()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1175 | } |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 1176 | if (!fFoundExternalSamplerDecl && var.var().type() == *fContext.fTypes.fSamplerExternalOES) { |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 1177 | if (this->caps().externalTextureExtensionString()) { |
| 1178 | this->writeExtension(this->caps().externalTextureExtensionString()); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1179 | } |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 1180 | if (this->caps().secondExternalTextureExtensionString()) { |
| 1181 | this->writeExtension(this->caps().secondExternalTextureExtensionString()); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1182 | } |
| 1183 | fFoundExternalSamplerDecl = true; |
Ethan Nicholas | b4dc419 | 2017-06-02 10:16:28 -0400 | [diff] [blame] | 1184 | } |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 1185 | if (!fFoundRectSamplerDecl && var.var().type() == *fContext.fTypes.fSampler2DRect) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1186 | fFoundRectSamplerDecl = true; |
| 1187 | } |
| 1188 | this->write(";"); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | void GLSLCodeGenerator::writeStatement(const Statement& s) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1192 | switch (s.kind()) { |
| 1193 | case Statement::Kind::kBlock: |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1194 | this->writeBlock(s.as<Block>()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1195 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1196 | case Statement::Kind::kExpression: |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1197 | this->writeExpression(*s.as<ExpressionStatement>().expression(), Precedence::kTopLevel); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1198 | this->write(";"); |
| 1199 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1200 | case Statement::Kind::kReturn: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1201 | this->writeReturnStatement(s.as<ReturnStatement>()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1202 | break; |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1203 | case Statement::Kind::kVarDeclaration: |
| 1204 | this->writeVarDeclaration(s.as<VarDeclaration>(), false); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1205 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1206 | case Statement::Kind::kIf: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1207 | this->writeIfStatement(s.as<IfStatement>()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1208 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1209 | case Statement::Kind::kFor: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1210 | this->writeForStatement(s.as<ForStatement>()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1211 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1212 | case Statement::Kind::kDo: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1213 | this->writeDoStatement(s.as<DoStatement>()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1214 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1215 | case Statement::Kind::kSwitch: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1216 | this->writeSwitchStatement(s.as<SwitchStatement>()); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1217 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1218 | case Statement::Kind::kBreak: |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1219 | this->write("break;"); |
| 1220 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1221 | case Statement::Kind::kContinue: |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1222 | this->write("continue;"); |
| 1223 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1224 | case Statement::Kind::kDiscard: |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1225 | this->write("discard;"); |
| 1226 | break; |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 1227 | case Statement::Kind::kInlineMarker: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1228 | case Statement::Kind::kNop: |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1229 | this->write(";"); |
| 1230 | break; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1231 | default: |
John Stiles | eada7bc | 2021-02-02 16:29:32 -0500 | [diff] [blame] | 1232 | SkDEBUGFAILF("unsupported statement: %s", s.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1233 | break; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | void GLSLCodeGenerator::writeBlock(const Block& b) { |
John Stiles | 3744bd6 | 2021-01-26 13:49:43 -0500 | [diff] [blame] | 1238 | // Write scope markers if this block is a scope, or if the block is empty (since we need to emit |
| 1239 | // something here to make the code valid). |
| 1240 | bool isScope = b.isScope() || b.isEmpty(); |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1241 | if (isScope) { |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 1242 | this->writeLine("{"); |
| 1243 | fIndentation++; |
| 1244 | } |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1245 | for (const std::unique_ptr<Statement>& stmt : b.children()) { |
| 1246 | if (!stmt->isEmpty()) { |
| 1247 | this->writeStatement(*stmt); |
John Stiles | e8b5a73 | 2021-03-12 13:25:52 -0500 | [diff] [blame] | 1248 | this->finishLine(); |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1249 | } |
| 1250 | } |
| 1251 | if (isScope) { |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 1252 | fIndentation--; |
| 1253 | this->write("}"); |
| 1254 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | void GLSLCodeGenerator::writeIfStatement(const IfStatement& stmt) { |
| 1258 | this->write("if ("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1259 | this->writeExpression(*stmt.test(), Precedence::kTopLevel); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1260 | this->write(") "); |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1261 | this->writeStatement(*stmt.ifTrue()); |
| 1262 | if (stmt.ifFalse()) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1263 | this->write(" else "); |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1264 | this->writeStatement(*stmt.ifFalse()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | void GLSLCodeGenerator::writeForStatement(const ForStatement& f) { |
Brian Osman | d6f2338 | 2020-12-15 17:08:59 -0500 | [diff] [blame] | 1269 | // Emit loops of the form 'for(;test;)' as 'while(test)', which is probably how they started |
| 1270 | if (!f.initializer() && f.test() && !f.next()) { |
| 1271 | this->write("while ("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1272 | this->writeExpression(*f.test(), Precedence::kTopLevel); |
Brian Osman | d6f2338 | 2020-12-15 17:08:59 -0500 | [diff] [blame] | 1273 | this->write(") "); |
| 1274 | this->writeStatement(*f.statement()); |
| 1275 | return; |
| 1276 | } |
| 1277 | |
John Stiles | b1992c2 | 2021-04-19 14:57:04 -0400 | [diff] [blame] | 1278 | this->write("for ("); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 1279 | if (f.initializer() && !f.initializer()->isEmpty()) { |
John Stiles | b1992c2 | 2021-04-19 14:57:04 -0400 | [diff] [blame] | 1280 | this->writeStatement(*f.initializer()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1281 | } else { |
John Stiles | b1992c2 | 2021-04-19 14:57:04 -0400 | [diff] [blame] | 1282 | this->write("; "); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1283 | } |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 1284 | if (f.test()) { |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 1285 | if (this->caps().addAndTrueToLoopCondition()) { |
Adrienne Walker | ee8295c | 2018-08-21 10:56:30 -0700 | [diff] [blame] | 1286 | std::unique_ptr<Expression> and_true(new BinaryExpression( |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1287 | /*offset=*/-1, f.test()->clone(), Token::Kind::TK_LOGICALAND, |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 1288 | Literal::MakeBool(fContext, /*offset=*/-1, /*value=*/true), |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 1289 | fContext.fTypes.fBool.get())); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1290 | this->writeExpression(*and_true, Precedence::kTopLevel); |
Adrienne Walker | ee8295c | 2018-08-21 10:56:30 -0700 | [diff] [blame] | 1291 | } else { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1292 | this->writeExpression(*f.test(), Precedence::kTopLevel); |
Adrienne Walker | ee8295c | 2018-08-21 10:56:30 -0700 | [diff] [blame] | 1293 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1294 | } |
| 1295 | this->write("; "); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 1296 | if (f.next()) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1297 | this->writeExpression(*f.next(), Precedence::kTopLevel); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1298 | } |
| 1299 | this->write(") "); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 1300 | this->writeStatement(*f.statement()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1303 | void GLSLCodeGenerator::writeDoStatement(const DoStatement& d) { |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 1304 | if (!this->caps().rewriteDoWhileLoops()) { |
Adrienne Walker | 8b23ca6 | 2018-08-22 10:45:41 -0700 | [diff] [blame] | 1305 | this->write("do "); |
Ethan Nicholas | 1fd6116 | 2020-09-28 13:14:19 -0400 | [diff] [blame] | 1306 | this->writeStatement(*d.statement()); |
Adrienne Walker | 8b23ca6 | 2018-08-22 10:45:41 -0700 | [diff] [blame] | 1307 | this->write(" while ("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1308 | this->writeExpression(*d.test(), Precedence::kTopLevel); |
Adrienne Walker | 8b23ca6 | 2018-08-22 10:45:41 -0700 | [diff] [blame] | 1309 | this->write(");"); |
| 1310 | return; |
| 1311 | } |
| 1312 | |
| 1313 | // Otherwise, do the do while loop workaround, to rewrite loops of the form: |
| 1314 | // do { |
| 1315 | // CODE; |
| 1316 | // } while (CONDITION) |
| 1317 | // |
| 1318 | // to loops of the form |
| 1319 | // bool temp = false; |
| 1320 | // while (true) { |
| 1321 | // if (temp) { |
| 1322 | // if (!CONDITION) { |
| 1323 | // break; |
| 1324 | // } |
| 1325 | // } |
| 1326 | // temp = true; |
| 1327 | // CODE; |
| 1328 | // } |
| 1329 | String tmpVar = "_tmpLoopSeenOnce" + to_string(fVarCount++); |
| 1330 | this->write("bool "); |
| 1331 | this->write(tmpVar); |
| 1332 | this->writeLine(" = false;"); |
| 1333 | this->writeLine("while (true) {"); |
| 1334 | fIndentation++; |
| 1335 | this->write("if ("); |
| 1336 | this->write(tmpVar); |
| 1337 | this->writeLine(") {"); |
| 1338 | fIndentation++; |
| 1339 | this->write("if (!"); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1340 | this->writeExpression(*d.test(), Precedence::kPrefix); |
Adrienne Walker | 8b23ca6 | 2018-08-22 10:45:41 -0700 | [diff] [blame] | 1341 | this->writeLine(") {"); |
| 1342 | fIndentation++; |
| 1343 | this->writeLine("break;"); |
| 1344 | fIndentation--; |
| 1345 | this->writeLine("}"); |
| 1346 | fIndentation--; |
| 1347 | this->writeLine("}"); |
| 1348 | this->write(tmpVar); |
| 1349 | this->writeLine(" = true;"); |
Ethan Nicholas | 1fd6116 | 2020-09-28 13:14:19 -0400 | [diff] [blame] | 1350 | this->writeStatement(*d.statement()); |
John Stiles | e8b5a73 | 2021-03-12 13:25:52 -0500 | [diff] [blame] | 1351 | this->finishLine(); |
Adrienne Walker | 8b23ca6 | 2018-08-22 10:45:41 -0700 | [diff] [blame] | 1352 | fIndentation--; |
| 1353 | this->write("}"); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1354 | } |
| 1355 | |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1356 | void GLSLCodeGenerator::writeSwitchStatement(const SwitchStatement& s) { |
| 1357 | this->write("switch ("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1358 | this->writeExpression(*s.value(), Precedence::kTopLevel); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1359 | this->writeLine(") {"); |
| 1360 | fIndentation++; |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 1361 | for (const std::unique_ptr<Statement>& stmt : s.cases()) { |
| 1362 | const SwitchCase& c = stmt->as<SwitchCase>(); |
| 1363 | if (c.value()) { |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1364 | this->write("case "); |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 1365 | this->writeExpression(*c.value(), Precedence::kTopLevel); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1366 | this->writeLine(":"); |
| 1367 | } else { |
| 1368 | this->writeLine("default:"); |
| 1369 | } |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 1370 | if (!c.statement()->isEmpty()) { |
John Stiles | c3ce43b | 2021-03-09 15:37:01 -0500 | [diff] [blame] | 1371 | fIndentation++; |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 1372 | this->writeStatement(*c.statement()); |
John Stiles | e8b5a73 | 2021-03-12 13:25:52 -0500 | [diff] [blame] | 1373 | this->finishLine(); |
John Stiles | c3ce43b | 2021-03-09 15:37:01 -0500 | [diff] [blame] | 1374 | fIndentation--; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1375 | } |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1376 | } |
| 1377 | fIndentation--; |
John Stiles | bf16b6c | 2021-03-12 19:24:31 -0500 | [diff] [blame] | 1378 | this->finishLine(); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1379 | this->write("}"); |
| 1380 | } |
| 1381 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1382 | void GLSLCodeGenerator::writeReturnStatement(const ReturnStatement& r) { |
| 1383 | this->write("return"); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 1384 | if (r.expression()) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1385 | this->write(" "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1386 | this->writeExpression(*r.expression(), Precedence::kTopLevel); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1387 | } |
| 1388 | this->write(";"); |
| 1389 | } |
| 1390 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1391 | void GLSLCodeGenerator::writeHeader() { |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 1392 | if (this->caps().versionDeclString()) { |
| 1393 | this->write(this->caps().versionDeclString()); |
John Stiles | e8b5a73 | 2021-03-12 13:25:52 -0500 | [diff] [blame] | 1394 | this->finishLine(); |
Ethan Nicholas | ba9a04f | 2020-11-06 09:28:04 -0500 | [diff] [blame] | 1395 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1396 | } |
| 1397 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1398 | void GLSLCodeGenerator::writeProgramElement(const ProgramElement& e) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1399 | switch (e.kind()) { |
| 1400 | case ProgramElement::Kind::kExtension: |
Ethan Nicholas | efb09e2 | 2020-09-30 10:17:00 -0400 | [diff] [blame] | 1401 | this->writeExtension(e.as<Extension>().name()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1402 | break; |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1403 | case ProgramElement::Kind::kGlobalVar: { |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1404 | const VarDeclaration& decl = |
| 1405 | e.as<GlobalVarDeclaration>().declaration()->as<VarDeclaration>(); |
| 1406 | int builtin = decl.var().modifiers().fLayout.fBuiltin; |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1407 | if (builtin == -1) { |
| 1408 | // normal var |
| 1409 | this->writeVarDeclaration(decl, true); |
John Stiles | e8b5a73 | 2021-03-12 13:25:52 -0500 | [diff] [blame] | 1410 | this->finishLine(); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1411 | } else if (builtin == SK_FRAGCOLOR_BUILTIN && |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 1412 | this->caps().mustDeclareFragmentShaderOutput()) { |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1413 | if (fProgram.fConfig->fSettings.fFragColorIsInOut) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1414 | this->write("inout "); |
| 1415 | } else { |
| 1416 | this->write("out "); |
Mike Klein | 5ce3972 | 2017-06-27 22:52:03 +0000 | [diff] [blame] | 1417 | } |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1418 | if (usesPrecisionModifiers()) { |
| 1419 | this->write("mediump "); |
| 1420 | } |
| 1421 | this->writeLine("vec4 sk_FragColor;"); |
Mike Klein | 5ce3972 | 2017-06-27 22:52:03 +0000 | [diff] [blame] | 1422 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1423 | break; |
Mike Klein | 5ce3972 | 2017-06-27 22:52:03 +0000 | [diff] [blame] | 1424 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1425 | case ProgramElement::Kind::kInterfaceBlock: |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1426 | this->writeInterfaceBlock(e.as<InterfaceBlock>()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1427 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1428 | case ProgramElement::Kind::kFunction: |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1429 | this->writeFunction(e.as<FunctionDefinition>()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1430 | break; |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1431 | case ProgramElement::Kind::kFunctionPrototype: |
| 1432 | this->writeFunctionPrototype(e.as<FunctionPrototype>()); |
| 1433 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1434 | case ProgramElement::Kind::kModifiers: { |
Ethan Nicholas | 077050b | 2020-10-13 10:30:20 -0400 | [diff] [blame] | 1435 | const Modifiers& modifiers = e.as<ModifiersDeclaration>().modifiers(); |
Chris Dalton | f1b47bb | 2017-10-06 11:57:51 -0600 | [diff] [blame] | 1436 | this->writeModifiers(modifiers, true); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1437 | this->writeLine(";"); |
| 1438 | break; |
Chris Dalton | f1b47bb | 2017-10-06 11:57:51 -0600 | [diff] [blame] | 1439 | } |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 1440 | case ProgramElement::Kind::kStructDefinition: |
Brian Osman | 02bc522 | 2021-01-28 11:00:20 -0500 | [diff] [blame] | 1441 | this->writeStructDefinition(e.as<StructDefinition>()); |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 1442 | break; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1443 | default: |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1444 | SkDEBUGFAILF("unsupported program element %s\n", e.description().c_str()); |
| 1445 | break; |
Ethan Nicholas | c070939 | 2017-06-27 11:20:22 -0400 | [diff] [blame] | 1446 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1447 | } |
| 1448 | |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 1449 | void GLSLCodeGenerator::writeInputVars() { |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 1450 | if (fProgram.fInputs.fUseFlipRTUniform) { |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 1451 | const char* precision = usesPrecisionModifiers() ? "highp " : ""; |
| 1452 | fGlobals.writeText("uniform "); |
| 1453 | fGlobals.writeText(precision); |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 1454 | fGlobals.writeText("vec2 " SKSL_RTFLIP_NAME ";\n"); |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 1455 | } |
| 1456 | } |
| 1457 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1458 | bool GLSLCodeGenerator::generateCode() { |
John Stiles | fffe384 | 2020-09-02 15:44:18 -0400 | [diff] [blame] | 1459 | this->writeHeader(); |
Ethan Nicholas | 88f6d37 | 2018-07-27 10:03:46 -0400 | [diff] [blame] | 1460 | OutputStream* rawOut = fOut; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1461 | StringStream body; |
| 1462 | fOut = &body; |
John Stiles | 9d7aa41 | 2021-01-15 15:54:02 -0500 | [diff] [blame] | 1463 | // Write all the program elements except for functions. |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1464 | for (const ProgramElement* e : fProgram.elements()) { |
John Stiles | 9d7aa41 | 2021-01-15 15:54:02 -0500 | [diff] [blame] | 1465 | if (!e->is<FunctionDefinition>()) { |
| 1466 | this->writeProgramElement(*e); |
| 1467 | } |
| 1468 | } |
| 1469 | // Write the functions last. |
| 1470 | // Why don't we write things in their original order? Because the Inliner likes to move function |
| 1471 | // bodies around. After inlining, code can inadvertently move upwards, above ProgramElements |
| 1472 | // that the code relies on. |
| 1473 | for (const ProgramElement* e : fProgram.elements()) { |
| 1474 | if (e->is<FunctionDefinition>()) { |
| 1475 | this->writeProgramElement(*e); |
| 1476 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1477 | } |
| 1478 | fOut = rawOut; |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 1479 | |
Ethan Nicholas | 88f6d37 | 2018-07-27 10:03:46 -0400 | [diff] [blame] | 1480 | write_stringstream(fExtensions, *rawOut); |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 1481 | this->writeInputVars(); |
Ethan Nicholas | 88f6d37 | 2018-07-27 10:03:46 -0400 | [diff] [blame] | 1482 | write_stringstream(fGlobals, *rawOut); |
Brian Osman | cc10d79 | 2018-07-20 13:09:45 -0400 | [diff] [blame] | 1483 | |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 1484 | if (!this->caps().canUseFragCoord()) { |
Brian Osman | cc10d79 | 2018-07-20 13:09:45 -0400 | [diff] [blame] | 1485 | Layout layout; |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1486 | switch (fProgram.fConfig->fKind) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 1487 | case ProgramKind::kVertex: { |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 1488 | Modifiers modifiers(layout, Modifiers::kOut_Flag); |
Brian Osman | cc10d79 | 2018-07-20 13:09:45 -0400 | [diff] [blame] | 1489 | this->writeModifiers(modifiers, true); |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 1490 | if (this->usesPrecisionModifiers()) { |
| 1491 | this->write("highp "); |
| 1492 | } |
Brian Osman | cc10d79 | 2018-07-20 13:09:45 -0400 | [diff] [blame] | 1493 | this->write("vec4 sk_FragCoord_Workaround;\n"); |
| 1494 | break; |
| 1495 | } |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 1496 | case ProgramKind::kFragment: { |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 1497 | Modifiers modifiers(layout, Modifiers::kIn_Flag); |
Brian Osman | cc10d79 | 2018-07-20 13:09:45 -0400 | [diff] [blame] | 1498 | this->writeModifiers(modifiers, true); |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 1499 | if (this->usesPrecisionModifiers()) { |
| 1500 | this->write("highp "); |
| 1501 | } |
Brian Osman | cc10d79 | 2018-07-20 13:09:45 -0400 | [diff] [blame] | 1502 | this->write("vec4 sk_FragCoord_Workaround;\n"); |
| 1503 | break; |
| 1504 | } |
| 1505 | default: |
| 1506 | break; |
| 1507 | } |
| 1508 | } |
| 1509 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1510 | if (this->usesPrecisionModifiers()) { |
Brian Osman | f600520 | 2021-07-02 17:12:59 -0400 | [diff] [blame] | 1511 | const char* precision = |
| 1512 | fProgram.fConfig->fSettings.fForceHighPrecision ? "highp" : "mediump"; |
| 1513 | this->write(String::printf("precision %s float;\n", precision)); |
| 1514 | this->write(String::printf("precision %s sampler2D;\n", precision)); |
| 1515 | if (fFoundExternalSamplerDecl && !this->caps().noDefaultPrecisionForExternalSamplers()) { |
| 1516 | this->write(String::printf("precision %s samplerExternalOES;\n", precision)); |
Brian Salomon | 67529b2 | 2019-08-13 15:31:04 -0400 | [diff] [blame] | 1517 | } |
| 1518 | if (fFoundRectSamplerDecl) { |
Brian Osman | f600520 | 2021-07-02 17:12:59 -0400 | [diff] [blame] | 1519 | this->write(String::printf("precision %s sampler2DRect;\n", precision)); |
Brian Salomon | 67529b2 | 2019-08-13 15:31:04 -0400 | [diff] [blame] | 1520 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1521 | } |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 1522 | write_stringstream(fExtraFunctions, *rawOut); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1523 | write_stringstream(body, *rawOut); |
Ethan Nicholas | 39f6da4 | 2021-08-23 13:10:07 -0400 | [diff] [blame] | 1524 | return fContext.fErrors->errorCount() == 0; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1525 | } |
| 1526 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 1527 | } // namespace SkSL |