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