blob: 237148a8b7ee2a52a7b2954dd23205a5dca659de [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()) {
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400100 addExtraEmitCodeLine("SkString " + name +
101 " = fragBuilder->ensureCoords2D(args.fTransformedCoords[" +
102 to_string(index) + "]);");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400103 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) {
Brian Osman495993a2018-10-16 15:45:55 -0400135 if (var.fModifiers.fLayout.fCType == SkSL::Layout::CType::kSkPMColor4f) {
Brian Osmanf28e55d2018-10-03 16:35:54 -0400136 return "{SK_FloatNaN, SK_FloatNaN, SK_FloatNaN, SK_FloatNaN}";
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500137 }
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 Nicholas78aceb22018-08-31 16:13:58 -0400171 switch (layout.fCType) {
172 case Layout::CType::kSkPMColor:
173 fFormatArgs.push_back("SkGetPackedR32(" + cppCode + ") / 255.0");
174 fFormatArgs.push_back("SkGetPackedG32(" + cppCode + ") / 255.0");
175 fFormatArgs.push_back("SkGetPackedB32(" + cppCode + ") / 255.0");
176 fFormatArgs.push_back("SkGetPackedA32(" + cppCode + ") / 255.0");
177 break;
Brian Osmanf28e55d2018-10-03 16:35:54 -0400178 case Layout::CType::kSkPMColor4f:
179 fFormatArgs.push_back(cppCode + ".fR");
180 fFormatArgs.push_back(cppCode + ".fG");
181 fFormatArgs.push_back(cppCode + ".fB");
182 fFormatArgs.push_back(cppCode + ".fA");
183 break;
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400184 case Layout::CType::kSkRect: // fall through
185 case Layout::CType::kDefault:
186 fFormatArgs.push_back(cppCode + ".left()");
187 fFormatArgs.push_back(cppCode + ".top()");
188 fFormatArgs.push_back(cppCode + ".right()");
189 fFormatArgs.push_back(cppCode + ".bottom()");
190 break;
191 default:
192 SkASSERT(false);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400193 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500194 } else if (type.kind() == Type::kEnum_Kind) {
195 this->write("%d");
196 fFormatArgs.push_back("(int) " + cppCode);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400197 } else if (type == *fContext.fInt4_Type ||
198 type == *fContext.fShort4_Type ||
199 type == *fContext.fByte4_Type) {
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500200 this->write(type.name() + "(%d, %d, %d, %d)");
201 fFormatArgs.push_back(cppCode + ".left()");
202 fFormatArgs.push_back(cppCode + ".top()");
203 fFormatArgs.push_back(cppCode + ".right()");
204 fFormatArgs.push_back(cppCode + ".bottom()");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400205 } else {
Ethan Nicholas82399462017-10-16 12:35:44 -0400206 printf("unsupported runtime value type '%s'\n", String(type.fName).c_str());
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400207 SkASSERT(false);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400208 }
209}
210
211void CPPCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) {
212 if (is_private(var)) {
Ethan Nicholasd608c092017-10-26 09:30:08 -0400213 this->writeRuntimeValue(var.fType, var.fModifiers.fLayout, var.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400214 } else {
215 this->writeExpression(value, kTopLevel_Precedence);
216 }
217}
218
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400219String CPPCodeGenerator::getSamplerHandle(const Variable& var) {
220 int samplerCount = 0;
Ethan Nicholas68990be2017-07-13 09:36:52 -0400221 for (const auto param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400222 if (&var == param) {
223 return "args.fTexSamplers[" + to_string(samplerCount) + "]";
224 }
225 if (param->fType.kind() == Type::kSampler_Kind) {
226 ++samplerCount;
227 }
228 }
229 ABORT("should have found sampler in parameters\n");
230}
231
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400232void CPPCodeGenerator::writeIntLiteral(const IntLiteral& i) {
233 this->write(to_string((int32_t) i.fValue));
234}
235
Ethan Nicholas82399462017-10-16 12:35:44 -0400236void CPPCodeGenerator::writeSwizzle(const Swizzle& swizzle) {
237 if (fCPPMode) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400238 SkASSERT(swizzle.fComponents.size() == 1); // no support for multiple swizzle components yet
Ethan Nicholas82399462017-10-16 12:35:44 -0400239 this->writeExpression(*swizzle.fBase, kPostfix_Precedence);
240 switch (swizzle.fComponents[0]) {
241 case 0: this->write(".left()"); break;
242 case 1: this->write(".top()"); break;
243 case 2: this->write(".right()"); break;
244 case 3: this->write(".bottom()"); break;
245 }
246 } else {
247 INHERITED::writeSwizzle(swizzle);
248 }
249}
250
Ethan Nicholas762466e2017-06-29 10:03:38 -0400251void CPPCodeGenerator::writeVariableReference(const VariableReference& ref) {
Ethan Nicholas82399462017-10-16 12:35:44 -0400252 if (fCPPMode) {
253 this->write(ref.fVariable.fName);
254 return;
255 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400256 switch (ref.fVariable.fModifiers.fLayout.fBuiltin) {
257 case SK_INCOLOR_BUILTIN:
258 this->write("%s");
Michael Ludwig231de032018-08-30 14:33:01 -0400259 // EmitArgs.fInputColor is automatically set to half4(1) if
260 // no input was specified
261 fFormatArgs.push_back(String("args.fInputColor"));
Ethan Nicholas762466e2017-06-29 10:03:38 -0400262 break;
263 case SK_OUTCOLOR_BUILTIN:
264 this->write("%s");
265 fFormatArgs.push_back(String("args.fOutputColor"));
266 break;
Ethan Nicholascd700e92018-08-24 16:43:57 -0400267 case SK_WIDTH_BUILTIN:
268 this->write("sk_Width");
269 break;
270 case SK_HEIGHT_BUILTIN:
271 this->write("sk_Height");
272 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400273 default:
274 if (ref.fVariable.fType.kind() == Type::kSampler_Kind) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400275 this->write("%s");
276 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerVariable(" +
277 this->getSamplerHandle(ref.fVariable) + ").c_str()");
278 return;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400279 }
280 if (ref.fVariable.fModifiers.fFlags & Modifiers::kUniform_Flag) {
281 this->write("%s");
282 String name = ref.fVariable.fName;
Brian Osman1cb41712017-10-19 12:54:52 -0400283 String var = String::printf("args.fUniformHandler->getUniformCStr(%sVar)",
284 HCodeGenerator::FieldName(name.c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400285 String code;
286 if (ref.fVariable.fModifiers.fLayout.fWhen.size()) {
287 code = String::printf("%sVar.isValid() ? %s : \"%s\"",
288 HCodeGenerator::FieldName(name.c_str()).c_str(),
289 var.c_str(),
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400290 default_value(ref.fVariable.fType).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400291 } else {
292 code = var;
293 }
294 fFormatArgs.push_back(code);
295 } else if (SectionAndParameterHelper::IsParameter(ref.fVariable)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700296 String name(ref.fVariable.fName);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400297 this->writeRuntimeValue(ref.fVariable.fType, ref.fVariable.fModifiers.fLayout,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700298 String::printf("_outer.%s()", name.c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400299 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700300 this->write(ref.fVariable.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400301 }
302 }
303}
304
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400305void CPPCodeGenerator::writeIfStatement(const IfStatement& s) {
306 if (s.fIsStatic) {
307 this->write("@");
308 }
309 INHERITED::writeIfStatement(s);
310}
311
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400312void CPPCodeGenerator::writeReturnStatement(const ReturnStatement& s) {
313 if (fInMain) {
314 fErrors.error(s.fOffset, "fragmentProcessor main() may not contain return statements");
315 }
316 INHERITED::writeReturnStatement(s);
317}
318
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400319void CPPCodeGenerator::writeSwitchStatement(const SwitchStatement& s) {
320 if (s.fIsStatic) {
321 this->write("@");
322 }
323 INHERITED::writeSwitchStatement(s);
324}
325
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400326void CPPCodeGenerator::writeFieldAccess(const FieldAccess& access) {
327 if (access.fBase->fType.name() == "fragmentProcessor") {
328 // Special field access on fragment processors are converted into function calls on
329 // GrFragmentProcessor's getters.
330 if (access.fBase->fKind != Expression::kVariableReference_Kind) {
331 fErrors.error(access.fBase->fOffset, "fragmentProcessor must be a reference\n");
332 return;
333 }
334
335 const Type::Field& field = fContext.fFragmentProcessor_Type->fields()[access.fFieldIndex];
336 int index = getChildFPIndex((const VariableReference&) *access.fBase);
337 String cppAccess = String::printf("_outer.childProcessor(%s).%s()",
338 to_string(index).c_str(), String(field.fName).c_str());
339
340 if (fCPPMode) {
341 this->write(cppAccess.c_str());
342 } else {
343 writeRuntimeValue(*field.fType, Layout(), cppAccess);
344 }
345 return;
346 }
347 INHERITED::writeFieldAccess(access);
348}
349
350int CPPCodeGenerator::getChildFPIndex(const VariableReference& reference) const {
351 int index = 0;
352 bool found = false;
353 for (const auto& p : fProgram) {
354 if (ProgramElement::kVar_Kind == p.fKind) {
355 const VarDeclarations& decls = (const VarDeclarations&) p;
356 for (const auto& raw : decls.fVars) {
357 const VarDeclaration& decl = (VarDeclaration&) *raw;
358 if (decl.fVar == &reference.fVariable) {
359 found = true;
360 } else if (decl.fVar->fType == *fContext.fFragmentProcessor_Type) {
361 ++index;
362 }
363 }
364 }
365 if (found) {
366 break;
367 }
368 }
369 SkASSERT(found);
370 return index;
371}
372
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400373void CPPCodeGenerator::writeFunctionCall(const FunctionCall& c) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400374 if (c.fFunction.fBuiltin && c.fFunction.fName == "process") {
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400375 // Sanity checks that are detected by function definition in sksl_fp.inc
376 SkASSERT(c.fArguments.size() == 1 || c.fArguments.size() == 2);
377 SkASSERT("fragmentProcessor" == c.fArguments[0]->fType.name());
378
379 // Actually fail during compilation if arguments with valid types are
380 // provided that are not variable references, since process() is a
381 // special function that impacts code emission.
382 if (c.fArguments[0]->fKind != Expression::kVariableReference_Kind) {
383 fErrors.error(c.fArguments[0]->fOffset,
384 "process()'s fragmentProcessor argument must be a variable reference\n");
385 return;
386 }
387 if (c.fArguments.size() > 1) {
388 // Second argument must also be a half4 expression
389 SkASSERT("half4" == c.fArguments[1]->fType.name());
390 }
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400391 int index = getChildFPIndex((const VariableReference&) *c.fArguments[0]);
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400392
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400393 // Start a new extra emit code section so that the emitted child processor can depend on
394 // sksl variables defined in earlier sksl code.
395 this->newExtraEmitCodeBlock();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400396
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400397 // Set to the empty string when no input color parameter should be emitted, which means this
398 // must be properly formatted with a prefixed comma when the parameter should be inserted
399 // into the emitChild() parameter list.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400400 String inputArg;
401 if (c.fArguments.size() > 1) {
402 SkASSERT(c.fArguments.size() == 2);
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400403 // Use the emitChild() variant that accepts an input color, so convert the 2nd
404 // argument's expression into C++ code that produces sksl stored in an SkString.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400405 String inputName = "_input" + to_string(index);
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400406 addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments[1], inputName));
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400407
408 // emitChild() needs a char*
409 inputArg = ", " + inputName + ".c_str()";
410 }
411
412 // Write the output handling after the possible input handling
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400413 String childName = "_child" + to_string(index);
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400414 addExtraEmitCodeLine("SkString " + childName + "(\"" + childName + "\");");
415 addExtraEmitCodeLine("this->emitChild(" + to_string(index) + inputArg +
416 ", &" + childName + ", args);");
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400417
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400418 this->write("%s");
419 fFormatArgs.push_back(childName + ".c_str()");
420 return;
421 }
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400422 INHERITED::writeFunctionCall(c);
423 if (c.fFunction.fBuiltin && c.fFunction.fName == "texture") {
424 this->write(".%s");
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400425 SkASSERT(c.fArguments.size() >= 1);
426 SkASSERT(c.fArguments[0]->fKind == Expression::kVariableReference_Kind);
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400427 String sampler = this->getSamplerHandle(((VariableReference&) *c.fArguments[0]).fVariable);
428 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerSwizzle(" + sampler +
429 ").c_str()");
430 }
431}
432
Ethan Nicholas762466e2017-06-29 10:03:38 -0400433void CPPCodeGenerator::writeFunction(const FunctionDefinition& f) {
434 if (f.fDeclaration.fName == "main") {
435 fFunctionHeader = "";
436 OutputStream* oldOut = fOut;
437 StringStream buffer;
438 fOut = &buffer;
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400439 fInMain = true;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400440 for (const auto& s : ((Block&) *f.fBody).fStatements) {
441 this->writeStatement(*s);
442 this->writeLine();
443 }
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400444 fInMain = false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400445
446 fOut = oldOut;
447 this->write(fFunctionHeader);
448 this->write(buffer.str());
449 } else {
450 INHERITED::writeFunction(f);
451 }
452}
453
454void CPPCodeGenerator::writeSetting(const Setting& s) {
455 static constexpr const char* kPrefix = "sk_Args.";
456 if (!strncmp(s.fName.c_str(), kPrefix, strlen(kPrefix))) {
457 const char* name = s.fName.c_str() + strlen(kPrefix);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400458 this->writeRuntimeValue(s.fType, Layout(), HCodeGenerator::FieldName(name).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400459 } else {
460 this->write(s.fName.c_str());
461 }
462}
463
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400464bool CPPCodeGenerator::writeSection(const char* name, const char* prefix) {
Ethan Nicholas68990be2017-07-13 09:36:52 -0400465 const Section* s = fSectionAndParameterHelper.getSection(name);
466 if (s) {
467 this->writef("%s%s", prefix, s->fText.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400468 return true;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400469 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400470 return false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400471}
472
473void CPPCodeGenerator::writeProgramElement(const ProgramElement& p) {
474 if (p.fKind == ProgramElement::kSection_Kind) {
475 return;
476 }
477 if (p.fKind == ProgramElement::kVar_Kind) {
478 const VarDeclarations& decls = (const VarDeclarations&) p;
479 if (!decls.fVars.size()) {
480 return;
481 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000482 const Variable& var = *((VarDeclaration&) *decls.fVars[0]).fVar;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400483 if (var.fModifiers.fFlags & (Modifiers::kIn_Flag | Modifiers::kUniform_Flag) ||
484 -1 != var.fModifiers.fLayout.fBuiltin) {
485 return;
486 }
487 }
488 INHERITED::writeProgramElement(p);
489}
490
491void CPPCodeGenerator::addUniform(const Variable& var) {
492 if (!needs_uniform_var(var)) {
493 return;
494 }
495 const char* precision;
496 if (var.fModifiers.fFlags & Modifiers::kHighp_Flag) {
497 precision = "kHigh_GrSLPrecision";
498 } else if (var.fModifiers.fFlags & Modifiers::kMediump_Flag) {
499 precision = "kMedium_GrSLPrecision";
500 } else if (var.fModifiers.fFlags & Modifiers::kLowp_Flag) {
501 precision = "kLow_GrSLPrecision";
502 } else {
503 precision = "kDefault_GrSLPrecision";
504 }
505 const char* type;
506 if (var.fType == *fContext.fFloat_Type) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400507 type = "kFloat_GrSLType";
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400508 } else if (var.fType == *fContext.fHalf_Type) {
509 type = "kHalf_GrSLType";
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400510 } else if (var.fType == *fContext.fFloat2_Type) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400511 type = "kFloat2_GrSLType";
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400512 } else if (var.fType == *fContext.fHalf2_Type) {
513 type = "kHalf2_GrSLType";
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400514 } else if (var.fType == *fContext.fFloat4_Type) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400515 type = "kFloat4_GrSLType";
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400516 } else if (var.fType == *fContext.fHalf4_Type) {
517 type = "kHalf4_GrSLType";
Brian Osman1cb41712017-10-19 12:54:52 -0400518 } else if (var.fType == *fContext.fFloat4x4_Type) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400519 type = "kFloat4x4_GrSLType";
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400520 } else if (var.fType == *fContext.fHalf4x4_Type) {
521 type = "kHalf4x4_GrSLType";
Ethan Nicholas762466e2017-06-29 10:03:38 -0400522 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700523 ABORT("unsupported uniform type: %s %s;\n", String(var.fType.fName).c_str(),
524 String(var.fName).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400525 }
526 if (var.fModifiers.fLayout.fWhen.size()) {
527 this->writef(" if (%s) {\n ", var.fModifiers.fLayout.fWhen.c_str());
528 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700529 String name(var.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400530 this->writef(" %sVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, %s, "
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700531 "%s, \"%s\");\n", HCodeGenerator::FieldName(name.c_str()).c_str(), type, precision,
532 name.c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400533 if (var.fModifiers.fLayout.fWhen.size()) {
534 this->write(" }\n");
535 }
536}
537
Ethan Nicholascd700e92018-08-24 16:43:57 -0400538void CPPCodeGenerator::writeInputVars() {
539}
540
Ethan Nicholas762466e2017-06-29 10:03:38 -0400541void CPPCodeGenerator::writePrivateVars() {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400542 for (const auto& p : fProgram) {
543 if (ProgramElement::kVar_Kind == p.fKind) {
544 const VarDeclarations& decls = (const VarDeclarations&) p;
545 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000546 VarDeclaration& decl = (VarDeclaration&) *raw;
547 if (is_private(*decl.fVar)) {
548 if (decl.fVar->fType == *fContext.fFragmentProcessor_Type) {
549 fErrors.error(decl.fOffset,
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400550 "fragmentProcessor variables must be declared 'in'");
551 return;
552 }
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500553 this->writef("%s %s = %s;\n",
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000554 HCodeGenerator::FieldType(fContext, decl.fVar->fType,
555 decl.fVar->fModifiers.fLayout).c_str(),
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500556 String(decl.fVar->fName).c_str(),
557 default_value(*decl.fVar).c_str());
Michael Ludwiga4275592018-08-31 10:52:47 -0400558 } else if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
559 // An auto-tracked uniform in variable, so add a field to hold onto the prior
560 // state. Note that tracked variables must be uniform in's and that is validated
561 // before writePrivateVars() is called.
562 const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *decl.fVar);
563 SkASSERT(mapper && mapper->supportsTracking());
564
565 String name = HCodeGenerator::FieldName(String(decl.fVar->fName).c_str());
566 // The member statement is different if the mapper reports a default value
567 if (mapper->defaultValue().size() > 0) {
568 this->writef("%s %sPrev = %s;\n",
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400569 Layout::CTypeToStr(mapper->ctype()), name.c_str(),
Michael Ludwiga4275592018-08-31 10:52:47 -0400570 mapper->defaultValue().c_str());
571 } else {
572 this->writef("%s %sPrev;\n",
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400573 Layout::CTypeToStr(mapper->ctype()), name.c_str());
Michael Ludwiga4275592018-08-31 10:52:47 -0400574 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400575 }
576 }
577 }
578 }
579}
580
581void CPPCodeGenerator::writePrivateVarValues() {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400582 for (const auto& p : fProgram) {
583 if (ProgramElement::kVar_Kind == p.fKind) {
584 const VarDeclarations& decls = (const VarDeclarations&) p;
585 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000586 VarDeclaration& decl = (VarDeclaration&) *raw;
587 if (is_private(*decl.fVar) && decl.fValue) {
588 this->writef("%s = ", String(decl.fVar->fName).c_str());
Ethan Nicholas82399462017-10-16 12:35:44 -0400589 fCPPMode = true;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000590 this->writeExpression(*decl.fValue, kAssignment_Precedence);
Ethan Nicholas82399462017-10-16 12:35:44 -0400591 fCPPMode = false;
592 this->write(";\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400593 }
594 }
595 }
596 }
597}
598
Ethan Nicholas82399462017-10-16 12:35:44 -0400599static bool is_accessible(const Variable& var) {
600 return Type::kSampler_Kind != var.fType.kind() &&
601 Type::kOther_Kind != var.fType.kind();
602}
603
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400604void CPPCodeGenerator::newExtraEmitCodeBlock() {
605 // This should only be called when emitting SKSL for emitCode(), which can be detected if the
606 // cpp buffer is not null, and the cpp buffer is not the current output.
607 SkASSERT(fCPPBuffer && fCPPBuffer != fOut);
608
609 // Start a new block as an empty string
610 fExtraEmitCodeBlocks.push_back("");
611 // Mark its location in the output buffer, uses ${\d} for the token since ${} will not occur in
612 // valid sksl and makes detection trivial.
613 this->writef("${%zu}", fExtraEmitCodeBlocks.size() - 1);
614}
615
616void CPPCodeGenerator::addExtraEmitCodeLine(const String& toAppend) {
617 SkASSERT(fExtraEmitCodeBlocks.size() > 0);
618 String& currentBlock = fExtraEmitCodeBlocks[fExtraEmitCodeBlocks.size() - 1];
619 // Automatically add indentation and newline
620 currentBlock += " " + toAppend + "\n";
621}
622
623void CPPCodeGenerator::flushEmittedCode() {
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400624 if (fCPPBuffer == nullptr) {
625 // Not actually within writeEmitCode() so nothing to flush
626 return;
627 }
628
629 StringStream* skslBuffer = static_cast<StringStream*>(fOut);
630
631 String sksl = skslBuffer->str();
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400632 // Empty the accumulation buffer since its current contents are consumed.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400633 skslBuffer->reset();
634
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400635 // Switch to the cpp buffer
Michael Ludwigd0440192018-09-07 14:24:52 +0000636 fOut = fCPPBuffer;
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400637
638 // Iterate through the sksl, keeping track of where the last statement ended (e.g. the latest
639 // encountered ';', '{', or '}'). If an extra emit code block token is encountered then the
640 // code from 0 to last statement end is sent to writeCodeAppend, the extra code block is
641 // appended to the cpp buffer, and then the sksl string is trimmed to start where the last
642 // statement left off (minus the encountered token).
643 size_t i = 0;
644 int flushPoint = -1;
645 int tokenStart = -1;
646 while (i < sksl.size()) {
647 if (tokenStart >= 0) {
648 // Looking for the end of the token
649 if (sksl[i] == '}') {
650 // Must append the sksl from 0 to flushPoint (inclusive) then the extra code
651 // accumulated in the block with index parsed from chars [tokenStart+2, i-1]
652 String toFlush = String(sksl.c_str(), flushPoint + 1);
653 // writeCodeAppend automatically removes the format args that it consumed, so
654 // fFormatArgs will be in a valid state for any future sksl
655 this->writeCodeAppend(toFlush);
656
657 int codeBlock = stoi(String(sksl.c_str() + tokenStart + 2, i - tokenStart - 2));
658 SkASSERT(codeBlock < (int) fExtraEmitCodeBlocks.size());
659 if (fExtraEmitCodeBlocks[codeBlock].size() > 0) {
660 this->write(fExtraEmitCodeBlocks[codeBlock].c_str());
661 }
662
663 // Now reset the sksl buffer to start after the flush point, but remove the token.
664 String compacted = String(sksl.c_str() + flushPoint + 1,
665 tokenStart - flushPoint - 1);
666 if (i < sksl.size() - 1) {
667 compacted += String(sksl.c_str() + i + 1, sksl.size() - i - 1);
668 }
669 sksl = compacted;
670
671 // And reset iteration
672 i = -1;
673 flushPoint = -1;
674 tokenStart = -1;
675 }
676 } else {
677 // Looking for the start of extra emit block tokens, and tracking when statements end
678 if (sksl[i] == ';' || sksl[i] == '{' || sksl[i] == '}') {
679 flushPoint = i;
680 } else if (i < sksl.size() - 1 && sksl[i] == '$' && sksl[i + 1] == '{') {
681 // found an extra emit code block token
682 tokenStart = i++;
683 }
684 }
685 i++;
Michael Ludwigd0440192018-09-07 14:24:52 +0000686 }
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400687
688 // Once we've gone through the sksl string to this point, there are no remaining extra emit
689 // code blocks to interleave, so append the remainder as usual.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400690 this->writeCodeAppend(sksl);
691
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400692 // After appending, switch back to the emptied sksl buffer and reset the extra code blocks
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400693 fOut = skslBuffer;
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400694 fExtraEmitCodeBlocks.clear();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400695}
696
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400697void CPPCodeGenerator::writeCodeAppend(const String& code) {
698 // codeAppendf can only handle appending 1024 bytes at a time, so we need to break the string
699 // into chunks. Unfortunately we can't tell exactly how long the string is going to end up,
700 // because printf escape sequences get replaced by strings of unknown length, but keeping the
701 // format string below 512 bytes is probably safe.
702 static constexpr size_t maxChunkSize = 512;
703 size_t start = 0;
704 size_t index = 0;
705 size_t argStart = 0;
706 size_t argCount;
707 while (index < code.size()) {
708 argCount = 0;
709 this->write(" fragBuilder->codeAppendf(\"");
710 while (index < code.size() && index < start + maxChunkSize) {
711 if ('%' == code[index]) {
712 if (index == start + maxChunkSize - 1 || index == code.size() - 1) {
713 break;
714 }
715 if (code[index + 1] != '%') {
716 ++argCount;
717 }
Ethan Nicholasef0c9fd2017-10-30 10:04:14 -0400718 } else if ('\\' == code[index] && index == start + maxChunkSize - 1) {
719 // avoid splitting an escape sequence that happens to fall across a chunk boundary
720 break;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400721 }
722 ++index;
723 }
724 fOut->write(code.c_str() + start, index - start);
725 this->write("\"");
726 for (size_t i = argStart; i < argStart + argCount; ++i) {
727 this->writef(", %s", fFormatArgs[i].c_str());
728 }
729 this->write(");\n");
730 argStart += argCount;
731 start = index;
732 }
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400733
734 // argStart is equal to the number of fFormatArgs that were consumed
735 // so they should be removed from the list
736 if (argStart > 0) {
737 fFormatArgs.erase(fFormatArgs.begin(), fFormatArgs.begin() + argStart);
738 }
739}
740
741String CPPCodeGenerator::convertSKSLExpressionToCPP(const Expression& e,
742 const String& cppVar) {
743 // To do this conversion, we temporarily switch the sksl output stream
744 // to an empty stringstream and reset the format args to empty.
745 OutputStream* oldSKSL = fOut;
746 StringStream exprBuffer;
747 fOut = &exprBuffer;
748
749 std::vector<String> oldArgs(fFormatArgs);
750 fFormatArgs.clear();
751
752 // Convert the argument expression into a format string and args
753 this->writeExpression(e, Precedence::kTopLevel_Precedence);
754 std::vector<String> newArgs(fFormatArgs);
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400755 String expr = exprBuffer.str();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400756
757 // After generating, restore the original output stream and format args
758 fFormatArgs = oldArgs;
759 fOut = oldSKSL;
760
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400761 // The sksl written to exprBuffer is not processed by flushEmittedCode(), so any extra emit code
762 // block tokens won't get handled. So we need to strip them from the expression and stick them
763 // to the end of the original sksl stream.
764 String exprFormat = "";
765 int tokenStart = -1;
766 for (size_t i = 0; i < expr.size(); i++) {
767 if (tokenStart >= 0) {
768 if (expr[i] == '}') {
769 // End of the token, so append the token to fOut
770 fOut->write(expr.c_str() + tokenStart, i - tokenStart + 1);
771 tokenStart = -1;
772 }
773 } else {
774 if (i < expr.size() - 1 && expr[i] == '$' && expr[i + 1] == '{') {
775 tokenStart = i++;
776 } else {
777 exprFormat += expr[i];
778 }
779 }
780 }
781
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400782 // Now build the final C++ code snippet from the format string and args
783 String cppExpr;
784 if (newArgs.size() == 0) {
785 // This was a static expression, so we can simplify the input
786 // color declaration in the emitted code to just a static string
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400787 cppExpr = "SkString " + cppVar + "(\"" + exprFormat + "\");";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400788 } else {
789 // String formatting must occur dynamically, so have the C++ declaration
790 // use SkStringPrintf with the format args that were accumulated
791 // when the expression was written.
792 cppExpr = "SkString " + cppVar + " = SkStringPrintf(\"" + exprFormat + "\"";
793 for (size_t i = 0; i < newArgs.size(); i++) {
794 cppExpr += ", " + newArgs[i];
795 }
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400796 cppExpr += ");";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400797 }
798 return cppExpr;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400799}
800
Ethan Nicholas762466e2017-06-29 10:03:38 -0400801bool CPPCodeGenerator::writeEmitCode(std::vector<const Variable*>& uniforms) {
802 this->write(" void emitCode(EmitArgs& args) override {\n"
803 " GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;\n");
804 this->writef(" const %s& _outer = args.fFp.cast<%s>();\n"
805 " (void) _outer;\n",
806 fFullName.c_str(), fFullName.c_str());
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400807 for (const auto& p : fProgram) {
808 if (ProgramElement::kVar_Kind == p.fKind) {
809 const VarDeclarations& decls = (const VarDeclarations&) p;
810 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000811 VarDeclaration& decl = (VarDeclaration&) *raw;
812 String nameString(decl.fVar->fName);
Ethan Nicholas82399462017-10-16 12:35:44 -0400813 const char* name = nameString.c_str();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000814 if (SectionAndParameterHelper::IsParameter(*decl.fVar) &&
815 is_accessible(*decl.fVar)) {
Ethan Nicholas82399462017-10-16 12:35:44 -0400816 this->writef(" auto %s = _outer.%s();\n"
817 " (void) %s;\n",
818 name, name, name);
819 }
820 }
821 }
822 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400823 this->writePrivateVarValues();
824 for (const auto u : uniforms) {
825 this->addUniform(*u);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400826 }
827 this->writeSection(EMIT_CODE_SECTION);
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400828
829 // Save original buffer as the CPP buffer for flushEmittedCode()
830 fCPPBuffer = fOut;
831 StringStream skslBuffer;
832 fOut = &skslBuffer;
833
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400834 this->newExtraEmitCodeBlock();
Ethan Nicholas762466e2017-06-29 10:03:38 -0400835 bool result = INHERITED::generateCode();
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400836 this->flushEmittedCode();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400837
838 // Then restore the original CPP buffer and close the function
839 fOut = fCPPBuffer;
840 fCPPBuffer = nullptr;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400841 this->write(" }\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400842 return result;
843}
844
845void CPPCodeGenerator::writeSetData(std::vector<const Variable*>& uniforms) {
846 const char* fullName = fFullName.c_str();
Ethan Nicholas68990be2017-07-13 09:36:52 -0400847 const Section* section = fSectionAndParameterHelper.getSection(SET_DATA_SECTION);
848 const char* pdman = section ? section->fArgument.c_str() : "pdman";
Ethan Nicholas762466e2017-06-29 10:03:38 -0400849 this->writef(" void onSetData(const GrGLSLProgramDataManager& %s, "
850 "const GrFragmentProcessor& _proc) override {\n",
851 pdman);
852 bool wroteProcessor = false;
853 for (const auto u : uniforms) {
Michael Ludwiga4275592018-08-31 10:52:47 -0400854 if (is_uniform_in(*u)) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400855 if (!wroteProcessor) {
856 this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName, fullName);
857 wroteProcessor = true;
858 this->writef(" {\n");
859 }
Michael Ludwiga4275592018-08-31 10:52:47 -0400860
861 const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *u);
862 SkASSERT(mapper);
863
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700864 String nameString(u->fName);
865 const char* name = nameString.c_str();
Michael Ludwiga4275592018-08-31 10:52:47 -0400866
867 // Switches for setData behavior in the generated code
868 bool conditionalUniform = u->fModifiers.fLayout.fWhen != "";
869 bool isTracked = u->fModifiers.fLayout.fFlags & Layout::kTracked_Flag;
870 bool needsValueDeclaration = isTracked || !mapper->canInlineUniformValue();
871
872 String uniformName = HCodeGenerator::FieldName(name) + "Var";
873
874 String indent = " "; // 8 by default, 12 when nested for conditional uniforms
875 if (conditionalUniform) {
876 // Add a pre-check to make sure the uniform was emitted
877 // before trying to send any data to the GPU
878 this->writef(" if (%s.isValid()) {\n", uniformName.c_str());
879 indent += " ";
880 }
881
882 String valueVar = "";
883 if (needsValueDeclaration) {
884 valueVar.appendf("%sValue", name);
885 // Use AccessType since that will match the return type of _outer's public API.
886 String valueType = HCodeGenerator::AccessType(fContext, u->fType,
887 u->fModifiers.fLayout);
888 this->writef("%s%s %s = _outer.%s();\n",
889 indent.c_str(), valueType.c_str(), valueVar.c_str(), name);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400890 } else {
Michael Ludwiga4275592018-08-31 10:52:47 -0400891 // Not tracked and the mapper only needs to use the value once
892 // so send it a safe expression instead of the variable name
893 valueVar.appendf("(_outer.%s())", name);
894 }
895
896 if (isTracked) {
897 SkASSERT(mapper->supportsTracking());
898
899 String prevVar = HCodeGenerator::FieldName(name) + "Prev";
900 this->writef("%sif (%s) {\n"
901 "%s %s;\n"
902 "%s %s;\n"
903 "%s}\n", indent.c_str(),
904 mapper->dirtyExpression(valueVar, prevVar).c_str(), indent.c_str(),
905 mapper->saveState(valueVar, prevVar).c_str(), indent.c_str(),
906 mapper->setUniform(pdman, uniformName, valueVar).c_str(), indent.c_str());
907 } else {
908 this->writef("%s%s;\n", indent.c_str(),
909 mapper->setUniform(pdman, uniformName, valueVar).c_str());
910 }
911
912 if (conditionalUniform) {
913 // Close the earlier precheck block
914 this->writef(" }\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400915 }
916 }
917 }
918 if (wroteProcessor) {
919 this->writef(" }\n");
920 }
Ethan Nicholas68990be2017-07-13 09:36:52 -0400921 if (section) {
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500922 int samplerIndex = 0;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400923 for (const auto& p : fProgram) {
924 if (ProgramElement::kVar_Kind == p.fKind) {
925 const VarDeclarations& decls = (const VarDeclarations&) p;
926 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000927 VarDeclaration& decl = (VarDeclaration&) *raw;
928 String nameString(decl.fVar->fName);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700929 const char* name = nameString.c_str();
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500930 if (decl.fVar->fType.kind() == Type::kSampler_Kind) {
931 this->writef(" GrSurfaceProxy& %sProxy = "
932 "*_outer.textureSampler(%d).proxy();\n",
933 name, samplerIndex);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400934 this->writef(" GrTexture& %s = *%sProxy.peekTexture();\n",
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500935 name, name);
936 this->writef(" (void) %s;\n", name);
937 ++samplerIndex;
938 } else if (needs_uniform_var(*decl.fVar)) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400939 this->writef(" UniformHandle& %s = %sVar;\n"
940 " (void) %s;\n",
941 name, HCodeGenerator::FieldName(name).c_str(), name);
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000942 } else if (SectionAndParameterHelper::IsParameter(*decl.fVar) &&
943 decl.fVar->fType != *fContext.fFragmentProcessor_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400944 if (!wroteProcessor) {
945 this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName,
946 fullName);
947 wroteProcessor = true;
948 }
949 this->writef(" auto %s = _outer.%s();\n"
950 " (void) %s;\n",
951 name, name, name);
952 }
953 }
954 }
955 }
956 this->writeSection(SET_DATA_SECTION);
957 }
958 this->write(" }\n");
959}
960
Brian Salomonf7dcd762018-07-30 14:48:15 -0400961void CPPCodeGenerator::writeOnTextureSampler() {
962 bool foundSampler = false;
963 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
964 if (param->fType.kind() == Type::kSampler_Kind) {
965 if (!foundSampler) {
966 this->writef(
967 "const GrFragmentProcessor::TextureSampler& %s::onTextureSampler(int "
968 "index) const {\n",
969 fFullName.c_str());
970 this->writef(" return IthTextureSampler(index, %s",
971 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
972 foundSampler = true;
973 } else {
974 this->writef(", %s",
975 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
976 }
977 }
978 }
979 if (foundSampler) {
980 this->write(");\n}\n");
981 }
982}
983
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400984void CPPCodeGenerator::writeClone() {
985 if (!this->writeSection(CLONE_SECTION)) {
986 if (fSectionAndParameterHelper.getSection(FIELDS_SECTION)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700987 fErrors.error(0, "fragment processors with custom @fields must also have a custom"
988 "@clone");
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400989 }
990 this->writef("%s::%s(const %s& src)\n"
Ethan Nicholasabff9562017-10-09 10:54:08 -0400991 ": INHERITED(k%s_ClassID, src.optimizationFlags())", fFullName.c_str(),
992 fFullName.c_str(), fFullName.c_str(), fFullName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400993 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400994 if (param->fType == *fContext.fFragmentProcessor_Type) {
995 continue;
996 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700997 String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str());
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400998 this->writef("\n, %s(src.%s)",
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400999 fieldName.c_str(),
Ethan Nicholasc9472af2017-10-10 16:30:21 -04001000 fieldName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001001 }
Ethan Nicholas929a6812018-08-06 14:56:59 -04001002 const auto transforms = fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION);
1003 for (size_t i = 0; i < transforms.size(); ++i) {
1004 const Section& s = *transforms[i];
1005 String fieldName = HCodeGenerator::CoordTransformName(s.fArgument, i);
1006 this->writef("\n, %s(src.%s)", fieldName.c_str(), fieldName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001007 }
Ethan Nicholasabff9562017-10-09 10:54:08 -04001008 this->writef(" {\n");
Ethan Nicholasc9472af2017-10-10 16:30:21 -04001009 int childCount = 0;
Brian Salomonf7dcd762018-07-30 14:48:15 -04001010 int samplerCount = 0;
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001011 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
1012 if (param->fType.kind() == Type::kSampler_Kind) {
Brian Salomonf7dcd762018-07-30 14:48:15 -04001013 ++samplerCount;
Ethan Nicholasc9472af2017-10-10 16:30:21 -04001014 } else if (param->fType == *fContext.fFragmentProcessor_Type) {
1015 this->writef(" this->registerChildProcessor(src.childProcessor(%d).clone());"
1016 "\n", childCount++);
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001017 }
1018 }
Brian Salomonf7dcd762018-07-30 14:48:15 -04001019 if (samplerCount) {
1020 this->writef(" this->setTextureSamplerCnt(%d);", samplerCount);
1021 }
Ethan Nicholas929a6812018-08-06 14:56:59 -04001022 for (size_t i = 0; i < transforms.size(); ++i) {
1023 const Section& s = *transforms[i];
1024 String fieldName = HCodeGenerator::CoordTransformName(s.fArgument, i);
1025 this->writef(" this->addCoordTransform(&%s);\n", fieldName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001026 }
1027 this->write("}\n");
Brian Salomonaff329b2017-08-11 09:40:37 -04001028 this->writef("std::unique_ptr<GrFragmentProcessor> %s::clone() const {\n",
1029 fFullName.c_str());
1030 this->writef(" return std::unique_ptr<GrFragmentProcessor>(new %s(*this));\n",
1031 fFullName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001032 this->write("}\n");
1033 }
1034}
1035
Ethan Nicholas762466e2017-06-29 10:03:38 -04001036void CPPCodeGenerator::writeTest() {
Ethan Nicholas68990be2017-07-13 09:36:52 -04001037 const Section* test = fSectionAndParameterHelper.getSection(TEST_CODE_SECTION);
1038 if (test) {
Brian Salomonaff329b2017-08-11 09:40:37 -04001039 this->writef(
1040 "GR_DEFINE_FRAGMENT_PROCESSOR_TEST(%s);\n"
1041 "#if GR_TEST_UTILS\n"
1042 "std::unique_ptr<GrFragmentProcessor> %s::TestCreate(GrProcessorTestData* %s) {\n",
1043 fFullName.c_str(),
1044 fFullName.c_str(),
1045 test->fArgument.c_str());
Ethan Nicholas68990be2017-07-13 09:36:52 -04001046 this->writeSection(TEST_CODE_SECTION);
1047 this->write("}\n"
1048 "#endif\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -04001049 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001050}
1051
1052void CPPCodeGenerator::writeGetKey() {
1053 this->writef("void %s::onGetGLSLProcessorKey(const GrShaderCaps& caps, "
1054 "GrProcessorKeyBuilder* b) const {\n",
1055 fFullName.c_str());
Ethan Nicholas68990be2017-07-13 09:36:52 -04001056 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001057 String nameString(param->fName);
1058 const char* name = nameString.c_str();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001059 if (param->fModifiers.fLayout.fKey != Layout::kNo_Key &&
1060 (param->fModifiers.fFlags & Modifiers::kUniform_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001061 fErrors.error(param->fOffset,
Ethan Nicholas762466e2017-06-29 10:03:38 -04001062 "layout(key) may not be specified on uniforms");
1063 }
1064 switch (param->fModifiers.fLayout.fKey) {
1065 case Layout::kKey_Key:
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001066 if (param->fType == *fContext.fFloat4x4_Type) {
1067 ABORT("no automatic key handling for float4x4\n");
1068 } else if (param->fType == *fContext.fFloat2_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001069 this->writef(" b->add32(%s.fX);\n",
1070 HCodeGenerator::FieldName(name).c_str());
1071 this->writef(" b->add32(%s.fY);\n",
1072 HCodeGenerator::FieldName(name).c_str());
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001073 } else if (param->fType == *fContext.fFloat4_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001074 this->writef(" b->add32(%s.x());\n",
1075 HCodeGenerator::FieldName(name).c_str());
1076 this->writef(" b->add32(%s.y());\n",
1077 HCodeGenerator::FieldName(name).c_str());
1078 this->writef(" b->add32(%s.width());\n",
1079 HCodeGenerator::FieldName(name).c_str());
1080 this->writef(" b->add32(%s.height());\n",
1081 HCodeGenerator::FieldName(name).c_str());
1082 } else {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001083 this->writef(" b->add32((int32_t) %s);\n",
Ethan Nicholas762466e2017-06-29 10:03:38 -04001084 HCodeGenerator::FieldName(name).c_str());
1085 }
1086 break;
1087 case Layout::kIdentity_Key:
1088 if (param->fType.kind() != Type::kMatrix_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001089 fErrors.error(param->fOffset,
Ethan Nicholas762466e2017-06-29 10:03:38 -04001090 "layout(key=identity) requires matrix type");
1091 }
1092 this->writef(" b->add32(%s.isIdentity() ? 1 : 0);\n",
1093 HCodeGenerator::FieldName(name).c_str());
1094 break;
1095 case Layout::kNo_Key:
1096 break;
1097 }
1098 }
1099 this->write("}\n");
1100}
1101
1102bool CPPCodeGenerator::generateCode() {
1103 std::vector<const Variable*> uniforms;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001104 for (const auto& p : fProgram) {
1105 if (ProgramElement::kVar_Kind == p.fKind) {
1106 const VarDeclarations& decls = (const VarDeclarations&) p;
1107 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001108 VarDeclaration& decl = (VarDeclaration&) *raw;
1109 if ((decl.fVar->fModifiers.fFlags & Modifiers::kUniform_Flag) &&
1110 decl.fVar->fType.kind() != Type::kSampler_Kind) {
1111 uniforms.push_back(decl.fVar);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001112 }
Michael Ludwiga4275592018-08-31 10:52:47 -04001113
1114 if (is_uniform_in(*decl.fVar)) {
1115 // Validate the "uniform in" declarations to make sure they are fully supported,
1116 // instead of generating surprising C++
1117 const UniformCTypeMapper* mapper =
1118 UniformCTypeMapper::Get(fContext, *decl.fVar);
1119 if (mapper == nullptr) {
1120 fErrors.error(decl.fOffset, String(decl.fVar->fName)
1121 + "'s type is not supported for use as a 'uniform in'");
1122 return false;
1123 }
1124 if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
1125 if (!mapper->supportsTracking()) {
1126 fErrors.error(decl.fOffset, String(decl.fVar->fName)
1127 + "'s type does not support state tracking");
1128 return false;
1129 }
1130 }
1131
1132 } else {
1133 // If it's not a uniform_in, it's an error to be tracked
1134 if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
1135 fErrors.error(decl.fOffset, "Non-'in uniforms' cannot be tracked");
1136 return false;
1137 }
1138 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001139 }
1140 }
1141 }
1142 const char* baseName = fName.c_str();
1143 const char* fullName = fFullName.c_str();
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05001144 this->writef("%s\n", HCodeGenerator::GetHeader(fProgram, fErrors).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001145 this->writef(kFragmentProcessorHeader, fullName);
Greg Daniel3e8c3452018-04-06 10:37:55 -04001146 this->writef("#include \"%s.h\"\n", fullName);
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04001147 this->writeSection(CPP_SECTION);
Brian Osman1cb41712017-10-19 12:54:52 -04001148 this->writef("#include \"glsl/GrGLSLFragmentProcessor.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -04001149 "#include \"glsl/GrGLSLFragmentShaderBuilder.h\"\n"
1150 "#include \"glsl/GrGLSLProgramBuilder.h\"\n"
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05001151 "#include \"GrTexture.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -04001152 "#include \"SkSLCPP.h\"\n"
1153 "#include \"SkSLUtil.h\"\n"
1154 "class GrGLSL%s : public GrGLSLFragmentProcessor {\n"
1155 "public:\n"
1156 " GrGLSL%s() {}\n",
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04001157 baseName, baseName);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001158 bool result = this->writeEmitCode(uniforms);
1159 this->write("private:\n");
1160 this->writeSetData(uniforms);
1161 this->writePrivateVars();
1162 for (const auto& u : uniforms) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001163 if (needs_uniform_var(*u) && !(u->fModifiers.fFlags & Modifiers::kIn_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001164 this->writef(" UniformHandle %sVar;\n",
1165 HCodeGenerator::FieldName(String(u->fName).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001166 }
1167 }
Ethan Nicholas68990be2017-07-13 09:36:52 -04001168 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001169 if (needs_uniform_var(*param)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001170 this->writef(" UniformHandle %sVar;\n",
1171 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001172 }
1173 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001174 this->writef("};\n"
1175 "GrGLSLFragmentProcessor* %s::onCreateGLSLInstance() const {\n"
1176 " return new GrGLSL%s();\n"
1177 "}\n",
1178 fullName, baseName);
1179 this->writeGetKey();
1180 this->writef("bool %s::onIsEqual(const GrFragmentProcessor& other) const {\n"
1181 " const %s& that = other.cast<%s>();\n"
1182 " (void) that;\n",
1183 fullName, fullName, fullName);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001184 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -04001185 if (param->fType == *fContext.fFragmentProcessor_Type) {
1186 continue;
1187 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001188 String nameString(param->fName);
1189 const char* name = nameString.c_str();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001190 this->writef(" if (%s != that.%s) return false;\n",
1191 HCodeGenerator::FieldName(name).c_str(),
1192 HCodeGenerator::FieldName(name).c_str());
1193 }
1194 this->write(" return true;\n"
1195 "}\n");
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001196 this->writeClone();
Brian Salomonf7dcd762018-07-30 14:48:15 -04001197 this->writeOnTextureSampler();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001198 this->writeTest();
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04001199 this->writeSection(CPP_END_SECTION);
Greg Daniel3e8c3452018-04-06 10:37:55 -04001200
Ethan Nicholas762466e2017-06-29 10:03:38 -04001201 result &= 0 == fErrors.errorCount();
1202 return result;
1203}
1204
1205} // namespace