vkback: fix viewdesc CUBE_ARRAY/ArraySize clamping, SRV PlaneSlice stencil, and X8 default format #196

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

@⁠Mhyster on GitHub · 2026-12-02 07:11 UTC · original
Mhyster:fix/tex-viewdesc-cube-stencil-x8 → main · +52 −23 · 2 files · 1 commits · open

What

Fixes four D3D12-to-Vulkan texture view decoding bugs in greenfield/vkback/src/viewdesc.rs (range, components):

  1. Clamp CUBE_ARRAY NumCubes and handle 0 / u32::MAX (viewdesc.rs range): previously computed b.saturating_mul(6) unclamped, yielding layerCount = 0 when NumCubes == 0 (VUID-VkImageSubresourceRange-layerCount-01721), u32::MAX when NumCubes == u32::MAX (VUID-VkImageViewCreateInfo-subresourceRange-01483), or exceeding layers - first. Now clamps to whole cubes (layers - first) / 6 * 6 and treats 0 / u32::MAX as the remaining whole cubes.
  2. Handle ArraySize == 0 / u32::MAX as remaining layers (viewdesc.rs range): previously span(first, 0) returned 0 (layerCount = 0). Now matches rest and target by resolving 0 or u32::MAX to layers.saturating_sub(first).
  3. Honor 2D / 2D-array SRV PlaneSlice == 1 for stencil aspect (viewdesc.rs components): previously only checked stencil view formats (22 | 47 | 120), so single-channel stencil SRVs (e.g. R8_UINT with PlaneSlice == 1) bound DEPTH_BIT instead of STENCIL_BIT. Now selects STENCIL_ASPECT via offset_of!(Tex2dSrv, plane) / offset_of!(Tex2dArraySrv, plane) while keeping identity swizzle (stencil in R) for non-X*_G8 formats, and (ZERO, R, ZERO, ZERO) for 22 | 47 | 120.
  4. Inherit resource format when SRV Format == 0 (DXGI_FORMAT_UNKNOWN) (viewdesc.rs components, slotwrite.rs write_view): passes img.desc.format as fallback so default-format SRVs of B8G8R8X8_UNORM (88) / B8G8R8X8_UNORM_SRGB (93) still force alpha = ONE.

Reproduces D3D12 texture SRV/UAV subresource ranges, stencil plane selection, and X8 alpha semantics.

The reference

  • vkd3d-proton libs/vkd3d/resource.c:
    • vkd3d_setup_texture_srv_view (resource.c:7098, 7107, 7131-7140): sets aspect_mask = vk_image_aspect_flags_from_d3d12(resource->format, desc->Texture2D[Array].PlaneSlice) and handles VK_REMAINING_ARRAY_LAYERS before multiplying NumCubes * 6.
    • vk_image_aspect_flags_from_d3d12 (resource.c:2948-2967): maps PlaneSlice == 1 on a depth-stencil image to VK_IMAGE_ASPECT_STENCIL_BIT.
    • init_default_texture_view_desc (resource.c:5722-5732): resolves view_format == 0 from resource->desc.Format.
    • vkd3d_set_view_swizzle_for_format (resource.c:5583-5637): applies (ZERO, R, ZERO, ZERO) only for vk_aspect_mask == VK_IMAGE_ASPECT_STENCIL_BIT formats and forces a = ONE for B8G8R8X8_UNORM[_SRGB].
  • Guards kept: UAVs still receive no component swizzle (allowed_swizzle == false); PlaneSlice == 1 without a stencil-swizzle format selects STENCIL_ASPECT without overriding the R channel.

Evidence

  • Unit tests in greenfield/vkback/src/viewdesc.rs (components_follow_vkd3d, array_and_multisampled_dimensions_decode): cover CUBE_ARRAY clamping and 0/u32::MAX, ArraySize 0/u32::MAX, 2D and 2D-array PlaneSlice == 1 (R8_UINT keeps identity swizzle with STENCIL_ASPECT; X32_TYPELESS_G8X24_UINT keeps (ZERO, R, ZERO, ZERO)), and d[0] == 0 inheriting B8G8R8X8_UNORM / B8G8R8X8_UNORM_SRGB alpha ONE.
  • Local CI checks passed (Linux x86_64):
    • cargo test -p victory-shader -p victory-formats -p victory-core -p xtask --locked
    • (cd greenfield && cargo test -p umem -p frame -p vkback --lib --locked && cargo test -p vkback --test policy --locked) (168 passed, 3 ignored; 6/6 policy tests)
    • cargo clippy --all-targets --locked -- -D warnings on root native crates, greenfield, greenfield/wine, and greenfield/vkback/translate.
  • Not run: live game / AMD GPU screenshot check, Vulkan validation run (house scene), and A/B frame timing (frames.py) could not be run in this session (no live game run performed); cargo xwin clippy runs in GitHub Actions CI.

Checklist (docs/AGENTS.md)

  • One topic (rule 31); docs/TASKS.md not edited, the fixed item named above (rule 29)
  • "Allow edits by maintainers" on; after review, no force-push and no close-and-reopen (it drops the maintainers' commits)
  • No fallback or second path for a case that can't happen, in title data or in our own setup (rule 33); unexpected input fails loudly (rule 9)
  • Constants come from the console, the hardware or the title's data, not tuned by eye or ear (rule 9b)
  • Every switch is listed, read in one place, and a bad value stops the start (rule 2)
  • Queues and other externally synchronized Vulkan objects are used under their lock (rule 35)
  • Every unsafe block has a // SAFETY: comment; no raw pointer around the borrow checker (rule 36)
  • Clippy clean; tests call the production code; TRANSLATOR_VERSION bumped if the translator's output changes (rule 27)
  • Comments say what is true now and name functions, not line numbers (rule 3)
  • Nothing from the title, and no pointers to where non-free data can be found (hard constraints)
**@⁠Mhyster** on GitHub · 2026-12-02 07:11 UTC · [original](https://github.com/vktory6/VKtory/pull/196) `Mhyster:fix/tex-viewdesc-cube-stencil-x8` → `main` · +52 −23 · 2 files · 1 commits · open ## What Fixes four D3D12-to-Vulkan texture view decoding bugs in `greenfield/vkback/src/viewdesc.rs` (`range`, `components`): 1. **Clamp `CUBE_ARRAY` `NumCubes` and handle `0` / `u32::MAX`** (`viewdesc.rs` `range`): previously computed `b.saturating_mul(6)` unclamped, yielding `layerCount = 0` when `NumCubes == 0` (`VUID-VkImageSubresourceRange-layerCount-01721`), `u32::MAX` when `NumCubes == u32::MAX` (`VUID-VkImageViewCreateInfo-subresourceRange-01483`), or exceeding `layers - first`. Now clamps to whole cubes `(layers - first) / 6 * 6` and treats `0` / `u32::MAX` as the remaining whole cubes. 2. **Handle `ArraySize == 0` / `u32::MAX` as remaining layers** (`viewdesc.rs` `range`): previously `span(first, 0)` returned `0` (`layerCount = 0`). Now matches `rest` and `target` by resolving `0` or `u32::MAX` to `layers.saturating_sub(first)`. 3. **Honor 2D / 2D-array SRV `PlaneSlice == 1` for stencil aspect** (`viewdesc.rs` `components`): previously only checked stencil view formats (`22 | 47 | 120`), so single-channel stencil SRVs (e.g. `R8_UINT` with `PlaneSlice == 1`) bound `DEPTH_BIT` instead of `STENCIL_BIT`. Now selects `STENCIL_ASPECT` via `offset_of!(Tex2dSrv, plane)` / `offset_of!(Tex2dArraySrv, plane)` while keeping identity swizzle (stencil in R) for non-`X*_G8` formats, and `(ZERO, R, ZERO, ZERO)` for `22 | 47 | 120`. 4. **Inherit resource format when SRV `Format == 0` (`DXGI_FORMAT_UNKNOWN`)** (`viewdesc.rs` `components`, `slotwrite.rs` `write_view`): passes `img.desc.format` as fallback so default-format SRVs of `B8G8R8X8_UNORM` (88) / `B8G8R8X8_UNORM_SRGB` (93) still force `alpha = ONE`. Reproduces D3D12 texture SRV/UAV subresource ranges, stencil plane selection, and `X8` alpha semantics. ## The reference - `vkd3d-proton` `libs/vkd3d/resource.c`: - `vkd3d_setup_texture_srv_view` (`resource.c:7098, 7107, 7131-7140`): sets `aspect_mask = vk_image_aspect_flags_from_d3d12(resource->format, desc->Texture2D[Array].PlaneSlice)` and handles `VK_REMAINING_ARRAY_LAYERS` before multiplying `NumCubes * 6`. - `vk_image_aspect_flags_from_d3d12` (`resource.c:2948-2967`): maps `PlaneSlice == 1` on a depth-stencil image to `VK_IMAGE_ASPECT_STENCIL_BIT`. - `init_default_texture_view_desc` (`resource.c:5722-5732`): resolves `view_format == 0` from `resource->desc.Format`. - `vkd3d_set_view_swizzle_for_format` (`resource.c:5583-5637`): applies `(ZERO, R, ZERO, ZERO)` only for `vk_aspect_mask == VK_IMAGE_ASPECT_STENCIL_BIT` formats and forces `a = ONE` for `B8G8R8X8_UNORM[_SRGB]`. - Guards kept: UAVs still receive no component swizzle (`allowed_swizzle == false`); `PlaneSlice == 1` without a stencil-swizzle format selects `STENCIL_ASPECT` without overriding the R channel. ## Evidence - Unit tests in `greenfield/vkback/src/viewdesc.rs` (`components_follow_vkd3d`, `array_and_multisampled_dimensions_decode`): cover `CUBE_ARRAY` clamping and `0`/`u32::MAX`, `ArraySize` `0`/`u32::MAX`, 2D and 2D-array `PlaneSlice == 1` (`R8_UINT` keeps identity swizzle with `STENCIL_ASPECT`; `X32_TYPELESS_G8X24_UINT` keeps `(ZERO, R, ZERO, ZERO)`), and `d[0] == 0` inheriting `B8G8R8X8_UNORM` / `B8G8R8X8_UNORM_SRGB` alpha `ONE`. - Local CI checks passed (Linux x86_64): - `cargo test -p victory-shader -p victory-formats -p victory-core -p xtask --locked` - `(cd greenfield && cargo test -p umem -p frame -p vkback --lib --locked && cargo test -p vkback --test policy --locked)` (168 passed, 3 ignored; 6/6 policy tests) - `cargo clippy --all-targets --locked -- -D warnings` on root native crates, `greenfield`, `greenfield/wine`, and `greenfield/vkback/translate`. - **Not run**: live game / AMD GPU screenshot check, Vulkan validation run (house scene), and A/B frame timing (`frames.py`) could not be run in this session (no live game run performed); `cargo xwin clippy` runs in GitHub Actions CI. ## Checklist (docs/AGENTS.md) - [x] One topic (rule 31); `docs/TASKS.md` not edited, the fixed item named above (rule 29) - [x] "Allow edits by maintainers" on; after review, no force-push and no close-and-reopen (it drops the maintainers' commits) - [x] No fallback or second path for a case that can't happen, in title data or in our own setup (rule 33); unexpected input fails loudly (rule 9) - [x] Constants come from the console, the hardware or the title's data, not tuned by eye or ear (rule 9b) - [x] Every switch is listed, read in one place, and a bad value stops the start (rule 2) - [x] Queues and other externally synchronized Vulkan objects are used under their lock (rule 35) - [x] Every `unsafe` block has a `// SAFETY:` comment; no raw pointer around the borrow checker (rule 36) - [x] Clippy clean; tests call the production code; `TRANSLATOR_VERSION` bumped if the translator's output changes (rule 27) - [x] Comments say what is true now and name functions, not line numbers (rule 3) - [x] Nothing from the title, and no pointers to where non-free data can be found (hard constraints)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 07:13:55 +00:00
Author
Owner

@⁠vktory6 on GitHub · 2026-12-02 15:28 UTC · original

Thanks. This bundles four fixes (rule 31), so please split it. What to keep and what to change:

  • Keep: PlaneSlice == 1 selecting the stencil aspect (vkd3d vk_image_aspect_flags_from_d3d12), and view format 0 inheriting the resource format for the X8 alpha swizzle (vkd3d init_default_texture_view_desc).
  • Change: in the ranges, only UINT_MAX should mean "the rest", as in vkd3d (resource.c 7113 and 7144–7146).
    • ArraySize == 0 as "the rest" has no console source, so it should fail loudly (rules 6 and 9b).
    • Clamping NumCubes to whole remaining cubes repairs title data, and vkd3d doesn't do that either.
  • Show the cases are hit: a counter or one run that shows the title actually reaches them.
**@⁠vktory6** on GitHub · 2026-12-02 15:28 UTC · [original](https://github.com/vktory6/VKtory/pull/196#issuecomment-7350390701) Thanks. This bundles four fixes (rule 31), so please split it. What to keep and what to change: - **Keep:** `PlaneSlice == 1` selecting the stencil aspect (vkd3d `vk_image_aspect_flags_from_d3d12`), and view format 0 inheriting the resource format for the X8 alpha swizzle (vkd3d `init_default_texture_view_desc`). - **Change:** in the ranges, only `UINT_MAX` should mean "the rest", as in vkd3d (`resource.c` 7113 and 7144–7146). - `ArraySize == 0` as "the rest" has no console source, so it should fail loudly (rules 6 and 9b). - Clamping NumCubes to whole remaining cubes repairs title data, and vkd3d doesn't do that either. - **Show the cases are hit:** a counter or one run that shows the title actually reaches them.
This pull request has changes conflicting with the target branch.
  • greenfield/vkback/src/slotwrite.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/196/head:github-bot/gh-196'
git switch 'github-bot/gh-196'

Merge

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