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/SkSLSectionAndParameterHelper.h" 12 #include "src/sksl/codegen/SkSLGLSLCodeGenerator.h" 13 14 #include <set> 15 16 #if defined(SKSL_STANDALONE) || GR_TEST_UTILS 17 18 namespace SkSL { 19 20 class CPPCodeGenerator : public GLSLCodeGenerator { 21 public: 22 CPPCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors, 23 String name, OutputStream* out); 24 25 bool generateCode() override; 26 27 private: 28 using Precedence = Operator::Precedence; 29 30 void writef(const char* s, va_list va) SK_PRINTF_LIKE(2, 0); 31 32 void writef(const char* s, ...) SK_PRINTF_LIKE(2, 3); 33 34 bool writeSection(const char* name, const char* prefix = ""); 35 36 void writeHeader() override; 37 38 bool usesPrecisionModifiers() const override; 39 40 String getTypeName(const Type& type) override; 41 42 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence) override; 43 44 void writeIntLiteral(const IntLiteral& i) override; 45 46 void writeSwizzle(const Swizzle& swizzle) 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 String getSampleVarName(const char* prefix, int sampleCounter); 59 60 void writeFunctionCall(const FunctionCall& c) override; 61 62 void writeFunction(const FunctionDefinition& f) override; 63 64 void prepareHelperFunction(const FunctionDeclaration& decl); 65 66 void prototypeHelperFunction(const FunctionDeclaration& decl); 67 68 void writeSetting(const Setting& s) override; 69 70 void writeProgramElement(const ProgramElement& p) override; 71 72 void addUniform(const Variable& var); 73 74 // writes a printf escape that will be filled in at runtime by the given C++ expression string 75 void writeRuntimeValue(const Type& type, const Layout& layout, const String& cppCode); 76 String formatRuntimeValue(const Type& type, const Layout& layout, const String& cppCode, 77 std::vector<String>* formatArgs); 78 79 void writeVarInitializer(const Variable& var, const Expression& value) override; 80 81 void writeInputVars() override; 82 83 void writePrivateVars(); 84 85 void writePrivateVarValues(); 86 87 void writeCodeAppend(const String& code); 88 89 String assembleCodeAndFormatArgPrintf(const String& code); 90 91 bool writeEmitCode(std::vector<const Variable*>& uniforms); 92 93 void writeSetData(std::vector<const Variable*>& uniforms); 94 95 void writeGetKey(); 96 97 void writeOnTextureSampler(); 98 99 void writeClone(); 100 101 void writeDumpInfo(); 102 103 void writeTest(); 104 105 // If the returned C++ is included in the generated code, then the variable name stored in 106 // cppVar will refer to a valid SkString that matches the Expression. Successful returns leave 107 // the output buffer (and related state) unmodified. 108 // 109 // In the simplest cases, this will return "SkString {cppVar}(\"{e}\");", while more advanced 110 // cases will properly insert format arguments. 111 String convertSKSLExpressionToCPP(const Expression& e, const String& cppVar); 112 113 // Process accumulated sksl to split it into appended code sections, properly interleaved with 114 // the extra emit code blocks, based on statement/block locations and the inserted tokens 115 // from newExtraEmitCodeBlock(). It is necessary to split the sksl after the program has been 116 // fully walked since many elements redirect fOut to simultaneously build header sections and 117 // bodies that are then concatenated; due to this it is not possible to split the sksl emission 118 // on the fly. 119 void flushEmittedCode(); 120 121 // Start a new extra emit code block for accumulating C++ code. This will insert a token into 122 // the sksl stream to mark the fence between previous complete sksl statements and where the 123 // C++ code added to the new block will be added to emitCode(). These tokens are removed by 124 // flushEmittedCode() as it consumes them before passing pure sksl to writeCodeAppend(). 125 void newExtraEmitCodeBlock(); 126 127 // Append CPP code to the current extra emit code block. 128 void addExtraEmitCodeLine(const String& toAppend); 129 130 int getChildFPIndex(const Variable& var) const; 131 132 String fName; 133 String fFullName; 134 SectionAndParameterHelper fSectionAndParameterHelper; 135 std::vector<String> fExtraEmitCodeBlocks; 136 137 std::vector<String> fFormatArgs; 138 // true if the sksl declared its main() function with a float2 parameter AND referenced that 139 // parameter in its body. 140 bool fAccessSampleCoordsDirectly = false; 141 142 // If true, we are writing a C++ expression instead of a GLSL expression 143 bool fCPPMode = false; 144 145 // True while compiling the main() function of the FP. 146 bool fInMain = false; 147 148 // Gives unique but predictable names to invocations of sample(). 149 int fSampleCounter = 0; 150 151 // if not null, we are accumulating SkSL for emitCode into fOut, which 152 // replaced the original buffer with a StringStream. The original buffer is 153 // stored here for restoration. 154 OutputStream* fCPPBuffer = nullptr; 155 156 using INHERITED = GLSLCodeGenerator; 157 }; 158 159 } // namespace SkSL 160 161 #endif // defined(SKSL_STANDALONE) || GR_TEST_UTILS 162 163 #endif // SKSL_CPPCODEGENERATOR 164