SDL: GPU: Enable Metal validation in debug mode (#11691)

From 62124e2c283dc6a5d411df0612e197155f5b299a Mon Sep 17 00:00:00 2001
From: Caleb Cornett <[EMAIL REDACTED]>
Date: Mon, 23 Dec 2024 18:08:14 -0500
Subject: [PATCH] GPU: Enable Metal validation in debug mode (#11691)

---
 src/gpu/metal/SDL_gpu_metal.m | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/gpu/metal/SDL_gpu_metal.m b/src/gpu/metal/SDL_gpu_metal.m
index be64b366ff14c..028e18a438633 100644
--- a/src/gpu/metal/SDL_gpu_metal.m
+++ b/src/gpu/metal/SDL_gpu_metal.m
@@ -4343,6 +4343,15 @@ static void METAL_INTERNAL_DestroyBlitResources(
         MetalRenderer *renderer;
         id<MTLDevice> device = NULL;
 
+        if (debugMode) {
+            /* Due to a Metal driver quirk, once a MTLDevice has been created
+             * with this environment variable set, the Metal validation layers
+             * will remain enabled for the rest of the application's lifespan,
+             * even if the device is destroyed and recreated.
+             */
+            SDL_setenv_unsafe("MTL_DEBUG_LAYER", "1", 0);
+        }
+
         // Create the Metal device and command queue
 #ifdef SDL_PLATFORM_MACOS
         if (preferLowPower) {