diff options
Diffstat (limited to 'Source/UnitTests')
| -rw-r--r-- | Source/UnitTests/Common/BitFieldTest.cpp | 376 | ||||
| -rw-r--r-- | Source/UnitTests/Common/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | Source/UnitTests/Common/EnumFormatterTest.cpp | 67 | ||||
| -rw-r--r-- | Source/UnitTests/UnitTests.vcxproj | 1 | ||||
| -rw-r--r-- | Source/UnitTests/VideoCommon/VertexLoaderTest.cpp | 223 |
5 files changed, 561 insertions, 107 deletions
diff --git a/Source/UnitTests/Common/BitFieldTest.cpp b/Source/UnitTests/Common/BitFieldTest.cpp index 32b587469f..e7a7bb764c 100644 --- a/Source/UnitTests/Common/BitFieldTest.cpp +++ b/Source/UnitTests/Common/BitFieldTest.cpp @@ -6,6 +6,15 @@ #include "Common/BitField.h" #include "Common/CommonTypes.h" +#include "Common/EnumFormatter.h" + +enum class TestEnum : u64 +{ + A, + B, + C, + D, +}; union TestUnion { @@ -21,6 +30,11 @@ union TestUnion BitField<30, 4, s64> at_dword_boundary; // goes over the boundary of two u32 values BitField<15, 1, s64> signed_1bit; // allowed values: -1 and 0 + + BitField<63, 1, bool, u64> flag; + + BitField<16, 2, TestEnum> enum_1; + BitField<48, 2, TestEnum> enum_2; }; // table of raw numbers to test with @@ -51,6 +65,9 @@ TEST(BitField, Storage) EXPECT_EQ(sizeof(TestUnion), sizeof(object.regular_field_signed)); EXPECT_EQ(sizeof(TestUnion), sizeof(object.at_dword_boundary)); EXPECT_EQ(sizeof(TestUnion), sizeof(object.signed_1bit)); + EXPECT_EQ(sizeof(TestUnion), sizeof(object.flag)); + EXPECT_EQ(sizeof(TestUnion), sizeof(object.enum_1)); + EXPECT_EQ(sizeof(TestUnion), sizeof(object.enum_2)); // Now write some values to one field and check if this reflects properly // in the others. @@ -82,6 +99,9 @@ TEST(BitField, Read) EXPECT_EQ(object.regular_field_signed, (s64)object.regular_field_signed); EXPECT_EQ(object.at_dword_boundary, (s64)object.at_dword_boundary); EXPECT_EQ(object.signed_1bit, (s64)object.signed_1bit); + EXPECT_EQ(object.flag, (bool)object.flag); + EXPECT_EQ(object.enum_1, static_cast<TestEnum>(object.enum_1)); + EXPECT_EQ(object.enum_2, static_cast<TestEnum>(object.enum_2)); // Now make sure the value is indeed correct EXPECT_EQ(val, object.full_u64); @@ -91,6 +111,9 @@ TEST(BitField, Read) EXPECT_EQ(((s64)(object.hex << 52)) >> 61, object.regular_field_signed); EXPECT_EQ(((s64)(object.hex << 30)) >> 60, object.at_dword_boundary); EXPECT_EQ(((object.hex >> 15) & 1) ? -1 : 0, object.signed_1bit); + EXPECT_EQ((bool)object.flag, ((object.hex >> 63) & 1)); + EXPECT_EQ(static_cast<TestEnum>((object.hex >> 16) & 3), object.enum_1); + EXPECT_EQ(static_cast<TestEnum>((object.hex >> 48) & 3), object.enum_2); } } @@ -122,6 +145,10 @@ TEST(BitField, Assignment) // Assignment from other BitField object.at_dword_boundary = object.regular_field_signed; EXPECT_EQ(object.regular_field_signed, object.at_dword_boundary); + + // Assignment to field of a type with a size smaller than the underlying type + object.flag = (val & 2); + EXPECT_EQ(object.flag, (val & 2) != 0); } } @@ -165,5 +192,354 @@ TEST(BitField, Alignment) // Assignment from other BitField object.at_dword_boundary = object.regular_field_signed; EXPECT_EQ(object.regular_field_signed, object.at_dword_boundary); + + // Assignment to field of a type with a size smaller than the underlying type + object.flag = (val & 2); + EXPECT_EQ(object.flag, (val & 2) != 0); + } +} + +template <> +struct fmt::formatter<TestEnum> : EnumFormatter<TestEnum::D> +{ + formatter() : EnumFormatter({"A", "B", "C", "D"}) {} +}; + +// Test behavior of using BitFields with fmt +TEST(BitField, Fmt) +{ + TestUnion object; + + for (u64 val : table) + { + object.hex = val; + + // Formatting the BitField should be the same as formatting its value + EXPECT_EQ(fmt::to_string(object.full_u64), fmt::to_string(object.full_u64.Value())); + EXPECT_EQ(fmt::to_string(object.full_s64), fmt::to_string(object.full_s64.Value())); + EXPECT_EQ(fmt::to_string(object.regular_field_unsigned), + fmt::to_string(object.regular_field_unsigned.Value())); + EXPECT_EQ(fmt::to_string(object.regular_field_unsigned2), + fmt::to_string(object.regular_field_unsigned2.Value())); + EXPECT_EQ(fmt::to_string(object.regular_field_signed), + fmt::to_string(object.regular_field_signed.Value())); + EXPECT_EQ(fmt::to_string(object.at_dword_boundary), + fmt::to_string(object.at_dword_boundary.Value())); + EXPECT_EQ(fmt::to_string(object.signed_1bit), fmt::to_string(object.signed_1bit.Value())); + EXPECT_EQ(fmt::to_string(object.flag), fmt::to_string(object.flag.Value())); + // The custom enum formatter should be used properly. + EXPECT_EQ(fmt::to_string(object.enum_1), fmt::to_string(object.enum_1.Value())); + EXPECT_EQ(fmt::to_string(object.enum_2), fmt::to_string(object.enum_2.Value())); + + // Formatting the BitField should respect the format spec + EXPECT_EQ(fmt::format("{:02x}", object.full_u64), + fmt::format("{:02x}", object.full_u64.Value())); + EXPECT_EQ(fmt::format("{:02x}", object.full_s64), + fmt::format("{:02x}", object.full_s64.Value())); + EXPECT_EQ(fmt::format("{:02x}", object.regular_field_unsigned), + fmt::format("{:02x}", object.regular_field_unsigned.Value())); + EXPECT_EQ(fmt::format("{:02x}", object.regular_field_unsigned2), + fmt::format("{:02x}", object.regular_field_unsigned2.Value())); + EXPECT_EQ(fmt::format("{:02x}", object.regular_field_signed), + fmt::format("{:02x}", object.regular_field_signed.Value())); + EXPECT_EQ(fmt::format("{:02x}", object.at_dword_boundary), + fmt::format("{:02x}", object.at_dword_boundary.Value())); + EXPECT_EQ(fmt::format("{:02x}", object.signed_1bit), + fmt::format("{:02x}", object.signed_1bit.Value())); + EXPECT_EQ(fmt::format("{:02x}", object.flag), fmt::format("{:02x}", object.flag.Value())); + EXPECT_EQ(fmt::format("{:s}", object.enum_1), fmt::format("{:s}", object.enum_1.Value())); + EXPECT_EQ(fmt::format("{:s}", object.enum_2), fmt::format("{:s}", object.enum_2.Value())); + } +} + +union TestUnion2 +{ + u32 hex; + BitField<0, 2, u32> a; + BitField<2, 2, u32> b; + BitField<4, 2, u32> c; + BitFieldArray<0, 2, 3, u32> arr; +}; + +TEST(BitFieldArray, Unsigned) +{ + TestUnion2 object; + object.hex = 0; + const TestUnion2& objectc = object; + + for (u32 value : object.arr) + { + EXPECT_EQ(value, 0u); + } + + object.arr[0] = 2; + EXPECT_EQ(object.arr[0], 2u); + EXPECT_EQ(object.a, 2u); + EXPECT_EQ(object.hex, 0b00'00'10u); + + object.arr[1] = 3; + EXPECT_EQ(object.arr[1], 3u); + EXPECT_EQ(object.b, 3u); + EXPECT_EQ(object.hex, 0b00'11'10u); + + object.arr[2] = object.arr[1]; + EXPECT_EQ(object.arr[2], 3u); + EXPECT_EQ(object.c, 3u); + EXPECT_EQ(object.hex, 0b11'11'10u); + + object.arr[1] = objectc.arr[0]; + EXPECT_EQ(object.arr[1], 2u); + EXPECT_EQ(object.b, 2u); + EXPECT_EQ(object.hex, 0b11'10'10u); + + for (auto ref : object.arr) + { + ref = 1; + } + EXPECT_EQ(object.a, 1u); + EXPECT_EQ(object.b, 1u); + EXPECT_EQ(object.c, 1u); + EXPECT_EQ(object.hex, 0b01'01'01u); + + std::fill_n(object.arr.begin(), object.arr.Size(), 3); + EXPECT_EQ(object.arr[0], 3u); + EXPECT_EQ(object.arr[1], 3u); + EXPECT_EQ(object.arr[2], 3u); + EXPECT_EQ(object.hex, 0b11'11'11u); + + for (u32 i = 0; i < object.arr.Size(); i++) + { + object.arr[i] = i; + } + EXPECT_EQ(object.hex, 0b10'01'00u); + + EXPECT_EQ(objectc.arr[0], 0u); + EXPECT_EQ(objectc.arr[1], 1u); + EXPECT_EQ(objectc.arr[2], 2u); + + u32 counter = 0; + for (u32 value : objectc.arr) + { + EXPECT_EQ(value, counter); + counter++; + } + + EXPECT_EQ("[0, 1, 2]", fmt::format("[{}]", fmt::join(object.arr, ", "))); + EXPECT_EQ("[0b00, 0b01, 0b10]", fmt::format("[{:#04b}]", fmt::join(object.arr, ", "))); +} + +union TestUnion3 +{ + s32 hex; + BitField<5, 2, s32> a; + BitField<7, 2, s32> b; + BitField<9, 2, s32> c; + BitFieldArray<5, 2, 3, s32> arr; +}; + +TEST(BitFieldArray, Signed) +{ + TestUnion3 object; + object.hex = 0; + const TestUnion3& objectc = object; + + for (s32 value : object.arr) + { + EXPECT_EQ(value, 0); + } + + object.arr[0] = -2; + EXPECT_EQ(object.arr[0], -2); + EXPECT_EQ(object.a, -2); + EXPECT_EQ(object.hex, 0b00'00'10'00000); + + object.arr[1] = -1; + EXPECT_EQ(object.arr[1], -1); + EXPECT_EQ(object.b, -1); + EXPECT_EQ(object.hex, 0b00'11'10'00000); + + object.arr[2] = object.arr[1]; + EXPECT_EQ(object.arr[2], -1); + EXPECT_EQ(object.c, -1); + EXPECT_EQ(object.hex, 0b11'11'10'00000); + + object.arr[1] = objectc.arr[0]; + EXPECT_EQ(object.arr[1], -2); + EXPECT_EQ(object.b, -2); + EXPECT_EQ(object.hex, 0b11'10'10'00000); + + for (auto ref : object.arr) + { + ref = 1; + } + EXPECT_EQ(object.a, 1); + EXPECT_EQ(object.b, 1); + EXPECT_EQ(object.c, 1); + EXPECT_EQ(object.hex, 0b01'01'01'00000); + + std::fill_n(object.arr.begin(), object.arr.Size(), -1); + EXPECT_EQ(object.arr[0], -1); + EXPECT_EQ(object.arr[1], -1); + EXPECT_EQ(object.arr[2], -1); + EXPECT_EQ(object.hex, 0b11'11'11'00000); + + for (u32 i = 0; i < object.arr.Size(); i++) + { + object.arr[i] = i; + } + EXPECT_EQ(object.hex, 0b10'01'00'00000); + + EXPECT_EQ(objectc.arr[0], 0); + EXPECT_EQ(objectc.arr[1], 1); + EXPECT_EQ(objectc.arr[2], -2); + + u32 counter = 0; + for (s32 value : objectc.arr) + { + EXPECT_EQ(value, object.arr[counter++]); + } + + EXPECT_EQ("[0, 1, -2]", fmt::format("[{}]", fmt::join(object.arr, ", "))); + EXPECT_EQ("[+0b00, +0b01, -0b10]", fmt::format("[{:+#05b}]", fmt::join(object.arr, ", "))); +} + +union TestUnion4 +{ + u64 hex; + BitField<30, 2, TestEnum> a; + BitField<32, 2, TestEnum> b; + BitField<34, 2, TestEnum> c; + BitField<36, 2, TestEnum> d; + BitFieldArray<30, 2, 4, TestEnum> arr; +}; + +TEST(BitFieldArray, Enum) +{ + TestUnion4 object; + object.hex = 0; + const TestUnion4& objectc = object; + + for (TestEnum value : object.arr) + { + EXPECT_EQ(value, TestEnum::A); + } + + object.arr[0] = TestEnum::B; + EXPECT_EQ(object.arr[0], TestEnum::B); + EXPECT_EQ(object.a, TestEnum::B); + EXPECT_EQ(object.hex, 0b00'00'00'01ull << 30); + + object.arr[1] = TestEnum::C; + EXPECT_EQ(object.arr[1], TestEnum::C); + EXPECT_EQ(object.b, TestEnum::C); + EXPECT_EQ(object.hex, 0b00'00'10'01ull << 30); + + object.arr[2] = object.arr[1]; + EXPECT_EQ(object.arr[2], TestEnum::C); + EXPECT_EQ(object.c, TestEnum::C); + EXPECT_EQ(object.hex, 0b00'10'10'01ull << 30); + + object.arr[3] = objectc.arr[0]; + EXPECT_EQ(object.arr[3], TestEnum::B); + EXPECT_EQ(object.d, TestEnum::B); + EXPECT_EQ(object.hex, 0b01'10'10'01ull << 30); + + for (auto ref : object.arr) + { + ref = TestEnum::D; + } + EXPECT_EQ(object.a, TestEnum::D); + EXPECT_EQ(object.b, TestEnum::D); + EXPECT_EQ(object.c, TestEnum::D); + EXPECT_EQ(object.d, TestEnum::D); + EXPECT_EQ(object.hex, 0b11'11'11'11ull << 30); + + std::fill_n(object.arr.begin(), object.arr.Size(), TestEnum::C); + EXPECT_EQ(object.a, TestEnum::C); + EXPECT_EQ(object.b, TestEnum::C); + EXPECT_EQ(object.c, TestEnum::C); + EXPECT_EQ(object.d, TestEnum::C); + EXPECT_EQ(object.hex, 0b10'10'10'10ull << 30); + + for (u32 i = 0; i < object.arr.Size(); i++) + { + object.arr[i] = static_cast<TestEnum>(i); + } + EXPECT_EQ(object.hex, 0b11'10'01'00ull << 30); + + EXPECT_EQ(objectc.arr[0], TestEnum::A); + EXPECT_EQ(objectc.arr[1], TestEnum::B); + EXPECT_EQ(objectc.arr[2], TestEnum::C); + EXPECT_EQ(objectc.arr[3], TestEnum::D); + + u32 counter = 0; + for (TestEnum value : objectc.arr) + { + EXPECT_EQ(value, object.arr[counter++]); + } + + EXPECT_EQ("[A (0), B (1), C (2), D (3)]", fmt::format("[{}]", fmt::join(object.arr, ", "))); + EXPECT_EQ("[0x0u /* A */, 0x1u /* B */, 0x2u /* C */, 0x3u /* D */]", + fmt::format("[{:s}]", fmt::join(object.arr, ", "))); +} + +union TestUnion5 +{ + u64 hex; + BitFieldArray<0, 5, 6, u8, u64> arr1; + BitFieldArray<30, 1, 4, bool, u64> arr2; +}; + +TEST(BitFieldArray, StorageType) +{ + TestUnion5 object; + const u64 arr2_hex_1 = 0b1010ull << 30; + object.hex = arr2_hex_1; + const TestUnion5& objectc = object; + + EXPECT_FALSE(object.arr2[0]); + EXPECT_TRUE(object.arr2[1]); + EXPECT_FALSE(object.arr2[2]); + EXPECT_TRUE(object.arr2[3]); + + object.arr1[0] = 0; + object.arr1[1] = 1; + object.arr1[2] = 2; + object.arr1[3] = 4; + object.arr1[4] = 8; + object.arr1[5] = 16; + const u64 arr1_hex = 0b10000'01000'00100'00010'00001'00000; + EXPECT_EQ(object.hex, arr1_hex | arr2_hex_1); + + object.arr2[2] = object.arr2[0] = true; + object.arr2[3] = object.arr2[1] = false; + const u64 arr2_hex_2 = 0b0101ull << 30; + EXPECT_EQ(object.hex, arr1_hex | arr2_hex_2); + + object.arr2[2] = object.arr2[1]; + object.arr2[3] = objectc.arr2[0]; + const u64 arr2_hex_3 = 0b1001ull << 30; + EXPECT_EQ(object.hex, arr1_hex | arr2_hex_3); + + u32 counter = 0; + for (u8 value : object.arr1) + { + EXPECT_EQ(value, object.arr1[counter++]); + } + counter = 0; + for (bool value : object.arr2) + { + EXPECT_EQ(value, object.arr2[counter++]); + } + + counter = 0; + for (u8 value : objectc.arr1) + { + EXPECT_EQ(value, object.arr1[counter++]); + } + counter = 0; + for (bool value : objectc.arr2) + { + EXPECT_EQ(value, object.arr2[counter++]); } } diff --git a/Source/UnitTests/Common/CMakeLists.txt b/Source/UnitTests/Common/CMakeLists.txt index 120fee6c33..86d9dedfcf 100644 --- a/Source/UnitTests/Common/CMakeLists.txt +++ b/Source/UnitTests/Common/CMakeLists.txt @@ -5,6 +5,7 @@ add_dolphin_test(BlockingLoopTest BlockingLoopTest.cpp) add_dolphin_test(BusyLoopTest BusyLoopTest.cpp) add_dolphin_test(CommonFuncsTest CommonFuncsTest.cpp) add_dolphin_test(CryptoEcTest Crypto/EcTest.cpp) +add_dolphin_test(EnumFormatterTest EnumFormatterTest.cpp) add_dolphin_test(EventTest EventTest.cpp) add_dolphin_test(FixedSizeQueueTest FixedSizeQueueTest.cpp) add_dolphin_test(FlagTest FlagTest.cpp) diff --git a/Source/UnitTests/Common/EnumFormatterTest.cpp b/Source/UnitTests/Common/EnumFormatterTest.cpp new file mode 100644 index 0000000000..fb713db2f2 --- /dev/null +++ b/Source/UnitTests/Common/EnumFormatterTest.cpp @@ -0,0 +1,67 @@ +// Copyright 2021 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include <gtest/gtest.h> + +#include "Common/CommonTypes.h" +#include "Common/EnumFormatter.h" + +enum class Enum1 : u32 +{ + A = 0, + B = 1, + C = 2, +}; + +template <> +struct fmt::formatter<Enum1> : EnumFormatter<Enum1::C> +{ + formatter() : EnumFormatter({"A", "B", "C"}) {} +}; + +enum class Enum2 : s32 +{ + D = 0, + E = 1, + F = 3, +}; + +template <> +struct fmt::formatter<Enum2> : EnumFormatter<Enum2::F> +{ + static constexpr array_type names = {"D", "E", nullptr, "F"}; + formatter() : EnumFormatter(names) {} +}; + +TEST(EnumUtil, Enum1) +{ + EXPECT_EQ(fmt::to_string(Enum1::A), "A (0)"); + EXPECT_EQ(fmt::to_string(Enum1::B), "B (1)"); + EXPECT_EQ(fmt::to_string(Enum1::C), "C (2)"); + EXPECT_EQ(fmt::to_string(static_cast<Enum1>(3)), "Invalid (3)"); + EXPECT_EQ(fmt::to_string(static_cast<Enum1>(4)), "Invalid (4)"); + + EXPECT_EQ(fmt::format("{:s}", Enum1::A), "0x0u /* A */"); + EXPECT_EQ(fmt::format("{:s}", Enum1::B), "0x1u /* B */"); + EXPECT_EQ(fmt::format("{:s}", Enum1::C), "0x2u /* C */"); + EXPECT_EQ(fmt::format("{:s}", static_cast<Enum1>(3)), "0x3u /* Invalid */"); + EXPECT_EQ(fmt::format("{:s}", static_cast<Enum1>(4)), "0x4u /* Invalid */"); +} + +TEST(EnumUtil, Enum2) +{ + EXPECT_EQ(fmt::to_string(Enum2::D), "D (0)"); + EXPECT_EQ(fmt::to_string(Enum2::E), "E (1)"); + EXPECT_EQ(fmt::to_string(static_cast<Enum2>(2)), "Invalid (2)"); + EXPECT_EQ(fmt::to_string(Enum2::F), "F (3)"); + EXPECT_EQ(fmt::to_string(static_cast<Enum2>(4)), "Invalid (4)"); + EXPECT_EQ(fmt::to_string(static_cast<Enum2>(-1)), "Invalid (-1)"); + + EXPECT_EQ(fmt::format("{:s}", Enum2::D), "0x0u /* D */"); + EXPECT_EQ(fmt::format("{:s}", Enum2::E), "0x1u /* E */"); + EXPECT_EQ(fmt::format("{:s}", static_cast<Enum2>(2)), "0x2u /* Invalid */"); + EXPECT_EQ(fmt::format("{:s}", Enum2::F), "0x3u /* F */"); + EXPECT_EQ(fmt::format("{:s}", static_cast<Enum2>(4)), "0x4u /* Invalid */"); + EXPECT_EQ(fmt::format("{:s}", static_cast<Enum2>(-1)), "0xffffffffu /* Invalid */"); +} diff --git a/Source/UnitTests/UnitTests.vcxproj b/Source/UnitTests/UnitTests.vcxproj index 230ac50412..d0b0c27fb6 100644 --- a/Source/UnitTests/UnitTests.vcxproj +++ b/Source/UnitTests/UnitTests.vcxproj @@ -49,6 +49,7 @@ <ClCompile Include="Common\BusyLoopTest.cpp" /> <ClCompile Include="Common\CommonFuncsTest.cpp" /> <ClCompile Include="Common\Crypto\EcTest.cpp" /> + <ClCompile Include="Common\EnumFormatterTest.cpp" /> <ClCompile Include="Common\EventTest.cpp" /> <ClCompile Include="Common\FixedSizeQueueTest.cpp" /> <ClCompile Include="Common\FlagTest.cpp" /> diff --git a/Source/UnitTests/VideoCommon/VertexLoaderTest.cpp b/Source/UnitTests/VideoCommon/VertexLoaderTest.cpp index 666476dfce..bb09b1608e 100644 --- a/Source/UnitTests/VideoCommon/VertexLoaderTest.cpp +++ b/Source/UnitTests/VideoCommon/VertexLoaderTest.cpp @@ -28,7 +28,7 @@ TEST(VertexLoaderUID, UniqueEnough) memset(&vat, 0, sizeof(vat)); uids.insert(VertexLoaderUID(vtx_desc, vat)); - vtx_desc.Hex = 0xFEDCBA9876543210ull; + vtx_desc.SetLegacyHex(0xFEDCBA9876543210ull); EXPECT_EQ(uids.end(), uids.find(VertexLoaderUID(vtx_desc, vat))); uids.insert(VertexLoaderUID(vtx_desc, vat)); @@ -106,29 +106,37 @@ protected: std::unique_ptr<VertexLoaderBase> m_loader; }; -class VertexLoaderParamTest : public VertexLoaderTest, - public ::testing::WithParamInterface<std::tuple<int, int, int, int>> +class VertexLoaderParamTest + : public VertexLoaderTest, + public ::testing::WithParamInterface< + std::tuple<VertexComponentFormat, ComponentFormat, CoordComponentCount, int>> { }; -INSTANTIATE_TEST_CASE_P(AllCombinations, VertexLoaderParamTest, - ::testing::Combine(::testing::Values(DIRECT, INDEX8, INDEX16), - ::testing::Values(FORMAT_UBYTE, FORMAT_BYTE, - FORMAT_USHORT, FORMAT_SHORT, - FORMAT_FLOAT), - ::testing::Values(0, 1), // elements - ::testing::Values(0, 1, 31) // frac - )); +INSTANTIATE_TEST_CASE_P( + AllCombinations, VertexLoaderParamTest, + ::testing::Combine( + ::testing::Values(VertexComponentFormat::Direct, VertexComponentFormat::Index8, + VertexComponentFormat::Index16), + ::testing::Values(ComponentFormat::UByte, ComponentFormat::Byte, ComponentFormat::UShort, + ComponentFormat::Short, ComponentFormat::Float), + ::testing::Values(CoordComponentCount::XY, CoordComponentCount::XYZ), + ::testing::Values(0, 1, 31) // frac + )); TEST_P(VertexLoaderParamTest, PositionAll) { - int addr, format, elements, frac; + VertexComponentFormat addr; + ComponentFormat format; + CoordComponentCount elements; + int frac; std::tie(addr, format, elements, frac) = GetParam(); - this->m_vtx_desc.Position = addr; + this->m_vtx_desc.low.Position = addr; this->m_vtx_attr.g0.PosFormat = format; this->m_vtx_attr.g0.PosElements = elements; this->m_vtx_attr.g0.PosFrac = frac; this->m_vtx_attr.g0.ByteDequant = true; - elements += 2; + const u32 elem_size = GetElementSize(format); + const u32 elem_count = elements == CoordComponentCount::XY ? 2 : 3; std::vector<float> values = { std::numeric_limits<float>::lowest(), @@ -153,38 +161,37 @@ TEST_P(VertexLoaderParamTest, PositionAll) ASSERT_EQ(0u, values.size() % 2); ASSERT_EQ(0u, values.size() % 3); - int count = (int)values.size() / elements; - u32 elem_size = 1 << (format / 2); - size_t input_size = elements * elem_size; - if (addr & MASK_INDEXED) + int count = (int)values.size() / elem_count; + size_t input_size = elem_count * elem_size; + if (IsIndexed(addr)) { - input_size = addr - 1; + input_size = addr == VertexComponentFormat::Index8 ? 1 : 2; for (int i = 0; i < count; i++) - if (addr == INDEX8) + if (addr == VertexComponentFormat::Index8) Input<u8>(i); else Input<u16>(i); VertexLoaderManager::cached_arraybases[ARRAY_POSITION] = m_src.GetPointer(); - g_main_cp_state.array_strides[ARRAY_POSITION] = elements * elem_size; + g_main_cp_state.array_strides[ARRAY_POSITION] = elem_count * elem_size; } - CreateAndCheckSizes(input_size, elements * sizeof(float)); + CreateAndCheckSizes(input_size, elem_count * sizeof(float)); for (float value : values) { switch (format) { - case FORMAT_UBYTE: + case ComponentFormat::UByte: Input((u8)value); break; - case FORMAT_BYTE: + case ComponentFormat::Byte: Input((s8)value); break; - case FORMAT_USHORT: + case ComponentFormat::UShort: Input((u16)value); break; - case FORMAT_SHORT: + case ComponentFormat::Short: Input((s16)value); break; - case FORMAT_FLOAT: + case ComponentFormat::Float: Input(value); break; } @@ -192,29 +199,29 @@ TEST_P(VertexLoaderParamTest, PositionAll) RunVertices(count); - float scale = 1.f / (1u << (format == FORMAT_FLOAT ? 0 : frac)); + float scale = 1.f / (1u << (format == ComponentFormat::Float ? 0 : frac)); for (auto iter = values.begin(); iter != values.end();) { float f, g; switch (format) { - case FORMAT_UBYTE: + case ComponentFormat::UByte: f = (u8)*iter++; g = (u8)*iter++; break; - case FORMAT_BYTE: + case ComponentFormat::Byte: f = (s8)*iter++; g = (s8)*iter++; break; - case FORMAT_USHORT: + case ComponentFormat::UShort: f = (u16)*iter++; g = (u16)*iter++; break; - case FORMAT_SHORT: + case ComponentFormat::Short: f = (s16)*iter++; g = (s16)*iter++; break; - case FORMAT_FLOAT: + case ComponentFormat::Float: f = *iter++; g = *iter++; break; @@ -228,8 +235,8 @@ TEST_P(VertexLoaderParamTest, PositionAll) TEST_F(VertexLoaderTest, PositionIndex16FloatXY) { - m_vtx_desc.Position = INDEX16; - m_vtx_attr.g0.PosFormat = FORMAT_FLOAT; + m_vtx_desc.low.Position = VertexComponentFormat::Index16; + m_vtx_attr.g0.PosFormat = ComponentFormat::Float; CreateAndCheckSizes(sizeof(u16), 2 * sizeof(float)); Input<u16>(1); Input<u16>(0); @@ -246,47 +253,49 @@ TEST_F(VertexLoaderTest, PositionIndex16FloatXY) } class VertexLoaderSpeedTest : public VertexLoaderTest, - public ::testing::WithParamInterface<std::tuple<int, int>> + public ::testing::WithParamInterface<std::tuple<ComponentFormat, int>> { }; -INSTANTIATE_TEST_CASE_P(FormatsAndElements, VertexLoaderSpeedTest, - ::testing::Combine(::testing::Values(FORMAT_UBYTE, FORMAT_BYTE, - FORMAT_USHORT, FORMAT_SHORT, - FORMAT_FLOAT), - ::testing::Values(0, 1) // elements - )); +INSTANTIATE_TEST_CASE_P( + FormatsAndElements, VertexLoaderSpeedTest, + ::testing::Combine(::testing::Values(ComponentFormat::UByte, ComponentFormat::Byte, + ComponentFormat::UShort, ComponentFormat::Short, + ComponentFormat::Float), + ::testing::Values(0, 1))); TEST_P(VertexLoaderSpeedTest, PositionDirectAll) { - int format, elements; - std::tie(format, elements) = GetParam(); - const char* map[] = {"u8", "s8", "u16", "s16", "float"}; - printf("format: %s, elements: %d\n", map[format], elements); - m_vtx_desc.Position = DIRECT; + ComponentFormat format; + int elements_i; + std::tie(format, elements_i) = GetParam(); + CoordComponentCount elements = static_cast<CoordComponentCount>(elements_i); + fmt::print("format: {}, elements: {}\n", format, elements); + const u32 elem_count = elements == CoordComponentCount::XY ? 2 : 3; + m_vtx_desc.low.Position = VertexComponentFormat::Direct; m_vtx_attr.g0.PosFormat = format; m_vtx_attr.g0.PosElements = elements; - elements += 2; - size_t elem_size = static_cast<size_t>(1) << (format / 2); - CreateAndCheckSizes(elements * elem_size, elements * sizeof(float)); + const size_t elem_size = GetElementSize(format); + CreateAndCheckSizes(elem_count * elem_size, elem_count * sizeof(float)); for (int i = 0; i < 1000; ++i) RunVertices(100000); } TEST_P(VertexLoaderSpeedTest, TexCoordSingleElement) { - int format, elements; - std::tie(format, elements) = GetParam(); - const char* map[] = {"u8", "s8", "u16", "s16", "float"}; - printf("format: %s, elements: %d\n", map[format], elements); - m_vtx_desc.Position = DIRECT; - m_vtx_attr.g0.PosFormat = FORMAT_BYTE; - m_vtx_desc.Tex0Coord = DIRECT; + ComponentFormat format; + int elements_i; + std::tie(format, elements_i) = GetParam(); + TexComponentCount elements = static_cast<TexComponentCount>(elements_i); + fmt::print("format: {}, elements: {}\n", format, elements); + const u32 elem_count = elements == TexComponentCount::S ? 1 : 2; + m_vtx_desc.low.Position = VertexComponentFormat::Direct; + m_vtx_attr.g0.PosFormat = ComponentFormat::Byte; + m_vtx_desc.high.Tex0Coord = VertexComponentFormat::Direct; m_vtx_attr.g0.Tex0CoordFormat = format; m_vtx_attr.g0.Tex0CoordElements = elements; - elements += 1; - size_t elem_size = static_cast<size_t>(1) << (format / 2); - CreateAndCheckSizes(2 * sizeof(s8) + elements * elem_size, - 2 * sizeof(float) + elements * sizeof(float)); + const size_t elem_size = GetElementSize(format); + CreateAndCheckSizes(2 * sizeof(s8) + elem_count * elem_size, + 2 * sizeof(float) + elem_count * sizeof(float)); for (int i = 0; i < 1000; ++i) RunVertices(100000); } @@ -294,52 +303,52 @@ TEST_P(VertexLoaderSpeedTest, TexCoordSingleElement) TEST_F(VertexLoaderTest, LargeFloatVertexSpeed) { // Enables most attributes in floating point indexed mode to test speed. - m_vtx_desc.PosMatIdx = 1; - m_vtx_desc.Tex0MatIdx = 1; - m_vtx_desc.Tex1MatIdx = 1; - m_vtx_desc.Tex2MatIdx = 1; - m_vtx_desc.Tex3MatIdx = 1; - m_vtx_desc.Tex4MatIdx = 1; - m_vtx_desc.Tex5MatIdx = 1; - m_vtx_desc.Tex6MatIdx = 1; - m_vtx_desc.Tex7MatIdx = 1; - m_vtx_desc.Position = INDEX16; - m_vtx_desc.Normal = INDEX16; - m_vtx_desc.Color0 = INDEX16; - m_vtx_desc.Color1 = INDEX16; - m_vtx_desc.Tex0Coord = INDEX16; - m_vtx_desc.Tex1Coord = INDEX16; - m_vtx_desc.Tex2Coord = INDEX16; - m_vtx_desc.Tex3Coord = INDEX16; - m_vtx_desc.Tex4Coord = INDEX16; - m_vtx_desc.Tex5Coord = INDEX16; - m_vtx_desc.Tex6Coord = INDEX16; - m_vtx_desc.Tex7Coord = INDEX16; - - m_vtx_attr.g0.PosElements = 1; // XYZ - m_vtx_attr.g0.PosFormat = FORMAT_FLOAT; - m_vtx_attr.g0.NormalElements = 1; // NBT - m_vtx_attr.g0.NormalFormat = FORMAT_FLOAT; - m_vtx_attr.g0.Color0Elements = 1; // Has Alpha - m_vtx_attr.g0.Color0Comp = FORMAT_32B_8888; - m_vtx_attr.g0.Color1Elements = 1; // Has Alpha - m_vtx_attr.g0.Color1Comp = FORMAT_32B_8888; - m_vtx_attr.g0.Tex0CoordElements = 1; // ST - m_vtx_attr.g0.Tex0CoordFormat = FORMAT_FLOAT; - m_vtx_attr.g1.Tex1CoordElements = 1; // ST - m_vtx_attr.g1.Tex1CoordFormat = FORMAT_FLOAT; - m_vtx_attr.g1.Tex2CoordElements = 1; // ST - m_vtx_attr.g1.Tex2CoordFormat = FORMAT_FLOAT; - m_vtx_attr.g1.Tex3CoordElements = 1; // ST - m_vtx_attr.g1.Tex3CoordFormat = FORMAT_FLOAT; - m_vtx_attr.g1.Tex4CoordElements = 1; // ST - m_vtx_attr.g1.Tex4CoordFormat = FORMAT_FLOAT; - m_vtx_attr.g2.Tex5CoordElements = 1; // ST - m_vtx_attr.g2.Tex5CoordFormat = FORMAT_FLOAT; - m_vtx_attr.g2.Tex6CoordElements = 1; // ST - m_vtx_attr.g2.Tex6CoordFormat = FORMAT_FLOAT; - m_vtx_attr.g2.Tex7CoordElements = 1; // ST - m_vtx_attr.g2.Tex7CoordFormat = FORMAT_FLOAT; + m_vtx_desc.low.PosMatIdx = 1; + m_vtx_desc.low.Tex0MatIdx = 1; + m_vtx_desc.low.Tex1MatIdx = 1; + m_vtx_desc.low.Tex2MatIdx = 1; + m_vtx_desc.low.Tex3MatIdx = 1; + m_vtx_desc.low.Tex4MatIdx = 1; + m_vtx_desc.low.Tex5MatIdx = 1; + m_vtx_desc.low.Tex6MatIdx = 1; + m_vtx_desc.low.Tex7MatIdx = 1; + m_vtx_desc.low.Position = VertexComponentFormat::Index16; + m_vtx_desc.low.Normal = VertexComponentFormat::Index16; + m_vtx_desc.low.Color0 = VertexComponentFormat::Index16; + m_vtx_desc.low.Color1 = VertexComponentFormat::Index16; + m_vtx_desc.high.Tex0Coord = VertexComponentFormat::Index16; + m_vtx_desc.high.Tex1Coord = VertexComponentFormat::Index16; + m_vtx_desc.high.Tex2Coord = VertexComponentFormat::Index16; + m_vtx_desc.high.Tex3Coord = VertexComponentFormat::Index16; + m_vtx_desc.high.Tex4Coord = VertexComponentFormat::Index16; + m_vtx_desc.high.Tex5Coord = VertexComponentFormat::Index16; + m_vtx_desc.high.Tex6Coord = VertexComponentFormat::Index16; + m_vtx_desc.high.Tex7Coord = VertexComponentFormat::Index16; + + m_vtx_attr.g0.PosElements = CoordComponentCount::XYZ; + m_vtx_attr.g0.PosFormat = ComponentFormat::Float; + m_vtx_attr.g0.NormalElements = NormalComponentCount::NBT; + m_vtx_attr.g0.NormalFormat = ComponentFormat::Float; + m_vtx_attr.g0.Color0Elements = ColorComponentCount::RGBA; + m_vtx_attr.g0.Color0Comp = ColorFormat::RGBA8888; + m_vtx_attr.g0.Color1Elements = ColorComponentCount::RGBA; + m_vtx_attr.g0.Color1Comp = ColorFormat::RGBA8888; + m_vtx_attr.g0.Tex0CoordElements = TexComponentCount::ST; + m_vtx_attr.g0.Tex0CoordFormat = ComponentFormat::Float; + m_vtx_attr.g1.Tex1CoordElements = TexComponentCount::ST; + m_vtx_attr.g1.Tex1CoordFormat = ComponentFormat::Float; + m_vtx_attr.g1.Tex2CoordElements = TexComponentCount::ST; + m_vtx_attr.g1.Tex2CoordFormat = ComponentFormat::Float; + m_vtx_attr.g1.Tex3CoordElements = TexComponentCount::ST; + m_vtx_attr.g1.Tex3CoordFormat = ComponentFormat::Float; + m_vtx_attr.g1.Tex4CoordElements = TexComponentCount::ST; + m_vtx_attr.g1.Tex4CoordFormat = ComponentFormat::Float; + m_vtx_attr.g2.Tex5CoordElements = TexComponentCount::ST; + m_vtx_attr.g2.Tex5CoordFormat = ComponentFormat::Float; + m_vtx_attr.g2.Tex6CoordElements = TexComponentCount::ST; + m_vtx_attr.g2.Tex6CoordFormat = ComponentFormat::Float; + m_vtx_attr.g2.Tex7CoordElements = TexComponentCount::ST; + m_vtx_attr.g2.Tex7CoordFormat = ComponentFormat::Float; CreateAndCheckSizes(33, 156); |
