Repository navigation
Recompiler: gather4 with an explicit LOD reads the mip it names - #1812
brothernog wants to merge 1 commit 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:
|
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_l / _l_o took mip 0 in the HLSL backend. The level now follows the S# as in the direct backend (xcl-dev#1951): LOD plus LOD_BIAS, clamped to MIN_LOD..MAX_LOD, raised to the T# MIN_LOD, then floor(l + 257/512) (the LOD rounded to 1/256, ties up), clamped to the view's mips; MIP_FILTER none reads floor(T# MIN_LOD). 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. The half-border modes (4, 5) give no border colour, as in desc::sampler_d3d12 and the direct backend. 2D and 2D arrays; cube and 3D are refused. The kernel test runner gets mip chains (TestTexture::mips from the data length). gpu_gather4_explicit_lod_hlsl / _direct run the same cases through each backend (one test per backend, so a driver crash names it); the direct one runs xcl-dev#1951's code on a GPU.
8eb5a5c to
a56538a
Compare
|
Rebased onto @futurepro62 thanks for the AMD run. I have no AMD GPU here, so I couldn't reproduce the crash. Two changes should make a rerun more telling:
I read the host side again ( Running the direct test against #1951 turned up one disagreement: the half-border address modes 4 ( Tested
This comment was AI-assisted. |
|
Independent test and smoke report for head Tested
Regression run (
PR Not tested: targeted gather unit tests (filter gather4 matched 0), full workspace suite, NVIDIA/Intel GPUs, Linux/Wine, or world load past the 60 s smoke window. AI use: Antigravity (Gemini 4 Neon) ran the builds, tests, and smoke runs and drafted this report; the account owner reviewed and approved posting. |
|
Independent AMD RDNA4 / Linux (RADV, native wave64) test report for head Hardware & environment
Tests (
Not tested
AI use
|
|
Rerun of head Result: on the RDNA2 iGPU the HLSL form crashes and the direct form passes. Tested
So on this machine it comes down to the HLSL path of the explicit-LOD gather, and the direct form passes on the same iGPU. The same test passes on the discrete RX 9070 above, so this may be specific to the integrated RDNA2 part rather than to AMD in general. The RX 6600 run above (a discrete RDNA2 card, the closest match) matched 0 tests with its filter, so it doesn't settle that yet. Not tested
AI use
|
|
Re-verification on RTX 3060 Ti (Windows 11), new head merged with current
Thanks! — Devin |
image_gather4_landimage_gather4_l_oread mip 0 in the HLSL backend, whatever LOD the program passes:Gather{ch}()has no level argument, and the texel offset was scaled with mip 0's size (emit.rs, thehas_lz || gather4arm). #1951 (merged) fixed the same forms in the direct DXIL backend; this PR does the HLSL side with the same level rule and adds GPU tests that run both backends. 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 (1.75 → 1, 2.5 → 2). The T#MIN_LODcomes after the S# clamp.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 (gat_axin the HLSL prelude):desc::sampler_d3d12and dxil: emulate explicit-LOD gather4_l(_o) and support UNORM=1 non-cube sampling #1951. Not measured on RDNA2.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.gather4_c_l,_c_l_o), the bias forms (gather4_b,_b_cl,_c_band their_oforms), and plaingather4/gather4_cin a pixel shader.Files
gpu/recomp/emit.rs: the explicit-LOD gather path and thegat_axhelper.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.gpu/recomp/tests.rs:gpu_gather4_explicit_lod_hlslandgpu_gather4_explicit_lod_directrun the same cases through each backend (one test per backend, so a driver crash names it). The direct one runs dxil: emulate explicit-LOD gather4_l(_o) and support UNORM=1 non-cube sampling #1951's code on a GPU. They use an 8×8 texture and a 2-layer array, 4 mips each, with distinct values per mip and per texel, and 18 S# / T# cases: point, linear and no mip filter;MAX_LOD1,MIN_LOD2, bias ±1; wrap, mirror, mirror once and mixed axes; the border and half-border variants; T#MIN_LOD1.75 and 2.5 with no mip filter, 1.75 with a point mip filter, and 1.75 over an S#MAX_LODof 1. LODs include the ties 0.5 and 2.5, 0.499, one past the last mip and one below 0, plus negative and far offsets.gpu_ps_image_gather4_l_s_sharp_level: @upipibi's three review cases, in a pixel shader through the runtime's ownsampler_d3d12(HLSL only: the draw helper is HLSL-only).Tested
main68a284a+ this PR (a56538a):cargo test -p xcl --lib -- gather4 --test-threads=1(debug): 8 passed.cargo test --release -p xcl --lib -- --test-threads=4: 1222 passed, 1 failed, 26 ignored. The failure,gpu::recomp::lower_dxil::tests::a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier, fails the same way onmain68a284aalone.emit.rsback tomain(tests kept):gpu_gather4_explicit_lod_hlslfails (gather4_l hlsl [clamp, point mip] lane 8 (uv 0, 1; lod 0.5; offset (-1, 0))) and so doesgpu_ps_image_gather4_l_s_sharp_level((a) LOD 1, full range, point mip);gpu_gather4_explicit_lod_directpasses.cargo clippy --release -p xcl --all-targets: no new warnings (818 on68a284aand on this branch, same count).8eb5a5c; see the thread), @upipibi (RTX 3070) and @JJAutoCompare (Arc A770).Regression run
With
XCL_NETWORK=0 XCL_PAGE_BUDGET_MB=4096, back to back,mainfirst (one run each; frame counts vary from run to run, so the difference is noise-level):main68a284a:This PR (
a56538a):Overlap
levelmatch inemit.rs(sample offsets), so whichever lands second needs a small rebase._lzforms only.Not tested
8eb5a5cis not reproduced or explained here) and Intel at this head; wave64-native hardware.MIN_LODwithBASE_LEVEL> 0, the half-border modes on hardware, integer-format textures.AI use
AI-assisted.