Skip to content

GPU: copy unaligned buffer SRVs into element-aligned scratch memory - #986

Closed
CitizenCinema wants to merge 1 commit into
Marvisak:mainfrom
CitizenCinema:fix/unaligned-buffer-srv
Closed

CitizenCinema wants to merge 1 commit into
Marvisak:mainfrom
CitizenCinema:fix/unaligned-buffer-srv

Conversation

@CitizenCinema

Copy link
Copy Markdown

Root cause

Titles sub-allocate placed structured buffer SRVs at 16-byte-aligned addresses inside much larger shared title buffers (e.g., 182 MB transient ring buffers). Because a D3D12 D3D12_BUFFER_SRV addresses elements via FirstElement in multiples of StructureByteStride, descriptor/host.rs::buffer_srv() cannot point directly into the shared shadow buffer when shadow.offset(address) % element != 0.

Previously, buffer_srv() logged once ("buffer view not on an element of its host copy") and returned None, causing null_for() to write a null_buffer_view (all zeros) into the host descriptor slot. In GTA VI's vfx_proj_decal_hit_detection (eeed82b877b44b5f), buffer B (112 B, StructureByteStride = 56) landed at shadow offsets with offset % 56 != 0 (24 / 48), so B silently read all zeros (base = 0), pointing A into the overlapping descriptor heap (0x45495676 tags) and causing a ~4.8 s DXGI_ERROR_DEVICE_HUNG TDR even for element 0 (u6 = 0).

Fix

  • Added HostList::scratch_aligned(len, align) in crates/victory-gpu/src/host.rs (sharing scratch_in with scratch() while preserving 256-byte alignment for CBVs).
  • Added unaligned_srv(&View) and HostHeap::srv_at() in crates/victory-gpu/src/descriptor/host.rs to detect View::Shader / View::Placed buffer SRVs whose shadow offset is not divisible by their element stride, clamping NumElements to the buffer's declared length.
  • Updated draw.rs::tables() to promote descriptor tables containing an unaligned buffer SRV into fresh dynamic slots (keeping scratch buffer lifetimes tied to the command list, matching View::Constant), defer-copy their bytes into element-aligned scratch memory at submission, and bind an SRV with FirstElement = at / align.
  • Added unit test unaligned_buffer_srv_is_detected in crates/victory-gpu/src/descriptor/host.rs.

Verification

  • cargo test -p victory-gpu: 51 passed, 0 failed (including unaligned_buffer_srv_is_detected) and cargo test -p victory-shader: 120 passed, 0 failed.
  • Combined with the companion StructuredBuffer lowering fix (fix/structured-buffer-flat-offset), GTA VI ran live on RTX 5090 for 8m19s with no VICTORY_SKIP_PIPELINES: 0 slow submissions, 0 GPU hangs, 0 "buffer view not on an element of its host copy" SRV warnings, steady 2.0–2.2 fps (eeed82b877b44b5f running cleanly every frame past frame 1114; stopped cleanly by hand).
  • Note: unaligned buffer UAVs (e.g., element = 44) are left for a separate follow-up.

Placed structured buffer SRVs sub-allocated inside a larger title buffer can land at shadow offsets not divisible by StructureByteStride (e.g. offset % 56 != 0 in GTA VI hit detection). buffer_srv() rejected these views, leaving null_buffer_view (zeros) in the host descriptor slot. Detect unaligned buffer SRVs, promote their descriptor table to fresh dynamic slots, copy their bytes at submission into an element-aligned upload scratch slice, and bind an SRV with FirstElement = at / element.
@MichaelSmithAI

MichaelSmithAI commented Nov 24, 2026 •

Copy link
Copy Markdown

A guest command-list readback isolates #986’s staging behavior on RTX 4090 under Wine/vkd3d-proton 3.2. Two dispatches used the same table for a two-record, 56-byte-stride structured SRV. The fixture checked that only one dynamic slot was allocated. After closing the list and before submitting it, the CPU changed fields in both records. GPU readback then matched the new field pairs [17, 31] and [41, 59]. The control passed on the author revision and on our gstack + #986 build. On the matched ffe310e baseline, the same view allocated zero staged slots. The fixture stopped at that assertion before queue submission, so there is no baseline GPU readback.

This verifies submission-time staging of CPU-written records. A GPU producer earlier in the same submission remains a separate case. The reported 8m19s title result used #986 together with #984, so the title effect of the staging change alone remains open.

@Zipettte

Copy link
Copy Markdown

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at d8d7c2c: cargo test -p victory-gpu passes, 51 passed / 0 failed. Workspace baseline on main (ffe310e) was green on the same machine (342 passed).

Tested with AI assistance (Claude).

MfDev0 pushed a commit to MfDev0/victory that referenced this pull request Nov 24, 2026
A structured buffer SRV placed inside a larger title buffer can start
off an element of its host copy (offset % stride != 0); FirstElement
cannot address it, so the view was left null and read zeros (upstream
traced a 4.8 s DEVICE_HUNG to it). Such a view's table goes to fresh
dynamic slots, its elements are copied at submission into upload scratch
memory aligned to the element, and the slot views that.

shortcut: copied from title memory, so GPU writes earlier in the same
submission are not seen.

From upstream PR Marvisak#986 (d8d7c2c), hand-ported.

Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.com>
@SantannaCarlos

Copy link
Copy Markdown
Contributor

The same placed views also come as unordered views, of 44-byte elements: in a 10-minute world run on an RTX 4070, 315 such ranges, all left null on main. #1019 handles both kinds with one copy per view range (an unordered view's writes go back through the copy-back), in case that saves duplicate work on the follow-up you mention.

@justlovett0

Copy link
Copy Markdown

Verified on Windows 10 + RTX 3080 — this stack (with #1260 and #1267) eliminated the world device removal and took the world from ~1 fps to ~20 fps.

Setup: Win10 21H2 19044.7846, RTX 3080 10 GB, driver 621.24 (32.0.16.2124), Ryzen 9 7900X, 32 GB. Build: main ffe310e + #175 + #994 + #302 + #193 + #704 + #309 + #1067 + #953 + this PR + #1260 + #1267 + #1254, release+diagnostics. Retail GTA VI 1.0.2.1, Series X. VICTORY_DEBUG_PS=1 VICTORY_SHOW_TARGET=2560x1440-f28-3.

before copy fixes + #986/#1260/#1267
world fps ~0.9–1.6 20.0
copies per frame 270–400 MB, ~400 ms 0.4 MB, ~20 ms
device removal every run, ~2–3 min in (24–33-list submissions) none in a ~2.3 min world run

This matches the GTT-fill mechanism described here: the unaligned SRV scratch copy storm (#1260) and the re-copied 174 MB heap union (#1267) were both the frame-rate cost and the timed removal. With them gone the world runs continuously until I closed it (the exit was a teardown segfault, not a GPU fault).

Cross-ref: #1260 (one aligned copy per range per list), #1267 (each declared byte copied once per submission). The three together are what produced the result; I can't isolate each one's share from this run, but the combined effect is unambiguous.

AI use: Qwen Code (underlying model name not disclosed by the tool) built the stack, ran the title, extracted the counts and drafted this comment at the user's request.

@alladeen321123

Copy link
Copy Markdown

I compared three diagnostics builds on WARP with D3D12 debug, GPU-based validation and DRED enabled, using the same title and scripted Player input. Clean ffe310e logged 13 occurrences of validation ID 940 across four pixel shaders (heap index 0, expected Texture2D, actual Texture1D). This PR alone at d8d7c2c logged zero. Adding #1260 and #1267 also logged zero. All three runs still reached device removal 0x887A0005. The three pixel pipelines we tracked were present in submission summaries on both patched runs. These are log occurrences from one run per build.

A separate targeted experiment on #568 helps explain the difference for pixel 280967ad158cee0b. Its placed structured SRV at table slot 1, byte offset 0 has a 304-byte stride. Three observed shadow offsets had remainders of 112, 208 and 208 bytes; host view creation fell back to null in all three cases. Giving only that shader an aligned, submission-populated copy removed its ID 940 messages in a repeat while other pixel shaders continued reporting them. This supports the SRV fix for this path; the later WARP removal remains open.

@Marvisak

Copy link
Copy Markdown
Owner

Automated triage for the maintainers. Closing because this change is already in main, or main has since replaced the code it patches.

Unaligned structured SRVs get an own copy in main (shadow::for_view / exact, held by the slot, commit 86a5ed6); the per-draw scratch copy here was the costlier variant (#1260 measured 35 GB of copies).

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants