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