diff --git a/GPU/Math3D.h b/GPU/Math3D.h index f14aa6e1c5..1cd2a041a2 100644 --- a/GPU/Math3D.h +++ b/GPU/Math3D.h @@ -1377,15 +1377,20 @@ __forceinline unsigned int Vec4::ToRGBA() const __m128i c = _mm_cvtps_epi32(_mm_mul_ps(SAFE_M128(vec), _mm_set_ps1(255.0f))); __m128i c16 = _mm_packs_epi32(c, c); return _mm_cvtsi128_si32(_mm_packus_epi16(c16, c16)); +#elif PPSSPP_ARCH(ARM64_NEON) + // Round to nearest, like SSE. + uint16x4_t c16 = vqmovun_s32(vcvtnq_s32_f32(vmulq_f32(vec, vdupq_n_f32(255.0f)))); + uint8x8_t c8 = vqmovn_u16(vcombine_u16(c16, c16)); + return vget_lane_u32(vreinterpret_u32_u8(c8), 0); #elif PPSSPP_ARCH(ARM_NEON) - uint16x4_t c16 = vqmovun_s32(vcvtq_s32_f32(vmulq_f32(vec, vdupq_n_f32(255.0f)))); + uint16x4_t c16 = vqmovun_s32(vcvtq_s32_f32(vaddq_f32(vmulq_f32(vec, vdupq_n_f32(255.0f)), vdupq_n_f32(0.5f)))); uint8x8_t c8 = vqmovn_u16(vcombine_u16(c16, c16)); return vget_lane_u32(vreinterpret_u32_u8(c8), 0); #else - return (clamp_u8((int)(r() * 255.f)) << 0) | - (clamp_u8((int)(g() * 255.f)) << 8) | - (clamp_u8((int)(b() * 255.f)) << 16) | - (clamp_u8((int)(a() * 255.f)) << 24); + return (clamp_u8((int)lrintf(r() * 255.f)) << 0) | + (clamp_u8((int)lrintf(g() * 255.f)) << 8) | + (clamp_u8((int)lrintf(b() * 255.f)) << 16) | + (clamp_u8((int)lrintf(a() * 255.f)) << 24); #endif } @@ -1492,7 +1497,7 @@ inline Vec4 Vec4::operator << (const int amount) const { template<> inline Vec4 Vec4::operator >> (const int amount) const { - return Vec4(_mm_srli_epi32(SAFE_M128I(ivec), amount)); + return Vec4(_mm_srai_epi32(SAFE_M128I(ivec), amount)); } // Vec4 operation diff --git a/GPU/Software/Sampler.cpp b/GPU/Software/Sampler.cpp index 83dd05eff7..6c85b88ea5 100644 --- a/GPU/Software/Sampler.cpp +++ b/GPU/Software/Sampler.cpp @@ -468,13 +468,19 @@ static inline void ApplyTexelClamp(int out_u[N], int out_v[N], const int u[N], c } } +// A texel coordinate in 1/16 texel, truncated. Out of int range it's INT_MIN, as the x86 sampler JIT's +// conversion gives (gpu/probe exp5: s >= 2^18 on a 512 wide repeating texture reads texel 0 on a PSP). +static inline int TexelFixed(float f) { + return f >= -2147483648.0f && f < 2147483648.0f ? (int)f : INT_MIN; +} + static inline void GetTexelCoordinates(int level, float s, float t, int &out_u, int &out_v, const SamplerID &samplerID) { int width = samplerID.cached.sizes[level].w; int height = samplerID.cached.sizes[level].h; // The GE truncates to 1/16 texel, toward zero (gpu/probe exp57). - int base_u = (int)(s * width * 16.0f); - int base_v = (int)(t * height * 16.0f); + int base_u = TexelFixed(s * width * 16.0f); + int base_v = TexelFixed(t * height * 16.0f); base_u >>= 4; base_v >>= 4; @@ -696,7 +702,7 @@ static inline Vec4IntResult SOFTRAST_CALL GetTexelCoordinatesQuadS(int level, fl int width = samplerID.cached.sizes[level].w; // The GE truncates to 1/16 texel, toward zero (gpu/probe exp57). - int base_u = (int)(in_s * width * 16) - 8; + int base_u = TexelFixed(in_s * width * 16) - 8; frac_u = base_u & 0x0F; base_u >>= 4; @@ -707,7 +713,7 @@ static inline Vec4IntResult SOFTRAST_CALL GetTexelCoordinatesQuadS(int level, fl static inline Vec4IntResult SOFTRAST_CALL GetTexelCoordinatesQuadT(int level, float in_t, int &frac_v, const SamplerID &samplerID) { int height = samplerID.cached.sizes[level].h; - int base_v = (int)(in_t * height * 16) - 8; + int base_v = TexelFixed(in_t * height * 16) - 8; frac_v = base_v & 0x0F; base_v >>= 4; diff --git a/GPU/Software/TransformUnit.cpp b/GPU/Software/TransformUnit.cpp index 8604e45e1a..32482dc4e5 100644 --- a/GPU/Software/TransformUnit.cpp +++ b/GPU/Software/TransformUnit.cpp @@ -377,7 +377,11 @@ void ComputeTransformState(TransformState *state, const VertexReader &vreader) { canSkipWorldPos = false; } } - state->lightingState.viewDir = PSPViewDirection(gstate.viewMatrix); + // The viewer direction (see PSPViewDirection), normalized like the GE does. + Vec3f viewDir(gstate.viewMatrix[2], gstate.viewMatrix[5], gstate.viewMatrix[8]); + if (GENormalize(viewDir) == 0.0f) + viewDir = Vec3f(0.0f, 0.0f, 1.0f); + state->lightingState.viewDir = viewDir; float world[16]; float view[16];