]> git.tdb.fi Git - libs/gl.git/blob - source/core/program.h
Rename some things in Program
[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 "datatype.h"
9 #include "gl.h"
10 #include "module.h"
11 #include "vertexformat.h"
12
13 namespace Msp {
14 namespace GL {
15
16 class Shader;
17
18 /**
19 A complete shader program.  Programs can be assembled of individual Shaders or
20 generated with a set of standard features.
21 */
22 class Program: public Bindable<Program>
23 {
24 public:
25         class Loader: public DataFile::CollectionObjectLoader<Program>
26         {
27         public:
28                 Loader(Program &, Collection &);
29
30         private:
31                 virtual void finish();
32
33                 void attribute(unsigned, const std::string &);
34                 void fragment_shader(const std::string &);
35                 void geometry_shader(const std::string &);
36                 void module(const std::string &);
37                 void vertex_shader(const std::string &);
38         };
39
40 private:
41         class SpecializationLoader: public DataFile::Loader
42         {
43         private:
44                 std::map<std::string, int> &spec_values;
45
46                 static ActionMap shared_actions;
47
48         public:
49                 SpecializationLoader(std::map<std::string, int> &);
50
51         private:
52                 virtual void init_actions();
53
54                 void specialize_bool(const std::string &, bool);
55                 void specialize_int(const std::string &, int);
56         };
57
58 public:
59         typedef unsigned LayoutHash;
60         struct UniformBlockInfo;
61
62         struct UniformInfo
63         {
64                 std::string name;
65                 const UniformBlockInfo *block;
66                 union
67                 {
68                         int location;
69                         unsigned offset;
70                 };
71                 unsigned array_size;
72                 unsigned array_stride;
73                 unsigned matrix_stride;
74                 DataType type;
75
76                 UniformInfo();
77         };
78
79         struct UniformBlockInfo
80         {
81                 std::string name;
82                 unsigned data_size;
83                 int bind_point;
84                 std::vector<const UniformInfo *> uniforms;
85                 LayoutHash layout_hash;
86
87                 UniformBlockInfo();
88         };
89
90         struct AttributeInfo
91         {
92                 std::string name;
93                 unsigned location;
94                 unsigned array_size;
95                 DataType type;
96
97                 AttributeInfo();
98         };
99
100         typedef std::map<std::string, UniformInfo> UniformMap;
101         typedef std::map<std::string, UniformBlockInfo> UniformBlockMap;
102         typedef std::map<std::string, AttributeInfo> AttributeMap;
103
104 private:
105         unsigned id;
106         std::vector<unsigned> stage_ids;
107         const Module *module;
108         bool linked;
109         UniformBlockMap uniform_blocks;
110         UniformMap uniforms;
111         LayoutHash uniform_layout_hash;
112         AttributeMap attributes;
113
114 public:
115         /// Constructs an empty Program with no shader stages attached.
116         Program();
117
118         /// Constructs a Program from unified source code using ProgramCompiler.
119         DEPRECATED Program(const std::string &);
120
121         /// Constructs a Program from vertex and fragment shader source code.
122         DEPRECATED Program(const std::string &, const std::string &);
123
124         /// Constructs a Program from a Module, with specialization constants.
125         Program(const Module &, const std::map<std::string, int> & = std::map<std::string, int>());
126
127 private:
128         void init();
129 public:
130         virtual ~Program();
131
132         void add_stages(const Module &, const std::map<std::string, int> & = std::map<std::string, int>());
133 private:
134         unsigned add_stage(GLenum);
135         void add_glsl_stages(const GlslModule &, const std::map<std::string, int> &);
136         void compile_glsl_stage(unsigned);
137         void add_spirv_stages(const SpirVModule &, const std::map<std::string, int> &);
138
139 public:
140         DEPRECATED void attach_shader(Shader &shader);
141         DEPRECATED void attach_shader_owned(Shader *shader);
142         DEPRECATED void detach_shader(Shader &shader);
143         DEPRECATED const std::vector<Shader *> &get_attached_shaders() const;
144
145         DEPRECATED void bind_attribute(unsigned, const std::string &);
146         DEPRECATED void bind_attribute(VertexAttribute, const std::string &);
147         DEPRECATED void bind_fragment_data(unsigned, const std::string &);
148
149         void link();
150 private:
151         void query_uniforms();
152         void query_uniform_blocks(const std::vector<UniformInfo *> &);
153         void query_attributes();
154         void collect_uniforms();
155         void collect_block_uniforms(UniformBlockInfo &, const SpirVModule::Structure &, const std::string &, unsigned);
156         void collect_attributes();
157         void update_layout_hash();
158         static LayoutHash compute_layout_hash(const std::vector<const UniformInfo *> &);
159         static bool uniform_location_compare(const UniformInfo *, const UniformInfo *);
160 public:
161         bool is_linked() const { return linked; }
162         DEPRECATED std::string get_info_log() const;
163
164         LayoutHash get_uniform_layout_hash() const { return uniform_layout_hash; }
165         const UniformBlockMap &get_uniform_blocks() const { return uniform_blocks; }
166         const UniformBlockInfo &get_uniform_block_info(const std::string &) const;
167         const UniformMap &get_uniforms() const { return uniforms; }
168         const UniformInfo &get_uniform_info(const std::string &) const;
169         int get_uniform_location(const std::string &) const;
170         const AttributeMap &get_attributes() const { return attributes; }
171         const AttributeInfo &get_attribute_info(const std::string &) const;
172         int get_attribute_location(const std::string &) const;
173
174         void bind() const;
175         static void unbind();
176 };
177
178 } // namespace GL
179 } // namespace Msp
180
181 #endif