]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/compiler.h
Remove Vulkan checks from feature converters
[libs/gl.git] / source / glsl / compiler.h
1 #ifndef MSP_GL_SL_COMPILER_H_
2 #define MSP_GL_SL_COMPILER_H_
3
4 #include <vector>
5 #include <msp/datafile/collection.h>
6 #include <msp/io/base.h>
7 #include "parser.h"
8 #include "syntax.h"
9
10 namespace Msp {
11 namespace GL {
12 namespace SL {
13
14 class Compiler
15 {
16 public:
17         enum Mode
18         {
19                 MODULE,
20                 PROGRAM,
21                 SPIRV
22         };
23
24 private:
25         enum OptimizeResult
26         {
27                 NEXT_STAGE,
28                 REDO_STAGE,
29                 REDO_PREVIOUS
30         };
31
32         enum ResolveFlags
33         {
34                 RESOLVE_BLOCKS = 1,
35                 RESOLVE_TYPES = 2,
36                 RESOLVE_VARIABLES = 4,
37                 RESOLVE_EXPRESSIONS = 8,
38                 RESOLVE_FUNCTIONS = 16,
39                 RESOLVE_ALL = 31
40         };
41
42         Features features;
43         Module *module = 0;
44         std::vector<std::string> imported_names;
45         bool compiled = false;
46         bool specialized = false;
47         std::map<std::string, int> spec_values;
48
49 public:
50         /** Creates a compiler targeting a specific set of features. */
51         Compiler(const Features &);
52
53         ~Compiler();
54
55 private:
56         void clear();
57
58 public:
59         /** Sets the source code to be compiled.  Only builtin imports are
60         available. */
61         void set_source(const std::string &, const std::string & = "<string>");
62
63         /** Loads source code from an I/O object.  If a collection is used, imports
64         can be fetched from it. */
65         void load_source(IO::Base &, DataFile::Collection * = 0, const std::string & = "<file>");
66
67         /** Loads source code from an I/O object.  Only builtin imports are
68         available. */
69         void load_source(IO::Base &, const std::string &);
70
71         /** Specializes the shader.  All specialization constants are considered
72         specialized, even if they do not appear in the map. */
73         void specialize(const std::map<std::string, int> &);
74
75         /** Compiles the shader. */
76         void compile(Mode);
77
78         /** Returns combined GLSL source for all shader stages.  The result is
79         suitable for feeding back to the compiler. */
80         std::string get_combined_glsl() const;
81
82         /** Returns a list of compiled stage types. */
83         std::vector<Stage::Type> get_stages() const;
84
85         /** Returns GLSL source for a single shader stage.  The result is standard
86         GLSL suitable for OpenGL or an external GLSL compiler. */
87         std::string get_stage_glsl(Stage::Type) const;
88
89         /** Returns a combined SPIR-V binary for all shader stages.  The result is
90         suitable for use with OpenGL or Vulkan. */
91         std::vector<std::uint32_t> get_combined_spirv() const;
92
93         /** Returns a map of vertex attribute locations.  If the target GLSL version
94         supports interface layouts, the map is empty (locations are included in the
95         GLSL soucre). */
96         const std::map<std::string, unsigned> &get_vertex_attributes() const;
97
98         /** Returns a map of fragment output locations.  If the target GLSL version
99         supports interface layouts, the map is empty (locations are included in the
100         GLSL soucre). */
101         const std::map<std::string, unsigned> &get_fragment_outputs() const;
102
103         /** Returns a map of texture bindings.  If the target GLSL version supports
104         bindings, the map is empty (bindings are included in the GLSL source). */
105         const std::map<std::string, unsigned> &get_texture_bindings() const;
106
107         /** Returns a map of uniform block bindings.  If the target GLSL version
108         supports bindings, the map is empty (bindings are included in the GLSL
109         source). */
110         const std::map<std::string, unsigned> &get_uniform_block_bindings() const;
111
112         unsigned get_n_clip_distances() const;
113
114         /** Returns the mapping of source indices to filenames.  Can be used to
115         translate error messages. */
116         const SourceMap &get_source_map() const;
117
118         /** Returns a textual representation of the syntax tree for a shader stage.
119         Intended for debugging purposes. */
120         std::string get_stage_debug(Stage::Type, bool = false) const;
121
122         /** Returns diagnostics from compilation.  The output is intended to be
123         viewed by humans. */
124         std::string get_diagnostics() const;
125
126 private:
127         /** Appends a module to the target, processing any imports found in it. */
128         void append_module(const Module &, ModuleCache &);
129
130         /** Appends a single stage to the matching stage of the target. */
131         void append_stage(const Stage &);
132
133         /// Imports a module by name and appends it to the target. */
134         void import(ModuleCache &, const std::string &);
135
136         /** Generates any implicitly defines syntactic structures and resolves
137         variables. */
138         void generate(Stage &);
139
140         template<typename T>
141         bool resolve(Stage &, unsigned &, unsigned);
142
143         /** Resolves various references between nodes.  Flags can be specified to
144         request resolving particular aspects.  Resolving may ripple into other
145         aspects as necessary. */
146         void resolve(Stage &, unsigned = RESOLVE_ALL);
147
148         /** Runs validators on a stage.  Diagnostic messages are recorded in the
149         stage for later inspection. */
150         void validate(Stage &);
151
152         /** Checks a stage's recorded diagnostics for errors.  If any are found,
153         returns true. */
154         bool check_errors(Stage &);
155
156         static bool diagnostic_line_order(const Diagnostic &, const Diagnostic &);
157
158         /** Applies optimizations to a stage.  The return value indicates which
159         stage should be optimized next. */
160         OptimizeResult optimize(Stage &);
161
162         /** Performs final adjustments on a stage after compilation. */
163         void finalize(Stage &, Mode);
164
165         static void inject_block(Block &, const Block &);
166 };
167
168 } // namespace SL
169 } // namespace GL
170 } // namespace Msp
171
172 #endif