blob: a322ebd5e9283d90cca70e63bcbdfc5b8ab2821b [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
8#include "SkSLCPPCodeGenerator.h"
9
10#include "SkSLCompiler.h"
Michael Ludwiga4275592018-08-31 10:52:47 -040011#include "SkSLCPPUniformCTypes.h"
Ethan Nicholas762466e2017-06-29 10:03:38 -040012#include "SkSLHCodeGenerator.h"
13
Michael Ludwig92e4c7f2018-08-30 16:08:18 -040014#include <algorithm>
15
Ethan Nicholas762466e2017-06-29 10:03:38 -040016namespace SkSL {
17
18static bool needs_uniform_var(const Variable& var) {
Ethan Nicholas5f9836e2017-12-20 15:16:33 -050019 return (var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
20 var.fType.kind() != Type::kSampler_Kind;
Ethan Nicholas762466e2017-06-29 10:03:38 -040021}
22
23CPPCodeGenerator::CPPCodeGenerator(const Context* context, const Program* program,
24 ErrorReporter* errors, String name, OutputStream* out)
25: INHERITED(context, program, errors, out)
26, fName(std::move(name))
27, fFullName(String::printf("Gr%s", fName.c_str()))
28, fSectionAndParameterHelper(*program, *errors) {
29 fLineEnding = "\\n";
30}
31
32void CPPCodeGenerator::writef(const char* s, va_list va) {
33 static constexpr int BUFFER_SIZE = 1024;
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040034 va_list copy;
35 va_copy(copy, va);
Ethan Nicholas762466e2017-06-29 10:03:38 -040036 char buffer[BUFFER_SIZE];
37 int length = vsnprintf(buffer, BUFFER_SIZE, s, va);
38 if (length < BUFFER_SIZE) {
39 fOut->write(buffer, length);
40 } else {
41 std::unique_ptr<char[]> heap(new char[length + 1]);
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040042 vsprintf(heap.get(), s, copy);
Ethan Nicholas762466e2017-06-29 10:03:38 -040043 fOut->write(heap.get(), length);
44 }
z102.zhangd74f2c82018-08-10 09:08:47 +080045 va_end(copy);
Ethan Nicholas762466e2017-06-29 10:03:38 -040046}
47
48void CPPCodeGenerator::writef(const char* s, ...) {
49 va_list va;
50 va_start(va, s);
51 this->writef(s, va);
52 va_end(va);
53}
54
55void CPPCodeGenerator::writeHeader() {
56}
57
Ethan Nicholasf7b88202017-09-18 14:10:39 -040058bool CPPCodeGenerator::usesPrecisionModifiers() const {
59 return false;
Ethan Nicholas762466e2017-06-29 10:03:38 -040060}
61
Ethan Nicholasf7b88202017-09-18 14:10:39 -040062String CPPCodeGenerator::getTypeName(const Type& type) {
63 return type.name();
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040064}
Ethan Nicholasf7b88202017-09-18 14:10:39 -040065
Ethan Nicholas762466e2017-06-29 10:03:38 -040066void CPPCodeGenerator::writeBinaryExpression(const BinaryExpression& b,
67 Precedence parentPrecedence) {
68 if (b.fOperator == Token::PERCENT) {
69 // need to use "%%" instead of "%" b/c the code will be inside of a printf
70 Precedence precedence = GetBinaryPrecedence(b.fOperator);
71 if (precedence >= parentPrecedence) {
72 this->write("(");
73 }
74 this->writeExpression(*b.fLeft, precedence);
75 this->write(" %% ");
76 this->writeExpression(*b.fRight, precedence);
77 if (precedence >= parentPrecedence) {
78 this->write(")");
79 }
Ethan Nicholasee1c8a72019-02-22 10:50:47 -050080 } else if (b.fLeft->fKind == Expression::kNullLiteral_Kind ||
81 b.fRight->fKind == Expression::kNullLiteral_Kind) {
82 const Variable* var;
83 if (b.fLeft->fKind != Expression::kNullLiteral_Kind) {
84 SkASSERT(b.fLeft->fKind == Expression::kVariableReference_Kind);
85 var = &((VariableReference&) *b.fLeft).fVariable;
86 } else {
87 SkASSERT(b.fRight->fKind == Expression::kVariableReference_Kind);
88 var = &((VariableReference&) *b.fRight).fVariable;
89 }
90 SkASSERT(var->fType.kind() == Type::kNullable_Kind &&
91 var->fType.componentType() == *fContext.fFragmentProcessor_Type);
92 this->write("%s");
93 const char* op;
94 switch (b.fOperator) {
95 case Token::EQEQ:
96 op = "<";
97 break;
98 case Token::NEQ:
99 op = ">=";
100 break;
101 default:
102 SkASSERT(false);
103 }
104 fFormatArgs.push_back("_outer." + String(var->fName) + "_index() " + op + " 0 ? \"true\" "
105 ": \"false\"");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400106 } else {
107 INHERITED::writeBinaryExpression(b, parentPrecedence);
108 }
109}
110
111void CPPCodeGenerator::writeIndexExpression(const IndexExpression& i) {
112 const Expression& base = *i.fBase;
113 if (base.fKind == Expression::kVariableReference_Kind) {
114 int builtin = ((VariableReference&) base).fVariable.fModifiers.fLayout.fBuiltin;
115 if (SK_TRANSFORMEDCOORDS2D_BUILTIN == builtin) {
116 this->write("%s");
117 if (i.fIndex->fKind != Expression::kIntLiteral_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700118 fErrors.error(i.fIndex->fOffset,
Ethan Nicholas762466e2017-06-29 10:03:38 -0400119 "index into sk_TransformedCoords2D must be an integer literal");
120 return;
121 }
122 int64_t index = ((IntLiteral&) *i.fIndex).fValue;
123 String name = "sk_TransformedCoords2D_" + to_string(index);
124 fFormatArgs.push_back(name + ".c_str()");
125 if (fWrittenTransformedCoords.find(index) == fWrittenTransformedCoords.end()) {
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400126 addExtraEmitCodeLine("SkString " + name +
127 " = fragBuilder->ensureCoords2D(args.fTransformedCoords[" +
128 to_string(index) + "]);");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400129 fWrittenTransformedCoords.insert(index);
130 }
131 return;
132 } else if (SK_TEXTURESAMPLERS_BUILTIN == builtin) {
133 this->write("%s");
134 if (i.fIndex->fKind != Expression::kIntLiteral_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700135 fErrors.error(i.fIndex->fOffset,
Ethan Nicholas762466e2017-06-29 10:03:38 -0400136 "index into sk_TextureSamplers must be an integer literal");
137 return;
138 }
139 int64_t index = ((IntLiteral&) *i.fIndex).fValue;
140 fFormatArgs.push_back(" fragBuilder->getProgramBuilder()->samplerVariable("
141 "args.fTexSamplers[" + to_string(index) + "]).c_str()");
142 return;
143 }
144 }
145 INHERITED::writeIndexExpression(i);
146}
147
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400148static String default_value(const Type& type) {
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500149 if (type.fName == "bool") {
150 return "false";
151 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400152 switch (type.kind()) {
153 case Type::kScalar_Kind: return "0";
154 case Type::kVector_Kind: return type.name() + "(0)";
155 case Type::kMatrix_Kind: return type.name() + "(1)";
156 default: ABORT("unsupported default_value type\n");
157 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400158}
159
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500160static String default_value(const Variable& var) {
Brian Osman495993a2018-10-16 15:45:55 -0400161 if (var.fModifiers.fLayout.fCType == SkSL::Layout::CType::kSkPMColor4f) {
Brian Osmanf28e55d2018-10-03 16:35:54 -0400162 return "{SK_FloatNaN, SK_FloatNaN, SK_FloatNaN, SK_FloatNaN}";
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500163 }
164 return default_value(var.fType);
165}
166
Ethan Nicholas762466e2017-06-29 10:03:38 -0400167static bool is_private(const Variable& var) {
168 return !(var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
169 !(var.fModifiers.fFlags & Modifiers::kIn_Flag) &&
170 var.fStorage == Variable::kGlobal_Storage &&
171 var.fModifiers.fLayout.fBuiltin == -1;
172}
173
Michael Ludwiga4275592018-08-31 10:52:47 -0400174static bool is_uniform_in(const Variable& var) {
175 return (var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
176 (var.fModifiers.fFlags & Modifiers::kIn_Flag) &&
177 var.fType.kind() != Type::kSampler_Kind;
178}
179
Ethan Nicholasd608c092017-10-26 09:30:08 -0400180void CPPCodeGenerator::writeRuntimeValue(const Type& type, const Layout& layout,
181 const String& cppCode) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400182 if (type.isFloat()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400183 this->write("%f");
184 fFormatArgs.push_back(cppCode);
185 } else if (type == *fContext.fInt_Type) {
186 this->write("%d");
187 fFormatArgs.push_back(cppCode);
188 } else if (type == *fContext.fBool_Type) {
189 this->write("%s");
190 fFormatArgs.push_back("(" + cppCode + " ? \"true\" : \"false\")");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400191 } else if (type == *fContext.fFloat2_Type || type == *fContext.fHalf2_Type) {
192 this->write(type.name() + "(%f, %f)");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400193 fFormatArgs.push_back(cppCode + ".fX");
194 fFormatArgs.push_back(cppCode + ".fY");
Ethan Nicholas82399462017-10-16 12:35:44 -0400195 } else if (type == *fContext.fFloat4_Type || type == *fContext.fHalf4_Type) {
196 this->write(type.name() + "(%f, %f, %f, %f)");
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400197 switch (layout.fCType) {
198 case Layout::CType::kSkPMColor:
199 fFormatArgs.push_back("SkGetPackedR32(" + cppCode + ") / 255.0");
200 fFormatArgs.push_back("SkGetPackedG32(" + cppCode + ") / 255.0");
201 fFormatArgs.push_back("SkGetPackedB32(" + cppCode + ") / 255.0");
202 fFormatArgs.push_back("SkGetPackedA32(" + cppCode + ") / 255.0");
203 break;
Brian Osmanf28e55d2018-10-03 16:35:54 -0400204 case Layout::CType::kSkPMColor4f:
205 fFormatArgs.push_back(cppCode + ".fR");
206 fFormatArgs.push_back(cppCode + ".fG");
207 fFormatArgs.push_back(cppCode + ".fB");
208 fFormatArgs.push_back(cppCode + ".fA");
209 break;
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400210 case Layout::CType::kSkRect: // fall through
211 case Layout::CType::kDefault:
212 fFormatArgs.push_back(cppCode + ".left()");
213 fFormatArgs.push_back(cppCode + ".top()");
214 fFormatArgs.push_back(cppCode + ".right()");
215 fFormatArgs.push_back(cppCode + ".bottom()");
216 break;
217 default:
218 SkASSERT(false);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400219 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500220 } else if (type.kind() == Type::kEnum_Kind) {
221 this->write("%d");
222 fFormatArgs.push_back("(int) " + cppCode);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400223 } else if (type == *fContext.fInt4_Type ||
224 type == *fContext.fShort4_Type ||
225 type == *fContext.fByte4_Type) {
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500226 this->write(type.name() + "(%d, %d, %d, %d)");
227 fFormatArgs.push_back(cppCode + ".left()");
228 fFormatArgs.push_back(cppCode + ".top()");
229 fFormatArgs.push_back(cppCode + ".right()");
230 fFormatArgs.push_back(cppCode + ".bottom()");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400231 } else {
Ethan Nicholas82399462017-10-16 12:35:44 -0400232 printf("unsupported runtime value type '%s'\n", String(type.fName).c_str());
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400233 SkASSERT(false);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400234 }
235}
236
237void CPPCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) {
238 if (is_private(var)) {
Ethan Nicholasd608c092017-10-26 09:30:08 -0400239 this->writeRuntimeValue(var.fType, var.fModifiers.fLayout, var.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400240 } else {
241 this->writeExpression(value, kTopLevel_Precedence);
242 }
243}
244
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400245String CPPCodeGenerator::getSamplerHandle(const Variable& var) {
246 int samplerCount = 0;
Ethan Nicholas68990be2017-07-13 09:36:52 -0400247 for (const auto param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400248 if (&var == param) {
249 return "args.fTexSamplers[" + to_string(samplerCount) + "]";
250 }
251 if (param->fType.kind() == Type::kSampler_Kind) {
252 ++samplerCount;
253 }
254 }
255 ABORT("should have found sampler in parameters\n");
256}
257
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400258void CPPCodeGenerator::writeIntLiteral(const IntLiteral& i) {
259 this->write(to_string((int32_t) i.fValue));
260}
261
Ethan Nicholas82399462017-10-16 12:35:44 -0400262void CPPCodeGenerator::writeSwizzle(const Swizzle& swizzle) {
263 if (fCPPMode) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400264 SkASSERT(swizzle.fComponents.size() == 1); // no support for multiple swizzle components yet
Ethan Nicholas82399462017-10-16 12:35:44 -0400265 this->writeExpression(*swizzle.fBase, kPostfix_Precedence);
266 switch (swizzle.fComponents[0]) {
267 case 0: this->write(".left()"); break;
268 case 1: this->write(".top()"); break;
269 case 2: this->write(".right()"); break;
270 case 3: this->write(".bottom()"); break;
271 }
272 } else {
273 INHERITED::writeSwizzle(swizzle);
274 }
275}
276
Ethan Nicholas762466e2017-06-29 10:03:38 -0400277void CPPCodeGenerator::writeVariableReference(const VariableReference& ref) {
Ethan Nicholas82399462017-10-16 12:35:44 -0400278 if (fCPPMode) {
279 this->write(ref.fVariable.fName);
280 return;
281 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400282 switch (ref.fVariable.fModifiers.fLayout.fBuiltin) {
283 case SK_INCOLOR_BUILTIN:
284 this->write("%s");
Michael Ludwig231de032018-08-30 14:33:01 -0400285 // EmitArgs.fInputColor is automatically set to half4(1) if
286 // no input was specified
287 fFormatArgs.push_back(String("args.fInputColor"));
Ethan Nicholas762466e2017-06-29 10:03:38 -0400288 break;
289 case SK_OUTCOLOR_BUILTIN:
290 this->write("%s");
291 fFormatArgs.push_back(String("args.fOutputColor"));
292 break;
Ethan Nicholascd700e92018-08-24 16:43:57 -0400293 case SK_WIDTH_BUILTIN:
294 this->write("sk_Width");
295 break;
296 case SK_HEIGHT_BUILTIN:
297 this->write("sk_Height");
298 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400299 default:
300 if (ref.fVariable.fType.kind() == Type::kSampler_Kind) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400301 this->write("%s");
302 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerVariable(" +
303 this->getSamplerHandle(ref.fVariable) + ").c_str()");
304 return;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400305 }
306 if (ref.fVariable.fModifiers.fFlags & Modifiers::kUniform_Flag) {
307 this->write("%s");
308 String name = ref.fVariable.fName;
Brian Osman1cb41712017-10-19 12:54:52 -0400309 String var = String::printf("args.fUniformHandler->getUniformCStr(%sVar)",
310 HCodeGenerator::FieldName(name.c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400311 String code;
312 if (ref.fVariable.fModifiers.fLayout.fWhen.size()) {
313 code = String::printf("%sVar.isValid() ? %s : \"%s\"",
314 HCodeGenerator::FieldName(name.c_str()).c_str(),
315 var.c_str(),
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400316 default_value(ref.fVariable.fType).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400317 } else {
318 code = var;
319 }
320 fFormatArgs.push_back(code);
321 } else if (SectionAndParameterHelper::IsParameter(ref.fVariable)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700322 String name(ref.fVariable.fName);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400323 this->writeRuntimeValue(ref.fVariable.fType, ref.fVariable.fModifiers.fLayout,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700324 String::printf("_outer.%s()", name.c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400325 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700326 this->write(ref.fVariable.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400327 }
328 }
329}
330
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400331void CPPCodeGenerator::writeIfStatement(const IfStatement& s) {
332 if (s.fIsStatic) {
333 this->write("@");
334 }
335 INHERITED::writeIfStatement(s);
336}
337
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400338void CPPCodeGenerator::writeReturnStatement(const ReturnStatement& s) {
339 if (fInMain) {
340 fErrors.error(s.fOffset, "fragmentProcessor main() may not contain return statements");
341 }
342 INHERITED::writeReturnStatement(s);
343}
344
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400345void CPPCodeGenerator::writeSwitchStatement(const SwitchStatement& s) {
346 if (s.fIsStatic) {
347 this->write("@");
348 }
349 INHERITED::writeSwitchStatement(s);
350}
351
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400352void CPPCodeGenerator::writeFieldAccess(const FieldAccess& access) {
353 if (access.fBase->fType.name() == "fragmentProcessor") {
354 // Special field access on fragment processors are converted into function calls on
355 // GrFragmentProcessor's getters.
356 if (access.fBase->fKind != Expression::kVariableReference_Kind) {
357 fErrors.error(access.fBase->fOffset, "fragmentProcessor must be a reference\n");
358 return;
359 }
360
361 const Type::Field& field = fContext.fFragmentProcessor_Type->fields()[access.fFieldIndex];
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500362 const Variable& var = ((const VariableReference&) *access.fBase).fVariable;
363 String cppAccess = String::printf("_outer.childProcessor(_outer.%s_index()).%s()",
364 String(var.fName).c_str(),
365 String(field.fName).c_str());
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400366
367 if (fCPPMode) {
368 this->write(cppAccess.c_str());
369 } else {
370 writeRuntimeValue(*field.fType, Layout(), cppAccess);
371 }
372 return;
373 }
374 INHERITED::writeFieldAccess(access);
375}
376
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500377int CPPCodeGenerator::getChildFPIndex(const Variable& var) const {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400378 int index = 0;
379 bool found = false;
380 for (const auto& p : fProgram) {
381 if (ProgramElement::kVar_Kind == p.fKind) {
382 const VarDeclarations& decls = (const VarDeclarations&) p;
383 for (const auto& raw : decls.fVars) {
384 const VarDeclaration& decl = (VarDeclaration&) *raw;
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500385 if (decl.fVar == &var) {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400386 found = true;
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500387 } else if (decl.fVar->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400388 ++index;
389 }
390 }
391 }
392 if (found) {
393 break;
394 }
395 }
396 SkASSERT(found);
397 return index;
398}
399
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400400void CPPCodeGenerator::writeFunctionCall(const FunctionCall& c) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400401 if (c.fFunction.fBuiltin && c.fFunction.fName == "process") {
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400402 // Sanity checks that are detected by function definition in sksl_fp.inc
403 SkASSERT(c.fArguments.size() == 1 || c.fArguments.size() == 2);
404 SkASSERT("fragmentProcessor" == c.fArguments[0]->fType.name());
405
406 // Actually fail during compilation if arguments with valid types are
407 // provided that are not variable references, since process() is a
408 // special function that impacts code emission.
409 if (c.fArguments[0]->fKind != Expression::kVariableReference_Kind) {
410 fErrors.error(c.fArguments[0]->fOffset,
411 "process()'s fragmentProcessor argument must be a variable reference\n");
412 return;
413 }
414 if (c.fArguments.size() > 1) {
415 // Second argument must also be a half4 expression
416 SkASSERT("half4" == c.fArguments[1]->fType.name());
417 }
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500418 const Variable& child = ((const VariableReference&) *c.fArguments[0]).fVariable;
419 int index = getChildFPIndex(child);
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400420
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400421 // Start a new extra emit code section so that the emitted child processor can depend on
422 // sksl variables defined in earlier sksl code.
423 this->newExtraEmitCodeBlock();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400424
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400425 // Set to the empty string when no input color parameter should be emitted, which means this
426 // must be properly formatted with a prefixed comma when the parameter should be inserted
427 // into the emitChild() parameter list.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400428 String inputArg;
429 if (c.fArguments.size() > 1) {
430 SkASSERT(c.fArguments.size() == 2);
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400431 // Use the emitChild() variant that accepts an input color, so convert the 2nd
432 // argument's expression into C++ code that produces sksl stored in an SkString.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400433 String inputName = "_input" + to_string(index);
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400434 addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments[1], inputName));
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400435
436 // emitChild() needs a char*
437 inputArg = ", " + inputName + ".c_str()";
438 }
439
440 // Write the output handling after the possible input handling
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400441 String childName = "_child" + to_string(index);
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400442 addExtraEmitCodeLine("SkString " + childName + "(\"" + childName + "\");");
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500443 if (c.fArguments[0]->fType.kind() == Type::kNullable_Kind) {
444 addExtraEmitCodeLine("if (_outer." + String(child.fName) + "_index() >= 0) {\n ");
445 }
446 addExtraEmitCodeLine("this->emitChild(_outer." + String(child.fName) + "_index()" +
447 inputArg + ", &" + childName + ", args);");
448 if (c.fArguments[0]->fType.kind() == Type::kNullable_Kind) {
449 addExtraEmitCodeLine("}");
450 }
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400451 this->write("%s");
452 fFormatArgs.push_back(childName + ".c_str()");
453 return;
454 }
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400455 INHERITED::writeFunctionCall(c);
456 if (c.fFunction.fBuiltin && c.fFunction.fName == "texture") {
457 this->write(".%s");
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400458 SkASSERT(c.fArguments.size() >= 1);
459 SkASSERT(c.fArguments[0]->fKind == Expression::kVariableReference_Kind);
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400460 String sampler = this->getSamplerHandle(((VariableReference&) *c.fArguments[0]).fVariable);
461 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerSwizzle(" + sampler +
462 ").c_str()");
463 }
464}
465
Ethan Nicholas762466e2017-06-29 10:03:38 -0400466void CPPCodeGenerator::writeFunction(const FunctionDefinition& f) {
467 if (f.fDeclaration.fName == "main") {
468 fFunctionHeader = "";
469 OutputStream* oldOut = fOut;
470 StringStream buffer;
471 fOut = &buffer;
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400472 fInMain = true;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400473 for (const auto& s : ((Block&) *f.fBody).fStatements) {
474 this->writeStatement(*s);
475 this->writeLine();
476 }
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400477 fInMain = false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400478
479 fOut = oldOut;
480 this->write(fFunctionHeader);
481 this->write(buffer.str());
482 } else {
483 INHERITED::writeFunction(f);
484 }
485}
486
487void CPPCodeGenerator::writeSetting(const Setting& s) {
488 static constexpr const char* kPrefix = "sk_Args.";
489 if (!strncmp(s.fName.c_str(), kPrefix, strlen(kPrefix))) {
490 const char* name = s.fName.c_str() + strlen(kPrefix);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400491 this->writeRuntimeValue(s.fType, Layout(), HCodeGenerator::FieldName(name).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400492 } else {
493 this->write(s.fName.c_str());
494 }
495}
496
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400497bool CPPCodeGenerator::writeSection(const char* name, const char* prefix) {
Ethan Nicholas68990be2017-07-13 09:36:52 -0400498 const Section* s = fSectionAndParameterHelper.getSection(name);
499 if (s) {
500 this->writef("%s%s", prefix, s->fText.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400501 return true;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400502 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400503 return false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400504}
505
506void CPPCodeGenerator::writeProgramElement(const ProgramElement& p) {
507 if (p.fKind == ProgramElement::kSection_Kind) {
508 return;
509 }
510 if (p.fKind == ProgramElement::kVar_Kind) {
511 const VarDeclarations& decls = (const VarDeclarations&) p;
512 if (!decls.fVars.size()) {
513 return;
514 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000515 const Variable& var = *((VarDeclaration&) *decls.fVars[0]).fVar;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400516 if (var.fModifiers.fFlags & (Modifiers::kIn_Flag | Modifiers::kUniform_Flag) ||
517 -1 != var.fModifiers.fLayout.fBuiltin) {
518 return;
519 }
520 }
521 INHERITED::writeProgramElement(p);
522}
523
524void CPPCodeGenerator::addUniform(const Variable& var) {
525 if (!needs_uniform_var(var)) {
526 return;
527 }
528 const char* precision;
529 if (var.fModifiers.fFlags & Modifiers::kHighp_Flag) {
530 precision = "kHigh_GrSLPrecision";
531 } else if (var.fModifiers.fFlags & Modifiers::kMediump_Flag) {
532 precision = "kMedium_GrSLPrecision";
533 } else if (var.fModifiers.fFlags & Modifiers::kLowp_Flag) {
534 precision = "kLow_GrSLPrecision";
535 } else {
536 precision = "kDefault_GrSLPrecision";
537 }
538 const char* type;
539 if (var.fType == *fContext.fFloat_Type) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400540 type = "kFloat_GrSLType";
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400541 } else if (var.fType == *fContext.fHalf_Type) {
542 type = "kHalf_GrSLType";
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400543 } else if (var.fType == *fContext.fFloat2_Type) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400544 type = "kFloat2_GrSLType";
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400545 } else if (var.fType == *fContext.fHalf2_Type) {
546 type = "kHalf2_GrSLType";
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400547 } else if (var.fType == *fContext.fFloat4_Type) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400548 type = "kFloat4_GrSLType";
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400549 } else if (var.fType == *fContext.fHalf4_Type) {
550 type = "kHalf4_GrSLType";
Brian Osman1cb41712017-10-19 12:54:52 -0400551 } else if (var.fType == *fContext.fFloat4x4_Type) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400552 type = "kFloat4x4_GrSLType";
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400553 } else if (var.fType == *fContext.fHalf4x4_Type) {
554 type = "kHalf4x4_GrSLType";
Ethan Nicholas762466e2017-06-29 10:03:38 -0400555 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700556 ABORT("unsupported uniform type: %s %s;\n", String(var.fType.fName).c_str(),
557 String(var.fName).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400558 }
559 if (var.fModifiers.fLayout.fWhen.size()) {
560 this->writef(" if (%s) {\n ", var.fModifiers.fLayout.fWhen.c_str());
561 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700562 String name(var.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400563 this->writef(" %sVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, %s, "
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700564 "%s, \"%s\");\n", HCodeGenerator::FieldName(name.c_str()).c_str(), type, precision,
565 name.c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400566 if (var.fModifiers.fLayout.fWhen.size()) {
567 this->write(" }\n");
568 }
569}
570
Ethan Nicholascd700e92018-08-24 16:43:57 -0400571void CPPCodeGenerator::writeInputVars() {
572}
573
Ethan Nicholas762466e2017-06-29 10:03:38 -0400574void CPPCodeGenerator::writePrivateVars() {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400575 for (const auto& p : fProgram) {
576 if (ProgramElement::kVar_Kind == p.fKind) {
577 const VarDeclarations& decls = (const VarDeclarations&) p;
578 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000579 VarDeclaration& decl = (VarDeclaration&) *raw;
580 if (is_private(*decl.fVar)) {
581 if (decl.fVar->fType == *fContext.fFragmentProcessor_Type) {
582 fErrors.error(decl.fOffset,
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400583 "fragmentProcessor variables must be declared 'in'");
584 return;
585 }
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500586 this->writef("%s %s = %s;\n",
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000587 HCodeGenerator::FieldType(fContext, decl.fVar->fType,
588 decl.fVar->fModifiers.fLayout).c_str(),
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500589 String(decl.fVar->fName).c_str(),
590 default_value(*decl.fVar).c_str());
Michael Ludwiga4275592018-08-31 10:52:47 -0400591 } else if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
592 // An auto-tracked uniform in variable, so add a field to hold onto the prior
593 // state. Note that tracked variables must be uniform in's and that is validated
594 // before writePrivateVars() is called.
595 const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *decl.fVar);
596 SkASSERT(mapper && mapper->supportsTracking());
597
598 String name = HCodeGenerator::FieldName(String(decl.fVar->fName).c_str());
599 // The member statement is different if the mapper reports a default value
600 if (mapper->defaultValue().size() > 0) {
601 this->writef("%s %sPrev = %s;\n",
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400602 Layout::CTypeToStr(mapper->ctype()), name.c_str(),
Michael Ludwiga4275592018-08-31 10:52:47 -0400603 mapper->defaultValue().c_str());
604 } else {
605 this->writef("%s %sPrev;\n",
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400606 Layout::CTypeToStr(mapper->ctype()), name.c_str());
Michael Ludwiga4275592018-08-31 10:52:47 -0400607 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400608 }
609 }
610 }
611 }
612}
613
614void CPPCodeGenerator::writePrivateVarValues() {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400615 for (const auto& p : fProgram) {
616 if (ProgramElement::kVar_Kind == p.fKind) {
617 const VarDeclarations& decls = (const VarDeclarations&) p;
618 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000619 VarDeclaration& decl = (VarDeclaration&) *raw;
620 if (is_private(*decl.fVar) && decl.fValue) {
621 this->writef("%s = ", String(decl.fVar->fName).c_str());
Ethan Nicholas82399462017-10-16 12:35:44 -0400622 fCPPMode = true;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000623 this->writeExpression(*decl.fValue, kAssignment_Precedence);
Ethan Nicholas82399462017-10-16 12:35:44 -0400624 fCPPMode = false;
625 this->write(";\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400626 }
627 }
628 }
629 }
630}
631
Ethan Nicholas82399462017-10-16 12:35:44 -0400632static bool is_accessible(const Variable& var) {
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500633 const Type& type = var.fType.nonnullable();
634 return Type::kSampler_Kind != type.kind() &&
635 Type::kOther_Kind != type.kind();
Ethan Nicholas82399462017-10-16 12:35:44 -0400636}
637
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400638void CPPCodeGenerator::newExtraEmitCodeBlock() {
639 // This should only be called when emitting SKSL for emitCode(), which can be detected if the
640 // cpp buffer is not null, and the cpp buffer is not the current output.
641 SkASSERT(fCPPBuffer && fCPPBuffer != fOut);
642
643 // Start a new block as an empty string
644 fExtraEmitCodeBlocks.push_back("");
645 // Mark its location in the output buffer, uses ${\d} for the token since ${} will not occur in
646 // valid sksl and makes detection trivial.
647 this->writef("${%zu}", fExtraEmitCodeBlocks.size() - 1);
648}
649
650void CPPCodeGenerator::addExtraEmitCodeLine(const String& toAppend) {
651 SkASSERT(fExtraEmitCodeBlocks.size() > 0);
652 String& currentBlock = fExtraEmitCodeBlocks[fExtraEmitCodeBlocks.size() - 1];
653 // Automatically add indentation and newline
654 currentBlock += " " + toAppend + "\n";
655}
656
657void CPPCodeGenerator::flushEmittedCode() {
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400658 if (fCPPBuffer == nullptr) {
659 // Not actually within writeEmitCode() so nothing to flush
660 return;
661 }
662
663 StringStream* skslBuffer = static_cast<StringStream*>(fOut);
664
665 String sksl = skslBuffer->str();
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400666 // Empty the accumulation buffer since its current contents are consumed.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400667 skslBuffer->reset();
668
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400669 // Switch to the cpp buffer
Michael Ludwigd0440192018-09-07 14:24:52 +0000670 fOut = fCPPBuffer;
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400671
672 // Iterate through the sksl, keeping track of where the last statement ended (e.g. the latest
673 // encountered ';', '{', or '}'). If an extra emit code block token is encountered then the
674 // code from 0 to last statement end is sent to writeCodeAppend, the extra code block is
675 // appended to the cpp buffer, and then the sksl string is trimmed to start where the last
676 // statement left off (minus the encountered token).
677 size_t i = 0;
678 int flushPoint = -1;
679 int tokenStart = -1;
680 while (i < sksl.size()) {
681 if (tokenStart >= 0) {
682 // Looking for the end of the token
683 if (sksl[i] == '}') {
684 // Must append the sksl from 0 to flushPoint (inclusive) then the extra code
685 // accumulated in the block with index parsed from chars [tokenStart+2, i-1]
686 String toFlush = String(sksl.c_str(), flushPoint + 1);
687 // writeCodeAppend automatically removes the format args that it consumed, so
688 // fFormatArgs will be in a valid state for any future sksl
689 this->writeCodeAppend(toFlush);
690
691 int codeBlock = stoi(String(sksl.c_str() + tokenStart + 2, i - tokenStart - 2));
692 SkASSERT(codeBlock < (int) fExtraEmitCodeBlocks.size());
693 if (fExtraEmitCodeBlocks[codeBlock].size() > 0) {
694 this->write(fExtraEmitCodeBlocks[codeBlock].c_str());
695 }
696
697 // Now reset the sksl buffer to start after the flush point, but remove the token.
698 String compacted = String(sksl.c_str() + flushPoint + 1,
699 tokenStart - flushPoint - 1);
700 if (i < sksl.size() - 1) {
701 compacted += String(sksl.c_str() + i + 1, sksl.size() - i - 1);
702 }
703 sksl = compacted;
704
705 // And reset iteration
706 i = -1;
707 flushPoint = -1;
708 tokenStart = -1;
709 }
710 } else {
711 // Looking for the start of extra emit block tokens, and tracking when statements end
712 if (sksl[i] == ';' || sksl[i] == '{' || sksl[i] == '}') {
713 flushPoint = i;
714 } else if (i < sksl.size() - 1 && sksl[i] == '$' && sksl[i + 1] == '{') {
715 // found an extra emit code block token
716 tokenStart = i++;
717 }
718 }
719 i++;
Michael Ludwigd0440192018-09-07 14:24:52 +0000720 }
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400721
722 // Once we've gone through the sksl string to this point, there are no remaining extra emit
723 // code blocks to interleave, so append the remainder as usual.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400724 this->writeCodeAppend(sksl);
725
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400726 // After appending, switch back to the emptied sksl buffer and reset the extra code blocks
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400727 fOut = skslBuffer;
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400728 fExtraEmitCodeBlocks.clear();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400729}
730
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400731void CPPCodeGenerator::writeCodeAppend(const String& code) {
732 // codeAppendf can only handle appending 1024 bytes at a time, so we need to break the string
733 // into chunks. Unfortunately we can't tell exactly how long the string is going to end up,
734 // because printf escape sequences get replaced by strings of unknown length, but keeping the
735 // format string below 512 bytes is probably safe.
736 static constexpr size_t maxChunkSize = 512;
737 size_t start = 0;
738 size_t index = 0;
739 size_t argStart = 0;
740 size_t argCount;
741 while (index < code.size()) {
742 argCount = 0;
743 this->write(" fragBuilder->codeAppendf(\"");
744 while (index < code.size() && index < start + maxChunkSize) {
745 if ('%' == code[index]) {
746 if (index == start + maxChunkSize - 1 || index == code.size() - 1) {
747 break;
748 }
749 if (code[index + 1] != '%') {
750 ++argCount;
751 }
Ethan Nicholasef0c9fd2017-10-30 10:04:14 -0400752 } else if ('\\' == code[index] && index == start + maxChunkSize - 1) {
753 // avoid splitting an escape sequence that happens to fall across a chunk boundary
754 break;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400755 }
756 ++index;
757 }
758 fOut->write(code.c_str() + start, index - start);
759 this->write("\"");
760 for (size_t i = argStart; i < argStart + argCount; ++i) {
761 this->writef(", %s", fFormatArgs[i].c_str());
762 }
763 this->write(");\n");
764 argStart += argCount;
765 start = index;
766 }
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400767
768 // argStart is equal to the number of fFormatArgs that were consumed
769 // so they should be removed from the list
770 if (argStart > 0) {
771 fFormatArgs.erase(fFormatArgs.begin(), fFormatArgs.begin() + argStart);
772 }
773}
774
775String CPPCodeGenerator::convertSKSLExpressionToCPP(const Expression& e,
776 const String& cppVar) {
777 // To do this conversion, we temporarily switch the sksl output stream
778 // to an empty stringstream and reset the format args to empty.
779 OutputStream* oldSKSL = fOut;
780 StringStream exprBuffer;
781 fOut = &exprBuffer;
782
783 std::vector<String> oldArgs(fFormatArgs);
784 fFormatArgs.clear();
785
786 // Convert the argument expression into a format string and args
787 this->writeExpression(e, Precedence::kTopLevel_Precedence);
788 std::vector<String> newArgs(fFormatArgs);
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400789 String expr = exprBuffer.str();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400790
791 // After generating, restore the original output stream and format args
792 fFormatArgs = oldArgs;
793 fOut = oldSKSL;
794
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400795 // The sksl written to exprBuffer is not processed by flushEmittedCode(), so any extra emit code
796 // block tokens won't get handled. So we need to strip them from the expression and stick them
797 // to the end of the original sksl stream.
798 String exprFormat = "";
799 int tokenStart = -1;
800 for (size_t i = 0; i < expr.size(); i++) {
801 if (tokenStart >= 0) {
802 if (expr[i] == '}') {
803 // End of the token, so append the token to fOut
804 fOut->write(expr.c_str() + tokenStart, i - tokenStart + 1);
805 tokenStart = -1;
806 }
807 } else {
808 if (i < expr.size() - 1 && expr[i] == '$' && expr[i + 1] == '{') {
809 tokenStart = i++;
810 } else {
811 exprFormat += expr[i];
812 }
813 }
814 }
815
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400816 // Now build the final C++ code snippet from the format string and args
817 String cppExpr;
818 if (newArgs.size() == 0) {
819 // This was a static expression, so we can simplify the input
820 // color declaration in the emitted code to just a static string
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400821 cppExpr = "SkString " + cppVar + "(\"" + exprFormat + "\");";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400822 } else {
823 // String formatting must occur dynamically, so have the C++ declaration
824 // use SkStringPrintf with the format args that were accumulated
825 // when the expression was written.
826 cppExpr = "SkString " + cppVar + " = SkStringPrintf(\"" + exprFormat + "\"";
827 for (size_t i = 0; i < newArgs.size(); i++) {
828 cppExpr += ", " + newArgs[i];
829 }
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400830 cppExpr += ");";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400831 }
832 return cppExpr;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400833}
834
Ethan Nicholas762466e2017-06-29 10:03:38 -0400835bool CPPCodeGenerator::writeEmitCode(std::vector<const Variable*>& uniforms) {
836 this->write(" void emitCode(EmitArgs& args) override {\n"
837 " GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;\n");
838 this->writef(" const %s& _outer = args.fFp.cast<%s>();\n"
839 " (void) _outer;\n",
840 fFullName.c_str(), fFullName.c_str());
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400841 for (const auto& p : fProgram) {
842 if (ProgramElement::kVar_Kind == p.fKind) {
843 const VarDeclarations& decls = (const VarDeclarations&) p;
844 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000845 VarDeclaration& decl = (VarDeclaration&) *raw;
846 String nameString(decl.fVar->fName);
Ethan Nicholas82399462017-10-16 12:35:44 -0400847 const char* name = nameString.c_str();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000848 if (SectionAndParameterHelper::IsParameter(*decl.fVar) &&
849 is_accessible(*decl.fVar)) {
Ethan Nicholas82399462017-10-16 12:35:44 -0400850 this->writef(" auto %s = _outer.%s();\n"
851 " (void) %s;\n",
852 name, name, name);
853 }
854 }
855 }
856 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400857 this->writePrivateVarValues();
858 for (const auto u : uniforms) {
859 this->addUniform(*u);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400860 }
861 this->writeSection(EMIT_CODE_SECTION);
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400862
863 // Save original buffer as the CPP buffer for flushEmittedCode()
864 fCPPBuffer = fOut;
865 StringStream skslBuffer;
866 fOut = &skslBuffer;
867
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400868 this->newExtraEmitCodeBlock();
Ethan Nicholas762466e2017-06-29 10:03:38 -0400869 bool result = INHERITED::generateCode();
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400870 this->flushEmittedCode();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400871
872 // Then restore the original CPP buffer and close the function
873 fOut = fCPPBuffer;
874 fCPPBuffer = nullptr;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400875 this->write(" }\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400876 return result;
877}
878
879void CPPCodeGenerator::writeSetData(std::vector<const Variable*>& uniforms) {
880 const char* fullName = fFullName.c_str();
Ethan Nicholas68990be2017-07-13 09:36:52 -0400881 const Section* section = fSectionAndParameterHelper.getSection(SET_DATA_SECTION);
882 const char* pdman = section ? section->fArgument.c_str() : "pdman";
Ethan Nicholas762466e2017-06-29 10:03:38 -0400883 this->writef(" void onSetData(const GrGLSLProgramDataManager& %s, "
884 "const GrFragmentProcessor& _proc) override {\n",
885 pdman);
886 bool wroteProcessor = false;
887 for (const auto u : uniforms) {
Michael Ludwiga4275592018-08-31 10:52:47 -0400888 if (is_uniform_in(*u)) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400889 if (!wroteProcessor) {
890 this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName, fullName);
891 wroteProcessor = true;
892 this->writef(" {\n");
893 }
Michael Ludwiga4275592018-08-31 10:52:47 -0400894
895 const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *u);
896 SkASSERT(mapper);
897
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700898 String nameString(u->fName);
899 const char* name = nameString.c_str();
Michael Ludwiga4275592018-08-31 10:52:47 -0400900
901 // Switches for setData behavior in the generated code
902 bool conditionalUniform = u->fModifiers.fLayout.fWhen != "";
903 bool isTracked = u->fModifiers.fLayout.fFlags & Layout::kTracked_Flag;
904 bool needsValueDeclaration = isTracked || !mapper->canInlineUniformValue();
905
906 String uniformName = HCodeGenerator::FieldName(name) + "Var";
907
908 String indent = " "; // 8 by default, 12 when nested for conditional uniforms
909 if (conditionalUniform) {
910 // Add a pre-check to make sure the uniform was emitted
911 // before trying to send any data to the GPU
912 this->writef(" if (%s.isValid()) {\n", uniformName.c_str());
913 indent += " ";
914 }
915
916 String valueVar = "";
917 if (needsValueDeclaration) {
918 valueVar.appendf("%sValue", name);
919 // Use AccessType since that will match the return type of _outer's public API.
920 String valueType = HCodeGenerator::AccessType(fContext, u->fType,
921 u->fModifiers.fLayout);
922 this->writef("%s%s %s = _outer.%s();\n",
923 indent.c_str(), valueType.c_str(), valueVar.c_str(), name);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400924 } else {
Michael Ludwiga4275592018-08-31 10:52:47 -0400925 // Not tracked and the mapper only needs to use the value once
926 // so send it a safe expression instead of the variable name
927 valueVar.appendf("(_outer.%s())", name);
928 }
929
930 if (isTracked) {
931 SkASSERT(mapper->supportsTracking());
932
933 String prevVar = HCodeGenerator::FieldName(name) + "Prev";
934 this->writef("%sif (%s) {\n"
935 "%s %s;\n"
936 "%s %s;\n"
937 "%s}\n", indent.c_str(),
938 mapper->dirtyExpression(valueVar, prevVar).c_str(), indent.c_str(),
939 mapper->saveState(valueVar, prevVar).c_str(), indent.c_str(),
940 mapper->setUniform(pdman, uniformName, valueVar).c_str(), indent.c_str());
941 } else {
942 this->writef("%s%s;\n", indent.c_str(),
943 mapper->setUniform(pdman, uniformName, valueVar).c_str());
944 }
945
946 if (conditionalUniform) {
947 // Close the earlier precheck block
948 this->writef(" }\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400949 }
950 }
951 }
952 if (wroteProcessor) {
953 this->writef(" }\n");
954 }
Ethan Nicholas68990be2017-07-13 09:36:52 -0400955 if (section) {
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500956 int samplerIndex = 0;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400957 for (const auto& p : fProgram) {
958 if (ProgramElement::kVar_Kind == p.fKind) {
959 const VarDeclarations& decls = (const VarDeclarations&) p;
960 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000961 VarDeclaration& decl = (VarDeclaration&) *raw;
962 String nameString(decl.fVar->fName);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700963 const char* name = nameString.c_str();
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500964 if (decl.fVar->fType.kind() == Type::kSampler_Kind) {
965 this->writef(" GrSurfaceProxy& %sProxy = "
966 "*_outer.textureSampler(%d).proxy();\n",
967 name, samplerIndex);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400968 this->writef(" GrTexture& %s = *%sProxy.peekTexture();\n",
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500969 name, name);
970 this->writef(" (void) %s;\n", name);
971 ++samplerIndex;
972 } else if (needs_uniform_var(*decl.fVar)) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400973 this->writef(" UniformHandle& %s = %sVar;\n"
974 " (void) %s;\n",
975 name, HCodeGenerator::FieldName(name).c_str(), name);
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000976 } else if (SectionAndParameterHelper::IsParameter(*decl.fVar) &&
977 decl.fVar->fType != *fContext.fFragmentProcessor_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400978 if (!wroteProcessor) {
979 this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName,
980 fullName);
981 wroteProcessor = true;
982 }
983 this->writef(" auto %s = _outer.%s();\n"
984 " (void) %s;\n",
985 name, name, name);
986 }
987 }
988 }
989 }
990 this->writeSection(SET_DATA_SECTION);
991 }
992 this->write(" }\n");
993}
994
Brian Salomonf7dcd762018-07-30 14:48:15 -0400995void CPPCodeGenerator::writeOnTextureSampler() {
996 bool foundSampler = false;
997 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
998 if (param->fType.kind() == Type::kSampler_Kind) {
999 if (!foundSampler) {
1000 this->writef(
1001 "const GrFragmentProcessor::TextureSampler& %s::onTextureSampler(int "
1002 "index) const {\n",
1003 fFullName.c_str());
1004 this->writef(" return IthTextureSampler(index, %s",
1005 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
1006 foundSampler = true;
1007 } else {
1008 this->writef(", %s",
1009 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
1010 }
1011 }
1012 }
1013 if (foundSampler) {
1014 this->write(");\n}\n");
1015 }
1016}
1017
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001018void CPPCodeGenerator::writeClone() {
1019 if (!this->writeSection(CLONE_SECTION)) {
1020 if (fSectionAndParameterHelper.getSection(FIELDS_SECTION)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001021 fErrors.error(0, "fragment processors with custom @fields must also have a custom"
1022 "@clone");
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001023 }
1024 this->writef("%s::%s(const %s& src)\n"
Ethan Nicholasabff9562017-10-09 10:54:08 -04001025 ": INHERITED(k%s_ClassID, src.optimizationFlags())", fFullName.c_str(),
1026 fFullName.c_str(), fFullName.c_str(), fFullName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001027 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Robert Phillipsbce7d862019-02-21 22:53:57 +00001028 String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str());
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001029 if (param->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
1030 this->writef("\n, %s_index(src.%s_index)",
1031 fieldName.c_str(),
1032 fieldName.c_str());
1033 } else {
1034 this->writef("\n, %s(src.%s)",
1035 fieldName.c_str(),
1036 fieldName.c_str());
1037 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001038 }
Ethan Nicholas929a6812018-08-06 14:56:59 -04001039 const auto transforms = fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION);
1040 for (size_t i = 0; i < transforms.size(); ++i) {
1041 const Section& s = *transforms[i];
1042 String fieldName = HCodeGenerator::CoordTransformName(s.fArgument, i);
1043 this->writef("\n, %s(src.%s)", fieldName.c_str(), fieldName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001044 }
Ethan Nicholasabff9562017-10-09 10:54:08 -04001045 this->writef(" {\n");
Brian Salomonf7dcd762018-07-30 14:48:15 -04001046 int samplerCount = 0;
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001047 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
1048 if (param->fType.kind() == Type::kSampler_Kind) {
Brian Salomonf7dcd762018-07-30 14:48:15 -04001049 ++samplerCount;
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001050 } else if (param->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
1051 String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str());
1052 if (param->fType.kind() == Type::kNullable_Kind) {
1053 this->writef(" if (%s_index >= 0) {\n ", fieldName.c_str());
1054 }
1055 this->writef(" this->registerChildProcessor(src.childProcessor(%s_index)."
1056 "clone());\n", fieldName.c_str());
1057 if (param->fType.kind() == Type::kNullable_Kind) {
1058 this->writef(" }\n");
1059 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001060 }
1061 }
Brian Salomonf7dcd762018-07-30 14:48:15 -04001062 if (samplerCount) {
1063 this->writef(" this->setTextureSamplerCnt(%d);", samplerCount);
1064 }
Ethan Nicholas929a6812018-08-06 14:56:59 -04001065 for (size_t i = 0; i < transforms.size(); ++i) {
1066 const Section& s = *transforms[i];
1067 String fieldName = HCodeGenerator::CoordTransformName(s.fArgument, i);
1068 this->writef(" this->addCoordTransform(&%s);\n", fieldName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001069 }
1070 this->write("}\n");
Brian Salomonaff329b2017-08-11 09:40:37 -04001071 this->writef("std::unique_ptr<GrFragmentProcessor> %s::clone() const {\n",
1072 fFullName.c_str());
1073 this->writef(" return std::unique_ptr<GrFragmentProcessor>(new %s(*this));\n",
1074 fFullName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001075 this->write("}\n");
1076 }
1077}
1078
Ethan Nicholas762466e2017-06-29 10:03:38 -04001079void CPPCodeGenerator::writeTest() {
Ethan Nicholas68990be2017-07-13 09:36:52 -04001080 const Section* test = fSectionAndParameterHelper.getSection(TEST_CODE_SECTION);
1081 if (test) {
Brian Salomonaff329b2017-08-11 09:40:37 -04001082 this->writef(
1083 "GR_DEFINE_FRAGMENT_PROCESSOR_TEST(%s);\n"
1084 "#if GR_TEST_UTILS\n"
1085 "std::unique_ptr<GrFragmentProcessor> %s::TestCreate(GrProcessorTestData* %s) {\n",
1086 fFullName.c_str(),
1087 fFullName.c_str(),
1088 test->fArgument.c_str());
Ethan Nicholas68990be2017-07-13 09:36:52 -04001089 this->writeSection(TEST_CODE_SECTION);
1090 this->write("}\n"
1091 "#endif\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -04001092 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001093}
1094
1095void CPPCodeGenerator::writeGetKey() {
1096 this->writef("void %s::onGetGLSLProcessorKey(const GrShaderCaps& caps, "
1097 "GrProcessorKeyBuilder* b) const {\n",
1098 fFullName.c_str());
Ethan Nicholas68990be2017-07-13 09:36:52 -04001099 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001100 String nameString(param->fName);
1101 const char* name = nameString.c_str();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001102 if (param->fModifiers.fLayout.fKey != Layout::kNo_Key &&
1103 (param->fModifiers.fFlags & Modifiers::kUniform_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001104 fErrors.error(param->fOffset,
Ethan Nicholas762466e2017-06-29 10:03:38 -04001105 "layout(key) may not be specified on uniforms");
1106 }
1107 switch (param->fModifiers.fLayout.fKey) {
1108 case Layout::kKey_Key:
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001109 if (param->fType == *fContext.fFloat4x4_Type) {
1110 ABORT("no automatic key handling for float4x4\n");
1111 } else if (param->fType == *fContext.fFloat2_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001112 this->writef(" b->add32(%s.fX);\n",
1113 HCodeGenerator::FieldName(name).c_str());
1114 this->writef(" b->add32(%s.fY);\n",
1115 HCodeGenerator::FieldName(name).c_str());
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001116 } else if (param->fType == *fContext.fFloat4_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001117 this->writef(" b->add32(%s.x());\n",
1118 HCodeGenerator::FieldName(name).c_str());
1119 this->writef(" b->add32(%s.y());\n",
1120 HCodeGenerator::FieldName(name).c_str());
1121 this->writef(" b->add32(%s.width());\n",
1122 HCodeGenerator::FieldName(name).c_str());
1123 this->writef(" b->add32(%s.height());\n",
1124 HCodeGenerator::FieldName(name).c_str());
1125 } else {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001126 this->writef(" b->add32((int32_t) %s);\n",
Ethan Nicholas762466e2017-06-29 10:03:38 -04001127 HCodeGenerator::FieldName(name).c_str());
1128 }
1129 break;
1130 case Layout::kIdentity_Key:
1131 if (param->fType.kind() != Type::kMatrix_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001132 fErrors.error(param->fOffset,
Ethan Nicholas762466e2017-06-29 10:03:38 -04001133 "layout(key=identity) requires matrix type");
1134 }
1135 this->writef(" b->add32(%s.isIdentity() ? 1 : 0);\n",
1136 HCodeGenerator::FieldName(name).c_str());
1137 break;
1138 case Layout::kNo_Key:
1139 break;
1140 }
1141 }
1142 this->write("}\n");
1143}
1144
1145bool CPPCodeGenerator::generateCode() {
1146 std::vector<const Variable*> uniforms;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001147 for (const auto& p : fProgram) {
1148 if (ProgramElement::kVar_Kind == p.fKind) {
1149 const VarDeclarations& decls = (const VarDeclarations&) p;
1150 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001151 VarDeclaration& decl = (VarDeclaration&) *raw;
1152 if ((decl.fVar->fModifiers.fFlags & Modifiers::kUniform_Flag) &&
1153 decl.fVar->fType.kind() != Type::kSampler_Kind) {
1154 uniforms.push_back(decl.fVar);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001155 }
Michael Ludwiga4275592018-08-31 10:52:47 -04001156
1157 if (is_uniform_in(*decl.fVar)) {
1158 // Validate the "uniform in" declarations to make sure they are fully supported,
1159 // instead of generating surprising C++
1160 const UniformCTypeMapper* mapper =
1161 UniformCTypeMapper::Get(fContext, *decl.fVar);
1162 if (mapper == nullptr) {
1163 fErrors.error(decl.fOffset, String(decl.fVar->fName)
1164 + "'s type is not supported for use as a 'uniform in'");
1165 return false;
1166 }
1167 if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
1168 if (!mapper->supportsTracking()) {
1169 fErrors.error(decl.fOffset, String(decl.fVar->fName)
1170 + "'s type does not support state tracking");
1171 return false;
1172 }
1173 }
1174
1175 } else {
1176 // If it's not a uniform_in, it's an error to be tracked
1177 if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
1178 fErrors.error(decl.fOffset, "Non-'in uniforms' cannot be tracked");
1179 return false;
1180 }
1181 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001182 }
1183 }
1184 }
1185 const char* baseName = fName.c_str();
1186 const char* fullName = fFullName.c_str();
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05001187 this->writef("%s\n", HCodeGenerator::GetHeader(fProgram, fErrors).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001188 this->writef(kFragmentProcessorHeader, fullName);
Greg Daniel3e8c3452018-04-06 10:37:55 -04001189 this->writef("#include \"%s.h\"\n", fullName);
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04001190 this->writeSection(CPP_SECTION);
Brian Osman1cb41712017-10-19 12:54:52 -04001191 this->writef("#include \"glsl/GrGLSLFragmentProcessor.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -04001192 "#include \"glsl/GrGLSLFragmentShaderBuilder.h\"\n"
1193 "#include \"glsl/GrGLSLProgramBuilder.h\"\n"
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05001194 "#include \"GrTexture.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -04001195 "#include \"SkSLCPP.h\"\n"
1196 "#include \"SkSLUtil.h\"\n"
1197 "class GrGLSL%s : public GrGLSLFragmentProcessor {\n"
1198 "public:\n"
1199 " GrGLSL%s() {}\n",
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04001200 baseName, baseName);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001201 bool result = this->writeEmitCode(uniforms);
1202 this->write("private:\n");
1203 this->writeSetData(uniforms);
1204 this->writePrivateVars();
1205 for (const auto& u : uniforms) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001206 if (needs_uniform_var(*u) && !(u->fModifiers.fFlags & Modifiers::kIn_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001207 this->writef(" UniformHandle %sVar;\n",
1208 HCodeGenerator::FieldName(String(u->fName).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001209 }
1210 }
Ethan Nicholas68990be2017-07-13 09:36:52 -04001211 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001212 if (needs_uniform_var(*param)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001213 this->writef(" UniformHandle %sVar;\n",
1214 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001215 }
1216 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001217 this->writef("};\n"
1218 "GrGLSLFragmentProcessor* %s::onCreateGLSLInstance() const {\n"
1219 " return new GrGLSL%s();\n"
1220 "}\n",
1221 fullName, baseName);
1222 this->writeGetKey();
1223 this->writef("bool %s::onIsEqual(const GrFragmentProcessor& other) const {\n"
1224 " const %s& that = other.cast<%s>();\n"
1225 " (void) that;\n",
1226 fullName, fullName, fullName);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001227 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001228 if (param->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -04001229 continue;
1230 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001231 String nameString(param->fName);
1232 const char* name = nameString.c_str();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001233 this->writef(" if (%s != that.%s) return false;\n",
1234 HCodeGenerator::FieldName(name).c_str(),
1235 HCodeGenerator::FieldName(name).c_str());
1236 }
1237 this->write(" return true;\n"
1238 "}\n");
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001239 this->writeClone();
Brian Salomonf7dcd762018-07-30 14:48:15 -04001240 this->writeOnTextureSampler();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001241 this->writeTest();
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04001242 this->writeSection(CPP_END_SECTION);
Greg Daniel3e8c3452018-04-06 10:37:55 -04001243
Ethan Nicholas762466e2017-06-29 10:03:38 -04001244 result &= 0 == fErrors.errorCount();
1245 return result;
1246}
1247
1248} // namespace