forked from melonDS-emu/melonDS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPU3D_Texcache.h
330 lines (266 loc) · 10.7 KB
/
GPU3D_Texcache.h
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
#ifndef GPU3D_TEXCACHE
#define GPU3D_TEXCACHE
#include "types.h"
#include "GPU.h"
#include <assert.h>
#include <unordered_map>
#include <vector>
#define XXH_STATIC_LINKING_ONLY
#include "xxhash/xxhash.h"
namespace melonDS
{
inline u32 TextureWidth(u32 texparam)
{
return 8 << ((texparam >> 20) & 0x7);
}
inline u32 TextureHeight(u32 texparam)
{
return 8 << ((texparam >> 23) & 0x7);
}
enum
{
outputFmt_RGB6A5,
outputFmt_RGBA8,
outputFmt_BGRA8
};
template <int outputFmt>
void ConvertBitmapTexture(u32 width, u32 height, u32* output, u32 addr, GPU& gpu);
template <int outputFmt>
void ConvertCompressedTexture(u32 width, u32 height, u32* output, u32 addr, u32 addrAux, u32 palAddr, GPU& gpu);
template <int outputFmt, int X, int Y>
void ConvertAXIYTexture(u32 width, u32 height, u32* output, u32 addr, u32 palAddr, GPU& gpu);
template <int outputFmt, int colorBits>
void ConvertNColorsTexture(u32 width, u32 height, u32* output, u32 addr, u32 palAddr, bool color0Transparent, GPU& gpu);
template <typename TexLoaderT, typename TexHandleT>
class Texcache
{
public:
Texcache(const TexLoaderT& texloader)
: TexLoader(texloader) // probably better if this would be a move constructor???
{}
u64 MaskedHash(u8* vram, u32 vramSize, u32 addr, u32 size)
{
u64 hash = 0;
while (size > 0)
{
u32 pieceSize;
if (addr + size > vramSize)
// wraps around, only do the part inside
pieceSize = vramSize - addr;
else
// fits completely inside
pieceSize = size;
hash = XXH64(&vram[addr], pieceSize, hash);
addr += pieceSize;
addr &= (vramSize - 1);
assert(size >= pieceSize);
size -= pieceSize;
}
return hash;
}
bool CheckInvalid(u32 start, u32 size, u64 oldHash, u64* dirty, u8* vram, u32 vramSize)
{
u32 startBit = start / VRAMDirtyGranularity;
u32 bitsCount = ((start + size + VRAMDirtyGranularity - 1) / VRAMDirtyGranularity) - startBit;
u32 startEntry = startBit >> 6;
u64 entriesCount = ((startBit + bitsCount + 0x3F) >> 6) - startEntry;
for (u32 j = startEntry; j < startEntry + entriesCount; j++)
{
if (GetRangedBitMask(j, startBit, bitsCount) & dirty[j & ((vramSize / VRAMDirtyGranularity)-1)])
{
if (MaskedHash(vram, vramSize, start, size) != oldHash)
return true;
}
}
return false;
}
bool Update(GPU& gpu)
{
auto textureDirty = gpu.VRAMDirty_Texture.DeriveState(gpu.VRAMMap_Texture, gpu);
auto texPalDirty = gpu.VRAMDirty_TexPal.DeriveState(gpu.VRAMMap_TexPal, gpu);
bool textureChanged = gpu.MakeVRAMFlat_TextureCoherent(textureDirty);
bool texPalChanged = gpu.MakeVRAMFlat_TexPalCoherent(texPalDirty);
if (textureChanged || texPalChanged)
{
//printf("check invalidation %d\n", TexCache.size());
for (auto it = Cache.begin(); it != Cache.end();)
{
TexCacheEntry& entry = it->second;
if (textureChanged)
{
for (u32 i = 0; i < 2; i++)
{
if (CheckInvalid(entry.TextureRAMStart[i], entry.TextureRAMSize[i],
entry.TextureHash[i],
textureDirty.Data,
gpu.VRAMFlat_Texture, sizeof(gpu.VRAMFlat_Texture)))
goto invalidate;
}
}
if (texPalChanged && entry.TexPalSize > 0)
{
if (CheckInvalid(entry.TexPalStart, entry.TexPalSize,
entry.TexPalHash,
texPalDirty.Data,
gpu.VRAMFlat_TexPal, sizeof(gpu.VRAMFlat_TexPal)))
goto invalidate;
}
it++;
continue;
invalidate:
FreeTextures[entry.WidthLog2][entry.HeightLog2].push_back(entry.Texture);
//printf("invalidating texture %d\n", entry.ImageDescriptor);
it = Cache.erase(it);
}
return true;
}
return false;
}
void GetTexture(GPU& gpu, u32 texParam, u32 palBase, TexHandleT& textureHandle, u32& layer, u32*& helper)
{
// remove sampling and texcoord gen params
texParam &= ~0xC00F0000;
u32 fmt = (texParam >> 26) & 0x7;
u64 key = texParam;
if (fmt != 7)
{
key |= (u64)palBase << 32;
if (fmt == 5)
key &= ~((u64)1 << 29);
}
//printf("%" PRIx64 " %" PRIx32 " %" PRIx32 "\n", key, texParam, palBase);
assert(fmt != 0 && "no texture is not a texture format!");
auto it = Cache.find(key);
if (it != Cache.end())
{
textureHandle = it->second.Texture.TextureID;
layer = it->second.Texture.Layer;
helper = &it->second.LastVariant;
return;
}
u32 widthLog2 = (texParam >> 20) & 0x7;
u32 heightLog2 = (texParam >> 23) & 0x7;
u32 width = 8 << widthLog2;
u32 height = 8 << heightLog2;
u32 addr = (texParam & 0xFFFF) * 8;
TexCacheEntry entry = {0};
entry.TextureRAMStart[0] = addr;
entry.WidthLog2 = widthLog2;
entry.HeightLog2 = heightLog2;
// apparently a new texture
if (fmt == 7)
{
entry.TextureRAMSize[0] = width*height*2;
ConvertBitmapTexture<outputFmt_RGB6A5>(width, height, DecodingBuffer, addr, gpu);
}
else if (fmt == 5)
{
u32 slot1addr = 0x20000 + ((addr & 0x1FFFC) >> 1);
if (addr >= 0x40000)
slot1addr += 0x10000;
entry.TextureRAMSize[0] = width*height/16*4;
entry.TextureRAMStart[1] = slot1addr;
entry.TextureRAMSize[1] = width*height/16*2;
entry.TexPalStart = palBase*16;
entry.TexPalSize = 0x10000;
ConvertCompressedTexture<outputFmt_RGB6A5>(width, height, DecodingBuffer, addr, slot1addr, entry.TexPalStart, gpu);
}
else
{
u32 texSize, palAddr = palBase*16, numPalEntries;
switch (fmt)
{
case 1: texSize = width*height; numPalEntries = 32; break;
case 6: texSize = width*height; numPalEntries = 8; break;
case 2: texSize = width*height/4; numPalEntries = 4; palAddr >>= 1; break;
case 3: texSize = width*height/2; numPalEntries = 16; break;
case 4: texSize = width*height; numPalEntries = 256; break;
}
palAddr &= 0x1FFFF;
/*printf("creating texture | fmt: %d | %dx%d | %08x | %08x\n", fmt, width, height, addr, palAddr);
svcSleepThread(1000*1000);*/
entry.TextureRAMSize[0] = texSize;
entry.TexPalStart = palAddr;
entry.TexPalSize = numPalEntries*2;
//assert(entry.TexPalStart+entry.TexPalSize <= 128*1024*1024);
bool color0Transparent = texParam & (1 << 29);
switch (fmt)
{
case 1: ConvertAXIYTexture<outputFmt_RGB6A5, 3, 5>(width, height, DecodingBuffer, addr, palAddr, gpu); break;
case 6: ConvertAXIYTexture<outputFmt_RGB6A5, 5, 3>(width, height, DecodingBuffer, addr, palAddr, gpu); break;
case 2: ConvertNColorsTexture<outputFmt_RGB6A5, 2>(width, height, DecodingBuffer, addr, palAddr, color0Transparent, gpu); break;
case 3: ConvertNColorsTexture<outputFmt_RGB6A5, 4>(width, height, DecodingBuffer, addr, palAddr, color0Transparent, gpu); break;
case 4: ConvertNColorsTexture<outputFmt_RGB6A5, 8>(width, height, DecodingBuffer, addr, palAddr, color0Transparent, gpu); break;
}
}
for (int i = 0; i < 2; i++)
{
if (entry.TextureRAMSize[i])
entry.TextureHash[i] = MaskedHash(gpu.VRAMFlat_Texture, sizeof(gpu.VRAMFlat_Texture),
entry.TextureRAMStart[i], entry.TextureRAMSize[i]);
}
if (entry.TexPalSize)
entry.TexPalHash = MaskedHash(gpu.VRAMFlat_TexPal, sizeof(gpu.VRAMFlat_TexPal),
entry.TexPalStart, entry.TexPalSize);
auto& texArrays = TexArrays[widthLog2][heightLog2];
auto& freeTextures = FreeTextures[widthLog2][heightLog2];
if (freeTextures.size() == 0)
{
texArrays.resize(texArrays.size()+1);
TexHandleT& array = texArrays[texArrays.size()-1];
u32 layers = std::min<u32>((8*1024*1024) / (width*height*4), 64);
// allocate new array texture
//printf("allocating new layer set for %d %d %d %d\n", width, height, texArrays.size()-1, array.ImageDescriptor);
array = TexLoader.GenerateTexture(width, height, layers);
for (u32 i = 0; i < layers; i++)
{
freeTextures.push_back(TexArrayEntry{array, i});
}
}
TexArrayEntry storagePlace = freeTextures[freeTextures.size()-1];
freeTextures.pop_back();
entry.Texture = storagePlace;
TexLoader.UploadTexture(storagePlace.TextureID, width, height, storagePlace.Layer, DecodingBuffer);
//printf("using storage place %d %d | %d %d (%d)\n", width, height, storagePlace.TexArrayIdx, storagePlace.LayerIdx, array.ImageDescriptor);
textureHandle = storagePlace.TextureID;
layer = storagePlace.Layer;
helper = &Cache.emplace(std::make_pair(key, entry)).first->second.LastVariant;
}
void Reset()
{
for (u32 i = 0; i < 8; i++)
{
for (u32 j = 0; j < 8; j++)
{
for (u32 k = 0; k < TexArrays[i][j].size(); k++)
TexLoader.DeleteTexture(TexArrays[i][j][k]);
TexArrays[i][j].clear();
FreeTextures[i][j].clear();
}
}
Cache.clear();
}
private:
struct TexArrayEntry
{
TexHandleT TextureID;
u32 Layer;
};
struct TexCacheEntry
{
u32 LastVariant; // very cheap way to make variant lookup faster
u32 TextureRAMStart[2], TextureRAMSize[2];
u32 TexPalStart, TexPalSize;
u8 WidthLog2, HeightLog2;
TexArrayEntry Texture;
u64 TextureHash[2];
u64 TexPalHash;
};
std::unordered_map<u64, TexCacheEntry> Cache;
TexLoaderT TexLoader;
std::vector<TexArrayEntry> FreeTextures[8][8];
std::vector<TexHandleT> TexArrays[8][8];
u32 DecodingBuffer[1024*1024];
};
}
#endif