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 "src/sksl/SkSLGLSLCodeGenerator.h" |
12 | #include "src/sksl/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 | |
30 | bool writeSection(const char* name, const char* prefix = "" ); |
31 | |
32 | void () override; |
33 | |
34 | bool usesPrecisionModifiers() const override; |
35 | |
36 | String getTypeName(const Type& type) override; |
37 | |
38 | void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence) override; |
39 | |
40 | void writeIndexExpression(const IndexExpression& i) override; |
41 | |
42 | void writeIntLiteral(const IntLiteral& i) override; |
43 | |
44 | void writeSwizzle(const Swizzle& swizzle) override; |
45 | |
46 | void writeFieldAccess(const FieldAccess& access) override; |
47 | |
48 | void writeVariableReference(const VariableReference& ref) override; |
49 | |
50 | String getSamplerHandle(const Variable& var); |
51 | |
52 | void writeIfStatement(const IfStatement& s) override; |
53 | |
54 | void writeReturnStatement(const ReturnStatement& s) override; |
55 | |
56 | void writeSwitchStatement(const SwitchStatement& s) override; |
57 | |
58 | void writeFunctionCall(const FunctionCall& c) override; |
59 | |
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 |
69 | void writeRuntimeValue(const Type& type, const Layout& layout, const String& cppCode); |
70 | |
71 | void writeVarInitializer(const Variable& var, const Expression& value) override; |
72 | |
73 | void writeInputVars() override; |
74 | |
75 | void writePrivateVars(); |
76 | |
77 | void writePrivateVarValues(); |
78 | |
79 | void writeCodeAppend(const String& code); |
80 | |
81 | bool writeEmitCode(std::vector<const Variable*>& uniforms); |
82 | |
83 | void writeSetData(std::vector<const Variable*>& uniforms); |
84 | |
85 | void writeGetKey(); |
86 | |
87 | void writeOnTextureSampler(); |
88 | |
89 | void writeClone(); |
90 | |
91 | void writeTest(); |
92 | |
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. |
96 | // |
97 | // In the simplest cases, this will return "SkString {cppVar}(\"{e}\");", while more advanced |
98 | // cases will properly insert format arguments. |
99 | String convertSKSLExpressionToCPP(const Expression& e, const String& cppVar); |
100 | |
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 (); |
114 | |
115 | // Append CPP code to the current extra emit code block. |
116 | void (const String& toAppend); |
117 | |
118 | int getChildFPIndex(const Variable& var) const; |
119 | |
120 | String fName; |
121 | String fFullName; |
122 | SectionAndParameterHelper fSectionAndParameterHelper; |
123 | std::vector<String> ; |
124 | |
125 | std::vector<String> fFormatArgs; |
126 | std::set<int> fWrittenTransformedCoords; |
127 | // if true, we are writing a C++ expression instead of a GLSL expression |
128 | bool fCPPMode = false; |
129 | bool fInMain = false; |
130 | |
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 | |
136 | typedef GLSLCodeGenerator INHERITED; |
137 | }; |
138 | |
139 | } |
140 | |
141 | #endif |
142 | |