]> git.tdb.fi Git - libs/gl.git/blob - source/core/program.h
Store default-block uniform data in a memory block
[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 "gl.h"
8 #include "module.h"
9 #include "reflectdata.h"
10 #include "vertexformat.h"
11
12 namespace Msp {
13 namespace GL {
14
15 /**
16 A complete shader program.  Programs can be assembled of individual Shaders or
17 generated with a set of standard features.
18 */
19 class Program
20 {
21         friend class PipelineState;
22
23 public:
24         class Loader: public DataFile::CollectionObjectLoader<Program>
25         {
26         public:
27                 Loader(Program &, Collection &);
28
29         private:
30                 void module(const std::string &);
31         };
32
33 private:
34         class SpecializationLoader: public DataFile::Loader
35         {
36         private:
37                 std::map<std::string, int> &spec_values;
38
39                 static ActionMap shared_actions;
40
41         public:
42                 SpecializationLoader(std::map<std::string, int> &);
43
44         private:
45                 virtual void init_actions();
46
47                 void specialize_bool(const std::string &, bool);
48                 void specialize_int(const std::string &, int);
49         };
50
51         enum Stage
52         {
53                 VERTEX,
54                 GEOMETRY,
55                 FRAGMENT,
56                 MAX_STAGES
57         };
58
59         struct TransientData
60         {
61                 std::map<std::string, unsigned> textures;
62                 std::map<std::string, unsigned> blocks;
63                 std::map<unsigned, int> spec_values;
64         };
65
66         struct UniformCall
67         {
68                 using FuncPtr = void (*)(unsigned, unsigned, const void *);
69
70                 unsigned location;
71                 unsigned size;
72                 FuncPtr func;
73
74                 UniformCall(unsigned l, unsigned s, FuncPtr f): location(l), size(s), func(f) { }
75         };
76
77         unsigned id;
78         unsigned stage_ids[MAX_STAGES];
79         bool linked;
80         ReflectData reflect_data;
81         std::vector<UniformCall> uniform_calls;
82         std::string debug_name;
83
84 public:
85         /// Constructs an empty Program with no shader stages attached.
86         Program();
87
88         /// Constructs a Program from a Module, with specialization constants.
89         Program(const Module &, const std::map<std::string, int> & = std::map<std::string, int>());
90
91 private:
92         void init();
93 public:
94         virtual ~Program();
95
96         void add_stages(const Module &, const std::map<std::string, int> & = std::map<std::string, int>());
97 private:
98         bool has_stages() const;
99         unsigned add_stage(Stage);
100         void add_glsl_stages(const GlslModule &, const std::map<std::string, int> &, TransientData &);
101         void compile_glsl_stage(const GlslModule &, unsigned);
102         void add_spirv_stages(const SpirVModule &, const std::map<std::string, int> &, TransientData &);
103
104         void finalize(const Module &, const TransientData &);
105         void query_uniforms();
106         void query_uniform_blocks(const std::vector<ReflectData::UniformInfo *> &);
107         void query_attributes();
108         void collect_uniforms(const SpirVModule &, const std::map<unsigned, int> &);
109         void collect_block_uniforms(const SpirVModule::Structure &, const std::string &, unsigned, const std::map<unsigned, int> &, std::vector<std::string> &);
110         void collect_attributes(const SpirVModule &);
111
112 public:
113         ReflectData::LayoutHash get_uniform_layout_hash() const { return reflect_data.layout_hash; }
114         const std::vector<ReflectData::UniformBlockInfo> &get_uniform_blocks() const { return reflect_data.uniform_blocks; }
115         const ReflectData::UniformBlockInfo &get_uniform_block_info(const std::string &) const;
116         const std::vector<ReflectData::UniformInfo> &get_uniforms() const { return reflect_data.uniforms; }
117         const ReflectData::UniformInfo &get_uniform_info(const std::string &) const;
118         const ReflectData::UniformInfo &get_uniform_info(Tag) const;
119         int get_uniform_location(const std::string &) const;
120         int get_uniform_location(Tag) const;
121         int get_uniform_binding(Tag) const;
122         const std::vector<ReflectData::AttributeInfo> &get_attributes() const { return reflect_data.attributes; }
123         const ReflectData::AttributeInfo &get_attribute_info(const std::string &) const;
124         int get_attribute_location(const std::string &) const;
125
126         void set_debug_name(const std::string &);
127 private:
128         void set_stage_debug_name(unsigned, Stage);
129 };
130
131 } // namespace GL
132 } // namespace Msp
133
134 #endif