SDL: GPU: Fix and generalize BytesPerRow() and BytesPerImage() (#10663)

From d4b80726142d9108f16d4806c09779d612501608 Mon Sep 17 00:00:00 2001
From: Andrei Alexeyev <[EMAIL REDACTED]>
Date: Sun, 1 Sep 2024 09:07:38 +0300
Subject: [PATCH] GPU: Fix and generalize BytesPerRow() and BytesPerImage()
 (#10663)

These functions had special cases for a few BC formats, but all
block-compressed formats should need the same logic. Furthermore they
weren't handling the sRGB variants of those formats.
---
 src/gpu/SDL_sysgpu.h | 21 ++++++---------------
 1 file changed, 6 insertions(+), 15 deletions(-)

diff --git a/src/gpu/SDL_sysgpu.h b/src/gpu/SDL_sysgpu.h
index d8257cd6229bf..79e1ea09a7cae 100644
--- a/src/gpu/SDL_sysgpu.h
+++ b/src/gpu/SDL_sysgpu.h
@@ -173,14 +173,9 @@ static inline Uint32 BytesPerRow(
     SDL_GPUTextureFormat format)
 {
     Uint32 blocksPerRow = width;
+    Uint32 pixelRowsPerBlock = Texture_GetBlockSize(format);
 
-    if (format == SDL_GPU_TEXTUREFORMAT_BC1_UNORM ||
-        format == SDL_GPU_TEXTUREFORMAT_BC2_UNORM ||
-        format == SDL_GPU_TEXTUREFORMAT_BC3_UNORM ||
-        format == SDL_GPU_TEXTUREFORMAT_BC7_UNORM) {
-        blocksPerRow = (width + 3) / 4;
-    }
-
+    blocksPerRow = (width + pixelRowsPerBlock - 1) / pixelRowsPerBlock;
     return blocksPerRow * SDL_GPUTextureFormatTexelBlockSize(format);
 }
 
@@ -191,15 +186,11 @@ static inline Sint32 BytesPerImage(
 {
     Uint32 blocksPerRow = width;
     Uint32 blocksPerColumn = height;
+    Uint32 pixelRowsPerBlock = Texture_GetBlockSize(format);
+    Uint32 pixelColumnsPerBlock = pixelRowsPerBlock;
 
-    if (format == SDL_GPU_TEXTUREFORMAT_BC1_UNORM ||
-        format == SDL_GPU_TEXTUREFORMAT_BC2_UNORM ||
-        format == SDL_GPU_TEXTUREFORMAT_BC3_UNORM ||
-        format == SDL_GPU_TEXTUREFORMAT_BC7_UNORM) {
-        blocksPerRow = (width + 3) / 4;
-        blocksPerColumn = (height + 3) / 4;
-    }
-
+    blocksPerRow = (width + pixelRowsPerBlock - 1) / pixelRowsPerBlock;
+    blocksPerColumn = (height + pixelColumnsPerBlock - 1) / pixelColumnsPerBlock;
     return blocksPerRow * blocksPerColumn * SDL_GPUTextureFormatTexelBlockSize(format);
 }