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