Skip to content

victory-gpu: refresh and route external host textures through one copy - #2726

Open
mgunes1311-lab wants to merge 6 commits into
Marvisak:mainfrom
mgunes1311-lab:gpu-external-host-texture
Open

mgunes1311-lab wants to merge 6 commits into
Marvisak:mainfrom
mgunes1311-lab:gpu-external-host-texture

Conversation

@mgunes1311-lab

Copy link
Copy Markdown
Contributor

Summary

  • Hold one Arc<HostTexture> (resource::host_copy) across use-stamping, content refresh, and state tracking (HostList::move_texture) in external::host_texture, preventing concurrent eviction from replacing the host texture between lookup and list state tracking.
  • Refresh read-only external texture inputs before transitioning: copy from incompatible GPU-written placed aliases (resource::alias_source -> texture::alias_copy) and upload title-written textures (texture::title_written -> texture::upload) so host libraries (such as fidelityfx upscaling) sample initialized texels on first use.
  • Route depth textures with writable twins through twin::route: return twin.planes[0].texture in UNORDERED_ACCESS for single-plane twins, reject UNORDERED_ACCESS on multi-plane depth+stencil twins without mutating list.twins, and synchronize newer twin writes back into the depth texture before shader or copy reads.
  • Record target usage via capture::note_target under diagnostics when an external caller transitions a texture into a write target state.

Coordination

Testing

  • cargo test -p victory-gpu --locked external::tests:: (3 synthetic D3D12 GPU tests covering title-written uploads + guarded-page updates, first-read incompatible placed alias copy, and 1-plane/2-plane depth twin routing)
  • cargo test -p victory-hle --test fidelityfx --locked
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings
  • cargo run -p xtask --locked -- policy

@kotekompotek

Copy link
Copy Markdown

Verified the semantics against main:

  • The eviction-race fix is real: the old path re-resolved the host texture (host_resource then list.transition->host_copy), so an eviction between lookup and tracking could leave the returned resource and the tracked Arc diverging. Holding one host_copy Arc across stamp/refresh/move_texture (track keys on Arc::as_ptr) closes it.
  • Refresh gating is right: alias_copy + title_written upload only on pure-read states (READ bits, no TARGET) - a write transition correctly skips uploading content that's about to be overwritten.
  • Twin handling mirrors HostList::transition's route contract (route=true means the use is served from the twin side), but returns the writable plane to the external caller instead of the depth texture - which is the correct object for their UAV write, and sync-back then flows through the existing Use records. Rejecting UA on multi-plane twins before route (no list.twins mutation) is the safe choice.

Tests cover exactly the three risky paths (guarded-page upload, alias first-read, 1/2-plane routing). LGTM.

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

The alias refresh can still switch host copies mid-recording. host_texture() captures copy, but alias_source() and alias_copy() look up the destination again. If eviction clears the cached host texture after alias_source() picks a writer, alias_copy() can populate a replacement while move_texture() and the return value still use the original copy. The held Arc keeps the original alive, but it doesn’t keep it in the resource cache.

Could the alias lookup and copy use the destination Arc already held by host_texture()? A regression could force eviction between lookup and refresh and check that the returned texture contains the alias writer’s texels.

AI-assisted with Codex.

@mgunes1311-lab

Copy link
Copy Markdown
Contributor Author

Good catch — threaded the held Arc<HostTexture> through resource::alias_source_for, texture::alias_copy_to, and texture::upload_copy (with texture::staging_for) so external::host_texture refreshes and tracks that single held copy without re-resolving or repopulating an evicted slot, and added external::tests::eviction_after_capturing_copy_refreshes_the_held_host_texture covering eviction between host_copy lookup and refresh on both the alias and upload paths.

@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown

Independent WARP/CPU verification for head 14c54b0 against origin/main 13f91ca. State check: open, non-draft, clean merge-tree; no oni-swr comment found.

Environment: Windows x64, Rust 1.98.1 (48a229cea 2026-09-01), Windows SDK dxil.dll 10.0.26100.0 (file version 1.8.2502.11); VIctory WARP and D3D12 WARP env vars set. CARGO_TARGET_DIR=D:\victory-warp-2720-target.

Commands/results:

  • Baseline: cargo test --locked -p victory-gpu --lib — 195 passed, 0 failed, 1 ignored. cargo clippy --locked -p victory-gpu --all-targets -- -D warnings — pass.
  • PR: cargo test --locked -p victory-gpu external::tests:: — 4/4 passed; cargo test --locked -p victory-gpu --lib — 199 passed, 0 failed, 1 ignored; cargo clippy --locked -p victory-gpu --all-targets -- -D warnings — pass.
  • Diagnostics: cargo test --locked -p victory-gpu --features diagnostics external::tests:: — 3 passed, 1 failed (eviction_after_capturing_copy_refreshes_the_held_host_texture fails at give_up_now(placed)); serial rerun fails at the same assertion. Diagnostics Clippy passes.
  • Negative control: disabled the read-refresh branch; title_written_textures_upload_before_external_reads failed on uploaded texel (0 vs expected 4281541137).

Review: no production blocker found in the inspected alias/upload/twin paths. The new eviction regression test currently does not pass with diagnostics enabled, so that configuration needs a test adjustment or explanation.

Not tested: physical GPU, retail title/game behavior, release build. AI disclosure: Codex (gpt-6-luna).

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Independent Windows 11 + NVIDIA GeForce RTX 5090 + WARP verification and code review of 14c54b0b (on 13f91ca):

Verification (rustc 1.98.1 MSVC)

  • cargo xtask policy: 184 files ok; cargo clippy --locked -p victory-gpu --all-targets (default & --features diagnostics): 0 warnings.
  • WARP (VICTORY_D3D12_WARP=1, --features diagnostics):
    • cargo test --locked -p victory-gpu --features diagnostics -- --test-threads=1 -> 200 passed, 2 failed, 1 ignored (external::tests::eviction_after_capturing_copy_refreshes_the_held_host_texture fails at external.rs:549, and host::batch::a_copy_is_refreshed_once_per_submission_by_the_pages_written fails at host.rs:3523).
  • RTX 5090:
    • cargo test --locked -p victory-gpu --lib -- external::tests:: --test-threads=1 -> 3 passed, 1 failed (reproduces the exact same external.rs:549 failure deterministically).
    • Running eviction_after_capturing_copy_refreshes_the_held_host_texture or host::batch::a_copy_is_refreshed_once_per_submission_by_the_pages_written without running aliased_textures_copy_from_their_gpu_writer_on_first_external_read beforehand passes 100%.

Bug Found in Code Review (Root Cause of the Deterministic --test-threads=1 Failures)

  • Redundant upload_copy after alias_copy_to leaks HostTexture + watch::Tracker in bindless::uploads's static LIST (crates/victory-gpu/src/external.rs:44-51):
    1. When alias_source_for matches and calls alias_copy_to, host_writes_copy defers setting host_written = true until submission (run_deferred).
    2. Immediately afterward, external.rs:48-50 runs if crate::texture::title_written(&copy.texture) { crate::texture::upload_copy(host, resource, copy); }. Because host_written is still false (and alias_copy_to marks first_use(to | 1) while upload_copy checks first_use(to)), upload_copy still creates a Staging + watch::track_copy and pushes Step::Texture(staging, copy.clone()).
    3. During host.execute, bindless::uploads (host.rs:1178, which runs before run_deferred) sees host_written == false and records copy into bindless::uploads's global static LIST (bindless.rs:202), keeping copy (now host_written = true) and its Staging/watch::Tracker alive until a later bindless::uploads call resets that list.
    4. When the next test reuses the same virtual address, MEMORY upgrades that still-live host_written = true copy (tripping assert!(give_up_now(placed)) at external.rs:549) and the stale watch::Tracker breaks host::batch (left: 0, right: 65536 at host.rs:3523).
  • Suggested fix: skip upload_copy when alias_copy_to already populated copy from an aliased GPU writer (else if crate::texture::title_written(&copy.texture)).
  • Merge-ordering note with GPU: pin textures with pending host_writes against eviction #2601: overlaps crates/victory-gpu/src/texture.rs (host_writes_copy/alias_copy_to vs GPU: pin textures with pending host_writes against eviction #2601's PendingHostWrite/prepare_host_write — host_writes_copy will want to wire into PendingHostWrite once GPU: pin textures with pending host_writes against eviction #2601 lands).

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Holding one Arc<HostTexture> across stamping, refresh and state tracking in external::host_texture (crates/victory-gpu/src/external.rs) is a sensible fix for the eviction race, and the *_for/*_to variants in resource.rs and texture.rs read correctly.

It is not mergeable as it stands. The check CI job fails on this head, and runs reported in the comments show external::tests::eviction_after_capturing_copy_refreshes_the_held_host_texture failing at external.rs:549 when it runs after the alias test. The likely cause is in host_texture_for: after texture::alias_copy_to fills the copy, texture::upload_copy still runs, because host_written is only set when the list is submitted. That upload queues title memory over the alias copy and keeps a staging buffer alive in bindless::uploads.

What would change the verdict: skip the upload when the alias copy has already run (else if title_written), and get a green CI run.


Generated by Claude Code

@zed6000

zed6000 commented Nov 26, 2026

Copy link
Copy Markdown

Merged onto current main 63bcca4 (git merges it without a conflict), cargo test -p victory-gpu --lib --locked -- --test-threads=1 on an RX 7900 XTX aborts the test process:

thread 'resource::tests::a_null_view_has_no_alias_source' panicked at crates\victory-gpu\src\resource.rs:1510:14:
null pointer dereference occurred
thread caused non-unwinding panic. aborting.
(exit code: 0xc0000409, STATUS_STACK_BUFFER_OVERRUN)

The cause is a semantic conflict with main that git doesn't flag. Since 219878a0 ("GPU: a null shader view has no alias source", the fix for the in-game crash in #2847), main's alias_source starts with if placed.is_null() { return None; }. The merged alias_source from this PR is now let copy = made(resource(placed))?; alias_source_for(placed, &copy), which dereferences placed before any null check, so a null view crashes again. Keeping the null check at the top of alias_source (before made(resource(placed))) should restore it.

In the same full run, external::tests::eviction_after_capturing_copy_refreshes_the_held_host_texture and host::batch::a_copy_is_refreshed_once_per_submission_by_the_pages_written fail too, in two runs out of two. Run on their own, both pass, so something earlier in the suite affects them. I didn't narrow that down. All of this is with this PR's 14c54b0; main alone passes all 231.

@mgunes1311-lab

Copy link
Copy Markdown
Contributor Author

Updated onto main (63bcca4) and addressed the review findings:

  • external::host_texture: changed if crate::texture::title_written(&copy.texture) after alias_copy_to to else if so a title-memory upload neither overwrites a fresh render-target/depth alias copy nor retains copy in bindless::uploads.
  • resource::alias_source: restored an explicit if placed.is_null() { return None; } guard before made(resource(placed))? after merging 63bcca4.

Re-verified external::tests::* (--features diagnostics -- --test-threads=1), resource::tests::a_null_view_has_no_alias_source, host::batch::a_copy_is_refreshed_once_per_submission_by_the_pages_written, cargo clippy --workspace --all-targets --all-features --locked -- -D warnings, and cargo xtask policy on Windows 11 (x86_64-pc-windows-msvc).

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants