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