Repository navigation
Recompiler: gather4 with an explicit LOD reads the mip it names - #1812
brothernog wants to merge 3 commits into
Conversation
|
Checked on Ampere: RTX 3070 8 GB, driver 616.56, Windows 11, debug, Merge with Results. A gap: the sampler's LOD clamp. The mip is chosen from the raw LOD, but the four
"Console level" follows a hardware measurement on RDNA2 (gfx1036) in boykopovar/AnyPS5#23856. For On rounding versus truncation: the same measurement found the level changes at exactly Test (pixel shader, 3 cases)fn review_gather4_l_case(lod: u64, w1: u32, w2: u32, level: u32) -> (usize, Option<(usize, usize, [f32; 4], [f32; 4])>) {
use windows::Win32::Graphics::Direct3D12::*;
use windows::Win32::Graphics::Dxgi::Common::{DXGI_FORMAT_R32G32B32A32_FLOAT, DXGI_SAMPLE_DESC};
let inv = (1.0f32 / 16.0).to_bits();
let ps = prog(&[
("V_MUL_F32", "ENC_VOP2", &[("VDST", 4), ("SRC0", 255), ("VSRC1", 2)], Some(inv)),
("V_MUL_F32", "ENC_VOP2", &[("VDST", 5), ("SRC0", 255), ("VSRC1", 3)], Some(inv)),
("V_MOV_B32", "ENC_VOP1", &[("VDST", 6), ("SRC0", lod)], None),
("IMAGE_GATHER4_L", "ENC_MIMG", &[("VDATA", 8), ("VADDR", 4), ("SRSRC", 4), ("SSAMP", 6), ("DMASK", 0x1), ("DIM", 1)], None),
("EXP", "ENC_EXP", &[("TGT", 0), ("EN", 0xF), ("VSRC0", 8), ("VSRC1", 9), ("VSRC2", 10), ("VSRC3", 11), ("DONE", 1), ("VM", 1)], None),
]);
let mut data = Vec::new();
for k in 0..4u32 {
let n = 8 >> k;
for _y in 0..n {
for x in 0..n {
data.extend_from_slice(&[(100 * k + x) as f32; 4].map(f32::to_le_bytes).concat());
}
}
}
let res = D3D12_RESOURCE_DESC {
Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D,
Width: 8,
Height: 8,
DepthOrArraySize: 1,
MipLevels: 4,
Format: DXGI_FORMAT_R32G32B32A32_FLOAT,
SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 },
..Default::default()
};
let mut srv = D3D12_SHADER_RESOURCE_VIEW_DESC {
Format: DXGI_FORMAT_R32G32B32A32_FLOAT,
ViewDimension: D3D12_SRV_DIMENSION_TEXTURE2D,
Shader4ComponentMapping: D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING,
..Default::default()
};
srv.Anonymous.Texture2D = D3D12_TEX2D_SRV { MipLevels: 4, ..Default::default() };
let tex = runner::ViewTexture { res, srv, data, row_bytes: 8 * 16 };
let s_sharp = [2 | 2 << 3 | 2 << 6, w1, w2, 0];
let mut sgprs = vec![0u32; 32];
sgprs[16..24].copy_from_slice(&tsharp(0, 77, 9));
sgprs[24..28].copy_from_slice(&s_sharp);
let px = draw_console_views(None, Some(&ps), &[[0.0, 0.0], [0.0, 32.0], [32.0, 0.0]], &sgprs, &[tex], &[crate::gpu::desc::sampler_d3d12(&s_sharp, false)]);
let n = (8 >> level) as i32;
let cl = |c: i32| c.clamp(0, n - 1);
let mut wrong = Vec::new();
for (i, p) in px.iter().enumerate() {
if p[0] < 0.0 {
continue; // not covered
}
let (x, y) = (i % 16, i / 16);
let u = (x as f32 + 0.5) / 16.0;
let left = (u * n as f32 - 0.5).floor() as i32;
let _ = y;
let t = |c: i32| (100 * level as i32 + cl(c)) as f32;
let want = [t(left), t(left + 1), t(left + 1), t(left)];
if *p != want {
wrong.push((x, y, *p, want));
}
}
(wrong.len(), wrong.first().copied())
}
#[test]
fn review_gather4_l_reads_the_level_it_names() {
if !gpu_ready() {
return;
}
let a = review_gather4_l_case(242, 0xFFF << 12, 1 << 26, 1); // lod 1.0, full range, point mip
let b = review_gather4_l_case(244, 0x100 << 12, 1 << 26, 1); // lod 2.0, MAX_LOD 1.0
let c = review_gather4_l_case(242, 0xFFF << 12, 0, 0); // lod 1.0, MIP_FILTER none
assert_eq!((a.0, b.0, c.0), (0, 0, 0), "{a:?} {b:?} {c:?}");
}AI-assisted (Claude Code). Synthetic tests only. |
|
Full-suite run, since this PR has no test report yet. Tested exact head Tested
Not tested
AI use
|
f0d69fe to
7f804be
Compare
|
@upipibi thanks, both points were real, and the measurement pointer settled the rounding question I had left open. I rebased onto
@futurepro62 thanks for the full-suite run on the 5070 Ti. It was on Not covered by a test: a static (constant) S#. The words come from the code there, through the same expressions. This comment was AI-assisted. |
|
Re-tested On the remaining AI-assisted (Claude Code). Synthetic tests only. |
|
Follow-up from reading
I can't say how many programs in the retail pack use these, and I haven't measured any of them on a GPU, so I'm not suggesting they block this PR. They might be worth a line in the description, or a |
|
Verification on RTX 3060 Ti (Windows 11): merge conflict with current |
|
To move this forward before merge:
|
7f804be to
51361a6
Compare
|
@upipibi thanks for the retest, and for the T# rule. It's in as
Two things your summary doesn't settle, so I wrote them down as assumptions:
@DavidM1911 thanks, the conflict was real: #1969 moved the direct DXIL sample path into a guarded closure. The branch is rebased onto @futurepro62 agreed on all three. They are outside this PR, and I've listed them in the description as known gaps:
I'd rather not refuse them here. They translate today and read mip 0, and a refusal would turn every program that uses them into a translation failure. That trade belongs in its own PR, with pack counts. #1951 covers the compare forms in the direct DXIL backend. This comment was AI-assisted. |
|
CI Next step before merge: please rebase onto |
|
Tested Clean PR head I prebuilt with & $testExe gpu::recomp::tests::gpu_gather4_explicit_lod --exact --nocapture --test-threads=1
& $testExe gpu::recomp::tests::gpu_ps_image_gather4_l_s_sharp_level --exact --nocapture --test-threads=12 passed, 0 failed, 0 ignored, 0 skipped. Each invocation filtered out 981 tests.
Not tested Direct DXIL pixel shaders, static S#, nonzero texture base levels, comparison or implicit-LOD gathers, other GPUs, the current AI use Codex in T3 Code ( Arc A770 rerun at
|
|
Re-checked One case doesn't: LOD 0.499 (as a literal) reads level 0 on both backends, where RDNA2 reads level 1. boykopovar/AnyPS5#23666 measured the level change at LOD·256 = 127.5: 127.495 reads level 0, and 127.5 already reads level 1. That's because the LOD is first rounded to 1/256 with halves up, and then the nearest level is taken with ties up. On your two assumptions:
AI-assisted (Claude Code). Synthetic tests only. |
51361a6 to
b0f160f
Compare
|
Reviewed Two items remain before merge:
|
image_gather4_l / _l_o took mip 0 in both backends. The level now follows the S#: LOD plus LOD_BIAS, clamped to MIN_LOD..MAX_LOD, rounded half up, clamped to the view's mips; MIP_FILTER none reads level 0. The four taps are Loads at that level with the S# addressing applied per axis, so the host sampler and the view's clamp cannot move them. 2D and 2D arrays; cube and 3D are refused.
Per the gfx1036 measurement: with MIP_FILTER none the level is floor(T# MIN_LOD); otherwise the T# MIN_LOD (word1[19:8], u4.8) raises the LOD after the S# clamp, before the half-up rounding. Both backends; four T# MIN_LOD cases in gpu_gather4_explicit_lod.
RDNA2 rounds the LOD to u4.8 with halves up, then takes the nearest level with ties up (boykopovar/AnyPS5#23666): floor(l + 257/512) does both, so LOD 0.499 reads level 1. Both backends; gpu_gather4_explicit_lod tests 0.499 instead of 0.6.
b0f160f to
8eb5a5c
Compare
|
@raylitebull Thanks for catching the rounding edge case. It’s fixed in |
|
Re-checked AI-assisted (Claude Code). Synthetic tests only. |
|
@upipibi Thanks for retesting both backends on |
|
Thanks @JJAutoCompare for rerunning both fixtures on the Arc A770! Could someone with an AMD GPU independently verify that explicit LOD 0.499 selects mip 1 on both the HLSL and direct DXIL paths at |
|
AMD run of head Result: the new test Tested
Not tested
AI use
|
image_gather4_landimage_gather4_l_oread mip 0 in both backends, whatever LOD the program passes. On the HLSL side,Gather{ch}()has no level argument, and the texel offset was scaled with mip 0's size (emit.rs, thehas_lz || gather4arm). The direct DXIL path had the same logic inlower_image.rs. In the retail title's shader pack,image_gather4_lappears in 16 programs andimage_gather4_l_oin 16.What changes
LOD_BIAS, clamped to the S#MIN_LOD..MAX_LOD(u4.8), raised to the T#MIN_LOD(word1[19:8], u4.8), thenfloor(l + 257/512), then clamped to the view's last mip.floor(l + 257/512)does both steps;round()in DXIL goes to even.MIP_FILTERnone it readsfloor(T# MIN_LOD)at every LOD, as the same measurement found (1.75 → 1, 2.5 → 2). The T#MIN_LODcomes after the S# clamp; the other order was off there.MIN_LODis taken relative to the view's base level, like the LOD. Ashortcut:comment marks it: the tests only useBASE_LEVEL0.Loads at that level, so the host sampler's MinLOD/MaxLOD, bias and filter, and the view'sResourceMinLODClamp, can't move them to another level. The S# addressing is applied per axis in the shader:floor(coord * size(level) - 0.5)plus the 6-bit signed offset, counted in texels of that level. The output is inGather's component order, for the dmask channel.gather4 explicit LOD (not 2D)instead of silently reading mip 0. No program in the retail pack hits it:xcl shaders --recompstill translates 26 698 of 26 698 with 0 errors._lzgather.gather4_c_l,_c_l_o) keep the mip-0GatherCmp.SampleCmpLevelneeds SM 6.7, and withLoads the depth compare would have to be done in the shader.gather4_b,_b_cl,_c_band their_oforms) drop the bias.gather4/gather4_cin a pixel shader read level 0, not the implicit LOD.Files
gpu/recomp/emit.rs(plus thegat_axhelper in the HLSL prelude) andgpu/dxil/lower_image.rs(Lw::gather_axis): the explicit-LOD gather path.gpu/recomp/runner.rs: kernel test textures can carry mips.TestTexture::mips()works the count out from the data's length (SRV-only 2D and 2D arrays whose data holds each slice's full chain to some level; 1 otherwise). It is a method, not a field, so no other test's literal changes. An earlier field version broke twice on literals that other PRs added tomain.gpu/recomp/tests.rs:gpu_gather4_explicit_lodruns through both the HLSL and the direct DXIL backend. It uses an 8×8 texture and a 2-layer array, 4 mips each, with distinct values per mip and per texel. It covers 18 S# / T# cases:MAX_LOD1,MIN_LOD2, bias ±1;MIN_LODof 1.75 and 2.5 with no mip filter, 1.75 with a point mip filter, and 1.75 over an S#MAX_LODof 1.gpu_ps_image_gather4_l_s_sharp_level: @upipibi's three review cases, in a pixel shader through the runtime's ownsampler_d3d12. It runs on the HLSL backend only, because the draw helper is HLSL-only.Tested
maincbc296a+ this PR,cargo test --release -p xcl --lib -- --test-threads=4: 1100 passed, 0 failed, 26 ignored. Same withXCL_HEAP_WINDOWS=1. WithXCL_DIRECT_DXIL=1, 1099 passed and 1 failed (gpu_image_get_resinfo_1d_array), which fails the same way onmaincbc296aalone.emit.rs/lower_image.rschanges reverted (tests kept), both tests fail. First failure:gather4_l hlsl [clamp, point mip] lane 8 (uv 0, 1; lod 0.5; offset (-1, 0)), got[70, 70, 70, 70], expected[1030, …]. Also(b) LOD 2, MAX_LOD 1: pixels off the level-1 footprint, 192 instead of 0.lower_image.rsreverted:gather4_l direct dxil [clamp, point mip] … lod 0.5.cargo clippy --release -p xcl --all-targets: no new warnings (772 oncbc296aand on this branch).5f3e9ec(888 passed); @upipibi checked that head on an RTX 3070.Regression run
With
XCL_NETWORK=0 XCL_PAGE_BUDGET_MB=4096, back to back:maincbc296a:This PR (
8eb5a5c): 110.4 % ofmain's frames, 89.7 % of its peak commit.Overlap
levelmatch inemit.rs(sample offsets), so whichever lands second needs a small rebase._lzforms only.Lw::mimg_samplerand the direct DXIL sample path into a guarded closure; this branch is rebased on both (maincbc296a), as three commits.gather4_l/_l_oforms in the direct DXIL backend only, plus the compare andUNORMforms. The two conflict inlower_image.rs; this PR also covers the HLSL backend and checks both with GPU pixel tests.Not tested
MIN_LODwithBASE_LEVEL> 0, and whether a fractional T#MIN_LODwith a point mip filter rounds half up (as here) or down.float4as before.AI use
MIN_LODfollow-up was written by Claude Opus 5.5 directly.AI-assisted.