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 <algorithm>
|
2013-12-29 15:55:09 -08:00
|
|
|
#include <cstring>
|
2013-08-17 11:23:51 +02:00
|
|
|
|
2021-10-17 08:54:45 -07:00
|
|
|
#include "Common/TimeUtil.h"
|
2013-08-17 11:23:51 +02:00
|
|
|
#include "Core/MemMap.h"
|
|
|
|
#include "Core/Reporting.h"
|
|
|
|
#include "GPU/ge_constants.h"
|
2020-10-21 23:09:34 +02:00
|
|
|
|
2013-08-17 11:23:51 +02:00
|
|
|
#include "GPU/GPUState.h"
|
2013-09-15 12:46:14 +02:00
|
|
|
#include "GPU/Directx9/TextureCacheDX9.h"
|
2020-08-03 23:24:50 +02:00
|
|
|
#include "GPU/Directx9/FramebufferManagerDX9.h"
|
2014-09-17 23:26:20 +02:00
|
|
|
#include "GPU/Directx9/ShaderManagerDX9.h"
|
|
|
|
#include "GPU/Directx9/DepalettizeShaderDX9.h"
|
2020-10-04 23:24:14 +02:00
|
|
|
#include "Common/GPU/D3D9/D3D9StateCache.h"
|
2020-08-03 23:17:22 +02:00
|
|
|
#include "GPU/Common/FramebufferManagerCommon.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"
|
2020-10-04 00:25:21 +02:00
|
|
|
#include "Common/Math/math_util.h"
|
2013-08-17 11:23:51 +02:00
|
|
|
|
2020-12-10 11:18:43 +01:00
|
|
|
// NOTE: In the D3D backends, we flip R and B in the shaders, so while these look wrong, they're OK.
|
|
|
|
|
|
|
|
Draw::DataFormat FromD3D9Format(u32 fmt) {
|
|
|
|
switch (fmt) {
|
|
|
|
case D3DFMT_A8R8G8B8: default: return Draw::DataFormat::R8G8B8A8_UNORM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
D3DFORMAT ToD3D9Format(Draw::DataFormat fmt) {
|
|
|
|
switch (fmt) {
|
|
|
|
case Draw::DataFormat::R8G8B8A8_UNORM: default: return D3DFMT_A8R8G8B8;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
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;
|
2017-02-20 00:05:23 +01:00
|
|
|
isBgraBackend_ = true;
|
2014-09-09 00:53:01 -07:00
|
|
|
|
2017-03-02 11:39:02 +01:00
|
|
|
device_ = (LPDIRECT3DDEVICE9)draw->GetNativeObject(Draw::NativeObject::DEVICE);
|
|
|
|
deviceEx_ = (LPDIRECT3DDEVICE9EX)draw->GetNativeObject(Draw::NativeObject::DEVICE_EX);
|
2014-09-20 22:39:01 -07:00
|
|
|
D3DCAPS9 pCaps;
|
|
|
|
ZeroMemory(&pCaps, sizeof(pCaps));
|
|
|
|
HRESULT result = 0;
|
2017-03-02 11:39:02 +01:00
|
|
|
if (deviceEx_) {
|
|
|
|
result = deviceEx_->GetDeviceCaps(&pCaps);
|
2014-09-20 22:39:01 -07:00
|
|
|
} else {
|
2017-03-02 11:39:02 +01:00
|
|
|
result = device_->GetDeviceCaps(&pCaps);
|
2014-09-20 22:39:01 -07:00
|
|
|
}
|
|
|
|
if (FAILED(result)) {
|
|
|
|
WARN_LOG(G3D, "Failed to get the device caps!");
|
|
|
|
maxAnisotropyLevel = 16;
|
|
|
|
} else {
|
|
|
|
maxAnisotropyLevel = pCaps.MaxAnisotropy;
|
|
|
|
}
|
2015-03-15 20:05:35 -07:00
|
|
|
|
|
|
|
nextTexture_ = nullptr;
|
2017-03-02 11:39:02 +01:00
|
|
|
device_->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;
|
|
|
|
}
|
|
|
|
|
2017-02-23 17:31:24 +01:00
|
|
|
void TextureCacheDX9::ReleaseTexture(TexCacheEntry *entry, bool delete_them) {
|
2017-02-19 22:31:07 +01:00
|
|
|
LPDIRECT3DTEXTURE9 &texture = DxTex(entry);
|
|
|
|
if (texture) {
|
|
|
|
texture->Release();
|
|
|
|
texture = nullptr;
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
2014-09-09 00:53:01 -07:00
|
|
|
}
|
|
|
|
|
2020-09-20 20:35:42 +02:00
|
|
|
void TextureCacheDX9::InvalidateLastTexture() {
|
|
|
|
lastBoundTexture = INVALID_TEX;
|
2017-02-19 23:50:04 +01:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-13 22:37:16 +02:00
|
|
|
void TextureCacheDX9::ApplySamplingParams(const SamplerCacheKey &key) {
|
|
|
|
dxstate.texMinFilter.set(key.minFilt ? D3DTEXF_LINEAR : D3DTEXF_POINT);
|
|
|
|
dxstate.texMipFilter.set(key.mipFilt ? D3DTEXF_LINEAR : D3DTEXF_POINT);
|
|
|
|
dxstate.texMagFilter.set(key.magFilt ? D3DTEXF_LINEAR : D3DTEXF_POINT);
|
2020-09-13 23:10:43 +02:00
|
|
|
|
|
|
|
// DX9 mip levels are .. odd. The "max level" sets the LARGEST mip to use.
|
|
|
|
// We can enforce only the top mip level by setting a massive negative lod bias.
|
|
|
|
|
|
|
|
if (!key.mipEnable) {
|
|
|
|
dxstate.texMaxMipLevel.set(0);
|
|
|
|
dxstate.texMipLodBias.set(-100.0f);
|
|
|
|
} else {
|
|
|
|
dxstate.texMipLodBias.set((float)key.lodBias / 256.0f);
|
|
|
|
dxstate.texMaxMipLevel.set(key.minLevel / 256);
|
|
|
|
}
|
2020-09-13 22:37:16 +02:00
|
|
|
|
|
|
|
dxstate.texAddressU.set(key.sClamp ? D3DTADDRESS_CLAMP : D3DTADDRESS_WRAP);
|
|
|
|
dxstate.texAddressV.set(key.tClamp ? D3DTADDRESS_CLAMP : D3DTADDRESS_WRAP);
|
2014-09-09 00:53:01 -07:00
|
|
|
}
|
|
|
|
|
2013-09-15 12:46:14 +02:00
|
|
|
void TextureCacheDX9::StartFrame() {
|
2017-03-25 11:34:21 -07:00
|
|
|
InvalidateLastTexture();
|
2014-09-09 00:53:01 -07:00
|
|
|
timesInvalidatedAllThisFrame_ = 0;
|
2021-10-17 08:54:45 -07:00
|
|
|
replacementTimeThisFrame_ = 0.0;
|
2014-09-09 00:53:01 -07:00
|
|
|
|
|
|
|
if (texelsScaledThisFrame_) {
|
2020-09-13 15:57:26 +02:00
|
|
|
VERBOSE_LOG(G3D, "Scaled %i texels", texelsScaledThisFrame_);
|
2014-09-09 00:53:01 -07:00
|
|
|
}
|
|
|
|
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;
|
2017-03-02 11:39:02 +01:00
|
|
|
device_->SetSamplerState(0, D3DSAMP_MAXANISOTROPY, anisotropyLevel);
|
2016-03-17 21:56:04 -07:00
|
|
|
}
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-08-27 20:46:39 -07:00
|
|
|
if (replacer_.Enabled())
|
2020-08-28 01:15:22 -07:00
|
|
|
clutHash_ = XXH32((const char *)clutBufRaw_, clutExtendedBytes, 0xC0108888);
|
2020-08-27 20:46:39 -07:00
|
|
|
else
|
|
|
|
clutHash_ = XXH3_64bits((const char *)clutBufRaw_, clutExtendedBytes) & 0xFFFFFFFF;
|
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;
|
|
|
|
}
|
|
|
|
|
2017-02-19 23:07:00 +01:00
|
|
|
void TextureCacheDX9::BindTexture(TexCacheEntry *entry) {
|
|
|
|
LPDIRECT3DTEXTURE9 texture = DxTex(entry);
|
|
|
|
if (texture != lastBoundTexture) {
|
2017-03-02 11:39:02 +01:00
|
|
|
device_->SetTexture(0, texture);
|
2017-02-19 23:07:00 +01:00
|
|
|
lastBoundTexture = texture;
|
|
|
|
}
|
2020-09-13 23:46:57 +02:00
|
|
|
int maxLevel = (entry->status & TexCacheEntry::STATUS_BAD_MIPS) ? 0 : entry->maxLevel;
|
2021-02-27 17:17:21 -08:00
|
|
|
SamplerCacheKey samplerKey = GetSamplingParams(maxLevel, entry);
|
2020-09-13 23:46:57 +02:00
|
|
|
ApplySamplingParams(samplerKey);
|
2017-02-19 23:07:00 +01:00
|
|
|
}
|
|
|
|
|
2017-02-08 15:43:53 +01:00
|
|
|
void TextureCacheDX9::Unbind() {
|
2017-03-02 11:39:02 +01:00
|
|
|
device_->SetTexture(0, NULL);
|
2017-03-25 11:34:21 -07:00
|
|
|
InvalidateLastTexture();
|
2013-09-10 22:35:38 +02: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-03-02 11:39:02 +01:00
|
|
|
TextureShaderApplierDX9(LPDIRECT3DDEVICE9 device, LPDIRECT3DPIXELSHADER9 pshader, LPDIRECT3DVERTEXDECLARATION9 decl, float bufferW, float bufferH, int renderW, int renderH, float xoff, float yoff)
|
|
|
|
: device_(device), 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},
|
2018-11-24 10:47:49 -08:00
|
|
|
{ 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},
|
|
|
|
{0, 1},
|
2018-11-24 10:47:49 -08:00
|
|
|
{1, 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;
|
2018-11-24 10:47:49 -08:00
|
|
|
const float top = (bufferH_ - v1) * invHalfHeight - 1.0f + yoff;
|
|
|
|
const float bottom = (bufferH_ - v2) * invHalfHeight - 1.0f + yoff;
|
2017-03-17 10:00:28 +01:00
|
|
|
|
|
|
|
float z = 0.0f;
|
2018-11-24 10:47:49 -08:00
|
|
|
verts_[0].pos = Pos(left, top, z);
|
|
|
|
verts_[1].pos = Pos(right, top, z);
|
|
|
|
verts_[2].pos = Pos(left, bottom, z);
|
|
|
|
verts_[3].pos = Pos(right, bottom, z);
|
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;
|
2018-11-24 10:47:49 -08:00
|
|
|
|
|
|
|
verts_[0].uv = UV(uvleft, uvtop);
|
|
|
|
verts_[1].uv = UV(uvright, uvtop);
|
|
|
|
verts_[2].uv = UV(uvleft, uvbottom);
|
|
|
|
verts_[3].uv = UV(uvright, uvbottom);
|
2018-04-22 10:43:46 -07:00
|
|
|
|
|
|
|
// We need to reapply the texture next time since we cropped UV.
|
|
|
|
gstate_c.Dirty(DIRTY_TEXTURE_PARAMS);
|
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) {
|
2017-03-02 11:39:02 +01:00
|
|
|
device_->SetPixelShader(pshader_);
|
|
|
|
device_->SetVertexShader(vshader);
|
|
|
|
device_->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() {
|
2022-07-24 21:40:04 +02:00
|
|
|
// Intentionally bypassing the dxstate cache here (and using .Restore to recover afterwards). Not sure if this is a good idea.
|
2017-03-02 11:39:02 +01:00
|
|
|
device_->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
|
|
|
|
device_->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
|
|
|
|
device_->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_ALPHA);
|
|
|
|
device_->SetRenderState(D3DRS_ZENABLE, FALSE);
|
|
|
|
device_->SetRenderState(D3DRS_STENCILENABLE, FALSE);
|
|
|
|
device_->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
|
|
|
|
device_->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
|
2015-03-15 20:05:35 -07:00
|
|
|
|
2017-02-05 20:05:03 +01:00
|
|
|
D3DVIEWPORT9 vp{ 0, 0, (DWORD)renderW_, (DWORD)renderH_, 0.0f, 1.0f };
|
2017-03-02 11:39:02 +01:00
|
|
|
device_->SetViewport(&vp);
|
2018-11-24 10:47:49 -08:00
|
|
|
HRESULT hr = device_->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, verts_, (3 + 2) * sizeof(float));
|
2015-03-15 20:05:35 -07:00
|
|
|
if (FAILED(hr)) {
|
2021-02-14 09:59:04 -08:00
|
|
|
ERROR_LOG_REPORT(G3D, "Depal render failed: %08x", (uint32_t)hr);
|
2015-03-15 20:05:35 -07:00
|
|
|
}
|
|
|
|
dxstate.Restore();
|
2016-01-03 23:44:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2017-03-02 11:39:02 +01:00
|
|
|
LPDIRECT3DDEVICE9 device_;
|
2016-01-03 23:44:06 -08:00
|
|
|
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_;
|
|
|
|
};
|
|
|
|
|
2020-09-12 14:25:50 +02:00
|
|
|
void TextureCacheDX9::ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer, GETextureFormat texFormat, FramebufferNotificationChannel channel) {
|
2016-01-03 23:44:06 -08:00
|
|
|
LPDIRECT3DPIXELSHADER9 pshader = nullptr;
|
2017-04-04 11:09:29 +02:00
|
|
|
uint32_t clutMode = gstate.clutformat & 0xFFFFFF;
|
2020-09-12 14:25:50 +02:00
|
|
|
bool need_depalettize = IsClutFormat(texFormat);
|
|
|
|
if (need_depalettize && !g_Config.bDisableSlowFramebufEffects) {
|
2017-04-04 11:09:29 +02:00
|
|
|
pshader = depalShaderCache_->GetDepalettizePixelShader(clutMode, framebuffer->drawnFormat);
|
2016-01-03 23:44:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pshader) {
|
2017-04-04 11:09:29 +02:00
|
|
|
const GEPaletteFormat clutFormat = gstate.getClutPaletteFormat();
|
2016-01-03 23:44:06 -08:00
|
|
|
LPDIRECT3DTEXTURE9 clutTexture = depalShaderCache_->GetClutTexture(clutFormat, clutHash_, clutBuf_);
|
|
|
|
|
2020-11-05 14:51:46 +01:00
|
|
|
Draw::Framebuffer *depalFBO = framebufferManagerDX9_->GetTempFBO(TempFBO::DEPAL, framebuffer->renderWidth, framebuffer->renderHeight);
|
2020-05-21 11:24:05 +02:00
|
|
|
draw_->BindFramebufferAsRenderTarget(depalFBO, { Draw::RPAction::DONT_CARE, Draw::RPAction::DONT_CARE, Draw::RPAction::DONT_CARE }, "Depal");
|
2016-01-03 23:44:06 -08:00
|
|
|
shaderManager_->DirtyLastShader();
|
|
|
|
|
|
|
|
float xoff = -0.5f / framebuffer->renderWidth;
|
|
|
|
float yoff = 0.5f / framebuffer->renderHeight;
|
|
|
|
|
2017-03-02 11:39:02 +01:00
|
|
|
TextureShaderApplierDX9 shaderApply(device_, 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());
|
|
|
|
|
2017-03-02 11:39:02 +01:00
|
|
|
device_->SetTexture(1, clutTexture);
|
|
|
|
device_->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_POINT);
|
|
|
|
device_->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
|
|
|
|
device_->SetSamplerState(1, D3DSAMP_MIPFILTER, D3DTEXF_NONE);
|
2015-03-15 20:05:35 -07:00
|
|
|
|
2018-11-24 10:19:10 -08:00
|
|
|
framebufferManagerDX9_->BindFramebufferAsColorTexture(0, framebuffer, BINDFBCOLOR_SKIP_COPY | BINDFBCOLOR_FORCE_SELF);
|
2017-03-02 11:39:02 +01:00
|
|
|
device_->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
|
|
|
|
device_->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
|
|
|
|
device_->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE);
|
2017-04-22 18:36:25 -07:00
|
|
|
device_->SetSamplerState(0, D3DSAMP_MIPMAPLODBIAS, 0);
|
|
|
|
device_->SetSamplerState(0, D3DSAMP_MAXMIPLEVEL, 0);
|
2016-01-03 23:44:06 -08:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2022-04-15 12:34:50 +02:00
|
|
|
CheckAlphaResult alphaStatus = CheckAlpha(clutBuf_, getClutDestFormat(clutFormat), clutTotalColors);
|
|
|
|
gstate_c.SetTextureFullAlpha(alphaStatus == CHECKALPHA_FULL);
|
2015-03-15 20:05:35 -07:00
|
|
|
} else {
|
2017-02-17 14:30:42 +01:00
|
|
|
framebufferManagerDX9_->BindFramebufferAsColorTexture(0, framebuffer, BINDFBCOLOR_MAY_COPY_WITH_UV | BINDFBCOLOR_APPLY_TEX_OFFSET);
|
2016-05-21 15:40:58 -07:00
|
|
|
|
2017-04-03 17:04:58 +02:00
|
|
|
gstate_c.SetTextureFullAlpha(gstate.getTextureFormat() == GE_TFMT_5650);
|
2015-03-15 20:05:35 -07:00
|
|
|
}
|
|
|
|
|
2020-06-02 09:51:38 +02:00
|
|
|
framebufferManagerDX9_->RebindFramebuffer("RebindFramebuffer - ApplyTextureFromFramebuffer");
|
2020-09-13 23:25:21 +02:00
|
|
|
|
2020-09-13 23:46:57 +02:00
|
|
|
SamplerCacheKey samplerKey = GetFramebufferSamplingParams(framebuffer->bufferWidth, framebuffer->bufferHeight);
|
|
|
|
ApplySamplingParams(samplerKey);
|
2015-03-15 20:05:35 -07:00
|
|
|
}
|
|
|
|
|
2018-03-25 10:49:28 +02:00
|
|
|
void TextureCacheDX9::BuildTexture(TexCacheEntry *const entry) {
|
2016-05-01 16:20:05 -07:00
|
|
|
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);
|
|
|
|
|
2016-05-01 16:20:05 -07:00
|
|
|
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
|
|
|
|
2017-04-01 21:17:58 +02:00
|
|
|
// If size reaches 1, stop, and override maxlevel.
|
|
|
|
int tw = gstate.getTextureWidth(i);
|
|
|
|
int th = gstate.getTextureHeight(i);
|
|
|
|
if (tw == 1 || th == 1) {
|
|
|
|
maxLevel = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
if (tw != 1 && tw != (gstate.getTextureWidth(i - 1) >> 1))
|
|
|
|
badMipSizes = true;
|
|
|
|
else if (th != 1 && th != (gstate.getTextureHeight(i - 1) >> 1))
|
|
|
|
badMipSizes = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
int w = gstate.getTextureWidth(0);
|
|
|
|
int h = gstate.getTextureHeight(0);
|
2021-10-17 12:36:20 -07:00
|
|
|
ReplacedTexture &replaced = FindReplacement(entry, w, h);
|
|
|
|
if (replaced.Valid()) {
|
|
|
|
// We're replacing, so we won't scale.
|
|
|
|
scaleFactor = 1;
|
|
|
|
maxLevel = replaced.MaxLevel();
|
|
|
|
badMipSizes = false;
|
2016-04-30 13:44:31 -07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
// Seems to cause problems in Tactics Ogre.
|
|
|
|
if (badMipSizes) {
|
|
|
|
maxLevel = 0;
|
|
|
|
}
|
|
|
|
|
2020-12-10 10:28:48 +01:00
|
|
|
int level = 0;
|
2017-02-15 00:56:53 +09:00
|
|
|
if (IsFakeMipmapChange()) {
|
2017-05-31 21:42:07 -07:00
|
|
|
// NOTE: Since the level is not part of the cache key, we assume it never changes.
|
2022-04-22 22:58:47 +02:00
|
|
|
level = std::max(0, gstate.getTexLevelOffset16() / 16);
|
2017-02-21 11:29:51 +01:00
|
|
|
}
|
2020-12-10 10:28:48 +01:00
|
|
|
|
2015-03-15 19:46:36 -07:00
|
|
|
LPDIRECT3DTEXTURE9 &texture = DxTex(entry);
|
2020-12-10 11:18:43 +01:00
|
|
|
// Create texture
|
|
|
|
D3DPOOL pool = D3DPOOL_MANAGED;
|
|
|
|
int usage = 0;
|
|
|
|
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;
|
|
|
|
D3DFORMAT tfmt = (D3DFORMAT)(dstFmt);
|
|
|
|
if (replaced.GetSize(level, tw, th)) {
|
|
|
|
tfmt = ToD3D9Format(replaced.Format(level));
|
|
|
|
} else {
|
|
|
|
tw *= scaleFactor;
|
|
|
|
th *= scaleFactor;
|
|
|
|
if (scaleFactor > 1) {
|
|
|
|
tfmt = D3DFMT_A8R8G8B8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
HRESULT hr;
|
|
|
|
if (IsFakeMipmapChange())
|
|
|
|
hr = device_->CreateTexture(tw, th, 1, usage, tfmt, pool, &texture, NULL);
|
|
|
|
else
|
|
|
|
hr = device_->CreateTexture(tw, th, levels, usage, tfmt, pool, &texture, NULL);
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
INFO_LOG(G3D, "Failed to create D3D texture: %dx%d", tw, th);
|
|
|
|
ReleaseTexture(entry, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadTextureLevel(*entry, replaced, level, 0, maxLevel, scaleFactor, dstFmt);
|
|
|
|
|
2015-03-15 19:46:36 -07:00
|
|
|
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++) {
|
2020-12-10 11:18:43 +01:00
|
|
|
LoadTextureLevel(*entry, replaced, i, i, maxLevel, scaleFactor, dstFmt);
|
2014-09-14 09:13:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 21:23:20 -07:00
|
|
|
if (maxLevel == 0) {
|
|
|
|
entry->status |= TexCacheEntry::STATUS_BAD_MIPS;
|
|
|
|
} else {
|
|
|
|
entry->status &= ~TexCacheEntry::STATUS_BAD_MIPS;
|
|
|
|
}
|
2016-04-30 13:44:31 -07:00
|
|
|
if (replaced.Valid()) {
|
2017-12-13 23:11:40 +01:00
|
|
|
entry->SetAlphaStatus(TexCacheEntry::TexStatus(replaced.AlphaStatus()));
|
2016-04-30 13:44:31 -07:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2022-04-15 12:34:50 +02:00
|
|
|
CheckAlphaResult TextureCacheDX9::CheckAlpha(const u32 *pixelData, u32 dstFmt, int w) {
|
2013-08-17 11:23:51 +02:00
|
|
|
switch (dstFmt) {
|
|
|
|
case D3DFMT_A4R4G4B4:
|
2022-04-15 12:34:50 +02:00
|
|
|
return CheckAlpha16((const u16 *)pixelData, w, 0xF000);
|
2013-08-17 11:23:51 +02:00
|
|
|
case D3DFMT_A1R5G5B5:
|
2022-04-15 12:34:50 +02:00
|
|
|
return CheckAlpha16((const u16 *)pixelData, w, 0x8000);
|
2013-08-17 11:23:51 +02:00
|
|
|
case D3DFMT_R5G6B5:
|
2015-05-24 22:55:43 -07:00
|
|
|
// Never has any alpha.
|
2022-04-15 12:34:50 +02:00
|
|
|
return CHECKALPHA_FULL;
|
2013-08-17 11:23:51 +02:00
|
|
|
default:
|
2022-04-15 12:34:50 +02:00
|
|
|
return CheckAlpha32(pixelData, w, 0xFF000000);
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 11:18:43 +01:00
|
|
|
void TextureCacheDX9::LoadTextureLevel(TexCacheEntry &entry, ReplacedTexture &replaced, int srcLevel, int dstLevel, int maxLevel, int scaleFactor, u32 dstFmt) {
|
|
|
|
int w = gstate.getTextureWidth(srcLevel);
|
|
|
|
int h = gstate.getTextureHeight(srcLevel);
|
2013-08-17 11:23:51 +02:00
|
|
|
|
2016-06-25 09:13:14 -07:00
|
|
|
LPDIRECT3DTEXTURE9 &texture = DxTex(&entry);
|
|
|
|
D3DLOCKED_RECT rect;
|
2017-02-22 18:27:46 +01:00
|
|
|
|
|
|
|
HRESULT result;
|
2020-12-10 11:18:43 +01:00
|
|
|
uint32_t lockFlag = dstLevel == 0 ? D3DLOCK_DISCARD : 0; // Can only discard the top level
|
|
|
|
result = texture->LockRect(dstLevel, &rect, NULL, lockFlag);
|
2017-02-22 18:27:46 +01:00
|
|
|
if (FAILED(result)) {
|
|
|
|
ERROR_LOG(G3D, "Failed to lock D3D texture: %dx%d", w, h);
|
|
|
|
return;
|
|
|
|
}
|
2016-06-25 09:13:14 -07:00
|
|
|
|
2013-08-17 11:23:51 +02:00
|
|
|
gpuStats.numTexturesDecoded++;
|
2020-12-10 11:18:43 +01:00
|
|
|
if (replaced.GetSize(srcLevel, w, h)) {
|
2021-10-17 08:54:45 -07:00
|
|
|
double replaceStart = time_now_d();
|
2020-12-10 11:18:43 +01:00
|
|
|
replaced.Load(srcLevel, rect.pBits, rect.Pitch);
|
2021-10-17 08:54:45 -07:00
|
|
|
replacementTimeThisFrame_ += time_now_d() - replaceStart;
|
2020-12-10 11:18:43 +01:00
|
|
|
dstFmt = ToD3D9Format(replaced.Format(srcLevel));
|
2016-04-30 13:44:31 -07:00
|
|
|
} 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();
|
2020-12-10 11:18:43 +01:00
|
|
|
u32 texaddr = gstate.getTextureAddress(srcLevel);
|
|
|
|
int bufw = GetTextureBufw(srcLevel, texaddr, tfmt);
|
2016-06-25 09:13:14 -07:00
|
|
|
int bpp = dstFmt == D3DFMT_A8R8G8B8 ? 4 : 2;
|
|
|
|
|
|
|
|
u32 *pixelData = (u32 *)rect.pBits;
|
|
|
|
int decPitch = rect.Pitch;
|
|
|
|
if (scaleFactor > 1) {
|
2017-02-20 00:19:58 +01:00
|
|
|
tmpTexBufRearrange_.resize(std::max(bufw, w) * h);
|
|
|
|
pixelData = tmpTexBufRearrange_.data();
|
2016-06-25 09:13:14 -07:00
|
|
|
// We want to end up with a neatly packed texture for scaling.
|
|
|
|
decPitch = w * bpp;
|
|
|
|
}
|
|
|
|
|
2020-12-10 11:18:43 +01:00
|
|
|
CheckAlphaResult alphaResult = DecodeTextureLevel((u8 *)pixelData, decPitch, tfmt, clutformat, texaddr, srcLevel, bufw, false, false);
|
|
|
|
entry.SetAlphaStatus(alphaResult, srcLevel);
|
2017-11-12 16:19:28 -08: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.
|
2020-08-16 12:48:09 +02:00
|
|
|
_assert_(dstFmt == D3DFMT_A8R8G8B8);
|
2016-06-25 09:13:14 -07:00
|
|
|
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
|
|
|
|
2017-02-20 00:19:58 +01:00
|
|
|
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;
|
2021-02-20 20:59:04 -08:00
|
|
|
replacedInfo.isVideo = IsVideo(entry.addr);
|
2016-05-01 08:54:43 -07:00
|
|
|
replacedInfo.isFinal = (entry.status & TexCacheEntry::STATUS_TO_SCALE) == 0;
|
|
|
|
replacedInfo.scaleFactor = scaleFactor;
|
|
|
|
replacedInfo.fmt = FromD3D9Format(dstFmt);
|
|
|
|
|
2020-12-10 11:18:43 +01:00
|
|
|
replacer_.NotifyTextureDecoded(replacedInfo, pixelData, decPitch, srcLevel, w, h);
|
2014-09-14 09:13:06 -07:00
|
|
|
}
|
|
|
|
}
|
2013-08-27 16:31:06 +02:00
|
|
|
|
2020-12-10 11:18:43 +01:00
|
|
|
texture->UnlockRect(0);
|
2013-08-17 11:23:51 +02:00
|
|
|
}
|
|
|
|
|
2017-10-18 13:03:49 +02:00
|
|
|
bool TextureCacheDX9::GetCurrentTextureDebug(GPUDebugBuffer &buffer, int level) {
|
2020-09-20 20:35:42 +02:00
|
|
|
SetTexture();
|
2017-10-18 13:03:49 +02:00
|
|
|
ApplyTexture();
|
|
|
|
|
|
|
|
LPDIRECT3DBASETEXTURE9 baseTex;
|
|
|
|
LPDIRECT3DTEXTURE9 tex;
|
|
|
|
LPDIRECT3DSURFACE9 offscreen = nullptr;
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
bool success = false;
|
|
|
|
hr = device_->GetTexture(0, &baseTex);
|
|
|
|
if (SUCCEEDED(hr) && baseTex != NULL) {
|
|
|
|
hr = baseTex->QueryInterface(IID_IDirect3DTexture9, (void **)&tex);
|
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
D3DSURFACE_DESC desc;
|
|
|
|
D3DLOCKED_RECT locked;
|
|
|
|
tex->GetLevelDesc(level, &desc);
|
|
|
|
RECT rect = { 0, 0, (LONG)desc.Width, (LONG)desc.Height };
|
|
|
|
hr = tex->LockRect(level, &locked, &rect, D3DLOCK_READONLY);
|
|
|
|
|
|
|
|
// If it fails, this means it's a render-to-texture, so we have to get creative.
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LPDIRECT3DSURFACE9 renderTarget = nullptr;
|
|
|
|
hr = tex->GetSurfaceLevel(level, &renderTarget);
|
|
|
|
if (renderTarget && SUCCEEDED(hr)) {
|
|
|
|
hr = device_->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &offscreen, NULL);
|
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
hr = device_->GetRenderTargetData(renderTarget, offscreen);
|
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
hr = offscreen->LockRect(&locked, &rect, D3DLOCK_READONLY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
renderTarget->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
GPUDebugBufferFormat fmt;
|
|
|
|
int pixelSize;
|
|
|
|
switch (desc.Format) {
|
|
|
|
case D3DFMT_A1R5G5B5:
|
|
|
|
fmt = gstate_c.bgraTexture ? GPU_DBG_FORMAT_5551 : GPU_DBG_FORMAT_5551_BGRA;
|
|
|
|
pixelSize = 2;
|
|
|
|
break;
|
|
|
|
case D3DFMT_A4R4G4B4:
|
|
|
|
fmt = gstate_c.bgraTexture ? GPU_DBG_FORMAT_4444 : GPU_DBG_FORMAT_4444_BGRA;
|
|
|
|
pixelSize = 2;
|
|
|
|
break;
|
|
|
|
case D3DFMT_R5G6B5:
|
|
|
|
fmt = gstate_c.bgraTexture ? GPU_DBG_FORMAT_565 : GPU_DBG_FORMAT_565_BGRA;
|
|
|
|
pixelSize = 2;
|
|
|
|
break;
|
|
|
|
case D3DFMT_A8R8G8B8:
|
|
|
|
fmt = gstate_c.bgraTexture ? GPU_DBG_FORMAT_8888 : GPU_DBG_FORMAT_8888_BGRA;
|
|
|
|
pixelSize = 4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fmt = GPU_DBG_FORMAT_INVALID;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fmt != GPU_DBG_FORMAT_INVALID) {
|
|
|
|
buffer.Allocate(locked.Pitch / pixelSize, desc.Height, fmt, false);
|
|
|
|
memcpy(buffer.GetData(), locked.pBits, locked.Pitch * desc.Height);
|
|
|
|
success = true;
|
|
|
|
} else {
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
if (offscreen) {
|
|
|
|
offscreen->UnlockRect();
|
|
|
|
offscreen->Release();
|
|
|
|
} else {
|
|
|
|
tex->UnlockRect(level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tex->Release();
|
|
|
|
}
|
|
|
|
baseTex->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2013-09-15 08:53:21 -07:00
|
|
|
};
|