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