diff options
| author | traeb0n <masonbartelme@gmail.com> | 2026-11-21 22:19:11 -0600 |
|---|---|---|
| committer | traeb0n <masonbartelme@gmail.com> | 2026-11-22 09:19:29 -0600 |
| commit | 9df4eda200b6cd7b8b33dd6f8bc84d681d2c7a29 (patch) | |
| tree | b2a7c168e48971b4490cd7d77c8d1079518c3518 | |
| parent | 6a6704df1592e1dc4a3215ddf1423a78adb52045 (diff) | |
Runtime: VulkanQueue keeps its PreparedList storage between executions
execute built a new std::vector<PreparedList> on every call, so every
command's detail::Prepared entry (1288 bytes on main now) was made in
fresh memory and freed again: replay_cpu_bench measures that at 274-292
ns per command on an i7-14700K, against 85 ns refilled in kept storage,
over half of the layer's whole CPU cost per command. The queue now keeps
the vector as a member (per queue, like its command buffers: one execute
at a time on a queue, as acquire() and submit() already assume) and
resizes it to the list count; prepare refills each list's entries in
place, where the per-entry vectors also keep their capacity.
replay_cpu_bench mirrors the change and reports both floors (entries in
kept storage and made fresh). Its frame goes from 425 to 194 ns per
command (a 21,504-command frame from 9.1 ms to 4.2 ms).
d3d12_vulkan_device_test executes {B, A}, then {B} alone, and checks
that only B ran the second time: with storage that only grows, a stale A
is replayed after B and the test fails (checked).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| -rw-r--r-- | runtime/src/vulkan_backend.h | 8 | ||||
| -rw-r--r-- | runtime/test/d3d12_vulkan_device_test.cpp | 48 | ||||
| -rw-r--r-- | runtime/test/replay_cpu_bench.cpp | 46 |
3 files changed, 82 insertions, 20 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index e577e6af0..e2aa0c830 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h | |||
| @@ -1279,7 +1279,10 @@ public: | |||
| 1279 | } | 1279 | } |
| 1280 | } scratch_guard{context_.scratch_.get(), progress_, false}; | 1280 | } scratch_guard{context_.scratch_.get(), progress_, false}; |
| 1281 | bindless_commit = context_.bindless_commit_; | 1281 | bindless_commit = context_.bindless_commit_; |
| 1282 | std::vector<PreparedList> prepared(lists.size()); | 1282 | // Kept between executions: prepare refills each list's entries in the storage the last execution left, where |
| 1283 | // making them fresh for every command was about half the layer's CPU cost per command (replay_cpu_bench). | ||
| 1284 | auto& prepared = prepared_; | ||
| 1285 | prepared.resize(lists.size()); | ||
| 1283 | ReplayFunctions replay = context_.replay_; | 1286 | ReplayFunctions replay = context_.replay_; |
| 1284 | std::uint64_t captures = 0; | 1287 | std::uint64_t captures = 0; |
| 1285 | for (const auto* list : lists) | 1288 | for (const auto* list : lists) |
| @@ -1632,6 +1635,9 @@ private: | |||
| 1632 | std::unique_ptr<OcclusionResolve> occlusion_resolve_; | 1635 | std::unique_ptr<OcclusionResolve> occlusion_resolve_; |
| 1633 | std::vector<VkSemaphore> waits_; | 1636 | std::vector<VkSemaphore> waits_; |
| 1634 | std::vector<std::uint64_t> wait_values_; | 1637 | std::vector<std::uint64_t> wait_values_; |
| 1638 | // execute's working storage. Per queue like buffers_: one execute at a time on a queue, as acquire() and | ||
| 1639 | // submit() already assume; other queues prepare concurrently with their own. | ||
| 1640 | std::vector<PreparedList> prepared_; | ||
| 1635 | std::uint32_t open_labels_ = 0; // queue labels begun and not ended | 1641 | std::uint32_t open_labels_ = 0; // queue labels begun and not ended |
| 1636 | bool ok_ = false; | 1642 | bool ok_ = false; |
| 1637 | }; | 1643 | }; |
diff --git a/runtime/test/d3d12_vulkan_device_test.cpp b/runtime/test/d3d12_vulkan_device_test.cpp index a528202bc..9ed215ec6 100644 --- a/runtime/test/d3d12_vulkan_device_test.cpp +++ b/runtime/test/d3d12_vulkan_device_test.cpp | |||
| @@ -4461,6 +4461,54 @@ int main() { | |||
| 4461 | report("reserved buffer", 0, down + tiled_bytes_down, up + tiled_bytes_up, 256); | 4461 | report("reserved buffer", 0, down + tiled_bytes_down, up + tiled_bytes_up, 256); |
| 4462 | } | 4462 | } |
| 4463 | 4463 | ||
| 4464 | // Several lists in one ExecuteCommandLists, then fewer. The queue keeps its prepared storage between | ||
| 4465 | // executions, so the second must replay only the list it is given. B clears blue and A red: {B, A} leaves | ||
| 4466 | // red, then {B} alone must leave blue (a stale A replayed after it would leave red). | ||
| 4467 | { | ||
| 4468 | Ref<ID3D12CommandAllocator> allocator_a, allocator_b; | ||
| 4469 | Ref<ID3D12GraphicsCommandList> list_a, list_b; | ||
| 4470 | Ref<ID3D12Resource> target; | ||
| 4471 | EXPECT(SUCCEEDED(create(default_heap, | ||
| 4472 | texture_desc(size, size, DXGI_FORMAT_R8G8B8A8_UNORM, | ||
| 4473 | D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET), | ||
| 4474 | D3D12_RESOURCE_STATE_RENDER_TARGET, target))); | ||
| 4475 | device->CreateRenderTargetView(target.p, nullptr, rtv1); // the lists that used this slot have run | ||
| 4476 | for (auto [a, l, colour] : {std::tuple{&allocator_a, &list_a, red}, std::tuple{&allocator_b, &list_b, blue}}) { | ||
| 4477 | EXPECT(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&a->p)))); | ||
| 4478 | EXPECT(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, a->p, nullptr, | ||
| 4479 | IID_PPV_ARGS(&l->p)))); | ||
| 4480 | (*l)->ClearRenderTargetView(rtv1, colour, 0, nullptr); | ||
| 4481 | EXPECT((*l)->Close() == S_OK); | ||
| 4482 | } | ||
| 4483 | ID3D12CommandList* b_then_a[] = {list_b.p, list_a.p}; | ||
| 4484 | queue->ExecuteCommandLists(2, b_then_a); | ||
| 4485 | EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success); | ||
| 4486 | ID3D12CommandList* b_only[] = {list_b.p}; | ||
| 4487 | queue->ExecuteCommandLists(1, b_only); | ||
| 4488 | EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success); | ||
| 4489 | |||
| 4490 | UINT64 target_cursor = 0; | ||
| 4491 | const auto target_place = place(device.p, target.p, 0, target_cursor); | ||
| 4492 | std::memset(down, 0xcd, 65536); | ||
| 4493 | EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr))); | ||
| 4494 | barrier = transition(target.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE); | ||
| 4495 | list->ResourceBarrier(1, &barrier); | ||
| 4496 | { | ||
| 4497 | const auto dst = footprint(readback.p, target_place); | ||
| 4498 | const auto src = subresource(target.p, 0); | ||
| 4499 | list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr); | ||
| 4500 | } | ||
| 4501 | EXPECT(list->Close() == S_OK); | ||
| 4502 | queue->ExecuteCommandLists(1, lists); | ||
| 4503 | EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success); | ||
| 4504 | EXPECT(submit_and_wait()); | ||
| 4505 | const std::uint8_t blue_texel[4] = {0, 0, 255, 255}; | ||
| 4506 | for (UINT row = 0; row < size; ++row) | ||
| 4507 | for (UINT col = 0; col < size; ++col) | ||
| 4508 | report("second ExecuteCommandLists, fewer lists", row * size + col, | ||
| 4509 | down + target_place.Offset + row * target_place.Footprint.RowPitch + col * 4, blue_texel, 4); | ||
| 4510 | } | ||
| 4511 | |||
| 4464 | // Queries (docs/d3d12_vulkan.md section 4.6). The layer has no draws yet, so occlusion and pipeline | 4512 | // Queries (docs/d3d12_vulkan.md section 4.6). The layer has no draws yet, so occlusion and pipeline |
| 4465 | // statistics count nothing and read zero; what is checked is that every query is reset and run, that | 4513 | // statistics count nothing and read zero; what is checked is that every query is reset and run, that |
| 4466 | // results land as UINT64s at the offsets asked for (over a 0xcd fill), that timestamps advance at the | 4514 | // results land as UINT64s at the offsets asked for (over a 0xcd fill), that timestamps advance at the |
diff --git a/runtime/test/replay_cpu_bench.cpp b/runtime/test/replay_cpu_bench.cpp index 1ca0dccc3..8e85452bc 100644 --- a/runtime/test/replay_cpu_bench.cpp +++ b/runtime/test/replay_cpu_bench.cpp | |||
| @@ -10,8 +10,8 @@ | |||
| 10 | // timed on its own, so one slow kind stands out; | 10 | // timed on its own, so one slow kind stands out; |
| 11 | // - a frame: L lists of P passes, each pass a mix of what a frame without draws records today (barriers, | 11 | // - a frame: L lists of P passes, each pass a mix of what a frame without draws records today (barriers, |
| 12 | // whole-view and rectangle clears, buffer and texture copies, a UAV clear, queries and predication). | 12 | // whole-view and rectangle clears, buffer and texture copies, a UAV clear, queries and predication). |
| 13 | // prepare includes allocating the PreparedList and planning the query batch. emit includes committing query state | 13 | // prepare includes refilling the PreparedList storage kept from the last execution (as VulkanQueue::execute keeps |
| 14 | // and freeing the preparation data, as VulkanQueue::execute does. | 14 | // prepared_) and planning the query batch. emit includes committing query state, as execute does. |
| 15 | // Each figure is the median over --reps runs after --warmup runs. Heap allocations (global operator new) and | 15 | // Each figure is the median over --reps runs after --warmup runs. Heap allocations (global operator new) and |
| 16 | // Vulkan calls are counted per command; both are deterministic, unlike the times. | 16 | // Vulkan calls are counted per command; both are deterministic, unlike the times. |
| 17 | // | 17 | // |
| @@ -320,6 +320,7 @@ template<typename Build> | |||
| 320 | Result run(Scene& scene, std::size_t lists, Build&& build, int reps, int warmup) { | 320 | Result run(Scene& scene, std::size_t lists, Build&& build, int reps, int warmup) { |
| 321 | using clock = std::chrono::steady_clock; | 321 | using clock = std::chrono::steady_clock; |
| 322 | std::vector<CommandList> recorded(lists); | 322 | std::vector<CommandList> recorded(lists); |
| 323 | std::vector<PreparedList> prepared; | ||
| 323 | const VkCommandBuffer command_buffer = fake_handle<VkCommandBuffer>(0x10); | 324 | const VkCommandBuffer command_buffer = fake_handle<VkCommandBuffer>(0x10); |
| 324 | const auto resolve = [&scene](ResourceId id) { return scene.resolve(id); }; | 325 | const auto resolve = [&scene](ResourceId id) { return scene.resolve(id); }; |
| 325 | std::vector<double> t_record, t_prepare, t_emit; | 326 | std::vector<double> t_record, t_prepare, t_emit; |
| @@ -337,7 +338,8 @@ Result run(Scene& scene, std::size_t lists, Build&& build, int reps, int warmup) | |||
| 337 | clock::time_point t2; | 338 | clock::time_point t2; |
| 338 | std::size_t a2, v2; | 339 | std::size_t a2, v2; |
| 339 | { | 340 | { |
| 340 | std::vector<PreparedList> prepared(lists); | 341 | // The PreparedList storage is kept between executions, as VulkanQueue::execute keeps prepared_. |
| 342 | prepared.resize(lists); | ||
| 341 | QueryReplay queries; | 343 | QueryReplay queries; |
| 342 | for (std::size_t i = 0; i < lists; ++i) { | 344 | for (std::size_t i = 0; i < lists; ++i) { |
| 343 | if (const auto r = prepare(recorded[i], resolve, scene.vk, prepared[i]); r != ReplayResult::success) { | 345 | if (const auto r = prepare(recorded[i], resolve, scene.vk, prepared[i]); r != ReplayResult::success) { |
| @@ -508,26 +510,32 @@ int main(int argc, char** argv) { | |||
| 508 | std::snprintf(name, sizeof name, "frame (%zu lists x %zu passes)", lists, passes); | 510 | std::snprintf(name, sizeof name, "frame (%zu lists x %zu passes)", lists, passes); |
| 509 | print_row(name, f, csv); | 511 | print_row(name, f, csv); |
| 510 | 512 | ||
| 511 | // The floor under prepare and emit: making and freeing one PreparedList entry per command, which execute does | 513 | // The floor under prepare: one PreparedList entry per command, refilled in storage kept from the last |
| 512 | // for every list before any command is looked at. | 514 | // execution (what execute does), and for comparison made in new storage and freed every time. |
| 513 | std::vector<double> floor_ns; | 515 | const auto entries_ns = [&](bool kept) { |
| 514 | for (int rep = 0; rep < warmup + reps; ++rep) { | 516 | std::vector<double> ns; |
| 515 | const auto t0 = std::chrono::steady_clock::now(); | 517 | PreparedList storage; |
| 516 | { | 518 | for (int rep = 0; rep < warmup + reps; ++rep) { |
| 517 | PreparedList list; | 519 | const auto t0 = std::chrono::steady_clock::now(); |
| 518 | list.commands.assign(f.commands, {}); | 520 | if (kept) { |
| 521 | storage.commands.assign(f.commands, {}); | ||
| 522 | } else { | ||
| 523 | PreparedList fresh; | ||
| 524 | fresh.commands.assign(f.commands, {}); | ||
| 525 | } | ||
| 526 | const auto t1 = std::chrono::steady_clock::now(); | ||
| 527 | if (rep >= warmup) ns.push_back(std::chrono::duration<double, std::nano>(t1 - t0).count() / double(f.commands)); | ||
| 519 | } | 528 | } |
| 520 | const auto t1 = std::chrono::steady_clock::now(); | 529 | return median(ns); |
| 521 | if (rep >= warmup) | 530 | }; |
| 522 | floor_ns.push_back(std::chrono::duration<double, std::nano>(t1 - t0).count() / double(f.commands)); | 531 | const double kept = entries_ns(true), fresh = entries_ns(false); |
| 523 | } | ||
| 524 | const double floor = median(floor_ns); | ||
| 525 | if (csv) { | 532 | if (csv) { |
| 526 | std::printf("prepared_entries_only,%zu,0,%.1f,0,%.1f,0,0,0,0\n", f.commands, floor, floor); | 533 | std::printf("prepared_entries_kept,%zu,0,%.1f,0,%.1f,0,0,0,0\n", f.commands, kept, kept); |
| 534 | std::printf("prepared_entries_fresh,%zu,0,%.1f,0,%.1f,0,0,0,0\n", f.commands, fresh, fresh); | ||
| 527 | return failures ? 1 : 0; | 535 | return failures ? 1 : 0; |
| 528 | } | 536 | } |
| 529 | std::printf("%-28s %8zu | %9s %9.1f %9s %9.1f |\n", " PreparedList entries only", f.commands, "", floor, "", | 537 | std::printf("%-28s %8zu | %9s %9.1f %9s %9.1f |\n", " entries, storage kept", f.commands, "", kept, "", kept); |
| 530 | floor); | 538 | std::printf("%-28s %8zu | %9s %9.1f %9s %9.1f |\n", " entries, made fresh", f.commands, "", fresh, "", fresh); |
| 531 | std::printf("\nframe: %.1f us per ExecuteCommandLists of %zu commands, layer only (no driver)\n", | 539 | std::printf("\nframe: %.1f us per ExecuteCommandLists of %zu commands, layer only (no driver)\n", |
| 532 | (f.record.ns + f.prepare.ns + f.emit.ns) * double(f.commands) / 1000.0, f.commands); | 540 | (f.record.ns + f.prepare.ns + f.emit.ns) * double(f.commands) / 1000.0, f.commands); |
| 533 | std::printf("one PreparedList entry (detail::Prepared): %zu bytes; one recorded Command: %zu bytes\n", | 541 | std::printf("one PreparedList entry (detail::Prepared): %zu bytes; one recorded Command: %zu bytes\n", |