]> git.tdb.fi Git - libs/gl.git/blob - source/program.h
Support rotating the camera frustum
[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 /**
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 standard();
35                 void vertex_shader(const std::string &);
36         };
37
38         typedef unsigned LayoutHash;
39         struct UniformBlockInfo;
40
41         struct UniformInfo
42         {
43                 std::string name;
44                 const UniformBlockInfo *block;
45                 unsigned location;
46                 unsigned size;
47                 unsigned array_stride;
48                 unsigned matrix_stride;
49                 GLenum type;
50         };
51
52         struct UniformBlockInfo
53         {
54                 std::string name;
55                 unsigned data_size;
56                 int bind_point;
57                 std::vector<const UniformInfo *> uniforms;
58                 LayoutHash layout_hash;
59         };
60
61         typedef std::list<Shader *> ShaderList;
62         typedef std::map<std::string, UniformInfo> UniformMap;
63         typedef std::map<std::string, UniformBlockInfo> UniformBlockMap;
64
65 private:
66         unsigned id;
67         ShaderList shaders;
68         ShaderList owned_data;
69         bool linked;
70         UniformBlockMap uniform_blocks;
71         UniformMap uniforms;
72         LayoutHash uniform_layout_hash;
73         bool legacy_vars;
74
75 public:
76         /// Constructs an empty Program with no Shaders attached.
77         Program();
78
79         /// Constructs a Program with standard features.
80         Program(const ProgramBuilder::StandardFeatures &);
81
82         /// Constructs a Program from vertex and fragment shader source code.
83         Program(const std::string &, const std::string &);
84
85 private:
86         void init();
87 public:
88         virtual ~Program();
89
90         void attach_shader(Shader &shader);
91         void attach_shader_owned(Shader *shader);
92         void detach_shader(Shader &shader);
93         const ShaderList &get_attached_shaders() const { return shaders; }
94
95         void bind_attribute(unsigned, const std::string &);
96         void bind_attribute(VertexComponent, const std::string &);
97         void bind_fragment_data(unsigned, const std::string &);
98
99         void link();
100 private:
101         static LayoutHash compute_layout_hash(const std::vector<const UniformInfo *> &);
102         static bool uniform_location_compare(const UniformInfo *, const UniformInfo *);
103 public:
104         bool is_linked() const { return linked; }
105         std::string get_info_log() const;
106
107         LayoutHash get_uniform_layout_hash() const { return uniform_layout_hash; }
108         const UniformBlockMap &get_uniform_blocks() const { return uniform_blocks; }
109         const UniformBlockInfo &get_uniform_block_info(const std::string &) const;
110         const UniformMap &get_uniforms() const { return uniforms; }
111         const UniformInfo &get_uniform_info(const std::string &) const;
112         int get_uniform_location(const std::string &) const;
113
114         bool uses_legacy_variables() const { return legacy_vars; }
115
116         void bind() const;
117         static void unbind();
118 };
119
120 } // namespace GL
121 } // namespace Msp
122
123 #endif