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 commands.begin_frame(frame_index);
48 if(!current_state || state_stack.size()>1)
49 throw invalid_operation("Renderer::end");
51 RendererBackend::end();
55 texture_stack.clear();
59 void Renderer::push_state()
61 if(state_stack.empty())
62 throw invalid_operation("Renderer::push_state");
64 state_stack.push_back(state_stack.back());
65 current_state = &state_stack.back();
68 void Renderer::pop_state()
70 if(state_stack.size()==1)
71 throw stack_underflow("Renderer::pop_state");
73 uintptr_t old_pipeline = current_state->pipeline_key;
75 state_stack.pop_back();
76 current_state = &state_stack.back();
79 if(current_state->pipeline_key!=old_pipeline)
80 changed |= PIPELINE_KEY;
83 Renderer::State &Renderer::get_state() const
87 throw invalid_operation("Renderer::get_state");
89 return *current_state;
92 void Renderer::set_pipeline_key(uintptr_t key)
94 State &state = get_state();
95 if(key!=state.pipeline_key)
97 state.pipeline_key = key;
98 changed |= PIPELINE_KEY;
102 void Renderer::set_camera(const Camera &c)
104 get_state().camera = &c;
105 set_matrix(Matrix());
108 void Renderer::set_matrix(const Matrix &matrix)
110 get_state().model_matrix = matrix;
114 void Renderer::set_framebuffer(const Framebuffer *f)
116 get_state().framebuffer = f;
119 void Renderer::set_viewport(const Rect *v)
121 get_state().viewport = v;
124 void Renderer::set_scissor(const Rect *s)
126 get_state().scissor = s;
129 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
131 get_state().shprog = p;
136 void Renderer::add_shader_data(const ProgramData &d)
138 State &state = get_state();
140 if(state.shdata_count<shdata_stack.size())
142 const BoundProgramData &top = shdata_stack.back();
143 if(top.shdata==&d && top.generation==d.get_generation())
145 ++state.shdata_count;
151 shdata_stack.push_back(&d);
152 state.shdata_count = shdata_stack.size();
153 changed |= SHADER_DATA;
156 void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
158 set_texture(tag, tex, -1, samp);
161 void Renderer::set_texture(Tag tag, const Texture *tex, int level, const Sampler *samp)
163 State &state = get_state();
167 if(ResourceManager *res_mgr = tex->get_manager())
168 res_mgr->resource_used(*tex);
169 if(!tex->is_loaded())
170 tex = &placeholder_texture;
173 if(texture_stack.size()>state.texture_count)
175 BoundTexture &bt = texture_stack[state.texture_count];
176 if(bt.tag==tag && bt.texture==tex && bt.sampler==samp)
178 ++state.texture_count;
185 for(auto i=texture_stack.end(); i!=texture_stack.begin(); )
188 i->replaced = texture_stack.size();
192 texture_stack.emplace_back();
193 BoundTexture &bound_tex = texture_stack.back();
195 bound_tex.texture = tex;
196 bound_tex.sampler = samp;
197 bound_tex.level = level;
198 state.texture_count = texture_stack.size();
201 void Renderer::flush_shader_data()
203 const State &state = get_state();
205 if(shdata_stack.size()>state.shdata_count)
206 shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
209 void Renderer::flush_textures()
211 const State &state = get_state();
213 for(unsigned i=0; i<state.texture_count; ++i)
214 if(texture_stack[i].replaced>=static_cast<int>(state.texture_count))
215 texture_stack[i].replaced = -1;
217 texture_stack.erase(texture_stack.begin()+state.texture_count, texture_stack.end());
220 void Renderer::set_vertex_setup(const VertexSetup *vs)
222 get_state().vertex_setup = vs;
225 void Renderer::set_front_face(FaceWinding winding)
227 get_state().front_face = winding;
230 void Renderer::set_face_cull(CullMode cull)
232 get_state().face_cull = cull;
235 void Renderer::set_depth_test(const DepthTest *dt)
237 get_state().depth_test = dt;
240 void Renderer::set_stencil_test(const StencilTest *st)
242 get_state().stencil_test = st;
245 void Renderer::set_blend(const Blend *b)
247 get_state().blend = b;
250 void Renderer::set_object_lod_bias(unsigned b)
252 get_state().object_lod_bias = b;
255 void Renderer::clear(const ClearValue *values)
258 commands.use_pipeline(&get_pipeline_state());
259 commands.clear(values);
262 void Renderer::draw(const Batch &batch)
265 batch.refresh(frame_index);
266 PipelineState &ps = get_pipeline_state();
267 ps.set_primitive_type(batch.get_type());
268 commands.use_pipeline(&ps);
269 commands.draw(batch);
272 void Renderer::draw_instanced(const Batch &batch, unsigned count)
275 batch.refresh(frame_index);
276 PipelineState &ps = get_pipeline_state();
277 ps.set_primitive_type(batch.get_type());
278 commands.use_pipeline(&ps);
279 commands.draw_instanced(batch, count);
282 void Renderer::resolve_multisample(Framebuffer &target)
284 const State &state = get_state();
286 if(!state.framebuffer)
287 throw invalid_operation("Renderer::resolve_multisample");
289 unsigned width = state.framebuffer->get_width();
290 unsigned height = state.framebuffer->get_height();
291 if(target.get_width()!=width || target.get_height()!=height)
292 throw incompatible_data("Renderer::resolve_multisample");
295 commands.use_pipeline(&get_pipeline_state());
296 commands.resolve_multisample(target);
299 void Renderer::begin_query(const QueryPool &pool, unsigned index)
301 commands.begin_query(pool, index);
304 void Renderer::end_query(const QueryPool &pool, unsigned index)
306 commands.end_query(pool, index);
309 PipelineState &Renderer::get_pipeline_state()
311 if(changed&PIPELINE_KEY)
313 RendererBackend::set_pipeline_key(current_state->pipeline_key);
314 changed &= ~PIPELINE_KEY;
317 return RendererBackend::get_pipeline_state();
320 void Renderer::apply_framebuffer()
322 const State &state = get_state();
324 PipelineState &ps = get_pipeline_state();
326 ps.set_framebuffer(state.framebuffer);
327 static const Rect default_rect = Rect::max();
328 ps.set_viewport(state.viewport ? *state.viewport : default_rect);
329 ps.set_scissor(state.scissor ? *state.scissor : default_rect);
332 void Renderer::apply_state()
334 const State &state = get_state();
337 throw invalid_operation("Renderer::apply_state");
341 PipelineState &ps = get_pipeline_state();
342 bool pipeline_changed = (&ps!=last_pipeline);
345 bool shprog_changed = (state.shprog!=ps.get_shader_program());
346 ps.set_shader_program(state.shprog);
348 bool shdata_changed = changed&SHADER_DATA;
349 for(auto i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i)
350 shdata_changed = (i->shdata->get_generation()!=i->generation);
351 bool extra_shdata = (shdata_stack.size()>state.shdata_count);
355 shdata_changed = true;
361 standard_shdata.uniform("world_obj_matrix", state.model_matrix);
362 LinAl::SquareMatrix<float, 3> nm = state.model_matrix.block<3, 3>(0, 0);
363 nm = transpose(invert(nm));
364 standard_shdata.uniform("world_obj_normal_matrix", nm);
366 shdata_changed = true;
369 if(shdata_changed || shprog_changed || pipeline_changed || extra_shdata)
372 shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
373 standard_shdata.apply(*state.shprog, ps, frame_index);
375 state.camera->get_shader_data().apply(*state.shprog, ps, frame_index);
376 for(const BoundProgramData &d: shdata_stack)
378 d.shdata->apply(*state.shprog, ps, frame_index);
379 d.generation = d.shdata->get_generation();
381 changed &= ~SHADER_DATA;
384 if(state.vertex_setup)
386 if(const VertexArray *array = state.vertex_setup->get_vertex_array())
387 array->refresh(frame_index);
388 if(const VertexArray *array = state.vertex_setup->get_instance_array())
389 array->refresh(frame_index);
391 ps.set_vertex_setup(state.vertex_setup);
393 ps.set_front_face(state.front_face);
394 ps.set_face_cull(state.face_cull);
396 if(state.texture_count<texture_stack.size())
399 for(const BoundTexture &t: texture_stack)
400 if(t.texture && t.replaced<0)
402 if(t.binding<0 || shprog_changed)
403 t.binding = state.shprog->get_uniform_binding(t.tag);
405 ps.set_texture(t.binding, t.texture, t.level, t.sampler);
408 static const DepthTest default_depth_test;
409 ps.set_depth_test(state.depth_test ? *state.depth_test : default_depth_test);
410 static const StencilTest default_stencil_test;
411 ps.set_stencil_test(state.stencil_test ? *state.stencil_test : default_stencil_test);
412 static const Blend default_blend;
413 ps.set_blend(state.blend ? *state.blend : default_blend);
417 Renderer::BoundProgramData::BoundProgramData(const ProgramData *d):