Debian Codegraph @

Source/UnitTests/VideoCommon/VertexLoaderTest.cpp

raw symbols SCIP
1 // Copyright 2014 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include <bit>
5 #include <limits>
6 #include <memory>
7 #include <tuple>
8 #include <unordered_set>
9
10 #include <gtest/gtest.h> // NOLINT
11
12 #include "Common/MathUtil.h"
13 #include "VideoCommon/CPMemory.h"
14 #include "VideoCommon/DataReader.h"
15 #include "VideoCommon/OpcodeDecoding.h"
16 #include "VideoCommon/VertexLoaderBase.h"
17 #include "VideoCommon/VertexLoaderManager.h"
18
19 TEST(VertexLoaderUID, UniqueEnough)
20 {
21 std::unordered_set<VertexLoaderUID> uids;
22
23 TVtxDesc vtx_desc;
24 VAT vat;
25 uids.insert(VertexLoaderUID(vtx_desc, vat));
26
27 vtx_desc.low.Hex = 0x76543210;
28 vtx_desc.high.Hex = 0xFEDCBA98;
29 EXPECT_FALSE(uids.contains(VertexLoaderUID(vtx_desc, vat)));
30 uids.insert(VertexLoaderUID(vtx_desc, vat));
31
32 vat.g0.Hex = 0xFFFFFFFF;
33 vat.g1.Hex = 0xFFFFFFFF;
34 vat.g2.Hex = 0xFFFFFFFF;
35 EXPECT_FALSE(uids.contains(VertexLoaderUID(vtx_desc, vat)));
36 uids.insert(VertexLoaderUID(vtx_desc, vat));
37 }
38
39 static u8 input_memory[16 * 1024 * 1024];
40 static u8 output_memory[16 * 1024 * 1024];
41
42 class VertexLoaderTest : public testing::Test
43 {
44 protected:
45 void SetUp() override
46 {
47 memset(input_memory, 0, sizeof(input_memory));
48 memset(output_memory, 0xFF, sizeof(input_memory));
49
50 m_vtx_desc.low.Hex = 0;
51 m_vtx_desc.high.Hex = 0;
52
53 m_vtx_attr.g0.Hex = 0;
54 m_vtx_attr.g1.Hex = 0;
55 m_vtx_attr.g2.Hex = 0;
56
57 m_loader = nullptr;
58
59 ResetPointers();
60 }
61
62 void CreateAndCheckSizes(size_t input_size, size_t output_size)
63 {
64 m_loader = VertexLoaderBase::CreateVertexLoader(m_vtx_desc, m_vtx_attr);
65 ASSERT_EQ(input_size, m_loader->m_vertex_size);
66 ASSERT_EQ((int)output_size, m_loader->m_native_vtx_decl.stride);
67 }
68
69 template <typename T>
70 void Input(T val)
71 {
72 // Write swapped.
73 m_src.Write<T, true>(val);
74 }
75
76 void ExpectOut(float expected)
77 {
78 // Read unswapped.
79 const float actual = m_dst.Read<float, false>();
80
81 if (!actual || std::isnan(actual))
82 EXPECT_EQ(std::bit_cast<u32>(expected), std::bit_cast<u32>(actual));
83 else
84 EXPECT_EQ(expected, actual);
85 }
86
87 void RunVertices(int count, int expected_count = -1)
88 {
89 if (expected_count == -1)
90 expected_count = count;
91 ResetPointers();
92 int actual_count = m_loader->RunVertices(m_src.GetPointer(), m_dst.GetPointer(), count);
93 EXPECT_EQ(actual_count, expected_count);
94 }
95
96 void ResetPointers()
97 {
98 m_src = DataReader(input_memory, input_memory + sizeof(input_memory));
99 m_dst = DataReader(output_memory, output_memory + sizeof(output_memory));
100 }
101
102 DataReader m_src;
103 DataReader m_dst;
104
105 TVtxDesc m_vtx_desc;
106 VAT m_vtx_attr;
107 std::unique_ptr<VertexLoaderBase> m_loader;
108 };
109
110 class VertexLoaderParamTest
111 : public VertexLoaderTest,
112 public ::testing::WithParamInterface<
113 std::tuple<VertexComponentFormat, ComponentFormat, CoordComponentCount, int>>
114 {
115 };
116 INSTANTIATE_TEST_SUITE_P(
117 AllCombinations, VertexLoaderParamTest,
118 ::testing::Combine(
119 ::testing::Values(VertexComponentFormat::Direct, VertexComponentFormat::Index8,
120 VertexComponentFormat::Index16),
121 ::testing::Values(ComponentFormat::UByte, ComponentFormat::Byte, ComponentFormat::UShort,
122 ComponentFormat::Short, ComponentFormat::Float,
123 ComponentFormat::InvalidFloat5, ComponentFormat::InvalidFloat6,
124 ComponentFormat::InvalidFloat7),
125 ::testing::Values(CoordComponentCount::XY, CoordComponentCount::XYZ),
126 ::testing::Values(0, 1, 31) // frac
127 ));
128
129 TEST_P(VertexLoaderParamTest, PositionAll)
130 {
131 auto [addr, format, elements, frac] = GetParam();
132 this->m_vtx_desc.low.Position = addr;
133 this->m_vtx_attr.g0.PosFormat = format;
134 this->m_vtx_attr.g0.PosElements = elements;
135 this->m_vtx_attr.g0.PosFrac = frac;
136 this->m_vtx_attr.g0.ByteDequant = true;
137 const u32 elem_size = GetElementSize(format);
138 const u32 elem_count = elements == CoordComponentCount::XY ? 2 : 3;
139
140 std::vector<float> values = {
141 std::numeric_limits<float>::lowest(),
142 std::numeric_limits<float>::denorm_min(),
143 std::numeric_limits<float>::min(),
144 std::numeric_limits<float>::max(),
145 std::numeric_limits<float>::quiet_NaN(),
146 std::numeric_limits<float>::infinity(),
147 -0x8000,
148 -0x80,
149 -1,
150 -0.0,
151 0,
152 1,
153 123,
154 0x7F,
155 0xFF,
156 0x7FFF,
157 0xFFFF,
158 12345678,
159 };
160 ASSERT_EQ(0u, values.size() % 2);
161 ASSERT_EQ(0u, values.size() % 3);
162
163 int count = (int)values.size() / elem_count;
164 size_t input_size = elem_count * elem_size;
165 if (IsIndexed(addr))
166 {
167 input_size = addr == VertexComponentFormat::Index8 ? 1 : 2;
168 for (int i = 0; i < count; i++)
169 {
170 if (addr == VertexComponentFormat::Index8)
171 Input<u8>(i);
172 else
173 Input<u16>(i);
174 }
175 VertexLoaderManager::cached_arraybases[CPArray::Position] = m_src.GetPointer();
176 g_main_cp_state.array_strides[CPArray::Position] = elem_count * elem_size;
177 }
178 CreateAndCheckSizes(input_size, elem_count * sizeof(float));
179 for (float value : values)
180 {
181 switch (format)
182 {
183 case ComponentFormat::UByte:
184 Input(MathUtil::SaturatingCast<u8>(value));
185 break;
186 case ComponentFormat::Byte:
187 Input(MathUtil::SaturatingCast<s8>(value));
188 break;
189 case ComponentFormat::UShort:
190 Input(MathUtil::SaturatingCast<u16>(value));
191 break;
192 case ComponentFormat::Short:
193 Input(MathUtil::SaturatingCast<s16>(value));
194 break;
195 case ComponentFormat::Float:
196 case ComponentFormat::InvalidFloat5:
197 case ComponentFormat::InvalidFloat6:
198 case ComponentFormat::InvalidFloat7:
199 Input(value);
200 break;
201 }
202 }
203
204 RunVertices(count);
205
206 float scale = 1.f / (1u << (format >= ComponentFormat::Float ? 0 : frac));
207 for (auto iter = values.begin(); iter != values.end();)
208 {
209 float f, g;
210 switch (format)
211 {
212 case ComponentFormat::UByte:
213 f = MathUtil::SaturatingCast<u8>(*iter++);
214 g = MathUtil::SaturatingCast<u8>(*iter++);
215 break;
216 case ComponentFormat::Byte:
217 f = MathUtil::SaturatingCast<s8>(*iter++);
218 g = MathUtil::SaturatingCast<s8>(*iter++);
219 break;
220 case ComponentFormat::UShort:
221 f = MathUtil::SaturatingCast<u16>(*iter++);
222 g = MathUtil::SaturatingCast<u16>(*iter++);
223 break;
224 case ComponentFormat::Short:
225 f = MathUtil::SaturatingCast<s16>(*iter++);
226 g = MathUtil::SaturatingCast<s16>(*iter++);
227 break;
228 case ComponentFormat::Float:
229 case ComponentFormat::InvalidFloat5:
230 case ComponentFormat::InvalidFloat6:
231 case ComponentFormat::InvalidFloat7:
232 f = *iter++;
233 g = *iter++;
234 break;
235 default:
236 FAIL() << "Unknown format";
237 }
238 ExpectOut(f * scale);
239 ExpectOut(g * scale);
240 }
241 }
242
243 TEST_F(VertexLoaderTest, PositionIndex16FloatXY)
244 {
245 m_vtx_desc.low.Position = VertexComponentFormat::Index16;
246 m_vtx_attr.g0.PosFormat = ComponentFormat::Float;
247 CreateAndCheckSizes(sizeof(u16), 2 * sizeof(float));
248 Input<u16>(1);
249 Input<u16>(0);
250 VertexLoaderManager::cached_arraybases[CPArray::Position] = m_src.GetPointer();
251 g_main_cp_state.array_strides[CPArray::Position] = sizeof(float); // ;)
252 Input(1.f);
253 Input(2.f);
254 Input(3.f);
255 RunVertices(2);
256 ExpectOut(2);
257 ExpectOut(3);
258 ExpectOut(1);
259 ExpectOut(2);
260 }
261
262 class VertexLoaderSpeedTest : public VertexLoaderTest,
263 public ::testing::WithParamInterface<std::tuple<ComponentFormat, int>>
264 {
265 };
266 INSTANTIATE_TEST_SUITE_P(
267 FormatsAndElements, VertexLoaderSpeedTest,
268 ::testing::Combine(::testing::Values(ComponentFormat::UByte, ComponentFormat::Byte,
269 ComponentFormat::UShort, ComponentFormat::Short,
270 ComponentFormat::Float),
271 ::testing::Values(0, 1)));
272
273 TEST_P(VertexLoaderSpeedTest, PositionDirectAll)
274 {
275 auto [format, elements_i] = GetParam();
276 CoordComponentCount elements = static_cast<CoordComponentCount>(elements_i);
277 fmt::print("format: {}, elements: {}\n", format, elements);
278 const u32 elem_count = elements == CoordComponentCount::XY ? 2 : 3;
279 m_vtx_desc.low.Position = VertexComponentFormat::Direct;
280 m_vtx_attr.g0.PosFormat = format;
281 m_vtx_attr.g0.PosElements = elements;
282 const size_t elem_size = GetElementSize(format);
283 CreateAndCheckSizes(elem_count * elem_size, elem_count * sizeof(float));
284 for (int i = 0; i < 1000; ++i)
285 RunVertices(100000);
286 }
287
288 TEST_P(VertexLoaderSpeedTest, TexCoordSingleElement)
289 {
290 auto [format, elements_i] = GetParam();
291 TexComponentCount elements = static_cast<TexComponentCount>(elements_i);
292 fmt::print("format: {}, elements: {}\n", format, elements);
293 const u32 elem_count = elements == TexComponentCount::S ? 1 : 2;
294 m_vtx_desc.low.Position = VertexComponentFormat::Direct;
295 m_vtx_attr.g0.PosFormat = ComponentFormat::Byte;
296 m_vtx_desc.high.Tex0Coord = VertexComponentFormat::Direct;
297 m_vtx_attr.g0.Tex0CoordFormat = format;
298 m_vtx_attr.g0.Tex0CoordElements = elements;
299 const size_t elem_size = GetElementSize(format);
300 CreateAndCheckSizes(2 * sizeof(s8) + elem_count * elem_size,
301 2 * sizeof(float) + elem_count * sizeof(float));
302 for (int i = 0; i < 1000; ++i)
303 RunVertices(100000);
304 }
305
306 TEST_F(VertexLoaderTest, LargeFloatVertexSpeed)
307 {
308 // Enables most attributes in floating point indexed mode to test speed.
309 m_vtx_desc.low.PosMatIdx = true;
310 m_vtx_desc.low.Tex0MatIdx = true;
311 m_vtx_desc.low.Tex1MatIdx = true;
312 m_vtx_desc.low.Tex2MatIdx = true;
313 m_vtx_desc.low.Tex3MatIdx = true;
314 m_vtx_desc.low.Tex4MatIdx = true;
315 m_vtx_desc.low.Tex5MatIdx = true;
316 m_vtx_desc.low.Tex6MatIdx = true;
317 m_vtx_desc.low.Tex7MatIdx = true;
318 m_vtx_desc.low.Position = VertexComponentFormat::Index16;
319 m_vtx_desc.low.Normal = VertexComponentFormat::Index16;
320 m_vtx_desc.low.Color0 = VertexComponentFormat::Index16;
321 m_vtx_desc.low.Color1 = VertexComponentFormat::Index16;
322 m_vtx_desc.high.Tex0Coord = VertexComponentFormat::Index16;
323 m_vtx_desc.high.Tex1Coord = VertexComponentFormat::Index16;
324 m_vtx_desc.high.Tex2Coord = VertexComponentFormat::Index16;
325 m_vtx_desc.high.Tex3Coord = VertexComponentFormat::Index16;
326 m_vtx_desc.high.Tex4Coord = VertexComponentFormat::Index16;
327 m_vtx_desc.high.Tex5Coord = VertexComponentFormat::Index16;
328 m_vtx_desc.high.Tex6Coord = VertexComponentFormat::Index16;
329 m_vtx_desc.high.Tex7Coord = VertexComponentFormat::Index16;
330
331 m_vtx_attr.g0.PosElements = CoordComponentCount::XYZ;
332 m_vtx_attr.g0.PosFormat = ComponentFormat::Float;
333 m_vtx_attr.g0.NormalElements = NormalComponentCount::NTB;
334 m_vtx_attr.g0.NormalFormat = ComponentFormat::Float;
335 m_vtx_attr.g0.Color0Elements = ColorComponentCount::RGBA;
336 m_vtx_attr.g0.Color0Comp = ColorFormat::RGBA8888;
337 m_vtx_attr.g0.Color1Elements = ColorComponentCount::RGBA;
338 m_vtx_attr.g0.Color1Comp = ColorFormat::RGBA8888;
339 m_vtx_attr.g0.Tex0CoordElements = TexComponentCount::ST;
340 m_vtx_attr.g0.Tex0CoordFormat = ComponentFormat::Float;
341 m_vtx_attr.g1.Tex1CoordElements = TexComponentCount::ST;
342 m_vtx_attr.g1.Tex1CoordFormat = ComponentFormat::Float;
343 m_vtx_attr.g1.Tex2CoordElements = TexComponentCount::ST;
344 m_vtx_attr.g1.Tex2CoordFormat = ComponentFormat::Float;
345 m_vtx_attr.g1.Tex3CoordElements = TexComponentCount::ST;
346 m_vtx_attr.g1.Tex3CoordFormat = ComponentFormat::Float;
347 m_vtx_attr.g1.Tex4CoordElements = TexComponentCount::ST;
348 m_vtx_attr.g1.Tex4CoordFormat = ComponentFormat::Float;
349 m_vtx_attr.g2.Tex5CoordElements = TexComponentCount::ST;
350 m_vtx_attr.g2.Tex5CoordFormat = ComponentFormat::Float;
351 m_vtx_attr.g2.Tex6CoordElements = TexComponentCount::ST;
352 m_vtx_attr.g2.Tex6CoordFormat = ComponentFormat::Float;
353 m_vtx_attr.g2.Tex7CoordElements = TexComponentCount::ST;
354 m_vtx_attr.g2.Tex7CoordFormat = ComponentFormat::Float;
355
356 CreateAndCheckSizes(33, 156);
357
358 for (int i = 0; i < NUM_VERTEX_COMPONENT_ARRAYS; i++)
359 {
360 VertexLoaderManager::cached_arraybases[static_cast<CPArray>(i)] = m_src.GetPointer();
361 g_main_cp_state.array_strides[static_cast<CPArray>(i)] = 129;
362 }
363
364 // This test is only done 100x in a row since it's ~20x slower using the
365 // current vertex loader implementation.
366 for (int i = 0; i < 100; ++i)
367 RunVertices(100000);
368 }
369
370 TEST_F(VertexLoaderTest, DirectAllComponents)
371 {
372 m_vtx_desc.low.PosMatIdx = true;
373 m_vtx_desc.low.Tex0MatIdx = true;
374 m_vtx_desc.low.Tex1MatIdx = true;
375 m_vtx_desc.low.Tex2MatIdx = true;
376 m_vtx_desc.low.Tex3MatIdx = true;
377 m_vtx_desc.low.Tex4MatIdx = true;
378 m_vtx_desc.low.Tex5MatIdx = true;
379 m_vtx_desc.low.Tex6MatIdx = true;
380 m_vtx_desc.low.Tex7MatIdx = true;
381 m_vtx_desc.low.Position = VertexComponentFormat::Direct;
382 m_vtx_desc.low.Normal = VertexComponentFormat::Direct;
383 m_vtx_desc.low.Color0 = VertexComponentFormat::Direct;
384 m_vtx_desc.low.Color1 = VertexComponentFormat::Direct;
385 m_vtx_desc.high.Tex0Coord = VertexComponentFormat::Direct;
386 m_vtx_desc.high.Tex1Coord = VertexComponentFormat::Direct;
387 m_vtx_desc.high.Tex2Coord = VertexComponentFormat::Direct;
388 m_vtx_desc.high.Tex3Coord = VertexComponentFormat::Direct;
389 m_vtx_desc.high.Tex4Coord = VertexComponentFormat::Direct;
390 m_vtx_desc.high.Tex5Coord = VertexComponentFormat::Direct;
391 m_vtx_desc.high.Tex6Coord = VertexComponentFormat::Direct;
392 m_vtx_desc.high.Tex7Coord = VertexComponentFormat::Direct;
393
394 m_vtx_attr.g0.PosElements = CoordComponentCount::XYZ;
395 m_vtx_attr.g0.PosFormat = ComponentFormat::Float;
396 m_vtx_attr.g0.NormalElements = NormalComponentCount::NTB;
397 m_vtx_attr.g0.NormalFormat = ComponentFormat::Float;
398 m_vtx_attr.g0.Color0Elements = ColorComponentCount::RGBA;
399 m_vtx_attr.g0.Color0Comp = ColorFormat::RGBA8888;
400 m_vtx_attr.g0.Color1Elements = ColorComponentCount::RGBA;
401 m_vtx_attr.g0.Color1Comp = ColorFormat::RGBA8888;
402 m_vtx_attr.g0.Tex0CoordElements = TexComponentCount::ST;
403 m_vtx_attr.g0.Tex0CoordFormat = ComponentFormat::Float;
404 m_vtx_attr.g1.Tex1CoordElements = TexComponentCount::ST;
405 m_vtx_attr.g1.Tex1CoordFormat = ComponentFormat::Float;
406 m_vtx_attr.g1.Tex2CoordElements = TexComponentCount::ST;
407 m_vtx_attr.g1.Tex2CoordFormat = ComponentFormat::Float;
408 m_vtx_attr.g1.Tex3CoordElements = TexComponentCount::ST;
409 m_vtx_attr.g1.Tex3CoordFormat = ComponentFormat::Float;
410 m_vtx_attr.g1.Tex4CoordElements = TexComponentCount::ST;
411 m_vtx_attr.g1.Tex4CoordFormat = ComponentFormat::Float;
412 m_vtx_attr.g2.Tex5CoordElements = TexComponentCount::ST;
413 m_vtx_attr.g2.Tex5CoordFormat = ComponentFormat::Float;
414 m_vtx_attr.g2.Tex6CoordElements = TexComponentCount::ST;
415 m_vtx_attr.g2.Tex6CoordFormat = ComponentFormat::Float;
416 m_vtx_attr.g2.Tex7CoordElements = TexComponentCount::ST;
417 m_vtx_attr.g2.Tex7CoordFormat = ComponentFormat::Float;
418
419 CreateAndCheckSizes(129, 39 * sizeof(float));
420
421 // Pos matrix idx
422 Input<u8>(20);
423 // Tex matrix idx
424 Input<u8>(0);
425 Input<u8>(1);
426 Input<u8>(2);
427 Input<u8>(3);
428 Input<u8>(4);
429 Input<u8>(5);
430 Input<u8>(6);
431 Input<u8>(7);
432 // Position
433 Input(-1.0f);
434 Input(-2.0f);
435 Input(-3.0f);
436 // Normal
437 Input(-4.0f);
438 Input(-5.0f);
439 Input(-6.0f);
440 // Tangent
441 Input(-7.0f);
442 Input(-8.0f);
443 Input(-9.0f);
444 // Binormal
445 Input(-10.0f);
446 Input(-11.0f);
447 Input(-12.0f);
448 // Colors
449 Input<u32>(0x01234567);
450 Input<u32>(0x89abcdef);
451 // Texture coordinates
452 Input(0.1f);
453 Input(-0.9f);
454 Input(1.1f);
455 Input(-1.9f);
456 Input(2.1f);
457 Input(-2.9f);
458 Input(3.1f);
459 Input(-3.9f);
460 Input(4.1f);
461 Input(-4.9f);
462 Input(5.1f);
463 Input(-5.9f);
464 Input(6.1f);
465 Input(-6.9f);
466 Input(7.1f);
467 Input(-7.9f);
468
469 RunVertices(1);
470
471 // Position matrix
472 ASSERT_EQ(m_loader->m_native_vtx_decl.posmtx.offset, 0 * sizeof(float));
473 EXPECT_EQ((m_dst.Read<u32, false>()), 20u);
474 // Position
475 ASSERT_EQ(m_loader->m_native_vtx_decl.position.offset, 1 * sizeof(float));
476 ExpectOut(-1.0f);
477 ExpectOut(-2.0f);
478 ExpectOut(-3.0f);
479 // Normal
480 ASSERT_EQ(m_loader->m_native_vtx_decl.normals[0].offset, 4 * sizeof(float));
481 ExpectOut(-4.0f);
482 ExpectOut(-5.0f);
483 ExpectOut(-6.0f);
484 // Tangent
485 ASSERT_EQ(m_loader->m_native_vtx_decl.normals[1].offset, 7 * sizeof(float));
486 ExpectOut(-7.0f);
487 ExpectOut(-8.0f);
488 ExpectOut(-9.0f);
489 // Binormal
490 ASSERT_EQ(m_loader->m_native_vtx_decl.normals[2].offset, 10 * sizeof(float));
491 ExpectOut(-10.0f);
492 ExpectOut(-11.0f);
493 ExpectOut(-12.0f);
494 // Colors
495 ASSERT_EQ(m_loader->m_native_vtx_decl.colors[0].offset, 13 * sizeof(float));
496 EXPECT_EQ((m_dst.Read<u32, true>()), 0x01234567u);
497 ASSERT_EQ(m_loader->m_native_vtx_decl.colors[1].offset, 14 * sizeof(float));
498 EXPECT_EQ((m_dst.Read<u32, true>()), 0x89abcdefu);
499 // Texture coordinates and matrices (interleaved)
500 ASSERT_EQ(m_loader->m_native_vtx_decl.texcoords[0].offset, 15 * sizeof(float));
501 ExpectOut(0.1f); // S
502 ExpectOut(-0.9f); // T
503 ExpectOut(0.0f); // matrix (yes, a float)
504 ASSERT_EQ(m_loader->m_native_vtx_decl.texcoords[1].offset, 18 * sizeof(float));
505 ExpectOut(1.1f);
506 ExpectOut(-1.9f);
507 ExpectOut(1.0f);
508 ASSERT_EQ(m_loader->m_native_vtx_decl.texcoords[2].offset, 21 * sizeof(float));
509 ExpectOut(2.1f);
510 ExpectOut(-2.9f);
511 ExpectOut(2.0f);
512 ASSERT_EQ(m_loader->m_native_vtx_decl.texcoords[3].offset, 24 * sizeof(float));
513 ExpectOut(3.1f);
514 ExpectOut(-3.9f);
515 ExpectOut(3.0f);
516 ASSERT_EQ(m_loader->m_native_vtx_decl.texcoords[4].offset, 27 * sizeof(float));
517 ExpectOut(4.1f);
518 ExpectOut(-4.9f);
519 ExpectOut(4.0f);
520 ASSERT_EQ(m_loader->m_native_vtx_decl.texcoords[5].offset, 30 * sizeof(float));
521 ExpectOut(5.1f);
522 ExpectOut(-5.9f);
523 ExpectOut(5.0f);
524 ASSERT_EQ(m_loader->m_native_vtx_decl.texcoords[6].offset, 33 * sizeof(float));
525 ExpectOut(6.1f);
526 ExpectOut(-6.9f);
527 ExpectOut(6.0f);
528 ASSERT_EQ(m_loader->m_native_vtx_decl.texcoords[7].offset, 36 * sizeof(float));
529 ExpectOut(7.1f);
530 ExpectOut(-7.9f);
531 ExpectOut(7.0f);
532 }
533
534 class VertexLoaderNormalTest
535 : public VertexLoaderTest,
536 public ::testing::WithParamInterface<
537 std::tuple<VertexComponentFormat, ComponentFormat, NormalComponentCount, bool>>
538 {
539 };
540 INSTANTIATE_TEST_SUITE_P(
541 AllCombinations, VertexLoaderNormalTest,
542 ::testing::Combine(
543 ::testing::Values(VertexComponentFormat::NotPresent, VertexComponentFormat::Direct,
544 VertexComponentFormat::Index8, VertexComponentFormat::Index16),
545 ::testing::Values(ComponentFormat::UByte, ComponentFormat::Byte, ComponentFormat::UShort,
546 ComponentFormat::Short, ComponentFormat::Float,
547 ComponentFormat::InvalidFloat5, ComponentFormat::InvalidFloat6,
548 ComponentFormat::InvalidFloat7),
549 ::testing::Values(NormalComponentCount::N, NormalComponentCount::NTB),
550 ::testing::Values(false, true)));
551
552 TEST_P(VertexLoaderNormalTest, NormalAll)
553 {
554 auto [addr, format, elements, index3] = GetParam();
555
556 m_vtx_desc.low.Position = VertexComponentFormat::Direct;
557 m_vtx_attr.g0.PosFormat = ComponentFormat::Float;
558 m_vtx_attr.g0.PosElements = CoordComponentCount::XY;
559 m_vtx_attr.g0.PosFrac = 0;
560 m_vtx_desc.low.Normal = addr;
561 m_vtx_attr.g0.NormalFormat = format;
562 m_vtx_attr.g0.NormalElements = elements;
563 m_vtx_attr.g0.NormalIndex3 = index3;
564
565 const u32 in_size = [&]() -> u32 {
566 if (addr == VertexComponentFormat::NotPresent)
567 return 0;
568
569 if (IsIndexed(addr))
570 {
571 const u32 base_size = (addr == VertexComponentFormat::Index8) ? 1 : 2;
572 if (elements == NormalComponentCount::NTB)
573 return (index3 ? 3 : 1) * base_size;
574 else
575 return 1 * base_size;
576 }
577 else
578 {
579 const u32 base_count = (elements == NormalComponentCount::NTB) ? 9 : 3;
580 const u32 base_size = GetElementSize(format);
581 return base_count * base_size;
582 }
583 }();
584 const u32 out_size = [&]() -> u32 {
585 if (addr == VertexComponentFormat::NotPresent)
586 return 0;
587
588 const u32 base_count = (elements == NormalComponentCount::NTB) ? 9 : 3;
589 return base_count * sizeof(float);
590 }();
591
592 CreateAndCheckSizes(2 * sizeof(float) + in_size, 2 * sizeof(float) + out_size);
593
594 auto input_with_expected_type = [&](float value) {
595 switch (format)
596 {
597 case ComponentFormat::UByte:
598 Input<u8>(value * (1 << 7));
599 break;
600 case ComponentFormat::Byte:
601 Input<s8>(value * (1 << 6));
602 break;
603 case ComponentFormat::UShort:
604 Input<u16>(value * (1 << 15));
605 break;
606 case ComponentFormat::Short:
607 Input<s16>(value * (1 << 14));
608 break;
609 case ComponentFormat::Float:
610 case ComponentFormat::InvalidFloat5:
611 case ComponentFormat::InvalidFloat6:
612 case ComponentFormat::InvalidFloat7:
613 Input<float>(value);
614 break;
615 }
616 };
617
618 auto create_normal = [&](int counter_base) {
619 if (addr == VertexComponentFormat::Direct)
620 {
621 input_with_expected_type(counter_base / 32.f);
622 input_with_expected_type((counter_base + 1) / 32.f);
623 input_with_expected_type((counter_base + 2) / 32.f);
624 }
625 else if (addr == VertexComponentFormat::Index8)
626 {
627 // We set up arrays so that this works
628 Input<u8>(counter_base);
629 }
630 else if (addr == VertexComponentFormat::Index16)
631 {
632 Input<u16>(counter_base);
633 }
634 // Do nothing for NotPresent
635 };
636 auto create_tangent_and_binormal = [&](int counter_base) {
637 if (IsIndexed(addr))
638 {
639 // With NormalIndex3, specifying the same index 3 times should give the same result
640 // as specifying one index in non-index3 mode (as the index is biased by bytes).
641 // If index3 is disabled, we don't want to write any more indices.
642 if (index3)
643 {
644 // Tangent
645 create_normal(counter_base);
646 // Binormal
647 create_normal(counter_base);
648 }
649 }
650 else
651 {
652 // Tangent
653 create_normal(counter_base + 3);
654 // Binormal
655 create_normal(counter_base + 6);
656 }
657 };
658
659 // Create our two vertices
660 // Position 1
661 Input(4.0f);
662 Input(8.0f);
663 // Normal 1
664 create_normal(1);
665 if (elements == NormalComponentCount::NTB)
666 {
667 create_tangent_and_binormal(1);
668 }
669
670 // Position 2
671 Input(6.0f);
672 Input(12.0f);
673 // Normal 1
674 create_normal(10);
675 if (elements == NormalComponentCount::NTB)
676 {
677 create_tangent_and_binormal(10);
678 }
679
680 // Create an array for indexed representations
681 for (int i = 0; i < NUM_VERTEX_COMPONENT_ARRAYS; i++)
682 {
683 VertexLoaderManager::cached_arraybases[static_cast<CPArray>(i)] = m_src.GetPointer();
684 g_main_cp_state.array_strides[static_cast<CPArray>(i)] = GetElementSize(format);
685 }
686
687 for (int i = 0; i < 32; i++)
688 input_with_expected_type(i / 32.f);
689
690 // Pre-fill these values to detect if they're modified
691 VertexLoaderManager::normal_cache = {-42.f, -43.f, -44.f, -45.f};
692 VertexLoaderManager::binormal_cache = {42.f, 43.f, 44.f, 45.f};
693 VertexLoaderManager::tangent_cache = {46.f, 47.f, 48.f, 49.f};
694
695 RunVertices(2);
696
697 // First vertex, position
698 ExpectOut(4.0f);
699 ExpectOut(8.0f);
700 if (addr != VertexComponentFormat::NotPresent)
701 {
702 // Normal
703 ExpectOut(1 / 32.f);
704 ExpectOut(2 / 32.f);
705 ExpectOut(3 / 32.f);
706 if (elements == NormalComponentCount::NTB)
707 {
708 // Tangent
709 ExpectOut(4 / 32.f);
710 ExpectOut(5 / 32.f);
711 ExpectOut(6 / 32.f);
712 // Binormal
713 ExpectOut(7 / 32.f);
714 ExpectOut(8 / 32.f);
715 ExpectOut(9 / 32.f);
716 }
717 }
718
719 // Second vertex, position
720 ExpectOut(6.0f);
721 ExpectOut(12.0f);
722 if (addr != VertexComponentFormat::NotPresent)
723 {
724 // Normal
725 ExpectOut(10 / 32.f);
726 ExpectOut(11 / 32.f);
727 ExpectOut(12 / 32.f);
728 EXPECT_EQ(VertexLoaderManager::normal_cache[0], 10 / 32.f);
729 EXPECT_EQ(VertexLoaderManager::normal_cache[1], 11 / 32.f);
730 EXPECT_EQ(VertexLoaderManager::normal_cache[2], 12 / 32.f);
731 if (elements == NormalComponentCount::NTB)
732 {
733 // Tangent
734 ExpectOut(13 / 32.f);
735 ExpectOut(14 / 32.f);
736 ExpectOut(15 / 32.f);
737 // Binormal
738 ExpectOut(16 / 32.f);
739 ExpectOut(17 / 32.f);
740 ExpectOut(18 / 32.f);
741
742 EXPECT_EQ(VertexLoaderManager::tangent_cache[0], 13 / 32.f);
743 EXPECT_EQ(VertexLoaderManager::tangent_cache[1], 14 / 32.f);
744 EXPECT_EQ(VertexLoaderManager::tangent_cache[2], 15 / 32.f);
745 // Last index is padding/junk
746 EXPECT_EQ(VertexLoaderManager::binormal_cache[0], 16 / 32.f);
747 EXPECT_EQ(VertexLoaderManager::binormal_cache[1], 17 / 32.f);
748 EXPECT_EQ(VertexLoaderManager::binormal_cache[2], 18 / 32.f);
749 }
750 }
751
752 if (addr == VertexComponentFormat::NotPresent)
753 {
754 // Expect these to not be written
755 EXPECT_EQ(VertexLoaderManager::normal_cache[0], -42.f);
756 EXPECT_EQ(VertexLoaderManager::normal_cache[1], -43.f);
757 EXPECT_EQ(VertexLoaderManager::normal_cache[2], -44.f);
758 EXPECT_EQ(VertexLoaderManager::normal_cache[3], -45.f);
759 }
760 if (addr == VertexComponentFormat::NotPresent || elements == NormalComponentCount::N)
761 {
762 // Expect these to not be written
763 EXPECT_EQ(VertexLoaderManager::binormal_cache[0], 42.f);
764 EXPECT_EQ(VertexLoaderManager::binormal_cache[1], 43.f);
765 EXPECT_EQ(VertexLoaderManager::binormal_cache[2], 44.f);
766 EXPECT_EQ(VertexLoaderManager::binormal_cache[3], 45.f);
767 EXPECT_EQ(VertexLoaderManager::tangent_cache[0], 46.f);
768 EXPECT_EQ(VertexLoaderManager::tangent_cache[1], 47.f);
769 EXPECT_EQ(VertexLoaderManager::tangent_cache[2], 48.f);
770 EXPECT_EQ(VertexLoaderManager::tangent_cache[3], 49.f);
771 }
772 }
773
774 class VertexLoaderSkippedColorsTest : public VertexLoaderTest,
775 public ::testing::WithParamInterface<std::tuple<bool, bool>>
776 {
777 };
778 INSTANTIATE_TEST_SUITE_P(AllCombinations, VertexLoaderSkippedColorsTest,
779 ::testing::Combine(::testing::Values(false, true),
780 ::testing::Values(false, true)));
781
782 TEST_P(VertexLoaderSkippedColorsTest, SkippedColors)
783 {
784 auto [enable_color_0, enable_color_1] = GetParam();
785
786 size_t input_size = 1;
787 size_t output_size = 3 * sizeof(float);
788 size_t color_0_offset = 0;
789 size_t color_1_offset = 0;
790
791 m_vtx_desc.low.Position = VertexComponentFormat::Index8;
792 if (enable_color_0)
793 {
794 m_vtx_desc.low.Color0 = VertexComponentFormat::Index8;
795 input_size++;
796 color_0_offset = output_size;
797 output_size += sizeof(u32);
798 }
799 if (enable_color_1)
800 {
801 m_vtx_desc.low.Color1 = VertexComponentFormat::Index8;
802 input_size++;
803 color_1_offset = output_size;
804 output_size += sizeof(u32);
805 }
806
807 m_vtx_attr.g0.PosElements = CoordComponentCount::XYZ;
808 m_vtx_attr.g0.PosFormat = ComponentFormat::Float;
809 m_vtx_attr.g0.Color0Elements = ColorComponentCount::RGBA;
810 m_vtx_attr.g0.Color0Comp = ColorFormat::RGBA8888;
811 m_vtx_attr.g0.Color1Elements = ColorComponentCount::RGBA;
812 m_vtx_attr.g0.Color1Comp = ColorFormat::RGBA8888;
813
814 CreateAndCheckSizes(input_size, output_size);
815
816 // Vertex 0
817 Input<u8>(1);
818 if (enable_color_0)
819 Input<u8>(1);
820 if (enable_color_1)
821 Input<u8>(1);
822 // Vertex 1
823 Input<u8>(0);
824 if (enable_color_0)
825 Input<u8>(0);
826 if (enable_color_1)
827 Input<u8>(0);
828 // Position array
829 VertexLoaderManager::cached_arraybases[CPArray::Position] = m_src.GetPointer();
830 g_main_cp_state.array_strides[CPArray::Position] =
831 sizeof(float); // so 1, 2, 3 for index 0; 2, 3, 4 for index 1
832 Input(1.f);
833 Input(2.f);
834 Input(3.f);
835 Input(4.f);
836 // Color array 0
837 VertexLoaderManager::cached_arraybases[CPArray::Color0] = m_src.GetPointer();
838 g_main_cp_state.array_strides[CPArray::Color0] = sizeof(u32);
839 Input<u32>(0x00010203u);
840 Input<u32>(0x04050607u);
841 // Color array 1
842 VertexLoaderManager::cached_arraybases[CPArray::Color1] = m_src.GetPointer();
843 g_main_cp_state.array_strides[CPArray::Color1] = sizeof(u32);
844 Input<u32>(0x08090a0bu);
845 Input<u32>(0x0c0d0e0fu);
846
847 ASSERT_EQ(m_loader->m_native_vtx_decl.colors[0].enable, enable_color_0);
848 if (enable_color_0)
849 {
850 ASSERT_EQ(m_loader->m_native_vtx_decl.colors[0].offset, color_0_offset);
851 }
852 ASSERT_EQ(m_loader->m_native_vtx_decl.colors[1].enable, enable_color_1);
853 if (enable_color_1)
854 {
855 ASSERT_EQ(m_loader->m_native_vtx_decl.colors[1].offset, color_1_offset);
856 }
857
858 RunVertices(2);
859 // Vertex 0
860 ExpectOut(2);
861 ExpectOut(3);
862 ExpectOut(4);
863 if (enable_color_0)
864 {
865 EXPECT_EQ((m_dst.Read<u32, true>()), 0x04050607u);
866 }
867 if (enable_color_1)
868 {
869 EXPECT_EQ((m_dst.Read<u32, true>()), 0x0c0d0e0fu);
870 }
871 // Vertex 1
872 ExpectOut(1);
873 ExpectOut(2);
874 ExpectOut(3);
875 if (enable_color_0)
876 {
877 EXPECT_EQ((m_dst.Read<u32, true>()), 0x00010203u);
878 }
879 if (enable_color_1)
880 {
881 EXPECT_EQ((m_dst.Read<u32, true>()), 0x08090a0bu);
882 }
883 }
884
885 class VertexLoaderSkippedTexCoordsTest : public VertexLoaderTest,
886 public ::testing::WithParamInterface<u32>
887 {
888 public:
889 static constexpr u32 NUM_COMPONENTS_TO_TEST = 3;
890 static constexpr u32 NUM_PARAMETERS_PER_COMPONENT = 3;
891 static constexpr u32 NUM_COMBINATIONS =
892 1 << (NUM_COMPONENTS_TO_TEST * NUM_PARAMETERS_PER_COMPONENT);
893 };
894 INSTANTIATE_TEST_SUITE_P(AllCombinations, VertexLoaderSkippedTexCoordsTest,
895 ::testing::Range(0u, VertexLoaderSkippedTexCoordsTest::NUM_COMBINATIONS));
896
897 TEST_P(VertexLoaderSkippedTexCoordsTest, SkippedTextures)
898 {
899 std::array<bool, NUM_COMPONENTS_TO_TEST> enable_tex, enable_matrix, use_st;
900 const u32 param = GetParam();
901 for (u32 component = 0; component < NUM_COMPONENTS_TO_TEST; component++)
902 {
903 const u32 bits = param >> (component * NUM_PARAMETERS_PER_COMPONENT);
904 enable_tex[component] = (bits & 1);
905 enable_matrix[component] = (bits & 2);
906 use_st[component] = (bits & 4);
907 }
908
909 size_t input_size = 1;
910 size_t output_size = 3 * sizeof(float);
911
912 std::array<bool, NUM_COMPONENTS_TO_TEST> component_enabled{};
913 std::array<size_t, NUM_COMPONENTS_TO_TEST> component_offset{};
914
915 m_vtx_desc.low.Position = VertexComponentFormat::Index8;
916 m_vtx_attr.g0.PosElements = CoordComponentCount::XYZ;
917 m_vtx_attr.g0.PosFormat = ComponentFormat::Float;
918
919 for (size_t i = 0; i < NUM_COMPONENTS_TO_TEST; i++)
920 {
921 if (enable_matrix[i] || enable_tex[i])
922 {
923 component_enabled[i] = true;
924 component_offset[i] = output_size;
925 if (enable_matrix[i])
926 {
927 output_size += 3 * sizeof(float);
928 }
929 else
930 {
931 if (use_st[i])
932 {
933 output_size += 2 * sizeof(float);
934 }
935 else
936 {
937 output_size += sizeof(float);
938 }
939 }
940 }
941 if (enable_matrix[i])
942 {
943 m_vtx_desc.low.TexMatIdx[i] = enable_matrix[i];
944 input_size++;
945 }
946 if (enable_tex[i])
947 {
948 m_vtx_desc.high.TexCoord[i] = VertexComponentFormat::Index8;
949 input_size++;
950 }
951
952 m_vtx_attr.SetTexElements(i, use_st[i] ? TexComponentCount::ST : TexComponentCount::S);
953 m_vtx_attr.SetTexFormat(i, ComponentFormat::Float);
954 m_vtx_attr.SetTexFrac(i, 0);
955 }
956
957 CreateAndCheckSizes(input_size, output_size);
958
959 // Vertex 0
960 for (size_t i = 0; i < NUM_COMPONENTS_TO_TEST; i++)
961 {
962 if (enable_matrix[i])
963 Input<u8>(u8(20 + i));
964 }
965 Input<u8>(1); // Position
966 for (size_t i = 0; i < NUM_COMPONENTS_TO_TEST; i++)
967 {
968 if (enable_tex[i])
969 Input<u8>(1);
970 }
971 // Vertex 1
972 for (size_t i = 0; i < NUM_COMPONENTS_TO_TEST; i++)
973 {
974 if (enable_matrix[i])
975 Input<u8>(u8(10 + i));
976 }
977 Input<u8>(0); // Position
978 for (size_t i = 0; i < NUM_COMPONENTS_TO_TEST; i++)
979 {
980 if (enable_tex[i])
981 Input<u8>(0);
982 }
983 // Position array
984 VertexLoaderManager::cached_arraybases[CPArray::Position] = m_src.GetPointer();
985 g_main_cp_state.array_strides[CPArray::Position] =
986 sizeof(float); // so 1, 2, 3 for index 0; 2, 3, 4 for index 1
987 Input(1.f);
988 Input(2.f);
989 Input(3.f);
990 Input(4.f);
991 // Texture coord arrays
992 for (u8 i = 0; i < NUM_COMPONENTS_TO_TEST; i++)
993 {
994 VertexLoaderManager::cached_arraybases[CPArray::TexCoord0 + i] = m_src.GetPointer();
995 g_main_cp_state.array_strides[CPArray::TexCoord0 + i] = 2 * sizeof(float);
996 Input<float>(i * 100 + 11);
997 Input<float>(i * 100 + 12);
998 Input<float>(i * 100 + 21);
999 Input<float>(i * 100 + 22);
1000 }
1001
1002 for (size_t i = 0; i < NUM_COMPONENTS_TO_TEST; i++)
1003 {
1004 ASSERT_EQ(m_loader->m_native_vtx_decl.texcoords[i].enable, component_enabled[i]);
1005 if (component_enabled[i])
1006 {
1007 ASSERT_EQ(m_loader->m_native_vtx_decl.texcoords[i].offset, component_offset[i]);
1008 }
1009 }
1010
1011 RunVertices(2);
1012
1013 // Vertex 0
1014 ExpectOut(2);
1015 ExpectOut(3);
1016 ExpectOut(4);
1017 for (size_t i = 0; i < NUM_COMPONENTS_TO_TEST; i++)
1018 {
1019 size_t num_read = 0;
1020 if (enable_tex[i])
1021 {
1022 ExpectOut(i * 100 + 21);
1023 num_read++;
1024 if (use_st[i])
1025 {
1026 ExpectOut(i * 100 + 22);
1027 num_read++;
1028 }
1029 }
1030 if (enable_matrix[i])
1031 {
1032 // With a matrix there are always 3 components; otherwise-unused components should be 0
1033 while (num_read++ < 2)
1034 ExpectOut(0);
1035 ExpectOut(20 + i);
1036 }
1037 }
1038 // Vertex 1
1039 ExpectOut(1);
1040 ExpectOut(2);
1041 ExpectOut(3);
1042 for (size_t i = 0; i < NUM_COMPONENTS_TO_TEST; i++)
1043 {
1044 size_t num_read = 0;
1045 if (enable_tex[i])
1046 {
1047 ExpectOut(i * 100 + 11);
1048 num_read++;
1049 if (use_st[i])
1050 {
1051 ExpectOut(i * 100 + 12);
1052 num_read++;
1053 }
1054 }
1055 if (enable_matrix[i])
1056 {
1057 // With a matrix there are always 3 components; otherwise-unused components should be 0
1058 while (num_read++ < 2)
1059 ExpectOut(0);
1060 ExpectOut(10 + i);
1061 }
1062 }
1063 }
1064
1065 // For gtest, which doesn't know about our fmt::formatters by default
1066 static void PrintTo(const VertexComponentFormat& t, std::ostream* os)
1067 {
1068 *os << fmt::to_string(t);
1069 }
1070 static void PrintTo(const ComponentFormat& t, std::ostream* os)
1071 {
1072 *os << fmt::to_string(t);
1073 }
1074 static void PrintTo(const CoordComponentCount& t, std::ostream* os)
1075 {
1076 *os << fmt::to_string(t);
1077 }
1078 static void PrintTo(const NormalComponentCount& t, std::ostream* os)
1079 {
1080 *os << fmt::to_string(t);
1081 }