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