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