X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Frender%2Frenderer.cpp;h=50d3887369a30cbcdf6b8b1f99e83ea33ca08a94;hb=94cadd1618f93239b1cb0acbd4f958257c035c98;hp=4fb2b0c79bc01dc8dcbf6fbc58b79aea98b0f441;hpb=f1e31ee641f0cafdfe2015bff206b9ee2ad85abf;p=libs%2Fgl.git diff --git a/source/render/renderer.cpp b/source/render/renderer.cpp index 4fb2b0c7..50d38873 100644 --- a/source/render/renderer.cpp +++ b/source/render/renderer.cpp @@ -10,6 +10,7 @@ #include "renderable.h" #include "renderer.h" #include "resourcemanager.h" +#include "resources.h" #include "sampler.h" #include "texture.h" #include "vertexarray.h" @@ -20,7 +21,12 @@ using namespace std; namespace Msp { namespace GL { -Renderer::Renderer() +const Tag Renderer::world_obj_matrix_tag("world_obj_matrix"); +const Tag Renderer::world_obj_normal_matrix_tag("world_obj_normal_matrix"); + +Renderer::Renderer(): + placeholder_texture(Resources::get_global().get("_placeholder.png")), + default_sampler(Resources::get_global().get("_linear_clamp.samp")) { state_stack.reserve(16); shdata_stack.reserve(32); @@ -32,12 +38,13 @@ void Renderer::begin() if(current_state) throw invalid_operation("Renderer::begin"); + ++frame_index; state_stack.emplace_back(); current_state = &state_stack.back(); RendererBackend::begin(); - add_shader_data(standard_shdata); + commands.begin_frame(frame_index); } void Renderer::end() @@ -67,9 +74,14 @@ void Renderer::pop_state() if(state_stack.size()==1) throw stack_underflow("Renderer::pop_state"); + uintptr_t old_pipeline = current_state->pipeline_key; + state_stack.pop_back(); current_state = &state_stack.back(); changed |= MATRIX; + + if(current_state->pipeline_key!=old_pipeline) + changed |= PIPELINE_KEY; } Renderer::State &Renderer::get_state() const @@ -81,10 +93,19 @@ Renderer::State &Renderer::get_state() const return *current_state; } +void Renderer::set_pipeline_key(uintptr_t key) +{ + State &state = get_state(); + if(key!=state.pipeline_key) + { + state.pipeline_key = key; + changed |= PIPELINE_KEY; + } +} + void Renderer::set_camera(const Camera &c) { get_state().camera = &c; - add_shader_data(c.get_shader_data()); set_matrix(Matrix()); } @@ -137,38 +158,62 @@ void Renderer::add_shader_data(const ProgramData &d) } void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp) +{ + set_texture(tag, tex, -1, samp); +} + +void Renderer::set_texture(Tag tag, const Texture *tex, int level, const Sampler *samp) { State &state = get_state(); if(tex) + { if(ResourceManager *res_mgr = tex->get_manager()) res_mgr->resource_used(*tex); + if(!tex->is_loaded()) + tex = &placeholder_texture; + if(!samp) + samp = &default_sampler; + } + else + samp = 0; - if(texture_stack.size()>state.texture_count) + set_resource(texture_stack, state.texture_count, tag, { tex, samp, level }); +} + +void Renderer::set_storage_texture(Tag tag, const Texture *tex) +{ + State &state = get_state(); + set_resource(texture_stack, state.texture_count, tag, { tex, 0, 0 }); +} + +template +void Renderer::set_resource(vector> &stack, unsigned &count, Tag tag, const T &res) +{ + if(stack.size()>count) { - BoundTexture &bt = texture_stack[state.texture_count]; - if(bt.tag==tag && bt.texture==tex && bt.sampler==samp) + BoundResource &top = stack[count]; + if(top.tag==tag && top.resource==res) { - ++state.texture_count; + ++count; return; } else - flush_textures(); + flush_resources(stack, count); } - for(auto i=texture_stack.end(); i!=texture_stack.begin(); ) + for(auto i=stack.end(); i!=stack.begin(); ) if((--i)->tag==tag) { - i->replaced = texture_stack.size(); + i->replaced = stack.size(); break; } - texture_stack.emplace_back(); - BoundTexture &bound_tex = texture_stack.back(); - bound_tex.tag = tag; - bound_tex.texture = tex; - bound_tex.sampler = samp; - state.texture_count = texture_stack.size(); + stack.emplace_back(); + BoundResource &bound_res = stack.back(); + bound_res.tag = tag; + bound_res.resource = res; + count = stack.size(); } void Renderer::flush_shader_data() @@ -179,15 +224,14 @@ void Renderer::flush_shader_data() shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end()); } -void Renderer::flush_textures() +template +void Renderer::flush_resources(vector> &stack, unsigned &count) { - const State &state = get_state(); + for(unsigned i=0; i=static_cast(count)) + stack[i].replaced = -1; - for(unsigned i=0; i=static_cast(state.texture_count)) - texture_stack[i].replaced = -1; - - texture_stack.erase(texture_stack.begin()+state.texture_count, texture_stack.end()); + stack.erase(stack.begin()+count, stack.end()); } void Renderer::set_vertex_setup(const VertexSetup *vs) @@ -227,48 +271,49 @@ void Renderer::set_object_lod_bias(unsigned b) void Renderer::clear(const ClearValue *values) { - const State &state = get_state(); - - pipeline_state.set_framebuffer(state.framebuffer); - pipeline_state.set_viewport(state.viewport); - pipeline_state.set_scissor(state.scissor); - commands.use_pipeline(&pipeline_state); + apply_framebuffer(); + commands.use_pipeline(&get_pipeline_state()); commands.clear(values); } void Renderer::draw(const Batch &batch) { apply_state(); - batch.refresh(); - pipeline_state.set_primitive_type(batch.get_type()); - commands.use_pipeline(&pipeline_state); + batch.refresh(frame_index); + PipelineState &ps = get_pipeline_state(); + ps.set_primitive_type(batch.get_type()); + commands.use_pipeline(&ps); commands.draw(batch); } void Renderer::draw_instanced(const Batch &batch, unsigned count) { apply_state(); - batch.refresh(); - pipeline_state.set_primitive_type(batch.get_type()); - commands.use_pipeline(&pipeline_state); + batch.refresh(frame_index); + PipelineState &ps = get_pipeline_state(); + ps.set_primitive_type(batch.get_type()); + commands.use_pipeline(&ps); commands.draw_instanced(batch, count); } -void Renderer::resolve_multisample(Framebuffer &target) +void Renderer::dispatch(unsigned count_x, unsigned count_y, unsigned count_z) +{ + apply_state(); + PipelineState &ps = get_pipeline_state(); + commands.use_pipeline(&ps); + commands.dispatch(count_x, count_y, count_z); +} + +void Renderer::resolve_multisample() { const State &state = get_state(); if(!state.framebuffer) throw invalid_operation("Renderer::resolve_multisample"); - unsigned width = state.framebuffer->get_width(); - unsigned height = state.framebuffer->get_height(); - if(target.get_width()!=width || target.get_height()!=height) - throw incompatible_data("Renderer::resolve_multisample"); - - pipeline_state.set_framebuffer(state.framebuffer); - commands.use_pipeline(&pipeline_state); - commands.resolve_multisample(target); + apply_framebuffer(); + commands.use_pipeline(&get_pipeline_state()); + commands.resolve_multisample(); } void Renderer::begin_query(const QueryPool &pool, unsigned index) @@ -281,41 +326,76 @@ void Renderer::end_query(const QueryPool &pool, unsigned index) commands.end_query(pool, index); } -void Renderer::apply_state() +PipelineState &Renderer::get_pipeline_state() +{ + if(changed&PIPELINE_KEY) + { + RendererBackend::set_pipeline_key(current_state->pipeline_key); + changed &= ~PIPELINE_KEY; + } + + return RendererBackend::get_pipeline_state(); +} + +void Renderer::apply_framebuffer() { const State &state = get_state(); + PipelineState &ps = get_pipeline_state(); + + ps.set_framebuffer(state.framebuffer); + static const Rect default_rect = Rect::max(); + ps.set_viewport(state.viewport ? *state.viewport : default_rect); + ps.set_scissor(state.scissor ? *state.scissor : default_rect); +} + +void Renderer::apply_state() +{ + State &state = get_state(); + if(!state.shprog) throw invalid_operation("Renderer::apply_state"); - pipeline_state.set_framebuffer(state.framebuffer); - pipeline_state.set_viewport(state.viewport); - pipeline_state.set_scissor(state.scissor); + apply_framebuffer(); - bool shprog_changed = (state.shprog!=pipeline_state.get_shader_program()); - pipeline_state.set_shader_program(state.shprog); + PipelineState &ps = get_pipeline_state(); + bool pipeline_changed = (&ps!=last_pipeline); + last_pipeline = &ps; - if(changed&MATRIX) - { - standard_shdata.uniform("world_obj_matrix", state.model_matrix); - LinAl::SquareMatrix nm = state.model_matrix.block<3, 3>(0, 0); - nm = transpose(invert(nm)); - standard_shdata.uniform("world_obj_normal_matrix", nm); - changed &= ~MATRIX; - } + bool shprog_changed = (state.shprog!=ps.get_shader_program()); + ps.set_shader_program(state.shprog); bool shdata_changed = changed&SHADER_DATA; for(auto i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i) shdata_changed = (i->shdata->get_generation()!=i->generation); bool extra_shdata = (shdata_stack.size()>state.shdata_count); - if(shdata_changed || shprog_changed || extra_shdata) + if(changed&CAMERA) + { + shdata_changed = true; + changed &= ~CAMERA; + } + + if(changed&MATRIX) + { + standard_shdata.uniform(world_obj_matrix_tag, state.model_matrix); + LinAl::Matrix nm = state.model_matrix.block<3, 3>(0, 0); + nm = transpose(invert(nm)); + standard_shdata.uniform(world_obj_normal_matrix_tag, nm); + changed &= ~MATRIX; + shdata_changed = true; + } + + if(shdata_changed || shprog_changed || pipeline_changed || extra_shdata) { if(extra_shdata) shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end()); + standard_shdata.apply(*state.shprog, ps, frame_index); + if(state.camera) + state.camera->get_shader_data().apply(*state.shprog, ps, frame_index); for(const BoundProgramData &d: shdata_stack) { - d.shdata->apply(*state.shprog, pipeline_state); + d.shdata->apply(*state.shprog, ps, frame_index); d.generation = d.shdata->get_generation(); } changed &= ~SHADER_DATA; @@ -324,30 +404,38 @@ void Renderer::apply_state() if(state.vertex_setup) { if(const VertexArray *array = state.vertex_setup->get_vertex_array()) - array->refresh(); + array->refresh(frame_index); if(const VertexArray *array = state.vertex_setup->get_instance_array()) - array->refresh(); + array->refresh(frame_index); } - pipeline_state.set_vertex_setup(state.vertex_setup); + ps.set_vertex_setup(state.vertex_setup); - pipeline_state.set_front_face(state.front_face); - pipeline_state.set_face_cull(state.face_cull); + ps.set_front_face(state.front_face); + ps.set_face_cull(state.face_cull); if(state.texture_count &t: texture_stack) + if(t.resource.texture && t.replaced<0) { if(t.binding<0 || shprog_changed) t.binding = state.shprog->get_uniform_binding(t.tag); if(t.binding>=0) - pipeline_state.set_texture(t.binding, t.texture, t.sampler); + { + if(t.resource.sampler) + ps.set_texture(t.binding, t.resource.texture, t.resource.level, t.resource.sampler); + else + ps.set_storage_texture(t.binding, t.resource.texture); + } } - pipeline_state.set_depth_test(state.depth_test); - pipeline_state.set_stencil_test(state.stencil_test); - pipeline_state.set_blend(state.blend); + static const DepthTest default_depth_test; + ps.set_depth_test(state.depth_test ? *state.depth_test : default_depth_test); + static const StencilTest default_stencil_test; + ps.set_stencil_test(state.stencil_test ? *state.stencil_test : default_stencil_test); + static const Blend default_blend; + ps.set_blend(state.blend ? *state.blend : default_blend); }