Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Common/TextureCacheCommon.h
3186 views
1
// Copyright (c) 2013- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#pragma once
19
20
#include <map>
21
#include <vector>
22
#include <memory>
23
24
#include "Common/CommonTypes.h"
25
#include "Common/MemoryUtil.h"
26
#include "Core/System.h"
27
#include "GPU/GPU.h"
28
#include "GPU/Common/GPUDebugInterface.h"
29
#include "GPU/Common/TextureDecoder.h"
30
#include "GPU/Common/TextureScalerCommon.h"
31
#include "GPU/Common/TextureShaderCommon.h"
32
#include "GPU/Common/TextureReplacer.h"
33
#include "GPU/GPUDefinitions.h"
34
35
class Draw2D;
36
37
enum FramebufferNotification {
38
NOTIFY_FB_CREATED,
39
NOTIFY_FB_UPDATED,
40
NOTIFY_FB_DESTROYED,
41
};
42
43
// Changes more frequent than this will be considered "frequent" and prevent texture scaling.
44
#define TEXCACHE_FRAME_CHANGE_FREQUENT 6
45
// Note: only used when hash backoff is disabled.
46
#define TEXCACHE_FRAME_CHANGE_FREQUENT_REGAIN_TRUST 33
47
48
#define TEXCACHE_MAX_TEXELS_SCALED (256*256) // Per frame
49
50
struct VirtualFramebuffer;
51
class TextureReplacer;
52
class ShaderManagerCommon;
53
54
enum class TexDecodeFlags {
55
EXPAND32 = 1,
56
REVERSE_COLORS = 2,
57
TO_CLUT8 = 4,
58
};
59
ENUM_CLASS_BITOPS(TexDecodeFlags);
60
61
namespace Draw {
62
class DrawContext;
63
class Texture;
64
}
65
66
namespace GPURecord {
67
class Recorder;
68
}
69
70
// Used by D3D11 and Vulkan, could be used by modern GL
71
struct SamplerCacheKey {
72
union {
73
uint64_t fullKey;
74
struct {
75
// These are 8.8 fixed point.
76
int16_t maxLevel;
77
int16_t minLevel;
78
int16_t lodBias;
79
80
bool mipEnable : 1;
81
bool minFilt : 1;
82
bool mipFilt : 1;
83
bool magFilt : 1;
84
bool sClamp : 1;
85
bool tClamp : 1;
86
bool aniso : 1;
87
bool texture3d : 1;
88
};
89
};
90
bool operator < (const SamplerCacheKey &other) const {
91
return fullKey < other.fullKey;
92
}
93
void ToString(std::string *str) const {
94
str->resize(sizeof(*this));
95
memcpy(&(*str)[0], this, sizeof(*this));
96
}
97
void FromString(const std::string &str) {
98
memcpy(this, &str[0], sizeof(*this));
99
}
100
};
101
102
class GLRTexture;
103
class VulkanTexture;
104
105
// Allow the extra bits from the remasters for the purposes of this.
106
inline int dimWidth(u16 dim) {
107
return 1 << (dim & 0xFF);
108
}
109
110
inline int dimHeight(u16 dim) {
111
return 1 << ((dim >> 8) & 0xFF);
112
}
113
114
// Enough information about a texture to match it to framebuffers.
115
struct TextureDefinition {
116
u32 addr;
117
u16 bufw;
118
u16 dim;
119
GETextureFormat format;
120
};
121
122
// Texture replacement state machine:
123
// Call FindReplacement during PrepareBuild.
124
// If replacedTexture gets set: If not found, -> STATUS_TO_REPLACE, otherwise directly -> STATUS_IS_SCALED.
125
// If replacedTexture is null, leave it at null.
126
// If replacedTexture is set in SetTexture and STATUS_IS_SCALED is not set, query status. If ready rebuild texture, which will set STATUS_IS_SCALED.
127
128
// NOTE: These only handle textures loaded directly from PSP memory contents.
129
// Framebuffer textures do not have entries, we bind the framebuffers directly.
130
// At one point we might merge the concepts of framebuffers and textures, but that
131
// moment is far away.
132
133
// TODO: Shrink this struct. There is some fluff.
134
struct TexCacheEntry {
135
~TexCacheEntry() {
136
if (texturePtr || textureName || vkTex)
137
Crash();
138
}
139
// After marking STATUS_UNRELIABLE, if it stays the same this many frames we'll trust it again.
140
const static int FRAMES_REGAIN_TRUST = 1000;
141
142
enum TexStatus {
143
STATUS_HASHING = 0x00,
144
STATUS_RELIABLE = 0x01, // Don't bother rehashing.
145
STATUS_UNRELIABLE = 0x02, // Always recheck hash.
146
STATUS_MASK = 0x03,
147
148
STATUS_ALPHA_UNKNOWN = 0x04,
149
STATUS_ALPHA_FULL = 0x00, // Has no alpha channel, or always full alpha.
150
STATUS_ALPHA_MASK = 0x04,
151
152
STATUS_CLUT_VARIANTS = 0x08, // Has multiple CLUT variants.
153
STATUS_CHANGE_FREQUENT = 0x10, // Changes often (less than 6 frames in between.)
154
STATUS_CLUT_RECHECK = 0x20, // Another texture with same addr had a hashfail.
155
STATUS_TO_SCALE = 0x80, // Pending texture scaling in a later frame.
156
STATUS_IS_SCALED_OR_REPLACED = 0x100, // Has been scaled already (ignored for replacement checks).
157
STATUS_TO_REPLACE = 0x0200, // Pending texture replacement.
158
// When hashing large textures, we optimize 512x512 down to 512x272 by default, since this
159
// is commonly the only part accessed. If access is made above 272, we hash the entire
160
// texture, and set this flag to allow scaling the texture just once for the new hash.
161
STATUS_FREE_CHANGE = 0x0400, // Allow one change before marking "frequent".
162
163
STATUS_NO_MIPS = 0x0800, // Has bad or unusable mipmap levels.
164
165
STATUS_FRAMEBUFFER_OVERLAP = 0x1000,
166
167
STATUS_FORCE_REBUILD = 0x2000,
168
169
STATUS_3D = 0x4000,
170
171
STATUS_CLUT_GPU = 0x8000,
172
173
STATUS_VIDEO = 0x10000,
174
STATUS_BGRA = 0x20000,
175
};
176
177
// TexStatus enum flag combination.
178
u32 status;
179
180
u32 addr;
181
u32 minihash;
182
u8 format; // GeTextureFormat
183
u8 maxLevel;
184
u16 dim;
185
u16 bufw;
186
union {
187
GLRTexture *textureName;
188
void *texturePtr;
189
VulkanTexture *vkTex;
190
};
191
#ifdef _WIN32
192
void *textureView; // Used by D3D11 only for the shader resource view.
193
#endif
194
int invalidHint;
195
int lastFrame;
196
int numFrames;
197
int numInvalidated;
198
u32 framesUntilNextFullHash;
199
u32 fullhash;
200
u32 cluthash;
201
u16 maxSeenV;
202
ReplacedTexture *replacedTexture;
203
204
TexStatus GetHashStatus() {
205
return TexStatus(status & STATUS_MASK);
206
}
207
void SetHashStatus(TexStatus newStatus) {
208
status = (status & ~STATUS_MASK) | newStatus;
209
}
210
TexStatus GetAlphaStatus() {
211
return TexStatus(status & STATUS_ALPHA_MASK);
212
}
213
void SetAlphaStatus(TexStatus newStatus) {
214
status = (status & ~STATUS_ALPHA_MASK) | newStatus;
215
}
216
void SetAlphaStatus(TexStatus newStatus, int level) {
217
// For non-level zero, only set more restrictive.
218
if (newStatus == STATUS_ALPHA_UNKNOWN || level == 0) {
219
SetAlphaStatus(newStatus);
220
}
221
}
222
void SetAlphaStatus(CheckAlphaResult alphaResult, int level) {
223
TexStatus newStatus = (TexStatus)alphaResult;
224
// For non-level zero, only set more restrictive.
225
if (newStatus == STATUS_ALPHA_UNKNOWN || level == 0) {
226
SetAlphaStatus(newStatus);
227
}
228
}
229
230
// This is the full size in RAM, not the half size we use sometimes as a "safe" underestimate.
231
u32 SizeInRAM() const {
232
return (textureBitsPerPixel[format] * bufw * dimHeight(dim)) / 8;
233
}
234
235
bool Matches(u16 dim2, u8 format2, u8 maxLevel2) const;
236
u64 CacheKey() const;
237
static u64 CacheKey(u32 addr, u8 format, u16 dim, u32 cluthash);
238
};
239
240
// Can't be unordered_map, we use lower_bound ... although for some reason that (used to?) compiles on MSVC.
241
// Would really like to replace this with DenseHashMap but can't as long as we need lower_bound.
242
typedef std::map<u64, std::unique_ptr<TexCacheEntry>> TexCache;
243
244
// Urgh.
245
#ifdef IGNORE
246
#undef IGNORE
247
#endif
248
249
struct FramebufferMatchInfo {
250
int16_t xOffset;
251
int16_t yOffset;
252
bool reinterpret;
253
GEBufferFormat reinterpretTo;
254
};
255
256
struct AttachCandidate {
257
VirtualFramebuffer *fb;
258
FramebufferMatchInfo match;
259
RasterChannel channel;
260
int relevancy;
261
262
std::string ToString() const;
263
};
264
265
class FramebufferManagerCommon;
266
267
struct BuildTexturePlan {
268
// Inputs
269
bool hardwareScaling = false;
270
bool slowScaler = true;
271
272
// Set if the PSP software specified an unusual mip chain,
273
// such as the same size throughout, or anything else that doesn't divide by
274
// two on each level. If this is set, we won't generate mips nor use any.
275
// However, we still respect baseLevelSrc.
276
bool badMipSizes;
277
278
// Number of mip levels to load from PSP memory (or replacement).
279
int levelsToLoad;
280
281
// The number of levels in total to create.
282
// If greater than maxLevelToLoad, the backend is expected to either generate
283
// the missing levels, or limit itself to levelsToLoad levels.
284
int levelsToCreate;
285
286
// The maximum number of mips levels we can create for this texture.
287
int maxPossibleLevels;
288
289
// Load the 0-mip from this PSP texture level instead of 0.
290
// If non-zero, we are only loading one level.
291
int baseLevelSrc;
292
293
// The scale factor of the final texture.
294
int scaleFactor;
295
296
// Whether it's a video texture or not. Some decisions might depend on this.
297
bool isVideo;
298
299
// Unscaled size of the 0-mip of the original texture.
300
// Don't really need to have it here, but convenient.
301
int w;
302
int h;
303
304
// Scaled (or replaced) size of the 0-mip of the final texture.
305
int createW;
306
int createH;
307
308
// Used for 3D textures only. If not a 3D texture, will be 1.
309
int depth;
310
311
// The replacement for the texture.
312
ReplacedTexture *replaced;
313
// Need to only check once since it can change during the load!
314
bool doReplace;
315
bool saveTexture;
316
317
// TODO: Expand32 should probably also be decided in PrepareBuildTexture.
318
bool decodeToClut8;
319
320
void GetMipSize(int level, int *w, int *h) const {
321
if (doReplace) {
322
replaced->GetSize(level, w, h);
323
return;
324
}
325
if (depth == 1) {
326
*w = createW >> level;
327
*h = createH >> level;
328
} else {
329
// 3D texture, we look for layers instead of levels.
330
*w = createW;
331
*h = createH;
332
}
333
}
334
};
335
336
class TextureCacheCommon {
337
public:
338
TextureCacheCommon(Draw::DrawContext *draw, Draw2D *draw2D);
339
virtual ~TextureCacheCommon();
340
341
void LoadClut(u32 clutAddr, u32 loadBytes, GPURecord::Recorder *recorder);
342
bool GetCurrentClutBuffer(GPUDebugBuffer &buffer);
343
344
// This updates nextTexture_ / nextFramebufferTexture_, which is then used by ApplyTexture.
345
// TODO: Return stuff directly instead of keeping state.
346
TexCacheEntry *SetTexture();
347
348
void SetShaderManager(ShaderManagerCommon *sm) {
349
shaderManager_ = sm;
350
}
351
352
void ApplyTexture(bool doBind = true);
353
bool SetOffsetTexture(u32 yOffset);
354
void Invalidate(u32 addr, int size, GPUInvalidationType type);
355
void InvalidateAll(GPUInvalidationType type);
356
void ClearNextFrame();
357
358
TextureShaderCache *GetTextureShaderCache() { return textureShaderCache_; }
359
360
virtual void ForgetLastTexture() = 0;
361
virtual void Clear(bool delete_them);
362
virtual void NotifyConfigChanged();
363
virtual void ApplySamplingParams(const SamplerCacheKey &key) = 0;
364
365
// FramebufferManager keeps TextureCache updated about what regions of memory are being rendered to,
366
// so that it can invalidate TexCacheEntries pointed at those addresses.
367
void NotifyFramebuffer(VirtualFramebuffer *framebuffer, FramebufferNotification msg);
368
void NotifyWriteFormattedFromMemory(u32 addr, int size, int width, GEBufferFormat fmt);
369
370
size_t NumLoadedTextures() const {
371
return cache_.size();
372
}
373
374
bool IsFakeMipmapChange() {
375
return PSP_CoreParameter().compat.flags().FakeMipmapChange && gstate.getTexLevelMode() == GE_TEXLEVEL_MODE_CONST;
376
}
377
bool VideoIsPlaying() {
378
return !videos_.empty();
379
}
380
virtual bool GetCurrentTextureDebug(GPUDebugBuffer &buffer, int level, bool *isFramebuffer) { return false; }
381
382
virtual void StartFrame();
383
384
virtual void DeviceLost() = 0;
385
virtual void DeviceRestore(Draw::DrawContext *draw) = 0;
386
387
// Accessors for the debugger.
388
virtual void *GetNativeTextureView(const TexCacheEntry *entry, bool flat) const = 0;
389
390
const TexCache &Cache() const { return cache_; }
391
const TexCache &SecondCache() const { return secondCache_; }
392
393
const size_t CacheSizeEstimate() const { return cacheSizeEstimate_; }
394
const size_t SecondCacheSizeEstimate() const { return secondCacheSizeEstimate_; }
395
396
struct VideoInfo {
397
u32 addr;
398
u32 size;
399
int flips;
400
};
401
402
const std::vector<VideoInfo> &Videos() const {
403
return videos_;
404
}
405
406
protected:
407
bool PrepareBuildTexture(BuildTexturePlan &plan, TexCacheEntry *entry);
408
409
virtual void BindTexture(TexCacheEntry *entry) = 0;
410
virtual void Unbind() = 0;
411
virtual void ReleaseTexture(TexCacheEntry *entry, bool delete_them) = 0;
412
void DeleteTexture(TexCache::iterator it);
413
void Decimate(TexCacheEntry *exceptThisOne, bool forcePressure); // forcePressure defaults to false.
414
415
void ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer, GETextureFormat texFormat, RasterChannel channel);
416
void ApplyTextureDepal(TexCacheEntry *entry);
417
418
void HandleTextureChange(TexCacheEntry *const entry, const char *reason, bool initialMatch, bool doDelete);
419
virtual void BuildTexture(TexCacheEntry *const entry) = 0;
420
virtual void UpdateCurrentClut(GEPaletteFormat clutFormat, u32 clutBase, bool clutIndexIsSimple) = 0;
421
bool CheckFullHash(TexCacheEntry *entry, bool &doDelete);
422
423
virtual void BindAsClutTexture(Draw::Texture *tex, bool smooth) {}
424
425
CheckAlphaResult DecodeTextureLevel(u8 *out, int outPitch, GETextureFormat format, GEPaletteFormat clutformat, uint32_t texaddr, int level, int bufw, TexDecodeFlags flags);
426
static void UnswizzleFromMem(u32 *dest, u32 destPitch, const u8 *texptr, u32 bufw, u32 height, u32 bytesPerPixel);
427
CheckAlphaResult ReadIndexedTex(u8 *out, int outPitch, int level, const u8 *texptr, int bytesPerIndex, int bufw, bool reverseColors, bool expandTo32Bit);
428
ReplacedTexture *FindReplacement(TexCacheEntry *entry, int *w, int *h, int *d);
429
void PollReplacement(TexCacheEntry *entry, int *w, int *h, int *d);
430
431
// Return value is mapData normally, but could be another buffer allocated with AllocateAlignedMemory.
432
void LoadTextureLevel(TexCacheEntry &entry, uint8_t *mapData, size_t dataSize, int mapRowPitch, BuildTexturePlan &plan, int srcLevel, Draw::DataFormat dstFmt, TexDecodeFlags texDecFlags);
433
434
template <typename T>
435
inline const T *GetCurrentClut() {
436
return (const T *)clutBuf_;
437
}
438
439
template <typename T>
440
inline const T *GetCurrentRawClut() {
441
return (const T *)clutBufRaw_;
442
}
443
444
static u32 EstimateTexMemoryUsage(const TexCacheEntry *entry);
445
446
SamplerCacheKey GetSamplingParams(int maxLevel, const TexCacheEntry *entry);
447
SamplerCacheKey GetFramebufferSamplingParams(u16 bufferWidth, u16 bufferHeight);
448
void UpdateMaxSeenV(TexCacheEntry *entry, bool throughMode);
449
450
bool MatchFramebuffer(const TextureDefinition &entry, VirtualFramebuffer *framebuffer, u32 texaddrOffset, RasterChannel channel, FramebufferMatchInfo *matchInfo) const;
451
452
bool GetBestFramebufferCandidate(const TextureDefinition &entry, u32 texAddrOffset, AttachCandidate *bestCandidate) const;
453
454
void SetTextureFramebuffer(const AttachCandidate &candidate);
455
bool GetCurrentFramebufferTextureDebug(GPUDebugBuffer &buffer, bool *isFramebuffer);
456
457
virtual void BoundFramebufferTexture() {}
458
459
void DecimateVideos();
460
bool IsVideo(u32 texaddr) const;
461
462
static CheckAlphaResult CheckCLUTAlpha(const uint8_t *pixelData, GEPaletteFormat clutFmt, int w);
463
464
static inline u32 QuickTexHash(TextureReplacer &replacer, u32 addr, int bufw, int w, int h, bool swizzled, GETextureFormat format, const TexCacheEntry *entry) {
465
if (replacer.Enabled()) {
466
return replacer.ComputeHash(addr, bufw, w, h, swizzled, format, entry->maxSeenV);
467
}
468
469
if (h == 512 && entry->maxSeenV < 512 && entry->maxSeenV != 0) {
470
h = (int)entry->maxSeenV;
471
}
472
473
u32 sizeInRAM;
474
if (swizzled) {
475
// In swizzle mode, textures are stored in rectangular blocks with the height 8.
476
// That means that for a 64x4 texture, like in issue #9308, we would only hash half of the texture!
477
// In theory, we should make sure to only hash half of each block, but in reality it's not likely that
478
// games are using that memory for anything else. So we'll just make sure to compute the full size to hash.
479
// To do that, we just use the same calculation but round the height upwards to the nearest multiple of 8.
480
sizeInRAM = (textureBitsPerPixel[format] * bufw * ((h + 7) & ~7)) >> 3;
481
} else {
482
sizeInRAM = (textureBitsPerPixel[format] * bufw * h) >> 3;
483
}
484
const u32 *checkp = (const u32 *)Memory::GetPointer(addr);
485
486
gpuStats.numTextureDataBytesHashed += sizeInRAM;
487
488
if (Memory::IsValidAddress(addr + sizeInRAM)) {
489
return StableQuickTexHash(checkp, sizeInRAM);
490
} else {
491
return 0;
492
}
493
}
494
495
static inline u32 MiniHash(const u32 *ptr) {
496
return ptr[0];
497
}
498
499
Draw::DrawContext *draw_;
500
Draw2D *draw2D_;
501
502
TextureReplacer replacer_;
503
TextureScalerCommon scaler_;
504
FramebufferManagerCommon *framebufferManager_;
505
TextureShaderCache *textureShaderCache_;
506
ShaderManagerCommon *shaderManager_;
507
508
bool clearCacheNextFrame_ = false;
509
bool lowMemoryMode_ = false;
510
511
int decimationCounter_;
512
int texelsScaledThisFrame_ = 0;
513
int timesInvalidatedAllThisFrame_ = 0;
514
double replacementTimeThisFrame_ = 0;
515
// Recomputed once per frame. Depends FPS and soon also config.
516
double replacementFrameBudgetSeconds_ = 0.5 / 60.0;
517
518
TexCache cache_;
519
u32 cacheSizeEstimate_ = 0;
520
521
TexCache secondCache_;
522
u32 secondCacheSizeEstimate_ = 0;
523
524
std::vector<VideoInfo> videos_;
525
526
AlignedVector<u32, 16> tmpTexBuf32_;
527
AlignedVector<u32, 16> tmpTexBufRearrange_;
528
529
TexCacheEntry *nextTexture_ = nullptr;
530
bool failedTexture_ = false;
531
VirtualFramebuffer *nextFramebufferTexture_ = nullptr;
532
RasterChannel nextFramebufferTextureChannel_ = RASTER_COLOR;
533
534
u32 clutHash_ = 0;
535
536
// Raw is where we keep the original bytes. Converted is where we swap colors if necessary.
537
u32 *clutBufRaw_;
538
u32 *clutBufConverted_;
539
// This is the active one.
540
u32 *clutBuf_;
541
u32 clutLastFormat_ = 0xFFFFFFFF;
542
u32 clutTotalBytes_ = 0;
543
u32 clutMaxBytes_ = 0;
544
u32 clutRenderAddress_ = 0xFFFFFFFF;
545
u32 clutRenderOffset_;
546
GEBufferFormat clutRenderFormat_;
547
548
// True if the clut is just alpha values in the same order (RGBA4444-bit only.)
549
bool clutAlphaLinear_ = false;
550
u16 clutAlphaLinearColor_;
551
552
// Facilities for GPU depal of static textures.
553
Draw::Framebuffer *dynamicClutTemp_ = nullptr;
554
Draw::Framebuffer *dynamicClutFbo_ = nullptr;
555
556
int standardScaleFactor_;
557
int shaderScaleFactor_ = 0;
558
559
const char *nextChangeReason_;
560
bool nextNeedsRehash_;
561
bool nextNeedsChange_;
562
bool nextNeedsRebuild_;
563
564
u32 *expandClut_;
565
};
566
567
inline bool TexCacheEntry::Matches(u16 dim2, u8 format2, u8 maxLevel2) const {
568
return dim == dim2 && format == format2 && maxLevel == maxLevel2;
569
}
570
571
inline u64 TexCacheEntry::CacheKey() const {
572
return CacheKey(addr, format, dim, cluthash);
573
}
574
575
inline u64 TexCacheEntry::CacheKey(u32 addr, u8 format, u16 dim, u32 cluthash) {
576
u64 cachekey = ((u64)(addr & 0x3FFFFFFF) << 32) | dim;
577
bool hasClut = (format & 4) != 0;
578
if (hasClut) {
579
cachekey ^= cluthash;
580
}
581
return cachekey;
582
}
583
584