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