1 /* 2 * Copyright 2021 Google LLC 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_DSLCPPCODEGENERATOR 9 #define SKSL_DSLCPPCODEGENERATOR 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 DSLCPPCodeGenerator : public GLSLCodeGenerator { 21 public: 22 DSLCPPCodeGenerator(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 writeAnyConstructor(const AnyConstructor& c, Precedence parentPrecedence) override; 31 32 void writeBlock(const Block& b); 33 34 void writeCastConstructor(const AnyConstructor& c, Precedence parentPrecedence) override; 35 36 void writeDoStatement(const DoStatement& d); 37 38 void writeFloatLiteral(const FloatLiteral& f) override; 39 40 void writeForStatement(const ForStatement& f); 41 42 void writeFunctionBody(const Block& b); 43 44 void writeIfStatement(const IfStatement& r) override; 45 46 void writeReturnStatement(const ReturnStatement& r) override; 47 48 void writeStatement(const Statement& s); 49 50 void writeSwitchStatement(const SwitchStatement& s) override; 51 52 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence) override; 53 54 void writeVar(const Variable& var); 55 56 void writeVarCtorExpression(const Variable& var); 57 58 void writeVarDeclaration(const VarDeclaration& var, bool global); 59 60 void writef(const char* s, va_list va) SK_PRINTF_LIKE(2, 0); 61 62 void writef(const char* s, ...) SK_PRINTF_LIKE(2, 3); 63 64 bool writeSection(const char* name, const char* prefix = ""); 65 66 void writeHeader() override; 67 68 void writeCppInitialValue(const Variable& var); 69 70 bool usesPrecisionModifiers() const override; 71 72 String getTypeName(const Type& type) override; 73 74 String getDSLType(const Type& type); 75 76 String getDSLModifiers(const Modifiers& type); 77 78 String getDefaultDSLValue(const Variable& var); 79 80 void writeSwizzle(const Swizzle& swizzle) override; 81 82 void writeVariableReference(const VariableReference& ref) override; 83 84 void writeFunctionCall(const FunctionCall& c) override; 85 86 void writeFunction(const FunctionDefinition& f) override; 87 88 void prepareHelperFunction(const FunctionDeclaration& decl); 89 90 void prototypeHelperFunction(const FunctionDeclaration& decl); 91 92 void writeSetting(const Setting& s) override; 93 94 void writeProgramElement(const ProgramElement& p) override; 95 96 void addUniform(const Variable& var); 97 98 // writes a printf escape that will be filled in at runtime by the given C++ expression string 99 String formatRuntimeValue(const Type& type, const Layout& layout, const String& cppCode, 100 std::vector<String>* formatArgs); 101 102 void writeInputVars() override; 103 104 void writePrivateVars(); 105 106 void writePrivateVarValues(); 107 108 bool writeEmitCode(std::vector<const Variable*>& uniforms); 109 110 void writeSetData(std::vector<const Variable*>& uniforms); 111 112 void writeGetKey(); 113 114 void writeClone(); 115 116 void writeDumpInfo(); 117 118 void writeTest(); 119 120 int getChildFPIndex(const Variable& var) const; 121 122 const char* getVariableCppName(const Variable& var); 123 124 String fName; 125 String fFullName; 126 SectionAndParameterHelper fSectionAndParameterHelper; 127 std::unordered_map<const Variable*, String> fVariableCppNames; 128 129 // true if the sksl declared its main() function with a float2 parameter AND referenced that 130 // parameter in its body. 131 bool fAccessSampleCoordsDirectly = false; 132 133 // If true, we are writing a C++ expression instead of a GLSL expression 134 bool fCPPMode = false; 135 136 // True while compiling the main() function of the FP. 137 bool fInMain = false; 138 139 using INHERITED = GLSLCodeGenerator; 140 }; 141 142 } // namespace SkSL 143 144 #endif // defined(SKSL_STANDALONE) || GR_TEST_UTILS 145 146 #endif // SKSL_DSLCPPCODEGENERATOR 147