]> git.tdb.fi Git - libs/gl.git/commitdiff
Delay loading matrices until they are needed
authorMikko Rasa <tdb@tdb.fi>
Wed, 15 Oct 2014 21:12:32 +0000 (00:12 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 15 Oct 2014 21:25:38 +0000 (00:25 +0300)
If all renderables use modern shaders, it's not necessary to touch the
system matrix stacks at all.  OpenGL ES 2.0 and OpenGL 3.0 core do not
even have system matrix stacks.

source/renderer.cpp
source/renderer.h

index 606dd341336fcbc8668bc8724e38898f9fe084bb..0207f9378324aaed15a22725fbaa5897bf666bff 100644 (file)
@@ -22,7 +22,8 @@ namespace GL {
 
 Renderer::Renderer(const Camera *c):
        mtx_stack(*this),
-       mtx_changed(false),
+       mtx_changed(true),
+       matrices_loaded(false),
        camera(c),
        state_stack(1),
        lighting_changed(false),
@@ -32,11 +33,8 @@ Renderer::Renderer(const Camera *c):
        shdata_stack.reserve(32);
        state = &state_stack.back();
 
-       MatrixStack::modelview().push();
        if(camera)
        {
-               MatrixStack::projection().push();
-               camera->apply();
                mtx_stack.load(camera->get_matrix());
                standard_shdata.uniform("projection_matrix", camera->get_projection_matrix());
        }
@@ -49,9 +47,12 @@ Renderer::Renderer(const Camera *c):
 
 Renderer::~Renderer()
 {
-       if(camera)
-               MatrixStack::projection().pop();
-       MatrixStack::modelview().pop();
+       if(matrices_loaded)
+       {
+               if(camera)
+                       MatrixStack::projection().pop();
+               MatrixStack::modelview().pop();
+       }
 
        Mesh::unbind();
        Texturing::unbind();
@@ -157,6 +158,7 @@ void Renderer::escape()
 {
        apply_state();
        Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
+       matrices_loaded = false;
 }
 
 void Renderer::exclude(const Renderable &renderable)
@@ -282,10 +284,24 @@ void Renderer::apply_state()
        else
                WindingTest::unbind();
 
-       if(legacy_bindings && mtx_changed)
+       if(legacy_bindings)
        {
-               MatrixStack::modelview() = mtx_stack.top();
-               mtx_changed = false;
+               if(!matrices_loaded)
+               {
+                       MatrixStack::modelview().push();
+                       if(camera)
+                       {
+                               MatrixStack::projection().push();
+                               camera->apply();
+                       }
+                       matrices_loaded = true;
+               }
+
+               if(mtx_changed)
+               {
+                       MatrixStack::modelview() = mtx_stack.top();
+                       mtx_changed = false;
+               }
        }
 }
 
index 1be1a4549211828d8b3dedfeea7898a4c0a526b3..ccb23a22a709ff1f864e6179435c9ead36bb5db8 100644 (file)
@@ -91,6 +91,7 @@ private:
 
        MtxStack mtx_stack;
        bool mtx_changed;
+       bool matrices_loaded;
        const Camera *camera;
        std::vector<State> state_stack;
        State *state;