From 0606f3bc0c49a7936f0f57e9301fbbe662d35316 Mon Sep 17 00:00:00 2001 From: ziga-lunarg Date: Sat, 4 Jan 2025 17:57:01 +0200 Subject: [PATCH 1/3] Fix trim with external formats --- framework/decode/vulkan_object_info.h | 3 + .../decode/vulkan_replay_consumer_base.cpp | 21 +++++++ .../vulkan_replay_dump_resources_common.cpp | 2 + framework/encode/vulkan_handle_wrappers.h | 1 + framework/encode/vulkan_state_tracker.cpp | 10 ++++ .../vulkan_state_tracker_initializers.h | 6 ++ framework/encode/vulkan_state_writer.cpp | 37 ++++++++---- framework/graphics/vulkan_resources_util.cpp | 59 ++++++++++++------- framework/graphics/vulkan_resources_util.h | 2 + 9 files changed, 108 insertions(+), 33 deletions(-) diff --git a/framework/decode/vulkan_object_info.h b/framework/decode/vulkan_object_info.h index 4d18a647d3..b278cc9872 100644 --- a/framework/decode/vulkan_object_info.h +++ b/framework/decode/vulkan_object_info.h @@ -396,6 +396,7 @@ struct VulkanImageInfo : public VulkanObjectInfo VkImageUsageFlags usage{ 0 }; VkImageType type{}; VkFormat format{}; + bool external_format{ false }; VkExtent3D extent{ 0, 0, 0 }; VkImageTiling tiling{}; VkSampleCountFlagBits sample_count{}; @@ -406,6 +407,8 @@ struct VulkanImageInfo : public VulkanObjectInfo VkImageLayout current_layout{ VK_IMAGE_LAYOUT_UNDEFINED }; VkImageLayout intermediate_layout{ VK_IMAGE_LAYOUT_UNDEFINED }; + + VkDeviceSize size{ 0 }; }; struct VulkanPipelineCacheData diff --git a/framework/decode/vulkan_replay_consumer_base.cpp b/framework/decode/vulkan_replay_consumer_base.cpp index 62ecb3f9aa..740b6893f2 100644 --- a/framework/decode/vulkan_replay_consumer_base.cpp +++ b/framework/decode/vulkan_replay_consumer_base.cpp @@ -4745,6 +4745,14 @@ VkResult VulkanReplayConsumerBase::OverrideBindImageMemory(PFN_vkBindImageMemory image_info->handle, image_info->allocator_data, memory_info->allocator_data); } + if (image_info->external_format) + { + VkMemoryRequirements image_mem_reqs; + GetDeviceTable(device_info->handle) + ->GetImageMemoryRequirements(device_info->handle, image_info->handle, &image_mem_reqs); + image_info->size = image_mem_reqs.size; + } + return result; } @@ -5113,6 +5121,19 @@ VulkanReplayConsumerBase::OverrideCreateImage(PFN_vkCreateImage { image_info->queue_family_index = 0; } + + auto external_format_android = graphics::vulkan_struct_get_pnext(replay_create_info); + if (external_format_android && external_format_android->externalFormat != 0) + { + image_info->external_format = true; + } + else + { + VkMemoryRequirements image_mem_reqs; + GetDeviceTable(device_info->handle) + ->GetImageMemoryRequirements(device_info->handle, image_info->handle, &image_mem_reqs); + image_info->size = image_mem_reqs.size; + } } return result; diff --git a/framework/decode/vulkan_replay_dump_resources_common.cpp b/framework/decode/vulkan_replay_dump_resources_common.cpp index d6cb90d270..6a4fa05eb2 100644 --- a/framework/decode/vulkan_replay_dump_resources_common.cpp +++ b/framework/decode/vulkan_replay_dump_resources_common.cpp @@ -506,6 +506,8 @@ VkResult DumpImageToFile(const VulkanImageInfo* image_info, image_info->sample_count, (layout == VK_IMAGE_LAYOUT_MAX_ENUM) ? image_info->intermediate_layout : layout, image_info->queue_family_index, + image_info->external_format, + image_info->size, aspect, data, subresource_offsets, diff --git a/framework/encode/vulkan_handle_wrappers.h b/framework/encode/vulkan_handle_wrappers.h index 6c6143aa52..d3186896a4 100644 --- a/framework/encode/vulkan_handle_wrappers.h +++ b/framework/encode/vulkan_handle_wrappers.h @@ -220,6 +220,7 @@ struct ImageWrapper : public HandleWrapper, AssetWrapperBase { VkImageType image_type{ VK_IMAGE_TYPE_2D }; VkFormat format{ VK_FORMAT_UNDEFINED }; + bool external_format{ false }; VkExtent3D extent{ 0, 0, 0 }; uint32_t mip_levels{ 0 }; uint32_t array_layers{ 0 }; diff --git a/framework/encode/vulkan_state_tracker.cpp b/framework/encode/vulkan_state_tracker.cpp index 3d9a2d846a..e57ea90c69 100644 --- a/framework/encode/vulkan_state_tracker.cpp +++ b/framework/encode/vulkan_state_tracker.cpp @@ -556,6 +556,16 @@ void VulkanStateTracker::TrackImageMemoryBinding( { wrapper->bind_pnext = vulkan_trackers::TrackStruct(bind_info_pnext, wrapper->bind_pnext_memory); } + + // AHB image memory requirements can only be queried after the memory is bound + if (wrapper->external_format) + { + const VulkanDeviceTable* device_table = vulkan_wrappers::GetDeviceTable(device); + VkMemoryRequirements image_mem_reqs; + assert(wrapper->handle != VK_NULL_HANDLE); + device_table->GetImageMemoryRequirements(device, wrapper->handle, &image_mem_reqs); + wrapper->size = image_mem_reqs.size; + } } void VulkanStateTracker::TrackMappedMemory(VkDevice device, diff --git a/framework/encode/vulkan_state_tracker_initializers.h b/framework/encode/vulkan_state_tracker_initializers.h index 2361053118..a0e11681a5 100644 --- a/framework/encode/vulkan_state_tracker_initializers.h +++ b/framework/encode/vulkan_state_tracker_initializers.h @@ -652,6 +652,12 @@ inline void InitializeStatehandle != VK_NULL_HANDLE); device_table->GetImageMemoryRequirements(parent_handle, wrapper->handle, &image_mem_reqs); wrapper->size = image_mem_reqs.size; + + auto external_format_android = graphics::vulkan_struct_get_pnext(create_info); + if (external_format_android && external_format_android->externalFormat != 0) + { + wrapper->external_format = true; + } } template <> diff --git a/framework/encode/vulkan_state_writer.cpp b/framework/encode/vulkan_state_writer.cpp index afe054fb97..4e7c4c44f2 100644 --- a/framework/encode/vulkan_state_writer.cpp +++ b/framework/encode/vulkan_state_writer.cpp @@ -170,9 +170,9 @@ uint64_t VulkanStateWriter::WriteState(const VulkanStateTable& state_table, uint WriteMappedMemoryState(state_table); WriteBufferViewState(state_table); + StandardCreateWrite(state_table); WriteImageViewState(state_table); StandardCreateWrite(state_table); - StandardCreateWrite(state_table); // Retrieve buffer-device-addresses WriteBufferDeviceAddressState(state_table); @@ -2266,6 +2266,8 @@ void VulkanStateWriter::ProcessImageMemory(const vulkan_wrappers::DeviceWrapper* image_wrapper->samples, image_wrapper->current_layout, image_wrapper->queue_family_index, + image_wrapper->external_format, + image_wrapper->size, snapshot_entry.aspect, data, subresource_offsets, @@ -2422,6 +2424,8 @@ void VulkanStateWriter::ProcessImageMemoryWithAssetFile(const vulkan_wrappers::D image_wrapper->samples, image_wrapper->current_layout, image_wrapper->queue_family_index, + image_wrapper->external_format, + image_wrapper->size, snapshot_entry.aspect, data, subresource_offsets, @@ -2759,17 +2763,26 @@ void VulkanStateWriter::WriteImageMemoryState(const VulkanStateTable& state_tabl snapshot_info.need_staging_copy = need_staging_copy; snapshot_info.aspect = aspect; - snapshot_info.resource_size = resource_util.GetImageResourceSizesOptimal(wrapper->handle, - wrapper->format, - wrapper->image_type, - wrapper->extent, - wrapper->mip_levels, - wrapper->array_layers, - wrapper->tiling, - aspect, - nullptr, - &snapshot_info.level_sizes, - true); + if (wrapper->external_format) + { + snapshot_info.resource_size = wrapper->size; + snapshot_info.level_sizes.push_back(wrapper->size); + } + else + { + snapshot_info.resource_size = + resource_util.GetImageResourceSizesOptimal(wrapper->handle, + wrapper->format, + wrapper->image_type, + wrapper->extent, + wrapper->mip_levels, + wrapper->array_layers, + wrapper->tiling, + aspect, + nullptr, + &snapshot_info.level_sizes, + true); + } if ((*max_resource_size) < snapshot_info.resource_size) { diff --git a/framework/graphics/vulkan_resources_util.cpp b/framework/graphics/vulkan_resources_util.cpp index dc84917474..49853b6cf9 100644 --- a/framework/graphics/vulkan_resources_util.cpp +++ b/framework/graphics/vulkan_resources_util.cpp @@ -1642,6 +1642,8 @@ VkResult VulkanResourcesUtil::ReadFromImageResourceStaging(VkImage VkSampleCountFlags samples, VkImageLayout layout, uint32_t queue_family_index, + bool external_format, + VkDeviceSize size, VkImageAspectFlagBits aspect, std::vector& data, std::vector& subresource_offsets, @@ -1691,17 +1693,25 @@ VkResult VulkanResourcesUtil::ReadFromImageResourceStaging(VkImage subresource_offsets.clear(); subresource_sizes.clear(); - resource_size = GetImageResourceSizesOptimal(image, - use_blit ? dst_format : format, - type, - use_blit ? scaled_extent : extent, - mip_levels, - array_layers, - tiling, - aspect, - &subresource_offsets, - &subresource_sizes, - all_layers_per_level); + if (external_format) + { + resource_size = size; + subresource_sizes.push_back(resource_size); + } + else + { + resource_size = GetImageResourceSizesOptimal(image, + use_blit ? dst_format : format, + type, + use_blit ? scaled_extent : extent, + mip_levels, + array_layers, + tiling, + aspect, + &subresource_offsets, + &subresource_sizes, + all_layers_per_level); + } queue = GetQueue(queue_family_index, 0); if (queue == VK_NULL_HANDLE) @@ -1794,16 +1804,23 @@ VkResult VulkanResourcesUtil::ReadFromImageResourceStaging(VkImage assert(scaled_image != VK_NULL_HANDLE); - // Copy image to staging buffer - CopyImageBuffer(scaled_image, - staging_buffer_.buffer, - use_blit ? scaled_extent : extent, - mip_levels, - array_layers, - aspect, - subresource_sizes, - all_layers_per_level, - kImageToBuffer); + if (external_format) + { + // Todo + } + else + { + // Copy image to staging buffer + CopyImageBuffer(scaled_image, + staging_buffer_.buffer, + use_blit ? scaled_extent : extent, + mip_levels, + array_layers, + aspect, + subresource_sizes, + all_layers_per_level, + kImageToBuffer); + } // Cache flushing barrier. Make results visible to host VkBufferMemoryBarrier buffer_barrier; diff --git a/framework/graphics/vulkan_resources_util.h b/framework/graphics/vulkan_resources_util.h index 87c154221a..4b97a31472 100644 --- a/framework/graphics/vulkan_resources_util.h +++ b/framework/graphics/vulkan_resources_util.h @@ -110,6 +110,8 @@ class VulkanResourcesUtil VkSampleCountFlags samples, VkImageLayout layout, uint32_t queue_family_index, + bool external_format, + VkDeviceSize size, VkImageAspectFlagBits aspect, std::vector& data, std::vector& subresource_offsets, From 606d132db75fcac9c96a203222368ede00536945 Mon Sep 17 00:00:00 2001 From: ziga-lunarg Date: Wed, 8 Jan 2025 13:39:28 +0200 Subject: [PATCH 2/3] Fix comments --- .../decode/vulkan_replay_consumer_base.cpp | 6 ++++-- framework/encode/vulkan_state_tracker.cpp | 3 +-- .../vulkan_state_tracker_initializers.h | 19 +++++++++++-------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/framework/decode/vulkan_replay_consumer_base.cpp b/framework/decode/vulkan_replay_consumer_base.cpp index 740b6893f2..888aae6206 100644 --- a/framework/decode/vulkan_replay_consumer_base.cpp +++ b/framework/decode/vulkan_replay_consumer_base.cpp @@ -4745,6 +4745,7 @@ VkResult VulkanReplayConsumerBase::OverrideBindImageMemory(PFN_vkBindImageMemory image_info->handle, image_info->allocator_data, memory_info->allocator_data); } + // Memory requirements for image with external format can only be queried after the memory is bound if (image_info->external_format) { VkMemoryRequirements image_mem_reqs; @@ -5122,8 +5123,9 @@ VulkanReplayConsumerBase::OverrideCreateImage(PFN_vkCreateImage image_info->queue_family_index = 0; } - auto external_format_android = graphics::vulkan_struct_get_pnext(replay_create_info); - if (external_format_android && external_format_android->externalFormat != 0) + // Memory requirements for image with external format can only be queried after the memory is bound + auto* external_format_android = graphics::vulkan_struct_get_pnext(replay_create_info); + if (external_format_android != nullptr && external_format_android->externalFormat != 0) { image_info->external_format = true; } diff --git a/framework/encode/vulkan_state_tracker.cpp b/framework/encode/vulkan_state_tracker.cpp index e57ea90c69..1a1b69d001 100644 --- a/framework/encode/vulkan_state_tracker.cpp +++ b/framework/encode/vulkan_state_tracker.cpp @@ -562,8 +562,7 @@ void VulkanStateTracker::TrackImageMemoryBinding( { const VulkanDeviceTable* device_table = vulkan_wrappers::GetDeviceTable(device); VkMemoryRequirements image_mem_reqs; - assert(wrapper->handle != VK_NULL_HANDLE); - device_table->GetImageMemoryRequirements(device, wrapper->handle, &image_mem_reqs); + device_table->GetImageMemoryRequirements(device, image, &image_mem_reqs); wrapper->size = image_mem_reqs.size; } } diff --git a/framework/encode/vulkan_state_tracker_initializers.h b/framework/encode/vulkan_state_tracker_initializers.h index a0e11681a5..d4d274fce9 100644 --- a/framework/encode/vulkan_state_tracker_initializers.h +++ b/framework/encode/vulkan_state_tracker_initializers.h @@ -647,16 +647,19 @@ inline void InitializeStatequeue_family_index = create_info->pQueueFamilyIndices[0]; } - const VulkanDeviceTable* device_table = vulkan_wrappers::GetDeviceTable(parent_handle); - VkMemoryRequirements image_mem_reqs; - assert(wrapper->handle != VK_NULL_HANDLE); - device_table->GetImageMemoryRequirements(parent_handle, wrapper->handle, &image_mem_reqs); - wrapper->size = image_mem_reqs.size; - - auto external_format_android = graphics::vulkan_struct_get_pnext(create_info); - if (external_format_android && external_format_android->externalFormat != 0) + auto* external_format_android = graphics::vulkan_struct_get_pnext(create_info); + if (external_format_android != nullptr && external_format_android->externalFormat != 0) { wrapper->external_format = true; + wrapper->size = 0; + } + else + { + const VulkanDeviceTable* device_table = vulkan_wrappers::GetDeviceTable(parent_handle); + VkMemoryRequirements image_mem_reqs; + assert(wrapper->handle != VK_NULL_HANDLE); + device_table->GetImageMemoryRequirements(parent_handle, wrapper->handle, &image_mem_reqs); + wrapper->size = image_mem_reqs.size; } } From 559c2afa53545ab936880349dc29e3d2214cff20 Mon Sep 17 00:00:00 2001 From: ziga-lunarg Date: Fri, 17 Jan 2025 17:34:45 +0200 Subject: [PATCH 3/3] Fix replay image handle --- framework/decode/vulkan_replay_consumer_base.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/decode/vulkan_replay_consumer_base.cpp b/framework/decode/vulkan_replay_consumer_base.cpp index 888aae6206..9ac9d909f9 100644 --- a/framework/decode/vulkan_replay_consumer_base.cpp +++ b/framework/decode/vulkan_replay_consumer_base.cpp @@ -5133,7 +5133,7 @@ VulkanReplayConsumerBase::OverrideCreateImage(PFN_vkCreateImage { VkMemoryRequirements image_mem_reqs; GetDeviceTable(device_info->handle) - ->GetImageMemoryRequirements(device_info->handle, image_info->handle, &image_mem_reqs); + ->GetImageMemoryRequirements(device_info->handle, *replay_image, &image_mem_reqs); image_info->size = image_mem_reqs.size; } }