blob: be402088cce5d42d1857cad3ff172cbf3f6f3715 [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"
11#include "SkSLHCodeGenerator.h"
12
13namespace SkSL {
14
15static bool needs_uniform_var(const Variable& var) {
Ethan Nicholas5f9836e2017-12-20 15:16:33 -050016 return (var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
17 var.fType.kind() != Type::kSampler_Kind;
Ethan Nicholas762466e2017-06-29 10:03:38 -040018}
19
20CPPCodeGenerator::CPPCodeGenerator(const Context* context, const Program* program,
21 ErrorReporter* errors, String name, OutputStream* out)
22: INHERITED(context, program, errors, out)
23, fName(std::move(name))
24, fFullName(String::printf("Gr%s", fName.c_str()))
25, fSectionAndParameterHelper(*program, *errors) {
26 fLineEnding = "\\n";
27}
28
29void CPPCodeGenerator::writef(const char* s, va_list va) {
30 static constexpr int BUFFER_SIZE = 1024;
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040031 va_list copy;
32 va_copy(copy, va);
Ethan Nicholas762466e2017-06-29 10:03:38 -040033 char buffer[BUFFER_SIZE];
34 int length = vsnprintf(buffer, BUFFER_SIZE, s, va);
35 if (length < BUFFER_SIZE) {
36 fOut->write(buffer, length);
37 } else {
38 std::unique_ptr<char[]> heap(new char[length + 1]);
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040039 vsprintf(heap.get(), s, copy);
Ethan Nicholas762466e2017-06-29 10:03:38 -040040 fOut->write(heap.get(), length);
41 }
42}
43
44void CPPCodeGenerator::writef(const char* s, ...) {
45 va_list va;
46 va_start(va, s);
47 this->writef(s, va);
48 va_end(va);
49}
50
51void CPPCodeGenerator::writeHeader() {
52}
53
Ethan Nicholasf7b88202017-09-18 14:10:39 -040054bool CPPCodeGenerator::usesPrecisionModifiers() const {
55 return false;
Ethan Nicholas762466e2017-06-29 10:03:38 -040056}
57
Ethan Nicholasf7b88202017-09-18 14:10:39 -040058String CPPCodeGenerator::getTypeName(const Type& type) {
59 return type.name();
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040060}
Ethan Nicholasf7b88202017-09-18 14:10:39 -040061
Ethan Nicholas762466e2017-06-29 10:03:38 -040062void CPPCodeGenerator::writeBinaryExpression(const BinaryExpression& b,
63 Precedence parentPrecedence) {
64 if (b.fOperator == Token::PERCENT) {
65 // need to use "%%" instead of "%" b/c the code will be inside of a printf
66 Precedence precedence = GetBinaryPrecedence(b.fOperator);
67 if (precedence >= parentPrecedence) {
68 this->write("(");
69 }
70 this->writeExpression(*b.fLeft, precedence);
71 this->write(" %% ");
72 this->writeExpression(*b.fRight, precedence);
73 if (precedence >= parentPrecedence) {
74 this->write(")");
75 }
76 } else {
77 INHERITED::writeBinaryExpression(b, parentPrecedence);
78 }
79}
80
81void CPPCodeGenerator::writeIndexExpression(const IndexExpression& i) {
82 const Expression& base = *i.fBase;
83 if (base.fKind == Expression::kVariableReference_Kind) {
84 int builtin = ((VariableReference&) base).fVariable.fModifiers.fLayout.fBuiltin;
85 if (SK_TRANSFORMEDCOORDS2D_BUILTIN == builtin) {
86 this->write("%s");
87 if (i.fIndex->fKind != Expression::kIntLiteral_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070088 fErrors.error(i.fIndex->fOffset,
Ethan Nicholas762466e2017-06-29 10:03:38 -040089 "index into sk_TransformedCoords2D must be an integer literal");
90 return;
91 }
92 int64_t index = ((IntLiteral&) *i.fIndex).fValue;
93 String name = "sk_TransformedCoords2D_" + to_string(index);
94 fFormatArgs.push_back(name + ".c_str()");
95 if (fWrittenTransformedCoords.find(index) == fWrittenTransformedCoords.end()) {
Brian Osman72a37be2017-08-15 09:19:53 -040096 fExtraEmitCodeCode += " SkString " + name +
Ethan Nicholas762466e2017-06-29 10:03:38 -040097 " = fragBuilder->ensureCoords2D(args.fTransformedCoords[" +
98 to_string(index) + "]);\n";
99 fWrittenTransformedCoords.insert(index);
100 }
101 return;
102 } else if (SK_TEXTURESAMPLERS_BUILTIN == builtin) {
103 this->write("%s");
104 if (i.fIndex->fKind != Expression::kIntLiteral_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700105 fErrors.error(i.fIndex->fOffset,
Ethan Nicholas762466e2017-06-29 10:03:38 -0400106 "index into sk_TextureSamplers must be an integer literal");
107 return;
108 }
109 int64_t index = ((IntLiteral&) *i.fIndex).fValue;
110 fFormatArgs.push_back(" fragBuilder->getProgramBuilder()->samplerVariable("
111 "args.fTexSamplers[" + to_string(index) + "]).c_str()");
112 return;
113 }
114 }
115 INHERITED::writeIndexExpression(i);
116}
117
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400118static String default_value(const Type& type) {
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500119 if (type.fName == "bool") {
120 return "false";
121 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400122 switch (type.kind()) {
123 case Type::kScalar_Kind: return "0";
124 case Type::kVector_Kind: return type.name() + "(0)";
125 case Type::kMatrix_Kind: return type.name() + "(1)";
126 default: ABORT("unsupported default_value type\n");
127 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400128}
129
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500130static String default_value(const Variable& var) {
131 if (var.fModifiers.fLayout.fCType == "GrColor4f") {
132 return "GrColor4f::kIllegalConstructor";
133 }
134 return default_value(var.fType);
135}
136
Ethan Nicholas762466e2017-06-29 10:03:38 -0400137static bool is_private(const Variable& var) {
138 return !(var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
139 !(var.fModifiers.fFlags & Modifiers::kIn_Flag) &&
140 var.fStorage == Variable::kGlobal_Storage &&
141 var.fModifiers.fLayout.fBuiltin == -1;
142}
143
Ethan Nicholasd608c092017-10-26 09:30:08 -0400144void CPPCodeGenerator::writeRuntimeValue(const Type& type, const Layout& layout,
145 const String& cppCode) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400146 if (type.isFloat()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400147 this->write("%f");
148 fFormatArgs.push_back(cppCode);
149 } else if (type == *fContext.fInt_Type) {
150 this->write("%d");
151 fFormatArgs.push_back(cppCode);
152 } else if (type == *fContext.fBool_Type) {
153 this->write("%s");
154 fFormatArgs.push_back("(" + cppCode + " ? \"true\" : \"false\")");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400155 } else if (type == *fContext.fFloat2_Type || type == *fContext.fHalf2_Type) {
156 this->write(type.name() + "(%f, %f)");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400157 fFormatArgs.push_back(cppCode + ".fX");
158 fFormatArgs.push_back(cppCode + ".fY");
Ethan Nicholas82399462017-10-16 12:35:44 -0400159 } else if (type == *fContext.fFloat4_Type || type == *fContext.fHalf4_Type) {
160 this->write(type.name() + "(%f, %f, %f, %f)");
Ethan Nicholasd608c092017-10-26 09:30:08 -0400161 if (layout.fCType == "SkPMColor") {
162 fFormatArgs.push_back("SkGetPackedR32(" + cppCode + ") / 255.0");
163 fFormatArgs.push_back("SkGetPackedG32(" + cppCode + ") / 255.0");
164 fFormatArgs.push_back("SkGetPackedB32(" + cppCode + ") / 255.0");
165 fFormatArgs.push_back("SkGetPackedA32(" + cppCode + ") / 255.0");
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500166 } else if (layout.fCType == "GrColor4f") {
167 fFormatArgs.push_back(cppCode + ".fRGBA[0]");
168 fFormatArgs.push_back(cppCode + ".fRGBA[1]");
169 fFormatArgs.push_back(cppCode + ".fRGBA[2]");
170 fFormatArgs.push_back(cppCode + ".fRGBA[3]");
Ethan Nicholasd608c092017-10-26 09:30:08 -0400171 } else {
172 fFormatArgs.push_back(cppCode + ".left()");
173 fFormatArgs.push_back(cppCode + ".top()");
174 fFormatArgs.push_back(cppCode + ".right()");
175 fFormatArgs.push_back(cppCode + ".bottom()");
176 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500177 } else if (type.kind() == Type::kEnum_Kind) {
178 this->write("%d");
179 fFormatArgs.push_back("(int) " + cppCode);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400180 } else if (type == *fContext.fInt4_Type ||
181 type == *fContext.fShort4_Type ||
182 type == *fContext.fByte4_Type) {
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500183 this->write(type.name() + "(%d, %d, %d, %d)");
184 fFormatArgs.push_back(cppCode + ".left()");
185 fFormatArgs.push_back(cppCode + ".top()");
186 fFormatArgs.push_back(cppCode + ".right()");
187 fFormatArgs.push_back(cppCode + ".bottom()");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400188 } else {
Ethan Nicholas82399462017-10-16 12:35:44 -0400189 printf("unsupported runtime value type '%s'\n", String(type.fName).c_str());
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400190 SkASSERT(false);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400191 }
192}
193
194void CPPCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) {
195 if (is_private(var)) {
Ethan Nicholasd608c092017-10-26 09:30:08 -0400196 this->writeRuntimeValue(var.fType, var.fModifiers.fLayout, var.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400197 } else {
198 this->writeExpression(value, kTopLevel_Precedence);
199 }
200}
201
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400202String CPPCodeGenerator::getSamplerHandle(const Variable& var) {
203 int samplerCount = 0;
Ethan Nicholas68990be2017-07-13 09:36:52 -0400204 for (const auto param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400205 if (&var == param) {
206 return "args.fTexSamplers[" + to_string(samplerCount) + "]";
207 }
208 if (param->fType.kind() == Type::kSampler_Kind) {
209 ++samplerCount;
210 }
211 }
212 ABORT("should have found sampler in parameters\n");
213}
214
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400215void CPPCodeGenerator::writeIntLiteral(const IntLiteral& i) {
216 this->write(to_string((int32_t) i.fValue));
217}
218
Ethan Nicholas82399462017-10-16 12:35:44 -0400219void CPPCodeGenerator::writeSwizzle(const Swizzle& swizzle) {
220 if (fCPPMode) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400221 SkASSERT(swizzle.fComponents.size() == 1); // no support for multiple swizzle components yet
Ethan Nicholas82399462017-10-16 12:35:44 -0400222 this->writeExpression(*swizzle.fBase, kPostfix_Precedence);
223 switch (swizzle.fComponents[0]) {
224 case 0: this->write(".left()"); break;
225 case 1: this->write(".top()"); break;
226 case 2: this->write(".right()"); break;
227 case 3: this->write(".bottom()"); break;
228 }
229 } else {
230 INHERITED::writeSwizzle(swizzle);
231 }
232}
233
Ethan Nicholas762466e2017-06-29 10:03:38 -0400234void CPPCodeGenerator::writeVariableReference(const VariableReference& ref) {
Ethan Nicholas82399462017-10-16 12:35:44 -0400235 if (fCPPMode) {
236 this->write(ref.fVariable.fName);
237 return;
238 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400239 switch (ref.fVariable.fModifiers.fLayout.fBuiltin) {
240 case SK_INCOLOR_BUILTIN:
241 this->write("%s");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400242 fFormatArgs.push_back(String("args.fInputColor ? args.fInputColor : \"half4(1)\""));
Ethan Nicholas762466e2017-06-29 10:03:38 -0400243 break;
244 case SK_OUTCOLOR_BUILTIN:
245 this->write("%s");
246 fFormatArgs.push_back(String("args.fOutputColor"));
247 break;
248 default:
249 if (ref.fVariable.fType.kind() == Type::kSampler_Kind) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400250 this->write("%s");
251 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerVariable(" +
252 this->getSamplerHandle(ref.fVariable) + ").c_str()");
253 return;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400254 }
255 if (ref.fVariable.fModifiers.fFlags & Modifiers::kUniform_Flag) {
256 this->write("%s");
257 String name = ref.fVariable.fName;
Brian Osman1cb41712017-10-19 12:54:52 -0400258 String var = String::printf("args.fUniformHandler->getUniformCStr(%sVar)",
259 HCodeGenerator::FieldName(name.c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400260 String code;
261 if (ref.fVariable.fModifiers.fLayout.fWhen.size()) {
262 code = String::printf("%sVar.isValid() ? %s : \"%s\"",
263 HCodeGenerator::FieldName(name.c_str()).c_str(),
264 var.c_str(),
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400265 default_value(ref.fVariable.fType).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400266 } else {
267 code = var;
268 }
269 fFormatArgs.push_back(code);
270 } else if (SectionAndParameterHelper::IsParameter(ref.fVariable)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700271 String name(ref.fVariable.fName);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400272 this->writeRuntimeValue(ref.fVariable.fType, ref.fVariable.fModifiers.fLayout,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700273 String::printf("_outer.%s()", name.c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400274 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700275 this->write(ref.fVariable.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400276 }
277 }
278}
279
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400280void CPPCodeGenerator::writeIfStatement(const IfStatement& s) {
281 if (s.fIsStatic) {
282 this->write("@");
283 }
284 INHERITED::writeIfStatement(s);
285}
286
287void CPPCodeGenerator::writeSwitchStatement(const SwitchStatement& s) {
288 if (s.fIsStatic) {
289 this->write("@");
290 }
291 INHERITED::writeSwitchStatement(s);
292}
293
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400294void CPPCodeGenerator::writeFunctionCall(const FunctionCall& c) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400295 if (c.fFunction.fBuiltin && c.fFunction.fName == "process") {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400296 SkASSERT(c.fArguments.size() == 1);
297 SkASSERT(Expression::kVariableReference_Kind == c.fArguments[0]->fKind);
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400298 int index = 0;
299 bool found = false;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400300 for (const auto& p : fProgram) {
301 if (ProgramElement::kVar_Kind == p.fKind) {
302 const VarDeclarations& decls = (const VarDeclarations&) p;
303 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000304 VarDeclaration& decl = (VarDeclaration&) *raw;
305 if (decl.fVar == &((VariableReference&) *c.fArguments[0]).fVariable) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400306 found = true;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000307 } else if (decl.fVar->fType == *fContext.fFragmentProcessor_Type) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400308 ++index;
309 }
310 }
311 }
312 if (found) {
313 break;
314 }
315 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400316 SkASSERT(found);
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400317 String childName = "_child" + to_string(index);
318 fExtraEmitCodeCode += " SkString " + childName + "(\"" + childName + "\");\n" +
319 " this->emitChild(" + to_string(index) + ", &" + childName +
320 ", args);\n";
321 this->write("%s");
322 fFormatArgs.push_back(childName + ".c_str()");
323 return;
324 }
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400325 INHERITED::writeFunctionCall(c);
326 if (c.fFunction.fBuiltin && c.fFunction.fName == "texture") {
327 this->write(".%s");
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400328 SkASSERT(c.fArguments.size() >= 1);
329 SkASSERT(c.fArguments[0]->fKind == Expression::kVariableReference_Kind);
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400330 String sampler = this->getSamplerHandle(((VariableReference&) *c.fArguments[0]).fVariable);
331 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerSwizzle(" + sampler +
332 ").c_str()");
333 }
334}
335
Ethan Nicholas762466e2017-06-29 10:03:38 -0400336void CPPCodeGenerator::writeFunction(const FunctionDefinition& f) {
337 if (f.fDeclaration.fName == "main") {
338 fFunctionHeader = "";
339 OutputStream* oldOut = fOut;
340 StringStream buffer;
341 fOut = &buffer;
342 for (const auto& s : ((Block&) *f.fBody).fStatements) {
343 this->writeStatement(*s);
344 this->writeLine();
345 }
346
347 fOut = oldOut;
348 this->write(fFunctionHeader);
349 this->write(buffer.str());
350 } else {
351 INHERITED::writeFunction(f);
352 }
353}
354
355void CPPCodeGenerator::writeSetting(const Setting& s) {
356 static constexpr const char* kPrefix = "sk_Args.";
357 if (!strncmp(s.fName.c_str(), kPrefix, strlen(kPrefix))) {
358 const char* name = s.fName.c_str() + strlen(kPrefix);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400359 this->writeRuntimeValue(s.fType, Layout(), HCodeGenerator::FieldName(name).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400360 } else {
361 this->write(s.fName.c_str());
362 }
363}
364
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400365bool CPPCodeGenerator::writeSection(const char* name, const char* prefix) {
Ethan Nicholas68990be2017-07-13 09:36:52 -0400366 const Section* s = fSectionAndParameterHelper.getSection(name);
367 if (s) {
368 this->writef("%s%s", prefix, s->fText.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400369 return true;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400370 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400371 return false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400372}
373
374void CPPCodeGenerator::writeProgramElement(const ProgramElement& p) {
375 if (p.fKind == ProgramElement::kSection_Kind) {
376 return;
377 }
378 if (p.fKind == ProgramElement::kVar_Kind) {
379 const VarDeclarations& decls = (const VarDeclarations&) p;
380 if (!decls.fVars.size()) {
381 return;
382 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000383 const Variable& var = *((VarDeclaration&) *decls.fVars[0]).fVar;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400384 if (var.fModifiers.fFlags & (Modifiers::kIn_Flag | Modifiers::kUniform_Flag) ||
385 -1 != var.fModifiers.fLayout.fBuiltin) {
386 return;
387 }
388 }
389 INHERITED::writeProgramElement(p);
390}
391
392void CPPCodeGenerator::addUniform(const Variable& var) {
393 if (!needs_uniform_var(var)) {
394 return;
395 }
396 const char* precision;
397 if (var.fModifiers.fFlags & Modifiers::kHighp_Flag) {
398 precision = "kHigh_GrSLPrecision";
399 } else if (var.fModifiers.fFlags & Modifiers::kMediump_Flag) {
400 precision = "kMedium_GrSLPrecision";
401 } else if (var.fModifiers.fFlags & Modifiers::kLowp_Flag) {
402 precision = "kLow_GrSLPrecision";
403 } else {
404 precision = "kDefault_GrSLPrecision";
405 }
406 const char* type;
407 if (var.fType == *fContext.fFloat_Type) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400408 type = "kFloat_GrSLType";
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400409 } else if (var.fType == *fContext.fHalf_Type) {
410 type = "kHalf_GrSLType";
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400411 } else if (var.fType == *fContext.fFloat2_Type) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400412 type = "kFloat2_GrSLType";
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400413 } else if (var.fType == *fContext.fHalf2_Type) {
414 type = "kHalf2_GrSLType";
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400415 } else if (var.fType == *fContext.fFloat4_Type) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400416 type = "kFloat4_GrSLType";
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400417 } else if (var.fType == *fContext.fHalf4_Type) {
418 type = "kHalf4_GrSLType";
Brian Osman1cb41712017-10-19 12:54:52 -0400419 } else if (var.fType == *fContext.fFloat4x4_Type) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400420 type = "kFloat4x4_GrSLType";
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400421 } else if (var.fType == *fContext.fHalf4x4_Type) {
422 type = "kHalf4x4_GrSLType";
Ethan Nicholas762466e2017-06-29 10:03:38 -0400423 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700424 ABORT("unsupported uniform type: %s %s;\n", String(var.fType.fName).c_str(),
425 String(var.fName).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400426 }
427 if (var.fModifiers.fLayout.fWhen.size()) {
428 this->writef(" if (%s) {\n ", var.fModifiers.fLayout.fWhen.c_str());
429 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700430 String name(var.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400431 this->writef(" %sVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, %s, "
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700432 "%s, \"%s\");\n", HCodeGenerator::FieldName(name.c_str()).c_str(), type, precision,
433 name.c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400434 if (var.fModifiers.fLayout.fWhen.size()) {
435 this->write(" }\n");
436 }
437}
438
439void CPPCodeGenerator::writePrivateVars() {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400440 for (const auto& p : fProgram) {
441 if (ProgramElement::kVar_Kind == p.fKind) {
442 const VarDeclarations& decls = (const VarDeclarations&) p;
443 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000444 VarDeclaration& decl = (VarDeclaration&) *raw;
445 if (is_private(*decl.fVar)) {
446 if (decl.fVar->fType == *fContext.fFragmentProcessor_Type) {
447 fErrors.error(decl.fOffset,
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400448 "fragmentProcessor variables must be declared 'in'");
449 return;
450 }
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500451 this->writef("%s %s = %s;\n",
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000452 HCodeGenerator::FieldType(fContext, decl.fVar->fType,
453 decl.fVar->fModifiers.fLayout).c_str(),
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500454 String(decl.fVar->fName).c_str(),
455 default_value(*decl.fVar).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400456 }
457 }
458 }
459 }
460}
461
462void CPPCodeGenerator::writePrivateVarValues() {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400463 for (const auto& p : fProgram) {
464 if (ProgramElement::kVar_Kind == p.fKind) {
465 const VarDeclarations& decls = (const VarDeclarations&) p;
466 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000467 VarDeclaration& decl = (VarDeclaration&) *raw;
468 if (is_private(*decl.fVar) && decl.fValue) {
469 this->writef("%s = ", String(decl.fVar->fName).c_str());
Ethan Nicholas82399462017-10-16 12:35:44 -0400470 fCPPMode = true;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000471 this->writeExpression(*decl.fValue, kAssignment_Precedence);
Ethan Nicholas82399462017-10-16 12:35:44 -0400472 fCPPMode = false;
473 this->write(";\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400474 }
475 }
476 }
477 }
478}
479
Ethan Nicholas82399462017-10-16 12:35:44 -0400480static bool is_accessible(const Variable& var) {
481 return Type::kSampler_Kind != var.fType.kind() &&
482 Type::kOther_Kind != var.fType.kind();
483}
484
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400485void CPPCodeGenerator::writeCodeAppend(const String& code) {
486 // codeAppendf can only handle appending 1024 bytes at a time, so we need to break the string
487 // into chunks. Unfortunately we can't tell exactly how long the string is going to end up,
488 // because printf escape sequences get replaced by strings of unknown length, but keeping the
489 // format string below 512 bytes is probably safe.
490 static constexpr size_t maxChunkSize = 512;
491 size_t start = 0;
492 size_t index = 0;
493 size_t argStart = 0;
494 size_t argCount;
495 while (index < code.size()) {
496 argCount = 0;
497 this->write(" fragBuilder->codeAppendf(\"");
498 while (index < code.size() && index < start + maxChunkSize) {
499 if ('%' == code[index]) {
500 if (index == start + maxChunkSize - 1 || index == code.size() - 1) {
501 break;
502 }
503 if (code[index + 1] != '%') {
504 ++argCount;
505 }
Ethan Nicholasef0c9fd2017-10-30 10:04:14 -0400506 } else if ('\\' == code[index] && index == start + maxChunkSize - 1) {
507 // avoid splitting an escape sequence that happens to fall across a chunk boundary
508 break;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400509 }
510 ++index;
511 }
512 fOut->write(code.c_str() + start, index - start);
513 this->write("\"");
514 for (size_t i = argStart; i < argStart + argCount; ++i) {
515 this->writef(", %s", fFormatArgs[i].c_str());
516 }
517 this->write(");\n");
518 argStart += argCount;
519 start = index;
520 }
521}
522
Ethan Nicholas762466e2017-06-29 10:03:38 -0400523bool CPPCodeGenerator::writeEmitCode(std::vector<const Variable*>& uniforms) {
524 this->write(" void emitCode(EmitArgs& args) override {\n"
525 " GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;\n");
526 this->writef(" const %s& _outer = args.fFp.cast<%s>();\n"
527 " (void) _outer;\n",
528 fFullName.c_str(), fFullName.c_str());
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400529 for (const auto& p : fProgram) {
530 if (ProgramElement::kVar_Kind == p.fKind) {
531 const VarDeclarations& decls = (const VarDeclarations&) p;
532 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000533 VarDeclaration& decl = (VarDeclaration&) *raw;
534 String nameString(decl.fVar->fName);
Ethan Nicholas82399462017-10-16 12:35:44 -0400535 const char* name = nameString.c_str();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000536 if (SectionAndParameterHelper::IsParameter(*decl.fVar) &&
537 is_accessible(*decl.fVar)) {
Ethan Nicholas82399462017-10-16 12:35:44 -0400538 this->writef(" auto %s = _outer.%s();\n"
539 " (void) %s;\n",
540 name, name, name);
541 }
542 }
543 }
544 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400545 this->writePrivateVarValues();
546 for (const auto u : uniforms) {
547 this->addUniform(*u);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400548 }
549 this->writeSection(EMIT_CODE_SECTION);
550 OutputStream* old = fOut;
551 StringStream mainBuffer;
552 fOut = &mainBuffer;
553 bool result = INHERITED::generateCode();
554 fOut = old;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400555 this->writef("%s", fExtraEmitCodeCode.c_str());
556 this->writeCodeAppend(mainBuffer.str());
557 this->write(" }\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400558 return result;
559}
560
561void CPPCodeGenerator::writeSetData(std::vector<const Variable*>& uniforms) {
562 const char* fullName = fFullName.c_str();
Ethan Nicholas68990be2017-07-13 09:36:52 -0400563 const Section* section = fSectionAndParameterHelper.getSection(SET_DATA_SECTION);
564 const char* pdman = section ? section->fArgument.c_str() : "pdman";
Ethan Nicholas762466e2017-06-29 10:03:38 -0400565 this->writef(" void onSetData(const GrGLSLProgramDataManager& %s, "
566 "const GrFragmentProcessor& _proc) override {\n",
567 pdman);
568 bool wroteProcessor = false;
569 for (const auto u : uniforms) {
570 if (u->fModifiers.fFlags & Modifiers::kIn_Flag) {
571 if (!wroteProcessor) {
572 this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName, fullName);
573 wroteProcessor = true;
574 this->writef(" {\n");
575 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700576 String nameString(u->fName);
577 const char* name = nameString.c_str();
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400578 if (u->fType == *fContext.fFloat4_Type || u->fType == *fContext.fHalf4_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400579 this->writef(" const SkRect %sValue = _outer.%s();\n"
Ethan Nicholasee338732017-07-17 15:13:45 -0400580 " %s.set4fv(%sVar, 1, (float*) &%sValue);\n",
Ethan Nicholas762466e2017-06-29 10:03:38 -0400581 name, name, pdman, HCodeGenerator::FieldName(name).c_str(), name);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400582 } else if (u->fType == *fContext.fFloat4x4_Type ||
583 u->fType == *fContext.fHalf4x4_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400584 this->writef(" float %sValue[16];\n"
585 " _outer.%s().asColMajorf(%sValue);\n"
586 " %s.setMatrix4f(%sVar, %sValue);\n",
587 name, name, name, pdman, HCodeGenerator::FieldName(name).c_str(),
588 name);
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400589 } else if (u->fType == *fContext.fFragmentProcessor_Type) {
590 // do nothing
Ethan Nicholas762466e2017-06-29 10:03:38 -0400591 } else {
592 this->writef(" %s.set1f(%sVar, _outer.%s());\n",
593 pdman, HCodeGenerator::FieldName(name).c_str(), name);
594 }
595 }
596 }
597 if (wroteProcessor) {
598 this->writef(" }\n");
599 }
Ethan Nicholas68990be2017-07-13 09:36:52 -0400600 if (section) {
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500601 int samplerIndex = 0;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400602 for (const auto& p : fProgram) {
603 if (ProgramElement::kVar_Kind == p.fKind) {
604 const VarDeclarations& decls = (const VarDeclarations&) p;
605 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000606 VarDeclaration& decl = (VarDeclaration&) *raw;
607 String nameString(decl.fVar->fName);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700608 const char* name = nameString.c_str();
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500609 if (decl.fVar->fType.kind() == Type::kSampler_Kind) {
610 this->writef(" GrSurfaceProxy& %sProxy = "
611 "*_outer.textureSampler(%d).proxy();\n",
612 name, samplerIndex);
613 this->writef(" GrTexture& %s = *%sProxy.priv().peekTexture();\n",
614 name, name);
615 this->writef(" (void) %s;\n", name);
616 ++samplerIndex;
617 } else if (needs_uniform_var(*decl.fVar)) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400618 this->writef(" UniformHandle& %s = %sVar;\n"
619 " (void) %s;\n",
620 name, HCodeGenerator::FieldName(name).c_str(), name);
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000621 } else if (SectionAndParameterHelper::IsParameter(*decl.fVar) &&
622 decl.fVar->fType != *fContext.fFragmentProcessor_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400623 if (!wroteProcessor) {
624 this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName,
625 fullName);
626 wroteProcessor = true;
627 }
628 this->writef(" auto %s = _outer.%s();\n"
629 " (void) %s;\n",
630 name, name, name);
631 }
632 }
633 }
634 }
635 this->writeSection(SET_DATA_SECTION);
636 }
637 this->write(" }\n");
638}
639
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400640void CPPCodeGenerator::writeClone() {
641 if (!this->writeSection(CLONE_SECTION)) {
642 if (fSectionAndParameterHelper.getSection(FIELDS_SECTION)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700643 fErrors.error(0, "fragment processors with custom @fields must also have a custom"
644 "@clone");
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400645 }
646 this->writef("%s::%s(const %s& src)\n"
Ethan Nicholasabff9562017-10-09 10:54:08 -0400647 ": INHERITED(k%s_ClassID, src.optimizationFlags())", fFullName.c_str(),
648 fFullName.c_str(), fFullName.c_str(), fFullName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400649 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400650 if (param->fType == *fContext.fFragmentProcessor_Type) {
651 continue;
652 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700653 String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str());
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400654 this->writef("\n, %s(src.%s)",
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400655 fieldName.c_str(),
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400656 fieldName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400657 }
658 for (const Section* s : fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION)) {
659 String fieldName = HCodeGenerator::FieldName(s->fArgument.c_str());
660 this->writef("\n, %sCoordTransform(src.%sCoordTransform)", fieldName.c_str(),
661 fieldName.c_str());
662 }
Ethan Nicholasabff9562017-10-09 10:54:08 -0400663 this->writef(" {\n");
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400664 int childCount = 0;
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400665 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
666 if (param->fType.kind() == Type::kSampler_Kind) {
667 this->writef(" this->addTextureSampler(&%s);\n",
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700668 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400669 } else if (param->fType == *fContext.fFragmentProcessor_Type) {
670 this->writef(" this->registerChildProcessor(src.childProcessor(%d).clone());"
671 "\n", childCount++);
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400672 }
673 }
674 for (const Section* s : fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION)) {
675 String field = HCodeGenerator::FieldName(s->fArgument.c_str());
676 this->writef(" this->addCoordTransform(&%sCoordTransform);\n", field.c_str());
677 }
678 this->write("}\n");
Brian Salomonaff329b2017-08-11 09:40:37 -0400679 this->writef("std::unique_ptr<GrFragmentProcessor> %s::clone() const {\n",
680 fFullName.c_str());
681 this->writef(" return std::unique_ptr<GrFragmentProcessor>(new %s(*this));\n",
682 fFullName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400683 this->write("}\n");
684 }
685}
686
Ethan Nicholas762466e2017-06-29 10:03:38 -0400687void CPPCodeGenerator::writeTest() {
Ethan Nicholas68990be2017-07-13 09:36:52 -0400688 const Section* test = fSectionAndParameterHelper.getSection(TEST_CODE_SECTION);
689 if (test) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400690 this->writef(
691 "GR_DEFINE_FRAGMENT_PROCESSOR_TEST(%s);\n"
692 "#if GR_TEST_UTILS\n"
693 "std::unique_ptr<GrFragmentProcessor> %s::TestCreate(GrProcessorTestData* %s) {\n",
694 fFullName.c_str(),
695 fFullName.c_str(),
696 test->fArgument.c_str());
Ethan Nicholas68990be2017-07-13 09:36:52 -0400697 this->writeSection(TEST_CODE_SECTION);
698 this->write("}\n"
699 "#endif\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400700 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400701}
702
703void CPPCodeGenerator::writeGetKey() {
704 this->writef("void %s::onGetGLSLProcessorKey(const GrShaderCaps& caps, "
705 "GrProcessorKeyBuilder* b) const {\n",
706 fFullName.c_str());
Ethan Nicholas68990be2017-07-13 09:36:52 -0400707 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700708 String nameString(param->fName);
709 const char* name = nameString.c_str();
Ethan Nicholas762466e2017-06-29 10:03:38 -0400710 if (param->fModifiers.fLayout.fKey != Layout::kNo_Key &&
711 (param->fModifiers.fFlags & Modifiers::kUniform_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700712 fErrors.error(param->fOffset,
Ethan Nicholas762466e2017-06-29 10:03:38 -0400713 "layout(key) may not be specified on uniforms");
714 }
715 switch (param->fModifiers.fLayout.fKey) {
716 case Layout::kKey_Key:
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400717 if (param->fType == *fContext.fFloat4x4_Type) {
718 ABORT("no automatic key handling for float4x4\n");
719 } else if (param->fType == *fContext.fFloat2_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400720 this->writef(" b->add32(%s.fX);\n",
721 HCodeGenerator::FieldName(name).c_str());
722 this->writef(" b->add32(%s.fY);\n",
723 HCodeGenerator::FieldName(name).c_str());
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400724 } else if (param->fType == *fContext.fFloat4_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400725 this->writef(" b->add32(%s.x());\n",
726 HCodeGenerator::FieldName(name).c_str());
727 this->writef(" b->add32(%s.y());\n",
728 HCodeGenerator::FieldName(name).c_str());
729 this->writef(" b->add32(%s.width());\n",
730 HCodeGenerator::FieldName(name).c_str());
731 this->writef(" b->add32(%s.height());\n",
732 HCodeGenerator::FieldName(name).c_str());
733 } else {
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500734 this->writef(" b->add32((int32_t) %s);\n",
Ethan Nicholas762466e2017-06-29 10:03:38 -0400735 HCodeGenerator::FieldName(name).c_str());
736 }
737 break;
738 case Layout::kIdentity_Key:
739 if (param->fType.kind() != Type::kMatrix_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700740 fErrors.error(param->fOffset,
Ethan Nicholas762466e2017-06-29 10:03:38 -0400741 "layout(key=identity) requires matrix type");
742 }
743 this->writef(" b->add32(%s.isIdentity() ? 1 : 0);\n",
744 HCodeGenerator::FieldName(name).c_str());
745 break;
746 case Layout::kNo_Key:
747 break;
748 }
749 }
750 this->write("}\n");
751}
752
753bool CPPCodeGenerator::generateCode() {
754 std::vector<const Variable*> uniforms;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400755 for (const auto& p : fProgram) {
756 if (ProgramElement::kVar_Kind == p.fKind) {
757 const VarDeclarations& decls = (const VarDeclarations&) p;
758 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000759 VarDeclaration& decl = (VarDeclaration&) *raw;
760 if ((decl.fVar->fModifiers.fFlags & Modifiers::kUniform_Flag) &&
761 decl.fVar->fType.kind() != Type::kSampler_Kind) {
762 uniforms.push_back(decl.fVar);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400763 }
764 }
765 }
766 }
767 const char* baseName = fName.c_str();
768 const char* fullName = fFullName.c_str();
Ethan Nicholas130fb3f2018-02-01 12:14:34 -0500769 this->writef("%s\n", HCodeGenerator::GetHeader(fProgram, fErrors).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400770 this->writef(kFragmentProcessorHeader, fullName);
Greg Daniel3e8c3452018-04-06 10:37:55 -0400771 this->writef("#include \"%s.h\"\n", fullName);
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400772 this->writeSection(CPP_SECTION);
Brian Osman1cb41712017-10-19 12:54:52 -0400773 this->writef("#include \"glsl/GrGLSLFragmentProcessor.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400774 "#include \"glsl/GrGLSLFragmentShaderBuilder.h\"\n"
775 "#include \"glsl/GrGLSLProgramBuilder.h\"\n"
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500776 "#include \"GrTexture.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400777 "#include \"SkSLCPP.h\"\n"
778 "#include \"SkSLUtil.h\"\n"
779 "class GrGLSL%s : public GrGLSLFragmentProcessor {\n"
780 "public:\n"
781 " GrGLSL%s() {}\n",
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400782 baseName, baseName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400783 bool result = this->writeEmitCode(uniforms);
784 this->write("private:\n");
785 this->writeSetData(uniforms);
786 this->writePrivateVars();
787 for (const auto& u : uniforms) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400788 if (needs_uniform_var(*u) && !(u->fModifiers.fFlags & Modifiers::kIn_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700789 this->writef(" UniformHandle %sVar;\n",
790 HCodeGenerator::FieldName(String(u->fName).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400791 }
792 }
Ethan Nicholas68990be2017-07-13 09:36:52 -0400793 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400794 if (needs_uniform_var(*param)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700795 this->writef(" UniformHandle %sVar;\n",
796 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400797 }
798 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400799 this->writef("};\n"
800 "GrGLSLFragmentProcessor* %s::onCreateGLSLInstance() const {\n"
801 " return new GrGLSL%s();\n"
802 "}\n",
803 fullName, baseName);
804 this->writeGetKey();
805 this->writef("bool %s::onIsEqual(const GrFragmentProcessor& other) const {\n"
806 " const %s& that = other.cast<%s>();\n"
807 " (void) that;\n",
808 fullName, fullName, fullName);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400809 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400810 if (param->fType == *fContext.fFragmentProcessor_Type) {
811 continue;
812 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700813 String nameString(param->fName);
814 const char* name = nameString.c_str();
Ethan Nicholas762466e2017-06-29 10:03:38 -0400815 this->writef(" if (%s != that.%s) return false;\n",
816 HCodeGenerator::FieldName(name).c_str(),
817 HCodeGenerator::FieldName(name).c_str());
818 }
819 this->write(" return true;\n"
820 "}\n");
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400821 this->writeClone();
Ethan Nicholas762466e2017-06-29 10:03:38 -0400822 this->writeTest();
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400823 this->writeSection(CPP_END_SECTION);
Greg Daniel3e8c3452018-04-06 10:37:55 -0400824
Ethan Nicholas762466e2017-06-29 10:03:38 -0400825 result &= 0 == fErrors.errorCount();
826 return result;
827}
828
829} // namespace