diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-26 17:42:43 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-26 17:42:43 -0500 |
| commit | 83d9eee2bbf58d729b2e0feb31bb5ad0aa71473b (patch) | |
| tree | f7887597cccff5d68d1ce024cd6eee02ef6dc391 | |
| parent | 134a1d17ff6ab53a5af77ed7e664282a4489a25f (diff) | |
| parent | 9807c5ec2464f4960039b76d6a8f356743e6798a (diff) | |
Merge branch 'descriptor-refill' into slot-records-merged
The T# records, the room lists and the texture and room changes of descriptor-refill now live in the exact-range
index of slot-records-index. src/console_heap_slot_records.h: a third index, by T# address (the exact range
[address, address + 1)), of the same groups with a settled and a waiting list; a texture gone visits exactly the
T#s recorded for its address, a texture added the waiting ones. The records waiting for room (V# halves refused a
geometry copy, T#s refused for a full bindless array) are kept in a list per kind with each record's place, so a
record leaves it in O(1) and room made visits only the records waiting for it. Interface as descriptor-refill
has it (states, set_texture, base, textures, textures_waiting); device code, README rows and both sides' docs kept.
32 bytes a half.
Tests: console_heap_slot_records_test, both sides' cases (60,010-slot release; 300,000 slots in 74 blocks, a
40-byte release looks at 3; 400 random rounds against a full re-resolution; a texture registered looks at the 10 of
60,015 waiting on its address; room changes touch only the records waiting for room); a copy that skips settled
lists on a removal fails. Linux ctest 115/115.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| -rw-r--r-- | docs/d3d12_vulkan.md | 21 | ||||
| -rw-r--r-- | runtime/CMakeLists.txt | 10 | ||||
| -rw-r--r-- | runtime/README.md | 4 | ||||
| -rw-r--r-- | runtime/shims/d3d12_vulkan_device.cpp | 263 | ||||
| -rw-r--r-- | runtime/src/console_heap_slot_records.h | 260 | ||||
| -rw-r--r-- | runtime/test/console_heap_slot_records_test.cpp | 107 | ||||
| -rw-r--r-- | runtime/test/d3d12_tsharp_refill_test.cpp | 341 | ||||
| -rw-r--r-- | runtime/test/d3d12_unbound_tsharp_rules_test.cpp | 51 |
8 files changed, 890 insertions, 167 deletions
diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 685c9b393..9121ec0ad 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md | |||
| @@ -2112,6 +2112,27 @@ The game can use D3D12 descriptors in three ways, and the layer must handle all | |||
| 2112 | of a buffer 10 of 60,010 slots name looks at those 10, that a 40-byte release among 300,000 slots packed into 74 | 2112 | of a buffer 10 of 60,010 slots name looks at those 10, that a 40-byte release among 300,000 slots packed into 74 |
| 2113 | blocks of 64 KiB looks at the 3 it overlaps, and compares the copy with a full re-resolution through random | 2113 | blocks of 64 KiB looks at the 3 it overlaps, and compares the copy with a full re-resolution through random |
| 2114 | writes, releases, additions and alias changes. | 2114 | writes, releases, additions and alias changes. |
| 2115 | A title-written T# has a record too, under the exact base address it names (`bind_title_image` looks textures up | ||
| 2116 | by that address, or by the other view of texture-pool memory): bound, or waiting for a texture there (no texture, | ||
| 2117 | or one whose type, samples, mips, layers, image or views do not fit the T#), or waiting for room in its bindless | ||
| 2118 | array. A texture registered, replaced or published at an address (`note_image_address`, a title texture's | ||
| 2119 | placement, a streamed texture's image), or gone from it (`forget_image`, a placement removed), goes on the same | ||
| 2120 | list, and the next submission fixes again exactly the T#s naming that address; freed bindless entries fix the | ||
| 2121 | T#s waiting for room. Nothing looks at a waiting T# otherwise: before, a round robin tried the waiting slots again | ||
| 2122 | for 500 µs a submission (`LEONIDA_CONSOLE_RETRY_US`), which in world runs took tens of submissions to come round | ||
| 2123 | and counted every look as a T# "naming no texture" again. A texture that goes takes its T#s out of the descriptor | ||
| 2124 | hash table at once (`forget_image_views_locked`), so a shader reading the slot gets the fallback; the slot then | ||
| 2125 | waits for the next texture at the address, which a texture streamed out and back in needs. A V# half whose | ||
| 2126 | geometry copy was refused for lack of room waits for room the same way: the slots are fixed again when geometry | ||
| 2127 | copies are freed (`GeometryViews::free_generation`), not only when something is added over their base. | ||
| 2128 | A T# with no texture at its address is counted by what the address is (`unbound T#s by reason` in the heap's log | ||
| 2129 | line): title memory, a texture of the layer's own that is gone, an address whose low 32 bits are one of the | ||
| 2130 | layer's texture addresses with other high bits (the texture there with the T#'s shape, with another, or gone), | ||
| 2131 | or none of these. The heap's `slot records` line gives the T#s recorded, those waiting, the T# fixes that left | ||
| 2132 | one waiting and the refills since the last line. `d3d12_tsharp_refill_test` samples, through the last of 60,001 | ||
| 2133 | waiting slots, a texture made after the T# was written, at the first submission after it appears. T#s are a third | ||
| 2134 | index of the same kind, keyed by their address: a texture change visits exactly the T#s recorded for that address, | ||
| 2135 | and the records waiting for room are kept in a list per kind, which a change saying room was made visits whole. | ||
| 2115 | Console resource heap copies and shader-visible CBV/SRV/UAV heaps with at least 65,536 slots request | 2136 | Console resource heap copies and shader-visible CBV/SRV/UAV heaps with at least 65,536 slots request |
| 2116 | 64 MiB of zeroed padding after their last descriptor. `LEONIDA_HEAP_GUARD_MB` sets the padding in MiB | 2137 | 64 MiB of zeroed padding after their last descriptor. `LEONIDA_HEAP_GUARD_MB` sets the padding in MiB |
| 2117 | (0 disables it). When arena space is tight, allocation can fall back to the heap alone. Only indices | 2138 | (0 disables it). When arena space is tight, allocation can fall back to the heap alone. Only indices |
diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index f8edc3508..08fbbc7e3 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt | |||
| @@ -4195,6 +4195,16 @@ if(TARGET Vulkan::Headers) | |||
| 4195 | target_compile_options(d3d12_set0_compute_test PRIVATE /W4 /permissive- /utf-8) | 4195 | target_compile_options(d3d12_set0_compute_test PRIVATE /W4 /permissive- /utf-8) |
| 4196 | add_test(NAME d3d12_set0_compute COMMAND d3d12_set0_compute_test) | 4196 | add_test(NAME d3d12_set0_compute COMMAND d3d12_set0_compute_test) |
| 4197 | set_tests_properties(d3d12_set0_compute PROPERTIES SKIP_RETURN_CODE 77) | 4197 | set_tests_properties(d3d12_set0_compute PROPERTIES SKIP_RETURN_CODE 77) |
| 4198 | # A T# written into a console heap before its texture exists samples the texture at the first submission after | ||
| 4199 | # it appears (the heap's slot records), and leaves the hash table when it goes. | ||
| 4200 | add_executable(d3d12_tsharp_refill_test test/d3d12_tsharp_refill_test.cpp shims/d3d12_xs_device.cpp | ||
| 4201 | shims/d3d12_xs_pso_stream.cpp ${LEONIDA_TEST_CS_SET0_SAMPLE}) | ||
| 4202 | target_link_libraries(d3d12_tsharp_refill_test PRIVATE gta6rt_d3d12_com) | ||
| 4203 | target_compile_definitions(d3d12_tsharp_refill_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX | ||
| 4204 | LEONIDA_TEST_CS_SET0_SAMPLE_SPV="${LEONIDA_TEST_CS_SET0_SAMPLE}") | ||
| 4205 | target_compile_options(d3d12_tsharp_refill_test PRIVATE /W4 /permissive- /utf-8) | ||
| 4206 | add_test(NAME d3d12_tsharp_refill COMMAND d3d12_tsharp_refill_test) | ||
| 4207 | set_tests_properties(d3d12_tsharp_refill PROPERTIES SKIP_RETURN_CODE 77) | ||
| 4198 | set(depth_uav_shader ${CMAKE_CURRENT_SOURCE_DIR}/test/glsl/cs_depth_stencil_uav.comp) | 4208 | set(depth_uav_shader ${CMAKE_CURRENT_SOURCE_DIR}/test/glsl/cs_depth_stencil_uav.comp) |
| 4199 | foreach(variant IN ITEMS DEPTH_STENCIL_2D DEPTH_STENCIL_ARRAY DEPTH16_2D DEPTH16_ARRAY) | 4209 | foreach(variant IN ITEMS DEPTH_STENCIL_2D DEPTH_STENCIL_ARRAY DEPTH16_2D DEPTH16_ARRAY) |
| 4200 | set(spv ${CMAKE_CURRENT_BINARY_DIR}/generated/cs_depth_uav_${variant}.spv) | 4210 | set(spv ${CMAKE_CURRENT_BINARY_DIR}/generated/cs_depth_uav_${variant}.spv) |
diff --git a/runtime/README.md b/runtime/README.md index 9aabdf12c..3b8c53537 100644 --- a/runtime/README.md +++ b/runtime/README.md | |||
| @@ -378,8 +378,8 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w | |||
| 378 | | `LEONIDA_WINDOW` | unset | `WxH` resizes the title's window when the presenter attaches to it. | | 378 | | `LEONIDA_WINDOW` | unset | `WxH` resizes the title's window when the presenter attaches to it. | |
| 379 | | `LEONIDA_PRESENT_DUMP` | unset | *Diagnostic.* `DIR` or `DIR,N` writes every Nth presented frame (60 by default) as a PNG into `DIR` (`src/present_dump.h`). The files are pictures of the title: keep them out of commits, issues and pull requests. | | 379 | | `LEONIDA_PRESENT_DUMP` | unset | *Diagnostic.* `DIR` or `DIR,N` writes every Nth presented frame (60 by default) as a PNG into `DIR` (`src/present_dump.h`). The files are pictures of the title: keep them out of commits, issues and pull requests. | |
| 380 | | `LEONIDA_LOCAL_SPEECH` | unset | `1` forwards the title's speech-synthesis calls to the host's installed `xgameruntime.dll` where there is one. Unset, and wherever that library is missing, the silent voice answers. No profile sets it. | | 380 | | `LEONIDA_LOCAL_SPEECH` | unset | `1` forwards the title's speech-synthesis calls to the host's installed `xgameruntime.dll` where there is one. Unset, and wherever that library is missing, the silent voice answers. No profile sets it. | |
| 381 | | `LEONIDA_CONSOLE_FIX_US` | `0` | Microseconds one submission may spend fixing the changed slots of a console descriptor heap's copy; `0` sets no limit. Past it, each further changed slot is written as a null descriptor and queued for `LEONIDA_CONSOLE_RETRY_US`, so a shader reads it as empty until its turn comes: a limit changes results, and can hang the GPU. | | 381 | | `LEONIDA_CONSOLE_FIX_US` | `0` | Microseconds one submission may spend fixing the changed slots of a console descriptor heap's copy; `0` sets no limit. Past it, each further changed slot is written as a null descriptor and fixed first at the next submission, under the same limit, so a shader reads it as empty until then: a limit changes results, and can hang the GPU. | |
| 382 | | `LEONIDA_CONSOLE_RETRY_US` | `500` | Microseconds one submission may spend retrying, round robin, the console heap slots not settled yet (a T# whose texture does not exist yet, or one deferred by `LEONIDA_CONSOLE_FIX_US`). `0` retries all of them at every submission. | | 382 | | `LEONIDA_CONSOLE_RETRY_US` | `500` | `LEONIDA_CONSOLE_HEAP_GPU` only: microseconds one submission may spend retrying, round robin, the T#s of a heap the GPU pass translates whose texture does not exist yet. `0` retries all of them at every submission. The CPU copy (the default) has no retries: a T# waiting for its texture is fixed when a texture appears at its address (docs/d3d12_vulkan.md section 3.4). | |
| 383 | | `LEONIDA_SHADOW_ALIAS_MS` | `0` | The shortest time, in milliseconds, between two copies of the title-memory pages that XMem maps at two addresses (no write guard can watch them) into the slot-133 shadows and title windows. `0` copies them at every submission; a larger value lets a GPU read of them be up to that many milliseconds behind the title's writes. | | 383 | | `LEONIDA_SHADOW_ALIAS_MS` | `0` | The shortest time, in milliseconds, between two copies of the title-memory pages that XMem maps at two addresses (no write guard can watch them) into the slot-133 shadows and title windows. `0` copies them at every submission; a larger value lets a GPU read of them be up to that many milliseconds behind the title's writes. | |
| 384 | | `LEONIDA_SHADOW_KEEP` | on | The slot-133 shadows and title windows copy a page only in the 256-byte pieces where the title's bytes changed since the last copy, so what a shader stored in the copy stays, also on pages copied without a sign that the title wrote them (aliased, unguarded, pinned). A title writing the same bytes again to reset such a value is not seen. `0` copies those pages whole, as before, overwriting the GPU's writes. | | 384 | | `LEONIDA_SHADOW_KEEP` | on | The slot-133 shadows and title windows copy a page only in the 256-byte pieces where the title's bytes changed since the last copy, so what a shader stored in the copy stays, also on pages copied without a sign that the title wrote them (aliased, unguarded, pinned). A title writing the same bytes again to reset such a value is not seen. `0` copies those pages whole, as before, overwriting the GPU's writes. | |
| 385 | | `LEONIDA_REFRESH_PER_FRAME` | `0` | Trades correctness for speed. `1`: the slot-133 shadows, title windows and geometry copies are brought up to date only by the first submission after each present, not by every submission; `2`: the console descriptor heaps too. A GPU read of them can then be a frame behind the title's writes (wrong pixels). `0` refreshes all of them at every submission. | | 385 | | `LEONIDA_REFRESH_PER_FRAME` | `0` | Trades correctness for speed. `1`: the slot-133 shadows, title windows and geometry copies are brought up to date only by the first submission after each present, not by every submission; `2`: the console descriptor heaps too. A GPU read of them can then be a frame behind the title's writes (wrong pixels). `0` refreshes all of them at every submission. | |
diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 18df31e1f..5b18b5b97 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp | |||
| @@ -2349,6 +2349,22 @@ class Resource; | |||
| 2349 | struct DeviceState; | 2349 | struct DeviceState; |
| 2350 | // `why`, when given, says what refused a T#: the first refusals are logged with their words (scan_console_slots). | 2350 | // `why`, when given, says what refused a T#: the first refusals are logged with their words (scan_console_slots). |
| 2351 | TitleImage bind_title_image(DeviceState& device, const leonida::gfx10::ImageWords& words, const char** why = nullptr); | 2351 | TitleImage bind_title_image(DeviceState& device, const leonida::gfx10::ImageWords& words, const char** why = nullptr); |
| 2352 | // Why a T# found no room (bind_image_locked): set 0's array of its dimension, or that array's storage range, was full. | ||
| 2353 | // A console heap's slot record keeps such a T# waiting for room (DeviceState::fix_console_slot). | ||
| 2354 | constexpr const char* kTsharpArrayFull = "the bindless array of the T#'s dimension is full"; | ||
| 2355 | constexpr const char* kTsharpStorageFull = "the bindless storage arrays of the T#'s dimension are full"; | ||
| 2356 | // Why a T# found no texture (TitleImage::unknown), by what its address is (bind_title_image): the title's memory (a | ||
| 2357 | // texture the title placed there without any call the layer sees); an address of the layer's own textures | ||
| 2358 | // (Resource::image_address: 1 << 32 and up, 256 apart) whose texture is gone; an address whose low 32 bits are one of | ||
| 2359 | // those (the title rebuilt the T# with other high bits) and whose texture is there with the T#'s shape, there with | ||
| 2360 | // another shape, or gone; or none of these (no memory of the title's or texture of ours: data that decodes as a T#, | ||
| 2361 | // or an address the layer does not know). | ||
| 2362 | constexpr const char* kTsharpNoTextureTitleMemory = "no texture at the T# address: title memory"; | ||
| 2363 | constexpr const char* kTsharpNoTextureGone = "no texture at the T# address: a texture of ours that is gone"; | ||
| 2364 | constexpr const char* kTsharpNoTextureLow32Same = "no texture at the T# address: its low 32 bits name a texture of ours of its shape"; | ||
| 2365 | constexpr const char* kTsharpNoTextureLow32Other = "no texture at the T# address: its low 32 bits name a texture of ours of another shape"; | ||
| 2366 | constexpr const char* kTsharpNoTextureLow32Gone = "no texture at the T# address: its low 32 bits name a texture of ours that is gone"; | ||
| 2367 | constexpr const char* kTsharpNoTextureNothing = "no texture at the T# address: no title memory and no texture of ours"; | ||
| 2352 | struct TitleTexture; // a texture the title keeps in its own memory (TitleTexture, after Resource) | 2368 | struct TitleTexture; // a texture the title keeps in its own memory (TitleTexture, after Resource) |
| 2353 | 2369 | ||
| 2354 | // The first refused T#s of each reason, with where they sit in the heap and their words, and counts of the | 2370 | // The first refused T#s of each reason, with where they sit in the heap and their words, and counts of the |
| @@ -3003,7 +3019,9 @@ struct DeviceState { | |||
| 3003 | if (!leonida::texture_memory_readable(reinterpret_cast<std::uintptr_t>(from), size)) return Result::unreadable; | 3019 | if (!leonida::texture_memory_readable(reinterpret_cast<std::uintptr_t>(from), size)) return Result::unreadable; |
| 3004 | return copy_guarded(to, from, size, false) ? Result::copied : Result::interrupted; | 3020 | return copy_guarded(to, from, size, false) ? Result::copied : Result::interrupted; |
| 3005 | } | 3021 | } |
| 3006 | std::uint64_t geometry_va(std::uint64_t guest, std::uint64_t span, bool read_only) { | 3022 | // `refused` (when given) is set when a copy was asked for and refused (no room under the cap, or no block): it may |
| 3023 | // be made once geometry copies are freed (GeometryViews::free_generation). | ||
| 3024 | std::uint64_t geometry_va(std::uint64_t guest, std::uint64_t span, bool read_only, bool* refused = nullptr) { | ||
| 3007 | if (!geometry.cap() || !span || !guest || !watch_shadow_writes()) return 0; | 3025 | if (!geometry.cap() || !span || !guest || !watch_shadow_writes()) return 0; |
| 3008 | const auto cpu = leonida::title_gpu_views::to_cpu(guest, span); | 3026 | const auto cpu = leonida::title_gpu_views::to_cpu(guest, span); |
| 3009 | auto& aliases = leonida::title_va_aliases(); | 3027 | auto& aliases = leonida::title_va_aliases(); |
| @@ -3019,7 +3037,9 @@ struct DeviceState { | |||
| 3019 | return geometry.find(cpu, span); | 3037 | return geometry.find(cpu, span); |
| 3020 | } | 3038 | } |
| 3021 | } | 3039 | } |
| 3022 | return geometry.resolve(*this, cpu, span, copy_geometry); | 3040 | const std::uint64_t va = geometry.resolve(*this, cpu, span, copy_geometry); |
| 3041 | if (!va && refused) *refused = true; | ||
| 3042 | return va; | ||
| 3023 | } | 3043 | } |
| 3024 | 3044 | ||
| 3025 | // LEONIDA_STREAM_BUFFER_WINDOWS=0: a window may not come from a block outside the arena. | 3045 | // LEONIDA_STREAM_BUFFER_WINDOWS=0: a window may not come from a block outside the arena. |
| @@ -3914,8 +3934,10 @@ struct DeviceState { | |||
| 3914 | // `refused` (when given): for an answer of 0, the copy an alias range named that was not backed (0: none), so the | 3934 | // `refused` (when given): for an answer of 0, the copy an alias range named that was not backed (0: none), so the |
| 3915 | // console heap's slot record is found again when a backing is added there. | 3935 | // console heap's slot record is found again when a backing is added there. |
| 3916 | uint64_t vsharp_address(uint64_t base, uint64_t bytes, leonida::TitleVaAliases::Kind kind = leonida::TitleVaAliases::created_view, | 3936 | uint64_t vsharp_address(uint64_t base, uint64_t bytes, leonida::TitleVaAliases::Kind kind = leonida::TitleVaAliases::created_view, |
| 3917 | bool make_window = true, bool try_geometry = false, uint64_t* refused = nullptr) { | 3937 | bool make_window = true, bool try_geometry = false, uint64_t* refused = nullptr, |
| 3938 | bool* refused_room = nullptr) { | ||
| 3918 | if (refused) *refused = 0; | 3939 | if (refused) *refused = 0; |
| 3940 | if (refused_room) *refused_room = false; | ||
| 3919 | if (!base || !bytes) return 0; | 3941 | if (!base || !bytes) return 0; |
| 3920 | auto& aliases = leonida::title_va_aliases(); | 3942 | auto& aliases = leonida::title_va_aliases(); |
| 3921 | constexpr uint64_t mask = (1ull << 48) - 1; | 3943 | constexpr uint64_t mask = (1ull << 48) - 1; |
| @@ -3956,7 +3978,7 @@ struct DeviceState { | |||
| 3956 | // Read the other way: bit 47 as a sign, for a below-arena buffer's V# the layer wrote itself. | 3978 | // Read the other way: bit 47 as a sign, for a below-arena buffer's V# the layer wrote itself. |
| 3957 | if (offset == base && base <= mask && (base & (1ull << 47)) && backed(base | ~mask)) return base | ~mask; | 3979 | if (offset == base && base <= mask && (base & (1ull << 47)) && backed(base | ~mask)) return base | ~mask; |
| 3958 | if (try_geometry) { | 3980 | if (try_geometry) { |
| 3959 | to = geometry_va(base, bytes, false); | 3981 | to = geometry_va(base, bytes, false, refused_room); |
| 3960 | if (to && encodable(to)) return path(3), to; | 3982 | if (to && encodable(to)) return path(3), to; |
| 3961 | } | 3983 | } |
| 3962 | uint64_t lo{}, hi{}; | 3984 | uint64_t lo{}, hi{}; |
| @@ -4027,11 +4049,14 @@ struct DeviceState { | |||
| 4027 | const bool copy_geometry = leonida::gfx10::decode(words, desc) && | 4049 | const bool copy_geometry = leonida::gfx10::decode(words, desc) && |
| 4028 | (path != VsharpPath::scratch || span <= kTitleWindowMax || (desc.format && leonida::gfx10::encode(desc) == words)); | 4050 | (path != VsharpPath::scratch || span <= kTitleWindowMax || (desc.format && leonida::gfx10::encode(desc) == words)); |
| 4029 | uint64_t refused = 0; | 4051 | uint64_t refused = 0; |
| 4052 | bool refused_room = false; | ||
| 4030 | const auto to = vsharp_address(base, span, leonida::TitleVaAliases::table_view, | 4053 | const auto to = vsharp_address(base, span, leonida::TitleVaAliases::table_view, |
| 4031 | path != VsharpPath::scratch, copy_geometry, &refused); | 4054 | path != VsharpPath::scratch, copy_geometry, &refused, &refused_room); |
| 4032 | if (record) { | 4055 | if (record) { |
| 4033 | // The copy's device address (mod 2^64: a buffer below the arena has a negative offset). | 4056 | // The copy's device address (mod 2^64: a buffer below the arena has a negative offset). A geometry copy |
| 4034 | record->state = to ? leonida::ConsoleHeapSlotRecords::resolved : leonida::ConsoleHeapSlotRecords::waiting; | 4057 | // refused for room is asked for again when copies are freed (SlotChange::geometry_room). |
| 4058 | using Records = leonida::ConsoleHeapSlotRecords; | ||
| 4059 | record->state = to ? Records::resolved : refused_room ? Records::waiting_room : Records::waiting; | ||
| 4035 | record->base = base; | 4060 | record->base = base; |
| 4036 | record->span = span; | 4061 | record->span = span; |
| 4037 | record->target = to ? arena.address + to : refused ? arena.address + refused : 0; | 4062 | record->target = to ? arena.address + to : refused ? arena.address + refused : 0; |
| @@ -4295,7 +4320,8 @@ struct DeviceState { | |||
| 4295 | // the console VA of a GPU copy (rebase_title_vsharp), and an unregistered T# is bound (bind_title_image). | 4320 | // the console VA of a GPU copy (rebase_title_vsharp), and an unregistered T# is bound (bind_title_image). |
| 4296 | // The tracker copies into a host mirror of the title's bytes, and only the slots whose bytes differ from what | 4321 | // The tracker copies into a host mirror of the title's bytes, and only the slots whose bytes differ from what |
| 4297 | // their arena slot was made from (a second host copy) are fixed and written there again; a T# that found no | 4322 | // their arena slot was made from (a second host copy) are fixed and written there again; a T# that found no |
| 4298 | // texture and a V# into title memory with no copy yet are tried at every submission until they do. Pages xmem | 4323 | // texture and a V# that found no backing are recorded as waiting (ConsoleHeap::records) and fixed again when a |
| 4324 | // texture, backing, alias or room comes for them (apply_slot_changes), never by looking again. Pages xmem | ||
| 4299 | // maps at two addresses are copied at every submission, and fixing all their slots each time, reading the | 4325 | // maps at two addresses are copied at every submission, and fixing all their slots each time, reading the |
| 4300 | // arena (uncached device memory) to do it, cost more than a frame (295 ms a submission for the 4 MiB of the | 4326 | // arena (uncached device memory) to do it, cost more than a frame (295 ms a submission for the 4 MiB of the |
| 4301 | // retail heap in use, RTX 4070 Ti). | 4327 | // retail heap in use, RTX 4070 Ti). |
| @@ -4328,12 +4354,18 @@ struct DeviceState { | |||
| 4328 | ShadowPagesPtr pages; | 4354 | ShadowPagesPtr pages; |
| 4329 | std::vector<std::uint8_t> mirror; // the title's bytes as the tracker last copied them | 4355 | std::vector<std::uint8_t> mirror; // the title's bytes as the tracker last copied them |
| 4330 | std::vector<std::uint8_t> made; // the title's bytes each arena slot was last made from | 4356 | std::vector<std::uint8_t> made; // the title's bytes each arena slot was last made from |
| 4331 | std::vector<std::uint8_t> retry; // per slot: not settled yet (fix_console_slot), in `retries` | 4357 | // Per slot: in `retries`. On the CPU path a slot LEONIDA_CONSOLE_FIX_US deferred (fix_deferred_slots); in the |
| 4332 | // A resource heap's V# halves: what each resolved to (fix_console_slot), so a change of the address space | 4358 | // GPU mode (refresh_console_heap_gpu) also a T# whose texture is still to come (retry_console_tsharps). |
| 4333 | // re-fixes the slots it touches (apply_slot_changes), and a V# whose buffer comes later is fixed when it does. | 4359 | std::vector<std::uint8_t> retry; |
| 4360 | // A resource heap's V# halves and T#s: what each resolved to or waits for (fix_console_slot), so a change of | ||
| 4361 | // the address space or of the textures re-fixes the slots it touches (apply_slot_changes), and a V# whose | ||
| 4362 | // buffer or a T# whose texture comes later is fixed when it does. | ||
| 4334 | leonida::ConsoleHeapSlotRecords records; | 4363 | leonida::ConsoleHeapSlotRecords records; |
| 4335 | std::vector<std::uint32_t> retries; | 4364 | std::vector<std::uint32_t> retries; |
| 4336 | std::size_t retry_at = 0; // where retry_console_slots goes on | 4365 | std::size_t retry_at = 0; // where retry_console_tsharps goes on (GPU mode) |
| 4366 | // Since the last log line: T# fixes left waiting (for a texture at their address, or room), and T#s that | ||
| 4367 | // waited and are bound now. | ||
| 4368 | std::uint64_t texture_waits = 0, texture_refills = 0; | ||
| 4337 | // fix_console_slots' budget, for the current refresh, and the slots it nulled since the last log line | 4369 | // fix_console_slots' budget, for the current refresh, and the slots it nulled since the last log line |
| 4338 | std::chrono::steady_clock::time_point fix_start{}; | 4370 | std::chrono::steady_clock::time_point fix_start{}; |
| 4339 | bool fix_over = false; | 4371 | bool fix_over = false; |
| @@ -4429,12 +4461,17 @@ struct DeviceState { | |||
| 4429 | } | 4461 | } |
| 4430 | 4462 | ||
| 4431 | // Slot `slot` of a heap copy, as the title wrote it, fixed up as described above, and the only writer of a | 4463 | // Slot `slot` of a heap copy, as the title wrote it, fixed up as described above, and the only writer of a |
| 4432 | // resource heap's slot records. True when it is settled: not a T# whose texture is still to come. A sampler heap | 4464 | // resource heap's slot records. A T# that found no texture, or one that does not fit it, is recorded as waiting |
| 4433 | // holds S#s: copied as written. | 4465 | // for the texture at its address (or for room in its bindless array): a texture registered, replaced or gone there |
| 4434 | bool fix_console_slot(ConsoleHeap& heap, std::size_t slot) { | 4466 | // fixes the slot again (apply_slot_changes), and nothing looks at it before. A sampler heap holds S#s: copied as |
| 4467 | // written. | ||
| 4468 | void fix_console_slot(ConsoleHeap& heap, std::size_t slot) { | ||
| 4435 | std::uint32_t w[kDescriptorSize / 4]; | 4469 | std::uint32_t w[kDescriptorSize / 4]; |
| 4436 | std::memcpy(w, heap.previous + slot * kDescriptorSize, kDescriptorSize); | 4470 | std::memcpy(w, heap.previous + slot * kDescriptorSize, kDescriptorSize); |
| 4437 | bool settled = true, vsharp = false; | 4471 | bool vsharp = false; |
| 4472 | using Records = leonida::ConsoleHeapSlotRecords; | ||
| 4473 | Records::State texture = Records::none; | ||
| 4474 | std::uint64_t texture_address = 0; | ||
| 4438 | VsharpRecord halves[2]; | 4475 | VsharpRecord halves[2]; |
| 4439 | const VsharpPath path = heap.scratch ? VsharpPath::scratch : VsharpPath::shadow; | 4476 | const VsharpPath path = heap.scratch ? VsharpPath::scratch : VsharpPath::shadow; |
| 4440 | if (!heap.resource_heap || !(w[0] | w[1] | w[2] | w[3])) { | 4477 | if (!heap.resource_heap || !(w[0] | w[1] | w[2] | w[3])) { |
| @@ -4459,14 +4496,26 @@ struct DeviceState { | |||
| 4459 | if (leonida::gfx10::is_image(words)) { | 4496 | if (leonida::gfx10::is_image(words)) { |
| 4460 | const char* why = nullptr; | 4497 | const char* why = nullptr; |
| 4461 | const TitleImage result = bind_title_image(*this, words, &why); | 4498 | const TitleImage result = bind_title_image(*this, words, &why); |
| 4462 | if (result == TitleImage::unknown || result == TitleImage::refused) { | 4499 | const bool unbound = result == TitleImage::unknown || result == TitleImage::refused; |
| 4500 | if (unbound) { | ||
| 4463 | note_refused_tsharp(heap, slot * kDescriptorSize, words, why); | 4501 | note_refused_tsharp(heap, slot * kDescriptorSize, words, why); |
| 4464 | if (result == TitleImage::refused) | 4502 | ++heap.refused_reasons[why ? why : "?"]; |
| 4465 | ++heap.refused_reasons[why ? why : "?"]; | ||
| 4466 | } | 4503 | } |
| 4467 | ++heap.images[unsigned(result)]; | 4504 | ++heap.images[unsigned(result)]; |
| 4468 | settled = result != TitleImage::unknown && result != TitleImage::refused; | 4505 | if (result == TitleImage::not_tsharp) { |
| 4469 | if (result == TitleImage::not_tsharp) std::memset(w, 0, 16); | 4506 | std::memset(w, 0, 16); |
| 4507 | } else { | ||
| 4508 | leonida::gfx10::Image image{}; | ||
| 4509 | leonida::gfx10::decode(words, image); | ||
| 4510 | texture_address = image.address; | ||
| 4511 | texture = !unbound ? Records::texture_bound | ||
| 4512 | : why == kTsharpArrayFull || why == kTsharpStorageFull ? Records::texture_waiting_room | ||
| 4513 | : Records::texture_waiting; | ||
| 4514 | if (unbound) ++heap.texture_waits; | ||
| 4515 | else if (Records::is_waiting(heap.records.state(slot, 0)) && | ||
| 4516 | heap.records.base(slot, 0) == texture_address) | ||
| 4517 | ++heap.texture_refills; // the T# waited for this address and is bound now | ||
| 4518 | } | ||
| 4470 | } else { | 4519 | } else { |
| 4471 | // Neither a buffer V# (TYPE is not 0) nor a T# the layer can decode: data the title keeps in the slot. | 4520 | // Neither a buffer V# (TYPE is not 0) nor a T# the layer can decode: data the title keeps in the slot. |
| 4472 | // No image is found by these words (the bindless lookup misses, as for a null T#), but a shader that | 4521 | // No image is found by these words (the bindless lookup misses, as for a null T#), but a shader that |
| @@ -4482,10 +4531,11 @@ struct DeviceState { | |||
| 4482 | for (unsigned half = 0; half < 2; ++half) | 4531 | for (unsigned half = 0; half < 2; ++half) |
| 4483 | heap.records.set(slot, half, halves[half].state, halves[half].base, halves[half].span, | 4532 | heap.records.set(slot, half, halves[half].state, halves[half].base, halves[half].span, |
| 4484 | halves[half].target); | 4533 | halves[half].target); |
| 4534 | else if (texture != Records::none) | ||
| 4535 | heap.records.set_texture(slot, texture, texture_address); | ||
| 4485 | else | 4536 | else |
| 4486 | heap.records.clear(slot); | 4537 | heap.records.clear(slot); |
| 4487 | } | 4538 | } |
| 4488 | return settled; | ||
| 4489 | } | 4539 | } |
| 4490 | 4540 | ||
| 4491 | // Diagnostics only: the class of a changed slot, and whether its V# halves were seen before (ConsoleHeap::changed). | 4541 | // Diagnostics only: the class of a changed slot, and whether its V# halves were seen before (ConsoleHeap::changed). |
| @@ -4519,7 +4569,7 @@ struct DeviceState { | |||
| 4519 | // The slots on [offset, offset + bytes) of a heap copy's mirror, just brought up to date: each that differs from | 4569 | // The slots on [offset, offset + bytes) of a heap copy's mirror, just brought up to date: each that differs from |
| 4520 | // what its arena slot was made from is written there and fixed up. | 4570 | // what its arena slot was made from is written there and fixed up. |
| 4521 | // LEONIDA_CONSOLE_FIX_US limits the fixing in one submission (0, the default: no limit). Past it a changed slot is | 4571 | // LEONIDA_CONSOLE_FIX_US limits the fixing in one submission (0, the default: no limit). Past it a changed slot is |
| 4522 | // written as a null descriptor and left to retry_console_slots: after the story load the title rewrote up to | 4572 | // written as a null descriptor and fixed at a later submission (fix_deferred_slots): after the story load the title rewrote up to |
| 4523 | // 3.7 MiB of its heap at once and one submission spent 28 s fixing it (speed-8). A shader reads such a slot as | 4573 | // 3.7 MiB of its heap at once and one submission spent 28 s fixing it (speed-8). A shader reads such a slot as |
| 4524 | // empty until its turn comes, so a limit changes results: at the first world frame lib_cs_01144 read the size it | 4574 | // empty until its turn comes, so a limit changes results: at the first world frame lib_cs_01144 read the size it |
| 4525 | // divides by from a nulled buffer slot and its loops never ended, which lost the device. | 4575 | // divides by from a nulled buffer slot and its loops never ended, which lost the device. |
| @@ -4559,20 +4609,43 @@ struct DeviceState { | |||
| 4559 | } | 4609 | } |
| 4560 | continue; | 4610 | continue; |
| 4561 | } | 4611 | } |
| 4562 | if (fix_console_slot(heap, slot)) { | 4612 | heap.retry[slot] = 0; |
| 4563 | heap.retry[slot] = 0; | 4613 | fix_console_slot(heap, slot); |
| 4564 | } else if (!heap.retry[slot]) { | 4614 | } |
| 4565 | heap.retry[slot] = 1; | 4615 | } |
| 4566 | heap.retries.push_back(std::uint32_t(slot)); | 4616 | |
| 4617 | // The slots LEONIDA_CONSOLE_FIX_US deferred at an earlier submission (null in the copy meanwhile): fixed first, | ||
| 4618 | // under the same budget; those past it wait for the next submission. Nothing else is in the list on this path. | ||
| 4619 | void fix_deferred_slots(ConsoleHeap& heap) { | ||
| 4620 | static const std::uint64_t budget_us = [] { | ||
| 4621 | char text[16] = {}; | ||
| 4622 | const DWORD n = GetEnvironmentVariableA("LEONIDA_CONSOLE_FIX_US", text, sizeof(text)); | ||
| 4623 | return n > 0 && n < sizeof(text) ? std::strtoull(text, nullptr, 10) : 0ull; | ||
| 4624 | }(); | ||
| 4625 | std::size_t done = 0; | ||
| 4626 | for (; done < heap.retries.size(); ++done) { | ||
| 4627 | if (budget_us && !(++heap.fix_count & 15) && | ||
| 4628 | std::chrono::steady_clock::now() - heap.fix_start >= std::chrono::microseconds(budget_us)) { | ||
| 4629 | heap.fix_over = true; | ||
| 4630 | break; | ||
| 4567 | } | 4631 | } |
| 4632 | const std::uint32_t slot = heap.retries[done]; | ||
| 4633 | if (!heap.retry[slot]) continue; // rewritten or zeroed since | ||
| 4634 | heap.retry[slot] = 0; | ||
| 4635 | fix_console_slot(heap, slot); | ||
| 4636 | ++heap.retried; | ||
| 4568 | } | 4637 | } |
| 4638 | heap.retries.erase(heap.retries.begin(), heap.retries.begin() + std::ptrdiff_t(done)); | ||
| 4569 | } | 4639 | } |
| 4570 | 4640 | ||
| 4571 | // The changes since the last submission (slot_changes), applied to a resource heap's copy: each slot whose record | 4641 | // The changes since the last submission (slot_changes), applied to a resource heap's copy: each slot whose record |
| 4572 | // a change touches is fixed again - a V# whose backing, copy or alias range went, so the copy never names freed | 4642 | // a change touches is fixed again - a V# whose backing, copy or alias range went, so the copy never names freed |
| 4573 | // memory, and a waiting V# over which a backing, alias range or title memory was added. Only the slots near the | 4643 | // memory, a waiting V# over which a backing, alias range or title memory was added or for which geometry room was |
| 4574 | // changed ranges are looked at (ConsoleHeapSlotRecords::touched); nothing reads the copy. | 4644 | // made, and a T# whose texture was registered, replaced or went at its address, or that waited for room in its |
| 4645 | // bindless array when entries were freed. Only the slots the changes name are looked at | ||
| 4646 | // (ConsoleHeapSlotRecords::touched); nothing reads the copy. | ||
| 4575 | std::vector<std::uint32_t> slot_change_scratch; // console_heap_mutex | 4647 | std::vector<std::uint32_t> slot_change_scratch; // console_heap_mutex |
| 4648 | std::uint64_t geometry_room_seen = 0; // geometry.free_generation() at the last refresh (console_heap_mutex) | ||
| 4576 | void apply_slot_changes(ConsoleHeap& heap, const std::vector<leonida::SlotChange>& changes) { | 4649 | void apply_slot_changes(ConsoleHeap& heap, const std::vector<leonida::SlotChange>& changes) { |
| 4577 | if (!heap.resource_heap || changes.empty()) return; | 4650 | if (!heap.resource_heap || changes.empty()) return; |
| 4578 | const auto start = std::chrono::steady_clock::now(); | 4651 | const auto start = std::chrono::steady_clock::now(); |
| @@ -4581,48 +4654,12 @@ struct DeviceState { | |||
| 4581 | heap.record_changes += changes.size(); | 4654 | heap.record_changes += changes.size(); |
| 4582 | heap.record_looked += heap.records.looked_at() - looked; | 4655 | heap.record_looked += heap.records.looked_at() - looked; |
| 4583 | heap.record_refixed += slot_change_scratch.size(); | 4656 | heap.record_refixed += slot_change_scratch.size(); |
| 4657 | // A slot deferred by the fix budget (null in the copy) is fixed from its list, not here. | ||
| 4584 | for (const std::uint32_t slot : slot_change_scratch) | 4658 | for (const std::uint32_t slot : slot_change_scratch) |
| 4585 | if (!fix_console_slot(heap, slot) && !heap.retry[slot]) { | 4659 | if (!heap.retry[slot]) fix_console_slot(heap, slot); |
| 4586 | heap.retry[slot] = 1; | ||
| 4587 | heap.retries.push_back(slot); | ||
| 4588 | } | ||
| 4589 | heap.record_ms += std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - start).count(); | 4660 | heap.record_ms += std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - start).count(); |
| 4590 | } | 4661 | } |
| 4591 | 4662 | ||
| 4592 | // The slots of a heap copy not settled yet: fixed again, and kept while they are still not. Round robin, for at | ||
| 4593 | // most LEONIDA_CONSOLE_RETRY_US a submission (500 by default, 0: all of them every time): during the story load | ||
| 4594 | // thousands of T#s name textures that come much later, and trying them all at every submission cost hundreds of | ||
| 4595 | // milliseconds each. One not reached waits a few submissions more for its texture. | ||
| 4596 | void retry_console_slots(ConsoleHeap& heap) { | ||
| 4597 | static const std::uint64_t budget_us = [] { | ||
| 4598 | char text[16] = {}; | ||
| 4599 | const DWORD n = GetEnvironmentVariableA("LEONIDA_CONSOLE_RETRY_US", text, sizeof(text)); | ||
| 4600 | return n > 0 && n < sizeof(text) ? std::strtoull(text, nullptr, 10) : 500ull; | ||
| 4601 | }(); | ||
| 4602 | const auto start = std::chrono::steady_clock::now(); | ||
| 4603 | std::size_t i = heap.retry_at, visited = 0; | ||
| 4604 | const std::size_t total = heap.retries.size(); | ||
| 4605 | while (visited < total && !heap.retries.empty()) { | ||
| 4606 | if (i >= heap.retries.size()) i = 0; | ||
| 4607 | const std::uint32_t slot = heap.retries[i]; | ||
| 4608 | ++visited; | ||
| 4609 | // Rewritten or zeroed since, or settled now: out of the list (the last one takes its place, seen later). | ||
| 4610 | const bool settled = !heap.retry[slot] || fix_console_slot(heap, slot); | ||
| 4611 | if (settled) { | ||
| 4612 | heap.retry[slot] = 0; | ||
| 4613 | heap.retries[i] = heap.retries.back(); | ||
| 4614 | heap.retries.pop_back(); | ||
| 4615 | } else { | ||
| 4616 | ++i; | ||
| 4617 | } | ||
| 4618 | if (budget_us && !(visited & 31) && | ||
| 4619 | std::chrono::steady_clock::now() - start >= std::chrono::microseconds(budget_us)) | ||
| 4620 | break; | ||
| 4621 | } | ||
| 4622 | heap.retry_at = i; | ||
| 4623 | heap.retried += visited; | ||
| 4624 | } | ||
| 4625 | |||
| 4626 | void note_refused_tsharp(ConsoleHeap& heap, std::size_t offset, const leonida::gfx10::ImageWords& words, | 4663 | void note_refused_tsharp(ConsoleHeap& heap, std::size_t offset, const leonida::gfx10::ImageWords& words, |
| 4627 | const char* why) { | 4664 | const char* why) { |
| 4628 | unbound_tsharp_tracker().note(heap.console_va, offset, words, why); | 4665 | unbound_tsharp_tracker().note(heap.console_va, offset, words, why); |
| @@ -4651,17 +4688,18 @@ struct DeviceState { | |||
| 4651 | heap.fix_start = t1; | 4688 | heap.fix_start = t1; |
| 4652 | heap.fix_over = false; | 4689 | heap.fix_over = false; |
| 4653 | heap.fix_count = 0; | 4690 | heap.fix_count = 0; |
| 4654 | heap.pages->for_each_copied([&](std::size_t offset, std::size_t bytes) { fix_console_slots(heap, offset, bytes); }); | ||
| 4655 | // Even an unchanged title slot may name a buffer released since the last submission: the slots the changes | ||
| 4656 | // touch are fixed again, found by their records. | ||
| 4657 | if (changes) apply_slot_changes(heap, *changes); | ||
| 4658 | const auto t2 = clock::now(); | 4691 | const auto t2 = clock::now(); |
| 4659 | // T#s whose texture is not there yet, and slots the fix budget deferred. | 4692 | // Slots the fix budget deferred at an earlier submission (LEONIDA_CONSOLE_FIX_US; none without it). |
| 4660 | if (!heap.retries.empty()) retry_console_slots(heap); | 4693 | if (!heap.retries.empty()) fix_deferred_slots(heap); |
| 4661 | const auto t3 = clock::now(); | 4694 | const auto t3 = clock::now(); |
| 4695 | heap.pages->for_each_copied([&](std::size_t offset, std::size_t bytes) { fix_console_slots(heap, offset, bytes); }); | ||
| 4696 | // Even an unchanged title slot may name a buffer released since the last submission, or wait for a buffer or | ||
| 4697 | // texture that came: the slots the changes touch are fixed again, found by their records. | ||
| 4698 | if (changes) apply_slot_changes(heap, *changes); | ||
| 4699 | const auto t4 = clock::now(); | ||
| 4662 | heap.pages_ms += ms(t0, t1); | 4700 | heap.pages_ms += ms(t0, t1); |
| 4663 | heap.fix_ms += ms(t1, t2); | ||
| 4664 | heap.retry_ms += ms(t2, t3); | 4701 | heap.retry_ms += ms(t2, t3); |
| 4702 | heap.fix_ms += ms(t1, t2) + ms(t3, t4); | ||
| 4665 | } | 4703 | } |
| 4666 | 4704 | ||
| 4667 | // Before every submission, ahead of the slot-133 shadows. | 4705 | // Before every submission, ahead of the slot-133 shadows. |
| @@ -4684,6 +4722,11 @@ struct DeviceState { | |||
| 4684 | std::lock_guard taken(slot_change_mutex); | 4722 | std::lock_guard taken(slot_change_mutex); |
| 4685 | changes.swap(slot_changes); | 4723 | changes.swap(slot_changes); |
| 4686 | } | 4724 | } |
| 4725 | // Geometry copies freed since (GeometryViews::reap): the V# halves refused a copy for lack of room ask again. | ||
| 4726 | if (const std::uint64_t freed = geometry.free_generation(); freed != geometry_room_seen) { | ||
| 4727 | geometry_room_seen = freed; | ||
| 4728 | changes.push_back({leonida::SlotChange::geometry_room, 0, 1}); | ||
| 4729 | } | ||
| 4687 | for (const auto& heap : console_heaps) { | 4730 | for (const auto& heap : console_heaps) { |
| 4688 | const auto start = std::chrono::steady_clock::now(); | 4731 | const auto start = std::chrono::steady_clock::now(); |
| 4689 | refresh_console_heap(*heap, &changes); | 4732 | refresh_console_heap(*heap, &changes); |
| @@ -4698,11 +4741,11 @@ struct DeviceState { | |||
| 4698 | heap->log_time = start; | 4741 | heap->log_time = start; |
| 4699 | std::fprintf(stderr, | 4742 | std::fprintf(stderr, |
| 4700 | "[rt] d3d12: console descriptor heap 0x%llx (%s): submissions up to %llu %s %.1f KiB each " | 4743 | "[rt] d3d12: console descriptor heap 0x%llx (%s): submissions up to %llu %s %.1f KiB each " |
| 4701 | "in %.3f ms on average (pages %.3f, fixing copied slots %.3f, %llu left to retries past the " | 4744 | "in %.3f ms on average (pages %.3f, fixing slots %.3f, %llu deferred past the budget, " |
| 4702 | "budget, retrying %.3f: %.1f slots a " | 4745 | "deferred ones %.3f: %.1f slots a submission, %zu left), %llu write faults; %llu title V#s " |
| 4703 | "submission, %zu waiting), %llu write faults; %llu title V#s rebased, %llu with no copy given " | 4746 | "rebased, %llu with no copy given no records (%zu V# halves waiting for a backing), T#s %llu " |
| 4704 | "no records (%zu V# halves waiting for a backing), T#s %llu known, %llu bound, %llu " | 4747 | "known, %llu bound, %llu naming no texture, %llu refused, %llu words that are no V# or T# " |
| 4705 | "naming no texture, %llu refused, %llu words that are no V# or T# (data, null in the copy)\n", | 4748 | "(data, null in the copy)\n", |
| 4706 | static_cast<unsigned long long>(heap->console_va), heap->imported ? "imported" : "copy", | 4749 | static_cast<unsigned long long>(heap->console_va), heap->imported ? "imported" : "copy", |
| 4707 | static_cast<unsigned long long>(heap->refreshes), "copied", | 4750 | static_cast<unsigned long long>(heap->refreshes), "copied", |
| 4708 | double(heap->copied) / 1024.0 / count, | 4751 | double(heap->copied) / 1024.0 / count, |
| @@ -4736,13 +4779,21 @@ struct DeviceState { | |||
| 4736 | static_cast<unsigned long long>(paths[4]), static_cast<unsigned long long>(paths[5])); | 4779 | static_cast<unsigned long long>(paths[4]), static_cast<unsigned long long>(paths[5])); |
| 4737 | // What releases, alias changes and additions cost: per submission, the changes taken, the record | 4780 | // What releases, alias changes and additions cost: per submission, the changes taken, the record |
| 4738 | // entries looked at to find their slots, and the slots fixed again (apply_slot_changes). | 4781 | // entries looked at to find their slots, and the slots fixed again (apply_slot_changes). |
| 4782 | // T#s: those in the heap now, those of them waiting (for a texture at their address, or room), and | ||
| 4783 | // since the last line the T# fixes that left one waiting and the waiting ones bound by a texture that | ||
| 4784 | // came (refills). | ||
| 4739 | std::fprintf(stderr, | 4785 | std::fprintf(stderr, |
| 4740 | "[rt] d3d12: console descriptor heap 0x%llx slot records per submission: %.1f changes, " | 4786 | "[rt] d3d12: console descriptor heap 0x%llx slot records per submission: %.1f changes, " |
| 4741 | "%.1f slots looked at, %.1f fixed again, %.3f ms; %zu V# halves recorded\n", | 4787 | "%.1f slots looked at, %.1f fixed again, %.3f ms; %zu V# halves recorded; T#s %zu " |
| 4788 | "recorded, %zu waiting for a texture; since the last line %llu T# fixes left waiting, " | ||
| 4789 | "%llu refills\n", | ||
| 4742 | static_cast<unsigned long long>(heap->console_va), double(heap->record_changes) / count, | 4790 | static_cast<unsigned long long>(heap->console_va), double(heap->record_changes) / count, |
| 4743 | double(heap->record_looked) / count, double(heap->record_refixed) / count, | 4791 | double(heap->record_looked) / count, double(heap->record_refixed) / count, |
| 4744 | heap->record_ms / count, heap->records.recorded()); | 4792 | heap->record_ms / count, heap->records.recorded(), heap->records.textures(), |
| 4793 | heap->records.textures_waiting(), static_cast<unsigned long long>(heap->texture_waits), | ||
| 4794 | static_cast<unsigned long long>(heap->texture_refills)); | ||
| 4745 | for (auto& n : heap->changed) n = 0; | 4795 | for (auto& n : heap->changed) n = 0; |
| 4796 | heap->texture_waits = heap->texture_refills = 0; | ||
| 4746 | heap->vsharp_halves = heap->vsharp_repeats = 0; | 4797 | heap->vsharp_halves = heap->vsharp_repeats = 0; |
| 4747 | heap->record_changes = heap->record_looked = heap->record_refixed = 0; | 4798 | heap->record_changes = heap->record_looked = heap->record_refixed = 0; |
| 4748 | heap->record_ms = 0; | 4799 | heap->record_ms = 0; |
| @@ -7314,8 +7365,8 @@ struct DeviceState { | |||
| 7314 | const Refusal refusal = bindless->image_refusal(dimension, entry.storage != VK_NULL_HANDLE, sampled_class); | 7365 | const Refusal refusal = bindless->image_refusal(dimension, entry.storage != VK_NULL_HANDLE, sampled_class); |
| 7315 | if (why) | 7366 | if (why) |
| 7316 | *why = refusal == Refusal::no_array ? "set 0 has no array for the T#'s dimension" | 7367 | *why = refusal == Refusal::no_array ? "set 0 has no array for the T#'s dimension" |
| 7317 | : refusal == Refusal::storage_full ? "the bindless storage arrays of the T#'s dimension are full" | 7368 | : refusal == Refusal::storage_full ? kTsharpStorageFull |
| 7318 | : "the bindless array of the T#'s dimension is full"; | 7369 | : kTsharpArrayFull; |
| 7319 | if (refusal != Refusal::no_array && | 7370 | if (refusal != Refusal::no_array && |
| 7320 | first_diagnostic("a bindless image array is full; dimension", int(dimension))) { | 7371 | first_diagnostic("a bindless image array is full; dimension", int(dimension))) { |
| 7321 | // Whether the array holds live textures or stale views: the T#s it holds, the textures they name, | 7372 | // Whether the array holds live textures or stale views: the T#s it holds, the textures they name, |
| @@ -7369,7 +7420,18 @@ struct DeviceState { | |||
| 7369 | void note_image_address(std::uint64_t address, const void* resource) { | 7420 | void note_image_address(std::uint64_t address, const void* resource) { |
| 7370 | std::lock_guard lock(descriptor_mutex); | 7421 | std::lock_guard lock(descriptor_mutex); |
| 7371 | images_by_address[address] = resource; | 7422 | images_by_address[address] = resource; |
| 7372 | } | 7423 | note_texture_change(leonida::SlotChange::texture_added, address); |
| 7424 | } | ||
| 7425 | // A texture registered, replaced or gone at `address`: the console heaps' T#s naming it (by that address or by | ||
| 7426 | // the other view of texture-pool memory, title_gpu_views) are fixed again at the next submission. | ||
| 7427 | void note_texture_change(leonida::SlotChange::Kind kind, std::uint64_t address) { | ||
| 7428 | if (!address) return; | ||
| 7429 | note_slot_change(kind, address, 1); | ||
| 7430 | if (const std::uint64_t other = leonida::title_gpu_views::other_view(address)) | ||
| 7431 | note_slot_change(kind, other, 1); | ||
| 7432 | } | ||
| 7433 | // Why no texture answers a T# (bind_title_image; descriptor_mutex held): the kTsharpNoTexture* reasons. | ||
| 7434 | const char* no_texture_reason_locked(const leonida::gfx10::Image& image); | ||
| 7373 | // LEONIDA_TRACE_TEXTURE=<address>[,<address>...] (hex, the base a T# names): the resource id of an image registered | 7435 | // LEONIDA_TRACE_TEXTURE=<address>[,<address>...] (hex, the base a T# names): the resource id of an image registered |
| 7374 | // at that address, so a draw's T# can be followed to the passes that write it (fsdraw rt0, UAV views, copies). | 7436 | // at that address, so a draw's T# can be followed to the passes that write it (fsdraw rt0, UAV views, copies). |
| 7375 | static void trace_image(std::uint64_t address, leonida::ResourceId id, const char* where) { | 7437 | static void trace_image(std::uint64_t address, leonida::ResourceId id, const char* where) { |
| @@ -7403,21 +7465,29 @@ struct DeviceState { | |||
| 7403 | images_by_address.erase(address); | 7465 | images_by_address.erase(address); |
| 7404 | forget_image_views_locked(address); | 7466 | forget_image_views_locked(address); |
| 7405 | } | 7467 | } |
| 7468 | // Its T#s stop naming it at once (a shader's lookup of their words misses from the next table flush on); the | ||
| 7469 | // console heap slots that held them wait for the next texture at the address (note_texture_change), and the | ||
| 7470 | // indices freed let T#s that found their array full bind. | ||
| 7406 | void forget_image_views_locked(std::uint64_t address) { | 7471 | void forget_image_views_locked(std::uint64_t address) { |
| 7472 | note_texture_change(leonida::SlotChange::texture_gone, address); | ||
| 7407 | if (!bindless) return; | 7473 | if (!bindless) return; |
| 7474 | bool freed = false; | ||
| 7408 | const auto [first, last] = image_keys.equal_range(address); | 7475 | const auto [first, last] = image_keys.equal_range(address); |
| 7409 | for (auto it = first; it != last; ++it) { | 7476 | for (auto it = first; it != last; ++it) { |
| 7410 | const auto entry = image_entries.find(it->second); | 7477 | const auto entry = image_entries.find(it->second); |
| 7411 | bindless->erase_key(it->second); | 7478 | bindless->erase_key(it->second); |
| 7412 | release(entry->second); | 7479 | release(entry->second); |
| 7413 | image_entries.erase(entry); | 7480 | image_entries.erase(entry); |
| 7481 | freed = true; | ||
| 7414 | } | 7482 | } |
| 7415 | image_keys.erase(first, last); | 7483 | image_keys.erase(first, last); |
| 7416 | std::erase_if(replaced_images, [&](const ImageEntry& entry) { | 7484 | std::erase_if(replaced_images, [&](const ImageEntry& entry) { |
| 7417 | if (entry.address != address) return false; | 7485 | if (entry.address != address) return false; |
| 7418 | release(entry); | 7486 | release(entry); |
| 7487 | freed = true; | ||
| 7419 | return true; | 7488 | return true; |
| 7420 | }); | 7489 | }); |
| 7490 | if (freed) note_slot_change(leonida::SlotChange::texture_room, 0, 1); | ||
| 7421 | } | 7491 | } |
| 7422 | 7492 | ||
| 7423 | // Gives the S# `words` a sampler in set 0 and an entry in the hash table, unless it has them. | 7493 | // Gives the S# `words` a sampler in set 0 and an entry in the hash table, unless it has them. |
| @@ -9194,6 +9264,8 @@ public: | |||
| 9194 | for (const auto& [words, image] : pending.mapped().keys) | 9264 | for (const auto& [words, image] : pending.mapped().keys) |
| 9195 | device_.bind_image_locked(words, image, image_, format_, aspects_, false, has_view_format_list(), | 9265 | device_.bind_image_locked(words, image, image_, format_, aspects_, false, has_view_format_list(), |
| 9196 | uint_backed_); | 9266 | uint_backed_); |
| 9267 | // Its image is there now: title-written T#s refused while it had none are fixed again. | ||
| 9268 | device_.note_texture_change(leonida::SlotChange::texture_added, image_address_); | ||
| 9197 | } | 9269 | } |
| 9198 | deferred_stream_ = false; | 9270 | deferred_stream_ = false; |
| 9199 | } | 9271 | } |
| @@ -13507,6 +13579,7 @@ void DeviceState::title_texture_gone(const void* resource, std::uint64_t address | |||
| 13507 | // reads gpu_placements under descriptor_mutex alone, so the placement must not outlive the resource. | 13579 | // reads gpu_placements under descriptor_mutex alone, so the placement must not outlive the resource. |
| 13508 | std::lock_guard descriptors(descriptor_mutex); | 13580 | std::lock_guard descriptors(descriptor_mutex); |
| 13509 | gpu_placements.remove(address, resource); | 13581 | gpu_placements.remove(address, resource); |
| 13582 | note_texture_change(leonida::SlotChange::texture_gone, address); | ||
| 13510 | } | 13583 | } |
| 13511 | const auto it = title_textures.find(address); | 13584 | const auto it = title_textures.find(address); |
| 13512 | if (it != title_textures.end() && it->second->resource == resource) it->second->resource = nullptr; | 13585 | if (it != title_textures.end() && it->second->resource == resource) it->second->resource = nullptr; |
| @@ -13518,6 +13591,7 @@ void DeviceState::title_texture_gone(const void* resource, std::uint64_t address | |||
| 13518 | { | 13591 | { |
| 13519 | std::lock_guard descriptors(descriptor_mutex); | 13592 | std::lock_guard descriptors(descriptor_mutex); |
| 13520 | gpu_placements.remove(address, resource); | 13593 | gpu_placements.remove(address, resource); |
| 13594 | note_texture_change(leonida::SlotChange::texture_gone, address); | ||
| 13521 | } | 13595 | } |
| 13522 | const auto it = title_textures.find(address); | 13596 | const auto it = title_textures.find(address); |
| 13523 | if (it != title_textures.end() && it->second->resource == resource) { | 13597 | if (it != title_textures.end() && it->second->resource == resource) { |
| @@ -13599,6 +13673,22 @@ void note_unknown_tsharp(const leonida::gfx10::ImageWords& words, const leonida: | |||
| 13599 | } | 13673 | } |
| 13600 | } | 13674 | } |
| 13601 | 13675 | ||
| 13676 | const char* DeviceState::no_texture_reason_locked(const leonida::gfx10::Image& image) { | ||
| 13677 | std::uint64_t lo = 0, hi = 0; | ||
| 13678 | if (leonida::title_va_aliases().title_memory(image.address, lo, hi)) return kTsharpNoTextureTitleMemory; | ||
| 13679 | // The layer's own texture addresses: 1 << 32 and up, 256 apart (Resource::image_address). | ||
| 13680 | constexpr std::uint64_t ours = std::uint64_t(1) << 32; | ||
| 13681 | const std::uint64_t next = next_image_address.load(std::memory_order_relaxed); | ||
| 13682 | if (image.address >= ours && image.address < next) return kTsharpNoTextureGone; | ||
| 13683 | const std::uint64_t low = ours | (image.address & 0xffffffffull); | ||
| 13684 | if (low >= next) return kTsharpNoTextureNothing; | ||
| 13685 | const auto found = images_by_address.find(low); | ||
| 13686 | if (found == images_by_address.end()) return kTsharpNoTextureLow32Gone; | ||
| 13687 | const auto& owner = *static_cast<const Resource*>(found->second); | ||
| 13688 | return TitleGpuPlacements<Resource>::matches(image, owner.desc()) ? kTsharpNoTextureLow32Same | ||
| 13689 | : kTsharpNoTextureLow32Other; | ||
| 13690 | } | ||
| 13691 | |||
| 13602 | TitleImage bind_title_image(DeviceState& device, const leonida::gfx10::ImageWords& words, const char** why) { | 13692 | TitleImage bind_title_image(DeviceState& device, const leonida::gfx10::ImageWords& words, const char** why) { |
| 13603 | namespace g = leonida::gfx10; | 13693 | namespace g = leonida::gfx10; |
| 13604 | const auto refuse = [why](const char* reason) { | 13694 | const auto refuse = [why](const char* reason) { |
| @@ -13631,7 +13721,7 @@ TitleImage bind_title_image(DeviceState& device, const leonida::gfx10::ImageWord | |||
| 13631 | if (auto* candidate = device.gpu_placements.find(image.address, alias, matches)) owner = candidate; | 13721 | if (auto* candidate = device.gpu_placements.find(image.address, alias, matches)) owner = candidate; |
| 13632 | } | 13722 | } |
| 13633 | if (!owner) { | 13723 | if (!owner) { |
| 13634 | if (why) *why = "no texture at T# address"; | 13724 | if (why) *why = device.no_texture_reason_locked(image); |
| 13635 | note_unknown_tsharp(words, image); | 13725 | note_unknown_tsharp(words, image); |
| 13636 | return TitleImage::unknown; | 13726 | return TitleImage::unknown; |
| 13637 | } | 13727 | } |
| @@ -15427,6 +15517,7 @@ public: | |||
| 15427 | if (entry->gpu_written && !(address & 0xff) && texture->image_address() != address) { | 15517 | if (entry->gpu_written && !(address & 0xff) && texture->image_address() != address) { |
| 15428 | std::lock_guard descriptors(state_.descriptor_mutex); | 15518 | std::lock_guard descriptors(state_.descriptor_mutex); |
| 15429 | state_.gpu_placements.add(address, texture); | 15519 | state_.gpu_placements.add(address, texture); |
| 15520 | state_.note_texture_change(leonida::SlotChange::texture_added, address); | ||
| 15430 | } | 15521 | } |
| 15431 | auto& slot = state_.title_textures[address]; | 15522 | auto& slot = state_.title_textures[address]; |
| 15432 | if (slot && slot->owned) retire_title_texture(*slot); | 15523 | if (slot && slot->owned) retire_title_texture(*slot); |
diff --git a/runtime/src/console_heap_slot_records.h b/runtime/src/console_heap_slot_records.h index ae4f151bf..9e37b67e3 100644 --- a/runtime/src/console_heap_slot_records.h +++ b/runtime/src/console_heap_slot_records.h | |||
| @@ -1,30 +1,39 @@ | |||
| 1 | #pragma once | 1 | #pragma once |
| 2 | // What each buffer V# of a console resource heap's GPU copy resolved to (docs/d3d12_vulkan.md section 3.4), kept in | 2 | // What each buffer V# and title-written T# of a console resource heap's GPU copy resolved to (docs/d3d12_vulkan.md |
| 3 | // host RAM so that a change of the address space re-fixes exactly the slots it touches. | 3 | // section 3.4), kept in host RAM so that a change of the address space re-fixes exactly the slots it touches. |
| 4 | // | 4 | // |
| 5 | // A slot of the copy is the title's descriptor with its V# bases rewritten (DeviceState::fix_console_slot). What a | 5 | // A slot of the copy is the title's descriptor with its V# bases rewritten and its T# bound (DeviceState:: |
| 6 | // base became depends on things that change after the slot was written: a buffer or window released, an alias range | 6 | // fix_console_slot). What a base became depends on things that change after the slot was written: a buffer or window |
| 7 | // removed, a geometry copy retired, or (for a base nothing backed yet) a buffer, alias or title memory added. Without | 7 | // released, an alias range removed, a geometry copy retired, a texture registered, replaced or gone at an address, or |
| 8 | // a record of which slot resolved into which range, a change could only be applied by walking every live slot. Here | 8 | // (for a base nothing backed yet) a buffer, alias or title memory added. Without a record of which slot resolved into |
| 9 | // each V# half (the buffer V# and the UAV counter V# of a slot) records: | 9 | // which range, a change could only be applied by walking every live slot. Here each V# half (the buffer V# and the |
| 10 | // UAV counter V# of a slot) records: | ||
| 10 | // - source: the bytes the title's V# names, [base, base + span) (the base is the V#'s 48 bits); | 11 | // - source: the bytes the title's V# names, [base, base + span) (the base is the V#'s 48 bits); |
| 11 | // - target: the device addresses its GPU copy names, [target, target + span); for a half that resolved to nothing, | 12 | // - target: the device addresses its GPU copy names, [target, target + span); for a half that resolved to nothing, |
| 12 | // those of a copy that was found but refused (not backed yet), or none; | 13 | // those of a copy that was found but refused (not backed yet), or none; |
| 13 | // - state: resolved, waiting (written with no base and no records until something backs it) or none. | 14 | // - state: resolved, waiting (written with no base and no records until something backs it), waiting_room (as |
| 15 | // waiting, and its geometry copy was refused for lack of room) or none. | ||
| 16 | // The T# of a slot has a record too (set_texture), in the first half: the address it names (its base), and whether a | ||
| 17 | // texture bound it or it waits for one (texture_waiting_room: refused because its bindless array was full). Title- | ||
| 18 | // written T#s are looked up by their exact base address (bind_title_image), so a third index holds them by that | ||
| 19 | // address alone. | ||
| 14 | // | 20 | // |
| 15 | // Two indexes, by source and by target, hold the halves by their exact range. The halves that name the same range | 21 | // The three indexes (V# source, V# target, T# address) hold the records by their exact range (a T#'s is its address |
| 16 | // share one group (the title writes the same V# into thousands of slots), which keeps them in two lists, resolved and | 22 | // and the byte after it). The records that name the same range share one group (the title writes the same descriptor |
| 17 | // waiting, with each half's place in its list, so a half moves or goes in O(1) and no list holds a stale entry. The | 23 | // into thousands of slots), which keeps them in two lists, settled (resolved, bound) and waiting, with each record's |
| 18 | // groups are ordered by start, separately for each power-of-two class of their length: a group of class c (length at | 24 | // place in its list, so a record moves or goes in O(1) and no list holds a stale entry. The groups are ordered by |
| 19 | // most 2^c) overlaps [lo, hi) only if it starts in (lo - 2^c, hi), so a query searches each class in use once and walks | 25 | // start, separately for each power-of-two class of their length: a group of class c (length at most 2^c) overlaps |
| 20 | // only the groups that start there. | 26 | // [lo, hi) only if it starts in (lo - 2^c, hi), so a query searches each class in use once and walks only the groups |
| 27 | // that start there. Records waiting for room are also kept in one list per kind (V# halves, T#s), with their place, | ||
| 28 | // which a change saying room was made visits whole. | ||
| 21 | // | 29 | // |
| 22 | // The bound: a change visits exactly the halves whose recorded range overlaps it (a release or removal: resolved and | 30 | // The bound: a change visits exactly the records it concerns - a release or removal the V# halves whose recorded |
| 23 | // waiting ones; an addition: waiting ones only); looked_at() counts those and nothing else. The groups walked to find | 31 | // range overlaps it, an addition the waiting ones among them, a texture gone the T#s naming its address, a texture |
| 24 | // them are the overlapping ones plus, per class, those that start less than 2^c before the change and end before it: | 32 | // added the waiting ones, room made the records waiting for that room - and looked_at() counts those and nothing |
| 25 | // each costs one comparison, never a slot. Memory: 20 bytes a half (40 a slot, allocated with calloc, so pages of | 33 | // else. The groups walked to find them are the overlapping ones plus, per class, those that start less than 2^c |
| 26 | // slots never written are never touched), and per distinct range a group (two lists of 4 bytes a half, a hash entry | 34 | // before the change and end before it: each costs one comparison, never a slot. Memory: 32 bytes a half (64 a slot, |
| 27 | // and an ordered-map entry). | 35 | // allocated with calloc, so pages of slots never written are never touched), and per distinct range a group (two lists |
| 36 | // of 4 bytes a record, a hash entry and an ordered-map entry). | ||
| 28 | // Portable, standard library only: console_heap_slot_records_test exercises it without a GPU. | 37 | // Portable, standard library only: console_heap_slot_records_test exercises it without a GPU. |
| 29 | #include <algorithm> | 38 | #include <algorithm> |
| 30 | #include <array> | 39 | #include <array> |
| @@ -42,10 +51,14 @@ namespace leonida { | |||
| 42 | // One change since the last submission, as a range; hi is exclusive and greater than lo. | 51 | // One change since the last submission, as a range; hi is exclusive and greater than lo. |
| 43 | struct SlotChange { | 52 | struct SlotChange { |
| 44 | enum Kind : std::uint8_t { | 53 | enum Kind : std::uint8_t { |
| 45 | target_gone, // device addresses: a backing released, a copy's block destroyed, an alias range's copy gone | 54 | target_gone, // device addresses: a backing released, a copy's block destroyed, an alias range's copy gone |
| 46 | source_gone, // V# bases: an alias range removed or replaced, or a copy that answered for it gone | 55 | source_gone, // V# bases: an alias range removed or replaced, or a copy that answered for it gone |
| 47 | target_added, // device addresses: a backing added (a refused alias target may be backed now) | 56 | target_added, // device addresses: a backing added (a refused alias target may be backed now) |
| 48 | source_added, // V# bases: an alias range or title memory added, or a backing added at those arena offsets | 57 | source_added, // V# bases: an alias range or title memory added, or a backing added at those arena offsets |
| 58 | texture_gone, // a T# address (lo; hi = lo + 1): the texture registered there went or was replaced | ||
| 59 | texture_added, // a T# address: a texture registered or replaced there, or its image published | ||
| 60 | geometry_room, // geometry copies were freed: halves refused a copy for lack of room may get one (no range) | ||
| 61 | texture_room, // bindless image entries were freed: T#s refused for a full array may bind (no range) | ||
| 49 | }; | 62 | }; |
| 50 | Kind kind; | 63 | Kind kind; |
| 51 | std::uint64_t lo, hi; | 64 | std::uint64_t lo, hi; |
| @@ -53,7 +66,21 @@ struct SlotChange { | |||
| 53 | 66 | ||
| 54 | class ConsoleHeapSlotRecords { | 67 | class ConsoleHeapSlotRecords { |
| 55 | public: | 68 | public: |
| 56 | enum State : std::uint8_t { none = 0, resolved = 1, waiting = 2 }; | 69 | // V# halves: resolved, waiting (for a backing over its base or target), waiting_room (and for geometry room). |
| 70 | // T#s (the first half): texture_bound, texture_waiting (for a texture at its address), texture_waiting_room (and | ||
| 71 | // for room in its bindless array). | ||
| 72 | enum State : std::uint8_t { | ||
| 73 | none = 0, | ||
| 74 | resolved = 1, | ||
| 75 | waiting = 2, | ||
| 76 | waiting_room = 3, | ||
| 77 | texture_bound = 4, | ||
| 78 | texture_waiting = 5, | ||
| 79 | texture_waiting_room = 6, | ||
| 80 | }; | ||
| 81 | static bool is_vsharp(State s) { return s >= resolved && s <= waiting_room; } | ||
| 82 | static bool is_texture(State s) { return s >= texture_bound; } | ||
| 83 | static bool is_waiting(State s) { return s == waiting || s == waiting_room || s >= texture_waiting; } | ||
| 57 | 84 | ||
| 58 | void reset(std::size_t slots) { | 85 | void reset(std::size_t slots) { |
| 59 | slots_ = slots; | 86 | slots_ = slots; |
| @@ -61,46 +88,42 @@ public: | |||
| 61 | halves_.reset(slots ? static_cast<Half*>(std::calloc(slots * 2, sizeof(Half))) : nullptr); | 88 | halves_.reset(slots ? static_cast<Half*>(std::calloc(slots * 2, sizeof(Half))) : nullptr); |
| 62 | if (slots && !halves_) slots_ = 0; | 89 | if (slots && !halves_) slots_ = 0; |
| 63 | for (auto& index : indexes_) index = Index{}; | 90 | for (auto& index : indexes_) index = Index{}; |
| 91 | for (auto& room : room_) room.clear(); | ||
| 64 | groups_.clear(); | 92 | groups_.clear(); |
| 65 | free_groups_.clear(); | 93 | free_groups_.clear(); |
| 66 | recorded_ = waiting_ = 0; | 94 | recorded_ = waiting_ = textures_count_ = textures_waiting_ = 0; |
| 67 | looked_at_ = groups_looked_at_ = 0; | 95 | looked_at_ = groups_looked_at_ = 0; |
| 68 | } | 96 | } |
| 69 | std::size_t slots() const { return slots_; } | 97 | std::size_t slots() const { return slots_; } |
| 70 | std::size_t recorded() const { return recorded_; } // halves in state resolved or waiting | 98 | std::size_t recorded() const { return recorded_; } // V# halves resolved or waiting |
| 71 | std::size_t waiting_count() const { return waiting_; } // halves in state waiting | 99 | std::size_t waiting_count() const { return waiting_; } // V# halves waiting (for a backing or room) |
| 72 | std::size_t ranges() const { return groups_.size() - free_groups_.size(); } // distinct ranges, both indexes | 100 | std::size_t textures() const { return textures_count_; } // T# records, bound or waiting |
| 73 | std::uint64_t looked_at() const { return looked_at_; } // halves visited by touched(), all calls | 101 | std::size_t textures_waiting() const { return textures_waiting_; } // T#s waiting for a texture or room |
| 102 | std::size_t ranges() const { return groups_.size() - free_groups_.size(); } // distinct ranges, all indexes | ||
| 103 | std::uint64_t looked_at() const { return looked_at_; } // records visited by touched(), all calls | ||
| 74 | std::uint64_t ranges_looked_at() const { return groups_looked_at_; } // groups compared by touched(), all calls | 104 | std::uint64_t ranges_looked_at() const { return groups_looked_at_; } // groups compared by touched(), all calls |
| 75 | 105 | ||
| 76 | // Half `half` (0: the buffer V#, 1: the UAV counter V#) of `slot` now names [base, base + span) and resolved as | 106 | // Half `half` (0: the buffer V#, 1: the UAV counter V#) of `slot` now names [base, base + span) and resolved as |
| 77 | // `state` says, its copy at `target` (0: none; see above). span 0 counts as 1. | 107 | // `state` says (none or a V# state), its copy at `target` (0: none; see above). span 0 counts as 1. |
| 78 | void set(std::size_t slot, unsigned half, State state, std::uint64_t base, std::uint64_t span, | 108 | void set(std::size_t slot, unsigned half, State state, std::uint64_t base, std::uint64_t span, |
| 79 | std::uint64_t target) { | 109 | std::uint64_t target) { |
| 80 | if (slot >= slots_ || half > 1) return; | 110 | if (slot >= slots_ || half > 1 || is_texture(state)) return; |
| 81 | const auto key = std::uint32_t(slot * 2 + half); | ||
| 82 | Half& h = halves_[key]; | ||
| 83 | if (state == none) { | 111 | if (state == none) { |
| 84 | if (h.state != none) { | 112 | drop(std::uint32_t(slot * 2 + half)); |
| 85 | for (int which = 0; which < 2; ++which) detach(which, key); | ||
| 86 | count(State(h.state), -1); | ||
| 87 | h.state = none; | ||
| 88 | } | ||
| 89 | return; | 113 | return; |
| 90 | } | 114 | } |
| 91 | if (!span) span = 1; | 115 | if (!span) span = 1; |
| 92 | const Range ranges[2] = {range(base, span), target ? range(target, span) : Range{}}; | 116 | const Range ranges[2] = {range(base, span), target ? range(target, span) : Range{}}; |
| 93 | for (int which = 0; which < 2; ++which) { | 117 | place(std::uint32_t(slot * 2 + half), state, base, ranges); |
| 94 | const std::uint32_t group = h.group[which]; | 118 | } |
| 95 | const bool keep = group && h.state == state && groups_[group - 1].start == ranges[which].start && | 119 | |
| 96 | groups_[group - 1].end == ranges[which].end; | 120 | // The T# of `slot` names `address` (its base) and is bound or waits as `state` (a T# state) says. The slot's |
| 97 | if (keep) continue; | 121 | // second half is no V# then: its record goes. |
| 98 | detach(which, key); | 122 | void set_texture(std::size_t slot, State state, std::uint64_t address) { |
| 99 | if (ranges[which].end) attach(which, key, ranges[which], state); | 123 | if (slot >= slots_ || !is_texture(state)) return; |
| 100 | } | 124 | drop(std::uint32_t(slot * 2 + 1)); |
| 101 | count(State(h.state), -1); | 125 | const Range ranges[2] = {range(address, 1), Range{}}; |
| 102 | h.state = state; | 126 | place(std::uint32_t(slot * 2), state, address, ranges); |
| 103 | count(state, +1); | ||
| 104 | } | 127 | } |
| 105 | 128 | ||
| 106 | void clear(std::size_t slot) { | 129 | void clear(std::size_t slot) { |
| @@ -111,37 +134,59 @@ public: | |||
| 111 | end = (std::min)(end, slots_); | 134 | end = (std::min)(end, slots_); |
| 112 | for (std::size_t slot = first; slot < end; ++slot) | 135 | for (std::size_t slot = first; slot < end; ++slot) |
| 113 | for (unsigned half = 0; half < 2; ++half) | 136 | for (unsigned half = 0; half < 2; ++half) |
| 114 | if (halves_[slot * 2 + half].state != none) set(slot, half, none, 0, 0, 0); | 137 | if (halves_[slot * 2 + half].state != none) drop(std::uint32_t(slot * 2 + half)); |
| 115 | } | 138 | } |
| 116 | 139 | ||
| 140 | std::uint64_t base(std::size_t slot, unsigned half) const { | ||
| 141 | return slot < slots_ && half < 2 ? halves_[slot * 2 + half].base : 0; | ||
| 142 | } | ||
| 117 | State state(std::size_t slot, unsigned half) const { | 143 | State state(std::size_t slot, unsigned half) const { |
| 118 | return slot < slots_ && half < 2 ? State(halves_[slot * 2 + half].state) : none; | 144 | return slot < slots_ && half < 2 ? State(halves_[slot * 2 + half].state) : none; |
| 119 | } | 145 | } |
| 120 | std::uint64_t target(std::size_t slot, unsigned half) const { | 146 | std::uint64_t target(std::size_t slot, unsigned half) const { |
| 121 | if (slot >= slots_ || half > 1) return 0; | 147 | if (slot >= slots_ || half > 1) return 0; |
| 122 | const std::uint32_t group = halves_[slot * 2 + half].group[kTarget]; | 148 | const Half& h = halves_[slot * 2 + half]; |
| 123 | return group ? groups_[group - 1].start : 0; | 149 | return is_vsharp(State(h.state)) && h.group[1] ? groups_[h.group[1] - 1].start : 0; |
| 124 | } | 150 | } |
| 125 | 151 | ||
| 126 | // The slots `changes` touch, each once, ascending, appended to `out` (cleared first): | 152 | // The slots `changes` touch, each once, ascending, appended to `out` (cleared first): |
| 127 | // target_gone: halves whose [target, target + span) overlaps the range (whatever their state); | 153 | // target_gone: V# halves whose [target, target + span) overlaps the range (whatever their state); |
| 128 | // source_gone: halves whose [base, base + span) overlaps it; | 154 | // source_gone: V# halves whose [base, base + span) overlaps it; |
| 129 | // target_added, source_added: the same, waiting halves only (a resolved one stays valid until something goes). | 155 | // target_added, source_added: the same, waiting halves only (a resolved one stays valid until something goes); |
| 156 | // texture_gone: T#s naming the address lo, bound or waiting; texture_added: those waiting; | ||
| 157 | // geometry_room: V# halves waiting for room; texture_room: T#s waiting for room (each list once a call). | ||
| 130 | void touched(const SlotChange* changes, std::size_t count, std::vector<std::uint32_t>& out) { | 158 | void touched(const SlotChange* changes, std::size_t count, std::vector<std::uint32_t>& out) { |
| 131 | out.clear(); | 159 | out.clear(); |
| 160 | bool room[2] = {}; | ||
| 132 | for (std::size_t i = 0; i < count; ++i) { | 161 | for (std::size_t i = 0; i < count; ++i) { |
| 133 | const SlotChange& change = changes[i]; | 162 | const SlotChange& change = changes[i]; |
| 163 | switch (change.kind) { | ||
| 164 | case SlotChange::geometry_room: room[0] = true; continue; | ||
| 165 | case SlotChange::texture_room: room[1] = true; continue; | ||
| 166 | case SlotChange::texture_gone: | ||
| 167 | case SlotChange::texture_added: | ||
| 168 | if (change.lo != UINT64_MAX) | ||
| 169 | find(kTexture, change.lo, change.lo + 1, change.kind == SlotChange::texture_added, out); | ||
| 170 | continue; | ||
| 171 | default: break; | ||
| 172 | } | ||
| 134 | if (change.hi <= change.lo) continue; | 173 | if (change.hi <= change.lo) continue; |
| 135 | const bool target = change.kind == SlotChange::target_gone || change.kind == SlotChange::target_added; | 174 | const bool target = change.kind == SlotChange::target_gone || change.kind == SlotChange::target_added; |
| 136 | const bool waiting_only = change.kind == SlotChange::target_added || change.kind == SlotChange::source_added; | 175 | const bool waiting_only = change.kind == SlotChange::target_added || change.kind == SlotChange::source_added; |
| 137 | find(target ? kTarget : kSource, change.lo, change.hi, waiting_only, out); | 176 | find(target ? kTarget : kSource, change.lo, change.hi, waiting_only, out); |
| 138 | } | 177 | } |
| 178 | for (int which = 0; which < 2; ++which) | ||
| 179 | if (room[which]) | ||
| 180 | for (const std::uint32_t key : room_[which]) { | ||
| 181 | ++looked_at_; | ||
| 182 | out.push_back(key >> 1); | ||
| 183 | } | ||
| 139 | std::sort(out.begin(), out.end()); | 184 | std::sort(out.begin(), out.end()); |
| 140 | out.erase(std::unique(out.begin(), out.end()), out.end()); | 185 | out.erase(std::unique(out.begin(), out.end()), out.end()); |
| 141 | } | 186 | } |
| 142 | 187 | ||
| 143 | private: | 188 | private: |
| 144 | static constexpr int kSource = 0, kTarget = 1; | 189 | static constexpr int kSource = 0, kTarget = 1, kTexture = 2; |
| 145 | static constexpr std::uint64_t kMaxSpan = (std::uint64_t(1) << 56) - 1; | 190 | static constexpr std::uint64_t kMaxSpan = (std::uint64_t(1) << 56) - 1; |
| 146 | static constexpr unsigned kClasses = 65; | 191 | static constexpr unsigned kClasses = 65; |
| 147 | 192 | ||
| @@ -154,10 +199,13 @@ private: | |||
| 154 | return std::hash<std::uint64_t>{}(r.start ^ (r.end * 0x9e3779b97f4a7c15ull)); | 199 | return std::hash<std::uint64_t>{}(r.start ^ (r.end * 0x9e3779b97f4a7c15ull)); |
| 155 | } | 200 | } |
| 156 | }; | 201 | }; |
| 157 | // Zero-initialised by calloc: no group (ids are 1-based), state none. | 202 | // Zero-initialised by calloc: no group (ids are 1-based), state none. A V# half is in the source index (group[0]) |
| 203 | // and, with a target, the target index (group[1]); a T# in the texture index (group[0]). | ||
| 158 | struct Half { | 204 | struct Half { |
| 159 | std::uint32_t group[2]; // the source and the target group, id + 1, 0: none | 205 | std::uint64_t base; // the V#'s base or the T#'s address, as given |
| 160 | std::uint32_t place[2]; // the half's place in that group's list for its state | 206 | std::uint32_t group[2]; // id + 1, 0: none |
| 207 | std::uint32_t place[2]; // the record's place in that group's list for its state | ||
| 208 | std::uint32_t room; // its place in room_ while it waits for room | ||
| 161 | std::uint8_t state; | 209 | std::uint8_t state; |
| 162 | }; | 210 | }; |
| 163 | struct Free { | 211 | struct Free { |
| @@ -166,7 +214,8 @@ private: | |||
| 166 | using ByStart = std::multimap<std::uint64_t, std::uint32_t>; // start -> group id | 214 | using ByStart = std::multimap<std::uint64_t, std::uint32_t>; // start -> group id |
| 167 | struct Group { | 215 | struct Group { |
| 168 | std::uint64_t start = 0, end = 0; | 216 | std::uint64_t start = 0, end = 0; |
| 169 | std::vector<std::uint32_t> keys[2]; // [0] resolved halves, [1] waiting halves | 217 | std::vector<std::uint32_t> keys[2]; // [0] settled records (resolved, bound), [1] waiting ones |
| 218 | int index = 0; | ||
| 170 | unsigned cls = 0; | 219 | unsigned cls = 0; |
| 171 | ByStart::iterator at{}; | 220 | ByStart::iterator at{}; |
| 172 | }; | 221 | }; |
| @@ -184,14 +233,68 @@ private: | |||
| 184 | static unsigned length_class(std::uint64_t length) { | 233 | static unsigned length_class(std::uint64_t length) { |
| 185 | return length <= 1 ? 0u : unsigned(64 - std::countl_zero(length - 1)); | 234 | return length <= 1 ? 0u : unsigned(64 - std::countl_zero(length - 1)); |
| 186 | } | 235 | } |
| 187 | static int list_of(State state) { return state == waiting ? 1 : 0; } | 236 | static int list_of(State state) { return is_waiting(state) ? 1 : 0; } |
| 237 | // The index group[n] of a record in `state` is in. | ||
| 238 | static int index_of(State state, int n) { return is_texture(state) ? kTexture : n; } | ||
| 239 | // The room list a record in `state` is in, or -1. | ||
| 240 | static int room_of(State state) { return state == waiting_room ? 0 : state == texture_waiting_room ? 1 : -1; } | ||
| 188 | 241 | ||
| 189 | void count(State state, int delta) { | 242 | void count(State state, int delta) { |
| 190 | if (state != none) recorded_ += std::size_t(std::ptrdiff_t(delta)); | 243 | const std::size_t d = std::size_t(std::ptrdiff_t(delta)); |
| 191 | if (state == waiting) waiting_ += std::size_t(std::ptrdiff_t(delta)); | 244 | if (is_vsharp(state)) recorded_ += d; |
| 245 | if (state == waiting || state == waiting_room) waiting_ += d; | ||
| 246 | if (is_texture(state)) textures_count_ += d; | ||
| 247 | if (state == texture_waiting || state == texture_waiting_room) textures_waiting_ += d; | ||
| 248 | } | ||
| 249 | |||
| 250 | // Records `key` in `state` under ranges[0] (and ranges[1], a V#'s target); what is unchanged stays where it is. | ||
| 251 | void place(std::uint32_t key, State state, std::uint64_t base, const Range (&ranges)[2]) { | ||
| 252 | Half& h = halves_[key]; | ||
| 253 | const State old = State(h.state); | ||
| 254 | for (int n = 0; n < 2; ++n) { | ||
| 255 | const std::uint32_t group = h.group[n]; | ||
| 256 | const bool keep = group && list_of(old) == list_of(state) && index_of(old, n) == index_of(state, n) && | ||
| 257 | groups_[group - 1].start == ranges[n].start && groups_[group - 1].end == ranges[n].end; | ||
| 258 | if (keep) continue; | ||
| 259 | detach(n, key); // in the list of the old state, which h.state still holds | ||
| 260 | if (ranges[n].end) attach(n, index_of(state, n), key, ranges[n], state); | ||
| 261 | } | ||
| 262 | if (room_of(old) != room_of(state)) { | ||
| 263 | leave_room(key); | ||
| 264 | if (room_of(state) >= 0) { | ||
| 265 | auto& room = room_[room_of(state)]; | ||
| 266 | h.room = std::uint32_t(room.size()); | ||
| 267 | room.push_back(key); | ||
| 268 | } | ||
| 269 | } | ||
| 270 | count(old, -1); | ||
| 271 | h.state = state; | ||
| 272 | h.base = base; | ||
| 273 | count(state, +1); | ||
| 192 | } | 274 | } |
| 193 | 275 | ||
| 194 | void attach(int which, std::uint32_t key, Range r, State state) { | 276 | void drop(std::uint32_t key) { |
| 277 | Half& h = halves_[key]; | ||
| 278 | if (h.state == none) return; | ||
| 279 | for (int n = 0; n < 2; ++n) detach(n, key); | ||
| 280 | leave_room(key); | ||
| 281 | count(State(h.state), -1); | ||
| 282 | h.state = none; | ||
| 283 | h.base = 0; | ||
| 284 | } | ||
| 285 | |||
| 286 | void leave_room(std::uint32_t key) { | ||
| 287 | Half& h = halves_[key]; | ||
| 288 | const int which = room_of(State(h.state)); | ||
| 289 | if (which < 0) return; | ||
| 290 | auto& room = room_[which]; | ||
| 291 | const std::uint32_t last = room.back(); | ||
| 292 | room[h.room] = last; | ||
| 293 | halves_[last].room = h.room; | ||
| 294 | room.pop_back(); | ||
| 295 | } | ||
| 296 | |||
| 297 | void attach(int n, int which, std::uint32_t key, Range r, State state) { | ||
| 195 | Index& index = indexes_[which]; | 298 | Index& index = indexes_[which]; |
| 196 | auto [it, made] = index.by_range.try_emplace(r, 0); | 299 | auto [it, made] = index.by_range.try_emplace(r, 0); |
| 197 | if (made) { | 300 | if (made) { |
| @@ -206,6 +309,7 @@ private: | |||
| 206 | Group& g = groups_[id]; | 309 | Group& g = groups_[id]; |
| 207 | g.start = r.start; | 310 | g.start = r.start; |
| 208 | g.end = r.end; | 311 | g.end = r.end; |
| 312 | g.index = which; | ||
| 209 | g.cls = length_class(r.end - r.start); | 313 | g.cls = length_class(r.end - r.start); |
| 210 | g.at = index.by_start[g.cls].emplace(r.start, id); | 314 | g.at = index.by_start[g.cls].emplace(r.start, id); |
| 211 | index.used[g.cls >> 6] |= std::uint64_t(1) << (g.cls & 63); | 315 | index.used[g.cls >> 6] |= std::uint64_t(1) << (g.cls & 63); |
| @@ -214,24 +318,25 @@ private: | |||
| 214 | Group& g = groups_[it->second]; | 318 | Group& g = groups_[it->second]; |
| 215 | auto& list = g.keys[list_of(state)]; | 319 | auto& list = g.keys[list_of(state)]; |
| 216 | Half& h = halves_[key]; | 320 | Half& h = halves_[key]; |
| 217 | h.group[which] = it->second + 1; | 321 | h.group[n] = it->second + 1; |
| 218 | h.place[which] = std::uint32_t(list.size()); | 322 | h.place[n] = std::uint32_t(list.size()); |
| 219 | list.push_back(key); | 323 | list.push_back(key); |
| 220 | } | 324 | } |
| 221 | 325 | ||
| 222 | void detach(int which, std::uint32_t key) { | 326 | // Takes `key` out of its group[n], from the list of its current state. |
| 327 | void detach(int n, std::uint32_t key) { | ||
| 223 | Half& h = halves_[key]; | 328 | Half& h = halves_[key]; |
| 224 | if (!h.group[which]) return; | 329 | if (!h.group[n]) return; |
| 225 | const std::uint32_t id = h.group[which] - 1; | 330 | const std::uint32_t id = h.group[n] - 1; |
| 226 | Group& g = groups_[id]; | 331 | Group& g = groups_[id]; |
| 227 | auto& list = g.keys[list_of(State(h.state))]; | 332 | auto& list = g.keys[list_of(State(h.state))]; |
| 228 | const std::uint32_t place = h.place[which], last = list.back(); | 333 | const std::uint32_t at = h.place[n], last = list.back(); |
| 229 | list[place] = last; | 334 | list[at] = last; |
| 230 | halves_[last].place[which] = place; | 335 | halves_[last].place[n] = at; |
| 231 | list.pop_back(); | 336 | list.pop_back(); |
| 232 | h.group[which] = 0; | 337 | h.group[n] = 0; |
| 233 | if (!g.keys[0].empty() || !g.keys[1].empty()) return; | 338 | if (!g.keys[0].empty() || !g.keys[1].empty()) return; |
| 234 | Index& index = indexes_[which]; | 339 | Index& index = indexes_[g.index]; |
| 235 | index.by_range.erase(Range{g.start, g.end}); | 340 | index.by_range.erase(Range{g.start, g.end}); |
| 236 | index.by_start[g.cls].erase(g.at); | 341 | index.by_start[g.cls].erase(g.at); |
| 237 | if (index.by_start[g.cls].empty()) index.used[g.cls >> 6] &= ~(std::uint64_t(1) << (g.cls & 63)); | 342 | if (index.by_start[g.cls].empty()) index.used[g.cls >> 6] &= ~(std::uint64_t(1) << (g.cls & 63)); |
| @@ -263,10 +368,11 @@ private: | |||
| 263 | 368 | ||
| 264 | std::size_t slots_ = 0; | 369 | std::size_t slots_ = 0; |
| 265 | std::unique_ptr<Half[], Free> halves_; | 370 | std::unique_ptr<Half[], Free> halves_; |
| 266 | std::array<Index, 2> indexes_; | 371 | std::array<Index, 3> indexes_; |
| 372 | std::array<std::vector<std::uint32_t>, 2> room_; // 0: V# halves in waiting_room, 1: T#s in texture_waiting_room | ||
| 267 | std::vector<Group> groups_; | 373 | std::vector<Group> groups_; |
| 268 | std::vector<std::uint32_t> free_groups_; | 374 | std::vector<std::uint32_t> free_groups_; |
| 269 | std::size_t recorded_ = 0, waiting_ = 0; | 375 | std::size_t recorded_ = 0, waiting_ = 0, textures_count_ = 0, textures_waiting_ = 0; |
| 270 | std::uint64_t looked_at_ = 0, groups_looked_at_ = 0; | 376 | std::uint64_t looked_at_ = 0, groups_looked_at_ = 0; |
| 271 | }; | 377 | }; |
| 272 | 378 | ||
diff --git a/runtime/test/console_heap_slot_records_test.cpp b/runtime/test/console_heap_slot_records_test.cpp index 9600581da..14bdcb170 100644 --- a/runtime/test/console_heap_slot_records_test.cpp +++ b/runtime/test/console_heap_slot_records_test.cpp | |||
| @@ -336,6 +336,111 @@ int differential() { | |||
| 336 | return 0; | 336 | return 0; |
| 337 | } | 337 | } |
| 338 | 338 | ||
| 339 | // T# records (set_texture): a T# waits on the exact address it names; a texture registered there touches the slots | ||
| 340 | // waiting on it and only those; a texture gone touches the bound ones; a slot rewritten while waiting leaves no | ||
| 341 | // stale entry; many slots on one texture; V# and T# changes never touch each other's records. | ||
| 342 | int textures() { | ||
| 343 | using R = ConsoleHeapSlotRecords; | ||
| 344 | R records; | ||
| 345 | records.reset(70000); | ||
| 346 | constexpr std::uint64_t kTex = 0x100000100ull, kOther = 0x100000200ull, kNear = 0x100000300ull, | ||
| 347 | kRebound = 0x100000400ull; | ||
| 348 | std::vector<std::uint32_t> out; | ||
| 349 | const auto touched = [&](SlotChange::Kind kind, std::uint64_t address) { | ||
| 350 | const SlotChange change{kind, address, address + 1}; | ||
| 351 | records.touched(&change, 1, out); | ||
| 352 | return out; | ||
| 353 | }; | ||
| 354 | // 60,000 T#s wait on kTex, 10 on kOther, 5 are bound to kNear (each address another texture). | ||
| 355 | for (std::uint32_t slot = 0; slot < 60000; ++slot) records.set_texture(slot, R::texture_waiting, kTex); | ||
| 356 | for (std::uint32_t slot = 60000; slot < 60010; ++slot) records.set_texture(slot, R::texture_waiting, kOther); | ||
| 357 | for (std::uint32_t slot = 60010; slot < 60015; ++slot) records.set_texture(slot, R::texture_bound, kNear); | ||
| 358 | EXPECT(records.textures() == 60015 && records.textures_waiting() == 60010 && records.recorded() == 0); | ||
| 359 | // A texture at kOther: its 10 slots, looking at 10 entries. | ||
| 360 | std::uint64_t looked = records.looked_at(); | ||
| 361 | EXPECT(touched(SlotChange::texture_added, kOther).size() == 10 && out.front() == 60000 && out.back() == 60009); | ||
| 362 | EXPECT(records.looked_at() - looked == 10); | ||
| 363 | // A texture added where no T# waits, or where only bound ones are: nothing. | ||
| 364 | EXPECT(touched(SlotChange::texture_added, kOther + 0x1000).empty()); | ||
| 365 | EXPECT(touched(SlotChange::texture_added, kNear).empty()); | ||
| 366 | // V# changes over those addresses never touch T# records. | ||
| 367 | for (const auto kind : {SlotChange::target_gone, SlotChange::source_gone, SlotChange::target_added, | ||
| 368 | SlotChange::source_added}) { | ||
| 369 | const SlotChange change{kind, kTex - 0x10000, kTex + 0x10000}; | ||
| 370 | records.touched(&change, 1, out); | ||
| 371 | EXPECT(out.empty()); | ||
| 372 | } | ||
| 373 | // The texture at kNear goes: its bound slots, which the fix makes waiting. | ||
| 374 | EXPECT(touched(SlotChange::texture_gone, kNear).size() == 5); | ||
| 375 | for (std::uint32_t slot = 60010; slot < 60015; ++slot) records.set_texture(slot, R::texture_waiting, kNear); | ||
| 376 | EXPECT(records.textures_waiting() == 60015); | ||
| 377 | // Slot 7 is rewritten while waiting: now a T# of kOther, then a V#, then zeroed. Each time kTex no longer names it. | ||
| 378 | records.set_texture(7, R::texture_waiting, kOther); | ||
| 379 | EXPECT(touched(SlotChange::texture_added, kTex).size() == 59999); | ||
| 380 | EXPECT(touched(SlotChange::texture_added, kOther).size() == 11); | ||
| 381 | records.set(7, 0, R::resolved, 0x5000, 256, kArena + 0x5000); | ||
| 382 | EXPECT(touched(SlotChange::texture_added, kOther).size() == 10); | ||
| 383 | EXPECT(records.textures() == 60014 && records.recorded() == 1); | ||
| 384 | records.clear(7); | ||
| 385 | EXPECT(touched(SlotChange::texture_gone, kOther).size() == 10 && records.recorded() == 0); | ||
| 386 | // The texture comes: the fix binds all 59,999; a later texture added there touches none (they wait no more), a | ||
| 387 | // texture gone touches them all again. | ||
| 388 | for (std::uint32_t slot = 0; slot < 60000; ++slot) | ||
| 389 | if (slot != 7) records.set_texture(slot, R::texture_bound, kTex); | ||
| 390 | EXPECT(touched(SlotChange::texture_added, kTex).empty()); | ||
| 391 | EXPECT(touched(SlotChange::texture_gone, kTex).size() == 59999); | ||
| 392 | // Rebinding the same address many times leaves one entry per slot (the bucket is cleaned as it grows). | ||
| 393 | for (int round = 0; round < 8; ++round) | ||
| 394 | for (std::uint32_t slot = 0; slot < 100; ++slot) | ||
| 395 | records.set_texture(slot, round & 1 ? R::texture_bound : R::texture_waiting, kRebound); | ||
| 396 | looked = records.looked_at(); | ||
| 397 | EXPECT(touched(SlotChange::texture_gone, kRebound).size() == 100); | ||
| 398 | EXPECT(records.looked_at() - looked <= 200); | ||
| 399 | std::printf("textures: a texture registered touches only the T#s waiting on its address (10 of 60,015, 10 looked " | ||
| 400 | "at); one gone touches the bound ones; rewritten slots leave no stale entry; V# changes touch none\n"); | ||
| 401 | return 0; | ||
| 402 | } | ||
| 403 | |||
| 404 | // Waiting for room: a V# half refused a geometry copy for lack of room is fixed again when copies are freed | ||
| 405 | // (geometry_room) whatever its address; a T# refused for a full bindless array when entries are freed (texture_room). | ||
| 406 | // The A1 gap: such a half has no target, so no target change ever found it. | ||
| 407 | int room() { | ||
| 408 | using R = ConsoleHeapSlotRecords; | ||
| 409 | R records; | ||
| 410 | records.reset(64); | ||
| 411 | records.set(1, 0, R::waiting_room, 0x7000000000ull, 4096, 0); // geometry refused for room | ||
| 412 | records.set(2, 0, R::waiting, 0x7000100000ull, 4096, 0); // nothing at its base | ||
| 413 | records.set(3, 1, R::waiting_room, 0x7000200000ull, 64, 0); // the counter half | ||
| 414 | records.set_texture(4, R::texture_waiting_room, 0x100000400ull); | ||
| 415 | records.set_texture(5, R::texture_waiting, 0x100000500ull); | ||
| 416 | std::vector<std::uint32_t> out; | ||
| 417 | const SlotChange geometry{SlotChange::geometry_room, 0, 1}, textures{SlotChange::texture_room, 0, 1}; | ||
| 418 | records.touched(&geometry, 1, out); | ||
| 419 | EXPECT(out == std::vector<std::uint32_t>({1, 3})); | ||
| 420 | records.touched(&textures, 1, out); | ||
| 421 | EXPECT(out == std::vector<std::uint32_t>({4})); | ||
| 422 | // Both at once, each list once; duplicates of one change count once. | ||
| 423 | const SlotChange both[] = {geometry, textures, geometry}; | ||
| 424 | records.touched(both, 3, out); | ||
| 425 | EXPECT(out == std::vector<std::uint32_t>({1, 3, 4})); | ||
| 426 | // Resolved since (a copy was made): out of the list; waiting for an address alone: still found by the address. | ||
| 427 | records.set(1, 0, R::resolved, 0x7000000000ull, 4096, kArena + 0x10000); | ||
| 428 | records.set_texture(4, R::texture_waiting, 0x100000400ull); | ||
| 429 | records.touched(both, 3, out); | ||
| 430 | EXPECT(out == std::vector<std::uint32_t>({3})); | ||
| 431 | const SlotChange added{SlotChange::texture_added, 0x100000400ull, 0x100000401ull}; | ||
| 432 | records.touched(&added, 1, out); | ||
| 433 | EXPECT(out == std::vector<std::uint32_t>({4})); | ||
| 434 | // A room waiter is also found by its base, as any waiting half. | ||
| 435 | const SlotChange source{SlotChange::source_added, 0x7000200000ull, 0x7000200040ull}; | ||
| 436 | records.touched(&source, 1, out); | ||
| 437 | EXPECT(out == std::vector<std::uint32_t>({3})); | ||
| 438 | EXPECT(records.waiting_count() == 2 && records.textures_waiting() == 2); | ||
| 439 | std::printf("room: geometry room touches only the halves refused a copy (2 of 3 waiting), texture room only the " | ||
| 440 | "T#s refused for a full array; resolved ones leave the lists\n"); | ||
| 441 | return 0; | ||
| 442 | } | ||
| 443 | |||
| 339 | } // namespace | 444 | } // namespace |
| 340 | 445 | ||
| 341 | // Many slots in a few 64 KiB blocks, as the world's heaps have them (82,000-472,000 entries looked at for 2-15 | 446 | // Many slots in a few 64 KiB blocks, as the world's heaps have them (82,000-472,000 entries looked at for 2-15 |
| @@ -399,6 +504,8 @@ int main() { | |||
| 399 | if (cases()) return 1; | 504 | if (cases()) return 1; |
| 400 | if (dense_blocks_look_at_overlaps_only()) return 1; | 505 | if (dense_blocks_look_at_overlaps_only()) return 1; |
| 401 | if (differential()) return 1; | 506 | if (differential()) return 1; |
| 507 | if (textures()) return 1; | ||
| 508 | if (room()) return 1; | ||
| 402 | std::printf("console_heap_slot_records_test: ok\n"); | 509 | std::printf("console_heap_slot_records_test: ok\n"); |
| 403 | return 0; | 510 | return 0; |
| 404 | } | 511 | } |
diff --git a/runtime/test/d3d12_tsharp_refill_test.cpp b/runtime/test/d3d12_tsharp_refill_test.cpp new file mode 100644 index 000000000..8fd67f061 --- /dev/null +++ b/runtime/test/d3d12_tsharp_refill_test.cpp | |||
| @@ -0,0 +1,341 @@ | |||
| 1 | // A T# the title writes into a console descriptor heap (Xbox slot 14) before its texture exists is bound when the | ||
| 2 | // texture appears, at the next submission, through the heap's slot records (docs/d3d12_vulkan.md section 3.4): no | ||
| 3 | // pass looks at waiting T#s again on its own. Through D3D12 alone, on the Xbox-ABI proxy over the Vulkan device: | ||
| 4 | // - a probe texture gives the T# words of an 8x8 RGBA8 texture and the address the layer gives textures (the next | ||
| 5 | // one is 256 bytes on, Resource::image_address); | ||
| 6 | // - a console heap of 60,001 slots, written before the heap is made: 60,000 T#s naming addresses no texture will | ||
| 7 | // ever have, then, last, the T# of the texture to come. All wait after the heap's first refresh; | ||
| 8 | // - the texture is made at the address the T# names, filled by a copy, and sampled through that slot by the | ||
| 9 | // test/glsl/cs_set0_sample.comp shader (d3d12_set0_compute_test) in the same submission. Its texels must come | ||
| 10 | // back exactly. With a pass that tries waiting T#s round robin under a time budget, the last of 60,001 is not | ||
| 11 | // reached in that submission and the shader reads the fallback image; | ||
| 12 | // - the texture goes: its T# leaves the descriptor hash table at once, and the slot waits again. | ||
| 13 | // Set LEONIDA_VK_VALIDATION=1 to run under VK_LAYER_KHRONOS_validation (0 errors required). No game data: the program | ||
| 14 | // is two marker words and the module is GLSL written to the translator's contract. | ||
| 15 | #include "shader_fixture_path.h" | ||
| 16 | #include "test_temp_dir.h" | ||
| 17 | #include "vk_test_device.h" | ||
| 18 | #include "d3d12_test_shaders.h" | ||
| 19 | #include "../shims/d3d12_com.h" | ||
| 20 | #include "../shims/d3d12_xs_device.h" | ||
| 21 | #include "../src/shader_library.h" | ||
| 22 | |||
| 23 | #include <array> | ||
| 24 | #include <cstdint> | ||
| 25 | #include <cstring> | ||
| 26 | #include <filesystem> | ||
| 27 | #include <fstream> | ||
| 28 | #include <vector> | ||
| 29 | |||
| 30 | #ifndef LEONIDA_TEST_CS_SET0_SAMPLE_SPV | ||
| 31 | #error "LEONIDA_TEST_CS_SET0_SAMPLE_SPV must name the compiled test shader" | ||
| 32 | #endif | ||
| 33 | |||
| 34 | #define EXPECT(condition) do { \ | ||
| 35 | if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \ | ||
| 36 | } while (0) | ||
| 37 | |||
| 38 | using namespace d3d12test; | ||
| 39 | using XboxHeapHandleFn = std::uint64_t(WINAPI*)(void* heap); // heap slots 9/10: handle in RAX | ||
| 40 | |||
| 41 | namespace { | ||
| 42 | |||
| 43 | using Texel = std::array<std::uint8_t, 4>; | ||
| 44 | constexpr UINT texture_size = 8, texels = texture_size * texture_size, run_bytes = texels * 4; | ||
| 45 | constexpr UINT waiting = 60000; // T#s that never find a texture, ahead of the one that will | ||
| 46 | constexpr UINT kDescriptor = 32; | ||
| 47 | |||
| 48 | Texel pattern(UINT x, UINT y) { | ||
| 49 | return {std::uint8_t(200 - x * 20), std::uint8_t(30 + y * 25), std::uint8_t((x * 8 + y) * 3), 255}; | ||
| 50 | } | ||
| 51 | |||
| 52 | // The T# words `words` with their base address replaced (dword 0: address bits 8..39, dword 1 bits 0..7: 40..47). | ||
| 53 | std::array<std::uint32_t, 8> at_address(std::array<std::uint32_t, 8> words, std::uint64_t address) { | ||
| 54 | words[0] = std::uint32_t(address >> 8); | ||
| 55 | words[1] = (words[1] & ~0xffu) | (std::uint32_t(address >> 40) & 0xffu); | ||
| 56 | return words; | ||
| 57 | } | ||
| 58 | |||
| 59 | } // namespace | ||
| 60 | |||
| 61 | int main() { | ||
| 62 | vktest::Device d; | ||
| 63 | const auto device_status = d.initialize(); | ||
| 64 | if (device_status == vktest::DeviceStatus::unavailable) return 77; | ||
| 65 | if (device_status != vktest::DeviceStatus::ready) return 1; | ||
| 66 | if (!d.bindless) { | ||
| 67 | std::printf("SKIP: the device lacks the descriptor indexing features of the bindless set 0\n"); | ||
| 68 | return 77; | ||
| 69 | } | ||
| 70 | |||
| 71 | const auto program = fssm_program({0x43530003, 0xbf810000}); | ||
| 72 | const auto container = shader_container(program); | ||
| 73 | const testfiles::ScratchDir scratch("leonida_tsharp_refill_test"); | ||
| 74 | const auto& library = scratch.path(); | ||
| 75 | EXPECT(testfiles::copy_file(vktest::shader_fixture_path(LEONIDA_TEST_CS_SET0_SAMPLE_SPV), | ||
| 76 | library / "cs_set0_sample.spv")); | ||
| 77 | { | ||
| 78 | std::ofstream index(library / "index.json", std::ios::binary); | ||
| 79 | index << "{\"cs_set0_sample\": {\"stage\": \"cs\", \"fssm_sha256\": \"" << leonida::sha256_hex(program) | ||
| 80 | << "\", \"bindings\": [0, 1, 2], \"push_constant_bytes\": 144, \"local_size\": [64, 1, 1], " | ||
| 81 | "\"uses_code_base\": false, \"wave64\": false}}"; | ||
| 82 | } | ||
| 83 | |||
| 84 | std::vector<std::uint8_t> results(run_bytes); | ||
| 85 | std::array<std::uint32_t, 8> refill_words{}; | ||
| 86 | { | ||
| 87 | rt::d3d12::VulkanDeviceInfo info{}; | ||
| 88 | info.get_instance_proc = d.instance_proc; | ||
| 89 | info.instance = d.instance; | ||
| 90 | info.physical = d.physical; | ||
| 91 | info.device = d.device; | ||
| 92 | info.queue = d.queue; | ||
| 93 | info.queue_family = d.family; | ||
| 94 | info.buffer_device_address = true; | ||
| 95 | info.bindless = d.bindless; | ||
| 96 | info.bindless_partially_bound = d.partially_bound; | ||
| 97 | info.image_view_min_lod = d.image_view_min_lod; | ||
| 98 | info.shader_dir = library.string(); | ||
| 99 | ID3D12Device* inner = nullptr; | ||
| 100 | EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&inner)))); | ||
| 101 | Ref<ID3D12Device> device; | ||
| 102 | EXPECT(SUCCEEDED(rt::d3d12_xs::create_device_proxy(inner, __uuidof(ID3D12Device), | ||
| 103 | reinterpret_cast<void**>(&device.p), /*console_lists=*/false))); | ||
| 104 | rt::d3d12::set_device_outer(inner, device.p); | ||
| 105 | if (rt::d3d12::device_push_constants_need_fallback(inner)) { | ||
| 106 | std::printf("d3d12_tsharp_refill_test: skipped, the PSO needs 144 push-constant bytes and this GPU has %u\n", | ||
| 107 | rt::d3d12::device_max_push_constants_size(inner)); | ||
| 108 | return 77; | ||
| 109 | } | ||
| 110 | |||
| 111 | // Root signature and PSO as in d3d12_set0_compute_test: s4 the view's offset in dwords, s5 the first output | ||
| 112 | // element, s6:s7 the output, s8 the view table, s9 the sampler table. | ||
| 113 | D3D12_DESCRIPTOR_RANGE view_range{D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, 0, 0, 0}; | ||
| 114 | D3D12_DESCRIPTOR_RANGE sampler_range{D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER, 1, 0, 0, 0}; | ||
| 115 | D3D12_ROOT_PARAMETER params[4] = {}; | ||
| 116 | params[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; | ||
| 117 | params[0].Constants = {0, 0, 2}; | ||
| 118 | params[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_UAV; | ||
| 119 | params[1].Descriptor = {0, 0}; | ||
| 120 | params[2].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; | ||
| 121 | params[2].DescriptorTable = {1, &view_range}; | ||
| 122 | params[3].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; | ||
| 123 | params[3].DescriptorTable = {1, &sampler_range}; | ||
| 124 | D3D12_VERSIONED_ROOT_SIGNATURE_DESC root_desc{}; | ||
| 125 | root_desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_0; | ||
| 126 | root_desc.Desc_1_0 = {4, params, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_NONE}; | ||
| 127 | const auto root_blob = serialize_root_signature(root_desc); | ||
| 128 | EXPECT(!root_blob.empty()); | ||
| 129 | Ref<ID3D12RootSignature> root; | ||
| 130 | EXPECT(SUCCEEDED(device->CreateRootSignature(0, root_blob.data(), root_blob.size(), IID_PPV_ARGS(&root.p)))); | ||
| 131 | D3D12_COMPUTE_PIPELINE_STATE_DESC pso_desc{}; | ||
| 132 | pso_desc.pRootSignature = root.p; | ||
| 133 | pso_desc.CS = {container.data(), container.size()}; | ||
| 134 | Ref<ID3D12PipelineState> pso; | ||
| 135 | EXPECT(SUCCEEDED(device->CreateComputePipelineState(&pso_desc, IID_PPV_ARGS(&pso.p)))); | ||
| 136 | |||
| 137 | // Every buffer first: from here on the only textures made are the probe and the one to come. | ||
| 138 | const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT}; | ||
| 139 | const D3D12_HEAP_PROPERTIES upload_heap{D3D12_HEAP_TYPE_UPLOAD}; | ||
| 140 | const D3D12_HEAP_PROPERTIES readback_heap{D3D12_HEAP_TYPE_READBACK}; | ||
| 141 | Ref<ID3D12Resource> upload, output, readback; | ||
| 142 | const auto upload_desc = buffer_desc(4096); | ||
| 143 | EXPECT(SUCCEEDED(device->CreateCommittedResource(&upload_heap, D3D12_HEAP_FLAG_NONE, &upload_desc, | ||
| 144 | D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, | ||
| 145 | IID_PPV_ARGS(&upload.p)))); | ||
| 146 | const auto output_desc = buffer_desc(results.size(), D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS); | ||
| 147 | EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &output_desc, | ||
| 148 | D3D12_RESOURCE_STATE_UNORDERED_ACCESS, nullptr, | ||
| 149 | IID_PPV_ARGS(&output.p)))); | ||
| 150 | const auto readback_desc = buffer_desc(results.size()); | ||
| 151 | EXPECT(SUCCEEDED(device->CreateCommittedResource(&readback_heap, D3D12_HEAP_FLAG_NONE, &readback_desc, | ||
| 152 | D3D12_RESOURCE_STATE_COPY_DEST, nullptr, | ||
| 153 | IID_PPV_ARGS(&readback.p)))); | ||
| 154 | const D3D12_DESCRIPTOR_HEAP_DESC view_heap_desc{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 1, | ||
| 155 | D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 0}; | ||
| 156 | const D3D12_DESCRIPTOR_HEAP_DESC sampler_heap_desc{D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, 1, | ||
| 157 | D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 0}; | ||
| 158 | Ref<ID3D12DescriptorHeap> view_heap, sampler_heap; | ||
| 159 | EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&view_heap_desc, IID_PPV_ARGS(&view_heap.p)))); | ||
| 160 | EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&sampler_heap_desc, IID_PPV_ARGS(&sampler_heap.p)))); | ||
| 161 | const std::uint64_t view_cpu = reinterpret_cast<XboxHeapHandleFn>(slot(view_heap.p, 9))(view_heap.p); | ||
| 162 | const std::uint64_t sampler_cpu = reinterpret_cast<XboxHeapHandleFn>(slot(sampler_heap.p, 9))(sampler_heap.p); | ||
| 163 | const std::uint64_t sampler_gpu = reinterpret_cast<XboxHeapHandleFn>(slot(sampler_heap.p, 10))(sampler_heap.p); | ||
| 164 | EXPECT(view_cpu && sampler_cpu && sampler_gpu < (1ull << 32)); | ||
| 165 | D3D12_SAMPLER_DESC sampler_desc{}; | ||
| 166 | sampler_desc.Filter = D3D12_FILTER_MIN_MAG_MIP_POINT; | ||
| 167 | sampler_desc.AddressU = sampler_desc.AddressV = sampler_desc.AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP; | ||
| 168 | sampler_desc.MaxAnisotropy = 1; | ||
| 169 | sampler_desc.ComparisonFunc = D3D12_COMPARISON_FUNC_NEVER; | ||
| 170 | sampler_desc.MaxLOD = D3D12_FLOAT32_MAX; | ||
| 171 | device->CreateSampler(&sampler_desc, D3D12_CPU_DESCRIPTOR_HANDLE{SIZE_T(sampler_cpu)}); | ||
| 172 | |||
| 173 | // The probe: the T# words of an 8x8 RGBA8 texture, and where the layer put it. | ||
| 174 | D3D12_RESOURCE_DESC texture_desc{}; | ||
| 175 | texture_desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; | ||
| 176 | texture_desc.Width = texture_desc.Height = texture_size; | ||
| 177 | texture_desc.DepthOrArraySize = 1; | ||
| 178 | texture_desc.MipLevels = 1; | ||
| 179 | texture_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; | ||
| 180 | texture_desc.SampleDesc.Count = 1; | ||
| 181 | Ref<ID3D12Resource> probe; | ||
| 182 | EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &texture_desc, | ||
| 183 | D3D12_RESOURCE_STATE_COMMON, nullptr, | ||
| 184 | IID_PPV_ARGS(&probe.p)))); | ||
| 185 | D3D12_SHADER_RESOURCE_VIEW_DESC srv{}; | ||
| 186 | srv.Format = DXGI_FORMAT_R8G8B8A8_UNORM; | ||
| 187 | srv.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; | ||
| 188 | srv.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; | ||
| 189 | srv.Texture2D = {0, 1, 0, 0.0f}; | ||
| 190 | device->CreateShaderResourceView(probe.p, &srv, D3D12_CPU_DESCRIPTOR_HANDLE{SIZE_T(view_cpu)}); | ||
| 191 | std::array<std::uint32_t, 8> probe_words{}; | ||
| 192 | std::memcpy(probe_words.data(), reinterpret_cast<const void*>(view_cpu), sizeof(probe_words)); | ||
| 193 | const std::uint64_t probe_address = rt::d3d12::resource_image_address(probe.p); | ||
| 194 | EXPECT(probe_address && (probe_words[0] == std::uint32_t(probe_address >> 8))); | ||
| 195 | const std::uint64_t refill_address = probe_address + 256; | ||
| 196 | refill_words = at_address(probe_words, refill_address); | ||
| 197 | |||
| 198 | // The console heap, written before it is made: 60,000 T#s at addresses no texture will have (a range of the | ||
| 199 | // address space the layer never gives textures, 256 apart), then the one to come, last. | ||
| 200 | constexpr UINT slots = waiting + 1; | ||
| 201 | constexpr std::uint64_t bytes = std::uint64_t(slots) * kDescriptor; | ||
| 202 | auto* title = static_cast<std::uint8_t*>(VirtualAlloc(nullptr, bytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); | ||
| 203 | EXPECT(title); | ||
| 204 | for (UINT i = 0; i < waiting; ++i) { | ||
| 205 | const auto words = at_address(probe_words, 0x7e0000000000ull + std::uint64_t(i) * 256); | ||
| 206 | std::memcpy(title + std::size_t(i) * kDescriptor, words.data(), sizeof(words)); | ||
| 207 | } | ||
| 208 | std::memcpy(title + std::size_t(waiting) * kDescriptor, refill_words.data(), sizeof(refill_words)); | ||
| 209 | const D3D12_DESCRIPTOR_HEAP_DESC console_desc{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, slots, | ||
| 210 | D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 0}; | ||
| 211 | Ref<ID3D12DescriptorHeap> console_heap; | ||
| 212 | EXPECT(SUCCEEDED(rt::d3d12::create_console_descriptor_heap(device.p, &console_desc, | ||
| 213 | reinterpret_cast<std::uint64_t>(title), bytes, | ||
| 214 | IID_PPV_ARGS(&console_heap.p)))); | ||
| 215 | D3D12_DESCRIPTOR_HEAP_DESC got{}; | ||
| 216 | D3D12_GPU_VIRTUAL_ADDRESS shader_va = 0; | ||
| 217 | EXPECT(rt::d3d12::descriptor_heap_info(console_heap.p, got, shader_va)); | ||
| 218 | const std::uint64_t refill_slot_va = shader_va + std::uint64_t(waiting) * kDescriptor; | ||
| 219 | EXPECT(refill_slot_va < (1ull << 32)); | ||
| 220 | std::uint32_t key[8]; | ||
| 221 | std::memcpy(key, refill_words.data(), sizeof(key)); | ||
| 222 | EXPECT(rt::d3d12::bindless_hash_lookup(device.p, key) == 0); // no texture yet: the slot waits | ||
| 223 | |||
| 224 | D3D12_COMMAND_QUEUE_DESC queue_desc{}; | ||
| 225 | Ref<ID3D12CommandQueue> queue; | ||
| 226 | Ref<ID3D12CommandAllocator> allocator; | ||
| 227 | Ref<ID3D12GraphicsCommandList> list; | ||
| 228 | Ref<ID3D12Fence> fence; | ||
| 229 | EXPECT(SUCCEEDED(device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&queue.p)))); | ||
| 230 | EXPECT(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p)))); | ||
| 231 | EXPECT(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr, | ||
| 232 | IID_PPV_ARGS(&list.p)))); | ||
| 233 | EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p)))); | ||
| 234 | // One empty submission: the waiting T#s have had a refresh with nothing new (as a running title's would). | ||
| 235 | EXPECT(list->Close() == S_OK); | ||
| 236 | ID3D12CommandList* lists[] = {list.p}; | ||
| 237 | queue->ExecuteCommandLists(1, lists); | ||
| 238 | EXPECT(SUCCEEDED(queue->Signal(fence.p, 1))); | ||
| 239 | EXPECT(SUCCEEDED(fence->SetEventOnCompletion(1, nullptr))); | ||
| 240 | EXPECT(rt::d3d12::bindless_hash_lookup(device.p, key) == 0); | ||
| 241 | |||
| 242 | // The texture comes, at the address the T# names. | ||
| 243 | Ref<ID3D12Resource> texture; | ||
| 244 | EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &texture_desc, | ||
| 245 | D3D12_RESOURCE_STATE_COPY_DEST, nullptr, | ||
| 246 | IID_PPV_ARGS(&texture.p)))); | ||
| 247 | EXPECT(rt::d3d12::resource_image_address(texture.p) == refill_address); | ||
| 248 | D3D12_PLACED_SUBRESOURCE_FOOTPRINT footprint{}; | ||
| 249 | device->GetCopyableFootprints(&texture_desc, 0, 1, 0, &footprint, nullptr, nullptr, nullptr); | ||
| 250 | EXPECT(footprint.Footprint.RowPitch * texture_size <= 4096); | ||
| 251 | std::uint8_t* up = nullptr; | ||
| 252 | EXPECT(SUCCEEDED(upload->Map(0, nullptr, reinterpret_cast<void**>(&up)))); | ||
| 253 | for (UINT y = 0; y < texture_size; ++y) | ||
| 254 | for (UINT x = 0; x < texture_size; ++x) | ||
| 255 | std::memcpy(up + y * footprint.Footprint.RowPitch + x * 4, pattern(x, y).data(), 4); | ||
| 256 | |||
| 257 | // One submission: fill the texture and sample it through the waiting slot. | ||
| 258 | EXPECT(SUCCEEDED(allocator->Reset())); | ||
| 259 | EXPECT(SUCCEEDED(list->Reset(allocator.p, nullptr))); | ||
| 260 | D3D12_TEXTURE_COPY_LOCATION dst{}; | ||
| 261 | dst.pResource = texture.p; | ||
| 262 | dst.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; | ||
| 263 | D3D12_TEXTURE_COPY_LOCATION src{}; | ||
| 264 | src.pResource = upload.p; | ||
| 265 | src.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; | ||
| 266 | src.PlacedFootprint = footprint; | ||
| 267 | list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr); | ||
| 268 | D3D12_RESOURCE_BARRIER sampled = | ||
| 269 | transition(texture.p, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE); | ||
| 270 | list->ResourceBarrier(1, &sampled); | ||
| 271 | ID3D12DescriptorHeap* heaps[] = {view_heap.p, sampler_heap.p}; | ||
| 272 | list->SetDescriptorHeaps(2, heaps); | ||
| 273 | list->SetComputeRootSignature(root.p); | ||
| 274 | list->SetPipelineState(pso.p); | ||
| 275 | const D3D12_GPU_VIRTUAL_ADDRESS output_va = output->GetGPUVirtualAddress(); | ||
| 276 | EXPECT(output_va < (1ull << 32)); | ||
| 277 | list->SetComputeRootUnorderedAccessView(1, output_va); | ||
| 278 | list->SetComputeRootDescriptorTable(2, D3D12_GPU_DESCRIPTOR_HANDLE{refill_slot_va}); | ||
| 279 | list->SetComputeRootDescriptorTable(3, D3D12_GPU_DESCRIPTOR_HANDLE{sampler_gpu}); | ||
| 280 | const std::uint32_t offset_and_first[2] = {0, 0}; | ||
| 281 | list->SetComputeRoot32BitConstants(0, 2, offset_and_first, 0); | ||
| 282 | list->Dispatch(1, 1, 1); | ||
| 283 | D3D12_RESOURCE_BARRIER to_copy = transition(output.p, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, | ||
| 284 | D3D12_RESOURCE_STATE_COPY_SOURCE); | ||
| 285 | list->ResourceBarrier(1, &to_copy); | ||
| 286 | list->CopyBufferRegion(readback.p, 0, output.p, 0, results.size()); | ||
| 287 | EXPECT(list->Close() == S_OK); | ||
| 288 | queue->ExecuteCommandLists(1, lists); | ||
| 289 | EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success); | ||
| 290 | EXPECT(SUCCEEDED(queue->Signal(fence.p, 2))); | ||
| 291 | EXPECT(SUCCEEDED(fence->SetEventOnCompletion(2, nullptr))); | ||
| 292 | void* mapped = nullptr; | ||
| 293 | EXPECT(SUCCEEDED(readback->Map(0, nullptr, &mapped))); | ||
| 294 | std::memcpy(results.data(), mapped, results.size()); | ||
| 295 | EXPECT(rt::d3d12::bindless_hash_lookup(device.p, key) != 0); | ||
| 296 | |||
| 297 | // The texture goes: its T# leaves the hash table at once (a shader reading the slot gets the fallback), and | ||
| 298 | // the next submission takes the slot's record back to waiting. | ||
| 299 | texture.reset(); | ||
| 300 | EXPECT(rt::d3d12::bindless_hash_lookup(device.p, key) == 0); | ||
| 301 | EXPECT(SUCCEEDED(allocator->Reset())); | ||
| 302 | EXPECT(SUCCEEDED(list->Reset(allocator.p, nullptr))); | ||
| 303 | EXPECT(list->Close() == S_OK); | ||
| 304 | queue->ExecuteCommandLists(1, lists); | ||
| 305 | EXPECT(SUCCEEDED(queue->Signal(fence.p, 3))); | ||
| 306 | EXPECT(SUCCEEDED(fence->SetEventOnCompletion(3, nullptr))); | ||
| 307 | EXPECT(rt::d3d12::bindless_hash_lookup(device.p, key) == 0); | ||
| 308 | |||
| 309 | for (auto* r : {&probe, &upload, &output, &readback}) r->reset(); | ||
| 310 | pso.reset(); | ||
| 311 | root.reset(); | ||
| 312 | view_heap.reset(); | ||
| 313 | sampler_heap.reset(); | ||
| 314 | console_heap.reset(); | ||
| 315 | list.reset(); | ||
| 316 | allocator.reset(); | ||
| 317 | queue.reset(); | ||
| 318 | fence.reset(); | ||
| 319 | VirtualFree(title, 0, MEM_RELEASE); | ||
| 320 | EXPECT(rt::d3d12_xs::device_proxy_ref_count(device.p) == 1); | ||
| 321 | } | ||
| 322 | |||
| 323 | unsigned mismatches = 0; | ||
| 324 | for (UINT i = 0; i < texels; ++i) { | ||
| 325 | const UINT x = i % texture_size, y = i / texture_size; | ||
| 326 | const Texel want = pattern(x, y); | ||
| 327 | const std::uint8_t* got = results.data() + i * 4; | ||
| 328 | if (!std::memcmp(got, want.data(), 4)) continue; | ||
| 329 | if (mismatches++ < 8) | ||
| 330 | std::fprintf(stderr, "texel %u: got %u,%u,%u,%u want %u,%u,%u,%u\n", i, got[0], got[1], got[2], got[3], | ||
| 331 | want[0], want[1], want[2], want[3]); | ||
| 332 | } | ||
| 333 | EXPECT(!mismatches); | ||
| 334 | if (d.validation_errors()) { | ||
| 335 | std::fprintf(stderr, "%u validation errors\n", d.validation_errors()); | ||
| 336 | return 1; | ||
| 337 | } | ||
| 338 | std::printf("T# refill: the last of %u waiting T#s sampled its texture, all %u texels, at the first submission " | ||
| 339 | "after the texture appeared; its T# left the hash table when the texture went\n", waiting + 1, texels); | ||
| 340 | return 0; | ||
| 341 | } | ||
diff --git a/runtime/test/d3d12_unbound_tsharp_rules_test.cpp b/runtime/test/d3d12_unbound_tsharp_rules_test.cpp index bc9aa1ca9..d7a6f8d93 100644 --- a/runtime/test/d3d12_unbound_tsharp_rules_test.cpp +++ b/runtime/test/d3d12_unbound_tsharp_rules_test.cpp | |||
| @@ -374,8 +374,55 @@ int main() { | |||
| 374 | const char* why = nullptr; | 374 | const char* why = nullptr; |
| 375 | const auto result = rt::d3d12::test_bind_title_image(device.p, words, &why); | 375 | const auto result = rt::d3d12::test_bind_title_image(device.p, words, &why); |
| 376 | check(result == rt::d3d12::TitleImage::unknown, "unknown address returns TitleImage::unknown"); | 376 | check(result == rt::d3d12::TitleImage::unknown, "unknown address returns TitleImage::unknown"); |
| 377 | check(why && std::string(why) == "no texture at T# address", | 377 | check(why && std::string(why) == "no texture at the T# address: no title memory and no texture of ours", |
| 378 | "unknown address explains no texture at T# address"); | 378 | "unknown address explains no texture at the T# address, and what the address is"); |
| 379 | } | ||
| 380 | |||
| 381 | // The classes of an address with no texture: one of ours that is gone, and one whose low 32 bits name one of | ||
| 382 | // ours (of the T#'s shape, of another, or gone) with other high bits. | ||
| 383 | { | ||
| 384 | D3D12_RESOURCE_DESC desc{}; | ||
| 385 | desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; | ||
| 386 | desc.Width = 16; | ||
| 387 | desc.Height = 4; | ||
| 388 | desc.DepthOrArraySize = 1; | ||
| 389 | desc.MipLevels = 1; | ||
| 390 | desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; | ||
| 391 | desc.SampleDesc.Count = 1; | ||
| 392 | Ref<ID3D12Resource> texture; | ||
| 393 | check(SUCCEEDED(device->CreateCommittedResource(&heap_props, D3D12_HEAP_FLAG_NONE, &desc, | ||
| 394 | D3D12_RESOURCE_STATE_COMMON, nullptr, | ||
| 395 | IID_PPV_ARGS(texture.put()))), | ||
| 396 | "created a 16x4 texture"); | ||
| 397 | const std::uint64_t addr = rt::d3d12::resource_image_address(texture.p); | ||
| 398 | check(addr >> 32 == 1, "the layer's texture addresses start at 1 << 32"); | ||
| 399 | g::Image img{}; | ||
| 400 | img.address = 0x7f0000000000ull | (addr & 0xffffffffull); | ||
| 401 | img.type = g::ImageType::image_2d; | ||
| 402 | img.format = g::format_from_vulkan(VK_FORMAT_R8G8B8A8_UNORM); | ||
| 403 | img.width = 16; | ||
| 404 | img.height = 4; | ||
| 405 | img.layer_count = 1; | ||
| 406 | img.mip_count = 1; | ||
| 407 | const auto reason = [&](const g::Image& image) { | ||
| 408 | const char* why = nullptr; | ||
| 409 | const auto result = rt::d3d12::test_bind_title_image(device.p, g::encode(image), &why); | ||
| 410 | return result == rt::d3d12::TitleImage::unknown && why ? std::string(why) : std::string("?"); | ||
| 411 | }; | ||
| 412 | check(reason(img) == "no texture at the T# address: its low 32 bits name a texture of ours of its shape", | ||
| 413 | "other high bits, same shape"); | ||
| 414 | g::Image other = img; | ||
| 415 | other.width = 32; | ||
| 416 | check(reason(other) == | ||
| 417 | "no texture at the T# address: its low 32 bits name a texture of ours of another shape", | ||
| 418 | "other high bits, another shape"); | ||
| 419 | texture.put(); // released: its address leaves the device's table | ||
| 420 | check(reason(img) == "no texture at the T# address: its low 32 bits name a texture of ours that is gone", | ||
| 421 | "other high bits, texture gone"); | ||
| 422 | g::Image gone = img; | ||
| 423 | gone.address = addr; | ||
| 424 | check(reason(gone) == "no texture at the T# address: a texture of ours that is gone", | ||
| 425 | "the address of a texture of ours that is gone"); | ||
| 379 | } | 426 | } |
| 380 | 427 | ||
| 381 | // Not a valid T# (decode failure) | 428 | // Not a valid T# (decode failure) |