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 changed |= MATERIAL_SHDATA;
154 void Renderer::set_lighting(const Lighting *l)
157 changed |= LIGHTING_SHDATA;
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 Lighting *old_lighting = state->lighting;
230 const Clipping *old_clipping = state->clipping;
231 state_stack.pop_back();
232 state = &state_stack.back();
234 bool camera_changed = (state->camera!=old_camera);
236 changed |= CAMERA_SHDATA;
237 if(state->lighting!=old_lighting)
238 changed |= LIGHTING_SHDATA;
239 if(state->clipping!=old_clipping)
240 changed |= CLIPPING_SHDATA;
245 if(state_stack.size()>1)
246 throw invalid_operation("Renderer::end");
250 set_camera(*default_camera);
251 shdata_stack.clear();
254 for(vector<BoundTexture>::iterator i=texture_stack.begin(); i!=texture_stack.end(); ++i)
257 Texture::unbind_from(i->unit);
258 Sampler::unbind_from(i->unit);
262 VertexSetup::unbind();
263 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
264 WindingTest::unbind();
267 void Renderer::exclude(const Renderable &renderable)
269 excluded.insert(&renderable);
272 void Renderer::include(const Renderable &renderable)
274 excluded.erase(&renderable);
277 void Renderer::render(const Renderable &renderable, Tag tag)
279 if(!excluded.count(&renderable))
280 renderable.render(*this, tag);
283 void Renderer::draw(const Batch &batch)
290 void Renderer::draw_instanced(const Batch &batch, unsigned count)
294 batch.draw_instanced(count);
297 void Renderer::apply_state()
300 throw invalid_operation("Renderer::apply_state");
302 /* We (mostly) let the objects themselves figure out if the binding has
305 if(state->texture_count<texture_stack.size())
308 for(vector<BoundTexture>::const_iterator i=texture_stack.begin(); i!=texture_stack.end(); ++i)
310 int unit = (i->tag.id ? state->shprog->get_uniform_binding(i->tag) : i->unit);
314 i->texture->bind_to(unit);
316 i->sampler->bind_to(unit);
322 state->clipping->bind();
326 bool shprog_changed = (state->shprog!=Program::current());
327 state->shprog->bind();
331 standard_shdata.uniform("world_obj_matrix", state->model_matrix);
332 LinAl::SquareMatrix<float, 3> nm = state->model_matrix.block<3, 3>(0, 0);
333 nm = transpose(invert(nm));
334 standard_shdata.uniform("world_obj_normal_matrix", nm);
335 changed = (changed&~MATRIX)|STANDARD_SHDATA;
338 if(state->camera && ((changed&CAMERA_SHDATA) || shprog_changed))
340 state->camera->get_shader_data().apply();
341 changed &= ~CAMERA_SHDATA;
344 if(state->material && ((changed&MATERIAL_SHDATA) || shprog_changed))
346 state->material->get_shader_data().apply();
347 changed &= ~MATERIAL_SHDATA;
350 if(state->lighting && ((changed&LIGHTING_SHDATA) || shprog_changed))
352 state->lighting->get_shader_data().apply();
353 changed &= ~LIGHTING_SHDATA;
356 if(state->clipping && ((changed&CLIPPING_SHDATA) || shprog_changed))
358 state->clipping->get_shader_data().apply();
359 changed &= ~CLIPPING_SHDATA;
362 if((changed&STANDARD_SHDATA) || shprog_changed)
364 standard_shdata.apply();
365 changed &= ~STANDARD_SHDATA;
368 bool shdata_changed = changed&SHADER_DATA;
369 for(vector<BoundProgramData>::const_iterator i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i)
370 shdata_changed = (i->shdata->get_generation()!=i->generation);
371 bool extra_shdata = (shdata_stack.size()>state->shdata_count);
373 if(shdata_changed || shprog_changed || extra_shdata)
376 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
377 for(vector<BoundProgramData>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
380 i->generation = i->shdata->get_generation();
382 changed &= ~SHADER_DATA;
385 if(state->vertex_setup)
386 state->vertex_setup->bind();
388 VertexSetup::unbind();
390 if(state->winding_test)
392 if(state->reverse_winding)
393 state->winding_test->get_reverse().bind();
395 state->winding_test->bind();
398 WindingTest::unbind();
402 Renderer::BoundTexture::BoundTexture():
410 Renderer::BoundProgramData::BoundProgramData(const ProgramData *d):
416 Renderer::State::State():
419 lowest_effect_texunit(TexUnit::get_n_units()),
427 reverse_winding(false),