5 #include "framebuffer.h"
9 #include "programdata.h"
10 #include "renderable.h"
12 #include "resourcemanager.h"
15 #include "vertexarray.h"
16 #include "vertexsetup.h"
25 state_stack.reserve(16);
26 state_stack.push_back(State());
27 shdata_stack.reserve(32);
28 state = &state_stack.back();
29 add_shader_data(standard_shdata);
37 void Renderer::set_camera(const Camera &c)
40 add_shader_data(c.get_shader_data());
44 void Renderer::set_matrix(const Matrix &matrix)
46 state->model_matrix = matrix;
50 void Renderer::transform(const Matrix &matrix)
52 state->model_matrix *= matrix;
56 void Renderer::set_framebuffer(const Framebuffer *f)
58 state->framebuffer = f;
61 void Renderer::set_viewport(const Rect *v)
66 void Renderer::set_scissor(const Rect *s)
71 void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
74 if(ResourceManager *res_mgr = tex->get_manager())
75 res_mgr->resource_used(*tex);
77 if(texture_stack.size()>state->texture_count)
79 BoundTexture &bt = texture_stack[state->texture_count];
80 if(bt.tag==tag && bt.texture==tex && bt.sampler==samp)
82 ++state->texture_count;
89 for(auto i=texture_stack.end(); i!=texture_stack.begin(); )
92 i->replaced = texture_stack.size();
96 texture_stack.push_back(BoundTexture());
97 BoundTexture &bound_tex = texture_stack.back();
99 bound_tex.texture = tex;
100 bound_tex.sampler = samp;
101 state->texture_count = texture_stack.size();
104 void Renderer::flush_textures()
106 for(unsigned i=0; i<state->texture_count; ++i)
107 if(texture_stack[i].replaced>=static_cast<int>(state->texture_count))
108 texture_stack[i].replaced = -1;
110 texture_stack.erase(texture_stack.begin()+state->texture_count, texture_stack.end());
113 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
120 void Renderer::add_shader_data(const ProgramData &d)
122 if(state->shdata_count<shdata_stack.size())
124 const BoundProgramData &top = shdata_stack.back();
125 if(top.shdata==&d && top.generation==d.get_generation())
127 ++state->shdata_count;
133 shdata_stack.push_back(&d);
134 state->shdata_count = shdata_stack.size();
135 changed |= SHADER_DATA;
138 void Renderer::flush_shader_data()
140 if(shdata_stack.size()>state->shdata_count)
141 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
144 void Renderer::set_vertex_setup(const VertexSetup *vs)
146 state->vertex_setup = vs;
149 void Renderer::set_front_face(FaceWinding winding)
151 state->front_face = winding;
154 void Renderer::set_face_cull(CullMode cull)
156 state->face_cull = cull;
159 void Renderer::set_depth_test(const DepthTest *dt)
161 state->depth_test = dt;
164 void Renderer::set_stencil_test(const StencilTest *st)
166 state->stencil_test = st;
169 void Renderer::set_blend(const Blend *b)
174 void Renderer::set_object_lod_bias(unsigned b)
176 state->object_lod_bias = b;
179 void Renderer::push_state()
181 state_stack.push_back(state_stack.back());
182 state = &state_stack.back();
185 void Renderer::pop_state()
187 if(state_stack.size()==1)
188 throw stack_underflow("Renderer::pop_state");
190 state_stack.pop_back();
191 state = &state_stack.back();
197 if(state_stack.size()>1)
198 throw invalid_operation("Renderer::end");
201 shdata_stack.clear();
202 add_shader_data(standard_shdata);
204 commands.use_pipeline(0);
207 void Renderer::clear(const ClearValue *values)
209 pipeline_state.set_framebuffer(state->framebuffer);
210 pipeline_state.set_viewport(state->viewport);
211 pipeline_state.set_scissor(state->scissor);
212 commands.use_pipeline(&pipeline_state);
213 commands.clear(values);
216 void Renderer::draw(const Batch &batch)
220 commands.use_pipeline(&pipeline_state);
221 commands.draw(batch);
224 void Renderer::draw_instanced(const Batch &batch, unsigned count)
228 commands.use_pipeline(&pipeline_state);
229 commands.draw_instanced(batch, count);
232 void Renderer::resolve_multisample(Framebuffer &target)
234 if(!state->framebuffer)
235 throw invalid_operation("Renderer::resolve_multisample");
237 unsigned width = state->framebuffer->get_width();
238 unsigned height = state->framebuffer->get_height();
239 if(target.get_width()!=width || target.get_height()!=height)
240 throw incompatible_data("Renderer::resolve_multisample");
242 pipeline_state.set_framebuffer(state->framebuffer);
243 commands.use_pipeline(&pipeline_state);
244 commands.resolve_multisample(target);
247 void Renderer::begin_query(const QueryPool &pool, unsigned index)
249 commands.begin_query(pool, index);
252 void Renderer::end_query(const QueryPool &pool, unsigned index)
254 commands.end_query(pool, index);
257 void Renderer::apply_state()
260 throw invalid_operation("Renderer::apply_state");
264 standard_shdata.uniform("world_obj_matrix", state->model_matrix);
265 LinAl::SquareMatrix<float, 3> nm = state->model_matrix.block<3, 3>(0, 0);
266 nm = transpose(invert(nm));
267 standard_shdata.uniform("world_obj_normal_matrix", nm);
271 pipeline_state.set_framebuffer(state->framebuffer);
272 pipeline_state.set_viewport(state->viewport);
273 pipeline_state.set_scissor(state->scissor);
275 bool shprog_changed = (state->shprog!=pipeline_state.get_shader_program());
276 pipeline_state.set_shader_program(state->shprog);
278 if(state->vertex_setup)
280 if(const VertexArray *array = state->vertex_setup->get_vertex_array())
282 if(const VertexArray *array = state->vertex_setup->get_instance_array())
285 pipeline_state.set_vertex_setup(state->vertex_setup);
287 pipeline_state.set_front_face(state->front_face);
288 pipeline_state.set_face_cull(state->face_cull);
290 if(state->texture_count<texture_stack.size())
293 for(const BoundTexture &t: texture_stack)
294 if(t.texture && t.replaced<0)
296 if(t.binding<0 || shprog_changed)
297 t.binding = state->shprog->get_uniform_binding(t.tag);
299 pipeline_state.set_texture(t.binding, t.texture, t.sampler);
302 bool shdata_changed = changed&SHADER_DATA;
303 for(auto i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i)
304 shdata_changed = (i->shdata->get_generation()!=i->generation);
305 bool extra_shdata = (shdata_stack.size()>state->shdata_count);
307 if(shdata_changed || shprog_changed || extra_shdata)
310 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
311 for(const BoundProgramData &d: shdata_stack)
313 d.shdata->apply(*state->shprog, pipeline_state);
314 d.generation = d.shdata->get_generation();
316 changed &= ~SHADER_DATA;
319 pipeline_state.set_depth_test(state->depth_test);
320 pipeline_state.set_stencil_test(state->stencil_test);
321 pipeline_state.set_blend(state->blend);
325 Renderer::BoundProgramData::BoundProgramData(const ProgramData *d):