Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix trim with external formats #1942

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
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: 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
23 changes: 23 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,15 @@ 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)
antonio-lunarg marked this conversation as resolved.
Show resolved Hide resolved
{
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 +5122,20 @@ VulkanReplayConsumerBase::OverrideCreateImage(PFN_vkCreateImage
{
image_info->queue_family_index = 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<VkExternalFormatANDROID>(replay_create_info);
if (external_format_android != nullptr && external_format_android->externalFormat != 0)
{
image_info->external_format = true;
antonio-lunarg marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
VkMemoryRequirements image_mem_reqs;
GetDeviceTable(device_info->handle)
->GetImageMemoryRequirements(device_info->handle, *replay_image, &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 @@ -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,
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
9 changes: 9 additions & 0 deletions framework/encode/vulkan_state_tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,15 @@ 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;
device_table->GetImageMemoryRequirements(device, image, &image_mem_reqs);
wrapper->size = image_mem_reqs.size;
}
}

void VulkanStateTracker::TrackMappedMemory(VkDevice device,
Expand Down
19 changes: 14 additions & 5 deletions framework/encode/vulkan_state_tracker_initializers.h
Original file line number Diff line number Diff line change
Expand Up @@ -647,11 +647,20 @@ inline void InitializeState<VkDevice, vulkan_wrappers::ImageWrapper, VkImageCrea
wrapper->queue_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<VkExternalFormatANDROID>(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;
}
}

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 =
antonio-lunarg marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -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<uint8_t>& data,
std::vector<uint64_t>& subresource_offsets,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
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
Loading