X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Frender%2Frenderer.cpp;h=0c5bf77454ed25c32151f2a44ed189723234a192;hb=016f0f0dd51511f98d0bf398d99199d7dec1543c;hp=4fb2b0c79bc01dc8dcbf6fbc58b79aea98b0f441;hpb=f1e31ee641f0cafdfe2015bff206b9ee2ad85abf;p=libs%2Fgl.git diff --git a/source/render/renderer.cpp b/source/render/renderer.cpp index 4fb2b0c7..0c5bf774 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,8 @@ using namespace std; namespace Msp { namespace GL { -Renderer::Renderer() +Renderer::Renderer(): + placeholder_texture(Resources::get_global().get("_placeholder.png")) { state_stack.reserve(16); shdata_stack.reserve(32); @@ -32,12 +34,14 @@ 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() @@ -137,12 +141,21 @@ 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(texture_stack.size()>state.texture_count) { @@ -168,6 +181,7 @@ void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp) bound_tex.tag = tag; bound_tex.texture = tex; bound_tex.sampler = samp; + bound_tex.level = level; state.texture_count = texture_stack.size(); } @@ -227,11 +241,7 @@ 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); + apply_framebuffer(); commands.use_pipeline(&pipeline_state); commands.clear(values); } @@ -239,7 +249,7 @@ void Renderer::clear(const ClearValue *values) void Renderer::draw(const Batch &batch) { apply_state(); - batch.refresh(); + batch.refresh(frame_index); pipeline_state.set_primitive_type(batch.get_type()); commands.use_pipeline(&pipeline_state); commands.draw(batch); @@ -248,7 +258,7 @@ void Renderer::draw(const Batch &batch) void Renderer::draw_instanced(const Batch &batch, unsigned count) { apply_state(); - batch.refresh(); + batch.refresh(frame_index); pipeline_state.set_primitive_type(batch.get_type()); commands.use_pipeline(&pipeline_state); commands.draw_instanced(batch, count); @@ -281,6 +291,16 @@ void Renderer::end_query(const QueryPool &pool, unsigned index) commands.end_query(pool, index); } +void Renderer::apply_framebuffer() +{ + const State &state = get_state(); + + pipeline_state.set_framebuffer(state.framebuffer); + static const Rect default_rect = Rect::max(); + pipeline_state.set_viewport(state.viewport ? *state.viewport : default_rect); + pipeline_state.set_scissor(state.scissor ? *state.scissor : default_rect); +} + void Renderer::apply_state() { const State &state = get_state(); @@ -288,9 +308,7 @@ void Renderer::apply_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); @@ -315,7 +333,7 @@ void Renderer::apply_state() shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end()); for(const BoundProgramData &d: shdata_stack) { - d.shdata->apply(*state.shprog, pipeline_state); + d.shdata->apply(*state.shprog, pipeline_state, frame_index); d.generation = d.shdata->get_generation(); } changed &= ~SHADER_DATA; @@ -324,9 +342,9 @@ 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); @@ -342,12 +360,15 @@ void Renderer::apply_state() 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); + pipeline_state.set_texture(t.binding, t.texture, t.level, t.sampler); } - 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; + pipeline_state.set_depth_test(state.depth_test ? *state.depth_test : default_depth_test); + static const StencilTest default_stencil_test; + pipeline_state.set_stencil_test(state.stencil_test ? *state.stencil_test : default_stencil_test); + static const Blend default_blend; + pipeline_state.set_blend(state.blend ? *state.blend : default_blend); }