From d1d92612b0190404c4ba3f1724f4a538fb589cd2 Mon Sep 17 00:00:00 2001 From: Royoo <3230853833@qq.com> Date: Thu, 26 Nov 2026 09:46:50 +0800 Subject: [PATCH] Keep live SRVs intact during resource retirement --- CMakeLists.txt | 6 +- docs/startup-runtime.md | 31 ++--- src/startup_stubs/d3d12_xs_device.cpp | 153 ++------------------- tests/startup_resource_retire_views.cpp | 174 +++++++++++------------- 4 files changed, 109 insertions(+), 255 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c8612e7b39..ee6a43f171 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1449,8 +1449,8 @@ foreach(backend IN ITEMS warp hardware) ENVIRONMENT "X2W_D3D12_BUDGET=1;X2W_D3D12_PERMISSIVE=1;X2W_D3D12_LIST_NOOP=1;X2W_D3D12_RENDER=1;X2W_RESOURCE_INDEX_CHECK=1;X2W_D3D12_DEBUG=0") endforeach() -# X2W_D3D12_RELEASE_RETIRED_VIEWS=1 frees a released class texture that only tracked slot-18 descriptor slots keep alive by -# replacing those slots with null descriptors. `keep` is the negative control (views switch off, or retire switch off). +# Preserve live SRVs and their copies with and without the legacy views switch. Also verify that an +# explicit overwrite permits ordinary retirement, and that disabling base retirement keeps the resource. add_executable(startup_resource_retire_views tests/startup_resource_retire_views.cpp) target_include_directories(startup_resource_retire_views PRIVATE src) target_compile_features(startup_resource_retire_views PRIVATE cxx_std_17) @@ -1463,7 +1463,7 @@ foreach(backend IN ITEMS warp hardware) ENVIRONMENT "${retire_views_env};X2W_D3D12_RELEASE_RETIRED=1;X2W_D3D12_RELEASE_RETIRED_VIEWS=1") endforeach() set(retire_views_keep "X2W_D3D12_BUDGET=1;X2W_D3D12_PERMISSIVE=1;X2W_D3D12_LIST_NOOP=1;X2W_D3D12_RENDER=1;X2W_D3D12_DEBUG=0") - add_test(NAME startup_resource_retire_views_keep_${backend} COMMAND startup_resource_retire_views ${backend} keep) + add_test(NAME startup_resource_retire_views_keep_${backend} COMMAND startup_resource_retire_views ${backend}) set_tests_properties(startup_resource_retire_views_keep_${backend} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77 ENVIRONMENT "${retire_views_keep};X2W_D3D12_RELEASE_RETIRED=1") add_test(NAME startup_resource_retire_views_noretire_${backend} COMMAND startup_resource_retire_views ${backend} keep) diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 0f3b0cdef4..5be328f88a 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -1351,7 +1351,7 @@ ctest --preset dev --no-tests=error -R '^startup_trap_header(_prefix_control|_pr | `X2W_D3D12_QUERY_SLOTS=1` | diagnostic, in render mode: binds command-list slots 28 to 30 to the hypothesised occlusion query methods (see [Occlusion queries](#occlusion-queries)). Unset, the three slots stay no-ops | | `X2W_D3D12_BUDGET=1` | adds [video memory budget and allocation counts](#d3d12-resource-budget) to the D3D12 heartbeat | | `X2W_D3D12_RELEASE_RETIRED=1` | opt-in, effective with `X2W_D3D12_RENDER=1` only (the list handlers that record holds exist only there): frees the desktop object of a released committed 2D texture of one narrow class once no recorded list, submission or release-time fence refers to it ([details](#d3d12-resource-budget)). Unset, nothing changes | -| `X2W_D3D12_RELEASE_RETIRED_VIEWS=1` | opt-in, effective only together with `X2W_D3D12_RELEASE_RETIRED=1`: a released class texture that only tracked slot-18 descriptor slots keep alive (the title never overwrites them) is freed once those slots are replaced by null descriptors ([details](#d3d12-resource-budget)). Unset, nothing changes | +| `X2W_D3D12_RELEASE_RETIRED_VIEWS=1` | legacy diagnostic. Logs that clearing live slots is disabled and enables the existing retirement breakdown when base retirement and budgets are on ([details](#d3d12-resource-budget)) | | `X2W_D3D12_RETIRE_REASONS=1` | diagnostic only, effective with `X2W_D3D12_RELEASE_RETIRED=1` and `X2W_D3D12_BUDGET=1`: adds one `x2w-retire held:` line to the heartbeat that counts final-released resources whose desktop object is still alive, by reason ([details](#d3d12-resource-budget)). Unset, nothing is recorded | | `X2W_D3D12_VIEW_CENSUS=1` | diagnostic only: counts new distinct view records by device slot (a heartbeat line), and logs the record vector capacity and hash bucket count before either grows ([details](#d3d12-view-census)). Unset, nothing is counted or logged | | `X2W_DRAW_NULL_IDENTITY=1` | null descriptors read as identity values instead of zero | @@ -1459,24 +1459,17 @@ unordered-access flag, and live in a default heap without heap flags: reusing the same title address or desktop pointer does not skip uploads or resolve a retired wrapper. -`X2W_D3D12_RELEASE_RETIRED_VIEWS=1` (only with `X2W_D3D12_RELEASE_RETIRED=1`) ends the -`view-refs` retention. Once a released class texture's release-time fences have passed, -the drain (a try-lock, never blocking) replaces every tracked slot-18 descriptor slot -that names it, in every heap and so every `CopyDescriptors*` copy, by a null SRV of the -same dimension: the shadow entry and its ref are dropped, the desktop descriptor is -rewritten with `CreateShaderResourceView(nullptr, ...)`, the view records naming the -resource lose it, and the view token in title descriptor memory is cleared when it -still carries that id. A draw that reads the slot afterwards binds a null texture -(zeros). The texture is then freed by the normal rules: lists and submissions that hold -it through `retire_hold` keep the desktop object until the recording is dropped or the -submission's fence passes. No recorded list or submission reads a title heap's SRV -descriptor (translated draws build their own descriptors from the tokens at draw time -and hold what they sample), so the rewrite cannot reach GPU work. A slot that cannot be -rewritten keeps its ref and the texture stays retained. Not covered: a title thread -that writes the same slot at the moment of the drain, and a guest that uses the -resource after its final release (unsupported with the switch, as before). -`startup_resource_retire_views_*` reads the desktop descriptors back through a compute -probe; the `keep` and `noretire` cases are the negative controls. +A slot-18 descriptor and each tracked copy hold the resource until the slot is explicitly overwritten +or its heap is destroyed. A later binding of that slot still reads the resource after the last guest +Release and the release-time fences. A list or submission holding the resource keeps it alive after +the slot is overwritten, until the recording is discarded or the submission retires. + +The legacy `X2W_D3D12_RELEASE_RETIRED_VIEWS=1` setting only enables the retirement breakdown below and +logs that clearing live slots is disabled. The memory-pressure A/B in +[#2379](https://github.com/R0Y00/Xbox2Windows/issues/2379#issuecomment-7179801296) used the earlier +experimental rewrite. `startup_resource_retire_views_*` checks GPU readback from the original and +copied descriptors, and from a recorded copy behind a gated queue. It also checks retirement after +explicit overwrite and normal completion, with the legacy setting on and off. `X2W_D3D12_RETIRE_REASONS=1` (with `X2W_D3D12_RELEASE_RETIRED=1` and `X2W_D3D12_BUDGET=1`) adds a diagnostic line to the heartbeat. It counts every diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 0dcb9e742e..90faeaf78f 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -171,8 +171,6 @@ struct alignas(16) WrapperOwn { // accesses: a race needs the guest to use a resource across its own final Release. bool retire_hold_missed; // Resource: retire_hold could not record a use (no desktop object or no room), so the // drain must keep it for the process instead of dropping a list's desktop object. - bool retire_views_stuck; // Resource: X2W_D3D12_RELEASE_RETIRED_VIEWS could not clear every descriptor slot that - // names it, so it stays retained (and is not scanned again). std::uint8_t retire_never; // RetireNever of this resource (set only with X2W_D3D12_RELEASE_RETIRED) std::uint8_t retire_left; // RetireLeft: the first use that took it out of the class volatile LONG retire_view_refs; // Live tracked slot-18 SRV descriptor slots referencing this resource. @@ -822,17 +820,22 @@ const D3D12_CLEAR_VALUE* packed_clear_value(const D3D12_CLEAR_VALUE* clear, DXGI return © } -bool release_retired_enabled() +bool legacy_retire_view_stats() { - static const bool on = x2w_local::env_on("X2W_D3D12_RELEASE_RETIRED"); - return on; + static const bool requested = [] { + if (!x2w_local::env_on("X2W_D3D12_RELEASE_RETIRED_VIEWS")) return false; + log("x2w-d3d12: X2W_D3D12_RELEASE_RETIRED_VIEWS no longer clears live descriptors; budget statistics only"); + return true; + }(); + return requested; } -// X2W_D3D12_RELEASE_RETIRED_VIEWS=1 (needs X2W_D3D12_RELEASE_RETIRED=1): a released class member that only tracked slot-18 -// descriptor slots keep alive is freed once every such slot has been replaced by a null descriptor. -bool release_retired_views_enabled() +bool release_retired_enabled() { - static const bool on = release_retired_enabled() && x2w_local::env_on("X2W_D3D12_RELEASE_RETIRED_VIEWS"); + static const bool on = [] { + (void)legacy_retire_view_stats(); + return x2w_local::env_on("X2W_D3D12_RELEASE_RETIRED"); + }(); return on; } @@ -4960,34 +4963,6 @@ void retire_resource(Wrapper* w) if (!queued) InterlockedIncrement64(&retire_kept); // stays retained, as without the switch } -// A null SRV of the dimension the title's view had (the draw path binds the same R8G8B8A8_UNORM null for a view that -// resolves to nothing). A null resource reads no more of the description than its format and dimension. -D3D12_SHADER_RESOURCE_VIEW_DESC retire_null_srv_desc(const ViewRecord& record) -{ - D3D12_SHADER_RESOURCE_VIEW_DESC desc{}; - desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; - desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; - desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; - if (record.desc_bytes >= 40) { - D3D12_SRV_DIMENSION dimension; - std::memcpy(&dimension, record.desc + 4, sizeof(dimension)); - if (dimension >= D3D12_SRV_DIMENSION_TEXTURE1D && dimension <= D3D12_SRV_DIMENSION_TEXTURECUBEARRAY) - desc.ViewDimension = dimension; - } - switch (desc.ViewDimension) { - case D3D12_SRV_DIMENSION_TEXTURE1D: desc.Texture1D.MipLevels = 1; break; - case D3D12_SRV_DIMENSION_TEXTURE1DARRAY: desc.Texture1DArray.MipLevels = 1; desc.Texture1DArray.ArraySize = 1; break; - case D3D12_SRV_DIMENSION_TEXTURE2DARRAY: desc.Texture2DArray.MipLevels = 1; desc.Texture2DArray.ArraySize = 1; break; - case D3D12_SRV_DIMENSION_TEXTURE2DMSARRAY: desc.Texture2DMSArray.ArraySize = 1; break; - case D3D12_SRV_DIMENSION_TEXTURE3D: desc.Texture3D.MipLevels = 1; break; - case D3D12_SRV_DIMENSION_TEXTURECUBE: desc.TextureCube.MipLevels = 1; break; - case D3D12_SRV_DIMENSION_TEXTURECUBEARRAY: desc.TextureCubeArray.MipLevels = 1; desc.TextureCubeArray.NumCubes = 1; break; - case D3D12_SRV_DIMENSION_TEXTURE2DMS: break; - default: desc.Texture2D.MipLevels = 1; break; - } - return desc; -} - // True when the release-time fence of every queue has passed (retire_lock held). bool retire_fences_reached(const RetiredResource& item) { @@ -4997,106 +4972,10 @@ bool retire_fences_reached(const RetiredResource& item) return reached; } -// X2W_D3D12_RELEASE_RETIRED_VIEWS=1: a released class member whose release-time fences have passed and that only -// tracked slot-18 descriptor slots still name gets those slots replaced by null descriptors, so the main drain loop can -// free it. Every slot of every heap that has a shadow is handled, copies included (they are counted the same way). For -// each slot, under note_lock: the shadow entry is cleared and its ref dropped, and the desktop descriptor is rewritten -// as a null SRV of the same dimension. The view records that name the resource then lose it (so a token that a raw -// title copy left behind resolves to nothing), and the token in title descriptor memory is cleared when it still -// carries the id. A slot that cannot be rewritten keeps its ref, and the resource stays retained. Nothing waits. -// Why this cannot reach GPU work: translated draws never bind a title heap. They build their own descriptors from the -// title's tokens at draw time and retire_hold what they sample, so a descriptor of a title heap is read by no recorded -// list or submission (ClearUnorderedAccessView binds a heap for UAV slots only, and those are not touched here). -void retire_null_ready_views() -{ - std::vector ready; - try { - ready.reserve(64); - } catch (const std::bad_alloc&) { - return; - } - if (!TryAcquireSRWLockShared(&retire_lock)) return; - for (const RetiredResource& item : retired_resources) { - Wrapper* w = item.wrapper; - if (ready.size() == ready.capacity()) break; - if (w->own().retire_hold_missed || w->own().retire_views_stuck || !retire_fences_reached(item)) continue; - if (InterlockedCompareExchange(&w->own().retire_view_refs, 0, 0) <= 0) continue; - if (InterlockedCompareExchange(&w->own().refs, 0, 0) != 0) continue; // referenced again: leave its views - ready.push_back(w); - } - ReleaseSRWLockShared(&retire_lock); - if (ready.empty()) return; - - struct Cleared { - u64 title_cpu; - std::uint32_t index, increment, id; - }; - std::vector cleared; - std::size_t stuck = 0; - // draw_lock first, in the order draw_translated takes them: a draw holds it from reading a view record until it - // retire_holds the texture, so clearing the records under it means a draw either recorded its hold already or - // resolves the token to nothing. - if (!TryAcquireSRWLockExclusive(&draw_lock)) return; - if (!TryAcquireSRWLockExclusive(¬e_lock)) { - ReleaseSRWLockExclusive(&draw_lock); - return; - } - try { - std::unordered_set ids; // view ids that name a ready resource - for (std::uint32_t id = 0; id < view_records().size(); ++id) { - const ViewRecord& record = view_records()[id]; - if (record.slot != 18 || !view_materialized()[id]) continue; - if (std::find(ready.begin(), ready.end(), reinterpret_cast(record.resource)) != ready.end()) - ids.insert(id); - } - ID3D12Device* const device = the_device ? device_of(the_device) : nullptr; - for (const HeapInfo& h : heap_infos()) { - if (!h.shadow || ids.empty() || !device || !h.desktop_cpu) continue; - for (std::uint32_t i = 0; i < h.count; ++i) { - const std::uint32_t slot_id = h.shadow[i]; - if (!slot_id || !ids.count(slot_id - 1)) continue; - const ViewRecord& record = view_records()[slot_id - 1]; - const D3D12_SHADER_RESOURCE_VIEW_DESC null_view = retire_null_srv_desc(record); - device->CreateShaderResourceView(nullptr, &null_view, - D3D12_CPU_DESCRIPTOR_HANDLE{static_cast(h.desktop_cpu + u64{i} * h.increment)}); - h.shadow[i] = 0; - retire_release_view_ref(reinterpret_cast(record.resource)); - if (h.title_cpu && h.title_cpu != h.desktop_cpu) cleared.push_back({h.title_cpu, i, h.increment, slot_id - 1}); - } - } - for (Wrapper* w : ready) { - if (InterlockedCompareExchange(&w->own().retire_view_refs, 0, 0) > 0) { - w->own().retire_views_stuck = true; - ++stuck; - continue; - } - for (std::uint32_t id : ids) - if (view_records()[id].resource == reinterpret_cast(w)) view_records()[id].resource = 0; - } - } catch (const std::bad_alloc&) { - } - ReleaseSRWLockExclusive(¬e_lock); - ReleaseSRWLockExclusive(&draw_lock); - for (const Cleared& slot : cleared) { - const u64 token = slot.title_cpu + u64{slot.index} * title_descriptor_stride(slot.title_cpu, slot.increment); - if (!title_backed(token, 32) || !x2w_local::writable(reinterpret_cast(token), 32)) continue; - std::uint32_t words[2]; - std::memcpy(words, reinterpret_cast(token), sizeof(words)); - if (words[0] == view_token_magic && words[1] == slot.id) - InterlockedCompareExchange(reinterpret_cast(token), 0, static_cast(view_token_magic)); - } - if (stuck) { - static LONG reported; - if (first_calls(reported, 4)) log("x2w-d3d12 retire: %zu released textures keep an untracked descriptor; they stay retained", stuck); - } -} - - // Never blocks: a contended lock or an unfinished queue leaves the work for the next call. void drain_retired_resources() { if (!InterlockedCompareExchange(&retire_pending, 0, 0)) return; - if (release_retired_views_enabled()) retire_null_ready_views(); constexpr std::size_t batch = 256; struct RetiredDrop { Wrapper* wrapper; @@ -5127,10 +5006,7 @@ void drain_retired_resources() ++i; continue; } - bool reached = item.targets.size() <= retire_queues.size(); - for (std::size_t q = 0; q < item.targets.size() && reached; ++q) - reached = retire_queues[q].fence->GetCompletedValue() >= item.targets[q]; - if (!reached) { + if (!retire_fences_reached(item)) { ++i; continue; } @@ -5195,7 +5071,8 @@ void drain_retired_resources() // is recorded and no line is built. bool retire_reasons_enabled() { - static const bool on = release_retired_enabled() && x2w_budget::enabled() && x2w_local::env_on("X2W_D3D12_RETIRE_REASONS"); + static const bool on = release_retired_enabled() && x2w_budget::enabled() && + (legacy_retire_view_stats() || x2w_local::env_on("X2W_D3D12_RETIRE_REASONS")); return on; } diff --git a/tests/startup_resource_retire_views.cpp b/tests/startup_resource_retire_views.cpp index 6db82d2895..263dd96dcd 100644 --- a/tests/startup_resource_retire_views.cpp +++ b/tests/startup_resource_retire_views.cpp @@ -1,24 +1,9 @@ -// X2W_D3D12_RELEASE_RETIRED_VIEWS=1 (with X2W_D3D12_RELEASE_RETIRED=1, Refs #2379): a released committed 2D texture that -// only tracked slot-18 descriptor slots keep alive is freed once those slots hold null descriptors. The title never has -// to overwrite them. -// +// Live SRVs keep retired textures available for later bindings (#2379). // startup_resource_retire_views [keep] -// -// `keep` is the negative control: the views switch is off (or the retire switch is, with the views switch set), so a -// released texture stays retained exactly as before and its descriptors keep naming it. -// -// The fixture reads the desktop descriptors the shim wrote: a compute shader on the fixture's own desktop queue loads -// texel (0,0) through a slot of a guest heap (bound directly when the heap is shader-visible, copied into a probe -// heap when it is not). A filled texture reads back non-zero, a null descriptor reads back zero. -// Cases: -// 1 single SRV in a shader-visible heap, never overwritten: freed after the release fences, descriptor null, view -// token in title descriptor memory cleared. -// 2 the same texture also copied into a second (non-shader-visible) heap, release behind a gated queue: nothing is -// touched until the gate opens, then both copies are null and the texture is freed (copies are handled). -// 3 a closed list recorded a CopyResource from the texture, submitted behind a gate: descriptors are nulled but the -// desktop object lives until the submission's fence, and the copy still reads the texels. -// 4 a closed, never submitted list names the texture: nulled, retained until the recording is discarded. -// 5 a live texture's descriptor is never touched. +// `keep` runs without base retirement, as a control for the budget drop after explicit overwrite. +// The legacy views switch may be on or off. Both settings preserve the original and copied slots. +// The fixture uses the shim's descriptor creation and copying paths and reads the desktop descriptors +// with a compute probe. The gated queue and the two recorded copies cover pending GPU work. #include "startup_stubs/resource_budget.h" #include @@ -417,75 +402,74 @@ D3D12_SHADER_RESOURCE_VIEW_DESC texture_view() return srv; } -// 1: one SRV in a shader-visible heap, never overwritten. -bool case1_single_view(Context& c, bool on, GuestHeap& visible) +void overwrite_srv(Context& c, const GuestHeap& heap, UINT index) { - Res& T = c.make_committed("T1"); - if (!c.fill_desktop(T.guest.Get(), 1)) return false; const auto srv = texture_view(); - invoke(c.guest, 18, T.guest.Get(), &srv, visible.cpu); - if (!c.slot_is(visible, 0, false, "case 1 control: live texture's descriptor") || - Context::token_word(visible, 0, c.increment) != view_token_magic) { - std::puts("FAIL: case 1 control: live texture's view token missing"); - return false; - } - c.release(T); - if (on) { - T.freed = true; - if (!c.settle("T1 freed with its descriptor never overwritten", "T1 kept although only a descriptor names it")) - return false; - if (!c.slot_is(visible, 0, true, "case 1: descriptor after the free")) return false; - if (Context::token_word(visible, 0, c.increment) == view_token_magic) { - std::puts("FAIL: case 1: view token still in title descriptor memory after the free"); - return false; - } - return true; - } + invoke(c.guest, 18, static_cast(nullptr), &srv, heap.cpu + c.increment * static_cast(index)); +} + +// 1: a later binding of the same slot after the last guest Release. +bool case1_single_view(Context& c, bool retire_on, GuestHeap& visible) +{ + Res& texture = c.make_committed("T1"); + if (!c.fill_desktop(texture.guest.Get(), 1)) return false; + const auto srv = texture_view(); + invoke(c.guest, 18, texture.guest.Get(), &srv, visible.cpu); + if (!c.slot_is(visible, 0, false, "case 1: live descriptor") || + Context::token_word(visible, 0, c.increment) != view_token_magic) return false; + + c.release(texture); for (int i = 0; i < 8; ++i) - if (!c.stays("T1 released, views switch off", "T1 freed without the views switch")) return false; - return c.slot_is(visible, 0, false, "case 1 control: descriptor with the views switch off"); + if (!c.stays("T1 with a live slot", "T1 retired before its slot was overwritten")) return false; + if (!c.slot_is(visible, 0, false, "case 1: rebound descriptor after Release") || + Context::token_word(visible, 0, c.increment) != view_token_magic) return false; + + overwrite_srv(c, visible, 0); + if (!c.slot_is(visible, 0, true, "case 1: explicitly overwritten slot")) return false; + texture.freed = retire_on; + return c.settle("T1 after explicit overwrite", "unexpected backing lifetime after overwrite"); } // 2: a copy in a second heap, release behind a gated queue. -bool case2_copy_and_gate(Context& c, bool on, GuestHeap& visible, GuestHeap& staging) +bool case2_copy_and_gate(Context& c, bool retire_on, GuestHeap& visible, GuestHeap& staging) { - Res& T = c.make_committed("T2"); - if (!c.fill_desktop(T.guest.Get(), 2)) return false; + Res& texture = c.make_committed("T2"); + if (!c.fill_desktop(texture.guest.Get(), 2)) return false; const auto srv = texture_view(); - invoke(c.guest, 18, T.guest.Get(), &srv, visible.cpu + c.increment); + invoke(c.guest, 18, texture.guest.Get(), &srv, visible.cpu + c.increment); invoke(c.guest, 24, 1u, staging.cpu, visible.cpu + c.increment, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); ComPtr gate; if (!require(invoke(c.guest, 36, UINT64{0}, D3D12_FENCE_FLAG_NONE, &__uuidof(ID3D12Fence), gate.GetAddressOf()), "gate") || - !require(invoke(c.gated.queue.Get(), 17, gate.Get(), UINT64{1}), "wait")) - return false; - c.release(T); + !require(invoke(c.gated.queue.Get(), 17, gate.Get(), UINT64{1}), "wait")) return false; + + c.release(texture); for (int i = 0; i < 8; ++i) - if (!c.stays("T2 released behind a closed gate", "T2 freed before the release fences passed")) return false; - if (!c.slot_is(visible, 1, false, "case 2: original descriptor before the gate opens") || - !c.slot_is(staging, 0, false, "case 2: copied descriptor before the gate opens")) - return false; + if (!c.stays("T2 behind a closed gate", "T2 retired before the release fences passed")) return false; + if (!c.slot_is(visible, 1, false, "case 2: original before the gate opens") || + !c.slot_is(staging, 0, false, "case 2: copy before the gate opens")) return false; if (!require(invoke(gate.Get(), 10, UINT64{1}), "signal gate") || !c.sync(c.gated)) return false; - if (!on) { - for (int i = 0; i < 8; ++i) - if (!c.stays("T2 released, views switch off", "T2 freed without the views switch")) return false; - return c.slot_is(staging, 0, false, "case 2 control: copied descriptor with the views switch off"); - } - T.freed = true; - if (!c.settle("T2 freed after the gate opened", "T2 kept after its fences passed")) return false; - return c.slot_is(visible, 1, true, "case 2: original descriptor after the free") && - c.slot_is(staging, 0, true, "case 2: copied descriptor after the free"); + if (!c.stays("T2 after its release fences", "T2 retired while both descriptors were live") || + !c.slot_is(visible, 1, false, "case 2: original after the release fences") || + !c.slot_is(staging, 0, false, "case 2: copy after the release fences")) return false; + + overwrite_srv(c, visible, 1); + if (!c.stays("T2 with only its copy live", "T2 retired while its copy was live") || + !c.slot_is(staging, 0, false, "case 2: copy after overwriting the original")) return false; + overwrite_srv(c, staging, 0); + if (!c.slot_is(staging, 0, true, "case 2: explicitly overwritten copy")) return false; + texture.freed = retire_on; + return c.settle("T2 after both slots were overwritten", "unexpected backing lifetime after overwriting both slots"); } -// 3 and 4: lists that name the texture. -bool case3_list_holds(Context& c, bool on, GuestHeap& visible) +// 3 and 4: lists that name the texture. Overwriting the slots cannot drop list or submission holds. +bool case3_list_holds(Context& c, bool retire_on, GuestHeap& visible) { Res& T3 = c.make_committed("T3"); Res& D3 = c.make_committed("D3"); Res& T4 = c.make_committed("T4"); Res& D4 = c.make_committed("D4"); if (!c.fill_desktop(T3.guest.Get(), 3) || !c.fill_desktop(D3.guest.Get(), 99) || !c.fill_desktop(T4.guest.Get(), 4) || - !c.fill_desktop(D4.guest.Get(), 99)) - return false; + !c.fill_desktop(D4.guest.Get(), 99)) return false; const auto srv = texture_view(); invoke(c.guest, 18, T3.guest.Get(), &srv, visible.cpu + 2 * c.increment); invoke(c.guest, 18, T4.guest.Get(), &srv, visible.cpu + 3 * c.increment); @@ -494,30 +478,31 @@ bool case3_list_holds(Context& c, bool on, GuestHeap& visible) invoke(submitted.list.Get(), 17, D3.guest.Get(), T3.guest.Get()); invoke(discarded.list.Get(), 17, D4.guest.Get(), T4.guest.Get()); if (!c.close(submitted) || !c.close(discarded)) return false; + c.release(T3); c.release(T4); for (int i = 0; i < 8; ++i) - if (!c.stays("T3/T4 released behind closed lists", "a texture left the budget while a closed list names it")) - return false; - if (on && (!c.slot_is(visible, 2, true, "case 3: descriptor of the listed texture T3") || - !c.slot_is(visible, 3, true, "case 4: descriptor of the listed texture T4"))) - return false; + if (!c.stays("T3/T4 behind closed lists", "a texture retired while its list was closed")) return false; + if (!c.slot_is(visible, 2, false, "case 3: slot with a closed list") || + !c.slot_is(visible, 3, false, "case 4: slot with a closed list")) return false; + ComPtr gate; if (!require(invoke(c.guest, 36, UINT64{0}, D3D12_FENCE_FLAG_NONE, &__uuidof(ID3D12Fence), gate.GetAddressOf()), "gate3") || - !require(invoke(c.gated.queue.Get(), 17, gate.Get(), UINT64{1}), "wait3")) - return false; + !require(invoke(c.gated.queue.Get(), 17, gate.Get(), UINT64{1}), "wait3")) return false; c.execute(c.gated, submitted); - if (!SUCCEEDED(c.reset_list(submitted))) return false; + if (!SUCCEEDED(c.reset_list(submitted)) || + !c.slot_is(visible, 2, false, "case 3: live slot during an in-flight recording")) return false; + overwrite_srv(c, visible, 2); + overwrite_srv(c, visible, 3); for (int i = 0; i < 8; ++i) - if (!c.stays("T3 held by a gated submission", "T3 left the budget while its submission is in flight")) return false; - if (!SUCCEEDED(c.reset_list(discarded))) return false; // discards the recording that was never submitted - if (on) T4.freed = true; - if (!c.settle("T4 freed after its recording was discarded", "T4 kept after its recording was discarded")) return false; + if (!c.stays("T3/T4 after overwrite", "a texture retired while its list or submission still held it")) return false; + if (!SUCCEEDED(c.reset_list(discarded))) return false; + T4.freed = retire_on; + if (!c.settle("T4 after its recording was discarded", "unexpected T4 lifetime after discarding the list")) return false; if (!require(invoke(gate.Get(), 10, UINT64{1}), "signal gate3") || !c.sync(c.gated)) return false; - if (on) T3.freed = true; - if (!c.settle("T3 freed after its submission's fence", "T3 kept after its submission's fence passed")) return false; - // The copy ran against the live texels, not against anything freed or nulled. - return c.holds(D3.guest.Get(), 3, "D3 copy of T3 recorded before the release"); + T3.freed = retire_on; + if (!c.settle("T3 after its submission's fence", "unexpected T3 lifetime after the fence")) return false; + return c.holds(D3.guest.Get(), 3, "D3 copy of T3 recorded before Release and overwrite"); } // 5: a live texture is never touched. @@ -573,12 +558,11 @@ int main(int argc, char** argv) value[0] = 0; const bool views_on = GetEnvironmentVariableA("X2W_D3D12_RELEASE_RETIRED_VIEWS", value, sizeof(value)) == 1 && value[0] == '1'; const bool keep_mode = argc == 3; - // The effective switch needs both; `keep` runs with either one missing. - if ((retire_on && views_on) == keep_mode) return 2; - const bool on = !keep_mode; + if (retire_on == keep_mode) return 2; DXGI_ADAPTER_DESC1 adapter_desc{}; adapter->GetDesc1(&adapter_desc); - std::printf("adapter=%ls debug_layer=%s mode=%s\n", adapter_desc.Description, debug ? "enabled" : "off", keep_mode ? "keep" : "on"); + std::printf("adapter=%ls debug_layer=%s retire=%s legacy_views=%s\n", adapter_desc.Description, + debug ? "enabled" : "off", retire_on ? "on" : "off", views_on ? "on" : "off"); Context c{guest.Get(), device.Get()}; try { @@ -638,12 +622,12 @@ int main(int argc, char** argv) GuestHeap visible, staging; if (!c.make_heap(visible, 8, true) || !c.make_heap(staging, 1, false)) return 4; - if (!case1_single_view(c, on, visible)) return 5; - std::puts("1 single SRV in a shader-visible heap, never overwritten: freed with a null descriptor and a cleared token"); - if (!case2_copy_and_gate(c, on, visible, staging)) return 5; - std::puts("2 copied descriptor behind a closed gate: untouched until the fences pass, then both copies null"); - if (!case3_list_holds(c, on, visible)) return 5; - std::puts("3/4 listed texture: descriptors null, desktop object kept until the submission's fence or the discard"); + if (!case1_single_view(c, retire_on, visible)) return 5; + std::puts("1 same slot rebind reads its texture after Release and permits retirement after overwrite"); + if (!case2_copy_and_gate(c, retire_on, visible, staging)) return 5; + std::puts("2 original and copied slots read their texture after the release fences, retaining it through the final overwrite"); + if (!case3_list_holds(c, retire_on, visible)) return 5; + std::puts("3/4 overwritten slots preserve list and submission holds, recorded copy reads the original texels"); if (!case5_live_control(c, visible)) return 5; std::puts("5 live texture: descriptor untouched"); } catch (int code) { @@ -652,6 +636,6 @@ int main(int argc, char** argv) const UINT64 errors = debug ? debug->GetNumStoredMessages() : 0; std::printf("debug_errors_or_warnings=%llu\n", errors); if (errors || FAILED(device->GetDeviceRemovedReason())) return 4; - std::puts(on ? "release-retired views checks passed" : "views-off control checks passed"); + std::puts(retire_on ? "live descriptor retirement checks passed" : "base retirement off: all retention controls passed"); return 0; }