-
Notifications
You must be signed in to change notification settings - Fork 64
/
refterm_glyph_cache.c
379 lines (302 loc) · 9.66 KB
/
refterm_glyph_cache.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#ifndef Assert
#define Assert(...)
#define GLYPH_TABLE_UNDEF_ASSERT
#endif
#define DEBUG_VALIDATE_LRU 0
struct glyph_entry
{
glyph_hash HashValue;
uint32_t NextWithSameHash;
uint32_t NextLRU;
uint32_t PrevLRU;
gpu_glyph_index GPUIndex;
// NOTE(casey): For user use:
uint32_t FilledState;
uint16_t DimX;
uint16_t DimY;
#if DEBUG_VALIDATE_LRU
size_t Ordering;
#endif
};
struct glyph_table
{
glyph_table_stats Stats;
uint32_t HashMask;
uint32_t HashCount;
uint32_t EntryCount;
uint32_t *HashTable;
glyph_entry *Entries;
#if DEBUG_VALIDATE_LRU
uint32_t LastLRUCount;
#endif
};
static gpu_glyph_index PackGlyphCachePoint(uint32_t X, uint32_t Y)
{
gpu_glyph_index Result = {(Y << 16) | X};
return Result;
}
static glyph_cache_point UnpackGlyphCachePoint(gpu_glyph_index P)
{
glyph_cache_point Result;
Result.X = (P.Value & 0xffff);
Result.Y = (P.Value >> 16);
return Result;
}
static int GlyphHashesAreEqual(glyph_hash A, glyph_hash B)
{
__m128i Compare = _mm_cmpeq_epi32(A.Value, B.Value);
int Result = (_mm_movemask_epi8(Compare) == 0xffff);
return Result;
}
static uint32_t *GetSlotPointer(glyph_table *Table, glyph_hash RunHash)
{
uint32_t HashIndex = _mm_cvtsi128_si32(RunHash.Value);
uint32_t HashSlot = (HashIndex & Table->HashMask);
Assert(HashSlot < Table->HashCount);
uint32_t *Result = &Table->HashTable[HashSlot];
return Result;
}
static glyph_entry *GetEntry(glyph_table *Table, uint32_t Index)
{
Assert(Index < Table->EntryCount);
glyph_entry *Result = Table->Entries + Index;
return Result;
}
static glyph_entry *GetSentinel(glyph_table *Table)
{
glyph_entry *Result = Table->Entries;
return Result;
}
static glyph_table_stats GetAndClearStats(glyph_table *Table)
{
glyph_table_stats Result = Table->Stats;
glyph_table_stats ZeroStats = {0};
Table->Stats = ZeroStats;
return Result;
}
static void UpdateGlyphCacheEntry(glyph_table *Table, uint32_t ID, uint32_t NewState, uint16_t NewDimX, uint16_t NewDimY)
{
glyph_entry *Entry = GetEntry(Table, ID);
Entry->FilledState = NewState;
Entry->DimX = NewDimX;
Entry->DimY = NewDimY;
}
#if DEBUG_VALIDATE_LRU
static void ValidateLRU(glyph_table *Table, int ExpectedCountChange)
{
uint32_t EntryCount = 0;
glyph_entry *Sentinel = GetSentinel(Table);
size_t LastOrdering = Sentinel->Ordering;
for(uint32_t EntryIndex = Sentinel->NextLRU;
EntryIndex != 0;
)
{
glyph_entry *Entry = GetEntry(Table, EntryIndex);
Assert(Entry->Ordering < LastOrdering);
LastOrdering = Entry->Ordering;
EntryIndex = Entry->NextLRU;
++EntryCount;
}
if((Table->LastLRUCount + ExpectedCountChange) != EntryCount)
{
__debugbreak();
}
Table->LastLRUCount = EntryCount;
}
#else
#define ValidateLRU(...)
#endif
static void RecycleLRU(glyph_table *Table)
{
glyph_entry *Sentinel = GetSentinel(Table);
// NOTE(casey): There are no more unused entries, evict the least recently used one
Assert(Sentinel->PrevLRU);
// NOTE(casey): Remove least recently used element from the LRU chain
uint32_t EntryIndex = Sentinel->PrevLRU;
glyph_entry *Entry = GetEntry(Table, EntryIndex);
glyph_entry *Prev = GetEntry(Table, Entry->PrevLRU);
Prev->NextLRU = 0;
Sentinel->PrevLRU = Entry->PrevLRU;
ValidateLRU(Table, -1);
// NOTE(casey): Find the location of this entry in its hash chain
uint32_t *NextIndex = GetSlotPointer(Table, Entry->HashValue);
while(*NextIndex != EntryIndex)
{
Assert(*NextIndex);
NextIndex = &GetEntry(Table, *NextIndex)->NextWithSameHash;
}
// NOTE(casey): Remove least recently used element from its hash chain, and place it on the free chain
Assert(*NextIndex == EntryIndex);
*NextIndex = Entry->NextWithSameHash;
Entry->NextWithSameHash = Sentinel->NextWithSameHash;
Sentinel->NextWithSameHash = EntryIndex;
// NOTE(casey): Clear the index count and state
UpdateGlyphCacheEntry(Table, EntryIndex, 0, 0, 0);
++Table->Stats.RecycleCount;
}
static uint32_t PopFreeEntry(glyph_table *Table)
{
glyph_entry *Sentinel = GetSentinel(Table);
if(!Sentinel->NextWithSameHash)
{
RecycleLRU(Table);
}
uint32_t Result = Sentinel->NextWithSameHash;
Assert(Result);
// NOTE(casey): Pop this unused entry off the sentinel's chain of unused entries
glyph_entry *Entry = GetEntry(Table, Result);
Sentinel->NextWithSameHash = Entry->NextWithSameHash;
Entry->NextWithSameHash = 0;
Assert(Entry);
Assert(Entry != Sentinel);
Assert(Entry->DimX == 0);
Assert(Entry->DimY == 0);
Assert(Entry->FilledState == 0);
Assert(Entry->NextWithSameHash == 0);
Assert(Entry == GetEntry(Table, Result));
return Result;
}
static glyph_state FindGlyphEntryByHash(glyph_table *Table, glyph_hash RunHash)
{
glyph_entry *Result = 0;
uint32_t *Slot = GetSlotPointer(Table, RunHash);
uint32_t EntryIndex = *Slot;
while(EntryIndex)
{
glyph_entry *Entry = GetEntry(Table, EntryIndex);
if(GlyphHashesAreEqual(Entry->HashValue, RunHash))
{
Result = Entry;
break;
}
EntryIndex = Entry->NextWithSameHash;
}
if(Result)
{
Assert(EntryIndex);
// NOTE(casey): An existing entry was found, remove it from the LRU
glyph_entry *Prev = GetEntry(Table, Result->PrevLRU);
glyph_entry *Next = GetEntry(Table, Result->NextLRU);
Prev->NextLRU = Result->NextLRU;
Next->PrevLRU = Result->PrevLRU;
ValidateLRU(Table, -1);
++Table->Stats.HitCount;
}
else
{
// NOTE(casey): No existing entry was found, allocate a new one and link it into the hash chain
EntryIndex = PopFreeEntry(Table);
Assert(EntryIndex);
Result = GetEntry(Table, EntryIndex);
Assert(Result->FilledState == 0);
Assert(Result->NextWithSameHash == 0);
Assert(Result->DimX == 0);
Assert(Result->DimY == 0);
Result->NextWithSameHash = *Slot;
Result->HashValue = RunHash;
*Slot = EntryIndex;
++Table->Stats.MissCount;
}
// NOTE(casey): Update the LRU doubly-linked list to ensure this entry is now "first"
glyph_entry *Sentinel = GetSentinel(Table);
Assert(Result != Sentinel);
Result->NextLRU = Sentinel->NextLRU;
Result->PrevLRU = 0;
glyph_entry *NextLRU = GetEntry(Table, Sentinel->NextLRU);
NextLRU->PrevLRU = EntryIndex;
Sentinel->NextLRU = EntryIndex;
#if DEBUG_VALIDATE_LRU
Result->Ordering = Sentinel->Ordering++;
#endif
ValidateLRU(Table, 1);
glyph_state State;
State.ID = EntryIndex;
State.DimX = Result->DimX;
State.DimY = Result->DimY;
State.GPUIndex = Result->GPUIndex;
State.FilledState = Result->FilledState;
return State;
}
static void InitializeDirectGlyphTable(glyph_table_params Params, gpu_glyph_index *Table, int SkipZeroSlot)
{
Assert(Params.CacheTileCountInX >= 1);
if(SkipZeroSlot)
{
SkipZeroSlot = 1;
}
uint32_t X = SkipZeroSlot;
uint32_t Y = 0;
for(uint32_t EntryIndex = 0;
EntryIndex < (Params.ReservedTileCount - SkipZeroSlot);
++EntryIndex)
{
if(X >= Params.CacheTileCountInX)
{
X = 0;
++Y;
}
Table[EntryIndex] = PackGlyphCachePoint(X, Y);
++X;
}
}
static size_t GetGlyphTableFootprint(glyph_table_params Params)
{
size_t HashSize = Params.HashCount*sizeof(uint32_t);
size_t EntrySize = Params.EntryCount*sizeof(glyph_entry);
size_t Result = (sizeof(glyph_table) + HashSize + EntrySize);
return Result;
}
static glyph_table *PlaceGlyphTableInMemory(glyph_table_params Params, void *Memory)
{
Assert(Params.HashCount >= 1);
Assert(Params.EntryCount >= 2);
Assert(IsPowerOfTwo(Params.HashCount));
Assert(Params.CacheTileCountInX >= 1);
glyph_table *Result = 0;
if(Memory)
{
// NOTE(casey): Always put the glyph_entry array at the base of the memory, because the
// compiler may generate aligned-SSE ops, which would crash if it was unaligned.
glyph_entry *Entries = (glyph_entry *)Memory;
Result = (glyph_table *)(Entries + Params.EntryCount);
Result->HashTable = (uint32_t *)(Result + 1);
Result->Entries = Entries;
Result->HashMask = Params.HashCount - 1;
Result->HashCount = Params.HashCount;
Result->EntryCount = Params.EntryCount;
memset(Result->HashTable, 0, Result->HashCount*sizeof(Result->HashTable[0]));
uint32_t StartingTile = Params.ReservedTileCount;
glyph_entry *Sentinel = GetSentinel(Result);
uint32_t X = StartingTile % Params.CacheTileCountInX;
uint32_t Y = StartingTile / Params.CacheTileCountInX;
for(uint32_t EntryIndex = 0;
EntryIndex < Params.EntryCount;
++EntryIndex)
{
if(X >= Params.CacheTileCountInX)
{
X = 0;
++Y;
}
glyph_entry *Entry = GetEntry(Result, EntryIndex);
if((EntryIndex+1) < Params.EntryCount)
{
Entry->NextWithSameHash = EntryIndex + 1;
}
else
{
Entry->NextWithSameHash = 0;
}
Entry->GPUIndex = PackGlyphCachePoint(X, Y);
Entry->FilledState = 0;
Entry->DimX = 0;
Entry->DimY = 0;
++X;
}
GetAndClearStats(Result);
}
return Result;
}
#ifdef GLYPH_TABLE_UNDEF_ASSERT
#undef Assert
#endif