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