blob: ffa85bd59dca38ee82f4e3ab684a7d9453aebb65 [file] [log] [blame]
ethannicholasf789b382016-08-03 12:43:36 -07001/*
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 Daniel64773e62016-11-22 09:44:03 -05007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/SkSLGLSLCodeGenerator.h"
ethannicholasf789b382016-08-03 12:43:36 -07009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/sksl/SkSLCompiler.h"
11#include "src/sksl/ir/SkSLExpressionStatement.h"
12#include "src/sksl/ir/SkSLExtension.h"
13#include "src/sksl/ir/SkSLIndexExpression.h"
14#include "src/sksl/ir/SkSLModifiersDeclaration.h"
15#include "src/sksl/ir/SkSLNop.h"
16#include "src/sksl/ir/SkSLVariableReference.h"
ethannicholasf789b382016-08-03 12:43:36 -070017
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -040018#ifndef SKSL_STANDALONE
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/private/SkOnce.h"
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -040020#endif
21
ethannicholasf789b382016-08-03 12:43:36 -070022namespace SkSL {
23
24void 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 Nicholas9e1138d2016-11-21 10:39:35 -050030 fOut->writeText(" ");
ethannicholasf789b382016-08-03 12:43:36 -070031 }
32 }
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050033 fOut->writeText(s);
ethannicholasf789b382016-08-03 12:43:36 -070034 fAtLineStart = false;
35}
36
37void GLSLCodeGenerator::writeLine(const char* s) {
38 this->write(s);
Ethan Nicholas762466e2017-06-29 10:03:38 -040039 fOut->writeText(fLineEnding);
ethannicholasf789b382016-08-03 12:43:36 -070040 fAtLineStart = true;
41}
42
Ethan Nicholas0df1b042017-03-31 13:56:23 -040043void GLSLCodeGenerator::write(const String& s) {
ethannicholasf789b382016-08-03 12:43:36 -070044 this->write(s.c_str());
45}
46
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070047void 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 Nicholas0df1b042017-03-31 13:56:23 -040060void GLSLCodeGenerator::writeLine(const String& s) {
ethannicholasf789b382016-08-03 12:43:36 -070061 this->writeLine(s.c_str());
62}
63
64void GLSLCodeGenerator::writeLine() {
65 this->writeLine("");
66}
67
Ethan Nicholas88f6d372018-07-27 10:03:46 -040068void GLSLCodeGenerator::writeExtension(const String& name) {
69 this->writeExtension(name, true);
70}
71
72void 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");
ethannicholasf789b382016-08-03 12:43:36 -070076}
77
Ethan Nicholasf7b88202017-09-18 14:10:39 -040078bool GLSLCodeGenerator::usesPrecisionModifiers() const {
79 return fProgram.fSettings.fCaps->usesPrecisionModifiers();
80}
81
82String GLSLCodeGenerator::getTypeName(const Type& type) {
83 switch (type.kind()) {
84 case Type::kVector_Kind: {
85 Type component = type.componentType();
86 String result;
87 if (component == *fContext.fFloat_Type || component == *fContext.fHalf_Type) {
88 result = "vec";
89 }
90 else if (component == *fContext.fDouble_Type) {
91 result = "dvec";
92 }
Ethan Nicholase1f55022019-02-05 17:17:40 -050093 else if (component.isSigned()) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -040094 result = "ivec";
95 }
Ethan Nicholase1f55022019-02-05 17:17:40 -050096 else if (component.isUnsigned()) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -040097 result = "uvec";
98 }
99 else if (component == *fContext.fBool_Type) {
100 result = "bvec";
101 }
102 else {
103 ABORT("unsupported vector type");
104 }
105 result += to_string(type.columns());
106 return result;
107 }
108 case Type::kMatrix_Kind: {
109 String result;
110 Type component = type.componentType();
111 if (component == *fContext.fFloat_Type || component == *fContext.fHalf_Type) {
112 result = "mat";
113 }
114 else if (component == *fContext.fDouble_Type) {
115 result = "dmat";
116 }
117 else {
118 ABORT("unsupported matrix type");
119 }
120 result += to_string(type.columns());
121 if (type.columns() != type.rows()) {
122 result += "x";
123 result += to_string(type.rows());
124 }
125 return result;
126 }
127 case Type::kArray_Kind: {
128 String result = this->getTypeName(type.componentType()) + "[";
129 if (type.columns() != -1) {
130 result += to_string(type.columns());
131 }
132 result += "]";
133 return result;
134 }
135 case Type::kScalar_Kind: {
136 if (type == *fContext.fHalf_Type) {
137 return "float";
138 }
139 else if (type == *fContext.fShort_Type) {
140 return "int";
141 }
142 else if (type == *fContext.fUShort_Type) {
143 return "uint";
144 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400145 else if (type == *fContext.fByte_Type) {
146 return "int";
147 }
148 else if (type == *fContext.fUByte_Type) {
149 return "uint";
150 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400151 else {
152 return type.name();
153 }
154 break;
155 }
156 default:
157 return type.name();
158 }
159}
160
ethannicholasf789b382016-08-03 12:43:36 -0700161void GLSLCodeGenerator::writeType(const Type& type) {
162 if (type.kind() == Type::kStruct_Kind) {
163 for (const Type* search : fWrittenStructs) {
164 if (*search == type) {
165 // already written
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700166 this->write(type.fName);
ethannicholasf789b382016-08-03 12:43:36 -0700167 return;
168 }
169 }
170 fWrittenStructs.push_back(&type);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700171 this->write("struct ");
172 this->write(type.fName);
173 this->writeLine(" {");
ethannicholasf789b382016-08-03 12:43:36 -0700174 fIndentation++;
175 for (const auto& f : type.fields()) {
ethannicholas5961bc92016-10-12 06:39:56 -0700176 this->writeModifiers(f.fModifiers, false);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400177 this->writeTypePrecision(*f.fType);
ethannicholasf789b382016-08-03 12:43:36 -0700178 // sizes (which must be static in structs) are part of the type name here
ethannicholas0730be72016-09-01 07:59:02 -0700179 this->writeType(*f.fType);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700180 this->write(" ");
181 this->write(f.fName);
182 this->writeLine(";");
ethannicholasf789b382016-08-03 12:43:36 -0700183 }
184 fIndentation--;
Ethan Nicholas19671772016-11-28 16:30:17 -0500185 this->write("}");
ethannicholasf789b382016-08-03 12:43:36 -0700186 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400187 this->write(this->getTypeName(type));
ethannicholasf789b382016-08-03 12:43:36 -0700188 }
189}
190
191void GLSLCodeGenerator::writeExpression(const Expression& expr, Precedence parentPrecedence) {
192 switch (expr.fKind) {
193 case Expression::kBinary_Kind:
194 this->writeBinaryExpression((BinaryExpression&) expr, parentPrecedence);
195 break;
196 case Expression::kBoolLiteral_Kind:
197 this->writeBoolLiteral((BoolLiteral&) expr);
198 break;
199 case Expression::kConstructor_Kind:
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400200 this->writeConstructor((Constructor&) expr, parentPrecedence);
ethannicholasf789b382016-08-03 12:43:36 -0700201 break;
202 case Expression::kIntLiteral_Kind:
203 this->writeIntLiteral((IntLiteral&) expr);
204 break;
205 case Expression::kFieldAccess_Kind:
206 this->writeFieldAccess(((FieldAccess&) expr));
207 break;
208 case Expression::kFloatLiteral_Kind:
209 this->writeFloatLiteral(((FloatLiteral&) expr));
210 break;
211 case Expression::kFunctionCall_Kind:
212 this->writeFunctionCall((FunctionCall&) expr);
213 break;
214 case Expression::kPrefix_Kind:
215 this->writePrefixExpression((PrefixExpression&) expr, parentPrecedence);
216 break;
217 case Expression::kPostfix_Kind:
218 this->writePostfixExpression((PostfixExpression&) expr, parentPrecedence);
219 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400220 case Expression::kSetting_Kind:
221 this->writeSetting((Setting&) expr);
222 break;
ethannicholasf789b382016-08-03 12:43:36 -0700223 case Expression::kSwizzle_Kind:
224 this->writeSwizzle((Swizzle&) expr);
225 break;
226 case Expression::kVariableReference_Kind:
227 this->writeVariableReference((VariableReference&) expr);
228 break;
229 case Expression::kTernary_Kind:
230 this->writeTernaryExpression((TernaryExpression&) expr, parentPrecedence);
231 break;
232 case Expression::kIndex_Kind:
233 this->writeIndexExpression((IndexExpression&) expr);
234 break;
235 default:
236 ABORT("unsupported expression: %s", expr.description().c_str());
237 }
238}
239
ethannicholas5961bc92016-10-12 06:39:56 -0700240static bool is_abs(Expression& expr) {
241 if (expr.fKind != Expression::kFunctionCall_Kind) {
242 return false;
243 }
244 return ((FunctionCall&) expr).fFunction.fName == "abs";
245}
246
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500247// turns min(abs(x), y) into ((tmpVar1 = abs(x)) < (tmpVar2 = y) ? tmpVar1 : tmpVar2) to avoid a
ethannicholas5961bc92016-10-12 06:39:56 -0700248// Tegra3 compiler bug.
249void GLSLCodeGenerator::writeMinAbsHack(Expression& absExpr, Expression& otherExpr) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400250 SkASSERT(!fProgram.fSettings.fCaps->canUseMinAndAbsTogether());
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400251 String tmpVar1 = "minAbsHackVar" + to_string(fVarCount++);
252 String tmpVar2 = "minAbsHackVar" + to_string(fVarCount++);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400253 this->fFunctionHeader += String(" ") + this->getTypePrecision(absExpr.fType) +
254 this->getTypeName(absExpr.fType) + " " + tmpVar1 + ";\n";
255 this->fFunctionHeader += String(" ") + this->getTypePrecision(otherExpr.fType) +
256 this->getTypeName(otherExpr.fType) + " " + tmpVar2 + ";\n";
ethannicholas5961bc92016-10-12 06:39:56 -0700257 this->write("((" + tmpVar1 + " = ");
258 this->writeExpression(absExpr, kTopLevel_Precedence);
259 this->write(") < (" + tmpVar2 + " = ");
260 this->writeExpression(otherExpr, kAssignment_Precedence);
261 this->write(") ? " + tmpVar1 + " : " + tmpVar2 + ")");
262}
263
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500264void GLSLCodeGenerator::writeInverseSqrtHack(const Expression& x) {
265 this->write("(1.0 / sqrt(");
266 this->writeExpression(x, kTopLevel_Precedence);
267 this->write("))");
268}
269
270void GLSLCodeGenerator::writeDeterminantHack(const Expression& mat) {
271 String name;
272 if (mat.fType == *fContext.fFloat2x2_Type || mat.fType == *fContext.fHalf2x2_Type) {
273 name = "_determinant2";
274 if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
275 fWrittenIntrinsics.insert(name);
276 fExtraFunctions.writeText((
277 "float " + name + "(mat2 m) {"
278 " return m[0][0] * m[1][1] - m[0][1] * m[1][0];"
279 "}"
280 ).c_str());
281 }
282 }
283 else if (mat.fType == *fContext.fFloat3x3_Type || mat.fType == *fContext.fHalf3x3_Type) {
284 name = "_determinant3";
285 if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
286 fWrittenIntrinsics.insert(name);
287 fExtraFunctions.writeText((
288 "float " + name + "(mat3 m) {"
289 " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];"
290 " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];"
291 " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];"
292 " float b01 = a22 * a11 - a12 * a21;"
293 " float b11 = -a22 * a10 + a12 * a20;"
294 " float b21 = a21 * a10 - a11 * a20;"
295 " return a00 * b01 + a01 * b11 + a02 * b21;"
296 "}"
297 ).c_str());
298 }
299 }
300 else if (mat.fType == *fContext.fFloat4x4_Type || mat.fType == *fContext.fHalf4x4_Type) {
301 name = "_determinant3";
302 if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
303 fWrittenIntrinsics.insert(name);
304 fExtraFunctions.writeText((
305 "mat4 " + name + "(mat4 m) {"
306 " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3];"
307 " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3];"
308 " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2], a23 = m[2][3];"
309 " float a30 = m[3][0], a31 = m[3][1], a32 = m[3][2], a33 = m[3][3];"
310 " float b00 = a00 * a11 - a01 * a10;"
311 " float b01 = a00 * a12 - a02 * a10;"
312 " float b02 = a00 * a13 - a03 * a10;"
313 " float b03 = a01 * a12 - a02 * a11;"
314 " float b04 = a01 * a13 - a03 * a11;"
315 " float b05 = a02 * a13 - a03 * a12;"
316 " float b06 = a20 * a31 - a21 * a30;"
317 " float b07 = a20 * a32 - a22 * a30;"
318 " float b08 = a20 * a33 - a23 * a30;"
319 " float b09 = a21 * a32 - a22 * a31;"
320 " float b10 = a21 * a33 - a23 * a31;"
321 " float b11 = a22 * a33 - a23 * a32;"
322 " return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;"
323 "}"
324 ).c_str());
325 }
326 }
327 else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400328 SkASSERT(false);
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500329 }
330 this->write(name + "(");
331 this->writeExpression(mat, kTopLevel_Precedence);
332 this->write(")");
333}
334
335void GLSLCodeGenerator::writeInverseHack(const Expression& mat) {
336 String name;
337 if (mat.fType == *fContext.fFloat2x2_Type || mat.fType == *fContext.fHalf2x2_Type) {
338 name = "_inverse2";
339 if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
340 fWrittenIntrinsics.insert(name);
341 fExtraFunctions.writeText((
342 "mat2 " + name + "(mat2 m) {"
343 " return mat2(m[1][1], -m[0][1], -m[1][0], m[0][0]) / "
344 "(m[0][0] * m[1][1] - m[0][1] * m[1][0]);"
345 "}"
346 ).c_str());
347 }
348 }
349 else if (mat.fType == *fContext.fFloat3x3_Type || mat.fType == *fContext.fHalf3x3_Type) {
350 name = "_inverse3";
351 if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
352 fWrittenIntrinsics.insert(name);
353 fExtraFunctions.writeText((
354 "mat3 " + name + "(mat3 m) {"
355 " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];"
356 " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];"
357 " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];"
358 " float b01 = a22 * a11 - a12 * a21;"
359 " float b11 = -a22 * a10 + a12 * a20;"
360 " float b21 = a21 * a10 - a11 * a20;"
361 " float det = a00 * b01 + a01 * b11 + a02 * b21;"
362 " return mat3(b01, (-a22 * a01 + a02 * a21), (a12 * a01 - a02 * a11),"
363 " b11, (a22 * a00 - a02 * a20), (-a12 * a00 + a02 * a10),"
364 " b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10)) / det;"
365 "}"
366 ).c_str());
367 }
368 }
369 else if (mat.fType == *fContext.fFloat4x4_Type || mat.fType == *fContext.fHalf4x4_Type) {
370 name = "_inverse4";
371 if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
372 fWrittenIntrinsics.insert(name);
373 fExtraFunctions.writeText((
374 "mat4 " + name + "(mat4 m) {"
375 " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3];"
376 " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3];"
377 " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2], a23 = m[2][3];"
378 " float a30 = m[3][0], a31 = m[3][1], a32 = m[3][2], a33 = m[3][3];"
379 " float b00 = a00 * a11 - a01 * a10;"
380 " float b01 = a00 * a12 - a02 * a10;"
381 " float b02 = a00 * a13 - a03 * a10;"
382 " float b03 = a01 * a12 - a02 * a11;"
383 " float b04 = a01 * a13 - a03 * a11;"
384 " float b05 = a02 * a13 - a03 * a12;"
385 " float b06 = a20 * a31 - a21 * a30;"
386 " float b07 = a20 * a32 - a22 * a30;"
387 " float b08 = a20 * a33 - a23 * a30;"
388 " float b09 = a21 * a32 - a22 * a31;"
389 " float b10 = a21 * a33 - a23 * a31;"
390 " float b11 = a22 * a33 - a23 * a32;"
391 " float det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - "
392 " b04 * b07 + b05 * b06;"
393 " return mat4("
394 " a11 * b11 - a12 * b10 + a13 * b09,"
395 " a02 * b10 - a01 * b11 - a03 * b09,"
396 " a31 * b05 - a32 * b04 + a33 * b03,"
397 " a22 * b04 - a21 * b05 - a23 * b03,"
398 " a12 * b08 - a10 * b11 - a13 * b07,"
399 " a00 * b11 - a02 * b08 + a03 * b07,"
400 " a32 * b02 - a30 * b05 - a33 * b01,"
401 " a20 * b05 - a22 * b02 + a23 * b01,"
402 " a10 * b10 - a11 * b08 + a13 * b06,"
403 " a01 * b08 - a00 * b10 - a03 * b06,"
404 " a30 * b04 - a31 * b02 + a33 * b00,"
405 " a21 * b02 - a20 * b04 - a23 * b00,"
406 " a11 * b07 - a10 * b09 - a12 * b06,"
407 " a00 * b09 - a01 * b07 + a02 * b06,"
408 " a31 * b01 - a30 * b03 - a32 * b00,"
409 " a20 * b03 - a21 * b01 + a22 * b00) / det;"
410 "}"
411 ).c_str());
412 }
413 }
414 else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400415 SkASSERT(false);
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500416 }
417 this->write(name + "(");
418 this->writeExpression(mat, kTopLevel_Precedence);
419 this->write(")");
420}
421
422void GLSLCodeGenerator::writeTransposeHack(const Expression& mat) {
423 String name = "transpose" + to_string(mat.fType.columns()) + to_string(mat.fType.rows());
424 if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
425 fWrittenIntrinsics.insert(name);
426 String type = this->getTypeName(mat.fType);
427 const Type& base = mat.fType.componentType();
428 String transposed = this->getTypeName(base.toCompound(fContext,
429 mat.fType.rows(),
430 mat.fType.columns()));
431 fExtraFunctions.writeText((transposed + " " + name + "(" + type + " m) {\nreturn " +
432 transposed + "(").c_str());
433 const char* separator = "";
434 for (int row = 0; row < mat.fType.rows(); ++row) {
435 for (int column = 0; column < mat.fType.columns(); ++column) {
436 fExtraFunctions.writeText(separator);
437 fExtraFunctions.writeText(("m[" + to_string(column) + "][" + to_string(row) +
438 "]").c_str());
439 separator = ", ";
440 }
441 }
442 fExtraFunctions.writeText("); }");
443 }
444 this->write(name + "(");
445 this->writeExpression(mat, kTopLevel_Precedence);
446 this->write(")");
447}
448
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400449std::unordered_map<StringFragment, GLSLCodeGenerator::FunctionClass>*
450 GLSLCodeGenerator::fFunctionClasses = nullptr;
451
ethannicholasf789b382016-08-03 12:43:36 -0700452void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) {
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400453#ifdef SKSL_STANDALONE
454 if (!fFunctionClasses) {
455#else
456 static SkOnce once;
457 once([] {
458#endif
459 fFunctionClasses = new std::unordered_map<StringFragment, FunctionClass>();
Adrienne Walker92b161f2018-08-22 10:41:52 -0700460 (*fFunctionClasses)["abs"] = FunctionClass::kAbs;
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400461 (*fFunctionClasses)["atan"] = FunctionClass::kAtan;
462 (*fFunctionClasses)["determinant"] = FunctionClass::kDeterminant;
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700463 (*fFunctionClasses)["dFdx"] = FunctionClass::kDFdx;
464 (*fFunctionClasses)["dFdy"] = FunctionClass::kDFdy;
465 (*fFunctionClasses)["fwidth"] = FunctionClass::kFwidth;
Chris Daltona7086182018-11-16 09:33:43 -0500466 (*fFunctionClasses)["fma"] = FunctionClass::kFMA;
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400467 (*fFunctionClasses)["fract"] = FunctionClass::kFract;
468 (*fFunctionClasses)["inverse"] = FunctionClass::kInverse;
469 (*fFunctionClasses)["inverseSqrt"] = FunctionClass::kInverseSqrt;
470 (*fFunctionClasses)["min"] = FunctionClass::kMin;
Adrienne Walker2f4c09b2018-08-22 16:04:57 -0700471 (*fFunctionClasses)["pow"] = FunctionClass::kPow;
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400472 (*fFunctionClasses)["saturate"] = FunctionClass::kSaturate;
Ethan Nicholas13863662019-07-29 13:05:15 -0400473 (*fFunctionClasses)["sample"] = FunctionClass::kTexture;
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400474 (*fFunctionClasses)["transpose"] = FunctionClass::kTranspose;
ethannicholas5961bc92016-10-12 06:39:56 -0700475 }
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400476#ifndef SKSL_STANDALONE
477 );
478#endif
479 const auto found = c.fFunction.fBuiltin ? fFunctionClasses->find(c.fFunction.fName) :
480 fFunctionClasses->end();
Brian Osman8a83ca42018-02-12 14:32:17 -0500481 bool isTextureFunctionWithBias = false;
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400482 bool nameWritten = false;
483 if (found != fFunctionClasses->end()) {
484 switch (found->second) {
Adrienne Walker92b161f2018-08-22 10:41:52 -0700485 case FunctionClass::kAbs: {
486 if (!fProgram.fSettings.fCaps->emulateAbsIntFunction())
487 break;
488 SkASSERT(c.fArguments.size() == 1);
489 if (c.fArguments[0]->fType != *fContext.fInt_Type)
490 break;
491 // abs(int) on Intel OSX is incorrect, so emulate it:
492 String name = "_absemulation";
493 this->write(name);
494 nameWritten = true;
495 if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
496 fWrittenIntrinsics.insert(name);
497 fExtraFunctions.writeText((
498 "int " + name + "(int x) {\n"
499 " return x * sign(x);\n"
500 "}\n"
501 ).c_str());
502 }
503 break;
504 }
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400505 case FunctionClass::kAtan:
506 if (fProgram.fSettings.fCaps->mustForceNegatedAtanParamToFloat() &&
507 c.fArguments.size() == 2 &&
508 c.fArguments[1]->fKind == Expression::kPrefix_Kind) {
509 const PrefixExpression& p = (PrefixExpression&) *c.fArguments[1];
510 if (p.fOperator == Token::MINUS) {
511 this->write("atan(");
512 this->writeExpression(*c.fArguments[0], kSequence_Precedence);
513 this->write(", -1.0 * ");
514 this->writeExpression(*p.fOperand, kMultiplicative_Precedence);
515 this->write(")");
516 return;
517 }
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500518 }
519 break;
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700520 case FunctionClass::kDFdy:
521 if (fProgram.fSettings.fFlipY) {
522 // Flipping Y also negates the Y derivatives.
523 this->write("-dFdy");
524 nameWritten = true;
525 }
526 // fallthru
527 case FunctionClass::kDFdx:
528 case FunctionClass::kFwidth:
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400529 if (!fFoundDerivatives &&
530 fProgram.fSettings.fCaps->shaderDerivativeExtensionString()) {
531 SkASSERT(fProgram.fSettings.fCaps->shaderDerivativeSupport());
532 this->writeExtension(fProgram.fSettings.fCaps->shaderDerivativeExtensionString());
533 fFoundDerivatives = true;
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500534 }
535 break;
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400536 case FunctionClass::kDeterminant:
537 if (fProgram.fSettings.fCaps->generation() < k150_GrGLSLGeneration) {
538 SkASSERT(c.fArguments.size() == 1);
539 this->writeDeterminantHack(*c.fArguments[0]);
540 return;
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500541 }
542 break;
Chris Daltona7086182018-11-16 09:33:43 -0500543 case FunctionClass::kFMA:
544 if (!fProgram.fSettings.fCaps->builtinFMASupport()) {
545 SkASSERT(c.fArguments.size() == 3);
546 this->write("((");
547 this->writeExpression(*c.fArguments[0], kSequence_Precedence);
548 this->write(") * (");
549 this->writeExpression(*c.fArguments[1], kSequence_Precedence);
550 this->write(") + (");
551 this->writeExpression(*c.fArguments[2], kSequence_Precedence);
552 this->write("))");
553 return;
554 }
555 break;
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400556 case FunctionClass::kFract:
557 if (!fProgram.fSettings.fCaps->canUseFractForNegativeValues()) {
558 SkASSERT(c.fArguments.size() == 1);
559 this->write("(0.5 - sign(");
560 this->writeExpression(*c.fArguments[0], kSequence_Precedence);
561 this->write(") * (0.5 - fract(abs(");
562 this->writeExpression(*c.fArguments[0], kSequence_Precedence);
563 this->write("))))");
564 return;
565 }
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500566 break;
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400567 case FunctionClass::kInverse:
568 if (fProgram.fSettings.fCaps->generation() < k140_GrGLSLGeneration) {
569 SkASSERT(c.fArguments.size() == 1);
570 this->writeInverseHack(*c.fArguments[0]);
571 return;
572 }
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500573 break;
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400574 case FunctionClass::kInverseSqrt:
575 if (fProgram.fSettings.fCaps->generation() < k130_GrGLSLGeneration) {
576 SkASSERT(c.fArguments.size() == 1);
577 this->writeInverseSqrtHack(*c.fArguments[0]);
578 return;
579 }
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500580 break;
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400581 case FunctionClass::kMin:
582 if (!fProgram.fSettings.fCaps->canUseMinAndAbsTogether()) {
583 SkASSERT(c.fArguments.size() == 2);
584 if (is_abs(*c.fArguments[0])) {
585 this->writeMinAbsHack(*c.fArguments[0], *c.fArguments[1]);
586 return;
587 }
588 if (is_abs(*c.fArguments[1])) {
589 // note that this violates the GLSL left-to-right evaluation semantics.
590 // I doubt it will ever end up mattering, but it's worth calling out.
591 this->writeMinAbsHack(*c.fArguments[1], *c.fArguments[0]);
592 return;
593 }
594 }
595 break;
Adrienne Walker2f4c09b2018-08-22 16:04:57 -0700596 case FunctionClass::kPow:
597 if (!fProgram.fSettings.fCaps->removePowWithConstantExponent()) {
598 break;
599 }
600 // pow(x, y) on some NVIDIA drivers causes crashes if y is a
601 // constant. It's hard to tell what constitutes "constant" here
602 // so just replace in all cases.
603
604 // Change pow(x, y) into exp2(y * log2(x))
605 this->write("exp2(");
606 this->writeExpression(*c.fArguments[1], kMultiplicative_Precedence);
607 this->write(" * log2(");
608 this->writeExpression(*c.fArguments[0], kSequence_Precedence);
609 this->write("))");
610 return;
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400611 case FunctionClass::kSaturate:
612 SkASSERT(c.fArguments.size() == 1);
613 this->write("clamp(");
614 this->writeExpression(*c.fArguments[0], kSequence_Precedence);
615 this->write(", 0.0, 1.0)");
616 return;
617 case FunctionClass::kTexture: {
618 const char* dim = "";
619 bool proj = false;
620 switch (c.fArguments[0]->fType.dimensions()) {
621 case SpvDim1D:
622 dim = "1D";
623 isTextureFunctionWithBias = true;
624 if (c.fArguments[1]->fType == *fContext.fFloat_Type) {
625 proj = false;
626 } else {
627 SkASSERT(c.fArguments[1]->fType == *fContext.fFloat2_Type);
628 proj = true;
629 }
630 break;
631 case SpvDim2D:
632 dim = "2D";
633 if (c.fArguments[0]->fType != *fContext.fSamplerExternalOES_Type) {
634 isTextureFunctionWithBias = true;
635 }
636 if (c.fArguments[1]->fType == *fContext.fFloat2_Type) {
637 proj = false;
638 } else {
639 SkASSERT(c.fArguments[1]->fType == *fContext.fFloat3_Type);
640 proj = true;
641 }
642 break;
643 case SpvDim3D:
644 dim = "3D";
645 isTextureFunctionWithBias = true;
646 if (c.fArguments[1]->fType == *fContext.fFloat3_Type) {
647 proj = false;
648 } else {
649 SkASSERT(c.fArguments[1]->fType == *fContext.fFloat4_Type);
650 proj = true;
651 }
652 break;
653 case SpvDimCube:
654 dim = "Cube";
655 isTextureFunctionWithBias = true;
656 proj = false;
657 break;
658 case SpvDimRect:
Khushal Sagar2cb13152019-09-11 23:17:26 +0000659 dim = "2DRect";
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400660 proj = false;
661 break;
662 case SpvDimBuffer:
663 SkASSERT(false); // doesn't exist
664 dim = "Buffer";
665 proj = false;
666 break;
667 case SpvDimSubpassData:
668 SkASSERT(false); // doesn't exist
669 dim = "SubpassData";
670 proj = false;
671 break;
672 }
Ethan Nicholas13863662019-07-29 13:05:15 -0400673 if (fTextureFunctionOverride != "") {
674 this->write(fTextureFunctionOverride.c_str());
675 } else {
676 this->write("texture");
677 if (fProgram.fSettings.fCaps->generation() < k130_GrGLSLGeneration) {
678 this->write(dim);
679 }
680 if (proj) {
681 this->write("Proj");
682 }
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400683 }
684 nameWritten = true;
685 break;
686 }
687 case FunctionClass::kTranspose:
688 if (fProgram.fSettings.fCaps->generation() < k130_GrGLSLGeneration) {
689 SkASSERT(c.fArguments.size() == 1);
690 this->writeTransposeHack(*c.fArguments[0]);
691 return;
692 }
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500693 break;
694 }
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400695 }
696 if (!nameWritten) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500697 this->write(c.fFunction.fName);
698 }
699 this->write("(");
ethannicholasf789b382016-08-03 12:43:36 -0700700 const char* separator = "";
701 for (const auto& arg : c.fArguments) {
702 this->write(separator);
703 separator = ", ";
704 this->writeExpression(*arg, kSequence_Precedence);
705 }
Brian Osman8a83ca42018-02-12 14:32:17 -0500706 if (fProgram.fSettings.fSharpenTextures && isTextureFunctionWithBias) {
707 this->write(", -0.5");
708 }
ethannicholasf789b382016-08-03 12:43:36 -0700709 this->write(")");
710}
711
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400712void GLSLCodeGenerator::writeConstructor(const Constructor& c, Precedence parentPrecedence) {
713 if (c.fArguments.size() == 1 &&
Ethan Nicholase1f55022019-02-05 17:17:40 -0500714 (this->getTypeName(c.fType) == this->getTypeName(c.fArguments[0]->fType) ||
715 (c.fType.kind() == Type::kScalar_Kind &&
716 c.fArguments[0]->fType == *fContext.fFloatLiteral_Type))) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400717 // in cases like half(float), they're different types as far as SkSL is concerned but the
718 // same type as far as GLSL is concerned. We avoid a redundant float(float) by just writing
719 // out the inner expression here.
720 this->writeExpression(*c.fArguments[0], parentPrecedence);
721 return;
722 }
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400723 this->writeType(c.fType);
724 this->write("(");
ethannicholasf789b382016-08-03 12:43:36 -0700725 const char* separator = "";
726 for (const auto& arg : c.fArguments) {
727 this->write(separator);
728 separator = ", ";
729 this->writeExpression(*arg, kSequence_Precedence);
730 }
731 this->write(")");
732}
733
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500734void GLSLCodeGenerator::writeFragCoord() {
Brian Osmancd3261a2018-01-16 13:52:29 +0000735 if (!fProgram.fSettings.fCaps->canUseFragCoord()) {
Brian Salomondba65f92018-01-22 08:43:38 -0500736 if (!fSetupFragCoordWorkaround) {
737 const char* precision = usesPrecisionModifiers() ? "highp " : "";
738 fFunctionHeader += precision;
739 fFunctionHeader += " float sk_FragCoord_InvW = 1. / sk_FragCoord_Workaround.w;\n";
740 fFunctionHeader += precision;
741 fFunctionHeader += " vec4 sk_FragCoord_Resolved = "
742 "vec4(sk_FragCoord_Workaround.xyz * sk_FragCoord_InvW, sk_FragCoord_InvW);\n";
743 // Ensure that we get exact .5 values for x and y.
744 fFunctionHeader += " sk_FragCoord_Resolved.xy = floor(sk_FragCoord_Resolved.xy) + "
745 "vec2(.5);\n";
746 fSetupFragCoordWorkaround = true;
747 }
748 this->write("sk_FragCoord_Resolved");
Brian Osmancd3261a2018-01-16 13:52:29 +0000749 return;
750 }
751
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500752 // We only declare "gl_FragCoord" when we're in the case where we want to use layout qualifiers
753 // to reverse y. Otherwise it isn't necessary and whether the "in" qualifier appears in the
754 // declaration varies in earlier GLSL specs. So it is simpler to omit it.
755 if (!fProgram.fSettings.fFlipY) {
756 this->write("gl_FragCoord");
757 } else if (const char* extension =
Ethan Nicholascd700e92018-08-24 16:43:57 -0400758 fProgram.fSettings.fCaps->fragCoordConventionsExtensionString()) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500759 if (!fSetupFragPositionGlobal) {
760 if (fProgram.fSettings.fCaps->generation() < k150_GrGLSLGeneration) {
Ethan Nicholas88f6d372018-07-27 10:03:46 -0400761 this->writeExtension(extension);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500762 }
Ethan Nicholas88f6d372018-07-27 10:03:46 -0400763 fGlobals.writeText("layout(origin_upper_left) in vec4 gl_FragCoord;\n");
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500764 fSetupFragPositionGlobal = true;
Greg Daniele8e4a3e2016-12-12 17:20:42 +0000765 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500766 this->write("gl_FragCoord");
Greg Daniele8e4a3e2016-12-12 17:20:42 +0000767 } else {
Ethan Nicholascd700e92018-08-24 16:43:57 -0400768 if (!fSetupFragPositionLocal) {
Michael Ludwig5e1f6ea2018-12-03 15:14:50 -0500769 fFunctionHeader += usesPrecisionModifiers() ? "highp " : "";
Michael Ludwigf0b60442018-12-10 14:43:38 +0000770 fFunctionHeader += " vec4 sk_FragCoord = vec4(gl_FragCoord.x, " SKSL_RTHEIGHT_NAME
771 " - gl_FragCoord.y, gl_FragCoord.z, gl_FragCoord.w);\n";
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500772 fSetupFragPositionLocal = true;
773 }
774 this->write("sk_FragCoord");
775 }
776}
777
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500778void GLSLCodeGenerator::writeVariableReference(const VariableReference& ref) {
779 switch (ref.fVariable.fModifiers.fLayout.fBuiltin) {
780 case SK_FRAGCOLOR_BUILTIN:
781 if (fProgram.fSettings.fCaps->mustDeclareFragmentShaderOutput()) {
782 this->write("sk_FragColor");
783 } else {
784 this->write("gl_FragColor");
785 }
786 break;
787 case SK_FRAGCOORD_BUILTIN:
788 this->writeFragCoord();
789 break;
Ethan Nicholascd700e92018-08-24 16:43:57 -0400790 case SK_WIDTH_BUILTIN:
791 this->write("u_skRTWidth");
792 break;
793 case SK_HEIGHT_BUILTIN:
794 this->write("u_skRTHeight");
795 break;
Chris Dalton49d14e92018-07-27 12:38:35 -0600796 case SK_CLOCKWISE_BUILTIN:
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600797 this->write(fProgram.fSettings.fFlipY ? "(!gl_FrontFacing)" : "gl_FrontFacing");
Chris Dalton49d14e92018-07-27 12:38:35 -0600798 break;
Chris Daltonb0fd4b12019-10-29 13:41:22 -0600799 case SK_SAMPLEMASK_BUILTIN:
Chris Dalton8a64a442019-10-29 18:54:58 -0600800 SkASSERT(fProgram.fSettings.fCaps->sampleMaskSupport());
Chris Daltonb0fd4b12019-10-29 13:41:22 -0600801 this->write("gl_SampleMask");
802 break;
Ethan Nicholasa51740c2017-02-07 14:53:32 -0500803 case SK_VERTEXID_BUILTIN:
804 this->write("gl_VertexID");
805 break;
Chris Dalton8580d512017-10-14 22:12:33 -0600806 case SK_INSTANCEID_BUILTIN:
807 this->write("gl_InstanceID");
808 break;
Ethan Nicholas67d64602017-02-09 10:15:25 -0500809 case SK_CLIPDISTANCE_BUILTIN:
810 this->write("gl_ClipDistance");
811 break;
Ethan Nicholas52cad152017-02-16 16:37:32 -0500812 case SK_IN_BUILTIN:
813 this->write("gl_in");
814 break;
815 case SK_INVOCATIONID_BUILTIN:
816 this->write("gl_InvocationID");
817 break;
Ethan Nicholaseab2baa2018-04-13 15:16:27 -0400818 case SK_LASTFRAGCOLOR_BUILTIN:
819 this->write(fProgram.fSettings.fCaps->fbFetchColorName());
820 break;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500821 default:
822 this->write(ref.fVariable.fName);
ethannicholas5961bc92016-10-12 06:39:56 -0700823 }
ethannicholasf789b382016-08-03 12:43:36 -0700824}
825
826void GLSLCodeGenerator::writeIndexExpression(const IndexExpression& expr) {
827 this->writeExpression(*expr.fBase, kPostfix_Precedence);
828 this->write("[");
829 this->writeExpression(*expr.fIndex, kTopLevel_Precedence);
830 this->write("]");
831}
832
Brian Osmancd3261a2018-01-16 13:52:29 +0000833bool is_sk_position(const FieldAccess& f) {
834 return "sk_Position" == f.fBase->fType.fields()[f.fFieldIndex].fName;
835}
836
ethannicholasf789b382016-08-03 12:43:36 -0700837void GLSLCodeGenerator::writeFieldAccess(const FieldAccess& f) {
838 if (f.fOwnerKind == FieldAccess::kDefault_OwnerKind) {
839 this->writeExpression(*f.fBase, kPostfix_Precedence);
840 this->write(".");
841 }
Ethan Nicholas67d64602017-02-09 10:15:25 -0500842 switch (f.fBase->fType.fields()[f.fFieldIndex].fModifiers.fLayout.fBuiltin) {
843 case SK_CLIPDISTANCE_BUILTIN:
844 this->write("gl_ClipDistance");
845 break;
846 default:
Ethan Nicholasbed683a2017-09-26 14:23:59 -0400847 StringFragment name = f.fBase->fType.fields()[f.fFieldIndex].fName;
848 if (name == "sk_Position") {
849 this->write("gl_Position");
850 } else if (name == "sk_PointSize") {
851 this->write("gl_PointSize");
852 } else {
853 this->write(f.fBase->fType.fields()[f.fFieldIndex].fName);
854 }
Ethan Nicholas67d64602017-02-09 10:15:25 -0500855 }
ethannicholasf789b382016-08-03 12:43:36 -0700856}
857
Ethan Nicholase455f652019-09-13 12:52:55 -0400858void GLSLCodeGenerator::writeConstantSwizzle(const Swizzle& swizzle, const String& constants) {
859 this->writeType(swizzle.fType);
860 this->write("(");
861 this->write(constants);
862 this->write(")");
863}
864
865void GLSLCodeGenerator::writeSwizzleMask(const Swizzle& swizzle, const String& mask) {
ethannicholasf789b382016-08-03 12:43:36 -0700866 this->writeExpression(*swizzle.fBase, kPostfix_Precedence);
867 this->write(".");
Ethan Nicholase455f652019-09-13 12:52:55 -0400868 this->write(mask);
869}
870
871void GLSLCodeGenerator::writeSwizzleConstructor(const Swizzle& swizzle, const String& constants,
872 const String& mask,
873 GLSLCodeGenerator::SwizzleOrder order) {
874 this->writeType(swizzle.fType);
875 this->write("(");
876 if (order == SwizzleOrder::CONSTANTS_FIRST) {
877 this->write(constants);
878 this->write(", ");
879 this->writeSwizzleMask(swizzle, mask);
880 } else {
881 this->writeSwizzleMask(swizzle, mask);
882 this->write(", ");
883 this->write(constants);
884 }
885 this->write(")");
886}
887
888void GLSLCodeGenerator::writeSwizzleConstructor(const Swizzle& swizzle, const String& constants,
889 const String& mask, const String& reswizzle) {
890 this->writeSwizzleConstructor(swizzle, constants, mask, SwizzleOrder::MASK_FIRST);
891 this->write(".");
892 this->write(reswizzle);
893}
894
895// Writing a swizzle is complicated due to the handling of constant swizzle components. The most
896// problematic case is a mask like '.r00a'. A naive approach might turn that into
897// 'vec4(base.r, 0, 0, base.a)', but that would cause 'base' to be evaluated twice. We instead
898// group the swizzle mask ('ra') and constants ('0, 0') together and use a secondary swizzle to put
899// them back into the right order, so in this case we end up with something like
900// 'vec4(base4.ra, 0, 0).rbag'.
901void GLSLCodeGenerator::writeSwizzle(const Swizzle& swizzle) {
902 // has a 1 bit in every position for which the swizzle mask is a constant, so 'r0b1' would
903 // yield binary 0101.
904 int constantBits = 0;
905 String mask;
906 String constants;
907 // compute mask ("ra") and constant ("0, 0") strings, and fill in constantBits
ethannicholasf789b382016-08-03 12:43:36 -0700908 for (int c : swizzle.fComponents) {
Ethan Nicholase455f652019-09-13 12:52:55 -0400909 constantBits <<= 1;
910 switch (c) {
911 case SKSL_SWIZZLE_0:
912 constantBits |= 1;
913 if (constants.length() > 0) {
914 constants += ", ";
915 }
916 constants += "0";
917 break;
918 case SKSL_SWIZZLE_1:
919 constantBits |= 1;
920 if (constants.length() > 0) {
921 constants += ", ";
922 }
923 constants += "1";
924 break;
925 case 0:
926 mask += "x";
927 break;
928 case 1:
929 mask += "y";
930 break;
931 case 2:
932 mask += "z";
933 break;
934 case 3:
935 mask += "w";
936 break;
937 default:
938 SkASSERT(false);
Ethan Nicholasac285b12019-02-12 16:05:18 -0500939 }
940 }
Ethan Nicholase455f652019-09-13 12:52:55 -0400941 switch (swizzle.fComponents.size()) {
942 case 1:
943 if (constantBits == 1) {
944 this->write(constants);
945 }
946 else {
947 this->writeSwizzleMask(swizzle, mask);
948 }
949 break;
950 case 2:
951 switch (constantBits) {
952 case 0: // 00
953 this->writeSwizzleMask(swizzle, mask);
954 break;
955 case 1: // 01
956 this->writeSwizzleConstructor(swizzle, constants, mask,
957 SwizzleOrder::MASK_FIRST);
958 break;
959 case 2: // 10
960 this->writeSwizzleConstructor(swizzle, constants, mask,
961 SwizzleOrder::CONSTANTS_FIRST);
962 break;
963 case 3: // 11
964 this->writeConstantSwizzle(swizzle, constants);
965 break;
966 default:
967 SkASSERT(false);
968 }
969 break;
970 case 3:
971 switch (constantBits) {
972 case 0: // 000
973 this->writeSwizzleMask(swizzle, mask);
974 break;
975 case 1: // 001
976 case 3: // 011
977 this->writeSwizzleConstructor(swizzle, constants, mask,
978 SwizzleOrder::MASK_FIRST);
979 break;
980 case 4: // 100
981 case 6: // 110
982 this->writeSwizzleConstructor(swizzle, constants, mask,
983 SwizzleOrder::CONSTANTS_FIRST);
984 break;
985 case 2: // 010
986 this->writeSwizzleConstructor(swizzle, constants, mask, "xzy");
987 break;
988 case 5: // 101
989 this->writeSwizzleConstructor(swizzle, constants, mask, "yxz");
990 break;
991 case 7: // 111
992 this->writeConstantSwizzle(swizzle, constants);
993 break;
994 }
995 break;
996 case 4:
997 switch (constantBits) {
998 case 0: // 0000
999 this->writeSwizzleMask(swizzle, mask);
1000 break;
1001 case 1: // 0001
1002 case 3: // 0011
1003 case 7: // 0111
1004 this->writeSwizzleConstructor(swizzle, constants, mask,
1005 SwizzleOrder::MASK_FIRST);
1006 break;
1007 case 8: // 1000
1008 case 12: // 1100
1009 case 14: // 1110
1010 this->writeSwizzleConstructor(swizzle, constants, mask,
1011 SwizzleOrder::CONSTANTS_FIRST);
1012 break;
1013 case 2: // 0010
1014 this->writeSwizzleConstructor(swizzle, constants, mask, "xywz");
1015 break;
1016 case 4: // 0100
1017 this->writeSwizzleConstructor(swizzle, constants, mask, "xwyz");
1018 break;
1019 case 5: // 0101
1020 this->writeSwizzleConstructor(swizzle, constants, mask, "xzyw");
1021 break;
1022 case 6: // 0110
1023 this->writeSwizzleConstructor(swizzle, constants, mask, "xzwy");
1024 break;
1025 case 9: // 1001
1026 this->writeSwizzleConstructor(swizzle, constants, mask, "zxyw");
1027 break;
1028 case 10: // 1010
1029 this->writeSwizzleConstructor(swizzle, constants, mask, "zxwy");
1030 break;
1031 case 11: // 1011
1032 this->writeSwizzleConstructor(swizzle, constants, mask, "yxzw");
1033 break;
1034 case 13: // 1101
1035 this->writeSwizzleConstructor(swizzle, constants, mask, "yzxw");
1036 break;
1037 case 15: // 1111
1038 this->writeConstantSwizzle(swizzle, constants);
1039 break;
1040 }
ethannicholasf789b382016-08-03 12:43:36 -07001041 }
1042}
1043
Ethan Nicholas762466e2017-06-29 10:03:38 -04001044GLSLCodeGenerator::Precedence GLSLCodeGenerator::GetBinaryPrecedence(Token::Kind op) {
ethannicholasf789b382016-08-03 12:43:36 -07001045 switch (op) {
1046 case Token::STAR: // fall through
1047 case Token::SLASH: // fall through
1048 case Token::PERCENT: return GLSLCodeGenerator::kMultiplicative_Precedence;
1049 case Token::PLUS: // fall through
1050 case Token::MINUS: return GLSLCodeGenerator::kAdditive_Precedence;
1051 case Token::SHL: // fall through
1052 case Token::SHR: return GLSLCodeGenerator::kShift_Precedence;
1053 case Token::LT: // fall through
1054 case Token::GT: // fall through
1055 case Token::LTEQ: // fall through
1056 case Token::GTEQ: return GLSLCodeGenerator::kRelational_Precedence;
1057 case Token::EQEQ: // fall through
1058 case Token::NEQ: return GLSLCodeGenerator::kEquality_Precedence;
1059 case Token::BITWISEAND: return GLSLCodeGenerator::kBitwiseAnd_Precedence;
1060 case Token::BITWISEXOR: return GLSLCodeGenerator::kBitwiseXor_Precedence;
1061 case Token::BITWISEOR: return GLSLCodeGenerator::kBitwiseOr_Precedence;
1062 case Token::LOGICALAND: return GLSLCodeGenerator::kLogicalAnd_Precedence;
1063 case Token::LOGICALXOR: return GLSLCodeGenerator::kLogicalXor_Precedence;
1064 case Token::LOGICALOR: return GLSLCodeGenerator::kLogicalOr_Precedence;
1065 case Token::EQ: // fall through
1066 case Token::PLUSEQ: // fall through
1067 case Token::MINUSEQ: // fall through
1068 case Token::STAREQ: // fall through
1069 case Token::SLASHEQ: // fall through
1070 case Token::PERCENTEQ: // fall through
1071 case Token::SHLEQ: // fall through
1072 case Token::SHREQ: // fall through
1073 case Token::LOGICALANDEQ: // fall through
1074 case Token::LOGICALXOREQ: // fall through
1075 case Token::LOGICALOREQ: // fall through
1076 case Token::BITWISEANDEQ: // fall through
1077 case Token::BITWISEXOREQ: // fall through
1078 case Token::BITWISEOREQ: return GLSLCodeGenerator::kAssignment_Precedence;
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001079 case Token::COMMA: return GLSLCodeGenerator::kSequence_Precedence;
ethannicholasf789b382016-08-03 12:43:36 -07001080 default: ABORT("unsupported binary operator");
1081 }
1082}
1083
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001084void GLSLCodeGenerator::writeBinaryExpression(const BinaryExpression& b,
ethannicholasf789b382016-08-03 12:43:36 -07001085 Precedence parentPrecedence) {
Adrienne Walkerc02165f2018-08-21 11:08:11 -07001086 if (fProgram.fSettings.fCaps->unfoldShortCircuitAsTernary() &&
1087 (b.fOperator == Token::LOGICALAND || b.fOperator == Token::LOGICALOR)) {
1088 this->writeShortCircuitWorkaroundExpression(b, parentPrecedence);
1089 return;
1090 }
1091
Ethan Nicholas762466e2017-06-29 10:03:38 -04001092 Precedence precedence = GetBinaryPrecedence(b.fOperator);
ethannicholasf789b382016-08-03 12:43:36 -07001093 if (precedence >= parentPrecedence) {
1094 this->write("(");
1095 }
Ethan Nicholas0b631962018-07-24 13:41:11 -04001096 bool positionWorkaround = fProgramKind == Program::Kind::kVertex_Kind &&
1097 Compiler::IsAssignment(b.fOperator) &&
Brian Osmancd3261a2018-01-16 13:52:29 +00001098 Expression::kFieldAccess_Kind == b.fLeft->fKind &&
1099 is_sk_position((FieldAccess&) *b.fLeft) &&
1100 !strstr(b.fRight->description().c_str(), "sk_RTAdjust") &&
1101 !fProgram.fSettings.fCaps->canUseFragCoord();
1102 if (positionWorkaround) {
1103 this->write("sk_FragCoord_Workaround = (");
1104 }
ethannicholasf789b382016-08-03 12:43:36 -07001105 this->writeExpression(*b.fLeft, precedence);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001106 this->write(" ");
1107 this->write(Compiler::OperatorName(b.fOperator));
1108 this->write(" ");
ethannicholasf789b382016-08-03 12:43:36 -07001109 this->writeExpression(*b.fRight, precedence);
Brian Osmancd3261a2018-01-16 13:52:29 +00001110 if (positionWorkaround) {
1111 this->write(")");
1112 }
ethannicholasf789b382016-08-03 12:43:36 -07001113 if (precedence >= parentPrecedence) {
1114 this->write(")");
1115 }
1116}
1117
Adrienne Walkerc02165f2018-08-21 11:08:11 -07001118void GLSLCodeGenerator::writeShortCircuitWorkaroundExpression(const BinaryExpression& b,
1119 Precedence parentPrecedence) {
1120 if (kTernary_Precedence >= parentPrecedence) {
1121 this->write("(");
1122 }
1123
1124 // Transform:
1125 // a && b => a ? b : false
1126 // a || b => a ? true : b
1127 this->writeExpression(*b.fLeft, kTernary_Precedence);
1128 this->write(" ? ");
1129 if (b.fOperator == Token::LOGICALAND) {
1130 this->writeExpression(*b.fRight, kTernary_Precedence);
1131 } else {
1132 BoolLiteral boolTrue(fContext, -1, true);
1133 this->writeBoolLiteral(boolTrue);
1134 }
1135 this->write(" : ");
1136 if (b.fOperator == Token::LOGICALAND) {
1137 BoolLiteral boolFalse(fContext, -1, false);
1138 this->writeBoolLiteral(boolFalse);
1139 } else {
1140 this->writeExpression(*b.fRight, kTernary_Precedence);
1141 }
1142 if (kTernary_Precedence >= parentPrecedence) {
1143 this->write(")");
1144 }
1145}
1146
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001147void GLSLCodeGenerator::writeTernaryExpression(const TernaryExpression& t,
ethannicholasf789b382016-08-03 12:43:36 -07001148 Precedence parentPrecedence) {
1149 if (kTernary_Precedence >= parentPrecedence) {
1150 this->write("(");
1151 }
1152 this->writeExpression(*t.fTest, kTernary_Precedence);
1153 this->write(" ? ");
1154 this->writeExpression(*t.fIfTrue, kTernary_Precedence);
1155 this->write(" : ");
1156 this->writeExpression(*t.fIfFalse, kTernary_Precedence);
1157 if (kTernary_Precedence >= parentPrecedence) {
1158 this->write(")");
1159 }
1160}
1161
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001162void GLSLCodeGenerator::writePrefixExpression(const PrefixExpression& p,
ethannicholasf789b382016-08-03 12:43:36 -07001163 Precedence parentPrecedence) {
1164 if (kPrefix_Precedence >= parentPrecedence) {
1165 this->write("(");
1166 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001167 this->write(Compiler::OperatorName(p.fOperator));
ethannicholasf789b382016-08-03 12:43:36 -07001168 this->writeExpression(*p.fOperand, kPrefix_Precedence);
1169 if (kPrefix_Precedence >= parentPrecedence) {
1170 this->write(")");
1171 }
1172}
1173
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001174void GLSLCodeGenerator::writePostfixExpression(const PostfixExpression& p,
ethannicholasf789b382016-08-03 12:43:36 -07001175 Precedence parentPrecedence) {
1176 if (kPostfix_Precedence >= parentPrecedence) {
1177 this->write("(");
1178 }
1179 this->writeExpression(*p.fOperand, kPostfix_Precedence);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001180 this->write(Compiler::OperatorName(p.fOperator));
ethannicholasf789b382016-08-03 12:43:36 -07001181 if (kPostfix_Precedence >= parentPrecedence) {
1182 this->write(")");
1183 }
1184}
1185
1186void GLSLCodeGenerator::writeBoolLiteral(const BoolLiteral& b) {
1187 this->write(b.fValue ? "true" : "false");
1188}
1189
1190void GLSLCodeGenerator::writeIntLiteral(const IntLiteral& i) {
ethannicholas5961bc92016-10-12 06:39:56 -07001191 if (i.fType == *fContext.fUInt_Type) {
1192 this->write(to_string(i.fValue & 0xffffffff) + "u");
Ethan Nicholas58d56482017-12-19 09:29:22 -05001193 } else if (i.fType == *fContext.fUShort_Type) {
1194 this->write(to_string(i.fValue & 0xffff) + "u");
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001195 } else if (i.fType == *fContext.fUByte_Type) {
1196 this->write(to_string(i.fValue & 0xff) + "u");
1197 } else {
ethannicholas5961bc92016-10-12 06:39:56 -07001198 this->write(to_string((int32_t) i.fValue));
1199 }
ethannicholasf789b382016-08-03 12:43:36 -07001200}
1201
1202void GLSLCodeGenerator::writeFloatLiteral(const FloatLiteral& f) {
1203 this->write(to_string(f.fValue));
1204}
1205
Ethan Nicholas762466e2017-06-29 10:03:38 -04001206void GLSLCodeGenerator::writeSetting(const Setting& s) {
1207 ABORT("internal error; setting was not folded to a constant during compilation\n");
1208}
1209
ethannicholasf789b382016-08-03 12:43:36 -07001210void GLSLCodeGenerator::writeFunction(const FunctionDefinition& f) {
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -04001211 fSetupFragPositionLocal = false;
1212 fSetupFragCoordWorkaround = false;
Ethan Nicholas00543112018-07-31 09:44:36 -04001213 if (fProgramKind != Program::kPipelineStage_Kind) {
1214 this->writeTypePrecision(f.fDeclaration.fReturnType);
1215 this->writeType(f.fDeclaration.fReturnType);
1216 this->write(" " + f.fDeclaration.fName + "(");
1217 const char* separator = "";
1218 for (const auto& param : f.fDeclaration.fParameters) {
1219 this->write(separator);
1220 separator = ", ";
1221 this->writeModifiers(param->fModifiers, false);
1222 std::vector<int> sizes;
1223 const Type* type = &param->fType;
1224 while (type->kind() == Type::kArray_Kind) {
1225 sizes.push_back(type->columns());
1226 type = &type->componentType();
1227 }
1228 this->writeTypePrecision(*type);
1229 this->writeType(*type);
1230 this->write(" " + param->fName);
1231 for (int s : sizes) {
1232 if (s <= 0) {
1233 this->write("[]");
1234 } else {
1235 this->write("[" + to_string(s) + "]");
1236 }
ethannicholas5961bc92016-10-12 06:39:56 -07001237 }
1238 }
Ethan Nicholas00543112018-07-31 09:44:36 -04001239 this->writeLine(") {");
1240 fIndentation++;
ethannicholasf789b382016-08-03 12:43:36 -07001241 }
ethannicholas5961bc92016-10-12 06:39:56 -07001242 fFunctionHeader = "";
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001243 OutputStream* oldOut = fOut;
1244 StringStream buffer;
ethannicholas5961bc92016-10-12 06:39:56 -07001245 fOut = &buffer;
Ethan Nicholascb670962017-04-20 19:31:52 -04001246 this->writeStatements(((Block&) *f.fBody).fStatements);
Ethan Nicholas00543112018-07-31 09:44:36 -04001247 if (fProgramKind != Program::kPipelineStage_Kind) {
1248 fIndentation--;
1249 this->writeLine("}");
1250 }
ethannicholas5961bc92016-10-12 06:39:56 -07001251
1252 fOut = oldOut;
1253 this->write(fFunctionHeader);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001254 this->write(buffer.str());
ethannicholasf789b382016-08-03 12:43:36 -07001255}
1256
Greg Daniel64773e62016-11-22 09:44:03 -05001257void GLSLCodeGenerator::writeModifiers(const Modifiers& modifiers,
ethannicholas5961bc92016-10-12 06:39:56 -07001258 bool globalContext) {
Brian Salomonf9f45122016-11-29 11:59:17 -05001259 if (modifiers.fFlags & Modifiers::kFlat_Flag) {
1260 this->write("flat ");
1261 }
ethannicholas5961bc92016-10-12 06:39:56 -07001262 if (modifiers.fFlags & Modifiers::kNoPerspective_Flag) {
1263 this->write("noperspective ");
1264 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001265 String layout = modifiers.fLayout.description();
Ethan Nicholas8da9e942017-03-09 16:35:09 -05001266 if (layout.size()) {
1267 this->write(layout + " ");
1268 }
Brian Salomonf9f45122016-11-29 11:59:17 -05001269 if (modifiers.fFlags & Modifiers::kReadOnly_Flag) {
1270 this->write("readonly ");
1271 }
1272 if (modifiers.fFlags & Modifiers::kWriteOnly_Flag) {
1273 this->write("writeonly ");
1274 }
1275 if (modifiers.fFlags & Modifiers::kCoherent_Flag) {
1276 this->write("coherent ");
1277 }
1278 if (modifiers.fFlags & Modifiers::kVolatile_Flag) {
1279 this->write("volatile ");
1280 }
1281 if (modifiers.fFlags & Modifiers::kRestrict_Flag) {
1282 this->write("restrict ");
ethannicholas5961bc92016-10-12 06:39:56 -07001283 }
Greg Daniel64773e62016-11-22 09:44:03 -05001284 if ((modifiers.fFlags & Modifiers::kIn_Flag) &&
ethannicholas5961bc92016-10-12 06:39:56 -07001285 (modifiers.fFlags & Modifiers::kOut_Flag)) {
1286 this->write("inout ");
1287 } else if (modifiers.fFlags & Modifiers::kIn_Flag) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001288 if (globalContext &&
1289 fProgram.fSettings.fCaps->generation() < GrGLSLGeneration::k130_GrGLSLGeneration) {
ethannicholas5961bc92016-10-12 06:39:56 -07001290 this->write(fProgramKind == Program::kVertex_Kind ? "attribute "
1291 : "varying ");
1292 } else {
1293 this->write("in ");
1294 }
1295 } else if (modifiers.fFlags & Modifiers::kOut_Flag) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001296 if (globalContext &&
1297 fProgram.fSettings.fCaps->generation() < GrGLSLGeneration::k130_GrGLSLGeneration) {
ethannicholas5961bc92016-10-12 06:39:56 -07001298 this->write("varying ");
1299 } else {
1300 this->write("out ");
1301 }
1302 }
1303 if (modifiers.fFlags & Modifiers::kUniform_Flag) {
1304 this->write("uniform ");
1305 }
1306 if (modifiers.fFlags & Modifiers::kConst_Flag) {
1307 this->write("const ");
1308 }
Ethan Nicholasa7ceb502019-01-11 10:31:48 -05001309 if (modifiers.fFlags & Modifiers::kPLS_Flag) {
1310 this->write("__pixel_localEXT ");
1311 }
1312 if (modifiers.fFlags & Modifiers::kPLSIn_Flag) {
1313 this->write("__pixel_local_inEXT ");
1314 }
1315 if (modifiers.fFlags & Modifiers::kPLSOut_Flag) {
1316 this->write("__pixel_local_outEXT ");
1317 }
Ethan Nicholas858fecc2019-03-07 13:19:18 -05001318 switch (modifiers.fLayout.fFormat) {
1319 case Layout::Format::kUnspecified:
1320 break;
Robert Phillipsebab03f2019-07-22 08:48:18 -04001321 case Layout::Format::kRGBA32F: // fall through
Ethan Nicholas858fecc2019-03-07 13:19:18 -05001322 case Layout::Format::kR32F:
ethannicholas5961bc92016-10-12 06:39:56 -07001323 this->write("highp ");
Ethan Nicholas858fecc2019-03-07 13:19:18 -05001324 break;
Robert Phillipsebab03f2019-07-22 08:48:18 -04001325 case Layout::Format::kRGBA16F: // fall through
1326 case Layout::Format::kR16F: // fall through
1327 case Layout::Format::kLUMINANCE16F: // fall through
Ethan Nicholas858fecc2019-03-07 13:19:18 -05001328 case Layout::Format::kRG16F:
1329 this->write("mediump ");
1330 break;
Robert Phillipsebab03f2019-07-22 08:48:18 -04001331 case Layout::Format::kRGBA8: // fall through
1332 case Layout::Format::kR8: // fall through
1333 case Layout::Format::kRGBA8I: // fall through
Ethan Nicholas858fecc2019-03-07 13:19:18 -05001334 case Layout::Format::kR8I:
1335 this->write("lowp ");
1336 break;
ethannicholas5961bc92016-10-12 06:39:56 -07001337 }
ethannicholasf789b382016-08-03 12:43:36 -07001338}
1339
1340void GLSLCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) {
Ethan Nicholas52cad152017-02-16 16:37:32 -05001341 if (intf.fTypeName == "sk_PerVertex") {
ethannicholasf789b382016-08-03 12:43:36 -07001342 return;
1343 }
ethannicholas5961bc92016-10-12 06:39:56 -07001344 this->writeModifiers(intf.fVariable.fModifiers, true);
Ethan Nicholas50afc172017-02-16 14:49:57 -05001345 this->writeLine(intf.fTypeName + " {");
ethannicholasf789b382016-08-03 12:43:36 -07001346 fIndentation++;
Ethan Nicholas50afc172017-02-16 14:49:57 -05001347 const Type* structType = &intf.fVariable.fType;
1348 while (structType->kind() == Type::kArray_Kind) {
1349 structType = &structType->componentType();
1350 }
1351 for (const auto& f : structType->fields()) {
ethannicholas5961bc92016-10-12 06:39:56 -07001352 this->writeModifiers(f.fModifiers, false);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001353 this->writeTypePrecision(*f.fType);
ethannicholas0730be72016-09-01 07:59:02 -07001354 this->writeType(*f.fType);
ethannicholasf789b382016-08-03 12:43:36 -07001355 this->writeLine(" " + f.fName + ";");
1356 }
1357 fIndentation--;
Ethan Nicholas50afc172017-02-16 14:49:57 -05001358 this->write("}");
1359 if (intf.fInstanceName.size()) {
1360 this->write(" ");
1361 this->write(intf.fInstanceName);
1362 for (const auto& size : intf.fSizes) {
1363 this->write("[");
1364 if (size) {
1365 this->writeExpression(*size, kTopLevel_Precedence);
1366 }
1367 this->write("]");
1368 }
1369 }
1370 this->writeLine(";");
ethannicholasf789b382016-08-03 12:43:36 -07001371}
1372
Ethan Nicholas762466e2017-06-29 10:03:38 -04001373void GLSLCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) {
1374 this->writeExpression(value, kTopLevel_Precedence);
1375}
1376
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001377const char* GLSLCodeGenerator::getTypePrecision(const Type& type) {
1378 if (usesPrecisionModifiers()) {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001379 switch (type.kind()) {
1380 case Type::kScalar_Kind:
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001381 if (type == *fContext.fShort_Type || type == *fContext.fUShort_Type ||
1382 type == *fContext.fByte_Type || type == *fContext.fUByte_Type) {
Chris Daltonc2d0dd62018-03-07 07:46:10 -07001383 if (fProgram.fSettings.fForceHighPrecision ||
1384 fProgram.fSettings.fCaps->incompleteShortIntPrecision()) {
1385 return "highp ";
1386 }
1387 return "mediump ";
1388 }
1389 if (type == *fContext.fHalf_Type) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001390 return fProgram.fSettings.fForceHighPrecision ? "highp " : "mediump ";
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001391 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001392 if (type == *fContext.fFloat_Type || type == *fContext.fInt_Type ||
1393 type == *fContext.fUInt_Type) {
1394 return "highp ";
1395 }
1396 return "";
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001397 case Type::kVector_Kind: // fall through
1398 case Type::kMatrix_Kind:
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001399 return this->getTypePrecision(type.componentType());
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001400 default:
1401 break;
1402 }
1403 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001404 return "";
1405}
1406
1407void GLSLCodeGenerator::writeTypePrecision(const Type& type) {
1408 this->write(this->getTypePrecision(type));
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001409}
1410
ethannicholas5961bc92016-10-12 06:39:56 -07001411void GLSLCodeGenerator::writeVarDeclarations(const VarDeclarations& decl, bool global) {
Ethan Nicholas14efcbf2017-11-07 09:23:38 -05001412 if (!decl.fVars.size()) {
1413 return;
1414 }
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001415 bool wroteType = false;
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001416 for (const auto& stmt : decl.fVars) {
1417 VarDeclaration& var = (VarDeclaration&) *stmt;
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001418 if (wroteType) {
1419 this->write(", ");
1420 } else {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001421 this->writeModifiers(var.fVar->fModifiers, global);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001422 this->writeTypePrecision(decl.fBaseType);
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001423 this->writeType(decl.fBaseType);
1424 this->write(" ");
1425 wroteType = true;
1426 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001427 this->write(var.fVar->fName);
1428 for (const auto& size : var.fSizes) {
ethannicholasf789b382016-08-03 12:43:36 -07001429 this->write("[");
ethannicholas5961bc92016-10-12 06:39:56 -07001430 if (size) {
1431 this->writeExpression(*size, kTopLevel_Precedence);
1432 }
ethannicholasf789b382016-08-03 12:43:36 -07001433 this->write("]");
1434 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001435 if (var.fValue) {
ethannicholasf789b382016-08-03 12:43:36 -07001436 this->write(" = ");
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001437 this->writeVarInitializer(*var.fVar, *var.fValue);
ethannicholasf789b382016-08-03 12:43:36 -07001438 }
Brian Osman4b2f9152018-04-17 11:19:57 -04001439 if (!fFoundExternalSamplerDecl && var.fVar->fType == *fContext.fSamplerExternalOES_Type) {
1440 if (fProgram.fSettings.fCaps->externalTextureExtensionString()) {
Ethan Nicholas88f6d372018-07-27 10:03:46 -04001441 this->writeExtension(fProgram.fSettings.fCaps->externalTextureExtensionString());
Brian Osman4b2f9152018-04-17 11:19:57 -04001442 }
Brian Osman061020e2018-04-17 14:22:15 -04001443 if (fProgram.fSettings.fCaps->secondExternalTextureExtensionString()) {
Ethan Nicholas88f6d372018-07-27 10:03:46 -04001444 this->writeExtension(
1445 fProgram.fSettings.fCaps->secondExternalTextureExtensionString());
Brian Osman061020e2018-04-17 14:22:15 -04001446 }
Brian Osman4b2f9152018-04-17 11:19:57 -04001447 fFoundExternalSamplerDecl = true;
1448 }
Brian Salomon67529b22019-08-13 15:31:04 -04001449 if (!fFoundRectSamplerDecl && var.fVar->fType == *fContext.fSampler2DRect_Type) {
1450 fFoundRectSamplerDecl = true;
1451 }
ethannicholasf789b382016-08-03 12:43:36 -07001452 }
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001453 if (wroteType) {
1454 this->write(";");
1455 }
ethannicholasf789b382016-08-03 12:43:36 -07001456}
1457
1458void GLSLCodeGenerator::writeStatement(const Statement& s) {
1459 switch (s.fKind) {
1460 case Statement::kBlock_Kind:
1461 this->writeBlock((Block&) s);
1462 break;
1463 case Statement::kExpression_Kind:
1464 this->writeExpression(*((ExpressionStatement&) s).fExpression, kTopLevel_Precedence);
1465 this->write(";");
1466 break;
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001467 case Statement::kReturn_Kind:
ethannicholasf789b382016-08-03 12:43:36 -07001468 this->writeReturnStatement((ReturnStatement&) s);
1469 break;
ethannicholas14fe8cc2016-09-07 13:37:16 -07001470 case Statement::kVarDeclarations_Kind:
ethannicholas5961bc92016-10-12 06:39:56 -07001471 this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclaration, false);
ethannicholasf789b382016-08-03 12:43:36 -07001472 break;
1473 case Statement::kIf_Kind:
1474 this->writeIfStatement((IfStatement&) s);
1475 break;
1476 case Statement::kFor_Kind:
1477 this->writeForStatement((ForStatement&) s);
1478 break;
1479 case Statement::kWhile_Kind:
1480 this->writeWhileStatement((WhileStatement&) s);
1481 break;
1482 case Statement::kDo_Kind:
1483 this->writeDoStatement((DoStatement&) s);
1484 break;
Ethan Nicholasaf197692017-02-27 13:26:45 -05001485 case Statement::kSwitch_Kind:
1486 this->writeSwitchStatement((SwitchStatement&) s);
1487 break;
ethannicholasf789b382016-08-03 12:43:36 -07001488 case Statement::kBreak_Kind:
1489 this->write("break;");
1490 break;
1491 case Statement::kContinue_Kind:
1492 this->write("continue;");
1493 break;
1494 case Statement::kDiscard_Kind:
1495 this->write("discard;");
1496 break;
Ethan Nicholascb670962017-04-20 19:31:52 -04001497 case Statement::kNop_Kind:
1498 this->write(";");
1499 break;
ethannicholasf789b382016-08-03 12:43:36 -07001500 default:
1501 ABORT("unsupported statement: %s", s.description().c_str());
1502 }
1503}
1504
Ethan Nicholascb670962017-04-20 19:31:52 -04001505void GLSLCodeGenerator::writeStatements(const std::vector<std::unique_ptr<Statement>>& statements) {
1506 for (const auto& s : statements) {
1507 if (!s->isEmpty()) {
1508 this->writeStatement(*s);
1509 this->writeLine();
1510 }
1511 }
1512}
1513
ethannicholasf789b382016-08-03 12:43:36 -07001514void GLSLCodeGenerator::writeBlock(const Block& b) {
1515 this->writeLine("{");
1516 fIndentation++;
Ethan Nicholascb670962017-04-20 19:31:52 -04001517 this->writeStatements(b.fStatements);
ethannicholasf789b382016-08-03 12:43:36 -07001518 fIndentation--;
1519 this->write("}");
1520}
1521
1522void GLSLCodeGenerator::writeIfStatement(const IfStatement& stmt) {
1523 this->write("if (");
1524 this->writeExpression(*stmt.fTest, kTopLevel_Precedence);
1525 this->write(") ");
1526 this->writeStatement(*stmt.fIfTrue);
1527 if (stmt.fIfFalse) {
1528 this->write(" else ");
1529 this->writeStatement(*stmt.fIfFalse);
1530 }
1531}
1532
1533void GLSLCodeGenerator::writeForStatement(const ForStatement& f) {
1534 this->write("for (");
Ethan Nicholasb310fd52017-06-09 13:46:34 -04001535 if (f.fInitializer && !f.fInitializer->isEmpty()) {
ethannicholasf789b382016-08-03 12:43:36 -07001536 this->writeStatement(*f.fInitializer);
1537 } else {
1538 this->write("; ");
1539 }
1540 if (f.fTest) {
Adrienne Walkeree8295c2018-08-21 10:56:30 -07001541 if (fProgram.fSettings.fCaps->addAndTrueToLoopCondition()) {
1542 std::unique_ptr<Expression> and_true(new BinaryExpression(
1543 -1, f.fTest->clone(), Token::LOGICALAND,
1544 std::unique_ptr<BoolLiteral>(new BoolLiteral(fContext, -1,
1545 true)),
1546 *fContext.fBool_Type));
1547 this->writeExpression(*and_true, kTopLevel_Precedence);
1548 } else {
1549 this->writeExpression(*f.fTest, kTopLevel_Precedence);
1550 }
ethannicholasf789b382016-08-03 12:43:36 -07001551 }
1552 this->write("; ");
1553 if (f.fNext) {
1554 this->writeExpression(*f.fNext, kTopLevel_Precedence);
1555 }
1556 this->write(") ");
1557 this->writeStatement(*f.fStatement);
1558}
1559
1560void GLSLCodeGenerator::writeWhileStatement(const WhileStatement& w) {
1561 this->write("while (");
1562 this->writeExpression(*w.fTest, kTopLevel_Precedence);
1563 this->write(") ");
1564 this->writeStatement(*w.fStatement);
1565}
1566
1567void GLSLCodeGenerator::writeDoStatement(const DoStatement& d) {
Adrienne Walker8b23ca62018-08-22 10:45:41 -07001568 if (!fProgram.fSettings.fCaps->rewriteDoWhileLoops()) {
1569 this->write("do ");
1570 this->writeStatement(*d.fStatement);
1571 this->write(" while (");
1572 this->writeExpression(*d.fTest, kTopLevel_Precedence);
1573 this->write(");");
1574 return;
1575 }
1576
1577 // Otherwise, do the do while loop workaround, to rewrite loops of the form:
1578 // do {
1579 // CODE;
1580 // } while (CONDITION)
1581 //
1582 // to loops of the form
1583 // bool temp = false;
1584 // while (true) {
1585 // if (temp) {
1586 // if (!CONDITION) {
1587 // break;
1588 // }
1589 // }
1590 // temp = true;
1591 // CODE;
1592 // }
1593 String tmpVar = "_tmpLoopSeenOnce" + to_string(fVarCount++);
1594 this->write("bool ");
1595 this->write(tmpVar);
1596 this->writeLine(" = false;");
1597 this->writeLine("while (true) {");
1598 fIndentation++;
1599 this->write("if (");
1600 this->write(tmpVar);
1601 this->writeLine(") {");
1602 fIndentation++;
1603 this->write("if (!");
1604 this->writeExpression(*d.fTest, kPrefix_Precedence);
1605 this->writeLine(") {");
1606 fIndentation++;
1607 this->writeLine("break;");
1608 fIndentation--;
1609 this->writeLine("}");
1610 fIndentation--;
1611 this->writeLine("}");
1612 this->write(tmpVar);
1613 this->writeLine(" = true;");
ethannicholasf789b382016-08-03 12:43:36 -07001614 this->writeStatement(*d.fStatement);
Adrienne Walker8b23ca62018-08-22 10:45:41 -07001615 this->writeLine();
1616 fIndentation--;
1617 this->write("}");
ethannicholasf789b382016-08-03 12:43:36 -07001618}
1619
Ethan Nicholasaf197692017-02-27 13:26:45 -05001620void GLSLCodeGenerator::writeSwitchStatement(const SwitchStatement& s) {
1621 this->write("switch (");
1622 this->writeExpression(*s.fValue, kTopLevel_Precedence);
1623 this->writeLine(") {");
1624 fIndentation++;
1625 for (const auto& c : s.fCases) {
1626 if (c->fValue) {
1627 this->write("case ");
1628 this->writeExpression(*c->fValue, kTopLevel_Precedence);
1629 this->writeLine(":");
1630 } else {
1631 this->writeLine("default:");
1632 }
1633 fIndentation++;
1634 for (const auto& stmt : c->fStatements) {
1635 this->writeStatement(*stmt);
1636 this->writeLine();
1637 }
1638 fIndentation--;
1639 }
1640 fIndentation--;
1641 this->write("}");
1642}
1643
ethannicholasf789b382016-08-03 12:43:36 -07001644void GLSLCodeGenerator::writeReturnStatement(const ReturnStatement& r) {
1645 this->write("return");
1646 if (r.fExpression) {
1647 this->write(" ");
1648 this->writeExpression(*r.fExpression, kTopLevel_Precedence);
1649 }
1650 this->write(";");
1651}
1652
Ethan Nicholas762466e2017-06-29 10:03:38 -04001653void GLSLCodeGenerator::writeHeader() {
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001654 this->write(fProgram.fSettings.fCaps->versionDeclString());
ethannicholasf789b382016-08-03 12:43:36 -07001655 this->writeLine();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001656}
1657
Ethan Nicholas762466e2017-06-29 10:03:38 -04001658void GLSLCodeGenerator::writeProgramElement(const ProgramElement& e) {
1659 switch (e.fKind) {
1660 case ProgramElement::kExtension_Kind:
Ethan Nicholas88f6d372018-07-27 10:03:46 -04001661 this->writeExtension(((Extension&) e).fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001662 break;
1663 case ProgramElement::kVar_Kind: {
1664 VarDeclarations& decl = (VarDeclarations&) e;
1665 if (decl.fVars.size() > 0) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001666 int builtin = ((VarDeclaration&) *decl.fVars[0]).fVar->fModifiers.fLayout.fBuiltin;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001667 if (builtin == -1) {
1668 // normal var
1669 this->writeVarDeclarations(decl, true);
1670 this->writeLine();
1671 } else if (builtin == SK_FRAGCOLOR_BUILTIN &&
Ethan Nicholasa7ceb502019-01-11 10:31:48 -05001672 fProgram.fSettings.fCaps->mustDeclareFragmentShaderOutput() &&
1673 ((VarDeclaration&) *decl.fVars[0]).fVar->fWriteCount) {
Brian Salomondc092132018-04-04 10:14:16 -04001674 if (fProgram.fSettings.fFragColorIsInOut) {
1675 this->write("inout ");
1676 } else {
1677 this->write("out ");
1678 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001679 if (usesPrecisionModifiers()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001680 this->write("mediump ");
Mike Klein5ce39722017-06-27 22:52:03 +00001681 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001682 this->writeLine("vec4 sk_FragColor;");
Mike Klein5ce39722017-06-27 22:52:03 +00001683 }
Mike Klein5ce39722017-06-27 22:52:03 +00001684 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001685 break;
Mike Klein5ce39722017-06-27 22:52:03 +00001686 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001687 case ProgramElement::kInterfaceBlock_Kind:
1688 this->writeInterfaceBlock((InterfaceBlock&) e);
1689 break;
1690 case ProgramElement::kFunction_Kind:
1691 this->writeFunction((FunctionDefinition&) e);
1692 break;
Chris Daltonf1b47bb2017-10-06 11:57:51 -06001693 case ProgramElement::kModifiers_Kind: {
1694 const Modifiers& modifiers = ((ModifiersDeclaration&) e).fModifiers;
1695 if (!fFoundGSInvocations && modifiers.fLayout.fInvocations >= 0) {
1696 if (fProgram.fSettings.fCaps->gsInvocationsExtensionString()) {
Ethan Nicholas88f6d372018-07-27 10:03:46 -04001697 this->writeExtension(fProgram.fSettings.fCaps->gsInvocationsExtensionString());
Chris Daltonf1b47bb2017-10-06 11:57:51 -06001698 }
1699 fFoundGSInvocations = true;
1700 }
1701 this->writeModifiers(modifiers, true);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001702 this->writeLine(";");
1703 break;
Chris Daltonf1b47bb2017-10-06 11:57:51 -06001704 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001705 case ProgramElement::kEnum_Kind:
1706 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001707 default:
1708 printf("%s\n", e.description().c_str());
1709 ABORT("unsupported program element");
Ethan Nicholasc0709392017-06-27 11:20:22 -04001710 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001711}
1712
Ethan Nicholascd700e92018-08-24 16:43:57 -04001713void GLSLCodeGenerator::writeInputVars() {
1714 if (fProgram.fInputs.fRTWidth) {
1715 const char* precision = usesPrecisionModifiers() ? "highp " : "";
1716 fGlobals.writeText("uniform ");
1717 fGlobals.writeText(precision);
1718 fGlobals.writeText("float " SKSL_RTWIDTH_NAME ";\n");
1719 }
1720 if (fProgram.fInputs.fRTHeight) {
1721 const char* precision = usesPrecisionModifiers() ? "highp " : "";
1722 fGlobals.writeText("uniform ");
1723 fGlobals.writeText(precision);
1724 fGlobals.writeText("float " SKSL_RTHEIGHT_NAME ";\n");
1725 }
1726}
1727
Ethan Nicholas762466e2017-06-29 10:03:38 -04001728bool GLSLCodeGenerator::generateCode() {
Ethan Nicholas00543112018-07-31 09:44:36 -04001729 if (fProgramKind != Program::kPipelineStage_Kind) {
1730 this->writeHeader();
1731 }
Chris Dalton8fd79552018-01-11 00:46:14 -05001732 if (Program::kGeometry_Kind == fProgramKind &&
1733 fProgram.fSettings.fCaps->geometryShaderExtensionString()) {
Ethan Nicholas88f6d372018-07-27 10:03:46 -04001734 this->writeExtension(fProgram.fSettings.fCaps->geometryShaderExtensionString());
Chris Dalton8fd79552018-01-11 00:46:14 -05001735 }
Ethan Nicholas88f6d372018-07-27 10:03:46 -04001736 OutputStream* rawOut = fOut;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001737 StringStream body;
1738 fOut = &body;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001739 for (const auto& e : fProgram) {
1740 this->writeProgramElement(e);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001741 }
1742 fOut = rawOut;
ethannicholasddb37d62016-10-20 09:54:00 -07001743
Ethan Nicholas88f6d372018-07-27 10:03:46 -04001744 write_stringstream(fExtensions, *rawOut);
Ethan Nicholascd700e92018-08-24 16:43:57 -04001745 this->writeInputVars();
Ethan Nicholas88f6d372018-07-27 10:03:46 -04001746 write_stringstream(fGlobals, *rawOut);
Brian Osmancc10d792018-07-20 13:09:45 -04001747
1748 if (!fProgram.fSettings.fCaps->canUseFragCoord()) {
1749 Layout layout;
1750 switch (fProgram.fKind) {
1751 case Program::kVertex_Kind: {
Ethan Nicholas858fecc2019-03-07 13:19:18 -05001752 Modifiers modifiers(layout, Modifiers::kOut_Flag);
Brian Osmancc10d792018-07-20 13:09:45 -04001753 this->writeModifiers(modifiers, true);
Ethan Nicholas858fecc2019-03-07 13:19:18 -05001754 if (this->usesPrecisionModifiers()) {
1755 this->write("highp ");
1756 }
Brian Osmancc10d792018-07-20 13:09:45 -04001757 this->write("vec4 sk_FragCoord_Workaround;\n");
1758 break;
1759 }
1760 case Program::kFragment_Kind: {
Ethan Nicholas858fecc2019-03-07 13:19:18 -05001761 Modifiers modifiers(layout, Modifiers::kIn_Flag);
Brian Osmancc10d792018-07-20 13:09:45 -04001762 this->writeModifiers(modifiers, true);
Ethan Nicholas858fecc2019-03-07 13:19:18 -05001763 if (this->usesPrecisionModifiers()) {
1764 this->write("highp ");
1765 }
Brian Osmancc10d792018-07-20 13:09:45 -04001766 this->write("vec4 sk_FragCoord_Workaround;\n");
1767 break;
1768 }
1769 default:
1770 break;
1771 }
1772 }
1773
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001774 if (this->usesPrecisionModifiers()) {
1775 this->writeLine("precision mediump float;");
Brian Salomon67529b22019-08-13 15:31:04 -04001776 this->writeLine("precision mediump sampler2D;");
Brian Salomon5a5f3e82019-08-16 15:05:40 -04001777 if (fFoundExternalSamplerDecl &&
1778 !fProgram.fSettings.fCaps->noDefaultPrecisionForExternalSamplers()) {
Brian Salomon67529b22019-08-13 15:31:04 -04001779 this->writeLine("precision mediump samplerExternalOES;");
1780 }
1781 if (fFoundRectSamplerDecl) {
1782 this->writeLine("precision mediump sampler2DRect;");
1783 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001784 }
Ethan Nicholas6e6525c2018-01-03 17:03:56 -05001785 write_stringstream(fExtraFunctions, *rawOut);
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001786 write_stringstream(body, *rawOut);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001787 return true;
ethannicholasf789b382016-08-03 12:43:36 -07001788}
1789
1790}