blob: f9c6ee3b50e471ff4bdeb37c0dd0358ae4745f3b [file] [log] [blame]
Ethan Nicholas762466e2017-06-29 10:03:38 -04001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkSLCPPCodeGenerator.h"
9
10#include "SkSLCompiler.h"
Michael Ludwiga4275592018-08-31 10:52:47 -040011#include "SkSLCPPUniformCTypes.h"
Ethan Nicholas762466e2017-06-29 10:03:38 -040012#include "SkSLHCodeGenerator.h"
13
Michael Ludwig92e4c7f2018-08-30 16:08:18 -040014#include <algorithm>
15
Ethan Nicholas762466e2017-06-29 10:03:38 -040016namespace SkSL {
17
18static bool needs_uniform_var(const Variable& var) {
Ethan Nicholas5f9836e2017-12-20 15:16:33 -050019 return (var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
20 var.fType.kind() != Type::kSampler_Kind;
Ethan Nicholas762466e2017-06-29 10:03:38 -040021}
22
23CPPCodeGenerator::CPPCodeGenerator(const Context* context, const Program* program,
24 ErrorReporter* errors, String name, OutputStream* out)
25: INHERITED(context, program, errors, out)
26, fName(std::move(name))
27, fFullName(String::printf("Gr%s", fName.c_str()))
28, fSectionAndParameterHelper(*program, *errors) {
29 fLineEnding = "\\n";
30}
31
32void CPPCodeGenerator::writef(const char* s, va_list va) {
33 static constexpr int BUFFER_SIZE = 1024;
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040034 va_list copy;
35 va_copy(copy, va);
Ethan Nicholas762466e2017-06-29 10:03:38 -040036 char buffer[BUFFER_SIZE];
37 int length = vsnprintf(buffer, BUFFER_SIZE, s, va);
38 if (length < BUFFER_SIZE) {
39 fOut->write(buffer, length);
40 } else {
41 std::unique_ptr<char[]> heap(new char[length + 1]);
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040042 vsprintf(heap.get(), s, copy);
Ethan Nicholas762466e2017-06-29 10:03:38 -040043 fOut->write(heap.get(), length);
44 }
z102.zhangd74f2c82018-08-10 09:08:47 +080045 va_end(copy);
Ethan Nicholas762466e2017-06-29 10:03:38 -040046}
47
48void CPPCodeGenerator::writef(const char* s, ...) {
49 va_list va;
50 va_start(va, s);
51 this->writef(s, va);
52 va_end(va);
53}
54
55void CPPCodeGenerator::writeHeader() {
56}
57
Ethan Nicholasf7b88202017-09-18 14:10:39 -040058bool CPPCodeGenerator::usesPrecisionModifiers() const {
59 return false;
Ethan Nicholas762466e2017-06-29 10:03:38 -040060}
61
Ethan Nicholasf7b88202017-09-18 14:10:39 -040062String CPPCodeGenerator::getTypeName(const Type& type) {
63 return type.name();
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040064}
Ethan Nicholasf7b88202017-09-18 14:10:39 -040065
Ethan Nicholas762466e2017-06-29 10:03:38 -040066void CPPCodeGenerator::writeBinaryExpression(const BinaryExpression& b,
67 Precedence parentPrecedence) {
68 if (b.fOperator == Token::PERCENT) {
69 // need to use "%%" instead of "%" b/c the code will be inside of a printf
70 Precedence precedence = GetBinaryPrecedence(b.fOperator);
71 if (precedence >= parentPrecedence) {
72 this->write("(");
73 }
74 this->writeExpression(*b.fLeft, precedence);
75 this->write(" %% ");
76 this->writeExpression(*b.fRight, precedence);
77 if (precedence >= parentPrecedence) {
78 this->write(")");
79 }
80 } else {
81 INHERITED::writeBinaryExpression(b, parentPrecedence);
82 }
83}
84
85void CPPCodeGenerator::writeIndexExpression(const IndexExpression& i) {
86 const Expression& base = *i.fBase;
87 if (base.fKind == Expression::kVariableReference_Kind) {
88 int builtin = ((VariableReference&) base).fVariable.fModifiers.fLayout.fBuiltin;
89 if (SK_TRANSFORMEDCOORDS2D_BUILTIN == builtin) {
90 this->write("%s");
91 if (i.fIndex->fKind != Expression::kIntLiteral_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070092 fErrors.error(i.fIndex->fOffset,
Ethan Nicholas762466e2017-06-29 10:03:38 -040093 "index into sk_TransformedCoords2D must be an integer literal");
94 return;
95 }
96 int64_t index = ((IntLiteral&) *i.fIndex).fValue;
97 String name = "sk_TransformedCoords2D_" + to_string(index);
98 fFormatArgs.push_back(name + ".c_str()");
99 if (fWrittenTransformedCoords.find(index) == fWrittenTransformedCoords.end()) {
Brian Osman72a37be2017-08-15 09:19:53 -0400100 fExtraEmitCodeCode += " SkString " + name +
Ethan Nicholas762466e2017-06-29 10:03:38 -0400101 " = fragBuilder->ensureCoords2D(args.fTransformedCoords[" +
102 to_string(index) + "]);\n";
103 fWrittenTransformedCoords.insert(index);
104 }
105 return;
106 } else if (SK_TEXTURESAMPLERS_BUILTIN == builtin) {
107 this->write("%s");
108 if (i.fIndex->fKind != Expression::kIntLiteral_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700109 fErrors.error(i.fIndex->fOffset,
Ethan Nicholas762466e2017-06-29 10:03:38 -0400110 "index into sk_TextureSamplers must be an integer literal");
111 return;
112 }
113 int64_t index = ((IntLiteral&) *i.fIndex).fValue;
114 fFormatArgs.push_back(" fragBuilder->getProgramBuilder()->samplerVariable("
115 "args.fTexSamplers[" + to_string(index) + "]).c_str()");
116 return;
117 }
118 }
119 INHERITED::writeIndexExpression(i);
120}
121
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400122static String default_value(const Type& type) {
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500123 if (type.fName == "bool") {
124 return "false";
125 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400126 switch (type.kind()) {
127 case Type::kScalar_Kind: return "0";
128 case Type::kVector_Kind: return type.name() + "(0)";
129 case Type::kMatrix_Kind: return type.name() + "(1)";
130 default: ABORT("unsupported default_value type\n");
131 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400132}
133
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500134static String default_value(const Variable& var) {
135 if (var.fModifiers.fLayout.fCType == "GrColor4f") {
136 return "GrColor4f::kIllegalConstructor";
137 }
138 return default_value(var.fType);
139}
140
Ethan Nicholas762466e2017-06-29 10:03:38 -0400141static bool is_private(const Variable& var) {
142 return !(var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
143 !(var.fModifiers.fFlags & Modifiers::kIn_Flag) &&
144 var.fStorage == Variable::kGlobal_Storage &&
145 var.fModifiers.fLayout.fBuiltin == -1;
146}
147
Michael Ludwiga4275592018-08-31 10:52:47 -0400148static bool is_uniform_in(const Variable& var) {
149 return (var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
150 (var.fModifiers.fFlags & Modifiers::kIn_Flag) &&
151 var.fType.kind() != Type::kSampler_Kind;
152}
153
Ethan Nicholasd608c092017-10-26 09:30:08 -0400154void CPPCodeGenerator::writeRuntimeValue(const Type& type, const Layout& layout,
155 const String& cppCode) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400156 if (type.isFloat()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400157 this->write("%f");
158 fFormatArgs.push_back(cppCode);
159 } else if (type == *fContext.fInt_Type) {
160 this->write("%d");
161 fFormatArgs.push_back(cppCode);
162 } else if (type == *fContext.fBool_Type) {
163 this->write("%s");
164 fFormatArgs.push_back("(" + cppCode + " ? \"true\" : \"false\")");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400165 } else if (type == *fContext.fFloat2_Type || type == *fContext.fHalf2_Type) {
166 this->write(type.name() + "(%f, %f)");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400167 fFormatArgs.push_back(cppCode + ".fX");
168 fFormatArgs.push_back(cppCode + ".fY");
Ethan Nicholas82399462017-10-16 12:35:44 -0400169 } else if (type == *fContext.fFloat4_Type || type == *fContext.fHalf4_Type) {
170 this->write(type.name() + "(%f, %f, %f, %f)");
Ethan Nicholasd608c092017-10-26 09:30:08 -0400171 if (layout.fCType == "SkPMColor") {
172 fFormatArgs.push_back("SkGetPackedR32(" + cppCode + ") / 255.0");
173 fFormatArgs.push_back("SkGetPackedG32(" + cppCode + ") / 255.0");
174 fFormatArgs.push_back("SkGetPackedB32(" + cppCode + ") / 255.0");
175 fFormatArgs.push_back("SkGetPackedA32(" + cppCode + ") / 255.0");
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500176 } else if (layout.fCType == "GrColor4f") {
177 fFormatArgs.push_back(cppCode + ".fRGBA[0]");
178 fFormatArgs.push_back(cppCode + ".fRGBA[1]");
179 fFormatArgs.push_back(cppCode + ".fRGBA[2]");
180 fFormatArgs.push_back(cppCode + ".fRGBA[3]");
Ethan Nicholasd608c092017-10-26 09:30:08 -0400181 } else {
182 fFormatArgs.push_back(cppCode + ".left()");
183 fFormatArgs.push_back(cppCode + ".top()");
184 fFormatArgs.push_back(cppCode + ".right()");
185 fFormatArgs.push_back(cppCode + ".bottom()");
186 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500187 } else if (type.kind() == Type::kEnum_Kind) {
188 this->write("%d");
189 fFormatArgs.push_back("(int) " + cppCode);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400190 } else if (type == *fContext.fInt4_Type ||
191 type == *fContext.fShort4_Type ||
192 type == *fContext.fByte4_Type) {
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500193 this->write(type.name() + "(%d, %d, %d, %d)");
194 fFormatArgs.push_back(cppCode + ".left()");
195 fFormatArgs.push_back(cppCode + ".top()");
196 fFormatArgs.push_back(cppCode + ".right()");
197 fFormatArgs.push_back(cppCode + ".bottom()");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400198 } else {
Ethan Nicholas82399462017-10-16 12:35:44 -0400199 printf("unsupported runtime value type '%s'\n", String(type.fName).c_str());
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400200 SkASSERT(false);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400201 }
202}
203
204void CPPCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) {
205 if (is_private(var)) {
Ethan Nicholasd608c092017-10-26 09:30:08 -0400206 this->writeRuntimeValue(var.fType, var.fModifiers.fLayout, var.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400207 } else {
208 this->writeExpression(value, kTopLevel_Precedence);
209 }
210}
211
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400212String CPPCodeGenerator::getSamplerHandle(const Variable& var) {
213 int samplerCount = 0;
Ethan Nicholas68990be2017-07-13 09:36:52 -0400214 for (const auto param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400215 if (&var == param) {
216 return "args.fTexSamplers[" + to_string(samplerCount) + "]";
217 }
218 if (param->fType.kind() == Type::kSampler_Kind) {
219 ++samplerCount;
220 }
221 }
222 ABORT("should have found sampler in parameters\n");
223}
224
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400225void CPPCodeGenerator::writeIntLiteral(const IntLiteral& i) {
226 this->write(to_string((int32_t) i.fValue));
227}
228
Ethan Nicholas82399462017-10-16 12:35:44 -0400229void CPPCodeGenerator::writeSwizzle(const Swizzle& swizzle) {
230 if (fCPPMode) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400231 SkASSERT(swizzle.fComponents.size() == 1); // no support for multiple swizzle components yet
Ethan Nicholas82399462017-10-16 12:35:44 -0400232 this->writeExpression(*swizzle.fBase, kPostfix_Precedence);
233 switch (swizzle.fComponents[0]) {
234 case 0: this->write(".left()"); break;
235 case 1: this->write(".top()"); break;
236 case 2: this->write(".right()"); break;
237 case 3: this->write(".bottom()"); break;
238 }
239 } else {
240 INHERITED::writeSwizzle(swizzle);
241 }
242}
243
Ethan Nicholas762466e2017-06-29 10:03:38 -0400244void CPPCodeGenerator::writeVariableReference(const VariableReference& ref) {
Ethan Nicholas82399462017-10-16 12:35:44 -0400245 if (fCPPMode) {
246 this->write(ref.fVariable.fName);
247 return;
248 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400249 switch (ref.fVariable.fModifiers.fLayout.fBuiltin) {
250 case SK_INCOLOR_BUILTIN:
251 this->write("%s");
Michael Ludwig231de032018-08-30 14:33:01 -0400252 // EmitArgs.fInputColor is automatically set to half4(1) if
253 // no input was specified
254 fFormatArgs.push_back(String("args.fInputColor"));
Ethan Nicholas762466e2017-06-29 10:03:38 -0400255 break;
256 case SK_OUTCOLOR_BUILTIN:
257 this->write("%s");
258 fFormatArgs.push_back(String("args.fOutputColor"));
259 break;
Ethan Nicholascd700e92018-08-24 16:43:57 -0400260 case SK_WIDTH_BUILTIN:
261 this->write("sk_Width");
262 break;
263 case SK_HEIGHT_BUILTIN:
264 this->write("sk_Height");
265 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400266 default:
267 if (ref.fVariable.fType.kind() == Type::kSampler_Kind) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400268 this->write("%s");
269 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerVariable(" +
270 this->getSamplerHandle(ref.fVariable) + ").c_str()");
271 return;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400272 }
273 if (ref.fVariable.fModifiers.fFlags & Modifiers::kUniform_Flag) {
274 this->write("%s");
275 String name = ref.fVariable.fName;
Brian Osman1cb41712017-10-19 12:54:52 -0400276 String var = String::printf("args.fUniformHandler->getUniformCStr(%sVar)",
277 HCodeGenerator::FieldName(name.c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400278 String code;
279 if (ref.fVariable.fModifiers.fLayout.fWhen.size()) {
280 code = String::printf("%sVar.isValid() ? %s : \"%s\"",
281 HCodeGenerator::FieldName(name.c_str()).c_str(),
282 var.c_str(),
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400283 default_value(ref.fVariable.fType).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400284 } else {
285 code = var;
286 }
287 fFormatArgs.push_back(code);
288 } else if (SectionAndParameterHelper::IsParameter(ref.fVariable)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700289 String name(ref.fVariable.fName);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400290 this->writeRuntimeValue(ref.fVariable.fType, ref.fVariable.fModifiers.fLayout,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700291 String::printf("_outer.%s()", name.c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400292 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700293 this->write(ref.fVariable.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400294 }
295 }
296}
297
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400298void CPPCodeGenerator::writeIfStatement(const IfStatement& s) {
299 if (s.fIsStatic) {
300 this->write("@");
301 }
302 INHERITED::writeIfStatement(s);
303}
304
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400305void CPPCodeGenerator::writeReturnStatement(const ReturnStatement& s) {
306 if (fInMain) {
307 fErrors.error(s.fOffset, "fragmentProcessor main() may not contain return statements");
308 }
309 INHERITED::writeReturnStatement(s);
310}
311
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400312void CPPCodeGenerator::writeSwitchStatement(const SwitchStatement& s) {
313 if (s.fIsStatic) {
314 this->write("@");
315 }
316 INHERITED::writeSwitchStatement(s);
317}
318
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400319void CPPCodeGenerator::writeFunctionCall(const FunctionCall& c) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400320 if (c.fFunction.fBuiltin && c.fFunction.fName == "process") {
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400321 // Sanity checks that are detected by function definition in sksl_fp.inc
322 SkASSERT(c.fArguments.size() == 1 || c.fArguments.size() == 2);
323 SkASSERT("fragmentProcessor" == c.fArguments[0]->fType.name());
324
325 // Actually fail during compilation if arguments with valid types are
326 // provided that are not variable references, since process() is a
327 // special function that impacts code emission.
328 if (c.fArguments[0]->fKind != Expression::kVariableReference_Kind) {
329 fErrors.error(c.fArguments[0]->fOffset,
330 "process()'s fragmentProcessor argument must be a variable reference\n");
331 return;
332 }
333 if (c.fArguments.size() > 1) {
334 // Second argument must also be a half4 expression
335 SkASSERT("half4" == c.fArguments[1]->fType.name());
336 }
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400337 int index = 0;
338 bool found = false;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400339 for (const auto& p : fProgram) {
340 if (ProgramElement::kVar_Kind == p.fKind) {
341 const VarDeclarations& decls = (const VarDeclarations&) p;
342 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000343 VarDeclaration& decl = (VarDeclaration&) *raw;
344 if (decl.fVar == &((VariableReference&) *c.fArguments[0]).fVariable) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400345 found = true;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000346 } else if (decl.fVar->fType == *fContext.fFragmentProcessor_Type) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400347 ++index;
348 }
349 }
350 }
351 if (found) {
352 break;
353 }
354 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400355 SkASSERT(found);
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400356
357 // Flush all previous statements so that this emitted child can
358 // depend upon any declared variables within that section
359 this->flushEmittedCode();
360
361 // Set to the empty string when no input color parameter should be emitted,
362 // which means this must be properly formatted with a prefixed comma
363 // when the parameter should be inserted into the emitChild() parameter list.
364 String inputArg;
365 if (c.fArguments.size() > 1) {
366 SkASSERT(c.fArguments.size() == 2);
367 // Use the emitChild() variant that accepts an input color, so convert
368 // the 2nd argument's expression into C++ code that produces sksl
369 // stored in an SkString.
370
371 String inputName = "_input" + to_string(index);
372 fExtraEmitCodeCode += " " + convertSKSLExpressionToCPP(
373 *c.fArguments[1], inputName);
374
375 // emitChild() needs a char*
376 inputArg = ", " + inputName + ".c_str()";
377 }
378
379 // Write the output handling after the possible input handling
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400380 String childName = "_child" + to_string(index);
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400381 fExtraEmitCodeCode += " SkString " + childName + "(\"" + childName + "\");\n";
382 fExtraEmitCodeCode += " this->emitChild(" + to_string(index) + inputArg +
383 ", &" + childName + ", args);\n";
384
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400385 this->write("%s");
386 fFormatArgs.push_back(childName + ".c_str()");
387 return;
388 }
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400389 INHERITED::writeFunctionCall(c);
390 if (c.fFunction.fBuiltin && c.fFunction.fName == "texture") {
391 this->write(".%s");
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400392 SkASSERT(c.fArguments.size() >= 1);
393 SkASSERT(c.fArguments[0]->fKind == Expression::kVariableReference_Kind);
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400394 String sampler = this->getSamplerHandle(((VariableReference&) *c.fArguments[0]).fVariable);
395 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerSwizzle(" + sampler +
396 ").c_str()");
397 }
398}
399
Ethan Nicholas762466e2017-06-29 10:03:38 -0400400void CPPCodeGenerator::writeFunction(const FunctionDefinition& f) {
401 if (f.fDeclaration.fName == "main") {
402 fFunctionHeader = "";
403 OutputStream* oldOut = fOut;
404 StringStream buffer;
405 fOut = &buffer;
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400406 fInMain = true;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400407 for (const auto& s : ((Block&) *f.fBody).fStatements) {
408 this->writeStatement(*s);
409 this->writeLine();
410 }
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400411 fInMain = false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400412
413 fOut = oldOut;
414 this->write(fFunctionHeader);
415 this->write(buffer.str());
416 } else {
417 INHERITED::writeFunction(f);
418 }
419}
420
421void CPPCodeGenerator::writeSetting(const Setting& s) {
422 static constexpr const char* kPrefix = "sk_Args.";
423 if (!strncmp(s.fName.c_str(), kPrefix, strlen(kPrefix))) {
424 const char* name = s.fName.c_str() + strlen(kPrefix);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400425 this->writeRuntimeValue(s.fType, Layout(), HCodeGenerator::FieldName(name).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400426 } else {
427 this->write(s.fName.c_str());
428 }
429}
430
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400431bool CPPCodeGenerator::writeSection(const char* name, const char* prefix) {
Ethan Nicholas68990be2017-07-13 09:36:52 -0400432 const Section* s = fSectionAndParameterHelper.getSection(name);
433 if (s) {
434 this->writef("%s%s", prefix, s->fText.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400435 return true;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400436 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400437 return false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400438}
439
440void CPPCodeGenerator::writeProgramElement(const ProgramElement& p) {
441 if (p.fKind == ProgramElement::kSection_Kind) {
442 return;
443 }
444 if (p.fKind == ProgramElement::kVar_Kind) {
445 const VarDeclarations& decls = (const VarDeclarations&) p;
446 if (!decls.fVars.size()) {
447 return;
448 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000449 const Variable& var = *((VarDeclaration&) *decls.fVars[0]).fVar;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400450 if (var.fModifiers.fFlags & (Modifiers::kIn_Flag | Modifiers::kUniform_Flag) ||
451 -1 != var.fModifiers.fLayout.fBuiltin) {
452 return;
453 }
454 }
455 INHERITED::writeProgramElement(p);
456}
457
458void CPPCodeGenerator::addUniform(const Variable& var) {
459 if (!needs_uniform_var(var)) {
460 return;
461 }
462 const char* precision;
463 if (var.fModifiers.fFlags & Modifiers::kHighp_Flag) {
464 precision = "kHigh_GrSLPrecision";
465 } else if (var.fModifiers.fFlags & Modifiers::kMediump_Flag) {
466 precision = "kMedium_GrSLPrecision";
467 } else if (var.fModifiers.fFlags & Modifiers::kLowp_Flag) {
468 precision = "kLow_GrSLPrecision";
469 } else {
470 precision = "kDefault_GrSLPrecision";
471 }
472 const char* type;
473 if (var.fType == *fContext.fFloat_Type) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400474 type = "kFloat_GrSLType";
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400475 } else if (var.fType == *fContext.fHalf_Type) {
476 type = "kHalf_GrSLType";
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400477 } else if (var.fType == *fContext.fFloat2_Type) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400478 type = "kFloat2_GrSLType";
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400479 } else if (var.fType == *fContext.fHalf2_Type) {
480 type = "kHalf2_GrSLType";
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400481 } else if (var.fType == *fContext.fFloat4_Type) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400482 type = "kFloat4_GrSLType";
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400483 } else if (var.fType == *fContext.fHalf4_Type) {
484 type = "kHalf4_GrSLType";
Brian Osman1cb41712017-10-19 12:54:52 -0400485 } else if (var.fType == *fContext.fFloat4x4_Type) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400486 type = "kFloat4x4_GrSLType";
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400487 } else if (var.fType == *fContext.fHalf4x4_Type) {
488 type = "kHalf4x4_GrSLType";
Ethan Nicholas762466e2017-06-29 10:03:38 -0400489 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700490 ABORT("unsupported uniform type: %s %s;\n", String(var.fType.fName).c_str(),
491 String(var.fName).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400492 }
493 if (var.fModifiers.fLayout.fWhen.size()) {
494 this->writef(" if (%s) {\n ", var.fModifiers.fLayout.fWhen.c_str());
495 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700496 String name(var.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400497 this->writef(" %sVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, %s, "
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700498 "%s, \"%s\");\n", HCodeGenerator::FieldName(name.c_str()).c_str(), type, precision,
499 name.c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400500 if (var.fModifiers.fLayout.fWhen.size()) {
501 this->write(" }\n");
502 }
503}
504
Ethan Nicholascd700e92018-08-24 16:43:57 -0400505void CPPCodeGenerator::writeInputVars() {
506}
507
Ethan Nicholas762466e2017-06-29 10:03:38 -0400508void CPPCodeGenerator::writePrivateVars() {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400509 for (const auto& p : fProgram) {
510 if (ProgramElement::kVar_Kind == p.fKind) {
511 const VarDeclarations& decls = (const VarDeclarations&) p;
512 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000513 VarDeclaration& decl = (VarDeclaration&) *raw;
514 if (is_private(*decl.fVar)) {
515 if (decl.fVar->fType == *fContext.fFragmentProcessor_Type) {
516 fErrors.error(decl.fOffset,
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400517 "fragmentProcessor variables must be declared 'in'");
518 return;
519 }
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500520 this->writef("%s %s = %s;\n",
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000521 HCodeGenerator::FieldType(fContext, decl.fVar->fType,
522 decl.fVar->fModifiers.fLayout).c_str(),
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500523 String(decl.fVar->fName).c_str(),
524 default_value(*decl.fVar).c_str());
Michael Ludwiga4275592018-08-31 10:52:47 -0400525 } else if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
526 // An auto-tracked uniform in variable, so add a field to hold onto the prior
527 // state. Note that tracked variables must be uniform in's and that is validated
528 // before writePrivateVars() is called.
529 const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *decl.fVar);
530 SkASSERT(mapper && mapper->supportsTracking());
531
532 String name = HCodeGenerator::FieldName(String(decl.fVar->fName).c_str());
533 // The member statement is different if the mapper reports a default value
534 if (mapper->defaultValue().size() > 0) {
535 this->writef("%s %sPrev = %s;\n",
536 mapper->ctype().c_str(), name.c_str(),
537 mapper->defaultValue().c_str());
538 } else {
539 this->writef("%s %sPrev;\n",
540 mapper->ctype().c_str(), name.c_str());
541 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400542 }
543 }
544 }
545 }
546}
547
548void CPPCodeGenerator::writePrivateVarValues() {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400549 for (const auto& p : fProgram) {
550 if (ProgramElement::kVar_Kind == p.fKind) {
551 const VarDeclarations& decls = (const VarDeclarations&) p;
552 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000553 VarDeclaration& decl = (VarDeclaration&) *raw;
554 if (is_private(*decl.fVar) && decl.fValue) {
555 this->writef("%s = ", String(decl.fVar->fName).c_str());
Ethan Nicholas82399462017-10-16 12:35:44 -0400556 fCPPMode = true;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000557 this->writeExpression(*decl.fValue, kAssignment_Precedence);
Ethan Nicholas82399462017-10-16 12:35:44 -0400558 fCPPMode = false;
559 this->write(";\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400560 }
561 }
562 }
563 }
564}
565
Ethan Nicholas82399462017-10-16 12:35:44 -0400566static bool is_accessible(const Variable& var) {
567 return Type::kSampler_Kind != var.fType.kind() &&
568 Type::kOther_Kind != var.fType.kind();
569}
570
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400571void CPPCodeGenerator::flushEmittedCode(bool flushAll) {
572 if (fCPPBuffer == nullptr) {
573 // Not actually within writeEmitCode() so nothing to flush
574 return;
575 }
576
577 StringStream* skslBuffer = static_cast<StringStream*>(fOut);
578
579 String sksl = skslBuffer->str();
580 // Empty the accumulation buffer; if there are any partial statements in
581 // the extracted sksl string they will be re-added later
582 skslBuffer->reset();
583
584 if (!flushAll) {
585 // Find the last ';', '{', or '}' and leave everything after that in the buffer
586 int lastStatementEnd = sksl.findLastOf(';');
587 int lastBlockOpen = sksl.findLastOf('{');
588 int lastBlockEnd = sksl.findLastOf('}');
589
590 int flushPoint = std::max(lastStatementEnd, std::max(lastBlockEnd, lastBlockOpen));
591
592 // NOTE: this does the right thing when flushPoint = -1
593 if (flushPoint < (int) sksl.size() - 1) {
594 // There is partial sksl content that can't be flushed yet so put
595 // that back into the sksl buffer and remove it from the string
596 skslBuffer->writeText(sksl.c_str() + flushPoint + 1);
597 sksl = String(sksl.c_str(), flushPoint + 1);
598 }
599 }
600
601 // Switch back to the CPP buffer for the actual code appending statements
602 fOut = fCPPBuffer;
603 if (fExtraEmitCodeCode.size() > 0) {
604 this->writef("%s", fExtraEmitCodeCode.c_str());
605 fExtraEmitCodeCode.reset();
606 }
607 // writeCodeAppend automatically removes the format args that it consumed,
608 // so fFormatArgs will be in a valid state for any partial statements left
609 // in the sksl buffer.
610 this->writeCodeAppend(sksl);
611
612 // After appending, switch back to an sksl buffer that contains any
613 // remaining partial statements that couldn't be appended
614 fOut = skslBuffer;
615}
616
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400617void CPPCodeGenerator::writeCodeAppend(const String& code) {
618 // codeAppendf can only handle appending 1024 bytes at a time, so we need to break the string
619 // into chunks. Unfortunately we can't tell exactly how long the string is going to end up,
620 // because printf escape sequences get replaced by strings of unknown length, but keeping the
621 // format string below 512 bytes is probably safe.
622 static constexpr size_t maxChunkSize = 512;
623 size_t start = 0;
624 size_t index = 0;
625 size_t argStart = 0;
626 size_t argCount;
627 while (index < code.size()) {
628 argCount = 0;
629 this->write(" fragBuilder->codeAppendf(\"");
630 while (index < code.size() && index < start + maxChunkSize) {
631 if ('%' == code[index]) {
632 if (index == start + maxChunkSize - 1 || index == code.size() - 1) {
633 break;
634 }
635 if (code[index + 1] != '%') {
636 ++argCount;
637 }
Ethan Nicholasef0c9fd2017-10-30 10:04:14 -0400638 } else if ('\\' == code[index] && index == start + maxChunkSize - 1) {
639 // avoid splitting an escape sequence that happens to fall across a chunk boundary
640 break;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400641 }
642 ++index;
643 }
644 fOut->write(code.c_str() + start, index - start);
645 this->write("\"");
646 for (size_t i = argStart; i < argStart + argCount; ++i) {
647 this->writef(", %s", fFormatArgs[i].c_str());
648 }
649 this->write(");\n");
650 argStart += argCount;
651 start = index;
652 }
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400653
654 // argStart is equal to the number of fFormatArgs that were consumed
655 // so they should be removed from the list
656 if (argStart > 0) {
657 fFormatArgs.erase(fFormatArgs.begin(), fFormatArgs.begin() + argStart);
658 }
659}
660
661String CPPCodeGenerator::convertSKSLExpressionToCPP(const Expression& e,
662 const String& cppVar) {
663 // To do this conversion, we temporarily switch the sksl output stream
664 // to an empty stringstream and reset the format args to empty.
665 OutputStream* oldSKSL = fOut;
666 StringStream exprBuffer;
667 fOut = &exprBuffer;
668
669 std::vector<String> oldArgs(fFormatArgs);
670 fFormatArgs.clear();
671
672 // Convert the argument expression into a format string and args
673 this->writeExpression(e, Precedence::kTopLevel_Precedence);
674 std::vector<String> newArgs(fFormatArgs);
675 String exprFormat = exprBuffer.str();
676
677 // After generating, restore the original output stream and format args
678 fFormatArgs = oldArgs;
679 fOut = oldSKSL;
680
681 // Now build the final C++ code snippet from the format string and args
682 String cppExpr;
683 if (newArgs.size() == 0) {
684 // This was a static expression, so we can simplify the input
685 // color declaration in the emitted code to just a static string
686 cppExpr = "SkString " + cppVar + "(\"" + exprFormat + "\");\n";
687 } else {
688 // String formatting must occur dynamically, so have the C++ declaration
689 // use SkStringPrintf with the format args that were accumulated
690 // when the expression was written.
691 cppExpr = "SkString " + cppVar + " = SkStringPrintf(\"" + exprFormat + "\"";
692 for (size_t i = 0; i < newArgs.size(); i++) {
693 cppExpr += ", " + newArgs[i];
694 }
695 cppExpr += ");\n";
696 }
697 return cppExpr;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400698}
699
Ethan Nicholas762466e2017-06-29 10:03:38 -0400700bool CPPCodeGenerator::writeEmitCode(std::vector<const Variable*>& uniforms) {
701 this->write(" void emitCode(EmitArgs& args) override {\n"
702 " GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;\n");
703 this->writef(" const %s& _outer = args.fFp.cast<%s>();\n"
704 " (void) _outer;\n",
705 fFullName.c_str(), fFullName.c_str());
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400706 for (const auto& p : fProgram) {
707 if (ProgramElement::kVar_Kind == p.fKind) {
708 const VarDeclarations& decls = (const VarDeclarations&) p;
709 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000710 VarDeclaration& decl = (VarDeclaration&) *raw;
711 String nameString(decl.fVar->fName);
Ethan Nicholas82399462017-10-16 12:35:44 -0400712 const char* name = nameString.c_str();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000713 if (SectionAndParameterHelper::IsParameter(*decl.fVar) &&
714 is_accessible(*decl.fVar)) {
Ethan Nicholas82399462017-10-16 12:35:44 -0400715 this->writef(" auto %s = _outer.%s();\n"
716 " (void) %s;\n",
717 name, name, name);
718 }
719 }
720 }
721 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400722 this->writePrivateVarValues();
723 for (const auto u : uniforms) {
724 this->addUniform(*u);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400725 }
726 this->writeSection(EMIT_CODE_SECTION);
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400727
728 // Save original buffer as the CPP buffer for flushEmittedCode()
729 fCPPBuffer = fOut;
730 StringStream skslBuffer;
731 fOut = &skslBuffer;
732
Ethan Nicholas762466e2017-06-29 10:03:38 -0400733 bool result = INHERITED::generateCode();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400734 // Final flush in case there is anything extra, forcing it to emit everything
735 this->flushEmittedCode(true);
736
737 // Then restore the original CPP buffer and close the function
738 fOut = fCPPBuffer;
739 fCPPBuffer = nullptr;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400740 this->write(" }\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400741 return result;
742}
743
744void CPPCodeGenerator::writeSetData(std::vector<const Variable*>& uniforms) {
745 const char* fullName = fFullName.c_str();
Ethan Nicholas68990be2017-07-13 09:36:52 -0400746 const Section* section = fSectionAndParameterHelper.getSection(SET_DATA_SECTION);
747 const char* pdman = section ? section->fArgument.c_str() : "pdman";
Ethan Nicholas762466e2017-06-29 10:03:38 -0400748 this->writef(" void onSetData(const GrGLSLProgramDataManager& %s, "
749 "const GrFragmentProcessor& _proc) override {\n",
750 pdman);
751 bool wroteProcessor = false;
752 for (const auto u : uniforms) {
Michael Ludwiga4275592018-08-31 10:52:47 -0400753 if (is_uniform_in(*u)) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400754 if (!wroteProcessor) {
755 this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName, fullName);
756 wroteProcessor = true;
757 this->writef(" {\n");
758 }
Michael Ludwiga4275592018-08-31 10:52:47 -0400759
760 const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *u);
761 SkASSERT(mapper);
762
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700763 String nameString(u->fName);
764 const char* name = nameString.c_str();
Michael Ludwiga4275592018-08-31 10:52:47 -0400765
766 // Switches for setData behavior in the generated code
767 bool conditionalUniform = u->fModifiers.fLayout.fWhen != "";
768 bool isTracked = u->fModifiers.fLayout.fFlags & Layout::kTracked_Flag;
769 bool needsValueDeclaration = isTracked || !mapper->canInlineUniformValue();
770
771 String uniformName = HCodeGenerator::FieldName(name) + "Var";
772
773 String indent = " "; // 8 by default, 12 when nested for conditional uniforms
774 if (conditionalUniform) {
775 // Add a pre-check to make sure the uniform was emitted
776 // before trying to send any data to the GPU
777 this->writef(" if (%s.isValid()) {\n", uniformName.c_str());
778 indent += " ";
779 }
780
781 String valueVar = "";
782 if (needsValueDeclaration) {
783 valueVar.appendf("%sValue", name);
784 // Use AccessType since that will match the return type of _outer's public API.
785 String valueType = HCodeGenerator::AccessType(fContext, u->fType,
786 u->fModifiers.fLayout);
787 this->writef("%s%s %s = _outer.%s();\n",
788 indent.c_str(), valueType.c_str(), valueVar.c_str(), name);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400789 } else {
Michael Ludwiga4275592018-08-31 10:52:47 -0400790 // Not tracked and the mapper only needs to use the value once
791 // so send it a safe expression instead of the variable name
792 valueVar.appendf("(_outer.%s())", name);
793 }
794
795 if (isTracked) {
796 SkASSERT(mapper->supportsTracking());
797
798 String prevVar = HCodeGenerator::FieldName(name) + "Prev";
799 this->writef("%sif (%s) {\n"
800 "%s %s;\n"
801 "%s %s;\n"
802 "%s}\n", indent.c_str(),
803 mapper->dirtyExpression(valueVar, prevVar).c_str(), indent.c_str(),
804 mapper->saveState(valueVar, prevVar).c_str(), indent.c_str(),
805 mapper->setUniform(pdman, uniformName, valueVar).c_str(), indent.c_str());
806 } else {
807 this->writef("%s%s;\n", indent.c_str(),
808 mapper->setUniform(pdman, uniformName, valueVar).c_str());
809 }
810
811 if (conditionalUniform) {
812 // Close the earlier precheck block
813 this->writef(" }\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400814 }
815 }
816 }
817 if (wroteProcessor) {
818 this->writef(" }\n");
819 }
Ethan Nicholas68990be2017-07-13 09:36:52 -0400820 if (section) {
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500821 int samplerIndex = 0;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400822 for (const auto& p : fProgram) {
823 if (ProgramElement::kVar_Kind == p.fKind) {
824 const VarDeclarations& decls = (const VarDeclarations&) p;
825 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000826 VarDeclaration& decl = (VarDeclaration&) *raw;
827 String nameString(decl.fVar->fName);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700828 const char* name = nameString.c_str();
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500829 if (decl.fVar->fType.kind() == Type::kSampler_Kind) {
830 this->writef(" GrSurfaceProxy& %sProxy = "
831 "*_outer.textureSampler(%d).proxy();\n",
832 name, samplerIndex);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400833 this->writef(" GrTexture& %s = *%sProxy.peekTexture();\n",
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500834 name, name);
835 this->writef(" (void) %s;\n", name);
836 ++samplerIndex;
837 } else if (needs_uniform_var(*decl.fVar)) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400838 this->writef(" UniformHandle& %s = %sVar;\n"
839 " (void) %s;\n",
840 name, HCodeGenerator::FieldName(name).c_str(), name);
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000841 } else if (SectionAndParameterHelper::IsParameter(*decl.fVar) &&
842 decl.fVar->fType != *fContext.fFragmentProcessor_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400843 if (!wroteProcessor) {
844 this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName,
845 fullName);
846 wroteProcessor = true;
847 }
848 this->writef(" auto %s = _outer.%s();\n"
849 " (void) %s;\n",
850 name, name, name);
851 }
852 }
853 }
854 }
855 this->writeSection(SET_DATA_SECTION);
856 }
857 this->write(" }\n");
858}
859
Brian Salomonf7dcd762018-07-30 14:48:15 -0400860void CPPCodeGenerator::writeOnTextureSampler() {
861 bool foundSampler = false;
862 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
863 if (param->fType.kind() == Type::kSampler_Kind) {
864 if (!foundSampler) {
865 this->writef(
866 "const GrFragmentProcessor::TextureSampler& %s::onTextureSampler(int "
867 "index) const {\n",
868 fFullName.c_str());
869 this->writef(" return IthTextureSampler(index, %s",
870 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
871 foundSampler = true;
872 } else {
873 this->writef(", %s",
874 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
875 }
876 }
877 }
878 if (foundSampler) {
879 this->write(");\n}\n");
880 }
881}
882
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400883void CPPCodeGenerator::writeClone() {
884 if (!this->writeSection(CLONE_SECTION)) {
885 if (fSectionAndParameterHelper.getSection(FIELDS_SECTION)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700886 fErrors.error(0, "fragment processors with custom @fields must also have a custom"
887 "@clone");
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400888 }
889 this->writef("%s::%s(const %s& src)\n"
Ethan Nicholasabff9562017-10-09 10:54:08 -0400890 ": INHERITED(k%s_ClassID, src.optimizationFlags())", fFullName.c_str(),
891 fFullName.c_str(), fFullName.c_str(), fFullName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400892 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400893 if (param->fType == *fContext.fFragmentProcessor_Type) {
894 continue;
895 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700896 String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str());
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400897 this->writef("\n, %s(src.%s)",
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400898 fieldName.c_str(),
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400899 fieldName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400900 }
Ethan Nicholas929a6812018-08-06 14:56:59 -0400901 const auto transforms = fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION);
902 for (size_t i = 0; i < transforms.size(); ++i) {
903 const Section& s = *transforms[i];
904 String fieldName = HCodeGenerator::CoordTransformName(s.fArgument, i);
905 this->writef("\n, %s(src.%s)", fieldName.c_str(), fieldName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400906 }
Ethan Nicholasabff9562017-10-09 10:54:08 -0400907 this->writef(" {\n");
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400908 int childCount = 0;
Brian Salomonf7dcd762018-07-30 14:48:15 -0400909 int samplerCount = 0;
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400910 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
911 if (param->fType.kind() == Type::kSampler_Kind) {
Brian Salomonf7dcd762018-07-30 14:48:15 -0400912 ++samplerCount;
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400913 } else if (param->fType == *fContext.fFragmentProcessor_Type) {
914 this->writef(" this->registerChildProcessor(src.childProcessor(%d).clone());"
915 "\n", childCount++);
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400916 }
917 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400918 if (samplerCount) {
919 this->writef(" this->setTextureSamplerCnt(%d);", samplerCount);
920 }
Ethan Nicholas929a6812018-08-06 14:56:59 -0400921 for (size_t i = 0; i < transforms.size(); ++i) {
922 const Section& s = *transforms[i];
923 String fieldName = HCodeGenerator::CoordTransformName(s.fArgument, i);
924 this->writef(" this->addCoordTransform(&%s);\n", fieldName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400925 }
926 this->write("}\n");
Brian Salomonaff329b2017-08-11 09:40:37 -0400927 this->writef("std::unique_ptr<GrFragmentProcessor> %s::clone() const {\n",
928 fFullName.c_str());
929 this->writef(" return std::unique_ptr<GrFragmentProcessor>(new %s(*this));\n",
930 fFullName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400931 this->write("}\n");
932 }
933}
934
Ethan Nicholas762466e2017-06-29 10:03:38 -0400935void CPPCodeGenerator::writeTest() {
Ethan Nicholas68990be2017-07-13 09:36:52 -0400936 const Section* test = fSectionAndParameterHelper.getSection(TEST_CODE_SECTION);
937 if (test) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400938 this->writef(
939 "GR_DEFINE_FRAGMENT_PROCESSOR_TEST(%s);\n"
940 "#if GR_TEST_UTILS\n"
941 "std::unique_ptr<GrFragmentProcessor> %s::TestCreate(GrProcessorTestData* %s) {\n",
942 fFullName.c_str(),
943 fFullName.c_str(),
944 test->fArgument.c_str());
Ethan Nicholas68990be2017-07-13 09:36:52 -0400945 this->writeSection(TEST_CODE_SECTION);
946 this->write("}\n"
947 "#endif\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400948 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400949}
950
951void CPPCodeGenerator::writeGetKey() {
952 this->writef("void %s::onGetGLSLProcessorKey(const GrShaderCaps& caps, "
953 "GrProcessorKeyBuilder* b) const {\n",
954 fFullName.c_str());
Ethan Nicholas68990be2017-07-13 09:36:52 -0400955 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700956 String nameString(param->fName);
957 const char* name = nameString.c_str();
Ethan Nicholas762466e2017-06-29 10:03:38 -0400958 if (param->fModifiers.fLayout.fKey != Layout::kNo_Key &&
959 (param->fModifiers.fFlags & Modifiers::kUniform_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700960 fErrors.error(param->fOffset,
Ethan Nicholas762466e2017-06-29 10:03:38 -0400961 "layout(key) may not be specified on uniforms");
962 }
963 switch (param->fModifiers.fLayout.fKey) {
964 case Layout::kKey_Key:
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400965 if (param->fType == *fContext.fFloat4x4_Type) {
966 ABORT("no automatic key handling for float4x4\n");
967 } else if (param->fType == *fContext.fFloat2_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400968 this->writef(" b->add32(%s.fX);\n",
969 HCodeGenerator::FieldName(name).c_str());
970 this->writef(" b->add32(%s.fY);\n",
971 HCodeGenerator::FieldName(name).c_str());
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400972 } else if (param->fType == *fContext.fFloat4_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400973 this->writef(" b->add32(%s.x());\n",
974 HCodeGenerator::FieldName(name).c_str());
975 this->writef(" b->add32(%s.y());\n",
976 HCodeGenerator::FieldName(name).c_str());
977 this->writef(" b->add32(%s.width());\n",
978 HCodeGenerator::FieldName(name).c_str());
979 this->writef(" b->add32(%s.height());\n",
980 HCodeGenerator::FieldName(name).c_str());
981 } else {
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500982 this->writef(" b->add32((int32_t) %s);\n",
Ethan Nicholas762466e2017-06-29 10:03:38 -0400983 HCodeGenerator::FieldName(name).c_str());
984 }
985 break;
986 case Layout::kIdentity_Key:
987 if (param->fType.kind() != Type::kMatrix_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700988 fErrors.error(param->fOffset,
Ethan Nicholas762466e2017-06-29 10:03:38 -0400989 "layout(key=identity) requires matrix type");
990 }
991 this->writef(" b->add32(%s.isIdentity() ? 1 : 0);\n",
992 HCodeGenerator::FieldName(name).c_str());
993 break;
994 case Layout::kNo_Key:
995 break;
996 }
997 }
998 this->write("}\n");
999}
1000
1001bool CPPCodeGenerator::generateCode() {
1002 std::vector<const Variable*> uniforms;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001003 for (const auto& p : fProgram) {
1004 if (ProgramElement::kVar_Kind == p.fKind) {
1005 const VarDeclarations& decls = (const VarDeclarations&) p;
1006 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001007 VarDeclaration& decl = (VarDeclaration&) *raw;
1008 if ((decl.fVar->fModifiers.fFlags & Modifiers::kUniform_Flag) &&
1009 decl.fVar->fType.kind() != Type::kSampler_Kind) {
1010 uniforms.push_back(decl.fVar);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001011 }
Michael Ludwiga4275592018-08-31 10:52:47 -04001012
1013 if (is_uniform_in(*decl.fVar)) {
1014 // Validate the "uniform in" declarations to make sure they are fully supported,
1015 // instead of generating surprising C++
1016 const UniformCTypeMapper* mapper =
1017 UniformCTypeMapper::Get(fContext, *decl.fVar);
1018 if (mapper == nullptr) {
1019 fErrors.error(decl.fOffset, String(decl.fVar->fName)
1020 + "'s type is not supported for use as a 'uniform in'");
1021 return false;
1022 }
1023 if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
1024 if (!mapper->supportsTracking()) {
1025 fErrors.error(decl.fOffset, String(decl.fVar->fName)
1026 + "'s type does not support state tracking");
1027 return false;
1028 }
1029 }
1030
1031 } else {
1032 // If it's not a uniform_in, it's an error to be tracked
1033 if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
1034 fErrors.error(decl.fOffset, "Non-'in uniforms' cannot be tracked");
1035 return false;
1036 }
1037 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001038 }
1039 }
1040 }
1041 const char* baseName = fName.c_str();
1042 const char* fullName = fFullName.c_str();
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05001043 this->writef("%s\n", HCodeGenerator::GetHeader(fProgram, fErrors).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001044 this->writef(kFragmentProcessorHeader, fullName);
Greg Daniel3e8c3452018-04-06 10:37:55 -04001045 this->writef("#include \"%s.h\"\n", fullName);
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04001046 this->writeSection(CPP_SECTION);
Brian Osman1cb41712017-10-19 12:54:52 -04001047 this->writef("#include \"glsl/GrGLSLFragmentProcessor.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -04001048 "#include \"glsl/GrGLSLFragmentShaderBuilder.h\"\n"
1049 "#include \"glsl/GrGLSLProgramBuilder.h\"\n"
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05001050 "#include \"GrTexture.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -04001051 "#include \"SkSLCPP.h\"\n"
1052 "#include \"SkSLUtil.h\"\n"
1053 "class GrGLSL%s : public GrGLSLFragmentProcessor {\n"
1054 "public:\n"
1055 " GrGLSL%s() {}\n",
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04001056 baseName, baseName);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001057 bool result = this->writeEmitCode(uniforms);
1058 this->write("private:\n");
1059 this->writeSetData(uniforms);
1060 this->writePrivateVars();
1061 for (const auto& u : uniforms) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001062 if (needs_uniform_var(*u) && !(u->fModifiers.fFlags & Modifiers::kIn_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001063 this->writef(" UniformHandle %sVar;\n",
1064 HCodeGenerator::FieldName(String(u->fName).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001065 }
1066 }
Ethan Nicholas68990be2017-07-13 09:36:52 -04001067 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001068 if (needs_uniform_var(*param)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001069 this->writef(" UniformHandle %sVar;\n",
1070 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001071 }
1072 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001073 this->writef("};\n"
1074 "GrGLSLFragmentProcessor* %s::onCreateGLSLInstance() const {\n"
1075 " return new GrGLSL%s();\n"
1076 "}\n",
1077 fullName, baseName);
1078 this->writeGetKey();
1079 this->writef("bool %s::onIsEqual(const GrFragmentProcessor& other) const {\n"
1080 " const %s& that = other.cast<%s>();\n"
1081 " (void) that;\n",
1082 fullName, fullName, fullName);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001083 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -04001084 if (param->fType == *fContext.fFragmentProcessor_Type) {
1085 continue;
1086 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001087 String nameString(param->fName);
1088 const char* name = nameString.c_str();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001089 this->writef(" if (%s != that.%s) return false;\n",
1090 HCodeGenerator::FieldName(name).c_str(),
1091 HCodeGenerator::FieldName(name).c_str());
1092 }
1093 this->write(" return true;\n"
1094 "}\n");
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001095 this->writeClone();
Brian Salomonf7dcd762018-07-30 14:48:15 -04001096 this->writeOnTextureSampler();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001097 this->writeTest();
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04001098 this->writeSection(CPP_END_SECTION);
Greg Daniel3e8c3452018-04-06 10:37:55 -04001099
Ethan Nicholas762466e2017-06-29 10:03:38 -04001100 result &= 0 == fErrors.errorCount();
1101 return result;
1102}
1103
1104} // namespace