blob: 90694d2772cf2e7edf8395b3d0fca7e249e09f4d [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
ethannicholasf789b382016-08-03 12:43:36 -07008#include "SkSLGLSLCodeGenerator.h"
9
Ethan Nicholas941e7e22016-12-12 15:33:30 -050010#include "SkSLCompiler.h"
ethannicholasf789b382016-08-03 12:43:36 -070011#include "ir/SkSLExpressionStatement.h"
12#include "ir/SkSLExtension.h"
13#include "ir/SkSLIndexExpression.h"
ethannicholas5961bc92016-10-12 06:39:56 -070014#include "ir/SkSLModifiersDeclaration.h"
Ethan Nicholascb670962017-04-20 19:31:52 -040015#include "ir/SkSLNop.h"
ethannicholasf789b382016-08-03 12:43:36 -070016#include "ir/SkSLVariableReference.h"
17
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -040018#ifndef SKSL_STANDALONE
19#include "SkOnce.h"
20#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 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -040093 else if (component == *fContext.fInt_Type ||
94 component == *fContext.fShort_Type ||
95 component == *fContext.fByte_Type) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -040096 result = "ivec";
97 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -040098 else if (component == *fContext.fUInt_Type ||
99 component == *fContext.fUShort_Type ||
100 component == *fContext.fUByte_Type) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400101 result = "uvec";
102 }
103 else if (component == *fContext.fBool_Type) {
104 result = "bvec";
105 }
106 else {
107 ABORT("unsupported vector type");
108 }
109 result += to_string(type.columns());
110 return result;
111 }
112 case Type::kMatrix_Kind: {
113 String result;
114 Type component = type.componentType();
115 if (component == *fContext.fFloat_Type || component == *fContext.fHalf_Type) {
116 result = "mat";
117 }
118 else if (component == *fContext.fDouble_Type) {
119 result = "dmat";
120 }
121 else {
122 ABORT("unsupported matrix type");
123 }
124 result += to_string(type.columns());
125 if (type.columns() != type.rows()) {
126 result += "x";
127 result += to_string(type.rows());
128 }
129 return result;
130 }
131 case Type::kArray_Kind: {
132 String result = this->getTypeName(type.componentType()) + "[";
133 if (type.columns() != -1) {
134 result += to_string(type.columns());
135 }
136 result += "]";
137 return result;
138 }
139 case Type::kScalar_Kind: {
140 if (type == *fContext.fHalf_Type) {
141 return "float";
142 }
143 else if (type == *fContext.fShort_Type) {
144 return "int";
145 }
146 else if (type == *fContext.fUShort_Type) {
147 return "uint";
148 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400149 else if (type == *fContext.fByte_Type) {
150 return "int";
151 }
152 else if (type == *fContext.fUByte_Type) {
153 return "uint";
154 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400155 else {
156 return type.name();
157 }
158 break;
159 }
160 default:
161 return type.name();
162 }
163}
164
ethannicholasf789b382016-08-03 12:43:36 -0700165void GLSLCodeGenerator::writeType(const Type& type) {
166 if (type.kind() == Type::kStruct_Kind) {
167 for (const Type* search : fWrittenStructs) {
168 if (*search == type) {
169 // already written
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700170 this->write(type.fName);
ethannicholasf789b382016-08-03 12:43:36 -0700171 return;
172 }
173 }
174 fWrittenStructs.push_back(&type);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700175 this->write("struct ");
176 this->write(type.fName);
177 this->writeLine(" {");
ethannicholasf789b382016-08-03 12:43:36 -0700178 fIndentation++;
179 for (const auto& f : type.fields()) {
ethannicholas5961bc92016-10-12 06:39:56 -0700180 this->writeModifiers(f.fModifiers, false);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400181 this->writeTypePrecision(*f.fType);
ethannicholasf789b382016-08-03 12:43:36 -0700182 // sizes (which must be static in structs) are part of the type name here
ethannicholas0730be72016-09-01 07:59:02 -0700183 this->writeType(*f.fType);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700184 this->write(" ");
185 this->write(f.fName);
186 this->writeLine(";");
ethannicholasf789b382016-08-03 12:43:36 -0700187 }
188 fIndentation--;
Ethan Nicholas19671772016-11-28 16:30:17 -0500189 this->write("}");
ethannicholasf789b382016-08-03 12:43:36 -0700190 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400191 this->write(this->getTypeName(type));
ethannicholasf789b382016-08-03 12:43:36 -0700192 }
193}
194
195void GLSLCodeGenerator::writeExpression(const Expression& expr, Precedence parentPrecedence) {
196 switch (expr.fKind) {
197 case Expression::kBinary_Kind:
198 this->writeBinaryExpression((BinaryExpression&) expr, parentPrecedence);
199 break;
200 case Expression::kBoolLiteral_Kind:
201 this->writeBoolLiteral((BoolLiteral&) expr);
202 break;
203 case Expression::kConstructor_Kind:
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400204 this->writeConstructor((Constructor&) expr, parentPrecedence);
ethannicholasf789b382016-08-03 12:43:36 -0700205 break;
206 case Expression::kIntLiteral_Kind:
207 this->writeIntLiteral((IntLiteral&) expr);
208 break;
209 case Expression::kFieldAccess_Kind:
210 this->writeFieldAccess(((FieldAccess&) expr));
211 break;
212 case Expression::kFloatLiteral_Kind:
213 this->writeFloatLiteral(((FloatLiteral&) expr));
214 break;
215 case Expression::kFunctionCall_Kind:
216 this->writeFunctionCall((FunctionCall&) expr);
217 break;
218 case Expression::kPrefix_Kind:
219 this->writePrefixExpression((PrefixExpression&) expr, parentPrecedence);
220 break;
221 case Expression::kPostfix_Kind:
222 this->writePostfixExpression((PostfixExpression&) expr, parentPrecedence);
223 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400224 case Expression::kSetting_Kind:
225 this->writeSetting((Setting&) expr);
226 break;
ethannicholasf789b382016-08-03 12:43:36 -0700227 case Expression::kSwizzle_Kind:
228 this->writeSwizzle((Swizzle&) expr);
229 break;
230 case Expression::kVariableReference_Kind:
231 this->writeVariableReference((VariableReference&) expr);
232 break;
233 case Expression::kTernary_Kind:
234 this->writeTernaryExpression((TernaryExpression&) expr, parentPrecedence);
235 break;
236 case Expression::kIndex_Kind:
237 this->writeIndexExpression((IndexExpression&) expr);
238 break;
239 default:
240 ABORT("unsupported expression: %s", expr.description().c_str());
241 }
242}
243
ethannicholas5961bc92016-10-12 06:39:56 -0700244static bool is_abs(Expression& expr) {
245 if (expr.fKind != Expression::kFunctionCall_Kind) {
246 return false;
247 }
248 return ((FunctionCall&) expr).fFunction.fName == "abs";
249}
250
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500251// turns min(abs(x), y) into ((tmpVar1 = abs(x)) < (tmpVar2 = y) ? tmpVar1 : tmpVar2) to avoid a
ethannicholas5961bc92016-10-12 06:39:56 -0700252// Tegra3 compiler bug.
253void GLSLCodeGenerator::writeMinAbsHack(Expression& absExpr, Expression& otherExpr) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400254 SkASSERT(!fProgram.fSettings.fCaps->canUseMinAndAbsTogether());
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400255 String tmpVar1 = "minAbsHackVar" + to_string(fVarCount++);
256 String tmpVar2 = "minAbsHackVar" + to_string(fVarCount++);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400257 this->fFunctionHeader += String(" ") + this->getTypePrecision(absExpr.fType) +
258 this->getTypeName(absExpr.fType) + " " + tmpVar1 + ";\n";
259 this->fFunctionHeader += String(" ") + this->getTypePrecision(otherExpr.fType) +
260 this->getTypeName(otherExpr.fType) + " " + tmpVar2 + ";\n";
ethannicholas5961bc92016-10-12 06:39:56 -0700261 this->write("((" + tmpVar1 + " = ");
262 this->writeExpression(absExpr, kTopLevel_Precedence);
263 this->write(") < (" + tmpVar2 + " = ");
264 this->writeExpression(otherExpr, kAssignment_Precedence);
265 this->write(") ? " + tmpVar1 + " : " + tmpVar2 + ")");
266}
267
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500268void GLSLCodeGenerator::writeInverseSqrtHack(const Expression& x) {
269 this->write("(1.0 / sqrt(");
270 this->writeExpression(x, kTopLevel_Precedence);
271 this->write("))");
272}
273
274void GLSLCodeGenerator::writeDeterminantHack(const Expression& mat) {
275 String name;
276 if (mat.fType == *fContext.fFloat2x2_Type || mat.fType == *fContext.fHalf2x2_Type) {
277 name = "_determinant2";
278 if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
279 fWrittenIntrinsics.insert(name);
280 fExtraFunctions.writeText((
281 "float " + name + "(mat2 m) {"
282 " return m[0][0] * m[1][1] - m[0][1] * m[1][0];"
283 "}"
284 ).c_str());
285 }
286 }
287 else if (mat.fType == *fContext.fFloat3x3_Type || mat.fType == *fContext.fHalf3x3_Type) {
288 name = "_determinant3";
289 if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
290 fWrittenIntrinsics.insert(name);
291 fExtraFunctions.writeText((
292 "float " + name + "(mat3 m) {"
293 " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];"
294 " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];"
295 " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];"
296 " float b01 = a22 * a11 - a12 * a21;"
297 " float b11 = -a22 * a10 + a12 * a20;"
298 " float b21 = a21 * a10 - a11 * a20;"
299 " return a00 * b01 + a01 * b11 + a02 * b21;"
300 "}"
301 ).c_str());
302 }
303 }
304 else if (mat.fType == *fContext.fFloat4x4_Type || mat.fType == *fContext.fHalf4x4_Type) {
305 name = "_determinant3";
306 if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
307 fWrittenIntrinsics.insert(name);
308 fExtraFunctions.writeText((
309 "mat4 " + name + "(mat4 m) {"
310 " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3];"
311 " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3];"
312 " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2], a23 = m[2][3];"
313 " float a30 = m[3][0], a31 = m[3][1], a32 = m[3][2], a33 = m[3][3];"
314 " float b00 = a00 * a11 - a01 * a10;"
315 " float b01 = a00 * a12 - a02 * a10;"
316 " float b02 = a00 * a13 - a03 * a10;"
317 " float b03 = a01 * a12 - a02 * a11;"
318 " float b04 = a01 * a13 - a03 * a11;"
319 " float b05 = a02 * a13 - a03 * a12;"
320 " float b06 = a20 * a31 - a21 * a30;"
321 " float b07 = a20 * a32 - a22 * a30;"
322 " float b08 = a20 * a33 - a23 * a30;"
323 " float b09 = a21 * a32 - a22 * a31;"
324 " float b10 = a21 * a33 - a23 * a31;"
325 " float b11 = a22 * a33 - a23 * a32;"
326 " return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;"
327 "}"
328 ).c_str());
329 }
330 }
331 else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400332 SkASSERT(false);
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500333 }
334 this->write(name + "(");
335 this->writeExpression(mat, kTopLevel_Precedence);
336 this->write(")");
337}
338
339void GLSLCodeGenerator::writeInverseHack(const Expression& mat) {
340 String name;
341 if (mat.fType == *fContext.fFloat2x2_Type || mat.fType == *fContext.fHalf2x2_Type) {
342 name = "_inverse2";
343 if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
344 fWrittenIntrinsics.insert(name);
345 fExtraFunctions.writeText((
346 "mat2 " + name + "(mat2 m) {"
347 " return mat2(m[1][1], -m[0][1], -m[1][0], m[0][0]) / "
348 "(m[0][0] * m[1][1] - m[0][1] * m[1][0]);"
349 "}"
350 ).c_str());
351 }
352 }
353 else if (mat.fType == *fContext.fFloat3x3_Type || mat.fType == *fContext.fHalf3x3_Type) {
354 name = "_inverse3";
355 if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
356 fWrittenIntrinsics.insert(name);
357 fExtraFunctions.writeText((
358 "mat3 " + name + "(mat3 m) {"
359 " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];"
360 " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];"
361 " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];"
362 " float b01 = a22 * a11 - a12 * a21;"
363 " float b11 = -a22 * a10 + a12 * a20;"
364 " float b21 = a21 * a10 - a11 * a20;"
365 " float det = a00 * b01 + a01 * b11 + a02 * b21;"
366 " return mat3(b01, (-a22 * a01 + a02 * a21), (a12 * a01 - a02 * a11),"
367 " b11, (a22 * a00 - a02 * a20), (-a12 * a00 + a02 * a10),"
368 " b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10)) / det;"
369 "}"
370 ).c_str());
371 }
372 }
373 else if (mat.fType == *fContext.fFloat4x4_Type || mat.fType == *fContext.fHalf4x4_Type) {
374 name = "_inverse4";
375 if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
376 fWrittenIntrinsics.insert(name);
377 fExtraFunctions.writeText((
378 "mat4 " + name + "(mat4 m) {"
379 " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3];"
380 " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3];"
381 " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2], a23 = m[2][3];"
382 " float a30 = m[3][0], a31 = m[3][1], a32 = m[3][2], a33 = m[3][3];"
383 " float b00 = a00 * a11 - a01 * a10;"
384 " float b01 = a00 * a12 - a02 * a10;"
385 " float b02 = a00 * a13 - a03 * a10;"
386 " float b03 = a01 * a12 - a02 * a11;"
387 " float b04 = a01 * a13 - a03 * a11;"
388 " float b05 = a02 * a13 - a03 * a12;"
389 " float b06 = a20 * a31 - a21 * a30;"
390 " float b07 = a20 * a32 - a22 * a30;"
391 " float b08 = a20 * a33 - a23 * a30;"
392 " float b09 = a21 * a32 - a22 * a31;"
393 " float b10 = a21 * a33 - a23 * a31;"
394 " float b11 = a22 * a33 - a23 * a32;"
395 " float det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - "
396 " b04 * b07 + b05 * b06;"
397 " return mat4("
398 " a11 * b11 - a12 * b10 + a13 * b09,"
399 " a02 * b10 - a01 * b11 - a03 * b09,"
400 " a31 * b05 - a32 * b04 + a33 * b03,"
401 " a22 * b04 - a21 * b05 - a23 * b03,"
402 " a12 * b08 - a10 * b11 - a13 * b07,"
403 " a00 * b11 - a02 * b08 + a03 * b07,"
404 " a32 * b02 - a30 * b05 - a33 * b01,"
405 " a20 * b05 - a22 * b02 + a23 * b01,"
406 " a10 * b10 - a11 * b08 + a13 * b06,"
407 " a01 * b08 - a00 * b10 - a03 * b06,"
408 " a30 * b04 - a31 * b02 + a33 * b00,"
409 " a21 * b02 - a20 * b04 - a23 * b00,"
410 " a11 * b07 - a10 * b09 - a12 * b06,"
411 " a00 * b09 - a01 * b07 + a02 * b06,"
412 " a31 * b01 - a30 * b03 - a32 * b00,"
413 " a20 * b03 - a21 * b01 + a22 * b00) / det;"
414 "}"
415 ).c_str());
416 }
417 }
418 else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400419 SkASSERT(false);
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500420 }
421 this->write(name + "(");
422 this->writeExpression(mat, kTopLevel_Precedence);
423 this->write(")");
424}
425
426void GLSLCodeGenerator::writeTransposeHack(const Expression& mat) {
427 String name = "transpose" + to_string(mat.fType.columns()) + to_string(mat.fType.rows());
428 if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
429 fWrittenIntrinsics.insert(name);
430 String type = this->getTypeName(mat.fType);
431 const Type& base = mat.fType.componentType();
432 String transposed = this->getTypeName(base.toCompound(fContext,
433 mat.fType.rows(),
434 mat.fType.columns()));
435 fExtraFunctions.writeText((transposed + " " + name + "(" + type + " m) {\nreturn " +
436 transposed + "(").c_str());
437 const char* separator = "";
438 for (int row = 0; row < mat.fType.rows(); ++row) {
439 for (int column = 0; column < mat.fType.columns(); ++column) {
440 fExtraFunctions.writeText(separator);
441 fExtraFunctions.writeText(("m[" + to_string(column) + "][" + to_string(row) +
442 "]").c_str());
443 separator = ", ";
444 }
445 }
446 fExtraFunctions.writeText("); }");
447 }
448 this->write(name + "(");
449 this->writeExpression(mat, kTopLevel_Precedence);
450 this->write(")");
451}
452
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400453std::unordered_map<StringFragment, GLSLCodeGenerator::FunctionClass>*
454 GLSLCodeGenerator::fFunctionClasses = nullptr;
455
ethannicholasf789b382016-08-03 12:43:36 -0700456void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) {
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400457#ifdef SKSL_STANDALONE
458 if (!fFunctionClasses) {
459#else
460 static SkOnce once;
461 once([] {
462#endif
463 fFunctionClasses = new std::unordered_map<StringFragment, FunctionClass>();
464 (*fFunctionClasses)["atan"] = FunctionClass::kAtan;
465 (*fFunctionClasses)["determinant"] = FunctionClass::kDeterminant;
466 (*fFunctionClasses)["dFdx"] = FunctionClass::kDerivative;
467 (*fFunctionClasses)["dFdy"] = FunctionClass::kDerivative;
468 (*fFunctionClasses)["fract"] = FunctionClass::kFract;
469 (*fFunctionClasses)["inverse"] = FunctionClass::kInverse;
470 (*fFunctionClasses)["inverseSqrt"] = FunctionClass::kInverseSqrt;
471 (*fFunctionClasses)["min"] = FunctionClass::kMin;
472 (*fFunctionClasses)["saturate"] = FunctionClass::kSaturate;
473 (*fFunctionClasses)["texture"] = FunctionClass::kTexture;
474 (*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) {
485 case FunctionClass::kAtan:
486 if (fProgram.fSettings.fCaps->mustForceNegatedAtanParamToFloat() &&
487 c.fArguments.size() == 2 &&
488 c.fArguments[1]->fKind == Expression::kPrefix_Kind) {
489 const PrefixExpression& p = (PrefixExpression&) *c.fArguments[1];
490 if (p.fOperator == Token::MINUS) {
491 this->write("atan(");
492 this->writeExpression(*c.fArguments[0], kSequence_Precedence);
493 this->write(", -1.0 * ");
494 this->writeExpression(*p.fOperand, kMultiplicative_Precedence);
495 this->write(")");
496 return;
497 }
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500498 }
499 break;
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400500 case FunctionClass::kDerivative:
501 if (!fFoundDerivatives &&
502 fProgram.fSettings.fCaps->shaderDerivativeExtensionString()) {
503 SkASSERT(fProgram.fSettings.fCaps->shaderDerivativeSupport());
504 this->writeExtension(fProgram.fSettings.fCaps->shaderDerivativeExtensionString());
505 fFoundDerivatives = true;
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500506 }
507 break;
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400508 case FunctionClass::kDeterminant:
509 if (fProgram.fSettings.fCaps->generation() < k150_GrGLSLGeneration) {
510 SkASSERT(c.fArguments.size() == 1);
511 this->writeDeterminantHack(*c.fArguments[0]);
512 return;
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500513 }
514 break;
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400515 case FunctionClass::kFract:
516 if (!fProgram.fSettings.fCaps->canUseFractForNegativeValues()) {
517 SkASSERT(c.fArguments.size() == 1);
518 this->write("(0.5 - sign(");
519 this->writeExpression(*c.fArguments[0], kSequence_Precedence);
520 this->write(") * (0.5 - fract(abs(");
521 this->writeExpression(*c.fArguments[0], kSequence_Precedence);
522 this->write("))))");
523 return;
524 }
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500525 break;
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400526 case FunctionClass::kInverse:
527 if (fProgram.fSettings.fCaps->generation() < k140_GrGLSLGeneration) {
528 SkASSERT(c.fArguments.size() == 1);
529 this->writeInverseHack(*c.fArguments[0]);
530 return;
531 }
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500532 break;
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400533 case FunctionClass::kInverseSqrt:
534 if (fProgram.fSettings.fCaps->generation() < k130_GrGLSLGeneration) {
535 SkASSERT(c.fArguments.size() == 1);
536 this->writeInverseSqrtHack(*c.fArguments[0]);
537 return;
538 }
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500539 break;
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400540 case FunctionClass::kMin:
541 if (!fProgram.fSettings.fCaps->canUseMinAndAbsTogether()) {
542 SkASSERT(c.fArguments.size() == 2);
543 if (is_abs(*c.fArguments[0])) {
544 this->writeMinAbsHack(*c.fArguments[0], *c.fArguments[1]);
545 return;
546 }
547 if (is_abs(*c.fArguments[1])) {
548 // note that this violates the GLSL left-to-right evaluation semantics.
549 // I doubt it will ever end up mattering, but it's worth calling out.
550 this->writeMinAbsHack(*c.fArguments[1], *c.fArguments[0]);
551 return;
552 }
553 }
554 break;
555 case FunctionClass::kSaturate:
556 SkASSERT(c.fArguments.size() == 1);
557 this->write("clamp(");
558 this->writeExpression(*c.fArguments[0], kSequence_Precedence);
559 this->write(", 0.0, 1.0)");
560 return;
561 case FunctionClass::kTexture: {
562 const char* dim = "";
563 bool proj = false;
564 switch (c.fArguments[0]->fType.dimensions()) {
565 case SpvDim1D:
566 dim = "1D";
567 isTextureFunctionWithBias = true;
568 if (c.fArguments[1]->fType == *fContext.fFloat_Type) {
569 proj = false;
570 } else {
571 SkASSERT(c.fArguments[1]->fType == *fContext.fFloat2_Type);
572 proj = true;
573 }
574 break;
575 case SpvDim2D:
576 dim = "2D";
577 if (c.fArguments[0]->fType != *fContext.fSamplerExternalOES_Type) {
578 isTextureFunctionWithBias = true;
579 }
580 if (c.fArguments[1]->fType == *fContext.fFloat2_Type) {
581 proj = false;
582 } else {
583 SkASSERT(c.fArguments[1]->fType == *fContext.fFloat3_Type);
584 proj = true;
585 }
586 break;
587 case SpvDim3D:
588 dim = "3D";
589 isTextureFunctionWithBias = true;
590 if (c.fArguments[1]->fType == *fContext.fFloat3_Type) {
591 proj = false;
592 } else {
593 SkASSERT(c.fArguments[1]->fType == *fContext.fFloat4_Type);
594 proj = true;
595 }
596 break;
597 case SpvDimCube:
598 dim = "Cube";
599 isTextureFunctionWithBias = true;
600 proj = false;
601 break;
602 case SpvDimRect:
603 dim = "Rect";
604 proj = false;
605 break;
606 case SpvDimBuffer:
607 SkASSERT(false); // doesn't exist
608 dim = "Buffer";
609 proj = false;
610 break;
611 case SpvDimSubpassData:
612 SkASSERT(false); // doesn't exist
613 dim = "SubpassData";
614 proj = false;
615 break;
616 }
617 this->write("texture");
618 if (fProgram.fSettings.fCaps->generation() < k130_GrGLSLGeneration) {
619 this->write(dim);
620 }
621 if (proj) {
622 this->write("Proj");
623 }
624 nameWritten = true;
625 break;
626 }
627 case FunctionClass::kTranspose:
628 if (fProgram.fSettings.fCaps->generation() < k130_GrGLSLGeneration) {
629 SkASSERT(c.fArguments.size() == 1);
630 this->writeTransposeHack(*c.fArguments[0]);
631 return;
632 }
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500633 break;
634 }
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400635 }
636 if (!nameWritten) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500637 this->write(c.fFunction.fName);
638 }
639 this->write("(");
ethannicholasf789b382016-08-03 12:43:36 -0700640 const char* separator = "";
641 for (const auto& arg : c.fArguments) {
642 this->write(separator);
643 separator = ", ";
644 this->writeExpression(*arg, kSequence_Precedence);
645 }
Brian Osman8a83ca42018-02-12 14:32:17 -0500646 if (fProgram.fSettings.fSharpenTextures && isTextureFunctionWithBias) {
647 this->write(", -0.5");
648 }
ethannicholasf789b382016-08-03 12:43:36 -0700649 this->write(")");
650}
651
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400652void GLSLCodeGenerator::writeConstructor(const Constructor& c, Precedence parentPrecedence) {
653 if (c.fArguments.size() == 1 &&
654 this->getTypeName(c.fType) == this->getTypeName(c.fArguments[0]->fType)) {
655 // in cases like half(float), they're different types as far as SkSL is concerned but the
656 // same type as far as GLSL is concerned. We avoid a redundant float(float) by just writing
657 // out the inner expression here.
658 this->writeExpression(*c.fArguments[0], parentPrecedence);
659 return;
660 }
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400661 this->writeType(c.fType);
662 this->write("(");
ethannicholasf789b382016-08-03 12:43:36 -0700663 const char* separator = "";
664 for (const auto& arg : c.fArguments) {
665 this->write(separator);
666 separator = ", ";
667 this->writeExpression(*arg, kSequence_Precedence);
668 }
669 this->write(")");
670}
671
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500672void GLSLCodeGenerator::writeFragCoord() {
Brian Osmancd3261a2018-01-16 13:52:29 +0000673 if (!fProgram.fSettings.fCaps->canUseFragCoord()) {
Brian Salomondba65f92018-01-22 08:43:38 -0500674 if (!fSetupFragCoordWorkaround) {
675 const char* precision = usesPrecisionModifiers() ? "highp " : "";
676 fFunctionHeader += precision;
677 fFunctionHeader += " float sk_FragCoord_InvW = 1. / sk_FragCoord_Workaround.w;\n";
678 fFunctionHeader += precision;
679 fFunctionHeader += " vec4 sk_FragCoord_Resolved = "
680 "vec4(sk_FragCoord_Workaround.xyz * sk_FragCoord_InvW, sk_FragCoord_InvW);\n";
681 // Ensure that we get exact .5 values for x and y.
682 fFunctionHeader += " sk_FragCoord_Resolved.xy = floor(sk_FragCoord_Resolved.xy) + "
683 "vec2(.5);\n";
684 fSetupFragCoordWorkaround = true;
685 }
686 this->write("sk_FragCoord_Resolved");
Brian Osmancd3261a2018-01-16 13:52:29 +0000687 return;
688 }
689
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500690 // We only declare "gl_FragCoord" when we're in the case where we want to use layout qualifiers
691 // to reverse y. Otherwise it isn't necessary and whether the "in" qualifier appears in the
692 // declaration varies in earlier GLSL specs. So it is simpler to omit it.
693 if (!fProgram.fSettings.fFlipY) {
694 this->write("gl_FragCoord");
695 } else if (const char* extension =
Greg Daniele6ab9982018-08-22 13:56:32 +0000696 fProgram.fSettings.fCaps->fragCoordConventionsExtensionString()) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500697 if (!fSetupFragPositionGlobal) {
698 if (fProgram.fSettings.fCaps->generation() < k150_GrGLSLGeneration) {
Ethan Nicholas88f6d372018-07-27 10:03:46 -0400699 this->writeExtension(extension);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500700 }
Ethan Nicholas88f6d372018-07-27 10:03:46 -0400701 fGlobals.writeText("layout(origin_upper_left) in vec4 gl_FragCoord;\n");
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500702 fSetupFragPositionGlobal = true;
Greg Daniele8e4a3e2016-12-12 17:20:42 +0000703 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500704 this->write("gl_FragCoord");
Greg Daniele8e4a3e2016-12-12 17:20:42 +0000705 } else {
Greg Daniele6ab9982018-08-22 13:56:32 +0000706 if (!fSetupFragPositionGlobal) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500707 // The Adreno compiler seems to be very touchy about access to "gl_FragCoord".
708 // Accessing glFragCoord.zw can cause a program to fail to link. Additionally,
709 // depending on the surrounding code, accessing .xy with a uniform involved can
Brian Osmancd3261a2018-01-16 13:52:29 +0000710 // do the same thing. Copying gl_FragCoord.xy into a temp float2 beforehand
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500711 // (and only accessing .xy) seems to "fix" things.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400712 const char* precision = usesPrecisionModifiers() ? "highp " : "";
Greg Daniele6ab9982018-08-22 13:56:32 +0000713 fGlobals.writeText("uniform ");
714 fGlobals.writeText(precision);
715 fGlobals.writeText("float " SKSL_RTHEIGHT_NAME ";\n");
716 fSetupFragPositionGlobal = true;
717 }
718 if (!fSetupFragPositionLocal) {
719 const char* precision = usesPrecisionModifiers() ? "highp " : "";
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500720 fFunctionHeader += precision;
721 fFunctionHeader += " vec2 _sktmpCoord = gl_FragCoord.xy;\n";
722 fFunctionHeader += precision;
Greg Daniele6ab9982018-08-22 13:56:32 +0000723 fFunctionHeader += " vec4 sk_FragCoord = vec4(_sktmpCoord.x, " SKSL_RTHEIGHT_NAME
724 " - _sktmpCoord.y, 1.0, 1.0);\n";
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500725 fSetupFragPositionLocal = true;
726 }
727 this->write("sk_FragCoord");
728 }
729}
730
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500731void GLSLCodeGenerator::writeVariableReference(const VariableReference& ref) {
732 switch (ref.fVariable.fModifiers.fLayout.fBuiltin) {
733 case SK_FRAGCOLOR_BUILTIN:
734 if (fProgram.fSettings.fCaps->mustDeclareFragmentShaderOutput()) {
735 this->write("sk_FragColor");
736 } else {
737 this->write("gl_FragColor");
738 }
739 break;
740 case SK_FRAGCOORD_BUILTIN:
741 this->writeFragCoord();
742 break;
Chris Dalton49d14e92018-07-27 12:38:35 -0600743 case SK_CLOCKWISE_BUILTIN:
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600744 this->write(fProgram.fSettings.fFlipY ? "(!gl_FrontFacing)" : "gl_FrontFacing");
Chris Dalton49d14e92018-07-27 12:38:35 -0600745 break;
Ethan Nicholasa51740c2017-02-07 14:53:32 -0500746 case SK_VERTEXID_BUILTIN:
747 this->write("gl_VertexID");
748 break;
Chris Dalton8580d512017-10-14 22:12:33 -0600749 case SK_INSTANCEID_BUILTIN:
750 this->write("gl_InstanceID");
751 break;
Ethan Nicholas67d64602017-02-09 10:15:25 -0500752 case SK_CLIPDISTANCE_BUILTIN:
753 this->write("gl_ClipDistance");
754 break;
Ethan Nicholas52cad152017-02-16 16:37:32 -0500755 case SK_IN_BUILTIN:
756 this->write("gl_in");
757 break;
758 case SK_INVOCATIONID_BUILTIN:
759 this->write("gl_InvocationID");
760 break;
Ethan Nicholaseab2baa2018-04-13 15:16:27 -0400761 case SK_LASTFRAGCOLOR_BUILTIN:
762 this->write(fProgram.fSettings.fCaps->fbFetchColorName());
763 break;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500764 default:
765 this->write(ref.fVariable.fName);
ethannicholas5961bc92016-10-12 06:39:56 -0700766 }
ethannicholasf789b382016-08-03 12:43:36 -0700767}
768
769void GLSLCodeGenerator::writeIndexExpression(const IndexExpression& expr) {
770 this->writeExpression(*expr.fBase, kPostfix_Precedence);
771 this->write("[");
772 this->writeExpression(*expr.fIndex, kTopLevel_Precedence);
773 this->write("]");
774}
775
Brian Osmancd3261a2018-01-16 13:52:29 +0000776bool is_sk_position(const FieldAccess& f) {
777 return "sk_Position" == f.fBase->fType.fields()[f.fFieldIndex].fName;
778}
779
ethannicholasf789b382016-08-03 12:43:36 -0700780void GLSLCodeGenerator::writeFieldAccess(const FieldAccess& f) {
781 if (f.fOwnerKind == FieldAccess::kDefault_OwnerKind) {
782 this->writeExpression(*f.fBase, kPostfix_Precedence);
783 this->write(".");
784 }
Ethan Nicholas67d64602017-02-09 10:15:25 -0500785 switch (f.fBase->fType.fields()[f.fFieldIndex].fModifiers.fLayout.fBuiltin) {
786 case SK_CLIPDISTANCE_BUILTIN:
787 this->write("gl_ClipDistance");
788 break;
789 default:
Ethan Nicholasbed683a2017-09-26 14:23:59 -0400790 StringFragment name = f.fBase->fType.fields()[f.fFieldIndex].fName;
791 if (name == "sk_Position") {
792 this->write("gl_Position");
793 } else if (name == "sk_PointSize") {
794 this->write("gl_PointSize");
795 } else {
796 this->write(f.fBase->fType.fields()[f.fFieldIndex].fName);
797 }
Ethan Nicholas67d64602017-02-09 10:15:25 -0500798 }
ethannicholasf789b382016-08-03 12:43:36 -0700799}
800
801void GLSLCodeGenerator::writeSwizzle(const Swizzle& swizzle) {
802 this->writeExpression(*swizzle.fBase, kPostfix_Precedence);
803 this->write(".");
804 for (int c : swizzle.fComponents) {
805 this->write(&("x\0y\0z\0w\0"[c * 2]));
806 }
807}
808
Ethan Nicholas762466e2017-06-29 10:03:38 -0400809GLSLCodeGenerator::Precedence GLSLCodeGenerator::GetBinaryPrecedence(Token::Kind op) {
ethannicholasf789b382016-08-03 12:43:36 -0700810 switch (op) {
811 case Token::STAR: // fall through
812 case Token::SLASH: // fall through
813 case Token::PERCENT: return GLSLCodeGenerator::kMultiplicative_Precedence;
814 case Token::PLUS: // fall through
815 case Token::MINUS: return GLSLCodeGenerator::kAdditive_Precedence;
816 case Token::SHL: // fall through
817 case Token::SHR: return GLSLCodeGenerator::kShift_Precedence;
818 case Token::LT: // fall through
819 case Token::GT: // fall through
820 case Token::LTEQ: // fall through
821 case Token::GTEQ: return GLSLCodeGenerator::kRelational_Precedence;
822 case Token::EQEQ: // fall through
823 case Token::NEQ: return GLSLCodeGenerator::kEquality_Precedence;
824 case Token::BITWISEAND: return GLSLCodeGenerator::kBitwiseAnd_Precedence;
825 case Token::BITWISEXOR: return GLSLCodeGenerator::kBitwiseXor_Precedence;
826 case Token::BITWISEOR: return GLSLCodeGenerator::kBitwiseOr_Precedence;
827 case Token::LOGICALAND: return GLSLCodeGenerator::kLogicalAnd_Precedence;
828 case Token::LOGICALXOR: return GLSLCodeGenerator::kLogicalXor_Precedence;
829 case Token::LOGICALOR: return GLSLCodeGenerator::kLogicalOr_Precedence;
830 case Token::EQ: // fall through
831 case Token::PLUSEQ: // fall through
832 case Token::MINUSEQ: // fall through
833 case Token::STAREQ: // fall through
834 case Token::SLASHEQ: // fall through
835 case Token::PERCENTEQ: // fall through
836 case Token::SHLEQ: // fall through
837 case Token::SHREQ: // fall through
838 case Token::LOGICALANDEQ: // fall through
839 case Token::LOGICALXOREQ: // fall through
840 case Token::LOGICALOREQ: // fall through
841 case Token::BITWISEANDEQ: // fall through
842 case Token::BITWISEXOREQ: // fall through
843 case Token::BITWISEOREQ: return GLSLCodeGenerator::kAssignment_Precedence;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400844 case Token::COMMA: return GLSLCodeGenerator::kSequence_Precedence;
ethannicholasf789b382016-08-03 12:43:36 -0700845 default: ABORT("unsupported binary operator");
846 }
847}
848
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400849void GLSLCodeGenerator::writeBinaryExpression(const BinaryExpression& b,
ethannicholasf789b382016-08-03 12:43:36 -0700850 Precedence parentPrecedence) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400851 Precedence precedence = GetBinaryPrecedence(b.fOperator);
ethannicholasf789b382016-08-03 12:43:36 -0700852 if (precedence >= parentPrecedence) {
853 this->write("(");
854 }
Ethan Nicholas0b631962018-07-24 13:41:11 -0400855 bool positionWorkaround = fProgramKind == Program::Kind::kVertex_Kind &&
856 Compiler::IsAssignment(b.fOperator) &&
Brian Osmancd3261a2018-01-16 13:52:29 +0000857 Expression::kFieldAccess_Kind == b.fLeft->fKind &&
858 is_sk_position((FieldAccess&) *b.fLeft) &&
859 !strstr(b.fRight->description().c_str(), "sk_RTAdjust") &&
860 !fProgram.fSettings.fCaps->canUseFragCoord();
861 if (positionWorkaround) {
862 this->write("sk_FragCoord_Workaround = (");
863 }
ethannicholasf789b382016-08-03 12:43:36 -0700864 this->writeExpression(*b.fLeft, precedence);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700865 this->write(" ");
866 this->write(Compiler::OperatorName(b.fOperator));
867 this->write(" ");
ethannicholasf789b382016-08-03 12:43:36 -0700868 this->writeExpression(*b.fRight, precedence);
Brian Osmancd3261a2018-01-16 13:52:29 +0000869 if (positionWorkaround) {
870 this->write(")");
871 }
ethannicholasf789b382016-08-03 12:43:36 -0700872 if (precedence >= parentPrecedence) {
873 this->write(")");
874 }
875}
876
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400877void GLSLCodeGenerator::writeTernaryExpression(const TernaryExpression& t,
ethannicholasf789b382016-08-03 12:43:36 -0700878 Precedence parentPrecedence) {
879 if (kTernary_Precedence >= parentPrecedence) {
880 this->write("(");
881 }
882 this->writeExpression(*t.fTest, kTernary_Precedence);
883 this->write(" ? ");
884 this->writeExpression(*t.fIfTrue, kTernary_Precedence);
885 this->write(" : ");
886 this->writeExpression(*t.fIfFalse, kTernary_Precedence);
887 if (kTernary_Precedence >= parentPrecedence) {
888 this->write(")");
889 }
890}
891
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400892void GLSLCodeGenerator::writePrefixExpression(const PrefixExpression& p,
ethannicholasf789b382016-08-03 12:43:36 -0700893 Precedence parentPrecedence) {
894 if (kPrefix_Precedence >= parentPrecedence) {
895 this->write("(");
896 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700897 this->write(Compiler::OperatorName(p.fOperator));
ethannicholasf789b382016-08-03 12:43:36 -0700898 this->writeExpression(*p.fOperand, kPrefix_Precedence);
899 if (kPrefix_Precedence >= parentPrecedence) {
900 this->write(")");
901 }
902}
903
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400904void GLSLCodeGenerator::writePostfixExpression(const PostfixExpression& p,
ethannicholasf789b382016-08-03 12:43:36 -0700905 Precedence parentPrecedence) {
906 if (kPostfix_Precedence >= parentPrecedence) {
907 this->write("(");
908 }
909 this->writeExpression(*p.fOperand, kPostfix_Precedence);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700910 this->write(Compiler::OperatorName(p.fOperator));
ethannicholasf789b382016-08-03 12:43:36 -0700911 if (kPostfix_Precedence >= parentPrecedence) {
912 this->write(")");
913 }
914}
915
916void GLSLCodeGenerator::writeBoolLiteral(const BoolLiteral& b) {
917 this->write(b.fValue ? "true" : "false");
918}
919
920void GLSLCodeGenerator::writeIntLiteral(const IntLiteral& i) {
ethannicholas5961bc92016-10-12 06:39:56 -0700921 if (i.fType == *fContext.fUInt_Type) {
922 this->write(to_string(i.fValue & 0xffffffff) + "u");
Ethan Nicholas58d56482017-12-19 09:29:22 -0500923 } else if (i.fType == *fContext.fUShort_Type) {
924 this->write(to_string(i.fValue & 0xffff) + "u");
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400925 } else if (i.fType == *fContext.fUByte_Type) {
926 this->write(to_string(i.fValue & 0xff) + "u");
927 } else {
ethannicholas5961bc92016-10-12 06:39:56 -0700928 this->write(to_string((int32_t) i.fValue));
929 }
ethannicholasf789b382016-08-03 12:43:36 -0700930}
931
932void GLSLCodeGenerator::writeFloatLiteral(const FloatLiteral& f) {
933 this->write(to_string(f.fValue));
934}
935
Ethan Nicholas762466e2017-06-29 10:03:38 -0400936void GLSLCodeGenerator::writeSetting(const Setting& s) {
937 ABORT("internal error; setting was not folded to a constant during compilation\n");
938}
939
ethannicholasf789b382016-08-03 12:43:36 -0700940void GLSLCodeGenerator::writeFunction(const FunctionDefinition& f) {
Ethan Nicholas00543112018-07-31 09:44:36 -0400941 if (fProgramKind != Program::kPipelineStage_Kind) {
942 this->writeTypePrecision(f.fDeclaration.fReturnType);
943 this->writeType(f.fDeclaration.fReturnType);
944 this->write(" " + f.fDeclaration.fName + "(");
945 const char* separator = "";
946 for (const auto& param : f.fDeclaration.fParameters) {
947 this->write(separator);
948 separator = ", ";
949 this->writeModifiers(param->fModifiers, false);
950 std::vector<int> sizes;
951 const Type* type = &param->fType;
952 while (type->kind() == Type::kArray_Kind) {
953 sizes.push_back(type->columns());
954 type = &type->componentType();
955 }
956 this->writeTypePrecision(*type);
957 this->writeType(*type);
958 this->write(" " + param->fName);
959 for (int s : sizes) {
960 if (s <= 0) {
961 this->write("[]");
962 } else {
963 this->write("[" + to_string(s) + "]");
964 }
ethannicholas5961bc92016-10-12 06:39:56 -0700965 }
966 }
Ethan Nicholas00543112018-07-31 09:44:36 -0400967 this->writeLine(") {");
968 fIndentation++;
ethannicholasf789b382016-08-03 12:43:36 -0700969 }
ethannicholas5961bc92016-10-12 06:39:56 -0700970 fFunctionHeader = "";
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400971 OutputStream* oldOut = fOut;
972 StringStream buffer;
ethannicholas5961bc92016-10-12 06:39:56 -0700973 fOut = &buffer;
Ethan Nicholascb670962017-04-20 19:31:52 -0400974 this->writeStatements(((Block&) *f.fBody).fStatements);
Ethan Nicholas00543112018-07-31 09:44:36 -0400975 if (fProgramKind != Program::kPipelineStage_Kind) {
976 fIndentation--;
977 this->writeLine("}");
978 }
ethannicholas5961bc92016-10-12 06:39:56 -0700979
980 fOut = oldOut;
981 this->write(fFunctionHeader);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400982 this->write(buffer.str());
ethannicholasf789b382016-08-03 12:43:36 -0700983}
984
Greg Daniel64773e62016-11-22 09:44:03 -0500985void GLSLCodeGenerator::writeModifiers(const Modifiers& modifiers,
ethannicholas5961bc92016-10-12 06:39:56 -0700986 bool globalContext) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500987 if (modifiers.fFlags & Modifiers::kFlat_Flag) {
988 this->write("flat ");
989 }
ethannicholas5961bc92016-10-12 06:39:56 -0700990 if (modifiers.fFlags & Modifiers::kNoPerspective_Flag) {
991 this->write("noperspective ");
992 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400993 String layout = modifiers.fLayout.description();
Ethan Nicholas8da9e942017-03-09 16:35:09 -0500994 if (layout.size()) {
995 this->write(layout + " ");
996 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500997 if (modifiers.fFlags & Modifiers::kReadOnly_Flag) {
998 this->write("readonly ");
999 }
1000 if (modifiers.fFlags & Modifiers::kWriteOnly_Flag) {
1001 this->write("writeonly ");
1002 }
1003 if (modifiers.fFlags & Modifiers::kCoherent_Flag) {
1004 this->write("coherent ");
1005 }
1006 if (modifiers.fFlags & Modifiers::kVolatile_Flag) {
1007 this->write("volatile ");
1008 }
1009 if (modifiers.fFlags & Modifiers::kRestrict_Flag) {
1010 this->write("restrict ");
ethannicholas5961bc92016-10-12 06:39:56 -07001011 }
Greg Daniel64773e62016-11-22 09:44:03 -05001012 if ((modifiers.fFlags & Modifiers::kIn_Flag) &&
ethannicholas5961bc92016-10-12 06:39:56 -07001013 (modifiers.fFlags & Modifiers::kOut_Flag)) {
1014 this->write("inout ");
1015 } else if (modifiers.fFlags & Modifiers::kIn_Flag) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001016 if (globalContext &&
1017 fProgram.fSettings.fCaps->generation() < GrGLSLGeneration::k130_GrGLSLGeneration) {
ethannicholas5961bc92016-10-12 06:39:56 -07001018 this->write(fProgramKind == Program::kVertex_Kind ? "attribute "
1019 : "varying ");
1020 } else {
1021 this->write("in ");
1022 }
1023 } else if (modifiers.fFlags & Modifiers::kOut_Flag) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001024 if (globalContext &&
1025 fProgram.fSettings.fCaps->generation() < GrGLSLGeneration::k130_GrGLSLGeneration) {
ethannicholas5961bc92016-10-12 06:39:56 -07001026 this->write("varying ");
1027 } else {
1028 this->write("out ");
1029 }
1030 }
1031 if (modifiers.fFlags & Modifiers::kUniform_Flag) {
1032 this->write("uniform ");
1033 }
1034 if (modifiers.fFlags & Modifiers::kConst_Flag) {
1035 this->write("const ");
1036 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001037 if (usesPrecisionModifiers()) {
ethannicholas5961bc92016-10-12 06:39:56 -07001038 if (modifiers.fFlags & Modifiers::kLowp_Flag) {
1039 this->write("lowp ");
1040 }
1041 if (modifiers.fFlags & Modifiers::kMediump_Flag) {
1042 this->write("mediump ");
1043 }
1044 if (modifiers.fFlags & Modifiers::kHighp_Flag) {
1045 this->write("highp ");
1046 }
1047 }
ethannicholasf789b382016-08-03 12:43:36 -07001048}
1049
1050void GLSLCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) {
Ethan Nicholas52cad152017-02-16 16:37:32 -05001051 if (intf.fTypeName == "sk_PerVertex") {
ethannicholasf789b382016-08-03 12:43:36 -07001052 return;
1053 }
ethannicholas5961bc92016-10-12 06:39:56 -07001054 this->writeModifiers(intf.fVariable.fModifiers, true);
Ethan Nicholas50afc172017-02-16 14:49:57 -05001055 this->writeLine(intf.fTypeName + " {");
ethannicholasf789b382016-08-03 12:43:36 -07001056 fIndentation++;
Ethan Nicholas50afc172017-02-16 14:49:57 -05001057 const Type* structType = &intf.fVariable.fType;
1058 while (structType->kind() == Type::kArray_Kind) {
1059 structType = &structType->componentType();
1060 }
1061 for (const auto& f : structType->fields()) {
ethannicholas5961bc92016-10-12 06:39:56 -07001062 this->writeModifiers(f.fModifiers, false);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001063 this->writeTypePrecision(*f.fType);
ethannicholas0730be72016-09-01 07:59:02 -07001064 this->writeType(*f.fType);
ethannicholasf789b382016-08-03 12:43:36 -07001065 this->writeLine(" " + f.fName + ";");
1066 }
1067 fIndentation--;
Ethan Nicholas50afc172017-02-16 14:49:57 -05001068 this->write("}");
1069 if (intf.fInstanceName.size()) {
1070 this->write(" ");
1071 this->write(intf.fInstanceName);
1072 for (const auto& size : intf.fSizes) {
1073 this->write("[");
1074 if (size) {
1075 this->writeExpression(*size, kTopLevel_Precedence);
1076 }
1077 this->write("]");
1078 }
1079 }
1080 this->writeLine(";");
ethannicholasf789b382016-08-03 12:43:36 -07001081}
1082
Ethan Nicholas762466e2017-06-29 10:03:38 -04001083void GLSLCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) {
1084 this->writeExpression(value, kTopLevel_Precedence);
1085}
1086
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001087const char* GLSLCodeGenerator::getTypePrecision(const Type& type) {
1088 if (usesPrecisionModifiers()) {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001089 switch (type.kind()) {
1090 case Type::kScalar_Kind:
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001091 if (type == *fContext.fShort_Type || type == *fContext.fUShort_Type ||
1092 type == *fContext.fByte_Type || type == *fContext.fUByte_Type) {
Chris Daltonc2d0dd62018-03-07 07:46:10 -07001093 if (fProgram.fSettings.fForceHighPrecision ||
1094 fProgram.fSettings.fCaps->incompleteShortIntPrecision()) {
1095 return "highp ";
1096 }
1097 return "mediump ";
1098 }
1099 if (type == *fContext.fHalf_Type) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001100 return fProgram.fSettings.fForceHighPrecision ? "highp " : "mediump ";
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001101 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001102 if (type == *fContext.fFloat_Type || type == *fContext.fInt_Type ||
1103 type == *fContext.fUInt_Type) {
1104 return "highp ";
1105 }
1106 return "";
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001107 case Type::kVector_Kind: // fall through
1108 case Type::kMatrix_Kind:
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001109 return this->getTypePrecision(type.componentType());
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001110 default:
1111 break;
1112 }
1113 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001114 return "";
1115}
1116
1117void GLSLCodeGenerator::writeTypePrecision(const Type& type) {
1118 this->write(this->getTypePrecision(type));
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001119}
1120
ethannicholas5961bc92016-10-12 06:39:56 -07001121void GLSLCodeGenerator::writeVarDeclarations(const VarDeclarations& decl, bool global) {
Ethan Nicholas14efcbf2017-11-07 09:23:38 -05001122 if (!decl.fVars.size()) {
1123 return;
1124 }
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001125 bool wroteType = false;
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001126 for (const auto& stmt : decl.fVars) {
1127 VarDeclaration& var = (VarDeclaration&) *stmt;
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001128 if (wroteType) {
1129 this->write(", ");
1130 } else {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001131 this->writeModifiers(var.fVar->fModifiers, global);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001132 this->writeTypePrecision(decl.fBaseType);
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001133 this->writeType(decl.fBaseType);
1134 this->write(" ");
1135 wroteType = true;
1136 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001137 this->write(var.fVar->fName);
1138 for (const auto& size : var.fSizes) {
ethannicholasf789b382016-08-03 12:43:36 -07001139 this->write("[");
ethannicholas5961bc92016-10-12 06:39:56 -07001140 if (size) {
1141 this->writeExpression(*size, kTopLevel_Precedence);
1142 }
ethannicholasf789b382016-08-03 12:43:36 -07001143 this->write("]");
1144 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001145 if (var.fValue) {
ethannicholasf789b382016-08-03 12:43:36 -07001146 this->write(" = ");
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001147 this->writeVarInitializer(*var.fVar, *var.fValue);
ethannicholasf789b382016-08-03 12:43:36 -07001148 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001149 if (!fFoundImageDecl && var.fVar->fType == *fContext.fImage2D_Type) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001150 if (fProgram.fSettings.fCaps->imageLoadStoreExtensionString()) {
Ethan Nicholas88f6d372018-07-27 10:03:46 -04001151 this->writeExtension(fProgram.fSettings.fCaps->imageLoadStoreExtensionString());
Brian Salomon2a51de82016-11-16 12:06:01 -05001152 }
1153 fFoundImageDecl = true;
1154 }
Brian Osman4b2f9152018-04-17 11:19:57 -04001155 if (!fFoundExternalSamplerDecl && var.fVar->fType == *fContext.fSamplerExternalOES_Type) {
1156 if (fProgram.fSettings.fCaps->externalTextureExtensionString()) {
Ethan Nicholas88f6d372018-07-27 10:03:46 -04001157 this->writeExtension(fProgram.fSettings.fCaps->externalTextureExtensionString());
Brian Osman4b2f9152018-04-17 11:19:57 -04001158 }
Brian Osman061020e2018-04-17 14:22:15 -04001159 if (fProgram.fSettings.fCaps->secondExternalTextureExtensionString()) {
Ethan Nicholas88f6d372018-07-27 10:03:46 -04001160 this->writeExtension(
1161 fProgram.fSettings.fCaps->secondExternalTextureExtensionString());
Brian Osman061020e2018-04-17 14:22:15 -04001162 }
Brian Osman4b2f9152018-04-17 11:19:57 -04001163 fFoundExternalSamplerDecl = true;
1164 }
ethannicholasf789b382016-08-03 12:43:36 -07001165 }
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001166 if (wroteType) {
1167 this->write(";");
1168 }
ethannicholasf789b382016-08-03 12:43:36 -07001169}
1170
1171void GLSLCodeGenerator::writeStatement(const Statement& s) {
1172 switch (s.fKind) {
1173 case Statement::kBlock_Kind:
1174 this->writeBlock((Block&) s);
1175 break;
1176 case Statement::kExpression_Kind:
1177 this->writeExpression(*((ExpressionStatement&) s).fExpression, kTopLevel_Precedence);
1178 this->write(";");
1179 break;
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001180 case Statement::kReturn_Kind:
ethannicholasf789b382016-08-03 12:43:36 -07001181 this->writeReturnStatement((ReturnStatement&) s);
1182 break;
ethannicholas14fe8cc2016-09-07 13:37:16 -07001183 case Statement::kVarDeclarations_Kind:
ethannicholas5961bc92016-10-12 06:39:56 -07001184 this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclaration, false);
ethannicholasf789b382016-08-03 12:43:36 -07001185 break;
1186 case Statement::kIf_Kind:
1187 this->writeIfStatement((IfStatement&) s);
1188 break;
1189 case Statement::kFor_Kind:
1190 this->writeForStatement((ForStatement&) s);
1191 break;
1192 case Statement::kWhile_Kind:
1193 this->writeWhileStatement((WhileStatement&) s);
1194 break;
1195 case Statement::kDo_Kind:
1196 this->writeDoStatement((DoStatement&) s);
1197 break;
Ethan Nicholasaf197692017-02-27 13:26:45 -05001198 case Statement::kSwitch_Kind:
1199 this->writeSwitchStatement((SwitchStatement&) s);
1200 break;
ethannicholasf789b382016-08-03 12:43:36 -07001201 case Statement::kBreak_Kind:
1202 this->write("break;");
1203 break;
1204 case Statement::kContinue_Kind:
1205 this->write("continue;");
1206 break;
1207 case Statement::kDiscard_Kind:
1208 this->write("discard;");
1209 break;
Ethan Nicholascb670962017-04-20 19:31:52 -04001210 case Statement::kNop_Kind:
1211 this->write(";");
1212 break;
ethannicholasf789b382016-08-03 12:43:36 -07001213 default:
1214 ABORT("unsupported statement: %s", s.description().c_str());
1215 }
1216}
1217
Ethan Nicholascb670962017-04-20 19:31:52 -04001218void GLSLCodeGenerator::writeStatements(const std::vector<std::unique_ptr<Statement>>& statements) {
1219 for (const auto& s : statements) {
1220 if (!s->isEmpty()) {
1221 this->writeStatement(*s);
1222 this->writeLine();
1223 }
1224 }
1225}
1226
ethannicholasf789b382016-08-03 12:43:36 -07001227void GLSLCodeGenerator::writeBlock(const Block& b) {
1228 this->writeLine("{");
1229 fIndentation++;
Ethan Nicholascb670962017-04-20 19:31:52 -04001230 this->writeStatements(b.fStatements);
ethannicholasf789b382016-08-03 12:43:36 -07001231 fIndentation--;
1232 this->write("}");
1233}
1234
1235void GLSLCodeGenerator::writeIfStatement(const IfStatement& stmt) {
1236 this->write("if (");
1237 this->writeExpression(*stmt.fTest, kTopLevel_Precedence);
1238 this->write(") ");
1239 this->writeStatement(*stmt.fIfTrue);
1240 if (stmt.fIfFalse) {
1241 this->write(" else ");
1242 this->writeStatement(*stmt.fIfFalse);
1243 }
1244}
1245
1246void GLSLCodeGenerator::writeForStatement(const ForStatement& f) {
1247 this->write("for (");
Ethan Nicholasb310fd52017-06-09 13:46:34 -04001248 if (f.fInitializer && !f.fInitializer->isEmpty()) {
ethannicholasf789b382016-08-03 12:43:36 -07001249 this->writeStatement(*f.fInitializer);
1250 } else {
1251 this->write("; ");
1252 }
1253 if (f.fTest) {
Adrienne Walkeree8295c2018-08-21 10:56:30 -07001254 if (fProgram.fSettings.fCaps->addAndTrueToLoopCondition()) {
1255 std::unique_ptr<Expression> and_true(new BinaryExpression(
1256 -1, f.fTest->clone(), Token::LOGICALAND,
1257 std::unique_ptr<BoolLiteral>(new BoolLiteral(fContext, -1,
1258 true)),
1259 *fContext.fBool_Type));
1260 this->writeExpression(*and_true, kTopLevel_Precedence);
1261 } else {
1262 this->writeExpression(*f.fTest, kTopLevel_Precedence);
1263 }
ethannicholasf789b382016-08-03 12:43:36 -07001264 }
1265 this->write("; ");
1266 if (f.fNext) {
1267 this->writeExpression(*f.fNext, kTopLevel_Precedence);
1268 }
1269 this->write(") ");
1270 this->writeStatement(*f.fStatement);
1271}
1272
1273void GLSLCodeGenerator::writeWhileStatement(const WhileStatement& w) {
1274 this->write("while (");
1275 this->writeExpression(*w.fTest, kTopLevel_Precedence);
1276 this->write(") ");
1277 this->writeStatement(*w.fStatement);
1278}
1279
1280void GLSLCodeGenerator::writeDoStatement(const DoStatement& d) {
1281 this->write("do ");
1282 this->writeStatement(*d.fStatement);
1283 this->write(" while (");
1284 this->writeExpression(*d.fTest, kTopLevel_Precedence);
1285 this->write(");");
1286}
1287
Ethan Nicholasaf197692017-02-27 13:26:45 -05001288void GLSLCodeGenerator::writeSwitchStatement(const SwitchStatement& s) {
1289 this->write("switch (");
1290 this->writeExpression(*s.fValue, kTopLevel_Precedence);
1291 this->writeLine(") {");
1292 fIndentation++;
1293 for (const auto& c : s.fCases) {
1294 if (c->fValue) {
1295 this->write("case ");
1296 this->writeExpression(*c->fValue, kTopLevel_Precedence);
1297 this->writeLine(":");
1298 } else {
1299 this->writeLine("default:");
1300 }
1301 fIndentation++;
1302 for (const auto& stmt : c->fStatements) {
1303 this->writeStatement(*stmt);
1304 this->writeLine();
1305 }
1306 fIndentation--;
1307 }
1308 fIndentation--;
1309 this->write("}");
1310}
1311
ethannicholasf789b382016-08-03 12:43:36 -07001312void GLSLCodeGenerator::writeReturnStatement(const ReturnStatement& r) {
1313 this->write("return");
1314 if (r.fExpression) {
1315 this->write(" ");
1316 this->writeExpression(*r.fExpression, kTopLevel_Precedence);
1317 }
1318 this->write(";");
1319}
1320
Ethan Nicholas762466e2017-06-29 10:03:38 -04001321void GLSLCodeGenerator::writeHeader() {
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001322 this->write(fProgram.fSettings.fCaps->versionDeclString());
ethannicholasf789b382016-08-03 12:43:36 -07001323 this->writeLine();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001324}
1325
Ethan Nicholas762466e2017-06-29 10:03:38 -04001326void GLSLCodeGenerator::writeProgramElement(const ProgramElement& e) {
1327 switch (e.fKind) {
1328 case ProgramElement::kExtension_Kind:
Ethan Nicholas88f6d372018-07-27 10:03:46 -04001329 this->writeExtension(((Extension&) e).fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001330 break;
1331 case ProgramElement::kVar_Kind: {
1332 VarDeclarations& decl = (VarDeclarations&) e;
1333 if (decl.fVars.size() > 0) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001334 int builtin = ((VarDeclaration&) *decl.fVars[0]).fVar->fModifiers.fLayout.fBuiltin;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001335 if (builtin == -1) {
1336 // normal var
1337 this->writeVarDeclarations(decl, true);
1338 this->writeLine();
1339 } else if (builtin == SK_FRAGCOLOR_BUILTIN &&
1340 fProgram.fSettings.fCaps->mustDeclareFragmentShaderOutput()) {
Brian Salomondc092132018-04-04 10:14:16 -04001341 if (fProgram.fSettings.fFragColorIsInOut) {
1342 this->write("inout ");
1343 } else {
1344 this->write("out ");
1345 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001346 if (usesPrecisionModifiers()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001347 this->write("mediump ");
Mike Klein5ce39722017-06-27 22:52:03 +00001348 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001349 this->writeLine("vec4 sk_FragColor;");
Mike Klein5ce39722017-06-27 22:52:03 +00001350 }
Mike Klein5ce39722017-06-27 22:52:03 +00001351 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001352 break;
Mike Klein5ce39722017-06-27 22:52:03 +00001353 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001354 case ProgramElement::kInterfaceBlock_Kind:
1355 this->writeInterfaceBlock((InterfaceBlock&) e);
1356 break;
1357 case ProgramElement::kFunction_Kind:
1358 this->writeFunction((FunctionDefinition&) e);
1359 break;
Chris Daltonf1b47bb2017-10-06 11:57:51 -06001360 case ProgramElement::kModifiers_Kind: {
1361 const Modifiers& modifiers = ((ModifiersDeclaration&) e).fModifiers;
1362 if (!fFoundGSInvocations && modifiers.fLayout.fInvocations >= 0) {
1363 if (fProgram.fSettings.fCaps->gsInvocationsExtensionString()) {
Ethan Nicholas88f6d372018-07-27 10:03:46 -04001364 this->writeExtension(fProgram.fSettings.fCaps->gsInvocationsExtensionString());
Chris Daltonf1b47bb2017-10-06 11:57:51 -06001365 }
1366 fFoundGSInvocations = true;
1367 }
1368 this->writeModifiers(modifiers, true);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001369 this->writeLine(";");
1370 break;
Chris Daltonf1b47bb2017-10-06 11:57:51 -06001371 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001372 case ProgramElement::kEnum_Kind:
1373 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001374 default:
1375 printf("%s\n", e.description().c_str());
1376 ABORT("unsupported program element");
Ethan Nicholasc0709392017-06-27 11:20:22 -04001377 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001378}
1379
1380bool GLSLCodeGenerator::generateCode() {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001381 fProgramKind = fProgram.fKind;
Ethan Nicholas00543112018-07-31 09:44:36 -04001382 if (fProgramKind != Program::kPipelineStage_Kind) {
1383 this->writeHeader();
1384 }
Chris Dalton8fd79552018-01-11 00:46:14 -05001385 if (Program::kGeometry_Kind == fProgramKind &&
1386 fProgram.fSettings.fCaps->geometryShaderExtensionString()) {
Ethan Nicholas88f6d372018-07-27 10:03:46 -04001387 this->writeExtension(fProgram.fSettings.fCaps->geometryShaderExtensionString());
Chris Dalton8fd79552018-01-11 00:46:14 -05001388 }
Ethan Nicholas88f6d372018-07-27 10:03:46 -04001389 OutputStream* rawOut = fOut;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001390 StringStream body;
1391 fOut = &body;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001392 for (const auto& e : fProgram) {
1393 this->writeProgramElement(e);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001394 }
1395 fOut = rawOut;
ethannicholasddb37d62016-10-20 09:54:00 -07001396
Ethan Nicholas88f6d372018-07-27 10:03:46 -04001397 write_stringstream(fExtensions, *rawOut);
1398 write_stringstream(fGlobals, *rawOut);
Brian Osmancc10d792018-07-20 13:09:45 -04001399
1400 if (!fProgram.fSettings.fCaps->canUseFragCoord()) {
1401 Layout layout;
1402 switch (fProgram.fKind) {
1403 case Program::kVertex_Kind: {
1404 Modifiers modifiers(layout, Modifiers::kOut_Flag | Modifiers::kHighp_Flag);
1405 this->writeModifiers(modifiers, true);
1406 this->write("vec4 sk_FragCoord_Workaround;\n");
1407 break;
1408 }
1409 case Program::kFragment_Kind: {
1410 Modifiers modifiers(layout, Modifiers::kIn_Flag | Modifiers::kHighp_Flag);
1411 this->writeModifiers(modifiers, true);
1412 this->write("vec4 sk_FragCoord_Workaround;\n");
1413 break;
1414 }
1415 default:
1416 break;
1417 }
1418 }
1419
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001420 if (this->usesPrecisionModifiers()) {
1421 this->writeLine("precision mediump float;");
1422 }
Ethan Nicholas6e6525c2018-01-03 17:03:56 -05001423 write_stringstream(fExtraFunctions, *rawOut);
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001424 write_stringstream(body, *rawOut);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001425 return true;
ethannicholasf789b382016-08-03 12:43:36 -07001426}
1427
1428}