]> git.tdb.fi Git - libs/gl.git/blobdiff - source/render/renderer.cpp
Store simpler states by value in PipelineState
[libs/gl.git] / source / render / renderer.cpp
index 6c8f1b67b29c0deb30161db2acfb19f6cffb2b79..0c5bf77454ed25c32151f2a44ed189723234a192 100644 (file)
@@ -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,28 +21,27 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-Renderer::Renderer()
+Renderer::Renderer():
+       placeholder_texture(Resources::get_global().get<Texture>("_placeholder.png"))
 {
        state_stack.reserve(16);
        shdata_stack.reserve(32);
        texture_stack.reserve(32);
 }
 
-Renderer::~Renderer()
-{
-}
-
 void Renderer::begin()
 {
        if(current_state)
                throw invalid_operation("Renderer::begin");
 
-       state_stack.push_back(State());
+       ++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()
@@ -141,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)
        {
@@ -167,11 +176,12 @@ void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
                        break;
                }
 
-       texture_stack.push_back(BoundTexture());
+       texture_stack.emplace_back();
        BoundTexture &bound_tex = texture_stack.back();
        bound_tex.tag = tag;
        bound_tex.texture = tex;
        bound_tex.sampler = samp;
+       bound_tex.level = level;
        state.texture_count = texture_stack.size();
 }
 
@@ -231,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);
 }
@@ -243,7 +249,8 @@ 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);
 }
@@ -251,7 +258,8 @@ 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);
 }
@@ -283,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();
@@ -290,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);
@@ -317,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;
@@ -326,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);
 
@@ -344,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);
 }