GPU: validate index buffer format, alignment, and empty views before binding #3183

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

@⁠fuzeofficlal on GitHub · 2026-11-26 11:26 UTC · original
fuzeofficlal:fix/gpu-index-buffer-validation → main · +345 −15 · 2 files · 1 commits · open

Problem

In crates/victory-gpu/src/draw.rs, State::prepare_for forwarded self.index directly to shadow::find and ID3D12GraphicsCommandList::IASetIndexBuffer without checking D3D12's index buffer view rules:

  1. Invalid index formats: When SizeInBytes > 0, passing any format other than DXGI_FORMAT_R16_UINT or DXGI_FORMAT_R32_UINT (such as DXGI_FORMAT_UNKNOWN, R32_FLOAT, or R8_UINT) emits a D3D12 Debug Layer error:
    ID3D12CommandList::IASetIndexBuffer: The Format (...) is not valid for usage as an index buffer format.
  2. Unaligned BufferLocation: When SizeInBytes > 0, passing a host GPU address not aligned to 2 bytes (R16_UINT) or 4 bytes (R32_UINT) emits a D3D12 Debug Layer error:
    ID3D12CommandList::IASetIndexBuffer: Buffer address (...) is not aligned properly. The Format (...) imposes an alignment of (2|4) on the address.
  3. Empty (size == 0) and null-address (address == 0, size > 0) views: Both fell through to shadow::find(0, ...), which fell back to shadow::loose(0, ...) and allocated a 4 MiB host buffer around address 0 instead of clearing the host index buffer (IASetIndexBuffer(None)) for empty views or rejecting null-address non-empty views.

Fix

Extract State::bind_index (used by both State::draw_indexed and indexed State::execute_indirect through State::prepare_for):

  • When v.size == 0, call list.list.IASetIndexBuffer(None) and return true so valid empty/cleared index buffer draws continue to work without allocating a shadow buffer or leaving a stale index buffer bound.
  • When v.size > 0, reject v.address == 0, reject formats other than DXGI_FORMAT_R16_UINT (2-byte alignment) and DXGI_FORMAT_R32_UINT (4-byte alignment), and verify shadow.gpu(v.address).is_multiple_of(align) before refreshing and calling IASetIndexBuffer. Valid odd byte sizes (e.g., 3 bytes for R16_UINT, 6 bytes for R32_UINT) remain accepted as permitted by D3D12.

Verification

Added end-to-end D3D12 test draw::tests::prepare_validates_index_buffer_format_alignment_and_empty_views (plus #[cfg(all(test, windows))] helper Shader::register_test in pipeline.rs):

  • Before fix (origin/main): fails on both RTX 5090 + VICTORY_D3D12_DEBUG=1 and WARP + VICTORY_D3D12_DEBUG=1 with ID3D12CommandList::IASetIndexBuffer: The Format (0, UNKNOWN) is not valid for usage as an index buffer format. (and fails rejection of unaligned and null-address non-empty views).
  • After fix:
    • Targeted test passes on hardware default, hardware --features diagnostics + VICTORY_D3D12_DEBUG=1, and WARP --features diagnostics + VICTORY_D3D12_DEBUG=1 with host.log_messages() == None.
    • Full cargo test -p victory-gpu --lib --locked passes on hardware default (230 passed), hardware diagnostics + DEBUG=1 (244 passed), and WARP diagnostics + DEBUG=1 (246 passed).
    • cargo clippy --workspace --all-targets --locked -- -D warnings (default and --all-features) and xtask policy checks pass cleanly.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 11:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/3183) `fuzeofficlal:fix/gpu-index-buffer-validation` → `main` · +345 −15 · 2 files · 1 commits · open ### Problem In `crates/victory-gpu/src/draw.rs`, `State::prepare_for` forwarded `self.index` directly to `shadow::find` and `ID3D12GraphicsCommandList::IASetIndexBuffer` without checking D3D12's index buffer view rules: 1. **Invalid index formats**: When `SizeInBytes > 0`, passing any format other than `DXGI_FORMAT_R16_UINT` or `DXGI_FORMAT_R32_UINT` (such as `DXGI_FORMAT_UNKNOWN`, `R32_FLOAT`, or `R8_UINT`) emits a D3D12 Debug Layer error: `ID3D12CommandList::IASetIndexBuffer: The Format (...) is not valid for usage as an index buffer format.` 2. **Unaligned `BufferLocation`**: When `SizeInBytes > 0`, passing a host GPU address not aligned to 2 bytes (`R16_UINT`) or 4 bytes (`R32_UINT`) emits a D3D12 Debug Layer error: `ID3D12CommandList::IASetIndexBuffer: Buffer address (...) is not aligned properly. The Format (...) imposes an alignment of (2|4) on the address.` 3. **Empty (`size == 0`) and null-address (`address == 0, size > 0`) views**: Both fell through to `shadow::find(0, ...)`, which fell back to `shadow::loose(0, ...)` and allocated a 4 MiB host buffer around address `0` instead of clearing the host index buffer (`IASetIndexBuffer(None)`) for empty views or rejecting null-address non-empty views. ### Fix Extract `State::bind_index` (used by both `State::draw_indexed` and indexed `State::execute_indirect` through `State::prepare_for`): - When `v.size == 0`, call `list.list.IASetIndexBuffer(None)` and return `true` so valid empty/cleared index buffer draws continue to work without allocating a shadow buffer or leaving a stale index buffer bound. - When `v.size > 0`, reject `v.address == 0`, reject formats other than `DXGI_FORMAT_R16_UINT` (2-byte alignment) and `DXGI_FORMAT_R32_UINT` (4-byte alignment), and verify `shadow.gpu(v.address).is_multiple_of(align)` before refreshing and calling `IASetIndexBuffer`. Valid odd byte sizes (e.g., 3 bytes for `R16_UINT`, 6 bytes for `R32_UINT`) remain accepted as permitted by D3D12. ### Verification Added end-to-end D3D12 test `draw::tests::prepare_validates_index_buffer_format_alignment_and_empty_views` (plus `#[cfg(all(test, windows))]` helper `Shader::register_test` in `pipeline.rs`): - **Before fix (`origin/main`)**: fails on both RTX 5090 + `VICTORY_D3D12_DEBUG=1` and WARP + `VICTORY_D3D12_DEBUG=1` with `ID3D12CommandList::IASetIndexBuffer: The Format (0, UNKNOWN) is not valid for usage as an index buffer format.` (and fails rejection of unaligned and null-address non-empty views). - **After fix**: - Targeted test passes on hardware default, hardware `--features diagnostics` + `VICTORY_D3D12_DEBUG=1`, and WARP `--features diagnostics` + `VICTORY_D3D12_DEBUG=1` with `host.log_messages() == None`. - Full `cargo test -p victory-gpu --lib --locked` passes on hardware default (`230 passed`), hardware `diagnostics + DEBUG=1` (`244 passed`), and WARP `diagnostics + DEBUG=1` (`246 passed`). - `cargo clippy --workspace --all-targets --locked -- -D warnings` (default and `--all-features`) and `xtask` policy checks pass cleanly.
State::prepare_for previously forwarded self.index directly to shadow::find and ID3D12GraphicsCommandList::IASetIndexBuffer without validating the D3D12 index buffer view contract:

- Non-empty index views with a format other than DXGI_FORMAT_R16_UINT or DXGI_FORMAT_R32_UINT trigger D3D12 Debug Layer errors ('The Format (...) is not valid for usage as an index buffer format').
- Non-empty index views whose host BufferLocation is not 2-byte aligned (R16_UINT) or 4-byte aligned (R32_UINT) trigger D3D12 Debug Layer alignment errors.
- Empty views (size == 0) or null-address non-empty views (address == 0, size > 0) fell through to shadow::find, allocating a 4 MiB loose shadow buffer around address 0 instead of clearing the index buffer or rejecting the null view.

Extract State::bind_index to:
1. Clear the host index buffer with IASetIndexBuffer(None) when size == 0 (preserving valid empty/cleared index buffer draws without allocating a shadow copy).
2. Reject non-empty views with address == 0, formats other than R16_UINT / R32_UINT, or host GPU addresses not aligned to 2 / 4 bytes before calling IASetIndexBuffer.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 11:33:04 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 12:10 UTC · original · approved these changes

Verified on RTX 5090 against origin/main (63bcca4):

  • cargo test -p victory-gpu --lib draw:: -- --test-threads=1: 21/21 passed (including prepare_validates_index_buffer_format_alignment_and_empty_views).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly validates index buffer format, alignment, and empty views before binding.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 12:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/3183#pullrequestreview-6073893123) · **approved these changes** Verified on RTX 5090 against `origin/main` (`63bcca4`): - `cargo test -p victory-gpu --lib draw:: -- --test-threads=1`: 21/21 passed (including `prepare_validates_index_buffer_format_alignment_and_empty_views`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly validates index buffer format, alignment, and empty views before binding. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠oni-swr on GitHub · 2026-11-26 14:43 UTC · original

WARP verification of head 4b235220 (base = current main 63bcca4).

Setup: Windows 11, VICTORY_WARP=1 + VICTORY_D3D12_WARP=1, --features diagnostics (software adapter only, no physical GPU), Windows SDK dxil.dll, synthetic tests only.

  • cargo test -p victory-gpu --lib: 247 passed, 0 failed, 1 ignored
  • New test draw::tests::prepare_validates_index_buffer_format_alignment_and_empty_views: pass
  • Control (main + only this PR's test and its #[cfg(test)] register_test helper, no fix): fails (expected rejection for unknown_format)

Verified with Claude Code (Claude Sonnet 5.5).

**@⁠oni-swr** on GitHub · 2026-11-26 14:43 UTC · [original](https://github.com/Marvisak/VIctory/pull/3183#issuecomment-7195237832) **WARP verification** of head `4b235220` (base = current main `63bcca4`). Setup: Windows 11, `VICTORY_WARP=1` + `VICTORY_D3D12_WARP=1`, `--features diagnostics` (software adapter only, no physical GPU), Windows SDK `dxil.dll`, synthetic tests only. - `cargo test -p victory-gpu --lib`: **247 passed, 0 failed, 1 ignored** - New test `draw::tests::prepare_validates_index_buffer_format_alignment_and_empty_views`: **pass** - Control (main + only this PR's test and its `#[cfg(test)]` `register_test` helper, no fix): **fails** (`expected rejection for unknown_format`) Verified with Claude Code (Claude Sonnet 5.5).
Author
Owner

@⁠Jackster on GitHub · 2026-11-26 16:06 UTC · original · edited 2026-11-26 16:08 UTC

AMD check on a Radeon RX 7900 XTX (gfx1100, 32.0.32029.2010):

  • draw::tests::prepare_validates_index_buffer_format_alignment_and_empty_views passes on the hardware adapter.
  • Full victory-gpu suite: 232 passed / 0 failed / 1 ignored.

Synthetic only. AI-assisted.

**@⁠Jackster** on GitHub · 2026-11-26 16:06 UTC · [original](https://github.com/Marvisak/VIctory/pull/3183#issuecomment-7197194712) · edited 2026-11-26 16:08 UTC AMD check on a Radeon RX 7900 XTX (gfx1100, 32.0.32029.2010): - `draw::tests::prepare_validates_index_buffer_format_alignment_and_empty_views` passes on the hardware adapter. - Full `victory-gpu` suite: 232 passed / 0 failed / 1 ignored. Synthetic only. AI-assisted.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3183'
git switch 'github-bot/gh-3183'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3183'
git switch 'github-bot/gh-3183'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3183'
git switch 'main'
git merge --squash 'github-bot/gh-3183'
git switch 'main'
git merge --ff-only 'github-bot/gh-3183'
git switch 'main'
git merge 'github-bot/gh-3183'
git push origin 'main'
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!3183
No description provided.