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