| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#if defined(USE_FMA) |
| 5 |
#define VECTOR_NAMESPACE CPUCull_FMA |
| 6 |
#elif defined(USE_AVX) |
| 7 |
#define VECTOR_NAMESPACE CPUCull_AVX |
| 8 |
#elif defined(USE_SSE41) |
| 9 |
#define VECTOR_NAMESPACE CPUCull_SSE41 |
| 10 |
#elif defined(USE_SSE3) |
| 11 |
#define VECTOR_NAMESPACE CPUCull_SSE3 |
| 12 |
#elif defined(USE_SSE) |
| 13 |
#define VECTOR_NAMESPACE CPUCull_SSE |
| 14 |
#elif defined(USE_NEON) |
| 15 |
#define VECTOR_NAMESPACE CPUCull_NEON |
| 16 |
#elif defined(NO_SIMD) |
| 17 |
#define VECTOR_NAMESPACE CPUCull_Scalar |
| 18 |
#else |
| 19 |
#error This file is meant to be used by CPUCull.cpp only! |
| 20 |
#endif |
| 21 |
|
| 22 |
#if defined(__GNUC__) && defined(USE_FMA) && !(defined(__AVX__) && defined(__FMA__)) |
| 23 |
#define ATTR_TARGET __attribute__((target("avx,fma"))) |
| 24 |
#elif defined(__GNUC__) && defined(USE_AVX) && !defined(__AVX__) |
| 25 |
#define ATTR_TARGET __attribute__((target("avx"))) |
| 26 |
#elif defined(__GNUC__) && defined(USE_SSE41) && !defined(__SSE4_1__) |
| 27 |
#define ATTR_TARGET __attribute__((target("sse4.1"))) |
| 28 |
#elif defined(__GNUC__) && defined(USE_SSE3) && !defined(__SSE3__) |
| 29 |
#define ATTR_TARGET __attribute__((target("sse3"))) |
| 30 |
#else |
| 31 |
#define ATTR_TARGET |
| 32 |
#endif |
| 33 |
|
| 34 |
namespace VECTOR_NAMESPACE |
| 35 |
{ |
| 36 |
#if defined(USE_SSE) |
| 37 |
typedef __m128 Vector; |
| 38 |
#elif defined(USE_NEON) |
| 39 |
typedef float32x4_t Vector; |
| 40 |
#else |
| 41 |
struct alignas(16) Vector |
| 42 |
{ |
| 43 |
float x, y, z, w; |
| 44 |
}; |
| 45 |
#endif |
| 46 |
static_assert(sizeof(Vector) == 16); |
| 47 |
|
| 48 |
#ifdef USE_NEON |
| 49 |
ATTR_TARGET DOLPHIN_FORCE_INLINE static Vector vsetr_f32(float x, float y, float z, float w) |
| 50 |
{ |
| 51 |
float tmp[4] = {x, y, z, w}; |
| 52 |
return vld1q_f32(tmp); |
| 53 |
} |
| 54 |
ATTR_TARGET DOLPHIN_FORCE_INLINE static void vuzp12q_f32(Vector& a, Vector& b) |
| 55 |
{ |
| 56 |
Vector tmp = vuzp2q_f32(a, b); |
| 57 |
a = vuzp1q_f32(a, b); |
| 58 |
b = tmp; |
| 59 |
} |
| 60 |
#endif |
| 61 |
#ifdef USE_SSE |
| 62 |
template <int i> |
| 63 |
ATTR_TARGET DOLPHIN_FORCE_INLINE static Vector vector_broadcast(Vector v) |
| 64 |
{ |
| 65 |
return _mm_shuffle_ps(v, v, _MM_SHUFFLE(i, i, i, i)); |
| 66 |
} |
| 67 |
#endif |
| 68 |
#ifdef USE_AVX |
| 69 |
template <int i> |
| 70 |
ATTR_TARGET DOLPHIN_FORCE_INLINE static __m256 vector_broadcast(__m256 v) |
| 71 |
{ |
| 72 |
return _mm256_shuffle_ps(v, v, _MM_SHUFFLE(i, i, i, i)); |
| 73 |
} |
| 74 |
#endif |
| 75 |
|
| 76 |
#ifdef USE_AVX |
| 77 |
ATTR_TARGET DOLPHIN_FORCE_INLINE static void TransposeYMM(__m256& o0, __m256& o1, |
| 78 |
__m256& o2, __m256& o3) |
| 79 |
{ |
| 80 |
__m256d tmp0 = _mm256_castps_pd(_mm256_unpacklo_ps(o0, o1)); |
| 81 |
__m256d tmp1 = _mm256_castps_pd(_mm256_unpacklo_ps(o2, o3)); |
| 82 |
__m256d tmp2 = _mm256_castps_pd(_mm256_unpackhi_ps(o0, o1)); |
| 83 |
__m256d tmp3 = _mm256_castps_pd(_mm256_unpackhi_ps(o2, o3)); |
| 84 |
o0 = _mm256_castpd_ps(_mm256_unpacklo_pd(tmp0, tmp1)); |
| 85 |
o1 = _mm256_castpd_ps(_mm256_unpackhi_pd(tmp0, tmp1)); |
| 86 |
o2 = _mm256_castpd_ps(_mm256_unpacklo_pd(tmp2, tmp3)); |
| 87 |
o3 = _mm256_castpd_ps(_mm256_unpackhi_pd(tmp2, tmp3)); |
| 88 |
} |
| 89 |
|
| 90 |
ATTR_TARGET DOLPHIN_FORCE_INLINE static void LoadTransposedYMM(const void* source, __m256& o0, |
| 91 |
__m256& o1, __m256& o2, __m256& o3) |
| 92 |
{ |
| 93 |
const Vector* vsource = static_cast<const Vector*>(source); |
| 94 |
o0 = _mm256_broadcast_ps(&vsource[0]); |
| 95 |
o1 = _mm256_broadcast_ps(&vsource[1]); |
| 96 |
o2 = _mm256_broadcast_ps(&vsource[2]); |
| 97 |
o3 = _mm256_broadcast_ps(&vsource[3]); |
| 98 |
TransposeYMM(o0, o1, o2, o3); |
| 99 |
} |
| 100 |
|
| 101 |
ATTR_TARGET DOLPHIN_FORCE_INLINE static void LoadPosYMM(const void* sourcel, const void* sourceh, |
| 102 |
__m256& o0, __m256& o1, __m256& o2) |
| 103 |
{ |
| 104 |
const Vector* vsourcel = static_cast<const Vector*>(sourcel); |
| 105 |
const Vector* vsourceh = static_cast<const Vector*>(sourceh); |
| 106 |
o0 = _mm256_insertf128_ps(_mm256_castps128_ps256(vsourcel[0]), vsourceh[0], 1); |
| 107 |
o1 = _mm256_insertf128_ps(_mm256_castps128_ps256(vsourcel[1]), vsourceh[1], 1); |
| 108 |
o2 = _mm256_insertf128_ps(_mm256_castps128_ps256(vsourcel[2]), vsourceh[2], 1); |
| 109 |
} |
| 110 |
|
| 111 |
ATTR_TARGET DOLPHIN_FORCE_INLINE static void |
| 112 |
LoadTransposedPosYMM(const void* source, __m256& o0, __m256& o1, __m256& o2, __m256& o3) |
| 113 |
{ |
| 114 |
const Vector* vsource = static_cast<const Vector*>(source); |
| 115 |
o0 = _mm256_broadcast_ps(&vsource[0]); |
| 116 |
o1 = _mm256_broadcast_ps(&vsource[1]); |
| 117 |
o2 = _mm256_broadcast_ps(&vsource[2]); |
| 118 |
o3 = _mm256_setr_ps(0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f); |
| 119 |
TransposeYMM(o0, o1, o2, o3); |
| 120 |
} |
| 121 |
|
| 122 |
ATTR_TARGET DOLPHIN_FORCE_INLINE static __m256 ApplyMatrixYMM(__m256 v, __m256 m0, __m256 m1, |
| 123 |
__m256 m2, __m256 m3) |
| 124 |
{ |
| 125 |
__m256 output = _mm256_mul_ps(vector_broadcast<0>(v), m0); |
| 126 |
#ifdef USE_FMA |
| 127 |
output = _mm256_fmadd_ps(vector_broadcast<1>(v), m1, output); |
| 128 |
output = _mm256_fmadd_ps(vector_broadcast<2>(v), m2, output); |
| 129 |
output = _mm256_fmadd_ps(vector_broadcast<3>(v), m3, output); |
| 130 |
#else |
| 131 |
output = _mm256_add_ps(output, _mm256_mul_ps(vector_broadcast<1>(v), m1)); |
| 132 |
output = _mm256_add_ps(output, _mm256_mul_ps(vector_broadcast<2>(v), m2)); |
| 133 |
output = _mm256_add_ps(output, _mm256_mul_ps(vector_broadcast<3>(v), m3)); |
| 134 |
#endif |
| 135 |
return output; |
| 136 |
} |
| 137 |
|
| 138 |
ATTR_TARGET DOLPHIN_FORCE_INLINE static __m256 |
| 139 |
TransformVertexNoTransposeYMM(__m256 vertex, __m256 pos0, __m256 pos1, __m256 pos2, |
| 140 |
__m256 proj0, __m256 proj1, __m256 proj2, __m256 proj3) |
| 141 |
{ |
| 142 |
__m256 mul0 = _mm256_mul_ps(vertex, pos0); |
| 143 |
__m256 mul1 = _mm256_mul_ps(vertex, pos1); |
| 144 |
__m256 mul2 = _mm256_mul_ps(vertex, pos2); |
| 145 |
__m256 mul3 = _mm256_setr_ps(0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f); |
| 146 |
__m256 output = _mm256_hadd_ps(_mm256_hadd_ps(mul0, mul1), _mm256_hadd_ps(mul2, mul3)); |
| 147 |
return ApplyMatrixYMM(output, proj0, proj1, proj2, proj3); |
| 148 |
} |
| 149 |
|
| 150 |
template <bool PositionHas3Elems> |
| 151 |
ATTR_TARGET DOLPHIN_FORCE_INLINE static __m256 |
| 152 |
TransformVertexYMM(__m256 vertex, __m256 pos0, __m256 pos1, __m256 pos2, __m256 pos3, |
| 153 |
__m256 proj0, __m256 proj1, __m256 proj2, __m256 proj3) |
| 154 |
{ |
| 155 |
__m256 output = pos3; |
| 156 |
#ifdef USE_FMA |
| 157 |
output = _mm256_fmadd_ps(vector_broadcast<0>(vertex), pos0, output); |
| 158 |
output = _mm256_fmadd_ps(vector_broadcast<1>(vertex), pos1, output); |
| 159 |
if constexpr (PositionHas3Elems) |
| 160 |
output = _mm256_fmadd_ps(vector_broadcast<2>(vertex), pos2, output); |
| 161 |
#else |
| 162 |
output = _mm256_add_ps(output, _mm256_mul_ps(vector_broadcast<0>(vertex), pos0)); |
| 163 |
output = _mm256_add_ps(output, _mm256_mul_ps(vector_broadcast<1>(vertex), pos1)); |
| 164 |
if constexpr (PositionHas3Elems) |
| 165 |
output = _mm256_add_ps(output, _mm256_mul_ps(vector_broadcast<2>(vertex), pos2)); |
| 166 |
#endif |
| 167 |
return ApplyMatrixYMM(output, proj0, proj1, proj2, proj3); |
| 168 |
} |
| 169 |
|
| 170 |
template <bool PositionHas3Elems, bool PerVertexPosMtx> |
| 171 |
ATTR_TARGET DOLPHIN_FORCE_INLINE static __m256 |
| 172 |
LoadTransform2Vertices(const u8* v0data, const u8* v1data, |
| 173 |
__m256 pos0, __m256 pos1, __m256 pos2, __m256 pos3, |
| 174 |
__m256 proj0, __m256 proj1, __m256 proj2, __m256 proj3) |
| 175 |
{ |
| 176 |
__m256 v01; |
| 177 |
if constexpr (PerVertexPosMtx) |
| 178 |
{ |
| 179 |
|
| 180 |
|
| 181 |
u32 v0idx = v0data[0] & 0x3f; |
| 182 |
u32 v1idx = v1data[0] & 0x3f; |
| 183 |
v0data += sizeof(u32); |
| 184 |
v1data += sizeof(u32); |
| 185 |
|
| 186 |
const float* v0fdata = reinterpret_cast<const float*>(v0data); |
| 187 |
const float* v1fdata = reinterpret_cast<const float*>(v1data); |
| 188 |
|
| 189 |
LoadPosYMM(&xfmem.posMatrices[v0idx * 4], &xfmem.posMatrices[v1idx * 4], pos0, pos1, pos2); |
| 190 |
|
| 191 |
if constexpr (PositionHas3Elems) |
| 192 |
{ |
| 193 |
__m256 base = _mm256_set1_ps(1.0f); |
| 194 |
v01 = _mm256_blend_ps(_mm256_loadu2_m128(v1fdata, v0fdata), base, 0x88); |
| 195 |
} |
| 196 |
else |
| 197 |
{ |
| 198 |
__m256 base = _mm256_unpacklo_ps(_mm256_setzero_ps(), _mm256_set1_ps(1.0f)); |
| 199 |
__m256 v1 = _mm256_castpd_ps(_mm256_broadcast_sd(reinterpret_cast<const double*>(v1data))); |
| 200 |
__m128 v0 = _mm_loadl_pi(_mm_setzero_ps(), reinterpret_cast<const __m64*>(v0data)); |
| 201 |
v01 = _mm256_blend_ps(_mm256_castps128_ps256(v0), v1, 0x30); |
| 202 |
v01 = _mm256_blend_ps(v01, base, 0xcc); |
| 203 |
} |
| 204 |
|
| 205 |
v01 = TransformVertexNoTransposeYMM(v01, pos0, pos1, pos2, proj0, proj1, proj2, proj3); |
| 206 |
} |
| 207 |
else |
| 208 |
{ |
| 209 |
const float* v0fdata = reinterpret_cast<const float*>(v0data); |
| 210 |
const float* v1fdata = reinterpret_cast<const float*>(v1data); |
| 211 |
if constexpr (PositionHas3Elems) |
| 212 |
{ |
| 213 |
v01 = _mm256_loadu2_m128(v1fdata, v0fdata); |
| 214 |
} |
| 215 |
else |
| 216 |
{ |
| 217 |
__m256 v1 = _mm256_castpd_ps(_mm256_broadcast_sd(reinterpret_cast<const double*>(v1data))); |
| 218 |
__m128 v0 = _mm_loadl_pi(_mm_setzero_ps(), reinterpret_cast<const __m64*>(v0data)); |
| 219 |
v01 = _mm256_blend_ps(_mm256_castps128_ps256(v0), v1, 0x30); |
| 220 |
} |
| 221 |
|
| 222 |
#ifdef __clang__ |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
asm("" : "+x"(v01)::); |
| 228 |
#endif |
| 229 |
|
| 230 |
v01 = TransformVertexYMM<PositionHas3Elems>(v01, pos0, pos1, pos2, pos3, |
| 231 |
proj0, proj1, proj2, proj3); |
| 232 |
} |
| 233 |
|
| 234 |
return v01; |
| 235 |
} |
| 236 |
|
| 237 |
#endif |
| 238 |
|
| 239 |
#ifndef USE_AVX |
| 240 |
|
| 241 |
ATTR_TARGET DOLPHIN_FORCE_INLINE static void LoadTransposed(const void* source, Vector& o0, |
| 242 |
Vector& o1, Vector& o2, Vector& o3) |
| 243 |
{ |
| 244 |
#if defined(USE_SSE) |
| 245 |
const Vector* vsource = static_cast<const Vector*>(source); |
| 246 |
o0 = vsource[0]; |
| 247 |
o1 = vsource[1]; |
| 248 |
o2 = vsource[2]; |
| 249 |
o3 = vsource[3]; |
| 250 |
_MM_TRANSPOSE4_PS(o0, o1, o2, o3); |
| 251 |
#elif defined(USE_NEON) |
| 252 |
float32x4x4_t ld = vld4q_f32(static_cast<const float*>(source)); |
| 253 |
o0 = ld.val[0]; |
| 254 |
o1 = ld.val[1]; |
| 255 |
o2 = ld.val[2]; |
| 256 |
o3 = ld.val[3]; |
| 257 |
#else |
| 258 |
const Vector* vsource = static_cast<const Vector*>(source); |
| 259 |
|
| 260 |
o0.x = vsource[0].x; o0.y = vsource[1].x; o0.z = vsource[2].x; o0.w = vsource[3].x; |
| 261 |
o1.x = vsource[0].y; o1.y = vsource[1].y; o1.z = vsource[2].y; o1.w = vsource[3].y; |
| 262 |
o2.x = vsource[0].z; o2.y = vsource[1].z; o2.z = vsource[2].z; o2.w = vsource[3].z; |
| 263 |
o3.x = vsource[0].w; o3.y = vsource[1].w; o3.z = vsource[2].w; o3.w = vsource[3].w; |
| 264 |
|
| 265 |
#endif |
| 266 |
} |
| 267 |
|
| 268 |
ATTR_TARGET DOLPHIN_FORCE_INLINE static void LoadTransposedPos(const void* source, Vector& o0, |
| 269 |
Vector& o1, Vector& o2, Vector& o3) |
| 270 |
{ |
| 271 |
const Vector* vsource = static_cast<const Vector*>(source); |
| 272 |
#if defined(USE_SSE) |
| 273 |
o0 = vsource[0]; |
| 274 |
o1 = vsource[1]; |
| 275 |
o2 = vsource[2]; |
| 276 |
o3 = _mm_setr_ps(0.0f, 0.0f, 0.0f, 1.0f); |
| 277 |
_MM_TRANSPOSE4_PS(o0, o1, o2, o3); |
| 278 |
#elif defined(USE_NEON) |
| 279 |
float32x4x2_t ld01 = vld2q_f32(static_cast<const float*>(source)); |
| 280 |
o0 = ld01.val[0]; |
| 281 |
o1 = ld01.val[1]; |
| 282 |
o2 = vsource[2]; |
| 283 |
o3 = vsetr_f32(0.0f, 0.0f, 0.0f, 1.0f); |
| 284 |
vuzp12q_f32(o2, o3); |
| 285 |
vuzp12q_f32(o0, o2); |
| 286 |
vuzp12q_f32(o1, o3); |
| 287 |
#else |
| 288 |
|
| 289 |
o0.x = vsource[0].x; o0.y = vsource[1].x; o0.z = vsource[2].x; o0.w = 0.0f; |
| 290 |
o1.x = vsource[0].y; o1.y = vsource[1].y; o1.z = vsource[2].y; o1.w = 0.0f; |
| 291 |
o2.x = vsource[0].z; o2.y = vsource[1].z; o2.z = vsource[2].z; o2.w = 0.0f; |
| 292 |
o3.x = vsource[0].w; o3.y = vsource[1].w; o3.z = vsource[2].w; o3.w = 1.0f; |
| 293 |
|
| 294 |
#endif |
| 295 |
} |
| 296 |
#endif |
| 297 |
|
| 298 |
#ifndef USE_NEON |
| 299 |
ATTR_TARGET DOLPHIN_FORCE_INLINE static void LoadPos(const void* source, |
| 300 |
Vector& o0, Vector& o1, Vector& o2) |
| 301 |
{ |
| 302 |
const Vector* vsource = static_cast<const Vector*>(source); |
| 303 |
o0 = vsource[0]; |
| 304 |
o1 = vsource[1]; |
| 305 |
o2 = vsource[2]; |
| 306 |
} |
| 307 |
#endif |
| 308 |
|
| 309 |
ATTR_TARGET DOLPHIN_FORCE_INLINE static Vector ApplyMatrix(Vector v, Vector m0, Vector m1, |
| 310 |
Vector m2, Vector m3) |
| 311 |
{ |
| 312 |
#if defined(USE_SSE) |
| 313 |
Vector output = _mm_mul_ps(vector_broadcast<0>(v), m0); |
| 314 |
#ifdef USE_FMA |
| 315 |
output = _mm_fmadd_ps(vector_broadcast<1>(v), m1, output); |
| 316 |
output = _mm_fmadd_ps(vector_broadcast<2>(v), m2, output); |
| 317 |
output = _mm_fmadd_ps(vector_broadcast<3>(v), m3, output); |
| 318 |
#else |
| 319 |
output = _mm_add_ps(output, _mm_mul_ps(vector_broadcast<1>(v), m1)); |
| 320 |
output = _mm_add_ps(output, _mm_mul_ps(vector_broadcast<2>(v), m2)); |
| 321 |
output = _mm_add_ps(output, _mm_mul_ps(vector_broadcast<3>(v), m3)); |
| 322 |
#endif |
| 323 |
return output; |
| 324 |
#elif defined(USE_NEON) |
| 325 |
Vector output = vmulq_laneq_f32(m0, v, 0); |
| 326 |
output = vfmaq_laneq_f32(output, m1, v, 1); |
| 327 |
output = vfmaq_laneq_f32(output, m2, v, 2); |
| 328 |
output = vfmaq_laneq_f32(output, m3, v, 3); |
| 329 |
return output; |
| 330 |
#else |
| 331 |
Vector output; |
| 332 |
output.x = v.x * m0.x + v.y * m1.x + v.z * m2.x + v.w * m3.x; |
| 333 |
output.y = v.x * m0.y + v.y * m1.y + v.z * m2.y + v.w * m3.y; |
| 334 |
output.z = v.x * m0.z + v.y * m1.z + v.z * m2.z + v.w * m3.z; |
| 335 |
output.w = v.x * m0.w + v.y * m1.w + v.z * m2.w + v.w * m3.w; |
| 336 |
return output; |
| 337 |
#endif |
| 338 |
} |
| 339 |
|
| 340 |
#ifndef USE_NEON |
| 341 |
ATTR_TARGET DOLPHIN_FORCE_INLINE static Vector |
| 342 |
TransformVertexNoTranspose(Vector vertex, Vector pos0, Vector pos1, Vector pos2, |
| 343 |
Vector proj0, Vector proj1, Vector proj2, Vector proj3) |
| 344 |
{ |
| 345 |
#ifdef USE_SSE |
| 346 |
Vector mul0 = _mm_mul_ps(vertex, pos0); |
| 347 |
Vector mul1 = _mm_mul_ps(vertex, pos1); |
| 348 |
Vector mul2 = _mm_mul_ps(vertex, pos2); |
| 349 |
Vector mul3 = _mm_setr_ps(0.0f, 0.0f, 0.0f, 1.0f); |
| 350 |
#ifdef USE_SSE3 |
| 351 |
Vector output = _mm_hadd_ps(_mm_hadd_ps(mul0, mul1), _mm_hadd_ps(mul2, mul3)); |
| 352 |
#else |
| 353 |
Vector t0 = _mm_add_ps(_mm_unpacklo_ps(mul0, mul2), _mm_unpackhi_ps(mul0, mul2)); |
| 354 |
Vector t1 = _mm_add_ps(_mm_unpacklo_ps(mul1, mul3), _mm_unpackhi_ps(mul1, mul3)); |
| 355 |
Vector output = _mm_add_ps(_mm_unpacklo_ps(t0, t1), _mm_unpackhi_ps(t0, t1)); |
| 356 |
#endif |
| 357 |
#else |
| 358 |
Vector output; |
| 359 |
output.x = vertex.x * pos0.x + vertex.y * pos0.y + vertex.z * pos0.z + vertex.w * pos0.w; |
| 360 |
output.y = vertex.x * pos1.x + vertex.y * pos1.y + vertex.z * pos1.z + vertex.w * pos1.w; |
| 361 |
output.z = vertex.x * pos2.x + vertex.y * pos2.y + vertex.z * pos2.z + vertex.w * pos2.w; |
| 362 |
output.w = 1.0f; |
| 363 |
#endif |
| 364 |
output = ApplyMatrix(output, proj0, proj1, proj2, proj3); |
| 365 |
return output; |
| 366 |
} |
| 367 |
#endif |
| 368 |
|
| 369 |
template <bool PositionHas3Elems> |
| 370 |
ATTR_TARGET DOLPHIN_FORCE_INLINE static Vector |
| 371 |
TransformVertex(Vector vertex, Vector pos0, Vector pos1, Vector pos2, Vector pos3, |
| 372 |
Vector proj0, Vector proj1, Vector proj2, Vector proj3) |
| 373 |
{ |
| 374 |
Vector output = pos3; |
| 375 |
#if defined(USE_FMA) |
| 376 |
output = _mm_fmadd_ps(vector_broadcast<0>(vertex), pos0, output); |
| 377 |
output = _mm_fmadd_ps(vector_broadcast<1>(vertex), pos1, output); |
| 378 |
if constexpr (PositionHas3Elems) |
| 379 |
output = _mm_fmadd_ps(vector_broadcast<2>(vertex), pos2, output); |
| 380 |
#elif defined(USE_SSE) |
| 381 |
output = _mm_add_ps(output, _mm_mul_ps(vector_broadcast<0>(vertex), pos0)); |
| 382 |
output = _mm_add_ps(output, _mm_mul_ps(vector_broadcast<1>(vertex), pos1)); |
| 383 |
if constexpr (PositionHas3Elems) |
| 384 |
output = _mm_add_ps(output, _mm_mul_ps(vector_broadcast<2>(vertex), pos2)); |
| 385 |
#elif defined(USE_NEON) |
| 386 |
output = vfmaq_laneq_f32(output, pos0, vertex, 0); |
| 387 |
output = vfmaq_laneq_f32(output, pos1, vertex, 1); |
| 388 |
if constexpr (PositionHas3Elems) |
| 389 |
output = vfmaq_laneq_f32(output, pos2, vertex, 2); |
| 390 |
#else |
| 391 |
output.x += vertex.x * pos0.x + vertex.y * pos1.x; |
| 392 |
output.y += vertex.x * pos0.y + vertex.y * pos1.y; |
| 393 |
output.z += vertex.x * pos0.z + vertex.y * pos1.z; |
| 394 |
if constexpr (PositionHas3Elems) |
| 395 |
{ |
| 396 |
output.x += vertex.z * pos2.x; |
| 397 |
output.y += vertex.z * pos2.y; |
| 398 |
output.z += vertex.z * pos2.z; |
| 399 |
} |
| 400 |
#endif |
| 401 |
output = ApplyMatrix(output, proj0, proj1, proj2, proj3); |
| 402 |
return output; |
| 403 |
} |
| 404 |
|
| 405 |
template <bool PositionHas3Elems, bool PerVertexPosMtx> |
| 406 |
ATTR_TARGET DOLPHIN_FORCE_INLINE static Vector |
| 407 |
LoadTransformVertex(const u8* data, Vector pos0, Vector pos1, Vector pos2, Vector pos3, |
| 408 |
Vector proj0, Vector proj1, Vector proj2, Vector proj3) |
| 409 |
{ |
| 410 |
Vector vertex; |
| 411 |
if constexpr (PerVertexPosMtx) |
| 412 |
{ |
| 413 |
|
| 414 |
|
| 415 |
u32 idx = data[0] & 0x3f; |
| 416 |
data += sizeof(u32); |
| 417 |
|
| 418 |
const float* fdata = reinterpret_cast<const float*>(data); |
| 419 |
|
| 420 |
#ifdef USE_NEON |
| 421 |
LoadTransposedPos(&xfmem.posMatrices[idx * 4], pos0, pos1, pos2, pos3); |
| 422 |
|
| 423 |
if constexpr (PositionHas3Elems) |
| 424 |
{ |
| 425 |
vertex = vld1q_f32(fdata); |
| 426 |
} |
| 427 |
else |
| 428 |
{ |
| 429 |
vertex = vcombine_f32(vld1_f32(fdata), vdup_n_f32(0.0f)); |
| 430 |
} |
| 431 |
|
| 432 |
vertex = TransformVertex<PositionHas3Elems>(vertex, pos0, pos1, pos2, pos3, |
| 433 |
proj0, proj1, proj2, proj3); |
| 434 |
#else |
| 435 |
LoadPos(&xfmem.posMatrices[idx * 4], pos0, pos1, pos2); |
| 436 |
|
| 437 |
if constexpr (PositionHas3Elems) |
| 438 |
{ |
| 439 |
#if defined(USE_SSE) |
| 440 |
#ifdef USE_SSE41 |
| 441 |
Vector base = _mm_set1_ps(1.0f); |
| 442 |
vertex = _mm_blend_ps(_mm_loadu_ps(fdata), base, 8); |
| 443 |
#else |
| 444 |
Vector base = _mm_setr_ps(0.0f, 0.0f, 0.0f, 1.0f); |
| 445 |
Vector mask = _mm_castsi128_ps(_mm_setr_epi32(-1, -1, -1, 0)); |
| 446 |
vertex = _mm_or_ps(_mm_and_ps(_mm_loadu_ps(fdata), mask), base); |
| 447 |
#endif |
| 448 |
#else |
| 449 |
vertex.x = fdata[0]; |
| 450 |
vertex.y = fdata[1]; |
| 451 |
vertex.z = fdata[2]; |
| 452 |
vertex.w = 1.0f; |
| 453 |
#endif |
| 454 |
} |
| 455 |
else |
| 456 |
{ |
| 457 |
#if defined(USE_SSE) |
| 458 |
Vector base = _mm_setr_ps(0.0f, 0.0f, 0.0f, 1.0f); |
| 459 |
vertex = _mm_loadl_pi(base, reinterpret_cast<const __m64*>(fdata)); |
| 460 |
#else |
| 461 |
vertex.x = fdata[0]; |
| 462 |
vertex.y = fdata[1]; |
| 463 |
vertex.z = 0.0f; |
| 464 |
vertex.w = 1.0f; |
| 465 |
#endif |
| 466 |
} |
| 467 |
|
| 468 |
vertex = TransformVertexNoTranspose(vertex, pos0, pos1, pos2, proj0, proj1, proj2, proj3); |
| 469 |
#endif |
| 470 |
} |
| 471 |
else |
| 472 |
{ |
| 473 |
const float* fdata = reinterpret_cast<const float*>(data); |
| 474 |
if constexpr (PositionHas3Elems) |
| 475 |
{ |
| 476 |
#if defined(USE_SSE) |
| 477 |
vertex = _mm_loadu_ps(fdata); |
| 478 |
#elif defined(USE_NEON) |
| 479 |
vertex = vld1q_f32(fdata); |
| 480 |
#else |
| 481 |
vertex.x = fdata[0]; |
| 482 |
vertex.y = fdata[1]; |
| 483 |
vertex.z = fdata[2]; |
| 484 |
vertex.w = 1.0f; |
| 485 |
#endif |
| 486 |
} |
| 487 |
else |
| 488 |
{ |
| 489 |
#if defined(USE_SSE) |
| 490 |
vertex = _mm_loadl_pi(_mm_setzero_ps(), reinterpret_cast<const __m64*>(fdata)); |
| 491 |
#elif defined(USE_NEON) |
| 492 |
vertex = vcombine_f32(vld1_f32(fdata), vdup_n_f32(0.0f)); |
| 493 |
#else |
| 494 |
vertex.x = fdata[0]; |
| 495 |
vertex.y = fdata[1]; |
| 496 |
vertex.z = 0.0f; |
| 497 |
vertex.w = 1.0f; |
| 498 |
#endif |
| 499 |
} |
| 500 |
|
| 501 |
vertex = TransformVertex<PositionHas3Elems>(vertex, pos0, pos1, pos2, pos3, |
| 502 |
proj0, proj1, proj2, proj3); |
| 503 |
} |
| 504 |
|
| 505 |
return vertex; |
| 506 |
} |
| 507 |
|
| 508 |
template <bool PositionHas3Elems, bool PerVertexPosMtx> |
| 509 |
ATTR_TARGET static void TransformVertices(void* output, const void* vertices, u32 stride, int count) |
| 510 |
{ |
| 511 |
const VertexShaderManager& vsmanager = Core::System::GetInstance().GetVertexShaderManager(); |
| 512 |
const u8* cvertices = static_cast<const u8*>(vertices); |
| 513 |
Vector* voutput = static_cast<Vector*>(output); |
| 514 |
u32 idx = g_main_cp_state.matrix_index_a.PosNormalMtxIdx & 0x3f; |
| 515 |
#ifdef USE_AVX |
| 516 |
__m256 proj0, proj1, proj2, proj3; |
| 517 |
__m256 pos0, pos1, pos2, pos3; |
| 518 |
LoadTransposedYMM(vsmanager.constants.projection.data(), proj0, proj1, proj2, proj3); |
| 519 |
LoadTransposedPosYMM(&xfmem.posMatrices[idx * 4], pos0, pos1, pos2, pos3); |
| 520 |
for (int i = 1; i < count; i += 2) |
| 521 |
{ |
| 522 |
const u8* v0data = cvertices; |
| 523 |
const u8* v1data = cvertices + stride; |
| 524 |
__m256 v01 = LoadTransform2Vertices<PositionHas3Elems, PerVertexPosMtx>( |
| 525 |
v0data, v1data, pos0, pos1, pos2, pos3, proj0, proj1, proj2, proj3); |
| 526 |
_mm256_store_ps(reinterpret_cast<float*>(voutput), v01); |
| 527 |
cvertices += stride * 2; |
| 528 |
voutput += 2; |
| 529 |
} |
| 530 |
if (count & 1) |
| 531 |
{ |
| 532 |
*voutput = LoadTransformVertex<PositionHas3Elems, PerVertexPosMtx>( |
| 533 |
cvertices, |
| 534 |
_mm256_castps256_ps128(pos0), _mm256_castps256_ps128(pos1), |
| 535 |
_mm256_castps256_ps128(pos2), _mm256_castps256_ps128(pos3), |
| 536 |
_mm256_castps256_ps128(proj0), _mm256_castps256_ps128(proj1), |
| 537 |
_mm256_castps256_ps128(proj2), _mm256_castps256_ps128(proj3)); |
| 538 |
} |
| 539 |
#else |
| 540 |
Vector proj0, proj1, proj2, proj3; |
| 541 |
Vector pos0, pos1, pos2, pos3; |
| 542 |
LoadTransposed(vsmanager.constants.projection.data(), proj0, proj1, proj2, proj3); |
| 543 |
LoadTransposedPos(&xfmem.posMatrices[idx * 4], pos0, pos1, pos2, pos3); |
| 544 |
for (int i = 0; i < count; i++) |
| 545 |
{ |
| 546 |
*voutput = LoadTransformVertex<PositionHas3Elems, PerVertexPosMtx>( |
| 547 |
cvertices, pos0, pos1, pos2, pos3, proj0, proj1, proj2, proj3); |
| 548 |
cvertices += stride; |
| 549 |
voutput += 1; |
| 550 |
} |
| 551 |
#endif |
| 552 |
} |
| 553 |
|
| 554 |
template <CullMode Mode> |
| 555 |
ATTR_TARGET DOLPHIN_FORCE_INLINE static bool CullTriangle(const CPUCull::TransformedVertex& a, |
| 556 |
const CPUCull::TransformedVertex& b, |
| 557 |
const CPUCull::TransformedVertex& c) |
| 558 |
{ |
| 559 |
if (Mode == CullMode::All) |
| 560 |
return true; |
| 561 |
|
| 562 |
Vector va = reinterpret_cast<const Vector&>(a); |
| 563 |
Vector vb = reinterpret_cast<const Vector&>(b); |
| 564 |
Vector vc = reinterpret_cast<const Vector&>(c); |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
#if defined(USE_SSE) |
| 569 |
Vector wxzya = _mm_shuffle_ps(va, va, _MM_SHUFFLE(1, 2, 0, 3)); |
| 570 |
Vector wxzyc = _mm_shuffle_ps(vc, vc, _MM_SHUFFLE(1, 2, 0, 3)); |
| 571 |
Vector ywzxb = _mm_shuffle_ps(vb, vb, _MM_SHUFFLE(0, 2, 3, 1)); |
| 572 |
Vector part0 = _mm_mul_ps(va, wxzyc); |
| 573 |
Vector part1 = _mm_mul_ps(vc, wxzya); |
| 574 |
Vector part2 = _mm_mul_ps(_mm_sub_ps(part0, part1), ywzxb); |
| 575 |
#ifdef USE_SSE3 |
| 576 |
Vector part3 = _mm_movehdup_ps(part2); |
| 577 |
#else |
| 578 |
Vector part3 = vector_broadcast<1>(part2); |
| 579 |
#endif |
| 580 |
Vector part4 = vector_broadcast<3>(part2); |
| 581 |
Vector part5 = _mm_add_ss(_mm_add_ss(part2, part3), part4); |
| 582 |
float normal_z_dir; |
| 583 |
_mm_store_ss(&normal_z_dir, part5); |
| 584 |
#elif defined(USE_NEON) |
| 585 |
Vector zero = vdupq_n_f32(0.0f); |
| 586 |
Vector wx0ya = vextq_f32(va, vzip1q_f32(va, zero), 3); |
| 587 |
Vector wx0yc = vextq_f32(vc, vzip1q_f32(vc, zero), 3); |
| 588 |
Vector ywxxb = vuzp2q_f32(vb, vdupq_laneq_f32(vb, 0)); |
| 589 |
Vector part0 = vmulq_f32(va, wx0yc); |
| 590 |
Vector part1 = vmulq_f32(vc, wx0ya); |
| 591 |
Vector part2 = vmulq_f32(vsubq_f32(part0, part1), ywxxb); |
| 592 |
float normal_z_dir = vaddvq_f32(part2); |
| 593 |
#else |
| 594 |
float normal_z_dir = (c.w * a.x - a.w * c.x) * b.y + |
| 595 |
(c.x * a.y - a.x * c.y) * b.w + |
| 596 |
(c.y * a.w - a.y * c.w) * b.x; |
| 597 |
#endif |
| 598 |
bool cull = false; |
| 599 |
switch (Mode) |
| 600 |
{ |
| 601 |
case CullMode::None: |
| 602 |
cull = normal_z_dir == 0; |
| 603 |
break; |
| 604 |
case CullMode::Front: |
| 605 |
cull = normal_z_dir <= 0; |
| 606 |
break; |
| 607 |
case CullMode::Back: |
| 608 |
cull = normal_z_dir >= 0; |
| 609 |
break; |
| 610 |
case CullMode::All: |
| 611 |
cull = true; |
| 612 |
break; |
| 613 |
} |
| 614 |
if (cull) |
| 615 |
return true; |
| 616 |
|
| 617 |
#if defined(USE_SSE) |
| 618 |
Vector xyab = _mm_unpacklo_ps(va, vb); |
| 619 |
Vector zwab = _mm_unpackhi_ps(va, vb); |
| 620 |
Vector allx = _mm_shuffle_ps(xyab, vc, _MM_SHUFFLE(0, 0, 1, 0)); |
| 621 |
Vector ally = _mm_shuffle_ps(xyab, vc, _MM_SHUFFLE(1, 1, 3, 2)); |
| 622 |
Vector allpw = _mm_shuffle_ps(zwab, vc, _MM_SHUFFLE(3, 3, 3, 2)); |
| 623 |
Vector allnw = _mm_xor_ps(allpw, _mm_set1_ps(-0.0f)); |
| 624 |
__m128i x_gt_pw = _mm_castps_si128(_mm_cmple_ps(allpw, allx)); |
| 625 |
__m128i y_gt_pw = _mm_castps_si128(_mm_cmple_ps(allpw, ally)); |
| 626 |
__m128i x_lt_nw = _mm_castps_si128(_mm_cmplt_ps(allx, allnw)); |
| 627 |
__m128i y_lt_nw = _mm_castps_si128(_mm_cmplt_ps(ally, allnw)); |
| 628 |
__m128i any_out_of_bounds = _mm_packs_epi16(_mm_packs_epi32(x_lt_nw, y_lt_nw), |
| 629 |
_mm_packs_epi32(x_gt_pw, y_gt_pw)); |
| 630 |
cull |= 0 != _mm_movemask_epi8(_mm_cmpeq_epi32(_mm_set1_epi32(~0), any_out_of_bounds)); |
| 631 |
#elif defined(USE_NEON) |
| 632 |
float64x2_t xyab = vreinterpretq_f64_f32(vzip1q_f32(va, vb)); |
| 633 |
float64x2_t xycc = vreinterpretq_f64_f32(vzip1q_f32(vc, vc)); |
| 634 |
float32x4_t allx = vreinterpretq_f32_f64(vzip1q_f64(xyab, xycc)); |
| 635 |
float32x4_t ally = vreinterpretq_f32_f64(vzip2q_f64(xyab, xycc)); |
| 636 |
float32x4_t allpw = vextq_f32(vzip2q_f32(va, vb), vdupq_laneq_f32(vc, 3), 2); |
| 637 |
float32x4_t allnw = vnegq_f32(allpw); |
| 638 |
uint16x8_t x_gt_pw = vreinterpretq_u16_u32(vcgtq_f32(allx, allpw)); |
| 639 |
uint16x8_t y_gt_pw = vreinterpretq_u16_u32(vcgtq_f32(ally, allpw)); |
| 640 |
uint16x8_t x_lt_nw = vreinterpretq_u16_u32(vcltq_f32(allx, allnw)); |
| 641 |
uint16x8_t y_lt_nw = vreinterpretq_u16_u32(vcltq_f32(ally, allnw)); |
| 642 |
uint8x16_t lt_nw = vreinterpretq_u8_u16(vuzp1q_u16(x_lt_nw, y_lt_nw)); |
| 643 |
uint8x16_t gt_pw = vreinterpretq_u8_u16(vuzp1q_u16(x_gt_pw, y_gt_pw)); |
| 644 |
uint32x4_t any_out_of_bounds = vreinterpretq_u32_u8(vuzp1q_u8(lt_nw, gt_pw)); |
| 645 |
cull |= 0xFFFFFFFF == vmaxvq_u32(any_out_of_bounds); |
| 646 |
#else |
| 647 |
cull |= a.x < -a.w && b.x < -b.w && c.x < -c.w; |
| 648 |
cull |= a.y < -a.w && b.y < -b.w && c.y < -c.w; |
| 649 |
cull |= a.x > a.w && b.x > b.w && c.x > c.w; |
| 650 |
cull |= a.y > a.w && b.y > b.w && c.y > c.w; |
| 651 |
#endif |
| 652 |
|
| 653 |
return cull; |
| 654 |
} |
| 655 |
|
| 656 |
template <OpcodeDecoder::Primitive Primitive, CullMode Mode> |
| 657 |
ATTR_TARGET static bool AreAllVerticesCulled(const CPUCull::TransformedVertex* transformed, |
| 658 |
int count) |
| 659 |
{ |
| 660 |
switch (Primitive) |
| 661 |
{ |
| 662 |
case OpcodeDecoder::Primitive::GX_DRAW_QUADS: |
| 663 |
case OpcodeDecoder::Primitive::GX_DRAW_QUADS_2: |
| 664 |
{ |
| 665 |
int i = 3; |
| 666 |
for (; i < count; i += 4) |
| 667 |
{ |
| 668 |
if (!CullTriangle<Mode>(transformed[i - 3], transformed[i - 2], transformed[i - 1])) |
| 669 |
return false; |
| 670 |
if (!CullTriangle<Mode>(transformed[i - 3], transformed[i - 1], transformed[i - 0])) |
| 671 |
return false; |
| 672 |
} |
| 673 |
|
| 674 |
if (i == count) |
| 675 |
{ |
| 676 |
if (!CullTriangle<Mode>(transformed[i - 3], transformed[i - 2], transformed[i - 1])) |
| 677 |
return false; |
| 678 |
} |
| 679 |
break; |
| 680 |
} |
| 681 |
case OpcodeDecoder::Primitive::GX_DRAW_TRIANGLES: |
| 682 |
for (int i = 2; i < count; i += 3) |
| 683 |
{ |
| 684 |
if (!CullTriangle<Mode>(transformed[i - 2], transformed[i - 1], transformed[i - 0])) |
| 685 |
return false; |
| 686 |
} |
| 687 |
break; |
| 688 |
case OpcodeDecoder::Primitive::GX_DRAW_TRIANGLE_STRIP: |
| 689 |
{ |
| 690 |
bool wind = false; |
| 691 |
for (int i = 2; i < count; ++i) |
| 692 |
{ |
| 693 |
if (!CullTriangle<Mode>(transformed[i - 2], transformed[i - !wind], transformed[i - wind])) |
| 694 |
return false; |
| 695 |
wind = !wind; |
| 696 |
} |
| 697 |
break; |
| 698 |
} |
| 699 |
case OpcodeDecoder::Primitive::GX_DRAW_TRIANGLE_FAN: |
| 700 |
for (int i = 2; i < count; ++i) |
| 701 |
{ |
| 702 |
if (!CullTriangle<Mode>(transformed[0], transformed[i - 1], transformed[i])) |
| 703 |
return false; |
| 704 |
} |
| 705 |
break; |
| 706 |
} |
| 707 |
|
| 708 |
return true; |
| 709 |
} |
| 710 |
|
| 711 |
} |
| 712 |
|
| 713 |
#undef ATTR_TARGET |
| 714 |
#undef VECTOR_NAMESPACE |