Skip to content

GPU: refresh texture views when shared host textures are replaced - #3288

Open
fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/gpu-refresh-replaced-texture-views
Open

fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/gpu-refresh-replaced-texture-views

Conversation

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Summary

When a title creates or mirrors a read-only texture view at a console memory address before a render target or unordered-access texture at the same address (MemoryKey) replaces the memory's HostTexture in resource::shared_texture, existing shader-visible descriptor heap slots and read-only sibling Resource objects stay permanently bound to the stale pre-replacement HostTexture. Bindless shaders (which do not rewrite already-populated heap slots) and subsequent resource operations therefore continue sampling stale zeroed texels instead of the GPU-rendered target.

Root Cause

  1. resource::shared_texture replaced the MEMORY map entry with the upgraded HostTexture, but never retargeted compatible read-only title-written sibling Resource handles or notified shader-visible descriptor heaps that the old HostTexture was superseded.
  2. descriptor::sync_heaps only re-mirrored slots whose descriptor memory pages were modified (sync_slots), leaving unmodified slots holding the old HostTexture stale indefinitely (including View::Placed slots already materialized via make_read and regular View::Shader slots).
  3. HostHeap::mirror's needs_remake check only checked whether the held ID3D12Resource changed for is_placed_texture views, ignoring non-placed View::Shader slots even when bound through a descriptor table (for_draw == true).
  4. bindless::uploads checked host_written but not !texture::title_written, which could upload stale title memory over a texture handle retargeted to a GPU-written HostTexture.

Fix

  • crates/victory-gpu/src/resource.rs:
    • Track replaced host texture raw pointers behind an atomic fast-path flag (HAS_REPLACED + REPLACED_TEXTURES, zero lock/scan cost when no texture is replaced).
    • In shared_texture, after releasing the MEMORY lock: if the superseded HostTexture is title-written and not host_written, retarget compatible sibling Resource objects at the same address and MemoryKey (using the same TEXTURES_AT -> r.host lock order as give_up/candidates) to the upgraded HostTexture. GPU-writable and host_written textures are left untouched on their owning Resource handles so rendered/copied contents are never discarded. Always record the replaced ID3D12Resource pointer for heap refresh.
  • crates/victory-gpu/src/descriptor.rs:
    • Drain replaced host textures in sync_heaps and call HostHeap::refresh_replaced on shader-visible view heaps (including heaps without page write-tracking).
  • crates/victory-gpu/src/descriptor/host.rs:
    • Add HostHeap::refresh_replaced to re-mirror only already-materialized title slots (0..count) still holding a replaced HostTexture (never eagerly materializing unmade View::Placed slots; retiring the old COM reference via keep).
    • Generalize HostHeap::mirror's needs_remake check to also cover non-placed texture View::Shader slots.
  • crates/victory-gpu/src/bindless.rs:
    • Skip CPU title-memory uploads in bindless::uploads when a tracked texture is no longer title_written.

Verification

  • Added 4 GPU tests (verified RED before fix on both hardware D3D12 and WARP, GREEN after fix with D3D12 Debug Layer enabled and zero debug messages):
    • bindless::tests::bindless_placed_srv_sees_render_target_that_replaces_its_host_texture
    • bindless::tests::bindless_resource_srv_sees_render_target_that_replaces_its_host_texture
    • descriptor::host::tests::non_placed_srv_remakes_on_table_mirror_after_host_texture_replacement
    • descriptor::host::tests::gpu_written_replaced_host_texture_stays_with_its_owner_while_placed_views_refresh
  • Passed full victory-gpu test suites on hardware D3D12 (default & --features diagnostics with Debug Layer) and WARP (--features diagnostics with Debug Layer), cargo clippy --workspace --all-targets (default & --all-features, -D warnings), git diff --check, and cargo run --locked -p xtask -- policy.

When a title creates or mirrors a read-only texture view at a console memory address before a render target or unordered-access texture at the same address replaces the memory's HostTexture in shared_texture, existing shader-visible descriptor heap slots and read-only sibling Resource handles stayed bound to the stale pre-replacement host texture. Bindless shaders and subsequent resource operations therefore kept reading stale zeroed texels instead of the GPU-rendered target.

Track replaced host textures in resource::shared_texture, retarget read-only title-written sibling resources whose HostTexture has not been written on the host to the upgraded HostTexture when compatible, refresh any shader-visible view heap slots still holding a replaced texture during descriptor::sync_heaps, generalize HostHeap::mirror's remake check to non-placed texture SRVs, and skip bindless CPU uploads once a texture has been upgraded to a GPU-written host texture.

@Dukestar69 Dukestar69 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.

Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090):

  • Tracking replaced host textures (note_replaced / take_replaced), retargeting read-only sibling title textures while keeping GPU-written ones bridged via alias_source, and refreshing affected descriptor heap slots and bindless views cleanly prevents stale SRV reads after shared host texture replacement.
  • cargo test -p victory-gpu --lib bindless:: -- --test-threads=1 (RTX 5090): 5 passed, 0 failed (including bindless_placed_srv_sees_render_target_that_replaces_its_host_texture and bindless_resource_srv_sees_render_target_that_replaces_its_host_texture).
  • cargo test -p victory-gpu --lib descriptor:: -- --test-threads=1 --skip dynamic_slots_are_held_until_released (RTX 5090): 23 passed, 0 failed (including gpu_written_replaced_host_texture_stays_with_its_owner_while_placed_views_refresh and non_placed_srv_remakes_on_table_mirror_after_host_texture_replacement).
  • cargo test -p victory-gpu --lib resource:: -- --test-threads=1 (RTX 5090): 18 passed, 0 failed.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

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

@oni-swr

oni-swr commented Nov 26, 2026 •

Copy link
Copy Markdown

WARP verification report (test report only, no code changes)

  • PR head: d24fd443
  • main: 63bcca43 (control); WARP software adapter forced with VICTORY_WARP=1 VICTORY_D3D12_WARP=1, --features diagnostics, d3d10warp.dll 10.0.26100.9818; dxil.dll 1.8.2502.11 (Windows Kit 10.0.26100.0, via VICTORY_DXIL). No physical GPU. Synthetic tests only, no game data.
  • Commands: cargo test -p victory-gpu --lib --features diagnostics --no-run -j 4, then the test binary with --exact <name> --test-threads 1 per test, then the whole lib suite.
  • New tests, all pass on the PR: bindless::tests::bindless_placed_srv_sees_render_target_that_replaces_its_host_texture, bindless::tests::bindless_resource_srv_sees_render_target_that_replaces_its_host_texture, descriptor::host::tests::non_placed_srv_remakes_on_table_mirror_after_host_texture_replacement, descriptor::host::tests::gpu_written_replaced_host_texture_stays_with_its_owner_while_placed_views_refresh.
  • Control: main plus only the four test bodies (and their test helpers; no source change). All four fail on main: the two bindless tests sample the old texture (dword 0: 4000, expected 4128 / 4192); the non-placed test fails the retargeted HostTexture pointer check; the placed-view test fails placed view slot refreshes to the upgraded HostTexture.
  • victory-gpu lib suite: PR 249 passed, 1 failed, 1 ignored; main plus the tests 5 failed (the 4 new tests and the failure below).
  • The one failure on the PR is frame::tests::waits_end_on_ticks_and_count_frames; it fails alone in its own process on the PR and on main, so it is not caused by this PR.
  • Not tested: physical GPU.

AI use: Claude Code (Claude Sonnet 5.5)

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

I checked this exact d24fd44 with the selective mip upload candidate from #1210. They conflict in the bindless upload gate. The integration 303628c keeps this PR's GPU-written texture check. CPU-fed textures that received a host copy reach the subresource refresh, which preserves any mip still backed by that copy.

All four replacement-view tests pass on verified app-local Agility 1.620.2/WARP 1.0.22. The selective mip test also passes all three runs with linear/packed textures and both watched and untracked memory. Full GPU diagnostics 252 passed, 0 failed, 1 ignored, including runtime identity. Strict GPU Clippy passed with default and all features, and policy passed.

This resolution is available if both changes are adopted. If folding it into another commit, please retain its Git author or add Co-authored-by: andrewpozdnakov7-jpg <andrewpozdnakov7-jpg@users.noreply.github.com>. AI-assisted with Codex.

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.

4 participants