diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index f8edc3508e..ed0b9b006b 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -2901,6 +2901,14 @@ 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 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 f2fa3a45c2..721e15bbbd 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -9274,6 +9274,27 @@ 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 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 + // 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 && !host_transfer_ && !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; @@ -9325,7 +9346,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. @@ -16000,6 +16022,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 69c3a6607a..d75a600e7f 100644 --- a/runtime/test/d3d12_existing_heap_test.cpp +++ b/runtime/test/d3d12_existing_heap_test.cpp @@ -1521,6 +1521,65 @@ 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. +// 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; + 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; +} + namespace { std::atomic g_view_av_thread{0}; std::atomic g_view_avs{0}; @@ -2342,8 +2401,16 @@ 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_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) { // 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"); + } _putenv_s("LEONIDA_TITLE_TEXTURE_PERIOD_MS", "0"); _putenv_s("LEONIDA_TITLE_TEXTURE_CHECK_KB", "0"); vktest::Device vk; @@ -2380,6 +2447,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, show_after_unstaged); + 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");