blob: 08d88a9988df5e6bc39d82d8fd7490671d5e677b [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:
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 Nicholasf57c0d62017-07-31 11:18:22 -040030 bool writeSection(const char* name, const char* prefix = "");
Ethan Nicholas762466e2017-06-29 10:03:38 -040031
32 void writeHeader() override;
33
Ethan Nicholasf7b88202017-09-18 14:10:39 -040034 bool usesPrecisionModifiers() const override;
Ethan Nicholas762466e2017-06-29 10:03:38 -040035
Ethan Nicholasf7b88202017-09-18 14:10:39 -040036 String getTypeName(const Type& type) override;
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040037
Ethan Nicholas762466e2017-06-29 10:03:38 -040038 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence) override;
39
40 void writeIndexExpression(const IndexExpression& i) override;
41
Ethan Nicholasdcba08e2017-08-02 10:52:54 -040042 void writeIntLiteral(const IntLiteral& i) override;
43
Ethan Nicholas82399462017-10-16 12:35:44 -040044 void writeSwizzle(const Swizzle& swizzle) override;
45
Michael Ludwig9094f2c2018-09-07 13:44:21 -040046 void writeFieldAccess(const FieldAccess& access) override;
47
Ethan Nicholas762466e2017-06-29 10:03:38 -040048 void writeVariableReference(const VariableReference& ref) override;
49
Ethan Nicholasceb4d482017-07-10 15:40:20 -040050 String getSamplerHandle(const Variable& var);
51
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -040052 void writeIfStatement(const IfStatement& s) override;
53
Ethan Nicholasf1b14642018-08-09 16:18:07 -040054 void writeReturnStatement(const ReturnStatement& s) override;
55
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -040056 void writeSwitchStatement(const SwitchStatement& s) override;
57
Ethan Nicholasceb4d482017-07-10 15:40:20 -040058 void writeFunctionCall(const FunctionCall& c) override;
59
Ethan Nicholas762466e2017-06-29 10:03:38 -040060 void writeFunction(const FunctionDefinition& f) override;
61
62 void writeSetting(const Setting& s) override;
63
64 void writeProgramElement(const ProgramElement& p) override;
65
66 void addUniform(const Variable& var);
67
68 // writes a printf escape that will be filled in at runtime by the given C++ expression string
Ethan Nicholasd608c092017-10-26 09:30:08 -040069 void writeRuntimeValue(const Type& type, const Layout& layout, const String& cppCode);
Ethan Nicholas762466e2017-06-29 10:03:38 -040070
71 void writeVarInitializer(const Variable& var, const Expression& value) override;
72
Ethan Nicholascd700e92018-08-24 16:43:57 -040073 void writeInputVars() override;
74
Ethan Nicholas762466e2017-06-29 10:03:38 -040075 void writePrivateVars();
76
77 void writePrivateVarValues();
78
Ethan Nicholas5b6e6272017-10-13 13:11:06 -040079 void writeCodeAppend(const String& code);
80
Ethan Nicholas762466e2017-06-29 10:03:38 -040081 bool writeEmitCode(std::vector<const Variable*>& uniforms);
82
83 void writeSetData(std::vector<const Variable*>& uniforms);
84
85 void writeGetKey();
86
Brian Salomonf7dcd762018-07-30 14:48:15 -040087 void writeOnTextureSampler();
88
Ethan Nicholasf57c0d62017-07-31 11:18:22 -040089 void writeClone();
90
Ethan Nicholas762466e2017-06-29 10:03:38 -040091 void writeTest();
92
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -040093 // If the returned C++ is included in the generated code, then the variable name stored in
94 // cppVar will refer to a valid SkString that matches the Expression. Successful returns leave
95 // the output buffer (and related state) unmodified.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -040096 //
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -040097 // In the simplest cases, this will return "SkString {cppVar}(\"{e}\");", while more advanced
98 // cases will properly insert format arguments.
Michael Ludwig92e4c7f2018-08-30 16:08:18 -040099 String convertSKSLExpressionToCPP(const Expression& e, const String& cppVar);
100
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400101 // Process accumulated sksl to split it into appended code sections, properly interleaved with
102 // the extra emit code blocks, based on statement/block locations and the inserted tokens
103 // from newExtraEmitCodeBlock(). It is necessary to split the sksl after the program has been
104 // fully walked since many elements redirect fOut to simultaneously build header sections and
105 // bodies that are then concatenated; due to this it is not possible to split the sksl emission
106 // on the fly.
107 void flushEmittedCode();
108
109 // Start a new extra emit code block for accumulating C++ code. This will insert a token into
110 // the sksl stream to mark the fence between previous complete sksl statements and where the
111 // C++ code added to the new block will be added to emitCode(). These tokens are removed by
112 // flushEmittedCode() as it consumes them before passing pure sksl to writeCodeAppend().
113 void newExtraEmitCodeBlock();
114
115 // Append CPP code to the current extra emit code block.
116 void addExtraEmitCodeLine(const String& toAppend);
117
Robert Phillipsbce7d862019-02-21 22:53:57 +0000118 int getChildFPIndex(const VariableReference& reference) const;
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400119
Ethan Nicholas762466e2017-06-29 10:03:38 -0400120 String fName;
121 String fFullName;
122 SectionAndParameterHelper fSectionAndParameterHelper;
Michael Ludwig1fc5fbd2018-09-07 13:13:06 -0400123 std::vector<String> fExtraEmitCodeBlocks;
124
Ethan Nicholas762466e2017-06-29 10:03:38 -0400125 std::vector<String> fFormatArgs;
126 std::set<int> fWrittenTransformedCoords;
Ethan Nicholas82399462017-10-16 12:35:44 -0400127 // if true, we are writing a C++ expression instead of a GLSL expression
128 bool fCPPMode = false;
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400129 bool fInMain = false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400130
Michael Ludwig92e4c7f2018-08-30 16:08:18 -0400131 // if not null, we are accumulating SkSL for emitCode into fOut, which
132 // replaced the original buffer with a StringStream. The original buffer is
133 // stored here for restoration.
134 OutputStream* fCPPBuffer = nullptr;
135
Ethan Nicholas762466e2017-06-29 10:03:38 -0400136 typedef GLSLCodeGenerator INHERITED;
137};
138
139}
140
141#endif