diff options
| author | AREA52 Agent <area52@localhost> | 2026-11-21 15:31:09 -0800 |
|---|---|---|
| committer | AREA52 Agent <area52@localhost> | 2026-11-21 15:31:09 -0800 |
| commit | 25ca1d63f2883ff88ab8bfcd3afd8cc426ab141c (patch) | |
| tree | 8cdca7f6e87736acea8d1e1105d8b4d8eb05ad24 /runtime/test/vk_harness.cpp | |
| parent | eef019d7baf91e819d80f3aa12fd6d3a050b7c5f (diff) | |
| parent | d937b7f092dbc188b59754e5c73c1a9dac553cf9 (diff) | |
Merge upstream main into SHADER-7 and retain filtered clip dependencies
Diffstat (limited to 'runtime/test/vk_harness.cpp')
| -rw-r--r-- | runtime/test/vk_harness.cpp | 99 |
1 files changed, 95 insertions, 4 deletions
diff --git a/runtime/test/vk_harness.cpp b/runtime/test/vk_harness.cpp index b5a092030..028d58e74 100644 --- a/runtime/test/vk_harness.cpp +++ b/runtime/test/vk_harness.cpp | |||
| @@ -5,14 +5,21 @@ | |||
| 5 | // vk_harness <ms.spv> <fs.spv> <out.raw> --mesh-group-readback X Y Z | 5 | // vk_harness <ms.spv> <fs.spv> <out.raw> --mesh-group-readback X Y Z |
| 6 | // The latter captures one full-screen triangle per mesh workgroup, in Vulkan rasterization order. | 6 | // The latter captures one full-screen triangle per mesh workgroup, in Vulkan rasterization order. |
| 7 | // | 7 | // |
| 8 | // Push constants follow the contract in docs/design.md: s0..s31, then the heap base lo/hi. They are zero unless | ||
| 9 | // VKH_SGPRS=w0,w1,... sets s0, s1, ... (decimal or 0x hex) | ||
| 10 | // VKH_HEAP=addr=word,... allocates a zeroed 64 KiB heap (Xbox VA 0 = its device address), writes each word at | ||
| 11 | // its address and passes the heap base, so a V# in VKH_SGPRS can point into it. | ||
| 12 | // | ||
| 8 | #define VK_NO_PROTOTYPES | 13 | #define VK_NO_PROTOTYPES |
| 9 | #include "vulkan_loader.h" | 14 | #include "vulkan_loader.h" |
| 10 | 15 | ||
| 16 | #include <cctype> | ||
| 11 | #include <cstddef> | 17 | #include <cstddef> |
| 12 | #include <cstdio> | 18 | #include <cstdio> |
| 13 | #include <cstdlib> | 19 | #include <cstdlib> |
| 14 | #include <cstring> | 20 | #include <cstring> |
| 15 | #include <fstream> | 21 | #include <fstream> |
| 22 | #include <utility> | ||
| 16 | #include <vector> | 23 | #include <vector> |
| 17 | 24 | ||
| 18 | static PFN_vkGetInstanceProcAddr gipa; | 25 | static PFN_vkGetInstanceProcAddr gipa; |
| @@ -34,6 +41,40 @@ static std::vector<uint32_t> read_spv(const char* path) { | |||
| 34 | return w; | 41 | return w; |
| 35 | } | 42 | } |
| 36 | 43 | ||
| 44 | static bool parse_u32(const char*& s, uint32_t& v) { | ||
| 45 | const bool hex = s[0] == '0' && (s[1] == 'x' || s[1] == 'X'); | ||
| 46 | const char* digits = hex ? s + 2 : s; | ||
| 47 | if (!(hex ? std::isxdigit(uint8_t(*digits)) : std::isdigit(uint8_t(*digits)))) return false; // no sign or space | ||
| 48 | char* end; | ||
| 49 | unsigned long long x = std::strtoull(digits, &end, hex ? 16 : 10); | ||
| 50 | if (x > 0xFFFFFFFFull) return false; | ||
| 51 | v = uint32_t(x); s = end; | ||
| 52 | return true; | ||
| 53 | } | ||
| 54 | |||
| 55 | // A list separator must be followed by another item: "1,2," is rejected. | ||
| 56 | static bool next_item(const char*& s) { | ||
| 57 | if (!*s) return true; | ||
| 58 | return *s++ == ',' && *s; | ||
| 59 | } | ||
| 60 | |||
| 61 | static bool parse_sgprs(const char* s, uint32_t* sgprs) { | ||
| 62 | for (uint32_t i = 0; *s; i++) { | ||
| 63 | if (i == 32 || !parse_u32(s, sgprs[i]) || !next_item(s)) return false; | ||
| 64 | } | ||
| 65 | return true; | ||
| 66 | } | ||
| 67 | |||
| 68 | static bool parse_heap(const char* s, std::vector<std::pair<uint32_t, uint32_t>>& words, uint32_t heap_bytes) { | ||
| 69 | while (*s) { | ||
| 70 | uint32_t addr, word; | ||
| 71 | if (!parse_u32(s, addr) || *s++ != '=' || !parse_u32(s, word) || !next_item(s)) return false; | ||
| 72 | if (addr % 4 || addr > heap_bytes - 4) return false; | ||
| 73 | words.emplace_back(addr, word); | ||
| 74 | } | ||
| 75 | return true; | ||
| 76 | } | ||
| 77 | |||
| 37 | int main(int argc, char** argv) { | 78 | int main(int argc, char** argv) { |
| 38 | if (argc < 4) { | 79 | if (argc < 4) { |
| 39 | std::fprintf(stderr, "usage: vk_harness vs.spv fs.spv out.raw [w h [clip-test-s0 clip-test-s1]]\n" | 80 | std::fprintf(stderr, "usage: vk_harness vs.spv fs.spv out.raw [w h [clip-test-s0 clip-test-s1]]\n" |
| @@ -63,6 +104,21 @@ int main(int argc, char** argv) { | |||
| 63 | } else if (argc > 5) { | 104 | } else if (argc > 5) { |
| 64 | W = atoi(argv[4]); H = atoi(argv[5]); | 105 | W = atoi(argv[4]); H = atoi(argv[5]); |
| 65 | } | 106 | } |
| 107 | uint32_t pc[36] = {}; | ||
| 108 | const char* sgprs_env = std::getenv("VKH_SGPRS"); | ||
| 109 | if (sgprs_env && !parse_sgprs(sgprs_env, pc)) { | ||
| 110 | std::fprintf(stderr, "VKH_SGPRS: expected up to 32 comma-separated words, got \"%s\"\n", sgprs_env); | ||
| 111 | return 2; | ||
| 112 | } | ||
| 113 | const uint32_t heap_bytes = 64 * 1024; | ||
| 114 | const char* heap_env = std::getenv("VKH_HEAP"); | ||
| 115 | if (heap_env && !*heap_env) heap_env = nullptr; | ||
| 116 | std::vector<std::pair<uint32_t, uint32_t>> heap_words; | ||
| 117 | if (heap_env && !parse_heap(heap_env, heap_words, heap_bytes)) { | ||
| 118 | std::fprintf(stderr, "VKH_HEAP: expected addr=word pairs, 4-byte aligned below 0x%x, got \"%s\"\n", | ||
| 119 | heap_bytes, heap_env); | ||
| 120 | return 2; | ||
| 121 | } | ||
| 66 | gipa = load_vulkan(); | 122 | gipa = load_vulkan(); |
| 67 | if (!gipa) return 1; | 123 | if (!gipa) return 1; |
| 68 | VkInstance inst = VK_NULL_HANDLE; | 124 | VkInstance inst = VK_NULL_HANDLE; |
| @@ -167,7 +223,9 @@ int main(int argc, char** argv) { | |||
| 167 | m8.maintenance8 = VK_TRUE; m8.pNext = chain; chain = &m8; | 223 | m8.maintenance8 = VK_TRUE; m8.pNext = chain; chain = &m8; |
| 168 | } | 224 | } |
| 169 | #endif | 225 | #endif |
| 226 | VkPhysicalDeviceVulkan12Features f12q{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES}; | ||
| 170 | VkPhysicalDeviceVulkan13Features f13q{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES}; | 227 | VkPhysicalDeviceVulkan13Features f13q{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES}; |
| 228 | f13q.pNext = &f12q; | ||
| 171 | VkPhysicalDeviceFeatures2 f2q{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2}; | 229 | VkPhysicalDeviceFeatures2 f2q{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2}; |
| 172 | f2q.pNext = &f13q; | 230 | f2q.pNext = &f13q; |
| 173 | vkGetPhysicalDeviceFeatures2(pd, &f2q); | 231 | vkGetPhysicalDeviceFeatures2(pd, &f2q); |
| @@ -177,6 +235,14 @@ int main(int argc, char** argv) { | |||
| 177 | f13.pNext = chain; | 235 | f13.pNext = chain; |
| 178 | chain = &f13; | 236 | chain = &f13; |
| 179 | } | 237 | } |
| 238 | VkPhysicalDeviceVulkan12Features f12{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES}; | ||
| 239 | if (heap_env) { | ||
| 240 | // 77 is the "this device cannot run the test" code that check.py and run_gpu_tests.py report as SKIP. | ||
| 241 | if (!f12q.bufferDeviceAddress) { std::fprintf(stderr, "unsupported: VKH_HEAP needs bufferDeviceAddress\n"); return 77; } | ||
| 242 | f12.bufferDeviceAddress = VK_TRUE; | ||
| 243 | f12.pNext = chain; | ||
| 244 | chain = &f12; | ||
| 245 | } | ||
| 180 | for (auto e : enable) std::fprintf(stderr, "enabled %s\n", e); | 246 | for (auto e : enable) std::fprintf(stderr, "enabled %s\n", e); |
| 181 | VkDeviceCreateInfo dci{VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO}; | 247 | VkDeviceCreateInfo dci{VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO}; |
| 182 | dci.queueCreateInfoCount = 1; dci.pQueueCreateInfos = &qci; | 248 | dci.queueCreateInfoCount = 1; dci.pQueueCreateInfos = &qci; |
| @@ -196,6 +262,10 @@ int main(int argc, char** argv) { | |||
| 196 | feats.geometryShader = gs_path ? VK_TRUE : VK_FALSE; | 262 | feats.geometryShader = gs_path ? VK_TRUE : VK_FALSE; |
| 197 | feats.shaderClipDistance = f2q.features.shaderClipDistance; | 263 | feats.shaderClipDistance = f2q.features.shaderClipDistance; |
| 198 | feats.shaderCullDistance = f2q.features.shaderCullDistance; | 264 | feats.shaderCullDistance = f2q.features.shaderCullDistance; |
| 265 | if (heap_env) { | ||
| 266 | if (!f2q.features.shaderInt64) { std::fprintf(stderr, "unsupported: VKH_HEAP needs shaderInt64\n"); return 77; } | ||
| 267 | feats.shaderInt64 = VK_TRUE; // heap addresses are 64-bit | ||
| 268 | } | ||
| 199 | dci.pEnabledFeatures = &feats; | 269 | dci.pEnabledFeatures = &feats; |
| 200 | VkDevice dev; | 270 | VkDevice dev; |
| 201 | CHECK(vkCreateDevice(pd, &dci, nullptr, &dev)); | 271 | CHECK(vkCreateDevice(pd, &dci, nullptr, &dev)); |
| @@ -325,7 +395,7 @@ int main(int argc, char** argv) { | |||
| 325 | VkPipelineColorBlendAttachmentState cba{}; cba.colorWriteMask = 0xF; | 395 | VkPipelineColorBlendAttachmentState cba{}; cba.colorWriteMask = 0xF; |
| 326 | VkPipelineColorBlendStateCreateInfo cb{VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO}; | 396 | VkPipelineColorBlendStateCreateInfo cb{VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO}; |
| 327 | cb.attachmentCount = 1; cb.pAttachments = &cba; | 397 | cb.attachmentCount = 1; cb.pAttachments = &cba; |
| 328 | // Translated shaders declare a 136-byte push-constant block (user SGPRs + heap base); zeros here. | 398 | // Translated shaders declare a 136-byte push-constant block (user SGPRs + heap base); see VKH_SGPRS / VKH_HEAP. |
| 329 | const VkShaderStageFlags pcs = (mesh ? VK_SHADER_STAGE_MESH_BIT_EXT : VK_SHADER_STAGE_VERTEX_BIT) | | 399 | const VkShaderStageFlags pcs = (mesh ? VK_SHADER_STAGE_MESH_BIT_EXT : VK_SHADER_STAGE_VERTEX_BIT) | |
| 330 | VK_SHADER_STAGE_FRAGMENT_BIT | (gs_path ? VK_SHADER_STAGE_GEOMETRY_BIT : 0); | 400 | VK_SHADER_STAGE_FRAGMENT_BIT | (gs_path ? VK_SHADER_STAGE_GEOMETRY_BIT : 0); |
| 331 | VkPushConstantRange pcr{pcs, 0, 144}; | 401 | VkPushConstantRange pcr{pcs, 0, 144}; |
| @@ -358,6 +428,28 @@ int main(int argc, char** argv) { | |||
| 358 | bai.memoryTypeIndex = memtype(br.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); | 428 | bai.memoryTypeIndex = memtype(br.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); |
| 359 | VkDeviceMemory bmem; CHECK(vkAllocateMemory(dev, &bai, nullptr, &bmem)); | 429 | VkDeviceMemory bmem; CHECK(vkAllocateMemory(dev, &bai, nullptr, &bmem)); |
| 360 | CHECK(vkBindBufferMemory(dev, buf, bmem, 0)); | 430 | CHECK(vkBindBufferMemory(dev, buf, bmem, 0)); |
| 431 | if (heap_env) { | ||
| 432 | LOADD(vkGetBufferDeviceAddress); | ||
| 433 | VkBufferCreateInfo hbi{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; | ||
| 434 | hbi.size = heap_bytes; | ||
| 435 | hbi.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT; | ||
| 436 | VkBuffer heap; CHECK(vkCreateBuffer(dev, &hbi, nullptr, &heap)); | ||
| 437 | VkMemoryRequirements hr; vkGetBufferMemoryRequirements(dev, heap, &hr); | ||
| 438 | VkMemoryAllocateFlagsInfo haf{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO}; | ||
| 439 | haf.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT; | ||
| 440 | VkMemoryAllocateInfo hai{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO}; | ||
| 441 | hai.pNext = &haf; hai.allocationSize = hr.size; | ||
| 442 | hai.memoryTypeIndex = memtype(hr.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); | ||
| 443 | VkDeviceMemory hmem; CHECK(vkAllocateMemory(dev, &hai, nullptr, &hmem)); | ||
| 444 | CHECK(vkBindBufferMemory(dev, heap, hmem, 0)); | ||
| 445 | void* hp; CHECK(vkMapMemory(dev, hmem, 0, heap_bytes, 0, &hp)); | ||
| 446 | std::memset(hp, 0, heap_bytes); | ||
| 447 | for (auto [addr, word] : heap_words) std::memcpy(static_cast<char*>(hp) + addr, &word, 4); | ||
| 448 | VkBufferDeviceAddressInfo hda{VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO}; | ||
| 449 | hda.buffer = heap; | ||
| 450 | const uint64_t base = vkGetBufferDeviceAddress(dev, &hda); | ||
| 451 | pc[32] = uint32_t(base); pc[33] = uint32_t(base >> 32); | ||
| 452 | } | ||
| 361 | VkCommandPoolCreateInfo cpi{VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO}; cpi.queueFamilyIndex = qi; | 453 | VkCommandPoolCreateInfo cpi{VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO}; cpi.queueFamilyIndex = qi; |
| 362 | VkCommandPool pool; CHECK(vkCreateCommandPool(dev, &cpi, nullptr, &pool)); | 454 | VkCommandPool pool; CHECK(vkCreateCommandPool(dev, &cpi, nullptr, &pool)); |
| 363 | VkCommandBufferAllocateInfo cai{VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO}; | 455 | VkCommandBufferAllocateInfo cai{VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO}; |
| @@ -381,13 +473,12 @@ int main(int argc, char** argv) { | |||
| 381 | std::fprintf(stderr, "mesh workgroups: %u x %u x %u\n", groups[0], groups[1], groups[2]); | 473 | std::fprintf(stderr, "mesh workgroups: %u x %u x %u\n", groups[0], groups[1], groups[2]); |
| 382 | } | 474 | } |
| 383 | vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipe); | 475 | vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipe); |
| 384 | uint32_t zeros[36] = {}; | ||
| 385 | if (clip_shifts) { | 476 | if (clip_shifts) { |
| 386 | const float shifts[] = {float(std::atof(argv[6])), float(std::atof(argv[7]))}; | 477 | const float shifts[] = {float(std::atof(argv[6])), float(std::atof(argv[7]))}; |
| 387 | std::memcpy(zeros, shifts, sizeof(shifts)); | 478 | std::memcpy(pc, shifts, sizeof(shifts)); |
| 388 | } | 479 | } |
| 389 | reinterpret_cast<PFN_vkCmdPushConstants>(vkGetDeviceProcAddr(dev, "vkCmdPushConstants"))( | 480 | reinterpret_cast<PFN_vkCmdPushConstants>(vkGetDeviceProcAddr(dev, "vkCmdPushConstants"))( |
| 390 | cmd, pl, pcs, 0, sizeof(zeros), zeros); | 481 | cmd, pl, pcs, 0, sizeof(pc), pc); |
| 391 | if (mesh) | 482 | if (mesh) |
| 392 | reinterpret_cast<PFN_vkCmdDrawMeshTasksEXT>(vkGetDeviceProcAddr(dev, "vkCmdDrawMeshTasksEXT"))(cmd, groups[0], groups[1], groups[2]); | 483 | reinterpret_cast<PFN_vkCmdDrawMeshTasksEXT>(vkGetDeviceProcAddr(dev, "vkCmdDrawMeshTasksEXT"))(cmd, groups[0], groups[1], groups[2]); |
| 393 | else | 484 | else |