From b39f4573634505ff167e39b9daac3d89bb10f140 Mon Sep 17 00:00:00 2001 From: kd-11 Date: Tue, 13 Dec 2022 02:56:55 +0300 Subject: [PATCH] vk: Zero-initialize scratch VRAM allocations --- rpcs3/Emu/RSX/VK/vkutils/scratch.cpp | 20 ++++++++++++++++---- rpcs3/Emu/RSX/VK/vkutils/scratch.h | 4 +--- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/rpcs3/Emu/RSX/VK/vkutils/scratch.cpp b/rpcs3/Emu/RSX/VK/vkutils/scratch.cpp index 75e7430e53..9697b5fbf4 100644 --- a/rpcs3/Emu/RSX/VK/vkutils/scratch.cpp +++ b/rpcs3/Emu/RSX/VK/vkutils/scratch.cpp @@ -152,9 +152,10 @@ namespace vk return ptr.get(); } - vk::buffer* get_scratch_buffer(u32 queue_family, u64 min_required_size) + std::pair get_scratch_buffer(u32 queue_family, u64 min_required_size) { auto& scratch_buffer = g_scratch_buffers_pool[queue_family].get_buf(); + bool is_new = false; if (scratch_buffer && scratch_buffer->size() < min_required_size) { @@ -170,14 +171,25 @@ namespace vk scratch_buffer = std::make_unique(*g_render_device, alloc_size, g_render_device->get_memory_mapping().device_local, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 0, VMM_ALLOCATION_POOL_SCRATCH); + + is_new = true; } - return scratch_buffer.get(); + return { scratch_buffer.get(), is_new }; } - vk::buffer* get_scratch_buffer(const vk::command_buffer& cmd, u64 min_required_size) + vk::buffer* get_scratch_buffer(const vk::command_buffer& cmd, u64 min_required_size, bool zero_memory) { - return get_scratch_buffer(cmd.get_queue_family(), min_required_size); + const auto [buf, init_mem] = get_scratch_buffer(cmd.get_queue_family(), min_required_size); + + if (init_mem || zero_memory) + { + // Zero-initialize the allocated VRAM + const u64 zero_length = init_mem ? buf->size() : utils::align(min_required_size, 4); + vkCmdFillBuffer(cmd, buf->value, 0, zero_length, 0); + } + + return buf; } void clear_scratch_resources() diff --git a/rpcs3/Emu/RSX/VK/vkutils/scratch.h b/rpcs3/Emu/RSX/VK/vkutils/scratch.h index 4baed891b7..312db68c8a 100644 --- a/rpcs3/Emu/RSX/VK/vkutils/scratch.h +++ b/rpcs3/Emu/RSX/VK/vkutils/scratch.h @@ -6,9 +6,7 @@ namespace vk VkSampler null_sampler(); image_view* null_image_view(const command_buffer& cmd, VkImageViewType type); image* get_typeless_helper(VkFormat format, rsx::format_class format_class, u32 requested_width, u32 requested_height); - - buffer* get_scratch_buffer(u32 queue_family, u64 min_required_size); - buffer* get_scratch_buffer(const command_buffer& cmd, u64 min_required_size); + buffer* get_scratch_buffer(const command_buffer& cmd, u64 min_required_size, bool zero_memory = false); void clear_scratch_resources(); }