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