]> git.tdb.fi Git - libs/gl.git/commitdiff
Use a per-camera cache in ZSortedScene
authorMikko Rasa <tdb@tdb.fi>
Mon, 14 Mar 2022 07:10:12 +0000 (09:10 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 14 Mar 2022 07:10:12 +0000 (09:10 +0200)
It's very common to have multiple cameras in a scene due to effects like
shadow and environment maps.  Using a single cache for all of them is
detrimental since the order will be different for each.

source/render/zsortedscene.cpp
source/render/zsortedscene.h

index 9c56f26e8ca39a3f583f836fd0ede67fb5922b33..bdce8aa9cd4f9cab7126d2dd0d7630f99219ebc2 100644 (file)
@@ -14,8 +14,8 @@ void ZSortedScene::add(Renderable &r)
        if(i==content.end() || *i!=&r)
        {
                content.insert(i, &r);
-               if(!sorted_cache.empty())
-                       sorted_cache.push_back(&r);
+               for(auto &kvp: sorted_cache)
+                       kvp.second.push_back(&r);
        }
 }
 
@@ -39,26 +39,16 @@ void ZSortedScene::set_reference(DepthReference r)
        reference = r;
 }
 
-void ZSortedScene::populate_cache() const
-{
-       if(sorted_cache.empty() && !content.empty())
-       {
-               sorted_cache.reserve(content.size());
-               sorted_cache.insert(sorted_cache.end(), content.begin(), content.end());
-       }
-}
-
 void ZSortedScene::setup_frame(Renderer &renderer)
 {
-       populate_cache();
-       for(const SortedRenderable &r: sorted_cache)
-               r.renderable->setup_frame(renderer);
+       for(Renderable *r: content)
+               r->setup_frame(renderer);
 }
 
 void ZSortedScene::finish_frame()
 {
-       for(const SortedRenderable &r: sorted_cache)
-               r.renderable->finish_frame();
+       for(Renderable *r: content)
+               r->finish_frame();
 }
 
 void ZSortedScene::render(Renderer &renderer, Tag tag) const
@@ -66,22 +56,27 @@ void ZSortedScene::render(Renderer &renderer, Tag tag) const
        if(content.empty())
                return;
 
-       populate_cache();
-
        const Camera *camera = renderer.get_camera();
        if(!camera)
        {
-               for(const SortedRenderable &r: sorted_cache)
-                       r.renderable->render(renderer, tag);
+               for(Renderable *r: content)
+                       r->render(renderer, tag);
                return;
        }
 
+       vector<SortedRenderable> &cache = sorted_cache[camera];
+       if(cache.empty() && !content.empty())
+       {
+               cache.reserve(content.size());
+               cache.insert(cache.end(), content.begin(), content.end());
+       }
+
        const Vector3 &camera_pos = camera->get_position();
        const Vector3 &look_dir = camera->get_look_direction();
        float radius_factor = reference-1.0f;
        float sign = 1.0f-order*2.0f;
 
-       for(SortedRenderable &r: sorted_cache)
+       for(SortedRenderable &r: cache)
        {
                r.in_frustum = camera->is_in_frustum(*r.renderable);
                if(!r.in_frustum)
@@ -99,18 +94,18 @@ void ZSortedScene::render(Renderer &renderer, Tag tag) const
                        r.depth = 0;
        }
 
-       for(auto i=sorted_cache.begin(), j=i; i!=sorted_cache.end(); ++i)
+       for(auto i=cache.begin(), j=i; i!=cache.end(); ++i)
                if(i->in_frustum)
                {
                        if(i!=j)
                                swap(*i, *j);
 
-                       if(j!=sorted_cache.begin() && *j<*(j-1))
+                       if(j!=cache.begin() && *j<*(j-1))
                        {
                                SortedRenderable sr = *j;
                                auto k = j-1;
                                *j = *k;
-                               while(k!=sorted_cache.begin() && sr<*(k-1))
+                               while(k!=cache.begin() && sr<*(k-1))
                                {
                                        *k = *(k-1);
                                        --k;
@@ -121,7 +116,7 @@ void ZSortedScene::render(Renderer &renderer, Tag tag) const
                        ++j;
                }
 
-       for(auto i=sorted_cache.begin(); (i!=sorted_cache.end() && i->in_frustum); ++i)
+       for(auto i=cache.begin(); (i!=cache.end() && i->in_frustum); ++i)
                i->renderable->render(renderer, tag);
 }
 
index 778b097e801fdf913c418b85e403ff3030237791..be23ac271979be39fda50a9a248847ff195b2bfb 100644 (file)
@@ -48,7 +48,7 @@ private:
        std::vector<Renderable *> content;
        SortOrder order = BACK_TO_FRONT;
        DepthReference reference = FURTHEST;
-       mutable std::vector<SortedRenderable> sorted_cache;
+       mutable std::map<const Camera *, std::vector<SortedRenderable> > sorted_cache;
 
 public:
        virtual void add(Renderable &);
@@ -60,10 +60,6 @@ public:
        /// Sets the reference point for sorting.  Default is furthest from camera.
        void set_reference(DepthReference);
 
-private:
-       void populate_cache() const;
-
-public:
        virtual void setup_frame(Renderer &);
        virtual void finish_frame();