diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 43886af30f..56f415386d 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -4284,6 +4284,15 @@ Games and middleware call these every frame (PIX events), so they must be cheap - **Labels, only with `VK_EXT_debug_utils`.** It is an instance extension, so `VulkanDeviceInfo::debug_utils` says whether the instance enabled it; `create_vulkan_device_standalone` enables it when the loader lists it. Without it every marker call is a no-op (nothing logged, nothing recorded on the queue). +- **Compute shader modules.** With `VK_EXT_debug_utils`, each module is named with its shader-library entry name + before pipeline creation. This lets GPU-assisted validation select an individual compute shader using + `VK_LAYER_GPUAV_ENABLE=1`, `VK_LAYER_GPUAV_SELECT_INSTRUMENTED_SHADERS=1` and + `VK_LAYER_GPUAV_SHADERS_TO_INSTRUMENT=` alongside `LEONIDA_VK_VALIDATION=1`. + Shader code and execution are unchanged; without the extension or naming entry point no naming call is made. + The layer's selection setting must be enabled to avoid instrumenting the entire library. + With a recent Khronos validation layer installed, `python -I scripts/check_compute_gpuav_selection.py + /d3d12_compute_test.exe` checks selected and unmatched names using synthetic compute readbacks and + the layer's instrumented-module dumps. It is an opt-in GPU-AV check, not a default CTest requirement. - **Command lists.** `SetMarker`/`BeginEvent`/`EndEvent` are recorded in the IR (`DebugMarker`) in order with the other commands and replayed as `vkCmdInsertDebugUtilsLabelEXT`/`vkCmdBeginDebugUtilsLabelEXT`/ `vkCmdEndDebugUtilsLabelEXT` into the submission's command buffer. diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 1fb36401e3..0ae3da4896 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -2194,6 +2194,7 @@ struct DeviceState { std::mutex title_image_mutex; std::multimap title_images; VulkanDeviceInfo info; + PFN_vkSetDebugUtilsObjectNameEXT set_debug_name = nullptr; std::vector families; template @@ -10447,6 +10448,13 @@ ComputeBuild build_compute_pipeline(DeviceState& device, const leonida::ShaderEn { const leonida::aftermath::ShaderName named(entry->name); if (vk.vkCreateShaderModule(dev, &module_info, nullptr, &module) != VK_SUCCESS) return out; + if (device.set_debug_name) { + VkDebugUtilsObjectNameInfoEXT name{VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT}; + name.objectType = VK_OBJECT_TYPE_SHADER_MODULE; + name.objectHandle = reinterpret_cast(module); + name.pObjectName = entry->name.c_str(); + device.set_debug_name(dev, &name); + } } pipeline_info.stage.module = module; journal.create(); @@ -13870,6 +13878,7 @@ class Device final f = reinterpret_cast>(info.get_instance_proc(info.instance, name)); }; load(labels.cmd_insert, "vkCmdInsertDebugUtilsLabelEXT"); + load(state_.set_debug_name, "vkSetDebugUtilsObjectNameEXT"); load(labels.cmd_begin, "vkCmdBeginDebugUtilsLabelEXT"); load(labels.cmd_end, "vkCmdEndDebugUtilsLabelEXT"); load(labels.queue_insert, "vkQueueInsertDebugUtilsLabelEXT"); diff --git a/runtime/test/d3d12_compute_test.cpp b/runtime/test/d3d12_compute_test.cpp index 05591a780a..7d4c0dd68d 100644 --- a/runtime/test/d3d12_compute_test.cpp +++ b/runtime/test/d3d12_compute_test.cpp @@ -299,6 +299,7 @@ int main() { { rt::d3d12::VulkanDeviceInfo info{}; info.prepare_on_bind=d.prepare_on_bind; + info.debug_utils = d.debug_utils; info.get_instance_proc = d.instance_proc; info.instance = d.instance; info.physical = d.physical; diff --git a/scripts/check_compute_gpuav_selection.py b/scripts/check_compute_gpuav_selection.py new file mode 100644 index 0000000000..978c0e6bc4 --- /dev/null +++ b/scripts/check_compute_gpuav_selection.py @@ -0,0 +1,70 @@ +"""Check compute-module debug-name selection with an installed Khronos GPU-AV layer. + +Uses only d3d12_compute_test's synthetic shaders. This is an opt-in integration +check: it requires a Vulkan device and a layer supporting selective GPU-AV and +instrumented-shader dumps. It is not part of the default CTest suite. +""" +import argparse +import os +from pathlib import Path +import subprocess +import tempfile + + +def run_case(executable, directory, selector): + directory.mkdir() + cache = directory / "cache" + cache.mkdir() + env = os.environ.copy() + env.update({ + "LEONIDA_VK_VALIDATION": "1", + "LEONIDA_PIPELINE_CACHE_DIR": str(cache), + "VK_LAYER_GPUAV_ENABLE": "1", + "VK_LAYER_GPUAV_SHADER_INSTRUMENTATION": "1", + "VK_LAYER_GPUAV_SELECT_INSTRUMENTED_SHADERS": "1", + "VK_LAYER_GPUAV_SHADERS_TO_INSTRUMENT": selector, + "VK_LAYER_GPUAV_DEBUG_DUMP_INSTRUMENTED_SHADERS": "1", + }) + result = subprocess.run([str(executable)], cwd=directory, env=env, + capture_output=True, timeout=120) + if result.returncode == 77: + return None + if result.returncode: + raise RuntimeError(f"compute fixture failed ({result.returncode}):\n" + + result.stderr.decode("utf-8", errors="replace")) + dumps = list(directory.glob("dump_*_after.spv")) + for after in dumps: + before = after.with_name(after.name.replace("_after.spv", "_before.spv")) + original, instrumented = before.read_bytes(), after.read_bytes() + if original[:4] != b"\x03\x02\x23\x07" or instrumented[:4] != b"\x03\x02\x23\x07": + raise RuntimeError("layer dump is not SPIR-V") + if original == instrumented: + raise RuntimeError("selected module was not instrumented") + return len(dumps) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("compute_test", type=Path, help="path to d3d12_compute_test") + args = parser.parse_args() + executable = args.compute_test.resolve(strict=True) + with tempfile.TemporaryDirectory(prefix="leonida_gpuav_selection_") as scratch: + root = Path(scratch) + selected = run_case(executable, root / "selected", "cs_user_data") + if selected is None: + print("SKIP: no usable Vulkan device") + return 77 + if not selected: + raise RuntimeError("named selection produced no instrumented module; " + "check module naming and the installed validation layer") + unmatched = run_case(executable, root / "unmatched", "__leonida_no_matching_shader__") + if unmatched is None: + raise RuntimeError("Vulkan device became unavailable during the check") + if unmatched: + raise RuntimeError("unmatched selection instrumented a shader") + print(f"PASS: selected {selected} module(s), unmatched 0; compute readback checks passed") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main())