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/SkSLCPPCodeGenerator.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/SkSLCPPUniformCTypes.h" |
| 13 | #include "src/sksl/SkSLCompiler.h" |
| 14 | #include "src/sksl/SkSLHCodeGenerator.h" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 15 | |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 16 | #include <algorithm> |
| 17 | |
Ethan Nicholas | 2a479a5 | 2020-08-18 16:29:45 -0400 | [diff] [blame] | 18 | #if defined(SKSL_STANDALONE) || defined(GR_TEST_UTILS) |
| 19 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 20 | namespace SkSL { |
| 21 | |
| 22 | static bool needs_uniform_var(const Variable& var) { |
Ethan Nicholas | 5f9836e | 2017-12-20 15:16:33 -0500 | [diff] [blame] | 23 | return (var.fModifiers.fFlags & Modifiers::kUniform_Flag) && |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 24 | var.type().typeKind() != Type::TypeKind::kSampler; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | CPPCodeGenerator::CPPCodeGenerator(const Context* context, const Program* program, |
| 28 | ErrorReporter* errors, String name, OutputStream* out) |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 29 | : INHERITED(context, program, errors, out) |
| 30 | , fName(std::move(name)) |
| 31 | , fFullName(String::printf("Gr%s", fName.c_str())) |
| 32 | , fSectionAndParameterHelper(program, *errors) { |
| 33 | fLineEnding = "\n"; |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 34 | fTextureFunctionOverride = "sample"; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void CPPCodeGenerator::writef(const char* s, va_list va) { |
| 38 | static constexpr int BUFFER_SIZE = 1024; |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 39 | va_list copy; |
| 40 | va_copy(copy, va); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 41 | char buffer[BUFFER_SIZE]; |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 42 | int length = std::vsnprintf(buffer, BUFFER_SIZE, s, va); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 43 | if (length < BUFFER_SIZE) { |
| 44 | fOut->write(buffer, length); |
| 45 | } else { |
| 46 | std::unique_ptr<char[]> heap(new char[length + 1]); |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 47 | vsprintf(heap.get(), s, copy); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 48 | fOut->write(heap.get(), length); |
| 49 | } |
z102.zhang | d74f2c8 | 2018-08-10 09:08:47 +0800 | [diff] [blame] | 50 | va_end(copy); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void CPPCodeGenerator::writef(const char* s, ...) { |
| 54 | va_list va; |
| 55 | va_start(va, s); |
| 56 | this->writef(s, va); |
| 57 | va_end(va); |
| 58 | } |
| 59 | |
| 60 | void CPPCodeGenerator::writeHeader() { |
| 61 | } |
| 62 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 63 | bool CPPCodeGenerator::usesPrecisionModifiers() const { |
| 64 | return false; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 65 | } |
| 66 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 67 | String CPPCodeGenerator::getTypeName(const Type& type) { |
| 68 | return type.name(); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 69 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 70 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 71 | void CPPCodeGenerator::writeBinaryExpression(const BinaryExpression& b, |
| 72 | Precedence parentPrecedence) { |
Ethan Nicholas | 1d3e0e0 | 2020-09-16 15:19:24 -0400 | [diff] [blame] | 73 | const Expression& left = b.left(); |
| 74 | const Expression& right = b.right(); |
| 75 | Token::Kind op = b.getOperator(); |
| 76 | if (op == Token::Kind::TK_PERCENT) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 77 | // need to use "%%" instead of "%" b/c the code will be inside of a printf |
Ethan Nicholas | 1d3e0e0 | 2020-09-16 15:19:24 -0400 | [diff] [blame] | 78 | Precedence precedence = GetBinaryPrecedence(op); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 79 | if (precedence >= parentPrecedence) { |
| 80 | this->write("("); |
| 81 | } |
Ethan Nicholas | 1d3e0e0 | 2020-09-16 15:19:24 -0400 | [diff] [blame] | 82 | this->writeExpression(left, precedence); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 83 | this->write(" %% "); |
Ethan Nicholas | 1d3e0e0 | 2020-09-16 15:19:24 -0400 | [diff] [blame] | 84 | this->writeExpression(right, precedence); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 85 | if (precedence >= parentPrecedence) { |
| 86 | this->write(")"); |
| 87 | } |
Ethan Nicholas | 1d3e0e0 | 2020-09-16 15:19:24 -0400 | [diff] [blame] | 88 | } else if (left.kind() == Expression::Kind::kNullLiteral || |
| 89 | right.kind() == Expression::Kind::kNullLiteral) { |
Ethan Nicholas | ee1c8a7 | 2019-02-22 10:50:47 -0500 | [diff] [blame] | 90 | const Variable* var; |
Ethan Nicholas | 1d3e0e0 | 2020-09-16 15:19:24 -0400 | [diff] [blame] | 91 | if (left.kind() != Expression::Kind::kNullLiteral) { |
| 92 | var = &left.as<VariableReference>().fVariable; |
Ethan Nicholas | ee1c8a7 | 2019-02-22 10:50:47 -0500 | [diff] [blame] | 93 | } else { |
Ethan Nicholas | 1d3e0e0 | 2020-09-16 15:19:24 -0400 | [diff] [blame] | 94 | var = &right.as<VariableReference>().fVariable; |
Ethan Nicholas | ee1c8a7 | 2019-02-22 10:50:47 -0500 | [diff] [blame] | 95 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 96 | SkASSERT(var->type().typeKind() == Type::TypeKind::kNullable && |
| 97 | var->type().componentType() == *fContext.fFragmentProcessor_Type); |
Ethan Nicholas | ee1c8a7 | 2019-02-22 10:50:47 -0500 | [diff] [blame] | 98 | this->write("%s"); |
Ethan Nicholas | 1d3e0e0 | 2020-09-16 15:19:24 -0400 | [diff] [blame] | 99 | const char* prefix = ""; |
| 100 | switch (op) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 101 | case Token::Kind::TK_EQEQ: |
Ethan Nicholas | 1d3e0e0 | 2020-09-16 15:19:24 -0400 | [diff] [blame] | 102 | prefix = "!"; |
Ethan Nicholas | ee1c8a7 | 2019-02-22 10:50:47 -0500 | [diff] [blame] | 103 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 104 | case Token::Kind::TK_NEQ: |
Ethan Nicholas | 1d3e0e0 | 2020-09-16 15:19:24 -0400 | [diff] [blame] | 105 | prefix = ""; |
Ethan Nicholas | ee1c8a7 | 2019-02-22 10:50:47 -0500 | [diff] [blame] | 106 | break; |
| 107 | default: |
| 108 | SkASSERT(false); |
| 109 | } |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 110 | int childIndex = this->getChildFPIndex(*var); |
Ethan Nicholas | 1d3e0e0 | 2020-09-16 15:19:24 -0400 | [diff] [blame] | 111 | fFormatArgs.push_back(String(prefix) + "_outer.childProcessor(" + to_string(childIndex) + |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 112 | ") ? \"true\" : \"false\""); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 113 | } else { |
| 114 | INHERITED::writeBinaryExpression(b, parentPrecedence); |
| 115 | } |
| 116 | } |
| 117 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 118 | static String default_value(const Type& type) { |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 119 | if (type.fName == "bool") { |
| 120 | return "false"; |
| 121 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 122 | switch (type.typeKind()) { |
| 123 | case Type::TypeKind::kScalar: return "0"; |
| 124 | case Type::TypeKind::kVector: return type.name() + "(0)"; |
| 125 | case Type::TypeKind::kMatrix: return type.name() + "(1)"; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 126 | default: ABORT("unsupported default_value type\n"); |
| 127 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 128 | } |
| 129 | |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 130 | static String default_value(const Variable& var) { |
Brian Osman | 495993a | 2018-10-16 15:45:55 -0400 | [diff] [blame] | 131 | if (var.fModifiers.fLayout.fCType == SkSL::Layout::CType::kSkPMColor4f) { |
Brian Osman | f28e55d | 2018-10-03 16:35:54 -0400 | [diff] [blame] | 132 | return "{SK_FloatNaN, SK_FloatNaN, SK_FloatNaN, SK_FloatNaN}"; |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 133 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 134 | return default_value(var.type()); |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 135 | } |
| 136 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 137 | static bool is_private(const Variable& var) { |
| 138 | return !(var.fModifiers.fFlags & Modifiers::kUniform_Flag) && |
| 139 | !(var.fModifiers.fFlags & Modifiers::kIn_Flag) && |
| 140 | var.fStorage == Variable::kGlobal_Storage && |
| 141 | var.fModifiers.fLayout.fBuiltin == -1; |
| 142 | } |
| 143 | |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 144 | static bool is_uniform_in(const Variable& var) { |
| 145 | return (var.fModifiers.fFlags & Modifiers::kUniform_Flag) && |
| 146 | (var.fModifiers.fFlags & Modifiers::kIn_Flag) && |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 147 | var.type().typeKind() != Type::TypeKind::kSampler; |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 148 | } |
| 149 | |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 150 | String CPPCodeGenerator::formatRuntimeValue(const Type& type, |
| 151 | const Layout& layout, |
| 152 | const String& cppCode, |
| 153 | std::vector<String>* formatArgs) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 154 | if (type.typeKind() == Type::TypeKind::kArray) { |
Ethan Nicholas | 7018bcf | 2020-08-20 15:57:22 -0400 | [diff] [blame] | 155 | String result("["); |
| 156 | const char* separator = ""; |
| 157 | for (int i = 0; i < type.columns(); i++) { |
| 158 | result += separator + this->formatRuntimeValue(type.componentType(), layout, |
| 159 | "(" + cppCode + ")[" + to_string(i) + |
| 160 | "]", formatArgs); |
| 161 | separator = ","; |
| 162 | } |
| 163 | result += "]"; |
| 164 | return result; |
| 165 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 166 | if (type.isFloat()) { |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 167 | formatArgs->push_back(cppCode); |
| 168 | return "%f"; |
| 169 | } |
| 170 | if (type == *fContext.fInt_Type) { |
| 171 | formatArgs->push_back(cppCode); |
| 172 | return "%d"; |
| 173 | } |
| 174 | if (type == *fContext.fBool_Type) { |
| 175 | formatArgs->push_back("(" + cppCode + " ? \"true\" : \"false\")"); |
| 176 | return "%s"; |
| 177 | } |
| 178 | if (type == *fContext.fFloat2_Type || type == *fContext.fHalf2_Type) { |
| 179 | formatArgs->push_back(cppCode + ".fX"); |
| 180 | formatArgs->push_back(cppCode + ".fY"); |
| 181 | return type.name() + "(%f, %f)"; |
| 182 | } |
| 183 | if (type == *fContext.fFloat3_Type || type == *fContext.fHalf3_Type) { |
| 184 | formatArgs->push_back(cppCode + ".fX"); |
| 185 | formatArgs->push_back(cppCode + ".fY"); |
| 186 | formatArgs->push_back(cppCode + ".fZ"); |
| 187 | return type.name() + "(%f, %f, %f)"; |
| 188 | } |
| 189 | if (type == *fContext.fFloat4_Type || type == *fContext.fHalf4_Type) { |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 190 | switch (layout.fCType) { |
| 191 | case Layout::CType::kSkPMColor: |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 192 | formatArgs->push_back("SkGetPackedR32(" + cppCode + ") / 255.0"); |
| 193 | formatArgs->push_back("SkGetPackedG32(" + cppCode + ") / 255.0"); |
| 194 | formatArgs->push_back("SkGetPackedB32(" + cppCode + ") / 255.0"); |
| 195 | formatArgs->push_back("SkGetPackedA32(" + cppCode + ") / 255.0"); |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 196 | break; |
Brian Osman | f28e55d | 2018-10-03 16:35:54 -0400 | [diff] [blame] | 197 | case Layout::CType::kSkPMColor4f: |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 198 | formatArgs->push_back(cppCode + ".fR"); |
| 199 | formatArgs->push_back(cppCode + ".fG"); |
| 200 | formatArgs->push_back(cppCode + ".fB"); |
| 201 | formatArgs->push_back(cppCode + ".fA"); |
Brian Osman | f28e55d | 2018-10-03 16:35:54 -0400 | [diff] [blame] | 202 | break; |
Mike Reed | b26b4e7 | 2020-01-22 14:31:21 -0500 | [diff] [blame] | 203 | case Layout::CType::kSkV4: |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 204 | formatArgs->push_back(cppCode + ".x"); |
| 205 | formatArgs->push_back(cppCode + ".y"); |
| 206 | formatArgs->push_back(cppCode + ".z"); |
| 207 | formatArgs->push_back(cppCode + ".w"); |
Brian Salomon | eca66b3 | 2019-06-01 11:18:15 -0400 | [diff] [blame] | 208 | break; |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 209 | case Layout::CType::kSkRect: |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 210 | case Layout::CType::kDefault: |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 211 | formatArgs->push_back(cppCode + ".left()"); |
| 212 | formatArgs->push_back(cppCode + ".top()"); |
| 213 | formatArgs->push_back(cppCode + ".right()"); |
| 214 | formatArgs->push_back(cppCode + ".bottom()"); |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 215 | break; |
| 216 | default: |
| 217 | SkASSERT(false); |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 218 | } |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 219 | return type.name() + "(%f, %f, %f, %f)"; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 220 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 221 | if (type.typeKind() == Type::TypeKind::kMatrix) { |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 222 | SkASSERT(type.componentType() == *fContext.fFloat_Type || |
| 223 | type.componentType() == *fContext.fHalf_Type); |
| 224 | |
| 225 | String format = type.name() + "("; |
| 226 | for (int c = 0; c < type.columns(); ++c) { |
| 227 | for (int r = 0; r < type.rows(); ++r) { |
| 228 | formatArgs->push_back(String::printf("%s.rc(%d, %d)", cppCode.c_str(), r, c)); |
| 229 | format += "%f, "; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | // Replace trailing ", " with ")". |
| 234 | format.pop_back(); |
| 235 | format.back() = ')'; |
| 236 | return format; |
| 237 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 238 | if (type.typeKind() == Type::TypeKind::kEnum) { |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 239 | formatArgs->push_back("(int) " + cppCode); |
| 240 | return "%d"; |
| 241 | } |
| 242 | if (type == *fContext.fInt4_Type || |
| 243 | type == *fContext.fShort4_Type || |
| 244 | type == *fContext.fByte4_Type) { |
| 245 | formatArgs->push_back(cppCode + ".left()"); |
| 246 | formatArgs->push_back(cppCode + ".top()"); |
| 247 | formatArgs->push_back(cppCode + ".right()"); |
| 248 | formatArgs->push_back(cppCode + ".bottom()"); |
| 249 | return type.name() + "(%d, %d, %d, %d)"; |
| 250 | } |
| 251 | |
| 252 | SkDEBUGFAILF("unsupported runtime value type '%s'\n", String(type.fName).c_str()); |
| 253 | return ""; |
| 254 | } |
| 255 | |
| 256 | void CPPCodeGenerator::writeRuntimeValue(const Type& type, const Layout& layout, |
| 257 | const String& cppCode) { |
| 258 | this->write(this->formatRuntimeValue(type, layout, cppCode, &fFormatArgs)); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void CPPCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) { |
| 262 | if (is_private(var)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 263 | this->writeRuntimeValue(var.type(), var.fModifiers.fLayout, var.fName); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 264 | } else { |
| 265 | this->writeExpression(value, kTopLevel_Precedence); |
| 266 | } |
| 267 | } |
| 268 | |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 269 | String CPPCodeGenerator::getSamplerHandle(const Variable& var) { |
| 270 | int samplerCount = 0; |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 271 | for (const auto param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 272 | if (&var == param) { |
| 273 | return "args.fTexSamplers[" + to_string(samplerCount) + "]"; |
| 274 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 275 | if (param->type().typeKind() == Type::TypeKind::kSampler) { |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 276 | ++samplerCount; |
| 277 | } |
| 278 | } |
| 279 | ABORT("should have found sampler in parameters\n"); |
| 280 | } |
| 281 | |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 282 | void CPPCodeGenerator::writeIntLiteral(const IntLiteral& i) { |
| 283 | this->write(to_string((int32_t) i.fValue)); |
| 284 | } |
| 285 | |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 286 | void CPPCodeGenerator::writeSwizzle(const Swizzle& swizzle) { |
| 287 | if (fCPPMode) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 288 | SkASSERT(swizzle.fComponents.size() == 1); // no support for multiple swizzle components yet |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 289 | this->writeExpression(*swizzle.fBase, kPostfix_Precedence); |
| 290 | switch (swizzle.fComponents[0]) { |
| 291 | case 0: this->write(".left()"); break; |
| 292 | case 1: this->write(".top()"); break; |
| 293 | case 2: this->write(".right()"); break; |
| 294 | case 3: this->write(".bottom()"); break; |
| 295 | } |
| 296 | } else { |
| 297 | INHERITED::writeSwizzle(swizzle); |
| 298 | } |
| 299 | } |
| 300 | |
John Stiles | 735a5a7 | 2020-08-26 10:21:10 -0400 | [diff] [blame] | 301 | void CPPCodeGenerator::setReturnType(int offset, ReturnType typeToSet) { |
| 302 | if (fReturnType == ReturnType::kNothing) { |
| 303 | fReturnType = typeToSet; |
| 304 | } else if (fReturnType != typeToSet) { |
| 305 | fErrors.error(offset, |
| 306 | "Fragment processors must not mix sk_OutColor and return statements\n"); |
| 307 | } |
| 308 | } |
| 309 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 310 | void CPPCodeGenerator::writeVariableReference(const VariableReference& ref) { |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 311 | if (fCPPMode) { |
| 312 | this->write(ref.fVariable.fName); |
| 313 | return; |
| 314 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 315 | switch (ref.fVariable.fModifiers.fLayout.fBuiltin) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 316 | case SK_OUTCOLOR_BUILTIN: |
| 317 | this->write("%s"); |
| 318 | fFormatArgs.push_back(String("args.fOutputColor")); |
John Stiles | 735a5a7 | 2020-08-26 10:21:10 -0400 | [diff] [blame] | 319 | this->setReturnType(ref.fOffset, ReturnType::kUsesSkOutColor); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 320 | break; |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 321 | case SK_MAIN_COORDS_BUILTIN: |
| 322 | this->write("%s"); |
| 323 | fFormatArgs.push_back(String("args.fSampleCoord")); |
| 324 | fAccessSampleCoordsDirectly = true; |
| 325 | break; |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 326 | case SK_WIDTH_BUILTIN: |
| 327 | this->write("sk_Width"); |
| 328 | break; |
| 329 | case SK_HEIGHT_BUILTIN: |
| 330 | this->write("sk_Height"); |
| 331 | break; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 332 | default: |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 333 | if (ref.fVariable.type().typeKind() == Type::TypeKind::kSampler) { |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 334 | this->write("%s"); |
| 335 | fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerVariable(" + |
Stephen White | d523a06 | 2019-06-19 13:12:46 -0400 | [diff] [blame] | 336 | this->getSamplerHandle(ref.fVariable) + ")"); |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 337 | return; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 338 | } |
| 339 | if (ref.fVariable.fModifiers.fFlags & Modifiers::kUniform_Flag) { |
| 340 | this->write("%s"); |
| 341 | String name = ref.fVariable.fName; |
Brian Osman | 1cb4171 | 2017-10-19 12:54:52 -0400 | [diff] [blame] | 342 | String var = String::printf("args.fUniformHandler->getUniformCStr(%sVar)", |
| 343 | HCodeGenerator::FieldName(name.c_str()).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 344 | String code; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 345 | if (ref.fVariable.fModifiers.fLayout.fWhen.fLength) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 346 | code = String::printf("%sVar.isValid() ? %s : \"%s\"", |
| 347 | HCodeGenerator::FieldName(name.c_str()).c_str(), |
| 348 | var.c_str(), |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 349 | default_value(ref.fVariable.type()).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 350 | } else { |
| 351 | code = var; |
| 352 | } |
| 353 | fFormatArgs.push_back(code); |
| 354 | } else if (SectionAndParameterHelper::IsParameter(ref.fVariable)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 355 | String name(ref.fVariable.fName); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 356 | this->writeRuntimeValue(ref.fVariable.type(), ref.fVariable.fModifiers.fLayout, |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 357 | String::printf("_outer.%s", name.c_str()).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 358 | } else { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 359 | this->write(ref.fVariable.fName); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 364 | void CPPCodeGenerator::writeIfStatement(const IfStatement& s) { |
| 365 | if (s.fIsStatic) { |
| 366 | this->write("@"); |
| 367 | } |
| 368 | INHERITED::writeIfStatement(s); |
| 369 | } |
| 370 | |
Ethan Nicholas | f1b1464 | 2018-08-09 16:18:07 -0400 | [diff] [blame] | 371 | void CPPCodeGenerator::writeReturnStatement(const ReturnStatement& s) { |
| 372 | if (fInMain) { |
John Stiles | 735a5a7 | 2020-08-26 10:21:10 -0400 | [diff] [blame] | 373 | this->setReturnType(s.fOffset, ReturnType::kUsesExplicitReturn); |
Ethan Nicholas | f1b1464 | 2018-08-09 16:18:07 -0400 | [diff] [blame] | 374 | } |
| 375 | INHERITED::writeReturnStatement(s); |
| 376 | } |
| 377 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 378 | void CPPCodeGenerator::writeSwitchStatement(const SwitchStatement& s) { |
| 379 | if (s.fIsStatic) { |
| 380 | this->write("@"); |
| 381 | } |
| 382 | INHERITED::writeSwitchStatement(s); |
| 383 | } |
| 384 | |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 385 | void CPPCodeGenerator::writeFieldAccess(const FieldAccess& access) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 386 | if (access.fBase->type().name() == "fragmentProcessor") { |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 387 | // Special field access on fragment processors are converted into function calls on |
| 388 | // GrFragmentProcessor's getters. |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 389 | if (access.fBase->kind() != Expression::Kind::kVariableReference) { |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 390 | fErrors.error(access.fBase->fOffset, "fragmentProcessor must be a reference\n"); |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | const Type::Field& field = fContext.fFragmentProcessor_Type->fields()[access.fFieldIndex]; |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 395 | const Variable& var = access.fBase->as<VariableReference>().fVariable; |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 396 | String cppAccess = String::printf("_outer.childProcessor(%d)->%s()", |
| 397 | this->getChildFPIndex(var), |
Ethan Nicholas | ee1c8a7 | 2019-02-22 10:50:47 -0500 | [diff] [blame] | 398 | String(field.fName).c_str()); |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 399 | |
| 400 | if (fCPPMode) { |
| 401 | this->write(cppAccess.c_str()); |
| 402 | } else { |
| 403 | writeRuntimeValue(*field.fType, Layout(), cppAccess); |
| 404 | } |
| 405 | return; |
| 406 | } |
| 407 | INHERITED::writeFieldAccess(access); |
| 408 | } |
| 409 | |
Ethan Nicholas | ee1c8a7 | 2019-02-22 10:50:47 -0500 | [diff] [blame] | 410 | int CPPCodeGenerator::getChildFPIndex(const Variable& var) const { |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 411 | int index = 0; |
| 412 | bool found = false; |
| 413 | for (const auto& p : fProgram) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 414 | if (p.kind() == ProgramElement::Kind::kVar) { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 415 | const VarDeclarations& decls = p.as<VarDeclarations>(); |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 416 | for (const auto& raw : decls.fVars) { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 417 | const VarDeclaration& decl = raw->as<VarDeclaration>(); |
Ethan Nicholas | ee1c8a7 | 2019-02-22 10:50:47 -0500 | [diff] [blame] | 418 | if (decl.fVar == &var) { |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 419 | found = true; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 420 | } else if (decl.fVar->type().nonnullable() == *fContext.fFragmentProcessor_Type) { |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 421 | ++index; |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | if (found) { |
| 426 | break; |
| 427 | } |
| 428 | } |
| 429 | SkASSERT(found); |
| 430 | return index; |
| 431 | } |
| 432 | |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 433 | void CPPCodeGenerator::writeFunctionCall(const FunctionCall& c) { |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 434 | if (c.fFunction.fBuiltin && c.fFunction.fName == "sample" && |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 435 | c.fArguments[0]->type().typeKind() != Type::TypeKind::kSampler) { |
Leon Scroggins III | 982fff2 | 2020-07-31 14:09:06 -0400 | [diff] [blame] | 436 | // Validity checks that are detected by function definition in sksl_fp.inc |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 437 | SkASSERT(c.fArguments.size() >= 1 && c.fArguments.size() <= 3); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 438 | SkASSERT("fragmentProcessor" == c.fArguments[0]->type().name() || |
| 439 | "fragmentProcessor?" == c.fArguments[0]->type().name()); |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 440 | |
| 441 | // Actually fail during compilation if arguments with valid types are |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 442 | // provided that are not variable references, since sample() is a |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 443 | // special function that impacts code emission. |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 444 | if (c.fArguments[0]->kind() != Expression::Kind::kVariableReference) { |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 445 | fErrors.error(c.fArguments[0]->fOffset, |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 446 | "sample()'s fragmentProcessor argument must be a variable reference\n"); |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 447 | return; |
| 448 | } |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 449 | const Variable& child = c.fArguments[0]->as<VariableReference>().fVariable; |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 450 | |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 451 | // Start a new extra emit code section so that the emitted child processor can depend on |
| 452 | // sksl variables defined in earlier sksl code. |
| 453 | this->newExtraEmitCodeBlock(); |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 454 | |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 455 | String inputColor; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 456 | if (c.fArguments.size() > 1 && c.fArguments[1]->type().name() == "half4") { |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 457 | // Use the invokeChild() variant that accepts an input color, so convert the 2nd |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 458 | // argument's expression into C++ code that produces sksl stored in an SkString. |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 459 | String inputColorName = "_input" + to_string(c.fOffset); |
John Stiles | d060c9d | 2020-06-08 11:44:25 -0400 | [diff] [blame] | 460 | addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments[1], inputColorName)); |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 461 | |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 462 | // invokeChild() needs a char* and a pre-pended comma |
| 463 | inputColor = ", " + inputColorName + ".c_str()"; |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 464 | } |
| 465 | |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 466 | String inputCoord; |
| 467 | String invokeFunction = "invokeChild"; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 468 | if (c.fArguments.back()->type().name() == "float2") { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 469 | // Invoking child with explicit coordinates at this call site |
| 470 | inputCoord = "_coords" + to_string(c.fOffset); |
| 471 | addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments.back(), inputCoord)); |
| 472 | inputCoord.append(".c_str()"); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 473 | } else if (c.fArguments.back()->type().name() == "float3x3") { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 474 | // Invoking child with a matrix, sampling relative to the input coords. |
| 475 | invokeFunction = "invokeChildWithMatrix"; |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 476 | SampleUsage usage = Analysis::GetSampleUsage(fProgram, child); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 477 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 478 | if (!usage.hasUniformMatrix()) { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 479 | inputCoord = "_matrix" + to_string(c.fOffset); |
| 480 | addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments.back(), inputCoord)); |
| 481 | inputCoord.append(".c_str()"); |
| 482 | } |
| 483 | // else pass in the empty string to rely on invokeChildWithMatrix's automatic uniform |
| 484 | // resolution |
| 485 | } |
| 486 | if (!inputCoord.empty()) { |
| 487 | inputCoord = ", " + inputCoord; |
| 488 | } |
| 489 | |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 490 | // Write the output handling after the possible input handling |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 491 | String childName = "_sample" + to_string(c.fOffset); |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 492 | String childIndexStr = to_string(this->getChildFPIndex(child)); |
| 493 | addExtraEmitCodeLine("SkString " + childName + " = this->" + invokeFunction + "(" + |
| 494 | childIndexStr + inputColor + ", args" + inputCoord + ");"); |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 495 | |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame] | 496 | this->write("%s"); |
| 497 | fFormatArgs.push_back(childName + ".c_str()"); |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 498 | return; |
| 499 | } |
Ethan Nicholas | 095f5b4 | 2019-08-30 11:51:41 -0400 | [diff] [blame] | 500 | if (c.fFunction.fBuiltin) { |
| 501 | INHERITED::writeFunctionCall(c); |
| 502 | } else { |
| 503 | this->write("%s"); |
| 504 | fFormatArgs.push_back((String(c.fFunction.fName) + "_name.c_str()").c_str()); |
| 505 | this->write("("); |
| 506 | const char* separator = ""; |
| 507 | for (const auto& arg : c.fArguments) { |
| 508 | this->write(separator); |
| 509 | separator = ", "; |
| 510 | this->writeExpression(*arg, kSequence_Precedence); |
| 511 | } |
| 512 | this->write(")"); |
| 513 | } |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 514 | if (c.fFunction.fBuiltin && c.fFunction.fName == "sample") { |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 515 | this->write(".%s"); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 516 | SkASSERT(c.fArguments.size() >= 1); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 517 | SkASSERT(c.fArguments[0]->kind() == Expression::Kind::kVariableReference); |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 518 | String sampler = this->getSamplerHandle(c.fArguments[0]->as<VariableReference>().fVariable); |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 519 | fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerSwizzle(" + sampler + |
Greg Daniel | 369ee6b | 2019-12-02 15:30:02 -0500 | [diff] [blame] | 520 | ").asString().c_str()"); |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | |
Ethan Nicholas | 095f5b4 | 2019-08-30 11:51:41 -0400 | [diff] [blame] | 524 | static const char* glsltype_string(const Context& context, const Type& type) { |
| 525 | if (type == *context.fFloat_Type) { |
| 526 | return "kFloat_GrSLType"; |
| 527 | } else if (type == *context.fHalf_Type) { |
| 528 | return "kHalf_GrSLType"; |
John Stiles | 0e8149c | 2020-08-18 12:23:40 -0400 | [diff] [blame] | 529 | } else if (type == *context.fInt_Type) { |
| 530 | return "kInt_GrSLType"; |
Ethan Nicholas | 095f5b4 | 2019-08-30 11:51:41 -0400 | [diff] [blame] | 531 | } else if (type == *context.fFloat2_Type) { |
| 532 | return "kFloat2_GrSLType"; |
| 533 | } else if (type == *context.fHalf2_Type) { |
| 534 | return "kHalf2_GrSLType"; |
John Stiles | 0e8149c | 2020-08-18 12:23:40 -0400 | [diff] [blame] | 535 | } else if (type == *context.fInt2_Type) { |
| 536 | return "kInt2_GrSLType"; |
Ethan Nicholas | 8ae1b56 | 2019-12-17 15:18:02 -0500 | [diff] [blame] | 537 | } else if (type == *context.fFloat3_Type) { |
| 538 | return "kFloat3_GrSLType"; |
| 539 | } else if (type == *context.fHalf3_Type) { |
| 540 | return "kHalf3_GrSLType"; |
John Stiles | 0e8149c | 2020-08-18 12:23:40 -0400 | [diff] [blame] | 541 | } else if (type == *context.fInt3_Type) { |
| 542 | return "kInt3_GrSLType"; |
Ethan Nicholas | 095f5b4 | 2019-08-30 11:51:41 -0400 | [diff] [blame] | 543 | } else if (type == *context.fFloat4_Type) { |
| 544 | return "kFloat4_GrSLType"; |
| 545 | } else if (type == *context.fHalf4_Type) { |
| 546 | return "kHalf4_GrSLType"; |
John Stiles | 0e8149c | 2020-08-18 12:23:40 -0400 | [diff] [blame] | 547 | } else if (type == *context.fInt4_Type) { |
| 548 | return "kInt4_GrSLType"; |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 549 | } else if (type == *context.fFloat2x2_Type) { |
| 550 | return "kFloat2x2_GrSLType"; |
| 551 | } else if (type == *context.fHalf2x2_Type) { |
| 552 | return "kHalf2x2_GrSLType"; |
John Stiles | 0e8149c | 2020-08-18 12:23:40 -0400 | [diff] [blame] | 553 | } else if (type == *context.fFloat2x3_Type) { |
| 554 | return "kFloat2x3_GrSLType"; |
| 555 | } else if (type == *context.fHalf2x3_Type) { |
| 556 | return "kHalf2x3_GrSLType"; |
| 557 | } else if (type == *context.fFloat2x4_Type) { |
| 558 | return "kFloat2x4_GrSLType"; |
| 559 | } else if (type == *context.fHalf2x4_Type) { |
| 560 | return "kHalf2x4_GrSLType"; |
| 561 | } else if (type == *context.fFloat3x2_Type) { |
| 562 | return "kFloat3x2_GrSLType"; |
| 563 | } else if (type == *context.fHalf3x2_Type) { |
| 564 | return "kHalf3x2_GrSLType"; |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 565 | } else if (type == *context.fFloat3x3_Type) { |
| 566 | return "kFloat3x3_GrSLType"; |
| 567 | } else if (type == *context.fHalf3x3_Type) { |
| 568 | return "kHalf3x3_GrSLType"; |
John Stiles | 0e8149c | 2020-08-18 12:23:40 -0400 | [diff] [blame] | 569 | } else if (type == *context.fFloat3x4_Type) { |
| 570 | return "kFloat3x4_GrSLType"; |
| 571 | } else if (type == *context.fHalf3x4_Type) { |
| 572 | return "kHalf3x4_GrSLType"; |
| 573 | } else if (type == *context.fFloat4x2_Type) { |
| 574 | return "kFloat4x2_GrSLType"; |
| 575 | } else if (type == *context.fHalf4x2_Type) { |
| 576 | return "kHalf4x2_GrSLType"; |
| 577 | } else if (type == *context.fFloat4x3_Type) { |
| 578 | return "kFloat4x3_GrSLType"; |
| 579 | } else if (type == *context.fHalf4x3_Type) { |
| 580 | return "kHalf4x3_GrSLType"; |
Ethan Nicholas | 095f5b4 | 2019-08-30 11:51:41 -0400 | [diff] [blame] | 581 | } else if (type == *context.fFloat4x4_Type) { |
| 582 | return "kFloat4x4_GrSLType"; |
| 583 | } else if (type == *context.fHalf4x4_Type) { |
| 584 | return "kHalf4x4_GrSLType"; |
| 585 | } else if (type == *context.fVoid_Type) { |
| 586 | return "kVoid_GrSLType"; |
John Stiles | 648a81e | 2020-09-02 10:52:14 -0400 | [diff] [blame] | 587 | } else if (type == *context.fBool_Type) { |
| 588 | return "kBool_GrSLType"; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 589 | } else if (type.typeKind() == Type::TypeKind::kEnum) { |
Ethan Nicholas | 8ae1b56 | 2019-12-17 15:18:02 -0500 | [diff] [blame] | 590 | return "int"; |
Ethan Nicholas | 095f5b4 | 2019-08-30 11:51:41 -0400 | [diff] [blame] | 591 | } |
| 592 | SkASSERT(false); |
| 593 | return nullptr; |
| 594 | } |
| 595 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 596 | void CPPCodeGenerator::writeFunction(const FunctionDefinition& f) { |
Ethan Nicholas | 095f5b4 | 2019-08-30 11:51:41 -0400 | [diff] [blame] | 597 | const FunctionDeclaration& decl = f.fDeclaration; |
Brian Osman | 08f986d | 2020-05-13 17:06:46 -0400 | [diff] [blame] | 598 | if (decl.fBuiltin) { |
| 599 | return; |
| 600 | } |
Ethan Nicholas | 095f5b4 | 2019-08-30 11:51:41 -0400 | [diff] [blame] | 601 | fFunctionHeader = ""; |
| 602 | OutputStream* oldOut = fOut; |
| 603 | StringStream buffer; |
| 604 | fOut = &buffer; |
| 605 | if (decl.fName == "main") { |
Ethan Nicholas | f1b1464 | 2018-08-09 16:18:07 -0400 | [diff] [blame] | 606 | fInMain = true; |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 607 | for (const auto& s : f.fBody->as<Block>().fStatements) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 608 | this->writeStatement(*s); |
| 609 | this->writeLine(); |
| 610 | } |
Ethan Nicholas | f1b1464 | 2018-08-09 16:18:07 -0400 | [diff] [blame] | 611 | fInMain = false; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 612 | |
| 613 | fOut = oldOut; |
| 614 | this->write(fFunctionHeader); |
| 615 | this->write(buffer.str()); |
| 616 | } else { |
Ethan Nicholas | 095f5b4 | 2019-08-30 11:51:41 -0400 | [diff] [blame] | 617 | this->addExtraEmitCodeLine("SkString " + decl.fName + "_name;"); |
| 618 | String args = "const GrShaderVar " + decl.fName + "_args[] = { "; |
| 619 | const char* separator = ""; |
| 620 | for (const auto& param : decl.fParameters) { |
| 621 | args += String(separator) + "GrShaderVar(\"" + param->fName + "\", " + |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 622 | glsltype_string(fContext, param->type()) + ")"; |
Ethan Nicholas | 095f5b4 | 2019-08-30 11:51:41 -0400 | [diff] [blame] | 623 | separator = ", "; |
| 624 | } |
| 625 | args += "};"; |
| 626 | this->addExtraEmitCodeLine(args.c_str()); |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 627 | for (const auto& s : f.fBody->as<Block>().fStatements) { |
Ethan Nicholas | 095f5b4 | 2019-08-30 11:51:41 -0400 | [diff] [blame] | 628 | this->writeStatement(*s); |
| 629 | this->writeLine(); |
| 630 | } |
| 631 | |
| 632 | fOut = oldOut; |
| 633 | String emit = "fragBuilder->emitFunction("; |
| 634 | emit += glsltype_string(fContext, decl.fReturnType); |
| 635 | emit += ", \"" + decl.fName + "\""; |
| 636 | emit += ", " + to_string((int64_t) decl.fParameters.size()); |
| 637 | emit += ", " + decl.fName + "_args"; |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 638 | emit += ",\nR\"SkSL(" + buffer.str() + ")SkSL\""; |
Ethan Nicholas | 095f5b4 | 2019-08-30 11:51:41 -0400 | [diff] [blame] | 639 | emit += ", &" + decl.fName + "_name);"; |
| 640 | this->addExtraEmitCodeLine(emit.c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | |
| 644 | void CPPCodeGenerator::writeSetting(const Setting& s) { |
Brian Osman | f265afd | 2020-08-04 13:23:36 -0400 | [diff] [blame] | 645 | this->write(s.fName.c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 646 | } |
| 647 | |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 648 | bool CPPCodeGenerator::writeSection(const char* name, const char* prefix) { |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 649 | const Section* s = fSectionAndParameterHelper.getSection(name); |
| 650 | if (s) { |
| 651 | this->writef("%s%s", prefix, s->fText.c_str()); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 652 | return true; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 653 | } |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 654 | return false; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | void CPPCodeGenerator::writeProgramElement(const ProgramElement& p) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 658 | switch (p.kind()) { |
| 659 | case ProgramElement::Kind::kSection: |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 660 | return; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 661 | case ProgramElement::Kind::kVar: { |
| 662 | const VarDeclarations& decls = p.as<VarDeclarations>(); |
| 663 | if (!decls.fVars.size()) { |
| 664 | return; |
| 665 | } |
| 666 | const Variable& var = *decls.fVars[0]->as<VarDeclaration>().fVar; |
| 667 | if (var.fModifiers.fFlags & (Modifiers::kIn_Flag | Modifiers::kUniform_Flag) || |
| 668 | -1 != var.fModifiers.fLayout.fBuiltin) { |
| 669 | return; |
| 670 | } |
| 671 | break; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 672 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 673 | default: |
| 674 | break; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 675 | } |
| 676 | INHERITED::writeProgramElement(p); |
| 677 | } |
| 678 | |
| 679 | void CPPCodeGenerator::addUniform(const Variable& var) { |
| 680 | if (!needs_uniform_var(var)) { |
| 681 | return; |
| 682 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 683 | if (var.fModifiers.fLayout.fWhen.fLength) { |
| 684 | this->writef(" if (%s) {\n ", String(var.fModifiers.fLayout.fWhen).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 685 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 686 | String name(var.fName); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 687 | if (var.type().typeKind() != Type::TypeKind::kArray) { |
Ethan Nicholas | 7018bcf | 2020-08-20 15:57:22 -0400 | [diff] [blame] | 688 | this->writef(" %sVar = args.fUniformHandler->addUniform(&_outer, " |
| 689 | "kFragment_GrShaderFlag, %s, \"%s\");\n", |
| 690 | HCodeGenerator::FieldName(name.c_str()).c_str(), |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 691 | glsltype_string(fContext, var.type()), |
Ethan Nicholas | 7018bcf | 2020-08-20 15:57:22 -0400 | [diff] [blame] | 692 | name.c_str()); |
| 693 | } else { |
| 694 | this->writef(" %sVar = args.fUniformHandler->addUniformArray(&_outer, " |
| 695 | "kFragment_GrShaderFlag, %s, \"%s\", %d);\n", |
| 696 | HCodeGenerator::FieldName(name.c_str()).c_str(), |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 697 | glsltype_string(fContext, var.type().componentType()), |
Ethan Nicholas | 7018bcf | 2020-08-20 15:57:22 -0400 | [diff] [blame] | 698 | name.c_str(), |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 699 | var.type().columns()); |
Ethan Nicholas | 7018bcf | 2020-08-20 15:57:22 -0400 | [diff] [blame] | 700 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 701 | if (var.fModifiers.fLayout.fWhen.fLength) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 702 | this->write(" }\n"); |
| 703 | } |
| 704 | } |
| 705 | |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 706 | void CPPCodeGenerator::writeInputVars() { |
| 707 | } |
| 708 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 709 | void CPPCodeGenerator::writePrivateVars() { |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 710 | for (const auto& p : fProgram) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 711 | if (p.kind() == ProgramElement::Kind::kVar) { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 712 | const VarDeclarations& decls = p.as<VarDeclarations>(); |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 713 | for (const auto& raw : decls.fVars) { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 714 | VarDeclaration& decl = raw->as<VarDeclaration>(); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 715 | if (is_private(*decl.fVar)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 716 | if (decl.fVar->type() == *fContext.fFragmentProcessor_Type) { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 717 | fErrors.error(decl.fOffset, |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 718 | "fragmentProcessor variables must be declared 'in'"); |
| 719 | return; |
| 720 | } |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 721 | this->writef("%s %s = %s;\n", |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 722 | HCodeGenerator::FieldType(fContext, decl.fVar->type(), |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 723 | decl.fVar->fModifiers.fLayout).c_str(), |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 724 | String(decl.fVar->fName).c_str(), |
| 725 | default_value(*decl.fVar).c_str()); |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 726 | } else if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) { |
| 727 | // An auto-tracked uniform in variable, so add a field to hold onto the prior |
| 728 | // state. Note that tracked variables must be uniform in's and that is validated |
| 729 | // before writePrivateVars() is called. |
| 730 | const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *decl.fVar); |
| 731 | SkASSERT(mapper && mapper->supportsTracking()); |
| 732 | |
| 733 | String name = HCodeGenerator::FieldName(String(decl.fVar->fName).c_str()); |
| 734 | // The member statement is different if the mapper reports a default value |
| 735 | if (mapper->defaultValue().size() > 0) { |
| 736 | this->writef("%s %sPrev = %s;\n", |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 737 | Layout::CTypeToStr(mapper->ctype()), name.c_str(), |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 738 | mapper->defaultValue().c_str()); |
| 739 | } else { |
| 740 | this->writef("%s %sPrev;\n", |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 741 | Layout::CTypeToStr(mapper->ctype()), name.c_str()); |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 742 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | void CPPCodeGenerator::writePrivateVarValues() { |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 750 | for (const auto& p : fProgram) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 751 | if (p.kind() == ProgramElement::Kind::kVar) { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 752 | const VarDeclarations& decls = p.as<VarDeclarations>(); |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 753 | for (const auto& raw : decls.fVars) { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 754 | VarDeclaration& decl = raw->as<VarDeclaration>(); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 755 | if (is_private(*decl.fVar) && decl.fValue) { |
| 756 | this->writef("%s = ", String(decl.fVar->fName).c_str()); |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 757 | fCPPMode = true; |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 758 | this->writeExpression(*decl.fValue, kAssignment_Precedence); |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 759 | fCPPMode = false; |
| 760 | this->write(";\n"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 767 | static bool is_accessible(const Variable& var) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 768 | const Type& type = var.type().nonnullable(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 769 | return Type::TypeKind::kSampler != type.typeKind() && |
| 770 | Type::TypeKind::kOther != type.typeKind(); |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 771 | } |
| 772 | |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 773 | void CPPCodeGenerator::newExtraEmitCodeBlock() { |
| 774 | // This should only be called when emitting SKSL for emitCode(), which can be detected if the |
| 775 | // cpp buffer is not null, and the cpp buffer is not the current output. |
| 776 | SkASSERT(fCPPBuffer && fCPPBuffer != fOut); |
| 777 | |
| 778 | // Start a new block as an empty string |
| 779 | fExtraEmitCodeBlocks.push_back(""); |
| 780 | // Mark its location in the output buffer, uses ${\d} for the token since ${} will not occur in |
| 781 | // valid sksl and makes detection trivial. |
| 782 | this->writef("${%zu}", fExtraEmitCodeBlocks.size() - 1); |
| 783 | } |
| 784 | |
| 785 | void CPPCodeGenerator::addExtraEmitCodeLine(const String& toAppend) { |
| 786 | SkASSERT(fExtraEmitCodeBlocks.size() > 0); |
| 787 | String& currentBlock = fExtraEmitCodeBlocks[fExtraEmitCodeBlocks.size() - 1]; |
| 788 | // Automatically add indentation and newline |
| 789 | currentBlock += " " + toAppend + "\n"; |
| 790 | } |
| 791 | |
| 792 | void CPPCodeGenerator::flushEmittedCode() { |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 793 | if (fCPPBuffer == nullptr) { |
| 794 | // Not actually within writeEmitCode() so nothing to flush |
| 795 | return; |
| 796 | } |
| 797 | |
| 798 | StringStream* skslBuffer = static_cast<StringStream*>(fOut); |
| 799 | |
| 800 | String sksl = skslBuffer->str(); |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 801 | // Empty the accumulation buffer since its current contents are consumed. |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 802 | skslBuffer->reset(); |
| 803 | |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 804 | // Switch to the cpp buffer |
Michael Ludwig | d044019 | 2018-09-07 14:24:52 +0000 | [diff] [blame] | 805 | fOut = fCPPBuffer; |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 806 | |
| 807 | // Iterate through the sksl, keeping track of where the last statement ended (e.g. the latest |
| 808 | // encountered ';', '{', or '}'). If an extra emit code block token is encountered then the |
| 809 | // code from 0 to last statement end is sent to writeCodeAppend, the extra code block is |
| 810 | // appended to the cpp buffer, and then the sksl string is trimmed to start where the last |
| 811 | // statement left off (minus the encountered token). |
| 812 | size_t i = 0; |
| 813 | int flushPoint = -1; |
| 814 | int tokenStart = -1; |
| 815 | while (i < sksl.size()) { |
| 816 | if (tokenStart >= 0) { |
| 817 | // Looking for the end of the token |
| 818 | if (sksl[i] == '}') { |
| 819 | // Must append the sksl from 0 to flushPoint (inclusive) then the extra code |
| 820 | // accumulated in the block with index parsed from chars [tokenStart+2, i-1] |
| 821 | String toFlush = String(sksl.c_str(), flushPoint + 1); |
| 822 | // writeCodeAppend automatically removes the format args that it consumed, so |
| 823 | // fFormatArgs will be in a valid state for any future sksl |
| 824 | this->writeCodeAppend(toFlush); |
| 825 | |
| 826 | int codeBlock = stoi(String(sksl.c_str() + tokenStart + 2, i - tokenStart - 2)); |
| 827 | SkASSERT(codeBlock < (int) fExtraEmitCodeBlocks.size()); |
| 828 | if (fExtraEmitCodeBlocks[codeBlock].size() > 0) { |
| 829 | this->write(fExtraEmitCodeBlocks[codeBlock].c_str()); |
| 830 | } |
| 831 | |
| 832 | // Now reset the sksl buffer to start after the flush point, but remove the token. |
| 833 | String compacted = String(sksl.c_str() + flushPoint + 1, |
| 834 | tokenStart - flushPoint - 1); |
| 835 | if (i < sksl.size() - 1) { |
| 836 | compacted += String(sksl.c_str() + i + 1, sksl.size() - i - 1); |
| 837 | } |
| 838 | sksl = compacted; |
| 839 | |
| 840 | // And reset iteration |
| 841 | i = -1; |
| 842 | flushPoint = -1; |
| 843 | tokenStart = -1; |
| 844 | } |
| 845 | } else { |
| 846 | // Looking for the start of extra emit block tokens, and tracking when statements end |
| 847 | if (sksl[i] == ';' || sksl[i] == '{' || sksl[i] == '}') { |
| 848 | flushPoint = i; |
| 849 | } else if (i < sksl.size() - 1 && sksl[i] == '$' && sksl[i + 1] == '{') { |
| 850 | // found an extra emit code block token |
| 851 | tokenStart = i++; |
| 852 | } |
| 853 | } |
| 854 | i++; |
Michael Ludwig | d044019 | 2018-09-07 14:24:52 +0000 | [diff] [blame] | 855 | } |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 856 | |
| 857 | // Once we've gone through the sksl string to this point, there are no remaining extra emit |
| 858 | // code blocks to interleave, so append the remainder as usual. |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 859 | this->writeCodeAppend(sksl); |
| 860 | |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 861 | // After appending, switch back to the emptied sksl buffer and reset the extra code blocks |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 862 | fOut = skslBuffer; |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 863 | fExtraEmitCodeBlocks.clear(); |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 864 | } |
| 865 | |
Ethan Nicholas | 5b6e627 | 2017-10-13 13:11:06 -0400 | [diff] [blame] | 866 | void CPPCodeGenerator::writeCodeAppend(const String& code) { |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 867 | if (!code.empty()) { |
| 868 | // Count % format specifiers. |
| 869 | size_t argCount = 0; |
| 870 | for (size_t index = 0; index < code.size(); ++index) { |
Ethan Nicholas | 5b6e627 | 2017-10-13 13:11:06 -0400 | [diff] [blame] | 871 | if ('%' == code[index]) { |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 872 | if (index == code.size() - 1) { |
Ethan Nicholas | 5b6e627 | 2017-10-13 13:11:06 -0400 | [diff] [blame] | 873 | break; |
| 874 | } |
| 875 | if (code[index + 1] != '%') { |
| 876 | ++argCount; |
| 877 | } |
| 878 | } |
Ethan Nicholas | 5b6e627 | 2017-10-13 13:11:06 -0400 | [diff] [blame] | 879 | } |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 880 | |
| 881 | // Emit the code string. |
| 882 | this->writef(" fragBuilder->codeAppendf(\n" |
| 883 | "R\"SkSL(%s)SkSL\"\n", code.c_str()); |
| 884 | for (size_t i = 0; i < argCount; ++i) { |
Ethan Nicholas | 5b6e627 | 2017-10-13 13:11:06 -0400 | [diff] [blame] | 885 | this->writef(", %s", fFormatArgs[i].c_str()); |
| 886 | } |
| 887 | this->write(");\n"); |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 888 | |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 889 | // argCount is equal to the number of fFormatArgs that were consumed, so they should be |
| 890 | // removed from the list. |
| 891 | if (argCount > 0) { |
| 892 | fFormatArgs.erase(fFormatArgs.begin(), fFormatArgs.begin() + argCount); |
| 893 | } |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | |
| 897 | String CPPCodeGenerator::convertSKSLExpressionToCPP(const Expression& e, |
| 898 | const String& cppVar) { |
| 899 | // To do this conversion, we temporarily switch the sksl output stream |
| 900 | // to an empty stringstream and reset the format args to empty. |
| 901 | OutputStream* oldSKSL = fOut; |
| 902 | StringStream exprBuffer; |
| 903 | fOut = &exprBuffer; |
| 904 | |
| 905 | std::vector<String> oldArgs(fFormatArgs); |
| 906 | fFormatArgs.clear(); |
| 907 | |
| 908 | // Convert the argument expression into a format string and args |
| 909 | this->writeExpression(e, Precedence::kTopLevel_Precedence); |
| 910 | std::vector<String> newArgs(fFormatArgs); |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 911 | String expr = exprBuffer.str(); |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 912 | |
| 913 | // After generating, restore the original output stream and format args |
| 914 | fFormatArgs = oldArgs; |
| 915 | fOut = oldSKSL; |
| 916 | |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 917 | // The sksl written to exprBuffer is not processed by flushEmittedCode(), so any extra emit code |
| 918 | // block tokens won't get handled. So we need to strip them from the expression and stick them |
| 919 | // to the end of the original sksl stream. |
| 920 | String exprFormat = ""; |
| 921 | int tokenStart = -1; |
| 922 | for (size_t i = 0; i < expr.size(); i++) { |
| 923 | if (tokenStart >= 0) { |
| 924 | if (expr[i] == '}') { |
| 925 | // End of the token, so append the token to fOut |
| 926 | fOut->write(expr.c_str() + tokenStart, i - tokenStart + 1); |
| 927 | tokenStart = -1; |
| 928 | } |
| 929 | } else { |
| 930 | if (i < expr.size() - 1 && expr[i] == '$' && expr[i + 1] == '{') { |
| 931 | tokenStart = i++; |
| 932 | } else { |
| 933 | exprFormat += expr[i]; |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 938 | // Now build the final C++ code snippet from the format string and args |
| 939 | String cppExpr; |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 940 | if (newArgs.empty()) { |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 941 | // This was a static expression, so we can simplify the input |
| 942 | // color declaration in the emitted code to just a static string |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 943 | cppExpr = "SkString " + cppVar + "(\"" + exprFormat + "\");"; |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 944 | } else if (newArgs.size() == 1 && exprFormat == "%s") { |
| 945 | // If the format expression is simply "%s", we can avoid an expensive call to printf. |
| 946 | // This happens fairly often in codegen so it is worth simplifying. |
| 947 | cppExpr = "SkString " + cppVar + "(" + newArgs[0] + ");"; |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 948 | } else { |
| 949 | // String formatting must occur dynamically, so have the C++ declaration |
| 950 | // use SkStringPrintf with the format args that were accumulated |
| 951 | // when the expression was written. |
| 952 | cppExpr = "SkString " + cppVar + " = SkStringPrintf(\"" + exprFormat + "\""; |
| 953 | for (size_t i = 0; i < newArgs.size(); i++) { |
| 954 | cppExpr += ", " + newArgs[i]; |
| 955 | } |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 956 | cppExpr += ");"; |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 957 | } |
| 958 | return cppExpr; |
Ethan Nicholas | 5b6e627 | 2017-10-13 13:11:06 -0400 | [diff] [blame] | 959 | } |
| 960 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 961 | bool CPPCodeGenerator::writeEmitCode(std::vector<const Variable*>& uniforms) { |
| 962 | this->write(" void emitCode(EmitArgs& args) override {\n" |
| 963 | " GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;\n"); |
| 964 | this->writef(" const %s& _outer = args.fFp.cast<%s>();\n" |
| 965 | " (void) _outer;\n", |
| 966 | fFullName.c_str(), fFullName.c_str()); |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 967 | for (const auto& p : fProgram) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 968 | if (p.kind() == ProgramElement::Kind::kVar) { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 969 | const VarDeclarations& decls = p.as<VarDeclarations>(); |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 970 | for (const auto& raw : decls.fVars) { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 971 | VarDeclaration& decl = raw->as<VarDeclaration>(); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 972 | String nameString(decl.fVar->fName); |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 973 | const char* name = nameString.c_str(); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 974 | if (SectionAndParameterHelper::IsParameter(*decl.fVar) && |
| 975 | is_accessible(*decl.fVar)) { |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 976 | this->writef(" auto %s = _outer.%s;\n" |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 977 | " (void) %s;\n", |
| 978 | name, name, name); |
| 979 | } |
| 980 | } |
| 981 | } |
| 982 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 983 | this->writePrivateVarValues(); |
| 984 | for (const auto u : uniforms) { |
| 985 | this->addUniform(*u); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 986 | } |
John Stiles | 02b1128 | 2020-08-10 15:25:24 -0400 | [diff] [blame] | 987 | this->writeSection(kEmitCodeSection); |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 988 | |
| 989 | // Save original buffer as the CPP buffer for flushEmittedCode() |
| 990 | fCPPBuffer = fOut; |
| 991 | StringStream skslBuffer; |
| 992 | fOut = &skslBuffer; |
| 993 | |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 994 | this->newExtraEmitCodeBlock(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 995 | bool result = INHERITED::generateCode(); |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 996 | this->flushEmittedCode(); |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 997 | |
| 998 | // Then restore the original CPP buffer and close the function |
| 999 | fOut = fCPPBuffer; |
| 1000 | fCPPBuffer = nullptr; |
Ethan Nicholas | 5b6e627 | 2017-10-13 13:11:06 -0400 | [diff] [blame] | 1001 | this->write(" }\n"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1002 | return result; |
| 1003 | } |
| 1004 | |
| 1005 | void CPPCodeGenerator::writeSetData(std::vector<const Variable*>& uniforms) { |
| 1006 | const char* fullName = fFullName.c_str(); |
John Stiles | 02b1128 | 2020-08-10 15:25:24 -0400 | [diff] [blame] | 1007 | const Section* section = fSectionAndParameterHelper.getSection(kSetDataSection); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 1008 | const char* pdman = section ? section->fArgument.c_str() : "pdman"; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1009 | this->writef(" void onSetData(const GrGLSLProgramDataManager& %s, " |
| 1010 | "const GrFragmentProcessor& _proc) override {\n", |
| 1011 | pdman); |
| 1012 | bool wroteProcessor = false; |
John Stiles | 06f3d08 | 2020-06-04 11:07:21 -0400 | [diff] [blame] | 1013 | for (const Variable* u : uniforms) { |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 1014 | if (is_uniform_in(*u)) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1015 | if (!wroteProcessor) { |
| 1016 | this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName, fullName); |
| 1017 | wroteProcessor = true; |
| 1018 | this->writef(" {\n"); |
| 1019 | } |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 1020 | |
| 1021 | const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *u); |
| 1022 | SkASSERT(mapper); |
| 1023 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1024 | String nameString(u->fName); |
| 1025 | const char* name = nameString.c_str(); |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 1026 | |
| 1027 | // Switches for setData behavior in the generated code |
| 1028 | bool conditionalUniform = u->fModifiers.fLayout.fWhen != ""; |
| 1029 | bool isTracked = u->fModifiers.fLayout.fFlags & Layout::kTracked_Flag; |
| 1030 | bool needsValueDeclaration = isTracked || !mapper->canInlineUniformValue(); |
| 1031 | |
| 1032 | String uniformName = HCodeGenerator::FieldName(name) + "Var"; |
| 1033 | |
| 1034 | String indent = " "; // 8 by default, 12 when nested for conditional uniforms |
| 1035 | if (conditionalUniform) { |
| 1036 | // Add a pre-check to make sure the uniform was emitted |
| 1037 | // before trying to send any data to the GPU |
| 1038 | this->writef(" if (%s.isValid()) {\n", uniformName.c_str()); |
| 1039 | indent += " "; |
| 1040 | } |
| 1041 | |
| 1042 | String valueVar = ""; |
| 1043 | if (needsValueDeclaration) { |
| 1044 | valueVar.appendf("%sValue", name); |
| 1045 | // Use AccessType since that will match the return type of _outer's public API. |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1046 | String valueType = HCodeGenerator::AccessType(fContext, u->type(), |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 1047 | u->fModifiers.fLayout); |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 1048 | this->writef("%s%s %s = _outer.%s;\n", |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 1049 | indent.c_str(), valueType.c_str(), valueVar.c_str(), name); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1050 | } else { |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 1051 | // Not tracked and the mapper only needs to use the value once |
| 1052 | // so send it a safe expression instead of the variable name |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 1053 | valueVar.appendf("(_outer.%s)", name); |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | if (isTracked) { |
| 1057 | SkASSERT(mapper->supportsTracking()); |
| 1058 | |
| 1059 | String prevVar = HCodeGenerator::FieldName(name) + "Prev"; |
| 1060 | this->writef("%sif (%s) {\n" |
| 1061 | "%s %s;\n" |
| 1062 | "%s %s;\n" |
| 1063 | "%s}\n", indent.c_str(), |
| 1064 | mapper->dirtyExpression(valueVar, prevVar).c_str(), indent.c_str(), |
| 1065 | mapper->saveState(valueVar, prevVar).c_str(), indent.c_str(), |
| 1066 | mapper->setUniform(pdman, uniformName, valueVar).c_str(), indent.c_str()); |
| 1067 | } else { |
| 1068 | this->writef("%s%s;\n", indent.c_str(), |
| 1069 | mapper->setUniform(pdman, uniformName, valueVar).c_str()); |
| 1070 | } |
| 1071 | |
| 1072 | if (conditionalUniform) { |
| 1073 | // Close the earlier precheck block |
| 1074 | this->writef(" }\n"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | } |
| 1078 | if (wroteProcessor) { |
| 1079 | this->writef(" }\n"); |
| 1080 | } |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 1081 | if (section) { |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 1082 | int samplerIndex = 0; |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1083 | for (const auto& p : fProgram) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1084 | if (p.kind() == ProgramElement::Kind::kVar) { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1085 | const VarDeclarations& decls = p.as<VarDeclarations>(); |
John Stiles | 06f3d08 | 2020-06-04 11:07:21 -0400 | [diff] [blame] | 1086 | for (const std::unique_ptr<Statement>& raw : decls.fVars) { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1087 | const VarDeclaration& decl = raw->as<VarDeclaration>(); |
John Stiles | 06f3d08 | 2020-06-04 11:07:21 -0400 | [diff] [blame] | 1088 | const Variable& variable = *decl.fVar; |
| 1089 | String nameString(variable.fName); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1090 | const char* name = nameString.c_str(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1091 | if (variable.type().typeKind() == Type::TypeKind::kSampler) { |
Robert Phillips | bd99c0c | 2019-12-12 13:26:58 +0000 | [diff] [blame] | 1092 | this->writef(" const GrSurfaceProxyView& %sView = " |
| 1093 | "_outer.textureSampler(%d).view();\n", |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 1094 | name, samplerIndex); |
Robert Phillips | bd99c0c | 2019-12-12 13:26:58 +0000 | [diff] [blame] | 1095 | this->writef(" GrTexture& %s = *%sView.proxy()->peekTexture();\n", |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 1096 | name, name); |
| 1097 | this->writef(" (void) %s;\n", name); |
| 1098 | ++samplerIndex; |
John Stiles | 06f3d08 | 2020-06-04 11:07:21 -0400 | [diff] [blame] | 1099 | } else if (needs_uniform_var(variable)) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1100 | this->writef(" UniformHandle& %s = %sVar;\n" |
| 1101 | " (void) %s;\n", |
| 1102 | name, HCodeGenerator::FieldName(name).c_str(), name); |
John Stiles | 06f3d08 | 2020-06-04 11:07:21 -0400 | [diff] [blame] | 1103 | } else if (SectionAndParameterHelper::IsParameter(variable) && |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1104 | variable.type() != *fContext.fFragmentProcessor_Type) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1105 | if (!wroteProcessor) { |
| 1106 | this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName, |
| 1107 | fullName); |
| 1108 | wroteProcessor = true; |
| 1109 | } |
John Stiles | 06f3d08 | 2020-06-04 11:07:21 -0400 | [diff] [blame] | 1110 | |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1111 | if (variable.type().nonnullable() != *fContext.fFragmentProcessor_Type) { |
John Stiles | 06f3d08 | 2020-06-04 11:07:21 -0400 | [diff] [blame] | 1112 | this->writef(" auto %s = _outer.%s;\n" |
| 1113 | " (void) %s;\n", |
| 1114 | name, name, name); |
| 1115 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1116 | } |
| 1117 | } |
| 1118 | } |
| 1119 | } |
John Stiles | 02b1128 | 2020-08-10 15:25:24 -0400 | [diff] [blame] | 1120 | this->writeSection(kSetDataSection); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1121 | } |
| 1122 | this->write(" }\n"); |
| 1123 | } |
| 1124 | |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 1125 | void CPPCodeGenerator::writeOnTextureSampler() { |
| 1126 | bool foundSampler = false; |
| 1127 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1128 | if (param->type().typeKind() == Type::TypeKind::kSampler) { |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 1129 | if (!foundSampler) { |
| 1130 | this->writef( |
| 1131 | "const GrFragmentProcessor::TextureSampler& %s::onTextureSampler(int " |
| 1132 | "index) const {\n", |
| 1133 | fFullName.c_str()); |
| 1134 | this->writef(" return IthTextureSampler(index, %s", |
| 1135 | HCodeGenerator::FieldName(String(param->fName).c_str()).c_str()); |
| 1136 | foundSampler = true; |
| 1137 | } else { |
| 1138 | this->writef(", %s", |
| 1139 | HCodeGenerator::FieldName(String(param->fName).c_str()).c_str()); |
| 1140 | } |
| 1141 | } |
| 1142 | } |
| 1143 | if (foundSampler) { |
| 1144 | this->write(");\n}\n"); |
| 1145 | } |
| 1146 | } |
| 1147 | |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 1148 | void CPPCodeGenerator::writeClone() { |
John Stiles | 02b1128 | 2020-08-10 15:25:24 -0400 | [diff] [blame] | 1149 | if (!this->writeSection(kCloneSection)) { |
| 1150 | if (fSectionAndParameterHelper.getSection(kFieldsSection)) { |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 1151 | fErrors.error(/*offset=*/0, "fragment processors with custom @fields must also have a " |
| 1152 | "custom @clone"); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 1153 | } |
| 1154 | this->writef("%s::%s(const %s& src)\n" |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 1155 | ": INHERITED(k%s_ClassID, src.optimizationFlags())", fFullName.c_str(), |
| 1156 | fFullName.c_str(), fFullName.c_str(), fFullName.c_str()); |
John Stiles | 06f3d08 | 2020-06-04 11:07:21 -0400 | [diff] [blame] | 1157 | for (const Variable* param : fSectionAndParameterHelper.getParameters()) { |
Robert Phillips | bce7d86 | 2019-02-21 22:53:57 +0000 | [diff] [blame] | 1158 | String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str()); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1159 | if (param->type().nonnullable() != *fContext.fFragmentProcessor_Type) { |
Ethan Nicholas | ee1c8a7 | 2019-02-22 10:50:47 -0500 | [diff] [blame] | 1160 | this->writef("\n, %s(src.%s)", |
| 1161 | fieldName.c_str(), |
| 1162 | fieldName.c_str()); |
| 1163 | } |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 1164 | } |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 1165 | this->writef(" {\n"); |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 1166 | this->writef(" this->cloneAndRegisterAllChildProcessors(src);\n"); |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 1167 | int samplerCount = 0; |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 1168 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1169 | if (param->type().typeKind() == Type::TypeKind::kSampler) { |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 1170 | ++samplerCount; |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 1171 | } |
| 1172 | } |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 1173 | if (samplerCount) { |
| 1174 | this->writef(" this->setTextureSamplerCnt(%d);", samplerCount); |
| 1175 | } |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 1176 | if (fAccessSampleCoordsDirectly) { |
| 1177 | this->writef(" this->setUsesSampleCoordsDirectly();\n"); |
| 1178 | } |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 1179 | this->write("}\n"); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 1180 | this->writef("std::unique_ptr<GrFragmentProcessor> %s::clone() const {\n", |
| 1181 | fFullName.c_str()); |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 1182 | this->writef(" return std::make_unique<%s>(*this);\n", |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 1183 | fFullName.c_str()); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 1184 | this->write("}\n"); |
| 1185 | } |
| 1186 | } |
| 1187 | |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 1188 | void CPPCodeGenerator::writeDumpInfo() { |
John Stiles | 8d9bf64 | 2020-08-12 15:07:45 -0400 | [diff] [blame] | 1189 | this->writef("#if GR_TEST_UTILS\n" |
John Stiles | cab5886 | 2020-08-12 15:47:06 -0400 | [diff] [blame] | 1190 | "SkString %s::onDumpInfo() const {\n", fFullName.c_str()); |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 1191 | |
| 1192 | if (!this->writeSection(kDumpInfoSection)) { |
| 1193 | if (fSectionAndParameterHelper.getSection(kFieldsSection)) { |
| 1194 | fErrors.error(/*offset=*/0, "fragment processors with custom @fields must also have a " |
| 1195 | "custom @dumpInfo"); |
| 1196 | } |
| 1197 | |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 1198 | String formatString; |
| 1199 | std::vector<String> argumentList; |
| 1200 | |
| 1201 | for (const Variable* param : fSectionAndParameterHelper.getParameters()) { |
| 1202 | // dumpInfo() doesn't need to log child FPs. |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1203 | if (param->type().nonnullable() == *fContext.fFragmentProcessor_Type) { |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 1204 | continue; |
| 1205 | } |
| 1206 | |
| 1207 | // Add this field onto the format string and argument list. |
| 1208 | String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str()); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1209 | String runtimeValue = this->formatRuntimeValue(param->type(), |
| 1210 | param->fModifiers.fLayout, |
| 1211 | param->fName, |
| 1212 | &argumentList); |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 1213 | formatString.appendf("%s%s=%s", |
| 1214 | formatString.empty() ? "" : ", ", |
| 1215 | fieldName.c_str(), |
| 1216 | runtimeValue.c_str()); |
| 1217 | } |
| 1218 | |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 1219 | if (!formatString.empty()) { |
John Stiles | cab5886 | 2020-08-12 15:47:06 -0400 | [diff] [blame] | 1220 | // Emit the finished format string and associated arguments. |
| 1221 | this->writef(" return SkStringPrintf(\"(%s)\"", formatString.c_str()); |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 1222 | |
John Stiles | cab5886 | 2020-08-12 15:47:06 -0400 | [diff] [blame] | 1223 | for (const String& argument : argumentList) { |
| 1224 | this->writef(", %s", argument.c_str()); |
| 1225 | } |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 1226 | |
John Stiles | cab5886 | 2020-08-12 15:47:06 -0400 | [diff] [blame] | 1227 | this->write(");"); |
| 1228 | } else { |
| 1229 | // No fields to dump at all; just return an empty string. |
| 1230 | this->write(" return SkString();"); |
| 1231 | } |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 1232 | } |
| 1233 | |
John Stiles | cab5886 | 2020-08-12 15:47:06 -0400 | [diff] [blame] | 1234 | this->write("\n" |
| 1235 | "}\n" |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 1236 | "#endif\n"); |
| 1237 | } |
| 1238 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1239 | void CPPCodeGenerator::writeTest() { |
John Stiles | 02b1128 | 2020-08-10 15:25:24 -0400 | [diff] [blame] | 1240 | const Section* test = fSectionAndParameterHelper.getSection(kTestCodeSection); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 1241 | if (test) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 1242 | this->writef( |
| 1243 | "GR_DEFINE_FRAGMENT_PROCESSOR_TEST(%s);\n" |
| 1244 | "#if GR_TEST_UTILS\n" |
| 1245 | "std::unique_ptr<GrFragmentProcessor> %s::TestCreate(GrProcessorTestData* %s) {\n", |
| 1246 | fFullName.c_str(), |
| 1247 | fFullName.c_str(), |
| 1248 | test->fArgument.c_str()); |
John Stiles | 02b1128 | 2020-08-10 15:25:24 -0400 | [diff] [blame] | 1249 | this->writeSection(kTestCodeSection); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 1250 | this->write("}\n" |
| 1251 | "#endif\n"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1252 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | void CPPCodeGenerator::writeGetKey() { |
| 1256 | this->writef("void %s::onGetGLSLProcessorKey(const GrShaderCaps& caps, " |
| 1257 | "GrProcessorKeyBuilder* b) const {\n", |
| 1258 | fFullName.c_str()); |
Ethan Nicholas | cab767f | 2019-07-01 13:32:07 -0400 | [diff] [blame] | 1259 | for (const auto& p : fProgram) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1260 | if (p.kind() == ProgramElement::Kind::kVar) { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1261 | const VarDeclarations& decls = p.as<VarDeclarations>(); |
Ethan Nicholas | cab767f | 2019-07-01 13:32:07 -0400 | [diff] [blame] | 1262 | for (const auto& raw : decls.fVars) { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1263 | const VarDeclaration& decl = raw->as<VarDeclaration>(); |
Ethan Nicholas | cab767f | 2019-07-01 13:32:07 -0400 | [diff] [blame] | 1264 | const Variable& var = *decl.fVar; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1265 | const Type& varType = var.type(); |
Ethan Nicholas | cab767f | 2019-07-01 13:32:07 -0400 | [diff] [blame] | 1266 | String nameString(var.fName); |
| 1267 | const char* name = nameString.c_str(); |
| 1268 | if (var.fModifiers.fLayout.fKey != Layout::kNo_Key && |
| 1269 | (var.fModifiers.fFlags & Modifiers::kUniform_Flag)) { |
| 1270 | fErrors.error(var.fOffset, |
| 1271 | "layout(key) may not be specified on uniforms"); |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 1272 | } |
Ethan Nicholas | cab767f | 2019-07-01 13:32:07 -0400 | [diff] [blame] | 1273 | switch (var.fModifiers.fLayout.fKey) { |
| 1274 | case Layout::kKey_Key: |
| 1275 | if (is_private(var)) { |
| 1276 | this->writef("%s %s =", |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1277 | HCodeGenerator::FieldType(fContext, varType, |
Ethan Nicholas | cab767f | 2019-07-01 13:32:07 -0400 | [diff] [blame] | 1278 | var.fModifiers.fLayout).c_str(), |
| 1279 | String(var.fName).c_str()); |
| 1280 | if (decl.fValue) { |
| 1281 | fCPPMode = true; |
| 1282 | this->writeExpression(*decl.fValue, kAssignment_Precedence); |
| 1283 | fCPPMode = false; |
| 1284 | } else { |
| 1285 | this->writef("%s", default_value(var).c_str()); |
| 1286 | } |
| 1287 | this->write(";\n"); |
| 1288 | } |
| 1289 | if (var.fModifiers.fLayout.fWhen.fLength) { |
| 1290 | this->writef("if (%s) {", String(var.fModifiers.fLayout.fWhen).c_str()); |
| 1291 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1292 | if (varType == *fContext.fHalf4_Type) { |
Ethan Nicholas | cab767f | 2019-07-01 13:32:07 -0400 | [diff] [blame] | 1293 | this->writef(" uint16_t red = SkFloatToHalf(%s.fR);\n", |
| 1294 | HCodeGenerator::FieldName(name).c_str()); |
| 1295 | this->writef(" uint16_t green = SkFloatToHalf(%s.fG);\n", |
| 1296 | HCodeGenerator::FieldName(name).c_str()); |
| 1297 | this->writef(" uint16_t blue = SkFloatToHalf(%s.fB);\n", |
| 1298 | HCodeGenerator::FieldName(name).c_str()); |
| 1299 | this->writef(" uint16_t alpha = SkFloatToHalf(%s.fA);\n", |
| 1300 | HCodeGenerator::FieldName(name).c_str()); |
| 1301 | this->write(" b->add32(((uint32_t)red << 16) | green);\n"); |
| 1302 | this->write(" b->add32(((uint32_t)blue << 16) | alpha);\n"); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1303 | } else if (varType == *fContext.fHalf_Type || |
| 1304 | varType == *fContext.fFloat_Type) { |
John Stiles | 45f5b03 | 2020-07-27 17:31:29 -0400 | [diff] [blame] | 1305 | this->writef(" b->add32(sk_bit_cast<uint32_t>(%s));\n", |
Ethan Nicholas | cab767f | 2019-07-01 13:32:07 -0400 | [diff] [blame] | 1306 | HCodeGenerator::FieldName(name).c_str()); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1307 | } else if (varType.isInteger() || varType == *fContext.fBool_Type || |
| 1308 | varType.typeKind() == Type::TypeKind::kEnum) { |
John Stiles | 45f5b03 | 2020-07-27 17:31:29 -0400 | [diff] [blame] | 1309 | this->writef(" b->add32((uint32_t) %s);\n", |
| 1310 | HCodeGenerator::FieldName(name).c_str()); |
| 1311 | } else { |
| 1312 | ABORT("NOT YET IMPLEMENTED: automatic key handling for %s\n", |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1313 | varType.displayName().c_str()); |
Ethan Nicholas | cab767f | 2019-07-01 13:32:07 -0400 | [diff] [blame] | 1314 | } |
| 1315 | if (var.fModifiers.fLayout.fWhen.fLength) { |
| 1316 | this->write("}"); |
| 1317 | } |
| 1318 | break; |
| 1319 | case Layout::kIdentity_Key: |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1320 | if (varType.typeKind() != Type::TypeKind::kMatrix) { |
Ethan Nicholas | cab767f | 2019-07-01 13:32:07 -0400 | [diff] [blame] | 1321 | fErrors.error(var.fOffset, |
| 1322 | "layout(key=identity) requires matrix type"); |
| 1323 | } |
| 1324 | this->writef(" b->add32(%s.isIdentity() ? 1 : 0);\n", |
| 1325 | HCodeGenerator::FieldName(name).c_str()); |
| 1326 | break; |
| 1327 | case Layout::kNo_Key: |
| 1328 | break; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1329 | } |
Ethan Nicholas | cab767f | 2019-07-01 13:32:07 -0400 | [diff] [blame] | 1330 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1331 | } |
| 1332 | } |
| 1333 | this->write("}\n"); |
| 1334 | } |
| 1335 | |
| 1336 | bool CPPCodeGenerator::generateCode() { |
| 1337 | std::vector<const Variable*> uniforms; |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1338 | for (const auto& p : fProgram) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1339 | if (p.kind() == ProgramElement::Kind::kVar) { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1340 | const VarDeclarations& decls = p.as<VarDeclarations>(); |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1341 | for (const auto& raw : decls.fVars) { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1342 | VarDeclaration& decl = raw->as<VarDeclaration>(); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1343 | if ((decl.fVar->fModifiers.fFlags & Modifiers::kUniform_Flag) && |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1344 | decl.fVar->type().typeKind() != Type::TypeKind::kSampler) { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1345 | uniforms.push_back(decl.fVar); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1346 | } |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 1347 | |
| 1348 | if (is_uniform_in(*decl.fVar)) { |
| 1349 | // Validate the "uniform in" declarations to make sure they are fully supported, |
| 1350 | // instead of generating surprising C++ |
| 1351 | const UniformCTypeMapper* mapper = |
| 1352 | UniformCTypeMapper::Get(fContext, *decl.fVar); |
| 1353 | if (mapper == nullptr) { |
| 1354 | fErrors.error(decl.fOffset, String(decl.fVar->fName) |
| 1355 | + "'s type is not supported for use as a 'uniform in'"); |
| 1356 | return false; |
| 1357 | } |
| 1358 | if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) { |
| 1359 | if (!mapper->supportsTracking()) { |
| 1360 | fErrors.error(decl.fOffset, String(decl.fVar->fName) |
| 1361 | + "'s type does not support state tracking"); |
| 1362 | return false; |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | } else { |
| 1367 | // If it's not a uniform_in, it's an error to be tracked |
| 1368 | if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) { |
| 1369 | fErrors.error(decl.fOffset, "Non-'in uniforms' cannot be tracked"); |
| 1370 | return false; |
| 1371 | } |
| 1372 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1373 | } |
| 1374 | } |
| 1375 | } |
| 1376 | const char* baseName = fName.c_str(); |
| 1377 | const char* fullName = fFullName.c_str(); |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 1378 | this->writef("%s\n", HCodeGenerator::GetHeader(fProgram, fErrors).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1379 | this->writef(kFragmentProcessorHeader, fullName); |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 1380 | this->writef("#include \"%s.h\"\n\n", fullName); |
John Stiles | 02b1128 | 2020-08-10 15:25:24 -0400 | [diff] [blame] | 1381 | this->writeSection(kCppSection); |
John Stiles | 45f5b03 | 2020-07-27 17:31:29 -0400 | [diff] [blame] | 1382 | this->writef("#include \"src/core/SkUtils.h\"\n" |
| 1383 | "#include \"src/gpu/GrTexture.h\"\n" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 1384 | "#include \"src/gpu/glsl/GrGLSLFragmentProcessor.h\"\n" |
| 1385 | "#include \"src/gpu/glsl/GrGLSLFragmentShaderBuilder.h\"\n" |
| 1386 | "#include \"src/gpu/glsl/GrGLSLProgramBuilder.h\"\n" |
| 1387 | "#include \"src/sksl/SkSLCPP.h\"\n" |
| 1388 | "#include \"src/sksl/SkSLUtil.h\"\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1389 | "class GrGLSL%s : public GrGLSLFragmentProcessor {\n" |
| 1390 | "public:\n" |
| 1391 | " GrGLSL%s() {}\n", |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 1392 | baseName, baseName); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1393 | bool result = this->writeEmitCode(uniforms); |
| 1394 | this->write("private:\n"); |
| 1395 | this->writeSetData(uniforms); |
| 1396 | this->writePrivateVars(); |
| 1397 | for (const auto& u : uniforms) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1398 | if (needs_uniform_var(*u) && !(u->fModifiers.fFlags & Modifiers::kIn_Flag)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1399 | this->writef(" UniformHandle %sVar;\n", |
| 1400 | HCodeGenerator::FieldName(String(u->fName).c_str()).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1401 | } |
| 1402 | } |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 1403 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1404 | if (needs_uniform_var(*param)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1405 | this->writef(" UniformHandle %sVar;\n", |
| 1406 | HCodeGenerator::FieldName(String(param->fName).c_str()).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1407 | } |
| 1408 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1409 | this->writef("};\n" |
| 1410 | "GrGLSLFragmentProcessor* %s::onCreateGLSLInstance() const {\n" |
| 1411 | " return new GrGLSL%s();\n" |
| 1412 | "}\n", |
| 1413 | fullName, baseName); |
| 1414 | this->writeGetKey(); |
| 1415 | this->writef("bool %s::onIsEqual(const GrFragmentProcessor& other) const {\n" |
| 1416 | " const %s& that = other.cast<%s>();\n" |
| 1417 | " (void) that;\n", |
| 1418 | fullName, fullName, fullName); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 1419 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1420 | if (param->type().nonnullable() == *fContext.fFragmentProcessor_Type) { |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 1421 | continue; |
| 1422 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1423 | String nameString(param->fName); |
| 1424 | const char* name = nameString.c_str(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1425 | this->writef(" if (%s != that.%s) return false;\n", |
| 1426 | HCodeGenerator::FieldName(name).c_str(), |
| 1427 | HCodeGenerator::FieldName(name).c_str()); |
| 1428 | } |
| 1429 | this->write(" return true;\n" |
| 1430 | "}\n"); |
John Stiles | 735a5a7 | 2020-08-26 10:21:10 -0400 | [diff] [blame] | 1431 | this->writef("bool %s::usesExplicitReturn() const {\n" |
| 1432 | " return %s;\n" |
| 1433 | "}\n", |
| 1434 | fullName, fReturnType == ReturnType::kUsesExplicitReturn ? "true" : "false"); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 1435 | this->writeClone(); |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 1436 | this->writeDumpInfo(); |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 1437 | this->writeOnTextureSampler(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1438 | this->writeTest(); |
John Stiles | 02b1128 | 2020-08-10 15:25:24 -0400 | [diff] [blame] | 1439 | this->writeSection(kCppEndSection); |
Greg Daniel | 3e8c345 | 2018-04-06 10:37:55 -0400 | [diff] [blame] | 1440 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1441 | result &= 0 == fErrors.errorCount(); |
| 1442 | return result; |
| 1443 | } |
| 1444 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 1445 | } // namespace SkSL |
Ethan Nicholas | 2a479a5 | 2020-08-18 16:29:45 -0400 | [diff] [blame] | 1446 | |
| 1447 | #endif // defined(SKSL_STANDALONE) || defined(GR_TEST_UTILS) |