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

Open
AscilCH wants to merge 1 commit from upstream-pr/353 into main
AscilCH commented 2026-11-24 23:49:53 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by AscilCH on 2026-11-24T23:49:53Z.


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

<!-- greyfinch-sync:fxceless/victory-fork:issue:6593804818 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/353). Originally posted by [AscilCH](https://github.com/AscilCH) on 2026-11-24T23:49:53Z. --- ## 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.)*
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/353:upstream-pr/353'
git switch 'upstream-pr/353'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/353'
git switch 'upstream-pr/353'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/353'
git switch 'upstream-pr/353'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/353'
git switch 'main'
git merge --squash 'upstream-pr/353'
git switch 'main'
git merge --ff-only 'upstream-pr/353'
git switch 'main'
git merge 'upstream-pr/353'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
fxceless/victory-fork!353
No description provided.