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