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

Open
fuzeofficlal wants to merge 1 commit from upstream-pr/3183 into main
fuzeofficlal commented 2026-11-26 11:26:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T11:26:19Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6625642060 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3183). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T11:26:19Z. --- ### 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.
Dukestar69 commented 2026-11-26 12:10:03 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T12:10:03Z.

Review: APPROVED


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

<!-- greyfinch-sync:Marvisak/VIctory:review:6073893123 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3183#pullrequestreview-6073893123). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T12:10:03Z. **Review:** APPROVED --- 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).
oni-swr commented 2026-11-26 15:02:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-26T14:43:47Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7195237832 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3183#issuecomment-7195237832). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-26T14:43:47Z. --- **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).
Jackster commented 2026-11-26 16:06:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Jackster on 2026-11-26T16:06:39Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7197194712 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3183#issuecomment-7197194712). Originally posted by [Jackster](https://github.com/Jackster) on 2026-11-26T16:06:39Z. --- 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 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/3183:upstream-pr/3183'
git switch 'upstream-pr/3183'

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/3183'
git switch 'upstream-pr/3183'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3183'
git switch 'upstream-pr/3183'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3183'
git switch 'main'
git merge --squash 'upstream-pr/3183'
git switch 'main'
git merge --ff-only 'upstream-pr/3183'
git switch 'main'
git merge 'upstream-pr/3183'
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
Marvisak/VIctory!3181
No description provided.