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