From 83e73a2f590437143f630a4eea008fd70a91f2bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Manuel=20J=C3=A1quez=20Leal?= Date: Sat, 1 Apr 2023 11:25:07 +0200 Subject: [PATCH] vulkan: find memory type index with requirements The specification says: VUID-vkAllocateMemory-pAllocateInfo-01713 must pAllocateInfo->allocationSize be less than or equal to VkPhysicalDeviceMemoryProperties::memoryHeaps[memindex].size where memindex = VkPhysicalDeviceMemoryProperties::memoryTypes[pAllocateInfo->memoryTypeIndex].heapIndex as returned by vkGetPhysicalDeviceMemoryProperties for the VkPhysicalDevice that device was created from. Though this can be catch by the validation layer, the requested frame size depends on the use case so it's better to check this restriction by our code. This patch also makes use of this new function to find memory type index, and removes the unused function to find memory type index, which, as GstVulkan is considered unstable, we can do it. Part-of: --- .../gst-libs/gst/vulkan/gstvkbuffermemory.c | 4 +- .../gst-libs/gst/vulkan/gstvkimagememory.c | 4 +- .../gst-libs/gst/vulkan/gstvkmemory.c | 37 +++++++++++-------- .../gst-libs/gst/vulkan/gstvkmemory.h | 5 ++- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkbuffermemory.c b/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkbuffermemory.c index d3437a4245..cf4040fd7f 100644 --- a/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkbuffermemory.c +++ b/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkbuffermemory.c @@ -126,8 +126,8 @@ _vk_buffer_mem_new_alloc_with_buffer_info (GstAllocator * allocator, ¶ms, buffer_info->size, user_data, notify); mem->buffer = buffer; - if (!gst_vulkan_memory_find_memory_type_index_with_type_properties (device, - mem->requirements.memoryTypeBits, mem_prop_flags, &type_idx)) + if (!gst_vulkan_memory_find_memory_type_index_with_requirements (device, + &mem->requirements, mem_prop_flags, &type_idx)) goto error; mem->vk_mem = (GstVulkanMemory *) gst_vulkan_memory_alloc (device, type_idx, diff --git a/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkimagememory.c b/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkimagememory.c index 4fc3215248..755fe3823d 100644 --- a/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkimagememory.c +++ b/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkimagememory.c @@ -169,8 +169,8 @@ _vk_image_mem_new_alloc_with_image_info (GstAllocator * allocator, "vkGetPhysicalDeviceImageFormatProperties") < 0) goto vk_error; - if (!gst_vulkan_memory_find_memory_type_index_with_type_properties (device, - mem->requirements.memoryTypeBits, mem_prop_flags, &type_idx)) + if (!gst_vulkan_memory_find_memory_type_index_with_requirements (device, + &mem->requirements, mem_prop_flags, &type_idx)) goto error; if ((mem->requirements.alignment & (mem->requirements.alignment - 1)) != 0) { diff --git a/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkmemory.c b/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkmemory.c index 18bd5b316f..c38c2592a6 100644 --- a/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkmemory.c +++ b/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkmemory.c @@ -236,34 +236,39 @@ _vk_mem_free (GstAllocator * allocator, GstMemory * memory) } /** - * gst_vulkan_memory_find_memory_type_index_with_type_properties: + * gst_vulkan_memory_find_memory_type_index_with_requirements: * @device: a #GstVulkanDevice - * @type_bits: memory type bits to search for + * @req: memory requirements to look for * @properties: memory properties to search for - * @type_index: resulting index of the memory type + * @type_index: (out): resulting index of the memory type * * Returns: whether a valid memory type could be found * - * Since: 1.18 + * Since: 1.24 */ gboolean -gst_vulkan_memory_find_memory_type_index_with_type_properties (GstVulkanDevice * - device, guint32 type_bits, VkMemoryPropertyFlags properties, +gst_vulkan_memory_find_memory_type_index_with_requirements (GstVulkanDevice * + device, VkMemoryRequirements * req, VkMemoryPropertyFlags properties, guint32 * type_index) { guint32 i; + VkPhysicalDeviceMemoryProperties *props; + + g_return_val_if_fail (GST_IS_VULKAN_DEVICE (device), FALSE); + + props = &device->physical_device->memory_properties; /* Search memtypes to find first index with those properties */ - for (i = 0; i < 32; i++) { - if ((type_bits & 1) == 1) { - /* Type is available, does it match user properties? */ - if ((device->physical_device->memory_properties.memoryTypes[i]. - propertyFlags & properties) == properties) { - *type_index = i; - return TRUE; - } - } - type_bits >>= 1; + for (i = 0; i < props->memoryTypeCount; i++) { + if (!(req->memoryTypeBits & (1 << i))) + continue; + if ((props->memoryTypes[i].propertyFlags & properties) != properties) + continue; + if (req->size > props->memoryHeaps[props->memoryTypes[i].heapIndex].size) + continue; + + *type_index = i; + return TRUE; } return FALSE; diff --git a/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkmemory.h b/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkmemory.h index 129b377244..9e6defa147 100644 --- a/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkmemory.h +++ b/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkmemory.h @@ -138,11 +138,12 @@ GstMemory * gst_vulkan_memory_alloc (GstVulkanDevice * device, VkMemoryPropertyFlags mem_prop_flags); GST_VULKAN_API -gboolean gst_vulkan_memory_find_memory_type_index_with_type_properties (GstVulkanDevice * device, - guint32 type_bits, +gboolean gst_vulkan_memory_find_memory_type_index_with_requirements (GstVulkanDevice * device, + VkMemoryRequirements * req, VkMemoryPropertyFlags properties, guint32 * type_index); + G_END_DECLS #endif /* _GST_VULKAN_BASE_BUFFER_H_ */