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