Nebula
Loading...
Searching...
No Matches
texturetilecache.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
10//------------------------------------------------------------------------------
11#include "core/types.h"
12#include "math/scalar.h"
13namespace Terrain
14{
15
16
23static_assert(alignof(IndirectionEntry) == 4);
24static_assert(sizeof(IndirectionEntry) == 4);
25
27{
28 struct Entry
29 {
30 uint64 tiles : 10; // must hold at least SubTextureMaxTiles from TerrainContext.h
31 uint64 tileX : 11; // must hold at least SubTextureMaxTiles
32 uint64 tileY : 11; // same as above
33 uint64 subTextureIndex : 32; // the index of the subtexture
34 };
35 union
36 {
39 };
40
41 bool operator>(const TileCacheEntry& rhs) const
42 {
43 return this->hash > rhs.hash;
44 }
45 bool operator<(const TileCacheEntry& rhs) const
46 {
47 return this->hash < rhs.hash;
48 }
49 bool operator==(const TileCacheEntry& rhs) const
50 {
51 return this->hash == rhs.hash;
52 }
53 bool operator!=(const TileCacheEntry& rhs) const
54 {
55 return this->hash != rhs.hash;
56 }
57};
58
59static const TileCacheEntry InvalidTileCacheEntry = TileCacheEntry{ 0x3FF, 0x7FF, 0x7FF, 0xFFFFFFFF };
60
107
108//------------------------------------------------------------------------------
111inline
113 : head(nullptr)
114 , tail(nullptr)
115 , tiles(0)
116 , tileSize(0)
117{
118}
119
120//------------------------------------------------------------------------------
123inline
127
128//------------------------------------------------------------------------------
131inline void
132TextureTileCache::Setup(uint tileSize, uint textureSize)
133{
134 this->head = this->tail = nullptr;
135 this->tiles = textureSize / tileSize;
136 this->tileSize = tileSize;
137
138 // keep all nodes linearly in memory!
139 this->nodes.Resize(this->tiles * this->tiles);
140
141 // setup storage
142 for (uint x = 0; x < this->tiles; x++)
143 {
144 for (uint y = 0; y < this->tiles; y++)
145 {
146 // calculate node index 2D->1D
147 uint index = x + y * this->tiles;
148
149 // set the data, this will be static
150 Node* node = &this->nodes[index];
151 node->offset = Math::uint2{ x * tileSize, y * tileSize };
153 node->prev = nullptr;
154 node->next = nullptr;
155
156 // Add to linked list
157 this->InsertBeginning(node);
158 }
159 }
160}
161
162//------------------------------------------------------------------------------
167{
169
170 CacheResult result;
171 result.didCache = false;
173
174 IndexT index = this->lookup.FindIndex(entry);
175 if (index == InvalidIndex)
176 {
177 // Grab the tail
178 Node* node = this->tail;
179 this->Remove(node);
180 if (node->entry != InvalidTileCacheEntry)
181 {
182 this->lookup.Erase(node->entry);
183 result.evicted = node->entry;
184 }
185
186 // make most recent
187 this->InsertBeginning(node);
188 node->entry = entry;
189 this->lookup.Add(entry, node);
190
191 // update entry
192 result.cached = node->offset;
193 result.didCache = true;
194 }
195 else
196 {
197 Node* node = this->lookup.ValueAtIndex(index);
198 result.cached = node->offset;
199 this->Remove(node);
200 this->InsertBeginning(node);
201 }
202 return result;
203}
204
205//------------------------------------------------------------------------------
208inline void
210{
211 Node* node = this->head->next;
212 while (node != nullptr)
213 {
214 Node* next = node->next;
215 this->Remove(node);
216 node = next;
217 }
218 this->head = nullptr;
219 this->tail = nullptr;
220 this->nodes.Clear();
221 this->lookup.Clear();
222}
223
224//------------------------------------------------------------------------------
227inline void
229{
230 if (this->head == nullptr)
231 {
232 this->head = node;
233 this->tail = node;
234 node->prev = nullptr;
235 node->next = nullptr;
236 }
237 else
238 this->InsertBefore(this->head, node);
239}
240
241//------------------------------------------------------------------------------
244inline void
246{
247 newNode->next = node;
248 if (node->prev == nullptr)
249 {
250 newNode->prev = nullptr;
251 this->head = newNode;
252 }
253 else
254 {
255 newNode->prev = node->prev;
256 node->prev->next = newNode;
257 }
258 node->prev = newNode;
259}
260
261//------------------------------------------------------------------------------
264inline void
266{
267 // reassign neighbors
268 if (node->prev == nullptr)
269 this->head = node->next;
270 else
271 node->prev->next = node->next;
272
273 if (node->next == nullptr)
274 this->tail = node->prev;
275 else
276 node->next->prev = node->prev;
277
278 node->next = nullptr;
279 node->prev = nullptr;
280}
281
282} // namespace Terrain
Definition texturetilecache.h:62
uint tileSize
Definition texturetilecache.h:105
~TextureTileCache()
destructor
Definition texturetilecache.h:124
TextureTileCache()
constructor
Definition texturetilecache.h:112
void InsertBeginning(Node *node)
insert at beginning of list
Definition texturetilecache.h:228
Node * head
Definition texturetilecache.h:100
Util::FixedArray< Node > nodes
Definition texturetilecache.h:98
uint tiles
Definition texturetilecache.h:104
Node * tail
Definition texturetilecache.h:101
void Clear()
clear cache
Definition texturetilecache.h:209
CacheResult Cache(TileCacheEntry entry)
get tile, if invalid coord, gets the last used, otherwise, bumps the use of it
Definition texturetilecache.h:166
void Remove(Node *node)
detach node and fix tail & head
Definition texturetilecache.h:265
void Setup(uint tileSize, uint textureSize)
setup cache
Definition texturetilecache.h:132
void InsertBefore(Node *node, Node *newNode)
insert before another node
Definition texturetilecache.h:245
Util::Dictionary< TileCacheEntry, Node * > lookup
Definition texturetilecache.h:102
A collection of key/value pairs with quick value retrieval by key at roughly O(log n).
Definition dictionary.h:34
Implements a fixed size one-dimensional array.
Definition fixedarray.h:20
#define n_assert(exp)
Definition debug.h:50
The occupancy quad tree implements a tree which allows for a quick search.
Definition occupancyquadtree.h:15
static const TileCacheEntry InvalidTileCacheEntry
Definition texturetilecache.h:59
Nebula's scalar datatype.
Definition scalar.h:112
Definition texturetilecache.h:18
uint physicalOffsetY
Definition texturetilecache.h:21
uint physicalOffsetX
Definition texturetilecache.h:20
uint mip
Definition texturetilecache.h:19
Definition texturetilecache.h:66
Math::uint2 cached
Definition texturetilecache.h:67
TileCacheEntry evicted
Definition texturetilecache.h:69
bool didCache
Definition texturetilecache.h:68
Definition texturetilecache.h:84
Node * next
Definition texturetilecache.h:88
Math::uint2 offset
Definition texturetilecache.h:86
Node * prev
Definition texturetilecache.h:87
TileCacheEntry entry
Definition texturetilecache.h:85
Definition texturetilecache.h:29
uint64 tiles
Definition texturetilecache.h:30
uint64 tileX
Definition texturetilecache.h:31
uint64 subTextureIndex
Definition texturetilecache.h:33
uint64 tileY
Definition texturetilecache.h:32
Definition texturetilecache.h:27
bool operator>(const TileCacheEntry &rhs) const
Definition texturetilecache.h:41
bool operator!=(const TileCacheEntry &rhs) const
Definition texturetilecache.h:53
Entry entry
Definition texturetilecache.h:37
uint64 hash
Definition texturetilecache.h:38
bool operator<(const TileCacheEntry &rhs) const
Definition texturetilecache.h:45
bool operator==(const TileCacheEntry &rhs) const
Definition texturetilecache.h:49
static const int InvalidIndex
Definition types.h:54
unsigned int uint
Definition types.h:31
uint64_t uint64
Definition types.h:36
int IndexT
Definition types.h:48