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();
53 void Renderer::set_camera(const Camera &c)
56 changed |= CAMERA_SHDATA;
60 void Renderer::set_matrix(const Matrix &matrix)
62 state->model_matrix = matrix;
66 void Renderer::transform(const Matrix &matrix)
68 state->model_matrix *= matrix;
72 void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
74 set_texture(tag, -1, tex, samp);
77 void Renderer::set_texture(Tag tag, int unit, const Texture *tex, const Sampler *samp)
79 if(texture_stack.size()>state->texture_count)
81 BoundTexture &bt = texture_stack[state->texture_count];
82 if((!tag.id || bt.tag==tag) && (unit<0 || bt.unit==unit) && bt.texture==tex && bt.sampler==samp)
84 ++state->texture_count;
91 for(vector<BoundTexture>::iterator i=texture_stack.end(); i!=texture_stack.begin(); )
92 if((--i)->tag==tag && i->unit==unit)
94 i->replaced = texture_stack.size();
98 texture_stack.push_back(BoundTexture());
99 BoundTexture &bound_tex = texture_stack.back();
101 bound_tex.unit = unit;
102 bound_tex.texture = tex;
103 bound_tex.sampler = samp;
104 state->texture_count = texture_stack.size();
107 void Renderer::set_texture(const Texture *t, const Sampler *s)
109 set_texture(Tag(), 0, t, s);
112 void Renderer::flush_textures()
114 for(unsigned i=0; i<texture_stack.size(); ++i)
116 BoundTexture &bt = texture_stack[i];
117 if(i>=state->texture_count && bt.unit>=0)
119 Texture::unbind_from(bt.unit);
120 Sampler::unbind_from(bt.unit);
122 else if(bt.replaced>=static_cast<int>(state->texture_count))
126 texture_stack.erase(texture_stack.begin()+state->texture_count, texture_stack.end());
129 #pragma GCC diagnostic push
130 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
131 void Renderer::set_texturing(const Texturing *t)
135 unsigned n_units = TexUnit::get_n_units();
136 for(unsigned i=0; i<n_units; ++i)
137 if(const Texture *tex = t->get_attached_texture(i))
138 set_texture(Tag(), i, tex, t->get_attached_sampler(i));
141 #pragma GCC diagnostic pop
143 unsigned Renderer::allocate_effect_texunit()
145 return --state->lowest_effect_texunit;
148 void Renderer::set_material(const Material *m)
151 add_shader_data(m->get_shader_data());
154 void Renderer::set_lighting(const Lighting *l)
157 add_shader_data(l->get_shader_data());
160 void Renderer::set_clipping(const Clipping *c)
163 changed |= CLIPPING_SHDATA;
166 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
173 void Renderer::add_shader_data(const ProgramData &d)
175 if(state->shdata_count<shdata_stack.size())
177 const BoundProgramData &top = shdata_stack.back();
178 if(top.shdata==&d && top.generation==d.get_generation())
180 ++state->shdata_count;
186 shdata_stack.push_back(&d);
187 state->shdata_count = shdata_stack.size();
188 changed |= SHADER_DATA;
191 void Renderer::flush_shader_data()
193 if(shdata_stack.size()>state->shdata_count)
194 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
197 void Renderer::set_vertex_setup(const VertexSetup *vs)
199 state->vertex_setup = vs;
202 void Renderer::set_winding_test(const WindingTest *w)
204 state->winding_test = w;
207 void Renderer::set_reverse_winding(bool r)
209 state->reverse_winding = r;
212 void Renderer::set_object_lod_bias(unsigned b)
214 state->object_lod_bias = b;
217 void Renderer::push_state()
219 state_stack.push_back(state_stack.back());
220 state = &state_stack.back();
223 void Renderer::pop_state()
225 if(state_stack.size()==1)
226 throw stack_underflow("Renderer::pop_state");
228 const Camera *old_camera = state->camera;
229 const Clipping *old_clipping = state->clipping;
230 state_stack.pop_back();
231 state = &state_stack.back();
233 bool camera_changed = (state->camera!=old_camera);
235 changed |= CAMERA_SHDATA;
236 if(state->clipping!=old_clipping)
237 changed |= CLIPPING_SHDATA;
242 if(state_stack.size()>1)
243 throw invalid_operation("Renderer::end");
247 set_camera(*default_camera);
248 shdata_stack.clear();
251 for(vector<BoundTexture>::iterator i=texture_stack.begin(); i!=texture_stack.end(); ++i)
254 Texture::unbind_from(i->unit);
255 Sampler::unbind_from(i->unit);
259 VertexSetup::unbind();
260 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
261 WindingTest::unbind();
264 void Renderer::exclude(const Renderable &renderable)
266 excluded.insert(&renderable);
269 void Renderer::include(const Renderable &renderable)
271 excluded.erase(&renderable);
274 void Renderer::render(const Renderable &renderable, Tag tag)
276 if(!excluded.count(&renderable))
277 renderable.render(*this, tag);
280 void Renderer::draw(const Batch &batch)
287 void Renderer::draw_instanced(const Batch &batch, unsigned count)
291 batch.draw_instanced(count);
294 void Renderer::apply_state()
297 throw invalid_operation("Renderer::apply_state");
299 /* We (mostly) let the objects themselves figure out if the binding has
302 if(state->texture_count<texture_stack.size())
305 for(vector<BoundTexture>::const_iterator i=texture_stack.begin(); i!=texture_stack.end(); ++i)
307 int unit = (i->tag.id ? state->shprog->get_uniform_binding(i->tag) : i->unit);
311 i->texture->bind_to(unit);
313 i->sampler->bind_to(unit);
319 state->clipping->bind();
323 bool shprog_changed = (state->shprog!=Program::current());
324 state->shprog->bind();
328 standard_shdata.uniform("world_obj_matrix", state->model_matrix);
329 LinAl::SquareMatrix<float, 3> nm = state->model_matrix.block<3, 3>(0, 0);
330 nm = transpose(invert(nm));
331 standard_shdata.uniform("world_obj_normal_matrix", nm);
332 changed = (changed&~MATRIX)|STANDARD_SHDATA;
335 if(state->camera && ((changed&CAMERA_SHDATA) || shprog_changed))
337 state->camera->get_shader_data().apply();
338 changed &= ~CAMERA_SHDATA;
341 if(state->clipping && ((changed&CLIPPING_SHDATA) || shprog_changed))
343 state->clipping->get_shader_data().apply();
344 changed &= ~CLIPPING_SHDATA;
347 if((changed&STANDARD_SHDATA) || shprog_changed)
349 standard_shdata.apply();
350 changed &= ~STANDARD_SHDATA;
353 bool shdata_changed = changed&SHADER_DATA;
354 for(vector<BoundProgramData>::const_iterator i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i)
355 shdata_changed = (i->shdata->get_generation()!=i->generation);
356 bool extra_shdata = (shdata_stack.size()>state->shdata_count);
358 if(shdata_changed || shprog_changed || extra_shdata)
361 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
362 for(vector<BoundProgramData>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
365 i->generation = i->shdata->get_generation();
367 changed &= ~SHADER_DATA;
370 if(state->vertex_setup)
371 state->vertex_setup->bind();
373 VertexSetup::unbind();
375 if(state->winding_test)
377 if(state->reverse_winding)
378 state->winding_test->get_reverse().bind();
380 state->winding_test->bind();
383 WindingTest::unbind();
387 Renderer::BoundTexture::BoundTexture():
395 Renderer::BoundProgramData::BoundProgramData(const ProgramData *d):
401 Renderer::State::State():
404 lowest_effect_texunit(TexUnit::get_n_units()),
410 reverse_winding(false),