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