9 #include "programdata.h"
10 #include "renderable.h"
14 #include "texturing.h"
16 #include "vertexarray.h"
17 #include "vertexsetup.h"
18 #include "windingtest.h"
31 Renderer::Renderer(const Camera *c):
42 state_stack.reserve(16);
43 state_stack.push_back(State());
44 shdata_stack.reserve(32);
45 state = &state_stack.back();
47 standard_shdata.uniform("clip_eye_matrix", Matrix());
48 standard_shdata.uniform("eye_world_matrix", Matrix());
56 void Renderer::set_camera(const Camera &c)
59 standard_shdata.uniform("clip_eye_matrix", state->camera->get_projection_matrix());
60 standard_shdata.uniform("eye_world_matrix", state->camera->get_view_matrix());
61 changed |= STANDARD_SHDATA;
65 void Renderer::set_matrix(const Matrix &matrix)
67 state->model_matrix = matrix;
71 void Renderer::transform(const Matrix &matrix)
73 state->model_matrix *= matrix;
77 void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
79 set_texture(tag, -1, tex, samp);
82 void Renderer::set_texture(Tag tag, int unit, const Texture *tex, const Sampler *samp)
84 if(texture_stack.size()>state->texture_count)
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)
89 ++state->texture_count;
96 for(vector<BoundTexture>::iterator i=texture_stack.end(); i!=texture_stack.begin(); )
97 if((--i)->tag==tag && i->unit==unit)
99 i->replaced = texture_stack.size();
103 texture_stack.push_back(BoundTexture());
104 BoundTexture &bound_tex = texture_stack.back();
106 bound_tex.unit = unit;
107 bound_tex.texture = tex;
108 bound_tex.sampler = samp;
109 state->texture_count = texture_stack.size();
112 void Renderer::set_texture(const Texture *t, const Sampler *s)
114 set_texture(Tag(), 0, t, s);
117 void Renderer::flush_textures()
119 for(unsigned i=0; i<texture_stack.size(); ++i)
121 BoundTexture &bt = texture_stack[i];
122 if(i>=state->texture_count && bt.unit>=0)
124 Texture::unbind_from(bt.unit);
125 Sampler::unbind_from(bt.unit);
127 else if(bt.replaced>=static_cast<int>(state->texture_count))
131 texture_stack.erase(texture_stack.begin()+state->texture_count, texture_stack.end());
134 #pragma GCC diagnostic push
135 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
136 void Renderer::set_texturing(const Texturing *t)
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));
146 #pragma GCC diagnostic pop
148 unsigned Renderer::allocate_effect_texunit()
150 return --state->lowest_effect_texunit;
153 void Renderer::set_material(const Material *m)
156 changed |= MATERIAL_SHDATA;
159 void Renderer::set_lighting(const Lighting *l)
164 l->update_shader_data(standard_shdata, Matrix());
165 changed |= STANDARD_SHDATA;
169 void Renderer::set_clipping(const Clipping *c)
174 c->update_shader_data(standard_shdata, Matrix());
175 changed |= STANDARD_SHDATA;
179 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
186 void Renderer::add_shader_data(const ProgramData &d)
188 if(state->shdata_count<shdata_stack.size())
190 const BoundProgramData &top = shdata_stack.back();
191 if(top.shdata==&d && top.generation==d.get_generation())
193 ++state->shdata_count;
199 shdata_stack.push_back(&d);
200 state->shdata_count = shdata_stack.size();
201 changed |= SHADER_DATA;
204 void Renderer::flush_shader_data()
206 if(shdata_stack.size()>state->shdata_count)
207 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
210 void Renderer::set_vertex_setup(const VertexSetup *vs)
212 state->vertex_setup = vs;
215 void Renderer::set_winding_test(const WindingTest *w)
217 state->winding_test = w;
220 void Renderer::set_reverse_winding(bool r)
222 state->reverse_winding = r;
225 void Renderer::set_object_lod_bias(unsigned b)
227 state->object_lod_bias = b;
230 void Renderer::push_state()
232 state_stack.push_back(state_stack.back());
233 state = &state_stack.back();
236 void Renderer::pop_state()
238 if(state_stack.size()==1)
239 throw stack_underflow("Renderer::pop_state");
241 const Camera *old_camera = state->camera;
242 const Lighting *old_lighting = state->lighting;
243 const Clipping *old_clipping = state->clipping;
244 state_stack.pop_back();
245 state = &state_stack.back();
247 bool camera_changed = (state->camera!=old_camera);
252 standard_shdata.uniform("clip_eye_matrix", state->camera->get_projection_matrix());
253 standard_shdata.uniform("eye_world_matrix", state->camera->get_view_matrix());
257 standard_shdata.uniform("clip_eye_matrix", Matrix());
258 standard_shdata.uniform("eye_world_matrix", Matrix());
260 changed |= STANDARD_SHDATA;
262 if(state->lighting!=old_lighting)
266 state->lighting->update_shader_data(standard_shdata, Matrix());
267 changed |= STANDARD_SHDATA;
270 if(state->clipping!=old_clipping)
274 state->clipping->update_shader_data(standard_shdata, Matrix());
275 changed |= STANDARD_SHDATA;
282 if(state_stack.size()>1)
283 throw invalid_operation("Renderer::end");
287 set_camera(*default_camera);
289 standard_shdata.uniform("clip_eye_matrix", Matrix());
290 shdata_stack.clear();
293 for(vector<BoundTexture>::iterator i=texture_stack.begin(); i!=texture_stack.end(); ++i)
296 Texture::unbind_from(i->unit);
297 Sampler::unbind_from(i->unit);
301 VertexSetup::unbind();
302 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
303 WindingTest::unbind();
306 void Renderer::exclude(const Renderable &renderable)
308 excluded.insert(&renderable);
311 void Renderer::include(const Renderable &renderable)
313 excluded.erase(&renderable);
316 void Renderer::render(const Renderable &renderable, Tag tag)
318 if(!excluded.count(&renderable))
319 renderable.render(*this, tag);
322 void Renderer::draw(const Batch &batch)
329 void Renderer::draw_instanced(const Batch &batch, unsigned count)
333 batch.draw_instanced(count);
336 void Renderer::apply_state()
339 throw invalid_operation("Renderer::apply_state");
341 /* We (mostly) let the objects themselves figure out if the binding has
344 if(state->texture_count<texture_stack.size())
347 for(vector<BoundTexture>::const_iterator i=texture_stack.begin(); i!=texture_stack.end(); ++i)
349 int unit = (i->tag.id ? state->shprog->get_uniform_binding(i->tag) : i->unit);
353 i->texture->bind_to(unit);
355 i->sampler->bind_to(unit);
361 state->clipping->bind();
365 bool shprog_changed = (state->shprog!=Program::current());
366 state->shprog->bind();
370 standard_shdata.uniform("world_obj_matrix", state->model_matrix);
371 LinAl::SquareMatrix<float, 3> nm = state->model_matrix.block<3, 3>(0, 0);
372 nm = transpose(invert(nm));
373 standard_shdata.uniform("world_obj_normal_matrix", nm);
374 changed = (changed&~MATRIX)|STANDARD_SHDATA;
377 if(state->material && ((changed&MATERIAL_SHDATA) || shprog_changed))
379 state->material->get_shader_data().apply();
380 changed &= ~MATERIAL_SHDATA;
383 if((changed&STANDARD_SHDATA) || shprog_changed)
385 standard_shdata.apply();
386 changed &= ~STANDARD_SHDATA;
389 bool shdata_changed = changed&SHADER_DATA;
390 for(vector<BoundProgramData>::const_iterator i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i)
391 shdata_changed = (i->shdata->get_generation()!=i->generation);
392 bool extra_shdata = (shdata_stack.size()>state->shdata_count);
394 if(shdata_changed || shprog_changed || extra_shdata)
397 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
398 for(vector<BoundProgramData>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
401 i->generation = i->shdata->get_generation();
403 changed &= ~SHADER_DATA;
406 if(state->vertex_setup)
407 state->vertex_setup->bind();
409 VertexSetup::unbind();
411 if(state->winding_test)
413 if(state->reverse_winding)
414 state->winding_test->get_reverse().bind();
416 state->winding_test->bind();
419 WindingTest::unbind();
423 Renderer::BoundTexture::BoundTexture():
431 Renderer::BoundProgramData::BoundProgramData(const ProgramData *d):
437 Renderer::State::State():
440 lowest_effect_texunit(TexUnit::get_n_units()),
448 reverse_winding(false),