Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 16 | namespace SkSL { |
| 17 | |
| 18 | HCodeGenerator::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 | |
| 25 | String HCodeGenerator::ParameterType(const Type& type) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 26 | if (type.fName == "float2") { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 27 | return "SkPoint"; |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 28 | } else if (type.fName == "int4") { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 29 | return "SkIRect"; |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 30 | } else if (type.fName == "float4") { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 31 | return "SkRect"; |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 32 | } else if (type.fName == "float4x4") { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 33 | return "SkMatrix44"; |
| 34 | } else if (type.kind() == Type::kSampler_Kind) { |
| 35 | return "sk_sp<GrTextureProxy>"; |
| 36 | } else if (type.fName == "colorSpaceXform") { |
| 37 | return "sk_sp<GrColorSpaceXform>"; |
| 38 | } |
| 39 | return type.name(); |
| 40 | } |
| 41 | |
| 42 | String HCodeGenerator::FieldType(const Type& type) { |
| 43 | if (type.kind() == Type::kSampler_Kind) { |
| 44 | return "TextureSampler"; |
| 45 | } |
| 46 | return ParameterType(type); |
| 47 | } |
| 48 | |
| 49 | void HCodeGenerator::writef(const char* s, va_list va) { |
| 50 | static constexpr int BUFFER_SIZE = 1024; |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 51 | va_list copy; |
| 52 | va_copy(copy, va); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 53 | 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 Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 59 | vsprintf(heap.get(), s, copy); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 60 | fOut->write(heap.get(), length); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void 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 | |
| 71 | bool HCodeGenerator::writeSection(const char* name, const char* prefix) { |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 72 | const Section* s = fSectionAndParameterHelper.getSection(name); |
| 73 | if (s) { |
| 74 | this->writef("%s%s", prefix, s->fText.c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 75 | return true; |
| 76 | } |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | void 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 Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 84 | const Section* section = fSectionAndParameterHelper.getSection(CONSTRUCTOR_PARAMS_SECTION); |
| 85 | if (section) { |
| 86 | const char* s = section->fText.c_str(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 87 | #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 | |
| 123 | void HCodeGenerator::writeMake() { |
| 124 | const char* separator; |
| 125 | if (!this->writeSection(MAKE_SECTION)) { |
| 126 | this->writef(" static sk_sp<GrFragmentProcessor> Make("); |
| 127 | separator = ""; |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 128 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 129 | this->writef("%s%s %s", separator, ParameterType(param->fType).c_str(), |
| 130 | param->fName.c_str()); |
| 131 | separator = ", "; |
| 132 | } |
| 133 | this->writeSection(CONSTRUCTOR_PARAMS_SECTION, separator); |
| 134 | this->writef(") {\n" |
| 135 | " return sk_sp<GrFragmentProcessor>(new %s(", |
| 136 | fFullName.c_str()); |
| 137 | separator = ""; |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 138 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 139 | this->writef("%s%s", separator, param->fName.c_str()); |
| 140 | separator = ", "; |
| 141 | } |
| 142 | this->writeExtraConstructorParams(separator); |
| 143 | this->writef("));\n" |
| 144 | " }\n"); |
| 145 | } |
| 146 | } |
| 147 | |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 148 | void HCodeGenerator::failOnSection(const char* section, const char* msg) { |
| 149 | std::vector<const Section*> s = fSectionAndParameterHelper.getSections(section); |
| 150 | if (s.size()) { |
| 151 | fErrors.error(s[0]->fPosition, String("@") + section + " " + msg); |
| 152 | } |
| 153 | } |
| 154 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 155 | void HCodeGenerator::writeConstructor() { |
| 156 | if (this->writeSection(CONSTRUCTOR_SECTION)) { |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 157 | 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 Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 163 | } |
| 164 | this->writef(" %s(", fFullName.c_str()); |
| 165 | const char* separator = ""; |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 166 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 167 | this->writef("%s%s %s", separator, ParameterType(param->fType).c_str(), |
| 168 | param->fName.c_str()); |
| 169 | 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 Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 179 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 180 | const char* name = param->fName.c_str(); |
| 181 | if (param->fType.kind() == Type::kSampler_Kind) { |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 182 | this->writef("\n , %s(std::move(%s)", FieldName(name).c_str(), name); |
| 183 | for (const Section* s : fSectionAndParameterHelper.getSections( |
| 184 | SAMPLER_PARAMS_SECTION)) { |
| 185 | if (s->fArgument == name) { |
| 186 | this->writef(", %s", s->fText.c_str()); |
| 187 | } |
| 188 | } |
| 189 | this->writef(")"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 190 | } else { |
| 191 | this->writef("\n , %s(%s)", FieldName(name).c_str(), name); |
| 192 | } |
| 193 | } |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 194 | for (const Section* s : fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION)) { |
| 195 | String field = FieldName(s->fArgument.c_str()); |
| 196 | this->writef("\n , %sCoordTransform(%s, %s.proxy())", field.c_str(), s->fText.c_str(), |
| 197 | field.c_str()); |
| 198 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 199 | this->writef(" {\n"); |
| 200 | this->writeSection(CONSTRUCTOR_CODE_SECTION); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 201 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 202 | if (param->fType.kind() == Type::kSampler_Kind) { |
| 203 | this->writef(" this->addTextureSampler(&%s);\n", |
| 204 | FieldName(param->fName.c_str()).c_str()); |
| 205 | } |
| 206 | } |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 207 | for (const Section* s : fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION)) { |
| 208 | String field = FieldName(s->fArgument.c_str()); |
| 209 | this->writef(" this->addCoordTransform(&%sCoordTransform);\n", field.c_str()); |
| 210 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 211 | this->writef(" this->initClassID<%s>();\n" |
| 212 | " }\n", |
| 213 | fFullName.c_str()); |
| 214 | } |
| 215 | |
| 216 | void HCodeGenerator::writeFields() { |
| 217 | this->writeSection(FIELDS_SECTION); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 218 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 219 | const char* name = param->fName.c_str(); |
| 220 | this->writef(" %s %s;\n", FieldType(param->fType).c_str(), FieldName(name).c_str()); |
| 221 | } |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 222 | for (const Section* s : fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION)) { |
| 223 | this->writef(" GrCoordTransform %sCoordTransform;\n", |
| 224 | FieldName(s->fArgument.c_str()).c_str()); |
| 225 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | bool HCodeGenerator::generateCode() { |
| 229 | this->writef(kFragmentProcessorHeader, fFullName.c_str()); |
| 230 | this->writef("#ifndef %s_DEFINED\n" |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 231 | "#define %s_DEFINED\n", |
| 232 | fFullName.c_str(), |
| 233 | fFullName.c_str()); |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 234 | this->writef("#include \"SkTypes.h\"\n" |
| 235 | "#if SK_SUPPORT_GPU\n"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 236 | this->writeSection(HEADER_SECTION); |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 237 | this->writef("#include \"GrFragmentProcessor.h\"\n" |
| 238 | "#include \"GrCoordTransform.h\"\n" |
Brian Salomon | 71603cc | 2017-07-27 22:23:39 -0400 | [diff] [blame] | 239 | "#include \"GrColorSpaceXform.h\"\n"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 240 | this->writef("class %s : public GrFragmentProcessor {\n" |
| 241 | "public:\n", |
| 242 | fFullName.c_str()); |
| 243 | this->writeSection(CLASS_SECTION); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 244 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 245 | if (param->fType.kind() == Type::kSampler_Kind) { |
| 246 | continue; |
| 247 | } |
| 248 | const char* name = param->fName.c_str(); |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 249 | this->writef(" %s %s() const { return %s; }\n", |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 250 | FieldType(param->fType).c_str(), name, FieldName(name).c_str()); |
| 251 | } |
| 252 | this->writeMake(); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame^] | 253 | this->writef(" %s(const %s& src);\n" |
| 254 | " sk_sp<GrFragmentProcessor> clone() const override;\n" |
| 255 | " const char* name() const override { return \"%s\"; }\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 256 | "private:\n", |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame^] | 257 | fFullName.c_str(), fFullName.c_str(), fName.c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 258 | this->writeConstructor(); |
| 259 | this->writef(" GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;\n" |
| 260 | " void onGetGLSLProcessorKey(const GrShaderCaps&," |
| 261 | "GrProcessorKeyBuilder*) const override;\n" |
| 262 | " bool onIsEqual(const GrFragmentProcessor&) const override;\n" |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 263 | " GR_DECLARE_FRAGMENT_PROCESSOR_TEST\n"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 264 | this->writeFields(); |
| 265 | this->writef(" typedef GrFragmentProcessor INHERITED;\n" |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 266 | "};\n"); |
| 267 | this->writeSection(HEADER_END_SECTION); |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 268 | this->writef("#endif\n" |
| 269 | "#endif\n"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 270 | return 0 == fErrors.errorCount(); |
| 271 | } |
| 272 | |
| 273 | } // namespace |