Fixed issues with YUV texture updates in the 2D renderer

This commit is contained in:
Sam Lantinga
2025-12-04 07:59:19 -08:00
parent 8be18741e1
commit 09304831f6
4 changed files with 64 additions and 43 deletions

View File

@@ -1585,22 +1585,25 @@ static bool D3D11_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture,
#ifdef SDL_HAVE_YUV #ifdef SDL_HAVE_YUV
if (textureData->nv12) { if (textureData->nv12) {
const Uint8 *Yplane = (const Uint8 *)srcPixels; int UVbpp = SDL_BYTESPERPIXEL(texture->format) * 2;
const Uint8 *UVplane = Yplane + rect->h * srcPitch; int Ypitch = srcPitch;
int UVpitch = (srcPitch + (UVbpp - 1)) & ~(UVbpp - 1);
const Uint8 *plane0 = (const Uint8 *)srcPixels;
const Uint8 *plane1 = plane0 + rect->h * srcPitch;
return D3D11_UpdateTextureNV(renderer, texture, rect, Yplane, srcPitch, UVplane, srcPitch); return D3D11_UpdateTextureNV(renderer, texture, rect, plane0, Ypitch, plane1, UVpitch);
} else if (textureData->yuv) { } else if (textureData->yuv) {
int Ypitch = srcPitch; int Ypitch = srcPitch;
int UVpitch = ((Ypitch + 1) / 2); int UVpitch = ((Ypitch + 1) / 2);
const Uint8 *Yplane = (const Uint8 *)srcPixels; const Uint8 *plane0 = (const Uint8 *)srcPixels;
const Uint8 *Uplane = Yplane + rect->h * Ypitch; const Uint8 *plane1 = plane0 + rect->h * Ypitch;
const Uint8 *Vplane = Uplane + ((rect->h + 1) / 2) * UVpitch; const Uint8 *plane2 = plane1 + ((rect->h + 1) / 2) * UVpitch;
if (texture->format == SDL_PIXELFORMAT_YV12) { if (texture->format == SDL_PIXELFORMAT_YV12) {
return D3D11_UpdateTextureYUV(renderer, texture, rect, Yplane, Ypitch, Vplane, UVpitch, Uplane, UVpitch); return D3D11_UpdateTextureYUV(renderer, texture, rect, plane0, Ypitch, plane2, UVpitch, plane1, UVpitch);
} else { } else {
return D3D11_UpdateTextureYUV(renderer, texture, rect, Yplane, Ypitch, Uplane, UVpitch, Vplane, UVpitch); return D3D11_UpdateTextureYUV(renderer, texture, rect, plane0, Ypitch, plane1, UVpitch, plane2, UVpitch);
} }
} }
#endif #endif
@@ -1652,6 +1655,7 @@ static bool D3D11_UpdateTextureNV(SDL_Renderer *renderer, SDL_Texture *texture,
HRESULT result; HRESULT result;
D3D11_TEXTURE2D_DESC stagingTextureDesc; D3D11_TEXTURE2D_DESC stagingTextureDesc;
D3D11_MAPPED_SUBRESOURCE textureMemory; D3D11_MAPPED_SUBRESOURCE textureMemory;
int bpp = SDL_BYTESPERPIXEL(texture->format);
if (!textureData) { if (!textureData) {
return SDL_SetError("Texture is not currently available"); return SDL_SetError("Texture is not currently available");
@@ -1695,7 +1699,7 @@ static bool D3D11_UpdateTextureNV(SDL_Renderer *renderer, SDL_Texture *texture,
src = Yplane; src = Yplane;
dst = (Uint8 *)textureMemory.pData; dst = (Uint8 *)textureMemory.pData;
length = w; length = w * bpp;
if (length == (UINT)Ypitch && length == textureMemory.RowPitch) { if (length == (UINT)Ypitch && length == textureMemory.RowPitch) {
SDL_memcpy(dst, src, (size_t)length * h); SDL_memcpy(dst, src, (size_t)length * h);
} else { } else {
@@ -1713,7 +1717,7 @@ static bool D3D11_UpdateTextureNV(SDL_Renderer *renderer, SDL_Texture *texture,
} }
src = UVplane; src = UVplane;
length = w; length = ((w + 1) / 2) * 2 * bpp;
h = (h + 1) / 2; h = (h + 1) / 2;
if (stagingTextureDesc.Format == DXGI_FORMAT_P010) { if (stagingTextureDesc.Format == DXGI_FORMAT_P010) {
length = (length + 3) & ~3; length = (length + 3) & ~3;

View File

@@ -2087,10 +2087,10 @@ static bool D3D12_UpdateTextureYUV(SDL_Renderer *renderer, SDL_Texture *texture,
if (!D3D12_UpdateTextureInternal(rendererData, textureData->mainTexture, 0, rect->x, rect->y, rect->w, rect->h, Yplane, Ypitch, &textureData->mainResourceState)) { if (!D3D12_UpdateTextureInternal(rendererData, textureData->mainTexture, 0, rect->x, rect->y, rect->w, rect->h, Yplane, Ypitch, &textureData->mainResourceState)) {
return false; return false;
} }
if (!D3D12_UpdateTextureInternal(rendererData, textureData->mainTextureU, 0, rect->x / 2, rect->y / 2, rect->w / 2, rect->h / 2, Uplane, Upitch, &textureData->mainResourceStateU)) { if (!D3D12_UpdateTextureInternal(rendererData, textureData->mainTextureU, 0, rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, Uplane, Upitch, &textureData->mainResourceStateU)) {
return false; return false;
} }
if (!D3D12_UpdateTextureInternal(rendererData, textureData->mainTextureV, 0, rect->x / 2, rect->y / 2, rect->w / 2, rect->h / 2, Vplane, Vpitch, &textureData->mainResourceStateV)) { if (!D3D12_UpdateTextureInternal(rendererData, textureData->mainTextureV, 0, rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, Vplane, Vpitch, &textureData->mainResourceStateV)) {
return false; return false;
} }
if (textureData->mainTextureResourceView.ptr == rendererData->currentShaderResource.ptr) { if (textureData->mainTextureResourceView.ptr == rendererData->currentShaderResource.ptr) {
@@ -2115,7 +2115,7 @@ static bool D3D12_UpdateTextureNV(SDL_Renderer *renderer, SDL_Texture *texture,
if (!D3D12_UpdateTextureInternal(rendererData, textureData->mainTexture, 0, rect->x, rect->y, rect->w, rect->h, Yplane, Ypitch, &textureData->mainResourceState)) { if (!D3D12_UpdateTextureInternal(rendererData, textureData->mainTexture, 0, rect->x, rect->y, rect->w, rect->h, Yplane, Ypitch, &textureData->mainResourceState)) {
return false; return false;
} }
if (!D3D12_UpdateTextureInternal(rendererData, textureData->mainTexture, 1, rect->x, rect->y, rect->w, rect->h, UVplane, UVpitch, &textureData->mainResourceState)) { if (!D3D12_UpdateTextureInternal(rendererData, textureData->mainTexture, 1, rect->x, rect->y, (rect->w + 1) & ~1, (rect->h + 1) & ~1, UVplane, UVpitch, &textureData->mainResourceState)) {
return false; return false;
} }
if (textureData->mainTextureResourceView.ptr == rendererData->currentShaderResource.ptr) { if (textureData->mainTextureResourceView.ptr == rendererData->currentShaderResource.ptr) {

View File

@@ -522,7 +522,6 @@ static bool GPU_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, cons
if (texture->format == SDL_PIXELFORMAT_P010) { if (texture->format == SDL_PIXELFORMAT_P010) {
UVpitch = (pitch + 3) & ~3; UVpitch = (pitch + 3) & ~3;
} else { } else {
bpp = 1;
UVpitch = (pitch + 1) & ~1; UVpitch = (pitch + 1) & ~1;
} }
retval &= GPU_UpdateTextureInternal(renderdata, cpass, data->textureNV, bpp, rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, UVplane, UVpitch); retval &= GPU_UpdateTextureInternal(renderdata, cpass, data->textureNV, bpp, rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, UVplane, UVpitch);
@@ -534,8 +533,13 @@ static bool GPU_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, cons
const Uint8 *Uplane = Yplane + rect->h * Ypitch; const Uint8 *Uplane = Yplane + rect->h * Ypitch;
const Uint8 *Vplane = Uplane + ((rect->h + 1) / 2) * UVpitch; const Uint8 *Vplane = Uplane + ((rect->h + 1) / 2) * UVpitch;
retval &= GPU_UpdateTextureInternal(renderdata, cpass, data->textureU, bpp, rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, Uplane, UVpitch); if (texture->format == SDL_PIXELFORMAT_YV12) {
retval &= GPU_UpdateTextureInternal(renderdata, cpass, data->textureV, bpp, rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, Vplane, UVpitch); retval &= GPU_UpdateTextureInternal(renderdata, cpass, data->textureV, bpp, rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, Uplane, UVpitch);
retval &= GPU_UpdateTextureInternal(renderdata, cpass, data->textureU, bpp, rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, Vplane, UVpitch);
} else {
retval &= GPU_UpdateTextureInternal(renderdata, cpass, data->textureU, bpp, rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, Uplane, UVpitch);
retval &= GPU_UpdateTextureInternal(renderdata, cpass, data->textureV, bpp, rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, Vplane, UVpitch);
}
} }
#endif #endif

View File

@@ -440,6 +440,8 @@ static VkDeviceSize VULKAN_GetBytesPerPixel(VkFormat vkFormat, int plane)
return 1; return 1;
case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM: case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
return (plane == 0) ? 1 : 2; return (plane == 0) ? 1 : 2;
case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16:
return (plane == 0) ? 2 : 4;
default: default:
return 4; return 4;
} }
@@ -2708,8 +2710,8 @@ static bool VULKAN_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, S
samplerCreateInfo.magFilter = VK_FILTER_NEAREST; samplerCreateInfo.magFilter = VK_FILTER_NEAREST;
samplerCreateInfo.minFilter = VK_FILTER_NEAREST; samplerCreateInfo.minFilter = VK_FILTER_NEAREST;
samplerCreateInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST; samplerCreateInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST;
samplerCreateInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT; samplerCreateInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
samplerCreateInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT; samplerCreateInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
samplerCreateInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; samplerCreateInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
samplerCreateInfo.mipLodBias = 0.0f; samplerCreateInfo.mipLodBias = 0.0f;
samplerCreateInfo.anisotropyEnable = VK_FALSE; samplerCreateInfo.anisotropyEnable = VK_FALSE;
@@ -2911,6 +2913,19 @@ static bool VULKAN_UpdateTextureInternal(VULKAN_RenderData *rendererData, VkImag
return true; return true;
} }
#ifdef SDL_HAVE_YUV
static bool VULKAN_UpdateTextureNV(SDL_Renderer *renderer, SDL_Texture *texture,
const SDL_Rect *rect,
const Uint8 *Yplane, int Ypitch,
const Uint8 *UVplane, int UVpitch);
static bool VULKAN_UpdateTextureYUV(SDL_Renderer *renderer, SDL_Texture *texture,
const SDL_Rect *rect,
const Uint8 *Yplane, int Ypitch,
const Uint8 *Uplane, int Upitch,
const Uint8 *Vplane, int Vpitch);
#endif
static bool VULKAN_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, static bool VULKAN_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture,
const SDL_Rect *rect, const void *srcPixels, const SDL_Rect *rect, const void *srcPixels,
int srcPitch) int srcPitch)
@@ -2922,38 +2937,36 @@ static bool VULKAN_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture,
return SDL_SetError("Texture is not currently available"); return SDL_SetError("Texture is not currently available");
} }
if (!VULKAN_UpdateTextureInternal(rendererData, textureData->mainImage.image, textureData->mainImage.format, 0, rect->x, rect->y, rect->w, rect->h, srcPixels, srcPitch, &textureData->mainImage.imageLayout)) {
return false;
}
#ifdef SDL_HAVE_YUV #ifdef SDL_HAVE_YUV
Uint32 numPlanes = VULKAN_VkFormatGetNumPlanes(textureData->mainImage.format); Uint32 numPlanes = VULKAN_VkFormatGetNumPlanes(textureData->mainImage.format);
// Skip to the correct offset into the next texture if (numPlanes == 2) {
srcPixels = (const void *)((const Uint8 *)srcPixels + rect->h * srcPitch); // NV12/NV21 data
// YUV data int UVbpp = VULKAN_GetBytesPerPixel(textureData->mainImage.format, 1);
if (numPlanes == 3) { int Ypitch = srcPitch;
for (Uint32 plane = 1; plane < numPlanes; plane++) { int UVpitch = (srcPitch + (UVbpp - 1)) & ~(UVbpp - 1);
if (!VULKAN_UpdateTextureInternal(rendererData, textureData->mainImage.image, textureData->mainImage.format, plane, rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, srcPixels, (srcPitch + 1) / 2, &textureData->mainImage.imageLayout)) { const Uint8 *plane0 = (const Uint8 *)srcPixels;
return false; const Uint8 *plane1 = plane0 + rect->h * srcPitch;
}
// Skip to the correct offset into the next texture return VULKAN_UpdateTextureNV(renderer, texture, rect, plane0, Ypitch, plane1, UVpitch);
srcPixels = (const void *)((const Uint8 *)srcPixels + ((rect->h + 1) / 2) * ((srcPitch + 1) / 2));
} } else if (numPlanes == 3) {
} // YUV data
// NV12/NV21 data int Ypitch = srcPitch;
else if (numPlanes == 2) int UVpitch = ((Ypitch + 1) / 2);
{ const Uint8 *plane0 = (const Uint8 *)srcPixels;
if (texture->format == SDL_PIXELFORMAT_P010) { const Uint8 *plane1 = plane0 + rect->h * Ypitch;
srcPitch = (srcPitch + 3) & ~3; const Uint8 *plane2 = plane1 + ((rect->h + 1) / 2) * UVpitch;
if (texture->format == SDL_PIXELFORMAT_YV12) {
return VULKAN_UpdateTextureYUV(renderer, texture, rect, plane0, Ypitch, plane2, UVpitch, plane1, UVpitch);
} else { } else {
srcPitch = (srcPitch + 1) & ~1; return VULKAN_UpdateTextureYUV(renderer, texture, rect, plane0, Ypitch, plane1, UVpitch, plane2, UVpitch);
}
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)) {
return false;
} }
} }
#endif #endif
if (!VULKAN_UpdateTextureInternal(rendererData, textureData->mainImage.image, textureData->mainImage.format, 0, rect->x, rect->y, rect->w, rect->h, srcPixels, srcPitch, &textureData->mainImage.imageLayout)) {
return false;
}
return true; return true;
} }