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