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