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