blob: 412f0f47661fe427b67927f24f8192adfcf0c8f4 [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
Greg Daniela28ea672020-09-25 11:12:56 -040018#if defined(SKSL_STANDALONE) || GR_TEST_UTILS
Ethan Nicholas2a479a52020-08-18 16:29:45 -040019
Ethan Nicholas762466e2017-06-29 10:03:38 -040020namespace SkSL {
21
22static bool needs_uniform_var(const Variable& var) {
Ethan Nicholas5f9836e2017-12-20 15:16:33 -050023 return (var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
Ethan Nicholas30d30222020-09-11 12:27:26 -040024 var.type().typeKind() != Type::TypeKind::kSampler;
Ethan Nicholas762466e2017-06-29 10:03:38 -040025}
26
27CPPCodeGenerator::CPPCodeGenerator(const Context* context, const Program* program,
28 ErrorReporter* errors, String name, OutputStream* out)
John Stiles50819422020-06-18 13:00:38 -040029 : INHERITED(context, program, errors, out)
30 , fName(std::move(name))
31 , fFullName(String::printf("Gr%s", fName.c_str()))
32 , fSectionAndParameterHelper(program, *errors) {
33 fLineEnding = "\n";
Ethan Nicholas13863662019-07-29 13:05:15 -040034 fTextureFunctionOverride = "sample";
Ethan Nicholas762466e2017-06-29 10:03:38 -040035}
36
37void CPPCodeGenerator::writef(const char* s, va_list va) {
38 static constexpr int BUFFER_SIZE = 1024;
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040039 va_list copy;
40 va_copy(copy, va);
Ethan Nicholas762466e2017-06-29 10:03:38 -040041 char buffer[BUFFER_SIZE];
John Stiles50819422020-06-18 13:00:38 -040042 int length = std::vsnprintf(buffer, BUFFER_SIZE, s, va);
Ethan Nicholas762466e2017-06-29 10:03:38 -040043 if (length < BUFFER_SIZE) {
44 fOut->write(buffer, length);
45 } else {
46 std::unique_ptr<char[]> heap(new char[length + 1]);
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040047 vsprintf(heap.get(), s, copy);
Ethan Nicholas762466e2017-06-29 10:03:38 -040048 fOut->write(heap.get(), length);
49 }
z102.zhangd74f2c82018-08-10 09:08:47 +080050 va_end(copy);
Ethan Nicholas762466e2017-06-29 10:03:38 -040051}
52
53void CPPCodeGenerator::writef(const char* s, ...) {
54 va_list va;
55 va_start(va, s);
56 this->writef(s, va);
57 va_end(va);
58}
59
60void CPPCodeGenerator::writeHeader() {
61}
62
Ethan Nicholasf7b88202017-09-18 14:10:39 -040063bool CPPCodeGenerator::usesPrecisionModifiers() const {
64 return false;
Ethan Nicholas762466e2017-06-29 10:03:38 -040065}
66
Ethan Nicholasf7b88202017-09-18 14:10:39 -040067String CPPCodeGenerator::getTypeName(const Type& type) {
68 return type.name();
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040069}
Ethan Nicholasf7b88202017-09-18 14:10:39 -040070
Ethan Nicholas762466e2017-06-29 10:03:38 -040071void CPPCodeGenerator::writeBinaryExpression(const BinaryExpression& b,
72 Precedence parentPrecedence) {
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -040073 const Expression& left = b.left();
74 const Expression& right = b.right();
75 Token::Kind op = b.getOperator();
76 if (op == Token::Kind::TK_PERCENT) {
Ethan Nicholas762466e2017-06-29 10:03:38 -040077 // need to use "%%" instead of "%" b/c the code will be inside of a printf
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -040078 Precedence precedence = GetBinaryPrecedence(op);
Ethan Nicholas762466e2017-06-29 10:03:38 -040079 if (precedence >= parentPrecedence) {
80 this->write("(");
81 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -040082 this->writeExpression(left, precedence);
Ethan Nicholas762466e2017-06-29 10:03:38 -040083 this->write(" %% ");
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -040084 this->writeExpression(right, precedence);
Ethan Nicholas762466e2017-06-29 10:03:38 -040085 if (precedence >= parentPrecedence) {
86 this->write(")");
87 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -040088 } else if (left.kind() == Expression::Kind::kNullLiteral ||
89 right.kind() == Expression::Kind::kNullLiteral) {
Ethan Nicholasee1c8a72019-02-22 10:50:47 -050090 const Variable* var;
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -040091 if (left.kind() != Expression::Kind::kNullLiteral) {
Brian Osman79457ef2020-09-24 15:01:27 -040092 var = left.as<VariableReference>().fVariable;
Ethan Nicholasee1c8a72019-02-22 10:50:47 -050093 } else {
Brian Osman79457ef2020-09-24 15:01:27 -040094 var = right.as<VariableReference>().fVariable;
Ethan Nicholasee1c8a72019-02-22 10:50:47 -050095 }
Ethan Nicholas30d30222020-09-11 12:27:26 -040096 SkASSERT(var->type().typeKind() == Type::TypeKind::kNullable &&
97 var->type().componentType() == *fContext.fFragmentProcessor_Type);
Ethan Nicholasee1c8a72019-02-22 10:50:47 -050098 this->write("%s");
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -040099 const char* prefix = "";
100 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400101 case Token::Kind::TK_EQEQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400102 prefix = "!";
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500103 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400104 case Token::Kind::TK_NEQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400105 prefix = "";
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500106 break;
107 default:
108 SkASSERT(false);
109 }
Brian Osman12c5d292020-07-13 16:11:35 -0400110 int childIndex = this->getChildFPIndex(*var);
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400111 fFormatArgs.push_back(String(prefix) + "_outer.childProcessor(" + to_string(childIndex) +
Brian Osman12c5d292020-07-13 16:11:35 -0400112 ") ? \"true\" : \"false\"");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400113 } else {
114 INHERITED::writeBinaryExpression(b, parentPrecedence);
115 }
116}
117
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400118static String default_value(const Type& type) {
Ethan Nicholas556b8be2020-10-02 17:08:55 -0400119 if (type.name() == "bool") {
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500120 return "false";
121 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400122 switch (type.typeKind()) {
123 case Type::TypeKind::kScalar: return "0";
124 case Type::TypeKind::kVector: return type.name() + "(0)";
125 case Type::TypeKind::kMatrix: return type.name() + "(1)";
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400126 default: ABORT("unsupported default_value type\n");
127 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400128}
129
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500130static String default_value(const Variable& var) {
Brian Osman495993a2018-10-16 15:45:55 -0400131 if (var.fModifiers.fLayout.fCType == SkSL::Layout::CType::kSkPMColor4f) {
Brian Osmanf28e55d2018-10-03 16:35:54 -0400132 return "{SK_FloatNaN, SK_FloatNaN, SK_FloatNaN, SK_FloatNaN}";
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500133 }
Ethan Nicholas30d30222020-09-11 12:27:26 -0400134 return default_value(var.type());
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500135}
136
Ethan Nicholas762466e2017-06-29 10:03:38 -0400137static bool is_private(const Variable& var) {
138 return !(var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
139 !(var.fModifiers.fFlags & Modifiers::kIn_Flag) &&
140 var.fStorage == Variable::kGlobal_Storage &&
141 var.fModifiers.fLayout.fBuiltin == -1;
142}
143
Michael Ludwiga4275592018-08-31 10:52:47 -0400144static bool is_uniform_in(const Variable& var) {
145 return (var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
146 (var.fModifiers.fFlags & Modifiers::kIn_Flag) &&
Ethan Nicholas30d30222020-09-11 12:27:26 -0400147 var.type().typeKind() != Type::TypeKind::kSampler;
Michael Ludwiga4275592018-08-31 10:52:47 -0400148}
149
John Stiles47b4e222020-08-12 09:56:50 -0400150String CPPCodeGenerator::formatRuntimeValue(const Type& type,
151 const Layout& layout,
152 const String& cppCode,
153 std::vector<String>* formatArgs) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400154 if (type.typeKind() == Type::TypeKind::kArray) {
Ethan Nicholas7018bcf2020-08-20 15:57:22 -0400155 String result("[");
156 const char* separator = "";
157 for (int i = 0; i < type.columns(); i++) {
158 result += separator + this->formatRuntimeValue(type.componentType(), layout,
159 "(" + cppCode + ")[" + to_string(i) +
160 "]", formatArgs);
161 separator = ",";
162 }
163 result += "]";
164 return result;
165 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400166 if (type.isFloat()) {
John Stiles47b4e222020-08-12 09:56:50 -0400167 formatArgs->push_back(cppCode);
168 return "%f";
169 }
170 if (type == *fContext.fInt_Type) {
171 formatArgs->push_back(cppCode);
172 return "%d";
173 }
174 if (type == *fContext.fBool_Type) {
175 formatArgs->push_back("(" + cppCode + " ? \"true\" : \"false\")");
176 return "%s";
177 }
178 if (type == *fContext.fFloat2_Type || type == *fContext.fHalf2_Type) {
179 formatArgs->push_back(cppCode + ".fX");
180 formatArgs->push_back(cppCode + ".fY");
181 return type.name() + "(%f, %f)";
182 }
183 if (type == *fContext.fFloat3_Type || type == *fContext.fHalf3_Type) {
184 formatArgs->push_back(cppCode + ".fX");
185 formatArgs->push_back(cppCode + ".fY");
186 formatArgs->push_back(cppCode + ".fZ");
187 return type.name() + "(%f, %f, %f)";
188 }
189 if (type == *fContext.fFloat4_Type || type == *fContext.fHalf4_Type) {
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400190 switch (layout.fCType) {
191 case Layout::CType::kSkPMColor:
John Stiles47b4e222020-08-12 09:56:50 -0400192 formatArgs->push_back("SkGetPackedR32(" + cppCode + ") / 255.0");
193 formatArgs->push_back("SkGetPackedG32(" + cppCode + ") / 255.0");
194 formatArgs->push_back("SkGetPackedB32(" + cppCode + ") / 255.0");
195 formatArgs->push_back("SkGetPackedA32(" + cppCode + ") / 255.0");
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400196 break;
Brian Osmanf28e55d2018-10-03 16:35:54 -0400197 case Layout::CType::kSkPMColor4f:
John Stiles47b4e222020-08-12 09:56:50 -0400198 formatArgs->push_back(cppCode + ".fR");
199 formatArgs->push_back(cppCode + ".fG");
200 formatArgs->push_back(cppCode + ".fB");
201 formatArgs->push_back(cppCode + ".fA");
Brian Osmanf28e55d2018-10-03 16:35:54 -0400202 break;
Mike Reedb26b4e72020-01-22 14:31:21 -0500203 case Layout::CType::kSkV4:
John Stiles47b4e222020-08-12 09:56:50 -0400204 formatArgs->push_back(cppCode + ".x");
205 formatArgs->push_back(cppCode + ".y");
206 formatArgs->push_back(cppCode + ".z");
207 formatArgs->push_back(cppCode + ".w");
Brian Salomoneca66b32019-06-01 11:18:15 -0400208 break;
John Stiles47b4e222020-08-12 09:56:50 -0400209 case Layout::CType::kSkRect:
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400210 case Layout::CType::kDefault:
John Stiles47b4e222020-08-12 09:56:50 -0400211 formatArgs->push_back(cppCode + ".left()");
212 formatArgs->push_back(cppCode + ".top()");
213 formatArgs->push_back(cppCode + ".right()");
214 formatArgs->push_back(cppCode + ".bottom()");
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400215 break;
216 default:
217 SkASSERT(false);
Ethan Nicholasd608c092017-10-26 09:30:08 -0400218 }
John Stiles47b4e222020-08-12 09:56:50 -0400219 return type.name() + "(%f, %f, %f, %f)";
Ethan Nicholas762466e2017-06-29 10:03:38 -0400220 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400221 if (type.typeKind() == Type::TypeKind::kMatrix) {
John Stiles47b4e222020-08-12 09:56:50 -0400222 SkASSERT(type.componentType() == *fContext.fFloat_Type ||
223 type.componentType() == *fContext.fHalf_Type);
224
225 String format = type.name() + "(";
226 for (int c = 0; c < type.columns(); ++c) {
227 for (int r = 0; r < type.rows(); ++r) {
228 formatArgs->push_back(String::printf("%s.rc(%d, %d)", cppCode.c_str(), r, c));
229 format += "%f, ";
230 }
231 }
232
233 // Replace trailing ", " with ")".
234 format.pop_back();
235 format.back() = ')';
236 return format;
237 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400238 if (type.typeKind() == Type::TypeKind::kEnum) {
John Stiles47b4e222020-08-12 09:56:50 -0400239 formatArgs->push_back("(int) " + cppCode);
240 return "%d";
241 }
242 if (type == *fContext.fInt4_Type ||
243 type == *fContext.fShort4_Type ||
244 type == *fContext.fByte4_Type) {
245 formatArgs->push_back(cppCode + ".left()");
246 formatArgs->push_back(cppCode + ".top()");
247 formatArgs->push_back(cppCode + ".right()");
248 formatArgs->push_back(cppCode + ".bottom()");
249 return type.name() + "(%d, %d, %d, %d)";
250 }
251
Ethan Nicholas556b8be2020-10-02 17:08:55 -0400252 SkDEBUGFAILF("unsupported runtime value type '%s'\n", String(type.name()).c_str());
John Stiles47b4e222020-08-12 09:56:50 -0400253 return "";
254}
255
256void CPPCodeGenerator::writeRuntimeValue(const Type& type, const Layout& layout,
257 const String& cppCode) {
258 this->write(this->formatRuntimeValue(type, layout, cppCode, &fFormatArgs));
Ethan Nicholas762466e2017-06-29 10:03:38 -0400259}
260
261void CPPCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) {
262 if (is_private(var)) {
Ethan Nicholas556b8be2020-10-02 17:08:55 -0400263 this->writeRuntimeValue(var.type(), var.fModifiers.fLayout, var.name());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400264 } else {
265 this->writeExpression(value, kTopLevel_Precedence);
266 }
267}
268
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400269String CPPCodeGenerator::getSamplerHandle(const Variable& var) {
270 int samplerCount = 0;
Ethan Nicholas68990be2017-07-13 09:36:52 -0400271 for (const auto param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400272 if (&var == param) {
273 return "args.fTexSamplers[" + to_string(samplerCount) + "]";
274 }
Ethan Nicholas30d30222020-09-11 12:27:26 -0400275 if (param->type().typeKind() == Type::TypeKind::kSampler) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400276 ++samplerCount;
277 }
278 }
279 ABORT("should have found sampler in parameters\n");
280}
281
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400282void CPPCodeGenerator::writeIntLiteral(const IntLiteral& i) {
Ethan Nicholase96cdd12020-09-28 16:27:18 -0400283 this->write(to_string((int32_t) i.value()));
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400284}
285
Ethan Nicholas82399462017-10-16 12:35:44 -0400286void CPPCodeGenerator::writeSwizzle(const Swizzle& swizzle) {
287 if (fCPPMode) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400288 SkASSERT(swizzle.fComponents.size() == 1); // no support for multiple swizzle components yet
Ethan Nicholas82399462017-10-16 12:35:44 -0400289 this->writeExpression(*swizzle.fBase, kPostfix_Precedence);
290 switch (swizzle.fComponents[0]) {
291 case 0: this->write(".left()"); break;
292 case 1: this->write(".top()"); break;
293 case 2: this->write(".right()"); break;
294 case 3: this->write(".bottom()"); break;
295 }
296 } else {
297 INHERITED::writeSwizzle(swizzle);
298 }
299}
300
John Stiles735a5a72020-08-26 10:21:10 -0400301void CPPCodeGenerator::setReturnType(int offset, ReturnType typeToSet) {
302 if (fReturnType == ReturnType::kNothing) {
303 fReturnType = typeToSet;
304 } else if (fReturnType != typeToSet) {
305 fErrors.error(offset,
306 "Fragment processors must not mix sk_OutColor and return statements\n");
307 }
308}
309
Ethan Nicholas762466e2017-06-29 10:03:38 -0400310void CPPCodeGenerator::writeVariableReference(const VariableReference& ref) {
Ethan Nicholas82399462017-10-16 12:35:44 -0400311 if (fCPPMode) {
Ethan Nicholas556b8be2020-10-02 17:08:55 -0400312 this->write(ref.fVariable->name());
Ethan Nicholas82399462017-10-16 12:35:44 -0400313 return;
314 }
Brian Osman79457ef2020-09-24 15:01:27 -0400315 switch (ref.fVariable->fModifiers.fLayout.fBuiltin) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400316 case SK_OUTCOLOR_BUILTIN:
317 this->write("%s");
318 fFormatArgs.push_back(String("args.fOutputColor"));
John Stiles735a5a72020-08-26 10:21:10 -0400319 this->setReturnType(ref.fOffset, ReturnType::kUsesSkOutColor);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400320 break;
Michael Ludwigfc2fdf02020-06-29 17:20:13 -0400321 case SK_MAIN_COORDS_BUILTIN:
322 this->write("%s");
323 fFormatArgs.push_back(String("args.fSampleCoord"));
324 fAccessSampleCoordsDirectly = true;
325 break;
Ethan Nicholascd700e92018-08-24 16:43:57 -0400326 case SK_WIDTH_BUILTIN:
327 this->write("sk_Width");
328 break;
329 case SK_HEIGHT_BUILTIN:
330 this->write("sk_Height");
331 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400332 default:
Brian Osman79457ef2020-09-24 15:01:27 -0400333 if (ref.fVariable->type().typeKind() == Type::TypeKind::kSampler) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400334 this->write("%s");
335 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerVariable(" +
Brian Osman79457ef2020-09-24 15:01:27 -0400336 this->getSamplerHandle(*ref.fVariable) + ")");
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400337 return;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400338 }
Brian Osman79457ef2020-09-24 15:01:27 -0400339 if (ref.fVariable->fModifiers.fFlags & Modifiers::kUniform_Flag) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400340 this->write("%s");
Ethan Nicholas556b8be2020-10-02 17:08:55 -0400341 String name = ref.fVariable->name();
Brian Osman1cb41712017-10-19 12:54:52 -0400342 String var = String::printf("args.fUniformHandler->getUniformCStr(%sVar)",
343 HCodeGenerator::FieldName(name.c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400344 String code;
Brian Osman79457ef2020-09-24 15:01:27 -0400345 if (ref.fVariable->fModifiers.fLayout.fWhen.fLength) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400346 code = String::printf("%sVar.isValid() ? %s : \"%s\"",
347 HCodeGenerator::FieldName(name.c_str()).c_str(),
348 var.c_str(),
Brian Osman79457ef2020-09-24 15:01:27 -0400349 default_value(ref.fVariable->type()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400350 } else {
351 code = var;
352 }
353 fFormatArgs.push_back(code);
Brian Osman79457ef2020-09-24 15:01:27 -0400354 } else if (SectionAndParameterHelper::IsParameter(*ref.fVariable)) {
Ethan Nicholas556b8be2020-10-02 17:08:55 -0400355 String name(ref.fVariable->name());
Brian Osman79457ef2020-09-24 15:01:27 -0400356 this->writeRuntimeValue(ref.fVariable->type(), ref.fVariable->fModifiers.fLayout,
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400357 String::printf("_outer.%s", name.c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400358 } else {
Ethan Nicholas556b8be2020-10-02 17:08:55 -0400359 this->write(ref.fVariable->name());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400360 }
361 }
362}
363
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400364void CPPCodeGenerator::writeIfStatement(const IfStatement& s) {
365 if (s.fIsStatic) {
366 this->write("@");
367 }
368 INHERITED::writeIfStatement(s);
369}
370
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400371void CPPCodeGenerator::writeReturnStatement(const ReturnStatement& s) {
372 if (fInMain) {
John Stiles735a5a72020-08-26 10:21:10 -0400373 this->setReturnType(s.fOffset, ReturnType::kUsesExplicitReturn);
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400374 }
375 INHERITED::writeReturnStatement(s);
376}
377
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400378void CPPCodeGenerator::writeSwitchStatement(const SwitchStatement& s) {
379 if (s.fIsStatic) {
380 this->write("@");
381 }
382 INHERITED::writeSwitchStatement(s);
383}
384
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400385void CPPCodeGenerator::writeFieldAccess(const FieldAccess& access) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400386 if (access.fBase->type().name() == "fragmentProcessor") {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400387 // Special field access on fragment processors are converted into function calls on
388 // GrFragmentProcessor's getters.
Brian Osman79457ef2020-09-24 15:01:27 -0400389 if (!access.fBase->is<VariableReference>()) {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400390 fErrors.error(access.fBase->fOffset, "fragmentProcessor must be a reference\n");
391 return;
392 }
393
394 const Type::Field& field = fContext.fFragmentProcessor_Type->fields()[access.fFieldIndex];
Brian Osman79457ef2020-09-24 15:01:27 -0400395 const Variable& var = *access.fBase->as<VariableReference>().fVariable;
Brian Osman12c5d292020-07-13 16:11:35 -0400396 String cppAccess = String::printf("_outer.childProcessor(%d)->%s()",
397 this->getChildFPIndex(var),
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500398 String(field.fName).c_str());
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400399
400 if (fCPPMode) {
401 this->write(cppAccess.c_str());
402 } else {
403 writeRuntimeValue(*field.fType, Layout(), cppAccess);
404 }
405 return;
406 }
407 INHERITED::writeFieldAccess(access);
408}
409
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500410int CPPCodeGenerator::getChildFPIndex(const Variable& var) const {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400411 int index = 0;
412 bool found = false;
413 for (const auto& p : fProgram) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400414 if (p.kind() == ProgramElement::Kind::kVar) {
John Stiles3dc0da62020-08-19 17:48:31 -0400415 const VarDeclarations& decls = p.as<VarDeclarations>();
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400416 for (const auto& raw : decls.fVars) {
John Stiles3dc0da62020-08-19 17:48:31 -0400417 const VarDeclaration& decl = raw->as<VarDeclaration>();
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500418 if (decl.fVar == &var) {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400419 found = true;
Ethan Nicholas30d30222020-09-11 12:27:26 -0400420 } else if (decl.fVar->type().nonnullable() == *fContext.fFragmentProcessor_Type) {
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400421 ++index;
422 }
423 }
424 }
425 if (found) {
426 break;
427 }
428 }
429 SkASSERT(found);
430 return index;
431}
432
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400433void CPPCodeGenerator::writeFunctionCall(const FunctionCall& c) {
Ethan Nicholas556b8be2020-10-02 17:08:55 -0400434 if (c.fFunction.fBuiltin && c.fFunction.name() == "sample" &&
Ethan Nicholas30d30222020-09-11 12:27:26 -0400435 c.fArguments[0]->type().typeKind() != Type::TypeKind::kSampler) {
Leon Scroggins III982fff22020-07-31 14:09:06 -0400436 // Validity checks that are detected by function definition in sksl_fp.inc
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400437 SkASSERT(c.fArguments.size() >= 1 && c.fArguments.size() <= 3);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400438 SkASSERT("fragmentProcessor" == c.fArguments[0]->type().name() ||
439 "fragmentProcessor?" == c.fArguments[0]->type().name());
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400440
441 // Actually fail during compilation if arguments with valid types are
Ethan Nicholas13863662019-07-29 13:05:15 -0400442 // provided that are not variable references, since sample() is a
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400443 // special function that impacts code emission.
Brian Osman79457ef2020-09-24 15:01:27 -0400444 if (!c.fArguments[0]->is<VariableReference>()) {
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400445 fErrors.error(c.fArguments[0]->fOffset,
Ethan Nicholas13863662019-07-29 13:05:15 -0400446 "sample()'s fragmentProcessor argument must be a variable reference\n");
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400447 return;
448 }
Brian Osman79457ef2020-09-24 15:01:27 -0400449 const Variable& child = *c.fArguments[0]->as<VariableReference>().fVariable;
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400450
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400451 // Start a new extra emit code section so that the emitted child processor can depend on
452 // sksl variables defined in earlier sksl code.
453 this->newExtraEmitCodeBlock();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400454
Michael Ludwige88320b2020-06-24 09:04:56 -0400455 String inputColor;
Ethan Nicholas30d30222020-09-11 12:27:26 -0400456 if (c.fArguments.size() > 1 && c.fArguments[1]->type().name() == "half4") {
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400457 // Use the invokeChild() variant that accepts an input color, so convert the 2nd
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400458 // argument's expression into C++ code that produces sksl stored in an SkString.
Brian Osman12c5d292020-07-13 16:11:35 -0400459 String inputColorName = "_input" + to_string(c.fOffset);
John Stilesd060c9d2020-06-08 11:44:25 -0400460 addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments[1], inputColorName));
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400461
Michael Ludwige88320b2020-06-24 09:04:56 -0400462 // invokeChild() needs a char* and a pre-pended comma
463 inputColor = ", " + inputColorName + ".c_str()";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400464 }
465
Michael Ludwige88320b2020-06-24 09:04:56 -0400466 String inputCoord;
467 String invokeFunction = "invokeChild";
Ethan Nicholas30d30222020-09-11 12:27:26 -0400468 if (c.fArguments.back()->type().name() == "float2") {
Michael Ludwige88320b2020-06-24 09:04:56 -0400469 // Invoking child with explicit coordinates at this call site
470 inputCoord = "_coords" + to_string(c.fOffset);
471 addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments.back(), inputCoord));
472 inputCoord.append(".c_str()");
Ethan Nicholas30d30222020-09-11 12:27:26 -0400473 } else if (c.fArguments.back()->type().name() == "float3x3") {
Michael Ludwige88320b2020-06-24 09:04:56 -0400474 // Invoking child with a matrix, sampling relative to the input coords.
475 invokeFunction = "invokeChildWithMatrix";
Brian Osman1298bc42020-06-30 13:39:35 -0400476 SampleUsage usage = Analysis::GetSampleUsage(fProgram, child);
Michael Ludwige88320b2020-06-24 09:04:56 -0400477
Brian Osman1298bc42020-06-30 13:39:35 -0400478 if (!usage.hasUniformMatrix()) {
Michael Ludwige88320b2020-06-24 09:04:56 -0400479 inputCoord = "_matrix" + to_string(c.fOffset);
480 addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments.back(), inputCoord));
481 inputCoord.append(".c_str()");
482 }
483 // else pass in the empty string to rely on invokeChildWithMatrix's automatic uniform
484 // resolution
485 }
486 if (!inputCoord.empty()) {
487 inputCoord = ", " + inputCoord;
488 }
489
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400490 // Write the output handling after the possible input handling
Ethan Nicholas13863662019-07-29 13:05:15 -0400491 String childName = "_sample" + to_string(c.fOffset);
Brian Osman12c5d292020-07-13 16:11:35 -0400492 String childIndexStr = to_string(this->getChildFPIndex(child));
493 addExtraEmitCodeLine("SkString " + childName + " = this->" + invokeFunction + "(" +
494 childIndexStr + inputColor + ", args" + inputCoord + ");");
John Stiles50819422020-06-18 13:00:38 -0400495
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000496 this->write("%s");
497 fFormatArgs.push_back(childName + ".c_str()");
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400498 return;
499 }
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400500 if (c.fFunction.fBuiltin) {
501 INHERITED::writeFunctionCall(c);
502 } else {
503 this->write("%s");
Ethan Nicholas556b8be2020-10-02 17:08:55 -0400504 fFormatArgs.push_back((String(c.fFunction.name()) + "_name.c_str()").c_str());
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400505 this->write("(");
506 const char* separator = "";
507 for (const auto& arg : c.fArguments) {
508 this->write(separator);
509 separator = ", ";
510 this->writeExpression(*arg, kSequence_Precedence);
511 }
512 this->write(")");
513 }
Ethan Nicholas556b8be2020-10-02 17:08:55 -0400514 if (c.fFunction.fBuiltin && c.fFunction.name() == "sample") {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400515 this->write(".%s");
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400516 SkASSERT(c.fArguments.size() >= 1);
Brian Osman79457ef2020-09-24 15:01:27 -0400517 SkASSERT(c.fArguments[0]->is<VariableReference>());
518 String sampler =
519 this->getSamplerHandle(*c.fArguments[0]->as<VariableReference>().fVariable);
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400520 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerSwizzle(" + sampler +
Greg Daniel369ee6b2019-12-02 15:30:02 -0500521 ").asString().c_str()");
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400522 }
523}
524
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400525static const char* glsltype_string(const Context& context, const Type& type) {
526 if (type == *context.fFloat_Type) {
527 return "kFloat_GrSLType";
528 } else if (type == *context.fHalf_Type) {
529 return "kHalf_GrSLType";
John Stiles0e8149c2020-08-18 12:23:40 -0400530 } else if (type == *context.fInt_Type) {
531 return "kInt_GrSLType";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400532 } else if (type == *context.fFloat2_Type) {
533 return "kFloat2_GrSLType";
534 } else if (type == *context.fHalf2_Type) {
535 return "kHalf2_GrSLType";
John Stiles0e8149c2020-08-18 12:23:40 -0400536 } else if (type == *context.fInt2_Type) {
537 return "kInt2_GrSLType";
Ethan Nicholas8ae1b562019-12-17 15:18:02 -0500538 } else if (type == *context.fFloat3_Type) {
539 return "kFloat3_GrSLType";
540 } else if (type == *context.fHalf3_Type) {
541 return "kHalf3_GrSLType";
John Stiles0e8149c2020-08-18 12:23:40 -0400542 } else if (type == *context.fInt3_Type) {
543 return "kInt3_GrSLType";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400544 } else if (type == *context.fFloat4_Type) {
545 return "kFloat4_GrSLType";
546 } else if (type == *context.fHalf4_Type) {
547 return "kHalf4_GrSLType";
John Stiles0e8149c2020-08-18 12:23:40 -0400548 } else if (type == *context.fInt4_Type) {
549 return "kInt4_GrSLType";
Ethan Nicholas58430122020-04-14 09:54:02 -0400550 } else if (type == *context.fFloat2x2_Type) {
551 return "kFloat2x2_GrSLType";
552 } else if (type == *context.fHalf2x2_Type) {
553 return "kHalf2x2_GrSLType";
John Stiles0e8149c2020-08-18 12:23:40 -0400554 } else if (type == *context.fFloat2x3_Type) {
555 return "kFloat2x3_GrSLType";
556 } else if (type == *context.fHalf2x3_Type) {
557 return "kHalf2x3_GrSLType";
558 } else if (type == *context.fFloat2x4_Type) {
559 return "kFloat2x4_GrSLType";
560 } else if (type == *context.fHalf2x4_Type) {
561 return "kHalf2x4_GrSLType";
562 } else if (type == *context.fFloat3x2_Type) {
563 return "kFloat3x2_GrSLType";
564 } else if (type == *context.fHalf3x2_Type) {
565 return "kHalf3x2_GrSLType";
Ethan Nicholas58430122020-04-14 09:54:02 -0400566 } else if (type == *context.fFloat3x3_Type) {
567 return "kFloat3x3_GrSLType";
568 } else if (type == *context.fHalf3x3_Type) {
569 return "kHalf3x3_GrSLType";
John Stiles0e8149c2020-08-18 12:23:40 -0400570 } else if (type == *context.fFloat3x4_Type) {
571 return "kFloat3x4_GrSLType";
572 } else if (type == *context.fHalf3x4_Type) {
573 return "kHalf3x4_GrSLType";
574 } else if (type == *context.fFloat4x2_Type) {
575 return "kFloat4x2_GrSLType";
576 } else if (type == *context.fHalf4x2_Type) {
577 return "kHalf4x2_GrSLType";
578 } else if (type == *context.fFloat4x3_Type) {
579 return "kFloat4x3_GrSLType";
580 } else if (type == *context.fHalf4x3_Type) {
581 return "kHalf4x3_GrSLType";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400582 } else if (type == *context.fFloat4x4_Type) {
583 return "kFloat4x4_GrSLType";
584 } else if (type == *context.fHalf4x4_Type) {
585 return "kHalf4x4_GrSLType";
586 } else if (type == *context.fVoid_Type) {
587 return "kVoid_GrSLType";
John Stiles648a81e2020-09-02 10:52:14 -0400588 } else if (type == *context.fBool_Type) {
589 return "kBool_GrSLType";
Ethan Nicholase6592142020-09-08 10:22:09 -0400590 } else if (type.typeKind() == Type::TypeKind::kEnum) {
Ethan Nicholas8ae1b562019-12-17 15:18:02 -0500591 return "int";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400592 }
593 SkASSERT(false);
594 return nullptr;
595}
596
Ethan Nicholas762466e2017-06-29 10:03:38 -0400597void CPPCodeGenerator::writeFunction(const FunctionDefinition& f) {
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400598 const FunctionDeclaration& decl = f.fDeclaration;
Brian Osman08f986d2020-05-13 17:06:46 -0400599 if (decl.fBuiltin) {
600 return;
601 }
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400602 fFunctionHeader = "";
603 OutputStream* oldOut = fOut;
604 StringStream buffer;
605 fOut = &buffer;
Ethan Nicholas556b8be2020-10-02 17:08:55 -0400606 if (decl.name() == "main") {
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400607 fInMain = true;
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400608 for (const std::unique_ptr<Statement>& s : f.fBody->as<Block>().children()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400609 this->writeStatement(*s);
610 this->writeLine();
611 }
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400612 fInMain = false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400613
614 fOut = oldOut;
615 this->write(fFunctionHeader);
616 this->write(buffer.str());
617 } else {
Ethan Nicholas556b8be2020-10-02 17:08:55 -0400618 this->addExtraEmitCodeLine("SkString " + decl.name() + "_name;");
619 String args = "const GrShaderVar " + decl.name() + "_args[] = { ";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400620 const char* separator = "";
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400621 for (const Variable* param : decl.fParameters) {
Ethan Nicholas556b8be2020-10-02 17:08:55 -0400622 args += String(separator) + "GrShaderVar(\"" + param->name() + "\", " +
Ethan Nicholas30d30222020-09-11 12:27:26 -0400623 glsltype_string(fContext, param->type()) + ")";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400624 separator = ", ";
625 }
626 args += "};";
627 this->addExtraEmitCodeLine(args.c_str());
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400628 for (const std::unique_ptr<Statement>& s : f.fBody->as<Block>().children()) {
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400629 this->writeStatement(*s);
630 this->writeLine();
631 }
632
633 fOut = oldOut;
634 String emit = "fragBuilder->emitFunction(";
635 emit += glsltype_string(fContext, decl.fReturnType);
Ethan Nicholas556b8be2020-10-02 17:08:55 -0400636 emit += ", \"" + decl.name() + "\"";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400637 emit += ", " + to_string((int64_t) decl.fParameters.size());
Ethan Nicholas556b8be2020-10-02 17:08:55 -0400638 emit += ", " + decl.name() + "_args";
John Stiles50819422020-06-18 13:00:38 -0400639 emit += ",\nR\"SkSL(" + buffer.str() + ")SkSL\"";
Ethan Nicholas556b8be2020-10-02 17:08:55 -0400640 emit += ", &" + decl.name() + "_name);";
Ethan Nicholas095f5b42019-08-30 11:51:41 -0400641 this->addExtraEmitCodeLine(emit.c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400642 }
643}
644
645void CPPCodeGenerator::writeSetting(const Setting& s) {
Brian Osmanf265afd2020-08-04 13:23:36 -0400646 this->write(s.fName.c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400647}
648
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400649bool CPPCodeGenerator::writeSection(const char* name, const char* prefix) {
Ethan Nicholas68990be2017-07-13 09:36:52 -0400650 const Section* s = fSectionAndParameterHelper.getSection(name);
651 if (s) {
652 this->writef("%s%s", prefix, s->fText.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400653 return true;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400654 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400655 return false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400656}
657
658void CPPCodeGenerator::writeProgramElement(const ProgramElement& p) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400659 switch (p.kind()) {
660 case ProgramElement::Kind::kSection:
Ethan Nicholas762466e2017-06-29 10:03:38 -0400661 return;
Ethan Nicholase6592142020-09-08 10:22:09 -0400662 case ProgramElement::Kind::kVar: {
663 const VarDeclarations& decls = p.as<VarDeclarations>();
664 if (!decls.fVars.size()) {
665 return;
666 }
667 const Variable& var = *decls.fVars[0]->as<VarDeclaration>().fVar;
668 if (var.fModifiers.fFlags & (Modifiers::kIn_Flag | Modifiers::kUniform_Flag) ||
669 -1 != var.fModifiers.fLayout.fBuiltin) {
670 return;
671 }
672 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400673 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400674 default:
675 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400676 }
677 INHERITED::writeProgramElement(p);
678}
679
680void CPPCodeGenerator::addUniform(const Variable& var) {
681 if (!needs_uniform_var(var)) {
682 return;
683 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400684 if (var.fModifiers.fLayout.fWhen.fLength) {
685 this->writef(" if (%s) {\n ", String(var.fModifiers.fLayout.fWhen).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400686 }
Ethan Nicholas556b8be2020-10-02 17:08:55 -0400687 String name(var.name());
Ethan Nicholas30d30222020-09-11 12:27:26 -0400688 if (var.type().typeKind() != Type::TypeKind::kArray) {
Ethan Nicholas7018bcf2020-08-20 15:57:22 -0400689 this->writef(" %sVar = args.fUniformHandler->addUniform(&_outer, "
690 "kFragment_GrShaderFlag, %s, \"%s\");\n",
691 HCodeGenerator::FieldName(name.c_str()).c_str(),
Ethan Nicholas30d30222020-09-11 12:27:26 -0400692 glsltype_string(fContext, var.type()),
Ethan Nicholas7018bcf2020-08-20 15:57:22 -0400693 name.c_str());
694 } else {
695 this->writef(" %sVar = args.fUniformHandler->addUniformArray(&_outer, "
696 "kFragment_GrShaderFlag, %s, \"%s\", %d);\n",
697 HCodeGenerator::FieldName(name.c_str()).c_str(),
Ethan Nicholas30d30222020-09-11 12:27:26 -0400698 glsltype_string(fContext, var.type().componentType()),
Ethan Nicholas7018bcf2020-08-20 15:57:22 -0400699 name.c_str(),
Ethan Nicholas30d30222020-09-11 12:27:26 -0400700 var.type().columns());
Ethan Nicholas7018bcf2020-08-20 15:57:22 -0400701 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400702 if (var.fModifiers.fLayout.fWhen.fLength) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400703 this->write(" }\n");
704 }
705}
706
Ethan Nicholascd700e92018-08-24 16:43:57 -0400707void CPPCodeGenerator::writeInputVars() {
708}
709
Ethan Nicholas762466e2017-06-29 10:03:38 -0400710void CPPCodeGenerator::writePrivateVars() {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400711 for (const auto& p : fProgram) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400712 if (p.kind() == ProgramElement::Kind::kVar) {
John Stiles3dc0da62020-08-19 17:48:31 -0400713 const VarDeclarations& decls = p.as<VarDeclarations>();
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400714 for (const auto& raw : decls.fVars) {
John Stiles3dc0da62020-08-19 17:48:31 -0400715 VarDeclaration& decl = raw->as<VarDeclaration>();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000716 if (is_private(*decl.fVar)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400717 if (decl.fVar->type() == *fContext.fFragmentProcessor_Type) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000718 fErrors.error(decl.fOffset,
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400719 "fragmentProcessor variables must be declared 'in'");
720 return;
721 }
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500722 this->writef("%s %s = %s;\n",
Ethan Nicholas30d30222020-09-11 12:27:26 -0400723 HCodeGenerator::FieldType(fContext, decl.fVar->type(),
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000724 decl.fVar->fModifiers.fLayout).c_str(),
Ethan Nicholas556b8be2020-10-02 17:08:55 -0400725 String(decl.fVar->name()).c_str(),
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500726 default_value(*decl.fVar).c_str());
Michael Ludwiga4275592018-08-31 10:52:47 -0400727 } else if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
728 // An auto-tracked uniform in variable, so add a field to hold onto the prior
729 // state. Note that tracked variables must be uniform in's and that is validated
730 // before writePrivateVars() is called.
731 const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *decl.fVar);
732 SkASSERT(mapper && mapper->supportsTracking());
733
Ethan Nicholas556b8be2020-10-02 17:08:55 -0400734 String name = HCodeGenerator::FieldName(String(decl.fVar->name()).c_str());
Michael Ludwiga4275592018-08-31 10:52:47 -0400735 // The member statement is different if the mapper reports a default value
736 if (mapper->defaultValue().size() > 0) {
737 this->writef("%s %sPrev = %s;\n",
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400738 Layout::CTypeToStr(mapper->ctype()), name.c_str(),
Michael Ludwiga4275592018-08-31 10:52:47 -0400739 mapper->defaultValue().c_str());
740 } else {
741 this->writef("%s %sPrev;\n",
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400742 Layout::CTypeToStr(mapper->ctype()), name.c_str());
Michael Ludwiga4275592018-08-31 10:52:47 -0400743 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400744 }
745 }
746 }
747 }
748}
749
750void CPPCodeGenerator::writePrivateVarValues() {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400751 for (const auto& p : fProgram) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400752 if (p.kind() == ProgramElement::Kind::kVar) {
John Stiles3dc0da62020-08-19 17:48:31 -0400753 const VarDeclarations& decls = p.as<VarDeclarations>();
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400754 for (const auto& raw : decls.fVars) {
John Stiles3dc0da62020-08-19 17:48:31 -0400755 VarDeclaration& decl = raw->as<VarDeclaration>();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000756 if (is_private(*decl.fVar) && decl.fValue) {
Ethan Nicholas556b8be2020-10-02 17:08:55 -0400757 this->writef("%s = ", String(decl.fVar->name()).c_str());
Ethan Nicholas82399462017-10-16 12:35:44 -0400758 fCPPMode = true;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000759 this->writeExpression(*decl.fValue, kAssignment_Precedence);
Ethan Nicholas82399462017-10-16 12:35:44 -0400760 fCPPMode = false;
761 this->write(";\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400762 }
763 }
764 }
765 }
766}
767
Ethan Nicholas82399462017-10-16 12:35:44 -0400768static bool is_accessible(const Variable& var) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400769 const Type& type = var.type().nonnullable();
Ethan Nicholase6592142020-09-08 10:22:09 -0400770 return Type::TypeKind::kSampler != type.typeKind() &&
771 Type::TypeKind::kOther != type.typeKind();
Ethan Nicholas82399462017-10-16 12:35:44 -0400772}
773
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400774void CPPCodeGenerator::newExtraEmitCodeBlock() {
775 // This should only be called when emitting SKSL for emitCode(), which can be detected if the
776 // cpp buffer is not null, and the cpp buffer is not the current output.
777 SkASSERT(fCPPBuffer && fCPPBuffer != fOut);
778
779 // Start a new block as an empty string
780 fExtraEmitCodeBlocks.push_back("");
781 // Mark its location in the output buffer, uses ${\d} for the token since ${} will not occur in
782 // valid sksl and makes detection trivial.
783 this->writef("${%zu}", fExtraEmitCodeBlocks.size() - 1);
784}
785
786void CPPCodeGenerator::addExtraEmitCodeLine(const String& toAppend) {
787 SkASSERT(fExtraEmitCodeBlocks.size() > 0);
788 String& currentBlock = fExtraEmitCodeBlocks[fExtraEmitCodeBlocks.size() - 1];
789 // Automatically add indentation and newline
790 currentBlock += " " + toAppend + "\n";
791}
792
793void CPPCodeGenerator::flushEmittedCode() {
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400794 if (fCPPBuffer == nullptr) {
795 // Not actually within writeEmitCode() so nothing to flush
796 return;
797 }
798
799 StringStream* skslBuffer = static_cast<StringStream*>(fOut);
800
801 String sksl = skslBuffer->str();
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400802 // Empty the accumulation buffer since its current contents are consumed.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400803 skslBuffer->reset();
804
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400805 // Switch to the cpp buffer
Michael Ludwigd0440192018-09-07 14:24:52 +0000806 fOut = fCPPBuffer;
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400807
808 // Iterate through the sksl, keeping track of where the last statement ended (e.g. the latest
809 // encountered ';', '{', or '}'). If an extra emit code block token is encountered then the
810 // code from 0 to last statement end is sent to writeCodeAppend, the extra code block is
811 // appended to the cpp buffer, and then the sksl string is trimmed to start where the last
812 // statement left off (minus the encountered token).
813 size_t i = 0;
814 int flushPoint = -1;
815 int tokenStart = -1;
816 while (i < sksl.size()) {
817 if (tokenStart >= 0) {
818 // Looking for the end of the token
819 if (sksl[i] == '}') {
820 // Must append the sksl from 0 to flushPoint (inclusive) then the extra code
821 // accumulated in the block with index parsed from chars [tokenStart+2, i-1]
822 String toFlush = String(sksl.c_str(), flushPoint + 1);
823 // writeCodeAppend automatically removes the format args that it consumed, so
824 // fFormatArgs will be in a valid state for any future sksl
825 this->writeCodeAppend(toFlush);
826
827 int codeBlock = stoi(String(sksl.c_str() + tokenStart + 2, i - tokenStart - 2));
828 SkASSERT(codeBlock < (int) fExtraEmitCodeBlocks.size());
829 if (fExtraEmitCodeBlocks[codeBlock].size() > 0) {
830 this->write(fExtraEmitCodeBlocks[codeBlock].c_str());
831 }
832
833 // Now reset the sksl buffer to start after the flush point, but remove the token.
834 String compacted = String(sksl.c_str() + flushPoint + 1,
835 tokenStart - flushPoint - 1);
836 if (i < sksl.size() - 1) {
837 compacted += String(sksl.c_str() + i + 1, sksl.size() - i - 1);
838 }
839 sksl = compacted;
840
841 // And reset iteration
842 i = -1;
843 flushPoint = -1;
844 tokenStart = -1;
845 }
846 } else {
847 // Looking for the start of extra emit block tokens, and tracking when statements end
848 if (sksl[i] == ';' || sksl[i] == '{' || sksl[i] == '}') {
849 flushPoint = i;
850 } else if (i < sksl.size() - 1 && sksl[i] == '$' && sksl[i + 1] == '{') {
851 // found an extra emit code block token
852 tokenStart = i++;
853 }
854 }
855 i++;
Michael Ludwigd0440192018-09-07 14:24:52 +0000856 }
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400857
858 // Once we've gone through the sksl string to this point, there are no remaining extra emit
859 // code blocks to interleave, so append the remainder as usual.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400860 this->writeCodeAppend(sksl);
861
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400862 // After appending, switch back to the emptied sksl buffer and reset the extra code blocks
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400863 fOut = skslBuffer;
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400864 fExtraEmitCodeBlocks.clear();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400865}
866
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400867void CPPCodeGenerator::writeCodeAppend(const String& code) {
John Stiles50819422020-06-18 13:00:38 -0400868 if (!code.empty()) {
869 // Count % format specifiers.
870 size_t argCount = 0;
871 for (size_t index = 0; index < code.size(); ++index) {
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400872 if ('%' == code[index]) {
John Stiles50819422020-06-18 13:00:38 -0400873 if (index == code.size() - 1) {
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400874 break;
875 }
876 if (code[index + 1] != '%') {
877 ++argCount;
878 }
879 }
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400880 }
John Stiles50819422020-06-18 13:00:38 -0400881
882 // Emit the code string.
883 this->writef(" fragBuilder->codeAppendf(\n"
884 "R\"SkSL(%s)SkSL\"\n", code.c_str());
885 for (size_t i = 0; i < argCount; ++i) {
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400886 this->writef(", %s", fFormatArgs[i].c_str());
887 }
888 this->write(");\n");
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400889
John Stiles50819422020-06-18 13:00:38 -0400890 // argCount is equal to the number of fFormatArgs that were consumed, so they should be
891 // removed from the list.
892 if (argCount > 0) {
893 fFormatArgs.erase(fFormatArgs.begin(), fFormatArgs.begin() + argCount);
894 }
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400895 }
896}
897
898String CPPCodeGenerator::convertSKSLExpressionToCPP(const Expression& e,
899 const String& cppVar) {
900 // To do this conversion, we temporarily switch the sksl output stream
901 // to an empty stringstream and reset the format args to empty.
902 OutputStream* oldSKSL = fOut;
903 StringStream exprBuffer;
904 fOut = &exprBuffer;
905
906 std::vector<String> oldArgs(fFormatArgs);
907 fFormatArgs.clear();
908
909 // Convert the argument expression into a format string and args
910 this->writeExpression(e, Precedence::kTopLevel_Precedence);
911 std::vector<String> newArgs(fFormatArgs);
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400912 String expr = exprBuffer.str();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400913
914 // After generating, restore the original output stream and format args
915 fFormatArgs = oldArgs;
916 fOut = oldSKSL;
917
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400918 // The sksl written to exprBuffer is not processed by flushEmittedCode(), so any extra emit code
919 // block tokens won't get handled. So we need to strip them from the expression and stick them
920 // to the end of the original sksl stream.
921 String exprFormat = "";
922 int tokenStart = -1;
923 for (size_t i = 0; i < expr.size(); i++) {
924 if (tokenStart >= 0) {
925 if (expr[i] == '}') {
926 // End of the token, so append the token to fOut
927 fOut->write(expr.c_str() + tokenStart, i - tokenStart + 1);
928 tokenStart = -1;
929 }
930 } else {
931 if (i < expr.size() - 1 && expr[i] == '$' && expr[i + 1] == '{') {
932 tokenStart = i++;
933 } else {
934 exprFormat += expr[i];
935 }
936 }
937 }
938
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400939 // Now build the final C++ code snippet from the format string and args
940 String cppExpr;
John Stiles50819422020-06-18 13:00:38 -0400941 if (newArgs.empty()) {
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400942 // This was a static expression, so we can simplify the input
943 // color declaration in the emitted code to just a static string
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400944 cppExpr = "SkString " + cppVar + "(\"" + exprFormat + "\");";
John Stiles50819422020-06-18 13:00:38 -0400945 } else if (newArgs.size() == 1 && exprFormat == "%s") {
946 // If the format expression is simply "%s", we can avoid an expensive call to printf.
947 // This happens fairly often in codegen so it is worth simplifying.
948 cppExpr = "SkString " + cppVar + "(" + newArgs[0] + ");";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400949 } else {
950 // String formatting must occur dynamically, so have the C++ declaration
951 // use SkStringPrintf with the format args that were accumulated
952 // when the expression was written.
953 cppExpr = "SkString " + cppVar + " = SkStringPrintf(\"" + exprFormat + "\"";
954 for (size_t i = 0; i < newArgs.size(); i++) {
955 cppExpr += ", " + newArgs[i];
956 }
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400957 cppExpr += ");";
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400958 }
959 return cppExpr;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -0400960}
961
Ethan Nicholas762466e2017-06-29 10:03:38 -0400962bool CPPCodeGenerator::writeEmitCode(std::vector<const Variable*>& uniforms) {
963 this->write(" void emitCode(EmitArgs& args) override {\n"
964 " GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;\n");
965 this->writef(" const %s& _outer = args.fFp.cast<%s>();\n"
966 " (void) _outer;\n",
967 fFullName.c_str(), fFullName.c_str());
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400968 for (const auto& p : fProgram) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400969 if (p.kind() == ProgramElement::Kind::kVar) {
John Stiles3dc0da62020-08-19 17:48:31 -0400970 const VarDeclarations& decls = p.as<VarDeclarations>();
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400971 for (const auto& raw : decls.fVars) {
John Stiles3dc0da62020-08-19 17:48:31 -0400972 VarDeclaration& decl = raw->as<VarDeclaration>();
Ethan Nicholas556b8be2020-10-02 17:08:55 -0400973 String nameString(decl.fVar->name());
Ethan Nicholas82399462017-10-16 12:35:44 -0400974 const char* name = nameString.c_str();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000975 if (SectionAndParameterHelper::IsParameter(*decl.fVar) &&
976 is_accessible(*decl.fVar)) {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400977 this->writef(" auto %s = _outer.%s;\n"
Ethan Nicholas82399462017-10-16 12:35:44 -0400978 " (void) %s;\n",
979 name, name, name);
980 }
981 }
982 }
983 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400984 this->writePrivateVarValues();
985 for (const auto u : uniforms) {
986 this->addUniform(*u);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400987 }
John Stiles02b11282020-08-10 15:25:24 -0400988 this->writeSection(kEmitCodeSection);
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400989
990 // Save original buffer as the CPP buffer for flushEmittedCode()
991 fCPPBuffer = fOut;
992 StringStream skslBuffer;
993 fOut = &skslBuffer;
994
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400995 this->newExtraEmitCodeBlock();
Ethan Nicholas762466e2017-06-29 10:03:38 -0400996 bool result = INHERITED::generateCode();
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400997 this->flushEmittedCode();
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400998
999 // Then restore the original CPP buffer and close the function
1000 fOut = fCPPBuffer;
1001 fCPPBuffer = nullptr;
Ethan Nicholas5b6e6272017-10-13 13:11:06 -04001002 this->write(" }\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -04001003 return result;
1004}
1005
1006void CPPCodeGenerator::writeSetData(std::vector<const Variable*>& uniforms) {
1007 const char* fullName = fFullName.c_str();
John Stiles02b11282020-08-10 15:25:24 -04001008 const Section* section = fSectionAndParameterHelper.getSection(kSetDataSection);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001009 const char* pdman = section ? section->fArgument.c_str() : "pdman";
Ethan Nicholas762466e2017-06-29 10:03:38 -04001010 this->writef(" void onSetData(const GrGLSLProgramDataManager& %s, "
1011 "const GrFragmentProcessor& _proc) override {\n",
1012 pdman);
1013 bool wroteProcessor = false;
John Stiles06f3d082020-06-04 11:07:21 -04001014 for (const Variable* u : uniforms) {
Michael Ludwiga4275592018-08-31 10:52:47 -04001015 if (is_uniform_in(*u)) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001016 if (!wroteProcessor) {
1017 this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName, fullName);
1018 wroteProcessor = true;
1019 this->writef(" {\n");
1020 }
Michael Ludwiga4275592018-08-31 10:52:47 -04001021
1022 const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *u);
1023 SkASSERT(mapper);
1024
Ethan Nicholas556b8be2020-10-02 17:08:55 -04001025 String nameString(u->name());
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001026 const char* name = nameString.c_str();
Michael Ludwiga4275592018-08-31 10:52:47 -04001027
1028 // Switches for setData behavior in the generated code
1029 bool conditionalUniform = u->fModifiers.fLayout.fWhen != "";
1030 bool isTracked = u->fModifiers.fLayout.fFlags & Layout::kTracked_Flag;
1031 bool needsValueDeclaration = isTracked || !mapper->canInlineUniformValue();
1032
1033 String uniformName = HCodeGenerator::FieldName(name) + "Var";
1034
1035 String indent = " "; // 8 by default, 12 when nested for conditional uniforms
1036 if (conditionalUniform) {
1037 // Add a pre-check to make sure the uniform was emitted
1038 // before trying to send any data to the GPU
1039 this->writef(" if (%s.isValid()) {\n", uniformName.c_str());
1040 indent += " ";
1041 }
1042
1043 String valueVar = "";
1044 if (needsValueDeclaration) {
1045 valueVar.appendf("%sValue", name);
1046 // Use AccessType since that will match the return type of _outer's public API.
Ethan Nicholas30d30222020-09-11 12:27:26 -04001047 String valueType = HCodeGenerator::AccessType(fContext, u->type(),
Michael Ludwiga4275592018-08-31 10:52:47 -04001048 u->fModifiers.fLayout);
Ethan Nicholasbcd51e82019-04-09 10:40:41 -04001049 this->writef("%s%s %s = _outer.%s;\n",
Michael Ludwiga4275592018-08-31 10:52:47 -04001050 indent.c_str(), valueType.c_str(), valueVar.c_str(), name);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001051 } else {
Michael Ludwiga4275592018-08-31 10:52:47 -04001052 // Not tracked and the mapper only needs to use the value once
1053 // so send it a safe expression instead of the variable name
Ethan Nicholasbcd51e82019-04-09 10:40:41 -04001054 valueVar.appendf("(_outer.%s)", name);
Michael Ludwiga4275592018-08-31 10:52:47 -04001055 }
1056
1057 if (isTracked) {
1058 SkASSERT(mapper->supportsTracking());
1059
1060 String prevVar = HCodeGenerator::FieldName(name) + "Prev";
1061 this->writef("%sif (%s) {\n"
1062 "%s %s;\n"
1063 "%s %s;\n"
1064 "%s}\n", indent.c_str(),
1065 mapper->dirtyExpression(valueVar, prevVar).c_str(), indent.c_str(),
1066 mapper->saveState(valueVar, prevVar).c_str(), indent.c_str(),
1067 mapper->setUniform(pdman, uniformName, valueVar).c_str(), indent.c_str());
1068 } else {
1069 this->writef("%s%s;\n", indent.c_str(),
1070 mapper->setUniform(pdman, uniformName, valueVar).c_str());
1071 }
1072
1073 if (conditionalUniform) {
1074 // Close the earlier precheck block
1075 this->writef(" }\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -04001076 }
1077 }
1078 }
1079 if (wroteProcessor) {
1080 this->writef(" }\n");
1081 }
Ethan Nicholas68990be2017-07-13 09:36:52 -04001082 if (section) {
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05001083 int samplerIndex = 0;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001084 for (const auto& p : fProgram) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001085 if (p.kind() == ProgramElement::Kind::kVar) {
John Stiles3dc0da62020-08-19 17:48:31 -04001086 const VarDeclarations& decls = p.as<VarDeclarations>();
John Stiles06f3d082020-06-04 11:07:21 -04001087 for (const std::unique_ptr<Statement>& raw : decls.fVars) {
John Stiles3dc0da62020-08-19 17:48:31 -04001088 const VarDeclaration& decl = raw->as<VarDeclaration>();
John Stiles06f3d082020-06-04 11:07:21 -04001089 const Variable& variable = *decl.fVar;
Ethan Nicholas556b8be2020-10-02 17:08:55 -04001090 String nameString(variable.name());
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001091 const char* name = nameString.c_str();
Ethan Nicholas30d30222020-09-11 12:27:26 -04001092 if (variable.type().typeKind() == Type::TypeKind::kSampler) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +00001093 this->writef(" const GrSurfaceProxyView& %sView = "
1094 "_outer.textureSampler(%d).view();\n",
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05001095 name, samplerIndex);
Robert Phillipsbd99c0c2019-12-12 13:26:58 +00001096 this->writef(" GrTexture& %s = *%sView.proxy()->peekTexture();\n",
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05001097 name, name);
1098 this->writef(" (void) %s;\n", name);
1099 ++samplerIndex;
John Stiles06f3d082020-06-04 11:07:21 -04001100 } else if (needs_uniform_var(variable)) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001101 this->writef(" UniformHandle& %s = %sVar;\n"
1102 " (void) %s;\n",
1103 name, HCodeGenerator::FieldName(name).c_str(), name);
John Stiles06f3d082020-06-04 11:07:21 -04001104 } else if (SectionAndParameterHelper::IsParameter(variable) &&
Ethan Nicholas30d30222020-09-11 12:27:26 -04001105 variable.type() != *fContext.fFragmentProcessor_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001106 if (!wroteProcessor) {
1107 this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName,
1108 fullName);
1109 wroteProcessor = true;
1110 }
John Stiles06f3d082020-06-04 11:07:21 -04001111
Ethan Nicholas30d30222020-09-11 12:27:26 -04001112 if (variable.type().nonnullable() != *fContext.fFragmentProcessor_Type) {
John Stiles06f3d082020-06-04 11:07:21 -04001113 this->writef(" auto %s = _outer.%s;\n"
1114 " (void) %s;\n",
1115 name, name, name);
1116 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001117 }
1118 }
1119 }
1120 }
John Stiles02b11282020-08-10 15:25:24 -04001121 this->writeSection(kSetDataSection);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001122 }
1123 this->write(" }\n");
1124}
1125
Brian Salomonf7dcd762018-07-30 14:48:15 -04001126void CPPCodeGenerator::writeOnTextureSampler() {
1127 bool foundSampler = false;
1128 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001129 if (param->type().typeKind() == Type::TypeKind::kSampler) {
Brian Salomonf7dcd762018-07-30 14:48:15 -04001130 if (!foundSampler) {
1131 this->writef(
1132 "const GrFragmentProcessor::TextureSampler& %s::onTextureSampler(int "
1133 "index) const {\n",
1134 fFullName.c_str());
1135 this->writef(" return IthTextureSampler(index, %s",
Ethan Nicholas556b8be2020-10-02 17:08:55 -04001136 HCodeGenerator::FieldName(String(param->name()).c_str()).c_str());
Brian Salomonf7dcd762018-07-30 14:48:15 -04001137 foundSampler = true;
1138 } else {
1139 this->writef(", %s",
Ethan Nicholas556b8be2020-10-02 17:08:55 -04001140 HCodeGenerator::FieldName(String(param->name()).c_str()).c_str());
Brian Salomonf7dcd762018-07-30 14:48:15 -04001141 }
1142 }
1143 }
1144 if (foundSampler) {
1145 this->write(");\n}\n");
1146 }
1147}
1148
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001149void CPPCodeGenerator::writeClone() {
John Stiles02b11282020-08-10 15:25:24 -04001150 if (!this->writeSection(kCloneSection)) {
1151 if (fSectionAndParameterHelper.getSection(kFieldsSection)) {
John Stiles47b4e222020-08-12 09:56:50 -04001152 fErrors.error(/*offset=*/0, "fragment processors with custom @fields must also have a "
1153 "custom @clone");
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001154 }
1155 this->writef("%s::%s(const %s& src)\n"
Ethan Nicholasabff9562017-10-09 10:54:08 -04001156 ": INHERITED(k%s_ClassID, src.optimizationFlags())", fFullName.c_str(),
1157 fFullName.c_str(), fFullName.c_str(), fFullName.c_str());
John Stiles06f3d082020-06-04 11:07:21 -04001158 for (const Variable* param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas556b8be2020-10-02 17:08:55 -04001159 String fieldName = HCodeGenerator::FieldName(String(param->name()).c_str());
Ethan Nicholas30d30222020-09-11 12:27:26 -04001160 if (param->type().nonnullable() != *fContext.fFragmentProcessor_Type) {
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001161 this->writef("\n, %s(src.%s)",
1162 fieldName.c_str(),
1163 fieldName.c_str());
1164 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001165 }
Ethan Nicholasabff9562017-10-09 10:54:08 -04001166 this->writef(" {\n");
Brian Osman12c5d292020-07-13 16:11:35 -04001167 this->writef(" this->cloneAndRegisterAllChildProcessors(src);\n");
Brian Salomonf7dcd762018-07-30 14:48:15 -04001168 int samplerCount = 0;
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001169 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001170 if (param->type().typeKind() == Type::TypeKind::kSampler) {
Brian Salomonf7dcd762018-07-30 14:48:15 -04001171 ++samplerCount;
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001172 }
1173 }
Brian Salomonf7dcd762018-07-30 14:48:15 -04001174 if (samplerCount) {
1175 this->writef(" this->setTextureSamplerCnt(%d);", samplerCount);
1176 }
Michael Ludwige88320b2020-06-24 09:04:56 -04001177 if (fAccessSampleCoordsDirectly) {
1178 this->writef(" this->setUsesSampleCoordsDirectly();\n");
1179 }
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001180 this->write("}\n");
Brian Salomonaff329b2017-08-11 09:40:37 -04001181 this->writef("std::unique_ptr<GrFragmentProcessor> %s::clone() const {\n",
1182 fFullName.c_str());
John Stilesfbd050b2020-08-03 13:21:46 -04001183 this->writef(" return std::make_unique<%s>(*this);\n",
Brian Salomonaff329b2017-08-11 09:40:37 -04001184 fFullName.c_str());
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001185 this->write("}\n");
1186 }
1187}
1188
John Stiles47b4e222020-08-12 09:56:50 -04001189void CPPCodeGenerator::writeDumpInfo() {
John Stiles8d9bf642020-08-12 15:07:45 -04001190 this->writef("#if GR_TEST_UTILS\n"
John Stilescab58862020-08-12 15:47:06 -04001191 "SkString %s::onDumpInfo() const {\n", fFullName.c_str());
John Stiles47b4e222020-08-12 09:56:50 -04001192
1193 if (!this->writeSection(kDumpInfoSection)) {
1194 if (fSectionAndParameterHelper.getSection(kFieldsSection)) {
1195 fErrors.error(/*offset=*/0, "fragment processors with custom @fields must also have a "
1196 "custom @dumpInfo");
1197 }
1198
John Stiles47b4e222020-08-12 09:56:50 -04001199 String formatString;
1200 std::vector<String> argumentList;
1201
1202 for (const Variable* param : fSectionAndParameterHelper.getParameters()) {
1203 // dumpInfo() doesn't need to log child FPs.
Ethan Nicholas30d30222020-09-11 12:27:26 -04001204 if (param->type().nonnullable() == *fContext.fFragmentProcessor_Type) {
John Stiles47b4e222020-08-12 09:56:50 -04001205 continue;
1206 }
1207
1208 // Add this field onto the format string and argument list.
Ethan Nicholas556b8be2020-10-02 17:08:55 -04001209 String fieldName = HCodeGenerator::FieldName(String(param->name()).c_str());
Ethan Nicholas30d30222020-09-11 12:27:26 -04001210 String runtimeValue = this->formatRuntimeValue(param->type(),
1211 param->fModifiers.fLayout,
Ethan Nicholas556b8be2020-10-02 17:08:55 -04001212 param->name(),
Ethan Nicholas30d30222020-09-11 12:27:26 -04001213 &argumentList);
John Stiles47b4e222020-08-12 09:56:50 -04001214 formatString.appendf("%s%s=%s",
1215 formatString.empty() ? "" : ", ",
1216 fieldName.c_str(),
1217 runtimeValue.c_str());
1218 }
1219
John Stiles47b4e222020-08-12 09:56:50 -04001220 if (!formatString.empty()) {
John Stilescab58862020-08-12 15:47:06 -04001221 // Emit the finished format string and associated arguments.
1222 this->writef(" return SkStringPrintf(\"(%s)\"", formatString.c_str());
John Stiles47b4e222020-08-12 09:56:50 -04001223
John Stilescab58862020-08-12 15:47:06 -04001224 for (const String& argument : argumentList) {
1225 this->writef(", %s", argument.c_str());
1226 }
John Stiles47b4e222020-08-12 09:56:50 -04001227
John Stilescab58862020-08-12 15:47:06 -04001228 this->write(");");
1229 } else {
1230 // No fields to dump at all; just return an empty string.
1231 this->write(" return SkString();");
1232 }
John Stiles47b4e222020-08-12 09:56:50 -04001233 }
1234
John Stilescab58862020-08-12 15:47:06 -04001235 this->write("\n"
1236 "}\n"
John Stiles47b4e222020-08-12 09:56:50 -04001237 "#endif\n");
1238}
1239
Ethan Nicholas762466e2017-06-29 10:03:38 -04001240void CPPCodeGenerator::writeTest() {
John Stiles02b11282020-08-10 15:25:24 -04001241 const Section* test = fSectionAndParameterHelper.getSection(kTestCodeSection);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001242 if (test) {
Brian Salomonaff329b2017-08-11 09:40:37 -04001243 this->writef(
1244 "GR_DEFINE_FRAGMENT_PROCESSOR_TEST(%s);\n"
1245 "#if GR_TEST_UTILS\n"
1246 "std::unique_ptr<GrFragmentProcessor> %s::TestCreate(GrProcessorTestData* %s) {\n",
1247 fFullName.c_str(),
1248 fFullName.c_str(),
1249 test->fArgument.c_str());
John Stiles02b11282020-08-10 15:25:24 -04001250 this->writeSection(kTestCodeSection);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001251 this->write("}\n"
1252 "#endif\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -04001253 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001254}
1255
1256void CPPCodeGenerator::writeGetKey() {
1257 this->writef("void %s::onGetGLSLProcessorKey(const GrShaderCaps& caps, "
1258 "GrProcessorKeyBuilder* b) const {\n",
1259 fFullName.c_str());
Ethan Nicholascab767f2019-07-01 13:32:07 -04001260 for (const auto& p : fProgram) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001261 if (p.kind() == ProgramElement::Kind::kVar) {
John Stiles3dc0da62020-08-19 17:48:31 -04001262 const VarDeclarations& decls = p.as<VarDeclarations>();
Ethan Nicholascab767f2019-07-01 13:32:07 -04001263 for (const auto& raw : decls.fVars) {
John Stiles3dc0da62020-08-19 17:48:31 -04001264 const VarDeclaration& decl = raw->as<VarDeclaration>();
Ethan Nicholascab767f2019-07-01 13:32:07 -04001265 const Variable& var = *decl.fVar;
Ethan Nicholas30d30222020-09-11 12:27:26 -04001266 const Type& varType = var.type();
Ethan Nicholas556b8be2020-10-02 17:08:55 -04001267 String nameString(var.name());
Ethan Nicholascab767f2019-07-01 13:32:07 -04001268 const char* name = nameString.c_str();
1269 if (var.fModifiers.fLayout.fKey != Layout::kNo_Key &&
1270 (var.fModifiers.fFlags & Modifiers::kUniform_Flag)) {
1271 fErrors.error(var.fOffset,
1272 "layout(key) may not be specified on uniforms");
Ethan Nicholasbcd51e82019-04-09 10:40:41 -04001273 }
Ethan Nicholascab767f2019-07-01 13:32:07 -04001274 switch (var.fModifiers.fLayout.fKey) {
1275 case Layout::kKey_Key:
1276 if (is_private(var)) {
1277 this->writef("%s %s =",
Ethan Nicholas30d30222020-09-11 12:27:26 -04001278 HCodeGenerator::FieldType(fContext, varType,
Ethan Nicholascab767f2019-07-01 13:32:07 -04001279 var.fModifiers.fLayout).c_str(),
Ethan Nicholas556b8be2020-10-02 17:08:55 -04001280 String(var.name()).c_str());
Ethan Nicholascab767f2019-07-01 13:32:07 -04001281 if (decl.fValue) {
1282 fCPPMode = true;
1283 this->writeExpression(*decl.fValue, kAssignment_Precedence);
1284 fCPPMode = false;
1285 } else {
1286 this->writef("%s", default_value(var).c_str());
1287 }
1288 this->write(";\n");
1289 }
1290 if (var.fModifiers.fLayout.fWhen.fLength) {
1291 this->writef("if (%s) {", String(var.fModifiers.fLayout.fWhen).c_str());
1292 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04001293 if (varType == *fContext.fHalf4_Type) {
Ethan Nicholascab767f2019-07-01 13:32:07 -04001294 this->writef(" uint16_t red = SkFloatToHalf(%s.fR);\n",
1295 HCodeGenerator::FieldName(name).c_str());
1296 this->writef(" uint16_t green = SkFloatToHalf(%s.fG);\n",
1297 HCodeGenerator::FieldName(name).c_str());
1298 this->writef(" uint16_t blue = SkFloatToHalf(%s.fB);\n",
1299 HCodeGenerator::FieldName(name).c_str());
1300 this->writef(" uint16_t alpha = SkFloatToHalf(%s.fA);\n",
1301 HCodeGenerator::FieldName(name).c_str());
1302 this->write(" b->add32(((uint32_t)red << 16) | green);\n");
1303 this->write(" b->add32(((uint32_t)blue << 16) | alpha);\n");
Ethan Nicholas30d30222020-09-11 12:27:26 -04001304 } else if (varType == *fContext.fHalf_Type ||
1305 varType == *fContext.fFloat_Type) {
John Stiles45f5b032020-07-27 17:31:29 -04001306 this->writef(" b->add32(sk_bit_cast<uint32_t>(%s));\n",
Ethan Nicholascab767f2019-07-01 13:32:07 -04001307 HCodeGenerator::FieldName(name).c_str());
Ethan Nicholas30d30222020-09-11 12:27:26 -04001308 } else if (varType.isInteger() || varType == *fContext.fBool_Type ||
1309 varType.typeKind() == Type::TypeKind::kEnum) {
John Stiles45f5b032020-07-27 17:31:29 -04001310 this->writef(" b->add32((uint32_t) %s);\n",
1311 HCodeGenerator::FieldName(name).c_str());
1312 } else {
1313 ABORT("NOT YET IMPLEMENTED: automatic key handling for %s\n",
Ethan Nicholas30d30222020-09-11 12:27:26 -04001314 varType.displayName().c_str());
Ethan Nicholascab767f2019-07-01 13:32:07 -04001315 }
1316 if (var.fModifiers.fLayout.fWhen.fLength) {
1317 this->write("}");
1318 }
1319 break;
1320 case Layout::kIdentity_Key:
Ethan Nicholas30d30222020-09-11 12:27:26 -04001321 if (varType.typeKind() != Type::TypeKind::kMatrix) {
Ethan Nicholascab767f2019-07-01 13:32:07 -04001322 fErrors.error(var.fOffset,
1323 "layout(key=identity) requires matrix type");
1324 }
1325 this->writef(" b->add32(%s.isIdentity() ? 1 : 0);\n",
1326 HCodeGenerator::FieldName(name).c_str());
1327 break;
1328 case Layout::kNo_Key:
1329 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001330 }
Ethan Nicholascab767f2019-07-01 13:32:07 -04001331 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001332 }
1333 }
1334 this->write("}\n");
1335}
1336
1337bool CPPCodeGenerator::generateCode() {
1338 std::vector<const Variable*> uniforms;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001339 for (const auto& p : fProgram) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001340 if (p.kind() == ProgramElement::Kind::kVar) {
John Stiles3dc0da62020-08-19 17:48:31 -04001341 const VarDeclarations& decls = p.as<VarDeclarations>();
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001342 for (const auto& raw : decls.fVars) {
John Stiles3dc0da62020-08-19 17:48:31 -04001343 VarDeclaration& decl = raw->as<VarDeclaration>();
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001344 if ((decl.fVar->fModifiers.fFlags & Modifiers::kUniform_Flag) &&
Ethan Nicholas30d30222020-09-11 12:27:26 -04001345 decl.fVar->type().typeKind() != Type::TypeKind::kSampler) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001346 uniforms.push_back(decl.fVar);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001347 }
Michael Ludwiga4275592018-08-31 10:52:47 -04001348
1349 if (is_uniform_in(*decl.fVar)) {
1350 // Validate the "uniform in" declarations to make sure they are fully supported,
1351 // instead of generating surprising C++
1352 const UniformCTypeMapper* mapper =
1353 UniformCTypeMapper::Get(fContext, *decl.fVar);
1354 if (mapper == nullptr) {
Ethan Nicholas556b8be2020-10-02 17:08:55 -04001355 fErrors.error(decl.fOffset, String(decl.fVar->name())
Michael Ludwiga4275592018-08-31 10:52:47 -04001356 + "'s type is not supported for use as a 'uniform in'");
1357 return false;
1358 }
1359 if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
1360 if (!mapper->supportsTracking()) {
Ethan Nicholas556b8be2020-10-02 17:08:55 -04001361 fErrors.error(decl.fOffset, String(decl.fVar->name())
Michael Ludwiga4275592018-08-31 10:52:47 -04001362 + "'s type does not support state tracking");
1363 return false;
1364 }
1365 }
1366
1367 } else {
1368 // If it's not a uniform_in, it's an error to be tracked
1369 if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
1370 fErrors.error(decl.fOffset, "Non-'in uniforms' cannot be tracked");
1371 return false;
1372 }
1373 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001374 }
1375 }
1376 }
1377 const char* baseName = fName.c_str();
1378 const char* fullName = fFullName.c_str();
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05001379 this->writef("%s\n", HCodeGenerator::GetHeader(fProgram, fErrors).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001380 this->writef(kFragmentProcessorHeader, fullName);
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001381 this->writef("#include \"%s.h\"\n\n", fullName);
John Stiles02b11282020-08-10 15:25:24 -04001382 this->writeSection(kCppSection);
John Stiles45f5b032020-07-27 17:31:29 -04001383 this->writef("#include \"src/core/SkUtils.h\"\n"
1384 "#include \"src/gpu/GrTexture.h\"\n"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001385 "#include \"src/gpu/glsl/GrGLSLFragmentProcessor.h\"\n"
1386 "#include \"src/gpu/glsl/GrGLSLFragmentShaderBuilder.h\"\n"
1387 "#include \"src/gpu/glsl/GrGLSLProgramBuilder.h\"\n"
1388 "#include \"src/sksl/SkSLCPP.h\"\n"
1389 "#include \"src/sksl/SkSLUtil.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -04001390 "class GrGLSL%s : public GrGLSLFragmentProcessor {\n"
1391 "public:\n"
1392 " GrGLSL%s() {}\n",
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04001393 baseName, baseName);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001394 bool result = this->writeEmitCode(uniforms);
1395 this->write("private:\n");
1396 this->writeSetData(uniforms);
1397 this->writePrivateVars();
1398 for (const auto& u : uniforms) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001399 if (needs_uniform_var(*u) && !(u->fModifiers.fFlags & Modifiers::kIn_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001400 this->writef(" UniformHandle %sVar;\n",
Ethan Nicholas556b8be2020-10-02 17:08:55 -04001401 HCodeGenerator::FieldName(String(u->name()).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001402 }
1403 }
Ethan Nicholas68990be2017-07-13 09:36:52 -04001404 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001405 if (needs_uniform_var(*param)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001406 this->writef(" UniformHandle %sVar;\n",
Ethan Nicholas556b8be2020-10-02 17:08:55 -04001407 HCodeGenerator::FieldName(String(param->name()).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -04001408 }
1409 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001410 this->writef("};\n"
1411 "GrGLSLFragmentProcessor* %s::onCreateGLSLInstance() const {\n"
1412 " return new GrGLSL%s();\n"
1413 "}\n",
1414 fullName, baseName);
1415 this->writeGetKey();
1416 this->writef("bool %s::onIsEqual(const GrFragmentProcessor& other) const {\n"
1417 " const %s& that = other.cast<%s>();\n"
1418 " (void) that;\n",
1419 fullName, fullName, fullName);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001420 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001421 if (param->type().nonnullable() == *fContext.fFragmentProcessor_Type) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -04001422 continue;
1423 }
Ethan Nicholas556b8be2020-10-02 17:08:55 -04001424 String nameString(param->name());
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001425 const char* name = nameString.c_str();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001426 this->writef(" if (%s != that.%s) return false;\n",
1427 HCodeGenerator::FieldName(name).c_str(),
1428 HCodeGenerator::FieldName(name).c_str());
1429 }
1430 this->write(" return true;\n"
1431 "}\n");
John Stiles735a5a72020-08-26 10:21:10 -04001432 this->writef("bool %s::usesExplicitReturn() const {\n"
1433 " return %s;\n"
1434 "}\n",
1435 fullName, fReturnType == ReturnType::kUsesExplicitReturn ? "true" : "false");
Ethan Nicholasf57c0d62017-07-31 11:18:22 -04001436 this->writeClone();
John Stiles47b4e222020-08-12 09:56:50 -04001437 this->writeDumpInfo();
Brian Salomonf7dcd762018-07-30 14:48:15 -04001438 this->writeOnTextureSampler();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001439 this->writeTest();
John Stiles02b11282020-08-10 15:25:24 -04001440 this->writeSection(kCppEndSection);
Greg Daniel3e8c3452018-04-06 10:37:55 -04001441
Ethan Nicholas762466e2017-06-29 10:03:38 -04001442 result &= 0 == fErrors.errorCount();
1443 return result;
1444}
1445
John Stilesa6841be2020-08-06 14:11:56 -04001446} // namespace SkSL
Ethan Nicholas2a479a52020-08-18 16:29:45 -04001447
Greg Daniela28ea672020-09-25 11:12:56 -04001448#endif // defined(SKSL_STANDALONE) || GR_TEST_UTILS