Debian Codegraph @

Source/Core/VideoCommon/VertexLoaderBase.cpp

raw symbols SCIP
1 // Copyright 2014 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoCommon/VertexLoaderBase.h"
5
6 #include <array>
7 #include <bit>
8 #include <cstring>
9 #include <memory>
10 #include <vector>
11
12 #include <fmt/ranges.h>
13
14 #include "Common/Assert.h"
15 #include "Common/CommonTypes.h"
16 #include "Common/MsgHandler.h"
17
18 #include "VideoCommon/VertexLoader.h"
19 #include "VideoCommon/VertexLoaderManager.h"
20 #include "VideoCommon/VertexLoader_Color.h"
21 #include "VideoCommon/VertexLoader_Normal.h"
22 #include "VideoCommon/VertexLoader_Position.h"
23 #include "VideoCommon/VertexLoader_TextCoord.h"
24 #include "VideoCommon/VideoConfig.h"
25
26 #ifdef _M_X86_64
27 #include "VideoCommon/VertexLoaderX64.h"
28 #elif defined(_M_ARM_64)
29 #include "VideoCommon/VertexLoaderARM64.h"
30 #endif
31
32 // a hacky implementation to compare two vertex loaders
33 class VertexLoaderTester : public VertexLoaderBase
34 {
35 public:
36 VertexLoaderTester(std::unique_ptr<VertexLoaderBase> a_, std::unique_ptr<VertexLoaderBase> b_,
37 const TVtxDesc& vtx_desc, const VAT& vtx_attr)
38 : VertexLoaderBase(vtx_desc, vtx_attr), a(std::move(a_)), b(std::move(b_))
39 {
40 ASSERT(a && b);
41 if (a->m_vertex_size == b->m_vertex_size && a->m_native_components == b->m_native_components &&
42 a->m_native_vtx_decl.stride == b->m_native_vtx_decl.stride)
43 {
44 // These are generated from the VAT and vertex desc, so they should match.
45 // m_native_vtx_decl.stride isn't set yet, though.
46 ASSERT(m_vertex_size == a->m_vertex_size && m_native_components == a->m_native_components);
47
48 memcpy(&m_native_vtx_decl, &a->m_native_vtx_decl, sizeof(PortableVertexDeclaration));
49 }
50 else
51 {
52 PanicAlertFmt("Can't compare vertex loaders that expect different vertex formats!\n"
53 "a: m_vertex_size {}, m_native_components {:#010x}, stride {}\n"
54 "b: m_vertex_size {}, m_native_components {:#010x}, stride {}",
55 a->m_vertex_size, a->m_native_components, a->m_native_vtx_decl.stride,
56 b->m_vertex_size, b->m_native_components, b->m_native_vtx_decl.stride);
57 }
58 }
59 int RunVertices(const u8* src, u8* dst, int count) override
60 {
61 buffer_a.resize(count * a->m_native_vtx_decl.stride + 4);
62 buffer_b.resize(count * b->m_native_vtx_decl.stride + 4);
63
64 const std::array<u32, 3> old_position_matrix_index_cache =
65 VertexLoaderManager::position_matrix_index_cache;
66 const std::array<std::array<float, 4>, 3> old_position_cache =
67 VertexLoaderManager::position_cache;
68 const std::array<float, 4> old_normal_cache = VertexLoaderManager::normal_cache;
69 const std::array<float, 4> old_tangent_cache = VertexLoaderManager::tangent_cache;
70 const std::array<float, 4> old_binormal_cache = VertexLoaderManager::binormal_cache;
71
72 const int count_a = a->RunVertices(src, buffer_a.data(), count);
73
74 const std::array<u32, 3> a_position_matrix_index_cache =
75 VertexLoaderManager::position_matrix_index_cache;
76 const std::array<std::array<float, 4>, 3> a_position_cache =
77 VertexLoaderManager::position_cache;
78 const std::array<float, 4> a_normal_cache = VertexLoaderManager::normal_cache;
79 const std::array<float, 4> a_tangent_cache = VertexLoaderManager::tangent_cache;
80 const std::array<float, 4> a_binormal_cache = VertexLoaderManager::binormal_cache;
81
82 // Reset state before running b
83 VertexLoaderManager::position_matrix_index_cache = old_position_matrix_index_cache;
84 VertexLoaderManager::position_cache = old_position_cache;
85 VertexLoaderManager::normal_cache = old_normal_cache;
86 VertexLoaderManager::tangent_cache = old_tangent_cache;
87 VertexLoaderManager::binormal_cache = old_binormal_cache;
88
89 const int count_b = b->RunVertices(src, buffer_b.data(), count);
90
91 const std::array<u32, 3> b_position_matrix_index_cache =
92 VertexLoaderManager::position_matrix_index_cache;
93 const std::array<std::array<float, 4>, 3> b_position_cache =
94 VertexLoaderManager::position_cache;
95 const std::array<float, 4> b_normal_cache = VertexLoaderManager::normal_cache;
96 const std::array<float, 4> b_tangent_cache = VertexLoaderManager::tangent_cache;
97 const std::array<float, 4> b_binormal_cache = VertexLoaderManager::binormal_cache;
98
99 ASSERT_MSG(VIDEO, count_a == count_b,
100 "The two vertex loaders have loaded a different amount of vertices (a: {}, b: {}).",
101 count_a, count_b);
102
103 ASSERT_MSG(VIDEO,
104 memcmp(buffer_a.data(), buffer_b.data(),
105 std::min(count_a, count_b) * m_native_vtx_decl.stride) == 0,
106 "The two vertex loaders have loaded different data. Configuration:"
107 "\nVertex desc:\n{}\n\nVertex attr:\n{}",
108 m_VtxDesc, m_VtxAttr);
109
110 ASSERT_MSG(VIDEO, a_position_matrix_index_cache == b_position_matrix_index_cache,
111 "Expected matching position matrix caches after loading (a: {}; b: {})",
112 fmt::join(a_position_matrix_index_cache, ", "),
113 fmt::join(b_position_matrix_index_cache, ", "));
114
115 // Some games (e.g. Donkey Kong Country Returns) have a few draws that contain NaN.
116 // Since NaN != NaN, we need to compare the bits instead.
117 const auto bit_equal = [](float val_a, float val_b) {
118 return std::bit_cast<u32>(val_a) == std::bit_cast<u32>(val_b);
119 };
120
121 // The last element is allowed to be garbage for SIMD overwrites.
122 // For XY, the last 2 are garbage.
123 const bool positions_match = [&] {
124 const size_t max_component = m_VtxAttr.g0.PosElements == CoordComponentCount::XYZ ? 3 : 2;
125 for (size_t vertex = 0; vertex < 3; vertex++)
126 {
127 if (!std::equal(a_position_cache[vertex].begin(),
128 a_position_cache[vertex].begin() + max_component,
129 b_position_cache[vertex].begin(), bit_equal))
130 {
131 return false;
132 }
133 }
134 return true;
135 }();
136
137 ASSERT_MSG(VIDEO, positions_match,
138 "Expected matching position caches after loading (a: {} / {} / {}; b: {} / {} / {})",
139 fmt::join(a_position_cache[0], ", "), fmt::join(a_position_cache[1], ", "),
140 fmt::join(a_position_cache[2], ", "), fmt::join(b_position_cache[0], ", "),
141 fmt::join(b_position_cache[1], ", "), fmt::join(b_position_cache[2], ", "));
142
143 // The last element is allowed to be garbage for SIMD overwrites
144 ASSERT_MSG(VIDEO,
145 std::equal(a_normal_cache.begin(), a_normal_cache.begin() + 3,
146 b_normal_cache.begin(), b_normal_cache.begin() + 3, bit_equal),
147 "Expected matching normal caches after loading (a: {}; b: {})",
148 fmt::join(a_normal_cache, ", "), fmt::join(b_normal_cache, ", "));
149
150 ASSERT_MSG(VIDEO,
151 std::equal(a_tangent_cache.begin(), a_tangent_cache.begin() + 3,
152 b_tangent_cache.begin(), b_tangent_cache.begin() + 3, bit_equal),
153 "Expected matching tangent caches after loading (a: {}; b: {})",
154 fmt::join(a_tangent_cache, ", "), fmt::join(b_tangent_cache, ", "));
155
156 ASSERT_MSG(VIDEO,
157 std::equal(a_binormal_cache.begin(), a_binormal_cache.begin() + 3,
158 b_binormal_cache.begin(), b_binormal_cache.begin() + 3, bit_equal),
159 "Expected matching binormal caches after loading (a: {}; b: {})",
160 fmt::join(a_binormal_cache, ", "), fmt::join(b_binormal_cache, ", "));
161
162 memcpy(dst, buffer_a.data(), count_a * m_native_vtx_decl.stride);
163 m_numLoadedVertices += count;
164 return count_a;
165 }
166
167 private:
168 std::unique_ptr<VertexLoaderBase> a;
169 std::unique_ptr<VertexLoaderBase> b;
170
171 std::vector<u8> buffer_a;
172 std::vector<u8> buffer_b;
173 };
174
175 u32 VertexLoaderBase::GetVertexSize(const TVtxDesc& vtx_desc, const VAT& vtx_attr)
176 {
177 u32 size = 0;
178
179 // Each enabled TexMatIdx adds one byte, as does PosMatIdx
180 size += std::popcount(vtx_desc.low.Hex & 0x1FF);
181
182 const u32 pos_size = VertexLoader_Position::GetSize(vtx_desc.low.Position, vtx_attr.g0.PosFormat,
183 vtx_attr.g0.PosElements);
184 size += pos_size;
185 const u32 norm_size =
186 VertexLoader_Normal::GetSize(vtx_desc.low.Normal, vtx_attr.g0.NormalFormat,
187 vtx_attr.g0.NormalElements, vtx_attr.g0.NormalIndex3);
188 size += norm_size;
189 for (u32 i = 0; i < vtx_desc.low.Color.Size(); i++)
190 {
191 const u32 color_size =
192 VertexLoader_Color::GetSize(vtx_desc.low.Color[i], vtx_attr.GetColorFormat(i));
193 size += color_size;
194 }
195 for (u32 i = 0; i < vtx_desc.high.TexCoord.Size(); i++)
196 {
197 const u32 tc_size = VertexLoader_TextCoord::GetSize(
198 vtx_desc.high.TexCoord[i], vtx_attr.GetTexFormat(i), vtx_attr.GetTexElements(i));
199 size += tc_size;
200 }
201
202 return size;
203 }
204
205 u32 VertexLoaderBase::GetVertexComponents(const TVtxDesc& vtx_desc, const VAT& vtx_attr)
206 {
207 u32 components = 0;
208 if (vtx_desc.low.PosMatIdx)
209 components |= VB_HAS_POSMTXIDX;
210 for (u32 i = 0; i < vtx_desc.low.TexMatIdx.Size(); i++)
211 {
212 if (vtx_desc.low.TexMatIdx[i])
213 components |= VB_HAS_TEXMTXIDX0 << i;
214 }
215 // Vertices always have positions; thus there is no VB_HAS_POS as it would always be set
216 if (vtx_desc.low.Normal != VertexComponentFormat::NotPresent)
217 {
218 components |= VB_HAS_NORMAL;
219 if (vtx_attr.g0.NormalElements == NormalComponentCount::NTB)
220 components |= VB_HAS_TANGENT | VB_HAS_BINORMAL;
221 }
222 for (u32 i = 0; i < vtx_desc.low.Color.Size(); i++)
223 {
224 if (vtx_desc.low.Color[i] != VertexComponentFormat::NotPresent)
225 components |= VB_HAS_COL0 << i;
226 }
227 for (u32 i = 0; i < vtx_desc.high.TexCoord.Size(); i++)
228 {
229 if (vtx_desc.high.TexCoord[i] != VertexComponentFormat::NotPresent)
230 components |= VB_HAS_UV0 << i;
231 }
232 return components;
233 }
234
235 std::unique_ptr<VertexLoaderBase> VertexLoaderBase::CreateVertexLoader(const TVtxDesc& vtx_desc,
236 const VAT& vtx_attr)
237 {
238 const VertexLoaderType loader_type = g_ActiveConfig.vertex_loader_type;
239
240 if (loader_type == VertexLoaderType::Software)
241 {
242 return std::make_unique<VertexLoader>(vtx_desc, vtx_attr);
243 }
244
245 std::unique_ptr<VertexLoaderBase> native_loader = nullptr;
246
247 #if defined(_M_X86_64)
248 native_loader = std::make_unique<VertexLoaderX64>(vtx_desc, vtx_attr);
249 #elif defined(_M_ARM_64)
250 native_loader = std::make_unique<VertexLoaderARM64>(vtx_desc, vtx_attr);
251 #endif
252
253 // Use the software loader as a fallback
254 // (not currently applicable, as both VertexLoaderX64 and VertexLoaderARM64
255 // are always usable, but if a loader that only works on some CPUs is created
256 // then this fallback would be used)
257 if (!native_loader)
258 {
259 return std::make_unique<VertexLoader>(vtx_desc, vtx_attr);
260 }
261
262 if (loader_type == VertexLoaderType::Compare)
263 {
264 return std::make_unique<VertexLoaderTester>(
265 std::make_unique<VertexLoader>(vtx_desc, vtx_attr), // the software one
266 std::move(native_loader), // the new one to compare
267 vtx_desc, vtx_attr);
268 }
269
270 return native_loader;
271 }