diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-24 19:02:22 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-24 19:54:46 -0500 |
| commit | ea43b63b7a3ec5e89100b7bc15a8fbd06a435fdd (patch) | |
| tree | 5f0a973c6e1f8479f7cb165ee46690e5ccbc71dc | |
| parent | 869c88979621cacac26542a0b35b4448fde4f994 (diff) | |
A8 render targets: guard test for device extensions, and the fallback case
From rung-428-on-main (554f0e4ec). The bug that commit fixed, VK_KHR_maintenance5 enabled inside maintenance8's #ifdef, is not in #984's copy of the A8 support: the enable already sits under its own guard here, so only the comment on it comes over. What the commit adds besides still applies:
tests/test_vulkan_extension_guards.py reads the runtime sources and fails on any device extension enabled inside another extension's #ifdef. d3d12_a8_copy_test also forces the fallback (a8_render_target false) on any device: A8_UNORM render targets are refused at creation, in GetResourceAllocationInfo and in the format support, no A8 image is created, and plain A8_UNORM textures still copy.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
| -rw-r--r-- | runtime/shims/d3d12_vulkan_device.cpp | 2 | ||||
| -rw-r--r-- | runtime/test/d3d12_a8_copy_test.cpp | 47 | ||||
| -rw-r--r-- | tests/test_vulkan_extension_guards.py | 102 |
3 files changed, 149 insertions, 2 deletions
diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 308b7dd78..b4a7a9587 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp | |||
| @@ -15080,6 +15080,8 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin | |||
| 15080 | #ifdef VK_KHR_MAINTENANCE_8_EXTENSION_NAME | 15080 | #ifdef VK_KHR_MAINTENANCE_8_EXTENSION_NAME |
| 15081 | if (info.maintenance8) device_extensions.push_back(VK_KHR_MAINTENANCE_8_EXTENSION_NAME); | 15081 | if (info.maintenance8) device_extensions.push_back(VK_KHR_MAINTENANCE_8_EXTENSION_NAME); |
| 15082 | #endif | 15082 | #endif |
| 15083 | // Under its own guard only: headers or a driver without VK_KHR_maintenance8 still have the A8 path, and | ||
| 15084 | // a8_render_target is never set without this extension being enabled. | ||
| 15083 | #ifdef VK_KHR_MAINTENANCE_5_EXTENSION_NAME | 15085 | #ifdef VK_KHR_MAINTENANCE_5_EXTENSION_NAME |
| 15084 | if (info.a8_render_target) device_extensions.push_back(VK_KHR_MAINTENANCE_5_EXTENSION_NAME); | 15086 | if (info.a8_render_target) device_extensions.push_back(VK_KHR_MAINTENANCE_5_EXTENSION_NAME); |
| 15085 | #endif | 15087 | #endif |
diff --git a/runtime/test/d3d12_a8_copy_test.cpp b/runtime/test/d3d12_a8_copy_test.cpp index a83a41810..ff77688f0 100644 --- a/runtime/test/d3d12_a8_copy_test.cpp +++ b/runtime/test/d3d12_a8_copy_test.cpp | |||
| @@ -10,7 +10,11 @@ | |||
| 10 | // wraps vkCreateImage and vkCmdCopyImage behind the instance-proc pointer it hands the layer, so it sees this | 10 | // wraps vkCreateImage and vkCmdCopyImage behind the instance-proc pointer it hands the layer, so it sees this |
| 11 | // without the validation layer; with LEONIDA_VK_VALIDATION=1 and the layer installed, the layer's errors also | 11 | // without the validation layer; with LEONIDA_VK_VALIDATION=1 and the layer installed, the layer's errors also |
| 12 | // fail it; | 12 | // fail it; |
| 13 | // - the same-backing copies (A8 -> A8, R8 -> R8) are the controls and still copy image to image. | 13 | // - the same-backing copies (A8 -> A8, R8 -> R8) are the controls and still copy image to image; |
| 14 | // - a device without the A8 path (a8_render_target false, which VK_KHR_maintenance5 not being enabled means) | ||
| 15 | // takes the documented fallback: A8_UNORM render targets refused, other A8_UNORM textures R8_UNORM images that | ||
| 16 | // copy as before. tests/test_vulkan_extension_guards.py checks that the extension is enabled wherever the path | ||
| 17 | // is taken. | ||
| 14 | // Synthetic data only. | 18 | // Synthetic data only. |
| 15 | #include "vk_test_device.h" | 19 | #include "vk_test_device.h" |
| 16 | #include "../shims/d3d12_com.h" | 20 | #include "../shims/d3d12_com.h" |
| @@ -290,7 +294,46 @@ int main() { | |||
| 290 | ++failures; | 294 | ++failures; |
| 291 | } | 295 | } |
| 292 | } else { | 296 | } else { |
| 293 | std::printf("A8 render targets: not on this device, only the R8-backed copies below run\n"); | 297 | std::printf("A8 render targets: not on this device, only the fallback below runs\n"); |
| 298 | } | ||
| 299 | |||
| 300 | // The documented fallback of a device without VK_FORMAT_A8_UNORM_KHR (or without VK_KHR_maintenance5 enabled, | ||
| 301 | // a8_render_target false; forced here on any device): A8_UNORM render targets are refused at creation and in | ||
| 302 | // the format support, and every other A8_UNORM texture is an R8_UNORM image that copies as before. | ||
| 303 | { | ||
| 304 | Ref<ID3D12Device> device; | ||
| 305 | if (!make_device(vk, false, device)) return 1; | ||
| 306 | const unsigned a8_before = a8_images; | ||
| 307 | D3D12_FEATURE_DATA_FORMAT_SUPPORT support{DXGI_FORMAT_A8_UNORM}; | ||
| 308 | if (FAILED(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &support, sizeof(support))) || | ||
| 309 | !(support.Support1 & D3D12_FORMAT_SUPPORT1_TEXTURE2D) || | ||
| 310 | (support.Support1 & (D3D12_FORMAT_SUPPORT1_RENDER_TARGET | D3D12_FORMAT_SUPPORT1_BLENDABLE))) { | ||
| 311 | std::fprintf(stderr, "fallback: A8_UNORM format support 0x%08x (want TEXTURE2D, no RENDER_TARGET or " | ||
| 312 | "BLENDABLE)\n", unsigned(support.Support1)); | ||
| 313 | ++failures; | ||
| 314 | } | ||
| 315 | const D3D12_RESOURCE_ALLOCATION_INFO size = device->GetResourceAllocationInfo(0, 1, &target); | ||
| 316 | const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT}; | ||
| 317 | Ref<ID3D12Resource> refused; | ||
| 318 | const HRESULT hr = device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &target, | ||
| 319 | D3D12_RESOURCE_STATE_COMMON, nullptr, | ||
| 320 | IID_PPV_ARGS(&refused.p)); | ||
| 321 | if (SUCCEEDED(hr) || refused.p || size.SizeInBytes != UINT64_MAX) { | ||
| 322 | std::fprintf(stderr, "fallback: an A8_UNORM render target was not refused (hr 0x%08lx, size %llu)\n", | ||
| 323 | static_cast<unsigned long>(hr), static_cast<unsigned long long>(size.SizeInBytes)); | ||
| 324 | ++failures; | ||
| 325 | } else { | ||
| 326 | std::printf("fallback: A8_UNORM render target refused (hr 0x%08lx), format support 0x%08x\n", | ||
| 327 | static_cast<unsigned long>(hr), unsigned(support.Support1)); | ||
| 328 | } | ||
| 329 | if (!copy_case(device.p, "fallback: CopyResource A8 texture -> A8 texture", plain, plain, true) || | ||
| 330 | !copy_case(device.p, "fallback: CopyTextureRegion A8 texture -> A8 texture (3x3 box)", plain, plain, | ||
| 331 | false)) | ||
| 332 | ++failures; | ||
| 333 | if (a8_images != a8_before) { | ||
| 334 | std::fprintf(stderr, "fallback: %u VK_FORMAT_A8_UNORM_KHR images created\n", a8_images - a8_before); | ||
| 335 | ++failures; | ||
| 336 | } | ||
| 294 | } | 337 | } |
| 295 | 338 | ||
| 296 | if (unknown_copies) { | 339 | if (unknown_copies) { |
diff --git a/tests/test_vulkan_extension_guards.py b/tests/test_vulkan_extension_guards.py new file mode 100644 index 000000000..d562f48e1 --- /dev/null +++ b/tests/test_vulkan_extension_guards.py | |||
| @@ -0,0 +1,102 @@ | |||
| 1 | """A Vulkan device extension is enabled under its own header guard, never under another extension's. | ||
| 2 | |||
| 3 | python -I tests/test_vulkan_extension_guards.py | ||
| 4 | |||
| 5 | The runtime builds against whatever Vulkan headers the machine has, so each optional extension sits behind | ||
| 6 | ``#ifdef VK_..._EXTENSION_NAME``. When the line that enables extension A sits inside the guard of extension B, | ||
| 7 | headers that have A but not B drop A from the device while the code that detected A still uses it. That happened | ||
| 8 | with ``VK_KHR_maintenance5`` (the A8_UNORM render-target path, ``VulkanDeviceInfo::a8_render_target``), whose | ||
| 9 | enable line was nested in the ``VK_KHR_maintenance8`` guard of ``create_vulkan_device_standalone``: with | ||
| 10 | Vulkan-Headers 1.4.304 the device used ``VK_FORMAT_A8_UNORM_KHR`` without the extension (review of #428). | ||
| 11 | |||
| 12 | The check reads the runtime's C++ sources, tracks the ``#if``/``#ifdef`` stack, and fails on any | ||
| 13 | ``push_back(VK_X_EXTENSION_NAME)`` with a ``VK_Y_EXTENSION_NAME`` guard (Y != X) open around it. Standard library | ||
| 14 | only; no build, no GPU. | ||
| 15 | """ | ||
| 16 | import os | ||
| 17 | import re | ||
| 18 | import sys | ||
| 19 | import unittest | ||
| 20 | |||
| 21 | ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
| 22 | SOURCES = (os.path.join("runtime", "shims"), os.path.join("runtime", "src"), os.path.join("runtime", "test")) | ||
| 23 | PUSH = re.compile(r"push_back\(\s*(VK_\w+_EXTENSION_NAME)\s*\)") | ||
| 24 | GUARD_NAME = re.compile(r"\b(VK_\w+_EXTENSION_NAME)\b") | ||
| 25 | DIRECTIVE = re.compile(r"^\s*#\s*(if|ifdef|ifndef|elif|else|endif)\b(.*)$") | ||
| 26 | |||
| 27 | |||
| 28 | def foreign_guards(text): | ||
| 29 | """[(line, pushed, guard)] for every extension enabled inside another extension's guard.""" | ||
| 30 | stack = [] # one list of guard names per open #if | ||
| 31 | found = [] | ||
| 32 | for number, line in enumerate(text.splitlines(), 1): | ||
| 33 | directive = DIRECTIVE.match(line) | ||
| 34 | if directive: | ||
| 35 | kind, rest = directive.groups() | ||
| 36 | if kind in ("if", "ifdef"): | ||
| 37 | stack.append(GUARD_NAME.findall(rest)) | ||
| 38 | elif kind == "ifndef": | ||
| 39 | stack.append([]) # the extension is absent inside: not a guard that enables anything | ||
| 40 | elif kind in ("elif", "else"): | ||
| 41 | if stack: | ||
| 42 | stack[-1] = [] if kind == "else" else GUARD_NAME.findall(rest) | ||
| 43 | elif kind == "endif" and stack: | ||
| 44 | stack.pop() | ||
| 45 | continue | ||
| 46 | for pushed in PUSH.findall(line): | ||
| 47 | for guards in stack: | ||
| 48 | for guard in guards: | ||
| 49 | if guard != pushed: | ||
| 50 | found.append((number, pushed, guard)) | ||
| 51 | return found | ||
| 52 | |||
| 53 | |||
| 54 | def runtime_sources(root): | ||
| 55 | for folder in SOURCES: | ||
| 56 | base = os.path.join(root, folder) | ||
| 57 | if not os.path.isdir(base): | ||
| 58 | continue | ||
| 59 | for dirpath, _, files in os.walk(base): | ||
| 60 | for name in sorted(files): | ||
| 61 | if name.endswith((".cpp", ".h", ".hpp")): | ||
| 62 | yield os.path.join(dirpath, name) | ||
| 63 | |||
| 64 | |||
| 65 | class ExtensionGuards(unittest.TestCase): | ||
| 66 | def test_detects_a_foreign_guard(self): | ||
| 67 | nested = ( | ||
| 68 | "#ifdef VK_KHR_MAINTENANCE_8_EXTENSION_NAME\n" | ||
| 69 | " if (m8) v.push_back(VK_KHR_MAINTENANCE_8_EXTENSION_NAME);\n" | ||
| 70 | "#ifdef VK_KHR_MAINTENANCE_5_EXTENSION_NAME\n" | ||
| 71 | " if (a8) v.push_back(VK_KHR_MAINTENANCE_5_EXTENSION_NAME);\n" | ||
| 72 | "#endif\n" | ||
| 73 | "#endif\n") | ||
| 74 | self.assertEqual(foreign_guards(nested), | ||
| 75 | [(4, "VK_KHR_MAINTENANCE_5_EXTENSION_NAME", "VK_KHR_MAINTENANCE_8_EXTENSION_NAME")]) | ||
| 76 | |||
| 77 | def test_accepts_own_guards_and_else_branches(self): | ||
| 78 | flat = ( | ||
| 79 | "#ifdef VK_KHR_MAINTENANCE_8_EXTENSION_NAME\n" | ||
| 80 | " if (m8) v.push_back(VK_KHR_MAINTENANCE_8_EXTENSION_NAME);\n" | ||
| 81 | "#endif\n" | ||
| 82 | "#if defined(VK_KHR_MAINTENANCE_5_EXTENSION_NAME) && defined(_WIN32)\n" | ||
| 83 | " if (a8) v.push_back(VK_KHR_MAINTENANCE_5_EXTENSION_NAME);\n" | ||
| 84 | "#endif\n" | ||
| 85 | "#ifdef VK_EXT_FOO_EXTENSION_NAME\n" | ||
| 86 | "#else\n" | ||
| 87 | " v.push_back(VK_EXT_BAR_EXTENSION_NAME);\n" | ||
| 88 | "#endif\n") | ||
| 89 | self.assertEqual(foreign_guards(flat), []) | ||
| 90 | |||
| 91 | def test_runtime_sources(self): | ||
| 92 | problems = [] | ||
| 93 | for path in runtime_sources(ROOT): | ||
| 94 | with open(path, encoding="utf-8", errors="replace") as f: | ||
| 95 | for line, pushed, guard in foreign_guards(f.read()): | ||
| 96 | problems.append("%s:%d enables %s inside the guard of %s" | ||
| 97 | % (os.path.relpath(path, ROOT), line, pushed, guard)) | ||
| 98 | self.assertEqual(problems, [], "\n".join(problems)) | ||
| 99 | |||
| 100 | |||
| 101 | if __name__ == "__main__": | ||
| 102 | sys.exit(0 if unittest.main(exit=False).result.wasSuccessful() else 1) | ||