5 #include "framebuffer.h"
9 #include "programdata.h"
10 #include "renderable.h"
12 #include "resourcemanager.h"
13 #include "resources.h"
16 #include "vertexarray.h"
17 #include "vertexsetup.h"
25 placeholder_texture(Resources::get_global().get<Texture>("_placeholder.png"))
27 state_stack.reserve(16);
28 shdata_stack.reserve(32);
29 texture_stack.reserve(32);
32 void Renderer::begin()
35 throw invalid_operation("Renderer::begin");
38 state_stack.emplace_back();
39 current_state = &state_stack.back();
41 RendererBackend::begin();
43 add_shader_data(standard_shdata);
44 commands.begin_frame(frame_index);
49 if(!current_state || state_stack.size()>1)
50 throw invalid_operation("Renderer::end");
52 RendererBackend::end();
56 texture_stack.clear();
60 void Renderer::push_state()
62 if(state_stack.empty())
63 throw invalid_operation("Renderer::push_state");
65 state_stack.push_back(state_stack.back());
66 current_state = &state_stack.back();
69 void Renderer::pop_state()
71 if(state_stack.size()==1)
72 throw stack_underflow("Renderer::pop_state");
74 uintptr_t old_pipeline = current_state->pipeline_key;
76 state_stack.pop_back();
77 current_state = &state_stack.back();
80 if(current_state->pipeline_key!=old_pipeline)
81 changed |= PIPELINE_KEY;
84 Renderer::State &Renderer::get_state() const
88 throw invalid_operation("Renderer::get_state");
90 return *current_state;
93 void Renderer::set_pipeline_key(uintptr_t key)
95 State &state = get_state();
96 if(key!=state.pipeline_key)
98 state.pipeline_key = key;
99 changed |= PIPELINE_KEY;
103 void Renderer::set_camera(const Camera &c)
105 get_state().camera = &c;
106 add_shader_data(c.get_shader_data());
107 set_matrix(Matrix());
110 void Renderer::set_matrix(const Matrix &matrix)
112 get_state().model_matrix = matrix;
116 void Renderer::set_framebuffer(const Framebuffer *f)
118 get_state().framebuffer = f;
121 void Renderer::set_viewport(const Rect *v)
123 get_state().viewport = v;
126 void Renderer::set_scissor(const Rect *s)
128 get_state().scissor = s;
131 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
133 get_state().shprog = p;
138 void Renderer::add_shader_data(const ProgramData &d)
140 State &state = get_state();
142 if(state.shdata_count<shdata_stack.size())
144 const BoundProgramData &top = shdata_stack.back();
145 if(top.shdata==&d && top.generation==d.get_generation())
147 ++state.shdata_count;
153 shdata_stack.push_back(&d);
154 state.shdata_count = shdata_stack.size();
155 changed |= SHADER_DATA;
158 void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
160 set_texture(tag, tex, -1, samp);
163 void Renderer::set_texture(Tag tag, const Texture *tex, int level, const Sampler *samp)
165 State &state = get_state();
169 if(ResourceManager *res_mgr = tex->get_manager())
170 res_mgr->resource_used(*tex);
171 if(!tex->is_loaded())
172 tex = &placeholder_texture;
175 if(texture_stack.size()>state.texture_count)
177 BoundTexture &bt = texture_stack[state.texture_count];
178 if(bt.tag==tag && bt.texture==tex && bt.sampler==samp)
180 ++state.texture_count;
187 for(auto i=texture_stack.end(); i!=texture_stack.begin(); )
190 i->replaced = texture_stack.size();
194 texture_stack.emplace_back();
195 BoundTexture &bound_tex = texture_stack.back();
197 bound_tex.texture = tex;
198 bound_tex.sampler = samp;
199 bound_tex.level = level;
200 state.texture_count = texture_stack.size();
203 void Renderer::flush_shader_data()
205 const State &state = get_state();
207 if(shdata_stack.size()>state.shdata_count)
208 shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
211 void Renderer::flush_textures()
213 const State &state = get_state();
215 for(unsigned i=0; i<state.texture_count; ++i)
216 if(texture_stack[i].replaced>=static_cast<int>(state.texture_count))
217 texture_stack[i].replaced = -1;
219 texture_stack.erase(texture_stack.begin()+state.texture_count, texture_stack.end());
222 void Renderer::set_vertex_setup(const VertexSetup *vs)
224 get_state().vertex_setup = vs;
227 void Renderer::set_front_face(FaceWinding winding)
229 get_state().front_face = winding;
232 void Renderer::set_face_cull(CullMode cull)
234 get_state().face_cull = cull;
237 void Renderer::set_depth_test(const DepthTest *dt)
239 get_state().depth_test = dt;
242 void Renderer::set_stencil_test(const StencilTest *st)
244 get_state().stencil_test = st;
247 void Renderer::set_blend(const Blend *b)
249 get_state().blend = b;
252 void Renderer::set_object_lod_bias(unsigned b)
254 get_state().object_lod_bias = b;
257 void Renderer::clear(const ClearValue *values)
260 commands.use_pipeline(&get_pipeline_state());
261 commands.clear(values);
264 void Renderer::draw(const Batch &batch)
267 batch.refresh(frame_index);
268 PipelineState &ps = get_pipeline_state();
269 ps.set_primitive_type(batch.get_type());
270 commands.use_pipeline(&ps);
271 commands.draw(batch);
274 void Renderer::draw_instanced(const Batch &batch, unsigned count)
277 batch.refresh(frame_index);
278 PipelineState &ps = get_pipeline_state();
279 ps.set_primitive_type(batch.get_type());
280 commands.use_pipeline(&ps);
281 commands.draw_instanced(batch, count);
284 void Renderer::resolve_multisample(Framebuffer &target)
286 const State &state = get_state();
288 if(!state.framebuffer)
289 throw invalid_operation("Renderer::resolve_multisample");
291 unsigned width = state.framebuffer->get_width();
292 unsigned height = state.framebuffer->get_height();
293 if(target.get_width()!=width || target.get_height()!=height)
294 throw incompatible_data("Renderer::resolve_multisample");
297 commands.use_pipeline(&get_pipeline_state());
298 commands.resolve_multisample(target);
301 void Renderer::begin_query(const QueryPool &pool, unsigned index)
303 commands.begin_query(pool, index);
306 void Renderer::end_query(const QueryPool &pool, unsigned index)
308 commands.end_query(pool, index);
311 PipelineState &Renderer::get_pipeline_state()
313 if(changed&PIPELINE_KEY)
315 RendererBackend::set_pipeline_key(current_state->pipeline_key);
316 changed &= ~PIPELINE_KEY;
319 return RendererBackend::get_pipeline_state();
322 void Renderer::apply_framebuffer()
324 const State &state = get_state();
326 PipelineState &ps = get_pipeline_state();
328 ps.set_framebuffer(state.framebuffer);
329 static const Rect default_rect = Rect::max();
330 ps.set_viewport(state.viewport ? *state.viewport : default_rect);
331 ps.set_scissor(state.scissor ? *state.scissor : default_rect);
334 void Renderer::apply_state()
336 const State &state = get_state();
339 throw invalid_operation("Renderer::apply_state");
343 PipelineState &ps = get_pipeline_state();
344 bool pipeline_changed = (&ps!=last_pipeline);
347 bool shprog_changed = (state.shprog!=ps.get_shader_program());
348 ps.set_shader_program(state.shprog);
352 standard_shdata.uniform("world_obj_matrix", state.model_matrix);
353 LinAl::SquareMatrix<float, 3> nm = state.model_matrix.block<3, 3>(0, 0);
354 nm = transpose(invert(nm));
355 standard_shdata.uniform("world_obj_normal_matrix", nm);
359 bool shdata_changed = changed&SHADER_DATA;
360 for(auto i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i)
361 shdata_changed = (i->shdata->get_generation()!=i->generation);
362 bool extra_shdata = (shdata_stack.size()>state.shdata_count);
364 if(shdata_changed || shprog_changed || pipeline_changed || extra_shdata)
367 shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
368 for(const BoundProgramData &d: shdata_stack)
370 d.shdata->apply(*state.shprog, ps, frame_index);
371 d.generation = d.shdata->get_generation();
373 changed &= ~SHADER_DATA;
376 if(state.vertex_setup)
378 if(const VertexArray *array = state.vertex_setup->get_vertex_array())
379 array->refresh(frame_index);
380 if(const VertexArray *array = state.vertex_setup->get_instance_array())
381 array->refresh(frame_index);
383 ps.set_vertex_setup(state.vertex_setup);
385 ps.set_front_face(state.front_face);
386 ps.set_face_cull(state.face_cull);
388 if(state.texture_count<texture_stack.size())
391 for(const BoundTexture &t: texture_stack)
392 if(t.texture && t.replaced<0)
394 if(t.binding<0 || shprog_changed)
395 t.binding = state.shprog->get_uniform_binding(t.tag);
397 ps.set_texture(t.binding, t.texture, t.level, t.sampler);
400 static const DepthTest default_depth_test;
401 ps.set_depth_test(state.depth_test ? *state.depth_test : default_depth_test);
402 static const StencilTest default_stencil_test;
403 ps.set_stencil_test(state.stencil_test ? *state.stencil_test : default_stencil_test);
404 static const Blend default_blend;
405 ps.set_blend(state.blend ? *state.blend : default_blend);
409 Renderer::BoundProgramData::BoundProgramData(const ProgramData *d):