mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 18:58:37 +00:00
647 lines
37 KiB
Python
647 lines
37 KiB
Python
"""Run the test shaders that build.py made on this machine's GPU and compare with what each one must give.
|
|
|
|
python -I runtime/test/shaders/check.py <out dir of build.py> [dir with the test programs]
|
|
|
|
The test programs are vk_compute_test, vk_store_test, vk_harness, vk_texture_test and vk_bindless_test (runtime/build by default). With
|
|
the Visual Studio generator they are in runtime/build/RelWithDebInfo, Release and so on: that subfolder is found by
|
|
itself (the newest complete one if there are several) and its name is printed.
|
|
Each line is one shader: ok, or what came back instead. The wave32 test ps_bitmask_perm is only judged on lanes
|
|
0-31, since a GPU with 64-wide subgroups puts half the pixels in lanes it wasn't written for.
|
|
The two whole-wave mask checks are skipped when the device cannot provide a full 64-wide compute subgroup.
|
|
The cs_w64x_* / ms_w64x_* cross-half tests (lanes 32-63 against lanes 0-31, see their .asm headers) are skipped there
|
|
too. With LEONIDA_W64_EMULATE=1 they run without that requirement, on a device whose subgroups are narrower (NVIDIA),
|
|
to check a translator that emulates wave64 on them. scripts/run_gpu_tests.py builds the mesh fixtures and
|
|
fs_passthru.spv; here the mesh checks run when those files are in <out dir>/spirv.
|
|
ps_tex is skipped when the device lacks a feature the bindless set or the shader needs.
|
|
ps_texoff (texel offsets, on vk_texture_test's texture) is skipped on a device without VK_KHR_maintenance8.
|
|
"""
|
|
import collections
|
|
import json
|
|
import math
|
|
import os
|
|
import re
|
|
import struct
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
EXE = ".exe" if os.name == "nt" else ""
|
|
UNSUPPORTED = 77 # a test tool exits with this when the device lacks a feature
|
|
|
|
# The programs every run needs. Multi-config CMake generators (Visual Studio, Xcode, Ninja Multi-Config) put each of
|
|
# them in build/<Config> instead of build/ (TEST-38).
|
|
TEST_PROGRAMS = ("vk_compute_test", "vk_store_test", "vk_harness")
|
|
CONFIG_DIRS = ("RelWithDebInfo", "Release", "Debug", "MinSizeRel")
|
|
|
|
|
|
def complete_build_dirs(build, programs):
|
|
"""`build` and its configuration subfolders, in that order, that hold all of `programs` (file names with extension)."""
|
|
return [d for d in (build, *(os.path.join(build, c) for c in CONFIG_DIRS))
|
|
if all(os.path.isfile(os.path.join(d, n)) for n in programs)]
|
|
|
|
|
|
def resolve_build_dir(build, programs):
|
|
"""The folder that holds all of `programs`.
|
|
|
|
Makefile and Ninja builds leave them in `build`. With the Visual Studio generator they land in build/RelWithDebInfo,
|
|
build/Release and so on. `build` itself wins when it has them all; otherwise the configuration folder that has them
|
|
all and was built last. If none has them, `build` is returned, so a "not found" message names the folder given."""
|
|
found = complete_build_dirs(build, programs)
|
|
if not found or found[0] == build:
|
|
return build
|
|
return max(found, key=lambda d: max(os.path.getmtime(os.path.join(d, n)) for n in programs))
|
|
|
|
|
|
def pick_build_dir(build, programs):
|
|
"""resolve_build_dir, and say which folder was picked when it is a configuration subfolder, so a report can name
|
|
the build it ran and a stale pick is visible."""
|
|
chosen = resolve_build_dir(build, programs)
|
|
if chosen != build:
|
|
names = [os.path.basename(d) for d in complete_build_dirs(build, programs)]
|
|
print(f"test programs: {chosen}" + (f" (newest of {', '.join(names)})" if len(names) > 1 else ""))
|
|
return chosen
|
|
|
|
|
|
def not_found_message(build, missing):
|
|
return (f"Test programs not found in {build}: {', '.join(missing)}\n"
|
|
f"(its {', '.join(CONFIG_DIRS)} subfolders were searched too)\n"
|
|
"Build them with cmake -S runtime -B runtime/build, then cmake --build runtime/build.\n"
|
|
"CMake only builds them when it finds Vulkan. If it printed \"Vulkan SDK not found\", install the "
|
|
"Vulkan headers (vulkan-headers on Arch) and configure again.")
|
|
|
|
COMPUTE = [("cs_lds", "4", "mirror64"), ("cs_exec64", "1", "exec64"), ("cs_bvh", "1", "bvh"),
|
|
("cs_workgroup_yz", "2x3x4", "workgroup_yz")]
|
|
CS_STORE = [0x400080ff, 0xe00003ff, 0x3c004000, 0xeeeeeeee, 0x40003c00, 0xbc003800]
|
|
CS_SMEM = [0x5eed0024, 0x5eed0028, 0x5eed002c, 0x5eed0030, 0x5eed0010, 0x5eed0014, 0x5eed0018, 0x5eed001c]
|
|
WAVE32_DUMPS = ("cs_store", "cs_smem_soffset_imm", "cs_wqm_w32", "cs_w32_lane_wrap", "cs_movrelsd_2")
|
|
# Wave32 lane-mask dumps: their EXEC/VCC masks and lane selects cover 32 lanes, so a device whose widest subgroup is
|
|
# narrower (Mesa llvmpipe: 8) can't produce their words. They are skipped there, as ps_bitmask_perm is.
|
|
WAVE32_LANE_DUMPS = ("cs_wqm_w32", "cs_w32_lane_wrap")
|
|
CS_DUMPS = [("cs_store", CS_STORE), ("cs_smem_soffset_imm", CS_SMEM),
|
|
# SHADER-52: wave32 lane select is & 31; lane 40 wraps to 8
|
|
("cs_w32_lane_wrap", [25, 16, 777, 777]),
|
|
# v_movrelsd_2_b32: VGPR[D + M0[25:16]] = VGPR[S0 + M0[9:0]]
|
|
("cs_movrelsd_2", [11, 33, 33, 55, 1717, 77, 1515, 1717]),
|
|
("cs_wave64_ballot", [32, 16, 24, 40, 8, 48]),
|
|
("cs_wm_half_ops", [32, 16, 16, 0, 16]), ("cs_wm_half_cmp", [1, 0, 1, 1]),
|
|
("cs_wm_bfm64", [16, 8, 8, 40]), ("cs_wm_scc_rfl", [55, 48]),
|
|
# mask-only wave64: s_bcnt1_i32_b64 on ordinary SGPRs, no WAVE64_RE EXEC markers (#210 / SHADER-28)
|
|
("cs_w64_mask_detect", [48]),
|
|
("cs_wqm_w64", [20, 4, 12, 8, 1, 0, 15728895, 20]), ("cs_wqm_w32", [12, 1, 4, 0, 8, 15728880, 4, 32]),
|
|
("cs_wqm_w64_half", [11, 14, 0, 1, 6, 8, 12, 2])]
|
|
# num_records bounds check (cs_bounds.asm): one thread, so no 64-wide subgroup requirement.
|
|
BOUNDS = [("cs_bounds", [0x12345678, 0x12345678, 0x00000000, 0xEEEEEEEE])]
|
|
CMPX_CLASS_WORDS = [0xEEEEEEEE] * 3 + [0x22] + [0x11] * 4
|
|
# Negative global_load/global_store offsets (cs_global_neg_offset.asm): 40 groups make a 0x2800-byte heap that holds
|
|
# both the right addresses and the 4096-lower ones; only group 0 thread 0 runs, so no wave64 requirement.
|
|
GLOBAL_OFFSET = [("cs_global_neg_offset", "40", [0x5EED1FFC, 0x8BAD1800, 0x5EED1FF0, 0x5EED1FF4,
|
|
0x0000009E, 0xFFFF8BAD, 0x57023FFC, 0xEEEEEEEE])]
|
|
# cs_user_sgprs.asm: vk_compute_test puts these words in s4..s7 and the shader stores them back.
|
|
USER_SGPRS = [0x4F000000, 0xDEADBEEF, 7, 0xFFFFFFFF]
|
|
USER_SGPRS_ENV = {"VKC_SGPRS": ",".join(hex(w) for w in USER_SGPRS)}
|
|
USER_SGPRS_WORDS = USER_SGPRS + [0xEEEEEEEE] * 4
|
|
# cs_cvt_f32_sat.asm (#300): s4 is a binary32 input, out[0] = v_cvt_u32_f32, out[1] = v_cvt_i32_f32 (truncate,
|
|
# saturate, NaN -> 0). One dispatch per input through VKC_SGPRS; the inputs of tests/semantic/test_float_to_int.py.
|
|
CVT_F32_SAT_CASES = (
|
|
(0x00000000, 0, 0), (0x80000000, 0, 0), (0x00000001, 0, 0), (0x3FC00000, 1, 1), (0xBFC00000, 0, 0xFFFFFFFF),
|
|
(0x4EFFFFFF, 0x7FFFFF80, 0x7FFFFF80), (0x4F000000, 0x80000000, 0x7FFFFFFF), (0x4F7FFFFF, 0xFFFFFF00, 0x7FFFFFFF),
|
|
(0x4F800000, 0xFFFFFFFF, 0x7FFFFFFF), (0xCEFFFFFF, 0, 0x80000080), (0xCF000000, 0, 0x80000000),
|
|
(0xCF000001, 0, 0x80000000), (0x7F800000, 0xFFFFFFFF, 0x7FFFFFFF), (0xFF800000, 0, 0x80000000),
|
|
(0x7F800001, 0, 0), (0xFFC00000, 0, 0))
|
|
# Cross-half wave64 tests (x = cross-half): exact results need 64 lanes, or an emulation of them on narrower subgroups.
|
|
W64_EMULATED = os.environ.get("LEONIDA_W64_EMULATE") == "1"
|
|
W64X_DUMPS = [("cs_w64x_readlane", [121, 121, 16, 97, 16, 100, 100, 121]),
|
|
("cs_w64x_writelane", [4660, 77, 99, 2748, 0, 0, 4660, 99]),
|
|
("cs_w64x_readfirstlane", [140, 140, 163, 132, 100, 105, 133, 48]),
|
|
("cs_w64x_bpermute", [36, 316, 253, 218, 435, 225, 0, 442]),
|
|
("cs_w64x_branch", [0, 0, 0, 0, 5, 5, 9, 9]),
|
|
("cs_w64x_mask_reuse", [8, 40, 8, 40, 2, 20, 10, 64]),
|
|
("cs_w64x_mask_reuse2", [10, 20, 8, 8, 5, 2, 132, 131]),
|
|
("cs_w64x_chain_barrier", [564, 563, 500, 627]), # 128-thread group
|
|
("cs_w64x_ballot", [2053, 2061, 2069, 2077, 2085, 2093, 2101, 2109]),
|
|
("cs_w64x_wg128_branches", [121, 121, 343, 343, 14, 31, 19812, 22728]), # 128-thread group
|
|
("cs_w64x_lds_raw", [132, 137, 100, 108, 131, 101, 149, 118]),
|
|
("cs_w64x_lds_war", [1032, 1063, 1000, 1031, 2032, 2063, 2000, 2031]),
|
|
("cs_w64x_sbarrier", [605, 605, 686, 686, 3032, 3013, 3032, 3031]), # 128-thread group
|
|
("cs_w64x_load_ballot_load", [43, 74, 75, 42, 42, 18, 41, 32])]
|
|
# A 40-thread group (one partial wave) cannot use --require-wave64 (full subgroups), so it asks for subgroup size 64.
|
|
W64X_PARTIAL = [("cs_w64x_partial", [40, 40, 528, 0, 0, 532, 539, 524820])]
|
|
PIXEL_BITS = {"ps_legacy_mul": (0, 0, 0, 0x40c00000),
|
|
"ps_ops": (0x48804300, 5, 7, 0xfffffffc), "ps_ops2": (0xabcd4400, 0x46001234, 0xfffffffa, 7),
|
|
"ps_ops3": (0xfffffffa, 0x44660066, 0x44aabbcc, 16), "ps_ops4": (0x34000010, 0x00120800, 0xbeef0000, 0x12343c00),
|
|
"ps_ops5": (0x12340010, 0xbeef0800, 0xaaaaffff, 0xdeadbc00),
|
|
"ps_ops6": (0xf800ffff, 0x01000010, 0x00800800, 0x8000fff0),
|
|
"ps_ops7": (0x00ff8077, 0xff0000ff, 0x89abcdef, 0x00070003),
|
|
"ps_ops8": (0x80000000, 0xffffffff, 0x000f100e, 0x0f0f0f1f),
|
|
"ps_f32_mods": (0xc0256a01, 0x3f800000, 0x41e00000, 0x3fc00000),
|
|
"ps_scc": (1, 1, 3, 0x80000000), "ps_sdwa": (0x000000f1, 0xfffefff2, 0x3c003344, 0xfff70000),
|
|
"ps_vcc_x2": (0x11223344, 0x55667788, 0, 0),
|
|
"ps_med3_mods": (0x3f800000, 0x40400000, 0x40c00000, 0x3f400000),
|
|
"ps_scc_minmax": (0x00030205, 0x00030207, 0x00030209, 0x00030203),
|
|
"ps_fma_literal": (0x41b80000, 0x41800000, 0x40e00000, 0x41a40000),
|
|
"ps_f64_mods": (0x40040000, 0x3ff00000, 0x3fe40000, 0x3ff00000),
|
|
# f16 op_sel, partial writes, inline constants: words measured on RDNA2 hardware (.asm headers)
|
|
"ps_f16_med3_opsel": (0xaaaa4100, 0xaaaa4200, 0xaaaa3e00, 0x40005555),
|
|
"ps_f16_med3_mods": (0x41005555, 0x3c005555, 0x41003800, 0xaaaaba00),
|
|
"ps_f16_vop2": (0xaaaa4000, 0xaaaac000, 0xaaaa3a00, 0x42003f00),
|
|
"ps_f16_vop2b": (0xaaaa3e00, 0xaaaab800, 0xaaaa3c00, 0x42003400),
|
|
"ps_f16_vop1": (0xaaaa4000, 0xaaaabc00, 0xaaaac000, 0xaaaa8000),
|
|
"ps_f16_vop1b": (0xaaaa3400, 0xaaaa4000, 0xaaaab400, 0xaaaa3800),
|
|
"ps_f16_const": (0xaaaa3d00, 0x38005555, 0xaaaa3800, 0x3f000000),
|
|
"ps_f16_const2": (0x42003d00, 0x3d004200, 0xaaaabc00, 3),
|
|
"ps_f16_pack": (0x34003c00, 0x38003400, 0x34000000, 0x44004200)}
|
|
# Pixel shaders that read memory: vk_harness puts this V# in s[0:3] and seeds the heap it points at, as
|
|
# tests/semantic/test_value_semantics.py does for the interpreter.
|
|
PIXEL_ENV = {"ps_vcc_x2": {"VKH_SGPRS": "0x1000,0,16,0", "VKH_HEAP": "0x1000=0x11223344,0x1004=0x55667788"}}
|
|
CLIP_TESTS = ("vs_clip", "vs_clip_param4", "vs_clip_undeclared")
|
|
# One definition: a later assignment used to drop ps_texoff (and any entry added by an earlier assignment) from
|
|
# the standalone run. Keep every pixel fixture in this tuple.
|
|
PIXEL_TESTS = sorted((*PIXEL_BITS, "ps_bary", "ps_flat", "ps_bitmask_perm", "ps_texoff",
|
|
"ps_uniform_branch", "ps_wqm", "ps_scc_mask32"))
|
|
TEXOFF_ARGS = ("2", "92", "0", "0", "0") # ps_texoff: T# pointer in s2, then the shader's literal S#
|
|
TEXTURE_MIP_TEST = "ps_texture_mip0"
|
|
TEXTURE_MIP_DEPENDENCIES = ("vs_ids", "ps_texture_sample")
|
|
|
|
|
|
# Windows error codes for a program the system refuses to start, and who refused it.
|
|
BLOCKED_BY = {4551: "Windows Application Control (Smart App Control or an App Control policy)",
|
|
1260: "a Windows software restriction policy",
|
|
225: "Microsoft Defender, which reported it as a threat"}
|
|
|
|
|
|
def start_failure(exe, err):
|
|
"""One result line for a test program that could not be started, instead of a traceback that ends the run.
|
|
Smart App Control blocks unsigned programs it has no reputation for, so a fresh build can be refused."""
|
|
name = os.path.basename(str(exe))
|
|
who = BLOCKED_BY.get(getattr(err, "winerror", None))
|
|
if who is None:
|
|
return f"could not start {name}: {err}"
|
|
return (f"could not start {name}: blocked by {who}. A freshly built test program has no reputation yet: "
|
|
"allow it, or turn Smart App Control off (Windows Security > App & browser control), and run again")
|
|
|
|
|
|
def run(*cmd, env=None):
|
|
try:
|
|
r = subprocess.run(cmd, capture_output=True, text=True, env=env)
|
|
except OSError as e:
|
|
if not isinstance(e, FileNotFoundError):
|
|
return 1, start_failure(cmd[0], e)
|
|
tools = os.path.abspath(os.path.dirname(cmd[0]))
|
|
missing = [t + EXE for t in TEST_PROGRAMS if not os.path.isfile(os.path.join(tools, t + EXE))]
|
|
sys.exit(not_found_message(tools, missing))
|
|
return r.returncode, r.stdout + r.stderr
|
|
|
|
|
|
def failure(code, output, lines):
|
|
details = "\n".join(output.strip().splitlines()[-lines:])
|
|
return f"exit {code}: {details or 'no test output'}"
|
|
|
|
|
|
class Skip(str):
|
|
"""A result that is neither a pass nor a failure: the device can't run the test."""
|
|
|
|
|
|
def check_mesh_v5(data):
|
|
if len(data) != 24 * 16:
|
|
return f"expected 24 RGBA32F pixels (384 bytes), got {len(data)} bytes"
|
|
pixels = iter(struct.iter_unpack("<4f", data))
|
|
errors = []
|
|
for z in range(4):
|
|
for y in range(3):
|
|
for x in range(2):
|
|
pixel = next(pixels)
|
|
# v5 = linear group index * NumThreads + lane (#209); the shader records lanes 0, 1 and 31 of 32
|
|
g = x + 2*y + 6*z
|
|
want = (32 * g, 32 * g + 1, 32 * g + 31, 1)
|
|
if pixel != want:
|
|
errors.append(f"({x},{y},{z}): got {pixel}, expected {want}")
|
|
return f"{len(errors)}/24 mismatches; first {errors[0]}" if errors else ""
|
|
|
|
|
|
def mesh_v5_test(tools, spirv, raw):
|
|
shader = Path(spirv) / "ms_v5_workgroup.spv"
|
|
if not shader.is_file():
|
|
return Skip("SKIP (build the mesh fixture with scripts/run_gpu_tests.py)")
|
|
fs = Path(spirv) / "fs_passthru.spv"
|
|
if not fs.is_file():
|
|
return "missing fs_passthru.spv"
|
|
code, out = run(os.path.join(tools, "vk_harness" + EXE), shader, fs, raw, "--mesh-group-readback", "2", "3", "4")
|
|
if code == UNSUPPORTED:
|
|
return Skip("SKIP (mesh shading unavailable)")
|
|
if code or not raw.is_file():
|
|
return failure(code, out, 3)
|
|
return check_mesh_v5(raw.read_bytes())
|
|
|
|
|
|
def partial_wave_test(tools, spirv, name, expected_words):
|
|
"""A group smaller than a wave: run with subgroup size 64 requested (no full subgroups); None if the device
|
|
has no 64-wide subgroup. With LEONIDA_W64_EMULATE=1 it asks for 32 instead, the width the emulation is for, so
|
|
a device that defaults to 64 (AMD) runs the emulated path at 32 too (#166)."""
|
|
shader = os.path.join(spirv, name + ".spv")
|
|
if not os.path.isfile(shader):
|
|
return f"missing {name}.spv"
|
|
old = os.environ.get("VKC_SUBGROUP_SIZE")
|
|
os.environ["VKC_SUBGROUP_SIZE"] = "32" if W64_EMULATED else "64"
|
|
try:
|
|
code, out = run(os.path.join(tools, "vk_compute_test" + EXE), shader, "1", "dump")
|
|
finally:
|
|
if old is None:
|
|
os.environ.pop("VKC_SUBGROUP_SIZE", None)
|
|
else:
|
|
os.environ["VKC_SUBGROUP_SIZE"] = old
|
|
if code != 0:
|
|
return failure(code, out, 2)
|
|
if not W64_EMULATED and not any(int(x) >= 64 for x in re.findall(r"subgroup \d+\.\.(\d+)", out)):
|
|
return None
|
|
m = re.search(r"^out\[0\.\.7\] = ((?:[0-9]+ ){7}[0-9]+)$", out, re.MULTILINE)
|
|
if m and [int(x) for x in m.group(1).split()] == expected_words:
|
|
return ""
|
|
return failure(code, out, 2)
|
|
|
|
|
|
def narrow_subgroup_skip(out):
|
|
"""A Skip when vk_compute_test's "device: ... (subgroup MIN..MAX)" line says MAX is below 32 lanes, else None."""
|
|
m = re.search(r"subgroup \d+\.\.(\d+)", out)
|
|
if m and int(m.group(1)) < 32:
|
|
return Skip(f"SKIP (subgroup {m.group(1)} is narrower than 32 lanes)")
|
|
return None
|
|
|
|
|
|
def compute_test(tools, spirv, name, groups, mode, *, require_wave64=False, expected_words=None, needs_32_lanes=False,
|
|
env=None):
|
|
"""Return an empty string for a pass, None for unsupported wave64, a Skip for a wave32 lane-mask test on a device
|
|
narrower than 32 lanes (needs_32_lanes), or a failure diagnostic. `env` adds variables for vk_compute_test
|
|
(VKC_SGPRS)."""
|
|
shader = os.path.join(spirv, name + ".spv")
|
|
args = []
|
|
if require_wave64:
|
|
if not os.path.isfile(shader):
|
|
return f"missing {name}.spv"
|
|
args.append("--require-wave64")
|
|
extra = {"env": dict(os.environ, **env)} if env else {}
|
|
code, out = run(os.path.join(tools, "vk_compute_test" + EXE), shader, groups, mode, *args, **extra)
|
|
if require_wave64 and code == 77: # vk_compute_test: the device cannot run a 64-wide compute subgroup
|
|
return None
|
|
if needs_32_lanes and (skip := narrow_subgroup_skip(out)):
|
|
return skip
|
|
if code != 0:
|
|
return failure(code, out, 2)
|
|
if require_wave64 and "compute subgroup size: 64" not in out.splitlines():
|
|
return "64-wide subgroup was not confirmed (rebuild vk_compute_test)"
|
|
if mode == "workgroup_yz":
|
|
if "2x3x4 workgroup Y/Z: 0 mismatches" in out.splitlines():
|
|
return ""
|
|
elif expected_words is not None:
|
|
m = re.search(r"^out\[0\.\.7\] = ((?:[0-9]+ ){7}[0-9]+)$", out, re.MULTILINE)
|
|
if m and [int(x) for x in m.group(1).split()][:len(expected_words)] == expected_words:
|
|
return ""
|
|
else:
|
|
m = re.search(r"(\d+) mismatches", out)
|
|
if m and m.group(1) == "0":
|
|
return ""
|
|
return failure(code, out, 2)
|
|
|
|
|
|
def cvt_f32_sat_test(tools, spirv):
|
|
"""cs_cvt_f32_sat once per input of CVT_F32_SAT_CASES: an empty string when every pair of words is exact."""
|
|
wrong = []
|
|
for bits, unsigned, signed in CVT_F32_SAT_CASES:
|
|
problem = compute_test(tools, spirv, "cs_cvt_f32_sat", "1", "dump", expected_words=[unsigned, signed],
|
|
env={"VKC_SGPRS": f"{bits:#010x}"})
|
|
if problem:
|
|
wrong.append(f"{bits:#010x}: {problem}")
|
|
return f"{len(wrong)} of {len(CVT_F32_SAT_CASES)} inputs wrong, first {wrong[0]}" if wrong else ""
|
|
|
|
|
|
def mesh_w64x_test(tools, spirv, name):
|
|
"""ms_w64x_ballot: ms_tri's picture, blue 0.5 only if the ballot/mbcnt compaction was exact across both halves."""
|
|
shader, fs = os.path.join(spirv, name + ".spv"), os.path.join(spirv, "fs_passthru.spv")
|
|
if not (os.path.isfile(shader) and os.path.isfile(fs)):
|
|
return Skip("SKIP (mesh shader: build it with scripts/run_gpu_tests.py, build.py does not)")
|
|
env = dict(os.environ, VKH_MESH="1")
|
|
if not W64_EMULATED:
|
|
env["VKH_SUBGROUP_SIZE"] = "64"
|
|
with tempfile.TemporaryDirectory(prefix="leonida-mesh-") as tmp:
|
|
raw = Path(tmp) / "pixels.raw"
|
|
harness = os.path.join(tools, "vk_harness" + EXE)
|
|
try:
|
|
r = subprocess.run([harness, shader, fs, raw], capture_output=True, text=True, env=env)
|
|
except OSError as e:
|
|
return start_failure(harness, e)
|
|
if r.returncode == UNSUPPORTED: # vk_harness: the device has no VK_EXT_mesh_shader
|
|
m = re.search(r"unsupported: .*", r.stderr)
|
|
return Skip(f"SKIP ({m.group(0) if m else 'unsupported device'})")
|
|
if r.returncode or not raw.is_file():
|
|
return failure(r.returncode, r.stdout + r.stderr, 2)
|
|
data = raw.read_bytes()
|
|
if len(data) != 64 * 64 * 16:
|
|
return "vk_harness returned an unexpected pixel count"
|
|
px = list(struct.iter_unpack("<4f", data))
|
|
if not W64_EMULATED and 64 not in [int(x) for x in re.findall(r"subgroup \d+\.\.(\d+)", r.stderr)]:
|
|
return None # the device has no 64-wide subgroups
|
|
wrong = sum(abs(p[0] - ((i % 64) + .5) / 64) > 1e-3 or abs(p[1] - ((i // 64) + .5) / 64) > 1e-3
|
|
or abs(p[2] - .5) > 1e-3 or abs(p[3] - 1) > 1e-3 for i, p in enumerate(px))
|
|
return "" if not wrong else f"{wrong} of {len(px)} pixels differ (blue {px[0][2]:.3g} at pixel 0, want 0.5)"
|
|
|
|
|
|
def subgroup_size(spirv, name):
|
|
"""The wave size the translator recorded for a shader (index.json), or None."""
|
|
try:
|
|
with open(os.path.join(spirv, "index.json"), encoding="utf-8") as f:
|
|
return json.load(f).get(name, {}).get("subgroup_size")
|
|
except (OSError, ValueError):
|
|
return None
|
|
|
|
|
|
def bindless_test(tools, spirv):
|
|
"""ps_tex: a texture found through the descriptor hash table and the bindless set (vk_bindless_test)."""
|
|
shaders = [os.path.join(spirv, name + ".spv") for name in ("vs_ids", "ps_tex")]
|
|
for path in shaders:
|
|
if not os.path.isfile(path):
|
|
return f"missing {os.path.basename(path)}"
|
|
code, out = run(os.path.join(tools, "vk_bindless_test" + EXE), *shaders)
|
|
if code == UNSUPPORTED:
|
|
m = re.search(r"unsupported: .*", out)
|
|
return Skip(f"SKIP ({m.group(0) if m else 'unsupported device'})")
|
|
m = re.search(r"(\d+) draws read back, (\d+) bad pixels", out)
|
|
return "" if code == 0 and m and m.group(2) == "0" else failure(code, out, 3)
|
|
|
|
|
|
def texture_gradient_a16_test(tools: str, spirv: str) -> str:
|
|
"""Check every pixel of the mixed f32-gradient/a16-coordinate sample."""
|
|
vs = os.path.join(spirv, "vs_ids.spv")
|
|
fs = os.path.join(spirv, "ps_texture_grad_a16.spv")
|
|
with tempfile.TemporaryDirectory(prefix="leonida-texture-") as tmp:
|
|
raw = Path(tmp) / "pixels.raw"
|
|
code, out = run(os.path.join(tools, "vk_texture_test" + EXE), vs, fs, raw,
|
|
"0", "92", "c00000", "14500000", "0")
|
|
if code or not raw.is_file():
|
|
return failure(code, out, 3)
|
|
data = raw.read_bytes()
|
|
if len(data) != 64 * 64 * 16:
|
|
return "vk_texture_test returned an unexpected pixel count"
|
|
wrong = 0
|
|
first = ""
|
|
for index, pixel in enumerate(struct.iter_unpack("<4f", data)):
|
|
x, y = index % 64, index // 64
|
|
want = (32 * (x // 8) / 255, 32 * (y // 8) / 255, 128 / 255, 1.0)
|
|
if any(not math.isclose(actual, expected, rel_tol=0, abs_tol=1e-6)
|
|
for actual, expected in zip(pixel, want)):
|
|
if not wrong:
|
|
first = f"at ({x},{y}): got {pixel}, expected {want}"
|
|
wrong += 1
|
|
return f"{wrong} of 4096 pixels differ; {first}" if wrong else ""
|
|
|
|
|
|
def pixels(tools, spirv, name, fmt):
|
|
# Draw at the shader's own wave size, as run_gpu_tests.py does (#146): a wave32 pixel shader run as wave64
|
|
# (RADV's default on RDNA1) reads lanes it was never written for.
|
|
sg = subgroup_size(spirv, name)
|
|
env = dict(os.environ, VKH_SUBGROUP_SIZE=str(sg)) if sg else None
|
|
if name in PIXEL_ENV:
|
|
env = dict(env or os.environ, **PIXEL_ENV[name])
|
|
with tempfile.TemporaryDirectory(prefix="leonida-pixels-") as tmp:
|
|
raw = Path(tmp) / "pixels.raw"
|
|
code, out = run(os.path.join(tools, "vk_harness" + EXE), os.path.join(spirv, "vs_ids.spv"),
|
|
os.path.join(spirv, name + ".spv"), raw, env=env)
|
|
if code == UNSUPPORTED: # vk_harness: the shader needs a device feature this GPU lacks
|
|
return Skip(""), out
|
|
if code or not raw.is_file():
|
|
return None, out
|
|
data = raw.read_bytes()
|
|
if len(data) != 64 * 64 * 16: # default harness viewport, four 32-bit components per pixel
|
|
return None, out
|
|
return list(struct.iter_unpack(fmt, data)), out
|
|
|
|
|
|
def texoff_words(px, py):
|
|
"""ps_texoff's mrt0 words at pixel (px, py), from the rule in docs/design.md (derivation in ps_texoff.asm)."""
|
|
bx, by = px >> 3, py >> 3 # per-pixel offset (bx - 4, by - 4) from texel (4, 4)
|
|
x0, y0 = max(bx - 1, 0), max(by - 1, 0) # gather footprint 3..4 moved by the offset, clamped at 0
|
|
return (bx | by << 4, 0x0761, x0 | bx << 4 | bx << 8 | x0 << 12, by | by << 4 | y0 << 8 | y0 << 12)
|
|
|
|
|
|
def texoff_problem(data, output):
|
|
"""Judge ps_texoff's 64x64 RGBA32 dump: (failure text or "", what was covered). The +16 / -32 sample (bits 8-15
|
|
of .y) is only defined, and so only judged, where vk_texture_test printed texel offset limits that cover it."""
|
|
m = re.search(r"texel offset limits: (-?\d+)\.\.(-?\d+)", output)
|
|
wide = bool(m) and int(m.group(1)) <= -32 and int(m.group(2)) >= 16
|
|
if len(data) != 64 * 64 * 16:
|
|
return "vk_texture_test returned an unexpected pixel count", ""
|
|
keep = (0xFFFFFFFF, 0xFFFFFFFF if wide else 0xFFFF00FF, 0xFFFFFFFF, 0xFFFFFFFF)
|
|
px = list(struct.iter_unpack("<4I", data))
|
|
bad = [(i, got) for i, got in enumerate(px)
|
|
if any((g ^ w) & k for g, w, k in zip(got, texoff_words(i % 64, i // 64), keep))]
|
|
limits = f"{m.group(1)}..{m.group(2)}" if m else "unknown"
|
|
covered = "offsets +16/-32 judged" if wide else f"offsets +16/-32 not judged (device texel offset limits {limits})"
|
|
if not bad:
|
|
return "", covered
|
|
i, got = bad[0]
|
|
return (f"{len(bad)} of {len(px)} pixels differ; first at ({i % 64},{i // 64}): got {[hex(x) for x in got]}, "
|
|
f"want {[hex(x) for x in texoff_words(i % 64, i // 64)]} ({covered})"), covered
|
|
|
|
|
|
def texoff_test(tools, spirv, name):
|
|
"""ps_texoff on vk_texture_test (it binds the texture, sampler and descriptor table); vs_ids supplies attr0."""
|
|
with tempfile.TemporaryDirectory(prefix="leonida-pixels-") as tmp:
|
|
raw = Path(tmp) / "pixels.raw"
|
|
code, out = run(os.path.join(tools, "vk_texture_test" + EXE), os.path.join(spirv, "vs_ids.spv"),
|
|
os.path.join(spirv, name + ".spv"), raw, *TEXOFF_ARGS,
|
|
env=dict(os.environ, VKT_NEED_MAINTENANCE8="1"))
|
|
if code == UNSUPPORTED: # vk_texture_test: no VK_KHR_maintenance8 (a non-constant Offset outside gather)
|
|
m = re.search(r"unsupported: .*", out)
|
|
return Skip(f"SKIP ({m.group(0) if m else 'unsupported device'})")
|
|
if code or not raw.is_file():
|
|
return failure(code, out, 2)
|
|
return texoff_problem(raw.read_bytes(), out)[0]
|
|
|
|
|
|
def texture_mip_mismatch(data: bytes, mip: int, *, transpose_uv: bool = False) -> str:
|
|
width = 64
|
|
if len(data) != width**2 * 16:
|
|
return f"expected {width**2} RGBA32F pixels, got {len(data)} bytes"
|
|
one = 0x3f800000
|
|
quadrants = ((one, 0, 0, one), (0, one, 0, one),
|
|
(0, 0, one, one), (one, one, 0, 0))
|
|
for i, actual in enumerate(struct.iter_unpack("<4I", data)):
|
|
y, x = divmod(i, width)
|
|
# The normalized UV at each pixel center lies within the same quadrant of mip 0.
|
|
u, v = (y, x) if transpose_uv else (x, y)
|
|
expected = (0, one, one, one) if mip == 1 else quadrants[2 * (v >= width // 2) + (u >= width // 2)]
|
|
if actual != expected:
|
|
return f"({x},{y}): RGBA32F bits {actual} != {expected}"
|
|
return ""
|
|
|
|
|
|
def texture_mip_test(tools: str, spirv: str) -> str:
|
|
tool = Path(tools) / ("vk_texture_test" + EXE)
|
|
if not tool.is_file():
|
|
return f"missing {tool}"
|
|
shaders = Path(spirv)
|
|
vertex = shaders / "vs_ids.spv"
|
|
with tempfile.TemporaryDirectory(prefix="leonida-mip-") as tmp:
|
|
for mip, name in enumerate((TEXTURE_MIP_TEST, "ps_texture_sample")):
|
|
fragment = shaders / (name + ".spv")
|
|
for shader in (vertex, fragment):
|
|
if not shader.is_file():
|
|
return f"missing {shader}"
|
|
raw = Path(tmp) / f"mip{mip}.raw"
|
|
code, output = run(tool, vertex, fragment, raw, "0", "92", "c00000", "14500000", "0", "--two-mip")
|
|
if code == UNSUPPORTED:
|
|
return Skip(f"SKIP (texture mip test: {output.strip() or 'unsupported device'})")
|
|
if code or not raw.is_file():
|
|
return f"{name}: {failure(code, output, 4)}"
|
|
data = raw.read_bytes()
|
|
mismatch = texture_mip_mismatch(data, mip)
|
|
if mismatch:
|
|
return f"{name}: {mismatch}"
|
|
if not texture_mip_mismatch(data, 1 - mip).startswith("(0,0):"):
|
|
return f"{name}: wrong mip control missed the expected first pixel"
|
|
if mip == 0 and not texture_mip_mismatch(data, mip, transpose_uv=True).startswith("(32,0):"):
|
|
return f"{name}: transposed UV control missed the expected first pixel"
|
|
return ""
|
|
|
|
|
|
def pixel_test(tools, spirv, name):
|
|
path = os.path.join(spirv, name + ".spv")
|
|
if not os.path.isfile(path):
|
|
return f"missing {name}.spv"
|
|
if name == "ps_texoff":
|
|
return texoff_test(tools, spirv, name)
|
|
px, out = pixels(tools, spirv, name, "<4f" if name == "ps_bary" else "<2I2f" if name in ("ps_flat", "ps_uniform_branch", "ps_scc_mask32") else "<4I")
|
|
if isinstance(px, Skip): # exit 77: the device cannot run this shader (e.g. no barycentrics)
|
|
m = re.search(r"unsupported: .*", out)
|
|
return Skip(f"SKIP ({m.group(0) if m else 'unsupported device'})")
|
|
if name == "ps_bitmask_perm":
|
|
# Decide from the reported width before the pixel count: a narrow device's frame can be short, and with no
|
|
# width line the lanes 0-31 this shader writes are not known to exist.
|
|
m = re.search(r"subgroup \d+\.\.(\d+)", out)
|
|
if not m:
|
|
return Skip("SKIP (vk_harness reported no subgroup width)")
|
|
if int(m.group(1)) < 32:
|
|
return Skip(f"SKIP (subgroup {m.group(1)} is narrower than 32 lanes)")
|
|
if not px:
|
|
return out if out.startswith("could not start") else "vk_harness failed or returned no pixels"
|
|
n = len(px)
|
|
if name in PIXEL_BITS:
|
|
wrong = sum(p != PIXEL_BITS[name] for p in px)
|
|
return "" if not wrong else f"{wrong} of {n} pixels differ, e.g. {[hex(x) for x in next(p for p in px if p != PIXEL_BITS[name])]}"
|
|
if name == "ps_bary": # raw i, j against the interpolated attr0.xy / 2
|
|
if any(not math.isfinite(v) for p in px for v in p):
|
|
return "barycentrics contain non-finite values"
|
|
err = max(max(abs(a / 2 - i), abs(b / 2 - j)) for a, b, i, j in px)
|
|
return "" if err < 1e-6 else f"barycentrics off by {err:.3g}"
|
|
if name == "ps_flat":
|
|
if any(not math.isfinite(v) for p in px for v in p[2:]):
|
|
return "UVs contain non-finite values"
|
|
w = int(n ** 0.5)
|
|
wrong = sum((a, b) != (0x101, 0x102) for a, b, _, _ in px)
|
|
err = max(max(abs(u - (i % w + 0.5) / w), abs(v - (i // w + 0.5) / w)) for i, (_, _, u, v) in enumerate(px))
|
|
return "" if not wrong and err < 1e-6 else f"{wrong} pixels without the first vertex's value, uv off by {err:.3g}"
|
|
if name == "ps_bitmask_perm":
|
|
low = [p for p in px if p[0] < 32]
|
|
wrong = sum(not (a == lane ^ 9 and b == ((lane & 5) | 16) ^ 1) for lane, a, b, _ in low)
|
|
return "" if low and not wrong else f"{wrong} of {len(low)} pixels in lanes 0-31 wrong"
|
|
if name == "ps_wqm": # (wqm, own, wqm, own) per pixel, ps_wqm.asm
|
|
w = int(n ** 0.5)
|
|
wrong = sum(p != (int(i % w % 4 < 2 and i // w % 4 >= 2), int(i % w % 4 == 1 and i // w % 4 == 2)) * 2 for i, p in
|
|
enumerate(px))
|
|
return "" if not wrong else f"{wrong} of {n} pixels differ from the whole-quad expectation"
|
|
if name in ("ps_uniform_branch", "ps_scc_mask32"): # (s5, own VCC bit): 8 wherever the bit is set, never 7 there
|
|
c = collections.Counter((a, b) for a, b, _, _ in px)
|
|
fine = c[(8, 1)] and c[(8, 0)] and c[(7, 0)] and set(c) <= {(8, 1), (8, 0), (7, 0)}
|
|
return "" if fine else f"(s5, bit) counts {dict(c)}"
|
|
return "no check written for this shader"
|
|
|
|
|
|
def clip_test(tools, spirv, name):
|
|
with tempfile.TemporaryDirectory(prefix="leonida-clip-") as tmp:
|
|
raw = Path(tmp) / "pixels.raw"
|
|
for clip_x, clip_y in ((False, False), (True, False), (False, True), (True, True)):
|
|
code, output = run(os.path.join(tools, "vk_harness" + EXE), os.path.join(spirv, name + ".spv"),
|
|
os.path.join(spirv, "ps_ops.spv"), raw, "64", "64",
|
|
"0" if clip_x else "4", "0" if clip_y else "4")
|
|
if code == UNSUPPORTED:
|
|
return Skip("SKIP (shaderClipDistance unavailable)")
|
|
if code:
|
|
return failure(code, output, 2)
|
|
if not raw.is_file():
|
|
return "missing framebuffer"
|
|
data = raw.read_bytes()
|
|
raw.unlink()
|
|
if len(data) != 64 * 64 * 16:
|
|
return "incomplete framebuffer"
|
|
clear = (0xbf800000,) * 4
|
|
wrong = 0
|
|
for i, pixel in enumerate(struct.iter_unpack("<4I", data)):
|
|
inside = (not clip_x or i % 64 < 32) and (not clip_y or i // 64 < 16)
|
|
drawn = inside or name == "vs_clip_undeclared"
|
|
wrong += pixel != (PIXEL_BITS["ps_ops"] if drawn else clear)
|
|
if wrong:
|
|
return f"x={clip_x} y={clip_y}: {wrong} of 4096 pixels differ"
|
|
return ""
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) < 2:
|
|
sys.exit(__doc__)
|
|
spirv = os.path.join(sys.argv[1], "spirv")
|
|
tools = sys.argv[2] if len(sys.argv) > 2 else os.path.join(HERE, "..", "..", "build")
|
|
tools = pick_build_dir(os.path.abspath(tools), tuple(t + EXE for t in TEST_PROGRAMS))
|
|
results = []
|
|
for name, groups, expect in COMPUTE:
|
|
results.append((name, compute_test(tools, spirv, name, groups, expect)))
|
|
results.append(("cs_wavemask", compute_test(tools, spirv, "cs_wavemask", "1", "wavemask",
|
|
require_wave64=not W64_EMULATED)))
|
|
for name, want in CS_DUMPS: # all but cs_store need a 64-wide subgroup unless wave64 is emulated
|
|
results.append((name, compute_test(tools, spirv, name, "1", "dump",
|
|
require_wave64=name not in WAVE32_DUMPS and not W64_EMULATED,
|
|
expected_words=want, needs_32_lanes=name in WAVE32_LANE_DUMPS)))
|
|
for name, want in BOUNDS:
|
|
results.append((name, compute_test(tools, spirv, name, "1", "dump", expected_words=want)))
|
|
results.append(("cs_cmpx_class_f32", compute_test(tools, spirv, "cs_cmpx_class_f32", "1", "dump",
|
|
expected_words=CMPX_CLASS_WORDS)))
|
|
for name, groups, want in GLOBAL_OFFSET:
|
|
results.append((name, compute_test(tools, spirv, name, groups, "dump", expected_words=want)))
|
|
results.append(("cs_user_sgprs", compute_test(tools, spirv, "cs_user_sgprs", "1", "dump",
|
|
expected_words=USER_SGPRS_WORDS, env=USER_SGPRS_ENV)))
|
|
results.append(("cs_cvt_f32_sat", cvt_f32_sat_test(tools, spirv)))
|
|
for name, want in W64X_DUMPS: # without --require-wave64 when LEONIDA_W64_EMULATE=1
|
|
results.append((name, compute_test(tools, spirv, name, "1", "dump", require_wave64=not W64_EMULATED,
|
|
expected_words=want)))
|
|
for name, want in W64X_PARTIAL:
|
|
results.append((name, partial_wave_test(tools, spirv, name, want)))
|
|
results.append(("ms_w64x_ballot", mesh_w64x_test(tools, spirv, "ms_w64x_ballot")))
|
|
results.append(("ms_wave_info_s3", mesh_w64x_test(tools, spirv, "ms_wave_info_s3")))
|
|
with tempfile.TemporaryDirectory(prefix="leonida-mesh-") as tmp:
|
|
results.append(("ms_v5_workgroup", mesh_v5_test(tools, spirv, Path(tmp) / "groups.raw")))
|
|
for name, args in (("cs_imgstore", []), ("cs_imgload", ["load"])):
|
|
code, out = run(os.path.join(tools, "vk_store_test" + EXE), os.path.join(spirv, name + ".spv"), *args)
|
|
if code == UNSUPPORTED: # vk_store_test: the device lacks a feature this test needs
|
|
m = re.search(r"unsupported: .*", out)
|
|
results.append((name, Skip(f"SKIP ({m.group(0) if m else 'unsupported device'})")))
|
|
continue
|
|
bad = re.findall(r"(\d+) bad", out)
|
|
results.append((name, "" if code == 0 and len(bad) == 3 and set(bad) == {"0"} else failure(code, out, 3)))
|
|
for name in CLIP_TESTS:
|
|
results.append((name, clip_test(tools, spirv, name)))
|
|
for name in PIXEL_TESTS:
|
|
results.append((name, pixel_test(tools, spirv, name)))
|
|
results.append(("ps_tex", bindless_test(tools, spirv)))
|
|
results.append((TEXTURE_MIP_TEST, texture_mip_test(tools, spirv)))
|
|
results.append(("ps_texture_grad_a16", texture_gradient_a16_test(tools, spirv)))
|
|
for name, problem in results:
|
|
if problem is None:
|
|
problem = "SKIP (full 64-wide compute subgroups unavailable)"
|
|
print(f"{name:20} {'ok' if not problem else problem}")
|
|
skipped = sum(p is None or isinstance(p, Skip) for _, p in results)
|
|
failed = sum(bool(p) for _, p in results) - sum(isinstance(p, Skip) for _, p in results)
|
|
print(f"{len(results) - failed - skipped} of {len(results)} pass, {skipped} skipped")
|
|
return 1 if failed else 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|