Skip to content

Commit fb2287d

Browse files
committed
Fixed updating NV12 and YV12 textures on the direct3d11 renderer
Fixes #9928
1 parent 8604847 commit fb2287d

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

src/render/direct3d11/SDL_render_d3d11.c

+26-23
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,11 @@ static int D3D11_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture,
16261626
const Uint8 *Uplane = Yplane + rect->h * Ypitch;
16271627
const Uint8 *Vplane = Uplane + ((rect->h + 1) / 2) * UVpitch;
16281628

1629-
return D3D11_UpdateTextureYUV(renderer, texture, rect, Yplane, Ypitch, Uplane, UVpitch, Vplane, UVpitch);
1629+
if (texture->format == SDL_PIXELFORMAT_YV12) {
1630+
return D3D11_UpdateTextureYUV(renderer, texture, rect, Yplane, Ypitch, Vplane, UVpitch, Uplane, UVpitch);
1631+
} else {
1632+
return D3D11_UpdateTextureYUV(renderer, texture, rect, Yplane, Ypitch, Uplane, UVpitch, Vplane, UVpitch);
1633+
}
16301634
}
16311635
#endif
16321636

@@ -1653,10 +1657,10 @@ static int D3D11_UpdateTextureYUV(SDL_Renderer *renderer, SDL_Texture *texture,
16531657
if (D3D11_UpdateTextureInternal(rendererData, textureData->mainTexture, SDL_BYTESPERPIXEL(texture->format), rect->x, rect->y, rect->w, rect->h, Yplane, Ypitch) < 0) {
16541658
return -1;
16551659
}
1656-
if (D3D11_UpdateTextureInternal(rendererData, textureData->mainTextureU, SDL_BYTESPERPIXEL(texture->format), rect->x / 2, rect->y / 2, rect->w / 2, rect->h / 2, Uplane, Upitch) < 0) {
1660+
if (D3D11_UpdateTextureInternal(rendererData, textureData->mainTextureU, SDL_BYTESPERPIXEL(texture->format), rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, Uplane, Upitch) < 0) {
16571661
return -1;
16581662
}
1659-
if (D3D11_UpdateTextureInternal(rendererData, textureData->mainTextureV, SDL_BYTESPERPIXEL(texture->format), rect->x / 2, rect->y / 2, rect->w / 2, rect->h / 2, Vplane, Vpitch) < 0) {
1663+
if (D3D11_UpdateTextureInternal(rendererData, textureData->mainTextureV, SDL_BYTESPERPIXEL(texture->format), rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, Vplane, Vpitch) < 0) {
16601664
return -1;
16611665
}
16621666
return 0;
@@ -1693,6 +1697,11 @@ static int D3D11_UpdateTextureNV(SDL_Renderer *renderer, SDL_Texture *texture,
16931697
stagingTextureDesc.MiscFlags = 0;
16941698
stagingTextureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
16951699
stagingTextureDesc.Usage = D3D11_USAGE_STAGING;
1700+
if (stagingTextureDesc.Format == DXGI_FORMAT_NV12 ||
1701+
stagingTextureDesc.Format == DXGI_FORMAT_P010) {
1702+
stagingTextureDesc.Width = (stagingTextureDesc.Width + 1) & ~1;
1703+
stagingTextureDesc.Height = (stagingTextureDesc.Height + 1) & ~1;
1704+
}
16961705
result = ID3D11Device_CreateTexture2D(rendererData->d3dDevice,
16971706
&stagingTextureDesc,
16981707
NULL,
@@ -1714,11 +1723,10 @@ static int D3D11_UpdateTextureNV(SDL_Renderer *renderer, SDL_Texture *texture,
17141723
}
17151724

17161725
src = Yplane;
1717-
dst = textureMemory.pData;
1726+
dst = (Uint8 *)textureMemory.pData;
17181727
length = w;
17191728
if (length == (UINT)Ypitch && length == textureMemory.RowPitch) {
1720-
SDL_memcpy(dst, src, (size_t)length * rect->h);
1721-
dst += length * rect->h;
1729+
SDL_memcpy(dst, src, (size_t)length * h);
17221730
} else {
17231731
if (length > (UINT)Ypitch) {
17241732
length = Ypitch;
@@ -1733,26 +1741,21 @@ static int D3D11_UpdateTextureNV(SDL_Renderer *renderer, SDL_Texture *texture,
17331741
}
17341742
}
17351743

1736-
/* Adjust dimensions for the UV plane */
1737-
w = ((w + 1) / 2) * 2;
1738-
h = ((h + 1) / 2);
1739-
17401744
src = UVplane;
17411745
length = w;
1742-
if (length == (UINT)UVpitch && length == textureMemory.RowPitch) {
1743-
SDL_memcpy(dst, src, (size_t)length * h);
1746+
h = (h + 1) / 2;
1747+
if (stagingTextureDesc.Format == DXGI_FORMAT_P010) {
1748+
length = (length + 3) & ~3;
1749+
UVpitch = (UVpitch + 3) & ~3;
17441750
} else {
1745-
if (length > (UINT)UVpitch) {
1746-
length = UVpitch;
1747-
}
1748-
if (length > textureMemory.RowPitch) {
1749-
length = textureMemory.RowPitch;
1750-
}
1751-
for (row = 0; row < h; ++row) {
1752-
SDL_memcpy(dst, src, length);
1753-
src += UVpitch;
1754-
dst += textureMemory.RowPitch;
1755-
}
1751+
length = (length + 1) & ~1;
1752+
UVpitch = (UVpitch + 1) & ~1;
1753+
}
1754+
dst = (Uint8 *)textureMemory.pData + stagingTextureDesc.Height * textureMemory.RowPitch;
1755+
for (row = 0; row < h; ++row) {
1756+
SDL_memcpy(dst, src, length);
1757+
src += UVpitch;
1758+
dst += textureMemory.RowPitch;
17561759
}
17571760

17581761
/* Commit the pixel buffer's changes back to the staging texture: */

0 commit comments

Comments
 (0)