blob: 474f3e3636f641b864260a7ec212560d82a88db1 [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
8#include "SkSLHCodeGenerator.h"
9
10#include "SkSLUtil.h"
11#include "ir/SkSLFunctionDeclaration.h"
12#include "ir/SkSLFunctionDefinition.h"
13#include "ir/SkSLSection.h"
14#include "ir/SkSLVarDeclarations.h"
15
16namespace SkSL {
17
Ethan Nicholasc9472af2017-10-10 16:30:21 -040018HCodeGenerator::HCodeGenerator(const Context* context, const Program* program,
19 ErrorReporter* errors, String name, OutputStream* out)
Ethan Nicholas762466e2017-06-29 10:03:38 -040020: INHERITED(program, errors, out)
Ethan Nicholasc9472af2017-10-10 16:30:21 -040021, fContext(*context)
Ethan Nicholas762466e2017-06-29 10:03:38 -040022, fName(std::move(name))
23, fFullName(String::printf("Gr%s", fName.c_str()))
24, fSectionAndParameterHelper(*program, *errors) {}
25
Ethan Nicholasd608c092017-10-26 09:30:08 -040026String HCodeGenerator::ParameterType(const Context& context, const Type& type,
27 const Layout& layout) {
28 if (layout.fCType != "") {
29 return layout.fCType;
30 } else if (type == *context.fFloat_Type || type == *context.fHalf_Type) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -040031 return "float";
Ethan Nicholasc9472af2017-10-10 16:30:21 -040032 } else if (type == *context.fFloat2_Type || type == *context.fHalf2_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -040033 return "SkPoint";
Ethan Nicholasc9472af2017-10-10 16:30:21 -040034 } else if (type == *context.fInt4_Type || type == *context.fShort4_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -040035 return "SkIRect";
Ethan Nicholasc9472af2017-10-10 16:30:21 -040036 } else if (type == *context.fFloat4_Type || type == *context.fHalf4_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -040037 return "SkRect";
Ethan Nicholasc9472af2017-10-10 16:30:21 -040038 } else if (type == *context.fFloat4x4_Type || type == *context.fHalf4x4_Type) {
Ethan Nicholas762466e2017-06-29 10:03:38 -040039 return "SkMatrix44";
40 } else if (type.kind() == Type::kSampler_Kind) {
41 return "sk_sp<GrTextureProxy>";
Ethan Nicholasc9472af2017-10-10 16:30:21 -040042 } else if (type == *context.fFragmentProcessor_Type) {
43 return "std::unique_ptr<GrFragmentProcessor>";
Ethan Nicholas762466e2017-06-29 10:03:38 -040044 }
45 return type.name();
46}
47
Ethan Nicholasd608c092017-10-26 09:30:08 -040048String HCodeGenerator::FieldType(const Context& context, const Type& type,
49 const Layout& layout) {
Ethan Nicholas762466e2017-06-29 10:03:38 -040050 if (type.kind() == Type::kSampler_Kind) {
51 return "TextureSampler";
Ethan Nicholasc9472af2017-10-10 16:30:21 -040052 } else if (type == *context.fFragmentProcessor_Type) {
53 // we don't store fragment processors in fields, they get registered via
54 // registerChildProcessor instead
55 ASSERT(false);
56 return "<error>";
Ethan Nicholas762466e2017-06-29 10:03:38 -040057 }
Ethan Nicholasd608c092017-10-26 09:30:08 -040058 return ParameterType(context, type, layout);
Ethan Nicholas762466e2017-06-29 10:03:38 -040059}
60
61void HCodeGenerator::writef(const char* s, va_list va) {
62 static constexpr int BUFFER_SIZE = 1024;
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040063 va_list copy;
64 va_copy(copy, va);
Ethan Nicholas762466e2017-06-29 10:03:38 -040065 char buffer[BUFFER_SIZE];
66 int length = vsnprintf(buffer, BUFFER_SIZE, s, va);
67 if (length < BUFFER_SIZE) {
68 fOut->write(buffer, length);
69 } else {
70 std::unique_ptr<char[]> heap(new char[length + 1]);
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040071 vsprintf(heap.get(), s, copy);
Ethan Nicholas762466e2017-06-29 10:03:38 -040072 fOut->write(heap.get(), length);
73 }
74}
75
76void HCodeGenerator::writef(const char* s, ...) {
77 va_list va;
78 va_start(va, s);
79 this->writef(s, va);
80 va_end(va);
81}
82
83bool HCodeGenerator::writeSection(const char* name, const char* prefix) {
Ethan Nicholas68990be2017-07-13 09:36:52 -040084 const Section* s = fSectionAndParameterHelper.getSection(name);
85 if (s) {
86 this->writef("%s%s", prefix, s->fText.c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -040087 return true;
88 }
89 return false;
90}
91
92void HCodeGenerator::writeExtraConstructorParams(const char* separator) {
93 // super-simple parse, just assume the last token before a comma is the name of a parameter
94 // (which is true as long as there are no multi-parameter template types involved). Will replace
95 // this with something more robust if the need arises.
Ethan Nicholas68990be2017-07-13 09:36:52 -040096 const Section* section = fSectionAndParameterHelper.getSection(CONSTRUCTOR_PARAMS_SECTION);
97 if (section) {
98 const char* s = section->fText.c_str();
Ethan Nicholas762466e2017-06-29 10:03:38 -040099 #define BUFFER_SIZE 64
100 char lastIdentifier[BUFFER_SIZE];
101 int lastIdentifierLength = 0;
102 bool foundBreak = false;
103 while (*s) {
104 char c = *s;
105 ++s;
106 if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
107 c == '_') {
108 if (foundBreak) {
109 lastIdentifierLength = 0;
110 foundBreak = false;
111 }
112 ASSERT(lastIdentifierLength < BUFFER_SIZE);
113 lastIdentifier[lastIdentifierLength] = c;
114 ++lastIdentifierLength;
115 } else {
116 foundBreak = true;
117 if (c == ',') {
118 ASSERT(lastIdentifierLength < BUFFER_SIZE);
119 lastIdentifier[lastIdentifierLength] = 0;
120 this->writef("%s%s", separator, lastIdentifier);
121 separator = ", ";
122 } else if (c != ' ' && c != '\t' && c != '\n' && c != '\r') {
123 lastIdentifierLength = 0;
124 }
125 }
126 }
127 if (lastIdentifierLength) {
128 ASSERT(lastIdentifierLength < BUFFER_SIZE);
129 lastIdentifier[lastIdentifierLength] = 0;
130 this->writef("%s%s", separator, lastIdentifier);
131 }
132 }
133}
134
135void HCodeGenerator::writeMake() {
136 const char* separator;
137 if (!this->writeSection(MAKE_SECTION)) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400138 this->writef(" static std::unique_ptr<GrFragmentProcessor> Make(");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400139 separator = "";
Ethan Nicholas68990be2017-07-13 09:36:52 -0400140 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasd608c092017-10-26 09:30:08 -0400141 this->writef("%s%s %s", separator, ParameterType(fContext, param->fType,
142 param->fModifiers.fLayout).c_str(),
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700143 String(param->fName).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400144 separator = ", ";
145 }
146 this->writeSection(CONSTRUCTOR_PARAMS_SECTION, separator);
147 this->writef(") {\n"
Brian Salomonaff329b2017-08-11 09:40:37 -0400148 " return std::unique_ptr<GrFragmentProcessor>(new %s(",
Ethan Nicholas762466e2017-06-29 10:03:38 -0400149 fFullName.c_str());
150 separator = "";
Ethan Nicholas68990be2017-07-13 09:36:52 -0400151 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400152 if (param->fType == *fContext.fFragmentProcessor_Type) {
Robert Phillips7a59f232017-11-08 15:31:30 -0500153 this->writef("%sstd::move(%s)", separator, String(param->fName).c_str());
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400154 } else {
155 this->writef("%s%s", separator, String(param->fName).c_str());
156 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400157 separator = ", ";
158 }
159 this->writeExtraConstructorParams(separator);
160 this->writef("));\n"
161 " }\n");
162 }
163}
164
Ethan Nicholas68990be2017-07-13 09:36:52 -0400165void HCodeGenerator::failOnSection(const char* section, const char* msg) {
166 std::vector<const Section*> s = fSectionAndParameterHelper.getSections(section);
167 if (s.size()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700168 fErrors.error(s[0]->fOffset, String("@") + section + " " + msg);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400169 }
170}
171
Ethan Nicholas762466e2017-06-29 10:03:38 -0400172void HCodeGenerator::writeConstructor() {
173 if (this->writeSection(CONSTRUCTOR_SECTION)) {
Ethan Nicholas68990be2017-07-13 09:36:52 -0400174 const char* msg = "may not be present when constructor is overridden";
175 this->failOnSection(CONSTRUCTOR_CODE_SECTION, msg);
176 this->failOnSection(CONSTRUCTOR_PARAMS_SECTION, msg);
177 this->failOnSection(COORD_TRANSFORM_SECTION, msg);
178 this->failOnSection(INITIALIZERS_SECTION, msg);
179 this->failOnSection(OPTIMIZATION_FLAGS_SECTION, msg);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400180 }
181 this->writef(" %s(", fFullName.c_str());
182 const char* separator = "";
Ethan Nicholas68990be2017-07-13 09:36:52 -0400183 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasd608c092017-10-26 09:30:08 -0400184 this->writef("%s%s %s", separator, ParameterType(fContext, param->fType,
185 param->fModifiers.fLayout).c_str(),
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700186 String(param->fName).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400187 separator = ", ";
188 }
189 this->writeSection(CONSTRUCTOR_PARAMS_SECTION, separator);
190 this->writef(")\n"
Ethan Nicholasabff9562017-10-09 10:54:08 -0400191 " : INHERITED(k%s_ClassID", fFullName.c_str());
192 if (!this->writeSection(OPTIMIZATION_FLAGS_SECTION, ", (OptimizationFlags) ")) {
193 this->writef(", kNone_OptimizationFlags");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400194 }
195 this->writef(")");
196 this->writeSection(INITIALIZERS_SECTION, "\n , ");
Ethan Nicholas68990be2017-07-13 09:36:52 -0400197 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700198 String nameString(param->fName);
199 const char* name = nameString.c_str();
Ethan Nicholas762466e2017-06-29 10:03:38 -0400200 if (param->fType.kind() == Type::kSampler_Kind) {
Ethan Nicholas68990be2017-07-13 09:36:52 -0400201 this->writef("\n , %s(std::move(%s)", FieldName(name).c_str(), name);
202 for (const Section* s : fSectionAndParameterHelper.getSections(
203 SAMPLER_PARAMS_SECTION)) {
204 if (s->fArgument == name) {
205 this->writef(", %s", s->fText.c_str());
206 }
207 }
208 this->writef(")");
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400209 } else if (param->fType == *fContext.fFragmentProcessor_Type) {
210 // do nothing
Ethan Nicholas762466e2017-06-29 10:03:38 -0400211 } else {
212 this->writef("\n , %s(%s)", FieldName(name).c_str(), name);
213 }
214 }
Ethan Nicholas68990be2017-07-13 09:36:52 -0400215 for (const Section* s : fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION)) {
216 String field = FieldName(s->fArgument.c_str());
217 this->writef("\n , %sCoordTransform(%s, %s.proxy())", field.c_str(), s->fText.c_str(),
218 field.c_str());
219 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400220 this->writef(" {\n");
221 this->writeSection(CONSTRUCTOR_CODE_SECTION);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400222 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400223 if (param->fType.kind() == Type::kSampler_Kind) {
224 this->writef(" this->addTextureSampler(&%s);\n",
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700225 FieldName(String(param->fName).c_str()).c_str());
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400226 } else if (param->fType == *fContext.fFragmentProcessor_Type) {
227 this->writef(" this->registerChildProcessor(std::move(%s));",
228 String(param->fName).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400229 }
230 }
Ethan Nicholas68990be2017-07-13 09:36:52 -0400231 for (const Section* s : fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION)) {
232 String field = FieldName(s->fArgument.c_str());
233 this->writef(" this->addCoordTransform(&%sCoordTransform);\n", field.c_str());
234 }
Ethan Nicholasabff9562017-10-09 10:54:08 -0400235 this->writef(" }\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400236}
237
238void HCodeGenerator::writeFields() {
239 this->writeSection(FIELDS_SECTION);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400240 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400241 if (param->fType == *fContext.fFragmentProcessor_Type) {
242 continue;
243 }
Ethan Nicholasd608c092017-10-26 09:30:08 -0400244 this->writef(" %s %s;\n", FieldType(fContext, param->fType,
245 param->fModifiers.fLayout).c_str(),
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700246 FieldName(String(param->fName).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400247 }
Ethan Nicholas68990be2017-07-13 09:36:52 -0400248 for (const Section* s : fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION)) {
249 this->writef(" GrCoordTransform %sCoordTransform;\n",
250 FieldName(s->fArgument.c_str()).c_str());
251 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400252}
253
254bool HCodeGenerator::generateCode() {
255 this->writef(kFragmentProcessorHeader, fFullName.c_str());
256 this->writef("#ifndef %s_DEFINED\n"
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400257 "#define %s_DEFINED\n",
258 fFullName.c_str(),
259 fFullName.c_str());
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400260 this->writef("#include \"SkTypes.h\"\n"
261 "#if SK_SUPPORT_GPU\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400262 this->writeSection(HEADER_SECTION);
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400263 this->writef("#include \"GrFragmentProcessor.h\"\n"
Brian Osman1cb41712017-10-19 12:54:52 -0400264 "#include \"GrCoordTransform.h\"\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400265 this->writef("class %s : public GrFragmentProcessor {\n"
266 "public:\n",
267 fFullName.c_str());
268 this->writeSection(CLASS_SECTION);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400269 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400270 if (param->fType.kind() == Type::kSampler_Kind ||
271 param->fType.kind() == Type::kOther_Kind) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400272 continue;
273 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700274 String nameString(param->fName);
275 const char* name = nameString.c_str();
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400276 this->writef(" %s %s() const { return %s; }\n",
Ethan Nicholasd608c092017-10-26 09:30:08 -0400277 FieldType(fContext, param->fType, param->fModifiers.fLayout).c_str(), name,
278 FieldName(name).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400279 }
280 this->writeMake();
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400281 this->writef(" %s(const %s& src);\n"
Brian Salomonaff329b2017-08-11 09:40:37 -0400282 " std::unique_ptr<GrFragmentProcessor> clone() const override;\n"
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400283 " const char* name() const override { return \"%s\"; }\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400284 "private:\n",
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400285 fFullName.c_str(), fFullName.c_str(), fName.c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400286 this->writeConstructor();
287 this->writef(" GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;\n"
288 " void onGetGLSLProcessorKey(const GrShaderCaps&,"
289 "GrProcessorKeyBuilder*) const override;\n"
290 " bool onIsEqual(const GrFragmentProcessor&) const override;\n"
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400291 " GR_DECLARE_FRAGMENT_PROCESSOR_TEST\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400292 this->writeFields();
293 this->writef(" typedef GrFragmentProcessor INHERITED;\n"
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400294 "};\n");
295 this->writeSection(HEADER_END_SECTION);
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400296 this->writef("#endif\n"
297 "#endif\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400298 return 0 == fErrors.errorCount();
299}
300
301} // namespace