blob: 39255d601738e68f97f1f3ce6f80fe063337932f [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
Ethan Nicholas762466e2017-06-29 10:03:38 -040046 void writeVariableReference(const VariableReference& ref) override;
47
Ethan Nicholasceb4d482017-07-10 15:40:20 -040048 String getSamplerHandle(const Variable& var);
49
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -040050 void writeIfStatement(const IfStatement& s) override;
51
Ethan Nicholasf1b14642018-08-09 16:18:07 -040052 void writeReturnStatement(const ReturnStatement& s) override;
53
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -040054 void writeSwitchStatement(const SwitchStatement& s) override;
55
Ethan Nicholasceb4d482017-07-10 15:40:20 -040056 void writeFunctionCall(const FunctionCall& c) override;
57
Ethan Nicholas762466e2017-06-29 10:03:38 -040058 void writeFunction(const FunctionDefinition& f) override;
59
60 void writeSetting(const Setting& s) override;
61
62 void writeProgramElement(const ProgramElement& p) override;
63
64 void addUniform(const Variable& var);
65
66 // writes a printf escape that will be filled in at runtime by the given C++ expression string
Ethan Nicholasd608c092017-10-26 09:30:08 -040067 void writeRuntimeValue(const Type& type, const Layout& layout, const String& cppCode);
Ethan Nicholas762466e2017-06-29 10:03:38 -040068
69 void writeVarInitializer(const Variable& var, const Expression& value) override;
70
Ethan Nicholascd700e92018-08-24 16:43:57 -040071 void writeInputVars() override;
72
Ethan Nicholas762466e2017-06-29 10:03:38 -040073 void writePrivateVars();
74
75 void writePrivateVarValues();
76
Ethan Nicholas5b6e6272017-10-13 13:11:06 -040077 void writeCodeAppend(const String& code);
78
Ethan Nicholas762466e2017-06-29 10:03:38 -040079 bool writeEmitCode(std::vector<const Variable*>& uniforms);
80
81 void writeSetData(std::vector<const Variable*>& uniforms);
82
83 void writeGetKey();
84
Brian Salomonf7dcd762018-07-30 14:48:15 -040085 void writeOnTextureSampler();
86
Ethan Nicholasf57c0d62017-07-31 11:18:22 -040087 void writeClone();
88
Ethan Nicholas762466e2017-06-29 10:03:38 -040089 void writeTest();
90
91 String fName;
92 String fFullName;
93 SectionAndParameterHelper fSectionAndParameterHelper;
94 String fExtraEmitCodeCode;
95 std::vector<String> fFormatArgs;
96 std::set<int> fWrittenTransformedCoords;
Ethan Nicholas82399462017-10-16 12:35:44 -040097 // if true, we are writing a C++ expression instead of a GLSL expression
98 bool fCPPMode = false;
Ethan Nicholasf1b14642018-08-09 16:18:07 -040099 bool fInMain = false;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400100
101 typedef GLSLCodeGenerator INHERITED;
102};
103
104}
105
106#endif