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);
34 void Renderer::begin()
37 throw invalid_operation("Renderer::begin");
39 state_stack.push_back(State());
40 current_state = &state_stack.back();
41 add_shader_data(standard_shdata);
46 if(!current_state || state_stack.size()>1)
47 throw invalid_operation("Renderer::end");
51 texture_stack.clear();
54 commands.use_pipeline(0);
57 void Renderer::push_state()
59 if(state_stack.empty())
60 throw invalid_operation("Renderer::push_state");
62 state_stack.push_back(state_stack.back());
63 current_state = &state_stack.back();
66 void Renderer::pop_state()
68 if(state_stack.size()==1)
69 throw stack_underflow("Renderer::pop_state");
71 state_stack.pop_back();
72 current_state = &state_stack.back();
76 Renderer::State &Renderer::get_state() const
80 throw invalid_operation("Renderer::get_state");
82 return *current_state;
85 void Renderer::set_camera(const Camera &c)
87 get_state().camera = &c;
88 add_shader_data(c.get_shader_data());
92 void Renderer::set_matrix(const Matrix &matrix)
94 get_state().model_matrix = matrix;
98 void Renderer::set_framebuffer(const Framebuffer *f)
100 get_state().framebuffer = f;
103 void Renderer::set_viewport(const Rect *v)
105 get_state().viewport = v;
108 void Renderer::set_scissor(const Rect *s)
110 get_state().scissor = s;
113 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
115 get_state().shprog = p;
120 void Renderer::add_shader_data(const ProgramData &d)
122 State &state = get_state();
124 if(state.shdata_count<shdata_stack.size())
126 const BoundProgramData &top = shdata_stack.back();
127 if(top.shdata==&d && top.generation==d.get_generation())
129 ++state.shdata_count;
135 shdata_stack.push_back(&d);
136 state.shdata_count = shdata_stack.size();
137 changed |= SHADER_DATA;
140 void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
142 State &state = get_state();
145 if(ResourceManager *res_mgr = tex->get_manager())
146 res_mgr->resource_used(*tex);
148 if(texture_stack.size()>state.texture_count)
150 BoundTexture &bt = texture_stack[state.texture_count];
151 if(bt.tag==tag && bt.texture==tex && bt.sampler==samp)
153 ++state.texture_count;
160 for(auto i=texture_stack.end(); i!=texture_stack.begin(); )
163 i->replaced = texture_stack.size();
167 texture_stack.push_back(BoundTexture());
168 BoundTexture &bound_tex = texture_stack.back();
170 bound_tex.texture = tex;
171 bound_tex.sampler = samp;
172 state.texture_count = texture_stack.size();
175 void Renderer::flush_shader_data()
177 const State &state = get_state();
179 if(shdata_stack.size()>state.shdata_count)
180 shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
183 void Renderer::flush_textures()
185 const State &state = get_state();
187 for(unsigned i=0; i<state.texture_count; ++i)
188 if(texture_stack[i].replaced>=static_cast<int>(state.texture_count))
189 texture_stack[i].replaced = -1;
191 texture_stack.erase(texture_stack.begin()+state.texture_count, texture_stack.end());
194 void Renderer::set_vertex_setup(const VertexSetup *vs)
196 get_state().vertex_setup = vs;
199 void Renderer::set_front_face(FaceWinding winding)
201 get_state().front_face = winding;
204 void Renderer::set_face_cull(CullMode cull)
206 get_state().face_cull = cull;
209 void Renderer::set_depth_test(const DepthTest *dt)
211 get_state().depth_test = dt;
214 void Renderer::set_stencil_test(const StencilTest *st)
216 get_state().stencil_test = st;
219 void Renderer::set_blend(const Blend *b)
221 get_state().blend = b;
224 void Renderer::set_object_lod_bias(unsigned b)
226 get_state().object_lod_bias = b;
229 void Renderer::clear(const ClearValue *values)
231 const State &state = get_state();
233 pipeline_state.set_framebuffer(state.framebuffer);
234 pipeline_state.set_viewport(state.viewport);
235 pipeline_state.set_scissor(state.scissor);
236 commands.use_pipeline(&pipeline_state);
237 commands.clear(values);
240 void Renderer::draw(const Batch &batch)
244 commands.use_pipeline(&pipeline_state);
245 commands.draw(batch);
248 void Renderer::draw_instanced(const Batch &batch, unsigned count)
252 commands.use_pipeline(&pipeline_state);
253 commands.draw_instanced(batch, count);
256 void Renderer::resolve_multisample(Framebuffer &target)
258 const State &state = get_state();
260 if(!state.framebuffer)
261 throw invalid_operation("Renderer::resolve_multisample");
263 unsigned width = state.framebuffer->get_width();
264 unsigned height = state.framebuffer->get_height();
265 if(target.get_width()!=width || target.get_height()!=height)
266 throw incompatible_data("Renderer::resolve_multisample");
268 pipeline_state.set_framebuffer(state.framebuffer);
269 commands.use_pipeline(&pipeline_state);
270 commands.resolve_multisample(target);
273 void Renderer::begin_query(const QueryPool &pool, unsigned index)
275 commands.begin_query(pool, index);
278 void Renderer::end_query(const QueryPool &pool, unsigned index)
280 commands.end_query(pool, index);
283 void Renderer::apply_state()
285 const State &state = get_state();
288 throw invalid_operation("Renderer::apply_state");
290 pipeline_state.set_framebuffer(state.framebuffer);
291 pipeline_state.set_viewport(state.viewport);
292 pipeline_state.set_scissor(state.scissor);
294 bool shprog_changed = (state.shprog!=pipeline_state.get_shader_program());
295 pipeline_state.set_shader_program(state.shprog);
299 standard_shdata.uniform("world_obj_matrix", state.model_matrix);
300 LinAl::SquareMatrix<float, 3> nm = state.model_matrix.block<3, 3>(0, 0);
301 nm = transpose(invert(nm));
302 standard_shdata.uniform("world_obj_normal_matrix", nm);
306 bool shdata_changed = changed&SHADER_DATA;
307 for(auto i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i)
308 shdata_changed = (i->shdata->get_generation()!=i->generation);
309 bool extra_shdata = (shdata_stack.size()>state.shdata_count);
311 if(shdata_changed || shprog_changed || extra_shdata)
314 shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
315 for(const BoundProgramData &d: shdata_stack)
317 d.shdata->apply(*state.shprog, pipeline_state);
318 d.generation = d.shdata->get_generation();
320 changed &= ~SHADER_DATA;
323 if(state.vertex_setup)
325 if(const VertexArray *array = state.vertex_setup->get_vertex_array())
327 if(const VertexArray *array = state.vertex_setup->get_instance_array())
330 pipeline_state.set_vertex_setup(state.vertex_setup);
332 pipeline_state.set_front_face(state.front_face);
333 pipeline_state.set_face_cull(state.face_cull);
335 if(state.texture_count<texture_stack.size())
338 for(const BoundTexture &t: texture_stack)
339 if(t.texture && t.replaced<0)
341 if(t.binding<0 || shprog_changed)
342 t.binding = state.shprog->get_uniform_binding(t.tag);
344 pipeline_state.set_texture(t.binding, t.texture, t.sampler);
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):