SDL: GPU: Metal: Rebind storage buffers when changing the pipeline moves them. (e8fbb)

From e8fbb7e8200508a8002ecc72dbd97f39355a742b Mon Sep 17 00:00:00 2001
From: Jakub Wasilewski <[EMAIL REDACTED]>
Date: Sun, 14 Dec 2025 11:16:02 +0100
Subject: [PATCH] GPU: Metal: Rebind storage buffers when changing the pipeline
 moves them.

(cherry picked from commit 73c9f25867e60c8a7c18ff9b427c1c5e7ffdc8be)
---
 src/gpu/metal/SDL_gpu_metal.m | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/src/gpu/metal/SDL_gpu_metal.m b/src/gpu/metal/SDL_gpu_metal.m
index c93b8a0e02d57..2071919cf6f9d 100644
--- a/src/gpu/metal/SDL_gpu_metal.m
+++ b/src/gpu/metal/SDL_gpu_metal.m
@@ -2373,6 +2373,7 @@ static void METAL_BindGraphicsPipeline(
 {
     @autoreleasepool {
         MetalCommandBuffer *metalCommandBuffer = (MetalCommandBuffer *)commandBuffer;
+        MetalGraphicsPipeline *previousPipeline = metalCommandBuffer->graphics_pipeline;
         MetalGraphicsPipeline *pipeline = (MetalGraphicsPipeline *)graphicsPipeline;
         SDL_GPURasterizerState *rast = &pipeline->rasterizerState;
         Uint32 i;
@@ -2415,6 +2416,17 @@ static void METAL_BindGraphicsPipeline(
                     metalCommandBuffer);
             }
         }
+
+        if (previousPipeline && previousPipeline != pipeline) {
+            // if the number of uniform buffers has changed, the storage buffers will move as well
+            // and need a rebind at their new locations
+            if (previousPipeline->header.num_vertex_uniform_buffers != pipeline->header.num_vertex_uniform_buffers) {
+                metalCommandBuffer->needVertexStorageBufferBind = true;
+            }
+            if (previousPipeline->header.num_fragment_uniform_buffers != pipeline->header.num_fragment_uniform_buffers) {
+                metalCommandBuffer->needFragmentStorageBufferBind = true;
+            }
+        }
     }
 }