diff --git a/runtime/test/vk_harness.cpp b/runtime/test/vk_harness.cpp index fc1f352b85..836b198e6c 100644 --- a/runtime/test/vk_harness.cpp +++ b/runtime/test/vk_harness.cpp @@ -7,6 +7,7 @@ // // Push constants follow the contract in docs/design.md: s0..s31, then the heap base lo/hi. They are zero unless // VKH_SGPRS=w0,w1,... sets s0, s1, ... (decimal or 0x hex) +// VKH_GS_PRIMITIVES=1 reports "gs_primitives=N" on stderr for the VKH_GS draw (77 if unsupported) // VKH_HEAP=addr=word,... allocates a zeroed 64 KiB heap (Xbox VA 0 = its device address), writes each word at // its address and passes the heap base, so a V# in VKH_SGPRS can point into it. // VKH_HEAP_DUMP= after the draw, writes the 64 KiB heap (needs VKH_HEAP, which may be empty-valued as @@ -476,11 +477,22 @@ int main(int argc, char** argv) { // VKH_GS=: insert a geometry stage between the vertex and fragment shaders. VKH_SUBGROUP_SIZE then // applies to it, unless VKH_GS_HELPER=1 says the GS only feeds the pixel shader under test. const char* gs_path = std::getenv("VKH_GS"); + const char* count_opt = std::getenv("VKH_GS_PRIMITIVES"); + const bool count_gs = count_opt && std::strcmp(count_opt, "1") == 0; + if (count_gs && (!gs_path || !*gs_path || mesh || mesh_compute)) { + std::fprintf(stderr, "VKH_GS_PRIMITIVES=1 requires a VS/GS/FS draw with VKH_GS\n"); + return 1; + } if (gs_path && !f2q.features.geometryShader) { // MoltenVK: Metal has no geometry stage std::fprintf(stderr, "unsupported: no geometryShader\n"); return 77; } VkPhysicalDeviceFeatures feats{}; + if (count_gs && !f2q.features.pipelineStatisticsQuery) { + std::fprintf(stderr, "unsupported: no pipelineStatisticsQuery\n"); + return 77; + } + feats.pipelineStatisticsQuery = count_gs; if (clip_shifts && !f2q.features.shaderClipDistance) { std::fprintf(stderr, "unsupported: shaderClipDistance\n"); return 77; @@ -1010,6 +1022,17 @@ int main(int argc, char** argv) { VkCommandBuffer cmd; CHECK(vkAllocateCommandBuffers(dev, &cai, &cmd)); VkCommandBufferBeginInfo cbi{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; CHECK(vkBeginCommandBuffer(cmd, &cbi)); + VkQueryPool gs_query = VK_NULL_HANDLE; + if (count_gs) { + LOADD(vkCreateQueryPool); + LOADD(vkCmdResetQueryPool); + VkQueryPoolCreateInfo info{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO}; + info.queryType = VK_QUERY_TYPE_PIPELINE_STATISTICS; + info.queryCount = 1; + info.pipelineStatistics = VK_QUERY_PIPELINE_STATISTIC_GEOMETRY_SHADER_PRIMITIVES_BIT; + CHECK(vkCreateQueryPool(dev, &info, nullptr, &gs_query)); + vkCmdResetQueryPool(cmd, gs_query, 0, 1); + } VkClearValue clears[2]{}; for (float& c : clears[0].color.float32) c = -1; // marks untouched pixels if (clip_shifts) { @@ -1088,6 +1111,10 @@ int main(int argc, char** argv) { } // VKH_VERTICES=: draw n vertices instead of one triangle, so a vertex shader fills whole waves (TEST-251). const char* vertices_env = std::getenv("VKH_VERTICES"); + if (count_gs) { + LOADD(vkCmdBeginQuery); + vkCmdBeginQuery(cmd, gs_query, 0, 0); + } if (draw_mesh_path) { VkDeviceSize offset = 0; vkCmdBindVertexBuffers(cmd, 0, 1, &vbo, &offset); @@ -1106,6 +1133,10 @@ int main(int argc, char** argv) { if (const char* d = std::getenv("VKH_DRAW")) std::sscanf(d, "%u,%u", &first_vertex, &first_instance); vkCmdDraw(cmd, 3, 1, first_vertex, first_instance); } + if (count_gs) { + LOADD(vkCmdEndQuery); + vkCmdEndQuery(cmd, gs_query, 0); + } vkCmdEndRenderPass(cmd); VkBufferImageCopy reg{}; reg.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}; reg.imageExtent = {W, H, 1}; vkCmdCopyImageToBuffer(cmd, img, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, buf, 1, ®); @@ -1119,6 +1150,15 @@ int main(int argc, char** argv) { VkSubmitInfo si{VK_STRUCTURE_TYPE_SUBMIT_INFO}; si.commandBufferCount = 1; si.pCommandBuffers = &cmd; CHECK(vkQueueSubmit(q, 1, &si, VK_NULL_HANDLE)); CHECK(vkQueueWaitIdle(q)); + if (count_gs) { + LOADD(vkGetQueryPoolResults); + LOADD(vkDestroyQueryPool); + uint64_t primitives = 0; + CHECK(vkGetQueryPoolResults(dev, gs_query, 0, 1, sizeof(primitives), &primitives, + sizeof(primitives), VK_QUERY_RESULT_64_BIT)); + vkDestroyQueryPool(dev, gs_query, nullptr); + std::fprintf(stderr, "gs_primitives=%llu\n", static_cast(primitives)); + } void* p; CHECK(vkMapMemory(dev, bmem, 0, bi.size, 0, &p)); std::ofstream output(argv[3], std::ios::binary); output.write(static_cast(p), std::streamsize(bi.size)); diff --git a/tests/test_gs_primitive_readback.py b/tests/test_gs_primitive_readback.py new file mode 100644 index 0000000000..f189e60ca9 --- /dev/null +++ b/tests/test_gs_primitive_readback.py @@ -0,0 +1,57 @@ +"""GPU regression for translated GS strip cuts. Set LEONIDA_VK_HARNESS to the built vk_harness.""" +import os +from pathlib import Path +import struct +import subprocess + +import pytest + +from conftest import GS_TRI_DXBC, Shader, requires_tools + + +@requires_tools +def test_offscreen_geometry_strip_cuts(tmp_path): + harness = os.environ.get("LEONIDA_VK_HARNESS") + if not harness: + pytest.skip("set LEONIDA_VK_HARNESS to the built vk_harness") + + # Form a nondegenerate triangle with z=-1, w=1, entirely behind Vulkan's near clip plane. + vs = Shader(tmp_path, "vs_clipped", """ +v_lshlrev_b32_e32 v1, 1, v0 +v_and_b32_e32 v1, 2, v1 +v_and_b32_e32 v2, 2, v0 +v_cvt_f32_u32_e32 v1, v1 +v_cvt_f32_u32_e32 v2, v2 +v_mov_b32_e32 v3, -1.0 +v_mov_b32_e32 v4, 1.0 +exp pos0 v1, v2, v3, v4 done +exp param1 v1, v2, v3, v4 +s_endpgm +""") + ps = Shader(tmp_path, "ps_white", """ +v_mov_b32_e32 v0, 1.0 +exp mrt0 v0, v0, v0, v0 done +s_endpgm +""") + for shader in (vs, ps): + shader.validate(shader.translate()) + + fixture = Path(__file__).resolve().parents[1] / "runtime/test/shaders/gs_tri.asm" + triangle = fixture.read_text(encoding="utf-8") + # Three single-vertex strips produce zero triangles. One three-vertex strip produces one. + singles = triangle.replace("GS_OP_EMIT,", "GS_OP_EMIT_CUT,") + env = {key: value for key, value in os.environ.items() if not key.startswith("VKH_")} + for expected, asm in [(0, singles), (1, triangle)]: + gs = Shader(tmp_path, f"gs_strip_{expected}", asm, dxbc=GS_TRI_DXBC) + gs.validate(gs.translate()) + raw = tmp_path / f"{gs.name}.raw" + result = subprocess.run( + [harness, tmp_path / "vs_clipped.spv", tmp_path / "ps_white.spv", raw, "8", "8"], + env=dict(env, VKH_GS=str(tmp_path / f"{gs.name}.spv"), VKH_GS_PRIMITIVES="1"), + capture_output=True, text=True, timeout=30, + ) + if result.returncode == 77: + pytest.skip(result.stderr) + assert result.returncode == 0, result.stderr + assert f"gs_primitives={expected}" in result.stderr.splitlines(), result.stderr + assert raw.read_bytes() == struct.pack("<4f", -1, -1, -1, -1) * 64