diff options
| author | Taffyyy-ops <359860351+Taffyyy-ops@users.noreply.github.com> | 2026-11-24 15:11:59 +0100 |
|---|---|---|
| committer | Taffyyy-ops <359860351+Taffyyy-ops@users.noreply.github.com> | 2026-11-24 18:08:29 +0100 |
| commit | e00d9d05bc065234ce68aa2db829a6cf8ea2b389 (patch) | |
| tree | fb47dd1ce5f1821051340aea4c525bb242180d13 /runtime | |
| parent | fce586c6d6f74160eccff015d9806e179188d862 (diff) | |
vk_pipeline_check: match runtime device features and restore pipeline_cache_seed test (RT-PIPELINE-CACHE-SEED)
Restores the vk_pipeline_check / runtime Vulkan device cache-key parity fix from
ancestor commit c8a102d0cf (Senne Cools), which had been dropped in bulk merge
6dece9231, and extends it for NVIDIA Blackwell (RTX 5060, driver 621.24) + #577:
- Disable robustBufferAccess, scalarBlockLayout and robustImageAccess on
vk_pipeline_check's VkDevice to match create_vulkan_device_standalone.
- Always enable VK_EXT_mesh_shader and VK_KHR_maintenance8 when supported
(rather than only when selected shaders request them) and keep
meshShaderQueries as supported (matching #577 in d3d12_vulkan_device.cpp),
because NVIDIA hashes both into pipeline cache keys even for compute shaders.
- Restore and register the pipeline_cache_seed CTest with a self-contained
pipeline_cache_seed_test that embeds a minimal synthetic compute SPIR-V module
(no glslangValidator required) and runs vk_pipeline_check under its own exe
filename.
- Update docs/pipeline_check.md ("The cache and the runtime's device").
AI-assisted: yes
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/CMakeLists.txt | 11 | ||||
| -rw-r--r-- | runtime/test/pipeline_cache_seed_test.cpp | 418 | ||||
| -rw-r--r-- | runtime/test/vk_pipeline_check.cpp | 34 |
3 files changed, 450 insertions, 13 deletions
diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 3b334c713..7faeead6d 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt | |||
| @@ -2219,6 +2219,17 @@ if(TARGET Vulkan::Headers) | |||
| 2219 | set_tests_properties(d3d12_indirect_dispatch_count PROPERTIES SKIP_RETURN_CODE 77) | 2219 | set_tests_properties(d3d12_indirect_dispatch_count PROPERTIES SKIP_RETURN_CODE 77) |
| 2220 | endif() | 2220 | endif() |
| 2221 | 2221 | ||
| 2222 | # The cache vk_pipeline_check saves has to hit on the runtime's device (docs/pipeline_check.md): the test | ||
| 2223 | # compiles an embedded synthetic compute shader through a same-named copy of the checker, then creates the | ||
| 2224 | # same pipeline on the runtime's device from the saved cache. | ||
| 2225 | add_executable(pipeline_cache_seed_test test/pipeline_cache_seed_test.cpp) | ||
| 2226 | add_dependencies(pipeline_cache_seed_test vk_pipeline_check) | ||
| 2227 | target_link_libraries(pipeline_cache_seed_test PRIVATE gta6rt_d3d12_com) | ||
| 2228 | target_compile_definitions(pipeline_cache_seed_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) | ||
| 2229 | target_compile_options(pipeline_cache_seed_test PRIVATE /W4 /permissive- /utf-8) | ||
| 2230 | add_test(NAME pipeline_cache_seed COMMAND pipeline_cache_seed_test $<TARGET_FILE:vk_pipeline_check>) | ||
| 2231 | set_tests_properties(pipeline_cache_seed PROPERTIES SKIP_RETURN_CODE 77) | ||
| 2232 | |||
| 2222 | # What Close returns after one recorded call, against WARP; today's differences are listed in the test. | 2233 | # What Close returns after one recorded call, against WARP; today's differences are listed in the test. |
| 2223 | add_executable(d3d12_close_parity_test test/d3d12_close_parity_test.cpp) | 2234 | add_executable(d3d12_close_parity_test test/d3d12_close_parity_test.cpp) |
| 2224 | target_link_libraries(d3d12_close_parity_test PRIVATE gta6rt_d3d12_com d3d12 dxgi) | 2235 | target_link_libraries(d3d12_close_parity_test PRIVATE gta6rt_d3d12_com d3d12 dxgi) |
diff --git a/runtime/test/pipeline_cache_seed_test.cpp b/runtime/test/pipeline_cache_seed_test.cpp new file mode 100644 index 000000000..782a0707c --- /dev/null +++ b/runtime/test/pipeline_cache_seed_test.cpp | |||
| @@ -0,0 +1,418 @@ | |||
| 1 | // The cache vk_pipeline_check saves has to hit on the runtime's device: | ||
| 2 | // pipeline_cache_seed_test <vk_pipeline_check.exe> | ||
| 3 | // | ||
| 4 | // Writes a synthetic minimal compute SPIR-V module and index.json to a temporary directory, runs a copy of | ||
| 5 | // vk_pipeline_check named after this test executable (a driver can make the executable file name part of the pipeline | ||
| 6 | // cache key, see docs/pipeline_check.md), and then creates the exact same pipeline on the runtime's own device | ||
| 7 | // (create_vulkan_device_standalone) from the saved cache. Passes when Vulkan creation feedback reports an | ||
| 8 | // application pipeline cache hit. | ||
| 9 | // | ||
| 10 | // Exit 77 (skipped): no Vulkan device for the runtime, pre-Vulkan 1.3 device without core pipeline creation feedback, | ||
| 11 | // vk_pipeline_check built no pipeline, cache written for another GPU/driver, or a driver that reports no hit even for | ||
| 12 | // a cache this device wrote itself a moment before. | ||
| 13 | #include "../shims/d3d12_com.h" | ||
| 14 | #include "../src/shader_library.h" | ||
| 15 | |||
| 16 | #include <wrl/client.h> | ||
| 17 | |||
| 18 | #include <array> | ||
| 19 | #include <cstdint> | ||
| 20 | #include <cstdio> | ||
| 21 | #include <cstring> | ||
| 22 | #include <filesystem> | ||
| 23 | #include <fstream> | ||
| 24 | #include <iterator> | ||
| 25 | #include <string> | ||
| 26 | #include <system_error> | ||
| 27 | #include <vector> | ||
| 28 | |||
| 29 | namespace { | ||
| 30 | |||
| 31 | // Minimal valid SPIR-V 1.0 compute shader: `void main() {}` with LocalSize(1, 1, 1). | ||
| 32 | constexpr std::array<std::uint32_t, 35> kMinimalComputeSpv = { | ||
| 33 | 0x07230203u, 0x00010000u, 0u, 6u, 0u, | ||
| 34 | (2u << 16) | 17u, 1u, // OpCapability Shader | ||
| 35 | (3u << 16) | 14u, 0u, 1u, // OpMemoryModel Logical GLSL450 | ||
| 36 | (5u << 16) | 15u, 5u, 4u, 0x6e69616du, 0x00000000u, // OpEntryPoint GLCompute %4 "main" | ||
| 37 | (6u << 16) | 16u, 4u, 17u, 1u, 1u, 1u, // OpExecutionMode %4 LocalSize 1 1 1 | ||
| 38 | (2u << 16) | 19u, 2u, // %2 = OpTypeVoid | ||
| 39 | (3u << 16) | 33u, 3u, 2u, // %3 = OpTypeFunction %2 | ||
| 40 | (5u << 16) | 54u, 2u, 4u, 0u, 3u, // %4 = OpFunction %2 None %3 | ||
| 41 | (2u << 16) | 248u, 5u, // %5 = OpLabel | ||
| 42 | (1u << 16) | 253u, // OpReturn | ||
| 43 | (1u << 16) | 56u, // OpFunctionEnd | ||
| 44 | }; | ||
| 45 | |||
| 46 | struct TempDirGuard { | ||
| 47 | std::filesystem::path path; | ||
| 48 | ~TempDirGuard() { | ||
| 49 | if (!path.empty()) { | ||
| 50 | std::error_code ec; | ||
| 51 | std::filesystem::remove_all(path, ec); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | }; | ||
| 55 | |||
| 56 | struct UniqueHandle { | ||
| 57 | HANDLE h = nullptr; | ||
| 58 | UniqueHandle() = default; | ||
| 59 | explicit UniqueHandle(HANDLE handle) : h(handle) {} | ||
| 60 | ~UniqueHandle() { reset(); } | ||
| 61 | UniqueHandle(const UniqueHandle&) = delete; | ||
| 62 | UniqueHandle& operator=(const UniqueHandle&) = delete; | ||
| 63 | void reset(HANDLE next = nullptr) { | ||
| 64 | if (h && h != INVALID_HANDLE_VALUE) CloseHandle(h); | ||
| 65 | h = next; | ||
| 66 | } | ||
| 67 | HANDLE get() const { return h; } | ||
| 68 | }; | ||
| 69 | |||
| 70 | struct VulkanObjects { | ||
| 71 | VkDevice device = VK_NULL_HANDLE; | ||
| 72 | PFN_vkDestroyShaderModule destroy_module = nullptr; | ||
| 73 | PFN_vkDestroyPipelineLayout destroy_layout = nullptr; | ||
| 74 | PFN_vkDestroyPipelineCache destroy_cache = nullptr; | ||
| 75 | VkShaderModule shader = VK_NULL_HANDLE; | ||
| 76 | VkPipelineLayout layout = VK_NULL_HANDLE; | ||
| 77 | VkPipelineCache seeded = VK_NULL_HANDLE; | ||
| 78 | VkPipelineCache first = VK_NULL_HANDLE; | ||
| 79 | VkPipelineCache second = VK_NULL_HANDLE; | ||
| 80 | ~VulkanObjects() { | ||
| 81 | if (!device) return; | ||
| 82 | if (second && destroy_cache) destroy_cache(device, second, nullptr); | ||
| 83 | if (first && destroy_cache) destroy_cache(device, first, nullptr); | ||
| 84 | if (seeded && destroy_cache) destroy_cache(device, seeded, nullptr); | ||
| 85 | if (layout && destroy_layout) destroy_layout(device, layout, nullptr); | ||
| 86 | if (shader && destroy_module) destroy_module(device, shader, nullptr); | ||
| 87 | } | ||
| 88 | }; | ||
| 89 | |||
| 90 | struct Created { | ||
| 91 | bool ok = false; | ||
| 92 | bool hit = false; | ||
| 93 | }; | ||
| 94 | |||
| 95 | std::vector<char> read_file(const std::filesystem::path& path) { | ||
| 96 | std::ifstream file(path, std::ios::binary); | ||
| 97 | return {std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()}; | ||
| 98 | } | ||
| 99 | |||
| 100 | bool write_bytes(const std::filesystem::path& path, const void* data, std::size_t size) { | ||
| 101 | std::ofstream file(path, std::ios::binary | std::ios::trunc); | ||
| 102 | if (!file) return false; | ||
| 103 | file.write(static_cast<const char*>(data), static_cast<std::streamsize>(size)); | ||
| 104 | file.flush(); | ||
| 105 | return file.good(); | ||
| 106 | } | ||
| 107 | |||
| 108 | std::filesystem::path create_temp_dir() { | ||
| 109 | std::wstring base(MAX_PATH, L'\0'); | ||
| 110 | const DWORD len = GetTempPathW(static_cast<DWORD>(base.size()), base.data()); | ||
| 111 | std::filesystem::path root; | ||
| 112 | if (len > 0 && len < base.size()) { | ||
| 113 | base.resize(len); | ||
| 114 | root = base; | ||
| 115 | } else { | ||
| 116 | std::error_code ec; | ||
| 117 | root = std::filesystem::temp_directory_path(ec); | ||
| 118 | if (ec) return {}; | ||
| 119 | } | ||
| 120 | const std::wstring name = | ||
| 121 | L"leonida_pipeline_cache_seed_" + std::to_wstring(GetCurrentProcessId()) + L"_" + | ||
| 122 | std::to_wstring(GetTickCount64()); | ||
| 123 | const std::filesystem::path dir = root / name; | ||
| 124 | std::error_code ec; | ||
| 125 | std::filesystem::remove_all(dir, ec); | ||
| 126 | if (!std::filesystem::create_directories(dir, ec) && !std::filesystem::is_directory(dir, ec)) return {}; | ||
| 127 | return dir; | ||
| 128 | } | ||
| 129 | |||
| 130 | std::wstring self_filename() { | ||
| 131 | std::wstring path(MAX_PATH, L'\0'); | ||
| 132 | for (;;) { | ||
| 133 | const DWORD len = GetModuleFileNameW(nullptr, path.data(), static_cast<DWORD>(path.size())); | ||
| 134 | if (len == 0) return {}; | ||
| 135 | if (len < path.size()) { | ||
| 136 | path.resize(len); | ||
| 137 | return std::filesystem::path(path).filename().native(); | ||
| 138 | } | ||
| 139 | if (path.size() >= 32768) return {}; | ||
| 140 | path.resize(path.size() * 2); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | void append_quoted_arg(std::wstring& out, const std::wstring& arg) { | ||
| 145 | if (!out.empty()) out.push_back(L' '); | ||
| 146 | out.push_back(L'"'); | ||
| 147 | std::size_t backslashes = 0; | ||
| 148 | for (wchar_t ch : arg) { | ||
| 149 | if (ch == L'\\') { | ||
| 150 | ++backslashes; | ||
| 151 | } else if (ch == L'"') { | ||
| 152 | out.append(backslashes * 2 + 1, L'\\'); | ||
| 153 | out.push_back(L'"'); | ||
| 154 | backslashes = 0; | ||
| 155 | } else { | ||
| 156 | out.append(backslashes, L'\\'); | ||
| 157 | backslashes = 0; | ||
| 158 | out.push_back(ch); | ||
| 159 | } | ||
| 160 | } | ||
| 161 | out.append(backslashes * 2, L'\\'); | ||
| 162 | out.push_back(L'"'); | ||
| 163 | } | ||
| 164 | |||
| 165 | bool run_checker_process(const std::filesystem::path& exe, const std::filesystem::path& spirv_dir, | ||
| 166 | const std::filesystem::path& cache_path, const std::filesystem::path& log_path, | ||
| 167 | DWORD& exit_code) { | ||
| 168 | SECURITY_ATTRIBUTES sa{}; | ||
| 169 | sa.nLength = sizeof(sa); | ||
| 170 | sa.bInheritHandle = TRUE; | ||
| 171 | UniqueHandle log_file(CreateFileW(log_path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, &sa, CREATE_ALWAYS, | ||
| 172 | FILE_ATTRIBUTE_NORMAL, nullptr)); | ||
| 173 | if (log_file.get() == INVALID_HANDLE_VALUE) return false; | ||
| 174 | |||
| 175 | std::wstring cmd_line; | ||
| 176 | append_quoted_arg(cmd_line, exe.native()); | ||
| 177 | append_quoted_arg(cmd_line, spirv_dir.native()); | ||
| 178 | append_quoted_arg(cmd_line, cache_path.native()); | ||
| 179 | append_quoted_arg(cmd_line, L"--jobs"); | ||
| 180 | append_quoted_arg(cmd_line, L"1"); | ||
| 181 | |||
| 182 | STARTUPINFOW si{}; | ||
| 183 | si.cb = sizeof(si); | ||
| 184 | si.dwFlags = STARTF_USESTDHANDLES; | ||
| 185 | si.hStdOutput = log_file.get(); | ||
| 186 | si.hStdError = log_file.get(); | ||
| 187 | |||
| 188 | PROCESS_INFORMATION pi{}; | ||
| 189 | const BOOL started = | ||
| 190 | CreateProcessW(exe.c_str(), cmd_line.data(), nullptr, nullptr, TRUE, 0, nullptr, nullptr, &si, &pi); | ||
| 191 | log_file.reset(); | ||
| 192 | if (!started) return false; | ||
| 193 | |||
| 194 | UniqueHandle proc(pi.hProcess); | ||
| 195 | UniqueHandle thread(pi.hThread); | ||
| 196 | const DWORD wait = WaitForSingleObject(proc.get(), 120000); | ||
| 197 | if (wait != WAIT_OBJECT_0) { | ||
| 198 | TerminateProcess(proc.get(), 1); | ||
| 199 | WaitForSingleObject(proc.get(), 5000); | ||
| 200 | return false; | ||
| 201 | } | ||
| 202 | return GetExitCodeProcess(proc.get(), &exit_code) != FALSE; | ||
| 203 | } | ||
| 204 | |||
| 205 | } // namespace | ||
| 206 | |||
| 207 | int wmain(int argc, wchar_t** argv) { | ||
| 208 | if (argc != 2 || !argv[1] || !*argv[1]) { | ||
| 209 | std::fprintf(stderr, "usage: pipeline_cache_seed_test <vk_pipeline_check.exe>\n"); | ||
| 210 | return 2; | ||
| 211 | } | ||
| 212 | const std::filesystem::path checker_src(argv[1]); | ||
| 213 | std::error_code ec; | ||
| 214 | if (!std::filesystem::is_regular_file(checker_src, ec)) { | ||
| 215 | std::fprintf(stderr, "checker executable not found\n"); | ||
| 216 | return 1; | ||
| 217 | } | ||
| 218 | |||
| 219 | TempDirGuard temp; | ||
| 220 | temp.path = create_temp_dir(); | ||
| 221 | if (temp.path.empty()) { | ||
| 222 | std::fprintf(stderr, "failed to create temporary directory\n"); | ||
| 223 | return 1; | ||
| 224 | } | ||
| 225 | |||
| 226 | Microsoft::WRL::ComPtr<ID3D12Device> d3d; | ||
| 227 | if (FAILED(rt::d3d12::create_vulkan_device_standalone(IID_PPV_ARGS(&d3d))) || !d3d) { | ||
| 228 | std::printf("SKIP: no Vulkan device for the runtime\n"); | ||
| 229 | return 77; | ||
| 230 | } | ||
| 231 | const rt::d3d12::VulkanDeviceInfo* info = rt::d3d12::vulkan_device_info(d3d.Get()); | ||
| 232 | if (!info || !info->get_instance_proc) return 1; | ||
| 233 | const VkDevice device = info->device; | ||
| 234 | const auto device_proc = reinterpret_cast<PFN_vkGetDeviceProcAddr>( | ||
| 235 | info->get_instance_proc(info->instance, "vkGetDeviceProcAddr")); | ||
| 236 | const auto physical_properties = reinterpret_cast<PFN_vkGetPhysicalDeviceProperties>( | ||
| 237 | info->get_instance_proc(info->instance, "vkGetPhysicalDeviceProperties")); | ||
| 238 | if (!device_proc || !physical_properties) return 1; | ||
| 239 | |||
| 240 | #define LOAD(name) const auto name = reinterpret_cast<PFN_##name>(device_proc(device, #name)) | ||
| 241 | LOAD(vkCreateShaderModule); | ||
| 242 | LOAD(vkDestroyShaderModule); | ||
| 243 | LOAD(vkCreatePipelineLayout); | ||
| 244 | LOAD(vkDestroyPipelineLayout); | ||
| 245 | LOAD(vkCreatePipelineCache); | ||
| 246 | LOAD(vkDestroyPipelineCache); | ||
| 247 | LOAD(vkGetPipelineCacheData); | ||
| 248 | LOAD(vkCreateComputePipelines); | ||
| 249 | LOAD(vkDestroyPipeline); | ||
| 250 | #undef LOAD | ||
| 251 | if (!vkCreateShaderModule || !vkDestroyShaderModule || !vkCreatePipelineLayout || !vkDestroyPipelineLayout || | ||
| 252 | !vkCreatePipelineCache || !vkDestroyPipelineCache || !vkGetPipelineCacheData || !vkCreateComputePipelines || | ||
| 253 | !vkDestroyPipeline) { | ||
| 254 | return 1; | ||
| 255 | } | ||
| 256 | |||
| 257 | VkPhysicalDeviceProperties properties{}; | ||
| 258 | physical_properties(info->physical, &properties); | ||
| 259 | if (properties.apiVersion < VK_API_VERSION_1_3) { | ||
| 260 | std::printf("SKIP: %s is pre-Vulkan 1.3 (no core pipeline creation feedback)\n", properties.deviceName); | ||
| 261 | return 77; | ||
| 262 | } | ||
| 263 | |||
| 264 | const std::filesystem::path spv_path = temp.path / L"seed.spv"; | ||
| 265 | const std::filesystem::path index_path = temp.path / L"index.json"; | ||
| 266 | const std::filesystem::path cache_path = temp.path / L"pipeline_cache.bin"; | ||
| 267 | const std::filesystem::path log_path = temp.path / L"checker.log"; | ||
| 268 | const std::filesystem::path checker_dir = temp.path / L"checker"; | ||
| 269 | const std::wstring self_name = self_filename(); | ||
| 270 | if (self_name.empty()) { | ||
| 271 | std::fprintf(stderr, "GetModuleFileNameW failed\n"); | ||
| 272 | return 1; | ||
| 273 | } | ||
| 274 | if (!std::filesystem::create_directories(checker_dir, ec) && !std::filesystem::is_directory(checker_dir, ec)) { | ||
| 275 | std::fprintf(stderr, "failed to create checker staging directory\n"); | ||
| 276 | return 1; | ||
| 277 | } | ||
| 278 | const std::filesystem::path checker_copy = checker_dir / self_name; | ||
| 279 | // CopyFileW rather than std::filesystem::copy_file: Wine 9 lacks CopyFile2. | ||
| 280 | if (!CopyFileW(checker_src.c_str(), checker_copy.c_str(), FALSE)) { | ||
| 281 | std::fprintf(stderr, "CopyFileW failed (%lu)\n", static_cast<unsigned long>(GetLastError())); | ||
| 282 | return 1; | ||
| 283 | } | ||
| 284 | if (!write_bytes(spv_path, kMinimalComputeSpv.data(), kMinimalComputeSpv.size() * sizeof(std::uint32_t))) { | ||
| 285 | std::fprintf(stderr, "failed to write synthetic compute shader\n"); | ||
| 286 | return 1; | ||
| 287 | } | ||
| 288 | static_assert(sizeof(leonida::ShaderUserData) == 144, | ||
| 289 | "synthetic index.json push_constant_bytes must match ShaderUserData"); | ||
| 290 | const char index_json[] = | ||
| 291 | "{\"seed\":{\"stage\":\"cs\",\"bindings\":[],\"push_constant_bytes\":144," | ||
| 292 | "\"local_size\":[1,1,1],\"wave64\":false}}\n"; | ||
| 293 | if (!write_bytes(index_path, index_json, sizeof(index_json) - 1)) { | ||
| 294 | std::fprintf(stderr, "failed to write synthetic index.json\n"); | ||
| 295 | return 1; | ||
| 296 | } | ||
| 297 | |||
| 298 | DWORD checker_exit = 1; | ||
| 299 | if (!run_checker_process(checker_copy, temp.path, cache_path, log_path, checker_exit)) { | ||
| 300 | std::fprintf(stderr, "failed to run copied vk_pipeline_check process\n"); | ||
| 301 | return 1; | ||
| 302 | } | ||
| 303 | const std::vector<char> log_bytes = read_file(log_path); | ||
| 304 | const std::string log_text(log_bytes.begin(), log_bytes.end()); | ||
| 305 | const bool built_ok = log_text.find("cs pipelines: 1 ok, 0 failed, 0 skipped") != std::string::npos; | ||
| 306 | const bool has_cache = std::filesystem::is_regular_file(cache_path, ec); | ||
| 307 | if (!built_ok || !has_cache) { | ||
| 308 | if (log_text.find("no Vulkan physical device") != std::string::npos || | ||
| 309 | log_text.find("vulkan-1") != std::string::npos || | ||
| 310 | log_text.find("libvulkan") != std::string::npos || | ||
| 311 | log_text.find("cs pipelines: 0 ok, 0 failed, 1 skipped") != std::string::npos) { | ||
| 312 | std::printf("SKIP: vk_pipeline_check built no pipeline here\n"); | ||
| 313 | return 77; | ||
| 314 | } | ||
| 315 | std::fprintf(stderr, "vk_pipeline_check did not build the pipeline (exit %lu)\n%s\n", | ||
| 316 | static_cast<unsigned long>(checker_exit), log_text.c_str()); | ||
| 317 | return 1; | ||
| 318 | } | ||
| 319 | |||
| 320 | const std::vector<char> seed = read_file(cache_path); | ||
| 321 | if (seed.size() < sizeof(VkPipelineCacheHeaderVersionOne)) { | ||
| 322 | std::fprintf(stderr, "pipeline_cache.bin is too small (%zu bytes)\n", seed.size()); | ||
| 323 | return 1; | ||
| 324 | } | ||
| 325 | VkPipelineCacheHeaderVersionOne header{}; | ||
| 326 | std::memcpy(&header, seed.data(), sizeof(header)); | ||
| 327 | if (header.vendorID != properties.vendorID || header.deviceID != properties.deviceID || | ||
| 328 | std::memcmp(header.pipelineCacheUUID, properties.pipelineCacheUUID, VK_UUID_SIZE) != 0) { | ||
| 329 | std::printf("SKIP: pipeline_cache.bin was written for another GPU or driver than the runtime's %s\n", | ||
| 330 | properties.deviceName); | ||
| 331 | return 77; | ||
| 332 | } | ||
| 333 | |||
| 334 | VulkanObjects vk; | ||
| 335 | vk.device = device; | ||
| 336 | vk.destroy_module = vkDestroyShaderModule; | ||
| 337 | vk.destroy_layout = vkDestroyPipelineLayout; | ||
| 338 | vk.destroy_cache = vkDestroyPipelineCache; | ||
| 339 | |||
| 340 | VkShaderModuleCreateInfo module_info{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO}; | ||
| 341 | module_info.codeSize = kMinimalComputeSpv.size() * sizeof(std::uint32_t); | ||
| 342 | module_info.pCode = kMinimalComputeSpv.data(); | ||
| 343 | if (vkCreateShaderModule(device, &module_info, nullptr, &vk.shader) != VK_SUCCESS) return 1; | ||
| 344 | |||
| 345 | const VkPushConstantRange push{VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(leonida::ShaderUserData)}; | ||
| 346 | VkPipelineLayoutCreateInfo layout_info{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO}; | ||
| 347 | layout_info.pushConstantRangeCount = 1; | ||
| 348 | layout_info.pPushConstantRanges = &push; | ||
| 349 | if (vkCreatePipelineLayout(device, &layout_info, nullptr, &vk.layout) != VK_SUCCESS) return 1; | ||
| 350 | |||
| 351 | const auto make_cache = [&](const void* data, std::size_t size) { | ||
| 352 | VkPipelineCacheCreateInfo cache_info{VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO}; | ||
| 353 | cache_info.initialDataSize = size; | ||
| 354 | cache_info.pInitialData = data; | ||
| 355 | VkPipelineCache cache = VK_NULL_HANDLE; | ||
| 356 | if (vkCreatePipelineCache(device, &cache_info, nullptr, &cache) != VK_SUCCESS) return VkPipelineCache{}; | ||
| 357 | return cache; | ||
| 358 | }; | ||
| 359 | |||
| 360 | leonida::ShaderEntry entry; | ||
| 361 | entry.stage = "cs"; | ||
| 362 | entry.local_size = {1, 1, 1}; | ||
| 363 | const auto create = [&](VkPipelineCache cache) { | ||
| 364 | VkPipelineCreationFeedback feedback{}; | ||
| 365 | const VkPipelineCreationFeedbackCreateInfo feedback_info{ | ||
| 366 | VK_STRUCTURE_TYPE_PIPELINE_CREATION_FEEDBACK_CREATE_INFO, nullptr, &feedback, 0, nullptr}; | ||
| 367 | VkComputePipelineCreateInfo pipeline_info{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, &feedback_info}; | ||
| 368 | pipeline_info.stage = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO}; | ||
| 369 | pipeline_info.stage.stage = VK_SHADER_STAGE_COMPUTE_BIT; | ||
| 370 | pipeline_info.stage.module = vk.shader; | ||
| 371 | pipeline_info.stage.pName = "main"; | ||
| 372 | VkPipelineShaderStageRequiredSubgroupSizeCreateInfo required_size{}; | ||
| 373 | leonida::apply_subgroup_request(leonida::decide_subgroup_size(entry, info->subgroup), required_size, | ||
| 374 | pipeline_info.stage); | ||
| 375 | pipeline_info.layout = vk.layout; | ||
| 376 | VkPipeline pipeline = VK_NULL_HANDLE; | ||
| 377 | Created result; | ||
| 378 | if (vkCreateComputePipelines(device, cache, 1, &pipeline_info, nullptr, &pipeline) != VK_SUCCESS) return result; | ||
| 379 | vkDestroyPipeline(device, pipeline, nullptr); | ||
| 380 | result.ok = true; | ||
| 381 | result.hit = (feedback.flags & VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT) != 0 && | ||
| 382 | (feedback.flags & VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT) != 0; | ||
| 383 | return result; | ||
| 384 | }; | ||
| 385 | |||
| 386 | vk.seeded = make_cache(seed.data(), seed.size()); | ||
| 387 | const Created from_seed = vk.seeded ? create(vk.seeded) : Created{}; | ||
| 388 | if (!from_seed.ok) { | ||
| 389 | std::fprintf(stderr, "FAIL: the pipeline was not created from the seeded cache\n"); | ||
| 390 | return 1; | ||
| 391 | } | ||
| 392 | if (from_seed.hit) { | ||
| 393 | std::printf("ok: vk_pipeline_check cache hits on the runtime's device (%s)\n", properties.deviceName); | ||
| 394 | return 0; | ||
| 395 | } | ||
| 396 | |||
| 397 | // Does this driver report a hit at all? Write a cache on this device and read it back. | ||
| 398 | vk.first = make_cache(nullptr, 0); | ||
| 399 | std::vector<char> own; | ||
| 400 | std::size_t own_size = 0; | ||
| 401 | if (vk.first && create(vk.first).ok && vkGetPipelineCacheData(device, vk.first, &own_size, nullptr) == VK_SUCCESS) { | ||
| 402 | own.resize(own_size); | ||
| 403 | if (vkGetPipelineCacheData(device, vk.first, &own_size, own.data()) != VK_SUCCESS) own.clear(); | ||
| 404 | own.resize(own.empty() ? 0 : own_size); | ||
| 405 | } | ||
| 406 | vk.second = own.empty() ? VkPipelineCache{} : make_cache(own.data(), own.size()); | ||
| 407 | const bool reports_hits = vk.second && create(vk.second).hit; | ||
| 408 | if (reports_hits) { | ||
| 409 | std::fprintf(stderr, | ||
| 410 | "FAIL: vk_pipeline_check cache does not hit on the runtime's device (%s), which does hit a " | ||
| 411 | "cache it wrote itself: vk_pipeline_check's device differs from the runtime's in something the " | ||
| 412 | "driver makes part of a pipeline's cache key\n", | ||
| 413 | properties.deviceName); | ||
| 414 | return 1; | ||
| 415 | } | ||
| 416 | std::printf("SKIP: %s reports no cache hit even for a cache it wrote itself\n", properties.deviceName); | ||
| 417 | return 77; | ||
| 418 | } | ||
diff --git a/runtime/test/vk_pipeline_check.cpp b/runtime/test/vk_pipeline_check.cpp index 7b4f8d533..a237f60ff 100644 --- a/runtime/test/vk_pipeline_check.cpp +++ b/runtime/test/vk_pipeline_check.cpp | |||
| @@ -7,8 +7,10 @@ | |||
| 7 | // descriptor-free shaders with the standard push-constant range | 7 | // descriptor-free shaders with the standard push-constant range |
| 8 | // - ps/vs/ms/gs: graphics pipeline via vkCreateGraphicsPipelines (Vulkan 1.3 dynamic rendering, no render pass) | 8 | // - ps/vs/ms/gs: graphics pipeline via vkCreateGraphicsPipelines (Vulkan 1.3 dynamic rendering, no render pass) |
| 9 | // paired with minimal partner stubs from <spirv dir>/stubs/ (generated by tools/spirv_stubs.py) | 9 | // paired with minimal partner stubs from <spirv dir>/stubs/ (generated by tools/spirv_stubs.py) |
| 10 | // Only optional device extensions needed by the selected shaders (and supported by the GPU) are enabled. Shaders | 10 | // VK_EXT_mesh_shader (with meshShaderQueries) and VK_KHR_maintenance8 are always enabled when supported so the |
| 11 | // whose required features or partner stubs are unavailable are reported as skipped rather than failed. | 11 | // saved pipeline cache matches the runtime's device key; other optional device extensions are enabled only when |
| 12 | // needed by the selected shaders. Shaders whose required features or partner stubs are unavailable are reported as | ||
| 13 | // skipped rather than failed. | ||
| 12 | // [cache.bin] loaded first if it exists (a stopped run resumes: cached pipelines are instant), saved every | 14 | // [cache.bin] loaded first if it exists (a stopped run resumes: cached pipelines are instant), saved every |
| 13 | // 10 minutes and at the end | 15 | // 10 minutes and at the end |
| 14 | // --jobs N compile on up to N worker threads (default: half the hardware threads) | 16 | // --jobs N compile on up to N worker threads (default: half the hardware threads) |
| @@ -1058,14 +1060,13 @@ int main(int argc, char** argv) { | |||
| 1058 | return failed ? 1 : 0; | 1060 | return failed ? 1 : 0; |
| 1059 | } | 1061 | } |
| 1060 | 1062 | ||
| 1061 | // Scan selected shaders to request only the optional extensions they actually use. | 1063 | // Scan selected shaders to request the optional shader extensions they use. VK_EXT_mesh_shader and |
| 1062 | bool want_mesh = false, want_bary = false, want_lock = false, want_m8 = false, want_fma = false, want_clock = false; | 1064 | // VK_KHR_maintenance8 are always enabled when supported (even for compute-only runs), because |
| 1065 | // create_vulkan_device_standalone enables both unconditionally and NVIDIA includes them in its cache key. | ||
| 1066 | bool want_bary = false, want_lock = false, want_fma = false, want_clock = false; | ||
| 1063 | for (auto& [_, meta] : work) { | 1067 | for (auto& [_, meta] : work) { |
| 1064 | if (meta->stage == "ms") want_mesh = true; | ||
| 1065 | if (meta->needs_maintenance8) want_m8 = true; | ||
| 1066 | for (const auto& feat : meta->features) { | 1068 | for (const auto& feat : meta->features) { |
| 1067 | if (feat == "meshShader") want_mesh = true; | 1069 | if (feat == "fragmentShaderBarycentric") want_bary = true; |
| 1068 | else if (feat == "fragmentShaderBarycentric") want_bary = true; | ||
| 1069 | else if (feat == "fragmentShaderPixelInterlock") want_lock = true; | 1070 | else if (feat == "fragmentShaderPixelInterlock") want_lock = true; |
| 1070 | else if (feat == "shaderFmaFloat32" || feat == "shaderFmaFloat64") want_fma = true; // OpFmaKHR (--fma khr) | 1071 | else if (feat == "shaderFmaFloat32" || feat == "shaderFmaFloat64") want_fma = true; // OpFmaKHR (--fma khr) |
| 1071 | else if (feat == "shaderSubgroupClock" || feat == "shaderDeviceClock") want_clock = true; | 1072 | else if (feat == "shaderSubgroupClock" || feat == "shaderDeviceClock") want_clock = true; |
| @@ -1137,13 +1138,11 @@ int main(int argc, char** argv) { | |||
| 1137 | link(&f11, reinterpret_cast<VkBaseOutStructure*>(&f11), nullptr, true); // shaderDrawParameters (VS v0/v3) | 1138 | link(&f11, reinterpret_cast<VkBaseOutStructure*>(&f11), nullptr, true); // shaderDrawParameters (VS v0/v3) |
| 1138 | link(&f12, reinterpret_cast<VkBaseOutStructure*>(&f12), nullptr, true); | 1139 | link(&f12, reinterpret_cast<VkBaseOutStructure*>(&f12), nullptr, true); |
| 1139 | link(&f13, reinterpret_cast<VkBaseOutStructure*>(&f13), nullptr, true); | 1140 | link(&f13, reinterpret_cast<VkBaseOutStructure*>(&f13), nullptr, true); |
| 1140 | const bool ext_mesh = link(&mesh, reinterpret_cast<VkBaseOutStructure*>(&mesh), VK_EXT_MESH_SHADER_EXTENSION_NAME, want_mesh); | 1141 | const bool ext_mesh = link(&mesh, reinterpret_cast<VkBaseOutStructure*>(&mesh), VK_EXT_MESH_SHADER_EXTENSION_NAME, true); |
| 1141 | const bool ext_bary = link(&bary, reinterpret_cast<VkBaseOutStructure*>(&bary), VK_KHR_FRAGMENT_SHADER_BARYCENTRIC_EXTENSION_NAME, want_bary); | 1142 | const bool ext_bary = link(&bary, reinterpret_cast<VkBaseOutStructure*>(&bary), VK_KHR_FRAGMENT_SHADER_BARYCENTRIC_EXTENSION_NAME, want_bary); |
| 1142 | const bool ext_lock = link(&lock, reinterpret_cast<VkBaseOutStructure*>(&lock), VK_EXT_FRAGMENT_SHADER_INTERLOCK_EXTENSION_NAME, want_lock); | 1143 | const bool ext_lock = link(&lock, reinterpret_cast<VkBaseOutStructure*>(&lock), VK_EXT_FRAGMENT_SHADER_INTERLOCK_EXTENSION_NAME, want_lock); |
| 1143 | #ifdef VK_KHR_MAINTENANCE_8_EXTENSION_NAME | 1144 | #ifdef VK_KHR_MAINTENANCE_8_EXTENSION_NAME |
| 1144 | const bool ext_m8 = link(&m8, reinterpret_cast<VkBaseOutStructure*>(&m8), VK_KHR_MAINTENANCE_8_EXTENSION_NAME, want_m8); | 1145 | const bool ext_m8 = link(&m8, reinterpret_cast<VkBaseOutStructure*>(&m8), VK_KHR_MAINTENANCE_8_EXTENSION_NAME, true); |
| 1145 | #else | ||
| 1146 | (void)want_m8; | ||
| 1147 | #endif | 1146 | #endif |
| 1148 | #ifdef VK_KHR_SHADER_FMA_EXTENSION_NAME | 1147 | #ifdef VK_KHR_SHADER_FMA_EXTENSION_NAME |
| 1149 | VkPhysicalDeviceShaderFmaFeaturesKHR fma{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_FMA_FEATURES_KHR}; | 1148 | VkPhysicalDeviceShaderFmaFeaturesKHR fma{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_FMA_FEATURES_KHR}; |
| @@ -1172,6 +1171,12 @@ int main(int argc, char** argv) { | |||
| 1172 | VkPhysicalDeviceFeatures2 f2{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2}; | 1171 | VkPhysicalDeviceFeatures2 f2{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2}; |
| 1173 | f2.pNext = chain; | 1172 | f2.pNext = chain; |
| 1174 | vkGetPhysicalDeviceFeatures2(pd, &f2); // fills every linked struct with what's supported | 1173 | vkGetPhysicalDeviceFeatures2(pd, &f2); // fills every linked struct with what's supported |
| 1174 | // The runtime's device (create_vulkan_device_standalone) never enables these three, and a driver makes them part | ||
| 1175 | // of a pipeline's cache key: with any of them on here, the cache this tool saves does not hit in gta6rt | ||
| 1176 | // (pipeline_cache_seed_test; docs/pipeline_check.md, "The cache and the runtime's device"). | ||
| 1177 | f2.features.robustBufferAccess = VK_FALSE; | ||
| 1178 | f12.scalarBlockLayout = VK_FALSE; | ||
| 1179 | f13.robustImageAccess = VK_FALSE; | ||
| 1175 | VkPhysicalDeviceMeshShaderPropertiesEXT mesh_props{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MESH_SHADER_PROPERTIES_EXT}; | 1180 | VkPhysicalDeviceMeshShaderPropertiesEXT mesh_props{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MESH_SHADER_PROPERTIES_EXT}; |
| 1176 | if (ext_mesh && mesh.meshShader) { | 1181 | if (ext_mesh && mesh.meshShader) { |
| 1177 | VkPhysicalDeviceProperties2 mesh_device_props{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2}; | 1182 | VkPhysicalDeviceProperties2 mesh_device_props{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2}; |
| @@ -1179,7 +1184,10 @@ int main(int argc, char** argv) { | |||
| 1179 | vkGetPhysicalDeviceProperties2(pd, &mesh_device_props); | 1184 | vkGetPhysicalDeviceProperties2(pd, &mesh_device_props); |
| 1180 | } | 1185 | } |
| 1181 | if (ext_mesh) { | 1186 | if (ext_mesh) { |
| 1182 | mesh.multiviewMeshShader = mesh.primitiveFragmentShadingRateMeshShader = mesh.meshShaderQueries = VK_FALSE; | 1187 | // Keep meshShaderQueries as supported: create_vulkan_device_standalone enables it when present (#577), |
| 1188 | // and NVIDIA hashes it into the pipeline cache key even for compute pipelines. | ||
| 1189 | if (!f2.features.pipelineStatisticsQuery) mesh.meshShaderQueries = VK_FALSE; | ||
| 1190 | mesh.multiviewMeshShader = mesh.primitiveFragmentShadingRateMeshShader = VK_FALSE; | ||
| 1183 | mesh.taskShader = VK_FALSE; | 1191 | mesh.taskShader = VK_FALSE; |
| 1184 | } | 1192 | } |
| 1185 | float prio = 1; | 1193 | float prio = 1; |