Merge separate NEON functions into the normal functions.

We no longer support non-NEON ARM.

It's nice also to have the NEON and SSE implementations "close" to each
other, easier to port optimizations back and forth etc.
This commit is contained in:
Henrik Rydgård committed 2022-04-12 23:43:21 +02:00
1 parent 6de574104c
commit a68ddd0a8d
34 files changed
+437 -855

No files matched your search

+281 -55
View File
@@ -16,7 +16,9 @@
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include "ppsspp_config.h"
#include "ext/xxhash.h"
#include "Common/Common.h"
#include "Common/Data/Convert/ColorConv.h"
#include "Common/CPUDetect.h"
@@ -25,12 +27,21 @@
#include "GPU/GPU.h"
#include "GPU/GPUState.h"
#include "GPU/Common/TextureDecoder.h"
// NEON is in a separate file so that it can be compiled with a runtime check.
#include "GPU/Common/TextureDecoderNEON.h"
#ifdef _M_SSE
#include <emmintrin.h>
#include <smmintrin.h>
#endif
#if PPSSPP_ARCH(ARM_NEON)
#if defined(_MSC_VER) && PPSSPP_ARCH(ARM64)
#include <arm64_neon.h>
#else
#include <arm_neon.h>
#endif
#endif
#ifdef _M_SSE
u32 QuickTexHashSSE2(const void *checkp, u32 size) {
u32 check = 0;
@@ -66,6 +77,107 @@ u32 QuickTexHashSSE2(const void *checkp, u32 size) {
}
#endif
#if PPSSPP_ARCH(ARM_NEON)
alignas(16) static const u16 QuickTexHashInitial[8] = { 0xc00bU, 0x9bd9U, 0x4b73U, 0xb651U, 0x4d9bU, 0x4309U, 0x0083U, 0x0001U };
u32 QuickTexHashNEON(const void *checkp, u32 size) {
u32 check = 0;
__builtin_prefetch(checkp, 0, 0);
if (((intptr_t)checkp & 0xf) == 0 && (size & 0x3f) == 0) {
#if PPSSPP_PLATFORM(IOS) || PPSSPP_ARCH(ARM64) || defined(_MSC_VER) || !PPSSPP_ARCH(ARMV7)
uint32x4_t cursor = vdupq_n_u32(0);
uint16x8_t cursor2 = vld1q_u16(QuickTexHashInitial);
uint16x8_t update = vdupq_n_u16(0x2455U);
const u32 *p = (const u32 *)checkp;
const u32 *pend = p + size / 4;
while (p < pend) {
cursor = vreinterpretq_u32_u16(vmlaq_u16(vreinterpretq_u16_u32(cursor), vreinterpretq_u16_u32(vld1q_u32(&p[4 * 0])), cursor2));
cursor = veorq_u32(cursor, vld1q_u32(&p[4 * 1]));
cursor = vaddq_u32(cursor, vld1q_u32(&p[4 * 2]));
cursor = veorq_u32(cursor, vreinterpretq_u32_u16(vmulq_u16(vreinterpretq_u16_u32(vld1q_u32(&p[4 * 3])), cursor2)));
cursor2 = vaddq_u16(cursor2, update);
p += 4 * 4;
}
cursor = vaddq_u32(cursor, vreinterpretq_u32_u16(cursor2));
uint32x2_t mixed = vadd_u32(vget_high_u32(cursor), vget_low_u32(cursor));
check = vget_lane_u32(mixed, 0) + vget_lane_u32(mixed, 1);
#else
// TODO: Why does this crash on iOS, but only certain devices?
// It's faster than the above, but I guess it sucks to be using an iPhone.
// As of 2020 clang, it's still faster by ~1.4%.
// d0/d1 (q0) - cursor
// d2/d3 (q1) - cursor2
// d4/d5 (q2) - update
// d16-d23 (q8-q11) - memory transfer
asm volatile (
// Initialize cursor.
"vmov.i32 q0, #0\n"
// Initialize cursor2.
"movw r0, 0xc00b\n"
"movt r0, 0x9bd9\n"
"movw r1, 0x4b73\n"
"movt r1, 0xb651\n"
"vmov d2, r0, r1\n"
"movw r0, 0x4d9b\n"
"movt r0, 0x4309\n"
"movw r1, 0x0083\n"
"movt r1, 0x0001\n"
"vmov d3, r0, r1\n"
// Initialize update.
"movw r0, 0x2455\n"
"vdup.i16 q2, r0\n"
// This is where we end.
"add r0, %1, %2\n"
// Okay, do the memory hashing.
"QuickTexHashNEON_next:\n"
"pld [%2, #0xc0]\n"
"vldmia %2!, {d16-d23}\n"
"vmla.i16 q0, q1, q8\n"
"vmul.i16 q11, q11, q1\n"
"veor.i32 q0, q0, q9\n"
"cmp %2, r0\n"
"vadd.i32 q0, q0, q10\n"
"vadd.i16 q1, q1, q2\n"
"veor.i32 q0, q0, q11\n"
"blo QuickTexHashNEON_next\n"
// Now let's get the result.
"vadd.i32 q0, q0, q1\n"
"vadd.i32 d0, d0, d1\n"
"vmov r0, r1, d0\n"
"add %0, r0, r1\n"
: "=r"(check)
: "r"(size), "r"(checkp)
: "r0", "r1", "d0", "d1", "d2", "d3", "d4", "d5", "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23", "cc"
);
#endif
} else {
const u32 size_u32 = size / 4;
const u32 *p = (const u32 *)checkp;
for (u32 i = 0; i < size_u32; i += 4) {
check += p[i + 0];
check ^= p[i + 1];
check += p[i + 2];
check ^= p[i + 3];
}
}
return check;
}
#endif // PPSSPP_ARCH(ARM_NEON)
// Masks to downalign bufw to 16 bytes, and wrap at 2048.
static const u32 textureAlignMask16[16] = {
0x7FF & ~(((8 * 16) / 16) - 1), //GE_TFMT_5650,
@@ -99,6 +211,7 @@ u32 GetTextureBufw(int level, u32 texaddr, GETextureFormat format) {
return bufw;
}
// Is this compatible with QuickTexHashNEON/SSE?
u32 QuickTexHashNonSSE(const void *checkp, u32 size) {
u32 check = 0;
@@ -150,39 +263,7 @@ u32 QuickTexHashNonSSE(const void *checkp, u32 size) {
return check;
}
#if !PPSSPP_ARCH(ARM64) && !defined(_M_SSE)
static u32 QuickTexHashBasic(const void *checkp, u32 size) {
#if PPSSPP_ARCH(ARM) && defined(__GNUC__)
__builtin_prefetch(checkp, 0, 0);
u32 check;
asm volatile (
// Let's change size to the end address.
"add %1, %1, %2\n"
"mov r6, #0\n"
".align 2\n"
// If we have zero sized input, we'll return garbage. Oh well, shouldn't happen.
"QuickTexHashBasic_next:\n"
"ldmia %2!, {r2-r5}\n"
"add r6, r6, r2\n"
"eor r6, r6, r3\n"
"cmp %2, %1\n"
"add r6, r6, r4\n"
"eor r6, r6, r5\n"
"blo QuickTexHashBasic_next\n"
".align 2\n"
"QuickTexHashBasic_done:\n"
"mov %0, r6\n"
: "=r"(check)
: "r"(size), "r"(checkp)
: "r2", "r3", "r4", "r5", "r6"
);
#else
u32 QuickTexHashBasic(const void *checkp, u32 size) {
u32 check = 0;
const u32 size_u32 = size / 4;
const u32 *p = (const u32 *)checkp;
@@ -192,11 +273,9 @@ static u32 QuickTexHashBasic(const void *checkp, u32 size) {
check += p[i + 2];
check ^= p[i + 3];
}
#endif
return check;
}
#endif
void DoSwizzleTex16(const u32 *ysrcp, u8 *texptr, int bxc, int byc, u32 pitch) {
// ysrcp is in 32-bits, so this is convenient.
@@ -252,11 +331,12 @@ void DoSwizzleTex16(const u32 *ysrcp, u8 *texptr, int bxc, int byc, u32 pitch) {
}
}
void DoUnswizzleTex16Basic(const u8 *texptr, u32 *ydestp, int bxc, int byc, u32 pitch) {
void DoUnswizzleTex16(const u8 *texptr, u32 *ydestp, int bxc, int byc, u32 pitch) {
// ydestp is in 32-bits, so this is convenient.
const u32 pitchBy32 = pitch >> 2;
#ifdef _M_SSE
// This check is pretty much a given, right?
if (((uintptr_t)ydestp & 0xF) == 0 && (pitch & 0xF) == 0) {
const __m128i *src = (const __m128i *)texptr;
// The pitch parameter is in bytes, so shift down for 128-bit.
@@ -287,6 +367,37 @@ void DoUnswizzleTex16Basic(const u8 *texptr, u32 *ydestp, int bxc, int byc, u32
ydestp += pitchBy32 * 8;
}
} else
#elif PPSSPP_ARCH(ARM_NEON)
if (((uintptr_t)ydestp & 0xF) == 0 && (pitch & 0xF) == 0) {
// TODO: Does this really do anything meaningful?
__builtin_prefetch(texptr, 0, 0);
const u32 *src = (const u32 *)texptr;
for (int by = 0; by < byc; by++) {
u32 *xdest = ydestp;
for (int bx = 0; bx < bxc; bx++) {
u32 *dest = xdest;
for (int n = 0; n < 2; n++) {
// Textures are always 16-byte aligned so this is fine.
uint32x4_t temp1 = vld1q_u32(src);
uint32x4_t temp2 = vld1q_u32(src + 4);
uint32x4_t temp3 = vld1q_u32(src + 8);
uint32x4_t temp4 = vld1q_u32(src + 12);
vst1q_u32(dest, temp1);
dest += pitchBy32;
vst1q_u32(dest, temp2);
dest += pitchBy32;
vst1q_u32(dest, temp3);
dest += pitchBy32;
vst1q_u32(dest, temp4);
dest += pitchBy32;
src += 16;
}
xdest += 4;
}
ydestp += pitchBy32 * 8;
}
} else
#endif
{
const u32 *src = (const u32 *)texptr;
@@ -306,23 +417,6 @@ void DoUnswizzleTex16Basic(const u8 *texptr, u32 *ydestp, int bxc, int byc, u32
}
}
#if !PPSSPP_ARCH(ARM64) && !defined(_M_SSE)
QuickTexHashFunc DoQuickTexHash = &QuickTexHashBasic;
QuickTexHashFunc StableQuickTexHash = &QuickTexHashNonSSE;
UnswizzleTex16Func DoUnswizzleTex16 = &DoUnswizzleTex16Basic;
#endif
// This has to be done after CPUDetect has done its magic.
void SetupTextureDecoder() {
#if PPSSPP_ARCH(ARM_NEON) && !PPSSPP_ARCH(ARM64)
if (cpu_info.bNEON) {
DoQuickTexHash = &QuickTexHashNEON;
StableQuickTexHash = &QuickTexHashNEON;
DoUnswizzleTex16 = &DoUnswizzleTex16NEON;
}
#endif
}
// S3TC / DXT Decoder
class DXTDecoder {
public:
@@ -661,6 +755,138 @@ CheckAlphaResult CheckAlphaRGBA5551SSE2(const u32 *pixelData, int stride, int w,
return CHECKALPHA_FULL;
}
#endif // _M_SSE
#if PPSSPP_ARCH(ARM_NEON)
static inline bool VectorIsNonZeroNEON(const uint32x4_t &v) {
u64 low = vgetq_lane_u64(vreinterpretq_u64_u32(v), 0);
u64 high = vgetq_lane_u64(vreinterpretq_u64_u32(v), 1);
return (low | high) != 0;
}
#ifndef _MSC_VER
// MSVC consider this function the same as the one above! uint16x8_t is typedef'd to the same type as uint32x4_t.
static inline bool VectorIsNonZeroNEON(const uint16x8_t &v) {
u64 low = vgetq_lane_u64(vreinterpretq_u64_u16(v), 0);
u64 high = vgetq_lane_u64(vreinterpretq_u64_u16(v), 1);
return (low | high) != 0;
}
#endif
CheckAlphaResult CheckAlphaRGBA8888NEON(const u32 *pixelData, int stride, int w, int h) {
const u32 *p = (const u32 *)pixelData;
const uint32x4_t mask = vdupq_n_u32(0xFF000000);
uint32x4_t bits = mask;
for (int y = 0; y < h; ++y) {
for (int i = 0; i < w; i += 4) {
const uint32x4_t a = vld1q_u32(&p[i]);
bits = vandq_u32(bits, a);
}
uint32x4_t result = veorq_u32(bits, mask);
if (VectorIsNonZeroNEON(result)) {
return CHECKALPHA_ANY;
}
p += stride;
}
return CHECKALPHA_FULL;
}
CheckAlphaResult CheckAlphaABGR4444NEON(const u32 *pixelData, int stride, int w, int h) {
const u16 *p = (const u16 *)pixelData;
const uint16x8_t mask = vdupq_n_u16((u16)0x000F);
uint16x8_t bits = mask;
for (int y = 0; y < h; ++y) {
for (int i = 0; i < w; i += 8) {
const uint16x8_t a = vld1q_u16(&p[i]);
bits = vandq_u16(bits, a);
}
uint16x8_t result = veorq_u16(bits, mask);
if (VectorIsNonZeroNEON(result)) {
return CHECKALPHA_ANY;
}
p += stride;
}
return CHECKALPHA_FULL;
}
CheckAlphaResult CheckAlphaABGR1555NEON(const u32 *pixelData, int stride, int w, int h) {
const u16 *p = (const u16 *)pixelData;
const uint16x8_t mask = vdupq_n_u16((u16)0x0001);
uint16x8_t bits = mask;
for (int y = 0; y < h; ++y) {
for (int i = 0; i < w; i += 8) {
const uint16x8_t a = vld1q_u16(&p[i]);
bits = vandq_u16(bits, a);
}
uint16x8_t result = veorq_u16(bits, mask);
if (VectorIsNonZeroNEON(result)) {
return CHECKALPHA_ANY;
}
p += stride;
}
return CHECKALPHA_FULL;
}
CheckAlphaResult CheckAlphaRGBA4444NEON(const u32 *pixelData, int stride, int w, int h) {
const u16 *p = (const u16 *)pixelData;
const uint16x8_t mask = vdupq_n_u16((u16)0xF000);
uint16x8_t bits = mask;
for (int y = 0; y < h; ++y) {
for (int i = 0; i < w; i += 8) {
const uint16x8_t a = vld1q_u16(&p[i]);
bits = vandq_u16(bits, a);
}
uint16x8_t result = veorq_u16(bits, mask);
if (VectorIsNonZeroNEON(result)) {
return CHECKALPHA_ANY;
}
p += stride;
}
return CHECKALPHA_FULL;
}
CheckAlphaResult CheckAlphaRGBA5551NEON(const u32 *pixelData, int stride, int w, int h) {
const u16 *p = (const u16 *)pixelData;
const uint16x8_t mask = vdupq_n_u16((u16)0x8000);
uint16x8_t bits = mask;
for (int y = 0; y < h; ++y) {
for (int i = 0; i < w; i += 8) {
const uint16x8_t a = vld1q_u16(&p[i]);
bits = vandq_u16(bits, a);
}
uint16x8_t result = veorq_u16(bits, mask);
if (VectorIsNonZeroNEON(result)) {
return CHECKALPHA_ANY;
}
p += stride;
}
return CHECKALPHA_FULL;
}
#endif
CheckAlphaResult CheckAlphaRGBA8888Basic(const u32 *pixelData, int stride, int w, int h) {