• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "src/sksl/codegen/SkSLCPPCodeGenerator.h"
9 
10 #include "src/sksl/SkSLAnalysis.h"
11 #include "src/sksl/SkSLCPPUniformCTypes.h"
12 #include "src/sksl/SkSLCompiler.h"
13 #include "src/sksl/codegen/SkSLHCodeGenerator.h"
14 #include "src/sksl/ir/SkSLEnum.h"
15 
16 #include <algorithm>
17 
18 #if defined(SKSL_STANDALONE) || GR_TEST_UTILS
19 
20 namespace SkSL {
21 
needs_uniform_var(const Variable & var)22 static bool needs_uniform_var(const Variable& var) {
23     return (var.modifiers().fFlags & Modifiers::kUniform_Flag) &&
24             var.type().typeKind() != Type::TypeKind::kSampler;
25 }
26 
CPPCodeGenerator(const Context * context,const Program * program,ErrorReporter * errors,String name,OutputStream * out)27 CPPCodeGenerator::CPPCodeGenerator(const Context* context, const Program* program,
28                                    ErrorReporter* errors, String name, OutputStream* out)
29     : INHERITED(context, program, errors, out)
30     , fName(std::move(name))
31     , fFullName(String::printf("Gr%s", fName.c_str()))
32     , fSectionAndParameterHelper(program, *errors) {
33     fLineEnding = "\n";
34     fTextureFunctionOverride = "sample";
35 }
36 
writef(const char * s,va_list va)37 void CPPCodeGenerator::writef(const char* s, va_list va) {
38     static constexpr int BUFFER_SIZE = 1024;
39     va_list copy;
40     va_copy(copy, va);
41     char buffer[BUFFER_SIZE];
42     int length = std::vsnprintf(buffer, BUFFER_SIZE, s, va);
43     if (length < BUFFER_SIZE) {
44         fOut->write(buffer, length);
45     } else {
46         std::unique_ptr<char[]> heap(new char[length + 1]);
47         vsprintf(heap.get(), s, copy);
48         fOut->write(heap.get(), length);
49     }
50     va_end(copy);
51 }
52 
writef(const char * s,...)53 void CPPCodeGenerator::writef(const char* s, ...) {
54     va_list va;
55     va_start(va, s);
56     this->writef(s, va);
57     va_end(va);
58 }
59 
writeHeader()60 void CPPCodeGenerator::writeHeader() {
61 }
62 
usesPrecisionModifiers() const63 bool CPPCodeGenerator::usesPrecisionModifiers() const {
64     return false;
65 }
66 
getTypeName(const Type & type)67 String CPPCodeGenerator::getTypeName(const Type& type) {
68     return type.name();
69 }
70 
writeBinaryExpression(const BinaryExpression & b,Precedence parentPrecedence)71 void CPPCodeGenerator::writeBinaryExpression(const BinaryExpression& b,
72                                              Precedence parentPrecedence) {
73     const Expression& left = *b.left();
74     const Expression& right = *b.right();
75     Operator op = b.getOperator();
76     if (op.kind() == Token::Kind::TK_PERCENT) {
77         // need to use "%%" instead of "%" b/c the code will be inside of a printf
78         Precedence precedence = op.getBinaryPrecedence();
79         if (precedence >= parentPrecedence) {
80             this->write("(");
81         }
82         this->writeExpression(left, precedence);
83         this->write(" %% ");
84         this->writeExpression(right, precedence);
85         if (precedence >= parentPrecedence) {
86             this->write(")");
87         }
88     } else {
89         INHERITED::writeBinaryExpression(b, parentPrecedence);
90     }
91 }
92 
default_value(const Type & type)93 static String default_value(const Type& type) {
94     if (type.isBoolean()) {
95         return "false";
96     }
97     switch (type.typeKind()) {
98         case Type::TypeKind::kScalar: return "0";
99         case Type::TypeKind::kVector: return type.name() + "(0)";
100         case Type::TypeKind::kMatrix: return type.name() + "(1)";
101         default: SK_ABORT("unsupported default_value type");
102     }
103 }
104 
default_value(const Variable & var)105 static String default_value(const Variable& var) {
106     if (var.modifiers().fLayout.fCType == SkSL::Layout::CType::kSkPMColor4f) {
107         return "{SK_FloatNaN, SK_FloatNaN, SK_FloatNaN, SK_FloatNaN}";
108     }
109     return default_value(var.type());
110 }
111 
is_private(const Variable & var)112 static bool is_private(const Variable& var) {
113     const Modifiers& modifiers = var.modifiers();
114     return !(modifiers.fFlags & Modifiers::kUniform_Flag) &&
115            !(modifiers.fFlags & Modifiers::kIn_Flag) &&
116            var.storage() == Variable::Storage::kGlobal &&
117            modifiers.fLayout.fBuiltin == -1;
118 }
119 
is_uniform_in(const Variable & var)120 static bool is_uniform_in(const Variable& var) {
121     const Modifiers& modifiers = var.modifiers();
122     return (modifiers.fFlags & Modifiers::kUniform_Flag) &&
123            (modifiers.fFlags & Modifiers::kIn_Flag) &&
124            var.type().typeKind() != Type::TypeKind::kSampler;
125 }
126 
formatRuntimeValue(const Type & type,const Layout & layout,const String & cppCode,std::vector<String> * formatArgs)127 String CPPCodeGenerator::formatRuntimeValue(const Type& type,
128                                             const Layout& layout,
129                                             const String& cppCode,
130                                             std::vector<String>* formatArgs) {
131     if (type.isArray()) {
132         String result("[");
133         const char* separator = "";
134         for (int i = 0; i < type.columns(); i++) {
135             result += separator + this->formatRuntimeValue(type.componentType(), layout,
136                                                            "(" + cppCode + ")[" + to_string(i) +
137                                                            "]", formatArgs);
138             separator = ",";
139         }
140         result += "]";
141         return result;
142     }
143     if (type.isFloat()) {
144         formatArgs->push_back(cppCode);
145         return "%f";
146     }
147     if (type == *fContext.fTypes.fInt) {
148         formatArgs->push_back(cppCode);
149         return "%d";
150     }
151     if (type == *fContext.fTypes.fBool) {
152         formatArgs->push_back("(" + cppCode + " ? \"true\" : \"false\")");
153         return "%s";
154     }
155     if (type == *fContext.fTypes.fFloat2 || type == *fContext.fTypes.fHalf2) {
156         formatArgs->push_back(cppCode + ".fX");
157         formatArgs->push_back(cppCode + ".fY");
158         return type.name() + "(%f, %f)";
159     }
160     if (type == *fContext.fTypes.fFloat3 || type == *fContext.fTypes.fHalf3) {
161         formatArgs->push_back(cppCode + ".fX");
162         formatArgs->push_back(cppCode + ".fY");
163         formatArgs->push_back(cppCode + ".fZ");
164         return type.name() + "(%f, %f, %f)";
165     }
166     if (type == *fContext.fTypes.fFloat4 || type == *fContext.fTypes.fHalf4) {
167         switch (layout.fCType) {
168             case Layout::CType::kSkPMColor:
169                 formatArgs->push_back("SkGetPackedR32(" + cppCode + ") / 255.0");
170                 formatArgs->push_back("SkGetPackedG32(" + cppCode + ") / 255.0");
171                 formatArgs->push_back("SkGetPackedB32(" + cppCode + ") / 255.0");
172                 formatArgs->push_back("SkGetPackedA32(" + cppCode + ") / 255.0");
173                 break;
174             case Layout::CType::kSkPMColor4f:
175                 formatArgs->push_back(cppCode + ".fR");
176                 formatArgs->push_back(cppCode + ".fG");
177                 formatArgs->push_back(cppCode + ".fB");
178                 formatArgs->push_back(cppCode + ".fA");
179                 break;
180             case Layout::CType::kSkV4:
181                 formatArgs->push_back(cppCode + ".x");
182                 formatArgs->push_back(cppCode + ".y");
183                 formatArgs->push_back(cppCode + ".z");
184                 formatArgs->push_back(cppCode + ".w");
185                 break;
186             case Layout::CType::kSkRect:
187             case Layout::CType::kDefault:
188                 formatArgs->push_back(cppCode + ".left()");
189                 formatArgs->push_back(cppCode + ".top()");
190                 formatArgs->push_back(cppCode + ".right()");
191                 formatArgs->push_back(cppCode + ".bottom()");
192                 break;
193             default:
194                 SkASSERT(false);
195         }
196         return type.name() + "(%f, %f, %f, %f)";
197     }
198     if (type.isMatrix()) {
199         SkASSERT(type.componentType() == *fContext.fTypes.fFloat ||
200                  type.componentType() == *fContext.fTypes.fHalf);
201 
202         String format = type.name() + "(";
203         for (int c = 0; c < type.columns(); ++c) {
204             for (int r = 0; r < type.rows(); ++r) {
205                 formatArgs->push_back(String::printf("%s.rc(%d, %d)", cppCode.c_str(), r, c));
206                 format += "%f, ";
207             }
208         }
209 
210         // Replace trailing ", " with ")".
211         format.pop_back();
212         format.back() = ')';
213         return format;
214     }
215     if (type.isEnum()) {
216         formatArgs->push_back("(int) " + cppCode);
217         return "%d";
218     }
219     if (type == *fContext.fTypes.fInt4 ||
220         type == *fContext.fTypes.fShort4) {
221         formatArgs->push_back(cppCode + ".left()");
222         formatArgs->push_back(cppCode + ".top()");
223         formatArgs->push_back(cppCode + ".right()");
224         formatArgs->push_back(cppCode + ".bottom()");
225         return type.name() + "(%d, %d, %d, %d)";
226     }
227 
228     SkDEBUGFAILF("unsupported runtime value type '%s'\n", String(type.name()).c_str());
229     return "";
230 }
231 
writeRuntimeValue(const Type & type,const Layout & layout,const String & cppCode)232 void CPPCodeGenerator::writeRuntimeValue(const Type& type, const Layout& layout,
233                                          const String& cppCode) {
234     this->write(this->formatRuntimeValue(type, layout, cppCode, &fFormatArgs));
235 }
236 
writeVarInitializer(const Variable & var,const Expression & value)237 void CPPCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) {
238     if (is_private(var)) {
239         this->writeRuntimeValue(var.type(), var.modifiers().fLayout, var.name());
240     } else {
241         this->writeExpression(value, Precedence::kTopLevel);
242     }
243 }
244 
getSamplerHandle(const Variable & var)245 String CPPCodeGenerator::getSamplerHandle(const Variable& var) {
246     int samplerCount = 0;
247     for (const auto param : fSectionAndParameterHelper.getParameters()) {
248         if (&var == param) {
249             return "args.fTexSamplers[" + to_string(samplerCount) + "]";
250         }
251         if (param->type().typeKind() == Type::TypeKind::kSampler) {
252             ++samplerCount;
253         }
254     }
255     SK_ABORT("should have found sampler in parameters\n");
256 }
257 
writeIntLiteral(const IntLiteral & i)258 void CPPCodeGenerator::writeIntLiteral(const IntLiteral& i) {
259     this->write(to_string(i.value()));
260 }
261 
writeSwizzle(const Swizzle & swizzle)262 void CPPCodeGenerator::writeSwizzle(const Swizzle& swizzle) {
263     if (fCPPMode) {
264         // no support for multiple swizzle components yet
265         SkASSERT(swizzle.components().size() == 1);
266         this->writeExpression(*swizzle.base(), Precedence::kPostfix);
267         switch (swizzle.components()[0]) {
268             case 0: this->write(".left()");   break;
269             case 1: this->write(".top()");    break;
270             case 2: this->write(".right()");  break;
271             case 3: this->write(".bottom()"); break;
272         }
273     } else {
274         INHERITED::writeSwizzle(swizzle);
275     }
276 }
277 
writeVariableReference(const VariableReference & ref)278 void CPPCodeGenerator::writeVariableReference(const VariableReference& ref) {
279     if (fCPPMode) {
280         this->write(ref.variable()->name());
281         return;
282     }
283     switch (ref.variable()->modifiers().fLayout.fBuiltin) {
284         case SK_MAIN_COORDS_BUILTIN:
285             this->write("%s");
286             fFormatArgs.push_back(String("args.fSampleCoord"));
287             fAccessSampleCoordsDirectly = true;
288             break;
289         default:
290             const Variable& var = *ref.variable();
291             if (var.type().typeKind() == Type::TypeKind::kSampler) {
292                 this->write("%s");
293                 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerVariable(" +
294                                       this->getSamplerHandle(*ref.variable()) + ")");
295                 return;
296             }
297             if (var.modifiers().fFlags & Modifiers::kUniform_Flag) {
298                 this->write("%s");
299                 String name = var.name();
300                 String varCode = String::printf("args.fUniformHandler->getUniformCStr(%sVar)",
301                                                 HCodeGenerator::FieldName(name.c_str()).c_str());
302                 String code;
303                 if (var.modifiers().fLayout.fWhen.fLength) {
304                     code = String::printf("%sVar.isValid() ? %s : \"%s\"",
305                                           HCodeGenerator::FieldName(name.c_str()).c_str(),
306                                           varCode.c_str(),
307                                           default_value(var.type()).c_str());
308                 } else {
309                     code = varCode;
310                 }
311                 fFormatArgs.push_back(code);
312             } else if (SectionAndParameterHelper::IsParameter(var)) {
313                 String name(var.name());
314                 this->writeRuntimeValue(var.type(), var.modifiers().fLayout,
315                                         String::printf("_outer.%s", name.c_str()).c_str());
316             } else {
317                 this->write(var.name());
318             }
319     }
320 }
321 
writeIfStatement(const IfStatement & s)322 void CPPCodeGenerator::writeIfStatement(const IfStatement& s) {
323     if (s.isStatic()) {
324         this->write("@");
325     }
326     INHERITED::writeIfStatement(s);
327 }
328 
writeReturnStatement(const ReturnStatement & s)329 void CPPCodeGenerator::writeReturnStatement(const ReturnStatement& s) {
330     INHERITED::writeReturnStatement(s);
331 }
332 
writeSwitchStatement(const SwitchStatement & s)333 void CPPCodeGenerator::writeSwitchStatement(const SwitchStatement& s) {
334     if (s.isStatic()) {
335         this->write("@");
336     }
337     INHERITED::writeSwitchStatement(s);
338 }
339 
getChildFPIndex(const Variable & var) const340 int CPPCodeGenerator::getChildFPIndex(const Variable& var) const {
341     int index = 0;
342     for (const ProgramElement* p : fProgram.elements()) {
343         if (p->is<GlobalVarDeclaration>()) {
344             const VarDeclaration& decl =
345                                   p->as<GlobalVarDeclaration>().declaration()->as<VarDeclaration>();
346             if (&decl.var() == &var) {
347                 return index;
348             } else if (decl.var().type().isFragmentProcessor()) {
349                 ++index;
350             }
351         }
352     }
353     SkDEBUGFAIL("child fragment processor not found");
354     return 0;
355 }
356 
getSampleVarName(const char * prefix,int sampleCounter)357 String CPPCodeGenerator::getSampleVarName(const char* prefix, int sampleCounter) {
358     return String::printf("%s%d", prefix, sampleCounter);
359 }
360 
writeFunctionCall(const FunctionCall & c)361 void CPPCodeGenerator::writeFunctionCall(const FunctionCall& c) {
362     const FunctionDeclaration& function = c.function();
363     const ExpressionArray& arguments = c.arguments();
364     if (function.isBuiltin() && function.name() == "sample" &&
365         arguments[0]->type().typeKind() != Type::TypeKind::kSampler) {
366         int sampleCounter = fSampleCounter++;
367 
368         // Validity checks that are detected by function definition in sksl_fp.inc
369         SkASSERT(arguments.size() >= 1 && arguments.size() <= 3);
370         SkASSERT(arguments[0]->type().isFragmentProcessor());
371 
372         // Actually fail during compilation if arguments with valid types are
373         // provided that are not variable references, since sample() is a
374         // special function that impacts code emission.
375         if (!arguments[0]->is<VariableReference>()) {
376             fErrors.error(arguments[0]->fOffset,
377                     "sample()'s fragmentProcessor argument must be a variable reference\n");
378             return;
379         }
380         const Variable& child = *arguments[0]->as<VariableReference>().variable();
381 
382         // Start a new extra emit code section so that the emitted child processor can depend on
383         // sksl variables defined in earlier sksl code.
384         this->newExtraEmitCodeBlock();
385 
386         // inputColor is an optional argument that always appears last
387         String inputColor;
388         if (arguments.back()->type().name() == "half4") {
389             // Use the invokeChild() variant that accepts an input color, so convert the 2nd
390             // argument's expression into C++ code that produces sksl stored in an SkString.
391             String inputColorName = this->getSampleVarName("_input", sampleCounter);
392             addExtraEmitCodeLine(convertSKSLExpressionToCPP(*arguments.back(), inputColorName));
393 
394             // invokeChild() needs a char* and a pre-pended comma
395             inputColor = ", " + inputColorName + ".c_str()";
396         }
397 
398         // If coords are present, they're float2. They appear right after the fp.
399         String inputCoord;
400         if (arguments.size() > 1) {
401             if (arguments[1]->type().name() == "float2") {
402                 // Invoking child with explicit coordinates at this call site
403                 inputCoord = this->getSampleVarName("_coords", sampleCounter);
404                 addExtraEmitCodeLine(convertSKSLExpressionToCPP(*arguments[1], inputCoord));
405                 inputCoord.append(".c_str()");
406             }
407         }
408         if (!inputCoord.empty()) {
409             inputCoord = ", " + inputCoord;
410         }
411 
412         // Write the output handling after the possible input handling
413         String childName = this->getSampleVarName("_sample", sampleCounter);
414         String childIndexStr = to_string(this->getChildFPIndex(child));
415         addExtraEmitCodeLine("SkString " + childName + " = this->invokeChild(" + childIndexStr +
416                              inputColor + ", args" + inputCoord + ");");
417 
418         this->write("%s");
419         fFormatArgs.push_back(childName + ".c_str()");
420         return;
421     }
422     if (function.isBuiltin()) {
423         INHERITED::writeFunctionCall(c);
424     } else {
425         this->write("%s");
426         fFormatArgs.push_back((String(function.name()) + "_name.c_str()").c_str());
427         this->write("(");
428         const char* separator = "";
429         for (const auto& arg : arguments) {
430             this->write(separator);
431             separator = ", ";
432             this->writeExpression(*arg, Precedence::kSequence);
433         }
434         this->write(")");
435     }
436     if (function.isBuiltin() && function.name() == "sample") {
437         this->write(".%s");
438         SkASSERT(arguments.size() >= 1);
439         SkASSERT(arguments[0]->is<VariableReference>());
440         String sampler =
441                 this->getSamplerHandle(*arguments[0]->as<VariableReference>().variable());
442         fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerSwizzle(" + sampler +
443                               ").asString().c_str()");
444     }
445 }
446 
glsltype_string(const Context & context,const Type & type)447 static const char* glsltype_string(const Context& context, const Type& type) {
448     // If a new GrSL type is added, this function will need to be updated.
449     static_assert(kGrSLTypeCount == 49);
450 
451     if (type == *context.fTypes.fVoid    ) { return "kVoid_GrSLType";     }
452     if (type == *context.fTypes.fBool    ) { return "kBool_GrSLType";     }
453     if (type == *context.fTypes.fBool2   ) { return "kBool2_GrSLType";    }
454     if (type == *context.fTypes.fBool3   ) { return "kBool3_GrSLType";    }
455     if (type == *context.fTypes.fBool4   ) { return "kBool4_GrSLType";    }
456     if (type == *context.fTypes.fShort   ) { return "kShort_GrSLType";    }
457     if (type == *context.fTypes.fShort2  ) { return "kShort2_GrSLType";   }
458     if (type == *context.fTypes.fShort3  ) { return "kShort3_GrSLType";   }
459     if (type == *context.fTypes.fShort4  ) { return "kShort4_GrSLType";   }
460     if (type == *context.fTypes.fUShort  ) { return "kUShort_GrSLType";   }
461     if (type == *context.fTypes.fUShort2 ) { return "kUShort2_GrSLType";  }
462     if (type == *context.fTypes.fUShort3 ) { return "kUShort3_GrSLType";  }
463     if (type == *context.fTypes.fUShort4 ) { return "kUShort4_GrSLType";  }
464     if (type == *context.fTypes.fFloat   ) { return "kFloat_GrSLType";    }
465     if (type == *context.fTypes.fFloat2  ) { return "kFloat2_GrSLType";   }
466     if (type == *context.fTypes.fFloat3  ) { return "kFloat3_GrSLType";   }
467     if (type == *context.fTypes.fFloat4  ) { return "kFloat4_GrSLType";   }
468     if (type == *context.fTypes.fFloat2x2) { return "kFloat2x2_GrSLType"; }
469     if (type == *context.fTypes.fFloat3x3) { return "kFloat3x3_GrSLType"; }
470     if (type == *context.fTypes.fFloat4x4) { return "kFloat4x4_GrSLType"; }
471     if (type == *context.fTypes.fHalf    ) { return "kHalf_GrSLType";     }
472     if (type == *context.fTypes.fHalf2   ) { return "kHalf2_GrSLType";    }
473     if (type == *context.fTypes.fHalf3   ) { return "kHalf3_GrSLType";    }
474     if (type == *context.fTypes.fHalf4   ) { return "kHalf4_GrSLType";    }
475     if (type == *context.fTypes.fHalf2x2 ) { return "kHalf2x2_GrSLType";  }
476     if (type == *context.fTypes.fHalf3x3 ) { return "kHalf3x3_GrSLType";  }
477     if (type == *context.fTypes.fHalf4x4 ) { return "kHalf4x4_GrSLType";  }
478     if (type == *context.fTypes.fInt     ) { return "kInt_GrSLType";      }
479     if (type == *context.fTypes.fInt2    ) { return "kInt2_GrSLType";     }
480     if (type == *context.fTypes.fInt3    ) { return "kInt3_GrSLType";     }
481     if (type == *context.fTypes.fInt4    ) { return "kInt4_GrSLType";     }
482     if (type == *context.fTypes.fUInt    ) { return "kUint_GrSLType";     }
483     if (type == *context.fTypes.fUInt2   ) { return "kUint2_GrSLType";    }
484     if (type == *context.fTypes.fUInt3   ) { return "kUint3_GrSLType";    }
485     if (type == *context.fTypes.fUInt4   ) { return "kUint4_GrSLType";    }
486     if (type.isEnum())                     { return "kInt_GrSLType";      }
487 
488     SkDEBUGFAILF("unsupported type: %s", type.description().c_str());
489     return nullptr;
490 }
491 
prepareHelperFunction(const FunctionDeclaration & decl)492 void CPPCodeGenerator::prepareHelperFunction(const FunctionDeclaration& decl) {
493     if (decl.isBuiltin() || decl.isMain()) {
494         return;
495     }
496 
497     String funcName = decl.name();
498     this->addExtraEmitCodeLine(
499             String::printf("SkString %s_name = fragBuilder->getMangledFunctionName(\"%s\");",
500                            funcName.c_str(),
501                            funcName.c_str()));
502 
503     String args = String::printf("const GrShaderVar %s_args[] = { ", funcName.c_str());
504     const char* separator = "";
505     for (const Variable* param : decl.parameters()) {
506         String paramName = param->name();
507         args.appendf("%sGrShaderVar(\"%s\", %s)", separator, paramName.c_str(),
508                                                   glsltype_string(fContext, param->type()));
509         separator = ", ";
510     }
511     args += " };";
512 
513     this->addExtraEmitCodeLine(args.c_str());
514 }
515 
prototypeHelperFunction(const FunctionDeclaration & decl)516 void CPPCodeGenerator::prototypeHelperFunction(const FunctionDeclaration& decl) {
517     String funcName = decl.name();
518     this->addExtraEmitCodeLine(String::printf(
519             "fragBuilder->emitFunctionPrototype(%s, %s_name.c_str(), {%s_args, %zu});",
520             glsltype_string(fContext, decl.returnType()),
521             funcName.c_str(),
522             funcName.c_str(),
523             decl.parameters().size()));
524 }
525 
writeFunction(const FunctionDefinition & f)526 void CPPCodeGenerator::writeFunction(const FunctionDefinition& f) {
527     const FunctionDeclaration& decl = f.declaration();
528     if (decl.isBuiltin()) {
529         return;
530     }
531     fFunctionHeader.clear();
532     OutputStream* oldOut = fOut;
533     StringStream buffer;
534     fOut = &buffer;
535     if (decl.isMain()) {
536         fInMain = true;
537         for (const std::unique_ptr<Statement>& s : f.body()->as<Block>().children()) {
538             this->writeStatement(*s);
539             this->writeLine();
540         }
541         fInMain = false;
542 
543         fOut = oldOut;
544         this->write(fFunctionHeader);
545         this->write(buffer.str());
546     } else {
547         for (const std::unique_ptr<Statement>& s : f.body()->as<Block>().children()) {
548             this->writeStatement(*s);
549             this->writeLine();
550         }
551 
552         fOut = oldOut;
553         String funcName = decl.name();
554 
555         String funcImpl;
556         if (!fFormatArgs.empty()) {
557             this->addExtraEmitCodeLine("const String " + funcName + "_impl = String::printf(" +
558                                        assembleCodeAndFormatArgPrintf(buffer.str()).c_str() + ");");
559             funcImpl = String::printf(" %s_impl.c_str()", funcName.c_str());
560         } else {
561             funcImpl = "\nR\"SkSL(" + buffer.str() + ")SkSL\"";
562         }
563 
564         this->addExtraEmitCodeLine(String::printf(
565                 "fragBuilder->emitFunction(%s, %s_name.c_str(), {%s_args, %zu},%s);",
566                 glsltype_string(fContext, decl.returnType()),
567                 funcName.c_str(),
568                 funcName.c_str(),
569                 decl.parameters().size(),
570                 funcImpl.c_str()));
571     }
572 }
573 
writeSetting(const Setting & s)574 void CPPCodeGenerator::writeSetting(const Setting& s) {
575     this->writef("sk_Caps.%s", s.name().c_str());
576 }
577 
writeSection(const char * name,const char * prefix)578 bool CPPCodeGenerator::writeSection(const char* name, const char* prefix) {
579     const Section* s = fSectionAndParameterHelper.getSection(name);
580     if (s) {
581         this->writef("%s%s", prefix, s->text().c_str());
582         return true;
583     }
584     return false;
585 }
586 
writeProgramElement(const ProgramElement & p)587 void CPPCodeGenerator::writeProgramElement(const ProgramElement& p) {
588     switch (p.kind()) {
589         case ProgramElement::Kind::kSection:
590             return;
591         case ProgramElement::Kind::kGlobalVar: {
592             const GlobalVarDeclaration& decl = p.as<GlobalVarDeclaration>();
593             const Variable& var = decl.declaration()->as<VarDeclaration>().var();
594             if (var.modifiers().fFlags & (Modifiers::kIn_Flag | Modifiers::kUniform_Flag) ||
595                 -1 != var.modifiers().fLayout.fBuiltin) {
596                 return;
597             }
598             break;
599         }
600         case ProgramElement::Kind::kFunctionPrototype: {
601             // Function prototypes are handled at the C++ level (in writeEmitCode).
602             // We don't want prototypes to be emitted inside the FP's main() function.
603             return;
604         }
605         default:
606             break;
607     }
608     INHERITED::writeProgramElement(p);
609 }
610 
addUniform(const Variable & var)611 void CPPCodeGenerator::addUniform(const Variable& var) {
612     if (!needs_uniform_var(var)) {
613         return;
614     }
615     if (var.modifiers().fLayout.fWhen.fLength) {
616         this->writef("        if (%s) {\n    ", String(var.modifiers().fLayout.fWhen).c_str());
617     }
618     String name(var.name());
619     if (!var.type().isArray()) {
620         this->writef("        %sVar = args.fUniformHandler->addUniform(&_outer, "
621                      "kFragment_GrShaderFlag, %s, \"%s\");\n",
622                      HCodeGenerator::FieldName(name.c_str()).c_str(),
623                      glsltype_string(fContext, var.type()),
624                      name.c_str());
625     } else {
626         this->writef("        %sVar = args.fUniformHandler->addUniformArray(&_outer, "
627                      "kFragment_GrShaderFlag, %s, \"%s\", %d);\n",
628                      HCodeGenerator::FieldName(name.c_str()).c_str(),
629                      glsltype_string(fContext, var.type().componentType()),
630                      name.c_str(),
631                      var.type().columns());
632     }
633     if (var.modifiers().fLayout.fWhen.fLength) {
634         this->write("        }\n");
635     }
636 }
637 
writeInputVars()638 void CPPCodeGenerator::writeInputVars() {
639 }
640 
writePrivateVars()641 void CPPCodeGenerator::writePrivateVars() {
642     for (const ProgramElement* p : fProgram.elements()) {
643         if (p->is<GlobalVarDeclaration>()) {
644             const GlobalVarDeclaration& global = p->as<GlobalVarDeclaration>();
645             const Variable& var = global.declaration()->as<VarDeclaration>().var();
646             if (is_private(var)) {
647                 if (var.type().isFragmentProcessor()) {
648                     fErrors.error(global.fOffset,
649                                   "fragmentProcessor variables must be declared 'in'");
650                     return;
651                 }
652                 this->writef("%s %s = %s;\n",
653                              HCodeGenerator::FieldType(fContext, var.type(),
654                                                        var.modifiers().fLayout).c_str(),
655                              String(var.name()).c_str(),
656                              default_value(var).c_str());
657             } else if (var.modifiers().fLayout.fFlags & Layout::kTracked_Flag) {
658                 // An auto-tracked uniform in variable, so add a field to hold onto the prior
659                 // state. Note that tracked variables must be uniform in's and that is validated
660                 // before writePrivateVars() is called.
661                 const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, var);
662                 SkASSERT(mapper);
663 
664                 String name = HCodeGenerator::FieldName(String(var.name()).c_str());
665                 // The member statement is different if the mapper reports a default value
666                 if (mapper->defaultValue().size() > 0) {
667                     this->writef("%s %sPrev = %s;\n",
668                                     Layout::CTypeToStr(mapper->ctype()), name.c_str(),
669                                     mapper->defaultValue().c_str());
670                 } else {
671                     this->writef("%s %sPrev;\n",
672                                     Layout::CTypeToStr(mapper->ctype()), name.c_str());
673                 }
674             }
675         }
676     }
677 }
678 
writePrivateVarValues()679 void CPPCodeGenerator::writePrivateVarValues() {
680     for (const ProgramElement* p : fProgram.elements()) {
681         if (p->is<GlobalVarDeclaration>()) {
682             const GlobalVarDeclaration& global = p->as<GlobalVarDeclaration>();
683             const VarDeclaration& decl = global.declaration()->as<VarDeclaration>();
684             if (is_private(decl.var()) && decl.value()) {
685                 this->writef("%s = ", String(decl.var().name()).c_str());
686                 fCPPMode = true;
687                 this->writeExpression(*decl.value(), Precedence::kAssignment);
688                 fCPPMode = false;
689                 this->write(";\n");
690             }
691         }
692     }
693 }
694 
is_accessible(const Variable & var)695 static bool is_accessible(const Variable& var) {
696     const Type& type = var.type();
697     return !type.isFragmentProcessor() &&
698            Type::TypeKind::kSampler != type.typeKind() &&
699            Type::TypeKind::kOther != type.typeKind();
700 }
701 
newExtraEmitCodeBlock()702 void CPPCodeGenerator::newExtraEmitCodeBlock() {
703     // This should only be called when emitting SKSL for emitCode(), which can be detected if the
704     // cpp buffer is not null, and the cpp buffer is not the current output.
705     SkASSERT(fCPPBuffer && fCPPBuffer != fOut);
706 
707     // Start a new block as an empty string
708     fExtraEmitCodeBlocks.push_back("");
709     // Mark its location in the output buffer, uses ${\d} for the token since ${} will not occur in
710     // valid sksl and makes detection trivial.
711     this->writef("${%zu}", fExtraEmitCodeBlocks.size() - 1);
712 }
713 
addExtraEmitCodeLine(const String & toAppend)714 void CPPCodeGenerator::addExtraEmitCodeLine(const String& toAppend) {
715     SkASSERT(fExtraEmitCodeBlocks.size() > 0);
716     String& currentBlock = fExtraEmitCodeBlocks[fExtraEmitCodeBlocks.size() - 1];
717     // Automatically add indentation and newline
718     currentBlock += "        " + toAppend + "\n";
719 }
720 
flushEmittedCode()721 void CPPCodeGenerator::flushEmittedCode() {
722     if (fCPPBuffer == nullptr) {
723         // Not actually within writeEmitCode() so nothing to flush
724         return;
725     }
726 
727     StringStream* skslBuffer = static_cast<StringStream*>(fOut);
728 
729     String sksl = skslBuffer->str();
730     // Empty the accumulation buffer since its current contents are consumed.
731     skslBuffer->reset();
732 
733     // Switch to the cpp buffer
734     fOut = fCPPBuffer;
735 
736     // Iterate through the sksl, keeping track of where the last statement ended (e.g. the latest
737     // encountered ';', '{', or '}'). If an extra emit code block token is encountered then the
738     // code from 0 to last statement end is sent to writeCodeAppend, the extra code block is
739     // appended to the cpp buffer, and then the sksl string is trimmed to start where the last
740     // statement left off (minus the encountered token).
741     size_t i = 0;
742     int flushPoint = -1;
743     int tokenStart = -1;
744     while (i < sksl.size()) {
745         if (tokenStart >= 0) {
746             // Looking for the end of the token
747             if (sksl[i] == '}') {
748                 // Must append the sksl from 0 to flushPoint (inclusive) then the extra code
749                 // accumulated in the block with index parsed from chars [tokenStart+2, i-1]
750                 String toFlush = String(sksl.c_str(), flushPoint + 1);
751                 // writeCodeAppend automatically removes the format args that it consumed, so
752                 // fFormatArgs will be in a valid state for any future sksl
753                 this->writeCodeAppend(toFlush);
754 
755                 SKSL_INT codeBlock;
756                 SkAssertResult(
757                         stoi(StringFragment(sksl.c_str() + tokenStart + 2, i - tokenStart - 2),
758                              &codeBlock));
759                 SkASSERT((size_t)codeBlock < fExtraEmitCodeBlocks.size());
760                 if (fExtraEmitCodeBlocks[codeBlock].size() > 0) {
761                     this->write(fExtraEmitCodeBlocks[codeBlock].c_str());
762                 }
763 
764                 // Now reset the sksl buffer to start after the flush point, but remove the token.
765                 String compacted = String(sksl.c_str() + flushPoint + 1,
766                                           tokenStart - flushPoint - 1);
767                 if (i < sksl.size() - 1) {
768                     compacted += String(sksl.c_str() + i + 1, sksl.size() - i - 1);
769                 }
770                 sksl = compacted;
771 
772                 // And reset iteration
773                 i = -1;
774                 flushPoint = -1;
775                 tokenStart = -1;
776             }
777         } else {
778             // Looking for the start of extra emit block tokens, and tracking when statements end
779             if (sksl[i] == ';' || sksl[i] == '{' || sksl[i] == '}') {
780                 flushPoint = i;
781             } else if (i < sksl.size() - 1 && sksl[i] == '$' && sksl[i + 1] == '{') {
782                 // found an extra emit code block token
783                 tokenStart = i++;
784             }
785         }
786         i++;
787     }
788 
789     // Once we've gone through the sksl string to this point, there are no remaining extra emit
790     // code blocks to interleave, so append the remainder as usual.
791     this->writeCodeAppend(sksl);
792 
793     // After appending, switch back to the emptied sksl buffer and reset the extra code blocks
794     fOut = skslBuffer;
795     fExtraEmitCodeBlocks.clear();
796 }
797 
assembleCodeAndFormatArgPrintf(const String & code)798 String CPPCodeGenerator::assembleCodeAndFormatArgPrintf(const String& code) {
799     // Count % format specifiers.
800     size_t argCount = 0;
801     for (size_t index = 0; index < code.size(); ++index) {
802         if ('%' == code[index]) {
803             if (index == code.size() - 1) {
804                 SkDEBUGFAIL("found a dangling format specifier at the end of a string");
805                 break;
806             }
807             if (code[index + 1] == '%') {
808                 // %% indicates a literal % sign, not a format argument. Skip over the next
809                 // character to avoid mistakenly counting that one as an argument.
810                 ++index;
811             } else {
812                 // Count the format argument that we found.
813                 ++argCount;
814             }
815         }
816     }
817 
818     // Assemble the printf arguments.
819     String result = String::printf("R\"SkSL(%s)SkSL\"\n", code.c_str());
820     for (size_t i = 0; i < argCount; ++i) {
821         result += ", ";
822         result += fFormatArgs[i].c_str();
823     }
824 
825     // argCount is equal to the number of fFormatArgs that were consumed, so they should be
826     // removed from the list.
827     if (argCount > 0) {
828         fFormatArgs.erase(fFormatArgs.begin(), fFormatArgs.begin() + argCount);
829     }
830 
831     return result;
832 }
833 
writeCodeAppend(const String & code)834 void CPPCodeGenerator::writeCodeAppend(const String& code) {
835     if (!code.empty()) {
836         this->write("        fragBuilder->codeAppendf(\n");
837         this->write(assembleCodeAndFormatArgPrintf(code));
838         this->write(");\n");
839     }
840 }
841 
convertSKSLExpressionToCPP(const Expression & e,const String & cppVar)842 String CPPCodeGenerator::convertSKSLExpressionToCPP(const Expression& e,
843                                                     const String& cppVar) {
844     // To do this conversion, we temporarily switch the sksl output stream
845     // to an empty stringstream and reset the format args to empty.
846     OutputStream* oldSKSL = fOut;
847     StringStream exprBuffer;
848     fOut = &exprBuffer;
849 
850     std::vector<String> oldArgs(fFormatArgs);
851     fFormatArgs.clear();
852 
853     // Convert the argument expression into a format string and args
854     this->writeExpression(e, Precedence::kTopLevel);
855     std::vector<String> newArgs(fFormatArgs);
856     String expr = exprBuffer.str();
857 
858     // After generating, restore the original output stream and format args
859     fFormatArgs = oldArgs;
860     fOut = oldSKSL;
861 
862     // The sksl written to exprBuffer is not processed by flushEmittedCode(), so any extra emit code
863     // block tokens won't get handled. So we need to strip them from the expression and stick them
864     // to the end of the original sksl stream.
865     String exprFormat = "";
866     int tokenStart = -1;
867     for (size_t i = 0; i < expr.size(); i++) {
868         if (tokenStart >= 0) {
869             if (expr[i] == '}') {
870                 // End of the token, so append the token to fOut
871                 fOut->write(expr.c_str() + tokenStart, i - tokenStart + 1);
872                 tokenStart = -1;
873             }
874         } else {
875             if (i < expr.size() - 1 && expr[i] == '$' && expr[i + 1] == '{') {
876                 tokenStart = i++;
877             } else {
878                 exprFormat += expr[i];
879             }
880         }
881     }
882 
883     // Now build the final C++ code snippet from the format string and args
884     String cppExpr;
885     if (newArgs.empty()) {
886         // This was a static expression, so we can simplify the input
887         // color declaration in the emitted code to just a static string
888         cppExpr = "SkString " + cppVar + "(\"" + exprFormat + "\");";
889     } else if (newArgs.size() == 1 && exprFormat == "%s") {
890         // If the format expression is simply "%s", we can avoid an expensive call to printf.
891         // This happens fairly often in codegen so it is worth simplifying.
892         cppExpr = "SkString " + cppVar + "(" + newArgs[0] + ");";
893     } else {
894         // String formatting must occur dynamically, so have the C++ declaration
895         // use SkStringPrintf with the format args that were accumulated
896         // when the expression was written.
897         cppExpr = "SkString " + cppVar + " = SkStringPrintf(\"" + exprFormat + "\"";
898         for (size_t i = 0; i < newArgs.size(); i++) {
899             cppExpr += ", " + newArgs[i];
900         }
901         cppExpr += ");";
902     }
903     return cppExpr;
904 }
905 
writeEmitCode(std::vector<const Variable * > & uniforms)906 bool CPPCodeGenerator::writeEmitCode(std::vector<const Variable*>& uniforms) {
907     this->write("    void emitCode(EmitArgs& args) override {\n"
908                 "        GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;\n");
909     this->writef("        const %s& _outer = args.fFp.cast<%s>();\n"
910                  "        (void) _outer;\n",
911                  fFullName.c_str(), fFullName.c_str());
912     for (const ProgramElement* p : fProgram.elements()) {
913         if (p->is<GlobalVarDeclaration>()) {
914             const GlobalVarDeclaration& global = p->as<GlobalVarDeclaration>();
915             const VarDeclaration& decl = global.declaration()->as<VarDeclaration>();
916             String nameString(decl.var().name());
917             const char* name = nameString.c_str();
918             if (SectionAndParameterHelper::IsParameter(decl.var()) &&
919                 is_accessible(decl.var())) {
920                 this->writef("        auto %s = _outer.%s;\n"
921                              "        (void) %s;\n",
922                              name, name, name);
923             }
924         }
925     }
926     this->writePrivateVarValues();
927     for (const auto u : uniforms) {
928         this->addUniform(*u);
929     }
930     this->writeSection(kEmitCodeSection);
931 
932     // Save original buffer as the CPP buffer for flushEmittedCode()
933     fCPPBuffer = fOut;
934     StringStream skslBuffer;
935     fOut = &skslBuffer;
936 
937     this->newExtraEmitCodeBlock();
938 
939     // Generate mangled names and argument lists for helper functions.
940     std::unordered_set<const FunctionDeclaration*> definedHelpers;
941     for (const ProgramElement* p : fProgram.elements()) {
942         if (p->is<FunctionDefinition>()) {
943             const FunctionDeclaration* decl = &p->as<FunctionDefinition>().declaration();
944             definedHelpers.insert(decl);
945             this->prepareHelperFunction(*decl);
946         }
947     }
948 
949     // Emit prototypes for defined helper functions that originally had prototypes in the FP file.
950     // (If a function was prototyped but never defined, we skip it, since it wasn't prepared above.)
951     for (const ProgramElement* p : fProgram.elements()) {
952         if (p->is<FunctionPrototype>()) {
953             const FunctionDeclaration* decl = &p->as<FunctionPrototype>().declaration();
954             if (definedHelpers.find(decl) != definedHelpers.end()) {
955                 this->prototypeHelperFunction(*decl);
956             }
957         }
958     }
959 
960     bool result = INHERITED::generateCode();
961     this->flushEmittedCode();
962 
963     // Then restore the original CPP buffer and close the function
964     fOut = fCPPBuffer;
965     fCPPBuffer = nullptr;
966     this->write("    }\n");
967     return result;
968 }
969 
writeSetData(std::vector<const Variable * > & uniforms)970 void CPPCodeGenerator::writeSetData(std::vector<const Variable*>& uniforms) {
971     const char* fullName = fFullName.c_str();
972     const Section* section = fSectionAndParameterHelper.getSection(kSetDataSection);
973     const char* pdman = section ? section->argument().c_str() : "pdman";
974     this->writef("    void onSetData(const GrGLSLProgramDataManager& %s, "
975                                     "const GrFragmentProcessor& _proc) override {\n",
976                  pdman);
977     bool wroteProcessor = false;
978     for (const Variable* u : uniforms) {
979         if (is_uniform_in(*u)) {
980             if (!wroteProcessor) {
981                 this->writef("        const %s& _outer = _proc.cast<%s>();\n", fullName, fullName);
982                 wroteProcessor = true;
983                 this->writef("        {\n");
984             }
985 
986             const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *u);
987             SkASSERT(mapper);
988 
989             String nameString(u->name());
990             const char* name = nameString.c_str();
991 
992             // Switches for setData behavior in the generated code
993             bool conditionalUniform = u->modifiers().fLayout.fWhen != "";
994             bool isTracked = u->modifiers().fLayout.fFlags & Layout::kTracked_Flag;
995             bool needsValueDeclaration = isTracked || !mapper->canInlineUniformValue();
996 
997             String uniformName = HCodeGenerator::FieldName(name) + "Var";
998 
999             String indent = "        "; // 8 by default, 12 when nested for conditional uniforms
1000             if (conditionalUniform) {
1001                 // Add a pre-check to make sure the uniform was emitted
1002                 // before trying to send any data to the GPU
1003                 this->writef("        if (%s.isValid()) {\n", uniformName.c_str());
1004                 indent += "    ";
1005             }
1006 
1007             String valueVar = "";
1008             if (needsValueDeclaration) {
1009                 valueVar.appendf("%sValue", name);
1010                 // Use AccessType since that will match the return type of _outer's public API.
1011                 String valueType = HCodeGenerator::AccessType(fContext, u->type(),
1012                                                               u->modifiers().fLayout);
1013                 this->writef("%s%s %s = _outer.%s;\n",
1014                              indent.c_str(), valueType.c_str(), valueVar.c_str(), name);
1015             } else {
1016                 // Not tracked and the mapper only needs to use the value once
1017                 // so send it a safe expression instead of the variable name
1018                 valueVar.appendf("(_outer.%s)", name);
1019             }
1020 
1021             if (isTracked) {
1022                 String prevVar = HCodeGenerator::FieldName(name) + "Prev";
1023                 this->writef("%sif (%s) {\n"
1024                              "%s    %s;\n"
1025                              "%s    %s;\n"
1026                              "%s}\n", indent.c_str(),
1027                         mapper->dirtyExpression(valueVar, prevVar).c_str(), indent.c_str(),
1028                         mapper->saveState(valueVar, prevVar).c_str(), indent.c_str(),
1029                         mapper->setUniform(pdman, uniformName, valueVar).c_str(), indent.c_str());
1030             } else {
1031                 this->writef("%s%s;\n", indent.c_str(),
1032                         mapper->setUniform(pdman, uniformName, valueVar).c_str());
1033             }
1034 
1035             if (conditionalUniform) {
1036                 // Close the earlier precheck block
1037                 this->writef("        }\n");
1038             }
1039         }
1040     }
1041     if (wroteProcessor) {
1042         this->writef("        }\n");
1043     }
1044     if (section) {
1045         int samplerIndex = 0;
1046         for (const ProgramElement* p : fProgram.elements()) {
1047             if (p->is<GlobalVarDeclaration>()) {
1048                 const GlobalVarDeclaration& global = p->as<GlobalVarDeclaration>();
1049                 const VarDeclaration& decl = global.declaration()->as<VarDeclaration>();
1050                 const Variable& variable = decl.var();
1051                 String nameString(variable.name());
1052                 const char* name = nameString.c_str();
1053                 if (variable.type().typeKind() == Type::TypeKind::kSampler) {
1054                     this->writef("        const GrSurfaceProxyView& %sView = "
1055                                  "_outer.textureSampler(%d).view();\n",
1056                                  name, samplerIndex);
1057                     this->writef("        GrTexture& %s = *%sView.proxy()->peekTexture();\n",
1058                                  name, name);
1059                     this->writef("        (void) %s;\n", name);
1060                     ++samplerIndex;
1061                 } else if (needs_uniform_var(variable)) {
1062                     this->writef("        UniformHandle& %s = %sVar;\n"
1063                                     "        (void) %s;\n",
1064                                     name, HCodeGenerator::FieldName(name).c_str(), name);
1065                 } else if (SectionAndParameterHelper::IsParameter(variable) &&
1066                            !variable.type().isFragmentProcessor()) {
1067                     if (!wroteProcessor) {
1068                         this->writef("        const %s& _outer = _proc.cast<%s>();\n", fullName,
1069                                      fullName);
1070                         wroteProcessor = true;
1071                     }
1072 
1073                     if (!variable.type().isFragmentProcessor()) {
1074                         this->writef("        auto %s = _outer.%s;\n"
1075                                         "        (void) %s;\n",
1076                                         name, name, name);
1077                     }
1078                 }
1079             }
1080         }
1081         this->writeSection(kSetDataSection);
1082     }
1083     this->write("    }\n");
1084 }
1085 
writeOnTextureSampler()1086 void CPPCodeGenerator::writeOnTextureSampler() {
1087     bool foundSampler = false;
1088     for (const auto& param : fSectionAndParameterHelper.getParameters()) {
1089         if (param->type().typeKind() == Type::TypeKind::kSampler) {
1090             if (!foundSampler) {
1091                 this->writef(
1092                         "const GrFragmentProcessor::TextureSampler& %s::onTextureSampler(int "
1093                         "index) const {\n",
1094                         fFullName.c_str());
1095                 this->writef("    return IthTextureSampler(index, %s",
1096                              HCodeGenerator::FieldName(String(param->name()).c_str()).c_str());
1097                 foundSampler = true;
1098             } else {
1099                 this->writef(", %s",
1100                              HCodeGenerator::FieldName(String(param->name()).c_str()).c_str());
1101             }
1102         }
1103     }
1104     if (foundSampler) {
1105         this->write(");\n}\n");
1106     }
1107 }
1108 
writeClone()1109 void CPPCodeGenerator::writeClone() {
1110     if (!this->writeSection(kCloneSection)) {
1111         if (fSectionAndParameterHelper.getSection(kFieldsSection)) {
1112             fErrors.error(/*offset=*/0, "fragment processors with custom @fields must also have a "
1113                                         "custom @clone");
1114         }
1115         this->writef("%s::%s(const %s& src)\n"
1116                      ": INHERITED(k%s_ClassID, src.optimizationFlags())", fFullName.c_str(),
1117                      fFullName.c_str(), fFullName.c_str(), fFullName.c_str());
1118         for (const Variable* param : fSectionAndParameterHelper.getParameters()) {
1119             String fieldName = HCodeGenerator::FieldName(String(param->name()).c_str());
1120             if (!param->type().isFragmentProcessor()) {
1121                 this->writef("\n, %s(src.%s)",
1122                              fieldName.c_str(),
1123                              fieldName.c_str());
1124             }
1125         }
1126         this->writef(" {\n");
1127         this->writef("        this->cloneAndRegisterAllChildProcessors(src);\n");
1128         int samplerCount = 0;
1129         for (const auto& param : fSectionAndParameterHelper.getParameters()) {
1130             if (param->type().typeKind() == Type::TypeKind::kSampler) {
1131                 ++samplerCount;
1132             }
1133         }
1134         if (samplerCount) {
1135             this->writef("     this->setTextureSamplerCnt(%d);", samplerCount);
1136         }
1137         if (fAccessSampleCoordsDirectly) {
1138             this->writef("    this->setUsesSampleCoordsDirectly();\n");
1139         }
1140         this->write("}\n");
1141         this->writef("std::unique_ptr<GrFragmentProcessor> %s::clone() const {\n",
1142                      fFullName.c_str());
1143         this->writef("    return std::make_unique<%s>(*this);\n",
1144                      fFullName.c_str());
1145         this->write("}\n");
1146     }
1147 }
1148 
writeDumpInfo()1149 void CPPCodeGenerator::writeDumpInfo() {
1150     this->writef("#if GR_TEST_UTILS\n"
1151                  "SkString %s::onDumpInfo() const {\n", fFullName.c_str());
1152 
1153     if (!this->writeSection(kDumpInfoSection)) {
1154         if (fSectionAndParameterHelper.getSection(kFieldsSection)) {
1155             fErrors.error(/*offset=*/0, "fragment processors with custom @fields must also have a "
1156                                         "custom @dumpInfo");
1157         }
1158 
1159         String formatString;
1160         std::vector<String> argumentList;
1161 
1162         for (const Variable* param : fSectionAndParameterHelper.getParameters()) {
1163             // dumpInfo() doesn't need to log child FPs.
1164             if (param->type().isFragmentProcessor()) {
1165                 continue;
1166             }
1167 
1168             // Add this field onto the format string and argument list.
1169             String fieldName = HCodeGenerator::FieldName(String(param->name()).c_str());
1170             String runtimeValue = this->formatRuntimeValue(param->type(),
1171                                                            param->modifiers().fLayout,
1172                                                            param->name(),
1173                                                            &argumentList);
1174             formatString.appendf("%s%s=%s",
1175                                  formatString.empty() ? "" : ", ",
1176                                  fieldName.c_str(),
1177                                  runtimeValue.c_str());
1178         }
1179 
1180         if (!formatString.empty()) {
1181             // Emit the finished format string and associated arguments.
1182             this->writef("    return SkStringPrintf(\"(%s)\"", formatString.c_str());
1183 
1184             for (const String& argument : argumentList) {
1185                 this->writef(", %s", argument.c_str());
1186             }
1187 
1188             this->write(");");
1189         } else {
1190             // No fields to dump at all; just return an empty string.
1191             this->write("    return SkString();");
1192         }
1193     }
1194 
1195     this->write("\n"
1196                 "}\n"
1197                 "#endif\n");
1198 }
1199 
writeTest()1200 void CPPCodeGenerator::writeTest() {
1201     const Section* test = fSectionAndParameterHelper.getSection(kTestCodeSection);
1202     if (test) {
1203         this->writef(
1204                 "GR_DEFINE_FRAGMENT_PROCESSOR_TEST(%s);\n"
1205                 "#if GR_TEST_UTILS\n"
1206                 "std::unique_ptr<GrFragmentProcessor> %s::TestCreate(GrProcessorTestData* %s) {\n",
1207                 fFullName.c_str(),
1208                 fFullName.c_str(),
1209                 test->argument().c_str());
1210         this->writeSection(kTestCodeSection);
1211         this->write("}\n"
1212                     "#endif\n");
1213     }
1214 }
1215 
bits_needed(uint32_t v)1216 static int bits_needed(uint32_t v) {
1217     int bits = 1;
1218     while (v >= (1u << bits)) {
1219         bits++;
1220     }
1221     return bits;
1222 }
1223 
writeGetKey()1224 void CPPCodeGenerator::writeGetKey() {
1225     auto bitsForEnum = [&](const Type& type) {
1226         for (const ProgramElement* e : fProgram.elements()) {
1227             if (!e->is<Enum>() || type.name() != e->as<Enum>().typeName()) {
1228                 continue;
1229             }
1230             SKSL_INT minVal = 0, maxVal = 0;
1231             auto gatherEnumRange = [&](StringFragment, SKSL_INT value) {
1232                 minVal = std::min(minVal, value);
1233                 maxVal = std::max(maxVal, value);
1234             };
1235             e->as<Enum>().foreach(gatherEnumRange);
1236             if (minVal < 0) {
1237                 // Found a negative value in the enum, just use 32 bits
1238                 return 32;
1239             }
1240             SkASSERT(SkTFitsIn<uint32_t>(maxVal));
1241             return bits_needed(maxVal);
1242         }
1243         SK_ABORT("Didn't find declaring element for enum type!");
1244         return 32;
1245     };
1246 
1247     this->writef("void %s::onGetGLSLProcessorKey(const GrShaderCaps& caps, "
1248                                                 "GrProcessorKeyBuilder* b) const {\n",
1249                  fFullName.c_str());
1250     for (const ProgramElement* p : fProgram.elements()) {
1251         if (p->is<GlobalVarDeclaration>()) {
1252             const GlobalVarDeclaration& global = p->as<GlobalVarDeclaration>();
1253             const VarDeclaration& decl = global.declaration()->as<VarDeclaration>();
1254             const Variable& var = decl.var();
1255             const Type& varType = var.type();
1256             String nameString(var.name());
1257             const char* name = nameString.c_str();
1258             if (var.modifiers().fLayout.fFlags & Layout::kKey_Flag) {
1259                 if (var.modifiers().fFlags & Modifiers::kUniform_Flag) {
1260                     fErrors.error(var.fOffset, "layout(key) may not be specified on uniforms");
1261                 }
1262                 if (is_private(var)) {
1263                     this->writef(
1264                             "%s %s =",
1265                             HCodeGenerator::FieldType(fContext, varType, var.modifiers().fLayout)
1266                                     .c_str(),
1267                             String(var.name()).c_str());
1268                     if (decl.value()) {
1269                         fCPPMode = true;
1270                         this->writeExpression(*decl.value(), Precedence::kAssignment);
1271                         fCPPMode = false;
1272                     } else {
1273                         this->writef("%s", default_value(var).c_str());
1274                     }
1275                     this->write(";\n");
1276                 }
1277                 if (var.modifiers().fLayout.fWhen.fLength) {
1278                     this->writef("if (%s) {", String(var.modifiers().fLayout.fWhen).c_str());
1279                 }
1280                 if (varType == *fContext.fTypes.fHalf4) {
1281                     this->writef("    uint16_t red = SkFloatToHalf(%s.fR);\n",
1282                                  HCodeGenerator::FieldName(name).c_str());
1283                     this->writef("    uint16_t green = SkFloatToHalf(%s.fG);\n",
1284                                  HCodeGenerator::FieldName(name).c_str());
1285                     this->writef("    uint16_t blue = SkFloatToHalf(%s.fB);\n",
1286                                  HCodeGenerator::FieldName(name).c_str());
1287                     this->writef("    uint16_t alpha = SkFloatToHalf(%s.fA);\n",
1288                                  HCodeGenerator::FieldName(name).c_str());
1289                     this->writef("    b->add32(((uint32_t)red << 16) | green, \"%s.rg\");\n", name);
1290                     this->writef("    b->add32(((uint32_t)blue << 16) | alpha, \"%s.ba\");\n",
1291                                  name);
1292                 } else if (varType == *fContext.fTypes.fHalf ||
1293                            varType == *fContext.fTypes.fFloat) {
1294                     this->writef("    b->add32(sk_bit_cast<uint32_t>(%s), \"%s\");\n",
1295                                  HCodeGenerator::FieldName(name).c_str(), name);
1296                 } else if (varType.isBoolean()) {
1297                     this->writef("    b->addBool(%s, \"%s\");\n",
1298                                  HCodeGenerator::FieldName(name).c_str(), name);
1299                 } else if (varType.isEnum()) {
1300                     this->writef("    b->addBits(%d, (uint32_t) %s, \"%s\");\n",
1301                                  bitsForEnum(varType), HCodeGenerator::FieldName(name).c_str(),
1302                                  name);
1303                 } else if (varType.isInteger()) {
1304                     this->writef("    b->add32((uint32_t) %s, \"%s\");\n",
1305                                  HCodeGenerator::FieldName(name).c_str(), name);
1306                 } else {
1307                     SK_ABORT("NOT YET IMPLEMENTED: automatic key handling for %s\n",
1308                              varType.displayName().c_str());
1309                 }
1310                 if (var.modifiers().fLayout.fWhen.fLength) {
1311                     this->write("}");
1312                 }
1313             }
1314         }
1315     }
1316     this->write("}\n");
1317 }
1318 
generateCode()1319 bool CPPCodeGenerator::generateCode() {
1320     std::vector<const Variable*> uniforms;
1321     for (const ProgramElement* p : fProgram.elements()) {
1322         if (p->is<GlobalVarDeclaration>()) {
1323             const GlobalVarDeclaration& global = p->as<GlobalVarDeclaration>();
1324             const VarDeclaration& decl = global.declaration()->as<VarDeclaration>();
1325             if ((decl.var().modifiers().fFlags & Modifiers::kUniform_Flag) &&
1326                         decl.var().type().typeKind() != Type::TypeKind::kSampler) {
1327                 uniforms.push_back(&decl.var());
1328             }
1329 
1330             if (is_uniform_in(decl.var())) {
1331                 // Validate the "uniform in" declarations to make sure they are fully supported,
1332                 // instead of generating surprising C++
1333                 const UniformCTypeMapper* mapper =
1334                         UniformCTypeMapper::Get(fContext, decl.var());
1335                 if (mapper == nullptr) {
1336                     fErrors.error(decl.fOffset, String(decl.var().name())
1337                             + "'s type is not supported for use as a 'uniform in'");
1338                     return false;
1339                 }
1340             } else {
1341                 // If it's not a uniform_in, it's an error to be tracked
1342                 if (decl.var().modifiers().fLayout.fFlags & Layout::kTracked_Flag) {
1343                     fErrors.error(decl.fOffset, "Non-'in uniforms' cannot be tracked");
1344                     return false;
1345                 }
1346             }
1347         }
1348     }
1349     const char* baseName = fName.c_str();
1350     const char* fullName = fFullName.c_str();
1351     this->writef("%s\n", HCodeGenerator::GetHeader(fProgram, fErrors).c_str());
1352     this->writef(kFragmentProcessorHeader, fullName);
1353     this->writef("#include \"%s.h\"\n\n", fullName);
1354     this->writeSection(kCppSection);
1355     this->writef("#include \"src/core/SkUtils.h\"\n"
1356                  "#include \"src/gpu/GrTexture.h\"\n"
1357                  "#include \"src/gpu/glsl/GrGLSLFragmentProcessor.h\"\n"
1358                  "#include \"src/gpu/glsl/GrGLSLFragmentShaderBuilder.h\"\n"
1359                  "#include \"src/gpu/glsl/GrGLSLProgramBuilder.h\"\n"
1360                  "#include \"src/sksl/SkSLCPP.h\"\n"
1361                  "#include \"src/sksl/SkSLUtil.h\"\n"
1362                  "class GrGLSL%s : public GrGLSLFragmentProcessor {\n"
1363                  "public:\n"
1364                  "    GrGLSL%s() {}\n",
1365                  baseName, baseName);
1366     bool result = this->writeEmitCode(uniforms);
1367     this->write("private:\n");
1368     this->writeSetData(uniforms);
1369     this->writePrivateVars();
1370     for (const auto& u : uniforms) {
1371         if (needs_uniform_var(*u) && !(u->modifiers().fFlags & Modifiers::kIn_Flag)) {
1372             this->writef("    UniformHandle %sVar;\n",
1373                          HCodeGenerator::FieldName(String(u->name()).c_str()).c_str());
1374         }
1375     }
1376     for (const auto& param : fSectionAndParameterHelper.getParameters()) {
1377         if (needs_uniform_var(*param)) {
1378             this->writef("    UniformHandle %sVar;\n",
1379                          HCodeGenerator::FieldName(String(param->name()).c_str()).c_str());
1380         }
1381     }
1382     this->writef("};\n"
1383                  "std::unique_ptr<GrGLSLFragmentProcessor> %s::onMakeProgramImpl() const {\n"
1384                  "    return std::make_unique<GrGLSL%s>();\n"
1385                  "}\n",
1386                  fullName, baseName);
1387     this->writeGetKey();
1388     this->writef("bool %s::onIsEqual(const GrFragmentProcessor& other) const {\n"
1389                  "    const %s& that = other.cast<%s>();\n"
1390                  "    (void) that;\n",
1391                  fullName, fullName, fullName);
1392     for (const auto& param : fSectionAndParameterHelper.getParameters()) {
1393         if (param->type().isFragmentProcessor()) {
1394             continue;
1395         }
1396         String nameString(param->name());
1397         const char* name = nameString.c_str();
1398         this->writef("    if (%s != that.%s) return false;\n",
1399                      HCodeGenerator::FieldName(name).c_str(),
1400                      HCodeGenerator::FieldName(name).c_str());
1401     }
1402     this->write("    return true;\n"
1403                 "}\n");
1404     this->writeClone();
1405     this->writeDumpInfo();
1406     this->writeOnTextureSampler();
1407     this->writeTest();
1408     this->writeSection(kCppEndSection);
1409 
1410     result &= 0 == fErrors.errorCount();
1411     return result;
1412 }
1413 
1414 }  // namespace SkSL
1415 
1416 #endif // defined(SKSL_STANDALONE) || GR_TEST_UTILS
1417