Skip to content

GPU: share live host textures across aliased addresses and canonicalize view descriptors - #643

Closed
nathanbzrr wants to merge 3 commits into
Marvisak:mainfrom
nathanbzrr:gpu/alias-live-host-textures
Closed

nathanbzrr wants to merge 3 commits into
Marvisak:mainfrom
nathanbzrr:gpu/alias-live-host-textures

Conversation

@nathanbzrr

Copy link
Copy Markdown
Contributor

Summary

Two texture aliasing and view deduplication bugs cause aliased render targets and placed SRVs to read back zeros and leak duplicate host resources/views:

  1. hand_out_resource creates a fresh all-zero HostTexture even when a live Resource at the same virtual address already owns the rendered GPU copy. Downstream CreatePlacedShaderResourceViewX (texture_at) or aliased CreatePlacedResource* calls then see flags == 0, causing texture::title_written to overwrite the separate host copy with CPU zeros and isolating state tracking.
  2. ResourceDesc, D3D12_SHADER_RESOURCE_VIEW_DESC, and D3D12_UNORDERED_ACCESS_VIEW_DESC contain struct padding words and unused union tail words; uncanonicalized stack bytes break PLACED_TEXTURES and VIEWS.index deduplication.

Changes (crates/victory-gpu/src/resource.rs, crates/victory-gpu/src/descriptor.rs)

  • Add canonical_resource_desc (zeroing offsets 4..8 and 52..56), canonical_srv_desc (zeroing d[3] alignment padding and per-ViewDimension unused union words), canonical_uav_desc, and canonical_placed_desc, verified with compile-time offset_of! / size_of! assertions against windows-0.62.2.
  • Apply canonicalization in texture_at, create_srv, create_uav, create_placed_srv, and create_placed_uav while preserving placed desc[10..16].
  • Add a Windows LIVE_TEXTURES VA registry (TextureKey, LiveTexture, find_live_texture, register_live_texture, unregister_live_texture) in resource.rs:
    • hand_out_resource reuses a compatible live Arc<HostTexture> at the same VA (preferring GPU-writable targets, enforcing geometry/family, usage-flag, typed-depth SRV, and non-typeless writable format compatibility) without resetting HostTexture::state.
    • Drop for Resource unregisters dropped resources from LIVE_TEXTURES.
    • texture_at upgrades non-writable PLACED_TEXTURES hits if a compatible live GPU-writable HostTexture becomes active at that VA (without freeing prior This pointers, maintaining PLACED_TEXTURES -> LIVE_TEXTURES lock order).

Verification

Isolated verification on branch gpu/alias-live-host-textures (5cab0f1):

  • rustfmt --edition 2024 --config skip_children=true --check crates/victory-gpu/src/resource.rs crates/victory-gpu/src/descriptor.rs (passed)
  • cargo test -p victory-gpu -- --test-threads=1 (46 passed, 0 failed, 1 ignored; includes canonicalization_deduplicates_padding_and_unused_union_words, placed_srv_and_aliased_resource_see_rendered_target_pixels, and incompatible_alias_flags_or_geometry_do_not_share_host_texture)
  • VICTORY_D3D12_WARP=1 VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --features diagnostics -- --test-threads=1 (48 passed, 0 failed, 1 ignored; zero D3D12 debug layer errors)
  • cargo clippy -p victory-gpu --all-targets (0 warnings in resource.rs and descriptor.rs; 1 pre-existing warning at host.rs:267)
  • cargo xtask policy (151 files ok)

No game files. AI-assisted; reviewed and verified with the tests above.

@LAKSHYA-cmd-tech LAKSHYA-cmd-tech left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: float-to-unorm rounding variance in placed_srv_and_aliased_resource_see_rendered_target_pixels

Tested on Windows 11 with Rust 1.98.1 (x86_64-pc-windows-msvc).

1. Test Failure

Running cargo test -p victory-gpu failed in
esource::tests::placed_srv_and_aliased_resource_see_rendered_target_pixels:
ext thread 'resource::tests::placed_srv_and_aliased_resource_see_rendered_target_pixels' panicked at crates\victory-gpu\src\resource.rs:2324:21: assertion left == right failed: pixel (0, 0) left: [64, 127, 191, 255] right: [64, 128, 191, 255]

2. Root Cause

In placed_srv_and_aliased_resource_see_rendered_target_pixels:
ust let clear = [0.25f32, 0.5, 0.75, 1.0];
The second channel (�.5f32) maps to �.5 * 255.0 = 127.5 in R8G8B8A8_UNORM. Depending on host GPU hardware / driver truncation vs round-to-nearest-even, 127.5 can read back as 127 rather than 128 (here on NVIDIA it produces 127).

3. Suggested Fix

Allow a tolerance for the middle channel or test with exact boundaries, e.g.:
ust assert_eq!(row[x * 4], 64); assert!(row[x * 4 + 1] == 127 || row[x * 4 + 1] == 128, "green: {}", row[x * 4 + 1]); assert_eq!(row[x * 4 + 2], 191); assert_eq!(row[x * 4 + 3], 255);

4. Architecture & Implementation

The canonicalization (canonical_resource_desc, canonical_srv_desc, etc.) and the VA registry in LIVE_TEXTURES for reusing compatible GPU-writable HostTexture across aliased addresses is very clean and properly maintains PLACED_TEXTURES -> LIVE_TEXTURES lock ordering.

@nathanbzrr

nathanbzrr commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @LAKSHYA-cmd-tech for catching the 0.5 * 255.0 = 127.5 float-to-UNORM rounding variance!

Updated clear in placed_srv_and_aliased_resource_see_rendered_target_pixels (commit e7b2182) to [64.0f32 / 255.0, 128.0 / 255.0, 191.0 / 255.0, 1.0] so all channels hit exact integers regardless of hardware rounding vs truncation on 0.5 ties.

Re-verified on branch gpu/alias-live-host-textures:

  • cargo test -p victory-gpu -- --test-threads=1 (46 passed, 0 failed, 1 ignored)
  • VICTORY_D3D12_WARP=1 VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --features diagnostics -- --test-threads=1 (48 passed, 0 failed, 1 ignored)
  • cargo clippy -p victory-gpu --all-targets (0 warnings in resource.rs)
  • cargo xtask policy (151 files ok)

@YouKnowWh

Copy link
Copy Markdown

Tested 5cab0f1 on Windows 11 with an RTX 5070 Ti (driver 621.14), using synthetic textures in a diagnostics build.

I used two identical 4×4 placed textures in one guest heap, initialized B to a sentinel, uploaded a known pattern to A, and read 64 bytes back from B. This comparison checks host backing identity without issuing an aliasing barrier. On the d2a4bae + #322 baseline, B retained its sentinel at the same offset. On this PR it returned all 64 bytes of A's pattern for both R10G10B10A2_UNORM and R8G8B8A8_UNORM. The different-offset control retained the sentinel, and the same-object control returned the pattern on both versions. The incompatible-alias test also passed on the PR.

One test portability finding: the unchanged assertion in placed_srv_and_aliased_resource_see_rendered_target_pixels failed at pixel (0,0): got [64,127,191,255], expected [64,128,191,255]. Changing only the clear to [1.0,0.0,1.0,1.0] and the expected bytes to [255,0,255,255] made the full 16×16 test pass on the same machine. Endpoint colors keep the exact equality check while avoiding the midpoint conversion in this aliasing test.

This verifies the synthetic case. We haven't established that the two textures seen during gameplay overlap in guest memory. AI-assisted test setup and reporting.

@nathanbzrr

Copy link
Copy Markdown
Contributor Author

Thanks @YouKnowWh for verifying aliased host backing identity on RTX 5070 Ti across both R10G10B10A2_UNORM and R8G8B8A8_UNORM and the controls!

Note that commit e7b2182 (pushed right before your comment on 5cab0f1) already updated clear in placed_srv_and_aliased_resource_see_rendered_target_pixels from [0.25, 0.5, 0.75, 1.0] to exact [64.0f32 / 255.0, 128.0 / 255.0, 191.0 / 255.0, 1.0] so the green channel lands on an exact integer regardless of driver 0.5-tie rounding vs truncation.

@DeerSilver

Copy link
Copy Markdown

Tested the current head e7b2182e5 (with the exact n / 255.0 clear) merged onto main ffe310e (clean merge) on NVIDIA GeForce RTX 5060 Laptop GPU (Blackwell), driver 621.24, Windows 11 26300, Rust 1.98.1: pass. The earlier RTX 5070 Ti check was of 5cab0f1. No game files: the test suite only.

  • The PR's three tests pass:
    • canonicalization_deduplicates_padding_and_unused_union_words
    • placed_srv_and_aliased_resource_see_rendered_target_pixels
    • incompatible_alias_flags_or_geometry_do_not_share_host_texture
  • The two resource::tests cases return early without a host GPU. Here they ran on the RTX 5060 (DXGI adapter 0 on this laptop). So the rendered-target pixels read through the placed SRV and the aliased resource match exactly on NVIDIA with the new clear values.
  • cargo test --workspace --locked: 345 passed, 0 failed, 6 ignored, against 342 / 6 on main at the same commit.
  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean.

AI-assisted (Claude Code).

@rpmaster2

Copy link
Copy Markdown

@nathanbzrr an AMD data point for this PR, because it changes what reaches the screen and I have not seen RDNA4 numbers in this thread.

I tested main @ffe310e with #878 + #585 + this PR + #581 stacked (all four merge cleanly; one semantic fix needed: #581 still says UNORDERED where this PR renamed it UNORDERED_FLAG). Diag build, VICTORY_D3D12_DRED=1, RX 9070 XT, Adrenalin 26.11.2, one 600 s run.

What changed on screen for the first time with this stack:

  • the "Rockstar Games presents" card appears at all;
  • subtitle text is sharp and, more telling, stops drawing on top of itself - before the stack the same cutscene piled subtitle lines into an unreadable smear at the same frame rate, so that was the wrong texture winning an alias, not a missing clear;
  • animal silhouettes (snake, crocodile) and a blue/red line below centre appear in the cutscene.

Stability is unchanged: the run still lost the device at frame 1349 with the same page fault ... inside freed ["texture 1024x1024 f98 ..."] seen on plain main (that is #597's removal, not this PR's).

One number that may matter to you: in a separate 240 s run with the draw trace on, exactly 20 slots were bound as null views because make found "no host texture and not a buffer" for the view's resource - zero fits refusals, zero texture_at misses, and zero host-texture creation failures in the log. If the alias work is meant to make a view of a re-aliased address resolve to the live host texture, those 20 are the ones that still do not.

Not tested: NVIDIA, other titles, cargo test on the stack. This is a game-run observation, not a correctness claim.

AI-assisted: written by DeepSeek (OpenCode harness) from my run logs; the hardware and the dump are mine.

nazmi pushed a commit to nazmi/VIctory that referenced this pull request Nov 24, 2026
Applied as the net diff of upstream PR Marvisak#643 (head e7b2182), 2 commits: e7b2182,5cab0f15.
simnJS added a commit to MiamiGame/XTP that referenced this pull request Nov 24, 2026
…view; read-backs wait under async submission

dark-world.md: section 6 lists what a world capture must show (the pass
writing the 4K output, #841; aliases sharing; planes; host texture
residency) with the exact capture command; section 8 is the critical
review of #643, #211, #469/#206 and the #492 port. Claims the captures do
not prove yet are written as such.

Tests' texture read-back drains the host queue (#595 made execute
asynchronous).

The alias sharing in resource/alias.rs and resource.rs (commit 2775f7f and
after) is ported from Marvisak/VIctory#643 (head e7b2182) by nathanbzrr,
reworked (see dark-world.md section 8.1).
The plane composition in dxgi/planes.rs (commit 2775f7f and after) is
ported from Marvisak/VIctory#211 (head 24a387d) by Yaekai, reworked (see
dark-world.md section 8.2).

Co-authored-by: nathanbzrr <nbezerradev@gmail.com>
Co-authored-by: Yaekai <21324379+Yaekai@users.noreply.github.com>
@nathanbzrr
nathanbzrr force-pushed the gpu/alias-live-host-textures branch from e7b2182 to 23aace6 Compare November 24, 2026 21:39
@nathanbzrr

Copy link
Copy Markdown
Contributor Author

Thanks @rpmaster2 for the RX 9070 XT in-game report and for noting the constant rename against #581!

Updated branch gpu/alias-live-host-textures (head 23aace6, rebased onto main ffe310e):

  • Restored the DEPTH, UNORDERED, and DENY_SHADER_RESOURCE constant names in crates/victory-gpu/src/resource.rs so #581 merges cleanly without semantic rename friction.
  • Updated the initial texture_at miss path so a Resource with host.is_none() is released (resource_release(out)) and not inserted into PLACED_TEXTURES, preventing permanent caching of failed host-texture creations.

About those 20 "no host texture and not a buffer" slots in descriptor/host.rs::make: with zero host-texture creation failures and zero texture_at misses, make's View::Shader / View::Unordered fallback hits when resource == 0 (explicit null-resource CreateShaderResourceView(NULL, ...) / CreateUnorderedAccessView(NULL, ...) calls, which also don't go through placed_texture) or a buffer view has desc == None, rather than a missed live-texture alias.

Re-verified on 23aace6 (Windows MSVC):

  • rustfmt --edition 2024 --check crates\victory-gpu\src\resource.rs: clean
  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean
  • cargo test --workspace --locked: 345 passed, 0 failed, 6 ignored (victory-gpu: 53 passed, 0 failed, 1 ignored; xtask: 4 passed)

@Gargalindis

Copy link
Copy Markdown

AI-assisted (Claude Code). RTX 5080, Windows 11. A data point from the game for this PR; I have read the PR's code at e7b2182 and have not run it.

An alias pair this PR's key leaves separate: resources written as R32_UINT and read by address as R9G9B9E5_SHAREDEXP.

While tracing why the world's light buffers read zero, I added a warning for the case this PR is about: a CreatePlacedShaderResourceViewX view (texture_at) naming an address where a texture the host GPU renders or writes already lives. In the opening world scene it fires for 50 textures in one run (48 in another), and every one has the same shape:

  • the title resource at the address is format 42 (R32_UINT) with ALLOW_UNORDERED_ACCESS (a few also ALLOW_RENDER_TARGET), written by compute passes;
  • the view by address names the same address and the same size with format 67 (R9G9B9E5_SHAREDEXP).

So the title packs shared-exponent colours into a uint texture from compute and samples the same memory as R9G9B9E5. Sizes seen: ten at 2560x1440, 1280x720, 2048x456, 2048x512, 2304x576, mip-like chains from 640x360 down to 40x23, 128x128x6 and 128x128x8, and five 80x45x64 volumes. Among the readers are the tiled-lighting pixel stages (two full-resolution ones each) and the scattering passes.

Why I think this PR does not share them: texture_key compares family(pc_format(desc.format)), and family(42) is 39 while family(67) is 67 (not in FAMILIES), so find_live_texture returns nothing and the view keeps a separate host texture fed from title memory. Sharing one host texture would not work for this pair anyway, since no typeless family holds both formats.

What I tried as an experiment on my own tree (not on this branch): before a list samples such a view, CopyResource from the R32_UINT host texture into the R9G9B9E5 one. The Direct3D 10.1 format-conversion table lists R32_UINT/R32_SINT with R9G9B9E5_SHAREDEXP as a copy that reinterprets the bits. On this card that ran over a thousand copies across 35 addresses with no device removal. I have no evidence yet that it changes the picture: 47 of the 50 source textures are themselves zero in my runs because the passes that fill them are dropped or produce zero, so treat the copy as untested for correctness.

Two questions, if useful to you:

  1. Would you want this pair handled in this PR (a registry lookup that tolerates the 42/67 pair and returns "copy from this live texture" in place of "share it"), or left for a follow-up on top of it? I am glad to send the follow-up.
  2. Does anyone know whether relaxed format casting permits an R9G9B9E5 shader view on an R32_UINT resource? The pages I read do not say, which is why I copied.

@nathanbzrr

Copy link
Copy Markdown
Contributor Author

Closing this PR as superseded by recent main (70564ae):

  • 13bbd6f (GPU: one host texture per texture memory, and writable twins for depth textures, decision 0007) now shares one host texture per texture memory across resource objects and placed views via MemoryKey / MEMORY / shared_texture, uses CreateCommittedResource3 castable format lists, adds writable twins for depth+UAV textures (twin.rs), and already clears padding words 1 and 13 in texture_at.
  • 683ad7a bridges textures sharing memory under differing descriptions via host copies.

Thank you @rpmaster2 for the earlier review feedback! The small canonical_srv_desc/canonical_uav_desc padding-zeroing helper from crates/victory-gpu/src/descriptor.rs can be submitted as a standalone follow-up PR on main if descriptor deduplication still needs it.

@nathanbzrr nathanbzrr closed this Nov 24, 2026
@nathanbzrr
nathanbzrr deleted the gpu/alias-live-host-textures branch November 25, 2026 00:32
geiyiliyabumo pushed a commit to geiyiliyabumo/VIctory that referenced this pull request Nov 25, 2026
…st-76-111a

GPU: test alias view over compute-written texture (Marvisak#76)
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.

6 participants