Skip to content

Shaders: support image_sample_d_cl and image_gather4_l lowering (#293, #653) - #694

Closed
LAKSHYA-cmd-tech wants to merge 1 commit into
Marvisak:mainfrom
LAKSHYA-cmd-tech:fix/image-sample-d-cl-and-gather4-l
Closed

LAKSHYA-cmd-tech wants to merge 1 commit into
Marvisak:mainfrom
LAKSHYA-cmd-tech:fix/image-sample-d-cl-and-gather4-l

Conversation

@LAKSHYA-cmd-tech

Copy link
Copy Markdown

What

Resolves two frequent shader conversion failures during world load identified in #293 (~103 dropped draws/dispatches per frame) and #653:

  1. image_sample_d_cl:

    • Explicit gradient sampling with LOD clamp (_cl).
    • Mapped to DXIL opcode 63 (sampleGrad.f32), reading the clamp operand at irst + grads + coords and passing it as minLODClamped (operand 16 of SampleGrad).
    • Also added image_sample_cl and image_sample_b_cl clamp handling.
  2. image_gather4_l and image_gather4_l_o:

    • Texture gather with explicit level (_l), which console compute and pixel shaders emit.
    • Lowers to DXIL opcode 73 ( extureGather.f32), extracting texel coordinates (and programmable offsets for _o) and gathering at the base mip level.

Tests

  • Added synthetic unit test image_sample_d_cl_and_gather4_l_translate in crates/victory-shader/src/emit/lower.rs.
  • cargo test -p victory-shader: 70 passed, 0 failed.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean (0 warnings).
  • cargo xtask policy: ok (151 files verified).

No game data used.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cargo test -p victory-shader --locked on macOS 26 arm64, rustc 1.98.1, head c169328: 57 passed, 0 failed (+1 vs main — the new lowering coverage runs ungated). Approving.

@r4tedx

r4tedx commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

A review note from reading the diff at c169328, with two facts about the title's shaders that testers measured for #160, which lowers the same two instructions. I have not built or run this PR.

1. image_gather4_l reads level 0. The change adds image_gather4_l and image_gather4_l_o to the arm that emits textureGather (opcode 73), next to the _lz forms. textureGather has no level operand, and I see nothing in the diff that uses the level, so a gather at level 2 would return level 0's texels. That matters here: over the retail shaders, none of the 12 image_gather4_l programs has a level the translator can prove is 0 (EduAzevedo on #160). #360 had the same gap and its author deferred it. In #160 a gather at a run-time level becomes four samples at the footprint's texel centres at that level, so the sampler's address mode and level range still apply; a level known to be 0 stays a real gather.

2. image_sample_d_cl on a cube. The clamp is read at first + grads + coords, and the gradient count is not changed. Every image_sample_d_cl in the title is on a cube, with eight address registers and no a16: two gradients per direction (the face coordinates'), then the three coordinates and the clamp (EduAzevedo). main expects three gradients per direction on a cube and runs past the operand list. #318 lowered _d_cl without that cube rule and the count of refused programs did not move: 27 at image_sample_d_cl became 27 at an image address (nullmarsh on #318). From reading only, I expect the same here.

A smaller point: the new test checks that the two programs lower, not what they compute. A GPU test with a mipmapped texture whose levels hold different values would have caught the first point.

Where #160 stands, for whoever decides between them: on the title's shaders 23 of the 27 image_sample_d_cl programs and all 12 image_gather4_l programs translate and validate, nothing that translated on main is lost, and in the title #293's top G-buffer pixel shader 7bdefdfde461f6e3 runs with it (nullmarsh). Those are the linked testers' measurements, not mine.

Nothing was run for this note; the title was not used. AI-assisted.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

I ran the same generated mip texture on c169328 and #160 at 25e917b, on the default D3D12 adapter and WARP.

With 32 lanes requesting mip 0, 1 or 2, image_gather4_l on this PR matched 11/32 lanes on each backend. All 21 requests for mips 1 and 2 missed, and the representative lanes for both returned the mip-0 tuple. Both controls (image_sample_l and image_gather4_lz) matched 32/32. The same gather matched 32/32 on #160 on each backend.

The 2D image_sample_d_cl case on this PR correctly selected minimum LODs 0, 1 and 2 per lane. The eight-address cube case from #160's synthetic test stopped at an image address on this PR. On #160 it ran and matched the expected face and mip on both backends.

I also checked image_sample_b_cl with a zero bias and minimum LOD 2. The plain _b control matched 32/32, while _b_cl matched 0/32. Counting the bias address for both SampleBias forms in first (usize::from(op == 61) in place of the exact image_sample_b name check) restored 32/32 on both. #160 already passed that case as well.

These results favor using #160 for the overlapping gather and cube paths. If the sampling clamp changes are split out here, the bias-address fix above preserves the coordinate and clamp operand positions.

@slint6614-dev

Copy link
Copy Markdown

Measured on the full retail shader pack (26,503 containers) with victory translate --validate, against main d2a4bae (emitted / validated):

VS/PS CS MS cargo test -p victory-shader
main 12272 / 12172 1693 / 1670 8191 / 8129 ok
#694 12272 / 12172 1705 / 1682 8191 / 8129 ok
#563 12297 / 12197 1693 / 1670 8191 / 8129 ok
#160 12345 / 12237 1705 / 1682 8191 / 8129 ok
#136 12619 / 12619 1796 / 1697 8355 / 8302 ok

Windows 11, dxil.dll from SDK 10.0.26100.

@iurysf

iurysf commented Nov 24, 2026

Copy link
Copy Markdown

Measured offline against main c2e7254 with victory translate --list --validate over the title's shaders.rpf + executable (26,096 programs), dxil.dll 1.9.2602.17, comparing outcomes program by program: failures 3,734 -> 3,722 (−12). All 12 "image_gather4_l" refusals now emit and validate, and there are no regressions. On this corpus, the 27 "image_sample_d_cl" refusals are not among those fixed here; #563 clears those (27 -> 0, see my comment there).

AI-assisted.

@Halfect Halfect left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A merge-compile note on current head 0beec4d, verified locally. The correctness questions raised above (gather level, cube gradients, _b_cl bias operand) are separate and still stand; this is only about building against current main.

main commit 6638098 (merged with #566) changed victory_shader::resource::track to take a fourth argument, lanes: Option<&Structured> — tracked for open PRs in #801, which lists this PR among the mergeable ones affected. GitHub shows no text conflict, so nothing looks wrong until CI or a merger runs the tests:

git merge --no-edit 0beec4d   # onto main ffe310e — merges cleanly
cargo check --locked -p victory-shader --tests
error[E0061]: this function takes 4 arguments but 3 arguments were supplied
  --> crates/victory-shader/src/emit/lower.rs:7471
   | let res = resource::track(&p, &native.layout, native.stage).unwrap();
  --> crates/victory-shader/src/emit/lower.rs:7516
   | let res = resource::track(&p, &native_cs.layout, native_cs.stage).unwrap();

Both are the new calls in image_sample_d_cl_and_gather4_l_translate. Since the test computes st after track and exercises no per-lane descriptors, passing None keeps the old behavior:

let res = resource::track(&p, &native.layout, native.stage, None).unwrap();

With that one change at both call sites, the merged tree compiles and the new test passes (cargo test --locked -p victory-shader --lib image_sample_d_cl_and_gather4_l_translate: 1 passed, Windows, rustc 1.98.1). Same fix two of my own PRs needed today (#655, #683).

Verified CPU-only; no game files, no GPU run.

MfDev0 referenced this pull request in MfDev0/victory Nov 24, 2026
The LOD form of a gather stopped its program. DXIL gathers from level 0
and has no LOD operand, so the console's LOD is not read: exact for the
level-0 gathers titles make of shadow maps and G-buffers.

From upstream JMAN730#694 (0beec4d, JMAN730#293, JMAN730#653); its image_sample_cl /
image_sample_b_cl / image_sample_d_cl halves are already in this tree.
The test also covers image_sample_d_cl.

(cherry picked from commit 0beec4d)

Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.com>
@bittertea0323

Copy link
Copy Markdown

Verified offline against the whole shader corpus (26,084 programs, 4070 SUPER / Windows 10)

Applied 0beec4d cleanly on ffe310e + local diag changes, then ran the offline batch over the
title's shader container dump (26,503 containers, 26,084 programs, same file both runs):

failure class / metric before after
emit: image_sample_d_cl 29 0
emit: image_gather4_l 16 0
emit: a cross-lane operation outside compute 46 48
compute programs emitting DXIL 1443 / 2582 1459 / 2582
vertex+pixel programs emitting DXIL 12252 / 14027 12252 / 14027
mesh programs emitting DXIL 8214 / 8713 8214 / 8713

Both target classes are gone and 16 more compute programs reach DXIL. Two things worth knowing
before merge:

  1. The 29 sample_d_cl programs did not become emittable — the VS+PS count is unchanged — so
    they carry a second failure class as well. The counter dropping is not the same as those
    programs emitting.
  2. a cross-lane operation outside compute went 46 → 48: a new failure rather than a
    reshuffle of the existing ones. Small, but worth a look.

Repro: cargo build --release -p victory then victory translate <container dump>; both runs
used the same container file on the same host. The rest of the failure table is unchanged from
baseline (translate-baseline in our notes), so nothing else moved.

PainDe0Mie added a commit to PainDe0Mie/VIctory that referenced this pull request Nov 24, 2026
PainDe0Mie added a commit to PainDe0Mie/VIctory that referenced this pull request Nov 24, 2026
MfDev0 referenced this pull request in MfDev0/victory Nov 24, 2026
makeDouble from the register pair, then fptrunc; declares double
precision (ShaderFlags bit 2, SFI0 0x1). Inline constants read as a
64-bit operand are doubles (floats) or sign-extended integers, and abs
and neg act on the high half's sign bit.

From upstream PR Marvisak#978 (c8c117a), hand-ported: image_sample_d_cl was in
already (JMAN730#694); the double-precision flag is bit 2, not bit 0
(DisableOptimizations), and inline float constants are encoded as
doubles.

Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.com>
nazmi added a commit to nazmi/VIctory that referenced this pull request Nov 24, 2026
A real run of the title refuses 31 shaders with 'an address in an SGPR that is no root parameter', but that message is wrong for most of them: the parameter exists and the address came from its second or later dword. The message now says which case it is - a root descriptor's high half, a root constant past the first dword, a descriptor heap index, a parameter's later dword, or genuinely no parameter - so the next run decides the fix instead of requiring a guess.

Diagnostics only: no lowering behaviour changes. docs/world-load-diagnosis.md records what the run achieved, the ranked refusals, and why Marvisak#694's image_gather4_l change was not taken.
nazmi added a commit to nazmi/VIctory that referenced this pull request Nov 24, 2026
A real run of the title refuses 31 shaders with 'an address in an SGPR that is no root parameter', but that message is wrong for most of them: the parameter exists and the address came from its second or later dword. The message now says which case it is - a root descriptor's high half, a root constant past the first dword, a descriptor heap index, a parameter's later dword, or genuinely no parameter - so the next run decides the fix instead of requiring a guess.

Diagnostics only: no lowering behaviour changes. docs/world-load-diagnosis.md records what the run achieved, the ranked refusals, and why Marvisak#694's image_gather4_l change was not taken.
@alexdeliu

Copy link
Copy Markdown

Merged onto main @ ffe310eb, this PR (efd02d56) no longer compiles its tests. crates/victory-shader/src/emit/lower.rs:7471 (and lower.rs:7516) calls resource::track with 3 arguments. Since 6638098 it takes a 4th, lanes: Option<&Structured>; main's tests pass Some(&st) with let st = structure::structure(&p).unwrap();.

Checked with CI's steps (both clippy runs with -D warnings, cargo test --workspace) on Windows 11, AMD RX 9070. Every open PR: https://gist.github.com/alexdeliu/ef9ef0ebc1776ff104b5aa8eaea18e87

@MoonYueya

Copy link
Copy Markdown

This PR covers the two instructions our own 303-failure window flagged most often in class E, so we are not going to re-describe them — you have the lowering. Two things from our side that the PR description does not mention, in case they are useful for the follow-on cases:

Parse the modifiers as a set, not as part of the opcode name. l, lz, o and cl are orthogonal to the gather/sample family, which is what makes image_gather4_l and image_sample_d_cl fall out of the opcode table in the first place — they are not missing opcodes, they are unparsed suffixes. Structuring the check as "family + modifier set" also picks up image_gather4_l_o (level and offset) and image_sample_cl / image_sample_b_cl for free, which the PR body does not name.

Gather picks one channel per instruction. The dmask names which one, so the call is GatherRed / GatherGreen / GatherBlue / GatherAlpha, and GatherCmp* when a comparison sampler is bound. A dmask that does not name exactly one channel is worth refusing explicitly rather than picking one.

For reference on the second point, our own lowering's shaping: has_cl / has_o / has_l / has_lz come out of a single mods split, and the LOD is 0u when lz is present, the address when l is present, and computed from derivatives otherwise (pixel stage only). That ordering matters — lz has to win over l, and both have to win over the implicit-derivative path.

(AI-assisted write-up, checked against our own lowering.)

Context: failure measurements in #1481.

@MrEjecutor

Copy link
Copy Markdown

image_gather4_l here gathers at level 0, but the title names a level; a version that reads it, with a GPU test, is on a branch

DXIL TextureGather (opcode 73) has no level operand: it always reads mip 0. With this PR, image_gather4_l / image_gather4_l_o translate, but they read level 0 wherever the console program asks for level n.

Which programs use them, on the retail shaders (victory translate --list on main ffe310e): 17 compute programs are refused on image_gather4_l today, 11 in meshlet_culling (CS_BatchCullMeshlets, CS_BatchCullPreSkinnedMeshlets, CS_BatchCullBatchCSMeshlets and their _Prev / _ParaboloidShadows variants) and 6 in water_micro_geom_renderer (patch generation and culling). According to the fxceless/victory-gta6 fork (the one that renders the lit world on NVIDIA, #1528), the meshlet culling reads its depth pyramid this way: a 4x4 block at the meshlet's level, as one image_gather4_l and three image_gather4_l_o. Read at level 0, "most of the world was culled against the wrong depths", in the fork's words.

A version that reads the named level, written from scratch on main: https://github.com/MrEjecutor/VIctory/tree/gather4-l-level (one commit, 4f44097, on ffe310e). For 2D textures it reads the 2x2 footprint at the named level with four textureLoads in gather order, the offset in texels of that level; cube, array and 16-bit forms stay refused. Shortcuts, marked in the code: the level is rounded and clamped to the texture's levels, and texels past the edge are clamped whatever the sampler says.

Measured:

  • translate --validate over the retail shaders: no program refused on image_gather4_l any more; all of meshlet_culling validates; compute validated 1,417 → 1,433 of 2,582.
  • A GPU test (gather_level_tests.rs, on an RTX 5060 Ti and on WARP): six gathers at levels 1 and 2 of an 8x8 texture, with and without offsets and at the edge, return the expected texels. With the level forced to 0 the test fails on its first case.
  • cargo test -p victory-shader: 122 passed (Rust 1.100). cargo xtask policy: ok. Clippy with warnings denied is clean on the change (1.100 flags one existing loop in lower.rs:678, unrelated).

Not tested: the title with this branch (on this card main alone still hangs in the world, see #747); AMD; the water programs' output.

If it is useful, it can replace the two gather arms here, or be cherry-picked on its own.

AI use: written with Codex (gpt-6-luna) and Claude Code (Claude Opus 5.5); checked and measured by mrejecutor. The analysis that pointed at the level is fxceless's; no code was taken from their fork.

@Marvisak

Copy link
Copy Markdown
Owner

Automated triage for the maintainers. Closing because this change is already in main, or main has since replaced the code it patches.

image_sample_d_cl and image_gather4_l/_l_o are both lowered in main (emit/lower.rs image(), gather_at_level).

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

@Marvisak Marvisak closed this Nov 25, 2026
@MrEjecutor

Copy link
Copy Markdown

Follow-up to my comment above: main at 70564ae now lowers image_gather4_l / image_gather4_l_o itself and loads the four texels from the named level (gather_at_level in emit/lower.rs, with the sampler's border and a level proven whole, which my version did not handle). So the gather4-l-level branch I pointed to is superseded and I am not proposing it.

AI use: Claude Code; checked by mrejecutor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.