blob: a3efcf48f53ceb94b1b15d7cb402cb7d1174bed6 [file] [log] [blame]
Ethan Nicholas95046142021-01-07 10:57:27 -05001/*
2 * Copyright 2020 Google LLC
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_DSLWRITER
9#define SKSL_DSLWRITER
10
Ethan Nicholasdd2fdea2021-07-20 15:23:04 -040011#include "include/core/SkStringView.h"
Ethan Nicholasc8452722021-10-07 10:47:32 -040012#include "include/core/SkTypes.h"
Ethan Nicholas95046142021-01-07 10:57:27 -050013#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
Brian Salomon48959462021-08-11 13:01:06 -040014#include "src/gpu/GrFragmentProcessor.h"
Ethan Nicholasc8452722021-10-07 10:47:32 -040015#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
Ethan Nicholas95046142021-01-07 10:57:27 -050016#endif // !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
Ethan Nicholas95046142021-01-07 10:57:27 -050017
Ethan Nicholasc8452722021-10-07 10:47:32 -040018#include <memory>
Ethan Nicholas95046142021-01-07 10:57:27 -050019
20namespace SkSL {
21
Ethan Nicholasdaed2592021-03-04 14:30:25 -050022class Variable;
Ethan Nicholasc8452722021-10-07 10:47:32 -040023class Statement;
Ethan Nicholas95046142021-01-07 10:57:27 -050024
25namespace dsl {
26
Ethan Nicholasa2d22b22021-07-15 10:35:54 -040027class DSLGlobalVar;
28class DSLParameter;
Ethan Nicholasc8452722021-10-07 10:47:32 -040029class DSLStatement;
30class DSLVarBase;
Ethan Nicholasa2d22b22021-07-15 10:35:54 -040031class DSLVar;
Ethan Nicholas95046142021-01-07 10:57:27 -050032
33/**
Ethan Nicholasc8452722021-10-07 10:47:32 -040034 * Various utility methods needed by DSL code.
Ethan Nicholas95046142021-01-07 10:57:27 -050035 */
36class DSLWriter {
37public:
Ethan Nicholasbffe80a2021-01-11 15:42:44 -050038 /**
Ethan Nicholas55a63af2021-05-18 10:12:58 -040039 * Returns whether name mangling is enabled. Mangling is important for the DSL because its
40 * variables normally all go into the same symbol table; for instance if you were to translate
41 * this legal (albeit silly) GLSL code:
42 * int x;
43 * {
44 * int x;
45 * }
46 *
47 * into DSL, you'd end up with:
48 * DSLVar x1(kInt_Type, "x");
49 * DSLVar x2(kInt_Type, "x");
50 * Declare(x1);
51 * Block(Declare(x2));
52 *
53 * with x1 and x2 ending up in the same symbol table. This is fine as long as their effective
54 * names are different, so mangling prevents this situation from causing problems.
Ethan Nicholasbffe80a2021-01-11 15:42:44 -050055 */
Ethan Nicholasc8452722021-10-07 10:47:32 -040056 static bool ManglingEnabled();
57
58 static skstd::string_view Name(skstd::string_view name);
59
60 /**
61 * Returns the SkSL variable corresponding to a DSL var.
62 */
63 static const SkSL::Variable* Var(DSLVarBase& var);
64
65 /**
66 * Creates an SkSL variable corresponding to a DSLParameter.
67 */
68 static std::unique_ptr<SkSL::Variable> CreateParameterVar(DSLParameter& var);
69
70 /**
71 * Returns the SkSL declaration corresponding to a DSLVar.
72 */
73 static std::unique_ptr<SkSL::Statement> Declaration(DSLVarBase& var);
74
75 /**
76 * For use in testing only: marks the variable as having been declared, so that it can be
77 * destroyed without generating errors.
78 */
79 static void MarkDeclared(DSLVarBase& var);
Ethan Nicholas55a63af2021-05-18 10:12:58 -040080
81 /**
82 * Returns whether DSLVars should automatically be marked declared upon creation. This is used
83 * to simplify testing.
84 */
Ethan Nicholasc8452722021-10-07 10:47:32 -040085 static bool MarkVarsDeclared();
Ethan Nicholasbffe80a2021-01-11 15:42:44 -050086
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -040087 /**
Ethan Nicholasc8452722021-10-07 10:47:32 -040088 * Adds a new declaration into an existing declaration statement. This either turns the original
89 * declaration into an unscoped block or, if it already was, appends a new statement to the end
90 * of it.
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -040091 */
Ethan Nicholasc8452722021-10-07 10:47:32 -040092 static void AddVarDeclaration(DSLStatement& existing, DSLVar& additional);
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -040093
Ethan Nicholasc8452722021-10-07 10:47:32 -040094 /**
95 * Clears any elements or symbols which have been output.
96 */
97 static void Reset();
Ethan Nicholas95046142021-01-07 10:57:27 -050098
Ethan Nicholasc3bb9e32021-02-02 11:51:03 -050099#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
Ethan Nicholasc8452722021-10-07 10:47:32 -0400100 static GrGLSLUniformHandler::UniformHandle VarUniformHandle(const DSLGlobalVar& var);
101#endif
Ethan Nicholas95046142021-01-07 10:57:27 -0500102
103 friend class DSLCore;
Ethan Nicholas961d9442021-03-16 16:37:29 -0400104 friend class DSLVar;
Ethan Nicholas95046142021-01-07 10:57:27 -0500105};
106
107} // namespace dsl
108
109} // namespace SkSL
110
111#endif