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: |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame^] | 26 | // When inside writeEmitCode(), certain SkSL elements need to control |
| 27 | // when fragBuilder->codeAppendf is added to the function block. This |
| 28 | // takes all completed statements in the SkSL buffer, and their corresponding |
| 29 | // format args, and writes them into the emitCode()'s statement block |
| 30 | // using writeCodeAppend(). |
| 31 | // |
| 32 | // This control is necessary for handling special functions in SkSL, like |
| 33 | // process(), which need to intermix the current FP's SkSL with that of |
| 34 | // an emitted child. |
| 35 | // |
| 36 | // :forceAll - If false, only the completed statements (terminated by ;), |
| 37 | // will be flushed and the sksl buffer will be set to any partial |
| 38 | // statements that remain. If true, everything is flushed, regardless. |
| 39 | void flushEmittedCode(bool forceAll = false); |
| 40 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 41 | void writef(const char* s, va_list va) SKSL_PRINTF_LIKE(2, 0); |
| 42 | |
| 43 | void writef(const char* s, ...) SKSL_PRINTF_LIKE(2, 3); |
| 44 | |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 45 | bool writeSection(const char* name, const char* prefix = ""); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 46 | |
| 47 | void writeHeader() override; |
| 48 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 49 | bool usesPrecisionModifiers() const override; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 50 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 51 | String getTypeName(const Type& type) override; |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 52 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 53 | void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence) override; |
| 54 | |
| 55 | void writeIndexExpression(const IndexExpression& i) override; |
| 56 | |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 57 | void writeIntLiteral(const IntLiteral& i) override; |
| 58 | |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 59 | void writeSwizzle(const Swizzle& swizzle) override; |
| 60 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 61 | void writeVariableReference(const VariableReference& ref) override; |
| 62 | |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 63 | String getSamplerHandle(const Variable& var); |
| 64 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 65 | void writeIfStatement(const IfStatement& s) override; |
| 66 | |
Ethan Nicholas | f1b1464 | 2018-08-09 16:18:07 -0400 | [diff] [blame] | 67 | void writeReturnStatement(const ReturnStatement& s) override; |
| 68 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 69 | void writeSwitchStatement(const SwitchStatement& s) override; |
| 70 | |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 71 | void writeFunctionCall(const FunctionCall& c) override; |
| 72 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 73 | void writeFunction(const FunctionDefinition& f) override; |
| 74 | |
| 75 | void writeSetting(const Setting& s) override; |
| 76 | |
| 77 | void writeProgramElement(const ProgramElement& p) override; |
| 78 | |
| 79 | void addUniform(const Variable& var); |
| 80 | |
| 81 | // 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] | 82 | void writeRuntimeValue(const Type& type, const Layout& layout, const String& cppCode); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 83 | |
| 84 | void writeVarInitializer(const Variable& var, const Expression& value) override; |
| 85 | |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 86 | void writeInputVars() override; |
| 87 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 88 | void writePrivateVars(); |
| 89 | |
| 90 | void writePrivateVarValues(); |
| 91 | |
Ethan Nicholas | 5b6e627 | 2017-10-13 13:11:06 -0400 | [diff] [blame] | 92 | void writeCodeAppend(const String& code); |
| 93 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 94 | bool writeEmitCode(std::vector<const Variable*>& uniforms); |
| 95 | |
| 96 | void writeSetData(std::vector<const Variable*>& uniforms); |
| 97 | |
| 98 | void writeGetKey(); |
| 99 | |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 100 | void writeOnTextureSampler(); |
| 101 | |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 102 | void writeClone(); |
| 103 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 104 | void writeTest(); |
| 105 | |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame^] | 106 | // If the returned C++ is included in the generated code, then the variable |
| 107 | // name stored in cppVar will refer to a valid SkString that matches the |
| 108 | // Expression. Successful returns leave the output buffer (and related state) |
| 109 | // unmodified. |
| 110 | // |
| 111 | // In the simplest cases, this will return "SkString {cppVar}(\"{e}\");", |
| 112 | // while more advanced cases will properly insert format arguments. |
| 113 | String convertSKSLExpressionToCPP(const Expression& e, const String& cppVar); |
| 114 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 115 | String fName; |
| 116 | String fFullName; |
| 117 | SectionAndParameterHelper fSectionAndParameterHelper; |
| 118 | String fExtraEmitCodeCode; |
| 119 | std::vector<String> fFormatArgs; |
| 120 | std::set<int> fWrittenTransformedCoords; |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 121 | // if true, we are writing a C++ expression instead of a GLSL expression |
| 122 | bool fCPPMode = false; |
Ethan Nicholas | f1b1464 | 2018-08-09 16:18:07 -0400 | [diff] [blame] | 123 | bool fInMain = false; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 124 | |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame^] | 125 | // if not null, we are accumulating SkSL for emitCode into fOut, which |
| 126 | // replaced the original buffer with a StringStream. The original buffer is |
| 127 | // stored here for restoration. |
| 128 | OutputStream* fCPPBuffer = nullptr; |
| 129 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 130 | typedef GLSLCodeGenerator INHERITED; |
| 131 | }; |
| 132 | |
| 133 | } |
| 134 | |
| 135 | #endif |