| 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 | String formatRuntimeValue(const Type& type, const Layout& layout, const String& cppCode, | 
|---|
| 71 | std::vector<String>* formatArgs); | 
|---|
| 72 |  | 
|---|
| 73 | void writeVarInitializer(const Variable& var, const Expression& value) override; | 
|---|
| 74 |  | 
|---|
| 75 | void writeInputVars() override; | 
|---|
| 76 |  | 
|---|
| 77 | void writePrivateVars(); | 
|---|
| 78 |  | 
|---|
| 79 | void writePrivateVarValues(); | 
|---|
| 80 |  | 
|---|
| 81 | void writeCodeAppend(const String& code); | 
|---|
| 82 |  | 
|---|
| 83 | bool writeEmitCode(std::vector<const Variable*>& uniforms); | 
|---|
| 84 |  | 
|---|
| 85 | void writeSetData(std::vector<const Variable*>& uniforms); | 
|---|
| 86 |  | 
|---|
| 87 | void writeGetKey(); | 
|---|
| 88 |  | 
|---|
| 89 | void writeOnTextureSampler(); | 
|---|
| 90 |  | 
|---|
| 91 | void writeClone(); | 
|---|
| 92 |  | 
|---|
| 93 | void writeDumpInfo(); | 
|---|
| 94 |  | 
|---|
| 95 | void writeTest(); | 
|---|
| 96 |  | 
|---|
| 97 | // If the returned C++ is included in the generated code, then the variable name stored in | 
|---|
| 98 | // cppVar will refer to a valid SkString that matches the Expression. Successful returns leave | 
|---|
| 99 | // the output buffer (and related state) unmodified. | 
|---|
| 100 | // | 
|---|
| 101 | // In the simplest cases, this will return "SkString {cppVar}(\"{e}\");", while more advanced | 
|---|
| 102 | // cases will properly insert format arguments. | 
|---|
| 103 | String convertSKSLExpressionToCPP(const Expression& e, const String& cppVar); | 
|---|
| 104 |  | 
|---|
| 105 | // Process accumulated sksl to split it into appended code sections, properly interleaved with | 
|---|
| 106 | // the extra emit code blocks, based on statement/block locations and the inserted tokens | 
|---|
| 107 | // from newExtraEmitCodeBlock(). It is necessary to split the sksl after the program has been | 
|---|
| 108 | // fully walked since many elements redirect fOut to simultaneously build header sections and | 
|---|
| 109 | // bodies that are then concatenated; due to this it is not possible to split the sksl emission | 
|---|
| 110 | // on the fly. | 
|---|
| 111 | void flushEmittedCode(); | 
|---|
| 112 |  | 
|---|
| 113 | // Start a new extra emit code block for accumulating C++ code. This will insert a token into | 
|---|
| 114 | // the sksl stream to mark the fence between previous complete sksl statements and where the | 
|---|
| 115 | // C++ code added to the new block will be added to emitCode(). These tokens are removed by | 
|---|
| 116 | // flushEmittedCode() as it consumes them before passing pure sksl to writeCodeAppend(). | 
|---|
| 117 | void (); | 
|---|
| 118 |  | 
|---|
| 119 | // Append CPP code to the current extra emit code block. | 
|---|
| 120 | void (const String& toAppend); | 
|---|
| 121 |  | 
|---|
| 122 | int getChildFPIndex(const Variable& var) const; | 
|---|
| 123 |  | 
|---|
| 124 | String fName; | 
|---|
| 125 | String fFullName; | 
|---|
| 126 | SectionAndParameterHelper fSectionAndParameterHelper; | 
|---|
| 127 | std::vector<String> ; | 
|---|
| 128 |  | 
|---|
| 129 | std::vector<String> fFormatArgs; | 
|---|
| 130 | // true if the sksl declared its main() function with a float2 parameter AND referenced that | 
|---|
| 131 | // parameter in its body. | 
|---|
| 132 | bool fAccessSampleCoordsDirectly = false; | 
|---|
| 133 |  | 
|---|
| 134 | // if true, we are writing a C++ expression instead of a GLSL expression | 
|---|
| 135 | bool fCPPMode = false; | 
|---|
| 136 | bool fInMain = false; | 
|---|
| 137 |  | 
|---|
| 138 | // if not null, we are accumulating SkSL for emitCode into fOut, which | 
|---|
| 139 | // replaced the original buffer with a StringStream. The original buffer is | 
|---|
| 140 | // stored here for restoration. | 
|---|
| 141 | OutputStream* fCPPBuffer = nullptr; | 
|---|
| 142 |  | 
|---|
| 143 | typedef GLSLCodeGenerator INHERITED; | 
|---|
| 144 | }; | 
|---|
| 145 |  | 
|---|
| 146 | }  // namespace SkSL | 
|---|
| 147 |  | 
|---|
| 148 | #endif | 
|---|
| 149 |  | 
|---|