diff options
| author | sds-228 <329233051+sds-228@users.noreply.github.com> | 2026-11-23 04:28:47 +0300 |
|---|---|---|
| committer | sds-228 <329233051+sds-228@users.noreply.github.com> | 2026-11-23 04:28:47 +0300 |
| commit | 7d71e8328ad6c3b41fa9ac47ea4eb27fdd3afa8a (patch) | |
| tree | 52f7119acdd5ebffecf88c4fd805be7587159081 /runtime | |
| parent | 03b09a086592a197db64d91655c6e65c30764d96 (diff) | |
vulkan: retain prepared-list storage across smaller submissions
resize() down destroyed the per-list vectors that prepare refills, so a queue alternating 8 lists and 1 list rebuilt them every frame. Grow the vector only, and iterate the current list count in the query and emit loops so retained entries are never replayed. Registers the existing vk_queue_test with CTest and adds an 8-then-1 GPU regression with a negative control.
AI-assisted (OpenCode)
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | runtime/src/vulkan_backend.h | 15 | ||||
| -rw-r--r-- | runtime/test/vk_queue_test.cpp | 41 |
3 files changed, 52 insertions, 6 deletions
diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index ed946d115..c834a6344 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt | |||
| @@ -869,7 +869,7 @@ if(TARGET Vulkan::Headers) | |||
| 869 | add_test(NAME depth24_convert COMMAND depth24_convert_test) | 869 | add_test(NAME depth24_convert COMMAND depth24_convert_test) |
| 870 | set_tests_properties(depth24_convert PROPERTIES SKIP_RETURN_CODE 77) | 870 | set_tests_properties(depth24_convert PROPERTIES SKIP_RETURN_CODE 77) |
| 871 | # Self-checking Vulkan tests with no shaders or game data; each exits 77 without a Vulkan device (TEST-514). | 871 | # Self-checking Vulkan tests with no shaders or game data; each exits 77 without a Vulkan device (TEST-514). |
| 872 | foreach(test vk_command_replay vk_predicated_copy vk_predicate_capture) | 872 | foreach(test vk_command_replay vk_predicated_copy vk_predicate_capture vk_queue) |
| 873 | add_test(NAME ${test} COMMAND ${test}_test) | 873 | add_test(NAME ${test} COMMAND ${test}_test) |
| 874 | set_tests_properties(${test} PROPERTIES SKIP_RETURN_CODE 77) | 874 | set_tests_properties(${test} PROPERTIES SKIP_RETURN_CODE 77) |
| 875 | endforeach() | 875 | endforeach() |
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index 03ab7ba26..97959be69 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h | |||
| @@ -1500,6 +1500,10 @@ public: | |||
| 1500 | 1500 | ||
| 1501 | bool ok() const { return ok_; } | 1501 | bool ok() const { return ok_; } |
| 1502 | 1502 | ||
| 1503 | // Entries kept between executions. Execute only uses the first lists.size(); a larger count is retained storage, | ||
| 1504 | // not work the next submission will replay (vk_queue_test checks both halves of that contract). | ||
| 1505 | std::size_t prepared_entries() const { return prepared_.size(); } | ||
| 1506 | |||
| 1503 | template<typename GetHeap> | 1507 | template<typename GetHeap> |
| 1504 | bool update_tiles(ResourceId id, std::span<const BufferTileRegion> regions, | 1508 | bool update_tiles(ResourceId id, std::span<const BufferTileRegion> regions, |
| 1505 | std::span<const BufferTileRange> ranges, GetHeap&& get_heap) { | 1509 | std::span<const BufferTileRange> ranges, GetHeap&& get_heap) { |
| @@ -1679,7 +1683,10 @@ private: | |||
| 1679 | // Kept between executions: prepare refills each list's entries in the storage the last execution left, where | 1683 | // Kept between executions: prepare refills each list's entries in the storage the last execution left, where |
| 1680 | // making them fresh for every command was about half the layer's CPU cost per command (replay_cpu_bench). | 1684 | // making them fresh for every command was about half the layer's CPU cost per command (replay_cpu_bench). |
| 1681 | auto& prepared = prepared_; | 1685 | auto& prepared = prepared_; |
| 1682 | prepared.resize(lists.size()); | 1686 | // Keep the storage from a larger batch. resize() down would destroy the unused entries and their vectors, so |
| 1687 | // a queue that sees 8 lists and then 1 in every frame would rebuild them on each execution. The loops below | ||
| 1688 | // use only the first lists.size() entries, so stale entries are never prepared, queried or emitted. | ||
| 1689 | if (prepared.size() < lists.size()) prepared.resize(lists.size()); | ||
| 1683 | ReplayFunctions replay = context_.replay_; | 1690 | ReplayFunctions replay = context_.replay_; |
| 1684 | replay.queue_stages = kind_ == QueueKind::copy ? detail::queue_stages_copy | 1691 | replay.queue_stages = kind_ == QueueKind::copy ? detail::queue_stages_copy |
| 1685 | : kind_ == QueueKind::compute ? detail::queue_stages_compute | 1692 | : kind_ == QueueKind::compute ? detail::queue_stages_compute |
| @@ -1797,7 +1804,8 @@ private: | |||
| 1797 | if (has_queries && !tracker_lock.owns_lock()) tracker_lock.lock(); | 1804 | if (has_queries && !tracker_lock.owns_lock()) tracker_lock.lock(); |
| 1798 | QueryReplay queries; | 1805 | QueryReplay queries; |
| 1799 | std::uint32_t base = 0, statistics_base = 0; | 1806 | std::uint32_t base = 0, statistics_base = 0; |
| 1800 | for (auto& list : prepared) { | 1807 | for (std::size_t i = 0; i < lists.size(); ++i) { |
| 1808 | auto& list = prepared[i]; | ||
| 1801 | const auto result = queries.plan(list, physical, base, can_resolve_occlusion, statistics, statistics_base); | 1809 | const auto result = queries.plan(list, physical, base, can_resolve_occlusion, statistics, statistics_base); |
| 1802 | if (result != ReplayResult::success) return result; | 1810 | if (result != ReplayResult::success) return result; |
| 1803 | base += list.segment_count; | 1811 | base += list.segment_count; |
| @@ -1893,7 +1901,8 @@ private: | |||
| 1893 | bool wrote_readback = false; | 1901 | bool wrote_readback = false; |
| 1894 | VkPipelineStageFlags2 readback_stages = 0; | 1902 | VkPipelineStageFlags2 readback_stages = 0; |
| 1895 | std::uint32_t open_labels = 0; // events may span the lists of one ExecuteCommandLists | 1903 | std::uint32_t open_labels = 0; // events may span the lists of one ExecuteCommandLists |
| 1896 | for (const auto& list : prepared) { | 1904 | for (std::size_t i = 0; i < lists.size(); ++i) { |
| 1905 | const auto& list = prepared[i]; | ||
| 1897 | emit(list, command_buffer, replay, &next_tracker, &open_labels); | 1906 | emit(list, command_buffer, replay, &next_tracker, &open_labels); |
| 1898 | wrote_readback |= list.wrote_readback; | 1907 | wrote_readback |= list.wrote_readback; |
| 1899 | readback_stages |= list.readback_src_stages; | 1908 | readback_stages |= list.readback_src_stages; |
diff --git a/runtime/test/vk_queue_test.cpp b/runtime/test/vk_queue_test.cpp index 84289198c..c4a1ee339 100644 --- a/runtime/test/vk_queue_test.cpp +++ b/runtime/test/vk_queue_test.cpp | |||
| @@ -94,17 +94,19 @@ int main() { | |||
| 94 | 94 | ||
| 95 | // Buffers: an upload source, a GPU-side destination and a readback, all host-visible for the test. | 95 | // Buffers: an upload source, a GPU-side destination and a readback, all host-visible for the test. |
| 96 | void *upload_map = nullptr, *mid_map = nullptr, *readback_map = nullptr, *tex_readback_map = nullptr; | 96 | void *upload_map = nullptr, *mid_map = nullptr, *readback_map = nullptr, *tex_readback_map = nullptr; |
| 97 | void* stale_src_map = nullptr; | ||
| 97 | const VkBufferUsageFlags usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; | 98 | const VkBufferUsageFlags usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; |
| 98 | VkBuffer upload = d.buffer(kSize, usage, &upload_map); | 99 | VkBuffer upload = d.buffer(kSize, usage, &upload_map); |
| 99 | VkBuffer mid = d.buffer(kSize, usage, &mid_map); | 100 | VkBuffer mid = d.buffer(kSize, usage, &mid_map); |
| 100 | VkBuffer readback = d.buffer(kSize, usage, &readback_map); | 101 | VkBuffer readback = d.buffer(kSize, usage, &readback_map); |
| 101 | VkBuffer tex_readback = d.buffer(kTex * kTex * 4, usage, &tex_readback_map); | 102 | VkBuffer tex_readback = d.buffer(kTex * kTex * 4, usage, &tex_readback_map); |
| 103 | VkBuffer stale_src = d.buffer(kSize, usage, &stale_src_map); | ||
| 102 | VkImage texture = d.image(VK_FORMAT_R8G8B8A8_UNORM, kTex, kTex, 1, VK_IMAGE_USAGE_SAMPLED_BIT); | 104 | VkImage texture = d.image(VK_FORMAT_R8G8B8A8_UNORM, kTex, kTex, 1, VK_IMAGE_USAGE_SAMPLED_BIT); |
| 103 | EXPECT(upload && mid && readback && tex_readback && texture); | 105 | EXPECT(upload && mid && readback && tex_readback && stale_src && texture); |
| 104 | auto* up = static_cast<std::uint8_t*>(upload_map); | 106 | auto* up = static_cast<std::uint8_t*>(upload_map); |
| 105 | for (std::uint32_t i = 0; i < kSize; ++i) up[i] = pattern(i, 7); | 107 | for (std::uint32_t i = 0; i < kSize; ++i) up[i] = pattern(i, 7); |
| 106 | 108 | ||
| 107 | const ResourceId upload_id = 1, mid_id = 2, readback_id = 3, tex_id = 4, tex_readback_id = 5; | 109 | const ResourceId upload_id = 1, mid_id = 2, readback_id = 3, tex_id = 4, tex_readback_id = 5, stale_src_id = 7; |
| 108 | auto add_buffer = [&](ResourceId id, VkBuffer b, VkDeviceSize size) { | 110 | auto add_buffer = [&](ResourceId id, VkBuffer b, VkDeviceSize size) { |
| 109 | ResourceBinding r; | 111 | ResourceBinding r; |
| 110 | r.buffer = b; | 112 | r.buffer = b; |
| @@ -115,6 +117,7 @@ int main() { | |||
| 115 | add_buffer(mid_id, mid, kSize); | 117 | add_buffer(mid_id, mid, kSize); |
| 116 | add_buffer(readback_id, readback, kSize); | 118 | add_buffer(readback_id, readback, kSize); |
| 117 | add_buffer(tex_readback_id, tex_readback, kTex * kTex * 4); | 119 | add_buffer(tex_readback_id, tex_readback, kTex * kTex * 4); |
| 120 | add_buffer(stale_src_id, stale_src, kSize); | ||
| 118 | ResourceBinding tex; | 121 | ResourceBinding tex; |
| 119 | tex.image = texture; | 122 | tex.image = texture; |
| 120 | tex.format = VK_FORMAT_R8G8B8A8_UNORM; | 123 | tex.format = VK_FORMAT_R8G8B8A8_UNORM; |
| @@ -182,6 +185,40 @@ int main() { | |||
| 182 | std::printf("direct -> compute: %u bad bytes\n", bad); | 185 | std::printf("direct -> compute: %u bad bytes\n", bad); |
| 183 | EXPECT(bad == 0); | 186 | EXPECT(bad == 0); |
| 184 | 187 | ||
| 188 | // 3b. A smaller ExecuteCommandLists must keep the prepared storage but must not replay stale entries. | ||
| 189 | // Eight lists, each copying a non-overlapping chunk from a source that stays at 0xaa, grow prepared_ to eight. | ||
| 190 | // A one-list submission then copies only its own chunk from the upload source, now 0xbb. With resize() down and | ||
| 191 | // range-for over all entries, the seven stale copies run after that one and put 0xaa back into mid. | ||
| 192 | constexpr std::uint32_t kChunk = kSize / 8; | ||
| 193 | auto* old = static_cast<std::uint8_t*>(stale_src_map); | ||
| 194 | std::memset(old, 0xaa, kSize); | ||
| 195 | CommandList stale[8]; | ||
| 196 | const CommandList* many[8]; | ||
| 197 | for (std::uint32_t i = 0; i < 8; ++i) { | ||
| 198 | EXPECT(stale[i].copy_buffer({mid_id, i * kChunk, stale_src_id, i * kChunk, kChunk})); | ||
| 199 | EXPECT(stale[i].close()); | ||
| 200 | many[i] = &stale[i]; | ||
| 201 | } | ||
| 202 | EXPECT(direct.execute(many) == ReplayResult::success); | ||
| 203 | EXPECT(direct.signal(fence.semaphore(), 10)); | ||
| 204 | EXPECT(fence.wait(10, 5'000'000'000ull)); | ||
| 205 | auto* md = static_cast<std::uint8_t*>(mid_map); | ||
| 206 | std::memset(md, 0, kSize); | ||
| 207 | for (std::uint32_t i = 0; i < kSize; ++i) up[i] = 0xbb; | ||
| 208 | CommandList final_copy; | ||
| 209 | EXPECT(final_copy.copy_buffer({mid_id, 7 * kChunk, upload_id, 7 * kChunk, kChunk})); | ||
| 210 | EXPECT(final_copy.close()); | ||
| 211 | const CommandList* one[1] = {&final_copy}; | ||
| 212 | EXPECT(direct.execute(one) == ReplayResult::success); | ||
| 213 | EXPECT(direct.signal(fence.semaphore(), 11)); | ||
| 214 | EXPECT(fence.wait(11, 5'000'000'000ull)); | ||
| 215 | EXPECT(direct.prepared_entries() == 8); | ||
| 216 | bad = 0; | ||
| 217 | for (std::uint32_t i = 0; i < kSize; ++i) | ||
| 218 | bad += md[i] != (i >= 7 * kChunk ? 0xbb : 0x00); | ||
| 219 | std::printf("prepared storage: %zu entries kept, %u stale bytes\n", direct.prepared_entries(), bad); | ||
| 220 | EXPECT(bad == 0); | ||
| 221 | |||
| 185 | // 4. A copy queue refuses a render-target clear and submits nothing. | 222 | // 4. A copy queue refuses a render-target clear and submits nothing. |
| 186 | VkImage rt_image = d.image(VK_FORMAT_R8G8B8A8_UNORM, 8, 8, 1, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); | 223 | VkImage rt_image = d.image(VK_FORMAT_R8G8B8A8_UNORM, 8, 8, 1, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); |
| 187 | ResourceBinding rt; | 224 | ResourceBinding rt; |