From: Mikko Rasa Date: Tue, 28 Aug 2012 09:10:38 +0000 (+0300) Subject: More efficient stack management X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=1b7b75e3aaec35433cce81fc58e58e50e5a3f36e More efficient stack management Vectors are better than lists, since they don't need to allocate new memory for every element. A suitably sized reserve() does away with any initial reallocations in most cases. ProgramData in Renderer can be stored in a semi-independent stack as well, since it can only be removed with a pop. State is now devoid of dynamically allocated memory, making the stack operations even faster. --- diff --git a/source/matrix.cpp b/source/matrix.cpp index 549fe8b6..b7f76bc7 100644 --- a/source/matrix.cpp +++ b/source/matrix.cpp @@ -278,12 +278,14 @@ GLenum MatrixStack::current_mode = GL_MODELVIEW; MatrixStack::MatrixStack(GLenum m): mode(m) { + matrices.reserve(mode==GL_MODELVIEW ? 32 : 4); matrices.push_back(Matrix()); } MatrixStack::MatrixStack(): mode(0) { + matrices.reserve(32); matrices.push_back(Matrix()); } diff --git a/source/matrix.h b/source/matrix.h index e32df7e3..5444b8b4 100644 --- a/source/matrix.h +++ b/source/matrix.h @@ -1,7 +1,7 @@ #ifndef MSP_GL_MATRIX_H_ #define MSP_GL_MATRIX_H_ -#include +#include #include "gl.h" #include "vector.h" @@ -81,7 +81,7 @@ public: private: GLenum mode; - std::list matrices; + std::vector matrices; static GLenum current_mode; diff --git a/source/renderer.cpp b/source/renderer.cpp index c751b0e3..94875084 100644 --- a/source/renderer.cpp +++ b/source/renderer.cpp @@ -21,11 +21,14 @@ Renderer::Renderer(const Camera *c): mtx_changed(false), camera(c), state_stack(1), - state(&state_stack.back()), vertex_array(0), vertex_array_changed(false), element_buffer(0) { + state_stack.reserve(16); + shdata_stack.reserve(32); + state = &state_stack.back(); + MatrixStack::modelview().push(); if(camera) { @@ -73,12 +76,16 @@ void Renderer::set_shader_program(const Program *p, const ProgramData *d) state->shprog = p; if(p && d) add_shader_data(*d); + + /* Even if we have no new shdata, the existing ones need to be re-applied + to the new program */ shdata_changed = true; } void Renderer::add_shader_data(const ProgramData &d) { - state->shdata.push_back(&d); + shdata_stack.push_back(&d); + state->shdata_count = shdata_stack.size(); shdata_changed = true; } @@ -112,6 +119,8 @@ void Renderer::pop_state() state_stack.pop_back(); state = &state_stack.back(); + if(shdata_stack.size()>state->shdata_count) + shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end()); mtx_stack.pop(); mtx_changed = true; shdata_changed = true; @@ -170,7 +179,7 @@ void Renderer::apply_state() state->shprog->bind(); if(shdata_changed) { - for(vector::const_iterator i=state->shdata.begin(); i!=state->shdata.end(); ++i) + for(vector::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i) (*i)->apply(); shdata_changed = false; } diff --git a/source/renderer.h b/source/renderer.h index 5536702b..6115a7fe 100644 --- a/source/renderer.h +++ b/source/renderer.h @@ -26,6 +26,11 @@ will often be more efficient. This is especially true for ObjectInstances. The Renderer works by deferring GL state changes until something is actually being drawn. This avoids many unnecessary GL calls if consecutive renderables use the same resources. + +A state stack is provided to help with state scoping. Typically a Renderable +will push the current state on entry, set whatever state it requires, render +itself, and pop the state when it's done. An RAII helper class is provided for +the push/pop operation. */ class Renderer { @@ -47,7 +52,7 @@ private: const Texturing *texturing; const Material *material; const Program *shprog; - std::vector shdata; + unsigned shdata_count; const WindingTest *winding_test; State(); @@ -67,12 +72,13 @@ private: MtxStack mtx_stack; bool mtx_changed; const Camera *camera; - std::list state_stack; + std::vector state_stack; State *state; + std::vector shdata_stack; + bool shdata_changed; const VertexArray *vertex_array; bool vertex_array_changed; const Buffer *element_buffer; - bool shdata_changed; public: Renderer(const Camera *); @@ -99,7 +105,11 @@ public: void set_element_buffer(const Buffer *); void set_winding_test(const WindingTest *); + /** Saves the current state so it can be restored later. */ void push_state(); + + /** Restores a previously saved state. Must be matched with an earlier + push_state call. */ void pop_state(); /** Prepares for temporarily bypassing the Renderer by synchronizing the