summaryrefslogtreecommitdiff
path: root/Source/Core/VideoCommon
diff options
context:
space:
mode:
authorMartino Fontana <tinozzo123@gmail.com>2026-09-15 18:03:33 +0200
committerMartino Fontana <tinozzo123@gmail.com>2026-10-31 22:44:13 +0100
commitd74e757bad78b00224720f30437204e0257476bb (patch)
treee0789ad3ead77a35316041922b2ac4dea9969cf4 /Source/Core/VideoCommon
parent2c6a598fae31be8c123181e74e3f2f08d601e0d3 (diff)
TextureCacheBase: Optimize EFB and XFB uninitialization
For EFB, optimize the case where bytes_per_row == stride. For XFB, do that, and make sure that autovectorization is used without intrinsics. They were only used on x86, so that should improve performance on ARM. (MSVC ARM specifically will load to register on every iteration, and I give up on this stupid compiler, but it should still be better than before.) For reference, on x86 with SIMD, on a scene that runs at 3000 FPS, this function takes 1.5% of time according to the profiler. I spent way too much time making all the compilers cooperate, and I hate it.
Diffstat (limited to 'Source/Core/VideoCommon')
-rw-r--r--Source/Core/VideoCommon/TextureCacheBase.cpp73
1 files changed, 45 insertions, 28 deletions
diff --git a/Source/Core/VideoCommon/TextureCacheBase.cpp b/Source/Core/VideoCommon/TextureCacheBase.cpp
index 44020bbc90..b84fceb737 100644
--- a/Source/Core/VideoCommon/TextureCacheBase.cpp
+++ b/Source/Core/VideoCommon/TextureCacheBase.cpp
@@ -11,9 +11,6 @@
#include <string>
#include <utility>
#include <vector>
-#if defined(_M_X86_64)
-#include <pmmintrin.h>
-#endif
#include <fmt/format.h>
@@ -2357,7 +2354,7 @@ void TextureCacheBase::CopyRenderTargetToTexture(FramebufferCopyResolvedData& re
{
action->BeforeEFB(&efb);
}
- if (skip == true)
+ if (skip)
{
if (resolved_data.copy_to_ram)
{
@@ -2502,11 +2499,18 @@ void TextureCacheBase::UninitializeEFBMemory(u8* dst, u32 stride, u32 bytes_per_
// Setting a clear byte different from 0 is also useful when the
// game does use the texture from RAM, if we can more closely
// resemble an actual EFB copy (e.g. Excite Truck).
- u8* ptr = dst;
- for (u32 i = 0; i < num_blocks_y; i++)
+ if (bytes_per_row == stride)
{
- std::memset(ptr, g_ActiveConfig.uSkipEFBCopyToRamClearByte, bytes_per_row);
- ptr += stride;
+ std::memset(dst, g_ActiveConfig.uSkipEFBCopyToRamClearByte, bytes_per_row * num_blocks_y);
+ }
+ else
+ {
+ u8* ptr = dst;
+ for (u32 i = 0; i < num_blocks_y; i++)
+ {
+ std::memset(ptr, g_ActiveConfig.uSkipEFBCopyToRamClearByte, bytes_per_row);
+ ptr += stride;
+ }
}
}
@@ -2520,35 +2524,48 @@ void TextureCacheBase::UninitializeXFBMemory(u8* dst, u32 stride, u32 bytes_per_
// which is why we still set uninitialized xfb memory to fuchsia
// (Y=1,U=254,V=254) instead of dark green (Y=0,U=0,V=0) in YUV
// like is done in the EFB path.
+ constexpr std::array<u8, 2> BYTES = {0x01, 0xFE};
+
+ // MSVC is a terrible compiler, and it absolutely refuses to use SIMD unless you do specifically a
+ // memcpy with a constant size and a source that can only be in memory (which is why this is also
+ // static). Otherwise, it insists in doing many simple stores with immediates. On ARM the current
+ // solution still makes it load on every iteration, but it's the best I could do on this stupid
+ // compiler.
+ // If you want to look for prettier ways to make compilers optimize this function well without
+ // intrinsics, have fun: https://godbolt.org/z/GraGzP6eY
+ // Clang not included because it's good (if not too aggressive) at autovectorizing.
+ static constexpr std::array<decltype(BYTES), 16> pattern1 = {
+ BYTES, BYTES, BYTES, BYTES, BYTES, BYTES, BYTES, BYTES,
+ BYTES, BYTES, BYTES, BYTES, BYTES, BYTES, BYTES, BYTES,
+ };
+ // This is needed because GCC otherwise will think that the two memcpy below may alias (and won't
+ // autovectorize as a result). Sigh...
+ static constexpr auto pattern2 = pattern1;
-#if defined(_M_X86_64)
- __m128i sixteenBytes = _mm_set1_epi16((s16)(u16)0xFE01);
-#endif
+ // Note that `bytes_per_block = format == TextureFormat::RGBA8 ? 64 : 32;`
+ static_assert(32 % sizeof(pattern1) == 0);
+ if (bytes_per_row % 32 != 0)
+ {
+ std::unreachable();
+ }
- for (u32 i = 0; i < num_blocks_y; i++)
+ if (bytes_per_row == stride)
{
- u32 size = bytes_per_row;
- u8* rowdst = dst;
-#if defined(_M_X86_64)
- while (size >= 16)
+ for (u32 i = 0; i < bytes_per_row * num_blocks_y; i += sizeof(pattern1))
{
- _mm_storeu_si128((__m128i*)rowdst, sixteenBytes);
- size -= 16;
- rowdst += 16;
+ std::memcpy(dst + i, pattern1.data(), sizeof(pattern1));
}
-#endif
- for (u32 offset = 0; offset < size; offset++)
+ }
+ else
+ {
+ for (u32 i = 0; i < num_blocks_y; i++)
{
- if (offset & 1)
- {
- rowdst[offset] = 254;
- }
- else
+ for (u32 j = 0; j < bytes_per_row; j += sizeof(pattern2))
{
- rowdst[offset] = 1;
+ std::memcpy(dst + j, pattern2.data(), sizeof(pattern2));
}
+ dst += stride;
}
- dst += stride;
}
}