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