Skip to content

d3d12_xs: opt-in PresentX slot routing and windowless capture for the first trace - #6230

Open
Kam471 wants to merge 3 commits into
ItsNotRexed:mainfrom
Kam471:assist-presentx-capture
Open

Kam471 wants to merge 3 commits into
ItsNotRexed:mainfrom
Kam471:assist-presentx-capture

Conversation

@Kam471

@Kam471 Kam471 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

What

Opt-in routing of the trace-named Xbox queue methods and a windowless first-frame capture, so the first trace can name a PresentX slot and the next run can produce an image without a code change. The queue proxy still refuses every unconfigured slot past the public 19 with E_NOTIMPL.

Environment

Variable Meaning
LEONIDA_PRESENTX_SLOT=<n> Route queue slot n (19..127) as PresentX(planeCount, planes, params). Unset: the old E_NOTIMPL refusal.
LEONIDA_PRESENTX_SUSPEND_SLOT=<n> Optional. Route slot n as SuspendX(flags); returns S_OK.
LEONIDA_PRESENTX_RESUME_SLOT=<n> Optional. Route slot n as ResumeX(); returns S_OK.
LEONIDA_CAPTURE=<dir> On a routed PresentX, capture instead of presenting: no window, no swapchain, no WindowPresenter. Unset: a routed present is accepted and dropped.

The variables are read once, on the first queue call past the public chain, so the path is inert when they are unset. The first routed call is logged with the slot and the three argument words, e.g.

[rt] presentx: routed queue slots: PresentX=40 SuspendX=41 ResumeX=42 (windowless capture)
[rt] d3d12_xs: queue slot 40 routed to PresentX (args 0x1 0x... 0x0)
[rt] presentx: wrote ...\present-0-plane-0.ppm (8x8, BGRA)

Capture and guards

  • Plane count must be 0..2. A null plane array, a null ppResources, or a bad pointer returns E_INVALIDARG with one log line.
  • The plane array, each ppResources array and each resource pointer are checked with VirtualQuery for readable, committed memory before they are read; the resource's vtable (QueryInterface slot 0, GetDesc slot 10, GetHeapProperties slot 14) must be readable and executable. Guard pages and PAGE_NOACCESS never fault.
  • Before any ID3D12Resource method is called, the resource must answer the layer's private Resource tag (kLeonidaResourceTag, shared through runtime/shims/d3d12_object_tags.h and used by the layer's Resource class itself). A foreign object with a readable/executable vtable, or a host PC resource, is refused with E_INVALIDARG; its GetDesc is never called. The probe covers this with a synthetic foreign vtable.
  • The resource must be a single-sample, single-slice 2D texture, BGRA8 or RGBA8 (_UNORM/_SRGB), size 1..32768, created with D3D12_HEAP_FLAG_ALLOW_DISPLAY. Anything else gives E_INVALIDARG (one line) and no file.
  • Refusal lines are throttled per distinct message (one per 250 ms) so a mis-routed slot cannot flood stderr; repeats are counted and dropped.
  • Each plane with ResourceCount >= 1 takes ppResources[0], copies subresource 0 to a committed READBACK buffer through the presenting queue, and writes present-<n>-plane-<m>.ppm (P6, RGB; n counts routed presents including blanks, m is the plane). Source BGRA/RGBA is decoded per the resource desc.
  • The texture is assumed to be in D3D12_RESOURCE_STATE_PRESENT (the documented Xbox back-buffer state): the copy transitions PRESENT -> COPY_SOURCE -> PRESENT. The console's plane struct and 64-bit token are the existing provisional layouts in xbox_present_types.h; "committed" itself is not queryable through ID3D12Resource, so the enforceable part (desc + ALLOW_DISPLAY) is checked.
  • A zero-plane PresentX writes nothing and returns S_OK.
  • The capture mutex is held only around the GPU copy/readback/PPM write, after all validation. The whole feature is compiled into gta6rt and adds no dependency to the device proxy's test target beyond one new .cpp.

Tests

Built on 561b65a83 (RelWithDebInfo, MSVC 19.44.35228, Vulkan SDK 1.4.363.0), run on NVIDIA GeForce RTX 5070 Ti, driver 621.24, Windows 11 26200, LEONIDA_VK_DEVICE='RTX 5070 Ti'.

Target / test Result
gta6rt build OK
d3d12_present_test (d3d12_present) passed; 70 frames cleared/presented through the Xbox-ABI proxy
presentx_capture_probe capture passed: 1/1 present, 1 PPM present-0-plane-0.ppm, 203 bytes (11-byte P6 header + 8×8×3), all 64 texels match; blank + SuspendX + ResumeX S_OK
presentx_capture_probe malformed passed: 10/10 E_INVALIDARG, foreign GetDesc/heap calls 0, 40 repeated foreign calls still refused, no files, no crash
presentx_capture_probe inert passed: 1/1 E_NOTIMPL, no files
xs_proxy_mock + no_reserve + no_protect + no_handler 4/4 passed
d3d12_com, allocator_proxy, xbox_present, d3d12_compute passed
d3d12_boot_movie skipped 77 (needs LEONIDA_GAME_DIR/LEONIDA_MOVIE)

ctest over those 13 tests: 12 passed, 1 skipped, 0 failed. LEONIDA_VK_VALIDATION=1 on the capture probe: 0 validation errors/VUIDs.

The malformed set covers: null plane array, null ppResources, 3 planes, null resource, PAGE_NOACCESS plane array, PAGE_NOACCESS ppResources, PAGE_NOACCESS resource, a 2D texture without ALLOW_DISPLAY, a buffer, and a foreign object with a readable/executable vtable whose GetDesc must not run.

PPM check (first texel): expected BGRA (30,20,10,255) -> PPM RGB (10,20,30); the probe checks all 64 texels, not a sample.

Missing requested target

d3d12_mesh_compute_test does not exist at 561b65a83 (ninja: error: unknown target 'd3d12_mesh_compute_test'); it is present only on the unmerged fork/assist-4408-rebased branch. I did not cherry-pick unrelated mesh-compute work. The existing d3d12_compute_test was built and passed 1/1 as the nearest compute coverage.

Notes

  • Supplies the LEONIDA_PRESENTX_SLOT/LEONIDA_CAPTURE branch that docs: the 5-hour exe-day first-frame runbook #6198 describes as "if the branch lands".
  • Capture requires a Leonida layer ID3D12Resource (it answers the private layer tag). Host PC resources are deliberately refused: a readable/executable vtable alone does not prove the object is one of ours.
  • AI-assisted: yes.
  • No game data, traces, captures or logs are committed. The PPM is written to a temp directory by the probe.

… first trace

LEONIDA_PRESENTX_SLOT/_SUSPEND_SLOT/_RESUME_SLOT route only the named queue slot past the public 19 and log its first call; every other tail slot keeps E_NOTIMPL. LEONIDA_CAPTURE=<dir> makes a routed PresentX validate the plane count (0-2), the plane array, ppResources and each resource vtable with VirtualQuery, then copy ppResources[0] through a committed readback buffer on the presenting queue to present-<n>-plane-<m>.ppm (BGRA/RGBA8, ALLOW_DISPLAY). No window and no swapchain are created. Malformed calls return E_INVALIDARG with one log line; a zero-plane present writes nothing and returns S_OK. The presentx_capture_probe covers the PPM pixels, nine malformed arg sets and the inert E_NOTIMPL path.
@Kam471

Kam471 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Reviewer verification: built the PR head; all three probe modes pass on real hardware (8x8 BGRA pixel-exact PPM, malformed 9/9 E_INVALIDARG with no files, inert returns the old E_NOTIMPL). Also verified against the layer: the readback is a real GPU-timeline signal (d3d12_com.cpp:1447, :1218), the 30s-timeout Release past a still-pending copy is safe because destruction is deferred by timeline ticket (runtime/src/vulkan_backend.h:1408-14), PRESENT→COPY_SOURCE→PRESENT is FIFO-ordered behind the title's already-submitted work on the presenting queue, and the PPM channel order is right.

  1. presentx_capture.cpp:119-136, :158 — readable/executable ≠ identity. The checks prove the resource pointer, vtable and vtable[10]/[14] targets are readable/executable, not that the object is a Resource: a mis-routed slot whose args decode as a plausible plane struct still calls GetDesc/GetHeapProperties through a possibly-wrong vtable, and a callee that never writes leaves desc as uninitialized stack. The layer's own tag-QI identity check (d3d12_com_object.h:37, :117 — already used on title-supplied pointers at d3d12_com.cpp:1370) belongs here; with it the "never a fault" claim (presentx_capture.h:7, docs §4.5 new bullet) would hold for arbitrary slots. TOCTOU (a concurrent decommit between check and read) remains a theoretical residual either way.
  2. presentx_capture.cpp:107-109 — refuse() logs on every call while every routing log is once-only; a mis-routed slot called once per frame floods stderr, and presentx_capture.h:32 says "logged once".
  3. Probe gaps (not code bugs): no 2-plane or RGBA-source case, and nothing exercises the timeout path, so those are code-read only.

Verdict: merge, and safe to enable on exe day staged as documented (route → drop, then LEONIDA_CAPTURE) — the default with no env is verified inert, and this path is also the only way to measure the slot. But land (1) first: "never a fault" is the claim the slot guess leans on. Top risks: wrong-slot wrong-vtable call; the provisional plane layout even when the slot is right (xbox_present_types.h says it is not the console ABI); title-side handling of E_INVALIDARG/S_OK returned from a method it did not think it called.

…e refusals

A readable/executable vtable alone does not identify the object behind a mis-routed PresentX. check_resource now calls QueryInterface with the layer's private Resource tag (shared in d3d12_object_tags.h and used by the Resource class so they cannot drift) and refuses before GetDesc/GetHeapProperties unless the tag succeeds. The probe adds a foreign object with a readable/executable vtable and asserts its GetDesc is never called. Refusal lines are throttled per distinct message (one per 250 ms) so a mis-routed slot cannot flood stderr. The capture path now requires a Leonida layer resource; host PC resources are refused by design.
@Kam471

Kam471 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Reviewer finding applied in 255efe0 (delta on top of 7d52b0a).

What changed

  • Layer-object identity before any ID3D12Resource call. check_resource now checks vtable[0] (QueryInterface) executable as well as slots 10/14, then calls QueryInterface(kLeonidaResourceTag, ...). Only a successful tag QI is followed by GetDesc/GetHeapProperties; a foreign object is refused with E_INVALIDARG and its desc is never called. The tag is the existing Resource::kTag from d3d12_com_object.h, now shared through the private runtime/shims/d3d12_object_tags.h constant kLeonidaResourceTag and used by Resource itself so the two cannot drift. The Resource class stays private; no public interface changed. No blocker.
  • Refusal throttling. Each distinct refusal line is logged at most once per 250 ms; repeats are counted and dropped. Distinct failure modes still get their first line. Success lines are not throttled.
  • Probe. Added a synthetic foreign object with a readable/executable 15-entry vtable (slot 0 returns E_NOINTERFACE; slots 10/14 would return a texture desc/heap but must not be called). The probe asserts its QI was called, its GetDesc and GetHeapProperties were not called, and 40 further calls still return E_INVALIDARG.
  • Host PC resources are now refused by design: they do not answer the layer's private tag. The game path uses the Vulkan layer's resources, which do.

Counts (RTX 5070 Ti, driver 621.24, Windows 11 26200, LEONIDA_VK_DEVICE='RTX 5070 Ti')

  • capture: 1/1 present, 1 PPM present-0-plane-0.ppm (8x8 BGRA, P6, 203 bytes, all 64 texels checked); blank + SuspendX + ResumeX S_OK.
  • malformed: 10/10 E_INVALIDARG, foreign GetDesc/heap calls 0, 40 repeated foreign calls still E_INVALIDARG, no files, no crash. Log evidence: 10 distinct refusal lines for 50 refusal calls (the 40 repeats are throttled).
  • inert: 1/1 E_NOTIMPL, no files.
  • CTest over presentx + present/proxy/Xbox/compute: 13 tests — 12 passed, 1 skipped, 0 failed (skip is package-gated d3d12_boot_movie).
  • d3d12_present: passed, 70 frames.
  • LEONIDA_VK_VALIDATION=1 capture probe: 0 validation errors/VUIDs.

The requested d3d12_mesh_compute_test still does not exist at base 561b65a83 (only on fork/assist-4408-rebased); d3d12_compute_test passed 1/1 as before.

@Razdva122 Razdva122 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found two ways windowless capture can report success without a completed image at 255efe094.

  1. At runtime/shims/presentx_capture.cpp:367, if the device is lost after Signal succeeds, this layer's Fence::GetCompletedValue returns UINT64_MAX (d3d12_com.cpp:1219). The polling loop treats it as reaching 1. With a stub fence returning that sentinel, the code proceeded to the Map branch. Check the sampled value for removal before comparing it with 1, and return a failure before mapping or writing. D3D12 fence return contract
  2. At runtime/shims/presentx_capture.cpp:268, out.good() is evaluated before std::ofstream closes. The first buffered write error can surface during close. For the same 8×8 RGBA input, the writer returned true for both a healthy 203-byte file and a zero-byte file when I imposed a zero-byte process file-size limit. The caller would log a successful capture for either result. Explicitly close the stream before testing its state so a write failure reaches the caller.

I compiled the writer and polling block from this head with Apple Clang on macOS arm64. These were synthetic CPU probes. AI-assisted review.

Kam471 added a commit to Kam471/Leonida that referenced this pull request Nov 23, 2026
The kit now ships gta6rt-final.exe (base ba4b0bd + ItsNotRexed#3777 + ItsNotRexed#5867/ItsNotRexed#5808/ItsNotRexed#5851 + ItsNotRexed#6230 @ 255efe0 + ItsNotRexed#6107/ItsNotRexed#6149 + runtime layer 7bce00b + ItsNotRexed#6351 XMemTransferMemory). Prefer it, fall back to gta6rt-final-pre-xmem.exe, then to the trace-only gta6rt.exe; verify the selected binary's hash and the five strings that decide capture and the Series startup path. Add the discover-presentx-slot.ps1 helper and its launch-exeday.ps1 bin caveat.
HDR10 planes present as DXGI_FORMAT_R10G10B10A2_UNORM; dxgiformat.h has no
_SRGB variant. Accept it in check_resource and decode each little-endian
32-bit texel (R bits 0..9, G 10..19, B 20..29) to 8-bit RGB in write_ppm.
The existing Width*4 footprint check already covers its 4-byte texels.

Extend presentx_capture_probe capture mode with an 8x8 HDR10 texture whose
quadrants are red, green, blue and a mid 512/511/513 grey, and check the
second PPM's bytes.
@Kam471

Kam471 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Extended the windowless capture to HDR10 planes at head 03df5238f (branch assist-presentx-capture).

  • check_resource now also accepts DXGI_FORMAT_R10G10B10A2_UNORM. dxgiformat.h has no R10G10B10A2_UNORM_SRGB enum, so there is no SRGB variant to add. BGRA8/RGBA8 paths are unchanged.
  • write_ppm decodes the little-endian 32-bit texel as R bits 0..9, G 10..19, B 20..29 and rounds with (v * 255 + 511) / 1023. The PPM stays 8-bit, so PQ HDR looks dark by design; noted in the comment.
  • Readback footprint is still Width * 4; all accepted formats are 4 bytes/texel, and the row-pitch check is asserted by the new case.
  • presentx_capture_probe capture adds an 8x8 R10G10B10A2_UNORM plane with red/green/blue/mid (512,511,513) quadrants and checks the second PPM bytes.

Counts: capture 2/2 presents, 2 PPMs exact; malformed 10/10 E_INVALIDARG, 0 foreign GetDesc calls, 0 files; inert 1/1 E_NOTIMPL, 0 files; d3d12_present 70 frames passed; probe capture with LEONIDA_VK_VALIDATION=1 2/2, no validation errors. git diff --check clean.

@thehollowedone

Copy link
Copy Markdown

Independent native Windows check of 03df5238. RTX 3080 Ti, NVIDIA 621.35, MSVC 19.44, Vulkan SDK 1.4.363.0.

Both synthetic image cases from presentx_capture_probe capture passed. The BGRA and HDR10 captures each produced an 8 × 8, 8-bit PPM with all 64 pixels matching. The HDR10 midpoint codes (512, 511, 513) rounded to (128, 127, 128). Khronos core and synchronization validation reported zero errors. The run used the published runtime and covered only the two image captures.

AI-assisted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants