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