Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -3326,7 +3326,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.
Expand Down
26 changes: 17 additions & 9 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9102,9 +9102,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) {
Expand All @@ -9113,11 +9121,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) {
Expand All @@ -9143,6 +9147,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);
};
Expand Down Expand Up @@ -9369,6 +9374,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;
Expand Down Expand Up @@ -10123,10 +10129,11 @@ class Resource final
std::shared_ptr<const leonida::TileHeapAllocation> 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;
Expand All @@ -10135,12 +10142,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();
Expand Down
5 changes: 4 additions & 1 deletion runtime/test/d3d12_existing_heap_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1398,7 +1398,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;
Expand All @@ -1421,6 +1421,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<std::uint8_t, pitch * height / 4> pattern{}, result{};
for (std::size_t i = 0; i < pattern.size(); ++i) pattern[i] = std::uint8_t(i * 13 + 71);
Expand Down Expand Up @@ -2400,6 +2402,7 @@ int main(int argc, char** argv) {
Ref<ID3D12Device> staged;
if (FAILED(rt::d3d12::create_vulkan_device(no_host_copy, IID_PPV_ARGS(&staged.p)))) return 1;
failures += check_placed_view_map_changes(staged.p);
failures += check_shared_bc_placements(staged.p, true);
failures += check_mapped_plane(staged.p);
failures += check_mapped_bc_array(staged.p);
}
Expand Down
Loading