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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/sksl/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" |
| 13 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 14 | #include "src/sksl/ir/SkSLExtension.h" |
| 15 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 16 | #include "src/sksl/ir/SkSLModifiersDeclaration.h" |
| 17 | #include "src/sksl/ir/SkSLNop.h" |
| 18 | #include "src/sksl/ir/SkSLVariableReference.h" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 19 | |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 20 | #ifndef SKSL_STANDALONE |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "include/private/SkOnce.h" |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 22 | #endif |
| 23 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 24 | namespace SkSL { |
| 25 | |
| 26 | void GLSLCodeGenerator::write(const char* s) { |
| 27 | if (s[0] == 0) { |
| 28 | return; |
| 29 | } |
| 30 | if (fAtLineStart) { |
| 31 | for (int i = 0; i < fIndentation; i++) { |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 32 | fOut->writeText(" "); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 33 | } |
| 34 | } |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 35 | fOut->writeText(s); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 36 | fAtLineStart = false; |
| 37 | } |
| 38 | |
| 39 | void GLSLCodeGenerator::writeLine(const char* s) { |
| 40 | this->write(s); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 41 | fOut->writeText(fLineEnding); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 42 | fAtLineStart = true; |
| 43 | } |
| 44 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 45 | void GLSLCodeGenerator::write(const String& s) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 46 | this->write(s.c_str()); |
| 47 | } |
| 48 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 49 | void GLSLCodeGenerator::write(StringFragment s) { |
| 50 | if (!s.fLength) { |
| 51 | return; |
| 52 | } |
| 53 | if (fAtLineStart) { |
| 54 | for (int i = 0; i < fIndentation; i++) { |
| 55 | fOut->writeText(" "); |
| 56 | } |
| 57 | } |
| 58 | fOut->write(s.fChars, s.fLength); |
| 59 | fAtLineStart = false; |
| 60 | } |
| 61 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 62 | void GLSLCodeGenerator::writeLine(const String& s) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 63 | this->writeLine(s.c_str()); |
| 64 | } |
| 65 | |
| 66 | void GLSLCodeGenerator::writeLine() { |
| 67 | this->writeLine(""); |
| 68 | } |
| 69 | |
Ethan Nicholas | 88f6d37 | 2018-07-27 10:03:46 -0400 | [diff] [blame] | 70 | void GLSLCodeGenerator::writeExtension(const String& name) { |
| 71 | this->writeExtension(name, true); |
| 72 | } |
| 73 | |
| 74 | void GLSLCodeGenerator::writeExtension(const String& name, bool require) { |
| 75 | fExtensions.writeText("#extension "); |
| 76 | fExtensions.write(name.c_str(), name.length()); |
| 77 | fExtensions.writeText(require ? " : require\n" : " : enable\n"); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 80 | bool GLSLCodeGenerator::usesPrecisionModifiers() const { |
| 81 | return fProgram.fSettings.fCaps->usesPrecisionModifiers(); |
| 82 | } |
| 83 | |
| 84 | String GLSLCodeGenerator::getTypeName(const Type& type) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 85 | switch (type.typeKind()) { |
| 86 | case Type::TypeKind::kVector: { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 87 | const Type& component = type.componentType(); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 88 | String result; |
| 89 | if (component == *fContext.fFloat_Type || component == *fContext.fHalf_Type) { |
| 90 | result = "vec"; |
| 91 | } |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 92 | else if (component.isSigned()) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 93 | result = "ivec"; |
| 94 | } |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 95 | else if (component.isUnsigned()) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 96 | result = "uvec"; |
| 97 | } |
| 98 | else if (component == *fContext.fBool_Type) { |
| 99 | result = "bvec"; |
| 100 | } |
| 101 | else { |
| 102 | ABORT("unsupported vector type"); |
| 103 | } |
| 104 | result += to_string(type.columns()); |
| 105 | return result; |
| 106 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 107 | case Type::TypeKind::kMatrix: { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 108 | String result; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 109 | const Type& component = type.componentType(); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 110 | if (component == *fContext.fFloat_Type || component == *fContext.fHalf_Type) { |
| 111 | result = "mat"; |
| 112 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 113 | else { |
| 114 | ABORT("unsupported matrix type"); |
| 115 | } |
| 116 | result += to_string(type.columns()); |
| 117 | if (type.columns() != type.rows()) { |
| 118 | result += "x"; |
| 119 | result += to_string(type.rows()); |
| 120 | } |
| 121 | return result; |
| 122 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 123 | case Type::TypeKind::kArray: { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 124 | String result = this->getTypeName(type.componentType()) + "["; |
| 125 | if (type.columns() != -1) { |
| 126 | result += to_string(type.columns()); |
| 127 | } |
| 128 | result += "]"; |
| 129 | return result; |
| 130 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 131 | case Type::TypeKind::kScalar: { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 132 | if (type == *fContext.fHalf_Type) { |
| 133 | return "float"; |
| 134 | } |
| 135 | else if (type == *fContext.fShort_Type) { |
| 136 | return "int"; |
| 137 | } |
| 138 | else if (type == *fContext.fUShort_Type) { |
| 139 | return "uint"; |
| 140 | } |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 141 | else if (type == *fContext.fByte_Type) { |
| 142 | return "int"; |
| 143 | } |
| 144 | else if (type == *fContext.fUByte_Type) { |
| 145 | return "uint"; |
| 146 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 147 | else { |
| 148 | return type.name(); |
| 149 | } |
| 150 | break; |
| 151 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 152 | case Type::TypeKind::kEnum: |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 153 | return "int"; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 154 | default: |
| 155 | return type.name(); |
| 156 | } |
| 157 | } |
| 158 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 159 | void GLSLCodeGenerator::writeType(const Type& type) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 160 | if (type.typeKind() == Type::TypeKind::kStruct) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 161 | for (const Type* search : fWrittenStructs) { |
| 162 | if (*search == type) { |
| 163 | // already written |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 164 | this->write(type.fName); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 165 | return; |
| 166 | } |
| 167 | } |
| 168 | fWrittenStructs.push_back(&type); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 169 | this->write("struct "); |
| 170 | this->write(type.fName); |
| 171 | this->writeLine(" {"); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 172 | fIndentation++; |
| 173 | for (const auto& f : type.fields()) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 174 | this->writeModifiers(f.fModifiers, false); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 175 | this->writeTypePrecision(*f.fType); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 176 | // sizes (which must be static in structs) are part of the type name here |
ethannicholas | 0730be7 | 2016-09-01 07:59:02 -0700 | [diff] [blame] | 177 | this->writeType(*f.fType); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 178 | this->write(" "); |
| 179 | this->write(f.fName); |
| 180 | this->writeLine(";"); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 181 | } |
| 182 | fIndentation--; |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 183 | this->write("}"); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 184 | } else { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 185 | this->write(this->getTypeName(type)); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | void GLSLCodeGenerator::writeExpression(const Expression& expr, Precedence parentPrecedence) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 190 | switch (expr.kind()) { |
| 191 | case Expression::Kind::kBinary: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 192 | this->writeBinaryExpression(expr.as<BinaryExpression>(), parentPrecedence); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 193 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 194 | case Expression::Kind::kBoolLiteral: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 195 | this->writeBoolLiteral(expr.as<BoolLiteral>()); |
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::kConstructor: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 198 | this->writeConstructor(expr.as<Constructor>(), parentPrecedence); |
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::kIntLiteral: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 201 | this->writeIntLiteral(expr.as<IntLiteral>()); |
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::kFieldAccess: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 204 | this->writeFieldAccess(expr.as<FieldAccess>()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 205 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 206 | case Expression::Kind::kFloatLiteral: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 207 | this->writeFloatLiteral(expr.as<FloatLiteral>()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 208 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 209 | case Expression::Kind::kFunctionCall: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 210 | this->writeFunctionCall(expr.as<FunctionCall>()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 211 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 212 | case Expression::Kind::kPrefix: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 213 | this->writePrefixExpression(expr.as<PrefixExpression>(), parentPrecedence); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 214 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 215 | case Expression::Kind::kPostfix: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 216 | this->writePostfixExpression(expr.as<PostfixExpression>(), parentPrecedence); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 217 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 218 | case Expression::Kind::kSetting: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 219 | this->writeSetting(expr.as<Setting>()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 220 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 221 | case Expression::Kind::kSwizzle: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 222 | this->writeSwizzle(expr.as<Swizzle>()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 223 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 224 | case Expression::Kind::kVariableReference: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 225 | this->writeVariableReference(expr.as<VariableReference>()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 226 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 227 | case Expression::Kind::kTernary: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 228 | this->writeTernaryExpression(expr.as<TernaryExpression>(), parentPrecedence); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 229 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 230 | case Expression::Kind::kIndex: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 231 | this->writeIndexExpression(expr.as<IndexExpression>()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 232 | break; |
| 233 | default: |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 234 | #ifdef SK_DEBUG |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 235 | ABORT("unsupported expression: %s", expr.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 236 | #endif |
| 237 | break; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 241 | static bool is_abs(Expression& expr) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 242 | if (expr.kind() != Expression::Kind::kFunctionCall) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 243 | return false; |
| 244 | } |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 245 | return expr.as<FunctionCall>().fFunction.fName == "abs"; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 248 | // 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] | 249 | // Tegra3 compiler bug. |
| 250 | void GLSLCodeGenerator::writeMinAbsHack(Expression& absExpr, Expression& otherExpr) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 251 | SkASSERT(!fProgram.fSettings.fCaps->canUseMinAndAbsTogether()); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 252 | String tmpVar1 = "minAbsHackVar" + to_string(fVarCount++); |
| 253 | String tmpVar2 = "minAbsHackVar" + to_string(fVarCount++); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 254 | this->fFunctionHeader += String(" ") + this->getTypePrecision(absExpr.type()) + |
| 255 | this->getTypeName(absExpr.type()) + " " + tmpVar1 + ";\n"; |
| 256 | this->fFunctionHeader += String(" ") + this->getTypePrecision(otherExpr.type()) + |
| 257 | this->getTypeName(otherExpr.type()) + " " + tmpVar2 + ";\n"; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 258 | this->write("((" + tmpVar1 + " = "); |
| 259 | this->writeExpression(absExpr, kTopLevel_Precedence); |
| 260 | this->write(") < (" + tmpVar2 + " = "); |
| 261 | this->writeExpression(otherExpr, kAssignment_Precedence); |
| 262 | this->write(") ? " + tmpVar1 + " : " + tmpVar2 + ")"); |
| 263 | } |
| 264 | |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 265 | void GLSLCodeGenerator::writeInverseSqrtHack(const Expression& x) { |
| 266 | this->write("(1.0 / sqrt("); |
| 267 | this->writeExpression(x, kTopLevel_Precedence); |
| 268 | this->write("))"); |
| 269 | } |
| 270 | |
| 271 | void GLSLCodeGenerator::writeDeterminantHack(const Expression& mat) { |
| 272 | String name; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 273 | const Type& type = mat.type(); |
| 274 | if (type == *fContext.fFloat2x2_Type || type == *fContext.fHalf2x2_Type) { |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 275 | name = "_determinant2"; |
| 276 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 277 | fWrittenIntrinsics.insert(name); |
| 278 | fExtraFunctions.writeText(( |
| 279 | "float " + name + "(mat2 m) {" |
| 280 | " return m[0][0] * m[1][1] - m[0][1] * m[1][0];" |
| 281 | "}" |
| 282 | ).c_str()); |
| 283 | } |
| 284 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 285 | else if (type == *fContext.fFloat3x3_Type || type == *fContext.fHalf3x3_Type) { |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 286 | name = "_determinant3"; |
| 287 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 288 | fWrittenIntrinsics.insert(name); |
| 289 | fExtraFunctions.writeText(( |
| 290 | "float " + name + "(mat3 m) {" |
| 291 | " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];" |
| 292 | " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];" |
| 293 | " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];" |
| 294 | " float b01 = a22 * a11 - a12 * a21;" |
| 295 | " float b11 = -a22 * a10 + a12 * a20;" |
| 296 | " float b21 = a21 * a10 - a11 * a20;" |
| 297 | " return a00 * b01 + a01 * b11 + a02 * b21;" |
| 298 | "}" |
| 299 | ).c_str()); |
| 300 | } |
| 301 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 302 | else if (type == *fContext.fFloat4x4_Type || type == *fContext.fHalf4x4_Type) { |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 303 | name = "_determinant3"; |
| 304 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 305 | fWrittenIntrinsics.insert(name); |
| 306 | fExtraFunctions.writeText(( |
| 307 | "mat4 " + name + "(mat4 m) {" |
| 308 | " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3];" |
| 309 | " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3];" |
| 310 | " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2], a23 = m[2][3];" |
| 311 | " float a30 = m[3][0], a31 = m[3][1], a32 = m[3][2], a33 = m[3][3];" |
| 312 | " float b00 = a00 * a11 - a01 * a10;" |
| 313 | " float b01 = a00 * a12 - a02 * a10;" |
| 314 | " float b02 = a00 * a13 - a03 * a10;" |
| 315 | " float b03 = a01 * a12 - a02 * a11;" |
| 316 | " float b04 = a01 * a13 - a03 * a11;" |
| 317 | " float b05 = a02 * a13 - a03 * a12;" |
| 318 | " float b06 = a20 * a31 - a21 * a30;" |
| 319 | " float b07 = a20 * a32 - a22 * a30;" |
| 320 | " float b08 = a20 * a33 - a23 * a30;" |
| 321 | " float b09 = a21 * a32 - a22 * a31;" |
| 322 | " float b10 = a21 * a33 - a23 * a31;" |
| 323 | " float b11 = a22 * a33 - a23 * a32;" |
| 324 | " return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;" |
| 325 | "}" |
| 326 | ).c_str()); |
| 327 | } |
| 328 | } |
| 329 | else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 330 | SkASSERT(false); |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 331 | } |
| 332 | this->write(name + "("); |
| 333 | this->writeExpression(mat, kTopLevel_Precedence); |
| 334 | this->write(")"); |
| 335 | } |
| 336 | |
| 337 | void GLSLCodeGenerator::writeInverseHack(const Expression& mat) { |
| 338 | String name; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 339 | const Type& type = mat.type(); |
| 340 | if (type == *fContext.fFloat2x2_Type || type == *fContext.fHalf2x2_Type) { |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 341 | name = "_inverse2"; |
| 342 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 343 | fWrittenIntrinsics.insert(name); |
| 344 | fExtraFunctions.writeText(( |
| 345 | "mat2 " + name + "(mat2 m) {" |
| 346 | " return mat2(m[1][1], -m[0][1], -m[1][0], m[0][0]) / " |
| 347 | "(m[0][0] * m[1][1] - m[0][1] * m[1][0]);" |
| 348 | "}" |
| 349 | ).c_str()); |
| 350 | } |
| 351 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 352 | else if (type == *fContext.fFloat3x3_Type || type == *fContext.fHalf3x3_Type) { |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 353 | name = "_inverse3"; |
| 354 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 355 | fWrittenIntrinsics.insert(name); |
| 356 | fExtraFunctions.writeText(( |
| 357 | "mat3 " + name + "(mat3 m) {" |
| 358 | " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];" |
| 359 | " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];" |
| 360 | " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];" |
| 361 | " float b01 = a22 * a11 - a12 * a21;" |
| 362 | " float b11 = -a22 * a10 + a12 * a20;" |
| 363 | " float b21 = a21 * a10 - a11 * a20;" |
| 364 | " float det = a00 * b01 + a01 * b11 + a02 * b21;" |
| 365 | " return mat3(b01, (-a22 * a01 + a02 * a21), (a12 * a01 - a02 * a11)," |
| 366 | " b11, (a22 * a00 - a02 * a20), (-a12 * a00 + a02 * a10)," |
| 367 | " b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10)) / det;" |
| 368 | "}" |
| 369 | ).c_str()); |
| 370 | } |
| 371 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 372 | else if (type == *fContext.fFloat4x4_Type || type == *fContext.fHalf4x4_Type) { |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 373 | name = "_inverse4"; |
| 374 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 375 | fWrittenIntrinsics.insert(name); |
| 376 | fExtraFunctions.writeText(( |
| 377 | "mat4 " + name + "(mat4 m) {" |
| 378 | " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3];" |
| 379 | " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3];" |
| 380 | " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2], a23 = m[2][3];" |
| 381 | " float a30 = m[3][0], a31 = m[3][1], a32 = m[3][2], a33 = m[3][3];" |
| 382 | " float b00 = a00 * a11 - a01 * a10;" |
| 383 | " float b01 = a00 * a12 - a02 * a10;" |
| 384 | " float b02 = a00 * a13 - a03 * a10;" |
| 385 | " float b03 = a01 * a12 - a02 * a11;" |
| 386 | " float b04 = a01 * a13 - a03 * a11;" |
| 387 | " float b05 = a02 * a13 - a03 * a12;" |
| 388 | " float b06 = a20 * a31 - a21 * a30;" |
| 389 | " float b07 = a20 * a32 - a22 * a30;" |
| 390 | " float b08 = a20 * a33 - a23 * a30;" |
| 391 | " float b09 = a21 * a32 - a22 * a31;" |
| 392 | " float b10 = a21 * a33 - a23 * a31;" |
| 393 | " float b11 = a22 * a33 - a23 * a32;" |
| 394 | " float det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - " |
| 395 | " b04 * b07 + b05 * b06;" |
| 396 | " return mat4(" |
| 397 | " a11 * b11 - a12 * b10 + a13 * b09," |
| 398 | " a02 * b10 - a01 * b11 - a03 * b09," |
| 399 | " a31 * b05 - a32 * b04 + a33 * b03," |
| 400 | " a22 * b04 - a21 * b05 - a23 * b03," |
| 401 | " a12 * b08 - a10 * b11 - a13 * b07," |
| 402 | " a00 * b11 - a02 * b08 + a03 * b07," |
| 403 | " a32 * b02 - a30 * b05 - a33 * b01," |
| 404 | " a20 * b05 - a22 * b02 + a23 * b01," |
| 405 | " a10 * b10 - a11 * b08 + a13 * b06," |
| 406 | " a01 * b08 - a00 * b10 - a03 * b06," |
| 407 | " a30 * b04 - a31 * b02 + a33 * b00," |
| 408 | " a21 * b02 - a20 * b04 - a23 * b00," |
| 409 | " a11 * b07 - a10 * b09 - a12 * b06," |
| 410 | " a00 * b09 - a01 * b07 + a02 * b06," |
| 411 | " a31 * b01 - a30 * b03 - a32 * b00," |
| 412 | " a20 * b03 - a21 * b01 + a22 * b00) / det;" |
| 413 | "}" |
| 414 | ).c_str()); |
| 415 | } |
| 416 | } |
| 417 | else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 418 | SkASSERT(false); |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 419 | } |
| 420 | this->write(name + "("); |
| 421 | this->writeExpression(mat, kTopLevel_Precedence); |
| 422 | this->write(")"); |
| 423 | } |
| 424 | |
| 425 | void GLSLCodeGenerator::writeTransposeHack(const Expression& mat) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 426 | const Type& type = mat.type(); |
| 427 | String name = "transpose" + to_string(type.columns()) + to_string(type.rows()); |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 428 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 429 | fWrittenIntrinsics.insert(name); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 430 | String typeName = this->getTypeName(type); |
| 431 | const Type& base = type.componentType(); |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 432 | String transposed = this->getTypeName(base.toCompound(fContext, |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 433 | type.rows(), |
| 434 | type.columns())); |
| 435 | fExtraFunctions.writeText((transposed + " " + name + "(" + typeName + " m) {\nreturn " + |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 436 | transposed + "(").c_str()); |
| 437 | const char* separator = ""; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 438 | for (int row = 0; row < type.rows(); ++row) { |
| 439 | for (int column = 0; column < type.columns(); ++column) { |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 440 | fExtraFunctions.writeText(separator); |
| 441 | fExtraFunctions.writeText(("m[" + to_string(column) + "][" + to_string(row) + |
| 442 | "]").c_str()); |
| 443 | separator = ", "; |
| 444 | } |
| 445 | } |
| 446 | fExtraFunctions.writeText("); }"); |
| 447 | } |
| 448 | this->write(name + "("); |
| 449 | this->writeExpression(mat, kTopLevel_Precedence); |
| 450 | this->write(")"); |
| 451 | } |
| 452 | |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 453 | std::unordered_map<StringFragment, GLSLCodeGenerator::FunctionClass>* |
| 454 | GLSLCodeGenerator::fFunctionClasses = nullptr; |
| 455 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 456 | void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) { |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 457 | #ifdef SKSL_STANDALONE |
| 458 | if (!fFunctionClasses) { |
| 459 | #else |
| 460 | static SkOnce once; |
| 461 | once([] { |
| 462 | #endif |
| 463 | fFunctionClasses = new std::unordered_map<StringFragment, FunctionClass>(); |
Adrienne Walker | 92b161f | 2018-08-22 10:41:52 -0700 | [diff] [blame] | 464 | (*fFunctionClasses)["abs"] = FunctionClass::kAbs; |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 465 | (*fFunctionClasses)["atan"] = FunctionClass::kAtan; |
| 466 | (*fFunctionClasses)["determinant"] = FunctionClass::kDeterminant; |
Chris Dalton | b8af5ad | 2019-02-25 14:54:21 -0700 | [diff] [blame] | 467 | (*fFunctionClasses)["dFdx"] = FunctionClass::kDFdx; |
| 468 | (*fFunctionClasses)["dFdy"] = FunctionClass::kDFdy; |
| 469 | (*fFunctionClasses)["fwidth"] = FunctionClass::kFwidth; |
Chris Dalton | a708618 | 2018-11-16 09:33:43 -0500 | [diff] [blame] | 470 | (*fFunctionClasses)["fma"] = FunctionClass::kFMA; |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 471 | (*fFunctionClasses)["fract"] = FunctionClass::kFract; |
| 472 | (*fFunctionClasses)["inverse"] = FunctionClass::kInverse; |
| 473 | (*fFunctionClasses)["inverseSqrt"] = FunctionClass::kInverseSqrt; |
| 474 | (*fFunctionClasses)["min"] = FunctionClass::kMin; |
Adrienne Walker | 2f4c09b | 2018-08-22 16:04:57 -0700 | [diff] [blame] | 475 | (*fFunctionClasses)["pow"] = FunctionClass::kPow; |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 476 | (*fFunctionClasses)["saturate"] = FunctionClass::kSaturate; |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 477 | (*fFunctionClasses)["sample"] = FunctionClass::kTexture; |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 478 | (*fFunctionClasses)["transpose"] = FunctionClass::kTranspose; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 479 | } |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 480 | #ifndef SKSL_STANDALONE |
| 481 | ); |
| 482 | #endif |
| 483 | const auto found = c.fFunction.fBuiltin ? fFunctionClasses->find(c.fFunction.fName) : |
| 484 | fFunctionClasses->end(); |
Brian Osman | 8a83ca4 | 2018-02-12 14:32:17 -0500 | [diff] [blame] | 485 | bool isTextureFunctionWithBias = false; |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 486 | bool nameWritten = false; |
| 487 | if (found != fFunctionClasses->end()) { |
| 488 | switch (found->second) { |
Adrienne Walker | 92b161f | 2018-08-22 10:41:52 -0700 | [diff] [blame] | 489 | case FunctionClass::kAbs: { |
| 490 | if (!fProgram.fSettings.fCaps->emulateAbsIntFunction()) |
| 491 | break; |
| 492 | SkASSERT(c.fArguments.size() == 1); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 493 | if (c.fArguments[0]->type() != *fContext.fInt_Type) |
Adrienne Walker | 92b161f | 2018-08-22 10:41:52 -0700 | [diff] [blame] | 494 | break; |
| 495 | // abs(int) on Intel OSX is incorrect, so emulate it: |
| 496 | String name = "_absemulation"; |
| 497 | this->write(name); |
| 498 | nameWritten = true; |
| 499 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 500 | fWrittenIntrinsics.insert(name); |
| 501 | fExtraFunctions.writeText(( |
| 502 | "int " + name + "(int x) {\n" |
| 503 | " return x * sign(x);\n" |
| 504 | "}\n" |
| 505 | ).c_str()); |
| 506 | } |
| 507 | break; |
| 508 | } |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 509 | case FunctionClass::kAtan: |
| 510 | if (fProgram.fSettings.fCaps->mustForceNegatedAtanParamToFloat() && |
| 511 | c.fArguments.size() == 2 && |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 512 | c.fArguments[1]->kind() == Expression::Kind::kPrefix) { |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 513 | const PrefixExpression& p = (PrefixExpression&) *c.fArguments[1]; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 514 | if (p.fOperator == Token::Kind::TK_MINUS) { |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 515 | this->write("atan("); |
| 516 | this->writeExpression(*c.fArguments[0], kSequence_Precedence); |
| 517 | this->write(", -1.0 * "); |
| 518 | this->writeExpression(*p.fOperand, kMultiplicative_Precedence); |
| 519 | this->write(")"); |
| 520 | return; |
| 521 | } |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 522 | } |
| 523 | break; |
Chris Dalton | b8af5ad | 2019-02-25 14:54:21 -0700 | [diff] [blame] | 524 | case FunctionClass::kDFdy: |
| 525 | if (fProgram.fSettings.fFlipY) { |
| 526 | // Flipping Y also negates the Y derivatives. |
| 527 | this->write("-dFdy"); |
| 528 | nameWritten = true; |
| 529 | } |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 530 | [[fallthrough]]; |
Chris Dalton | b8af5ad | 2019-02-25 14:54:21 -0700 | [diff] [blame] | 531 | case FunctionClass::kDFdx: |
| 532 | case FunctionClass::kFwidth: |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 533 | if (!fFoundDerivatives && |
| 534 | fProgram.fSettings.fCaps->shaderDerivativeExtensionString()) { |
| 535 | SkASSERT(fProgram.fSettings.fCaps->shaderDerivativeSupport()); |
| 536 | this->writeExtension(fProgram.fSettings.fCaps->shaderDerivativeExtensionString()); |
| 537 | fFoundDerivatives = true; |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 538 | } |
| 539 | break; |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 540 | case FunctionClass::kDeterminant: |
| 541 | if (fProgram.fSettings.fCaps->generation() < k150_GrGLSLGeneration) { |
| 542 | SkASSERT(c.fArguments.size() == 1); |
| 543 | this->writeDeterminantHack(*c.fArguments[0]); |
| 544 | return; |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 545 | } |
| 546 | break; |
Chris Dalton | a708618 | 2018-11-16 09:33:43 -0500 | [diff] [blame] | 547 | case FunctionClass::kFMA: |
| 548 | if (!fProgram.fSettings.fCaps->builtinFMASupport()) { |
| 549 | SkASSERT(c.fArguments.size() == 3); |
| 550 | this->write("(("); |
| 551 | this->writeExpression(*c.fArguments[0], kSequence_Precedence); |
| 552 | this->write(") * ("); |
| 553 | this->writeExpression(*c.fArguments[1], kSequence_Precedence); |
| 554 | this->write(") + ("); |
| 555 | this->writeExpression(*c.fArguments[2], kSequence_Precedence); |
| 556 | this->write("))"); |
| 557 | return; |
| 558 | } |
| 559 | break; |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 560 | case FunctionClass::kFract: |
| 561 | if (!fProgram.fSettings.fCaps->canUseFractForNegativeValues()) { |
| 562 | SkASSERT(c.fArguments.size() == 1); |
| 563 | this->write("(0.5 - sign("); |
| 564 | this->writeExpression(*c.fArguments[0], kSequence_Precedence); |
| 565 | this->write(") * (0.5 - fract(abs("); |
| 566 | this->writeExpression(*c.fArguments[0], kSequence_Precedence); |
| 567 | this->write("))))"); |
| 568 | return; |
| 569 | } |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 570 | break; |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 571 | case FunctionClass::kInverse: |
| 572 | if (fProgram.fSettings.fCaps->generation() < k140_GrGLSLGeneration) { |
| 573 | SkASSERT(c.fArguments.size() == 1); |
| 574 | this->writeInverseHack(*c.fArguments[0]); |
| 575 | return; |
| 576 | } |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 577 | break; |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 578 | case FunctionClass::kInverseSqrt: |
| 579 | if (fProgram.fSettings.fCaps->generation() < k130_GrGLSLGeneration) { |
| 580 | SkASSERT(c.fArguments.size() == 1); |
| 581 | this->writeInverseSqrtHack(*c.fArguments[0]); |
| 582 | return; |
| 583 | } |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 584 | break; |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 585 | case FunctionClass::kMin: |
| 586 | if (!fProgram.fSettings.fCaps->canUseMinAndAbsTogether()) { |
| 587 | SkASSERT(c.fArguments.size() == 2); |
| 588 | if (is_abs(*c.fArguments[0])) { |
| 589 | this->writeMinAbsHack(*c.fArguments[0], *c.fArguments[1]); |
| 590 | return; |
| 591 | } |
| 592 | if (is_abs(*c.fArguments[1])) { |
| 593 | // note that this violates the GLSL left-to-right evaluation semantics. |
| 594 | // I doubt it will ever end up mattering, but it's worth calling out. |
| 595 | this->writeMinAbsHack(*c.fArguments[1], *c.fArguments[0]); |
| 596 | return; |
| 597 | } |
| 598 | } |
| 599 | break; |
Adrienne Walker | 2f4c09b | 2018-08-22 16:04:57 -0700 | [diff] [blame] | 600 | case FunctionClass::kPow: |
| 601 | if (!fProgram.fSettings.fCaps->removePowWithConstantExponent()) { |
| 602 | break; |
| 603 | } |
| 604 | // pow(x, y) on some NVIDIA drivers causes crashes if y is a |
| 605 | // constant. It's hard to tell what constitutes "constant" here |
| 606 | // so just replace in all cases. |
| 607 | |
| 608 | // Change pow(x, y) into exp2(y * log2(x)) |
| 609 | this->write("exp2("); |
| 610 | this->writeExpression(*c.fArguments[1], kMultiplicative_Precedence); |
| 611 | this->write(" * log2("); |
| 612 | this->writeExpression(*c.fArguments[0], kSequence_Precedence); |
| 613 | this->write("))"); |
| 614 | return; |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 615 | case FunctionClass::kSaturate: |
| 616 | SkASSERT(c.fArguments.size() == 1); |
| 617 | this->write("clamp("); |
| 618 | this->writeExpression(*c.fArguments[0], kSequence_Precedence); |
| 619 | this->write(", 0.0, 1.0)"); |
| 620 | return; |
| 621 | case FunctionClass::kTexture: { |
| 622 | const char* dim = ""; |
| 623 | bool proj = false; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 624 | const Type& arg0Type = c.fArguments[0]->type(); |
| 625 | const Type& arg1Type = c.fArguments[1]->type(); |
| 626 | switch (arg0Type.dimensions()) { |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 627 | case SpvDim1D: |
| 628 | dim = "1D"; |
| 629 | isTextureFunctionWithBias = true; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 630 | if (arg1Type == *fContext.fFloat_Type) { |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 631 | proj = false; |
| 632 | } else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 633 | SkASSERT(arg1Type == *fContext.fFloat2_Type); |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 634 | proj = true; |
| 635 | } |
| 636 | break; |
| 637 | case SpvDim2D: |
| 638 | dim = "2D"; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 639 | if (arg0Type != *fContext.fSamplerExternalOES_Type) { |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 640 | isTextureFunctionWithBias = true; |
| 641 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 642 | if (arg1Type == *fContext.fFloat2_Type) { |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 643 | proj = false; |
| 644 | } else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 645 | SkASSERT(arg1Type == *fContext.fFloat3_Type); |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 646 | proj = true; |
| 647 | } |
| 648 | break; |
| 649 | case SpvDim3D: |
| 650 | dim = "3D"; |
| 651 | isTextureFunctionWithBias = true; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 652 | if (arg1Type == *fContext.fFloat3_Type) { |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 653 | proj = false; |
| 654 | } else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 655 | SkASSERT(arg1Type == *fContext.fFloat4_Type); |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 656 | proj = true; |
| 657 | } |
| 658 | break; |
| 659 | case SpvDimCube: |
| 660 | dim = "Cube"; |
| 661 | isTextureFunctionWithBias = true; |
| 662 | proj = false; |
| 663 | break; |
| 664 | case SpvDimRect: |
Khushal Sagar | 2cb1315 | 2019-09-11 23:17:26 +0000 | [diff] [blame] | 665 | dim = "2DRect"; |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 666 | proj = false; |
| 667 | break; |
| 668 | case SpvDimBuffer: |
| 669 | SkASSERT(false); // doesn't exist |
| 670 | dim = "Buffer"; |
| 671 | proj = false; |
| 672 | break; |
| 673 | case SpvDimSubpassData: |
| 674 | SkASSERT(false); // doesn't exist |
| 675 | dim = "SubpassData"; |
| 676 | proj = false; |
| 677 | break; |
| 678 | } |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 679 | if (fTextureFunctionOverride != "") { |
| 680 | this->write(fTextureFunctionOverride.c_str()); |
| 681 | } else { |
| 682 | this->write("texture"); |
| 683 | if (fProgram.fSettings.fCaps->generation() < k130_GrGLSLGeneration) { |
| 684 | this->write(dim); |
| 685 | } |
| 686 | if (proj) { |
| 687 | this->write("Proj"); |
| 688 | } |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 689 | } |
| 690 | nameWritten = true; |
| 691 | break; |
| 692 | } |
| 693 | case FunctionClass::kTranspose: |
| 694 | if (fProgram.fSettings.fCaps->generation() < k130_GrGLSLGeneration) { |
| 695 | SkASSERT(c.fArguments.size() == 1); |
| 696 | this->writeTransposeHack(*c.fArguments[0]); |
| 697 | return; |
| 698 | } |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 699 | break; |
| 700 | } |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 701 | } |
| 702 | if (!nameWritten) { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 703 | this->write(c.fFunction.fName); |
| 704 | } |
| 705 | this->write("("); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 706 | const char* separator = ""; |
| 707 | for (const auto& arg : c.fArguments) { |
| 708 | this->write(separator); |
| 709 | separator = ", "; |
| 710 | this->writeExpression(*arg, kSequence_Precedence); |
| 711 | } |
Brian Osman | 8a83ca4 | 2018-02-12 14:32:17 -0500 | [diff] [blame] | 712 | if (fProgram.fSettings.fSharpenTextures && isTextureFunctionWithBias) { |
| 713 | this->write(", -0.5"); |
| 714 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 715 | this->write(")"); |
| 716 | } |
| 717 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 718 | void GLSLCodeGenerator::writeConstructor(const Constructor& c, Precedence parentPrecedence) { |
| 719 | if (c.fArguments.size() == 1 && |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 720 | (this->getTypeName(c.type()) == this->getTypeName(c.fArguments[0]->type()) || |
| 721 | (c.type().typeKind() == Type::TypeKind::kScalar && |
| 722 | c.fArguments[0]->type() == *fContext.fFloatLiteral_Type))) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 723 | // in cases like half(float), they're different types as far as SkSL is concerned but the |
| 724 | // same type as far as GLSL is concerned. We avoid a redundant float(float) by just writing |
| 725 | // out the inner expression here. |
| 726 | this->writeExpression(*c.fArguments[0], parentPrecedence); |
| 727 | return; |
| 728 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 729 | this->writeType(c.type()); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 730 | this->write("("); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 731 | const char* separator = ""; |
| 732 | for (const auto& arg : c.fArguments) { |
| 733 | this->write(separator); |
| 734 | separator = ", "; |
| 735 | this->writeExpression(*arg, kSequence_Precedence); |
| 736 | } |
| 737 | this->write(")"); |
| 738 | } |
| 739 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 740 | void GLSLCodeGenerator::writeFragCoord() { |
Brian Osman | cd3261a | 2018-01-16 13:52:29 +0000 | [diff] [blame] | 741 | if (!fProgram.fSettings.fCaps->canUseFragCoord()) { |
Brian Salomon | dba65f9 | 2018-01-22 08:43:38 -0500 | [diff] [blame] | 742 | if (!fSetupFragCoordWorkaround) { |
| 743 | const char* precision = usesPrecisionModifiers() ? "highp " : ""; |
| 744 | fFunctionHeader += precision; |
| 745 | fFunctionHeader += " float sk_FragCoord_InvW = 1. / sk_FragCoord_Workaround.w;\n"; |
| 746 | fFunctionHeader += precision; |
| 747 | fFunctionHeader += " vec4 sk_FragCoord_Resolved = " |
| 748 | "vec4(sk_FragCoord_Workaround.xyz * sk_FragCoord_InvW, sk_FragCoord_InvW);\n"; |
| 749 | // Ensure that we get exact .5 values for x and y. |
| 750 | fFunctionHeader += " sk_FragCoord_Resolved.xy = floor(sk_FragCoord_Resolved.xy) + " |
| 751 | "vec2(.5);\n"; |
| 752 | fSetupFragCoordWorkaround = true; |
| 753 | } |
| 754 | this->write("sk_FragCoord_Resolved"); |
Brian Osman | cd3261a | 2018-01-16 13:52:29 +0000 | [diff] [blame] | 755 | return; |
| 756 | } |
| 757 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 758 | // We only declare "gl_FragCoord" when we're in the case where we want to use layout qualifiers |
| 759 | // to reverse y. Otherwise it isn't necessary and whether the "in" qualifier appears in the |
| 760 | // declaration varies in earlier GLSL specs. So it is simpler to omit it. |
| 761 | if (!fProgram.fSettings.fFlipY) { |
| 762 | this->write("gl_FragCoord"); |
| 763 | } else if (const char* extension = |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 764 | fProgram.fSettings.fCaps->fragCoordConventionsExtensionString()) { |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 765 | if (!fSetupFragPositionGlobal) { |
| 766 | if (fProgram.fSettings.fCaps->generation() < k150_GrGLSLGeneration) { |
Ethan Nicholas | 88f6d37 | 2018-07-27 10:03:46 -0400 | [diff] [blame] | 767 | this->writeExtension(extension); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 768 | } |
Ethan Nicholas | 88f6d37 | 2018-07-27 10:03:46 -0400 | [diff] [blame] | 769 | fGlobals.writeText("layout(origin_upper_left) in vec4 gl_FragCoord;\n"); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 770 | fSetupFragPositionGlobal = true; |
Greg Daniel | e8e4a3e | 2016-12-12 17:20:42 +0000 | [diff] [blame] | 771 | } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 772 | this->write("gl_FragCoord"); |
Greg Daniel | e8e4a3e | 2016-12-12 17:20:42 +0000 | [diff] [blame] | 773 | } else { |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 774 | if (!fSetupFragPositionLocal) { |
Michael Ludwig | 5e1f6ea | 2018-12-03 15:14:50 -0500 | [diff] [blame] | 775 | fFunctionHeader += usesPrecisionModifiers() ? "highp " : ""; |
Michael Ludwig | f0b6044 | 2018-12-10 14:43:38 +0000 | [diff] [blame] | 776 | fFunctionHeader += " vec4 sk_FragCoord = vec4(gl_FragCoord.x, " SKSL_RTHEIGHT_NAME |
| 777 | " - gl_FragCoord.y, gl_FragCoord.z, gl_FragCoord.w);\n"; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 778 | fSetupFragPositionLocal = true; |
| 779 | } |
| 780 | this->write("sk_FragCoord"); |
| 781 | } |
| 782 | } |
| 783 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 784 | void GLSLCodeGenerator::writeVariableReference(const VariableReference& ref) { |
| 785 | switch (ref.fVariable.fModifiers.fLayout.fBuiltin) { |
| 786 | case SK_FRAGCOLOR_BUILTIN: |
| 787 | if (fProgram.fSettings.fCaps->mustDeclareFragmentShaderOutput()) { |
| 788 | this->write("sk_FragColor"); |
| 789 | } else { |
| 790 | this->write("gl_FragColor"); |
| 791 | } |
| 792 | break; |
| 793 | case SK_FRAGCOORD_BUILTIN: |
| 794 | this->writeFragCoord(); |
| 795 | break; |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 796 | case SK_WIDTH_BUILTIN: |
| 797 | this->write("u_skRTWidth"); |
| 798 | break; |
| 799 | case SK_HEIGHT_BUILTIN: |
| 800 | this->write("u_skRTHeight"); |
| 801 | break; |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 802 | case SK_CLOCKWISE_BUILTIN: |
Chris Dalton | c8ece3d | 2018-07-30 15:03:45 -0600 | [diff] [blame] | 803 | this->write(fProgram.fSettings.fFlipY ? "(!gl_FrontFacing)" : "gl_FrontFacing"); |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 804 | break; |
Chris Dalton | b0fd4b1 | 2019-10-29 13:41:22 -0600 | [diff] [blame] | 805 | case SK_SAMPLEMASK_BUILTIN: |
Chris Dalton | 8a64a44 | 2019-10-29 18:54:58 -0600 | [diff] [blame] | 806 | SkASSERT(fProgram.fSettings.fCaps->sampleMaskSupport()); |
Chris Dalton | b0fd4b1 | 2019-10-29 13:41:22 -0600 | [diff] [blame] | 807 | this->write("gl_SampleMask"); |
| 808 | break; |
Ethan Nicholas | a51740c | 2017-02-07 14:53:32 -0500 | [diff] [blame] | 809 | case SK_VERTEXID_BUILTIN: |
| 810 | this->write("gl_VertexID"); |
| 811 | break; |
Chris Dalton | 8580d51 | 2017-10-14 22:12:33 -0600 | [diff] [blame] | 812 | case SK_INSTANCEID_BUILTIN: |
| 813 | this->write("gl_InstanceID"); |
| 814 | break; |
Ethan Nicholas | 67d6460 | 2017-02-09 10:15:25 -0500 | [diff] [blame] | 815 | case SK_CLIPDISTANCE_BUILTIN: |
| 816 | this->write("gl_ClipDistance"); |
| 817 | break; |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 818 | case SK_IN_BUILTIN: |
| 819 | this->write("gl_in"); |
| 820 | break; |
| 821 | case SK_INVOCATIONID_BUILTIN: |
| 822 | this->write("gl_InvocationID"); |
| 823 | break; |
Ethan Nicholas | eab2baa | 2018-04-13 15:16:27 -0400 | [diff] [blame] | 824 | case SK_LASTFRAGCOLOR_BUILTIN: |
| 825 | this->write(fProgram.fSettings.fCaps->fbFetchColorName()); |
| 826 | break; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 827 | default: |
| 828 | this->write(ref.fVariable.fName); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 829 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | void GLSLCodeGenerator::writeIndexExpression(const IndexExpression& expr) { |
| 833 | this->writeExpression(*expr.fBase, kPostfix_Precedence); |
| 834 | this->write("["); |
| 835 | this->writeExpression(*expr.fIndex, kTopLevel_Precedence); |
| 836 | this->write("]"); |
| 837 | } |
| 838 | |
Brian Osman | cd3261a | 2018-01-16 13:52:29 +0000 | [diff] [blame] | 839 | bool is_sk_position(const FieldAccess& f) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 840 | return "sk_Position" == f.fBase->type().fields()[f.fFieldIndex].fName; |
Brian Osman | cd3261a | 2018-01-16 13:52:29 +0000 | [diff] [blame] | 841 | } |
| 842 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 843 | void GLSLCodeGenerator::writeFieldAccess(const FieldAccess& f) { |
| 844 | if (f.fOwnerKind == FieldAccess::kDefault_OwnerKind) { |
| 845 | this->writeExpression(*f.fBase, kPostfix_Precedence); |
| 846 | this->write("."); |
| 847 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 848 | const Type& baseType = f.fBase->type(); |
| 849 | switch (baseType.fields()[f.fFieldIndex].fModifiers.fLayout.fBuiltin) { |
Ethan Nicholas | 67d6460 | 2017-02-09 10:15:25 -0500 | [diff] [blame] | 850 | case SK_CLIPDISTANCE_BUILTIN: |
| 851 | this->write("gl_ClipDistance"); |
| 852 | break; |
| 853 | default: |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 854 | StringFragment name = baseType.fields()[f.fFieldIndex].fName; |
Ethan Nicholas | bed683a | 2017-09-26 14:23:59 -0400 | [diff] [blame] | 855 | if (name == "sk_Position") { |
| 856 | this->write("gl_Position"); |
| 857 | } else if (name == "sk_PointSize") { |
| 858 | this->write("gl_PointSize"); |
| 859 | } else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 860 | this->write(baseType.fields()[f.fFieldIndex].fName); |
Ethan Nicholas | bed683a | 2017-09-26 14:23:59 -0400 | [diff] [blame] | 861 | } |
Ethan Nicholas | 67d6460 | 2017-02-09 10:15:25 -0500 | [diff] [blame] | 862 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 865 | void GLSLCodeGenerator::writeSwizzle(const Swizzle& swizzle) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 866 | this->writeExpression(*swizzle.fBase, kPostfix_Precedence); |
| 867 | this->write("."); |
| 868 | for (int c : swizzle.fComponents) { |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 869 | SkASSERT(c >= 0 && c <= 3); |
| 870 | this->write(&("x\0y\0z\0w\0"[c * 2])); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 871 | } |
| 872 | } |
| 873 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 874 | GLSLCodeGenerator::Precedence GLSLCodeGenerator::GetBinaryPrecedence(Token::Kind op) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 875 | switch (op) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 876 | case Token::Kind::TK_STAR: // fall through |
| 877 | case Token::Kind::TK_SLASH: // fall through |
| 878 | case Token::Kind::TK_PERCENT: return GLSLCodeGenerator::kMultiplicative_Precedence; |
| 879 | case Token::Kind::TK_PLUS: // fall through |
| 880 | case Token::Kind::TK_MINUS: return GLSLCodeGenerator::kAdditive_Precedence; |
| 881 | case Token::Kind::TK_SHL: // fall through |
| 882 | case Token::Kind::TK_SHR: return GLSLCodeGenerator::kShift_Precedence; |
| 883 | case Token::Kind::TK_LT: // fall through |
| 884 | case Token::Kind::TK_GT: // fall through |
| 885 | case Token::Kind::TK_LTEQ: // fall through |
| 886 | case Token::Kind::TK_GTEQ: return GLSLCodeGenerator::kRelational_Precedence; |
| 887 | case Token::Kind::TK_EQEQ: // fall through |
| 888 | case Token::Kind::TK_NEQ: return GLSLCodeGenerator::kEquality_Precedence; |
| 889 | case Token::Kind::TK_BITWISEAND: return GLSLCodeGenerator::kBitwiseAnd_Precedence; |
| 890 | case Token::Kind::TK_BITWISEXOR: return GLSLCodeGenerator::kBitwiseXor_Precedence; |
| 891 | case Token::Kind::TK_BITWISEOR: return GLSLCodeGenerator::kBitwiseOr_Precedence; |
| 892 | case Token::Kind::TK_LOGICALAND: return GLSLCodeGenerator::kLogicalAnd_Precedence; |
| 893 | case Token::Kind::TK_LOGICALXOR: return GLSLCodeGenerator::kLogicalXor_Precedence; |
| 894 | case Token::Kind::TK_LOGICALOR: return GLSLCodeGenerator::kLogicalOr_Precedence; |
| 895 | case Token::Kind::TK_EQ: // fall through |
| 896 | case Token::Kind::TK_PLUSEQ: // fall through |
| 897 | case Token::Kind::TK_MINUSEQ: // fall through |
| 898 | case Token::Kind::TK_STAREQ: // fall through |
| 899 | case Token::Kind::TK_SLASHEQ: // fall through |
| 900 | case Token::Kind::TK_PERCENTEQ: // fall through |
| 901 | case Token::Kind::TK_SHLEQ: // fall through |
| 902 | case Token::Kind::TK_SHREQ: // fall through |
| 903 | case Token::Kind::TK_LOGICALANDEQ: // fall through |
| 904 | case Token::Kind::TK_LOGICALXOREQ: // fall through |
| 905 | case Token::Kind::TK_LOGICALOREQ: // fall through |
| 906 | case Token::Kind::TK_BITWISEANDEQ: // fall through |
| 907 | case Token::Kind::TK_BITWISEXOREQ: // fall through |
| 908 | case Token::Kind::TK_BITWISEOREQ: return GLSLCodeGenerator::kAssignment_Precedence; |
| 909 | case Token::Kind::TK_COMMA: return GLSLCodeGenerator::kSequence_Precedence; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 910 | default: ABORT("unsupported binary operator"); |
| 911 | } |
| 912 | } |
| 913 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 914 | void GLSLCodeGenerator::writeBinaryExpression(const BinaryExpression& b, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 915 | Precedence parentPrecedence) { |
Adrienne Walker | c02165f | 2018-08-21 11:08:11 -0700 | [diff] [blame] | 916 | if (fProgram.fSettings.fCaps->unfoldShortCircuitAsTernary() && |
Ethan Nicholas | bf66ffb | 2020-09-16 22:05:10 +0000 | [diff] [blame^] | 917 | (b.fOperator == Token::Kind::TK_LOGICALAND || |
| 918 | b.fOperator == Token::Kind::TK_LOGICALOR)) { |
Adrienne Walker | c02165f | 2018-08-21 11:08:11 -0700 | [diff] [blame] | 919 | this->writeShortCircuitWorkaroundExpression(b, parentPrecedence); |
| 920 | return; |
| 921 | } |
| 922 | |
Ethan Nicholas | bf66ffb | 2020-09-16 22:05:10 +0000 | [diff] [blame^] | 923 | Precedence precedence = GetBinaryPrecedence(b.fOperator); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 924 | if (precedence >= parentPrecedence) { |
| 925 | this->write("("); |
| 926 | } |
Ethan Nicholas | 0b63196 | 2018-07-24 13:41:11 -0400 | [diff] [blame] | 927 | bool positionWorkaround = fProgramKind == Program::Kind::kVertex_Kind && |
Ethan Nicholas | bf66ffb | 2020-09-16 22:05:10 +0000 | [diff] [blame^] | 928 | Compiler::IsAssignment(b.fOperator) && |
| 929 | b.fLeft->kind() == Expression::Kind::kFieldAccess && |
| 930 | is_sk_position((FieldAccess&) *b.fLeft) && |
| 931 | !b.fRight->containsRTAdjust() && |
Brian Osman | cd3261a | 2018-01-16 13:52:29 +0000 | [diff] [blame] | 932 | !fProgram.fSettings.fCaps->canUseFragCoord(); |
| 933 | if (positionWorkaround) { |
| 934 | this->write("sk_FragCoord_Workaround = ("); |
| 935 | } |
Ethan Nicholas | bf66ffb | 2020-09-16 22:05:10 +0000 | [diff] [blame^] | 936 | this->writeExpression(*b.fLeft, precedence); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 937 | this->write(" "); |
Ethan Nicholas | bf66ffb | 2020-09-16 22:05:10 +0000 | [diff] [blame^] | 938 | this->write(Compiler::OperatorName(b.fOperator)); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 939 | this->write(" "); |
Ethan Nicholas | bf66ffb | 2020-09-16 22:05:10 +0000 | [diff] [blame^] | 940 | this->writeExpression(*b.fRight, precedence); |
Brian Osman | cd3261a | 2018-01-16 13:52:29 +0000 | [diff] [blame] | 941 | if (positionWorkaround) { |
| 942 | this->write(")"); |
| 943 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 944 | if (precedence >= parentPrecedence) { |
| 945 | this->write(")"); |
| 946 | } |
| 947 | } |
| 948 | |
Adrienne Walker | c02165f | 2018-08-21 11:08:11 -0700 | [diff] [blame] | 949 | void GLSLCodeGenerator::writeShortCircuitWorkaroundExpression(const BinaryExpression& b, |
| 950 | Precedence parentPrecedence) { |
| 951 | if (kTernary_Precedence >= parentPrecedence) { |
| 952 | this->write("("); |
| 953 | } |
| 954 | |
| 955 | // Transform: |
| 956 | // a && b => a ? b : false |
| 957 | // a || b => a ? true : b |
Ethan Nicholas | bf66ffb | 2020-09-16 22:05:10 +0000 | [diff] [blame^] | 958 | this->writeExpression(*b.fLeft, kTernary_Precedence); |
Adrienne Walker | c02165f | 2018-08-21 11:08:11 -0700 | [diff] [blame] | 959 | this->write(" ? "); |
Ethan Nicholas | bf66ffb | 2020-09-16 22:05:10 +0000 | [diff] [blame^] | 960 | if (b.fOperator == Token::Kind::TK_LOGICALAND) { |
| 961 | this->writeExpression(*b.fRight, kTernary_Precedence); |
Adrienne Walker | c02165f | 2018-08-21 11:08:11 -0700 | [diff] [blame] | 962 | } else { |
| 963 | BoolLiteral boolTrue(fContext, -1, true); |
| 964 | this->writeBoolLiteral(boolTrue); |
| 965 | } |
| 966 | this->write(" : "); |
Ethan Nicholas | bf66ffb | 2020-09-16 22:05:10 +0000 | [diff] [blame^] | 967 | if (b.fOperator == Token::Kind::TK_LOGICALAND) { |
Adrienne Walker | c02165f | 2018-08-21 11:08:11 -0700 | [diff] [blame] | 968 | BoolLiteral boolFalse(fContext, -1, false); |
| 969 | this->writeBoolLiteral(boolFalse); |
| 970 | } else { |
Ethan Nicholas | bf66ffb | 2020-09-16 22:05:10 +0000 | [diff] [blame^] | 971 | this->writeExpression(*b.fRight, kTernary_Precedence); |
Adrienne Walker | c02165f | 2018-08-21 11:08:11 -0700 | [diff] [blame] | 972 | } |
| 973 | if (kTernary_Precedence >= parentPrecedence) { |
| 974 | this->write(")"); |
| 975 | } |
| 976 | } |
| 977 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 978 | void GLSLCodeGenerator::writeTernaryExpression(const TernaryExpression& t, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 979 | Precedence parentPrecedence) { |
| 980 | if (kTernary_Precedence >= parentPrecedence) { |
| 981 | this->write("("); |
| 982 | } |
| 983 | this->writeExpression(*t.fTest, kTernary_Precedence); |
| 984 | this->write(" ? "); |
| 985 | this->writeExpression(*t.fIfTrue, kTernary_Precedence); |
| 986 | this->write(" : "); |
| 987 | this->writeExpression(*t.fIfFalse, kTernary_Precedence); |
| 988 | if (kTernary_Precedence >= parentPrecedence) { |
| 989 | this->write(")"); |
| 990 | } |
| 991 | } |
| 992 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 993 | void GLSLCodeGenerator::writePrefixExpression(const PrefixExpression& p, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 994 | Precedence parentPrecedence) { |
| 995 | if (kPrefix_Precedence >= parentPrecedence) { |
| 996 | this->write("("); |
| 997 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 998 | this->write(Compiler::OperatorName(p.fOperator)); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 999 | this->writeExpression(*p.fOperand, kPrefix_Precedence); |
| 1000 | if (kPrefix_Precedence >= parentPrecedence) { |
| 1001 | this->write(")"); |
| 1002 | } |
| 1003 | } |
| 1004 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1005 | void GLSLCodeGenerator::writePostfixExpression(const PostfixExpression& p, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1006 | Precedence parentPrecedence) { |
| 1007 | if (kPostfix_Precedence >= parentPrecedence) { |
| 1008 | this->write("("); |
| 1009 | } |
| 1010 | this->writeExpression(*p.fOperand, kPostfix_Precedence); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1011 | this->write(Compiler::OperatorName(p.fOperator)); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1012 | if (kPostfix_Precedence >= parentPrecedence) { |
| 1013 | this->write(")"); |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | void GLSLCodeGenerator::writeBoolLiteral(const BoolLiteral& b) { |
| 1018 | this->write(b.fValue ? "true" : "false"); |
| 1019 | } |
| 1020 | |
| 1021 | void GLSLCodeGenerator::writeIntLiteral(const IntLiteral& i) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1022 | const Type& type = i.type(); |
| 1023 | if (type == *fContext.fUInt_Type) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1024 | this->write(to_string(i.fValue & 0xffffffff) + "u"); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1025 | } else if (type == *fContext.fUShort_Type) { |
Ethan Nicholas | 58d5648 | 2017-12-19 09:29:22 -0500 | [diff] [blame] | 1026 | this->write(to_string(i.fValue & 0xffff) + "u"); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1027 | } else if (type == *fContext.fUByte_Type) { |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1028 | this->write(to_string(i.fValue & 0xff) + "u"); |
| 1029 | } else { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1030 | this->write(to_string((int32_t) i.fValue)); |
| 1031 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | void GLSLCodeGenerator::writeFloatLiteral(const FloatLiteral& f) { |
| 1035 | this->write(to_string(f.fValue)); |
| 1036 | } |
| 1037 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1038 | void GLSLCodeGenerator::writeSetting(const Setting& s) { |
| 1039 | ABORT("internal error; setting was not folded to a constant during compilation\n"); |
| 1040 | } |
| 1041 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1042 | void GLSLCodeGenerator::writeFunction(const FunctionDefinition& f) { |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 1043 | fSetupFragPositionLocal = false; |
| 1044 | fSetupFragCoordWorkaround = false; |
John Stiles | fffe384 | 2020-09-02 15:44:18 -0400 | [diff] [blame] | 1045 | |
| 1046 | // The pipeline-stage code generator can't use functions written this way, so make sure we don't |
| 1047 | // accidentally end up here. |
| 1048 | SkASSERT(fProgramKind != Program::kPipelineStage_Kind); |
| 1049 | |
| 1050 | this->writeTypePrecision(f.fDeclaration.fReturnType); |
| 1051 | this->writeType(f.fDeclaration.fReturnType); |
| 1052 | this->write(" " + f.fDeclaration.fName + "("); |
| 1053 | const char* separator = ""; |
| 1054 | for (const auto& param : f.fDeclaration.fParameters) { |
| 1055 | this->write(separator); |
| 1056 | separator = ", "; |
| 1057 | this->writeModifiers(param->fModifiers, false); |
| 1058 | std::vector<int> sizes; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1059 | const Type* type = ¶m->type(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1060 | while (type->typeKind() == Type::TypeKind::kArray) { |
John Stiles | fffe384 | 2020-09-02 15:44:18 -0400 | [diff] [blame] | 1061 | sizes.push_back(type->columns()); |
| 1062 | type = &type->componentType(); |
| 1063 | } |
| 1064 | this->writeTypePrecision(*type); |
| 1065 | this->writeType(*type); |
| 1066 | this->write(" " + param->fName); |
| 1067 | for (int s : sizes) { |
| 1068 | if (s <= 0) { |
| 1069 | this->write("[]"); |
| 1070 | } else { |
| 1071 | this->write("[" + to_string(s) + "]"); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1072 | } |
| 1073 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1074 | } |
John Stiles | fffe384 | 2020-09-02 15:44:18 -0400 | [diff] [blame] | 1075 | this->writeLine(") {"); |
| 1076 | fIndentation++; |
| 1077 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1078 | fFunctionHeader = ""; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1079 | OutputStream* oldOut = fOut; |
| 1080 | StringStream buffer; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1081 | fOut = &buffer; |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1082 | this->writeStatements(f.fBody->as<Block>().fStatements); |
John Stiles | fffe384 | 2020-09-02 15:44:18 -0400 | [diff] [blame] | 1083 | |
| 1084 | fIndentation--; |
| 1085 | this->writeLine("}"); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1086 | |
| 1087 | fOut = oldOut; |
| 1088 | this->write(fFunctionHeader); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1089 | this->write(buffer.str()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1090 | } |
| 1091 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1092 | void GLSLCodeGenerator::writeModifiers(const Modifiers& modifiers, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1093 | bool globalContext) { |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 1094 | if (modifiers.fFlags & Modifiers::kFlat_Flag) { |
| 1095 | this->write("flat "); |
| 1096 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1097 | if (modifiers.fFlags & Modifiers::kNoPerspective_Flag) { |
| 1098 | this->write("noperspective "); |
| 1099 | } |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1100 | String layout = modifiers.fLayout.description(); |
Ethan Nicholas | 8da9e94 | 2017-03-09 16:35:09 -0500 | [diff] [blame] | 1101 | if (layout.size()) { |
| 1102 | this->write(layout + " "); |
| 1103 | } |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 1104 | if (modifiers.fFlags & Modifiers::kReadOnly_Flag) { |
| 1105 | this->write("readonly "); |
| 1106 | } |
| 1107 | if (modifiers.fFlags & Modifiers::kWriteOnly_Flag) { |
| 1108 | this->write("writeonly "); |
| 1109 | } |
| 1110 | if (modifiers.fFlags & Modifiers::kCoherent_Flag) { |
| 1111 | this->write("coherent "); |
| 1112 | } |
| 1113 | if (modifiers.fFlags & Modifiers::kVolatile_Flag) { |
| 1114 | this->write("volatile "); |
| 1115 | } |
| 1116 | if (modifiers.fFlags & Modifiers::kRestrict_Flag) { |
| 1117 | this->write("restrict "); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1118 | } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1119 | if ((modifiers.fFlags & Modifiers::kIn_Flag) && |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1120 | (modifiers.fFlags & Modifiers::kOut_Flag)) { |
| 1121 | this->write("inout "); |
| 1122 | } else if (modifiers.fFlags & Modifiers::kIn_Flag) { |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1123 | if (globalContext && |
| 1124 | fProgram.fSettings.fCaps->generation() < GrGLSLGeneration::k130_GrGLSLGeneration) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1125 | this->write(fProgramKind == Program::kVertex_Kind ? "attribute " |
| 1126 | : "varying "); |
| 1127 | } else { |
| 1128 | this->write("in "); |
| 1129 | } |
| 1130 | } else if (modifiers.fFlags & Modifiers::kOut_Flag) { |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1131 | if (globalContext && |
| 1132 | fProgram.fSettings.fCaps->generation() < GrGLSLGeneration::k130_GrGLSLGeneration) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1133 | this->write("varying "); |
| 1134 | } else { |
| 1135 | this->write("out "); |
| 1136 | } |
| 1137 | } |
| 1138 | if (modifiers.fFlags & Modifiers::kUniform_Flag) { |
| 1139 | this->write("uniform "); |
| 1140 | } |
| 1141 | if (modifiers.fFlags & Modifiers::kConst_Flag) { |
| 1142 | this->write("const "); |
| 1143 | } |
Ethan Nicholas | a7ceb50 | 2019-01-11 10:31:48 -0500 | [diff] [blame] | 1144 | if (modifiers.fFlags & Modifiers::kPLS_Flag) { |
| 1145 | this->write("__pixel_localEXT "); |
| 1146 | } |
| 1147 | if (modifiers.fFlags & Modifiers::kPLSIn_Flag) { |
| 1148 | this->write("__pixel_local_inEXT "); |
| 1149 | } |
| 1150 | if (modifiers.fFlags & Modifiers::kPLSOut_Flag) { |
| 1151 | this->write("__pixel_local_outEXT "); |
| 1152 | } |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 1153 | switch (modifiers.fLayout.fFormat) { |
| 1154 | case Layout::Format::kUnspecified: |
| 1155 | break; |
Robert Phillips | ebab03f | 2019-07-22 08:48:18 -0400 | [diff] [blame] | 1156 | case Layout::Format::kRGBA32F: // fall through |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 1157 | case Layout::Format::kR32F: |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1158 | this->write("highp "); |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 1159 | break; |
Robert Phillips | ebab03f | 2019-07-22 08:48:18 -0400 | [diff] [blame] | 1160 | case Layout::Format::kRGBA16F: // fall through |
| 1161 | case Layout::Format::kR16F: // fall through |
| 1162 | case Layout::Format::kLUMINANCE16F: // fall through |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 1163 | case Layout::Format::kRG16F: |
| 1164 | this->write("mediump "); |
| 1165 | break; |
Robert Phillips | ebab03f | 2019-07-22 08:48:18 -0400 | [diff] [blame] | 1166 | case Layout::Format::kRGBA8: // fall through |
| 1167 | case Layout::Format::kR8: // fall through |
| 1168 | case Layout::Format::kRGBA8I: // fall through |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 1169 | case Layout::Format::kR8I: |
| 1170 | this->write("lowp "); |
| 1171 | break; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1172 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | void GLSLCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) { |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 1176 | if (intf.fTypeName == "sk_PerVertex") { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1177 | return; |
| 1178 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1179 | this->writeModifiers(intf.fVariable.fModifiers, true); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1180 | this->writeLine(intf.fTypeName + " {"); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1181 | fIndentation++; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1182 | const Type* structType = &intf.fVariable.type(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1183 | while (structType->typeKind() == Type::TypeKind::kArray) { |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1184 | structType = &structType->componentType(); |
| 1185 | } |
| 1186 | for (const auto& f : structType->fields()) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1187 | this->writeModifiers(f.fModifiers, false); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1188 | this->writeTypePrecision(*f.fType); |
ethannicholas | 0730be7 | 2016-09-01 07:59:02 -0700 | [diff] [blame] | 1189 | this->writeType(*f.fType); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1190 | this->writeLine(" " + f.fName + ";"); |
| 1191 | } |
| 1192 | fIndentation--; |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1193 | this->write("}"); |
| 1194 | if (intf.fInstanceName.size()) { |
| 1195 | this->write(" "); |
| 1196 | this->write(intf.fInstanceName); |
| 1197 | for (const auto& size : intf.fSizes) { |
| 1198 | this->write("["); |
| 1199 | if (size) { |
| 1200 | this->writeExpression(*size, kTopLevel_Precedence); |
| 1201 | } |
| 1202 | this->write("]"); |
| 1203 | } |
| 1204 | } |
| 1205 | this->writeLine(";"); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1206 | } |
| 1207 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1208 | void GLSLCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) { |
| 1209 | this->writeExpression(value, kTopLevel_Precedence); |
| 1210 | } |
| 1211 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1212 | const char* GLSLCodeGenerator::getTypePrecision(const Type& type) { |
| 1213 | if (usesPrecisionModifiers()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1214 | switch (type.typeKind()) { |
| 1215 | case Type::TypeKind::kScalar: |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1216 | if (type == *fContext.fShort_Type || type == *fContext.fUShort_Type || |
| 1217 | type == *fContext.fByte_Type || type == *fContext.fUByte_Type) { |
Chris Dalton | c2d0dd6 | 2018-03-07 07:46:10 -0700 | [diff] [blame] | 1218 | if (fProgram.fSettings.fForceHighPrecision || |
| 1219 | fProgram.fSettings.fCaps->incompleteShortIntPrecision()) { |
| 1220 | return "highp "; |
| 1221 | } |
| 1222 | return "mediump "; |
| 1223 | } |
| 1224 | if (type == *fContext.fHalf_Type) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1225 | return fProgram.fSettings.fForceHighPrecision ? "highp " : "mediump "; |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1226 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1227 | if (type == *fContext.fFloat_Type || type == *fContext.fInt_Type || |
| 1228 | type == *fContext.fUInt_Type) { |
| 1229 | return "highp "; |
| 1230 | } |
| 1231 | return ""; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1232 | case Type::TypeKind::kVector: // fall through |
| 1233 | case Type::TypeKind::kMatrix: |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1234 | return this->getTypePrecision(type.componentType()); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1235 | default: |
| 1236 | break; |
| 1237 | } |
| 1238 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1239 | return ""; |
| 1240 | } |
| 1241 | |
| 1242 | void GLSLCodeGenerator::writeTypePrecision(const Type& type) { |
| 1243 | this->write(this->getTypePrecision(type)); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1244 | } |
| 1245 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1246 | void GLSLCodeGenerator::writeVarDeclarations(const VarDeclarations& decl, bool global) { |
Ethan Nicholas | 14efcbf | 2017-11-07 09:23:38 -0500 | [diff] [blame] | 1247 | if (!decl.fVars.size()) { |
| 1248 | return; |
| 1249 | } |
Ethan Nicholas | b4dc419 | 2017-06-02 10:16:28 -0400 | [diff] [blame] | 1250 | bool wroteType = false; |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1251 | for (const auto& stmt : decl.fVars) { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1252 | const VarDeclaration& var = stmt->as<VarDeclaration>(); |
Ethan Nicholas | b4dc419 | 2017-06-02 10:16:28 -0400 | [diff] [blame] | 1253 | if (wroteType) { |
| 1254 | this->write(", "); |
| 1255 | } else { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1256 | this->writeModifiers(var.fVar->fModifiers, global); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1257 | this->writeTypePrecision(decl.fBaseType); |
Ethan Nicholas | b4dc419 | 2017-06-02 10:16:28 -0400 | [diff] [blame] | 1258 | this->writeType(decl.fBaseType); |
| 1259 | this->write(" "); |
| 1260 | wroteType = true; |
| 1261 | } |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1262 | this->write(var.fVar->fName); |
| 1263 | for (const auto& size : var.fSizes) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1264 | this->write("["); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1265 | if (size) { |
| 1266 | this->writeExpression(*size, kTopLevel_Precedence); |
| 1267 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1268 | this->write("]"); |
| 1269 | } |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1270 | if (var.fValue) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1271 | this->write(" = "); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1272 | this->writeVarInitializer(*var.fVar, *var.fValue); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1273 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1274 | if (!fFoundExternalSamplerDecl && var.fVar->type() == *fContext.fSamplerExternalOES_Type) { |
Brian Osman | 4b2f915 | 2018-04-17 11:19:57 -0400 | [diff] [blame] | 1275 | if (fProgram.fSettings.fCaps->externalTextureExtensionString()) { |
Ethan Nicholas | 88f6d37 | 2018-07-27 10:03:46 -0400 | [diff] [blame] | 1276 | this->writeExtension(fProgram.fSettings.fCaps->externalTextureExtensionString()); |
Brian Osman | 4b2f915 | 2018-04-17 11:19:57 -0400 | [diff] [blame] | 1277 | } |
Brian Osman | 061020e | 2018-04-17 14:22:15 -0400 | [diff] [blame] | 1278 | if (fProgram.fSettings.fCaps->secondExternalTextureExtensionString()) { |
Ethan Nicholas | 88f6d37 | 2018-07-27 10:03:46 -0400 | [diff] [blame] | 1279 | this->writeExtension( |
| 1280 | fProgram.fSettings.fCaps->secondExternalTextureExtensionString()); |
Brian Osman | 061020e | 2018-04-17 14:22:15 -0400 | [diff] [blame] | 1281 | } |
Brian Osman | 4b2f915 | 2018-04-17 11:19:57 -0400 | [diff] [blame] | 1282 | fFoundExternalSamplerDecl = true; |
| 1283 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1284 | if (!fFoundRectSamplerDecl && var.fVar->type() == *fContext.fSampler2DRect_Type) { |
Brian Salomon | 67529b2 | 2019-08-13 15:31:04 -0400 | [diff] [blame] | 1285 | fFoundRectSamplerDecl = true; |
| 1286 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1287 | } |
Ethan Nicholas | b4dc419 | 2017-06-02 10:16:28 -0400 | [diff] [blame] | 1288 | if (wroteType) { |
| 1289 | this->write(";"); |
| 1290 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | void GLSLCodeGenerator::writeStatement(const Statement& s) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1294 | switch (s.kind()) { |
| 1295 | case Statement::Kind::kBlock: |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1296 | this->writeBlock(s.as<Block>()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1297 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1298 | case Statement::Kind::kExpression: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1299 | this->writeExpression(*s.as<ExpressionStatement>().fExpression, kTopLevel_Precedence); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1300 | this->write(";"); |
| 1301 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1302 | case Statement::Kind::kReturn: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1303 | this->writeReturnStatement(s.as<ReturnStatement>()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1304 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1305 | case Statement::Kind::kVarDeclarations: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1306 | this->writeVarDeclarations(*s.as<VarDeclarationsStatement>().fDeclaration, false); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1307 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1308 | case Statement::Kind::kIf: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1309 | this->writeIfStatement(s.as<IfStatement>()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1310 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1311 | case Statement::Kind::kFor: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1312 | this->writeForStatement(s.as<ForStatement>()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1313 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1314 | case Statement::Kind::kWhile: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1315 | this->writeWhileStatement(s.as<WhileStatement>()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1316 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1317 | case Statement::Kind::kDo: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1318 | this->writeDoStatement(s.as<DoStatement>()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1319 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1320 | case Statement::Kind::kSwitch: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1321 | this->writeSwitchStatement(s.as<SwitchStatement>()); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1322 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1323 | case Statement::Kind::kBreak: |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1324 | this->write("break;"); |
| 1325 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1326 | case Statement::Kind::kContinue: |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1327 | this->write("continue;"); |
| 1328 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1329 | case Statement::Kind::kDiscard: |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1330 | this->write("discard;"); |
| 1331 | break; |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 1332 | case Statement::Kind::kInlineMarker: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1333 | case Statement::Kind::kNop: |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1334 | this->write(";"); |
| 1335 | break; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1336 | default: |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1337 | #ifdef SK_DEBUG |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1338 | ABORT("unsupported statement: %s", s.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1339 | #endif |
| 1340 | break; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1341 | } |
| 1342 | } |
| 1343 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1344 | void GLSLCodeGenerator::writeStatements(const std::vector<std::unique_ptr<Statement>>& statements) { |
| 1345 | for (const auto& s : statements) { |
| 1346 | if (!s->isEmpty()) { |
| 1347 | this->writeStatement(*s); |
| 1348 | this->writeLine(); |
| 1349 | } |
| 1350 | } |
| 1351 | } |
| 1352 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1353 | void GLSLCodeGenerator::writeBlock(const Block& b) { |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 1354 | if (b.fIsScope) { |
| 1355 | this->writeLine("{"); |
| 1356 | fIndentation++; |
| 1357 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1358 | this->writeStatements(b.fStatements); |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 1359 | if (b.fIsScope) { |
| 1360 | fIndentation--; |
| 1361 | this->write("}"); |
| 1362 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | void GLSLCodeGenerator::writeIfStatement(const IfStatement& stmt) { |
| 1366 | this->write("if ("); |
| 1367 | this->writeExpression(*stmt.fTest, kTopLevel_Precedence); |
| 1368 | this->write(") "); |
| 1369 | this->writeStatement(*stmt.fIfTrue); |
| 1370 | if (stmt.fIfFalse) { |
| 1371 | this->write(" else "); |
| 1372 | this->writeStatement(*stmt.fIfFalse); |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | void GLSLCodeGenerator::writeForStatement(const ForStatement& f) { |
| 1377 | this->write("for ("); |
Ethan Nicholas | b310fd5 | 2017-06-09 13:46:34 -0400 | [diff] [blame] | 1378 | if (f.fInitializer && !f.fInitializer->isEmpty()) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1379 | this->writeStatement(*f.fInitializer); |
| 1380 | } else { |
| 1381 | this->write("; "); |
| 1382 | } |
| 1383 | if (f.fTest) { |
Adrienne Walker | ee8295c | 2018-08-21 10:56:30 -0700 | [diff] [blame] | 1384 | if (fProgram.fSettings.fCaps->addAndTrueToLoopCondition()) { |
| 1385 | std::unique_ptr<Expression> and_true(new BinaryExpression( |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1386 | -1, f.fTest->clone(), Token::Kind::TK_LOGICALAND, |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 1387 | std::make_unique<BoolLiteral>(fContext, -1, true), |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1388 | fContext.fBool_Type.get())); |
Adrienne Walker | ee8295c | 2018-08-21 10:56:30 -0700 | [diff] [blame] | 1389 | this->writeExpression(*and_true, kTopLevel_Precedence); |
| 1390 | } else { |
| 1391 | this->writeExpression(*f.fTest, kTopLevel_Precedence); |
| 1392 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1393 | } |
| 1394 | this->write("; "); |
| 1395 | if (f.fNext) { |
| 1396 | this->writeExpression(*f.fNext, kTopLevel_Precedence); |
| 1397 | } |
| 1398 | this->write(") "); |
| 1399 | this->writeStatement(*f.fStatement); |
| 1400 | } |
| 1401 | |
| 1402 | void GLSLCodeGenerator::writeWhileStatement(const WhileStatement& w) { |
| 1403 | this->write("while ("); |
| 1404 | this->writeExpression(*w.fTest, kTopLevel_Precedence); |
| 1405 | this->write(") "); |
| 1406 | this->writeStatement(*w.fStatement); |
| 1407 | } |
| 1408 | |
| 1409 | void GLSLCodeGenerator::writeDoStatement(const DoStatement& d) { |
Adrienne Walker | 8b23ca6 | 2018-08-22 10:45:41 -0700 | [diff] [blame] | 1410 | if (!fProgram.fSettings.fCaps->rewriteDoWhileLoops()) { |
| 1411 | this->write("do "); |
| 1412 | this->writeStatement(*d.fStatement); |
| 1413 | this->write(" while ("); |
| 1414 | this->writeExpression(*d.fTest, kTopLevel_Precedence); |
| 1415 | this->write(");"); |
| 1416 | return; |
| 1417 | } |
| 1418 | |
| 1419 | // Otherwise, do the do while loop workaround, to rewrite loops of the form: |
| 1420 | // do { |
| 1421 | // CODE; |
| 1422 | // } while (CONDITION) |
| 1423 | // |
| 1424 | // to loops of the form |
| 1425 | // bool temp = false; |
| 1426 | // while (true) { |
| 1427 | // if (temp) { |
| 1428 | // if (!CONDITION) { |
| 1429 | // break; |
| 1430 | // } |
| 1431 | // } |
| 1432 | // temp = true; |
| 1433 | // CODE; |
| 1434 | // } |
| 1435 | String tmpVar = "_tmpLoopSeenOnce" + to_string(fVarCount++); |
| 1436 | this->write("bool "); |
| 1437 | this->write(tmpVar); |
| 1438 | this->writeLine(" = false;"); |
| 1439 | this->writeLine("while (true) {"); |
| 1440 | fIndentation++; |
| 1441 | this->write("if ("); |
| 1442 | this->write(tmpVar); |
| 1443 | this->writeLine(") {"); |
| 1444 | fIndentation++; |
| 1445 | this->write("if (!"); |
| 1446 | this->writeExpression(*d.fTest, kPrefix_Precedence); |
| 1447 | this->writeLine(") {"); |
| 1448 | fIndentation++; |
| 1449 | this->writeLine("break;"); |
| 1450 | fIndentation--; |
| 1451 | this->writeLine("}"); |
| 1452 | fIndentation--; |
| 1453 | this->writeLine("}"); |
| 1454 | this->write(tmpVar); |
| 1455 | this->writeLine(" = true;"); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1456 | this->writeStatement(*d.fStatement); |
Adrienne Walker | 8b23ca6 | 2018-08-22 10:45:41 -0700 | [diff] [blame] | 1457 | this->writeLine(); |
| 1458 | fIndentation--; |
| 1459 | this->write("}"); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1460 | } |
| 1461 | |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1462 | void GLSLCodeGenerator::writeSwitchStatement(const SwitchStatement& s) { |
| 1463 | this->write("switch ("); |
| 1464 | this->writeExpression(*s.fValue, kTopLevel_Precedence); |
| 1465 | this->writeLine(") {"); |
| 1466 | fIndentation++; |
| 1467 | for (const auto& c : s.fCases) { |
| 1468 | if (c->fValue) { |
| 1469 | this->write("case "); |
| 1470 | this->writeExpression(*c->fValue, kTopLevel_Precedence); |
| 1471 | this->writeLine(":"); |
| 1472 | } else { |
| 1473 | this->writeLine("default:"); |
| 1474 | } |
| 1475 | fIndentation++; |
| 1476 | for (const auto& stmt : c->fStatements) { |
| 1477 | this->writeStatement(*stmt); |
| 1478 | this->writeLine(); |
| 1479 | } |
| 1480 | fIndentation--; |
| 1481 | } |
| 1482 | fIndentation--; |
| 1483 | this->write("}"); |
| 1484 | } |
| 1485 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1486 | void GLSLCodeGenerator::writeReturnStatement(const ReturnStatement& r) { |
| 1487 | this->write("return"); |
| 1488 | if (r.fExpression) { |
| 1489 | this->write(" "); |
| 1490 | this->writeExpression(*r.fExpression, kTopLevel_Precedence); |
| 1491 | } |
| 1492 | this->write(";"); |
| 1493 | } |
| 1494 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1495 | void GLSLCodeGenerator::writeHeader() { |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1496 | this->write(fProgram.fSettings.fCaps->versionDeclString()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1497 | this->writeLine(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1498 | } |
| 1499 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1500 | void GLSLCodeGenerator::writeProgramElement(const ProgramElement& e) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1501 | switch (e.kind()) { |
| 1502 | case ProgramElement::Kind::kExtension: |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1503 | this->writeExtension(e.as<Extension>().fName); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1504 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1505 | case ProgramElement::Kind::kVar: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1506 | const VarDeclarations& decl = e.as<VarDeclarations>(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1507 | if (decl.fVars.size() > 0) { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1508 | int builtin = decl.fVars[0]->as<VarDeclaration>().fVar->fModifiers.fLayout.fBuiltin; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1509 | if (builtin == -1) { |
| 1510 | // normal var |
| 1511 | this->writeVarDeclarations(decl, true); |
| 1512 | this->writeLine(); |
| 1513 | } else if (builtin == SK_FRAGCOLOR_BUILTIN && |
Ethan Nicholas | a7ceb50 | 2019-01-11 10:31:48 -0500 | [diff] [blame] | 1514 | fProgram.fSettings.fCaps->mustDeclareFragmentShaderOutput() && |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1515 | decl.fVars[0]->as<VarDeclaration>().fVar->fWriteCount) { |
Brian Salomon | dc09213 | 2018-04-04 10:14:16 -0400 | [diff] [blame] | 1516 | if (fProgram.fSettings.fFragColorIsInOut) { |
| 1517 | this->write("inout "); |
| 1518 | } else { |
| 1519 | this->write("out "); |
| 1520 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1521 | if (usesPrecisionModifiers()) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1522 | this->write("mediump "); |
Mike Klein | 5ce3972 | 2017-06-27 22:52:03 +0000 | [diff] [blame] | 1523 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1524 | this->writeLine("vec4 sk_FragColor;"); |
Mike Klein | 5ce3972 | 2017-06-27 22:52:03 +0000 | [diff] [blame] | 1525 | } |
Mike Klein | 5ce3972 | 2017-06-27 22:52:03 +0000 | [diff] [blame] | 1526 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1527 | break; |
Mike Klein | 5ce3972 | 2017-06-27 22:52:03 +0000 | [diff] [blame] | 1528 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1529 | case ProgramElement::Kind::kInterfaceBlock: |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1530 | this->writeInterfaceBlock(e.as<InterfaceBlock>()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1531 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1532 | case ProgramElement::Kind::kFunction: |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1533 | this->writeFunction(e.as<FunctionDefinition>()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1534 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1535 | case ProgramElement::Kind::kModifiers: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1536 | const Modifiers& modifiers = e.as<ModifiersDeclaration>().fModifiers; |
Chris Dalton | f1b47bb | 2017-10-06 11:57:51 -0600 | [diff] [blame] | 1537 | if (!fFoundGSInvocations && modifiers.fLayout.fInvocations >= 0) { |
| 1538 | if (fProgram.fSettings.fCaps->gsInvocationsExtensionString()) { |
Ethan Nicholas | 88f6d37 | 2018-07-27 10:03:46 -0400 | [diff] [blame] | 1539 | this->writeExtension(fProgram.fSettings.fCaps->gsInvocationsExtensionString()); |
Chris Dalton | f1b47bb | 2017-10-06 11:57:51 -0600 | [diff] [blame] | 1540 | } |
| 1541 | fFoundGSInvocations = true; |
| 1542 | } |
| 1543 | this->writeModifiers(modifiers, true); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1544 | this->writeLine(";"); |
| 1545 | break; |
Chris Dalton | f1b47bb | 2017-10-06 11:57:51 -0600 | [diff] [blame] | 1546 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1547 | case ProgramElement::Kind::kEnum: |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1548 | break; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1549 | default: |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1550 | #ifdef SK_DEBUG |
| 1551 | printf("unsupported program element %s\n", e.description().c_str()); |
| 1552 | #endif |
| 1553 | SkASSERT(false); |
Ethan Nicholas | c070939 | 2017-06-27 11:20:22 -0400 | [diff] [blame] | 1554 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1555 | } |
| 1556 | |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 1557 | void GLSLCodeGenerator::writeInputVars() { |
| 1558 | if (fProgram.fInputs.fRTWidth) { |
| 1559 | const char* precision = usesPrecisionModifiers() ? "highp " : ""; |
| 1560 | fGlobals.writeText("uniform "); |
| 1561 | fGlobals.writeText(precision); |
| 1562 | fGlobals.writeText("float " SKSL_RTWIDTH_NAME ";\n"); |
| 1563 | } |
| 1564 | if (fProgram.fInputs.fRTHeight) { |
| 1565 | const char* precision = usesPrecisionModifiers() ? "highp " : ""; |
| 1566 | fGlobals.writeText("uniform "); |
| 1567 | fGlobals.writeText(precision); |
| 1568 | fGlobals.writeText("float " SKSL_RTHEIGHT_NAME ";\n"); |
| 1569 | } |
| 1570 | } |
| 1571 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1572 | bool GLSLCodeGenerator::generateCode() { |
John Stiles | fffe384 | 2020-09-02 15:44:18 -0400 | [diff] [blame] | 1573 | this->writeHeader(); |
Chris Dalton | 8fd7955 | 2018-01-11 00:46:14 -0500 | [diff] [blame] | 1574 | if (Program::kGeometry_Kind == fProgramKind && |
| 1575 | fProgram.fSettings.fCaps->geometryShaderExtensionString()) { |
Ethan Nicholas | 88f6d37 | 2018-07-27 10:03:46 -0400 | [diff] [blame] | 1576 | this->writeExtension(fProgram.fSettings.fCaps->geometryShaderExtensionString()); |
Chris Dalton | 8fd7955 | 2018-01-11 00:46:14 -0500 | [diff] [blame] | 1577 | } |
Ethan Nicholas | 88f6d37 | 2018-07-27 10:03:46 -0400 | [diff] [blame] | 1578 | OutputStream* rawOut = fOut; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1579 | StringStream body; |
| 1580 | fOut = &body; |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1581 | for (const auto& e : fProgram) { |
| 1582 | this->writeProgramElement(e); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1583 | } |
| 1584 | fOut = rawOut; |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 1585 | |
Ethan Nicholas | 88f6d37 | 2018-07-27 10:03:46 -0400 | [diff] [blame] | 1586 | write_stringstream(fExtensions, *rawOut); |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 1587 | this->writeInputVars(); |
Ethan Nicholas | 88f6d37 | 2018-07-27 10:03:46 -0400 | [diff] [blame] | 1588 | write_stringstream(fGlobals, *rawOut); |
Brian Osman | cc10d79 | 2018-07-20 13:09:45 -0400 | [diff] [blame] | 1589 | |
| 1590 | if (!fProgram.fSettings.fCaps->canUseFragCoord()) { |
| 1591 | Layout layout; |
| 1592 | switch (fProgram.fKind) { |
| 1593 | case Program::kVertex_Kind: { |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 1594 | Modifiers modifiers(layout, Modifiers::kOut_Flag); |
Brian Osman | cc10d79 | 2018-07-20 13:09:45 -0400 | [diff] [blame] | 1595 | this->writeModifiers(modifiers, true); |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 1596 | if (this->usesPrecisionModifiers()) { |
| 1597 | this->write("highp "); |
| 1598 | } |
Brian Osman | cc10d79 | 2018-07-20 13:09:45 -0400 | [diff] [blame] | 1599 | this->write("vec4 sk_FragCoord_Workaround;\n"); |
| 1600 | break; |
| 1601 | } |
| 1602 | case Program::kFragment_Kind: { |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 1603 | Modifiers modifiers(layout, Modifiers::kIn_Flag); |
Brian Osman | cc10d79 | 2018-07-20 13:09:45 -0400 | [diff] [blame] | 1604 | this->writeModifiers(modifiers, true); |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 1605 | if (this->usesPrecisionModifiers()) { |
| 1606 | this->write("highp "); |
| 1607 | } |
Brian Osman | cc10d79 | 2018-07-20 13:09:45 -0400 | [diff] [blame] | 1608 | this->write("vec4 sk_FragCoord_Workaround;\n"); |
| 1609 | break; |
| 1610 | } |
| 1611 | default: |
| 1612 | break; |
| 1613 | } |
| 1614 | } |
| 1615 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1616 | if (this->usesPrecisionModifiers()) { |
| 1617 | this->writeLine("precision mediump float;"); |
Brian Salomon | 67529b2 | 2019-08-13 15:31:04 -0400 | [diff] [blame] | 1618 | this->writeLine("precision mediump sampler2D;"); |
Brian Salomon | 5a5f3e8 | 2019-08-16 15:05:40 -0400 | [diff] [blame] | 1619 | if (fFoundExternalSamplerDecl && |
| 1620 | !fProgram.fSettings.fCaps->noDefaultPrecisionForExternalSamplers()) { |
Brian Salomon | 67529b2 | 2019-08-13 15:31:04 -0400 | [diff] [blame] | 1621 | this->writeLine("precision mediump samplerExternalOES;"); |
| 1622 | } |
| 1623 | if (fFoundRectSamplerDecl) { |
| 1624 | this->writeLine("precision mediump sampler2DRect;"); |
| 1625 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1626 | } |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 1627 | write_stringstream(fExtraFunctions, *rawOut); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1628 | write_stringstream(body, *rawOut); |
Brian Osman | 3000d6b | 2020-07-31 15:57:28 -0400 | [diff] [blame] | 1629 | return 0 == fErrors.errorCount(); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1630 | } |
| 1631 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 1632 | } // namespace SkSL |