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" |
| 11 | #include "SkSLHCodeGenerator.h" |
| 12 | |
| 13 | namespace SkSL { |
| 14 | |
| 15 | static bool needs_uniform_var(const Variable& var) { |
Ethan Nicholas | 5f9836e | 2017-12-20 15:16:33 -0500 | [diff] [blame] | 16 | return (var.fModifiers.fFlags & Modifiers::kUniform_Flag) && |
| 17 | var.fType.kind() != Type::kSampler_Kind; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | CPPCodeGenerator::CPPCodeGenerator(const Context* context, const Program* program, |
| 21 | ErrorReporter* errors, String name, OutputStream* out) |
| 22 | : INHERITED(context, program, errors, out) |
| 23 | , fName(std::move(name)) |
| 24 | , fFullName(String::printf("Gr%s", fName.c_str())) |
| 25 | , fSectionAndParameterHelper(*program, *errors) { |
| 26 | fLineEnding = "\\n"; |
| 27 | } |
| 28 | |
| 29 | void CPPCodeGenerator::writef(const char* s, va_list va) { |
| 30 | static constexpr int BUFFER_SIZE = 1024; |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 31 | va_list copy; |
| 32 | va_copy(copy, va); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 33 | char buffer[BUFFER_SIZE]; |
| 34 | int length = vsnprintf(buffer, BUFFER_SIZE, s, va); |
| 35 | if (length < BUFFER_SIZE) { |
| 36 | fOut->write(buffer, length); |
| 37 | } else { |
| 38 | std::unique_ptr<char[]> heap(new char[length + 1]); |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 39 | vsprintf(heap.get(), s, copy); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 40 | fOut->write(heap.get(), length); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void CPPCodeGenerator::writef(const char* s, ...) { |
| 45 | va_list va; |
| 46 | va_start(va, s); |
| 47 | this->writef(s, va); |
| 48 | va_end(va); |
| 49 | } |
| 50 | |
| 51 | void CPPCodeGenerator::writeHeader() { |
| 52 | } |
| 53 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 54 | bool CPPCodeGenerator::usesPrecisionModifiers() const { |
| 55 | return false; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 56 | } |
| 57 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 58 | String CPPCodeGenerator::getTypeName(const Type& type) { |
| 59 | return type.name(); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 60 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 61 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 62 | void CPPCodeGenerator::writeBinaryExpression(const BinaryExpression& b, |
| 63 | Precedence parentPrecedence) { |
| 64 | if (b.fOperator == Token::PERCENT) { |
| 65 | // need to use "%%" instead of "%" b/c the code will be inside of a printf |
| 66 | Precedence precedence = GetBinaryPrecedence(b.fOperator); |
| 67 | if (precedence >= parentPrecedence) { |
| 68 | this->write("("); |
| 69 | } |
| 70 | this->writeExpression(*b.fLeft, precedence); |
| 71 | this->write(" %% "); |
| 72 | this->writeExpression(*b.fRight, precedence); |
| 73 | if (precedence >= parentPrecedence) { |
| 74 | this->write(")"); |
| 75 | } |
| 76 | } else { |
| 77 | INHERITED::writeBinaryExpression(b, parentPrecedence); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void CPPCodeGenerator::writeIndexExpression(const IndexExpression& i) { |
| 82 | const Expression& base = *i.fBase; |
| 83 | if (base.fKind == Expression::kVariableReference_Kind) { |
| 84 | int builtin = ((VariableReference&) base).fVariable.fModifiers.fLayout.fBuiltin; |
| 85 | if (SK_TRANSFORMEDCOORDS2D_BUILTIN == builtin) { |
| 86 | this->write("%s"); |
| 87 | if (i.fIndex->fKind != Expression::kIntLiteral_Kind) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 88 | fErrors.error(i.fIndex->fOffset, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 89 | "index into sk_TransformedCoords2D must be an integer literal"); |
| 90 | return; |
| 91 | } |
| 92 | int64_t index = ((IntLiteral&) *i.fIndex).fValue; |
| 93 | String name = "sk_TransformedCoords2D_" + to_string(index); |
| 94 | fFormatArgs.push_back(name + ".c_str()"); |
| 95 | if (fWrittenTransformedCoords.find(index) == fWrittenTransformedCoords.end()) { |
Brian Osman | 72a37be | 2017-08-15 09:19:53 -0400 | [diff] [blame] | 96 | fExtraEmitCodeCode += " SkString " + name + |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 97 | " = fragBuilder->ensureCoords2D(args.fTransformedCoords[" + |
| 98 | to_string(index) + "]);\n"; |
| 99 | fWrittenTransformedCoords.insert(index); |
| 100 | } |
| 101 | return; |
| 102 | } else if (SK_TEXTURESAMPLERS_BUILTIN == builtin) { |
| 103 | this->write("%s"); |
| 104 | if (i.fIndex->fKind != Expression::kIntLiteral_Kind) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 105 | fErrors.error(i.fIndex->fOffset, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 106 | "index into sk_TextureSamplers must be an integer literal"); |
| 107 | return; |
| 108 | } |
| 109 | int64_t index = ((IntLiteral&) *i.fIndex).fValue; |
| 110 | fFormatArgs.push_back(" fragBuilder->getProgramBuilder()->samplerVariable(" |
| 111 | "args.fTexSamplers[" + to_string(index) + "]).c_str()"); |
| 112 | return; |
| 113 | } |
| 114 | } |
| 115 | INHERITED::writeIndexExpression(i); |
| 116 | } |
| 117 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 118 | static String default_value(const Type& type) { |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 119 | if (type.fName == "bool") { |
| 120 | return "false"; |
| 121 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 122 | switch (type.kind()) { |
| 123 | case Type::kScalar_Kind: return "0"; |
| 124 | case Type::kVector_Kind: return type.name() + "(0)"; |
| 125 | case Type::kMatrix_Kind: return type.name() + "(1)"; |
| 126 | default: ABORT("unsupported default_value type\n"); |
| 127 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 128 | } |
| 129 | |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 130 | static String default_value(const Variable& var) { |
| 131 | if (var.fModifiers.fLayout.fCType == "GrColor4f") { |
| 132 | return "GrColor4f::kIllegalConstructor"; |
| 133 | } |
| 134 | return default_value(var.fType); |
| 135 | } |
| 136 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 137 | static bool is_private(const Variable& var) { |
| 138 | return !(var.fModifiers.fFlags & Modifiers::kUniform_Flag) && |
| 139 | !(var.fModifiers.fFlags & Modifiers::kIn_Flag) && |
| 140 | var.fStorage == Variable::kGlobal_Storage && |
| 141 | var.fModifiers.fLayout.fBuiltin == -1; |
| 142 | } |
| 143 | |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 144 | void CPPCodeGenerator::writeRuntimeValue(const Type& type, const Layout& layout, |
| 145 | const String& cppCode) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 146 | if (type.isFloat()) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 147 | this->write("%f"); |
| 148 | fFormatArgs.push_back(cppCode); |
| 149 | } else if (type == *fContext.fInt_Type) { |
| 150 | this->write("%d"); |
| 151 | fFormatArgs.push_back(cppCode); |
| 152 | } else if (type == *fContext.fBool_Type) { |
| 153 | this->write("%s"); |
| 154 | fFormatArgs.push_back("(" + cppCode + " ? \"true\" : \"false\")"); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 155 | } else if (type == *fContext.fFloat2_Type || type == *fContext.fHalf2_Type) { |
| 156 | this->write(type.name() + "(%f, %f)"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 157 | fFormatArgs.push_back(cppCode + ".fX"); |
| 158 | fFormatArgs.push_back(cppCode + ".fY"); |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 159 | } else if (type == *fContext.fFloat4_Type || type == *fContext.fHalf4_Type) { |
| 160 | this->write(type.name() + "(%f, %f, %f, %f)"); |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 161 | if (layout.fCType == "SkPMColor") { |
| 162 | fFormatArgs.push_back("SkGetPackedR32(" + cppCode + ") / 255.0"); |
| 163 | fFormatArgs.push_back("SkGetPackedG32(" + cppCode + ") / 255.0"); |
| 164 | fFormatArgs.push_back("SkGetPackedB32(" + cppCode + ") / 255.0"); |
| 165 | fFormatArgs.push_back("SkGetPackedA32(" + cppCode + ") / 255.0"); |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 166 | } else if (layout.fCType == "GrColor4f") { |
| 167 | fFormatArgs.push_back(cppCode + ".fRGBA[0]"); |
| 168 | fFormatArgs.push_back(cppCode + ".fRGBA[1]"); |
| 169 | fFormatArgs.push_back(cppCode + ".fRGBA[2]"); |
| 170 | fFormatArgs.push_back(cppCode + ".fRGBA[3]"); |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 171 | } else { |
| 172 | fFormatArgs.push_back(cppCode + ".left()"); |
| 173 | fFormatArgs.push_back(cppCode + ".top()"); |
| 174 | fFormatArgs.push_back(cppCode + ".right()"); |
| 175 | fFormatArgs.push_back(cppCode + ".bottom()"); |
| 176 | } |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 177 | } else if (type.kind() == Type::kEnum_Kind) { |
| 178 | this->write("%d"); |
| 179 | fFormatArgs.push_back("(int) " + cppCode); |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame^] | 180 | } else if (type == *fContext.fInt4_Type || |
| 181 | type == *fContext.fShort4_Type || |
| 182 | type == *fContext.fByte4_Type) { |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 183 | this->write(type.name() + "(%d, %d, %d, %d)"); |
| 184 | fFormatArgs.push_back(cppCode + ".left()"); |
| 185 | fFormatArgs.push_back(cppCode + ".top()"); |
| 186 | fFormatArgs.push_back(cppCode + ".right()"); |
| 187 | fFormatArgs.push_back(cppCode + ".bottom()"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 188 | } else { |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 189 | printf("unsupported runtime value type '%s'\n", String(type.fName).c_str()); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 190 | SkASSERT(false); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | void CPPCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) { |
| 195 | if (is_private(var)) { |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 196 | this->writeRuntimeValue(var.fType, var.fModifiers.fLayout, var.fName); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 197 | } else { |
| 198 | this->writeExpression(value, kTopLevel_Precedence); |
| 199 | } |
| 200 | } |
| 201 | |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 202 | String CPPCodeGenerator::getSamplerHandle(const Variable& var) { |
| 203 | int samplerCount = 0; |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 204 | for (const auto param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 205 | if (&var == param) { |
| 206 | return "args.fTexSamplers[" + to_string(samplerCount) + "]"; |
| 207 | } |
| 208 | if (param->fType.kind() == Type::kSampler_Kind) { |
| 209 | ++samplerCount; |
| 210 | } |
| 211 | } |
| 212 | ABORT("should have found sampler in parameters\n"); |
| 213 | } |
| 214 | |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 215 | void CPPCodeGenerator::writeIntLiteral(const IntLiteral& i) { |
| 216 | this->write(to_string((int32_t) i.fValue)); |
| 217 | } |
| 218 | |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 219 | void CPPCodeGenerator::writeSwizzle(const Swizzle& swizzle) { |
| 220 | if (fCPPMode) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 221 | SkASSERT(swizzle.fComponents.size() == 1); // no support for multiple swizzle components yet |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 222 | this->writeExpression(*swizzle.fBase, kPostfix_Precedence); |
| 223 | switch (swizzle.fComponents[0]) { |
| 224 | case 0: this->write(".left()"); break; |
| 225 | case 1: this->write(".top()"); break; |
| 226 | case 2: this->write(".right()"); break; |
| 227 | case 3: this->write(".bottom()"); break; |
| 228 | } |
| 229 | } else { |
| 230 | INHERITED::writeSwizzle(swizzle); |
| 231 | } |
| 232 | } |
| 233 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 234 | void CPPCodeGenerator::writeVariableReference(const VariableReference& ref) { |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 235 | if (fCPPMode) { |
| 236 | this->write(ref.fVariable.fName); |
| 237 | return; |
| 238 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 239 | switch (ref.fVariable.fModifiers.fLayout.fBuiltin) { |
| 240 | case SK_INCOLOR_BUILTIN: |
| 241 | this->write("%s"); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 242 | fFormatArgs.push_back(String("args.fInputColor ? args.fInputColor : \"half4(1)\"")); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 243 | break; |
| 244 | case SK_OUTCOLOR_BUILTIN: |
| 245 | this->write("%s"); |
| 246 | fFormatArgs.push_back(String("args.fOutputColor")); |
| 247 | break; |
| 248 | default: |
| 249 | if (ref.fVariable.fType.kind() == Type::kSampler_Kind) { |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 250 | this->write("%s"); |
| 251 | fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerVariable(" + |
| 252 | this->getSamplerHandle(ref.fVariable) + ").c_str()"); |
| 253 | return; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 254 | } |
| 255 | if (ref.fVariable.fModifiers.fFlags & Modifiers::kUniform_Flag) { |
| 256 | this->write("%s"); |
| 257 | String name = ref.fVariable.fName; |
Brian Osman | 1cb4171 | 2017-10-19 12:54:52 -0400 | [diff] [blame] | 258 | String var = String::printf("args.fUniformHandler->getUniformCStr(%sVar)", |
| 259 | HCodeGenerator::FieldName(name.c_str()).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 260 | String code; |
| 261 | if (ref.fVariable.fModifiers.fLayout.fWhen.size()) { |
| 262 | code = String::printf("%sVar.isValid() ? %s : \"%s\"", |
| 263 | HCodeGenerator::FieldName(name.c_str()).c_str(), |
| 264 | var.c_str(), |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 265 | default_value(ref.fVariable.fType).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 266 | } else { |
| 267 | code = var; |
| 268 | } |
| 269 | fFormatArgs.push_back(code); |
| 270 | } else if (SectionAndParameterHelper::IsParameter(ref.fVariable)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 271 | String name(ref.fVariable.fName); |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 272 | this->writeRuntimeValue(ref.fVariable.fType, ref.fVariable.fModifiers.fLayout, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 273 | String::printf("_outer.%s()", name.c_str()).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 274 | } else { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 275 | this->write(ref.fVariable.fName); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 280 | void CPPCodeGenerator::writeIfStatement(const IfStatement& s) { |
| 281 | if (s.fIsStatic) { |
| 282 | this->write("@"); |
| 283 | } |
| 284 | INHERITED::writeIfStatement(s); |
| 285 | } |
| 286 | |
| 287 | void CPPCodeGenerator::writeSwitchStatement(const SwitchStatement& s) { |
| 288 | if (s.fIsStatic) { |
| 289 | this->write("@"); |
| 290 | } |
| 291 | INHERITED::writeSwitchStatement(s); |
| 292 | } |
| 293 | |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 294 | void CPPCodeGenerator::writeFunctionCall(const FunctionCall& c) { |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 295 | if (c.fFunction.fBuiltin && c.fFunction.fName == "process") { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 296 | SkASSERT(c.fArguments.size() == 1); |
| 297 | SkASSERT(Expression::kVariableReference_Kind == c.fArguments[0]->fKind); |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 298 | int index = 0; |
| 299 | bool found = false; |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 300 | for (const auto& p : fProgram) { |
| 301 | if (ProgramElement::kVar_Kind == p.fKind) { |
| 302 | const VarDeclarations& decls = (const VarDeclarations&) p; |
| 303 | for (const auto& raw : decls.fVars) { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 304 | VarDeclaration& decl = (VarDeclaration&) *raw; |
| 305 | if (decl.fVar == &((VariableReference&) *c.fArguments[0]).fVariable) { |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 306 | found = true; |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 307 | } else if (decl.fVar->fType == *fContext.fFragmentProcessor_Type) { |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 308 | ++index; |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | if (found) { |
| 313 | break; |
| 314 | } |
| 315 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 316 | SkASSERT(found); |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 317 | String childName = "_child" + to_string(index); |
| 318 | fExtraEmitCodeCode += " SkString " + childName + "(\"" + childName + "\");\n" + |
| 319 | " this->emitChild(" + to_string(index) + ", &" + childName + |
| 320 | ", args);\n"; |
| 321 | this->write("%s"); |
| 322 | fFormatArgs.push_back(childName + ".c_str()"); |
| 323 | return; |
| 324 | } |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 325 | INHERITED::writeFunctionCall(c); |
| 326 | if (c.fFunction.fBuiltin && c.fFunction.fName == "texture") { |
| 327 | this->write(".%s"); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 328 | SkASSERT(c.fArguments.size() >= 1); |
| 329 | SkASSERT(c.fArguments[0]->fKind == Expression::kVariableReference_Kind); |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 330 | String sampler = this->getSamplerHandle(((VariableReference&) *c.fArguments[0]).fVariable); |
| 331 | fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerSwizzle(" + sampler + |
| 332 | ").c_str()"); |
| 333 | } |
| 334 | } |
| 335 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 336 | void CPPCodeGenerator::writeFunction(const FunctionDefinition& f) { |
| 337 | if (f.fDeclaration.fName == "main") { |
| 338 | fFunctionHeader = ""; |
| 339 | OutputStream* oldOut = fOut; |
| 340 | StringStream buffer; |
| 341 | fOut = &buffer; |
| 342 | for (const auto& s : ((Block&) *f.fBody).fStatements) { |
| 343 | this->writeStatement(*s); |
| 344 | this->writeLine(); |
| 345 | } |
| 346 | |
| 347 | fOut = oldOut; |
| 348 | this->write(fFunctionHeader); |
| 349 | this->write(buffer.str()); |
| 350 | } else { |
| 351 | INHERITED::writeFunction(f); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | void CPPCodeGenerator::writeSetting(const Setting& s) { |
| 356 | static constexpr const char* kPrefix = "sk_Args."; |
| 357 | if (!strncmp(s.fName.c_str(), kPrefix, strlen(kPrefix))) { |
| 358 | const char* name = s.fName.c_str() + strlen(kPrefix); |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 359 | this->writeRuntimeValue(s.fType, Layout(), HCodeGenerator::FieldName(name).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 360 | } else { |
| 361 | this->write(s.fName.c_str()); |
| 362 | } |
| 363 | } |
| 364 | |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 365 | bool CPPCodeGenerator::writeSection(const char* name, const char* prefix) { |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 366 | const Section* s = fSectionAndParameterHelper.getSection(name); |
| 367 | if (s) { |
| 368 | this->writef("%s%s", prefix, s->fText.c_str()); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 369 | return true; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 370 | } |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 371 | return false; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | void CPPCodeGenerator::writeProgramElement(const ProgramElement& p) { |
| 375 | if (p.fKind == ProgramElement::kSection_Kind) { |
| 376 | return; |
| 377 | } |
| 378 | if (p.fKind == ProgramElement::kVar_Kind) { |
| 379 | const VarDeclarations& decls = (const VarDeclarations&) p; |
| 380 | if (!decls.fVars.size()) { |
| 381 | return; |
| 382 | } |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 383 | const Variable& var = *((VarDeclaration&) *decls.fVars[0]).fVar; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 384 | if (var.fModifiers.fFlags & (Modifiers::kIn_Flag | Modifiers::kUniform_Flag) || |
| 385 | -1 != var.fModifiers.fLayout.fBuiltin) { |
| 386 | return; |
| 387 | } |
| 388 | } |
| 389 | INHERITED::writeProgramElement(p); |
| 390 | } |
| 391 | |
| 392 | void CPPCodeGenerator::addUniform(const Variable& var) { |
| 393 | if (!needs_uniform_var(var)) { |
| 394 | return; |
| 395 | } |
| 396 | const char* precision; |
| 397 | if (var.fModifiers.fFlags & Modifiers::kHighp_Flag) { |
| 398 | precision = "kHigh_GrSLPrecision"; |
| 399 | } else if (var.fModifiers.fFlags & Modifiers::kMediump_Flag) { |
| 400 | precision = "kMedium_GrSLPrecision"; |
| 401 | } else if (var.fModifiers.fFlags & Modifiers::kLowp_Flag) { |
| 402 | precision = "kLow_GrSLPrecision"; |
| 403 | } else { |
| 404 | precision = "kDefault_GrSLPrecision"; |
| 405 | } |
| 406 | const char* type; |
| 407 | if (var.fType == *fContext.fFloat_Type) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 408 | type = "kFloat_GrSLType"; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 409 | } else if (var.fType == *fContext.fHalf_Type) { |
| 410 | type = "kHalf_GrSLType"; |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 411 | } else if (var.fType == *fContext.fFloat2_Type) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 412 | type = "kFloat2_GrSLType"; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 413 | } else if (var.fType == *fContext.fHalf2_Type) { |
| 414 | type = "kHalf2_GrSLType"; |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 415 | } else if (var.fType == *fContext.fFloat4_Type) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 416 | type = "kFloat4_GrSLType"; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 417 | } else if (var.fType == *fContext.fHalf4_Type) { |
| 418 | type = "kHalf4_GrSLType"; |
Brian Osman | 1cb4171 | 2017-10-19 12:54:52 -0400 | [diff] [blame] | 419 | } else if (var.fType == *fContext.fFloat4x4_Type) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 420 | type = "kFloat4x4_GrSLType"; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 421 | } else if (var.fType == *fContext.fHalf4x4_Type) { |
| 422 | type = "kHalf4x4_GrSLType"; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 423 | } else { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 424 | ABORT("unsupported uniform type: %s %s;\n", String(var.fType.fName).c_str(), |
| 425 | String(var.fName).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 426 | } |
| 427 | if (var.fModifiers.fLayout.fWhen.size()) { |
| 428 | this->writef(" if (%s) {\n ", var.fModifiers.fLayout.fWhen.c_str()); |
| 429 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 430 | String name(var.fName); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 431 | this->writef(" %sVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, %s, " |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 432 | "%s, \"%s\");\n", HCodeGenerator::FieldName(name.c_str()).c_str(), type, precision, |
| 433 | name.c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 434 | if (var.fModifiers.fLayout.fWhen.size()) { |
| 435 | this->write(" }\n"); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | void CPPCodeGenerator::writePrivateVars() { |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 440 | for (const auto& p : fProgram) { |
| 441 | if (ProgramElement::kVar_Kind == p.fKind) { |
| 442 | const VarDeclarations& decls = (const VarDeclarations&) p; |
| 443 | for (const auto& raw : decls.fVars) { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 444 | VarDeclaration& decl = (VarDeclaration&) *raw; |
| 445 | if (is_private(*decl.fVar)) { |
| 446 | if (decl.fVar->fType == *fContext.fFragmentProcessor_Type) { |
| 447 | fErrors.error(decl.fOffset, |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 448 | "fragmentProcessor variables must be declared 'in'"); |
| 449 | return; |
| 450 | } |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 451 | this->writef("%s %s = %s;\n", |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 452 | HCodeGenerator::FieldType(fContext, decl.fVar->fType, |
| 453 | decl.fVar->fModifiers.fLayout).c_str(), |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 454 | String(decl.fVar->fName).c_str(), |
| 455 | default_value(*decl.fVar).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | void CPPCodeGenerator::writePrivateVarValues() { |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 463 | for (const auto& p : fProgram) { |
| 464 | if (ProgramElement::kVar_Kind == p.fKind) { |
| 465 | const VarDeclarations& decls = (const VarDeclarations&) p; |
| 466 | for (const auto& raw : decls.fVars) { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 467 | VarDeclaration& decl = (VarDeclaration&) *raw; |
| 468 | if (is_private(*decl.fVar) && decl.fValue) { |
| 469 | this->writef("%s = ", String(decl.fVar->fName).c_str()); |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 470 | fCPPMode = true; |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 471 | this->writeExpression(*decl.fValue, kAssignment_Precedence); |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 472 | fCPPMode = false; |
| 473 | this->write(";\n"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 480 | static bool is_accessible(const Variable& var) { |
| 481 | return Type::kSampler_Kind != var.fType.kind() && |
| 482 | Type::kOther_Kind != var.fType.kind(); |
| 483 | } |
| 484 | |
Ethan Nicholas | 5b6e627 | 2017-10-13 13:11:06 -0400 | [diff] [blame] | 485 | void CPPCodeGenerator::writeCodeAppend(const String& code) { |
| 486 | // codeAppendf can only handle appending 1024 bytes at a time, so we need to break the string |
| 487 | // into chunks. Unfortunately we can't tell exactly how long the string is going to end up, |
| 488 | // because printf escape sequences get replaced by strings of unknown length, but keeping the |
| 489 | // format string below 512 bytes is probably safe. |
| 490 | static constexpr size_t maxChunkSize = 512; |
| 491 | size_t start = 0; |
| 492 | size_t index = 0; |
| 493 | size_t argStart = 0; |
| 494 | size_t argCount; |
| 495 | while (index < code.size()) { |
| 496 | argCount = 0; |
| 497 | this->write(" fragBuilder->codeAppendf(\""); |
| 498 | while (index < code.size() && index < start + maxChunkSize) { |
| 499 | if ('%' == code[index]) { |
| 500 | if (index == start + maxChunkSize - 1 || index == code.size() - 1) { |
| 501 | break; |
| 502 | } |
| 503 | if (code[index + 1] != '%') { |
| 504 | ++argCount; |
| 505 | } |
Ethan Nicholas | ef0c9fd | 2017-10-30 10:04:14 -0400 | [diff] [blame] | 506 | } else if ('\\' == code[index] && index == start + maxChunkSize - 1) { |
| 507 | // avoid splitting an escape sequence that happens to fall across a chunk boundary |
| 508 | break; |
Ethan Nicholas | 5b6e627 | 2017-10-13 13:11:06 -0400 | [diff] [blame] | 509 | } |
| 510 | ++index; |
| 511 | } |
| 512 | fOut->write(code.c_str() + start, index - start); |
| 513 | this->write("\""); |
| 514 | for (size_t i = argStart; i < argStart + argCount; ++i) { |
| 515 | this->writef(", %s", fFormatArgs[i].c_str()); |
| 516 | } |
| 517 | this->write(");\n"); |
| 518 | argStart += argCount; |
| 519 | start = index; |
| 520 | } |
| 521 | } |
| 522 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 523 | bool CPPCodeGenerator::writeEmitCode(std::vector<const Variable*>& uniforms) { |
| 524 | this->write(" void emitCode(EmitArgs& args) override {\n" |
| 525 | " GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;\n"); |
| 526 | this->writef(" const %s& _outer = args.fFp.cast<%s>();\n" |
| 527 | " (void) _outer;\n", |
| 528 | fFullName.c_str(), fFullName.c_str()); |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 529 | for (const auto& p : fProgram) { |
| 530 | if (ProgramElement::kVar_Kind == p.fKind) { |
| 531 | const VarDeclarations& decls = (const VarDeclarations&) p; |
| 532 | for (const auto& raw : decls.fVars) { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 533 | VarDeclaration& decl = (VarDeclaration&) *raw; |
| 534 | String nameString(decl.fVar->fName); |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 535 | const char* name = nameString.c_str(); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 536 | if (SectionAndParameterHelper::IsParameter(*decl.fVar) && |
| 537 | is_accessible(*decl.fVar)) { |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 538 | this->writef(" auto %s = _outer.%s();\n" |
| 539 | " (void) %s;\n", |
| 540 | name, name, name); |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 545 | this->writePrivateVarValues(); |
| 546 | for (const auto u : uniforms) { |
| 547 | this->addUniform(*u); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 548 | } |
| 549 | this->writeSection(EMIT_CODE_SECTION); |
| 550 | OutputStream* old = fOut; |
| 551 | StringStream mainBuffer; |
| 552 | fOut = &mainBuffer; |
| 553 | bool result = INHERITED::generateCode(); |
| 554 | fOut = old; |
Ethan Nicholas | 5b6e627 | 2017-10-13 13:11:06 -0400 | [diff] [blame] | 555 | this->writef("%s", fExtraEmitCodeCode.c_str()); |
| 556 | this->writeCodeAppend(mainBuffer.str()); |
| 557 | this->write(" }\n"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 558 | return result; |
| 559 | } |
| 560 | |
| 561 | void CPPCodeGenerator::writeSetData(std::vector<const Variable*>& uniforms) { |
| 562 | const char* fullName = fFullName.c_str(); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 563 | const Section* section = fSectionAndParameterHelper.getSection(SET_DATA_SECTION); |
| 564 | const char* pdman = section ? section->fArgument.c_str() : "pdman"; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 565 | this->writef(" void onSetData(const GrGLSLProgramDataManager& %s, " |
| 566 | "const GrFragmentProcessor& _proc) override {\n", |
| 567 | pdman); |
| 568 | bool wroteProcessor = false; |
| 569 | for (const auto u : uniforms) { |
| 570 | if (u->fModifiers.fFlags & Modifiers::kIn_Flag) { |
| 571 | if (!wroteProcessor) { |
| 572 | this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName, fullName); |
| 573 | wroteProcessor = true; |
| 574 | this->writef(" {\n"); |
| 575 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 576 | String nameString(u->fName); |
| 577 | const char* name = nameString.c_str(); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 578 | if (u->fType == *fContext.fFloat4_Type || u->fType == *fContext.fHalf4_Type) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 579 | this->writef(" const SkRect %sValue = _outer.%s();\n" |
Ethan Nicholas | ee33873 | 2017-07-17 15:13:45 -0400 | [diff] [blame] | 580 | " %s.set4fv(%sVar, 1, (float*) &%sValue);\n", |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 581 | name, name, pdman, HCodeGenerator::FieldName(name).c_str(), name); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 582 | } else if (u->fType == *fContext.fFloat4x4_Type || |
| 583 | u->fType == *fContext.fHalf4x4_Type) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 584 | this->writef(" float %sValue[16];\n" |
| 585 | " _outer.%s().asColMajorf(%sValue);\n" |
| 586 | " %s.setMatrix4f(%sVar, %sValue);\n", |
| 587 | name, name, name, pdman, HCodeGenerator::FieldName(name).c_str(), |
| 588 | name); |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 589 | } else if (u->fType == *fContext.fFragmentProcessor_Type) { |
| 590 | // do nothing |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 591 | } else { |
| 592 | this->writef(" %s.set1f(%sVar, _outer.%s());\n", |
| 593 | pdman, HCodeGenerator::FieldName(name).c_str(), name); |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | if (wroteProcessor) { |
| 598 | this->writef(" }\n"); |
| 599 | } |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 600 | if (section) { |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 601 | int samplerIndex = 0; |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 602 | for (const auto& p : fProgram) { |
| 603 | if (ProgramElement::kVar_Kind == p.fKind) { |
| 604 | const VarDeclarations& decls = (const VarDeclarations&) p; |
| 605 | for (const auto& raw : decls.fVars) { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 606 | VarDeclaration& decl = (VarDeclaration&) *raw; |
| 607 | String nameString(decl.fVar->fName); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 608 | const char* name = nameString.c_str(); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 609 | if (decl.fVar->fType.kind() == Type::kSampler_Kind) { |
| 610 | this->writef(" GrSurfaceProxy& %sProxy = " |
| 611 | "*_outer.textureSampler(%d).proxy();\n", |
| 612 | name, samplerIndex); |
| 613 | this->writef(" GrTexture& %s = *%sProxy.priv().peekTexture();\n", |
| 614 | name, name); |
| 615 | this->writef(" (void) %s;\n", name); |
| 616 | ++samplerIndex; |
| 617 | } else if (needs_uniform_var(*decl.fVar)) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 618 | this->writef(" UniformHandle& %s = %sVar;\n" |
| 619 | " (void) %s;\n", |
| 620 | name, HCodeGenerator::FieldName(name).c_str(), name); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 621 | } else if (SectionAndParameterHelper::IsParameter(*decl.fVar) && |
| 622 | decl.fVar->fType != *fContext.fFragmentProcessor_Type) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 623 | if (!wroteProcessor) { |
| 624 | this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName, |
| 625 | fullName); |
| 626 | wroteProcessor = true; |
| 627 | } |
| 628 | this->writef(" auto %s = _outer.%s();\n" |
| 629 | " (void) %s;\n", |
| 630 | name, name, name); |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | this->writeSection(SET_DATA_SECTION); |
| 636 | } |
| 637 | this->write(" }\n"); |
| 638 | } |
| 639 | |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 640 | void CPPCodeGenerator::writeClone() { |
| 641 | if (!this->writeSection(CLONE_SECTION)) { |
| 642 | if (fSectionAndParameterHelper.getSection(FIELDS_SECTION)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 643 | fErrors.error(0, "fragment processors with custom @fields must also have a custom" |
| 644 | "@clone"); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 645 | } |
| 646 | this->writef("%s::%s(const %s& src)\n" |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 647 | ": INHERITED(k%s_ClassID, src.optimizationFlags())", fFullName.c_str(), |
| 648 | fFullName.c_str(), fFullName.c_str(), fFullName.c_str()); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 649 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 650 | if (param->fType == *fContext.fFragmentProcessor_Type) { |
| 651 | continue; |
| 652 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 653 | String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str()); |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 654 | this->writef("\n, %s(src.%s)", |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 655 | fieldName.c_str(), |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 656 | fieldName.c_str()); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 657 | } |
| 658 | for (const Section* s : fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION)) { |
| 659 | String fieldName = HCodeGenerator::FieldName(s->fArgument.c_str()); |
| 660 | this->writef("\n, %sCoordTransform(src.%sCoordTransform)", fieldName.c_str(), |
| 661 | fieldName.c_str()); |
| 662 | } |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 663 | this->writef(" {\n"); |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 664 | int childCount = 0; |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 665 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
| 666 | if (param->fType.kind() == Type::kSampler_Kind) { |
| 667 | this->writef(" this->addTextureSampler(&%s);\n", |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 668 | HCodeGenerator::FieldName(String(param->fName).c_str()).c_str()); |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 669 | } else if (param->fType == *fContext.fFragmentProcessor_Type) { |
| 670 | this->writef(" this->registerChildProcessor(src.childProcessor(%d).clone());" |
| 671 | "\n", childCount++); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 672 | } |
| 673 | } |
| 674 | for (const Section* s : fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION)) { |
| 675 | String field = HCodeGenerator::FieldName(s->fArgument.c_str()); |
| 676 | this->writef(" this->addCoordTransform(&%sCoordTransform);\n", field.c_str()); |
| 677 | } |
| 678 | this->write("}\n"); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 679 | this->writef("std::unique_ptr<GrFragmentProcessor> %s::clone() const {\n", |
| 680 | fFullName.c_str()); |
| 681 | this->writef(" return std::unique_ptr<GrFragmentProcessor>(new %s(*this));\n", |
| 682 | fFullName.c_str()); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 683 | this->write("}\n"); |
| 684 | } |
| 685 | } |
| 686 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 687 | void CPPCodeGenerator::writeTest() { |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 688 | const Section* test = fSectionAndParameterHelper.getSection(TEST_CODE_SECTION); |
| 689 | if (test) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 690 | this->writef( |
| 691 | "GR_DEFINE_FRAGMENT_PROCESSOR_TEST(%s);\n" |
| 692 | "#if GR_TEST_UTILS\n" |
| 693 | "std::unique_ptr<GrFragmentProcessor> %s::TestCreate(GrProcessorTestData* %s) {\n", |
| 694 | fFullName.c_str(), |
| 695 | fFullName.c_str(), |
| 696 | test->fArgument.c_str()); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 697 | this->writeSection(TEST_CODE_SECTION); |
| 698 | this->write("}\n" |
| 699 | "#endif\n"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 700 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | void CPPCodeGenerator::writeGetKey() { |
| 704 | this->writef("void %s::onGetGLSLProcessorKey(const GrShaderCaps& caps, " |
| 705 | "GrProcessorKeyBuilder* b) const {\n", |
| 706 | fFullName.c_str()); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 707 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 708 | String nameString(param->fName); |
| 709 | const char* name = nameString.c_str(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 710 | if (param->fModifiers.fLayout.fKey != Layout::kNo_Key && |
| 711 | (param->fModifiers.fFlags & Modifiers::kUniform_Flag)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 712 | fErrors.error(param->fOffset, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 713 | "layout(key) may not be specified on uniforms"); |
| 714 | } |
| 715 | switch (param->fModifiers.fLayout.fKey) { |
| 716 | case Layout::kKey_Key: |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 717 | if (param->fType == *fContext.fFloat4x4_Type) { |
| 718 | ABORT("no automatic key handling for float4x4\n"); |
| 719 | } else if (param->fType == *fContext.fFloat2_Type) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 720 | this->writef(" b->add32(%s.fX);\n", |
| 721 | HCodeGenerator::FieldName(name).c_str()); |
| 722 | this->writef(" b->add32(%s.fY);\n", |
| 723 | HCodeGenerator::FieldName(name).c_str()); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 724 | } else if (param->fType == *fContext.fFloat4_Type) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 725 | this->writef(" b->add32(%s.x());\n", |
| 726 | HCodeGenerator::FieldName(name).c_str()); |
| 727 | this->writef(" b->add32(%s.y());\n", |
| 728 | HCodeGenerator::FieldName(name).c_str()); |
| 729 | this->writef(" b->add32(%s.width());\n", |
| 730 | HCodeGenerator::FieldName(name).c_str()); |
| 731 | this->writef(" b->add32(%s.height());\n", |
| 732 | HCodeGenerator::FieldName(name).c_str()); |
| 733 | } else { |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 734 | this->writef(" b->add32((int32_t) %s);\n", |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 735 | HCodeGenerator::FieldName(name).c_str()); |
| 736 | } |
| 737 | break; |
| 738 | case Layout::kIdentity_Key: |
| 739 | if (param->fType.kind() != Type::kMatrix_Kind) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 740 | fErrors.error(param->fOffset, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 741 | "layout(key=identity) requires matrix type"); |
| 742 | } |
| 743 | this->writef(" b->add32(%s.isIdentity() ? 1 : 0);\n", |
| 744 | HCodeGenerator::FieldName(name).c_str()); |
| 745 | break; |
| 746 | case Layout::kNo_Key: |
| 747 | break; |
| 748 | } |
| 749 | } |
| 750 | this->write("}\n"); |
| 751 | } |
| 752 | |
| 753 | bool CPPCodeGenerator::generateCode() { |
| 754 | std::vector<const Variable*> uniforms; |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 755 | for (const auto& p : fProgram) { |
| 756 | if (ProgramElement::kVar_Kind == p.fKind) { |
| 757 | const VarDeclarations& decls = (const VarDeclarations&) p; |
| 758 | for (const auto& raw : decls.fVars) { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 759 | VarDeclaration& decl = (VarDeclaration&) *raw; |
| 760 | if ((decl.fVar->fModifiers.fFlags & Modifiers::kUniform_Flag) && |
| 761 | decl.fVar->fType.kind() != Type::kSampler_Kind) { |
| 762 | uniforms.push_back(decl.fVar); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | const char* baseName = fName.c_str(); |
| 768 | const char* fullName = fFullName.c_str(); |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 769 | this->writef("%s\n", HCodeGenerator::GetHeader(fProgram, fErrors).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 770 | this->writef(kFragmentProcessorHeader, fullName); |
Greg Daniel | 3e8c345 | 2018-04-06 10:37:55 -0400 | [diff] [blame] | 771 | this->writef("#include \"%s.h\"\n", fullName); |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 772 | this->writeSection(CPP_SECTION); |
Brian Osman | 1cb4171 | 2017-10-19 12:54:52 -0400 | [diff] [blame] | 773 | this->writef("#include \"glsl/GrGLSLFragmentProcessor.h\"\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 774 | "#include \"glsl/GrGLSLFragmentShaderBuilder.h\"\n" |
| 775 | "#include \"glsl/GrGLSLProgramBuilder.h\"\n" |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 776 | "#include \"GrTexture.h\"\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 777 | "#include \"SkSLCPP.h\"\n" |
| 778 | "#include \"SkSLUtil.h\"\n" |
| 779 | "class GrGLSL%s : public GrGLSLFragmentProcessor {\n" |
| 780 | "public:\n" |
| 781 | " GrGLSL%s() {}\n", |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 782 | baseName, baseName); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 783 | bool result = this->writeEmitCode(uniforms); |
| 784 | this->write("private:\n"); |
| 785 | this->writeSetData(uniforms); |
| 786 | this->writePrivateVars(); |
| 787 | for (const auto& u : uniforms) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 788 | if (needs_uniform_var(*u) && !(u->fModifiers.fFlags & Modifiers::kIn_Flag)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 789 | this->writef(" UniformHandle %sVar;\n", |
| 790 | HCodeGenerator::FieldName(String(u->fName).c_str()).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 791 | } |
| 792 | } |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 793 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 794 | if (needs_uniform_var(*param)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 795 | this->writef(" UniformHandle %sVar;\n", |
| 796 | HCodeGenerator::FieldName(String(param->fName).c_str()).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 797 | } |
| 798 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 799 | this->writef("};\n" |
| 800 | "GrGLSLFragmentProcessor* %s::onCreateGLSLInstance() const {\n" |
| 801 | " return new GrGLSL%s();\n" |
| 802 | "}\n", |
| 803 | fullName, baseName); |
| 804 | this->writeGetKey(); |
| 805 | this->writef("bool %s::onIsEqual(const GrFragmentProcessor& other) const {\n" |
| 806 | " const %s& that = other.cast<%s>();\n" |
| 807 | " (void) that;\n", |
| 808 | fullName, fullName, fullName); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 809 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 810 | if (param->fType == *fContext.fFragmentProcessor_Type) { |
| 811 | continue; |
| 812 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 813 | String nameString(param->fName); |
| 814 | const char* name = nameString.c_str(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 815 | this->writef(" if (%s != that.%s) return false;\n", |
| 816 | HCodeGenerator::FieldName(name).c_str(), |
| 817 | HCodeGenerator::FieldName(name).c_str()); |
| 818 | } |
| 819 | this->write(" return true;\n" |
| 820 | "}\n"); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 821 | this->writeClone(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 822 | this->writeTest(); |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 823 | this->writeSection(CPP_END_SECTION); |
Greg Daniel | 3e8c345 | 2018-04-06 10:37:55 -0400 | [diff] [blame] | 824 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 825 | result &= 0 == fErrors.errorCount(); |
| 826 | return result; |
| 827 | } |
| 828 | |
| 829 | } // namespace |