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 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 46 | void writeVariableReference(const VariableReference& ref) override; |
| 47 | |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 48 | String getSamplerHandle(const Variable& var); |
| 49 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 50 | void writeIfStatement(const IfStatement& s) override; |
| 51 | |
| 52 | void writeSwitchStatement(const SwitchStatement& s) override; |
| 53 | |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 54 | void writeFunctionCall(const FunctionCall& c) override; |
| 55 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 56 | void writeFunction(const FunctionDefinition& f) override; |
| 57 | |
| 58 | void writeSetting(const Setting& s) override; |
| 59 | |
| 60 | void writeProgramElement(const ProgramElement& p) override; |
| 61 | |
| 62 | void addUniform(const Variable& var); |
| 63 | |
| 64 | // 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] | 65 | void writeRuntimeValue(const Type& type, const Layout& layout, const String& cppCode); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 66 | |
| 67 | void writeVarInitializer(const Variable& var, const Expression& value) override; |
| 68 | |
| 69 | void writePrivateVars(); |
| 70 | |
| 71 | void writePrivateVarValues(); |
| 72 | |
Ethan Nicholas | 5b6e627 | 2017-10-13 13:11:06 -0400 | [diff] [blame] | 73 | void writeCodeAppend(const String& code); |
| 74 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 75 | bool writeEmitCode(std::vector<const Variable*>& uniforms); |
| 76 | |
| 77 | void writeSetData(std::vector<const Variable*>& uniforms); |
| 78 | |
| 79 | void writeGetKey(); |
| 80 | |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 81 | void writeClone(); |
| 82 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 83 | void writeTest(); |
| 84 | |
| 85 | String fName; |
| 86 | String fFullName; |
| 87 | SectionAndParameterHelper fSectionAndParameterHelper; |
| 88 | String fExtraEmitCodeCode; |
| 89 | std::vector<String> fFormatArgs; |
| 90 | std::set<int> fWrittenTransformedCoords; |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 91 | // if true, we are writing a C++ expression instead of a GLSL expression |
| 92 | bool fCPPMode = false; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 93 | |
| 94 | typedef GLSLCodeGenerator INHERITED; |
| 95 | }; |
| 96 | |
| 97 | } |
| 98 | |
| 99 | #endif |