]> git.tdb.fi Git - libs/gl.git/blob - source/core/program.h
Split Module into a base class and format-specific class
[libs/gl.git] / source / core / program.h
1 #ifndef MSP_GL_PROGRAM_H_
2 #define MSP_GL_PROGRAM_H_
3
4 #include <string>
5 #include <vector>
6 #include <msp/datafile/objectloader.h>
7 #include "bindable.h"
8 #include "gl.h"
9 #include "vertexformat.h"
10
11 namespace Msp {
12 namespace GL {
13
14 class GlslModule;
15 class Module;
16 class Shader;
17
18 /**
19 A complete shader program.  Programs can be assembled of individual Shaders or
20 generated with a set of standard features.
21 */
22 class Program: public Bindable<Program>
23 {
24 public:
25         class Loader: public DataFile::CollectionObjectLoader<Program>
26         {
27         public:
28                 Loader(Program &, Collection &);
29
30         private:
31                 virtual void finish();
32
33                 void attribute(unsigned, const std::string &);
34                 void fragment_shader(const std::string &);
35                 void geometry_shader(const std::string &);
36                 void module(const std::string &);
37                 void vertex_shader(const std::string &);
38         };
39
40 private:
41         class SpecializationLoader: public DataFile::Loader
42         {
43         private:
44                 std::map<std::string, int> &spec_values;
45
46                 static ActionMap shared_actions;
47
48         public:
49                 SpecializationLoader(std::map<std::string, int> &);
50
51         private:
52                 virtual void init_actions();
53
54                 void specialize_bool(const std::string &, bool);
55                 void specialize_int(const std::string &, int);
56         };
57
58 public:
59         typedef unsigned LayoutHash;
60         struct UniformBlockInfo;
61
62         struct UniformInfo
63         {
64                 std::string name;
65                 const UniformBlockInfo *block;
66                 unsigned location;
67                 unsigned size;
68                 unsigned array_stride;
69                 unsigned matrix_stride;
70                 GLenum type;
71         };
72
73         struct UniformBlockInfo
74         {
75                 std::string name;
76                 unsigned data_size;
77                 int bind_point;
78                 std::vector<const UniformInfo *> uniforms;
79                 LayoutHash layout_hash;
80         };
81
82         struct AttributeInfo
83         {
84                 std::string name;
85                 unsigned location;
86                 unsigned size;
87                 GLenum type;
88         };
89
90         typedef std::map<std::string, UniformInfo> UniformMap;
91         typedef std::map<std::string, UniformBlockInfo> UniformBlockMap;
92         typedef std::map<std::string, AttributeInfo> AttributeMap;
93
94 private:
95         unsigned id;
96         std::vector<unsigned> stage_ids;
97         const Module *module;
98         bool linked;
99         UniformBlockMap uniform_blocks;
100         UniformMap uniforms;
101         LayoutHash uniform_layout_hash;
102         AttributeMap attributes;
103
104 public:
105         /// Constructs an empty Program with no shader stages attached.
106         Program();
107
108         /// Constructs a Program from unified source code using ProgramCompiler.
109         DEPRECATED Program(const std::string &);
110
111         /// Constructs a Program from vertex and fragment shader source code.
112         DEPRECATED Program(const std::string &, const std::string &);
113
114         /// Constructs a Program from a Module, with specialization constants.
115         Program(const Module &, const std::map<std::string, int> & = std::map<std::string, int>());
116
117 private:
118         void init();
119 public:
120         virtual ~Program();
121
122         void add_stages(const Module &, const std::map<std::string, int> & = std::map<std::string, int>());
123 private:
124         unsigned add_stage(GLenum);
125         void add_glsl_stages(const GlslModule &, const std::map<std::string, int> &);
126         void compile_glsl_stage(unsigned);
127
128 public:
129         DEPRECATED void attach_shader(Shader &shader);
130         DEPRECATED void attach_shader_owned(Shader *shader);
131         DEPRECATED void detach_shader(Shader &shader);
132         DEPRECATED const std::vector<Shader *> &get_attached_shaders() const;
133
134         DEPRECATED void bind_attribute(unsigned, const std::string &);
135         DEPRECATED void bind_attribute(VertexAttribute, const std::string &);
136         DEPRECATED void bind_fragment_data(unsigned, const std::string &);
137
138         void link();
139 private:
140         static void require_type(GLenum);
141         void query_uniforms();
142         void query_uniform_blocks(const std::vector<UniformInfo *> &);
143         void query_attributes();
144         static LayoutHash compute_layout_hash(const std::vector<const UniformInfo *> &);
145         static bool uniform_location_compare(const UniformInfo *, const UniformInfo *);
146 public:
147         bool is_linked() const { return linked; }
148         DEPRECATED std::string get_info_log() const;
149
150         LayoutHash get_uniform_layout_hash() const { return uniform_layout_hash; }
151         const UniformBlockMap &get_uniform_blocks() const { return uniform_blocks; }
152         const UniformBlockInfo &get_uniform_block_info(const std::string &) const;
153         const UniformMap &get_uniforms() const { return uniforms; }
154         const UniformInfo &get_uniform_info(const std::string &) const;
155         int get_uniform_location(const std::string &) const;
156         const AttributeMap &get_attributes() const { return attributes; }
157         const AttributeInfo &get_attribute_info(const std::string &) const;
158         int get_attribute_location(const std::string &) const;
159
160         void bind() const;
161         static void unbind();
162 };
163
164 } // namespace GL
165 } // namespace Msp
166
167 #endif