blob: 8b7fc72fe1ef9d793d3d9c17d59245d78ff4830a [file] [log] [blame]
Ethan Nicholas762466e2017-06-29 10:03:38 -04001/*
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 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/SkSLCPPCodeGenerator.h"
Ethan Nicholas762466e2017-06-29 10:03:38 -04009
Brian Osman1298bc42020-06-30 13:39:35 -040010#include "include/private/SkSLSampleUsage.h"
Michael Ludwig8f3a8362020-06-29 17:27:00 -040011#include "src/sksl/SkSLAnalysis.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/sksl/SkSLCPPUniformCTypes.h"
13#include "src/sksl/SkSLCompiler.h"
14#include "src/sksl/SkSLHCodeGenerator.h"
Ethan Nicholas762466e2017-06-29 10:03:38 -040015
Michael Ludwig92e4c7f2018-08-30 16:08:18 -040016#include <algorithm>
17
Ethan Nicholas762466e2017-06-29 10:03:38 -040018namespace SkSL {
19
20static bool needs_uniform_var(const Variable& var) {
Ethan Nicholas5f9836e2017-12-20 15:16:33 -050021 return (var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
22 var.fType.kind() != Type::kSampler_Kind;
Ethan Nicholas762466e2017-06-29 10:03:38 -040023}
24
25CPPCodeGenerator::CPPCodeGenerator(const Context* context, const Program* program,
26 ErrorReporter* errors, String name, OutputStream* out)
John Stiles50819422020-06-18 13:00:38 -040027 : INHERITED(context, program, errors, out)
28 , fName(std::move(name))
29 , fFullName(String::printf("Gr%s", fName.c_str()))
30 , fSectionAndParameterHelper(program, *errors) {
31 fLineEnding = "\n";
Ethan Nicholas13863662019-07-29 13:05:15 -040032 fTextureFunctionOverride = "sample";
Ethan Nicholas762466e2017-06-29 10:03:38 -040033}
34
35void CPPCodeGenerator::writef(const char* s, va_list va) {
36 static constexpr int BUFFER_SIZE = 1024;
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040037 va_list copy;
38 va_copy(copy, va);
Ethan Nicholas762466e2017-06-29 10:03:38 -040039 char buffer[BUFFER_SIZE];
John Stiles50819422020-06-18 13:00:38 -040040 int length = std::vsnprintf(buffer, BUFFER_SIZE, s, va);
Ethan Nicholas762466e2017-06-29 10:03:38 -040041 if (length < BUFFER_SIZE) {
42 fOut->write(buffer, length);
43 } else {
44 std::unique_ptr<char[]> heap(new char[length + 1]);
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040045 vsprintf(heap.get(), s, copy);
Ethan Nicholas762466e2017-06-29 10:03:38 -040046 fOut->write(heap.get(), length);
47 }
z102.zhangd74f2c82018-08-10 09:08:47 +080048 va_end(copy);
Ethan Nicholas762466e2017-06-29 10:03:38 -040049}
50
51void CPPCodeGenerator::writef(const char* s, ...) {
52 va_list va;
53 va_start(va, s);
54 this->writef(s, va);
55 va_end(va);
56}
57
58void CPPCodeGenerator::writeHeader() {
59}
60
Ethan Nicholasf7b88202017-09-18 14:10:39 -040061bool CPPCodeGenerator::usesPrecisionModifiers() const {
62 return false;
Ethan Nicholas762466e2017-06-29 10:03:38 -040063}
64
Ethan Nicholasf7b88202017-09-18 14:10:39 -040065String CPPCodeGenerator::getTypeName(const Type& type) {
66 return type.name();
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040067}
Ethan Nicholasf7b88202017-09-18 14:10:39 -040068
Ethan Nicholas762466e2017-06-29 10:03:38 -040069void CPPCodeGenerator::writeBinaryExpression(const BinaryExpression& b,
70 Precedence parentPrecedence) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -040071 if (b.fOperator == Token::Kind::TK_PERCENT) {
Ethan Nicholas762466e2017-06-29 10:03:38 -040072 // need to use "%%" instead of "%" b/c the code will be inside of a printf
73 Precedence precedence = GetBinaryPrecedence(b.fOperator);
74 if (precedence >= parentPrecedence) {
75 this->write("(");
76 }
77 this->writeExpression(*b.fLeft, precedence);
78 this->write(" %% ");
79 this->writeExpression(*b.fRight, precedence);
80 if (precedence >= parentPrecedence) {
81 this->write(")");
82 }
Ethan Nicholasee1c8a72019-02-22 10:50:47 -050083 } else if (b.fLeft->fKind == Expression::kNullLiteral_Kind ||
84 b.fRight->fKind == Expression::kNullLiteral_Kind) {
85 const Variable* var;
86 if (b.fLeft->fKind != Expression::kNullLiteral_Kind) {
87 SkASSERT(b.fLeft->fKind == Expression::kVariableReference_Kind);
88 var = &((VariableReference&) *b.fLeft).fVariable;
89 } else {
90 SkASSERT(b.fRight->fKind == Expression::kVariableReference_Kind);
91 var = &((VariableReference&) *b.fRight).fVariable;
92 }
93 SkASSERT(var->fType.kind() == Type::kNullable_Kind &&
94 var->fType.componentType() == *fContext.fFragmentProcessor_Type);
95 this->write("%s");
Brian Osman12c5d292020-07-13 16:11:35 -040096 const char* op = "";
Ethan Nicholasee1c8a72019-02-22 10:50:47 -050097 switch (b.fOperator) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -040098 case Token::Kind::TK_EQEQ:
Brian Osman12c5d292020-07-13 16:11:35 -040099 op = "!";
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500100 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400101 case Token::Kind::TK_NEQ:
Brian Osman12c5d292020-07-13 16:11:35 -0400102 op = "";
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500103 break;
104 default:
105 SkASSERT(false);
106 }
Brian Osman12c5d292020-07-13 16:11:35 -0400107 int childIndex = this->getChildFPIndex(*var);
108 fFormatArgs.push_back(String(op) + "_outer.childProcessor(" + to_string(childIndex) +
109 ") ? \"true\" : \"false\"");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400110 } else {
111 INHERITED::writeBinaryExpression(b, parentPrecedence);
112 }
113}
114
115void CPPCodeGenerator::writeIndexExpression(const IndexExpression& i) {
116 const Expression& base = *i.fBase;
117 if (base.fKind == Expression::kVariableReference_Kind) {
118 int builtin = ((VariableReference&) base).fVariable.fModifiers.fLayout.fBuiltin;
Michael Ludwigfc2fdf02020-06-29 17:20:13 -0400119 if (SK_TEXTURESAMPLERS_BUILTIN == builtin) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400120 this->write("%s");
121 if (i.fIndex->fKind != Expression::kIntLiteral_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700122 fErrors.error(i.fIndex->fOffset,
Ethan Nicholas762466e2017-06-29 10:03:38 -0400123 "index into sk_TextureSamplers must be an integer literal");
124 return;
125 }
126 int64_t index = ((IntLiteral&) *i.fIndex).fValue;
127 fFormatArgs.push_back(" fragBuilder->getProgramBuilder()->samplerVariable("
Stephen Whited523a062019-06-19 13:12:46 -0400128 "args.fTexSamplers[" + to_string(index) + "])");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400129 return;
130 }
131 }
132 INHERITED::writeIndexExpression(i);
133}
134
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400135static String default_value(const Type& type) {
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500136 if (type.fName == "bool") {
137 return "false";
138 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400139 switch (type.kind()) {
140 case Type::kScalar_Kind: return "0";
141 case Type::kVector_Kind: return type.name() + "(0)";
142 case Type::kMatrix_Kind: return type.name() + "(1)";
143 default: ABORT("unsupported default_value type\n");
144 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400145}
146
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500147static String default_value(const Variable& var) {
Brian Osman495993a2018-10-16 15:45:55 -0400148 if (var.fModifiers.fLayout.fCType == SkSL::Layout::CType::kSkPMColor4f) {
Brian Osmanf28e55d2018-10-03 16:35:54 -0400149 return "{SK_FloatNaN, SK_FloatNaN, SK_FloatNaN, SK_FloatNaN}";
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500150 }
151 return default_value(var.fType);
152}
153
Ethan Nicholas762466e2017-06-29 10:03:38 -0400154static bool is_private(const Variable& var) {
155 return !(var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
156 !(var.fModifiers.fFlags & Modifiers::kIn_Flag) &&
157 var.fStorage == Variable::kGlobal_Storage &&
158 var.fModifiers.fLayout.fBuiltin == -1;
159}
160
Michael Ludwiga4275592018-08-31 10:52:47 -0400161static bool is_uniform_in(const Variable& var) {
162 return (var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
163 (var.fModifiers.fFlags & Modifiers::kIn_Flag) &&
164 var.fType.kind() != Type::kSampler_Kind;
165}
166
John Stiles47b4e222020-08-12 09:56:50 -0400167String CPPCodeGenerator::formatRuntimeValue(const Type& type,
168 const Layout& layout,
169 const String& cppCode,
170 std::vector<String>* formatArgs) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400171 if (type.isFloat()) {
John Stiles47b4e222020-08-12 09:56:50 -0400172 formatArgs->push_back(cppCode);
173 return "%f";
174 }
175 if (type == *fContext.fInt_Type) {
176 formatArgs->push_back(cppCode);
177 return "%d";
178 }
179 if (type == *fContext.fBool_Type) {
180 formatArgs->push_back("(" + cppCode + " ? \"true\" : \"false\")");
181 return "%s";
182 }
183 if (type == *fContext.fFloat2_Type || type == *fContext.fHalf2_Type) {
184 formatArgs->push_back(cppCode + ".fX");
185 formatArgs->push_back(cppCode + ".fY");
186 return type.name() + "(%f, %f)";
187 }
188 if (type == *fContext.fFloat3_Type || type == *fContext.fHalf3_Type) {
189 formatArgs->push_back(cppCode + ".fX");
190 formatArgs->push_back(cppCode + ".fY");
191 formatArgs->push_back(cppCode + ".fZ");
192 return type.name() + "(%f, %f, %f)";
193 }
194 if (type == *fContext.fFloat4_Type || type == *fContext.fHalf4_Type) {
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400195 switch (layout.fCType) {
196 case Layout::CType::kSkPMColor:
John Stiles47b4e222020-08-12 09:56:50 -0400197 formatArgs->push_back("SkGetPackedR32(" + cppCode + ") / 255.0");
198 formatArgs->push_back("SkGetPackedG32(" + cppCode + ") / 255.0");
199 formatArgs->push_back("SkGetPackedB32(" + cppCode + ") / 255.0");
200 formatArgs->push_back("SkGetPackedA32(" + cppCode + ") / 255.0");
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400201 break;
Brian Osmanf28e55d2018-10-03 16:35:54 -0400202 case Layout::CType::kSkPMColor4f:
John Stiles47b4e222020-08-12 09:56:50 -0400203 formatArgs->push_back(cppCode + ".fR");
204 formatArgs->push_back(cppCode + ".fG");
205 formatArgs->push_back(cppCode + ".fB");
206 formatArgs->push_back(cppCode + ".fA");
Brian Osmanf28e55d2018-10-03 16:35:54 -0400207 break;
Mike Reedb26b4e72020-01-22 14:31:21 -0500208 case Layout::CType::kSkV4:
John Stiles47b4e222020-08-12 09:56:50 -0400209 formatArgs->push_back(cppCode + ".x");
210 formatArgs->push_back(cppCode + ".y");
211 formatArgs->push_back(cppCode + ".z");
212 formatArgs->push_back(cppCode + ".w");
Brian Salomoneca66b32019-06-01 11:18:15 -0400213 break;
John Stiles47b4e222020-08-12 09:56:50 -0400214 case Layout::CType::kSkRect:
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400215 case Layout::CType::kDefault:
John Stiles47b4e222020-08-12 09:56:50 -0400216 formatArgs->push_back(cppCode + ".left()");
217 formatArgs->push_back(cppCode + ".top()");
218 formatArgs->push_back(cppCode + ".right()");
219 formatArgs->push_back(cppCode + ".bottom()");
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400220 break;
221 default:
222 SkASSERT(false);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400223 }
John Stiles47b4e222020-08-12 09:56:50 -0400224 return type.name() + "(%f, %f, %f, %f)";
Ethan Nicholas762466e2017-06-29 10:03:38 -0400225 }
John Stiles47b4e222020-08-12 09:56:50 -0400226 if (type.kind() == Type::kMatrix_Kind) {
227 SkASSERT(type.componentType() == *fContext.fFloat_Type ||
228 type.componentType() == *fContext.fHalf_Type);
229
230 String format = type.name() + "(";
231 for (int c = 0; c < type.columns(); ++c) {
232 for (int r = 0; r < type.rows(); ++r) {
233 formatArgs->push_back(String::printf("%s.rc(%d, %d)", cppCode.c_str(), r, c));
234 format += "%f, ";
235 }
236 }
237
238 // Replace trailing ", " with ")".
239 format.pop_back();
240 format.back() = ')';
241 return format;
242 }
243 if (type.kind() == Type::kEnum_Kind) {
244 formatArgs->push_back("(int) " + cppCode);
245 return "%d";
246 }
247 if (type == *fContext.fInt4_Type ||
248 type == *fContext.fShort4_Type ||
249 type == *fContext.fByte4_Type) {
250 formatArgs->push_back(cppCode + ".left()");
251 formatArgs->push_back(cppCode + ".top()");
252 formatArgs->push_back(cppCode + ".right()");
253 formatArgs->push_back(cppCode + ".bottom()");
254 return type.name() + "(%d, %d, %d, %d)";
255 }
256
257 SkDEBUGFAILF("unsupported runtime value type '%s'\n", String(type.fName).c_str());
258 return "";
259}
260
261void CPPCodeGenerator::writeRuntimeValue(const Type& type, const Layout& layout,
262 const String& cppCode) {
263 this->write(this->formatRuntimeValue(type, layout, cppCode, &fFormatArgs));
Ethan Nicholas762466e2017-06-29 10:03:38 -0400264}
265
266void CPPCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) {
267 if (is_private(var)) {
Ethan Nicholasd608c092017-10-26 09:30:08 -0400268 this->writeRuntimeValue(var.fType, var.fModifiers.fLayout, var.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400269 } else {
270 this->writeExpression(value, kTopLevel_Precedence);
271 }
272}
273
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400274String CPPCodeGenerator::getSamplerHandle(const Variable& var) {
275 int samplerCount = 0;
Ethan Nicholas68990be2017-07-13 09:36:52 -0400276 for (const auto param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400277 if (&var == param) {
278 return "args.fTexSamplers[" + to_string(samplerCount) + "]";
279 }
280 if (param->fType.kind() == Type::kSampler_Kind) {
281 ++samplerCount;
282 }
283 }
284 ABORT("should have found sampler in parameters\n");
285}
286
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400287void CPPCodeGenerator::writeIntLiteral(const IntLiteral& i) {
288 this->write(to_string((int32_t) i.fValue));
289}
290
Ethan Nicholas82399462017-10-16 12:35:44 -0400291void CPPCodeGenerator::writeSwizzle(const Swizzle& swizzle) {
292 if (fCPPMode) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400293 SkASSERT(swizzle.fComponents.size() == 1); // no support for multiple swizzle components yet
Ethan Nicholas82399462017-10-16 12:35:44 -0400294 this->writeExpression(*swizzle.fBase, kPostfix_Precedence);
295 switch (swizzle.fComponents[0]) {
296 case 0: this->write(".left()"); break;
297 case 1: this->write(".top()"); break;
298 case 2: this->write(".right()"); break;
299 case 3: this->write(".bottom()"); break;
300 }
301 } else {
302 INHERITED::writeSwizzle(swizzle);
303 }
304}
305
Ethan Nicholas762466e2017-06-29 10:03:38 -0400306void CPPCodeGenerator::writeVariableReference(const VariableReference& ref) {
Ethan Nicholas82399462017-10-16 12:35:44 -0400307 if (fCPPMode) {
308 this->write(ref.fVariable.fName);
309 return;
310 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400311 switch (ref.fVariable.fModifiers.fLayout.fBuiltin) {
312 case SK_INCOLOR_BUILTIN:
313 this->write("%s");
Michael Ludwig231de032018-08-30 14:33:01 -0400314 // EmitArgs.fInputColor is automatically set to half4(1) if
315 // no input was specified
316 fFormatArgs.push_back(String("args.fInputColor"));
Ethan Nicholas762466e2017-06-29 10:03:38 -0400317 break;
318 case SK_OUTCOLOR_BUILTIN:
319 this->write("%s");
320 fFormatArgs.push_back(String("args.fOutputColor"));
321 break;
Michael Ludwigfc2fdf02020-06-29 17:20:13 -0400322 case SK_MAIN_COORDS_BUILTIN:
323 this->write("%s");
324 fFormatArgs.push_back(String("args.fSampleCoord"));
325 fAccessSampleCoordsDirectly = true;
326 break;
Ethan Nicholascd700e92018-08-24 16:43:57 -0400327 case SK_WIDTH_BUILTIN:
328 this->write("sk_Width");
329 break;
330 case SK_HEIGHT_BUILTIN:
331 this->write("sk_Height");
332 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400333 default:
334 if (ref.fVariable.fType.kind() == Type::kSampler_Kind) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400335 this->write("%s");
336 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerVariable(" +
Stephen Whited523a062019-06-19 13:12:46 -0400337 this->getSamplerHandle(ref.fVariable) + ")");
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400338 return;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400339 }
340 if (ref.fVariable.fModifiers.fFlags & Modifiers::kUniform_Flag) {
341 this->write("%s");
342 String name = ref.fVariable.fName;
Brian Osman1cb41712017-10-19 12:54:52 -0400343 String var = String::printf("args.fUniformHandler->getUniformCStr(%sVar)",
344 HCodeGenerator::FieldName(name.c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400345 String code;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400346 if (ref.fVariable.fModifiers.fLayout.fWhen.fLength) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400347 code = String::printf("%sVar.isValid() ? %s : \"%s\"",
348 HCodeGenerator::FieldName(name.c_str()).c_str(),
349 var.c_str(),
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400350 default_value(ref.fVariable.fType).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400351 } else {
352 code = var;
353 }
354 fFormatArgs.push_back(code);
355 } else if (SectionAndParameterHelper::IsParameter(ref.fVariable)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700356 String name(ref.fVariable.fName);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400357 this->writeRuntimeValue(ref.fVariable.fType, ref.fVariable.fModifiers.fLayout,
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400358 String::printf("_outer.%s", name.c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400359 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700360 this->write(ref.fVariable.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400361 }
362 }
363}
364
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400365void CPPCodeGenerator::writeIfStatement(const IfStatement& s) {
366 if (s.fIsStatic) {
367 this->write("@");
368 }
369 INHERITED::writeIfStatement(s);
370}
371
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400372void CPPCodeGenerator::writeReturnStatement(const ReturnStatement& s) {
373 if (fInMain) {
374 fErrors.error(s.fOffset, "fragmentProcessor main() may not contain return statements");
375 }
376 INHERITED::writeReturnStatement(s);
377}
378
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400379void CPPCodeGenerator::writeSwitchStatement(const SwitchStatement& s) {
380 if (s.fIsStatic) {
381 this->write("@");
382 }
383 INHERITED::writeSwitchStatement(s);
384}
385
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400386void CPPCodeGenerator::writeFieldAccess(const FieldAccess& access) {
387 if (access.fBase->fType.name() == "fragmentProcessor") {
388 // Special field access on fragment processors are converted into function calls on
389 // GrFragmentProcessor's getters.
390 if (access.fBase->fKind != Expression::kVariableReference_Kind) {
391 fErrors.error(access.fBase->fOffset, "fragmentProcessor must be a reference\n");
392 return;
393 }
394
395 const Type::Field& field = fContext.fFragmentProcessor_Type->fields()[access.fFieldIndex];
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500396 const Variable& var = ((const VariableReference&) *access.fBase).fVariable;
Brian Osman12c5d292020-07-13 16:11:35 -0400397 String cppAccess = String::printf("_outer.childProcessor(%d)->%s()",
398 this->getChildFPIndex(var),
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500399 String(field.fName).c_str());
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400400
401 if (fCPPMode) {
402 this->write(cppAccess.c_str());
403 } else {
404 writeRuntimeValue(*field.fType, Layout(), cppAccess);
405 }
406 return;
407 }
408 INHERITED::writeFieldAccess(access);
409}
410
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500411int CPPCodeGenerator::getChildFPIndex(const Variable& var) const {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400412 int index = 0;
413 bool found = false;
414 for (const auto& p : fProgram) {
415 if (ProgramElement::kVar_Kind == p.fKind) {
416 const VarDeclarations& decls = (const VarDeclarations&) p;
417 for (const auto& raw : decls.fVars) {
418 const VarDeclaration& decl = (VarDeclaration&) *raw;
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500419 if (decl.fVar == &var) {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400420 found = true;
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500421 } else if (decl.fVar->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400422 ++index;
423 }
424 }
425 }
426 if (found) {
427 break;
428 }
429 }
430 SkASSERT(found);
431 return index;
432}
433
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400434void CPPCodeGenerator::writeFunctionCall(const FunctionCall& c) {
Ethan Nicholas13863662019-07-29 13:05:15 -0400435 if (c.fFunction.fBuiltin && c.fFunction.fName == "sample" &&
436 c.fArguments[0]->fType.kind() != Type::Kind::kSampler_Kind) {
Leon Scroggins III982fff22020-07-31 14:09:06 -0400437 // Validity checks that are detected by function definition in sksl_fp.inc
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400438 SkASSERT(c.fArguments.size() >= 1 && c.fArguments.size() <= 3);
Florin Malita390f9bd2019-03-04 12:25:57 -0500439 SkASSERT("fragmentProcessor" == c.fArguments[0]->fType.name() ||
440 "fragmentProcessor?" == c.fArguments[0]->fType.name());
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400441
442 // Actually fail during compilation if arguments with valid types are
Ethan Nicholas13863662019-07-29 13:05:15 -0400443 // provided that are not variable references, since sample() is a
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400444 // special function that impacts code emission.
445 if (c.fArguments[0]->fKind != Expression::kVariableReference_Kind) {
446 fErrors.error(c.fArguments[0]->fOffset,
Ethan Nicholas13863662019-07-29 13:05:15 -0400447 "sample()'s fragmentProcessor argument must be a variable reference\n");
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400448 return;
449 }
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500450 const Variable& child = ((const VariableReference&) *c.fArguments[0]).fVariable;
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400451
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400452 // Start a new extra emit code section so that the emitted child processor can depend on
453 // sksl variables defined in earlier sksl code.
454 this->newExtraEmitCodeBlock();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400455
Michael Ludwige88320b2020-06-24 09:04:56 -0400456 String inputColor;
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400457 if (c.fArguments.size() > 1 && c.fArguments[1]->fType.name() == "half4") {
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400458 // Use the invokeChild() variant that accepts an input color, so convert the 2nd
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400459 // argument's expression into C++ code that produces sksl stored in an SkString.
Brian Osman12c5d292020-07-13 16:11:35 -0400460 String inputColorName = "_input" + to_string(c.fOffset);
John Stilesd060c9d2020-06-08 11:44:25 -0400461 addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments[1], inputColorName));
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400462
Michael Ludwige88320b2020-06-24 09:04:56 -0400463 // invokeChild() needs a char* and a pre-pended comma
464 inputColor = ", " + inputColorName + ".c_str()";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400465 }
466
Michael Ludwige88320b2020-06-24 09:04:56 -0400467 String inputCoord;
468 String invokeFunction = "invokeChild";
469 if (c.fArguments.back()->fType.name() == "float2") {
470 // Invoking child with explicit coordinates at this call site
471 inputCoord = "_coords" + to_string(c.fOffset);
472 addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments.back(), inputCoord));
473 inputCoord.append(".c_str()");
474 } else if (c.fArguments.back()->fType.name() == "float3x3") {
475 // Invoking child with a matrix, sampling relative to the input coords.
476 invokeFunction = "invokeChildWithMatrix";
Brian Osman1298bc42020-06-30 13:39:35 -0400477 SampleUsage usage = Analysis::GetSampleUsage(fProgram, child);
Michael Ludwige88320b2020-06-24 09:04:56 -0400478
Brian Osman1298bc42020-06-30 13:39:35 -0400479 if (!usage.hasUniformMatrix()) {
Michael Ludwige88320b2020-06-24 09:04:56 -0400480 inputCoord = "_matrix" + to_string(c.fOffset);
481 addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments.back(), inputCoord));
482 inputCoord.append(".c_str()");
483 }
484 // else pass in the empty string to rely on invokeChildWithMatrix's automatic uniform
485 // resolution
486 }
487 if (!inputCoord.empty()) {
488 inputCoord = ", " + inputCoord;
489 }
490
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400491 // Write the output handling after the possible input handling
Ethan Nicholas13863662019-07-29 13:05:15 -0400492 String childName = "_sample" + to_string(c.fOffset);
Brian Osman12c5d292020-07-13 16:11:35 -0400493 String childIndexStr = to_string(this->getChildFPIndex(child));
494 addExtraEmitCodeLine("SkString " + childName + " = this->" + invokeFunction + "(" +
495 childIndexStr + inputColor + ", args" + inputCoord + ");");
John Stiles50819422020-06-18 13:00:38 -0400496
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000497 this->write("%s");
498 fFormatArgs.push_back(childName + ".c_str()");
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400499 return;
500 }
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400501 if (c.fFunction.fBuiltin) {
502 INHERITED::writeFunctionCall(c);
503 } else {
504 this->write("%s");
505 fFormatArgs.push_back((String(c.fFunction.fName) + "_name.c_str()").c_str());
506 this->write("(");
507 const char* separator = "";
508 for (const auto& arg : c.fArguments) {
509 this->write(separator);
510 separator = ", ";
511 this->writeExpression(*arg, kSequence_Precedence);
512 }
513 this->write(")");
514 }
Ethan Nicholas13863662019-07-29 13:05:15 -0400515 if (c.fFunction.fBuiltin && c.fFunction.fName == "sample") {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400516 this->write(".%s");
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400517 SkASSERT(c.fArguments.size() >= 1);
518 SkASSERT(c.fArguments[0]->fKind == Expression::kVariableReference_Kind);
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400519 String sampler = this->getSamplerHandle(((VariableReference&) *c.fArguments[0]).fVariable);
520 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerSwizzle(" + sampler +
Greg Daniel369ee6b2019-12-02 15:30:02 -0500521 ").asString().c_str()");
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400522 }
523}
524
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400525static const char* glsltype_string(const Context& context, const Type& type) {
526 if (type == *context.fFloat_Type) {
527 return "kFloat_GrSLType";
528 } else if (type == *context.fHalf_Type) {
529 return "kHalf_GrSLType";
530 } else if (type == *context.fFloat2_Type) {
531 return "kFloat2_GrSLType";
532 } else if (type == *context.fHalf2_Type) {
533 return "kHalf2_GrSLType";
Ethan Nicholas8ae1b562019-12-17 15:18:02 -0500534 } else if (type == *context.fFloat3_Type) {
535 return "kFloat3_GrSLType";
536 } else if (type == *context.fHalf3_Type) {
537 return "kHalf3_GrSLType";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400538 } else if (type == *context.fFloat4_Type) {
539 return "kFloat4_GrSLType";
540 } else if (type == *context.fHalf4_Type) {
541 return "kHalf4_GrSLType";
Ethan Nicholas58430122020-04-14 09:54:02 -0400542 } else if (type == *context.fFloat2x2_Type) {
543 return "kFloat2x2_GrSLType";
544 } else if (type == *context.fHalf2x2_Type) {
545 return "kHalf2x2_GrSLType";
546 } else if (type == *context.fFloat3x3_Type) {
547 return "kFloat3x3_GrSLType";
548 } else if (type == *context.fHalf3x3_Type) {
549 return "kHalf3x3_GrSLType";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400550 } else if (type == *context.fFloat4x4_Type) {
551 return "kFloat4x4_GrSLType";
552 } else if (type == *context.fHalf4x4_Type) {
553 return "kHalf4x4_GrSLType";
554 } else if (type == *context.fVoid_Type) {
555 return "kVoid_GrSLType";
Ethan Nicholas8ae1b562019-12-17 15:18:02 -0500556 } else if (type.kind() == Type::kEnum_Kind) {
557 return "int";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400558 }
559 SkASSERT(false);
560 return nullptr;
561}
562
Ethan Nicholas762466e2017-06-29 10:03:38 -0400563void CPPCodeGenerator::writeFunction(const FunctionDefinition& f) {
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400564 const FunctionDeclaration& decl = f.fDeclaration;
Brian Osman08f986d2020-05-13 17:06:46 -0400565 if (decl.fBuiltin) {
566 return;
567 }
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400568 fFunctionHeader = "";
569 OutputStream* oldOut = fOut;
570 StringStream buffer;
571 fOut = &buffer;
572 if (decl.fName == "main") {
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400573 fInMain = true;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400574 for (const auto& s : ((Block&) *f.fBody).fStatements) {
575 this->writeStatement(*s);
576 this->writeLine();
577 }
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400578 fInMain = false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400579
580 fOut = oldOut;
581 this->write(fFunctionHeader);
582 this->write(buffer.str());
583 } else {
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400584 this->addExtraEmitCodeLine("SkString " + decl.fName + "_name;");
585 String args = "const GrShaderVar " + decl.fName + "_args[] = { ";
586 const char* separator = "";
587 for (const auto& param : decl.fParameters) {
588 args += String(separator) + "GrShaderVar(\"" + param->fName + "\", " +
589 glsltype_string(fContext, param->fType) + ")";
590 separator = ", ";
591 }
592 args += "};";
593 this->addExtraEmitCodeLine(args.c_str());
594 for (const auto& s : ((Block&) *f.fBody).fStatements) {
595 this->writeStatement(*s);
596 this->writeLine();
597 }
598
599 fOut = oldOut;
600 String emit = "fragBuilder->emitFunction(";
601 emit += glsltype_string(fContext, decl.fReturnType);
602 emit += ", \"" + decl.fName + "\"";
603 emit += ", " + to_string((int64_t) decl.fParameters.size());
604 emit += ", " + decl.fName + "_args";
John Stiles50819422020-06-18 13:00:38 -0400605 emit += ",\nR\"SkSL(" + buffer.str() + ")SkSL\"";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400606 emit += ", &" + decl.fName + "_name);";
607 this->addExtraEmitCodeLine(emit.c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400608 }
609}
610
611void CPPCodeGenerator::writeSetting(const Setting& s) {
Brian Osmanf265afd2020-08-04 13:23:36 -0400612 this->write(s.fName.c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400613}
614
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400615bool CPPCodeGenerator::writeSection(const char* name, const char* prefix) {
Ethan Nicholas68990be2017-07-13 09:36:52 -0400616 const Section* s = fSectionAndParameterHelper.getSection(name);
617 if (s) {
618 this->writef("%s%s", prefix, s->fText.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400619 return true;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400620 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400621 return false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400622}
623
624void CPPCodeGenerator::writeProgramElement(const ProgramElement& p) {
625 if (p.fKind == ProgramElement::kSection_Kind) {
626 return;
627 }
628 if (p.fKind == ProgramElement::kVar_Kind) {
629 const VarDeclarations& decls = (const VarDeclarations&) p;
630 if (!decls.fVars.size()) {
631 return;
632 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000633 const Variable& var = *((VarDeclaration&) *decls.fVars[0]).fVar;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400634 if (var.fModifiers.fFlags & (Modifiers::kIn_Flag | Modifiers::kUniform_Flag) ||
635 -1 != var.fModifiers.fLayout.fBuiltin) {
636 return;
637 }
638 }
639 INHERITED::writeProgramElement(p);
640}
641
642void CPPCodeGenerator::addUniform(const Variable& var) {
643 if (!needs_uniform_var(var)) {
644 return;
645 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400646 if (var.fModifiers.fLayout.fWhen.fLength) {
647 this->writef(" if (%s) {\n ", String(var.fModifiers.fLayout.fWhen).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400648 }
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400649 const char* type = glsltype_string(fContext, var.fType);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700650 String name(var.fName);
Ethan Nicholas16464c32020-04-06 13:53:05 -0400651 this->writef(" %sVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag,"
652 " %s, \"%s\");\n", HCodeGenerator::FieldName(name.c_str()).c_str(), type,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700653 name.c_str());
Ethan Nicholasfc994162019-06-06 10:04:27 -0400654 if (var.fModifiers.fLayout.fWhen.fLength) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400655 this->write(" }\n");
656 }
657}
658
Ethan Nicholascd700e92018-08-24 16:43:57 -0400659void CPPCodeGenerator::writeInputVars() {
660}
661
Ethan Nicholas762466e2017-06-29 10:03:38 -0400662void CPPCodeGenerator::writePrivateVars() {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400663 for (const auto& p : fProgram) {
664 if (ProgramElement::kVar_Kind == p.fKind) {
665 const VarDeclarations& decls = (const VarDeclarations&) p;
666 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000667 VarDeclaration& decl = (VarDeclaration&) *raw;
668 if (is_private(*decl.fVar)) {
669 if (decl.fVar->fType == *fContext.fFragmentProcessor_Type) {
670 fErrors.error(decl.fOffset,
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400671 "fragmentProcessor variables must be declared 'in'");
672 return;
673 }
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500674 this->writef("%s %s = %s;\n",
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000675 HCodeGenerator::FieldType(fContext, decl.fVar->fType,
676 decl.fVar->fModifiers.fLayout).c_str(),
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500677 String(decl.fVar->fName).c_str(),
678 default_value(*decl.fVar).c_str());
Michael Ludwiga4275592018-08-31 10:52:47 -0400679 } else if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
680 // An auto-tracked uniform in variable, so add a field to hold onto the prior
681 // state. Note that tracked variables must be uniform in's and that is validated
682 // before writePrivateVars() is called.
683 const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *decl.fVar);
684 SkASSERT(mapper && mapper->supportsTracking());
685
686 String name = HCodeGenerator::FieldName(String(decl.fVar->fName).c_str());
687 // The member statement is different if the mapper reports a default value
688 if (mapper->defaultValue().size() > 0) {
689 this->writef("%s %sPrev = %s;\n",
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400690 Layout::CTypeToStr(mapper->ctype()), name.c_str(),
Michael Ludwiga4275592018-08-31 10:52:47 -0400691 mapper->defaultValue().c_str());
692 } else {
693 this->writef("%s %sPrev;\n",
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400694 Layout::CTypeToStr(mapper->ctype()), name.c_str());
Michael Ludwiga4275592018-08-31 10:52:47 -0400695 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400696 }
697 }
698 }
699 }
700}
701
702void CPPCodeGenerator::writePrivateVarValues() {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400703 for (const auto& p : fProgram) {
704 if (ProgramElement::kVar_Kind == p.fKind) {
705 const VarDeclarations& decls = (const VarDeclarations&) p;
706 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000707 VarDeclaration& decl = (VarDeclaration&) *raw;
708 if (is_private(*decl.fVar) && decl.fValue) {
709 this->writef("%s = ", String(decl.fVar->fName).c_str());
Ethan Nicholas82399462017-10-16 12:35:44 -0400710 fCPPMode = true;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000711 this->writeExpression(*decl.fValue, kAssignment_Precedence);
Ethan Nicholas82399462017-10-16 12:35:44 -0400712 fCPPMode = false;
713 this->write(";\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400714 }
715 }
716 }
717 }
718}
719
Ethan Nicholas82399462017-10-16 12:35:44 -0400720static bool is_accessible(const Variable& var) {
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500721 const Type& type = var.fType.nonnullable();
722 return Type::kSampler_Kind != type.kind() &&
723 Type::kOther_Kind != type.kind();
Ethan Nicholas82399462017-10-16 12:35:44 -0400724}
725
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400726void CPPCodeGenerator::newExtraEmitCodeBlock() {
727 // This should only be called when emitting SKSL for emitCode(), which can be detected if the
728 // cpp buffer is not null, and the cpp buffer is not the current output.
729 SkASSERT(fCPPBuffer && fCPPBuffer != fOut);
730
731 // Start a new block as an empty string
732 fExtraEmitCodeBlocks.push_back("");
733 // Mark its location in the output buffer, uses ${\d} for the token since ${} will not occur in
734 // valid sksl and makes detection trivial.
735 this->writef("${%zu}", fExtraEmitCodeBlocks.size() - 1);
736}
737
738void CPPCodeGenerator::addExtraEmitCodeLine(const String& toAppend) {
739 SkASSERT(fExtraEmitCodeBlocks.size() > 0);
740 String& currentBlock = fExtraEmitCodeBlocks[fExtraEmitCodeBlocks.size() - 1];
741 // Automatically add indentation and newline
742 currentBlock += " " + toAppend + "\n";
743}
744
745void CPPCodeGenerator::flushEmittedCode() {
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400746 if (fCPPBuffer == nullptr) {
747 // Not actually within writeEmitCode() so nothing to flush
748 return;
749 }
750
751 StringStream* skslBuffer = static_cast<StringStream*>(fOut);
752
753 String sksl = skslBuffer->str();
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400754 // Empty the accumulation buffer since its current contents are consumed.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400755 skslBuffer->reset();
756
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400757 // Switch to the cpp buffer
Michael Ludwigd0440192018-09-07 14:24:52 +0000758 fOut = fCPPBuffer;
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400759
760 // Iterate through the sksl, keeping track of where the last statement ended (e.g. the latest
761 // encountered ';', '{', or '}'). If an extra emit code block token is encountered then the
762 // code from 0 to last statement end is sent to writeCodeAppend, the extra code block is
763 // appended to the cpp buffer, and then the sksl string is trimmed to start where the last
764 // statement left off (minus the encountered token).
765 size_t i = 0;
766 int flushPoint = -1;
767 int tokenStart = -1;
768 while (i < sksl.size()) {
769 if (tokenStart >= 0) {
770 // Looking for the end of the token
771 if (sksl[i] == '}') {
772 // Must append the sksl from 0 to flushPoint (inclusive) then the extra code
773 // accumulated in the block with index parsed from chars [tokenStart+2, i-1]
774 String toFlush = String(sksl.c_str(), flushPoint + 1);
775 // writeCodeAppend automatically removes the format args that it consumed, so
776 // fFormatArgs will be in a valid state for any future sksl
777 this->writeCodeAppend(toFlush);
778
779 int codeBlock = stoi(String(sksl.c_str() + tokenStart + 2, i - tokenStart - 2));
780 SkASSERT(codeBlock < (int) fExtraEmitCodeBlocks.size());
781 if (fExtraEmitCodeBlocks[codeBlock].size() > 0) {
782 this->write(fExtraEmitCodeBlocks[codeBlock].c_str());
783 }
784
785 // Now reset the sksl buffer to start after the flush point, but remove the token.
786 String compacted = String(sksl.c_str() + flushPoint + 1,
787 tokenStart - flushPoint - 1);
788 if (i < sksl.size() - 1) {
789 compacted += String(sksl.c_str() + i + 1, sksl.size() - i - 1);
790 }
791 sksl = compacted;
792
793 // And reset iteration
794 i = -1;
795 flushPoint = -1;
796 tokenStart = -1;
797 }
798 } else {
799 // Looking for the start of extra emit block tokens, and tracking when statements end
800 if (sksl[i] == ';' || sksl[i] == '{' || sksl[i] == '}') {
801 flushPoint = i;
802 } else if (i < sksl.size() - 1 && sksl[i] == '$' && sksl[i + 1] == '{') {
803 // found an extra emit code block token
804 tokenStart = i++;
805 }
806 }
807 i++;
Michael Ludwigd0440192018-09-07 14:24:52 +0000808 }
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400809
810 // Once we've gone through the sksl string to this point, there are no remaining extra emit
811 // code blocks to interleave, so append the remainder as usual.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400812 this->writeCodeAppend(sksl);
813
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400814 // After appending, switch back to the emptied sksl buffer and reset the extra code blocks
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400815 fOut = skslBuffer;
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400816 fExtraEmitCodeBlocks.clear();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400817}
818
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400819void CPPCodeGenerator::writeCodeAppend(const String& code) {
John Stiles50819422020-06-18 13:00:38 -0400820 if (!code.empty()) {
821 // Count % format specifiers.
822 size_t argCount = 0;
823 for (size_t index = 0; index < code.size(); ++index) {
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400824 if ('%' == code[index]) {
John Stiles50819422020-06-18 13:00:38 -0400825 if (index == code.size() - 1) {
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400826 break;
827 }
828 if (code[index + 1] != '%') {
829 ++argCount;
830 }
831 }
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400832 }
John Stiles50819422020-06-18 13:00:38 -0400833
834 // Emit the code string.
835 this->writef(" fragBuilder->codeAppendf(\n"
836 "R\"SkSL(%s)SkSL\"\n", code.c_str());
837 for (size_t i = 0; i < argCount; ++i) {
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400838 this->writef(", %s", fFormatArgs[i].c_str());
839 }
840 this->write(");\n");
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400841
John Stiles50819422020-06-18 13:00:38 -0400842 // argCount is equal to the number of fFormatArgs that were consumed, so they should be
843 // removed from the list.
844 if (argCount > 0) {
845 fFormatArgs.erase(fFormatArgs.begin(), fFormatArgs.begin() + argCount);
846 }
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400847 }
848}
849
850String CPPCodeGenerator::convertSKSLExpressionToCPP(const Expression& e,
851 const String& cppVar) {
852 // To do this conversion, we temporarily switch the sksl output stream
853 // to an empty stringstream and reset the format args to empty.
854 OutputStream* oldSKSL = fOut;
855 StringStream exprBuffer;
856 fOut = &exprBuffer;
857
858 std::vector<String> oldArgs(fFormatArgs);
859 fFormatArgs.clear();
860
861 // Convert the argument expression into a format string and args
862 this->writeExpression(e, Precedence::kTopLevel_Precedence);
863 std::vector<String> newArgs(fFormatArgs);
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400864 String expr = exprBuffer.str();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400865
866 // After generating, restore the original output stream and format args
867 fFormatArgs = oldArgs;
868 fOut = oldSKSL;
869
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400870 // The sksl written to exprBuffer is not processed by flushEmittedCode(), so any extra emit code
871 // block tokens won't get handled. So we need to strip them from the expression and stick them
872 // to the end of the original sksl stream.
873 String exprFormat = "";
874 int tokenStart = -1;
875 for (size_t i = 0; i < expr.size(); i++) {
876 if (tokenStart >= 0) {
877 if (expr[i] == '}') {
878 // End of the token, so append the token to fOut
879 fOut->write(expr.c_str() + tokenStart, i - tokenStart + 1);
880 tokenStart = -1;
881 }
882 } else {
883 if (i < expr.size() - 1 && expr[i] == '$' && expr[i + 1] == '{') {
884 tokenStart = i++;
885 } else {
886 exprFormat += expr[i];
887 }
888 }
889 }
890
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400891 // Now build the final C++ code snippet from the format string and args
892 String cppExpr;
John Stiles50819422020-06-18 13:00:38 -0400893 if (newArgs.empty()) {
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400894 // This was a static expression, so we can simplify the input
895 // color declaration in the emitted code to just a static string
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400896 cppExpr = "SkString " + cppVar + "(\"" + exprFormat + "\");";
John Stiles50819422020-06-18 13:00:38 -0400897 } else if (newArgs.size() == 1 && exprFormat == "%s") {
898 // If the format expression is simply "%s", we can avoid an expensive call to printf.
899 // This happens fairly often in codegen so it is worth simplifying.
900 cppExpr = "SkString " + cppVar + "(" + newArgs[0] + ");";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400901 } else {
902 // String formatting must occur dynamically, so have the C++ declaration
903 // use SkStringPrintf with the format args that were accumulated
904 // when the expression was written.
905 cppExpr = "SkString " + cppVar + " = SkStringPrintf(\"" + exprFormat + "\"";
906 for (size_t i = 0; i < newArgs.size(); i++) {
907 cppExpr += ", " + newArgs[i];
908 }
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400909 cppExpr += ");";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400910 }
911 return cppExpr;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400912}
913
Ethan Nicholas762466e2017-06-29 10:03:38 -0400914bool CPPCodeGenerator::writeEmitCode(std::vector<const Variable*>& uniforms) {
915 this->write(" void emitCode(EmitArgs& args) override {\n"
916 " GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;\n");
917 this->writef(" const %s& _outer = args.fFp.cast<%s>();\n"
918 " (void) _outer;\n",
919 fFullName.c_str(), fFullName.c_str());
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400920 for (const auto& p : fProgram) {
921 if (ProgramElement::kVar_Kind == p.fKind) {
922 const VarDeclarations& decls = (const VarDeclarations&) p;
923 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000924 VarDeclaration& decl = (VarDeclaration&) *raw;
925 String nameString(decl.fVar->fName);
Ethan Nicholas82399462017-10-16 12:35:44 -0400926 const char* name = nameString.c_str();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000927 if (SectionAndParameterHelper::IsParameter(*decl.fVar) &&
928 is_accessible(*decl.fVar)) {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400929 this->writef(" auto %s = _outer.%s;\n"
Ethan Nicholas82399462017-10-16 12:35:44 -0400930 " (void) %s;\n",
931 name, name, name);
932 }
933 }
934 }
935 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400936 this->writePrivateVarValues();
937 for (const auto u : uniforms) {
938 this->addUniform(*u);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400939 }
John Stiles02b11282020-08-10 15:25:24 -0400940 this->writeSection(kEmitCodeSection);
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400941
942 // Save original buffer as the CPP buffer for flushEmittedCode()
943 fCPPBuffer = fOut;
944 StringStream skslBuffer;
945 fOut = &skslBuffer;
946
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400947 this->newExtraEmitCodeBlock();
Ethan Nicholas762466e2017-06-29 10:03:38 -0400948 bool result = INHERITED::generateCode();
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400949 this->flushEmittedCode();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400950
951 // Then restore the original CPP buffer and close the function
952 fOut = fCPPBuffer;
953 fCPPBuffer = nullptr;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400954 this->write(" }\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400955 return result;
956}
957
958void CPPCodeGenerator::writeSetData(std::vector<const Variable*>& uniforms) {
959 const char* fullName = fFullName.c_str();
John Stiles02b11282020-08-10 15:25:24 -0400960 const Section* section = fSectionAndParameterHelper.getSection(kSetDataSection);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400961 const char* pdman = section ? section->fArgument.c_str() : "pdman";
Ethan Nicholas762466e2017-06-29 10:03:38 -0400962 this->writef(" void onSetData(const GrGLSLProgramDataManager& %s, "
963 "const GrFragmentProcessor& _proc) override {\n",
964 pdman);
965 bool wroteProcessor = false;
John Stiles06f3d082020-06-04 11:07:21 -0400966 for (const Variable* u : uniforms) {
Michael Ludwiga4275592018-08-31 10:52:47 -0400967 if (is_uniform_in(*u)) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400968 if (!wroteProcessor) {
969 this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName, fullName);
970 wroteProcessor = true;
971 this->writef(" {\n");
972 }
Michael Ludwiga4275592018-08-31 10:52:47 -0400973
974 const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *u);
975 SkASSERT(mapper);
976
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700977 String nameString(u->fName);
978 const char* name = nameString.c_str();
Michael Ludwiga4275592018-08-31 10:52:47 -0400979
980 // Switches for setData behavior in the generated code
981 bool conditionalUniform = u->fModifiers.fLayout.fWhen != "";
982 bool isTracked = u->fModifiers.fLayout.fFlags & Layout::kTracked_Flag;
983 bool needsValueDeclaration = isTracked || !mapper->canInlineUniformValue();
984
985 String uniformName = HCodeGenerator::FieldName(name) + "Var";
986
987 String indent = " "; // 8 by default, 12 when nested for conditional uniforms
988 if (conditionalUniform) {
989 // Add a pre-check to make sure the uniform was emitted
990 // before trying to send any data to the GPU
991 this->writef(" if (%s.isValid()) {\n", uniformName.c_str());
992 indent += " ";
993 }
994
995 String valueVar = "";
996 if (needsValueDeclaration) {
997 valueVar.appendf("%sValue", name);
998 // Use AccessType since that will match the return type of _outer's public API.
999 String valueType = HCodeGenerator::AccessType(fContext, u->fType,
1000 u->fModifiers.fLayout);
Ethan Nicholasbcd51e82019-04-09 10:40:41 -04001001 this->writef("%s%s %s = _outer.%s;\n",
Michael Ludwiga4275592018-08-31 10:52:47 -04001002 indent.c_str(), valueType.c_str(), valueVar.c_str(), name);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001003 } else {
Michael Ludwiga4275592018-08-31 10:52:47 -04001004 // Not tracked and the mapper only needs to use the value once
1005 // so send it a safe expression instead of the variable name
Ethan Nicholasbcd51e82019-04-09 10:40:41 -04001006 valueVar.appendf("(_outer.%s)", name);
Michael Ludwiga4275592018-08-31 10:52:47 -04001007 }
1008
1009 if (isTracked) {
1010 SkASSERT(mapper->supportsTracking());
1011
1012 String prevVar = HCodeGenerator::FieldName(name) + "Prev";
1013 this->writef("%sif (%s) {\n"
1014 "%s %s;\n"
1015 "%s %s;\n"
1016 "%s}\n", indent.c_str(),
1017 mapper->dirtyExpression(valueVar, prevVar).c_str(), indent.c_str(),
1018 mapper->saveState(valueVar, prevVar).c_str(), indent.c_str(),
1019 mapper->setUniform(pdman, uniformName, valueVar).c_str(), indent.c_str());
1020 } else {
1021 this->writef("%s%s;\n", indent.c_str(),
1022 mapper->setUniform(pdman, uniformName, valueVar).c_str());
1023 }
1024
1025 if (conditionalUniform) {
1026 // Close the earlier precheck block
1027 this->writef(" }\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -04001028 }
1029 }
1030 }
1031 if (wroteProcessor) {
1032 this->writef(" }\n");
1033 }
Ethan Nicholas68990be2017-07-13 09:36:52 -04001034 if (section) {
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05001035 int samplerIndex = 0;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001036 for (const auto& p : fProgram) {
1037 if (ProgramElement::kVar_Kind == p.fKind) {
1038 const VarDeclarations& decls = (const VarDeclarations&) p;
John Stiles06f3d082020-06-04 11:07:21 -04001039 for (const std::unique_ptr<Statement>& raw : decls.fVars) {
1040 const VarDeclaration& decl = static_cast<VarDeclaration&>(*raw);
1041 const Variable& variable = *decl.fVar;
1042 String nameString(variable.fName);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001043 const char* name = nameString.c_str();
John Stiles06f3d082020-06-04 11:07:21 -04001044 if (variable.fType.kind() == Type::kSampler_Kind) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +00001045 this->writef(" const GrSurfaceProxyView& %sView = "
1046 "_outer.textureSampler(%d).view();\n",
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05001047 name, samplerIndex);
Robert Phillipsbd99c0c2019-12-12 13:26:58 +00001048 this->writef(" GrTexture& %s = *%sView.proxy()->peekTexture();\n",
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05001049 name, name);
1050 this->writef(" (void) %s;\n", name);
1051 ++samplerIndex;
John Stiles06f3d082020-06-04 11:07:21 -04001052 } else if (needs_uniform_var(variable)) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001053 this->writef(" UniformHandle& %s = %sVar;\n"
1054 " (void) %s;\n",
1055 name, HCodeGenerator::FieldName(name).c_str(), name);
John Stiles06f3d082020-06-04 11:07:21 -04001056 } else if (SectionAndParameterHelper::IsParameter(variable) &&
1057 variable.fType != *fContext.fFragmentProcessor_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001058 if (!wroteProcessor) {
1059 this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName,
1060 fullName);
1061 wroteProcessor = true;
1062 }
John Stiles06f3d082020-06-04 11:07:21 -04001063
1064 if (variable.fType.nonnullable() != *fContext.fFragmentProcessor_Type) {
1065 this->writef(" auto %s = _outer.%s;\n"
1066 " (void) %s;\n",
1067 name, name, name);
1068 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001069 }
1070 }
1071 }
1072 }
John Stiles02b11282020-08-10 15:25:24 -04001073 this->writeSection(kSetDataSection);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001074 }
1075 this->write(" }\n");
1076}
1077
Brian Salomonf7dcd762018-07-30 14:48:15 -04001078void CPPCodeGenerator::writeOnTextureSampler() {
1079 bool foundSampler = false;
1080 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
1081 if (param->fType.kind() == Type::kSampler_Kind) {
1082 if (!foundSampler) {
1083 this->writef(
1084 "const GrFragmentProcessor::TextureSampler& %s::onTextureSampler(int "
1085 "index) const {\n",
1086 fFullName.c_str());
1087 this->writef(" return IthTextureSampler(index, %s",
1088 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
1089 foundSampler = true;
1090 } else {
1091 this->writef(", %s",
1092 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
1093 }
1094 }
1095 }
1096 if (foundSampler) {
1097 this->write(");\n}\n");
1098 }
1099}
1100
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001101void CPPCodeGenerator::writeClone() {
John Stiles02b11282020-08-10 15:25:24 -04001102 if (!this->writeSection(kCloneSection)) {
1103 if (fSectionAndParameterHelper.getSection(kFieldsSection)) {
John Stiles47b4e222020-08-12 09:56:50 -04001104 fErrors.error(/*offset=*/0, "fragment processors with custom @fields must also have a "
1105 "custom @clone");
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001106 }
1107 this->writef("%s::%s(const %s& src)\n"
Ethan Nicholasabff9562017-10-09 10:54:08 -04001108 ": INHERITED(k%s_ClassID, src.optimizationFlags())", fFullName.c_str(),
1109 fFullName.c_str(), fFullName.c_str(), fFullName.c_str());
John Stiles06f3d082020-06-04 11:07:21 -04001110 for (const Variable* param : fSectionAndParameterHelper.getParameters()) {
Robert Phillipsbce7d862019-02-21 22:53:57 +00001111 String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str());
John Stiles88183902020-06-10 16:40:38 -04001112 if (param->fType.nonnullable() != *fContext.fFragmentProcessor_Type) {
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001113 this->writef("\n, %s(src.%s)",
1114 fieldName.c_str(),
1115 fieldName.c_str());
1116 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001117 }
Ethan Nicholasabff9562017-10-09 10:54:08 -04001118 this->writef(" {\n");
Brian Osman12c5d292020-07-13 16:11:35 -04001119 this->writef(" this->cloneAndRegisterAllChildProcessors(src);\n");
Brian Salomonf7dcd762018-07-30 14:48:15 -04001120 int samplerCount = 0;
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001121 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
1122 if (param->fType.kind() == Type::kSampler_Kind) {
Brian Salomonf7dcd762018-07-30 14:48:15 -04001123 ++samplerCount;
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001124 }
1125 }
Brian Salomonf7dcd762018-07-30 14:48:15 -04001126 if (samplerCount) {
1127 this->writef(" this->setTextureSamplerCnt(%d);", samplerCount);
1128 }
Michael Ludwige88320b2020-06-24 09:04:56 -04001129 if (fAccessSampleCoordsDirectly) {
1130 this->writef(" this->setUsesSampleCoordsDirectly();\n");
1131 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001132 this->write("}\n");
Brian Salomonaff329b2017-08-11 09:40:37 -04001133 this->writef("std::unique_ptr<GrFragmentProcessor> %s::clone() const {\n",
1134 fFullName.c_str());
John Stilesfbd050b2020-08-03 13:21:46 -04001135 this->writef(" return std::make_unique<%s>(*this);\n",
Brian Salomonaff329b2017-08-11 09:40:37 -04001136 fFullName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001137 this->write("}\n");
1138 }
1139}
1140
John Stiles47b4e222020-08-12 09:56:50 -04001141void CPPCodeGenerator::writeDumpInfo() {
John Stilese0709e92020-08-12 15:39:58 +00001142 this->writef("#ifdef SK_DEBUG\n"
John Stiles47b4e222020-08-12 09:56:50 -04001143 "SkString %s::dumpInfo() const {\n", fFullName.c_str());
1144
1145 if (!this->writeSection(kDumpInfoSection)) {
1146 if (fSectionAndParameterHelper.getSection(kFieldsSection)) {
1147 fErrors.error(/*offset=*/0, "fragment processors with custom @fields must also have a "
1148 "custom @dumpInfo");
1149 }
1150
1151 this->writef(" return SkStringPrintf(\"%s", fName.c_str());
1152
1153 String formatString;
1154 std::vector<String> argumentList;
1155
1156 for (const Variable* param : fSectionAndParameterHelper.getParameters()) {
1157 // dumpInfo() doesn't need to log child FPs.
1158 if (param->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
1159 continue;
1160 }
1161
1162 // Add this field onto the format string and argument list.
1163 String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str());
1164 String runtimeValue = this->formatRuntimeValue(param->fType, param->fModifiers.fLayout,
1165 param->fName, &argumentList);
1166 formatString.appendf("%s%s=%s",
1167 formatString.empty() ? "" : ", ",
1168 fieldName.c_str(),
1169 runtimeValue.c_str());
1170 }
1171
1172 // Append finished format string.
1173 if (!formatString.empty()) {
1174 this->writef("(%s)", formatString.c_str());
1175 }
1176
1177 // Close-quote, then append each argument.
1178 this->write("\"");
1179 for (const String& argument : argumentList) {
1180 this->writef(", %s", argument.c_str());
1181 }
1182
1183 this->write(");");
1184 }
1185
1186 this->write("\n}\n"
1187 "#endif\n");
1188}
1189
Ethan Nicholas762466e2017-06-29 10:03:38 -04001190void CPPCodeGenerator::writeTest() {
John Stiles02b11282020-08-10 15:25:24 -04001191 const Section* test = fSectionAndParameterHelper.getSection(kTestCodeSection);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001192 if (test) {
Brian Salomonaff329b2017-08-11 09:40:37 -04001193 this->writef(
1194 "GR_DEFINE_FRAGMENT_PROCESSOR_TEST(%s);\n"
1195 "#if GR_TEST_UTILS\n"
1196 "std::unique_ptr<GrFragmentProcessor> %s::TestCreate(GrProcessorTestData* %s) {\n",
1197 fFullName.c_str(),
1198 fFullName.c_str(),
1199 test->fArgument.c_str());
John Stiles02b11282020-08-10 15:25:24 -04001200 this->writeSection(kTestCodeSection);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001201 this->write("}\n"
1202 "#endif\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -04001203 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001204}
1205
1206void CPPCodeGenerator::writeGetKey() {
1207 this->writef("void %s::onGetGLSLProcessorKey(const GrShaderCaps& caps, "
1208 "GrProcessorKeyBuilder* b) const {\n",
1209 fFullName.c_str());
Ethan Nicholascab767f2019-07-01 13:32:07 -04001210 for (const auto& p : fProgram) {
1211 if (ProgramElement::kVar_Kind == p.fKind) {
1212 const VarDeclarations& decls = (const VarDeclarations&) p;
1213 for (const auto& raw : decls.fVars) {
1214 const VarDeclaration& decl = (VarDeclaration&) *raw;
1215 const Variable& var = *decl.fVar;
1216 String nameString(var.fName);
1217 const char* name = nameString.c_str();
1218 if (var.fModifiers.fLayout.fKey != Layout::kNo_Key &&
1219 (var.fModifiers.fFlags & Modifiers::kUniform_Flag)) {
1220 fErrors.error(var.fOffset,
1221 "layout(key) may not be specified on uniforms");
Ethan Nicholasbcd51e82019-04-09 10:40:41 -04001222 }
Ethan Nicholascab767f2019-07-01 13:32:07 -04001223 switch (var.fModifiers.fLayout.fKey) {
1224 case Layout::kKey_Key:
1225 if (is_private(var)) {
1226 this->writef("%s %s =",
1227 HCodeGenerator::FieldType(fContext, var.fType,
1228 var.fModifiers.fLayout).c_str(),
1229 String(var.fName).c_str());
1230 if (decl.fValue) {
1231 fCPPMode = true;
1232 this->writeExpression(*decl.fValue, kAssignment_Precedence);
1233 fCPPMode = false;
1234 } else {
1235 this->writef("%s", default_value(var).c_str());
1236 }
1237 this->write(";\n");
1238 }
1239 if (var.fModifiers.fLayout.fWhen.fLength) {
1240 this->writef("if (%s) {", String(var.fModifiers.fLayout.fWhen).c_str());
1241 }
John Stilesb3038f82020-07-27 17:33:25 -04001242 if (var.fType == *fContext.fHalf4_Type) {
Ethan Nicholascab767f2019-07-01 13:32:07 -04001243 this->writef(" uint16_t red = SkFloatToHalf(%s.fR);\n",
1244 HCodeGenerator::FieldName(name).c_str());
1245 this->writef(" uint16_t green = SkFloatToHalf(%s.fG);\n",
1246 HCodeGenerator::FieldName(name).c_str());
1247 this->writef(" uint16_t blue = SkFloatToHalf(%s.fB);\n",
1248 HCodeGenerator::FieldName(name).c_str());
1249 this->writef(" uint16_t alpha = SkFloatToHalf(%s.fA);\n",
1250 HCodeGenerator::FieldName(name).c_str());
1251 this->write(" b->add32(((uint32_t)red << 16) | green);\n");
1252 this->write(" b->add32(((uint32_t)blue << 16) | alpha);\n");
John Stiles45f5b032020-07-27 17:31:29 -04001253 } else if (var.fType == *fContext.fHalf_Type ||
1254 var.fType == *fContext.fFloat_Type) {
1255 this->writef(" b->add32(sk_bit_cast<uint32_t>(%s));\n",
Ethan Nicholascab767f2019-07-01 13:32:07 -04001256 HCodeGenerator::FieldName(name).c_str());
John Stiles45f5b032020-07-27 17:31:29 -04001257 } else if (var.fType.isInteger() || var.fType == *fContext.fBool_Type ||
1258 var.fType.kind() == Type::kEnum_Kind) {
1259 this->writef(" b->add32((uint32_t) %s);\n",
1260 HCodeGenerator::FieldName(name).c_str());
1261 } else {
1262 ABORT("NOT YET IMPLEMENTED: automatic key handling for %s\n",
1263 var.fType.displayName().c_str());
Ethan Nicholascab767f2019-07-01 13:32:07 -04001264 }
1265 if (var.fModifiers.fLayout.fWhen.fLength) {
1266 this->write("}");
1267 }
1268 break;
1269 case Layout::kIdentity_Key:
1270 if (var.fType.kind() != Type::kMatrix_Kind) {
1271 fErrors.error(var.fOffset,
1272 "layout(key=identity) requires matrix type");
1273 }
1274 this->writef(" b->add32(%s.isIdentity() ? 1 : 0);\n",
1275 HCodeGenerator::FieldName(name).c_str());
1276 break;
1277 case Layout::kNo_Key:
1278 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001279 }
Ethan Nicholascab767f2019-07-01 13:32:07 -04001280 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001281 }
1282 }
1283 this->write("}\n");
1284}
1285
1286bool CPPCodeGenerator::generateCode() {
1287 std::vector<const Variable*> uniforms;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001288 for (const auto& p : fProgram) {
1289 if (ProgramElement::kVar_Kind == p.fKind) {
1290 const VarDeclarations& decls = (const VarDeclarations&) p;
1291 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001292 VarDeclaration& decl = (VarDeclaration&) *raw;
1293 if ((decl.fVar->fModifiers.fFlags & Modifiers::kUniform_Flag) &&
1294 decl.fVar->fType.kind() != Type::kSampler_Kind) {
1295 uniforms.push_back(decl.fVar);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001296 }
Michael Ludwiga4275592018-08-31 10:52:47 -04001297
1298 if (is_uniform_in(*decl.fVar)) {
1299 // Validate the "uniform in" declarations to make sure they are fully supported,
1300 // instead of generating surprising C++
1301 const UniformCTypeMapper* mapper =
1302 UniformCTypeMapper::Get(fContext, *decl.fVar);
1303 if (mapper == nullptr) {
1304 fErrors.error(decl.fOffset, String(decl.fVar->fName)
1305 + "'s type is not supported for use as a 'uniform in'");
1306 return false;
1307 }
1308 if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
1309 if (!mapper->supportsTracking()) {
1310 fErrors.error(decl.fOffset, String(decl.fVar->fName)
1311 + "'s type does not support state tracking");
1312 return false;
1313 }
1314 }
1315
1316 } else {
1317 // If it's not a uniform_in, it's an error to be tracked
1318 if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
1319 fErrors.error(decl.fOffset, "Non-'in uniforms' cannot be tracked");
1320 return false;
1321 }
1322 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001323 }
1324 }
1325 }
1326 const char* baseName = fName.c_str();
1327 const char* fullName = fFullName.c_str();
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05001328 this->writef("%s\n", HCodeGenerator::GetHeader(fProgram, fErrors).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001329 this->writef(kFragmentProcessorHeader, fullName);
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001330 this->writef("#include \"%s.h\"\n\n", fullName);
John Stiles02b11282020-08-10 15:25:24 -04001331 this->writeSection(kCppSection);
John Stiles45f5b032020-07-27 17:31:29 -04001332 this->writef("#include \"src/core/SkUtils.h\"\n"
1333 "#include \"src/gpu/GrTexture.h\"\n"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001334 "#include \"src/gpu/glsl/GrGLSLFragmentProcessor.h\"\n"
1335 "#include \"src/gpu/glsl/GrGLSLFragmentShaderBuilder.h\"\n"
1336 "#include \"src/gpu/glsl/GrGLSLProgramBuilder.h\"\n"
1337 "#include \"src/sksl/SkSLCPP.h\"\n"
1338 "#include \"src/sksl/SkSLUtil.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -04001339 "class GrGLSL%s : public GrGLSLFragmentProcessor {\n"
1340 "public:\n"
1341 " GrGLSL%s() {}\n",
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04001342 baseName, baseName);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001343 bool result = this->writeEmitCode(uniforms);
1344 this->write("private:\n");
1345 this->writeSetData(uniforms);
1346 this->writePrivateVars();
1347 for (const auto& u : uniforms) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001348 if (needs_uniform_var(*u) && !(u->fModifiers.fFlags & Modifiers::kIn_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001349 this->writef(" UniformHandle %sVar;\n",
1350 HCodeGenerator::FieldName(String(u->fName).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001351 }
1352 }
Ethan Nicholas68990be2017-07-13 09:36:52 -04001353 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001354 if (needs_uniform_var(*param)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001355 this->writef(" UniformHandle %sVar;\n",
1356 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001357 }
1358 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001359 this->writef("};\n"
1360 "GrGLSLFragmentProcessor* %s::onCreateGLSLInstance() const {\n"
1361 " return new GrGLSL%s();\n"
1362 "}\n",
1363 fullName, baseName);
1364 this->writeGetKey();
1365 this->writef("bool %s::onIsEqual(const GrFragmentProcessor& other) const {\n"
1366 " const %s& that = other.cast<%s>();\n"
1367 " (void) that;\n",
1368 fullName, fullName, fullName);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001369 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001370 if (param->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -04001371 continue;
1372 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001373 String nameString(param->fName);
1374 const char* name = nameString.c_str();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001375 this->writef(" if (%s != that.%s) return false;\n",
1376 HCodeGenerator::FieldName(name).c_str(),
1377 HCodeGenerator::FieldName(name).c_str());
1378 }
1379 this->write(" return true;\n"
1380 "}\n");
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001381 this->writeClone();
John Stiles47b4e222020-08-12 09:56:50 -04001382 this->writeDumpInfo();
Brian Salomonf7dcd762018-07-30 14:48:15 -04001383 this->writeOnTextureSampler();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001384 this->writeTest();
John Stiles02b11282020-08-10 15:25:24 -04001385 this->writeSection(kCppEndSection);
Greg Daniel3e8c3452018-04-06 10:37:55 -04001386
Ethan Nicholas762466e2017-06-29 10:03:38 -04001387 result &= 0 == fErrors.errorCount();
1388 return result;
1389}
1390
John Stilesa6841be2020-08-06 14:11:56 -04001391} // namespace SkSL