]> git.tdb.fi Git - libs/gl.git/blobdiff - source/render/renderer.cpp
Redesign depth and stencil test and blend state management
[libs/gl.git] / source / render / renderer.cpp
index d86b78046eb24454f954155f687d0c2ec9c710c0..68b7927dcb7067c0e3331c33f3f09bab1bf6a04a 100644 (file)
 #include "programdata.h"
 #include "renderable.h"
 #include "renderer.h"
+#include "resourcemanager.h"
 #include "sampler.h"
 #include "texture.h"
-#include "texunit.h"
 #include "vertexarray.h"
 #include "vertexsetup.h"
-#include "windingtest.h"
 
 using namespace std;
 
@@ -57,6 +56,10 @@ void Renderer::transform(const Matrix &matrix)
 
 void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
 {
+       if(tex)
+               if(ResourceManager *res_mgr = tex->get_manager())
+                       res_mgr->resource_used(*tex);
+
        if(texture_stack.size()>state->texture_count)
        {
                BoundTexture &bt = texture_stack[state->texture_count];
@@ -86,17 +89,9 @@ void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
 
 void Renderer::flush_textures()
 {
-       for(unsigned i=0; i<texture_stack.size(); ++i)
-       {
-               BoundTexture &bt = texture_stack[i];
-               if(i>=state->texture_count && bt.unit>=0)
-               {
-                       Texture::unbind_from(bt.unit);
-                       Sampler::unbind_from(bt.unit);
-               }
-               else if(bt.replaced>=static_cast<int>(state->texture_count))
-                       bt.replaced = -1;
-       }
+       for(unsigned i=0; i<state->texture_count; ++i)
+               if(texture_stack[i].replaced>=static_cast<int>(state->texture_count))
+                       texture_stack[i].replaced = -1;
 
        texture_stack.erase(texture_stack.begin()+state->texture_count, texture_stack.end());
 }
@@ -156,14 +151,29 @@ void Renderer::set_vertex_setup(const VertexSetup *vs)
        state->vertex_setup = vs;
 }
 
-void Renderer::set_winding_test(const WindingTest *w)
+void Renderer::set_front_face(FaceWinding winding)
+{
+       state->front_face = winding;
+}
+
+void Renderer::set_face_cull(CullMode cull)
+{
+       state->face_cull = cull;
+}
+
+void Renderer::set_depth_test(const DepthTest *dt)
 {
-       state->winding_test = w;
+       state->depth_test = dt;
 }
 
-void Renderer::set_reverse_winding(bool r)
+void Renderer::set_stencil_test(const StencilTest *st)
 {
-       state->reverse_winding = r;
+       state->stencil_test = st;
+}
+
+void Renderer::set_blend(const Blend *b)
+{
+       state->blend = b;
 }
 
 void Renderer::set_object_lod_bias(unsigned b)
@@ -194,19 +204,10 @@ void Renderer::end()
 
        *state = State();
        shdata_stack.clear();
+       add_shader_data(standard_shdata);
        excluded.clear();
 
-       for(vector<BoundTexture>::iterator i=texture_stack.begin(); i!=texture_stack.end(); ++i)
-               if(i->unit>=0)
-               {
-                       Texture::unbind_from(i->unit);
-                       Sampler::unbind_from(i->unit);
-               }
-       Clipping::unbind();
-       Program::unbind();
-       VertexSetup::unbind();
-       Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
-       WindingTest::unbind();
+       PipelineState::clear();
 }
 
 void Renderer::exclude(const Renderable &renderable)
@@ -244,40 +245,39 @@ void Renderer::apply_state()
        if(!state->shprog)
                throw invalid_operation("Renderer::apply_state");
 
-       /* We (mostly) let the objects themselves figure out if the binding has
-       changed */
+       if(changed&MATRIX)
+       {
+               standard_shdata.uniform("world_obj_matrix", state->model_matrix);
+               LinAl::SquareMatrix<float, 3> nm = state->model_matrix.block<3, 3>(0, 0);
+               nm = transpose(invert(nm));
+               standard_shdata.uniform("world_obj_normal_matrix", nm);
+               changed &= ~MATRIX;
+       }
 
-       if(state->texture_count<texture_stack.size())
-               flush_textures();
+       bool shprog_changed = (state->shprog!=pipeline_state.get_shader_program());
+       pipeline_state.set_shader_program(state->shprog);
 
-       for(vector<BoundTexture>::const_iterator i=texture_stack.begin(); i!=texture_stack.end(); ++i)
+       if(state->vertex_setup)
        {
-               int unit = state->shprog->get_uniform_binding(i->tag);
-               if(unit>=0)
-               {
-                       if(i->texture)
-                               i->texture->bind_to(unit);
-                       if(i->sampler)
-                               i->sampler->bind_to(unit);
-                       i->unit = unit;
-               }
+               if(const VertexArray *array = state->vertex_setup->get_vertex_array())
+                       array->refresh();
+               if(const VertexArray *array = state->vertex_setup->get_instance_array())
+                       array->refresh();
        }
+       pipeline_state.set_vertex_setup(state->vertex_setup);
 
-       if(state->clipping)
-               state->clipping->bind();
-       else
-               Clipping::unbind();
+       pipeline_state.set_front_face(state->front_face);
+       pipeline_state.set_face_cull(state->face_cull);
+       pipeline_state.set_enabled_clip_planes(state->clipping ? (1<<state->clipping->get_n_planes())-1 : 0);
 
-       bool shprog_changed = (state->shprog!=Program::current());
-       state->shprog->bind();
+       if(state->texture_count<texture_stack.size())
+               flush_textures();
 
-       if(changed&MATRIX)
+       for(vector<BoundTexture>::const_iterator i=texture_stack.begin(); i!=texture_stack.end(); ++i)
        {
-               standard_shdata.uniform("world_obj_matrix", state->model_matrix);
-               LinAl::SquareMatrix<float, 3> nm = state->model_matrix.block<3, 3>(0, 0);
-               nm = transpose(invert(nm));
-               standard_shdata.uniform("world_obj_normal_matrix", nm);
-               changed &= ~MATRIX;
+               int unit = (i->tag.id ? state->shprog->get_uniform_binding(i->tag) : i->unit);
+               if(unit>=0)
+                       pipeline_state.set_texture(unit, i->texture, i->sampler);
        }
 
        bool shdata_changed = changed&SHADER_DATA;
@@ -291,26 +291,17 @@ void Renderer::apply_state()
                        shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
                for(vector<BoundProgramData>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
                {
-                       i->shdata->apply();
+                       i->shdata->apply(*state->shprog, pipeline_state);
                        i->generation = i->shdata->get_generation();
                }
                changed &= ~SHADER_DATA;
        }
 
-       if(state->vertex_setup)
-               state->vertex_setup->bind();
-       else
-               VertexSetup::unbind();
+       pipeline_state.set_depth_test(state->depth_test);
+       pipeline_state.set_stencil_test(state->stencil_test);
+       pipeline_state.set_blend(state->blend);
 
-       if(state->winding_test)
-       {
-               if(state->reverse_winding)
-                       state->winding_test->get_reverse().bind();
-               else
-                       state->winding_test->bind();
-       }
-       else
-               WindingTest::unbind();
+       pipeline_state.apply();
 }
 
 
@@ -336,8 +327,11 @@ Renderer::State::State():
        shprog(0),
        shdata_count(0),
        vertex_setup(0),
-       winding_test(0),
-       reverse_winding(false),
+       front_face(NON_MANIFOLD),
+       face_cull(NO_CULL),
+       depth_test(0),
+       stencil_test(0),
+       blend(0),
        object_lod_bias(0)
 { }