"""The Insert menu's GPU device row (gpu/vk/gpu_choice.h and its use in vk_main.cpp's pick_device), no game or GPU required. python -I -B tests/test_gpu_choice.py [--cxx g++] """ import argparse import subprocess import tempfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] def preset_exempt(menu, value): """the menu has no quality preset that a change of the row's could reset ((Triforce, options_version 7) the Quality preset row is gone, and with it the memory budget it set: the card's own), and the row is on its tab""" start = menu.index("&opts.%s," % value, menu.index("Row rows[] = {")) row = menu[start:menu.index("},", start)] return bool(row) and "opts.preset" not in menu and "void apply_preset" not in menu def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--cxx", default="g++") args = parser.parse_args() vk = ROOT / "gpu" / "vk" main_src = (vk / "vk_main.cpp").read_text(encoding="utf-8") pick = main_src[main_src.index("bool pick_device() {"):] pick = pick[:pick.index("\n}\n")] for need, why in (("menu_init()", "the saved choice is read before it is used"), ("menu_set_gpu_count((int)valid_gpus.size())", "the row offers only the usable GPUs"), ("p.apiVersion >= VK_API_VERSION_1_3", "only Vulkan 1.3 GPUs are listed"), ('getenv("RX_VK_DEVICE")', "RX_VK_DEVICE wins over the row"), ("gpu_choice::pick(menu_gpu_name(), gpu_device_names, opts.gpu_device)", "the row's choice is taken, by its saved name first")): if need not in pick: raise SystemExit("pick_device: %s is missing (%s)" % (need, why)) if pick.index("menu_init()") > pick.index("gpu_choice::pick("): raise SystemExit("pick_device reads the GPU row before menu_init") if pick.index('getenv("RX_VK_DEVICE")') > pick.index("gpu_choice::pick("): raise SystemExit("pick_device takes the GPU row before RX_VK_DEVICE") # ((Triforce) 11-22) a number saved without a name on the processor graphics: Auto, after RX_VK_DEVICE and before pick, the # types beside the names, and the row set to the card Auto took unnamed = "gpu_choice::unnamed_integrated(menu_gpu_name(), gpu_device_names, valid_types, opts.gpu_device)" if unnamed not in pick: raise SystemExit("pick_device: a number without a name on the processor graphics is taken (no unnamed_integrated)") if not pick.index('getenv("RX_VK_DEVICE")') < pick.index(unnamed) < pick.index("gpu_choice::pick("): raise SystemExit("pick_device: unnamed_integrated is not between RX_VK_DEVICE and the row's pick") if "valid_types.push_back((int)p.deviceType)" not in pick or "gpu_device_names.push_back" not in pick: raise SystemExit("pick_device: the device types are not listed beside the names") fix = pick.index("if (unnamed_integrated) {") if not pick.index("best = (int)i") < fix < pick.index("dev.physical = all[best]") or "opts.gpu_device = (int)k + 1" not in pick[fix:]: raise SystemExit("pick_device: the row does not follow the card Auto took for an unnamed number") menu = (vk / "vk_menu.cpp").read_text(encoding="utf-8") if not preset_exempt(menu, "gpu_device"): raise SystemExit("vk_menu.cpp: changing the GPU row resets the quality preset (and its VRAM budget) to Custom") with tempfile.TemporaryDirectory(prefix="gpu-choice-") as tmp: exe = Path(tmp) / "gpu-choice" subprocess.run([args.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror", str(ROOT / "tests" / "test_gpu_choice.cpp"), "-o", str(exe)], check=True) subprocess.run([str(exe)], check=True) if __name__ == "__main__": main()