5 #include "framebuffer.h"
9 #include "programdata.h"
10 #include "renderable.h"
12 #include "resourcemanager.h"
15 #include "vertexarray.h"
16 #include "vertexsetup.h"
26 state_stack.reserve(16);
27 state_stack.push_back(State());
28 shdata_stack.reserve(32);
29 state = &state_stack.back();
30 add_shader_data(standard_shdata);
38 void Renderer::set_camera(const Camera &c)
41 add_shader_data(c.get_shader_data());
45 void Renderer::set_matrix(const Matrix &matrix)
47 state->model_matrix = matrix;
51 void Renderer::transform(const Matrix &matrix)
53 state->model_matrix *= matrix;
57 void Renderer::set_framebuffer(const Framebuffer *f)
59 state->framebuffer = f;
62 void Renderer::set_viewport(const Rect *v)
67 void Renderer::set_scissor(const Rect *s)
72 void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
75 if(ResourceManager *res_mgr = tex->get_manager())
76 res_mgr->resource_used(*tex);
78 if(texture_stack.size()>state->texture_count)
80 BoundTexture &bt = texture_stack[state->texture_count];
81 if(bt.tag==tag && bt.texture==tex && bt.sampler==samp)
83 ++state->texture_count;
90 for(auto i=texture_stack.end(); i!=texture_stack.begin(); )
93 i->replaced = texture_stack.size();
97 texture_stack.push_back(BoundTexture());
98 BoundTexture &bound_tex = texture_stack.back();
100 bound_tex.texture = tex;
101 bound_tex.sampler = samp;
102 state->texture_count = texture_stack.size();
105 void Renderer::flush_textures()
107 for(unsigned i=0; i<state->texture_count; ++i)
108 if(texture_stack[i].replaced>=static_cast<int>(state->texture_count))
109 texture_stack[i].replaced = -1;
111 texture_stack.erase(texture_stack.begin()+state->texture_count, texture_stack.end());
114 void Renderer::set_material(const Material *m)
117 add_shader_data(m->get_shader_data());
120 void Renderer::set_lighting(const Lighting *l)
123 add_shader_data(l->get_shader_data());
126 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
133 void Renderer::add_shader_data(const ProgramData &d)
135 if(state->shdata_count<shdata_stack.size())
137 const BoundProgramData &top = shdata_stack.back();
138 if(top.shdata==&d && top.generation==d.get_generation())
140 ++state->shdata_count;
145 flush_shader_data_();
146 shdata_stack.push_back(&d);
147 state->shdata_count = shdata_stack.size();
148 changed |= SHADER_DATA;
151 void Renderer::flush_shader_data_()
153 if(shdata_stack.size()>state->shdata_count)
154 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
157 void Renderer::set_vertex_setup(const VertexSetup *vs)
159 state->vertex_setup = vs;
162 void Renderer::set_front_face(FaceWinding winding)
164 state->front_face = winding;
167 void Renderer::set_face_cull(CullMode cull)
169 state->face_cull = cull;
172 void Renderer::set_depth_test(const DepthTest *dt)
174 state->depth_test = dt;
177 void Renderer::set_stencil_test(const StencilTest *st)
179 state->stencil_test = st;
182 void Renderer::set_blend(const Blend *b)
187 void Renderer::set_object_lod_bias(unsigned b)
189 state->object_lod_bias = b;
192 void Renderer::push_state()
194 state_stack.push_back(state_stack.back());
195 state = &state_stack.back();
198 void Renderer::pop_state()
200 if(state_stack.size()==1)
201 throw stack_underflow("Renderer::pop_state");
203 state_stack.pop_back();
204 state = &state_stack.back();
210 if(state_stack.size()>1)
211 throw invalid_operation("Renderer::end");
214 shdata_stack.clear();
215 add_shader_data(standard_shdata);
218 commands.use_pipeline(0);
221 void Renderer::exclude(const Renderable &renderable)
223 excluded.insert(&renderable);
226 void Renderer::include(const Renderable &renderable)
228 excluded.erase(&renderable);
231 void Renderer::render(const Renderable &renderable, Tag tag)
233 if(!excluded.count(&renderable))
234 renderable.render(*this, tag);
237 void Renderer::clear(const ClearValue *values)
239 pipeline_state.set_framebuffer(state->framebuffer);
240 pipeline_state.set_viewport(state->viewport);
241 pipeline_state.set_scissor(state->scissor);
242 commands.use_pipeline(&pipeline_state);
243 commands.clear(values);
246 void Renderer::draw(const Batch &batch)
250 commands.use_pipeline(&pipeline_state);
251 commands.draw(batch);
254 void Renderer::draw_instanced(const Batch &batch, unsigned count)
258 commands.use_pipeline(&pipeline_state);
259 commands.draw_instanced(batch, count);
262 void Renderer::resolve_multisample(Framebuffer &target)
264 if(!state->framebuffer)
265 throw invalid_operation("Renderer::resolve_multisample");
267 unsigned width = state->framebuffer->get_width();
268 unsigned height = state->framebuffer->get_height();
269 if(target.get_width()!=width || target.get_height()!=height)
270 throw incompatible_data("Renderer::resolve_multisample");
272 pipeline_state.set_framebuffer(state->framebuffer);
273 commands.use_pipeline(&pipeline_state);
274 commands.resolve_multisample(target);
277 void Renderer::begin_query(const QueryPool &pool, unsigned index)
279 commands.begin_query(pool, index);
282 void Renderer::end_query(const QueryPool &pool, unsigned index)
284 commands.end_query(pool, index);
287 void Renderer::apply_state()
290 throw invalid_operation("Renderer::apply_state");
294 standard_shdata.uniform("world_obj_matrix", state->model_matrix);
295 LinAl::SquareMatrix<float, 3> nm = state->model_matrix.block<3, 3>(0, 0);
296 nm = transpose(invert(nm));
297 standard_shdata.uniform("world_obj_normal_matrix", nm);
301 pipeline_state.set_framebuffer(state->framebuffer);
302 pipeline_state.set_viewport(state->viewport);
303 pipeline_state.set_scissor(state->scissor);
305 bool shprog_changed = (state->shprog!=pipeline_state.get_shader_program());
306 pipeline_state.set_shader_program(state->shprog);
308 if(state->vertex_setup)
310 if(const VertexArray *array = state->vertex_setup->get_vertex_array())
312 if(const VertexArray *array = state->vertex_setup->get_instance_array())
315 pipeline_state.set_vertex_setup(state->vertex_setup);
317 pipeline_state.set_front_face(state->front_face);
318 pipeline_state.set_face_cull(state->face_cull);
320 if(state->texture_count<texture_stack.size())
323 for(const BoundTexture &t: texture_stack)
325 int unit = (t.tag.id ? state->shprog->get_uniform_binding(t.tag) : t.unit);
327 pipeline_state.set_texture(unit, t.texture, t.sampler);
330 bool shdata_changed = changed&SHADER_DATA;
331 for(auto i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i)
332 shdata_changed = (i->shdata->get_generation()!=i->generation);
333 bool extra_shdata = (shdata_stack.size()>state->shdata_count);
335 if(shdata_changed || shprog_changed || extra_shdata)
338 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
339 for(const BoundProgramData &d: shdata_stack)
341 d.shdata->apply(*state->shprog, pipeline_state);
342 d.generation = d.shdata->get_generation();
344 changed &= ~SHADER_DATA;
347 pipeline_state.set_depth_test(state->depth_test);
348 pipeline_state.set_stencil_test(state->stencil_test);
349 pipeline_state.set_blend(state->blend);
353 Renderer::BoundProgramData::BoundProgramData(const ProgramData *d):