blob: b4f31ba1b04f4f76309d49ebd27b95662ceaa4da [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
34 void writePrecisionModifier() override;
35
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040036 void writeType(const Type& type) override;
37
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
42 void writeVariableReference(const VariableReference& ref) override;
43
Ethan Nicholasceb4d482017-07-10 15:40:20 -040044 String getSamplerHandle(const Variable& var);
45
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -040046 void writeIfStatement(const IfStatement& s) override;
47
48 void writeSwitchStatement(const SwitchStatement& s) override;
49
Ethan Nicholasceb4d482017-07-10 15:40:20 -040050 void writeFunctionCall(const FunctionCall& c) override;
51
Ethan Nicholas762466e2017-06-29 10:03:38 -040052 void writeFunction(const FunctionDefinition& f) override;
53
54 void writeSetting(const Setting& s) override;
55
56 void writeProgramElement(const ProgramElement& p) override;
57
58 void addUniform(const Variable& var);
59
60 // writes a printf escape that will be filled in at runtime by the given C++ expression string
61 void writeRuntimeValue(const Type& type, const String& cppCode);
62
63 void writeVarInitializer(const Variable& var, const Expression& value) override;
64
65 void writePrivateVars();
66
67 void writePrivateVarValues();
68
69 bool writeEmitCode(std::vector<const Variable*>& uniforms);
70
71 void writeSetData(std::vector<const Variable*>& uniforms);
72
73 void writeGetKey();
74
Ethan Nicholasf57c0d62017-07-31 11:18:22 -040075 void writeClone();
76
Ethan Nicholas762466e2017-06-29 10:03:38 -040077 void writeTest();
78
79 String fName;
80 String fFullName;
81 SectionAndParameterHelper fSectionAndParameterHelper;
82 String fExtraEmitCodeCode;
83 std::vector<String> fFormatArgs;
84 std::set<int> fWrittenTransformedCoords;
85 bool fNeedColorSpaceHelper = false;
86
87 typedef GLSLCodeGenerator INHERITED;
88};
89
90}
91
92#endif