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