GPU: align appended input-element offsets like D3D12 #618

Manually merged
github merged 1 commit from gh-pr/618/head into gh-pr/618/base 2026-11-26 04:10:19 +00:00
Owner

@⁠JFIREonFIRE on GitHub · 2026-11-24 05:43 UTC · original
JFIREonFIRE:fix/append-aligned-input-offsets → main · +72 −2 · 1 files · 1 commits · merged 2026-11-25 00:11 UTC

GraphicsState::elements() (crates/victory-gpu/src/pipeline/host.rs) works out each input element's byte offset. For D3D12_APPEND_ALIGNED_ELEMENT it used the end of the previous element as-is. D3D12 (and vkd3d-proton) start an appended element at the next multiple of min(4, its size), so after a 1- or 2-byte element (R8, R16_FLOAT, R8G8_UNORM...) the computed offset was too small:

Layout (slot 0) Before After / D3D12
R8G8_UNORM @⁠0, R32G32_FLOAT appended 0, 2 0, 4
R8_UNORM @⁠0, R16_FLOAT appended, R8_UNORM appended 0, 1, 3 0, 2, 4
R8G8B8A8_UNORM @⁠0, R32G32_FLOAT, R32G32B32_FLOAT appended 0, 4, 12 unchanged

The host pipeline gets the title's descriptions unchanged, so ordinary draws were never affected. The offsets computed here feed only draw.rs's rectangle-list expansion, which then read the fourth vertex's attributes from the wrong bytes for such layouts. I have not checked whether the title's rectangle-list pipelines use a layout like this; this is a correctness fix found by reading the code.

Also: an explicit AlignedByteOffset near 4 GiB overflowed offset + bytes (a panic in debug builds). It now saturates.

Tests

  • New pipeline::host::tests module: appended_elements_are_aligned and a_huge_explicit_offset_does_not_overflow. Both fail on main; both pass with this change.
  • cargo test --workspace passes on Windows 11 with an RTX 3090, including the hardware shader tests.
  • No game data used.

I searched open and closed PRs and issues for APPEND_ALIGNED, AlignedByteOffset and input-element offsets and found nothing touching this.

AI use: Claude Code (Claude Opus 5.5 / Fable 5.2) found the issue, wrote the change and tests, and ran them.

🤖 Generated with Claude Code

**@⁠JFIREonFIRE** on GitHub · 2026-11-24 05:43 UTC · [original](https://github.com/Marvisak/VIctory/pull/618) `JFIREonFIRE:fix/append-aligned-input-offsets` → `main` · +72 −2 · 1 files · 1 commits · merged 2026-11-25 00:11 UTC `GraphicsState::elements()` (`crates/victory-gpu/src/pipeline/host.rs`) works out each input element's byte offset. For `D3D12_APPEND_ALIGNED_ELEMENT` it used the end of the previous element as-is. D3D12 (and vkd3d-proton) start an appended element at the next multiple of **min(4, its size)**, so after a 1- or 2-byte element (`R8`, `R16_FLOAT`, `R8G8_UNORM`...) the computed offset was too small: | Layout (slot 0) | Before | After / D3D12 | |---|---|---| | `R8G8_UNORM` @⁠0, `R32G32_FLOAT` appended | 0, **2** | 0, 4 | | `R8_UNORM` @⁠0, `R16_FLOAT` appended, `R8_UNORM` appended | 0, **1**, **3** | 0, 2, 4 | | `R8G8B8A8_UNORM` @⁠0, `R32G32_FLOAT`, `R32G32B32_FLOAT` appended | 0, 4, 12 | unchanged | The host pipeline gets the title's descriptions unchanged, so ordinary draws were never affected. The offsets computed here feed only `draw.rs`'s rectangle-list expansion, which then read the fourth vertex's attributes from the wrong bytes for such layouts. I have not checked whether the title's rectangle-list pipelines use a layout like this; this is a correctness fix found by reading the code. Also: an explicit `AlignedByteOffset` near 4 GiB overflowed `offset + bytes` (a panic in debug builds). It now saturates. ### Tests - New `pipeline::host::tests` module: `appended_elements_are_aligned` and `a_huge_explicit_offset_does_not_overflow`. Both fail on `main`; both pass with this change. - `cargo test --workspace` passes on Windows 11 with an RTX 3090, including the hardware shader tests. - No game data used. I searched open and closed PRs and issues for `APPEND_ALIGNED`, `AlignedByteOffset` and input-element offsets and found nothing touching this. **AI use:** Claude Code (Claude Opus 5.5 / Fable 5.2) found the issue, wrote the change and tests, and ran them. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
`GraphicsState::elements()` placed an element with
`D3D12_APPEND_ALIGNED_ELEMENT` right after the previous one. D3D12 (and
vkd3d) start it at the next multiple of min(4, its size), so after a 1- or
2-byte element (R8, R16, R8G8...) the offset was too small. The host
pipeline gets the title's descriptions unchanged, so only the offsets
read here were wrong: the rectangle-list expansion read the fourth
vertex's attributes from the wrong bytes.

An explicit offset near 4 GiB also overflowed the next appended offset
(a panic in debug builds); it now saturates.

Tests: `appended_elements_are_aligned` and
`a_huge_explicit_offset_does_not_overflow` fail without the change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:10:17 +00:00
github manually merged commit 691236d606 into gh-pr/618/base 2026-11-26 04:10:19 +00:00
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-24 07:26 UTC · original

I ran a synthetic 4×4 rectangle through GraphicsState::elements and primitive::expand_rects, then rasterized and read it back in native D3D12. The last input follows a 16-bit element and needs to start at byte 12. Native APPEND_ALIGNED_ELEMENT and an explicit byte-12 layout produced identical pixels.

On d2a4bae, the CPU layout resolved to [0, 8, 10] and matched the native reference at 10/16 pixels. On 691236d, it resolved to [0, 8, 12] and matched all 16. The RTX 5090 and WARP agreed on both results, with zero D3D12 debug-layer errors. This covers layout resolution and rectangle expansion with actual rasterization.

**@⁠fuzeofficlal** on GitHub · 2026-11-24 07:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/618#issuecomment-7124994255) I ran a synthetic 4×4 rectangle through `GraphicsState::elements` and `primitive::expand_rects`, then rasterized and read it back in native D3D12. The last input follows a 16-bit element and needs to start at byte 12. Native `APPEND_ALIGNED_ELEMENT` and an explicit byte-12 layout produced identical pixels. On `d2a4bae`, the CPU layout resolved to `[0, 8, 10]` and matched the native reference at 10/16 pixels. On `691236d`, it resolved to `[0, 8, 12]` and matched all 16. The RTX 5090 and WARP agreed on both results, with zero D3D12 debug-layer errors. This covers layout resolution and rectangle expansion with actual rasterization.
Author
Owner

@⁠Yaekai on GitHub · 2026-11-24 16:03 UTC · original

Independent test & review on Windows 11 + NVIDIA RTX 3070 (Rust 1.98.1) at head eb9a10d:

  • cargo test -p victory-gpu --lib: 45 passed, 0 failed (including appended_elements_are_aligned and a_huge_explicit_offset_does_not_overflow).
  • cargo clippy -p victory-gpu -- -D warnings: 0 warnings.
  • Checked the alignment math (bytes.clamp(1, 4) with checked_next_multiple_of + saturating_add): matches D3D12's D3D12_APPEND_ALIGNED_ELEMENT rules cleanly with no overflow paths.

Looks clean and ready to merge!

**@⁠Yaekai** on GitHub · 2026-11-24 16:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/618#issuecomment-7137040285) Independent test & review on Windows 11 + NVIDIA RTX 3070 (Rust 1.98.1) at head `eb9a10d`: - `cargo test -p victory-gpu --lib`: 45 passed, 0 failed (including `appended_elements_are_aligned` and `a_huge_explicit_offset_does_not_overflow`). - `cargo clippy -p victory-gpu -- -D warnings`: 0 warnings. - Checked the alignment math (`bytes.clamp(1, 4)` with `checked_next_multiple_of` + `saturating_add`): matches D3D12's `D3D12_APPEND_ALIGNED_ELEMENT` rules cleanly with no overflow paths. Looks clean and ready to merge!
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!618
No description provided.