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