blob: e07769d927c84f653c82fb08837554fd16eac160 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/sksl/SkSLCPPUniformCTypes.h"
11#include "src/sksl/SkSLCompiler.h"
12#include "src/sksl/SkSLHCodeGenerator.h"
Ethan Nicholas762466e2017-06-29 10:03:38 -040013
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 }
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400104 fFormatArgs.push_back("_outer." + String(var->fName) + "_index " + op + " 0 ? \"true\" "
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500105 ": \"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;
Brian Salomoneca66b32019-06-01 11:18:15 -0400210 case Layout::CType::kSkVector4:
211 fFormatArgs.push_back(cppCode + ".fData[0]");
212 fFormatArgs.push_back(cppCode + ".fData[1]");
213 fFormatArgs.push_back(cppCode + ".fData[2]");
214 fFormatArgs.push_back(cppCode + ".fData[3]");
215 break;
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400216 case Layout::CType::kSkRect: // fall through
217 case Layout::CType::kDefault:
218 fFormatArgs.push_back(cppCode + ".left()");
219 fFormatArgs.push_back(cppCode + ".top()");
220 fFormatArgs.push_back(cppCode + ".right()");
221 fFormatArgs.push_back(cppCode + ".bottom()");
222 break;
223 default:
224 SkASSERT(false);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400225 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500226 } else if (type.kind() == Type::kEnum_Kind) {
227 this->write("%d");
228 fFormatArgs.push_back("(int) " + cppCode);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400229 } else if (type == *fContext.fInt4_Type ||
230 type == *fContext.fShort4_Type ||
231 type == *fContext.fByte4_Type) {
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500232 this->write(type.name() + "(%d, %d, %d, %d)");
233 fFormatArgs.push_back(cppCode + ".left()");
234 fFormatArgs.push_back(cppCode + ".top()");
235 fFormatArgs.push_back(cppCode + ".right()");
236 fFormatArgs.push_back(cppCode + ".bottom()");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400237 } else {
Ethan Nicholas82399462017-10-16 12:35:44 -0400238 printf("unsupported runtime value type '%s'\n", String(type.fName).c_str());
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400239 SkASSERT(false);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400240 }
241}
242
243void CPPCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) {
244 if (is_private(var)) {
Ethan Nicholasd608c092017-10-26 09:30:08 -0400245 this->writeRuntimeValue(var.fType, var.fModifiers.fLayout, var.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400246 } else {
247 this->writeExpression(value, kTopLevel_Precedence);
248 }
249}
250
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400251String CPPCodeGenerator::getSamplerHandle(const Variable& var) {
252 int samplerCount = 0;
Ethan Nicholas68990be2017-07-13 09:36:52 -0400253 for (const auto param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400254 if (&var == param) {
255 return "args.fTexSamplers[" + to_string(samplerCount) + "]";
256 }
257 if (param->fType.kind() == Type::kSampler_Kind) {
258 ++samplerCount;
259 }
260 }
261 ABORT("should have found sampler in parameters\n");
262}
263
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400264void CPPCodeGenerator::writeIntLiteral(const IntLiteral& i) {
265 this->write(to_string((int32_t) i.fValue));
266}
267
Ethan Nicholas82399462017-10-16 12:35:44 -0400268void CPPCodeGenerator::writeSwizzle(const Swizzle& swizzle) {
269 if (fCPPMode) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400270 SkASSERT(swizzle.fComponents.size() == 1); // no support for multiple swizzle components yet
Ethan Nicholas82399462017-10-16 12:35:44 -0400271 this->writeExpression(*swizzle.fBase, kPostfix_Precedence);
272 switch (swizzle.fComponents[0]) {
273 case 0: this->write(".left()"); break;
274 case 1: this->write(".top()"); break;
275 case 2: this->write(".right()"); break;
276 case 3: this->write(".bottom()"); break;
277 }
278 } else {
279 INHERITED::writeSwizzle(swizzle);
280 }
281}
282
Ethan Nicholas762466e2017-06-29 10:03:38 -0400283void CPPCodeGenerator::writeVariableReference(const VariableReference& ref) {
Ethan Nicholas82399462017-10-16 12:35:44 -0400284 if (fCPPMode) {
285 this->write(ref.fVariable.fName);
286 return;
287 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400288 switch (ref.fVariable.fModifiers.fLayout.fBuiltin) {
289 case SK_INCOLOR_BUILTIN:
290 this->write("%s");
Michael Ludwig231de032018-08-30 14:33:01 -0400291 // EmitArgs.fInputColor is automatically set to half4(1) if
292 // no input was specified
293 fFormatArgs.push_back(String("args.fInputColor"));
Ethan Nicholas762466e2017-06-29 10:03:38 -0400294 break;
295 case SK_OUTCOLOR_BUILTIN:
296 this->write("%s");
297 fFormatArgs.push_back(String("args.fOutputColor"));
298 break;
Ethan Nicholascd700e92018-08-24 16:43:57 -0400299 case SK_WIDTH_BUILTIN:
300 this->write("sk_Width");
301 break;
302 case SK_HEIGHT_BUILTIN:
303 this->write("sk_Height");
304 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400305 default:
306 if (ref.fVariable.fType.kind() == Type::kSampler_Kind) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400307 this->write("%s");
308 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerVariable(" +
309 this->getSamplerHandle(ref.fVariable) + ").c_str()");
310 return;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400311 }
312 if (ref.fVariable.fModifiers.fFlags & Modifiers::kUniform_Flag) {
313 this->write("%s");
314 String name = ref.fVariable.fName;
Brian Osman1cb41712017-10-19 12:54:52 -0400315 String var = String::printf("args.fUniformHandler->getUniformCStr(%sVar)",
316 HCodeGenerator::FieldName(name.c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400317 String code;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400318 if (ref.fVariable.fModifiers.fLayout.fWhen.fLength) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400319 code = String::printf("%sVar.isValid() ? %s : \"%s\"",
320 HCodeGenerator::FieldName(name.c_str()).c_str(),
321 var.c_str(),
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400322 default_value(ref.fVariable.fType).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400323 } else {
324 code = var;
325 }
326 fFormatArgs.push_back(code);
327 } else if (SectionAndParameterHelper::IsParameter(ref.fVariable)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700328 String name(ref.fVariable.fName);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400329 this->writeRuntimeValue(ref.fVariable.fType, ref.fVariable.fModifiers.fLayout,
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400330 String::printf("_outer.%s", name.c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400331 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700332 this->write(ref.fVariable.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400333 }
334 }
335}
336
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400337void CPPCodeGenerator::writeIfStatement(const IfStatement& s) {
338 if (s.fIsStatic) {
339 this->write("@");
340 }
341 INHERITED::writeIfStatement(s);
342}
343
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400344void CPPCodeGenerator::writeReturnStatement(const ReturnStatement& s) {
345 if (fInMain) {
346 fErrors.error(s.fOffset, "fragmentProcessor main() may not contain return statements");
347 }
348 INHERITED::writeReturnStatement(s);
349}
350
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400351void CPPCodeGenerator::writeSwitchStatement(const SwitchStatement& s) {
352 if (s.fIsStatic) {
353 this->write("@");
354 }
355 INHERITED::writeSwitchStatement(s);
356}
357
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400358void CPPCodeGenerator::writeFieldAccess(const FieldAccess& access) {
359 if (access.fBase->fType.name() == "fragmentProcessor") {
360 // Special field access on fragment processors are converted into function calls on
361 // GrFragmentProcessor's getters.
362 if (access.fBase->fKind != Expression::kVariableReference_Kind) {
363 fErrors.error(access.fBase->fOffset, "fragmentProcessor must be a reference\n");
364 return;
365 }
366
367 const Type::Field& field = fContext.fFragmentProcessor_Type->fields()[access.fFieldIndex];
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500368 const Variable& var = ((const VariableReference&) *access.fBase).fVariable;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400369 String cppAccess = String::printf("_outer.childProcessor(_outer.%s_index).%s()",
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500370 String(var.fName).c_str(),
371 String(field.fName).c_str());
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400372
373 if (fCPPMode) {
374 this->write(cppAccess.c_str());
375 } else {
376 writeRuntimeValue(*field.fType, Layout(), cppAccess);
377 }
378 return;
379 }
380 INHERITED::writeFieldAccess(access);
381}
382
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500383int CPPCodeGenerator::getChildFPIndex(const Variable& var) const {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400384 int index = 0;
385 bool found = false;
386 for (const auto& p : fProgram) {
387 if (ProgramElement::kVar_Kind == p.fKind) {
388 const VarDeclarations& decls = (const VarDeclarations&) p;
389 for (const auto& raw : decls.fVars) {
390 const VarDeclaration& decl = (VarDeclaration&) *raw;
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500391 if (decl.fVar == &var) {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400392 found = true;
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500393 } else if (decl.fVar->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400394 ++index;
395 }
396 }
397 }
398 if (found) {
399 break;
400 }
401 }
402 SkASSERT(found);
403 return index;
404}
405
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400406void CPPCodeGenerator::writeFunctionCall(const FunctionCall& c) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400407 if (c.fFunction.fBuiltin && c.fFunction.fName == "process") {
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400408 // Sanity checks that are detected by function definition in sksl_fp.inc
409 SkASSERT(c.fArguments.size() == 1 || c.fArguments.size() == 2);
Florin Malita390f9bd2019-03-04 12:25:57 -0500410 SkASSERT("fragmentProcessor" == c.fArguments[0]->fType.name() ||
411 "fragmentProcessor?" == c.fArguments[0]->fType.name());
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400412
413 // Actually fail during compilation if arguments with valid types are
414 // provided that are not variable references, since process() is a
415 // special function that impacts code emission.
416 if (c.fArguments[0]->fKind != Expression::kVariableReference_Kind) {
417 fErrors.error(c.fArguments[0]->fOffset,
418 "process()'s fragmentProcessor argument must be a variable reference\n");
419 return;
420 }
421 if (c.fArguments.size() > 1) {
422 // Second argument must also be a half4 expression
423 SkASSERT("half4" == c.fArguments[1]->fType.name());
424 }
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500425 const Variable& child = ((const VariableReference&) *c.fArguments[0]).fVariable;
426 int index = getChildFPIndex(child);
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400427
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400428 // Start a new extra emit code section so that the emitted child processor can depend on
429 // sksl variables defined in earlier sksl code.
430 this->newExtraEmitCodeBlock();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400431
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400432 // Set to the empty string when no input color parameter should be emitted, which means this
433 // must be properly formatted with a prefixed comma when the parameter should be inserted
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000434 // into the emitChild() parameter list.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400435 String inputArg;
436 if (c.fArguments.size() > 1) {
437 SkASSERT(c.fArguments.size() == 2);
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000438 // Use the emitChild() variant that accepts an input color, so convert the 2nd
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400439 // argument's expression into C++ code that produces sksl stored in an SkString.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400440 String inputName = "_input" + to_string(index);
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400441 addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments[1], inputName));
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400442
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000443 // emitChild() needs a char*
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400444 inputArg = ", " + inputName + ".c_str()";
445 }
446
447 // Write the output handling after the possible input handling
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000448 String childName = "_child" + to_string(index);
449 addExtraEmitCodeLine("SkString " + childName + "(\"" + childName + "\");");
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500450 if (c.fArguments[0]->fType.kind() == Type::kNullable_Kind) {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400451 addExtraEmitCodeLine("if (_outer." + String(child.fName) + "_index >= 0) {\n ");
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500452 }
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000453 addExtraEmitCodeLine("this->emitChild(_outer." + String(child.fName) + "_index" +
454 inputArg + ", &" + childName + ", args);");
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500455 if (c.fArguments[0]->fType.kind() == Type::kNullable_Kind) {
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000456 // Null FPs are not emitted, but their output can still be referenced in dependent
457 // expressions - thus we always declare the variable.
458 // Note: this is essentially dead code required to satisfy the compiler, because
459 // 'process' function calls should always be guarded at a higher level, in the .fp
460 // source.
461 addExtraEmitCodeLine(
462 "} else {"
463 " fragBuilder->codeAppendf(\"half4 %s;\", " + childName + ".c_str());"
464 "}");
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500465 }
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000466 this->write("%s");
467 fFormatArgs.push_back(childName + ".c_str()");
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400468 return;
469 }
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400470 INHERITED::writeFunctionCall(c);
471 if (c.fFunction.fBuiltin && c.fFunction.fName == "texture") {
472 this->write(".%s");
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400473 SkASSERT(c.fArguments.size() >= 1);
474 SkASSERT(c.fArguments[0]->fKind == Expression::kVariableReference_Kind);
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400475 String sampler = this->getSamplerHandle(((VariableReference&) *c.fArguments[0]).fVariable);
476 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerSwizzle(" + sampler +
477 ").c_str()");
478 }
479}
480
Ethan Nicholas762466e2017-06-29 10:03:38 -0400481void CPPCodeGenerator::writeFunction(const FunctionDefinition& f) {
482 if (f.fDeclaration.fName == "main") {
483 fFunctionHeader = "";
484 OutputStream* oldOut = fOut;
485 StringStream buffer;
486 fOut = &buffer;
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400487 fInMain = true;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400488 for (const auto& s : ((Block&) *f.fBody).fStatements) {
489 this->writeStatement(*s);
490 this->writeLine();
491 }
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400492 fInMain = false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400493
494 fOut = oldOut;
495 this->write(fFunctionHeader);
496 this->write(buffer.str());
497 } else {
498 INHERITED::writeFunction(f);
499 }
500}
501
502void CPPCodeGenerator::writeSetting(const Setting& s) {
503 static constexpr const char* kPrefix = "sk_Args.";
504 if (!strncmp(s.fName.c_str(), kPrefix, strlen(kPrefix))) {
505 const char* name = s.fName.c_str() + strlen(kPrefix);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400506 this->writeRuntimeValue(s.fType, Layout(), HCodeGenerator::FieldName(name).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400507 } else {
508 this->write(s.fName.c_str());
509 }
510}
511
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400512bool CPPCodeGenerator::writeSection(const char* name, const char* prefix) {
Ethan Nicholas68990be2017-07-13 09:36:52 -0400513 const Section* s = fSectionAndParameterHelper.getSection(name);
514 if (s) {
515 this->writef("%s%s", prefix, s->fText.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400516 return true;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400517 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400518 return false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400519}
520
521void CPPCodeGenerator::writeProgramElement(const ProgramElement& p) {
522 if (p.fKind == ProgramElement::kSection_Kind) {
523 return;
524 }
525 if (p.fKind == ProgramElement::kVar_Kind) {
526 const VarDeclarations& decls = (const VarDeclarations&) p;
527 if (!decls.fVars.size()) {
528 return;
529 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000530 const Variable& var = *((VarDeclaration&) *decls.fVars[0]).fVar;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400531 if (var.fModifiers.fFlags & (Modifiers::kIn_Flag | Modifiers::kUniform_Flag) ||
532 -1 != var.fModifiers.fLayout.fBuiltin) {
533 return;
534 }
535 }
536 INHERITED::writeProgramElement(p);
537}
538
539void CPPCodeGenerator::addUniform(const Variable& var) {
540 if (!needs_uniform_var(var)) {
541 return;
542 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400543 const char* type;
544 if (var.fType == *fContext.fFloat_Type) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400545 type = "kFloat_GrSLType";
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400546 } else if (var.fType == *fContext.fHalf_Type) {
547 type = "kHalf_GrSLType";
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400548 } else if (var.fType == *fContext.fFloat2_Type) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400549 type = "kFloat2_GrSLType";
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400550 } else if (var.fType == *fContext.fHalf2_Type) {
551 type = "kHalf2_GrSLType";
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400552 } else if (var.fType == *fContext.fFloat4_Type) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400553 type = "kFloat4_GrSLType";
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400554 } else if (var.fType == *fContext.fHalf4_Type) {
555 type = "kHalf4_GrSLType";
Brian Osman1cb41712017-10-19 12:54:52 -0400556 } else if (var.fType == *fContext.fFloat4x4_Type) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400557 type = "kFloat4x4_GrSLType";
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400558 } else if (var.fType == *fContext.fHalf4x4_Type) {
559 type = "kHalf4x4_GrSLType";
Ethan Nicholas762466e2017-06-29 10:03:38 -0400560 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700561 ABORT("unsupported uniform type: %s %s;\n", String(var.fType.fName).c_str(),
562 String(var.fName).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400563 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400564 if (var.fModifiers.fLayout.fWhen.fLength) {
565 this->writef(" if (%s) {\n ", String(var.fModifiers.fLayout.fWhen).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400566 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700567 String name(var.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400568 this->writef(" %sVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, %s, "
Ethan Nicholas858fecc2019-03-07 13:19:18 -0500569 "\"%s\");\n", HCodeGenerator::FieldName(name.c_str()).c_str(), type,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700570 name.c_str());
Ethan Nicholasfc994162019-06-06 10:04:27 -0400571 if (var.fModifiers.fLayout.fWhen.fLength) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400572 this->write(" }\n");
573 }
574}
575
Ethan Nicholascd700e92018-08-24 16:43:57 -0400576void CPPCodeGenerator::writeInputVars() {
577}
578
Ethan Nicholas762466e2017-06-29 10:03:38 -0400579void CPPCodeGenerator::writePrivateVars() {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400580 for (const auto& p : fProgram) {
581 if (ProgramElement::kVar_Kind == p.fKind) {
582 const VarDeclarations& decls = (const VarDeclarations&) p;
583 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000584 VarDeclaration& decl = (VarDeclaration&) *raw;
585 if (is_private(*decl.fVar)) {
586 if (decl.fVar->fType == *fContext.fFragmentProcessor_Type) {
587 fErrors.error(decl.fOffset,
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400588 "fragmentProcessor variables must be declared 'in'");
589 return;
590 }
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500591 this->writef("%s %s = %s;\n",
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000592 HCodeGenerator::FieldType(fContext, decl.fVar->fType,
593 decl.fVar->fModifiers.fLayout).c_str(),
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500594 String(decl.fVar->fName).c_str(),
595 default_value(*decl.fVar).c_str());
Michael Ludwiga4275592018-08-31 10:52:47 -0400596 } else if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
597 // An auto-tracked uniform in variable, so add a field to hold onto the prior
598 // state. Note that tracked variables must be uniform in's and that is validated
599 // before writePrivateVars() is called.
600 const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *decl.fVar);
601 SkASSERT(mapper && mapper->supportsTracking());
602
603 String name = HCodeGenerator::FieldName(String(decl.fVar->fName).c_str());
604 // The member statement is different if the mapper reports a default value
605 if (mapper->defaultValue().size() > 0) {
606 this->writef("%s %sPrev = %s;\n",
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400607 Layout::CTypeToStr(mapper->ctype()), name.c_str(),
Michael Ludwiga4275592018-08-31 10:52:47 -0400608 mapper->defaultValue().c_str());
609 } else {
610 this->writef("%s %sPrev;\n",
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400611 Layout::CTypeToStr(mapper->ctype()), name.c_str());
Michael Ludwiga4275592018-08-31 10:52:47 -0400612 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400613 }
614 }
615 }
616 }
617}
618
619void CPPCodeGenerator::writePrivateVarValues() {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400620 for (const auto& p : fProgram) {
621 if (ProgramElement::kVar_Kind == p.fKind) {
622 const VarDeclarations& decls = (const VarDeclarations&) p;
623 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000624 VarDeclaration& decl = (VarDeclaration&) *raw;
625 if (is_private(*decl.fVar) && decl.fValue) {
626 this->writef("%s = ", String(decl.fVar->fName).c_str());
Ethan Nicholas82399462017-10-16 12:35:44 -0400627 fCPPMode = true;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000628 this->writeExpression(*decl.fValue, kAssignment_Precedence);
Ethan Nicholas82399462017-10-16 12:35:44 -0400629 fCPPMode = false;
630 this->write(";\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400631 }
632 }
633 }
634 }
635}
636
Ethan Nicholas82399462017-10-16 12:35:44 -0400637static bool is_accessible(const Variable& var) {
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500638 const Type& type = var.fType.nonnullable();
639 return Type::kSampler_Kind != type.kind() &&
640 Type::kOther_Kind != type.kind();
Ethan Nicholas82399462017-10-16 12:35:44 -0400641}
642
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400643void CPPCodeGenerator::newExtraEmitCodeBlock() {
644 // This should only be called when emitting SKSL for emitCode(), which can be detected if the
645 // cpp buffer is not null, and the cpp buffer is not the current output.
646 SkASSERT(fCPPBuffer && fCPPBuffer != fOut);
647
648 // Start a new block as an empty string
649 fExtraEmitCodeBlocks.push_back("");
650 // Mark its location in the output buffer, uses ${\d} for the token since ${} will not occur in
651 // valid sksl and makes detection trivial.
652 this->writef("${%zu}", fExtraEmitCodeBlocks.size() - 1);
653}
654
655void CPPCodeGenerator::addExtraEmitCodeLine(const String& toAppend) {
656 SkASSERT(fExtraEmitCodeBlocks.size() > 0);
657 String& currentBlock = fExtraEmitCodeBlocks[fExtraEmitCodeBlocks.size() - 1];
658 // Automatically add indentation and newline
659 currentBlock += " " + toAppend + "\n";
660}
661
662void CPPCodeGenerator::flushEmittedCode() {
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400663 if (fCPPBuffer == nullptr) {
664 // Not actually within writeEmitCode() so nothing to flush
665 return;
666 }
667
668 StringStream* skslBuffer = static_cast<StringStream*>(fOut);
669
670 String sksl = skslBuffer->str();
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400671 // Empty the accumulation buffer since its current contents are consumed.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400672 skslBuffer->reset();
673
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400674 // Switch to the cpp buffer
Michael Ludwigd0440192018-09-07 14:24:52 +0000675 fOut = fCPPBuffer;
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400676
677 // Iterate through the sksl, keeping track of where the last statement ended (e.g. the latest
678 // encountered ';', '{', or '}'). If an extra emit code block token is encountered then the
679 // code from 0 to last statement end is sent to writeCodeAppend, the extra code block is
680 // appended to the cpp buffer, and then the sksl string is trimmed to start where the last
681 // statement left off (minus the encountered token).
682 size_t i = 0;
683 int flushPoint = -1;
684 int tokenStart = -1;
685 while (i < sksl.size()) {
686 if (tokenStart >= 0) {
687 // Looking for the end of the token
688 if (sksl[i] == '}') {
689 // Must append the sksl from 0 to flushPoint (inclusive) then the extra code
690 // accumulated in the block with index parsed from chars [tokenStart+2, i-1]
691 String toFlush = String(sksl.c_str(), flushPoint + 1);
692 // writeCodeAppend automatically removes the format args that it consumed, so
693 // fFormatArgs will be in a valid state for any future sksl
694 this->writeCodeAppend(toFlush);
695
696 int codeBlock = stoi(String(sksl.c_str() + tokenStart + 2, i - tokenStart - 2));
697 SkASSERT(codeBlock < (int) fExtraEmitCodeBlocks.size());
698 if (fExtraEmitCodeBlocks[codeBlock].size() > 0) {
699 this->write(fExtraEmitCodeBlocks[codeBlock].c_str());
700 }
701
702 // Now reset the sksl buffer to start after the flush point, but remove the token.
703 String compacted = String(sksl.c_str() + flushPoint + 1,
704 tokenStart - flushPoint - 1);
705 if (i < sksl.size() - 1) {
706 compacted += String(sksl.c_str() + i + 1, sksl.size() - i - 1);
707 }
708 sksl = compacted;
709
710 // And reset iteration
711 i = -1;
712 flushPoint = -1;
713 tokenStart = -1;
714 }
715 } else {
716 // Looking for the start of extra emit block tokens, and tracking when statements end
717 if (sksl[i] == ';' || sksl[i] == '{' || sksl[i] == '}') {
718 flushPoint = i;
719 } else if (i < sksl.size() - 1 && sksl[i] == '$' && sksl[i + 1] == '{') {
720 // found an extra emit code block token
721 tokenStart = i++;
722 }
723 }
724 i++;
Michael Ludwigd0440192018-09-07 14:24:52 +0000725 }
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400726
727 // Once we've gone through the sksl string to this point, there are no remaining extra emit
728 // code blocks to interleave, so append the remainder as usual.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400729 this->writeCodeAppend(sksl);
730
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400731 // After appending, switch back to the emptied sksl buffer and reset the extra code blocks
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400732 fOut = skslBuffer;
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400733 fExtraEmitCodeBlocks.clear();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400734}
735
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400736void CPPCodeGenerator::writeCodeAppend(const String& code) {
737 // codeAppendf can only handle appending 1024 bytes at a time, so we need to break the string
738 // into chunks. Unfortunately we can't tell exactly how long the string is going to end up,
739 // because printf escape sequences get replaced by strings of unknown length, but keeping the
740 // format string below 512 bytes is probably safe.
741 static constexpr size_t maxChunkSize = 512;
742 size_t start = 0;
743 size_t index = 0;
744 size_t argStart = 0;
745 size_t argCount;
746 while (index < code.size()) {
747 argCount = 0;
748 this->write(" fragBuilder->codeAppendf(\"");
749 while (index < code.size() && index < start + maxChunkSize) {
750 if ('%' == code[index]) {
751 if (index == start + maxChunkSize - 1 || index == code.size() - 1) {
752 break;
753 }
754 if (code[index + 1] != '%') {
755 ++argCount;
756 }
Ethan Nicholasef0c9fd2017-10-30 10:04:14 -0400757 } else if ('\\' == code[index] && index == start + maxChunkSize - 1) {
758 // avoid splitting an escape sequence that happens to fall across a chunk boundary
759 break;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400760 }
761 ++index;
762 }
763 fOut->write(code.c_str() + start, index - start);
764 this->write("\"");
765 for (size_t i = argStart; i < argStart + argCount; ++i) {
766 this->writef(", %s", fFormatArgs[i].c_str());
767 }
768 this->write(");\n");
769 argStart += argCount;
770 start = index;
771 }
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400772
773 // argStart is equal to the number of fFormatArgs that were consumed
774 // so they should be removed from the list
775 if (argStart > 0) {
776 fFormatArgs.erase(fFormatArgs.begin(), fFormatArgs.begin() + argStart);
777 }
778}
779
780String CPPCodeGenerator::convertSKSLExpressionToCPP(const Expression& e,
781 const String& cppVar) {
782 // To do this conversion, we temporarily switch the sksl output stream
783 // to an empty stringstream and reset the format args to empty.
784 OutputStream* oldSKSL = fOut;
785 StringStream exprBuffer;
786 fOut = &exprBuffer;
787
788 std::vector<String> oldArgs(fFormatArgs);
789 fFormatArgs.clear();
790
791 // Convert the argument expression into a format string and args
792 this->writeExpression(e, Precedence::kTopLevel_Precedence);
793 std::vector<String> newArgs(fFormatArgs);
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400794 String expr = exprBuffer.str();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400795
796 // After generating, restore the original output stream and format args
797 fFormatArgs = oldArgs;
798 fOut = oldSKSL;
799
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400800 // The sksl written to exprBuffer is not processed by flushEmittedCode(), so any extra emit code
801 // block tokens won't get handled. So we need to strip them from the expression and stick them
802 // to the end of the original sksl stream.
803 String exprFormat = "";
804 int tokenStart = -1;
805 for (size_t i = 0; i < expr.size(); i++) {
806 if (tokenStart >= 0) {
807 if (expr[i] == '}') {
808 // End of the token, so append the token to fOut
809 fOut->write(expr.c_str() + tokenStart, i - tokenStart + 1);
810 tokenStart = -1;
811 }
812 } else {
813 if (i < expr.size() - 1 && expr[i] == '$' && expr[i + 1] == '{') {
814 tokenStart = i++;
815 } else {
816 exprFormat += expr[i];
817 }
818 }
819 }
820
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400821 // Now build the final C++ code snippet from the format string and args
822 String cppExpr;
823 if (newArgs.size() == 0) {
824 // This was a static expression, so we can simplify the input
825 // color declaration in the emitted code to just a static string
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400826 cppExpr = "SkString " + cppVar + "(\"" + exprFormat + "\");";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400827 } else {
828 // String formatting must occur dynamically, so have the C++ declaration
829 // use SkStringPrintf with the format args that were accumulated
830 // when the expression was written.
831 cppExpr = "SkString " + cppVar + " = SkStringPrintf(\"" + exprFormat + "\"";
832 for (size_t i = 0; i < newArgs.size(); i++) {
833 cppExpr += ", " + newArgs[i];
834 }
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400835 cppExpr += ");";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400836 }
837 return cppExpr;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400838}
839
Ethan Nicholas762466e2017-06-29 10:03:38 -0400840bool CPPCodeGenerator::writeEmitCode(std::vector<const Variable*>& uniforms) {
841 this->write(" void emitCode(EmitArgs& args) override {\n"
842 " GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;\n");
843 this->writef(" const %s& _outer = args.fFp.cast<%s>();\n"
844 " (void) _outer;\n",
845 fFullName.c_str(), fFullName.c_str());
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400846 for (const auto& p : fProgram) {
847 if (ProgramElement::kVar_Kind == p.fKind) {
848 const VarDeclarations& decls = (const VarDeclarations&) p;
849 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000850 VarDeclaration& decl = (VarDeclaration&) *raw;
851 String nameString(decl.fVar->fName);
Ethan Nicholas82399462017-10-16 12:35:44 -0400852 const char* name = nameString.c_str();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000853 if (SectionAndParameterHelper::IsParameter(*decl.fVar) &&
854 is_accessible(*decl.fVar)) {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400855 this->writef(" auto %s = _outer.%s;\n"
Ethan Nicholas82399462017-10-16 12:35:44 -0400856 " (void) %s;\n",
857 name, name, name);
858 }
859 }
860 }
861 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400862 this->writePrivateVarValues();
863 for (const auto u : uniforms) {
864 this->addUniform(*u);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400865 }
866 this->writeSection(EMIT_CODE_SECTION);
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400867
868 // Save original buffer as the CPP buffer for flushEmittedCode()
869 fCPPBuffer = fOut;
870 StringStream skslBuffer;
871 fOut = &skslBuffer;
872
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400873 this->newExtraEmitCodeBlock();
Ethan Nicholas762466e2017-06-29 10:03:38 -0400874 bool result = INHERITED::generateCode();
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400875 this->flushEmittedCode();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400876
877 // Then restore the original CPP buffer and close the function
878 fOut = fCPPBuffer;
879 fCPPBuffer = nullptr;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400880 this->write(" }\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400881 return result;
882}
883
884void CPPCodeGenerator::writeSetData(std::vector<const Variable*>& uniforms) {
885 const char* fullName = fFullName.c_str();
Ethan Nicholas68990be2017-07-13 09:36:52 -0400886 const Section* section = fSectionAndParameterHelper.getSection(SET_DATA_SECTION);
887 const char* pdman = section ? section->fArgument.c_str() : "pdman";
Ethan Nicholas762466e2017-06-29 10:03:38 -0400888 this->writef(" void onSetData(const GrGLSLProgramDataManager& %s, "
889 "const GrFragmentProcessor& _proc) override {\n",
890 pdman);
891 bool wroteProcessor = false;
892 for (const auto u : uniforms) {
Michael Ludwiga4275592018-08-31 10:52:47 -0400893 if (is_uniform_in(*u)) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400894 if (!wroteProcessor) {
895 this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName, fullName);
896 wroteProcessor = true;
897 this->writef(" {\n");
898 }
Michael Ludwiga4275592018-08-31 10:52:47 -0400899
900 const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *u);
901 SkASSERT(mapper);
902
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700903 String nameString(u->fName);
904 const char* name = nameString.c_str();
Michael Ludwiga4275592018-08-31 10:52:47 -0400905
906 // Switches for setData behavior in the generated code
907 bool conditionalUniform = u->fModifiers.fLayout.fWhen != "";
908 bool isTracked = u->fModifiers.fLayout.fFlags & Layout::kTracked_Flag;
909 bool needsValueDeclaration = isTracked || !mapper->canInlineUniformValue();
910
911 String uniformName = HCodeGenerator::FieldName(name) + "Var";
912
913 String indent = " "; // 8 by default, 12 when nested for conditional uniforms
914 if (conditionalUniform) {
915 // Add a pre-check to make sure the uniform was emitted
916 // before trying to send any data to the GPU
917 this->writef(" if (%s.isValid()) {\n", uniformName.c_str());
918 indent += " ";
919 }
920
921 String valueVar = "";
922 if (needsValueDeclaration) {
923 valueVar.appendf("%sValue", name);
924 // Use AccessType since that will match the return type of _outer's public API.
925 String valueType = HCodeGenerator::AccessType(fContext, u->fType,
926 u->fModifiers.fLayout);
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400927 this->writef("%s%s %s = _outer.%s;\n",
Michael Ludwiga4275592018-08-31 10:52:47 -0400928 indent.c_str(), valueType.c_str(), valueVar.c_str(), name);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400929 } else {
Michael Ludwiga4275592018-08-31 10:52:47 -0400930 // Not tracked and the mapper only needs to use the value once
931 // so send it a safe expression instead of the variable name
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400932 valueVar.appendf("(_outer.%s)", name);
Michael Ludwiga4275592018-08-31 10:52:47 -0400933 }
934
935 if (isTracked) {
936 SkASSERT(mapper->supportsTracking());
937
938 String prevVar = HCodeGenerator::FieldName(name) + "Prev";
939 this->writef("%sif (%s) {\n"
940 "%s %s;\n"
941 "%s %s;\n"
942 "%s}\n", indent.c_str(),
943 mapper->dirtyExpression(valueVar, prevVar).c_str(), indent.c_str(),
944 mapper->saveState(valueVar, prevVar).c_str(), indent.c_str(),
945 mapper->setUniform(pdman, uniformName, valueVar).c_str(), indent.c_str());
946 } else {
947 this->writef("%s%s;\n", indent.c_str(),
948 mapper->setUniform(pdman, uniformName, valueVar).c_str());
949 }
950
951 if (conditionalUniform) {
952 // Close the earlier precheck block
953 this->writef(" }\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400954 }
955 }
956 }
957 if (wroteProcessor) {
958 this->writef(" }\n");
959 }
Ethan Nicholas68990be2017-07-13 09:36:52 -0400960 if (section) {
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500961 int samplerIndex = 0;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400962 for (const auto& p : fProgram) {
963 if (ProgramElement::kVar_Kind == p.fKind) {
964 const VarDeclarations& decls = (const VarDeclarations&) p;
965 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000966 VarDeclaration& decl = (VarDeclaration&) *raw;
967 String nameString(decl.fVar->fName);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700968 const char* name = nameString.c_str();
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500969 if (decl.fVar->fType.kind() == Type::kSampler_Kind) {
970 this->writef(" GrSurfaceProxy& %sProxy = "
971 "*_outer.textureSampler(%d).proxy();\n",
972 name, samplerIndex);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400973 this->writef(" GrTexture& %s = *%sProxy.peekTexture();\n",
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500974 name, name);
975 this->writef(" (void) %s;\n", name);
976 ++samplerIndex;
977 } else if (needs_uniform_var(*decl.fVar)) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400978 this->writef(" UniformHandle& %s = %sVar;\n"
979 " (void) %s;\n",
980 name, HCodeGenerator::FieldName(name).c_str(), name);
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000981 } else if (SectionAndParameterHelper::IsParameter(*decl.fVar) &&
982 decl.fVar->fType != *fContext.fFragmentProcessor_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400983 if (!wroteProcessor) {
984 this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName,
985 fullName);
986 wroteProcessor = true;
987 }
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400988 this->writef(" auto %s = _outer.%s;\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400989 " (void) %s;\n",
990 name, name, name);
991 }
992 }
993 }
994 }
995 this->writeSection(SET_DATA_SECTION);
996 }
997 this->write(" }\n");
998}
999
Brian Salomonf7dcd762018-07-30 14:48:15 -04001000void CPPCodeGenerator::writeOnTextureSampler() {
1001 bool foundSampler = false;
1002 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
1003 if (param->fType.kind() == Type::kSampler_Kind) {
1004 if (!foundSampler) {
1005 this->writef(
1006 "const GrFragmentProcessor::TextureSampler& %s::onTextureSampler(int "
1007 "index) const {\n",
1008 fFullName.c_str());
1009 this->writef(" return IthTextureSampler(index, %s",
1010 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
1011 foundSampler = true;
1012 } else {
1013 this->writef(", %s",
1014 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
1015 }
1016 }
1017 }
1018 if (foundSampler) {
1019 this->write(");\n}\n");
1020 }
1021}
1022
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001023void CPPCodeGenerator::writeClone() {
1024 if (!this->writeSection(CLONE_SECTION)) {
1025 if (fSectionAndParameterHelper.getSection(FIELDS_SECTION)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001026 fErrors.error(0, "fragment processors with custom @fields must also have a custom"
1027 "@clone");
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001028 }
1029 this->writef("%s::%s(const %s& src)\n"
Ethan Nicholasabff9562017-10-09 10:54:08 -04001030 ": INHERITED(k%s_ClassID, src.optimizationFlags())", fFullName.c_str(),
1031 fFullName.c_str(), fFullName.c_str(), fFullName.c_str());
Ethan Nicholasbcd51e82019-04-09 10:40:41 -04001032 const auto transforms = fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION);
1033 for (size_t i = 0; i < transforms.size(); ++i) {
1034 const Section& s = *transforms[i];
1035 String fieldName = HCodeGenerator::CoordTransformName(s.fArgument, i);
1036 this->writef("\n, %s(src.%s)", fieldName.c_str(), fieldName.c_str());
1037 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001038 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Robert Phillipsbce7d862019-02-21 22:53:57 +00001039 String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str());
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001040 if (param->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
1041 this->writef("\n, %s_index(src.%s_index)",
1042 fieldName.c_str(),
1043 fieldName.c_str());
1044 } else {
1045 this->writef("\n, %s(src.%s)",
1046 fieldName.c_str(),
1047 fieldName.c_str());
1048 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001049 }
Ethan Nicholasabff9562017-10-09 10:54:08 -04001050 this->writef(" {\n");
Brian Salomonf7dcd762018-07-30 14:48:15 -04001051 int samplerCount = 0;
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001052 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
1053 if (param->fType.kind() == Type::kSampler_Kind) {
Brian Salomonf7dcd762018-07-30 14:48:15 -04001054 ++samplerCount;
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001055 } else if (param->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
1056 String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str());
1057 if (param->fType.kind() == Type::kNullable_Kind) {
1058 this->writef(" if (%s_index >= 0) {\n ", fieldName.c_str());
1059 }
1060 this->writef(" this->registerChildProcessor(src.childProcessor(%s_index)."
1061 "clone());\n", fieldName.c_str());
1062 if (param->fType.kind() == Type::kNullable_Kind) {
1063 this->writef(" }\n");
1064 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001065 }
1066 }
Brian Salomonf7dcd762018-07-30 14:48:15 -04001067 if (samplerCount) {
1068 this->writef(" this->setTextureSamplerCnt(%d);", samplerCount);
1069 }
Ethan Nicholas929a6812018-08-06 14:56:59 -04001070 for (size_t i = 0; i < transforms.size(); ++i) {
1071 const Section& s = *transforms[i];
1072 String fieldName = HCodeGenerator::CoordTransformName(s.fArgument, i);
1073 this->writef(" this->addCoordTransform(&%s);\n", fieldName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001074 }
1075 this->write("}\n");
Brian Salomonaff329b2017-08-11 09:40:37 -04001076 this->writef("std::unique_ptr<GrFragmentProcessor> %s::clone() const {\n",
1077 fFullName.c_str());
1078 this->writef(" return std::unique_ptr<GrFragmentProcessor>(new %s(*this));\n",
1079 fFullName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001080 this->write("}\n");
1081 }
1082}
1083
Ethan Nicholas762466e2017-06-29 10:03:38 -04001084void CPPCodeGenerator::writeTest() {
Ethan Nicholas68990be2017-07-13 09:36:52 -04001085 const Section* test = fSectionAndParameterHelper.getSection(TEST_CODE_SECTION);
1086 if (test) {
Brian Salomonaff329b2017-08-11 09:40:37 -04001087 this->writef(
1088 "GR_DEFINE_FRAGMENT_PROCESSOR_TEST(%s);\n"
1089 "#if GR_TEST_UTILS\n"
1090 "std::unique_ptr<GrFragmentProcessor> %s::TestCreate(GrProcessorTestData* %s) {\n",
1091 fFullName.c_str(),
1092 fFullName.c_str(),
1093 test->fArgument.c_str());
Ethan Nicholas68990be2017-07-13 09:36:52 -04001094 this->writeSection(TEST_CODE_SECTION);
1095 this->write("}\n"
1096 "#endif\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -04001097 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001098}
1099
1100void CPPCodeGenerator::writeGetKey() {
1101 this->writef("void %s::onGetGLSLProcessorKey(const GrShaderCaps& caps, "
1102 "GrProcessorKeyBuilder* b) const {\n",
1103 fFullName.c_str());
Ethan Nicholas68990be2017-07-13 09:36:52 -04001104 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001105 String nameString(param->fName);
1106 const char* name = nameString.c_str();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001107 if (param->fModifiers.fLayout.fKey != Layout::kNo_Key &&
1108 (param->fModifiers.fFlags & Modifiers::kUniform_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001109 fErrors.error(param->fOffset,
Ethan Nicholas762466e2017-06-29 10:03:38 -04001110 "layout(key) may not be specified on uniforms");
1111 }
1112 switch (param->fModifiers.fLayout.fKey) {
1113 case Layout::kKey_Key:
Ethan Nicholasfc994162019-06-06 10:04:27 -04001114 if (param->fModifiers.fLayout.fWhen.fLength) {
1115 this->writef("if (%s) {", String(param->fModifiers.fLayout.fWhen).c_str());
Ethan Nicholasbcd51e82019-04-09 10:40:41 -04001116 }
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001117 if (param->fType == *fContext.fFloat4x4_Type) {
1118 ABORT("no automatic key handling for float4x4\n");
1119 } else if (param->fType == *fContext.fFloat2_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001120 this->writef(" b->add32(%s.fX);\n",
1121 HCodeGenerator::FieldName(name).c_str());
1122 this->writef(" b->add32(%s.fY);\n",
1123 HCodeGenerator::FieldName(name).c_str());
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001124 } else if (param->fType == *fContext.fFloat4_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001125 this->writef(" b->add32(%s.x());\n",
1126 HCodeGenerator::FieldName(name).c_str());
1127 this->writef(" b->add32(%s.y());\n",
1128 HCodeGenerator::FieldName(name).c_str());
1129 this->writef(" b->add32(%s.width());\n",
1130 HCodeGenerator::FieldName(name).c_str());
1131 this->writef(" b->add32(%s.height());\n",
1132 HCodeGenerator::FieldName(name).c_str());
Brian Salomonc0d79e52019-04-10 15:02:11 -04001133 } else if (param->fType == *fContext.fHalf4_Type) {
1134 this->writef(" uint16_t red = SkFloatToHalf(%s.fR);\n",
1135 HCodeGenerator::FieldName(name).c_str());
1136 this->writef(" uint16_t green = SkFloatToHalf(%s.fG);\n",
1137 HCodeGenerator::FieldName(name).c_str());
1138 this->writef(" uint16_t blue = SkFloatToHalf(%s.fB);\n",
1139 HCodeGenerator::FieldName(name).c_str());
1140 this->writef(" uint16_t alpha = SkFloatToHalf(%s.fA);\n",
1141 HCodeGenerator::FieldName(name).c_str());
1142 this->write(" b->add32(((uint32_t)red << 16) | green);\n");
1143 this->write(" b->add32(((uint32_t)blue << 16) | alpha);\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -04001144 } else {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001145 this->writef(" b->add32((int32_t) %s);\n",
Ethan Nicholas762466e2017-06-29 10:03:38 -04001146 HCodeGenerator::FieldName(name).c_str());
1147 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04001148 if (param->fModifiers.fLayout.fWhen.fLength) {
Brian Salomonc0d79e52019-04-10 15:02:11 -04001149 this->write("}");
1150 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001151 break;
1152 case Layout::kIdentity_Key:
1153 if (param->fType.kind() != Type::kMatrix_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001154 fErrors.error(param->fOffset,
Ethan Nicholas762466e2017-06-29 10:03:38 -04001155 "layout(key=identity) requires matrix type");
1156 }
1157 this->writef(" b->add32(%s.isIdentity() ? 1 : 0);\n",
1158 HCodeGenerator::FieldName(name).c_str());
1159 break;
1160 case Layout::kNo_Key:
1161 break;
1162 }
1163 }
1164 this->write("}\n");
1165}
1166
1167bool CPPCodeGenerator::generateCode() {
1168 std::vector<const Variable*> uniforms;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001169 for (const auto& p : fProgram) {
1170 if (ProgramElement::kVar_Kind == p.fKind) {
1171 const VarDeclarations& decls = (const VarDeclarations&) p;
1172 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001173 VarDeclaration& decl = (VarDeclaration&) *raw;
1174 if ((decl.fVar->fModifiers.fFlags & Modifiers::kUniform_Flag) &&
1175 decl.fVar->fType.kind() != Type::kSampler_Kind) {
1176 uniforms.push_back(decl.fVar);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001177 }
Michael Ludwiga4275592018-08-31 10:52:47 -04001178
1179 if (is_uniform_in(*decl.fVar)) {
1180 // Validate the "uniform in" declarations to make sure they are fully supported,
1181 // instead of generating surprising C++
1182 const UniformCTypeMapper* mapper =
1183 UniformCTypeMapper::Get(fContext, *decl.fVar);
1184 if (mapper == nullptr) {
1185 fErrors.error(decl.fOffset, String(decl.fVar->fName)
1186 + "'s type is not supported for use as a 'uniform in'");
1187 return false;
1188 }
1189 if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
1190 if (!mapper->supportsTracking()) {
1191 fErrors.error(decl.fOffset, String(decl.fVar->fName)
1192 + "'s type does not support state tracking");
1193 return false;
1194 }
1195 }
1196
1197 } else {
1198 // If it's not a uniform_in, it's an error to be tracked
1199 if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
1200 fErrors.error(decl.fOffset, "Non-'in uniforms' cannot be tracked");
1201 return false;
1202 }
1203 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001204 }
1205 }
1206 }
1207 const char* baseName = fName.c_str();
1208 const char* fullName = fFullName.c_str();
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05001209 this->writef("%s\n", HCodeGenerator::GetHeader(fProgram, fErrors).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001210 this->writef(kFragmentProcessorHeader, fullName);
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001211 this->writef("#include \"%s.h\"\n\n", fullName);
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04001212 this->writeSection(CPP_SECTION);
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001213 this->writef("#include \"include/gpu/GrTexture.h\"\n"
1214 "#include \"src/gpu/glsl/GrGLSLFragmentProcessor.h\"\n"
1215 "#include \"src/gpu/glsl/GrGLSLFragmentShaderBuilder.h\"\n"
1216 "#include \"src/gpu/glsl/GrGLSLProgramBuilder.h\"\n"
1217 "#include \"src/sksl/SkSLCPP.h\"\n"
1218 "#include \"src/sksl/SkSLUtil.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -04001219 "class GrGLSL%s : public GrGLSLFragmentProcessor {\n"
1220 "public:\n"
1221 " GrGLSL%s() {}\n",
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04001222 baseName, baseName);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001223 bool result = this->writeEmitCode(uniforms);
1224 this->write("private:\n");
1225 this->writeSetData(uniforms);
1226 this->writePrivateVars();
1227 for (const auto& u : uniforms) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001228 if (needs_uniform_var(*u) && !(u->fModifiers.fFlags & Modifiers::kIn_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001229 this->writef(" UniformHandle %sVar;\n",
1230 HCodeGenerator::FieldName(String(u->fName).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001231 }
1232 }
Ethan Nicholas68990be2017-07-13 09:36:52 -04001233 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001234 if (needs_uniform_var(*param)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001235 this->writef(" UniformHandle %sVar;\n",
1236 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001237 }
1238 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001239 this->writef("};\n"
1240 "GrGLSLFragmentProcessor* %s::onCreateGLSLInstance() const {\n"
1241 " return new GrGLSL%s();\n"
1242 "}\n",
1243 fullName, baseName);
1244 this->writeGetKey();
1245 this->writef("bool %s::onIsEqual(const GrFragmentProcessor& other) const {\n"
1246 " const %s& that = other.cast<%s>();\n"
1247 " (void) that;\n",
1248 fullName, fullName, fullName);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001249 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001250 if (param->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -04001251 continue;
1252 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001253 String nameString(param->fName);
1254 const char* name = nameString.c_str();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001255 this->writef(" if (%s != that.%s) return false;\n",
1256 HCodeGenerator::FieldName(name).c_str(),
1257 HCodeGenerator::FieldName(name).c_str());
1258 }
1259 this->write(" return true;\n"
1260 "}\n");
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001261 this->writeClone();
Brian Salomonf7dcd762018-07-30 14:48:15 -04001262 this->writeOnTextureSampler();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001263 this->writeTest();
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04001264 this->writeSection(CPP_END_SECTION);
Greg Daniel3e8c3452018-04-06 10:37:55 -04001265
Ethan Nicholas762466e2017-06-29 10:03:38 -04001266 result &= 0 == fErrors.errorCount();
1267 return result;
1268}
1269
1270} // namespace