]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.cpp
Track whether implicit shader data needs to be re-applied
[libs/gl.git] / source / renderer.cpp
1 #include "batch.h"
2 #include "buffer.h"
3 #include "camera.h"
4 #include "clipping.h"
5 #include "error.h"
6 #include "lighting.h"
7 #include "material.h"
8 #include "mesh.h"
9 #include "program.h"
10 #include "programdata.h"
11 #include "renderable.h"
12 #include "renderer.h"
13 #include "texture.h"
14 #include "texturing.h"
15 #include "texunit.h"
16 #include "vertexarray.h"
17 #include "windingtest.h"
18
19 using namespace std;
20
21 namespace Msp {
22 namespace GL {
23
24 Renderer::Renderer(const Camera *c):
25         mtx_stack(*this),
26         changed(0),
27         matrices_loaded(false),
28         shdata_applied(0),
29         camera(0),
30         state_stack(1)
31 {
32         state_stack.reserve(16);
33         shdata_stack.reserve(32);
34         state = &state_stack.back();
35
36         begin(c);
37 }
38
39 Renderer::~Renderer()
40 {
41         end();
42 }
43
44 void Renderer::begin(const Camera *c)
45 {
46         if(state_stack.size()>1)
47                 throw invalid_operation("Renderer::begin");
48
49         reset_state();
50         excluded.clear();
51         camera = c;
52
53         if(camera)
54         {
55                 mtx_stack.load(camera->get_matrix());
56                 standard_shdata.uniform("projection_matrix", camera->get_projection_matrix());
57         }
58         else
59         {
60                 mtx_stack.load(MatrixStack::modelview().top());
61                 standard_shdata.uniform("projection_matrix", MatrixStack::projection().top());
62         }
63 }
64
65 void Renderer::set_matrix(const Matrix &matrix)
66 {
67         mtx_stack.load(matrix);
68 }
69
70 void Renderer::transform(const Matrix &matrix)
71 {
72         mtx_stack *= matrix;
73 }
74
75 void Renderer::set_texture(const Texture *t)
76 {
77         state->texture = t;
78         state->texturing = 0;
79 }
80
81 void Renderer::set_texturing(const Texturing *t)
82 {
83         state->texturing = t;
84         state->texture = 0;
85 }
86
87 unsigned Renderer::allocate_effect_texunit()
88 {
89         return --state->lowest_effect_texunit;
90 }
91
92 void Renderer::set_material(const Material *m)
93 {
94         state->material = m;
95         changed |= MATERIAL_SHDATA;
96 }
97
98 void Renderer::set_lighting(const Lighting *l)
99 {
100         state->lighting = l;
101         state->lighting_matrix = mtx_stack.top();
102         if(l)
103         {
104                 l->update_shader_data(standard_shdata, mtx_stack.top());
105                 changed |= STANDARD_SHDATA;
106         }
107         changed |= LIGHTING;
108 }
109
110 void Renderer::set_clipping(const Clipping *c)
111 {
112         state->clipping = c;
113         state->clipping_matrix = mtx_stack.top();
114         if(c)
115                 c->update_shader_data(standard_shdata, mtx_stack.top());
116         changed |= CLIPPING;
117 }
118
119 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
120 {
121         state->shprog = p;
122         if(p && d)
123                 add_shader_data(*d);
124 }
125
126 void Renderer::add_shader_data(const ProgramData &d)
127 {
128         shdata_stack.push_back(&d);
129         state->shdata_count = shdata_stack.size();
130         changed |= SHADER_DATA;
131 }
132
133 void Renderer::set_mesh(const Mesh *m)
134 {
135         state->mesh = m;
136 }
137
138 void Renderer::set_winding_test(const WindingTest *w)
139 {
140         state->winding_test = w;
141 }
142
143 void Renderer::set_reverse_winding(bool r)
144 {
145         state->reverse_winding = r;
146 }
147
148 void Renderer::push_state()
149 {
150         state_stack.push_back(state_stack.back());
151         state = &state_stack.back();
152         mtx_stack.push();
153 }
154
155 void Renderer::pop_state()
156 {
157         if(state_stack.size()==1)
158                 throw stack_underflow("Renderer::pop_state");
159
160         const Lighting *old_lighting = state->lighting;
161         const Clipping *old_clipping = state->clipping;
162         state_stack.pop_back();
163         state = &state_stack.back();
164         if(shdata_stack.size()>state->shdata_count)
165         {
166                 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
167                 changed |= SHADER_DATA;
168         }
169         shdata_applied = min<unsigned>(shdata_applied, shdata_stack.size());
170         mtx_stack.pop();
171         changed |= MATRIX;
172         if(state->lighting!=old_lighting)
173         {
174                 if(state->lighting)
175                         state->lighting->update_shader_data(standard_shdata, state->lighting_matrix);
176                 changed |= LIGHTING;
177         }
178         if(state->clipping!=old_clipping)
179         {
180                 if(state->clipping)
181                         state->clipping->update_shader_data(standard_shdata, state->clipping_matrix);
182                 changed |= CLIPPING;
183         }
184 }
185
186 void Renderer::escape()
187 {
188         apply_state();
189         Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
190         matrices_loaded = false;
191 }
192
193 void Renderer::end()
194 {
195         if(state_stack.size()>1)
196                 throw invalid_operation("Renderer::end");
197
198         reset_state();
199
200         Mesh::unbind();
201         Texturing::unbind();
202         Texture::unbind_from(0);
203         Material::unbind();
204         Lighting::unbind();
205         Clipping::unbind();
206         Program::unbind();
207         Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
208         WindingTest::unbind();
209 }
210
211 void Renderer::exclude(const Renderable &renderable)
212 {
213         excluded.insert(&renderable);
214 }
215
216 void Renderer::include(const Renderable &renderable)
217 {
218         excluded.erase(&renderable);
219 }
220
221 void Renderer::render(const Renderable &renderable, const Tag &tag)
222 {
223         if(!excluded.count(&renderable))
224                 renderable.render(*this, tag);
225 }
226
227 void Renderer::draw(const Batch &batch)
228 {
229         apply_state();
230
231         bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
232         if(state->mesh && legacy_bindings)
233         {
234                 if(const Buffer *ibuf = state->mesh->get_index_buffer())
235                         ibuf->bind_to(ELEMENT_ARRAY_BUFFER);
236                 else
237                         Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
238         }
239
240         batch.draw();
241 }
242
243 void Renderer::apply_state()
244 {
245         /* We (mostly) let the objects themselves figure out if the binding has
246         changed */
247
248         bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
249
250         if(state->texturing)
251                 state->texturing->bind();
252         else
253         {
254                 Texturing::unbind();
255                 if(state->texture)
256                         state->texture->bind_to(0);
257                 else
258                         Texture::unbind_from(0);
259         }
260
261         if(legacy_bindings)
262         {
263                 if(state->material)
264                         state->material->bind();
265                 else
266                         Material::unbind();
267
268                 if(changed&LIGHTING)
269                 {
270                         if(state->lighting)
271                         {
272                                 MatrixStack::modelview() = state->lighting_matrix;
273                                 state->lighting->bind();
274                                 changed = (changed&~LIGHTING)|LEGACY_MATRIX;
275                         }
276                         else
277                                 Lighting::unbind();
278                 }
279         }
280
281         if(state->clipping)
282         {
283                 if(legacy_bindings)
284                 {
285                         if(changed&CLIPPING)
286                         {
287                                 MatrixStack::modelview() = state->clipping_matrix;
288                                 state->clipping->bind(true);
289                                 changed = (changed&~CLIPPING)|LEGACY_MATRIX;
290                         }
291                 }
292                 else
293                         state->clipping->bind(false);
294         }
295         else
296                 Clipping::unbind();
297
298         if(state->shprog)
299         {
300                 bool shprog_changed = (state->shprog!=Program::current());
301                 state->shprog->bind();
302
303                 if(!legacy_bindings)
304                 {
305                         if(changed&MODERN_MATRIX)
306                         {
307                                 const Matrix &m = mtx_stack.top();
308                                 standard_shdata.uniform("eye_obj_matrix", mtx_stack.top());
309                                 LinAl::SquareMatrix<float, 3> nm = m.block<3, 3>(0, 0);
310                                 nm = transpose(invert(nm));
311                                 standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
312                                 changed = (changed&~MODERN_MATRIX)|STANDARD_SHDATA;
313                         }
314
315                         if(state->material && (changed&MATERIAL_SHDATA))
316                         {
317                                 state->material->get_shader_data().apply();
318                                 changed &= ~MATERIAL_SHDATA;
319                         }
320
321                         if(changed&STANDARD_SHDATA)
322                         {
323                                 standard_shdata.apply();
324                                 changed &= ~STANDARD_SHDATA;
325                         }
326                 }
327
328                 if((changed&SHADER_DATA) || shprog_changed)
329                 {
330                         vector<const ProgramData *>::const_iterator i = shdata_stack.begin();
331                         if(!shprog_changed)
332                                 i += shdata_applied;
333                         for(; i!=shdata_stack.end(); ++i)
334                                 (*i)->apply();
335                         changed &= ~SHADER_DATA;
336                         shdata_applied = shdata_stack.size();
337                 }
338         }
339         else
340                 Program::unbind();
341
342         if(state->mesh)
343         {
344                 if(legacy_bindings)
345                 {
346                         Mesh::unbind();
347                         state->mesh->get_vertices().apply();
348                 }
349                 else
350                         state->mesh->bind();
351         }
352         else
353                 Mesh::unbind();
354
355         if(state->winding_test)
356         {
357                 if(state->reverse_winding)
358                         state->winding_test->get_reverse().bind();
359                 else
360                         state->winding_test->bind();
361         }
362         else
363                 WindingTest::unbind();
364
365         if(legacy_bindings)
366         {
367                 if(!matrices_loaded)
368                 {
369                         MatrixStack::modelview().push();
370                         if(camera)
371                         {
372                                 MatrixStack::projection().push();
373                                 camera->apply();
374                         }
375                         matrices_loaded = true;
376                 }
377
378                 if(changed&LEGACY_MATRIX)
379                 {
380                         MatrixStack::modelview() = mtx_stack.top();
381                         changed &= ~LEGACY_MATRIX;
382                 }
383         }
384 }
385
386 void Renderer::reset_state()
387 {
388         if(!matrices_loaded)
389                 return;
390
391         if(camera)
392                 MatrixStack::projection().pop();
393         MatrixStack::modelview().pop();
394         matrices_loaded = false;
395         changed |= MATRIX;
396
397         *state = State();
398 }
399
400
401 Renderer::State::State():
402         texture(0),
403         texturing(0),
404         lowest_effect_texunit(TexUnit::get_n_units()),
405         material(0),
406         lighting(0),
407         clipping(0),
408         shprog(0),
409         shdata_count(0),
410         mesh(0),
411         winding_test(0),
412         reverse_winding(false)
413 { }
414
415
416 Renderer::MtxStack::MtxStack(Renderer &r):
417         renderer(r)
418 { }
419
420 void Renderer::MtxStack::update()
421 {
422         renderer.changed |= MATRIX;
423 }
424
425 } // namespace GL
426 } // namespace Msp