]> git.tdb.fi Git - libs/gl.git/blob - source/core/program.h
Use DataType instead of GLenum for Program variable types
[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 "datatype.h"
9 #include "gl.h"
10 #include "vertexformat.h"
11
12 namespace Msp {
13 namespace GL {
14
15 class GlslModule;
16 class Module;
17 class Shader;
18
19 /**
20 A complete shader program.  Programs can be assembled of individual Shaders or
21 generated with a set of standard features.
22 */
23 class Program: public Bindable<Program>
24 {
25 public:
26         class Loader: public DataFile::CollectionObjectLoader<Program>
27         {
28         public:
29                 Loader(Program &, Collection &);
30
31         private:
32                 virtual void finish();
33
34                 void attribute(unsigned, const std::string &);
35                 void fragment_shader(const std::string &);
36                 void geometry_shader(const std::string &);
37                 void module(const std::string &);
38                 void vertex_shader(const std::string &);
39         };
40
41 private:
42         class SpecializationLoader: public DataFile::Loader
43         {
44         private:
45                 std::map<std::string, int> &spec_values;
46
47                 static ActionMap shared_actions;
48
49         public:
50                 SpecializationLoader(std::map<std::string, int> &);
51
52         private:
53                 virtual void init_actions();
54
55                 void specialize_bool(const std::string &, bool);
56                 void specialize_int(const std::string &, int);
57         };
58
59 public:
60         typedef unsigned LayoutHash;
61         struct UniformBlockInfo;
62
63         struct UniformInfo
64         {
65                 std::string name;
66                 const UniformBlockInfo *block;
67                 unsigned location;
68                 unsigned size;
69                 unsigned array_stride;
70                 unsigned matrix_stride;
71                 DataType type;
72         };
73
74         struct UniformBlockInfo
75         {
76                 std::string name;
77                 unsigned data_size;
78                 int bind_point;
79                 std::vector<const UniformInfo *> uniforms;
80                 LayoutHash layout_hash;
81         };
82
83         struct AttributeInfo
84         {
85                 std::string name;
86                 unsigned location;
87                 unsigned size;
88                 DataType type;
89         };
90
91         typedef std::map<std::string, UniformInfo> UniformMap;
92         typedef std::map<std::string, UniformBlockInfo> UniformBlockMap;
93         typedef std::map<std::string, AttributeInfo> AttributeMap;
94
95 private:
96         unsigned id;
97         std::vector<unsigned> stage_ids;
98         const Module *module;
99         bool linked;
100         UniformBlockMap uniform_blocks;
101         UniformMap uniforms;
102         LayoutHash uniform_layout_hash;
103         AttributeMap attributes;
104
105 public:
106         /// Constructs an empty Program with no shader stages attached.
107         Program();
108
109         /// Constructs a Program from unified source code using ProgramCompiler.
110         DEPRECATED Program(const std::string &);
111
112         /// Constructs a Program from vertex and fragment shader source code.
113         DEPRECATED Program(const std::string &, const std::string &);
114
115         /// Constructs a Program from a Module, with specialization constants.
116         Program(const Module &, const std::map<std::string, int> & = std::map<std::string, int>());
117
118 private:
119         void init();
120 public:
121         virtual ~Program();
122
123         void add_stages(const Module &, const std::map<std::string, int> & = std::map<std::string, int>());
124 private:
125         unsigned add_stage(GLenum);
126         void add_glsl_stages(const GlslModule &, const std::map<std::string, int> &);
127         void compile_glsl_stage(unsigned);
128
129 public:
130         DEPRECATED void attach_shader(Shader &shader);
131         DEPRECATED void attach_shader_owned(Shader *shader);
132         DEPRECATED void detach_shader(Shader &shader);
133         DEPRECATED const std::vector<Shader *> &get_attached_shaders() const;
134
135         DEPRECATED void bind_attribute(unsigned, const std::string &);
136         DEPRECATED void bind_attribute(VertexAttribute, const std::string &);
137         DEPRECATED void bind_fragment_data(unsigned, const std::string &);
138
139         void link();
140 private:
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