Skip to content
Open
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
47 changes: 41 additions & 6 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -1707,8 +1707,30 @@ The copies:
(logged once otherwise).
- Any **other address in the title's memory** gets a window: the 64 KiB-aligned pages it covers, clipped to the
title's range, copied into the arena now and before every submission, through the same shadow refresh as slot 133
(so dirty-page tracking for that refresh applies to both). Windows stay until the device goes, up to
`LEONIDA_TITLE_WINDOWS_MB` (64 MiB by default). A window that
(so dirty-page tracking for that refresh applies to both). A window never covers bytes an existing range already
serves: it stays inside the free span around the address (and a stream-pool extent inside the free span in both of
the pair's views), because a range entered over a live one replaces it whole, and the copy that range answered from
would be left reached by nothing. Windows stay while an address range still answers from them, up to
`LEONIDA_TITLE_WINDOWS_MB` (64 MiB by default). A window that no range answers from any more (a slot-133 buffer the
title placed over its pages, or a BVH input's exact copy, replaced its ranges) is **retired** after the next
submission's refresh: the alias table's listener records the removed ranges' copies, and a window none of its
ranges is left for loses its stream-pool extent and its bytes of the budget at once. Its memory is freed once every
`ExecuteCommandLists` replay in progress then has recorded its submissions, every command list whose recording
began before the retirement is gone (reset, released, or a held batch's or the async worker's copy of it run), and
the submissions recorded by then have completed: a list resolves its root views when they are recorded, so one
recorded before the retirement and executed, or executed again, after it still names the window
(`src/command_list_epochs.h`). Until then its copy stays a target of its pages' tracker, refreshed like the others,
and what the GPU wrote in it goes back to the title's pages: with its submissions' write-back, and once more when it
is freed (a range noted when a list was recorded can be copied back before a held batch has run). A retired copy goes
back only on the bytes no live copy over them changed, byte by byte (the window and the buffer that replaced it can
each have written their own bytes of one 256-byte piece), and every copy is compared with the title's bytes as
they were before any copy went back. The bytes a retired copy returns are also written into every other copy over
them that still held the title's older bytes, as the next refresh would: a window is freed after its submission's
refresh, and the range that replaced it reads its last bytes in that same submission. `d3d12_title_window_orphans` covers it, with a writing batch held on a fence
while the next submission's refresh retires its window. A window is
not retired when its pages are released or remapped: a retail run measured no release at all of the pages windows
cover (the title remaps its pool frames instead), and the tracker follows the pages.
A tracker the window shared with a copy still live keeps its extent (a retirement never splits one). A window that
cannot fit between the title's address reservations can use a separate mapped buffer. The buffer's device address
must fit as a positive 47-bit offset from the arena base. The same window cap and dirty-page refresh apply.
`LEONIDA_STREAM_BUFFER_WINDOWS=0` requires the window to fit in the arena.
Expand All @@ -1721,7 +1743,7 @@ The copies:
title itself wrote in between keeps the title's newer bytes and its GPU result is dropped. The copy back asks the
write watch first (the refresh's own asking comes after it, so a hot page's last answer is a submission old), passes
over pages whose mapping changed, that a kernel write may be filling or that xmem maps at another address too, and
writes only the 256-byte pieces where the copy differs from the title's bytes: a read-only view writes nothing and
writes only the bytes where the copy differs from the title's (compared in 256-byte pieces): a read-only view writes nothing and
its pages stay guarded. The copy runs under the tracker's lock and can still fault on a page another guard keeps
read-only (the geometry watch, another view's shadow): the fault handler asks every tracker over the address, this
one included, and on the thread whose copy faulted it does not take that lock again (an SRW lock is not
Expand All @@ -1738,9 +1760,22 @@ The copies:
loses the device. The refusal and the count of zero-size views are logged.
- A **stream pool** (the title's CPU view paired with a 16-24 GiB GPU-only
alias) is not windowed per request. The GPU-only pages are translated to the CPU view of the same frames (rule B in
that note), and **one mapping serves a page-aligned readable run** of the pool (at most 1 MiB, a geometry copy when
the cap allows, a window otherwise). Both addresses of the pair are entered in the table, so a V# naming either
reaches that one mapping instead of asking for a second one; `LEONIDA_GEOMETRY_MB` is the preferred backing.
that note), and **one mapping serves a page-aligned readable run** of the pool (at most 1 MiB, and never past the
next mapping in either view; a geometry copy when the cap allows, a window otherwise). A request whose byte a
mapping already holds in either view is served by it when the mapping holds all of the request's bytes, and the
other view is entered as far as that mapping reaches. A request never gets a mapping shorter than its bytes (a V#'s
size, 64 KiB for a root view): one that runs into the next mapping, or past the end of the one holding its first
byte, gets one mapping over itself and every window it reaches, each whole, in both views (`title_pool_cover`,
`TitleVaAliases::replace_twin`), and those windows are retired at the next submission. `replace_twin` checks, under
the lock it replaces them under, that every range it would erase is one of those windows: a slot-133 buffer is
entered without the window lock, so one placed in the span after the plan makes it refuse, the new window is dropped
and the request falls back as below. Only with a range in the way
that is not a window (a slot-133 buffer, a geometry copy) or that reaches past the pool in its view, or pages that are
not readable, does it get a zero-size
view: a refusal there would also have the root views a title rings through a pool read zeros where it served them
before. `vsharp_address` asks again, like `TitleVaAliases::resolve`, when the range holding a V#'s base ends before
the V# does. Both addresses of the pair are entered in the table, so a V# naming either reaches that one mapping instead of
asking for a second one; `LEONIDA_GEOMETRY_MB` is the preferred backing.
Acceleration-structure destinations and scratch also resolve through the table, and a structure no window holds
whole gets a copy of its whole range, so the BVH builder's commands are recorded.
- `LEONIDA_GEOMETRY_MB` sets the device-local memory cap for streamed buffer views. The default is 0.
Expand Down
9 changes: 9 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3999,6 +3999,15 @@ if(TARGET Vulkan::Headers)
# the title's pages at the next refresh, and the title's own newer words still win (#1537, WRITE_INVALID).
add_test(NAME d3d12_title_window_write_back COMMAND d3d12_title_va_test --window-writeback)
set_tests_properties(d3d12_title_window_write_back PROPERTIES SKIP_RETURN_CODE 77)
# No window nothing reaches: a stream-pool request just before an extent stops where it starts, and a window a
# slot-133 buffer replaced is retired at the next submission, its bytes returned and its memory freed.
add_test(NAME d3d12_title_window_orphans COMMAND d3d12_title_va_test --window-orphans)
set_tests_properties(d3d12_title_window_orphans PROPERTIES SKIP_RETURN_CODE 77)
# The same with no per-submission copy back: a retired window's GPU writes reach the title's pages only when it
# is freed, on the bytes no live copy changed (a buffer over the same 256-byte piece wrote the others).
add_test(NAME d3d12_title_window_orphans_free COMMAND d3d12_title_va_test --window-orphans)
set_tests_properties(d3d12_title_window_orphans_free PROPERTIES SKIP_RETURN_CODE 77
ENVIRONMENT "LEONIDA_TITLE_WRITEBACK=0")
# A write-back copy that faults on a page another guard (a geometry watch cell) keeps read-only: the fault
# handler asks the tracker whose copy faulted too, which must not take its lock again on that thread (#2418).
add_test(NAME d3d12_title_window_write_back_fault COMMAND d3d12_title_va_test --window-writeback-fault)
Expand Down
6 changes: 6 additions & 0 deletions runtime/shims/d3d12_com.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,12 @@ bool title_shadow_buffer(ID3D12Resource* resource);
std::uint64_t title_host_imports(ID3D12Device* device);
// VirtualQuery calls made by the title-buffer shadows in their latest refresh. For tests.
std::uint64_t title_shadow_refresh_queries(ID3D12Device* device);
// The device's title windows now: how many and their bytes, how many were retired with their pages, and how many of
// those still wait for the GPU before their memory is freed. For tests.
struct TitleWindows {
std::uint64_t count = 0, bytes = 0, retired = 0, waiting = 0;
};
TitleWindows title_windows(ID3D12Device* device);
// The trackers behind the title-buffer shadows and title windows (copies whose pages overlap or touch share one).
// For tests.
std::uint64_t title_shadow_trackers(ID3D12Device* device);
Expand Down
12 changes: 12 additions & 0 deletions runtime/shims/d3d12_geometry_views.h
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,18 @@ class GeometryViews {
executing_.push_back(++execute_serial_);
return execute_serial_;
}
// The last replay begun, and the oldest still in progress (kAfterExecutes when none): what execute_end stamps by,
// for a copy the device retires the same way outside these views (a title window, DeviceState::retire_windows).
std::uint64_t execute_epoch() {
std::lock_guard lock(execute_mutex_);
return execute_serial_;
}
std::uint64_t oldest_execute() {
std::lock_guard lock(execute_mutex_);
std::uint64_t oldest = kAfterExecutes;
for (const auto s : executing_) oldest = (std::min)(oldest, s);
return oldest;
}
// The replay that began with `serial` has recorded its submissions. Views retired while only replays now finished
// were in progress get submitted() (the context's submitted_retire_ticket(), read here, after those replays
// recorded theirs) as their retire ticket. Lock order: mutex_ -> execute_mutex_, as in retire_range.
Expand Down
Loading
Loading