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