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