]> git.tdb.fi Git - libs/gl.git/blob - source/program.h
Store information about attributes in Program
[libs/gl.git] / source / 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 "programbuilder.h"
10 #include "vertexformat.h"
11
12 namespace Msp {
13 namespace GL {
14
15 class Shader;
16
17 /**
18 A complete shader program.  Programs can be assembled of individual Shaders or
19 generated with a set of standard features.
20 */
21 class Program: public Bindable<Program>
22 {
23 public:
24         class Loader: public DataFile::ObjectLoader<Program>
25         {
26         public:
27                 Loader(Program &);
28
29         private:
30                 virtual void finish();
31
32                 void attribute(unsigned, const std::string &);
33                 void fragment_shader(const std::string &);
34                 void geometry_shader(const std::string &);
35                 void standard();
36                 void vertex_shader(const std::string &);
37         };
38
39         typedef unsigned LayoutHash;
40         struct UniformBlockInfo;
41
42         struct UniformInfo
43         {
44                 std::string name;
45                 const UniformBlockInfo *block;
46                 unsigned location;
47                 unsigned size;
48                 unsigned array_stride;
49                 unsigned matrix_stride;
50                 GLenum type;
51         };
52
53         struct UniformBlockInfo
54         {
55                 std::string name;
56                 unsigned data_size;
57                 int bind_point;
58                 std::vector<const UniformInfo *> uniforms;
59                 LayoutHash layout_hash;
60         };
61
62         struct AttributeInfo
63         {
64                 std::string name;
65                 unsigned location;
66                 unsigned size;
67                 GLenum type;
68         };
69
70         typedef std::vector<Shader *> ShaderList;
71         typedef std::map<std::string, UniformInfo> UniformMap;
72         typedef std::map<std::string, UniformBlockInfo> UniformBlockMap;
73         typedef std::map<std::string, AttributeInfo> AttributeMap;
74
75 private:
76         unsigned id;
77         ShaderList shaders;
78         ShaderList owned_data;
79         bool linked;
80         UniformBlockMap uniform_blocks;
81         UniformMap uniforms;
82         LayoutHash uniform_layout_hash;
83         AttributeMap attributes;
84         bool legacy_vars;
85
86 public:
87         /// Constructs an empty Program with no Shaders attached.
88         Program();
89
90         /// Constructs a Program with standard features.
91         Program(const ProgramBuilder::StandardFeatures &);
92
93         /// Constructs a Program from unified source code using ProgramCompiler.
94         Program(const std::string &);
95
96         /// Constructs a Program from vertex and fragment shader source code.
97         Program(const std::string &, const std::string &);
98
99 private:
100         void init();
101 public:
102         virtual ~Program();
103
104         void attach_shader(Shader &shader);
105         void attach_shader_owned(Shader *shader);
106         void detach_shader(Shader &shader);
107         const ShaderList &get_attached_shaders() const { return shaders; }
108
109         void bind_attribute(unsigned, const std::string &);
110         void bind_attribute(VertexComponent, const std::string &);
111         void bind_fragment_data(unsigned, const std::string &);
112
113         void link();
114 private:
115         void query_uniforms();
116         void query_uniform_blocks(const std::vector<UniformInfo *> &);
117         void query_attributes();
118         static LayoutHash compute_layout_hash(const std::vector<const UniformInfo *> &);
119         static bool uniform_location_compare(const UniformInfo *, const UniformInfo *);
120 public:
121         bool is_linked() const { return linked; }
122         std::string get_info_log() const;
123
124         LayoutHash get_uniform_layout_hash() const { return uniform_layout_hash; }
125         const UniformBlockMap &get_uniform_blocks() const { return uniform_blocks; }
126         const UniformBlockInfo &get_uniform_block_info(const std::string &) const;
127         const UniformMap &get_uniforms() const { return uniforms; }
128         const UniformInfo &get_uniform_info(const std::string &) const;
129         int get_uniform_location(const std::string &) const;
130         const AttributeMap &get_attributes() const { return attributes; }
131         const AttributeInfo &get_attribute_info(const std::string &) const;
132         int get_attribute_location(const std::string &) const;
133
134         bool uses_legacy_variables() const { return legacy_vars; }
135
136         void bind() const;
137         static void unbind();
138 };
139
140 } // namespace GL
141 } // namespace Msp
142
143 #endif