| 1 | // Copyright 2015 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/VertexLoaderX64.h" |
| 5 | |
| 6 | #include <array> |
| 7 | #include <cstring> |
| 8 | |
| 9 | #include "Common/BitSet.h" |
| 10 | #include "Common/CPUDetect.h" |
| 11 | #include "Common/CommonTypes.h" |
| 12 | #include "Common/Intrinsics.h" |
| 13 | #include "Common/JitRegister.h" |
| 14 | #include "Common/x64ABI.h" |
| 15 | #include "Common/x64Emitter.h" |
| 16 | #include "VideoCommon/CPMemory.h" |
| 17 | #include "VideoCommon/VertexLoaderManager.h" |
| 18 | |
| 19 | using namespace Gen; |
| 20 | |
| 21 | static const X64Reg src_reg = ABI_PARAM1; |
| 22 | static const X64Reg dst_reg = ABI_PARAM2; |
| 23 | static const X64Reg scratch1 = RAX; |
| 24 | static const X64Reg scratch2 = ABI_PARAM3; |
| 25 | static const X64Reg scratch3 = ABI_PARAM4; |
| 26 | // The remaining number of vertices to be processed. Starts at count - 1, and the final loop has it |
| 27 | // at 0. |
| 28 | static const X64Reg remaining_reg = R10; |
| 29 | static const X64Reg skipped_reg = R11; |
| 30 | static const X64Reg base_reg = RBX; |
| 31 | |
| 32 | static const u8* memory_base_ptr = (u8*)&g_main_cp_state.array_strides; |
| 33 | |
| 34 | static OpArg MPIC(const void* ptr, X64Reg scale_reg, int scale = SCALE_1) |
| 35 | { |
| 36 | return MComplex(base_reg, scale_reg, scale, PtrOffset(ptr, memory_base_ptr)); |
| 37 | } |
| 38 | |
| 39 | static OpArg MPIC(const void* ptr) |
| 40 | { |
| 41 | return MDisp(base_reg, PtrOffset(ptr, memory_base_ptr)); |
| 42 | } |
| 43 | |
| 44 | VertexLoaderX64::VertexLoaderX64(const TVtxDesc& vtx_desc, const VAT& vtx_att) |
| 45 | : VertexLoaderBase(vtx_desc, vtx_att) |
| 46 | { |
| 47 | AllocCodeSpace(4096); |
| 48 | ClearCodeSpace(); |
| 49 | GenerateVertexLoader(); |
| 50 | WriteProtect(true); |
| 51 | |
| 52 | Common::JitRegister::Register(region, GetCodePtr(), "VertexLoaderX64\nVtx desc: \n{}\nVAT:\n{}", |
| 53 | vtx_desc, vtx_att); |
| 54 | } |
| 55 | |
| 56 | OpArg VertexLoaderX64::GetVertexAddr(CPArray array, VertexComponentFormat attribute) |
| 57 | { |
| 58 | if (IsIndexed(attribute)) |
| 59 | { |
| 60 | int bits = attribute == VertexComponentFormat::Index8 ? 8 : 16; |
| 61 | LoadAndSwap(bits, scratch1, MDisp(src_reg, m_src_ofs)); |
| 62 | m_src_ofs += bits / 8; |
| 63 | if (array == CPArray::Position) |
| 64 | { |
| 65 | CMP(bits, R(scratch1), Imm8(-1)); |
| 66 | m_skip_vertex = J_CC(CC_E, Jump::Near); |
| 67 | } |
| 68 | IMUL(32, scratch1, MPIC(&g_main_cp_state.array_strides[array])); |
| 69 | MOV(64, R(scratch2), MPIC(&VertexLoaderManager::cached_arraybases[array])); |
| 70 | return MRegSum(scratch1, scratch2); |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | return MDisp(src_reg, m_src_ofs); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void VertexLoaderX64::ReadVertex(OpArg data, VertexComponentFormat attribute, |
| 79 | ComponentFormat format, int count_in, int count_out, |
| 80 | bool dequantize, u8 scaling_exponent, |
| 81 | AttributeFormat* native_format) |
| 82 | { |
| 83 | using ShuffleRow = std::array<__m128i, 3>; |
| 84 | static const Common::EnumMap<ShuffleRow, ComponentFormat::InvalidFloat7> shuffle_lut = { |
| 85 | ShuffleRow{_mm_set_epi32(0xFFFFFFFFL, 0xFFFFFFFFL, 0xFFFFFFFFL, 0xFFFFFF00L), // 1x u8 |
| 86 | _mm_set_epi32(0xFFFFFFFFL, 0xFFFFFFFFL, 0xFFFFFF01L, 0xFFFFFF00L), // 2x u8 |
| 87 | _mm_set_epi32(0xFFFFFFFFL, 0xFFFFFF02L, 0xFFFFFF01L, 0xFFFFFF00L)}, // 3x u8 |
| 88 | ShuffleRow{_mm_set_epi32(0xFFFFFFFFL, 0xFFFFFFFFL, 0xFFFFFFFFL, 0x00FFFFFFL), // 1x s8 |
| 89 | _mm_set_epi32(0xFFFFFFFFL, 0xFFFFFFFFL, 0x01FFFFFFL, 0x00FFFFFFL), // 2x s8 |
| 90 | _mm_set_epi32(0xFFFFFFFFL, 0x02FFFFFFL, 0x01FFFFFFL, 0x00FFFFFFL)}, // 3x s8 |
| 91 | ShuffleRow{_mm_set_epi32(0xFFFFFFFFL, 0xFFFFFFFFL, 0xFFFFFFFFL, 0xFFFF0001L), // 1x u16 |
| 92 | _mm_set_epi32(0xFFFFFFFFL, 0xFFFFFFFFL, 0xFFFF0203L, 0xFFFF0001L), // 2x u16 |
| 93 | _mm_set_epi32(0xFFFFFFFFL, 0xFFFF0405L, 0xFFFF0203L, 0xFFFF0001L)}, // 3x u16 |
| 94 | ShuffleRow{_mm_set_epi32(0xFFFFFFFFL, 0xFFFFFFFFL, 0xFFFFFFFFL, 0x0001FFFFL), // 1x s16 |
| 95 | _mm_set_epi32(0xFFFFFFFFL, 0xFFFFFFFFL, 0x0203FFFFL, 0x0001FFFFL), // 2x s16 |
| 96 | _mm_set_epi32(0xFFFFFFFFL, 0x0405FFFFL, 0x0203FFFFL, 0x0001FFFFL)}, // 3x s16 |
| 97 | ShuffleRow{_mm_set_epi32(0xFFFFFFFFL, 0xFFFFFFFFL, 0xFFFFFFFFL, 0x00010203L), // 1x float |
| 98 | _mm_set_epi32(0xFFFFFFFFL, 0xFFFFFFFFL, 0x04050607L, 0x00010203L), // 2x float |
| 99 | _mm_set_epi32(0xFFFFFFFFL, 0x08090A0BL, 0x04050607L, 0x00010203L)}, // 3x float |
| 100 | ShuffleRow{_mm_set_epi32(0xFFFFFFFFL, 0xFFFFFFFFL, 0xFFFFFFFFL, 0x00010203L), // 1x invalid |
| 101 | _mm_set_epi32(0xFFFFFFFFL, 0xFFFFFFFFL, 0x04050607L, 0x00010203L), // 2x invalid |
| 102 | _mm_set_epi32(0xFFFFFFFFL, 0x08090A0BL, 0x04050607L, 0x00010203L)}, // 3x invalid |
| 103 | ShuffleRow{_mm_set_epi32(0xFFFFFFFFL, 0xFFFFFFFFL, 0xFFFFFFFFL, 0x00010203L), // 1x invalid |
| 104 | _mm_set_epi32(0xFFFFFFFFL, 0xFFFFFFFFL, 0x04050607L, 0x00010203L), // 2x invalid |
| 105 | _mm_set_epi32(0xFFFFFFFFL, 0x08090A0BL, 0x04050607L, 0x00010203L)}, // 3x invalid |
| 106 | ShuffleRow{_mm_set_epi32(0xFFFFFFFFL, 0xFFFFFFFFL, 0xFFFFFFFFL, 0x00010203L), // 1x invalid |
| 107 | _mm_set_epi32(0xFFFFFFFFL, 0xFFFFFFFFL, 0x04050607L, 0x00010203L), // 2x invalid |
| 108 | _mm_set_epi32(0xFFFFFFFFL, 0x08090A0BL, 0x04050607L, 0x00010203L)}, // 3x invalid |
| 109 | }; |
| 110 | static const __m128 scale_factors[32] = { |
| 111 | _mm_set_ps1(1. / (1u << 0)), _mm_set_ps1(1. / (1u << 1)), _mm_set_ps1(1. / (1u << 2)), |
| 112 | _mm_set_ps1(1. / (1u << 3)), _mm_set_ps1(1. / (1u << 4)), _mm_set_ps1(1. / (1u << 5)), |
| 113 | _mm_set_ps1(1. / (1u << 6)), _mm_set_ps1(1. / (1u << 7)), _mm_set_ps1(1. / (1u << 8)), |
| 114 | _mm_set_ps1(1. / (1u << 9)), _mm_set_ps1(1. / (1u << 10)), _mm_set_ps1(1. / (1u << 11)), |
| 115 | _mm_set_ps1(1. / (1u << 12)), _mm_set_ps1(1. / (1u << 13)), _mm_set_ps1(1. / (1u << 14)), |
| 116 | _mm_set_ps1(1. / (1u << 15)), _mm_set_ps1(1. / (1u << 16)), _mm_set_ps1(1. / (1u << 17)), |
| 117 | _mm_set_ps1(1. / (1u << 18)), _mm_set_ps1(1. / (1u << 19)), _mm_set_ps1(1. / (1u << 20)), |
| 118 | _mm_set_ps1(1. / (1u << 21)), _mm_set_ps1(1. / (1u << 22)), _mm_set_ps1(1. / (1u << 23)), |
| 119 | _mm_set_ps1(1. / (1u << 24)), _mm_set_ps1(1. / (1u << 25)), _mm_set_ps1(1. / (1u << 26)), |
| 120 | _mm_set_ps1(1. / (1u << 27)), _mm_set_ps1(1. / (1u << 28)), _mm_set_ps1(1. / (1u << 29)), |
| 121 | _mm_set_ps1(1. / (1u << 30)), _mm_set_ps1(1. / (1u << 31)), |
| 122 | }; |
| 123 | |
| 124 | X64Reg coords = XMM0; |
| 125 | |
| 126 | const auto write_zfreeze = [&] { // zfreeze |
| 127 | if (native_format == &m_native_vtx_decl.position) |
| 128 | { |
| 129 | CMP(32, R(remaining_reg), Imm8(3)); |
| 130 | FixupBranch dont_store = J_CC(CC_AE); |
| 131 | // The position cache is composed of 3 rows of 4 floats each; since each float is 4 bytes, |
| 132 | // we need to scale by 4 twice to cover the 4 floats. |
| 133 | LEA(32, scratch3, MScaled(remaining_reg, SCALE_4, 0)); |
| 134 | MOVUPS(MPIC(VertexLoaderManager::position_cache.data(), scratch3, SCALE_4), coords); |
| 135 | SetJumpTarget(dont_store); |
| 136 | } |
| 137 | else if (native_format == &m_native_vtx_decl.normals[0]) |
| 138 | { |
| 139 | TEST(32, R(remaining_reg), R(remaining_reg)); |
| 140 | FixupBranch dont_store = J_CC(CC_NZ); |
| 141 | // For similar reasons, the cached normal is 4 floats each |
| 142 | MOVUPS(MPIC(VertexLoaderManager::normal_cache.data()), coords); |
| 143 | SetJumpTarget(dont_store); |
| 144 | } |
| 145 | else if (native_format == &m_native_vtx_decl.normals[1]) |
| 146 | { |
| 147 | TEST(32, R(remaining_reg), R(remaining_reg)); |
| 148 | FixupBranch dont_store = J_CC(CC_NZ); |
| 149 | // For similar reasons, the cached tangent and binormal are 4 floats each |
| 150 | MOVUPS(MPIC(VertexLoaderManager::tangent_cache.data()), coords); |
| 151 | SetJumpTarget(dont_store); |
| 152 | } |
| 153 | else if (native_format == &m_native_vtx_decl.normals[2]) |
| 154 | { |
| 155 | CMP(32, R(remaining_reg), R(remaining_reg)); |
| 156 | FixupBranch dont_store = J_CC(CC_NZ); |
| 157 | // For similar reasons, the cached tangent and binormal are 4 floats each |
| 158 | MOVUPS(MPIC(VertexLoaderManager::binormal_cache.data()), coords); |
| 159 | SetJumpTarget(dont_store); |
| 160 | } |
| 161 | }; |
| 162 | |
| 163 | int elem_size = GetElementSize(format); |
| 164 | int load_bytes = elem_size * count_in; |
| 165 | OpArg dest = MDisp(dst_reg, m_dst_ofs); |
| 166 | |
| 167 | native_format->components = count_out; |
| 168 | native_format->enable = true; |
| 169 | native_format->offset = m_dst_ofs; |
| 170 | native_format->type = ComponentFormat::Float; |
| 171 | native_format->integer = false; |
| 172 | |
| 173 | m_dst_ofs += sizeof(float) * count_out; |
| 174 | |
| 175 | if (attribute == VertexComponentFormat::Direct) |
| 176 | m_src_ofs += load_bytes; |
| 177 | |
| 178 | if (cpu_info.bSSSE3) |
| 179 | { |
| 180 | if (load_bytes > 8) |
| 181 | MOVDQU(coords, data); |
| 182 | else if (load_bytes > 4) |
| 183 | MOVQ_xmm(coords, data); |
| 184 | else |
| 185 | MOVD_xmm(coords, data); |
| 186 | |
| 187 | PSHUFB(coords, MPIC(&shuffle_lut[format][count_in - 1])); |
| 188 | |
| 189 | // Sign-extend. |
| 190 | if (format == ComponentFormat::Byte) |
| 191 | PSRAD(coords, 24); |
| 192 | if (format == ComponentFormat::Short) |
| 193 | PSRAD(coords, 16); |
| 194 | } |
| 195 | else |
| 196 | { |
| 197 | // SSE2 |
| 198 | X64Reg temp = XMM1; |
| 199 | switch (format) |
| 200 | { |
| 201 | case ComponentFormat::UByte: |
| 202 | MOVD_xmm(coords, data); |
| 203 | PXOR(temp, R(temp)); |
| 204 | PUNPCKLBW(coords, R(temp)); |
| 205 | PUNPCKLWD(coords, R(temp)); |
| 206 | break; |
| 207 | case ComponentFormat::Byte: |
| 208 | MOVD_xmm(coords, data); |
| 209 | PUNPCKLBW(coords, R(coords)); |
| 210 | PUNPCKLWD(coords, R(coords)); |
| 211 | PSRAD(coords, 24); |
| 212 | break; |
| 213 | case ComponentFormat::UShort: |
| 214 | case ComponentFormat::Short: |
| 215 | switch (count_in) |
| 216 | { |
| 217 | case 1: |
| 218 | LoadAndSwap(32, scratch3, data); |
| 219 | MOVD_xmm(coords, R(scratch3)); // ......X. |
| 220 | break; |
| 221 | case 2: |
| 222 | LoadAndSwap(32, scratch3, data); |
| 223 | MOVD_xmm(coords, R(scratch3)); // ......XY |
| 224 | PSHUFLW(coords, R(coords), 0x24); // ....Y.X. |
| 225 | break; |
| 226 | case 3: |
| 227 | LoadAndSwap(64, scratch3, data); |
| 228 | MOVQ_xmm(coords, R(scratch3)); // ....XYZ. |
| 229 | PUNPCKLQDQ(coords, R(coords)); // ..Z.XYZ. |
| 230 | PSHUFLW(coords, R(coords), 0xAC); // ..Z.Y.X. |
| 231 | break; |
| 232 | } |
| 233 | if (format == ComponentFormat::Short) |
| 234 | PSRAD(coords, 16); |
| 235 | else |
| 236 | PSRLD(coords, 16); |
| 237 | break; |
| 238 | case ComponentFormat::Float: |
| 239 | case ComponentFormat::InvalidFloat5: |
| 240 | case ComponentFormat::InvalidFloat6: |
| 241 | case ComponentFormat::InvalidFloat7: |
| 242 | // Floats don't need to be scaled or converted, |
| 243 | // so we can just load/swap/store them directly |
| 244 | // and return early. |
| 245 | // (In SSSE3 we still need to store them.) |
| 246 | for (int i = 0; i < count_in; i++) |
| 247 | { |
| 248 | LoadAndSwap(32, scratch3, data); |
| 249 | MOV(32, dest, R(scratch3)); |
| 250 | data.AddMemOffset(sizeof(float)); |
| 251 | dest.AddMemOffset(sizeof(float)); |
| 252 | |
| 253 | // zfreeze |
| 254 | if (native_format == &m_native_vtx_decl.position || |
| 255 | native_format == &m_native_vtx_decl.normals[1] || |
| 256 | native_format == &m_native_vtx_decl.normals[2]) |
| 257 | { |
| 258 | if (cpu_info.bSSE4_1) |
| 259 | { |
| 260 | PINSRD(coords, R(scratch3), i); |
| 261 | } |
| 262 | else |
| 263 | { |
| 264 | PINSRW(coords, R(scratch3), 2 * i + 0); |
| 265 | SHR(32, R(scratch3), Imm8(16)); |
| 266 | PINSRW(coords, R(scratch3), 2 * i + 1); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | write_zfreeze(); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if (format < ComponentFormat::Float) |
| 276 | { |
| 277 | CVTDQ2PS(coords, R(coords)); |
| 278 | |
| 279 | if (dequantize && scaling_exponent) |
| 280 | MULPS(coords, MPIC(&scale_factors[scaling_exponent])); |
| 281 | } |
| 282 | |
| 283 | switch (count_out) |
| 284 | { |
| 285 | case 1: |
| 286 | MOVSS(dest, coords); |
| 287 | break; |
| 288 | case 2: |
| 289 | MOVLPS(dest, coords); |
| 290 | break; |
| 291 | case 3: |
| 292 | MOVUPS(dest, coords); |
| 293 | break; |
| 294 | } |
| 295 | |
| 296 | write_zfreeze(); |
| 297 | } |
| 298 | |
| 299 | void VertexLoaderX64::ReadColor(OpArg data, VertexComponentFormat attribute, ColorFormat format) |
| 300 | { |
| 301 | int load_bytes = 0; |
| 302 | switch (format) |
| 303 | { |
| 304 | case ColorFormat::RGB888: |
| 305 | case ColorFormat::RGB888x: |
| 306 | case ColorFormat::RGBA8888: |
| 307 | MOV(32, R(scratch1), data); |
| 308 | if (format != ColorFormat::RGBA8888) |
| 309 | OR(32, R(scratch1), Imm32(0xFF000000)); |
| 310 | MOV(32, MDisp(dst_reg, m_dst_ofs), R(scratch1)); |
| 311 | load_bytes = format == ColorFormat::RGB888 ? 3 : 4; |
| 312 | break; |
| 313 | |
| 314 | case ColorFormat::RGB565: |
| 315 | // RRRRRGGG GGGBBBBB |
| 316 | // AAAAAAAA BBBBBBBB GGGGGGGG RRRRRRRR |
| 317 | LoadAndSwap(16, scratch1, data); |
| 318 | if (cpu_info.bBMI1 && cpu_info.bBMI2FastParallelBitOps) |
| 319 | { |
| 320 | MOV(32, R(scratch2), Imm32(0x07C3F7C0)); |
| 321 | PDEP(32, scratch3, scratch1, R(scratch2)); |
| 322 | |
| 323 | MOV(32, R(scratch2), Imm32(0xF8FCF800)); |
| 324 | PDEP(32, scratch1, scratch1, R(scratch2)); |
| 325 | ANDN(32, scratch2, scratch2, R(scratch3)); |
| 326 | |
| 327 | OR(32, R(scratch1), R(scratch2)); |
| 328 | } |
| 329 | else |
| 330 | { |
| 331 | SHL(32, R(scratch1), Imm8(11)); |
| 332 | LEA(32, scratch2, MScaled(scratch1, SCALE_4, 0)); |
| 333 | LEA(32, scratch3, MScaled(scratch2, SCALE_8, 0)); |
| 334 | AND(32, R(scratch1), Imm32(0x0000F800)); |
| 335 | AND(32, R(scratch2), Imm32(0x00FC0000)); |
| 336 | AND(32, R(scratch3), Imm32(0xF8000000)); |
| 337 | OR(32, R(scratch1), R(scratch2)); |
| 338 | OR(32, R(scratch1), R(scratch3)); |
| 339 | |
| 340 | MOV(32, R(scratch2), R(scratch1)); |
| 341 | SHR(32, R(scratch1), Imm8(5)); |
| 342 | AND(32, R(scratch1), Imm32(0x07000700)); |
| 343 | OR(32, R(scratch1), R(scratch2)); |
| 344 | |
| 345 | SHR(32, R(scratch2), Imm8(6)); |
| 346 | AND(32, R(scratch2), Imm32(0x00030000)); |
| 347 | OR(32, R(scratch1), R(scratch2)); |
| 348 | } |
| 349 | OR(32, R(scratch1), Imm32(0x000000FF)); |
| 350 | SwapAndStore(32, MDisp(dst_reg, m_dst_ofs), scratch1); |
| 351 | load_bytes = 2; |
| 352 | break; |
| 353 | |
| 354 | case ColorFormat::RGBA4444: |
| 355 | // RRRRGGGG BBBBAAAA |
| 356 | // AAAAAAAA BBBBBBBB GGGGGGGG RRRRRRRR |
| 357 | LoadAndSwap(16, scratch1, data); |
| 358 | if (cpu_info.bBMI2FastParallelBitOps) |
| 359 | { |
| 360 | MOV(32, R(scratch2), Imm32(0x0F0F0F0F)); |
| 361 | PDEP(32, scratch1, scratch1, R(scratch2)); |
| 362 | } |
| 363 | else |
| 364 | { |
| 365 | MOV(32, R(scratch2), R(scratch1)); |
| 366 | SHL(32, R(scratch1), Imm8(8)); |
| 367 | OR(32, R(scratch1), R(scratch2)); |
| 368 | AND(32, R(scratch1), Imm32(0x00FF00FF)); |
| 369 | |
| 370 | MOV(32, R(scratch2), R(scratch1)); |
| 371 | SHL(32, R(scratch1), Imm8(4)); |
| 372 | OR(32, R(scratch1), R(scratch2)); |
| 373 | AND(32, R(scratch1), Imm32(0x0F0F0F0F)); |
| 374 | } |
| 375 | MOV(32, R(scratch2), R(scratch1)); |
| 376 | SHL(32, R(scratch1), Imm8(4)); |
| 377 | OR(32, R(scratch1), R(scratch2)); |
| 378 | SwapAndStore(32, MDisp(dst_reg, m_dst_ofs), scratch1); |
| 379 | load_bytes = 2; |
| 380 | break; |
| 381 | |
| 382 | case ColorFormat::RGBA6666: |
| 383 | // RRRRRRGG GGGGBBBB BBAAAAAA |
| 384 | // AAAAAAAA BBBBBBBB GGGGGGGG RRRRRRRR |
| 385 | data.AddMemOffset(-1); // subtract one from address so we can use a 32bit load and bswap |
| 386 | LoadAndSwap(32, scratch1, data); |
| 387 | if (cpu_info.bBMI2FastParallelBitOps) |
| 388 | { |
| 389 | MOV(32, R(scratch2), Imm32(0xFCFCFCFC)); |
| 390 | PDEP(32, scratch1, scratch1, R(scratch2)); |
| 391 | MOV(32, R(scratch2), R(scratch1)); |
| 392 | } |
| 393 | else |
| 394 | { |
| 395 | LEA(32, scratch2, MScaled(scratch1, SCALE_4, 0)); // ______RR RRRRGGGG GGBBBBBB AAAAAA__ |
| 396 | AND(32, R(scratch2), Imm32(0x00003FFC)); // ________ ________ __BBBBBB AAAAAA__ |
| 397 | SHL(32, R(scratch1), Imm8(6)); // __RRRRRR GGGGGGBB BBBBAAAA AA______ |
| 398 | AND(32, R(scratch1), Imm32(0x3FFC0000)); // __RRRRRR GGGGGG__ ________ ________ |
| 399 | OR(32, R(scratch1), R(scratch2)); // __RRRRRR GGGGGG__ __BBBBBB AAAAAA__ |
| 400 | |
| 401 | LEA(32, scratch2, MScaled(scratch1, SCALE_4, 0)); // RRRRRRGG GGGG____ BBBBBBAA AAAA____ |
| 402 | AND(32, R(scratch2), Imm32(0xFC00FC00)); // RRRRRR__ ________ BBBBBB__ ________ |
| 403 | AND(32, R(scratch1), Imm32(0x00FC00FC)); // ________ GGGGGG__ ________ AAAAAA__ |
| 404 | OR(32, R(scratch1), R(scratch2)); // RRRRRR__ GGGGGG__ BBBBBB__ AAAAAA__ |
| 405 | MOV(32, R(scratch2), R(scratch1)); |
| 406 | } |
| 407 | SHR(32, R(scratch1), Imm8(6)); |
| 408 | AND(32, R(scratch1), Imm32(0x03030303)); |
| 409 | OR(32, R(scratch1), R(scratch2)); |
| 410 | SwapAndStore(32, MDisp(dst_reg, m_dst_ofs), scratch1); |
| 411 | load_bytes = 3; |
| 412 | break; |
| 413 | } |
| 414 | if (attribute == VertexComponentFormat::Direct) |
| 415 | m_src_ofs += load_bytes; |
| 416 | } |
| 417 | |
| 418 | void VertexLoaderX64::GenerateVertexLoader() |
| 419 | { |
| 420 | BitSet32 regs = {src_reg, dst_reg, scratch1, scratch2, |
| 421 | scratch3, remaining_reg, skipped_reg, base_reg}; |
| 422 | regs &= ABI_ALL_CALLEE_SAVED; |
| 423 | regs[RBP] = true; // Give us a stack frame |
| 424 | ABI_PushRegistersAndAdjustStack(regs, 0); |
| 425 | |
| 426 | // Backup count since we're going to count it down. |
| 427 | PUSH(32, R(ABI_PARAM3)); |
| 428 | |
| 429 | // ABI_PARAM3 is one of the lower registers, so free it for scratch2. |
| 430 | // We also have it end at a value of 0, to simplify indexing for zfreeze; |
| 431 | // this requires subtracting 1 at the start. |
| 432 | LEA(32, remaining_reg, MDisp(ABI_PARAM3, -1)); |
| 433 | |
| 434 | MOV(64, R(base_reg), R(ABI_PARAM4)); |
| 435 | |
| 436 | if (IsIndexed(m_VtxDesc.low.Position)) |
| 437 | XOR(32, R(skipped_reg), R(skipped_reg)); |
| 438 | |
| 439 | // TODO: load constants into registers outside the main loop |
| 440 | |
| 441 | const u8* loop_start = GetCodePtr(); |
| 442 | |
| 443 | if (m_VtxDesc.low.PosMatIdx) |
| 444 | { |
| 445 | MOVZX(32, 8, scratch1, MDisp(src_reg, m_src_ofs)); |
| 446 | AND(32, R(scratch1), Imm8(0x3F)); |
| 447 | MOV(32, MDisp(dst_reg, m_dst_ofs), R(scratch1)); |
| 448 | |
| 449 | // zfreeze |
| 450 | CMP(32, R(remaining_reg), Imm8(3)); |
| 451 | FixupBranch dont_store = J_CC(CC_AE); |
| 452 | MOV(32, MPIC(VertexLoaderManager::position_matrix_index_cache.data(), remaining_reg, SCALE_4), |
| 453 | R(scratch1)); |
| 454 | SetJumpTarget(dont_store); |
| 455 | |
| 456 | m_native_vtx_decl.posmtx.components = 4; |
| 457 | m_native_vtx_decl.posmtx.enable = true; |
| 458 | m_native_vtx_decl.posmtx.offset = m_dst_ofs; |
| 459 | m_native_vtx_decl.posmtx.type = ComponentFormat::UByte; |
| 460 | m_native_vtx_decl.posmtx.integer = true; |
| 461 | m_src_ofs += sizeof(u8); |
| 462 | m_dst_ofs += sizeof(u32); |
| 463 | } |
| 464 | |
| 465 | std::array<u32, 8> texmatidx_ofs; |
| 466 | for (size_t i = 0; i < m_VtxDesc.low.TexMatIdx.Size(); i++) |
| 467 | { |
| 468 | if (m_VtxDesc.low.TexMatIdx[i]) |
| 469 | texmatidx_ofs[i] = m_src_ofs++; |
| 470 | } |
| 471 | |
| 472 | OpArg data = GetVertexAddr(CPArray::Position, m_VtxDesc.low.Position); |
| 473 | int pos_elements = m_VtxAttr.g0.PosElements == CoordComponentCount::XY ? 2 : 3; |
| 474 | ReadVertex(data, m_VtxDesc.low.Position, m_VtxAttr.g0.PosFormat, pos_elements, pos_elements, |
| 475 | m_VtxAttr.g0.ByteDequant, m_VtxAttr.g0.PosFrac, &m_native_vtx_decl.position); |
| 476 | |
| 477 | if (m_VtxDesc.low.Normal != VertexComponentFormat::NotPresent) |
| 478 | { |
| 479 | static constexpr Common::EnumMap<u8, ComponentFormat::InvalidFloat7> SCALE_MAP = {7, 6, 15, 14, |
| 480 | 0, 0, 0, 0}; |
| 481 | const u8 scaling_exponent = SCALE_MAP[m_VtxAttr.g0.NormalFormat]; |
| 482 | |
| 483 | // Normal |
| 484 | data = GetVertexAddr(CPArray::Normal, m_VtxDesc.low.Normal); |
| 485 | ReadVertex(data, m_VtxDesc.low.Normal, m_VtxAttr.g0.NormalFormat, 3, 3, true, scaling_exponent, |
| 486 | &m_native_vtx_decl.normals[0]); |
| 487 | |
| 488 | if (m_VtxAttr.g0.NormalElements == NormalComponentCount::NTB) |
| 489 | { |
| 490 | const bool index3 = IsIndexed(m_VtxDesc.low.Normal) && m_VtxAttr.g0.NormalIndex3; |
| 491 | const int elem_size = GetElementSize(m_VtxAttr.g0.NormalFormat); |
| 492 | const int load_bytes = elem_size * 3; |
| 493 | |
| 494 | // Tangent |
| 495 | // If in Index3 mode, and indexed components are used, replace the index with a new index. |
| 496 | if (index3) |
| 497 | data = GetVertexAddr(CPArray::Normal, m_VtxDesc.low.Normal); |
| 498 | // The tangent comes after the normal; even in index3 mode, this offset is applied. |
| 499 | // Note that this is different from adding 1 to the index, as the stride for indices may be |
| 500 | // different from the size of the tangent itself. |
| 501 | data.AddMemOffset(load_bytes); |
| 502 | |
| 503 | ReadVertex(data, m_VtxDesc.low.Normal, m_VtxAttr.g0.NormalFormat, 3, 3, true, |
| 504 | scaling_exponent, &m_native_vtx_decl.normals[1]); |
| 505 | |
| 506 | // Undo the offset above so that data points to the normal instead of the tangent. |
| 507 | // This way, we can add 2*elem_size below to always point to the binormal, even if we replace |
| 508 | // data with a new index (which would point to the normal). |
| 509 | data.AddMemOffset(-load_bytes); |
| 510 | |
| 511 | // Binormal |
| 512 | if (index3) |
| 513 | data = GetVertexAddr(CPArray::Normal, m_VtxDesc.low.Normal); |
| 514 | data.AddMemOffset(load_bytes * 2); |
| 515 | |
| 516 | ReadVertex(data, m_VtxDesc.low.Normal, m_VtxAttr.g0.NormalFormat, 3, 3, true, |
| 517 | scaling_exponent, &m_native_vtx_decl.normals[2]); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | for (u8 i = 0; i < m_VtxDesc.low.Color.Size(); i++) |
| 522 | { |
| 523 | if (m_VtxDesc.low.Color[i] != VertexComponentFormat::NotPresent) |
| 524 | { |
| 525 | data = GetVertexAddr(CPArray::Color0 + i, m_VtxDesc.low.Color[i]); |
| 526 | ReadColor(data, m_VtxDesc.low.Color[i], m_VtxAttr.GetColorFormat(i)); |
| 527 | m_native_vtx_decl.colors[i].components = 4; |
| 528 | m_native_vtx_decl.colors[i].enable = true; |
| 529 | m_native_vtx_decl.colors[i].offset = m_dst_ofs; |
| 530 | m_native_vtx_decl.colors[i].type = ComponentFormat::UByte; |
| 531 | m_native_vtx_decl.colors[i].integer = false; |
| 532 | m_dst_ofs += 4; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | for (u8 i = 0; i < m_VtxDesc.high.TexCoord.Size(); i++) |
| 537 | { |
| 538 | int elements = m_VtxAttr.GetTexElements(i) == TexComponentCount::ST ? 2 : 1; |
| 539 | if (m_VtxDesc.high.TexCoord[i] != VertexComponentFormat::NotPresent) |
| 540 | { |
| 541 | data = GetVertexAddr(CPArray::TexCoord0 + i, m_VtxDesc.high.TexCoord[i]); |
| 542 | u8 scaling_exponent = m_VtxAttr.GetTexFrac(i); |
| 543 | ReadVertex(data, m_VtxDesc.high.TexCoord[i], m_VtxAttr.GetTexFormat(i), elements, |
| 544 | m_VtxDesc.low.TexMatIdx[i] ? 2 : elements, m_VtxAttr.g0.ByteDequant, |
| 545 | scaling_exponent, &m_native_vtx_decl.texcoords[i]); |
| 546 | } |
| 547 | if (m_VtxDesc.low.TexMatIdx[i]) |
| 548 | { |
| 549 | m_native_vtx_decl.texcoords[i].components = 3; |
| 550 | m_native_vtx_decl.texcoords[i].enable = true; |
| 551 | m_native_vtx_decl.texcoords[i].type = ComponentFormat::Float; |
| 552 | m_native_vtx_decl.texcoords[i].integer = false; |
| 553 | MOVZX(64, 8, scratch1, MDisp(src_reg, texmatidx_ofs[i])); |
| 554 | if (m_VtxDesc.high.TexCoord[i] != VertexComponentFormat::NotPresent) |
| 555 | { |
| 556 | CVTSI2SS(XMM0, R(scratch1)); |
| 557 | MOVSS(MDisp(dst_reg, m_dst_ofs), XMM0); |
| 558 | m_dst_ofs += sizeof(float); |
| 559 | } |
| 560 | else |
| 561 | { |
| 562 | m_native_vtx_decl.texcoords[i].offset = m_dst_ofs; |
| 563 | PXOR(XMM0, R(XMM0)); |
| 564 | CVTSI2SS(XMM0, R(scratch1)); |
| 565 | SHUFPS(XMM0, R(XMM0), 0x45); // 000X -> 0X00 |
| 566 | MOVUPS(MDisp(dst_reg, m_dst_ofs), XMM0); |
| 567 | m_dst_ofs += sizeof(float) * 3; |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | // Prepare for the next vertex. |
| 573 | ADD(64, R(dst_reg), Imm32(m_dst_ofs)); |
| 574 | const u8* cont = GetCodePtr(); |
| 575 | ADD(64, R(src_reg), Imm32(m_src_ofs)); |
| 576 | |
| 577 | SUB(32, R(remaining_reg), Imm8(1)); |
| 578 | J_CC(CC_AE, loop_start); |
| 579 | |
| 580 | // Get the original count. |
| 581 | POP(32, R(ABI_RETURN)); |
| 582 | |
| 583 | ABI_PopRegistersAndAdjustStack(regs, 0); |
| 584 | |
| 585 | if (IsIndexed(m_VtxDesc.low.Position)) |
| 586 | { |
| 587 | SUB(32, R(ABI_RETURN), R(skipped_reg)); |
| 588 | RET(); |
| 589 | |
| 590 | SetJumpTarget(m_skip_vertex); |
| 591 | ADD(32, R(skipped_reg), Imm8(1)); |
| 592 | JMP(cont); |
| 593 | } |
| 594 | else |
| 595 | { |
| 596 | RET(); |
| 597 | } |
| 598 | |
| 599 | ASSERT_MSG(VIDEO, m_vertex_size == m_src_ofs, |
| 600 | "Vertex size from vertex loader ({}) does not match expected vertex size ({})!\nVtx " |
| 601 | "desc: {:08x} {:08x}\nVtx attr: {:08x} {:08x} {:08x}", |
| 602 | m_src_ofs, m_vertex_size, m_VtxDesc.low.Hex, m_VtxDesc.high.Hex, m_VtxAttr.g0.Hex, |
| 603 | m_VtxAttr.g1.Hex, m_VtxAttr.g2.Hex); |
| 604 | m_native_vtx_decl.stride = m_dst_ofs; |
| 605 | } |
| 606 | |
| 607 | int VertexLoaderX64::RunVertices(const u8* src, u8* dst, int count) |
| 608 | { |
| 609 | m_numLoadedVertices += count; |
| 610 | return ((int (*)(const u8* src, u8* dst, int count, const void* base))region)(src, dst, count, |
| 611 | memory_base_ptr); |
| 612 | } |