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