diff options
| author | Vonsek <45567075+Vonsek@users.noreply.github.com> | 2026-11-22 20:15:34 +0100 |
|---|---|---|
| committer | Vonsek <45567075+Vonsek@users.noreply.github.com> | 2026-11-22 20:15:34 +0100 |
| commit | 4c414d121663c06ab2cbfb04596cc2a39457be77 (patch) | |
| tree | e72d50d0ee8de6ccb4aa3a8d4650013bd69a3fbc /runtime/test/vk_harness.cpp | |
| parent | 162821017ea3c286c8bec860f3ff94f037912061 (diff) | |
tools: ps_oracle.py runs hand-written RDNA2 pixel-shader ISA on AMD GPUs
hw_oracle launches compute kernels only. vk_harness can now write a
pipeline's VK_KHR_pipeline_binary data and build a pipeline from it
(VKH_BINARY_OUT / VKH_BINARY_IN). ps_oracle.py puts assembled ISA in
place of _amdgpu_ps_main in the driver's PAL ELF, raises the pixel
stage's register counts in the msgpack metadata, optionally changes
other metadata entries, and draws with it, reading every pixel's mrt0
words back.
Checked on gfx1036 (driver 26.10.1): v_interp of a varying and the
parameter words P0 / P10 come back as expected. Tests in
tests/test_ps_oracle.py, docs in docs/shader_validation.md.
AI-assisted.
Diffstat (limited to 'runtime/test/vk_harness.cpp')
| -rw-r--r-- | runtime/test/vk_harness.cpp | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/runtime/test/vk_harness.cpp b/runtime/test/vk_harness.cpp index fe9ae194a..c296c9df4 100644 --- a/runtime/test/vk_harness.cpp +++ b/runtime/test/vk_harness.cpp | |||
| @@ -15,6 +15,9 @@ | |||
| 15 | // draws an indexed mesh instead of the 3-vertex triangle. The file is u32 | 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 | 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. | 17 | // indices; the layout lists location:float-components, one item per attribute. |
| 18 | // VKH_BINARY_OUT=<file> writes the pipeline's VK_KHR_pipeline_binary data after building it | ||
| 19 | // VKH_BINARY_IN=<file> builds the pipeline from such a file instead (tools/ps_oracle.py patches the pixel | ||
| 20 | // shader in it); both exit 77 without VK_KHR_pipeline_binary | ||
| 18 | // VKH_SUBGROUP_SIZE=32|64 requires that subgroup size of the fragment shader (or of the compute or geometry | 21 | // VKH_SUBGROUP_SIZE=32|64 requires that subgroup size of the fragment shader (or of the compute or geometry |
| 19 | // stage under test); stderr says "fragment subgroup size: N" when it took effect and | 22 | // stage under test); stderr says "fragment subgroup size: N" when it took effect and |
| 20 | // "... not available" when the device cannot require it (exit code stays 0). | 23 | // "... not available" when the device cannot require it (exit code stays 0). |
| @@ -30,6 +33,7 @@ | |||
| 30 | #include <cstdlib> | 33 | #include <cstdlib> |
| 31 | #include <cstring> | 34 | #include <cstring> |
| 32 | #include <fstream> | 35 | #include <fstream> |
| 36 | #include <iterator> | ||
| 33 | #include <utility> | 37 | #include <utility> |
| 34 | #include <vector> | 38 | #include <vector> |
| 35 | 39 | ||
| @@ -349,6 +353,29 @@ int main(int argc, char** argv) { | |||
| 349 | f12.pNext = chain; | 353 | f12.pNext = chain; |
| 350 | chain = &f12; | 354 | chain = &f12; |
| 351 | } | 355 | } |
| 356 | // VKH_BINARY_OUT=<file> / VKH_BINARY_IN=<file>: VK_KHR_pipeline_binary. OUT writes the pipeline's binaries after | ||
| 357 | // it is built; IN builds the pipeline from such a file instead of compiling the shaders (tools/ps_oracle.py | ||
| 358 | // patches the pixel shader's code in it). File: u32 count, then per binary u32 key size, 32 key bytes, u64 data | ||
| 359 | // size, the data. | ||
| 360 | const char* bin_out = std::getenv("VKH_BINARY_OUT"); | ||
| 361 | const char* bin_in = std::getenv("VKH_BINARY_IN"); | ||
| 362 | VkPhysicalDevicePipelineBinaryFeaturesKHR pbf{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_BINARY_FEATURES_KHR}; | ||
| 363 | VkPhysicalDeviceMaintenance5FeaturesKHR m5{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_5_FEATURES_KHR}; | ||
| 364 | if (bin_out || bin_in) { | ||
| 365 | VkPhysicalDeviceFeatures2 q{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2}; | ||
| 366 | VkPhysicalDevicePipelineBinaryFeaturesKHR pbq{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_BINARY_FEATURES_KHR}; | ||
| 367 | q.pNext = &pbq; | ||
| 368 | vkGetPhysicalDeviceFeatures2(pd, &q); | ||
| 369 | if (!has(VK_KHR_PIPELINE_BINARY_EXTENSION_NAME) || !has(VK_KHR_MAINTENANCE_5_EXTENSION_NAME) || | ||
| 370 | !pbq.pipelineBinaries) { | ||
| 371 | std::fprintf(stderr, "unsupported: no VK_KHR_pipeline_binary\n"); | ||
| 372 | return 77; | ||
| 373 | } | ||
| 374 | enable.push_back(VK_KHR_PIPELINE_BINARY_EXTENSION_NAME); | ||
| 375 | enable.push_back(VK_KHR_MAINTENANCE_5_EXTENSION_NAME); | ||
| 376 | pbf.pipelineBinaries = VK_TRUE; pbf.pNext = chain; chain = &pbf; | ||
| 377 | m5.maintenance5 = VK_TRUE; m5.pNext = chain; chain = &m5; | ||
| 378 | } | ||
| 352 | for (auto e : enable) std::fprintf(stderr, "enabled %s\n", e); | 379 | for (auto e : enable) std::fprintf(stderr, "enabled %s\n", e); |
| 353 | VkDeviceCreateInfo dci{VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO}; | 380 | VkDeviceCreateInfo dci{VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO}; |
| 354 | dci.queueCreateInfoCount = 1; dci.pQueueCreateInfos = &qci; | 381 | dci.queueCreateInfoCount = 1; dci.pQueueCreateInfos = &qci; |
| @@ -554,7 +581,76 @@ int main(int argc, char** argv) { | |||
| 554 | gpi.pDepthStencilState = &ds_state; | 581 | gpi.pDepthStencilState = &ds_state; |
| 555 | } | 582 | } |
| 556 | gpi.layout = pl; gpi.renderPass = rp; | 583 | gpi.layout = pl; gpi.renderPass = rp; |
| 584 | VkPipelineCreateFlags2CreateInfoKHR capture{VK_STRUCTURE_TYPE_PIPELINE_CREATE_FLAGS_2_CREATE_INFO_KHR}; | ||
| 585 | std::vector<VkPipelineBinaryKHR> in_bins; | ||
| 586 | VkPipelineBinaryInfoKHR bin_info{VK_STRUCTURE_TYPE_PIPELINE_BINARY_INFO_KHR}; | ||
| 587 | if (bin_out) { | ||
| 588 | capture.flags = VK_PIPELINE_CREATE_2_CAPTURE_DATA_BIT_KHR; | ||
| 589 | gpi.pNext = &capture; | ||
| 590 | } | ||
| 591 | if (bin_in) { | ||
| 592 | auto vkCreatePipelineBinariesKHR = reinterpret_cast<PFN_vkCreatePipelineBinariesKHR>( | ||
| 593 | vkGetDeviceProcAddr(dev, "vkCreatePipelineBinariesKHR")); | ||
| 594 | std::ifstream f(bin_in, std::ios::binary); | ||
| 595 | std::vector<char> all((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>()); | ||
| 596 | size_t at = 0; | ||
| 597 | auto take = [&](void* p, size_t n) { | ||
| 598 | if (at + n > all.size()) { std::fprintf(stderr, "%s: truncated\n", bin_in); std::exit(2); } | ||
| 599 | std::memcpy(p, all.data() + at, n); at += n; | ||
| 600 | }; | ||
| 601 | uint32_t count = 0; take(&count, 4); | ||
| 602 | std::vector<VkPipelineBinaryKeyKHR> keys(count, {VK_STRUCTURE_TYPE_PIPELINE_BINARY_KEY_KHR}); | ||
| 603 | std::vector<VkPipelineBinaryDataKHR> datas(count); | ||
| 604 | std::vector<std::vector<char>> blobs(count); | ||
| 605 | for (uint32_t i = 0; i < count; i++) { | ||
| 606 | take(&keys[i].keySize, 4); take(keys[i].key, VK_MAX_PIPELINE_BINARY_KEY_SIZE_KHR); | ||
| 607 | uint64_t n = 0; take(&n, 8); | ||
| 608 | blobs[i].resize(size_t(n)); take(blobs[i].data(), size_t(n)); | ||
| 609 | datas[i].dataSize = size_t(n); datas[i].pData = blobs[i].data(); | ||
| 610 | } | ||
| 611 | VkPipelineBinaryKeysAndDataKHR kd{count, keys.data(), datas.data()}; | ||
| 612 | VkPipelineBinaryCreateInfoKHR bci{VK_STRUCTURE_TYPE_PIPELINE_BINARY_CREATE_INFO_KHR}; | ||
| 613 | bci.pKeysAndDataInfo = &kd; | ||
| 614 | in_bins.resize(count); | ||
| 615 | VkPipelineBinaryHandlesInfoKHR hi{VK_STRUCTURE_TYPE_PIPELINE_BINARY_HANDLES_INFO_KHR}; | ||
| 616 | hi.pipelineBinaryCount = count; hi.pPipelineBinaries = in_bins.data(); | ||
| 617 | CHECK(vkCreatePipelineBinariesKHR(dev, &bci, nullptr, &hi)); | ||
| 618 | bin_info.binaryCount = count; bin_info.pPipelineBinaries = in_bins.data(); | ||
| 619 | gpi.pNext = &bin_info; | ||
| 620 | std::fprintf(stderr, "pipeline from %u binaries in %s\n", count, bin_in); | ||
| 621 | } | ||
| 557 | VkPipeline pipe; CHECK(vkCreateGraphicsPipelines(dev, VK_NULL_HANDLE, 1, &gpi, nullptr, &pipe)); | 622 | VkPipeline pipe; CHECK(vkCreateGraphicsPipelines(dev, VK_NULL_HANDLE, 1, &gpi, nullptr, &pipe)); |
| 623 | if (bin_out) { | ||
| 624 | auto vkCreatePipelineBinariesKHR = reinterpret_cast<PFN_vkCreatePipelineBinariesKHR>( | ||
| 625 | vkGetDeviceProcAddr(dev, "vkCreatePipelineBinariesKHR")); | ||
| 626 | auto vkGetPipelineBinaryDataKHR = reinterpret_cast<PFN_vkGetPipelineBinaryDataKHR>( | ||
| 627 | vkGetDeviceProcAddr(dev, "vkGetPipelineBinaryDataKHR")); | ||
| 628 | VkPipelineBinaryCreateInfoKHR bci{VK_STRUCTURE_TYPE_PIPELINE_BINARY_CREATE_INFO_KHR}; | ||
| 629 | bci.pipeline = pipe; | ||
| 630 | VkPipelineBinaryHandlesInfoKHR hi{VK_STRUCTURE_TYPE_PIPELINE_BINARY_HANDLES_INFO_KHR}; | ||
| 631 | CHECK(vkCreatePipelineBinariesKHR(dev, &bci, nullptr, &hi)); | ||
| 632 | std::vector<VkPipelineBinaryKHR> bins(hi.pipelineBinaryCount); | ||
| 633 | hi.pPipelineBinaries = bins.data(); | ||
| 634 | CHECK(vkCreatePipelineBinariesKHR(dev, &bci, nullptr, &hi)); | ||
| 635 | std::ofstream f(bin_out, std::ios::binary); | ||
| 636 | uint32_t count = uint32_t(bins.size()); | ||
| 637 | f.write(reinterpret_cast<const char*>(&count), 4); | ||
| 638 | for (auto b : bins) { | ||
| 639 | VkPipelineBinaryDataInfoKHR di{VK_STRUCTURE_TYPE_PIPELINE_BINARY_DATA_INFO_KHR}; | ||
| 640 | di.pipelineBinary = b; | ||
| 641 | VkPipelineBinaryKeyKHR key{VK_STRUCTURE_TYPE_PIPELINE_BINARY_KEY_KHR}; | ||
| 642 | size_t n = 0; | ||
| 643 | CHECK(vkGetPipelineBinaryDataKHR(dev, &di, &key, &n, nullptr)); | ||
| 644 | std::vector<char> data(n); | ||
| 645 | CHECK(vkGetPipelineBinaryDataKHR(dev, &di, &key, &n, data.data())); | ||
| 646 | uint64_t n64 = n; | ||
| 647 | f.write(reinterpret_cast<const char*>(&key.keySize), 4); | ||
| 648 | f.write(reinterpret_cast<const char*>(key.key), VK_MAX_PIPELINE_BINARY_KEY_SIZE_KHR); | ||
| 649 | f.write(reinterpret_cast<const char*>(&n64), 8); | ||
| 650 | f.write(data.data(), std::streamsize(n)); | ||
| 651 | } | ||
| 652 | std::fprintf(stderr, "wrote %u pipeline binaries to %s\n", count, bin_out); | ||
| 653 | } | ||
| 558 | VkPipeline cpipe = VK_NULL_HANDLE; | 654 | VkPipeline cpipe = VK_NULL_HANDLE; |
| 559 | if (mesh_compute) { | 655 | if (mesh_compute) { |
| 560 | VkComputePipelineCreateInfo cpi{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO}; | 656 | VkComputePipelineCreateInfo cpi{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO}; |