blob: 4a8016e0be66d1bda137f81e536453ce73087e5a [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 Osman061a5cf2020-06-24 14:50:25 -040010#include "include/private/SkSLSampleMatrix.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/sksl/SkSLCPPUniformCTypes.h"
12#include "src/sksl/SkSLCompiler.h"
13#include "src/sksl/SkSLHCodeGenerator.h"
Ethan Nicholas762466e2017-06-29 10:03:38 -040014
Michael Ludwig92e4c7f2018-08-30 16:08:18 -040015#include <algorithm>
16
Ethan Nicholas762466e2017-06-29 10:03:38 -040017namespace SkSL {
18
19static bool needs_uniform_var(const Variable& var) {
Ethan Nicholas5f9836e2017-12-20 15:16:33 -050020 return (var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
21 var.fType.kind() != Type::kSampler_Kind;
Ethan Nicholas762466e2017-06-29 10:03:38 -040022}
23
24CPPCodeGenerator::CPPCodeGenerator(const Context* context, const Program* program,
25 ErrorReporter* errors, String name, OutputStream* out)
John Stiles50819422020-06-18 13:00:38 -040026 : INHERITED(context, program, errors, out)
27 , fName(std::move(name))
28 , fFullName(String::printf("Gr%s", fName.c_str()))
29 , fSectionAndParameterHelper(program, *errors) {
30 fLineEnding = "\n";
Ethan Nicholas13863662019-07-29 13:05:15 -040031 fTextureFunctionOverride = "sample";
Ethan Nicholas762466e2017-06-29 10:03:38 -040032}
33
34void CPPCodeGenerator::writef(const char* s, va_list va) {
35 static constexpr int BUFFER_SIZE = 1024;
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040036 va_list copy;
37 va_copy(copy, va);
Ethan Nicholas762466e2017-06-29 10:03:38 -040038 char buffer[BUFFER_SIZE];
John Stiles50819422020-06-18 13:00:38 -040039 int length = std::vsnprintf(buffer, BUFFER_SIZE, s, va);
Ethan Nicholas762466e2017-06-29 10:03:38 -040040 if (length < BUFFER_SIZE) {
41 fOut->write(buffer, length);
42 } else {
43 std::unique_ptr<char[]> heap(new char[length + 1]);
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040044 vsprintf(heap.get(), s, copy);
Ethan Nicholas762466e2017-06-29 10:03:38 -040045 fOut->write(heap.get(), length);
46 }
z102.zhangd74f2c82018-08-10 09:08:47 +080047 va_end(copy);
Ethan Nicholas762466e2017-06-29 10:03:38 -040048}
49
50void CPPCodeGenerator::writef(const char* s, ...) {
51 va_list va;
52 va_start(va, s);
53 this->writef(s, va);
54 va_end(va);
55}
56
57void CPPCodeGenerator::writeHeader() {
58}
59
Ethan Nicholasf7b88202017-09-18 14:10:39 -040060bool CPPCodeGenerator::usesPrecisionModifiers() const {
61 return false;
Ethan Nicholas762466e2017-06-29 10:03:38 -040062}
63
Ethan Nicholasf7b88202017-09-18 14:10:39 -040064String CPPCodeGenerator::getTypeName(const Type& type) {
65 return type.name();
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040066}
Ethan Nicholasf7b88202017-09-18 14:10:39 -040067
Ethan Nicholas762466e2017-06-29 10:03:38 -040068void CPPCodeGenerator::writeBinaryExpression(const BinaryExpression& b,
69 Precedence parentPrecedence) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -040070 if (b.fOperator == Token::Kind::TK_PERCENT) {
Ethan Nicholas762466e2017-06-29 10:03:38 -040071 // need to use "%%" instead of "%" b/c the code will be inside of a printf
72 Precedence precedence = GetBinaryPrecedence(b.fOperator);
73 if (precedence >= parentPrecedence) {
74 this->write("(");
75 }
76 this->writeExpression(*b.fLeft, precedence);
77 this->write(" %% ");
78 this->writeExpression(*b.fRight, precedence);
79 if (precedence >= parentPrecedence) {
80 this->write(")");
81 }
Ethan Nicholasee1c8a72019-02-22 10:50:47 -050082 } else if (b.fLeft->fKind == Expression::kNullLiteral_Kind ||
83 b.fRight->fKind == Expression::kNullLiteral_Kind) {
84 const Variable* var;
85 if (b.fLeft->fKind != Expression::kNullLiteral_Kind) {
86 SkASSERT(b.fLeft->fKind == Expression::kVariableReference_Kind);
87 var = &((VariableReference&) *b.fLeft).fVariable;
88 } else {
89 SkASSERT(b.fRight->fKind == Expression::kVariableReference_Kind);
90 var = &((VariableReference&) *b.fRight).fVariable;
91 }
92 SkASSERT(var->fType.kind() == Type::kNullable_Kind &&
93 var->fType.componentType() == *fContext.fFragmentProcessor_Type);
94 this->write("%s");
95 const char* op;
96 switch (b.fOperator) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -040097 case Token::Kind::TK_EQEQ:
Ethan Nicholasee1c8a72019-02-22 10:50:47 -050098 op = "<";
99 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400100 case Token::Kind::TK_NEQ:
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500101 op = ">=";
102 break;
103 default:
104 SkASSERT(false);
105 }
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400106 fFormatArgs.push_back("_outer." + String(var->fName) + "_index " + op + " 0 ? \"true\" "
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500107 ": \"false\"");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400108 } else {
109 INHERITED::writeBinaryExpression(b, parentPrecedence);
110 }
111}
112
113void CPPCodeGenerator::writeIndexExpression(const IndexExpression& i) {
114 const Expression& base = *i.fBase;
115 if (base.fKind == Expression::kVariableReference_Kind) {
116 int builtin = ((VariableReference&) base).fVariable.fModifiers.fLayout.fBuiltin;
117 if (SK_TRANSFORMEDCOORDS2D_BUILTIN == builtin) {
118 this->write("%s");
119 if (i.fIndex->fKind != Expression::kIntLiteral_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700120 fErrors.error(i.fIndex->fOffset,
Ethan Nicholas762466e2017-06-29 10:03:38 -0400121 "index into sk_TransformedCoords2D must be an integer literal");
122 return;
123 }
124 int64_t index = ((IntLiteral&) *i.fIndex).fValue;
Michael Ludwig9aba6252020-06-22 14:46:36 -0400125 if (index != 0) {
126 fErrors.error(i.fIndex->fOffset, "Only sk_TransformedCoords2D[0] is allowed");
127 return;
128 }
Michael Ludwige88320b2020-06-24 09:04:56 -0400129 fAccessSampleCoordsDirectly = true;
130 fFormatArgs.push_back("args.fSampleCoord");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400131 return;
132 } else if (SK_TEXTURESAMPLERS_BUILTIN == builtin) {
133 this->write("%s");
134 if (i.fIndex->fKind != Expression::kIntLiteral_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700135 fErrors.error(i.fIndex->fOffset,
Ethan Nicholas762466e2017-06-29 10:03:38 -0400136 "index into sk_TextureSamplers must be an integer literal");
137 return;
138 }
139 int64_t index = ((IntLiteral&) *i.fIndex).fValue;
140 fFormatArgs.push_back(" fragBuilder->getProgramBuilder()->samplerVariable("
Stephen Whited523a062019-06-19 13:12:46 -0400141 "args.fTexSamplers[" + to_string(index) + "])");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400142 return;
143 }
144 }
145 INHERITED::writeIndexExpression(i);
146}
147
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400148static String default_value(const Type& type) {
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500149 if (type.fName == "bool") {
150 return "false";
151 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400152 switch (type.kind()) {
153 case Type::kScalar_Kind: return "0";
154 case Type::kVector_Kind: return type.name() + "(0)";
155 case Type::kMatrix_Kind: return type.name() + "(1)";
156 default: ABORT("unsupported default_value type\n");
157 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400158}
159
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500160static String default_value(const Variable& var) {
Brian Osman495993a2018-10-16 15:45:55 -0400161 if (var.fModifiers.fLayout.fCType == SkSL::Layout::CType::kSkPMColor4f) {
Brian Osmanf28e55d2018-10-03 16:35:54 -0400162 return "{SK_FloatNaN, SK_FloatNaN, SK_FloatNaN, SK_FloatNaN}";
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500163 }
164 return default_value(var.fType);
165}
166
Ethan Nicholas762466e2017-06-29 10:03:38 -0400167static bool is_private(const Variable& var) {
168 return !(var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
169 !(var.fModifiers.fFlags & Modifiers::kIn_Flag) &&
170 var.fStorage == Variable::kGlobal_Storage &&
171 var.fModifiers.fLayout.fBuiltin == -1;
172}
173
Michael Ludwiga4275592018-08-31 10:52:47 -0400174static bool is_uniform_in(const Variable& var) {
175 return (var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
176 (var.fModifiers.fFlags & Modifiers::kIn_Flag) &&
177 var.fType.kind() != Type::kSampler_Kind;
178}
179
Ethan Nicholasd608c092017-10-26 09:30:08 -0400180void CPPCodeGenerator::writeRuntimeValue(const Type& type, const Layout& layout,
181 const String& cppCode) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400182 if (type.isFloat()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400183 this->write("%f");
184 fFormatArgs.push_back(cppCode);
185 } else if (type == *fContext.fInt_Type) {
186 this->write("%d");
187 fFormatArgs.push_back(cppCode);
188 } else if (type == *fContext.fBool_Type) {
189 this->write("%s");
190 fFormatArgs.push_back("(" + cppCode + " ? \"true\" : \"false\")");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400191 } else if (type == *fContext.fFloat2_Type || type == *fContext.fHalf2_Type) {
192 this->write(type.name() + "(%f, %f)");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400193 fFormatArgs.push_back(cppCode + ".fX");
194 fFormatArgs.push_back(cppCode + ".fY");
Ethan Nicholas82399462017-10-16 12:35:44 -0400195 } else if (type == *fContext.fFloat4_Type || type == *fContext.fHalf4_Type) {
196 this->write(type.name() + "(%f, %f, %f, %f)");
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400197 switch (layout.fCType) {
198 case Layout::CType::kSkPMColor:
199 fFormatArgs.push_back("SkGetPackedR32(" + cppCode + ") / 255.0");
200 fFormatArgs.push_back("SkGetPackedG32(" + cppCode + ") / 255.0");
201 fFormatArgs.push_back("SkGetPackedB32(" + cppCode + ") / 255.0");
202 fFormatArgs.push_back("SkGetPackedA32(" + cppCode + ") / 255.0");
203 break;
Brian Osmanf28e55d2018-10-03 16:35:54 -0400204 case Layout::CType::kSkPMColor4f:
205 fFormatArgs.push_back(cppCode + ".fR");
206 fFormatArgs.push_back(cppCode + ".fG");
207 fFormatArgs.push_back(cppCode + ".fB");
208 fFormatArgs.push_back(cppCode + ".fA");
209 break;
Mike Reedb26b4e72020-01-22 14:31:21 -0500210 case Layout::CType::kSkV4:
211 fFormatArgs.push_back(cppCode + ".x");
212 fFormatArgs.push_back(cppCode + ".y");
213 fFormatArgs.push_back(cppCode + ".z");
214 fFormatArgs.push_back(cppCode + ".w");
Brian Salomoneca66b32019-06-01 11:18:15 -0400215 break;
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400216 case Layout::CType::kSkRect: // fall through
217 case Layout::CType::kDefault:
218 fFormatArgs.push_back(cppCode + ".left()");
219 fFormatArgs.push_back(cppCode + ".top()");
220 fFormatArgs.push_back(cppCode + ".right()");
221 fFormatArgs.push_back(cppCode + ".bottom()");
222 break;
223 default:
224 SkASSERT(false);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400225 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500226 } else if (type.kind() == Type::kEnum_Kind) {
227 this->write("%d");
228 fFormatArgs.push_back("(int) " + cppCode);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400229 } else if (type == *fContext.fInt4_Type ||
230 type == *fContext.fShort4_Type ||
231 type == *fContext.fByte4_Type) {
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500232 this->write(type.name() + "(%d, %d, %d, %d)");
233 fFormatArgs.push_back(cppCode + ".left()");
234 fFormatArgs.push_back(cppCode + ".top()");
235 fFormatArgs.push_back(cppCode + ".right()");
236 fFormatArgs.push_back(cppCode + ".bottom()");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400237 } else {
Ethan Nicholas82399462017-10-16 12:35:44 -0400238 printf("unsupported runtime value type '%s'\n", String(type.fName).c_str());
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400239 SkASSERT(false);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400240 }
241}
242
243void CPPCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) {
244 if (is_private(var)) {
Ethan Nicholasd608c092017-10-26 09:30:08 -0400245 this->writeRuntimeValue(var.fType, var.fModifiers.fLayout, var.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400246 } else {
247 this->writeExpression(value, kTopLevel_Precedence);
248 }
249}
250
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400251String CPPCodeGenerator::getSamplerHandle(const Variable& var) {
252 int samplerCount = 0;
Ethan Nicholas68990be2017-07-13 09:36:52 -0400253 for (const auto param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400254 if (&var == param) {
255 return "args.fTexSamplers[" + to_string(samplerCount) + "]";
256 }
257 if (param->fType.kind() == Type::kSampler_Kind) {
258 ++samplerCount;
259 }
260 }
261 ABORT("should have found sampler in parameters\n");
262}
263
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400264void CPPCodeGenerator::writeIntLiteral(const IntLiteral& i) {
265 this->write(to_string((int32_t) i.fValue));
266}
267
Ethan Nicholas82399462017-10-16 12:35:44 -0400268void CPPCodeGenerator::writeSwizzle(const Swizzle& swizzle) {
269 if (fCPPMode) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400270 SkASSERT(swizzle.fComponents.size() == 1); // no support for multiple swizzle components yet
Ethan Nicholas82399462017-10-16 12:35:44 -0400271 this->writeExpression(*swizzle.fBase, kPostfix_Precedence);
272 switch (swizzle.fComponents[0]) {
273 case 0: this->write(".left()"); break;
274 case 1: this->write(".top()"); break;
275 case 2: this->write(".right()"); break;
276 case 3: this->write(".bottom()"); break;
277 }
278 } else {
279 INHERITED::writeSwizzle(swizzle);
280 }
281}
282
Ethan Nicholas762466e2017-06-29 10:03:38 -0400283void CPPCodeGenerator::writeVariableReference(const VariableReference& ref) {
Ethan Nicholas82399462017-10-16 12:35:44 -0400284 if (fCPPMode) {
285 this->write(ref.fVariable.fName);
286 return;
287 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400288 switch (ref.fVariable.fModifiers.fLayout.fBuiltin) {
289 case SK_INCOLOR_BUILTIN:
290 this->write("%s");
Michael Ludwig231de032018-08-30 14:33:01 -0400291 // EmitArgs.fInputColor is automatically set to half4(1) if
292 // no input was specified
293 fFormatArgs.push_back(String("args.fInputColor"));
Ethan Nicholas762466e2017-06-29 10:03:38 -0400294 break;
295 case SK_OUTCOLOR_BUILTIN:
296 this->write("%s");
297 fFormatArgs.push_back(String("args.fOutputColor"));
298 break;
Ethan Nicholascd700e92018-08-24 16:43:57 -0400299 case SK_WIDTH_BUILTIN:
300 this->write("sk_Width");
301 break;
302 case SK_HEIGHT_BUILTIN:
303 this->write("sk_Height");
304 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400305 default:
306 if (ref.fVariable.fType.kind() == Type::kSampler_Kind) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400307 this->write("%s");
308 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerVariable(" +
Stephen Whited523a062019-06-19 13:12:46 -0400309 this->getSamplerHandle(ref.fVariable) + ")");
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400310 return;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400311 }
312 if (ref.fVariable.fModifiers.fFlags & Modifiers::kUniform_Flag) {
313 this->write("%s");
314 String name = ref.fVariable.fName;
Brian Osman1cb41712017-10-19 12:54:52 -0400315 String var = String::printf("args.fUniformHandler->getUniformCStr(%sVar)",
316 HCodeGenerator::FieldName(name.c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400317 String code;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400318 if (ref.fVariable.fModifiers.fLayout.fWhen.fLength) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400319 code = String::printf("%sVar.isValid() ? %s : \"%s\"",
320 HCodeGenerator::FieldName(name.c_str()).c_str(),
321 var.c_str(),
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400322 default_value(ref.fVariable.fType).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400323 } else {
324 code = var;
325 }
326 fFormatArgs.push_back(code);
327 } else if (SectionAndParameterHelper::IsParameter(ref.fVariable)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700328 String name(ref.fVariable.fName);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400329 this->writeRuntimeValue(ref.fVariable.fType, ref.fVariable.fModifiers.fLayout,
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400330 String::printf("_outer.%s", name.c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400331 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700332 this->write(ref.fVariable.fName);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400333 }
334 }
335}
336
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400337void CPPCodeGenerator::writeIfStatement(const IfStatement& s) {
338 if (s.fIsStatic) {
339 this->write("@");
340 }
341 INHERITED::writeIfStatement(s);
342}
343
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400344void CPPCodeGenerator::writeReturnStatement(const ReturnStatement& s) {
345 if (fInMain) {
346 fErrors.error(s.fOffset, "fragmentProcessor main() may not contain return statements");
347 }
348 INHERITED::writeReturnStatement(s);
349}
350
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400351void CPPCodeGenerator::writeSwitchStatement(const SwitchStatement& s) {
352 if (s.fIsStatic) {
353 this->write("@");
354 }
355 INHERITED::writeSwitchStatement(s);
356}
357
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400358void CPPCodeGenerator::writeFieldAccess(const FieldAccess& access) {
359 if (access.fBase->fType.name() == "fragmentProcessor") {
360 // Special field access on fragment processors are converted into function calls on
361 // GrFragmentProcessor's getters.
362 if (access.fBase->fKind != Expression::kVariableReference_Kind) {
363 fErrors.error(access.fBase->fOffset, "fragmentProcessor must be a reference\n");
364 return;
365 }
366
367 const Type::Field& field = fContext.fFragmentProcessor_Type->fields()[access.fFieldIndex];
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500368 const Variable& var = ((const VariableReference&) *access.fBase).fVariable;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400369 String cppAccess = String::printf("_outer.childProcessor(_outer.%s_index).%s()",
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500370 String(var.fName).c_str(),
371 String(field.fName).c_str());
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400372
373 if (fCPPMode) {
374 this->write(cppAccess.c_str());
375 } else {
376 writeRuntimeValue(*field.fType, Layout(), cppAccess);
377 }
378 return;
379 }
380 INHERITED::writeFieldAccess(access);
381}
382
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500383int CPPCodeGenerator::getChildFPIndex(const Variable& var) const {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400384 int index = 0;
385 bool found = false;
386 for (const auto& p : fProgram) {
387 if (ProgramElement::kVar_Kind == p.fKind) {
388 const VarDeclarations& decls = (const VarDeclarations&) p;
389 for (const auto& raw : decls.fVars) {
390 const VarDeclaration& decl = (VarDeclaration&) *raw;
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500391 if (decl.fVar == &var) {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400392 found = true;
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500393 } else if (decl.fVar->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400394 ++index;
395 }
396 }
397 }
398 if (found) {
399 break;
400 }
401 }
402 SkASSERT(found);
403 return index;
404}
405
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400406void CPPCodeGenerator::writeFunctionCall(const FunctionCall& c) {
Ethan Nicholas13863662019-07-29 13:05:15 -0400407 if (c.fFunction.fBuiltin && c.fFunction.fName == "sample" &&
408 c.fArguments[0]->fType.kind() != Type::Kind::kSampler_Kind) {
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400409 // Sanity checks that are detected by function definition in sksl_fp.inc
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400410 SkASSERT(c.fArguments.size() >= 1 && c.fArguments.size() <= 3);
Florin Malita390f9bd2019-03-04 12:25:57 -0500411 SkASSERT("fragmentProcessor" == c.fArguments[0]->fType.name() ||
412 "fragmentProcessor?" == c.fArguments[0]->fType.name());
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400413
414 // Actually fail during compilation if arguments with valid types are
Ethan Nicholas13863662019-07-29 13:05:15 -0400415 // provided that are not variable references, since sample() is a
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400416 // special function that impacts code emission.
417 if (c.fArguments[0]->fKind != Expression::kVariableReference_Kind) {
418 fErrors.error(c.fArguments[0]->fOffset,
Ethan Nicholas13863662019-07-29 13:05:15 -0400419 "sample()'s fragmentProcessor argument must be a variable reference\n");
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400420 return;
421 }
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500422 const Variable& child = ((const VariableReference&) *c.fArguments[0]).fVariable;
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400423
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400424 // Start a new extra emit code section so that the emitted child processor can depend on
425 // sksl variables defined in earlier sksl code.
426 this->newExtraEmitCodeBlock();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400427
Michael Ludwige88320b2020-06-24 09:04:56 -0400428 String inputColorName; // the sksl variable/expression, referenced later for null child FPs
429 String inputColor;
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400430 if (c.fArguments.size() > 1 && c.fArguments[1]->fType.name() == "half4") {
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400431 // Use the invokeChild() variant that accepts an input color, so convert the 2nd
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400432 // argument's expression into C++ code that produces sksl stored in an SkString.
John Stilesd060c9d2020-06-08 11:44:25 -0400433 inputColorName = "_input" + to_string(c.fOffset);
434 addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments[1], inputColorName));
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400435
Michael Ludwige88320b2020-06-24 09:04:56 -0400436 // invokeChild() needs a char* and a pre-pended comma
437 inputColor = ", " + inputColorName + ".c_str()";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400438 }
439
Michael Ludwige88320b2020-06-24 09:04:56 -0400440 String inputCoord;
441 String invokeFunction = "invokeChild";
442 if (c.fArguments.back()->fType.name() == "float2") {
443 // Invoking child with explicit coordinates at this call site
444 inputCoord = "_coords" + to_string(c.fOffset);
445 addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments.back(), inputCoord));
446 inputCoord.append(".c_str()");
447 } else if (c.fArguments.back()->fType.name() == "float3x3") {
448 // Invoking child with a matrix, sampling relative to the input coords.
449 invokeFunction = "invokeChildWithMatrix";
450 SampleMatrix matrix = SampleMatrix::Make(fProgram, child);
451
452 if (!matrix.isConstUniform()) {
453 inputCoord = "_matrix" + to_string(c.fOffset);
454 addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments.back(), inputCoord));
455 inputCoord.append(".c_str()");
456 }
457 // else pass in the empty string to rely on invokeChildWithMatrix's automatic uniform
458 // resolution
459 }
460 if (!inputCoord.empty()) {
461 inputCoord = ", " + inputCoord;
462 }
463
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400464 // Write the output handling after the possible input handling
Ethan Nicholas13863662019-07-29 13:05:15 -0400465 String childName = "_sample" + to_string(c.fOffset);
Brian Osman978693c2020-01-24 14:52:10 -0500466 addExtraEmitCodeLine("SkString " + childName + ";");
Michael Ludwige88320b2020-06-24 09:04:56 -0400467
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500468 if (c.fArguments[0]->fType.kind() == Type::kNullable_Kind) {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400469 addExtraEmitCodeLine("if (_outer." + String(child.fName) + "_index >= 0) {\n ");
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500470 }
Michael Ludwige88320b2020-06-24 09:04:56 -0400471 addExtraEmitCodeLine(childName + " = this->" + invokeFunction + "(_outer." +
472 String(child.fName) + "_index" + inputColor + ", args" +
473 inputCoord + ");");
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400474
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500475 if (c.fArguments[0]->fType.kind() == Type::kNullable_Kind) {
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000476 // Null FPs are not emitted, but their output can still be referenced in dependent
Brian Osman978693c2020-01-24 14:52:10 -0500477 // expressions - thus we always fill the variable with something.
John Stilesd060c9d2020-06-08 11:44:25 -0400478 // Sampling from a null fragment processor will provide in the input color as-is. This
479 // defaults to half4(1) if no color is specified.
John Stiles50819422020-06-18 13:00:38 -0400480 if (!inputColorName.empty()) {
481 addExtraEmitCodeLine(
482 "} else {"
483 " " + childName + ".swap(" + inputColorName + ");"
484 "}");
485 } else {
486 addExtraEmitCodeLine(
487 "} else {"
488 " " + childName + " = \"half4(1)\";"
489 "}");
490 }
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500491 }
John Stiles50819422020-06-18 13:00:38 -0400492
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000493 this->write("%s");
494 fFormatArgs.push_back(childName + ".c_str()");
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400495 return;
496 }
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400497 if (c.fFunction.fBuiltin) {
498 INHERITED::writeFunctionCall(c);
499 } else {
500 this->write("%s");
501 fFormatArgs.push_back((String(c.fFunction.fName) + "_name.c_str()").c_str());
502 this->write("(");
503 const char* separator = "";
504 for (const auto& arg : c.fArguments) {
505 this->write(separator);
506 separator = ", ";
507 this->writeExpression(*arg, kSequence_Precedence);
508 }
509 this->write(")");
510 }
Ethan Nicholas13863662019-07-29 13:05:15 -0400511 if (c.fFunction.fBuiltin && c.fFunction.fName == "sample") {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400512 this->write(".%s");
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400513 SkASSERT(c.fArguments.size() >= 1);
514 SkASSERT(c.fArguments[0]->fKind == Expression::kVariableReference_Kind);
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400515 String sampler = this->getSamplerHandle(((VariableReference&) *c.fArguments[0]).fVariable);
516 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerSwizzle(" + sampler +
Greg Daniel369ee6b2019-12-02 15:30:02 -0500517 ").asString().c_str()");
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400518 }
519}
520
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400521static const char* glsltype_string(const Context& context, const Type& type) {
522 if (type == *context.fFloat_Type) {
523 return "kFloat_GrSLType";
524 } else if (type == *context.fHalf_Type) {
525 return "kHalf_GrSLType";
526 } else if (type == *context.fFloat2_Type) {
527 return "kFloat2_GrSLType";
528 } else if (type == *context.fHalf2_Type) {
529 return "kHalf2_GrSLType";
Ethan Nicholas8ae1b562019-12-17 15:18:02 -0500530 } else if (type == *context.fFloat3_Type) {
531 return "kFloat3_GrSLType";
532 } else if (type == *context.fHalf3_Type) {
533 return "kHalf3_GrSLType";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400534 } else if (type == *context.fFloat4_Type) {
535 return "kFloat4_GrSLType";
536 } else if (type == *context.fHalf4_Type) {
537 return "kHalf4_GrSLType";
Ethan Nicholas58430122020-04-14 09:54:02 -0400538 } else if (type == *context.fFloat2x2_Type) {
539 return "kFloat2x2_GrSLType";
540 } else if (type == *context.fHalf2x2_Type) {
541 return "kHalf2x2_GrSLType";
542 } else if (type == *context.fFloat3x3_Type) {
543 return "kFloat3x3_GrSLType";
544 } else if (type == *context.fHalf3x3_Type) {
545 return "kHalf3x3_GrSLType";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400546 } else if (type == *context.fFloat4x4_Type) {
547 return "kFloat4x4_GrSLType";
548 } else if (type == *context.fHalf4x4_Type) {
549 return "kHalf4x4_GrSLType";
550 } else if (type == *context.fVoid_Type) {
551 return "kVoid_GrSLType";
Ethan Nicholas8ae1b562019-12-17 15:18:02 -0500552 } else if (type.kind() == Type::kEnum_Kind) {
553 return "int";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400554 }
555 SkASSERT(false);
556 return nullptr;
557}
558
Ethan Nicholas762466e2017-06-29 10:03:38 -0400559void CPPCodeGenerator::writeFunction(const FunctionDefinition& f) {
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400560 const FunctionDeclaration& decl = f.fDeclaration;
Brian Osman08f986d2020-05-13 17:06:46 -0400561 if (decl.fBuiltin) {
562 return;
563 }
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400564 fFunctionHeader = "";
565 OutputStream* oldOut = fOut;
566 StringStream buffer;
567 fOut = &buffer;
568 if (decl.fName == "main") {
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400569 fInMain = true;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400570 for (const auto& s : ((Block&) *f.fBody).fStatements) {
571 this->writeStatement(*s);
572 this->writeLine();
573 }
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400574 fInMain = false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400575
576 fOut = oldOut;
577 this->write(fFunctionHeader);
578 this->write(buffer.str());
579 } else {
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400580 this->addExtraEmitCodeLine("SkString " + decl.fName + "_name;");
581 String args = "const GrShaderVar " + decl.fName + "_args[] = { ";
582 const char* separator = "";
583 for (const auto& param : decl.fParameters) {
584 args += String(separator) + "GrShaderVar(\"" + param->fName + "\", " +
585 glsltype_string(fContext, param->fType) + ")";
586 separator = ", ";
587 }
588 args += "};";
589 this->addExtraEmitCodeLine(args.c_str());
590 for (const auto& s : ((Block&) *f.fBody).fStatements) {
591 this->writeStatement(*s);
592 this->writeLine();
593 }
594
595 fOut = oldOut;
596 String emit = "fragBuilder->emitFunction(";
597 emit += glsltype_string(fContext, decl.fReturnType);
598 emit += ", \"" + decl.fName + "\"";
599 emit += ", " + to_string((int64_t) decl.fParameters.size());
600 emit += ", " + decl.fName + "_args";
John Stiles50819422020-06-18 13:00:38 -0400601 emit += ",\nR\"SkSL(" + buffer.str() + ")SkSL\"";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400602 emit += ", &" + decl.fName + "_name);";
603 this->addExtraEmitCodeLine(emit.c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400604 }
605}
606
607void CPPCodeGenerator::writeSetting(const Setting& s) {
608 static constexpr const char* kPrefix = "sk_Args.";
609 if (!strncmp(s.fName.c_str(), kPrefix, strlen(kPrefix))) {
610 const char* name = s.fName.c_str() + strlen(kPrefix);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400611 this->writeRuntimeValue(s.fType, Layout(), HCodeGenerator::FieldName(name).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400612 } else {
613 this->write(s.fName.c_str());
614 }
615}
616
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400617bool CPPCodeGenerator::writeSection(const char* name, const char* prefix) {
Ethan Nicholas68990be2017-07-13 09:36:52 -0400618 const Section* s = fSectionAndParameterHelper.getSection(name);
619 if (s) {
620 this->writef("%s%s", prefix, s->fText.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400621 return true;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400622 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400623 return false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400624}
625
626void CPPCodeGenerator::writeProgramElement(const ProgramElement& p) {
627 if (p.fKind == ProgramElement::kSection_Kind) {
628 return;
629 }
630 if (p.fKind == ProgramElement::kVar_Kind) {
631 const VarDeclarations& decls = (const VarDeclarations&) p;
632 if (!decls.fVars.size()) {
633 return;
634 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000635 const Variable& var = *((VarDeclaration&) *decls.fVars[0]).fVar;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400636 if (var.fModifiers.fFlags & (Modifiers::kIn_Flag | Modifiers::kUniform_Flag) ||
637 -1 != var.fModifiers.fLayout.fBuiltin) {
638 return;
639 }
640 }
641 INHERITED::writeProgramElement(p);
642}
643
644void CPPCodeGenerator::addUniform(const Variable& var) {
645 if (!needs_uniform_var(var)) {
646 return;
647 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400648 if (var.fModifiers.fLayout.fWhen.fLength) {
649 this->writef(" if (%s) {\n ", String(var.fModifiers.fLayout.fWhen).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400650 }
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400651 const char* type = glsltype_string(fContext, var.fType);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700652 String name(var.fName);
Ethan Nicholas16464c32020-04-06 13:53:05 -0400653 this->writef(" %sVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag,"
654 " %s, \"%s\");\n", HCodeGenerator::FieldName(name.c_str()).c_str(), type,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700655 name.c_str());
Ethan Nicholasfc994162019-06-06 10:04:27 -0400656 if (var.fModifiers.fLayout.fWhen.fLength) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400657 this->write(" }\n");
658 }
659}
660
Ethan Nicholascd700e92018-08-24 16:43:57 -0400661void CPPCodeGenerator::writeInputVars() {
662}
663
Ethan Nicholas762466e2017-06-29 10:03:38 -0400664void CPPCodeGenerator::writePrivateVars() {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400665 for (const auto& p : fProgram) {
666 if (ProgramElement::kVar_Kind == p.fKind) {
667 const VarDeclarations& decls = (const VarDeclarations&) p;
668 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000669 VarDeclaration& decl = (VarDeclaration&) *raw;
670 if (is_private(*decl.fVar)) {
671 if (decl.fVar->fType == *fContext.fFragmentProcessor_Type) {
672 fErrors.error(decl.fOffset,
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400673 "fragmentProcessor variables must be declared 'in'");
674 return;
675 }
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500676 this->writef("%s %s = %s;\n",
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000677 HCodeGenerator::FieldType(fContext, decl.fVar->fType,
678 decl.fVar->fModifiers.fLayout).c_str(),
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500679 String(decl.fVar->fName).c_str(),
680 default_value(*decl.fVar).c_str());
Michael Ludwiga4275592018-08-31 10:52:47 -0400681 } else if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
682 // An auto-tracked uniform in variable, so add a field to hold onto the prior
683 // state. Note that tracked variables must be uniform in's and that is validated
684 // before writePrivateVars() is called.
685 const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *decl.fVar);
686 SkASSERT(mapper && mapper->supportsTracking());
687
688 String name = HCodeGenerator::FieldName(String(decl.fVar->fName).c_str());
689 // The member statement is different if the mapper reports a default value
690 if (mapper->defaultValue().size() > 0) {
691 this->writef("%s %sPrev = %s;\n",
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400692 Layout::CTypeToStr(mapper->ctype()), name.c_str(),
Michael Ludwiga4275592018-08-31 10:52:47 -0400693 mapper->defaultValue().c_str());
694 } else {
695 this->writef("%s %sPrev;\n",
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400696 Layout::CTypeToStr(mapper->ctype()), name.c_str());
Michael Ludwiga4275592018-08-31 10:52:47 -0400697 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400698 }
699 }
700 }
701 }
702}
703
704void CPPCodeGenerator::writePrivateVarValues() {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400705 for (const auto& p : fProgram) {
706 if (ProgramElement::kVar_Kind == p.fKind) {
707 const VarDeclarations& decls = (const VarDeclarations&) p;
708 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000709 VarDeclaration& decl = (VarDeclaration&) *raw;
710 if (is_private(*decl.fVar) && decl.fValue) {
711 this->writef("%s = ", String(decl.fVar->fName).c_str());
Ethan Nicholas82399462017-10-16 12:35:44 -0400712 fCPPMode = true;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000713 this->writeExpression(*decl.fValue, kAssignment_Precedence);
Ethan Nicholas82399462017-10-16 12:35:44 -0400714 fCPPMode = false;
715 this->write(";\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400716 }
717 }
718 }
719 }
720}
721
Ethan Nicholas82399462017-10-16 12:35:44 -0400722static bool is_accessible(const Variable& var) {
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500723 const Type& type = var.fType.nonnullable();
724 return Type::kSampler_Kind != type.kind() &&
725 Type::kOther_Kind != type.kind();
Ethan Nicholas82399462017-10-16 12:35:44 -0400726}
727
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400728void CPPCodeGenerator::newExtraEmitCodeBlock() {
729 // This should only be called when emitting SKSL for emitCode(), which can be detected if the
730 // cpp buffer is not null, and the cpp buffer is not the current output.
731 SkASSERT(fCPPBuffer && fCPPBuffer != fOut);
732
733 // Start a new block as an empty string
734 fExtraEmitCodeBlocks.push_back("");
735 // Mark its location in the output buffer, uses ${\d} for the token since ${} will not occur in
736 // valid sksl and makes detection trivial.
737 this->writef("${%zu}", fExtraEmitCodeBlocks.size() - 1);
738}
739
740void CPPCodeGenerator::addExtraEmitCodeLine(const String& toAppend) {
741 SkASSERT(fExtraEmitCodeBlocks.size() > 0);
742 String& currentBlock = fExtraEmitCodeBlocks[fExtraEmitCodeBlocks.size() - 1];
743 // Automatically add indentation and newline
744 currentBlock += " " + toAppend + "\n";
745}
746
747void CPPCodeGenerator::flushEmittedCode() {
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400748 if (fCPPBuffer == nullptr) {
749 // Not actually within writeEmitCode() so nothing to flush
750 return;
751 }
752
753 StringStream* skslBuffer = static_cast<StringStream*>(fOut);
754
755 String sksl = skslBuffer->str();
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400756 // Empty the accumulation buffer since its current contents are consumed.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400757 skslBuffer->reset();
758
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400759 // Switch to the cpp buffer
Michael Ludwigd0440192018-09-07 14:24:52 +0000760 fOut = fCPPBuffer;
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400761
762 // Iterate through the sksl, keeping track of where the last statement ended (e.g. the latest
763 // encountered ';', '{', or '}'). If an extra emit code block token is encountered then the
764 // code from 0 to last statement end is sent to writeCodeAppend, the extra code block is
765 // appended to the cpp buffer, and then the sksl string is trimmed to start where the last
766 // statement left off (minus the encountered token).
767 size_t i = 0;
768 int flushPoint = -1;
769 int tokenStart = -1;
770 while (i < sksl.size()) {
771 if (tokenStart >= 0) {
772 // Looking for the end of the token
773 if (sksl[i] == '}') {
774 // Must append the sksl from 0 to flushPoint (inclusive) then the extra code
775 // accumulated in the block with index parsed from chars [tokenStart+2, i-1]
776 String toFlush = String(sksl.c_str(), flushPoint + 1);
777 // writeCodeAppend automatically removes the format args that it consumed, so
778 // fFormatArgs will be in a valid state for any future sksl
779 this->writeCodeAppend(toFlush);
780
781 int codeBlock = stoi(String(sksl.c_str() + tokenStart + 2, i - tokenStart - 2));
782 SkASSERT(codeBlock < (int) fExtraEmitCodeBlocks.size());
783 if (fExtraEmitCodeBlocks[codeBlock].size() > 0) {
784 this->write(fExtraEmitCodeBlocks[codeBlock].c_str());
785 }
786
787 // Now reset the sksl buffer to start after the flush point, but remove the token.
788 String compacted = String(sksl.c_str() + flushPoint + 1,
789 tokenStart - flushPoint - 1);
790 if (i < sksl.size() - 1) {
791 compacted += String(sksl.c_str() + i + 1, sksl.size() - i - 1);
792 }
793 sksl = compacted;
794
795 // And reset iteration
796 i = -1;
797 flushPoint = -1;
798 tokenStart = -1;
799 }
800 } else {
801 // Looking for the start of extra emit block tokens, and tracking when statements end
802 if (sksl[i] == ';' || sksl[i] == '{' || sksl[i] == '}') {
803 flushPoint = i;
804 } else if (i < sksl.size() - 1 && sksl[i] == '$' && sksl[i + 1] == '{') {
805 // found an extra emit code block token
806 tokenStart = i++;
807 }
808 }
809 i++;
Michael Ludwigd0440192018-09-07 14:24:52 +0000810 }
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400811
812 // Once we've gone through the sksl string to this point, there are no remaining extra emit
813 // code blocks to interleave, so append the remainder as usual.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400814 this->writeCodeAppend(sksl);
815
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400816 // After appending, switch back to the emptied sksl buffer and reset the extra code blocks
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400817 fOut = skslBuffer;
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400818 fExtraEmitCodeBlocks.clear();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400819}
820
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400821void CPPCodeGenerator::writeCodeAppend(const String& code) {
John Stiles50819422020-06-18 13:00:38 -0400822 if (!code.empty()) {
823 // Count % format specifiers.
824 size_t argCount = 0;
825 for (size_t index = 0; index < code.size(); ++index) {
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400826 if ('%' == code[index]) {
John Stiles50819422020-06-18 13:00:38 -0400827 if (index == code.size() - 1) {
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400828 break;
829 }
830 if (code[index + 1] != '%') {
831 ++argCount;
832 }
833 }
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400834 }
John Stiles50819422020-06-18 13:00:38 -0400835
836 // Emit the code string.
837 this->writef(" fragBuilder->codeAppendf(\n"
838 "R\"SkSL(%s)SkSL\"\n", code.c_str());
839 for (size_t i = 0; i < argCount; ++i) {
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400840 this->writef(", %s", fFormatArgs[i].c_str());
841 }
842 this->write(");\n");
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400843
John Stiles50819422020-06-18 13:00:38 -0400844 // argCount is equal to the number of fFormatArgs that were consumed, so they should be
845 // removed from the list.
846 if (argCount > 0) {
847 fFormatArgs.erase(fFormatArgs.begin(), fFormatArgs.begin() + argCount);
848 }
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400849 }
850}
851
852String CPPCodeGenerator::convertSKSLExpressionToCPP(const Expression& e,
853 const String& cppVar) {
854 // To do this conversion, we temporarily switch the sksl output stream
855 // to an empty stringstream and reset the format args to empty.
856 OutputStream* oldSKSL = fOut;
857 StringStream exprBuffer;
858 fOut = &exprBuffer;
859
860 std::vector<String> oldArgs(fFormatArgs);
861 fFormatArgs.clear();
862
863 // Convert the argument expression into a format string and args
864 this->writeExpression(e, Precedence::kTopLevel_Precedence);
865 std::vector<String> newArgs(fFormatArgs);
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400866 String expr = exprBuffer.str();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400867
868 // After generating, restore the original output stream and format args
869 fFormatArgs = oldArgs;
870 fOut = oldSKSL;
871
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400872 // The sksl written to exprBuffer is not processed by flushEmittedCode(), so any extra emit code
873 // block tokens won't get handled. So we need to strip them from the expression and stick them
874 // to the end of the original sksl stream.
875 String exprFormat = "";
876 int tokenStart = -1;
877 for (size_t i = 0; i < expr.size(); i++) {
878 if (tokenStart >= 0) {
879 if (expr[i] == '}') {
880 // End of the token, so append the token to fOut
881 fOut->write(expr.c_str() + tokenStart, i - tokenStart + 1);
882 tokenStart = -1;
883 }
884 } else {
885 if (i < expr.size() - 1 && expr[i] == '$' && expr[i + 1] == '{') {
886 tokenStart = i++;
887 } else {
888 exprFormat += expr[i];
889 }
890 }
891 }
892
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400893 // Now build the final C++ code snippet from the format string and args
894 String cppExpr;
John Stiles50819422020-06-18 13:00:38 -0400895 if (newArgs.empty()) {
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400896 // This was a static expression, so we can simplify the input
897 // color declaration in the emitted code to just a static string
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400898 cppExpr = "SkString " + cppVar + "(\"" + exprFormat + "\");";
John Stiles50819422020-06-18 13:00:38 -0400899 } else if (newArgs.size() == 1 && exprFormat == "%s") {
900 // If the format expression is simply "%s", we can avoid an expensive call to printf.
901 // This happens fairly often in codegen so it is worth simplifying.
902 cppExpr = "SkString " + cppVar + "(" + newArgs[0] + ");";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400903 } else {
904 // String formatting must occur dynamically, so have the C++ declaration
905 // use SkStringPrintf with the format args that were accumulated
906 // when the expression was written.
907 cppExpr = "SkString " + cppVar + " = SkStringPrintf(\"" + exprFormat + "\"";
908 for (size_t i = 0; i < newArgs.size(); i++) {
909 cppExpr += ", " + newArgs[i];
910 }
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400911 cppExpr += ");";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400912 }
913 return cppExpr;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400914}
915
Ethan Nicholas762466e2017-06-29 10:03:38 -0400916bool CPPCodeGenerator::writeEmitCode(std::vector<const Variable*>& uniforms) {
917 this->write(" void emitCode(EmitArgs& args) override {\n"
918 " GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;\n");
919 this->writef(" const %s& _outer = args.fFp.cast<%s>();\n"
920 " (void) _outer;\n",
921 fFullName.c_str(), fFullName.c_str());
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400922 for (const auto& p : fProgram) {
923 if (ProgramElement::kVar_Kind == p.fKind) {
924 const VarDeclarations& decls = (const VarDeclarations&) p;
925 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000926 VarDeclaration& decl = (VarDeclaration&) *raw;
927 String nameString(decl.fVar->fName);
Ethan Nicholas82399462017-10-16 12:35:44 -0400928 const char* name = nameString.c_str();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000929 if (SectionAndParameterHelper::IsParameter(*decl.fVar) &&
930 is_accessible(*decl.fVar)) {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400931 this->writef(" auto %s = _outer.%s;\n"
Ethan Nicholas82399462017-10-16 12:35:44 -0400932 " (void) %s;\n",
933 name, name, name);
934 }
935 }
936 }
937 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400938 this->writePrivateVarValues();
939 for (const auto u : uniforms) {
940 this->addUniform(*u);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400941 }
942 this->writeSection(EMIT_CODE_SECTION);
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400943
944 // Save original buffer as the CPP buffer for flushEmittedCode()
945 fCPPBuffer = fOut;
946 StringStream skslBuffer;
947 fOut = &skslBuffer;
948
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400949 this->newExtraEmitCodeBlock();
Ethan Nicholas762466e2017-06-29 10:03:38 -0400950 bool result = INHERITED::generateCode();
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400951 this->flushEmittedCode();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400952
953 // Then restore the original CPP buffer and close the function
954 fOut = fCPPBuffer;
955 fCPPBuffer = nullptr;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400956 this->write(" }\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400957 return result;
958}
959
960void CPPCodeGenerator::writeSetData(std::vector<const Variable*>& uniforms) {
961 const char* fullName = fFullName.c_str();
Ethan Nicholas68990be2017-07-13 09:36:52 -0400962 const Section* section = fSectionAndParameterHelper.getSection(SET_DATA_SECTION);
963 const char* pdman = section ? section->fArgument.c_str() : "pdman";
Ethan Nicholas762466e2017-06-29 10:03:38 -0400964 this->writef(" void onSetData(const GrGLSLProgramDataManager& %s, "
965 "const GrFragmentProcessor& _proc) override {\n",
966 pdman);
967 bool wroteProcessor = false;
John Stiles06f3d082020-06-04 11:07:21 -0400968 for (const Variable* u : uniforms) {
Michael Ludwiga4275592018-08-31 10:52:47 -0400969 if (is_uniform_in(*u)) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400970 if (!wroteProcessor) {
971 this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName, fullName);
972 wroteProcessor = true;
973 this->writef(" {\n");
974 }
Michael Ludwiga4275592018-08-31 10:52:47 -0400975
976 const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *u);
977 SkASSERT(mapper);
978
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700979 String nameString(u->fName);
980 const char* name = nameString.c_str();
Michael Ludwiga4275592018-08-31 10:52:47 -0400981
982 // Switches for setData behavior in the generated code
983 bool conditionalUniform = u->fModifiers.fLayout.fWhen != "";
984 bool isTracked = u->fModifiers.fLayout.fFlags & Layout::kTracked_Flag;
985 bool needsValueDeclaration = isTracked || !mapper->canInlineUniformValue();
986
987 String uniformName = HCodeGenerator::FieldName(name) + "Var";
988
989 String indent = " "; // 8 by default, 12 when nested for conditional uniforms
990 if (conditionalUniform) {
991 // Add a pre-check to make sure the uniform was emitted
992 // before trying to send any data to the GPU
993 this->writef(" if (%s.isValid()) {\n", uniformName.c_str());
994 indent += " ";
995 }
996
997 String valueVar = "";
998 if (needsValueDeclaration) {
999 valueVar.appendf("%sValue", name);
1000 // Use AccessType since that will match the return type of _outer's public API.
1001 String valueType = HCodeGenerator::AccessType(fContext, u->fType,
1002 u->fModifiers.fLayout);
Ethan Nicholasbcd51e82019-04-09 10:40:41 -04001003 this->writef("%s%s %s = _outer.%s;\n",
Michael Ludwiga4275592018-08-31 10:52:47 -04001004 indent.c_str(), valueType.c_str(), valueVar.c_str(), name);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001005 } else {
Michael Ludwiga4275592018-08-31 10:52:47 -04001006 // Not tracked and the mapper only needs to use the value once
1007 // so send it a safe expression instead of the variable name
Ethan Nicholasbcd51e82019-04-09 10:40:41 -04001008 valueVar.appendf("(_outer.%s)", name);
Michael Ludwiga4275592018-08-31 10:52:47 -04001009 }
1010
1011 if (isTracked) {
1012 SkASSERT(mapper->supportsTracking());
1013
1014 String prevVar = HCodeGenerator::FieldName(name) + "Prev";
1015 this->writef("%sif (%s) {\n"
1016 "%s %s;\n"
1017 "%s %s;\n"
1018 "%s}\n", indent.c_str(),
1019 mapper->dirtyExpression(valueVar, prevVar).c_str(), indent.c_str(),
1020 mapper->saveState(valueVar, prevVar).c_str(), indent.c_str(),
1021 mapper->setUniform(pdman, uniformName, valueVar).c_str(), indent.c_str());
1022 } else {
1023 this->writef("%s%s;\n", indent.c_str(),
1024 mapper->setUniform(pdman, uniformName, valueVar).c_str());
1025 }
1026
1027 if (conditionalUniform) {
1028 // Close the earlier precheck block
1029 this->writef(" }\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -04001030 }
1031 }
1032 }
1033 if (wroteProcessor) {
1034 this->writef(" }\n");
1035 }
Ethan Nicholas68990be2017-07-13 09:36:52 -04001036 if (section) {
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05001037 int samplerIndex = 0;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001038 for (const auto& p : fProgram) {
1039 if (ProgramElement::kVar_Kind == p.fKind) {
1040 const VarDeclarations& decls = (const VarDeclarations&) p;
John Stiles06f3d082020-06-04 11:07:21 -04001041 for (const std::unique_ptr<Statement>& raw : decls.fVars) {
1042 const VarDeclaration& decl = static_cast<VarDeclaration&>(*raw);
1043 const Variable& variable = *decl.fVar;
1044 String nameString(variable.fName);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001045 const char* name = nameString.c_str();
John Stiles06f3d082020-06-04 11:07:21 -04001046 if (variable.fType.kind() == Type::kSampler_Kind) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +00001047 this->writef(" const GrSurfaceProxyView& %sView = "
1048 "_outer.textureSampler(%d).view();\n",
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05001049 name, samplerIndex);
Robert Phillipsbd99c0c2019-12-12 13:26:58 +00001050 this->writef(" GrTexture& %s = *%sView.proxy()->peekTexture();\n",
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05001051 name, name);
1052 this->writef(" (void) %s;\n", name);
1053 ++samplerIndex;
John Stiles06f3d082020-06-04 11:07:21 -04001054 } else if (needs_uniform_var(variable)) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001055 this->writef(" UniformHandle& %s = %sVar;\n"
1056 " (void) %s;\n",
1057 name, HCodeGenerator::FieldName(name).c_str(), name);
John Stiles06f3d082020-06-04 11:07:21 -04001058 } else if (SectionAndParameterHelper::IsParameter(variable) &&
1059 variable.fType != *fContext.fFragmentProcessor_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001060 if (!wroteProcessor) {
1061 this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName,
1062 fullName);
1063 wroteProcessor = true;
1064 }
John Stiles06f3d082020-06-04 11:07:21 -04001065
1066 if (variable.fType.nonnullable() != *fContext.fFragmentProcessor_Type) {
1067 this->writef(" auto %s = _outer.%s;\n"
1068 " (void) %s;\n",
1069 name, name, name);
1070 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001071 }
1072 }
1073 }
1074 }
1075 this->writeSection(SET_DATA_SECTION);
1076 }
1077 this->write(" }\n");
1078}
1079
Brian Salomonf7dcd762018-07-30 14:48:15 -04001080void CPPCodeGenerator::writeOnTextureSampler() {
1081 bool foundSampler = false;
1082 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
1083 if (param->fType.kind() == Type::kSampler_Kind) {
1084 if (!foundSampler) {
1085 this->writef(
1086 "const GrFragmentProcessor::TextureSampler& %s::onTextureSampler(int "
1087 "index) const {\n",
1088 fFullName.c_str());
1089 this->writef(" return IthTextureSampler(index, %s",
1090 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
1091 foundSampler = true;
1092 } else {
1093 this->writef(", %s",
1094 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
1095 }
1096 }
1097 }
1098 if (foundSampler) {
1099 this->write(");\n}\n");
1100 }
1101}
1102
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001103void CPPCodeGenerator::writeClone() {
1104 if (!this->writeSection(CLONE_SECTION)) {
1105 if (fSectionAndParameterHelper.getSection(FIELDS_SECTION)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001106 fErrors.error(0, "fragment processors with custom @fields must also have a custom"
1107 "@clone");
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001108 }
1109 this->writef("%s::%s(const %s& src)\n"
Ethan Nicholasabff9562017-10-09 10:54:08 -04001110 ": INHERITED(k%s_ClassID, src.optimizationFlags())", fFullName.c_str(),
1111 fFullName.c_str(), fFullName.c_str(), fFullName.c_str());
Ethan Nicholasbcd51e82019-04-09 10:40:41 -04001112 const auto transforms = fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION);
1113 for (size_t i = 0; i < transforms.size(); ++i) {
1114 const Section& s = *transforms[i];
1115 String fieldName = HCodeGenerator::CoordTransformName(s.fArgument, i);
1116 this->writef("\n, %s(src.%s)", fieldName.c_str(), fieldName.c_str());
1117 }
John Stiles06f3d082020-06-04 11:07:21 -04001118 for (const Variable* param : fSectionAndParameterHelper.getParameters()) {
Robert Phillipsbce7d862019-02-21 22:53:57 +00001119 String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str());
John Stiles88183902020-06-10 16:40:38 -04001120 if (param->fType.nonnullable() != *fContext.fFragmentProcessor_Type) {
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001121 this->writef("\n, %s(src.%s)",
1122 fieldName.c_str(),
1123 fieldName.c_str());
1124 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001125 }
Ethan Nicholasabff9562017-10-09 10:54:08 -04001126 this->writef(" {\n");
Brian Salomonf7dcd762018-07-30 14:48:15 -04001127 int samplerCount = 0;
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001128 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
1129 if (param->fType.kind() == Type::kSampler_Kind) {
Brian Salomonf7dcd762018-07-30 14:48:15 -04001130 ++samplerCount;
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001131 } else if (param->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
1132 String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str());
1133 if (param->fType.kind() == Type::kNullable_Kind) {
John Stiles88183902020-06-10 16:40:38 -04001134 this->writef(" if (src.%s_index >= 0) {\n", fieldName.c_str());
Brian Salomonb243b432020-02-20 14:41:47 -05001135 } else {
1136 this->write(" {\n");
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001137 }
John Stiles3779f442020-06-15 10:48:49 -04001138 this->writef(" %s_index = this->cloneAndRegisterChildProcessor("
1139 "src.childProcessor(src.%s_index));\n"
1140 " }\n",
1141 fieldName.c_str(), fieldName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001142 }
1143 }
Brian Salomonf7dcd762018-07-30 14:48:15 -04001144 if (samplerCount) {
1145 this->writef(" this->setTextureSamplerCnt(%d);", samplerCount);
1146 }
Ethan Nicholas929a6812018-08-06 14:56:59 -04001147 for (size_t i = 0; i < transforms.size(); ++i) {
1148 const Section& s = *transforms[i];
1149 String fieldName = HCodeGenerator::CoordTransformName(s.fArgument, i);
1150 this->writef(" this->addCoordTransform(&%s);\n", fieldName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001151 }
Michael Ludwige88320b2020-06-24 09:04:56 -04001152 if (fAccessSampleCoordsDirectly) {
1153 this->writef(" this->setUsesSampleCoordsDirectly();\n");
1154 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001155 this->write("}\n");
Brian Salomonaff329b2017-08-11 09:40:37 -04001156 this->writef("std::unique_ptr<GrFragmentProcessor> %s::clone() const {\n",
1157 fFullName.c_str());
1158 this->writef(" return std::unique_ptr<GrFragmentProcessor>(new %s(*this));\n",
1159 fFullName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001160 this->write("}\n");
1161 }
1162}
1163
Ethan Nicholas762466e2017-06-29 10:03:38 -04001164void CPPCodeGenerator::writeTest() {
Ethan Nicholas68990be2017-07-13 09:36:52 -04001165 const Section* test = fSectionAndParameterHelper.getSection(TEST_CODE_SECTION);
1166 if (test) {
Brian Salomonaff329b2017-08-11 09:40:37 -04001167 this->writef(
1168 "GR_DEFINE_FRAGMENT_PROCESSOR_TEST(%s);\n"
1169 "#if GR_TEST_UTILS\n"
1170 "std::unique_ptr<GrFragmentProcessor> %s::TestCreate(GrProcessorTestData* %s) {\n",
1171 fFullName.c_str(),
1172 fFullName.c_str(),
1173 test->fArgument.c_str());
Ethan Nicholas68990be2017-07-13 09:36:52 -04001174 this->writeSection(TEST_CODE_SECTION);
1175 this->write("}\n"
1176 "#endif\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -04001177 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001178}
1179
1180void CPPCodeGenerator::writeGetKey() {
1181 this->writef("void %s::onGetGLSLProcessorKey(const GrShaderCaps& caps, "
1182 "GrProcessorKeyBuilder* b) const {\n",
1183 fFullName.c_str());
Ethan Nicholascab767f2019-07-01 13:32:07 -04001184 for (const auto& p : fProgram) {
1185 if (ProgramElement::kVar_Kind == p.fKind) {
1186 const VarDeclarations& decls = (const VarDeclarations&) p;
1187 for (const auto& raw : decls.fVars) {
1188 const VarDeclaration& decl = (VarDeclaration&) *raw;
1189 const Variable& var = *decl.fVar;
1190 String nameString(var.fName);
1191 const char* name = nameString.c_str();
1192 if (var.fModifiers.fLayout.fKey != Layout::kNo_Key &&
1193 (var.fModifiers.fFlags & Modifiers::kUniform_Flag)) {
1194 fErrors.error(var.fOffset,
1195 "layout(key) may not be specified on uniforms");
Ethan Nicholasbcd51e82019-04-09 10:40:41 -04001196 }
Ethan Nicholascab767f2019-07-01 13:32:07 -04001197 switch (var.fModifiers.fLayout.fKey) {
1198 case Layout::kKey_Key:
1199 if (is_private(var)) {
1200 this->writef("%s %s =",
1201 HCodeGenerator::FieldType(fContext, var.fType,
1202 var.fModifiers.fLayout).c_str(),
1203 String(var.fName).c_str());
1204 if (decl.fValue) {
1205 fCPPMode = true;
1206 this->writeExpression(*decl.fValue, kAssignment_Precedence);
1207 fCPPMode = false;
1208 } else {
1209 this->writef("%s", default_value(var).c_str());
1210 }
1211 this->write(";\n");
1212 }
1213 if (var.fModifiers.fLayout.fWhen.fLength) {
1214 this->writef("if (%s) {", String(var.fModifiers.fLayout.fWhen).c_str());
1215 }
1216 if (var.fType == *fContext.fFloat4x4_Type) {
1217 ABORT("no automatic key handling for float4x4\n");
1218 } else if (var.fType == *fContext.fFloat2_Type) {
1219 this->writef(" b->add32(%s.fX);\n",
1220 HCodeGenerator::FieldName(name).c_str());
1221 this->writef(" b->add32(%s.fY);\n",
1222 HCodeGenerator::FieldName(name).c_str());
1223 } else if (var.fType == *fContext.fFloat4_Type) {
1224 this->writef(" b->add32(%s.x());\n",
1225 HCodeGenerator::FieldName(name).c_str());
1226 this->writef(" b->add32(%s.y());\n",
1227 HCodeGenerator::FieldName(name).c_str());
1228 this->writef(" b->add32(%s.width());\n",
1229 HCodeGenerator::FieldName(name).c_str());
1230 this->writef(" b->add32(%s.height());\n",
1231 HCodeGenerator::FieldName(name).c_str());
1232 } else if (var.fType == *fContext.fHalf4_Type) {
1233 this->writef(" uint16_t red = SkFloatToHalf(%s.fR);\n",
1234 HCodeGenerator::FieldName(name).c_str());
1235 this->writef(" uint16_t green = SkFloatToHalf(%s.fG);\n",
1236 HCodeGenerator::FieldName(name).c_str());
1237 this->writef(" uint16_t blue = SkFloatToHalf(%s.fB);\n",
1238 HCodeGenerator::FieldName(name).c_str());
1239 this->writef(" uint16_t alpha = SkFloatToHalf(%s.fA);\n",
1240 HCodeGenerator::FieldName(name).c_str());
1241 this->write(" b->add32(((uint32_t)red << 16) | green);\n");
1242 this->write(" b->add32(((uint32_t)blue << 16) | alpha);\n");
1243 } else {
1244 this->writef(" b->add32((int32_t) %s);\n",
1245 HCodeGenerator::FieldName(name).c_str());
1246 }
1247 if (var.fModifiers.fLayout.fWhen.fLength) {
1248 this->write("}");
1249 }
1250 break;
1251 case Layout::kIdentity_Key:
1252 if (var.fType.kind() != Type::kMatrix_Kind) {
1253 fErrors.error(var.fOffset,
1254 "layout(key=identity) requires matrix type");
1255 }
1256 this->writef(" b->add32(%s.isIdentity() ? 1 : 0);\n",
1257 HCodeGenerator::FieldName(name).c_str());
1258 break;
1259 case Layout::kNo_Key:
1260 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001261 }
Ethan Nicholascab767f2019-07-01 13:32:07 -04001262 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001263 }
1264 }
1265 this->write("}\n");
1266}
1267
1268bool CPPCodeGenerator::generateCode() {
1269 std::vector<const Variable*> uniforms;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001270 for (const auto& p : fProgram) {
1271 if (ProgramElement::kVar_Kind == p.fKind) {
1272 const VarDeclarations& decls = (const VarDeclarations&) p;
1273 for (const auto& raw : decls.fVars) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001274 VarDeclaration& decl = (VarDeclaration&) *raw;
1275 if ((decl.fVar->fModifiers.fFlags & Modifiers::kUniform_Flag) &&
1276 decl.fVar->fType.kind() != Type::kSampler_Kind) {
1277 uniforms.push_back(decl.fVar);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001278 }
Michael Ludwiga4275592018-08-31 10:52:47 -04001279
1280 if (is_uniform_in(*decl.fVar)) {
1281 // Validate the "uniform in" declarations to make sure they are fully supported,
1282 // instead of generating surprising C++
1283 const UniformCTypeMapper* mapper =
1284 UniformCTypeMapper::Get(fContext, *decl.fVar);
1285 if (mapper == nullptr) {
1286 fErrors.error(decl.fOffset, String(decl.fVar->fName)
1287 + "'s type is not supported for use as a 'uniform in'");
1288 return false;
1289 }
1290 if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
1291 if (!mapper->supportsTracking()) {
1292 fErrors.error(decl.fOffset, String(decl.fVar->fName)
1293 + "'s type does not support state tracking");
1294 return false;
1295 }
1296 }
1297
1298 } else {
1299 // If it's not a uniform_in, it's an error to be tracked
1300 if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
1301 fErrors.error(decl.fOffset, "Non-'in uniforms' cannot be tracked");
1302 return false;
1303 }
1304 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001305 }
1306 }
1307 }
1308 const char* baseName = fName.c_str();
1309 const char* fullName = fFullName.c_str();
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05001310 this->writef("%s\n", HCodeGenerator::GetHeader(fProgram, fErrors).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001311 this->writef(kFragmentProcessorHeader, fullName);
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001312 this->writef("#include \"%s.h\"\n\n", fullName);
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04001313 this->writeSection(CPP_SECTION);
Greg Daniel456f9b52020-03-05 19:14:18 +00001314 this->writef("#include \"src/gpu/GrTexture.h\"\n"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001315 "#include \"src/gpu/glsl/GrGLSLFragmentProcessor.h\"\n"
1316 "#include \"src/gpu/glsl/GrGLSLFragmentShaderBuilder.h\"\n"
1317 "#include \"src/gpu/glsl/GrGLSLProgramBuilder.h\"\n"
1318 "#include \"src/sksl/SkSLCPP.h\"\n"
1319 "#include \"src/sksl/SkSLUtil.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -04001320 "class GrGLSL%s : public GrGLSLFragmentProcessor {\n"
1321 "public:\n"
1322 " GrGLSL%s() {}\n",
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04001323 baseName, baseName);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001324 bool result = this->writeEmitCode(uniforms);
1325 this->write("private:\n");
1326 this->writeSetData(uniforms);
1327 this->writePrivateVars();
1328 for (const auto& u : uniforms) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001329 if (needs_uniform_var(*u) && !(u->fModifiers.fFlags & Modifiers::kIn_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001330 this->writef(" UniformHandle %sVar;\n",
1331 HCodeGenerator::FieldName(String(u->fName).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001332 }
1333 }
Ethan Nicholas68990be2017-07-13 09:36:52 -04001334 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001335 if (needs_uniform_var(*param)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001336 this->writef(" UniformHandle %sVar;\n",
1337 HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001338 }
1339 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001340 this->writef("};\n"
1341 "GrGLSLFragmentProcessor* %s::onCreateGLSLInstance() const {\n"
1342 " return new GrGLSL%s();\n"
1343 "}\n",
1344 fullName, baseName);
1345 this->writeGetKey();
1346 this->writef("bool %s::onIsEqual(const GrFragmentProcessor& other) const {\n"
1347 " const %s& that = other.cast<%s>();\n"
1348 " (void) that;\n",
1349 fullName, fullName, fullName);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001350 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001351 if (param->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -04001352 continue;
1353 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001354 String nameString(param->fName);
1355 const char* name = nameString.c_str();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001356 this->writef(" if (%s != that.%s) return false;\n",
1357 HCodeGenerator::FieldName(name).c_str(),
1358 HCodeGenerator::FieldName(name).c_str());
1359 }
1360 this->write(" return true;\n"
1361 "}\n");
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001362 this->writeClone();
Brian Salomonf7dcd762018-07-30 14:48:15 -04001363 this->writeOnTextureSampler();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001364 this->writeTest();
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04001365 this->writeSection(CPP_END_SECTION);
Greg Daniel3e8c3452018-04-06 10:37:55 -04001366
Ethan Nicholas762466e2017-06-29 10:03:38 -04001367 result &= 0 == fErrors.errorCount();
1368 return result;
1369}
1370
1371} // namespace