]> git.tdb.fi Git - libs/gl.git/blobdiff - source/renderer.cpp
Remove the deprecated ProgramBuilder class
[libs/gl.git] / source / renderer.cpp
index 90ac455107ffc078a6db1dbe8e452ea06ed44f7a..416a0fc89ced4827147982f234ca009fb94576c0 100644 (file)
@@ -5,11 +5,11 @@
 #include "error.h"
 #include "lighting.h"
 #include "material.h"
-#include "mesh.h"
 #include "program.h"
 #include "programdata.h"
 #include "renderable.h"
 #include "renderer.h"
+#include "sampler.h"
 #include "texture.h"
 #include "texturing.h"
 #include "texunit.h"
@@ -22,35 +22,35 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
+Renderer::Renderer():
+       default_camera(0)
+{
+       init();
+}
+
 Renderer::Renderer(const Camera *c):
-       default_camera(c),
-       changed(0),
-       matrices_loaded(false),
-       state_stack(1)
+       default_camera(c)
 {
-       state_stack.reserve(16);
-       shdata_stack.reserve(32);
-       state = &state_stack.back();
+       init();
 
        if(c)
                set_camera(*c);
-       else
-       {
-               standard_shdata.uniform("projection_matrix", Matrix());
-               standard_shdata.uniform("eye_world_matrix", Matrix());
-       }
 }
 
-Renderer::~Renderer()
+void Renderer::init()
 {
-       end();
+       state_stack.reserve(16);
+       state_stack.push_back(State());
+       shdata_stack.reserve(32);
+       state = &state_stack.back();
+
+       standard_shdata.uniform("projection_matrix", Matrix());
+       standard_shdata.uniform("eye_world_matrix", Matrix());
 }
 
-void Renderer::begin(const Camera *c)
+Renderer::~Renderer()
 {
        end();
-       if(c)
-               set_camera(*c);
 }
 
 void Renderer::set_camera(const Camera &c)
@@ -58,7 +58,7 @@ void Renderer::set_camera(const Camera &c)
        state->camera = &c;
        standard_shdata.uniform("projection_matrix", state->camera->get_projection_matrix());
        standard_shdata.uniform("eye_world_matrix", state->camera->get_view_matrix());
-       changed |= STANDARD_SHDATA|LEGACY_PROJECTION;
+       changed |= STANDARD_SHDATA;
        set_matrix(state->camera->get_view_matrix());
 }
 
@@ -74,9 +74,10 @@ void Renderer::transform(const Matrix &matrix)
        changed |= MATRIX;
 }
 
-void Renderer::set_texture(const Texture *t)
+void Renderer::set_texture(const Texture *t, const Sampler *s)
 {
        state->texture = t;
+       state->sampler = s;
        state->texturing = 0;
 }
 
@@ -84,6 +85,7 @@ void Renderer::set_texturing(const Texturing *t)
 {
        state->texturing = t;
        state->texture = 0;
+       state->sampler = 0;
 }
 
 unsigned Renderer::allocate_effect_texunit()
@@ -106,7 +108,6 @@ void Renderer::set_lighting(const Lighting *l)
                l->update_shader_data(standard_shdata, state->lighting_matrix);
                changed |= STANDARD_SHDATA;
        }
-       changed |= LEGACY_LIGHTING;
 }
 
 void Renderer::set_clipping(const Clipping *c)
@@ -118,7 +119,6 @@ void Renderer::set_clipping(const Clipping *c)
                c->update_shader_data(standard_shdata, state->clipping_matrix);
                changed |= STANDARD_SHDATA;
        }
-       changed |= LEGACY_CLIPPING;
 }
 
 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
@@ -134,17 +134,17 @@ void Renderer::add_shader_data(const ProgramData &d)
                ++state->shdata_count;
        else
        {
-               if(shdata_stack.size()>state->shdata_count)
-                       shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
+               flush_shader_data();
                shdata_stack.push_back(&d);
                state->shdata_count = shdata_stack.size();
                changed |= SHADER_DATA;
        }
 }
 
-void Renderer::set_mesh(const Mesh *m)
+void Renderer::flush_shader_data()
 {
-       state->mesh = m;
+       if(shdata_stack.size()>state->shdata_count)
+               shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
 }
 
 void Renderer::set_vertex_setup(const VertexSetup *vs)
@@ -197,7 +197,7 @@ void Renderer::pop_state()
                        standard_shdata.uniform("projection_matrix", Matrix());
                        standard_shdata.uniform("eye_world_matrix", Matrix());
                }
-               changed |= STANDARD_SHDATA|LEGACY_PROJECTION;
+               changed |= STANDARD_SHDATA;
        }
        /* This actually should compare the relevant matrices rather than check for
        camera, but in practice lighting and clipping is set right after the camera
@@ -209,7 +209,6 @@ void Renderer::pop_state()
                        state->lighting->update_shader_data(standard_shdata, state->lighting_matrix);
                        changed |= STANDARD_SHDATA;
                }
-               changed |= LEGACY_LIGHTING;
        }
        if(state->clipping!=old_clipping || camera_changed)
        {
@@ -218,7 +217,6 @@ void Renderer::pop_state()
                        state->clipping->update_shader_data(standard_shdata, state->clipping_matrix);
                        changed |= STANDARD_SHDATA;
                }
-               changed |= LEGACY_CLIPPING;
        }
 }
 
@@ -235,23 +233,13 @@ void Renderer::end()
        shdata_stack.clear();
        excluded.clear();
 
-       Mesh::unbind();
        Texturing::unbind();
        Texture::unbind_from(0);
-       Material::unbind();
-       Lighting::unbind();
        Clipping::unbind();
        Program::unbind();
        VertexSetup::unbind();
        Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
        WindingTest::unbind();
-
-       if(matrices_loaded)
-       {
-               MatrixStack::projection().pop();
-               MatrixStack::modelview().pop();
-               matrices_loaded = false;
-       }
 }
 
 void Renderer::exclude(const Renderable &renderable)
@@ -286,127 +274,74 @@ void Renderer::draw_instanced(const Batch &batch, unsigned count)
 
 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 */
 
-       bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
-       bool legacy_textures = !state->shprog;
-
        if(state->texturing)
-               state->texturing->bind(legacy_textures);
+               state->texturing->bind();
        else
        {
                Texturing::unbind();
                if(state->texture)
-                       state->texture->bind_to(0, legacy_textures);
-               else
-                       Texture::unbind_from(0);
-       }
-
-       if(legacy_bindings)
-       {
-               if(state->material)
-                       state->material->bind();
-               else
-                       Material::unbind();
-
-               if(changed&LEGACY_LIGHTING)
                {
-                       if(state->lighting)
-                       {
-                               MatrixStack::modelview() = state->lighting_matrix;
-                               state->lighting->bind();
-                               changed = (changed&~LEGACY_LIGHTING)|LEGACY_MATRIX;
-                       }
+                       if(state->sampler)
+                               state->sampler->bind_to(0);
                        else
-                               Lighting::unbind();
+                               Sampler::unbind_from(0);
+                       state->texture->bind_to(0);
                }
+               else
+                       Texture::unbind_from(0);
        }
 
        if(state->clipping)
-       {
-               if(legacy_bindings)
-               {
-                       if(changed&LEGACY_CLIPPING)
-                       {
-                               MatrixStack::modelview() = state->clipping_matrix;
-                               state->clipping->bind(true);
-                               changed = (changed&~LEGACY_CLIPPING)|LEGACY_MATRIX;
-                       }
-               }
-               else
-                       state->clipping->bind(false);
-       }
+               state->clipping->bind();
        else
                Clipping::unbind();
 
-       if(state->shprog)
-       {
-               bool shprog_changed = (state->shprog!=Program::current());
-               state->shprog->bind();
+       bool shprog_changed = (state->shprog!=Program::current());
+       state->shprog->bind();
 
-               if(!legacy_bindings)
-               {
-                       if(changed&MODERN_MATRIX)
-                       {
-                               standard_shdata.uniform("eye_obj_matrix", state->modelview_matrix);
-                               LinAl::SquareMatrix<float, 3> nm = state->modelview_matrix.block<3, 3>(0, 0);
-                               nm = transpose(invert(nm));
-                               standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
-                               changed = (changed&~MODERN_MATRIX)|STANDARD_SHDATA;
-                       }
-
-                       if(state->material && ((changed&MATERIAL_SHDATA) || shprog_changed))
-                       {
-                               state->material->get_shader_data().apply();
-                               changed &= ~MATERIAL_SHDATA;
-                       }
-
-                       if((changed&STANDARD_SHDATA) || shprog_changed)
-                       {
-                               standard_shdata.apply();
-                               changed &= ~STANDARD_SHDATA;
-                       }
-               }
-
-               bool extra_shdata = (shdata_stack.size()>state->shdata_count);
-
-               if((changed&SHADER_DATA) || shprog_changed || extra_shdata)
-               {
-                       if(extra_shdata)
-                               shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
-                       for(vector<const ProgramData *>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
-                               (*i)->apply();
-                       changed &= ~SHADER_DATA;
-               }
+       if(changed&MATRIX)
+       {
+               standard_shdata.uniform("eye_obj_matrix", state->modelview_matrix);
+               LinAl::SquareMatrix<float, 3> nm = state->modelview_matrix.block<3, 3>(0, 0);
+               nm = transpose(invert(nm));
+               standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
+               changed = (changed&~MATRIX)|STANDARD_SHDATA;
        }
-       else
-               Program::unbind();
 
-       if(state->mesh)
+       if(state->material && ((changed&MATERIAL_SHDATA) || shprog_changed))
        {
-               if(legacy_bindings)
-               {
-                       Mesh::unbind();
-                       state->mesh->get_vertices().apply();
-                       if(const Buffer *ibuf = state->mesh->get_index_buffer())
-                               ibuf->bind_to(ELEMENT_ARRAY_BUFFER);
-                       else
-                               Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
-               }
-               else
-                       state->mesh->bind();
+               state->material->get_shader_data().apply();
+               changed &= ~MATERIAL_SHDATA;
        }
-       else
+
+       if((changed&STANDARD_SHDATA) || shprog_changed)
        {
-               Mesh::unbind();
+               standard_shdata.apply();
+               changed &= ~STANDARD_SHDATA;
+       }
 
-               if(state->vertex_setup)
-                       state->vertex_setup->bind();
-               else
-                       VertexSetup::unbind();
+       bool extra_shdata = (shdata_stack.size()>state->shdata_count);
+
+       if((changed&SHADER_DATA) || shprog_changed || extra_shdata)
+       {
+               if(extra_shdata)
+                       shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
+               for(vector<const ProgramData *>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
+                       (*i)->apply();
+               changed &= ~SHADER_DATA;
        }
 
+       if(state->vertex_setup)
+               state->vertex_setup->bind();
+       else
+               VertexSetup::unbind();
+
        if(state->winding_test)
        {
                if(state->reverse_winding)
@@ -416,28 +351,6 @@ void Renderer::apply_state()
        }
        else
                WindingTest::unbind();
-
-       if(legacy_bindings)
-       {
-               if(!matrices_loaded)
-               {
-                       MatrixStack::modelview().push();
-                       MatrixStack::projection().push();
-                       matrices_loaded = true;
-               }
-
-               if(changed&LEGACY_PROJECTION)
-               {
-                       MatrixStack::projection() = state->camera->get_projection_matrix();
-                       changed &= ~LEGACY_PROJECTION;
-               }
-
-               if(changed&LEGACY_MATRIX)
-               {
-                       MatrixStack::modelview() = state->modelview_matrix;
-                       changed &= ~LEGACY_MATRIX;
-               }
-       }
 }
 
 
@@ -451,7 +364,6 @@ Renderer::State::State():
        clipping(0),
        shprog(0),
        shdata_count(0),
-       mesh(0),
        vertex_setup(0),
        winding_test(0),
        reverse_winding(false),