From c74f8f38340f91218b16b31926a12e240f7f91f3 Mon Sep 17 00:00:00 2001 From: DadaCap <167002618+DadaCap@users.noreply.github.com> Date: Tue, 24 Nov 2026 22:15:17 +0100 Subject: [PATCH 1/2] vulkan: a submission changes the layout tracker in place and takes its changes back (VK-TRACKER-NO-COPY) execute_now copied the whole LayoutTracker for every submission, so that a failed one leaves the layouts as they were: two heap allocations per tracked image for each ExecuteCommandLists, whatever the lists touch. On a retail boot's loading screen that copy took most of the submitting thread. LayoutTracker::begin() now notes what an image had before its first change, rollback() puts it back and commit() drops the notes; execute_now changes the device's tracker under tracker_mutex_, which it already held around the copy. Co-Authored-By: Claude Fable 5.2 --- runtime/src/vulkan_backend.h | 7 ++-- runtime/src/vulkan_replay.h | 38 +++++++++++++++++++- runtime/test/replay_texture_barrier_test.cpp | 23 ++++++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index ab6786a518..f678fa46a5 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h @@ -2593,7 +2593,9 @@ class VulkanQueue : private QueueHolds::Holder { } replay.bindless_set = context_.bindless_current_ ? context_.bindless_current_() : VK_NULL_HANDLE; const bool bindless_uploaded = std::exchange(context_.bindless_upload_pending_, false); - auto next_tracker = context_.tracker_; + // Changed in place, under tracker_mutex_ until this submission is in the queue or taken back. + LayoutTracker& next_tracker = context_.tracker_; + next_tracker.begin(); // Bindless images that may already be visible to shaders are initialized on a submission that runs shaders. // initialize() zeroes committed storage images and only transitions sparse images. bool runs_shaders = false; @@ -2714,13 +2716,14 @@ class VulkanQueue : private QueueHolds::Holder { wait_values_.pop_back(); } if (submitted) { - context_.tracker_ = std::move(next_tracker); + next_tracker.commit(); slot->predicate = std::move(batch); slot->predicated_copy = std::move(copy_batch); slot->counted_dispatch = std::move(counted_batch); queries.commit(); slot->segments = std::move(queries.retained); } else { + next_tracker.rollback(); discard(); } if (depth24_guard.active) { diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 619d538274..c8bcd4db83 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -47,6 +47,7 @@ #include #include #include +#include #include #include #include @@ -626,10 +627,35 @@ class LayoutTracker { Entry entry{resource.mip_levels, resource.array_layers, two_planes(resource) ? 2u : 1u, separate_ && two_planes(resource), {}}; entry.layouts.assign(std::size_t(entry.mips) * entry.layers * (entry.two_planes ? 2 : 1), layout); + note(id); entries_[id] = std::move(entry); } - void forget(ResourceId id) { entries_.erase(id); } + void forget(ResourceId id) { + note(id); + entries_.erase(id); + } + + // A submission changes the tracker in place and takes its changes back when it does not reach the queue: begin() + // starts noting what each changed image had, rollback() puts that back, commit() keeps the changes. (A copy of the + // tracker for every submission cost an allocation per tracked image: with thousands of streamed textures it took + // most of the submitting thread.) + void begin() { + undo_.clear(); + noting_ = true; + } + void commit() { + undo_.clear(); + noting_ = false; + } + void rollback() { + noting_ = false; + for (auto& [id, before] : undo_) { + if (before) entries_[id] = std::move(*before); + else entries_.erase(id); + } + undo_.clear(); + } bool tracked(ResourceId id) const { return entries_.count(id) != 0; } // Current layout of one D3D12 subresource; UNDEFINED when the resource or index is unknown. @@ -672,6 +698,7 @@ class LayoutTracker { void set(ResourceId id, const VkImageSubresourceRange& range, VkImageLayout layout) { const auto it = entries_.find(id); if (it == entries_.end()) return; + note(id); Entry& e = it->second; visit(e, range, [&](std::size_t index, VkImageAspectFlags, std::uint32_t, std::uint32_t) { e.layouts[index] = layout; @@ -706,8 +733,17 @@ class LayoutTracker { } } + // Between begin() and commit() or rollback(): keeps what `id` had before its first change (nothing: untracked). + void note(ResourceId id) { + if (!noting_ || undo_.count(id)) return; + const auto it = entries_.find(id); + undo_.emplace(id, it == entries_.end() ? std::optional{} : std::optional{it->second}); + } + bool separate_; std::unordered_map entries_; + std::unordered_map> undo_; + bool noting_ = false; }; // Each physical slot records one non-overlapping interval. Logical queries can share slots. diff --git a/runtime/test/replay_texture_barrier_test.cpp b/runtime/test/replay_texture_barrier_test.cpp index 66c594779e..8639b9b1a1 100644 --- a/runtime/test/replay_texture_barrier_test.cpp +++ b/runtime/test/replay_texture_barrier_test.cpp @@ -170,6 +170,29 @@ int main() { } } + // A submission that does not reach the queue takes its changes back; one that does keeps them. + { + constexpr ResourceId other = 2; + const VkImageSubresourceRange mip1{VK_IMAGE_ASPECT_COLOR_BIT, 1, 1, 0, 1}; + const VkImageLayout mip0 = tracker.layout(id, 0), was = tracker.layout(id, 1); + tracker.begin(); + tracker.set(id, mip1, transfer_src); + tracker.set(id, mip1, transfer_dst); // the second change of an image keeps what the first one noted + tracker.track(other, image, shader_read); + tracker.rollback(); + expect(tracker.layout(id, 1) == was && tracker.layout(id, 0) == mip0, "rollback puts a changed layout back"); + expect(!tracker.tracked(other), "rollback forgets an image tracked since begin"); + tracker.begin(); + tracker.forget(id); + tracker.rollback(); + expect(tracker.tracked(id) && tracker.layout(id, 1) == was, "rollback brings back a forgotten image"); + tracker.begin(); + tracker.set(id, mip1, transfer_src); + tracker.commit(); + tracker.rollback(); // nothing noted: no change + expect(tracker.layout(id, 1) == transfer_src && tracker.layout(id, 0) == mip0, "commit keeps the change"); + } + if (failures) return 1; std::printf("replay_texture_barrier_test: OK\n"); return 0; From 5959a09f53223eb26dad2c02da340d8045d66b11 Mon Sep 17 00:00:00 2001 From: DadaCap <167002618+DadaCap@users.noreply.github.com> Date: Tue, 24 Nov 2026 22:53:03 +0100 Subject: [PATCH 2/2] vulkan: a barrier that keeps the layout leaves the tracker alone A texture barrier or an attachment reuse whose pieces are already in the wanted layout still orders memory, but called LayoutTracker::set() and so copied the image's layouts into the undo notes for nothing. Patch and test by akimfomin, from the review of the pull request: the two paths use the layouts they already read. Discards and real layout changes still update the tracker. Co-Authored-By: Claude Fable 5.2 --- runtime/src/vulkan_replay.h | 9 ++- runtime/test/replay_texture_barrier_test.cpp | 59 ++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index c8bcd4db83..0ec4c39669 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -2322,6 +2322,7 @@ inline void to_attachment(const ReplayFunctions& vk, VkCommandBuffer command_buf const VkImageSubresourceRange range = planes_for_layout(a.binding, a.range, separate); auto pieces = current(tracker, a.id, range, layout); initialize(vk, command_buffer, a.binding, a.id, pieces, layout, tracker); + bool layout_changed = false; for (const auto& piece : pieces) { if (piece.layout == layout) { // Attachment writes of an earlier rendering scope are not ordered with this one without a barrier. @@ -2330,8 +2331,9 @@ inline void to_attachment(const ReplayFunctions& vk, VkCommandBuffer command_buf } image_barrier(vk, command_buffer, a.binding, piece.range, piece.layout, layout, VK_ACCESS_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, access, stages); + layout_changed = true; } - if (tracker) tracker->set(a.id, range, layout); + if (tracker && layout_changed) tracker->set(a.id, range, layout); } } // namespace detail @@ -3674,7 +3676,9 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c if (pieces.size() > few.size()) many.resize(pieces.size()); VkImageMemoryBarrier2* const barriers = many.empty() ? few.data() : many.data(); std::uint32_t barrier_count = 0; + bool layout_changed = discard; for (const auto& piece : pieces) { + layout_changed |= piece.layout != new_layout; VkImageMemoryBarrier2& b = barriers[barrier_count++]; b = {VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2}; b.srcStageMask = src.stages; @@ -3694,7 +3698,8 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c dependency.imageMemoryBarrierCount = barrier_count; dependency.pImageMemoryBarriers = barriers; vk.pipeline_barrier2(command_buffer, &dependency); - if (tracker) tracker->set(id, range, new_layout); + // Same-layout barriers still order memory, but leave nothing to journal. + if (tracker && layout_changed) tracker->set(id, range, new_layout); } else if (const auto* color_clear = std::get_if(&command)) { if (p.skip) continue; const VulkanAccess state = vulkan_access(d3d12_state::render_target, false); diff --git a/runtime/test/replay_texture_barrier_test.cpp b/runtime/test/replay_texture_barrier_test.cpp index 8639b9b1a1..49db7a4fea 100644 --- a/runtime/test/replay_texture_barrier_test.cpp +++ b/runtime/test/replay_texture_barrier_test.cpp @@ -68,6 +68,12 @@ void VKAPI_CALL record_barrier2(VkCommandBuffer, const VkDependencyInfo* info) { g_barriers.push_back(info->pImageMemoryBarriers[i]); } +void VKAPI_CALL record_memory_barrier(VkCommandBuffer, VkPipelineStageFlags, VkPipelineStageFlags, VkDependencyFlags, + std::uint32_t count, const VkMemoryBarrier* barriers, std::uint32_t, + const VkBufferMemoryBarrier*, std::uint32_t, const VkImageMemoryBarrier*) { + if (count == 1 && barriers[0].srcAccessMask && barriers[0].dstAccessMask) ++g_calls; +} + int failures = 0; void expect(bool ok, const char* what) { if (!ok) { @@ -193,6 +199,59 @@ int main() { expect(tracker.layout(id, 1) == transfer_src && tracker.layout(id, 0) == mip0, "commit keeps the change"); } + // Reusing a render-target layout still needs synchronization, but does not need an undo entry. + { + const VkImageSubresourceRange all_mips{VK_IMAGE_ASPECT_COLOR_BIT, 0, mips, 0, 1}; + const VkImageSubresourceRange last_mip{VK_IMAGE_ASPECT_COLOR_BIT, mips - 1, 1, 0, 1}; + tracker.set(id, all_mips, attachment); + tracker.begin(); + tracker.set(id, last_mip, transfer_src); + tracker.rollback(); // warm the undo map's buckets before measuring + + CommandList list; + expect(list.barrier(TextureBarrier{y::render_target, y::render_target, a::render_target, a::render_target, + l::render_target, l::render_target, id, all, 0}) && + list.close(), + "record a barrier that keeps the render-target layout"); + PreparedList prepared; + expect(prepare(list, resolve, vk, prepared) == ReplayResult::success, "prepare a same-layout barrier"); + g_barriers.clear(); + g_calls = 0; + tracker.begin(); + const std::size_t before = g_allocations; + emit(prepared, cb, vk, &tracker); + const std::size_t allocations = g_allocations - before; + if (allocations) std::fprintf(stderr, "same-layout emit allocated %zu times\n", allocations); + expect(allocations == 0, "same-layout emit does not allocate an undo entry"); + expect(g_calls == 1 && g_barriers.size() == 1 && g_barriers[0].oldLayout == attachment && + g_barriers[0].newLayout == attachment && g_barriers[0].srcAccessMask && g_barriers[0].dstAccessMask, + "same-layout emit still records one barrier with both access scopes"); + tracker.set(id, last_mip, transfer_src); + tracker.rollback(); + expect(tracker.layout(id, mips - 1) == attachment, "rollback restores a change after same-layout emit"); + + tracker.set(id, last_mip, transfer_dst); + g_barriers.clear(); + tracker.begin(); + emit(prepared, cb, vk, &tracker); // the first mips agree; only the last actually changes + expect(tracker.layout(id, mips - 1) == attachment, "update reaches the first different mip"); + tracker.rollback(); + expect(tracker.layout(id, 0) == attachment && tracker.layout(id, mips - 1) == transfer_dst, + "rollback restores the different mip without changing the others"); + + tracker.set(id, last_mip, attachment); + const detail::PreparedDraw::Attachment target{image, id, all_mips}; + vk.pipeline_barrier = record_memory_barrier; + g_calls = 0; + tracker.begin(); + const std::size_t before_attachment = g_allocations; + detail::to_attachment(vk, cb, target, attachment, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, + VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, &tracker, false); + expect(g_allocations == before_attachment, "reusing an attachment does not allocate an undo entry"); + expect(g_calls == 1, "reusing an attachment still orders its writes"); + tracker.commit(); + } + if (failures) return 1; std::printf("replay_texture_barrier_test: OK\n"); return 0;