From 891e5d45a5545d5b71d24682faa142a575a3060c Mon Sep 17 00:00:00 2001
From: David Reveman <reveman@google.com>
Date: Sat, 27 Apr 2019 16:01:18 +0000
Subject: [PATCH] [vulkan] Improve hw support for
 VK_LAYER_FUCHSIA_imagepipe_swapchain

This doesn't cause a change in behavior. The code is only used for
examples and the change simply increase hw support and allows for
optimizations in future drivers.

- Determine memory type index instead of assuming 0.
- This is a memory allocation dedicated to a single image. Inform
  the driver of this. Required by goldfish ICD and useful for
  other drivers.

The result of this change is that examples that use this layer can
be used when running fuchsia on aemu.

DX-939 #comment

Test: fx shell present_view fuchsia-pkg://fuchsia.com/vkcube_on_scenic#meta/vkcube_on_scenic.cmx
Change-Id: Ib27140ea6c5871e34af01d4ea631e2f4b80fa600
---
 .../src/swapchain/image_pipe_surface_async.cc       | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/garnet/lib/vulkan/src/swapchain/image_pipe_surface_async.cc b/garnet/lib/vulkan/src/swapchain/image_pipe_surface_async.cc
index 4a54d916842..aa8654d6883 100644
--- a/garnet/lib/vulkan/src/swapchain/image_pipe_surface_async.cc
+++ b/garnet/lib/vulkan/src/swapchain/image_pipe_surface_async.cc
@@ -47,9 +47,18 @@ bool ImagePipeSurfaceAsync::CreateImage(
     VkMemoryRequirements memory_requirements;
     pDisp->GetImageMemoryRequirements(device, image, &memory_requirements);
 
+    // Find lowest usable index.
+    uint32_t memory_type_index = __builtin_ctz(
+        memory_requirements.memoryTypeBits);
+
+    VkMemoryDedicatedAllocateInfo dedicated_allocate_info = {
+        .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
+        .pNext = nullptr,
+        .image = image,
+        .buffer = VK_NULL_HANDLE};
     VkExportMemoryAllocateInfoKHR export_allocate_info = {
         .sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR,
-        .pNext = nullptr,
+        .pNext = &dedicated_allocate_info,
         .handleTypes =
             VK_EXTERNAL_MEMORY_HANDLE_TYPE_TEMP_ZIRCON_VMO_BIT_FUCHSIA};
 
@@ -57,7 +66,7 @@ bool ImagePipeSurfaceAsync::CreateImage(
         .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
         .pNext = &export_allocate_info,
         .allocationSize = memory_requirements.size,
-        .memoryTypeIndex = 0,
+        .memoryTypeIndex = memory_type_index,
     };
     VkDeviceMemory memory;
     result = pDisp->AllocateMemory(device, &alloc_info, pAllocator, &memory);
-- 
GitLab