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