Skip to content

Commit

Permalink
Fix trim with external formats
Browse files Browse the repository at this point in the history
  • Loading branch information
ziga-lunarg committed Jan 4, 2025
1 parent 599920e commit 6650268
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 33 deletions.
3 changes: 3 additions & 0 deletions framework/decode/vulkan_object_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ struct VulkanImageInfo : public VulkanObjectInfo<VkImage>
VkImageUsageFlags usage{ 0 };
VkImageType type{};
VkFormat format{};
bool external_format{ false };
VkExtent3D extent{ 0, 0, 0 };
VkImageTiling tiling{};
VkSampleCountFlagBits sample_count{};
Expand All @@ -406,6 +407,8 @@ struct VulkanImageInfo : public VulkanObjectInfo<VkImage>

VkImageLayout current_layout{ VK_IMAGE_LAYOUT_UNDEFINED };
VkImageLayout intermediate_layout{ VK_IMAGE_LAYOUT_UNDEFINED };

VkDeviceSize size{ 0 };
};

struct VulkanPipelineCacheData
Expand Down
20 changes: 20 additions & 0 deletions framework/decode/vulkan_replay_consumer_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4745,6 +4745,13 @@ 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;
}

Expand Down Expand Up @@ -5113,6 +5120,19 @@ VulkanReplayConsumerBase::OverrideCreateImage(PFN_vkCreateImage
{
image_info->queue_family_index = 0;
}

auto external_format_android = graphics::vulkan_struct_get_pnext<VkExternalFormatANDROID>(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;
Expand Down
2 changes: 2 additions & 0 deletions framework/decode/vulkan_replay_dump_resources_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,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,
Expand Down
1 change: 1 addition & 0 deletions framework/encode/vulkan_handle_wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ struct ImageWrapper : public HandleWrapper<VkImage>, 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 };
Expand Down
10 changes: 10 additions & 0 deletions framework/encode/vulkan_state_tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions framework/encode/vulkan_state_tracker_initializers.h
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,12 @@ inline void InitializeState<VkDevice, vulkan_wrappers::ImageWrapper, VkImageCrea
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<VkExternalFormatANDROID>(create_info);
if (external_format_android && external_format_android->externalFormat != 0)
{
wrapper->external_format = true;
}
}

template <>
Expand Down
37 changes: 25 additions & 12 deletions framework/encode/vulkan_state_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ uint64_t VulkanStateWriter::WriteState(const VulkanStateTable& state_table, uint
WriteMappedMemoryState(state_table);

WriteBufferViewState(state_table);
StandardCreateWrite<vulkan_wrappers::SamplerYcbcrConversionWrapper>(state_table);
WriteImageViewState(state_table);
StandardCreateWrite<vulkan_wrappers::SamplerWrapper>(state_table);
StandardCreateWrite<vulkan_wrappers::SamplerYcbcrConversionWrapper>(state_table);

// Retrieve buffer-device-addresses
WriteBufferDeviceAddressState(state_table);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
{
Expand Down
59 changes: 38 additions & 21 deletions framework/graphics/vulkan_resources_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1633,6 +1633,8 @@ VkResult VulkanResourcesUtil::ReadFromImageResourceStaging(VkImage
VkSampleCountFlags samples,
VkImageLayout layout,
uint32_t queue_family_index,
bool external_format,
VkDeviceSize size,
VkImageAspectFlagBits aspect,
std::vector<uint8_t>& data,
std::vector<uint64_t>& subresource_offsets,
Expand Down Expand Up @@ -1682,17 +1684,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)
Expand Down Expand Up @@ -1785,16 +1795,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;
Expand Down
2 changes: 2 additions & 0 deletions framework/graphics/vulkan_resources_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ class VulkanResourcesUtil
VkSampleCountFlags samples,
VkImageLayout layout,
uint32_t queue_family_index,
bool external_format,
VkDeviceSize size,
VkImageAspectFlagBits aspect,
std::vector<uint8_t>& data,
std::vector<uint64_t>& subresource_offsets,
Expand Down

0 comments on commit 6650268

Please sign in to comment.