SDL: Fixed pitch alignment when updating SDL_PIXELFORMAT_P010 textures (2bedd)

From 2bedd7f02ec8ee98044db4f14241d7e13a47042b Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 1 Mar 2024 16:22:10 -0800
Subject: [PATCH] Fixed pitch alignment when updating SDL_PIXELFORMAT_P010
 textures

---
 src/render/direct3d11/SDL_render_d3d11.c |  9 +++++++--
 src/render/direct3d12/SDL_render_d3d12.c |  7 ++++++-
 src/render/vulkan/SDL_render_vulkan.c    | 12 +++++++++---
 3 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/src/render/direct3d11/SDL_render_d3d11.c b/src/render/direct3d11/SDL_render_d3d11.c
index 2d6841fae8e10..cef5c816a4371 100644
--- a/src/render/direct3d11/SDL_render_d3d11.c
+++ b/src/render/direct3d11/SDL_render_d3d11.c
@@ -1549,8 +1549,13 @@ static int D3D11_UpdateTextureInternal(D3D11_RenderData *rendererData, ID3D11Tex
         stagingTextureDesc.Format == DXGI_FORMAT_P010) {
         /* Copy the UV plane as well */
         h = (h + 1) / 2;
-        length = (length + 1) & ~1;
-        pitch = (pitch + 1) & ~1;
+        if (stagingTextureDesc.Format == DXGI_FORMAT_P010) {
+            length = (length + 3) & ~3;
+            pitch = (pitch + 3) & ~3;
+        } else {
+            length = (length + 1) & ~1;
+            pitch = (pitch + 1) & ~1;
+        }
         dst = (Uint8 *)textureMemory.pData + stagingTextureDesc.Height * textureMemory.RowPitch;
         for (row = 0; row < h; ++row) {
             SDL_memcpy(dst, src, length);
diff --git a/src/render/direct3d12/SDL_render_d3d12.c b/src/render/direct3d12/SDL_render_d3d12.c
index 470293a1bf7a9..e6b84bf9a5e43 100644
--- a/src/render/direct3d12/SDL_render_d3d12.c
+++ b/src/render/direct3d12/SDL_render_d3d12.c
@@ -1975,7 +1975,12 @@ static int D3D12_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture,
         /* Skip to the correct offset into the next texture */
         srcPixels = (const void *)((const Uint8 *)srcPixels + rect->h * srcPitch);
 
-        if (D3D12_UpdateTextureInternal(rendererData, textureData->mainTexture, 1, rect->x, rect->y, (rect->w + 1) & ~1, (rect->h + 1) & ~1, srcPixels, (srcPitch + 1) & ~1, &textureData->mainResourceState) < 0) {
+        if (texture->format == SDL_PIXELFORMAT_P010) {
+            srcPitch = (srcPitch + 3) & ~3;
+        } else {
+            srcPitch = (srcPitch + 1) & ~1;
+        }
+        if (D3D12_UpdateTextureInternal(rendererData, textureData->mainTexture, 1, rect->x, rect->y, (rect->w + 1) & ~1, (rect->h + 1) & ~1, srcPixels, srcPitch, &textureData->mainResourceState) < 0) {
             return -1;
         }
     }
diff --git a/src/render/vulkan/SDL_render_vulkan.c b/src/render/vulkan/SDL_render_vulkan.c
index f38f3f3a65658..b1f55d0741065 100644
--- a/src/render/vulkan/SDL_render_vulkan.c
+++ b/src/render/vulkan/SDL_render_vulkan.c
@@ -187,7 +187,7 @@ typedef struct
     float scRGB_output;
     float input_type;
     float color_scale;
-	float unused_pad0;
+    float unused_pad0;
 
     float tonemap_method;
     float tonemap_factor1;
@@ -2779,8 +2779,14 @@ static int VULKAN_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture,
     // NV12/NV21 data
     else if (numPlanes == 2)
     {
-        if (VULKAN_UpdateTextureInternal(rendererData, textureData->mainImage.image, textureData->mainImage.format, 1, rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, srcPixels, (srcPitch + 1) & ~1, &textureData->mainImage.imageLayout) < 0) {
-                return -1;
+        if (texture->format == SDL_PIXELFORMAT_P010) {
+            srcPitch = (srcPitch + 3) & ~3;
+        } else {
+            srcPitch = (srcPitch + 1) & ~1;
+        }
+
+        if (VULKAN_UpdateTextureInternal(rendererData, textureData->mainImage.image, textureData->mainImage.format, 1, rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, srcPixels, srcPitch, &textureData->mainImage.imageLayout) < 0) {
+            return -1;
         }
     }
 #endif