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 });
185 void Renderer::set_resource(vector<BoundResource<T>> &stack, unsigned &count, Tag tag, const T &res)
187 if(stack.size()>count)
189 BoundResource<T> &top = stack[count];
190 if(top.tag==tag && top.resource==res)
196 flush_resources(stack, count);
199 for(auto i=stack.end(); i!=stack.begin(); )
202 i->replaced = stack.size();
206 stack.emplace_back();
207 BoundResource<T> &bound_res = stack.back();
209 bound_res.resource = res;
210 count = stack.size();
213 void Renderer::flush_shader_data()
215 const State &state = get_state();
217 if(shdata_stack.size()>state.shdata_count)
218 shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
222 void Renderer::flush_resources(vector<BoundResource<T>> &stack, unsigned &count)
224 for(unsigned i=0; i<count; ++i)
225 if(stack[i].replaced>=static_cast<int>(count))
226 stack[i].replaced = -1;
228 stack.erase(stack.begin()+count, stack.end());
231 void Renderer::set_vertex_setup(const VertexSetup *vs)
233 get_state().vertex_setup = vs;
236 void Renderer::set_front_face(FaceWinding winding)
238 get_state().front_face = winding;
241 void Renderer::set_face_cull(CullMode cull)
243 get_state().face_cull = cull;
246 void Renderer::set_depth_test(const DepthTest *dt)
248 get_state().depth_test = dt;
251 void Renderer::set_stencil_test(const StencilTest *st)
253 get_state().stencil_test = st;
256 void Renderer::set_blend(const Blend *b)
258 get_state().blend = b;
261 void Renderer::set_object_lod_bias(unsigned b)
263 get_state().object_lod_bias = b;
266 void Renderer::clear(const ClearValue *values)
269 commands.use_pipeline(&get_pipeline_state());
270 commands.clear(values);
273 void Renderer::draw(const Batch &batch)
276 batch.refresh(frame_index);
277 PipelineState &ps = get_pipeline_state();
278 ps.set_primitive_type(batch.get_type());
279 commands.use_pipeline(&ps);
280 commands.draw(batch);
283 void Renderer::draw_instanced(const Batch &batch, unsigned count)
286 batch.refresh(frame_index);
287 PipelineState &ps = get_pipeline_state();
288 ps.set_primitive_type(batch.get_type());
289 commands.use_pipeline(&ps);
290 commands.draw_instanced(batch, count);
293 void Renderer::resolve_multisample(Framebuffer &target)
295 const State &state = get_state();
297 if(!state.framebuffer)
298 throw invalid_operation("Renderer::resolve_multisample");
300 unsigned width = state.framebuffer->get_width();
301 unsigned height = state.framebuffer->get_height();
302 if(target.get_width()!=width || target.get_height()!=height)
303 throw incompatible_data("Renderer::resolve_multisample");
306 commands.use_pipeline(&get_pipeline_state());
307 commands.resolve_multisample(target);
310 void Renderer::begin_query(const QueryPool &pool, unsigned index)
312 commands.begin_query(pool, index);
315 void Renderer::end_query(const QueryPool &pool, unsigned index)
317 commands.end_query(pool, index);
320 PipelineState &Renderer::get_pipeline_state()
322 if(changed&PIPELINE_KEY)
324 RendererBackend::set_pipeline_key(current_state->pipeline_key);
325 changed &= ~PIPELINE_KEY;
328 return RendererBackend::get_pipeline_state();
331 void Renderer::apply_framebuffer()
333 const State &state = get_state();
335 PipelineState &ps = get_pipeline_state();
337 ps.set_framebuffer(state.framebuffer);
338 static const Rect default_rect = Rect::max();
339 ps.set_viewport(state.viewport ? *state.viewport : default_rect);
340 ps.set_scissor(state.scissor ? *state.scissor : default_rect);
343 void Renderer::apply_state()
345 State &state = get_state();
348 throw invalid_operation("Renderer::apply_state");
352 PipelineState &ps = get_pipeline_state();
353 bool pipeline_changed = (&ps!=last_pipeline);
356 bool shprog_changed = (state.shprog!=ps.get_shader_program());
357 ps.set_shader_program(state.shprog);
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);
366 shdata_changed = true;
372 standard_shdata.uniform(world_obj_matrix_tag, state.model_matrix);
373 LinAl::Matrix<float, 3, 3> nm = state.model_matrix.block<3, 3>(0, 0);
374 nm = transpose(invert(nm));
375 standard_shdata.uniform(world_obj_normal_matrix_tag, nm);
377 shdata_changed = true;
380 if(shdata_changed || shprog_changed || pipeline_changed || extra_shdata)
383 shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
384 standard_shdata.apply(*state.shprog, ps, frame_index);
386 state.camera->get_shader_data().apply(*state.shprog, ps, frame_index);
387 for(const BoundProgramData &d: shdata_stack)
389 d.shdata->apply(*state.shprog, ps, frame_index);
390 d.generation = d.shdata->get_generation();
392 changed &= ~SHADER_DATA;
395 if(state.vertex_setup)
397 if(const VertexArray *array = state.vertex_setup->get_vertex_array())
398 array->refresh(frame_index);
399 if(const VertexArray *array = state.vertex_setup->get_instance_array())
400 array->refresh(frame_index);
402 ps.set_vertex_setup(state.vertex_setup);
404 ps.set_front_face(state.front_face);
405 ps.set_face_cull(state.face_cull);
407 if(state.texture_count<texture_stack.size())
408 flush_resources(texture_stack, state.texture_count);
410 for(const BoundResource<SampledTexture> &t: texture_stack)
411 if(t.resource.texture && t.replaced<0)
413 if(t.binding<0 || shprog_changed)
414 t.binding = state.shprog->get_uniform_binding(t.tag);
416 ps.set_texture(t.binding, t.resource.texture, t.resource.level, t.resource.sampler);
419 static const DepthTest default_depth_test;
420 ps.set_depth_test(state.depth_test ? *state.depth_test : default_depth_test);
421 static const StencilTest default_stencil_test;
422 ps.set_stencil_test(state.stencil_test ? *state.stencil_test : default_stencil_test);
423 static const Blend default_blend;
424 ps.set_blend(state.blend ? *state.blend : default_blend);
428 Renderer::BoundProgramData::BoundProgramData(const ProgramData *d):