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