From bd17351ef9c6716cc5951fa92a76c9580fb426f4 Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Tue, 22 Mar 2016 19:00:57 +0100 Subject: [PATCH] Texcache: Minor refactor/cleanup --- GPU/Common/TextureCacheCommon.cpp | 16 +++++------- GPU/Common/TextureCacheCommon.h | 2 +- GPU/Directx9/TextureCacheDX9.cpp | 14 +++++----- GPU/GLES/TextureCache.cpp | 14 +++++----- GPU/Vulkan/TextureCacheVulkan.cpp | 43 +++++++++---------------------- GPU/Vulkan/TextureCacheVulkan.h | 2 +- 6 files changed, 37 insertions(+), 54 deletions(-) diff --git a/GPU/Common/TextureCacheCommon.cpp b/GPU/Common/TextureCacheCommon.cpp index 5ab4079e1a..24a6e7df8c 100644 --- a/GPU/Common/TextureCacheCommon.cpp +++ b/GPU/Common/TextureCacheCommon.cpp @@ -307,7 +307,7 @@ void TextureCacheCommon::LoadClut(u32 clutAddr, u32 loadBytes) { clutMaxBytes_ = std::max(clutMaxBytes_, loadBytes); } -void *TextureCacheCommon::UnswizzleFromMem(const u8 *texptr, u32 bufw, u32 height, u32 bytesPerPixel) { +void TextureCacheCommon::UnswizzleFromMem(u32 *dest, const u8 *texptr, u32 bufw, u32 height, u32 bytesPerPixel) { const u32 rowWidth = (bytesPerPixel > 0) ? (bufw * bytesPerPixel) : (bufw / 2); const u32 pitch = rowWidth / 4; const int bxc = rowWidth / 16; @@ -317,15 +317,14 @@ void *TextureCacheCommon::UnswizzleFromMem(const u8 *texptr, u32 bufw, u32 heigh u32 ydest = 0; if (rowWidth >= 16) { - u32 *ydestp = tmpTexBuf32.data(); // The most common one, so it gets an optimized implementation. - DoUnswizzleTex16(texptr, ydestp, bxc, byc, pitch, rowWidth); + DoUnswizzleTex16(texptr, dest, bxc, byc, pitch, rowWidth); } else if (rowWidth == 8) { const u32 *src = (const u32 *) texptr; for (int by = 0; by < byc; by++) { for (int n = 0; n < 8; n++, ydest += 2) { - tmpTexBuf32[ydest + 0] = *src++; - tmpTexBuf32[ydest + 1] = *src++; + dest[ydest + 0] = *src++; + dest[ydest + 1] = *src++; src += 2; // skip two u32 } } @@ -333,7 +332,7 @@ void *TextureCacheCommon::UnswizzleFromMem(const u8 *texptr, u32 bufw, u32 heigh const u32 *src = (const u32 *) texptr; for (int by = 0; by < byc; by++) { for (int n = 0; n < 8; n++, ydest++) { - tmpTexBuf32[ydest] = *src++; + dest[ydest] = *src++; src += 3; } } @@ -343,7 +342,7 @@ void *TextureCacheCommon::UnswizzleFromMem(const u8 *texptr, u32 bufw, u32 heigh for (int n = 0; n < 4; n++, ydest++) { u16 n1 = src[0]; u16 n2 = src[8]; - tmpTexBuf32[ydest] = (u32)n1 | ((u32)n2 << 16); + dest[ydest] = (u32)n1 | ((u32)n2 << 16); src += 16; } } @@ -355,12 +354,11 @@ void *TextureCacheCommon::UnswizzleFromMem(const u8 *texptr, u32 bufw, u32 heigh u8 n2 = src[16]; u8 n3 = src[32]; u8 n4 = src[48]; - tmpTexBuf32[ydest] = (u32)n1 | ((u32)n2 << 8) | ((u32)n3 << 16) | ((u32)n4 << 24); + dest[ydest] = (u32)n1 | ((u32)n2 << 8) | ((u32)n3 << 16) | ((u32)n4 << 24); src += 64; } } } - return tmpTexBuf32.data(); } void *TextureCacheCommon::RearrangeBuf(void *inBuf, u32 inRowBytes, u32 outRowBytes, int h, bool allowInPlace) { diff --git a/GPU/Common/TextureCacheCommon.h b/GPU/Common/TextureCacheCommon.h index 9b24211d9e..d1e067a950 100644 --- a/GPU/Common/TextureCacheCommon.h +++ b/GPU/Common/TextureCacheCommon.h @@ -140,7 +140,7 @@ protected: // Can't be unordered_map, we use lower_bound ... although for some reason that compiles on MSVC. typedef std::map TexCache; - void *UnswizzleFromMem(const u8 *texptr, u32 bufw, u32 height, u32 bytesPerPixel); + void UnswizzleFromMem(u32 *dest, const u8 *texptr, u32 bufw, u32 height, u32 bytesPerPixel); void *RearrangeBuf(void *inBuf, u32 inRowBytes, u32 outRowBytes, int h, bool allowInPlace = true); void GetSamplingParams(int &minFilt, int &magFilt, bool &sClamp, bool &tClamp, float &lodBias, u8 maxLevel); diff --git a/GPU/Directx9/TextureCacheDX9.cpp b/GPU/Directx9/TextureCacheDX9.cpp index e772734e6d..d2e3f0d97b 100644 --- a/GPU/Directx9/TextureCacheDX9.cpp +++ b/GPU/Directx9/TextureCacheDX9.cpp @@ -457,7 +457,7 @@ void *TextureCacheDX9::ReadIndexedTex(int level, const u8 *texptr, int bytesPerI } } else { tmpTexBuf32.resize(std::max(bufw, w) * h); - UnswizzleFromMem(texptr, bufw, h, bytesPerIndex); + UnswizzleFromMem(tmpTexBuf32.data(), texptr, bufw, h, bytesPerIndex); switch (bytesPerIndex) { case 1: DeIndexTexture(tmpTexBuf16.data(), (u8 *) tmpTexBuf32.data(), length, clut); @@ -497,7 +497,7 @@ void *TextureCacheDX9::ReadIndexedTex(int level, const u8 *texptr, int bytesPerI } buf = tmpTexBuf32.data(); } else { - UnswizzleFromMem(texptr, bufw, h, bytesPerIndex); + UnswizzleFromMem(tmpTexBuf32.data(), texptr, bufw, h, bytesPerIndex); // Since we had to unswizzle to tmpTexBuf32, let's output to tmpTexBuf16. tmpTexBuf16.resize(std::max(bufw, w) * h * 2); u32 *dest32 = (u32 *) tmpTexBuf16.data(); @@ -1445,7 +1445,7 @@ void *TextureCacheDX9::DecodeTextureLevel(GETextureFormat format, GEPaletteForma } } else { tmpTexBuf32.resize(std::max(bufw, w) * h); - UnswizzleFromMem(texptr, bufw, h, 0); + UnswizzleFromMem(tmpTexBuf32.data(), texptr, bufw, h, 0); if (clutAlphaLinear_ && mipmapShareClut) { DeIndexTexture4OptimalRev(tmpTexBuf16.data(), (const u8 *)tmpTexBuf32.data(), bufw * h, clutAlphaLinearColor_); } else { @@ -1465,7 +1465,7 @@ void *TextureCacheDX9::DecodeTextureLevel(GETextureFormat format, GEPaletteForma DeIndexTexture4(tmpTexBuf32.data(), texptr, bufw * h, clut); finalBuf = tmpTexBuf32.data(); } else { - UnswizzleFromMem(texptr, bufw, h, 0); + UnswizzleFromMem(tmpTexBuf32.data(), texptr, bufw, h, 0); // Let's reuse tmpTexBuf16, just need double the space. tmpTexBuf16.resize(std::max(bufw, w) * h * 2); DeIndexTexture4((u32 *)tmpTexBuf16.data(), (u8 *)tmpTexBuf32.data(), bufw * h, clut); @@ -1510,7 +1510,8 @@ void *TextureCacheDX9::DecodeTextureLevel(GETextureFormat format, GEPaletteForma } else { tmpTexBuf32.resize(std::max(bufw, w) * h); - finalBuf = UnswizzleFromMem(texptr, bufw, h, 2); + UnswizzleFromMem(tmpTexBuf32.data(), texptr, bufw, h, 2); + finalBuf = tmpTexBuf32.data(); } break; @@ -1529,7 +1530,8 @@ void *TextureCacheDX9::DecodeTextureLevel(GETextureFormat format, GEPaletteForma } } else { tmpTexBuf32.resize(std::max(bufw, w) * h); - finalBuf = UnswizzleFromMem(texptr, bufw, h, 4); + UnswizzleFromMem(tmpTexBuf32.data(), texptr, bufw, h, 4); + finalBuf = tmpTexBuf32.data(); } break; diff --git a/GPU/GLES/TextureCache.cpp b/GPU/GLES/TextureCache.cpp index 5c096af52d..994959608c 100644 --- a/GPU/GLES/TextureCache.cpp +++ b/GPU/GLES/TextureCache.cpp @@ -457,7 +457,7 @@ void *TextureCache::ReadIndexedTex(int level, const u8 *texptr, int bytesPerInde } } else { tmpTexBuf32.resize(std::max(bufw, w) * h); - UnswizzleFromMem(texptr, bufw, h, bytesPerIndex); + UnswizzleFromMem(tmpTexBuf32.data(), texptr, bufw, h, bytesPerIndex); switch (bytesPerIndex) { case 1: DeIndexTexture(tmpTexBuf16.data(), (u8 *) tmpTexBuf32.data(), length, clut); @@ -497,7 +497,7 @@ void *TextureCache::ReadIndexedTex(int level, const u8 *texptr, int bytesPerInde } buf = tmpTexBuf32.data(); } else { - UnswizzleFromMem(texptr, bufw, h, bytesPerIndex); + UnswizzleFromMem(tmpTexBuf32.data(), texptr, bufw, h, bytesPerIndex); // Since we had to unswizzle to tmpTexBuf32, let's output to tmpTexBuf16. tmpTexBuf16.resize(std::max(bufw, w) * h * 2); u32 *dest32 = (u32 *) tmpTexBuf16.data(); @@ -1581,7 +1581,7 @@ void *TextureCache::DecodeTextureLevel(GETextureFormat format, GEPaletteFormat c } } else { tmpTexBuf32.resize(std::max(bufw, w) * h); - UnswizzleFromMem(texptr, bufw, h, 0); + UnswizzleFromMem(tmpTexBuf32.data(), texptr, bufw, h, 0); if (clutAlphaLinear_ && mipmapShareClut) { DeIndexTexture4Optimal(tmpTexBuf16.data(), (const u8 *)tmpTexBuf32.data(), bufw * h, clutAlphaLinearColor_); } else { @@ -1601,7 +1601,7 @@ void *TextureCache::DecodeTextureLevel(GETextureFormat format, GEPaletteFormat c DeIndexTexture4(tmpTexBuf32.data(), texptr, bufw * h, clut); finalBuf = tmpTexBuf32.data(); } else { - UnswizzleFromMem(texptr, bufw, h, 0); + UnswizzleFromMem(tmpTexBuf32.data(), texptr, bufw, h, 0); // Let's reuse tmpTexBuf16, just need double the space. tmpTexBuf16.resize(std::max(bufw, w) * h * 2); DeIndexTexture4((u32 *)tmpTexBuf16.data(), (u8 *)tmpTexBuf32.data(), bufw * h, clut); @@ -1645,7 +1645,8 @@ void *TextureCache::DecodeTextureLevel(GETextureFormat format, GEPaletteFormat c ConvertColors(finalBuf, texptr, dstFmt, bufw * h); } else { tmpTexBuf32.resize(std::max(bufw, w) * h); - finalBuf = UnswizzleFromMem(texptr, bufw, h, 2); + UnswizzleFromMem(tmpTexBuf32.data(), texptr, bufw, h, 2); + finalBuf = tmpTexBuf32.data(); ConvertColors(finalBuf, finalBuf, dstFmt, bufw * h); } break; @@ -1669,7 +1670,8 @@ void *TextureCache::DecodeTextureLevel(GETextureFormat format, GEPaletteFormat c } } else { tmpTexBuf32.resize(std::max(bufw, w) * h); - finalBuf = UnswizzleFromMem(texptr, bufw, h, 4); + UnswizzleFromMem(tmpTexBuf32.data(), texptr, bufw, h, 4); + finalBuf = tmpTexBuf32.data(); ConvertColors(finalBuf, finalBuf, dstFmt, bufw * h); } break; diff --git a/GPU/Vulkan/TextureCacheVulkan.cpp b/GPU/Vulkan/TextureCacheVulkan.cpp index 436f1c5165..c21b0a7c68 100644 --- a/GPU/Vulkan/TextureCacheVulkan.cpp +++ b/GPU/Vulkan/TextureCacheVulkan.cpp @@ -491,7 +491,6 @@ void *TextureCacheVulkan::ReadIndexedTex(int level, const u8 *texptr, int bytesP case GE_CMODE_16BIT_ABGR4444: { tmpTexBuf16.resize(std::max(bufw, w) * h); - tmpTexBufRearrange.resize(std::max(bufw, w) * h); const u16 *clut = GetCurrentClut(); if (!gstate.isTextureSwizzled()) { switch (bytesPerIndex) { @@ -509,7 +508,7 @@ void *TextureCacheVulkan::ReadIndexedTex(int level, const u8 *texptr, int bytesP } } else { tmpTexBuf32.resize(std::max(bufw, w) * h); - UnswizzleFromMem(texptr, bufw, h, bytesPerIndex); + UnswizzleFromMem(tmpTexBuf32.data(), texptr, bufw, h, bytesPerIndex); switch (bytesPerIndex) { case 1: DeIndexTexture(tmpTexBuf16.data(), (u8 *)tmpTexBuf32.data(), length, clut); @@ -531,7 +530,6 @@ void *TextureCacheVulkan::ReadIndexedTex(int level, const u8 *texptr, int bytesP case GE_CMODE_32BIT_ABGR8888: { tmpTexBuf32.resize(std::max(bufw, w) * h); - tmpTexBufRearrange.resize(std::max(bufw, w) * h); const u32 *clut = GetCurrentClut(); if (!gstate.isTextureSwizzled()) { switch (bytesPerIndex) { @@ -549,7 +547,7 @@ void *TextureCacheVulkan::ReadIndexedTex(int level, const u8 *texptr, int bytesP } buf = tmpTexBuf32.data(); } else { - UnswizzleFromMem(texptr, bufw, h, bytesPerIndex); + UnswizzleFromMem(tmpTexBuf32.data(), texptr, bufw, h, bytesPerIndex); // Since we had to unswizzle to tmpTexBuf32, let's output to tmpTexBuf16. tmpTexBuf16.resize(std::max(bufw, w) * h * 2); u32 *dest32 = (u32 *)tmpTexBuf16.data(); @@ -596,8 +594,6 @@ VkFormat getClutDestFormatVulkan(GEPaletteFormat format) { return VK_FORMAT_UNDEFINED; } -static const u8 texByteAlignMap[] = { 2, 2, 2, 4 }; - static const VkFilter MagFiltVK[2] = { VK_FILTER_NEAREST, VK_FILTER_LINEAR @@ -1520,10 +1516,9 @@ VkFormat TextureCacheVulkan::GetDestFormat(GETextureFormat format, GEPaletteForm } } -void *TextureCacheVulkan::DecodeTextureLevel(GETextureFormat format, GEPaletteFormat clutformat, int level, u32 &texByteAlign, VkFormat dstFmt, int scaleFactor, int *bufwout) { +void *TextureCacheVulkan::DecodeTextureLevel(GETextureFormat format, GEPaletteFormat clutformat, uint32_t texaddr, int level, VkFormat dstFmt, int scaleFactor, int bufw) { void *finalBuf = NULL; - u32 texaddr = gstate.getTextureAddress(level); bool swizzled = gstate.isTextureSwizzled(); if ((texaddr & 0x00600000) != 0 && Memory::IsVRAMAddress(texaddr)) { // This means it's in a mirror, possibly a swizzled mirror. Let's report. @@ -1535,9 +1530,6 @@ void *TextureCacheVulkan::DecodeTextureLevel(GETextureFormat format, GEPaletteFo // Note that (texaddr & 0x00600000) == 0x00600000 is very likely to be depth texturing. } - int bufw = GetTextureBufw(level, texaddr, format); - if (bufwout) - *bufwout = bufw; int w = gstate.getTextureWidth(level); int h = gstate.getTextureHeight(level); const u8 *texptr = Memory::GetPointer(texaddr); @@ -1554,9 +1546,7 @@ void *TextureCacheVulkan::DecodeTextureLevel(GETextureFormat format, GEPaletteFo case GE_CMODE_16BIT_ABGR4444: { tmpTexBuf16.resize(std::max(bufw, w) * h); - tmpTexBufRearrange.resize(std::max(bufw, w) * h); const u16 *clut = GetCurrentClut() + clutSharingOffset; - texByteAlign = 2; if (!swizzled) { if (clutAlphaLinear_ && mipmapShareClut) { DeIndexTexture4Optimal(tmpTexBuf16.data(), texptr, bufw * h, clutAlphaLinearColor_); @@ -1565,7 +1555,7 @@ void *TextureCacheVulkan::DecodeTextureLevel(GETextureFormat format, GEPaletteFo } } else { tmpTexBuf32.resize(std::max(bufw, w) * h); - UnswizzleFromMem(texptr, bufw, h, 0); + UnswizzleFromMem(tmpTexBuf32.data(), texptr, bufw, h, 0); if (clutAlphaLinear_ && mipmapShareClut) { DeIndexTexture4Optimal(tmpTexBuf16.data(), (const u8 *)tmpTexBuf32.data(), bufw * h, clutAlphaLinearColor_); } else { @@ -1579,13 +1569,12 @@ void *TextureCacheVulkan::DecodeTextureLevel(GETextureFormat format, GEPaletteFo case GE_CMODE_32BIT_ABGR8888: { tmpTexBuf32.resize(std::max(bufw, w) * h); - tmpTexBufRearrange.resize(std::max(bufw, w) * h); const u32 *clut = GetCurrentClut() + clutSharingOffset; if (!swizzled) { DeIndexTexture4(tmpTexBuf32.data(), texptr, bufw * h, clut); finalBuf = tmpTexBuf32.data(); } else { - UnswizzleFromMem(texptr, bufw, h, 0); + UnswizzleFromMem(tmpTexBuf32.data(), texptr, bufw, h, 0); // Let's reuse tmpTexBuf16, just need double the space. tmpTexBuf16.resize(std::max(bufw, w) * h * 2); DeIndexTexture4((u32 *)tmpTexBuf16.data(), (u8 *)tmpTexBuf32.data(), bufw * h, clut); @@ -1602,34 +1591,29 @@ void *TextureCacheVulkan::DecodeTextureLevel(GETextureFormat format, GEPaletteFo break; case GE_TFMT_CLUT8: - texByteAlign = texByteAlignMap[gstate.getClutPaletteFormat()]; finalBuf = ReadIndexedTex(level, texptr, 1, dstFmt, bufw); break; case GE_TFMT_CLUT16: - texByteAlign = texByteAlignMap[gstate.getClutPaletteFormat()]; finalBuf = ReadIndexedTex(level, texptr, 2, dstFmt, bufw); break; case GE_TFMT_CLUT32: - texByteAlign = texByteAlignMap[gstate.getClutPaletteFormat()]; finalBuf = ReadIndexedTex(level, texptr, 4, dstFmt, bufw); break; case GE_TFMT_4444: case GE_TFMT_5551: case GE_TFMT_5650: - texByteAlign = 2; - if (!swizzled) { int len = std::max(bufw, w) * h; tmpTexBuf16.resize(len); - tmpTexBufRearrange.resize(len); finalBuf = tmpTexBuf16.data(); ConvertColors(finalBuf, texptr, dstFmt, bufw * h); } else { tmpTexBuf32.resize(std::max(bufw, w) * h); - finalBuf = UnswizzleFromMem(texptr, bufw, h, 2); + UnswizzleFromMem(tmpTexBuf32.data(), texptr, bufw, h, 2); + finalBuf = tmpTexBuf32.data(); ConvertColors(finalBuf, finalBuf, dstFmt, bufw * h); } break; @@ -1641,13 +1625,13 @@ void *TextureCacheVulkan::DecodeTextureLevel(GETextureFormat format, GEPaletteFo finalBuf = (void *)texptr; } else { tmpTexBuf32.resize(std::max(bufw, w) * h); - tmpTexBufRearrange.resize(std::max(bufw, w) * h); finalBuf = tmpTexBuf32.data(); ConvertColors(finalBuf, texptr, dstFmt, bufw * h); } } else { tmpTexBuf32.resize(std::max(bufw, w) * h); - finalBuf = UnswizzleFromMem(texptr, bufw, h, 4); + UnswizzleFromMem(tmpTexBuf32.data(), texptr, bufw, h, 4); + finalBuf = tmpTexBuf32.data(); ConvertColors(finalBuf, finalBuf, dstFmt, bufw * h); } break; @@ -1656,7 +1640,6 @@ void *TextureCacheVulkan::DecodeTextureLevel(GETextureFormat format, GEPaletteFo { int minw = std::min(bufw, w); tmpTexBuf32.resize(std::max(bufw, w) * h); - tmpTexBufRearrange.resize(std::max(bufw, w) * h); u32 *dst = tmpTexBuf32.data(); DXT1Block *src = (DXT1Block*)texptr; @@ -1677,7 +1660,6 @@ void *TextureCacheVulkan::DecodeTextureLevel(GETextureFormat format, GEPaletteFo { int minw = std::min(bufw, w); tmpTexBuf32.resize(std::max(bufw, w) * h); - tmpTexBufRearrange.resize(std::max(bufw, w) * h); u32 *dst = tmpTexBuf32.data(); DXT3Block *src = (DXT3Block*)texptr; @@ -1698,7 +1680,6 @@ void *TextureCacheVulkan::DecodeTextureLevel(GETextureFormat format, GEPaletteFo { int minw = std::min(bufw, w); tmpTexBuf32.resize(std::max(bufw, w) * h); - tmpTexBufRearrange.resize(std::max(bufw, w) * h); u32 *dst = tmpTexBuf32.data(); DXT5Block *src = (DXT5Block*)texptr; @@ -1758,11 +1739,11 @@ void TextureCacheVulkan::LoadTextureLevel(TexCacheEntry &entry, uint8_t *writePt { PROFILE_THIS_SCOPE("decodetex"); - u32 texByteAlign = 1; GETextureFormat tfmt = (GETextureFormat)entry.format; GEPaletteFormat clutformat = gstate.getClutPaletteFormat(); - int bufw; - void *finalBuf = DecodeTextureLevel(tfmt, clutformat, level, texByteAlign, dstFmt, scaleFactor, &bufw); + u32 texaddr = gstate.getTextureAddress(level); + int bufw = GetTextureBufw(level, texaddr, tfmt); + void *finalBuf = DecodeTextureLevel(tfmt, clutformat, texaddr, level, dstFmt, scaleFactor, bufw); if (finalBuf == NULL) { return; } diff --git a/GPU/Vulkan/TextureCacheVulkan.h b/GPU/Vulkan/TextureCacheVulkan.h index 8a4998aa32..16fcd74236 100644 --- a/GPU/Vulkan/TextureCacheVulkan.h +++ b/GPU/Vulkan/TextureCacheVulkan.h @@ -128,7 +128,7 @@ private: void UpdateSamplingParams(TexCacheEntry &entry, SamplerCacheKey &key); void LoadTextureLevel(TexCacheEntry &entry, uint8_t *writePtr, int rowPitch, int level, bool replaceImages, int scaleFactor, VkFormat dstFmt); VkFormat GetDestFormat(GETextureFormat format, GEPaletteFormat clutFormat) const; - void *DecodeTextureLevel(GETextureFormat format, GEPaletteFormat clutformat, int level, u32 &texByteAlign, VkFormat dstFmt, int scaleFactor, int *bufw = 0); + void *DecodeTextureLevel(GETextureFormat format, GEPaletteFormat clutformat, uint32_t texaddr, int level, VkFormat dstFmt, int scaleFactor, int bufw); TexCacheEntry::Status CheckAlpha(const u32 *pixelData, VkFormat dstFmt, int stride, int w, int h); template const T *GetCurrentClut();