2014-12-13 00:51:14 +00:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2014-12-13 00:51:14 +00:00
|
|
|
|
2017-03-27 02:12:20 +00:00
|
|
|
#include "VideoCommon/VertexLoaderBase.h"
|
|
|
|
|
|
|
|
#include <array>
|
2022-12-18 15:51:32 +00:00
|
|
|
#include <bit>
|
2016-01-17 21:54:31 +00:00
|
|
|
#include <cstring>
|
2015-12-23 00:59:32 +00:00
|
|
|
#include <memory>
|
2016-01-17 21:54:31 +00:00
|
|
|
#include <string>
|
2014-12-18 22:27:10 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2019-11-22 22:10:41 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2021-07-12 01:24:55 +00:00
|
|
|
#include "Common/Assert.h"
|
2023-01-12 20:54:01 +00:00
|
|
|
#include "Common/BitUtils.h"
|
2016-01-17 21:54:31 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2015-10-16 18:18:27 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/MsgHandler.h"
|
2014-12-13 00:51:14 +00:00
|
|
|
|
|
|
|
#include "VideoCommon/VertexLoader.h"
|
2023-01-12 20:54:01 +00:00
|
|
|
#include "VideoCommon/VertexLoaderManager.h"
|
2021-03-11 20:55:25 +00:00
|
|
|
#include "VideoCommon/VertexLoader_Color.h"
|
|
|
|
#include "VideoCommon/VertexLoader_Normal.h"
|
|
|
|
#include "VideoCommon/VertexLoader_Position.h"
|
|
|
|
#include "VideoCommon/VertexLoader_TextCoord.h"
|
2014-12-13 00:51:14 +00:00
|
|
|
|
2015-01-15 00:46:23 +00:00
|
|
|
#ifdef _M_X86_64
|
|
|
|
#include "VideoCommon/VertexLoaderX64.h"
|
2015-02-13 00:52:07 +00:00
|
|
|
#elif defined(_M_ARM_64)
|
|
|
|
#include "VideoCommon/VertexLoaderARM64.h"
|
2015-01-15 00:46:23 +00:00
|
|
|
#endif
|
|
|
|
|
2014-12-18 22:27:10 +00:00
|
|
|
// a hacky implementation to compare two vertex loaders
|
|
|
|
class VertexLoaderTester : public VertexLoaderBase
|
|
|
|
{
|
|
|
|
public:
|
2015-12-23 00:59:32 +00:00
|
|
|
VertexLoaderTester(std::unique_ptr<VertexLoaderBase> a_, std::unique_ptr<VertexLoaderBase> b_,
|
|
|
|
const TVtxDesc& vtx_desc, const VAT& vtx_attr)
|
|
|
|
: VertexLoaderBase(vtx_desc, vtx_attr), a(std::move(a_)), b(std::move(b_))
|
2014-12-18 22:27:10 +00:00
|
|
|
{
|
2021-03-11 19:23:13 +00:00
|
|
|
ASSERT(a && b);
|
2021-03-11 20:55:25 +00:00
|
|
|
if (a->m_vertex_size == b->m_vertex_size && a->m_native_components == b->m_native_components &&
|
2021-03-11 19:23:13 +00:00
|
|
|
a->m_native_vtx_decl.stride == b->m_native_vtx_decl.stride)
|
|
|
|
{
|
2021-03-11 20:55:25 +00:00
|
|
|
// These are generated from the VAT and vertex desc, so they should match.
|
|
|
|
// m_native_vtx_decl.stride isn't set yet, though.
|
|
|
|
ASSERT(m_vertex_size == a->m_vertex_size && m_native_components == a->m_native_components);
|
|
|
|
|
2021-03-11 19:23:13 +00:00
|
|
|
memcpy(&m_native_vtx_decl, &a->m_native_vtx_decl, sizeof(PortableVertexDeclaration));
|
|
|
|
}
|
|
|
|
else
|
2015-01-11 15:20:44 +00:00
|
|
|
{
|
2021-03-11 20:55:25 +00:00
|
|
|
PanicAlertFmt("Can't compare vertex loaders that expect different vertex formats!\n"
|
|
|
|
"a: m_vertex_size {}, m_native_components {:#010x}, stride {}\n"
|
|
|
|
"b: m_vertex_size {}, m_native_components {:#010x}, stride {}",
|
|
|
|
a->m_vertex_size, a->m_native_components, a->m_native_vtx_decl.stride,
|
|
|
|
b->m_vertex_size, b->m_native_components, b->m_native_vtx_decl.stride);
|
2015-01-11 15:20:44 +00:00
|
|
|
}
|
2014-12-18 22:27:10 +00:00
|
|
|
}
|
2022-11-23 00:54:05 +00:00
|
|
|
int RunVertices(const u8* src, u8* dst, int count) override
|
2014-12-18 22:27:10 +00:00
|
|
|
{
|
2015-01-11 15:20:44 +00:00
|
|
|
buffer_a.resize(count * a->m_native_vtx_decl.stride + 4);
|
|
|
|
buffer_b.resize(count * b->m_native_vtx_decl.stride + 4);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2023-01-12 20:54:01 +00:00
|
|
|
const std::array<u32, 3> old_position_matrix_index_cache =
|
|
|
|
VertexLoaderManager::position_matrix_index_cache;
|
|
|
|
const std::array<std::array<float, 4>, 3> old_position_cache =
|
|
|
|
VertexLoaderManager::position_cache;
|
|
|
|
const std::array<float, 4> old_tangent_cache = VertexLoaderManager::tangent_cache;
|
|
|
|
const std::array<float, 4> old_binormal_cache = VertexLoaderManager::binormal_cache;
|
|
|
|
|
|
|
|
const int count_a = a->RunVertices(src, buffer_a.data(), count);
|
|
|
|
|
|
|
|
const std::array<u32, 3> a_position_matrix_index_cache =
|
|
|
|
VertexLoaderManager::position_matrix_index_cache;
|
|
|
|
const std::array<std::array<float, 4>, 3> a_position_cache =
|
|
|
|
VertexLoaderManager::position_cache;
|
|
|
|
const std::array<float, 4> a_tangent_cache = VertexLoaderManager::tangent_cache;
|
|
|
|
const std::array<float, 4> a_binormal_cache = VertexLoaderManager::binormal_cache;
|
|
|
|
|
|
|
|
// Reset state before running b
|
|
|
|
VertexLoaderManager::position_matrix_index_cache = old_position_matrix_index_cache;
|
|
|
|
VertexLoaderManager::position_cache = old_position_cache;
|
|
|
|
VertexLoaderManager::tangent_cache = old_tangent_cache;
|
|
|
|
VertexLoaderManager::binormal_cache = old_binormal_cache;
|
|
|
|
|
|
|
|
const int count_b = b->RunVertices(src, buffer_b.data(), count);
|
|
|
|
|
|
|
|
const std::array<u32, 3> b_position_matrix_index_cache =
|
|
|
|
VertexLoaderManager::position_matrix_index_cache;
|
|
|
|
const std::array<std::array<float, 4>, 3> b_position_cache =
|
|
|
|
VertexLoaderManager::position_cache;
|
|
|
|
const std::array<float, 4> b_tangent_cache = VertexLoaderManager::tangent_cache;
|
|
|
|
const std::array<float, 4> b_binormal_cache = VertexLoaderManager::binormal_cache;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2023-01-12 20:39:20 +00:00
|
|
|
ASSERT_MSG(VIDEO, count_a == count_b,
|
|
|
|
"The two vertex loaders have loaded a different amount of vertices (a: {}, b: {}).",
|
|
|
|
count_a, count_b);
|
|
|
|
|
|
|
|
ASSERT_MSG(VIDEO,
|
|
|
|
memcmp(buffer_a.data(), buffer_b.data(),
|
|
|
|
std::min(count_a, count_b) * m_native_vtx_decl.stride) == 0,
|
|
|
|
"The two vertex loaders have loaded different data. Configuration:"
|
|
|
|
"\nVertex desc:\n{}\n\nVertex attr:\n{}",
|
|
|
|
m_VtxDesc, m_VtxAttr);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2023-01-12 20:54:01 +00:00
|
|
|
ASSERT_MSG(VIDEO, a_position_matrix_index_cache == b_position_matrix_index_cache,
|
|
|
|
"Expected matching position matrix caches after loading (a: {}; b: {})",
|
|
|
|
fmt::join(a_position_matrix_index_cache, ", "),
|
|
|
|
fmt::join(b_position_matrix_index_cache, ", "));
|
|
|
|
|
|
|
|
// Some games (e.g. Donkey Kong Country Returns) have a few draws that contain NaN.
|
|
|
|
// Since NaN != NaN, we need to compare the bits instead.
|
2023-02-02 23:41:24 +00:00
|
|
|
const auto bit_equal = [](float val_a, float val_b) {
|
2022-08-06 04:10:17 +00:00
|
|
|
return std::bit_cast<u32>(val_a) == std::bit_cast<u32>(val_b);
|
2023-01-12 20:54:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// The last element is allowed to be garbage for SIMD overwrites.
|
|
|
|
// For XY, the last 2 are garbage.
|
|
|
|
const bool positions_match = [&] {
|
|
|
|
const size_t max_component = m_VtxAttr.g0.PosElements == CoordComponentCount::XYZ ? 3 : 2;
|
|
|
|
for (size_t vertex = 0; vertex < 3; vertex++)
|
|
|
|
{
|
|
|
|
if (!std::equal(a_position_cache[vertex].begin(),
|
|
|
|
a_position_cache[vertex].begin() + max_component,
|
|
|
|
b_position_cache[vertex].begin(), bit_equal))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}();
|
|
|
|
|
|
|
|
ASSERT_MSG(VIDEO, positions_match,
|
|
|
|
"Expected matching position caches after loading (a: {} / {} / {}; b: {} / {} / {})",
|
|
|
|
fmt::join(a_position_cache[0], ", "), fmt::join(a_position_cache[1], ", "),
|
|
|
|
fmt::join(a_position_cache[2], ", "), fmt::join(b_position_cache[0], ", "),
|
|
|
|
fmt::join(b_position_cache[1], ", "), fmt::join(b_position_cache[2], ", "));
|
|
|
|
|
|
|
|
// The last element is allowed to be garbage for SIMD overwrites
|
|
|
|
ASSERT_MSG(VIDEO,
|
|
|
|
std::equal(a_tangent_cache.begin(), a_tangent_cache.begin() + 3,
|
|
|
|
b_tangent_cache.begin(), b_tangent_cache.begin() + 3, bit_equal),
|
|
|
|
"Expected matching tangent caches after loading (a: {}; b: {})",
|
|
|
|
fmt::join(a_tangent_cache, ", "), fmt::join(b_tangent_cache, ", "));
|
|
|
|
|
|
|
|
ASSERT_MSG(VIDEO,
|
|
|
|
std::equal(a_binormal_cache.begin(), a_binormal_cache.begin() + 3,
|
|
|
|
b_binormal_cache.begin(), b_binormal_cache.begin() + 3, bit_equal),
|
|
|
|
"Expected matching binormal caches after loading (a: {}; b: {})",
|
|
|
|
fmt::join(a_binormal_cache, ", "), fmt::join(b_binormal_cache, ", "));
|
|
|
|
|
2022-11-23 00:54:05 +00:00
|
|
|
memcpy(dst, buffer_a.data(), count_a * m_native_vtx_decl.stride);
|
2015-01-11 15:20:44 +00:00
|
|
|
m_numLoadedVertices += count;
|
2014-12-18 22:27:10 +00:00
|
|
|
return count_a;
|
|
|
|
}
|
2018-04-12 12:18:04 +00:00
|
|
|
|
2014-12-18 22:27:10 +00:00
|
|
|
private:
|
2015-12-23 00:59:32 +00:00
|
|
|
std::unique_ptr<VertexLoaderBase> a;
|
|
|
|
std::unique_ptr<VertexLoaderBase> b;
|
|
|
|
|
|
|
|
std::vector<u8> buffer_a;
|
|
|
|
std::vector<u8> buffer_b;
|
2014-12-18 22:27:10 +00:00
|
|
|
};
|
|
|
|
|
2022-09-14 18:42:56 +00:00
|
|
|
u32 VertexLoaderBase::GetVertexSize(const TVtxDesc& vtx_desc, const VAT& vtx_attr)
|
2021-03-11 20:55:25 +00:00
|
|
|
{
|
2022-09-14 18:42:56 +00:00
|
|
|
u32 size = 0;
|
|
|
|
|
|
|
|
// Each enabled TexMatIdx adds one byte, as does PosMatIdx
|
2022-12-18 15:51:32 +00:00
|
|
|
size += std::popcount(vtx_desc.low.Hex & 0x1FF);
|
2022-09-14 18:42:56 +00:00
|
|
|
|
2021-03-11 23:43:00 +00:00
|
|
|
const u32 pos_size = VertexLoader_Position::GetSize(vtx_desc.low.Position, vtx_attr.g0.PosFormat,
|
|
|
|
vtx_attr.g0.PosElements);
|
2022-09-14 18:42:56 +00:00
|
|
|
size += pos_size;
|
2021-03-11 23:43:00 +00:00
|
|
|
const u32 norm_size =
|
|
|
|
VertexLoader_Normal::GetSize(vtx_desc.low.Normal, vtx_attr.g0.NormalFormat,
|
|
|
|
vtx_attr.g0.NormalElements, vtx_attr.g0.NormalIndex3);
|
2022-09-14 18:42:56 +00:00
|
|
|
size += norm_size;
|
2021-03-11 20:55:25 +00:00
|
|
|
for (u32 i = 0; i < vtx_desc.low.Color.Size(); i++)
|
|
|
|
{
|
2021-03-11 23:43:00 +00:00
|
|
|
const u32 color_size =
|
|
|
|
VertexLoader_Color::GetSize(vtx_desc.low.Color[i], vtx_attr.GetColorFormat(i));
|
2022-09-14 18:42:56 +00:00
|
|
|
size += color_size;
|
2021-03-11 20:55:25 +00:00
|
|
|
}
|
|
|
|
for (u32 i = 0; i < vtx_desc.high.TexCoord.Size(); i++)
|
|
|
|
{
|
2021-03-11 23:43:00 +00:00
|
|
|
const u32 tc_size = VertexLoader_TextCoord::GetSize(
|
|
|
|
vtx_desc.high.TexCoord[i], vtx_attr.GetTexFormat(i), vtx_attr.GetTexElements(i));
|
2022-09-14 18:42:56 +00:00
|
|
|
size += tc_size;
|
2021-03-11 20:55:25 +00:00
|
|
|
}
|
2021-03-11 23:43:00 +00:00
|
|
|
|
2021-03-11 20:55:25 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 VertexLoaderBase::GetVertexComponents(const TVtxDesc& vtx_desc, const VAT& vtx_attr)
|
|
|
|
{
|
|
|
|
u32 components = 0;
|
|
|
|
if (vtx_desc.low.PosMatIdx)
|
|
|
|
components |= VB_HAS_POSMTXIDX;
|
|
|
|
for (u32 i = 0; i < vtx_desc.low.TexMatIdx.Size(); i++)
|
|
|
|
{
|
|
|
|
if (vtx_desc.low.TexMatIdx[i])
|
|
|
|
components |= VB_HAS_TEXMTXIDX0 << i;
|
|
|
|
}
|
|
|
|
// Vertices always have positions; thus there is no VB_HAS_POS as it would always be set
|
|
|
|
if (vtx_desc.low.Normal != VertexComponentFormat::NotPresent)
|
|
|
|
{
|
2022-04-22 19:50:44 +00:00
|
|
|
components |= VB_HAS_NORMAL;
|
2022-05-17 18:54:24 +00:00
|
|
|
if (vtx_attr.g0.NormalElements == NormalComponentCount::NTB)
|
2022-04-22 19:50:44 +00:00
|
|
|
components |= VB_HAS_TANGENT | VB_HAS_BINORMAL;
|
2021-03-11 20:55:25 +00:00
|
|
|
}
|
|
|
|
for (u32 i = 0; i < vtx_desc.low.Color.Size(); i++)
|
|
|
|
{
|
|
|
|
if (vtx_desc.low.Color[i] != VertexComponentFormat::NotPresent)
|
|
|
|
components |= VB_HAS_COL0 << i;
|
|
|
|
}
|
|
|
|
for (u32 i = 0; i < vtx_desc.high.TexCoord.Size(); i++)
|
|
|
|
{
|
|
|
|
if (vtx_desc.high.TexCoord[i] != VertexComponentFormat::NotPresent)
|
|
|
|
components |= VB_HAS_UV0 << i;
|
|
|
|
}
|
|
|
|
return components;
|
|
|
|
}
|
|
|
|
|
2015-12-23 00:59:32 +00:00
|
|
|
std::unique_ptr<VertexLoaderBase> VertexLoaderBase::CreateVertexLoader(const TVtxDesc& vtx_desc,
|
|
|
|
const VAT& vtx_attr)
|
2014-12-13 00:51:14 +00:00
|
|
|
{
|
2021-03-11 19:23:13 +00:00
|
|
|
std::unique_ptr<VertexLoaderBase> loader = nullptr;
|
2014-12-13 00:51:14 +00:00
|
|
|
|
2022-11-23 00:54:05 +00:00
|
|
|
// #define COMPARE_VERTEXLOADERS
|
2015-01-15 00:46:23 +00:00
|
|
|
|
2021-03-11 19:23:13 +00:00
|
|
|
#if defined(_M_X86_64)
|
2015-12-23 00:59:32 +00:00
|
|
|
loader = std::make_unique<VertexLoaderX64>(vtx_desc, vtx_attr);
|
2015-02-13 00:52:07 +00:00
|
|
|
#elif defined(_M_ARM_64)
|
2015-12-23 00:59:32 +00:00
|
|
|
loader = std::make_unique<VertexLoaderARM64>(vtx_desc, vtx_attr);
|
2014-12-18 22:27:10 +00:00
|
|
|
#endif
|
|
|
|
|
2021-03-11 19:23:13 +00:00
|
|
|
// Use the software loader as a fallback
|
|
|
|
// (not currently applicable, as both VertexLoaderX64 and VertexLoaderARM64
|
|
|
|
// are always usable, but if a loader that only works on some CPUs is created
|
|
|
|
// then this fallback would be used)
|
|
|
|
if (!loader)
|
|
|
|
loader = std::make_unique<VertexLoader>(vtx_desc, vtx_attr);
|
|
|
|
|
|
|
|
#if defined(COMPARE_VERTEXLOADERS)
|
|
|
|
return std::make_unique<VertexLoaderTester>(
|
|
|
|
std::make_unique<VertexLoader>(vtx_desc, vtx_attr), // the software one
|
|
|
|
std::move(loader), // the new one to compare
|
|
|
|
vtx_desc, vtx_attr);
|
|
|
|
#else
|
|
|
|
return loader;
|
|
|
|
#endif
|
2014-12-13 00:51:14 +00:00
|
|
|
}
|