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")),
29 default_sampler(Resources::get_global().get<Sampler>("_linear_clamp.samp"))
31 state_stack.reserve(16);
32 shdata_stack.reserve(32);
33 texture_stack.reserve(32);
36 void Renderer::begin()
39 throw invalid_operation("Renderer::begin");
42 state_stack.emplace_back();
43 current_state = &state_stack.back();
45 RendererBackend::begin();
47 commands.begin_frame(frame_index);
52 if(!current_state || state_stack.size()>1)
53 throw invalid_operation("Renderer::end");
55 RendererBackend::end();
59 texture_stack.clear();
63 void Renderer::push_state()
65 if(state_stack.empty())
66 throw invalid_operation("Renderer::push_state");
68 state_stack.push_back(state_stack.back());
69 current_state = &state_stack.back();
72 void Renderer::pop_state()
74 if(state_stack.size()==1)
75 throw stack_underflow("Renderer::pop_state");
77 uintptr_t old_pipeline = current_state->pipeline_key;
79 state_stack.pop_back();
80 current_state = &state_stack.back();
83 if(current_state->pipeline_key!=old_pipeline)
84 changed |= PIPELINE_KEY;
87 Renderer::State &Renderer::get_state() const
91 throw invalid_operation("Renderer::get_state");
93 return *current_state;
96 void Renderer::set_pipeline_key(uintptr_t key)
98 State &state = get_state();
99 if(key!=state.pipeline_key)
101 state.pipeline_key = key;
102 changed |= PIPELINE_KEY;
106 void Renderer::set_camera(const Camera &c)
108 get_state().camera = &c;
109 set_matrix(Matrix());
112 void Renderer::set_matrix(const Matrix &matrix)
114 get_state().model_matrix = matrix;
118 void Renderer::set_framebuffer(const Framebuffer *f)
120 get_state().framebuffer = f;
123 void Renderer::set_viewport(const Rect *v)
125 get_state().viewport = v;
128 void Renderer::set_scissor(const Rect *s)
130 get_state().scissor = s;
133 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
135 get_state().shprog = p;
140 void Renderer::add_shader_data(const ProgramData &d)
142 State &state = get_state();
144 if(state.shdata_count<shdata_stack.size())
146 const BoundProgramData &top = shdata_stack.back();
147 if(top.shdata==&d && top.generation==d.get_generation())
149 ++state.shdata_count;
155 shdata_stack.push_back(&d);
156 state.shdata_count = shdata_stack.size();
157 changed |= SHADER_DATA;
160 void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
162 set_texture(tag, tex, -1, samp);
165 void Renderer::set_texture(Tag tag, const Texture *tex, int level, const Sampler *samp)
167 State &state = get_state();
171 if(ResourceManager *res_mgr = tex->get_manager())
172 res_mgr->resource_used(*tex);
173 if(!tex->is_loaded())
174 tex = &placeholder_texture;
176 samp = &default_sampler;
181 set_resource(texture_stack, state.texture_count, tag, { tex, samp, level });
184 void Renderer::set_storage_texture(Tag tag, const Texture *tex)
186 State &state = get_state();
187 set_resource(texture_stack, state.texture_count, tag, { tex, 0, 0 });
191 void Renderer::set_resource(vector<BoundResource<T>> &stack, unsigned &count, Tag tag, const T &res)
193 if(stack.size()>count)
195 BoundResource<T> &top = stack[count];
196 if(top.tag==tag && top.resource==res)
202 flush_resources(stack, count);
205 for(auto i=stack.end(); i!=stack.begin(); )
208 i->replaced = stack.size();
212 stack.emplace_back();
213 BoundResource<T> &bound_res = stack.back();
215 bound_res.resource = res;
216 count = stack.size();
219 void Renderer::flush_shader_data()
221 const State &state = get_state();
223 if(shdata_stack.size()>state.shdata_count)
224 shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
228 void Renderer::flush_resources(vector<BoundResource<T>> &stack, unsigned &count)
230 for(unsigned i=0; i<count; ++i)
231 if(stack[i].replaced>=static_cast<int>(count))
232 stack[i].replaced = -1;
234 stack.erase(stack.begin()+count, stack.end());
237 void Renderer::set_vertex_setup(const VertexSetup *vs)
239 get_state().vertex_setup = vs;
242 void Renderer::set_front_face(FaceWinding winding)
244 get_state().front_face = winding;
247 void Renderer::set_face_cull(CullMode cull)
249 get_state().face_cull = cull;
252 void Renderer::set_depth_test(const DepthTest *dt)
254 get_state().depth_test = dt;
257 void Renderer::set_stencil_test(const StencilTest *st)
259 get_state().stencil_test = st;
262 void Renderer::set_blend(const Blend *b)
264 get_state().blend = b;
267 void Renderer::set_object_lod_bias(unsigned b)
269 get_state().object_lod_bias = b;
272 void Renderer::clear(const ClearValue *values)
275 commands.use_pipeline(&get_pipeline_state());
276 commands.clear(values);
279 void Renderer::draw(const Batch &batch)
282 batch.refresh(frame_index);
283 PipelineState &ps = get_pipeline_state();
284 ps.set_primitive_type(batch.get_type());
285 commands.use_pipeline(&ps);
286 commands.draw(batch);
289 void Renderer::draw_instanced(const Batch &batch, unsigned count)
292 batch.refresh(frame_index);
293 PipelineState &ps = get_pipeline_state();
294 ps.set_primitive_type(batch.get_type());
295 commands.use_pipeline(&ps);
296 commands.draw_instanced(batch, count);
299 void Renderer::dispatch(unsigned count_x, unsigned count_y, unsigned count_z)
302 PipelineState &ps = get_pipeline_state();
303 commands.use_pipeline(&ps);
304 commands.dispatch(count_x, count_y, count_z);
307 void Renderer::resolve_multisample()
309 const State &state = get_state();
311 if(!state.framebuffer)
312 throw invalid_operation("Renderer::resolve_multisample");
315 commands.use_pipeline(&get_pipeline_state());
316 commands.resolve_multisample();
319 void Renderer::begin_query(const QueryPool &pool, unsigned index)
321 commands.begin_query(pool, index);
324 void Renderer::end_query(const QueryPool &pool, unsigned index)
326 commands.end_query(pool, index);
329 PipelineState &Renderer::get_pipeline_state()
331 if(changed&PIPELINE_KEY)
333 RendererBackend::set_pipeline_key(current_state->pipeline_key);
334 changed &= ~PIPELINE_KEY;
337 return RendererBackend::get_pipeline_state();
340 void Renderer::apply_framebuffer()
342 const State &state = get_state();
344 PipelineState &ps = get_pipeline_state();
346 ps.set_framebuffer(state.framebuffer);
347 static const Rect default_rect = Rect::max();
348 ps.set_viewport(state.viewport ? *state.viewport : default_rect);
349 ps.set_scissor(state.scissor ? *state.scissor : default_rect);
352 void Renderer::apply_state()
354 State &state = get_state();
357 throw invalid_operation("Renderer::apply_state");
361 PipelineState &ps = get_pipeline_state();
362 bool pipeline_changed = (&ps!=last_pipeline);
365 bool shprog_changed = (state.shprog!=ps.get_shader_program());
366 ps.set_shader_program(state.shprog);
368 bool shdata_changed = changed&SHADER_DATA;
369 for(auto i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i)
370 shdata_changed = (i->shdata->get_generation()!=i->generation);
371 bool extra_shdata = (shdata_stack.size()>state.shdata_count);
375 shdata_changed = true;
381 standard_shdata.uniform(world_obj_matrix_tag, state.model_matrix);
382 LinAl::Matrix<float, 3, 3> nm = state.model_matrix.block<3, 3>(0, 0);
383 nm = transpose(invert(nm));
384 standard_shdata.uniform(world_obj_normal_matrix_tag, nm);
386 shdata_changed = true;
389 if(shdata_changed || shprog_changed || pipeline_changed || extra_shdata)
392 shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
393 standard_shdata.apply(*state.shprog, ps, frame_index);
395 state.camera->get_shader_data().apply(*state.shprog, ps, frame_index);
396 for(const BoundProgramData &d: shdata_stack)
398 d.shdata->apply(*state.shprog, ps, frame_index);
399 d.generation = d.shdata->get_generation();
401 changed &= ~SHADER_DATA;
404 if(state.vertex_setup)
406 if(const VertexArray *array = state.vertex_setup->get_vertex_array())
407 array->refresh(frame_index);
408 if(const VertexArray *array = state.vertex_setup->get_instance_array())
409 array->refresh(frame_index);
411 ps.set_vertex_setup(state.vertex_setup);
413 ps.set_front_face(state.front_face);
414 ps.set_face_cull(state.face_cull);
416 if(state.texture_count<texture_stack.size())
417 flush_resources(texture_stack, state.texture_count);
419 for(const BoundResource<SampledTexture> &t: texture_stack)
420 if(t.resource.texture && t.replaced<0)
422 if(t.binding<0 || shprog_changed)
423 t.binding = state.shprog->get_uniform_binding(t.tag);
426 if(t.resource.sampler)
427 ps.set_texture(t.binding, t.resource.texture, t.resource.level, t.resource.sampler);
429 ps.set_storage_texture(t.binding, t.resource.texture);
433 static const DepthTest default_depth_test;
434 ps.set_depth_test(state.depth_test ? *state.depth_test : default_depth_test);
435 static const StencilTest default_stencil_test;
436 ps.set_stencil_test(state.stencil_test ? *state.stencil_test : default_stencil_test);
437 static const Blend default_blend;
438 ps.set_blend(state.blend ? *state.blend : default_blend);
442 Renderer::BoundProgramData::BoundProgramData(const ProgramData *d):