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:
authorSuarezPM <SuarezPM@users.noreply.github.com>2026-11-22 11:58:00 -0300
committerSuarezPM <SuarezPM@users.noreply.github.com>2026-11-22 12:28:20 -0300
commit07cc6efb206e75a01c1ce4f76a4440175d3f404c (patch)
treee4267c62fb824d6a0950c110bacb23fd008b91ca /runtime/test/vk_harness.cpp
parent4eb6155d3ac4f9d3fadb0d095c4ce13e76783615 (diff)
first_light: a real game model with its own translated VS/PS
tools/first_light.py carves the game's shaders, translates one real VS/PS pair, converts an exported OBJ to a mesh, writes a manifest and runs vk_harness. The harness gains VKH_DRAW_MESH/VKH_VERTEX_LAYOUT (indexed mesh, vertex input), inert when unset. The package-gated test draws the model and checks coverage and the centre pixel; docs/first_light.md records the measured result.
Diffstat (limited to 'runtime/test/vk_harness.cpp')
-rw-r--r--runtime/test/vk_harness.cpp103
1 files changed, 102 insertions, 1 deletions
diff --git a/runtime/test/vk_harness.cpp b/runtime/test/vk_harness.cpp
index 360a20fa7..a2491b64b 100644
--- a/runtime/test/vk_harness.cpp
+++ b/runtime/test/vk_harness.cpp
@@ -11,6 +11,10 @@
11// its address and passes the heap base, so a V# in VKH_SGPRS can point into it. 11// its address and passes the heap base, so a V# in VKH_SGPRS can point into it.
12// VKH_HEAP_DUMP=<file> after the draw, writes the 64 KiB heap (needs VKH_HEAP, which may be empty-valued as 12// VKH_HEAP_DUMP=<file> after the draw, writes the 64 KiB heap (needs VKH_HEAP, which may be empty-valued as
13// "0=0"), so a test can read what the pixel shader stored or atomically added. 13// "0=0"), so a test can read what the pixel shader stored or atomically added.
14// VKH_DRAW_MESH=<file> + VKH_VERTEX_LAYOUT=0:3f,1:2f
15// draws an indexed mesh instead of the 3-vertex triangle. The file is u32
16// vertex_count, u32 index_count, vertex_count*stride bytes, index_count u32
17// indices; the layout lists location:float-components, one item per attribute.
14// VKH_SUBGROUP_SIZE=32|64 requires that subgroup size of the fragment shader (or of the compute or geometry 18// VKH_SUBGROUP_SIZE=32|64 requires that subgroup size of the fragment shader (or of the compute or geometry
15// stage under test); stderr says "fragment subgroup size: N" when it took effect and 19// stage under test); stderr says "fragment subgroup size: N" when it took effect and
16// "... not available" when the device cannot require it (exit code stays 0). 20// "... not available" when the device cannot require it (exit code stays 0).
@@ -82,6 +86,42 @@ static bool parse_heap(const char* s, std::vector<std::pair<uint32_t, uint32_t>>
82 return true; 86 return true;
83} 87}
84 88
89// VKH_VERTEX_LAYOUT=0:3f,1:2f: one attribute per item, float components only. Location 0 is the first float3.
90struct VertexAttr {
91 uint32_t location, components, offset;
92 VkFormat format;
93};
94
95static bool parse_vertex_layout(const char* s, VertexAttr* attrs, uint32_t& count, uint32_t& stride) {
96 count = 0; stride = 0;
97 while (*s) {
98 uint32_t loc, comps;
99 if (count == 8 || !parse_u32(s, loc) || *s++ != ':' || !parse_u32(s, comps) ||
100 comps < 1 || comps > 4 || *s++ != 'f' || !next_item(s))
101 return false;
102 static const VkFormat formats[4] = {VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32G32_SFLOAT,
103 VK_FORMAT_R32G32B32_SFLOAT, VK_FORMAT_R32G32B32A32_SFLOAT};
104 attrs[count++] = {loc, comps, stride, formats[comps - 1]};
105 stride += comps * 4;
106 }
107 return count > 0;
108}
109
110// VKH_DRAW_MESH file: u32 vertex_count, u32 index_count, vertex_count*stride bytes, index_count u32 indices.
111static bool read_mesh(const char* path, uint32_t stride, std::vector<uint8_t>& vertices,
112 std::vector<uint32_t>& indices) {
113 std::ifstream f(path, std::ios::binary);
114 uint32_t vcount = 0, icount = 0;
115 f.read(reinterpret_cast<char*>(&vcount), 4);
116 f.read(reinterpret_cast<char*>(&icount), 4);
117 if (!f || vcount == 0 || icount == 0 || vcount > 10000000u || icount > 30000000u) return false;
118 vertices.resize(size_t(vcount) * stride);
119 indices.resize(icount);
120 f.read(reinterpret_cast<char*>(vertices.data()), std::streamsize(vertices.size()));
121 f.read(reinterpret_cast<char*>(indices.data()), std::streamsize(indices.size() * 4));
122 return bool(f);
123}
124
85int main(int argc, char** argv) { 125int main(int argc, char** argv) {
86 if (argc < 4) { 126 if (argc < 4) {
87 std::fprintf(stderr, "usage: vk_harness vs.spv fs.spv out.raw [w h [clip-test-s0 clip-test-s1]]\n" 127 std::fprintf(stderr, "usage: vk_harness vs.spv fs.spv out.raw [w h [clip-test-s0 clip-test-s1]]\n"
@@ -113,6 +153,17 @@ int main(int argc, char** argv) {
113 } 153 }
114 // Push constants: s0..s31, heap base (32..33), code base (34..35), --mesh-compute output address (36..37). 154 // Push constants: s0..s31, heap base (32..33), code base (34..35), --mesh-compute output address (36..37).
115 uint32_t pc[38] = {}; 155 uint32_t pc[38] = {};
156 const char* draw_mesh_path = std::getenv("VKH_DRAW_MESH");
157 if (draw_mesh_path && !*draw_mesh_path) draw_mesh_path = nullptr;
158 VertexAttr vertex_attrs[8];
159 uint32_t vertex_attr_count = 0, vertex_stride = 0;
160 if (draw_mesh_path) {
161 const char* layout = std::getenv("VKH_VERTEX_LAYOUT");
162 if (!layout || !parse_vertex_layout(layout, vertex_attrs, vertex_attr_count, vertex_stride)) {
163 std::fprintf(stderr, "VKH_VERTEX_LAYOUT: expected locations like 0:3f,1:2f with VKH_DRAW_MESH\n");
164 return 2;
165 }
166 }
116 const char* sgprs_env = std::getenv("VKH_SGPRS"); 167 const char* sgprs_env = std::getenv("VKH_SGPRS");
117 if (sgprs_env && !parse_sgprs(sgprs_env, pc)) { 168 if (sgprs_env && !parse_sgprs(sgprs_env, pc)) {
118 std::fprintf(stderr, "VKH_SGPRS: expected up to 32 comma-separated words, got \"%s\"\n", sgprs_env); 169 std::fprintf(stderr, "VKH_SGPRS: expected up to 32 comma-separated words, got \"%s\"\n", sgprs_env);
@@ -338,6 +389,7 @@ int main(int argc, char** argv) {
338 LOADD(vkCreateShaderModule); LOADD(vkCreatePipelineLayout); LOADD(vkCreateGraphicsPipelines); 389 LOADD(vkCreateShaderModule); LOADD(vkCreatePipelineLayout); LOADD(vkCreateGraphicsPipelines);
339 LOADD(vkCreateCommandPool); LOADD(vkAllocateCommandBuffers); LOADD(vkBeginCommandBuffer); 390 LOADD(vkCreateCommandPool); LOADD(vkAllocateCommandBuffers); LOADD(vkBeginCommandBuffer);
340 LOADD(vkCmdBeginRenderPass); LOADD(vkCmdBindPipeline); LOADD(vkCmdDraw); LOADD(vkCmdEndRenderPass); 391 LOADD(vkCmdBeginRenderPass); LOADD(vkCmdBindPipeline); LOADD(vkCmdDraw); LOADD(vkCmdEndRenderPass);
392 LOADD(vkCmdBindVertexBuffers); LOADD(vkCmdBindIndexBuffer); LOADD(vkCmdDrawIndexed);
341 LOADD(vkCmdCopyImageToBuffer); LOADD(vkEndCommandBuffer); LOADD(vkQueueSubmit); LOADD(vkQueueWaitIdle); 393 LOADD(vkCmdCopyImageToBuffer); LOADD(vkEndCommandBuffer); LOADD(vkQueueSubmit); LOADD(vkQueueWaitIdle);
342 LOADD(vkCreateBuffer); LOADD(vkGetBufferMemoryRequirements); LOADD(vkBindBufferMemory); LOADD(vkMapMemory); 394 LOADD(vkCreateBuffer); LOADD(vkGetBufferMemoryRequirements); LOADD(vkBindBufferMemory); LOADD(vkMapMemory);
343 VkQueue q; vkGetDeviceQueue(dev, qi, 0, &q); 395 VkQueue q; vkGetDeviceQueue(dev, qi, 0, &q);
@@ -453,6 +505,16 @@ int main(int argc, char** argv) {
453 std::fprintf(stderr, "fragment subgroup size: %u not available\n", req_sg); 505 std::fprintf(stderr, "fragment subgroup size: %u not available\n", req_sg);
454 } 506 }
455 VkPipelineVertexInputStateCreateInfo vin{VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO}; 507 VkPipelineVertexInputStateCreateInfo vin{VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO};
508 VkVertexInputBindingDescription vbind{0, vertex_stride, VK_VERTEX_INPUT_RATE_VERTEX};
509 VkVertexInputAttributeDescription vattr[8]{};
510 if (draw_mesh_path) {
511 for (uint32_t i = 0; i < vertex_attr_count; ++i)
512 vattr[i] = {vertex_attrs[i].location, 0, vertex_attrs[i].format, vertex_attrs[i].offset};
513 vin.vertexBindingDescriptionCount = 1;
514 vin.pVertexBindingDescriptions = &vbind;
515 vin.vertexAttributeDescriptionCount = vertex_attr_count;
516 vin.pVertexAttributeDescriptions = vattr;
517 }
456 VkPipelineInputAssemblyStateCreateInfo ia{VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO}; 518 VkPipelineInputAssemblyStateCreateInfo ia{VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO};
457 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; 519 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
458 VkViewport vp{0, 0, float(W), float(H), 0, 1}; VkRect2D sc{{0, 0}, {W, H}}; 520 VkViewport vp{0, 0, float(W), float(H), 0, 1}; VkRect2D sc{{0, 0}, {W, H}};
@@ -512,6 +574,39 @@ int main(int argc, char** argv) {
512 bai.memoryTypeIndex = memtype(br.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); 574 bai.memoryTypeIndex = memtype(br.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
513 VkDeviceMemory bmem; CHECK(vkAllocateMemory(dev, &bai, nullptr, &bmem)); 575 VkDeviceMemory bmem; CHECK(vkAllocateMemory(dev, &bai, nullptr, &bmem));
514 CHECK(vkBindBufferMemory(dev, buf, bmem, 0)); 576 CHECK(vkBindBufferMemory(dev, buf, bmem, 0));
577 std::vector<uint8_t> mesh_vertices;
578 std::vector<uint32_t> mesh_indices;
579 VkBuffer vbo = VK_NULL_HANDLE, ibo = VK_NULL_HANDLE;
580 VkDeviceMemory vbo_mem = VK_NULL_HANDLE, ibo_mem = VK_NULL_HANDLE;
581 if (draw_mesh_path) {
582 if (!read_mesh(draw_mesh_path, vertex_stride, mesh_vertices, mesh_indices)) {
583 std::fprintf(stderr, "VKH_DRAW_MESH: cannot read %s\n", draw_mesh_path);
584 return 1;
585 }
586 auto host_buffer = [&](VkDeviceSize bytes, VkBufferUsageFlags usage, VkBuffer& b,
587 VkDeviceMemory& m, void*& p) -> bool {
588 VkBufferCreateInfo ci{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
589 ci.size = bytes; ci.usage = usage;
590 if (vkCreateBuffer(dev, &ci, nullptr, &b) != VK_SUCCESS) return false;
591 VkMemoryRequirements rq; vkGetBufferMemoryRequirements(dev, b, &rq);
592 VkMemoryAllocateInfo ai{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
593 ai.allocationSize = rq.size;
594 ai.memoryTypeIndex = memtype(rq.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
595 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
596 return vkAllocateMemory(dev, &ai, nullptr, &m) == VK_SUCCESS &&
597 vkBindBufferMemory(dev, b, m, 0) == VK_SUCCESS &&
598 vkMapMemory(dev, m, 0, bytes, 0, &p) == VK_SUCCESS;
599 };
600 void* vmap = nullptr;
601 void* imap = nullptr;
602 if (!host_buffer(VkDeviceSize(mesh_vertices.size()), VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, vbo, vbo_mem, vmap) ||
603 !host_buffer(VkDeviceSize(mesh_indices.size() * 4), VK_BUFFER_USAGE_INDEX_BUFFER_BIT, ibo, ibo_mem, imap)) {
604 std::fprintf(stderr, "VKH_DRAW_MESH: cannot create the vertex or index buffer\n");
605 return 1;
606 }
607 std::memcpy(vmap, mesh_vertices.data(), mesh_vertices.size());
608 std::memcpy(imap, mesh_indices.data(), mesh_indices.size() * 4);
609 }
515 void* heap_map = nullptr; 610 void* heap_map = nullptr;
516 if (heap_env) { 611 if (heap_env) {
517 LOADD(vkGetBufferDeviceAddress); 612 LOADD(vkGetBufferDeviceAddress);
@@ -602,7 +697,13 @@ int main(int argc, char** argv) {
602 push(cmd, pl, pcs, 0, pc_bytes, pc); 697 push(cmd, pl, pcs, 0, pc_bytes, pc);
603 // VKH_VERTICES=<n>: draw n vertices instead of one triangle, so a vertex shader fills whole waves (TEST-251). 698 // VKH_VERTICES=<n>: draw n vertices instead of one triangle, so a vertex shader fills whole waves (TEST-251).
604 const char* vertices_env = std::getenv("VKH_VERTICES"); 699 const char* vertices_env = std::getenv("VKH_VERTICES");
605 if (const unsigned vertices = vertices_env ? unsigned(std::strtoul(vertices_env, nullptr, 10)) : 0; vertices && !mesh) 700 if (draw_mesh_path) {
701 VkDeviceSize offset = 0;
702 vkCmdBindVertexBuffers(cmd, 0, 1, &vbo, &offset);
703 vkCmdBindIndexBuffer(cmd, ibo, 0, VK_INDEX_TYPE_UINT32);
704 vkCmdDrawIndexed(cmd, uint32_t(mesh_indices.size()), 1, 0, 0, 0);
705 } else if (const unsigned vertices = vertices_env ? unsigned(std::strtoul(vertices_env, nullptr, 10)) : 0;
706 vertices && !mesh)
606 vkCmdDraw(cmd, vertices, 1, 0, 0); 707 vkCmdDraw(cmd, vertices, 1, 0, 0);
607 else if (mesh_compute) 708 else if (mesh_compute)
608 vkCmdDraw(cmd, uint32_t(mesh_groups * mesh_prims * 3), 1, 0, 0); 709 vkCmdDraw(cmd, uint32_t(mesh_groups * mesh_prims * 3), 1, 0, 0);