#include "ppsspp_config.h"
#include <algorithm>
#include "Common/Common.h"
#include "Common/Data/Convert/ColorConv.h"
#include "Common/Data/Collections/TinySet.h"
#include "Common/Profiler/Profiler.h"
#include "Common/LogReporting.h"
#include "Common/MemoryUtil.h"
#include "Common/StringUtils.h"
#include "Common/Math/SIMDHeaders.h"
#include "Common/TimeUtil.h"
#include "Common/Math/math_util.h"
#include "Common/GPU/thin3d.h"
#include "Core/HDRemaster.h"
#include "Core/Config.h"
#include "Core/Debugger/MemBlockInfo.h"
#include "Core/System.h"
#include "Core/HW/Display.h"
#include "GPU/Common/FramebufferManagerCommon.h"
#include "GPU/Common/TextureCacheCommon.h"
#include "GPU/Common/TextureDecoder.h"
#include "GPU/Common/GPUStateUtils.h"
#include "GPU/ge_constants.h"
#include "GPU/Debugger/Record.h"
#include "GPU/GPUState.h"
#include "Core/Util/PPGeDraw.h"
#define VIDEO_DECIMATE_AGE 4
#define TEXTURE_KILL_AGE 200
#define TEXTURE_KILL_AGE_LOWMEM 60
#define TEXTURE_SECOND_KILL_AGE 100
#define TEXTURE_KILL_AGE_CLUT 6
#define TEXTURE_CLUT_VARIANTS_MIN 6
#define TEXCACHE_DECIMATION_INTERVAL 13
#define TEXCACHE_MIN_PRESSURE 16 * 1024 * 1024
#define TEXCACHE_SECOND_MIN_PRESSURE 4 * 1024 * 1024
TextureCacheCommon::TextureCacheCommon(Draw::DrawContext *draw, Draw2D *draw2D)
: draw_(draw), draw2D_(draw2D), replacer_(draw) {
decimationCounter_ = TEXCACHE_DECIMATION_INTERVAL;
clutBufRaw_ = (u32 *)AllocateAlignedMemory(2048, 16);
clutBufConverted_ = (u32 *)AllocateAlignedMemory(2048, 16);
expandClut_ = (u32 *)AllocateAlignedMemory(2048, 16);
_assert_(clutBufRaw_ && clutBufConverted_ && expandClut_);
memset(clutBufRaw_, 0, 2048);
memset(clutBufConverted_, 0, 2048);
clutBuf_ = clutBufConverted_;
tmpTexBuf32_.resize(512 * 512);
tmpTexBufRearrange_.resize(512 * 512);
textureShaderCache_ = new TextureShaderCache(draw, draw2D_);
}
TextureCacheCommon::~TextureCacheCommon() {
delete textureShaderCache_;
FreeAlignedMemory(clutBufConverted_);
FreeAlignedMemory(clutBufRaw_);
FreeAlignedMemory(expandClut_);
}
void TextureCacheCommon::StartFrame() {
ForgetLastTexture();
textureShaderCache_->Decimate();
timesInvalidatedAllThisFrame_ = 0;
replacementTimeThisFrame_ = 0.0;
float fps;
__DisplayGetFPS(nullptr, &fps, nullptr);
if (fps <= 5.0f) {
fps = 60.0f;
}
float baseValue = 0.5f;
switch (g_Config.iReplacementTextureLoadSpeed) {
case (int)ReplacementTextureLoadSpeed::SLOW:
baseValue = 0.5f;
break;
case (int)ReplacementTextureLoadSpeed::MEDIUM:
baseValue = 0.75f;
break;
case (int)ReplacementTextureLoadSpeed::FAST:
baseValue = 1.0f;
break;
case (int)ReplacementTextureLoadSpeed::INSTANT:
baseValue = 100000.0f;
break;
}
replacementFrameBudgetSeconds_ = baseValue / fps;
if ((DebugOverlay)g_Config.iDebugOverlay == DebugOverlay::DEBUG_STATS) {
gpuStats.numReplacerTrackedTex = replacer_.GetNumTrackedTextures();
gpuStats.numCachedReplacedTextures = replacer_.GetNumCachedReplacedTextures();
}
if (texelsScaledThisFrame_) {
VERBOSE_LOG(Log::G3D, "Scaled %d texels", texelsScaledThisFrame_);
}
texelsScaledThisFrame_ = 0;
if (clearCacheNextFrame_) {
Clear(true);
clearCacheNextFrame_ = false;
} else {
Decimate(nullptr, false);
}
}
static int TexLog2(float delta) {
union FloatBits {
float f;
u32 u;
};
FloatBits f;
f.f = delta;
int useful = (f.u >> 15) & 0xFFFF;
return useful - 127 * 256;
}
SamplerCacheKey TextureCacheCommon::GetSamplingParams(int maxLevel, const TexCacheEntry *entry) {
SamplerCacheKey key{};
int minFilt = gstate.texfilter & 0x7;
key.minFilt = minFilt & 1;
key.mipEnable = (minFilt >> 2) & 1;
key.mipFilt = (minFilt >> 1) & 1;
key.magFilt = gstate.isMagnifyFilteringEnabled();
key.sClamp = gstate.isTexCoordClampedS();
key.tClamp = gstate.isTexCoordClampedT();
key.aniso = false;
key.texture3d = gstate_c.curTextureIs3D;
GETexLevelMode mipMode = gstate.getTexLevelMode();
bool autoMip = mipMode == GE_TEXLEVEL_MODE_AUTO;
float lodBias = (float)gstate.getTexLevelOffset16() * (1.0f / 16.0f);
if (mipMode == GE_TEXLEVEL_MODE_SLOPE) {
lodBias += 1.0f + TexLog2(gstate.getTextureLodSlope()) * (1.0f / 256.0f);
}
bool noMip = maxLevel == 0 || (!autoMip && lodBias <= 0.0f);
if (noMip) {
key.mipEnable = false;
key.mipFilt = 0;
lodBias = 0.0f;
}
if (!key.mipEnable) {
key.maxLevel = 0;
key.minLevel = 0;
key.lodBias = 0;
key.mipFilt = 0;
} else {
switch (mipMode) {
case GE_TEXLEVEL_MODE_AUTO:
key.maxLevel = maxLevel * 256;
key.minLevel = 0;
key.lodBias = (int)(lodBias * 256.0f);
if (gstate_c.Use(GPU_USE_ANISOTROPY) && g_Config.iAnisotropyLevel > 0) {
key.aniso = true;
}
break;
case GE_TEXLEVEL_MODE_CONST:
case GE_TEXLEVEL_MODE_UNKNOWN:
key.maxLevel = (int)(lodBias * 256.0f);
key.minLevel = (int)(lodBias * 256.0f);
key.lodBias = 0;
break;
case GE_TEXLEVEL_MODE_SLOPE:
key.maxLevel = maxLevel * 256;
key.minLevel = 0;
key.lodBias = 0;
break;
}
}
if (!key.magFilt && entry != nullptr && IsVideo(entry->addr)) {
key.magFilt = 1;
}
TextureFiltering forceFiltering = TEX_FILTER_AUTO;
bool useReplacerFiltering = false;
if (entry && replacer_.Enabled() && entry->replacedTexture) {
if (entry->replacedTexture->State() == ReplacementState::ACTIVE && entry->replacedTexture->NumLevels() > 1) {
key.mipEnable = true;
key.mipFilt = 1;
key.maxLevel = 9 * 256;
if (gstate_c.Use(GPU_USE_ANISOTROPY) && g_Config.iAnisotropyLevel > 0) {
key.aniso = true;
}
}
useReplacerFiltering = entry->replacedTexture->ForceFiltering(&forceFiltering);
}
if (!useReplacerFiltering) {
switch (g_Config.iTexFiltering) {
case TEX_FILTER_AUTO:
if (gstate.isModeThrough() && g_Config.iInternalResolution != 1) {
bool uglyColorTest = gstate.isColorTestEnabled() && !IsColorTestTriviallyTrue() && gstate.getColorTestRef() != 0;
if (uglyColorTest)
forceFiltering = TEX_FILTER_FORCE_NEAREST;
}
if (gstate_c.pixelMapped) {
forceFiltering = TEX_FILTER_FORCE_NEAREST;
}
break;
case TEX_FILTER_FORCE_LINEAR:
if ((!gstate.isColorTestEnabled() || IsColorTestTriviallyTrue()) &&
(!gstate.isAlphaTestEnabled() || IsAlphaTestTriviallyTrue())) {
forceFiltering = TEX_FILTER_FORCE_LINEAR;
}
break;
case TEX_FILTER_FORCE_NEAREST:
forceFiltering = TEX_FILTER_FORCE_NEAREST;
break;
case TEX_FILTER_AUTO_MAX_QUALITY:
default:
forceFiltering = TEX_FILTER_AUTO_MAX_QUALITY;
if (gstate_c.Use(GPU_USE_ANISOTROPY) && g_Config.iAnisotropyLevel > 0) {
key.aniso = true;
}
if (gstate.isModeThrough() && g_Config.iInternalResolution != 1) {
bool uglyColorTest = gstate.isColorTestEnabled() && !IsColorTestTriviallyTrue() && gstate.getColorTestRef() != 0;
if (uglyColorTest) {
forceFiltering = TEX_FILTER_FORCE_NEAREST;
key.aniso = false;
}
}
if (gstate_c.pixelMapped) {
forceFiltering = TEX_FILTER_FORCE_NEAREST;
key.aniso = false;
}
break;
}
}
switch (forceFiltering) {
case TEX_FILTER_AUTO:
break;
case TEX_FILTER_FORCE_LINEAR:
key.magFilt = 1;
key.minFilt = 1;
key.mipFilt = 1;
break;
case TEX_FILTER_FORCE_NEAREST:
key.magFilt = 0;
key.minFilt = 0;
break;
case TEX_FILTER_AUTO_MAX_QUALITY:
key.minFilt = 1;
key.mipFilt = 1;
key.maxLevel = 9 * 256;
key.lodBias = 0.0f;
break;
}
return key;
}
SamplerCacheKey TextureCacheCommon::GetFramebufferSamplingParams(u16 bufferWidth, u16 bufferHeight) {
SamplerCacheKey key = GetSamplingParams(0, nullptr);
if (g_Config.iTexFiltering == TEX_FILTER_AUTO_MAX_QUALITY) {
int minFilt = gstate.texfilter & 0x7;
key.minFilt = minFilt & 1;
}
key.mipEnable = false;
key.mipFilt = false;
key.aniso = 0.0f;
key.maxLevel = 0.0f;
key.lodBias = 0.0f;
int w = gstate.getTextureWidth(0);
int h = gstate.getTextureHeight(0);
if (w != bufferWidth || h != bufferHeight) {
key.sClamp = true;
key.tClamp = true;
}
return key;
}
void TextureCacheCommon::UpdateMaxSeenV(TexCacheEntry *entry, bool throughMode) {
if (entry->dim >= 0x900) {
if (entry->cluthash != 0 && entry->maxSeenV == 0) {
const u64 cachekeyMin = (u64)(entry->addr & 0x3FFFFFFF) << 32;
const u64 cachekeyMax = cachekeyMin + (1ULL << 32);
for (auto it = cache_.lower_bound(cachekeyMin), end = cache_.upper_bound(cachekeyMax); it != end; ++it) {
if (it->second->maxSeenV != 0) {
entry->maxSeenV = it->second->maxSeenV;
break;
}
}
}
if (throughMode) {
if (entry->maxSeenV == 0 && gstate_c.vertBounds.maxV > 0) {
entry->maxSeenV = std::max((u16)272, gstate_c.vertBounds.maxV);
} else if (gstate_c.vertBounds.maxV > entry->maxSeenV) {
entry->maxSeenV = 512;
entry->status |= TexCacheEntry::STATUS_FREE_CHANGE;
}
} else {
entry->maxSeenV = 512;
}
if (entry->cluthash != 0) {
const u64 cachekeyMin = (u64)(entry->addr & 0x3FFFFFFF) << 32;
const u64 cachekeyMax = cachekeyMin + (1ULL << 32);
for (auto it = cache_.lower_bound(cachekeyMin), end = cache_.upper_bound(cachekeyMax); it != end; ++it) {
it->second->maxSeenV = entry->maxSeenV;
}
}
}
}
TexCacheEntry *TextureCacheCommon::SetTexture() {
u8 level = 0;
if (IsFakeMipmapChange()) {
level = std::max(0, gstate.getTexLevelOffset16() / 16);
}
u32 texaddr = gstate.getTextureAddress(level);
if (!Memory::IsValidAddress(texaddr)) {
Unbind();
gstate_c.SetTextureIsVideo(false);
gstate_c.SetTextureIs3D(false);
gstate_c.SetTextureIsArray(false);
gstate_c.SetTextureIsFramebuffer(false);
return nullptr;
}
const u16 dim = gstate.getTextureDimension(level);
int w = gstate.getTextureWidth(level);
int h = gstate.getTextureHeight(level);
GETextureFormat texFormat = gstate.getTextureFormat();
if (texFormat >= 11) {
texFormat = GE_TFMT_5650;
}
bool hasClut = gstate.isTextureFormatIndexed();
bool hasClutGPU = false;
u32 cluthash;
if (hasClut) {
if (clutRenderAddress_ != 0xFFFFFFFF) {
gstate_c.curTextureXOffset = 0.0f;
gstate_c.curTextureYOffset = 0.0f;
hasClutGPU = true;
cluthash = 0;
} else {
if (clutLastFormat_ != gstate.clutformat) {
UpdateCurrentClut(gstate.getClutPaletteFormat(), gstate.getClutIndexStartPos(), gstate.isClutIndexSimple());
}
cluthash = clutHash_ ^ gstate.clutformat;
}
} else {
cluthash = 0;
}
u64 cachekey = TexCacheEntry::CacheKey(texaddr, texFormat, dim, cluthash);
int bufw = GetTextureBufw(0, texaddr, texFormat);
u8 maxLevel = gstate.getTextureMaxLevel();
u32 minihash = MiniHash((const u32 *)Memory::GetPointerUnchecked(texaddr));
TexCache::iterator entryIter = cache_.find(cachekey);
TexCacheEntry *entry = nullptr;
gstate_c.SetNeedShaderTexclamp(false);
gstate_c.skipDrawReason &= ~SKIPDRAW_BAD_FB_TEXTURE;
if (entryIter != cache_.end()) {
entry = entryIter->second.get();
bool match = entry->Matches(dim, texFormat, maxLevel);
const char *reason = "different params";
if (((entry->status & TexCacheEntry::STATUS_CLUT_GPU) != 0) != hasClutGPU) {
match = false;
}
if (entry->status & TexCacheEntry::STATUS_FRAMEBUFFER_OVERLAP) {
entry->status &= ~TexCacheEntry::STATUS_FRAMEBUFFER_OVERLAP;
match = false;
}
bool rehash = entry->GetHashStatus() == TexCacheEntry::STATUS_UNRELIABLE;
if (entry->status & TexCacheEntry::STATUS_CLUT_RECHECK) {
rehash = true;
entry->status &= ~TexCacheEntry::STATUS_CLUT_RECHECK;
} else if (!gstate_c.IsDirty(DIRTY_TEXTURE_IMAGE)) {
rehash = false;
}
if (entry->status & TexCacheEntry::STATUS_FORCE_REBUILD) {
match = false;
entry->status &= ~TexCacheEntry::STATUS_FORCE_REBUILD;
}
if (match) {
if (entry->lastFrame != gpuStats.numFlips) {
u32 diff = gpuStats.numFlips - entry->lastFrame;
entry->numFrames++;
if (entry->framesUntilNextFullHash < diff) {
if (entry->numFrames > 32) {
entry->framesUntilNextFullHash = std::min(512, entry->numFrames) + (((intptr_t)(entry->textureName) >> 12) & 15);
} else {
entry->framesUntilNextFullHash = entry->numFrames;
}
rehash = true;
} else {
entry->framesUntilNextFullHash -= diff;
}
}
if (entry->invalidHint > 180 || (entry->invalidHint > 15 && (dim >> 8) < 9 && (dim & 0xF) < 9)) {
entry->invalidHint = 0;
rehash = true;
}
if (minihash != entry->minihash) {
match = false;
reason = "minihash";
} else if (entry->GetHashStatus() == TexCacheEntry::STATUS_RELIABLE) {
rehash = false;
}
}
if (match && (entry->status & TexCacheEntry::STATUS_TO_SCALE) && standardScaleFactor_ != 1 && texelsScaledThisFrame_ < TEXCACHE_MAX_TEXELS_SCALED) {
if ((entry->status & TexCacheEntry::STATUS_CHANGE_FREQUENT) == 0) {
match = false;
reason = "scaling";
}
}
if (match && (entry->status & TexCacheEntry::STATUS_TO_REPLACE) && replacementTimeThisFrame_ < replacementFrameBudgetSeconds_) {
int w0 = gstate.getTextureWidth(0);
int h0 = gstate.getTextureHeight(0);
int d0 = 1;
if (entry->replacedTexture) {
PollReplacement(entry, &w0, &h0, &d0);
switch (entry->replacedTexture->State()) {
case ReplacementState::NOT_FOUND:
entry->status &= ~TexCacheEntry::STATUS_TO_REPLACE;
if (g_Config.bSaveNewTextures) {
match = false;
reason = "replacing";
}
break;
case ReplacementState::ACTIVE:
match = false;
reason = "replacing";
break;
default:
break;
}
}
}
if (match) {
gstate_c.curTextureWidth = w;
gstate_c.curTextureHeight = h;
gstate_c.SetTextureIsVideo(false);
gstate_c.SetTextureIs3D((entry->status & TexCacheEntry::STATUS_3D) != 0);
gstate_c.SetTextureIsArray(false);
gstate_c.SetTextureIsBGRA((entry->status & TexCacheEntry::STATUS_BGRA) != 0);
gstate_c.SetTextureIsFramebuffer(false);
if (rehash) {
entry->bufw = bufw;
entry->cluthash = cluthash;
}
nextTexture_ = entry;
nextNeedsRehash_ = rehash;
nextNeedsChange_ = false;
nextNeedsRebuild_ = false;
failedTexture_ = false;
VERBOSE_LOG(Log::G3D, "Texture at %08x found in cache, applying", texaddr);
return entry;
} else {
nextChangeReason_ = reason;
nextNeedsChange_ = true;
}
}
TextureDefinition def{};
def.addr = texaddr;
def.dim = dim;
def.format = texFormat;
def.bufw = bufw;
AttachCandidate bestCandidate;
if (GetBestFramebufferCandidate(def, 0, &bestCandidate)) {
if (entryIter != cache_.end()) {
DeleteTexture(entryIter);
}
nextTexture_ = nullptr;
nextNeedsRebuild_ = false;
SetTextureFramebuffer(bestCandidate);
return nullptr;
}
if (!entry) {
VERBOSE_LOG(Log::G3D, "No texture in cache for %08x, decoding...", texaddr);
entry = new TexCacheEntry{};
cache_[cachekey].reset(entry);
if (PPGeIsFontTextureAddress(texaddr)) {
entry->status = TexCacheEntry::STATUS_RELIABLE;
} else if (g_Config.bTextureBackoffCache && !IsVideo(texaddr)) {
entry->status = TexCacheEntry::STATUS_HASHING;
} else {
entry->status = TexCacheEntry::STATUS_UNRELIABLE;
}
if (hasClutGPU) {
WARN_LOG_N_TIMES(clutUseRender, 5, Log::G3D, "Using texture with dynamic CLUT: texfmt=%d, clutfmt=%d", gstate.getTextureFormat(), gstate.getClutPaletteFormat());
entry->status |= TexCacheEntry::STATUS_CLUT_GPU;
}
if (hasClut && clutRenderAddress_ == 0xFFFFFFFF) {
const u64 cachekeyMin = (u64)(texaddr & 0x3FFFFFFF) << 32;
const u64 cachekeyMax = cachekeyMin + (1ULL << 32);
int found = 0;
for (auto it = cache_.lower_bound(cachekeyMin), end = cache_.upper_bound(cachekeyMax); it != end; ++it) {
found++;
}
if (found >= TEXTURE_CLUT_VARIANTS_MIN) {
for (auto it = cache_.lower_bound(cachekeyMin), end = cache_.upper_bound(cachekeyMax); it != end; ++it) {
it->second->status |= TexCacheEntry::STATUS_CLUT_VARIANTS;
}
entry->status |= TexCacheEntry::STATUS_CLUT_VARIANTS;
}
}
nextNeedsChange_ = false;
}
entry->addr = texaddr;
entry->minihash = minihash;
entry->dim = dim;
entry->format = texFormat;
entry->maxLevel = maxLevel;
entry->status &= ~TexCacheEntry::STATUS_BGRA;
entry->bufw = bufw;
entry->cluthash = cluthash;
gstate_c.curTextureWidth = w;
gstate_c.curTextureHeight = h;
gstate_c.SetTextureIsVideo(false);
gstate_c.SetTextureIs3D((entry->status & TexCacheEntry::STATUS_3D) != 0);
gstate_c.SetTextureIsArray(false);
gstate_c.SetTextureIsFramebuffer(false);
failedTexture_ = false;
nextTexture_ = entry;
nextFramebufferTexture_ = nullptr;
nextNeedsRehash_ = true;
nextNeedsRebuild_ = true;
return entry;
}
bool TextureCacheCommon::GetBestFramebufferCandidate(const TextureDefinition &entry, u32 texAddrOffset, AttachCandidate *bestCandidate) const {
gpuStats.numFramebufferEvaluations++;
TinySet<AttachCandidate, 6> candidates;
const std::vector<VirtualFramebuffer *> &framebuffers = framebufferManager_->Framebuffers();
for (VirtualFramebuffer *framebuffer : framebuffers) {
FramebufferMatchInfo match{};
if (MatchFramebuffer(entry, framebuffer, texAddrOffset, RASTER_COLOR, &match)) {
candidates.push_back(AttachCandidate{ framebuffer, match, RASTER_COLOR });
}
match = {};
if (MatchFramebuffer(entry, framebuffer, texAddrOffset, RASTER_DEPTH, &match)) {
candidates.push_back(AttachCandidate{ framebuffer, match, RASTER_DEPTH });
}
}
if (candidates.size() == 0) {
return false;
} else if (candidates.size() == 1) {
*bestCandidate = candidates[0];
return true;
}
bool logging = Reporting::ShouldLogNTimes("multifbcandidate", 5);
int bestRelevancy = -1;
size_t bestIndex = -1;
bool kzCompat = PSP_CoreParameter().compat.flags().SplitFramebufferMargin;
for (size_t i = 0; i < candidates.size(); i++) {
AttachCandidate &candidate = candidates[i];
int relevancy = candidate.channel == RASTER_COLOR ? candidate.fb->colorBindSeq : candidate.fb->depthBindSeq;
if (candidate.channel == RASTER_COLOR &&
(candidate.match.yOffset != 0 || candidate.match.xOffset != 0) &&
candidate.fb->fb_address == (gstate.getFrameBufRawAddress() | 0x04000000)) {
relevancy -= 2;
}
if (candidate.match.xOffset != 0 && PSP_CoreParameter().compat.flags().DisallowFramebufferAtOffset) {
continue;
}
if (kzCompat && candidate.fb == framebufferManager_->GetCurrentRenderVFB()) {
continue;
}
if (logging) {
candidate.relevancy = relevancy;
}
if (relevancy > bestRelevancy) {
bestRelevancy = relevancy;
bestIndex = i;
}
}
if (logging) {
std::string cands;
for (size_t i = 0; i < candidates.size(); i++) {
cands += candidates[i].ToString();
if (i != candidates.size() - 1)
cands += "\n";
}
cands += "\n";
WARN_LOG(Log::G3D, "GetFramebufferCandidates(tex): Multiple (%d) candidate framebuffers. texaddr: %08x offset: %d (%dx%d stride %d, %s):\n%s",
(int)candidates.size(),
entry.addr, texAddrOffset, dimWidth(entry.dim), dimHeight(entry.dim), entry.bufw, GeTextureFormatToString(entry.format),
cands.c_str()
);
logging = true;
}
if (bestIndex != -1) {
if (logging) {
WARN_LOG(Log::G3D, "Chose candidate %d:\n%s\n", (int)bestIndex, candidates[bestIndex].ToString().c_str());
}
*bestCandidate = candidates[bestIndex];
return true;
} else {
return false;
}
}
void TextureCacheCommon::Decimate(TexCacheEntry *exceptThisOne, bool forcePressure) {
if (--decimationCounter_ <= 0) {
decimationCounter_ = TEXCACHE_DECIMATION_INTERVAL;
} else {
return;
}
if (forcePressure || cacheSizeEstimate_ >= TEXCACHE_MIN_PRESSURE) {
const u32 had = cacheSizeEstimate_;
ForgetLastTexture();
int killAgeBase = lowMemoryMode_ ? TEXTURE_KILL_AGE_LOWMEM : TEXTURE_KILL_AGE;
for (TexCache::iterator iter = cache_.begin(); iter != cache_.end(); ) {
if (iter->second.get() == exceptThisOne) {
++iter;
continue;
}
bool hasClut = (iter->second->status & TexCacheEntry::STATUS_CLUT_VARIANTS) != 0;
int killAge = hasClut ? TEXTURE_KILL_AGE_CLUT : killAgeBase;
if (iter->second->lastFrame + killAge < gpuStats.numFlips) {
DeleteTexture(iter++);
} else {
++iter;
}
}
VERBOSE_LOG(Log::G3D, "Decimated texture cache, saved %d estimated bytes - now %d bytes", had - cacheSizeEstimate_, cacheSizeEstimate_);
}
if (PSP_CoreParameter().compat.flags().SecondaryTextureCache && (forcePressure || secondCacheSizeEstimate_ >= TEXCACHE_SECOND_MIN_PRESSURE)) {
const u32 had = secondCacheSizeEstimate_;
for (TexCache::iterator iter = secondCache_.begin(); iter != secondCache_.end(); ) {
if (iter->second.get() == exceptThisOne) {
++iter;
continue;
}
if (lowMemoryMode_ || iter->second->lastFrame + TEXTURE_SECOND_KILL_AGE < gpuStats.numFlips) {
ReleaseTexture(iter->second.get(), true);
secondCacheSizeEstimate_ -= EstimateTexMemoryUsage(iter->second.get());
iter = secondCache_.erase(iter);
} else {
++iter;
}
}
VERBOSE_LOG(Log::G3D, "Decimated second texture cache, saved %d estimated bytes - now %d bytes", had - secondCacheSizeEstimate_, secondCacheSizeEstimate_);
}
DecimateVideos();
replacer_.Decimate(forcePressure ? ReplacerDecimateMode::FORCE_PRESSURE : ReplacerDecimateMode::NEW_FRAME);
}
void TextureCacheCommon::DecimateVideos() {
for (auto iter = videos_.begin(); iter != videos_.end(); ) {
if (iter->flips + VIDEO_DECIMATE_AGE < gpuStats.numFlips) {
iter = videos_.erase(iter);
} else {
++iter;
}
}
}
bool TextureCacheCommon::IsVideo(u32 texaddr) const {
texaddr &= 0x3FFFFFFF;
for (auto &info : videos_) {
if (texaddr < info.addr) {
continue;
}
if (texaddr < info.addr + info.size) {
return true;
}
}
return false;
}
void TextureCacheCommon::HandleTextureChange(TexCacheEntry *const entry, const char *reason, bool initialMatch, bool doDelete) {
cacheSizeEstimate_ -= EstimateTexMemoryUsage(entry);
entry->numInvalidated++;
gpuStats.numTextureInvalidations++;
DEBUG_LOG(Log::G3D, "Texture different or overwritten, reloading at %08x: %s", entry->addr, reason);
if (doDelete) {
ForgetLastTexture();
ReleaseTexture(entry, true);
entry->status &= ~(TexCacheEntry::STATUS_IS_SCALED_OR_REPLACED | TexCacheEntry::STATUS_TO_REPLACE);
}
if (entry->GetHashStatus() == TexCacheEntry::STATUS_RELIABLE) {
entry->SetHashStatus(TexCacheEntry::STATUS_HASHING);
}
if (entry->cluthash != 0) {
const u64 cachekeyMin = (u64)(entry->addr & 0x3FFFFFFF) << 32;
const u64 cachekeyMax = cachekeyMin + (1ULL << 32);
for (auto it = cache_.lower_bound(cachekeyMin), end = cache_.upper_bound(cachekeyMax); it != end; ++it) {
if (it->second->cluthash != entry->cluthash) {
it->second->status |= TexCacheEntry::STATUS_CLUT_RECHECK;
}
}
}
if (entry->numFrames < TEXCACHE_FRAME_CHANGE_FREQUENT) {
if (entry->status & TexCacheEntry::STATUS_FREE_CHANGE) {
entry->status &= ~TexCacheEntry::STATUS_FREE_CHANGE;
} else {
entry->status |= TexCacheEntry::STATUS_CHANGE_FREQUENT;
}
}
entry->numFrames = 0;
}
void TextureCacheCommon::NotifyFramebuffer(VirtualFramebuffer *framebuffer, FramebufferNotification msg) {
const u32 fb_addr = framebuffer->fb_address;
const u32 z_addr = framebuffer->z_address;
const u32 fb_bpp = BufferFormatBytesPerPixel(framebuffer->fb_format);
const u32 z_bpp = 2;
const u32 fb_stride = framebuffer->fb_stride;
const u32 z_stride = framebuffer->z_stride;
const u32 fb_endAddr = fb_addr + fb_stride * std::min((int)framebuffer->height, 16) * fb_bpp;
const u32 z_endAddr = z_addr + z_stride * std::min((int)framebuffer->height, 16) * z_bpp;
switch (msg) {
case NOTIFY_FB_CREATED:
case NOTIFY_FB_UPDATED:
{
u64 cacheKey = (u64)fb_addr << 32;
u64 cacheKeyEnd = (u64)fb_endAddr << 32;
for (auto it = cache_.lower_bound(cacheKey), end = cache_.upper_bound(cacheKeyEnd); it != end; ++it) {
it->second->status |= TexCacheEntry::STATUS_FRAMEBUFFER_OVERLAP;
gpuStats.numTextureInvalidationsByFramebuffer++;
}
if (z_stride != 0) {
cacheKey = (u64)z_addr << 32;
cacheKeyEnd = (u64)z_endAddr << 32;
for (auto it = cache_.lower_bound(cacheKey | 0x200000), end = cache_.upper_bound(cacheKeyEnd | 0x200000); it != end; ++it) {
it->second->status |= TexCacheEntry::STATUS_FRAMEBUFFER_OVERLAP;
gpuStats.numTextureInvalidationsByFramebuffer++;
}
for (auto it = cache_.lower_bound(cacheKey | 0x600000), end = cache_.upper_bound(cacheKeyEnd | 0x600000); it != end; ++it) {
it->second->status |= TexCacheEntry::STATUS_FRAMEBUFFER_OVERLAP;
gpuStats.numTextureInvalidationsByFramebuffer++;
}
}
break;
}
default:
break;
}
}
bool TextureCacheCommon::MatchFramebuffer(
const TextureDefinition &entry,
VirtualFramebuffer *framebuffer, u32 texaddrOffset, RasterChannel channel, FramebufferMatchInfo *matchInfo) const {
static const u32 MAX_SUBAREA_Y_OFFSET_SAFE = 32;
uint32_t fb_address = channel == RASTER_DEPTH ? framebuffer->z_address : framebuffer->fb_address;
uint32_t fb_stride = channel == RASTER_DEPTH ? framebuffer->z_stride : framebuffer->fb_stride;
GEBufferFormat fb_format = channel == RASTER_DEPTH ? GE_FORMAT_DEPTH16 : framebuffer->fb_format;
if (channel == RASTER_DEPTH && (framebuffer->z_address == framebuffer->fb_address || framebuffer->z_address == 0)) {
return false;
}
if (!fb_stride) {
return false;
}
switch (entry.format) {
case GE_TFMT_DXT1:
case GE_TFMT_DXT3:
case GE_TFMT_DXT5:
return false;
default: break;
}
uint32_t fb_stride_in_bytes = fb_stride * BufferFormatBytesPerPixel(fb_format);
uint32_t tex_stride_in_bytes = entry.bufw * textureBitsPerPixel[entry.format] / 8;
u32 addr = fb_address;
u32 texaddr = entry.addr + texaddrOffset;
bool texInVRAM = Memory::IsVRAMAddress(texaddr);
bool fbInVRAM = Memory::IsVRAMAddress(fb_address);
if (texInVRAM != fbInVRAM) {
return false;
}
if (texInVRAM) {
const u32 mirrorMask = 0x041FFFFF;
addr &= mirrorMask;
texaddr &= mirrorMask;
}
const bool noOffset = texaddr == addr;
const bool exactMatch = noOffset && entry.format < 4 && channel == RASTER_COLOR && fb_stride_in_bytes == tex_stride_in_bytes;
const u32 texWidth = 1 << ((entry.dim >> 0) & 0xf);
const u32 texHeight = 1 << ((entry.dim >> 8) & 0xf);
const u32 minSubareaHeight = texHeight / 4;
if (exactMatch) {
if (IsTextureFormatBufferCompatible(entry.format)) {
if (TextureFormatMatchesBufferFormat(entry.format, fb_format) || (framebuffer->usageFlags & FB_USAGE_BLUE_TO_ALPHA)) {
return true;
} else {
WARN_LOG_ONCE(diffFormat1, Log::G3D, "Found matching framebuffer with reinterpretable fb_format: %s != %s at %08x", GeTextureFormatToString(entry.format), GeBufferFormatToString(fb_format), fb_address);
*matchInfo = FramebufferMatchInfo{ 0, 0, true, TextureFormatToBufferFormat(entry.format) };
return true;
}
} else {
return false;
}
} else {
if (!framebufferManager_->UseBufferedRendering()) {
return false;
}
const bool matchingClutFormat =
(fb_format == GE_FORMAT_DEPTH16 && entry.format == GE_TFMT_CLUT16) ||
(fb_format == GE_FORMAT_DEPTH16 && entry.format == GE_TFMT_5650) ||
(fb_format == GE_FORMAT_8888 && entry.format == GE_TFMT_CLUT32) ||
(fb_format != GE_FORMAT_8888 && entry.format == GE_TFMT_CLUT16) ||
(fb_format == GE_FORMAT_8888 && entry.format == GE_TFMT_CLUT8) ||
(fb_format == GE_FORMAT_5551 && entry.format == GE_TFMT_CLUT8 && PSP_CoreParameter().compat.flags().SOCOMClut8Replacement);
const int texBitsPerPixel = TextureFormatBitsPerPixel(entry.format);
const int byteOffset = texaddr - addr;
if (byteOffset > 0) {
int texbpp = texBitsPerPixel;
if (fb_format == GE_FORMAT_5551 && entry.format == GE_TFMT_CLUT8) {
texbpp = 16;
}
matchInfo->yOffset = byteOffset / fb_stride_in_bytes;
matchInfo->xOffset = 8 * (byteOffset % fb_stride_in_bytes) / texbpp;
} else if (byteOffset < 0) {
int texelOffset = 8 * byteOffset / texBitsPerPixel;
if (texelOffset < -(int)entry.bufw || !PSP_CoreParameter().compat.flags().SplitFramebufferMargin) {
return false;
}
matchInfo->xOffset = entry.bufw == 0 ? 0 : -(-texelOffset % (int)entry.bufw);
}
if (matchInfo->yOffset > 0 && matchInfo->yOffset + minSubareaHeight >= framebuffer->height) {
return false;
}
const int xOffsetInBytes = matchInfo->xOffset * 8 / texBitsPerPixel;
const int texWidthInBytes = texWidth * 8 / texBitsPerPixel;
if (xOffsetInBytes >= framebuffer->BufferWidthInBytes() && xOffsetInBytes + texWidthInBytes <= (int)fb_stride_in_bytes) {
return false;
}
if (matchInfo->yOffset > MAX_SUBAREA_Y_OFFSET_SAFE && addr > 0x04110000 && !PSP_CoreParameter().compat.flags().AllowLargeFBTextureOffsets) {
WARN_LOG_ONCE(subareaIgnored, Log::G3D, "Ignoring possible texturing from framebuffer at %08x +%dx%d / %dx%d", fb_address, matchInfo->xOffset, matchInfo->yOffset, framebuffer->width, framebuffer->height);
return false;
}
if (fb_stride_in_bytes != tex_stride_in_bytes && texHeight > 1) {
return false;
}
if (matchingClutFormat) {
if (!noOffset) {
WARN_LOG_ONCE(subareaClut, Log::G3D, "Matching framebuffer (%s) using %s with offset at %08x +%dx%d", RasterChannelToString(channel), GeTextureFormatToString(entry.format), fb_address, matchInfo->xOffset, matchInfo->yOffset);
}
return true;
} else if (IsClutFormat((GETextureFormat)(entry.format)) || IsDXTFormat((GETextureFormat)(entry.format))) {
WARN_LOG_ONCE(fourEightBit, Log::G3D, "%s texture format not matching framebuffer of format %s at %08x/%d", GeTextureFormatToString(entry.format), GeBufferFormatToString(fb_format), fb_address, fb_stride);
return false;
}
if ((int)fb_format == (int)entry.format || matchingClutFormat) {
if ((int)fb_format != (int)entry.format) {
WARN_LOG_ONCE(diffFormat2, Log::G3D, "Matching framebuffer with different formats %s != %s at %08x",
GeTextureFormatToString(entry.format), GeBufferFormatToString(fb_format), fb_address);
return true;
} else {
WARN_LOG_ONCE(subarea, Log::G3D, "Matching from framebuffer at %08x +%dx%d", fb_address, matchInfo->xOffset, matchInfo->yOffset);
return true;
}
} else {
WARN_LOG_ONCE(diffFormat2, Log::G3D, "Ignoring possible texturing from framebuffer at %08x with incompatible format %s != %s (+%dx%d)",
fb_address, GeTextureFormatToString(entry.format), GeBufferFormatToString(fb_format), matchInfo->xOffset, matchInfo->yOffset);
return false;
}
}
}
void TextureCacheCommon::SetTextureFramebuffer(const AttachCandidate &candidate) {
VirtualFramebuffer *framebuffer = candidate.fb;
RasterChannel channel = candidate.channel;
if (candidate.match.reinterpret) {
framebuffer = framebufferManager_->ResolveFramebufferColorToFormat(candidate.fb, candidate.match.reinterpretTo);
}
_dbg_assert_msg_(framebuffer != nullptr, "Framebuffer must not be null.");
framebuffer->usageFlags |= FB_USAGE_TEXTURE;
framebuffer->last_frame_used = gpuStats.numFlips;
nextFramebufferTextureChannel_ = RASTER_COLOR;
if (framebufferManager_->UseBufferedRendering()) {
FramebufferMatchInfo fbInfo = candidate.match;
u64 depthUpperBits = (channel == RASTER_DEPTH && framebuffer->fb_format == GE_FORMAT_8888) ? ((gstate.getTextureAddress(0) & 0x600000) >> 20) : 0;
bool needsDepthXSwizzle = depthUpperBits == 2;
int texWidth = framebuffer->bufferWidth;
int texHeight = framebuffer->bufferHeight;
if (candidate.channel == RASTER_COLOR && gstate.getTextureFormat() == GE_TFMT_CLUT8 && framebuffer->fb_format == GE_FORMAT_5551 && PSP_CoreParameter().compat.flags().SOCOMClut8Replacement) {
texWidth *= 2.0f;
}
if (needsDepthXSwizzle) {
texWidth = RoundUpToPowerOf2(texWidth);
}
gstate_c.curTextureWidth = texWidth;
gstate_c.curTextureHeight = texHeight;
gstate_c.SetTextureIsFramebuffer(true);
gstate_c.SetTextureIsBGRA(false);
if ((gstate_c.curTextureXOffset == 0) != (fbInfo.xOffset == 0) || (gstate_c.curTextureYOffset == 0) != (fbInfo.yOffset == 0)) {
gstate_c.Dirty(DIRTY_FRAGMENTSHADER_STATE);
}
gstate_c.curTextureXOffset = fbInfo.xOffset;
gstate_c.curTextureYOffset = fbInfo.yOffset;
u32 texW = (u32)gstate.getTextureWidth(0);
u32 texH = (u32)gstate.getTextureHeight(0);
gstate_c.SetNeedShaderTexclamp(gstate_c.curTextureWidth != texW || gstate_c.curTextureHeight != texH);
if (gstate_c.curTextureXOffset != 0 || gstate_c.curTextureYOffset != 0) {
gstate_c.SetNeedShaderTexclamp(true);
}
if (channel == RASTER_DEPTH) {
framebuffer->usageFlags |= FB_USAGE_COLOR_MIXED_DEPTH;
}
if (channel == RASTER_DEPTH && !gstate_c.Use(GPU_USE_DEPTH_TEXTURE)) {
WARN_LOG_ONCE(ndepthtex, Log::G3D, "Depth textures not supported, not binding");
nextFramebufferTexture_ = nullptr;
failedTexture_ = true;
} else {
nextFramebufferTexture_ = framebuffer;
nextFramebufferTextureChannel_ = channel;
}
nextTexture_ = nullptr;
} else {
if (framebuffer->fbo) {
framebuffer->fbo->Release();
framebuffer->fbo = nullptr;
}
Unbind();
gstate_c.SetNeedShaderTexclamp(false);
nextFramebufferTexture_ = nullptr;
nextTexture_ = nullptr;
}
gstate_c.SetTextureIsVideo(false);
gstate_c.SetTextureIs3D(false);
gstate_c.SetTextureIsArray(true);
nextNeedsRehash_ = false;
nextNeedsChange_ = false;
nextNeedsRebuild_ = false;
}
bool TextureCacheCommon::SetOffsetTexture(u32 yOffset) {
if (!framebufferManager_->UseBufferedRendering()) {
return false;
}
u32 texaddr = gstate.getTextureAddress(0);
GETextureFormat fmt = gstate.getTextureFormat();
const u32 bpp = fmt == GE_TFMT_8888 ? 4 : 2;
const u32 texaddrOffset = yOffset * gstate.getTextureWidth(0) * bpp;
if (!Memory::IsValidAddress(texaddr) || !Memory::IsValidAddress(texaddr + texaddrOffset)) {
return false;
}
TextureDefinition def;
def.addr = texaddr;
def.format = fmt;
def.bufw = GetTextureBufw(0, texaddr, fmt);
def.dim = gstate.getTextureDimension(0);
AttachCandidate bestCandidate;
if (GetBestFramebufferCandidate(def, texaddrOffset, &bestCandidate)) {
SetTextureFramebuffer(bestCandidate);
return true;
} else {
return false;
}
}
bool TextureCacheCommon::GetCurrentFramebufferTextureDebug(GPUDebugBuffer &buffer, bool *isFramebuffer) {
if (!nextFramebufferTexture_)
return false;
*isFramebuffer = true;
VirtualFramebuffer *vfb = nextFramebufferTexture_;
u8 sf = vfb->renderScaleFactor;
int x = gstate_c.curTextureXOffset * sf;
int y = gstate_c.curTextureYOffset * sf;
int desiredW = gstate.getTextureWidth(0) * sf;
int desiredH = gstate.getTextureHeight(0) * sf;
int w = std::min(desiredW, vfb->bufferWidth * sf - x);
int h = std::min(desiredH, vfb->bufferHeight * sf - y);
bool retval;
if (nextFramebufferTextureChannel_ == RASTER_DEPTH) {
buffer.Allocate(desiredW, desiredH, GPU_DBG_FORMAT_FLOAT, false);
if (w < desiredW || h < desiredH)
buffer.ZeroBytes();
retval = draw_->CopyFramebufferToMemory(vfb->fbo, Draw::Aspect::DEPTH_BIT, x, y, w, h, Draw::DataFormat::D32F, buffer.GetData(), desiredW, Draw::ReadbackMode::BLOCK, "GetCurrentTextureDebug");
} else {
buffer.Allocate(desiredW, desiredH, GPU_DBG_FORMAT_8888, false);
if (w < desiredW || h < desiredH)
buffer.ZeroBytes();
retval = draw_->CopyFramebufferToMemory(vfb->fbo, Draw::Aspect::COLOR_BIT, x, y, w, h, Draw::DataFormat::R8G8B8A8_UNORM, buffer.GetData(), desiredW, Draw::ReadbackMode::BLOCK, "GetCurrentTextureDebug");
}
gstate_c.Dirty(DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE);
framebufferManager_->RebindFramebuffer("RebindFramebuffer - GetCurrentTextureDebug");
if (!retval)
ERROR_LOG(Log::G3D, "Failed to get debug texture: copy to memory failed");
return retval;
}
void TextureCacheCommon::NotifyConfigChanged() {
int scaleFactor = g_Config.iTexScalingLevel;
if (!gstate_c.Use(GPU_USE_TEXTURE_NPOT)) {
while ((scaleFactor & (scaleFactor - 1)) != 0) {
--scaleFactor;
}
}
if (scaleFactor <= 0) {
scaleFactor = 1;
}
standardScaleFactor_ = scaleFactor;
replacer_.NotifyConfigChanged();
}
void TextureCacheCommon::NotifyWriteFormattedFromMemory(u32 addr, int size, int width, GEBufferFormat fmt) {
addr &= 0x3FFFFFFF;
videos_.push_back({ addr, (u32)size, gpuStats.numFlips });
}
void TextureCacheCommon::LoadClut(u32 clutAddr, u32 loadBytes, GPURecord::Recorder *recorder) {
if (loadBytes == 0) {
return;
}
_assert_(loadBytes <= 2048);
clutTotalBytes_ = loadBytes;
clutRenderAddress_ = 0xFFFFFFFF;
if (!Memory::IsValidAddress(clutAddr)) {
memset(clutBufRaw_, 0x00, loadBytes);
clutLastFormat_ = 0xFFFFFFFF;
clutMaxBytes_ = std::max(clutMaxBytes_, loadBytes);
return;
}
if (Memory::IsVRAMAddress(clutAddr)) {
const u32 clutLoadAddr = clutAddr & 0x041FFFFF;
const u32 clutLoadEnd = clutLoadAddr + loadBytes;
static const u32 MAX_CLUT_OFFSET = 4096;
clutRenderOffset_ = MAX_CLUT_OFFSET;
const std::vector<VirtualFramebuffer *> &framebuffers = framebufferManager_->Framebuffers();
u32 bestClutAddress = 0xFFFFFFFF;
VirtualFramebuffer *chosenFramebuffer = nullptr;
for (VirtualFramebuffer *framebuffer : framebuffers) {
if (framebuffer->fb_stride == 0)
continue;
const u32 fb_address = framebuffer->fb_address;
const u32 fb_bpp = BufferFormatBytesPerPixel(framebuffer->fb_format);
int offset = clutLoadAddr - fb_address;
bool matchRange = offset >= 0 && offset < (int)(framebuffer->fb_stride * fb_bpp);
if (matchRange) {
int fbMatchWidth = framebuffer->width;
if (fbMatchWidth == 512) {
fbMatchWidth = 480;
}
int pixelOffsetX = ((offset / fb_bpp) % framebuffer->fb_stride);
bool inMargin = pixelOffsetX >= fbMatchWidth && (pixelOffsetX + (loadBytes / fb_bpp) <= framebuffer->fb_stride);
bool okAge = !PSP_CoreParameter().compat.flags().LoadCLUTFromCurrentFrameOnly || framebuffer->last_frame_render == gpuStats.numFlips;
if (matchRange && !inMargin && offset < (int)clutRenderOffset_) {
if (okAge) {
WARN_LOG_N_TIMES(clutfb, 5, Log::G3D, "Detected LoadCLUT(%d bytes) from framebuffer %08x (%s), last render %d frames ago, byte offset %d, pixel offset %d",
loadBytes, fb_address, GeBufferFormatToString(framebuffer->fb_format), gpuStats.numFlips - framebuffer->last_frame_render, offset, offset / fb_bpp);
framebuffer->last_frame_clut = gpuStats.numFlips;
framebuffer->last_frame_used = gpuStats.numFlips;
framebuffer->usageFlags |= FB_USAGE_CLUT;
bestClutAddress = framebuffer->fb_address;
clutRenderOffset_ = (u32)offset;
chosenFramebuffer = framebuffer;
if (offset == 0) {
break;
}
} else {
WARN_LOG(Log::G3D, "Ignoring CLUT load from %d frames old buffer at %08x", gpuStats.numFlips - framebuffer->last_frame_render, fb_address);
}
}
}
}
if (chosenFramebuffer && chosenFramebuffer->fbo) {
clutRenderAddress_ = bestClutAddress;
if (!dynamicClutTemp_) {
Draw::FramebufferDesc desc{};
desc.width = 512;
desc.height = 1;
desc.depth = 1;
desc.z_stencil = false;
desc.numLayers = 1;
desc.multiSampleLevel = 0;
desc.tag = "dynamic_clut";
dynamicClutFbo_ = draw_->CreateFramebuffer(desc);
desc.tag = "dynamic_clut_temp";
dynamicClutTemp_ = draw_->CreateFramebuffer(desc);
}
const u32 fb_bpp = BufferFormatBytesPerPixel(chosenFramebuffer->fb_format);
const int totalPixelsOffset = clutRenderOffset_ / fb_bpp;
const int clutYOffset = totalPixelsOffset / chosenFramebuffer->fb_stride;
const int clutXOffset = totalPixelsOffset % chosenFramebuffer->fb_stride;
const int scale = chosenFramebuffer->renderScaleFactor;
framebufferManager_->BlitUsingRaster(
chosenFramebuffer->fbo, clutXOffset * scale, clutYOffset * scale, (clutXOffset + 512.0f) * scale, (clutYOffset + 1.0f) * scale,
dynamicClutTemp_, 0.0f, 0.0f, 512.0f, 1.0f,
false, scale, framebufferManager_->Get2DPipeline(DRAW2D_COPY_COLOR_RECT2LIN), "copy_clut_to_temp");
framebufferManager_->RebindFramebuffer("after_copy_clut_to_temp");
clutRenderFormat_ = chosenFramebuffer->fb_format;
}
NotifyMemInfo(MemBlockFlags::ALLOC, clutAddr, loadBytes, "CLUT");
}
u32 bytes = Memory::ValidSize(clutAddr, loadBytes);
_assert_(bytes <= 2048);
bool performDownload = PSP_CoreParameter().compat.flags().AllowDownloadCLUT;
if (recorder->IsActive())
performDownload = true;
if (clutRenderAddress_ != 0xFFFFFFFF && performDownload) {
framebufferManager_->DownloadFramebufferForClut(clutRenderAddress_, clutRenderOffset_ + bytes);
Memory::MemcpyUnchecked(clutBufRaw_, clutAddr, bytes);
if (bytes < loadBytes) {
memset((u8 *)clutBufRaw_ + bytes, 0x00, loadBytes - bytes);
}
} else {
#ifdef _M_SSE
if (bytes == loadBytes) {
const __m128i *source = (const __m128i *)Memory::GetPointerUnchecked(clutAddr);
__m128i *dest = (__m128i *)clutBufRaw_;
int numBlocks = bytes / 32;
for (int i = 0; i < numBlocks; i++, source += 2, dest += 2) {
__m128i data1 = _mm_loadu_si128(source);
__m128i data2 = _mm_loadu_si128(source + 1);
_mm_store_si128(dest, data1);
_mm_store_si128(dest + 1, data2);
}
} else {
Memory::MemcpyUnchecked(clutBufRaw_, clutAddr, bytes);
if (bytes < loadBytes) {
memset((u8 *)clutBufRaw_ + bytes, 0x00, loadBytes - bytes);
}
}
#elif PPSSPP_ARCH(ARM_NEON)
if (bytes == loadBytes) {
const uint32_t *source = (const uint32_t *)Memory::GetPointerUnchecked(clutAddr);
uint32_t *dest = (uint32_t *)clutBufRaw_;
int numBlocks = bytes / 32;
for (int i = 0; i < numBlocks; i++, source += 8, dest += 8) {
uint32x4_t data1 = vld1q_u32(source);
uint32x4_t data2 = vld1q_u32(source + 4);
vst1q_u32(dest, data1);
vst1q_u32(dest + 4, data2);
}
} else {
Memory::MemcpyUnchecked(clutBufRaw_, clutAddr, bytes);
if (bytes < loadBytes) {
memset((u8 *)clutBufRaw_ + bytes, 0x00, loadBytes - bytes);
}
}
#else
Memory::MemcpyUnchecked(clutBufRaw_, clutAddr, bytes);
if (bytes < loadBytes) {
memset((u8 *)clutBufRaw_ + bytes, 0x00, loadBytes - bytes);
}
#endif
}
clutLastFormat_ = 0xFFFFFFFF;
clutMaxBytes_ = std::max(clutMaxBytes_, loadBytes);
}
void TextureCacheCommon::UnswizzleFromMem(u32 *dest, u32 destPitch, const u8 *texptr, u32 bufw, u32 height, u32 bytesPerPixel) {
const u32 rowWidth = (bytesPerPixel > 0) ? (bufw * bytesPerPixel) : (bufw / 2);
const int bxc = rowWidth / 16;
int byc = (height + 7) / 8;
DoUnswizzleTex16(texptr, dest, bxc, byc, destPitch);
}
bool TextureCacheCommon::GetCurrentClutBuffer(GPUDebugBuffer &buffer) {
const u32 bpp = gstate.getClutPaletteFormat() == GE_CMODE_32BIT_ABGR8888 ? 4 : 2;
const u32 pixels = 1024 / bpp;
buffer.Allocate(pixels, 1, (GEBufferFormat)gstate.getClutPaletteFormat());
memcpy(buffer.GetData(), clutBufRaw_, 1024);
return true;
}
u32 TextureCacheCommon::EstimateTexMemoryUsage(const TexCacheEntry *entry) {
const u16 dim = entry->dim;
const u8 dimW = ((dim >> 0) & 0xf);
const u8 dimH = ((dim >> 8) & 0xf);
u32 pixelSize = 2;
switch (entry->format) {
case GE_TFMT_CLUT4:
case GE_TFMT_CLUT8:
case GE_TFMT_CLUT16:
case GE_TFMT_CLUT32:
pixelSize = 4;
break;
case GE_TFMT_4444:
case GE_TFMT_5551:
case GE_TFMT_5650:
break;
case GE_TFMT_8888:
case GE_TFMT_DXT1:
case GE_TFMT_DXT3:
case GE_TFMT_DXT5:
default:
pixelSize = 4;
break;
}
return pixelSize << (dimW + dimH);
}
ReplacedTexture *TextureCacheCommon::FindReplacement(TexCacheEntry *entry, int *w, int *h, int *d) {
if (*d != 1) {
return nullptr;
}
if (!replacer_.ReplaceEnabled()) {
return nullptr;
}
if ((entry->status & TexCacheEntry::STATUS_VIDEO) && !replacer_.AllowVideo()) {
return nullptr;
}
double replaceStart = time_now_d();
u64 cachekey = entry->CacheKey();
ReplacedTexture *replaced = replacer_.FindReplacement(cachekey, entry->fullhash, *w, *h);
replacementTimeThisFrame_ += time_now_d() - replaceStart;
if (!replaced) {
return nullptr;
}
entry->replacedTexture = replaced;
PollReplacement(entry, w, h, d);
return replaced;
}
void TextureCacheCommon::PollReplacement(TexCacheEntry *entry, int *w, int *h, int *d) {
double waitBudget = replacementFrameBudgetSeconds_ - replacementTimeThisFrame_;
double replaceStart = time_now_d();
if (g_Config.iReplacementTextureLoadSpeed != ReplacementTextureLoadSpeed::INSTANT) {
waitBudget = 0.0;
}
if (entry->replacedTexture->Poll(waitBudget)) {
if (entry->replacedTexture->State() == ReplacementState::ACTIVE) {
entry->replacedTexture->GetSize(0, w, h);
entry->status |= TexCacheEntry::STATUS_IS_SCALED_OR_REPLACED;
}
entry->status &= ~TexCacheEntry::STATUS_TO_REPLACE;
}
replacementTimeThisFrame_ += time_now_d() - replaceStart;
switch (entry->replacedTexture->State()) {
case ReplacementState::UNLOADED:
case ReplacementState::PENDING:
entry->status |= TexCacheEntry::STATUS_TO_REPLACE;
break;
default:
break;
}
}
static void ReverseColors(void *dstBuf, const void *srcBuf, GETextureFormat fmt, int numPixels) {
switch (fmt) {
case GE_TFMT_4444:
ConvertRGBA4444ToABGR4444((u16 *)dstBuf, (const u16 *)srcBuf, numPixels);
break;
case GE_TFMT_5551:
ConvertRGBA5551ToABGR1555((u16 *)dstBuf, (const u16 *)srcBuf, numPixels);
break;
case GE_TFMT_5650:
ConvertRGB565ToBGR565((u16 *)dstBuf, (const u16 *)srcBuf, numPixels);
break;
default:
if (dstBuf != srcBuf) {
memcpy(dstBuf, srcBuf, numPixels * sizeof(u32));
}
break;
}
}
static inline void ConvertFormatToRGBA8888(GETextureFormat format, u32 *dst, const u16 *src, u32 numPixels) {
switch (format) {
case GE_TFMT_4444:
ConvertRGBA4444ToRGBA8888(dst, src, numPixels);
break;
case GE_TFMT_5551:
ConvertRGBA5551ToRGBA8888(dst, src, numPixels);
break;
case GE_TFMT_5650:
ConvertRGB565ToRGBA8888(dst, src, numPixels);
break;
default:
_dbg_assert_msg_(false, "Incorrect texture format.");
break;
}
}
static inline void ConvertFormatToRGBA8888(GEPaletteFormat format, u32 *dst, const u16 *src, u32 numPixels) {
ConvertFormatToRGBA8888(GETextureFormat(format), dst, src, numPixels);
}
template <typename DXTBlock, int n>
static CheckAlphaResult DecodeDXTBlocks(uint8_t *out, int outPitch, uint32_t texaddr, const uint8_t *texptr,
int w, int h, int bufw, bool reverseColors) {
int minw = std::min(bufw, w);
uint32_t *dst = (uint32_t *)out;
int outPitch32 = outPitch / sizeof(uint32_t);
const DXTBlock *src = (const DXTBlock *)texptr;
if (!Memory::IsValidRange(texaddr, (h / 4) * (bufw / 4) * sizeof(DXTBlock))) {
ERROR_LOG_REPORT(Log::G3D, "DXT%d texture extends beyond valid RAM: %08x + %d x %d", n, texaddr, bufw, h);
uint32_t limited = Memory::ValidSize(texaddr, (h / 4) * (bufw / 4) * sizeof(DXTBlock));
h = (((int)limited / sizeof(DXTBlock)) / (bufw / 4)) * 4;
}
u32 alphaSum = 1;
for (int y = 0; y < h; y += 4) {
u32 blockIndex = (y / 4) * (bufw / 4);
int blockHeight = std::min(h - y, 4);
for (int x = 0; x < minw; x += 4) {
int blockWidth = std::min(minw - x, 4);
if constexpr (n == 1)
DecodeDXT1Block(dst + outPitch32 * y + x, (const DXT1Block *)src + blockIndex, outPitch32, blockWidth, blockHeight, &alphaSum);
else if constexpr (n == 3)
DecodeDXT3Block(dst + outPitch32 * y + x, (const DXT3Block *)src + blockIndex, outPitch32, blockWidth, blockHeight);
else if constexpr (n == 5)
DecodeDXT5Block(dst + outPitch32 * y + x, (const DXT5Block *)src + blockIndex, outPitch32, blockWidth, blockHeight);
blockIndex++;
}
}
if (reverseColors) {
ReverseColors(out, out, GE_TFMT_8888, outPitch32 * h);
}
if constexpr (n == 1) {
return alphaSum == 1 ? CHECKALPHA_FULL : CHECKALPHA_ANY;
} else {
return CHECKALPHA_ANY;
}
}
inline u32 ClutFormatToFullAlpha(GEPaletteFormat fmt, bool reverseColors) {
switch (fmt) {
case GE_CMODE_16BIT_ABGR4444: return reverseColors ? 0x000F : 0xF000;
case GE_CMODE_16BIT_ABGR5551: return reverseColors ? 0x0001 : 0x8000;
case GE_CMODE_32BIT_ABGR8888: return 0xFF000000;
case GE_CMODE_16BIT_BGR5650: return 0;
default: return 0;
}
}
inline u32 TfmtRawToFullAlpha(GETextureFormat fmt) {
switch (fmt) {
case GE_TFMT_4444: return 0xF000;
case GE_TFMT_5551: return 0x8000;
case GE_TFMT_8888: return 0xFF000000;
case GE_TFMT_5650: return 0;
default: return 0;
}
}
static void Expand4To8Bits(u8 *dest, const u8 *src, int srcWidth) {
for (int i = 0; i < (srcWidth + 1) / 2; i++) {
u8 lower = src[i] & 0xF;
u8 upper = src[i] >> 4;
dest[i * 2] = lower;
dest[i * 2 + 1] = upper;
}
}
CheckAlphaResult TextureCacheCommon::DecodeTextureLevel(u8 *out, int outPitch, GETextureFormat format, GEPaletteFormat clutformat, uint32_t texaddr, int level, int bufw, TexDecodeFlags flags) {
u32 alphaSum = 0xFFFFFFFF;
u32 fullAlphaMask = 0x0;
bool expandTo32bit = (flags & TexDecodeFlags::EXPAND32) != 0;
bool reverseColors = (flags & TexDecodeFlags::REVERSE_COLORS) != 0;
bool toClut8 = (flags & TexDecodeFlags::TO_CLUT8) != 0;
if (toClut8 && format != GE_TFMT_CLUT8 && format != GE_TFMT_CLUT4) {
_dbg_assert_(false);
}
bool swizzled = gstate.isTextureSwizzled();
if ((texaddr & 0x00600000) != 0 && Memory::IsVRAMAddress(texaddr)) {
WARN_LOG_REPORT_ONCE(texmirror, Log::G3D, "Decoding texture from VRAM mirror at %08x swizzle=%d", texaddr, swizzled ? 1 : 0);
if ((texaddr & 0x00200000) == 0x00200000) {
swizzled = !swizzled;
}
}
int w = gstate.getTextureWidth(level);
int h = gstate.getTextureHeight(level);
const u8 *texptr = Memory::GetPointer(texaddr);
const uint32_t byteSize = (textureBitsPerPixel[format] * bufw * h) / 8;
char buf[128];
size_t len = snprintf(buf, sizeof(buf), "Tex_%08x_%dx%d_%s", texaddr, w, h, GeTextureFormatToString(format, clutformat));
NotifyMemInfo(MemBlockFlags::TEXTURE, texaddr, byteSize, buf, len);
switch (format) {
case GE_TFMT_CLUT4:
{
const bool mipmapShareClut = gstate.isClutSharedForMipmaps();
const int clutSharingOffset = mipmapShareClut ? 0 : level * 16;
if (swizzled) {
tmpTexBuf32_.resize(bufw * ((h + 7) & ~7));
UnswizzleFromMem(tmpTexBuf32_.data(), bufw / 2, texptr, bufw, h, 0);
texptr = (u8 *)tmpTexBuf32_.data();
}
if (toClut8) {
for (int y = 0; y < h; ++y) {
Expand4To8Bits((u8 *)out + outPitch * y, texptr + (bufw * y) / 2, w);
}
return CHECKALPHA_ANY;
}
switch (clutformat) {
case GE_CMODE_16BIT_BGR5650:
case GE_CMODE_16BIT_ABGR5551:
case GE_CMODE_16BIT_ABGR4444:
{
if (clutAlphaLinear_ && mipmapShareClut && !expandTo32bit && w >= 4) {
if (reverseColors) {
for (int y = 0; y < h; ++y) {
DeIndexTexture4Optimal((u16 *)(out + outPitch * y), texptr + (bufw * y) / 2, w, clutAlphaLinearColor_);
}
} else {
for (int y = 0; y < h; ++y) {
DeIndexTexture4OptimalRev((u16 *)(out + outPitch * y), texptr + (bufw * y) / 2, w, clutAlphaLinearColor_);
}
}
} else {
if (expandTo32bit) {
const u16 *clut = GetCurrentRawClut<u16>() + clutSharingOffset;
const int clutStart = gstate.getClutIndexStartPos();
if (gstate.getClutIndexShift() == 0 || gstate.getClutIndexMask() <= 16) {
ConvertFormatToRGBA8888(clutformat, expandClut_ + clutStart, clut + clutStart, 16);
} else {
ConvertFormatToRGBA8888(clutformat, expandClut_, clut, 512);
}
fullAlphaMask = 0xFF000000;
for (int y = 0; y < h; ++y) {
DeIndexTexture4<u32>((u32 *)(out + outPitch * y), texptr + (bufw * y) / 2, w, expandClut_, &alphaSum);
}
} else {
const u16 *clut = GetCurrentClut<u16>() + clutSharingOffset;
fullAlphaMask = ClutFormatToFullAlpha(clutformat, reverseColors);
for (int y = 0; y < h; ++y) {
DeIndexTexture4<u16>((u16 *)(out + outPitch * y), texptr + (bufw * y) / 2, w, clut, &alphaSum);
}
}
}
if (clutformat == GE_CMODE_16BIT_BGR5650) {
return CHECKALPHA_FULL;
}
}
break;
case GE_CMODE_32BIT_ABGR8888:
{
const u32 *clut = GetCurrentClut<u32>() + clutSharingOffset;
fullAlphaMask = 0xFF000000;
for (int y = 0; y < h; ++y) {
DeIndexTexture4<u32>((u32 *)(out + outPitch * y), texptr + (bufw * y) / 2, w, clut, &alphaSum);
}
}
break;
default:
ERROR_LOG_REPORT(Log::G3D, "Unknown CLUT4 texture mode %d", gstate.getClutPaletteFormat());
return CHECKALPHA_ANY;
}
}
break;
case GE_TFMT_CLUT8:
if (toClut8) {
if (gstate.isTextureSwizzled()) {
tmpTexBuf32_.resize(bufw * ((h + 7) & ~7));
UnswizzleFromMem(tmpTexBuf32_.data(), bufw, texptr, bufw, h, 1);
texptr = (u8 *)tmpTexBuf32_.data();
}
for (int y = 0; y < h; ++y) {
memcpy((u8 *)out + outPitch * y, texptr + (bufw * y), w);
}
return CHECKALPHA_ANY;
}
return ReadIndexedTex(out, outPitch, level, texptr, 1, bufw, reverseColors, expandTo32bit);
case GE_TFMT_CLUT16:
return ReadIndexedTex(out, outPitch, level, texptr, 2, bufw, reverseColors, expandTo32bit);
case GE_TFMT_CLUT32:
return ReadIndexedTex(out, outPitch, level, texptr, 4, bufw, reverseColors, expandTo32bit);
case GE_TFMT_4444:
case GE_TFMT_5551:
case GE_TFMT_5650:
if (!swizzled) {
fullAlphaMask = TfmtRawToFullAlpha(format);
if (expandTo32bit) {
for (int y = 0; y < h; ++y) {
CheckMask16((const u16 *)(texptr + bufw * sizeof(u16) * y), w, &alphaSum);
ConvertFormatToRGBA8888(format, (u32 *)(out + outPitch * y), (const u16 *)texptr + bufw * y, w);
}
} else if (reverseColors) {
for (int y = 0; y < h; ++y) {
CheckMask16((const u16 *)(texptr + bufw * sizeof(u16) * y), w, &alphaSum);
ReverseColors(out + outPitch * y, texptr + bufw * sizeof(u16) * y, format, w);
}
} else {
for (int y = 0; y < h; ++y) {
CopyAndSumMask16((u16 *)(out + outPitch * y), (u16 *)(texptr + bufw * sizeof(u16) * y), w, &alphaSum);
}
}
}
else {
tmpTexBuf32_.resize(bufw * ((h + 7) & ~7));
UnswizzleFromMem(tmpTexBuf32_.data(), bufw * 2, texptr, bufw, h, 2);
const u8 *unswizzled = (u8 *)tmpTexBuf32_.data();
fullAlphaMask = TfmtRawToFullAlpha(format);
if (expandTo32bit) {
for (int y = 0; y < h; ++y) {
CheckMask16((const u16 *)(unswizzled + bufw * sizeof(u16) * y), w, &alphaSum);
ConvertFormatToRGBA8888(format, (u32 *)(out + outPitch * y), (const u16 *)unswizzled + bufw * y, w);
}
} else if (reverseColors) {
for (int y = 0; y < h; ++y) {
CheckMask16((const u16 *)(unswizzled + bufw * sizeof(u16) * y), w, &alphaSum);
ReverseColors(out + outPitch * y, unswizzled + bufw * sizeof(u16) * y, format, w);
}
} else {
for (int y = 0; y < h; ++y) {
CopyAndSumMask16((u16 *)(out + outPitch * y), (const u16 *)(unswizzled + bufw * sizeof(u16) * y), w, &alphaSum);
}
}
}
if (format == GE_TFMT_5650) {
return CHECKALPHA_FULL;
}
break;
case GE_TFMT_8888:
if (!swizzled) {
fullAlphaMask = TfmtRawToFullAlpha(format);
if (reverseColors) {
for (int y = 0; y < h; ++y) {
CheckMask32((const u32 *)(texptr + bufw * sizeof(u32) * y), w, &alphaSum);
ReverseColors(out + outPitch * y, texptr + bufw * sizeof(u32) * y, format, w);
}
} else {
for (int y = 0; y < h; ++y) {
CopyAndSumMask32((u32 *)(out + outPitch * y), (const u32 *)(texptr + bufw * sizeof(u32) * y), w, &alphaSum);
}
}
}
else {
tmpTexBuf32_.resize(bufw * ((h + 7) & ~7));
UnswizzleFromMem(tmpTexBuf32_.data(), bufw * 4, texptr, bufw, h, 4);
const u8 *unswizzled = (u8 *)tmpTexBuf32_.data();
fullAlphaMask = TfmtRawToFullAlpha(format);
if (reverseColors) {
for (int y = 0; y < h; ++y) {
CheckMask32((const u32 *)(unswizzled + bufw * sizeof(u32) * y), w, &alphaSum);
ReverseColors(out + outPitch * y, unswizzled + bufw * sizeof(u32) * y, format, w);
}
} else {
for (int y = 0; y < h; ++y) {
CopyAndSumMask32((u32 *)(out + outPitch * y), (const u32 *)(unswizzled + bufw * sizeof(u32) * y), w, &alphaSum);
}
}
}
break;
case GE_TFMT_DXT1:
return DecodeDXTBlocks<DXT1Block, 1>(out, outPitch, texaddr, texptr, w, h, bufw, reverseColors);
case GE_TFMT_DXT3:
return DecodeDXTBlocks<DXT3Block, 3>(out, outPitch, texaddr, texptr, w, h, bufw, reverseColors);
case GE_TFMT_DXT5:
return DecodeDXTBlocks<DXT5Block, 5>(out, outPitch, texaddr, texptr, w, h, bufw, reverseColors);
default:
ERROR_LOG_REPORT(Log::G3D, "Unknown Texture Format %d!!!", format);
break;
}
return AlphaSumIsFull(alphaSum, fullAlphaMask) ? CHECKALPHA_FULL : CHECKALPHA_ANY;
}
CheckAlphaResult TextureCacheCommon::ReadIndexedTex(u8 *out, int outPitch, int level, const u8 *texptr, int bytesPerIndex, int bufw, bool reverseColors, bool expandTo32Bit) {
int w = gstate.getTextureWidth(level);
int h = gstate.getTextureHeight(level);
if (gstate.isTextureSwizzled()) {
tmpTexBuf32_.resize(bufw * ((h + 7) & ~7));
UnswizzleFromMem(tmpTexBuf32_.data(), bufw * bytesPerIndex, texptr, bufw, h, bytesPerIndex);
texptr = (u8 *)tmpTexBuf32_.data();
}
const bool mipmapShareClut = gstate.isClutSharedForMipmaps() || gstate.getClutLoadBlocks() != 0x40;
const int clutSharingOffset = mipmapShareClut ? 0 : (level & 1) * 256;
GEPaletteFormat palFormat = (GEPaletteFormat)gstate.getClutPaletteFormat();
const u16 *clut16 = (const u16 *)clutBuf_ + clutSharingOffset;
const u32 *clut32 = (const u32 *)clutBuf_ + clutSharingOffset;
if (expandTo32Bit && palFormat != GE_CMODE_32BIT_ABGR8888) {
const u16 *clut16raw = (const u16 *)clutBufRaw_ + clutSharingOffset;
const int clutStart = gstate.getClutIndexStartPos();
if (clutStart > 256) {
ConvertFormatToRGBA8888(GEPaletteFormat(palFormat), expandClut_, clut16raw, 512);
} else {
ConvertFormatToRGBA8888(GEPaletteFormat(palFormat), expandClut_ + clutStart, clut16raw + clutStart, 256);
}
clut32 = expandClut_;
palFormat = GE_CMODE_32BIT_ABGR8888;
}
u32 alphaSum = 0xFFFFFFFF;
u32 fullAlphaMask = ClutFormatToFullAlpha(palFormat, reverseColors);
switch (palFormat) {
case GE_CMODE_16BIT_BGR5650:
case GE_CMODE_16BIT_ABGR5551:
case GE_CMODE_16BIT_ABGR4444:
{
switch (bytesPerIndex) {
case 1:
for (int y = 0; y < h; ++y) {
DeIndexTexture((u16 *)(out + outPitch * y), (const u8 *)texptr + bufw * y, w, clut16, &alphaSum);
}
break;
case 2:
for (int y = 0; y < h; ++y) {
DeIndexTexture((u16 *)(out + outPitch * y), (const u16_le *)texptr + bufw * y, w, clut16, &alphaSum);
}
break;
case 4:
for (int y = 0; y < h; ++y) {
DeIndexTexture((u16 *)(out + outPitch * y), (const u32_le *)texptr + bufw * y, w, clut16, &alphaSum);
}
break;
}
}
break;
case GE_CMODE_32BIT_ABGR8888:
{
switch (bytesPerIndex) {
case 1:
for (int y = 0; y < h; ++y) {
DeIndexTexture((u32 *)(out + outPitch * y), (const u8 *)texptr + bufw * y, w, clut32, &alphaSum);
}
break;
case 2:
for (int y = 0; y < h; ++y) {
DeIndexTexture((u32 *)(out + outPitch * y), (const u16_le *)texptr + bufw * y, w, clut32, &alphaSum);
}
break;
case 4:
for (int y = 0; y < h; ++y) {
DeIndexTexture((u32 *)(out + outPitch * y), (const u32_le *)texptr + bufw * y, w, clut32, &alphaSum);
}
break;
}
}
break;
default:
ERROR_LOG_REPORT(Log::G3D, "Unhandled clut texture mode %d!!!", gstate.getClutPaletteFormat());
break;
}
if (palFormat == GE_CMODE_16BIT_BGR5650) {
return CHECKALPHA_FULL;
} else {
return AlphaSumIsFull(alphaSum, fullAlphaMask) ? CHECKALPHA_FULL : CHECKALPHA_ANY;
}
}
void TextureCacheCommon::ApplyTexture(bool doBind) {
TexCacheEntry *entry = nextTexture_;
if (!entry) {
ForgetLastTexture();
if (failedTexture_) {
BindTexture(nullptr);
} else if (nextFramebufferTexture_) {
ApplyTextureFramebuffer(nextFramebufferTexture_, gstate.getTextureFormat(), nextFramebufferTextureChannel_);
nextFramebufferTexture_ = nullptr;
}
return;
}
nextTexture_ = nullptr;
UpdateMaxSeenV(entry, gstate.isModeThrough());
if (nextNeedsRebuild_) {
if (IsVideo(entry->addr)) {
entry->status |= TexCacheEntry::STATUS_CHANGE_FREQUENT | TexCacheEntry::STATUS_VIDEO;
} else {
entry->status &= ~TexCacheEntry::STATUS_VIDEO;
}
if (nextNeedsRehash_) {
PROFILE_THIS_SCOPE("texhash");
int w = gstate.getTextureWidth(0);
int h = gstate.getTextureHeight(0);
bool swizzled = gstate.isTextureSwizzled();
entry->fullhash = QuickTexHash(replacer_, entry->addr, entry->bufw, w, h, swizzled, GETextureFormat(entry->format), entry);
}
if (nextNeedsChange_) {
HandleTextureChange(entry, nextChangeReason_, false, true);
}
} else if (nextNeedsRehash_) {
bool doDelete = true;
if (!CheckFullHash(entry, doDelete)) {
HandleTextureChange(entry, "hash fail", true, doDelete);
nextNeedsRebuild_ = true;
} else if (nextTexture_ != nullptr) {
entry = nextTexture_;
nextTexture_ = nullptr;
UpdateMaxSeenV(entry, gstate.isModeThrough());
}
}
if (nextNeedsRebuild_) {
_assert_(!entry->texturePtr);
BuildTexture(entry);
ForgetLastTexture();
}
gstate_c.SetTextureIsVideo((entry->status & TexCacheEntry::STATUS_VIDEO) != 0);
if (entry->status & TexCacheEntry::STATUS_CLUT_GPU) {
ApplyTextureDepal(entry);
entry->lastFrame = gpuStats.numFlips;
gstate_c.SetTextureFullAlpha(false);
gstate_c.SetTextureIs3D(false);
gstate_c.SetTextureIsArray(false);
gstate_c.SetTextureIsBGRA(false);
} else {
entry->lastFrame = gpuStats.numFlips;
if (doBind) {
BindTexture(entry);
}
gstate_c.SetTextureFullAlpha(entry->GetAlphaStatus() == TexCacheEntry::STATUS_ALPHA_FULL);
gstate_c.SetTextureIs3D((entry->status & TexCacheEntry::STATUS_3D) != 0);
gstate_c.SetTextureIsArray(false);
gstate_c.SetTextureIsBGRA((entry->status & TexCacheEntry::STATUS_BGRA) != 0);
gstate_c.SetUseShaderDepal(ShaderDepalMode::OFF);
}
}
static bool CanDepalettize(GETextureFormat texFormat, GEBufferFormat bufferFormat) {
if (IsClutFormat(texFormat)) {
switch (bufferFormat) {
case GE_FORMAT_4444:
case GE_FORMAT_565:
case GE_FORMAT_5551:
case GE_FORMAT_DEPTH16:
if (texFormat == GE_TFMT_CLUT16) {
return true;
}
if (texFormat == GE_TFMT_CLUT8 && bufferFormat == GE_FORMAT_5551 && PSP_CoreParameter().compat.flags().SOCOMClut8Replacement) {
return true;
}
break;
case GE_FORMAT_8888:
if (texFormat == GE_TFMT_CLUT32 || texFormat == GE_TFMT_CLUT8) {
return true;
}
break;
case GE_FORMAT_CLUT8:
case GE_FORMAT_INVALID:
return false;
}
WARN_LOG(Log::G3D, "Invalid CLUT/framebuffer combination: %s vs %s", GeTextureFormatToString(texFormat), GeBufferFormatToString(bufferFormat));
return false;
} else if (texFormat == GE_TFMT_5650 && bufferFormat == GE_FORMAT_DEPTH16) {
return true;
}
return false;
}
static bool CanUseSmoothDepal(const GPUgstate &gstate, GEBufferFormat framebufferFormat, const ClutTexture &clutTexture) {
for (int i = 0; i < ClutTexture::MAX_RAMPS; i++) {
if (gstate.getClutIndexStartPos() == clutTexture.rampStarts[i] &&
gstate.getClutIndexMask() < clutTexture.rampLengths[i]) {
switch (framebufferFormat) {
case GE_FORMAT_565:
if (gstate.getClutIndexShift() == 0 || gstate.getClutIndexShift() == 11) {
return gstate.getClutIndexMask() == 0x1F;
} else if (gstate.getClutIndexShift() == 5) {
return gstate.getClutIndexMask() == 0x3F;
}
break;
case GE_FORMAT_5551:
if (gstate.getClutIndexShift() == 0 || gstate.getClutIndexShift() == 5 || gstate.getClutIndexShift() == 10) {
return gstate.getClutIndexMask() == 0x1F;
}
break;
default:
break;
}
}
}
return false;
}
void TextureCacheCommon::ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer, GETextureFormat texFormat, RasterChannel channel) {
Draw2DPipeline *textureShader = nullptr;
uint32_t clutMode = gstate.clutformat & 0xFFFFFF;
bool depth = channel == RASTER_DEPTH;
bool need_depalettize = CanDepalettize(texFormat, depth ? GE_FORMAT_DEPTH16 : framebuffer->fb_format);
bool useShaderDepal = framebufferManager_->GetCurrentRenderVFB() != framebuffer &&
!depth && clutRenderAddress_ == 0xFFFFFFFF &&
!gstate_c.curTextureIs3D &&
draw_->GetShaderLanguageDesc().bitwiseOps &&
!(texFormat == GE_TFMT_CLUT8 && framebuffer->fb_format == GE_FORMAT_5551);
switch (draw_->GetShaderLanguageDesc().shaderLanguage) {
case ShaderLanguage::GLSL_1xx:
useShaderDepal = false;
break;
default:
break;
}
const GEPaletteFormat clutFormat = gstate.getClutPaletteFormat();
ClutTexture clutTexture{};
bool smoothedDepal = false;
u32 depthUpperBits = 0;
if (need_depalettize) {
if (clutRenderAddress_ == 0xFFFFFFFF) {
clutTexture = textureShaderCache_->GetClutTexture(clutFormat, clutHash_, clutBufRaw_);
smoothedDepal = CanUseSmoothDepal(gstate, framebuffer->fb_format, clutTexture);
} else {
GEBufferFormat expectedCLUTBufferFormat = (GEBufferFormat)clutFormat;
float scaleFactorX = 1.0f;
Draw2DPipeline *reinterpret = framebufferManager_->GetReinterpretPipeline(clutRenderFormat_, expectedCLUTBufferFormat, &scaleFactorX);
framebufferManager_->BlitUsingRaster(dynamicClutTemp_, 0.0f, 0.0f, 512.0f, 1.0f, dynamicClutFbo_, 0.0f, 0.0f, scaleFactorX * 512.0f, 1.0f, false, 1.0f, reinterpret, "reinterpret_clut");
}
if (useShaderDepal) {
BindAsClutTexture(clutTexture.texture, smoothedDepal);
framebufferManager_->BindFramebufferAsColorTexture(0, framebuffer, BINDFBCOLOR_MAY_COPY_WITH_UV | BINDFBCOLOR_APPLY_TEX_OFFSET, Draw::ALL_LAYERS);
BoundFramebufferTexture();
SamplerCacheKey samplerKey = GetFramebufferSamplingParams(framebuffer->bufferWidth, framebuffer->bufferHeight);
samplerKey.magFilt = false;
samplerKey.minFilt = false;
samplerKey.mipEnable = false;
ApplySamplingParams(samplerKey);
ShaderDepalMode mode = ShaderDepalMode::NORMAL;
if (texFormat == GE_TFMT_CLUT8 && framebuffer->fb_format == GE_FORMAT_8888) {
mode = ShaderDepalMode::CLUT8_8888;
smoothedDepal = false;
} else if (smoothedDepal) {
mode = ShaderDepalMode::SMOOTHED;
}
gstate_c.Dirty(DIRTY_DEPAL);
gstate_c.SetUseShaderDepal(mode);
gstate_c.depalFramebufferFormat = framebuffer->fb_format;
const u32 bytesPerColor = clutFormat == GE_CMODE_32BIT_ABGR8888 ? sizeof(u32) : sizeof(u16);
const u32 clutTotalColors = clutMaxBytes_ / bytesPerColor;
CheckAlphaResult alphaStatus = CheckCLUTAlpha((const uint8_t *)clutBufRaw_, clutFormat, clutTotalColors);
gstate_c.SetTextureFullAlpha(alphaStatus == CHECKALPHA_FULL);
draw_->Invalidate(InvalidationFlags::CACHED_RENDER_STATE);
return;
}
depthUpperBits = (depth && framebuffer->fb_format == GE_FORMAT_8888) ? ((gstate.getTextureAddress(0) & 0x600000) >> 20) : 0;
textureShader = textureShaderCache_->GetDepalettizeShader(clutMode, texFormat, depth ? GE_FORMAT_DEPTH16 : framebuffer->fb_format, smoothedDepal, depthUpperBits);
gstate_c.SetUseShaderDepal(ShaderDepalMode::OFF);
}
if (textureShader) {
bool needsDepthXSwizzle = depthUpperBits == 2;
int depalWidth = framebuffer->renderWidth;
int texWidth = framebuffer->bufferWidth;
if (needsDepthXSwizzle) {
texWidth = RoundUpToPowerOf2(framebuffer->bufferWidth);
depalWidth = texWidth * framebuffer->renderScaleFactor;
gstate_c.Dirty(DIRTY_UVSCALEOFFSET);
}
const KnownVertexBounds &bounds = gstate_c.vertBounds;
float u1 = 0.0f;
float v1 = 0.0f;
float u2 = depalWidth;
float v2 = framebuffer->renderHeight;
if (bounds.minV < bounds.maxV) {
u1 = (bounds.minU + gstate_c.curTextureXOffset) * framebuffer->renderScaleFactor;
v1 = (bounds.minV + gstate_c.curTextureYOffset) * framebuffer->renderScaleFactor;
u2 = (bounds.maxU + gstate_c.curTextureXOffset) * framebuffer->renderScaleFactor;
v2 = (bounds.maxV + gstate_c.curTextureYOffset) * framebuffer->renderScaleFactor;
gstate_c.Dirty(DIRTY_TEXTURE_PARAMS);
}
Draw::Framebuffer *depalFBO = framebufferManager_->GetTempFBO(TempFBO::DEPAL, depalWidth, framebuffer->renderHeight);
draw_->BindTexture(0, nullptr);
draw_->BindTexture(1, nullptr);
draw_->BindFramebufferAsRenderTarget(depalFBO, { Draw::RPAction::DONT_CARE, Draw::RPAction::DONT_CARE, Draw::RPAction::DONT_CARE }, "Depal");
draw_->InvalidateFramebuffer(Draw::FB_INVALIDATION_STORE, Draw::Aspect::DEPTH_BIT | Draw::Aspect::STENCIL_BIT);
draw_->SetScissorRect(u1, v1, u2 - u1, v2 - v1);
Draw::Viewport viewport{ 0.0f, 0.0f, (float)depalWidth, (float)framebuffer->renderHeight, 0.0f, 1.0f };
draw_->SetViewport(viewport);
draw_->BindFramebufferAsTexture(framebuffer->fbo, 0, depth ? Draw::Aspect::DEPTH_BIT : Draw::Aspect::COLOR_BIT, Draw::ALL_LAYERS);
if (clutRenderAddress_ == 0xFFFFFFFF) {
draw_->BindTexture(1, clutTexture.texture);
} else {
draw_->BindFramebufferAsTexture(dynamicClutFbo_, 1, Draw::Aspect::COLOR_BIT, 0);
}
Draw::SamplerState *nearest = textureShaderCache_->GetSampler(false);
Draw::SamplerState *clutSampler = textureShaderCache_->GetSampler(smoothedDepal);
draw_->BindSamplerStates(0, 1, &nearest);
draw_->BindSamplerStates(1, 1, &clutSampler);
draw2D_->Blit(textureShader, u1, v1, u2, v2, u1, v1, u2, v2, framebuffer->renderWidth, framebuffer->renderHeight, depalWidth, framebuffer->renderHeight, false, framebuffer->renderScaleFactor);
gpuStats.numDepal++;
gstate_c.curTextureWidth = texWidth;
gstate_c.Dirty(DIRTY_UVSCALEOFFSET);
draw_->BindTexture(0, nullptr);
framebufferManager_->RebindFramebuffer("ApplyTextureFramebuffer");
draw_->BindFramebufferAsTexture(depalFBO, 0, Draw::Aspect::COLOR_BIT, Draw::ALL_LAYERS);
BoundFramebufferTexture();
const u32 bytesPerColor = clutFormat == GE_CMODE_32BIT_ABGR8888 ? sizeof(u32) : sizeof(u16);
const u32 clutTotalColors = clutMaxBytes_ / bytesPerColor;
CheckAlphaResult alphaStatus = CheckCLUTAlpha((const uint8_t *)clutBufRaw_, clutFormat, clutTotalColors);
gstate_c.SetTextureFullAlpha(alphaStatus == CHECKALPHA_FULL);
draw_->Invalidate(InvalidationFlags::CACHED_RENDER_STATE);
shaderManager_->DirtyLastShader();
} else {
framebufferManager_->RebindFramebuffer("ApplyTextureFramebuffer");
framebufferManager_->BindFramebufferAsColorTexture(0, framebuffer, BINDFBCOLOR_MAY_COPY_WITH_UV | BINDFBCOLOR_APPLY_TEX_OFFSET, Draw::ALL_LAYERS);
BoundFramebufferTexture();
gstate_c.SetUseShaderDepal(ShaderDepalMode::OFF);
gstate_c.SetTextureFullAlpha(gstate.getTextureFormat() == GE_TFMT_5650);
}
SamplerCacheKey samplerKey = GetFramebufferSamplingParams(framebuffer->bufferWidth, framebuffer->bufferHeight);
ApplySamplingParams(samplerKey);
gstate_c.Dirty(DIRTY_ALL_RENDER_STATE);
}
void TextureCacheCommon::ApplyTextureDepal(TexCacheEntry *entry) {
uint32_t clutMode = gstate.clutformat & 0xFFFFFF;
switch (entry->format) {
case GE_TFMT_CLUT4:
case GE_TFMT_CLUT8:
break;
default:
_dbg_assert_(false);
return;
}
const GEPaletteFormat clutFormat = gstate.getClutPaletteFormat();
u32 depthUpperBits = 0;
GEBufferFormat expectedCLUTBufferFormat = (GEBufferFormat)clutFormat;
float scaleFactorX = 1.0f;
Draw2DPipeline *reinterpret = framebufferManager_->GetReinterpretPipeline(clutRenderFormat_, expectedCLUTBufferFormat, &scaleFactorX);
framebufferManager_->BlitUsingRaster(
dynamicClutTemp_, 0.0f, 0.0f, 512.0f, 1.0f, dynamicClutFbo_, 0.0f, 0.0f, scaleFactorX * 512.0f, 1.0f, false, 1.0f, reinterpret, "reinterpret_clut");
Draw2DPipeline *textureShader = textureShaderCache_->GetDepalettizeShader(clutMode, GE_TFMT_CLUT8, GE_FORMAT_CLUT8, false, 0);
gstate_c.SetUseShaderDepal(ShaderDepalMode::OFF);
int texWidth = gstate.getTextureWidth(0);
int texHeight = gstate.getTextureHeight(0);
const KnownVertexBounds &bounds = gstate_c.vertBounds;
float u1 = 0.0f;
float v1 = 0.0f;
float u2 = texWidth;
float v2 = texHeight;
if (bounds.minV < bounds.maxV) {
u1 = bounds.minU + gstate_c.curTextureXOffset;
v1 = bounds.minV + gstate_c.curTextureYOffset;
u2 = bounds.maxU + gstate_c.curTextureXOffset + 1.0f;
v2 = bounds.maxV + gstate_c.curTextureYOffset + 1.0f;
gstate_c.Dirty(DIRTY_TEXTURE_PARAMS);
}
Draw::Framebuffer *depalFBO = framebufferManager_->GetTempFBO(TempFBO::DEPAL, texWidth, texHeight);
draw_->BindTexture(0, nullptr);
draw_->BindTexture(1, nullptr);
draw_->BindFramebufferAsRenderTarget(depalFBO, { Draw::RPAction::DONT_CARE, Draw::RPAction::DONT_CARE, Draw::RPAction::DONT_CARE }, "Depal");
draw_->InvalidateFramebuffer(Draw::FB_INVALIDATION_STORE, Draw::Aspect::DEPTH_BIT | Draw::Aspect::STENCIL_BIT);
draw_->SetScissorRect(u1, v1, u2 - u1, v2 - v1);
Draw::Viewport viewport{ 0.0f, 0.0f, (float)texWidth, (float)texHeight, 0.0f, 1.0f };
draw_->SetViewport(viewport);
draw_->BindNativeTexture(0, GetNativeTextureView(entry, false));
draw_->BindFramebufferAsTexture(dynamicClutFbo_, 1, Draw::Aspect::COLOR_BIT, 0);
Draw::SamplerState *nearest = textureShaderCache_->GetSampler(false);
Draw::SamplerState *clutSampler = textureShaderCache_->GetSampler(false);
draw_->BindSamplerStates(0, 1, &nearest);
draw_->BindSamplerStates(1, 1, &clutSampler);
draw2D_->Blit(textureShader, u1, v1, u2, v2, u1, v1, u2, v2, texWidth, texHeight, texWidth, texHeight, false, 1);
gpuStats.numDepal++;
gstate_c.curTextureWidth = texWidth;
gstate_c.Dirty(DIRTY_UVSCALEOFFSET);
draw_->BindTexture(0, nullptr);
framebufferManager_->RebindFramebuffer("ApplyTextureFramebuffer");
draw_->BindFramebufferAsTexture(depalFBO, 0, Draw::Aspect::COLOR_BIT, 0);
BoundFramebufferTexture();
const u32 bytesPerColor = clutFormat == GE_CMODE_32BIT_ABGR8888 ? sizeof(u32) : sizeof(u16);
const u32 clutTotalColors = clutMaxBytes_ / bytesPerColor;
gstate_c.SetTextureFullAlpha(false);
draw_->Invalidate(InvalidationFlags::CACHED_RENDER_STATE);
shaderManager_->DirtyLastShader();
SamplerCacheKey samplerKey = GetFramebufferSamplingParams(texWidth, texHeight);
ApplySamplingParams(samplerKey);
gstate_c.Dirty(DIRTY_ALL_RENDER_STATE);
}
void TextureCacheCommon::Clear(bool delete_them) {
textureShaderCache_->Clear();
for (TexCache::iterator iter = cache_.begin(); iter != cache_.end(); ++iter) {
ReleaseTexture(iter->second.get(), delete_them);
}
for (TexCache::iterator iter = secondCache_.begin(); iter != secondCache_.end(); ++iter) {
ReleaseTexture(iter->second.get(), delete_them);
}
if (cache_.size() + secondCache_.size()) {
INFO_LOG(Log::G3D, "Texture cached cleared from %i textures", (int)(cache_.size() + secondCache_.size()));
cache_.clear();
secondCache_.clear();
cacheSizeEstimate_ = 0;
secondCacheSizeEstimate_ = 0;
}
videos_.clear();
if (dynamicClutFbo_) {
dynamicClutFbo_->Release();
dynamicClutFbo_ = nullptr;
}
if (dynamicClutTemp_) {
dynamicClutTemp_->Release();
dynamicClutTemp_ = nullptr;
}
}
void TextureCacheCommon::DeleteTexture(TexCache::iterator it) {
ReleaseTexture(it->second.get(), true);
cacheSizeEstimate_ -= EstimateTexMemoryUsage(it->second.get());
cache_.erase(it);
}
bool TextureCacheCommon::CheckFullHash(TexCacheEntry *entry, bool &doDelete) {
int w = gstate.getTextureWidth(0);
int h = gstate.getTextureHeight(0);
bool isVideo = IsVideo(entry->addr);
bool swizzled = gstate.isTextureSwizzled();
if (isVideo && g_Config.bTextureBackoffCache) {
entry->fullhash = (entry->fullhash + 0xA535A535) * 11 + (entry->fullhash & 4);
return false;
}
u32 fullhash;
{
PROFILE_THIS_SCOPE("texhash");
fullhash = QuickTexHash(replacer_, entry->addr, entry->bufw, w, h, swizzled, GETextureFormat(entry->format), entry);
}
if (fullhash == entry->fullhash) {
if (g_Config.bTextureBackoffCache && !isVideo) {
if (entry->GetHashStatus() != TexCacheEntry::STATUS_HASHING && entry->numFrames > TexCacheEntry::FRAMES_REGAIN_TRUST) {
entry->SetHashStatus(TexCacheEntry::STATUS_HASHING);
entry->status &= ~TexCacheEntry::STATUS_CHANGE_FREQUENT;
}
} else if (entry->numFrames > TEXCACHE_FRAME_CHANGE_FREQUENT_REGAIN_TRUST) {
entry->status &= ~TexCacheEntry::STATUS_CHANGE_FREQUENT;
}
return true;
}
if (PSP_CoreParameter().compat.flags().SecondaryTextureCache) {
entry->status |= TexCacheEntry::STATUS_UNRELIABLE;
if (entry->numInvalidated > 2 && entry->numInvalidated < 128 && !lowMemoryMode_) {
u64 secondKey = fullhash | (u64)entry->cluthash << 32;
TexCache::iterator secondIter = secondCache_.find(secondKey);
if (secondIter != secondCache_.end()) {
TexCacheEntry *secondEntry = secondIter->second.get();
if (secondEntry->Matches(entry->dim, entry->format, entry->maxLevel)) {
if (entry->numInvalidated > 8) {
--entry->numInvalidated;
}
nextTexture_ = secondEntry;
return true;
}
} else {
secondKey = entry->fullhash | ((u64)entry->cluthash << 32);
secondCacheSizeEstimate_ += EstimateTexMemoryUsage(entry);
auto oldIter = secondCache_.find(secondKey);
if (oldIter != secondCache_.end()) {
ReleaseTexture(oldIter->second.get(), true);
}
secondCache_[secondKey].reset(new TexCacheEntry(*entry));
entry->texturePtr = nullptr;
doDelete = false;
}
}
}
entry->fullhash = fullhash;
return false;
}
void TextureCacheCommon::Invalidate(u32 addr, int size, GPUInvalidationType type) {
const int LARGEST_TEXTURE_SIZE = 512 * 512 * 4;
addr &= 0x3FFFFFFF;
const u32 addr_end = addr + size;
if (type == GPU_INVALIDATE_ALL) {
gstate_c.Dirty(DIRTY_TEXTURE_IMAGE);
} else {
const u32 currentAddr = gstate.getTextureAddress(0);
if (addr_end >= currentAddr && addr < currentAddr + LARGEST_TEXTURE_SIZE) {
gstate_c.Dirty(DIRTY_TEXTURE_IMAGE);
}
}
if (!g_Config.bTextureBackoffCache && type != GPU_INVALIDATE_FORCE) {
return;
}
const u64 startKey = (u64)(addr - LARGEST_TEXTURE_SIZE) << 32;
u64 endKey = (u64)(addr + size + LARGEST_TEXTURE_SIZE) << 32;
if (endKey < startKey) {
endKey = (u64)-1;
}
for (TexCache::iterator iter = cache_.lower_bound(startKey), end = cache_.upper_bound(endKey); iter != end; ++iter) {
auto &entry = iter->second;
u32 texAddr = entry->addr;
u32 texEnd = entry->addr + entry->SizeInRAM() / 2;
if (addr < texEnd && addr_end > texAddr) {
if (entry->GetHashStatus() == TexCacheEntry::STATUS_RELIABLE) {
entry->SetHashStatus(TexCacheEntry::STATUS_HASHING);
}
if (type == GPU_INVALIDATE_FORCE) {
entry->fullhash = (entry->fullhash ^ 0x12345678) + 13;
entry->minihash = (entry->minihash ^ 0x89ABCDEF) + 89;
}
if (type != GPU_INVALIDATE_ALL) {
gpuStats.numTextureInvalidations++;
entry->numFrames = type == GPU_INVALIDATE_SAFE ? 256 : 0;
if (type == GPU_INVALIDATE_SAFE) {
u32 diff = gpuStats.numFlips - entry->lastFrame;
if (diff < TEXCACHE_FRAME_CHANGE_FREQUENT) {
entry->status |= TexCacheEntry::STATUS_CHANGE_FREQUENT;
}
}
entry->framesUntilNextFullHash = 0;
} else {
entry->invalidHint++;
}
}
}
}
void TextureCacheCommon::InvalidateAll(GPUInvalidationType ) {
if (!g_Config.bTextureBackoffCache) {
return;
}
if (timesInvalidatedAllThisFrame_ > 5) {
return;
}
timesInvalidatedAllThisFrame_++;
for (TexCache::iterator iter = cache_.begin(), end = cache_.end(); iter != end; ++iter) {
if (iter->second->GetHashStatus() == TexCacheEntry::STATUS_RELIABLE) {
iter->second->SetHashStatus(TexCacheEntry::STATUS_HASHING);
}
iter->second->invalidHint++;
}
}
void TextureCacheCommon::ClearNextFrame() {
clearCacheNextFrame_ = true;
}
std::string AttachCandidate::ToString() const {
return StringFromFormat("[%s seq:%d rel:%d C:%08x/%d(%s) Z:%08x/%d X:%d Y:%d reint: %s]",
RasterChannelToString(this->channel),
this->channel == RASTER_COLOR ? this->fb->colorBindSeq : this->fb->depthBindSeq,
this->relevancy,
this->fb->fb_address, this->fb->fb_stride, GeBufferFormatToString(this->fb->fb_format),
this->fb->z_address, this->fb->z_stride,
this->match.xOffset, this->match.yOffset, this->match.reinterpret ? "true" : "false");
}
bool TextureCacheCommon::PrepareBuildTexture(BuildTexturePlan &plan, TexCacheEntry *entry) {
gpuStats.numTexturesDecoded++;
cacheSizeEstimate_ += EstimateTexMemoryUsage(entry);
plan.badMipSizes = false;
plan.levelsToLoad = entry->maxLevel + 1;
for (int i = 0; i < plan.levelsToLoad; i++) {
u32 levelTexaddr = gstate.getTextureAddress(i);
if (!Memory::IsValidAddress(levelTexaddr)) {
plan.levelsToLoad = i;
break;
}
int tw = gstate.getTextureWidth(i);
int th = gstate.getTextureHeight(i);
if (tw == 1 || th == 1) {
plan.levelsToLoad = i + 1;
break;
}
if (i > 0) {
int lastW = gstate.getTextureWidth(i - 1);
int lastH = gstate.getTextureHeight(i - 1);
if (gstate_c.Use(GPU_USE_TEXTURE_LOD_CONTROL)) {
if (tw != 1 && tw != (lastW >> 1))
plan.badMipSizes = true;
else if (th != 1 && th != (lastH >> 1))
plan.badMipSizes = true;
}
}
}
plan.scaleFactor = standardScaleFactor_;
plan.depth = 1;
if (lowMemoryMode_ && !plan.hardwareScaling) {
plan.scaleFactor = plan.scaleFactor > 4 ? 4 : (plan.scaleFactor > 2 ? 2 : 1);
}
bool isFakeMipmapChange = false;
if (plan.badMipSizes) {
isFakeMipmapChange = IsFakeMipmapChange();
int tw = gstate.getTextureWidth(0);
int th = gstate.getTextureHeight(0);
bool pure3D = true;
for (int i = 0; i < plan.levelsToLoad; i++) {
if (gstate.getTextureWidth(i) != gstate.getTextureWidth(0) || gstate.getTextureHeight(i) != gstate.getTextureHeight(0)) {
pure3D = false;
break;
}
}
if (pure3D && plan.levelsToLoad == 2 && gstate.getTextureAddress(0) == gstate.getTextureAddress(1)) {
isFakeMipmapChange = false;
pure3D = false;
} else if (isFakeMipmapChange) {
pure3D = false;
}
if (pure3D && draw_->GetDeviceCaps().texture3DSupported) {
plan.depth = plan.levelsToLoad;
plan.scaleFactor = 1;
}
plan.levelsToLoad = 1;
plan.levelsToCreate = 1;
}
if (plan.hardwareScaling) {
plan.scaleFactor = shaderScaleFactor_;
}
plan.levelsToCreate = plan.levelsToLoad;
plan.w = gstate.getTextureWidth(0);
plan.h = gstate.getTextureHeight(0);
bool isPPGETexture = entry->addr >= PSP_GetKernelMemoryBase() && entry->addr < PSP_GetKernelMemoryEnd();
if (isPPGETexture) {
plan.scaleFactor = 1;
} else if (!g_DoubleTextureCoordinates) {
if (plan.w > 2048 || plan.h > 2048) {
ERROR_LOG(Log::G3D, "Bad texture dimensions: %dx%d", plan.w, plan.h);
return false;
}
}
if (PSP_CoreParameter().compat.flags().ForceLowerResolutionForEffectsOn && gstate.FrameBufStride() < 0x1E0) {
plan.scaleFactor = 1;
}
if ((entry->status & TexCacheEntry::STATUS_CHANGE_FREQUENT) != 0 && plan.scaleFactor != 1 && plan.slowScaler) {
entry->status |= TexCacheEntry::STATUS_TO_SCALE;
plan.scaleFactor = 1;
}
if (plan.scaleFactor != 1) {
if (texelsScaledThisFrame_ >= TEXCACHE_MAX_TEXELS_SCALED && plan.slowScaler) {
entry->status |= TexCacheEntry::STATUS_TO_SCALE;
plan.scaleFactor = 1;
} else {
entry->status &= ~TexCacheEntry::STATUS_TO_SCALE;
entry->status |= TexCacheEntry::STATUS_IS_SCALED_OR_REPLACED;
texelsScaledThisFrame_ += plan.w * plan.h;
}
}
plan.isVideo = IsVideo(entry->addr);
if (plan.scaleFactor > 1) {
plan.levelsToLoad = 1;
bool enableVideoUpscaling = false;
if (!enableVideoUpscaling && plan.isVideo) {
plan.scaleFactor = 1;
plan.levelsToCreate = 1;
}
}
bool canReplace = !isPPGETexture;
if (entry->status & TexCacheEntry::TexStatus::STATUS_CLUT_GPU) {
_dbg_assert_(entry->format == GE_TFMT_CLUT4 || entry->format == GE_TFMT_CLUT8);
plan.decodeToClut8 = true;
plan.levelsToCreate = 1;
plan.levelsToLoad = 1;
plan.maxPossibleLevels = 1;
plan.scaleFactor = 1;
plan.saveTexture = false;
canReplace = false;
} else {
plan.decodeToClut8 = false;
}
if (canReplace) {
plan.replaced = FindReplacement(entry, &plan.w, &plan.h, &plan.depth);
plan.doReplace = plan.replaced ? plan.replaced->State() == ReplacementState::ACTIVE : false;
} else {
plan.replaced = nullptr;
plan.doReplace = false;
}
plan.saveTexture = false;
if (plan.doReplace) {
plan.scaleFactor = 1;
plan.levelsToLoad = plan.replaced->NumLevels();
plan.levelsToCreate = plan.levelsToLoad;
plan.badMipSizes = false;
plan.replaced->GetSize(0, &plan.createW, &plan.createH);
} else {
if (replacer_.SaveEnabled() && !plan.doReplace && plan.depth == 1 && canReplace) {
ReplacedTextureDecodeInfo replacedInfo;
replacedInfo.cachekey = entry->CacheKey();
replacedInfo.hash = entry->fullhash;
replacedInfo.addr = entry->addr;
replacedInfo.isFinal = (entry->status & TexCacheEntry::STATUS_TO_SCALE) == 0;
replacedInfo.isVideo = plan.isVideo;
replacedInfo.fmt = Draw::DataFormat::R8G8B8A8_UNORM;
plan.saveTexture = replacer_.WillSave(replacedInfo);
}
plan.createW = plan.w * plan.scaleFactor;
plan.createH = plan.h * plan.scaleFactor;
}
plan.baseLevelSrc = 0;
if (isFakeMipmapChange) {
plan.baseLevelSrc = std::max(0, gstate.getTexLevelOffset16() / 16);
if ((plan.baseLevelSrc & 1) && gstate.getTextureAddress(plan.baseLevelSrc) == gstate.getTextureAddress(plan.baseLevelSrc & ~1)) {
plan.baseLevelSrc &= ~1;
}
plan.levelsToCreate = 1;
plan.levelsToLoad = 1;
_dbg_assert_(plan.depth == 1);
}
if (plan.isVideo || plan.depth != 1 || plan.decodeToClut8) {
plan.levelsToLoad = 1;
plan.maxPossibleLevels = 1;
} else {
plan.maxPossibleLevels = log2i(std::max(plan.createW, plan.createH)) + 1;
}
if (plan.levelsToCreate == 1) {
entry->status |= TexCacheEntry::STATUS_NO_MIPS;
} else {
entry->status &= ~TexCacheEntry::STATUS_NO_MIPS;
}
entry->status &= ~TexCacheEntry::STATUS_ALPHA_MASK;
return true;
}
void TextureCacheCommon::LoadTextureLevel(TexCacheEntry &entry, uint8_t *data, size_t dataSize, int stride, BuildTexturePlan &plan, int srcLevel, Draw::DataFormat dstFmt, TexDecodeFlags texDecFlags) {
int w = gstate.getTextureWidth(srcLevel);
int h = gstate.getTextureHeight(srcLevel);
PROFILE_THIS_SCOPE("decodetex");
if (plan.doReplace) {
plan.replaced->GetSize(srcLevel, &w, &h);
double replaceStart = time_now_d();
plan.replaced->CopyLevelTo(srcLevel, data, dataSize, stride);
replacementTimeThisFrame_ += time_now_d() - replaceStart;
} else {
GETextureFormat tfmt = (GETextureFormat)entry.format;
GEPaletteFormat clutformat = gstate.getClutPaletteFormat();
u32 texaddr = gstate.getTextureAddress(srcLevel);
const int bufw = GetTextureBufw(srcLevel, texaddr, tfmt);
u32 *pixelData;
int decPitch;
if (plan.scaleFactor > 1) {
tmpTexBufRearrange_.resize(std::max(bufw, w) * h);
pixelData = tmpTexBufRearrange_.data();
decPitch = w * 4;
} else {
pixelData = (u32 *)data;
decPitch = stride;
}
if (!gstate_c.Use(GPU_USE_16BIT_FORMATS) || dstFmt == Draw::DataFormat::R8G8B8A8_UNORM) {
texDecFlags |= TexDecodeFlags::EXPAND32;
}
if (entry.status & TexCacheEntry::STATUS_CLUT_GPU) {
texDecFlags |= TexDecodeFlags::TO_CLUT8;
}
CheckAlphaResult alphaResult = DecodeTextureLevel((u8 *)pixelData, decPitch, tfmt, clutformat, texaddr, srcLevel, bufw, texDecFlags);
entry.SetAlphaStatus(alphaResult, srcLevel);
int scaledW = w, scaledH = h;
if (plan.scaleFactor > 1) {
scaler_.ScaleAlways((u32 *)data, pixelData, w, h, &scaledW, &scaledH, plan.scaleFactor);
pixelData = (u32 *)data;
decPitch = scaledW * sizeof(u32);
if (decPitch != stride) {
for (int y = scaledH - 1; y >= 0; --y) {
memcpy((u8 *)data + stride * y, (u8 *)data + decPitch * y, scaledW *4);
}
decPitch = stride;
}
}
if (plan.saveTexture && !lowMemoryMode_) {
ReplacedTextureDecodeInfo replacedInfo;
replacedInfo.cachekey = entry.CacheKey();
replacedInfo.hash = entry.fullhash;
replacedInfo.addr = entry.addr;
replacedInfo.isVideo = IsVideo(entry.addr);
replacedInfo.isFinal = (entry.status & TexCacheEntry::STATUS_TO_SCALE) == 0;
replacedInfo.fmt = dstFmt;
replacer_.NotifyTextureDecoded(plan.replaced, replacedInfo, pixelData, decPitch, srcLevel, w, h, scaledW, scaledH);
}
}
}
CheckAlphaResult TextureCacheCommon::CheckCLUTAlpha(const uint8_t *pixelData, GEPaletteFormat clutFormat, int w) {
switch (clutFormat) {
case GE_CMODE_16BIT_ABGR4444:
return CheckAlpha16((const u16 *)pixelData, w, 0xF000);
case GE_CMODE_16BIT_ABGR5551:
return CheckAlpha16((const u16 *)pixelData, w, 0x8000);
case GE_CMODE_16BIT_BGR5650:
return CHECKALPHA_FULL;
default:
return CheckAlpha32((const u32 *)pixelData, w, 0xFF000000);
}
}