From ab67be7e5a2e32315227ec05b49aa4ac362ca6c0 Mon Sep 17 00:00:00 2001
From: LuncyBloont <[EMAIL REDACTED]>
Date: Sat, 6 Dec 2025 00:15:29 +0800
Subject: [PATCH] Fix crash in UploadToTexture() on DX12.
---
src/gpu/d3d12/SDL_gpu_d3d12.c | 25 ++++++++++++++++---------
1 file changed, 16 insertions(+), 9 deletions(-)
diff --git a/src/gpu/d3d12/SDL_gpu_d3d12.c b/src/gpu/d3d12/SDL_gpu_d3d12.c
index a67b690d78e6a..1deff7d7b4292 100644
--- a/src/gpu/d3d12/SDL_gpu_d3d12.c
+++ b/src/gpu/d3d12/SDL_gpu_d3d12.c
@@ -5925,6 +5925,7 @@ static void D3D12_UploadToTexture(
Uint32 alignedRowPitch;
Uint32 rowsPerSlice = source->rows_per_layer;
Uint32 bytesPerSlice;
+ Uint32 alignedBytesPerSlice;
bool needsRealignment;
bool needsPlacementCopy;
@@ -5962,10 +5963,13 @@ static void D3D12_UploadToTexture(
bytesPerSlice = rowsPerSlice * rowPitch;
- alignedRowPitch = D3D12_INTERNAL_Align(rowPitch, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
+ alignedRowPitch = BytesPerRow(destination->w, textureContainer->header.info.format);
+ alignedRowPitch = D3D12_INTERNAL_Align(alignedRowPitch, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
needsRealignment = rowsPerSlice != destination->h || rowPitch != alignedRowPitch;
needsPlacementCopy = source->offset % D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT != 0;
+ alignedBytesPerSlice = alignedRowPitch * destination->h;
+
sourceLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
sourceLocation.PlacedFootprint.Footprint.Format = SDLToD3D12_TextureFormat[textureContainer->header.info.format];
sourceLocation.PlacedFootprint.Footprint.RowPitch = alignedRowPitch;
@@ -5990,22 +5994,25 @@ static void D3D12_UploadToTexture(
for (Uint32 sliceIndex = 0; sliceIndex < destination->d; sliceIndex += 1) {
// copy row count minus one to avoid overread
- for (Uint32 rowIndex = 0; rowIndex < rowsPerSlice - 1; rowIndex += 1) {
+ for (Uint32 rowIndex = 0; rowIndex < destination->h - 1; rowIndex += 1) {
+
SDL_memcpy(
- temporaryBuffer->mapPointer + (sliceIndex * rowsPerSlice) + (rowIndex * alignedRowPitch),
+ temporaryBuffer->mapPointer + (sliceIndex * alignedBytesPerSlice) + (rowIndex * alignedRowPitch),
transferBufferContainer->activeBuffer->mapPointer + source->offset + (sliceIndex * bytesPerSlice) + (rowIndex * rowPitch),
- rowPitch);
+ alignedRowPitch);
+
}
- Uint32 offset = source->offset + (sliceIndex * bytesPerSlice) + ((rowsPerSlice - 1) * rowPitch);
+ Uint32 offset = source->offset + (sliceIndex * bytesPerSlice) + ((destination->h - 1) * rowPitch);
+
SDL_memcpy(
- temporaryBuffer->mapPointer + (sliceIndex * rowsPerSlice) + ((rowsPerSlice - 1) * alignedRowPitch),
+ temporaryBuffer->mapPointer + (sliceIndex * alignedBytesPerSlice) + ((destination->h - 1) * alignedRowPitch),
transferBufferContainer->activeBuffer->mapPointer + offset,
SDL_min(alignedRowPitch, transferBufferContainer->size - offset));
sourceLocation.PlacedFootprint.Footprint.Width = destination->w;
- sourceLocation.PlacedFootprint.Footprint.Height = rowsPerSlice;
+ sourceLocation.PlacedFootprint.Footprint.Height = destination->h;
sourceLocation.PlacedFootprint.Footprint.Depth = 1;
- sourceLocation.PlacedFootprint.Offset = (sliceIndex * bytesPerSlice);
+ sourceLocation.PlacedFootprint.Offset = (sliceIndex * alignedBytesPerSlice);
ID3D12GraphicsCommandList_CopyTextureRegion(
d3d12CommandBuffer->graphicsCommandList,
@@ -6046,7 +6053,7 @@ static void D3D12_UploadToTexture(
sourceLocation.PlacedFootprint.Offset = 0;
sourceLocation.PlacedFootprint.Footprint.Width = destination->w;
sourceLocation.PlacedFootprint.Footprint.Height = destination->h;
- sourceLocation.PlacedFootprint.Footprint.Depth = 1;
+ sourceLocation.PlacedFootprint.Footprint.Depth = destination->d;
ID3D12GraphicsCommandList_CopyTextureRegion(
d3d12CommandBuffer->graphicsCommandList,