blob: 5f9b8219e70646e7174d781065de19a44f59ccea [file] [log] [blame]
Ethan Nicholas762466e2017-06-29 10:03:38 -04001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/SkSLCPPCodeGenerator.h"
Ethan Nicholas762466e2017-06-29 10:03:38 -04009
Brian Osman1298bc42020-06-30 13:39:35 -040010#include "include/private/SkSLSampleUsage.h"
Michael Ludwig8f3a8362020-06-29 17:27:00 -040011#include "src/sksl/SkSLAnalysis.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/sksl/SkSLCPPUniformCTypes.h"
13#include "src/sksl/SkSLCompiler.h"
14#include "src/sksl/SkSLHCodeGenerator.h"
Ethan Nicholas762466e2017-06-29 10:03:38 -040015
Michael Ludwig92e4c7f2018-08-30 16:08:18 -040016#include <algorithm>
17
Ethan Nicholas762466e2017-06-29 10:03:38 -040018namespace SkSL {
19
20static bool needs_uniform_var(const Variable& var) {
Ethan Nicholas5f9836e2017-12-20 15:16:33 -050021 return (var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
22 var.fType.kind() != Type::kSampler_Kind;
Ethan Nicholas762466e2017-06-29 10:03:38 -040023}
24
25CPPCodeGenerator::CPPCodeGenerator(const Context* context, const Program* program,
26 ErrorReporter* errors, String name, OutputStream* out)
John Stiles50819422020-06-18 13:00:38 -040027 : INHERITED(context, program, errors, out)
28 , fName(std::move(name))
29 , fFullName(String::printf("Gr%s", fName.c_str()))
30 , fSectionAndParameterHelper(program, *errors) {
31 fLineEnding = "\n";
Ethan Nicholas13863662019-07-29 13:05:15 -040032 fTextureFunctionOverride = "sample";
Ethan Nicholas762466e2017-06-29 10:03:38 -040033}
34
35void CPPCodeGenerator::writef(const char* s, va_list va) {
36 static constexpr int BUFFER_SIZE = 1024;
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040037 va_list copy;
38 va_copy(copy, va);
Ethan Nicholas762466e2017-06-29 10:03:38 -040039 char buffer[BUFFER_SIZE];
John Stiles50819422020-06-18 13:00:38 -040040 int length = std::vsnprintf(buffer, BUFFER_SIZE, s, va);
Ethan Nicholas762466e2017-06-29 10:03:38 -040041 if (length < BUFFER_SIZE) {
42 fOut->write(buffer, length);
43 } else {
44 std::unique_ptr<char[]> heap(new char[length + 1]);
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040045 vsprintf(heap.get(), s, copy);
Ethan Nicholas762466e2017-06-29 10:03:38 -040046 fOut->write(heap.get(), length);
47 }
z102.zhangd74f2c82018-08-10 09:08:47 +080048 va_end(copy);
Ethan Nicholas762466e2017-06-29 10:03:38 -040049}
50
51void CPPCodeGenerator::writef(const char* s, ...) {
52 va_list va;
53 va_start(va, s);
54 this->writef(s, va);
55 va_end(va);
56}
57
58void CPPCodeGenerator::writeHeader() {
59}
60
Ethan Nicholasf7b88202017-09-18 14:10:39 -040061bool CPPCodeGenerator::usesPrecisionModifiers() const {
62 return false;
Ethan Nicholas762466e2017-06-29 10:03:38 -040063}
64
Ethan Nicholasf7b88202017-09-18 14:10:39 -040065String CPPCodeGenerator::getTypeName(const Type& type) {
66 return type.name();
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040067}
Ethan Nicholasf7b88202017-09-18 14:10:39 -040068
Ethan Nicholas762466e2017-06-29 10:03:38 -040069void CPPCodeGenerator::writeBinaryExpression(const BinaryExpression& b,
70 Precedence parentPrecedence) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -040071 if (b.fOperator == Token::Kind::TK_PERCENT) {
Ethan Nicholas762466e2017-06-29 10:03:38 -040072 // need to use "%%" instead of "%" b/c the code will be inside of a printf
73 Precedence precedence = GetBinaryPrecedence(b.fOperator);
74 if (precedence >= parentPrecedence) {
75 this->write("(");
76 }
77 this->writeExpression(*b.fLeft, precedence);
78 this->write(" %% ");
79 this->writeExpression(*b.fRight, precedence);
80 if (precedence >= parentPrecedence) {
81 this->write(")");
82 }
Ethan Nicholasee1c8a72019-02-22 10:50:47 -050083 } else if (b.fLeft->fKind == Expression::kNullLiteral_Kind ||
84 b.fRight->fKind == Expression::kNullLiteral_Kind) {
85 const Variable* var;
86 if (b.fLeft->fKind != Expression::kNullLiteral_Kind) {
John Stiles17c5b702020-08-18 10:40:03 -040087 var = &b.fLeft->as<VariableReference>().fVariable;
Ethan Nicholasee1c8a72019-02-22 10:50:47 -050088 } else {
John Stiles17c5b702020-08-18 10:40:03 -040089 var = &b.fRight->as<VariableReference>().fVariable;
Ethan Nicholasee1c8a72019-02-22 10:50:47 -050090 }
91 SkASSERT(var->fType.kind() == Type::kNullable_Kind &&
92 var->fType.componentType() == *fContext.fFragmentProcessor_Type);
93 this->write("%s");
Brian Osman12c5d292020-07-13 16:11:35 -040094 const char* op = "";
Ethan Nicholasee1c8a72019-02-22 10:50:47 -050095 switch (b.fOperator) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -040096 case Token::Kind::TK_EQEQ:
Brian Osman12c5d292020-07-13 16:11:35 -040097 op = "!";
Ethan Nicholasee1c8a72019-02-22 10:50:47 -050098 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -040099 case Token::Kind::TK_NEQ:
Brian Osman12c5d292020-07-13 16:11:35 -0400100 op = "";
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500101 break;
102 default:
103 SkASSERT(false);
104 }
Brian Osman12c5d292020-07-13 16:11:35 -0400105 int childIndex = this->getChildFPIndex(*var);
106 fFormatArgs.push_back(String(op) + "_outer.childProcessor(" + to_string(childIndex) +
107 ") ? \"true\" : \"false\"");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400108 } else {
109 INHERITED::writeBinaryExpression(b, parentPrecedence);
110 }
111}
112
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400113static String default_value(const Type& type) {
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500114 if (type.fName == "bool") {
115 return "false";
116 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400117 switch (type.kind()) {
118 case Type::kScalar_Kind: return "0";
119 case Type::kVector_Kind: return type.name() + "(0)";
120 case Type::kMatrix_Kind: return type.name() + "(1)";
121 default: ABORT("unsupported default_value type\n");
122 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400123}
124
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500125static String default_value(const Variable& var) {
Brian Osman495993a2018-10-16 15:45:55 -0400126 if (var.fModifiers.fLayout.fCType == SkSL::Layout::CType::kSkPMColor4f) {
Brian Osmanf28e55d2018-10-03 16:35:54 -0400127 return "{SK_FloatNaN, SK_FloatNaN, SK_FloatNaN, SK_FloatNaN}";
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500128 }
129 return default_value(var.fType);
130}
131
Ethan Nicholas762466e2017-06-29 10:03:38 -0400132static bool is_private(const Variable& var) {
133 return !(var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
134 !(var.fModifiers.fFlags & Modifiers::kIn_Flag) &&
135 var.fStorage == Variable::kGlobal_Storage &&
136 var.fModifiers.fLayout.fBuiltin == -1;
137}
138
Michael Ludwiga4275592018-08-31 10:52:47 -0400139static bool is_uniform_in(const Variable& var) {
140 return (var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
141 (var.fModifiers.fFlags & Modifiers::kIn_Flag) &&
142 var.fType.kind() != Type::kSampler_Kind;
143}
144
John Stiles47b4e222020-08-12 09:56:50 -0400145String CPPCodeGenerator::formatRuntimeValue(const Type& type,
146 const Layout& layout,
147 const String& cppCode,
148 std::vector<String>* formatArgs) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400149 if (type.isFloat()) {
John Stiles47b4e222020-08-12 09:56:50 -0400150 formatArgs->push_back(cppCode);
151 return "%f";
152 }
153 if (type == *fContext.fInt_Type) {
154 formatArgs->push_back(cppCode);
155 return "%d";
156 }
157 if (type == *fContext.fBool_Type) {
158 formatArgs->push_back("(" + cppCode + " ? \"true\" : \"false\")");
159 return "%s";
160 }
161 if (type == *fContext.fFloat2_Type || type == *fContext.fHalf2_Type) {
162 formatArgs->push_back(cppCode + ".fX");
163 formatArgs->push_back(cppCode + ".fY");
164 return type.name() + "(%f, %f)";
165 }
166 if (type == *fContext.fFloat3_Type || type == *fContext.fHalf3_Type) {
167 formatArgs->push_back(cppCode + ".fX");
168 formatArgs->push_back(cppCode + ".fY");
169 formatArgs->push_back(cppCode + ".fZ");
170 return type.name() + "(%f, %f, %f)";
171 }
172 if (type == *fContext.fFloat4_Type || type == *fContext.fHalf4_Type) {
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400173 switch (layout.fCType) {
174 case Layout::CType::kSkPMColor:
John Stiles47b4e222020-08-12 09:56:50 -0400175 formatArgs->push_back("SkGetPackedR32(" + cppCode + ") / 255.0");
176 formatArgs->push_back("SkGetPackedG32(" + cppCode + ") / 255.0");
177 formatArgs->push_back("SkGetPackedB32(" + cppCode + ") / 255.0");
178 formatArgs->push_back("SkGetPackedA32(" + cppCode + ") / 255.0");
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400179 break;
Brian Osmanf28e55d2018-10-03 16:35:54 -0400180 case Layout::CType::kSkPMColor4f:
John Stiles47b4e222020-08-12 09:56:50 -0400181 formatArgs->push_back(cppCode + ".fR");
182 formatArgs->push_back(cppCode + ".fG");
183 formatArgs->push_back(cppCode + ".fB");
184 formatArgs->push_back(cppCode + ".fA");
Brian Osmanf28e55d2018-10-03 16:35:54 -0400185 break;
Mike Reedb26b4e72020-01-22 14:31:21 -0500186 case Layout::CType::kSkV4:
John Stiles47b4e222020-08-12 09:56:50 -0400187 formatArgs->push_back(cppCode + ".x");
188 formatArgs->push_back(cppCode + ".y");
189 formatArgs->push_back(cppCode + ".z");
190 formatArgs->push_back(cppCode + ".w");
Brian Salomoneca66b32019-06-01 11:18:15 -0400191 break;
John Stiles47b4e222020-08-12 09:56:50 -0400192 case Layout::CType::kSkRect:
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400193 case Layout::CType::kDefault:
John Stiles47b4e222020-08-12 09:56:50 -0400194 formatArgs->push_back(cppCode + ".left()");
195 formatArgs->push_back(cppCode + ".top()");
196 formatArgs->push_back(cppCode + ".right()");
197 formatArgs->push_back(cppCode + ".bottom()");
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400198 break;
199 default:
200 SkASSERT(false);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400201 }
John Stiles47b4e222020-08-12 09:56:50 -0400202 return type.name() + "(%f, %f, %f, %f)";
Ethan Nicholas762466e2017-06-29 10:03:38 -0400203 }
John Stiles47b4e222020-08-12 09:56:50 -0400204 if (type.kind() == Type::kMatrix_Kind) {
205 SkASSERT(type.componentType() == *fContext.fFloat_Type ||
206 type.componentType() == *fContext.fHalf_Type);
207
208 String format = type.name() + "(";
209 for (int c = 0; c < type.columns(); ++c) {
210 for (int r = 0; r < type.rows(); ++r) {
211 formatArgs->push_back(String::printf("%s.rc(%d, %d)", cppCode.c_str(), r, c));
212 format += "%f, ";
213 }
214 }
215
216 // Replace trailing ", " with ")".
217 format.pop_back();
218 format.back() = ')';
219 return format;
220 }
221 if (type.kind() == Type::kEnum_Kind) {
222 formatArgs->push_back("(int) " + cppCode);
223 return "%d";
224 }
225 if (type == *fContext.fInt4_Type ||
226 type == *fContext.fShort4_Type ||
227 type == *fContext.fByte4_Type) {
228 formatArgs->push_back(cppCode + ".left()");
229 formatArgs->push_back(cppCode + ".top()");
230 formatArgs->push_back(cppCode + ".right()");
231 formatArgs->push_back(cppCode + ".bottom()");
232 return type.name() + "(%d, %d, %d, %d)";
233 }
234
235 SkDEBUGFAILF("unsupported runtime value type '%s'\n", String(type.fName).c_str());
236 return "";
237}
238
239void CPPCodeGenerator::writeRuntimeValue(const Type& type, const Layout& layout,
240 const String& cppCode) {
241 this->write(this->formatRuntimeValue(type, layout, cppCode, &fFormatArgs));
Ethan Nicholas762466e2017-06-29 10:03:38 -0400242}
243
244void CPPCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) {
245 if (is_private(var)) {
Ethan Nicholasd608c092017-10-26 09:30:08 -0400246 this->writeRuntimeValue(var.fType, var.fModifiers.fLayout, var.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400247 } else {
248 this->writeExpression(value, kTopLevel_Precedence);
249 }
250}
251
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400252String CPPCodeGenerator::getSamplerHandle(const Variable& var) {
253 int samplerCount = 0;
Ethan Nicholas68990be2017-07-13 09:36:52 -0400254 for (const auto param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400255 if (&var == param) {
256 return "args.fTexSamplers[" + to_string(samplerCount) + "]";
257 }
258 if (param->fType.kind() == Type::kSampler_Kind) {
259 ++samplerCount;
260 }
261 }
262 ABORT("should have found sampler in parameters\n");
263}
264
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400265void CPPCodeGenerator::writeIntLiteral(const IntLiteral& i) {
266 this->write(to_string((int32_t) i.fValue));
267}
268
Ethan Nicholas82399462017-10-16 12:35:44 -0400269void CPPCodeGenerator::writeSwizzle(const Swizzle& swizzle) {
270 if (fCPPMode) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400271 SkASSERT(swizzle.fComponents.size() == 1); // no support for multiple swizzle components yet
Ethan Nicholas82399462017-10-16 12:35:44 -0400272 this->writeExpression(*swizzle.fBase, kPostfix_Precedence);
273 switch (swizzle.fComponents[0]) {
274 case 0: this->write(".left()"); break;
275 case 1: this->write(".top()"); break;
276 case 2: this->write(".right()"); break;
277 case 3: this->write(".bottom()"); break;
278 }
279 } else {
280 INHERITED::writeSwizzle(swizzle);
281 }
282}
283
Ethan Nicholas762466e2017-06-29 10:03:38 -0400284void CPPCodeGenerator::writeVariableReference(const VariableReference& ref) {
Ethan Nicholas82399462017-10-16 12:35:44 -0400285 if (fCPPMode) {
286 this->write(ref.fVariable.fName);
287 return;
288 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400289 switch (ref.fVariable.fModifiers.fLayout.fBuiltin) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400290 case SK_OUTCOLOR_BUILTIN:
291 this->write("%s");
292 fFormatArgs.push_back(String("args.fOutputColor"));
293 break;
Michael Ludwigfc2fdf02020-06-29 17:20:13 -0400294 case SK_MAIN_COORDS_BUILTIN:
295 this->write("%s");
296 fFormatArgs.push_back(String("args.fSampleCoord"));
297 fAccessSampleCoordsDirectly = true;
298 break;
Ethan Nicholascd700e92018-08-24 16:43:57 -0400299 case SK_WIDTH_BUILTIN:
300 this->write("sk_Width");
301 break;
302 case SK_HEIGHT_BUILTIN:
303 this->write("sk_Height");
304 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400305 default:
306 if (ref.fVariable.fType.kind() == Type::kSampler_Kind) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400307 this->write("%s");
308 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerVariable(" +
Stephen Whited523a062019-06-19 13:12:46 -0400309 this->getSamplerHandle(ref.fVariable) + ")");
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400310 return;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400311 }
312 if (ref.fVariable.fModifiers.fFlags & Modifiers::kUniform_Flag) {
313 this->write("%s");
314 String name = ref.fVariable.fName;
Brian Osman1cb41712017-10-19 12:54:52 -0400315 String var = String::printf("args.fUniformHandler->getUniformCStr(%sVar)",
316 HCodeGenerator::FieldName(name.c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400317 String code;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400318 if (ref.fVariable.fModifiers.fLayout.fWhen.fLength) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400319 code = String::printf("%sVar.isValid() ? %s : \"%s\"",
320 HCodeGenerator::FieldName(name.c_str()).c_str(),
321 var.c_str(),
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400322 default_value(ref.fVariable.fType).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400323 } else {
324 code = var;
325 }
326 fFormatArgs.push_back(code);
327 } else if (SectionAndParameterHelper::IsParameter(ref.fVariable)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700328 String name(ref.fVariable.fName);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400329 this->writeRuntimeValue(ref.fVariable.fType, ref.fVariable.fModifiers.fLayout,
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400330 String::printf("_outer.%s", name.c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400331 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700332 this->write(ref.fVariable.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400333 }
334 }
335}
336
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400337void CPPCodeGenerator::writeIfStatement(const IfStatement& s) {
338 if (s.fIsStatic) {
339 this->write("@");
340 }
341 INHERITED::writeIfStatement(s);
342}
343
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400344void CPPCodeGenerator::writeReturnStatement(const ReturnStatement& s) {
345 if (fInMain) {
346 fErrors.error(s.fOffset, "fragmentProcessor main() may not contain return statements");
347 }
348 INHERITED::writeReturnStatement(s);
349}
350
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400351void CPPCodeGenerator::writeSwitchStatement(const SwitchStatement& s) {
352 if (s.fIsStatic) {
353 this->write("@");
354 }
355 INHERITED::writeSwitchStatement(s);
356}
357
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400358void CPPCodeGenerator::writeFieldAccess(const FieldAccess& access) {
359 if (access.fBase->fType.name() == "fragmentProcessor") {
360 // Special field access on fragment processors are converted into function calls on
361 // GrFragmentProcessor's getters.
362 if (access.fBase->fKind != Expression::kVariableReference_Kind) {
363 fErrors.error(access.fBase->fOffset, "fragmentProcessor must be a reference\n");
364 return;
365 }
366
367 const Type::Field& field = fContext.fFragmentProcessor_Type->fields()[access.fFieldIndex];
John Stiles3dc0da62020-08-19 17:48:31 -0400368 const Variable& var = access.fBase->as<VariableReference>().fVariable;
Brian Osman12c5d292020-07-13 16:11:35 -0400369 String cppAccess = String::printf("_outer.childProcessor(%d)->%s()",
370 this->getChildFPIndex(var),
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500371 String(field.fName).c_str());
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400372
373 if (fCPPMode) {
374 this->write(cppAccess.c_str());
375 } else {
376 writeRuntimeValue(*field.fType, Layout(), cppAccess);
377 }
378 return;
379 }
380 INHERITED::writeFieldAccess(access);
381}
382
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500383int CPPCodeGenerator::getChildFPIndex(const Variable& var) const {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400384 int index = 0;
385 bool found = false;
386 for (const auto& p : fProgram) {
387 if (ProgramElement::kVar_Kind == p.fKind) {
John Stiles3dc0da62020-08-19 17:48:31 -0400388 const VarDeclarations& decls = p.as<VarDeclarations>();
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400389 for (const auto& raw : decls.fVars) {
John Stiles3dc0da62020-08-19 17:48:31 -0400390 const VarDeclaration& decl = raw->as<VarDeclaration>();
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500391 if (decl.fVar == &var) {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400392 found = true;
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500393 } else if (decl.fVar->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400394 ++index;
395 }
396 }
397 }
398 if (found) {
399 break;
400 }
401 }
402 SkASSERT(found);
403 return index;
404}
405
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400406void CPPCodeGenerator::writeFunctionCall(const FunctionCall& c) {
Ethan Nicholas13863662019-07-29 13:05:15 -0400407 if (c.fFunction.fBuiltin && c.fFunction.fName == "sample" &&
408 c.fArguments[0]->fType.kind() != Type::Kind::kSampler_Kind) {
Leon Scroggins III982fff22020-07-31 14:09:06 -0400409 // Validity checks that are detected by function definition in sksl_fp.inc
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400410 SkASSERT(c.fArguments.size() >= 1 && c.fArguments.size() <= 3);
Florin Malita390f9bd2019-03-04 12:25:57 -0500411 SkASSERT("fragmentProcessor" == c.fArguments[0]->fType.name() ||
412 "fragmentProcessor?" == c.fArguments[0]->fType.name());
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400413
414 // Actually fail during compilation if arguments with valid types are
Ethan Nicholas13863662019-07-29 13:05:15 -0400415 // provided that are not variable references, since sample() is a
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400416 // special function that impacts code emission.
417 if (c.fArguments[0]->fKind != Expression::kVariableReference_Kind) {
418 fErrors.error(c.fArguments[0]->fOffset,
Ethan Nicholas13863662019-07-29 13:05:15 -0400419 "sample()'s fragmentProcessor argument must be a variable reference\n");
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400420 return;
421 }
John Stiles3dc0da62020-08-19 17:48:31 -0400422 const Variable& child = c.fArguments[0]->as<VariableReference>().fVariable;
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400423
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400424 // Start a new extra emit code section so that the emitted child processor can depend on
425 // sksl variables defined in earlier sksl code.
426 this->newExtraEmitCodeBlock();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400427
Michael Ludwige88320b2020-06-24 09:04:56 -0400428 String inputColor;
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400429 if (c.fArguments.size() > 1 && c.fArguments[1]->fType.name() == "half4") {
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400430 // Use the invokeChild() variant that accepts an input color, so convert the 2nd
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400431 // argument's expression into C++ code that produces sksl stored in an SkString.
Brian Osman12c5d292020-07-13 16:11:35 -0400432 String inputColorName = "_input" + to_string(c.fOffset);
John Stilesd060c9d2020-06-08 11:44:25 -0400433 addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments[1], inputColorName));
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400434
Michael Ludwige88320b2020-06-24 09:04:56 -0400435 // invokeChild() needs a char* and a pre-pended comma
436 inputColor = ", " + inputColorName + ".c_str()";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400437 }
438
Michael Ludwige88320b2020-06-24 09:04:56 -0400439 String inputCoord;
440 String invokeFunction = "invokeChild";
441 if (c.fArguments.back()->fType.name() == "float2") {
442 // Invoking child with explicit coordinates at this call site
443 inputCoord = "_coords" + to_string(c.fOffset);
444 addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments.back(), inputCoord));
445 inputCoord.append(".c_str()");
446 } else if (c.fArguments.back()->fType.name() == "float3x3") {
447 // Invoking child with a matrix, sampling relative to the input coords.
448 invokeFunction = "invokeChildWithMatrix";
Brian Osman1298bc42020-06-30 13:39:35 -0400449 SampleUsage usage = Analysis::GetSampleUsage(fProgram, child);
Michael Ludwige88320b2020-06-24 09:04:56 -0400450
Brian Osman1298bc42020-06-30 13:39:35 -0400451 if (!usage.hasUniformMatrix()) {
Michael Ludwige88320b2020-06-24 09:04:56 -0400452 inputCoord = "_matrix" + to_string(c.fOffset);
453 addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments.back(), inputCoord));
454 inputCoord.append(".c_str()");
455 }
456 // else pass in the empty string to rely on invokeChildWithMatrix's automatic uniform
457 // resolution
458 }
459 if (!inputCoord.empty()) {
460 inputCoord = ", " + inputCoord;
461 }
462
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400463 // Write the output handling after the possible input handling
Ethan Nicholas13863662019-07-29 13:05:15 -0400464 String childName = "_sample" + to_string(c.fOffset);
Brian Osman12c5d292020-07-13 16:11:35 -0400465 String childIndexStr = to_string(this->getChildFPIndex(child));
466 addExtraEmitCodeLine("SkString " + childName + " = this->" + invokeFunction + "(" +
467 childIndexStr + inputColor + ", args" + inputCoord + ");");
John Stiles50819422020-06-18 13:00:38 -0400468
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000469 this->write("%s");
470 fFormatArgs.push_back(childName + ".c_str()");
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400471 return;
472 }
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400473 if (c.fFunction.fBuiltin) {
474 INHERITED::writeFunctionCall(c);
475 } else {
476 this->write("%s");
477 fFormatArgs.push_back((String(c.fFunction.fName) + "_name.c_str()").c_str());
478 this->write("(");
479 const char* separator = "";
480 for (const auto& arg : c.fArguments) {
481 this->write(separator);
482 separator = ", ";
483 this->writeExpression(*arg, kSequence_Precedence);
484 }
485 this->write(")");
486 }
Ethan Nicholas13863662019-07-29 13:05:15 -0400487 if (c.fFunction.fBuiltin && c.fFunction.fName == "sample") {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400488 this->write(".%s");
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400489 SkASSERT(c.fArguments.size() >= 1);
490 SkASSERT(c.fArguments[0]->fKind == Expression::kVariableReference_Kind);
John Stiles3dc0da62020-08-19 17:48:31 -0400491 String sampler = this->getSamplerHandle(c.fArguments[0]->as<VariableReference>().fVariable);
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400492 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerSwizzle(" + sampler +
Greg Daniel369ee6b2019-12-02 15:30:02 -0500493 ").asString().c_str()");
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400494 }
495}
496
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400497static const char* glsltype_string(const Context& context, const Type& type) {
498 if (type == *context.fFloat_Type) {
499 return "kFloat_GrSLType";
500 } else if (type == *context.fHalf_Type) {
501 return "kHalf_GrSLType";
John Stiles0e8149c2020-08-18 12:23:40 -0400502 } else if (type == *context.fInt_Type) {
503 return "kInt_GrSLType";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400504 } else if (type == *context.fFloat2_Type) {
505 return "kFloat2_GrSLType";
506 } else if (type == *context.fHalf2_Type) {
507 return "kHalf2_GrSLType";
John Stiles0e8149c2020-08-18 12:23:40 -0400508 } else if (type == *context.fInt2_Type) {
509 return "kInt2_GrSLType";
Ethan Nicholas8ae1b562019-12-17 15:18:02 -0500510 } else if (type == *context.fFloat3_Type) {
511 return "kFloat3_GrSLType";
512 } else if (type == *context.fHalf3_Type) {
513 return "kHalf3_GrSLType";
John Stiles0e8149c2020-08-18 12:23:40 -0400514 } else if (type == *context.fInt3_Type) {
515 return "kInt3_GrSLType";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400516 } else if (type == *context.fFloat4_Type) {
517 return "kFloat4_GrSLType";
518 } else if (type == *context.fHalf4_Type) {
519 return "kHalf4_GrSLType";
John Stiles0e8149c2020-08-18 12:23:40 -0400520 } else if (type == *context.fInt4_Type) {
521 return "kInt4_GrSLType";
Ethan Nicholas58430122020-04-14 09:54:02 -0400522 } else if (type == *context.fFloat2x2_Type) {
523 return "kFloat2x2_GrSLType";
524 } else if (type == *context.fHalf2x2_Type) {
525 return "kHalf2x2_GrSLType";
John Stiles0e8149c2020-08-18 12:23:40 -0400526 } else if (type == *context.fFloat2x3_Type) {
527 return "kFloat2x3_GrSLType";
528 } else if (type == *context.fHalf2x3_Type) {
529 return "kHalf2x3_GrSLType";
530 } else if (type == *context.fFloat2x4_Type) {
531 return "kFloat2x4_GrSLType";
532 } else if (type == *context.fHalf2x4_Type) {
533 return "kHalf2x4_GrSLType";
534 } else if (type == *context.fFloat3x2_Type) {
535 return "kFloat3x2_GrSLType";
536 } else if (type == *context.fHalf3x2_Type) {
537 return "kHalf3x2_GrSLType";
Ethan Nicholas58430122020-04-14 09:54:02 -0400538 } else if (type == *context.fFloat3x3_Type) {
539 return "kFloat3x3_GrSLType";
540 } else if (type == *context.fHalf3x3_Type) {
541 return "kHalf3x3_GrSLType";
John Stiles0e8149c2020-08-18 12:23:40 -0400542 } else if (type == *context.fFloat3x4_Type) {
543 return "kFloat3x4_GrSLType";
544 } else if (type == *context.fHalf3x4_Type) {
545 return "kHalf3x4_GrSLType";
546 } else if (type == *context.fFloat4x2_Type) {
547 return "kFloat4x2_GrSLType";
548 } else if (type == *context.fHalf4x2_Type) {
549 return "kHalf4x2_GrSLType";
550 } else if (type == *context.fFloat4x3_Type) {
551 return "kFloat4x3_GrSLType";
552 } else if (type == *context.fHalf4x3_Type) {
553 return "kHalf4x3_GrSLType";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400554 } else if (type == *context.fFloat4x4_Type) {
555 return "kFloat4x4_GrSLType";
556 } else if (type == *context.fHalf4x4_Type) {
557 return "kHalf4x4_GrSLType";
558 } else if (type == *context.fVoid_Type) {
559 return "kVoid_GrSLType";
Ethan Nicholas8ae1b562019-12-17 15:18:02 -0500560 } else if (type.kind() == Type::kEnum_Kind) {
561 return "int";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400562 }
563 SkASSERT(false);
564 return nullptr;
565}
566
Ethan Nicholas762466e2017-06-29 10:03:38 -0400567void CPPCodeGenerator::writeFunction(const FunctionDefinition& f) {
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400568 const FunctionDeclaration& decl = f.fDeclaration;
Brian Osman08f986d2020-05-13 17:06:46 -0400569 if (decl.fBuiltin) {
570 return;
571 }
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400572 fFunctionHeader = "";
573 OutputStream* oldOut = fOut;
574 StringStream buffer;
575 fOut = &buffer;
576 if (decl.fName == "main") {
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400577 fInMain = true;
John Stiles3dc0da62020-08-19 17:48:31 -0400578 for (const auto& s : f.fBody->as<Block>().fStatements) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400579 this->writeStatement(*s);
580 this->writeLine();
581 }
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400582 fInMain = false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400583
584 fOut = oldOut;
585 this->write(fFunctionHeader);
586 this->write(buffer.str());
587 } else {
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400588 this->addExtraEmitCodeLine("SkString " + decl.fName + "_name;");
589 String args = "const GrShaderVar " + decl.fName + "_args[] = { ";
590 const char* separator = "";
591 for (const auto& param : decl.fParameters) {
592 args += String(separator) + "GrShaderVar(\"" + param->fName + "\", " +
593 glsltype_string(fContext, param->fType) + ")";
594 separator = ", ";
595 }
596 args += "};";
597 this->addExtraEmitCodeLine(args.c_str());
John Stiles3dc0da62020-08-19 17:48:31 -0400598 for (const auto& s : f.fBody->as<Block>().fStatements) {
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400599 this->writeStatement(*s);
600 this->writeLine();
601 }
602
603 fOut = oldOut;
604 String emit = "fragBuilder->emitFunction(";
605 emit += glsltype_string(fContext, decl.fReturnType);
606 emit += ", \"" + decl.fName + "\"";
607 emit += ", " + to_string((int64_t) decl.fParameters.size());
608 emit += ", " + decl.fName + "_args";
John Stiles50819422020-06-18 13:00:38 -0400609 emit += ",\nR\"SkSL(" + buffer.str() + ")SkSL\"";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400610 emit += ", &" + decl.fName + "_name);";
611 this->addExtraEmitCodeLine(emit.c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400612 }
613}
614
615void CPPCodeGenerator::writeSetting(const Setting& s) {
Brian Osmanf265afd2020-08-04 13:23:36 -0400616 this->write(s.fName.c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400617}
618
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400619bool CPPCodeGenerator::writeSection(const char* name, const char* prefix) {
Ethan Nicholas68990be2017-07-13 09:36:52 -0400620 const Section* s = fSectionAndParameterHelper.getSection(name);
621 if (s) {
622 this->writef("%s%s", prefix, s->fText.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400623 return true;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400624 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400625 return false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400626}
627
628void CPPCodeGenerator::writeProgramElement(const ProgramElement& p) {
629 if (p.fKind == ProgramElement::kSection_Kind) {
630 return;
631 }
632 if (p.fKind == ProgramElement::kVar_Kind) {
John Stiles3dc0da62020-08-19 17:48:31 -0400633 const VarDeclarations& decls = p.as<VarDeclarations>();
Ethan Nicholas762466e2017-06-29 10:03:38 -0400634 if (!decls.fVars.size()) {
635 return;
636 }
John Stiles3dc0da62020-08-19 17:48:31 -0400637 const Variable& var = *decls.fVars[0]->as<VarDeclaration>().fVar;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400638 if (var.fModifiers.fFlags & (Modifiers::kIn_Flag | Modifiers::kUniform_Flag) ||
639 -1 != var.fModifiers.fLayout.fBuiltin) {
640 return;
641 }
642 }
643 INHERITED::writeProgramElement(p);
644}
645
646void CPPCodeGenerator::addUniform(const Variable& var) {
647 if (!needs_uniform_var(var)) {
648 return;
649 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400650 if (var.fModifiers.fLayout.fWhen.fLength) {
651 this->writef(" if (%s) {\n ", String(var.fModifiers.fLayout.fWhen).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400652 }
Ethan Nicholas95b24b92020-08-20 00:03:58 +0000653 const char* type = glsltype_string(fContext, var.fType);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700654 String name(var.fName);
Ethan Nicholas95b24b92020-08-20 00:03:58 +0000655 this->writef(" %sVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag,"
656 " %s, \"%s\");\n", HCodeGenerator::FieldName(name.c_str()).c_str(), type,
657 name.c_str());
Ethan Nicholasfc994162019-06-06 10:04:27 -0400658 if (var.fModifiers.fLayout.fWhen.fLength) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400659 this->write(" }\n");
660 }
661}
662
Ethan Nicholascd700e92018-08-24 16:43:57 -0400663void CPPCodeGenerator::writeInputVars() {
664}
665
Ethan Nicholas762466e2017-06-29 10:03:38 -0400666void CPPCodeGenerator::writePrivateVars() {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400667 for (const auto& p : fProgram) {
668 if (ProgramElement::kVar_Kind == p.fKind) {
John Stiles3dc0da62020-08-19 17:48:31 -0400669 const VarDeclarations& decls = p.as<VarDeclarations>();
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400670 for (const auto& raw : decls.fVars) {
John Stiles3dc0da62020-08-19 17:48:31 -0400671 VarDeclaration& decl = raw->as<VarDeclaration>();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000672 if (is_private(*decl.fVar)) {
673 if (decl.fVar->fType == *fContext.fFragmentProcessor_Type) {
674 fErrors.error(decl.fOffset,
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400675 "fragmentProcessor variables must be declared 'in'");
676 return;
677 }
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500678 this->writef("%s %s = %s;\n",
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000679 HCodeGenerator::FieldType(fContext, decl.fVar->fType,
680 decl.fVar->fModifiers.fLayout).c_str(),
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500681 String(decl.fVar->fName).c_str(),
682 default_value(*decl.fVar).c_str());
Michael Ludwiga4275592018-08-31 10:52:47 -0400683 } else if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
684 // An auto-tracked uniform in variable, so add a field to hold onto the prior
685 // state. Note that tracked variables must be uniform in's and that is validated
686 // before writePrivateVars() is called.
687 const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *decl.fVar);
688 SkASSERT(mapper && mapper->supportsTracking());
689
690 String name = HCodeGenerator::FieldName(String(decl.fVar->fName).c_str());
691 // The member statement is different if the mapper reports a default value
692 if (mapper->defaultValue().size() > 0) {
693 this->writef("%s %sPrev = %s;\n",
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400694 Layout::CTypeToStr(mapper->ctype()), name.c_str(),
Michael Ludwiga4275592018-08-31 10:52:47 -0400695 mapper->defaultValue().c_str());
696 } else {
697 this->writef("%s %sPrev;\n",
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400698 Layout::CTypeToStr(mapper->ctype()), name.c_str());
Michael Ludwiga4275592018-08-31 10:52:47 -0400699 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400700 }
701 }
702 }
703 }
704}
705
706void CPPCodeGenerator::writePrivateVarValues() {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400707 for (const auto& p : fProgram) {
708 if (ProgramElement::kVar_Kind == p.fKind) {
John Stiles3dc0da62020-08-19 17:48:31 -0400709 const VarDeclarations& decls = p.as<VarDeclarations>();
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400710 for (const auto& raw : decls.fVars) {
John Stiles3dc0da62020-08-19 17:48:31 -0400711 VarDeclaration& decl = raw->as<VarDeclaration>();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000712 if (is_private(*decl.fVar) && decl.fValue) {
713 this->writef("%s = ", String(decl.fVar->fName).c_str());
Ethan Nicholas82399462017-10-16 12:35:44 -0400714 fCPPMode = true;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000715 this->writeExpression(*decl.fValue, kAssignment_Precedence);
Ethan Nicholas82399462017-10-16 12:35:44 -0400716 fCPPMode = false;
717 this->write(";\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400718 }
719 }
720 }
721 }
722}
723
Ethan Nicholas82399462017-10-16 12:35:44 -0400724static bool is_accessible(const Variable& var) {
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500725 const Type& type = var.fType.nonnullable();
726 return Type::kSampler_Kind != type.kind() &&
727 Type::kOther_Kind != type.kind();
Ethan Nicholas82399462017-10-16 12:35:44 -0400728}
729
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400730void CPPCodeGenerator::newExtraEmitCodeBlock() {
731 // This should only be called when emitting SKSL for emitCode(), which can be detected if the
732 // cpp buffer is not null, and the cpp buffer is not the current output.
733 SkASSERT(fCPPBuffer && fCPPBuffer != fOut);
734
735 // Start a new block as an empty string
736 fExtraEmitCodeBlocks.push_back("");
737 // Mark its location in the output buffer, uses ${\d} for the token since ${} will not occur in
738 // valid sksl and makes detection trivial.
739 this->writef("${%zu}", fExtraEmitCodeBlocks.size() - 1);
740}
741
742void CPPCodeGenerator::addExtraEmitCodeLine(const String& toAppend) {
743 SkASSERT(fExtraEmitCodeBlocks.size() > 0);
744 String& currentBlock = fExtraEmitCodeBlocks[fExtraEmitCodeBlocks.size() - 1];
745 // Automatically add indentation and newline
746 currentBlock += " " + toAppend + "\n";
747}
748
749void CPPCodeGenerator::flushEmittedCode() {
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400750 if (fCPPBuffer == nullptr) {
751 // Not actually within writeEmitCode() so nothing to flush
752 return;
753 }
754
755 StringStream* skslBuffer = static_cast<StringStream*>(fOut);
756
757 String sksl = skslBuffer->str();
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400758 // Empty the accumulation buffer since its current contents are consumed.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400759 skslBuffer->reset();
760
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400761 // Switch to the cpp buffer
Michael Ludwigd0440192018-09-07 14:24:52 +0000762 fOut = fCPPBuffer;
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400763
764 // Iterate through the sksl, keeping track of where the last statement ended (e.g. the latest
765 // encountered ';', '{', or '}'). If an extra emit code block token is encountered then the
766 // code from 0 to last statement end is sent to writeCodeAppend, the extra code block is
767 // appended to the cpp buffer, and then the sksl string is trimmed to start where the last
768 // statement left off (minus the encountered token).
769 size_t i = 0;
770 int flushPoint = -1;
771 int tokenStart = -1;
772 while (i < sksl.size()) {
773 if (tokenStart >= 0) {
774 // Looking for the end of the token
775 if (sksl[i] == '}') {
776 // Must append the sksl from 0 to flushPoint (inclusive) then the extra code
777 // accumulated in the block with index parsed from chars [tokenStart+2, i-1]
778 String toFlush = String(sksl.c_str(), flushPoint + 1);
779 // writeCodeAppend automatically removes the format args that it consumed, so
780 // fFormatArgs will be in a valid state for any future sksl
781 this->writeCodeAppend(toFlush);
782
783 int codeBlock = stoi(String(sksl.c_str() + tokenStart + 2, i - tokenStart - 2));
784 SkASSERT(codeBlock < (int) fExtraEmitCodeBlocks.size());
785 if (fExtraEmitCodeBlocks[codeBlock].size() > 0) {
786 this->write(fExtraEmitCodeBlocks[codeBlock].c_str());
787 }
788
789 // Now reset the sksl buffer to start after the flush point, but remove the token.
790 String compacted = String(sksl.c_str() + flushPoint + 1,
791 tokenStart - flushPoint - 1);
792 if (i < sksl.size() - 1) {
793 compacted += String(sksl.c_str() + i + 1, sksl.size() - i - 1);
794 }
795 sksl = compacted;
796
797 // And reset iteration
798 i = -1;
799 flushPoint = -1;
800 tokenStart = -1;
801 }
802 } else {
803 // Looking for the start of extra emit block tokens, and tracking when statements end
804 if (sksl[i] == ';' || sksl[i] == '{' || sksl[i] == '}') {
805 flushPoint = i;
806 } else if (i < sksl.size() - 1 && sksl[i] == '$' && sksl[i + 1] == '{') {
807 // found an extra emit code block token
808 tokenStart = i++;
809 }
810 }
811 i++;
Michael Ludwigd0440192018-09-07 14:24:52 +0000812 }
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400813
814 // Once we've gone through the sksl string to this point, there are no remaining extra emit
815 // code blocks to interleave, so append the remainder as usual.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400816 this->writeCodeAppend(sksl);
817
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400818 // After appending, switch back to the emptied sksl buffer and reset the extra code blocks
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400819 fOut = skslBuffer;
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400820 fExtraEmitCodeBlocks.clear();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400821}
822
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400823void CPPCodeGenerator::writeCodeAppend(const String& code) {
John Stiles50819422020-06-18 13:00:38 -0400824 if (!code.empty()) {
825 // Count % format specifiers.
826 size_t argCount = 0;
827 for (size_t index = 0; index < code.size(); ++index) {
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400828 if ('%' == code[index]) {
John Stiles50819422020-06-18 13:00:38 -0400829 if (index == code.size() - 1) {
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400830 break;
831 }
832 if (code[index + 1] != '%') {
833 ++argCount;
834 }
835 }
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400836 }
John Stiles50819422020-06-18 13:00:38 -0400837
838 // Emit the code string.
839 this->writef(" fragBuilder->codeAppendf(\n"
840 "R\"SkSL(%s)SkSL\"\n", code.c_str());
841 for (size_t i = 0; i < argCount; ++i) {
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400842 this->writef(", %s", fFormatArgs[i].c_str());
843 }
844 this->write(");\n");
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400845
John Stiles50819422020-06-18 13:00:38 -0400846 // argCount is equal to the number of fFormatArgs that were consumed, so they should be
847 // removed from the list.
848 if (argCount > 0) {
849 fFormatArgs.erase(fFormatArgs.begin(), fFormatArgs.begin() + argCount);
850 }
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400851 }
852}
853
854String CPPCodeGenerator::convertSKSLExpressionToCPP(const Expression& e,
855 const String& cppVar) {
856 // To do this conversion, we temporarily switch the sksl output stream
857 // to an empty stringstream and reset the format args to empty.
858 OutputStream* oldSKSL = fOut;
859 StringStream exprBuffer;
860 fOut = &exprBuffer;
861
862 std::vector<String> oldArgs(fFormatArgs);
863 fFormatArgs.clear();
864
865 // Convert the argument expression into a format string and args
866 this->writeExpression(e, Precedence::kTopLevel_Precedence);
867 std::vector<String> newArgs(fFormatArgs);
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400868 String expr = exprBuffer.str();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400869
870 // After generating, restore the original output stream and format args
871 fFormatArgs = oldArgs;
872 fOut = oldSKSL;
873
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400874 // The sksl written to exprBuffer is not processed by flushEmittedCode(), so any extra emit code
875 // block tokens won't get handled. So we need to strip them from the expression and stick them
876 // to the end of the original sksl stream.
877 String exprFormat = "";
878 int tokenStart = -1;
879 for (size_t i = 0; i < expr.size(); i++) {
880 if (tokenStart >= 0) {
881 if (expr[i] == '}') {
882 // End of the token, so append the token to fOut
883 fOut->write(expr.c_str() + tokenStart, i - tokenStart + 1);
884 tokenStart = -1;
885 }
886 } else {
887 if (i < expr.size() - 1 && expr[i] == '$' && expr[i + 1] == '{') {
888 tokenStart = i++;
889 } else {
890 exprFormat += expr[i];
891 }
892 }
893 }
894
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400895 // Now build the final C++ code snippet from the format string and args
896 String cppExpr;
John Stiles50819422020-06-18 13:00:38 -0400897 if (newArgs.empty()) {
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400898 // This was a static expression, so we can simplify the input
899 // color declaration in the emitted code to just a static string
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400900 cppExpr = "SkString " + cppVar + "(\"" + exprFormat + "\");";
John Stiles50819422020-06-18 13:00:38 -0400901 } else if (newArgs.size() == 1 && exprFormat == "%s") {
902 // If the format expression is simply "%s", we can avoid an expensive call to printf.
903 // This happens fairly often in codegen so it is worth simplifying.
904 cppExpr = "SkString " + cppVar + "(" + newArgs[0] + ");";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400905 } else {
906 // String formatting must occur dynamically, so have the C++ declaration
907 // use SkStringPrintf with the format args that were accumulated
908 // when the expression was written.
909 cppExpr = "SkString " + cppVar + " = SkStringPrintf(\"" + exprFormat + "\"";
910 for (size_t i = 0; i < newArgs.size(); i++) {
911 cppExpr += ", " + newArgs[i];
912 }
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400913 cppExpr += ");";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400914 }
915 return cppExpr;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400916}
917
Ethan Nicholas762466e2017-06-29 10:03:38 -0400918bool CPPCodeGenerator::writeEmitCode(std::vector<const Variable*>& uniforms) {
919 this->write(" void emitCode(EmitArgs& args) override {\n"
920 " GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;\n");
921 this->writef(" const %s& _outer = args.fFp.cast<%s>();\n"
922 " (void) _outer;\n",
923 fFullName.c_str(), fFullName.c_str());
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400924 for (const auto& p : fProgram) {
925 if (ProgramElement::kVar_Kind == p.fKind) {
John Stiles3dc0da62020-08-19 17:48:31 -0400926 const VarDeclarations& decls = p.as<VarDeclarations>();
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400927 for (const auto& raw : decls.fVars) {
John Stiles3dc0da62020-08-19 17:48:31 -0400928 VarDeclaration& decl = raw->as<VarDeclaration>();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000929 String nameString(decl.fVar->fName);
Ethan Nicholas82399462017-10-16 12:35:44 -0400930 const char* name = nameString.c_str();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000931 if (SectionAndParameterHelper::IsParameter(*decl.fVar) &&
932 is_accessible(*decl.fVar)) {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400933 this->writef(" auto %s = _outer.%s;\n"
Ethan Nicholas82399462017-10-16 12:35:44 -0400934 " (void) %s;\n",
935 name, name, name);
936 }
937 }
938 }
939 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400940 this->writePrivateVarValues();
941 for (const auto u : uniforms) {
942 this->addUniform(*u);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400943 }
John Stiles02b11282020-08-10 15:25:24 -0400944 this->writeSection(kEmitCodeSection);
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400945
946 // Save original buffer as the CPP buffer for flushEmittedCode()
947 fCPPBuffer = fOut;
948 StringStream skslBuffer;
949 fOut = &skslBuffer;
950
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400951 this->newExtraEmitCodeBlock();
Ethan Nicholas762466e2017-06-29 10:03:38 -0400952 bool result = INHERITED::generateCode();
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400953 this->flushEmittedCode();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400954
955 // Then restore the original CPP buffer and close the function
956 fOut = fCPPBuffer;
957 fCPPBuffer = nullptr;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400958 this->write(" }\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400959 return result;
960}
961
962void CPPCodeGenerator::writeSetData(std::vector<const Variable*>& uniforms) {
963 const char* fullName = fFullName.c_str();
John Stiles02b11282020-08-10 15:25:24 -0400964 const Section* section = fSectionAndParameterHelper.getSection(kSetDataSection);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400965 const char* pdman = section ? section->fArgument.c_str() : "pdman";
Ethan Nicholas762466e2017-06-29 10:03:38 -0400966 this->writef(" void onSetData(const GrGLSLProgramDataManager& %s, "
967 "const GrFragmentProcessor& _proc) override {\n",
968 pdman);
969 bool wroteProcessor = false;
John Stiles06f3d082020-06-04 11:07:21 -0400970 for (const Variable* u : uniforms) {
Michael Ludwiga4275592018-08-31 10:52:47 -0400971 if (is_uniform_in(*u)) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400972 if (!wroteProcessor) {
973 this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName, fullName);
974 wroteProcessor = true;
975 this->writef(" {\n");
976 }
Michael Ludwiga4275592018-08-31 10:52:47 -0400977
978 const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *u);
979 SkASSERT(mapper);
980
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700981 String nameString(u->fName);
982 const char* name = nameString.c_str();
Michael Ludwiga4275592018-08-31 10:52:47 -0400983
984 // Switches for setData behavior in the generated code
985 bool conditionalUniform = u->fModifiers.fLayout.fWhen != "";
986 bool isTracked = u->fModifiers.fLayout.fFlags & Layout::kTracked_Flag;
987 bool needsValueDeclaration = isTracked || !mapper->canInlineUniformValue();
988
989 String uniformName = HCodeGenerator::FieldName(name) + "Var";
990
991 String indent = " "; // 8 by default, 12 when nested for conditional uniforms
992 if (conditionalUniform) {
993 // Add a pre-check to make sure the uniform was emitted
994 // before trying to send any data to the GPU
995 this->writef(" if (%s.isValid()) {\n", uniformName.c_str());
996 indent += " ";
997 }
998
999 String valueVar = "";
1000 if (needsValueDeclaration) {
1001 valueVar.appendf("%sValue", name);
1002 // Use AccessType since that will match the return type of _outer's public API.
1003 String valueType = HCodeGenerator::AccessType(fContext, u->fType,
1004 u->fModifiers.fLayout);
Ethan Nicholasbcd51e82019-04-09 10:40:41 -04001005 this->writef("%s%s %s = _outer.%s;\n",
Michael Ludwiga4275592018-08-31 10:52:47 -04001006 indent.c_str(), valueType.c_str(), valueVar.c_str(), name);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001007 } else {
Michael Ludwiga4275592018-08-31 10:52:47 -04001008 // Not tracked and the mapper only needs to use the value once
1009 // so send it a safe expression instead of the variable name
Ethan Nicholasbcd51e82019-04-09 10:40:41 -04001010 valueVar.appendf("(_outer.%s)", name);
Michael Ludwiga4275592018-08-31 10:52:47 -04001011 }
1012
1013 if (isTracked) {
1014 SkASSERT(mapper->supportsTracking());
1015
1016 String prevVar = HCodeGenerator::FieldName(name) + "Prev";
1017 this->writef("%sif (%s) {\n"
1018 "%s %s;\n"
1019 "%s %s;\n"
1020 "%s}\n", indent.c_str(),
1021 mapper->dirtyExpression(valueVar, prevVar).c_str(), indent.c_str(),
1022 mapper->saveState(valueVar, prevVar).c_str(), indent.c_str(),
1023 mapper->setUniform(pdman, uniformName, valueVar).c_str(), indent.c_str());
1024 } else {
1025 this->writef("%s%s;\n", indent.c_str(),
1026 mapper->setUniform(pdman, uniformName, valueVar).c_str());
1027 }
1028
1029 if (conditionalUniform) {
1030 // Close the earlier precheck block
1031 this->writef(" }\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -04001032 }
1033 }
1034 }
1035 if (wroteProcessor) {
1036 this->writef(" }\n");
1037 }
Ethan Nicholas68990be2017-07-13 09:36:52 -04001038 if (section) {
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05001039 int samplerIndex = 0;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001040 for (const auto& p : fProgram) {
1041 if (ProgramElement::kVar_Kind == p.fKind) {
John Stiles3dc0da62020-08-19 17:48:31 -04001042 const VarDeclarations& decls = p.as<VarDeclarations>();
John Stiles06f3d082020-06-04 11:07:21 -04001043 for (const std::unique_ptr<Statement>& raw : decls.fVars) {
John Stiles3dc0da62020-08-19 17:48:31 -04001044 const VarDeclaration& decl = raw->as<VarDeclaration>();
John Stiles06f3d082020-06-04 11:07:21 -04001045 const Variable& variable = *decl.fVar;
1046 String nameString(variable.fName);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001047 const char* name = nameString.c_str();
John Stiles06f3d082020-06-04 11:07:21 -04001048 if (variable.fType.kind() == Type::kSampler_Kind) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +00001049 this->writef(" const GrSurfaceProxyView& %sView = "
1050 "_outer.textureSampler(%d).view();\n",
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05001051 name, samplerIndex);
Robert Phillipsbd99c0c2019-12-12 13:26:58 +00001052 this->writef(" GrTexture& %s = *%sView.proxy()->peekTexture();\n",
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05001053 name, name);
1054 this->writef(" (void) %s;\n", name);
1055 ++samplerIndex;
John Stiles06f3d082020-06-04 11:07:21 -04001056 } else if (needs_uniform_var(variable)) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001057 this->writef(" UniformHandle& %s = %sVar;\n"
1058 " (void) %s;\n",
1059 name, HCodeGenerator::FieldName(name).c_str(), name);
John Stiles06f3d082020-06-04 11:07:21 -04001060 } else if (SectionAndParameterHelper::IsParameter(variable) &&
1061 variable.fType != *fContext.fFragmentProcessor_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001062 if (!wroteProcessor) {
1063 this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName,
1064 fullName);
1065 wroteProcessor = true;
1066 }
John Stiles06f3d082020-06-04 11:07:21 -04001067
1068 if (variable.fType.nonnullable() != *fContext.fFragmentProcessor_Type) {
1069 this->writef(" auto %s = _outer.%s;\n"
1070 " (void) %s;\n",
1071 name, name, name);
1072 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001073 }
1074 }
1075 }
1076 }
John Stiles02b11282020-08-10 15:25:24 -04001077 this->writeSection(kSetDataSection);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001078 }
1079 this->write(" }\n");
1080}
1081
Brian Salomonf7dcd762018-07-30 14:48:15 -04001082void CPPCodeGenerator::writeOnTextureSampler() {
1083 bool foundSampler = false;
1084 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
1085 if (param->fType.kind() == Type::kSampler_Kind) {
1086 if (!foundSampler) {
1087 this->writef(
1088 "const GrFragmentProcessor::TextureSampler& %s::onTextureSampler(int "
1089 "index) const {\n",
1090 fFullName.c_str());
1091 this->writef(" return IthTextureSampler(index, %s",
1092 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
1093 foundSampler = true;
1094 } else {
1095 this->writef(", %s",
1096 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
1097 }
1098 }
1099 }
1100 if (foundSampler) {
1101 this->write(");\n}\n");
1102 }
1103}
1104
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001105void CPPCodeGenerator::writeClone() {
John Stiles02b11282020-08-10 15:25:24 -04001106 if (!this->writeSection(kCloneSection)) {
1107 if (fSectionAndParameterHelper.getSection(kFieldsSection)) {
John Stiles47b4e222020-08-12 09:56:50 -04001108 fErrors.error(/*offset=*/0, "fragment processors with custom @fields must also have a "
1109 "custom @clone");
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001110 }
1111 this->writef("%s::%s(const %s& src)\n"
Ethan Nicholasabff9562017-10-09 10:54:08 -04001112 ": INHERITED(k%s_ClassID, src.optimizationFlags())", fFullName.c_str(),
1113 fFullName.c_str(), fFullName.c_str(), fFullName.c_str());
John Stiles06f3d082020-06-04 11:07:21 -04001114 for (const Variable* param : fSectionAndParameterHelper.getParameters()) {
Robert Phillipsbce7d862019-02-21 22:53:57 +00001115 String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str());
John Stiles88183902020-06-10 16:40:38 -04001116 if (param->fType.nonnullable() != *fContext.fFragmentProcessor_Type) {
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001117 this->writef("\n, %s(src.%s)",
1118 fieldName.c_str(),
1119 fieldName.c_str());
1120 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001121 }
Ethan Nicholasabff9562017-10-09 10:54:08 -04001122 this->writef(" {\n");
Brian Osman12c5d292020-07-13 16:11:35 -04001123 this->writef(" this->cloneAndRegisterAllChildProcessors(src);\n");
Brian Salomonf7dcd762018-07-30 14:48:15 -04001124 int samplerCount = 0;
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001125 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
1126 if (param->fType.kind() == Type::kSampler_Kind) {
Brian Salomonf7dcd762018-07-30 14:48:15 -04001127 ++samplerCount;
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001128 }
1129 }
Brian Salomonf7dcd762018-07-30 14:48:15 -04001130 if (samplerCount) {
1131 this->writef(" this->setTextureSamplerCnt(%d);", samplerCount);
1132 }
Michael Ludwige88320b2020-06-24 09:04:56 -04001133 if (fAccessSampleCoordsDirectly) {
1134 this->writef(" this->setUsesSampleCoordsDirectly();\n");
1135 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001136 this->write("}\n");
Brian Salomonaff329b2017-08-11 09:40:37 -04001137 this->writef("std::unique_ptr<GrFragmentProcessor> %s::clone() const {\n",
1138 fFullName.c_str());
John Stilesfbd050b2020-08-03 13:21:46 -04001139 this->writef(" return std::make_unique<%s>(*this);\n",
Brian Salomonaff329b2017-08-11 09:40:37 -04001140 fFullName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001141 this->write("}\n");
1142 }
1143}
1144
John Stiles47b4e222020-08-12 09:56:50 -04001145void CPPCodeGenerator::writeDumpInfo() {
John Stiles8d9bf642020-08-12 15:07:45 -04001146 this->writef("#if GR_TEST_UTILS\n"
John Stilescab58862020-08-12 15:47:06 -04001147 "SkString %s::onDumpInfo() const {\n", fFullName.c_str());
John Stiles47b4e222020-08-12 09:56:50 -04001148
1149 if (!this->writeSection(kDumpInfoSection)) {
1150 if (fSectionAndParameterHelper.getSection(kFieldsSection)) {
1151 fErrors.error(/*offset=*/0, "fragment processors with custom @fields must also have a "
1152 "custom @dumpInfo");
1153 }
1154
John Stiles47b4e222020-08-12 09:56:50 -04001155 String formatString;
1156 std::vector<String> argumentList;
1157
1158 for (const Variable* param : fSectionAndParameterHelper.getParameters()) {
1159 // dumpInfo() doesn't need to log child FPs.
1160 if (param->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
1161 continue;
1162 }
1163
1164 // Add this field onto the format string and argument list.
1165 String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str());
1166 String runtimeValue = this->formatRuntimeValue(param->fType, param->fModifiers.fLayout,
1167 param->fName, &argumentList);
1168 formatString.appendf("%s%s=%s",
1169 formatString.empty() ? "" : ", ",
1170 fieldName.c_str(),
1171 runtimeValue.c_str());
1172 }
1173
John Stiles47b4e222020-08-12 09:56:50 -04001174 if (!formatString.empty()) {
John Stilescab58862020-08-12 15:47:06 -04001175 // Emit the finished format string and associated arguments.
1176 this->writef(" return SkStringPrintf(\"(%s)\"", formatString.c_str());
John Stiles47b4e222020-08-12 09:56:50 -04001177
John Stilescab58862020-08-12 15:47:06 -04001178 for (const String& argument : argumentList) {
1179 this->writef(", %s", argument.c_str());
1180 }
John Stiles47b4e222020-08-12 09:56:50 -04001181
John Stilescab58862020-08-12 15:47:06 -04001182 this->write(");");
1183 } else {
1184 // No fields to dump at all; just return an empty string.
1185 this->write(" return SkString();");
1186 }
John Stiles47b4e222020-08-12 09:56:50 -04001187 }
1188
John Stilescab58862020-08-12 15:47:06 -04001189 this->write("\n"
1190 "}\n"
John Stiles47b4e222020-08-12 09:56:50 -04001191 "#endif\n");
1192}
1193
Ethan Nicholas762466e2017-06-29 10:03:38 -04001194void CPPCodeGenerator::writeTest() {
John Stiles02b11282020-08-10 15:25:24 -04001195 const Section* test = fSectionAndParameterHelper.getSection(kTestCodeSection);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001196 if (test) {
Brian Salomonaff329b2017-08-11 09:40:37 -04001197 this->writef(
1198 "GR_DEFINE_FRAGMENT_PROCESSOR_TEST(%s);\n"
1199 "#if GR_TEST_UTILS\n"
1200 "std::unique_ptr<GrFragmentProcessor> %s::TestCreate(GrProcessorTestData* %s) {\n",
1201 fFullName.c_str(),
1202 fFullName.c_str(),
1203 test->fArgument.c_str());
John Stiles02b11282020-08-10 15:25:24 -04001204 this->writeSection(kTestCodeSection);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001205 this->write("}\n"
1206 "#endif\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -04001207 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001208}
1209
1210void CPPCodeGenerator::writeGetKey() {
1211 this->writef("void %s::onGetGLSLProcessorKey(const GrShaderCaps& caps, "
1212 "GrProcessorKeyBuilder* b) const {\n",
1213 fFullName.c_str());
Ethan Nicholascab767f2019-07-01 13:32:07 -04001214 for (const auto& p : fProgram) {
1215 if (ProgramElement::kVar_Kind == p.fKind) {
John Stiles3dc0da62020-08-19 17:48:31 -04001216 const VarDeclarations& decls = p.as<VarDeclarations>();
Ethan Nicholascab767f2019-07-01 13:32:07 -04001217 for (const auto& raw : decls.fVars) {
John Stiles3dc0da62020-08-19 17:48:31 -04001218 const VarDeclaration& decl = raw->as<VarDeclaration>();
Ethan Nicholascab767f2019-07-01 13:32:07 -04001219 const Variable& var = *decl.fVar;
1220 String nameString(var.fName);
1221 const char* name = nameString.c_str();
1222 if (var.fModifiers.fLayout.fKey != Layout::kNo_Key &&
1223 (var.fModifiers.fFlags & Modifiers::kUniform_Flag)) {
1224 fErrors.error(var.fOffset,
1225 "layout(key) may not be specified on uniforms");
Ethan Nicholasbcd51e82019-04-09 10:40:41 -04001226 }
Ethan Nicholascab767f2019-07-01 13:32:07 -04001227 switch (var.fModifiers.fLayout.fKey) {
1228 case Layout::kKey_Key:
1229 if (is_private(var)) {
1230 this->writef("%s %s =",
1231 HCodeGenerator::FieldType(fContext, var.fType,
1232 var.fModifiers.fLayout).c_str(),
1233 String(var.fName).c_str());
1234 if (decl.fValue) {
1235 fCPPMode = true;
1236 this->writeExpression(*decl.fValue, kAssignment_Precedence);
1237 fCPPMode = false;
1238 } else {
1239 this->writef("%s", default_value(var).c_str());
1240 }
1241 this->write(";\n");
1242 }
1243 if (var.fModifiers.fLayout.fWhen.fLength) {
1244 this->writef("if (%s) {", String(var.fModifiers.fLayout.fWhen).c_str());
1245 }
John Stilesb3038f82020-07-27 17:33:25 -04001246 if (var.fType == *fContext.fHalf4_Type) {
Ethan Nicholascab767f2019-07-01 13:32:07 -04001247 this->writef(" uint16_t red = SkFloatToHalf(%s.fR);\n",
1248 HCodeGenerator::FieldName(name).c_str());
1249 this->writef(" uint16_t green = SkFloatToHalf(%s.fG);\n",
1250 HCodeGenerator::FieldName(name).c_str());
1251 this->writef(" uint16_t blue = SkFloatToHalf(%s.fB);\n",
1252 HCodeGenerator::FieldName(name).c_str());
1253 this->writef(" uint16_t alpha = SkFloatToHalf(%s.fA);\n",
1254 HCodeGenerator::FieldName(name).c_str());
1255 this->write(" b->add32(((uint32_t)red << 16) | green);\n");
1256 this->write(" b->add32(((uint32_t)blue << 16) | alpha);\n");
John Stiles45f5b032020-07-27 17:31:29 -04001257 } else if (var.fType == *fContext.fHalf_Type ||
1258 var.fType == *fContext.fFloat_Type) {
1259 this->writef(" b->add32(sk_bit_cast<uint32_t>(%s));\n",
Ethan Nicholascab767f2019-07-01 13:32:07 -04001260 HCodeGenerator::FieldName(name).c_str());
John Stiles45f5b032020-07-27 17:31:29 -04001261 } else if (var.fType.isInteger() || var.fType == *fContext.fBool_Type ||
1262 var.fType.kind() == Type::kEnum_Kind) {
1263 this->writef(" b->add32((uint32_t) %s);\n",
1264 HCodeGenerator::FieldName(name).c_str());
1265 } else {
1266 ABORT("NOT YET IMPLEMENTED: automatic key handling for %s\n",
1267 var.fType.displayName().c_str());
Ethan Nicholascab767f2019-07-01 13:32:07 -04001268 }
1269 if (var.fModifiers.fLayout.fWhen.fLength) {
1270 this->write("}");
1271 }
1272 break;
1273 case Layout::kIdentity_Key:
1274 if (var.fType.kind() != Type::kMatrix_Kind) {
1275 fErrors.error(var.fOffset,
1276 "layout(key=identity) requires matrix type");
1277 }
1278 this->writef(" b->add32(%s.isIdentity() ? 1 : 0);\n",
1279 HCodeGenerator::FieldName(name).c_str());
1280 break;
1281 case Layout::kNo_Key:
1282 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001283 }
Ethan Nicholascab767f2019-07-01 13:32:07 -04001284 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001285 }
1286 }
1287 this->write("}\n");
1288}
1289
1290bool CPPCodeGenerator::generateCode() {
1291 std::vector<const Variable*> uniforms;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001292 for (const auto& p : fProgram) {
1293 if (ProgramElement::kVar_Kind == p.fKind) {
John Stiles3dc0da62020-08-19 17:48:31 -04001294 const VarDeclarations& decls = p.as<VarDeclarations>();
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001295 for (const auto& raw : decls.fVars) {
John Stiles3dc0da62020-08-19 17:48:31 -04001296 VarDeclaration& decl = raw->as<VarDeclaration>();
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001297 if ((decl.fVar->fModifiers.fFlags & Modifiers::kUniform_Flag) &&
1298 decl.fVar->fType.kind() != Type::kSampler_Kind) {
1299 uniforms.push_back(decl.fVar);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001300 }
Michael Ludwiga4275592018-08-31 10:52:47 -04001301
1302 if (is_uniform_in(*decl.fVar)) {
1303 // Validate the "uniform in" declarations to make sure they are fully supported,
1304 // instead of generating surprising C++
1305 const UniformCTypeMapper* mapper =
1306 UniformCTypeMapper::Get(fContext, *decl.fVar);
1307 if (mapper == nullptr) {
1308 fErrors.error(decl.fOffset, String(decl.fVar->fName)
1309 + "'s type is not supported for use as a 'uniform in'");
1310 return false;
1311 }
1312 if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
1313 if (!mapper->supportsTracking()) {
1314 fErrors.error(decl.fOffset, String(decl.fVar->fName)
1315 + "'s type does not support state tracking");
1316 return false;
1317 }
1318 }
1319
1320 } else {
1321 // If it's not a uniform_in, it's an error to be tracked
1322 if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
1323 fErrors.error(decl.fOffset, "Non-'in uniforms' cannot be tracked");
1324 return false;
1325 }
1326 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001327 }
1328 }
1329 }
1330 const char* baseName = fName.c_str();
1331 const char* fullName = fFullName.c_str();
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05001332 this->writef("%s\n", HCodeGenerator::GetHeader(fProgram, fErrors).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001333 this->writef(kFragmentProcessorHeader, fullName);
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001334 this->writef("#include \"%s.h\"\n\n", fullName);
John Stiles02b11282020-08-10 15:25:24 -04001335 this->writeSection(kCppSection);
John Stiles45f5b032020-07-27 17:31:29 -04001336 this->writef("#include \"src/core/SkUtils.h\"\n"
1337 "#include \"src/gpu/GrTexture.h\"\n"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001338 "#include \"src/gpu/glsl/GrGLSLFragmentProcessor.h\"\n"
1339 "#include \"src/gpu/glsl/GrGLSLFragmentShaderBuilder.h\"\n"
1340 "#include \"src/gpu/glsl/GrGLSLProgramBuilder.h\"\n"
1341 "#include \"src/sksl/SkSLCPP.h\"\n"
1342 "#include \"src/sksl/SkSLUtil.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -04001343 "class GrGLSL%s : public GrGLSLFragmentProcessor {\n"
1344 "public:\n"
1345 " GrGLSL%s() {}\n",
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04001346 baseName, baseName);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001347 bool result = this->writeEmitCode(uniforms);
1348 this->write("private:\n");
1349 this->writeSetData(uniforms);
1350 this->writePrivateVars();
1351 for (const auto& u : uniforms) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001352 if (needs_uniform_var(*u) && !(u->fModifiers.fFlags & Modifiers::kIn_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001353 this->writef(" UniformHandle %sVar;\n",
1354 HCodeGenerator::FieldName(String(u->fName).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001355 }
1356 }
Ethan Nicholas68990be2017-07-13 09:36:52 -04001357 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001358 if (needs_uniform_var(*param)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001359 this->writef(" UniformHandle %sVar;\n",
1360 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001361 }
1362 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001363 this->writef("};\n"
1364 "GrGLSLFragmentProcessor* %s::onCreateGLSLInstance() const {\n"
1365 " return new GrGLSL%s();\n"
1366 "}\n",
1367 fullName, baseName);
1368 this->writeGetKey();
1369 this->writef("bool %s::onIsEqual(const GrFragmentProcessor& other) const {\n"
1370 " const %s& that = other.cast<%s>();\n"
1371 " (void) that;\n",
1372 fullName, fullName, fullName);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001373 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001374 if (param->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -04001375 continue;
1376 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001377 String nameString(param->fName);
1378 const char* name = nameString.c_str();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001379 this->writef(" if (%s != that.%s) return false;\n",
1380 HCodeGenerator::FieldName(name).c_str(),
1381 HCodeGenerator::FieldName(name).c_str());
1382 }
1383 this->write(" return true;\n"
1384 "}\n");
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001385 this->writeClone();
John Stiles47b4e222020-08-12 09:56:50 -04001386 this->writeDumpInfo();
Brian Salomonf7dcd762018-07-30 14:48:15 -04001387 this->writeOnTextureSampler();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001388 this->writeTest();
John Stiles02b11282020-08-10 15:25:24 -04001389 this->writeSection(kCppEndSection);
Greg Daniel3e8c3452018-04-06 10:37:55 -04001390
Ethan Nicholas762466e2017-06-29 10:03:38 -04001391 result &= 0 == fErrors.errorCount();
1392 return result;
1393}
1394
John Stilesa6841be2020-08-06 14:11:56 -04001395} // namespace SkSL