1
// Copyright (c) 2013- PPSSPP Project.
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.
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.
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
23
#include "Common/CommonTypes.h"
24
#include "Common/MemoryUtil.h"
25
#include "Core/TextureReplacer.h"
26
#include "GPU/Common/GPUDebugInterface.h"
28
enum TextureFiltering {
30
TEX_FILTER_NEAREST = 2,
31
TEX_FILTER_LINEAR = 3,
32
TEX_FILTER_LINEAR_VIDEO = 4,
35
enum FramebufferNotification {
41
struct VirtualFramebuffer;
43
class CachedTextureVulkan;
45
class TextureCacheCommon {
48
virtual ~TextureCacheCommon();
50
void LoadClut(u32 clutAddr, u32 loadBytes);
51
bool GetCurrentClutBuffer(GPUDebugBuffer &buffer);
53
virtual bool SetOffsetTexture(u32 offset);
55
// FramebufferManager keeps TextureCache updated about what regions of memory are being rendered to.
56
void NotifyFramebuffer(u32 address, VirtualFramebuffer *framebuffer, FramebufferNotification msg);
57
void NotifyConfigChanged();
58
void NotifyVideoUpload(u32 addr, int size, int width, GEBufferFormat fmt);
60
int AttachedDrawingHeight();
62
// Wow this is starting to grow big. Soon need to start looking at resizing it.
64
struct TexCacheEntry {
65
// After marking STATUS_UNRELIABLE, if it stays the same this many frames we'll trust it again.
66
const static int FRAMES_REGAIN_TRUST = 1000;
69
STATUS_HASHING = 0x00,
70
STATUS_RELIABLE = 0x01, // Don't bother rehashing.
71
STATUS_UNRELIABLE = 0x02, // Always recheck hash.
74
STATUS_ALPHA_UNKNOWN = 0x04,
75
STATUS_ALPHA_FULL = 0x00, // Has no alpha channel, or always full alpha.
76
STATUS_ALPHA_SIMPLE = 0x08, // Like above, but also has 0 alpha (e.g. 5551.)
77
STATUS_ALPHA_MASK = 0x0c,
79
STATUS_CHANGE_FREQUENT = 0x10, // Changes often (less than 6 frames in between.)
80
STATUS_CLUT_RECHECK = 0x20, // Another texture with same addr had a hashfail.
81
STATUS_DEPALETTIZE = 0x40, // Needs to go through a depalettize pass.
82
STATUS_TO_SCALE = 0x80, // Pending texture scaling in a later frame.
83
STATUS_IS_SCALED = 0x100, // Has been scaled (can't be replaceImages'd.)
84
STATUS_FREE_CHANGE = 0x200, // Allow one change before marking "frequent".
87
// Status, but int so we can zero initialize.
91
VirtualFramebuffer *framebuffer; // if null, not sourced from an FBO.
96
u32 framesUntilNextFullHash;
104
CachedTextureVulkan *vkTex;
112
// Cache the current filter settings so we can avoid setting it again.
113
// (OpenGL madness where filter settings are attached to each texture. Unused in Vulkan).
119
Status GetHashStatus() {
120
return Status(status & STATUS_MASK);
122
void SetHashStatus(Status newStatus) {
123
status = (status & ~STATUS_MASK) | newStatus;
125
Status GetAlphaStatus() {
126
return Status(status & STATUS_ALPHA_MASK);
128
void SetAlphaStatus(Status newStatus) {
129
status = (status & ~STATUS_ALPHA_MASK) | newStatus;
131
void SetAlphaStatus(Status newStatus, int level) {
132
// For non-level zero, only set more restrictive.
133
if (newStatus == STATUS_ALPHA_UNKNOWN || level == 0) {
134
SetAlphaStatus(newStatus);
135
} else if (newStatus == STATUS_ALPHA_SIMPLE && GetAlphaStatus() == STATUS_ALPHA_FULL) {
136
SetAlphaStatus(STATUS_ALPHA_SIMPLE);
140
bool Matches(u16 dim2, u8 format2, u8 maxLevel2) const;
141
u64 CacheKey() const;
142
static u64 CacheKey(u32 addr, u8 format, u16 dim, u32 cluthash);
146
// Can't be unordered_map, we use lower_bound ... although for some reason that compiles on MSVC.
147
typedef std::map<u64, TexCacheEntry> TexCache;
149
// Separate to keep main texture cache size down.
150
struct AttachedFramebufferInfo {
155
bool DecodeTextureLevel(u8 *out, int outPitch, GETextureFormat format, GEPaletteFormat clutformat, uint32_t texaddr, int level, int bufw, bool reverseColors, bool useBGRA = false);
156
void UnswizzleFromMem(u32 *dest, u32 destPitch, const u8 *texptr, u32 bufw, u32 height, u32 bytesPerPixel);
157
bool ReadIndexedTex(u8 *out, int outPitch, int level, const u8 *texptr, int bytesPerIndex, int bufw);
159
template <typename T>
160
inline const T *GetCurrentClut() {
161
return (const T *)clutBuf_;
164
u32 EstimateTexMemoryUsage(const TexCacheEntry *entry);
165
void GetSamplingParams(int &minFilt, int &magFilt, bool &sClamp, bool &tClamp, float &lodBias, u8 maxLevel, u32 addr);
166
void UpdateMaxSeenV(TexCacheEntry *entry, bool throughMode);
168
virtual bool AttachFramebuffer(TexCacheEntry *entry, u32 address, VirtualFramebuffer *framebuffer, u32 texaddrOffset = 0) = 0;
169
void AttachFramebufferValid(TexCacheEntry *entry, VirtualFramebuffer *framebuffer, const AttachedFramebufferInfo &fbInfo);
170
void AttachFramebufferInvalid(TexCacheEntry *entry, VirtualFramebuffer *framebuffer, const AttachedFramebufferInfo &fbInfo);
171
void DetachFramebuffer(TexCacheEntry *entry, u32 address, VirtualFramebuffer *framebuffer);
173
virtual void DownloadFramebufferForClut(u32 clutAddr, u32 bytes) = 0;
175
void DecimateVideos();
177
TextureReplacer replacer;
180
u32 cacheSizeEstimate_;
182
std::vector<VirtualFramebuffer *> fbCache_;
183
std::map<u64, AttachedFramebufferInfo> fbTexInfo_;
185
std::map<u32, int> videos_;
187
SimpleBuf<u32> tmpTexBuf32;
188
SimpleBuf<u16> tmpTexBuf16;
189
SimpleBuf<u32> tmpTexBufRearrange;
191
TexCacheEntry *nextTexture_;
193
// Raw is where we keep the original bytes. Converted is where we swap colors if necessary.
195
u32 *clutBufConverted_;
196
// This is the active one.
201
u32 clutRenderAddress_;
202
u32 clutRenderOffset_;
203
// True if the clut is just alpha values in the same order (RGBA4444-bit only.)
204
bool clutAlphaLinear_;
205
u16 clutAlphaLinearColor_;
207
int standardScaleFactor_;
210
inline bool TextureCacheCommon::TexCacheEntry::Matches(u16 dim2, u8 format2, u8 maxLevel2) const {
211
return dim == dim2 && format == format2 && maxLevel == maxLevel2;
214
inline u64 TextureCacheCommon::TexCacheEntry::CacheKey() const {
215
return CacheKey(addr, format, dim, cluthash);
218
inline u64 TextureCacheCommon::TexCacheEntry::CacheKey(u32 addr, u8 format, u16 dim, u32 cluthash) {
219
u64 cachekey = ((u64)(addr & 0x3FFFFFFF) << 32) | dim;
220
bool hasClut = (format & 4) != 0;
222
cachekey ^= cluthash;