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