blob: 8a151244917b5d151a3efdfa070dac7539bf67cd [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) {
87 SkASSERT(b.fLeft->fKind == Expression::kVariableReference_Kind);
88 var = &((VariableReference&) *b.fLeft).fVariable;
89 } else {
90 SkASSERT(b.fRight->fKind == Expression::kVariableReference_Kind);
91 var = &((VariableReference&) *b.fRight).fVariable;
92 }
93 SkASSERT(var->fType.kind() == Type::kNullable_Kind &&
94 var->fType.componentType() == *fContext.fFragmentProcessor_Type);
95 this->write("%s");
Brian Osman12c5d292020-07-13 16:11:35 -040096 const char* op = "";
Ethan Nicholasee1c8a72019-02-22 10:50:47 -050097 switch (b.fOperator) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -040098 case Token::Kind::TK_EQEQ:
Brian Osman12c5d292020-07-13 16:11:35 -040099 op = "!";
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500100 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400101 case Token::Kind::TK_NEQ:
Brian Osman12c5d292020-07-13 16:11:35 -0400102 op = "";
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500103 break;
104 default:
105 SkASSERT(false);
106 }
Brian Osman12c5d292020-07-13 16:11:35 -0400107 int childIndex = this->getChildFPIndex(*var);
108 fFormatArgs.push_back(String(op) + "_outer.childProcessor(" + to_string(childIndex) +
109 ") ? \"true\" : \"false\"");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400110 } else {
111 INHERITED::writeBinaryExpression(b, parentPrecedence);
112 }
113}
114
115void CPPCodeGenerator::writeIndexExpression(const IndexExpression& i) {
116 const Expression& base = *i.fBase;
117 if (base.fKind == Expression::kVariableReference_Kind) {
118 int builtin = ((VariableReference&) base).fVariable.fModifiers.fLayout.fBuiltin;
Michael Ludwigfc2fdf02020-06-29 17:20:13 -0400119 if (SK_TEXTURESAMPLERS_BUILTIN == builtin) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400120 this->write("%s");
121 if (i.fIndex->fKind != Expression::kIntLiteral_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700122 fErrors.error(i.fIndex->fOffset,
Ethan Nicholas762466e2017-06-29 10:03:38 -0400123 "index into sk_TextureSamplers must be an integer literal");
124 return;
125 }
126 int64_t index = ((IntLiteral&) *i.fIndex).fValue;
127 fFormatArgs.push_back(" fragBuilder->getProgramBuilder()->samplerVariable("
Stephen Whited523a062019-06-19 13:12:46 -0400128 "args.fTexSamplers[" + to_string(index) + "])");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400129 return;
130 }
131 }
132 INHERITED::writeIndexExpression(i);
133}
134
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400135static String default_value(const Type& type) {
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500136 if (type.fName == "bool") {
137 return "false";
138 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400139 switch (type.kind()) {
140 case Type::kScalar_Kind: return "0";
141 case Type::kVector_Kind: return type.name() + "(0)";
142 case Type::kMatrix_Kind: return type.name() + "(1)";
143 default: ABORT("unsupported default_value type\n");
144 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400145}
146
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500147static String default_value(const Variable& var) {
Brian Osman495993a2018-10-16 15:45:55 -0400148 if (var.fModifiers.fLayout.fCType == SkSL::Layout::CType::kSkPMColor4f) {
Brian Osmanf28e55d2018-10-03 16:35:54 -0400149 return "{SK_FloatNaN, SK_FloatNaN, SK_FloatNaN, SK_FloatNaN}";
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500150 }
151 return default_value(var.fType);
152}
153
Ethan Nicholas762466e2017-06-29 10:03:38 -0400154static bool is_private(const Variable& var) {
155 return !(var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
156 !(var.fModifiers.fFlags & Modifiers::kIn_Flag) &&
157 var.fStorage == Variable::kGlobal_Storage &&
158 var.fModifiers.fLayout.fBuiltin == -1;
159}
160
Michael Ludwiga4275592018-08-31 10:52:47 -0400161static bool is_uniform_in(const Variable& var) {
162 return (var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
163 (var.fModifiers.fFlags & Modifiers::kIn_Flag) &&
164 var.fType.kind() != Type::kSampler_Kind;
165}
166
John Stilesa1df23c2020-08-11 17:19:55 -0400167String CPPCodeGenerator::formatRuntimeValue(const Type& type,
168 const Layout& layout,
169 const String& cppCode,
170 std::vector<String>* formatArgs) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400171 if (type.isFloat()) {
John Stilesa1df23c2020-08-11 17:19:55 -0400172 formatArgs->push_back(cppCode);
173 return "%f";
174 }
175 if (type == *fContext.fInt_Type) {
176 formatArgs->push_back(cppCode);
177 return "%d";
178 }
179 if (type == *fContext.fBool_Type) {
180 formatArgs->push_back("(" + cppCode + " ? \"true\" : \"false\")");
181 return "%s";
182 }
183 if (type == *fContext.fFloat2_Type || type == *fContext.fHalf2_Type) {
184 formatArgs->push_back(cppCode + ".fX");
185 formatArgs->push_back(cppCode + ".fY");
186 return type.name() + "(%f, %f)";
187 }
188 if (type == *fContext.fFloat3_Type || type == *fContext.fHalf3_Type) {
189 formatArgs->push_back(cppCode + ".fX");
190 formatArgs->push_back(cppCode + ".fY");
191 formatArgs->push_back(cppCode + ".fZ");
192 return type.name() + "(%f, %f, %f)";
193 }
194 if (type == *fContext.fFloat4_Type || type == *fContext.fHalf4_Type) {
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400195 switch (layout.fCType) {
196 case Layout::CType::kSkPMColor:
John Stilesa1df23c2020-08-11 17:19:55 -0400197 formatArgs->push_back("SkGetPackedR32(" + cppCode + ") / 255.0");
198 formatArgs->push_back("SkGetPackedG32(" + cppCode + ") / 255.0");
199 formatArgs->push_back("SkGetPackedB32(" + cppCode + ") / 255.0");
200 formatArgs->push_back("SkGetPackedA32(" + cppCode + ") / 255.0");
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400201 break;
Brian Osmanf28e55d2018-10-03 16:35:54 -0400202 case Layout::CType::kSkPMColor4f:
John Stilesa1df23c2020-08-11 17:19:55 -0400203 formatArgs->push_back(cppCode + ".fR");
204 formatArgs->push_back(cppCode + ".fG");
205 formatArgs->push_back(cppCode + ".fB");
206 formatArgs->push_back(cppCode + ".fA");
Brian Osmanf28e55d2018-10-03 16:35:54 -0400207 break;
Mike Reedb26b4e72020-01-22 14:31:21 -0500208 case Layout::CType::kSkV4:
John Stilesa1df23c2020-08-11 17:19:55 -0400209 formatArgs->push_back(cppCode + ".x");
210 formatArgs->push_back(cppCode + ".y");
211 formatArgs->push_back(cppCode + ".z");
212 formatArgs->push_back(cppCode + ".w");
Brian Salomoneca66b32019-06-01 11:18:15 -0400213 break;
John Stilesa1df23c2020-08-11 17:19:55 -0400214 case Layout::CType::kSkRect:
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400215 case Layout::CType::kDefault:
John Stilesa1df23c2020-08-11 17:19:55 -0400216 formatArgs->push_back(cppCode + ".left()");
217 formatArgs->push_back(cppCode + ".top()");
218 formatArgs->push_back(cppCode + ".right()");
219 formatArgs->push_back(cppCode + ".bottom()");
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400220 break;
221 default:
222 SkASSERT(false);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400223 }
John Stilesa1df23c2020-08-11 17:19:55 -0400224 return type.name() + "(%f, %f, %f, %f)";
Ethan Nicholas762466e2017-06-29 10:03:38 -0400225 }
John Stilesa1df23c2020-08-11 17:19:55 -0400226 if (type.kind() == Type::kMatrix_Kind) {
227 SkASSERT(type.componentType() == *fContext.fFloat_Type ||
228 type.componentType() == *fContext.fHalf_Type);
229
230 String format = type.name() + "(";
231 for (int c = 0; c < type.columns(); ++c) {
232 for (int r = 0; r < type.rows(); ++r) {
233 String& arg = formatArgs->emplace_back();
234 arg.appendf("%s.rc(%d, %d)", cppCode.c_str(), r, c);
235 format += "%f, ";
236 }
237 }
238
239 // Replace trailing ", " with ")".
240 format.pop_back();
241 format.back() = ')';
242 return format;
243 }
244 if (type.kind() == Type::kEnum_Kind) {
245 formatArgs->push_back("(int) " + cppCode);
246 return "%d";
247 }
248 if (type == *fContext.fInt4_Type ||
249 type == *fContext.fShort4_Type ||
250 type == *fContext.fByte4_Type) {
251 formatArgs->push_back(cppCode + ".left()");
252 formatArgs->push_back(cppCode + ".top()");
253 formatArgs->push_back(cppCode + ".right()");
254 formatArgs->push_back(cppCode + ".bottom()");
255 return type.name() + "(%d, %d, %d, %d)";
256 }
257
258 SkDEBUGFAILF("unsupported runtime value type '%s'\n", String(type.fName).c_str());
259 return "";
260}
261
262void CPPCodeGenerator::writeRuntimeValue(const Type& type, const Layout& layout,
263 const String& cppCode) {
264 this->write(this->formatRuntimeValue(type, layout, cppCode, &fFormatArgs));
Ethan Nicholas762466e2017-06-29 10:03:38 -0400265}
266
267void CPPCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) {
268 if (is_private(var)) {
Ethan Nicholasd608c092017-10-26 09:30:08 -0400269 this->writeRuntimeValue(var.fType, var.fModifiers.fLayout, var.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400270 } else {
271 this->writeExpression(value, kTopLevel_Precedence);
272 }
273}
274
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400275String CPPCodeGenerator::getSamplerHandle(const Variable& var) {
276 int samplerCount = 0;
Ethan Nicholas68990be2017-07-13 09:36:52 -0400277 for (const auto param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400278 if (&var == param) {
279 return "args.fTexSamplers[" + to_string(samplerCount) + "]";
280 }
281 if (param->fType.kind() == Type::kSampler_Kind) {
282 ++samplerCount;
283 }
284 }
285 ABORT("should have found sampler in parameters\n");
286}
287
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400288void CPPCodeGenerator::writeIntLiteral(const IntLiteral& i) {
289 this->write(to_string((int32_t) i.fValue));
290}
291
Ethan Nicholas82399462017-10-16 12:35:44 -0400292void CPPCodeGenerator::writeSwizzle(const Swizzle& swizzle) {
293 if (fCPPMode) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400294 SkASSERT(swizzle.fComponents.size() == 1); // no support for multiple swizzle components yet
Ethan Nicholas82399462017-10-16 12:35:44 -0400295 this->writeExpression(*swizzle.fBase, kPostfix_Precedence);
296 switch (swizzle.fComponents[0]) {
297 case 0: this->write(".left()"); break;
298 case 1: this->write(".top()"); break;
299 case 2: this->write(".right()"); break;
300 case 3: this->write(".bottom()"); break;
301 }
302 } else {
303 INHERITED::writeSwizzle(swizzle);
304 }
305}
306
Ethan Nicholas762466e2017-06-29 10:03:38 -0400307void CPPCodeGenerator::writeVariableReference(const VariableReference& ref) {
Ethan Nicholas82399462017-10-16 12:35:44 -0400308 if (fCPPMode) {
309 this->write(ref.fVariable.fName);
310 return;
311 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400312 switch (ref.fVariable.fModifiers.fLayout.fBuiltin) {
313 case SK_INCOLOR_BUILTIN:
314 this->write("%s");
Michael Ludwig231de032018-08-30 14:33:01 -0400315 // EmitArgs.fInputColor is automatically set to half4(1) if
316 // no input was specified
317 fFormatArgs.push_back(String("args.fInputColor"));
Ethan Nicholas762466e2017-06-29 10:03:38 -0400318 break;
319 case SK_OUTCOLOR_BUILTIN:
320 this->write("%s");
321 fFormatArgs.push_back(String("args.fOutputColor"));
322 break;
Michael Ludwigfc2fdf02020-06-29 17:20:13 -0400323 case SK_MAIN_COORDS_BUILTIN:
324 this->write("%s");
325 fFormatArgs.push_back(String("args.fSampleCoord"));
326 fAccessSampleCoordsDirectly = true;
327 break;
Ethan Nicholascd700e92018-08-24 16:43:57 -0400328 case SK_WIDTH_BUILTIN:
329 this->write("sk_Width");
330 break;
331 case SK_HEIGHT_BUILTIN:
332 this->write("sk_Height");
333 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400334 default:
335 if (ref.fVariable.fType.kind() == Type::kSampler_Kind) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400336 this->write("%s");
337 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerVariable(" +
Stephen Whited523a062019-06-19 13:12:46 -0400338 this->getSamplerHandle(ref.fVariable) + ")");
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400339 return;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400340 }
341 if (ref.fVariable.fModifiers.fFlags & Modifiers::kUniform_Flag) {
342 this->write("%s");
343 String name = ref.fVariable.fName;
Brian Osman1cb41712017-10-19 12:54:52 -0400344 String var = String::printf("args.fUniformHandler->getUniformCStr(%sVar)",
345 HCodeGenerator::FieldName(name.c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400346 String code;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400347 if (ref.fVariable.fModifiers.fLayout.fWhen.fLength) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400348 code = String::printf("%sVar.isValid() ? %s : \"%s\"",
349 HCodeGenerator::FieldName(name.c_str()).c_str(),
350 var.c_str(),
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400351 default_value(ref.fVariable.fType).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400352 } else {
353 code = var;
354 }
355 fFormatArgs.push_back(code);
356 } else if (SectionAndParameterHelper::IsParameter(ref.fVariable)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700357 String name(ref.fVariable.fName);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400358 this->writeRuntimeValue(ref.fVariable.fType, ref.fVariable.fModifiers.fLayout,
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400359 String::printf("_outer.%s", name.c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400360 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700361 this->write(ref.fVariable.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400362 }
363 }
364}
365
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400366void CPPCodeGenerator::writeIfStatement(const IfStatement& s) {
367 if (s.fIsStatic) {
368 this->write("@");
369 }
370 INHERITED::writeIfStatement(s);
371}
372
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400373void CPPCodeGenerator::writeReturnStatement(const ReturnStatement& s) {
374 if (fInMain) {
375 fErrors.error(s.fOffset, "fragmentProcessor main() may not contain return statements");
376 }
377 INHERITED::writeReturnStatement(s);
378}
379
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400380void CPPCodeGenerator::writeSwitchStatement(const SwitchStatement& s) {
381 if (s.fIsStatic) {
382 this->write("@");
383 }
384 INHERITED::writeSwitchStatement(s);
385}
386
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400387void CPPCodeGenerator::writeFieldAccess(const FieldAccess& access) {
388 if (access.fBase->fType.name() == "fragmentProcessor") {
389 // Special field access on fragment processors are converted into function calls on
390 // GrFragmentProcessor's getters.
391 if (access.fBase->fKind != Expression::kVariableReference_Kind) {
392 fErrors.error(access.fBase->fOffset, "fragmentProcessor must be a reference\n");
393 return;
394 }
395
396 const Type::Field& field = fContext.fFragmentProcessor_Type->fields()[access.fFieldIndex];
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500397 const Variable& var = ((const VariableReference&) *access.fBase).fVariable;
Brian Osman12c5d292020-07-13 16:11:35 -0400398 String cppAccess = String::printf("_outer.childProcessor(%d)->%s()",
399 this->getChildFPIndex(var),
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500400 String(field.fName).c_str());
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400401
402 if (fCPPMode) {
403 this->write(cppAccess.c_str());
404 } else {
405 writeRuntimeValue(*field.fType, Layout(), cppAccess);
406 }
407 return;
408 }
409 INHERITED::writeFieldAccess(access);
410}
411
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500412int CPPCodeGenerator::getChildFPIndex(const Variable& var) const {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400413 int index = 0;
414 bool found = false;
415 for (const auto& p : fProgram) {
416 if (ProgramElement::kVar_Kind == p.fKind) {
417 const VarDeclarations& decls = (const VarDeclarations&) p;
418 for (const auto& raw : decls.fVars) {
419 const VarDeclaration& decl = (VarDeclaration&) *raw;
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500420 if (decl.fVar == &var) {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400421 found = true;
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500422 } else if (decl.fVar->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400423 ++index;
424 }
425 }
426 }
427 if (found) {
428 break;
429 }
430 }
431 SkASSERT(found);
432 return index;
433}
434
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400435void CPPCodeGenerator::writeFunctionCall(const FunctionCall& c) {
Ethan Nicholas13863662019-07-29 13:05:15 -0400436 if (c.fFunction.fBuiltin && c.fFunction.fName == "sample" &&
437 c.fArguments[0]->fType.kind() != Type::Kind::kSampler_Kind) {
Leon Scroggins III982fff22020-07-31 14:09:06 -0400438 // Validity checks that are detected by function definition in sksl_fp.inc
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400439 SkASSERT(c.fArguments.size() >= 1 && c.fArguments.size() <= 3);
Florin Malita390f9bd2019-03-04 12:25:57 -0500440 SkASSERT("fragmentProcessor" == c.fArguments[0]->fType.name() ||
441 "fragmentProcessor?" == c.fArguments[0]->fType.name());
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400442
443 // Actually fail during compilation if arguments with valid types are
Ethan Nicholas13863662019-07-29 13:05:15 -0400444 // provided that are not variable references, since sample() is a
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400445 // special function that impacts code emission.
446 if (c.fArguments[0]->fKind != Expression::kVariableReference_Kind) {
447 fErrors.error(c.fArguments[0]->fOffset,
Ethan Nicholas13863662019-07-29 13:05:15 -0400448 "sample()'s fragmentProcessor argument must be a variable reference\n");
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400449 return;
450 }
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500451 const Variable& child = ((const VariableReference&) *c.fArguments[0]).fVariable;
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400452
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400453 // Start a new extra emit code section so that the emitted child processor can depend on
454 // sksl variables defined in earlier sksl code.
455 this->newExtraEmitCodeBlock();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400456
Michael Ludwige88320b2020-06-24 09:04:56 -0400457 String inputColor;
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400458 if (c.fArguments.size() > 1 && c.fArguments[1]->fType.name() == "half4") {
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400459 // Use the invokeChild() variant that accepts an input color, so convert the 2nd
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400460 // argument's expression into C++ code that produces sksl stored in an SkString.
Brian Osman12c5d292020-07-13 16:11:35 -0400461 String inputColorName = "_input" + to_string(c.fOffset);
John Stilesd060c9d2020-06-08 11:44:25 -0400462 addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments[1], inputColorName));
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400463
Michael Ludwige88320b2020-06-24 09:04:56 -0400464 // invokeChild() needs a char* and a pre-pended comma
465 inputColor = ", " + inputColorName + ".c_str()";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400466 }
467
Michael Ludwige88320b2020-06-24 09:04:56 -0400468 String inputCoord;
469 String invokeFunction = "invokeChild";
470 if (c.fArguments.back()->fType.name() == "float2") {
471 // Invoking child with explicit coordinates at this call site
472 inputCoord = "_coords" + to_string(c.fOffset);
473 addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments.back(), inputCoord));
474 inputCoord.append(".c_str()");
475 } else if (c.fArguments.back()->fType.name() == "float3x3") {
476 // Invoking child with a matrix, sampling relative to the input coords.
477 invokeFunction = "invokeChildWithMatrix";
Brian Osman1298bc42020-06-30 13:39:35 -0400478 SampleUsage usage = Analysis::GetSampleUsage(fProgram, child);
Michael Ludwige88320b2020-06-24 09:04:56 -0400479
Brian Osman1298bc42020-06-30 13:39:35 -0400480 if (!usage.hasUniformMatrix()) {
Michael Ludwige88320b2020-06-24 09:04:56 -0400481 inputCoord = "_matrix" + to_string(c.fOffset);
482 addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments.back(), inputCoord));
483 inputCoord.append(".c_str()");
484 }
485 // else pass in the empty string to rely on invokeChildWithMatrix's automatic uniform
486 // resolution
487 }
488 if (!inputCoord.empty()) {
489 inputCoord = ", " + inputCoord;
490 }
491
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400492 // Write the output handling after the possible input handling
Ethan Nicholas13863662019-07-29 13:05:15 -0400493 String childName = "_sample" + to_string(c.fOffset);
Brian Osman12c5d292020-07-13 16:11:35 -0400494 String childIndexStr = to_string(this->getChildFPIndex(child));
495 addExtraEmitCodeLine("SkString " + childName + " = this->" + invokeFunction + "(" +
496 childIndexStr + inputColor + ", args" + inputCoord + ");");
John Stiles50819422020-06-18 13:00:38 -0400497
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000498 this->write("%s");
499 fFormatArgs.push_back(childName + ".c_str()");
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400500 return;
501 }
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400502 if (c.fFunction.fBuiltin) {
503 INHERITED::writeFunctionCall(c);
504 } else {
505 this->write("%s");
506 fFormatArgs.push_back((String(c.fFunction.fName) + "_name.c_str()").c_str());
507 this->write("(");
508 const char* separator = "";
509 for (const auto& arg : c.fArguments) {
510 this->write(separator);
511 separator = ", ";
512 this->writeExpression(*arg, kSequence_Precedence);
513 }
514 this->write(")");
515 }
Ethan Nicholas13863662019-07-29 13:05:15 -0400516 if (c.fFunction.fBuiltin && c.fFunction.fName == "sample") {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400517 this->write(".%s");
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400518 SkASSERT(c.fArguments.size() >= 1);
519 SkASSERT(c.fArguments[0]->fKind == Expression::kVariableReference_Kind);
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400520 String sampler = this->getSamplerHandle(((VariableReference&) *c.fArguments[0]).fVariable);
521 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerSwizzle(" + sampler +
Greg Daniel369ee6b2019-12-02 15:30:02 -0500522 ").asString().c_str()");
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400523 }
524}
525
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400526static const char* glsltype_string(const Context& context, const Type& type) {
527 if (type == *context.fFloat_Type) {
528 return "kFloat_GrSLType";
529 } else if (type == *context.fHalf_Type) {
530 return "kHalf_GrSLType";
531 } else if (type == *context.fFloat2_Type) {
532 return "kFloat2_GrSLType";
533 } else if (type == *context.fHalf2_Type) {
534 return "kHalf2_GrSLType";
Ethan Nicholas8ae1b562019-12-17 15:18:02 -0500535 } else if (type == *context.fFloat3_Type) {
536 return "kFloat3_GrSLType";
537 } else if (type == *context.fHalf3_Type) {
538 return "kHalf3_GrSLType";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400539 } else if (type == *context.fFloat4_Type) {
540 return "kFloat4_GrSLType";
541 } else if (type == *context.fHalf4_Type) {
542 return "kHalf4_GrSLType";
Ethan Nicholas58430122020-04-14 09:54:02 -0400543 } else if (type == *context.fFloat2x2_Type) {
544 return "kFloat2x2_GrSLType";
545 } else if (type == *context.fHalf2x2_Type) {
546 return "kHalf2x2_GrSLType";
547 } else if (type == *context.fFloat3x3_Type) {
548 return "kFloat3x3_GrSLType";
549 } else if (type == *context.fHalf3x3_Type) {
550 return "kHalf3x3_GrSLType";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400551 } else if (type == *context.fFloat4x4_Type) {
552 return "kFloat4x4_GrSLType";
553 } else if (type == *context.fHalf4x4_Type) {
554 return "kHalf4x4_GrSLType";
555 } else if (type == *context.fVoid_Type) {
556 return "kVoid_GrSLType";
Ethan Nicholas8ae1b562019-12-17 15:18:02 -0500557 } else if (type.kind() == Type::kEnum_Kind) {
558 return "int";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400559 }
560 SkASSERT(false);
561 return nullptr;
562}
563
Ethan Nicholas762466e2017-06-29 10:03:38 -0400564void CPPCodeGenerator::writeFunction(const FunctionDefinition& f) {
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400565 const FunctionDeclaration& decl = f.fDeclaration;
Brian Osman08f986d2020-05-13 17:06:46 -0400566 if (decl.fBuiltin) {
567 return;
568 }
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400569 fFunctionHeader = "";
570 OutputStream* oldOut = fOut;
571 StringStream buffer;
572 fOut = &buffer;
573 if (decl.fName == "main") {
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400574 fInMain = true;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400575 for (const auto& s : ((Block&) *f.fBody).fStatements) {
576 this->writeStatement(*s);
577 this->writeLine();
578 }
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400579 fInMain = false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400580
581 fOut = oldOut;
582 this->write(fFunctionHeader);
583 this->write(buffer.str());
584 } else {
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400585 this->addExtraEmitCodeLine("SkString " + decl.fName + "_name;");
586 String args = "const GrShaderVar " + decl.fName + "_args[] = { ";
587 const char* separator = "";
588 for (const auto& param : decl.fParameters) {
589 args += String(separator) + "GrShaderVar(\"" + param->fName + "\", " +
590 glsltype_string(fContext, param->fType) + ")";
591 separator = ", ";
592 }
593 args += "};";
594 this->addExtraEmitCodeLine(args.c_str());
595 for (const auto& s : ((Block&) *f.fBody).fStatements) {
596 this->writeStatement(*s);
597 this->writeLine();
598 }
599
600 fOut = oldOut;
601 String emit = "fragBuilder->emitFunction(";
602 emit += glsltype_string(fContext, decl.fReturnType);
603 emit += ", \"" + decl.fName + "\"";
604 emit += ", " + to_string((int64_t) decl.fParameters.size());
605 emit += ", " + decl.fName + "_args";
John Stiles50819422020-06-18 13:00:38 -0400606 emit += ",\nR\"SkSL(" + buffer.str() + ")SkSL\"";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400607 emit += ", &" + decl.fName + "_name);";
608 this->addExtraEmitCodeLine(emit.c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400609 }
610}
611
612void CPPCodeGenerator::writeSetting(const Setting& s) {
Brian Osmanf265afd2020-08-04 13:23:36 -0400613 this->write(s.fName.c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400614}
615
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400616bool CPPCodeGenerator::writeSection(const char* name, const char* prefix) {
Ethan Nicholas68990be2017-07-13 09:36:52 -0400617 const Section* s = fSectionAndParameterHelper.getSection(name);
618 if (s) {
619 this->writef("%s%s", prefix, s->fText.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400620 return true;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400621 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400622 return false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400623}
624
625void CPPCodeGenerator::writeProgramElement(const ProgramElement& p) {
626 if (p.fKind == ProgramElement::kSection_Kind) {
627 return;
628 }
629 if (p.fKind == ProgramElement::kVar_Kind) {
630 const VarDeclarations& decls = (const VarDeclarations&) p;
631 if (!decls.fVars.size()) {
632 return;
633 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000634 const Variable& var = *((VarDeclaration&) *decls.fVars[0]).fVar;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400635 if (var.fModifiers.fFlags & (Modifiers::kIn_Flag | Modifiers::kUniform_Flag) ||
636 -1 != var.fModifiers.fLayout.fBuiltin) {
637 return;
638 }
639 }
640 INHERITED::writeProgramElement(p);
641}
642
643void CPPCodeGenerator::addUniform(const Variable& var) {
644 if (!needs_uniform_var(var)) {
645 return;
646 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400647 if (var.fModifiers.fLayout.fWhen.fLength) {
648 this->writef(" if (%s) {\n ", String(var.fModifiers.fLayout.fWhen).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400649 }
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400650 const char* type = glsltype_string(fContext, var.fType);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700651 String name(var.fName);
Ethan Nicholas16464c32020-04-06 13:53:05 -0400652 this->writef(" %sVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag,"
653 " %s, \"%s\");\n", HCodeGenerator::FieldName(name.c_str()).c_str(), type,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700654 name.c_str());
Ethan Nicholasfc994162019-06-06 10:04:27 -0400655 if (var.fModifiers.fLayout.fWhen.fLength) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400656 this->write(" }\n");
657 }
658}
659
Ethan Nicholascd700e92018-08-24 16:43:57 -0400660void CPPCodeGenerator::writeInputVars() {
661}
662
Ethan Nicholas762466e2017-06-29 10:03:38 -0400663void CPPCodeGenerator::writePrivateVars() {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400664 for (const auto& p : fProgram) {
665 if (ProgramElement::kVar_Kind == p.fKind) {
666 const VarDeclarations& decls = (const VarDeclarations&) p;
667 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000668 VarDeclaration& decl = (VarDeclaration&) *raw;
669 if (is_private(*decl.fVar)) {
670 if (decl.fVar->fType == *fContext.fFragmentProcessor_Type) {
671 fErrors.error(decl.fOffset,
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400672 "fragmentProcessor variables must be declared 'in'");
673 return;
674 }
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500675 this->writef("%s %s = %s;\n",
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000676 HCodeGenerator::FieldType(fContext, decl.fVar->fType,
677 decl.fVar->fModifiers.fLayout).c_str(),
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500678 String(decl.fVar->fName).c_str(),
679 default_value(*decl.fVar).c_str());
Michael Ludwiga4275592018-08-31 10:52:47 -0400680 } else if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
681 // An auto-tracked uniform in variable, so add a field to hold onto the prior
682 // state. Note that tracked variables must be uniform in's and that is validated
683 // before writePrivateVars() is called.
684 const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *decl.fVar);
685 SkASSERT(mapper && mapper->supportsTracking());
686
687 String name = HCodeGenerator::FieldName(String(decl.fVar->fName).c_str());
688 // The member statement is different if the mapper reports a default value
689 if (mapper->defaultValue().size() > 0) {
690 this->writef("%s %sPrev = %s;\n",
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400691 Layout::CTypeToStr(mapper->ctype()), name.c_str(),
Michael Ludwiga4275592018-08-31 10:52:47 -0400692 mapper->defaultValue().c_str());
693 } else {
694 this->writef("%s %sPrev;\n",
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400695 Layout::CTypeToStr(mapper->ctype()), name.c_str());
Michael Ludwiga4275592018-08-31 10:52:47 -0400696 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400697 }
698 }
699 }
700 }
701}
702
703void CPPCodeGenerator::writePrivateVarValues() {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400704 for (const auto& p : fProgram) {
705 if (ProgramElement::kVar_Kind == p.fKind) {
706 const VarDeclarations& decls = (const VarDeclarations&) p;
707 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000708 VarDeclaration& decl = (VarDeclaration&) *raw;
709 if (is_private(*decl.fVar) && decl.fValue) {
710 this->writef("%s = ", String(decl.fVar->fName).c_str());
Ethan Nicholas82399462017-10-16 12:35:44 -0400711 fCPPMode = true;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000712 this->writeExpression(*decl.fValue, kAssignment_Precedence);
Ethan Nicholas82399462017-10-16 12:35:44 -0400713 fCPPMode = false;
714 this->write(";\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400715 }
716 }
717 }
718 }
719}
720
Ethan Nicholas82399462017-10-16 12:35:44 -0400721static bool is_accessible(const Variable& var) {
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500722 const Type& type = var.fType.nonnullable();
723 return Type::kSampler_Kind != type.kind() &&
724 Type::kOther_Kind != type.kind();
Ethan Nicholas82399462017-10-16 12:35:44 -0400725}
726
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400727void CPPCodeGenerator::newExtraEmitCodeBlock() {
728 // This should only be called when emitting SKSL for emitCode(), which can be detected if the
729 // cpp buffer is not null, and the cpp buffer is not the current output.
730 SkASSERT(fCPPBuffer && fCPPBuffer != fOut);
731
732 // Start a new block as an empty string
733 fExtraEmitCodeBlocks.push_back("");
734 // Mark its location in the output buffer, uses ${\d} for the token since ${} will not occur in
735 // valid sksl and makes detection trivial.
736 this->writef("${%zu}", fExtraEmitCodeBlocks.size() - 1);
737}
738
739void CPPCodeGenerator::addExtraEmitCodeLine(const String& toAppend) {
740 SkASSERT(fExtraEmitCodeBlocks.size() > 0);
741 String& currentBlock = fExtraEmitCodeBlocks[fExtraEmitCodeBlocks.size() - 1];
742 // Automatically add indentation and newline
743 currentBlock += " " + toAppend + "\n";
744}
745
746void CPPCodeGenerator::flushEmittedCode() {
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400747 if (fCPPBuffer == nullptr) {
748 // Not actually within writeEmitCode() so nothing to flush
749 return;
750 }
751
752 StringStream* skslBuffer = static_cast<StringStream*>(fOut);
753
754 String sksl = skslBuffer->str();
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400755 // Empty the accumulation buffer since its current contents are consumed.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400756 skslBuffer->reset();
757
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400758 // Switch to the cpp buffer
Michael Ludwigd0440192018-09-07 14:24:52 +0000759 fOut = fCPPBuffer;
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400760
761 // Iterate through the sksl, keeping track of where the last statement ended (e.g. the latest
762 // encountered ';', '{', or '}'). If an extra emit code block token is encountered then the
763 // code from 0 to last statement end is sent to writeCodeAppend, the extra code block is
764 // appended to the cpp buffer, and then the sksl string is trimmed to start where the last
765 // statement left off (minus the encountered token).
766 size_t i = 0;
767 int flushPoint = -1;
768 int tokenStart = -1;
769 while (i < sksl.size()) {
770 if (tokenStart >= 0) {
771 // Looking for the end of the token
772 if (sksl[i] == '}') {
773 // Must append the sksl from 0 to flushPoint (inclusive) then the extra code
774 // accumulated in the block with index parsed from chars [tokenStart+2, i-1]
775 String toFlush = String(sksl.c_str(), flushPoint + 1);
776 // writeCodeAppend automatically removes the format args that it consumed, so
777 // fFormatArgs will be in a valid state for any future sksl
778 this->writeCodeAppend(toFlush);
779
780 int codeBlock = stoi(String(sksl.c_str() + tokenStart + 2, i - tokenStart - 2));
781 SkASSERT(codeBlock < (int) fExtraEmitCodeBlocks.size());
782 if (fExtraEmitCodeBlocks[codeBlock].size() > 0) {
783 this->write(fExtraEmitCodeBlocks[codeBlock].c_str());
784 }
785
786 // Now reset the sksl buffer to start after the flush point, but remove the token.
787 String compacted = String(sksl.c_str() + flushPoint + 1,
788 tokenStart - flushPoint - 1);
789 if (i < sksl.size() - 1) {
790 compacted += String(sksl.c_str() + i + 1, sksl.size() - i - 1);
791 }
792 sksl = compacted;
793
794 // And reset iteration
795 i = -1;
796 flushPoint = -1;
797 tokenStart = -1;
798 }
799 } else {
800 // Looking for the start of extra emit block tokens, and tracking when statements end
801 if (sksl[i] == ';' || sksl[i] == '{' || sksl[i] == '}') {
802 flushPoint = i;
803 } else if (i < sksl.size() - 1 && sksl[i] == '$' && sksl[i + 1] == '{') {
804 // found an extra emit code block token
805 tokenStart = i++;
806 }
807 }
808 i++;
Michael Ludwigd0440192018-09-07 14:24:52 +0000809 }
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400810
811 // Once we've gone through the sksl string to this point, there are no remaining extra emit
812 // code blocks to interleave, so append the remainder as usual.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400813 this->writeCodeAppend(sksl);
814
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400815 // After appending, switch back to the emptied sksl buffer and reset the extra code blocks
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400816 fOut = skslBuffer;
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400817 fExtraEmitCodeBlocks.clear();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400818}
819
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400820void CPPCodeGenerator::writeCodeAppend(const String& code) {
John Stiles50819422020-06-18 13:00:38 -0400821 if (!code.empty()) {
822 // Count % format specifiers.
823 size_t argCount = 0;
824 for (size_t index = 0; index < code.size(); ++index) {
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400825 if ('%' == code[index]) {
John Stiles50819422020-06-18 13:00:38 -0400826 if (index == code.size() - 1) {
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400827 break;
828 }
829 if (code[index + 1] != '%') {
830 ++argCount;
831 }
832 }
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400833 }
John Stiles50819422020-06-18 13:00:38 -0400834
835 // Emit the code string.
836 this->writef(" fragBuilder->codeAppendf(\n"
837 "R\"SkSL(%s)SkSL\"\n", code.c_str());
838 for (size_t i = 0; i < argCount; ++i) {
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400839 this->writef(", %s", fFormatArgs[i].c_str());
840 }
841 this->write(");\n");
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400842
John Stiles50819422020-06-18 13:00:38 -0400843 // argCount is equal to the number of fFormatArgs that were consumed, so they should be
844 // removed from the list.
845 if (argCount > 0) {
846 fFormatArgs.erase(fFormatArgs.begin(), fFormatArgs.begin() + argCount);
847 }
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400848 }
849}
850
851String CPPCodeGenerator::convertSKSLExpressionToCPP(const Expression& e,
852 const String& cppVar) {
853 // To do this conversion, we temporarily switch the sksl output stream
854 // to an empty stringstream and reset the format args to empty.
855 OutputStream* oldSKSL = fOut;
856 StringStream exprBuffer;
857 fOut = &exprBuffer;
858
859 std::vector<String> oldArgs(fFormatArgs);
860 fFormatArgs.clear();
861
862 // Convert the argument expression into a format string and args
863 this->writeExpression(e, Precedence::kTopLevel_Precedence);
864 std::vector<String> newArgs(fFormatArgs);
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400865 String expr = exprBuffer.str();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400866
867 // After generating, restore the original output stream and format args
868 fFormatArgs = oldArgs;
869 fOut = oldSKSL;
870
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400871 // The sksl written to exprBuffer is not processed by flushEmittedCode(), so any extra emit code
872 // block tokens won't get handled. So we need to strip them from the expression and stick them
873 // to the end of the original sksl stream.
874 String exprFormat = "";
875 int tokenStart = -1;
876 for (size_t i = 0; i < expr.size(); i++) {
877 if (tokenStart >= 0) {
878 if (expr[i] == '}') {
879 // End of the token, so append the token to fOut
880 fOut->write(expr.c_str() + tokenStart, i - tokenStart + 1);
881 tokenStart = -1;
882 }
883 } else {
884 if (i < expr.size() - 1 && expr[i] == '$' && expr[i + 1] == '{') {
885 tokenStart = i++;
886 } else {
887 exprFormat += expr[i];
888 }
889 }
890 }
891
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400892 // Now build the final C++ code snippet from the format string and args
893 String cppExpr;
John Stiles50819422020-06-18 13:00:38 -0400894 if (newArgs.empty()) {
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400895 // This was a static expression, so we can simplify the input
896 // color declaration in the emitted code to just a static string
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400897 cppExpr = "SkString " + cppVar + "(\"" + exprFormat + "\");";
John Stiles50819422020-06-18 13:00:38 -0400898 } else if (newArgs.size() == 1 && exprFormat == "%s") {
899 // If the format expression is simply "%s", we can avoid an expensive call to printf.
900 // This happens fairly often in codegen so it is worth simplifying.
901 cppExpr = "SkString " + cppVar + "(" + newArgs[0] + ");";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400902 } else {
903 // String formatting must occur dynamically, so have the C++ declaration
904 // use SkStringPrintf with the format args that were accumulated
905 // when the expression was written.
906 cppExpr = "SkString " + cppVar + " = SkStringPrintf(\"" + exprFormat + "\"";
907 for (size_t i = 0; i < newArgs.size(); i++) {
908 cppExpr += ", " + newArgs[i];
909 }
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400910 cppExpr += ");";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400911 }
912 return cppExpr;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400913}
914
Ethan Nicholas762466e2017-06-29 10:03:38 -0400915bool CPPCodeGenerator::writeEmitCode(std::vector<const Variable*>& uniforms) {
916 this->write(" void emitCode(EmitArgs& args) override {\n"
917 " GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;\n");
918 this->writef(" const %s& _outer = args.fFp.cast<%s>();\n"
919 " (void) _outer;\n",
920 fFullName.c_str(), fFullName.c_str());
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400921 for (const auto& p : fProgram) {
922 if (ProgramElement::kVar_Kind == p.fKind) {
923 const VarDeclarations& decls = (const VarDeclarations&) p;
924 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000925 VarDeclaration& decl = (VarDeclaration&) *raw;
926 String nameString(decl.fVar->fName);
Ethan Nicholas82399462017-10-16 12:35:44 -0400927 const char* name = nameString.c_str();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000928 if (SectionAndParameterHelper::IsParameter(*decl.fVar) &&
929 is_accessible(*decl.fVar)) {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400930 this->writef(" auto %s = _outer.%s;\n"
Ethan Nicholas82399462017-10-16 12:35:44 -0400931 " (void) %s;\n",
932 name, name, name);
933 }
934 }
935 }
936 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400937 this->writePrivateVarValues();
938 for (const auto u : uniforms) {
939 this->addUniform(*u);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400940 }
John Stiles02b11282020-08-10 15:25:24 -0400941 this->writeSection(kEmitCodeSection);
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400942
943 // Save original buffer as the CPP buffer for flushEmittedCode()
944 fCPPBuffer = fOut;
945 StringStream skslBuffer;
946 fOut = &skslBuffer;
947
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400948 this->newExtraEmitCodeBlock();
Ethan Nicholas762466e2017-06-29 10:03:38 -0400949 bool result = INHERITED::generateCode();
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400950 this->flushEmittedCode();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400951
952 // Then restore the original CPP buffer and close the function
953 fOut = fCPPBuffer;
954 fCPPBuffer = nullptr;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400955 this->write(" }\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400956 return result;
957}
958
959void CPPCodeGenerator::writeSetData(std::vector<const Variable*>& uniforms) {
960 const char* fullName = fFullName.c_str();
John Stiles02b11282020-08-10 15:25:24 -0400961 const Section* section = fSectionAndParameterHelper.getSection(kSetDataSection);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400962 const char* pdman = section ? section->fArgument.c_str() : "pdman";
Ethan Nicholas762466e2017-06-29 10:03:38 -0400963 this->writef(" void onSetData(const GrGLSLProgramDataManager& %s, "
964 "const GrFragmentProcessor& _proc) override {\n",
965 pdman);
966 bool wroteProcessor = false;
John Stiles06f3d082020-06-04 11:07:21 -0400967 for (const Variable* u : uniforms) {
Michael Ludwiga4275592018-08-31 10:52:47 -0400968 if (is_uniform_in(*u)) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400969 if (!wroteProcessor) {
970 this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName, fullName);
971 wroteProcessor = true;
972 this->writef(" {\n");
973 }
Michael Ludwiga4275592018-08-31 10:52:47 -0400974
975 const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *u);
976 SkASSERT(mapper);
977
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700978 String nameString(u->fName);
979 const char* name = nameString.c_str();
Michael Ludwiga4275592018-08-31 10:52:47 -0400980
981 // Switches for setData behavior in the generated code
982 bool conditionalUniform = u->fModifiers.fLayout.fWhen != "";
983 bool isTracked = u->fModifiers.fLayout.fFlags & Layout::kTracked_Flag;
984 bool needsValueDeclaration = isTracked || !mapper->canInlineUniformValue();
985
986 String uniformName = HCodeGenerator::FieldName(name) + "Var";
987
988 String indent = " "; // 8 by default, 12 when nested for conditional uniforms
989 if (conditionalUniform) {
990 // Add a pre-check to make sure the uniform was emitted
991 // before trying to send any data to the GPU
992 this->writef(" if (%s.isValid()) {\n", uniformName.c_str());
993 indent += " ";
994 }
995
996 String valueVar = "";
997 if (needsValueDeclaration) {
998 valueVar.appendf("%sValue", name);
999 // Use AccessType since that will match the return type of _outer's public API.
1000 String valueType = HCodeGenerator::AccessType(fContext, u->fType,
1001 u->fModifiers.fLayout);
Ethan Nicholasbcd51e82019-04-09 10:40:41 -04001002 this->writef("%s%s %s = _outer.%s;\n",
Michael Ludwiga4275592018-08-31 10:52:47 -04001003 indent.c_str(), valueType.c_str(), valueVar.c_str(), name);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001004 } else {
Michael Ludwiga4275592018-08-31 10:52:47 -04001005 // Not tracked and the mapper only needs to use the value once
1006 // so send it a safe expression instead of the variable name
Ethan Nicholasbcd51e82019-04-09 10:40:41 -04001007 valueVar.appendf("(_outer.%s)", name);
Michael Ludwiga4275592018-08-31 10:52:47 -04001008 }
1009
1010 if (isTracked) {
1011 SkASSERT(mapper->supportsTracking());
1012
1013 String prevVar = HCodeGenerator::FieldName(name) + "Prev";
1014 this->writef("%sif (%s) {\n"
1015 "%s %s;\n"
1016 "%s %s;\n"
1017 "%s}\n", indent.c_str(),
1018 mapper->dirtyExpression(valueVar, prevVar).c_str(), indent.c_str(),
1019 mapper->saveState(valueVar, prevVar).c_str(), indent.c_str(),
1020 mapper->setUniform(pdman, uniformName, valueVar).c_str(), indent.c_str());
1021 } else {
1022 this->writef("%s%s;\n", indent.c_str(),
1023 mapper->setUniform(pdman, uniformName, valueVar).c_str());
1024 }
1025
1026 if (conditionalUniform) {
1027 // Close the earlier precheck block
1028 this->writef(" }\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -04001029 }
1030 }
1031 }
1032 if (wroteProcessor) {
1033 this->writef(" }\n");
1034 }
Ethan Nicholas68990be2017-07-13 09:36:52 -04001035 if (section) {
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05001036 int samplerIndex = 0;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001037 for (const auto& p : fProgram) {
1038 if (ProgramElement::kVar_Kind == p.fKind) {
1039 const VarDeclarations& decls = (const VarDeclarations&) p;
John Stiles06f3d082020-06-04 11:07:21 -04001040 for (const std::unique_ptr<Statement>& raw : decls.fVars) {
1041 const VarDeclaration& decl = static_cast<VarDeclaration&>(*raw);
1042 const Variable& variable = *decl.fVar;
1043 String nameString(variable.fName);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001044 const char* name = nameString.c_str();
John Stiles06f3d082020-06-04 11:07:21 -04001045 if (variable.fType.kind() == Type::kSampler_Kind) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +00001046 this->writef(" const GrSurfaceProxyView& %sView = "
1047 "_outer.textureSampler(%d).view();\n",
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05001048 name, samplerIndex);
Robert Phillipsbd99c0c2019-12-12 13:26:58 +00001049 this->writef(" GrTexture& %s = *%sView.proxy()->peekTexture();\n",
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05001050 name, name);
1051 this->writef(" (void) %s;\n", name);
1052 ++samplerIndex;
John Stiles06f3d082020-06-04 11:07:21 -04001053 } else if (needs_uniform_var(variable)) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001054 this->writef(" UniformHandle& %s = %sVar;\n"
1055 " (void) %s;\n",
1056 name, HCodeGenerator::FieldName(name).c_str(), name);
John Stiles06f3d082020-06-04 11:07:21 -04001057 } else if (SectionAndParameterHelper::IsParameter(variable) &&
1058 variable.fType != *fContext.fFragmentProcessor_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001059 if (!wroteProcessor) {
1060 this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName,
1061 fullName);
1062 wroteProcessor = true;
1063 }
John Stiles06f3d082020-06-04 11:07:21 -04001064
1065 if (variable.fType.nonnullable() != *fContext.fFragmentProcessor_Type) {
1066 this->writef(" auto %s = _outer.%s;\n"
1067 " (void) %s;\n",
1068 name, name, name);
1069 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001070 }
1071 }
1072 }
1073 }
John Stiles02b11282020-08-10 15:25:24 -04001074 this->writeSection(kSetDataSection);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001075 }
1076 this->write(" }\n");
1077}
1078
Brian Salomonf7dcd762018-07-30 14:48:15 -04001079void CPPCodeGenerator::writeOnTextureSampler() {
1080 bool foundSampler = false;
1081 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
1082 if (param->fType.kind() == Type::kSampler_Kind) {
1083 if (!foundSampler) {
1084 this->writef(
1085 "const GrFragmentProcessor::TextureSampler& %s::onTextureSampler(int "
1086 "index) const {\n",
1087 fFullName.c_str());
1088 this->writef(" return IthTextureSampler(index, %s",
1089 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
1090 foundSampler = true;
1091 } else {
1092 this->writef(", %s",
1093 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
1094 }
1095 }
1096 }
1097 if (foundSampler) {
1098 this->write(");\n}\n");
1099 }
1100}
1101
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001102void CPPCodeGenerator::writeClone() {
John Stiles02b11282020-08-10 15:25:24 -04001103 if (!this->writeSection(kCloneSection)) {
1104 if (fSectionAndParameterHelper.getSection(kFieldsSection)) {
John Stilesa1df23c2020-08-11 17:19:55 -04001105 fErrors.error(/*offset=*/0, "fragment processors with custom @fields must also have a "
1106 "custom @clone");
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001107 }
1108 this->writef("%s::%s(const %s& src)\n"
Ethan Nicholasabff9562017-10-09 10:54:08 -04001109 ": INHERITED(k%s_ClassID, src.optimizationFlags())", fFullName.c_str(),
1110 fFullName.c_str(), fFullName.c_str(), fFullName.c_str());
John Stiles06f3d082020-06-04 11:07:21 -04001111 for (const Variable* param : fSectionAndParameterHelper.getParameters()) {
Robert Phillipsbce7d862019-02-21 22:53:57 +00001112 String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str());
John Stiles88183902020-06-10 16:40:38 -04001113 if (param->fType.nonnullable() != *fContext.fFragmentProcessor_Type) {
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001114 this->writef("\n, %s(src.%s)",
1115 fieldName.c_str(),
1116 fieldName.c_str());
1117 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001118 }
Ethan Nicholasabff9562017-10-09 10:54:08 -04001119 this->writef(" {\n");
Brian Osman12c5d292020-07-13 16:11:35 -04001120 this->writef(" this->cloneAndRegisterAllChildProcessors(src);\n");
Brian Salomonf7dcd762018-07-30 14:48:15 -04001121 int samplerCount = 0;
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001122 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
1123 if (param->fType.kind() == Type::kSampler_Kind) {
Brian Salomonf7dcd762018-07-30 14:48:15 -04001124 ++samplerCount;
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001125 }
1126 }
Brian Salomonf7dcd762018-07-30 14:48:15 -04001127 if (samplerCount) {
1128 this->writef(" this->setTextureSamplerCnt(%d);", samplerCount);
1129 }
Michael Ludwige88320b2020-06-24 09:04:56 -04001130 if (fAccessSampleCoordsDirectly) {
1131 this->writef(" this->setUsesSampleCoordsDirectly();\n");
1132 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001133 this->write("}\n");
Brian Salomonaff329b2017-08-11 09:40:37 -04001134 this->writef("std::unique_ptr<GrFragmentProcessor> %s::clone() const {\n",
1135 fFullName.c_str());
John Stilesfbd050b2020-08-03 13:21:46 -04001136 this->writef(" return std::make_unique<%s>(*this);\n",
Brian Salomonaff329b2017-08-11 09:40:37 -04001137 fFullName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001138 this->write("}\n");
1139 }
1140}
1141
John Stilesa1df23c2020-08-11 17:19:55 -04001142void CPPCodeGenerator::writeDumpInfo() {
1143 this->writef("#ifdef SK_DEBUG\n"
1144 "SkString %s::dumpInfo() const {\n", fFullName.c_str());
1145
1146 if (!this->writeSection(kDumpInfoSection)) {
1147 if (fSectionAndParameterHelper.getSection(kFieldsSection)) {
1148 fErrors.error(/*offset=*/0, "fragment processors with custom @fields must also have a "
1149 "custom @dumpInfo");
1150 }
1151
1152 this->writef(" return SkStringPrintf(\"%s", fName.c_str());
1153
1154 String formatString;
1155 std::vector<String> argumentList;
1156
1157 for (const Variable* param : fSectionAndParameterHelper.getParameters()) {
1158 // dumpInfo() doesn't need to log child FPs.
1159 if (param->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
1160 continue;
1161 }
1162
1163 // Add this field onto the format string and argument list.
1164 String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str());
1165 String runtimeValue = this->formatRuntimeValue(param->fType, param->fModifiers.fLayout,
1166 param->fName, &argumentList);
1167 formatString.appendf("%s%s=%s",
1168 formatString.empty() ? "" : ", ",
1169 fieldName.c_str(),
1170 runtimeValue.c_str());
1171 }
1172
1173 // Append finished format string.
1174 if (!formatString.empty()) {
1175 this->writef("(%s)", formatString.c_str());
1176 }
1177
1178 // Close-quote, then append each argument.
1179 this->write("\"");
1180 for (const String& argument : argumentList) {
1181 this->writef(", %s", argument.c_str());
1182 }
1183
1184 this->write(");");
1185 }
1186
1187 this->write("\n}\n"
1188 "#endif\n");
1189}
1190
Ethan Nicholas762466e2017-06-29 10:03:38 -04001191void CPPCodeGenerator::writeTest() {
John Stiles02b11282020-08-10 15:25:24 -04001192 const Section* test = fSectionAndParameterHelper.getSection(kTestCodeSection);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001193 if (test) {
Brian Salomonaff329b2017-08-11 09:40:37 -04001194 this->writef(
1195 "GR_DEFINE_FRAGMENT_PROCESSOR_TEST(%s);\n"
1196 "#if GR_TEST_UTILS\n"
1197 "std::unique_ptr<GrFragmentProcessor> %s::TestCreate(GrProcessorTestData* %s) {\n",
1198 fFullName.c_str(),
1199 fFullName.c_str(),
1200 test->fArgument.c_str());
John Stiles02b11282020-08-10 15:25:24 -04001201 this->writeSection(kTestCodeSection);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001202 this->write("}\n"
1203 "#endif\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -04001204 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001205}
1206
1207void CPPCodeGenerator::writeGetKey() {
1208 this->writef("void %s::onGetGLSLProcessorKey(const GrShaderCaps& caps, "
1209 "GrProcessorKeyBuilder* b) const {\n",
1210 fFullName.c_str());
Ethan Nicholascab767f2019-07-01 13:32:07 -04001211 for (const auto& p : fProgram) {
1212 if (ProgramElement::kVar_Kind == p.fKind) {
1213 const VarDeclarations& decls = (const VarDeclarations&) p;
1214 for (const auto& raw : decls.fVars) {
1215 const VarDeclaration& decl = (VarDeclaration&) *raw;
1216 const Variable& var = *decl.fVar;
1217 String nameString(var.fName);
1218 const char* name = nameString.c_str();
1219 if (var.fModifiers.fLayout.fKey != Layout::kNo_Key &&
1220 (var.fModifiers.fFlags & Modifiers::kUniform_Flag)) {
1221 fErrors.error(var.fOffset,
1222 "layout(key) may not be specified on uniforms");
Ethan Nicholasbcd51e82019-04-09 10:40:41 -04001223 }
Ethan Nicholascab767f2019-07-01 13:32:07 -04001224 switch (var.fModifiers.fLayout.fKey) {
1225 case Layout::kKey_Key:
1226 if (is_private(var)) {
1227 this->writef("%s %s =",
1228 HCodeGenerator::FieldType(fContext, var.fType,
1229 var.fModifiers.fLayout).c_str(),
1230 String(var.fName).c_str());
1231 if (decl.fValue) {
1232 fCPPMode = true;
1233 this->writeExpression(*decl.fValue, kAssignment_Precedence);
1234 fCPPMode = false;
1235 } else {
1236 this->writef("%s", default_value(var).c_str());
1237 }
1238 this->write(";\n");
1239 }
1240 if (var.fModifiers.fLayout.fWhen.fLength) {
1241 this->writef("if (%s) {", String(var.fModifiers.fLayout.fWhen).c_str());
1242 }
John Stilesb3038f82020-07-27 17:33:25 -04001243 if (var.fType == *fContext.fHalf4_Type) {
Ethan Nicholascab767f2019-07-01 13:32:07 -04001244 this->writef(" uint16_t red = SkFloatToHalf(%s.fR);\n",
1245 HCodeGenerator::FieldName(name).c_str());
1246 this->writef(" uint16_t green = SkFloatToHalf(%s.fG);\n",
1247 HCodeGenerator::FieldName(name).c_str());
1248 this->writef(" uint16_t blue = SkFloatToHalf(%s.fB);\n",
1249 HCodeGenerator::FieldName(name).c_str());
1250 this->writef(" uint16_t alpha = SkFloatToHalf(%s.fA);\n",
1251 HCodeGenerator::FieldName(name).c_str());
1252 this->write(" b->add32(((uint32_t)red << 16) | green);\n");
1253 this->write(" b->add32(((uint32_t)blue << 16) | alpha);\n");
John Stiles45f5b032020-07-27 17:31:29 -04001254 } else if (var.fType == *fContext.fHalf_Type ||
1255 var.fType == *fContext.fFloat_Type) {
1256 this->writef(" b->add32(sk_bit_cast<uint32_t>(%s));\n",
Ethan Nicholascab767f2019-07-01 13:32:07 -04001257 HCodeGenerator::FieldName(name).c_str());
John Stiles45f5b032020-07-27 17:31:29 -04001258 } else if (var.fType.isInteger() || var.fType == *fContext.fBool_Type ||
1259 var.fType.kind() == Type::kEnum_Kind) {
1260 this->writef(" b->add32((uint32_t) %s);\n",
1261 HCodeGenerator::FieldName(name).c_str());
1262 } else {
1263 ABORT("NOT YET IMPLEMENTED: automatic key handling for %s\n",
1264 var.fType.displayName().c_str());
Ethan Nicholascab767f2019-07-01 13:32:07 -04001265 }
1266 if (var.fModifiers.fLayout.fWhen.fLength) {
1267 this->write("}");
1268 }
1269 break;
1270 case Layout::kIdentity_Key:
1271 if (var.fType.kind() != Type::kMatrix_Kind) {
1272 fErrors.error(var.fOffset,
1273 "layout(key=identity) requires matrix type");
1274 }
1275 this->writef(" b->add32(%s.isIdentity() ? 1 : 0);\n",
1276 HCodeGenerator::FieldName(name).c_str());
1277 break;
1278 case Layout::kNo_Key:
1279 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001280 }
Ethan Nicholascab767f2019-07-01 13:32:07 -04001281 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001282 }
1283 }
1284 this->write("}\n");
1285}
1286
1287bool CPPCodeGenerator::generateCode() {
1288 std::vector<const Variable*> uniforms;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001289 for (const auto& p : fProgram) {
1290 if (ProgramElement::kVar_Kind == p.fKind) {
1291 const VarDeclarations& decls = (const VarDeclarations&) p;
1292 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001293 VarDeclaration& decl = (VarDeclaration&) *raw;
1294 if ((decl.fVar->fModifiers.fFlags & Modifiers::kUniform_Flag) &&
1295 decl.fVar->fType.kind() != Type::kSampler_Kind) {
1296 uniforms.push_back(decl.fVar);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001297 }
Michael Ludwiga4275592018-08-31 10:52:47 -04001298
1299 if (is_uniform_in(*decl.fVar)) {
1300 // Validate the "uniform in" declarations to make sure they are fully supported,
1301 // instead of generating surprising C++
1302 const UniformCTypeMapper* mapper =
1303 UniformCTypeMapper::Get(fContext, *decl.fVar);
1304 if (mapper == nullptr) {
1305 fErrors.error(decl.fOffset, String(decl.fVar->fName)
1306 + "'s type is not supported for use as a 'uniform in'");
1307 return false;
1308 }
1309 if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
1310 if (!mapper->supportsTracking()) {
1311 fErrors.error(decl.fOffset, String(decl.fVar->fName)
1312 + "'s type does not support state tracking");
1313 return false;
1314 }
1315 }
1316
1317 } else {
1318 // If it's not a uniform_in, it's an error to be tracked
1319 if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
1320 fErrors.error(decl.fOffset, "Non-'in uniforms' cannot be tracked");
1321 return false;
1322 }
1323 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001324 }
1325 }
1326 }
1327 const char* baseName = fName.c_str();
1328 const char* fullName = fFullName.c_str();
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05001329 this->writef("%s\n", HCodeGenerator::GetHeader(fProgram, fErrors).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001330 this->writef(kFragmentProcessorHeader, fullName);
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001331 this->writef("#include \"%s.h\"\n\n", fullName);
John Stiles02b11282020-08-10 15:25:24 -04001332 this->writeSection(kCppSection);
John Stiles45f5b032020-07-27 17:31:29 -04001333 this->writef("#include \"src/core/SkUtils.h\"\n"
1334 "#include \"src/gpu/GrTexture.h\"\n"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001335 "#include \"src/gpu/glsl/GrGLSLFragmentProcessor.h\"\n"
1336 "#include \"src/gpu/glsl/GrGLSLFragmentShaderBuilder.h\"\n"
1337 "#include \"src/gpu/glsl/GrGLSLProgramBuilder.h\"\n"
1338 "#include \"src/sksl/SkSLCPP.h\"\n"
1339 "#include \"src/sksl/SkSLUtil.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -04001340 "class GrGLSL%s : public GrGLSLFragmentProcessor {\n"
1341 "public:\n"
1342 " GrGLSL%s() {}\n",
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04001343 baseName, baseName);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001344 bool result = this->writeEmitCode(uniforms);
1345 this->write("private:\n");
1346 this->writeSetData(uniforms);
1347 this->writePrivateVars();
1348 for (const auto& u : uniforms) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001349 if (needs_uniform_var(*u) && !(u->fModifiers.fFlags & Modifiers::kIn_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001350 this->writef(" UniformHandle %sVar;\n",
1351 HCodeGenerator::FieldName(String(u->fName).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001352 }
1353 }
Ethan Nicholas68990be2017-07-13 09:36:52 -04001354 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001355 if (needs_uniform_var(*param)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001356 this->writef(" UniformHandle %sVar;\n",
1357 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001358 }
1359 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001360 this->writef("};\n"
1361 "GrGLSLFragmentProcessor* %s::onCreateGLSLInstance() const {\n"
1362 " return new GrGLSL%s();\n"
1363 "}\n",
1364 fullName, baseName);
1365 this->writeGetKey();
1366 this->writef("bool %s::onIsEqual(const GrFragmentProcessor& other) const {\n"
1367 " const %s& that = other.cast<%s>();\n"
1368 " (void) that;\n",
1369 fullName, fullName, fullName);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001370 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001371 if (param->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -04001372 continue;
1373 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001374 String nameString(param->fName);
1375 const char* name = nameString.c_str();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001376 this->writef(" if (%s != that.%s) return false;\n",
1377 HCodeGenerator::FieldName(name).c_str(),
1378 HCodeGenerator::FieldName(name).c_str());
1379 }
1380 this->write(" return true;\n"
1381 "}\n");
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001382 this->writeClone();
John Stilesa1df23c2020-08-11 17:19:55 -04001383 this->writeDumpInfo();
Brian Salomonf7dcd762018-07-30 14:48:15 -04001384 this->writeOnTextureSampler();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001385 this->writeTest();
John Stiles02b11282020-08-10 15:25:24 -04001386 this->writeSection(kCppEndSection);
Greg Daniel3e8c3452018-04-06 10:37:55 -04001387
Ethan Nicholas762466e2017-06-29 10:03:38 -04001388 result &= 0 == fErrors.errorCount();
1389 return result;
1390}
1391
John Stilesa6841be2020-08-06 14:11:56 -04001392} // namespace SkSL