icd: Make swapchain images persistent
authorPetr Kraus <petr_kraus@email.cz>
Wed, 8 Apr 2020 19:47:25 +0000 (21:47 +0200)
committerMark Lobodzinski <mark@lunarg.com>
Fri, 10 Apr 2020 19:02:51 +0000 (13:02 -0600)
icd/generated/mock_icd.cpp
scripts/mock_icd_generator.py

index 7a34a14..1f3167c 100644 (file)
@@ -39,6 +39,9 @@ static VkPhysicalDevice physical_device = (VkPhysicalDevice)CreateDispObjHandle(
 static unordered_map<VkDevice, unordered_map<uint32_t, unordered_map<uint32_t, VkQueue>>> queue_map;
 static unordered_map<VkDevice, unordered_map<VkBuffer, VkBufferCreateInfo>> buffer_map;
 
+static constexpr uint32_t icd_swapchain_image_count = 1;
+static std::unordered_map<VkSwapchainKHR, VkImage[icd_swapchain_image_count]> swapchain_image_map;
+
 // TODO: Would like to codegen this but limits aren't in XML
 static VkPhysicalDeviceLimits SetLimits(VkPhysicalDeviceLimits *limits) {
     limits->maxImageDimension1D = 4096;
@@ -849,7 +852,7 @@ static VKAPI_ATTR void VKAPI_CALL GetImageSubresourceLayout(
     const VkImageSubresource*                   pSubresource,
     VkSubresourceLayout*                        pLayout)
 {
-    // Need safe values. Callers are computing memory offsets from pLayout, with no return code to flag failure. 
+    // Need safe values. Callers are computing memory offsets from pLayout, with no return code to flag failure.
     *pLayout = VkSubresourceLayout(); // Default constructor zero values.
 }
 
@@ -2125,6 +2128,9 @@ static VKAPI_ATTR VkResult VKAPI_CALL CreateSwapchainKHR(
 {
     unique_lock_t lock(global_lock);
     *pSwapchain = (VkSwapchainKHR)global_unique_handle++;
+    for(uint32_t i = 0; i < icd_swapchain_image_count; ++i){
+        swapchain_image_map[*pSwapchain][i] = (VkImage)global_unique_handle++;
+    }
     return VK_SUCCESS;
 }
 
@@ -2133,7 +2139,8 @@ static VKAPI_ATTR void VKAPI_CALL DestroySwapchainKHR(
     VkSwapchainKHR                              swapchain,
     const VkAllocationCallbacks*                pAllocator)
 {
-//Destroy object
+    unique_lock_t lock(global_lock);
+    swapchain_image_map.clear();
 }
 
 static VKAPI_ATTR VkResult VKAPI_CALL GetSwapchainImagesKHR(
@@ -2142,19 +2149,16 @@ static VKAPI_ATTR VkResult VKAPI_CALL GetSwapchainImagesKHR(
     uint32_t*                                   pSwapchainImageCount,
     VkImage*                                    pSwapchainImages)
 {
-    constexpr uint32_t icd_image_count = 1;
-
     if (!pSwapchainImages) {
-        *pSwapchainImageCount = icd_image_count;
+        *pSwapchainImageCount = icd_swapchain_image_count;
     } else {
         unique_lock_t lock(global_lock);
-        for (uint32_t img_i = 0; img_i < (std::min)(*pSwapchainImageCount, icd_image_count); ++img_i){
-            // For simplicity always returns new handles, which is wrong
-            pSwapchainImages[img_i] = (VkImage)global_unique_handle++;
+        for (uint32_t img_i = 0; img_i < (std::min)(*pSwapchainImageCount, icd_swapchain_image_count); ++img_i){
+            pSwapchainImages[img_i] = swapchain_image_map.at(swapchain)[img_i];
         }
 
-        if (*pSwapchainImageCount < icd_image_count) return VK_INCOMPLETE;
-        else if (*pSwapchainImageCount > icd_image_count) *pSwapchainImageCount = icd_image_count;
+        if (*pSwapchainImageCount < icd_swapchain_image_count) return VK_INCOMPLETE;
+        else if (*pSwapchainImageCount > icd_swapchain_image_count) *pSwapchainImageCount = icd_swapchain_image_count;
     }
     return VK_SUCCESS;
 }
index 2f33d43..0b1062c 100644 (file)
@@ -64,6 +64,9 @@ static VkPhysicalDevice physical_device = (VkPhysicalDevice)CreateDispObjHandle(
 static unordered_map<VkDevice, unordered_map<uint32_t, unordered_map<uint32_t, VkQueue>>> queue_map;
 static unordered_map<VkDevice, unordered_map<VkBuffer, VkBufferCreateInfo>> buffer_map;
 
+static constexpr uint32_t icd_swapchain_image_count = 1;
+static std::unordered_map<VkSwapchainKHR, VkImage[icd_swapchain_image_count]> swapchain_image_map;
+
 // TODO: Would like to codegen this but limits aren't in XML
 static VkPhysicalDeviceLimits SetLimits(VkPhysicalDeviceLimits *limits) {
     limits->maxImageDimension1D = 4096;
@@ -876,23 +879,32 @@ CUSTOM_C_INTERCEPTS = {
     mapped_memory_map.erase(memory);
 ''',
 'vkGetImageSubresourceLayout': '''
-    // Need safe values. Callers are computing memory offsets from pLayout, with no return code to flag failure. 
+    // Need safe values. Callers are computing memory offsets from pLayout, with no return code to flag failure.
     *pLayout = VkSubresourceLayout(); // Default constructor zero values.
 ''',
+'vkCreateSwapchainKHR': '''
+    unique_lock_t lock(global_lock);
+    *pSwapchain = (VkSwapchainKHR)global_unique_handle++;
+    for(uint32_t i = 0; i < icd_swapchain_image_count; ++i){
+        swapchain_image_map[*pSwapchain][i] = (VkImage)global_unique_handle++;
+    }
+    return VK_SUCCESS;
+''',
+'vkDestroySwapchainKHR': '''
+    unique_lock_t lock(global_lock);
+    swapchain_image_map.clear();
+''',
 'vkGetSwapchainImagesKHR': '''
-    constexpr uint32_t icd_image_count = 1;
-
     if (!pSwapchainImages) {
-        *pSwapchainImageCount = icd_image_count;
+        *pSwapchainImageCount = icd_swapchain_image_count;
     } else {
         unique_lock_t lock(global_lock);
-        for (uint32_t img_i = 0; img_i < (std::min)(*pSwapchainImageCount, icd_image_count); ++img_i){
-            // For simplicity always returns new handles, which is wrong
-            pSwapchainImages[img_i] = (VkImage)global_unique_handle++;
+        for (uint32_t img_i = 0; img_i < (std::min)(*pSwapchainImageCount, icd_swapchain_image_count); ++img_i){
+            pSwapchainImages[img_i] = swapchain_image_map.at(swapchain)[img_i];
         }
 
-        if (*pSwapchainImageCount < icd_image_count) return VK_INCOMPLETE;
-        else if (*pSwapchainImageCount > icd_image_count) *pSwapchainImageCount = icd_image_count;
+        if (*pSwapchainImageCount < icd_swapchain_image_count) return VK_INCOMPLETE;
+        else if (*pSwapchainImageCount > icd_swapchain_image_count) *pSwapchainImageCount = icd_swapchain_image_count;
     }
     return VK_SUCCESS;
 ''',