Skip to content
16 changes: 16 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,22 @@ 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_D3D12_DEBUG=0")
endforeach()

add_executable(startup_resource_retire_title tests/startup_resource_retire_title.cpp)
target_include_directories(startup_resource_retire_title PRIVATE src)
target_compile_features(startup_resource_retire_title PRIVATE cxx_std_17)
target_link_libraries(startup_resource_retire_title PRIVATE d3d12_xs_shim d3d12 dxgi d3dcompiler)
foreach(backend IN ITEMS warp hardware)
foreach(debug IN ITEMS 0 1)
set(retire_title_env "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=${debug}")
add_test(NAME startup_resource_retire_title_${backend}_debug${debug} COMMAND startup_resource_retire_title ${backend})
set_tests_properties(startup_resource_retire_title_${backend}_debug${debug} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77
ENVIRONMENT "${retire_title_env};X2W_D3D12_RELEASE_RETIRED=1")
endforeach()
add_test(NAME startup_resource_retire_title_off_${backend} COMMAND startup_resource_retire_title ${backend} off)
set_tests_properties(startup_resource_retire_title_off_${backend} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77
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()

# GPU writes the dispatch record that the desktop command processor consumes (Refs #1412).
add_executable(startup_compute_binding_trace tests/startup_compute_binding_trace.cpp)
target_compile_features(startup_compute_binding_trace PRIVATE cxx_std_20)
Expand Down
16 changes: 16 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -14975,6 +14975,7 @@ check and requires the old exception to come back:
The checks are `tests/startup_gfx2hlsl_handler_arity_check.py`, `…_image_arity_check.py`,
`…_operand_count_check.py` and `…_scalar_arity_check.py`. They run on Linux and Windows without D3D12 or game
data, because a refusal is observable at the `Stage` entry point.

## Startup translator: v_sad_u8, v_sad_u16 and v_sad_u32

[`tools/startup/gfx2hlsl.py`](../tools/startup/gfx2hlsl.py) now lowers the gfx1013 VOP3A unsigned sum-of-absolute-differences instructions `v_sad_u8` (`0x15a`), `v_sad_u16` (`0x15c`) and `v_sad_u32` (`0x15d`) in `VEC_U` to three on-demand preamble helpers emitted in `Pipeline.common()` only when referenced by a stage (`sad_u8`, `sad_u16`, `sad_u32`):
Expand All @@ -14989,3 +14990,18 @@ data, because a refusal is observable at the `Stage` entry point.
```powershell
ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_sad_(controls|warp|hardware)$' -V
```

## Opt-in release of retired slot-133 title-memory textures (Slice 2)

Building on #2443 (Slice 1), `X2W_D3D12_RELEASE_RETIRED=1` extends desktop object retirement to device slot 133 (`CreatePlacedResourceX`) committed 2D textures with `title_memory` (non-RT/DS/UAV, 1-sample, `DEFAULT` heap), while keeping slot 127 `__XMemAllocationHooks__` resources out of the class:

- `drain_retired_resources()` erases the retired wrapper from `resource_address_index()` (under `note_lock`), erases its desktop pointer from `texture_uploads()` (under `draw_lock`), and cleans stale `written_textures()` entries whose generation was not bumped by a newer allocation/write at the same title address (also cleared immediately on final `Release` when no view or live sibling shares the address).
- `list_copy_texture.inl` calls `upload_texture()` for a subresource copy only when the resource is not in the retire class or has title memory (`!retire_when_idle || title_memory`), so with the switch off every resource uploads exactly as before; `texture_uploads()` accesses in `list_copy.inl` / `list_copy_texture.inl` are guarded with `draw_lock`.
- `view_slot` records a view only when the desktop created it: desktop view creation returns `void`, so a removed device is the only failure it reports, and a failed slot is recorded unmaterialized (releasing its previous view reference and adding none). `retire_hold` reads the wrapper's desktop pointer under the retire lock and marks a use it cannot record, which makes the drain keep that desktop object for the process instead of dropping one a list still names.
- `view_slot<18>` (SRV) tracks descriptor slots via `HeapInfo::shadow` across `note_view`, `note_copy`, and heap destruction (falling back to `retire_leave_class` for untracked slots or slots 19–21), and `draw_translated` holds bound textures with `retire_hold`.

[`tests/startup_resource_retire_title.cpp`](../tests/startup_resource_retire_title.cpp) (`startup_resource_retire_title_{warp,hardware}_debug0` and `_off_`) verifies slot 133 copy/upload holds and readbacks, same-address slot 70 (`existing_memory_texture`) reuse under `X2W_RESOURCE_INDEX_CHECK=1`, stale `bc7_width` `written_textures` cleanup on RGBA8 reuse, `CopyDescriptorsSimple` / overwrite / heap-release SRV retirement, the switch-off control that an ordinary committed texture (no title memory) copy still records its upload, and that a newer `written_textures` generation survives the older generation's retirement across WARP (`Microsoft Basic Render Driver`) and `NVIDIA GeForce GTX 1660 SUPER`. Case 7 closes the fixture's draw-path gap (review finding B1): a host-built translated pixel shader samples a slot 133 title-memory texture through a descriptor table, its view token must resolve to that texture (a skipped resolve binds a null view and the readback reports `pixel (0,0) got 00000000`), and `retire_hold` in the draw keeps the released texture retained while the closed recording names it (removed, the budget reports `retained=0/0 expected=65536/1` before the recording is dropped); the readback holds the exact sampled words plus a clear sentinel row and column throughout, including after the texture leaves the budget.

```powershell
ctest --preset dev --no-tests=error -R '^startup_resource_(retire|budget)' -V
```
80 changes: 56 additions & 24 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -1373,33 +1373,54 @@ those lifetimes intact. `unmeasured` counts tracking failures. DXGI usage can
also include allocations outside these categories, such as swap chain and
descriptor heap backing.

`X2W_D3D12_RELEASE_RETIRED=1` ends the retained lifetime for one class: a
texture created by device slot 27 or 127 that is two-dimensional, has one
sample, no render-target, depth-stencil or unordered-access flag, lives in a
default heap without heap flags and has no title memory. If the title exports
`__XMemAllocationHooks__`, every slot 127 resource gets title memory and none is in the class; the fixture runs
without that export. Every other resource
keeps the lifetime described above. A resource leaves the class for good when a
view is created for it (slots 18 to 21, also a refused one), when it is passed
to MakeResident or Evict (slots 34 and 35), or when it is presented. Only
wrappers can be in the class; a raw desktop pointer never is. Sampler feedback
is not checked: no device slot of the stand-in reaches it.
`X2W_D3D12_RELEASE_RETIRED=1` ends the retained lifetime for committed
2D textures that have one sample, no render-target, depth-stencil or
unordered-access flag, and live in a default heap without heap flags:
- **Slice 1**: slot 27 and slot 127 textures without title memory (if the title
exports `__XMemAllocationHooks__`, every slot 127 resource gets title memory
and stays out of the class).
- **Slice 2**: slot 133 (`CreatePlacedResourceX`) committed 2D textures with
title memory. Before their desktop `ID3D12Resource` is destroyed, they are
erased from `resource_address_index` and `texture_uploads`, and stale
`written_textures` entries whose generation has not changed are cleared so
reusing the same title address or desktop pointer does not skip uploads or
resolve a retired wrapper.

Every other resource (buffers, placed buffers, reserved resources, render/depth/UAV
targets, and slot 127 `__XMemAllocationHooks__` textures) keeps the lifetime
described above. A resource leaves the class for good when a UAV/RTV/DSV view is
created for it (slots 19 to 21, also a refused one), when a slot 18 SRV targets
an untracked descriptor slot or is copied out of tracked heaps, when it is passed
to MakeResident or Evict (slots 34 and 35), or when it is presented. A slot 18
SRV keeps the resource only when the desktop created the view: desktop view
creation returns `void`, so a removed device is the only failure it reports, and
a slot whose creation failed is recorded without a reference. Tracked
slot 18 SRVs (including slot 70 `device_memory_srv`) keep the resource retained
while any descriptor slot in `HeapInfo::shadow` names it (updated across
`note_view`, `CopyDescriptors*`, and heap `Release`), and translated draws hold
bound textures via `retire_hold`. Only wrappers can be in the class; a raw
desktop pointer never is. Sampler feedback is not checked: no device slot of the
stand-in reaches it.

The final guest release signals a private desktop fence on every queue the
stand-in created. The desktop object is released when all of those fences have
passed and nothing else names it. A copy (list slot 17), texture copy (16) or
resolve destination (19) that the stand-in forwards makes the list hold a
desktop reference to each resource of the class it names, source and
destination, until the list is Reset, fails to Close or is destroyed. A list
passed, all tracked slot 18 SRV descriptor refs have dropped to zero, and no
recorded list or submission still holds it. A copy (list slot 17), texture copy (16),
resolve destination (19), or translated draw SRV bind that the stand-in forwards
makes the list hold a desktop reference to each resource of the class it names,
source and destination, until the list is Reset, fails to Close or is destroyed. A list
that is closed and never submitted is thus discarded without any submission.
Each submission holds the same references until its queue passes a fence
signalled behind it. A release with no registered queue, and a hold, marker or
submission fence that cannot be recorded, keep the resource retained. Nothing
waits: `ExecuteCommandLists` and `Present` check and release without blocking.
submission fence that cannot be recorded, keep the resource retained. A use that
`retire_hold` cannot record (no desktop object left, or no room for the entry)
leaves the class and marks the resource so the drain keeps its desktop object for
the process, as an unfenced submission is kept, instead of dropping an object a
list still names. Nothing waits: `ExecuteCommandLists` and `Present` check and
release without blocking.
The wrapper stays allocated, and a guest that uses a resource after its final
release is not supported with the switch. Buffers, placed and reserved
resources, slot 133 and all resources with title memory are not in the class.
Slot 18 (`CopyTiles`) is not forwarded at all and is outside this change.
release is not supported with the switch. Slot 18 (`CopyTiles`) is not
forwarded at all and is outside this change.

The source-built fixture reads the process-wide counters through
`x2w_d3d12_budget_snapshot` and checks allocation sizes, final guest release,
Expand All @@ -1415,10 +1436,21 @@ its own fence, a destination released after recording (slots 17, 16 and 19), a
list destroyed while its submission is held, a copy queue, an open list that
D3D12 refuses to Reset and one that is destroyed, a release that no list
refers to behind a held queue, and, in one run with the class, a buffer,
render target, title-memory texture, viewed texture, resident texture and
presented texture that must stay retained. The fixture-only export
`x2w_d3d12_retire_probe` returns FALSE without the switch, like
`x2w_d3d12_budget_snapshot`. `startup_resource_retire_off_*`
render target, viewed texture (while its slot-18 SRV stays live), resident texture and
presented texture that must stay retained, alongside slot 133 title-memory textures.
`startup_resource_retire_title_{warp,hardware}_debug{0,1}` additionally test slot 133
`CopyTextureRegion`/`CopyResource` upload holds, same-address slot 70 (`existing_memory_texture`)
reuse with `X2W_RESOURCE_INDEX_CHECK=1`, stale `written_textures` `bc7_width` cleanup,
slot-18 SRV `CopyDescriptorsSimple` / overwrite / heap-release lifetime, and, with the
switch on, a newer `written_textures` generation kept when the older generation retires
and a translated draw that samples such a texture through a descriptor table (the view
token's resolution and the draw's `retire_hold`: the readback holds the sampled texels and
a clear sentinel corner, and the texture leaves the budget only after the closed draw
recording is dropped); the `_off_` case checks that an ordinary committed texture (no
title memory) still records its upload. The fixture-only exports `x2w_d3d12_retire_probe`,
`x2w_d3d12_resource_index_probe`, `x2w_d3d12_texture_upload_probe` and
`x2w_d3d12_written_probe` read the bookkeeping the shim keeps; the retire probe returns
FALSE without the switch, like `x2w_d3d12_budget_snapshot`. `startup_resource_retire_off_*`
runs the same steps without the switch. The hardware cases report 77 without a
physical adapter, and the debug-layer cases report 77 where the debug layer is not installed.

Expand Down
44 changes: 41 additions & 3 deletions src/startup_stubs/d3d12_capture.inl
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,17 @@ void note_view(int slot, u64 resource, u64 counter, const void* desc, std::size_
view_ids()[hash] = record.id;
++views_distinct;
}
if (std::uint32_t* slot = shadow_slot(dest_desktop)) *slot = record.id + 1;
if (std::uint32_t* slot_ptr = shadow_slot(dest_desktop)) {
const std::uint32_t prev = *slot_ptr;
*slot_ptr = record.id + 1;
if (prev != record.id + 1) {
if (slot == 18 && resource && materialized) retire_add_view_ref(reinterpret_cast<const void*>(resource));
if (prev && prev - 1 < view_records().size() && view_records()[prev - 1].slot == 18 && view_materialized()[prev - 1])
retire_release_view_ref(reinterpret_cast<const void*>(view_records()[prev - 1].resource));
}
} else if (slot == 18 && resource) {
retire_leave_class(reinterpret_cast<const void*>(resource));
}
ReleaseSRWLockExclusive(&note_lock);
const std::size_t token_bytes = slot == 22 ? x2w_startup::title_sampler_stride : 32u;
if (dest_title != dest_desktop && title_backed(dest_title, token_bytes) &&
Expand All @@ -329,8 +339,36 @@ void note_copy(u64 dest_title, u64 src_title, u64 dest_desktop, u64 src_desktop,
std::uint32_t* to = shadow_slot(dest_desktop, &dest_room);
const std::uint32_t* from = shadow_slot(src_desktop, &src_room);
if (to && count <= dest_room) {
if (from && count <= src_room) std::memmove(to, from, u64{count} * 4);
else std::memset(to, 0, u64{count} * 4);
if (from && count <= src_room) {
for (UINT i = 0; i < count; ++i) {
const std::uint32_t src_id = from[i];
if (src_id && src_id - 1 < view_records().size() &&
view_records()[src_id - 1].slot == 18 && view_materialized()[src_id - 1])
retire_add_view_ref(reinterpret_cast<const void*>(view_records()[src_id - 1].resource));
}
for (UINT i = 0; i < count; ++i) {
const std::uint32_t old_id = to[i];
if (old_id && old_id - 1 < view_records().size() &&
view_records()[old_id - 1].slot == 18 && view_materialized()[old_id - 1])
retire_release_view_ref(reinterpret_cast<const void*>(view_records()[old_id - 1].resource));
}
std::memmove(to, from, u64{count} * 4);
} else {
for (UINT i = 0; i < count; ++i) {
const std::uint32_t old_id = to[i];
if (old_id && old_id - 1 < view_records().size() &&
view_records()[old_id - 1].slot == 18 && view_materialized()[old_id - 1])
retire_release_view_ref(reinterpret_cast<const void*>(view_records()[old_id - 1].resource));
}
std::memset(to, 0, u64{count} * 4);
}
} else if (from && count <= src_room) {
for (UINT i = 0; i < count; ++i) {
const std::uint32_t src_id = from[i];
if (src_id && src_id - 1 < view_records().size() &&
view_records()[src_id - 1].slot == 18 && view_materialized()[src_id - 1])
retire_leave_class(reinterpret_cast<const void*>(view_records()[src_id - 1].resource));
}
}
ReleaseSRWLockExclusive(&note_lock);
const UINT stride = title_descriptor_stride(dest_title, increment);
Expand Down
5 changes: 4 additions & 1 deletion src/startup_stubs/d3d12_draw.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2034,7 +2034,10 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI
return false;
}
if (texture && !upload_texture(self, resource, texture)) texture = nullptr;
if (texture) require_state(self, texture, sampled_state(self, texture));
if (texture) {
require_state(self, texture, sampled_state(self, texture));
if (resource && !resource->own().tiled_address_srv) retire_hold(self, resource);
}
const bool had_texture = texture != nullptr;
bool mismatched = false;
static LONG shown;
Expand Down
Loading
Loading