gpu_adapters.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """Windows graphics adapters as DXGI reports them.
  2. Task Manager's "GPU memory" for an adapter is DXGI's dedicated video memory plus shared system
  3. memory, and that sum is what Windows lets the adapter allocate. An integrated GPU has almost no
  4. dedicated memory, so its shared allowance is its real pool: an Intel Arc B390 on a 31.5 GB machine
  5. gets 18 GB. Read through ctypes without environment-variable input or subprocesses.
  6. """
  7. from __future__ import annotations
  8. import ctypes
  9. import functools
  10. from contextlib import suppress
  11. from ctypes import wintypes
  12. from dataclasses import dataclass
  13. _SOFTWARE_FLAG = 0x2 # DXGI_ADAPTER_FLAG_SOFTWARE (Microsoft Basic Render Driver)
  14. _MAX_ADAPTERS = 16
  15. # vtable slots: IUnknown::Release, IDXGIFactory1::EnumAdapters1, IDXGIAdapter1::GetDesc1.
  16. _RELEASE, _ENUM_ADAPTERS1, _GET_DESC1 = 2, 12, 10
  17. @dataclass(frozen=True)
  18. class Adapter:
  19. description: str
  20. vendor_id: int
  21. dedicated_bytes: int
  22. shared_bytes: int
  23. software: bool
  24. @property
  25. def memory_bytes(self) -> int:
  26. """What Windows lets this adapter allocate (Task Manager's GPU memory)."""
  27. return self.dedicated_bytes + self.shared_bytes
  28. class _Guid(ctypes.Structure):
  29. _fields_ = [("Data1", wintypes.DWORD), ("Data2", wintypes.WORD), ("Data3", wintypes.WORD),
  30. ("Data4", ctypes.c_ubyte * 8)]
  31. class _Luid(ctypes.Structure):
  32. _fields_ = [("LowPart", wintypes.DWORD), ("HighPart", wintypes.LONG)]
  33. class _AdapterDesc1(ctypes.Structure):
  34. _fields_ = [("Description", ctypes.c_wchar * 128), ("VendorId", wintypes.UINT),
  35. ("DeviceId", wintypes.UINT), ("SubSysId", wintypes.UINT), ("Revision", wintypes.UINT),
  36. ("DedicatedVideoMemory", ctypes.c_size_t), ("DedicatedSystemMemory", ctypes.c_size_t),
  37. ("SharedSystemMemory", ctypes.c_size_t), ("AdapterLuid", _Luid), ("Flags", wintypes.UINT)]
  38. _IID_DXGI_FACTORY1 = _Guid(0x770AAE78, 0xF26F, 0x4DBA, (ctypes.c_ubyte * 8)(0xA8, 0x29, 0x25, 0x3C, 0x83, 0xD1, 0xB3, 0x87))
  39. def _method(obj: ctypes.c_void_p, slot: int, *argtypes):
  40. vtable = ctypes.cast(obj, ctypes.POINTER(ctypes.POINTER(ctypes.c_void_p)))[0]
  41. return ctypes.WINFUNCTYPE(ctypes.c_long, ctypes.c_void_p, *argtypes)(vtable[slot])
  42. @functools.cache
  43. def windows_gpu_adapters() -> tuple[Adapter, ...]:
  44. """Every adapter DXGI enumerates, in its order; empty off Windows or when DXGI can't answer."""
  45. with suppress(OSError, AttributeError):
  46. factory = ctypes.c_void_p()
  47. if ctypes.WinDLL("dxgi").CreateDXGIFactory1(ctypes.byref(_IID_DXGI_FACTORY1), ctypes.byref(factory)) != 0:
  48. return ()
  49. found: list[Adapter] = []
  50. try:
  51. enum = _method(factory, _ENUM_ADAPTERS1, wintypes.UINT, ctypes.POINTER(ctypes.c_void_p))
  52. for index in range(_MAX_ADAPTERS):
  53. adapter = ctypes.c_void_p()
  54. if enum(factory, index, ctypes.byref(adapter)) != 0: # DXGI_ERROR_NOT_FOUND ends the list
  55. break
  56. try:
  57. desc = _AdapterDesc1()
  58. if _method(adapter, _GET_DESC1, ctypes.POINTER(_AdapterDesc1))(adapter, ctypes.byref(desc)) == 0:
  59. found.append(Adapter(desc.Description, desc.VendorId, desc.DedicatedVideoMemory,
  60. desc.SharedSystemMemory, bool(desc.Flags & _SOFTWARE_FLAG)))
  61. finally:
  62. _method(adapter, _RELEASE)(adapter)
  63. finally:
  64. _method(factory, _RELEASE)(factory)
  65. return tuple(found)
  66. return ()