SDL: GPU Vulkan: add resolve info to framebuffer and render pass lookups

From fb165a542b0c4d171f8c7eea0b55e256080247e7 Mon Sep 17 00:00:00 2001
From: cosmonaut <[EMAIL REDACTED]>
Date: Fri, 27 Sep 2024 15:11:13 -0700
Subject: [PATCH] GPU Vulkan: add resolve info to framebuffer and render pass
 lookups

---
 src/gpu/vulkan/SDL_gpu_vulkan.c | 59 +++++++++++++++++++++++++++++++--
 1 file changed, 56 insertions(+), 3 deletions(-)

diff --git a/src/gpu/vulkan/SDL_gpu_vulkan.c b/src/gpu/vulkan/SDL_gpu_vulkan.c
index fc2e5fa27983d..ab80372196be0 100644
--- a/src/gpu/vulkan/SDL_gpu_vulkan.c
+++ b/src/gpu/vulkan/SDL_gpu_vulkan.c
@@ -893,6 +893,8 @@ typedef struct RenderPassHashTableKey
 {
     RenderPassColorTargetDescription colorTargetDescriptions[MAX_COLOR_TARGET_BINDINGS];
     Uint32 numColorTargets;
+    VkFormat resolveTargetFormats[MAX_COLOR_TARGET_BINDINGS];
+    Uint32 numResolveTargets;
     RenderPassDepthStencilTargetDescription depthStencilTargetDescription;
     VkSampleCountFlagBits sampleCount;
 } RenderPassHashTableKey;
@@ -906,6 +908,8 @@ typedef struct FramebufferHashTableKey
 {
     VkImageView colorAttachmentViews[MAX_COLOR_TARGET_BINDINGS];
     Uint32 numColorTargets;
+    VkImageView resolveAttachmentViews[MAX_COLOR_TARGET_BINDINGS];
+    Uint32 numResolveAttachments;
     VkImageView depthStencilAttachmentView;
     Uint32 width;
     Uint32 height;
@@ -2879,6 +2883,11 @@ static void VULKAN_INTERNAL_RemoveFramebuffersContainingView(
                 remove = true;
             }
         }
+        for (Uint32 i = 0; i < key->numResolveAttachments; i += 1) {
+            if (key->resolveAttachmentViews[i] == view) {
+                remove = true;
+            }
+        }
         if (key->depthStencilAttachmentView == view) {
             remove = true;
         }
@@ -3334,6 +3343,10 @@ static Uint32 VULKAN_INTERNAL_RenderPassHashFunction(
         result = result * hashFactor + hashTableKey->colorTargetDescriptions[i].format;
     }
 
+    for (Uint32 i = 0; i < hashTableKey->numResolveTargets; i += 1) {
+        result = result * hashFactor + hashTableKey->resolveTargetFormats[i];
+    }
+
     result = result * hashFactor + hashTableKey->depthStencilTargetDescription.loadOp;
     result = result * hashFactor + hashTableKey->depthStencilTargetDescription.storeOp;
     result = result * hashFactor + hashTableKey->depthStencilTargetDescription.stencilLoadOp;
@@ -3357,6 +3370,10 @@ static bool VULKAN_INTERNAL_RenderPassHashKeyMatch(
         return 0;
     }
 
+    if (a->numResolveTargets != b->numResolveTargets) {
+        return 0;
+    }
+
     if (a->sampleCount != b->sampleCount) {
         return 0;
     }
@@ -3375,6 +3392,12 @@ static bool VULKAN_INTERNAL_RenderPassHashKeyMatch(
         }
     }
 
+    for (Uint32 i = 0; i < a->numResolveTargets; i += 1) {
+        if (a->resolveTargetFormats[i] != b->resolveTargetFormats[i]) {
+            return 0;
+        }
+    }
+
     if (a->depthStencilTargetDescription.format != b->depthStencilTargetDescription.format) {
         return 0;
     }
@@ -3426,6 +3449,9 @@ static Uint32 VULKAN_INTERNAL_FramebufferHashFunction(
     for (Uint32 i = 0; i < hashTableKey->numColorTargets; i += 1) {
         result = result * hashFactor + (Uint32)(uintptr_t)hashTableKey->colorAttachmentViews[i];
     }
+    for (Uint32 i = 0; i < hashTableKey->numResolveAttachments; i += 1) {
+        result = result * hashFactor + (Uint32)(uintptr_t)hashTableKey->resolveAttachmentViews[i];
+    }
 
     result = result * hashFactor + (Uint32)(uintptr_t)hashTableKey->depthStencilAttachmentView;
     result = result * hashFactor + hashTableKey->width;
@@ -3446,12 +3472,22 @@ static bool VULKAN_INTERNAL_FramebufferHashKeyMatch(
         return 0;
     }
 
+    if (a->numResolveAttachments != b->numResolveAttachments) {
+        return 0;
+    }
+
     for (Uint32 i = 0; i < a->numColorTargets; i += 1) {
         if (a->colorAttachmentViews[i] != b->colorAttachmentViews[i]) {
             return 0;
         }
     }
 
+    for (Uint32 i = 0; i < a->numResolveAttachments; i += 1) {
+        if (a->resolveAttachmentViews[i] != b->resolveAttachmentViews[i]) {
+            return 0;
+        }
+    }
+
     if (a->depthStencilAttachmentView != b->depthStencilAttachmentView) {
         return 0;
     }
@@ -6835,10 +6871,17 @@ static VkRenderPass VULKAN_INTERNAL_FetchRenderPass(
     RenderPassHashTableKey key;
     Uint32 i;
 
+    SDL_zero(key);
+
     for (i = 0; i < numColorTargets; i += 1) {
         key.colorTargetDescriptions[i].format = SDLToVK_TextureFormat[((VulkanTextureContainer *)colorTargetInfos[i].texture)->header.info.format];
         key.colorTargetDescriptions[i].loadOp = colorTargetInfos[i].load_op;
         key.colorTargetDescriptions[i].storeOp = colorTargetInfos[i].store_op;
+
+        if (colorTargetInfos[i].resolve_texture != NULL) {
+            key.resolveTargetFormats[key.numResolveTargets] = SDLToVK_TextureFormat[((VulkanTextureContainer *)colorTargetInfos[i].resolve_texture)->header.info.format];
+            key.numResolveTargets += 1;
+        }
     }
 
     key.sampleCount = VK_SAMPLE_COUNT_1_BIT;
@@ -6921,9 +6964,8 @@ static VulkanFramebuffer *VULKAN_INTERNAL_FetchFramebuffer(
     Uint32 attachmentCount = 0;
     Uint32 i;
 
-    for (i = 0; i < MAX_COLOR_TARGET_BINDINGS; i += 1) {
-        key.colorAttachmentViews[i] = VK_NULL_HANDLE;
-    }
+    SDL_zero(imageViewAttachments);
+    SDL_zero(key);
 
     key.numColorTargets = numColorTargets;
 
@@ -6937,6 +6979,17 @@ static VulkanFramebuffer *VULKAN_INTERNAL_FetchFramebuffer(
         Uint32 rtvIndex =
             container->header.info.type == SDL_GPU_TEXTURETYPE_3D ? colorTargetInfos[i].layer_or_depth_plane : 0;
         key.colorAttachmentViews[i] = subresource->renderTargetViews[rtvIndex];
+
+        if (colorTargetInfos[i].resolve_texture != NULL) {
+            VulkanTextureContainer *resolveTextureContainer = (VulkanTextureContainer *)colorTargetInfos[i].resolve_texture;
+            VulkanTextureSubresource *resolveSubresource = VULKAN_INTERNAL_FetchTextureSubresource(
+                resolveTextureContainer,
+                colorTargetInfos[i].layer_or_depth_plane,
+                colorTargetInfos[i].mip_level);
+
+            key.resolveAttachmentViews[key.numResolveAttachments] = resolveSubresource->renderTargetViews[0];
+            key.numResolveAttachments += 1;
+        }
     }
 
     if (depthStencilTargetInfo == NULL) {