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