From bf39e61458e5dce539181e59ae9f247c566c4bd2 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 30 Apr 2016 13:44:31 -0700 Subject: [PATCH] Setup initial structure for texture replacements. --- CMakeLists.txt | 2 + GPU/Common/TextureCacheCommon.cpp | 4 + GPU/Common/TextureCacheCommon.h | 5 +- GPU/Common/TextureReplacer.cpp | 47 ++++++++++ GPU/Common/TextureReplacer.h | 105 ++++++++++++++++++++++ GPU/Directx9/TextureCacheDX9.cpp | 105 +++++++++++++++++----- GPU/Directx9/TextureCacheDX9.h | 2 +- GPU/GLES/TextureCache.cpp | 141 ++++++++++++++++++++++-------- GPU/GLES/TextureCache.h | 2 +- GPU/GPU.vcxproj | 2 + GPU/GPU.vcxproj.filters | 6 ++ GPU/Vulkan/TextureCacheVulkan.cpp | 69 +++++++++++++-- Qt/GPU.pro | 1 + android/jni/Android.mk | 1 + 14 files changed, 425 insertions(+), 67 deletions(-) create mode 100644 GPU/Common/TextureReplacer.cpp create mode 100644 GPU/Common/TextureReplacer.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d78af8512..5986403a05 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1491,6 +1491,8 @@ add_library(GPU OBJECT GPU/Common/TextureDecoder.h GPU/Common/TextureCacheCommon.cpp GPU/Common/TextureCacheCommon.h + GPU/Common/TextureReplacer.cpp + GPU/Common/TextureReplacer.h GPU/Common/TextureScalerCommon.cpp GPU/Common/TextureScalerCommon.h ${GPU_NEON} diff --git a/GPU/Common/TextureCacheCommon.cpp b/GPU/Common/TextureCacheCommon.cpp index 0c607e7a43..67345cd33a 100644 --- a/GPU/Common/TextureCacheCommon.cpp +++ b/GPU/Common/TextureCacheCommon.cpp @@ -51,6 +51,8 @@ TextureCacheCommon::TextureCacheCommon() tmpTexBuf32.resize(512 * 512); // 1MB tmpTexBuf16.resize(512 * 512); // 0.5MB tmpTexBufRearrange.resize(512 * 512); // 1MB + + replacer.Init(); } TextureCacheCommon::~TextureCacheCommon() { @@ -287,6 +289,8 @@ void TextureCacheCommon::NotifyConfigChanged() { } standardScaleFactor_ = scaleFactor; + + replacer.NotifyConfigChanged(); } void TextureCacheCommon::LoadClut(u32 clutAddr, u32 loadBytes) { diff --git a/GPU/Common/TextureCacheCommon.h b/GPU/Common/TextureCacheCommon.h index d2af36993e..17da5fbf76 100644 --- a/GPU/Common/TextureCacheCommon.h +++ b/GPU/Common/TextureCacheCommon.h @@ -21,8 +21,9 @@ #include #include "Common/CommonTypes.h" -#include "GPU/Common/GPUDebugInterface.h" #include "Common/MemoryUtil.h" +#include "GPU/Common/GPUDebugInterface.h" +#include "GPU/Common/TextureReplacer.h" enum TextureFiltering { TEX_FILTER_AUTO = 1, @@ -154,6 +155,8 @@ protected: virtual void DownloadFramebufferForClut(u32 clutAddr, u32 bytes) = 0; + TextureReplacer replacer; + TexCache cache; u32 cacheSizeEstimate_; diff --git a/GPU/Common/TextureReplacer.cpp b/GPU/Common/TextureReplacer.cpp new file mode 100644 index 0000000000..7a8fdd2997 --- /dev/null +++ b/GPU/Common/TextureReplacer.cpp @@ -0,0 +1,47 @@ +// Copyright (c) 2016- 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 "GPU/Common/TextureReplacer.h" + +TextureReplacer::TextureReplacer() : enabled_(false) { +} + +TextureReplacer::~TextureReplacer() { +} + + +void TextureReplacer::Init() { +} + +void TextureReplacer::NotifyConfigChanged() { +} + +u32 TextureReplacer::ComputeHash(u32 addr, int bufw, int w, int h, GETextureFormat fmt, u16 maxSeenV) { + return 0; +} + +ReplacedTexture TextureReplacer::FindReplacement(u32 hash) { + ReplacedTexture result; + result.alphaStatus_ = ReplacedTextureAlpha::UNKNOWN; + return result; +} + +void TextureReplacer::NotifyTextureDecoded(u32 hash, const void *data, int pitch, int w, int h, ReplacedTextureFormat fmt) { +} + +void ReplacedTexture::Load(int level, void *out, int rowPitch) { +} diff --git a/GPU/Common/TextureReplacer.h b/GPU/Common/TextureReplacer.h new file mode 100644 index 0000000000..b7a3f1e8b0 --- /dev/null +++ b/GPU/Common/TextureReplacer.h @@ -0,0 +1,105 @@ +// Copyright (c) 2016- 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/. + +#pragma once + +#include +#include "Common/Common.h" +#include "GPU/ge_constants.h" + +class TextureCacheCommon; +class TextureReplacer; + +enum class ReplacedTextureFormat { + F_5650, + F_5551, + F_4444, + F_8888, +}; + +// These must match the constants in TextureCacheCommon. +enum class ReplacedTextureAlpha { + UNKNOWN = 0x04, + FULL = 0x00, + SIMPLE = 0x08, +}; + +struct ReplacedTexureLevel { + int w; + int h; + ReplacedTextureFormat fmt; +}; + +struct ReplacedTexture { + inline bool Valid() { + return !levels_.empty(); + } + + bool GetSize(int level, int &w, int &h) { + if ((size_t)level < levels_.size()) { + w = levels_[level].w; + h = levels_[level].h; + return true; + } + return false; + } + + int MaxLevel() { + return (int)levels_.size() - 1; + } + + ReplacedTextureFormat Format(int level) { + if ((size_t)level < levels_.size()) { + return levels_[level].fmt; + } + return ReplacedTextureFormat::F_8888; + } + + u8 AlphaStatus() { + return (u8)alphaStatus_; + } + + void Load(int level, void *out, int rowPitch); + +protected: + std::vector levels_; + ReplacedTextureAlpha alphaStatus_; + + friend TextureReplacer; +}; + +class TextureReplacer { +public: + TextureReplacer(); + ~TextureReplacer(); + + void Init(); + void NotifyConfigChanged(); + + inline bool Enabled() { + return enabled_; + } + + u32 ComputeHash(u32 addr, int bufw, int w, int h, GETextureFormat fmt, u16 maxSeenV); + + ReplacedTexture FindReplacement(u32 hash); + + void NotifyTextureDecoded(u32 hash, const void *data, int pitch, int w, int h, ReplacedTextureFormat fmt); + +protected: + bool enabled_; +}; diff --git a/GPU/Directx9/TextureCacheDX9.cpp b/GPU/Directx9/TextureCacheDX9.cpp index 37c6e833fa..31bd902e83 100644 --- a/GPU/Directx9/TextureCacheDX9.cpp +++ b/GPU/Directx9/TextureCacheDX9.cpp @@ -585,7 +585,11 @@ static inline u32 MiniHash(const u32 *ptr) { return ptr[0]; } -static inline u32 QuickTexHash(u32 addr, int bufw, int w, int h, GETextureFormat format, TextureCacheDX9::TexCacheEntry *entry) { +static inline u32 QuickTexHash(TextureReplacer &replacer, u32 addr, int bufw, int w, int h, GETextureFormat format, TextureCacheDX9::TexCacheEntry *entry) { + if (replacer.Enabled()) { + return replacer.ComputeHash(addr, bufw, w, h, format, entry->maxSeenV); + } + if (h == 512 && entry->maxSeenV < 512 && entry->maxSeenV != 0) { h = (int)entry->maxSeenV; } @@ -1035,13 +1039,13 @@ void TextureCacheDX9::SetTexture(bool force) { bool hashFail = false; if (texhash != entry->hash) { - fullhash = QuickTexHash(texaddr, bufw, w, h, format, entry); + fullhash = QuickTexHash(replacer, texaddr, bufw, w, h, format, entry); hashFail = true; rehash = false; } if (rehash && entry->GetHashStatus() != TexCacheEntry::STATUS_RELIABLE) { - fullhash = QuickTexHash(texaddr, bufw, w, h, format, entry); + fullhash = QuickTexHash(replacer, texaddr, bufw, w, h, format, entry); if (fullhash != entry->fullhash) { hashFail = true; } else { @@ -1188,7 +1192,7 @@ void TextureCacheDX9::SetTexture(bool force) { // to avoid excessive clearing caused by cache invalidations. entry->sizeInRAM = (textureBitsPerPixel[format] * bufw * h / 2) / 8; - entry->fullhash = fullhash == 0 ? QuickTexHash(texaddr, bufw, w, h, format, entry) : fullhash; + entry->fullhash = fullhash == 0 ? QuickTexHash(replacer, texaddr, bufw, w, h, format, entry) : fullhash; entry->cluthash = cluthash; entry->status &= ~TexCacheEntry::STATUS_ALPHA_MASK; @@ -1251,6 +1255,16 @@ void TextureCacheDX9::SetTexture(bool force) { scaleFactor = scaleFactor > 4 ? 4 : (scaleFactor > 2 ? 2 : 1); } + ReplacedTexture replaced = replacer.FindReplacement(entry->fullhash); + if (replaced.GetSize(0, w, h)) { + // We're replacing, so we won't scale. + scaleFactor = 1; + if (g_Config.bMipMap) { + maxLevel = replaced.MaxLevel(); + badMipSizes = false; + } + } + // Don't scale the PPGe texture. if (entry->addr > 0x05000000 && entry->addr < 0x08800000) scaleFactor = 1; @@ -1279,7 +1293,7 @@ void TextureCacheDX9::SetTexture(bool force) { maxLevel = 0; } - LoadTextureLevel(*entry, 0, maxLevel, replaceImages, scaleFactor, dstFmt); + LoadTextureLevel(*entry, replaced, 0, maxLevel, replaceImages, scaleFactor, dstFmt); LPDIRECT3DTEXTURE9 &texture = DxTex(entry); if (!texture) { return; @@ -1288,10 +1302,14 @@ void TextureCacheDX9::SetTexture(bool force) { // Mipmapping is only enabled when texture scaling is disabled. if (maxLevel > 0 && scaleFactor == 1) { for (u32 i = 1; i <= maxLevel; i++) { - LoadTextureLevel(*entry, i, maxLevel, replaceImages, scaleFactor, dstFmt); + LoadTextureLevel(*entry, replaced, i, maxLevel, replaceImages, scaleFactor, dstFmt); } } + if (replaced.Valid()) { + entry->SetAlphaStatus(TexCacheEntry::Status(replaced.AlphaStatus())); + } + gstate_c.textureFullAlpha = entry->GetAlphaStatus() == TexCacheEntry::STATUS_ALPHA_FULL; gstate_c.textureSimpleAlpha = entry->GetAlphaStatus() != TexCacheEntry::STATUS_ALPHA_UNKNOWN; @@ -1590,31 +1608,74 @@ static inline void copyTexture(int xoffset, int yoffset, int w, int h, int pitch } } -void TextureCacheDX9::LoadTextureLevel(TexCacheEntry &entry, int level, int maxLevel, bool replaceImages, int scaleFactor, u32 dstFmt) { +ReplacedTextureFormat FromD3D9Format(u32 fmt) { + switch (fmt) { + 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; + } +} + +u32 ToD3D9Format(ReplacedTextureFormat fmt) { + switch (fmt) { + 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; + } +} + +void TextureCacheDX9::LoadTextureLevel(TexCacheEntry &entry, ReplacedTexture &replaced, int level, int maxLevel, bool replaceImages, int scaleFactor, u32 dstFmt) { // TODO: only do this once u32 texByteAlign = 1; - GEPaletteFormat clutformat = gstate.getClutPaletteFormat(); - int bufw; - void *finalBuf = DecodeTextureLevel(GETextureFormat(entry.format), clutformat, level, texByteAlign, dstFmt, &bufw); - if (finalBuf == NULL) { - return; - } - int w = gstate.getTextureWidth(level); int h = gstate.getTextureHeight(level); + int bufw; + u32 *pixelData; gpuStats.numTexturesDecoded++; + if (replaced.GetSize(level, w, h)) { + tmpTexBufRearrange.resize(w * h); + int bpp = replaced.Format(level) == ReplacedTextureFormat::F_8888 ? 4 : 2; + bufw = w; + replaced.Load(level, tmpTexBufRearrange.data(), bpp * w); + pixelData = tmpTexBufRearrange.data(); - u32 *pixelData = (u32 *)finalBuf; - if (scaleFactor > 1 && (entry.status & TexCacheEntry::STATUS_CHANGE_FREQUENT) == 0) - scaler.Scale(pixelData, dstFmt, w, h, scaleFactor); - - if ((entry.status & TexCacheEntry::STATUS_CHANGE_FREQUENT) == 0) { - TexCacheEntry::Status alphaStatus = CheckAlpha(pixelData, dstFmt, w, w, h); - entry.SetAlphaStatus(alphaStatus, level); + dstFmt = ToD3D9Format(replaced.Format(level)); } else { - entry.SetAlphaStatus(TexCacheEntry::STATUS_ALPHA_UNKNOWN); + GEPaletteFormat clutformat = gstate.getClutPaletteFormat(); + void *finalBuf = DecodeTextureLevel(GETextureFormat(entry.format), clutformat, level, texByteAlign, dstFmt, &bufw); + if (finalBuf == NULL) { + return; + } + + pixelData = (u32 *)finalBuf; + if (scaleFactor > 1 && (entry.status & TexCacheEntry::STATUS_CHANGE_FREQUENT) == 0) + scaler.Scale(pixelData, dstFmt, w, h, scaleFactor); + + if ((entry.status & TexCacheEntry::STATUS_CHANGE_FREQUENT) == 0) { + TexCacheEntry::Status alphaStatus = CheckAlpha(pixelData, dstFmt, w, w, h); + entry.SetAlphaStatus(alphaStatus, level); + } else { + entry.SetAlphaStatus(TexCacheEntry::STATUS_ALPHA_UNKNOWN); + } + + if (replacer.Enabled()) { + int bpp = dstFmt == D3DFMT_A8R8G8B8 ? 4 : 2; + replacer.NotifyTextureDecoded(entry.fullhash, pixelData, w * bpp, w, h, FromD3D9Format(dstFmt)); + } } LPDIRECT3DTEXTURE9 &texture = DxTex(&entry); diff --git a/GPU/Directx9/TextureCacheDX9.h b/GPU/Directx9/TextureCacheDX9.h index fea05abbda..ff9db0fa20 100644 --- a/GPU/Directx9/TextureCacheDX9.h +++ b/GPU/Directx9/TextureCacheDX9.h @@ -80,7 +80,7 @@ private: void DeleteTexture(TexCache::iterator it); void *ReadIndexedTex(int level, const u8 *texptr, int bytesPerIndex, u32 dstFmt, int bufw); void UpdateSamplingParams(TexCacheEntry &entry, bool force); - void LoadTextureLevel(TexCacheEntry &entry, int level, int maxLevel, bool replaceImages, int scaleFactor, u32 dstFmt); + void LoadTextureLevel(TexCacheEntry &entry, ReplacedTexture &replaced, int level, int maxLevel, bool replaceImages, int scaleFactor, u32 dstFmt); D3DFORMAT GetDestFormat(GETextureFormat format, GEPaletteFormat clutFormat) const; void *DecodeTextureLevel(GETextureFormat format, GEPaletteFormat clutformat, int level, u32 &texByteAlign, u32 &dstFmt, int *bufw = 0); TexCacheEntry::Status CheckAlpha(const u32 *pixelData, u32 dstFmt, int stride, int w, int h); diff --git a/GPU/GLES/TextureCache.cpp b/GPU/GLES/TextureCache.cpp index 50bee2cd62..d5a9599912 100644 --- a/GPU/GLES/TextureCache.cpp +++ b/GPU/GLES/TextureCache.cpp @@ -606,7 +606,11 @@ static inline u32 MiniHash(const u32 *ptr) { return ptr[0]; } -static inline u32 QuickTexHash(u32 addr, int bufw, int w, int h, GETextureFormat format, TextureCache::TexCacheEntry *entry) { +static inline u32 QuickTexHash(TextureReplacer &replacer, u32 addr, int bufw, int w, int h, GETextureFormat format, TextureCache::TexCacheEntry *entry) { + if (replacer.Enabled()) { + return replacer.ComputeHash(addr, bufw, w, h, format, entry->maxSeenV); + } + if (h == 512 && entry->maxSeenV < 512 && entry->maxSeenV != 0) { h = (int)entry->maxSeenV; } @@ -989,6 +993,35 @@ bool TextureCache::SetOffsetTexture(u32 offset) { return false; } +ReplacedTextureFormat FromGLESFormat(GLenum fmt) { + // TODO: 16-bit formats are incorrect, since swizzled. + switch (fmt) { + case GL_UNSIGNED_SHORT_5_6_5: + return ReplacedTextureFormat::F_5650; + case GL_UNSIGNED_SHORT_5_5_5_1: + return ReplacedTextureFormat::F_5551; + case GL_UNSIGNED_SHORT_4_4_4_4: + return ReplacedTextureFormat::F_4444; + case GL_UNSIGNED_BYTE: + default: + return ReplacedTextureFormat::F_8888; + } +} + +GLenum ToGLESFormat(ReplacedTextureFormat fmt) { + switch (fmt) { + case ReplacedTextureFormat::F_5650: + return GL_UNSIGNED_SHORT_5_6_5; + case ReplacedTextureFormat::F_5551: + return GL_UNSIGNED_SHORT_5_5_5_1; + case ReplacedTextureFormat::F_4444: + return GL_UNSIGNED_SHORT_4_4_4_4; + case ReplacedTextureFormat::F_8888: + default: + return GL_UNSIGNED_BYTE; + } +} + void TextureCache::SetTexture(bool force) { #ifdef DEBUG_TEXTURES if (SetDebugTexture()) { @@ -1111,13 +1144,13 @@ void TextureCache::SetTexture(bool force) { bool hashFail = false; if (texhash != entry->hash) { - fullhash = QuickTexHash(texaddr, bufw, w, h, format, entry); + fullhash = QuickTexHash(replacer, texaddr, bufw, w, h, format, entry); hashFail = true; rehash = false; } if (rehash && entry->GetHashStatus() != TexCacheEntry::STATUS_RELIABLE) { - fullhash = QuickTexHash(texaddr, bufw, w, h, format, entry); + fullhash = QuickTexHash(replacer, texaddr, bufw, w, h, format, entry); if (fullhash != entry->fullhash) { hashFail = true; } else { @@ -1264,7 +1297,7 @@ void TextureCache::SetTexture(bool force) { // to avoid excessive clearing caused by cache invalidations. entry->sizeInRAM = (textureBitsPerPixel[format] * bufw * h / 2) / 8; - entry->fullhash = fullhash == 0 ? QuickTexHash(texaddr, bufw, w, h, format, entry) : fullhash; + entry->fullhash = fullhash == 0 ? QuickTexHash(replacer, texaddr, bufw, w, h, format, entry) : fullhash; entry->cluthash = cluthash; entry->status &= ~TexCacheEntry::STATUS_ALPHA_MASK; @@ -1331,6 +1364,16 @@ void TextureCache::SetTexture(bool force) { scaleFactor = scaleFactor > 4 ? 4 : (scaleFactor > 2 ? 2 : 1); } + ReplacedTexture replaced = replacer.FindReplacement(entry->fullhash); + if (replaced.GetSize(0, w, h)) { + // We're replacing, so we won't scale. + scaleFactor = 1; + if (g_Config.bMipMap) { + maxLevel = replaced.MaxLevel(); + badMipSizes = false; + } + } + // Don't scale the PPGe texture. if (entry->addr > 0x05000000 && entry->addr < 0x08800000) scaleFactor = 1; @@ -1355,14 +1398,15 @@ void TextureCache::SetTexture(bool force) { // glTexStorage2D probably has few benefits for us anyway. if (false && gl_extensions.GLES3 && maxLevel > 0) { // glTexStorage2D requires the use of sized formats. + GLenum actualFmt = replaced.Valid() ? ToGLESFormat(replaced.Format(0)) : dstFmt; GLenum storageFmt = GL_RGBA8; - switch (dstFmt) { + switch (actualFmt) { case GL_UNSIGNED_BYTE: storageFmt = GL_RGBA8; break; case GL_UNSIGNED_SHORT_5_6_5: storageFmt = GL_RGB565; break; case GL_UNSIGNED_SHORT_4_4_4_4: storageFmt = GL_RGBA4; break; case GL_UNSIGNED_SHORT_5_5_5_1: storageFmt = GL_RGB5_A1; break; default: - ERROR_LOG(G3D, "Unknown dstfmt %i", (int)dstFmt); + ERROR_LOG(G3D, "Unknown dstfmt %i", (int)actualFmt); break; } // TODO: This may cause bugs, since it hard-sets the texture w/h, and we might try to reuse it later with a different size. @@ -1377,7 +1421,7 @@ void TextureCache::SetTexture(bool force) { // be as good quality as the game's own (might even be better in some cases though). // Always load base level texture here - LoadTextureLevel(*entry, 0, replaceImages, scaleFactor, dstFmt); + LoadTextureLevel(*entry, replaced, 0, replaceImages, scaleFactor, dstFmt); // Mipmapping only enable when texture scaling disable if (maxLevel > 0 && scaleFactor == 1) { @@ -1387,7 +1431,7 @@ void TextureCache::SetTexture(bool force) { glGenerateMipmap(GL_TEXTURE_2D); } else { for (int i = 1; i <= maxLevel; i++) { - LoadTextureLevel(*entry, i, replaceImages, scaleFactor, dstFmt); + LoadTextureLevel(*entry, replaced, i, replaceImages, scaleFactor, dstFmt); } glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, maxLevel); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, (float)maxLevel); @@ -1405,6 +1449,10 @@ void TextureCache::SetTexture(bool force) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0); } + if (replaced.Valid()) { + entry->SetAlphaStatus(TexCacheEntry::Status(replaced.AlphaStatus())); + } + if (gstate_c.Supports(GPU_SUPPORTS_ANISOTROPY)) { int aniso = 1 << g_Config.iAnisotropyLevel; float anisotropyLevel = (float) aniso > maxAnisotropyLevel ? maxAnisotropyLevel : (float) aniso; @@ -1709,50 +1757,69 @@ TextureCache::TexCacheEntry::Status TextureCache::CheckAlpha(const u32 *pixelDat return (TexCacheEntry::Status)res; } -void TextureCache::LoadTextureLevel(TexCacheEntry &entry, int level, bool replaceImages, int scaleFactor, GLenum dstFmt) { +void TextureCache::LoadTextureLevel(TexCacheEntry &entry, ReplacedTexture &replaced, int level, bool replaceImages, int scaleFactor, GLenum dstFmt) { int w = gstate.getTextureWidth(level); int h = gstate.getTextureHeight(level); bool useUnpack = false; bool useBGRA; u32 *pixelData; - { - - PROFILE_THIS_SCOPE("decodetex"); // TODO: only do this once u32 texByteAlign = 1; - GEPaletteFormat clutformat = gstate.getClutPaletteFormat(); - int bufw; - void *finalBuf = DecodeTextureLevel(GETextureFormat(entry.format), clutformat, level, texByteAlign, dstFmt, scaleFactor, &bufw); - if (finalBuf == NULL) { - return; - } - gpuStats.numTexturesDecoded++; - // Can restore these and remove the fixup at the end of DecodeTextureLevel on desktop GL and GLES 3. - if (scaleFactor == 1 && gstate_c.Supports(GPU_SUPPORTS_UNPACK_SUBIMAGE) && w != bufw) { - glPixelStorei(GL_UNPACK_ROW_LENGTH, bufw); - useUnpack = true; + if (replaced.Valid()) { + PROFILE_THIS_SCOPE("replacetex"); + + tmpTexBufRearrange.resize(w * h); + int bpp = replaced.Format(level) == ReplacedTextureFormat::F_8888 ? 4 : 2; + replaced.Load(level, tmpTexBufRearrange.data(), bpp * w); + pixelData = tmpTexBufRearrange.data(); + + dstFmt = ToGLESFormat(replaced.Format(level)); + + texByteAlign = bpp; + useBGRA = false; + } else { + + PROFILE_THIS_SCOPE("decodetex"); + + GEPaletteFormat clutformat = gstate.getClutPaletteFormat(); + int bufw; + void *finalBuf = DecodeTextureLevel(GETextureFormat(entry.format), clutformat, level, texByteAlign, dstFmt, scaleFactor, &bufw); + if (finalBuf == NULL) { + return; + } + + // Can restore these and remove the fixup at the end of DecodeTextureLevel on desktop GL and GLES 3. + if (scaleFactor == 1 && gstate_c.Supports(GPU_SUPPORTS_UNPACK_SUBIMAGE) && w != bufw) { + glPixelStorei(GL_UNPACK_ROW_LENGTH, bufw); + useUnpack = true; + } + + useBGRA = UseBGRA8888() && dstFmt == GL_UNSIGNED_BYTE; + + pixelData = (u32 *)finalBuf; + if (scaleFactor > 1) + scaler.Scale(pixelData, dstFmt, w, h, scaleFactor); + + if ((entry.status & TexCacheEntry::STATUS_CHANGE_FREQUENT) == 0) { + TexCacheEntry::Status alphaStatus = CheckAlpha(pixelData, dstFmt, useUnpack ? bufw : w, w, h); + entry.SetAlphaStatus(alphaStatus, level); + } else { + entry.SetAlphaStatus(TexCacheEntry::STATUS_ALPHA_UNKNOWN); + } + + if (replacer.Enabled()) { + int bpp = dstFmt == GL_UNSIGNED_BYTE ? 4 : 2; + // TODO: BGRA. + replacer.NotifyTextureDecoded(entry.fullhash, pixelData, w * bpp, w, h, FromGLESFormat(dstFmt)); + } } glPixelStorei(GL_UNPACK_ALIGNMENT, texByteAlign); - useBGRA = UseBGRA8888() && dstFmt == GL_UNSIGNED_BYTE; - - pixelData = (u32 *)finalBuf; - if (scaleFactor > 1) - scaler.Scale(pixelData, dstFmt, w, h, scaleFactor); - - if ((entry.status & TexCacheEntry::STATUS_CHANGE_FREQUENT) == 0) { - TexCacheEntry::Status alphaStatus = CheckAlpha(pixelData, dstFmt, useUnpack ? bufw : w, w, h); - entry.SetAlphaStatus(alphaStatus, level); - } else { - entry.SetAlphaStatus(TexCacheEntry::STATUS_ALPHA_UNKNOWN); - } - } - GLuint components = dstFmt == GL_UNSIGNED_SHORT_5_6_5 ? GL_RGB : GL_RGBA; GLuint components2 = components; diff --git a/GPU/GLES/TextureCache.h b/GPU/GLES/TextureCache.h index 946ce05394..10cddc6dcb 100644 --- a/GPU/GLES/TextureCache.h +++ b/GPU/GLES/TextureCache.h @@ -95,7 +95,7 @@ private: void DeleteTexture(TexCache::iterator it); void *ReadIndexedTex(int level, const u8 *texptr, int bytesPerIndex, GLuint dstFmt, int bufw); void UpdateSamplingParams(TexCacheEntry &entry, bool force); - void LoadTextureLevel(TexCacheEntry &entry, int level, bool replaceImages, int scaleFactor, GLenum dstFmt); + void LoadTextureLevel(TexCacheEntry &entry, ReplacedTexture &replaced, int level, bool replaceImages, int scaleFactor, GLenum dstFmt); GLenum GetDestFormat(GETextureFormat format, GEPaletteFormat clutFormat) const; void *DecodeTextureLevel(GETextureFormat format, GEPaletteFormat clutformat, int level, u32 &texByteAlign, GLenum dstFmt, int scaleFactor, int *bufw = 0); TexCacheEntry::Status CheckAlpha(const u32 *pixelData, GLenum dstFmt, int stride, int w, int h); diff --git a/GPU/GPU.vcxproj b/GPU/GPU.vcxproj index 5b49067da8..1c6733df42 100644 --- a/GPU/GPU.vcxproj +++ b/GPU/GPU.vcxproj @@ -199,6 +199,7 @@ true + @@ -275,6 +276,7 @@ true + diff --git a/GPU/GPU.vcxproj.filters b/GPU/GPU.vcxproj.filters index 9818d21e92..2ebdc39247 100644 --- a/GPU/GPU.vcxproj.filters +++ b/GPU/GPU.vcxproj.filters @@ -240,6 +240,9 @@ Vulkan + + Common + @@ -461,5 +464,8 @@ Vulkan + + Common + diff --git a/GPU/Vulkan/TextureCacheVulkan.cpp b/GPU/Vulkan/TextureCacheVulkan.cpp index 74c42fec1d..82d9a93f1f 100644 --- a/GPU/Vulkan/TextureCacheVulkan.cpp +++ b/GPU/Vulkan/TextureCacheVulkan.cpp @@ -591,7 +591,11 @@ static inline u32 MiniHash(const u32 *ptr) { return ptr[0]; } -static inline u32 QuickTexHash(u32 addr, int bufw, int w, int h, GETextureFormat format, TextureCacheVulkan::TexCacheEntry *entry) { +static inline u32 QuickTexHash(TextureReplacer &replacer, u32 addr, int bufw, int w, int h, GETextureFormat format, TextureCacheVulkan::TexCacheEntry *entry) { + if (replacer.Enabled()) { + return replacer.ComputeHash(addr, bufw, w, h, format, entry->maxSeenV); + } + if (h == 512 && entry->maxSeenV < 512 && entry->maxSeenV != 0) { h = (int)entry->maxSeenV; } @@ -874,6 +878,34 @@ bool TextureCacheVulkan::SetOffsetTexture(u32 offset) { return false; } +ReplacedTextureFormat FromVulkanFormat(VkFormat fmt) { + switch (fmt) { + case VULKAN_565_FORMAT: + return ReplacedTextureFormat::F_5650; + case VULKAN_1555_FORMAT: + return ReplacedTextureFormat::F_5551; + case VULKAN_4444_FORMAT: + return ReplacedTextureFormat::F_4444; + case VULKAN_8888_FORMAT: + default: + return ReplacedTextureFormat::F_8888; + } +} + +VkFormat ToVulkanFormat(ReplacedTextureFormat fmt) { + switch (fmt) { + case ReplacedTextureFormat::F_5650: + return VULKAN_565_FORMAT; + case ReplacedTextureFormat::F_5551: + return VULKAN_1555_FORMAT; + case ReplacedTextureFormat::F_4444: + return VULKAN_4444_FORMAT; + case ReplacedTextureFormat::F_8888: + default: + return VULKAN_8888_FORMAT; + } +} + void TextureCacheVulkan::SetTexture(VulkanPushBuffer *uploadBuffer) { #ifdef DEBUG_TEXTURES if (SetDebugTexture()) { @@ -993,13 +1025,13 @@ void TextureCacheVulkan::SetTexture(VulkanPushBuffer *uploadBuffer) { bool hashFail = false; if (texhash != entry->hash) { - fullhash = QuickTexHash(texaddr, bufw, w, h, format, entry); + fullhash = QuickTexHash(replacer, texaddr, bufw, w, h, format, entry); hashFail = true; rehash = false; } if (rehash && entry->GetHashStatus() != TexCacheEntry::STATUS_RELIABLE) { - fullhash = QuickTexHash(texaddr, bufw, w, h, format, entry); + fullhash = QuickTexHash(replacer, texaddr, bufw, w, h, format, entry); if (fullhash != entry->fullhash) { hashFail = true; } else { @@ -1142,7 +1174,7 @@ void TextureCacheVulkan::SetTexture(VulkanPushBuffer *uploadBuffer) { // to avoid excessive clearing caused by cache invalidations. entry->sizeInRAM = (textureBitsPerPixel[format] * bufw * h / 2) / 8; - entry->fullhash = fullhash == 0 ? QuickTexHash(texaddr, bufw, w, h, format, entry) : fullhash; + entry->fullhash = fullhash == 0 ? QuickTexHash(replacer, texaddr, bufw, w, h, format, entry) : fullhash; entry->cluthash = cluthash; entry->status &= ~TexCacheEntry::STATUS_ALPHA_MASK; @@ -1205,6 +1237,15 @@ void TextureCacheVulkan::SetTexture(VulkanPushBuffer *uploadBuffer) { scaleFactor = scaleFactor > 4 ? 4 : (scaleFactor > 2 ? 2 : 1); } + ReplacedTexture replaced = replacer.FindReplacement(entry->fullhash); + if (replaced.GetSize(0, w, h)) { + // We're replacing, so we won't scale. + scaleFactor = 1; + if (g_Config.bMipMap) { + maxLevel = replaced.MaxLevel(); + } + } + // Don't scale the PPGe texture. if (entry->addr > 0x05000000 && entry->addr < 0x08800000) scaleFactor = 1; @@ -1230,6 +1271,9 @@ void TextureCacheVulkan::SetTexture(VulkanPushBuffer *uploadBuffer) { } VkFormat actualFmt = scaleFactor > 1 ? VULKAN_8888_FORMAT : dstFmt; + if (replaced.Valid()) { + actualFmt = ToVulkanFormat(replaced.Format(0)); + } if (!entry->vkTex) { entry->vkTex = new CachedTextureVulkan(); entry->vkTex->texture_ = new VulkanTexture(vulkan_, allocator_); @@ -1290,15 +1334,29 @@ void TextureCacheVulkan::SetTexture(VulkanPushBuffer *uploadBuffer) { for (int i = 0; i <= maxLevel; i++) { int mipWidth = gstate.getTextureWidth(i) * scaleFactor; int mipHeight = gstate.getTextureHeight(i) * scaleFactor; + if (replaced.Valid()) { + replaced.GetSize(i, mipWidth, mipHeight); + } int bpp = actualFmt == VULKAN_8888_FORMAT ? 4 : 2; int stride = (mipWidth * bpp + 15) & ~15; int size = stride * mipHeight; uint32_t bufferOffset; VkBuffer texBuf; void *data = uploadBuffer->Push(size, &bufferOffset, &texBuf); - LoadTextureLevel(*entry, (uint8_t *)data, stride, i, scaleFactor, dstFmt); + if (replaced.Valid()) { + replaced.Load(i, data, stride); + } else { + LoadTextureLevel(*entry, (uint8_t *)data, stride, i, scaleFactor, dstFmt); + if (replacer.Enabled()) { + replacer.NotifyTextureDecoded(entry->fullhash, data, stride, mipWidth, mipHeight, FromVulkanFormat(actualFmt)); + } + } entry->vkTex->texture_->UploadMip(i, mipWidth, mipHeight, texBuf, bufferOffset, stride / bpp); } + + if (replaced.Valid()) { + entry->SetAlphaStatus(TexCacheEntry::Status(replaced.AlphaStatus())); + } } entry->vkTex->texture_->EndCreate(); @@ -1581,6 +1639,7 @@ void TextureCacheVulkan::LoadTextureLevel(TexCacheEntry &entry, uint8_t *writePt } if ((entry.status & TexCacheEntry::STATUS_CHANGE_FREQUENT) == 0) { + // TODO: bufw may be wrong. TexCacheEntry::Status alphaStatus = CheckAlpha(pixelData, dstFmt, bufw, w, h); entry.SetAlphaStatus(alphaStatus, level); } else { diff --git a/Qt/GPU.pro b/Qt/GPU.pro index e993b64b5a..ba4f607bf3 100644 --- a/Qt/GPU.pro +++ b/Qt/GPU.pro @@ -54,6 +54,7 @@ SOURCES += $$P/GPU/GeDisasm.cpp \ # GPU $$P/GPU/Common/TextureScalerCommon.cpp \ $$P/GPU/Common/VertexDecoderCommon.cpp \ $$P/GPU/Common/TextureCacheCommon.cpp \ + $$P/GPU/Common/TextureReplacer.cpp \ $$P/GPU/Common/TransformCommon.cpp \ $$P/GPU/Common/SoftwareTransformCommon.cpp \ $$P/GPU/Common/PostShader.cpp \ diff --git a/android/jni/Android.mk b/android/jni/Android.mk index 6eb9e787c8..8f00efe94c 100644 --- a/android/jni/Android.mk +++ b/android/jni/Android.mk @@ -207,6 +207,7 @@ EXEC_AND_LIB_FILES := \ $(SRC)/GPU/Common/SoftwareTransformCommon.cpp.arm \ $(SRC)/GPU/Common/VertexDecoderCommon.cpp.arm \ $(SRC)/GPU/Common/TextureCacheCommon.cpp.arm \ + $(SRC)/GPU/Common/TextureReplacer.cpp \ $(SRC)/GPU/Common/TextureScalerCommon.cpp.arm \ $(SRC)/GPU/Common/SplineCommon.cpp.arm \ $(SRC)/GPU/Common/DrawEngineCommon.cpp.arm \