#pragma once
#include <map>
#include <vector>
#include <memory>
#include "Common/CommonTypes.h"
#include "Common/MemoryUtil.h"
#include "Core/System.h"
#include "GPU/GPU.h"
#include "GPU/Common/GPUDebugInterface.h"
#include "GPU/Common/TextureDecoder.h"
#include "GPU/Common/TextureScalerCommon.h"
#include "GPU/Common/TextureShaderCommon.h"
#include "GPU/Common/TextureReplacer.h"
#include "GPU/GPUDefinitions.h"
class Draw2D;
enum FramebufferNotification {
NOTIFY_FB_CREATED,
NOTIFY_FB_UPDATED,
NOTIFY_FB_DESTROYED,
};
#define TEXCACHE_FRAME_CHANGE_FREQUENT 6
#define TEXCACHE_FRAME_CHANGE_FREQUENT_REGAIN_TRUST 33
#define TEXCACHE_MAX_TEXELS_SCALED (256*256)
struct VirtualFramebuffer;
class TextureReplacer;
class ShaderManagerCommon;
enum class TexDecodeFlags {
EXPAND32 = 1,
REVERSE_COLORS = 2,
TO_CLUT8 = 4,
};
ENUM_CLASS_BITOPS(TexDecodeFlags);
namespace Draw {
class DrawContext;
class Texture;
}
namespace GPURecord {
class Recorder;
}
struct SamplerCacheKey {
union {
uint64_t fullKey;
struct {
int16_t maxLevel;
int16_t minLevel;
int16_t lodBias;
bool mipEnable : 1;
bool minFilt : 1;
bool mipFilt : 1;
bool magFilt : 1;
bool sClamp : 1;
bool tClamp : 1;
bool aniso : 1;
bool texture3d : 1;
};
};
bool operator < (const SamplerCacheKey &other) const {
return fullKey < other.fullKey;
}
void ToString(std::string *str) const {
str->resize(sizeof(*this));
memcpy(&(*str)[0], this, sizeof(*this));
}
void FromString(const std::string &str) {
memcpy(this, &str[0], sizeof(*this));
}
};
class GLRTexture;
class VulkanTexture;
inline int dimWidth(u16 dim) {
return 1 << (dim & 0xFF);
}
inline int dimHeight(u16 dim) {
return 1 << ((dim >> 8) & 0xFF);
}
struct TextureDefinition {
u32 addr;
u16 bufw;
u16 dim;
GETextureFormat format;
};
struct TexCacheEntry {
~TexCacheEntry() {
if (texturePtr || textureName || vkTex)
Crash();
}
const static int FRAMES_REGAIN_TRUST = 1000;
enum TexStatus {
STATUS_HASHING = 0x00,
STATUS_RELIABLE = 0x01,
STATUS_UNRELIABLE = 0x02,
STATUS_MASK = 0x03,
STATUS_ALPHA_UNKNOWN = 0x04,
STATUS_ALPHA_FULL = 0x00,
STATUS_ALPHA_MASK = 0x04,
STATUS_CLUT_VARIANTS = 0x08,
STATUS_CHANGE_FREQUENT = 0x10,
STATUS_CLUT_RECHECK = 0x20,
STATUS_TO_SCALE = 0x80,
STATUS_IS_SCALED_OR_REPLACED = 0x100,
STATUS_TO_REPLACE = 0x0200,
STATUS_FREE_CHANGE = 0x0400,
STATUS_NO_MIPS = 0x0800,
STATUS_FRAMEBUFFER_OVERLAP = 0x1000,
STATUS_FORCE_REBUILD = 0x2000,
STATUS_3D = 0x4000,
STATUS_CLUT_GPU = 0x8000,
STATUS_VIDEO = 0x10000,
STATUS_BGRA = 0x20000,
};
u32 status;
u32 addr;
u32 minihash;
u8 format;
u8 maxLevel;
u16 dim;
u16 bufw;
union {
GLRTexture *textureName;
void *texturePtr;
VulkanTexture *vkTex;
};
#ifdef _WIN32
void *textureView;
#endif
int invalidHint;
int lastFrame;
int numFrames;
int numInvalidated;
u32 framesUntilNextFullHash;
u32 fullhash;
u32 cluthash;
u16 maxSeenV;
ReplacedTexture *replacedTexture;
TexStatus GetHashStatus() {
return TexStatus(status & STATUS_MASK);
}
void SetHashStatus(TexStatus newStatus) {
status = (status & ~STATUS_MASK) | newStatus;
}
TexStatus GetAlphaStatus() {
return TexStatus(status & STATUS_ALPHA_MASK);
}
void SetAlphaStatus(TexStatus newStatus) {
status = (status & ~STATUS_ALPHA_MASK) | newStatus;
}
void SetAlphaStatus(TexStatus newStatus, int level) {
if (newStatus == STATUS_ALPHA_UNKNOWN || level == 0) {
SetAlphaStatus(newStatus);
}
}
void SetAlphaStatus(CheckAlphaResult alphaResult, int level) {
TexStatus newStatus = (TexStatus)alphaResult;
if (newStatus == STATUS_ALPHA_UNKNOWN || level == 0) {
SetAlphaStatus(newStatus);
}
}
u32 SizeInRAM() const {
return (textureBitsPerPixel[format] * bufw * dimHeight(dim)) / 8;
}
bool Matches(u16 dim2, u8 format2, u8 maxLevel2) const;
u64 CacheKey() const;
static u64 CacheKey(u32 addr, u8 format, u16 dim, u32 cluthash);
};
typedef std::map<u64, std::unique_ptr<TexCacheEntry>> TexCache;
#ifdef IGNORE
#undef IGNORE
#endif
struct FramebufferMatchInfo {
int16_t xOffset;
int16_t yOffset;
bool reinterpret;
GEBufferFormat reinterpretTo;
};
struct AttachCandidate {
VirtualFramebuffer *fb;
FramebufferMatchInfo match;
RasterChannel channel;
int relevancy;
std::string ToString() const;
};
class FramebufferManagerCommon;
struct BuildTexturePlan {
bool hardwareScaling = false;
bool slowScaler = true;
bool badMipSizes;
int levelsToLoad;
int levelsToCreate;
int maxPossibleLevels;
int baseLevelSrc;
int scaleFactor;
bool isVideo;
int w;
int h;
int createW;
int createH;
int depth;
ReplacedTexture *replaced;
bool doReplace;
bool saveTexture;
bool decodeToClut8;
void GetMipSize(int level, int *w, int *h) const {
if (doReplace) {
replaced->GetSize(level, w, h);
return;
}
if (depth == 1) {
*w = createW >> level;
*h = createH >> level;
} else {
*w = createW;
*h = createH;
}
}
};
class TextureCacheCommon {
public:
TextureCacheCommon(Draw::DrawContext *draw, Draw2D *draw2D);
virtual ~TextureCacheCommon();
void LoadClut(u32 clutAddr, u32 loadBytes, GPURecord::Recorder *recorder);
bool GetCurrentClutBuffer(GPUDebugBuffer &buffer);
TexCacheEntry *SetTexture();
void SetShaderManager(ShaderManagerCommon *sm) {
shaderManager_ = sm;
}
void ApplyTexture(bool doBind = true);
bool SetOffsetTexture(u32 yOffset);
void Invalidate(u32 addr, int size, GPUInvalidationType type);
void InvalidateAll(GPUInvalidationType type);
void ClearNextFrame();
TextureShaderCache *GetTextureShaderCache() { return textureShaderCache_; }
virtual void ForgetLastTexture() = 0;
virtual void Clear(bool delete_them);
virtual void NotifyConfigChanged();
virtual void ApplySamplingParams(const SamplerCacheKey &key) = 0;
void NotifyFramebuffer(VirtualFramebuffer *framebuffer, FramebufferNotification msg);
void NotifyWriteFormattedFromMemory(u32 addr, int size, int width, GEBufferFormat fmt);
size_t NumLoadedTextures() const {
return cache_.size();
}
bool IsFakeMipmapChange() {
return PSP_CoreParameter().compat.flags().FakeMipmapChange && gstate.getTexLevelMode() == GE_TEXLEVEL_MODE_CONST;
}
bool VideoIsPlaying() {
return !videos_.empty();
}
virtual bool GetCurrentTextureDebug(GPUDebugBuffer &buffer, int level, bool *isFramebuffer) { return false; }
virtual void StartFrame();
virtual void DeviceLost() = 0;
virtual void DeviceRestore(Draw::DrawContext *draw) = 0;
virtual void *GetNativeTextureView(const TexCacheEntry *entry, bool flat) const = 0;
const TexCache &Cache() const { return cache_; }
const TexCache &SecondCache() const { return secondCache_; }
const size_t CacheSizeEstimate() const { return cacheSizeEstimate_; }
const size_t SecondCacheSizeEstimate() const { return secondCacheSizeEstimate_; }
struct VideoInfo {
u32 addr;
u32 size;
int flips;
};
const std::vector<VideoInfo> &Videos() const {
return videos_;
}
protected:
bool PrepareBuildTexture(BuildTexturePlan &plan, TexCacheEntry *entry);
virtual void BindTexture(TexCacheEntry *entry) = 0;
virtual void Unbind() = 0;
virtual void ReleaseTexture(TexCacheEntry *entry, bool delete_them) = 0;
void DeleteTexture(TexCache::iterator it);
void Decimate(TexCacheEntry *exceptThisOne, bool forcePressure);
void ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer, GETextureFormat texFormat, RasterChannel channel);
void ApplyTextureDepal(TexCacheEntry *entry);
void HandleTextureChange(TexCacheEntry *const entry, const char *reason, bool initialMatch, bool doDelete);
virtual void BuildTexture(TexCacheEntry *const entry) = 0;
virtual void UpdateCurrentClut(GEPaletteFormat clutFormat, u32 clutBase, bool clutIndexIsSimple) = 0;
bool CheckFullHash(TexCacheEntry *entry, bool &doDelete);
virtual void BindAsClutTexture(Draw::Texture *tex, bool smooth) {}
CheckAlphaResult DecodeTextureLevel(u8 *out, int outPitch, GETextureFormat format, GEPaletteFormat clutformat, uint32_t texaddr, int level, int bufw, TexDecodeFlags flags);
static void UnswizzleFromMem(u32 *dest, u32 destPitch, const u8 *texptr, u32 bufw, u32 height, u32 bytesPerPixel);
CheckAlphaResult ReadIndexedTex(u8 *out, int outPitch, int level, const u8 *texptr, int bytesPerIndex, int bufw, bool reverseColors, bool expandTo32Bit);
ReplacedTexture *FindReplacement(TexCacheEntry *entry, int *w, int *h, int *d);
void PollReplacement(TexCacheEntry *entry, int *w, int *h, int *d);
void LoadTextureLevel(TexCacheEntry &entry, uint8_t *mapData, size_t dataSize, int mapRowPitch, BuildTexturePlan &plan, int srcLevel, Draw::DataFormat dstFmt, TexDecodeFlags texDecFlags);
template <typename T>
inline const T *GetCurrentClut() {
return (const T *)clutBuf_;
}
template <typename T>
inline const T *GetCurrentRawClut() {
return (const T *)clutBufRaw_;
}
static u32 EstimateTexMemoryUsage(const TexCacheEntry *entry);
SamplerCacheKey GetSamplingParams(int maxLevel, const TexCacheEntry *entry);
SamplerCacheKey GetFramebufferSamplingParams(u16 bufferWidth, u16 bufferHeight);
void UpdateMaxSeenV(TexCacheEntry *entry, bool throughMode);
bool MatchFramebuffer(const TextureDefinition &entry, VirtualFramebuffer *framebuffer, u32 texaddrOffset, RasterChannel channel, FramebufferMatchInfo *matchInfo) const;
bool GetBestFramebufferCandidate(const TextureDefinition &entry, u32 texAddrOffset, AttachCandidate *bestCandidate) const;
void SetTextureFramebuffer(const AttachCandidate &candidate);
bool GetCurrentFramebufferTextureDebug(GPUDebugBuffer &buffer, bool *isFramebuffer);
virtual void BoundFramebufferTexture() {}
void DecimateVideos();
bool IsVideo(u32 texaddr) const;
static CheckAlphaResult CheckCLUTAlpha(const uint8_t *pixelData, GEPaletteFormat clutFmt, int w);
static inline u32 QuickTexHash(TextureReplacer &replacer, u32 addr, int bufw, int w, int h, bool swizzled, GETextureFormat format, const TexCacheEntry *entry) {
if (replacer.Enabled()) {
return replacer.ComputeHash(addr, bufw, w, h, swizzled, format, entry->maxSeenV);
}
if (h == 512 && entry->maxSeenV < 512 && entry->maxSeenV != 0) {
h = (int)entry->maxSeenV;
}
u32 sizeInRAM;
if (swizzled) {
sizeInRAM = (textureBitsPerPixel[format] * bufw * ((h + 7) & ~7)) >> 3;
} else {
sizeInRAM = (textureBitsPerPixel[format] * bufw * h) >> 3;
}
const u32 *checkp = (const u32 *)Memory::GetPointer(addr);
gpuStats.numTextureDataBytesHashed += sizeInRAM;
if (Memory::IsValidAddress(addr + sizeInRAM)) {
return StableQuickTexHash(checkp, sizeInRAM);
} else {
return 0;
}
}
static inline u32 MiniHash(const u32 *ptr) {
return ptr[0];
}
Draw::DrawContext *draw_;
Draw2D *draw2D_;
TextureReplacer replacer_;
TextureScalerCommon scaler_;
FramebufferManagerCommon *framebufferManager_;
TextureShaderCache *textureShaderCache_;
ShaderManagerCommon *shaderManager_;
bool clearCacheNextFrame_ = false;
bool lowMemoryMode_ = false;
int decimationCounter_;
int texelsScaledThisFrame_ = 0;
int timesInvalidatedAllThisFrame_ = 0;
double replacementTimeThisFrame_ = 0;
double replacementFrameBudgetSeconds_ = 0.5 / 60.0;
TexCache cache_;
u32 cacheSizeEstimate_ = 0;
TexCache secondCache_;
u32 secondCacheSizeEstimate_ = 0;
std::vector<VideoInfo> videos_;
AlignedVector<u32, 16> tmpTexBuf32_;
AlignedVector<u32, 16> tmpTexBufRearrange_;
TexCacheEntry *nextTexture_ = nullptr;
bool failedTexture_ = false;
VirtualFramebuffer *nextFramebufferTexture_ = nullptr;
RasterChannel nextFramebufferTextureChannel_ = RASTER_COLOR;
u32 clutHash_ = 0;
u32 *clutBufRaw_;
u32 *clutBufConverted_;
u32 *clutBuf_;
u32 clutLastFormat_ = 0xFFFFFFFF;
u32 clutTotalBytes_ = 0;
u32 clutMaxBytes_ = 0;
u32 clutRenderAddress_ = 0xFFFFFFFF;
u32 clutRenderOffset_;
GEBufferFormat clutRenderFormat_;
bool clutAlphaLinear_ = false;
u16 clutAlphaLinearColor_;
Draw::Framebuffer *dynamicClutTemp_ = nullptr;
Draw::Framebuffer *dynamicClutFbo_ = nullptr;
int standardScaleFactor_;
int shaderScaleFactor_ = 0;
const char *nextChangeReason_;
bool nextNeedsRehash_;
bool nextNeedsChange_;
bool nextNeedsRebuild_;
u32 *expandClut_;
};
inline bool TexCacheEntry::Matches(u16 dim2, u8 format2, u8 maxLevel2) const {
return dim == dim2 && format == format2 && maxLevel == maxLevel2;
}
inline u64 TexCacheEntry::CacheKey() const {
return CacheKey(addr, format, dim, cluthash);
}
inline u64 TexCacheEntry::CacheKey(u32 addr, u8 format, u16 dim, u32 cluthash) {
u64 cachekey = ((u64)(addr & 0x3FFFFFFF) << 32) | dim;
bool hasClut = (format & 4) != 0;
if (hasClut) {
cachekey ^= cluthash;
}
return cachekey;
}