blob: 4a0cad963306349fa4b61cd59024d14aeca57d12 [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");
96 const char* op;
97 switch (b.fOperator) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -040098 case Token::Kind::TK_EQEQ:
Ethan Nicholasee1c8a72019-02-22 10:50:47 -050099 op = "<";
100 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400101 case Token::Kind::TK_NEQ:
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500102 op = ">=";
103 break;
104 default:
105 SkASSERT(false);
106 }
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400107 fFormatArgs.push_back("_outer." + String(var->fName) + "_index " + op + " 0 ? \"true\" "
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500108 ": \"false\"");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400109 } else {
110 INHERITED::writeBinaryExpression(b, parentPrecedence);
111 }
112}
113
114void CPPCodeGenerator::writeIndexExpression(const IndexExpression& i) {
115 const Expression& base = *i.fBase;
116 if (base.fKind == Expression::kVariableReference_Kind) {
117 int builtin = ((VariableReference&) base).fVariable.fModifiers.fLayout.fBuiltin;
Michael Ludwigfc2fdf02020-06-29 17:20:13 -0400118 if (SK_TEXTURESAMPLERS_BUILTIN == builtin) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400119 this->write("%s");
120 if (i.fIndex->fKind != Expression::kIntLiteral_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700121 fErrors.error(i.fIndex->fOffset,
Ethan Nicholas762466e2017-06-29 10:03:38 -0400122 "index into sk_TextureSamplers must be an integer literal");
123 return;
124 }
125 int64_t index = ((IntLiteral&) *i.fIndex).fValue;
126 fFormatArgs.push_back(" fragBuilder->getProgramBuilder()->samplerVariable("
Stephen Whited523a062019-06-19 13:12:46 -0400127 "args.fTexSamplers[" + to_string(index) + "])");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400128 return;
129 }
130 }
131 INHERITED::writeIndexExpression(i);
132}
133
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400134static String default_value(const Type& type) {
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500135 if (type.fName == "bool") {
136 return "false";
137 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400138 switch (type.kind()) {
139 case Type::kScalar_Kind: return "0";
140 case Type::kVector_Kind: return type.name() + "(0)";
141 case Type::kMatrix_Kind: return type.name() + "(1)";
142 default: ABORT("unsupported default_value type\n");
143 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400144}
145
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500146static String default_value(const Variable& var) {
Brian Osman495993a2018-10-16 15:45:55 -0400147 if (var.fModifiers.fLayout.fCType == SkSL::Layout::CType::kSkPMColor4f) {
Brian Osmanf28e55d2018-10-03 16:35:54 -0400148 return "{SK_FloatNaN, SK_FloatNaN, SK_FloatNaN, SK_FloatNaN}";
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500149 }
150 return default_value(var.fType);
151}
152
Ethan Nicholas762466e2017-06-29 10:03:38 -0400153static bool is_private(const Variable& var) {
154 return !(var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
155 !(var.fModifiers.fFlags & Modifiers::kIn_Flag) &&
156 var.fStorage == Variable::kGlobal_Storage &&
157 var.fModifiers.fLayout.fBuiltin == -1;
158}
159
Michael Ludwiga4275592018-08-31 10:52:47 -0400160static bool is_uniform_in(const Variable& var) {
161 return (var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
162 (var.fModifiers.fFlags & Modifiers::kIn_Flag) &&
163 var.fType.kind() != Type::kSampler_Kind;
164}
165
Ethan Nicholasd608c092017-10-26 09:30:08 -0400166void CPPCodeGenerator::writeRuntimeValue(const Type& type, const Layout& layout,
167 const String& cppCode) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400168 if (type.isFloat()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400169 this->write("%f");
170 fFormatArgs.push_back(cppCode);
171 } else if (type == *fContext.fInt_Type) {
172 this->write("%d");
173 fFormatArgs.push_back(cppCode);
174 } else if (type == *fContext.fBool_Type) {
175 this->write("%s");
176 fFormatArgs.push_back("(" + cppCode + " ? \"true\" : \"false\")");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400177 } else if (type == *fContext.fFloat2_Type || type == *fContext.fHalf2_Type) {
178 this->write(type.name() + "(%f, %f)");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400179 fFormatArgs.push_back(cppCode + ".fX");
180 fFormatArgs.push_back(cppCode + ".fY");
Ethan Nicholas82399462017-10-16 12:35:44 -0400181 } else if (type == *fContext.fFloat4_Type || type == *fContext.fHalf4_Type) {
182 this->write(type.name() + "(%f, %f, %f, %f)");
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400183 switch (layout.fCType) {
184 case Layout::CType::kSkPMColor:
185 fFormatArgs.push_back("SkGetPackedR32(" + cppCode + ") / 255.0");
186 fFormatArgs.push_back("SkGetPackedG32(" + cppCode + ") / 255.0");
187 fFormatArgs.push_back("SkGetPackedB32(" + cppCode + ") / 255.0");
188 fFormatArgs.push_back("SkGetPackedA32(" + cppCode + ") / 255.0");
189 break;
Brian Osmanf28e55d2018-10-03 16:35:54 -0400190 case Layout::CType::kSkPMColor4f:
191 fFormatArgs.push_back(cppCode + ".fR");
192 fFormatArgs.push_back(cppCode + ".fG");
193 fFormatArgs.push_back(cppCode + ".fB");
194 fFormatArgs.push_back(cppCode + ".fA");
195 break;
Mike Reedb26b4e72020-01-22 14:31:21 -0500196 case Layout::CType::kSkV4:
197 fFormatArgs.push_back(cppCode + ".x");
198 fFormatArgs.push_back(cppCode + ".y");
199 fFormatArgs.push_back(cppCode + ".z");
200 fFormatArgs.push_back(cppCode + ".w");
Brian Salomoneca66b32019-06-01 11:18:15 -0400201 break;
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400202 case Layout::CType::kSkRect: // fall through
203 case Layout::CType::kDefault:
204 fFormatArgs.push_back(cppCode + ".left()");
205 fFormatArgs.push_back(cppCode + ".top()");
206 fFormatArgs.push_back(cppCode + ".right()");
207 fFormatArgs.push_back(cppCode + ".bottom()");
208 break;
209 default:
210 SkASSERT(false);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400211 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500212 } else if (type.kind() == Type::kEnum_Kind) {
213 this->write("%d");
214 fFormatArgs.push_back("(int) " + cppCode);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400215 } else if (type == *fContext.fInt4_Type ||
216 type == *fContext.fShort4_Type ||
217 type == *fContext.fByte4_Type) {
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500218 this->write(type.name() + "(%d, %d, %d, %d)");
219 fFormatArgs.push_back(cppCode + ".left()");
220 fFormatArgs.push_back(cppCode + ".top()");
221 fFormatArgs.push_back(cppCode + ".right()");
222 fFormatArgs.push_back(cppCode + ".bottom()");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400223 } else {
Ethan Nicholas82399462017-10-16 12:35:44 -0400224 printf("unsupported runtime value type '%s'\n", String(type.fName).c_str());
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400225 SkASSERT(false);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400226 }
227}
228
229void CPPCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) {
230 if (is_private(var)) {
Ethan Nicholasd608c092017-10-26 09:30:08 -0400231 this->writeRuntimeValue(var.fType, var.fModifiers.fLayout, var.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400232 } else {
233 this->writeExpression(value, kTopLevel_Precedence);
234 }
235}
236
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400237String CPPCodeGenerator::getSamplerHandle(const Variable& var) {
238 int samplerCount = 0;
Ethan Nicholas68990be2017-07-13 09:36:52 -0400239 for (const auto param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400240 if (&var == param) {
241 return "args.fTexSamplers[" + to_string(samplerCount) + "]";
242 }
243 if (param->fType.kind() == Type::kSampler_Kind) {
244 ++samplerCount;
245 }
246 }
247 ABORT("should have found sampler in parameters\n");
248}
249
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400250void CPPCodeGenerator::writeIntLiteral(const IntLiteral& i) {
251 this->write(to_string((int32_t) i.fValue));
252}
253
Ethan Nicholas82399462017-10-16 12:35:44 -0400254void CPPCodeGenerator::writeSwizzle(const Swizzle& swizzle) {
255 if (fCPPMode) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400256 SkASSERT(swizzle.fComponents.size() == 1); // no support for multiple swizzle components yet
Ethan Nicholas82399462017-10-16 12:35:44 -0400257 this->writeExpression(*swizzle.fBase, kPostfix_Precedence);
258 switch (swizzle.fComponents[0]) {
259 case 0: this->write(".left()"); break;
260 case 1: this->write(".top()"); break;
261 case 2: this->write(".right()"); break;
262 case 3: this->write(".bottom()"); break;
263 }
264 } else {
265 INHERITED::writeSwizzle(swizzle);
266 }
267}
268
Ethan Nicholas762466e2017-06-29 10:03:38 -0400269void CPPCodeGenerator::writeVariableReference(const VariableReference& ref) {
Ethan Nicholas82399462017-10-16 12:35:44 -0400270 if (fCPPMode) {
271 this->write(ref.fVariable.fName);
272 return;
273 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400274 switch (ref.fVariable.fModifiers.fLayout.fBuiltin) {
275 case SK_INCOLOR_BUILTIN:
276 this->write("%s");
Michael Ludwig231de032018-08-30 14:33:01 -0400277 // EmitArgs.fInputColor is automatically set to half4(1) if
278 // no input was specified
279 fFormatArgs.push_back(String("args.fInputColor"));
Ethan Nicholas762466e2017-06-29 10:03:38 -0400280 break;
281 case SK_OUTCOLOR_BUILTIN:
282 this->write("%s");
283 fFormatArgs.push_back(String("args.fOutputColor"));
284 break;
Michael Ludwigfc2fdf02020-06-29 17:20:13 -0400285 case SK_MAIN_COORDS_BUILTIN:
286 this->write("%s");
287 fFormatArgs.push_back(String("args.fSampleCoord"));
288 fAccessSampleCoordsDirectly = true;
289 break;
Ethan Nicholascd700e92018-08-24 16:43:57 -0400290 case SK_WIDTH_BUILTIN:
291 this->write("sk_Width");
292 break;
293 case SK_HEIGHT_BUILTIN:
294 this->write("sk_Height");
295 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400296 default:
297 if (ref.fVariable.fType.kind() == Type::kSampler_Kind) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400298 this->write("%s");
299 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerVariable(" +
Stephen Whited523a062019-06-19 13:12:46 -0400300 this->getSamplerHandle(ref.fVariable) + ")");
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400301 return;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400302 }
303 if (ref.fVariable.fModifiers.fFlags & Modifiers::kUniform_Flag) {
304 this->write("%s");
305 String name = ref.fVariable.fName;
Brian Osman1cb41712017-10-19 12:54:52 -0400306 String var = String::printf("args.fUniformHandler->getUniformCStr(%sVar)",
307 HCodeGenerator::FieldName(name.c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400308 String code;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400309 if (ref.fVariable.fModifiers.fLayout.fWhen.fLength) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400310 code = String::printf("%sVar.isValid() ? %s : \"%s\"",
311 HCodeGenerator::FieldName(name.c_str()).c_str(),
312 var.c_str(),
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400313 default_value(ref.fVariable.fType).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400314 } else {
315 code = var;
316 }
317 fFormatArgs.push_back(code);
318 } else if (SectionAndParameterHelper::IsParameter(ref.fVariable)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700319 String name(ref.fVariable.fName);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400320 this->writeRuntimeValue(ref.fVariable.fType, ref.fVariable.fModifiers.fLayout,
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400321 String::printf("_outer.%s", name.c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400322 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700323 this->write(ref.fVariable.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400324 }
325 }
326}
327
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400328void CPPCodeGenerator::writeIfStatement(const IfStatement& s) {
329 if (s.fIsStatic) {
330 this->write("@");
331 }
332 INHERITED::writeIfStatement(s);
333}
334
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400335void CPPCodeGenerator::writeReturnStatement(const ReturnStatement& s) {
336 if (fInMain) {
337 fErrors.error(s.fOffset, "fragmentProcessor main() may not contain return statements");
338 }
339 INHERITED::writeReturnStatement(s);
340}
341
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400342void CPPCodeGenerator::writeSwitchStatement(const SwitchStatement& s) {
343 if (s.fIsStatic) {
344 this->write("@");
345 }
346 INHERITED::writeSwitchStatement(s);
347}
348
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400349void CPPCodeGenerator::writeFieldAccess(const FieldAccess& access) {
350 if (access.fBase->fType.name() == "fragmentProcessor") {
351 // Special field access on fragment processors are converted into function calls on
352 // GrFragmentProcessor's getters.
353 if (access.fBase->fKind != Expression::kVariableReference_Kind) {
354 fErrors.error(access.fBase->fOffset, "fragmentProcessor must be a reference\n");
355 return;
356 }
357
358 const Type::Field& field = fContext.fFragmentProcessor_Type->fields()[access.fFieldIndex];
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500359 const Variable& var = ((const VariableReference&) *access.fBase).fVariable;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400360 String cppAccess = String::printf("_outer.childProcessor(_outer.%s_index).%s()",
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500361 String(var.fName).c_str(),
362 String(field.fName).c_str());
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400363
364 if (fCPPMode) {
365 this->write(cppAccess.c_str());
366 } else {
367 writeRuntimeValue(*field.fType, Layout(), cppAccess);
368 }
369 return;
370 }
371 INHERITED::writeFieldAccess(access);
372}
373
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500374int CPPCodeGenerator::getChildFPIndex(const Variable& var) const {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400375 int index = 0;
376 bool found = false;
377 for (const auto& p : fProgram) {
378 if (ProgramElement::kVar_Kind == p.fKind) {
379 const VarDeclarations& decls = (const VarDeclarations&) p;
380 for (const auto& raw : decls.fVars) {
381 const VarDeclaration& decl = (VarDeclaration&) *raw;
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500382 if (decl.fVar == &var) {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400383 found = true;
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500384 } else if (decl.fVar->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400385 ++index;
386 }
387 }
388 }
389 if (found) {
390 break;
391 }
392 }
393 SkASSERT(found);
394 return index;
395}
396
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400397void CPPCodeGenerator::writeFunctionCall(const FunctionCall& c) {
Ethan Nicholas13863662019-07-29 13:05:15 -0400398 if (c.fFunction.fBuiltin && c.fFunction.fName == "sample" &&
399 c.fArguments[0]->fType.kind() != Type::Kind::kSampler_Kind) {
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400400 // Sanity checks that are detected by function definition in sksl_fp.inc
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400401 SkASSERT(c.fArguments.size() >= 1 && c.fArguments.size() <= 3);
Florin Malita390f9bd2019-03-04 12:25:57 -0500402 SkASSERT("fragmentProcessor" == c.fArguments[0]->fType.name() ||
403 "fragmentProcessor?" == c.fArguments[0]->fType.name());
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400404
405 // Actually fail during compilation if arguments with valid types are
Ethan Nicholas13863662019-07-29 13:05:15 -0400406 // provided that are not variable references, since sample() is a
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400407 // special function that impacts code emission.
408 if (c.fArguments[0]->fKind != Expression::kVariableReference_Kind) {
409 fErrors.error(c.fArguments[0]->fOffset,
Ethan Nicholas13863662019-07-29 13:05:15 -0400410 "sample()'s fragmentProcessor argument must be a variable reference\n");
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400411 return;
412 }
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500413 const Variable& child = ((const VariableReference&) *c.fArguments[0]).fVariable;
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400414
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400415 // Start a new extra emit code section so that the emitted child processor can depend on
416 // sksl variables defined in earlier sksl code.
417 this->newExtraEmitCodeBlock();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400418
Michael Ludwige88320b2020-06-24 09:04:56 -0400419 String inputColorName; // the sksl variable/expression, referenced later for null child FPs
420 String inputColor;
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400421 if (c.fArguments.size() > 1 && c.fArguments[1]->fType.name() == "half4") {
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400422 // Use the invokeChild() variant that accepts an input color, so convert the 2nd
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400423 // argument's expression into C++ code that produces sksl stored in an SkString.
John Stilesd060c9d2020-06-08 11:44:25 -0400424 inputColorName = "_input" + to_string(c.fOffset);
425 addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments[1], inputColorName));
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400426
Michael Ludwige88320b2020-06-24 09:04:56 -0400427 // invokeChild() needs a char* and a pre-pended comma
428 inputColor = ", " + inputColorName + ".c_str()";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400429 }
430
Michael Ludwige88320b2020-06-24 09:04:56 -0400431 String inputCoord;
432 String invokeFunction = "invokeChild";
433 if (c.fArguments.back()->fType.name() == "float2") {
434 // Invoking child with explicit coordinates at this call site
435 inputCoord = "_coords" + to_string(c.fOffset);
436 addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments.back(), inputCoord));
437 inputCoord.append(".c_str()");
438 } else if (c.fArguments.back()->fType.name() == "float3x3") {
439 // Invoking child with a matrix, sampling relative to the input coords.
440 invokeFunction = "invokeChildWithMatrix";
Brian Osman1298bc42020-06-30 13:39:35 -0400441 SampleUsage usage = Analysis::GetSampleUsage(fProgram, child);
Michael Ludwige88320b2020-06-24 09:04:56 -0400442
Brian Osman1298bc42020-06-30 13:39:35 -0400443 if (!usage.hasUniformMatrix()) {
Michael Ludwige88320b2020-06-24 09:04:56 -0400444 inputCoord = "_matrix" + to_string(c.fOffset);
445 addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments.back(), inputCoord));
446 inputCoord.append(".c_str()");
447 }
448 // else pass in the empty string to rely on invokeChildWithMatrix's automatic uniform
449 // resolution
450 }
451 if (!inputCoord.empty()) {
452 inputCoord = ", " + inputCoord;
453 }
454
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400455 // Write the output handling after the possible input handling
Ethan Nicholas13863662019-07-29 13:05:15 -0400456 String childName = "_sample" + to_string(c.fOffset);
Brian Osman978693c2020-01-24 14:52:10 -0500457 addExtraEmitCodeLine("SkString " + childName + ";");
Michael Ludwige88320b2020-06-24 09:04:56 -0400458
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500459 if (c.fArguments[0]->fType.kind() == Type::kNullable_Kind) {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400460 addExtraEmitCodeLine("if (_outer." + String(child.fName) + "_index >= 0) {\n ");
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500461 }
Michael Ludwige88320b2020-06-24 09:04:56 -0400462 addExtraEmitCodeLine(childName + " = this->" + invokeFunction + "(_outer." +
463 String(child.fName) + "_index" + inputColor + ", args" +
464 inputCoord + ");");
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400465
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500466 if (c.fArguments[0]->fType.kind() == Type::kNullable_Kind) {
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000467 // Null FPs are not emitted, but their output can still be referenced in dependent
Brian Osman978693c2020-01-24 14:52:10 -0500468 // expressions - thus we always fill the variable with something.
John Stilesd060c9d2020-06-08 11:44:25 -0400469 // Sampling from a null fragment processor will provide in the input color as-is. This
470 // defaults to half4(1) if no color is specified.
John Stiles50819422020-06-18 13:00:38 -0400471 if (!inputColorName.empty()) {
472 addExtraEmitCodeLine(
473 "} else {"
474 " " + childName + ".swap(" + inputColorName + ");"
475 "}");
476 } else {
477 addExtraEmitCodeLine(
478 "} else {"
479 " " + childName + " = \"half4(1)\";"
480 "}");
481 }
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500482 }
John Stiles50819422020-06-18 13:00:38 -0400483
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000484 this->write("%s");
485 fFormatArgs.push_back(childName + ".c_str()");
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400486 return;
487 }
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400488 if (c.fFunction.fBuiltin) {
489 INHERITED::writeFunctionCall(c);
490 } else {
491 this->write("%s");
492 fFormatArgs.push_back((String(c.fFunction.fName) + "_name.c_str()").c_str());
493 this->write("(");
494 const char* separator = "";
495 for (const auto& arg : c.fArguments) {
496 this->write(separator);
497 separator = ", ";
498 this->writeExpression(*arg, kSequence_Precedence);
499 }
500 this->write(")");
501 }
Ethan Nicholas13863662019-07-29 13:05:15 -0400502 if (c.fFunction.fBuiltin && c.fFunction.fName == "sample") {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400503 this->write(".%s");
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400504 SkASSERT(c.fArguments.size() >= 1);
505 SkASSERT(c.fArguments[0]->fKind == Expression::kVariableReference_Kind);
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400506 String sampler = this->getSamplerHandle(((VariableReference&) *c.fArguments[0]).fVariable);
507 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerSwizzle(" + sampler +
Greg Daniel369ee6b2019-12-02 15:30:02 -0500508 ").asString().c_str()");
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400509 }
510}
511
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400512static const char* glsltype_string(const Context& context, const Type& type) {
513 if (type == *context.fFloat_Type) {
514 return "kFloat_GrSLType";
515 } else if (type == *context.fHalf_Type) {
516 return "kHalf_GrSLType";
517 } else if (type == *context.fFloat2_Type) {
518 return "kFloat2_GrSLType";
519 } else if (type == *context.fHalf2_Type) {
520 return "kHalf2_GrSLType";
Ethan Nicholas8ae1b562019-12-17 15:18:02 -0500521 } else if (type == *context.fFloat3_Type) {
522 return "kFloat3_GrSLType";
523 } else if (type == *context.fHalf3_Type) {
524 return "kHalf3_GrSLType";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400525 } else if (type == *context.fFloat4_Type) {
526 return "kFloat4_GrSLType";
527 } else if (type == *context.fHalf4_Type) {
528 return "kHalf4_GrSLType";
Ethan Nicholas58430122020-04-14 09:54:02 -0400529 } else if (type == *context.fFloat2x2_Type) {
530 return "kFloat2x2_GrSLType";
531 } else if (type == *context.fHalf2x2_Type) {
532 return "kHalf2x2_GrSLType";
533 } else if (type == *context.fFloat3x3_Type) {
534 return "kFloat3x3_GrSLType";
535 } else if (type == *context.fHalf3x3_Type) {
536 return "kHalf3x3_GrSLType";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400537 } else if (type == *context.fFloat4x4_Type) {
538 return "kFloat4x4_GrSLType";
539 } else if (type == *context.fHalf4x4_Type) {
540 return "kHalf4x4_GrSLType";
541 } else if (type == *context.fVoid_Type) {
542 return "kVoid_GrSLType";
Ethan Nicholas8ae1b562019-12-17 15:18:02 -0500543 } else if (type.kind() == Type::kEnum_Kind) {
544 return "int";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400545 }
546 SkASSERT(false);
547 return nullptr;
548}
549
Ethan Nicholas762466e2017-06-29 10:03:38 -0400550void CPPCodeGenerator::writeFunction(const FunctionDefinition& f) {
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400551 const FunctionDeclaration& decl = f.fDeclaration;
Brian Osman08f986d2020-05-13 17:06:46 -0400552 if (decl.fBuiltin) {
553 return;
554 }
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400555 fFunctionHeader = "";
556 OutputStream* oldOut = fOut;
557 StringStream buffer;
558 fOut = &buffer;
559 if (decl.fName == "main") {
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400560 fInMain = true;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400561 for (const auto& s : ((Block&) *f.fBody).fStatements) {
562 this->writeStatement(*s);
563 this->writeLine();
564 }
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400565 fInMain = false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400566
567 fOut = oldOut;
568 this->write(fFunctionHeader);
569 this->write(buffer.str());
570 } else {
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400571 this->addExtraEmitCodeLine("SkString " + decl.fName + "_name;");
572 String args = "const GrShaderVar " + decl.fName + "_args[] = { ";
573 const char* separator = "";
574 for (const auto& param : decl.fParameters) {
575 args += String(separator) + "GrShaderVar(\"" + param->fName + "\", " +
576 glsltype_string(fContext, param->fType) + ")";
577 separator = ", ";
578 }
579 args += "};";
580 this->addExtraEmitCodeLine(args.c_str());
581 for (const auto& s : ((Block&) *f.fBody).fStatements) {
582 this->writeStatement(*s);
583 this->writeLine();
584 }
585
586 fOut = oldOut;
587 String emit = "fragBuilder->emitFunction(";
588 emit += glsltype_string(fContext, decl.fReturnType);
589 emit += ", \"" + decl.fName + "\"";
590 emit += ", " + to_string((int64_t) decl.fParameters.size());
591 emit += ", " + decl.fName + "_args";
John Stiles50819422020-06-18 13:00:38 -0400592 emit += ",\nR\"SkSL(" + buffer.str() + ")SkSL\"";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400593 emit += ", &" + decl.fName + "_name);";
594 this->addExtraEmitCodeLine(emit.c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400595 }
596}
597
598void CPPCodeGenerator::writeSetting(const Setting& s) {
599 static constexpr const char* kPrefix = "sk_Args.";
600 if (!strncmp(s.fName.c_str(), kPrefix, strlen(kPrefix))) {
601 const char* name = s.fName.c_str() + strlen(kPrefix);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400602 this->writeRuntimeValue(s.fType, Layout(), HCodeGenerator::FieldName(name).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400603 } else {
604 this->write(s.fName.c_str());
605 }
606}
607
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400608bool CPPCodeGenerator::writeSection(const char* name, const char* prefix) {
Ethan Nicholas68990be2017-07-13 09:36:52 -0400609 const Section* s = fSectionAndParameterHelper.getSection(name);
610 if (s) {
611 this->writef("%s%s", prefix, s->fText.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400612 return true;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400613 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400614 return false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400615}
616
617void CPPCodeGenerator::writeProgramElement(const ProgramElement& p) {
618 if (p.fKind == ProgramElement::kSection_Kind) {
619 return;
620 }
621 if (p.fKind == ProgramElement::kVar_Kind) {
622 const VarDeclarations& decls = (const VarDeclarations&) p;
623 if (!decls.fVars.size()) {
624 return;
625 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000626 const Variable& var = *((VarDeclaration&) *decls.fVars[0]).fVar;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400627 if (var.fModifiers.fFlags & (Modifiers::kIn_Flag | Modifiers::kUniform_Flag) ||
628 -1 != var.fModifiers.fLayout.fBuiltin) {
629 return;
630 }
631 }
632 INHERITED::writeProgramElement(p);
633}
634
635void CPPCodeGenerator::addUniform(const Variable& var) {
636 if (!needs_uniform_var(var)) {
637 return;
638 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400639 if (var.fModifiers.fLayout.fWhen.fLength) {
640 this->writef(" if (%s) {\n ", String(var.fModifiers.fLayout.fWhen).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400641 }
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400642 const char* type = glsltype_string(fContext, var.fType);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700643 String name(var.fName);
Ethan Nicholas16464c32020-04-06 13:53:05 -0400644 this->writef(" %sVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag,"
645 " %s, \"%s\");\n", HCodeGenerator::FieldName(name.c_str()).c_str(), type,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700646 name.c_str());
Ethan Nicholasfc994162019-06-06 10:04:27 -0400647 if (var.fModifiers.fLayout.fWhen.fLength) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400648 this->write(" }\n");
649 }
650}
651
Ethan Nicholascd700e92018-08-24 16:43:57 -0400652void CPPCodeGenerator::writeInputVars() {
653}
654
Ethan Nicholas762466e2017-06-29 10:03:38 -0400655void CPPCodeGenerator::writePrivateVars() {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400656 for (const auto& p : fProgram) {
657 if (ProgramElement::kVar_Kind == p.fKind) {
658 const VarDeclarations& decls = (const VarDeclarations&) p;
659 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000660 VarDeclaration& decl = (VarDeclaration&) *raw;
661 if (is_private(*decl.fVar)) {
662 if (decl.fVar->fType == *fContext.fFragmentProcessor_Type) {
663 fErrors.error(decl.fOffset,
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400664 "fragmentProcessor variables must be declared 'in'");
665 return;
666 }
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500667 this->writef("%s %s = %s;\n",
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000668 HCodeGenerator::FieldType(fContext, decl.fVar->fType,
669 decl.fVar->fModifiers.fLayout).c_str(),
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500670 String(decl.fVar->fName).c_str(),
671 default_value(*decl.fVar).c_str());
Michael Ludwiga4275592018-08-31 10:52:47 -0400672 } else if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
673 // An auto-tracked uniform in variable, so add a field to hold onto the prior
674 // state. Note that tracked variables must be uniform in's and that is validated
675 // before writePrivateVars() is called.
676 const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *decl.fVar);
677 SkASSERT(mapper && mapper->supportsTracking());
678
679 String name = HCodeGenerator::FieldName(String(decl.fVar->fName).c_str());
680 // The member statement is different if the mapper reports a default value
681 if (mapper->defaultValue().size() > 0) {
682 this->writef("%s %sPrev = %s;\n",
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400683 Layout::CTypeToStr(mapper->ctype()), name.c_str(),
Michael Ludwiga4275592018-08-31 10:52:47 -0400684 mapper->defaultValue().c_str());
685 } else {
686 this->writef("%s %sPrev;\n",
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400687 Layout::CTypeToStr(mapper->ctype()), name.c_str());
Michael Ludwiga4275592018-08-31 10:52:47 -0400688 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400689 }
690 }
691 }
692 }
693}
694
695void CPPCodeGenerator::writePrivateVarValues() {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400696 for (const auto& p : fProgram) {
697 if (ProgramElement::kVar_Kind == p.fKind) {
698 const VarDeclarations& decls = (const VarDeclarations&) p;
699 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000700 VarDeclaration& decl = (VarDeclaration&) *raw;
701 if (is_private(*decl.fVar) && decl.fValue) {
702 this->writef("%s = ", String(decl.fVar->fName).c_str());
Ethan Nicholas82399462017-10-16 12:35:44 -0400703 fCPPMode = true;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000704 this->writeExpression(*decl.fValue, kAssignment_Precedence);
Ethan Nicholas82399462017-10-16 12:35:44 -0400705 fCPPMode = false;
706 this->write(";\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400707 }
708 }
709 }
710 }
711}
712
Ethan Nicholas82399462017-10-16 12:35:44 -0400713static bool is_accessible(const Variable& var) {
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500714 const Type& type = var.fType.nonnullable();
715 return Type::kSampler_Kind != type.kind() &&
716 Type::kOther_Kind != type.kind();
Ethan Nicholas82399462017-10-16 12:35:44 -0400717}
718
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400719void CPPCodeGenerator::newExtraEmitCodeBlock() {
720 // This should only be called when emitting SKSL for emitCode(), which can be detected if the
721 // cpp buffer is not null, and the cpp buffer is not the current output.
722 SkASSERT(fCPPBuffer && fCPPBuffer != fOut);
723
724 // Start a new block as an empty string
725 fExtraEmitCodeBlocks.push_back("");
726 // Mark its location in the output buffer, uses ${\d} for the token since ${} will not occur in
727 // valid sksl and makes detection trivial.
728 this->writef("${%zu}", fExtraEmitCodeBlocks.size() - 1);
729}
730
731void CPPCodeGenerator::addExtraEmitCodeLine(const String& toAppend) {
732 SkASSERT(fExtraEmitCodeBlocks.size() > 0);
733 String& currentBlock = fExtraEmitCodeBlocks[fExtraEmitCodeBlocks.size() - 1];
734 // Automatically add indentation and newline
735 currentBlock += " " + toAppend + "\n";
736}
737
738void CPPCodeGenerator::flushEmittedCode() {
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400739 if (fCPPBuffer == nullptr) {
740 // Not actually within writeEmitCode() so nothing to flush
741 return;
742 }
743
744 StringStream* skslBuffer = static_cast<StringStream*>(fOut);
745
746 String sksl = skslBuffer->str();
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400747 // Empty the accumulation buffer since its current contents are consumed.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400748 skslBuffer->reset();
749
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400750 // Switch to the cpp buffer
Michael Ludwigd0440192018-09-07 14:24:52 +0000751 fOut = fCPPBuffer;
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400752
753 // Iterate through the sksl, keeping track of where the last statement ended (e.g. the latest
754 // encountered ';', '{', or '}'). If an extra emit code block token is encountered then the
755 // code from 0 to last statement end is sent to writeCodeAppend, the extra code block is
756 // appended to the cpp buffer, and then the sksl string is trimmed to start where the last
757 // statement left off (minus the encountered token).
758 size_t i = 0;
759 int flushPoint = -1;
760 int tokenStart = -1;
761 while (i < sksl.size()) {
762 if (tokenStart >= 0) {
763 // Looking for the end of the token
764 if (sksl[i] == '}') {
765 // Must append the sksl from 0 to flushPoint (inclusive) then the extra code
766 // accumulated in the block with index parsed from chars [tokenStart+2, i-1]
767 String toFlush = String(sksl.c_str(), flushPoint + 1);
768 // writeCodeAppend automatically removes the format args that it consumed, so
769 // fFormatArgs will be in a valid state for any future sksl
770 this->writeCodeAppend(toFlush);
771
772 int codeBlock = stoi(String(sksl.c_str() + tokenStart + 2, i - tokenStart - 2));
773 SkASSERT(codeBlock < (int) fExtraEmitCodeBlocks.size());
774 if (fExtraEmitCodeBlocks[codeBlock].size() > 0) {
775 this->write(fExtraEmitCodeBlocks[codeBlock].c_str());
776 }
777
778 // Now reset the sksl buffer to start after the flush point, but remove the token.
779 String compacted = String(sksl.c_str() + flushPoint + 1,
780 tokenStart - flushPoint - 1);
781 if (i < sksl.size() - 1) {
782 compacted += String(sksl.c_str() + i + 1, sksl.size() - i - 1);
783 }
784 sksl = compacted;
785
786 // And reset iteration
787 i = -1;
788 flushPoint = -1;
789 tokenStart = -1;
790 }
791 } else {
792 // Looking for the start of extra emit block tokens, and tracking when statements end
793 if (sksl[i] == ';' || sksl[i] == '{' || sksl[i] == '}') {
794 flushPoint = i;
795 } else if (i < sksl.size() - 1 && sksl[i] == '$' && sksl[i + 1] == '{') {
796 // found an extra emit code block token
797 tokenStart = i++;
798 }
799 }
800 i++;
Michael Ludwigd0440192018-09-07 14:24:52 +0000801 }
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400802
803 // Once we've gone through the sksl string to this point, there are no remaining extra emit
804 // code blocks to interleave, so append the remainder as usual.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400805 this->writeCodeAppend(sksl);
806
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400807 // After appending, switch back to the emptied sksl buffer and reset the extra code blocks
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400808 fOut = skslBuffer;
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400809 fExtraEmitCodeBlocks.clear();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400810}
811
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400812void CPPCodeGenerator::writeCodeAppend(const String& code) {
John Stiles50819422020-06-18 13:00:38 -0400813 if (!code.empty()) {
814 // Count % format specifiers.
815 size_t argCount = 0;
816 for (size_t index = 0; index < code.size(); ++index) {
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400817 if ('%' == code[index]) {
John Stiles50819422020-06-18 13:00:38 -0400818 if (index == code.size() - 1) {
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400819 break;
820 }
821 if (code[index + 1] != '%') {
822 ++argCount;
823 }
824 }
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400825 }
John Stiles50819422020-06-18 13:00:38 -0400826
827 // Emit the code string.
828 this->writef(" fragBuilder->codeAppendf(\n"
829 "R\"SkSL(%s)SkSL\"\n", code.c_str());
830 for (size_t i = 0; i < argCount; ++i) {
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400831 this->writef(", %s", fFormatArgs[i].c_str());
832 }
833 this->write(");\n");
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400834
John Stiles50819422020-06-18 13:00:38 -0400835 // argCount is equal to the number of fFormatArgs that were consumed, so they should be
836 // removed from the list.
837 if (argCount > 0) {
838 fFormatArgs.erase(fFormatArgs.begin(), fFormatArgs.begin() + argCount);
839 }
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400840 }
841}
842
843String CPPCodeGenerator::convertSKSLExpressionToCPP(const Expression& e,
844 const String& cppVar) {
845 // To do this conversion, we temporarily switch the sksl output stream
846 // to an empty stringstream and reset the format args to empty.
847 OutputStream* oldSKSL = fOut;
848 StringStream exprBuffer;
849 fOut = &exprBuffer;
850
851 std::vector<String> oldArgs(fFormatArgs);
852 fFormatArgs.clear();
853
854 // Convert the argument expression into a format string and args
855 this->writeExpression(e, Precedence::kTopLevel_Precedence);
856 std::vector<String> newArgs(fFormatArgs);
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400857 String expr = exprBuffer.str();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400858
859 // After generating, restore the original output stream and format args
860 fFormatArgs = oldArgs;
861 fOut = oldSKSL;
862
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400863 // The sksl written to exprBuffer is not processed by flushEmittedCode(), so any extra emit code
864 // block tokens won't get handled. So we need to strip them from the expression and stick them
865 // to the end of the original sksl stream.
866 String exprFormat = "";
867 int tokenStart = -1;
868 for (size_t i = 0; i < expr.size(); i++) {
869 if (tokenStart >= 0) {
870 if (expr[i] == '}') {
871 // End of the token, so append the token to fOut
872 fOut->write(expr.c_str() + tokenStart, i - tokenStart + 1);
873 tokenStart = -1;
874 }
875 } else {
876 if (i < expr.size() - 1 && expr[i] == '$' && expr[i + 1] == '{') {
877 tokenStart = i++;
878 } else {
879 exprFormat += expr[i];
880 }
881 }
882 }
883
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400884 // Now build the final C++ code snippet from the format string and args
885 String cppExpr;
John Stiles50819422020-06-18 13:00:38 -0400886 if (newArgs.empty()) {
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400887 // This was a static expression, so we can simplify the input
888 // color declaration in the emitted code to just a static string
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400889 cppExpr = "SkString " + cppVar + "(\"" + exprFormat + "\");";
John Stiles50819422020-06-18 13:00:38 -0400890 } else if (newArgs.size() == 1 && exprFormat == "%s") {
891 // If the format expression is simply "%s", we can avoid an expensive call to printf.
892 // This happens fairly often in codegen so it is worth simplifying.
893 cppExpr = "SkString " + cppVar + "(" + newArgs[0] + ");";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400894 } else {
895 // String formatting must occur dynamically, so have the C++ declaration
896 // use SkStringPrintf with the format args that were accumulated
897 // when the expression was written.
898 cppExpr = "SkString " + cppVar + " = SkStringPrintf(\"" + exprFormat + "\"";
899 for (size_t i = 0; i < newArgs.size(); i++) {
900 cppExpr += ", " + newArgs[i];
901 }
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400902 cppExpr += ");";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400903 }
904 return cppExpr;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400905}
906
Ethan Nicholas762466e2017-06-29 10:03:38 -0400907bool CPPCodeGenerator::writeEmitCode(std::vector<const Variable*>& uniforms) {
908 this->write(" void emitCode(EmitArgs& args) override {\n"
909 " GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;\n");
910 this->writef(" const %s& _outer = args.fFp.cast<%s>();\n"
911 " (void) _outer;\n",
912 fFullName.c_str(), fFullName.c_str());
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400913 for (const auto& p : fProgram) {
914 if (ProgramElement::kVar_Kind == p.fKind) {
915 const VarDeclarations& decls = (const VarDeclarations&) p;
916 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000917 VarDeclaration& decl = (VarDeclaration&) *raw;
918 String nameString(decl.fVar->fName);
Ethan Nicholas82399462017-10-16 12:35:44 -0400919 const char* name = nameString.c_str();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000920 if (SectionAndParameterHelper::IsParameter(*decl.fVar) &&
921 is_accessible(*decl.fVar)) {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400922 this->writef(" auto %s = _outer.%s;\n"
Ethan Nicholas82399462017-10-16 12:35:44 -0400923 " (void) %s;\n",
924 name, name, name);
925 }
926 }
927 }
928 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400929 this->writePrivateVarValues();
930 for (const auto u : uniforms) {
931 this->addUniform(*u);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400932 }
933 this->writeSection(EMIT_CODE_SECTION);
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400934
935 // Save original buffer as the CPP buffer for flushEmittedCode()
936 fCPPBuffer = fOut;
937 StringStream skslBuffer;
938 fOut = &skslBuffer;
939
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400940 this->newExtraEmitCodeBlock();
Ethan Nicholas762466e2017-06-29 10:03:38 -0400941 bool result = INHERITED::generateCode();
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400942 this->flushEmittedCode();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400943
944 // Then restore the original CPP buffer and close the function
945 fOut = fCPPBuffer;
946 fCPPBuffer = nullptr;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400947 this->write(" }\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400948 return result;
949}
950
951void CPPCodeGenerator::writeSetData(std::vector<const Variable*>& uniforms) {
952 const char* fullName = fFullName.c_str();
Ethan Nicholas68990be2017-07-13 09:36:52 -0400953 const Section* section = fSectionAndParameterHelper.getSection(SET_DATA_SECTION);
954 const char* pdman = section ? section->fArgument.c_str() : "pdman";
Ethan Nicholas762466e2017-06-29 10:03:38 -0400955 this->writef(" void onSetData(const GrGLSLProgramDataManager& %s, "
956 "const GrFragmentProcessor& _proc) override {\n",
957 pdman);
958 bool wroteProcessor = false;
John Stiles06f3d082020-06-04 11:07:21 -0400959 for (const Variable* u : uniforms) {
Michael Ludwiga4275592018-08-31 10:52:47 -0400960 if (is_uniform_in(*u)) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400961 if (!wroteProcessor) {
962 this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName, fullName);
963 wroteProcessor = true;
964 this->writef(" {\n");
965 }
Michael Ludwiga4275592018-08-31 10:52:47 -0400966
967 const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *u);
968 SkASSERT(mapper);
969
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700970 String nameString(u->fName);
971 const char* name = nameString.c_str();
Michael Ludwiga4275592018-08-31 10:52:47 -0400972
973 // Switches for setData behavior in the generated code
974 bool conditionalUniform = u->fModifiers.fLayout.fWhen != "";
975 bool isTracked = u->fModifiers.fLayout.fFlags & Layout::kTracked_Flag;
976 bool needsValueDeclaration = isTracked || !mapper->canInlineUniformValue();
977
978 String uniformName = HCodeGenerator::FieldName(name) + "Var";
979
980 String indent = " "; // 8 by default, 12 when nested for conditional uniforms
981 if (conditionalUniform) {
982 // Add a pre-check to make sure the uniform was emitted
983 // before trying to send any data to the GPU
984 this->writef(" if (%s.isValid()) {\n", uniformName.c_str());
985 indent += " ";
986 }
987
988 String valueVar = "";
989 if (needsValueDeclaration) {
990 valueVar.appendf("%sValue", name);
991 // Use AccessType since that will match the return type of _outer's public API.
992 String valueType = HCodeGenerator::AccessType(fContext, u->fType,
993 u->fModifiers.fLayout);
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400994 this->writef("%s%s %s = _outer.%s;\n",
Michael Ludwiga4275592018-08-31 10:52:47 -0400995 indent.c_str(), valueType.c_str(), valueVar.c_str(), name);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400996 } else {
Michael Ludwiga4275592018-08-31 10:52:47 -0400997 // Not tracked and the mapper only needs to use the value once
998 // so send it a safe expression instead of the variable name
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400999 valueVar.appendf("(_outer.%s)", name);
Michael Ludwiga4275592018-08-31 10:52:47 -04001000 }
1001
1002 if (isTracked) {
1003 SkASSERT(mapper->supportsTracking());
1004
1005 String prevVar = HCodeGenerator::FieldName(name) + "Prev";
1006 this->writef("%sif (%s) {\n"
1007 "%s %s;\n"
1008 "%s %s;\n"
1009 "%s}\n", indent.c_str(),
1010 mapper->dirtyExpression(valueVar, prevVar).c_str(), indent.c_str(),
1011 mapper->saveState(valueVar, prevVar).c_str(), indent.c_str(),
1012 mapper->setUniform(pdman, uniformName, valueVar).c_str(), indent.c_str());
1013 } else {
1014 this->writef("%s%s;\n", indent.c_str(),
1015 mapper->setUniform(pdman, uniformName, valueVar).c_str());
1016 }
1017
1018 if (conditionalUniform) {
1019 // Close the earlier precheck block
1020 this->writef(" }\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -04001021 }
1022 }
1023 }
1024 if (wroteProcessor) {
1025 this->writef(" }\n");
1026 }
Ethan Nicholas68990be2017-07-13 09:36:52 -04001027 if (section) {
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05001028 int samplerIndex = 0;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001029 for (const auto& p : fProgram) {
1030 if (ProgramElement::kVar_Kind == p.fKind) {
1031 const VarDeclarations& decls = (const VarDeclarations&) p;
John Stiles06f3d082020-06-04 11:07:21 -04001032 for (const std::unique_ptr<Statement>& raw : decls.fVars) {
1033 const VarDeclaration& decl = static_cast<VarDeclaration&>(*raw);
1034 const Variable& variable = *decl.fVar;
1035 String nameString(variable.fName);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001036 const char* name = nameString.c_str();
John Stiles06f3d082020-06-04 11:07:21 -04001037 if (variable.fType.kind() == Type::kSampler_Kind) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +00001038 this->writef(" const GrSurfaceProxyView& %sView = "
1039 "_outer.textureSampler(%d).view();\n",
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05001040 name, samplerIndex);
Robert Phillipsbd99c0c2019-12-12 13:26:58 +00001041 this->writef(" GrTexture& %s = *%sView.proxy()->peekTexture();\n",
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05001042 name, name);
1043 this->writef(" (void) %s;\n", name);
1044 ++samplerIndex;
John Stiles06f3d082020-06-04 11:07:21 -04001045 } else if (needs_uniform_var(variable)) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001046 this->writef(" UniformHandle& %s = %sVar;\n"
1047 " (void) %s;\n",
1048 name, HCodeGenerator::FieldName(name).c_str(), name);
John Stiles06f3d082020-06-04 11:07:21 -04001049 } else if (SectionAndParameterHelper::IsParameter(variable) &&
1050 variable.fType != *fContext.fFragmentProcessor_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001051 if (!wroteProcessor) {
1052 this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName,
1053 fullName);
1054 wroteProcessor = true;
1055 }
John Stiles06f3d082020-06-04 11:07:21 -04001056
1057 if (variable.fType.nonnullable() != *fContext.fFragmentProcessor_Type) {
1058 this->writef(" auto %s = _outer.%s;\n"
1059 " (void) %s;\n",
1060 name, name, name);
1061 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001062 }
1063 }
1064 }
1065 }
1066 this->writeSection(SET_DATA_SECTION);
1067 }
1068 this->write(" }\n");
1069}
1070
Brian Salomonf7dcd762018-07-30 14:48:15 -04001071void CPPCodeGenerator::writeOnTextureSampler() {
1072 bool foundSampler = false;
1073 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
1074 if (param->fType.kind() == Type::kSampler_Kind) {
1075 if (!foundSampler) {
1076 this->writef(
1077 "const GrFragmentProcessor::TextureSampler& %s::onTextureSampler(int "
1078 "index) const {\n",
1079 fFullName.c_str());
1080 this->writef(" return IthTextureSampler(index, %s",
1081 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
1082 foundSampler = true;
1083 } else {
1084 this->writef(", %s",
1085 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
1086 }
1087 }
1088 }
1089 if (foundSampler) {
1090 this->write(");\n}\n");
1091 }
1092}
1093
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001094void CPPCodeGenerator::writeClone() {
1095 if (!this->writeSection(CLONE_SECTION)) {
1096 if (fSectionAndParameterHelper.getSection(FIELDS_SECTION)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001097 fErrors.error(0, "fragment processors with custom @fields must also have a custom"
1098 "@clone");
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001099 }
1100 this->writef("%s::%s(const %s& src)\n"
Ethan Nicholasabff9562017-10-09 10:54:08 -04001101 ": INHERITED(k%s_ClassID, src.optimizationFlags())", fFullName.c_str(),
1102 fFullName.c_str(), fFullName.c_str(), fFullName.c_str());
John Stiles06f3d082020-06-04 11:07:21 -04001103 for (const Variable* param : fSectionAndParameterHelper.getParameters()) {
Robert Phillipsbce7d862019-02-21 22:53:57 +00001104 String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str());
John Stiles88183902020-06-10 16:40:38 -04001105 if (param->fType.nonnullable() != *fContext.fFragmentProcessor_Type) {
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001106 this->writef("\n, %s(src.%s)",
1107 fieldName.c_str(),
1108 fieldName.c_str());
1109 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001110 }
Ethan Nicholasabff9562017-10-09 10:54:08 -04001111 this->writef(" {\n");
Brian Salomonf7dcd762018-07-30 14:48:15 -04001112 int samplerCount = 0;
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001113 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
1114 if (param->fType.kind() == Type::kSampler_Kind) {
Brian Salomonf7dcd762018-07-30 14:48:15 -04001115 ++samplerCount;
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001116 } else if (param->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
1117 String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str());
1118 if (param->fType.kind() == Type::kNullable_Kind) {
John Stiles88183902020-06-10 16:40:38 -04001119 this->writef(" if (src.%s_index >= 0) {\n", fieldName.c_str());
Brian Salomonb243b432020-02-20 14:41:47 -05001120 } else {
1121 this->write(" {\n");
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001122 }
John Stiles3779f442020-06-15 10:48:49 -04001123 this->writef(" %s_index = this->cloneAndRegisterChildProcessor("
1124 "src.childProcessor(src.%s_index));\n"
1125 " }\n",
1126 fieldName.c_str(), fieldName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001127 }
1128 }
Brian Salomonf7dcd762018-07-30 14:48:15 -04001129 if (samplerCount) {
1130 this->writef(" this->setTextureSamplerCnt(%d);", samplerCount);
1131 }
Michael Ludwige88320b2020-06-24 09:04:56 -04001132 if (fAccessSampleCoordsDirectly) {
1133 this->writef(" this->setUsesSampleCoordsDirectly();\n");
1134 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001135 this->write("}\n");
Brian Salomonaff329b2017-08-11 09:40:37 -04001136 this->writef("std::unique_ptr<GrFragmentProcessor> %s::clone() const {\n",
1137 fFullName.c_str());
1138 this->writef(" return std::unique_ptr<GrFragmentProcessor>(new %s(*this));\n",
1139 fFullName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001140 this->write("}\n");
1141 }
1142}
1143
Ethan Nicholas762466e2017-06-29 10:03:38 -04001144void CPPCodeGenerator::writeTest() {
Ethan Nicholas68990be2017-07-13 09:36:52 -04001145 const Section* test = fSectionAndParameterHelper.getSection(TEST_CODE_SECTION);
1146 if (test) {
Brian Salomonaff329b2017-08-11 09:40:37 -04001147 this->writef(
1148 "GR_DEFINE_FRAGMENT_PROCESSOR_TEST(%s);\n"
1149 "#if GR_TEST_UTILS\n"
1150 "std::unique_ptr<GrFragmentProcessor> %s::TestCreate(GrProcessorTestData* %s) {\n",
1151 fFullName.c_str(),
1152 fFullName.c_str(),
1153 test->fArgument.c_str());
Ethan Nicholas68990be2017-07-13 09:36:52 -04001154 this->writeSection(TEST_CODE_SECTION);
1155 this->write("}\n"
1156 "#endif\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -04001157 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001158}
1159
1160void CPPCodeGenerator::writeGetKey() {
1161 this->writef("void %s::onGetGLSLProcessorKey(const GrShaderCaps& caps, "
1162 "GrProcessorKeyBuilder* b) const {\n",
1163 fFullName.c_str());
Ethan Nicholascab767f2019-07-01 13:32:07 -04001164 for (const auto& p : fProgram) {
1165 if (ProgramElement::kVar_Kind == p.fKind) {
1166 const VarDeclarations& decls = (const VarDeclarations&) p;
1167 for (const auto& raw : decls.fVars) {
1168 const VarDeclaration& decl = (VarDeclaration&) *raw;
1169 const Variable& var = *decl.fVar;
1170 String nameString(var.fName);
1171 const char* name = nameString.c_str();
1172 if (var.fModifiers.fLayout.fKey != Layout::kNo_Key &&
1173 (var.fModifiers.fFlags & Modifiers::kUniform_Flag)) {
1174 fErrors.error(var.fOffset,
1175 "layout(key) may not be specified on uniforms");
Ethan Nicholasbcd51e82019-04-09 10:40:41 -04001176 }
Ethan Nicholascab767f2019-07-01 13:32:07 -04001177 switch (var.fModifiers.fLayout.fKey) {
1178 case Layout::kKey_Key:
1179 if (is_private(var)) {
1180 this->writef("%s %s =",
1181 HCodeGenerator::FieldType(fContext, var.fType,
1182 var.fModifiers.fLayout).c_str(),
1183 String(var.fName).c_str());
1184 if (decl.fValue) {
1185 fCPPMode = true;
1186 this->writeExpression(*decl.fValue, kAssignment_Precedence);
1187 fCPPMode = false;
1188 } else {
1189 this->writef("%s", default_value(var).c_str());
1190 }
1191 this->write(";\n");
1192 }
1193 if (var.fModifiers.fLayout.fWhen.fLength) {
1194 this->writef("if (%s) {", String(var.fModifiers.fLayout.fWhen).c_str());
1195 }
1196 if (var.fType == *fContext.fFloat4x4_Type) {
1197 ABORT("no automatic key handling for float4x4\n");
1198 } else if (var.fType == *fContext.fFloat2_Type) {
1199 this->writef(" b->add32(%s.fX);\n",
1200 HCodeGenerator::FieldName(name).c_str());
1201 this->writef(" b->add32(%s.fY);\n",
1202 HCodeGenerator::FieldName(name).c_str());
1203 } else if (var.fType == *fContext.fFloat4_Type) {
1204 this->writef(" b->add32(%s.x());\n",
1205 HCodeGenerator::FieldName(name).c_str());
1206 this->writef(" b->add32(%s.y());\n",
1207 HCodeGenerator::FieldName(name).c_str());
1208 this->writef(" b->add32(%s.width());\n",
1209 HCodeGenerator::FieldName(name).c_str());
1210 this->writef(" b->add32(%s.height());\n",
1211 HCodeGenerator::FieldName(name).c_str());
1212 } else if (var.fType == *fContext.fHalf4_Type) {
1213 this->writef(" uint16_t red = SkFloatToHalf(%s.fR);\n",
1214 HCodeGenerator::FieldName(name).c_str());
1215 this->writef(" uint16_t green = SkFloatToHalf(%s.fG);\n",
1216 HCodeGenerator::FieldName(name).c_str());
1217 this->writef(" uint16_t blue = SkFloatToHalf(%s.fB);\n",
1218 HCodeGenerator::FieldName(name).c_str());
1219 this->writef(" uint16_t alpha = SkFloatToHalf(%s.fA);\n",
1220 HCodeGenerator::FieldName(name).c_str());
1221 this->write(" b->add32(((uint32_t)red << 16) | green);\n");
1222 this->write(" b->add32(((uint32_t)blue << 16) | alpha);\n");
1223 } else {
1224 this->writef(" b->add32((int32_t) %s);\n",
1225 HCodeGenerator::FieldName(name).c_str());
1226 }
1227 if (var.fModifiers.fLayout.fWhen.fLength) {
1228 this->write("}");
1229 }
1230 break;
1231 case Layout::kIdentity_Key:
1232 if (var.fType.kind() != Type::kMatrix_Kind) {
1233 fErrors.error(var.fOffset,
1234 "layout(key=identity) requires matrix type");
1235 }
1236 this->writef(" b->add32(%s.isIdentity() ? 1 : 0);\n",
1237 HCodeGenerator::FieldName(name).c_str());
1238 break;
1239 case Layout::kNo_Key:
1240 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001241 }
Ethan Nicholascab767f2019-07-01 13:32:07 -04001242 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001243 }
1244 }
1245 this->write("}\n");
1246}
1247
1248bool CPPCodeGenerator::generateCode() {
1249 std::vector<const Variable*> uniforms;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001250 for (const auto& p : fProgram) {
1251 if (ProgramElement::kVar_Kind == p.fKind) {
1252 const VarDeclarations& decls = (const VarDeclarations&) p;
1253 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001254 VarDeclaration& decl = (VarDeclaration&) *raw;
1255 if ((decl.fVar->fModifiers.fFlags & Modifiers::kUniform_Flag) &&
1256 decl.fVar->fType.kind() != Type::kSampler_Kind) {
1257 uniforms.push_back(decl.fVar);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001258 }
Michael Ludwiga4275592018-08-31 10:52:47 -04001259
1260 if (is_uniform_in(*decl.fVar)) {
1261 // Validate the "uniform in" declarations to make sure they are fully supported,
1262 // instead of generating surprising C++
1263 const UniformCTypeMapper* mapper =
1264 UniformCTypeMapper::Get(fContext, *decl.fVar);
1265 if (mapper == nullptr) {
1266 fErrors.error(decl.fOffset, String(decl.fVar->fName)
1267 + "'s type is not supported for use as a 'uniform in'");
1268 return false;
1269 }
1270 if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
1271 if (!mapper->supportsTracking()) {
1272 fErrors.error(decl.fOffset, String(decl.fVar->fName)
1273 + "'s type does not support state tracking");
1274 return false;
1275 }
1276 }
1277
1278 } else {
1279 // If it's not a uniform_in, it's an error to be tracked
1280 if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
1281 fErrors.error(decl.fOffset, "Non-'in uniforms' cannot be tracked");
1282 return false;
1283 }
1284 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001285 }
1286 }
1287 }
1288 const char* baseName = fName.c_str();
1289 const char* fullName = fFullName.c_str();
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05001290 this->writef("%s\n", HCodeGenerator::GetHeader(fProgram, fErrors).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001291 this->writef(kFragmentProcessorHeader, fullName);
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001292 this->writef("#include \"%s.h\"\n\n", fullName);
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04001293 this->writeSection(CPP_SECTION);
Greg Daniel456f9b52020-03-05 19:14:18 +00001294 this->writef("#include \"src/gpu/GrTexture.h\"\n"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001295 "#include \"src/gpu/glsl/GrGLSLFragmentProcessor.h\"\n"
1296 "#include \"src/gpu/glsl/GrGLSLFragmentShaderBuilder.h\"\n"
1297 "#include \"src/gpu/glsl/GrGLSLProgramBuilder.h\"\n"
1298 "#include \"src/sksl/SkSLCPP.h\"\n"
1299 "#include \"src/sksl/SkSLUtil.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -04001300 "class GrGLSL%s : public GrGLSLFragmentProcessor {\n"
1301 "public:\n"
1302 " GrGLSL%s() {}\n",
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04001303 baseName, baseName);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001304 bool result = this->writeEmitCode(uniforms);
1305 this->write("private:\n");
1306 this->writeSetData(uniforms);
1307 this->writePrivateVars();
1308 for (const auto& u : uniforms) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001309 if (needs_uniform_var(*u) && !(u->fModifiers.fFlags & Modifiers::kIn_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001310 this->writef(" UniformHandle %sVar;\n",
1311 HCodeGenerator::FieldName(String(u->fName).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001312 }
1313 }
Ethan Nicholas68990be2017-07-13 09:36:52 -04001314 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001315 if (needs_uniform_var(*param)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001316 this->writef(" UniformHandle %sVar;\n",
1317 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001318 }
1319 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001320 this->writef("};\n"
1321 "GrGLSLFragmentProcessor* %s::onCreateGLSLInstance() const {\n"
1322 " return new GrGLSL%s();\n"
1323 "}\n",
1324 fullName, baseName);
1325 this->writeGetKey();
1326 this->writef("bool %s::onIsEqual(const GrFragmentProcessor& other) const {\n"
1327 " const %s& that = other.cast<%s>();\n"
1328 " (void) that;\n",
1329 fullName, fullName, fullName);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001330 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001331 if (param->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -04001332 continue;
1333 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001334 String nameString(param->fName);
1335 const char* name = nameString.c_str();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001336 this->writef(" if (%s != that.%s) return false;\n",
1337 HCodeGenerator::FieldName(name).c_str(),
1338 HCodeGenerator::FieldName(name).c_str());
1339 }
1340 this->write(" return true;\n"
1341 "}\n");
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001342 this->writeClone();
Brian Salomonf7dcd762018-07-30 14:48:15 -04001343 this->writeOnTextureSampler();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001344 this->writeTest();
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04001345 this->writeSection(CPP_END_SECTION);
Greg Daniel3e8c3452018-04-06 10:37:55 -04001346
Ethan Nicholas762466e2017-06-29 10:03:38 -04001347 result &= 0 == fErrors.errorCount();
1348 return result;
1349}
1350
1351} // namespace