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_t tiles : 10; // must hold at least SubTextureMaxTiles from TerrainContext.h
31 uint64_t tileX : 11; // must hold at least SubTextureMaxTiles
32 uint64_t tileY : 11; // same as above
33 uint64_t subTextureIndex : 32; // the index of the subtexture
34 };
35 union
36 {
38 uint64_t hash;
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
62{
63public:
64
71
75
77 void Setup(uint tileSize, uint textureSize);
81 void Clear();
83 void Reset();
84private:
92
94 void InsertBeginning(Node* node);
96 void InsertBefore(Node* node, Node* newNode);
98 void Remove(Node* node);
99
101
105
108};
109
110//------------------------------------------------------------------------------
113inline
115 : head(nullptr)
116 , tail(nullptr)
117 , tiles(0)
118 , tileSize(0)
119{
120}
121
122//------------------------------------------------------------------------------
125inline
129
130//------------------------------------------------------------------------------
133inline void
135{
136 this->head = this->tail = nullptr;
137 this->tiles = textureSize / tileSize;
138 this->tileSize = tileSize;
139
140 // keep all nodes linearly in memory!
141 this->nodes.Resize(this->tiles * this->tiles);
142
143 // setup storage
144 for (uint x = 0; x < this->tiles; x++)
145 {
146 for (uint y = 0; y < this->tiles; y++)
147 {
148 // calculate node index 2D->1D
149 uint index = x + y * this->tiles;
150
151 // set the data, this will be static
152 Node* node = &this->nodes[index];
153 node->offset = Math::uint2{ x * tileSize, y * tileSize };
155 node->prev = nullptr;
156 node->next = nullptr;
157
158 // Add to linked list
159 this->InsertBeginning(node);
160 }
161 }
162}
163
164//------------------------------------------------------------------------------
169{
171
172 CacheResult result;
173 result.didCache = false;
175
176 IndexT index = this->lookup.FindIndex(entry);
177 if (index == InvalidIndex)
178 {
179 // Grab the tail
180 Node* node = this->tail;
181 this->Remove(node);
182 if (node->entry != InvalidTileCacheEntry)
183 {
184 this->lookup.Erase(node->entry);
185 result.evicted = node->entry;
186 }
187
188 // make most recent
189 this->InsertBeginning(node);
190 node->entry = entry;
191 this->lookup.Add(entry, node);
192
193 // update entry
194 result.cached = node->offset;
195 result.didCache = true;
196 }
197 else
198 {
199 Node* node = this->lookup.ValueAtIndex(index);
200 result.cached = node->offset;
201 this->Remove(node);
202 this->InsertBeginning(node);
203 }
204 return result;
205}
206
207//------------------------------------------------------------------------------
210inline void
212{
213 Node* node = this->head->next;
214 while (node != nullptr)
215 {
216 Node* next = node->next;
217 this->Remove(node);
218 node = next;
219 }
220 this->head = nullptr;
221 this->tail = nullptr;
222
223 // setup storage
224 for (uint x = 0; x < this->tiles; x++)
225 {
226 for (uint y = 0; y < this->tiles; y++)
227 {
228 // calculate node index 2D->1D
229 uint index = x + y * this->tiles;
230
231 // set the data, this will be static
232 Node* node = &this->nodes[index];
233 node->offset = Math::uint2{ x * this->tileSize, y * this->tileSize };
235 node->prev = nullptr;
236 node->next = nullptr;
237
238 // Add to linked list
239 this->InsertBeginning(node);
240 }
241 }
242
243 this->nodes.Clear();
244 this->lookup.Clear();
245}
246
247//------------------------------------------------------------------------------
250inline void
252{
253 // setup storage
254 this->head = nullptr;
255 this->tail = nullptr;
256 this->lookup.Clear();
257 for (uint x = 0; x < this->tiles; x++)
258 {
259 for (uint y = 0; y < this->tiles; y++)
260 {
261 // calculate node index 2D->1D
262 uint index = x + y * this->tiles;
263
264 // set the data, this will be static
265 Node* node = &this->nodes[index];
266 node->offset = Math::uint2{ x * tileSize, y * tileSize };
268 node->prev = nullptr;
269 node->next = nullptr;
270
271 // Add to linked list
272 this->InsertBeginning(node);
273 }
274 }
275}
276
277//------------------------------------------------------------------------------
280inline void
282{
283 if (this->head == nullptr)
284 {
285 this->head = node;
286 this->tail = node;
287 node->prev = nullptr;
288 node->next = nullptr;
289 }
290 else
291 this->InsertBefore(this->head, node);
292}
293
294//------------------------------------------------------------------------------
297inline void
299{
300 newNode->next = node;
301 if (node->prev == nullptr)
302 {
303 newNode->prev = nullptr;
304 this->head = newNode;
305 }
306 else
307 {
308 newNode->prev = node->prev;
309 node->prev->next = newNode;
310 }
311 node->prev = newNode;
312}
313
314//------------------------------------------------------------------------------
317inline void
319{
320 // reassign neighbors
321 if (node->prev == nullptr)
322 this->head = node->next;
323 else
324 node->prev->next = node->next;
325
326 if (node->next == nullptr)
327 this->tail = node->prev;
328 else
329 node->next->prev = node->prev;
330
331 node->next = nullptr;
332 node->prev = nullptr;
333}
334
335} // namespace Terrain
uint tileSize
Definition texturetilecache.h:107
~TextureTileCache()
destructor
Definition texturetilecache.h:126
TextureTileCache()
constructor
Definition texturetilecache.h:114
void InsertBeginning(Node *node)
insert at beginning of list
Definition texturetilecache.h:281
Node * head
Definition texturetilecache.h:102
void Reset()
Reset the cache without freeing memory.
Definition texturetilecache.h:251
Util::FixedArray< Node > nodes
Definition texturetilecache.h:100
uint tiles
Definition texturetilecache.h:106
Node * tail
Definition texturetilecache.h:103
void Clear()
clear cache
Definition texturetilecache.h:211
CacheResult Cache(TileCacheEntry entry)
get tile, if invalid coord, gets the last used, otherwise, bumps the use of it
Definition texturetilecache.h:168
void Remove(Node *node)
detach node and fix tail & head
Definition texturetilecache.h:318
void Setup(uint tileSize, uint textureSize)
setup cache
Definition texturetilecache.h:134
void InsertBefore(Node *node, Node *newNode)
insert before another node
Definition texturetilecache.h:298
Util::Dictionary< TileCacheEntry, Node * > lookup
Definition texturetilecache.h:104
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:17
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:86
Node * next
Definition texturetilecache.h:90
Math::uint2 offset
Definition texturetilecache.h:88
Node * prev
Definition texturetilecache.h:89
TileCacheEntry entry
Definition texturetilecache.h:87
Definition texturetilecache.h:29
uint64_t tileX
Definition texturetilecache.h:31
uint64_t tileY
Definition texturetilecache.h:32
uint64_t tiles
Definition texturetilecache.h:30
uint64_t subTextureIndex
Definition texturetilecache.h:33
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_t 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:47
unsigned int uint
Definition types.h:33
int IndexT
Definition types.h:41