]> git.tdb.fi Git - libs/gl.git/blob - source/program.h
Additional overloads for creating pipelines
[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         typedef std::vector<Shader *> ShaderList;
63         typedef std::map<std::string, UniformInfo> UniformMap;
64         typedef std::map<std::string, UniformBlockInfo> UniformBlockMap;
65
66 private:
67         unsigned id;
68         ShaderList shaders;
69         ShaderList owned_data;
70         bool linked;
71         UniformBlockMap uniform_blocks;
72         UniformMap uniforms;
73         LayoutHash uniform_layout_hash;
74         bool legacy_vars;
75
76 public:
77         /// Constructs an empty Program with no Shaders attached.
78         Program();
79
80         /// Constructs a Program with standard features.
81         Program(const ProgramBuilder::StandardFeatures &);
82
83         /// Constructs a Program from unified source code using ProgramCompiler.
84         Program(const std::string &);
85
86         /// Constructs a Program from vertex and fragment shader source code.
87         Program(const std::string &, const std::string &);
88
89 private:
90         void init();
91 public:
92         virtual ~Program();
93
94         void attach_shader(Shader &shader);
95         void attach_shader_owned(Shader *shader);
96         void detach_shader(Shader &shader);
97         const ShaderList &get_attached_shaders() const { return shaders; }
98
99         void bind_attribute(unsigned, const std::string &);
100         void bind_attribute(VertexComponent, const std::string &);
101         void bind_fragment_data(unsigned, const std::string &);
102
103         void link();
104 private:
105         static LayoutHash compute_layout_hash(const std::vector<const UniformInfo *> &);
106         static bool uniform_location_compare(const UniformInfo *, const UniformInfo *);
107 public:
108         bool is_linked() const { return linked; }
109         std::string get_info_log() const;
110
111         LayoutHash get_uniform_layout_hash() const { return uniform_layout_hash; }
112         const UniformBlockMap &get_uniform_blocks() const { return uniform_blocks; }
113         const UniformBlockInfo &get_uniform_block_info(const std::string &) const;
114         const UniformMap &get_uniforms() const { return uniforms; }
115         const UniformInfo &get_uniform_info(const std::string &) const;
116         int get_uniform_location(const std::string &) const;
117
118         bool uses_legacy_variables() const { return legacy_vars; }
119
120         void bind() const;
121         static void unbind();
122 };
123
124 } // namespace GL
125 } // namespace Msp
126
127 #endif