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