]> git.tdb.fi Git - libs/gl.git/blob - source/core/program.h
Add support for integer vertex attributes
[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 "datatype.h"
8 #include "gl.h"
9 #include "module.h"
10 #include "tag.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
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                 Tag tag;
76                 int binding;
77
78                 UniformInfo();
79         };
80
81         struct UniformBlockInfo
82         {
83                 std::string name;
84                 unsigned data_size;
85                 int bind_point;
86                 std::vector<const UniformInfo *> uniforms;
87                 LayoutHash layout_hash;
88
89                 UniformBlockInfo();
90         };
91
92         struct AttributeInfo
93         {
94                 std::string name;
95                 unsigned location;
96                 unsigned array_size;
97                 DataType type;
98
99                 AttributeInfo();
100         };
101
102 private:
103         enum Stage
104         {
105                 VERTEX,
106                 GEOMETRY,
107                 FRAGMENT,
108                 MAX_STAGES
109         };
110
111         struct TransientData
112         {
113                 std::map<std::string, unsigned> textures;
114                 std::map<std::string, unsigned> blocks;
115                 std::map<unsigned, int> spec_values;
116         };
117
118         unsigned id;
119         unsigned stage_ids[MAX_STAGES];
120         const Module *module;
121         TransientData *transient;
122         bool linked;
123         std::vector<UniformBlockInfo> uniform_blocks;
124         std::vector<UniformInfo> uniforms;
125         LayoutHash uniform_layout_hash;
126         std::vector<AttributeInfo> attributes;
127         std::string debug_name;
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         unsigned get_id() const { return id; }
194
195         void set_debug_name(const std::string &);
196 private:
197         void set_stage_debug_name(unsigned, Stage);
198 };
199
200 } // namespace GL
201 } // namespace Msp
202
203 #endif