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