@@ -41,13 +41,21 @@ class LRUCacheTestApp final : public nbl::application_templates::MonoSystemMonoL
41
41
cache.insert (11 , ' d' );
42
42
cache.insert (12 , ' e' );
43
43
cache.insert (13 , ' f' );
44
-
44
+ cache.print (m_logger);
45
+ m_logger->log (" We're Referencing `10:c`" );
46
+ char returned = *(cache.get (10 ));
47
+ cache.print (m_logger);
48
+ m_logger->log (" We're erasing `11:d`" );
49
+ cache.erase (11 );
50
+ assert (cache.get (11 ) == nullptr );
51
+ cache.print (m_logger);
52
+ m_logger->log (" We're adding `11:d` again" );
53
+ cache.insert (11 , ' d' );
45
54
cache.print (m_logger);
46
55
47
- char returned = *(cache.get (11 ));
48
- assert (returned == ' d' );
49
- returned = *(cache.get (10 ));
50
56
assert (returned == ' c' );
57
+ returned = *(cache.get (11 ));
58
+ assert (returned == ' d' );
51
59
returned = *(cache.get (13 ));
52
60
assert (returned == ' f' );
53
61
@@ -105,6 +113,44 @@ class LRUCacheTestApp final : public nbl::application_templates::MonoSystemMonoL
105
113
#endif
106
114
m_logger->log (" all good" );
107
115
116
+ constexpr uint32_t InvalidIdx = ~0u ;
117
+ struct TextureReference
118
+ {
119
+ uint32_t alloc_idx;
120
+ uint64_t lastUsedSemaphoreValue;
121
+
122
+ // copy ctor
123
+ TextureReference (const TextureReference& tref)
124
+ {
125
+ assert (false ); // based on the code in this test, copy constuctor shouldn't be called
126
+ }
127
+ TextureReference (TextureReference&& tref) = default ;
128
+ inline TextureReference& operator =(TextureReference&& tref) = default ;
129
+
130
+ TextureReference (uint32_t alloc_idx, uint64_t semaphoreVal) : alloc_idx(alloc_idx), lastUsedSemaphoreValue(semaphoreVal) {}
131
+ TextureReference (uint64_t semaphoreVal) : TextureReference(InvalidIdx, semaphoreVal) {}
132
+ TextureReference () : TextureReference(InvalidIdx, ~0ull ) {}
133
+
134
+ // In LRU Cache `insert` function, in case of cache hit, we need to assign semaphore value to TextureReference without changing `alloc_idx`
135
+ inline TextureReference& operator =(uint64_t semamphoreVal) { lastUsedSemaphoreValue = semamphoreVal; return *this ; }
136
+ };
137
+ using TextureLRUCache = LRUCache<uint32_t , TextureReference>;
138
+
139
+ TextureLRUCache textureCache = TextureLRUCache (3u );
140
+
141
+ static_assert (std::is_assignable_v<TextureReference, uint64_t >);
142
+ static_assert (std::is_constructible_v<TextureReference, uint64_t >);
143
+
144
+ textureCache.insert (91u , TextureReference{ ~0u , 69u });
145
+ textureCache.insert (92u , TextureReference{ 20u , 70u });
146
+ textureCache.insert (93u , TextureReference{ 10u , 71u });
147
+ auto t = textureCache.get (91u );
148
+ assert (t->lastUsedSemaphoreValue == 69u ); // make 91 jump to front, now 92 is the LRU
149
+ // next insertion will evict because capacity is 3
150
+ auto insertion = textureCache.insert (99u , 6999ull , [](const TextureReference& evictedTextureRef) -> void { assert (evictedTextureRef.alloc_idx == 20u ); });
151
+ assert (insertion->alloc_idx == InvalidIdx);
152
+ assert (insertion->lastUsedSemaphoreValue == 6999ull );
153
+
108
154
return true ;
109
155
}
110
156
0 commit comments