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