From 13f2e50aee42ff20e4bcf7e40a4b6047d7394096 Mon Sep 17 00:00:00 2001 From: Yiwei Zhang Date: Fri, 15 Oct 2021 18:37:23 +0000 Subject: [PATCH] venus: add buffer cache init and usage flows 1. struct vn_buffer_cache_entry for buffer memory requirements 2. struct vn_buffer_cache for all buffer related cached info 3. implement vn_buffer_cache_init 4. implement vn_buffer_cache_fini 5. empty vn_buffer_get_max_buffer_size 6. empty vn_buffer_cache_entries_create 7. implement vn_buffer_cache_entries_destroy 8. empty vn_buffer_cache_get_memory_requirements Signed-off-by: Yiwei Zhang Reviewed-by: Chia-I Wu Reviewed-by: Ryan Neph Part-of: --- src/virtio/vulkan/vn_android.c | 2 +- src/virtio/vulkan/vn_buffer.c | 82 ++++++++++++++++++++++++++++++++++ src/virtio/vulkan/vn_buffer.h | 23 ++++++++++ src/virtio/vulkan/vn_device.c | 15 +++---- src/virtio/vulkan/vn_device.h | 4 +- 5 files changed, 113 insertions(+), 13 deletions(-) diff --git a/src/virtio/vulkan/vn_android.c b/src/virtio/vulkan/vn_android.c index 41642350fac..2d847dc2117 100644 --- a/src/virtio/vulkan/vn_android.c +++ b/src/virtio/vulkan/vn_android.c @@ -1238,7 +1238,7 @@ vn_android_buffer_from_ahb(struct vn_device *dev, * properties. */ (*out_buf)->requirements.memory.memoryRequirements.memoryTypeBits &= - dev->ahb_buffer_memory_type_bits; + dev->buffer_cache.ahb_mem_type_bits; assert((*out_buf)->requirements.memory.memoryRequirements.memoryTypeBits); diff --git a/src/virtio/vulkan/vn_buffer.c b/src/virtio/vulkan/vn_buffer.c index fcd03828999..5a5a8d25e5f 100644 --- a/src/virtio/vulkan/vn_buffer.c +++ b/src/virtio/vulkan/vn_buffer.c @@ -19,6 +19,81 @@ /* buffer commands */ +static VkResult +vn_buffer_cache_entries_create(struct vn_device *dev, + struct vn_buffer_cache_entry **out_entries, + uint32_t *out_entry_count) +{ + *out_entries = NULL; + *out_entry_count = 0; + return VK_SUCCESS; +} + +static void +vn_buffer_cache_entries_destroy(struct vn_device *dev, + struct vn_buffer_cache_entry *entries) +{ + const VkAllocationCallbacks *alloc = &dev->base.base.alloc; + + if (entries) + vk_free(alloc, entries); +} + +static VkResult +vn_buffer_get_max_buffer_size(struct vn_device *dev, + uint64_t *out_max_buffer_size) +{ + *out_max_buffer_size = 0; + return VK_SUCCESS; +} + +VkResult +vn_buffer_cache_init(struct vn_device *dev) +{ + uint32_t ahb_mem_type_bits = 0; + uint64_t max_buffer_size = 0; + struct vn_buffer_cache_entry *entries = NULL; + uint32_t entry_count = 0; + VkResult result; + + if (dev->base.base.enabled_extensions + .ANDROID_external_memory_android_hardware_buffer) { + result = + vn_android_get_ahb_buffer_memory_type_bits(dev, &ahb_mem_type_bits); + if (result != VK_SUCCESS) + return result; + } + + result = vn_buffer_get_max_buffer_size(dev, &max_buffer_size); + if (result != VK_SUCCESS) + return result; + + result = vn_buffer_cache_entries_create(dev, &entries, &entry_count); + if (result != VK_SUCCESS) + return result; + + dev->buffer_cache.ahb_mem_type_bits = ahb_mem_type_bits; + dev->buffer_cache.max_buffer_size = max_buffer_size; + dev->buffer_cache.entries = entries; + dev->buffer_cache.entry_count = entry_count; + return VK_SUCCESS; +} + +void +vn_buffer_cache_fini(struct vn_device *dev) +{ + vn_buffer_cache_entries_destroy(dev, dev->buffer_cache.entries); +} + +static bool +vn_buffer_cache_get_memory_requirements( + struct vn_buffer_cache *cache, + const VkBufferCreateInfo *create_info, + struct vn_buffer_memory_requirements *out) +{ + return false; +} + static VkResult vn_buffer_init(struct vn_device *dev, const VkBufferCreateInfo *create_info, @@ -28,6 +103,13 @@ vn_buffer_init(struct vn_device *dev, VkBuffer buf_handle = vn_buffer_to_handle(buf); VkResult result; + if (vn_buffer_cache_get_memory_requirements( + &dev->buffer_cache, create_info, &buf->requirements)) { + vn_async_vkCreateBuffer(dev->instance, dev_handle, create_info, NULL, + &buf_handle); + return VK_SUCCESS; + } + result = vn_call_vkCreateBuffer(dev->instance, dev_handle, create_info, NULL, &buf_handle); if (result != VK_SUCCESS) diff --git a/src/virtio/vulkan/vn_buffer.h b/src/virtio/vulkan/vn_buffer.h index 97277dc46c8..5ece8589a1f 100644 --- a/src/virtio/vulkan/vn_buffer.h +++ b/src/virtio/vulkan/vn_buffer.h @@ -18,6 +18,23 @@ struct vn_buffer_memory_requirements { VkMemoryDedicatedRequirements dedicated; }; +struct vn_buffer_cache_entry { + const VkBufferCreateInfo *create_info; + + struct vn_buffer_memory_requirements requirements; +}; + +struct vn_buffer_cache { + /* cache memory type requirement for AHB backed VkBuffer */ + uint32_t ahb_mem_type_bits; + + uint64_t max_buffer_size; + + /* cache memory requirements for common native buffer infos */ + struct vn_buffer_cache_entry *entries; + uint32_t entry_count; +}; + struct vn_buffer { struct vn_object_base base; @@ -42,4 +59,10 @@ vn_buffer_create(struct vn_device *dev, const VkAllocationCallbacks *alloc, struct vn_buffer **out_buf); +VkResult +vn_buffer_cache_init(struct vn_device *dev); + +void +vn_buffer_cache_fini(struct vn_device *dev); + #endif /* VN_BUFFER_H */ diff --git a/src/virtio/vulkan/vn_device.c b/src/virtio/vulkan/vn_device.c index e2d6d6b9ef8..f1d9e0c3a1d 100644 --- a/src/virtio/vulkan/vn_device.c +++ b/src/virtio/vulkan/vn_device.c @@ -302,16 +302,9 @@ vn_device_init(struct vn_device *dev, mtx_init(&pool->mutex, mtx_plain); } - if (dev->base.base.enabled_extensions - .ANDROID_external_memory_android_hardware_buffer) { - uint32_t mem_type_bits = 0; - result = - vn_android_get_ahb_buffer_memory_type_bits(dev, &mem_type_bits); - if (result != VK_SUCCESS) - goto fail; - - dev->ahb_buffer_memory_type_bits = mem_type_bits; - } + result = vn_buffer_cache_init(dev); + if (result != VK_SUCCESS) + goto fail; return VK_SUCCESS; @@ -381,6 +374,8 @@ vn_DestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) if (!dev) return; + vn_buffer_cache_fini(dev); + for (uint32_t i = 0; i < ARRAY_SIZE(dev->memory_pools); i++) vn_device_memory_pool_fini(dev, i); diff --git a/src/virtio/vulkan/vn_device.h b/src/virtio/vulkan/vn_device.h index 0efd0b26023..d3875f73ee1 100644 --- a/src/virtio/vulkan/vn_device.h +++ b/src/virtio/vulkan/vn_device.h @@ -13,6 +13,7 @@ #include "vn_common.h" +#include "vn_buffer.h" #include "vn_device_memory.h" struct vn_device { @@ -27,8 +28,7 @@ struct vn_device { struct vn_device_memory_pool memory_pools[VK_MAX_MEMORY_TYPES]; - /* cache memory type requirement for AHB backed VkBuffer */ - uint32_t ahb_buffer_memory_type_bits; + struct vn_buffer_cache buffer_cache; }; VK_DEFINE_HANDLE_CASTS(vn_device, base.base.base,