Skip to content

perf(runtime): move a draw's views out of the command variant (D3D-391) - #3693

Open
Ayinaki wants to merge 1 commit into
ItsNotRexed:mainfrom
Ayinaki:d3d391-draw-views
Open

Ayinaki wants to merge 1 commit into
ItsNotRexed:mainfrom
Ayinaki:d3d391-draw-views

Conversation

@Ayinaki

@Ayinaki Ayinaki commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

D3D-391, claimed in the coordination issue (#4816).

Rebased onto main at 98fa503f8, head 9c70d7b40. The DrawViews move this PR opened with has since landed on main through 2ce837826, so the rebase dropped it and this branch now carries only the parts that were still missing upstream: the review points from @kacper-serewis, @slopradish and @shdann. Mergeable, no conflicts.

What is in the branch

  1. The target loop keeps the array bound. prepare_draw now tests slot < draw.views->render_targets.size() beside the count and the pipeline's colour-format count. Checking slot < p.colors.size() alone is not enough: a direct replay caller can supply a count and a format list both larger than the eight slots DrawViews holds, and main still reads draw.views->render_targets[slot] with only those two bounds. (@kacper-serewis, @slopradish)
  2. The DrawViews comment says what owns the block. It now states that the shim allocates one block on the first draw after the render targets or the bound vertex buffers change, that the draws after it share it, and that a call which rebinds the values already bound does not start a new one. (@kacper-serewis)
  3. A no-op rebind does not invalidate. OMSetRenderTargets and set_vertex_buffers compare the incoming views against the bound ones and only set draw_views_stale_ when something differs, which is the case @shdann raised: a title that sets the same buffers before every draw would otherwise build and allocate a block per draw.

Cost

The block is 984 bytes by value in the command variant and 296 bytes out of line — measured, not estimated. With the no-op rebind check the steady case for a title that re-sets the same state allocates nothing. Where a title really does change its views per draw, the cost is one block (one make_shared, ~200 ns) and one copy of the 8 targets + 32 vertex-buffer slots per draw; on the bench frame, which records 16,384 draws that each change state, that shows as the frame taking 1–4 ms more than main's. #3784 removes that allocation if the maintainers prefer list-owned storage. The bench numbers in the earlier comments here were measured at f506039c9, before the rebase.

Tested

Windows 11 26200, MSVC (VS BuildTools), Python not involved; no GPU is named because everything below is CPU-only, and no game files are used.

command result
scripts/build_runtime.ps1 -jobs 4 508/508 targets, gta6rt.exe built, no errors
ctest -R d3d12_com 2/2 passed (d3d12_com, d3d12_compute)
ctest -R 'vk_command_replay|d3d12_trace|d3d12_com' 4/4 passed

d3d12_com is the test that walks the shim's recording path this PR touches; vk_command_replay exercises prepare_draw with the bound added in point 1. The GPU results earlier in this thread (@Vonsek on RDNA2 iGPU and RTX 5080, @Exopus-0x65f7ad0 on RTX 5060 Ti) were on 3c3749d0 and earlier, so a re-check on 9c70d7b40 is welcome.

Depends on: nothing.

@kacper-serewis kacper-serewis 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.

D3D-391 at f506039. Restore the target array size guard in prepare_draw (vulkan_replay.h:1893). A direct replay caller can supply a count and pipeline format list both larger than eight. Update command_list.h:407 to describe the shared snapshot allocation on the first draw after invalidation. MSVC x64 CPU checks passed for copy/move recording, snapshot replacement and release on reset. Command measured 296 bytes. COM invalidation and Vulkan preparation were inspected in source. AI-assisted.

@shdann

shdann commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Review note about memory allocation. An AI tool helped me to read the diff. I did not build or measure the change.

Result of the check

The stale flag is correct. Three functions change render_targets_, render_target_count_ and vertex_buffers_: OMSetRenderTargets, the reset function and set_vertex_buffers. Each function sets draw_views_stale_.

Possible problem

Many titles call IASetVertexBuffers before each draw. In that condition, each draw makes a new DrawViews block.

  • Each draw then does one heap allocation with make_shared<DrawViews>.
  • Each draw also copies all 8 targets and all 32 vertex buffer slots.
  • Before this change, each draw made one copy and no heap allocation.

The shared block helps only when a sequence of draws uses the same buffers.

Suggestions

  1. Do not set the stale flag if the new data is the same as the old data. Compare the new views with vertex_buffers_[start..start+count) in set_vertex_buffers. Do the same in OMSetRenderTargets.
  2. Keep the render targets and the vertex buffers in two different blocks. The render targets change less frequently. Then a vertex buffer change does not copy the targets.
  3. Keep only the slots that are bound. Use a count or a slot mask.
  4. Keep the views in the command list, not on the heap. Use a std::vector<DrawViews> or an arena that Reset clears. Draw then holds an index. This removes the allocation and the atomic reference count for each draw.

Test

Add a counter to the test. Count the draws and count the view blocks. Call IASetVertexBuffers before each draw. The two counts show the effect of a change.

@slopradish slopradish left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewed the CPU path against our fork's ported Draw/command variant.

The core change is correct: moving the two fixed arrays behind a shared_ptr drops the variant floor from 984 to 296 bytes, and the block is rebuilt on exactly the three state changes that can alter it (OMSetRenderTargets, set_vertex_buffers with count>0, reset_state). The if (count) guard on vertex-buffer invalidation is right — count=0 changes no slot, and an explicit unbind arrives as a view with a null BufferLocation, which does invalidate.

One note on the write-up: the 3890 -> 3824 us frame number is explicitly measured on a mix with no draws, so it is noise. The result worth citing is the 984 -> 296 byte command size, which is measured directly. The per-draw allocation cost remains unmeasured as you note — worth a follow-up once the draw path joins the bench.

CPU-only review; no GPU run from me.

@slopradish

Copy link
Copy Markdown

Follow-up to my approve: @kacper-serewis is right about the missing array bound in prepare_draw. I checked slot < p.colors.size() but the fixed array is DrawViews::render_targets[8], and a direct replay caller can pass a larger count than the COM path's D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT cap. Please restore the slot < draw.views->render_targets.size() term alongside the new count check. Everything else in my review stands.

@kacper-serewis kacper-serewis 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.

D3D-391 draw-containing CPU comparison. Median ns/draw (parent f847748 / #3693 f506039 / #3784 2fa5423): sparse unchanged 320/114/121; sparse changing 335/319/123; dense unchanged 330/106/316; dense changing 307/327/294. Nine interleaved rounds, 16,384 draws per fresh list, Windows x64 Clang 22.1.8 -O2. All recorded bindings and draw order verified after each sample. Production CommandList::draw timed including storage growth; harness modeled COM state/cache invalidation, excluding COM entry, destruction and replay. Shared machine, small differences inconclusive. Dense repeated bindings favored shared snapshots and sparse changing bindings favored list-owned storage. AI-assisted.

@Ayinaki

Ayinaki commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Both review points are in at bd5e274.

kacper-serewis, slopradish: the target loop keeps slot < draw.views->render_targets.size() next to the count and colour-format bounds, and the DrawViews comment now states that the block is allocated on the first draw after a state change and shared by the draws after it.

shdann: the per-draw rebind case is now handled where it starts. set_vertex_buffers and OMSetRenderTargets compare the incoming values against the ones bound before they invalidate, so calling IASetVertexBuffers with the buffers already bound does not start a new block. A title that sets one mesh per draw therefore allocates no view blocks in steady state, rather than one per draw.

I kept the refcounted block instead of taking the storage from the command list because of execute_bundle: a block stays valid when a Draw is copied into the parent list, while an index into the source list's vectors needs rebasing there. #3784 takes the list-owned route and rebases explicitly, and in kacper's comparison the two land about even.

Checks after the change: replay_cpu_bench, d3d12_com_test and command_list_test build with MSVC 14.52; replay_cpu_bench --self-test passes; Command is still 296 bytes. d3d12_com_test is still not run here, since the GPU is busy with other work.

@FaKL-Code

Copy link
Copy Markdown
Contributor

Tested bd5e274 merged onto current main aa2988c on NVIDIA GeForce RTX 4060 Ti, driver 610.88 (32.0.16.1088), Windows 11 26200, MSVC 14.51.36231 (VS 2026), Vulkan SDK 1.4.363.0: does not build against current main (replay_cpu_bench only); everything else passes

  • git merge is textually clean, but replay_cpu_bench.cpp fails to compile: "C2039: 'render_targets' is not a member of 'leonida::Draw'" at lines 279 and 580, and the same for vertex_buffers at 282 and 283.
    • Main added draws to the bench frame after this branch's base (85ab929 "Tests: replay_cpu_bench frame includes draws"). Those draws fill Draw::render_targets and vertex_buffers directly, and this PR moves both into the shared DrawViews block.
    • So the PR needs a rebase onto current main, with those two spots in the bench moved to Draw::views. 98d977a ("a draw's prepared data lives beside the per-command entries") also touched draw preparation since, so the rebase is worth a second look there too, though it compiles.
  • With that one file failing: ctest 143 of 144 pass, with only replay_cpu_bench_selftest "Not Run"; same 9 skips as main. No new compiler warnings. Loader test passes; validation run: 56 tests clean. d3d12_com -V passes.
  • As a probe, not pushed anywhere, I moved the two bench spots to a DrawViews block locally. Then:
    • replay_cpu_bench --self-test: "14 kinds and a 296-command frame recorded and replayed, ok".
    • One recorded Command: 296 bytes, against 984 on main; PreparedList entry 888 bytes on both.
    • The bench's draw record time went up (about 316 ns against 69 ns on main, 1.00 allocations per draw), but that's my probe allocating a fresh DrawViews for every draw. The real fix should share one block across the bench's draws, the way the shim does, or the draw rows will measure an allocation per draw instead of the layer.
  • On the earlier head (f506039, main f847748), everything passed: 131 of 131. The frame-time comparison was within noise: median 5387.8 µs on main against 5433.0 µs, 7 alternating runs each.

AI-assisted (Claude).

@Ayinaki

Ayinaki commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto aa2988cc at 3c3749d0; the two bench spots were the only thing that needed moving.

replay_cpu_bench.cpp now builds a DrawViews block per draw, in draw() and in the self-test's plain draw that used to assign render_targets directly. Every draw the bench records reads its own slice of the geometry arena (base comes from the draw index), so none of the frame's draws can share a block the way a title's repeated draws on one mesh do — under the shim those 16 draws per pass would all allocate too. 98d977ae needed no change: draw preparation and the prepared-draw storage self-test are unaffected by the block.

Your probe's numbers reproduce here. Same machine, --reps 5 --warmup 2, bench built from main and from this head:

row main aa2988cc this head
draw 51.1 ns, 0.00 allocations 256.0 ns, 1.00 allocations
draw indexed 48.7 ns 240.3 ns
frame 50.6 ns per command, 14227.6 us per ExecuteCommandLists 84.0 ns per command, 14367.7 us

So a title that rebinds its buffers per draw pays about 200 ns and one allocation per draw, and the frame as a whole moves about 1%. I won't hide that: the block is a shared_ptr snapshot, so a draw that rebinds has to allocate one. If that is too much for the record path, the way out is the list-owned storage of #3784 (an index into the list's vectors, rebased when a Draw is copied into a bundle), which is why I kept the refcounted block instead: in kacper's comparison the two landed about even, and the per-draw row is where the difference shows.

On this head, MSVC 14.52 (VS 18 BuildTools) and Vulkan SDK 1.4.363.1: the whole runtime builds clean, 451 targets, no warnings beyond the pre-existing C4834 in uav_clear.h; ctest 153 of 153 passed with the same 10 skipped (shader_library_corpus, bink x3, fsr_dispatch, bvh_builder, gameinput, vk_sync_validation, d3d12_rgb9e5_rtv, d3d12_first_light); replay_cpu_bench --self-test prints "14 kinds and a 296-command frame recorded and replayed, ok". d3d12_com_test still has not run here, since it needs the GPU.

@Ayinaki

Ayinaki commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

A correction to the frame number in my reply above: that one was a single run. Alternating the two binaries six times on this machine gives frame 14791 / 17093 / 14961 us on aa2988cc against 15795 / 21653 / 22748 us on this head — the head is higher in every pair. So the rebind is not +1% at frame level: main's frame records 16,384 draws since 85ab929d, each of them allocates a block here, and that is about 1-4 ms of an 11-14 ms frame. The record column of the draw rows is the stable part: 51-58 ns to 240-246 ns, 0.00 to 1.00 allocations per draw.

The body now carries those ranges and the trade in full. The list-owned storage in #3784 removes the per-draw allocation if the maintainers would rather the record path stayed allocation-free for a rebinding draw; I would rather see this closed in #3784's favour than argue the row back to where it was on main.

@Vonsek

Vonsek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 3c3749d on Windows 11 (MSVC BuildTools 2022, Ninja, RelWithDebInfo, -j 4; no Vulkan validation layer installed), one GPU at a time, device line confirmed: AMD Radeon RDNA2 iGPU (driver 26.10.1) and NVIDIA RTX 5080 (driver 616.92). Ran ctest -R d3d12_com (the test this PR changes): pass on both GPUs (1/1 each). No game files.

AI-assisted.

@Exopus-0x65f7ad0

Copy link
Copy Markdown

Tested 3c3749d0 on Windows 11 (26300), RTX 5060 Ti / 621.14, MSVC 14.51 (VS 2026), Vulkan SDK 1.4.363.0, RelWithDebInfo, clean build with -DLEONIDA_LLVM_MC (so the translated-shader D3D12 gate tests are built), full LEONIDA_VK_VALIDATION=1 ctest -j4: 144/144 passed, 0 VUIDs, 0 build errors. AI-assisted (Claude Code).

ItsNotRexed added a commit that referenced this pull request Nov 23, 2026
ItsNotRexed added a commit that referenced this pull request Nov 23, 2026
- replay_cpu_bench: #3693 moved a draw's render targets and vertex buffers into Draw::views (D3D-391); the bench
  builds a DrawViews block instead of writing the old inline arrays.
- xgameruntime_user_test links file_system.cpp, which now defines rt::fs::game_dir that xgameruntime.cpp calls.
- check_sparse_queue_family: a vkCreateInstance refused because the api_dump/validation layer is not installed is
  the documented "layer absent" skip (77), not a failure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d (D3D-391)

prepare_draw lost the bound on the target array when the count moved behind the views block. A
direct replay caller can pass a count and a colour-format list both above eight, so the loop
needs all three bounds.

Rebinding the values already bound no longer invalidates the block. A title that sets one mesh
per draw through IASetVertexBuffers would otherwise allocate a block per draw, which is the
case the shared snapshot was meant to avoid.
@Ayinaki

Ayinaki commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@kacper-serewis both points are in on the rebased head 9c70d7b40:

  • prepare_draw keeps slot < draw.views->render_targets.size() beside the count and the colour-format bound, so a direct replay caller with a larger count and format list stops at the eight slots.
  • The DrawViews comment now says the shim allocates one block on the first draw after the render targets or the bound vertex buffers change, the draws after it share it, and a rebind of the values already bound does not start a new one.

On the rebase itself: the DrawViews move went into main with 2ce837826, so I dropped the part that landed rather than restate it. What remains of this branch is only the three points above; the bench change is no longer part of it because main has it.

@Vonsek @Exopus-0x65f7ad0 the head changed to 9c70d7b40 (rebased on 98fa503f8, delta now 3 files / 32+ 11-). A re-run on that head would be welcome if either of you has the GPU free; the code that reaches the GPU path is unchanged from 3c3749d0 — what moved is the bound in prepare_draw and the comment — so the earlier results still describe the behaviour, but the checked-commit column should name the new head.

Local on this machine: scripts/build_runtime.ps1 508/508, ctest -R 'vk_command_replay|d3d12_trace|d3d12_com' 4/4 passed.

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.

7 participants