Files
xcl/scripts/gen_d3d12_layout.py
2026-11-20 21:03:53 +01:00

53 lines
2.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Generate crates/xcl/src/hle/d3d12_layout.rs from d3d12.h (microsoft/DirectX-Headers, MIT).
Usage: python -I scripts/gen_d3d12_layout.py <d3d12.h> crates/xcl/src/hle/d3d12_layout.rs
Extracts, for every ID3D12* COM interface: IID, parent interface and the full vtable
method order (including inherited IUnknown/ID3D12Object methods) from the C-style
`XxxVtbl` structs. XCL uses PC order as the first guess for D3D12.X objects and names
every slot in its logs/reports.
"""
import re
import sys
def main(src, dst):
text = open(src, encoding="utf-8", errors="replace").read()
iids = {}
parents = {}
for m in re.finditer(r'MIDL_INTERFACE\("([0-9a-fA-F-]{36})"\)\s*(\w+)\s*:\s*public\s+(\w+)', text):
iids[m.group(2)] = m.group(1).lower()
parents[m.group(2)] = m.group(3)
vtbls = {}
for m in re.finditer(r"typedef struct (\w+)Vtbl\s*\{(.*?)\}\s*\1Vtbl;", text, re.S):
name, body = m.group(1), m.group(2)
raw = re.findall(r"\(\s*STDMETHODCALLTYPE\s*\*\s*(\w+)\s*\)", body)
# struct-returning methods are declared twice (#if !defined(_WIN32) / #else)
methods = [m for i, m in enumerate(raw) if i == 0 or raw[i - 1] != m]
vtbls[name] = methods
names = sorted(n for n in vtbls if n.startswith("ID3D12") and n in iids)
out = [
"// @generated by scripts/gen_d3d12_layout.py from d3d12.h (microsoft/DirectX-Headers, MIT) – do not edit.",
"",
"pub struct Iface {",
" pub name: &'static str,",
" pub iid: &'static str,",
" pub parent: &'static str,",
" pub methods: &'static [&'static str],",
"}",
"",
"pub static IFACES: &[Iface] = &[",
]
for n in names:
ms = ", ".join(f'"{x}"' for x in vtbls[n])
out.append(f' Iface {{ name: "{n}", iid: "{iids[n]}", parent: "{parents.get(n, "")}", methods: &[{ms}] }},')
out.append("];")
open(dst, "w", encoding="utf-8", newline="\n").write("\n".join(out) + "\n")
dev = max((n for n in names if re.fullmatch(r"ID3D12Device\d*", n)), key=lambda n: len(vtbls[n]))
print(f"{len(names)} interfaces; most derived device {dev} with {len(vtbls[dev])} methods -> {dst}")
if __name__ == "__main__":
main(sys.argv[1], sys.argv[2])