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