Shaders: fix MIMG 3D Z texel offsets, a16 offset truncation, and cubemap sample_d LOD #353

Open
github-bot wants to merge 1 commit from github-bot/gh-353 into main AGit
Owner

@⁠AscilCH on GitHub · 2026-11-24 23:49 UTC · original
AscilCH:fix/shader-mimg-3d-offset-a16-and-cube-lod → main · +438 −33 · 3 files · 1 commits · open

Problem

Three MIMG sampling bugs in crates/victory-shader/src/emit/lower.rs:

  1. a16 truncates the offset word: Lower::image read the _o offset VGPR through addr_val(self, 0) (which masks to 16 bits when a16 is set) instead of addr_u32(self, 0), destroying bits [21:16] (the 3D Z offset) even though GFX10 offset words are always 32-bit.
  2. 3D Z texel offsets dropped & constant offset folding restricted to pixel op == 60: Offset decoding capped at offsets.min(2) (dropping Z in bits [21:16]), and folding constant offset words in -8..=7 into DXIL immediate constants only ran in the pixel op == 60 (sample.f32) arm — causing image_sample_lz_o (op == 62) and compute image_sample_o to emit non-immediate offset SSA values that dxil.dll rejects.
  3. Cubemap image_sample_d multiplies log2 by LOG2_E (1.4427x LOD bias): Lower::image called self.unary(23, texels) and multiplied by std::f32::consts::LOG2_E, assuming DXIL unary opcode 23 was ln. DXIL opcode 23 (Log, also used by v_log_f32) is already base-2 log2, so cubemap gradient samples shifted 1.4427× too high in the mip chain.

Fix

  • Read the offset word with addr_u32(self, 0)?, extract all offsets axes ([(0, 26), (1, 18), (2, 10)]), and fold constant offset words whose fields all lie in -8..=7 into immediate i32 constants once in the shared offset_word block before match op.
  • Remove the spurious LOG2_E multiply in the cubemap image_sample_d (63 if kind == 5) path.
  • Add #[cfg(test)] View::Texture3D and View::CubeMips helpers in emit::host and end-to-end tests mimg_3d_sample_offset_preserves_z_and_a16 (covering a16 = false and a16 = true with ±Z offsets) and mimg_cube_sample_d_selects_exact_log2_mip (measuring exact trilinear mip LODs 0.0, 1.0, 1.5, 2.0).

Verification

  • cargo test -p victory-shader --locked mimg_: passed (hardware D3D12 + WARP + dxil.dll)
  • cargo xtask policy: 169 files ok
  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings: clean
  • cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings: clean

(AI-assisted; synthetic tests only, no game data.)

**@⁠AscilCH** on GitHub · 2026-11-24 23:49 UTC · [original](https://github.com/fxceless/victory-fork/pull/353) `AscilCH:fix/shader-mimg-3d-offset-a16-and-cube-lod` → `main` · +438 −33 · 3 files · 1 commits · open ## Problem Three MIMG sampling bugs in `crates/victory-shader/src/emit/lower.rs`: 1. **`a16` truncates the offset word**: `Lower::image` read the `_o` offset VGPR through `addr_val(self, 0)` (which masks to 16 bits when `a16` is set) instead of `addr_u32(self, 0)`, destroying bits `[21:16]` (the 3D Z offset) even though GFX10 offset words are always 32-bit. 2. **3D Z texel offsets dropped & constant offset folding restricted to pixel `op == 60`**: Offset decoding capped at `offsets.min(2)` (dropping Z in bits `[21:16]`), and folding constant offset words in `-8..=7` into DXIL immediate constants only ran in the pixel `op == 60` (`sample.f32`) arm — causing `image_sample_lz_o` (`op == 62`) and compute `image_sample_o` to emit non-immediate offset SSA values that `dxil.dll` rejects. 3. **Cubemap `image_sample_d` multiplies `log2` by `LOG2_E` (`1.4427x` LOD bias)**: `Lower::image` called `self.unary(23, texels)` and multiplied by `std::f32::consts::LOG2_E`, assuming DXIL unary opcode `23` was `ln`. DXIL opcode `23` (`Log`, also used by `v_log_f32`) is already base-2 `log2`, so cubemap gradient samples shifted 1.4427× too high in the mip chain. ## Fix - Read the offset word with `addr_u32(self, 0)?`, extract all `offsets` axes (`[(0, 26), (1, 18), (2, 10)]`), and fold constant offset words whose fields all lie in `-8..=7` into immediate `i32` constants once in the shared `offset_word` block before `match op`. - Remove the spurious `LOG2_E` multiply in the cubemap `image_sample_d` (`63 if kind == 5`) path. - Add `#[cfg(test)]` `View::Texture3D` and `View::CubeMips` helpers in `emit::host` and end-to-end tests `mimg_3d_sample_offset_preserves_z_and_a16` (covering `a16 = false` and `a16 = true` with ±Z offsets) and `mimg_cube_sample_d_selects_exact_log2_mip` (measuring exact trilinear mip LODs `0.0, 1.0, 1.5, 2.0`). ## Verification - `cargo test -p victory-shader --locked mimg_`: passed (hardware D3D12 + WARP + `dxil.dll`) - `cargo xtask policy`: 169 files ok - `cargo clippy -p victory-shader --all-targets --locked -- -D warnings`: clean - `cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings`: clean *(AI-assisted; synthetic tests only, no game data.)*
Fix three MIMG texture-sampling bugs in `Lower::image`:

1. **a16 offset truncation**: `offset_word` read `addr_val(self, 0)` instead
   of `addr_u32(self, 0)`. Because `addr_val` masks to 16 bits when `a16`
   is set, bits `[21:16]` (the 3D Z offset) were zeroed on `a16` samples.
2. **Dropped 3D Z texel offset & non-sample-60 constant folding**: Offset
   extraction capped at `.min(2)`, silently dropping the Z offset (`bits
   [21:16]`) on 3D textures (`offsets == 3`). Furthermore, folding constant
   offset words in `-8..=7` to DXIL immediate constants only ran inside the
   pixel `op == 60` arm, leaving `image_sample_lz_o` (`op == 62`) and
   compute `image_sample_o` with non-immediate offsets rejected by `dxil.dll`.
   Move constant-offset folding into the shared `offset_word` block and
   extract all `offsets` axes (X, Y, Z).
3. **Cubemap `image_sample_d` LOD inflated by `LOG2_E` (`1.4427x`)**: DXIL
   unary opcode `23` (`dx.op.unary.f32(23, x)`, `Log`) is already base-2
   logarithm (`log2`), not natural log (`ln`). Multiplying its result by
   `LOG2_E` pushed every cubemap gradient sample ~1.44x higher in the mip
   chain.

Add `mimg_3d_sample_offset_preserves_z_and_a16` and
`mimg_cube_sample_d_selects_exact_log2_mip` (with `#[cfg(test)]`
`View::Texture3D` and `View::CubeMips` support in `emit::host`) verified
on hardware D3D12, WARP, and `dxil.dll`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:41:29 +00:00
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/host.rs
  • crates/victory-shader/src/emit/lower.rs
  • crates/victory-shader/src/emit/valu_tests.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/353/head:github-bot/gh-353'
git switch 'github-bot/gh-353'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-353'
git switch 'github-bot/gh-353'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-353'
git switch 'github-bot/gh-353'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-353'
git switch 'main'
git merge --squash 'github-bot/gh-353'
git switch 'main'
git merge --ff-only 'github-bot/gh-353'
git switch 'main'
git merge 'github-bot/gh-353'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.