blob: 482020af7ff708cd40529c11535404220e66c5b6 [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
18HCodeGenerator::HCodeGenerator(const Program* program, ErrorReporter* errors, String name,
19 OutputStream* out)
20: INHERITED(program, errors, out)
21, fName(std::move(name))
22, fFullName(String::printf("Gr%s", fName.c_str()))
23, fSectionAndParameterHelper(*program, *errors) {}
24
25String HCodeGenerator::ParameterType(const Type& type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070026 if (type.name() == "float2") {
Ethan Nicholas762466e2017-06-29 10:03:38 -040027 return "SkPoint";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070028 } else if (type.name() == "int4") {
Ethan Nicholas762466e2017-06-29 10:03:38 -040029 return "SkIRect";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070030 } else if (type.name() == "float4") {
Ethan Nicholas762466e2017-06-29 10:03:38 -040031 return "SkRect";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070032 } else if (type.name() == "float4x4") {
Ethan Nicholas762466e2017-06-29 10:03:38 -040033 return "SkMatrix44";
34 } else if (type.kind() == Type::kSampler_Kind) {
35 return "sk_sp<GrTextureProxy>";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070036 } else if (type.name() == "colorSpaceXform") {
Ethan Nicholas762466e2017-06-29 10:03:38 -040037 return "sk_sp<GrColorSpaceXform>";
38 }
39 return type.name();
40}
41
42String HCodeGenerator::FieldType(const Type& type) {
43 if (type.kind() == Type::kSampler_Kind) {
44 return "TextureSampler";
45 }
46 return ParameterType(type);
47}
48
49void HCodeGenerator::writef(const char* s, va_list va) {
50 static constexpr int BUFFER_SIZE = 1024;
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040051 va_list copy;
52 va_copy(copy, va);
Ethan Nicholas762466e2017-06-29 10:03:38 -040053 char buffer[BUFFER_SIZE];
54 int length = vsnprintf(buffer, BUFFER_SIZE, s, va);
55 if (length < BUFFER_SIZE) {
56 fOut->write(buffer, length);
57 } else {
58 std::unique_ptr<char[]> heap(new char[length + 1]);
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040059 vsprintf(heap.get(), s, copy);
Ethan Nicholas762466e2017-06-29 10:03:38 -040060 fOut->write(heap.get(), length);
61 }
62}
63
64void HCodeGenerator::writef(const char* s, ...) {
65 va_list va;
66 va_start(va, s);
67 this->writef(s, va);
68 va_end(va);
69}
70
71bool HCodeGenerator::writeSection(const char* name, const char* prefix) {
Ethan Nicholas68990be2017-07-13 09:36:52 -040072 const Section* s = fSectionAndParameterHelper.getSection(name);
73 if (s) {
74 this->writef("%s%s", prefix, s->fText.c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -040075 return true;
76 }
77 return false;
78}
79
80void HCodeGenerator::writeExtraConstructorParams(const char* separator) {
81 // super-simple parse, just assume the last token before a comma is the name of a parameter
82 // (which is true as long as there are no multi-parameter template types involved). Will replace
83 // this with something more robust if the need arises.
Ethan Nicholas68990be2017-07-13 09:36:52 -040084 const Section* section = fSectionAndParameterHelper.getSection(CONSTRUCTOR_PARAMS_SECTION);
85 if (section) {
86 const char* s = section->fText.c_str();
Ethan Nicholas762466e2017-06-29 10:03:38 -040087 #define BUFFER_SIZE 64
88 char lastIdentifier[BUFFER_SIZE];
89 int lastIdentifierLength = 0;
90 bool foundBreak = false;
91 while (*s) {
92 char c = *s;
93 ++s;
94 if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
95 c == '_') {
96 if (foundBreak) {
97 lastIdentifierLength = 0;
98 foundBreak = false;
99 }
100 ASSERT(lastIdentifierLength < BUFFER_SIZE);
101 lastIdentifier[lastIdentifierLength] = c;
102 ++lastIdentifierLength;
103 } else {
104 foundBreak = true;
105 if (c == ',') {
106 ASSERT(lastIdentifierLength < BUFFER_SIZE);
107 lastIdentifier[lastIdentifierLength] = 0;
108 this->writef("%s%s", separator, lastIdentifier);
109 separator = ", ";
110 } else if (c != ' ' && c != '\t' && c != '\n' && c != '\r') {
111 lastIdentifierLength = 0;
112 }
113 }
114 }
115 if (lastIdentifierLength) {
116 ASSERT(lastIdentifierLength < BUFFER_SIZE);
117 lastIdentifier[lastIdentifierLength] = 0;
118 this->writef("%s%s", separator, lastIdentifier);
119 }
120 }
121}
122
123void HCodeGenerator::writeMake() {
124 const char* separator;
125 if (!this->writeSection(MAKE_SECTION)) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400126 this->writef(" static std::unique_ptr<GrFragmentProcessor> Make(");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400127 separator = "";
Ethan Nicholas68990be2017-07-13 09:36:52 -0400128 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400129 this->writef("%s%s %s", separator, ParameterType(param->fType).c_str(),
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700130 String(param->fName).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400131 separator = ", ";
132 }
133 this->writeSection(CONSTRUCTOR_PARAMS_SECTION, separator);
134 this->writef(") {\n"
Brian Salomonaff329b2017-08-11 09:40:37 -0400135 " return std::unique_ptr<GrFragmentProcessor>(new %s(",
Ethan Nicholas762466e2017-06-29 10:03:38 -0400136 fFullName.c_str());
137 separator = "";
Ethan Nicholas68990be2017-07-13 09:36:52 -0400138 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700139 this->writef("%s%s", separator, String(param->fName).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400140 separator = ", ";
141 }
142 this->writeExtraConstructorParams(separator);
143 this->writef("));\n"
144 " }\n");
145 }
146}
147
Ethan Nicholas68990be2017-07-13 09:36:52 -0400148void HCodeGenerator::failOnSection(const char* section, const char* msg) {
149 std::vector<const Section*> s = fSectionAndParameterHelper.getSections(section);
150 if (s.size()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700151 fErrors.error(s[0]->fOffset, String("@") + section + " " + msg);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400152 }
153}
154
Ethan Nicholas762466e2017-06-29 10:03:38 -0400155void HCodeGenerator::writeConstructor() {
156 if (this->writeSection(CONSTRUCTOR_SECTION)) {
Ethan Nicholas68990be2017-07-13 09:36:52 -0400157 const char* msg = "may not be present when constructor is overridden";
158 this->failOnSection(CONSTRUCTOR_CODE_SECTION, msg);
159 this->failOnSection(CONSTRUCTOR_PARAMS_SECTION, msg);
160 this->failOnSection(COORD_TRANSFORM_SECTION, msg);
161 this->failOnSection(INITIALIZERS_SECTION, msg);
162 this->failOnSection(OPTIMIZATION_FLAGS_SECTION, msg);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400163 }
164 this->writef(" %s(", fFullName.c_str());
165 const char* separator = "";
Ethan Nicholas68990be2017-07-13 09:36:52 -0400166 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400167 this->writef("%s%s %s", separator, ParameterType(param->fType).c_str(),
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700168 String(param->fName).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400169 separator = ", ";
170 }
171 this->writeSection(CONSTRUCTOR_PARAMS_SECTION, separator);
172 this->writef(")\n"
173 " : INHERITED(");
174 if (!this->writeSection(OPTIMIZATION_FLAGS_SECTION, "(OptimizationFlags) ")) {
175 this->writef("kNone_OptimizationFlags");
176 }
177 this->writef(")");
178 this->writeSection(INITIALIZERS_SECTION, "\n , ");
Ethan Nicholas68990be2017-07-13 09:36:52 -0400179 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700180 String nameString(param->fName);
181 const char* name = nameString.c_str();
Ethan Nicholas762466e2017-06-29 10:03:38 -0400182 if (param->fType.kind() == Type::kSampler_Kind) {
Ethan Nicholas68990be2017-07-13 09:36:52 -0400183 this->writef("\n , %s(std::move(%s)", FieldName(name).c_str(), name);
184 for (const Section* s : fSectionAndParameterHelper.getSections(
185 SAMPLER_PARAMS_SECTION)) {
186 if (s->fArgument == name) {
187 this->writef(", %s", s->fText.c_str());
188 }
189 }
190 this->writef(")");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400191 } else {
192 this->writef("\n , %s(%s)", FieldName(name).c_str(), name);
193 }
194 }
Ethan Nicholas68990be2017-07-13 09:36:52 -0400195 for (const Section* s : fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION)) {
196 String field = FieldName(s->fArgument.c_str());
197 this->writef("\n , %sCoordTransform(%s, %s.proxy())", field.c_str(), s->fText.c_str(),
198 field.c_str());
199 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400200 this->writef(" {\n");
201 this->writeSection(CONSTRUCTOR_CODE_SECTION);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400202 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400203 if (param->fType.kind() == Type::kSampler_Kind) {
204 this->writef(" this->addTextureSampler(&%s);\n",
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700205 FieldName(String(param->fName).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400206 }
207 }
Ethan Nicholas68990be2017-07-13 09:36:52 -0400208 for (const Section* s : fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION)) {
209 String field = FieldName(s->fArgument.c_str());
210 this->writef(" this->addCoordTransform(&%sCoordTransform);\n", field.c_str());
211 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400212 this->writef(" this->initClassID<%s>();\n"
213 " }\n",
214 fFullName.c_str());
215}
216
217void HCodeGenerator::writeFields() {
218 this->writeSection(FIELDS_SECTION);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400219 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700220 this->writef(" %s %s;\n", FieldType(param->fType).c_str(),
221 FieldName(String(param->fName).c_str()).c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400222 }
Ethan Nicholas68990be2017-07-13 09:36:52 -0400223 for (const Section* s : fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION)) {
224 this->writef(" GrCoordTransform %sCoordTransform;\n",
225 FieldName(s->fArgument.c_str()).c_str());
226 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400227}
228
229bool HCodeGenerator::generateCode() {
230 this->writef(kFragmentProcessorHeader, fFullName.c_str());
231 this->writef("#ifndef %s_DEFINED\n"
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400232 "#define %s_DEFINED\n",
233 fFullName.c_str(),
234 fFullName.c_str());
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400235 this->writef("#include \"SkTypes.h\"\n"
236 "#if SK_SUPPORT_GPU\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400237 this->writeSection(HEADER_SECTION);
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400238 this->writef("#include \"GrFragmentProcessor.h\"\n"
239 "#include \"GrCoordTransform.h\"\n"
Brian Salomon71603cc2017-07-27 22:23:39 -0400240 "#include \"GrColorSpaceXform.h\"\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400241 this->writef("class %s : public GrFragmentProcessor {\n"
242 "public:\n",
243 fFullName.c_str());
244 this->writeSection(CLASS_SECTION);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400245 for (const auto& param : fSectionAndParameterHelper.getParameters()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400246 if (param->fType.kind() == Type::kSampler_Kind) {
247 continue;
248 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700249 String nameString(param->fName);
250 const char* name = nameString.c_str();
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400251 this->writef(" %s %s() const { return %s; }\n",
Ethan Nicholas762466e2017-06-29 10:03:38 -0400252 FieldType(param->fType).c_str(), name, FieldName(name).c_str());
253 }
254 this->writeMake();
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400255 this->writef(" %s(const %s& src);\n"
Brian Salomonaff329b2017-08-11 09:40:37 -0400256 " std::unique_ptr<GrFragmentProcessor> clone() const override;\n"
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400257 " const char* name() const override { return \"%s\"; }\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400258 "private:\n",
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400259 fFullName.c_str(), fFullName.c_str(), fName.c_str());
Ethan Nicholas762466e2017-06-29 10:03:38 -0400260 this->writeConstructor();
261 this->writef(" GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;\n"
262 " void onGetGLSLProcessorKey(const GrShaderCaps&,"
263 "GrProcessorKeyBuilder*) const override;\n"
264 " bool onIsEqual(const GrFragmentProcessor&) const override;\n"
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400265 " GR_DECLARE_FRAGMENT_PROCESSOR_TEST\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400266 this->writeFields();
267 this->writef(" typedef GrFragmentProcessor INHERITED;\n"
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400268 "};\n");
269 this->writeSection(HEADER_END_SECTION);
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400270 this->writef("#endif\n"
271 "#endif\n");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400272 return 0 == fErrors.errorCount();
273}
274
275} // namespace