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