diff --git a/CMakeLists.txt b/CMakeLists.txt index 47a3488..72e991c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -312,6 +312,8 @@ add_executable( src/tests/smoothing_tests.h src/tests/stencil_tests.cpp src/tests/stencil_tests.h + src/tests/stipple_tests.cpp + src/tests/stipple_tests.h src/tests/surface_clip_tests.cpp src/tests/surface_clip_tests.h src/tests/surface_pitch_tests.cpp diff --git a/src/main.cpp b/src/main.cpp index 893ceab..e6f19b0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -60,6 +60,7 @@ #include "tests/shade_model_tests.h" #include "tests/smoothing_tests.h" #include "tests/stencil_tests.h" +#include "tests/stipple_tests.h" #include "tests/surface_clip_tests.h" #include "tests/surface_pitch_tests.h" #include "tests/texgen_matrix_tests.h" @@ -581,6 +582,10 @@ static void register_suites(TestHost& host, std::vector(host, output_directory); test_suites.push_back(suite); } + { + auto suite = std::make_shared(host, output_directory); + test_suites.push_back(suite); + } { auto suite = std::make_shared(host, output_directory); test_suites.push_back(suite); diff --git a/src/nxdk_ext.h b/src/nxdk_ext.h index e8a1062..ae52db3 100644 --- a/src/nxdk_ext.h +++ b/src/nxdk_ext.h @@ -227,6 +227,11 @@ #define NV097_SET_EDGE_FLAG NV20_TCL_PRIMITIVE_3D_EDGE_FLAG +#define NV097_SET_STIPPLE_ENABLE NV20_TCL_PRIMITIVE_3D_POLYGON_STIPPLE_ENABLE +// 32×32 bit stipple pattern +#define NV097_SET_STIPPLE_PATERN 0x1480 // NV20_TCL_PRIMITIVE_3D_POLYGON_STIPPLE_PATTERN +#define NV097_SET_STIPPLE_PATERN_SIZE NV20_TCL_PRIMITIVE_3D_POLYGON_STIPPLE_PATTERN__SIZE + // NV_PVIDEO_INTR is already defined in outer.h #define NV_PVIDEO_INTR_BUFFER_0 (1 << 0) #define NV_PVIDEO_INTR_BUFFER_1 (1 << 4) diff --git a/src/pbkit_ext.cpp b/src/pbkit_ext.cpp index a6c93fe..80a4287 100644 --- a/src/pbkit_ext.cpp +++ b/src/pbkit_ext.cpp @@ -196,6 +196,42 @@ uint32_t *pb_push3f(uint32_t *p, DWORD command, float param1, float param2, floa return p + 4; } +uint32_t *pb_push2fv(uint32_t *p, DWORD command, const float *vector2) { + pb_push_to(SUBCH_3D, p, command, 2); + *((float *)(p + 1)) = *vector2++; + *((float *)(p + 2)) = *vector2++; + return p + 3; +} + +uint32_t *pb_push3fv(uint32_t *p, DWORD command, const float *vector3) { + pb_push_to(SUBCH_3D, p, command, 3); + *((float *)(p + 1)) = *vector3++; + *((float *)(p + 2)) = *vector3++; + *((float *)(p + 3)) = *vector3++; + return p + 4; +} + +uint32_t *pb_push4fv(uint32_t *p, DWORD command, const float *vector4) { + pb_push_to(SUBCH_3D, p, command, 4); + *((float *)(p + 1)) = *vector4++; + *((float *)(p + 2)) = *vector4++; + *((float *)(p + 3)) = *vector4++; + *((float *)(p + 4)) = *vector4++; + return p + 5; +} + +uint32_t *pb_push2v(uint32_t *p, DWORD command, const uint32_t *vector2) { + return pb_push2(p, command, vector2[0], vector2[1]); +} + +uint32_t *pb_push3v(uint32_t *p, DWORD command, const uint32_t *vector3) { + return pb_push3(p, command, vector3[0], vector3[1], vector3[2]); +} + +uint32_t *pb_push4v(uint32_t *p, DWORD command, const uint32_t *vector4) { + return pb_push4(p, command, vector4[0], vector4[1], vector4[2], vector4[3]); +} + void pb_fetch_pgraph_registers(uint8_t *registers) { // See https://github.com/XboxDev/nv2a-trace/blob/65bdd2369a5b216cfc47c9545f870c49d118276b/Trace.py#L32 diff --git a/src/pbkit_ext.h b/src/pbkit_ext.h index 6cfe89e..d5c0d7d 100644 --- a/src/pbkit_ext.h +++ b/src/pbkit_ext.h @@ -82,6 +82,14 @@ uint32_t* pb_push1f(uint32_t* p, DWORD command, float param1); uint32_t* pb_push2f(uint32_t* p, DWORD command, float param1, float param2); uint32_t* pb_push3f(uint32_t* p, DWORD command, float param1, float param2, float param3); +uint32_t* pb_push2fv(uint32_t* p, DWORD command, const float* vector2); +uint32_t* pb_push3fv(uint32_t* p, DWORD command, const float* vector3); +uint32_t* pb_push4fv(uint32_t* p, DWORD command, const float* vector4); + +uint32_t* pb_push2v(uint32_t* p, DWORD command, const uint32_t* vector2); +uint32_t* pb_push3v(uint32_t* p, DWORD command, const uint32_t* vector3); +uint32_t* pb_push4v(uint32_t* p, DWORD command, const uint32_t* vector4); + // Versions of pb_print that use a full-featured printf implementation instead of the PCDLIB one that does not yet // support floats. void pb_print_with_floats(const char* format, ...); diff --git a/src/tests/stipple_tests.cpp b/src/tests/stipple_tests.cpp new file mode 100644 index 0000000..e626b7b --- /dev/null +++ b/src/tests/stipple_tests.cpp @@ -0,0 +1,239 @@ +#include "stipple_tests.h" + +#include + +#include +#include + +#include "debug_output.h" +#include "pbkit_ext.h" +#include "shaders/precalculated_vertex_shader.h" +#include "vertex_buffer.h" + +static std::string MakeTestName(bool stipple_enabled, const std::string &stipple_pattern_name) { + return stipple_pattern_name + "_" + (stipple_enabled ? "On" : "Off"); +} + +static std::map> kStipplePatterns = { + {"Zero", {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"Border", + { + 0x00000000, 0x7FFFFFFE, 0x7FFFFFFE, 0x7FFFFFFE, 0x7FFFFFFE, 0x7FFFFFFE, 0x7FFFFFFE, 0x7FFFFFFE, + 0x7FFFFFFE, 0x7FFFFFFE, 0x7FFFFFFE, 0x7FFFFFFE, 0x7FFFFFFE, 0x7FFFFFFE, 0x7FFFFFFE, 0x7FFFFFFE, + 0x7FFFFFFE, 0x7FFFFFFE, 0x7FFFFFFE, 0x7FFFFFFE, 0x7FFFFFFE, 0x7FFFFFFE, 0x7FFFFFFE, 0x7FFFFFFE, + 0x7FFFFFFE, 0x7FFFFFFE, 0x7FFFFFFE, 0x7FFFFFFE, 0x7FFFFFFE, 0x7FFFFFFE, 0x7FFFFFFE, 0x00000000, + }}, + {"Alternating", + { + 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, + 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, + 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, + 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, + }}, + {"Checkered", + { + 0x0F0F0F0F, 0x0F0F0F0F, 0x0F0F0F0F, 0x0F0F0F0F, 0xF0F0F0F0, 0xF0F0F0F0, 0xF0F0F0F0, 0xF0F0F0F0, + 0x0F0F0F0F, 0x0F0F0F0F, 0x0F0F0F0F, 0x0F0F0F0F, 0xF0F0F0F0, 0xF0F0F0F0, 0xF0F0F0F0, 0xF0F0F0F0, + 0x0F0F0F0F, 0x0F0F0F0F, 0x0F0F0F0F, 0x0F0F0F0F, 0xF0F0F0F0, 0xF0F0F0F0, 0xF0F0F0F0, 0xF0F0F0F0, + 0x0F0F0F0F, 0x0F0F0F0F, 0x0F0F0F0F, 0x0F0F0F0F, 0xF0F0F0F0, 0xF0F0F0F0, 0xF0F0F0F0, 0xF0F0F0F0, + }}, +}; + +StippleTests::StippleTests(TestHost &host, std::string output_dir) + : TestSuite(host, std::move(output_dir), "Stipple tests") { + // Stipple patterns only take effect when enabled so there's just one test with stipple disabled. + { + const char *kPatternName = "Checkered"; + const auto &pattern = kStipplePatterns["Checkered"]; + const std::string test_name = MakeTestName(false, kPatternName); + tests_[test_name] = [this, test_name, pattern]() { Test(test_name, false, pattern); }; + } + + for (const auto &entry : kStipplePatterns) { + const std::string test_name = MakeTestName(true, entry.first); + const auto &pattern = entry.second; + tests_[test_name] = [this, test_name, pattern]() { Test(test_name, true, pattern); }; + } +} + +void StippleTests::Initialize() { + TestSuite::Initialize(); + + auto shader = std::make_shared(); + host_.SetVertexShaderProgram(shader); + + auto p = pb_begin(); + p = pb_push1(p, NV097_SET_DEPTH_TEST_ENABLE, false); + pb_end(p); + host_.SetBlend(true); +} + +void StippleTests::TearDownTest() { + auto p = pb_begin(); + p = pb_push1(p, NV097_SET_STIPPLE_ENABLE, false); + pb_end(p); +} + +static constexpr uint32_t kPalette[] = { + 0xFFFF3333, 0xFF33FF33, 0xFF3333FF, 0xFFFFFF33, 0xFF33FFFF, 0xFFFF33FF, 0xFF808080, 0xFFFFFFFF, +}; +static constexpr uint32_t kNumPaletteEntries = sizeof(kPalette) / sizeof(kPalette[0]); + +static void Draw(TestHost &host, float x, float y) { + { + // clang format off + constexpr float kVertices[][2] = { + {19.000000f, 15.000000f}, {56.000000f, 29.000000f}, {74.000000f, 77.000000f}, {45.694406f, 101.330152f}, + {36.000000f, 38.369815f}, {14.000000f, 23.000000f}, {85.000000f, 98.000000f}, {31.000000f, 40.000000f}, + {98.000000f, 38.000000f}, {17.000000f, 104.000000f}, {78.000000f, 65.000000f}, {31.644774f, 49.000000f}, + {105.000000f, 11.968842f}, {9.000000f, 40.000000f}, {105.962647f, 76.068491f}, {80.000000f, 87.000000f}, + }; + // clang format on + host.Begin(TestHost::PRIMITIVE_POINTS); + uint32_t i = 0; + for (auto pt : kVertices) { + host.SetDiffuse(kPalette[i]); + host.SetVertex(x + pt[0], y + pt[1], 1.0f); + i = (i + 1) % kNumPaletteEntries; + } + host.End(); + } + + { + // clang format off + constexpr float kVertices[][2] = { + {196.000000f, 47.000000f}, {194.000000f, 19.000000f}, {182.000000f, 97.000000f}, {106.186250f, 80.000000f}, + {127.000000f, 94.581630f}, {133.597443f, 13.763389f}, {205.000000f, 62.000000f}, {115.543920f, 16.000000f}, + {122.000000f, 3.000000f}, {117.062706f, 87.735558f}, {201.000000f, 28.664894f}, {146.114091f, 10.000000f}, + {205.356591f, 88.000000f}, {125.000000f, 107.000000f}, {190.828953f, 35.510304f}, {163.000000f, 105.956814f}, + }; + // clang format on + host.Begin(TestHost::PRIMITIVE_LINE_LOOP); + uint32_t i = 0; + for (auto pt : kVertices) { + host.SetDiffuse(kPalette[i]); + host.SetVertex(x + pt[0], y + pt[1], 1.0f); + i = (i + 1) % kNumPaletteEntries; + } + host.End(); + } + + { + // clang format off + constexpr float kVertices[][2] = { + {265.185925f, 35.000000f}, {249.778838f, 22.523717f}, {310.000000f, 19.000000f}, + {243.978231f, 47.650185f}, {304.000000f, 37.000000f}, {232.000000f, 102.522615f}, + }; + // clang format on + host.Begin(TestHost::PRIMITIVE_TRIANGLES); + uint32_t i = 0; + for (auto pt : kVertices) { + host.SetDiffuse(kPalette[i]); + host.SetVertex(x + pt[0], y + pt[1], 1.0f); + i = (i + 1) % kNumPaletteEntries; + } + host.End(); + } + + { + // clang format off + constexpr float kVertices[][2] = { + {0.000000f, 239.000000f}, {0.000000f, 120.000000f}, {52.500000f, 225.400000f}, + {54.750000f, 175.500000f}, {105.000000f, 239.000000f}, {105.000000f, 120.000000f}, + }; + // clang format on + host.Begin(TestHost::PRIMITIVE_QUAD_STRIP); + uint32_t i = 0; + for (auto pt : kVertices) { + host.SetDiffuse(kPalette[i]); + host.SetVertex(x + pt[0], y + pt[1], 1.0f); + i = (i + 1) % kNumPaletteEntries; + } + host.End(); + } + + { + // clang format off + constexpr float kVertices[][2] = { + {158.500000f, 205.000000f}, {115.545455f, 239.000000f}, {120.318182f, 120.000000f}, {147.045455f, 157.400000f}, + {158.500000f, 128.500000f}, {166.136364f, 145.500000f}, {204.318182f, 159.100000f}, {209.750000f, 239.000000f}, + }; + // clang format on + host.Begin(TestHost::PRIMITIVE_TRIANGLE_FAN); + uint32_t i = 0; + for (auto pt : kVertices) { + host.SetDiffuse(kPalette[i]); + host.SetVertex(x + pt[0], y + pt[1], 1.0f); + i = (i + 1) % kNumPaletteEntries; + } + host.End(); + } + + { + // clang format off + constexpr float kVertices[][2] = { + {218.000000f, 232.000000f}, {237.772727f, 142.100000f}, {258.772727f, 120.000000f}, + {302.681818f, 169.300000f}, {317.000000f, 230.500000f}, + }; + // clang format on + host.Begin(TestHost::PRIMITIVE_POLYGON); + uint32_t i = 0; + for (auto pt : kVertices) { + host.SetDiffuse(kPalette[i]); + host.SetVertex(x + pt[0], y + pt[1], 1.0f); + i = (i + 1) % kNumPaletteEntries; + } + host.End(); + } + + { + // clang format off + constexpr float kVertices[][2] = { + {0.f, 350.f}, + {60.f, 340.f}, + {58.5f, 425.4f}, + {12.75f, 407.5f}, + }; + // clang format on + host.Begin(TestHost::PRIMITIVE_QUADS); + uint32_t i = 0; + for (auto pt : kVertices) { + host.SetDiffuse(kPalette[i]); + host.SetVertex(x + pt[0], y + pt[1], 1.0f); + i = (i + 1) % kNumPaletteEntries; + } + host.End(); + } +} + +void StippleTests::Test(const std::string &name, bool stipple_enable, const std::vector &stipple_pattern) { + host_.PrepareDraw(0xFF202224); + + { + auto p = pb_begin(); + p = pb_push1(p, NV097_SET_STIPPLE_ENABLE, stipple_enable); + for (auto i = 0; i < NV097_SET_STIPPLE_PATERN_SIZE; i += 4) { + p = pb_push4v(p, NV097_SET_STIPPLE_PATERN + (i * 4), stipple_pattern.data() + i); + } + pb_end(p); + } + + const float x = host_.GetFramebufferWidthF() * 0.25f; + const float y = host_.GetFramebufferHeightF() * 0.10f; + Draw(host_, x, y); + + pb_print("%s\n", name.c_str()); + pb_printat(0, 15, (char *)"Pts"); + pb_printat(0, 28, (char *)"LLoop"); + pb_printat(0, 40, (char *)"Tri"); + + pb_printat(11, 15, (char *)"QStrip"); + pb_printat(11, 28, (char *)"TFan"); + pb_printat(11, 39, (char *)"Poly"); + + pb_printat(13, 15, (char *)"Quad"); + + pb_draw_text_screen(); + + host_.FinishDraw(allow_saving_, output_dir_, name); +} diff --git a/src/tests/stipple_tests.h b/src/tests/stipple_tests.h new file mode 100644 index 0000000..2c72b82 --- /dev/null +++ b/src/tests/stipple_tests.h @@ -0,0 +1,25 @@ +#ifndef NXDK_PGRAPH_TESTS_STIPPLE_TESTS_H +#define NXDK_PGRAPH_TESTS_STIPPLE_TESTS_H + +#include +#include + +#include "test_host.h" +#include "test_suite.h" + +class TestHost; +class VertexBuffer; + +// Tests behavior of 0x147C - 3D_POLYGON_STIPPLE_ENABLE +class StippleTests : public TestSuite { + public: + StippleTests(TestHost& host, std::string output_dir); + + void Initialize() override; + void TearDownTest() override; + + private: + void Test(const std::string& name, bool stipple_enable, const std::vector& stipple_pattern); +}; + +#endif // NXDK_PGRAPH_TESTS_STIPPLE_TESTS_H