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