SDL: Fixed some textures not being renderer properly

From 0df861d345499498ee18e8fe52afea407eabfd55 Mon Sep 17 00:00:00 2001
From: Francisco Javier Trujillo Mata <[EMAIL REDACTED]>
Date: Sun, 22 Jan 2023 22:15:46 +0100
Subject: [PATCH] Fixed some textures not being renderer properly

---
 src/render/psp/SDL_render_psp.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/src/render/psp/SDL_render_psp.c b/src/render/psp/SDL_render_psp.c
index 89757d959189c..d0e7718a1352b 100644
--- a/src/render/psp/SDL_render_psp.c
+++ b/src/render/psp/SDL_render_psp.c
@@ -119,6 +119,20 @@ static int PixelFormatToPSPFMT(Uint32 format)
     }
 }
 
+static int calculatePitchForTextureFormat(int width, int format)
+{
+    switch (format) {
+    case GU_PSM_5650:
+    case GU_PSM_5551:
+    case GU_PSM_4444:
+        return (width + 7) & ~7;
+    case GU_PSM_8888:
+        return (width + 3) & ~3;
+    default:
+        return width;
+    }
+}
+
 /* Return next power of 2 */
 static int TextureNextPow2(unsigned int w)
 {
@@ -146,11 +160,11 @@ static int PSP_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture)
         return SDL_OutOfMemory();
     }
 
-    psp_tex->width = texture->w;
+    psp_tex->format = PixelFormatToPSPFMT(texture->format);
+    psp_tex->width = calculatePitchForTextureFormat(texture->w, psp_tex->format);
     psp_tex->height = texture->h;
     psp_tex->textureWidth = TextureNextPow2(texture->w);
     psp_tex->textureHeight = TextureNextPow2(texture->h);
-    psp_tex->format = PixelFormatToPSPFMT(texture->format);
     psp_tex->data = SDL_calloc(1, getMemorySize(psp_tex->width, psp_tex->height, psp_tex->format));
 
     if (!psp_tex->data) {
@@ -455,7 +469,7 @@ static int PSP_RenderGeometry(SDL_Renderer *renderer, void *vertices, SDL_Render
         sceGuEnable(GU_TEXTURE_2D);
         sceGuTexMode(psp_tex->format, 0, 0, GU_FALSE);
         sceGuTexFilter(psp_tex->filter, psp_tex->filter);
-        sceGuTexImage(0, psp_tex->textureWidth, psp_tex->textureHeight, psp_tex->textureWidth, psp_tex->data);
+        sceGuTexImage(0, psp_tex->textureWidth, psp_tex->textureHeight, psp_tex->width, psp_tex->data);
         sceGuDrawArray(GU_TRIANGLES, GU_TEXTURE_32BITF | GU_COLOR_8888 | GU_VERTEX_32BITF | GU_TRANSFORM_2D, count, 0, verts);
         sceGuDisable(GU_TEXTURE_2D);
     } else {