From 1b179306dc0b83a75d5de2394f8edbac78edad0f Mon Sep 17 00:00:00 2001 From: Christoph Haag Date: Fri, 6 Aug 2021 13:48:33 +0200 Subject: [PATCH] comp: Only use dedicated allocation when supported/preferred Fixes OpenGL depth formats on Tegra --- src/xrt/auxiliary/vk/vk_helpers.c | 2 ++ src/xrt/auxiliary/vk/vk_helpers.h | 1 + src/xrt/auxiliary/vk/vk_image_allocator.c | 25 ++++++++++++++++--- src/xrt/auxiliary/vk/vk_image_allocator.h | 1 + .../client/comp_gl_memobj_swapchain.c | 2 +- src/xrt/compositor/main/comp_swapchain.c | 1 + src/xrt/include/xrt/xrt_compositor.h | 2 ++ src/xrt/ipc/client/ipc_client_compositor.c | 3 +++ src/xrt/ipc/server/ipc_server_handler.c | 3 +++ src/xrt/ipc/shared/ipcproto/common.py | 3 ++- src/xrt/ipc/shared/proto.json | 3 ++- src/xrt/ipc/shared/proto.schema.json | 3 ++- 12 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/xrt/auxiliary/vk/vk_helpers.c b/src/xrt/auxiliary/vk/vk_helpers.c index e14944058..f7b97291a 100644 --- a/src/xrt/auxiliary/vk/vk_helpers.c +++ b/src/xrt/auxiliary/vk/vk_helpers.c @@ -893,6 +893,8 @@ vk_get_device_functions(struct vk_bundle *vk) vk->vkFlushMappedMemoryRanges = GET_DEV_PROC(vk, vkFlushMappedMemoryRanges); vk->vkCreateImage = GET_DEV_PROC(vk, vkCreateImage); vk->vkGetImageMemoryRequirements = GET_DEV_PROC(vk, vkGetImageMemoryRequirements); + // because we use Vulkan API Version 1.0.x, we can only get the KHR version of this function + vk->vkGetImageMemoryRequirements2 = GET_DEV_PROC(vk, vkGetImageMemoryRequirements2KHR); vk->vkBindImageMemory = GET_DEV_PROC(vk, vkBindImageMemory); vk->vkDestroyImage = GET_DEV_PROC(vk, vkDestroyImage); vk->vkCreateImageView = GET_DEV_PROC(vk, vkCreateImageView); diff --git a/src/xrt/auxiliary/vk/vk_helpers.h b/src/xrt/auxiliary/vk/vk_helpers.h index b1e6dcdac..b2c7b4e73 100644 --- a/src/xrt/auxiliary/vk/vk_helpers.h +++ b/src/xrt/auxiliary/vk/vk_helpers.h @@ -150,6 +150,7 @@ struct vk_bundle PFN_vkCreateImage vkCreateImage; PFN_vkGetImageMemoryRequirements vkGetImageMemoryRequirements; + PFN_vkGetImageMemoryRequirements2 vkGetImageMemoryRequirements2; PFN_vkBindImageMemory vkBindImageMemory; PFN_vkDestroyImage vkDestroyImage; PFN_vkCreateImageView vkCreateImageView; diff --git a/src/xrt/auxiliary/vk/vk_image_allocator.c b/src/xrt/auxiliary/vk/vk_image_allocator.c index 53753cf2b..6472c1d4b 100644 --- a/src/xrt/auxiliary/vk/vk_image_allocator.c +++ b/src/xrt/auxiliary/vk/vk_image_allocator.c @@ -204,6 +204,24 @@ create_image(struct vk_bundle *vk, const struct xrt_swapchain_create_info *info, return ret; } + VkImageMemoryRequirementsInfo2 memory_requirements_info = { + .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2, + .image = image, + }; + + VkMemoryDedicatedRequirements memory_dedicated_requirements = { + .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS, + }; + VkMemoryRequirements2 memory_requirements = { + .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2, + .pNext = &memory_dedicated_requirements, + }; + vk->vkGetImageMemoryRequirements2(vk->device, &memory_requirements_info, &memory_requirements); + + VkBool32 use_dedicated_allocation = (memory_dedicated_requirements.requiresDedicatedAllocation != VK_FALSE) || + (memory_dedicated_requirements.prefersDedicatedAllocation != VK_FALSE); + U_LOG_D("create_image: Use dedicated allocation: %d", use_dedicated_allocation); + /* * Create and bind the memory. */ @@ -218,7 +236,7 @@ create_image(struct vk_bundle *vk, const struct xrt_swapchain_create_info *info, VkExportMemoryAllocateInfo export_alloc_info = { .sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR, - .pNext = &dedicated_memory_info, + .pNext = use_dedicated_allocation ? &dedicated_memory_info : NULL, .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR, }; @@ -226,7 +244,7 @@ create_image(struct vk_bundle *vk, const struct xrt_swapchain_create_info *info, VkExportMemoryAllocateInfo export_alloc_info = { .sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR, - .pNext = &dedicated_memory_info, + .pNext = use_dedicated_allocation ? &dedicated_memory_info : NULL, .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID, }; @@ -234,7 +252,7 @@ create_image(struct vk_bundle *vk, const struct xrt_swapchain_create_info *info, VkExportMemoryAllocateInfo export_alloc_info = { .sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR, - .pNext = &dedicated_memory_info, + .pNext = use_dedicated_allocation ? &dedicated_memory_info : NULL, .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT_KHR, }; @@ -252,6 +270,7 @@ create_image(struct vk_bundle *vk, const struct xrt_swapchain_create_info *info, out_image->handle = image; out_image->memory = device_memory; out_image->size = size; + out_image->use_dedicated_allocation = use_dedicated_allocation; return ret; } diff --git a/src/xrt/auxiliary/vk/vk_image_allocator.h b/src/xrt/auxiliary/vk/vk_image_allocator.h index db32337e9..864fd9e8f 100644 --- a/src/xrt/auxiliary/vk/vk_image_allocator.h +++ b/src/xrt/auxiliary/vk/vk_image_allocator.h @@ -28,6 +28,7 @@ struct vk_image VkImage handle; VkDeviceMemory memory; VkDeviceSize size; + bool use_dedicated_allocation; }; struct vk_image_collection diff --git a/src/xrt/compositor/client/comp_gl_memobj_swapchain.c b/src/xrt/compositor/client/comp_gl_memobj_swapchain.c index dbf089973..6a92c7e5d 100644 --- a/src/xrt/compositor/client/comp_gl_memobj_swapchain.c +++ b/src/xrt/compositor/client/comp_gl_memobj_swapchain.c @@ -96,7 +96,7 @@ client_gl_memobj_swapchain_create(struct xrt_compositor *xc, glCreateMemoryObjectsEXT(native_xsc->num_images, &sc->memory[0]); for (uint32_t i = 0; i < native_xsc->num_images; i++) { - GLint dedicated = GL_TRUE; + GLint dedicated = xscn->images[i].use_dedicated_allocation ? GL_TRUE : GL_FALSE; glMemoryObjectParameterivEXT(sc->memory[i], GL_DEDICATED_MEMORY_OBJECT_EXT, &dedicated); glImportMemoryFdEXT(sc->memory[i], xscn->images[i].size, GL_HANDLE_TYPE_OPAQUE_FD_EXT, xscn->images[i].handle); diff --git a/src/xrt/compositor/main/comp_swapchain.c b/src/xrt/compositor/main/comp_swapchain.c index 0bfd7fa58..a18dd31f5 100644 --- a/src/xrt/compositor/main/comp_swapchain.c +++ b/src/xrt/compositor/main/comp_swapchain.c @@ -327,6 +327,7 @@ comp_swapchain_create(struct xrt_compositor *xc, for (uint32_t i = 0; i < sc->vkic.num_images; i++) { sc->base.images[i].handle = handles[i]; sc->base.images[i].size = sc->vkic.images[i].size; + sc->base.images[i].use_dedicated_allocation = sc->vkic.images[i].use_dedicated_allocation; } do_post_create_vulkan_setup(c, info, sc); diff --git a/src/xrt/include/xrt/xrt_compositor.h b/src/xrt/include/xrt/xrt_compositor.h index 1add40ff2..c60845119 100644 --- a/src/xrt/include/xrt/xrt_compositor.h +++ b/src/xrt/include/xrt/xrt_compositor.h @@ -1370,6 +1370,8 @@ struct xrt_image_native * into Vulkan. */ size_t size; + + bool use_dedicated_allocation; }; /*! diff --git a/src/xrt/ipc/client/ipc_client_compositor.c b/src/xrt/ipc/client/ipc_client_compositor.c index 4279d1d49..ec0f105f4 100644 --- a/src/xrt/ipc/client/ipc_client_compositor.c +++ b/src/xrt/ipc/client/ipc_client_compositor.c @@ -207,12 +207,14 @@ swapchain_server_create(struct ipc_client_compositor *icc, uint32_t handle; uint32_t num_images; uint64_t size; + bool use_dedicated_allocation; r = ipc_call_swapchain_create(icc->ipc_c, // connection info, // in &handle, // out &num_images, // out &size, // out + &use_dedicated_allocation, // out remote_handles, // handles IPC_MAX_SWAPCHAIN_HANDLES); // handles if (r != XRT_SUCCESS) { @@ -232,6 +234,7 @@ swapchain_server_create(struct ipc_client_compositor *icc, for (uint32_t i = 0; i < num_images; i++) { ics->base.images[i].handle = remote_handles[i]; ics->base.images[i].size = size; + ics->base.images[i].use_dedicated_allocation = use_dedicated_allocation; } *out_xsc = &ics->base.base; diff --git a/src/xrt/ipc/server/ipc_server_handler.c b/src/xrt/ipc/server/ipc_server_handler.c index 5b3e786dc..9edb7afc0 100644 --- a/src/xrt/ipc/server/ipc_server_handler.c +++ b/src/xrt/ipc/server/ipc_server_handler.c @@ -679,6 +679,7 @@ ipc_handle_swapchain_create(volatile struct ipc_client_state *ics, uint32_t *out_id, uint32_t *out_num_images, uint64_t *out_size, + bool *out_use_dedicated_allocation, uint32_t max_num_handles, xrt_graphics_buffer_handle_t *out_handles, uint32_t *out_num_handles) @@ -718,6 +719,8 @@ ipc_handle_swapchain_create(volatile struct ipc_client_state *ics, *out_id = index; *out_size = xscn->images[0].size; *out_num_images = xsc->num_images; + // assuming all images allocated in the same swapchain have the same allocation requirements + *out_use_dedicated_allocation = xscn->images[0].use_dedicated_allocation; // Setup the fds. *out_num_handles = xsc->num_images; diff --git a/src/xrt/ipc/shared/ipcproto/common.py b/src/xrt/ipc/shared/ipcproto/common.py index 9c4d50f3b..3bb85afb7 100644 --- a/src/xrt/ipc/shared/ipcproto/common.py +++ b/src/xrt/ipc/shared/ipcproto/common.py @@ -55,7 +55,8 @@ class Arg: # Keep all these synchronized with the definitions in the JSON Schema. SCALAR_TYPES = set(("uint32_t", "int64_t", - "uint64_t")) + "uint64_t", + "bool")) AGGREGATE_RE = re.compile(r"((const )?struct|union) (xrt|ipc)_[a-z_]+") ENUM_RE = re.compile(r"enum xrt_[a-z_]+") diff --git a/src/xrt/ipc/shared/proto.json b/src/xrt/ipc/shared/proto.json index 8b27e7415..7078ec977 100644 --- a/src/xrt/ipc/shared/proto.json +++ b/src/xrt/ipc/shared/proto.json @@ -125,7 +125,8 @@ "out": [ {"name": "id", "type": "uint32_t"}, {"name": "num_images", "type": "uint32_t"}, - {"name": "size", "type": "uint64_t"} + {"name": "size", "type": "uint64_t"}, + {"name": "use_dedicated_allocation", "type": "bool"} ], "out_handles": {"type": "xrt_graphics_buffer_handle_t"} }, diff --git a/src/xrt/ipc/shared/proto.schema.json b/src/xrt/ipc/shared/proto.schema.json index ee044fa95..c291edabc 100644 --- a/src/xrt/ipc/shared/proto.schema.json +++ b/src/xrt/ipc/shared/proto.schema.json @@ -12,7 +12,8 @@ "enum": [ "uint32_t", "int64_t", - "uint64_t" + "uint64_t", + "bool" ] }, "aggregate": {