diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-27 06:39:35 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-27 06:39:35 -0500 |
| commit | b0712ff2ce72796d0db2d833ec3d5d0a542e2142 (patch) | |
| tree | 2553007a564e692af4aff036e43c98a1b4b31c06 /runtime | |
| parent | 23c33411962b84ecae28f4e82bd62fc6fba79444 (diff) | |
| parent | 3c9820f8daae7e478cdca7f7e1d395e60d7c4ddf (diff) | |
Merge test/round2 into heaps-in-place: per-piece sums and the T# index word for heaps read in place
title_mirror.h: a filtered page's sums per 256-byte piece are of the title's bytes (title compared with title); the
filter runs after that compare, only when a piece changed or the home is fresh, and writes the home. write_back skips
a filtered home, so page_mirror_write_back takes TitleMirror::write_back as is. A home leaving its filter is copied
whole once.
The T# index word (tsharp_index_word) is written by finish_in_place into the home's dword 6 once the T# is bound; the
filter leaves 0 (the fallback) there meanwhile, so the title's own dword 6 is never read as an index. Copy-path heaps
keep fix_console_slot's write. d3d12_heap_in_place_test checks a T# slot reads the tag and index while the title's
bytes keep their dword 6; title_mirror_hooks_test checks write-back skips filtered homes and the filter is not called
again for unchanged title bytes. set_tsharp_index_words_for_test for the test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/CMakeLists.txt | 6 | ||||
| -rw-r--r-- | runtime/README.md | 2 | ||||
| -rw-r--r-- | runtime/cmake/translator_deps.cmake | 2 | ||||
| -rw-r--r-- | runtime/shims/d3d12_com.h | 6 | ||||
| -rw-r--r-- | runtime/shims/d3d12_vulkan_device.cpp | 238 | ||||
| -rw-r--r-- | runtime/shims/xmem.cpp | 79 | ||||
| -rw-r--r-- | runtime/src/gfx10_descriptors.h | 39 | ||||
| -rw-r--r-- | runtime/src/shader_library.h | 23 | ||||
| -rw-r--r-- | runtime/src/title_heap.h | 207 | ||||
| -rw-r--r-- | runtime/src/title_mirror.h | 114 | ||||
| -rw-r--r-- | runtime/test/d3d12_heap_in_place_test.cpp | 50 | ||||
| -rw-r--r-- | runtime/test/d3d12_page_mirror_test.cpp | 40 | ||||
| -rw-r--r-- | runtime/test/gfx10_descriptors_test.cpp | 20 | ||||
| -rw-r--r-- | runtime/test/shader_library_test.cpp | 26 | ||||
| -rw-r--r-- | runtime/test/shaders/ps_table_fetch.asm | 3 | ||||
| -rw-r--r-- | runtime/test/shaders/ps_texture_sample.asm | 5 | ||||
| -rw-r--r-- | runtime/test/title_heap_test.cpp | 130 | ||||
| -rw-r--r-- | runtime/test/title_mirror_hooks_test.cpp | 16 | ||||
| -rw-r--r-- | runtime/test/vk_texture_test.cpp | 18 |
19 files changed, 938 insertions, 86 deletions
diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 7b2c77300..d1a07073d 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt | |||
| @@ -4918,6 +4918,12 @@ if(TARGET Vulkan::Headers) | |||
| 4918 | target_compile_options(title_mirror_hooks_test PRIVATE /W4 /permissive- /utf-8) | 4918 | target_compile_options(title_mirror_hooks_test PRIVATE /W4 /permissive- /utf-8) |
| 4919 | endif() | 4919 | endif() |
| 4920 | add_test(NAME title_mirror_hooks COMMAND title_mirror_hooks_test) | 4920 | add_test(NAME title_mirror_hooks COMMAND title_mirror_hooks_test) |
| 4921 | # The XMemAlloc heap in the page mirror's range (src/title_heap.h). | ||
| 4922 | add_executable(title_heap_test test/title_heap_test.cpp) | ||
| 4923 | if(WIN32 AND MSVC) | ||
| 4924 | target_compile_options(title_heap_test PRIVATE /W4 /permissive- /utf-8) | ||
| 4925 | endif() | ||
| 4926 | add_test(NAME title_heap COMMAND title_heap_test) | ||
| 4921 | # Queue construction cleans its Vulkan handles if holder registration runs out of host memory. | 4927 | # Queue construction cleans its Vulkan handles if holder registration runs out of host memory. |
| 4922 | # Which title textures the refresh looks at: only those something says changed (src/title_texture_watch.h). | 4928 | # Which title textures the refresh looks at: only those something says changed (src/title_texture_watch.h). |
| 4923 | add_executable(title_texture_watch_test test/title_texture_watch_test.cpp) | 4929 | add_executable(title_texture_watch_test test/title_texture_watch_test.cpp) |
diff --git a/runtime/README.md b/runtime/README.md index 62321a76f..1a9fcd14d 100644 --- a/runtime/README.md +++ b/runtime/README.md | |||
| @@ -414,7 +414,7 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w | |||
| 414 | | `LEONIDA_COMPILE_GATE` | unset | `1`: a compute pipeline build holds the device's submit lock for its driver calls, so no compute compile runs while a queue submits. Unset: the asynchronous workers compile while other queues submit. | | 414 | | `LEONIDA_COMPILE_GATE` | unset | `1`: a compute pipeline build holds the device's submit lock for its driver calls, so no compute compile runs while a queue submits. Unset: the asynchronous workers compile while other queues submit. | |
| 415 | | `LEONIDA_FAST_PIPELINES` | unset | `graphics`, `compute` or `all` (`1` = all) creates those pipelines with `VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT` (`shims/d3d12_pipeline_compile.h`): slower shaders, much shorter builds. Part of the compute journal key, so optimized and fast builds keep separate files. | | 415 | | `LEONIDA_FAST_PIPELINES` | unset | `graphics`, `compute` or `all` (`1` = all) creates those pipelines with `VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT` (`shims/d3d12_pipeline_compile.h`): slower shaders, much shorter builds. Part of the compute journal key, so optimized and fast builds keep separate files. | |
| 416 | | `LEONIDA_GRAPHICS_COMPILE_WORKER` | on | A second compile worker that takes graphics pipeline builds only, so a long compute build does not hold them up (`shims/d3d12_pipeline_compile.h`). `0`: the one general worker builds everything. | | 416 | | `LEONIDA_GRAPHICS_COMPILE_WORKER` | on | A second compile worker that takes graphics pipeline builds only, so a long compute build does not hold them up (`shims/d3d12_pipeline_compile.h`). `0`: the one general worker builds everything. | |
| 417 | | `LEONIDA_CONSOLE_HEAP_GPU` | unset | `1`: the V#s of the console descriptor heaps are rebased by a compute pass at the start of each submission (`src/console_heap_gpu.h`) instead of on the CPU. The CPU still finds the changed slots and registers the T#s; a V# the pass cannot answer is resolved after its submission and its slot queued again. | | 417 | | `LEONIDA_CONSOLE_HEAP_GPU` | unset | `1`: the V#s of the console descriptor heaps are rebased by a compute pass at the start of each submission (`src/console_heap_gpu.h`) instead of on the CPU. The CPU still finds the changed slots and registers the T#s; a V# the pass cannot answer is resolved after its submission and its slot queued again. The pass copies T#s as the title wrote them and does not write the bindless index word into their dword 6 (docs/design.md, "Bindless index in the T#"), so with a shader pack whose modules read that word (index.json `"tsharp_index": "dword6"`, every pack translated since) the setting does not apply to resource heaps: they are fixed on the CPU as without it, and the log says so once (`LEONIDA_CONSOLE_HEAP_GPU: the pass does not write the T# index word ...`). An older pack still gets the pass. | |
| 418 | | `LEONIDA_CONSOLE_HEAP_ARENA` | unset | With `LEONIDA_CONSOLE_HEAP_GPU=1`, `1` puts a console heap's GPU copy and its guard in the arena (device-local where the device has ReBAR) instead of an import of host memory. No room in the arena: the import. | | 418 | | `LEONIDA_CONSOLE_HEAP_ARENA` | unset | With `LEONIDA_CONSOLE_HEAP_GPU=1`, `1` puts a console heap's GPU copy and its guard in the arena (device-local where the device has ReBAR) instead of an import of host memory. No room in the arena: the import. | |
| 419 | | `LEONIDA_CONSOLE_GPU_TSHARPS` | on | With `LEONIDA_CONSOLE_HEAP_GPU=1`, the T#s of changed slots are registered by the resolver thread and the refresh only decodes them. `0` registers them on the submitting thread, under the fix budget. | | 419 | | `LEONIDA_CONSOLE_GPU_TSHARPS` | on | With `LEONIDA_CONSOLE_HEAP_GPU=1`, the T#s of changed slots are registered by the resolver thread and the refresh only decodes them. `0` registers them on the submitting thread, under the fix budget. | |
| 420 | | `LEONIDA_CONSOLE_GPU_RETRY_MS` | `2000` | With `LEONIDA_CONSOLE_HEAP_GPU=1`, how often V# words that got no backing are asked for again, on a timer that bounds the cost per submission. | | 420 | | `LEONIDA_CONSOLE_GPU_RETRY_MS` | `2000` | With `LEONIDA_CONSOLE_HEAP_GPU=1`, how often V# words that got no backing are asked for again, on a timer that bounds the cost per submission. | |
diff --git a/runtime/cmake/translator_deps.cmake b/runtime/cmake/translator_deps.cmake index 0cb1ecdf8..2f0d42ec9 100644 --- a/runtime/cmake/translator_deps.cmake +++ b/runtime/cmake/translator_deps.cmake | |||
| @@ -10,10 +10,12 @@ set(LEONIDA_TRANSLATOR_DEPS | |||
| 10 | ${_leonida_repo}/runtime/test/shaders/compute_fixture.py | 10 | ${_leonida_repo}/runtime/test/shaders/compute_fixture.py |
| 11 | ${_leonida_repo}/tools/bary_fallback_gs.py | 11 | ${_leonida_repo}/tools/bary_fallback_gs.py |
| 12 | ${_leonida_repo}/tools/cfg_structure.py | 12 | ${_leonida_repo}/tools/cfg_structure.py |
| 13 | ${_leonida_repo}/tools/descriptor_sources.py | ||
| 13 | ${_leonida_repo}/tools/file_mode.py | 14 | ${_leonida_repo}/tools/file_mode.py |
| 14 | ${_leonida_repo}/tools/fssm_header.py | 15 | ${_leonida_repo}/tools/fssm_header.py |
| 15 | ${_leonida_repo}/tools/fssm_unpack.py | 16 | ${_leonida_repo}/tools/fssm_unpack.py |
| 16 | ${_leonida_repo}/tools/gfx10_formats.json | 17 | ${_leonida_repo}/tools/gfx10_formats.json |
| 18 | ${_leonida_repo}/tools/heap_index_census.py | ||
| 17 | ${_leonida_repo}/tools/lane_masks.py | 19 | ${_leonida_repo}/tools/lane_masks.py |
| 18 | ${_leonida_repo}/tools/mc_command.py | 20 | ${_leonida_repo}/tools/mc_command.py |
| 19 | ${_leonida_repo}/tools/native_pso_hashes.py | 21 | ${_leonida_repo}/tools/native_pso_hashes.py |
diff --git a/runtime/shims/d3d12_com.h b/runtime/shims/d3d12_com.h index 63c615ec4..b2926aadf 100644 --- a/runtime/shims/d3d12_com.h +++ b/runtime/shims/d3d12_com.h | |||
| @@ -428,6 +428,9 @@ bool page_mirror_gpu_read(ID3D12Device* device, std::uint64_t title_va, void* ou | |||
| 428 | // The same at arena VA `va` (heap_base + va): what a shader reads there, a console heap read in place among it. False | 428 | // The same at arena VA `va` (heap_base + va): what a shader reads there, a console heap read in place among it. False |
| 429 | // when the mirror is off. For tests. | 429 | // when the mirror is off. For tests. |
| 430 | bool arena_gpu_read(ID3D12Device* device, std::uint64_t va, void* out, std::size_t bytes); | 430 | bool arena_gpu_read(ID3D12Device* device, std::uint64_t va, void* out, std::size_t bytes); |
| 431 | // Whether the layer writes the T# index word (gfx10_descriptors.h, tsharp_index_word) into the heaps shaders read, as a | ||
| 432 | // loaded library of modules that read it turns on. Returns what it was. For tests, which load no library. | ||
| 433 | bool set_tsharp_index_words_for_test(ID3D12Device* device, bool on); | ||
| 431 | // Whether a draw's or copy's binding of title memory [va, va + size) would be served by the page mirror (VulkanContext's | 434 | // Whether a draw's or copy's binding of title memory [va, va + size) would be served by the page mirror (VulkanContext's |
| 432 | // mirror lookup): it must have been named before. `size` comes back cut at the chunk's end. For tests. | 435 | // mirror lookup): it must have been named before. `size` comes back cut at the chunk's end. For tests. |
| 433 | bool page_mirror_buffer_at(ID3D12Device* device, std::uint64_t va, std::uint64_t& size); | 436 | bool page_mirror_buffer_at(ID3D12Device* device, std::uint64_t va, std::uint64_t& size); |
| @@ -464,7 +467,8 @@ struct BindlessStats { | |||
| 464 | BindlessStats bindless_stats(ID3D12Device* device); | 467 | BindlessStats bindless_stats(ID3D12Device* device); |
| 465 | VkDescriptorSetLayout bindless_set_layout(ID3D12Device* device); | 468 | VkDescriptorSetLayout bindless_set_layout(ID3D12Device* device); |
| 466 | // The bindless index the descriptor hash table holds for the 8 key dwords of a T# (or the 4 of an S# followed by | 469 | // The bindless index the descriptor hash table holds for the 8 key dwords of a T# (or the 4 of an S# followed by |
| 467 | // 0xFFFFFFFF x4), 0 (the fallback) when it has none or `device` has no such set. For tests. | 470 | // 0xFFFFFFFF x4), 0 (the fallback) when it has none or `device` has no such set. A T# read from one of the layer's |
| 471 | // heaps is looked up without its index word (dword 6), and gives 0 unless that word names the same index. For tests. | ||
| 468 | std::uint32_t bindless_hash_lookup(ID3D12Device* device, const std::uint32_t (&key)[8]); | 472 | std::uint32_t bindless_hash_lookup(ID3D12Device* device, const std::uint32_t (&key)[8]); |
| 469 | // The free indices of that set's 2D image and sampler arrays: how many more distinct 2D T#s and S#s | 473 | // The free indices of that set's 2D image and sampler arrays: how many more distinct 2D T#s and S#s |
| 470 | // CreateShaderResourceView and CreateSampler can register before they log that the array is full. Zeros when | 474 | // CreateShaderResourceView and CreateSampler can register before they log that the array is full. Zeros when |
diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 71fccacb8..5bf4c6e44 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp | |||
| @@ -253,24 +253,6 @@ bool copy_guarded_host(std::uint8_t* to, const std::uint8_t* from, std::size_t b | |||
| 253 | } | 253 | } |
| 254 | } | 254 | } |
| 255 | 255 | ||
| 256 | // Whether two byte ranges differ, one of them title memory that may go away meanwhile: false when a page faults. No | ||
| 257 | // C++ objects here: it is a __try frame, and the compare is a loop of its own (a fault inside the CRT's memcmp never | ||
| 258 | // reaches the __except under Wine, src/guarded_copy.h). | ||
| 259 | __declspec(noinline) LEONIDA_GUARDED_NO_BUILTIN bool bytes_differ(const std::uint8_t* a, const std::uint8_t* b, | ||
| 260 | std::size_t n) { | ||
| 261 | for (std::size_t i = 0; i < n; ++i) | ||
| 262 | if (a[i] != b[i]) return true; | ||
| 263 | return false; | ||
| 264 | } | ||
| 265 | bool differ_guarded(const std::uint8_t* a, const std::uint8_t* b, std::size_t n, bool& differs) { | ||
| 266 | __try { | ||
| 267 | differs = bytes_differ(a, b, n); | ||
| 268 | return true; | ||
| 269 | } __except (EXCEPTION_EXECUTE_HANDLER) { | ||
| 270 | return false; | ||
| 271 | } | ||
| 272 | } | ||
| 273 | |||
| 274 | // The same, reading the title's pages that xmem's frame table holds through their frames: a stream pool's GPU-only | 256 | // The same, reading the title's pages that xmem's frame table holds through their frames: a stream pool's GPU-only |
| 275 | // pages and their CPU twins after queue slot 31's copies have no host view (src/title_frames.h). A frame is a section's | 257 | // pages and their CPU twins after queue slot 31's copies have no host view (src/title_frames.h). A frame is a section's |
| 276 | // scratch view, cached memory, so it is not read streaming. | 258 | // scratch view, cached memory, so it is not read streaming. |
| @@ -4033,6 +4015,22 @@ struct DeviceState { | |||
| 4033 | --it; | 4015 | --it; |
| 4034 | return addr < it->second && (addr-it->first) % kDescriptorSize == 0; | 4016 | return addr < it->second && (addr-it->first) % kDescriptorSize == 0; |
| 4035 | } | 4017 | } |
| 4018 | // The T#s of a bound table in one of the layer's heaps get the index word their entry has now (tsharp_index_word): | ||
| 4019 | // Create*View wrote it, but a T# whose view came later (a stream published, a texture made at the address) or went | ||
| 4020 | // since, and one the title wrote raw or copied from where no index was, would otherwise keep the word of then. | ||
| 4021 | void refresh_tsharp_index_words(std::uint8_t* table, VkDeviceSize bytes) { | ||
| 4022 | namespace g = leonida::gfx10; | ||
| 4023 | std::lock_guard lock(descriptor_mutex); | ||
| 4024 | for (VkDeviceSize at = 0; at + kDescriptorSize <= bytes; at += kDescriptorSize) { | ||
| 4025 | g::ImageWords words; | ||
| 4026 | std::memcpy(words.data(), table + at, sizeof(words)); | ||
| 4027 | if (!g::is_image(words)) continue; | ||
| 4028 | const std::uint32_t word = tsharp_index_word_locked(g::tsharp_key(words)); | ||
| 4029 | if (word != words[g::kTsharpIndexDword]) | ||
| 4030 | std::memcpy(table + at + 4 * g::kTsharpIndexDword, &word, sizeof(word)); | ||
| 4031 | } | ||
| 4032 | } | ||
| 4033 | |||
| 4036 | // The buffer V#s of the table at `table_va` whose base is in the title's own memory get the address of a copy, | 4034 | // The buffer V#s of the table at `table_va` whose base is in the title's own memory get the address of a copy, |
| 4037 | // written in place: the title wrote them itself (or copied them), so no Create*View saw them. Images (T#s) and | 4035 | // written in place: the title wrote them itself (or copied them), so no Create*View saw them. Images (T#s) and |
| 4038 | // null slots are skipped; so is a V# already in the arena, so a rewritten one is left alone next time. | 4036 | // null slots are skipped; so is a V# already in the arena, so a rewritten one is left alone next time. |
| @@ -4058,6 +4056,7 @@ struct DeviceState { | |||
| 4058 | // Slots the title wrote in its own backing of this heap, if it gave the heap one. | 4056 | // Slots the title wrote in its own backing of this heap, if it gave the heap one. |
| 4059 | aliases.pull_descriptors(table, std::size_t(end - table_va)); | 4057 | aliases.pull_descriptors(table, std::size_t(end - table_va)); |
| 4060 | if (sampler) return; // S# words describe sampling state. They have no buffer address to translate. | 4058 | if (sampler) return; // S# words describe sampling state. They have no buffer address to translate. |
| 4059 | if (tsharp_index_words) refresh_tsharp_index_words(table, end - table_va); | ||
| 4061 | for (VkDeviceSize at = table_va; at + 16 <= end; at += kDescriptorSize) { | 4060 | for (VkDeviceSize at = table_va; at + 16 <= end; at += kDescriptorSize) { |
| 4062 | auto* words = reinterpret_cast<std::uint32_t*>(table + (at - table_va)); | 4061 | auto* words = reinterpret_cast<std::uint32_t*>(table + (at - table_va)); |
| 4063 | if (words[3] >> 30) continue; | 4062 | if (words[3] >> 30) continue; |
| @@ -4589,6 +4588,7 @@ struct DeviceState { | |||
| 4589 | bool last_local = true; | 4588 | bool last_local = true; |
| 4590 | std::uint64_t presents = 0; // present serial at the last log line | 4589 | std::uint64_t presents = 0; // present serial at the last log line |
| 4591 | std::uint64_t faults_seen = 0, hot_seen = 0, cold_seen = 0; // the tracking's counters at the last log line | 4590 | std::uint64_t faults_seen = 0, hot_seen = 0, cold_seen = 0; // the tracking's counters at the last log line |
| 4591 | std::uint64_t uploaded_seen = 0, conflicts_seen = 0; // the mirror's, likewise | ||
| 4592 | double ms = 0; | 4592 | double ms = 0; |
| 4593 | std::chrono::steady_clock::time_point since = std::chrono::steady_clock::now(); | 4593 | std::chrono::steady_clock::time_point since = std::chrono::steady_clock::now(); |
| 4594 | // The console heaps read in place (add_heap_in_place): their filter state is under this mutex too. | 4594 | // The console heaps read in place (add_heap_in_place): their filter state is under this mutex too. |
| @@ -4879,38 +4879,20 @@ struct DeviceState { | |||
| 4879 | } | 4879 | } |
| 4880 | return true; | 4880 | return true; |
| 4881 | } | 4881 | } |
| 4882 | // What the GPU wrote into the homes of mirrored pages of [begin, end) goes back to the title's pages, as | 4882 | // What the GPU wrote into the homes of mirrored pages of [begin, end) goes back to the title's pages |
| 4883 | // ShadowPages::write_back does for a shadow: after the submissions that could write them have retired | 4883 | // (TitleMirror::write_back: merged at the 256-byte piece, a piece only the GPU changed goes back, one both changed |
| 4884 | // (write_back_titles), only the 256-byte pieces whose home differs from the title's page, and only on a page nothing | 4884 | // keeps the title's bytes), once the submissions that could write them have retired (write_back_titles). Written |
| 4885 | // wrote since it was last watched (a page written on both sides keeps the title's bytes, which are uploaded over | 4885 | // through kernel_writes, which opens and pins the page and every other address of its physical page, so the |
| 4886 | // the home before the next submission). Written through kernel_writes, which opens and pins the page and every | 4886 | // readers over them take the bytes. Returns the bytes written. |
| 4887 | // other address of its physical page, so the readers over them take the bytes. Returns the bytes written. | ||
| 4888 | std::uint64_t page_mirror_write_back(std::uint64_t begin, std::uint64_t end) { | 4887 | std::uint64_t page_mirror_write_back(std::uint64_t begin, std::uint64_t end) { |
| 4889 | auto& m = page_mirror; | 4888 | auto& m = page_mirror; |
| 4890 | if (!m.mirror || begin >= end) return 0; | 4889 | if (!m.mirror || begin >= end) return 0; |
| 4891 | constexpr std::uint64_t kPage = leonida::TitleMirror::kPage, kPiece = 256; | ||
| 4892 | std::lock_guard lock(m.mutex); | 4890 | std::lock_guard lock(m.mutex); |
| 4893 | std::uint64_t written = 0; | 4891 | // A console heap read in place is skipped there: its home holds the filtered slots. |
| 4894 | for (std::uint64_t page = begin & ~(kPage - 1); page < end; page += kPage) { | 4892 | const std::uint64_t written = m.mirror->write_back(begin, end, [](std::uint64_t at, const std::uint8_t* bytes, |
| 4895 | VkDeviceMemory memory = VK_NULL_HANDLE; | 4893 | std::size_t n) { |
| 4896 | VkDeviceSize offset = 0; | 4894 | return leonida::kernel_writes::write(reinterpret_cast<void*>(static_cast<std::uintptr_t>(at)), bytes, n); |
| 4897 | std::uint8_t* home = nullptr; | 4895 | }); |
| 4898 | const std::uint8_t* frame = m.mirror->frame_at(page); | ||
| 4899 | if (!frame || !m.mirror->home_at(page, memory, offset, &home) || !m.tracking->unwritten(page)) continue; | ||
| 4900 | // A console heap read in place: its home holds the filtered slots, never to be copied over the title's. | ||
| 4901 | if (m.mirror->filtered_at(page)) continue; | ||
| 4902 | const std::uint64_t from = std::max(begin, page), to = std::min(end, page + kPage); | ||
| 4903 | for (std::uint64_t at = from & ~(kPiece - 1); at < to; at += kPiece) { | ||
| 4904 | const std::uint64_t a = std::max(at, from), b = std::min(at + kPiece, to); | ||
| 4905 | const std::uint8_t* gpu = home + (a - page); | ||
| 4906 | const std::uint8_t* title = frame + (a - page); | ||
| 4907 | bool differs = false; | ||
| 4908 | // A title page gone since (decommitted, unmapped) has nothing to write back into. | ||
| 4909 | if (!differ_guarded(gpu, title, std::size_t(b - a), differs) || !differs) continue; | ||
| 4910 | if (leonida::kernel_writes::write(reinterpret_cast<void*>(static_cast<std::uintptr_t>(a)), gpu, b - a)) | ||
| 4911 | written += b - a; | ||
| 4912 | } | ||
| 4913 | } | ||
| 4914 | m.written_back += written; | 4896 | m.written_back += written; |
| 4915 | return written; | 4897 | return written; |
| 4916 | } | 4898 | } |
| @@ -4971,16 +4953,19 @@ struct DeviceState { | |||
| 4971 | m.taken.clear(); | 4953 | m.taken.clear(); |
| 4972 | // Ordinary memory released or decommitted: its mirrored pages leave (named again, they are mirrored again). | 4954 | // Ordinary memory released or decommitted: its mirrored pages leave (named again, they are mirrored again). |
| 4973 | leonida::geometry_writes::take_released(m.released, m.released_ranges); | 4955 | leonida::geometry_writes::take_released(m.released, m.released_ranges); |
| 4956 | // Only pages the range holds whole: a freed heap block leaves the rest of its page to the blocks around it. | ||
| 4974 | for (const auto& [from, to] : m.released_ranges) { | 4957 | for (const auto& [from, to] : m.released_ranges) { |
| 4975 | for (auto page = m.identity.lower_bound(from & ~(leonida::TitleMirror::kPage - 1)); | 4958 | constexpr std::uint64_t kPage = leonida::TitleMirror::kPage; |
| 4976 | page != m.identity.end() && *page < to;) { | 4959 | for (auto page = m.identity.lower_bound((from + kPage - 1) & ~(kPage - 1)); |
| 4960 | page != m.identity.end() && *page + kPage <= to;) { | ||
| 4977 | m.tracking->unwatch(*page); | 4961 | m.tracking->unwatch(*page); |
| 4978 | m.mirror->map(*page, nullptr, nullptr, ticket); | 4962 | m.mirror->map(*page, nullptr, nullptr, ticket); |
| 4979 | ++m.identity_released; | 4963 | ++m.identity_released; |
| 4980 | page = m.identity.erase(page); | 4964 | page = m.identity.erase(page); |
| 4981 | } | 4965 | } |
| 4982 | for (auto run = m.ensured.begin(); run != m.ensured.end();) | 4966 | if (to - from >= kPage) // a run that lost a page is named again (pages still there map to themselves) |
| 4983 | run = run->first < to && from < run->second ? m.ensured.erase(run) : std::next(run); | 4967 | for (auto run = m.ensured.begin(); run != m.ensured.end();) |
| 4968 | run = run->first < to && from < run->second ? m.ensured.erase(run) : std::next(run); | ||
| 4984 | } | 4969 | } |
| 4985 | m.tracking->collect(m.written); // re-watched before the copies below | 4970 | m.tracking->collect(m.written); // re-watched before the copies below |
| 4986 | for (const std::uint64_t page : m.written) | 4971 | for (const std::uint64_t page : m.written) |
| @@ -5008,7 +4993,8 @@ struct DeviceState { | |||
| 5008 | "mirrored and %llu released, %llu buffer bindings cut at a chunk's end; %llu slot-133 buffers " | 4993 | "mirrored and %llu released, %llu buffer bindings cut at a chunk's end; %llu slot-133 buffers " |
| 5009 | "made on it, %llu KiB of GPU writes copied back; homes in device-local memory %llu units, in " | 4994 | "made on it, %llu KiB of GPU writes copied back; homes in device-local memory %llu units, in " |
| 5010 | "host memory %llu; tracking: %zu pages hot, %zu cold, %llu write faults, %llu hot and %llu " | 4995 | "host memory %llu; tracking: %zu pages hot, %zu cold, %llu write faults, %llu hot and %llu " |
| 5011 | "cold copies; %.1f KiB uploaded a submission, %.1f KiB a frame\n", | 4996 | "cold copies; uploaded %.1f KiB a submission and %.1f KiB a frame; written back %.1f KiB a " |
| 4997 | "submission and %.1f KiB a frame; %llu pieces written on both sides (the title's kept)\n", | ||
| 5012 | static_cast<unsigned long long>(c.pages), static_cast<unsigned long long>(c.homes), | 4998 | static_cast<unsigned long long>(c.pages), static_cast<unsigned long long>(c.homes), |
| 5013 | static_cast<unsigned long long>(c.units), | 4999 | static_cast<unsigned long long>(c.units), |
| 5014 | static_cast<unsigned long long>(c.units * (leonida::TitleMirror::kUnitBytes >> 20)), | 5000 | static_cast<unsigned long long>(c.units * (leonida::TitleMirror::kUnitBytes >> 20)), |
| @@ -5027,8 +5013,13 @@ struct DeviceState { | |||
| 5027 | policy.hot, policy.cold, static_cast<unsigned long long>(policy.faults - m.faults_seen), | 5013 | policy.hot, policy.cold, static_cast<unsigned long long>(policy.faults - m.faults_seen), |
| 5028 | static_cast<unsigned long long>(policy.hot_copies - m.hot_seen), | 5014 | static_cast<unsigned long long>(policy.hot_copies - m.hot_seen), |
| 5029 | static_cast<unsigned long long>(policy.cold_copies - m.cold_seen), | 5015 | static_cast<unsigned long long>(policy.cold_copies - m.cold_seen), |
| 5030 | m.refreshes ? double(m.uploads) * 64.0 / double(m.refreshes) : 0.0, | 5016 | m.refreshes ? double(c.uploaded_bytes - m.uploaded_seen) / 1024.0 / double(m.refreshes) : 0.0, |
| 5031 | frames ? double(m.uploads) * 64.0 / double(frames) : 0.0); | 5017 | frames ? double(c.uploaded_bytes - m.uploaded_seen) / 1024.0 / double(frames) : 0.0, |
| 5018 | m.refreshes ? double(m.written_back) / 1024.0 / double(m.refreshes) : 0.0, | ||
| 5019 | frames ? double(m.written_back) / 1024.0 / double(frames) : 0.0, | ||
| 5020 | static_cast<unsigned long long>(c.conflicts - m.conflicts_seen)); | ||
| 5021 | m.uploaded_seen = c.uploaded_bytes; | ||
| 5022 | m.conflicts_seen = c.conflicts; | ||
| 5032 | m.faults_seen = policy.faults; | 5023 | m.faults_seen = policy.faults; |
| 5033 | m.hot_seen = policy.hot_copies; | 5024 | m.hot_seen = policy.hot_copies; |
| 5034 | m.cold_seen = policy.cold_copies; | 5025 | m.cold_seen = policy.cold_copies; |
| @@ -5529,6 +5520,11 @@ struct DeviceState { | |||
| 5529 | else if (Records::is_waiting(heap.records.state(slot, 0)) && | 5520 | else if (Records::is_waiting(heap.records.state(slot, 0)) && |
| 5530 | heap.records.base(slot, 0) == texture_address) | 5521 | heap.records.base(slot, 0) == texture_address) |
| 5531 | ++heap.texture_refills; // the T# waited for this address and is bound now | 5522 | ++heap.texture_refills; // the T# waited for this address and is bound now |
| 5523 | // The copy's dword 6 is the index shaders take (gfx10_descriptors.h, tsharp_index_word), or 0 while | ||
| 5524 | // the T# waits: a texture coming, going or being replaced at its address fixes the slot again | ||
| 5525 | // (apply_slot_changes), so the word follows the texture the T# names. The title's bytes and the | ||
| 5526 | // T#'s key keep the title's dword 6. | ||
| 5527 | if (tsharp_index_words) w[leonida::gfx10::kTsharpIndexDword] = unbound ? 0u : tsharp_index_word(words); | ||
| 5532 | } | 5528 | } |
| 5533 | } else { | 5529 | } else { |
| 5534 | // Neither a buffer V# (TYPE is not 0) nor a T# the layer can decode: data the title keeps in the slot. | 5530 | // Neither a buffer V# (TYPE is not 0) nor a T# the layer can decode: data the title keeps in the slot. |
| @@ -5719,7 +5715,11 @@ struct DeviceState { | |||
| 5719 | std::memcpy(words.data(), w, sizeof(words)); | 5715 | std::memcpy(words.data(), w, sizeof(words)); |
| 5720 | leonida::gfx10::Image image{}; | 5716 | leonida::gfx10::Image image{}; |
| 5721 | if (leonida::gfx10::is_image(words) && leonida::gfx10::decode(words, image)) { | 5717 | if (leonida::gfx10::is_image(words) && leonida::gfx10::decode(words, image)) { |
| 5722 | // As the title wrote it; bound by finish_in_place [TSHARP-INDEX: where its bindless index goes in]. | 5718 | // As the title wrote it, bound by finish_in_place, which writes its bindless index into dword 6 |
| 5719 | // (gfx10_descriptors.h, tsharp_index_word; the entry's index needs descriptor_mutex, not taken under | ||
| 5720 | // the mirror's lock). Until then dword 6 is 0, the fallback: the title's own dword 6 (compression | ||
| 5721 | // metadata) is never read as an index. The title's bytes keep it. | ||
| 5722 | if (tsharp_index_words) w[leonida::gfx10::kTsharpIndexDword] = 0; | ||
| 5723 | defer = true; | 5723 | defer = true; |
| 5724 | } else { | 5724 | } else { |
| 5725 | heap.records.clear(slot); | 5725 | heap.records.clear(slot); |
| @@ -5869,6 +5869,11 @@ struct DeviceState { | |||
| 5869 | done[i].texture = !unbound ? Records::texture_bound | 5869 | done[i].texture = !unbound ? Records::texture_bound |
| 5870 | : why == kTsharpArrayFull || why == kTsharpStorageFull ? Records::texture_waiting_room | 5870 | : why == kTsharpArrayFull || why == kTsharpStorageFull ? Records::texture_waiting_room |
| 5871 | : Records::texture_waiting; | 5871 | : Records::texture_waiting; |
| 5872 | // The home's dword 6 is the index shaders take (tsharp_index_word), 0 while the T# waits; a texture | ||
| 5873 | // coming, going or replaced at its address touches the slot (apply_in_place_changes), which is filtered | ||
| 5874 | // and finished again, so the word follows the texture the T# names. The heap copy's write | ||
| 5875 | // (fix_console_slot) is for heaps not read in place. | ||
| 5876 | if (tsharp_index_words) w[leonida::gfx10::kTsharpIndexDword] = unbound ? 0u : tsharp_index_word(words); | ||
| 5872 | } | 5877 | } |
| 5873 | } | 5878 | } |
| 5874 | std::lock_guard lock(page_mirror.mutex); | 5879 | std::lock_guard lock(page_mirror.mutex); |
| @@ -6487,8 +6492,16 @@ struct DeviceState { | |||
| 6487 | const bool imported = heap->imported; | 6492 | const bool imported = heap->imported; |
| 6488 | const VkDeviceSize guarded = heap->guard; | 6493 | const VkDeviceSize guarded = heap->guard; |
| 6489 | arena_guard_bytes.fetch_add(guarded, std::memory_order_relaxed); | 6494 | arena_guard_bytes.fetch_add(guarded, std::memory_order_relaxed); |
| 6490 | if (heap_gpu && console_heap_gpu_enabled() && heap->scratch && resource_heap && !imported) | 6495 | // The pass copies a T#'s words as the title wrote them; it does not write the index word the library's modules |
| 6491 | attach_console_heap_gpu(*heap); | 6496 | // read (tsharp_index_words), so such a library's heaps are fixed on the CPU, which writes it. |
| 6497 | if (heap_gpu && console_heap_gpu_enabled() && heap->scratch && resource_heap && !imported) { | ||
| 6498 | if (!tsharp_index_words) | ||
| 6499 | attach_console_heap_gpu(*heap); | ||
| 6500 | else | ||
| 6501 | log_once("LEONIDA_CONSOLE_HEAP_GPU: the pass does not write the T# index word the shaders read; console " | ||
| 6502 | "descriptor heaps are fixed on the CPU; slots", | ||
| 6503 | int(std::min<VkDeviceSize>(bytes / kDescriptorSize, INT_MAX))); | ||
| 6504 | } | ||
| 6492 | { | 6505 | { |
| 6493 | std::lock_guard lock(console_heap_mutex); | 6506 | std::lock_guard lock(console_heap_mutex); |
| 6494 | if (heap->gpu >= 0) gpu_heaps[std::size_t(heap->gpu)] = heap.get(); | 6507 | if (heap->gpu >= 0) gpu_heaps[std::size_t(heap->gpu)] = heap.get(); |
| @@ -7970,7 +7983,7 @@ struct DeviceState { | |||
| 7970 | char what[96] = "no texture at that address"; | 7983 | char what[96] = "no texture at that address"; |
| 7971 | { | 7984 | { |
| 7972 | std::lock_guard lock(descriptor_mutex); | 7985 | std::lock_guard lock(descriptor_mutex); |
| 7973 | if (image_entries.count(words)) { | 7986 | if (image_entries.count(words) || image_entries.count(g::tsharp_key(words))) { |
| 7974 | kind = 3; | 7987 | kind = 3; |
| 7975 | std::snprintf(what, sizeof(what), "key registered"); | 7988 | std::snprintf(what, sizeof(what), "key registered"); |
| 7976 | } else if (const auto it = images_by_address.find(image.address); it != images_by_address.end()) { | 7989 | } else if (const auto it = images_by_address.find(image.address); it != images_by_address.end()) { |
| @@ -8510,6 +8523,10 @@ struct DeviceState { | |||
| 8510 | // True when the loaded library's modules read the table's kind word (create_bindless, descriptor_table.h): the | 8523 | // True when the loaded library's modules read the table's kind word (create_bindless, descriptor_table.h): the |
| 8511 | // image inserts then record which arrays each index is bound in. | 8524 | // image inserts then record which arrays each index is bound in. |
| 8512 | bool descriptor_table_tagged = false; | 8525 | bool descriptor_table_tagged = false; |
| 8526 | // True when the loaded library's modules read a T#'s bindless index from its dword 6 (create_bindless, | ||
| 8527 | // gfx10_descriptors.h tsharp_index_word): every T# the layer writes into a heap shaders read then carries it, in | ||
| 8528 | // the console heaps' copies (fix_console_slot) and in its own heaps (write_image_descriptor, scan_title_table). | ||
| 8529 | bool tsharp_index_words = false; | ||
| 8513 | std::mutex descriptor_mutex; // the set and the containers below | 8530 | std::mutex descriptor_mutex; // the set and the containers below |
| 8514 | // vkDeviceWaitIdle calls made for the bindless set (none, unless LEONIDA_BINDLESS_IDLE_WAIT restores the old wait). | 8531 | // vkDeviceWaitIdle calls made for the bindless set (none, unless LEONIDA_BINDLESS_IDLE_WAIT restores the old wait). |
| 8515 | std::atomic<std::uint64_t> bindless_idle_waits{0}; | 8532 | std::atomic<std::uint64_t> bindless_idle_waits{0}; |
| @@ -8612,6 +8629,7 @@ struct DeviceState { | |||
| 8612 | std::atomic<unsigned> pending_sample_count{}; | 8629 | std::atomic<unsigned> pending_sample_count{}; |
| 8613 | bool materialize_sample(const leonida::DescriptorKey& key); | 8630 | bool materialize_sample(const leonida::DescriptorKey& key); |
| 8614 | bool materialize_table(std::uint64_t address,std::uint32_t slots); | 8631 | bool materialize_table(std::uint64_t address,std::uint32_t slots); |
| 8632 | void write_tsharp_index_words(const std::vector<leonida::DescriptorKey>& keys); | ||
| 8615 | void remember_sample(const leonida::DescriptorKey& key, const PendingSample& sample) { | 8633 | void remember_sample(const leonida::DescriptorKey& key, const PendingSample& sample) { |
| 8616 | std::lock_guard lock(pending_view_mutex); | 8634 | std::lock_guard lock(pending_view_mutex); |
| 8617 | if (pending_samples.try_emplace(key,sample).second) { | 8635 | if (pending_samples.try_emplace(key,sample).second) { |
| @@ -8897,6 +8915,11 @@ struct DeviceState { | |||
| 8897 | if (existing != image_entries.end()) { | 8915 | if (existing != image_entries.end()) { |
| 8898 | replaced_images.push_back(existing->second); // the read-only entry of the same bytes | 8916 | replaced_images.push_back(existing->second); // the read-only entry of the same bytes |
| 8899 | existing->second = entry; | 8917 | existing->second = entry; |
| 8918 | // The console heap slots holding these words carry the read-only entry's index in their dword 6 | ||
| 8919 | // (tsharp_index_words): fixed again at the next submission (a texture_gone change re-fixes bound T#s, | ||
| 8920 | // under the address their records keep, tsharp_texture_key), they take this one, with its storage views. | ||
| 8921 | if (tsharp_index_words) | ||
| 8922 | note_texture_change(leonida::SlotChange::texture_gone, tsharp_texture_key(image.address)); | ||
| 8900 | } else { | 8923 | } else { |
| 8901 | image_entries.emplace(words, entry); | 8924 | image_entries.emplace(words, entry); |
| 8902 | image_keys.emplace(image.address, words); | 8925 | image_keys.emplace(image.address, words); |
| @@ -8909,6 +8932,22 @@ struct DeviceState { | |||
| 8909 | return true; | 8932 | return true; |
| 8910 | } | 8933 | } |
| 8911 | 8934 | ||
| 8935 | // The dword 6 that the T# `words` (its key: the descriptor as the title or Create*View wrote it) gets in a heap | ||
| 8936 | // shaders read (gfx10_descriptors.h, tsharp_index_word): its entry's index and the arrays that index is bound in, | ||
| 8937 | // which the set derives from what it bound there (image_kind_at); 0, the fallback, while the T# has no entry (no | ||
| 8938 | // texture yet, a stream still pending, no room). descriptor_mutex held. | ||
| 8939 | std::uint32_t tsharp_index_word_locked(const leonida::gfx10::ImageWords& words) const { | ||
| 8940 | if (!bindless) return 0; | ||
| 8941 | const auto it = image_entries.find(words); | ||
| 8942 | if (it == image_entries.end()) return 0; | ||
| 8943 | return leonida::gfx10::tsharp_index_word(it->second.index, | ||
| 8944 | bindless->image_kind_at(it->second.dimension, it->second.index)); | ||
| 8945 | } | ||
| 8946 | std::uint32_t tsharp_index_word(const leonida::gfx10::ImageWords& words) { | ||
| 8947 | std::lock_guard lock(descriptor_mutex); | ||
| 8948 | return tsharp_index_word_locked(words); | ||
| 8949 | } | ||
| 8950 | |||
| 8912 | // Textures by the address their T#s name them by (Resource::image_address), for the T#s a title writes into a | 8951 | // Textures by the address their T#s name them by (Resource::image_address), for the T#s a title writes into a |
| 8913 | // console descriptor heap itself (bind_title_image). Under descriptor_mutex: a texture leaves it in forget_image, | 8952 | // console descriptor heap itself (bind_title_image). Under descriptor_mutex: a texture leaves it in forget_image, |
| 8914 | // before its image goes. | 8953 | // before its image goes. |
| @@ -11780,6 +11819,7 @@ bool DeviceState::materialize_table(std::uint64_t address, std::uint32_t slots) | |||
| 11780 | for (const auto& [key,view]:pending_samples) | 11819 | for (const auto& [key,view]:pending_samples) |
| 11781 | if (view.owner->id() == address) keys.push_back(key); } | 11820 | if (view.owner->id() == address) keys.push_back(key); } |
| 11782 | for (const auto& key:keys) if (!materialize_sample(key)) return false; | 11821 | for (const auto& key:keys) if (!materialize_sample(key)) return false; |
| 11822 | write_tsharp_index_words(keys); | ||
| 11783 | return true; | 11823 | return true; |
| 11784 | } | 11824 | } |
| 11785 | std::uint64_t end = 0; | 11825 | std::uint64_t end = 0; |
| @@ -11799,6 +11839,7 @@ bool DeviceState::materialize_table(std::uint64_t address, std::uint32_t slots) | |||
| 11799 | std::vector<leonida::DescriptorKey> all; | 11839 | std::vector<leonida::DescriptorKey> all; |
| 11800 | {std::lock_guard lock(pending_view_mutex);for(const auto& [key,v]:pending_samples) all.push_back(key);} | 11840 | {std::lock_guard lock(pending_view_mutex);for(const auto& [key,v]:pending_samples) all.push_back(key);} |
| 11801 | for(const auto& key:all) if(!materialize_sample(key)) return false; | 11841 | for(const auto& key:all) if(!materialize_sample(key)) return false; |
| 11842 | write_tsharp_index_words(all); | ||
| 11802 | return true; | 11843 | return true; |
| 11803 | } | 11844 | } |
| 11804 | const auto refreshed = std::min<std::uint64_t>(end-address, std::uint64_t(kViewScanLimit)*kDescriptorSize); | 11845 | const auto refreshed = std::min<std::uint64_t>(end-address, std::uint64_t(kViewScanLimit)*kDescriptorSize); |
| @@ -11809,11 +11850,49 @@ bool DeviceState::materialize_table(std::uint64_t address, std::uint32_t slots) | |||
| 11809 | for (auto at=address; at<end; at+=kDescriptorSize) { | 11850 | for (auto at=address; at<end; at+=kDescriptorSize) { |
| 11810 | leonida::DescriptorKey key{}; | 11851 | leonida::DescriptorKey key{}; |
| 11811 | std::memcpy(key.data(),table+(at-address),sizeof(key)); | 11852 | std::memcpy(key.data(),table+(at-address),sizeof(key)); |
| 11812 | if (leonida::gfx10::is_image(key) && !materialize_sample(key)) return false; | 11853 | if (!leonida::gfx10::is_image(key)) continue; |
| 11854 | // A deferred view's slot was written without its index word (it had no index): its key is the slot without | ||
| 11855 | // the word, and once made the slot gets it. | ||
| 11856 | key = leonida::gfx10::tsharp_key(key); | ||
| 11857 | if (!materialize_sample(key)) return false; | ||
| 11858 | if (tsharp_index_words) { | ||
| 11859 | const std::uint32_t word = tsharp_index_word(key); | ||
| 11860 | std::memcpy(table + (at - address) + 4 * leonida::gfx10::kTsharpIndexDword, &word, sizeof(word)); | ||
| 11861 | } | ||
| 11813 | } | 11862 | } |
| 11814 | return true; | 11863 | return true; |
| 11815 | } | 11864 | } |
| 11816 | 11865 | ||
| 11866 | // The views of `keys` made at last (materialize_sample, LEONIDA_PIPELINE_COMPILE=on-demand) without a table extent to | ||
| 11867 | // name their slots: every T# of them in the layer's heaps (shader-visible, and the CPU-only ones a later | ||
| 11868 | // CopyDescriptors copies from) gets its index word (tsharp_index_word). Only while views are pending, once per batch. | ||
| 11869 | void DeviceState::write_tsharp_index_words(const std::vector<leonida::DescriptorKey>& keys) { | ||
| 11870 | if (!tsharp_index_words || keys.empty()) return; | ||
| 11871 | namespace g = leonida::gfx10; | ||
| 11872 | const std::set<leonida::DescriptorKey> wanted(keys.begin(), keys.end()); | ||
| 11873 | std::vector<std::pair<std::uint8_t*, std::size_t>> ranges; | ||
| 11874 | { | ||
| 11875 | std::lock_guard lock(visible_heap_mutex); | ||
| 11876 | for (const auto& [offset, heap] : visible_heaps) | ||
| 11877 | if (heap.type == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV) | ||
| 11878 | if (std::uint8_t* host = arena_host(offset, heap.end - offset)) | ||
| 11879 | ranges.emplace_back(host, std::size_t(heap.end - offset)); | ||
| 11880 | for (const auto& [begin, end] : cpu_view_heaps) | ||
| 11881 | ranges.emplace_back(reinterpret_cast<std::uint8_t*>(begin), std::size_t(end - begin)); | ||
| 11882 | } | ||
| 11883 | std::lock_guard lock(descriptor_mutex); | ||
| 11884 | for (const auto& [base, bytes] : ranges) | ||
| 11885 | for (std::size_t at = 0; at + kDescriptorSize <= bytes; at += kDescriptorSize) { | ||
| 11886 | g::ImageWords words; | ||
| 11887 | std::memcpy(words.data(), base + at, sizeof(words)); | ||
| 11888 | if (!g::is_image(words)) continue; | ||
| 11889 | const g::ImageWords key = g::tsharp_key(words); | ||
| 11890 | if (!wanted.count(key)) continue; | ||
| 11891 | const std::uint32_t word = tsharp_index_word_locked(key); | ||
| 11892 | std::memcpy(base + at + 4 * g::kTsharpIndexDword, &word, sizeof(word)); | ||
| 11893 | } | ||
| 11894 | } | ||
| 11895 | |||
| 11817 | class Heap final | 11896 | class Heap final |
| 11818 | : public Child<Heap, ID3D12HeapStubs, ID3D12Object, ID3D12DeviceChild, ID3D12Pageable, ID3D12Heap> { | 11897 | : public Child<Heap, ID3D12HeapStubs, ID3D12Object, ID3D12DeviceChild, ID3D12Pageable, ID3D12Heap> { |
| 11819 | public: | 11898 | public: |
| @@ -16426,6 +16505,21 @@ public: | |||
| 16426 | leonida::descriptor_table_layout_name(descriptor_table), table_modules.total()); | 16505 | leonida::descriptor_table_layout_name(descriptor_table), table_modules.total()); |
| 16427 | } | 16506 | } |
| 16428 | state_.descriptor_table_tagged = descriptor_table == leonida::DescriptorTableLayout::tagged; | 16507 | state_.descriptor_table_tagged = descriptor_table == leonida::DescriptorTableLayout::tagged; |
| 16508 | // Whether the loaded modules read a T#'s index from its dword 6 (shader_library.h, TsharpIndexCensus): one such | ||
| 16509 | // module needs the word in every heap. Older modules find heap T#s by their words in the hash table, which the | ||
| 16510 | // word changes, so a library with both kinds is reported: its older modules read the fallback for those T#s. | ||
| 16511 | if (const leonida::ShaderLibrary* library = state_.library()) { | ||
| 16512 | const leonida::TsharpIndexCensus& census = library->tsharp_index_census(); | ||
| 16513 | state_.tsharp_index_words = census.dword6 > 0; | ||
| 16514 | if (census.dword6 && census.none) | ||
| 16515 | std::fprintf(stderr, | ||
| 16516 | "[rt] d3d12: T# index word: %zu modules read it, %zu older ones look heap T#s up in the " | ||
| 16517 | "hash table and miss them (the heaps carry the word); translate the library again\n", | ||
| 16518 | census.dword6, census.none); | ||
| 16519 | else | ||
| 16520 | std::fprintf(stderr, "[rt] d3d12: T# index word: %s (all %zu modules)\n", | ||
| 16521 | census.dword6 ? "written into the heaps' T#s" : "not read", census.total()); | ||
| 16522 | } | ||
| 16429 | const auto get_properties = reinterpret_cast<PFN_vkGetPhysicalDeviceProperties2>( | 16523 | const auto get_properties = reinterpret_cast<PFN_vkGetPhysicalDeviceProperties2>( |
| 16430 | state_.info.get_instance_proc(state_.info.instance, "vkGetPhysicalDeviceProperties2")); | 16524 | state_.info.get_instance_proc(state_.info.instance, "vkGetPhysicalDeviceProperties2")); |
| 16431 | if (get_properties) { | 16525 | if (get_properties) { |
| @@ -17239,7 +17333,7 @@ public: | |||
| 17239 | if (!leonida::gfx10::is_image(result)) return false; | 17333 | if (!leonida::gfx10::is_image(result)) return false; |
| 17240 | std::lock_guard descriptors(state_.descriptor_mutex); | 17334 | std::lock_guard descriptors(state_.descriptor_mutex); |
| 17241 | if (!state_.bindless) return true; | 17335 | if (!state_.bindless) return true; |
| 17242 | const auto entry = state_.image_entries.find(result); | 17336 | const auto entry = state_.image_entries.find(leonida::gfx10::tsharp_key(result)); |
| 17243 | if (entry != state_.image_entries.end() && entry->second.storage) return true; | 17337 | if (entry != state_.image_entries.end() && entry->second.storage) return true; |
| 17244 | std::memset(reinterpret_cast<void*>(dest.ptr), 0, kDescriptorSize); | 17338 | std::memset(reinterpret_cast<void*>(dest.ptr), 0, kDescriptorSize); |
| 17245 | return false; | 17339 | return false; |
| @@ -22226,6 +22320,8 @@ private: | |||
| 22226 | log_once("shader access to 1D and 3D UAVs requires robustImageAccess; type", int(view.type)); | 22320 | log_once("shader access to 1D and 3D UAVs requires robustImageAccess; type", int(view.type)); |
| 22227 | } else { | 22321 | } else { |
| 22228 | const bool known_heap = state_.known_view_heap(destination.ptr); | 22322 | const bool known_heap = state_.known_view_heap(destination.ptr); |
| 22323 | // A deferred view has no index yet: its slot gets the index word when the view is made | ||
| 22324 | // (materialize_table, write_tsharp_index_words). | ||
| 22229 | if (!storage && resource.deferrable_sample() && state_.info.prepare_on_bind && known_heap && | 22325 | if (!storage && resource.deferrable_sample() && state_.info.prepare_on_bind && known_heap && |
| 22230 | !g::multisampled(image.type)) { | 22326 | !g::multisampled(image.type)) { |
| 22231 | state_.remember_sample(words,{const_cast<Resource*>(&resource),image,view_format,channels_format}); | 22327 | state_.remember_sample(words,{const_cast<Resource*>(&resource),image,view_format,channels_format}); |
| @@ -22239,6 +22335,11 @@ private: | |||
| 22239 | sampled, storage, slices); | 22335 | sampled, storage, slices); |
| 22240 | if (registered && (resource.sparse_image() || storage_plane)) state_.context->note_shader_image(resource.id()); | 22336 | if (registered && (resource.sparse_image() || storage_plane)) state_.context->note_shader_image(resource.id()); |
| 22241 | if (!registered && (g::multisampled(image.type) || storage_plane)) return; | 22337 | if (!registered && (g::multisampled(image.type) || storage_plane)) return; |
| 22338 | // In the layer's own heaps the bytes written here are what shaders load: dword 6 carries the index | ||
| 22339 | // (gfx10_descriptors.h, tsharp_index_word; 0 while the T# has none). A console heap (the title's memory) | ||
| 22340 | // is left as the title's own write would be: its copy gets the word when the slot is fixed. | ||
| 22341 | if (state_.tsharp_index_words && known_heap) | ||
| 22342 | words[g::kTsharpIndexDword] = state_.tsharp_index_word(words); | ||
| 22242 | } | 22343 | } |
| 22243 | // The CPU UAV used by ClearUnorderedAccessView is independent of set 0. | 22344 | // The CPU UAV used by ClearUnorderedAccessView is independent of set 0. |
| 22244 | std::memcpy(reinterpret_cast<void*>(destination.ptr), words.data(), sizeof(words)); | 22345 | std::memcpy(reinterpret_cast<void*>(destination.ptr), words.data(), sizeof(words)); |
| @@ -23243,6 +23344,14 @@ bool arena_gpu_read(ID3D12Device* device, std::uint64_t va, void* out, std::size | |||
| 23243 | return gpu_buffer_read(*state, state->arena.buffer, va, wait, out, bytes); | 23344 | return gpu_buffer_read(*state, state->arena.buffer, va, wait, out, bytes); |
| 23244 | } | 23345 | } |
| 23245 | 23346 | ||
| 23347 | bool set_tsharp_index_words_for_test(ID3D12Device* device, bool on) { | ||
| 23348 | DeviceState* state = mirror_state(device); | ||
| 23349 | if (!state) return false; | ||
| 23350 | const bool was = state->tsharp_index_words; | ||
| 23351 | state->tsharp_index_words = on; | ||
| 23352 | return was; | ||
| 23353 | } | ||
| 23354 | |||
| 23246 | bool page_mirror_buffer_at(ID3D12Device* device, std::uint64_t va, std::uint64_t& size) { | 23355 | bool page_mirror_buffer_at(ID3D12Device* device, std::uint64_t va, std::uint64_t& size) { |
| 23247 | ID3D12Device* real = device ? rt::d3d12_xs::device_proxy_real_device(device) : nullptr; | 23356 | ID3D12Device* real = device ? rt::d3d12_xs::device_proxy_real_device(device) : nullptr; |
| 23248 | if (!real) real = device; | 23357 | if (!real) real = device; |
| @@ -23389,7 +23498,16 @@ VkDescriptorSetLayout bindless_set_layout(ID3D12Device* device) { | |||
| 23389 | 23498 | ||
| 23390 | std::uint32_t bindless_hash_lookup(ID3D12Device* device, const std::uint32_t (&key)[8]) { | 23499 | std::uint32_t bindless_hash_lookup(ID3D12Device* device, const std::uint32_t (&key)[8]) { |
| 23391 | const leonida::BindlessSet* set = bindless_of(device); | 23500 | const leonida::BindlessSet* set = bindless_of(device); |
| 23392 | return set ? set->table().lookup(key) : 0; | 23501 | if (!set) return 0; |
| 23502 | // A T# read back from one of the layer's heaps carries its index in dword 6 (tsharp_index_word): its key is the | ||
| 23503 | // descriptor without it, and the word must name the index the table holds, which is what a shader reads. | ||
| 23504 | namespace g = leonida::gfx10; | ||
| 23505 | g::ImageWords words; | ||
| 23506 | std::memcpy(words.data(), key, sizeof(words)); | ||
| 23507 | if (!g::is_image(words) || !g::has_tsharp_index(words)) return set->table().lookup(key); | ||
| 23508 | const g::ImageWords stripped = g::tsharp_key(words); | ||
| 23509 | const std::uint32_t index = set->table().lookup(stripped.data()); | ||
| 23510 | return index == g::tsharp_index_of(words) ? index : 0; | ||
| 23393 | } | 23511 | } |
| 23394 | 23512 | ||
| 23395 | BindlessRoom bindless_room(ID3D12Device* device) { | 23513 | BindlessRoom bindless_room(ID3D12Device* device) { |
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp index 69a0b9b44..6a0f36e15 100644 --- a/runtime/shims/xmem.cpp +++ b/runtime/shims/xmem.cpp | |||
| @@ -46,6 +46,7 @@ | |||
| 46 | #include "../src/dstorage_trace.h" | 46 | #include "../src/dstorage_trace.h" |
| 47 | #include "../src/gpu_label_memory.h" | 47 | #include "../src/gpu_label_memory.h" |
| 48 | #include "../src/title_frames.h" | 48 | #include "../src/title_frames.h" |
| 49 | #include "../src/title_heap.h" | ||
| 49 | #include "../src/title_mirror_space.h" | 50 | #include "../src/title_mirror_space.h" |
| 50 | #include "../src/title_page_watch.h" | 51 | #include "../src/title_page_watch.h" |
| 51 | #include "../src/title_geometry_watch.h" | 52 | #include "../src/title_geometry_watch.h" |
| @@ -83,12 +84,57 @@ void note_attributes(const char* api, uint64_t attributes, size_t size, const vo | |||
| 83 | // The title's XMemAlloc blocks come from a heap of their own instead of the CRT heap (LEONIDA_TITLE_HEAP=0: not). Slot-133 | 84 | // The title's XMemAlloc blocks come from a heap of their own instead of the CRT heap (LEONIDA_TITLE_HEAP=0: not). Slot-133 |
| 84 | // imports pin title memory page by page for the GPU, and on the CRT heap a pinned page also holds the runtime's own | 85 | // imports pin title memory page by page for the GPU, and on the CRT heap a pinned page also holds the runtime's own |
| 85 | // objects (g_pages' nodes among them): a GPU store past a buffer's end, or into a block freed while its import still | 86 | // objects (g_pages' nodes among them): a GPU store past a buffer's end, or into a block freed while its import still |
| 86 | // lived, then corrupted the runtime (speed-30/41/49-52: g_pages found corrupt under g_phys_mutex). Each block keeps its | 87 | // lived, then corrupted the runtime (speed-30/41/49-52: g_pages found corrupt under g_phys_mutex). |
| 87 | // size and the heap allocation it sits in just below it. | 88 | // |
| 89 | // The heap is the runtime's own (src/title_heap.h) over a reservation in the page mirror's range | ||
| 90 | // (src/title_mirror_space.h), so a V# or pointer the title writes into a block reaches the mirror at heap_base + the | ||
| 91 | // block's address, and its committed pages are fed to the mirror as they are committed (they read the title's bytes | ||
| 92 | // there, not zeros). RtlCreateHeap over a given base faults under Wine. Where the range cannot take the reservation, a | ||
| 93 | // heap the host places, as before (each block keeps its size and the heap allocation it sits in just below it). | ||
| 88 | struct TitleHeapHeader { | 94 | struct TitleHeapHeader { |
| 89 | void* raw; | 95 | void* raw; |
| 90 | size_t size; | 96 | size_t size; |
| 91 | }; | 97 | }; |
| 98 | constexpr size_t kTitleHeapBytes = size_t(32) << 30; // address range only: pages are committed as blocks need them | ||
| 99 | |||
| 100 | // Tells the page mirror that the pages of [at, at + bytes) of the heap are committed (each shows itself) or not any | ||
| 101 | // more (defined with the feed, below). | ||
| 102 | void feed_heap_pages(uintptr_t at, size_t bytes, bool committed); | ||
| 103 | |||
| 104 | leonida::TitleHeap& mirror_heap() { | ||
| 105 | static leonida::TitleHeap heap; | ||
| 106 | return heap; | ||
| 107 | } | ||
| 108 | bool mirror_heap_ready() { | ||
| 109 | static const bool ready = [] { | ||
| 110 | const char* v = std::getenv("LEONIDA_TITLE_HEAP"); | ||
| 111 | if (v && std::strcmp(v, "0") == 0) return false; | ||
| 112 | uintptr_t base = 0; | ||
| 113 | // Not "plain": every committed page is fed to the mirror as it is committed, so a pointer the title wrote reads | ||
| 114 | // its bytes without anything naming the range first. | ||
| 115 | leonida::title_mirror_space::take(kTitleHeapBytes, 0x10000, false, [&](std::uint64_t at) { | ||
| 116 | void* const made = VirtualAlloc(reinterpret_cast<void*>(static_cast<uintptr_t>(at)), kTitleHeapBytes, | ||
| 117 | MEM_RESERVE, PAGE_READWRITE); | ||
| 118 | base = reinterpret_cast<uintptr_t>(made); | ||
| 119 | return made != nullptr; | ||
| 120 | }); | ||
| 121 | leonida::TitleHeap::Hooks hooks; | ||
| 122 | hooks.commit = [](uintptr_t at, size_t bytes) { | ||
| 123 | if (!VirtualAlloc(reinterpret_cast<void*>(at), bytes, MEM_COMMIT, PAGE_READWRITE)) return false; | ||
| 124 | feed_heap_pages(at, bytes, true); | ||
| 125 | return true; | ||
| 126 | }; | ||
| 127 | hooks.decommit = [](uintptr_t at, size_t bytes) { | ||
| 128 | feed_heap_pages(at, bytes, false); | ||
| 129 | VirtualFree(reinterpret_cast<void*>(at), bytes, MEM_DECOMMIT); | ||
| 130 | }; | ||
| 131 | const bool ok = base && mirror_heap().init(base, kTitleHeapBytes, std::move(hooks)); | ||
| 132 | std::fprintf(stderr, "[rt] xmem: XMemAlloc blocks come from %s\n", | ||
| 133 | ok ? "the runtime's heap in the page mirror's range" : "a heap the host places"); | ||
| 134 | return ok; | ||
| 135 | }(); | ||
| 136 | return ready; | ||
| 137 | } | ||
| 92 | HANDLE title_heap() { | 138 | HANDLE title_heap() { |
| 93 | static const HANDLE heap = [] { | 139 | static const HANDLE heap = [] { |
| 94 | // Set LEONIDA_TITLE_HEAP=0 to use the CRT heap. | 140 | // Set LEONIDA_TITLE_HEAP=0 to use the CRT heap. |
| @@ -102,8 +148,10 @@ HANDLE title_heap() { | |||
| 102 | } | 148 | } |
| 103 | void* allocate_block(size_t size) { | 149 | void* allocate_block(size_t size) { |
| 104 | size = size ? size : 1; | 150 | size = size ? size : 1; |
| 151 | if (mirror_heap_ready()) | ||
| 152 | if (void* p = mirror_heap().allocate(size)) return p; // full: the host's heap, as before | ||
| 105 | const HANDLE heap = title_heap(); | 153 | const HANDLE heap = title_heap(); |
| 106 | if (!heap) return _aligned_malloc(size, kAlign); | 154 | if (!heap) return _aligned_malloc(size, kAlign); // LEONIDA_TITLE_HEAP=0 |
| 107 | const size_t extra = kAlign + sizeof(TitleHeapHeader); | 155 | const size_t extra = kAlign + sizeof(TitleHeapHeader); |
| 108 | if (size > SIZE_MAX - extra) return nullptr; | 156 | if (size > SIZE_MAX - extra) return nullptr; |
| 109 | void* raw = HeapAlloc(heap, 0, size + extra); | 157 | void* raw = HeapAlloc(heap, 0, size + extra); |
| @@ -116,11 +164,16 @@ void* allocate_block(size_t size) { | |||
| 116 | } | 164 | } |
| 117 | size_t block_size(void* p) { | 165 | size_t block_size(void* p) { |
| 118 | if (!p) return 0; | 166 | if (!p) return 0; |
| 167 | if (mirror_heap_ready() && mirror_heap().owns(p)) return mirror_heap().size_of(p); | ||
| 119 | if (!title_heap()) return _aligned_msize(p, kAlign, 0); | 168 | if (!title_heap()) return _aligned_msize(p, kAlign, 0); |
| 120 | return (reinterpret_cast<TitleHeapHeader*>(p) - 1)->size; | 169 | return (reinterpret_cast<TitleHeapHeader*>(p) - 1)->size; |
| 121 | } | 170 | } |
| 122 | void free_block(void* p) { | 171 | void free_block(void* p) { |
| 123 | if (!p) return; | 172 | if (!p) return; |
| 173 | if (mirror_heap_ready() && mirror_heap().owns(p)) { | ||
| 174 | mirror_heap().release(p); | ||
| 175 | return; | ||
| 176 | } | ||
| 124 | const HANDLE heap = title_heap(); | 177 | const HANDLE heap = title_heap(); |
| 125 | if (!heap) return _aligned_free(p); | 178 | if (!heap) return _aligned_free(p); |
| 126 | HeapFree(heap, 0, (reinterpret_cast<TitleHeapHeader*>(p) - 1)->raw); | 179 | HeapFree(heap, 0, (reinterpret_cast<TitleHeapHeader*>(p) - 1)->raw); |
| @@ -581,6 +634,16 @@ HostUnorderedMap<uintptr_t, TableFrame> g_table_frames; | |||
| 581 | leonida::title_frames::Feed g_mirror_feed = nullptr; | 634 | leonida::title_frames::Feed g_mirror_feed = nullptr; |
| 582 | void* g_mirror_context = nullptr; | 635 | void* g_mirror_context = nullptr; |
| 583 | 636 | ||
| 637 | // The heap's committed pages show themselves (title_heap): fed to the page mirror as they are committed and | ||
| 638 | // decommitted, under g_phys_mutex like every other change of the feed. Called with the heap's lock held (heap, then | ||
| 639 | // g_phys_mutex; attach_mirror takes the heap's lock only after releasing g_phys_mutex). | ||
| 640 | void feed_heap_pages(uintptr_t at, size_t bytes, bool committed) { | ||
| 641 | const std::lock_guard lock(g_phys_mutex); | ||
| 642 | if (!g_mirror_feed) return; | ||
| 643 | for (uintptr_t page = at & ~uintptr_t(kPhysicalPageSize - 1); page < at + bytes; page += kPhysicalPageSize) | ||
| 644 | g_mirror_feed(g_mirror_context, page, committed ? reinterpret_cast<uint8_t*>(page) : nullptr, {}); | ||
| 645 | } | ||
| 646 | |||
| 584 | // With g_phys_mutex held: the page at `address` shows physical page `page` now (0: nothing), for the page mirror. | 647 | // With g_phys_mutex held: the page at `address` shows physical page `page` now (0: nothing), for the page mirror. |
| 585 | void feed_mirror(uintptr_t address, uint64_t page) { | 648 | void feed_mirror(uintptr_t address, uint64_t page) { |
| 586 | if (!g_mirror_feed || !leonida::title_mirror_space::contains(address)) return; | 649 | if (!g_mirror_feed || !leonida::title_mirror_space::contains(address)) return; |
| @@ -2922,7 +2985,17 @@ void slot31_mapping_counts(Slot31Counts& out) { | |||
| 2922 | // their physical pages' host addresses. | 2985 | // their physical pages' host addresses. |
| 2923 | // title_frames' Attach (src/title_frames.h): the page mirror's feed from now on, after every page of the mirror's range | 2986 | // title_frames' Attach (src/title_frames.h): the page mirror's feed from now on, after every page of the mirror's range |
| 2924 | // mapped now (the views, then the table pages), all under g_phys_mutex. | 2987 | // mapped now (the views, then the table pages), all under g_phys_mutex. |
| 2988 | void attach_mirror_locked(void* context, leonida::title_frames::Feed feed); | ||
| 2925 | void attach_mirror(void* context, leonida::title_frames::Feed feed) { | 2989 | void attach_mirror(void* context, leonida::title_frames::Feed feed) { |
| 2990 | attach_mirror_locked(context, feed); | ||
| 2991 | if (!feed || !mirror_heap_ready()) return; | ||
| 2992 | // The heap's committed pages, after g_phys_mutex (the heap takes it under its own lock). A page committed or | ||
| 2993 | // decommitted meanwhile was fed by the heap itself; one fed twice is mapped twice to the same bytes. | ||
| 2994 | std::vector<std::pair<uintptr_t, size_t>> committed; | ||
| 2995 | mirror_heap().for_each_committed([&](uintptr_t at, size_t bytes) { committed.emplace_back(at, bytes); }); | ||
| 2996 | for (const auto& [at, bytes] : committed) feed_heap_pages(at, bytes, true); | ||
| 2997 | } | ||
| 2998 | void attach_mirror_locked(void* context, leonida::title_frames::Feed feed) { | ||
| 2926 | const std::lock_guard lock(g_phys_mutex); | 2999 | const std::lock_guard lock(g_phys_mutex); |
| 2927 | g_mirror_feed = feed; | 3000 | g_mirror_feed = feed; |
| 2928 | g_mirror_context = context; | 3001 | g_mirror_context = context; |
diff --git a/runtime/src/gfx10_descriptors.h b/runtime/src/gfx10_descriptors.h index acfa590f8..5773c8734 100644 --- a/runtime/src/gfx10_descriptors.h +++ b/runtime/src/gfx10_descriptors.h | |||
| @@ -179,6 +179,45 @@ inline constexpr bool partial_volume_uav(const ImageWords& words, const Image& i | |||
| 179 | return range.first_w != 0 || range.w_size != (mip_depth ? mip_depth : 1u); | 179 | return range.first_w != 0 || range.w_size != (mip_depth ? mip_depth : 1u); |
| 180 | } | 180 | } |
| 181 | 181 | ||
| 182 | // The bindless index in the T# (docs/design.md, "Bindless index in the T#"). In the heaps shaders read (the copies of | ||
| 183 | // the console resource heaps and the layer's own shader-visible heaps) the layer writes dword 6 of every T# as | ||
| 184 | // TAG [31:28] | storage kind [27:23] | sampled kind [22:16] | index [15:0] | ||
| 185 | // where the kinds are descriptor_table.h's kind bits 8..12 and 0..6 for that index. A translated shader that loaded | ||
| 186 | // the T# from such a heap takes the index from this word and no hash-table lookup (tools/rdna2spirv.py, | ||
| 187 | // tsharp_index_word), and reads index 0 (the fallback) unless the word has the tag and dword 3 names an image (TYPE | ||
| 188 | // 8..15, bit 31). Dword 6 is the hardware's compression metadata (META_DATA_ADDRESS, COMPRESSION_EN and friends), | ||
| 189 | // which no Vulkan view uses; over the 1.0.2.1 corpus no shader reads dword 6 or 7 of a heap T# with a scalar | ||
| 190 | // instruction (tools/descriptor_source_census.py). The title's own bytes keep their dword 6, and a T#'s table key | ||
| 191 | // stays the descriptor as the title or Create*View wrote it (tsharp_key strips the word from the layer's heaps). | ||
| 192 | inline constexpr unsigned kTsharpIndexDword = 6; | ||
| 193 | inline constexpr std::uint32_t kTsharpIndexTag = 0xA; | ||
| 194 | inline constexpr std::uint32_t kTsharpIndexLimit = 0x10000; // an index past 16 bits gets no word (the fallback) | ||
| 195 | |||
| 196 | // The word for `index` bound in the arrays `kind` names (descriptor_table.h's kind bits); 0 for index 0 or one that | ||
| 197 | // does not fit, which every shader reads as the fallback. | ||
| 198 | inline constexpr std::uint32_t tsharp_index_word(std::uint32_t index, std::uint32_t kind) { | ||
| 199 | if (!index || index >= kTsharpIndexLimit) return 0; | ||
| 200 | return (kTsharpIndexTag << 28) | (((kind >> 8) & 0x1Fu) << 23) | ((kind & 0x7Fu) << 16) | index; | ||
| 201 | } | ||
| 202 | inline constexpr bool has_tsharp_index(const ImageWords& words) { | ||
| 203 | return (words[kTsharpIndexDword] >> 28) == kTsharpIndexTag; | ||
| 204 | } | ||
| 205 | // The index and kind a shader reads from `words` (0 and 0 without the tag or when dword 3 names no image). | ||
| 206 | inline constexpr std::uint32_t tsharp_index_of(const ImageWords& words) { | ||
| 207 | return has_tsharp_index(words) && (words[3] >> 31) ? words[kTsharpIndexDword] & 0xFFFFu : 0u; | ||
| 208 | } | ||
| 209 | inline constexpr std::uint32_t tsharp_kind_of(const ImageWords& words) { | ||
| 210 | if (!has_tsharp_index(words) || !(words[3] >> 31)) return 0; | ||
| 211 | const std::uint32_t w = words[kTsharpIndexDword]; | ||
| 212 | return ((w >> 16) & 0x7Fu) | (((w >> 23) & 0x1Fu) << 8); | ||
| 213 | } | ||
| 214 | // The key of a T# the layer wrote into one of its own heaps: dword 6 as encode() writes it (0) when it carries the | ||
| 215 | // index. Only for words read back from the layer's heaps; a title's T# keeps whatever its dword 6 holds. | ||
| 216 | inline constexpr ImageWords tsharp_key(ImageWords words) { | ||
| 217 | if (has_tsharp_index(words)) words[kTsharpIndexDword] = 0; | ||
| 218 | return words; | ||
| 219 | } | ||
| 220 | |||
| 182 | inline constexpr float image_min_lod(const ImageWords& words) { | 221 | inline constexpr float image_min_lod(const ImageWords& words) { |
| 183 | return float(detail::get(words[1], 8, 12)) / 256.0f; | 222 | return float(detail::get(words[1], 8, 12)) / 256.0f; |
| 184 | } | 223 | } |
diff --git a/runtime/src/shader_library.h b/runtime/src/shader_library.h index 87fd5058e..16cdff14d 100644 --- a/runtime/src/shader_library.h +++ b/runtime/src/shader_library.h | |||
| @@ -923,6 +923,19 @@ inline DescriptorTableLayout majority_descriptor_table_layout(const DescriptorTa | |||
| 923 | return census.tagged > 0 ? DescriptorTableLayout::tagged : DescriptorTableLayout::plain; | 923 | return census.tagged > 0 ? DescriptorTableLayout::tagged : DescriptorTableLayout::plain; |
| 924 | } | 924 | } |
| 925 | 925 | ||
| 926 | // Which modules read a T#'s bindless index from its dword 6 (gfx10_descriptors.h, tsharp_index_word; tools/rdna2spirv.py | ||
| 927 | // TSHARP_INDEX): index.json meta `"tsharp_index": "dword6"`. Such a module takes the index of a T# it loaded from a | ||
| 928 | // heap from that word and nothing else, so the layer must write it into every heap it keeps; a module translated | ||
| 929 | // before has no key and finds its T#s by their words in the hash table, which the written word changes (the copy's | ||
| 930 | // dword 6 is no longer the title's). The device writes the word when any module reads it, and says so when modules | ||
| 931 | // of both kinds are loaded: the older ones then miss the T#s of the heaps and read the fallback, so a library is to be | ||
| 932 | // translated by one translator. | ||
| 933 | struct TsharpIndexCensus { | ||
| 934 | std::size_t dword6 = 0; | ||
| 935 | std::size_t none = 0; | ||
| 936 | std::size_t total() const { return dword6 + none; } | ||
| 937 | }; | ||
| 938 | |||
| 926 | // LEONIDA_DESCRIPTOR_TABLE=plain|tagged overrides the modules' vote. False when `value` is null, empty or not one | 939 | // LEONIDA_DESCRIPTOR_TABLE=plain|tagged overrides the modules' vote. False when `value` is null, empty or not one |
| 927 | // of the two layouts (an unrecognised value is ignored rather than failing startup). | 940 | // of the two layouts (an unrecognised value is ignored rather than failing startup). |
| 928 | inline bool descriptor_table_layout_from_env(const char* value, DescriptorTableLayout* layout) { | 941 | inline bool descriptor_table_layout_from_env(const char* value, DescriptorTableLayout* layout) { |
| @@ -1028,6 +1041,7 @@ public: | |||
| 1028 | exe_count_ = 0; | 1041 | exe_count_ = 0; |
| 1029 | descriptor_hash_census_ = {}; | 1042 | descriptor_hash_census_ = {}; |
| 1030 | descriptor_table_census_ = {}; | 1043 | descriptor_table_census_ = {}; |
| 1044 | tsharp_index_census_ = {}; | ||
| 1031 | vsharp_base_census_ = {}; | 1045 | vsharp_base_census_ = {}; |
| 1032 | for (const char* folder : {"fspx", "fspx_gfx"}) { | 1046 | for (const char* folder : {"fspx", "fspx_gfx"}) { |
| 1033 | try { | 1047 | try { |
| @@ -1057,6 +1071,8 @@ public: | |||
| 1057 | DescriptorTableLayout descriptor_table_layout() const { | 1071 | DescriptorTableLayout descriptor_table_layout() const { |
| 1058 | return majority_descriptor_table_layout(descriptor_table_census_); | 1072 | return majority_descriptor_table_layout(descriptor_table_census_); |
| 1059 | } | 1073 | } |
| 1074 | // The modules that read a T#'s index from its dword 6 (TsharpIndexCensus) and those that do not. | ||
| 1075 | const TsharpIndexCensus& tsharp_index_census() const { return tsharp_index_census_; } | ||
| 1060 | // How the loaded modules read a V#'s 48-bit base: the device names buffers below heap_base (negative offsets) only | 1076 | // How the loaded modules read a V#'s 48-bit base: the device names buffers below heap_base (negative offsets) only |
| 1061 | // when every one of them sign-extends it. | 1077 | // when every one of them sign-extends it. |
| 1062 | const VsharpBaseCensus& vsharp_base_census() const { return vsharp_base_census_; } | 1078 | const VsharpBaseCensus& vsharp_base_census() const { return vsharp_base_census_; } |
| @@ -1240,6 +1256,7 @@ private: | |||
| 1240 | exe_count_ = 0; | 1256 | exe_count_ = 0; |
| 1241 | descriptor_hash_census_ = {}; | 1257 | descriptor_hash_census_ = {}; |
| 1242 | descriptor_table_census_ = {}; | 1258 | descriptor_table_census_ = {}; |
| 1259 | tsharp_index_census_ = {}; | ||
| 1243 | vsharp_base_census_ = {}; | 1260 | vsharp_base_census_ = {}; |
| 1244 | } | 1261 | } |
| 1245 | for (const auto& [name, meta] : root.object) { | 1262 | for (const auto& [name, meta] : root.object) { |
| @@ -1343,6 +1360,11 @@ private: | |||
| 1343 | ++descriptor_table_census_.tagged; | 1360 | ++descriptor_table_census_.tagged; |
| 1344 | else | 1361 | else |
| 1345 | ++descriptor_table_census_.plain; | 1362 | ++descriptor_table_census_.plain; |
| 1363 | // Whether this module reads a T#'s index from its dword 6 (TsharpIndexCensus). Absent is the hash lookup. | ||
| 1364 | if (const Json* ti = meta.find("tsharp_index"); ti && ti->kind == Json::Kind::string && ti->string == "dword6") | ||
| 1365 | ++tsharp_index_census_.dword6; | ||
| 1366 | else | ||
| 1367 | ++tsharp_index_census_.none; | ||
| 1346 | // How this module reads a V#'s 48-bit base (VsharpBaseCensus). Absent is the old zero extension. | 1368 | // How this module reads a V#'s 48-bit base (VsharpBaseCensus). Absent is the old zero extension. |
| 1347 | if (const Json* sx = meta.find("vsharp_base_sign_extended"); sx && sx->kind == Json::Kind::boolean && sx->boolean) | 1369 | if (const Json* sx = meta.find("vsharp_base_sign_extended"); sx && sx->kind == Json::Kind::boolean && sx->boolean) |
| 1348 | ++vsharp_base_census_.sign_extended; | 1370 | ++vsharp_base_census_.sign_extended; |
| @@ -1372,6 +1394,7 @@ private: | |||
| 1372 | Entries fspx_entries_; | 1394 | Entries fspx_entries_; |
| 1373 | DescriptorHashCensus descriptor_hash_census_; | 1395 | DescriptorHashCensus descriptor_hash_census_; |
| 1374 | DescriptorTableCensus descriptor_table_census_; | 1396 | DescriptorTableCensus descriptor_table_census_; |
| 1397 | TsharpIndexCensus tsharp_index_census_; | ||
| 1375 | VsharpBaseCensus vsharp_base_census_; | 1398 | VsharpBaseCensus vsharp_base_census_; |
| 1376 | }; | 1399 | }; |
| 1377 | 1400 | ||
diff --git a/runtime/src/title_heap.h b/runtime/src/title_heap.h new file mode 100644 index 000000000..92f7856f5 --- /dev/null +++ b/runtime/src/title_heap.h | |||
| @@ -0,0 +1,207 @@ | |||
| 1 | #pragma once | ||
| 2 | // The XMemAlloc heap (title_heap): the title's XMemAlloc / XMemAllocDefault blocks, carved from one reservation that | ||
| 3 | // xmem places in the page mirror's range (src/title_mirror_space.h), so a V# or pointer the title writes into a block | ||
| 4 | // reaches the mirror at heap_base + the block's address. Windows' RtlCreateHeap over a given base faults under Wine | ||
| 5 | // (it writes into the reserved base), so the heap is the runtime's own. | ||
| 6 | // | ||
| 7 | // What the title sees, as before: every block 64-byte aligned, a size of zero one byte, blocks that never overlap, | ||
| 8 | // XMemFree of a block from either entry point. There is no realloc and no size query in the title's ABI; size_of | ||
| 9 | // answers xmem's own bookkeeping (the bytes a free tells the readers about). | ||
| 10 | // | ||
| 11 | // The layout: | ||
| 12 | // - the reservation is handed out in 64 KiB pages by a first-fit list of free runs, coalesced on free; | ||
| 13 | // - blocks up to kSmallMax come from spans of kSpan bytes, one size class a span (multiples of 64 bytes up to 1 KiB, | ||
| 14 | // then powers of two), each class with a stack of free blocks kept outside the title's memory (a freed block's | ||
| 15 | // bytes are never written by the heap); | ||
| 16 | // - larger blocks are runs of whole pages, committed when handed out and decommitted when freed (they read zero when | ||
| 17 | // handed out again, as HeapAlloc's large blocks do). | ||
| 18 | // Pages are committed through the hooks (xmem: VirtualAlloc, then the page mirror's feed). One lock, taken for every | ||
| 19 | // call; nothing the title's memory holds is read or written under it. Portable: the hooks do the system calls. | ||
| 20 | #include <algorithm> | ||
| 21 | #include <cstddef> | ||
| 22 | #include <cstdint> | ||
| 23 | #include <functional> | ||
| 24 | #include <map> | ||
| 25 | #include <mutex> | ||
| 26 | #include <vector> | ||
| 27 | |||
| 28 | namespace leonida { | ||
| 29 | |||
| 30 | class TitleHeap { | ||
| 31 | public: | ||
| 32 | static constexpr std::size_t kAlign = 64; | ||
| 33 | static constexpr std::size_t kPage = 0x10000; | ||
| 34 | static constexpr std::size_t kSpan = std::size_t(1) << 20; | ||
| 35 | static constexpr std::size_t kSmallMax = 32768; | ||
| 36 | |||
| 37 | struct Hooks { | ||
| 38 | std::function<bool(std::uintptr_t, std::size_t)> commit; // [at, at + bytes) usable from now on | ||
| 39 | std::function<void(std::uintptr_t, std::size_t)> decommit; // [at, at + bytes) given back (reads zero later) | ||
| 40 | }; | ||
| 41 | struct Stats { | ||
| 42 | std::size_t committed = 0, spans = 0, large = 0, live = 0; | ||
| 43 | }; | ||
| 44 | |||
| 45 | bool init(std::uintptr_t base, std::size_t bytes, Hooks hooks) { | ||
| 46 | std::lock_guard lock(mutex_); | ||
| 47 | if (!base || bytes < kSpan || (base % kPage)) return false; | ||
| 48 | base_ = base; | ||
| 49 | bytes_ = bytes - bytes % kPage; | ||
| 50 | hooks_ = std::move(hooks); | ||
| 51 | free_runs_.clear(); | ||
| 52 | free_runs_[base_] = bytes_; | ||
| 53 | span_class_.assign(bytes_ / kSpan, kNoClass); | ||
| 54 | return true; | ||
| 55 | } | ||
| 56 | bool owns(const void* p) const { | ||
| 57 | const auto at = reinterpret_cast<std::uintptr_t>(p); | ||
| 58 | return base_ && at >= base_ && at - base_ < bytes_; | ||
| 59 | } | ||
| 60 | |||
| 61 | void* allocate(std::size_t size) { | ||
| 62 | if (!size) size = 1; | ||
| 63 | std::lock_guard lock(mutex_); | ||
| 64 | if (!base_) return nullptr; | ||
| 65 | if (size <= kSmallMax) { | ||
| 66 | const unsigned c = class_of(size); | ||
| 67 | Class& cls = classes_[c]; | ||
| 68 | if (cls.free.empty() && !grow(c)) return nullptr; | ||
| 69 | const std::uintptr_t at = cls.free.back(); | ||
| 70 | cls.free.pop_back(); | ||
| 71 | ++stats_.live; | ||
| 72 | return reinterpret_cast<void*>(at); | ||
| 73 | } | ||
| 74 | const std::size_t bytes = (size + kPage - 1) & ~(kPage - 1); | ||
| 75 | if (bytes < size) return nullptr; | ||
| 76 | const std::uintptr_t at = take_run(bytes); | ||
| 77 | if (!at) return nullptr; | ||
| 78 | if (hooks_.commit && !hooks_.commit(at, bytes)) { | ||
| 79 | give_run(at, bytes); | ||
| 80 | return nullptr; | ||
| 81 | } | ||
| 82 | large_[at] = bytes; | ||
| 83 | stats_.committed += bytes; | ||
| 84 | ++stats_.large; | ||
| 85 | ++stats_.live; | ||
| 86 | return reinterpret_cast<void*>(at); | ||
| 87 | } | ||
| 88 | |||
| 89 | // The bytes the block at `p` holds (its class's size, or its pages): 0 for a pointer that is not a live block's. | ||
| 90 | std::size_t size_of(const void* p) const { | ||
| 91 | const auto at = reinterpret_cast<std::uintptr_t>(p); | ||
| 92 | std::lock_guard lock(mutex_); | ||
| 93 | if (!owns(p)) return 0; | ||
| 94 | if (const auto it = large_.find(at); it != large_.end()) return it->second; | ||
| 95 | const unsigned c = span_class_[(at - base_) / kSpan]; | ||
| 96 | return c == kNoClass ? 0 : class_size(c); | ||
| 97 | } | ||
| 98 | |||
| 99 | // Frees a block allocate() returned. False (nothing done) for any other pointer. | ||
| 100 | bool release(void* p) { | ||
| 101 | const auto at = reinterpret_cast<std::uintptr_t>(p); | ||
| 102 | std::lock_guard lock(mutex_); | ||
| 103 | if (!owns(p)) return false; | ||
| 104 | if (const auto it = large_.find(at); it != large_.end()) { | ||
| 105 | const std::size_t bytes = it->second; | ||
| 106 | large_.erase(it); | ||
| 107 | if (hooks_.decommit) hooks_.decommit(at, bytes); | ||
| 108 | stats_.committed -= bytes; | ||
| 109 | --stats_.large; | ||
| 110 | --stats_.live; | ||
| 111 | give_run(at, bytes); | ||
| 112 | return true; | ||
| 113 | } | ||
| 114 | const unsigned c = span_class_[(at - base_) / kSpan]; | ||
| 115 | if (c == kNoClass || (at - base_) % kSpan % class_size(c)) return false; | ||
| 116 | classes_[c].free.push_back(at); | ||
| 117 | --stats_.live; | ||
| 118 | return true; | ||
| 119 | } | ||
| 120 | |||
| 121 | // f(at, bytes) for each committed range (a span or a large block), for a reader that starts late. | ||
| 122 | template <class F> | ||
| 123 | void for_each_committed(F&& f) const { | ||
| 124 | std::lock_guard lock(mutex_); | ||
| 125 | for (std::size_t s = 0; s < span_class_.size(); ++s) | ||
| 126 | if (span_class_[s] != kNoClass) f(base_ + s * kSpan, kSpan); | ||
| 127 | for (const auto& [at, bytes] : large_) f(at, bytes); | ||
| 128 | } | ||
| 129 | |||
| 130 | Stats stats() const { | ||
| 131 | std::lock_guard lock(mutex_); | ||
| 132 | return stats_; | ||
| 133 | } | ||
| 134 | |||
| 135 | static constexpr unsigned kClasses = 16 + 5; // 64..1024 by 64, then 2, 4, 8, 16, 32 KiB | ||
| 136 | static std::size_t class_size(unsigned c) { return c < 16 ? (c + 1) * 64 : std::size_t(1024) << (c - 15); } | ||
| 137 | static unsigned class_of(std::size_t size) { | ||
| 138 | if (size <= 1024) return unsigned((size + 63) / 64 - 1); | ||
| 139 | unsigned c = 16; | ||
| 140 | while (class_size(c) < size) ++c; | ||
| 141 | return c; | ||
| 142 | } | ||
| 143 | |||
| 144 | private: | ||
| 145 | static constexpr unsigned kNoClass = 0xff; | ||
| 146 | struct Class { | ||
| 147 | std::vector<std::uintptr_t> free; | ||
| 148 | }; | ||
| 149 | |||
| 150 | // A new span for class `c`: committed, cut into blocks pushed on its stack (in address order: the lowest first). | ||
| 151 | bool grow(unsigned c) { | ||
| 152 | const std::uintptr_t at = take_run(kSpan, kSpan); | ||
| 153 | if (!at) return false; | ||
| 154 | if (hooks_.commit && !hooks_.commit(at, kSpan)) { | ||
| 155 | give_run(at, kSpan); | ||
| 156 | return false; | ||
| 157 | } | ||
| 158 | span_class_[(at - base_) / kSpan] = std::uint8_t(c); | ||
| 159 | stats_.committed += kSpan; | ||
| 160 | ++stats_.spans; | ||
| 161 | const std::size_t size = class_size(c); | ||
| 162 | for (std::size_t off = kSpan / size * size; off >= size; off -= size) classes_[c].free.push_back(at + off - size); | ||
| 163 | return true; | ||
| 164 | } | ||
| 165 | // The first free run that holds `bytes` at an offset from the base aligned to `align` (spans are numbered from the | ||
| 166 | // base, which is only page-aligned). | ||
| 167 | std::uintptr_t take_run(std::size_t bytes, std::size_t align = kPage) { | ||
| 168 | for (auto it = free_runs_.begin(); it != free_runs_.end(); ++it) { | ||
| 169 | const std::uintptr_t start = base_ + ((it->first - base_ + align - 1) & ~(std::uintptr_t(align) - 1)); | ||
| 170 | const std::uintptr_t end = it->first + it->second; | ||
| 171 | if (start >= end || end - start < bytes) continue; | ||
| 172 | const std::uintptr_t run = it->first, run_end = end; | ||
| 173 | free_runs_.erase(it); | ||
| 174 | if (start > run) free_runs_[run] = start - run; | ||
| 175 | if (start + bytes < run_end) free_runs_[start + bytes] = run_end - (start + bytes); | ||
| 176 | return start; | ||
| 177 | } | ||
| 178 | return 0; | ||
| 179 | } | ||
| 180 | void give_run(std::uintptr_t at, std::size_t bytes) { | ||
| 181 | auto next = free_runs_.lower_bound(at); | ||
| 182 | if (next != free_runs_.end() && at + bytes == next->first) { | ||
| 183 | bytes += next->second; | ||
| 184 | next = free_runs_.erase(next); | ||
| 185 | } | ||
| 186 | if (next != free_runs_.begin()) { | ||
| 187 | const auto previous = std::prev(next); | ||
| 188 | if (previous->first + previous->second == at) { | ||
| 189 | previous->second += bytes; | ||
| 190 | return; | ||
| 191 | } | ||
| 192 | } | ||
| 193 | free_runs_[at] = bytes; | ||
| 194 | } | ||
| 195 | |||
| 196 | mutable std::mutex mutex_; | ||
| 197 | std::uintptr_t base_ = 0; | ||
| 198 | std::size_t bytes_ = 0; | ||
| 199 | Hooks hooks_; | ||
| 200 | std::map<std::uintptr_t, std::size_t> free_runs_; // start -> bytes | ||
| 201 | std::map<std::uintptr_t, std::size_t> large_; // a large block -> its bytes | ||
| 202 | std::vector<std::uint8_t> span_class_; // per span: its class, or kNoClass | ||
| 203 | Class classes_[kClasses]; | ||
| 204 | Stats stats_; | ||
| 205 | }; | ||
| 206 | |||
| 207 | } // namespace leonida | ||
diff --git a/runtime/src/title_mirror.h b/runtime/src/title_mirror.h index bac0fdc89..90ebd3205 100644 --- a/runtime/src/title_mirror.h +++ b/runtime/src/title_mirror.h | |||
| @@ -23,6 +23,8 @@ | |||
| 23 | // serialises (the device under its mirror mutex). | 23 | // serialises (the device under its mirror mutex). |
| 24 | #include <vulkan/vulkan.h> | 24 | #include <vulkan/vulkan.h> |
| 25 | 25 | ||
| 26 | #include "shadow_keep.h" | ||
| 27 | |||
| 26 | #include <cstdint> | 28 | #include <cstdint> |
| 27 | #include <cstring> | 29 | #include <cstring> |
| 28 | #include <deque> | 30 | #include <deque> |
| @@ -63,6 +65,9 @@ public: | |||
| 63 | std::uint64_t binds = 0, unbinds = 0, batches = 0, uploads = 0, upload_failures = 0, homes_freed = 0; // so far | 65 | std::uint64_t binds = 0, unbinds = 0, batches = 0, uploads = 0, upload_failures = 0, homes_freed = 0; // so far |
| 64 | std::uint64_t unmirrored_changes = 0; // feed entries outside every chunk | 66 | std::uint64_t unmirrored_changes = 0; // feed entries outside every chunk |
| 65 | std::uint64_t filtered = 0; // fills through hooks.filter (filter_range), of the uploads | 67 | std::uint64_t filtered = 0; // fills through hooks.filter (filter_range), of the uploads |
| 68 | // Bytes the uploads copied into homes, bytes write_back() copied to the title's pages, and 256-byte pieces | ||
| 69 | // both sides had changed (the title's bytes kept), so far. | ||
| 70 | std::uint64_t uploaded_bytes = 0, written_back_bytes = 0, conflicts = 0; | ||
| 66 | }; | 71 | }; |
| 67 | 72 | ||
| 68 | explicit TitleMirror(TitleMirrorHooks hooks) : hooks_(std::move(hooks)) {} | 73 | explicit TitleMirror(TitleMirrorHooks hooks) : hooks_(std::move(hooks)) {} |
| @@ -211,13 +216,53 @@ public: | |||
| 211 | const auto home = homes_.find(frame); | 216 | const auto home = homes_.find(frame); |
| 212 | if (home == homes_.end() || !home->second.dirty) continue; | 217 | if (home == homes_.end() || !home->second.dirty) continue; |
| 213 | home->second.dirty = false; | 218 | home->second.dirty = false; |
| 214 | fill(home->second, frame); | 219 | fill(home->second, frame, false); |
| 215 | ++n; | 220 | ++n; |
| 216 | } | 221 | } |
| 217 | dirty_.clear(); | 222 | dirty_.clear(); |
| 218 | return n; | 223 | return n; |
| 219 | } | 224 | } |
| 220 | 225 | ||
| 226 | // What the GPU wrote into the homes of the mirrored pages of [begin, end) goes to the title's pages, merged at the | ||
| 227 | // 256-byte piece against the sums both sides last agreed on: a piece only the GPU changed is written back with | ||
| 228 | // `write(va, bytes, n)` (and agreed on again); a piece only the title changed waits for the upload; a piece both | ||
| 229 | // changed keeps the title's bytes and is counted. Call it once the submissions that could write the range have | ||
| 230 | // completed. Returns the bytes written back. | ||
| 231 | template <class Write> | ||
| 232 | std::uint64_t write_back(std::uint64_t begin, std::uint64_t end, Write&& write) { | ||
| 233 | std::uint64_t written = 0; | ||
| 234 | alignas(64) std::uint8_t title[kPiece]; | ||
| 235 | for (std::uint64_t page = begin & ~(kPage - 1); page < end; page += kPage) { | ||
| 236 | const auto mapped = pages_.find(page); | ||
| 237 | if (mapped == pages_.end()) continue; | ||
| 238 | const auto found = homes_.find(mapped->second); | ||
| 239 | if (found == homes_.end()) continue; | ||
| 240 | Home& home = found->second; | ||
| 241 | // A filtered home holds the filter's bytes, not the title's: nothing of it is ever written back. | ||
| 242 | if (home.filter_va) continue; | ||
| 243 | const std::uint8_t* gpu = units_[home.unit].host + std::size_t(home.slot) * kPage; | ||
| 244 | const std::size_t k0 = std::size_t((std::max(begin, page) - page) / kPiece); | ||
| 245 | const std::size_t k1 = std::size_t((std::min(end, page + kPage) - page + kPiece - 1) / kPiece); | ||
| 246 | for (std::size_t k = k0; k < k1; ++k) { | ||
| 247 | const std::uint64_t agreed = home.sums[k]; | ||
| 248 | const std::uint64_t now = shadow_keep::sum_of(gpu + k * kPiece, kPiece); | ||
| 249 | if (now == agreed) continue; // the GPU did not change it | ||
| 250 | const std::uint8_t* from = mapped->second + k * kPiece; | ||
| 251 | if (!(hooks_.copy ? hooks_.copy(title, from, kPiece) : (std::memcpy(title, from, kPiece), true))) | ||
| 252 | continue; // the title's page went away | ||
| 253 | if (shadow_keep::sum_of(title, kPiece) != agreed) { | ||
| 254 | ++counters_.conflicts; // both changed it: the title's bytes win at the next upload | ||
| 255 | continue; | ||
| 256 | } | ||
| 257 | if (!write(page + k * kPiece, gpu + k * kPiece, kPiece)) continue; | ||
| 258 | home.sums[k] = now; | ||
| 259 | written += kPiece; | ||
| 260 | } | ||
| 261 | } | ||
| 262 | counters_.written_back_bytes += written; | ||
| 263 | return written; | ||
| 264 | } | ||
| 265 | |||
| 221 | // The binds noted since the last flush, as one batch signalling `value`. False when there were none (nothing to | 266 | // The binds noted since the last flush, as one batch signalling `value`. False when there were none (nothing to |
| 222 | // wait for) or the bind failed (counted; the caller logs). | 267 | // wait for) or the bind failed (counted; the caller logs). |
| 223 | bool flush(std::uint64_t value) { | 268 | bool flush(std::uint64_t value) { |
| @@ -296,10 +341,15 @@ public: | |||
| 296 | } | 341 | } |
| 297 | 342 | ||
| 298 | private: | 343 | private: |
| 344 | // A home and the sum of each of its 256-byte pieces as both sides last agreed on it (src/shadow_keep.h): what was | ||
| 345 | // filled from the title's page, or written back to it. A piece whose home differs from its sum was written by the | ||
| 346 | // GPU, one whose title page differs by the title. | ||
| 347 | static constexpr std::size_t kPiece = shadow_keep::kPiece, kPieces = kPage / kPiece; | ||
| 299 | struct Home { | 348 | struct Home { |
| 300 | std::uint32_t unit = 0, slot = 0, refs = 0; | 349 | std::uint32_t unit = 0, slot = 0, refs = 0; |
| 301 | std::shared_ptr<void> keep; | 350 | std::shared_ptr<void> keep; |
| 302 | bool dirty = false; | 351 | bool dirty = false; |
| 352 | std::unique_ptr<std::uint64_t[]> sums = std::make_unique<std::uint64_t[]>(kPieces); | ||
| 303 | std::uint64_t filter_va = 0; // a filtered page that shows it (filter_range), 0: filled by copy | 353 | std::uint64_t filter_va = 0; // a filtered page that shows it (filter_range), 0: filled by copy |
| 304 | bool fresh = true; // the next fill writes every byte (hooks.filter's `fresh`) | 354 | bool fresh = true; // the next fill writes every byte (hooks.filter's `fresh`) |
| 305 | }; | 355 | }; |
| @@ -318,20 +368,58 @@ private: | |||
| 318 | std::uint64_t ticket; | 368 | std::uint64_t ticket; |
| 319 | }; | 369 | }; |
| 320 | 370 | ||
| 321 | void fill(Home& home, const std::uint8_t* frame) { | 371 | // Brings the home up to date with the title's page, piece by piece: `whole` (a new home) copies every piece, and |
| 372 | // otherwise only a piece the title changed since both sides last agreed, so a piece only the GPU wrote keeps the | ||
| 373 | // GPU's bytes (and goes back with write_back). A piece both changed takes the title's bytes. | ||
| 374 | void fill(Home& home, const std::uint8_t* frame, bool whole) { | ||
| 322 | std::uint8_t* to = units_[home.unit].host + std::size_t(home.slot) * kPage; | 375 | std::uint8_t* to = units_[home.unit].host + std::size_t(home.slot) * kPage; |
| 323 | const bool fresh = home.fresh; | 376 | alignas(64) std::uint8_t title[kPiece]; |
| 324 | home.fresh = false; | 377 | bool failed = false; |
| 325 | const bool ok = home.filter_va && hooks_.filter ? hooks_.filter(home.filter_va, to, frame, kPage, fresh) | 378 | if (home.filter_va && hooks_.filter) { |
| 326 | : hooks_.copy ? hooks_.copy(to, frame, kPage) | 379 | // A filtered page (a console heap read in place): its sums are of the title's bytes the filter last read, |
| 327 | : (std::memcpy(to, frame, kPage), true); | 380 | // so the compare below is the title's bytes against the title's bytes; the filter, after it, writes the |
| 328 | if (!ok) { | 381 | // home - every byte when fresh, else the slots that changed - whenever a piece of the title's page did. |
| 329 | std::memset(to, 0, kPage); | 382 | // The home's bytes are the filter's, never compared with a sum (write_back skips it). |
| 330 | home.fresh = true; // zeros now: the next fill writes all again | 383 | const bool fresh = whole || home.fresh; |
| 331 | ++counters_.upload_failures; | 384 | std::size_t changed = 0; |
| 385 | for (std::size_t k = 0; k < kPieces; ++k) { | ||
| 386 | const std::uint8_t* from = frame + k * kPiece; | ||
| 387 | if (!(hooks_.copy ? hooks_.copy(title, from, kPiece) : (std::memcpy(title, from, kPiece), true))) | ||
| 388 | std::memset(title, 0, kPiece); | ||
| 389 | const std::uint64_t sum = shadow_keep::sum_of(title, kPiece); | ||
| 390 | if (sum == home.sums[k]) continue; | ||
| 391 | home.sums[k] = sum; | ||
| 392 | ++changed; | ||
| 393 | } | ||
| 394 | if (fresh || changed) { | ||
| 395 | home.fresh = false; | ||
| 396 | if (!hooks_.filter(home.filter_va, to, frame, kPage, fresh)) { | ||
| 397 | std::memset(to, 0, kPage); | ||
| 398 | home.fresh = true; // zeros now: the next fill writes all again | ||
| 399 | failed = true; | ||
| 400 | } | ||
| 401 | counters_.uploaded_bytes += (fresh ? kPieces : changed) * kPiece; | ||
| 402 | ++counters_.filtered; | ||
| 403 | } | ||
| 404 | } else { | ||
| 405 | // A home filtered until now (or zeroed by a failed fill) is copied whole once. | ||
| 406 | const bool all = whole || home.fresh; | ||
| 407 | home.fresh = false; | ||
| 408 | for (std::size_t k = 0; k < kPieces; ++k) { | ||
| 409 | const std::uint8_t* from = frame + k * kPiece; | ||
| 410 | if (!(hooks_.copy ? hooks_.copy(title, from, kPiece) : (std::memcpy(title, from, kPiece), true))) { | ||
| 411 | std::memset(title, 0, kPiece); | ||
| 412 | failed = true; | ||
| 413 | } | ||
| 414 | const std::uint64_t sum = shadow_keep::sum_of(title, kPiece); | ||
| 415 | if (!all && sum == home.sums[k]) continue; | ||
| 416 | std::memcpy(to + k * kPiece, title, kPiece); | ||
| 417 | home.sums[k] = sum; | ||
| 418 | counters_.uploaded_bytes += kPiece; | ||
| 419 | } | ||
| 332 | } | 420 | } |
| 421 | if (failed) ++counters_.upload_failures; | ||
| 333 | ++counters_.uploads; | 422 | ++counters_.uploads; |
| 334 | if (home.filter_va) ++counters_.filtered; | ||
| 335 | } | 423 | } |
| 336 | 424 | ||
| 337 | Home* home_of(std::uint8_t* frame, std::shared_ptr<void> keep, std::uint64_t va) { | 425 | Home* home_of(std::uint8_t* frame, std::shared_ptr<void> keep, std::uint64_t va) { |
| @@ -346,7 +434,7 @@ private: | |||
| 346 | home.slot = slot; | 434 | home.slot = slot; |
| 347 | home.keep = std::move(keep); | 435 | home.keep = std::move(keep); |
| 348 | home.filter_va = filtered(va) ? va : 0; | 436 | home.filter_va = filtered(va) ? va : 0; |
| 349 | fill(home, frame); | 437 | fill(home, frame, true); |
| 350 | return &home; | 438 | return &home; |
| 351 | } | 439 | } |
| 352 | 440 | ||
diff --git a/runtime/test/d3d12_heap_in_place_test.cpp b/runtime/test/d3d12_heap_in_place_test.cpp index 910857242..af171a9eb 100644 --- a/runtime/test/d3d12_heap_in_place_test.cpp +++ b/runtime/test/d3d12_heap_in_place_test.cpp | |||
| @@ -8,7 +8,10 @@ | |||
| 8 | // 2. a slot the title rewrites (through its own view: a guard fault) reaches the GPU at the next refresh, filtered; | 8 | // 2. a slot the title rewrites (through its own view: a guard fault) reaches the GPU at the next refresh, filtered; |
| 9 | // the title's bytes are again as written; | 9 | // the title's bytes are again as written; |
| 10 | // 3. the null slots after the heap read zero, and the heap's address is below 4 GiB; | 10 | // 3. the null slots after the heap read zero, and the heap's address is below 4 GiB; |
| 11 | // 4. no copy: the device names it "read in place". | 11 | // 4. no copy: the device names it "read in place"; |
| 12 | // 5. a T# in the heap (the words CreateShaderResourceView wrote for a texture of ours) reads with the bindless index | ||
| 13 | // in its dword 6 (gfx10_descriptors.h, tsharp_index_word: the tag and the texture's index) once the filter and its | ||
| 14 | // finishing have run, while the title's bytes keep the dword 6 the title wrote. | ||
| 12 | // Exit 77 when the device makes no page mirror (llvmpipe). No shaders, no game data. | 15 | // Exit 77 when the device makes no page mirror (llvmpipe). No shaders, no game data. |
| 13 | #include "../shims/d3d12_com.h" | 16 | #include "../shims/d3d12_com.h" |
| 14 | #include "../shims/d3d12_xs_device.h" | 17 | #include "../shims/d3d12_xs_device.h" |
| @@ -102,11 +105,40 @@ int test() { | |||
| 102 | const auto put = [&](unsigned slot, const leonida::gfx10::BufferWords& w) { | 105 | const auto put = [&](unsigned slot, const leonida::gfx10::BufferWords& w) { |
| 103 | std::memcpy(title + slot * kDescriptor, w.data(), sizeof(w)); | 106 | std::memcpy(title + slot * kDescriptor, w.data(), sizeof(w)); |
| 104 | }; | 107 | }; |
| 108 | // A texture of ours and its T#, as CreateShaderResourceView writes it into a CPU heap (before the index word is on: | ||
| 109 | // dword 6 as encode() writes it). The index word is then on, as a library of modules that read it turns it on. | ||
| 110 | D3D12_RESOURCE_DESC tex_desc{}; | ||
| 111 | tex_desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; | ||
| 112 | tex_desc.Width = 64; | ||
| 113 | tex_desc.Height = 64; | ||
| 114 | tex_desc.DepthOrArraySize = 1; | ||
| 115 | tex_desc.MipLevels = 1; | ||
| 116 | tex_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; | ||
| 117 | tex_desc.SampleDesc.Count = 1; | ||
| 118 | Ref<ID3D12Resource> texture; | ||
| 119 | EXPECT(SUCCEEDED(device.p->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &tex_desc, | ||
| 120 | D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, nullptr, | ||
| 121 | IID_PPV_ARGS(&texture.p)))); | ||
| 122 | const D3D12_DESCRIPTOR_HEAP_DESC cpu_desc{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 1, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, | ||
| 123 | 0}; | ||
| 124 | Ref<ID3D12DescriptorHeap> cpu_heap; | ||
| 125 | EXPECT(SUCCEEDED(device.p->CreateDescriptorHeap(&cpu_desc, IID_PPV_ARGS(&cpu_heap.p)))); | ||
| 126 | const D3D12_CPU_DESCRIPTOR_HANDLE cpu = cpu_heap.p->GetCPUDescriptorHandleForHeapStart(); | ||
| 127 | device.p->CreateShaderResourceView(texture.p, nullptr, cpu); | ||
| 128 | std::uint32_t tsharp[8]; | ||
| 129 | std::memcpy(tsharp, reinterpret_cast<const void*>(cpu.ptr), sizeof(tsharp)); | ||
| 130 | leonida::gfx10::ImageWords tsharp_words{}; | ||
| 131 | std::memcpy(tsharp_words.data(), tsharp, sizeof(tsharp_words)); | ||
| 132 | EXPECT(leonida::gfx10::is_image(tsharp_words) && !leonida::gfx10::has_tsharp_index(tsharp_words)); | ||
| 133 | tsharp[leonida::gfx10::kTsharpIndexDword] = 0x00C0FFEEu; // the title's own dword 6 (compression metadata) | ||
| 134 | rt::d3d12::set_tsharp_index_words_for_test(device.p, true); | ||
| 135 | |||
| 105 | put(0, raw_vsharp(arena_va, 256)); | 136 | put(0, raw_vsharp(arena_va, 256)); |
| 106 | put(1, raw_vsharp(nobody, 256)); | 137 | put(1, raw_vsharp(nobody, 256)); |
| 107 | put(2, raw_vsharp(mirrored, 256)); | 138 | put(2, raw_vsharp(mirrored, 256)); |
| 108 | const std::uint32_t data[8] = {0x12345000u, 0x00007777u, 0x00000400u, 0x40000000u, 1, 2, 3, 4}; | 139 | const std::uint32_t data[8] = {0x12345000u, 0x00007777u, 0x00000400u, 0x40000000u, 1, 2, 3, 4}; |
| 109 | std::memcpy(title + 3 * kDescriptor, data, sizeof(data)); | 140 | std::memcpy(title + 3 * kDescriptor, data, sizeof(data)); |
| 141 | std::memcpy(title + 6 * kDescriptor, tsharp, sizeof(tsharp)); | ||
| 110 | std::uint8_t written[5 * kDescriptor]; | 142 | std::uint8_t written[5 * kDescriptor]; |
| 111 | std::memcpy(written, title, sizeof(written)); | 143 | std::memcpy(written, title, sizeof(written)); |
| 112 | 144 | ||
| @@ -155,6 +187,18 @@ int test() { | |||
| 155 | EXPECT(rt::d3d12::page_mirror_write_back(device.p, std::uintptr_t(title), kPages * kPage64k) == 0); | 187 | EXPECT(rt::d3d12::page_mirror_write_back(device.p, std::uintptr_t(title), kPages * kPage64k) == 0); |
| 156 | for (unsigned i = 0; i < kDescriptor; ++i) EXPECT(title[5 * kDescriptor + i] == 0); | 188 | for (unsigned i = 0; i < kDescriptor; ++i) EXPECT(title[5 * kDescriptor + i] == 0); |
| 157 | 189 | ||
| 190 | // 5. The T#: dword 6 carries the tag and the texture's index, the other words as written; the title's dword 6 is | ||
| 191 | // its own. | ||
| 192 | std::uint32_t gpu_tsharp[8]{}; | ||
| 193 | EXPECT(rt::d3d12::arena_gpu_read(device.p, shader_va + 6 * kDescriptor, gpu_tsharp, sizeof(gpu_tsharp))); | ||
| 194 | const std::uint32_t word = gpu_tsharp[leonida::gfx10::kTsharpIndexDword]; | ||
| 195 | EXPECT((word >> 28) == leonida::gfx10::kTsharpIndexTag && (word & 0xFFFFu) != 0); | ||
| 196 | for (unsigned i = 0; i < 8; ++i) | ||
| 197 | if (i != leonida::gfx10::kTsharpIndexDword) EXPECT(gpu_tsharp[i] == tsharp[i]); | ||
| 198 | EXPECT(!std::memcmp(title + 6 * kDescriptor, tsharp, sizeof(tsharp))); | ||
| 199 | EXPECT(reinterpret_cast<const std::uint32_t*>(title + 6 * kDescriptor)[leonida::gfx10::kTsharpIndexDword] == | ||
| 200 | 0x00C0FFEEu); | ||
| 201 | |||
| 158 | // 3. The null slots after the heap. | 202 | // 3. The null slots after the heap. |
| 159 | std::uint32_t tail[64]{}; | 203 | std::uint32_t tail[64]{}; |
| 160 | std::memset(tail, 0xff, sizeof(tail)); | 204 | std::memset(tail, 0xff, sizeof(tail)); |
| @@ -162,8 +206,8 @@ int test() { | |||
| 162 | for (const std::uint32_t word : tail) EXPECT(word == 0); | 206 | for (const std::uint32_t word : tail) EXPECT(word == 0); |
| 163 | 207 | ||
| 164 | std::printf("heap in place: at heap_base + 0x%llx; the GPU reads a nobody's V# and a data word null and the others " | 208 | std::printf("heap in place: at heap_base + 0x%llx; the GPU reads a nobody's V# and a data word null and the others " |
| 165 | "as written, a rewrite at the next refresh, zeros after the heap; the title's bytes never change\n", | 209 | "as written, a rewrite at the next refresh, zeros after the heap, a T# with its index word (0x%08x); the title's bytes never change\n", |
| 166 | static_cast<unsigned long long>(shader_va)); | 210 | static_cast<unsigned long long>(shader_va), word); |
| 167 | heap.reset(); | 211 | heap.reset(); |
| 168 | EXPECT(map(title, kPages, nullptr) == title && free_pages(kPages, pages)); | 212 | EXPECT(map(title, kPages, nullptr) == title && free_pages(kPages, pages)); |
| 169 | return 0; | 213 | return 0; |
diff --git a/runtime/test/d3d12_page_mirror_test.cpp b/runtime/test/d3d12_page_mirror_test.cpp index 276528c4c..39a3461c9 100644 --- a/runtime/test/d3d12_page_mirror_test.cpp +++ b/runtime/test/d3d12_page_mirror_test.cpp | |||
| @@ -12,7 +12,11 @@ | |||
| 12 | // keeps the title's address as its base, the GPU reads the bytes and a later title write there, and a binding of | 12 | // keeps the title's address as its base, the GPU reads the bytes and a later title write there, and a binding of |
| 13 | // it is served by the mirror's chunk; | 13 | // it is served by the mirror's chunk; |
| 14 | // 6. a slot-133 buffer over mirrored memory is the mirror's chunk (no shadow); the GPU's writes into it go back to | 14 | // 6. a slot-133 buffer over mirrored memory is the mirror's chunk (no shadow); the GPU's writes into it go back to |
| 15 | // the title's page, and a page written on both sides keeps the title's bytes. | 15 | // the title's page, and a 256-byte piece written on both sides keeps the title's bytes; |
| 16 | // 7. one page, the GPU writes piece A and the title piece B in the same submission window (the title's write is | ||
| 17 | // uploaded before the GPU's goes back): both survive, on both sides; | ||
| 18 | // 8. XMemAlloc blocks lie in the mirror's range and are mirrored as the heap commits them: the GPU reads a small and | ||
| 19 | // a large block's bytes through a pointer into them that nothing named. | ||
| 16 | // Exit 77 when the device makes no page mirror (no sparse aliased strict buffers or capture-replay addresses: llvmpipe). | 20 | // Exit 77 when the device makes no page mirror (no sparse aliased strict buffers or capture-replay addresses: llvmpipe). |
| 17 | // No shaders, no game data. | 21 | // No shaders, no game data. |
| 18 | #include "../shims/d3d12_com.h" | 22 | #include "../shims/d3d12_com.h" |
| @@ -34,6 +38,8 @@ | |||
| 34 | namespace { | 38 | namespace { |
| 35 | 39 | ||
| 36 | using XMemVirtualAllocFn = void*(WINAPI*)(void*, SIZE_T, DWORD, ULONGLONG, DWORD); | 40 | using XMemVirtualAllocFn = void*(WINAPI*)(void*, SIZE_T, DWORD, ULONGLONG, DWORD); |
| 41 | using XMemAllocFn = void*(__stdcall*)(size_t, std::uint64_t); | ||
| 42 | using XMemFreeFn = void(__stdcall*)(void*, std::uint64_t); | ||
| 37 | using XMemAllocatePhysicalPagesFn = BOOL(WINAPI*)(std::uint64_t, std::uint64_t*, std::uint64_t*); | 43 | using XMemAllocatePhysicalPagesFn = BOOL(WINAPI*)(std::uint64_t, std::uint64_t*, std::uint64_t*); |
| 38 | using XMemFreePhysicalPagesFn = BOOL(WINAPI*)(std::uint64_t, const std::uint64_t*); | 44 | using XMemFreePhysicalPagesFn = BOOL(WINAPI*)(std::uint64_t, const std::uint64_t*); |
| 39 | using XMemMapPhysicalPagesFn = void*(WINAPI*)(void*, std::uint64_t, const std::uint64_t*); | 45 | using XMemMapPhysicalPagesFn = void*(WINAPI*)(void*, std::uint64_t, const std::uint64_t*); |
| @@ -179,6 +185,38 @@ int test() { | |||
| 179 | EXPECT(placed_words[0x100] == 0x7171u); | 185 | EXPECT(placed_words[0x100] == 0x7171u); |
| 180 | EXPECT(gpu_word(device.p, std::uintptr_t(placed) + 0x400) == 0x7171u); | 186 | EXPECT(gpu_word(device.p, std::uintptr_t(placed) + 0x400) == 0x7171u); |
| 181 | 187 | ||
| 188 | // 7. Pieces A (0x800) and B (0xc00) of one page: the GPU writes A, the title B; a GPU read refreshes the mirror | ||
| 189 | // first (B is uploaded, A's home bytes stay), then A goes back to the title's page. | ||
| 190 | const std::uint32_t gpu_a[4] = {0xA0A00001u, 0xA0A00002u, 0xA0A00003u, 0xA0A00004u}; | ||
| 191 | EXPECT(rt::d3d12::page_mirror_gpu_write(device.p, std::uintptr_t(placed) + 0x800, gpu_a, sizeof(gpu_a))); | ||
| 192 | std::thread([&] { placed_words[0x300] = 0xB0B00001u; }).join(); | ||
| 193 | EXPECT(gpu_word(device.p, std::uintptr_t(placed) + 0xc00) == 0xB0B00001u); | ||
| 194 | EXPECT(gpu_word(device.p, std::uintptr_t(placed) + 0x800) == 0xA0A00001u); | ||
| 195 | EXPECT(rt::d3d12::page_mirror_write_back(device.p, std::uintptr_t(placed), kPage64k) == 256); | ||
| 196 | EXPECT(placed_words[0x200] == 0xA0A00001u && placed_words[0x203] == 0xA0A00004u); | ||
| 197 | EXPECT(placed_words[0x300] == 0xB0B00001u); | ||
| 198 | EXPECT(gpu_word(device.p, std::uintptr_t(placed) + 0x800) == 0xA0A00001u); | ||
| 199 | EXPECT(gpu_word(device.p, std::uintptr_t(placed) + 0xc00) == 0xB0B00001u); | ||
| 200 | |||
| 201 | // 8. XMemAlloc blocks. | ||
| 202 | auto xmem_alloc = reinterpret_cast<XMemAllocFn>(rt::shims::find("xmem.dll", "XMemAlloc")); | ||
| 203 | auto xmem_free = reinterpret_cast<XMemFreeFn>(rt::shims::find("xmem.dll", "XMemFree")); | ||
| 204 | EXPECT(xmem_alloc && xmem_free); | ||
| 205 | auto* heap_small = static_cast<volatile std::uint32_t*>(xmem_alloc(3000, 0x5e50000)); | ||
| 206 | auto* heap_large = static_cast<volatile std::uint32_t*>(xmem_alloc(200000, 0)); | ||
| 207 | EXPECT(heap_small && heap_large && std::uintptr_t(heap_small) % 64 == 0); | ||
| 208 | EXPECT(leonida::title_mirror_space::contains(std::uintptr_t(heap_small)) && | ||
| 209 | leonida::title_mirror_space::contains(std::uintptr_t(heap_large))); | ||
| 210 | heap_small[700] = 0x81000700u; | ||
| 211 | heap_large[40000] = 0x82040000u; | ||
| 212 | EXPECT(gpu_word(device.p, std::uintptr_t(heap_small) + 2800) == 0x81000700u); | ||
| 213 | EXPECT(gpu_word(device.p, std::uintptr_t(heap_large) + 160000) == 0x82040000u); | ||
| 214 | std::thread([&] { heap_small[700] = 0x81000701u; }).join(); | ||
| 215 | EXPECT(gpu_word(device.p, std::uintptr_t(heap_small) + 2800) == 0x81000701u); | ||
| 216 | xmem_free(const_cast<std::uint32_t*>(heap_small), 0); | ||
| 217 | xmem_free(const_cast<std::uint32_t*>(heap_large), 0); | ||
| 218 | EXPECT(gpu_word(device.p, std::uintptr_t(heap_large) + 160000) == 0); // decommitted: unmapped, reads zero | ||
| 219 | |||
| 182 | const auto end = rt::d3d12::page_mirror_state(device.p); | 220 | const auto end = rt::d3d12::page_mirror_state(device.p); |
| 183 | std::printf("page mirror: heap_base 0x%llx, %llu pages in %llu homes over %llu chunk(s); slot-31 copies, a title " | 221 | std::printf("page mirror: heap_base 0x%llx, %llu pages in %llu homes over %llu chunk(s); slot-31 copies, a title " |
| 184 | "write through its own view and a remap reach heap_base + the GPU-only address; an unmapped page reads " | 222 | "write through its own view and a remap reach heap_base + the GPU-only address; an unmapped page reads " |
diff --git a/runtime/test/gfx10_descriptors_test.cpp b/runtime/test/gfx10_descriptors_test.cpp index f30d7f026..b5278de8c 100644 --- a/runtime/test/gfx10_descriptors_test.cpp +++ b/runtime/test/gfx10_descriptors_test.cpp | |||
| @@ -25,6 +25,26 @@ int main() { | |||
| 25 | // and TYPE (SQ_RSRC_IMG_2D = 9 at bits 31:28). | 25 | // and TYPE (SQ_RSRC_IMG_2D = 9 at bits 31:28). |
| 26 | const ImageWords plain = encode(Image{}); | 26 | const ImageWords plain = encode(Image{}); |
| 27 | CHECK((plain == ImageWords{0, 0, 0, 0x90000FAC, 0, 0, 0, 0})); | 27 | CHECK((plain == ImageWords{0, 0, 0, 0x90000FAC, 0, 0, 0, 0})); |
| 28 | |||
| 29 | // The bindless index in dword 6 (tsharp_index_word): TAG 0xA [31:28], the storage kind bits 8..12 at [27:23], the | ||
| 30 | // sampled kind bits 0..6 at [22:16], the index at [15:0]; decoded back as the translator does, and stripped from the | ||
| 31 | // key. Index 0 and an index past 16 bits get no word, which every shader reads as the fallback. | ||
| 32 | { | ||
| 33 | const std::uint32_t kind = 0x1Fu << 8 | 0x7Fu; // every array | ||
| 34 | CHECK(tsharp_index_word(0x1234, kind) == 0xAFFF1234u); | ||
| 35 | CHECK(tsharp_index_word(7, 1u | 1u << 8) == 0xA0810007u); | ||
| 36 | CHECK(tsharp_index_word(0, kind) == 0 && tsharp_index_word(0x10000, kind) == 0); | ||
| 37 | ImageWords words = plain; | ||
| 38 | words[kTsharpIndexDword] = tsharp_index_word(9, 1u << 2 | 1u << 10); | ||
| 39 | CHECK(has_tsharp_index(words) && tsharp_index_of(words) == 9 && tsharp_kind_of(words) == (1u << 2 | 1u << 10)); | ||
| 40 | CHECK(tsharp_key(words) == plain); | ||
| 41 | ImageWords no_image = words; | ||
| 42 | no_image[3] = 0x30000FAC; // TYPE 3: not an image, whatever dword 6 holds | ||
| 43 | CHECK(tsharp_index_of(no_image) == 0 && tsharp_kind_of(no_image) == 0); | ||
| 44 | ImageWords title = plain; | ||
| 45 | title[kTsharpIndexDword] = 0x1234; // a title's own dword 6 (no tag) stays in its key | ||
| 46 | CHECK(!has_tsharp_index(title) && tsharp_key(title) == title && tsharp_index_of(title) == 0); | ||
| 47 | } | ||
| 28 | // One field at a time: the bits that change, word by word. | 48 | // One field at a time: the bits that change, word by word. |
| 29 | const auto changed = [&](const Image& image) { | 49 | const auto changed = [&](const Image& image) { |
| 30 | ImageWords words = encode(image); | 50 | ImageWords words = encode(image); |
diff --git a/runtime/test/shader_library_test.cpp b/runtime/test/shader_library_test.cpp index f34c50024..06f105123 100644 --- a/runtime/test/shader_library_test.cpp +++ b/runtime/test/shader_library_test.cpp | |||
| @@ -864,6 +864,32 @@ int main() { | |||
| 864 | std::filesystem::remove_all(dir); | 864 | std::filesystem::remove_all(dir); |
| 865 | } | 865 | } |
| 866 | 866 | ||
| 867 | // The T# index-word marker (`"tsharp_index": "dword6"`, TsharpIndexCensus): the modules that take a heap T#'s | ||
| 868 | // bindless index from its dword 6, which the device then writes into every heap, and the older ones (no key, or | ||
| 869 | // a value this runtime does not know), which look such T#s up in the hash table. | ||
| 870 | { | ||
| 871 | const auto root = dir / "tsharp_index"; | ||
| 872 | const auto write_index = [](const std::filesystem::path& folder, const std::string& modules) { | ||
| 873 | std::filesystem::create_directories(folder); | ||
| 874 | std::ofstream index(folder / "index.json", std::ios::binary); | ||
| 875 | index << "{" << modules << "}"; | ||
| 876 | }; | ||
| 877 | const auto module = [](const char* name, const char* marker) { | ||
| 878 | std::string out = "{\"stage\": \"ps\", \"fssm_sha256\": \"" + std::string(name) + "\""; | ||
| 879 | if (marker) out += ", \"tsharp_index\": \"" + std::string(marker) + "\""; | ||
| 880 | return out + "}"; | ||
| 881 | }; | ||
| 882 | write_index(root, "\"a\": " + module("aa", "dword6") + ", \"b\": " + module("bb", nullptr) + ", \"c\": " + | ||
| 883 | module("cc", "dword7") + ", \"d\": " + module("dd", "dword6")); | ||
| 884 | ShaderLibrary mixed; | ||
| 885 | CHECK(mixed.load(root.string(), &error)); | ||
| 886 | CHECK(mixed.tsharp_index_census().dword6 == 2 && mixed.tsharp_index_census().none == 2); | ||
| 887 | write_index(root, "\"a\": " + module("aa", nullptr)); | ||
| 888 | CHECK(mixed.load(root.string(), &error)); // a reload replaces the census | ||
| 889 | CHECK(mixed.tsharp_index_census().dword6 == 0 && mixed.tsharp_index_census().none == 1); | ||
| 890 | std::filesystem::remove_all(root); | ||
| 891 | } | ||
| 892 | |||
| 867 | // The descriptor-table layout marker (`descriptor_table` in each module's meta, descriptor_table.h): absent is | 893 | // The descriptor-table layout marker (`descriptor_table` in each module's meta, descriptor_table.h): absent is |
| 868 | // the old plain layout. The kind word is additive - a tagged module reads it, a plain one ignores it and reads | 894 | // the old plain layout. The kind word is additive - a tagged module reads it, a plain one ignores it and reads |
| 869 | // the index at the same place - so one tagged module needs the tagged layout however many plain ones a merged or | 895 | // the index at the same place - so one tagged module needs the tagged layout however many plain ones a merged or |
diff --git a/runtime/test/shaders/ps_table_fetch.asm b/runtime/test/shaders/ps_table_fetch.asm index fad9cb9ee..c118c6fd9 100644 --- a/runtime/test/shaders/ps_table_fetch.asm +++ b/runtime/test/shaders/ps_table_fetch.asm | |||
| @@ -1,6 +1,9 @@ | |||
| 1 | ; vk_texture_test fetch-sint / fetch-uint / fetch-miss. Texel (0,0) holds bytes | 1 | ; vk_texture_test fetch-sint / fetch-uint / fetch-miss. Texel (0,0) holds bytes |
| 2 | ; 82 22 f8 4d: expect (-126,34,-8,77) or (130,34,248,77). A key mismatch | 2 | ; 82 22 f8 4d: expect (-126,34,-8,77) or (130,34,248,77). A key mismatch |
| 3 | ; fetches magenta UNORM at index zero. Convert raw integer words for RGBA32F output. | 3 | ; fetches magenta UNORM at index zero. Convert raw integer words for RGBA32F output. |
| 4 | ; s0 is a descriptor-table pointer (s1 = 0): the index comes from the T#'s dword 6 (docs/design.md, "Bindless index in | ||
| 5 | ; the T#"), and fetch-miss writes a dword 6 without the index tag, which must read index zero. | ||
| 6 | s_mov_b32 s1, 0 | ||
| 4 | s_load_dwordx8 s[8:15], s[0:1], 0 | 7 | s_load_dwordx8 s[8:15], s[0:1], 0 |
| 5 | s_waitcnt lgkmcnt(0) | 8 | s_waitcnt lgkmcnt(0) |
| 6 | v_mov_b32_e32 v0, 0 | 9 | v_mov_b32_e32 v0, 0 |
diff --git a/runtime/test/shaders/ps_texture_sample.asm b/runtime/test/shaders/ps_texture_sample.asm index 35e3c6ddb..e3851fde3 100644 --- a/runtime/test/shaders/ps_texture_sample.asm +++ b/runtime/test/shaders/ps_texture_sample.asm | |||
| @@ -2,10 +2,13 @@ | |||
| 2 | ; using normalized UV coordinates from attr0, outputs sampled color to mrt0. | 2 | ; using normalized UV coordinates from attr0, outputs sampled color to mrt0. |
| 3 | ; Run: vk_texture_test (autonomously, or with vs.spv fs.spv out.raw ptr_sgpr s0 s1 s2 s3) | 3 | ; Run: vk_texture_test (autonomously, or with vs.spv fs.spv out.raw ptr_sgpr s0 s1 s2 s3) |
| 4 | ; -> verifies center pixel = gradient color (0.5, 0.5, 0.5, 1.0) sampled via descriptor table | 4 | ; -> verifies center pixel = gradient color (0.5, 0.5, 0.5, 1.0) sampled via descriptor table |
| 5 | ; s[0:1] = 64-bit pointer to T# in GPU heap | 5 | ; s0 = descriptor-table pointer to the T# in the GPU heap, made a 32-bit pointer (s1 = 0) as the title's shaders do: |
| 6 | ; the shader takes the T#'s bindless index from its dword 6, with no hash-table lookup (docs/design.md, "Bindless | ||
| 7 | ; index in the T#"); vk_texture_test writes the index word there. | ||
| 6 | ; s[8:15] = 8-dword T# (image descriptor) | 8 | ; s[8:15] = 8-dword T# (image descriptor) |
| 7 | ; s[16:19] = 4-dword S# (sampler descriptor) | 9 | ; s[16:19] = 4-dword S# (sampler descriptor) |
| 8 | ; v[0:1] = barycentric UV coordinates | 10 | ; v[0:1] = barycentric UV coordinates |
| 11 | s_mov_b32 s1, 0 | ||
| 9 | v_interp_p1_f32_e32 v2, v0, attr0.x | 12 | v_interp_p1_f32_e32 v2, v0, attr0.x |
| 10 | v_interp_p2_f32_e32 v2, v1, attr0.x | 13 | v_interp_p2_f32_e32 v2, v1, attr0.x |
| 11 | v_interp_p1_f32_e32 v3, v0, attr0.y | 14 | v_interp_p1_f32_e32 v3, v0, attr0.y |
diff --git a/runtime/test/title_heap_test.cpp b/runtime/test/title_heap_test.cpp new file mode 100644 index 000000000..4eb9a08c7 --- /dev/null +++ b/runtime/test/title_heap_test.cpp | |||
| @@ -0,0 +1,130 @@ | |||
| 1 | // src/title_heap.h: the XMemAlloc heap the runtime keeps in the page mirror's range. No Windows headers, no GPU, no | ||
| 2 | // game data: the "reservation" is host memory here, and commit and decommit are counted. | ||
| 3 | // 1. blocks are 64-byte aligned, a size of zero is one byte, sizes fall in their class, large blocks are whole pages; | ||
| 4 | // 2. a randomised run of allocations and frees against a reference (a map of live blocks with their bytes): no two | ||
| 5 | // live blocks overlap, every block keeps what was written into it until it is freed, frees give space back; | ||
| 6 | // 3. a large block is committed when handed out and decommitted when freed; pointers the heap did not hand out are | ||
| 7 | // refused. | ||
| 8 | #include "../src/title_heap.h" | ||
| 9 | |||
| 10 | #include <cstdio> | ||
| 11 | #include <cstdlib> | ||
| 12 | #include <cstring> | ||
| 13 | #include <map> | ||
| 14 | #include <random> | ||
| 15 | #include <vector> | ||
| 16 | |||
| 17 | namespace { | ||
| 18 | |||
| 19 | int failures = 0; | ||
| 20 | #define EXPECT(condition) \ | ||
| 21 | do { \ | ||
| 22 | if (!(condition)) { \ | ||
| 23 | std::fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #condition); \ | ||
| 24 | ++failures; \ | ||
| 25 | } \ | ||
| 26 | } while (0) | ||
| 27 | |||
| 28 | using leonida::TitleHeap; | ||
| 29 | |||
| 30 | } // namespace | ||
| 31 | |||
| 32 | int main() { | ||
| 33 | constexpr std::size_t kBytes = std::size_t(256) << 20; | ||
| 34 | std::vector<std::uint8_t> memory(kBytes + TitleHeap::kPage); | ||
| 35 | const std::uintptr_t base = | ||
| 36 | (reinterpret_cast<std::uintptr_t>(memory.data()) + TitleHeap::kPage - 1) & ~std::uintptr_t(TitleHeap::kPage - 1); | ||
| 37 | std::size_t committed = 0, decommitted = 0; | ||
| 38 | TitleHeap heap; | ||
| 39 | TitleHeap::Hooks hooks; | ||
| 40 | hooks.commit = [&](std::uintptr_t, std::size_t bytes) { | ||
| 41 | committed += bytes; | ||
| 42 | return true; | ||
| 43 | }; | ||
| 44 | hooks.decommit = [&](std::uintptr_t at, std::size_t bytes) { | ||
| 45 | decommitted += bytes; | ||
| 46 | std::memset(reinterpret_cast<void*>(at), 0, bytes); // as a decommit reads back | ||
| 47 | }; | ||
| 48 | EXPECT(heap.init(base, kBytes, hooks)); | ||
| 49 | |||
| 50 | // 1. | ||
| 51 | void* zero = heap.allocate(0); | ||
| 52 | EXPECT(zero && reinterpret_cast<std::uintptr_t>(zero) % 64 == 0 && heap.size_of(zero) == 64); | ||
| 53 | for (std::size_t size : {1u, 63u, 64u, 65u, 1000u, 1024u, 1025u, 4096u, 30000u, 32768u}) { | ||
| 54 | void* p = heap.allocate(size); | ||
| 55 | EXPECT(p && reinterpret_cast<std::uintptr_t>(p) % 64 == 0 && heap.size_of(p) >= size && heap.owns(p)); | ||
| 56 | EXPECT(heap.release(p)); | ||
| 57 | } | ||
| 58 | void* large = heap.allocate(100000); | ||
| 59 | EXPECT(large && reinterpret_cast<std::uintptr_t>(large) % TitleHeap::kPage == 0 && | ||
| 60 | heap.size_of(large) == 2 * TitleHeap::kPage); | ||
| 61 | // 3. | ||
| 62 | const std::size_t before = decommitted; | ||
| 63 | std::memset(large, 0x5a, 100000); | ||
| 64 | EXPECT(heap.release(large) && decommitted == before + 2 * TitleHeap::kPage); | ||
| 65 | void* again = heap.allocate(100000); | ||
| 66 | EXPECT(again && static_cast<std::uint8_t*>(again)[0] == 0); // reads zero handed out again | ||
| 67 | EXPECT(heap.release(again)); | ||
| 68 | int outside = 0; | ||
| 69 | EXPECT(!heap.release(&outside) && !heap.owns(&outside)); | ||
| 70 | EXPECT(!heap.release(static_cast<std::uint8_t*>(zero) + 8)); // not a block's start | ||
| 71 | EXPECT(heap.release(zero)); | ||
| 72 | |||
| 73 | // 2. | ||
| 74 | std::mt19937 random(2026); | ||
| 75 | struct Live { | ||
| 76 | std::size_t size; | ||
| 77 | std::uint8_t tag; | ||
| 78 | }; | ||
| 79 | std::map<std::uintptr_t, Live> live; | ||
| 80 | const auto check_block = [&](std::uintptr_t at, const Live& b) { | ||
| 81 | const auto* bytes = reinterpret_cast<const std::uint8_t*>(at); | ||
| 82 | for (std::size_t i = 0; i < b.size; i += 97) | ||
| 83 | if (bytes[i] != std::uint8_t(b.tag + i)) return false; | ||
| 84 | return true; | ||
| 85 | }; | ||
| 86 | for (int round = 0; round < 20000; ++round) { | ||
| 87 | const bool free_one = !live.empty() && (random() % 100 < 45 || live.size() > 3000); | ||
| 88 | if (free_one) { | ||
| 89 | auto it = live.begin(); | ||
| 90 | std::advance(it, random() % live.size()); | ||
| 91 | EXPECT(check_block(it->first, it->second)); | ||
| 92 | EXPECT(heap.release(reinterpret_cast<void*>(it->first))); | ||
| 93 | live.erase(it); | ||
| 94 | continue; | ||
| 95 | } | ||
| 96 | const unsigned kind = random() % 100; | ||
| 97 | const std::size_t size = kind < 70 ? random() % 512 : kind < 95 ? random() % 32768 : 32769 + random() % 300000; | ||
| 98 | void* p = heap.allocate(size); | ||
| 99 | EXPECT(p != nullptr); | ||
| 100 | if (!p) break; | ||
| 101 | const auto at = reinterpret_cast<std::uintptr_t>(p); | ||
| 102 | EXPECT(at % 64 == 0 && heap.owns(p)); | ||
| 103 | const std::size_t span = size ? size : 1; | ||
| 104 | // No live block overlaps it. | ||
| 105 | auto next = live.lower_bound(at); | ||
| 106 | if (next != live.end()) EXPECT(at + span <= next->first); | ||
| 107 | if (next != live.begin()) { | ||
| 108 | const auto previous = std::prev(next); | ||
| 109 | EXPECT(previous->first + previous->second.size <= at); | ||
| 110 | } | ||
| 111 | const Live b{span, std::uint8_t(random())}; | ||
| 112 | auto* bytes = static_cast<std::uint8_t*>(p); | ||
| 113 | for (std::size_t i = 0; i < span; i += 97) bytes[i] = std::uint8_t(b.tag + i); | ||
| 114 | live[at] = b; | ||
| 115 | } | ||
| 116 | for (const auto& [at, b] : live) { | ||
| 117 | EXPECT(check_block(at, b)); | ||
| 118 | EXPECT(heap.release(reinterpret_cast<void*>(at))); | ||
| 119 | } | ||
| 120 | EXPECT(heap.stats().live == 0 && committed > 0); | ||
| 121 | |||
| 122 | if (failures) { | ||
| 123 | std::fprintf(stderr, "title_heap_test: %d failure(s)\n", failures); | ||
| 124 | return 1; | ||
| 125 | } | ||
| 126 | std::printf("title_heap_test: alignment, classes, large blocks, 20000 random allocations and frees against a " | ||
| 127 | "reference (%zu KiB committed, %zu KiB decommitted)\n", | ||
| 128 | committed >> 10, decommitted >> 10); | ||
| 129 | return 0; | ||
| 130 | } | ||
diff --git a/runtime/test/title_mirror_hooks_test.cpp b/runtime/test/title_mirror_hooks_test.cpp index e38bb49a3..c3a2238a1 100644 --- a/runtime/test/title_mirror_hooks_test.cpp +++ b/runtime/test/title_mirror_hooks_test.cpp | |||
| @@ -139,10 +139,26 @@ int filters() { | |||
| 139 | EXPECT(f.filtered.size() == 1 && f.filtered[0].va == kBase && f.filtered[0].fresh); | 139 | EXPECT(f.filtered.size() == 1 && f.filtered[0].va == kBase && f.filtered[0].fresh); |
| 140 | EXPECT(mirror.home_bytes(kBase)[100] == 0x5A && mirror.home_bytes(kBase + 2 * kPage)[100] == 0xA5); | 140 | EXPECT(mirror.home_bytes(kBase)[100] == 0x5A && mirror.home_bytes(kBase + 2 * kPage)[100] == 0xA5); |
| 141 | EXPECT(mirror.counters().filtered == 1 && mirror.counters().uploads == 2); | 141 | EXPECT(mirror.counters().filtered == 1 && mirror.counters().uploads == 2); |
| 142 | // Write-back: a filtered home differs from the title's page by the filter's bytes and is never written back; a | ||
| 143 | // copied home the GPU wrote is. | ||
| 144 | std::size_t written = 0; | ||
| 145 | const auto count_writes = [&](std::uint64_t va, const std::uint8_t* bytes, std::uint64_t n) { | ||
| 146 | if (va < kBase + 2 * kPage) return false; // the copied page only (a write elsewhere fails the counts) | ||
| 147 | std::memcpy(&frames[std::size_t(va - kBase)], bytes, std::size_t(n)); | ||
| 148 | written += std::size_t(n); | ||
| 149 | return true; | ||
| 150 | }; | ||
| 151 | EXPECT(mirror.write_back(kBase, kBase + kPage, count_writes) == 0 && written == 0); | ||
| 152 | mirror.home_bytes(kBase + 2 * kPage)[300] = 0x77; // a GPU store into the copied page | ||
| 153 | EXPECT(mirror.write_back(kBase + 2 * kPage, kBase + 3 * kPage, count_writes) == 256 && written == 256); | ||
| 154 | mirror.home_bytes(kBase + 2 * kPage)[300] = 0xA5; // as it was, for the cases below | ||
| 155 | EXPECT(mirror.write_back(kBase + 2 * kPage, kBase + 3 * kPage, count_writes) == 256); | ||
| 142 | // A write and its refill: through the filter, not fresh. | 156 | // A write and its refill: through the filter, not fresh. |
| 143 | frames[200] = 0x11; | 157 | frames[200] = 0x11; |
| 144 | EXPECT(mirror.dirty(kBase) && mirror.upload() == 1); | 158 | EXPECT(mirror.dirty(kBase) && mirror.upload() == 1); |
| 145 | EXPECT(f.filtered.size() == 2 && !f.filtered[1].fresh && mirror.home_bytes(kBase)[200] == 0x11); | 159 | EXPECT(f.filtered.size() == 2 && !f.filtered[1].fresh && mirror.home_bytes(kBase)[200] == 0x11); |
| 160 | // The upload compares the title's bytes with the title's: the same title bytes again fill nothing. | ||
| 161 | EXPECT(mirror.dirty(kBase) && mirror.upload() == 1 && f.filtered.size() == 2); | ||
| 146 | // The same physical page shown at a second, unfiltered address: one home, still filtered. | 162 | // The same physical page shown at a second, unfiltered address: one home, still filtered. |
| 147 | mirror.map(kBase + 5 * kPage, &frames[0], nullptr, 2); | 163 | mirror.map(kBase + 5 * kPage, &frames[0], nullptr, 2); |
| 148 | EXPECT(mirror.home_bytes(kBase + 5 * kPage) == mirror.home_bytes(kBase) && mirror.home_bytes(kBase + 5 * kPage)[0] == 0x5A); | 164 | EXPECT(mirror.home_bytes(kBase + 5 * kPage) == mirror.home_bytes(kBase) && mirror.home_bytes(kBase + 5 * kPage)[0] == 0x5A); |
diff --git a/runtime/test/vk_texture_test.cpp b/runtime/test/vk_texture_test.cpp index 97e93cb04..df0177bb3 100644 --- a/runtime/test/vk_texture_test.cpp +++ b/runtime/test/vk_texture_test.cpp | |||
| @@ -151,8 +151,14 @@ int main(int argc, char** argv) { | |||
| 151 | const VkFormat sampled_format = dref ? VK_FORMAT_D32_SFLOAT : | 151 | const VkFormat sampled_format = dref ? VK_FORMAT_D32_SFLOAT : |
| 152 | (uint_mips || fetch_uint) ? VK_FORMAT_R8G8B8A8_UINT : | 152 | (uint_mips || fetch_uint) ? VK_FORMAT_R8G8B8A8_UINT : |
| 153 | (signed_mips || fetch) ? VK_FORMAT_R8G8B8A8_SINT : VK_FORMAT_R8G8B8A8_UNORM; | 153 | (signed_mips || fetch) ? VK_FORMAT_R8G8B8A8_SINT : VK_FORMAT_R8G8B8A8_UNORM; |
| 154 | uint32_t tsharp[8] = {0x11111111, leonida::gfx10::format_from_vulkan(sampled_format) << 20, 0x33333333, 0x44440444, | 154 | // The shaders load the T# through a descriptor-table pointer (s[ptr_sgpr], high dword 0), so they take its index |
| 155 | 0x55555555, 0x66666666, 0x77777777, 0x88888888}; | 155 | // from dword 6 as the layer writes it (gfx10_descriptors.h, tsharp_index_word; docs/design.md, "Bindless index in |
| 156 | // the T#"): index 1, bound in both sampled arrays they read (see the table insert below). Word 3's TYPE is 9 (2D): | ||
| 157 | // a word without the tag, or a dword 3 that names no image, reads the fallback. | ||
| 158 | const uint32_t index_word = leonida::gfx10::tsharp_index_word(1, leonida::kind_sampled_bit(0) | | ||
| 159 | leonida::kind_sampled_bit(1)); | ||
| 160 | uint32_t tsharp[8] = {0x11111111, leonida::gfx10::format_from_vulkan(sampled_format) << 20, 0x33333333, 0x94440444, | ||
| 161 | 0x55555555, 0x66666666, index_word, 0x88888888}; | ||
| 156 | const uint32_t tsharp_va = 256; | 162 | const uint32_t tsharp_va = 256; |
| 157 | 163 | ||
| 158 | gipa = load_vulkan(); | 164 | gipa = load_vulkan(); |
| @@ -318,7 +324,13 @@ int main(int argc, char** argv) { | |||
| 318 | const VkDeviceSize heap_bytes = heap_mb && std::atoi(heap_mb) > 0 ? VkDeviceSize(std::atoi(heap_mb)) << 20 : 65536; | 324 | const VkDeviceSize heap_bytes = heap_mb && std::atoi(heap_mb) > 0 ? VkDeviceSize(std::atoi(heap_mb)) << 20 : 65536; |
| 319 | CHECK(make_buffer(heap_bytes, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, &heap, &heapm, &heapp)); | 325 | CHECK(make_buffer(heap_bytes, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, &heap, &heapm, &heapp)); |
| 320 | std::memset(heapp, 0, size_t(heap_bytes)); | 326 | std::memset(heapp, 0, size_t(heap_bytes)); |
| 321 | std::memcpy(static_cast<char*>(heapp) + tsharp_va, tsharp, 32); | 327 | { |
| 328 | // The miss control: a T# whose dword 6 carries no index (the title's own word) reads the fallback. | ||
| 329 | uint32_t in_heap[8]; | ||
| 330 | std::memcpy(in_heap, tsharp, sizeof(in_heap)); | ||
| 331 | if (fetch_miss) in_heap[leonida::gfx10::kTsharpIndexDword] = 0x77777777; | ||
| 332 | std::memcpy(static_cast<char*>(heapp) + tsharp_va, in_heap, 32); | ||
| 333 | } | ||
| 322 | if (two_mip) // The gather test loads its S# from the four words just after the T#. | 334 | if (two_mip) // The gather test loads its S# from the four words just after the T#. |
| 323 | std::memcpy(static_cast<char*>(heapp) + tsharp_va + 32, ssharp, 16); | 335 | std::memcpy(static_cast<char*>(heapp) + tsharp_va + 32, ssharp, 16); |
| 324 | VkBufferDeviceAddressInfo bdai{VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO}; bdai.buffer = heap; | 336 | VkBufferDeviceAddressInfo bdai{VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO}; bdai.buffer = heap; |