Skip to content

Shaders: extend image_gather4_l(_o) to all address modes, borders, a16, d16, unorm, and 2D arrays - #1853

Open
ESAT586 wants to merge 2 commits into
Marvisak:mainfrom
ESAT586:shader/gather-at-level-modes
Open

ESAT586 wants to merge 2 commits into
Marvisak:mainfrom
ESAT586:shader/gather-at-level-modes

Conversation

@ESAT586

@ESAT586 ESAT586 commented Nov 24, 2026

Copy link
Copy Markdown

Summary

Extends image_gather4_l and image_gather4_l_o emulation (Lower::gather_at_level) on top of 70564ae beyond 2D transparent-black-border 32 gathers:

  1. All 6 RDNA2 point/gather address modes per axis (including mixed X/Y):
    • WRAP (

…6, d16, unorm, and 2D arrays

Extend gather_at_level (image_gather4_l and image_gather4_l_o) beyond
2D transparent-black-border f32 gathers:

- Support all six RDNA2 point/gather address modes per axis (including
  mixed X/Y modes): WRAP (0), MIRROR (1), CLAMP_LAST_TEXEL (2),
  MIRROR_ONCE_LAST_TEXEL (3), CLAMP_BORDER (6), and
  MIRROR_ONCE_BORDER (7), using exact IEEE-754 period reduction
  (u - trunc(u) and u - 2 * trunc(u * 0.5)) for periodic modes and
  [-64, 64] clamping for clamp/border modes before the 64-bit fixed-point
  texel index calculation.
- Support all three built-in border colours (TRANSPARENT_BLACK,
  OPAQUE_BLACK returning 1.0 on alpha and 0.0 on RGB, and OPAQUE_WHITE
  returning 1.0 on every channel), ignoring �order_color_type when
  neither axis uses a border mode.
- Support whole sampler lod_bias, �16 (with f16 whole-LOD provenance
  tracking in whole_f16 for both low and high halves), d16 packed
  outputs, unnormalized (unorm / FORCE_UNNORMALIZED) coordinates on
  clamp/border modes, and Texture2DArray (dim = 5, ties-to-even slice
  rounding clamped to [0, array_size - 1]).
- Add View::MippedArray to the test harness and end-to-end GPU + WARP +
  DXIL-validated tests verifying all 11 sampler configurations against
  both the CPU reference and native hardware image_gather4_lz at level 0,
  plus �16, d16, unorm, Texture2DArray, and negative tests for
  still-refused sampler options.

AI disclosure: implemented and verified with Gemini 4 Neon (Extra High Mode)
using only synthetic test cases and public RDNA2 ISA documentation; zero
game binaries or copyrighted assets were accessed.
@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown

NVIDIA run: RTX 3070 8 GB, driver 616.56, plus WARP, Windows 11, debug, 0ec4750 (on main 70564ae). cargo test -p victory-shader --locked: 209 passed, 3 failed, 2 ignored. The 3 are #1744's NVIDIA failures on main.

On the cases left refused ("a gather at a level that may not be whole", fractional LOD limits and bias): the level rule for a fractional LOD has now been measured on RDNA2 (gfx1036) in boykopovar/AnyPS5#23856 and #23666. LOD bias is added first, then the S# MIN_LOD / MAX_LOD clamp, then the T# MIN_LOD. The LOD is rounded to 1/256 with halves up, and the nearest level is taken with ties up, i.e. level floor(lod + 0.501953125). A linear MIP_FILTER gathers that single level too. That would let those cases be accepted rather than refused.

AI-assisted (Claude Code). Synthetic tests only.

@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.

Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (70564ae), cargo test -p victory-shader --locked → 118 passed, 0 failed. GPU execution tests are #[cfg(all(test, windows))] and run only on Windows.

image_gather4_l(_o) extended to all address modes/borders: gather lowering coverage widened.

@AlanQiang0418

Copy link
Copy Markdown
Contributor

Two independent follow-ups on 0ec4750.

Null texture and an opaque border. I dispatched the PR's existing address-mode shader twice, keeping the sampler, input coordinates and output layout identical. The second dispatch bound a typed RGBA32F null Texture2D SRV. With opaque white/red or opaque black/alpha, both explicit-level gathers returned four 1.0 values on all 32 lanes. The native level-zero gather in the same shader returned four zeros. I reproduced this on an RTX 5080 (621.14) and Microsoft WARP. The runtime creates typed null texture descriptors in null_like and null_view. D3D12 null descriptor behavior

Null-texture fix and regression. The emulation checks the mip-zero width before calculating a footprint or applying a border color. All 11 sampler configurations now return zero for the null view on both backends, with the original live-texture checks still passing.

Whole-LOD check for packed A16 array coordinates. For v_lshl_or_b32 dst, src0, 16, src2, the upper half is src0.low | src2.high. With src0 = 0x3c00, changing src2 from 0x00003c00 to 0x02003c00 keeps the array slice at 1 but changes the half-float LOD from 1.0 to 1.5. The original proof accepts both. Packed-LOD fix and four synthetic cases. The literal and per-lane fractional cases are now rejected, and a per-lane whole LOD and a constant −1 remain accepted.

Both commits apply together. Five gather tests passed with both applied, and shader all-targets Clippy passed. The sixth gather test, gathers_at_a_level_match_the_hardware, still differs on RTX lane 0 ([0, 1, 0, 0] vs [0, 0, 0, 0]). I got the same result on clean upstream 70564ae, clean PR head 0ec4750, and both commits combined. Previous hardware report

…f in gather_at_level

Addresses two review findings on Marvisak#1853:
- Null Texture2D/Texture2DArray SRVs (mip-0 width == 0) now return 0.0 for all
  four channels even with opaque border colors (`OpaqueWhite`, `OpaqueBlack` alpha),
  matching native D3D12 `textureGather`, and avoid `URem` by zero on wrap/mirror.
- `whole_f16` on `v_lshl_or_b32 dst, src0, 16, src2` now requires `src2[31:16]` to
  be provably zero (or evaluates constant pairs directly) so a non-zero upper half
  in `src2` cannot turn a whole high-half f16 LOD into a fractional value; 16-bit
  float ops (`v_pack_b32_f16`, `v_min_f16`, `v_max_f16`) also decode inline float
  constants `240..=248` as f16 bits.
- Adds `View::NullTexture` (`#[cfg(test)]`) and regression tests covering all 11
  sampler configs against a null SRV on hardware + WARP plus the 4 `v_lshl_or_b32`
  packed-LOD validation cases.

Clean-room synthetic tests only; zero game data used.
AI-assisted with Gemini 4 Neon (Extra High Mode).
@ESAT586

ESAT586 commented Nov 25, 2026

Copy link
Copy Markdown
Author

Thanks @AlanQiang0418 and @upipibi for the thorough hardware runs and exact catches!

Both bugs reported by @AlanQiang0418 are now fixed in the latest commit on this branch:

  1. Null texture SRV + opaque border color: gather_at_level now checks mip-0 width (w0 == 0 from getDimensions(h, 0)), guards URem divisors against zero on WRAP/MIRROR, clamps null-view mip level to 0, and forces 0.0 across all 4 gather results (matching native D3D12 textureGather even under OpaqueWhite and OpaqueBlack alpha). Added View::NullTexture (#[cfg(test)]) and gathers_at_a_level_return_zero_on_null_texture_even_with_opaque_border testing all 11 sampler configs on hardware + WARP.
  2. whole_f16 soundness for v_lshl_or_b32 dst, src0, 16, src2: when inspecting the packed high half (hi == true), whole_f16 now evaluates constant (src0, src2) pairs together or requires high16_zero_op(src2) to prove src2[31:16] == 0 (and decodes inline float constants 240..=248 as f16 bits for 16-bit float ops). Added gathers_at_a_level_a16_packed_lshl_or_validates_src2_high_half covering literal fractional src2, per-lane unmasked src2, per-lane whole + masked src2, and constant -1.0f16.

Regarding @upipibi's note on fractional LOD rounding (floor(lod + 0.501953125)): keeping fractional-LOD acceptance out of this PR to stay focused and preserve the existing scope; happy to follow up separately if desired.

Re-verified on Windows 11 with NVIDIA GeForce RTX 3060 Ti + Microsoft D3D12 WARP + dxil.dll:

  • cargo test -p victory-shader --lib emit::synthetic_tests::gathers_at_a_level_ -- --test-threads=1 --skip gathers_at_a_level_match_the_hardware: 6 passed, 0 failed
  • cargo clippy --workspace --all-targets -- -D warnings & --all-features: 0 warnings
  • cargo xtask policy: ok

(Clean-room synthetic tests only, zero game data. AI-assisted with Gemini 4 Neon (Extra High Mode).)

@upipibi

upipibi commented Nov 25, 2026

Copy link
Copy Markdown

Re-checked 59b8454 on the RTX 3070 (driver 616.56, Windows 11, debug). cargo test -p victory-shader --locked gives 211 passed, 3 failed, 2 ignored. The 3 are #1744's NVIDIA failures on main (the two stride_tests heap-buffer reads and gathers_at_a_level_match_the_hardware). The new null-texture test passes here, so this matches main plus the new tests.

AI-assisted (Claude Code).

@AlanQiang0418

Copy link
Copy Markdown
Contributor

Retested your integrated 59b8454 on RTX 5080 (621.14) and WARP. The null-texture and packed-LOD cases pass. With #1887 at bb5acba applied, the previously failing legacy gather also passes: 7/7 gather tests.

I found one remaining way for a fractional LOD to pass the updated proof. Take the existing loaded slice word v13, copy it with v_and_b32 v16, v13, v13, and use v16 as the final operand of the shift-OR. The AND preserves bit 25, which can change the packed half from 1.0 to 1.5. On this head the checker fails to prove the first operand has zero high bits, then accepts the same value on its second visit. The synthetic shader consequently translates.

Follow-up commit on your new head caches the proof result and scopes loop assumptions to the incoming edges of each phi. The regression rejects the unmasked copy, accepts a copy masked to 16 bits, and checks both outcomes with a two-iteration loop. With the follow-up and #1887's view change applied, all seven gather tests, the indexed-buffer read test and shader all-targets Clippy passed.

@andrewpozdnakov7-jpg

Copy link
Copy Markdown

I checked the gather coordinate product on your current head, 59b8454. It sets Int64ShaderOps even though the final coordinate fits in 32 bits for every D3D12 texture dimension. DirectX feature requirements

16a4e77 is one commit directly on that head. It splits the signed product at bit 16 and preserves the exact footprint rounding. I ran the same test before and after on WARP, including a 16,381-texel-wide 2D array plus the A16, D16 and unnormalized cases. The texel readbacks matched on both builds. The SFI0 assertion failed with 0x8000 before and passed with zero after.

Full forced-WARP suite: 211 passed / 3 failed / 2 ignored before, 212 / 2 / 2 after. The legacy native gather comparison and lane permute produced the same failures in both runs. Strict shader Clippy and asset policy passed.

@AlanQiang0418

Copy link
Copy Markdown
Contributor

I compared the 32-bit coordinate patch with its parent 59b8454 on RTX 5080 (621.14) and Microsoft WARP. For each adapter and build I compared 1,408 four-texel gathers against the CPU reference (widths 16,381 and 16,384; 11 sampler configurations; 32 lanes; with and without offsets). All 1,408 matched. The patch cleared the generated shader's Int64 feature flag; the wider A16/D16/unnormalized array fixture also passed.

One detail for reproducing boundary comparisons: at width 16,384, normalized u = f32::from_bits(0x3d800ff0), v = 0.25, wrap, level zero, the reference and both translated versions returned [33793, 33794, 17410, 17409]. Native gather returned [33792, 33793, 17409, 17408]. Both backends differed from the translated footprint in 28/108 native level-zero comparisons at this width, with the same differences before and after the change. At width 16,381 the corresponding counts were 0/108 for RTX and 7/108 for WARP. The old and new code agreed even at the neighboring f32 values on either side of the tested thresholds.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

This widens Lower::gather_at_level (crates/victory-shader/src/emit/lower.rs) from the title's transparent-black clamp-to-border case to every address mode, all three border colours, whole bias, a16, d16, unnormalized coordinates and 2D arrays: about 1,700 lines. The hand-written texel addressing it adds needs closer review than this kind of read can give: mirror-once, unnormalized coordinates on border modes, the null-SRV and slice clamps, and the high-half f16 whole_f16 analysis. It isn't mergeable as is:

  • Evidence and scope. Main's comment says the path covers only what the title's culling shaders use. Please say which title shaders (offset and sampler word, no code bytes) need which of these modes. Then keep only those, and leave the rest refused instead of emulated.
  • Split it. The second commit (null textures, v_lshl_or_b32 high-half validation, f16 inline constants) is separate from the first and would be easier to review on its own.
  • View docs in crates/victory-shader/src/emit/host.rs. The MippedArray doc and variant were inserted in the middle of the Levels doc comment ("whose texels in level" / "n are all n"). Also, the commit message says View::NullTexture is #[cfg(test)], but in the diff it isn't.

The host-comparison tests are useful. Please include the results of a run on hardware and WARP. CI: not run on this head.


Generated by Claude Code

@Dathinel

Copy link
Copy Markdown
Contributor

I tested the two suggested follow-ups together on Windows. The 32-bit footprint change and the packed LOD proof fix merged cleanly on 59b8454. With the test-host delta from #1887 (ccfb029..62bd980), all 7 tests passed with cargo test --locked -p victory-shader --lib synthetic_tests::gathers_at_a_level, including the 16,381-wide array and packed-LOD loop cases.

As a control, keeping the combined tests and restoring only lower.rs from the 32-bit patch made the repeated-operand regression fail: translation accepted the unknown upper half. The two fixes can be evaluated together; the coordinate change alone still needs the LOD fix for that case. These synthetic results leave the requested evidence about which modes the title uses open. AI-assisted validation.

@sinan-cmyk

Copy link
Copy Markdown

Offline measurement on a retail dump

Result: no count change against main.

I merged head 59b8454 onto main 940d32f (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 23,918 on main, 23,918 with this PR.

No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants