mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-25 18:48:51 -04:00
Evil HD (SMAA Ultra, 16x filtering, full textures, ambient occlusion with bounce light, sunlight shafts, foliage drawn 4x as far, the remastered texture pack's loader and the launcher's download of it), FSR 3.1 frame generation for every GPU, Intel XeSS, FSR 4 model choice, ReShade presets, Auto (this PC), HUD placement, the options menu in 13 languages, a second input for every control, hold to toggle, DualSense speaker and haptics, NFC from the player's own dumps, the above-60 fps game fixes, the Debug tab's flight and Inventory fixes, the launcher's full settings, and over 250 fixes.
194 lines
8.7 KiB
Python
194 lines
8.7 KiB
Python
"""The game's CPU target follows the PC that builds it, and the AppImage says up front when its renderer cannot run
|
|
(nx2/host_cpu.py, nx2/make_game.py's check_cpu; issue 363). Game-free.
|
|
|
|
python -I -B tests/test_cpu_target.py
|
|
"""
|
|
import builtins
|
|
import contextlib
|
|
import importlib.util
|
|
import io
|
|
import os
|
|
from pathlib import Path
|
|
import sys
|
|
import unittest
|
|
from unittest import mock
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def load(name, path):
|
|
spec = importlib.util.spec_from_file_location(name, path)
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
host_cpu = load("host_cpu_under_test", ROOT / "nx2" / "host_cpu.py")
|
|
|
|
# (/proc/cpuinfo flags, cut to what matters plus some noise)
|
|
HASWELL = "fpu sse sse2 pni ssse3 fma cx16 sse4_1 sse4_2 movbe popcnt xsave avx f16c lahf_lm abm bmi1 avx2 bmi2"
|
|
IVY_BRIDGE = "fpu sse sse2 pni ssse3 cx16 sse4_1 sse4_2 popcnt xsave avx f16c lahf_lm" # (the Xeon E5-2667 v2 of 363)
|
|
CORE2 = "fpu sse sse2 pni ssse3 cx16 lahf_lm"
|
|
|
|
|
|
def cpuinfo(flags):
|
|
return "processor\t: 0\nvendor_id\t: GenuineIntel\nflags\t\t: %s\nvmx flags\t: ept\n\nprocessor\t: 1\n" \
|
|
"flags\t\t: %s\n" % (flags, flags)
|
|
|
|
|
|
def march_on_linux(text):
|
|
real_open = builtins.open
|
|
|
|
def fake_open(path, *args, **kwargs):
|
|
if path == "/proc/cpuinfo":
|
|
return io.StringIO(text)
|
|
return real_open(path, *args, **kwargs)
|
|
with mock.patch.object(host_cpu.os, "name", "posix"), mock.patch.object(builtins, "open", fake_open):
|
|
return host_cpu.host_march()
|
|
|
|
|
|
class HostMarch(unittest.TestCase):
|
|
def test_levels_from_cpuinfo(self):
|
|
self.assertEqual(march_on_linux(cpuinfo(HASWELL)), "x86-64-v3")
|
|
self.assertEqual(march_on_linux(cpuinfo(IVY_BRIDGE)), "x86-64-v2")
|
|
self.assertEqual(march_on_linux(cpuinfo(CORE2)), "x86-64")
|
|
|
|
def test_unreadable_cpu_keeps_v3(self):
|
|
self.assertEqual(march_on_linux("processor\t: 0\n"), "x86-64-v3")
|
|
|
|
def test_windows(self):
|
|
class K32:
|
|
def __init__(self, present):
|
|
self.IsProcessorFeaturePresent = lambda pf: pf in present
|
|
for present, want in (({38, 40}, "x86-64-v3"), ({38}, "x86-64-v2"), (set(), "x86-64")):
|
|
ctypes = mock.MagicMock()
|
|
ctypes.windll.kernel32 = K32(present)
|
|
with mock.patch.object(host_cpu.os, "name", "nt"), mock.patch.dict(sys.modules, {"ctypes": ctypes}):
|
|
self.assertEqual(host_cpu.host_march(), want, present)
|
|
|
|
def test_steamdeck_mtune(self):
|
|
real_open = builtins.open
|
|
|
|
def fake_deck(path, *args, **kwargs):
|
|
if path == "/proc/cpuinfo":
|
|
return io.StringIO("model name\t: AMD Custom APU 0405\nflags\t\t: fpu\n")
|
|
return real_open(path, *args, **kwargs)
|
|
|
|
with mock.patch.object(host_cpu.os, "name", "posix"), mock.patch.object(builtins, "open", fake_deck), \
|
|
mock.patch.dict(os.environ, {}, clear=True):
|
|
self.assertEqual(host_cpu.host_mtune(), "znver2")
|
|
|
|
with mock.patch.dict(os.environ, {"RX_STEAMDECK": "1"}, clear=True):
|
|
self.assertEqual(host_cpu.host_mtune(), "znver2")
|
|
|
|
with mock.patch.object(host_cpu.os, "name", "nt"), mock.patch.dict(os.environ, {}, clear=True):
|
|
self.assertEqual(host_cpu.host_mtune(), "") # (no guess on Windows: RX_MTUNE or RX_STEAMDECK say so)
|
|
|
|
def test_build_and_appimage_use_it(self):
|
|
build = (ROOT / "nx2" / "build.py").read_text(encoding="utf-8")
|
|
self.assertIn("from host_cpu import host_march", build)
|
|
self.assertIn('MARCH = os.environ.get("RX_MARCH") or host_march()', build)
|
|
appimage = (ROOT / "scripts" / "linux" / "appimage.sh").read_text(encoding="utf-8")
|
|
self.assertIn("RX_MARCH=x86-64-v3 RX_VK_ONLY=1", appimage) # (its prebuilt renderer: not the CI runner's CPU)
|
|
|
|
|
|
class AppImageCheck(unittest.TestCase):
|
|
"""make_game.py stops before reading the game when the AppImage's x86-64-v3 renderer meets a CPU without AVX2"""
|
|
|
|
def setUp(self):
|
|
self.make_game = load("make_game_cpu_test", ROOT / "nx2" / "make_game.py")
|
|
|
|
def run_check(self, env, march):
|
|
fake = mock.MagicMock(host_march=mock.MagicMock(return_value=march))
|
|
with mock.patch.dict(os.environ, {}, clear=False), mock.patch.dict(sys.modules, {"host_cpu": fake}), \
|
|
contextlib.redirect_stdout(io.StringIO()) as out:
|
|
for k in ("HOOKSHOT_VK_PREBUILT", "RX_MARCH"):
|
|
os.environ.pop(k, None)
|
|
os.environ.update(env)
|
|
try:
|
|
self.make_game.check_cpu()
|
|
except SystemExit as stopped:
|
|
return stopped.code, out.getvalue()
|
|
return None, out.getvalue()
|
|
|
|
def test_appimage_without_avx2_stops(self):
|
|
code, out = self.run_check({"HOOKSHOT_VK_PREBUILT": "/x/hookshot_vk.so"}, "x86-64-v2")
|
|
self.assertEqual(code, 1)
|
|
self.assertIn("AVX2", out)
|
|
|
|
def test_appimage_with_avx2_goes_on(self):
|
|
self.assertIsNone(self.run_check({"HOOKSHOT_VK_PREBUILT": "/x/hookshot_vk.so"}, "x86-64-v3")[0])
|
|
|
|
def test_source_build_without_avx2_goes_on(self):
|
|
self.assertIsNone(self.run_check({}, "x86-64-v2")[0])
|
|
|
|
def test_rx_march_is_the_players_choice(self):
|
|
env = {"HOOKSHOT_VK_PREBUILT": "/x/hookshot_vk.so", "RX_MARCH": "x86-64-v2"}
|
|
self.assertIsNone(self.run_check(env, "x86-64-v2")[0])
|
|
|
|
def test_checked_before_the_game_is_read(self):
|
|
src = (ROOT / "nx2" / "make_game.py").read_text(encoding="utf-8")
|
|
main = src[src.index("def main("):]
|
|
self.assertLess(main.index("check_cpu()"), main.index("drop_stale_objects(work)"))
|
|
check = src[src.index("def check(a, work):"):]
|
|
self.assertLess(check.index("check_cpu()"), check.index("if a.nup:"))
|
|
|
|
|
|
def clang_says(cpu, feats, tune=None):
|
|
"""What `clang -march=native -###` writes to stderr, cut to the cc1 line's parts that matter"""
|
|
words = ['"/usr/bin/clang-23"', '"-cc1"', '"-triple"', '"x86_64-unknown-linux-gnu"']
|
|
if cpu:
|
|
words += ['"-target-cpu"', '"%s"' % cpu]
|
|
for f in feats:
|
|
words += ['"-target-feature"', '"%s"' % f]
|
|
if tune:
|
|
words += ['"-tune-cpu"', '"%s"' % tune]
|
|
return "clang version 23.1.5\nTarget: x86_64-unknown-linux-gnu\n " + " ".join(words) + "\n"
|
|
|
|
|
|
class NativeCpu(unittest.TestCase):
|
|
"""((Triforce) HOOKSHOT_NATIVE_CPU) -march=native is keyed by the CPU clang resolves it to: another CPU, another key"""
|
|
|
|
def native(self, stderr=None, raises=None):
|
|
run = mock.MagicMock(side_effect=raises) if raises else \
|
|
mock.MagicMock(return_value=mock.MagicMock(stderr=stderr, stdout=""))
|
|
with mock.patch("subprocess.run", run):
|
|
return host_cpu.native_cpu("clang", ["-march=native"]), run
|
|
|
|
def test_cpu_tune_and_features_name_it(self):
|
|
zen4, run = self.native(clang_says("znver4", ["+avx512f", "+avx2", "-amx-tile"], "znver4"))
|
|
self.assertTrue(zen4.startswith("znver4_znver4_"), zen4)
|
|
self.assertIn("-###", run.call_args[0][0])
|
|
self.assertIn("-march=native", run.call_args[0][0])
|
|
# (the feature list's order is not a change)
|
|
self.assertEqual(self.native(clang_says("znver4", ["-amx-tile", "+avx2", "+avx512f"], "znver4"))[0], zen4)
|
|
|
|
def test_another_cpu_another_name(self):
|
|
zen4 = self.native(clang_says("znver4", ["+avx512f", "+avx2"]))[0]
|
|
# (the same model with AVX-512 off, as some BIOSes and VMs do: the name alone would not tell them apart)
|
|
no512 = self.native(clang_says("znver4", ["-avx512f", "+avx2"]))[0]
|
|
zen3 = self.native(clang_says("znver3", ["+avx2"]))[0]
|
|
self.assertEqual(len({zen4, no512, zen3}), 3)
|
|
self.assertTrue(zen3.startswith("znver3_znver3_"), zen3) # (no -tune-cpu: tuned for the CPU itself)
|
|
|
|
def test_no_answer_is_none(self):
|
|
self.assertIsNone(self.native(clang_says(None, ["+avx2"]))[0])
|
|
self.assertIsNone(self.native(clang_says("native", []))[0])
|
|
self.assertIsNone(self.native("")[0])
|
|
self.assertIsNone(self.native(raises=OSError("no clang"))[0])
|
|
|
|
|
|
def test_build_keys_it_and_leaves_it_off(self):
|
|
build = (ROOT / "nx2" / "build.py").read_text(encoding="utf-8")
|
|
start = build.index('_CLANG = toolchain.find("clang")')
|
|
block = build[start:build.index("MTUNE = ", start)]
|
|
# (off unless asked; native only where the CPU chose v3, never over an RX_MARCH; its name is in the flags, so in every key)
|
|
self.assertIn('os.environ.get("HOOKSHOT_NATIVE_CPU") == "1" and not os.environ.get("RX_MARCH") and MARCH == "x86-64-v3"',
|
|
block)
|
|
self.assertIn('CPU += ["-DHOOKSHOT_NATIVE_CPU=" + _native]', block)
|
|
self.assertIn("sys.exit(", block)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|