mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 08:58:36 +00:00
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>
103 lines
4.6 KiB
Python
103 lines
4.6 KiB
Python
"""A Vulkan device extension is enabled under its own header guard, never under another extension's.
|
|
|
|
python -I tests/test_vulkan_extension_guards.py
|
|
|
|
The runtime builds against whatever Vulkan headers the machine has, so each optional extension sits behind
|
|
``#ifdef VK_..._EXTENSION_NAME``. When the line that enables extension A sits inside the guard of extension B,
|
|
headers that have A but not B drop A from the device while the code that detected A still uses it. That happened
|
|
with ``VK_KHR_maintenance5`` (the A8_UNORM render-target path, ``VulkanDeviceInfo::a8_render_target``), whose
|
|
enable line was nested in the ``VK_KHR_maintenance8`` guard of ``create_vulkan_device_standalone``: with
|
|
Vulkan-Headers 1.4.304 the device used ``VK_FORMAT_A8_UNORM_KHR`` without the extension (review of #428).
|
|
|
|
The check reads the runtime's C++ sources, tracks the ``#if``/``#ifdef`` stack, and fails on any
|
|
``push_back(VK_X_EXTENSION_NAME)`` with a ``VK_Y_EXTENSION_NAME`` guard (Y != X) open around it. Standard library
|
|
only; no build, no GPU.
|
|
"""
|
|
import os
|
|
import re
|
|
import sys
|
|
import unittest
|
|
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
SOURCES = (os.path.join("runtime", "shims"), os.path.join("runtime", "src"), os.path.join("runtime", "test"))
|
|
PUSH = re.compile(r"push_back\(\s*(VK_\w+_EXTENSION_NAME)\s*\)")
|
|
GUARD_NAME = re.compile(r"\b(VK_\w+_EXTENSION_NAME)\b")
|
|
DIRECTIVE = re.compile(r"^\s*#\s*(if|ifdef|ifndef|elif|else|endif)\b(.*)$")
|
|
|
|
|
|
def foreign_guards(text):
|
|
"""[(line, pushed, guard)] for every extension enabled inside another extension's guard."""
|
|
stack = [] # one list of guard names per open #if
|
|
found = []
|
|
for number, line in enumerate(text.splitlines(), 1):
|
|
directive = DIRECTIVE.match(line)
|
|
if directive:
|
|
kind, rest = directive.groups()
|
|
if kind in ("if", "ifdef"):
|
|
stack.append(GUARD_NAME.findall(rest))
|
|
elif kind == "ifndef":
|
|
stack.append([]) # the extension is absent inside: not a guard that enables anything
|
|
elif kind in ("elif", "else"):
|
|
if stack:
|
|
stack[-1] = [] if kind == "else" else GUARD_NAME.findall(rest)
|
|
elif kind == "endif" and stack:
|
|
stack.pop()
|
|
continue
|
|
for pushed in PUSH.findall(line):
|
|
for guards in stack:
|
|
for guard in guards:
|
|
if guard != pushed:
|
|
found.append((number, pushed, guard))
|
|
return found
|
|
|
|
|
|
def runtime_sources(root):
|
|
for folder in SOURCES:
|
|
base = os.path.join(root, folder)
|
|
if not os.path.isdir(base):
|
|
continue
|
|
for dirpath, _, files in os.walk(base):
|
|
for name in sorted(files):
|
|
if name.endswith((".cpp", ".h", ".hpp")):
|
|
yield os.path.join(dirpath, name)
|
|
|
|
|
|
class ExtensionGuards(unittest.TestCase):
|
|
def test_detects_a_foreign_guard(self):
|
|
nested = (
|
|
"#ifdef VK_KHR_MAINTENANCE_8_EXTENSION_NAME\n"
|
|
" if (m8) v.push_back(VK_KHR_MAINTENANCE_8_EXTENSION_NAME);\n"
|
|
"#ifdef VK_KHR_MAINTENANCE_5_EXTENSION_NAME\n"
|
|
" if (a8) v.push_back(VK_KHR_MAINTENANCE_5_EXTENSION_NAME);\n"
|
|
"#endif\n"
|
|
"#endif\n")
|
|
self.assertEqual(foreign_guards(nested),
|
|
[(4, "VK_KHR_MAINTENANCE_5_EXTENSION_NAME", "VK_KHR_MAINTENANCE_8_EXTENSION_NAME")])
|
|
|
|
def test_accepts_own_guards_and_else_branches(self):
|
|
flat = (
|
|
"#ifdef VK_KHR_MAINTENANCE_8_EXTENSION_NAME\n"
|
|
" if (m8) v.push_back(VK_KHR_MAINTENANCE_8_EXTENSION_NAME);\n"
|
|
"#endif\n"
|
|
"#if defined(VK_KHR_MAINTENANCE_5_EXTENSION_NAME) && defined(_WIN32)\n"
|
|
" if (a8) v.push_back(VK_KHR_MAINTENANCE_5_EXTENSION_NAME);\n"
|
|
"#endif\n"
|
|
"#ifdef VK_EXT_FOO_EXTENSION_NAME\n"
|
|
"#else\n"
|
|
" v.push_back(VK_EXT_BAR_EXTENSION_NAME);\n"
|
|
"#endif\n")
|
|
self.assertEqual(foreign_guards(flat), [])
|
|
|
|
def test_runtime_sources(self):
|
|
problems = []
|
|
for path in runtime_sources(ROOT):
|
|
with open(path, encoding="utf-8", errors="replace") as f:
|
|
for line, pushed, guard in foreign_guards(f.read()):
|
|
problems.append("%s:%d enables %s inside the guard of %s"
|
|
% (os.path.relpath(path, ROOT), line, pushed, guard))
|
|
self.assertEqual(problems, [], "\n".join(problems))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(0 if unittest.main(exit=False).result.wasSuccessful() else 1)
|