mirror of
https://gitlab.freedesktop.org/monado/monado.git
synced 2025-03-03 21:26:36 +00:00
aux/vk: Tidy vk_image_allocator
This commit is contained in:
parent
f7b1f738fa
commit
1c67113e20
|
@ -22,12 +22,28 @@
|
||||||
#ifdef XRT_GRAPHICS_BUFFER_HANDLE_IS_AHARDWAREBUFFER
|
#ifdef XRT_GRAPHICS_BUFFER_HANDLE_IS_AHARDWAREBUFFER
|
||||||
#include "android/android_ahardwarebuffer_allocator.h"
|
#include "android/android_ahardwarebuffer_allocator.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
*
|
*
|
||||||
* Helper functions.
|
* Helper functions.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
VkExternalMemoryHandleTypeFlags
|
||||||
|
get_image_memory_handle_type(void)
|
||||||
|
{
|
||||||
|
#if defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_AHARDWAREBUFFER)
|
||||||
|
return VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
|
||||||
|
#elif defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_WIN32_HANDLE)
|
||||||
|
return VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT_KHR;
|
||||||
|
#elif defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_FD)
|
||||||
|
return VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR;
|
||||||
|
#else
|
||||||
|
#error "need port"
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
#if defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_FD)
|
#if defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_FD)
|
||||||
|
|
||||||
static VkResult
|
static VkResult
|
||||||
|
@ -136,11 +152,13 @@ create_image(struct vk_bundle *vk, const struct xrt_swapchain_create_info *info,
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Out->pNext
|
||||||
VkAndroidHardwareBufferFormatPropertiesANDROID a_buffer_format_props = {
|
VkAndroidHardwareBufferFormatPropertiesANDROID a_buffer_format_props = {
|
||||||
.sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID,
|
.sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID,
|
||||||
.format = image_format,
|
.format = image_format,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Out
|
||||||
VkAndroidHardwareBufferPropertiesANDROID a_buffer_props = {
|
VkAndroidHardwareBufferPropertiesANDROID a_buffer_props = {
|
||||||
.sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_PROPERTIES_ANDROID,
|
.sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_PROPERTIES_ANDROID,
|
||||||
.pNext = &a_buffer_format_props,
|
.pNext = &a_buffer_format_props,
|
||||||
|
@ -153,29 +171,37 @@ create_image(struct vk_bundle *vk, const struct xrt_swapchain_create_info *info,
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* Start of create image call.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void *next_chain = NULL;
|
||||||
|
#define CHAIN(STRUCT) \
|
||||||
|
do { \
|
||||||
|
(STRUCT).pNext = next_chain; \
|
||||||
|
next_chain = (void *)&(STRUCT); \
|
||||||
|
} while (false)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Create the image.
|
* Create the image.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
VkExternalMemoryHandleTypeFlags memory_handle_type = get_image_memory_handle_type();
|
||||||
|
|
||||||
VkExternalMemoryImageCreateInfoKHR external_memory_image_create_info = {
|
VkExternalMemoryImageCreateInfoKHR external_memory_image_create_info = {
|
||||||
.sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO_KHR,
|
.sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO_KHR,
|
||||||
#if defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_AHARDWAREBUFFER)
|
.handleTypes = memory_handle_type,
|
||||||
.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID,
|
.pNext = next_chain,
|
||||||
#elif defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_WIN32_HANDLE)
|
|
||||||
.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT_KHR,
|
|
||||||
#elif defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_FD)
|
|
||||||
.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR,
|
|
||||||
#else
|
|
||||||
#error "need port"
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
CHAIN(external_memory_image_create_info);
|
||||||
|
|
||||||
#if defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_AHARDWAREBUFFER)
|
#if defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_AHARDWAREBUFFER)
|
||||||
VkExternalFormatANDROID format_android = {
|
VkExternalFormatANDROID format_android = {
|
||||||
.sType = VK_STRUCTURE_TYPE_EXTERNAL_FORMAT_ANDROID,
|
.sType = VK_STRUCTURE_TYPE_EXTERNAL_FORMAT_ANDROID,
|
||||||
.pNext = &external_memory_image_create_info,
|
|
||||||
.externalFormat = a_buffer_format_props.externalFormat,
|
.externalFormat = a_buffer_format_props.externalFormat,
|
||||||
};
|
};
|
||||||
|
CHAIN(format_android);
|
||||||
|
|
||||||
// Android can't allocate native sRGB.
|
// Android can't allocate native sRGB.
|
||||||
// Use UNORM and correct gamma later.
|
// Use UNORM and correct gamma later.
|
||||||
|
@ -186,11 +212,7 @@ create_image(struct vk_bundle *vk, const struct xrt_swapchain_create_info *info,
|
||||||
|
|
||||||
VkImageCreateInfo create_info = {
|
VkImageCreateInfo create_info = {
|
||||||
.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
|
.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
|
||||||
#if defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_AHARDWAREBUFFER)
|
.pNext = next_chain,
|
||||||
.pNext = &format_android,
|
|
||||||
#else
|
|
||||||
.pNext = &external_memory_image_create_info,
|
|
||||||
#endif
|
|
||||||
.imageType = VK_IMAGE_TYPE_2D,
|
.imageType = VK_IMAGE_TYPE_2D,
|
||||||
.format = image_format,
|
.format = image_format,
|
||||||
.extent = {.width = info->width, .height = info->height, .depth = 1},
|
.extent = {.width = info->width, .height = info->height, .depth = 1},
|
||||||
|
@ -209,18 +231,23 @@ create_image(struct vk_bundle *vk, const struct xrt_swapchain_create_info *info,
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// In
|
||||||
VkImageMemoryRequirementsInfo2 memory_requirements_info = {
|
VkImageMemoryRequirementsInfo2 memory_requirements_info = {
|
||||||
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2,
|
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2,
|
||||||
.image = image,
|
.image = image,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Out->pNext
|
||||||
VkMemoryDedicatedRequirements memory_dedicated_requirements = {
|
VkMemoryDedicatedRequirements memory_dedicated_requirements = {
|
||||||
.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS,
|
.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Out
|
||||||
VkMemoryRequirements2 memory_requirements = {
|
VkMemoryRequirements2 memory_requirements = {
|
||||||
.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
|
.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
|
||||||
.pNext = &memory_dedicated_requirements,
|
.pNext = &memory_dedicated_requirements,
|
||||||
};
|
};
|
||||||
|
|
||||||
vk->vkGetImageMemoryRequirements2(vk->device, &memory_requirements_info, &memory_requirements);
|
vk->vkGetImageMemoryRequirements2(vk->device, &memory_requirements_info, &memory_requirements);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -245,41 +272,21 @@ create_image(struct vk_bundle *vk, const struct xrt_swapchain_create_info *info,
|
||||||
/*
|
/*
|
||||||
* Create and bind the memory.
|
* Create and bind the memory.
|
||||||
*/
|
*/
|
||||||
// vkAllocateMemory parameters
|
|
||||||
|
// In->pNext->pNext
|
||||||
VkMemoryDedicatedAllocateInfoKHR dedicated_memory_info = {
|
VkMemoryDedicatedAllocateInfoKHR dedicated_memory_info = {
|
||||||
.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR,
|
.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR,
|
||||||
.image = image,
|
.image = image,
|
||||||
.buffer = VK_NULL_HANDLE,
|
.buffer = VK_NULL_HANDLE,
|
||||||
};
|
};
|
||||||
|
|
||||||
#if defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_FD)
|
// In->pNext
|
||||||
|
|
||||||
VkExportMemoryAllocateInfo export_alloc_info = {
|
VkExportMemoryAllocateInfo export_alloc_info = {
|
||||||
.sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR,
|
.sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR,
|
||||||
.pNext = use_dedicated_allocation ? &dedicated_memory_info : NULL,
|
.pNext = use_dedicated_allocation ? &dedicated_memory_info : NULL,
|
||||||
.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR,
|
.handleTypes = memory_handle_type,
|
||||||
};
|
};
|
||||||
|
|
||||||
#elif defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_AHARDWAREBUFFER)
|
|
||||||
|
|
||||||
VkExportMemoryAllocateInfo export_alloc_info = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR,
|
|
||||||
.pNext = use_dedicated_allocation ? &dedicated_memory_info : NULL,
|
|
||||||
.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID,
|
|
||||||
};
|
|
||||||
|
|
||||||
#elif defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_WIN32_HANDLE)
|
|
||||||
|
|
||||||
VkExportMemoryAllocateInfo export_alloc_info = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR,
|
|
||||||
.pNext = use_dedicated_allocation ? &dedicated_memory_info : NULL,
|
|
||||||
.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT_KHR,
|
|
||||||
};
|
|
||||||
|
|
||||||
#else
|
|
||||||
#error "need port"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
ret = vk_alloc_and_bind_image_memory(vk, image, SIZE_MAX, &export_alloc_info, &device_memory, &size);
|
ret = vk_alloc_and_bind_image_memory(vk, image, SIZE_MAX, &export_alloc_info, &device_memory, &size);
|
||||||
if (ret != VK_SUCCESS) {
|
if (ret != VK_SUCCESS) {
|
||||||
U_LOG_E("vkAllocateMemory: %s", vk_result_string(ret));
|
U_LOG_E("vkAllocateMemory: %s", vk_result_string(ret));
|
||||||
|
@ -378,8 +385,12 @@ vk_ic_from_natives(struct vk_bundle *vk,
|
||||||
// Ensure that all handles are consumed or none are.
|
// Ensure that all handles are consumed or none are.
|
||||||
xrt_graphics_buffer_handle_t buf = u_graphics_buffer_ref(native_images[i].handle);
|
xrt_graphics_buffer_handle_t buf = u_graphics_buffer_ref(native_images[i].handle);
|
||||||
|
|
||||||
ret = vk_create_image_from_native(vk, xscci, &native_images[i], &out_vkic->images[i].handle,
|
ret = vk_create_image_from_native( //
|
||||||
&out_vkic->images[i].memory);
|
vk, // vk_bundle
|
||||||
|
xscci, // info
|
||||||
|
&native_images[i], // image_native
|
||||||
|
&out_vkic->images[i].handle, // out_image
|
||||||
|
&out_vkic->images[i].memory); // out_mem
|
||||||
if (ret != VK_SUCCESS) {
|
if (ret != VK_SUCCESS) {
|
||||||
u_graphics_buffer_unref(&buf);
|
u_graphics_buffer_unref(&buf);
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in a new issue