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