• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
11 #include "include/core/SkStringView.h"
12 #include "include/core/SkTypes.h"
13 #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
14 #include "src/gpu/GrFragmentProcessor.h"
15 #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
16 #endif // !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
17 
18 #include <memory>
19 
20 namespace SkSL {
21 
22 class Variable;
23 class Statement;
24 
25 namespace dsl {
26 
27 class DSLGlobalVar;
28 class DSLParameter;
29 class DSLStatement;
30 class DSLVarBase;
31 class DSLVar;
32 
33 /**
34  * Various utility methods needed by DSL code.
35  */
36 class DSLWriter {
37 public:
38     /**
39      * 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.
55      */
56     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);
80 
81     /**
82      * Returns whether DSLVars should automatically be marked declared upon creation. This is used
83      * to simplify testing.
84      */
85     static bool MarkVarsDeclared();
86 
87     /**
88      * 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.
91      */
92     static void AddVarDeclaration(DSLStatement& existing, DSLVar& additional);
93 
94     /**
95      * Clears any elements or symbols which have been output.
96      */
97     static void Reset();
98 
99 #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
100     static GrGLSLUniformHandler::UniformHandle VarUniformHandle(const DSLGlobalVar& var);
101 #endif
102 
103     friend class DSLCore;
104     friend class DSLVar;
105 };
106 
107 } // namespace dsl
108 
109 } // namespace SkSL
110 
111 #endif
112