From: Mikko Rasa Date: Mon, 14 Mar 2022 07:10:12 +0000 (+0200) Subject: Use a per-camera cache in ZSortedScene X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=23e368883b01ebb08ba2df56db91f19f26cf990d;p=libs%2Fgl.git Use a per-camera cache in ZSortedScene 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. --- diff --git a/source/render/zsortedscene.cpp b/source/render/zsortedscene.cpp index 9c56f26e..bdce8aa9 100644 --- a/source/render/zsortedscene.cpp +++ b/source/render/zsortedscene.cpp @@ -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 &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); } diff --git a/source/render/zsortedscene.h b/source/render/zsortedscene.h index 778b097e..be23ac27 100644 --- a/source/render/zsortedscene.h +++ b/source/render/zsortedscene.h @@ -48,7 +48,7 @@ private: std::vector content; SortOrder order = BACK_TO_FRONT; DepthReference reference = FURTHEST; - mutable std::vector sorted_cache; + mutable std::map > 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();