2013-08-17 11:23:51 +02:00
|
|
|
// Copyright (c) 2012- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <algorithm>
|
2016-06-25 09:13:14 -07:00
|
|
|
#include <cassert>
|
2013-12-29 15:55:09 -08:00
|
|
|
#include <cstring>
|
2013-08-17 11:23:51 +02:00
|
|
|
|
|
|
|
#include "Core/MemMap.h"
|
|
|
|
#include "Core/Reporting.h"
|
|
|
|
#include "GPU/ge_constants.h"
|
|
|
|
#include "GPU/GPUState.h"
|
2014-09-09 00:53:01 -07:00
|
|
|
#include "GPU/Directx9/PixelShaderGeneratorDX9.h"
|
2013-09-15 12:46:14 +02:00
|
|
|
#include "GPU/Directx9/TextureCacheDX9.h"
|
|
|
|
#include "GPU/Directx9/FramebufferDX9.h"
|
2014-09-17 23:26:20 +02:00
|
|
|
#include "GPU/Directx9/ShaderManagerDX9.h"
|
|
|
|
#include "GPU/Directx9/DepalettizeShaderDX9.h"
|
2017-02-05 20:54:24 +01:00
|
|
|
#include "gfx/d3d9_state.h"
|
2014-09-09 08:12:42 -07:00
|
|
|
#include "GPU/Common/FramebufferCommon.h"
|
2013-09-15 21:27:13 -07:00
|
|
|
#include "GPU/Common/TextureDecoder.h"
|
2013-08-17 11:23:51 +02:00
|
|
|
#include "Core/Config.h"
|
2013-12-29 15:55:09 -08:00
|
|
|
#include "Core/Host.h"
|
2013-08-17 11:23:51 +02:00
|
|
|
|
2013-08-19 20:36:43 +02:00
|
|
|
#include "ext/xxhash.h"
|
2013-09-15 12:46:14 +02:00
|
|
|
#include "math/math_util.h"
|
2013-08-17 11:23:51 +02:00
|
|
|
|
2014-08-23 01:52:46 +02:00
|
|
|
|
2013-09-15 08:53:21 -07:00
|
|
|
namespace DX9 {
|
|
|
|
|
2013-08-19 20:36:43 +02:00
|
|
|
#define INVALID_TEX (LPDIRECT3DTEXTURE9)(-1)
|
2013-08-17 11:23:51 +02:00
|
|
|
|
|
|
|
// If a texture hasn't been seen for this many frames, get rid of it.
|
|
|
|
#define TEXTURE_KILL_AGE 200
|
|
|
|
#define TEXTURE_KILL_AGE_LOWMEM 60
|
|
|
|
// Not used in lowmem mode.
|
|
|
|
#define TEXTURE_SECOND_KILL_AGE 100
|
|
|
|
|
2013-08-19 20:36:43 +02:00
|
|
|
// Try to be prime to other decimation intervals.
|
|
|
|
#define TEXCACHE_DECIMATION_INTERVAL 13
|
|
|
|
|
2014-09-09 00:53:01 -07:00
|
|
|
#define TEXCACHE_MAX_TEXELS_SCALED (256*256) // Per frame
|
|
|
|
|
2014-12-21 16:54:26 -08:00
|
|
|
#define TEXCACHE_MIN_PRESSURE 16 * 1024 * 1024 // Total in VRAM
|
|
|
|
#define TEXCACHE_SECOND_MIN_PRESSURE 4 * 1024 * 1024
|
|
|
|
|
2017-02-05 20:13:28 +01:00
|
|
|
static const D3DVERTEXELEMENT9 g_FramebufferVertexElements[] = {
|
|
|
|
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
|
|
|
|
{ 0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
|
|
|
|
D3DDECL_END()
|
|
|
|
};
|
|
|
|
|
2017-02-05 19:51:50 +01:00
|
|
|
TextureCacheDX9::TextureCacheDX9(Draw::DrawContext *draw)
|
2017-02-08 15:24:33 +01:00
|
|
|
: TextureCacheCommon(draw) {
|
2013-08-19 20:36:43 +02:00
|
|
|
lastBoundTexture = INVALID_TEX;
|
|
|
|
decimationCounter_ = TEXCACHE_DECIMATION_INTERVAL;
|
2014-09-09 00:53:01 -07:00
|
|
|
|
2014-09-20 22:39:01 -07:00
|
|
|
D3DCAPS9 pCaps;
|
|
|
|
ZeroMemory(&pCaps, sizeof(pCaps));
|
|
|
|
HRESULT result = 0;
|
|
|
|
if (pD3DdeviceEx) {
|
|
|
|
result = pD3DdeviceEx->GetDeviceCaps(&pCaps);
|
|
|
|
} else {
|
|
|
|
result = pD3Ddevice->GetDeviceCaps(&pCaps);
|
|
|
|
}
|
|
|
|
if (FAILED(result)) {
|
|
|
|
WARN_LOG(G3D, "Failed to get the device caps!");
|
|
|
|
maxAnisotropyLevel = 16;
|
|
|
|
} else {
|
|
|
|
maxAnisotropyLevel = pCaps.MaxAnisotropy;
|
|
|
|
}
|
2014-03-22 21:35:16 -07:00
|
|
|
SetupTextureDecoder();
|
2015-03-15 20:05:35 -07:00
|
|
|
|
|
|
|
nextTexture_ = nullptr;
|
2017-02-05 20:13:28 +01:00
|
|
|
pD3Ddevice->CreateVertexDeclaration(g_FramebufferVertexElements, &pFramebufferVertexDecl);
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
|
|
|
|
2013-09-15 12:46:14 +02:00
|
|
|
TextureCacheDX9::~TextureCacheDX9() {
|
2017-02-05 20:13:28 +01:00
|
|
|
pFramebufferVertexDecl->Release();
|
2015-01-24 20:36:01 -08:00
|
|
|
Clear(true);
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
|
|
|
|
2017-02-08 15:58:46 +01:00
|
|
|
void TextureCacheDX9::SetFramebufferManager(FramebufferManagerDX9 *fbManager) {
|
|
|
|
framebufferManagerDX9_ = fbManager;
|
|
|
|
framebufferManager_ = fbManager;
|
|
|
|
}
|
|
|
|
|
2013-09-15 12:46:14 +02:00
|
|
|
void TextureCacheDX9::Clear(bool delete_them) {
|
2013-08-17 11:23:51 +02:00
|
|
|
pD3Ddevice->SetTexture(0, NULL);
|
2013-08-19 20:36:43 +02:00
|
|
|
lastBoundTexture = INVALID_TEX;
|
2013-08-17 11:23:51 +02:00
|
|
|
if (delete_them) {
|
|
|
|
for (TexCache::iterator iter = cache.begin(); iter != cache.end(); ++iter) {
|
2015-03-15 19:46:36 -07:00
|
|
|
DEBUG_LOG(G3D, "Deleting texture %p", iter->second.texturePtr);
|
|
|
|
ReleaseTexture(&iter->second);
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
|
|
|
for (TexCache::iterator iter = secondCache.begin(); iter != secondCache.end(); ++iter) {
|
2015-03-15 19:46:36 -07:00
|
|
|
DEBUG_LOG(G3D, "Deleting texture %p", iter->second.texturePtr);
|
|
|
|
ReleaseTexture(&iter->second);
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cache.size() + secondCache.size()) {
|
|
|
|
INFO_LOG(G3D, "Texture cached cleared from %i textures", (int)(cache.size() + secondCache.size()));
|
|
|
|
cache.clear();
|
|
|
|
secondCache.clear();
|
2014-12-21 16:54:26 -08:00
|
|
|
cacheSizeEstimate_ = 0;
|
|
|
|
secondCacheSizeEstimate_ = 0;
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
2014-09-09 00:53:01 -07:00
|
|
|
fbTexInfo_.clear();
|
2016-05-01 08:39:18 -07:00
|
|
|
videos_.clear();
|
2014-09-09 00:53:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCacheDX9::DeleteTexture(TexCache::iterator it) {
|
2015-03-15 19:46:36 -07:00
|
|
|
ReleaseTexture(&it->second);
|
2016-03-26 12:07:49 -07:00
|
|
|
auto fbInfo = fbTexInfo_.find(it->first);
|
2014-09-09 00:53:01 -07:00
|
|
|
if (fbInfo != fbTexInfo_.end()) {
|
|
|
|
fbTexInfo_.erase(fbInfo);
|
|
|
|
}
|
2014-12-21 16:54:26 -08:00
|
|
|
|
|
|
|
cacheSizeEstimate_ -= EstimateTexMemoryUsage(&it->second);
|
2014-09-09 00:53:01 -07:00
|
|
|
cache.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCacheDX9::ForgetLastTexture() {
|
|
|
|
lastBoundTexture = INVALID_TEX;
|
2017-01-24 09:41:38 +01:00
|
|
|
gstate_c.Dirty(DIRTY_TEXTURE_PARAMS);
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Removes old textures.
|
2013-09-15 12:46:14 +02:00
|
|
|
void TextureCacheDX9::Decimate() {
|
2013-08-19 20:36:43 +02:00
|
|
|
if (--decimationCounter_ <= 0) {
|
|
|
|
decimationCounter_ = TEXCACHE_DECIMATION_INTERVAL;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-21 16:54:26 -08:00
|
|
|
if (cacheSizeEstimate_ >= TEXCACHE_MIN_PRESSURE) {
|
|
|
|
const u32 had = cacheSizeEstimate_;
|
|
|
|
|
|
|
|
pD3Ddevice->SetTexture(0, NULL);
|
|
|
|
lastBoundTexture = INVALID_TEX;
|
|
|
|
int killAge = lowMemoryMode_ ? TEXTURE_KILL_AGE_LOWMEM : TEXTURE_KILL_AGE;
|
|
|
|
for (TexCache::iterator iter = cache.begin(); iter != cache.end(); ) {
|
|
|
|
if (iter->second.lastFrame + killAge < gpuStats.numFlips) {
|
|
|
|
DeleteTexture(iter++);
|
|
|
|
} else {
|
|
|
|
++iter;
|
|
|
|
}
|
2013-12-29 15:10:07 -08:00
|
|
|
}
|
2014-12-21 16:54:26 -08:00
|
|
|
|
|
|
|
VERBOSE_LOG(G3D, "Decimated texture cache, saved %d estimated bytes - now %d bytes", had - cacheSizeEstimate_, cacheSizeEstimate_);
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
2013-12-29 15:10:07 -08:00
|
|
|
|
2014-12-21 16:54:26 -08:00
|
|
|
if (g_Config.bTextureSecondaryCache && secondCacheSizeEstimate_ >= TEXCACHE_SECOND_MIN_PRESSURE) {
|
|
|
|
const u32 had = secondCacheSizeEstimate_;
|
|
|
|
|
2013-12-29 15:10:07 -08:00
|
|
|
for (TexCache::iterator iter = secondCache.begin(); iter != secondCache.end(); ) {
|
|
|
|
// In low memory mode, we kill them all.
|
|
|
|
if (lowMemoryMode_ || iter->second.lastFrame + TEXTURE_KILL_AGE < gpuStats.numFlips) {
|
2015-03-15 19:46:36 -07:00
|
|
|
ReleaseTexture(&iter->second);
|
2013-12-29 15:10:07 -08:00
|
|
|
secondCache.erase(iter++);
|
|
|
|
} else {
|
|
|
|
++iter;
|
|
|
|
}
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
2014-12-21 16:54:26 -08:00
|
|
|
|
|
|
|
VERBOSE_LOG(G3D, "Decimated second texture cache, saved %d estimated bytes - now %d bytes", had - secondCacheSizeEstimate_, secondCacheSizeEstimate_);
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
2016-05-01 08:39:18 -07:00
|
|
|
|
|
|
|
DecimateVideos();
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
D3DFORMAT getClutDestFormat(GEPaletteFormat format) {
|
|
|
|
switch (format) {
|
|
|
|
case GE_CMODE_16BIT_ABGR4444:
|
|
|
|
return D3DFMT_A4R4G4B4;
|
|
|
|
case GE_CMODE_16BIT_ABGR5551:
|
|
|
|
return D3DFMT_A1R5G5B5;
|
|
|
|
case GE_CMODE_16BIT_BGR5650:
|
|
|
|
return D3DFMT_R5G6B5;
|
|
|
|
case GE_CMODE_32BIT_ABGR8888:
|
|
|
|
return D3DFMT_A8R8G8B8;
|
|
|
|
}
|
|
|
|
// Should never be here !
|
|
|
|
return D3DFMT_A8R8G8B8;
|
|
|
|
}
|
|
|
|
|
2014-08-23 01:52:46 +02:00
|
|
|
static const u8 MinFilt[8] = {
|
2013-08-17 11:23:51 +02:00
|
|
|
D3DTEXF_POINT,
|
|
|
|
D3DTEXF_LINEAR,
|
|
|
|
D3DTEXF_POINT,
|
|
|
|
D3DTEXF_LINEAR,
|
|
|
|
D3DTEXF_POINT, // GL_NEAREST_MIPMAP_NEAREST,
|
|
|
|
D3DTEXF_LINEAR, // GL_LINEAR_MIPMAP_NEAREST,
|
|
|
|
D3DTEXF_POINT, // GL_NEAREST_MIPMAP_LINEAR,
|
|
|
|
D3DTEXF_LINEAR, // GL_LINEAR_MIPMAP_LINEAR,
|
|
|
|
};
|
|
|
|
|
2014-08-23 01:52:46 +02:00
|
|
|
static const u8 MipFilt[8] = {
|
2013-08-17 11:23:51 +02:00
|
|
|
D3DTEXF_POINT,
|
|
|
|
D3DTEXF_LINEAR,
|
|
|
|
D3DTEXF_POINT,
|
|
|
|
D3DTEXF_LINEAR,
|
|
|
|
D3DTEXF_POINT, // GL_NEAREST_MIPMAP_NEAREST,
|
|
|
|
D3DTEXF_POINT, // GL_LINEAR_MIPMAP_NEAREST,
|
|
|
|
D3DTEXF_LINEAR, // GL_NEAREST_MIPMAP_LINEAR,
|
|
|
|
D3DTEXF_LINEAR, // GL_LINEAR_MIPMAP_LINEAR,
|
|
|
|
};
|
|
|
|
|
2014-08-23 01:52:46 +02:00
|
|
|
static const u8 MagFilt[2] = {
|
2013-08-17 11:23:51 +02:00
|
|
|
D3DTEXF_POINT,
|
|
|
|
D3DTEXF_LINEAR
|
|
|
|
};
|
|
|
|
|
2014-09-09 00:53:01 -07:00
|
|
|
void TextureCacheDX9::UpdateSamplingParams(TexCacheEntry &entry, bool force) {
|
|
|
|
int minFilt;
|
|
|
|
int magFilt;
|
|
|
|
bool sClamp;
|
|
|
|
bool tClamp;
|
|
|
|
float lodBias;
|
2016-05-01 08:53:48 -07:00
|
|
|
GetSamplingParams(minFilt, magFilt, sClamp, tClamp, lodBias, entry.maxLevel, entry.addr);
|
2014-09-09 00:53:01 -07:00
|
|
|
|
|
|
|
if (entry.maxLevel != 0) {
|
2014-09-14 09:13:06 -07:00
|
|
|
GETexLevelMode mode = gstate.getTexLevelMode();
|
|
|
|
switch (mode) {
|
|
|
|
case GE_TEXLEVEL_MODE_AUTO:
|
|
|
|
// TODO
|
|
|
|
break;
|
|
|
|
case GE_TEXLEVEL_MODE_CONST:
|
|
|
|
dxstate.texMipLodBias.set(lodBias);
|
|
|
|
// TODO
|
|
|
|
break;
|
|
|
|
case GE_TEXLEVEL_MODE_SLOPE:
|
|
|
|
// TODO
|
|
|
|
break;
|
2014-09-09 00:53:01 -07:00
|
|
|
}
|
2014-09-14 09:13:06 -07:00
|
|
|
entry.lodBias = lodBias;
|
2014-09-09 00:53:01 -07:00
|
|
|
}
|
2013-08-17 11:23:51 +02:00
|
|
|
|
2014-09-20 10:32:43 +02:00
|
|
|
D3DTEXTUREFILTERTYPE minf = (D3DTEXTUREFILTERTYPE)MinFilt[minFilt];
|
|
|
|
D3DTEXTUREFILTERTYPE mipf = (D3DTEXTUREFILTERTYPE)MipFilt[minFilt];
|
2014-09-20 14:06:02 +02:00
|
|
|
D3DTEXTUREFILTERTYPE magf = (D3DTEXTUREFILTERTYPE)MagFilt[magFilt];
|
2014-09-20 10:32:43 +02:00
|
|
|
|
2016-03-17 21:56:04 -07:00
|
|
|
if (gstate_c.Supports(GPU_SUPPORTS_ANISOTROPY) && g_Config.iAnisotropyLevel > 0 && minf == D3DTEXF_LINEAR) {
|
2014-09-20 13:03:22 +02:00
|
|
|
minf = D3DTEXF_ANISOTROPIC;
|
2014-09-20 10:32:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dxstate.texMinFilter.set(minf);
|
|
|
|
dxstate.texMipFilter.set(mipf);
|
|
|
|
dxstate.texMagFilter.set(magf);
|
2014-08-23 01:52:46 +02:00
|
|
|
dxstate.texAddressU.set(sClamp ? D3DTADDRESS_CLAMP : D3DTADDRESS_WRAP);
|
|
|
|
dxstate.texAddressV.set(tClamp ? D3DTADDRESS_CLAMP : D3DTADDRESS_WRAP);
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
|
|
|
|
2014-09-09 00:53:01 -07:00
|
|
|
void TextureCacheDX9::SetFramebufferSamplingParams(u16 bufferWidth, u16 bufferHeight) {
|
|
|
|
int minFilt;
|
|
|
|
int magFilt;
|
|
|
|
bool sClamp;
|
|
|
|
bool tClamp;
|
|
|
|
float lodBias;
|
2016-05-01 08:53:48 -07:00
|
|
|
GetSamplingParams(minFilt, magFilt, sClamp, tClamp, lodBias, 0, 0);
|
2014-09-09 00:53:01 -07:00
|
|
|
|
|
|
|
dxstate.texMinFilter.set(MinFilt[minFilt]);
|
|
|
|
dxstate.texMipFilter.set(MipFilt[minFilt]);
|
|
|
|
dxstate.texMagFilter.set(MagFilt[magFilt]);
|
|
|
|
|
|
|
|
// Often the framebuffer will not match the texture size. We'll wrap/clamp in the shader in that case.
|
|
|
|
// This happens whether we have OES_texture_npot or not.
|
|
|
|
int w = gstate.getTextureWidth(0);
|
|
|
|
int h = gstate.getTextureHeight(0);
|
|
|
|
if (w != bufferWidth || h != bufferHeight) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dxstate.texAddressU.set(sClamp ? D3DTADDRESS_CLAMP : D3DTADDRESS_WRAP);
|
|
|
|
dxstate.texAddressV.set(tClamp ? D3DTADDRESS_CLAMP : D3DTADDRESS_WRAP);
|
|
|
|
}
|
|
|
|
|
2013-09-15 12:46:14 +02:00
|
|
|
void TextureCacheDX9::StartFrame() {
|
2013-09-04 11:19:36 +02:00
|
|
|
lastBoundTexture = INVALID_TEX;
|
2014-09-09 00:53:01 -07:00
|
|
|
timesInvalidatedAllThisFrame_ = 0;
|
|
|
|
|
|
|
|
if (texelsScaledThisFrame_) {
|
|
|
|
// INFO_LOG(G3D, "Scaled %i texels", texelsScaledThisFrame_);
|
|
|
|
}
|
|
|
|
texelsScaledThisFrame_ = 0;
|
2013-12-29 15:10:07 -08:00
|
|
|
if (clearCacheNextFrame_) {
|
2013-08-17 11:23:51 +02:00
|
|
|
Clear(true);
|
|
|
|
clearCacheNextFrame_ = false;
|
|
|
|
} else {
|
|
|
|
Decimate();
|
|
|
|
}
|
2014-09-20 10:32:43 +02:00
|
|
|
|
2016-03-17 21:56:04 -07:00
|
|
|
if (gstate_c.Supports(GPU_SUPPORTS_ANISOTROPY)) {
|
|
|
|
DWORD aniso = 1 << g_Config.iAnisotropyLevel;
|
|
|
|
DWORD anisotropyLevel = aniso > maxAnisotropyLevel ? maxAnisotropyLevel : aniso;
|
|
|
|
pD3Ddevice->SetSamplerState(0, D3DSAMP_MAXANISOTROPY, anisotropyLevel);
|
|
|
|
}
|
2014-09-20 10:32:43 +02:00
|
|
|
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 MiniHash(const u32 *ptr) {
|
|
|
|
return ptr[0];
|
|
|
|
}
|
|
|
|
|
2015-07-29 11:38:42 +02:00
|
|
|
void TextureCacheDX9::UpdateCurrentClut(GEPaletteFormat clutFormat, u32 clutBase, bool clutIndexIsSimple) {
|
2013-08-17 11:23:51 +02:00
|
|
|
const u32 clutBaseBytes = clutBase * (clutFormat == GE_CMODE_32BIT_ABGR8888 ? sizeof(u32) : sizeof(u16));
|
|
|
|
// Technically, these extra bytes weren't loaded, but hopefully it was loaded earlier.
|
|
|
|
// If not, we're going to hash random data, which hopefully doesn't cause a performance issue.
|
2015-04-26 00:43:36 -07:00
|
|
|
//
|
|
|
|
// TODO: Actually, this seems like a hack. The game can upload part of a CLUT and reference other data.
|
|
|
|
// clutTotalBytes_ is the last amount uploaded. We should hash clutMaxBytes_, but this will often hash
|
|
|
|
// unrelated old entries for small palettes.
|
|
|
|
// Adding clutBaseBytes may just be mitigating this for some usage patterns.
|
|
|
|
const u32 clutExtendedBytes = std::min(clutTotalBytes_ + clutBaseBytes, clutMaxBytes_);
|
2013-08-17 11:23:51 +02:00
|
|
|
|
2014-10-26 17:49:24 -07:00
|
|
|
clutHash_ = DoReliableHash32((const char *)clutBufRaw_, clutExtendedBytes, 0xC0108888);
|
2014-08-28 01:20:21 -07:00
|
|
|
clutBuf_ = clutBufRaw_;
|
2013-08-17 11:23:51 +02:00
|
|
|
|
|
|
|
// Special optimization: fonts typically draw clut4 with just alpha values in a single color.
|
|
|
|
clutAlphaLinear_ = false;
|
|
|
|
clutAlphaLinearColor_ = 0;
|
2015-07-29 11:38:42 +02:00
|
|
|
if (clutFormat == GE_CMODE_16BIT_ABGR4444 && clutIndexIsSimple) {
|
2014-09-09 00:53:01 -07:00
|
|
|
const u16_le *clut = GetCurrentClut<u16_le>();
|
2013-08-17 11:23:51 +02:00
|
|
|
clutAlphaLinear_ = true;
|
2014-09-13 15:13:34 +02:00
|
|
|
clutAlphaLinearColor_ = clut[15] & 0x0FFF;
|
2013-08-17 11:23:51 +02:00
|
|
|
for (int i = 0; i < 16; ++i) {
|
2016-01-01 09:02:16 -08:00
|
|
|
u16 step = clutAlphaLinearColor_ | (i << 12);
|
|
|
|
if (clut[i] != step) {
|
2013-08-17 11:23:51 +02:00
|
|
|
clutAlphaLinear_ = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clutLastFormat_ = gstate.clutformat;
|
|
|
|
}
|
|
|
|
|
2013-09-15 12:46:14 +02:00
|
|
|
inline u32 TextureCacheDX9::GetCurrentClutHash() {
|
2013-08-17 11:23:51 +02:00
|
|
|
return clutHash_;
|
|
|
|
}
|
|
|
|
|
2017-02-08 15:43:53 +01:00
|
|
|
void TextureCacheDX9::Unbind() {
|
|
|
|
pD3Ddevice->SetTexture(0, NULL);
|
2013-09-10 22:35:38 +02:00
|
|
|
}
|
|
|
|
|
2015-03-15 20:05:35 -07:00
|
|
|
void TextureCacheDX9::ApplyTexture() {
|
2016-05-01 16:20:05 -07:00
|
|
|
TexCacheEntry *entry = nextTexture_;
|
|
|
|
if (entry == nullptr) {
|
2015-03-15 20:05:35 -07:00
|
|
|
return;
|
|
|
|
}
|
2016-05-01 16:20:05 -07:00
|
|
|
nextTexture_ = nullptr;
|
2015-03-15 20:05:35 -07:00
|
|
|
|
2016-05-01 16:20:05 -07:00
|
|
|
UpdateMaxSeenV(entry, gstate.isModeThrough());
|
|
|
|
|
|
|
|
bool replaceImages = false;
|
|
|
|
if (nextNeedsRebuild_) {
|
|
|
|
if (nextNeedsRehash_) {
|
|
|
|
// Update the hash on the texture.
|
|
|
|
int w = gstate.getTextureWidth(0);
|
|
|
|
int h = gstate.getTextureHeight(0);
|
|
|
|
entry->fullhash = QuickTexHash(replacer, entry->addr, entry->bufw, w, h, GETextureFormat(entry->format), entry);
|
|
|
|
}
|
|
|
|
if (nextNeedsChange_) {
|
|
|
|
// This texture existed previously, let's handle the change.
|
|
|
|
replaceImages = HandleTextureChange(entry, nextChangeReason_, false, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We actually build afterward (shared with rehash rebuild.)
|
|
|
|
} else if (nextNeedsRehash_) {
|
|
|
|
// Okay, this matched and didn't change - but let's check the hash. Maybe it will change.
|
|
|
|
bool doDelete = true;
|
|
|
|
if (!CheckFullHash(entry, doDelete)) {
|
|
|
|
replaceImages = HandleTextureChange(entry, "hash fail", true, doDelete);
|
|
|
|
nextNeedsRebuild_ = true;
|
|
|
|
} else if (nextTexture_ != nullptr) {
|
|
|
|
// Secondary cache picked a different texture, use it.
|
|
|
|
entry = nextTexture_;
|
|
|
|
nextTexture_ = nullptr;
|
|
|
|
UpdateMaxSeenV(entry, gstate.isModeThrough());
|
|
|
|
}
|
|
|
|
}
|
2015-03-15 21:38:01 -07:00
|
|
|
|
2016-05-01 16:20:05 -07:00
|
|
|
// Okay, now actually rebuild the texture if needed.
|
|
|
|
if (nextNeedsRebuild_) {
|
|
|
|
BuildTexture(entry, replaceImages);
|
2015-03-15 20:05:35 -07:00
|
|
|
}
|
|
|
|
|
2016-05-01 16:20:05 -07:00
|
|
|
entry->lastFrame = gpuStats.numFlips;
|
|
|
|
if (entry->framebuffer) {
|
|
|
|
ApplyTextureFramebuffer(entry, entry->framebuffer);
|
|
|
|
} else {
|
|
|
|
LPDIRECT3DTEXTURE9 texture = DxTex(entry);
|
|
|
|
if (texture != lastBoundTexture) {
|
|
|
|
pD3Ddevice->SetTexture(0, texture);
|
|
|
|
lastBoundTexture = texture;
|
|
|
|
}
|
|
|
|
UpdateSamplingParams(*entry, false);
|
|
|
|
|
|
|
|
gstate_c.textureFullAlpha = entry->GetAlphaStatus() == TexCacheEntry::STATUS_ALPHA_FULL;
|
|
|
|
gstate_c.textureSimpleAlpha = entry->GetAlphaStatus() != TexCacheEntry::STATUS_ALPHA_UNKNOWN;
|
|
|
|
}
|
2015-03-15 20:05:35 -07:00
|
|
|
}
|
|
|
|
|
2016-01-03 23:44:06 -08:00
|
|
|
class TextureShaderApplierDX9 {
|
|
|
|
public:
|
|
|
|
struct Pos {
|
|
|
|
Pos(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {
|
|
|
|
}
|
|
|
|
Pos() {
|
|
|
|
}
|
2015-03-15 20:05:35 -07:00
|
|
|
|
2016-01-03 23:44:06 -08:00
|
|
|
float x;
|
|
|
|
float y;
|
|
|
|
float z;
|
|
|
|
};
|
|
|
|
struct UV {
|
|
|
|
UV(float u_, float v_) : u(u_), v(v_) {
|
|
|
|
}
|
|
|
|
UV() {
|
|
|
|
}
|
2015-03-15 20:05:35 -07:00
|
|
|
|
2016-01-03 23:44:06 -08:00
|
|
|
float u;
|
|
|
|
float v;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PosUV {
|
|
|
|
Pos pos;
|
|
|
|
UV uv;
|
|
|
|
};
|
|
|
|
|
2017-02-05 20:13:28 +01:00
|
|
|
TextureShaderApplierDX9(LPDIRECT3DPIXELSHADER9 pshader, LPDIRECT3DVERTEXDECLARATION9 decl, float bufferW, float bufferH, int renderW, int renderH, float xoff, float yoff)
|
|
|
|
: pshader_(pshader), decl_(decl), bufferW_(bufferW), bufferH_(bufferH), renderW_(renderW), renderH_(renderH) {
|
2016-01-03 23:44:06 -08:00
|
|
|
static const Pos pos[4] = {
|
|
|
|
{-1, 1, 0},
|
|
|
|
{ 1, 1, 0},
|
|
|
|
{ 1, -1, 0},
|
|
|
|
{-1, -1, 0},
|
2015-09-13 11:14:51 -07:00
|
|
|
};
|
2016-01-03 23:44:06 -08:00
|
|
|
static const UV uv[4] = {
|
|
|
|
{0, 0},
|
|
|
|
{1, 0},
|
|
|
|
{1, 1},
|
|
|
|
{0, 1},
|
2015-09-13 11:14:51 -07:00
|
|
|
};
|
|
|
|
|
2016-01-03 23:44:06 -08:00
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
verts_[i].pos = pos[i];
|
|
|
|
verts_[i].pos.x += xoff;
|
|
|
|
verts_[i].pos.y += yoff;
|
|
|
|
verts_[i].uv = uv[i];
|
|
|
|
}
|
|
|
|
}
|
2015-09-13 11:14:51 -07:00
|
|
|
|
2016-01-03 23:44:06 -08:00
|
|
|
void ApplyBounds(const KnownVertexBounds &bounds, u32 uoff, u32 voff, float xoff, float yoff) {
|
2015-09-13 11:14:51 -07:00
|
|
|
// If min is not < max, then we don't have values (wasn't set during decode.)
|
2016-01-03 23:44:06 -08:00
|
|
|
if (bounds.minV < bounds.maxV) {
|
|
|
|
const float invWidth = 1.0f / bufferW_;
|
|
|
|
const float invHeight = 1.0f / bufferH_;
|
2015-09-13 11:14:51 -07:00
|
|
|
// Inverse of half = double.
|
|
|
|
const float invHalfWidth = invWidth * 2.0f;
|
|
|
|
const float invHalfHeight = invHeight * 2.0f;
|
|
|
|
|
2016-01-03 23:44:06 -08:00
|
|
|
const int u1 = bounds.minU + uoff;
|
|
|
|
const int v1 = bounds.minV + voff;
|
|
|
|
const int u2 = bounds.maxU + uoff;
|
|
|
|
const int v2 = bounds.maxV + voff;
|
2015-09-12 19:43:02 -07:00
|
|
|
|
|
|
|
const float left = u1 * invHalfWidth - 1.0f + xoff;
|
|
|
|
const float right = u2 * invHalfWidth - 1.0f + xoff;
|
|
|
|
const float top = v1 * invHalfHeight - 1.0f + yoff;
|
|
|
|
const float bottom = v2 * invHalfHeight - 1.0f + yoff;
|
2015-09-13 11:14:51 -07:00
|
|
|
// Points are: BL, BR, TR, TL.
|
2016-01-03 23:44:06 -08:00
|
|
|
verts_[0].pos = Pos(left, bottom, -1.0f);
|
|
|
|
verts_[1].pos = Pos(right, bottom, -1.0f);
|
|
|
|
verts_[2].pos = Pos(right, top, -1.0f);
|
|
|
|
verts_[3].pos = Pos(left, top, -1.0f);
|
2015-09-13 11:14:51 -07:00
|
|
|
|
|
|
|
// And also the UVs, same order.
|
2015-09-12 19:43:02 -07:00
|
|
|
const float uvleft = u1 * invWidth;
|
|
|
|
const float uvright = u2 * invWidth;
|
2016-01-03 23:44:06 -08:00
|
|
|
const float uvtop = v1 * invHeight;
|
2015-11-01 18:05:17 +01:00
|
|
|
const float uvbottom = v2 * invHeight;
|
2016-01-03 23:44:06 -08:00
|
|
|
verts_[0].uv = UV(uvleft, uvbottom);
|
|
|
|
verts_[1].uv = UV(uvright, uvbottom);
|
|
|
|
verts_[2].uv = UV(uvright, uvtop);
|
|
|
|
verts_[3].uv = UV(uvleft, uvtop);
|
2015-09-13 11:14:51 -07:00
|
|
|
}
|
2016-01-03 23:44:06 -08:00
|
|
|
}
|
2015-09-13 11:14:51 -07:00
|
|
|
|
2016-01-03 23:44:06 -08:00
|
|
|
void Use(LPDIRECT3DVERTEXSHADER9 vshader) {
|
|
|
|
pD3Ddevice->SetPixelShader(pshader_);
|
|
|
|
pD3Ddevice->SetVertexShader(vshader);
|
2017-02-05 20:13:28 +01:00
|
|
|
pD3Ddevice->SetVertexDeclaration(decl_);
|
2016-01-03 23:44:06 -08:00
|
|
|
}
|
2015-03-15 20:05:35 -07:00
|
|
|
|
2016-01-03 23:44:06 -08:00
|
|
|
void Shade() {
|
2015-03-15 20:05:35 -07:00
|
|
|
pD3Ddevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
|
|
|
|
pD3Ddevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
|
|
|
|
pD3Ddevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_ALPHA);
|
|
|
|
pD3Ddevice->SetRenderState(D3DRS_ZENABLE, FALSE);
|
|
|
|
pD3Ddevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
|
|
|
|
pD3Ddevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
|
|
|
|
pD3Ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
|
|
|
|
|
2017-02-05 20:05:03 +01:00
|
|
|
D3DVIEWPORT9 vp{ 0, 0, (DWORD)renderW_, (DWORD)renderH_, 0.0f, 1.0f };
|
|
|
|
pD3Ddevice->SetViewport(&vp);
|
2016-01-03 23:44:06 -08:00
|
|
|
HRESULT hr = pD3Ddevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, verts_, (3 + 2) * sizeof(float));
|
2015-03-15 20:05:35 -07:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
ERROR_LOG_REPORT(G3D, "Depal render failed: %08x", hr);
|
|
|
|
}
|
|
|
|
|
|
|
|
dxstate.Restore();
|
2016-01-03 23:44:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
LPDIRECT3DPIXELSHADER9 pshader_;
|
2017-02-05 20:13:28 +01:00
|
|
|
LPDIRECT3DVERTEXDECLARATION9 decl_;
|
2016-01-03 23:44:06 -08:00
|
|
|
PosUV verts_[4];
|
|
|
|
float bufferW_;
|
|
|
|
float bufferH_;
|
|
|
|
int renderW_;
|
|
|
|
int renderH_;
|
|
|
|
};
|
|
|
|
|
|
|
|
void TextureCacheDX9::ApplyTextureFramebuffer(TexCacheEntry *entry, VirtualFramebuffer *framebuffer) {
|
|
|
|
LPDIRECT3DPIXELSHADER9 pshader = nullptr;
|
|
|
|
const GEPaletteFormat clutFormat = gstate.getClutPaletteFormat();
|
|
|
|
if ((entry->status & TexCacheEntry::STATUS_DEPALETTIZE) && !g_Config.bDisableSlowFramebufEffects) {
|
|
|
|
pshader = depalShaderCache_->GetDepalettizePixelShader(clutFormat, framebuffer->drawnFormat);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pshader) {
|
|
|
|
LPDIRECT3DTEXTURE9 clutTexture = depalShaderCache_->GetClutTexture(clutFormat, clutHash_, clutBuf_);
|
|
|
|
|
2017-02-08 15:58:46 +01:00
|
|
|
Draw::Framebuffer *depalFBO = framebufferManagerDX9_->GetTempFBO(framebuffer->renderWidth, framebuffer->renderHeight, Draw::FBO_8888);
|
2017-02-06 11:26:24 +01:00
|
|
|
draw_->BindFramebufferAsRenderTarget(depalFBO);
|
2016-01-03 23:44:06 -08:00
|
|
|
shaderManager_->DirtyLastShader();
|
|
|
|
|
|
|
|
float xoff = -0.5f / framebuffer->renderWidth;
|
|
|
|
float yoff = 0.5f / framebuffer->renderHeight;
|
|
|
|
|
2017-02-05 20:13:28 +01:00
|
|
|
TextureShaderApplierDX9 shaderApply(pshader, pFramebufferVertexDecl, framebuffer->bufferWidth, framebuffer->bufferHeight, framebuffer->renderWidth, framebuffer->renderHeight, xoff, yoff);
|
2016-01-03 23:44:06 -08:00
|
|
|
shaderApply.ApplyBounds(gstate_c.vertBounds, gstate_c.curTextureXOffset, gstate_c.curTextureYOffset, xoff, yoff);
|
|
|
|
shaderApply.Use(depalShaderCache_->GetDepalettizeVertexShader());
|
|
|
|
|
|
|
|
pD3Ddevice->SetTexture(1, clutTexture);
|
|
|
|
pD3Ddevice->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_POINT);
|
|
|
|
pD3Ddevice->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
|
|
|
|
pD3Ddevice->SetSamplerState(1, D3DSAMP_MIPFILTER, D3DTEXF_NONE);
|
2015-03-15 20:05:35 -07:00
|
|
|
|
2017-02-08 15:58:46 +01:00
|
|
|
framebufferManagerDX9_->BindFramebufferColor(0, framebuffer, BINDFBCOLOR_SKIP_COPY);
|
2016-01-03 23:44:06 -08:00
|
|
|
pD3Ddevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
|
|
|
|
pD3Ddevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
|
|
|
|
pD3Ddevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE);
|
|
|
|
|
|
|
|
shaderApply.Shade();
|
|
|
|
|
2017-02-06 11:26:24 +01:00
|
|
|
draw_->BindFramebufferAsTexture(depalFBO, 0, Draw::FB_COLOR_BIT, 0);
|
2016-05-21 15:40:58 -07:00
|
|
|
|
|
|
|
const u32 bytesPerColor = clutFormat == GE_CMODE_32BIT_ABGR8888 ? sizeof(u32) : sizeof(u16);
|
|
|
|
const u32 clutTotalColors = clutMaxBytes_ / bytesPerColor;
|
|
|
|
|
|
|
|
TexCacheEntry::Status alphaStatus = CheckAlpha(clutBuf_, getClutDestFormat(clutFormat), clutTotalColors, clutTotalColors, 1);
|
|
|
|
gstate_c.textureFullAlpha = alphaStatus == TexCacheEntry::STATUS_ALPHA_FULL;
|
|
|
|
gstate_c.textureSimpleAlpha = alphaStatus == TexCacheEntry::STATUS_ALPHA_SIMPLE;
|
2015-03-15 20:05:35 -07:00
|
|
|
} else {
|
2016-05-21 15:40:58 -07:00
|
|
|
entry->status &= ~TexCacheEntry::STATUS_DEPALETTIZE;
|
|
|
|
|
2017-02-08 15:58:46 +01:00
|
|
|
framebufferManagerDX9_->BindFramebufferColor(0, framebuffer, BINDFBCOLOR_MAY_COPY_WITH_UV | BINDFBCOLOR_APPLY_TEX_OFFSET);
|
2016-05-21 15:40:58 -07:00
|
|
|
|
|
|
|
gstate_c.textureFullAlpha = gstate.getTextureFormat() == GE_TFMT_5650;
|
|
|
|
gstate_c.textureSimpleAlpha = gstate_c.textureFullAlpha;
|
2015-03-15 20:05:35 -07:00
|
|
|
}
|
|
|
|
|
2017-02-08 15:58:46 +01:00
|
|
|
framebufferManagerDX9_->RebindFramebuffer();
|
2015-03-15 20:05:35 -07:00
|
|
|
SetFramebufferSamplingParams(framebuffer->bufferWidth, framebuffer->bufferHeight);
|
|
|
|
|
|
|
|
lastBoundTexture = INVALID_TEX;
|
|
|
|
}
|
|
|
|
|
2013-11-15 15:15:12 +01:00
|
|
|
void TextureCacheDX9::SetTexture(bool force) {
|
2013-08-17 11:23:51 +02:00
|
|
|
#ifdef DEBUG_TEXTURES
|
|
|
|
if (SetDebugTexture()) {
|
|
|
|
// A different texture was bound, let's rebind next time.
|
2015-03-15 20:05:35 -07:00
|
|
|
lastBoundTexture = INVALID_TEX;
|
2013-08-17 11:23:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-11-15 15:15:12 +01:00
|
|
|
if (force) {
|
|
|
|
lastBoundTexture = INVALID_TEX;
|
|
|
|
}
|
2014-09-08 20:55:56 -07:00
|
|
|
|
2013-09-15 21:39:28 -07:00
|
|
|
u32 texaddr = gstate.getTextureAddress(0);
|
2013-08-17 11:23:51 +02:00
|
|
|
if (!Memory::IsValidAddress(texaddr)) {
|
|
|
|
// Bind a null texture and return.
|
|
|
|
pD3Ddevice->SetTexture(0, NULL);
|
2013-08-19 20:36:43 +02:00
|
|
|
lastBoundTexture = INVALID_TEX;
|
2013-08-17 11:23:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-14 11:06:03 -07:00
|
|
|
const u16 dim = gstate.getTextureDimension(0);
|
2014-09-08 20:55:56 -07:00
|
|
|
int w = gstate.getTextureWidth(0);
|
|
|
|
int h = gstate.getTextureHeight(0);
|
|
|
|
|
2013-08-17 11:23:51 +02:00
|
|
|
GETextureFormat format = gstate.getTextureFormat();
|
|
|
|
if (format >= 11) {
|
|
|
|
ERROR_LOG_REPORT(G3D, "Unknown texture format %i", format);
|
|
|
|
// TODO: Better assumption?
|
|
|
|
format = GE_TFMT_5650;
|
|
|
|
}
|
|
|
|
bool hasClut = gstate.isTextureFormatIndexed();
|
|
|
|
|
2014-09-08 20:55:56 -07:00
|
|
|
// Ignore uncached/kernel when caching.
|
2013-08-17 11:23:51 +02:00
|
|
|
u32 cluthash;
|
|
|
|
if (hasClut) {
|
|
|
|
if (clutLastFormat_ != gstate.clutformat) {
|
|
|
|
// We update here because the clut format can be specified after the load.
|
2015-07-29 11:38:42 +02:00
|
|
|
UpdateCurrentClut(gstate.getClutPaletteFormat(), gstate.getClutIndexStartPos(), gstate.isClutIndexSimple());
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
|
|
|
cluthash = GetCurrentClutHash() ^ gstate.clutformat;
|
|
|
|
} else {
|
|
|
|
cluthash = 0;
|
|
|
|
}
|
2016-03-26 12:01:55 -07:00
|
|
|
u64 cachekey = TexCacheEntry::CacheKey(texaddr, format, dim, cluthash);
|
2013-12-29 15:10:07 -08:00
|
|
|
|
|
|
|
int bufw = GetTextureBufw(0, texaddr, format);
|
2015-03-15 19:51:53 -07:00
|
|
|
u8 maxLevel = gstate.getTextureMaxLevel();
|
2013-08-17 11:23:51 +02:00
|
|
|
|
|
|
|
u32 texhash = MiniHash((const u32 *)Memory::GetPointer(texaddr));
|
|
|
|
|
|
|
|
TexCache::iterator iter = cache.find(cachekey);
|
|
|
|
TexCacheEntry *entry = NULL;
|
2014-09-09 00:53:01 -07:00
|
|
|
gstate_c.needShaderTexClamp = false;
|
2014-08-28 01:20:21 -07:00
|
|
|
gstate_c.bgraTexture = true;
|
2013-08-17 11:23:51 +02:00
|
|
|
gstate_c.skipDrawReason &= ~SKIPDRAW_BAD_FB_TEXTURE;
|
2013-09-04 11:19:36 +02:00
|
|
|
bool useBufferedRendering = g_Config.iRenderingMode != FB_NON_BUFFERED_MODE;
|
2013-08-17 11:23:51 +02:00
|
|
|
|
|
|
|
if (iter != cache.end()) {
|
|
|
|
entry = &iter->second;
|
2013-09-10 22:35:38 +02:00
|
|
|
// Validate the texture still matches the cache entry.
|
|
|
|
bool match = entry->Matches(dim, format, maxLevel);
|
2015-03-14 17:05:44 -07:00
|
|
|
const char *reason = "different params";
|
2013-09-10 22:35:38 +02:00
|
|
|
|
2013-08-17 11:23:51 +02:00
|
|
|
// Check for FBO - slow!
|
2013-12-29 15:10:07 -08:00
|
|
|
if (entry->framebuffer) {
|
|
|
|
if (match) {
|
2015-11-28 12:41:37 -08:00
|
|
|
if (hasClut && clutRenderAddress_ != 0xFFFFFFFF) {
|
2015-11-26 14:31:46 -08:00
|
|
|
WARN_LOG_REPORT_ONCE(clutAndTexRender, G3D, "Using rendered texture with rendered CLUT: texfmt=%d, clutfmt=%d", gstate.getTextureFormat(), gstate.getClutPaletteFormat());
|
|
|
|
}
|
|
|
|
|
2014-09-09 00:53:01 -07:00
|
|
|
SetTextureFramebuffer(entry, entry->framebuffer);
|
2013-12-29 15:10:07 -08:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// Make sure we re-evaluate framebuffers.
|
|
|
|
DetachFramebuffer(entry, texaddr, entry->framebuffer);
|
2015-03-14 17:05:44 -07:00
|
|
|
reason = "detached framebuf";
|
2013-12-29 15:10:07 -08:00
|
|
|
match = false;
|
|
|
|
}
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
2013-08-19 20:36:43 +02:00
|
|
|
|
2014-09-09 00:53:01 -07:00
|
|
|
bool rehash = entry->GetHashStatus() == TexCacheEntry::STATUS_UNRELIABLE;
|
2013-08-17 11:23:51 +02:00
|
|
|
|
2014-09-09 00:53:01 -07:00
|
|
|
// First let's see if another texture with the same address had a hashfail.
|
|
|
|
if (entry->status & TexCacheEntry::STATUS_CLUT_RECHECK) {
|
|
|
|
// Always rehash in this case, if one changed the rest all probably did.
|
|
|
|
rehash = true;
|
|
|
|
entry->status &= ~TexCacheEntry::STATUS_CLUT_RECHECK;
|
2017-01-24 09:41:38 +01:00
|
|
|
} else if (!gstate_c.IsDirty(DIRTY_TEXTURE_IMAGE)) {
|
2014-09-09 00:53:01 -07:00
|
|
|
// Okay, just some parameter change - the data didn't change, no need to rehash.
|
|
|
|
rehash = false;
|
|
|
|
}
|
|
|
|
|
2013-08-17 11:23:51 +02:00
|
|
|
if (match) {
|
2013-08-17 15:11:27 +02:00
|
|
|
if (entry->lastFrame != gpuStats.numFlips) {
|
2013-12-29 15:10:07 -08:00
|
|
|
u32 diff = gpuStats.numFlips - entry->lastFrame;
|
2013-08-17 11:23:51 +02:00
|
|
|
entry->numFrames++;
|
2013-12-29 15:10:07 -08:00
|
|
|
|
|
|
|
if (entry->framesUntilNextFullHash < diff) {
|
|
|
|
// Exponential backoff up to 512 frames. Textures are often reused.
|
|
|
|
if (entry->numFrames > 32) {
|
|
|
|
// Also, try to add some "randomness" to avoid rehashing several textures the same frame.
|
2015-03-15 19:46:36 -07:00
|
|
|
entry->framesUntilNextFullHash = std::min(512, entry->numFrames) + ((intptr_t)entry->texturePtr & 15);
|
2013-12-29 15:10:07 -08:00
|
|
|
} else {
|
|
|
|
entry->framesUntilNextFullHash = entry->numFrames;
|
|
|
|
}
|
|
|
|
rehash = true;
|
|
|
|
} else {
|
|
|
|
entry->framesUntilNextFullHash -= diff;
|
|
|
|
}
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If it's not huge or has been invalidated many times, recheck the whole texture.
|
2014-09-09 00:53:01 -07:00
|
|
|
if (entry->invalidHint > 180 || (entry->invalidHint > 15 && (dim >> 8) < 9 && (dim & 0xF) < 9)) {
|
2013-08-17 11:23:51 +02:00
|
|
|
entry->invalidHint = 0;
|
|
|
|
rehash = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (texhash != entry->hash) {
|
|
|
|
match = false;
|
2016-05-01 16:20:05 -07:00
|
|
|
} else if (entry->GetHashStatus() == TexCacheEntry::STATUS_RELIABLE) {
|
|
|
|
rehash = false;
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-03 23:06:15 -08:00
|
|
|
if (match && (entry->status & TexCacheEntry::STATUS_TO_SCALE) && standardScaleFactor_ != 1 && texelsScaledThisFrame_ < TEXCACHE_MAX_TEXELS_SCALED) {
|
2015-03-15 09:10:09 -07:00
|
|
|
if ((entry->status & TexCacheEntry::STATUS_CHANGE_FREQUENT) == 0) {
|
|
|
|
// INFO_LOG(G3D, "Reloading texture to do the scaling we skipped..");
|
|
|
|
match = false;
|
2015-03-14 17:05:44 -07:00
|
|
|
reason = "scaling";
|
2015-03-15 09:10:09 -07:00
|
|
|
}
|
2014-09-09 00:53:01 -07:00
|
|
|
}
|
|
|
|
|
2013-08-17 11:23:51 +02:00
|
|
|
if (match) {
|
|
|
|
// TODO: Mark the entry reliable if it's been safe for long enough?
|
|
|
|
//got one!
|
2016-05-01 16:20:05 -07:00
|
|
|
if (entry->texturePtr != lastBoundTexture) {
|
|
|
|
gstate_c.curTextureWidth = w;
|
|
|
|
gstate_c.curTextureHeight = h;
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
2016-05-01 16:20:05 -07:00
|
|
|
if (rehash) {
|
|
|
|
// Update in case any of these changed.
|
|
|
|
entry->sizeInRAM = (textureBitsPerPixel[format] * bufw * h / 2) / 8;
|
|
|
|
entry->bufw = bufw;
|
|
|
|
entry->cluthash = cluthash;
|
|
|
|
}
|
|
|
|
|
|
|
|
nextTexture_ = entry;
|
|
|
|
nextNeedsRehash_ = rehash;
|
|
|
|
nextNeedsChange_ = false;
|
|
|
|
// Might need a rebuild if the hash fails.
|
|
|
|
nextNeedsRebuild_= false;
|
2013-09-10 22:35:38 +02:00
|
|
|
VERBOSE_LOG(G3D, "Texture at %08x Found in Cache, applying", texaddr);
|
2013-08-17 11:23:51 +02:00
|
|
|
return; //Done!
|
|
|
|
} else {
|
2016-05-01 16:20:05 -07:00
|
|
|
nextChangeReason_ = reason;
|
|
|
|
nextNeedsChange_ = true;
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
|
|
|
} else {
|
2013-09-10 22:35:38 +02:00
|
|
|
VERBOSE_LOG(G3D, "No texture in cache, decoding...");
|
2013-08-17 11:23:51 +02:00
|
|
|
TexCacheEntry entryNew = {0};
|
|
|
|
cache[cachekey] = entryNew;
|
|
|
|
|
2015-11-28 12:41:37 -08:00
|
|
|
if (hasClut && clutRenderAddress_ != 0xFFFFFFFF) {
|
2015-11-26 14:31:46 -08:00
|
|
|
WARN_LOG_REPORT_ONCE(clutUseRender, G3D, "Using texture with rendered CLUT: texfmt=%d, clutfmt=%d", gstate.getTextureFormat(), gstate.getClutPaletteFormat());
|
|
|
|
}
|
|
|
|
|
2013-08-17 11:23:51 +02:00
|
|
|
entry = &cache[cachekey];
|
2013-12-29 15:10:07 -08:00
|
|
|
if (g_Config.bTextureBackoffCache) {
|
|
|
|
entry->status = TexCacheEntry::STATUS_HASHING;
|
|
|
|
} else {
|
|
|
|
entry->status = TexCacheEntry::STATUS_UNRELIABLE;
|
|
|
|
}
|
2013-08-17 11:23:51 +02:00
|
|
|
|
2016-05-01 16:20:05 -07:00
|
|
|
nextNeedsChange_ = false;
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// We have to decode it, let's setup the cache entry first.
|
|
|
|
entry->addr = texaddr;
|
|
|
|
entry->hash = texhash;
|
2016-05-01 16:20:05 -07:00
|
|
|
entry->dim = dim;
|
2013-08-17 11:23:51 +02:00
|
|
|
entry->format = format;
|
|
|
|
entry->maxLevel = maxLevel;
|
|
|
|
|
|
|
|
// This would overestimate the size in many case so we underestimate instead
|
|
|
|
// to avoid excessive clearing caused by cache invalidations.
|
2013-09-15 21:27:13 -07:00
|
|
|
entry->sizeInRAM = (textureBitsPerPixel[format] * bufw * h / 2) / 8;
|
2016-05-01 16:20:05 -07:00
|
|
|
entry->bufw = bufw;
|
2013-08-17 11:23:51 +02:00
|
|
|
|
|
|
|
entry->cluthash = cluthash;
|
|
|
|
|
|
|
|
gstate_c.curTextureWidth = w;
|
|
|
|
gstate_c.curTextureHeight = h;
|
|
|
|
|
2016-05-21 17:53:42 -07:00
|
|
|
// Before we go reading the texture from memory, let's check for render-to-texture.
|
|
|
|
// We must do this early so we have the right w/h.
|
|
|
|
entry->framebuffer = 0;
|
|
|
|
for (size_t i = 0, n = fbCache_.size(); i < n; ++i) {
|
|
|
|
auto framebuffer = fbCache_[i];
|
|
|
|
AttachFramebuffer(entry, framebuffer->fb_address, framebuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we ended up with a framebuffer, attach it - no texture decoding needed.
|
|
|
|
if (entry->framebuffer) {
|
|
|
|
SetTextureFramebuffer(entry, entry->framebuffer);
|
|
|
|
}
|
|
|
|
|
2016-05-01 16:20:05 -07:00
|
|
|
nextTexture_ = entry;
|
2016-05-21 17:53:42 -07:00
|
|
|
nextNeedsRehash_ = entry->framebuffer == nullptr;
|
|
|
|
// We still need to rebuild, to allocate a texture. But we'll bail early.
|
2016-05-01 16:20:05 -07:00
|
|
|
nextNeedsRebuild_= true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextureCacheDX9::CheckFullHash(TexCacheEntry *const entry, bool &doDelete) {
|
|
|
|
bool hashFail = false;
|
|
|
|
int w = gstate.getTextureWidth(0);
|
|
|
|
int h = gstate.getTextureHeight(0);
|
|
|
|
u32 fullhash = QuickTexHash(replacer, entry->addr, entry->bufw, w, h, GETextureFormat(entry->format), entry);
|
|
|
|
if (fullhash != entry->fullhash) {
|
|
|
|
hashFail = true;
|
|
|
|
} else {
|
|
|
|
if (g_Config.bTextureBackoffCache) {
|
|
|
|
if (entry->GetHashStatus() != TexCacheEntry::STATUS_HASHING && entry->numFrames > TexCacheEntry::FRAMES_REGAIN_TRUST) {
|
|
|
|
// Reset to STATUS_HASHING.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hashFail) {
|
|
|
|
entry->status |= TexCacheEntry::STATUS_UNRELIABLE;
|
|
|
|
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;
|
|
|
|
|
|
|
|
// Don't give up just yet. Let's try the secondary cache if it's been invalidated before.
|
|
|
|
// If it's failed a bunch of times, then the second cache is just wasting time and VRAM.
|
|
|
|
if (g_Config.bTextureSecondaryCache) {
|
|
|
|
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;
|
|
|
|
if (secondEntry->Matches(entry->dim, entry->format, entry->maxLevel)) {
|
|
|
|
// Reset the numInvalidated value lower, we got a match.
|
|
|
|
if (entry->numInvalidated > 8) {
|
|
|
|
--entry->numInvalidated;
|
|
|
|
}
|
|
|
|
nextTexture_ = secondEntry;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
secondKey = entry->fullhash | ((u64)entry->cluthash << 32);
|
|
|
|
secondCacheSizeEstimate_ += EstimateTexMemoryUsage(entry);
|
|
|
|
secondCache[secondKey] = *entry;
|
|
|
|
doDelete = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We know it failed, so update the full hash right away.
|
|
|
|
entry->fullhash = fullhash;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextureCacheDX9::HandleTextureChange(TexCacheEntry *const entry, const char *reason, bool initialMatch, bool doDelete) {
|
|
|
|
bool replaceImages = false;
|
|
|
|
|
|
|
|
cacheSizeEstimate_ -= EstimateTexMemoryUsage(entry);
|
|
|
|
entry->numInvalidated++;
|
|
|
|
gpuStats.numTextureInvalidations++;
|
|
|
|
DEBUG_LOG(G3D, "Texture different or overwritten, reloading at %08x: %s", entry->addr, reason);
|
|
|
|
if (doDelete) {
|
2016-06-06 19:35:58 -07:00
|
|
|
if (initialMatch && standardScaleFactor_ == 1 && (entry->status & TexCacheEntry::STATUS_IS_SCALED) == 0) {
|
2016-05-01 16:20:05 -07:00
|
|
|
// Actually, if size and number of levels match, let's try to avoid deleting and recreating.
|
|
|
|
// Instead, let's use glTexSubImage to replace the images.
|
|
|
|
replaceImages = true;
|
|
|
|
} else {
|
|
|
|
if (entry->texturePtr == lastBoundTexture) {
|
|
|
|
lastBoundTexture = INVALID_TEX;
|
|
|
|
}
|
|
|
|
ReleaseTexture(entry);
|
2016-06-06 19:35:58 -07:00
|
|
|
entry->status &= ~TexCacheEntry::STATUS_IS_SCALED;
|
2016-05-01 16:20:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Clear the reliable bit if set.
|
|
|
|
if (entry->GetHashStatus() == TexCacheEntry::STATUS_RELIABLE) {
|
|
|
|
entry->SetHashStatus(TexCacheEntry::STATUS_HASHING);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also, mark any textures with the same address but different clut. They need rechecking.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return replaceImages;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCacheDX9::BuildTexture(TexCacheEntry *const entry, bool replaceImages) {
|
|
|
|
entry->status &= ~TexCacheEntry::STATUS_ALPHA_MASK;
|
|
|
|
|
2014-12-21 16:54:26 -08:00
|
|
|
// For the estimate, we assume cluts always point to 8888 for simplicity.
|
|
|
|
cacheSizeEstimate_ += EstimateTexMemoryUsage(entry);
|
|
|
|
|
2013-12-29 15:10:07 -08:00
|
|
|
// TODO: If a framebuffer is attached here, might end up with a bad entry.texture.
|
|
|
|
// Should just always create one here or something (like GLES.)
|
|
|
|
|
2013-09-10 22:35:38 +02:00
|
|
|
if (entry->framebuffer) {
|
2016-05-21 17:53:42 -07:00
|
|
|
// Nothing else to do here.
|
2016-05-01 16:20:05 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((entry->bufw == 0 || (gstate.texbufwidth[0] & 0xf800) != 0) && entry->addr >= PSP_GetKernelMemoryEnd()) {
|
|
|
|
ERROR_LOG_REPORT(G3D, "Texture with unexpected bufw (full=%d)", gstate.texbufwidth[0] & 0xffff);
|
|
|
|
// Proceeding here can cause a crash.
|
2013-09-10 22:35:38 +02:00
|
|
|
return;
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust maxLevel to actually present levels..
|
2014-09-09 00:53:01 -07:00
|
|
|
bool badMipSizes = false;
|
2016-05-01 16:20:05 -07:00
|
|
|
int maxLevel = entry->maxLevel;
|
|
|
|
for (int i = 0; i <= maxLevel; i++) {
|
2013-08-17 11:23:51 +02:00
|
|
|
// If encountering levels pointing to nothing, adjust max level.
|
2013-09-15 21:39:28 -07:00
|
|
|
u32 levelTexaddr = gstate.getTextureAddress(i);
|
2013-08-17 11:23:51 +02:00
|
|
|
if (!Memory::IsValidAddress(levelTexaddr)) {
|
|
|
|
maxLevel = i - 1;
|
|
|
|
break;
|
|
|
|
}
|
2014-09-09 00:53:01 -07:00
|
|
|
|
2016-05-01 16:20:05 -07:00
|
|
|
if (i > 0 && gstate_c.Supports(GPU_SUPPORTS_TEXTURE_LOD_CONTROL)) {
|
2014-09-09 00:53:01 -07:00
|
|
|
int tw = gstate.getTextureWidth(i);
|
|
|
|
int th = gstate.getTextureHeight(i);
|
|
|
|
if (tw != 1 && tw != (gstate.getTextureWidth(i - 1) >> 1))
|
|
|
|
badMipSizes = true;
|
|
|
|
else if (th != 1 && th != (gstate.getTextureHeight(i - 1) >> 1))
|
|
|
|
badMipSizes = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// In addition, simply don't load more than level 0 if g_Config.bMipMap is false.
|
|
|
|
if (!g_Config.bMipMap) {
|
|
|
|
maxLevel = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If GLES3 is available, we can preallocate the storage, which makes texture loading more efficient.
|
2016-05-01 16:20:05 -07:00
|
|
|
D3DFORMAT dstFmt = GetDestFormat(GETextureFormat(entry->format), gstate.getClutPaletteFormat());
|
2014-09-09 00:53:01 -07:00
|
|
|
|
2016-01-03 23:06:15 -08:00
|
|
|
int scaleFactor = standardScaleFactor_;
|
2013-08-17 11:23:51 +02:00
|
|
|
|
2015-12-30 16:54:25 -08:00
|
|
|
// Rachet down scale factor in low-memory mode.
|
|
|
|
if (lowMemoryMode_) {
|
|
|
|
// Keep it even, though, just in case of npot troubles.
|
|
|
|
scaleFactor = scaleFactor > 4 ? 4 : (scaleFactor > 2 ? 2 : 1);
|
|
|
|
}
|
|
|
|
|
2016-05-01 16:20:05 -07:00
|
|
|
u64 cachekey = replacer.Enabled() ? entry->CacheKey() : 0;
|
|
|
|
int w = gstate.getTextureWidth(0);
|
|
|
|
int h = gstate.getTextureHeight(0);
|
2016-04-30 23:58:58 -07:00
|
|
|
ReplacedTexture &replaced = replacer.FindReplacement(cachekey, entry->fullhash, w, h);
|
2016-04-30 13:44:31 -07:00
|
|
|
if (replaced.GetSize(0, w, h)) {
|
2016-06-06 23:02:56 -07:00
|
|
|
if (replaceImages) {
|
|
|
|
// Since we're replacing the texture, we can't replace the image inside.
|
|
|
|
ReleaseTexture(entry);
|
|
|
|
replaceImages = false;
|
|
|
|
}
|
2016-04-30 13:44:31 -07:00
|
|
|
// We're replacing, so we won't scale.
|
|
|
|
scaleFactor = 1;
|
2016-06-06 19:35:58 -07:00
|
|
|
entry->status |= TexCacheEntry::STATUS_IS_SCALED;
|
2016-04-30 13:44:31 -07:00
|
|
|
if (g_Config.bMipMap) {
|
|
|
|
maxLevel = replaced.MaxLevel();
|
|
|
|
badMipSizes = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-09 00:53:01 -07:00
|
|
|
// Don't scale the PPGe texture.
|
2016-12-16 22:53:55 +02:00
|
|
|
if (entry->addr > 0x05000000 && entry->addr < PSP_GetKernelMemoryEnd())
|
2014-09-09 00:53:01 -07:00
|
|
|
scaleFactor = 1;
|
2016-05-06 19:58:01 -07:00
|
|
|
if ((entry->status & TexCacheEntry::STATUS_CHANGE_FREQUENT) != 0 && scaleFactor != 1) {
|
2015-12-30 16:54:25 -08:00
|
|
|
// Remember for later that we /wanted/ to scale this texture.
|
|
|
|
entry->status |= TexCacheEntry::STATUS_TO_SCALE;
|
|
|
|
scaleFactor = 1;
|
|
|
|
}
|
2013-08-17 11:23:51 +02:00
|
|
|
|
2015-12-30 16:54:25 -08:00
|
|
|
if (scaleFactor != 1) {
|
2014-09-09 00:53:01 -07:00
|
|
|
if (texelsScaledThisFrame_ >= TEXCACHE_MAX_TEXELS_SCALED) {
|
|
|
|
entry->status |= TexCacheEntry::STATUS_TO_SCALE;
|
|
|
|
scaleFactor = 1;
|
|
|
|
} else {
|
|
|
|
entry->status &= ~TexCacheEntry::STATUS_TO_SCALE;
|
2016-06-06 19:35:58 -07:00
|
|
|
entry->status |= TexCacheEntry::STATUS_IS_SCALED;
|
2014-09-09 00:53:01 -07:00
|
|
|
texelsScaledThisFrame_ += w * h;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-14 09:13:06 -07:00
|
|
|
if (replaceImages) {
|
|
|
|
// Make sure it's not currently set.
|
|
|
|
pD3Ddevice->SetTexture(0, NULL);
|
|
|
|
}
|
|
|
|
// Seems to cause problems in Tactics Ogre.
|
|
|
|
if (badMipSizes) {
|
|
|
|
maxLevel = 0;
|
|
|
|
}
|
|
|
|
|
2016-04-30 13:44:31 -07:00
|
|
|
LoadTextureLevel(*entry, replaced, 0, maxLevel, replaceImages, scaleFactor, dstFmt);
|
2015-03-15 19:46:36 -07:00
|
|
|
LPDIRECT3DTEXTURE9 &texture = DxTex(entry);
|
|
|
|
if (!texture) {
|
2014-09-14 09:13:06 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mipmapping is only enabled when texture scaling is disabled.
|
2015-12-30 16:54:25 -08:00
|
|
|
if (maxLevel > 0 && scaleFactor == 1) {
|
2016-05-01 16:20:05 -07:00
|
|
|
for (int i = 1; i <= maxLevel; i++) {
|
2016-04-30 13:44:31 -07:00
|
|
|
LoadTextureLevel(*entry, replaced, i, maxLevel, replaceImages, scaleFactor, dstFmt);
|
2014-09-14 09:13:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-30 13:44:31 -07:00
|
|
|
if (replaced.Valid()) {
|
|
|
|
entry->SetAlphaStatus(TexCacheEntry::Status(replaced.AlphaStatus()));
|
|
|
|
}
|
2014-09-09 00:53:01 -07:00
|
|
|
}
|
2013-08-17 11:23:51 +02:00
|
|
|
|
2014-09-09 00:53:01 -07:00
|
|
|
D3DFORMAT TextureCacheDX9::GetDestFormat(GETextureFormat format, GEPaletteFormat clutFormat) const {
|
|
|
|
switch (format) {
|
|
|
|
case GE_TFMT_CLUT4:
|
|
|
|
case GE_TFMT_CLUT8:
|
|
|
|
case GE_TFMT_CLUT16:
|
|
|
|
case GE_TFMT_CLUT32:
|
|
|
|
return getClutDestFormat(clutFormat);
|
|
|
|
case GE_TFMT_4444:
|
|
|
|
return D3DFMT_A4R4G4B4;
|
|
|
|
case GE_TFMT_5551:
|
|
|
|
return D3DFMT_A1R5G5B5;
|
|
|
|
case GE_TFMT_5650:
|
|
|
|
return D3DFMT_R5G6B5;
|
|
|
|
case GE_TFMT_8888:
|
|
|
|
case GE_TFMT_DXT1:
|
|
|
|
case GE_TFMT_DXT3:
|
|
|
|
case GE_TFMT_DXT5:
|
|
|
|
default:
|
|
|
|
return D3DFMT_A8R8G8B8;
|
|
|
|
}
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
|
|
|
|
2014-09-08 20:55:56 -07:00
|
|
|
TextureCacheDX9::TexCacheEntry::Status TextureCacheDX9::CheckAlpha(const u32 *pixelData, u32 dstFmt, int stride, int w, int h) {
|
2015-05-24 22:55:43 -07:00
|
|
|
CheckAlphaResult res;
|
2013-08-17 11:23:51 +02:00
|
|
|
switch (dstFmt) {
|
|
|
|
case D3DFMT_A4R4G4B4:
|
2015-05-24 22:55:43 -07:00
|
|
|
res = CheckAlphaRGBA4444Basic(pixelData, stride, w, h);
|
2013-08-17 11:23:51 +02:00
|
|
|
break;
|
|
|
|
case D3DFMT_A1R5G5B5:
|
2015-05-24 22:55:43 -07:00
|
|
|
res = CheckAlphaRGBA5551Basic(pixelData, stride, w, h);
|
2013-08-17 11:23:51 +02:00
|
|
|
break;
|
|
|
|
case D3DFMT_R5G6B5:
|
2015-05-24 22:55:43 -07:00
|
|
|
// Never has any alpha.
|
|
|
|
res = CHECKALPHA_FULL;
|
2013-08-17 11:23:51 +02:00
|
|
|
break;
|
|
|
|
default:
|
2015-05-24 22:55:43 -07:00
|
|
|
res = CheckAlphaRGBA8888Basic(pixelData, stride, w, h);
|
2013-08-17 11:23:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-05-24 22:55:43 -07:00
|
|
|
return (TexCacheEntry::Status)res;
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
|
|
|
|
2016-04-30 13:44:31 -07:00
|
|
|
ReplacedTextureFormat FromD3D9Format(u32 fmt) {
|
|
|
|
switch (fmt) {
|
2017-02-08 15:48:36 +01:00
|
|
|
case D3DFMT_R5G6B5: return ReplacedTextureFormat::F_5650;
|
|
|
|
case D3DFMT_A1R5G5B5: return ReplacedTextureFormat::F_5551;
|
|
|
|
case D3DFMT_A4R4G4B4: return ReplacedTextureFormat::F_4444;
|
|
|
|
case D3DFMT_A8R8G8B8: default: return ReplacedTextureFormat::F_8888;
|
2016-04-30 13:44:31 -07:00
|
|
|
}
|
|
|
|
}
|
2013-08-17 11:23:51 +02:00
|
|
|
|
2017-01-25 10:04:54 +01:00
|
|
|
D3DFORMAT ToD3D9Format(ReplacedTextureFormat fmt) {
|
2016-04-30 13:44:31 -07:00
|
|
|
switch (fmt) {
|
2017-02-08 15:48:36 +01:00
|
|
|
case ReplacedTextureFormat::F_5650: return D3DFMT_R5G6B5;
|
|
|
|
case ReplacedTextureFormat::F_5551: return D3DFMT_A1R5G5B5;
|
|
|
|
case ReplacedTextureFormat::F_4444: return D3DFMT_A4R4G4B4;
|
|
|
|
case ReplacedTextureFormat::F_8888: default: return D3DFMT_A8R8G8B8;
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
2016-04-30 13:44:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCacheDX9::LoadTextureLevel(TexCacheEntry &entry, ReplacedTexture &replaced, int level, int maxLevel, bool replaceImages, int scaleFactor, u32 dstFmt) {
|
2013-08-19 20:36:43 +02:00
|
|
|
int w = gstate.getTextureWidth(level);
|
|
|
|
int h = gstate.getTextureHeight(level);
|
2013-08-17 11:23:51 +02:00
|
|
|
|
2016-06-25 09:13:14 -07:00
|
|
|
LPDIRECT3DTEXTURE9 &texture = DxTex(&entry);
|
|
|
|
if (level == 0 && (!replaceImages || texture == nullptr)) {
|
|
|
|
// Create texture
|
|
|
|
D3DPOOL pool = D3DPOOL_MANAGED;
|
|
|
|
int usage = 0;
|
|
|
|
if (pD3DdeviceEx) {
|
|
|
|
pool = D3DPOOL_DEFAULT;
|
|
|
|
usage = D3DUSAGE_DYNAMIC; // TODO: Switch to using a staging texture?
|
|
|
|
}
|
|
|
|
int levels = scaleFactor == 1 ? maxLevel + 1 : 1;
|
|
|
|
int tw = w, th = h;
|
2017-01-25 10:04:54 +01:00
|
|
|
D3DFORMAT tfmt = (D3DFORMAT)(dstFmt);
|
2016-07-17 09:03:08 -07:00
|
|
|
if (replaced.GetSize(level, tw, th)) {
|
2017-01-25 10:04:54 +01:00
|
|
|
tfmt = ToD3D9Format(replaced.Format(level));
|
2016-07-17 09:03:08 -07:00
|
|
|
} else {
|
2016-06-25 09:13:14 -07:00
|
|
|
tw *= scaleFactor;
|
|
|
|
th *= scaleFactor;
|
|
|
|
if (scaleFactor > 1) {
|
|
|
|
tfmt = D3DFMT_A8R8G8B8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
HRESULT hr = pD3Ddevice->CreateTexture(tw, th, levels, usage, tfmt, pool, &texture, NULL);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
INFO_LOG(G3D, "Failed to create D3D texture");
|
|
|
|
ReleaseTexture(&entry);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
D3DLOCKED_RECT rect;
|
|
|
|
texture->LockRect(level, &rect, NULL, 0);
|
|
|
|
|
2013-08-17 11:23:51 +02:00
|
|
|
gpuStats.numTexturesDecoded++;
|
2016-04-30 13:44:31 -07:00
|
|
|
if (replaced.GetSize(level, w, h)) {
|
2016-06-25 09:13:14 -07:00
|
|
|
replaced.Load(level, rect.pBits, rect.Pitch);
|
2016-04-30 13:44:31 -07:00
|
|
|
dstFmt = ToD3D9Format(replaced.Format(level));
|
|
|
|
} else {
|
2016-06-25 09:13:14 -07:00
|
|
|
GETextureFormat tfmt = (GETextureFormat)entry.format;
|
2016-04-30 13:44:31 -07:00
|
|
|
GEPaletteFormat clutformat = gstate.getClutPaletteFormat();
|
2016-06-25 09:13:14 -07:00
|
|
|
u32 texaddr = gstate.getTextureAddress(level);
|
|
|
|
int bufw = GetTextureBufw(level, texaddr, tfmt);
|
|
|
|
int bpp = dstFmt == D3DFMT_A8R8G8B8 ? 4 : 2;
|
|
|
|
|
|
|
|
u32 *pixelData = (u32 *)rect.pBits;
|
|
|
|
int decPitch = rect.Pitch;
|
|
|
|
if (scaleFactor > 1) {
|
|
|
|
tmpTexBufRearrange.resize(std::max(bufw, w) * h);
|
|
|
|
pixelData = tmpTexBufRearrange.data();
|
|
|
|
// We want to end up with a neatly packed texture for scaling.
|
|
|
|
decPitch = w * bpp;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool decSuccess = DecodeTextureLevel((u8 *)pixelData, decPitch, tfmt, clutformat, texaddr, level, bufw, false);
|
|
|
|
if (!decSuccess) {
|
|
|
|
memset(pixelData, 0, decPitch * h);
|
2016-04-30 13:44:31 -07:00
|
|
|
}
|
2013-08-19 20:36:43 +02:00
|
|
|
|
2016-06-25 09:13:14 -07:00
|
|
|
if (scaleFactor > 1) {
|
|
|
|
scaler.ScaleAlways((u32 *)rect.pBits, pixelData, dstFmt, w, h, scaleFactor);
|
|
|
|
pixelData = (u32 *)rect.pBits;
|
|
|
|
|
|
|
|
// We always end up at 8888. Other parts assume this.
|
|
|
|
assert(dstFmt == D3DFMT_A8R8G8B8);
|
|
|
|
bpp = sizeof(u32);
|
|
|
|
decPitch = w * bpp;
|
|
|
|
|
|
|
|
if (decPitch != rect.Pitch) {
|
|
|
|
// Rearrange in place to match the requested pitch.
|
|
|
|
// (it can only be larger than w * bpp, and a match is likely.)
|
|
|
|
for (int y = h - 1; y >= 0; --y) {
|
|
|
|
memcpy((u8 *)rect.pBits + rect.Pitch * y, (u8 *)rect.pBits + decPitch * y, w * bpp);
|
|
|
|
}
|
|
|
|
decPitch = rect.Pitch;
|
|
|
|
}
|
|
|
|
}
|
2014-09-09 00:53:01 -07:00
|
|
|
|
2016-04-30 13:44:31 -07:00
|
|
|
if ((entry.status & TexCacheEntry::STATUS_CHANGE_FREQUENT) == 0) {
|
2016-06-25 09:13:14 -07:00
|
|
|
TexCacheEntry::Status alphaStatus = CheckAlpha(pixelData, dstFmt, decPitch / bpp, w, h);
|
2016-04-30 13:44:31 -07:00
|
|
|
entry.SetAlphaStatus(alphaStatus, level);
|
|
|
|
} else {
|
|
|
|
entry.SetAlphaStatus(TexCacheEntry::STATUS_ALPHA_UNKNOWN);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (replacer.Enabled()) {
|
2016-05-01 08:54:43 -07:00
|
|
|
ReplacedTextureDecodeInfo replacedInfo;
|
|
|
|
replacedInfo.cachekey = entry.CacheKey();
|
|
|
|
replacedInfo.hash = entry.fullhash;
|
|
|
|
replacedInfo.addr = entry.addr;
|
|
|
|
replacedInfo.isVideo = videos_.find(entry.addr & 0x3FFFFFFF) != videos_.end();
|
|
|
|
replacedInfo.isFinal = (entry.status & TexCacheEntry::STATUS_TO_SCALE) == 0;
|
|
|
|
replacedInfo.scaleFactor = scaleFactor;
|
|
|
|
replacedInfo.fmt = FromD3D9Format(dstFmt);
|
|
|
|
|
2016-06-25 09:13:14 -07:00
|
|
|
replacer.NotifyTextureDecoded(replacedInfo, pixelData, decPitch, level, w, h);
|
2014-09-14 09:13:06 -07:00
|
|
|
}
|
|
|
|
}
|
2013-08-27 16:31:06 +02:00
|
|
|
|
2015-03-15 19:46:36 -07:00
|
|
|
texture->UnlockRect(level);
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
|
|
|
|
2015-01-17 11:57:07 -08:00
|
|
|
bool TextureCacheDX9::DecodeTexture(u8 *output, const GPUgstate &state)
|
2013-08-17 11:23:51 +02:00
|
|
|
{
|
|
|
|
OutputDebugStringA("TextureCache::DecodeTexture : FixMe\r\n");
|
|
|
|
return true;
|
|
|
|
}
|
2013-09-15 08:53:21 -07:00
|
|
|
|
|
|
|
};
|