From 1e122861daa3bee283dcd0d8692508e54ce10b32 Mon Sep 17 00:00:00 2001 From: Yiwei Zhang Date: Wed, 14 Feb 2024 14:22:22 -0800 Subject: [PATCH] venus: refactor to add vn_cached_storage Signed-off-by: Yiwei Zhang Part-of: --- src/virtio/vulkan/vn_command_buffer.c | 32 ++++++------------- src/virtio/vulkan/vn_command_buffer.h | 11 ++----- src/virtio/vulkan/vn_common.h | 46 +++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 32 deletions(-) diff --git a/src/virtio/vulkan/vn_command_buffer.c b/src/virtio/vulkan/vn_command_buffer.c index b814d7feff6..a940de7d813 100644 --- a/src/virtio/vulkan/vn_command_buffer.c +++ b/src/virtio/vulkan/vn_command_buffer.c @@ -67,30 +67,12 @@ vn_dependency_info_has_present_src(uint32_t dep_count, return false; } -static void * -vn_cmd_get_tmp_data(struct vn_command_buffer *cmd, size_t size) -{ - struct vn_command_pool *pool = cmd->pool; - /* avoid shrinking in case of non efficient reallocation implementation */ - if (size > pool->tmp.size) { - void *data = - vk_realloc(&pool->allocator, pool->tmp.data, size, VN_DEFAULT_ALIGN, - VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); - if (!data) - return NULL; - - pool->tmp.data = data; - pool->tmp.size = size; - } - - return pool->tmp.data; -} - static inline VkImageMemoryBarrier * vn_cmd_get_image_memory_barriers(struct vn_command_buffer *cmd, uint32_t count) { - return vn_cmd_get_tmp_data(cmd, count * sizeof(VkImageMemoryBarrier)); + return vn_cached_storage_get(&cmd->pool->storage, + count * sizeof(VkImageMemoryBarrier)); } /* About VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, the spec says @@ -403,7 +385,7 @@ vn_cmd_fix_dependency_infos(struct vn_command_buffer *cmd, size_t tmp_size = dep_count * sizeof(VkDependencyInfo) + total_barrier_count * sizeof(VkImageMemoryBarrier2); - void *tmp = vn_cmd_get_tmp_data(cmd, tmp_size); + void *tmp = vn_cached_storage_get(&cmd->pool->storage, tmp_size); if (!tmp) { cmd->state = VN_COMMAND_BUFFER_STATE_INVALID; return dep_infos; @@ -682,6 +664,8 @@ vn_CreateCommandPool(VkDevice device, list_inithead(&pool->command_buffers); list_inithead(&pool->free_query_batches); + vn_cached_storage_init(&pool->storage, alloc); + VkCommandPool pool_handle = vn_command_pool_to_handle(pool); vn_async_vkCreateCommandPool(dev->primary_ring, device, pCreateInfo, NULL, &pool_handle); @@ -735,8 +719,7 @@ vn_DestroyCommandPool(VkDevice device, &pool->free_query_batches, head) vk_free(alloc, batch); - if (pool->tmp.data) - vk_free(alloc, pool->tmp.data); + vn_cached_storage_fini(&pool->storage); vn_object_base_fini(&pool->base); vk_free(alloc, pool); @@ -784,6 +767,9 @@ vn_ResetCommandPool(VkDevice device, list_for_each_entry_safe(struct vn_feedback_query_batch, batch, &pool->free_query_batches, head) vk_free(&pool->allocator, batch); + + vn_cached_storage_fini(&pool->storage); + vn_cached_storage_init(&pool->storage, &pool->allocator); } vn_async_vkResetCommandPool(dev->primary_ring, device, commandPool, flags); diff --git a/src/virtio/vulkan/vn_command_buffer.h b/src/virtio/vulkan/vn_command_buffer.h index a08a455b0b4..16f079f12c1 100644 --- a/src/virtio/vulkan/vn_command_buffer.h +++ b/src/virtio/vulkan/vn_command_buffer.h @@ -42,15 +42,8 @@ struct vn_command_pool { */ struct list_head free_query_batches; - /* Temporary storage for scrubbing VK_IMAGE_LAYOUT_PRESENT_SRC_KHR. The - * storage's lifetime is the command pool's lifetime. We increase the - * storage as needed, but never shrink it. Upon used by the cmd buffer, the - * storage must fit within command scope to avoid locking or suballocation. - */ - struct { - void *data; - size_t size; - } tmp; + /* for scrubbing VK_IMAGE_LAYOUT_PRESENT_SRC_KHR */ + struct vn_cached_storage storage; }; VK_DEFINE_NONDISP_HANDLE_CASTS(vn_command_pool, base.base, diff --git a/src/virtio/vulkan/vn_common.h b/src/virtio/vulkan/vn_common.h index f5b71179310..5cb93b1b2a6 100644 --- a/src/virtio/vulkan/vn_common.h +++ b/src/virtio/vulkan/vn_common.h @@ -238,6 +238,21 @@ struct vn_tls { struct list_head tls_rings; }; +/* A cached storage for object internal usages with below constraints: + * - It belongs to the object and shares the lifetime. + * - The storage reuse is protected by external synchronization. + * - The returned storage is not zero-initialized. + * - It never shrinks unless being purged via fini. + * + * The current users are: + * - VkCommandPool + */ +struct vn_cached_storage { + const VkAllocationCallbacks *alloc; + size_t size; + void *data; +}; + void vn_env_init(void); @@ -548,4 +563,35 @@ vn_cache_key_equal_function(const void *key1, const void *key2) return memcmp(key1, key2, SHA1_DIGEST_LENGTH) == 0; } +static inline void +vn_cached_storage_init(struct vn_cached_storage *storage, + const VkAllocationCallbacks *alloc) +{ + storage->alloc = alloc; + storage->size = 0; + storage->data = NULL; +} + +static inline void * +vn_cached_storage_get(struct vn_cached_storage *storage, size_t size) +{ + if (size > storage->size) { + void *data = + vk_realloc(storage->alloc, storage->data, size, VN_DEFAULT_ALIGN, + VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); + if (!data) + return NULL; + + storage->size = size; + storage->data = data; + } + return storage->data; +} + +static inline void +vn_cached_storage_fini(struct vn_cached_storage *storage) +{ + vk_free(storage->alloc, storage->data); +} + #endif /* VN_COMMON_H */