Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | #ifndef SKSL_CPPCODEGENERATOR |
| 9 | #define SKSL_CPPCODEGENERATOR |
| 10 | |
| 11 | #include "SkSLGLSLCodeGenerator.h" |
| 12 | #include "SkSLSectionAndParameterHelper.h" |
| 13 | |
| 14 | #include <set> |
| 15 | |
| 16 | namespace SkSL { |
| 17 | |
| 18 | class CPPCodeGenerator : public GLSLCodeGenerator { |
| 19 | public: |
| 20 | CPPCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors, |
| 21 | String name, OutputStream* out); |
| 22 | |
| 23 | bool generateCode() override; |
| 24 | |
| 25 | private: |
| 26 | void writef(const char* s, va_list va) SKSL_PRINTF_LIKE(2, 0); |
| 27 | |
| 28 | void writef(const char* s, ...) SKSL_PRINTF_LIKE(2, 3); |
| 29 | |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 30 | bool writeSection(const char* name, const char* prefix = ""); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 31 | |
| 32 | void writeHeader() override; |
| 33 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 34 | bool usesPrecisionModifiers() const override; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 35 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 36 | String getTypeName(const Type& type) override; |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 37 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 38 | void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence) override; |
| 39 | |
| 40 | void writeIndexExpression(const IndexExpression& i) override; |
| 41 | |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 42 | void writeIntLiteral(const IntLiteral& i) override; |
| 43 | |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 44 | void writeSwizzle(const Swizzle& swizzle) override; |
| 45 | |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 46 | void writeFieldAccess(const FieldAccess& access) override; |
| 47 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 48 | void writeVariableReference(const VariableReference& ref) override; |
| 49 | |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 50 | String getSamplerHandle(const Variable& var); |
| 51 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 52 | void writeIfStatement(const IfStatement& s) override; |
| 53 | |
Ethan Nicholas | f1b1464 | 2018-08-09 16:18:07 -0400 | [diff] [blame] | 54 | void writeReturnStatement(const ReturnStatement& s) override; |
| 55 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 56 | void writeSwitchStatement(const SwitchStatement& s) override; |
| 57 | |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 58 | void writeFunctionCall(const FunctionCall& c) override; |
| 59 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 60 | void writeFunction(const FunctionDefinition& f) override; |
| 61 | |
| 62 | void writeSetting(const Setting& s) override; |
| 63 | |
| 64 | void writeProgramElement(const ProgramElement& p) override; |
| 65 | |
| 66 | void addUniform(const Variable& var); |
| 67 | |
| 68 | // writes a printf escape that will be filled in at runtime by the given C++ expression string |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 69 | void writeRuntimeValue(const Type& type, const Layout& layout, const String& cppCode); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 70 | |
| 71 | void writeVarInitializer(const Variable& var, const Expression& value) override; |
| 72 | |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 73 | void writeInputVars() override; |
| 74 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 75 | void writePrivateVars(); |
| 76 | |
| 77 | void writePrivateVarValues(); |
| 78 | |
Ethan Nicholas | 5b6e627 | 2017-10-13 13:11:06 -0400 | [diff] [blame] | 79 | void writeCodeAppend(const String& code); |
| 80 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 81 | bool writeEmitCode(std::vector<const Variable*>& uniforms); |
| 82 | |
| 83 | void writeSetData(std::vector<const Variable*>& uniforms); |
| 84 | |
| 85 | void writeGetKey(); |
| 86 | |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 87 | void writeOnTextureSampler(); |
| 88 | |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 89 | void writeClone(); |
| 90 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 91 | void writeTest(); |
| 92 | |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 93 | // If the returned C++ is included in the generated code, then the variable name stored in |
| 94 | // cppVar will refer to a valid SkString that matches the Expression. Successful returns leave |
| 95 | // the output buffer (and related state) unmodified. |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 96 | // |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 97 | // In the simplest cases, this will return "SkString {cppVar}(\"{e}\");", while more advanced |
| 98 | // cases will properly insert format arguments. |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 99 | String convertSKSLExpressionToCPP(const Expression& e, const String& cppVar); |
| 100 | |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 101 | // Process accumulated sksl to split it into appended code sections, properly interleaved with |
| 102 | // the extra emit code blocks, based on statement/block locations and the inserted tokens |
| 103 | // from newExtraEmitCodeBlock(). It is necessary to split the sksl after the program has been |
| 104 | // fully walked since many elements redirect fOut to simultaneously build header sections and |
| 105 | // bodies that are then concatenated; due to this it is not possible to split the sksl emission |
| 106 | // on the fly. |
| 107 | void flushEmittedCode(); |
| 108 | |
| 109 | // Start a new extra emit code block for accumulating C++ code. This will insert a token into |
| 110 | // the sksl stream to mark the fence between previous complete sksl statements and where the |
| 111 | // C++ code added to the new block will be added to emitCode(). These tokens are removed by |
| 112 | // flushEmittedCode() as it consumes them before passing pure sksl to writeCodeAppend(). |
| 113 | void newExtraEmitCodeBlock(); |
| 114 | |
| 115 | // Append CPP code to the current extra emit code block. |
| 116 | void addExtraEmitCodeLine(const String& toAppend); |
| 117 | |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 118 | int getChildFPIndex(const VariableReference& reference) const; |
| 119 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 120 | String fName; |
| 121 | String fFullName; |
| 122 | SectionAndParameterHelper fSectionAndParameterHelper; |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 123 | std::vector<String> fExtraEmitCodeBlocks; |
| 124 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 125 | std::vector<String> fFormatArgs; |
| 126 | std::set<int> fWrittenTransformedCoords; |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 127 | // if true, we are writing a C++ expression instead of a GLSL expression |
| 128 | bool fCPPMode = false; |
Ethan Nicholas | f1b1464 | 2018-08-09 16:18:07 -0400 | [diff] [blame] | 129 | bool fInMain = false; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 130 | |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 131 | // if not null, we are accumulating SkSL for emitCode into fOut, which |
| 132 | // replaced the original buffer with a StringStream. The original buffer is |
| 133 | // stored here for restoration. |
| 134 | OutputStream* fCPPBuffer = nullptr; |
| 135 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 136 | typedef GLSLCodeGenerator INHERITED; |
| 137 | }; |
| 138 | |
| 139 | } |
| 140 | |
| 141 | #endif |