| 1 | // Copyright 2015 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/VertexLoaderARM64.h" |
| 5 | |
| 6 | #include <array> |
| 7 | |
| 8 | #include "Common/CommonTypes.h" |
| 9 | #include "VideoCommon/CPMemory.h" |
| 10 | #include "VideoCommon/VertexLoaderManager.h" |
| 11 | |
| 12 | using namespace Arm64Gen; |
| 13 | |
| 14 | constexpr ARM64Reg src_reg = ARM64Reg::X0; |
| 15 | constexpr ARM64Reg dst_reg = ARM64Reg::X1; |
| 16 | constexpr ARM64Reg remaining_reg = ARM64Reg::W2; |
| 17 | constexpr ARM64Reg skipped_reg = ARM64Reg::W17; |
| 18 | constexpr ARM64Reg scratch1_reg = ARM64Reg::W16; |
| 19 | constexpr ARM64Reg scratch2_reg = ARM64Reg::W15; |
| 20 | constexpr ARM64Reg scratch3_reg = ARM64Reg::W14; |
| 21 | constexpr ARM64Reg saved_count = ARM64Reg::W12; |
| 22 | |
| 23 | constexpr ARM64Reg stride_reg = ARM64Reg::X11; |
| 24 | constexpr ARM64Reg arraybase_reg = ARM64Reg::X10; |
| 25 | constexpr ARM64Reg scale_reg = ARM64Reg::X9; |
| 26 | |
| 27 | static constexpr int GetLoadSize(int load_bytes) |
| 28 | { |
| 29 | if (load_bytes == 1) |
| 30 | return 1; |
| 31 | else if (load_bytes <= 2) |
| 32 | return 2; |
| 33 | else if (load_bytes <= 4) |
| 34 | return 4; |
| 35 | else if (load_bytes <= 8) |
| 36 | return 8; |
| 37 | else |
| 38 | return 16; |
| 39 | } |
| 40 | |
| 41 | alignas(16) static const float scale_factors[] = { |
| 42 | 1.0 / (1ULL << 0), 1.0 / (1ULL << 1), 1.0 / (1ULL << 2), 1.0 / (1ULL << 3), |
| 43 | 1.0 / (1ULL << 4), 1.0 / (1ULL << 5), 1.0 / (1ULL << 6), 1.0 / (1ULL << 7), |
| 44 | 1.0 / (1ULL << 8), 1.0 / (1ULL << 9), 1.0 / (1ULL << 10), 1.0 / (1ULL << 11), |
| 45 | 1.0 / (1ULL << 12), 1.0 / (1ULL << 13), 1.0 / (1ULL << 14), 1.0 / (1ULL << 15), |
| 46 | 1.0 / (1ULL << 16), 1.0 / (1ULL << 17), 1.0 / (1ULL << 18), 1.0 / (1ULL << 19), |
| 47 | 1.0 / (1ULL << 20), 1.0 / (1ULL << 21), 1.0 / (1ULL << 22), 1.0 / (1ULL << 23), |
| 48 | 1.0 / (1ULL << 24), 1.0 / (1ULL << 25), 1.0 / (1ULL << 26), 1.0 / (1ULL << 27), |
| 49 | 1.0 / (1ULL << 28), 1.0 / (1ULL << 29), 1.0 / (1ULL << 30), 1.0 / (1ULL << 31), |
| 50 | }; |
| 51 | |
| 52 | VertexLoaderARM64::VertexLoaderARM64(const TVtxDesc& vtx_desc, const VAT& vtx_att) |
| 53 | : VertexLoaderBase(vtx_desc, vtx_att), m_float_emit(this) |
| 54 | { |
| 55 | AllocCodeSpace(4096); |
| 56 | const Common::ScopedJITPageWriteAndNoExecute enable_jit_page_writes; |
| 57 | ClearCodeSpace(); |
| 58 | GenerateVertexLoader(); |
| 59 | WriteProtect(true); |
| 60 | } |
| 61 | |
| 62 | // Returns the register to use as the base and an offset from that register. |
| 63 | // For indexed attributes, the index is read into scratch1_reg, and then scratch1_reg with no offset |
| 64 | // is returned. For direct attributes, an offset from src_reg is returned. |
| 65 | std::pair<Arm64Gen::ARM64Reg, u32> VertexLoaderARM64::GetVertexAddr(CPArray array, |
| 66 | VertexComponentFormat attribute) |
| 67 | { |
| 68 | if (IsIndexed(attribute)) |
| 69 | { |
| 70 | if (attribute == VertexComponentFormat::Index8) |
| 71 | { |
| 72 | LDURB(scratch1_reg, src_reg, m_src_ofs); |
| 73 | m_src_ofs += 1; |
| 74 | } |
| 75 | else // Index16 |
| 76 | { |
| 77 | LDURH(scratch1_reg, src_reg, m_src_ofs); |
| 78 | m_src_ofs += 2; |
| 79 | REV16(scratch1_reg, scratch1_reg); |
| 80 | } |
| 81 | |
| 82 | if (array == CPArray::Position) |
| 83 | { |
| 84 | EOR(scratch2_reg, scratch1_reg, |
| 85 | attribute == VertexComponentFormat::Index8 ? LogicalImm(0xFF, GPRSize::B32) : |
| 86 | LogicalImm(0xFFFF, GPRSize::B32)); |
| 87 | m_skip_vertex = CBZ(scratch2_reg); |
| 88 | } |
| 89 | |
| 90 | LDR(IndexType::Unsigned, scratch2_reg, stride_reg, static_cast<u8>(array) * 4); |
| 91 | MUL(scratch1_reg, scratch1_reg, scratch2_reg); |
| 92 | |
| 93 | LDR(IndexType::Unsigned, EncodeRegTo64(scratch2_reg), arraybase_reg, |
| 94 | static_cast<u8>(array) * 8); |
| 95 | ADD(EncodeRegTo64(scratch1_reg), EncodeRegTo64(scratch1_reg), EncodeRegTo64(scratch2_reg)); |
| 96 | return {EncodeRegTo64(scratch1_reg), 0}; |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | return {src_reg, m_src_ofs}; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void VertexLoaderARM64::ReadVertex(VertexComponentFormat attribute, ComponentFormat format, |
| 105 | int count_in, int count_out, bool dequantize, |
| 106 | u8 scaling_exponent, AttributeFormat* native_format, |
| 107 | ARM64Reg reg, u32 offset) |
| 108 | { |
| 109 | ARM64Reg coords = count_in == 3 ? ARM64Reg::Q31 : ARM64Reg::D31; |
| 110 | ARM64Reg scale = count_in == 3 ? ARM64Reg::Q30 : ARM64Reg::D30; |
| 111 | |
| 112 | int elem_size = GetElementSize(format); |
| 113 | int load_bytes = elem_size * count_in; |
| 114 | int load_size = GetLoadSize(load_bytes); |
| 115 | load_size <<= 3; |
| 116 | |
| 117 | m_float_emit.LDUR(load_size, coords, reg, offset); |
| 118 | |
| 119 | if (format < ComponentFormat::Float) |
| 120 | { |
| 121 | // Extend and convert to float |
| 122 | switch (format) |
| 123 | { |
| 124 | case ComponentFormat::UByte: |
| 125 | m_float_emit.UXTL(8, EncodeRegToDouble(coords), EncodeRegToDouble(coords)); |
| 126 | m_float_emit.UXTL(16, EncodeRegToDouble(coords), EncodeRegToDouble(coords)); |
| 127 | break; |
| 128 | case ComponentFormat::Byte: |
| 129 | m_float_emit.SXTL(8, EncodeRegToDouble(coords), EncodeRegToDouble(coords)); |
| 130 | m_float_emit.SXTL(16, EncodeRegToDouble(coords), EncodeRegToDouble(coords)); |
| 131 | break; |
| 132 | case ComponentFormat::UShort: |
| 133 | m_float_emit.REV16(8, EncodeRegToDouble(coords), EncodeRegToDouble(coords)); |
| 134 | m_float_emit.UXTL(16, EncodeRegToDouble(coords), EncodeRegToDouble(coords)); |
| 135 | break; |
| 136 | case ComponentFormat::Short: |
| 137 | m_float_emit.REV16(8, EncodeRegToDouble(coords), EncodeRegToDouble(coords)); |
| 138 | m_float_emit.SXTL(16, EncodeRegToDouble(coords), EncodeRegToDouble(coords)); |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | m_float_emit.SCVTF(32, coords, coords); |
| 143 | |
| 144 | if (dequantize && scaling_exponent) |
| 145 | { |
| 146 | m_float_emit.LDR(32, IndexType::Unsigned, scale, scale_reg, scaling_exponent * 4); |
| 147 | m_float_emit.FMUL(32, coords, coords, scale, 0); |
| 148 | } |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | m_float_emit.REV32(8, coords, coords); |
| 153 | } |
| 154 | |
| 155 | const u32 write_size = count_out == 3 ? 128 : count_out * 32; |
| 156 | m_float_emit.STUR(write_size, coords, dst_reg, m_dst_ofs); |
| 157 | |
| 158 | // Z-Freeze |
| 159 | if (native_format == &m_native_vtx_decl.position) |
| 160 | { |
| 161 | CMP(remaining_reg, 3); |
| 162 | FixupBranch dont_store = B(CC_GE); |
| 163 | MOVP2R(EncodeRegTo64(scratch2_reg), VertexLoaderManager::position_cache.data()); |
| 164 | m_float_emit.STR(128, coords, EncodeRegTo64(scratch2_reg), ArithOption(remaining_reg, true)); |
| 165 | SetJumpTarget(dont_store); |
| 166 | } |
| 167 | else if (native_format == &m_native_vtx_decl.normals[0]) |
| 168 | { |
| 169 | FixupBranch dont_store = CBNZ(remaining_reg); |
| 170 | MOVP2R(EncodeRegTo64(scratch2_reg), VertexLoaderManager::normal_cache.data()); |
| 171 | m_float_emit.STR(128, IndexType::Unsigned, coords, EncodeRegTo64(scratch2_reg), 0); |
| 172 | SetJumpTarget(dont_store); |
| 173 | } |
| 174 | else if (native_format == &m_native_vtx_decl.normals[1]) |
| 175 | { |
| 176 | FixupBranch dont_store = CBNZ(remaining_reg); |
| 177 | MOVP2R(EncodeRegTo64(scratch2_reg), VertexLoaderManager::tangent_cache.data()); |
| 178 | m_float_emit.STR(128, IndexType::Unsigned, coords, EncodeRegTo64(scratch2_reg), 0); |
| 179 | SetJumpTarget(dont_store); |
| 180 | } |
| 181 | else if (native_format == &m_native_vtx_decl.normals[2]) |
| 182 | { |
| 183 | FixupBranch dont_store = CBNZ(remaining_reg); |
| 184 | MOVP2R(EncodeRegTo64(scratch2_reg), VertexLoaderManager::binormal_cache.data()); |
| 185 | m_float_emit.STR(128, IndexType::Unsigned, coords, EncodeRegTo64(scratch2_reg), 0); |
| 186 | SetJumpTarget(dont_store); |
| 187 | } |
| 188 | |
| 189 | native_format->components = count_out; |
| 190 | native_format->enable = true; |
| 191 | native_format->offset = m_dst_ofs; |
| 192 | native_format->type = ComponentFormat::Float; |
| 193 | native_format->integer = false; |
| 194 | m_dst_ofs += sizeof(float) * count_out; |
| 195 | |
| 196 | if (attribute == VertexComponentFormat::Direct) |
| 197 | m_src_ofs += load_bytes; |
| 198 | } |
| 199 | |
| 200 | void VertexLoaderARM64::ReadColor(VertexComponentFormat attribute, ColorFormat format, ARM64Reg reg, |
| 201 | u32 offset) |
| 202 | { |
| 203 | int load_bytes = 0; |
| 204 | switch (format) |
| 205 | { |
| 206 | case ColorFormat::RGB888: |
| 207 | case ColorFormat::RGB888x: |
| 208 | case ColorFormat::RGBA8888: |
| 209 | LDUR(scratch2_reg, reg, offset); |
| 210 | |
| 211 | if (format != ColorFormat::RGBA8888) |
| 212 | ORR(scratch2_reg, scratch2_reg, LogicalImm(0xFF000000, GPRSize::B32)); |
| 213 | STR(IndexType::Unsigned, scratch2_reg, dst_reg, m_dst_ofs); |
| 214 | load_bytes = format == ColorFormat::RGB888 ? 3 : 4; |
| 215 | break; |
| 216 | |
| 217 | case ColorFormat::RGB565: |
| 218 | // RRRRRGGG GGGBBBBB |
| 219 | // AAAAAAAA BBBBBBBB GGGGGGGG RRRRRRRR |
| 220 | LDURH(scratch3_reg, reg, offset); |
| 221 | |
| 222 | REV16(scratch3_reg, scratch3_reg); |
| 223 | |
| 224 | // B |
| 225 | AND(scratch2_reg, scratch3_reg, LogicalImm(0x1F, GPRSize::B32)); |
| 226 | ORR(scratch2_reg, ARM64Reg::WSP, scratch2_reg, ArithOption(scratch2_reg, ShiftType::LSL, 3)); |
| 227 | ORR(scratch2_reg, scratch2_reg, scratch2_reg, ArithOption(scratch2_reg, ShiftType::LSR, 5)); |
| 228 | ORR(scratch1_reg, ARM64Reg::WSP, scratch2_reg, ArithOption(scratch2_reg, ShiftType::LSL, 16)); |
| 229 | |
| 230 | // G |
| 231 | UBFM(scratch2_reg, scratch3_reg, 5, 10); |
| 232 | ORR(scratch2_reg, ARM64Reg::WSP, scratch2_reg, ArithOption(scratch2_reg, ShiftType::LSL, 2)); |
| 233 | ORR(scratch2_reg, scratch2_reg, scratch2_reg, ArithOption(scratch2_reg, ShiftType::LSR, 6)); |
| 234 | ORR(scratch1_reg, scratch1_reg, scratch2_reg, ArithOption(scratch2_reg, ShiftType::LSL, 8)); |
| 235 | |
| 236 | // R |
| 237 | UBFM(scratch2_reg, scratch3_reg, 11, 15); |
| 238 | ORR(scratch1_reg, scratch1_reg, scratch2_reg, ArithOption(scratch2_reg, ShiftType::LSL, 3)); |
| 239 | ORR(scratch1_reg, scratch1_reg, scratch2_reg, ArithOption(scratch2_reg, ShiftType::LSR, 2)); |
| 240 | |
| 241 | // A |
| 242 | ORR(scratch1_reg, scratch1_reg, LogicalImm(0xFF000000, GPRSize::B32)); |
| 243 | |
| 244 | STR(IndexType::Unsigned, scratch1_reg, dst_reg, m_dst_ofs); |
| 245 | load_bytes = 2; |
| 246 | break; |
| 247 | |
| 248 | case ColorFormat::RGBA4444: |
| 249 | // BBBBAAAA RRRRGGGG |
| 250 | // REV16 - RRRRGGGG BBBBAAAA |
| 251 | // AAAAAAAA BBBBBBBB GGGGGGGG RRRRRRRR |
| 252 | LDURH(scratch3_reg, reg, offset); |
| 253 | |
| 254 | // R |
| 255 | UBFM(scratch1_reg, scratch3_reg, 4, 7); |
| 256 | |
| 257 | // G |
| 258 | AND(scratch2_reg, scratch3_reg, LogicalImm(0xF, GPRSize::B32)); |
| 259 | ORR(scratch1_reg, scratch1_reg, scratch2_reg, ArithOption(scratch2_reg, ShiftType::LSL, 8)); |
| 260 | |
| 261 | // B |
| 262 | UBFM(scratch2_reg, scratch3_reg, 12, 15); |
| 263 | ORR(scratch1_reg, scratch1_reg, scratch2_reg, ArithOption(scratch2_reg, ShiftType::LSL, 16)); |
| 264 | |
| 265 | // A |
| 266 | UBFM(scratch2_reg, scratch3_reg, 8, 11); |
| 267 | ORR(scratch1_reg, scratch1_reg, scratch2_reg, ArithOption(scratch2_reg, ShiftType::LSL, 24)); |
| 268 | |
| 269 | // Final duplication |
| 270 | ORR(scratch1_reg, scratch1_reg, scratch1_reg, ArithOption(scratch1_reg, ShiftType::LSL, 4)); |
| 271 | |
| 272 | STR(IndexType::Unsigned, scratch1_reg, dst_reg, m_dst_ofs); |
| 273 | load_bytes = 2; |
| 274 | break; |
| 275 | |
| 276 | case ColorFormat::RGBA6666: |
| 277 | // RRRRRRGG GGGGBBBB BBAAAAAA |
| 278 | // AAAAAAAA BBBBBBBB GGGGGGGG RRRRRRRR |
| 279 | LDUR(scratch3_reg, reg, offset - 1); |
| 280 | |
| 281 | REV32(scratch3_reg, scratch3_reg); |
| 282 | |
| 283 | // A |
| 284 | UBFM(scratch2_reg, scratch3_reg, 0, 5); |
| 285 | ORR(scratch2_reg, ARM64Reg::WSP, scratch2_reg, ArithOption(scratch2_reg, ShiftType::LSL, 2)); |
| 286 | ORR(scratch2_reg, scratch2_reg, scratch2_reg, ArithOption(scratch2_reg, ShiftType::LSR, 6)); |
| 287 | ORR(scratch1_reg, ARM64Reg::WSP, scratch2_reg, ArithOption(scratch2_reg, ShiftType::LSL, 24)); |
| 288 | |
| 289 | // B |
| 290 | UBFM(scratch2_reg, scratch3_reg, 6, 11); |
| 291 | ORR(scratch2_reg, ARM64Reg::WSP, scratch2_reg, ArithOption(scratch2_reg, ShiftType::LSL, 2)); |
| 292 | ORR(scratch2_reg, scratch2_reg, scratch2_reg, ArithOption(scratch2_reg, ShiftType::LSR, 6)); |
| 293 | ORR(scratch1_reg, scratch1_reg, scratch2_reg, ArithOption(scratch2_reg, ShiftType::LSL, 16)); |
| 294 | |
| 295 | // G |
| 296 | UBFM(scratch2_reg, scratch3_reg, 12, 17); |
| 297 | ORR(scratch2_reg, ARM64Reg::WSP, scratch2_reg, ArithOption(scratch2_reg, ShiftType::LSL, 2)); |
| 298 | ORR(scratch2_reg, scratch2_reg, scratch2_reg, ArithOption(scratch2_reg, ShiftType::LSR, 6)); |
| 299 | ORR(scratch1_reg, scratch1_reg, scratch2_reg, ArithOption(scratch2_reg, ShiftType::LSL, 8)); |
| 300 | |
| 301 | // R |
| 302 | UBFM(scratch2_reg, scratch3_reg, 18, 23); |
| 303 | ORR(scratch1_reg, scratch1_reg, scratch2_reg, ArithOption(scratch2_reg, ShiftType::LSL, 2)); |
| 304 | ORR(scratch1_reg, scratch1_reg, scratch2_reg, ArithOption(scratch2_reg, ShiftType::LSR, 4)); |
| 305 | |
| 306 | STR(IndexType::Unsigned, scratch1_reg, dst_reg, m_dst_ofs); |
| 307 | |
| 308 | load_bytes = 3; |
| 309 | break; |
| 310 | } |
| 311 | if (attribute == VertexComponentFormat::Direct) |
| 312 | m_src_ofs += load_bytes; |
| 313 | } |
| 314 | |
| 315 | void VertexLoaderARM64::GenerateVertexLoader() |
| 316 | { |
| 317 | // The largest input vertex (with the position matrix index and all texture matrix indices |
| 318 | // enabled, and all components set as direct) is 129 bytes (corresponding to a 156-byte |
| 319 | // output). This is small enough that we can always use the unscaled load/store instructions |
| 320 | // (which allow an offset from -256 to +255). |
| 321 | ASSERT(m_vertex_size <= 255); |
| 322 | |
| 323 | // R0 - Source pointer |
| 324 | // R1 - Destination pointer |
| 325 | // R2 - Count |
| 326 | // R30 - LR |
| 327 | // |
| 328 | // R0 return how many |
| 329 | // |
| 330 | // Registers we don't have to worry about saving |
| 331 | // R9-R17 are caller saved temporaries |
| 332 | // R18 is a temporary or platform specific register(iOS) |
| 333 | // |
| 334 | // VFP registers |
| 335 | // We can touch all except v8-v15 |
| 336 | // If we need to use those, we need to retain the lower 64bits(!) of the register |
| 337 | |
| 338 | bool has_tc = false; |
| 339 | bool has_tc_scale = false; |
| 340 | for (size_t i = 0; i < m_VtxDesc.high.TexCoord.Size(); i++) |
| 341 | { |
| 342 | has_tc |= m_VtxDesc.high.TexCoord[i] != VertexComponentFormat::NotPresent; |
| 343 | has_tc_scale |= (m_VtxAttr.GetTexFrac(i) != 0); |
| 344 | } |
| 345 | |
| 346 | bool need_scale = (m_VtxAttr.g0.ByteDequant && m_VtxAttr.g0.PosFrac) || |
| 347 | (has_tc && has_tc_scale) || |
| 348 | (m_VtxDesc.low.Normal != VertexComponentFormat::NotPresent); |
| 349 | |
| 350 | AlignCode16(); |
| 351 | if (IsIndexed(m_VtxDesc.low.Position)) |
| 352 | MOV(skipped_reg, ARM64Reg::WZR); |
| 353 | ADD(saved_count, remaining_reg, 1); |
| 354 | |
| 355 | MOVP2R(stride_reg, g_main_cp_state.array_strides.data()); |
| 356 | MOVP2R(arraybase_reg, VertexLoaderManager::cached_arraybases.data()); |
| 357 | |
| 358 | if (need_scale) |
| 359 | MOVP2R(scale_reg, scale_factors); |
| 360 | |
| 361 | const u8* loop_start = GetCodePtr(); |
| 362 | |
| 363 | if (m_VtxDesc.low.PosMatIdx) |
| 364 | { |
| 365 | LDRB(IndexType::Unsigned, scratch1_reg, src_reg, m_src_ofs); |
| 366 | AND(scratch1_reg, scratch1_reg, LogicalImm(0x3F, GPRSize::B32)); |
| 367 | STR(IndexType::Unsigned, scratch1_reg, dst_reg, m_dst_ofs); |
| 368 | |
| 369 | // Z-Freeze |
| 370 | CMP(remaining_reg, 3); |
| 371 | FixupBranch dont_store = B(CC_GE); |
| 372 | MOVP2R(EncodeRegTo64(scratch2_reg), VertexLoaderManager::position_matrix_index_cache.data()); |
| 373 | STR(scratch1_reg, EncodeRegTo64(scratch2_reg), ArithOption(remaining_reg, true)); |
| 374 | SetJumpTarget(dont_store); |
| 375 | |
| 376 | m_native_vtx_decl.posmtx.components = 4; |
| 377 | m_native_vtx_decl.posmtx.enable = true; |
| 378 | m_native_vtx_decl.posmtx.offset = m_dst_ofs; |
| 379 | m_native_vtx_decl.posmtx.type = ComponentFormat::UByte; |
| 380 | m_native_vtx_decl.posmtx.integer = true; |
| 381 | m_src_ofs += sizeof(u8); |
| 382 | m_dst_ofs += sizeof(u32); |
| 383 | } |
| 384 | |
| 385 | std::array<u32, 8> texmatidx_ofs; |
| 386 | for (size_t i = 0; i < m_VtxDesc.low.TexMatIdx.Size(); i++) |
| 387 | { |
| 388 | if (m_VtxDesc.low.TexMatIdx[i]) |
| 389 | texmatidx_ofs[i] = m_src_ofs++; |
| 390 | } |
| 391 | |
| 392 | // Position |
| 393 | { |
| 394 | const int pos_elements = m_VtxAttr.g0.PosElements == CoordComponentCount::XY ? 2 : 3; |
| 395 | |
| 396 | const auto [reg, offset] = GetVertexAddr(CPArray::Position, m_VtxDesc.low.Position); |
| 397 | ReadVertex(m_VtxDesc.low.Position, m_VtxAttr.g0.PosFormat, pos_elements, pos_elements, |
| 398 | m_VtxAttr.g0.ByteDequant, m_VtxAttr.g0.PosFrac, &m_native_vtx_decl.position, reg, |
| 399 | offset); |
| 400 | } |
| 401 | |
| 402 | if (m_VtxDesc.low.Normal != VertexComponentFormat::NotPresent) |
| 403 | { |
| 404 | static constexpr Common::EnumMap<u8, ComponentFormat::InvalidFloat7> SCALE_MAP = {7, 6, 15, 14, |
| 405 | 0, 0, 0, 0}; |
| 406 | const u8 scaling_exponent = SCALE_MAP[m_VtxAttr.g0.NormalFormat]; |
| 407 | |
| 408 | // Normal |
| 409 | auto [reg, offset] = GetVertexAddr(CPArray::Normal, m_VtxDesc.low.Normal); |
| 410 | ReadVertex(m_VtxDesc.low.Normal, m_VtxAttr.g0.NormalFormat, 3, 3, true, scaling_exponent, |
| 411 | &m_native_vtx_decl.normals[0], reg, offset); |
| 412 | |
| 413 | if (m_VtxAttr.g0.NormalElements == NormalComponentCount::NTB) |
| 414 | { |
| 415 | const bool index3 = IsIndexed(m_VtxDesc.low.Normal) && m_VtxAttr.g0.NormalIndex3; |
| 416 | const int elem_size = GetElementSize(m_VtxAttr.g0.NormalFormat); |
| 417 | const int load_bytes = elem_size * 3; |
| 418 | |
| 419 | // Tangent |
| 420 | // If in Index3 mode, and indexed components are used, replace the index with a new index. |
| 421 | if (index3) |
| 422 | std::tie(reg, offset) = GetVertexAddr(CPArray::Normal, m_VtxDesc.low.Normal); |
| 423 | // The tangent comes after the normal; even in index3 mode, an extra offset of load_bytes is |
| 424 | // applied. Note that this is different from adding 1 to the index, as the stride for indices |
| 425 | // may be different from the size of the tangent itself. |
| 426 | ReadVertex(m_VtxDesc.low.Normal, m_VtxAttr.g0.NormalFormat, 3, 3, true, scaling_exponent, |
| 427 | &m_native_vtx_decl.normals[1], reg, offset + load_bytes); |
| 428 | // Binormal |
| 429 | if (index3) |
| 430 | std::tie(reg, offset) = GetVertexAddr(CPArray::Normal, m_VtxDesc.low.Normal); |
| 431 | ReadVertex(m_VtxDesc.low.Normal, m_VtxAttr.g0.NormalFormat, 3, 3, true, scaling_exponent, |
| 432 | &m_native_vtx_decl.normals[2], reg, offset + load_bytes * 2); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | for (u8 i = 0; i < m_VtxDesc.low.Color.Size(); i++) |
| 437 | { |
| 438 | m_native_vtx_decl.colors[i].components = 4; |
| 439 | m_native_vtx_decl.colors[i].type = ComponentFormat::UByte; |
| 440 | m_native_vtx_decl.colors[i].integer = false; |
| 441 | |
| 442 | if (m_VtxDesc.low.Color[i] != VertexComponentFormat::NotPresent) |
| 443 | { |
| 444 | const auto [reg, offset] = GetVertexAddr(CPArray::Color0 + i, m_VtxDesc.low.Color[i]); |
| 445 | ReadColor(m_VtxDesc.low.Color[i], m_VtxAttr.GetColorFormat(i), reg, offset); |
| 446 | m_native_vtx_decl.colors[i].components = 4; |
| 447 | m_native_vtx_decl.colors[i].enable = true; |
| 448 | m_native_vtx_decl.colors[i].offset = m_dst_ofs; |
| 449 | m_native_vtx_decl.colors[i].type = ComponentFormat::UByte; |
| 450 | m_native_vtx_decl.colors[i].integer = false; |
| 451 | m_dst_ofs += 4; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | for (u8 i = 0; i < m_VtxDesc.high.TexCoord.Size(); i++) |
| 456 | { |
| 457 | m_native_vtx_decl.texcoords[i].offset = m_dst_ofs; |
| 458 | m_native_vtx_decl.texcoords[i].type = ComponentFormat::Float; |
| 459 | m_native_vtx_decl.texcoords[i].integer = false; |
| 460 | |
| 461 | const int elements = m_VtxAttr.GetTexElements(i) == TexComponentCount::S ? 1 : 2; |
| 462 | if (m_VtxDesc.high.TexCoord[i] != VertexComponentFormat::NotPresent) |
| 463 | { |
| 464 | const auto [reg, offset] = GetVertexAddr(CPArray::TexCoord0 + i, m_VtxDesc.high.TexCoord[i]); |
| 465 | u8 scaling_exponent = m_VtxAttr.GetTexFrac(i); |
| 466 | ReadVertex(m_VtxDesc.high.TexCoord[i], m_VtxAttr.GetTexFormat(i), elements, |
| 467 | m_VtxDesc.low.TexMatIdx[i] ? 2 : elements, m_VtxAttr.g0.ByteDequant, |
| 468 | scaling_exponent, &m_native_vtx_decl.texcoords[i], reg, offset); |
| 469 | } |
| 470 | if (m_VtxDesc.low.TexMatIdx[i]) |
| 471 | { |
| 472 | m_native_vtx_decl.texcoords[i].components = 3; |
| 473 | m_native_vtx_decl.texcoords[i].enable = true; |
| 474 | m_native_vtx_decl.texcoords[i].type = ComponentFormat::Float; |
| 475 | m_native_vtx_decl.texcoords[i].integer = false; |
| 476 | |
| 477 | LDRB(IndexType::Unsigned, scratch2_reg, src_reg, texmatidx_ofs[i]); |
| 478 | m_float_emit.UCVTF(ARM64Reg::S31, scratch2_reg); |
| 479 | |
| 480 | if (m_VtxDesc.high.TexCoord[i] != VertexComponentFormat::NotPresent) |
| 481 | { |
| 482 | m_float_emit.STR(32, IndexType::Unsigned, ARM64Reg::D31, dst_reg, m_dst_ofs); |
| 483 | m_dst_ofs += sizeof(float); |
| 484 | } |
| 485 | else |
| 486 | { |
| 487 | m_native_vtx_decl.texcoords[i].offset = m_dst_ofs; |
| 488 | |
| 489 | STUR(ARM64Reg::SP, dst_reg, m_dst_ofs); |
| 490 | m_float_emit.STR(32, IndexType::Unsigned, ARM64Reg::D31, dst_reg, m_dst_ofs + 8); |
| 491 | |
| 492 | m_dst_ofs += sizeof(float) * 3; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | // Prepare for the next vertex. |
| 498 | ADD(dst_reg, dst_reg, m_dst_ofs); |
| 499 | const u8* cont = GetCodePtr(); |
| 500 | ADD(src_reg, src_reg, m_src_ofs); |
| 501 | |
| 502 | SUBS(remaining_reg, remaining_reg, 1); |
| 503 | B(CCFlags::CC_GE, loop_start); |
| 504 | |
| 505 | if (IsIndexed(m_VtxDesc.low.Position)) |
| 506 | { |
| 507 | SUB(ARM64Reg::W0, saved_count, skipped_reg); |
| 508 | RET(ARM64Reg::X30); |
| 509 | |
| 510 | SetJumpTarget(m_skip_vertex); |
| 511 | ADD(skipped_reg, skipped_reg, 1); |
| 512 | B(cont); |
| 513 | } |
| 514 | else |
| 515 | { |
| 516 | MOV(ARM64Reg::W0, saved_count); |
| 517 | RET(ARM64Reg::X30); |
| 518 | } |
| 519 | |
| 520 | FlushIcache(); |
| 521 | |
| 522 | ASSERT_MSG(VIDEO, m_vertex_size == m_src_ofs, |
| 523 | "Vertex size from vertex loader ({}) does not match expected vertex size ({})!\nVtx " |
| 524 | "desc: {:08x} {:08x}\nVtx attr: {:08x} {:08x} {:08x}", |
| 525 | m_src_ofs, m_vertex_size, m_VtxDesc.low.Hex, m_VtxDesc.high.Hex, m_VtxAttr.g0.Hex, |
| 526 | m_VtxAttr.g1.Hex, m_VtxAttr.g2.Hex); |
| 527 | m_native_vtx_decl.stride = m_dst_ofs; |
| 528 | } |
| 529 | |
| 530 | int VertexLoaderARM64::RunVertices(const u8* src, u8* dst, int count) |
| 531 | { |
| 532 | m_numLoadedVertices += count; |
| 533 | return ((int (*)(const u8* src, u8* dst, int count))region)(src, dst, count - 1); |
| 534 | } |