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