]> git.tdb.fi Git - libs/gl.git/blob - source/program.h
Add an overload of bind_attribute that takes a VertexComponent
[libs/gl.git] / source / program.h
1 #ifndef MSP_GL_PROGRAM_H_
2 #define MSP_GL_PROGRAM_H_
3
4 #include <list>
5 #include <string>
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 class Program: public Bindable<Program>
18 {
19 public:
20         class Loader: public DataFile::ObjectLoader<Program>
21         {
22         public:
23                 Loader(Program &);
24
25         private:
26                 virtual void finish();
27
28                 void attribute(unsigned, const std::string &);
29                 void fragment_shader(const std::string &);
30                 void standard();
31                 void vertex_shader(const std::string &);
32         };
33
34         struct UniformBlockInfo;
35
36         struct UniformInfo
37         {
38                 std::string name;
39                 const UniformBlockInfo *block;
40                 unsigned location;
41                 unsigned size;
42                 unsigned array_stride;
43                 unsigned matrix_stride;
44                 GLenum type;
45         };
46
47         struct UniformBlockInfo
48         {
49                 std::string name;
50                 unsigned data_size;
51                 unsigned bind_point;
52                 std::vector<const UniformInfo *> uniforms;
53                 unsigned layout_hash;
54         };
55
56         typedef std::list<Shader *> ShaderList;
57         typedef std::map<std::string, UniformInfo> UniformMap;
58         typedef std::map<std::string, UniformBlockInfo> UniformBlockMap;
59
60 private:
61         unsigned id;
62         ShaderList shaders;
63         ShaderList owned_data;
64         bool linked;
65         UniformBlockMap uniform_blocks;
66         UniformMap uniforms;
67         unsigned uniform_layout_hash;
68         bool legacy_vars;
69
70 public:
71         Program();
72         Program(const ProgramBuilder::StandardFeatures &);
73         Program(const std::string &, const std::string &);
74 private:
75         void init();
76 public:
77         virtual ~Program();
78
79         void attach_shader(Shader &shader);
80         void attach_shader_owned(Shader *shader);
81         void detach_shader(Shader &shader);
82         const ShaderList &get_shaders() const { return shaders; }
83
84         void bind_attribute(unsigned, const std::string &);
85         void bind_attribute(VertexComponent, const std::string &);
86         void bind_fragment_data(unsigned, const std::string &);
87
88         void link();
89 private:
90         static unsigned compute_layout_hash(const std::vector<const UniformInfo *> &);
91         static bool uniform_location_compare(const UniformInfo *, const UniformInfo *);
92 public:
93         bool is_linked() const { return linked; }
94         std::string get_info_log() const;
95
96         unsigned get_uniform_layout_hash() const { return uniform_layout_hash; }
97         const UniformBlockMap &get_uniform_blocks() const { return uniform_blocks; }
98         const UniformBlockInfo &get_uniform_block_info(const std::string &) const;
99         const UniformMap &get_uniforms() const { return uniforms; }
100         const UniformInfo &get_uniform_info(const std::string &) const;
101         int get_uniform_location(const std::string &) const;
102
103         bool uses_legacy_variables() const { return legacy_vars; }
104
105         void bind() const;
106         static void unbind();
107 };
108
109 } // namespace GL
110 } // namespace Msp
111
112 #endif