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