blob: 6c67bad78399e9da9a08b27d2bbba12e2008372b [file] [log] [blame]
Ethan Nicholas762466e2017-06-29 10:03:38 -04001/*
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
16namespace SkSL {
17
18class CPPCodeGenerator : public GLSLCodeGenerator {
19public:
20 CPPCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
21 String name, OutputStream* out);
22
23 bool generateCode() override;
24
25private:
Michael Ludwig92e4c7f2018-08-30 16:08:18 -040026 // 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 Nicholas762466e2017-06-29 10:03:38 -040041 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 Nicholasf57c0d62017-07-31 11:18:22 -040045 bool writeSection(const char* name, const char* prefix = "");
Ethan Nicholas762466e2017-06-29 10:03:38 -040046
47 void writeHeader() override;
48
Ethan Nicholasf7b88202017-09-18 14:10:39 -040049 bool usesPrecisionModifiers() const override;
Ethan Nicholas762466e2017-06-29 10:03:38 -040050
Ethan Nicholasf7b88202017-09-18 14:10:39 -040051 String getTypeName(const Type& type) override;
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040052
Ethan Nicholas762466e2017-06-29 10:03:38 -040053 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence) override;
54
55 void writeIndexExpression(const IndexExpression& i) override;
56
Ethan Nicholasdcba08e2017-08-02 10:52:54 -040057 void writeIntLiteral(const IntLiteral& i) override;
58
Ethan Nicholas82399462017-10-16 12:35:44 -040059 void writeSwizzle(const Swizzle& swizzle) override;
60
Ethan Nicholas762466e2017-06-29 10:03:38 -040061 void writeVariableReference(const VariableReference& ref) override;
62
Ethan Nicholasceb4d482017-07-10 15:40:20 -040063 String getSamplerHandle(const Variable& var);
64
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -040065 void writeIfStatement(const IfStatement& s) override;
66
Ethan Nicholasf1b14642018-08-09 16:18:07 -040067 void writeReturnStatement(const ReturnStatement& s) override;
68
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -040069 void writeSwitchStatement(const SwitchStatement& s) override;
70
Ethan Nicholasceb4d482017-07-10 15:40:20 -040071 void writeFunctionCall(const FunctionCall& c) override;
72
Ethan Nicholas762466e2017-06-29 10:03:38 -040073 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 Nicholasd608c092017-10-26 09:30:08 -040082 void writeRuntimeValue(const Type& type, const Layout& layout, const String& cppCode);
Ethan Nicholas762466e2017-06-29 10:03:38 -040083
84 void writeVarInitializer(const Variable& var, const Expression& value) override;
85
Ethan Nicholascd700e92018-08-24 16:43:57 -040086 void writeInputVars() override;
87
Ethan Nicholas762466e2017-06-29 10:03:38 -040088 void writePrivateVars();
89
90 void writePrivateVarValues();
91
Ethan Nicholas5b6e6272017-10-13 13:11:06 -040092 void writeCodeAppend(const String& code);
93
Ethan Nicholas762466e2017-06-29 10:03:38 -040094 bool writeEmitCode(std::vector<const Variable*>& uniforms);
95
96 void writeSetData(std::vector<const Variable*>& uniforms);
97
98 void writeGetKey();
99
Brian Salomonf7dcd762018-07-30 14:48:15 -0400100 void writeOnTextureSampler();
101
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400102 void writeClone();
103
Ethan Nicholas762466e2017-06-29 10:03:38 -0400104 void writeTest();
105
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400106 // 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 Nicholas762466e2017-06-29 10:03:38 -0400115 String fName;
116 String fFullName;
117 SectionAndParameterHelper fSectionAndParameterHelper;
118 String fExtraEmitCodeCode;
119 std::vector<String> fFormatArgs;
120 std::set<int> fWrittenTransformedCoords;
Ethan Nicholas82399462017-10-16 12:35:44 -0400121 // if true, we are writing a C++ expression instead of a GLSL expression
122 bool fCPPMode = false;
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400123 bool fInMain = false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400124
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400125 // 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 Nicholas762466e2017-06-29 10:03:38 -0400130 typedef GLSLCodeGenerator INHERITED;
131};
132
133}
134
135#endif