Module: Mesa
Branch: main
Commit: 892988956328ffdc1cc0cf181c2cd3b6394408e7
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=892988956328ffdc1cc0cf181c2cd3b6394408e7

Author: Andrew Gazizov <[email protected]>
Date:   Thu Nov 16 19:01:44 2023 +0100

venus: Tighten the conditions for guest_vram device memory alloc

In addition to the platform requirement (use_guest_vram), device memory
allocations from dedicated heap (guest_vram) are necessary only when:

1. VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT is set and it indicates that
memory is host visible and assumed to be accessed by CPU (vkMapMemory).
2. One of external memory handle types is set, that indicates memory
can be exported with external handle.

In other cases it's not necessary to create virtgpu_bo object in the
guest and enough just perform vkAllocateMemory on host side without
memory import from dedicated heap.

Reported-by: Yiwei Zhang <[email protected]>
Signed-off-by: Andrew D. Gazizov <[email protected]>
Reviewed-by: Yiwei Zhang <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/26130>

---

 src/virtio/vulkan/vn_device_memory.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/src/virtio/vulkan/vn_device_memory.c 
b/src/virtio/vulkan/vn_device_memory.c
index 6b363542e62..c3e4d5a8367 100644
--- a/src/virtio/vulkan/vn_device_memory.c
+++ b/src/virtio/vulkan/vn_device_memory.c
@@ -468,9 +468,14 @@ vn_device_memory_alloc(struct vn_device *dev,
                        const VkMemoryAllocateInfo *alloc_info)
 {
    struct vk_device_memory *mem_vk = &mem->base.base;
+   const VkMemoryType *mem_type = &dev->physical_device->memory_properties
+                                    .memoryTypes[mem_vk->memory_type_index];
+
+   const bool has_guest_vram = dev->renderer->info.has_guest_vram;
+   const bool host_visible =
+      mem_type->propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
+   const bool export_alloc = mem_vk->export_handle_types;
 
-   const bool has_guest_vram =
-      dev->physical_device->instance->renderer->info.has_guest_vram;
    const VkExternalMemoryHandleTypeFlagBits renderer_handle_type =
       dev->physical_device->external_memory.renderer_handle_type;
    struct vn_device_memory_alloc_info local_info;
@@ -483,9 +488,9 @@ vn_device_memory_alloc(struct vn_device *dev,
       mem_vk->export_handle_types = renderer_handle_type;
    }
 
-   if (has_guest_vram) {
+   if (has_guest_vram && (host_visible || export_alloc)) {
       return vn_device_memory_alloc_guest_vram(dev, mem, alloc_info);
-   } else if (mem_vk->export_handle_types) {
+   } else if (export_alloc) {
       return vn_device_memory_alloc_export(dev, mem, alloc_info);
    } else {
       return vn_device_memory_alloc_simple(dev, mem, alloc_info);

Reply via email to