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