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
18 changes: 18 additions & 0 deletions docs/d3d12_command_objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,10 @@ shape and the shift around them, but the shape alone does not single the method
| 55 (`+0x1b8`) | **51** | `DiscardResource` | `(resource, null)` at `0x6dcb`, after the pending barriers are flushed through slot 26 |
| 59 (`+0x1d8`) | — | console only (shape) | four calls, two shapes: `(1, blob, size)` at `0x6b5a` and `0x6d53`, the blob a run of qwords whose first is `0xf3080 \| (qword count & 0x7f)`; and `(0, &qword, 8)` with the qword 1 at `0x6e52` and `0x6eb8`, only when a profiling bit of the context is set (`0x6e2a`). The first argument is set as a byte (`mov dl, 1` / `xor edx, edx`). It looks like one slot for both ends of a GPU event; no public method takes a bool first. With 55 at +4 and 61 at +2, the two slots the console lacks are among 56–60 |
| 61 (`+0x1e8`) | **59** | `ExecuteIndirect` | `(signature, 1, argument buffer, offset, null, 0)` at `0x59a0` |
| 71 (`+0x238`) | — | console only | `Write32BitValueBottomOfPipeX(address, u32 value, flush, flags)`: the 32-bit value, written into the title's memory once the list's work has run (the list values, [d3d12_xs_console_abi.md §5.5](d3d12_xs_console_abi.md)) |
| 72 (`+0x240`) | — | console only | `Write64BitValueBottomOfPipeX(address, u64 value, flush, flags)`: the 64-bit value; flags 1 ask for a GPU timestamp and 2 for a system timestamp instead of the value the call carried |
| 73 (`+0x248`) | — | console only | `Wait32BitValueX`: a wait on the 32-bit value; the list values hold the queue until it compares true or times out (§5.5 there) |
| 74 (`+0x250`) | — | console only | `Wait64BitValueX`: the same on the 64-bit value |
| 85 (`+0x2a8`) | — | `CopyTextureRegion` shape (inferred) | run: `(stack pointer, 0, 0)`, twice per dispatch. Statically six arguments, `(&dst, 0, 0, 0, &src, null)`: the public `CopyTextureRegion` (PC 16) with two `D3D12_TEXTURE_COPY_LOCATION`s, not one pointer. At `0x7147` dst is `{texture, SUBRESOURCE_INDEX, 0}` and src `{buffer, PLACED_FOOTPRINT, footprint}`, the footprint from device slot 38 (`GetCopyableFootprints`, `0x708b`); at `0x7191` both are subresource 0 (texture to texture). The caller also clears the next stack argument (`0x70e7`); whether the method takes a seventh is open, as is what console slot 16 is |
| 141 (`+0x468`) | — | console only | `(2, GPU VA, size, n)` at `0x65f8`: binds a buffer the DLL names `FFX_XBox_ComputeScratch`; `n` is the bound pipeline's scratch value from pipeline slot 11 (see above) |

Expand All @@ -573,6 +577,20 @@ So the console list has four more methods than the PC list between `ResourceBarr
Mapping the console list as "PC plus a constant" is wrong. The BVH builder adds 37, 39, 43 and 45 (the rest of the
+4 root group) and the console-only 71, 73, 80, 146 (`Barrier`) and 148 (sections 3.3 and 3.3a).

**The two value-write slots.** Slots 71 and 72 are the console's value writes and 73 and 74 the waits that go with
them. The slot numbers are measured, not read: the retail title's own trace calls slot 71 with a title address and
a value, slot 72 with a title address and the flags word and slot 74 with a wait's arguments, and the value writes
are at 71/72 rather than at 69/70; and `XboxBvhBuilder.dll` reads a 32-bit value write at `+0x238` (slot 71) and a masked wait at `+0x248`
(slot 73). The call shapes are xcl's (crates/xcl/src/hle/d3d12x/list_x.rs, GPL-2.0) and the deferred
bottom-of-pipe write model is VIctory's (crates/victory-gpu/src/list.rs, GPL-3.0): the write is not a GPU command,
it is a CPU write into the title's memory, made after the submission that recorded it has run. Both projects are
cited for facts and for the model only, no code copied. The runtime has two writers for them, one per list: the list
values (`runtime/shims/d3d12_xs_list_values.h`, on by default) for the lists they track, with the waits of slots 73
and 74; and the GPU-label path (`runtime/src/vulkan_gpu_labels.h`, `LEONIDA_GPU_LABELS=1`) for the others. Both
land a write whose address is a GPU address arena offset in the arena, never at that number in host memory, and one
on a page of xmem's that the title's view keeps `PAGE_NOACCESS` through xmem's own mapping of the physical page
(`runtime/src/value_write_sink.h`).

**The list object is wider than its vtable, and this is what that width is for.** Before a root-argument or dispatch
call the DLL checks a write cursor at `+0x18` against a limit (`+0x30` for root arguments, `+0x28` for a dispatch). While there is room,
it writes the command into that buffer itself instead of calling: a root argument is a header word from the
Expand Down
6 changes: 6 additions & 0 deletions docs/d3d12_xs_console_abi.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ The runtime's model:
reads half of it, and pinned like any host write, so the slot-133 shadows see it). Later than the console, never
earlier. Writes land in submission order across queues: the pump reads the fences newest write first, so a write
whose work waited for an older one is never seen done before it.
- Two targets that store cannot reach (`runtime/src/value_write_sink.h`). An address in the GPU address arena's
range is an arena offset, a default-heap buffer, not a host address: it is asked before any host store and lands
in the arena's own mapping (one in the arena's reserved start or a sparse buffer is dropped). A page of xmem's that
the title's view keeps `PAGE_NOACCESS` (a GPU-only window), where the store faults, lands through xmem's own
mapping of the physical page. Counted as `writes_arena` and `writes_title_pages`. In the boots to the story load no
write failed the host store (0 of 63 219), so both are a safety net.
- `write64` flags 1 and 2 take the GPU's timestamp at the write's place in the list: an `EndQuery(TIMESTAMP)` on
the real list (the Vulkan layer writes it at the bottom of the pipe) and a `ResolveQueryData` of that one slot into
a readback ring of the device's, read when the write lands. Flag 1 lands those ticks, which count at the queue's
Expand Down
9 changes: 9 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3086,6 +3086,15 @@ if(TARGET Vulkan::Headers)
target_compile_definitions(xbox_gpu_label_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX LEONIDA_VULKAN_DEVICE)
add_test(NAME xbox_gpu_label_completion COMMAND xbox_gpu_label_test)
set_tests_properties(xbox_gpu_label_completion PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 20)
# The list values' write fallbacks (src/value_write_sink.h): an arena offset lands in the arena, a PAGE_NOACCESS
# page of xmem's through xmem's own mapping. xmem.cpp and registry.cpp are compiled in for the xmem.dll exports.
add_executable(xbox_list_value_fallback_test test/xbox_list_value_fallback_test.cpp shims/d3d12_xs_device.cpp
shims/d3d12_xs_pso_stream.cpp shims/xmem.cpp shims/registry.cpp)
target_link_libraries(xbox_list_value_fallback_test PRIVATE gta6rt_d3d12_com psapi)
target_compile_definitions(xbox_list_value_fallback_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX LEONIDA_VULKAN_DEVICE)
target_link_options(xbox_list_value_fallback_test PRIVATE /OPT:NOREF)
add_test(NAME xbox_list_value_fallback COMMAND xbox_list_value_fallback_test)
set_tests_properties(xbox_list_value_fallback PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 30)
add_executable(d3d12_present_test test/d3d12_present_test.cpp)
target_link_libraries(d3d12_present_test PRIVATE gta6rt_d3d12_com user32 gdi32 dxgi)
target_compile_definitions(d3d12_present_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
Expand Down
2 changes: 1 addition & 1 deletion runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w
| `LEONIDA_PIPELINE_COMPILE` | unset | `on-demand` (the `--first-run-ui` profile sets it) prepares pipeline-state streams and sampled views at bind time: a placed 2D texture then waits for its memory until a command or a view first uses it. `cache-only` makes an eager build (`LEONIDA_LAZY_PIPELINES=0`) ask the driver's cache first and leaves what would have to be compiled to the first draw. `first-use` is accepted and means the default. When a pipeline is compiled is `LEONIDA_LAZY_PIPELINES`. |
| `LEONIDA_GRAPHICS_STAGE_LAYOUT` | on | Graphics root arguments are laid out as the console does for the vertex, pixel and geometry stages (from `s8`, a descriptor table in one dword). `0` keeps `rt::rootsig::pack()` (from `s0`, two dwords per address) for every graphics signature. |
| `LEONIDA_FRAME_PACING` | unset | `1` (the `--first-run-ui` profile sets it) hands device slots 78 to 81 (`SetFrameIntervalX`, `ScheduleFrameEventX`, `WaitFrameEventX`, `GetFrameStatisticsX`) to the device's own presentation once the first `PresentX` has built it, with the title's interval and origin event replayed to its pacer. Unset, the slots answer from the device's token counter with no wait. |
| `LEONIDA_GPU_LABELS` | unset | `1` records the labels of console list slots 71 and 72 and stores each into the title's memory when the GPU has finished the command buffer that carries it (`src/vulkan_gpu_labels.h`), for lists the list values (`LEONIDA_LIST_VALUES`, on by default) do not track: those land their slot 71 to 74 writes themselves, and a second writer could rewind a counter. Unset, the two slots of an untracked list do nothing. |
| `LEONIDA_GPU_LABELS` | unset | `1` records the labels of console list slots 71 and 72 and stores each into the title's memory when the GPU has finished the command buffer that carries it (`src/vulkan_gpu_labels.h`), for lists the list values (`LEONIDA_LIST_VALUES`, on by default) do not track: those land their slot 71 to 74 writes themselves, and a second writer could rewind a counter. Unset, the two slots of an untracked list do nothing. Both writers land a value at a GPU address arena offset in the arena and one on an xmem page the title sees as `PAGE_NOACCESS` through xmem's own mapping (`src/value_write_sink.h`). |
| `LEONIDA_LINEAR_MAP_FIRST` | unset | `Map` of a slot 133 texture leased as linear answers with the placement address and the title texture registry fills the image. `1` gives it the linear mapping instead: pinned pages, with a GPU-written texture read back into them, and a refusal when the pages are gone. |
| `LEONIDA_CONSOLE_D16S8` | by GPU | The console's `D16_UNORM_S8_UINT` family (DXGI 118 to 120) is made as the D24S8 family where the GPU has `D24_UNORM_S8_UINT` as a depth-stencil attachment and keeps its own D16/S8 path where it has none (AMD). `native` or `d24` chooses for every GPU. The device logs which it took. |
| `LEONIDA_RGBA4_COPY_REFUSE` | unset | `1` refuses a footprint copy to or from a `B4G4R4A4_UNORM` render target stored as RGBA8 (its texels are four bytes there, not two). Unset, the copy is made. |
Expand Down
43 changes: 43 additions & 0 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "d3d12_xs_native_pso.h"
#include "dxgi_com.h"
#include "../src/arena_size.h"
#include "../src/value_write_sink.h"
#include "../src/arena_blocks.h"
#include "../src/gfx10_descriptors.h"
#include "../src/depth24_fragment.h"
Expand Down Expand Up @@ -2720,6 +2721,33 @@ struct DeviceState {
return true;
}

// The console's value writes (list slots 71 and 72) whose address is an arena offset: a default-heap buffer.
// The arena is host-visible, so its host mapping holds the bytes the GPU reads. As in resolve_title_memory, an
// address below the arena's size is an arena address unless the title's own memory is there (arena_allocate
// keeps out of those ranges); an arena address is never stored to as a host address, so it is the arena's
// whether or not it can be written here (src/value_write_sink.h). Below the first offset the arena hands out are
// the null region, the zero page and the quad indices, which shaders read: a value there is refused, as is one
// past the end.
leonida::value_sink::Landing write_arena_value(std::uint64_t va, const void* bytes, unsigned size) {
using leonida::value_sink::Landing;
if (!arena.mapped || va >= arena.size) return Landing::elsewhere;
std::uint64_t title_lo = 0, title_hi = 0;
if (leonida::title_va_aliases().title_memory(va, title_lo, title_hi)) return Landing::elsewhere;
if (!bytes || !size || va < arena.first || size > arena.size - va) return Landing::refused;
// A sparse resource's memory is bound by the sparse pass, not by the arena's mapping.
if (context && context->is_sparse_va(va)) return Landing::refused;
// An aligned value in one store, so a reader never sees half of it.
if (size == 4 && !(va & 3))
std::atomic_ref(*reinterpret_cast<std::uint32_t*>(arena.mapped + va))
.store(*static_cast<const std::uint32_t*>(bytes), std::memory_order_relaxed);
else if (size == 8 && !(va & 7))
std::atomic_ref(*reinterpret_cast<std::uint64_t*>(arena.mapped + va))
.store(*static_cast<const std::uint64_t*>(bytes), std::memory_order_relaxed);
else
std::memcpy(arena.mapped + va, bytes, size);
return Landing::landed;
}

struct ShaderVisibleHeap {
VkDeviceSize end;
D3D12_DESCRIPTOR_HEAP_TYPE type;
Expand Down Expand Up @@ -2933,6 +2961,7 @@ struct DeviceState {
std::uint8_t* mapped = nullptr;
VkDeviceAddress address = 0;
VkDeviceSize size = 0;
VkDeviceSize first = 0; // the first offset handed out: below it are the null region and kArenaReserved
std::uint32_t memory_type = 0;
std::mutex mutex;
leonida::ArenaBlocks blocks; // offset -> size, guarded by mutex (src/arena_blocks.h)
Expand Down Expand Up @@ -10883,6 +10912,13 @@ class Device final
}
#endif
state_.context->set_depth_range_unrestricted(info.depth_range_unrestricted);
// The console's value writes (list slots 71 and 72) at an arena offset land in the arena
// (src/value_write_sink.h): the list values' pump and the GPU-label path ask this device before any host store.
leonida::value_sink::set_arena(
[](void* device, std::uint64_t address, const void* bytes, unsigned size) {
return static_cast<DeviceState*>(device)->write_arena_value(address, bytes, size);
},
&state_);
if (info.physical && state_.format_properties) {
state_.context->set_color_attachment_format(
[physical = info.physical, get_props = state_.format_properties](VkFormat format) {
Expand Down Expand Up @@ -11375,11 +11411,17 @@ class Device final
address.buffer = arena.buffer;
arena.address = vk.vkGetBufferDeviceAddress(state_.info.device, &address);
arena.size = info.size;
// The GPU address arena is the console's address space: the title names it by offset, so the label path
// has to find it (src/gpu_label_memory.h). Its address space starts at 0.
if (arena.mapped)
leonida::label::memory().track(0, static_cast<std::uintptr_t>(arena.size),
reinterpret_cast<std::uintptr_t>(arena.mapped), true);
{
// Under the arena's lock: an insert rebalances the tree and rewrites child links, which a walk on
// another thread must not see half done.
std::lock_guard lock(arena.mutex);
arena.blocks.reset(null_bytes + kArenaReserved, arena.size);
arena.first = null_bytes + kArenaReserved;
char check[2] = {};
size_t check_length = 0;
arena.check_all = getenv_s(&check_length, check, "LEONIDA_ARENA_CHECK") == 0 && check_length > 1 &&
Expand Down Expand Up @@ -11427,6 +11469,7 @@ class Device final
}

~Device() override {
leonida::value_sink::clear_arena(&state_); // first: it waits for a value write into the arena in progress
raw_images_.clear();
geometry_refresher_.reset(); // its refresh uses state_.geometry
title_filler_.reset(); // its fills use the title-texture registry
Expand Down
8 changes: 7 additions & 1 deletion runtime/shims/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3214,6 +3214,12 @@ std::uint64_t WINAPI list_slot(ListProxy* self, std::uint64_t a1, std::uint64_t
const unsigned width = Slot == 71 ? 4u : 8u;
const unsigned clock = Slot == 72 ? ((a4 & 1) ? 1u : ((a4 & 2) ? 2u : 0u)) : 0u;
auto target = leonida::label::memory().capture(a1, width);
if (!target.owner.lock() && rt::xmem::label_target_fn) {
// The pages the title maps are the title's memory too, and the xmem shim owns their page
// tables (runtime/shims/xmem.h).
leonida::label::Target mapped;
if (rt::xmem::label_target_fn(a1, width, mapped)) target = std::move(mapped);
}
if (target.owner.lock())
rt::d3d12::record_gpu_label(reinterpret_cast<ID3D12GraphicsCommandList*>(self->real),
{std::move(target), a2, clock});
Expand Down Expand Up @@ -6526,7 +6532,7 @@ const char* list_tail_slot_name(unsigned slot, unsigned char console_layout) {
X(waits_relayed) X(gates_held) X(gates_met) X(gates_timed_out) X(cuts) X(cuts_deferred) X(cut_failures) \
X(gates_short) X(demotions) X(late_met) X(bundle_ops) X(releases) X(release_us) X(release_us_max) \
X(pass_us_max) X(release_stalls) X(remaps_now) X(remaps_deferred) X(remaps_failed) X(remap_wait_us_max) \
X(stamps_gpu) X(stamps_cpu) X(labels_handed)
X(stamps_gpu) X(stamps_cpu) X(labels_handed) X(writes_arena) X(writes_title_pages)

ListValueStats list_value_stats() {
const list_values::Counters& c = list_values::counters();
Expand Down
9 changes: 9 additions & 0 deletions runtime/shims/d3d12_xs_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@
// CreateCommandList(0, type, allocator, initialState). The type is the first word of the Xbox desc; there
// is no node-mask argument in the console call (public HLE; the PC six-argument form differs). An riid the
// proxy does not answer (the console's own list IID) is answered with the proxy.
// slots 71/72 Write32BitValueBottomOfPipeX and Write64BitValueBottomOfPipeX: the console's value writes, with
// slots 73/74 (Wait32BitValueX, Wait64BitValueX) the waits that go with them. A list the list values
// track (d3d12_xs_list_values.h, LEONIDA_LIST_VALUES, on by default) records all four, lands each write
// once its part has run and holds its queue at a wait. Only for a list they do not track
// (LEONIDA_LIST_VALUES=0, bundles) does LEONIDA_GPU_LABELS=1 record slots 71/72 as GPU completion labels
// (runtime/src/vulkan_gpu_labels.h): one writer per list. Either writer lands a value at an arena offset
// in the arena, and one on a page of xmem's that the title's view keeps PAGE_NOACCESS through xmem's own
// mapping (runtime/src/value_write_sink.h).
// slots 78/79/80/81 SetFrameIntervalX, ScheduleFrameEventX, WaitFrameEventX, GetFrameStatisticsX. A presentation
// attached with set_presentation_handler (below) answers them; without one a per-device token state
// machine does, before the PC vtable is indexed: WaitFrameEventX returns at once with a fresh non-zero
Expand Down Expand Up @@ -476,6 +484,7 @@ struct ListValueStats {
std::uint64_t remaps_now, remaps_deferred, remaps_failed, remap_wait_us_max; // queue slot 31
std::uint64_t stamps_gpu, stamps_cpu; // write64 flags 1/2 landed with the GPU's timestamp, the CPU's clock
std::uint64_t labels_handed; // slot 71/72 writes given to LEONIDA_GPU_LABELS (lists not tracked here)
std::uint64_t writes_arena, writes_title_pages; // landed in the arena, through xmem's mapping (in writes_landed)
};
ListValueStats list_value_stats();
void reset_list_value_stats();
Expand Down
Loading
Loading