https://github.com/libsdl-org/SDL/commit/e9bc23196e6d0ebc5cb61117aba7299655658289
From e9bc23196e6d0ebc5cb61117aba7299655658289 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 8 Oct 2026 08:27:54 -0700
Subject: [PATCH] Added support for 2-plane external NV12 textures to the
Vulkan renderer
---
include/SDL3/SDL_render.h | 9 ++++++-
src/render/vulkan/SDL_render_vulkan.c | 35 ++++++++++++++++++++++-----
2 files changed, 37 insertions(+), 7 deletions(-)
diff --git a/include/SDL3/SDL_render.h b/include/SDL3/SDL_render.h
index b2ee1d3362726..323a85a94ec0a 100644
--- a/include/SDL3/SDL_render.h
+++ b/include/SDL3/SDL_render.h
@@ -804,8 +804,11 @@ extern SDL_DECLSPEC SDL_Texture * SDLCALL SDL_CreateTextureFromSurface(SDL_Rende
*
* - `SDL_PROP_TEXTURE_CREATE_VULKAN_TEXTURE_NUMBER`: the VkImage associated
* with the texture, if you want to wrap an existing texture. For NV12 style
- * textures this is the single two plane VkImage holding both the Y and UV
+ * textures this can be a single two plane VkImage holding both the Y and UV
* planes, and for YUV style textures it is the VkImage holding the Y plane.
+ * - `SDL_PROP_TEXTURE_CREATE_VULKAN_TEXTURE_UV_NUMBER`: the VkImage
+ * associated with the UV plane of an NV12 texture, if you want to wrap an
+ * existing texture.
* - `SDL_PROP_TEXTURE_CREATE_VULKAN_TEXTURE_U_NUMBER`: the VkImage associated
* with the U plane of a YUV texture, if you want to wrap an existing
* texture.
@@ -888,6 +891,7 @@ extern SDL_DECLSPEC SDL_Texture * SDLCALL SDL_CreateTextureWithProperties(SDL_Re
#define SDL_PROP_TEXTURE_CREATE_OPENGLES2_TEXTURE_V_NUMBER "SDL.texture.create.opengles2.texture_v"
#define SDL_PROP_TEXTURE_CREATE_VULKAN_TEXTURE_NUMBER "SDL.texture.create.vulkan.texture"
#define SDL_PROP_TEXTURE_CREATE_VULKAN_TEXTURE_U_NUMBER "SDL.texture.create.vulkan.texture_u"
+#define SDL_PROP_TEXTURE_CREATE_VULKAN_TEXTURE_UV_NUMBER "SDL.texture.create.vulkan.texture_uv"
#define SDL_PROP_TEXTURE_CREATE_VULKAN_TEXTURE_V_NUMBER "SDL.texture.create.vulkan.texture_v"
#define SDL_PROP_TEXTURE_CREATE_VULKAN_LAYOUT_NUMBER "SDL.texture.create.vulkan.layout"
#define SDL_PROP_TEXTURE_CREATE_VULKAN_USAGE_NUMBER "SDL.texture.create.vulkan.usage"
@@ -957,6 +961,8 @@ extern SDL_DECLSPEC SDL_Texture * SDLCALL SDL_CreateTextureWithProperties(SDL_Re
* texture. For NV12 style textures this is the single two plane VkImage
* holding both the Y and UV planes, and for YUV style textures it is the
* VkImage holding the Y plane.
+ * - `SDL_PROP_TEXTURE_VULKAN_TEXTURE_UV_NUMBER`: the VkImage associated with
+ * the UV plane of an NV12 texture, if it was created with separate planes.
* - `SDL_PROP_TEXTURE_VULKAN_TEXTURE_U_NUMBER`: the VkImage associated with
* the U plane of a YUV texture
* - `SDL_PROP_TEXTURE_VULKAN_TEXTURE_V_NUMBER`: the VkImage associated with
@@ -1043,6 +1049,7 @@ extern SDL_DECLSPEC SDL_PropertiesID SDLCALL SDL_GetTextureProperties(SDL_Textur
#define SDL_PROP_TEXTURE_OPENGLES2_TEXTURE_V_NUMBER "SDL.texture.opengles2.texture_v"
#define SDL_PROP_TEXTURE_OPENGLES2_TEXTURE_TARGET_NUMBER "SDL.texture.opengles2.target"
#define SDL_PROP_TEXTURE_VULKAN_TEXTURE_NUMBER "SDL.texture.vulkan.texture"
+#define SDL_PROP_TEXTURE_VULKAN_TEXTURE_UV_NUMBER "SDL.texture.vulkan.texture_uv"
#define SDL_PROP_TEXTURE_VULKAN_TEXTURE_U_NUMBER "SDL.texture.vulkan.texture_u"
#define SDL_PROP_TEXTURE_VULKAN_TEXTURE_V_NUMBER "SDL.texture.vulkan.texture_v"
#define SDL_PROP_TEXTURE_GPU_TEXTURE_POINTER "SDL.texture.gpu.texture"
diff --git a/src/render/vulkan/SDL_render_vulkan.c b/src/render/vulkan/SDL_render_vulkan.c
index f3ae7ca1bf3ef..0ef259f876cd0 100644
--- a/src/render/vulkan/SDL_render_vulkan.c
+++ b/src/render/vulkan/SDL_render_vulkan.c
@@ -3004,6 +3004,7 @@ static bool VULKAN_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, S
int numImages = VULKAN_GetFormatImageCount(texture->format);
int numImageViews = VULKAN_GetFormatImageViewCount(texture->format);
VkFormat textureFormat = VULKAN_GetVkImageFormat(texture->format, renderer->output_colorspace);
+ VkFormat chromaFormat = textureFormat;
uint32_t width = texture->w;
uint32_t height = texture->h;
uint32_t chromaWidth = width;
@@ -3180,18 +3181,29 @@ static bool VULKAN_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, S
}
usage |= (VkImageUsageFlags)SDL_GetNumberProperty(create_props, SDL_PROP_TEXTURE_CREATE_VULKAN_USAGE_NUMBER, 0);
- static const char *imageCreateProperties[] = {
+ const char *imageCreateProperties[] = {
SDL_PROP_TEXTURE_CREATE_VULKAN_TEXTURE_NUMBER,
SDL_PROP_TEXTURE_CREATE_VULKAN_TEXTURE_U_NUMBER,
SDL_PROP_TEXTURE_CREATE_VULKAN_TEXTURE_V_NUMBER
};
SDL_COMPILE_TIME_ASSERT(imageCreateProperties, SDL_arraysize(imageCreateProperties) == SDL_VULKAN_NUM_TEXTURE_BINDINGS);
+ if (numImageViews == 2 &&
+ SDL_HasProperty(create_props, SDL_PROP_TEXTURE_CREATE_VULKAN_TEXTURE_UV_NUMBER)) {
+ numImages = 2;
+ chromaWidth = (width / 2);
+ chromaHeight = (height / 2);
+ textureFormat = VULKAN_GetVkImageViewFormat(texture->format, 0, renderer->output_colorspace);
+ chromaFormat = VULKAN_GetVkImageViewFormat(texture->format, 1, renderer->output_colorspace);
+ imageCreateProperties[1] = SDL_PROP_TEXTURE_CREATE_VULKAN_TEXTURE_UV_NUMBER;
+ }
+
VkSamplerYcbcrConversionKHR samplerYcbcrConversion = (textureData->yuvPipeline ? textureData->yuvPipeline->samplerYcbcrConversion : VK_NULL_HANDLE);
for (int i = 0; i < numImages; ++i) {
uint32_t imageWidth = (i == 0 ? width : chromaWidth);
uint32_t imageHeight = (i == 0 ? height : chromaHeight);
- result = VULKAN_AllocateImage(rendererData, create_props, imageCreateProperties[i], imageWidth, imageHeight, textureFormat, usage, samplerYcbcrConversion, external, &textureData->images[i]);
+ VkFormat imageFormat = (i == 0 ? textureFormat : chromaFormat);
+ result = VULKAN_AllocateImage(rendererData, create_props, imageCreateProperties[i], imageWidth, imageHeight, imageFormat, usage, samplerYcbcrConversion, external, &textureData->images[i]);
if (result != VK_SUCCESS) {
SET_ERROR_CODE("VULKAN_AllocateImage()", result);
return false;
@@ -3231,13 +3243,18 @@ static bool VULKAN_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, S
}
}
- static const char *imageProperties[] = {
+ const char *imageProperties[] = {
SDL_PROP_TEXTURE_VULKAN_TEXTURE_NUMBER,
SDL_PROP_TEXTURE_VULKAN_TEXTURE_U_NUMBER,
SDL_PROP_TEXTURE_VULKAN_TEXTURE_V_NUMBER
};
SDL_COMPILE_TIME_ASSERT(imageProperties, SDL_arraysize(imageProperties) == SDL_VULKAN_NUM_TEXTURE_BINDINGS);
+ if (numImageViews == 2 &&
+ SDL_HasProperty(create_props, SDL_PROP_TEXTURE_CREATE_VULKAN_TEXTURE_UV_NUMBER)) {
+ imageProperties[1] = SDL_PROP_TEXTURE_VULKAN_TEXTURE_UV_NUMBER;
+ }
+
SDL_PropertiesID props = SDL_GetTextureProperties(texture);
for (int i = 0; i < numImages; ++i) {
SDL_SetNumberProperty(props, imageProperties[i], (Sint64)textureData->images[i].image);
@@ -3407,7 +3424,7 @@ static bool VULKAN_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture,
if (textureData->numImageViews == 2) {
// NV12/NV21 data
- int UVbpp = (int)VULKAN_GetBytesPerPixel(textureData->images[0].format, 1);
+ int UVbpp = (int)VULKAN_GetBytesPerPixel(textureData->images[textureData->numImages - 1].format, 1);
int Ypitch = srcPitch;
int UVpitch = (srcPitch + (UVbpp - 1)) & ~(UVbpp - 1);
const Uint8 *plane0 = (const Uint8 *)srcPixels;
@@ -3508,8 +3525,14 @@ static bool VULKAN_UpdateTextureNV(SDL_Renderer *renderer, SDL_Texture *texture,
return false;
}
- if (!VULKAN_UpdateTextureInternal(rendererData, textureData->images[0].image, textureData->images[0].format, 1, rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, UVplane, UVpitch, &textureData->images[0].imageLayout)) {
- return false;
+ if (textureData->numImages == 2) {
+ if (!VULKAN_UpdateTextureInternal(rendererData, textureData->images[1].image, textureData->images[1].format, 0, rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, UVplane, UVpitch, &textureData->images[1].imageLayout)) {
+ return false;
+ }
+ } else {
+ if (!VULKAN_UpdateTextureInternal(rendererData, textureData->images[0].image, textureData->images[0].format, 1, rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, UVplane, UVpitch, &textureData->images[0].imageLayout)) {
+ return false;
+ }
}
return true;
}