diff --git a/docs/experiments/startup-root-srv-at-draw.md b/docs/experiments/startup-root-srv-at-draw.md new file mode 100644 index 0000000000..51f7f6c552 --- /dev/null +++ b/docs/experiments/startup-root-srv-at-draw.md @@ -0,0 +1,100 @@ +# Graphics root SRV: no desktop copy that no draw reads (opt-in) + +Refs [#2948](https://github.com/R0Y00/Xbox2Windows/issues/2948), +[#2949](https://github.com/R0Y00/Xbox2Windows/pull/2949). + +List slot 44 (`SetGraphicsRootShaderResourceView`) at a title address without an upload mapping copies a 64 KiB +window of title memory into the shared upload ring and binds it on the desktop list (`list_root_srv`). In the current +code no draw reads that desktop binding: + +- Translated draws set their own root signature and read the SRV from its title address (`pass.root_address`, + `source == 2` in `draw_translated`) at the draw. +- `list_draw` forwards a draw only with `pass.pipeline_real`, which `list_pipeline` sets for a desktop pipeline + without an indirect graphics root. `build_stream_pipeline` accepts a graphics stream only with VS/PS or MS/PS + ("desktop indirect graphics stream needs ...") and gives every accepted one that root, so `pipeline_real` holds only + for compute pipelines. A vertex-only desktop stream is refused by the same rule. +- An indirect graphics draw (list slot 61) requires `pass.indirect_graphics.root == pass.graphics_root`: an empty + root signature (`root_without_bindings`), or for a cached mesh pipeline the title's root, whose only root SRV + (`cached_srv_root`) the draw binds from its validated lease. + +## What the switch changes + +- `X2W_D3D12_ROOT_SRV_AT_DRAW=1`: slot 44 skips that copy. The title address is kept for translated draws as before, + and the parameter stays unbound on the desktop list (`srv_bound` clear), so a forwarded `list_draw` that needs bit 0 + is skipped rather than drawn with an older binding. A desktop graphics pipeline that reads a root SRV, if one is + added, would need the copy at its draw. +- With the switch set, the render totals are followed every 300 frames by + `x2w-render root SRV copies (X2W_D3D12_ROOT_SRV_AT_DRAW): skipped N, copied M`. +- Unset, slot 44 copies at every call, as before. + +## Fixture + +```powershell +ctest --preset release --no-tests=error -j 1 -R '^startup_root_srv_draw_(warp|hardware)$' -V +``` + +`startup_root_srv_draw_*` (`X2W_TEST_ROOT_SRV_DRAW=write-after`, `tests/startup_root_srv_draw.cpp`): the stream +root's parameter 0 is a root SRV. Slot 44 binds a `VirtualAlloc` buffer holding red, the title writes green, then a +translated draw reads the SRV through its root pointer. Both modes draw green over the left half. After the draw is +recorded the fixture reads the stand-in's counts (`x2w_d3d12_root_srv_copies`): unset, `skipped 0, copied 1`; set, +`skipped 1, copied 0`. Mutant: ignoring the switch fails +`X2W_D3D12_ROOT_SRV_AT_DRAW=1: copies (skipped, copied) (0, 1), expected (1, 0)`. + +Not covered: a desktop draw that reads the binding, because none can be built (see above). + +## Startup run of the main executable + +Local diagnostic runs, Windows 10 22H2 (19045), RTX 3080 Ti, NVIDIA driver 32.0.16.1047, i7-4790K, 16 GB, release +build on `main` `98e4a2ee`, warm shader cache (revision 29), scripted input, 250 s. One build; only the switches +differ. Aggregates only; logs stay local. The first two tables were measured with the first revision of this change +(`bedfcafc`), which kept the address pending for a desktop draw instead of skipping the copy; no run made a copy at +a draw, so slot 44 did the same work as now. + +Each switch alone, against no switch: + +| | none | `ROOT_SRV_AT_DRAW` | `RING_RELEASE_AT_SUBMIT` | +|---|---|---|---| +| null bindings with cause `buffer` | 164,727 | 88 | 0 | +| `upload ring reservation refused` logged | yes | yes | no | +| visible | single black frames in the post-load animation | none seen | not watched | + +With `X2W_D3D12_RING_RELEASE_AT_SUBMIT=1` and `X2W_D3D12_TABLE_ALIAS=1` set in every run, two pairs, the second in +reverse order: + +| | switch unset | switch set | +|---|---|---| +| mean fps 150–240 s, pair 1 / pair 2 | 6.3 / 7.1 | 14.8 / 14.0 | +| frames presented, pair 1 / pair 2 | 3,663 / 3,541 | 4,689 / 4,804 | +| executes, pair 1 / pair 2 | 33,408 / 36,029 | 72,177 / 70,916 | +| slot 44 time 150–240 s (threads; µs per call) | 1.14–1.19; 43–49 | 0.28–0.29; 5 | +| copies not made at slot 44 / made at a draw | n/a | about 7 million / 0 | +| null bindings `token` per 1,000 translated draws | 33.8 / 32.3 | 36.7 / 26.9 | +| null bindings `buffer`, device removal | 0, none | 0, none | +| process commit peak | 19.1 / 19.0 GiB | 19.8 / 19.4 GiB | + +- The slot 44 copies were most of the upload ring traffic that #2948 describes: the switch alone removes all but + 88 of the buffer null bindings, but some reservations are still refused, so it complements #2949 rather than + replacing it. +- Without the table alias the frame rate barely changes (5.9 → 6.1 fps): the draw path is then bound by `frame_copy` + under `draw_lock`. +- Every run read `deferred N, made at a draw 0`: no draw in these runs read the desktop binding. + +Retest of this revision (merged with `main` `3b6c38a8`), same machine, cache and input, ring and alias set in every +run, two pairs, the second in reverse order: + +| | switch unset | switch set | +|---|---|---| +| mean fps 150–240 s, pair 1 / pair 2 | 6.3 / 6.5 | 13.9 / 13.4 | +| frames presented, pair 1 / pair 2 | 3,839 / 3,706 | 4,798 / 4,960 | +| executes, pair 1 / pair 2 | 24,065 / 33,220 | 69,730 / 54,789 | +| slot 44 time 150–240 s (threads; µs per call) | 1.17 / 1.11; 52 / 48 | 0.29 / 0.29; 5 / 6 | +| copies skipped / copied (render totals) | n/a | 6.9 / 4.9 million; 0 | +| null bindings `token` per 1,000 translated draws | 23.4 / 29.8 | 24.2 / 33.0 | +| null bindings `wrapper`, pair 1 / pair 2 | 0 / 40 | 107 / 101 | +| null bindings `buffer`, ring refusals, device removal | 0, none, none | 0, none, none | +| process commit peak | 18.7 / 18.5 GiB | 18.7 / 18.7 GiB | + +- `wrapper` null bindings also appear in an unset run (40). Per 1,000 translated draws they are 0 / 0.26 unset and + 0.37 / 0.40 set: few, but not explained here. + +Not established: desktop graphics pipelines that read a root SRV (none can be built today), and other adapters. diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 886a6dc4fd..ec8cb7b77c 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -687,6 +687,13 @@ one on the desktop list, as before; a flagged one is deferred (`root signature . the desktop list`, first three). Root SRVs (slot 44) are not forwarded under a deferred root. Translated draws use their own root signature (no flags) and are unaffected. +A root SRV (slot 44) at a title address without an upload mapping copies a 64 KiB window of title memory into the +upload ring for the desktop binding. No draw reads that binding today: translated draws set their own root signature +and copy the SRV from its title address at the draw, a forwarded slot 12 draw needs a desktop pipeline without an +indirect graphics root (only compute pipelines), and an indirect graphics draw binds a pipeline over the list's root. +With the opt-in `X2W_D3D12_ROOT_SRV_AT_DRAW=1`, slot 44 skips the copy and leaves the parameter unbound on the desktop +list (`startup_root_srv_draw_*`; see [the experiment note](experiments/startup-root-srv-at-draw.md)). + A real draw under a deferred root is decided by `src/startup_stubs/heap_indexed_root.h`: it may bind only when the list holds a mirror of every heap the root indexes, covering the indices the draw reads, and every root constant, descriptor table and root descriptor can be set again. A one-entry sampler heap covers only index 0 and never stands @@ -1502,6 +1509,7 @@ ctest --preset dev --no-tests=error -R '^startup_trap_header(_prefix_control|_pr | `X2W_SHADER_CACHE=` | translation cache: the runtime writes source requests `req_.{a,b,root}`, when needed RTV variants `req_.rtv`, and for device slot 67 compute pipelines `req_.{desc,sa,sb,data,root,c67}` (see [Compute pipeline requests](#compute-pipeline-requests)). A console pipeline that an `ExecuteIndirect` draw (list slot 61) refuses writes the same request as a direct draw, once per pipeline, and draws nothing (`startup_indirect_requests_warp`). Only direct draws poll for its manifest, so such a pipeline stays in the `missing` count until a direct draw uses it; the `x2w-render totals` line counts `pipelines first looked up by ExecuteIndirect N`. This is a per-process lookup count. Check for the ready marker `req_.a` to confirm a complete request is available. Run `tools/startup/gfx2hlsl.py --pending ` while the process is running, or start the run with `run.ps1 -Translate` (see [Translating shaders during a run](#translating-shaders-during-a-run)). Active draws poll for an absent manifest at most once per second per pipeline and use the published translation on a subsequent draw. The translator compiles `.vs.cso` and `.ps.cso` with `fxc.exe`: `X2W_FXC`, else the Windows SDK 10.0.26100.0 copy, else the newest `Windows Kits\10\bin\\x64`, else `PATH`. When none exists it writes `.fail` naming `X2W_FXC` without publishing a new manifest. `X2W_FXC=` (empty) publishes the manifest alone. Rerun `--pending` with compilation enabled to fill in missing bytecode for an existing manifest. | | `X2W_D3D12_LINEAR_BUFFERS=1` or `=copy` | opt-in backing for linear buffers placed at a title address (device slot 133, initial state `COMMON`): shared where the adapter, extent and memory allow it (private allocations larger than 256 MiB are skipped only with `X2W_D3D12_CAP_EXISTING_HEAP=1`), otherwise copied from title memory at each submission, otherwise the default backing is kept and the refusal recorded; `copy` never shares. Unset or `0`, slot 133 keeps its default backing. See [Linear buffer backing (device slot 133, opt-in)](#linear-buffer-backing-device-slot-133-opt-in) | | `X2W_D3D12_CAP_EXISTING_HEAP=1` | opt-in 256 MiB (`unplaced_existing_heap_allowed`) allocation-size cap on `OpenExistingHeapFromAddress` in device slot 133 (`startup_slot133_heap_cap_*`). Unset (default), private allocations larger than 256 MiB keep the formerly aliased path: a capped copied placement is refused by `CopyBufferRegion` (list slot 15) as `title_backed_source`, losing a late guest write made after recording and before submit. Set, allocations larger than 256 MiB skip `OpenExistingHeapFromAddress` so multi-GiB startup blocks are not pinned in `WorkingSetSize` and `NonLocal` GPU memory | +| `X2W_D3D12_ROOT_SRV_AT_DRAW=1` | opt-in, render mode: a graphics root SRV (slot 44) without an upload mapping is not copied into the upload ring, since no draw reads that desktop binding (default: a 64 KiB copy at every slot 44 call). Translated draws copy the SRV themselves and are unaffected (`startup_root_srv_draw_*`). See [the experiment note](experiments/startup-root-srv-at-draw.md) | | `X2W_D3D12_RING_RELEASE_AT_SUBMIT=1` | opt-in, render mode: a closed command list releases its upload ring chunks once its submit is fenced instead of holding them until Reset, and the ring grows in 16 MiB chunks up to 512 MiB (default: doubling chunks up to 256 MiB, holds until Reset). The larger ring can add up to about 1 GiB of process commit at peak (startup runs in the experiment note). A later submit of the same list before its Reset fences its unchanged chunks again; if one was reused in between, that list is refused before the execute and logged (`startup_address_console_gate_ring_keep_*`, `upload_reservation`). See [the experiment note](experiments/startup-upload-ring-release-at-submit.md) | | `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 | @@ -3479,7 +3487,7 @@ Buffers in `UNORDERED_ACCESS` or acceleration-structure state, and textures, kee A shared or copied placement logs `x2w-d3d12 Device::slot133(CreatePlacedResourceX?) linear buffer ... existing-heaps=
share=
->
shared|copied at submit`. A kept default logs the usual slot 133 line with `(kept default backing: linear refusal
, use=... type=... existing-heaps=
share=
)` appended. `x2w_d3d12_linear_buffer_info` reports path 3 with `refusal_hr`, and `stale`, `refresh_failures` and `last_refresh_hr` for a copied buffer. A refused submission logs `x2w-d3d12 linear buffers: submission on queue refused: its refresh would have to wait for queue (), so it has no safe completion order; none of its lists ran` (the first eight). `x2w_d3d12_linear_buffer_totals` counts `kept_default`, `refresh_failures` and `unsafe_refresh_refusals`. Fixtures read the same facts through `x2w_d3d12_linear_buffer_info` and `x2w_d3d12_linear_buffer_totals` (`src/startup_stubs/linear_buffer_report.h`). Fixture-only switches: `X2W_TEST_EXISTING_HEAPS_UNSUPPORTED=1` makes the feature query report no support, as an adapter with `EXISTING_HEAPS = 0` does. `X2W_TEST_LINEAR_REFRESH_FAIL=` makes staging requests 2, 2 + n, 2 + 2n and so on fail with `E_OUTOFMEMORY`. `X2W_TEST_LINEAR_STAGING_LIMIT=` shrinks the staging pool from 256 buffers to `n`. `x2w_d3d12_linear_buffer_internal_queue` hands fixtures the internal queue, so they can hold refreshes in flight behind a fence of their own. `x2w_d3d12_linear_buffer_test_pause` parks the next refresh at one point (before staging, after staging, or after its copy executed and before it is published), so a fixture can order two threads' refreshes or release a buffer while one is parked. Point 3 instead parks the next title `Wait` (queue slot 17) after the desktop `Wait` and before it is recorded, while it holds that queue's order lock. `X2W_TEST_LINEAR_BUFFER_MUTANT=` removes one guarantee for a negative control. Results and commands: [Production slice: slot 133 linear buffers](experiments.md#production-slice-slot-133-linear-buffers-1914). -Not covered: table SRVs of copied buffers in translated draws still take `frame_copy` at record, and root SRVs (`list_root_srv`) still take `upload_from_title` at bind. Neither reads the slot 133 backing. Shared buffers and their heaps live for the process, like every wrapper, so a private range under one cannot be released. +Not covered: table SRVs of copied buffers in translated draws still take `frame_copy` at record, and root SRVs (`list_root_srv`) still take `upload_from_title` at bind (not with `X2W_D3D12_ROOT_SRV_AT_DRAW=1`). Neither reads the slot 133 backing. Shared buffers and their heaps live for the process, like every wrapper, so a private range under one cannot be released. ## Buffer memory SRVs (device slot 70) diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 9976d39e39..f39cf7f04d 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -3329,6 +3329,15 @@ bool split_indirect_dispatch_enabled() } // The guest method has independent resource and byte-offset arguments. +// X2W_D3D12_ROOT_SRV_AT_DRAW=1 (opt-in): slot 44 makes no desktop copy of a root SRV without an upload mapping; the +// translated draws read it from its title address at the draw (list_root_srv). +bool root_srv_at_draw() +{ + static const bool on = GetEnvironmentVariableW(L"X2W_D3D12_ROOT_SRV_AT_DRAW", nullptr, 0) != 0; + return on; +} +LONG root_srv_copies_skipped, root_srv_copies_made; + void __stdcall list_execute_indirect(Wrapper* self, void* signature, UINT count, void* argument, u64 offset, void* count_buffer, u64 count_offset) { @@ -4147,6 +4156,17 @@ void __stdcall list_root_srv(Wrapper* self, UINT parameter, u64 address) pass.srv_bound |= 1u << parameter; return; } + // Without an upload mapping a 64 KiB window of title memory is copied into the upload ring for the desktop binding. + // No draw reads that binding: translated draws set their own root signature and read the SRV from root_address at + // the draw; every desktop graphics pipeline is an indirect one (build_stream_pipeline sets its root), so list_draw + // forwards only with a compute pipeline; and an indirect graphics draw binds a pipeline over the list's root, an + // empty one or, for a cached mesh pipeline, the title's root, whose only SRV it binds from its lease. With + // X2W_D3D12_ROOT_SRV_AT_DRAW=1 the copy is skipped and the parameter stays unbound (srv_bound clear). A desktop + // graphics pipeline that reads a root SRV would need the copy at its draw. + if (root_srv_at_draw()) { + InterlockedIncrement(&root_srv_copies_skipped); + return; + } const std::size_t bytes = readable_window(address, 0x10000); const D3D12_GPU_VIRTUAL_ADDRESS desktop = bytes ? upload_from_title(self, address, bytes) : 0; static LONG logged; @@ -4156,9 +4176,19 @@ void __stdcall list_root_srv(Wrapper* self, UINT parameter, u64 address) log("x2w-render root SRV %u at title 0x%llx: %zu bytes -> desktop 0x%llx [%s]", parameter, address, bytes, desktop, head); } if (!desktop) return; + InterlockedIncrement(&root_srv_copies_made); list->SetGraphicsRootShaderResourceView(parameter, desktop); pass.srv_bound |= 1u << parameter; } + +// Fixture export (startup_root_srv_draw): slot 44 root SRVs without an upload mapping since the process started. +// `skipped` counts only with X2W_D3D12_ROOT_SRV_AT_DRAW=1; `copied` counts every copy into the upload ring. +extern "C" __declspec(dllexport) void x2w_d3d12_root_srv_copies(LONG* skipped, LONG* copied) +{ + if (skipped) *skipped = root_srv_copies_skipped; + if (copied) *copied = root_srv_copies_made; +} + // List slot 42 (desktop 38 + 4), SetGraphicsRootConstantBufferView(parameter, title GPU address). // A translated shader reads a root CBV through the same pointer source as a root SRV (root_address). Nothing // is forwarded to the desktop list and srv_bound is left alone: bit 0 means a vertex-fetch SRV is bound. @@ -7069,6 +7099,9 @@ HRESULT __stdcall queue_present(Wrapper* self, UINT planes, const void* plane_pa for (int c = 0; c < x2w_local::null_cause_count; ++c) null_causes[c] = null_by_cause[c]; ReleaseSRWLockExclusive(&draw_lock); } + if (render_mode() && frame % 300 == 0 && root_srv_at_draw()) + log("x2w-render root SRV copies (X2W_D3D12_ROOT_SRV_AT_DRAW): skipped %ld, copied %ld", root_srv_copies_skipped, + root_srv_copies_made); if (render_mode() && frame % 300 == 0) log("x2w-render totals: resets %ld (failed %ld), closes %ld, clears %ld, target binds %ld (unresolved %ld), executes %ld, draws %ld (skipped %ld), " "translated draws %ld (not drawn %ld), pipelines translated %ld / missing %ld, null bindings %ld (entry %ld, buffer %ld, token %ld, wrapper %ld, dimension %ld), " diff --git a/tests/cmake/startup_root_srv_at_draw.cmake b/tests/cmake/startup_root_srv_at_draw.cmake new file mode 100644 index 0000000000..9bf9d83f15 --- /dev/null +++ b/tests/cmake/startup_root_srv_at_draw.cmake @@ -0,0 +1,12 @@ +# Graphics root SRV: no desktop copy that no draw reads (opt-in X2W_D3D12_ROOT_SRV_AT_DRAW=1). +# A translated draw reads a slot 44 root SRV whose title bytes change before the draw; it runs with the switch unset +# (slot 44 copies) and set (no copy). +target_sources(startup_stream_consumer PRIVATE tests/startup_root_srv_draw.cpp) +if(EXISTS "${INTAKE_PYTHON}") + foreach(backend IN ITEMS warp hardware) + add_test(NAME startup_root_srv_draw_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}" + "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_root_srv_draw_check.py" + "$" ${backend}) + set_tests_properties(startup_root_srv_draw_${backend} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77) + endforeach() +endif() diff --git a/tests/startup_root_srv_draw.cpp b/tests/startup_root_srv_draw.cpp new file mode 100644 index 0000000000..e3d11e08e1 --- /dev/null +++ b/tests/startup_root_srv_draw.cpp @@ -0,0 +1,38 @@ +#include + +#include +#include +#include + +#include "startup_stream_setup.h" + +extern "C" __declspec(dllimport) void x2w_d3d12_root_srv_copies(LONG* skipped, LONG* copied); + +// A translated draw that reads a graphics root SRV set through list slot 44, at a title address without an upload +// mapping (X2W_TEST_ROOT_SRV_DRAW=write-after). Slot 44 sees the buffer holding red; the title then writes green and +// draws. The translated draw reads the SRV from its title address at the draw, so it draws green whether or not slot 44 +// also copied the bytes for the desktop binding (skipped with X2W_D3D12_ROOT_SRV_AT_DRAW=1). After the draw is recorded +// it prints the stand-in's copy counts (x2w_d3d12_root_srv_copies). The stream root's parameter 0 is a root SRV in this +// mode (startup_stream_setup.cpp). +int draw_root_srv(void* list) +{ + char mode[16]{}; + GetEnvironmentVariableA("X2W_TEST_ROOT_SRV_DRAW", mode, sizeof(mode)); + if (std::strcmp(mode, "write-after")) return 2; + const StreamWord root = reinterpret_cast(stream_root_signature()); + if (!root) return 40; + // Title memory outside any resource: slot 44 finds no upload mapping. + auto* colour = static_cast(VirtualAlloc(nullptr, 0x10000, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); + if (!colour) return 41; + const float red[4] = {1, 0, 0, 1}, green[4] = {0, 1, 0, 1}; + std::memcpy(colour, red, sizeof(red)); + invoke(list, 34, root); // SetGraphicsRootSignature + invoke(list, 44, 0, reinterpret_cast(colour)); // SetGraphicsRootShaderResourceView(0, address) + std::memcpy(colour, green, sizeof(green)); + invoke(list, 12, 3, 1); // DrawInstanced(3, 1) + // The stand-in's slot 44 copy counts after the draw was recorded, for the check script. + LONG skipped = -1, copied = -1; + x2w_d3d12_root_srv_copies(&skipped, &copied); + std::printf("root SRV copies: skipped %ld, copied %ld\n", skipped, copied); + return 0; // The buffer stays allocated until the process ends. +} diff --git a/tests/startup_root_srv_draw_check.py b/tests/startup_root_srv_draw_check.py new file mode 100644 index 0000000000..e01f8fbde6 --- /dev/null +++ b/tests/startup_root_srv_draw_check.py @@ -0,0 +1,72 @@ +"""A graphics root SRV (list slot 44) at a title address without an upload mapping, read by a translated draw. + +tests/startup_root_srv_draw.cpp (X2W_TEST_ROOT_SRV_DRAW=write-after): slot 44 sees the buffer holding red, the title +writes green, then draws, and the stand-in's copy counts are printed. Run with X2W_D3D12_ROOT_SRV_AT_DRAW unset and set: + - both: the translated draw reads the SRV from its title address at the draw and draws green; + - unset: slot 44 also copies a window into the upload ring for the desktop binding (copied 1); + - set: no draw reads that binding, so slot 44 skips the copy (skipped 1, copied 0). +The left half of the 8x4 target is drawn, the right half keeps the clear colour. +""" +import os +from pathlib import Path +import re +import subprocess +import sys +import tempfile + +from startup_address_srv_check import prepare_draw_cache + +HLSL = ( + "ByteAddressBuffer colour : register(t0);\n" + "float4 main(float4 p : SV_Position) : SV_Target {\n" + " return asfloat(colour.Load4(0));\n" + "}\n" +) +CLEAR = bytes((0, 0, 255, 255)) +GREEN = bytes((0, 255, 0, 255)) +SWITCH = "X2W_D3D12_ROOT_SRV_AT_DRAW" +COUNTS = re.compile(r"root SRV copies: skipped (-?\d+), copied (-?\d+)") + + +def run(directory, consumer, backend, at_draw): + folder = directory / f"write-after-{int(at_draw)}" + folder.mkdir() + os.environ.pop(SWITCH, None) # an inherited setting must not decide the unset case + modes = {"X2W_TEST_ROOT_SRV_DRAW": "write-after", "X2W_TRAP_LOG": str(folder / "trap.log")} + if at_draw: + modes[SWITCH] = "1" + env, args, output = prepare_draw_cache(folder, consumer, HLSL, modes, "srv 0 buf root 0") + result = subprocess.run([*args, backend, output], env=env, capture_output=True, text=True) + print(result.stdout, end="") + if result.returncode == 77: + return None, None + if result.returncode: + raise RuntimeError(f"consumer failed ({result.returncode})\n{result.stderr[-5000:]}") + found = COUNTS.search(result.stdout) + if not found: + raise AssertionError(f"{SWITCH}={int(at_draw)}: no copy counts printed") + return output.read_bytes(), tuple(int(value) for value in found.groups()) + + +def check(directory, consumer, backend): + expected_pixels = (GREEN * 4 + CLEAR * 4) * 4 + # (skipped, copied) + expected_counts = {False: (0, 1), True: (1, 0)} + for at_draw in (False, True): + pixels, counts = run(directory, consumer, backend, at_draw) + if pixels is None: + return 77 + label = f"{SWITCH}={int(at_draw)}" + if pixels != expected_pixels: + raise AssertionError(f"{label}: actual {pixels.hex()} expected {expected_pixels.hex()}") + if counts != expected_counts[at_draw]: + raise AssertionError(f"{label}: copies (skipped, copied) {counts}, " + f"expected {expected_counts[at_draw]}") + print(f"{backend} {label}: 32 pixels exact (green, read at the draw); " + f"skipped {counts[0]}, copied {counts[1]}") + return 0 + + +if __name__ == "__main__": + with tempfile.TemporaryDirectory() as temporary: + sys.exit(check(Path(temporary), *sys.argv[1:])) diff --git a/tests/startup_stream_draw.cpp b/tests/startup_stream_draw.cpp index 9c7d7844bb..8848ae1572 100644 --- a/tests/startup_stream_draw.cpp +++ b/tests/startup_stream_draw.cpp @@ -487,6 +487,9 @@ int check_stream_draw(IDXGIAdapter* adapter, void* guest, void* pso, const char* } else if (bc_chain) { extern int draw_bc_chain(void*, void*); if (const int result = draw_bc_chain(guest, list)) return result; + } else if (GetEnvironmentVariableW(L"X2W_TEST_ROOT_SRV_DRAW", nullptr, 0)) { + extern int draw_root_srv(void*); + if (const int result = draw_root_srv(list)) return result; } else if (GetEnvironmentVariableW(L"X2W_TEST_ROOT_CBV", nullptr, 0)) { extern int root_heap_control(ID3D12Device*); extern int draw_root_cbv(void*); diff --git a/tests/startup_stream_setup.cpp b/tests/startup_stream_setup.cpp index 2e05936d37..c92d9b49ff 100644 --- a/tests/startup_stream_setup.cpp +++ b/tests/startup_stream_setup.cpp @@ -160,11 +160,13 @@ int prepare_stream_pipeline(const char* vs_path, const char* ps_path, const char } // X2W_TEST_ROOT_CBV= declares root parameter 0 as a constant buffer view (startup_root_cbv.cpp); // "wrong_type" declares a shader resource view there instead. + // X2W_TEST_ROOT_SRV_DRAW (startup_root_srv_draw.cpp) declares it as a shader resource view as well. D3D12_ROOT_PARAMETER root_cbv{}; char cbv_mode[16]{}; - if (!textured && GetEnvironmentVariableA("X2W_TEST_ROOT_CBV", cbv_mode, sizeof(cbv_mode))) { - root_cbv.ParameterType = std::strcmp(cbv_mode, "wrong_type") == 0 ? D3D12_ROOT_PARAMETER_TYPE_SRV - : D3D12_ROOT_PARAMETER_TYPE_CBV; + const bool root_srv_draw = GetEnvironmentVariableW(L"X2W_TEST_ROOT_SRV_DRAW", nullptr, 0) != 0; + if (!textured && (root_srv_draw || GetEnvironmentVariableA("X2W_TEST_ROOT_CBV", cbv_mode, sizeof(cbv_mode)))) { + root_cbv.ParameterType = root_srv_draw || std::strcmp(cbv_mode, "wrong_type") == 0 ? D3D12_ROOT_PARAMETER_TYPE_SRV + : D3D12_ROOT_PARAMETER_TYPE_CBV; root_cbv.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL; root_desc.NumParameters = 1; root_desc.pParameters = &root_cbv;