]> git.tdb.fi Git - libs/gl.git/blob - source/core/program.h
Rename VertexComponent to VertexAttribute
[libs/gl.git] / source / core / 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 Module;
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::CollectionObjectLoader<Program>
25         {
26         public:
27                 Loader(Program &, Collection &);
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 module(const std::string &);
36                 void vertex_shader(const std::string &);
37         };
38
39 private:
40         class SpecializationLoader: public DataFile::Loader
41         {
42         private:
43                 std::map<std::string, int> &spec_values;
44
45                 static ActionMap shared_actions;
46
47         public:
48                 SpecializationLoader(std::map<std::string, int> &);
49
50         private:
51                 virtual void init_actions();
52
53                 void specialize_bool(const std::string &, bool);
54                 void specialize_int(const std::string &, int);
55         };
56
57 public:
58         typedef unsigned LayoutHash;
59         struct UniformBlockInfo;
60
61         struct UniformInfo
62         {
63                 std::string name;
64                 const UniformBlockInfo *block;
65                 unsigned location;
66                 unsigned size;
67                 unsigned array_stride;
68                 unsigned matrix_stride;
69                 GLenum type;
70         };
71
72         struct UniformBlockInfo
73         {
74                 std::string name;
75                 unsigned data_size;
76                 int bind_point;
77                 std::vector<const UniformInfo *> uniforms;
78                 LayoutHash layout_hash;
79         };
80
81         struct AttributeInfo
82         {
83                 std::string name;
84                 unsigned location;
85                 unsigned size;
86                 GLenum type;
87         };
88
89         typedef std::map<std::string, UniformInfo> UniformMap;
90         typedef std::map<std::string, UniformBlockInfo> UniformBlockMap;
91         typedef std::map<std::string, AttributeInfo> AttributeMap;
92
93 private:
94         unsigned id;
95         std::vector<unsigned> shader_ids;
96         const Module *module;
97         bool linked;
98         UniformBlockMap uniform_blocks;
99         UniformMap uniforms;
100         LayoutHash uniform_layout_hash;
101         AttributeMap attributes;
102
103 public:
104         /// Constructs an empty Program with no shader stages attached.
105         Program();
106
107         /// Constructs a Program from unified source code using ProgramCompiler.
108         DEPRECATED Program(const std::string &);
109
110         /// Constructs a Program from vertex and fragment shader source code.
111         DEPRECATED Program(const std::string &, const std::string &);
112
113         /// Constructs a Program from a Module, with specialization constants.
114         Program(const Module &, const std::map<std::string, int> & = std::map<std::string, int>());
115
116 private:
117         void init();
118 public:
119         virtual ~Program();
120
121         void add_stages(const Module &, const std::map<std::string, int> & = std::map<std::string, int>());
122
123         DEPRECATED void attach_shader(Shader &shader);
124         DEPRECATED void attach_shader_owned(Shader *shader);
125         DEPRECATED void detach_shader(Shader &shader);
126         DEPRECATED const std::vector<Shader *> &get_attached_shaders() const;
127
128         DEPRECATED void bind_attribute(unsigned, const std::string &);
129         DEPRECATED void bind_attribute(VertexAttribute, const std::string &);
130         DEPRECATED void bind_fragment_data(unsigned, const std::string &);
131
132         void link();
133 private:
134         static void require_type(GLenum);
135         void query_uniforms();
136         void query_uniform_blocks(const std::vector<UniformInfo *> &);
137         void query_attributes();
138         static LayoutHash compute_layout_hash(const std::vector<const UniformInfo *> &);
139         static bool uniform_location_compare(const UniformInfo *, const UniformInfo *);
140 public:
141         bool is_linked() const { return linked; }
142         DEPRECATED std::string get_info_log() const;
143
144         LayoutHash get_uniform_layout_hash() const { return uniform_layout_hash; }
145         const UniformBlockMap &get_uniform_blocks() const { return uniform_blocks; }
146         const UniformBlockInfo &get_uniform_block_info(const std::string &) const;
147         const UniformMap &get_uniforms() const { return uniforms; }
148         const UniformInfo &get_uniform_info(const std::string &) const;
149         int get_uniform_location(const std::string &) const;
150         const AttributeMap &get_attributes() const { return attributes; }
151         const AttributeInfo &get_attribute_info(const std::string &) const;
152         int get_attribute_location(const std::string &) const;
153
154         void bind() const;
155         static void unbind();
156 };
157
158 } // namespace GL
159 } // namespace Msp
160
161 #endif