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