upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/vk_harness.cpp
diff options
context:
space:
mode:
authorJess-Doit <doit.jesss@gmail.com>2026-11-21 22:21:32 -0800
committerJess-Doit <doit.jesss@gmail.com>2026-11-22 00:21:36 -0800
commitcaad84c32b5d5315a09b5c8d462a59a2aa0fc1cc (patch)
tree5037e74f8e865498e534deabd6af1bf4d2939f46 /runtime/test/vk_harness.cpp
parent2e5c0f283c618fdb37b4e9241748fa1e91e1abda (diff)
Translator: --mesh-compute runs mesh shaders without VK_EXT_mesh_shader
9,282 of the 26,503 shaders are mesh shaders, and a device without VK_EXT_mesh_shader (NVIDIA Pascal, for one) can't run any of them. With --mesh-compute each becomes a GLCompute module with the same workgroup size and code; only the epilogue changes. After the same barrier, lane 0 writes the vertex and primitive counts, lane i its vertex and its primitive, into this workgroup's slot of an output buffer whose address is in push-constant dwords 36-37 (152 bytes). The slot layout is mesh_compute_layout() and the index records it as mesh_compute. - tools/mesh_compute_vs.py: the vertex shader that draws the slots with a non-indexed draw of groups * max_primitives * 3 vertices; unused and culled primitives are clipped away, params keep their locations. - vk_harness: VKH_MESH_COMPUTE (+ VKH_MESH_SLOT, VKH_MESH_PRIMS) dispatches the module, adds a compute->vertex barrier and draws. - run_gpu_tests.py / check.py: LEONIDA_MESH_COMPUTE=1 (check.mesh_env). - tests/test_mesh_compute.py; docs/design.md row "Mesh shaders as compute", docs/testing.md section. GTX 1060 (Pascal) with LEONIDA_MESH_COMPUTE=1 LEONIDA_W64_EMULATE=1: all five ms_* tests pass (SKIP before), zero validation messages. Mesh corpus: 9,282 of 9,282 translate and pass spirv-val with --mesh-compute --wave64-emulate, and all 9,282 generated vertex shaders validate. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SSJXX67xeoorUsLJ2f9zms
Diffstat (limited to 'runtime/test/vk_harness.cpp')
-rw-r--r--runtime/test/vk_harness.cpp116
1 files changed, 97 insertions, 19 deletions
diff --git a/runtime/test/vk_harness.cpp b/runtime/test/vk_harness.cpp
index ae178ab9e..535c7879f 100644
--- a/runtime/test/vk_harness.cpp
+++ b/runtime/test/vk_harness.cpp
@@ -105,7 +105,8 @@ int main(int argc, char** argv) {
105 } else if (argc > 5) { 105 } else if (argc > 5) {
106 W = atoi(argv[4]); H = atoi(argv[5]); 106 W = atoi(argv[4]); H = atoi(argv[5]);
107 } 107 }
108 uint32_t pc[36] = {}; 108 // Push constants: s0..s31, heap base (32..33), code base (34..35), --mesh-compute output address (36..37).
109 uint32_t pc[38] = {};
109 const char* sgprs_env = std::getenv("VKH_SGPRS"); 110 const char* sgprs_env = std::getenv("VKH_SGPRS");
110 if (sgprs_env && !parse_sgprs(sgprs_env, pc)) { 111 if (sgprs_env && !parse_sgprs(sgprs_env, pc)) {
111 std::fprintf(stderr, "VKH_SGPRS: expected up to 32 comma-separated words, got \"%s\"\n", sgprs_env); 112 std::fprintf(stderr, "VKH_SGPRS: expected up to 32 comma-separated words, got \"%s\"\n", sgprs_env);
@@ -203,8 +204,28 @@ int main(int argc, char** argv) {
203 enable.push_back(VK_EXT_FRAGMENT_SHADER_INTERLOCK_EXTENSION_NAME); 204 enable.push_back(VK_EXT_FRAGMENT_SHADER_INTERLOCK_EXTENSION_NAME);
204 lock.fragmentShaderPixelInterlock = VK_TRUE; lock.pNext = chain; chain = &lock; 205 lock.fragmentShaderPixelInterlock = VK_TRUE; lock.pNext = chain; chain = &lock;
205 } 206 }
207 // VKH_MESH_COMPUTE=<vs.spv>: the first shader is a mesh shader translated with rdna2spirv.py --mesh-compute.
208 // It is dispatched as compute over the mesh grid (1x1x1, or --mesh-group-readback's), writing its slots to an
209 // output buffer (VKH_MESH_SLOT bytes per workgroup, address in push-constant dwords 36..37). Then
210 // vs.spv (tools/mesh_compute_vs.py) draws groups * VKH_MESH_PRIMS * 3 vertices with the fragment shader. No
211 // VK_EXT_mesh_shader is needed.
212 const char* mesh_vs_path = std::getenv("VKH_MESH_COMPUTE");
213 if (mesh_vs_path && !*mesh_vs_path) mesh_vs_path = nullptr;
214 const bool mesh_compute = mesh_vs_path != nullptr;
215 uint64_t mesh_slot = 0;
216 uint32_t mesh_prims = 0;
217 if (mesh_compute) {
218 const char* slot_env = std::getenv("VKH_MESH_SLOT");
219 const char* prims_env = std::getenv("VKH_MESH_PRIMS");
220 mesh_slot = slot_env ? std::strtoull(slot_env, nullptr, 0) : 0;
221 mesh_prims = prims_env ? uint32_t(std::strtoul(prims_env, nullptr, 0)) : 0;
222 if (!mesh_slot || !mesh_prims) {
223 std::fprintf(stderr, "VKH_MESH_COMPUTE needs VKH_MESH_SLOT and VKH_MESH_PRIMS (index.json mesh_compute)\n");
224 return 2;
225 }
226 }
206 // VKH_MESH=1: the first shader is a mesh shader (VK_EXT_mesh_shader), drawn with vkCmdDrawMeshTasksEXT(1,1,1). 227 // VKH_MESH=1: the first shader is a mesh shader (VK_EXT_mesh_shader), drawn with vkCmdDrawMeshTasksEXT(1,1,1).
207 const bool mesh = group_readback || std::getenv("VKH_MESH") != nullptr; 228 const bool mesh = !mesh_compute && (group_readback || std::getenv("VKH_MESH") != nullptr);
208 VkPhysicalDeviceMeshShaderFeaturesEXT meshf{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MESH_SHADER_FEATURES_EXT}; 229 VkPhysicalDeviceMeshShaderFeaturesEXT meshf{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MESH_SHADER_FEATURES_EXT};
209 if (mesh) { 230 if (mesh) {
210 // Query the feature, not just the extension. 77: the device cannot run this test (as vk_compute_test and 231 // Query the feature, not just the extension. 77: the device cannot run this test (as vk_compute_test and
@@ -240,9 +261,12 @@ int main(int argc, char** argv) {
240 chain = &f13; 261 chain = &f13;
241 } 262 }
242 VkPhysicalDeviceVulkan12Features f12{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES}; 263 VkPhysicalDeviceVulkan12Features f12{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES};
243 if (heap_env) { 264 if (heap_env || mesh_compute) {
244 // 77 is the "this device cannot run the test" code that check.py and run_gpu_tests.py report as SKIP. 265 // 77 is the "this device cannot run the test" code that check.py and run_gpu_tests.py report as SKIP.
245 if (!f12q.bufferDeviceAddress) { std::fprintf(stderr, "unsupported: VKH_HEAP needs bufferDeviceAddress\n"); return 77; } 266 if (!f12q.bufferDeviceAddress) {
267 std::fprintf(stderr, "unsupported: %s needs bufferDeviceAddress\n", heap_env ? "VKH_HEAP" : "VKH_MESH_COMPUTE");
268 return 77;
269 }
246 f12.bufferDeviceAddress = VK_TRUE; 270 f12.bufferDeviceAddress = VK_TRUE;
247 f12.pNext = chain; 271 f12.pNext = chain;
248 chain = &f12; 272 chain = &f12;
@@ -274,9 +298,9 @@ int main(int argc, char** argv) {
274 } 298 }
275 feats.shaderFloat64 = VK_TRUE; 299 feats.shaderFloat64 = VK_TRUE;
276 } 300 }
277 if (heap_env) { 301 if (heap_env || mesh_compute) {
278 if (!f2q.features.shaderInt64) { std::fprintf(stderr, "unsupported: VKH_HEAP needs shaderInt64\n"); return 77; } 302 if (!f2q.features.shaderInt64) { std::fprintf(stderr, "unsupported: 64-bit addresses need shaderInt64\n"); return 77; }
279 feats.shaderInt64 = VK_TRUE; // heap addresses are 64-bit 303 feats.shaderInt64 = VK_TRUE; // heap and --mesh-compute output addresses are 64-bit
280 } 304 }
281 dci.pEnabledFeatures = &feats; 305 dci.pEnabledFeatures = &feats;
282 VkDevice dev; 306 VkDevice dev;
@@ -365,8 +389,9 @@ int main(int argc, char** argv) {
365 smi.codeSize = code.size() * 4; smi.pCode = code.data(); 389 smi.codeSize = code.size() * 4; smi.pCode = code.data();
366 return vkCreateShaderModule(dev, &smi, nullptr, m); 390 return vkCreateShaderModule(dev, &smi, nullptr, m);
367 }; 391 };
368 VkShaderModule vs, fs; 392 VkShaderModule vs, fs, csm = VK_NULL_HANDLE;
369 CHECK(module(argv[1], &vs)); CHECK(module(argv[2], &fs)); 393 CHECK(module(mesh_compute ? mesh_vs_path : argv[1], &vs)); CHECK(module(argv[2], &fs));
394 if (mesh_compute) CHECK(module(argv[1], &csm));
370 VkPipelineShaderStageCreateInfo stages[3]{{VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO}, 395 VkPipelineShaderStageCreateInfo stages[3]{{VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO},
371 {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO}, 396 {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO},
372 {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO}}; 397 {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO}};
@@ -410,10 +435,12 @@ int main(int argc, char** argv) {
410 VkPipelineColorBlendAttachmentState cba{}; cba.colorWriteMask = 0xF; 435 VkPipelineColorBlendAttachmentState cba{}; cba.colorWriteMask = 0xF;
411 VkPipelineColorBlendStateCreateInfo cb{VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO}; 436 VkPipelineColorBlendStateCreateInfo cb{VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO};
412 cb.attachmentCount = 1; cb.pAttachments = &cba; 437 cb.attachmentCount = 1; cb.pAttachments = &cba;
413 // Translated shaders declare a 136-byte push-constant block (user SGPRs + heap base); see VKH_SGPRS / VKH_HEAP. 438 // Translated shaders declare a 144-byte push-constant block (user SGPRs, heap base, code base; see VKH_SGPRS /
439 // VKH_HEAP); --mesh-compute modules and their vertex shaders add the output address (152 bytes).
414 const VkShaderStageFlags pcs = (mesh ? VK_SHADER_STAGE_MESH_BIT_EXT : VK_SHADER_STAGE_VERTEX_BIT) | 440 const VkShaderStageFlags pcs = (mesh ? VK_SHADER_STAGE_MESH_BIT_EXT : VK_SHADER_STAGE_VERTEX_BIT) |
415 VK_SHADER_STAGE_FRAGMENT_BIT | (gs_path ? VK_SHADER_STAGE_GEOMETRY_BIT : 0); 441 VK_SHADER_STAGE_FRAGMENT_BIT | (gs_path ? VK_SHADER_STAGE_GEOMETRY_BIT : 0) |
416 VkPushConstantRange pcr{pcs, 0, 144}; 442 (mesh_compute ? VK_SHADER_STAGE_COMPUTE_BIT : 0);
443 VkPushConstantRange pcr{pcs, 0, mesh_compute ? 152u : 144u};
417 VkPipelineLayoutCreateInfo pli{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO}; 444 VkPipelineLayoutCreateInfo pli{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO};
418 pli.pushConstantRangeCount = 1; pli.pPushConstantRanges = &pcr; 445 pli.pushConstantRangeCount = 1; pli.pPushConstantRanges = &pcr;
419 VkPipelineLayout pl; CHECK(vkCreatePipelineLayout(dev, &pli, nullptr, &pl)); 446 VkPipelineLayout pl; CHECK(vkCreatePipelineLayout(dev, &pli, nullptr, &pl));
@@ -434,6 +461,19 @@ int main(int argc, char** argv) {
434 } 461 }
435 gpi.layout = pl; gpi.renderPass = rp; 462 gpi.layout = pl; gpi.renderPass = rp;
436 VkPipeline pipe; CHECK(vkCreateGraphicsPipelines(dev, VK_NULL_HANDLE, 1, &gpi, nullptr, &pipe)); 463 VkPipeline pipe; CHECK(vkCreateGraphicsPipelines(dev, VK_NULL_HANDLE, 1, &gpi, nullptr, &pipe));
464 VkPipeline cpipe = VK_NULL_HANDLE;
465 if (mesh_compute) {
466 VkComputePipelineCreateInfo cpi{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO};
467 cpi.stage = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO};
468 cpi.stage.stage = VK_SHADER_STAGE_COMPUTE_BIT; cpi.stage.module = csm; cpi.stage.pName = "main";
469 // VKH_SUBGROUP_SIZE goes to the mesh shader under test, now a compute stage.
470 if (req_sg > 0 && f13q.subgroupSizeControl && req_sg >= sgProps.minSubgroupSize &&
471 req_sg <= sgProps.maxSubgroupSize && (sgProps.requiredSubgroupSizeStages & VK_SHADER_STAGE_COMPUTE_BIT))
472 cpi.stage.pNext = &reqSg;
473 cpi.layout = pl;
474 LOADD(vkCreateComputePipelines);
475 CHECK(vkCreateComputePipelines(dev, VK_NULL_HANDLE, 1, &cpi, nullptr, &cpipe));
476 }
437 VkBufferCreateInfo bi{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; 477 VkBufferCreateInfo bi{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
438 bi.size = VkDeviceSize(W) * H * 16; bi.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT; 478 bi.size = VkDeviceSize(W) * H * 16; bi.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT;
439 VkBuffer buf; CHECK(vkCreateBuffer(dev, &bi, nullptr, &buf)); 479 VkBuffer buf; CHECK(vkCreateBuffer(dev, &bi, nullptr, &buf));
@@ -465,6 +505,30 @@ int main(int argc, char** argv) {
465 const uint64_t base = vkGetBufferDeviceAddress(dev, &hda); 505 const uint64_t base = vkGetBufferDeviceAddress(dev, &hda);
466 pc[32] = uint32_t(base); pc[33] = uint32_t(base >> 32); 506 pc[32] = uint32_t(base); pc[33] = uint32_t(base >> 32);
467 } 507 }
508 const uint64_t mesh_groups = uint64_t(groups[0]) * groups[1] * groups[2];
509 if (mesh_compute) {
510 LOADD(vkGetBufferDeviceAddress);
511 VkBufferCreateInfo obi{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
512 obi.size = mesh_groups * mesh_slot;
513 obi.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
514 VkBuffer obuf; CHECK(vkCreateBuffer(dev, &obi, nullptr, &obuf));
515 VkMemoryRequirements orq; vkGetBufferMemoryRequirements(dev, obuf, &orq);
516 VkMemoryAllocateFlagsInfo oaf{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO};
517 oaf.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT;
518 VkMemoryAllocateInfo oai{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
519 oai.pNext = &oaf; oai.allocationSize = orq.size;
520 oai.memoryTypeIndex = memtype(orq.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
521 VkDeviceMemory omem; CHECK(vkAllocateMemory(dev, &oai, nullptr, &omem));
522 CHECK(vkBindBufferMemory(dev, obuf, omem, 0));
523 void* op; CHECK(vkMapMemory(dev, omem, 0, obi.size, 0, &op));
524 std::memset(op, 0, size_t(obi.size)); // a workgroup that never writes its header draws nothing
525 VkBufferDeviceAddressInfo oda{VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO};
526 oda.buffer = obuf;
527 const uint64_t oaddr = vkGetBufferDeviceAddress(dev, &oda);
528 pc[36] = uint32_t(oaddr); pc[37] = uint32_t(oaddr >> 32);
529 std::fprintf(stderr, "mesh compute: %llu workgroups x %llu bytes, %u primitives each\n",
530 (unsigned long long)mesh_groups, (unsigned long long)mesh_slot, mesh_prims);
531 }
468 VkCommandPoolCreateInfo cpi{VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO}; cpi.queueFamilyIndex = qi; 532 VkCommandPoolCreateInfo cpi{VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO}; cpi.queueFamilyIndex = qi;
469 VkCommandPool pool; CHECK(vkCreateCommandPool(dev, &cpi, nullptr, &pool)); 533 VkCommandPool pool; CHECK(vkCreateCommandPool(dev, &cpi, nullptr, &pool));
470 VkCommandBufferAllocateInfo cai{VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO}; 534 VkCommandBufferAllocateInfo cai{VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO};
@@ -474,6 +538,23 @@ int main(int argc, char** argv) {
474 CHECK(vkBeginCommandBuffer(cmd, &cbi)); 538 CHECK(vkBeginCommandBuffer(cmd, &cbi));
475 VkClearValue clears[2]{}; 539 VkClearValue clears[2]{};
476 for (float& c : clears[0].color.float32) c = -1; // marks untouched pixels 540 for (float& c : clears[0].color.float32) c = -1; // marks untouched pixels
541 if (clip_shifts) {
542 const float shifts[] = {float(std::atof(argv[6])), float(std::atof(argv[7]))};
543 std::memcpy(pc, shifts, sizeof(shifts));
544 }
545 auto push = reinterpret_cast<PFN_vkCmdPushConstants>(vkGetDeviceProcAddr(dev, "vkCmdPushConstants"));
546 const uint32_t pc_bytes = mesh_compute ? 152u : 144u;
547 if (mesh_compute) {
548 // The mesh shader as compute, then make its stores visible to the vertex shader's reads.
549 LOADD(vkCmdDispatch); LOADD(vkCmdPipelineBarrier);
550 vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, cpipe);
551 push(cmd, pl, pcs, 0, pc_bytes, pc);
552 vkCmdDispatch(cmd, groups[0], groups[1], groups[2]);
553 VkMemoryBarrier mb{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
554 mb.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT; mb.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
555 vkCmdPipelineBarrier(cmd, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT, 0,
556 1, &mb, 0, nullptr, 0, nullptr);
557 }
477 VkRenderPassBeginInfo rbi{VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO}; 558 VkRenderPassBeginInfo rbi{VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO};
478 rbi.renderPass = rp; rbi.framebuffer = fb; rbi.renderArea = sc; rbi.clearValueCount = rpi.attachmentCount; rbi.pClearValues = clears; 559 rbi.renderPass = rp; rbi.framebuffer = fb; rbi.renderArea = sc; rbi.clearValueCount = rpi.attachmentCount; rbi.pClearValues = clears;
479 vkCmdBeginRenderPass(cmd, &rbi, VK_SUBPASS_CONTENTS_INLINE); 560 vkCmdBeginRenderPass(cmd, &rbi, VK_SUBPASS_CONTENTS_INLINE);
@@ -488,13 +569,10 @@ int main(int argc, char** argv) {
488 std::fprintf(stderr, "mesh workgroups: %u x %u x %u\n", groups[0], groups[1], groups[2]); 569 std::fprintf(stderr, "mesh workgroups: %u x %u x %u\n", groups[0], groups[1], groups[2]);
489 } 570 }
490 vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipe); 571 vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipe);
491 if (clip_shifts) { 572 push(cmd, pl, pcs, 0, pc_bytes, pc);
492 const float shifts[] = {float(std::atof(argv[6])), float(std::atof(argv[7]))}; 573 if (mesh_compute)
493 std::memcpy(pc, shifts, sizeof(shifts)); 574 vkCmdDraw(cmd, uint32_t(mesh_groups * mesh_prims * 3), 1, 0, 0);
494 } 575 else if (mesh)
495 reinterpret_cast<PFN_vkCmdPushConstants>(vkGetDeviceProcAddr(dev, "vkCmdPushConstants"))(
496 cmd, pl, pcs, 0, sizeof(pc), pc);
497 if (mesh)
498 reinterpret_cast<PFN_vkCmdDrawMeshTasksEXT>(vkGetDeviceProcAddr(dev, "vkCmdDrawMeshTasksEXT"))(cmd, groups[0], groups[1], groups[2]); 576 reinterpret_cast<PFN_vkCmdDrawMeshTasksEXT>(vkGetDeviceProcAddr(dev, "vkCmdDrawMeshTasksEXT"))(cmd, groups[0], groups[1], groups[2]);
499 else 577 else
500 vkCmdDraw(cmd, 3, 1, 0, 0); 578 vkCmdDraw(cmd, 3, 1, 0, 0);