From 7fa9385621a9fbd6f6ed0af14ca9e60b6ec947fc Mon Sep 17 00:00:00 2001 From: Tyrannys <80750971+Tyrannys@users.noreply.github.com> Date: Thu, 26 Nov 2026 05:28:17 -0600 Subject: [PATCH 1/2] d3d12: show a staged slot-70 texture only after its first upload (RT-TITLE-TEXTURE-SHOW-AFTER-UPLOAD) A slot-70 texture the layer makes itself (TitleImageOrigin::streamed) and fills through the staged upload had its T#s bound in the bindless set from its first view on. A draw before the first fill (always with LEONIDA_TITLE_FILL_ASYNC=1, and whenever an inline fill is skipped) sampled an image never written and still in UNDEFINED layout, and a draw on another queue could overlap the upload's UNDEFINED -> TRANSFER_DST transition of it. Such a texture now keeps its T#s pending (they read the fallback at index 0) until publish_streamed_texture, which runs after the first upload has been submitted. TitleImageUpload::submit already orders every later submission behind that upload (an image barrier on its queue, a timeline wait on the others), so a draw that can see the image sees it uploaded. Memory is still bound at creation; only the descriptor waits. LEONIDA_TITLE_SHOW_AFTER_UPLOAD=0 restores the old behaviour. Idea from codec-d/cool_fox docs/findings.md (VICTORY_SHOW_AFTER); idea only, no code taken. Test: d3d12_title_show_after_upload (LEONIDA_TITLE_FILL_ASYNC=1 with a new LEONIDA_TITLE_FILL_DELAY_MS slow fill worker) checks the T# has no bindless entry before the upload and has one after it. It fails with LEONIDA_TITLE_SHOW_AFTER_UPLOAD=0. Co-Authored-By: Claude Opus 5.5 --- runtime/CMakeLists.txt | 4 ++ runtime/shims/d3d12_vulkan_device.cpp | 27 ++++++++- runtime/test/d3d12_existing_heap_test.cpp | 71 ++++++++++++++++++++++- 3 files changed, 100 insertions(+), 2 deletions(-) diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 10ee582b12..2ac7667dc5 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -2793,6 +2793,10 @@ if(TARGET Vulkan::Headers) # LEONIDA_TEXTURE_REFRESH_ASYNC=1 (set by the test itself): a mapped plane's first submission reads its bytes. add_test(NAME d3d12_mapped_plane_async COMMAND d3d12_existing_heap_test --mapped-plane-async) set_tests_properties(d3d12_mapped_plane_async PROPERTIES SKIP_RETURN_CODE 77) + # LEONIDA_TITLE_FILL_ASYNC=1 and a slow fill worker (set by the test itself): a staged slot-70 texture's T# names + # its image only once its first upload is submitted (RT-TITLE-TEXTURE-SHOW-AFTER-UPLOAD). + add_test(NAME d3d12_title_show_after_upload COMMAND d3d12_existing_heap_test --title-show-after-upload) + set_tests_properties(d3d12_title_show_after_upload PROPERTIES SKIP_RETURN_CODE 77) # The same with write watching off: the guarded cases only, the concurrent writers among them. add_test(NAME d3d12_existing_heap_no_write_watch COMMAND d3d12_existing_heap_test) set_tests_properties(d3d12_existing_heap_no_write_watch PROPERTIES SKIP_RETURN_CODE 77 diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 1fb36401e3..4f8952de15 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -8466,6 +8466,24 @@ class Resource final std::fprintf(stderr, "[rt] mem-probe: %s no-host-bits=0x%x type=%u\n", key, without.memoryTypeBits, type); } } + // A slot-70 texture of our own that is filled through the staged upload is shown only once its first upload + // has been submitted (publish_streamed_texture, after fill_title_texture or the fill worker's fill_unlocked). + // Until then its T#s stay pending and read the fallback at index 0. Before, its T#s named the image from the + // view on: a draw before the first fill (always with LEONIDA_TITLE_FILL_ASYNC=1, and whenever an inline fill is + // skipped) sampled an image never written and still UNDEFINED, and one on another queue could run while the + // upload's UNDEFINED -> TRANSFER_DST transition rewrote it. The upload orders every later submission behind it: + // the image barrier on its own queue, a semaphore wait on the others (TitleImageUpload::submit). Idea from + // codec-d/cool_fox docs/findings.md (VICTORY_SHOW_AFTER: a slot shows a new texture only after its upload ran); + // no code taken. LEONIDA_TITLE_SHOW_AFTER_UPLOAD=0 shows it at once, as before. + static const bool show_after_upload = [] { + const char* value = std::getenv("LEONIDA_TITLE_SHOW_AFTER_UPLOAD"); + return !value || std::strcmp(value, "0"); + }(); + if (show_after_upload && origin == TitleImageOrigin::streamed && staged_title_ && !deferred_stream_) { + deferred_stream_ = true; + std::lock_guard lock(device_.descriptor_mutex); + device_.pending_image_keys.try_emplace(image_); + } leonida::ResourceBinding binding{}; if (deferred_attachment_) { binding.residency_owner = this; @@ -8517,7 +8535,8 @@ class Resource final device_.context->add_resource(id(), binding_, host_transfer_ ? VK_IMAGE_LAYOUT_GENERAL : VK_IMAGE_LAYOUT_UNDEFINED); return S_OK; } - return deferred_stream_ ? S_OK : register_bound_texture(); + // A pending stream without memory registers once it has some (back_streamed_texture). + return deferred_stream_ && deferred_bytes_ ? S_OK : register_bound_texture(); } // Called with title_texture_mutex held, after a guarded read of the complete console texture. @@ -15088,6 +15107,12 @@ class Device final } if (refresh) refresh_title_textures(); if (!fill) continue; + // LEONIDA_TITLE_FILL_DELAY_MS (tests): a slow fill, so a view is written well before its texture's upload. + static const auto delay = std::chrono::milliseconds([] { + const char* v = std::getenv("LEONIDA_TITLE_FILL_DELAY_MS"); + return v && *v ? std::strtoll(v, nullptr, 10) : 0ll; + }()); + if (delay.count() > 0) std::this_thread::sleep_for(delay); bool filled = false; // A staged texture is filled without the registry lock (the title's threads creating views wait for // it: speed-31, 12 % of the main thread): its bytes, layout and resource are taken under the lock, the diff --git a/runtime/test/d3d12_existing_heap_test.cpp b/runtime/test/d3d12_existing_heap_test.cpp index 25fa1f6544..cf8dd945f3 100644 --- a/runtime/test/d3d12_existing_heap_test.cpp +++ b/runtime/test/d3d12_existing_heap_test.cpp @@ -1359,6 +1359,62 @@ int check_pending_stream_views(ID3D12Device* device) { return 0; } +// --title-show-after-upload (LEONIDA_TITLE_FILL_ASYNC=1 and a slow fill worker, set by the test itself): a slot-70 +// texture of readable pages, filled through the staged upload, is made by its first view, and its upload runs later on +// the fill worker. Its T# must not name the image before then: a shader would sample an image never written, in +// UNDEFINED layout (the RT-TITLE-TEXTURE-SHOW-AFTER-UPLOAD race; codec-d/cool_fox docs/findings.md, idea only). Once +// the upload is submitted the T# names it. +int check_title_show_after_upload(ID3D12Device* device) { + const char* who = "slot-70 view shown after its upload"; + if (!rt::d3d12::bindless_set_layout(device)) return 0; + constexpr UINT width = 64, height = 8; + constexpr SIZE_T bytes = 65536; + void* pages = VirtualAlloc(nullptr, bytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); + EXPECT(pages); + std::memset(pages, 73, bytes); + D3D12_RESOURCE_DESC desc{}; + desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + desc.Width = width; + desc.Height = height; + desc.DepthOrArraySize = desc.MipLevels = desc.SampleDesc.Count = 1; + desc.Format = DXGI_FORMAT_R8_UNORM; + desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + D3D12_SHADER_RESOURCE_VIEW_DESC srv{}; + srv.Format = desc.Format; + srv.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; + srv.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; + srv.Texture2D.MipLevels = 1; + leonida::gfx10::ImageWords slot{}; + std::array resource{}; + std::array words{}; + std::memcpy(resource.data(), &desc, sizeof(desc)); + std::memcpy(words.data(), &srv, sizeof(srv)); + auto index = [&] { + std::uint32_t key[8]; + std::memcpy(key, slot.data(), sizeof(key)); + return rt::d3d12::bindless_hash_lookup(device, key); + }; + EXPECT(rt::d3d12::write_placed_texture_view(device, false, resource.data(), words.data(), + reinterpret_cast(pages), {reinterpret_cast(slot.data())})); + // The fill worker sleeps before it fills: the T# is written, the upload has not run. + const bool shown_early = index() != 0; + if (shown_early) std::fprintf(stderr, "%s: the T# names the image before its upload\n", who); + EXPECT(!shown_early); + bool shown = false; + for (int i = 0; i < 300 && !(shown = index() != 0); ++i) Sleep(20); + EXPECT(shown); + std::printf("%s: hidden until the fill worker's upload, then shown\n", who); + // Replace the synthetic owner before releasing the CPU pages. + Ref placement; + desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; + EXPECT_HR(rt::d3d12::create_buffer_at_host_address(device, pages, desc, 6, IID_PPV_ARGS(&placement.p)), S_OK); + EXPECT(!index()); + placement.p->Release(); + placement.p = nullptr; + VirtualFree(pages, 0, MEM_RELEASE); + return 0; +} + // A persistent Map can become readable without notifying xmem or calling Map a second time. int check_mapped_plane(ID3D12Device* device) { const char* who = "persistent slot-133 plane"; @@ -2043,8 +2099,13 @@ void print_cases(const std::vector& cases, const char* who) { int main(int argc, char** argv) { const bool prefix_only = argc == 2 && std::strcmp(argv[1], "--host-prefix-only") == 0; const bool async_plane = argc == 2 && std::strcmp(argv[1], "--mapped-plane-async") == 0; - if (argc != 1 && !prefix_only && !async_plane) return 2; + const bool show_after = argc == 2 && std::strcmp(argv[1], "--title-show-after-upload") == 0; + if (argc != 1 && !prefix_only && !async_plane && !show_after) return 2; if (async_plane) _putenv_s("LEONIDA_TEXTURE_REFRESH_ASYNC", "1"); // read once, at the device's first refresh + if (show_after) { // both read once + _putenv_s("LEONIDA_TITLE_FILL_ASYNC", "1"); + _putenv_s("LEONIDA_TITLE_FILL_DELAY_MS", "500"); + } _putenv_s("LEONIDA_TITLE_TEXTURE_PERIOD_MS", "0"); _putenv_s("LEONIDA_TITLE_TEXTURE_CHECK_KB", "0"); vktest::Device vk; @@ -2081,6 +2142,14 @@ int main(int argc, char** argv) { result += check_mapped_plane_first_use(staged.p); return result || vk.validation_errors() ? 1 : 0; } + if (show_after) { + auto no_host_copy = info; // a title image filled through the staged upload + no_host_copy.host_image_copy = false; + Ref staged; + if (FAILED(rt::d3d12::create_vulkan_device(no_host_copy, IID_PPV_ARGS(&staged.p)))) return 1; + const int result = check_title_show_after_upload(staged.p); + return result || vk.validation_errors() ? 1 : 0; + } failures += check(ours.p, "vulkan"); const std::vector vulkan_cases = open_cases(ours.p); print_cases(vulkan_cases, "vulkan"); From e6fa5544f9a3c2c28f38e1175eabba903342be7d Mon Sep 17 00:00:00 2001 From: Tyrannys <80750971+Tyrannys@users.noreply.github.com> Date: Fri, 27 Nov 2026 10:15:28 -0600 Subject: [PATCH 2/2] d3d12: show-after-upload also for streamed textures filled by staging copy A slot-70 streamed texture that is neither staged nor host-transfer (a format without host-image transfer, LEONIDA_TITLE_IMAGE_STAGING=0 on a device without host copies, the retry_host fallback there) also starts UNDEFINED. It is filled through WriteToSubresource's waited staging copy, and fill_title_texture already publishes it after the first fill. The deferral now keys on !host_transfer_ instead of staged_title_, so these textures read the fallback until that fill too. Host-transfer images are made GENERAL and filled on the host, and are still shown at once. New test d3d12_title_show_after_upload_unstaged (LEONIDA_TITLE_IMAGE_STAGING=0, no host image copy): its T# named the image before the fill with the old condition (2/2 runs), and is hidden until the fill worker's upload with the new one (3/3). LEONIDA_TITLE_SHOW_AFTER_UPLOAD is now in the README's environment table. From lixonik's review of #2267. Co-Authored-By: Claude Opus 5.5 --- runtime/CMakeLists.txt | 4 ++++ runtime/README.md | 1 + runtime/shims/d3d12_vulkan_device.cpp | 7 +++++-- runtime/test/d3d12_existing_heap_test.cpp | 16 +++++++++++----- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index ade3113880..ed0b9b006b 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -2905,6 +2905,10 @@ if(TARGET Vulkan::Headers) # its image only once its first upload is submitted (RT-TITLE-TEXTURE-SHOW-AFTER-UPLOAD). add_test(NAME d3d12_title_show_after_upload COMMAND d3d12_existing_heap_test --title-show-after-upload) set_tests_properties(d3d12_title_show_after_upload PROPERTIES SKIP_RETURN_CODE 77) + # The same with LEONIDA_TITLE_IMAGE_STAGING=0 (set by the test itself): a texture filled through WriteToSubresource's + # staging copy, neither staged nor host-copied, starts UNDEFINED too. + add_test(NAME d3d12_title_show_after_upload_unstaged COMMAND d3d12_existing_heap_test --title-show-after-upload-unstaged) + set_tests_properties(d3d12_title_show_after_upload_unstaged PROPERTIES SKIP_RETURN_CODE 77) # The same with write watching off: the guarded cases only, the concurrent writers among them. add_test(NAME d3d12_existing_heap_no_write_watch COMMAND d3d12_existing_heap_test) set_tests_properties(d3d12_existing_heap_no_write_watch PROPERTIES SKIP_RETURN_CODE 77 diff --git a/runtime/README.md b/runtime/README.md index b440230c81..48cc688427 100644 --- a/runtime/README.md +++ b/runtime/README.md @@ -387,6 +387,7 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w | `LEONIDA_GEOMETRY_WHOLE_VIEWS` | unset | A geometry copy that is already filled copies only the 64 KiB tiles of title memory written since the last refresh (`shims/d3d12_geometry_views.h`). Any value other than `0` copies every view over a written tile whole, as before. | | `LEONIDA_GEOMETRY_HOST` | unset | Where the geometry copies (`shims/d3d12_geometry_views.h`, under `LEONIDA_GEOMETRY_MB`) live. Unset or `0`: device-local memory through the BAR while the driver gives it (several GB of VRAM in a world run). `1`: system memory outside VRAM, write-back where the device offers a cached host type; `2`: system memory, write-combined. The GPU then reads every vertex and index of a copy over the bus, which can cost frame time in geometry-heavy passes. A device whose host-visible memory is all device-local (an integrated GPU) keeps its types. The choice is logged once and in the `geometry buffers` line. | | `LEONIDA_TITLE_FILL_ASYNC` | unset | Trades correctness for speed. `1` stops device slot 70 views from hashing and filling the texture they name on the title's thread: a texture never filled or known stale is queued for a worker thread that fills one texture at a time. Until then a shader samples the image as it is, empty or a frame behind. | +| `LEONIDA_TITLE_SHOW_AFTER_UPLOAD` | `1` | `0` lets a device slot 70 view of a texture that starts empty (`UNDEFINED`: filled through a staged upload or a staging copy, not on the host) name its image at once. By default its T# reads the fallback texture until the first upload of its bytes has been submitted, so no shader samples an image never written. | | `LEONIDA_TITLE_VIEW_REFRESH_MS` | `0` | The shortest time, in milliseconds, between two checks of a title texture's bytes when a device slot 70 view of it is written. `0` checks at every view. The periodic pass still checks the texture on its own period, and a texture known stale is checked at once. Has no effect with `LEONIDA_TITLE_FILL_ASYNC=1`. | | `LEONIDA_TITLE_EVERY_BUDGET_US` | `0` | Microseconds one pass over the textures filled from the title's memory may spend on the textures it visits every pass (mapped by the title, or known stale), resuming where the last pass stopped; the texture that crosses the limit is finished. A mapped texture never filled yet is visited first whatever the limit. `0` visits all of them every pass. It limits the synchronous pass only: with `LEONIDA_TEXTURE_REFRESH_ASYNC=1` the pass runs on the refresh worker, and the limit does not apply. | | `LEONIDA_TITLE_TEXTURE_FILL_MS` | `8` | Milliseconds one pass over the textures filled from the title's memory may spend filling; a texture already started is finished. `0` removes the time limit and leaves `LEONIDA_TITLE_TEXTURE_FILL_MB` to bound the pass. | diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index b3894acb2e..721e15bbbd 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -9274,8 +9274,11 @@ class Resource final std::fprintf(stderr, "[rt] mem-probe: %s no-host-bits=0x%x type=%u\n", key, without.memoryTypeBits, type); } } - // A slot-70 texture of our own that is filled through the staged upload is shown only once its first upload + // A slot-70 texture of our own that starts UNDEFINED (filled through the staged upload, or through + // WriteToSubresource's waited staging copy: a format without host-image transfer, LEONIDA_TITLE_IMAGE_STAGING=0 + // on a device without host copies, the retry_host fallback there; lixonik) is shown only once its first upload // has been submitted (publish_streamed_texture, after fill_title_texture or the fill worker's fill_unlocked). + // A host-transfer image is made GENERAL and filled on the host, so it is shown at once as before. // Until then its T#s stay pending and read the fallback at index 0. Before, its T#s named the image from the // view on: a draw before the first fill (always with LEONIDA_TITLE_FILL_ASYNC=1, and whenever an inline fill is // skipped) sampled an image never written and still UNDEFINED, and one on another queue could run while the @@ -9287,7 +9290,7 @@ class Resource final const char* value = std::getenv("LEONIDA_TITLE_SHOW_AFTER_UPLOAD"); return !value || std::strcmp(value, "0"); }(); - if (show_after_upload && origin == TitleImageOrigin::streamed && staged_title_ && !deferred_stream_) { + if (show_after_upload && origin == TitleImageOrigin::streamed && !host_transfer_ && !deferred_stream_) { deferred_stream_ = true; std::lock_guard lock(device_.descriptor_mutex); device_.pending_image_keys.try_emplace(image_); diff --git a/runtime/test/d3d12_existing_heap_test.cpp b/runtime/test/d3d12_existing_heap_test.cpp index bd5a3db7af..d75a600e7f 100644 --- a/runtime/test/d3d12_existing_heap_test.cpp +++ b/runtime/test/d3d12_existing_heap_test.cpp @@ -1526,8 +1526,11 @@ int check_pending_stream_views(ID3D12Device* device) { // the fill worker. Its T# must not name the image before then: a shader would sample an image never written, in // UNDEFINED layout (the RT-TITLE-TEXTURE-SHOW-AFTER-UPLOAD race; codec-d/cool_fox docs/findings.md, idea only). Once // the upload is submitted the T# names it. -int check_title_show_after_upload(ID3D12Device* device) { - const char* who = "slot-70 view shown after its upload"; +// With `unstaged` (LEONIDA_TITLE_IMAGE_STAGING=0 on a device without host copies) the texture is neither staged nor +// host-copied: the fill worker fills it through WriteToSubresource's waited staging copy, and it starts UNDEFINED all +// the same (lixonik's review). +int check_title_show_after_upload(ID3D12Device* device, bool unstaged = false) { + const char* who = unstaged ? "unstaged slot-70 view shown after its upload" : "slot-70 view shown after its upload"; if (!rt::d3d12::bindless_set_layout(device)) return 0; constexpr UINT width = 64, height = 8; constexpr SIZE_T bytes = 65536; @@ -2398,10 +2401,13 @@ void print_cases(const std::vector& cases, const char* who) { int main(int argc, char** argv) { const bool prefix_only = argc == 2 && std::strcmp(argv[1], "--host-prefix-only") == 0; const bool async_plane = argc == 2 && std::strcmp(argv[1], "--mapped-plane-async") == 0; - const bool show_after = argc == 2 && std::strcmp(argv[1], "--title-show-after-upload") == 0; + const bool show_after_unstaged = argc == 2 && std::strcmp(argv[1], "--title-show-after-upload-unstaged") == 0; + const bool show_after = + show_after_unstaged || (argc == 2 && std::strcmp(argv[1], "--title-show-after-upload") == 0); if (argc != 1 && !prefix_only && !async_plane && !show_after) return 2; if (async_plane) _putenv_s("LEONIDA_TEXTURE_REFRESH_ASYNC", "1"); // read once, at the device's first refresh - if (show_after) { // both read once + if (show_after) { // all read once + if (show_after_unstaged) _putenv_s("LEONIDA_TITLE_IMAGE_STAGING", "0"); _putenv_s("LEONIDA_TITLE_FILL_ASYNC", "1"); _putenv_s("LEONIDA_TITLE_FILL_DELAY_MS", "500"); } @@ -2446,7 +2452,7 @@ int main(int argc, char** argv) { no_host_copy.host_image_copy = false; Ref staged; if (FAILED(rt::d3d12::create_vulkan_device(no_host_copy, IID_PPV_ARGS(&staged.p)))) return 1; - const int result = check_title_show_after_upload(staged.p); + const int result = check_title_show_after_upload(staged.p, show_after_unstaged); return result || vk.validation_errors() ? 1 : 0; } failures += check(ours.p, "vulkan");