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