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 | |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 10 | #include "SkSLParser.h" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 11 | #include "SkSLUtil.h" |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 12 | #include "ir/SkSLEnum.h" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 13 | #include "ir/SkSLFunctionDeclaration.h" |
| 14 | #include "ir/SkSLFunctionDefinition.h" |
| 15 | #include "ir/SkSLSection.h" |
| 16 | #include "ir/SkSLVarDeclarations.h" |
| 17 | |
| 18 | namespace SkSL { |
| 19 | |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 20 | HCodeGenerator::HCodeGenerator(const Context* context, const Program* program, |
| 21 | ErrorReporter* errors, String name, OutputStream* out) |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 22 | : INHERITED(program, errors, out) |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 23 | , fContext(*context) |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 24 | , fName(std::move(name)) |
| 25 | , fFullName(String::printf("Gr%s", fName.c_str())) |
| 26 | , fSectionAndParameterHelper(*program, *errors) {} |
| 27 | |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 28 | String HCodeGenerator::ParameterType(const Context& context, const Type& type, |
| 29 | const Layout& layout) { |
| 30 | if (layout.fCType != "") { |
| 31 | return layout.fCType; |
| 32 | } else if (type == *context.fFloat_Type || type == *context.fHalf_Type) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 33 | return "float"; |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 34 | } else if (type == *context.fFloat2_Type || type == *context.fHalf2_Type) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 35 | return "SkPoint"; |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame^] | 36 | } else if (type == *context.fInt4_Type || |
| 37 | type == *context.fShort4_Type || |
| 38 | type == *context.fByte4_Type) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 39 | return "SkIRect"; |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 40 | } else if (type == *context.fFloat4_Type || type == *context.fHalf4_Type) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 41 | return "SkRect"; |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 42 | } else if (type == *context.fFloat4x4_Type || type == *context.fHalf4x4_Type) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 43 | return "SkMatrix44"; |
| 44 | } else if (type.kind() == Type::kSampler_Kind) { |
| 45 | return "sk_sp<GrTextureProxy>"; |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 46 | } else if (type == *context.fFragmentProcessor_Type) { |
| 47 | return "std::unique_ptr<GrFragmentProcessor>"; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 48 | } |
| 49 | return type.name(); |
| 50 | } |
| 51 | |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 52 | String HCodeGenerator::FieldType(const Context& context, const Type& type, |
| 53 | const Layout& layout) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 54 | if (type.kind() == Type::kSampler_Kind) { |
| 55 | return "TextureSampler"; |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 56 | } else if (type == *context.fFragmentProcessor_Type) { |
| 57 | // we don't store fragment processors in fields, they get registered via |
| 58 | // registerChildProcessor instead |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 59 | SkASSERT(false); |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 60 | return "<error>"; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 61 | } |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 62 | return ParameterType(context, type, layout); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void HCodeGenerator::writef(const char* s, va_list va) { |
| 66 | static constexpr int BUFFER_SIZE = 1024; |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 67 | va_list copy; |
| 68 | va_copy(copy, va); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 69 | char buffer[BUFFER_SIZE]; |
| 70 | int length = vsnprintf(buffer, BUFFER_SIZE, s, va); |
| 71 | if (length < BUFFER_SIZE) { |
| 72 | fOut->write(buffer, length); |
| 73 | } else { |
| 74 | std::unique_ptr<char[]> heap(new char[length + 1]); |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 75 | vsprintf(heap.get(), s, copy); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 76 | fOut->write(heap.get(), length); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void HCodeGenerator::writef(const char* s, ...) { |
| 81 | va_list va; |
| 82 | va_start(va, s); |
| 83 | this->writef(s, va); |
| 84 | va_end(va); |
| 85 | } |
| 86 | |
| 87 | bool HCodeGenerator::writeSection(const char* name, const char* prefix) { |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 88 | const Section* s = fSectionAndParameterHelper.getSection(name); |
| 89 | if (s) { |
| 90 | this->writef("%s%s", prefix, s->fText.c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 91 | return true; |
| 92 | } |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | void HCodeGenerator::writeExtraConstructorParams(const char* separator) { |
| 97 | // super-simple parse, just assume the last token before a comma is the name of a parameter |
| 98 | // (which is true as long as there are no multi-parameter template types involved). Will replace |
| 99 | // this with something more robust if the need arises. |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 100 | const Section* section = fSectionAndParameterHelper.getSection(CONSTRUCTOR_PARAMS_SECTION); |
| 101 | if (section) { |
| 102 | const char* s = section->fText.c_str(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 103 | #define BUFFER_SIZE 64 |
| 104 | char lastIdentifier[BUFFER_SIZE]; |
| 105 | int lastIdentifierLength = 0; |
| 106 | bool foundBreak = false; |
| 107 | while (*s) { |
| 108 | char c = *s; |
| 109 | ++s; |
| 110 | if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || |
| 111 | c == '_') { |
| 112 | if (foundBreak) { |
| 113 | lastIdentifierLength = 0; |
| 114 | foundBreak = false; |
| 115 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 116 | SkASSERT(lastIdentifierLength < BUFFER_SIZE); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 117 | lastIdentifier[lastIdentifierLength] = c; |
| 118 | ++lastIdentifierLength; |
| 119 | } else { |
| 120 | foundBreak = true; |
| 121 | if (c == ',') { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 122 | SkASSERT(lastIdentifierLength < BUFFER_SIZE); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 123 | lastIdentifier[lastIdentifierLength] = 0; |
| 124 | this->writef("%s%s", separator, lastIdentifier); |
| 125 | separator = ", "; |
| 126 | } else if (c != ' ' && c != '\t' && c != '\n' && c != '\r') { |
| 127 | lastIdentifierLength = 0; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | if (lastIdentifierLength) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 132 | SkASSERT(lastIdentifierLength < BUFFER_SIZE); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 133 | lastIdentifier[lastIdentifierLength] = 0; |
| 134 | this->writef("%s%s", separator, lastIdentifier); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void HCodeGenerator::writeMake() { |
| 140 | const char* separator; |
| 141 | if (!this->writeSection(MAKE_SECTION)) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 142 | this->writef(" static std::unique_ptr<GrFragmentProcessor> Make("); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 143 | separator = ""; |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 144 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 145 | this->writef("%s%s %s", separator, ParameterType(fContext, param->fType, |
| 146 | param->fModifiers.fLayout).c_str(), |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 147 | String(param->fName).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 148 | separator = ", "; |
| 149 | } |
| 150 | this->writeSection(CONSTRUCTOR_PARAMS_SECTION, separator); |
| 151 | this->writef(") {\n" |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 152 | " return std::unique_ptr<GrFragmentProcessor>(new %s(", |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 153 | fFullName.c_str()); |
| 154 | separator = ""; |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 155 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 156 | if (param->fType == *fContext.fFragmentProcessor_Type) { |
Robert Phillips | 7a59f23 | 2017-11-08 15:31:30 -0500 | [diff] [blame] | 157 | this->writef("%sstd::move(%s)", separator, String(param->fName).c_str()); |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 158 | } else { |
| 159 | this->writef("%s%s", separator, String(param->fName).c_str()); |
| 160 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 161 | separator = ", "; |
| 162 | } |
| 163 | this->writeExtraConstructorParams(separator); |
| 164 | this->writef("));\n" |
| 165 | " }\n"); |
| 166 | } |
| 167 | } |
| 168 | |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 169 | void HCodeGenerator::failOnSection(const char* section, const char* msg) { |
| 170 | std::vector<const Section*> s = fSectionAndParameterHelper.getSections(section); |
| 171 | if (s.size()) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 172 | fErrors.error(s[0]->fOffset, String("@") + section + " " + msg); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 176 | void HCodeGenerator::writeConstructor() { |
| 177 | if (this->writeSection(CONSTRUCTOR_SECTION)) { |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 178 | const char* msg = "may not be present when constructor is overridden"; |
| 179 | this->failOnSection(CONSTRUCTOR_CODE_SECTION, msg); |
| 180 | this->failOnSection(CONSTRUCTOR_PARAMS_SECTION, msg); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 181 | this->failOnSection(INITIALIZERS_SECTION, msg); |
| 182 | this->failOnSection(OPTIMIZATION_FLAGS_SECTION, msg); |
Robert Phillips | 1e8501e | 2018-03-23 15:00:20 -0400 | [diff] [blame] | 183 | return; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 184 | } |
| 185 | this->writef(" %s(", fFullName.c_str()); |
| 186 | const char* separator = ""; |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 187 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 188 | this->writef("%s%s %s", separator, ParameterType(fContext, param->fType, |
| 189 | param->fModifiers.fLayout).c_str(), |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 190 | String(param->fName).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 191 | separator = ", "; |
| 192 | } |
| 193 | this->writeSection(CONSTRUCTOR_PARAMS_SECTION, separator); |
| 194 | this->writef(")\n" |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 195 | " : INHERITED(k%s_ClassID", fFullName.c_str()); |
| 196 | if (!this->writeSection(OPTIMIZATION_FLAGS_SECTION, ", (OptimizationFlags) ")) { |
| 197 | this->writef(", kNone_OptimizationFlags"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 198 | } |
| 199 | this->writef(")"); |
| 200 | this->writeSection(INITIALIZERS_SECTION, "\n , "); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 201 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 202 | String nameString(param->fName); |
| 203 | const char* name = nameString.c_str(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 204 | if (param->fType.kind() == Type::kSampler_Kind) { |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 205 | this->writef("\n , %s(std::move(%s)", FieldName(name).c_str(), name); |
| 206 | for (const Section* s : fSectionAndParameterHelper.getSections( |
| 207 | SAMPLER_PARAMS_SECTION)) { |
| 208 | if (s->fArgument == name) { |
| 209 | this->writef(", %s", s->fText.c_str()); |
| 210 | } |
| 211 | } |
| 212 | this->writef(")"); |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 213 | } else if (param->fType == *fContext.fFragmentProcessor_Type) { |
| 214 | // do nothing |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 215 | } else { |
| 216 | this->writef("\n , %s(%s)", FieldName(name).c_str(), name); |
| 217 | } |
| 218 | } |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 219 | for (const Section* s : fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION)) { |
| 220 | String field = FieldName(s->fArgument.c_str()); |
| 221 | this->writef("\n , %sCoordTransform(%s, %s.proxy())", field.c_str(), s->fText.c_str(), |
| 222 | field.c_str()); |
| 223 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 224 | this->writef(" {\n"); |
| 225 | this->writeSection(CONSTRUCTOR_CODE_SECTION); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 226 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 227 | if (param->fType.kind() == Type::kSampler_Kind) { |
| 228 | this->writef(" this->addTextureSampler(&%s);\n", |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 229 | FieldName(String(param->fName).c_str()).c_str()); |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 230 | } else if (param->fType == *fContext.fFragmentProcessor_Type) { |
| 231 | this->writef(" this->registerChildProcessor(std::move(%s));", |
| 232 | String(param->fName).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 233 | } |
| 234 | } |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 235 | for (const Section* s : fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION)) { |
| 236 | String field = FieldName(s->fArgument.c_str()); |
| 237 | this->writef(" this->addCoordTransform(&%sCoordTransform);\n", field.c_str()); |
| 238 | } |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 239 | this->writef(" }\n"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | void HCodeGenerator::writeFields() { |
| 243 | this->writeSection(FIELDS_SECTION); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 244 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 245 | if (param->fType == *fContext.fFragmentProcessor_Type) { |
| 246 | continue; |
| 247 | } |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 248 | this->writef(" %s %s;\n", FieldType(fContext, param->fType, |
| 249 | param->fModifiers.fLayout).c_str(), |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 250 | FieldName(String(param->fName).c_str()).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 251 | } |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 252 | for (const Section* s : fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION)) { |
| 253 | this->writef(" GrCoordTransform %sCoordTransform;\n", |
| 254 | FieldName(s->fArgument.c_str()).c_str()); |
| 255 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 256 | } |
| 257 | |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 258 | String HCodeGenerator::GetHeader(const Program& program, ErrorReporter& errors) { |
| 259 | SymbolTable types(&errors); |
| 260 | Parser parser(program.fSource->c_str(), program.fSource->length(), types, errors); |
| 261 | for (;;) { |
| 262 | Token header = parser.nextRawToken(); |
| 263 | switch (header.fKind) { |
| 264 | case Token::WHITESPACE: |
| 265 | break; |
| 266 | case Token::BLOCK_COMMENT: |
| 267 | return String(program.fSource->c_str() + header.fOffset, header.fLength); |
| 268 | default: |
| 269 | return ""; |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 274 | bool HCodeGenerator::generateCode() { |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 275 | this->writef("%s\n", GetHeader(fProgram, fErrors).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 276 | this->writef(kFragmentProcessorHeader, fFullName.c_str()); |
| 277 | this->writef("#ifndef %s_DEFINED\n" |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 278 | "#define %s_DEFINED\n", |
| 279 | fFullName.c_str(), |
| 280 | fFullName.c_str()); |
Greg Daniel | 3e8c345 | 2018-04-06 10:37:55 -0400 | [diff] [blame] | 281 | this->writef("#include \"SkTypes.h\"\n"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 282 | this->writeSection(HEADER_SECTION); |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 283 | this->writef("#include \"GrFragmentProcessor.h\"\n" |
Brian Osman | 1cb4171 | 2017-10-19 12:54:52 -0400 | [diff] [blame] | 284 | "#include \"GrCoordTransform.h\"\n"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 285 | this->writef("class %s : public GrFragmentProcessor {\n" |
| 286 | "public:\n", |
| 287 | fFullName.c_str()); |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 288 | for (const auto& p : fProgram) { |
| 289 | if (ProgramElement::kEnum_Kind == p.fKind && !((Enum&) p).fBuiltin) { |
| 290 | this->writef("%s\n", p.description().c_str()); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 291 | } |
| 292 | } |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 293 | this->writeSection(CLASS_SECTION); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 294 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 295 | if (param->fType.kind() == Type::kSampler_Kind || |
| 296 | param->fType.kind() == Type::kOther_Kind) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 297 | continue; |
| 298 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 299 | String nameString(param->fName); |
| 300 | const char* name = nameString.c_str(); |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 301 | this->writef(" %s %s() const { return %s; }\n", |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 302 | FieldType(fContext, param->fType, param->fModifiers.fLayout).c_str(), name, |
| 303 | FieldName(name).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 304 | } |
| 305 | this->writeMake(); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 306 | this->writef(" %s(const %s& src);\n" |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 307 | " std::unique_ptr<GrFragmentProcessor> clone() const override;\n" |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 308 | " const char* name() const override { return \"%s\"; }\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 309 | "private:\n", |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 310 | fFullName.c_str(), fFullName.c_str(), fName.c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 311 | this->writeConstructor(); |
| 312 | this->writef(" GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;\n" |
| 313 | " void onGetGLSLProcessorKey(const GrShaderCaps&," |
| 314 | "GrProcessorKeyBuilder*) const override;\n" |
| 315 | " bool onIsEqual(const GrFragmentProcessor&) const override;\n" |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 316 | " GR_DECLARE_FRAGMENT_PROCESSOR_TEST\n"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 317 | this->writeFields(); |
| 318 | this->writef(" typedef GrFragmentProcessor INHERITED;\n" |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 319 | "};\n"); |
| 320 | this->writeSection(HEADER_END_SECTION); |
Greg Daniel | 3e8c345 | 2018-04-06 10:37:55 -0400 | [diff] [blame] | 321 | this->writef("#endif\n"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 322 | return 0 == fErrors.errorCount(); |
| 323 | } |
| 324 | |
| 325 | } // namespace |