1#include "Foundation/Logging/Logger.h"
4#include "Resources/TextureManager.h"
6#include "Image360System.h"
10 using namespace Cogs::Core::Image360;
14 size_t cacheMapOffset(
size_t level)
25 if (level == 0)
return 0;
28 size_t t = (size_t(1) << (2 * n + 2)) - 1;
34 SlotIx tryEvictACacheSlot(
Cache& cache,
const uint32_t currentFrame)
45 if (1 < currentFrame - cacheItem.lastTouched) {
46 LOG_TRACE(logger,
"Map=%zu slot=%d rev=%u evicting cache slot, age=%u", cacheItem.mapIx, slotIx, cache.items[slotIx].revision, currentFrame - cacheItem.lastTouched);
47 assert(cache.map[cacheItem.mapIx] == slotIx);
49 cache.map[cacheItem.mapIx] = -1;
50 cacheItem.value.state = Cache::Item::State::None;
51 cacheItem.depth.state = Cache::Item::State::None;
52 cacheItem.mapIx = ~0u;
59 SlotIx tryAllocNewCacheSlot(
Cache& cache)
61 size_t o = cache.items.size();
64 SlotIx slotIx = SlotIx(o);
66 cache.
lru.emplace_back(slotIx);
68 Cache::Item& cacheItem = cache.items.emplace_back();
69 cacheItem.value.state = Cache::Item::State::None;
70 cacheItem.depth.state = Cache::Item::State::None;
71 cacheItem.mapIx = ~0u;
72 cacheItem.lastTouched = 3;
80void Cogs::Core::Image360::Cache::init(
const Config& config)
83 map.resize(cacheMapOffset(config.
treeDepth), -1);
86SlotIx Cogs::Core::Image360::Cache::isQuadInCache(
Context* context,
90 const uint32_t currentFrame,
91 const size_t cacheLevel,
92 const size_t cacheLevelIndex)
94 MapIx mapIx = cacheMapOffset(cacheLevel) + cacheLevelIndex;
96 SlotIx slotIx = map[mapIx];
98 Item& cacheItem = items[slotIx];
99 cacheItem.lastTouched = currentFrame;
100 if (cacheItem.value.state == Item::State::Resident && cacheItem.depth.state == Item::State::Resident) {
105 if (cacheItem.value.state == Item::State::None) {
106 cacheItem.value.state = Item::State::Loading;
107 fetcher.issueChannelFetch(context,
117 cacheItem.depth.state = Item::State::Loading;
120 if (config.
hasDepth && cacheItem.depth.state == Item::State::None) {
121 cacheItem.depth.state = Item::State::Loading;
122 fetcher.issueChannelFetch(context,
136 if (!fetcher.canLoadAnyItems(config)) {
141 slotIx = tryEvictACacheSlot(*
this, currentFrame);
143 slotIx = tryAllocNewCacheSlot(*
this);
147 Item& cacheItem = items[slotIx];
148 cacheItem.lastTouched = currentFrame;
149 cacheItem.revision++;
150 cacheItem.mapIx = mapIx;
153 cacheItem.value.state = Item::State::Loading;
154 fetcher.issueChannelFetch(context,
164 cacheItem.depth.state = Item::State::Loading;
166 fetcher.issueChannelFetch(context,
178 cacheItem.depth.state = Item::State::Resident;
186void Cogs::Core::Image360::Cache::updateLeastRecentlyUsedList(
const uint32_t currentFrame)
188 assert(items.size() == lru.size());
190 std::sort(lru.begin(),
192 [currentFrame, cache=
this](
const SlotIx a,
const SlotIx b) ->
bool
194 return (currentFrame - cache->items[a].lastTouched) < (currentFrame - cache->items[b].lastTouched);
196 lruPointer = lru.size();
198 for (
size_t i = 0; i + 1 < lruPointer; i++) {
199 assert(lru[i] != lru[i + 1]);
203 size_t evictable = 0;
205 SlotIx slotIx = lru[--lruPointer];
206 if (0 < currentFrame - items[slotIx].lastTouched) {
211 LOG_DEBUG(logger,
"Evictable %zu", evictable);
213 lruPointer = lru.size();
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Log implementation class.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
constexpr Log getLogger(const char(&name)[LEN]) noexcept
size_t maxItemCount
Number of tiles in cache.
size_t lruPointer
Points to one past the least recently and recycable item, decrements and zero means nothing to recycl...
std::vector< SlotIx > lru
Slots sorted s.t. the least recently used items is at end.
uint8_t valueChannel
Data channel to be used as value data. From component.
uint32_t treeDepth
Depth of tile hierarchy. From json.
uint8_t depthChannel
Data channel that contains depth data. From json.
bool hasDepth
If data has depth and component wants depth.