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);
39 if(state_stack.size()>1)
40 throw invalid_operation("Renderer::end");
44 add_shader_data(standard_shdata);
46 commands.use_pipeline(0);
49 void Renderer::push_state()
51 state_stack.push_back(state_stack.back());
52 state = &state_stack.back();
55 void Renderer::pop_state()
57 if(state_stack.size()==1)
58 throw stack_underflow("Renderer::pop_state");
60 state_stack.pop_back();
61 state = &state_stack.back();
65 void Renderer::set_camera(const Camera &c)
68 add_shader_data(c.get_shader_data());
72 void Renderer::set_matrix(const Matrix &matrix)
74 state->model_matrix = matrix;
78 void Renderer::transform(const Matrix &matrix)
80 state->model_matrix *= matrix;
84 void Renderer::set_framebuffer(const Framebuffer *f)
86 state->framebuffer = f;
89 void Renderer::set_viewport(const Rect *v)
94 void Renderer::set_scissor(const Rect *s)
99 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
106 void Renderer::add_shader_data(const ProgramData &d)
108 if(state->shdata_count<shdata_stack.size())
110 const BoundProgramData &top = shdata_stack.back();
111 if(top.shdata==&d && top.generation==d.get_generation())
113 ++state->shdata_count;
119 shdata_stack.push_back(&d);
120 state->shdata_count = shdata_stack.size();
121 changed |= SHADER_DATA;
124 void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
127 if(ResourceManager *res_mgr = tex->get_manager())
128 res_mgr->resource_used(*tex);
130 if(texture_stack.size()>state->texture_count)
132 BoundTexture &bt = texture_stack[state->texture_count];
133 if(bt.tag==tag && bt.texture==tex && bt.sampler==samp)
135 ++state->texture_count;
142 for(auto i=texture_stack.end(); i!=texture_stack.begin(); )
145 i->replaced = texture_stack.size();
149 texture_stack.push_back(BoundTexture());
150 BoundTexture &bound_tex = texture_stack.back();
152 bound_tex.texture = tex;
153 bound_tex.sampler = samp;
154 state->texture_count = texture_stack.size();
157 void Renderer::flush_shader_data()
159 if(shdata_stack.size()>state->shdata_count)
160 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
163 void Renderer::flush_textures()
165 for(unsigned i=0; i<state->texture_count; ++i)
166 if(texture_stack[i].replaced>=static_cast<int>(state->texture_count))
167 texture_stack[i].replaced = -1;
169 texture_stack.erase(texture_stack.begin()+state->texture_count, texture_stack.end());
172 void Renderer::set_vertex_setup(const VertexSetup *vs)
174 state->vertex_setup = vs;
177 void Renderer::set_front_face(FaceWinding winding)
179 state->front_face = winding;
182 void Renderer::set_face_cull(CullMode cull)
184 state->face_cull = cull;
187 void Renderer::set_depth_test(const DepthTest *dt)
189 state->depth_test = dt;
192 void Renderer::set_stencil_test(const StencilTest *st)
194 state->stencil_test = st;
197 void Renderer::set_blend(const Blend *b)
202 void Renderer::set_object_lod_bias(unsigned b)
204 state->object_lod_bias = b;
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");
262 pipeline_state.set_framebuffer(state->framebuffer);
263 pipeline_state.set_viewport(state->viewport);
264 pipeline_state.set_scissor(state->scissor);
266 bool shprog_changed = (state->shprog!=pipeline_state.get_shader_program());
267 pipeline_state.set_shader_program(state->shprog);
271 standard_shdata.uniform("world_obj_matrix", state->model_matrix);
272 LinAl::SquareMatrix<float, 3> nm = state->model_matrix.block<3, 3>(0, 0);
273 nm = transpose(invert(nm));
274 standard_shdata.uniform("world_obj_normal_matrix", nm);
278 bool shdata_changed = changed&SHADER_DATA;
279 for(auto i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i)
280 shdata_changed = (i->shdata->get_generation()!=i->generation);
281 bool extra_shdata = (shdata_stack.size()>state->shdata_count);
283 if(shdata_changed || shprog_changed || extra_shdata)
286 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
287 for(const BoundProgramData &d: shdata_stack)
289 d.shdata->apply(*state->shprog, pipeline_state);
290 d.generation = d.shdata->get_generation();
292 changed &= ~SHADER_DATA;
295 if(state->vertex_setup)
297 if(const VertexArray *array = state->vertex_setup->get_vertex_array())
299 if(const VertexArray *array = state->vertex_setup->get_instance_array())
302 pipeline_state.set_vertex_setup(state->vertex_setup);
304 pipeline_state.set_front_face(state->front_face);
305 pipeline_state.set_face_cull(state->face_cull);
307 if(state->texture_count<texture_stack.size())
310 for(const BoundTexture &t: texture_stack)
311 if(t.texture && t.replaced<0)
313 if(t.binding<0 || shprog_changed)
314 t.binding = state->shprog->get_uniform_binding(t.tag);
316 pipeline_state.set_texture(t.binding, t.texture, t.sampler);
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):