5 #include "deviceinfo.h"
10 #include "programdata.h"
11 #include "renderable.h"
16 #include "vertexarray.h"
17 #include "vertexsetup.h"
18 #include "windingtest.h"
27 state_stack.reserve(16);
28 state_stack.push_back(State());
29 shdata_stack.reserve(32);
30 state = &state_stack.back();
31 add_shader_data(standard_shdata);
39 void Renderer::set_camera(const Camera &c)
42 add_shader_data(c.get_shader_data());
46 void Renderer::set_matrix(const Matrix &matrix)
48 state->model_matrix = matrix;
52 void Renderer::transform(const Matrix &matrix)
54 state->model_matrix *= matrix;
58 void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
60 if(texture_stack.size()>state->texture_count)
62 BoundTexture &bt = texture_stack[state->texture_count];
63 if(bt.tag==tag && bt.texture==tex && bt.sampler==samp)
65 ++state->texture_count;
72 for(vector<BoundTexture>::iterator i=texture_stack.end(); i!=texture_stack.begin(); )
75 i->replaced = texture_stack.size();
79 texture_stack.push_back(BoundTexture());
80 BoundTexture &bound_tex = texture_stack.back();
82 bound_tex.texture = tex;
83 bound_tex.sampler = samp;
84 state->texture_count = texture_stack.size();
87 void Renderer::flush_textures()
89 for(unsigned i=0; i<texture_stack.size(); ++i)
91 BoundTexture &bt = texture_stack[i];
92 if(i>=state->texture_count && bt.unit>=0)
94 Texture::unbind_from(bt.unit);
95 Sampler::unbind_from(bt.unit);
97 else if(bt.replaced>=static_cast<int>(state->texture_count))
101 texture_stack.erase(texture_stack.begin()+state->texture_count, texture_stack.end());
104 void Renderer::set_material(const Material *m)
107 add_shader_data(m->get_shader_data());
110 void Renderer::set_lighting(const Lighting *l)
113 add_shader_data(l->get_shader_data());
116 void Renderer::set_clipping(const Clipping *c)
120 add_shader_data(c->get_shader_data());
123 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
130 void Renderer::add_shader_data(const ProgramData &d)
132 if(state->shdata_count<shdata_stack.size())
134 const BoundProgramData &top = shdata_stack.back();
135 if(top.shdata==&d && top.generation==d.get_generation())
137 ++state->shdata_count;
142 flush_shader_data_();
143 shdata_stack.push_back(&d);
144 state->shdata_count = shdata_stack.size();
145 changed |= SHADER_DATA;
148 void Renderer::flush_shader_data_()
150 if(shdata_stack.size()>state->shdata_count)
151 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
154 void Renderer::set_vertex_setup(const VertexSetup *vs)
156 state->vertex_setup = vs;
159 void Renderer::set_winding_test(const WindingTest *w)
161 state->winding_test = w;
164 void Renderer::set_reverse_winding(bool r)
166 state->reverse_winding = r;
169 void Renderer::set_object_lod_bias(unsigned b)
171 state->object_lod_bias = b;
174 void Renderer::push_state()
176 state_stack.push_back(state_stack.back());
177 state = &state_stack.back();
180 void Renderer::pop_state()
182 if(state_stack.size()==1)
183 throw stack_underflow("Renderer::pop_state");
185 state_stack.pop_back();
186 state = &state_stack.back();
192 if(state_stack.size()>1)
193 throw invalid_operation("Renderer::end");
196 shdata_stack.clear();
199 for(vector<BoundTexture>::iterator i=texture_stack.begin(); i!=texture_stack.end(); ++i)
202 Texture::unbind_from(i->unit);
203 Sampler::unbind_from(i->unit);
207 VertexSetup::unbind();
208 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
209 WindingTest::unbind();
212 void Renderer::exclude(const Renderable &renderable)
214 excluded.insert(&renderable);
217 void Renderer::include(const Renderable &renderable)
219 excluded.erase(&renderable);
222 void Renderer::render(const Renderable &renderable, Tag tag)
224 if(!excluded.count(&renderable))
225 renderable.render(*this, tag);
228 void Renderer::draw(const Batch &batch)
235 void Renderer::draw_instanced(const Batch &batch, unsigned count)
239 batch.draw_instanced(count);
242 void Renderer::apply_state()
245 throw invalid_operation("Renderer::apply_state");
247 /* We (mostly) let the objects themselves figure out if the binding has
250 if(state->texture_count<texture_stack.size())
253 for(vector<BoundTexture>::const_iterator i=texture_stack.begin(); i!=texture_stack.end(); ++i)
255 int unit = state->shprog->get_uniform_binding(i->tag);
259 i->texture->bind_to(unit);
261 i->sampler->bind_to(unit);
267 state->clipping->bind();
271 bool shprog_changed = (state->shprog!=Program::current());
272 state->shprog->bind();
276 standard_shdata.uniform("world_obj_matrix", state->model_matrix);
277 LinAl::SquareMatrix<float, 3> nm = state->model_matrix.block<3, 3>(0, 0);
278 nm = transpose(invert(nm));
279 standard_shdata.uniform("world_obj_normal_matrix", nm);
283 bool shdata_changed = changed&SHADER_DATA;
284 for(vector<BoundProgramData>::const_iterator i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i)
285 shdata_changed = (i->shdata->get_generation()!=i->generation);
286 bool extra_shdata = (shdata_stack.size()>state->shdata_count);
288 if(shdata_changed || shprog_changed || extra_shdata)
291 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
292 for(vector<BoundProgramData>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
295 i->generation = i->shdata->get_generation();
297 changed &= ~SHADER_DATA;
300 if(state->vertex_setup)
301 state->vertex_setup->bind();
303 VertexSetup::unbind();
305 if(state->winding_test)
307 if(state->reverse_winding)
308 state->winding_test->get_reverse().bind();
310 state->winding_test->bind();
313 WindingTest::unbind();
317 Renderer::BoundTexture::BoundTexture():
325 Renderer::BoundProgramData::BoundProgramData(const ProgramData *d):
331 Renderer::State::State():
334 lowest_effect_texunit(Limits::get_global().max_texture_bindings),
340 reverse_winding(false),