diff --git a/docs/running.md b/docs/running.md index 161e3153dc..a472f3dc97 100644 --- a/docs/running.md +++ b/docs/running.md @@ -11,6 +11,23 @@ Paths are placeholders: `` is the repository, `` the folder wit `gta6rt_tls_host.dll`, `` the work folder `make_game.py` writes, `` a folder for pipeline caches, `` the game's package folder and `` its executable. Under Wine, a Linux path is passed as `Z:`. +## Check a translated shader pack + +Before precompiling or starting the game, `gta6rt.exe --check-shader-pack [DIR]` checks the pack's mesh and +barycentric requirements against the GPU that `--run` would select with `LEONIDA_D3D12=vulkan` and the +same `LEONIDA_VK_DEVICE` setting. Omit `DIR` to use `LEONIDA_SHADERS` or the runtime's `shader_path.txt`. + +```sh +wine /gta6rt.exe --check-shader-pack 'Z:\path\to\work\shaders\spirv' +``` + +It reports missing GPU features, known native mesh/barycentric driver issues that `make_game.py` selects +fallbacks for, and translated fallbacks this runtime cannot use. It reads indexes only: no game files, +logical Vulkan device, pipeline compilation, or cache writes. Exit `0` means no mismatch was found, `1` +means blockers were found (even if some optional indexes could not be read), and `2` means the check +could not be completed and found no blockers. This does not verify SPIR-V files, shader interfaces, +other GPU requirements, or that the game will boot. + ## Settings every platform needs - **The title's hang watchdog.** The title exits on purpose (a write to address 0) when its main loop has not reported diff --git a/make_game.py b/make_game.py index f1af5a172d..2d65ab9c42 100644 --- a/make_game.py +++ b/make_game.py @@ -75,6 +75,7 @@ from shader_stage_manifest import shader_stems # noqa: E402 from replace_retry import replace as replace_retry # noqa: E402 from stale_build import newer_sources # noqa: E402 +from vk_subgroup_info import native_shader_driver_issue # noqa: E402 VERSIONS = os.path.join(ROOT, "game") STAGES = ["check", "shaders", "assets", "runtime", "pipelines"] @@ -2029,10 +2030,6 @@ def barycentric_fallback_override(): return None -# VkDriverId of Intel's Windows Vulkan driver (igvk64.dll). -VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS = 5 - - def runtime_gpu_mesh_draws_crash(): """True where the GPU the runtime would use is on Intel's Windows Vulkan driver with VK_EXT_mesh_shader: there vkCmdDrawMeshTasksEXT crashes (0xC0000409 in igvk64.dll) for a mesh shader with a live push-constant load at @@ -2042,8 +2039,8 @@ def runtime_gpu_mesh_draws_crash(): info = runtime_gpu_info() if not info or "driver_id" not in info: return None - return info.get("driver_id") == VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS and \ - info.get("mesh_in_supported_stages") is True + return info.get("mesh_in_supported_stages") is True and native_shader_driver_issue( + "mesh", info["driver_id"], info.get("vendor_id"), info.get("device_id")) def runtime_gpu_max_push_constants(): diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index f8edc3508e..61cc63fd3d 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -1521,6 +1521,8 @@ if(BUILD_TESTING) add_test(NAME descriptor_table COMMAND descriptor_table_test) add_executable(shader_library_test test/shader_library_test.cpp) add_test(NAME shader_library COMMAND shader_library_test) + add_executable(shader_pack_check_test test/shader_pack_check_test.cpp) + add_test(NAME shader_pack_check COMMAND shader_pack_check_test) add_executable(native_pso_blend_test test/native_pso_blend_test.cpp) add_test(NAME native_pso_blend COMMAND native_pso_blend_test) add_executable(native_pso_depth_test test/native_pso_depth_test.cpp) diff --git a/runtime/shims/d3d12_shader_pack_check.h b/runtime/shims/d3d12_shader_pack_check.h new file mode 100644 index 0000000000..d8407047e6 --- /dev/null +++ b/runtime/shims/d3d12_shader_pack_check.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace rt::d3d12 { + +// Uses the same Vulkan adapter selection as --run, without creating a logical device. An empty directory uses +// LEONIDA_SHADERS or shader_path.txt. Returns 0 for no known mismatch, 1 for blockers, 2 for an incomplete check. +int check_shader_pack(const std::filesystem::path& directory); + +} // namespace rt::d3d12 diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index f2fa3a45c2..709b25c5b4 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -13,6 +13,7 @@ #include "d3d12_compile_exit.h" #include "d3d12_pipeline_compile.h" #include "d3d12_precompile.h" +#include "d3d12_shader_pack_check.h" #include "d3d12_compile_helper.h" #include "d3d12_pipeline_checkpoint.h" #include "d3d12_feature_compat.h" @@ -57,6 +58,7 @@ #include "../src/resource_detile.h" #include "../src/console_texture_layout.h" #include "../src/shader_library.h" +#include "../src/shader_pack_check.h" #include "../src/shadow_keep.h" #include "../src/dstorage_trace.h" #include "../src/title_page_watch.h" @@ -21494,13 +21496,19 @@ HRESULT create_dxgi_factory(ID3D12Device* device, REFIID riid, void** out) { i.queue_family, riid, out); } -// Creates its own Vulkan instance and a Vulkan 1.3 device, or a 1.2 device with VK_KHR_dynamic_rendering and -// VK_KHR_synchronization2 (vulkan13_features.h). LEONIDA_VK_VALIDATION=1 adds VK_LAYER_KHRONOS_validation. -HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettings queue_test_settings) { - if (!out) return E_POINTER; - *out = nullptr; +struct ShaderPackProbe { + leonida::ShaderPackDevice device; + char name[VK_MAX_PHYSICAL_DEVICE_NAME_SIZE]{}; +}; + +// Creates its own Vulkan instance and device (or probes the same selected GPU and stops before the logical device). +// LEONIDA_VK_VALIDATION=1 adds VK_LAYER_KHRONOS_validation. +static HRESULT open_vulkan_standalone(REFIID riid, void** out, QueueTestSettings queue_test_settings, + ShaderPackProbe* probe) { + if (!probe && !out) return E_POINTER; + if (out) *out = nullptr; // LEONIDA_AFTERMATH=1: GPU crash dumps on before the Vulkan instance and device exist (src/vulkan_aftermath.h). - leonida::aftermath::enable(); + if (!probe) leonida::aftermath::enable(); static const HMODULE library = LoadLibraryA("vulkan-1.dll"); const auto get = library ? reinterpret_cast( reinterpret_cast(GetProcAddress(library, "vkGetInstanceProcAddr"))) @@ -21607,7 +21615,7 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin instance_info.enabledExtensionCount = static_cast(inst_exts.size()); instance_info.ppEnabledExtensionNames = inst_exts.empty() ? nullptr : inst_exts.data(); OwnedRuntime owned{}; - if (create_instance(&instance_info, nullptr, &owned.instance) != VK_SUCCESS) { + if (!create_instance || create_instance(&instance_info, nullptr, &owned.instance) != VK_SUCCESS) { std::fprintf(stderr, "[rt] d3d12: vkCreateInstance (Vulkan 1.3) failed\n"); return E_FAIL; } @@ -22044,6 +22052,19 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin *v13.next() = &barycentric; } } + if (probe) { + VkPhysicalDeviceDriverProperties driver{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES}; + VkPhysicalDeviceSubgroupProperties subgroup{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES, &driver}; + VkPhysicalDeviceProperties2 properties{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &subgroup}; + vkGetPhysicalDeviceProperties2(info.physical, &properties); + // make_game's mesh policy also requires mesh in the supported subgroup stages. + probe->device = leonida::shader_pack_device(info.mesh_shader, info.fragment_shader_barycentric, + (subgroup.supportedStages & VK_SHADER_STAGE_MESH_BIT_EXT) != 0, + driver.driverID, chosen.vendorID, chosen.deviceID); + std::memcpy(probe->name, chosen.deviceName, sizeof(probe->name)); + owned.destroy_instance(owned.instance, nullptr); + return S_OK; + } VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT interlock{ VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_SHADER_INTERLOCK_FEATURES_EXT}; if (has_device_extension(VK_EXT_FRAGMENT_SHADER_INTERLOCK_EXTENSION_NAME)) { @@ -22411,6 +22432,57 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin return hand_out(device, riid, out); } +HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettings queue_test_settings) { + return open_vulkan_standalone(riid, out, queue_test_settings, nullptr); +} + +int check_shader_pack(const std::filesystem::path& directory) { + std::string error; + const auto root = directory.empty() ? configured_shader_directory(&error) : directory; + if (root.empty()) { + std::fprintf(stderr, "[shader-pack] no shader directory: %s\n", error.c_str()); + return 2; + } + ShaderPackProbe probe; + if (FAILED(open_vulkan_standalone(__uuidof(ID3D12Device), nullptr, QueueTestSettings::ignore, &probe))) { + std::fprintf(stderr, "[shader-pack] could not probe the Vulkan device\n"); + return 2; + } + std::printf("[shader-pack] Vulkan device: %s (native mesh: %s, native barycentrics: %s)\n", probe.name, + probe.device.native_mesh ? "yes" : "no", probe.device.native_barycentric ? "yes" : "no"); + leonida::ShaderPackCheck report; + if (!leonida::check_shader_pack(root, probe.device, report, error)) { + std::fprintf(stderr, "[shader-pack] %s\n", error.c_str()); + return 2; + } + for (const auto& issue : report.optional_index_errors) + std::fprintf(stderr, "[shader-pack] incomplete scan: %s\n", issue.c_str()); + if (report.native_mesh_missing) + std::printf("[shader-pack] %zu mesh modules need VK_EXT_mesh_shader / meshShader, unavailable on this GPU\n", + report.native_mesh_missing); + if (report.native_mesh_driver_blocked) + std::printf("[shader-pack] %zu native mesh modules match a known driver issue (mesh draws can crash)\n", + report.native_mesh_driver_blocked); + if (report.mesh_compute_unimplemented) + std::printf("[shader-pack] %zu mesh modules were translated to compute, but this runtime cannot replay them\n", + report.mesh_compute_unimplemented); + if (report.native_barycentric_missing) + std::printf("[shader-pack] %zu pixel modules need fragmentShaderBarycentric, unavailable on this GPU\n", + report.native_barycentric_missing); + if (report.native_barycentric_driver_blocked) + std::printf("[shader-pack] %zu native barycentric modules match a known driver issue (barycentrics can be incorrect)\n", + report.native_barycentric_driver_blocked); + if (report.barycentric_fallback_unimplemented) + std::printf("[shader-pack] %zu pixel modules need the barycentric fallback, not implemented in this runtime\n", + report.barycentric_fallback_unimplemented); + if (report.unknown) + std::printf("[shader-pack] %zu entries have missing or malformed stage or mesh/barycentric metadata\n", report.unknown); + std::printf("[shader-pack] %zu indexed modules: %s. Metadata only; no pipelines tested.\n", report.indexed, + report.blocked() ? "mesh/barycentric blockers found" + : report.exit_code() ? "incomplete check" : "no known mesh/barycentric mismatch"); + return report.exit_code(); +} + // A Vulkan device made as `gta6rt --run` makes it, for building the library's compute pipelines with no title // (--precompile-pipelines, --compile-server), with the two layouts create_compute makes. class BuildHost { diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index 6f70390995..3f7311efc2 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp @@ -16,6 +16,9 @@ // does not stall it. Resumable: entries already journaled are skipped (--all: built anyway). Programs named by // LEONIDA_SKIP_COMPUTE are built too unless --skip-listed is given. See shims/d3d12_precompile.h. // +// gta6rt --check-shader-pack [DIR] +// Checks mesh and barycentric index metadata against the Vulkan GPU selected by --run. No game or pipelines. +// // gta6rt --run [--game-dir DIR] [--overlay DIR] [--trace-d3d12] [--trace FILE] [-- game arguments...] // The same, then the EXE's own TLS callbacks and its entry point, on a game thread. The game's argv and // command line are plus the arguments after "--"; exit/ExitProcess from the game end the run @@ -33,6 +36,7 @@ #include #include #include +#include #include #include @@ -194,6 +198,7 @@ void start_title_peek(std::uint8_t* base) { #include "vulkan_device_fault.h" #include "../shims/d3d12_compile_exit.h" #include "../shims/d3d12_precompile.h" +#include "../shims/d3d12_shader_pack_check.h" #include "../shims/package_splash.h" #endif #include "../shims/process_shim.h" @@ -263,6 +268,7 @@ int usage() { " gta6rt --first-run-ui [--game-dir DIR] [--overlay DIR] [--trace FILE]\n" " gta6rt --run [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE] [-- game arguments...]\n" " gta6rt --precompile-pipelines [--threads N] [--big-threads N] [--big-bytes N] [--all] [--smallest-first] [--limit N] [--min-bytes N] [--max-bytes N] [--filter PART]... [--skip-listed] [--list] [--via-helper]\n" + " gta6rt --check-shader-pack [DIR]\n" " gta6rt --compile-server [--parent PID] [--threads N] [--idle-threads N] [--idle-exit SECONDS]\n"); return 2; } @@ -387,6 +393,11 @@ static BOOL WINAPI show_package_window(HWND window, int command) { int wmain(int argc, wchar_t** argv) { #ifdef LEONIDA_VULKAN_DEVICE + if (argc > 1 && std::wstring_view(argv[1]) == L"--check-shader-pack") { + if (argc == 2) return rt::d3d12::check_shader_pack({}); + if (argc != 3 || !*argv[2] || std::wstring_view(argv[2]).starts_with(L"--")) return usage(); + return rt::d3d12::check_shader_pack(argv[2]); + } if (argc > 1 && std::wstring(argv[1]) == L"--precompile-pipelines") return precompile_main(argc, argv); if (argc > 1 && std::wstring(argv[1]) == L"--compile-server") return compile_server_main(argc, argv); #endif diff --git a/runtime/src/shader_driver_rules.inc b/runtime/src/shader_driver_rules.inc new file mode 100644 index 0000000000..4115adafe0 --- /dev/null +++ b/runtime/src/shader_driver_rules.inc @@ -0,0 +1,12 @@ +// Native shader paths that need a translation fallback on a known driver. Shared with tools/vk_subgroup_info.py. +// Columns: path, VkDriverId, PCI vendor ID, PCI device ID. Zero in a PCI column matches any ID. +// BUILD-2202: Intel Windows mesh draws crash on the corpus's live push-constant loads. +LEONIDA_SHADER_DRIVER_RULE(mesh, 5, 0, 0) +// SHADER-925: Mesa ANV returns a zero BaryCoordKHR.i on Intel Arc. +LEONIDA_SHADER_DRIVER_RULE(barycentric, 6, 0, 0) +// AMD's proprietary Windows driver returns a wrong BaryCoordKHR component on gfx90c (#5933). +// These are amdgpu's CHIP_RENOIR PCI IDs (Renoir, Lucienne, Cezanne and Barcelo). +LEONIDA_SHADER_DRIVER_RULE(barycentric, 1, 0x1002, 0x15E7) +LEONIDA_SHADER_DRIVER_RULE(barycentric, 1, 0x1002, 0x1636) +LEONIDA_SHADER_DRIVER_RULE(barycentric, 1, 0x1002, 0x1638) +LEONIDA_SHADER_DRIVER_RULE(barycentric, 1, 0x1002, 0x164C) diff --git a/runtime/src/shader_library.h b/runtime/src/shader_library.h index 7699dfbb0d..4282286702 100644 --- a/runtime/src/shader_library.h +++ b/runtime/src/shader_library.h @@ -953,6 +953,27 @@ inline bool descriptor_hash_kind_from_env(const char* value, DescriptorHashKind* return false; } +// The runtime's optional indexes, in lookup order. The read-only pack check scans the same set. +inline constexpr const char* shader_index_subdirectories[] = {"fspx", "fspx_compute", "fspx_gfx", "bvh", "exe", "library"}; + +inline bool read_shader_index(const std::filesystem::path& index, Json& root, std::string& error) { + std::ifstream file(index, std::ios::binary); + if (!file) { + const auto path = index.u8string(); + error = "cannot open " + std::string(path.begin(), path.end()); + return false; + } + const std::string text((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + constexpr std::string_view utf8_bom = "\xef\xbb\xbf"; + std::string_view json(text); + if (json.starts_with(utf8_bom)) json.remove_prefix(utf8_bom.size()); + if (!JsonReader(json).parse(root) || root.kind != Json::Kind::object) { + error = "index.json is not a JSON object"; + return false; + } + return true; +} + class ShaderLibrary { public: // Reads /index.json, then the indexes of the programs make_game.py translates from the package's DLLs and the @@ -989,7 +1010,7 @@ class ShaderLibrary { } catch (...) { return fail("index.json could not be read"); } - for (const char* sub_folder : {"fspx", "fspx_compute", "fspx_gfx", "bvh", "exe", "library"}) { + for (const char* sub_folder : shader_index_subdirectories) { try { const auto ignore = [](const std::string&) { return false; }; const std::filesystem::path sub = folder / sub_folder; @@ -1199,18 +1220,9 @@ class ShaderLibrary { // adds its entries under "", the path of their modules relative to the main folder. template bool load_index(const std::filesystem::path& dir, const std::string& prefix, Fail&& fail) { - const auto index = dir / "index.json"; - std::ifstream file(index, std::ios::binary); - if (!file) { - const auto path = index.u8string(); - return fail("cannot open " + std::string(path.begin(), path.end())); - } - const std::string text((std::istreambuf_iterator(file)), std::istreambuf_iterator()); - constexpr std::string_view utf8_bom = "\xef\xbb\xbf"; - std::string_view json(text); - if (json.starts_with(utf8_bom)) json.remove_prefix(utf8_bom.size()); Json root; - if (!JsonReader(json).parse(root) || root.kind != Json::Kind::object) return fail("index.json is not a JSON object"); + std::string error; + if (!read_shader_index(dir / "index.json", root, error)) return fail(error); if (prefix.empty()) { dir_ = dir; entries_.clear(); diff --git a/runtime/src/shader_pack_check.h b/runtime/src/shader_pack_check.h new file mode 100644 index 0000000000..5932d615a7 --- /dev/null +++ b/runtime/src/shader_pack_check.h @@ -0,0 +1,163 @@ +#pragma once + +// Metadata-only check for mesh and barycentric requirements. It deliberately does not test shader modules, +// VS/PS interfaces, other Vulkan features, or which entries the title will actually use. +#include "shader_library.h" + +namespace leonida { + +enum class NativeShaderPath { mesh, barycentric }; + +namespace detail { + +struct ShaderDriverRule { + NativeShaderPath path; + std::uint32_t driver, vendor, device; +}; + +inline constexpr ShaderDriverRule shader_driver_rules[] = { +#define LEONIDA_SHADER_DRIVER_RULE(path, driver, vendor, device) {NativeShaderPath::path, driver, vendor, device}, +#include "shader_driver_rules.inc" +#undef LEONIDA_SHADER_DRIVER_RULE +}; + +} // namespace detail + +inline constexpr bool native_shader_driver_issue(NativeShaderPath path, std::uint32_t driver, std::uint32_t vendor, + std::uint32_t device) { + return std::any_of(std::begin(detail::shader_driver_rules), std::end(detail::shader_driver_rules), + [&](const detail::ShaderDriverRule& rule) { + return rule.path == path && rule.driver == driver && + (!rule.vendor || rule.vendor == vendor) && (!rule.device || rule.device == device); + }); +} + +struct ShaderPackDevice { + bool native_mesh = false; + bool native_barycentric = false; + bool native_mesh_driver_issue = false; + bool native_barycentric_driver_issue = false; +}; + +inline constexpr ShaderPackDevice shader_pack_device(bool native_mesh, bool native_barycentric, bool mesh_subgroups, + std::uint32_t driver, std::uint32_t vendor, std::uint32_t device) { + return {native_mesh, native_barycentric, + mesh_subgroups && native_shader_driver_issue(NativeShaderPath::mesh, driver, vendor, device), + native_shader_driver_issue(NativeShaderPath::barycentric, driver, vendor, device)}; +} + +struct ShaderPackCheck { + std::size_t indexed = 0; + std::size_t native_mesh_missing = 0; + std::size_t native_mesh_driver_blocked = 0; + std::size_t mesh_compute_unimplemented = 0; + std::size_t native_barycentric_missing = 0; + std::size_t native_barycentric_driver_blocked = 0; + std::size_t barycentric_fallback_unimplemented = 0; + std::size_t unknown = 0; + std::vector optional_index_errors; + + bool blocked() const { + return native_mesh_missing || native_mesh_driver_blocked || mesh_compute_unimplemented || + native_barycentric_missing || native_barycentric_driver_blocked || barycentric_fallback_unimplemented; + } + int exit_code() const { return blocked() ? 1 : (!indexed || unknown || !optional_index_errors.empty() ? 2 : 0); } +}; + +namespace detail { + +inline bool shader_pack_string_array(const Json* field) { + return field && field->kind == Json::Kind::array && + std::all_of(field->array.begin(), field->array.end(), [](const Json& item) { + return item.kind == Json::Kind::string; + }); +} + +inline bool shader_pack_contains(const Json* array, std::string_view value) { + return array && array->kind == Json::Kind::array && + std::any_of(array->array.begin(), array->array.end(), [&](const Json& item) { + return item.kind == Json::Kind::string && item.string == value; + }); +} + +inline void check_shader_index(const Json& index, ShaderPackDevice device, ShaderPackCheck& report) { + for (const auto& [name, entry] : index.object) { + const auto string = [&](std::string_view key) { + const Json* field = entry.find(key); + return field && field->kind == Json::Kind::string; + }; + // As in ShaderLibrary: no program identity, no runtime lookup. Do not inspect or print shader names. + if (!string("fssm_sha256") && !string("fspx_code_sha256")) continue; + const Json* stage_field = entry.find("stage"); + const std::string_view stage = stage_field && stage_field->kind == Json::Kind::string + ? std::string_view(stage_field->string) : std::string_view{}; + if (stage != "ms" && stage != "ps" && stage != "vs" && stage != "gs" && stage != "cs") { + ++report.unknown; + continue; + } + ++report.indexed; + if (stage == "ms") { + if (const Json* compute = entry.find("mesh_compute")) { + if (compute->kind == Json::Kind::object) ++report.mesh_compute_unimplemented; + else ++report.unknown; + } else if (!device.native_mesh) { + ++report.native_mesh_missing; + } else if (device.native_mesh_driver_issue) { + ++report.native_mesh_driver_blocked; + } + } else if (stage == "ps") { + const Json* features = entry.find("features"); + const Json* capabilities = entry.find("capabilities"); + const bool needs_native = shader_pack_contains(features, "fragmentShaderBarycentric") || + shader_pack_contains(capabilities, "FragmentBarycentricKHR"); + if (needs_native) { + if (!device.native_barycentric) ++report.native_barycentric_missing; + else if (device.native_barycentric_driver_issue) ++report.native_barycentric_driver_blocked; + } + if (const Json* fallback = entry.find("bary_fallback_input")) { + const Json* loc = fallback->find("location"); + if (loc && loc->kind == Json::Kind::number && loc->number >= 0 && loc->number <= UINT32_MAX && + static_cast(loc->number) == loc->number) + ++report.barycentric_fallback_unimplemented; + else + ++report.unknown; + } else if (!needs_native && !shader_pack_string_array(features) && !shader_pack_string_array(capabilities)) { + ++report.unknown; // old or incomplete PS metadata cannot establish barycentric requirements + } + } + } +} + +} // namespace detail + +// A missing or unreadable main index is fatal. Optional failures are reported alongside findings from readable indexes. +inline bool check_shader_pack(const std::filesystem::path& directory, ShaderPackDevice device, ShaderPackCheck& report, + std::string& error) { + ShaderPackCheck checked; + const auto scan = [&](const std::filesystem::path& index, std::string& why) { + try { + Json root; + if (!read_shader_index(index, root, why)) return false; + detail::check_shader_index(root, device, checked); + return true; + } catch (...) { + why = "shader index could not be read"; + return false; + } + }; + if (!scan(directory / "index.json", error)) return false; + for (const char* sub : shader_index_subdirectories) { + const auto index = directory / sub / "index.json"; + std::error_code ec; + const bool present = std::filesystem::exists(index, ec); + std::string why; + if (ec || (present && !scan(index, why))) { + if (ec) why = ec.message(); + checked.optional_index_errors.push_back(std::string(sub) + "/index.json: " + why); + } + } + report = std::move(checked); + return true; +} + +} // namespace leonida diff --git a/runtime/test/shader_pack_check_test.cpp b/runtime/test/shader_pack_check_test.cpp new file mode 100644 index 0000000000..7d5116d306 --- /dev/null +++ b/runtime/test/shader_pack_check_test.cpp @@ -0,0 +1,97 @@ +// Metadata-only shader-pack checks. The fixtures are handwritten indexes, without any shader modules or game data. +#include "../src/shader_pack_check.h" +#include "test_temp_dir.h" + +#include + +#define CHECK(condition) do { \ + if (!(condition)) { \ + std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); \ + return 1; \ + } \ +} while (0) + +int main() { + using namespace leonida; + const testfiles::ScratchDir temp("leonida_shader_pack_check"); + const auto& dir = temp.path(); + const auto write = [&](const std::filesystem::path& path, std::string_view text) { + std::filesystem::create_directories((dir / path).parent_path()); + std::ofstream file(dir / path, std::ios::binary); + return static_cast(file.write(text.data(), static_cast(text.size()))); + }; + ShaderPackCheck report; + std::string error; + CHECK(!check_shader_pack(dir, {}, report, error) && error.find("cannot open") != std::string::npos); + CHECK(write("index.json", "{}")); + CHECK(check_shader_pack(dir, {}, report, error) && report.indexed == 0 && report.exit_code() == 2); + + // Feature and capability metadata both count; an entry the runtime cannot look up does not. + CHECK(write("index.json", "\xef\xbb\xbf" R"({ + "mesh": {"stage": "ms", "fssm_sha256": "x"}, + "pixel": {"stage": "ps", "fspx_code_sha256": "y", "features": ["fragmentShaderBarycentric"]}, + "pixel2": {"stage": "ps", "fssm_sha256": "z", "capabilities": ["FragmentBarycentricKHR"]}, + "ignored": {"stage": "ms"} + })")); + CHECK(check_shader_pack(dir, {true, true}, report, error)); + CHECK(report.indexed == 3 && !report.unknown && report.exit_code() == 0); + CHECK(check_shader_pack(dir, {}, report, error)); + CHECK(report.indexed == 3 && report.native_mesh_missing == 1 && report.native_barycentric_missing == 2); + CHECK(report.mesh_compute_unimplemented == 0 && report.barycentric_fallback_unimplemented == 0); + CHECK(report.exit_code() == 1); + + // Match the builder's driver workarounds even when both Vulkan features are exposed. Intel's mesh rule + // is limited to its Windows driver with mesh subgroup support; the barycentric rule is for Mesa ANV instead. + CHECK(check_shader_pack(dir, shader_pack_device(true, true, true, 5, 0x8086, 0x56A0), report, error)); + CHECK(report.native_mesh_driver_blocked == 1 && report.native_barycentric_driver_blocked == 0); + CHECK(report.native_mesh_missing == 0 && report.exit_code() == 1); + CHECK(check_shader_pack(dir, shader_pack_device(true, true, false, 5, 0x8086, 0x56A0), report, error)); + CHECK(report.exit_code() == 0); + CHECK(check_shader_pack(dir, shader_pack_device(true, true, true, 6, 0x8086, 0xE20B), report, error)); + CHECK(report.native_mesh_driver_blocked == 0 && report.native_barycentric_driver_blocked == 2); + CHECK(report.native_barycentric_missing == 0 && report.exit_code() == 1); + CHECK(check_shader_pack(dir, shader_pack_device(true, true, true, 1, 0x1002, 0x15E7), report, error)); + CHECK(report.native_barycentric_driver_blocked == 2 && report.exit_code() == 1); + CHECK(check_shader_pack(dir, shader_pack_device(true, true, true, 3, 0x1002, 0x15E7), report, error)); + CHECK(report.exit_code() == 0); // RADV on the same GPU is not affected. + CHECK(check_shader_pack(dir, shader_pack_device(true, true, true, 1, 0x1002, 0x164E), report, error)); + CHECK(report.exit_code() == 0); // AMD's proprietary driver on a different GPU is not affected. + + // Fallback markers are not proof of runtime support, even on a GPU that supports the native path. + CHECK(write("index.json", R"({"basic": {"stage": "ps", "fssm_sha256": "a", "features": []}})")); + CHECK(write("exe/index.json", R"({ + "mesh": {"stage": "ms", "fssm_sha256": "b", "mesh_compute": {"slot_bytes": 256}}, + "pixel": {"stage": "ps", "fssm_sha256": "c", "bary_fallback_input": {"location": 0}} + })")); + for (const ShaderPackDevice gpu : {ShaderPackDevice{}, ShaderPackDevice{true, true}}) { + CHECK(check_shader_pack(dir, gpu, report, error)); + CHECK(report.indexed == 3 && !report.unknown && report.exit_code() == 1); + CHECK(report.mesh_compute_unimplemented == 1 && report.barycentric_fallback_unimplemented == 1); + CHECK(report.native_mesh_missing == 0 && report.native_barycentric_missing == 0); + } + + CHECK(write("exe/index.json", R"({ + "old": {"stage": "ps", "fssm_sha256": "d"}, + "bad_mesh": {"stage": "ms", "fssm_sha256": "e", "mesh_compute": null}, + "bad_pixel": {"stage": "ps", "fssm_sha256": "f", "bary_fallback_input": {"location": 1.5}} + })")); + CHECK(check_shader_pack(dir, {}, report, error)); + CHECK(report.indexed == 4 && report.unknown == 3 && report.exit_code() == 2); + CHECK(write("exe/index.json", R"({"wrong_stage": {"stage": "MS", "fssm_sha256": "b"}})")); + CHECK(check_shader_pack(dir, {}, report, error)); + CHECK(report.indexed == 1 && report.unknown == 1 && report.exit_code() == 2); + + CHECK(write("fspx_gfx/index.json", "[]")); + CHECK(check_shader_pack(dir, {}, report, error)); + CHECK(report.optional_index_errors.size() == 1 && report.exit_code() == 2); + CHECK(report.optional_index_errors.front().find("fspx_gfx/index.json") != std::string::npos); + CHECK(write("index.json", R"({"first_mesh": {"stage": "ms", "fssm_sha256": "a"}})")); + CHECK(write("exe/index.json", R"({"later_mesh": {"stage": "ms", "fssm_sha256": "b"}})")); + CHECK(check_shader_pack(dir, {}, report, error)); + CHECK(report.indexed == 2 && report.native_mesh_missing == 2 && report.exit_code() == 1); + CHECK(report.optional_index_errors.size() == 1); + + CHECK(write("index.json", "{")); + CHECK(!check_shader_pack(dir, {}, report, error) && error.find("not a JSON object") != std::string::npos); + return 0; +} diff --git a/tests/test_make_game_translator_flags.py b/tests/test_make_game_translator_flags.py index ffae8710d9..80d6f2cd4b 100644 --- a/tests/test_make_game_translator_flags.py +++ b/tests/test_make_game_translator_flags.py @@ -289,7 +289,7 @@ def test_mesh_compute_follows_the_device(self): (("--mesh-compute", "--pc-tail-ubo"),) * 2) def test_runtime_gpu_mesh_draws_crash_reads_the_probe(self): - intel = make_game.VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS + intel = 5 # VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS for raw, want in (({"driver_id": intel, "mesh_in_supported_stages": True}, True), ({"driver_id": intel, "mesh_in_supported_stages": False}, False), ({"driver_id": intel}, False), diff --git a/tests/test_vk_subgroup_info.py b/tests/test_vk_subgroup_info.py index 63ff2fd40a..333ce3168a 100644 --- a/tests/test_vk_subgroup_info.py +++ b/tests/test_vk_subgroup_info.py @@ -902,7 +902,6 @@ def test_barycentric_fallback_gfx90c_amd_windows(self): for label, vendor, device, driver, want in cases: with self.subTest(case=label): self.assertIs(vk_info.barycentric_fallback(vendor, device, driver), want) - self.assertIs(vk_info.gfx90c_amd_windows(vendor, device, driver), want and driver == 1) res, _vk = self._run_query(FakeDevice(vendor_id=vendor, device_id=device, driver_id=driver)) self.assertEqual((res["vendor_id"], res["device_id"], res["driver_id"]), (vendor, device, driver)) self.assertIs(res["barycentric_fallback"], want) diff --git a/tools/vk_subgroup_info.py b/tools/vk_subgroup_info.py index ca7cd79a6e..d4b1d6951c 100644 --- a/tools/vk_subgroup_info.py +++ b/tools/vk_subgroup_info.py @@ -51,9 +51,11 @@ """ import argparse import ctypes +import functools import json import os import sys +from pathlib import Path VK_STRUCTURE_TYPE_APPLICATION_INFO = 0 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO = 1 @@ -80,10 +82,6 @@ # crash reports from 0x13C0, 0x164E, 0x73BF (RX 6800) and 0x73DF (RX 6700). RDNA2_DEVICE_IDS = ((0x73A0, 0x73FF), (0x7420, 0x743F), (0x163F, 0x163F), (0x1435, 0x1435), (0x1681, 0x1681), (0x1506, 0x1506), (0x164E, 0x164E), (0x13C0, 0x13C0)) -# gfx90c (GCN 5) iGPUs: the CHIP_RENOIR entries of Linux amdgpu's chipset table (amdgpu_drv.c), i.e. Renoir, Lucienne, -# Cezanne and Barcelo. #5933's wrong BaryCoordKHR was measured on 0x15E7 (Vega 8 of a Ryzen 7 5825U). Other GCN 5 -# parts (Raven/Picasso gfx902, Vega 10/12/20 cards) are a different GPU and are not tested, so they are not listed. -GFX90C_DEVICE_IDS = (0x15E7, 0x1636, 0x1638, 0x164C) DRIVER_PROPS_SIZE = 536 # sizeof(VkPhysicalDeviceDriverProperties): 16 + driverID + 2 x char[256] + version, padded BARY_FEATURES_SIZE = 24 # sizeof(VkPhysicalDeviceFragmentShaderBarycentricFeaturesKHR): 16 + one VkBool32 VK_SHADER_STAGE_COMPUTE_BIT = 0x20 @@ -182,18 +180,31 @@ def rdna2_amd_windows(vendor_id: int, device_id: int, driver_id: int) -> bool: and any(lo <= device_id <= hi for lo, hi in RDNA2_DEVICE_IDS)) -def gfx90c_amd_windows(vendor_id: int, device_id: int, driver_id: int) -> bool: - """A gfx90c (GCN 5) iGPU under AMD's proprietary (Windows) driver, whose native BaryCoordKHR is wrong (#5933).""" - return (vendor_id == AMD_VENDOR_ID and driver_id == VK_DRIVER_ID_AMD_PROPRIETARY - and device_id in GFX90C_DEVICE_IDS) +@functools.cache +def _shader_driver_rules(): + """Read the same rules the C++ checker compiles in, without needing a runtime build.""" + table = Path(__file__).resolve().parents[1] / "runtime" / "src" / "shader_driver_rules.inc" + rules = [] + for line in table.read_text(encoding="utf-8").splitlines(): + line = line.strip() + if not line or line.startswith("//"): + continue + path, driver, vendor, device = line.removeprefix("LEONIDA_SHADER_DRIVER_RULE(").removesuffix(")").split(",") + rules.append((path, int(driver, 0), int(vendor, 0), int(device, 0))) + return rules + + +def native_shader_driver_issue(path, driver_id, vendor_id=None, device_id=None): + return any(path == rule_path and driver_id == driver and + (vendor == 0 or vendor_id == vendor) and (device == 0 or device_id == device) + for rule_path, driver, vendor, device in _shader_driver_rules()) def barycentric_fallback(vendor_id: int, device_id: int, driver_id: int) -> bool: """Drivers that expose native barycentrics but return wrong BaryCoordKHR values for translated pixel shaders: Mesa ANV (BaryCoordKHR.i zero on Intel Arc, SHADER-925) and AMD's Windows driver on gfx90c (wrong third component, #5933).""" - return (driver_id == VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA - or gfx90c_amd_windows(vendor_id, device_id, driver_id)) + return native_shader_driver_issue("barycentric", driver_id, vendor_id, device_id) def physical_devices(vk, instance):