From 5592a115f0126ea8de1245e091338a8735b79d0e Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 4 Dec 2015 14:45:31 +0200 Subject: [PATCH] Cache renderables in SimpleScene into a vector Set iteration is rather expensive, but I want to retain the O(logn) performance of add and remove operations. --- source/simplescene.cpp | 21 +++++++++++++++++---- source/simplescene.h | 8 +++++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/source/simplescene.cpp b/source/simplescene.cpp index 73d08de2..4ccc714b 100644 --- a/source/simplescene.cpp +++ b/source/simplescene.cpp @@ -7,36 +7,49 @@ namespace GL { void SimpleScene::add(const Renderable &r) { renderables.insert(&r); + cache.clear(); } void SimpleScene::remove(const Renderable &r) { renderables.erase(&r); + cache.clear(); +} + +void SimpleScene::populate_cache() const +{ + if(cache.empty() && !renderables.empty()) + { + cache.reserve(renderables.size()); + cache.insert(cache.end(), renderables.begin(), renderables.end()); + } } void SimpleScene::setup_frame() const { - for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i) + populate_cache(); + for(RenderableArray::const_iterator i=cache.begin(); i!=cache.end(); ++i) (*i)->setup_frame(); } void SimpleScene::finish_frame() const { - for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i) + for(RenderableArray::const_iterator i=cache.begin(); i!=cache.end(); ++i) (*i)->finish_frame(); } void SimpleScene::render(Renderer &renderer, const Tag &tag) const { + populate_cache(); if(setup_frustum(renderer)) { - for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i) + for(RenderableArray::const_iterator i=cache.begin(); i!=cache.end(); ++i) if(!frustum_cull(**i)) renderer.render(**i, tag); } else { - for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i) + for(RenderableArray::const_iterator i=cache.begin(); i!=cache.end(); ++i) renderer.render(**i, tag); } } diff --git a/source/simplescene.h b/source/simplescene.h index deba4d5f..ae7d169e 100644 --- a/source/simplescene.h +++ b/source/simplescene.h @@ -8,19 +8,25 @@ namespace Msp { namespace GL { /** -The simplest possible Scene. Rendering order is unspecified. +A simple yet efficient scene. Rendering order is unspecified. */ class SimpleScene: public Scene { protected: typedef std::set RenderableSet; + typedef std::vector RenderableArray; RenderableSet renderables; + mutable RenderableArray cache; public: virtual void add(const Renderable &); virtual void remove(const Renderable &); +private: + void populate_cache() const; + +public: virtual void setup_frame() const; virtual void finish_frame() const; -- 2.45.2