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"
24 const Tag Renderer::world_obj_matrix_tag("world_obj_matrix");
25 const Tag Renderer::world_obj_normal_matrix_tag("world_obj_normal_matrix");
28 placeholder_texture(Resources::get_global().get<Texture>("_placeholder.png"))
30 state_stack.reserve(16);
31 shdata_stack.reserve(32);
32 texture_stack.reserve(32);
35 void Renderer::begin()
38 throw invalid_operation("Renderer::begin");
41 state_stack.emplace_back();
42 current_state = &state_stack.back();
44 RendererBackend::begin();
46 commands.begin_frame(frame_index);
51 if(!current_state || state_stack.size()>1)
52 throw invalid_operation("Renderer::end");
54 RendererBackend::end();
58 texture_stack.clear();
62 void Renderer::push_state()
64 if(state_stack.empty())
65 throw invalid_operation("Renderer::push_state");
67 state_stack.push_back(state_stack.back());
68 current_state = &state_stack.back();
71 void Renderer::pop_state()
73 if(state_stack.size()==1)
74 throw stack_underflow("Renderer::pop_state");
76 uintptr_t old_pipeline = current_state->pipeline_key;
78 state_stack.pop_back();
79 current_state = &state_stack.back();
82 if(current_state->pipeline_key!=old_pipeline)
83 changed |= PIPELINE_KEY;
86 Renderer::State &Renderer::get_state() const
90 throw invalid_operation("Renderer::get_state");
92 return *current_state;
95 void Renderer::set_pipeline_key(uintptr_t key)
97 State &state = get_state();
98 if(key!=state.pipeline_key)
100 state.pipeline_key = key;
101 changed |= PIPELINE_KEY;
105 void Renderer::set_camera(const Camera &c)
107 get_state().camera = &c;
108 set_matrix(Matrix());
111 void Renderer::set_matrix(const Matrix &matrix)
113 get_state().model_matrix = matrix;
117 void Renderer::set_framebuffer(const Framebuffer *f)
119 get_state().framebuffer = f;
122 void Renderer::set_viewport(const Rect *v)
124 get_state().viewport = v;
127 void Renderer::set_scissor(const Rect *s)
129 get_state().scissor = s;
132 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
134 get_state().shprog = p;
139 void Renderer::add_shader_data(const ProgramData &d)
141 State &state = get_state();
143 if(state.shdata_count<shdata_stack.size())
145 const BoundProgramData &top = shdata_stack.back();
146 if(top.shdata==&d && top.generation==d.get_generation())
148 ++state.shdata_count;
154 shdata_stack.push_back(&d);
155 state.shdata_count = shdata_stack.size();
156 changed |= SHADER_DATA;
159 void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
161 set_texture(tag, tex, -1, samp);
164 void Renderer::set_texture(Tag tag, const Texture *tex, int level, const Sampler *samp)
166 State &state = get_state();
170 if(ResourceManager *res_mgr = tex->get_manager())
171 res_mgr->resource_used(*tex);
172 if(!tex->is_loaded())
173 tex = &placeholder_texture;
176 if(texture_stack.size()>state.texture_count)
178 BoundTexture &bt = texture_stack[state.texture_count];
179 if(bt.tag==tag && bt.texture==tex && bt.sampler==samp)
181 ++state.texture_count;
188 for(auto i=texture_stack.end(); i!=texture_stack.begin(); )
191 i->replaced = texture_stack.size();
195 texture_stack.emplace_back();
196 BoundTexture &bound_tex = texture_stack.back();
198 bound_tex.texture = tex;
199 bound_tex.sampler = samp;
200 bound_tex.level = level;
201 state.texture_count = texture_stack.size();
204 void Renderer::flush_shader_data()
206 const State &state = get_state();
208 if(shdata_stack.size()>state.shdata_count)
209 shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
212 void Renderer::flush_textures()
214 const State &state = get_state();
216 for(unsigned i=0; i<state.texture_count; ++i)
217 if(texture_stack[i].replaced>=static_cast<int>(state.texture_count))
218 texture_stack[i].replaced = -1;
220 texture_stack.erase(texture_stack.begin()+state.texture_count, texture_stack.end());
223 void Renderer::set_vertex_setup(const VertexSetup *vs)
225 get_state().vertex_setup = vs;
228 void Renderer::set_front_face(FaceWinding winding)
230 get_state().front_face = winding;
233 void Renderer::set_face_cull(CullMode cull)
235 get_state().face_cull = cull;
238 void Renderer::set_depth_test(const DepthTest *dt)
240 get_state().depth_test = dt;
243 void Renderer::set_stencil_test(const StencilTest *st)
245 get_state().stencil_test = st;
248 void Renderer::set_blend(const Blend *b)
250 get_state().blend = b;
253 void Renderer::set_object_lod_bias(unsigned b)
255 get_state().object_lod_bias = b;
258 void Renderer::clear(const ClearValue *values)
261 commands.use_pipeline(&get_pipeline_state());
262 commands.clear(values);
265 void Renderer::draw(const Batch &batch)
268 batch.refresh(frame_index);
269 PipelineState &ps = get_pipeline_state();
270 ps.set_primitive_type(batch.get_type());
271 commands.use_pipeline(&ps);
272 commands.draw(batch);
275 void Renderer::draw_instanced(const Batch &batch, unsigned count)
278 batch.refresh(frame_index);
279 PipelineState &ps = get_pipeline_state();
280 ps.set_primitive_type(batch.get_type());
281 commands.use_pipeline(&ps);
282 commands.draw_instanced(batch, count);
285 void Renderer::resolve_multisample(Framebuffer &target)
287 const State &state = get_state();
289 if(!state.framebuffer)
290 throw invalid_operation("Renderer::resolve_multisample");
292 unsigned width = state.framebuffer->get_width();
293 unsigned height = state.framebuffer->get_height();
294 if(target.get_width()!=width || target.get_height()!=height)
295 throw incompatible_data("Renderer::resolve_multisample");
298 commands.use_pipeline(&get_pipeline_state());
299 commands.resolve_multisample(target);
302 void Renderer::begin_query(const QueryPool &pool, unsigned index)
304 commands.begin_query(pool, index);
307 void Renderer::end_query(const QueryPool &pool, unsigned index)
309 commands.end_query(pool, index);
312 PipelineState &Renderer::get_pipeline_state()
314 if(changed&PIPELINE_KEY)
316 RendererBackend::set_pipeline_key(current_state->pipeline_key);
317 changed &= ~PIPELINE_KEY;
320 return RendererBackend::get_pipeline_state();
323 void Renderer::apply_framebuffer()
325 const State &state = get_state();
327 PipelineState &ps = get_pipeline_state();
329 ps.set_framebuffer(state.framebuffer);
330 static const Rect default_rect = Rect::max();
331 ps.set_viewport(state.viewport ? *state.viewport : default_rect);
332 ps.set_scissor(state.scissor ? *state.scissor : default_rect);
335 void Renderer::apply_state()
337 const State &state = get_state();
340 throw invalid_operation("Renderer::apply_state");
344 PipelineState &ps = get_pipeline_state();
345 bool pipeline_changed = (&ps!=last_pipeline);
348 bool shprog_changed = (state.shprog!=ps.get_shader_program());
349 ps.set_shader_program(state.shprog);
351 bool shdata_changed = changed&SHADER_DATA;
352 for(auto i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i)
353 shdata_changed = (i->shdata->get_generation()!=i->generation);
354 bool extra_shdata = (shdata_stack.size()>state.shdata_count);
358 shdata_changed = true;
364 standard_shdata.uniform(world_obj_matrix_tag, state.model_matrix);
365 LinAl::Matrix<float, 3, 3> nm = state.model_matrix.block<3, 3>(0, 0);
366 nm = transpose(invert(nm));
367 standard_shdata.uniform(world_obj_normal_matrix_tag, nm);
369 shdata_changed = true;
372 if(shdata_changed || shprog_changed || pipeline_changed || extra_shdata)
375 shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
376 standard_shdata.apply(*state.shprog, ps, frame_index);
378 state.camera->get_shader_data().apply(*state.shprog, ps, frame_index);
379 for(const BoundProgramData &d: shdata_stack)
381 d.shdata->apply(*state.shprog, ps, frame_index);
382 d.generation = d.shdata->get_generation();
384 changed &= ~SHADER_DATA;
387 if(state.vertex_setup)
389 if(const VertexArray *array = state.vertex_setup->get_vertex_array())
390 array->refresh(frame_index);
391 if(const VertexArray *array = state.vertex_setup->get_instance_array())
392 array->refresh(frame_index);
394 ps.set_vertex_setup(state.vertex_setup);
396 ps.set_front_face(state.front_face);
397 ps.set_face_cull(state.face_cull);
399 if(state.texture_count<texture_stack.size())
402 for(const BoundTexture &t: texture_stack)
403 if(t.texture && t.replaced<0)
405 if(t.binding<0 || shprog_changed)
406 t.binding = state.shprog->get_uniform_binding(t.tag);
408 ps.set_texture(t.binding, t.texture, t.level, t.sampler);
411 static const DepthTest default_depth_test;
412 ps.set_depth_test(state.depth_test ? *state.depth_test : default_depth_test);
413 static const StencilTest default_stencil_test;
414 ps.set_stencil_test(state.stencil_test ? *state.stencil_test : default_stencil_test);
415 static const Blend default_blend;
416 ps.set_blend(state.blend ? *state.blend : default_blend);
420 Renderer::BoundProgramData::BoundProgramData(const ProgramData *d):