blob: 5f6fedcdb5cedb4cc105a3796ba13e4a9bba1df9 [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
18namespace SkSL {
19
20void GLSLCodeGenerator::write(const char* s) {
21 if (s[0] == 0) {
22 return;
23 }
24 if (fAtLineStart) {
25 for (int i = 0; i < fIndentation; i++) {
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050026 fOut->writeText(" ");
ethannicholasf789b382016-08-03 12:43:36 -070027 }
28 }
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050029 fOut->writeText(s);
ethannicholasf789b382016-08-03 12:43:36 -070030 fAtLineStart = false;
31}
32
33void GLSLCodeGenerator::writeLine(const char* s) {
34 this->write(s);
Ethan Nicholas762466e2017-06-29 10:03:38 -040035 fOut->writeText(fLineEnding);
ethannicholasf789b382016-08-03 12:43:36 -070036 fAtLineStart = true;
37}
38
Ethan Nicholas0df1b042017-03-31 13:56:23 -040039void GLSLCodeGenerator::write(const String& s) {
ethannicholasf789b382016-08-03 12:43:36 -070040 this->write(s.c_str());
41}
42
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070043void GLSLCodeGenerator::write(StringFragment s) {
44 if (!s.fLength) {
45 return;
46 }
47 if (fAtLineStart) {
48 for (int i = 0; i < fIndentation; i++) {
49 fOut->writeText(" ");
50 }
51 }
52 fOut->write(s.fChars, s.fLength);
53 fAtLineStart = false;
54}
55
Ethan Nicholas0df1b042017-03-31 13:56:23 -040056void GLSLCodeGenerator::writeLine(const String& s) {
ethannicholasf789b382016-08-03 12:43:36 -070057 this->writeLine(s.c_str());
58}
59
60void GLSLCodeGenerator::writeLine() {
61 this->writeLine("");
62}
63
64void GLSLCodeGenerator::writeExtension(const Extension& ext) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070065 this->write("#extension ");
66 this->write(ext.fName);
67 this->writeLine(" : enable");
ethannicholasf789b382016-08-03 12:43:36 -070068}
69
Ethan Nicholasf7b88202017-09-18 14:10:39 -040070bool GLSLCodeGenerator::usesPrecisionModifiers() const {
71 return fProgram.fSettings.fCaps->usesPrecisionModifiers();
72}
73
74String GLSLCodeGenerator::getTypeName(const Type& type) {
75 switch (type.kind()) {
76 case Type::kVector_Kind: {
77 Type component = type.componentType();
78 String result;
79 if (component == *fContext.fFloat_Type || component == *fContext.fHalf_Type) {
80 result = "vec";
81 }
82 else if (component == *fContext.fDouble_Type) {
83 result = "dvec";
84 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -040085 else if (component == *fContext.fInt_Type ||
86 component == *fContext.fShort_Type ||
87 component == *fContext.fByte_Type) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -040088 result = "ivec";
89 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -040090 else if (component == *fContext.fUInt_Type ||
91 component == *fContext.fUShort_Type ||
92 component == *fContext.fUByte_Type) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -040093 result = "uvec";
94 }
95 else if (component == *fContext.fBool_Type) {
96 result = "bvec";
97 }
98 else {
99 ABORT("unsupported vector type");
100 }
101 result += to_string(type.columns());
102 return result;
103 }
104 case Type::kMatrix_Kind: {
105 String result;
106 Type component = type.componentType();
107 if (component == *fContext.fFloat_Type || component == *fContext.fHalf_Type) {
108 result = "mat";
109 }
110 else if (component == *fContext.fDouble_Type) {
111 result = "dmat";
112 }
113 else {
114 ABORT("unsupported matrix type");
115 }
116 result += to_string(type.columns());
117 if (type.columns() != type.rows()) {
118 result += "x";
119 result += to_string(type.rows());
120 }
121 return result;
122 }
123 case Type::kArray_Kind: {
124 String result = this->getTypeName(type.componentType()) + "[";
125 if (type.columns() != -1) {
126 result += to_string(type.columns());
127 }
128 result += "]";
129 return result;
130 }
131 case Type::kScalar_Kind: {
132 if (type == *fContext.fHalf_Type) {
133 return "float";
134 }
135 else if (type == *fContext.fShort_Type) {
136 return "int";
137 }
138 else if (type == *fContext.fUShort_Type) {
139 return "uint";
140 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400141 else if (type == *fContext.fByte_Type) {
142 return "int";
143 }
144 else if (type == *fContext.fUByte_Type) {
145 return "uint";
146 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400147 else {
148 return type.name();
149 }
150 break;
151 }
152 default:
153 return type.name();
154 }
155}
156
ethannicholasf789b382016-08-03 12:43:36 -0700157void GLSLCodeGenerator::writeType(const Type& type) {
158 if (type.kind() == Type::kStruct_Kind) {
159 for (const Type* search : fWrittenStructs) {
160 if (*search == type) {
161 // already written
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700162 this->write(type.fName);
ethannicholasf789b382016-08-03 12:43:36 -0700163 return;
164 }
165 }
166 fWrittenStructs.push_back(&type);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700167 this->write("struct ");
168 this->write(type.fName);
169 this->writeLine(" {");
ethannicholasf789b382016-08-03 12:43:36 -0700170 fIndentation++;
171 for (const auto& f : type.fields()) {
ethannicholas5961bc92016-10-12 06:39:56 -0700172 this->writeModifiers(f.fModifiers, false);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400173 this->writeTypePrecision(*f.fType);
ethannicholasf789b382016-08-03 12:43:36 -0700174 // sizes (which must be static in structs) are part of the type name here
ethannicholas0730be72016-09-01 07:59:02 -0700175 this->writeType(*f.fType);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700176 this->write(" ");
177 this->write(f.fName);
178 this->writeLine(";");
ethannicholasf789b382016-08-03 12:43:36 -0700179 }
180 fIndentation--;
Ethan Nicholas19671772016-11-28 16:30:17 -0500181 this->write("}");
ethannicholasf789b382016-08-03 12:43:36 -0700182 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400183 this->write(this->getTypeName(type));
ethannicholasf789b382016-08-03 12:43:36 -0700184 }
185}
186
187void GLSLCodeGenerator::writeExpression(const Expression& expr, Precedence parentPrecedence) {
188 switch (expr.fKind) {
189 case Expression::kBinary_Kind:
190 this->writeBinaryExpression((BinaryExpression&) expr, parentPrecedence);
191 break;
192 case Expression::kBoolLiteral_Kind:
193 this->writeBoolLiteral((BoolLiteral&) expr);
194 break;
195 case Expression::kConstructor_Kind:
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400196 this->writeConstructor((Constructor&) expr, parentPrecedence);
ethannicholasf789b382016-08-03 12:43:36 -0700197 break;
198 case Expression::kIntLiteral_Kind:
199 this->writeIntLiteral((IntLiteral&) expr);
200 break;
201 case Expression::kFieldAccess_Kind:
202 this->writeFieldAccess(((FieldAccess&) expr));
203 break;
204 case Expression::kFloatLiteral_Kind:
205 this->writeFloatLiteral(((FloatLiteral&) expr));
206 break;
207 case Expression::kFunctionCall_Kind:
208 this->writeFunctionCall((FunctionCall&) expr);
209 break;
210 case Expression::kPrefix_Kind:
211 this->writePrefixExpression((PrefixExpression&) expr, parentPrecedence);
212 break;
213 case Expression::kPostfix_Kind:
214 this->writePostfixExpression((PostfixExpression&) expr, parentPrecedence);
215 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400216 case Expression::kSetting_Kind:
217 this->writeSetting((Setting&) expr);
218 break;
ethannicholasf789b382016-08-03 12:43:36 -0700219 case Expression::kSwizzle_Kind:
220 this->writeSwizzle((Swizzle&) expr);
221 break;
222 case Expression::kVariableReference_Kind:
223 this->writeVariableReference((VariableReference&) expr);
224 break;
225 case Expression::kTernary_Kind:
226 this->writeTernaryExpression((TernaryExpression&) expr, parentPrecedence);
227 break;
228 case Expression::kIndex_Kind:
229 this->writeIndexExpression((IndexExpression&) expr);
230 break;
231 default:
232 ABORT("unsupported expression: %s", expr.description().c_str());
233 }
234}
235
ethannicholas5961bc92016-10-12 06:39:56 -0700236static bool is_abs(Expression& expr) {
237 if (expr.fKind != Expression::kFunctionCall_Kind) {
238 return false;
239 }
240 return ((FunctionCall&) expr).fFunction.fName == "abs";
241}
242
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500243// turns min(abs(x), y) into ((tmpVar1 = abs(x)) < (tmpVar2 = y) ? tmpVar1 : tmpVar2) to avoid a
ethannicholas5961bc92016-10-12 06:39:56 -0700244// Tegra3 compiler bug.
245void GLSLCodeGenerator::writeMinAbsHack(Expression& absExpr, Expression& otherExpr) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400246 SkASSERT(!fProgram.fSettings.fCaps->canUseMinAndAbsTogether());
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400247 String tmpVar1 = "minAbsHackVar" + to_string(fVarCount++);
248 String tmpVar2 = "minAbsHackVar" + to_string(fVarCount++);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400249 this->fFunctionHeader += String(" ") + this->getTypePrecision(absExpr.fType) +
250 this->getTypeName(absExpr.fType) + " " + tmpVar1 + ";\n";
251 this->fFunctionHeader += String(" ") + this->getTypePrecision(otherExpr.fType) +
252 this->getTypeName(otherExpr.fType) + " " + tmpVar2 + ";\n";
ethannicholas5961bc92016-10-12 06:39:56 -0700253 this->write("((" + tmpVar1 + " = ");
254 this->writeExpression(absExpr, kTopLevel_Precedence);
255 this->write(") < (" + tmpVar2 + " = ");
256 this->writeExpression(otherExpr, kAssignment_Precedence);
257 this->write(") ? " + tmpVar1 + " : " + tmpVar2 + ")");
258}
259
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500260void GLSLCodeGenerator::writeInverseSqrtHack(const Expression& x) {
261 this->write("(1.0 / sqrt(");
262 this->writeExpression(x, kTopLevel_Precedence);
263 this->write("))");
264}
265
266void GLSLCodeGenerator::writeDeterminantHack(const Expression& mat) {
267 String name;
268 if (mat.fType == *fContext.fFloat2x2_Type || mat.fType == *fContext.fHalf2x2_Type) {
269 name = "_determinant2";
270 if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
271 fWrittenIntrinsics.insert(name);
272 fExtraFunctions.writeText((
273 "float " + name + "(mat2 m) {"
274 " return m[0][0] * m[1][1] - m[0][1] * m[1][0];"
275 "}"
276 ).c_str());
277 }
278 }
279 else if (mat.fType == *fContext.fFloat3x3_Type || mat.fType == *fContext.fHalf3x3_Type) {
280 name = "_determinant3";
281 if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
282 fWrittenIntrinsics.insert(name);
283 fExtraFunctions.writeText((
284 "float " + name + "(mat3 m) {"
285 " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];"
286 " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];"
287 " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];"
288 " float b01 = a22 * a11 - a12 * a21;"
289 " float b11 = -a22 * a10 + a12 * a20;"
290 " float b21 = a21 * a10 - a11 * a20;"
291 " return a00 * b01 + a01 * b11 + a02 * b21;"
292 "}"
293 ).c_str());
294 }
295 }
296 else if (mat.fType == *fContext.fFloat4x4_Type || mat.fType == *fContext.fHalf4x4_Type) {
297 name = "_determinant3";
298 if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
299 fWrittenIntrinsics.insert(name);
300 fExtraFunctions.writeText((
301 "mat4 " + name + "(mat4 m) {"
302 " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3];"
303 " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3];"
304 " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2], a23 = m[2][3];"
305 " float a30 = m[3][0], a31 = m[3][1], a32 = m[3][2], a33 = m[3][3];"
306 " float b00 = a00 * a11 - a01 * a10;"
307 " float b01 = a00 * a12 - a02 * a10;"
308 " float b02 = a00 * a13 - a03 * a10;"
309 " float b03 = a01 * a12 - a02 * a11;"
310 " float b04 = a01 * a13 - a03 * a11;"
311 " float b05 = a02 * a13 - a03 * a12;"
312 " float b06 = a20 * a31 - a21 * a30;"
313 " float b07 = a20 * a32 - a22 * a30;"
314 " float b08 = a20 * a33 - a23 * a30;"
315 " float b09 = a21 * a32 - a22 * a31;"
316 " float b10 = a21 * a33 - a23 * a31;"
317 " float b11 = a22 * a33 - a23 * a32;"
318 " return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;"
319 "}"
320 ).c_str());
321 }
322 }
323 else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400324 SkASSERT(false);
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500325 }
326 this->write(name + "(");
327 this->writeExpression(mat, kTopLevel_Precedence);
328 this->write(")");
329}
330
331void GLSLCodeGenerator::writeInverseHack(const Expression& mat) {
332 String name;
333 if (mat.fType == *fContext.fFloat2x2_Type || mat.fType == *fContext.fHalf2x2_Type) {
334 name = "_inverse2";
335 if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
336 fWrittenIntrinsics.insert(name);
337 fExtraFunctions.writeText((
338 "mat2 " + name + "(mat2 m) {"
339 " return mat2(m[1][1], -m[0][1], -m[1][0], m[0][0]) / "
340 "(m[0][0] * m[1][1] - m[0][1] * m[1][0]);"
341 "}"
342 ).c_str());
343 }
344 }
345 else if (mat.fType == *fContext.fFloat3x3_Type || mat.fType == *fContext.fHalf3x3_Type) {
346 name = "_inverse3";
347 if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
348 fWrittenIntrinsics.insert(name);
349 fExtraFunctions.writeText((
350 "mat3 " + name + "(mat3 m) {"
351 " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];"
352 " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];"
353 " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];"
354 " float b01 = a22 * a11 - a12 * a21;"
355 " float b11 = -a22 * a10 + a12 * a20;"
356 " float b21 = a21 * a10 - a11 * a20;"
357 " float det = a00 * b01 + a01 * b11 + a02 * b21;"
358 " return mat3(b01, (-a22 * a01 + a02 * a21), (a12 * a01 - a02 * a11),"
359 " b11, (a22 * a00 - a02 * a20), (-a12 * a00 + a02 * a10),"
360 " b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10)) / det;"
361 "}"
362 ).c_str());
363 }
364 }
365 else if (mat.fType == *fContext.fFloat4x4_Type || mat.fType == *fContext.fHalf4x4_Type) {
366 name = "_inverse4";
367 if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
368 fWrittenIntrinsics.insert(name);
369 fExtraFunctions.writeText((
370 "mat4 " + name + "(mat4 m) {"
371 " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3];"
372 " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3];"
373 " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2], a23 = m[2][3];"
374 " float a30 = m[3][0], a31 = m[3][1], a32 = m[3][2], a33 = m[3][3];"
375 " float b00 = a00 * a11 - a01 * a10;"
376 " float b01 = a00 * a12 - a02 * a10;"
377 " float b02 = a00 * a13 - a03 * a10;"
378 " float b03 = a01 * a12 - a02 * a11;"
379 " float b04 = a01 * a13 - a03 * a11;"
380 " float b05 = a02 * a13 - a03 * a12;"
381 " float b06 = a20 * a31 - a21 * a30;"
382 " float b07 = a20 * a32 - a22 * a30;"
383 " float b08 = a20 * a33 - a23 * a30;"
384 " float b09 = a21 * a32 - a22 * a31;"
385 " float b10 = a21 * a33 - a23 * a31;"
386 " float b11 = a22 * a33 - a23 * a32;"
387 " float det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - "
388 " b04 * b07 + b05 * b06;"
389 " return mat4("
390 " a11 * b11 - a12 * b10 + a13 * b09,"
391 " a02 * b10 - a01 * b11 - a03 * b09,"
392 " a31 * b05 - a32 * b04 + a33 * b03,"
393 " a22 * b04 - a21 * b05 - a23 * b03,"
394 " a12 * b08 - a10 * b11 - a13 * b07,"
395 " a00 * b11 - a02 * b08 + a03 * b07,"
396 " a32 * b02 - a30 * b05 - a33 * b01,"
397 " a20 * b05 - a22 * b02 + a23 * b01,"
398 " a10 * b10 - a11 * b08 + a13 * b06,"
399 " a01 * b08 - a00 * b10 - a03 * b06,"
400 " a30 * b04 - a31 * b02 + a33 * b00,"
401 " a21 * b02 - a20 * b04 - a23 * b00,"
402 " a11 * b07 - a10 * b09 - a12 * b06,"
403 " a00 * b09 - a01 * b07 + a02 * b06,"
404 " a31 * b01 - a30 * b03 - a32 * b00,"
405 " a20 * b03 - a21 * b01 + a22 * b00) / det;"
406 "}"
407 ).c_str());
408 }
409 }
410 else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400411 SkASSERT(false);
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500412 }
413 this->write(name + "(");
414 this->writeExpression(mat, kTopLevel_Precedence);
415 this->write(")");
416}
417
418void GLSLCodeGenerator::writeTransposeHack(const Expression& mat) {
419 String name = "transpose" + to_string(mat.fType.columns()) + to_string(mat.fType.rows());
420 if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
421 fWrittenIntrinsics.insert(name);
422 String type = this->getTypeName(mat.fType);
423 const Type& base = mat.fType.componentType();
424 String transposed = this->getTypeName(base.toCompound(fContext,
425 mat.fType.rows(),
426 mat.fType.columns()));
427 fExtraFunctions.writeText((transposed + " " + name + "(" + type + " m) {\nreturn " +
428 transposed + "(").c_str());
429 const char* separator = "";
430 for (int row = 0; row < mat.fType.rows(); ++row) {
431 for (int column = 0; column < mat.fType.columns(); ++column) {
432 fExtraFunctions.writeText(separator);
433 fExtraFunctions.writeText(("m[" + to_string(column) + "][" + to_string(row) +
434 "]").c_str());
435 separator = ", ";
436 }
437 }
438 fExtraFunctions.writeText("); }");
439 }
440 this->write(name + "(");
441 this->writeExpression(mat, kTopLevel_Precedence);
442 this->write(")");
443}
444
ethannicholasf789b382016-08-03 12:43:36 -0700445void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500446 if (!fProgram.fSettings.fCaps->canUseMinAndAbsTogether() && c.fFunction.fName == "min" &&
447 c.fFunction.fBuiltin) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400448 SkASSERT(c.fArguments.size() == 2);
ethannicholas5961bc92016-10-12 06:39:56 -0700449 if (is_abs(*c.fArguments[0])) {
450 this->writeMinAbsHack(*c.fArguments[0], *c.fArguments[1]);
451 return;
452 }
453 if (is_abs(*c.fArguments[1])) {
454 // note that this violates the GLSL left-to-right evaluation semantics. I doubt it will
455 // ever end up mattering, but it's worth calling out.
456 this->writeMinAbsHack(*c.fArguments[1], *c.fArguments[0]);
457 return;
458 }
459 }
Florin Malita3b30c4f2017-08-08 15:47:35 -0400460 if (!fProgram.fSettings.fCaps->canUseFractForNegativeValues() && c.fFunction.fName == "fract" &&
461 c.fFunction.fBuiltin) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400462 SkASSERT(c.fArguments.size() == 1);
Florin Malita3b30c4f2017-08-08 15:47:35 -0400463
464 this->write("(0.5 - sign(");
465 this->writeExpression(*c.fArguments[0], kSequence_Precedence);
466 this->write(") * (0.5 - fract(abs(");
467 this->writeExpression(*c.fArguments[0], kSequence_Precedence);
468 this->write("))))");
469
470 return;
471 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500472 if (fProgram.fSettings.fCaps->mustForceNegatedAtanParamToFloat() &&
473 c.fFunction.fName == "atan" &&
474 c.fFunction.fBuiltin && c.fArguments.size() == 2 &&
ethannicholasddb37d62016-10-20 09:54:00 -0700475 c.fArguments[1]->fKind == Expression::kPrefix_Kind) {
ethannicholasad146f62016-10-14 06:40:02 -0700476 const PrefixExpression& p = (PrefixExpression&) *c.fArguments[1];
477 if (p.fOperator == Token::MINUS) {
478 this->write("atan(");
479 this->writeExpression(*c.fArguments[0], kSequence_Precedence);
480 this->write(", -1.0 * ");
481 this->writeExpression(*p.fOperand, kMultiplicative_Precedence);
482 this->write(")");
483 return;
484 }
485 }
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500486 if (c.fFunction.fBuiltin && c.fFunction.fName == "determinant" &&
487 fProgram.fSettings.fCaps->generation() < k150_GrGLSLGeneration) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400488 SkASSERT(c.fArguments.size() == 1);
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500489 this->writeDeterminantHack(*c.fArguments[0]);
490 return;
491 }
492 if (c.fFunction.fBuiltin && c.fFunction.fName == "inverse" &&
493 fProgram.fSettings.fCaps->generation() < k140_GrGLSLGeneration) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400494 SkASSERT(c.fArguments.size() == 1);
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500495 this->writeInverseHack(*c.fArguments[0]);
496 return;
497 }
498 if (c.fFunction.fBuiltin && c.fFunction.fName == "inverseSqrt" &&
499 fProgram.fSettings.fCaps->generation() < k130_GrGLSLGeneration) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400500 SkASSERT(c.fArguments.size() == 1);
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500501 this->writeInverseSqrtHack(*c.fArguments[0]);
502 return;
503 }
504 if (c.fFunction.fBuiltin && c.fFunction.fName == "transpose" &&
505 fProgram.fSettings.fCaps->generation() < k130_GrGLSLGeneration) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400506 SkASSERT(c.fArguments.size() == 1);
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500507 this->writeTransposeHack(*c.fArguments[0]);
508 return;
509 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500510 if (!fFoundDerivatives && (c.fFunction.fName == "dFdx" || c.fFunction.fName == "dFdy") &&
511 c.fFunction.fBuiltin && fProgram.fSettings.fCaps->shaderDerivativeExtensionString()) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400512 SkASSERT(fProgram.fSettings.fCaps->shaderDerivativeSupport());
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500513 fHeader.writeText("#extension ");
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500514 fHeader.writeText(fProgram.fSettings.fCaps->shaderDerivativeExtensionString());
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500515 fHeader.writeText(" : require\n");
ethannicholasddb37d62016-10-20 09:54:00 -0700516 fFoundDerivatives = true;
517 }
Brian Osman8a83ca42018-02-12 14:32:17 -0500518 bool isTextureFunctionWithBias = false;
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500519 if (c.fFunction.fName == "texture" && c.fFunction.fBuiltin) {
520 const char* dim = "";
521 bool proj = false;
522 switch (c.fArguments[0]->fType.dimensions()) {
523 case SpvDim1D:
524 dim = "1D";
Brian Osman8a83ca42018-02-12 14:32:17 -0500525 isTextureFunctionWithBias = true;
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500526 if (c.fArguments[1]->fType == *fContext.fFloat_Type) {
527 proj = false;
528 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400529 SkASSERT(c.fArguments[1]->fType == *fContext.fFloat2_Type);
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500530 proj = true;
531 }
532 break;
533 case SpvDim2D:
534 dim = "2D";
Brian Osmanc35d7ea2018-02-21 12:00:14 -0500535 if (c.fArguments[0]->fType != *fContext.fSamplerExternalOES_Type) {
536 isTextureFunctionWithBias = true;
537 }
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400538 if (c.fArguments[1]->fType == *fContext.fFloat2_Type) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500539 proj = false;
540 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400541 SkASSERT(c.fArguments[1]->fType == *fContext.fFloat3_Type);
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500542 proj = true;
543 }
544 break;
545 case SpvDim3D:
546 dim = "3D";
Brian Osman8a83ca42018-02-12 14:32:17 -0500547 isTextureFunctionWithBias = true;
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400548 if (c.fArguments[1]->fType == *fContext.fFloat3_Type) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500549 proj = false;
550 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400551 SkASSERT(c.fArguments[1]->fType == *fContext.fFloat4_Type);
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500552 proj = true;
553 }
554 break;
555 case SpvDimCube:
556 dim = "Cube";
Brian Osman8a83ca42018-02-12 14:32:17 -0500557 isTextureFunctionWithBias = true;
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500558 proj = false;
559 break;
560 case SpvDimRect:
561 dim = "Rect";
562 proj = false;
563 break;
564 case SpvDimBuffer:
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400565 SkASSERT(false); // doesn't exist
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500566 dim = "Buffer";
567 proj = false;
568 break;
569 case SpvDimSubpassData:
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400570 SkASSERT(false); // doesn't exist
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500571 dim = "SubpassData";
572 proj = false;
573 break;
574 }
575 this->write("texture");
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500576 if (fProgram.fSettings.fCaps->generation() < k130_GrGLSLGeneration) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500577 this->write(dim);
578 }
579 if (proj) {
580 this->write("Proj");
581 }
582
583 } else {
584 this->write(c.fFunction.fName);
585 }
586 this->write("(");
ethannicholasf789b382016-08-03 12:43:36 -0700587 const char* separator = "";
588 for (const auto& arg : c.fArguments) {
589 this->write(separator);
590 separator = ", ";
591 this->writeExpression(*arg, kSequence_Precedence);
592 }
Brian Osman8a83ca42018-02-12 14:32:17 -0500593 if (fProgram.fSettings.fSharpenTextures && isTextureFunctionWithBias) {
594 this->write(", -0.5");
595 }
ethannicholasf789b382016-08-03 12:43:36 -0700596 this->write(")");
597}
598
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400599void GLSLCodeGenerator::writeConstructor(const Constructor& c, Precedence parentPrecedence) {
600 if (c.fArguments.size() == 1 &&
601 this->getTypeName(c.fType) == this->getTypeName(c.fArguments[0]->fType)) {
602 // in cases like half(float), they're different types as far as SkSL is concerned but the
603 // same type as far as GLSL is concerned. We avoid a redundant float(float) by just writing
604 // out the inner expression here.
605 this->writeExpression(*c.fArguments[0], parentPrecedence);
606 return;
607 }
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400608 this->writeType(c.fType);
609 this->write("(");
ethannicholasf789b382016-08-03 12:43:36 -0700610 const char* separator = "";
611 for (const auto& arg : c.fArguments) {
612 this->write(separator);
613 separator = ", ";
614 this->writeExpression(*arg, kSequence_Precedence);
615 }
616 this->write(")");
617}
618
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500619void GLSLCodeGenerator::writeFragCoord() {
Brian Osmancd3261a2018-01-16 13:52:29 +0000620 if (!fProgram.fSettings.fCaps->canUseFragCoord()) {
Brian Salomondba65f92018-01-22 08:43:38 -0500621 if (!fSetupFragCoordWorkaround) {
622 const char* precision = usesPrecisionModifiers() ? "highp " : "";
623 fFunctionHeader += precision;
624 fFunctionHeader += " float sk_FragCoord_InvW = 1. / sk_FragCoord_Workaround.w;\n";
625 fFunctionHeader += precision;
626 fFunctionHeader += " vec4 sk_FragCoord_Resolved = "
627 "vec4(sk_FragCoord_Workaround.xyz * sk_FragCoord_InvW, sk_FragCoord_InvW);\n";
628 // Ensure that we get exact .5 values for x and y.
629 fFunctionHeader += " sk_FragCoord_Resolved.xy = floor(sk_FragCoord_Resolved.xy) + "
630 "vec2(.5);\n";
631 fSetupFragCoordWorkaround = true;
632 }
633 this->write("sk_FragCoord_Resolved");
Brian Osmancd3261a2018-01-16 13:52:29 +0000634 return;
635 }
636
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500637 // We only declare "gl_FragCoord" when we're in the case where we want to use layout qualifiers
638 // to reverse y. Otherwise it isn't necessary and whether the "in" qualifier appears in the
639 // declaration varies in earlier GLSL specs. So it is simpler to omit it.
640 if (!fProgram.fSettings.fFlipY) {
641 this->write("gl_FragCoord");
642 } else if (const char* extension =
643 fProgram.fSettings.fCaps->fragCoordConventionsExtensionString()) {
644 if (!fSetupFragPositionGlobal) {
645 if (fProgram.fSettings.fCaps->generation() < k150_GrGLSLGeneration) {
646 fHeader.writeText("#extension ");
647 fHeader.writeText(extension);
648 fHeader.writeText(" : require\n");
649 }
650 fHeader.writeText("layout(origin_upper_left) in vec4 gl_FragCoord;\n");
651 fSetupFragPositionGlobal = true;
Greg Daniele8e4a3e2016-12-12 17:20:42 +0000652 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500653 this->write("gl_FragCoord");
Greg Daniele8e4a3e2016-12-12 17:20:42 +0000654 } else {
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500655 if (!fSetupFragPositionGlobal) {
656 // The Adreno compiler seems to be very touchy about access to "gl_FragCoord".
657 // Accessing glFragCoord.zw can cause a program to fail to link. Additionally,
658 // depending on the surrounding code, accessing .xy with a uniform involved can
Brian Osmancd3261a2018-01-16 13:52:29 +0000659 // do the same thing. Copying gl_FragCoord.xy into a temp float2 beforehand
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500660 // (and only accessing .xy) seems to "fix" things.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400661 const char* precision = usesPrecisionModifiers() ? "highp " : "";
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500662 fHeader.writeText("uniform ");
663 fHeader.writeText(precision);
664 fHeader.writeText("float " SKSL_RTHEIGHT_NAME ";\n");
665 fSetupFragPositionGlobal = true;
666 }
667 if (!fSetupFragPositionLocal) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400668 const char* precision = usesPrecisionModifiers() ? "highp " : "";
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500669 fFunctionHeader += precision;
670 fFunctionHeader += " vec2 _sktmpCoord = gl_FragCoord.xy;\n";
671 fFunctionHeader += precision;
672 fFunctionHeader += " vec4 sk_FragCoord = vec4(_sktmpCoord.x, " SKSL_RTHEIGHT_NAME
673 " - _sktmpCoord.y, 1.0, 1.0);\n";
674 fSetupFragPositionLocal = true;
675 }
676 this->write("sk_FragCoord");
677 }
678}
679
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500680void GLSLCodeGenerator::writeVariableReference(const VariableReference& ref) {
681 switch (ref.fVariable.fModifiers.fLayout.fBuiltin) {
682 case SK_FRAGCOLOR_BUILTIN:
683 if (fProgram.fSettings.fCaps->mustDeclareFragmentShaderOutput()) {
684 this->write("sk_FragColor");
685 } else {
686 this->write("gl_FragColor");
687 }
688 break;
689 case SK_FRAGCOORD_BUILTIN:
690 this->writeFragCoord();
691 break;
Ethan Nicholasa51740c2017-02-07 14:53:32 -0500692 case SK_VERTEXID_BUILTIN:
693 this->write("gl_VertexID");
694 break;
Chris Dalton8580d512017-10-14 22:12:33 -0600695 case SK_INSTANCEID_BUILTIN:
696 this->write("gl_InstanceID");
697 break;
Ethan Nicholas67d64602017-02-09 10:15:25 -0500698 case SK_CLIPDISTANCE_BUILTIN:
699 this->write("gl_ClipDistance");
700 break;
Ethan Nicholas52cad152017-02-16 16:37:32 -0500701 case SK_IN_BUILTIN:
702 this->write("gl_in");
703 break;
704 case SK_INVOCATIONID_BUILTIN:
705 this->write("gl_InvocationID");
706 break;
Ethan Nicholaseab2baa2018-04-13 15:16:27 -0400707 case SK_LASTFRAGCOLOR_BUILTIN:
708 this->write(fProgram.fSettings.fCaps->fbFetchColorName());
709 break;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500710 default:
711 this->write(ref.fVariable.fName);
ethannicholas5961bc92016-10-12 06:39:56 -0700712 }
ethannicholasf789b382016-08-03 12:43:36 -0700713}
714
715void GLSLCodeGenerator::writeIndexExpression(const IndexExpression& expr) {
716 this->writeExpression(*expr.fBase, kPostfix_Precedence);
717 this->write("[");
718 this->writeExpression(*expr.fIndex, kTopLevel_Precedence);
719 this->write("]");
720}
721
Brian Osmancd3261a2018-01-16 13:52:29 +0000722bool is_sk_position(const FieldAccess& f) {
723 return "sk_Position" == f.fBase->fType.fields()[f.fFieldIndex].fName;
724}
725
ethannicholasf789b382016-08-03 12:43:36 -0700726void GLSLCodeGenerator::writeFieldAccess(const FieldAccess& f) {
727 if (f.fOwnerKind == FieldAccess::kDefault_OwnerKind) {
728 this->writeExpression(*f.fBase, kPostfix_Precedence);
729 this->write(".");
730 }
Ethan Nicholas67d64602017-02-09 10:15:25 -0500731 switch (f.fBase->fType.fields()[f.fFieldIndex].fModifiers.fLayout.fBuiltin) {
732 case SK_CLIPDISTANCE_BUILTIN:
733 this->write("gl_ClipDistance");
734 break;
735 default:
Ethan Nicholasbed683a2017-09-26 14:23:59 -0400736 StringFragment name = f.fBase->fType.fields()[f.fFieldIndex].fName;
737 if (name == "sk_Position") {
738 this->write("gl_Position");
739 } else if (name == "sk_PointSize") {
740 this->write("gl_PointSize");
741 } else {
742 this->write(f.fBase->fType.fields()[f.fFieldIndex].fName);
743 }
Ethan Nicholas67d64602017-02-09 10:15:25 -0500744 }
ethannicholasf789b382016-08-03 12:43:36 -0700745}
746
747void GLSLCodeGenerator::writeSwizzle(const Swizzle& swizzle) {
748 this->writeExpression(*swizzle.fBase, kPostfix_Precedence);
749 this->write(".");
750 for (int c : swizzle.fComponents) {
751 this->write(&("x\0y\0z\0w\0"[c * 2]));
752 }
753}
754
Ethan Nicholas762466e2017-06-29 10:03:38 -0400755GLSLCodeGenerator::Precedence GLSLCodeGenerator::GetBinaryPrecedence(Token::Kind op) {
ethannicholasf789b382016-08-03 12:43:36 -0700756 switch (op) {
757 case Token::STAR: // fall through
758 case Token::SLASH: // fall through
759 case Token::PERCENT: return GLSLCodeGenerator::kMultiplicative_Precedence;
760 case Token::PLUS: // fall through
761 case Token::MINUS: return GLSLCodeGenerator::kAdditive_Precedence;
762 case Token::SHL: // fall through
763 case Token::SHR: return GLSLCodeGenerator::kShift_Precedence;
764 case Token::LT: // fall through
765 case Token::GT: // fall through
766 case Token::LTEQ: // fall through
767 case Token::GTEQ: return GLSLCodeGenerator::kRelational_Precedence;
768 case Token::EQEQ: // fall through
769 case Token::NEQ: return GLSLCodeGenerator::kEquality_Precedence;
770 case Token::BITWISEAND: return GLSLCodeGenerator::kBitwiseAnd_Precedence;
771 case Token::BITWISEXOR: return GLSLCodeGenerator::kBitwiseXor_Precedence;
772 case Token::BITWISEOR: return GLSLCodeGenerator::kBitwiseOr_Precedence;
773 case Token::LOGICALAND: return GLSLCodeGenerator::kLogicalAnd_Precedence;
774 case Token::LOGICALXOR: return GLSLCodeGenerator::kLogicalXor_Precedence;
775 case Token::LOGICALOR: return GLSLCodeGenerator::kLogicalOr_Precedence;
776 case Token::EQ: // fall through
777 case Token::PLUSEQ: // fall through
778 case Token::MINUSEQ: // fall through
779 case Token::STAREQ: // fall through
780 case Token::SLASHEQ: // fall through
781 case Token::PERCENTEQ: // fall through
782 case Token::SHLEQ: // fall through
783 case Token::SHREQ: // fall through
784 case Token::LOGICALANDEQ: // fall through
785 case Token::LOGICALXOREQ: // fall through
786 case Token::LOGICALOREQ: // fall through
787 case Token::BITWISEANDEQ: // fall through
788 case Token::BITWISEXOREQ: // fall through
789 case Token::BITWISEOREQ: return GLSLCodeGenerator::kAssignment_Precedence;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400790 case Token::COMMA: return GLSLCodeGenerator::kSequence_Precedence;
ethannicholasf789b382016-08-03 12:43:36 -0700791 default: ABORT("unsupported binary operator");
792 }
793}
794
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400795void GLSLCodeGenerator::writeBinaryExpression(const BinaryExpression& b,
ethannicholasf789b382016-08-03 12:43:36 -0700796 Precedence parentPrecedence) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400797 Precedence precedence = GetBinaryPrecedence(b.fOperator);
ethannicholasf789b382016-08-03 12:43:36 -0700798 if (precedence >= parentPrecedence) {
799 this->write("(");
800 }
Brian Osmancd3261a2018-01-16 13:52:29 +0000801 bool positionWorkaround = Compiler::IsAssignment(b.fOperator) &&
802 Expression::kFieldAccess_Kind == b.fLeft->fKind &&
803 is_sk_position((FieldAccess&) *b.fLeft) &&
804 !strstr(b.fRight->description().c_str(), "sk_RTAdjust") &&
805 !fProgram.fSettings.fCaps->canUseFragCoord();
806 if (positionWorkaround) {
807 this->write("sk_FragCoord_Workaround = (");
808 }
ethannicholasf789b382016-08-03 12:43:36 -0700809 this->writeExpression(*b.fLeft, precedence);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700810 this->write(" ");
811 this->write(Compiler::OperatorName(b.fOperator));
812 this->write(" ");
ethannicholasf789b382016-08-03 12:43:36 -0700813 this->writeExpression(*b.fRight, precedence);
Brian Osmancd3261a2018-01-16 13:52:29 +0000814 if (positionWorkaround) {
815 this->write(")");
816 }
ethannicholasf789b382016-08-03 12:43:36 -0700817 if (precedence >= parentPrecedence) {
818 this->write(")");
819 }
820}
821
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400822void GLSLCodeGenerator::writeTernaryExpression(const TernaryExpression& t,
ethannicholasf789b382016-08-03 12:43:36 -0700823 Precedence parentPrecedence) {
824 if (kTernary_Precedence >= parentPrecedence) {
825 this->write("(");
826 }
827 this->writeExpression(*t.fTest, kTernary_Precedence);
828 this->write(" ? ");
829 this->writeExpression(*t.fIfTrue, kTernary_Precedence);
830 this->write(" : ");
831 this->writeExpression(*t.fIfFalse, kTernary_Precedence);
832 if (kTernary_Precedence >= parentPrecedence) {
833 this->write(")");
834 }
835}
836
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400837void GLSLCodeGenerator::writePrefixExpression(const PrefixExpression& p,
ethannicholasf789b382016-08-03 12:43:36 -0700838 Precedence parentPrecedence) {
839 if (kPrefix_Precedence >= parentPrecedence) {
840 this->write("(");
841 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700842 this->write(Compiler::OperatorName(p.fOperator));
ethannicholasf789b382016-08-03 12:43:36 -0700843 this->writeExpression(*p.fOperand, kPrefix_Precedence);
844 if (kPrefix_Precedence >= parentPrecedence) {
845 this->write(")");
846 }
847}
848
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400849void GLSLCodeGenerator::writePostfixExpression(const PostfixExpression& p,
ethannicholasf789b382016-08-03 12:43:36 -0700850 Precedence parentPrecedence) {
851 if (kPostfix_Precedence >= parentPrecedence) {
852 this->write("(");
853 }
854 this->writeExpression(*p.fOperand, kPostfix_Precedence);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700855 this->write(Compiler::OperatorName(p.fOperator));
ethannicholasf789b382016-08-03 12:43:36 -0700856 if (kPostfix_Precedence >= parentPrecedence) {
857 this->write(")");
858 }
859}
860
861void GLSLCodeGenerator::writeBoolLiteral(const BoolLiteral& b) {
862 this->write(b.fValue ? "true" : "false");
863}
864
865void GLSLCodeGenerator::writeIntLiteral(const IntLiteral& i) {
ethannicholas5961bc92016-10-12 06:39:56 -0700866 if (i.fType == *fContext.fUInt_Type) {
867 this->write(to_string(i.fValue & 0xffffffff) + "u");
Ethan Nicholas58d56482017-12-19 09:29:22 -0500868 } else if (i.fType == *fContext.fUShort_Type) {
869 this->write(to_string(i.fValue & 0xffff) + "u");
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400870 } else if (i.fType == *fContext.fUByte_Type) {
871 this->write(to_string(i.fValue & 0xff) + "u");
872 } else {
ethannicholas5961bc92016-10-12 06:39:56 -0700873 this->write(to_string((int32_t) i.fValue));
874 }
ethannicholasf789b382016-08-03 12:43:36 -0700875}
876
877void GLSLCodeGenerator::writeFloatLiteral(const FloatLiteral& f) {
878 this->write(to_string(f.fValue));
879}
880
Ethan Nicholas762466e2017-06-29 10:03:38 -0400881void GLSLCodeGenerator::writeSetting(const Setting& s) {
882 ABORT("internal error; setting was not folded to a constant during compilation\n");
883}
884
ethannicholasf789b382016-08-03 12:43:36 -0700885void GLSLCodeGenerator::writeFunction(const FunctionDefinition& f) {
Kevin Lubickf2030782018-06-19 12:04:18 +0000886 this->writeTypePrecision(f.fDeclaration.fReturnType);
887 this->writeType(f.fDeclaration.fReturnType);
888 this->write(" " + f.fDeclaration.fName + "(");
889 const char* separator = "";
890 for (const auto& param : f.fDeclaration.fParameters) {
891 this->write(separator);
892 separator = ", ";
893 this->writeModifiers(param->fModifiers, false);
894 std::vector<int> sizes;
895 const Type* type = &param->fType;
896 while (type->kind() == Type::kArray_Kind) {
897 sizes.push_back(type->columns());
898 type = &type->componentType();
899 }
900 this->writeTypePrecision(*type);
901 this->writeType(*type);
902 this->write(" " + param->fName);
903 for (int s : sizes) {
904 if (s <= 0) {
905 this->write("[]");
906 } else {
907 this->write("[" + to_string(s) + "]");
ethannicholas5961bc92016-10-12 06:39:56 -0700908 }
909 }
ethannicholasf789b382016-08-03 12:43:36 -0700910 }
Kevin Lubickf2030782018-06-19 12:04:18 +0000911 this->writeLine(") {");
912
ethannicholas5961bc92016-10-12 06:39:56 -0700913 fFunctionHeader = "";
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400914 OutputStream* oldOut = fOut;
915 StringStream buffer;
ethannicholas5961bc92016-10-12 06:39:56 -0700916 fOut = &buffer;
Kevin Lubickf2030782018-06-19 12:04:18 +0000917 fIndentation++;
Ethan Nicholascb670962017-04-20 19:31:52 -0400918 this->writeStatements(((Block&) *f.fBody).fStatements);
Kevin Lubickf2030782018-06-19 12:04:18 +0000919 fIndentation--;
920 this->writeLine("}");
ethannicholas5961bc92016-10-12 06:39:56 -0700921
922 fOut = oldOut;
923 this->write(fFunctionHeader);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400924 this->write(buffer.str());
ethannicholasf789b382016-08-03 12:43:36 -0700925}
926
Greg Daniel64773e62016-11-22 09:44:03 -0500927void GLSLCodeGenerator::writeModifiers(const Modifiers& modifiers,
ethannicholas5961bc92016-10-12 06:39:56 -0700928 bool globalContext) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500929 if (modifiers.fFlags & Modifiers::kFlat_Flag) {
930 this->write("flat ");
931 }
ethannicholas5961bc92016-10-12 06:39:56 -0700932 if (modifiers.fFlags & Modifiers::kNoPerspective_Flag) {
933 this->write("noperspective ");
934 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400935 String layout = modifiers.fLayout.description();
Ethan Nicholas8da9e942017-03-09 16:35:09 -0500936 if (layout.size()) {
937 this->write(layout + " ");
938 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500939 if (modifiers.fFlags & Modifiers::kReadOnly_Flag) {
940 this->write("readonly ");
941 }
942 if (modifiers.fFlags & Modifiers::kWriteOnly_Flag) {
943 this->write("writeonly ");
944 }
945 if (modifiers.fFlags & Modifiers::kCoherent_Flag) {
946 this->write("coherent ");
947 }
948 if (modifiers.fFlags & Modifiers::kVolatile_Flag) {
949 this->write("volatile ");
950 }
951 if (modifiers.fFlags & Modifiers::kRestrict_Flag) {
952 this->write("restrict ");
ethannicholas5961bc92016-10-12 06:39:56 -0700953 }
Greg Daniel64773e62016-11-22 09:44:03 -0500954 if ((modifiers.fFlags & Modifiers::kIn_Flag) &&
ethannicholas5961bc92016-10-12 06:39:56 -0700955 (modifiers.fFlags & Modifiers::kOut_Flag)) {
956 this->write("inout ");
957 } else if (modifiers.fFlags & Modifiers::kIn_Flag) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500958 if (globalContext &&
959 fProgram.fSettings.fCaps->generation() < GrGLSLGeneration::k130_GrGLSLGeneration) {
ethannicholas5961bc92016-10-12 06:39:56 -0700960 this->write(fProgramKind == Program::kVertex_Kind ? "attribute "
961 : "varying ");
962 } else {
963 this->write("in ");
964 }
965 } else if (modifiers.fFlags & Modifiers::kOut_Flag) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500966 if (globalContext &&
967 fProgram.fSettings.fCaps->generation() < GrGLSLGeneration::k130_GrGLSLGeneration) {
ethannicholas5961bc92016-10-12 06:39:56 -0700968 this->write("varying ");
969 } else {
970 this->write("out ");
971 }
972 }
973 if (modifiers.fFlags & Modifiers::kUniform_Flag) {
974 this->write("uniform ");
975 }
976 if (modifiers.fFlags & Modifiers::kConst_Flag) {
977 this->write("const ");
978 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400979 if (usesPrecisionModifiers()) {
ethannicholas5961bc92016-10-12 06:39:56 -0700980 if (modifiers.fFlags & Modifiers::kLowp_Flag) {
981 this->write("lowp ");
982 }
983 if (modifiers.fFlags & Modifiers::kMediump_Flag) {
984 this->write("mediump ");
985 }
986 if (modifiers.fFlags & Modifiers::kHighp_Flag) {
987 this->write("highp ");
988 }
989 }
ethannicholasf789b382016-08-03 12:43:36 -0700990}
991
992void GLSLCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) {
Ethan Nicholas52cad152017-02-16 16:37:32 -0500993 if (intf.fTypeName == "sk_PerVertex") {
ethannicholasf789b382016-08-03 12:43:36 -0700994 return;
995 }
ethannicholas5961bc92016-10-12 06:39:56 -0700996 this->writeModifiers(intf.fVariable.fModifiers, true);
Ethan Nicholas50afc172017-02-16 14:49:57 -0500997 this->writeLine(intf.fTypeName + " {");
ethannicholasf789b382016-08-03 12:43:36 -0700998 fIndentation++;
Ethan Nicholas50afc172017-02-16 14:49:57 -0500999 const Type* structType = &intf.fVariable.fType;
1000 while (structType->kind() == Type::kArray_Kind) {
1001 structType = &structType->componentType();
1002 }
1003 for (const auto& f : structType->fields()) {
ethannicholas5961bc92016-10-12 06:39:56 -07001004 this->writeModifiers(f.fModifiers, false);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001005 this->writeTypePrecision(*f.fType);
ethannicholas0730be72016-09-01 07:59:02 -07001006 this->writeType(*f.fType);
ethannicholasf789b382016-08-03 12:43:36 -07001007 this->writeLine(" " + f.fName + ";");
1008 }
1009 fIndentation--;
Ethan Nicholas50afc172017-02-16 14:49:57 -05001010 this->write("}");
1011 if (intf.fInstanceName.size()) {
1012 this->write(" ");
1013 this->write(intf.fInstanceName);
1014 for (const auto& size : intf.fSizes) {
1015 this->write("[");
1016 if (size) {
1017 this->writeExpression(*size, kTopLevel_Precedence);
1018 }
1019 this->write("]");
1020 }
1021 }
1022 this->writeLine(";");
ethannicholasf789b382016-08-03 12:43:36 -07001023}
1024
Ethan Nicholas762466e2017-06-29 10:03:38 -04001025void GLSLCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) {
1026 this->writeExpression(value, kTopLevel_Precedence);
1027}
1028
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001029const char* GLSLCodeGenerator::getTypePrecision(const Type& type) {
1030 if (usesPrecisionModifiers()) {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001031 switch (type.kind()) {
1032 case Type::kScalar_Kind:
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001033 if (type == *fContext.fShort_Type || type == *fContext.fUShort_Type ||
1034 type == *fContext.fByte_Type || type == *fContext.fUByte_Type) {
Chris Daltonc2d0dd62018-03-07 07:46:10 -07001035 if (fProgram.fSettings.fForceHighPrecision ||
1036 fProgram.fSettings.fCaps->incompleteShortIntPrecision()) {
1037 return "highp ";
1038 }
1039 return "mediump ";
1040 }
1041 if (type == *fContext.fHalf_Type) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001042 return fProgram.fSettings.fForceHighPrecision ? "highp " : "mediump ";
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001043 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001044 if (type == *fContext.fFloat_Type || type == *fContext.fInt_Type ||
1045 type == *fContext.fUInt_Type) {
1046 return "highp ";
1047 }
1048 return "";
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001049 case Type::kVector_Kind: // fall through
1050 case Type::kMatrix_Kind:
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001051 return this->getTypePrecision(type.componentType());
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001052 default:
1053 break;
1054 }
1055 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001056 return "";
1057}
1058
1059void GLSLCodeGenerator::writeTypePrecision(const Type& type) {
1060 this->write(this->getTypePrecision(type));
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001061}
1062
ethannicholas5961bc92016-10-12 06:39:56 -07001063void GLSLCodeGenerator::writeVarDeclarations(const VarDeclarations& decl, bool global) {
Ethan Nicholas14efcbf2017-11-07 09:23:38 -05001064 if (!decl.fVars.size()) {
1065 return;
1066 }
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001067 bool wroteType = false;
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001068 for (const auto& stmt : decl.fVars) {
1069 VarDeclaration& var = (VarDeclaration&) *stmt;
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001070 if (wroteType) {
1071 this->write(", ");
1072 } else {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001073 this->writeModifiers(var.fVar->fModifiers, global);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001074 this->writeTypePrecision(decl.fBaseType);
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001075 this->writeType(decl.fBaseType);
1076 this->write(" ");
1077 wroteType = true;
1078 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001079 this->write(var.fVar->fName);
1080 for (const auto& size : var.fSizes) {
ethannicholasf789b382016-08-03 12:43:36 -07001081 this->write("[");
ethannicholas5961bc92016-10-12 06:39:56 -07001082 if (size) {
1083 this->writeExpression(*size, kTopLevel_Precedence);
1084 }
ethannicholasf789b382016-08-03 12:43:36 -07001085 this->write("]");
1086 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001087 if (var.fValue) {
ethannicholasf789b382016-08-03 12:43:36 -07001088 this->write(" = ");
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001089 this->writeVarInitializer(*var.fVar, *var.fValue);
ethannicholasf789b382016-08-03 12:43:36 -07001090 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001091 if (!fFoundImageDecl && var.fVar->fType == *fContext.fImage2D_Type) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001092 if (fProgram.fSettings.fCaps->imageLoadStoreExtensionString()) {
Ethan Nicholas9e1138d2016-11-21 10:39:35 -05001093 fHeader.writeText("#extension ");
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001094 fHeader.writeText(fProgram.fSettings.fCaps->imageLoadStoreExtensionString());
Ethan Nicholas9e1138d2016-11-21 10:39:35 -05001095 fHeader.writeText(" : require\n");
Brian Salomon2a51de82016-11-16 12:06:01 -05001096 }
1097 fFoundImageDecl = true;
1098 }
Brian Osman4b2f9152018-04-17 11:19:57 -04001099 if (!fFoundExternalSamplerDecl && var.fVar->fType == *fContext.fSamplerExternalOES_Type) {
1100 if (fProgram.fSettings.fCaps->externalTextureExtensionString()) {
1101 fHeader.writeText("#extension ");
1102 fHeader.writeText(fProgram.fSettings.fCaps->externalTextureExtensionString());
Brian Osman6cd92682018-04-25 13:13:19 -04001103 fHeader.writeText(" : enable\n");
Brian Osman4b2f9152018-04-17 11:19:57 -04001104 }
Brian Osman061020e2018-04-17 14:22:15 -04001105 if (fProgram.fSettings.fCaps->secondExternalTextureExtensionString()) {
1106 fHeader.writeText("#extension ");
1107 fHeader.writeText(fProgram.fSettings.fCaps->secondExternalTextureExtensionString());
Brian Osman6cd92682018-04-25 13:13:19 -04001108 fHeader.writeText(" : enable\n");
Brian Osman061020e2018-04-17 14:22:15 -04001109 }
Brian Osman4b2f9152018-04-17 11:19:57 -04001110 fFoundExternalSamplerDecl = true;
1111 }
ethannicholasf789b382016-08-03 12:43:36 -07001112 }
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001113 if (wroteType) {
1114 this->write(";");
1115 }
ethannicholasf789b382016-08-03 12:43:36 -07001116}
1117
1118void GLSLCodeGenerator::writeStatement(const Statement& s) {
1119 switch (s.fKind) {
1120 case Statement::kBlock_Kind:
1121 this->writeBlock((Block&) s);
1122 break;
1123 case Statement::kExpression_Kind:
1124 this->writeExpression(*((ExpressionStatement&) s).fExpression, kTopLevel_Precedence);
1125 this->write(";");
1126 break;
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001127 case Statement::kReturn_Kind:
ethannicholasf789b382016-08-03 12:43:36 -07001128 this->writeReturnStatement((ReturnStatement&) s);
1129 break;
ethannicholas14fe8cc2016-09-07 13:37:16 -07001130 case Statement::kVarDeclarations_Kind:
ethannicholas5961bc92016-10-12 06:39:56 -07001131 this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclaration, false);
ethannicholasf789b382016-08-03 12:43:36 -07001132 break;
1133 case Statement::kIf_Kind:
1134 this->writeIfStatement((IfStatement&) s);
1135 break;
1136 case Statement::kFor_Kind:
1137 this->writeForStatement((ForStatement&) s);
1138 break;
1139 case Statement::kWhile_Kind:
1140 this->writeWhileStatement((WhileStatement&) s);
1141 break;
1142 case Statement::kDo_Kind:
1143 this->writeDoStatement((DoStatement&) s);
1144 break;
Ethan Nicholasaf197692017-02-27 13:26:45 -05001145 case Statement::kSwitch_Kind:
1146 this->writeSwitchStatement((SwitchStatement&) s);
1147 break;
ethannicholasf789b382016-08-03 12:43:36 -07001148 case Statement::kBreak_Kind:
1149 this->write("break;");
1150 break;
1151 case Statement::kContinue_Kind:
1152 this->write("continue;");
1153 break;
1154 case Statement::kDiscard_Kind:
1155 this->write("discard;");
1156 break;
Ethan Nicholascb670962017-04-20 19:31:52 -04001157 case Statement::kNop_Kind:
1158 this->write(";");
1159 break;
ethannicholasf789b382016-08-03 12:43:36 -07001160 default:
1161 ABORT("unsupported statement: %s", s.description().c_str());
1162 }
1163}
1164
Ethan Nicholascb670962017-04-20 19:31:52 -04001165void GLSLCodeGenerator::writeStatements(const std::vector<std::unique_ptr<Statement>>& statements) {
1166 for (const auto& s : statements) {
1167 if (!s->isEmpty()) {
1168 this->writeStatement(*s);
1169 this->writeLine();
1170 }
1171 }
1172}
1173
ethannicholasf789b382016-08-03 12:43:36 -07001174void GLSLCodeGenerator::writeBlock(const Block& b) {
1175 this->writeLine("{");
1176 fIndentation++;
Ethan Nicholascb670962017-04-20 19:31:52 -04001177 this->writeStatements(b.fStatements);
ethannicholasf789b382016-08-03 12:43:36 -07001178 fIndentation--;
1179 this->write("}");
1180}
1181
1182void GLSLCodeGenerator::writeIfStatement(const IfStatement& stmt) {
1183 this->write("if (");
1184 this->writeExpression(*stmt.fTest, kTopLevel_Precedence);
1185 this->write(") ");
1186 this->writeStatement(*stmt.fIfTrue);
1187 if (stmt.fIfFalse) {
1188 this->write(" else ");
1189 this->writeStatement(*stmt.fIfFalse);
1190 }
1191}
1192
1193void GLSLCodeGenerator::writeForStatement(const ForStatement& f) {
1194 this->write("for (");
Ethan Nicholasb310fd52017-06-09 13:46:34 -04001195 if (f.fInitializer && !f.fInitializer->isEmpty()) {
ethannicholasf789b382016-08-03 12:43:36 -07001196 this->writeStatement(*f.fInitializer);
1197 } else {
1198 this->write("; ");
1199 }
1200 if (f.fTest) {
1201 this->writeExpression(*f.fTest, kTopLevel_Precedence);
1202 }
1203 this->write("; ");
1204 if (f.fNext) {
1205 this->writeExpression(*f.fNext, kTopLevel_Precedence);
1206 }
1207 this->write(") ");
1208 this->writeStatement(*f.fStatement);
1209}
1210
1211void GLSLCodeGenerator::writeWhileStatement(const WhileStatement& w) {
1212 this->write("while (");
1213 this->writeExpression(*w.fTest, kTopLevel_Precedence);
1214 this->write(") ");
1215 this->writeStatement(*w.fStatement);
1216}
1217
1218void GLSLCodeGenerator::writeDoStatement(const DoStatement& d) {
1219 this->write("do ");
1220 this->writeStatement(*d.fStatement);
1221 this->write(" while (");
1222 this->writeExpression(*d.fTest, kTopLevel_Precedence);
1223 this->write(");");
1224}
1225
Ethan Nicholasaf197692017-02-27 13:26:45 -05001226void GLSLCodeGenerator::writeSwitchStatement(const SwitchStatement& s) {
1227 this->write("switch (");
1228 this->writeExpression(*s.fValue, kTopLevel_Precedence);
1229 this->writeLine(") {");
1230 fIndentation++;
1231 for (const auto& c : s.fCases) {
1232 if (c->fValue) {
1233 this->write("case ");
1234 this->writeExpression(*c->fValue, kTopLevel_Precedence);
1235 this->writeLine(":");
1236 } else {
1237 this->writeLine("default:");
1238 }
1239 fIndentation++;
1240 for (const auto& stmt : c->fStatements) {
1241 this->writeStatement(*stmt);
1242 this->writeLine();
1243 }
1244 fIndentation--;
1245 }
1246 fIndentation--;
1247 this->write("}");
1248}
1249
ethannicholasf789b382016-08-03 12:43:36 -07001250void GLSLCodeGenerator::writeReturnStatement(const ReturnStatement& r) {
1251 this->write("return");
1252 if (r.fExpression) {
1253 this->write(" ");
1254 this->writeExpression(*r.fExpression, kTopLevel_Precedence);
1255 }
1256 this->write(";");
1257}
1258
Ethan Nicholas762466e2017-06-29 10:03:38 -04001259void GLSLCodeGenerator::writeHeader() {
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001260 this->write(fProgram.fSettings.fCaps->versionDeclString());
ethannicholasf789b382016-08-03 12:43:36 -07001261 this->writeLine();
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001262 for (const auto& e : fProgram) {
1263 if (e.fKind == ProgramElement::kExtension_Kind) {
1264 this->writeExtension((Extension&) e);
ethannicholas5961bc92016-10-12 06:39:56 -07001265 }
1266 }
Brian Osmancd3261a2018-01-16 13:52:29 +00001267 if (!fProgram.fSettings.fCaps->canUseFragCoord()) {
1268 Layout layout;
1269 switch (fProgram.fKind) {
1270 case Program::kVertex_Kind: {
1271 Modifiers modifiers(layout, Modifiers::kOut_Flag | Modifiers::kHighp_Flag);
1272 this->writeModifiers(modifiers, true);
1273 this->write("vec4 sk_FragCoord_Workaround;\n");
1274 break;
1275 }
1276 case Program::kFragment_Kind: {
1277 Modifiers modifiers(layout, Modifiers::kIn_Flag | Modifiers::kHighp_Flag);
1278 this->writeModifiers(modifiers, true);
1279 this->write("vec4 sk_FragCoord_Workaround;\n");
1280 break;
1281 }
1282 default:
1283 break;
1284 }
1285 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001286}
1287
Ethan Nicholas762466e2017-06-29 10:03:38 -04001288void GLSLCodeGenerator::writeProgramElement(const ProgramElement& e) {
1289 switch (e.fKind) {
1290 case ProgramElement::kExtension_Kind:
1291 break;
1292 case ProgramElement::kVar_Kind: {
1293 VarDeclarations& decl = (VarDeclarations&) e;
1294 if (decl.fVars.size() > 0) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001295 int builtin = ((VarDeclaration&) *decl.fVars[0]).fVar->fModifiers.fLayout.fBuiltin;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001296 if (builtin == -1) {
1297 // normal var
1298 this->writeVarDeclarations(decl, true);
1299 this->writeLine();
1300 } else if (builtin == SK_FRAGCOLOR_BUILTIN &&
1301 fProgram.fSettings.fCaps->mustDeclareFragmentShaderOutput()) {
Brian Salomondc092132018-04-04 10:14:16 -04001302 if (fProgram.fSettings.fFragColorIsInOut) {
1303 this->write("inout ");
1304 } else {
1305 this->write("out ");
1306 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001307 if (usesPrecisionModifiers()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001308 this->write("mediump ");
Mike Klein5ce39722017-06-27 22:52:03 +00001309 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001310 this->writeLine("vec4 sk_FragColor;");
Mike Klein5ce39722017-06-27 22:52:03 +00001311 }
Mike Klein5ce39722017-06-27 22:52:03 +00001312 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001313 break;
Mike Klein5ce39722017-06-27 22:52:03 +00001314 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001315 case ProgramElement::kInterfaceBlock_Kind:
1316 this->writeInterfaceBlock((InterfaceBlock&) e);
1317 break;
1318 case ProgramElement::kFunction_Kind:
1319 this->writeFunction((FunctionDefinition&) e);
1320 break;
Chris Daltonf1b47bb2017-10-06 11:57:51 -06001321 case ProgramElement::kModifiers_Kind: {
1322 const Modifiers& modifiers = ((ModifiersDeclaration&) e).fModifiers;
1323 if (!fFoundGSInvocations && modifiers.fLayout.fInvocations >= 0) {
1324 if (fProgram.fSettings.fCaps->gsInvocationsExtensionString()) {
1325 fHeader.writeText("#extension ");
1326 fHeader.writeText(fProgram.fSettings.fCaps->gsInvocationsExtensionString());
1327 fHeader.writeText(" : require\n");
1328 }
1329 fFoundGSInvocations = true;
1330 }
1331 this->writeModifiers(modifiers, true);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001332 this->writeLine(";");
1333 break;
Chris Daltonf1b47bb2017-10-06 11:57:51 -06001334 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001335 case ProgramElement::kEnum_Kind:
1336 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001337 default:
1338 printf("%s\n", e.description().c_str());
1339 ABORT("unsupported program element");
Ethan Nicholasc0709392017-06-27 11:20:22 -04001340 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001341}
1342
1343bool GLSLCodeGenerator::generateCode() {
1344 OutputStream* rawOut = fOut;
1345 fOut = &fHeader;
1346 fProgramKind = fProgram.fKind;
Kevin Lubickf2030782018-06-19 12:04:18 +00001347 this->writeHeader();
Chris Dalton8fd79552018-01-11 00:46:14 -05001348 if (Program::kGeometry_Kind == fProgramKind &&
1349 fProgram.fSettings.fCaps->geometryShaderExtensionString()) {
1350 fHeader.writeText("#extension ");
1351 fHeader.writeText(fProgram.fSettings.fCaps->geometryShaderExtensionString());
1352 fHeader.writeText(" : require\n");
1353 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001354 StringStream body;
1355 fOut = &body;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001356 for (const auto& e : fProgram) {
1357 this->writeProgramElement(e);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001358 }
1359 fOut = rawOut;
ethannicholasddb37d62016-10-20 09:54:00 -07001360
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001361 write_stringstream(fHeader, *rawOut);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001362 if (this->usesPrecisionModifiers()) {
1363 this->writeLine("precision mediump float;");
1364 }
Ethan Nicholas6e6525c2018-01-03 17:03:56 -05001365 write_stringstream(fExtraFunctions, *rawOut);
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001366 write_stringstream(body, *rawOut);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001367 return true;
ethannicholasf789b382016-08-03 12:43:36 -07001368}
1369
1370}