Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1482,8 +1482,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)
Expand All @@ -1496,7 +1496,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)
Expand Down
31 changes: 12 additions & 19 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,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_DEFAULTS=all` or `=off` | `all` is experimental: UAV, RTV and DSV descriptions with `ViewDimension` 0 also get the resource's default view, which drops their requested format and component mapping. `off` skips the measured SRV case too. Unset, only SRVs of typed non-depth textures get the default view ([details](#unknown-and-mismatched-view-dimensions)) |
| `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 |
Expand Down Expand Up @@ -1573,24 +1573,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
Expand Down
134 changes: 15 additions & 119 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,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.
Expand Down Expand Up @@ -827,17 +825,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;
}

Expand Down Expand Up @@ -5178,15 +5181,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)
{
unsigned dimension = D3D12_SRV_DIMENSION_TEXTURE2D;
if (record.desc_bytes >= 40) std::memcpy(&dimension, record.desc + 4, sizeof(dimension));
return null_srv_desc(dimension);
}

// True when the release-time fence of every queue has passed (retire_lock held).
bool retire_fences_reached(const RetiredResource& item)
{
Expand All @@ -5196,106 +5190,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<Wrapper*> 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> 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(&note_lock)) {
ReleaseSRWLockExclusive(&draw_lock);
return;
}
try {
std::unordered_set<std::uint32_t> 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<Wrapper*>(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<SIZE_T>(h.desktop_cpu + u64{i} * h.increment)});
h.shadow[i] = 0;
retire_release_view_ref(reinterpret_cast<const void*>(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<u64>(w)) view_records()[id].resource = 0;
}
} catch (const std::bad_alloc&) {
}
ReleaseSRWLockExclusive(&note_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<void*>(token), 32)) continue;
std::uint32_t words[2];
std::memcpy(words, reinterpret_cast<const void*>(token), sizeof(words));
if (words[0] == view_token_magic && words[1] == slot.id)
InterlockedCompareExchange(reinterpret_cast<volatile LONG*>(token), 0, static_cast<LONG>(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;
Expand Down Expand Up @@ -5326,10 +5224,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;
}
Expand Down Expand Up @@ -5394,7 +5289,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;
}

Expand Down
Loading
Loading