From 49ae99ffc9dd11695485d6f2fedc805080e5ff06 Mon Sep 17 00:00:00 2001 From: Tillmann Karras Date: Thu, 12 Mar 2015 11:27:22 +0100 Subject: [PATCH] UnitTests: rewrite vertex loader tests The position attribute now has complete coverage. --- .../VideoCommon/VertexLoaderTest.cpp | 375 ++++++++++-------- 1 file changed, 204 insertions(+), 171 deletions(-) diff --git a/Source/UnitTests/VideoCommon/VertexLoaderTest.cpp b/Source/UnitTests/VideoCommon/VertexLoaderTest.cpp index 966b037312..d3a5bc7b82 100644 --- a/Source/UnitTests/VideoCommon/VertexLoaderTest.cpp +++ b/Source/UnitTests/VideoCommon/VertexLoaderTest.cpp @@ -1,13 +1,18 @@ +#include +#include +#include +#include #include -#include "Common/Common.h" -#include "VideoCommon/DataReader.h" -#include "VideoCommon/VertexLoaderBase.h" - -// Needs to be included later because it defines a TEST macro that conflicts -// with a TEST method definition in x64Emitter.h. #include // NOLINT +#include "Common/Common.h" +#include "Common/MathUtil.h" +#include "VideoCommon/CPMemory.h" +#include "VideoCommon/DataReader.h" +#include "VideoCommon/OpcodeDecoding.h" +#include "VideoCommon/VertexLoaderBase.h" + TEST(VertexLoaderUID, UniqueEnough) { std::unordered_set uids; @@ -38,181 +43,209 @@ protected: void SetUp() override { - memset(&input_memory[0], 0, sizeof(input_memory)); - memset(&output_memory[0], 0, sizeof(input_memory)); + memset(input_memory, 0, sizeof(input_memory)); + memset(output_memory, 0xFF, sizeof(input_memory)); memset(&m_vtx_desc, 0, sizeof(m_vtx_desc)); memset(&m_vtx_attr, 0, sizeof(m_vtx_attr)); + m_loader = nullptr; + ResetPointers(); } - // Pushes a value to the input stream. + void CreateAndCheckSizes(size_t input_size, size_t output_size = 3 * sizeof(float)) + { + m_loader.reset(VertexLoaderBase::CreateVertexLoader(m_vtx_desc, m_vtx_attr)); + ASSERT_EQ((int)input_size, m_loader->m_VertexSize); + ASSERT_EQ((int)output_size, m_loader->m_native_vtx_decl.stride); + } + template void Input(T val) { - // Converts *to* big endian, not from. - *(T*)(&input_memory[m_input_pos]) = Common::FromBigEndian(val); - m_input_pos += sizeof(val); + // Write swapped. + m_src.Write(val); } - // Reads a value from the output stream. - template - T Output() + void ExpectOut(float val) { - T out = *(T*)&output_memory[m_output_pos]; - m_output_pos += sizeof(out); - return out; + // Read unswapped. + MathUtil::IntFloat expected(val), actual(m_dst.Read()); + if (!actual.f || actual.f != actual.f) + EXPECT_EQ(expected.i, actual.i); + else + EXPECT_EQ(expected.f, actual.f); } - // Combination of EXPECT_EQ and Output. - template - void ExpectOut(T val) + void RunVertices(int count, int expected_count = -1) { - EXPECT_EQ(val, Output()); + if (expected_count == -1) + expected_count = count; + ResetPointers(); + int actual_count = m_loader->RunVertices(m_src, m_dst, count, GX_DRAW_POINTS); + EXPECT_EQ(actual_count, expected_count); } void ResetPointers() { - m_input_pos = m_output_pos = 0; - src = DataReader(input_memory, input_memory + sizeof(input_memory)); - dst = DataReader(output_memory, output_memory + sizeof(output_memory)); + m_src = DataReader(input_memory, input_memory + sizeof(input_memory)); + m_dst = DataReader(output_memory, output_memory + sizeof(output_memory)); } - u32 m_input_pos, m_output_pos; - DataReader src; - DataReader dst; + DataReader m_src; + DataReader m_dst; TVtxDesc m_vtx_desc; VAT m_vtx_attr; + std::unique_ptr m_loader; }; -TEST_F(VertexLoaderTest, PositionDirectFloatXYZ) +class VertexLoaderParamTest : public VertexLoaderTest, public ::testing::WithParamInterface> {}; +extern int gtest_AllCombinationsVertexLoaderParamTest_dummy_; +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 + ) +); + +TEST_P(VertexLoaderParamTest, PositionAll) { - m_vtx_desc.Position = 1; // Direct - m_vtx_attr.g0.PosElements = 1; // XYZ - m_vtx_attr.g0.PosFormat = 4; // Float + int addr, format, elements, frac; + std::tie(addr, format, elements, frac) = GetParam(); + this->m_vtx_desc.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; - VertexLoaderBase* loader = VertexLoaderBase::CreateVertexLoader(m_vtx_desc, m_vtx_attr); + std::vector values = { + std::numeric_limits::lowest(), + std::numeric_limits::denorm_min(), + std::numeric_limits::min(), + std::numeric_limits::max(), + std::numeric_limits::quiet_NaN(), + std::numeric_limits::infinity(), + -0x8000, -0x80, -1, -0, 0, 1, 123, 0x7F, 0xFF, 0x7FFF, 0xFFFF, 12345678, + }; + ASSERT_EQ(0u, values.size() % 2); + ASSERT_EQ(0u, values.size() % 3); - ASSERT_EQ(3 * sizeof(float), (u32)loader->m_native_vtx_decl.stride); - ASSERT_EQ(3 * sizeof(float), (u32)loader->m_VertexSize); - - // Write some vertices. - Input(0.0f); Input(0.0f); Input(0.0f); - Input(1.0f); Input(0.0f); Input(0.0f); - Input(0.0f); Input(1.0f); Input(0.0f); - Input(0.0f); Input(0.0f); Input(1.0f); - - // Convert 4 points. "7" -> primitive are points. - int count = loader->RunVertices(src, dst, 4, 7); - src.Skip(4 * loader->m_VertexSize); - dst.Skip(count * loader->m_native_vtx_decl.stride); - delete loader; - - ExpectOut(0.0f); ExpectOut(0.0f); ExpectOut(0.0f); - ExpectOut(1.0f); ExpectOut(0.0f); ExpectOut(0.0f); - ExpectOut(0.0f); ExpectOut(1.0f); ExpectOut(0.0f); - ExpectOut(0.0f); ExpectOut(0.0f); ExpectOut(1.0f); - - // Test that scale does nothing for floating point inputs. - Input(1.0f); Input(2.0f); Input(4.0f); - m_vtx_attr.g0.PosFrac = 1; - loader = VertexLoaderBase::CreateVertexLoader(m_vtx_desc, m_vtx_attr); - count = loader->RunVertices(src, dst, 1, 7); - src.Skip(1 * loader->m_VertexSize); - dst.Skip(count * loader->m_native_vtx_decl.stride); - ExpectOut(1.0f); ExpectOut(2.0f); ExpectOut(4.0f); - delete loader; -} - -TEST_F(VertexLoaderTest, PositionDirectU16XY) -{ - m_vtx_desc.Position = 1; // Direct - m_vtx_attr.g0.PosElements = 0; // XY - m_vtx_attr.g0.PosFormat = 2; // U16 - - VertexLoaderBase* loader = VertexLoaderBase::CreateVertexLoader(m_vtx_desc, m_vtx_attr); - - ASSERT_EQ(3 * sizeof(float), (u32)loader->m_native_vtx_decl.stride); - ASSERT_EQ(2 * sizeof(u16), (u32)loader->m_VertexSize); - - // Write some vertices. - Input(0); Input(0); - Input(1); Input(2); - Input(256); Input(257); - Input(65535); Input(65534); - Input(12345); Input(54321); - - // Convert 5 points. "7" -> primitive are points. - int count = loader->RunVertices(src, dst, 5, 7); - src.Skip(5 * loader->m_VertexSize); - dst.Skip(count * loader->m_native_vtx_decl.stride); - delete loader; - - ExpectOut(0.0f); ExpectOut(0.0f); ExpectOut(0.0f); - ExpectOut(1.0f); ExpectOut(2.0f); ExpectOut(0.0f); - ExpectOut(256.0f); ExpectOut(257.0f); ExpectOut(0.0f); - ExpectOut(65535.0f); ExpectOut(65534.0f); ExpectOut(0.0f); - ExpectOut(12345.0f); ExpectOut(54321.0f); ExpectOut(0.0f); - - // Test that scale works on U16 inputs. - Input(42); Input(24); - m_vtx_attr.g0.PosFrac = 1; - m_vtx_attr.g0.ByteDequant = 1; - loader = VertexLoaderBase::CreateVertexLoader(m_vtx_desc, m_vtx_attr); - count = loader->RunVertices(src, dst, 1, 7); - src.Skip(1 * loader->m_VertexSize); - dst.Skip(count * loader->m_native_vtx_decl.stride); - ExpectOut(21.0f); ExpectOut(12.0f); ExpectOut(0.0f); - delete loader; -} - -TEST_F(VertexLoaderTest, PositionDirectFloatXYZSpeed) -{ - m_vtx_desc.Position = 1; // Direct - m_vtx_attr.g0.PosElements = 1; // XYZ - m_vtx_attr.g0.PosFormat = 4; // Float - - VertexLoaderBase* loader = VertexLoaderBase::CreateVertexLoader(m_vtx_desc, m_vtx_attr); - - ASSERT_EQ(3 * sizeof(float), (u32)loader->m_native_vtx_decl.stride); - ASSERT_EQ(3 * sizeof(float), (u32)loader->m_VertexSize); - - for (int i = 0; i < 1000; ++i) + int count = (int)values.size() / elements; + u32 elem_size = 1 << (format / 2); + size_t input_size = elements * elem_size; + if (addr & MASK_INDEXED) { - ResetPointers(); - int count = loader->RunVertices(src, dst, 100000, 7); - src.Skip(100000 * loader->m_VertexSize); - dst.Skip(count * loader->m_native_vtx_decl.stride); + input_size = addr - 1; + for (int i = 0; i < count; i++) + if (addr == INDEX8) + Input(i); + else + Input(i); + cached_arraybases[ARRAY_POSITION] = m_src.GetPointer(); + g_main_cp_state.array_strides[ARRAY_POSITION] = elements * elem_size; + } + CreateAndCheckSizes(input_size); + for (float value : values) + { + switch (format) + { + case FORMAT_UBYTE: Input((u8)value); break; + case FORMAT_BYTE: Input((s8)value); break; + case FORMAT_USHORT: Input((u16)value); break; + case FORMAT_SHORT: Input((s16)value); break; + case FORMAT_FLOAT: Input(value); break; + } + } + + RunVertices(count); + + float scale = 1.f / (1u << (format == FORMAT_FLOAT ? 0 : frac)); + for (auto iter = values.begin(); iter != values.end();) + { + float f, g; + switch (format) + { + case FORMAT_UBYTE: f = (u8)*iter++; g = (u8)*iter++; break; + case FORMAT_BYTE: f = (s8)*iter++; g = (s8)*iter++; break; + case FORMAT_USHORT: f = (u16)*iter++; g = (u16)*iter++; break; + case FORMAT_SHORT: f = (s16)*iter++; g = (s16)*iter++; break; + case FORMAT_FLOAT: f = *iter++; g = *iter++; break; + } + ExpectOut(f * scale); + ExpectOut(g * scale); + if (elements == 2) + m_dst.Skip(sizeof(float)); } - delete loader; } -TEST_F(VertexLoaderTest, PositionDirectU16XYSpeed) +TEST_F(VertexLoaderTest, PositionIndex16FloatXY) { - m_vtx_desc.Position = 1; // Direct - m_vtx_attr.g0.PosElements = 0; // XY - m_vtx_attr.g0.PosFormat = 2; // U16 + m_vtx_desc.Position = INDEX16; + m_vtx_attr.g0.PosFormat = FORMAT_FLOAT; + CreateAndCheckSizes(sizeof(u16)); + Input(1); Input(0); + cached_arraybases[ARRAY_POSITION] = m_src.GetPointer(); + g_main_cp_state.array_strides[ARRAY_POSITION] = sizeof(float); // ;) + Input(1.f); Input(2.f); Input(3.f); + RunVertices(2); + ExpectOut(2); ExpectOut(3); m_dst.Skip(sizeof(float)); + ExpectOut(1); ExpectOut(2); m_dst.Skip(sizeof(float)); +} - VertexLoaderBase* loader = VertexLoaderBase::CreateVertexLoader(m_vtx_desc, m_vtx_attr); - - ASSERT_EQ(3 * sizeof(float), (u32)loader->m_native_vtx_decl.stride); - ASSERT_EQ(2 * sizeof(u16), (u32)loader->m_VertexSize); +class VertexLoaderSpeedTest : public VertexLoaderTest, public ::testing::WithParamInterface> {}; +extern int gtest_FormatsAndElementsVertexLoaderSpeedTest_dummy_; +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 + ) +); +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; + m_vtx_attr.g0.PosFormat = format; + m_vtx_attr.g0.PosElements = elements; + elements += 2; + size_t elem_size = 1 << (format / 2); + CreateAndCheckSizes(elements * elem_size); for (int i = 0; i < 1000; ++i) - { - ResetPointers(); - int count = loader->RunVertices(src, dst, 100000, 7); - src.Skip(100000 * loader->m_VertexSize); - dst.Skip(count * loader->m_native_vtx_decl.stride); - } - delete loader; + 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; + m_vtx_attr.g0.Tex0CoordFormat = format; + m_vtx_attr.g0.Tex0CoordElements = elements; + elements += 1; + size_t elem_size = 1 << (format / 2); + CreateAndCheckSizes(2 * sizeof(s8) + elements * elem_size, + 2 * sizeof(float) + elements * sizeof(float)); + for (int i = 0; i < 1000; ++i) + RunVertices(100000); } TEST_F(VertexLoaderTest, LargeFloatVertexSpeed) { - // Enables most attributes in floating point direct mode to test speed. + // 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; @@ -222,54 +255,54 @@ TEST_F(VertexLoaderTest, LargeFloatVertexSpeed) m_vtx_desc.Tex5MatIdx = 1; m_vtx_desc.Tex6MatIdx = 1; m_vtx_desc.Tex7MatIdx = 1; - m_vtx_desc.Position = 1; - m_vtx_desc.Normal = 1; - m_vtx_desc.Color0 = 1; - m_vtx_desc.Color1 = 1; - m_vtx_desc.Tex0Coord = 1; - m_vtx_desc.Tex1Coord = 1; - m_vtx_desc.Tex2Coord = 1; - m_vtx_desc.Tex3Coord = 1; - m_vtx_desc.Tex4Coord = 1; - m_vtx_desc.Tex5Coord = 1; - m_vtx_desc.Tex6Coord = 1; - m_vtx_desc.Tex7Coord = 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 = 4; // Float + m_vtx_attr.g0.PosFormat = FORMAT_FLOAT; m_vtx_attr.g0.NormalElements = 1; // NBT - m_vtx_attr.g0.NormalFormat = 4; // Float + m_vtx_attr.g0.NormalFormat = FORMAT_FLOAT; m_vtx_attr.g0.Color0Elements = 1; // Has Alpha - m_vtx_attr.g0.Color0Comp = 5; // RGBA8888 + m_vtx_attr.g0.Color0Comp = FORMAT_32B_8888; m_vtx_attr.g0.Color1Elements = 1; // Has Alpha - m_vtx_attr.g0.Color1Comp = 5; // RGBA8888 + m_vtx_attr.g0.Color1Comp = FORMAT_32B_8888; m_vtx_attr.g0.Tex0CoordElements = 1; // ST - m_vtx_attr.g0.Tex0CoordFormat = 4; // Float + m_vtx_attr.g0.Tex0CoordFormat = FORMAT_FLOAT; m_vtx_attr.g1.Tex1CoordElements = 1; // ST - m_vtx_attr.g1.Tex1CoordFormat = 4; // Float + m_vtx_attr.g1.Tex1CoordFormat = FORMAT_FLOAT; m_vtx_attr.g1.Tex2CoordElements = 1; // ST - m_vtx_attr.g1.Tex2CoordFormat = 4; // Float + m_vtx_attr.g1.Tex2CoordFormat = FORMAT_FLOAT; m_vtx_attr.g1.Tex3CoordElements = 1; // ST - m_vtx_attr.g1.Tex3CoordFormat = 4; // Float + m_vtx_attr.g1.Tex3CoordFormat = FORMAT_FLOAT; m_vtx_attr.g1.Tex4CoordElements = 1; // ST - m_vtx_attr.g1.Tex4CoordFormat = 4; // Float + m_vtx_attr.g1.Tex4CoordFormat = FORMAT_FLOAT; m_vtx_attr.g2.Tex5CoordElements = 1; // ST - m_vtx_attr.g2.Tex5CoordFormat = 4; // Float + m_vtx_attr.g2.Tex5CoordFormat = FORMAT_FLOAT; m_vtx_attr.g2.Tex6CoordElements = 1; // ST - m_vtx_attr.g2.Tex6CoordFormat = 4; // Float + m_vtx_attr.g2.Tex6CoordFormat = FORMAT_FLOAT; m_vtx_attr.g2.Tex7CoordElements = 1; // ST - m_vtx_attr.g2.Tex7CoordFormat = 4; // Float + m_vtx_attr.g2.Tex7CoordFormat = FORMAT_FLOAT; - VertexLoaderBase* loader = VertexLoaderBase::CreateVertexLoader(m_vtx_desc, m_vtx_attr); + CreateAndCheckSizes(33, 156); + + for (int i = 0; i < 16; i++) + { + cached_arraybases[i] = m_src.GetPointer(); + g_main_cp_state.array_strides[i] = 129; + } // This test is only done 100x in a row since it's ~20x slower using the // current vertex loader implementation. for (int i = 0; i < 100; ++i) - { - ResetPointers(); - int count = loader->RunVertices(src, dst, 100000, 7); - src.Skip(100000 * loader->m_VertexSize); - dst.Skip(count * loader->m_native_vtx_decl.stride); - } - delete loader; + RunVertices(100000); }