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 shdata_stack.reserve(32);
27 texture_stack.reserve(32);
30 void Renderer::begin()
33 throw invalid_operation("Renderer::begin");
36 state_stack.emplace_back();
37 current_state = &state_stack.back();
39 RendererBackend::begin();
41 add_shader_data(standard_shdata);
42 commands.begin_frame(frame_index);
47 if(!current_state || state_stack.size()>1)
48 throw invalid_operation("Renderer::end");
50 RendererBackend::end();
54 texture_stack.clear();
58 void Renderer::push_state()
60 if(state_stack.empty())
61 throw invalid_operation("Renderer::push_state");
63 state_stack.push_back(state_stack.back());
64 current_state = &state_stack.back();
67 void Renderer::pop_state()
69 if(state_stack.size()==1)
70 throw stack_underflow("Renderer::pop_state");
72 state_stack.pop_back();
73 current_state = &state_stack.back();
77 Renderer::State &Renderer::get_state() const
81 throw invalid_operation("Renderer::get_state");
83 return *current_state;
86 void Renderer::set_camera(const Camera &c)
88 get_state().camera = &c;
89 add_shader_data(c.get_shader_data());
93 void Renderer::set_matrix(const Matrix &matrix)
95 get_state().model_matrix = matrix;
99 void Renderer::set_framebuffer(const Framebuffer *f)
101 get_state().framebuffer = f;
104 void Renderer::set_viewport(const Rect *v)
106 get_state().viewport = v;
109 void Renderer::set_scissor(const Rect *s)
111 get_state().scissor = s;
114 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
116 get_state().shprog = p;
121 void Renderer::add_shader_data(const ProgramData &d)
123 State &state = get_state();
125 if(state.shdata_count<shdata_stack.size())
127 const BoundProgramData &top = shdata_stack.back();
128 if(top.shdata==&d && top.generation==d.get_generation())
130 ++state.shdata_count;
136 shdata_stack.push_back(&d);
137 state.shdata_count = shdata_stack.size();
138 changed |= SHADER_DATA;
141 void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
143 State &state = get_state();
146 if(ResourceManager *res_mgr = tex->get_manager())
147 res_mgr->resource_used(*tex);
149 if(texture_stack.size()>state.texture_count)
151 BoundTexture &bt = texture_stack[state.texture_count];
152 if(bt.tag==tag && bt.texture==tex && bt.sampler==samp)
154 ++state.texture_count;
161 for(auto i=texture_stack.end(); i!=texture_stack.begin(); )
164 i->replaced = texture_stack.size();
168 texture_stack.emplace_back();
169 BoundTexture &bound_tex = texture_stack.back();
171 bound_tex.texture = tex;
172 bound_tex.sampler = samp;
173 state.texture_count = texture_stack.size();
176 void Renderer::flush_shader_data()
178 const State &state = get_state();
180 if(shdata_stack.size()>state.shdata_count)
181 shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
184 void Renderer::flush_textures()
186 const State &state = get_state();
188 for(unsigned i=0; i<state.texture_count; ++i)
189 if(texture_stack[i].replaced>=static_cast<int>(state.texture_count))
190 texture_stack[i].replaced = -1;
192 texture_stack.erase(texture_stack.begin()+state.texture_count, texture_stack.end());
195 void Renderer::set_vertex_setup(const VertexSetup *vs)
197 get_state().vertex_setup = vs;
200 void Renderer::set_front_face(FaceWinding winding)
202 get_state().front_face = winding;
205 void Renderer::set_face_cull(CullMode cull)
207 get_state().face_cull = cull;
210 void Renderer::set_depth_test(const DepthTest *dt)
212 get_state().depth_test = dt;
215 void Renderer::set_stencil_test(const StencilTest *st)
217 get_state().stencil_test = st;
220 void Renderer::set_blend(const Blend *b)
222 get_state().blend = b;
225 void Renderer::set_object_lod_bias(unsigned b)
227 get_state().object_lod_bias = b;
230 void Renderer::clear(const ClearValue *values)
232 const State &state = get_state();
234 pipeline_state.set_framebuffer(state.framebuffer);
235 pipeline_state.set_viewport(state.viewport);
236 pipeline_state.set_scissor(state.scissor);
237 commands.use_pipeline(&pipeline_state);
238 commands.clear(values);
241 void Renderer::draw(const Batch &batch)
245 pipeline_state.set_primitive_type(batch.get_type());
246 commands.use_pipeline(&pipeline_state);
247 commands.draw(batch);
250 void Renderer::draw_instanced(const Batch &batch, unsigned count)
254 pipeline_state.set_primitive_type(batch.get_type());
255 commands.use_pipeline(&pipeline_state);
256 commands.draw_instanced(batch, count);
259 void Renderer::resolve_multisample(Framebuffer &target)
261 const State &state = get_state();
263 if(!state.framebuffer)
264 throw invalid_operation("Renderer::resolve_multisample");
266 unsigned width = state.framebuffer->get_width();
267 unsigned height = state.framebuffer->get_height();
268 if(target.get_width()!=width || target.get_height()!=height)
269 throw incompatible_data("Renderer::resolve_multisample");
271 pipeline_state.set_framebuffer(state.framebuffer);
272 commands.use_pipeline(&pipeline_state);
273 commands.resolve_multisample(target);
276 void Renderer::begin_query(const QueryPool &pool, unsigned index)
278 commands.begin_query(pool, index);
281 void Renderer::end_query(const QueryPool &pool, unsigned index)
283 commands.end_query(pool, index);
286 void Renderer::apply_state()
288 const State &state = get_state();
291 throw invalid_operation("Renderer::apply_state");
293 pipeline_state.set_framebuffer(state.framebuffer);
294 pipeline_state.set_viewport(state.viewport);
295 pipeline_state.set_scissor(state.scissor);
297 bool shprog_changed = (state.shprog!=pipeline_state.get_shader_program());
298 pipeline_state.set_shader_program(state.shprog);
302 standard_shdata.uniform("world_obj_matrix", state.model_matrix);
303 LinAl::SquareMatrix<float, 3> nm = state.model_matrix.block<3, 3>(0, 0);
304 nm = transpose(invert(nm));
305 standard_shdata.uniform("world_obj_normal_matrix", nm);
309 bool shdata_changed = changed&SHADER_DATA;
310 for(auto i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i)
311 shdata_changed = (i->shdata->get_generation()!=i->generation);
312 bool extra_shdata = (shdata_stack.size()>state.shdata_count);
314 if(shdata_changed || shprog_changed || extra_shdata)
317 shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
318 for(const BoundProgramData &d: shdata_stack)
320 d.shdata->apply(*state.shprog, pipeline_state);
321 d.generation = d.shdata->get_generation();
323 changed &= ~SHADER_DATA;
326 if(state.vertex_setup)
328 if(const VertexArray *array = state.vertex_setup->get_vertex_array())
330 if(const VertexArray *array = state.vertex_setup->get_instance_array())
333 pipeline_state.set_vertex_setup(state.vertex_setup);
335 pipeline_state.set_front_face(state.front_face);
336 pipeline_state.set_face_cull(state.face_cull);
338 if(state.texture_count<texture_stack.size())
341 for(const BoundTexture &t: texture_stack)
342 if(t.texture && t.replaced<0)
344 if(t.binding<0 || shprog_changed)
345 t.binding = state.shprog->get_uniform_binding(t.tag);
347 pipeline_state.set_texture(t.binding, t.texture, t.sampler);
350 pipeline_state.set_depth_test(state.depth_test);
351 pipeline_state.set_stencil_test(state.stencil_test);
352 pipeline_state.set_blend(state.blend);
356 Renderer::BoundProgramData::BoundProgramData(const ProgramData *d):