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 | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame^] | 100 | addExtraEmitCodeLine("SkString " + name + |
| 101 | " = fragBuilder->ensureCoords2D(args.fTransformedCoords[" + |
| 102 | to_string(index) + "]);"); |
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 | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame^] | 364 | // Start a new extra emit code section so that the emitted child processor can depend on |
| 365 | // sksl variables defined in earlier sksl code. |
| 366 | this->newExtraEmitCodeBlock(); |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 367 | |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame^] | 368 | // Set to the empty string when no input color parameter should be emitted, which means this |
| 369 | // must be properly formatted with a prefixed comma when the parameter should be inserted |
| 370 | // 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 | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame^] | 374 | // Use the emitChild() variant that accepts an input color, so convert the 2nd |
| 375 | // argument's expression into C++ code that produces sksl stored in an SkString. |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 376 | String inputName = "_input" + to_string(index); |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame^] | 377 | addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments[1], inputName)); |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 378 | |
| 379 | // emitChild() needs a char* |
| 380 | inputArg = ", " + inputName + ".c_str()"; |
| 381 | } |
| 382 | |
| 383 | // Write the output handling after the possible input handling |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 384 | String childName = "_child" + to_string(index); |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame^] | 385 | addExtraEmitCodeLine("SkString " + childName + "(\"" + childName + "\");"); |
| 386 | addExtraEmitCodeLine("this->emitChild(" + to_string(index) + inputArg + |
| 387 | ", &" + childName + ", args);"); |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 388 | |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 389 | this->write("%s"); |
| 390 | fFormatArgs.push_back(childName + ".c_str()"); |
| 391 | return; |
| 392 | } |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 393 | INHERITED::writeFunctionCall(c); |
| 394 | if (c.fFunction.fBuiltin && c.fFunction.fName == "texture") { |
| 395 | this->write(".%s"); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 396 | SkASSERT(c.fArguments.size() >= 1); |
| 397 | SkASSERT(c.fArguments[0]->fKind == Expression::kVariableReference_Kind); |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 398 | String sampler = this->getSamplerHandle(((VariableReference&) *c.fArguments[0]).fVariable); |
| 399 | fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerSwizzle(" + sampler + |
| 400 | ").c_str()"); |
| 401 | } |
| 402 | } |
| 403 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 404 | void CPPCodeGenerator::writeFunction(const FunctionDefinition& f) { |
| 405 | if (f.fDeclaration.fName == "main") { |
| 406 | fFunctionHeader = ""; |
| 407 | OutputStream* oldOut = fOut; |
| 408 | StringStream buffer; |
| 409 | fOut = &buffer; |
Ethan Nicholas | f1b1464 | 2018-08-09 16:18:07 -0400 | [diff] [blame] | 410 | fInMain = true; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 411 | for (const auto& s : ((Block&) *f.fBody).fStatements) { |
| 412 | this->writeStatement(*s); |
| 413 | this->writeLine(); |
| 414 | } |
Ethan Nicholas | f1b1464 | 2018-08-09 16:18:07 -0400 | [diff] [blame] | 415 | fInMain = false; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 416 | |
| 417 | fOut = oldOut; |
| 418 | this->write(fFunctionHeader); |
| 419 | this->write(buffer.str()); |
| 420 | } else { |
| 421 | INHERITED::writeFunction(f); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | void CPPCodeGenerator::writeSetting(const Setting& s) { |
| 426 | static constexpr const char* kPrefix = "sk_Args."; |
| 427 | if (!strncmp(s.fName.c_str(), kPrefix, strlen(kPrefix))) { |
| 428 | const char* name = s.fName.c_str() + strlen(kPrefix); |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 429 | this->writeRuntimeValue(s.fType, Layout(), HCodeGenerator::FieldName(name).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 430 | } else { |
| 431 | this->write(s.fName.c_str()); |
| 432 | } |
| 433 | } |
| 434 | |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 435 | bool CPPCodeGenerator::writeSection(const char* name, const char* prefix) { |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 436 | const Section* s = fSectionAndParameterHelper.getSection(name); |
| 437 | if (s) { |
| 438 | this->writef("%s%s", prefix, s->fText.c_str()); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 439 | return true; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 440 | } |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 441 | return false; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | void CPPCodeGenerator::writeProgramElement(const ProgramElement& p) { |
| 445 | if (p.fKind == ProgramElement::kSection_Kind) { |
| 446 | return; |
| 447 | } |
| 448 | if (p.fKind == ProgramElement::kVar_Kind) { |
| 449 | const VarDeclarations& decls = (const VarDeclarations&) p; |
| 450 | if (!decls.fVars.size()) { |
| 451 | return; |
| 452 | } |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 453 | const Variable& var = *((VarDeclaration&) *decls.fVars[0]).fVar; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 454 | if (var.fModifiers.fFlags & (Modifiers::kIn_Flag | Modifiers::kUniform_Flag) || |
| 455 | -1 != var.fModifiers.fLayout.fBuiltin) { |
| 456 | return; |
| 457 | } |
| 458 | } |
| 459 | INHERITED::writeProgramElement(p); |
| 460 | } |
| 461 | |
| 462 | void CPPCodeGenerator::addUniform(const Variable& var) { |
| 463 | if (!needs_uniform_var(var)) { |
| 464 | return; |
| 465 | } |
| 466 | const char* precision; |
| 467 | if (var.fModifiers.fFlags & Modifiers::kHighp_Flag) { |
| 468 | precision = "kHigh_GrSLPrecision"; |
| 469 | } else if (var.fModifiers.fFlags & Modifiers::kMediump_Flag) { |
| 470 | precision = "kMedium_GrSLPrecision"; |
| 471 | } else if (var.fModifiers.fFlags & Modifiers::kLowp_Flag) { |
| 472 | precision = "kLow_GrSLPrecision"; |
| 473 | } else { |
| 474 | precision = "kDefault_GrSLPrecision"; |
| 475 | } |
| 476 | const char* type; |
| 477 | if (var.fType == *fContext.fFloat_Type) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 478 | type = "kFloat_GrSLType"; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 479 | } else if (var.fType == *fContext.fHalf_Type) { |
| 480 | type = "kHalf_GrSLType"; |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 481 | } else if (var.fType == *fContext.fFloat2_Type) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 482 | type = "kFloat2_GrSLType"; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 483 | } else if (var.fType == *fContext.fHalf2_Type) { |
| 484 | type = "kHalf2_GrSLType"; |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 485 | } else if (var.fType == *fContext.fFloat4_Type) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 486 | type = "kFloat4_GrSLType"; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 487 | } else if (var.fType == *fContext.fHalf4_Type) { |
| 488 | type = "kHalf4_GrSLType"; |
Brian Osman | 1cb4171 | 2017-10-19 12:54:52 -0400 | [diff] [blame] | 489 | } else if (var.fType == *fContext.fFloat4x4_Type) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 490 | type = "kFloat4x4_GrSLType"; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 491 | } else if (var.fType == *fContext.fHalf4x4_Type) { |
| 492 | type = "kHalf4x4_GrSLType"; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 493 | } else { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 494 | ABORT("unsupported uniform type: %s %s;\n", String(var.fType.fName).c_str(), |
| 495 | String(var.fName).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 496 | } |
| 497 | if (var.fModifiers.fLayout.fWhen.size()) { |
| 498 | this->writef(" if (%s) {\n ", var.fModifiers.fLayout.fWhen.c_str()); |
| 499 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 500 | String name(var.fName); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 501 | this->writef(" %sVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, %s, " |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 502 | "%s, \"%s\");\n", HCodeGenerator::FieldName(name.c_str()).c_str(), type, precision, |
| 503 | name.c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 504 | if (var.fModifiers.fLayout.fWhen.size()) { |
| 505 | this->write(" }\n"); |
| 506 | } |
| 507 | } |
| 508 | |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 509 | void CPPCodeGenerator::writeInputVars() { |
| 510 | } |
| 511 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 512 | void CPPCodeGenerator::writePrivateVars() { |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 513 | for (const auto& p : fProgram) { |
| 514 | if (ProgramElement::kVar_Kind == p.fKind) { |
| 515 | const VarDeclarations& decls = (const VarDeclarations&) p; |
| 516 | for (const auto& raw : decls.fVars) { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 517 | VarDeclaration& decl = (VarDeclaration&) *raw; |
| 518 | if (is_private(*decl.fVar)) { |
| 519 | if (decl.fVar->fType == *fContext.fFragmentProcessor_Type) { |
| 520 | fErrors.error(decl.fOffset, |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 521 | "fragmentProcessor variables must be declared 'in'"); |
| 522 | return; |
| 523 | } |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 524 | this->writef("%s %s = %s;\n", |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 525 | HCodeGenerator::FieldType(fContext, decl.fVar->fType, |
| 526 | decl.fVar->fModifiers.fLayout).c_str(), |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 527 | String(decl.fVar->fName).c_str(), |
| 528 | default_value(*decl.fVar).c_str()); |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 529 | } else if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) { |
| 530 | // An auto-tracked uniform in variable, so add a field to hold onto the prior |
| 531 | // state. Note that tracked variables must be uniform in's and that is validated |
| 532 | // before writePrivateVars() is called. |
| 533 | const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *decl.fVar); |
| 534 | SkASSERT(mapper && mapper->supportsTracking()); |
| 535 | |
| 536 | String name = HCodeGenerator::FieldName(String(decl.fVar->fName).c_str()); |
| 537 | // The member statement is different if the mapper reports a default value |
| 538 | if (mapper->defaultValue().size() > 0) { |
| 539 | this->writef("%s %sPrev = %s;\n", |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 540 | Layout::CTypeToStr(mapper->ctype()), name.c_str(), |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 541 | mapper->defaultValue().c_str()); |
| 542 | } else { |
| 543 | this->writef("%s %sPrev;\n", |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 544 | Layout::CTypeToStr(mapper->ctype()), name.c_str()); |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 545 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | void CPPCodeGenerator::writePrivateVarValues() { |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 553 | for (const auto& p : fProgram) { |
| 554 | if (ProgramElement::kVar_Kind == p.fKind) { |
| 555 | const VarDeclarations& decls = (const VarDeclarations&) p; |
| 556 | for (const auto& raw : decls.fVars) { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 557 | VarDeclaration& decl = (VarDeclaration&) *raw; |
| 558 | if (is_private(*decl.fVar) && decl.fValue) { |
| 559 | this->writef("%s = ", String(decl.fVar->fName).c_str()); |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 560 | fCPPMode = true; |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 561 | this->writeExpression(*decl.fValue, kAssignment_Precedence); |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 562 | fCPPMode = false; |
| 563 | this->write(";\n"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 570 | static bool is_accessible(const Variable& var) { |
| 571 | return Type::kSampler_Kind != var.fType.kind() && |
| 572 | Type::kOther_Kind != var.fType.kind(); |
| 573 | } |
| 574 | |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame^] | 575 | void CPPCodeGenerator::newExtraEmitCodeBlock() { |
| 576 | // This should only be called when emitting SKSL for emitCode(), which can be detected if the |
| 577 | // cpp buffer is not null, and the cpp buffer is not the current output. |
| 578 | SkASSERT(fCPPBuffer && fCPPBuffer != fOut); |
| 579 | |
| 580 | // Start a new block as an empty string |
| 581 | fExtraEmitCodeBlocks.push_back(""); |
| 582 | // Mark its location in the output buffer, uses ${\d} for the token since ${} will not occur in |
| 583 | // valid sksl and makes detection trivial. |
| 584 | this->writef("${%zu}", fExtraEmitCodeBlocks.size() - 1); |
| 585 | } |
| 586 | |
| 587 | void CPPCodeGenerator::addExtraEmitCodeLine(const String& toAppend) { |
| 588 | SkASSERT(fExtraEmitCodeBlocks.size() > 0); |
| 589 | String& currentBlock = fExtraEmitCodeBlocks[fExtraEmitCodeBlocks.size() - 1]; |
| 590 | // Automatically add indentation and newline |
| 591 | currentBlock += " " + toAppend + "\n"; |
| 592 | } |
| 593 | |
| 594 | void CPPCodeGenerator::flushEmittedCode() { |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 595 | if (fCPPBuffer == nullptr) { |
| 596 | // Not actually within writeEmitCode() so nothing to flush |
| 597 | return; |
| 598 | } |
| 599 | |
| 600 | StringStream* skslBuffer = static_cast<StringStream*>(fOut); |
| 601 | |
| 602 | String sksl = skslBuffer->str(); |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame^] | 603 | // Empty the accumulation buffer since its current contents are consumed. |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 604 | skslBuffer->reset(); |
| 605 | |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame^] | 606 | // Switch to the cpp buffer |
Michael Ludwig | d044019 | 2018-09-07 14:24:52 +0000 | [diff] [blame] | 607 | fOut = fCPPBuffer; |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame^] | 608 | |
| 609 | // Iterate through the sksl, keeping track of where the last statement ended (e.g. the latest |
| 610 | // encountered ';', '{', or '}'). If an extra emit code block token is encountered then the |
| 611 | // code from 0 to last statement end is sent to writeCodeAppend, the extra code block is |
| 612 | // appended to the cpp buffer, and then the sksl string is trimmed to start where the last |
| 613 | // statement left off (minus the encountered token). |
| 614 | size_t i = 0; |
| 615 | int flushPoint = -1; |
| 616 | int tokenStart = -1; |
| 617 | while (i < sksl.size()) { |
| 618 | if (tokenStart >= 0) { |
| 619 | // Looking for the end of the token |
| 620 | if (sksl[i] == '}') { |
| 621 | // Must append the sksl from 0 to flushPoint (inclusive) then the extra code |
| 622 | // accumulated in the block with index parsed from chars [tokenStart+2, i-1] |
| 623 | String toFlush = String(sksl.c_str(), flushPoint + 1); |
| 624 | // writeCodeAppend automatically removes the format args that it consumed, so |
| 625 | // fFormatArgs will be in a valid state for any future sksl |
| 626 | this->writeCodeAppend(toFlush); |
| 627 | |
| 628 | int codeBlock = stoi(String(sksl.c_str() + tokenStart + 2, i - tokenStart - 2)); |
| 629 | SkASSERT(codeBlock < (int) fExtraEmitCodeBlocks.size()); |
| 630 | if (fExtraEmitCodeBlocks[codeBlock].size() > 0) { |
| 631 | this->write(fExtraEmitCodeBlocks[codeBlock].c_str()); |
| 632 | } |
| 633 | |
| 634 | // Now reset the sksl buffer to start after the flush point, but remove the token. |
| 635 | String compacted = String(sksl.c_str() + flushPoint + 1, |
| 636 | tokenStart - flushPoint - 1); |
| 637 | if (i < sksl.size() - 1) { |
| 638 | compacted += String(sksl.c_str() + i + 1, sksl.size() - i - 1); |
| 639 | } |
| 640 | sksl = compacted; |
| 641 | |
| 642 | // And reset iteration |
| 643 | i = -1; |
| 644 | flushPoint = -1; |
| 645 | tokenStart = -1; |
| 646 | } |
| 647 | } else { |
| 648 | // Looking for the start of extra emit block tokens, and tracking when statements end |
| 649 | if (sksl[i] == ';' || sksl[i] == '{' || sksl[i] == '}') { |
| 650 | flushPoint = i; |
| 651 | } else if (i < sksl.size() - 1 && sksl[i] == '$' && sksl[i + 1] == '{') { |
| 652 | // found an extra emit code block token |
| 653 | tokenStart = i++; |
| 654 | } |
| 655 | } |
| 656 | i++; |
Michael Ludwig | d044019 | 2018-09-07 14:24:52 +0000 | [diff] [blame] | 657 | } |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame^] | 658 | |
| 659 | // Once we've gone through the sksl string to this point, there are no remaining extra emit |
| 660 | // code blocks to interleave, so append the remainder as usual. |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 661 | this->writeCodeAppend(sksl); |
| 662 | |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame^] | 663 | // After appending, switch back to the emptied sksl buffer and reset the extra code blocks |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 664 | fOut = skslBuffer; |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame^] | 665 | fExtraEmitCodeBlocks.clear(); |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 666 | } |
| 667 | |
Ethan Nicholas | 5b6e627 | 2017-10-13 13:11:06 -0400 | [diff] [blame] | 668 | void CPPCodeGenerator::writeCodeAppend(const String& code) { |
| 669 | // codeAppendf can only handle appending 1024 bytes at a time, so we need to break the string |
| 670 | // into chunks. Unfortunately we can't tell exactly how long the string is going to end up, |
| 671 | // because printf escape sequences get replaced by strings of unknown length, but keeping the |
| 672 | // format string below 512 bytes is probably safe. |
| 673 | static constexpr size_t maxChunkSize = 512; |
| 674 | size_t start = 0; |
| 675 | size_t index = 0; |
| 676 | size_t argStart = 0; |
| 677 | size_t argCount; |
| 678 | while (index < code.size()) { |
| 679 | argCount = 0; |
| 680 | this->write(" fragBuilder->codeAppendf(\""); |
| 681 | while (index < code.size() && index < start + maxChunkSize) { |
| 682 | if ('%' == code[index]) { |
| 683 | if (index == start + maxChunkSize - 1 || index == code.size() - 1) { |
| 684 | break; |
| 685 | } |
| 686 | if (code[index + 1] != '%') { |
| 687 | ++argCount; |
| 688 | } |
Ethan Nicholas | ef0c9fd | 2017-10-30 10:04:14 -0400 | [diff] [blame] | 689 | } else if ('\\' == code[index] && index == start + maxChunkSize - 1) { |
| 690 | // avoid splitting an escape sequence that happens to fall across a chunk boundary |
| 691 | break; |
Ethan Nicholas | 5b6e627 | 2017-10-13 13:11:06 -0400 | [diff] [blame] | 692 | } |
| 693 | ++index; |
| 694 | } |
| 695 | fOut->write(code.c_str() + start, index - start); |
| 696 | this->write("\""); |
| 697 | for (size_t i = argStart; i < argStart + argCount; ++i) { |
| 698 | this->writef(", %s", fFormatArgs[i].c_str()); |
| 699 | } |
| 700 | this->write(");\n"); |
| 701 | argStart += argCount; |
| 702 | start = index; |
| 703 | } |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 704 | |
| 705 | // argStart is equal to the number of fFormatArgs that were consumed |
| 706 | // so they should be removed from the list |
| 707 | if (argStart > 0) { |
| 708 | fFormatArgs.erase(fFormatArgs.begin(), fFormatArgs.begin() + argStart); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | String CPPCodeGenerator::convertSKSLExpressionToCPP(const Expression& e, |
| 713 | const String& cppVar) { |
| 714 | // To do this conversion, we temporarily switch the sksl output stream |
| 715 | // to an empty stringstream and reset the format args to empty. |
| 716 | OutputStream* oldSKSL = fOut; |
| 717 | StringStream exprBuffer; |
| 718 | fOut = &exprBuffer; |
| 719 | |
| 720 | std::vector<String> oldArgs(fFormatArgs); |
| 721 | fFormatArgs.clear(); |
| 722 | |
| 723 | // Convert the argument expression into a format string and args |
| 724 | this->writeExpression(e, Precedence::kTopLevel_Precedence); |
| 725 | std::vector<String> newArgs(fFormatArgs); |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame^] | 726 | String expr = exprBuffer.str(); |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 727 | |
| 728 | // After generating, restore the original output stream and format args |
| 729 | fFormatArgs = oldArgs; |
| 730 | fOut = oldSKSL; |
| 731 | |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame^] | 732 | // The sksl written to exprBuffer is not processed by flushEmittedCode(), so any extra emit code |
| 733 | // block tokens won't get handled. So we need to strip them from the expression and stick them |
| 734 | // to the end of the original sksl stream. |
| 735 | String exprFormat = ""; |
| 736 | int tokenStart = -1; |
| 737 | for (size_t i = 0; i < expr.size(); i++) { |
| 738 | if (tokenStart >= 0) { |
| 739 | if (expr[i] == '}') { |
| 740 | // End of the token, so append the token to fOut |
| 741 | fOut->write(expr.c_str() + tokenStart, i - tokenStart + 1); |
| 742 | tokenStart = -1; |
| 743 | } |
| 744 | } else { |
| 745 | if (i < expr.size() - 1 && expr[i] == '$' && expr[i + 1] == '{') { |
| 746 | tokenStart = i++; |
| 747 | } else { |
| 748 | exprFormat += expr[i]; |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 753 | // Now build the final C++ code snippet from the format string and args |
| 754 | String cppExpr; |
| 755 | if (newArgs.size() == 0) { |
| 756 | // This was a static expression, so we can simplify the input |
| 757 | // color declaration in the emitted code to just a static string |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame^] | 758 | cppExpr = "SkString " + cppVar + "(\"" + exprFormat + "\");"; |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 759 | } else { |
| 760 | // String formatting must occur dynamically, so have the C++ declaration |
| 761 | // use SkStringPrintf with the format args that were accumulated |
| 762 | // when the expression was written. |
| 763 | cppExpr = "SkString " + cppVar + " = SkStringPrintf(\"" + exprFormat + "\""; |
| 764 | for (size_t i = 0; i < newArgs.size(); i++) { |
| 765 | cppExpr += ", " + newArgs[i]; |
| 766 | } |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame^] | 767 | cppExpr += ");"; |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 768 | } |
| 769 | return cppExpr; |
Ethan Nicholas | 5b6e627 | 2017-10-13 13:11:06 -0400 | [diff] [blame] | 770 | } |
| 771 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 772 | bool CPPCodeGenerator::writeEmitCode(std::vector<const Variable*>& uniforms) { |
| 773 | this->write(" void emitCode(EmitArgs& args) override {\n" |
| 774 | " GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;\n"); |
| 775 | this->writef(" const %s& _outer = args.fFp.cast<%s>();\n" |
| 776 | " (void) _outer;\n", |
| 777 | fFullName.c_str(), fFullName.c_str()); |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 778 | for (const auto& p : fProgram) { |
| 779 | if (ProgramElement::kVar_Kind == p.fKind) { |
| 780 | const VarDeclarations& decls = (const VarDeclarations&) p; |
| 781 | for (const auto& raw : decls.fVars) { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 782 | VarDeclaration& decl = (VarDeclaration&) *raw; |
| 783 | String nameString(decl.fVar->fName); |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 784 | const char* name = nameString.c_str(); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 785 | if (SectionAndParameterHelper::IsParameter(*decl.fVar) && |
| 786 | is_accessible(*decl.fVar)) { |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 787 | this->writef(" auto %s = _outer.%s();\n" |
| 788 | " (void) %s;\n", |
| 789 | name, name, name); |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 794 | this->writePrivateVarValues(); |
| 795 | for (const auto u : uniforms) { |
| 796 | this->addUniform(*u); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 797 | } |
| 798 | this->writeSection(EMIT_CODE_SECTION); |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 799 | |
| 800 | // Save original buffer as the CPP buffer for flushEmittedCode() |
| 801 | fCPPBuffer = fOut; |
| 802 | StringStream skslBuffer; |
| 803 | fOut = &skslBuffer; |
| 804 | |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame^] | 805 | this->newExtraEmitCodeBlock(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 806 | bool result = INHERITED::generateCode(); |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame^] | 807 | this->flushEmittedCode(); |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 808 | |
| 809 | // Then restore the original CPP buffer and close the function |
| 810 | fOut = fCPPBuffer; |
| 811 | fCPPBuffer = nullptr; |
Ethan Nicholas | 5b6e627 | 2017-10-13 13:11:06 -0400 | [diff] [blame] | 812 | this->write(" }\n"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 813 | return result; |
| 814 | } |
| 815 | |
| 816 | void CPPCodeGenerator::writeSetData(std::vector<const Variable*>& uniforms) { |
| 817 | const char* fullName = fFullName.c_str(); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 818 | const Section* section = fSectionAndParameterHelper.getSection(SET_DATA_SECTION); |
| 819 | const char* pdman = section ? section->fArgument.c_str() : "pdman"; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 820 | this->writef(" void onSetData(const GrGLSLProgramDataManager& %s, " |
| 821 | "const GrFragmentProcessor& _proc) override {\n", |
| 822 | pdman); |
| 823 | bool wroteProcessor = false; |
| 824 | for (const auto u : uniforms) { |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 825 | if (is_uniform_in(*u)) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 826 | if (!wroteProcessor) { |
| 827 | this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName, fullName); |
| 828 | wroteProcessor = true; |
| 829 | this->writef(" {\n"); |
| 830 | } |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 831 | |
| 832 | const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *u); |
| 833 | SkASSERT(mapper); |
| 834 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 835 | String nameString(u->fName); |
| 836 | const char* name = nameString.c_str(); |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 837 | |
| 838 | // Switches for setData behavior in the generated code |
| 839 | bool conditionalUniform = u->fModifiers.fLayout.fWhen != ""; |
| 840 | bool isTracked = u->fModifiers.fLayout.fFlags & Layout::kTracked_Flag; |
| 841 | bool needsValueDeclaration = isTracked || !mapper->canInlineUniformValue(); |
| 842 | |
| 843 | String uniformName = HCodeGenerator::FieldName(name) + "Var"; |
| 844 | |
| 845 | String indent = " "; // 8 by default, 12 when nested for conditional uniforms |
| 846 | if (conditionalUniform) { |
| 847 | // Add a pre-check to make sure the uniform was emitted |
| 848 | // before trying to send any data to the GPU |
| 849 | this->writef(" if (%s.isValid()) {\n", uniformName.c_str()); |
| 850 | indent += " "; |
| 851 | } |
| 852 | |
| 853 | String valueVar = ""; |
| 854 | if (needsValueDeclaration) { |
| 855 | valueVar.appendf("%sValue", name); |
| 856 | // Use AccessType since that will match the return type of _outer's public API. |
| 857 | String valueType = HCodeGenerator::AccessType(fContext, u->fType, |
| 858 | u->fModifiers.fLayout); |
| 859 | this->writef("%s%s %s = _outer.%s();\n", |
| 860 | indent.c_str(), valueType.c_str(), valueVar.c_str(), name); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 861 | } else { |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 862 | // Not tracked and the mapper only needs to use the value once |
| 863 | // so send it a safe expression instead of the variable name |
| 864 | valueVar.appendf("(_outer.%s())", name); |
| 865 | } |
| 866 | |
| 867 | if (isTracked) { |
| 868 | SkASSERT(mapper->supportsTracking()); |
| 869 | |
| 870 | String prevVar = HCodeGenerator::FieldName(name) + "Prev"; |
| 871 | this->writef("%sif (%s) {\n" |
| 872 | "%s %s;\n" |
| 873 | "%s %s;\n" |
| 874 | "%s}\n", indent.c_str(), |
| 875 | mapper->dirtyExpression(valueVar, prevVar).c_str(), indent.c_str(), |
| 876 | mapper->saveState(valueVar, prevVar).c_str(), indent.c_str(), |
| 877 | mapper->setUniform(pdman, uniformName, valueVar).c_str(), indent.c_str()); |
| 878 | } else { |
| 879 | this->writef("%s%s;\n", indent.c_str(), |
| 880 | mapper->setUniform(pdman, uniformName, valueVar).c_str()); |
| 881 | } |
| 882 | |
| 883 | if (conditionalUniform) { |
| 884 | // Close the earlier precheck block |
| 885 | this->writef(" }\n"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | } |
| 889 | if (wroteProcessor) { |
| 890 | this->writef(" }\n"); |
| 891 | } |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 892 | if (section) { |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 893 | int samplerIndex = 0; |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 894 | for (const auto& p : fProgram) { |
| 895 | if (ProgramElement::kVar_Kind == p.fKind) { |
| 896 | const VarDeclarations& decls = (const VarDeclarations&) p; |
| 897 | for (const auto& raw : decls.fVars) { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 898 | VarDeclaration& decl = (VarDeclaration&) *raw; |
| 899 | String nameString(decl.fVar->fName); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 900 | const char* name = nameString.c_str(); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 901 | if (decl.fVar->fType.kind() == Type::kSampler_Kind) { |
| 902 | this->writef(" GrSurfaceProxy& %sProxy = " |
| 903 | "*_outer.textureSampler(%d).proxy();\n", |
| 904 | name, samplerIndex); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 905 | this->writef(" GrTexture& %s = *%sProxy.peekTexture();\n", |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 906 | name, name); |
| 907 | this->writef(" (void) %s;\n", name); |
| 908 | ++samplerIndex; |
| 909 | } else if (needs_uniform_var(*decl.fVar)) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 910 | this->writef(" UniformHandle& %s = %sVar;\n" |
| 911 | " (void) %s;\n", |
| 912 | name, HCodeGenerator::FieldName(name).c_str(), name); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 913 | } else if (SectionAndParameterHelper::IsParameter(*decl.fVar) && |
| 914 | decl.fVar->fType != *fContext.fFragmentProcessor_Type) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 915 | if (!wroteProcessor) { |
| 916 | this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName, |
| 917 | fullName); |
| 918 | wroteProcessor = true; |
| 919 | } |
| 920 | this->writef(" auto %s = _outer.%s();\n" |
| 921 | " (void) %s;\n", |
| 922 | name, name, name); |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | } |
| 927 | this->writeSection(SET_DATA_SECTION); |
| 928 | } |
| 929 | this->write(" }\n"); |
| 930 | } |
| 931 | |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 932 | void CPPCodeGenerator::writeOnTextureSampler() { |
| 933 | bool foundSampler = false; |
| 934 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
| 935 | if (param->fType.kind() == Type::kSampler_Kind) { |
| 936 | if (!foundSampler) { |
| 937 | this->writef( |
| 938 | "const GrFragmentProcessor::TextureSampler& %s::onTextureSampler(int " |
| 939 | "index) const {\n", |
| 940 | fFullName.c_str()); |
| 941 | this->writef(" return IthTextureSampler(index, %s", |
| 942 | HCodeGenerator::FieldName(String(param->fName).c_str()).c_str()); |
| 943 | foundSampler = true; |
| 944 | } else { |
| 945 | this->writef(", %s", |
| 946 | HCodeGenerator::FieldName(String(param->fName).c_str()).c_str()); |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | if (foundSampler) { |
| 951 | this->write(");\n}\n"); |
| 952 | } |
| 953 | } |
| 954 | |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 955 | void CPPCodeGenerator::writeClone() { |
| 956 | if (!this->writeSection(CLONE_SECTION)) { |
| 957 | if (fSectionAndParameterHelper.getSection(FIELDS_SECTION)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 958 | fErrors.error(0, "fragment processors with custom @fields must also have a custom" |
| 959 | "@clone"); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 960 | } |
| 961 | this->writef("%s::%s(const %s& src)\n" |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 962 | ": INHERITED(k%s_ClassID, src.optimizationFlags())", fFullName.c_str(), |
| 963 | fFullName.c_str(), fFullName.c_str(), fFullName.c_str()); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 964 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 965 | if (param->fType == *fContext.fFragmentProcessor_Type) { |
| 966 | continue; |
| 967 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 968 | String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str()); |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 969 | this->writef("\n, %s(src.%s)", |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 970 | fieldName.c_str(), |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 971 | fieldName.c_str()); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 972 | } |
Ethan Nicholas | 929a681 | 2018-08-06 14:56:59 -0400 | [diff] [blame] | 973 | const auto transforms = fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION); |
| 974 | for (size_t i = 0; i < transforms.size(); ++i) { |
| 975 | const Section& s = *transforms[i]; |
| 976 | String fieldName = HCodeGenerator::CoordTransformName(s.fArgument, i); |
| 977 | this->writef("\n, %s(src.%s)", fieldName.c_str(), fieldName.c_str()); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 978 | } |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 979 | this->writef(" {\n"); |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 980 | int childCount = 0; |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 981 | int samplerCount = 0; |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 982 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
| 983 | if (param->fType.kind() == Type::kSampler_Kind) { |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 984 | ++samplerCount; |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 985 | } else if (param->fType == *fContext.fFragmentProcessor_Type) { |
| 986 | this->writef(" this->registerChildProcessor(src.childProcessor(%d).clone());" |
| 987 | "\n", childCount++); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 988 | } |
| 989 | } |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 990 | if (samplerCount) { |
| 991 | this->writef(" this->setTextureSamplerCnt(%d);", samplerCount); |
| 992 | } |
Ethan Nicholas | 929a681 | 2018-08-06 14:56:59 -0400 | [diff] [blame] | 993 | for (size_t i = 0; i < transforms.size(); ++i) { |
| 994 | const Section& s = *transforms[i]; |
| 995 | String fieldName = HCodeGenerator::CoordTransformName(s.fArgument, i); |
| 996 | this->writef(" this->addCoordTransform(&%s);\n", fieldName.c_str()); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 997 | } |
| 998 | this->write("}\n"); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 999 | this->writef("std::unique_ptr<GrFragmentProcessor> %s::clone() const {\n", |
| 1000 | fFullName.c_str()); |
| 1001 | this->writef(" return std::unique_ptr<GrFragmentProcessor>(new %s(*this));\n", |
| 1002 | fFullName.c_str()); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 1003 | this->write("}\n"); |
| 1004 | } |
| 1005 | } |
| 1006 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1007 | void CPPCodeGenerator::writeTest() { |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 1008 | const Section* test = fSectionAndParameterHelper.getSection(TEST_CODE_SECTION); |
| 1009 | if (test) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 1010 | this->writef( |
| 1011 | "GR_DEFINE_FRAGMENT_PROCESSOR_TEST(%s);\n" |
| 1012 | "#if GR_TEST_UTILS\n" |
| 1013 | "std::unique_ptr<GrFragmentProcessor> %s::TestCreate(GrProcessorTestData* %s) {\n", |
| 1014 | fFullName.c_str(), |
| 1015 | fFullName.c_str(), |
| 1016 | test->fArgument.c_str()); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 1017 | this->writeSection(TEST_CODE_SECTION); |
| 1018 | this->write("}\n" |
| 1019 | "#endif\n"); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1020 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | void CPPCodeGenerator::writeGetKey() { |
| 1024 | this->writef("void %s::onGetGLSLProcessorKey(const GrShaderCaps& caps, " |
| 1025 | "GrProcessorKeyBuilder* b) const {\n", |
| 1026 | fFullName.c_str()); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 1027 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1028 | String nameString(param->fName); |
| 1029 | const char* name = nameString.c_str(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1030 | if (param->fModifiers.fLayout.fKey != Layout::kNo_Key && |
| 1031 | (param->fModifiers.fFlags & Modifiers::kUniform_Flag)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1032 | fErrors.error(param->fOffset, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1033 | "layout(key) may not be specified on uniforms"); |
| 1034 | } |
| 1035 | switch (param->fModifiers.fLayout.fKey) { |
| 1036 | case Layout::kKey_Key: |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1037 | if (param->fType == *fContext.fFloat4x4_Type) { |
| 1038 | ABORT("no automatic key handling for float4x4\n"); |
| 1039 | } else if (param->fType == *fContext.fFloat2_Type) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1040 | this->writef(" b->add32(%s.fX);\n", |
| 1041 | HCodeGenerator::FieldName(name).c_str()); |
| 1042 | this->writef(" b->add32(%s.fY);\n", |
| 1043 | HCodeGenerator::FieldName(name).c_str()); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1044 | } else if (param->fType == *fContext.fFloat4_Type) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1045 | this->writef(" b->add32(%s.x());\n", |
| 1046 | HCodeGenerator::FieldName(name).c_str()); |
| 1047 | this->writef(" b->add32(%s.y());\n", |
| 1048 | HCodeGenerator::FieldName(name).c_str()); |
| 1049 | this->writef(" b->add32(%s.width());\n", |
| 1050 | HCodeGenerator::FieldName(name).c_str()); |
| 1051 | this->writef(" b->add32(%s.height());\n", |
| 1052 | HCodeGenerator::FieldName(name).c_str()); |
| 1053 | } else { |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1054 | this->writef(" b->add32((int32_t) %s);\n", |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1055 | HCodeGenerator::FieldName(name).c_str()); |
| 1056 | } |
| 1057 | break; |
| 1058 | case Layout::kIdentity_Key: |
| 1059 | if (param->fType.kind() != Type::kMatrix_Kind) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1060 | fErrors.error(param->fOffset, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1061 | "layout(key=identity) requires matrix type"); |
| 1062 | } |
| 1063 | this->writef(" b->add32(%s.isIdentity() ? 1 : 0);\n", |
| 1064 | HCodeGenerator::FieldName(name).c_str()); |
| 1065 | break; |
| 1066 | case Layout::kNo_Key: |
| 1067 | break; |
| 1068 | } |
| 1069 | } |
| 1070 | this->write("}\n"); |
| 1071 | } |
| 1072 | |
| 1073 | bool CPPCodeGenerator::generateCode() { |
| 1074 | std::vector<const Variable*> uniforms; |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1075 | for (const auto& p : fProgram) { |
| 1076 | if (ProgramElement::kVar_Kind == p.fKind) { |
| 1077 | const VarDeclarations& decls = (const VarDeclarations&) p; |
| 1078 | for (const auto& raw : decls.fVars) { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1079 | VarDeclaration& decl = (VarDeclaration&) *raw; |
| 1080 | if ((decl.fVar->fModifiers.fFlags & Modifiers::kUniform_Flag) && |
| 1081 | decl.fVar->fType.kind() != Type::kSampler_Kind) { |
| 1082 | uniforms.push_back(decl.fVar); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1083 | } |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 1084 | |
| 1085 | if (is_uniform_in(*decl.fVar)) { |
| 1086 | // Validate the "uniform in" declarations to make sure they are fully supported, |
| 1087 | // instead of generating surprising C++ |
| 1088 | const UniformCTypeMapper* mapper = |
| 1089 | UniformCTypeMapper::Get(fContext, *decl.fVar); |
| 1090 | if (mapper == nullptr) { |
| 1091 | fErrors.error(decl.fOffset, String(decl.fVar->fName) |
| 1092 | + "'s type is not supported for use as a 'uniform in'"); |
| 1093 | return false; |
| 1094 | } |
| 1095 | if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) { |
| 1096 | if (!mapper->supportsTracking()) { |
| 1097 | fErrors.error(decl.fOffset, String(decl.fVar->fName) |
| 1098 | + "'s type does not support state tracking"); |
| 1099 | return false; |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | } else { |
| 1104 | // If it's not a uniform_in, it's an error to be tracked |
| 1105 | if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) { |
| 1106 | fErrors.error(decl.fOffset, "Non-'in uniforms' cannot be tracked"); |
| 1107 | return false; |
| 1108 | } |
| 1109 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1110 | } |
| 1111 | } |
| 1112 | } |
| 1113 | const char* baseName = fName.c_str(); |
| 1114 | const char* fullName = fFullName.c_str(); |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 1115 | this->writef("%s\n", HCodeGenerator::GetHeader(fProgram, fErrors).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1116 | this->writef(kFragmentProcessorHeader, fullName); |
Greg Daniel | 3e8c345 | 2018-04-06 10:37:55 -0400 | [diff] [blame] | 1117 | this->writef("#include \"%s.h\"\n", fullName); |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 1118 | this->writeSection(CPP_SECTION); |
Brian Osman | 1cb4171 | 2017-10-19 12:54:52 -0400 | [diff] [blame] | 1119 | this->writef("#include \"glsl/GrGLSLFragmentProcessor.h\"\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1120 | "#include \"glsl/GrGLSLFragmentShaderBuilder.h\"\n" |
| 1121 | "#include \"glsl/GrGLSLProgramBuilder.h\"\n" |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 1122 | "#include \"GrTexture.h\"\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1123 | "#include \"SkSLCPP.h\"\n" |
| 1124 | "#include \"SkSLUtil.h\"\n" |
| 1125 | "class GrGLSL%s : public GrGLSLFragmentProcessor {\n" |
| 1126 | "public:\n" |
| 1127 | " GrGLSL%s() {}\n", |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 1128 | baseName, baseName); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1129 | bool result = this->writeEmitCode(uniforms); |
| 1130 | this->write("private:\n"); |
| 1131 | this->writeSetData(uniforms); |
| 1132 | this->writePrivateVars(); |
| 1133 | for (const auto& u : uniforms) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1134 | if (needs_uniform_var(*u) && !(u->fModifiers.fFlags & Modifiers::kIn_Flag)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1135 | this->writef(" UniformHandle %sVar;\n", |
| 1136 | HCodeGenerator::FieldName(String(u->fName).c_str()).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1137 | } |
| 1138 | } |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 1139 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1140 | if (needs_uniform_var(*param)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1141 | this->writef(" UniformHandle %sVar;\n", |
| 1142 | HCodeGenerator::FieldName(String(param->fName).c_str()).c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1143 | } |
| 1144 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1145 | this->writef("};\n" |
| 1146 | "GrGLSLFragmentProcessor* %s::onCreateGLSLInstance() const {\n" |
| 1147 | " return new GrGLSL%s();\n" |
| 1148 | "}\n", |
| 1149 | fullName, baseName); |
| 1150 | this->writeGetKey(); |
| 1151 | this->writef("bool %s::onIsEqual(const GrFragmentProcessor& other) const {\n" |
| 1152 | " const %s& that = other.cast<%s>();\n" |
| 1153 | " (void) that;\n", |
| 1154 | fullName, fullName, fullName); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 1155 | for (const auto& param : fSectionAndParameterHelper.getParameters()) { |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 1156 | if (param->fType == *fContext.fFragmentProcessor_Type) { |
| 1157 | continue; |
| 1158 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1159 | String nameString(param->fName); |
| 1160 | const char* name = nameString.c_str(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1161 | this->writef(" if (%s != that.%s) return false;\n", |
| 1162 | HCodeGenerator::FieldName(name).c_str(), |
| 1163 | HCodeGenerator::FieldName(name).c_str()); |
| 1164 | } |
| 1165 | this->write(" return true;\n" |
| 1166 | "}\n"); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 1167 | this->writeClone(); |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 1168 | this->writeOnTextureSampler(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1169 | this->writeTest(); |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 1170 | this->writeSection(CPP_END_SECTION); |
Greg Daniel | 3e8c345 | 2018-04-06 10:37:55 -0400 | [diff] [blame] | 1171 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1172 | result &= 0 == fErrors.errorCount(); |
| 1173 | return result; |
| 1174 | } |
| 1175 | |
| 1176 | } // namespace |