diff options
| author | Tauwni <102470988+Tauwni@users.noreply.github.com> | 2026-11-26 12:19:12 +0100 |
|---|---|---|
| committer | Tauwni <102470988+Tauwni@users.noreply.github.com> | 2026-11-26 12:19:12 +0100 |
| commit | beffaa9a476ca1b3dfd9cfa55e39eafbb1f5eec6 (patch) | |
| tree | 32877805bf4eb46d9a89d5753db6b5a2b06dc845 /runtime/test/geometry_views_test.cpp | |
| parent | f29ca7bacae9507f83e8c56abdb604bd4efb4cf1 (diff) | |
| parent | a73a57e1339be072baf5522351c8ec7f1482b56b (diff) | |
Merge main into fix/null-descriptor-generated-dir
Diffstat (limited to 'runtime/test/geometry_views_test.cpp')
| -rw-r--r-- | runtime/test/geometry_views_test.cpp | 355 |
1 files changed, 352 insertions, 3 deletions
diff --git a/runtime/test/geometry_views_test.cpp b/runtime/test/geometry_views_test.cpp index 920283f41..caac93dc0 100644 --- a/runtime/test/geometry_views_test.cpp +++ b/runtime/test/geometry_views_test.cpp | |||
| @@ -4,16 +4,31 @@ | |||
| 4 | // 1. a new view is copied whole; | 4 | // 1. a new view is copied whole; |
| 5 | // 2. a write into one tile of a 1 MiB view (16 tiles) copies that tile's 64 KiB again, not the view; | 5 | // 2. a write into one tile of a 1 MiB view (16 tiles) copies that tile's 64 KiB again, not the view; |
| 6 | // 3. a write into a tile two views share is read once and copied into both; | 6 | // 3. a write into a tile two views share is read once and copied into both; |
| 7 | // 4. two written tiles of one view are two tile copies (LEONIDA_GEOMETRY_WHOLE_VIEWS=1, whole views, fails 2-4). | 7 | // 4. two written tiles of one view are two tile copies (LEONIDA_GEOMETRY_WHOLE_VIEWS=1, whole views, fails 2-4); |
| 8 | // 5. a small cap refuses past its budget without re-trying while nothing changed; | ||
| 9 | // 6. retiring a range defers freeing until its GPU ticket completes, then returns budget and lets refused ranges retry; | ||
| 10 | // 7. releasing every view in a sub-4 MiB block via geometry_writes::note_released destroys the block and frees its budget; | ||
| 11 | // 8. note_released ignores ranges outside the watched tile bounds and queues ranges inside them; | ||
| 12 | // 9. overflowing the release queue (65537 disjoint ranges) collapses to one covering range and retires covered views; | ||
| 13 | // 10. reaping the last view in a standard 4 MiB block keeps it as a spare and the next view reuses it; | ||
| 14 | // 11. under a cap, an unfitting 4 MiB spare is destroyed so a larger request can succeed (or fail if still over cap); | ||
| 15 | // 12. one note_released reaches every GeometryViews instance, and a destroyed instance unsubscribes cleanly; | ||
| 16 | // 13. a submission's sequence on an idle GPU (completed == submitted): a view retired by the drain in before_execute_ | ||
| 17 | // (kAfterExecutes) is freed only after every replay begun before the retirement recorded its submissions and those | ||
| 18 | // completed (a ticket read in the drain reaps it at once); its answers made without an alias are invalidated | ||
| 19 | // (forget_copy); its destroyed block leaves the lock-free block list and is reported to the device. | ||
| 8 | #include <windows.h> | 20 | #include <windows.h> |
| 9 | 21 | ||
| 10 | #include <cstdint> | 22 | #include <cstdint> |
| 11 | #include <cstdio> | 23 | #include <cstdio> |
| 12 | #include <cstdlib> | 24 | #include <cstdlib> |
| 13 | #include <cstring> | 25 | #include <cstring> |
| 26 | #include <utility> | ||
| 27 | #include <memory> | ||
| 14 | #include <vector> | 28 | #include <vector> |
| 15 | 29 | ||
| 16 | #include "../shims/d3d12_geometry_views.h" | 30 | #include "../shims/d3d12_geometry_views.h" |
| 31 | #include "../src/title_va_alias.h" | ||
| 17 | 32 | ||
| 18 | namespace { | 33 | namespace { |
| 19 | 34 | ||
| @@ -27,6 +42,10 @@ void check(bool condition, const char* what) { | |||
| 27 | } | 42 | } |
| 28 | 43 | ||
| 29 | // The Vulkan calls TitleBufferWindows::allocate makes, over host memory. | 44 | // The Vulkan calls TitleBufferWindows::allocate makes, over host memory. |
| 45 | int g_live_buffers = 0; | ||
| 46 | int g_gone_blocks = 0; // FakeDevice::title_block_gone calls | ||
| 47 | int g_live_allocs = 0; | ||
| 48 | int g_total_allocs = 0; | ||
| 30 | struct FakeBuffer { | 49 | struct FakeBuffer { |
| 31 | VkDeviceSize size = 0; | 50 | VkDeviceSize size = 0; |
| 32 | void* memory = nullptr; | 51 | void* memory = nullptr; |
| @@ -34,6 +53,7 @@ struct FakeBuffer { | |||
| 34 | VKAPI_ATTR VkResult VKAPI_CALL create_buffer(VkDevice, const VkBufferCreateInfo* info, const VkAllocationCallbacks*, | 53 | VKAPI_ATTR VkResult VKAPI_CALL create_buffer(VkDevice, const VkBufferCreateInfo* info, const VkAllocationCallbacks*, |
| 35 | VkBuffer* out) { | 54 | VkBuffer* out) { |
| 36 | auto* b = new FakeBuffer{info->size}; | 55 | auto* b = new FakeBuffer{info->size}; |
| 56 | ++g_live_buffers; | ||
| 37 | *out = reinterpret_cast<VkBuffer>(b); | 57 | *out = reinterpret_cast<VkBuffer>(b); |
| 38 | return VK_SUCCESS; | 58 | return VK_SUCCESS; |
| 39 | } | 59 | } |
| @@ -57,9 +77,11 @@ VKAPI_ATTR VkDeviceAddress VKAPI_CALL address(VkDevice, const VkBufferDeviceAddr | |||
| 57 | return VkDeviceAddress(reinterpret_cast<std::uintptr_t>(reinterpret_cast<FakeBuffer*>(info->buffer)->memory)); | 77 | return VkDeviceAddress(reinterpret_cast<std::uintptr_t>(reinterpret_cast<FakeBuffer*>(info->buffer)->memory)); |
| 58 | } | 78 | } |
| 59 | VKAPI_ATTR void VKAPI_CALL destroy_buffer(VkDevice, VkBuffer buffer, const VkAllocationCallbacks*) { | 79 | VKAPI_ATTR void VKAPI_CALL destroy_buffer(VkDevice, VkBuffer buffer, const VkAllocationCallbacks*) { |
| 80 | --g_live_buffers; | ||
| 60 | delete reinterpret_cast<FakeBuffer*>(buffer); | 81 | delete reinterpret_cast<FakeBuffer*>(buffer); |
| 61 | } | 82 | } |
| 62 | VKAPI_ATTR void VKAPI_CALL free_memory(VkDevice, VkDeviceMemory memory, const VkAllocationCallbacks*) { | 83 | VKAPI_ATTR void VKAPI_CALL free_memory(VkDevice, VkDeviceMemory memory, const VkAllocationCallbacks*) { |
| 84 | --g_live_allocs; | ||
| 63 | VirtualFree(memory, 0, MEM_RELEASE); | 85 | VirtualFree(memory, 0, MEM_RELEASE); |
| 64 | } | 86 | } |
| 65 | 87 | ||
| @@ -85,10 +107,16 @@ struct FakeDevice { | |||
| 85 | VkDeviceAddress address = 0; | 107 | VkDeviceAddress address = 0; |
| 86 | } arena; | 108 | } arena; |
| 87 | void share(VkBufferCreateInfo&) {} | 109 | void share(VkBufferCreateInfo&) {} |
| 110 | void title_block_gone(VkDeviceAddress, VkDeviceSize) { ++g_gone_blocks; } | ||
| 88 | VkDeviceMemory allocate(const VkMemoryRequirements& r, VkMemoryPropertyFlags required, | 111 | VkDeviceMemory allocate(const VkMemoryRequirements& r, VkMemoryPropertyFlags required, |
| 89 | VkMemoryPropertyFlags preferred, VkMemoryPropertyFlags* chosen, bool) { | 112 | VkMemoryPropertyFlags preferred, VkMemoryPropertyFlags* chosen, bool) { |
| 90 | if (chosen) *chosen = required | preferred; | 113 | if (chosen) *chosen = required | preferred; |
| 91 | return reinterpret_cast<VkDeviceMemory>(VirtualAlloc(nullptr, r.size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE)); | 114 | void* mem = VirtualAlloc(nullptr, r.size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); |
| 115 | if (mem) { | ||
| 116 | ++g_live_allocs; | ||
| 117 | ++g_total_allocs; | ||
| 118 | } | ||
| 119 | return reinterpret_cast<VkDeviceMemory>(mem); | ||
| 92 | } | 120 | } |
| 93 | }; | 121 | }; |
| 94 | 122 | ||
| @@ -118,7 +146,8 @@ int main() { | |||
| 118 | const auto base = reinterpret_cast<std::uintptr_t>(title); | 146 | const auto base = reinterpret_cast<std::uintptr_t>(title); |
| 119 | 147 | ||
| 120 | FakeDevice device; | 148 | FakeDevice device; |
| 121 | rt::d3d12::GeometryViews views; | 149 | const auto views_owner = std::make_unique<rt::d3d12::GeometryViews>(); |
| 150 | auto& views = *views_owner; | ||
| 122 | g_copies.clear(); | 151 | g_copies.clear(); |
| 123 | const std::uint64_t va = views.resolve(device, base, kView, copy); | 152 | const std::uint64_t va = views.resolve(device, base, kView, copy); |
| 124 | check(va != 0 && g_copies.size() == 1 && g_copies[0] == kView, "a new 1 MiB view is copied whole"); | 153 | check(va != 0 && g_copies.size() == 1 && g_copies[0] == kView, "a new 1 MiB view is copied whole"); |
| @@ -161,6 +190,326 @@ int main() { | |||
| 161 | check(!std::memcmp(copy_bytes, title, kView), "the copy matches the title's pages"); | 190 | check(!std::memcmp(copy_bytes, title, kView), "the copy matches the title's pages"); |
| 162 | 191 | ||
| 163 | views.release(device); | 192 | views.release(device); |
| 193 | VirtualFree(title, 0, MEM_RELEASE); | ||
| 194 | |||
| 195 | // 5-7. Small cap (2 MiB): fill, cap refusal (cached while nothing freed), deferred retirement for in-flight GPU | ||
| 196 | // work, retry after reap, and full block reclamation via geometry_writes::note_released. | ||
| 197 | auto* small_title = | ||
| 198 | static_cast<std::uint8_t*>(VirtualAlloc(nullptr, 3 * kView, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE)); | ||
| 199 | if (!small_title) return 1; | ||
| 200 | for (std::size_t i = 0; i < 3 * kView; ++i) small_title[i] = std::uint8_t((i * 13) ^ 0x5a); | ||
| 201 | const auto sbase = reinterpret_cast<std::uintptr_t>(small_title); | ||
| 202 | const auto range_a = sbase; | ||
| 203 | const auto range_b = sbase + kView; | ||
| 204 | const auto range_c = sbase + 2 * kView; | ||
| 205 | |||
| 206 | const auto capped_owner = std::make_unique<rt::d3d12::GeometryViews>(2 * kView); | ||
| 207 | |||
| 208 | auto& capped = *capped_owner; | ||
| 209 | const std::uint64_t va_a = capped.resolve(device, range_a, kView, copy); | ||
| 210 | const std::uint64_t va_b = capped.resolve(device, range_b, kView, copy); | ||
| 211 | check(va_a != 0 && va_b != 0 && capped.bytes() == 2 * kView && capped.reserved() == 2 * kView && | ||
| 212 | capped.live_views() == 2, | ||
| 213 | "small cap fills to its 2 MiB budget"); | ||
| 214 | |||
| 215 | // 5. Third 1 MiB view is refused; a repeat resolve without freeing short-circuits without re-counting a refusal. | ||
| 216 | const std::uint64_t va_c1 = capped.resolve(device, range_c, kView, copy); | ||
| 217 | check(va_c1 == 0 && capped.cap_refusals() == 1, "view past the 2 MiB cap is refused"); | ||
| 218 | const std::uint64_t va_c2 = capped.resolve(device, range_c, kView, copy); | ||
| 219 | check(va_c2 == 0 && capped.cap_refusals() == 1, | ||
| 220 | "repeated resolve of a refused range short-circuits while nothing has been freed"); | ||
| 221 | |||
| 222 | // 6. Retire A while GPU ticket 5 is still in flight: A disappears from lookup/find immediately, but its storage | ||
| 223 | // and budget are held until completed_ticket reaches 5. | ||
| 224 | std::uint64_t alias_removed_source = 0; | ||
| 225 | VkDeviceSize alias_removed_va = 0; | ||
| 226 | const std::size_t retired_a = capped.retire_range( | ||
| 227 | range_a, range_a + kView, 5, [&](std::uintptr_t src, std::size_t, VkDeviceSize v) { | ||
| 228 | alias_removed_source = src; | ||
| 229 | alias_removed_va = v; | ||
| 230 | }); | ||
| 231 | check(retired_a == 1 && alias_removed_source == range_a && alias_removed_va == va_a, | ||
| 232 | "retiring A's range reports A's view and invokes the alias-removal callback"); | ||
| 233 | std::uint64_t lookup_host = 0, lookup_bytes = 0; | ||
| 234 | check(capped.find(range_a, kView) == 0 && !capped.lookup(device.arena.address + va_a, lookup_host, lookup_bytes) && | ||
| 235 | capped.live_views() == 1 && capped.retired_count() == 1, | ||
| 236 | "retired view is immediately unlinked from find and lookup"); | ||
| 237 | |||
| 238 | check(capped.reap(device, 4) == 0 && capped.retired_count() == 1 && capped.bytes() == 2 * kView && | ||
| 239 | capped.free_generation() == 0 && g_live_buffers == 1 && g_live_allocs == 1, | ||
| 240 | "reap before GPU ticket completes does not free an in-use view"); | ||
| 241 | check(capped.resolve(device, range_c, kView, copy) == 0 && capped.cap_refusals() == 1, | ||
| 242 | "refused range stays refused while retired view is still in flight"); | ||
| 243 | |||
| 244 | // GPU completes ticket 5: A's sub-range is reclaimed and free_generation bumps, letting C retry and succeed. | ||
| 245 | check(capped.reap(device, 5) == 1 && capped.retired_count() == 0 && capped.bytes() == kView && | ||
| 246 | capped.free_generation() == 1 && capped.block_count() == 1, | ||
| 247 | "reap at GPU ticket 5 reclaims A's budget while keeping B's block live"); | ||
| 248 | const std::uint64_t va_c3 = capped.resolve(device, range_c, kView, copy); | ||
| 249 | check(va_c3 != 0 && capped.bytes() == 2 * kView && capped.live_views() == 2, | ||
| 250 | "previously refused range retries and succeeds after budget is freed"); | ||
| 251 | check(capped.lookup(device.arena.address + va_c3, lookup_host, lookup_bytes) && lookup_bytes == kView && | ||
| 252 | !std::memcmp(reinterpret_cast<const void*>(lookup_host), small_title + 2 * kView, kView), | ||
| 253 | "retried view holds C's title bytes"); | ||
| 254 | |||
| 255 | // 7. Release B and C via the xmem notification bridge (geometry_writes::note_released + drain_released): | ||
| 256 | // once their GPU ticket completes, the block becomes empty and its Vulkan buffer and memory are destroyed. | ||
| 257 | leonida::geometry_writes::note_released(range_b, range_c + kView); | ||
| 258 | check(capped.has_released(), "note_released queues released title ranges"); | ||
| 259 | capped.drain_released(device, 6, 5); | ||
| 260 | check(!capped.has_released() && capped.live_views() == 0 && capped.retired_count() == 2 && | ||
| 261 | g_live_buffers == 1 && g_live_allocs == 1, | ||
| 262 | "drain_released retires B and C at ticket 6 and defers freeing while completed ticket is 5"); | ||
| 263 | check(capped.reap(device, 6) == 2 && capped.retired_count() == 0 && capped.bytes() == 0 && | ||
| 264 | capped.reserved() == 0 && capped.block_count() == 0 && g_live_buffers == 0 && g_live_allocs == 0, | ||
| 265 | "reaping the last views in a block destroys its Vulkan buffer and returns all reserved budget"); | ||
| 266 | |||
| 267 | // A fresh 2 MiB view now fits in the fully reclaimed cap. | ||
| 268 | const std::uint64_t va_full = capped.resolve(device, range_a, 2 * kView, copy); | ||
| 269 | check(va_full != 0 && capped.bytes() == 2 * kView && capped.reserved() == 2 * kView && capped.live_views() == 1, | ||
| 270 | "a fresh 2 MiB view succeeds after full reclamation"); | ||
| 271 | |||
| 272 | capped.release(device); | ||
| 273 | check(g_live_buffers == 0 && g_live_allocs == 0, "teardown frees all remaining Vulkan buffers and memory"); | ||
| 274 | VirtualFree(small_title, 0, MEM_RELEASE); | ||
| 275 | |||
| 276 | // 8-11. Watched-bounds filtering, release-queue overflow collapse, 4 MiB spare block reuse and cap eviction. | ||
| 277 | auto* pool_title = | ||
| 278 | static_cast<std::uint8_t*>(VirtualAlloc(nullptr, 6 * kView, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE)); | ||
| 279 | if (!pool_title) return 1; | ||
| 280 | for (std::size_t i = 0; i < 6 * kView; ++i) pool_title[i] = std::uint8_t((i * 17) ^ 0x3c); | ||
| 281 | const auto pbase = reinterpret_cast<std::uintptr_t>(pool_title); | ||
| 282 | |||
| 283 | // 8. note_released outside [watched_lo, watched_hi) is ignored; inside is queued. | ||
| 284 | const auto watched_owner = std::make_unique<rt::d3d12::GeometryViews>(6 * kView); | ||
| 285 | auto& watched = *watched_owner; | ||
| 286 | const std::uint64_t va_w1 = watched.resolve(device, pbase, kView, copy); | ||
| 287 | const std::uint64_t va_w2 = watched.resolve(device, pbase + kView, kView, copy); | ||
| 288 | check(va_w1 != 0 && va_w2 != 0 && watched.live_views() == 2 && watched.block_count() == 1 && | ||
| 289 | watched.empty_blocks() == 0, | ||
| 290 | "two 1 MiB views share one 4 MiB block"); | ||
| 291 | const auto watched_lo = leonida::geometry_writes::state().watched_lo.load(std::memory_order_acquire); | ||
| 292 | const auto watched_hi = leonida::geometry_writes::state().watched_hi.load(std::memory_order_acquire); | ||
| 293 | check(watched_lo <= pbase && watched_hi >= pbase + 2 * kView, "watched bounds cover resolved views"); | ||
| 294 | leonida::geometry_writes::note_released(watched_lo - 4096, watched_lo); | ||
| 295 | leonida::geometry_writes::note_released(watched_hi, watched_hi + 4096); | ||
| 296 | check(!watched.has_released(), "note_released outside watched bounds is not queued"); | ||
| 297 | leonida::geometry_writes::note_released(pbase, pbase + 4096); | ||
| 298 | check(watched.has_released(), "note_released inside watched bounds is queued"); | ||
| 299 | std::vector<std::pair<std::uintptr_t, std::uintptr_t>> drained; | ||
| 300 | leonida::geometry_writes::take_released(watched.release_queue(), drained); | ||
| 301 | check(drained.size() == 1 && drained[0].first == pbase && drained[0].second == pbase + 4096 && | ||
| 302 | !watched.has_released(), | ||
| 303 | "take_released returns the queued in-bounds range"); | ||
| 304 | // A GPU-only twin freed by its own address lies outside the watched (CPU) bounds but still counts: its CPU view | ||
| 305 | // holds the views. Only the address arithmetic of the pair is used; nothing is mapped at the GPU address. | ||
| 306 | const std::uintptr_t gpu_twin = std::uintptr_t(0x600000000000ull); | ||
| 307 | leonida::title_gpu_views::note_reservation(pbase, 2 * kView, false); | ||
| 308 | check(leonida::title_gpu_views::note_reservation(gpu_twin, 2 * kView, true), "the GPU-only twin pairs"); | ||
| 309 | leonida::geometry_writes::note_released(gpu_twin + 2 * kView, gpu_twin + 2 * kView + 4096); | ||
| 310 | check(!watched.has_released(), "a range past the twin is not queued"); | ||
| 311 | leonida::geometry_writes::note_released(gpu_twin + kView, gpu_twin + kView + 4096); | ||
| 312 | check(watched.has_released(), "a range of the GPU-only twin over a watched view is queued"); | ||
| 313 | watched.drain_released(device, 3, 0); | ||
| 314 | check(watched.live_views() == 1 && watched.retired_count() == 1 && watched.find(pbase + kView, kView) == 0 && | ||
| 315 | watched.find(pbase, kView) == va_w1, | ||
| 316 | "draining the twin's range retires the view at its CPU address only"); | ||
| 317 | const std::uint64_t va_w2b = watched.resolve(device, pbase + kView, kView, copy); | ||
| 318 | check(va_w2b != 0, "the retired view is made again on the next resolve"); | ||
| 319 | |||
| 320 | // 9. Queue overflow: 65537 disjoint ranges collapse into one covering range; drain retires every covered view. | ||
| 321 | constexpr std::size_t kOverflowCount = 65537; | ||
| 322 | for (std::size_t i = 0; i < kOverflowCount; ++i) | ||
| 323 | leonida::geometry_writes::note_released(pbase + i * 16, pbase + i * 16 + 8); | ||
| 324 | leonida::geometry_writes::take_released(watched.release_queue(), drained); | ||
| 325 | check(drained.size() == 1 && drained[0].first == pbase && | ||
| 326 | drained[0].second == pbase + (kOverflowCount - 1) * 16 + 8 && !watched.has_released(), | ||
| 327 | "65537 disjoint ranges collapse into a single covering range"); | ||
| 328 | for (std::size_t i = 0; i < kOverflowCount; ++i) | ||
| 329 | leonida::geometry_writes::note_released(pbase + i * 16, pbase + i * 16 + 8); | ||
| 330 | watched.drain_released(device, 10, 9); | ||
| 331 | check(!watched.has_released() && watched.live_views() == 0 && watched.retired_count() == 2, | ||
| 332 | "drain_released after overflow retires every view inside the collapsed range"); | ||
| 333 | watched.release(device); | ||
| 334 | check(g_live_buffers == 0 && g_live_allocs == 0, "watched teardown frees its block"); | ||
| 335 | |||
| 336 | // 10. Spare 4 MiB block: retiring and reaping the sole view keeps the block empty and the next view reuses it. | ||
| 337 | const auto spare_owner = std::make_unique<rt::d3d12::GeometryViews>(6 * kView); | ||
| 338 | auto& spare = *spare_owner; | ||
| 339 | const std::uint64_t va_s1 = spare.resolve(device, pbase, kView, copy); | ||
| 340 | const int allocs_after_first = g_total_allocs; | ||
| 341 | check(va_s1 != 0 && spare.block_count() == 1 && spare.empty_blocks() == 0 && spare.reserved() == 4 * kView && | ||
| 342 | g_live_buffers == 1 && g_live_allocs == 1, | ||
| 343 | "first view under a 6 MiB cap allocates one standard 4 MiB block"); | ||
| 344 | check(spare.retire_range(pbase + kView - 16, pbase + kView, 1) == 1 && spare.reap(device, 1) == 1, | ||
| 345 | "retiring a tail subrange and reaping frees the view"); | ||
| 346 | check(spare.live_views() == 0 && spare.block_count() == 1 && spare.empty_blocks() == 1 && | ||
| 347 | spare.reserved() == 4 * kView && g_live_buffers == 1 && g_live_allocs == 1, | ||
| 348 | "reaping the last view keeps the empty 4 MiB block as a spare"); | ||
| 349 | const std::uint64_t va_s2 = spare.resolve(device, pbase + kView, kView, copy); | ||
| 350 | check(va_s2 != 0 && spare.block_count() == 1 && spare.empty_blocks() == 0 && g_live_buffers == 1 && | ||
| 351 | g_live_allocs == 1 && g_total_allocs == allocs_after_first, | ||
| 352 | "a new view reuses the spare 4 MiB block with no new allocation"); | ||
| 353 | |||
| 354 | // 11. Spare given up under the cap: destroy the unfitting 4 MiB spare so a larger request succeeds, and still | ||
| 355 | // refuse a request that remains over the cap after the spare is destroyed. | ||
| 356 | check(spare.retire_range(pbase + kView, pbase + 2 * kView, 2) == 1 && spare.reap(device, 2) == 1 && | ||
| 357 | spare.block_count() == 1 && spare.empty_blocks() == 1 && spare.reserved() == 4 * kView, | ||
| 358 | "reaping the reused view returns the 4 MiB block to spare"); | ||
| 359 | const std::uint64_t va_big = spare.resolve(device, pbase, 5 * kView, copy); | ||
| 360 | check(va_big != 0 && spare.block_count() == 1 && spare.empty_blocks() == 0 && spare.reserved() == 5 * kView && | ||
| 361 | g_live_buffers == 1 && g_live_allocs == 1, | ||
| 362 | "a 5 MiB request under a 6 MiB cap destroys the 4 MiB spare and succeeds"); | ||
| 363 | spare.release(device); | ||
| 364 | check(g_live_buffers == 0 && g_live_allocs == 0, "spare teardown frees its block"); | ||
| 365 | |||
| 366 | const auto tight_owner = std::make_unique<rt::d3d12::GeometryViews>(9 * kView); | ||
| 367 | |||
| 368 | auto& tight = *tight_owner; | ||
| 369 | const std::uint64_t va_t1 = tight.resolve(device, pbase, 3 * kView, copy); | ||
| 370 | const std::uint64_t va_t2 = tight.resolve(device, pbase + 3 * kView, 2 * kView, copy); | ||
| 371 | check(va_t1 != 0 && va_t2 != 0 && tight.block_count() == 2 && tight.reserved() == 8 * kView, | ||
| 372 | "two views that do not share one 4 MiB block reserve 8 MiB"); | ||
| 373 | check(tight.retire_range(pbase + 3 * kView, pbase + 5 * kView, 1) == 1 && tight.reap(device, 1) == 1 && | ||
| 374 | tight.block_count() == 2 && tight.empty_blocks() == 1 && tight.reserved() == 8 * kView, | ||
| 375 | "reaping the second block leaves one 4 MiB spare alongside one live 4 MiB block"); | ||
| 376 | const std::uint64_t va_over = tight.resolve(device, pbase, 6 * kView, copy); | ||
| 377 | check(va_over == 0 && tight.cap_refusals() == 1 && tight.block_count() == 1 && tight.empty_blocks() == 0 && | ||
| 378 | tight.reserved() == 4 * kView && g_live_buffers == 1 && g_live_allocs == 1, | ||
| 379 | "a 6 MiB request drops the 4 MiB spare and is refused because only 5 MiB of cap remain"); | ||
| 380 | const std::uint64_t va_fit = tight.resolve(device, pbase, 5 * kView, copy); | ||
| 381 | check(va_fit != 0 && tight.block_count() == 2 && tight.reserved() == 9 * kView && g_live_buffers == 2 && | ||
| 382 | g_live_allocs == 2, | ||
| 383 | "a 5 MiB request succeeds in the cap freed by dropping the spare"); | ||
| 384 | tight.release(device); | ||
| 385 | check(g_live_buffers == 0 && g_live_allocs == 0, "tight cap teardown frees all blocks"); | ||
| 386 | |||
| 387 | VirtualFree(pool_title, 0, MEM_RELEASE); | ||
| 388 | |||
| 389 | // 12. Two GeometryViews instances over the same title extent both receive one note_released; a destroyed instance | ||
| 390 | // unsubscribes so later releases do not touch it and surviving instances still receive them. | ||
| 391 | auto* shared_title = | ||
| 392 | static_cast<std::uint8_t*>(VirtualAlloc(nullptr, 2 * kView, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE)); | ||
| 393 | if (!shared_title) return 1; | ||
| 394 | for (std::size_t i = 0; i < 2 * kView; ++i) shared_title[i] = std::uint8_t((i * 29) ^ 0xa5); | ||
| 395 | const auto shbase = reinterpret_cast<std::uintptr_t>(shared_title); | ||
| 396 | |||
| 397 | { | ||
| 398 | const auto dev_a_owner = std::make_unique<rt::d3d12::GeometryViews>(2 * kView); | ||
| 399 | auto& dev_a = *dev_a_owner; | ||
| 400 | const auto dev_b_owner = std::make_unique<rt::d3d12::GeometryViews>(2 * kView); | ||
| 401 | auto& dev_b = *dev_b_owner; | ||
| 402 | const std::uint64_t va_a1 = dev_a.resolve(device, shbase, kView, copy); | ||
| 403 | const std::uint64_t va_b1 = dev_b.resolve(device, shbase, kView, copy); | ||
| 404 | check(va_a1 != 0 && va_b1 != 0 && dev_a.live_views() == 1 && dev_b.live_views() == 1 && | ||
| 405 | dev_a.bytes() == kView && dev_b.bytes() == kView, | ||
| 406 | "two GeometryViews instances resolve the same title range"); | ||
| 407 | |||
| 408 | leonida::geometry_writes::note_released(shbase, shbase + kView); | ||
| 409 | check(dev_a.has_released() && dev_b.has_released(), | ||
| 410 | "one note_released queues the range in both instances"); | ||
| 411 | |||
| 412 | dev_a.drain_released(device, 1, 1); | ||
| 413 | check(!dev_a.has_released() && dev_a.live_views() == 0 && dev_a.retired_count() == 0 && dev_a.bytes() == 0 && | ||
| 414 | dev_b.has_released() && dev_b.live_views() == 1 && dev_b.bytes() == kView, | ||
| 415 | "draining the first instance retires and reaps its view without emptying the second instance's queue"); | ||
| 416 | |||
| 417 | dev_b.drain_released(device, 1, 1); | ||
| 418 | check(!dev_b.has_released() && dev_b.live_views() == 0 && dev_b.retired_count() == 0 && dev_b.bytes() == 0, | ||
| 419 | "draining the second instance also retires and reaps its view"); | ||
| 420 | |||
| 421 | const std::uint64_t va_keep = dev_a.resolve(device, shbase + kView, kView, copy); | ||
| 422 | check(va_keep != 0 && dev_a.live_views() == 1 && dev_a.bytes() == kView, | ||
| 423 | "surviving instance resolves a second range"); | ||
| 424 | { | ||
| 425 | const auto ephemeral_owner = std::make_unique<rt::d3d12::GeometryViews>(2 * kView); | ||
| 426 | auto& ephemeral = *ephemeral_owner; | ||
| 427 | const std::uint64_t va_eph = ephemeral.resolve(device, shbase + kView, kView, copy); | ||
| 428 | check(va_eph != 0 && ephemeral.live_views() == 1, "scoped instance resolves the same range"); | ||
| 429 | ephemeral.release(device); | ||
| 430 | } | ||
| 431 | leonida::geometry_writes::note_released(shbase + kView, shbase + 2 * kView); | ||
| 432 | check(dev_a.has_released(), "note_released after another instance is destroyed reaches the surviving instance"); | ||
| 433 | dev_a.drain_released(device, 2, 2); | ||
| 434 | check(!dev_a.has_released() && dev_a.live_views() == 0 && dev_a.retired_count() == 0 && dev_a.bytes() == 0, | ||
| 435 | "surviving instance retires and reaps after the destroyed instance unsubscribed"); | ||
| 436 | |||
| 437 | dev_a.release(device); | ||
| 438 | dev_b.release(device); | ||
| 439 | } | ||
| 440 | check(g_live_buffers == 0 && g_live_allocs == 0, "multi-instance teardown frees all blocks"); | ||
| 441 | VirtualFree(shared_title, 0, MEM_RELEASE); | ||
| 442 | |||
| 443 | // 13. DeviceState's order for one submission: execute_begin, drain_geometry_releases (kAfterExecutes) and the | ||
| 444 | // console heaps in before_execute_, the replay records its submission, execute_end. Another queue's replay began | ||
| 445 | // before the release (its heap slots may already name the view); one more begins after the retirement. | ||
| 446 | auto* fly_title = static_cast<std::uint8_t*>(VirtualAlloc(nullptr, kView, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE)); | ||
| 447 | if (!fly_title) return 1; | ||
| 448 | for (std::size_t i = 0; i < kView; ++i) fly_title[i] = std::uint8_t(i * 13 + 1); | ||
| 449 | const auto flybase = reinterpret_cast<std::uintptr_t>(fly_title); | ||
| 450 | { | ||
| 451 | const auto fly_owner = std::make_unique<rt::d3d12::GeometryViews>(2 * kView); | ||
| 452 | auto& fly = *fly_owner; | ||
| 453 | const auto named_blocks = [&] { | ||
| 454 | std::size_t n = 0; | ||
| 455 | fly.pool().for_each_block([&](VkDeviceAddress address, VkDeviceSize) { n += address != 0; }); | ||
| 456 | return n; | ||
| 457 | }; | ||
| 458 | const std::uint64_t va_f = fly.resolve(device, flybase, kView, copy); | ||
| 459 | check(va_f != 0 && fly.live_views() == 1 && named_blocks() == 1, "a view in its own block, published"); | ||
| 460 | const std::uint64_t layout = fly.pool().layout_generation(); | ||
| 461 | const int gone = g_gone_blocks; | ||
| 462 | |||
| 463 | std::uint64_t submitted = 7; // completed == submitted below: the GPU is idle | ||
| 464 | const std::uint64_t other = fly.execute_begin(); | ||
| 465 | const std::uint64_t own = fly.execute_begin(); | ||
| 466 | leonida::TitleVaAliases aliases; | ||
| 467 | aliases.record_removals(true); | ||
| 468 | std::uint64_t cursor = 0; | ||
| 469 | { | ||
| 470 | leonida::DescriptorRanges skip; | ||
| 471 | aliases.changed_ranges(cursor, skip); | ||
| 472 | } | ||
| 473 | const std::uint64_t invalidations = aliases.invalidations(); | ||
| 474 | leonida::geometry_writes::note_released(flybase, flybase + kView); | ||
| 475 | fly.drain_released(device, rt::d3d12::GeometryViews::kAfterExecutes, submitted, | ||
| 476 | [&](std::uintptr_t source, std::size_t size, VkDeviceSize va) { | ||
| 477 | aliases.forget_copy(source, size, va); | ||
| 478 | }); | ||
| 479 | check(fly.live_views() == 0 && fly.find(flybase, kView) == 0 && fly.retired_count() == 1 && | ||
| 480 | fly.bytes() == kView && g_gone_blocks == gone, | ||
| 481 | "a view retired in before_execute_ is not reaped there, though the GPU is idle"); | ||
| 482 | |||
| 483 | leonida::DescriptorRanges changed; | ||
| 484 | const bool read = aliases.changed_ranges(cursor, changed); | ||
| 485 | changed.merge(); | ||
| 486 | std::vector<std::pair<std::uint64_t, std::uint64_t>> removed; | ||
| 487 | aliases.take_removed(removed); | ||
| 488 | check(read && aliases.invalidations() != invalidations && changed.overlaps(flybase, kView) && | ||
| 489 | removed.size() == 1 && removed[0].first == va_f && removed[0].second == kView, | ||
| 490 | "the retirement invalidates the heap slots and key answers made through the view without an alias"); | ||
| 491 | |||
| 492 | submitted = 8; // this replay's submission | ||
| 493 | fly.execute_end(own, [&] { return submitted; }); | ||
| 494 | check(fly.reap(device, 8) == 0 && fly.retired_count() == 1 && fly.bytes() == kView, | ||
| 495 | "not freed while a replay begun before the retirement is still in progress"); | ||
| 496 | |||
| 497 | const std::uint64_t later = fly.execute_begin(); | ||
| 498 | submitted = 9; // the other queue's submission | ||
| 499 | fly.execute_end(other, [&] { return submitted; }); | ||
| 500 | check(fly.reap(device, 8) == 0 && fly.retired_count() == 1, | ||
| 501 | "once those replays recorded theirs, the view waits for the last of their tickets"); | ||
| 502 | check(fly.reap(device, 9) == 1 && fly.retired_count() == 0 && fly.bytes() == 0, | ||
| 503 | "freed once that ticket completed; a replay begun after the retirement does not hold it"); | ||
| 504 | fly.execute_end(later, [&] { return submitted; }); | ||
| 505 | |||
| 506 | check(named_blocks() == 0 && fly.pool().layout_generation() != layout && g_gone_blocks == gone + 1 && | ||
| 507 | g_live_buffers == 0, | ||
| 508 | "the destroyed block leaves the lock-free block list and the device is told"); | ||
| 509 | fly.release(device); | ||
| 510 | } | ||
| 511 | VirtualFree(fly_title, 0, MEM_RELEASE); | ||
| 512 | |||
| 164 | std::printf("%d checks, %d failures\n", g_checks, g_failures); | 513 | std::printf("%d checks, %d failures\n", g_checks, g_failures); |
| 165 | return g_failures ? 1 : 0; | 514 | return g_failures ? 1 : 0; |
| 166 | } | 515 | } |