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