5 #include "deviceinfo.h"
10 #include "programdata.h"
11 #include "renderable.h"
15 #include "texturing.h"
17 #include "vertexarray.h"
18 #include "vertexsetup.h"
19 #include "windingtest.h"
32 Renderer::Renderer(const Camera *c):
43 state_stack.reserve(16);
44 state_stack.push_back(State());
45 shdata_stack.reserve(32);
46 state = &state_stack.back();
54 void Renderer::set_camera(const Camera &c)
57 changed |= CAMERA_SHDATA;
61 void Renderer::set_matrix(const Matrix &matrix)
63 state->model_matrix = matrix;
67 void Renderer::transform(const Matrix &matrix)
69 state->model_matrix *= matrix;
73 void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
75 set_texture(tag, -1, tex, samp);
78 void Renderer::set_texture(Tag tag, int unit, const Texture *tex, const Sampler *samp)
80 if(texture_stack.size()>state->texture_count)
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)
85 ++state->texture_count;
92 for(vector<BoundTexture>::iterator i=texture_stack.end(); i!=texture_stack.begin(); )
93 if((--i)->tag==tag && i->unit==unit)
95 i->replaced = texture_stack.size();
99 texture_stack.push_back(BoundTexture());
100 BoundTexture &bound_tex = texture_stack.back();
102 bound_tex.unit = unit;
103 bound_tex.texture = tex;
104 bound_tex.sampler = samp;
105 state->texture_count = texture_stack.size();
108 void Renderer::set_texture(const Texture *t, const Sampler *s)
110 set_texture(Tag(), 0, t, s);
113 void Renderer::flush_textures()
115 for(unsigned i=0; i<texture_stack.size(); ++i)
117 BoundTexture &bt = texture_stack[i];
118 if(i>=state->texture_count && bt.unit>=0)
120 Texture::unbind_from(bt.unit);
121 Sampler::unbind_from(bt.unit);
123 else if(bt.replaced>=static_cast<int>(state->texture_count))
127 texture_stack.erase(texture_stack.begin()+state->texture_count, texture_stack.end());
130 #pragma GCC diagnostic push
131 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
132 void Renderer::set_texturing(const Texturing *t)
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));
142 #pragma GCC diagnostic pop
144 unsigned Renderer::allocate_effect_texunit()
146 return --state->lowest_effect_texunit;
149 void Renderer::set_material(const Material *m)
152 add_shader_data(m->get_shader_data());
155 void Renderer::set_lighting(const Lighting *l)
158 add_shader_data(l->get_shader_data());
161 void Renderer::set_clipping(const Clipping *c)
164 changed |= CLIPPING_SHDATA;
167 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
174 void Renderer::add_shader_data(const ProgramData &d)
176 if(state->shdata_count<shdata_stack.size())
178 const BoundProgramData &top = shdata_stack.back();
179 if(top.shdata==&d && top.generation==d.get_generation())
181 ++state->shdata_count;
186 flush_shader_data_();
187 shdata_stack.push_back(&d);
188 state->shdata_count = shdata_stack.size();
189 changed |= SHADER_DATA;
192 void Renderer::flush_shader_data_()
194 if(shdata_stack.size()>state->shdata_count)
195 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
198 void Renderer::set_vertex_setup(const VertexSetup *vs)
200 state->vertex_setup = vs;
203 void Renderer::set_winding_test(const WindingTest *w)
205 state->winding_test = w;
208 void Renderer::set_reverse_winding(bool r)
210 state->reverse_winding = r;
213 void Renderer::set_object_lod_bias(unsigned b)
215 state->object_lod_bias = b;
218 void Renderer::push_state()
220 state_stack.push_back(state_stack.back());
221 state = &state_stack.back();
224 void Renderer::pop_state()
226 if(state_stack.size()==1)
227 throw stack_underflow("Renderer::pop_state");
229 const Camera *old_camera = state->camera;
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->clipping!=old_clipping)
238 changed |= CLIPPING_SHDATA;
243 if(state_stack.size()>1)
244 throw invalid_operation("Renderer::end");
248 set_camera(*default_camera);
249 shdata_stack.clear();
252 for(vector<BoundTexture>::iterator i=texture_stack.begin(); i!=texture_stack.end(); ++i)
255 Texture::unbind_from(i->unit);
256 Sampler::unbind_from(i->unit);
260 VertexSetup::unbind();
261 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
262 WindingTest::unbind();
265 void Renderer::exclude(const Renderable &renderable)
267 excluded.insert(&renderable);
270 void Renderer::include(const Renderable &renderable)
272 excluded.erase(&renderable);
275 void Renderer::render(const Renderable &renderable, Tag tag)
277 if(!excluded.count(&renderable))
278 renderable.render(*this, tag);
281 void Renderer::draw(const Batch &batch)
288 void Renderer::draw_instanced(const Batch &batch, unsigned count)
292 batch.draw_instanced(count);
295 void Renderer::apply_state()
298 throw invalid_operation("Renderer::apply_state");
300 /* We (mostly) let the objects themselves figure out if the binding has
303 if(state->texture_count<texture_stack.size())
306 for(vector<BoundTexture>::const_iterator i=texture_stack.begin(); i!=texture_stack.end(); ++i)
308 int unit = (i->tag.id ? state->shprog->get_uniform_binding(i->tag) : i->unit);
312 i->texture->bind_to(unit);
314 i->sampler->bind_to(unit);
320 state->clipping->bind();
324 bool shprog_changed = (state->shprog!=Program::current());
325 state->shprog->bind();
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;
336 if(state->camera && ((changed&CAMERA_SHDATA) || shprog_changed))
338 state->camera->get_shader_data().apply();
339 changed &= ~CAMERA_SHDATA;
342 if(state->clipping && ((changed&CLIPPING_SHDATA) || shprog_changed))
344 state->clipping->get_shader_data().apply();
345 changed &= ~CLIPPING_SHDATA;
348 if((changed&STANDARD_SHDATA) || shprog_changed)
350 standard_shdata.apply();
351 changed &= ~STANDARD_SHDATA;
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);
359 if(shdata_changed || shprog_changed || 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)
366 i->generation = i->shdata->get_generation();
368 changed &= ~SHADER_DATA;
371 if(state->vertex_setup)
372 state->vertex_setup->bind();
374 VertexSetup::unbind();
376 if(state->winding_test)
378 if(state->reverse_winding)
379 state->winding_test->get_reverse().bind();
381 state->winding_test->bind();
384 WindingTest::unbind();
388 Renderer::BoundTexture::BoundTexture():
396 Renderer::BoundProgramData::BoundProgramData(const ProgramData *d):
402 Renderer::State::State():
405 lowest_effect_texunit(Limits::get_global().max_texture_bindings),
411 reverse_winding(false),