From e500e994940dce9f2f9edb2ff2a29003bea162eb Mon Sep 17 00:00:00 2001 From: DadaCap <167002618+DadaCap@users.noreply.github.com> Date: Wed, 25 Nov 2026 19:14:30 +0100 Subject: [PATCH] d3d12: CPU-fillable placed BC images share a zero image without host copies too Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_vulkan.md | 3 ++- runtime/shims/d3d12_vulkan_device.cpp | 26 +++++++++++++++-------- runtime/test/d3d12_existing_heap_test.cpp | 5 ++++- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index c2e36319e2..c51efb7999 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2885,7 +2885,8 @@ The console has two list topologies Vulkan lacks: rectangle list (`IASetPrimitiv the driver's), as on D3D12. - GPU-writable slot-133 images on a default heap bind memory when replay, presentation or creation of a shader view first needs their Vulkan image. D3D12 placement and logical attachment views can precede that bind. - A CPU-fillable, typed BC texture can initially sample an immutable zero image of the same format, extent, + A CPU-fillable, typed BC texture (filled by host copies or the staged upload) can initially sample + an immutable zero image of the same format, extent, layer count and mip count. A CPU upload or explicit GPU command gives the placement private backing and republishes its existing descriptor keys. The cache holds at most 256 MiB. `LEONIDA_PLACED_IMAGE_DEFER=0` restores eager binding. diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index d2681ad6c4..20b0362ca5 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -6229,9 +6229,17 @@ class Resource final const char* value = std::getenv("LEONIDA_PLACED_IMAGE_DEFER"); return !value || std::strcmp(value, "0"); }(); + // A device without host copies has no other way to fill a title image: a refresh before a submission may not + // use a staged copy through the queue (host_copy_now). Its linear movie planes are staged too. + const bool no_host_copy = !device_.copy_memory_to_image && cpu_visible() && samples == 1 && + !format.d24_as_float && !format.rgb96; // Private BC copies start with the exact same zero blocks as the shared image. The host // transition and clear complete before a private view is published, including during command prepare. - if (defer_placed && origin == TitleImageOrigin::placed && host_transfer_ && device_.bindless && + // Without host copies the staged upload fills the private image, and the first shader submission clears what + // it has not filled. + const bool can_fill = host_transfer_ || (no_host_copy && stage_title && allow_staged_title && + device_.image_requirements); + if (defer_placed && origin == TitleImageOrigin::placed && can_fill && device_.bindless && desc_.Flags == D3D12_RESOURCE_FLAG_NONE && desc_.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE2D && desc_.SampleDesc.Count == 1 && asked_castable.empty() && !format.typeless && leonida::block_format(format.format, format.aspects).block > 1) { @@ -6240,11 +6248,7 @@ class Resource final shared_zero_ = device_.zero_sampled_image(desc_, format, footprint.size); } std::uint32_t plain_bits = 0; - // A device without host copies has no other way to fill a title image: a refresh before a submission may not - // use a staged copy through the queue (host_copy_now). Its linear movie planes are staged too. - const bool no_host_copy = !device_.copy_memory_to_image && cpu_visible() && samples == 1 && - !format.d24_as_float && !format.rgb96; - if (stage_title && !shared_zero_ && allow_staged_title && origin != TitleImageOrigin::ordinary && + if (stage_title && (!shared_zero_ || no_host_copy) && allow_staged_title && origin != TitleImageOrigin::ordinary && (no_host_copy || (!linear_placement && host_transfer_)) && format.aspects == VK_IMAGE_ASPECT_COLOR_BIT && device_.image_requirements) { for (std::uint32_t i = 0; i < device_.memory.memoryTypeCount; ++i) { @@ -6270,6 +6274,7 @@ class Resource final image_ = VK_NULL_HANDLE; memory_ = VK_NULL_HANDLE; staged_title_ = false; + shared_zero_ = VK_NULL_HANDLE; log_once("no room for a staged title image; retrying host-copyable backing, format", int(info.format)); return create_texture(format, asked_castable, origin, false); }; @@ -6495,6 +6500,7 @@ class Resource final } if (shared_zero_) { if (host_transfer_ && !host_initialize()) return false; + if (!host_transfer_) device_.context->note_shader_image(id()); // zeros until something fills it publish_streamed_texture(); } return true; @@ -7214,10 +7220,11 @@ class Resource final std::shared_ptr heap_range_; }; -// One immutable, host-zeroed BC image per exact view shape. It has the right sampled format, extent, +// One immutable, zeroed BC image per exact view shape (zeroed on the host, or by the first shader submission). It has +// the right sampled format, extent, // mip count and zero-block decoding even before the title first supplies data or copies into the placement. VkImage DeviceState::zero_sampled_image(const D3D12_RESOURCE_DESC& d, const Format& format, VkDeviceSize bytes) { - if (!host_transfer_format(format.format)) return VK_NULL_HANDLE; + if (copy_memory_to_image && !host_transfer_format(format.format)) return VK_NULL_HANDLE; const ZeroShape key{d.Format, d.Width, d.Height, d.DepthOrArraySize, d.MipLevels}; std::lock_guard lock(zero_image_mutex); if (auto it = zero_images.find(key); it != zero_images.end()) return it->second ? it->second->image() : VK_NULL_HANDLE; @@ -7226,12 +7233,13 @@ VkImage DeviceState::zero_sampled_image(const D3D12_RESOURCE_DESC& d, const Form if (used >= limit || bytes > limit - used) return VK_NULL_HANDLE; const D3D12_HEAP_PROPERTIES heap{D3D12_HEAP_TYPE_CUSTOM, D3D12_CPU_PAGE_PROPERTY_WRITE_BACK, D3D12_MEMORY_POOL_L0, 1, 1}; auto* zero = new Resource(*this, d, heap, D3D12_HEAP_FLAG_NONE); - if (FAILED(zero->create_texture(format)) || !zero->host_initialized_image() || + if (FAILED(zero->create_texture(format)) || (copy_memory_to_image && !zero->host_initialized_image()) || zero->image_backing_bytes() > limit - used) { zero->Release(); zero_images.emplace(key, nullptr); return VK_NULL_HANDLE; } + if (!zero->host_initialized_image()) context->note_shader_image(zero->id()); // cleared before a shader reads it zero_images.emplace(key, zero); zero_image_bytes.fetch_add(zero->image_backing_bytes(), std::memory_order_relaxed); return zero->image(); diff --git a/runtime/test/d3d12_existing_heap_test.cpp b/runtime/test/d3d12_existing_heap_test.cpp index 3d65fc7409..2c7b25b1cf 100644 --- a/runtime/test/d3d12_existing_heap_test.cpp +++ b/runtime/test/d3d12_existing_heap_test.cpp @@ -1236,7 +1236,7 @@ int check_placed_target_backing(ID3D12Device* device) { // Two sampled BC placements have distinct T#s. A CPU write to the first and a GPU copy into // the second must each promote only their destination and keep previously copied descriptors usable. -int check_shared_bc_placements(ID3D12Device* device) { +int check_shared_bc_placements(ID3D12Device* device, bool must_share = false) { const char* who = "shared slot-133 BC images"; if (!rt::d3d12::bindless_set_layout(device)) return 0; constexpr UINT width = 32, height = 32, pitch = width / 4 * 8; @@ -1259,6 +1259,8 @@ int check_shared_bc_placements(ID3D12Device* device) { const auto second = rt::d3d12::bindless_hash_lookup(device, bv); EXPECT(first && second && first != second); const bool shared = !rt::d3d12::vulkan_memory_flags(a.p); + const char* defer = std::getenv("LEONIDA_PLACED_IMAGE_DEFER"); + EXPECT(shared || !must_share || (defer && !std::strcmp(defer, "0"))); EXPECT(bool(rt::d3d12::vulkan_memory_flags(b.p)) == !shared); std::array pattern{}, result{}; for (std::size_t i = 0; i < pattern.size(); ++i) pattern[i] = std::uint8_t(i * 13 + 71); @@ -2036,6 +2038,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; + failures += check_shared_bc_placements(staged.p, true); failures += check_mapped_plane(staged.p); failures += check_mapped_bc_array(staged.p); }