From: Mikko Rasa Date: Wed, 15 Oct 2014 21:12:32 +0000 (+0300) Subject: Delay loading matrices until they are needed X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=f1034ec02beee2a497644fd91e680fd51ee1b5ac Delay loading matrices until they are needed 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. --- diff --git a/source/renderer.cpp b/source/renderer.cpp index 606dd341..0207f937 100644 --- a/source/renderer.cpp +++ b/source/renderer.cpp @@ -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; + } } } diff --git a/source/renderer.h b/source/renderer.h index 1be1a454..ccb23a22 100644 --- a/source/renderer.h +++ b/source/renderer.h @@ -91,6 +91,7 @@ private: MtxStack mtx_stack; bool mtx_changed; + bool matrices_loaded; const Camera *camera; std::vector state_stack; State *state;