]> git.tdb.fi Git - libs/gl.git/blob - source/programdata.cpp
Add integer overload of ProgramData::uniform1_array
[libs/gl.git] / source / programdata.cpp
1 #include "buffer.h"
2 #include "color.h"
3 #include "error.h"
4 #include "matrix.h"
5 #include "program.h"
6 #include "programdata.h"
7 #include "uniform.h"
8 #include "uniformblock.h"
9 #include "vector.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 ProgramData::ProgramData():
17         last_block(0),
18         buffer(0),
19         dirty(0)
20 { }
21
22 // Blocks are intentionally left uncopied
23 ProgramData::ProgramData(const ProgramData &other):
24         uniform_slots(other.uniform_slots),
25         uniforms(other.uniforms),
26         last_block(0),
27         buffer(0),
28         dirty(0)
29 {
30         for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
31                 *i = (*i)->clone();
32 }
33
34 ProgramData &ProgramData::operator=(const ProgramData &other)
35 {
36         for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
37                 delete *i;
38         uniforms.clear();
39
40         uniform_slots = other.uniform_slots;
41         for(vector<Uniform *>::const_iterator i=other.uniforms.begin(); i!=other.uniforms.end(); ++i)
42                 uniforms.push_back((*i)->clone());
43
44         for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
45                 delete i->second.block;
46         programs.clear();
47
48         last_block = 0;
49         buffer = 0;
50         dirty = 0;
51
52         return *this;
53 }
54
55 ProgramData::~ProgramData()
56 {
57         for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
58                 delete *i;
59         for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
60                 delete i->second.block;
61         delete buffer;
62 }
63
64 void ProgramData::uniform(const string &name, Uniform *uni)
65 {
66         if(name[name.size()-1]==']')
67                 throw invalid_argument("ProgramData::uniform");
68
69         SlotMap::iterator i = uniform_slots.find(name);
70         if(i!=uniform_slots.end())
71         {
72                 Uniform *&slot = uniforms[i->second];
73                 /* UniformBlock does not copy the uniforms, so existing default blocks
74                 will be left with stale pointers.  This is not a problem as long as no
75                 one stores pointers to the blocks and expects them to stay valid. */
76                 delete slot;
77                 slot = uni;
78
79                 if(i->second<MASK_BITS)
80                         dirty |= 1<<i->second;
81                 else  // Force a full update if the mask isn't wide enough
82                         dirty = ALL_ONES;
83         }
84         else
85         {
86                 uniform_slots[name] = uniforms.size();
87                 uniforms.push_back(uni);
88                 dirty = ALL_ONES;
89         }
90 }
91
92 void ProgramData::uniform(const string &name, int v)
93 {
94         uniform(name, new Uniform1i(v));
95 }
96
97 void ProgramData::uniform(const string &name, float v)
98 {
99         uniform(name, new Uniform1f(v));
100 }
101
102 void ProgramData::uniform(const string &name, float v0, float v1)
103 {
104         float va[2] = { v0, v1 };
105         uniform2(name, va);
106 }
107
108 void ProgramData::uniform2(const string &name, const float *v)
109 {
110         uniform(name, new Uniform2f(v));
111 }
112
113 void ProgramData::uniform(const string &name, float v0, float v1, float v2)
114 {
115         float va[3] = { v0, v1, v2 };
116         uniform3(name, va);
117 }
118
119 void ProgramData::uniform(const string &name, const Vector3 &v)
120 {
121         uniform(name, v.x, v.y, v.z);
122 }
123
124 void ProgramData::uniform3(const string &name, const float *v)
125 {
126         uniform(name, new Uniform3f(v));
127 }
128
129 void ProgramData::uniform(const string &name, float v0, float v1, float v2, float v3)
130 {
131         float va[4] = { v0, v1, v2, v3 };
132         uniform4(name, va);
133 }
134
135 void ProgramData::uniform(const string &name, const Vector4 &v)
136 {
137         uniform(name, v.x, v.y, v.z, v.w);
138 }
139
140 void ProgramData::uniform(const string &name, const Color &c)
141 {
142         uniform(name, c.r, c.g, c.b, c.a);
143 }
144
145 void ProgramData::uniform4(const string &name, const float *v)
146 {
147         uniform(name, new Uniform4f(v));
148 }
149
150 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 2> &m)
151 {
152         uniform_matrix2(name, &m(0, 0));
153 }
154
155 void ProgramData::uniform_matrix2(const string &name, const float *v)
156 {
157         uniform(name, new UniformMatrix2x2f(v));
158 }
159
160 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 3> &m)
161 {
162         uniform_matrix3(name, &m(0, 0));
163 }
164
165 void ProgramData::uniform_matrix3(const string &name, const float *v)
166 {
167         uniform(name, new UniformMatrix3x3f(v));
168 }
169
170 void ProgramData::uniform(const string &name, const Matrix &m)
171 {
172         uniform_matrix4(name, m.data());
173 }
174
175 void ProgramData::uniform_matrix4(const string &name, const float *v)
176 {
177         uniform(name, new UniformMatrix4x4f(v));
178 }
179
180 void ProgramData::uniform1_array(const string &name, unsigned n, const int *v)
181 {
182         uniform(name, new UniformArray<Uniform1i>(n, v));
183 }
184
185 void ProgramData::uniform1_array(const string &name, unsigned n, const float *v)
186 {
187         uniform(name, new UniformArray<Uniform1f>(n, v));
188 }
189
190 void ProgramData::uniform2_array(const string &name, unsigned n, const float *v)
191 {
192         uniform(name, new UniformArray<Uniform2f>(n, v));
193 }
194
195 void ProgramData::uniform3_array(const string &name, unsigned n, const float *v)
196 {
197         uniform(name, new UniformArray<Uniform3f>(n, v));
198 }
199
200 void ProgramData::uniform4_array(const string &name, unsigned n, const float *v)
201 {
202         uniform(name, new UniformArray<Uniform4f>(n, v));
203 }
204
205 void ProgramData::uniform_matrix4_array(const string &name, unsigned n, const float *v)
206 {
207         uniform(name, new UniformArray<UniformMatrix4x4f>(n, v));
208 }
209
210 unsigned ProgramData::compute_slot_mask(const Program::UniformBlockInfo &block) const
211 {
212         unsigned mask = 0;
213         for(vector<const Program::UniformInfo *>::const_iterator i=block.uniforms.begin(); i!=block.uniforms.end(); ++i)
214         {
215                 SlotMap::const_iterator j = uniform_slots.find((*i)->name);
216                 if(j!=uniform_slots.end() && j->second<MASK_BITS)
217                         mask |= 1<<j->second;
218         }
219
220         return mask;
221 }
222
223 void ProgramData::update_block(UniformBlock &block, const Program::UniformBlockInfo &info) const
224 {
225         for(vector<const Program::UniformInfo *>::const_iterator i=info.uniforms.begin(); i!=info.uniforms.end(); ++i)
226         {
227                 SlotMap::const_iterator j = uniform_slots.find((*i)->name);
228                 if(j!=uniform_slots.end())
229                         block.attach(**i, *uniforms[j->second]);
230         }
231 }
232
233 ProgramData::SharedBlock *ProgramData::get_shared_block(const Program::UniformBlockInfo &info) const
234 {
235         BlockMap::iterator i = blocks.find(info.layout_hash);
236         if(i==blocks.end())
237         {
238                 unsigned used = compute_slot_mask(info);
239                 if(!used)
240                         return 0;
241
242                 UniformBlock *block;
243                 if(info.bind_point>=0)
244                 {
245                         if(!buffer)
246                         {
247                                 buffer = new Buffer(UNIFORM_BUFFER);
248                                 buffer->set_usage(STREAM_DRAW);
249                         }
250
251                         block = new UniformBlock(info.data_size);
252                         block->use_buffer(buffer, last_block);
253                         last_block = block;
254                 }
255                 else
256                         block = new UniformBlock;
257
258                 i = blocks.insert(BlockMap::value_type(info.layout_hash, SharedBlock(used, block))).first;
259         }
260
261         return &i->second;
262 }
263
264 void ProgramData::apply() const
265 {
266         const Program *prog = Program::current();
267         if(!prog)
268                 throw invalid_operation("ProgramData::apply");
269
270         Program::LayoutHash layout = prog->get_uniform_layout_hash();
271         ProgramUniforms &pu = programs[layout];
272
273         Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
274         Mask affected = (dirty&pu.used) | force_dirty;
275         if(affected|pu.dirty)
276         {
277                 /* If the global dirty flag affects this program, add it to per-program
278                 dirty flags and clear the global flag.  A previously unseen program will
279                 always cause this to happen. */
280                 if(affected)
281                 {
282                         for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
283                                 i->second.dirty |= (dirty&i->second.used) | force_dirty;
284                         for(ProgramMap::iterator i=programs.begin(); i!=programs.end(); ++i)
285                                 i->second.dirty |= (dirty&i->second.used) | force_dirty;
286                         dirty = 0;
287                 }
288
289                 const Program::UniformBlockMap &prog_blocks = prog->get_uniform_blocks();
290
291                 if(pu.dirty==ALL_ONES)
292                 {
293                         /* The set of uniforms has changed since this program was last used.
294                         Regenerate the list of uniform blocks. */
295                         pu.blocks.clear();
296                         pu.blocks.reserve(prog_blocks.size());
297
298                         pu.used = 0;
299                         for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i)
300                         {
301                                 SharedBlock *shared = get_shared_block(i->second);
302                                 if(shared)
303                                 {
304                                         if(shared->dirty==ALL_ONES)
305                                                 shared->used = compute_slot_mask(i->second);
306                                         pu.used |= shared->used;
307                                 }
308
309                                 pu.blocks.push_back(ProgramBlock(i->second.bind_point, shared));
310                         }
311                 }
312
313                 // Update the contents of all dirty blocks.
314                 bool buffered_blocks_updated = false;
315                 std::vector<ProgramBlock>::iterator j = pu.blocks.begin();
316                 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i, ++j)
317                 {
318                         if(!j->shared || !j->shared->dirty)
319                                 continue;
320
321                         update_block(*j->block, i->second);
322                         j->shared->dirty = 0;
323                         buffered_blocks_updated |= (j->bind_point>=0);
324                 }
325
326                 pu.dirty = 0;
327
328                 /* If any blocks stored in the buffer were updated, bind the buffer here
329                 to avoid state thrashing. */
330                 if(buffered_blocks_updated)
331                         buffer->bind();
332         }
333
334         for(vector<ProgramBlock>::iterator i=pu.blocks.begin(); i!=pu.blocks.end(); ++i)
335                 if(i->block)
336                         i->block->apply(i->bind_point);
337 }
338
339
340 ProgramData::SharedBlock::SharedBlock(unsigned u, UniformBlock *b):
341         used(u),
342         dirty(u),
343         block(b)
344 { }
345
346
347 ProgramData::ProgramBlock::ProgramBlock():
348         bind_point(-1),
349         block(0),
350         shared(0)
351 { }
352
353 ProgramData::ProgramBlock::ProgramBlock(int p, SharedBlock *b):
354         bind_point(p),
355         block(b ? b->block : 0),
356         shared(b)
357 { }
358
359
360 ProgramData::ProgramUniforms::ProgramUniforms():
361         used(ALL_ONES),
362         dirty(ALL_ONES)
363 { }
364
365
366 ProgramData::Loader::Loader(ProgramData &pd):
367         DataFile::ObjectLoader<ProgramData>(pd)
368 {
369         add("uniform1i", &Loader::uniform1i);
370         add("uniform1f", &Loader::uniform1f);
371         add("uniform2f", &Loader::uniform2f);
372         add("uniform3f", &Loader::uniform3f);
373         add("uniform4f", &Loader::uniform4f);
374 }
375
376 void ProgramData::Loader::uniform1i(const string &n, int v)
377 {
378         obj.uniform(n, v);
379 }
380
381 void ProgramData::Loader::uniform1f(const string &n, float v)
382 {
383         obj.uniform(n, v);
384 }
385
386 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
387 {
388         obj.uniform(n, v0, v1);
389 }
390
391 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
392 {
393         obj.uniform(n, v0, v1, v2);
394 }
395
396 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
397 {
398         obj.uniform(n, v0, v1, v2, v3);
399 }
400
401 } // namespace GL
402 } // namespace Msp