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