r"""SHADER-539: measure on an AMD GPU (tools/hw_oracle) which mip level image_sample_l and image_sample_lz read under the S#'s MIN_LOD / MAX_LOD / LOD_BIAS / MIP_FILTER and the T#'s MIN_LOD and BASE_LEVEL. python -I tools/sampler_lod_probe.py # each row's level under both rules below (no GPU) python -I tools/sampler_lod_probe.py --oracle hw # native AMD GPU, wave32 and wave64, compared with the rule docs/d3d12_vulkan.md ("vk_sampler_test") says the S# LOD fields apply to an explicit LOD as Vulkan's minLod, maxLod and mipLodBias do, taken from radv and not measured on RDNA2, and that the T#'s MIN_LOD (ResourceMinLODClamp) is untested. Here a raw kernel samples a linear 64x64 R32_SFLOAT texture with seven levels, through a point min/mag filter, at the top-left texel. The texture memory is a ramp, every dword holding its own index, so each sample returns the dword offset of the texel it read: the start of the level it came from. The level starts are not assumed; a calibration row per level (image_sample_l at LOD n, no clamp) finds them, and every other row's result is named by that table. Two rules are computed for every row: - rdna2_level, the rule the hardware rows are checked against: the model the gfx1036 measurement fits (see its docstring; an exact half level rounds up, and the T#'s MIN_LOD clamps the fractional LOD before that rounding); - vulkan_level, for comparison: Vulkan's rule for the sampler and view the runtime makes of the same S# and T# (runtime/src/gfx10_descriptors.h): nearest is ceil(lambda + 0.5) - 1, and VK_EXT_image_view_min_lod raises the integer level to floor(minLod). Rows where it differs are marked "Vulkan reads n"; that is not a failure. DIFFERS means the hardware disagrees with the RDNA2 model, not with Vulkan. Standard library only; the texture is built here, no game files. Exit codes: 0 every row matches the RDNA2 model (not "the hardware equals Vulkan"), 1 a row differs, 2 harness error, 77 no hardware oracle. Usage: `python -I tools/sampler_lod_probe.py --help` lists the options. """ import argparse import math import os import struct import sys HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.join(HERE, "hw_oracle")) W = 64 LEVELS = 7 # 64x64 .. 1x1 RAMP_DWORDS = 16384 # more than the chain needs under any 256-byte level alignment CLAMP_LAST_TEXEL = 2 U = V = 0.25 / W # inside texel (0, 0) at every level # (name, instruction, LOD, S# (mip_filter, min_lod, max_lod, bias), T# (base_level, min_lod)) def rows(): plain = (1, 0.0, 15.0, 0.0) out = [(f"calib_lod{n}", "l", float(n), plain, (0, 0.0)) for n in range(LEVELS)] out += [ ("lz", "lz", 0.0, plain, (0, 0.0)), # S# MIN_LOD / MAX_LOD on an explicit LOD and on _lz ("s_min2_l0", "l", 0.0, (1, 2.0, 15.0, 0.0), (0, 0.0)), ("s_min2_lz", "lz", 0.0, (1, 2.0, 15.0, 0.0), (0, 0.0)), ("s_max1_l4", "l", 4.0, (1, 0.0, 1.0, 0.0), (0, 0.0)), ("s_max0_l3", "l", 3.0, (1, 0.0, 0.0, 0.0), (0, 0.0)), # S# LOD_BIAS on an explicit LOD and on _lz ("s_bias+2_l1", "l", 1.0, (1, 0.0, 15.0, 2.0), (0, 0.0)), ("s_bias+2_lz", "lz", 0.0, (1, 0.0, 15.0, 2.0), (0, 0.0)), ("s_bias-1_l3", "l", 3.0, (1, 0.0, 15.0, -1.0), (0, 0.0)), ("s_bias+2_max3_l3", "l", 3.0, (1, 0.0, 3.0, 2.0), (0, 0.0)), # bias before the clamp ("s_bias+0.5_l1", "l", 1.0, (1, 0.0, 15.0, 0.5), (0, 0.0)), # 1.5: the nearest-level rounding # point mip filter rounding of a fractional LOD ("l1.25", "l", 1.25, plain, (0, 0.0)), ("l1.5", "l", 1.5, plain, (0, 0.0)), ("l1.75", "l", 1.75, plain, (0, 0.0)), # MIP_FILTER none: the base level whatever the LOD and clamp ("mipnone_l3", "l", 3.0, (0, 0.0, 15.0, 0.0), (0, 0.0)), ("mipnone_min2_l0", "l", 0.0, (0, 2.0, 15.0, 0.0), (0, 0.0)), ("mipnone_t_min3_l0", "l", 0.0, (0, 0.0, 15.0, 0.0), (0, 3.0)), # the T#'s MIN_LOD still applies ("mipnone_t_min2.25", "l", 0.0, (0, 0.0, 15.0, 0.0), (0, 2.25)), ("mipnone_t_min2.5", "l", 0.0, (0, 0.0, 15.0, 0.0), (0, 2.5)), ("mipnone_base1_tmin3", "l", 0.0, (0, 0.0, 15.0, 0.0), (1, 3.0)), # T# MIN_LOD (ResourceMinLODClamp) ("t_min3_l1", "l", 1.0, plain, (0, 3.0)), ("t_min3_lz", "lz", 0.0, plain, (0, 3.0)), ("t_min3_l5", "l", 5.0, plain, (0, 3.0)), # above the clamp: unchanged ("t_min2.5_l0", "l", 0.0, plain, (0, 2.5)), # fractional clamp, point mip ("t_min2.5_l2.6", "l", 2.6, plain, (0, 2.5)), ("t_min2.25_l0", "l", 0.0, plain, (0, 2.25)), # ceil(MIN_LOD), or clamp then round? ("t_min2.75_l0", "l", 0.0, plain, (0, 2.75)), ("t_min2.25_lz", "lz", 0.0, plain, (0, 2.25)), # more half-way points: is an exact .5 rounded up wherever it comes from? ("l0.5", "l", 0.5, plain, (0, 0.0)), ("l2.5", "l", 2.5, plain, (0, 0.0)), ("s_min1.5_l0", "l", 0.0, (1, 1.5, 15.0, 0.0), (0, 0.0)), ("s_max1.5_l4", "l", 4.0, (1, 0.0, 1.5, 0.0), (0, 0.0)), ("l1.49", "l", 1.49, plain, (0, 0.0)), # BASE_LEVEL: S# fields count from the view's base, the T# MIN_LOD from the resource's level 0 ("base1_l0", "l", 0.0, plain, (1, 0.0)), ("base1_s_min1_l0", "l", 0.0, (1, 1.0, 15.0, 0.0), (1, 0.0)), ("base1_t_min2_l0", "l", 0.0, plain, (1, 2.0)), ("base1_t_min2_lz", "lz", 0.0, plain, (1, 2.0)), ] return out def vulkan_level(instr, lod, s, t): """Vulkan's level for the sampler and view the runtime makes of this S# and T#: nearest is ceil(lambda + 0.5) - 1, and VK_EXT_image_view_min_lod raises the integer level to floor(minLod).""" mip_filter, s_min, s_max, bias = s base, t_min = t last = LEVELS - 1 if mip_filter == 0: level = base else: lam = (0.0 if instr == "lz" else lod) + bias lam = min(max(lam, s_min), s_max) d = max(0, math.ceil(lam + 0.5) - 1) level = min(base + d, last) return min(max(level, math.floor(t_min)), last) def rdna2_level(instr, lod, s, t): """The model the gfx1036 measurement fits: lambda = LOD (0 for _lz) + LOD_BIAS, clamped to the S#'s [MIN_LOD, MAX_LOD]; with a point mip filter the T#'s MIN_LOD raises the still fractional LOD (counted from the resource's level 0) and the level is floor(lambda + 0.5): an exact .5 goes up. MIP_FILTER none ignores the S#'s LOD fields and reads the view's base level, raised to floor(T# MIN_LOD).""" mip_filter, s_min, s_max, bias = s base, t_min = t last = LEVELS - 1 if mip_filter == 0: return min(max(base, math.floor(t_min)), last) lam = (0.0 if instr == "lz" else lod) + bias lam = base + min(max(lam, s_min), s_max) lam = max(lam, t_min) return min(max(math.floor(lam + 0.5), base), last) rule_level = rdna2_level def fixed(value, frac_bits, total_bits, signed=False): v = round(value * (1 << frac_bits)) return v & ((1 << total_bits) - 1) if signed else max(0, min(v, (1 << total_bits) - 1)) def sampler_words(s): """S# positions as check.py's ssharp_words (Mesa gfx10-rsrc.json): WORD0 CLAMP_X/Y/Z [8:0], WORD1 MIN_LOD [11:0] and MAX_LOD [23:12] (4.8), WORD2 LOD_BIAS [13:0] (signed 5.8) and MIP_FILTER [27:26]. Point min/mag (0).""" mip_filter, s_min, s_max, bias = s clamp = CLAMP_LAST_TEXEL | CLAMP_LAST_TEXEL << 3 | CLAMP_LAST_TEXEL << 6 return [clamp, fixed(s_min, 8, 12) | fixed(s_max, 8, 12) << 12, fixed(bias, 8, 14, True) | mip_filter << 26, 0] def image_words(arch, t): """A linear 2D R32_SFLOAT T# with LEVELS levels. Mip fields as runtime/src/gfx10_descriptors.h has them: WORD1 MIN_LOD [19:8] (4.8), WORD3 BASE_LEVEL [15:12] and LAST_LEVEL [19:16], WORD5 MAX_MIP [7:4].""" import tsharp if tsharp.check_arch(arch) != "gfx10": raise ValueError(f"the mip fields here are GFX10's; {arch} is not GFX10") base, t_min = t w = tsharp.image_words(arch, "R32_SFLOAT", W, W, 4) w[1] |= fixed(t_min, 8, 12) << 8 w[3] |= base << 12 | (LEVELS - 1) << 16 w[5] |= (LEVELS - 1) << 4 return w def f32(x): return struct.unpack("2}{extra:12} rule {want} {'ok' if ok else 'DIFFERS'}{note}") print(f"{len(table) - bad}/{len(table)} rows match the rule; Vulkan's level differs on {vk_diff}") return 1 if bad else 0 except runner.Unavailable as e: print(f"hardware oracle unavailable: {e}") return 77 except RuntimeError as e: print(f"harness error: {e}") return 2 finally: oracle.close() if __name__ == "__main__": try: # a redirected stdout is strict ANSI code page under -I; write '?' for what it cannot encode sys.stdout.reconfigure(errors="replace") except (AttributeError, ValueError, OSError): pass sys.exit(main())