1 #ifndef MSP_GL_PROGRAMDATA_H_
2 #define MSP_GL_PROGRAMDATA_H_
5 #include <msp/datafile/objectloader.h>
20 Stores uniform variables for shader programs. The uniforms are stored in a
21 program-independent way, and UniformBlocks are created to match the uniform
22 layouts of different programs. If multiple programs have the same layout, the
23 same block is used for them.
25 The class is optimized for an access pattern where the set of uniforms and
26 programs stays constants, with only the values changing.
31 class Loader: public DataFile::ObjectLoader<ProgramData>
34 Loader(ProgramData &);
36 void uniform1i(const std::string &, int);
37 void uniform1f(const std::string &, float);
38 void uniform2i(const std::string &, int, int);
39 void uniform2f(const std::string &, float, float);
40 void uniform3i(const std::string &, int, int, int);
41 void uniform3f(const std::string &, float, float, float);
42 void uniform4i(const std::string &, int, int, int, int);
43 void uniform4f(const std::string &, float, float, float, float);
44 void uniform_array_(const std::string &, DataType, unsigned);
45 void uniform1i_array(const std::string &);
46 void uniform1f_array(const std::string &);
47 void uniform2i_array(const std::string &);
48 void uniform2f_array(const std::string &);
49 void uniform3i_array(const std::string &);
50 void uniform3f_array(const std::string &);
51 void uniform4i_array(const std::string &);
52 void uniform4f_array(const std::string &);
53 void uniform_array(const std::string &);
57 class ArrayLoader: public DataFile::Loader
61 unsigned element_size;
62 std::vector<char> data;
65 ArrayLoader(DataType, unsigned);
67 DataType get_data_type() const { return type; }
68 unsigned get_element_size() const { return element_size; }
69 const void *get_data() const { return &data[0]; }
70 unsigned get_size() const { return data.size()/(4*element_size); }
73 void uniform(DataType, unsigned, const void *);
75 void uniform1f(float);
76 void uniform2i(int, int);
77 void uniform2f(float, float);
78 void uniform3i(int, int, int);
79 void uniform3f(float, float, float);
80 void uniform4i(int, int, int, int);
81 void uniform4f(float, float, float, float);
84 typedef unsigned Mask;
88 MASK_BITS = sizeof(Mask)*8,
89 ALL_ONES = static_cast<Mask>(-1)
99 SharedBlock(unsigned, UniformBlock *);
109 ProgramBlock(int, SharedBlock *);
112 struct ProgramUniforms
114 std::vector<ProgramBlock> blocks;
121 typedef std::map<std::string, unsigned> SlotMap;
122 typedef std::map<Program::LayoutHash, SharedBlock> BlockMap;
123 typedef std::map<Program::LayoutHash, ProgramUniforms> ProgramMap;
125 // XXX All these mutables are a bit silly, but I'm out of better ideas
126 const Program *tied_program;
127 SlotMap uniform_slots;
128 std::vector<Uniform *> uniforms;
129 mutable BlockMap blocks;
130 mutable ProgramMap programs;
131 mutable UniformBlock *last_block;
132 mutable Buffer *buffer;
133 mutable unsigned dirty;
136 ProgramData(const Program * = 0);
137 ProgramData(const ProgramData &);
138 ProgramData &operator=(const ProgramData &);
142 void uniform(const std::string &, Uniform *);
144 void uniform(const std::string &, const Uniform &);
145 void uniform(const std::string &, int);
146 void uniform(const std::string &, float);
147 void uniform(const std::string &, int, int);
148 void uniform(const std::string &, float, float);
149 void uniform2(const std::string &, const int *);
150 void uniform2(const std::string &, const float *);
151 void uniform(const std::string &, int, int, int);
152 void uniform(const std::string &, float, float, float);
153 void uniform(const std::string &, const Vector3 &);
154 void uniform3(const std::string &, const int *);
155 void uniform3(const std::string &, const float *);
156 void uniform(const std::string &, int, int, int, int);
157 void uniform(const std::string &, float, float, float, float);
158 void uniform(const std::string &, const Vector4 &);
159 void uniform(const std::string &, const Color &);
160 void uniform4(const std::string &, const int *);
161 void uniform4(const std::string &, const float *);
162 void uniform(const std::string &, const LinAl::Matrix<float, 2, 2> &);
163 void uniform_matrix2(const std::string &, const float *);
164 void uniform(const std::string &, const LinAl::Matrix<float, 3, 3> &);
165 void uniform_matrix3(const std::string &, const float *);
166 void uniform(const std::string &, const Matrix &);
167 void uniform_matrix4(const std::string &, const float *);
168 void uniform1_array(const std::string &, unsigned, const int *);
169 void uniform1_array(const std::string &, unsigned, const float *);
170 void uniform2_array(const std::string &, unsigned, const int *);
171 void uniform2_array(const std::string &, unsigned, const float *);
172 void uniform3_array(const std::string &, unsigned, const int *);
173 void uniform3_array(const std::string &, unsigned, const float *);
174 void uniform4_array(const std::string &, unsigned, const int *);
175 void uniform4_array(const std::string &, unsigned, const float *);
176 void uniform_matrix2_array(const std::string &, unsigned, const float *);
177 void uniform_matrix3_array(const std::string &, unsigned, const float *);
178 void uniform_matrix4_array(const std::string &, unsigned, const float *);
179 void remove_uniform(const std::string &);
181 std::vector<std::string> get_uniform_names() const;
182 const Uniform &get_uniform(const std::string &) const;
185 unsigned compute_slot_mask(const Program::UniformBlockInfo &) const;
186 void update_block(UniformBlock &, const Program::UniformBlockInfo &) const;
187 SharedBlock *get_shared_block(const Program::UniformBlockInfo &) const;
190 /** Applies uniform blocks for the currently bound program, creating them