SDL: GPU: Debug mode layer and level index checks (2001c)

From 2001cc15d5899e0d20df9e52df9a3021cabce41b Mon Sep 17 00:00:00 2001
From: cosmonaut <[EMAIL REDACTED]>
Date: Wed, 14 May 2025 12:21:29 -0700
Subject: [PATCH] GPU: Debug mode layer and level index checks

---
 src/gpu/SDL_gpu.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/src/gpu/SDL_gpu.c b/src/gpu/SDL_gpu.c
index 34c99f5f9697e..7c4a0ede4b5c9 100644
--- a/src/gpu/SDL_gpu.c
+++ b/src/gpu/SDL_gpu.c
@@ -1525,6 +1525,14 @@ SDL_GPURenderPass *SDL_BeginGPURenderPass(
                     }
                 }
             }
+
+            if (color_target_infos[i].layer_or_depth_plane >= textureHeader->info.layer_count_or_depth) {
+                SDL_assert_release(!"Color target layer index must be less than the texture's layer count!");
+            }
+
+            if (color_target_infos[i].mip_level >= textureHeader->info.num_levels) {
+                SDL_assert_release(!"Color target mip level must be less than the texture's level count!");
+            }
         }
 
         if (depth_stencil_target_info != NULL) {
@@ -2061,6 +2069,14 @@ SDL_GPUComputePass *SDL_BeginGPUComputePass(
                 SDL_assert_release(!"Texture must be created with COMPUTE_STORAGE_WRITE or COMPUTE_STORAGE_SIMULTANEOUS_READ_WRITE flag");
                 return NULL;
             }
+
+            if (storage_texture_bindings[i].layer >= header->info.layer_count_or_depth) {
+                SDL_assert_release(!"Storage texture layer index must be less than the texture's layer count!");
+            }
+
+            if (storage_texture_bindings[i].mip_level >= header->info.num_levels) {
+                SDL_assert_release(!"Storage texture mip level must be less than the texture's level count!");
+            }
         }
 
         // TODO: validate buffer usage?