perf(gpu): zero-copy castable texture aliases (VICTORY_CASTABLE_ALIAS) #551
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/lobomfz--VIctory-xmem#551
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@chefulano on GitHub · 2026-12-05 02:46 UTC · original
Problem
Every placed format alias of a compute-written
R32_UINT/R32_SINTUAV texture viewed asR9G9B9E5_SHAREDEXPallocates a duplicate host texture and runs aCopyTextureRegionplus copy barriers on every read. On constrained 8 GiB VRAM / 16 GiB RAM AMD RDNA 3 (RX 7600M XT 8 GiB) hosts, those duplicate textures add VRAM pressure and per-frame copy overhead.Change
Adds an opt-in switch
VICTORY_CASTABLE_ALIAS=1(off by default; zero behavior change when unset) using D3D12 relaxed format casting (ID3D12Device10::CreateCommittedResource3), rebased cleanly on top of merged#550(dropping the duplicatedescriptor.rs/records.rshelpers already inmain):waits::options12()to read bothEnhancedBarriersSupportedandRelaxedFormatCastingSupported.resource::castable_alias_supported()runs a one-time probe verifyingRelaxedFormatCastingSupported,ID3D12Device10,R32_UINTUAV +R9G9B9E5_SHAREDEXPSRV format support, a 1×1CreateCommittedResource3allocation with[R32_UINT, R32_SINT, R9G9B9E5_SHAREDEXP], and typedR9G9B9E5_SHAREDEXPSRV creation +GetDeviceRemovedReason().is_ok()(noting thatGetDeviceRemovedReasononly detects device removal, not non-fatal SRV validation errors, so format-support checks gate creation first). Any failure falls back toCopyTextureRegion.host_texture_in()creates eligible UAV textures viaCreateCommittedResource3withcastable_initial_layout()always returning(0, 0)(D3D12_BARRIER_LAYOUT_COMMON+ tracked legacy state0) so the firstlist::ensure_state()transitions cleanly under legacyResourceBarrierinterop on native D3D12 (NVIDIA / WARP) and vkd3d-proton.descriptor::host::fits_view()/make_descriptor()preserve cross-family castable SRV formats.castable_source_copy()resolves the live sourceArc<HostTexture>entirely underTARGETS_AT(strict lock orderplaced.host.made[fills == true] →TARGETS_AT→source.host.made[fills == false]; zero unlocked raw-pointer dereferences), with a monotonicTARGETS_EPOCHcounter bumped underTARGETS_ATso steady-statehosted_for()skips lockingTARGETS_AT.hosted_for_allows_castable_init_bind()permits bothfit.is_none()and full-mipfit == Some((0, _))(still rejecting partial-mipfirst > 0).MadeSlottracks a monotonicprivate_dirtyflag set only by independent non-alias GPU destination writes (descriptor/host.rsUAV,draw.rsRTV/DSV,external.rsoutput_written,list.rsclear_rtv/clear_dsv, non-aliascopy_target,texture.rshost_written) so untouched view-first private copies safely upgradeprivate -> sharedon non-nooptexture::alias()calls (copy_target_for_alias()+HostList::alias_copy_would_be_noop()), retiring the oldMadeontoREPLACEDand queueing a descriptor rebuild viadescriptor::queue_texture_replaced().VICTORY_CASTABLE_ALIAS=torun.conf.defaultand documentsVICTORY_CASTABLE_ALIAS=1(includingBARRIER_LAYOUT_COMMON, view-first upgrades, and partial-mip / independently written fallbacks) indocs/SETTINGS.md.Measured impact
b1af1e9revision rejectedfit == Some((0, _))and locked view-first copies inFallbackCopy(0active binds /288,894fallbacks). After fixing full-mip init-bind and view-firstprivate -> sharedupgrades, clean 4-minute ABBA gameplay runs (4 × 240 s,0compiles) consistently engaged 57 active zero-copy castable alias binds (53within the first 60 s,0dirty fallbacks,0.0 swhiteout, full10/10non-inferiority parity).VICTORY_CASTABLE_ALIAS=1on vs off cut peak VRAM by-102 MiB(6,790 -> 6,688 MiB). On the clean 4-minute combined ABBA stack, average FPS was effectively tied with incumbent (27.80vs27.85 FPS); VRAM/copy savings are most relevant under tight VRAM budgets rather than heavily GPU-bound 16 GiB scenes.Verification
main(a5c6484, after#550and#355merged;0lines of diff indescriptor.rs/records.rs).cargo xtask policy:368 files ok;cargo test -p xtask -- policy::tests:7 passed, 0 failed;cargo test -p xtask -- switches::documented_switches_are_read:1 passed, 0 failed.cargo check --locked --target x86_64-pc-windows-msvc -p victory-gpu -p victory --testsandcargo clippy --no-deps --locked --target x86_64-pc-windows-msvc -p victory-gpu -p victory --all-targets: 0 errors, 0 new warnings.2 passed, 0 failed):resource::tests::castable_r32_selection_and_fallback_rulesandresource::tests::alias_views_are_not_pooled_or_evicted_as_title_filled. Live D3D12 probe and rendering require a D3D12 device.Pull request details or commits were not available from GitHub; kept as an issue.
github-bot referenced this issue2026-12-05 02:52:32 +00:00
github-bot referenced this issue2026-12-05 02:52:35 +00:00
@Kal0s-design on GitHub · 2026-12-05 03:11 UTC · original
Checked b1af1e9 on native Windows (one NVIDIA desktop card plus WARP, didn't run the game).
This branch is one commit on top of
b581f7eand does not contain #550's commit (65f3c84). The two duplicate the descriptor refresh plumbing (queue_texture_replaced, refresh_texture, refresh_slot, refresh_placed_at, is_placed_key_at) and a trial merge of the two heads conflicts in descriptor.rs, resource.rs and texture.rs, so whichever lands second needs a real rebase, not just a re-run.Tests. cargo test -p victory-gpu --lib on main and on the branch give the same five known failures (the two indirect host_list ones, the two pipeline library loads, the flaky watch one) and nothing new; the branch adds three passing tests. Same result with VICTORY_CASTABLE_ALIAS=1, with VICTORY_D3D12_DEBUG=1 on a diagnostics build (no test went down through host::fail, though the suite doesn't print layer warnings otherwise, and none of these tests create a castable texture anyway), and on WARP, where views_are_made_beside_the_heap_and_copied_in also fails but it fails the same way on main. clippy on victory-gpu has the same warning count as main and none of the warnings point at new lines. cargo check -p victory is clean apart from the existing room_lately warning.
Both the card and WARP report EnhancedBarriersSupported and RelaxedFormatCastingSupported as true, and castable_alias_supported() passes its probe on both.
The existing unit tests only cover the pure helpers, so nothing in the suite creates a castable texture on a device. I added a throwaway test (not pushed) that calls host_texture_in for an 8x8 R32_UINT UAV texture with the switch on, writes a pattern through an R32_UINT UAV in one dispatch, transitions it, reads it back through an R9G9B9E5_SHAREDEXP SRV in a second dispatch and compares against a CPU decode of the shared-exponent bits. All 64 texels match bit for bit on the card and on WARP, so the cast itself works and the castable list is accepted. As a sanity check, an SRV in R16G16_FLOAT on the same texture is rejected by the debug layer with "not specified in the list of castable formats", so the layer is really looking at these resources.
One real problem showed up under the debug layer. castable_initial_layout (resource.rs:3164, used at resource.rs:3637 and 3667) creates the texture with an initial barrier layout that matches the requested legacy state, for example D3D12_BARRIER_LAYOUT_UNORDERED_ACCESS when the title asks for 0x8. The rest of the host moves textures with legacy transitions (list::ensure_state builds them with host::transition and host/rec.rs:79 records them with ResourceBarrier), and the debug layer rejects that mix at ExecuteCommandLists on both the card and WARP. Its message says the UNORDERED_ACCESS layout "does not support barrier interop", and that interop needs the texture in BARRIER_LAYOUT_COMMON.
I get the same error with the 0xC0 mapping (SHADER_RESOURCE layout). With the requested state 0, which goes to LAYOUT_COMMON, the same test runs with no debug layer messages at all. My barriers there were recorded by hand rather than through ensure_state, but they are the same legacy transitions. The data still came out right on both devices, so today it only shows up as debug layer errors, but what happens is up to the driver. The simple fix looks like always passing D3D12_BARRIER_LAYOUT_COMMON and returning 0 as the tracked legacy state, and letting the first ensure_state do the transition, as already happens for the 0x40 and 0x80 fallbacks.
Smaller notes.
The SRV part of the probe (resource.rs:3346) checks GetDeviceRemovedReason after CreateShaderResourceView, but an invalid view doesn't remove the device, it only shows up in the debug layer. It's harmless here since the view is valid, but that check proves less than the PR body suggests.
Binding is order dependent. The first-use path in hosted_for (around resource.rs:1745) and castable_source_copy (resource.rs:3427) only share the source when the source already has a castable host copy and its made lock is free (try_lock at resource.rs:3451). If the placed view gets its host copy first, shared_alias_action returns FallbackCopy from then on, so that view keeps its private copy and the copy path for good. That's correct, just less of a saving than "zero duplicate VRAM" in SETTINGS.md implies.
When the view is shared, the placed view and the source are the same ID3D12Resource with one tracked state (HostList keys states by the HostTexture Arc), so a single draw or dispatch that binds the source as UAV and the placed view as SRV would ask for both states on one resource. With the copy path they were two resources. I don't know whether the title ever does that, so this is more a question than a bug.
The steady-state hosted_for skip only notices a source being added to or removed from TARGETS_AT (TARGETS_EPOCH). A source whose host copy is swapped for a new one without that would only be noticed in texture::alias through ensure_castable_alias, which re-checks the serial on every call. As far as I can see targets don't get their copies replaced today (sole_idle needs a strong count of 1, so a shared copy isn't let go either), so this seems fine, just worth knowing.
The two unwraps (resource.rs:1674 and 3501) only run on actions that need a source, so they can't fire. On lock order, the reconcile path blocks on the source's made lock while holding TARGETS_AT, so what matters is that nothing takes TARGETS_AT under a target's own made lock. I couldn't find a place that does. hand_out_resource and Resource::drop take it with no made lock held, and the alias_of call in hosted_for (resource.rs:1784) only runs for placed views, which are never targets.
Uploads can't reach a shared copy either. readable_inner, the bindless slot path in draw.rs and external.rs all check the host copy's own flags (title_written), and the shared copy has the UAV flag. move_out only moves pooled pieces, and the shared copy is committed.
The new row in docs/SETTINGS.md sits under "Experimental speed switches", which says those are listed in the menu under Performance, but there's no matching entry in run.conf.default, so the menu won't show it.
With the switch off I don't see a behaviour change. host_texture_in reaches the old CreateCommittedResource path, fits_view gets castable = false, and texture::alias skips the new branch. The only extra work is an uncontended mutex in make_waiting (descriptor.rs:1086) and forget, plus two relaxed atomic loads in hosted_for. The legal_state call moved into host_texture_in but computes the same value.
PR body against the code. The options12 split (waits.rs:122, 153), the probe steps, the lock order and the epoch counter are all as described. "removes the unused host_texture / host_texture_as wrappers" is slightly off, since host_texture was still used by hosted_for on main, and this branch replaces that call with host_texture_in(.., grow = true), which does the same thing. The "exact parity" claim for castable_initial_layout holds for the state tracking, but that layout choice is what causes the debug layer error above.
Not tested. The game, the real title R32 to R9G9B9E5 aliases, the end-to-end path through a title list (hosted_for binding, make_descriptor and fits_view, the queued descriptor refresh), any AMD or Intel driver, and vkd3d-proton. The layout finding comes from a hand-built list, not from a title list. I also didn't measure any performance change.
AI-assisted: yes
@luisaugustz on GitHub · 2026-12-05 13:30 UTC · original
Measured on RX 9070 XT + Ryzen 7 5800X (RADV, 4K output, FSR 4 Performance, ray tracing on), on the
downtownroute from #554:VICTORY_CASTABLE_ALIAS=1No change here on a 16 GB card.
One run's end screenshot showed a single bright green tree. The other two were clean, and similar green specks also appear in baseline runs, so it is probably unrelated.
Details are in #591.
@chefulano on GitHub · 2026-12-05 20:22 UTC · original
Rebased
#551onto currentmain(a5c6484, dropping alldescriptor.rs/records.rshelpers already merged by#550) with fixes for both bugs caught by @Kal0s-design and @luisaugustz:castable_initial_layout()now always returns(0, 0)(D3D12_BARRIER_LAYOUT_COMMONwith tracked legacy state0), so the firstlist::ensure_state()performs the transition cleanly without legacy barrier-interop errors on native D3D12 (NVIDIA / WARP) and vkd3d-proton.16.4vs16.4 FPSin#591(@luisaugustz, @Kal0s-design): Inb1af1e9,hosted_for()gated init-bind onfit.is_none()(rejecting placed views that enter with full-mipfit == Some((0, _))), and any view-first private copy stayed locked inFallbackCopypermanently. Instrumented gameplay onb1af1e9recorded 0 active castable alias binds and 288,894 fallbacks, explaining why it behaved identically to baseline in#591.fit == Some((0, _))init-bind (hosted_for_allows_castable_init_bind) and adds safeprivate -> sharedupgrades on non-nooptexture::alias()calls viaMadeSlot(private_dirtyset only by independent non-alias GPU destination writes: RTV/DSV, UAV, non-aliascopy_target, resolve,output_written,host_written, pluscopy_target_for_alias()andHostList::alias_copy_would_be_noop()).4 × 240 s,0compiles): active zero-copy castable aliases went from0to 57 live binds (53byt = 60 s,0dirty fallbacks,0.0 swhiteout, full10/10non-inferiority parity). On the clean 4-minute combined ABBA, overall average FPS was an effective tie (27.80vs27.85 FPS); on a 16 GiB card in a heavily GPU-bound 4K RT scene, VRAM and copy reductions may still be more visible than average FPS.VICTORY_CASTABLE_ALIAS=torun.conf.default, updateddocs/SETTINGS.mdto documentBARRIER_LAYOUT_COMMON, view-first upgrades, and partial-mip / independently written fallbacks, and updated the PR description (removing the stalehost_texturenote and noting theGetDeviceRemovedReasonprobe limit). On shared-resource state tracking: in the title'sR32_UINT -> R9G9B9E5_SHAREDEXPpasses, the UAV write and SRV read occur in separate passes rather than simultaneously in one draw/dispatch.Verified on
a5c6484:cargo xtask policy(368 files ok),policy::tests(7/7),switches::documented_switches_are_read(1/1),cargo check+cargo clippy --no-deps(0new warnings),resource::tests::castable_r32_selection_and_fallback_rules+alias_views_are_not_pooled_or_evicted_as_title_filled(2/2).