vkformat: try UNORM format first and decouple them from colorimetry

From the spec (chapter 34, v1.3.283):

````
UNORM: the components are unsigned normalized values in the range [0, 1]

SRGB: the R, G and B components are unsigned normalized value that represent
      values using sRGB nonlinear encoding, while the A component (if one
      exists) is a regular unsigned normalized value
```

The difference is the storage encoding, the first one is aimed for image
transfers, while the second is for shaders, mostly in the swapchain stage in the
pipeline, and it's done automatically if needed [1].

As far as I have checked, other frameworks (FFmpeg, GTK+), when import or export
images from/to Vulkan, use exclusively UNORM formats, while SRGB formats are
ignored.

My conclusion is that Vulkan formats are related on how bits are stored in
memory rather their transfer functions (colorimetry).

This patch does two interrelated changes:

1. It swaps certain color format maps to try first, in both
gst_vulkan_format_from_video_info() and gst_vulkan_format_from_video_info_2(),
the UNORM formats, when comparing its usage, and later check for SRGB.

2. It removes the code that check for colorimetry in
gst_vulkan_format_from_video_info_2(), since it not storage related.

1. https://community.khronos.org/t/noob-difference-between-unorm-and-srgb/106132/7

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/6797>
This commit is contained in:
Víctor Manuel Jáquez Leal 2024-04-17 12:19:03 +02:00
parent 417e784463
commit e913b4870a
2 changed files with 29 additions and 41 deletions

View file

@ -438,15 +438,15 @@ const static struct {
VkFormat vkfrmt;
VkFormat vkfrmts[GST_VIDEO_MAX_PLANES];
} vk_formats_map[] = {
/* RGB transfer sRGB */
{ GST_VIDEO_FORMAT_RGBA, VK_FORMAT_R8G8B8A8_SRGB, { VK_FORMAT_R8G8B8A8_UNORM, } },
{ GST_VIDEO_FORMAT_RGBx, VK_FORMAT_R8G8B8A8_SRGB, { VK_FORMAT_R8G8B8A8_UNORM, } },
{ GST_VIDEO_FORMAT_BGRA, VK_FORMAT_B8G8R8A8_SRGB, { VK_FORMAT_B8G8R8A8_UNORM, } },
{ GST_VIDEO_FORMAT_BGRx, VK_FORMAT_B8G8R8A8_SRGB, { VK_FORMAT_B8G8R8A8_UNORM, } },
{ GST_VIDEO_FORMAT_ARGB, VK_FORMAT_UNDEFINED, { VK_FORMAT_R8G8B8A8_UNORM, } },
{ GST_VIDEO_FORMAT_xRGB, VK_FORMAT_UNDEFINED, { VK_FORMAT_R8G8B8A8_UNORM, } },
{ GST_VIDEO_FORMAT_ABGR, VK_FORMAT_UNDEFINED, { VK_FORMAT_R8G8B8A8_UNORM, } },
{ GST_VIDEO_FORMAT_xBGR, VK_FORMAT_UNDEFINED, { VK_FORMAT_R8G8B8A8_UNORM, } },
/* RGB unsigned normalized format sRGB nonlinear encoding */
{ GST_VIDEO_FORMAT_RGBA, VK_FORMAT_R8G8B8A8_UNORM, { VK_FORMAT_R8G8B8A8_SRGB, } },
{ GST_VIDEO_FORMAT_RGBx, VK_FORMAT_R8G8B8A8_UNORM, { VK_FORMAT_R8G8B8A8_SRGB, } },
{ GST_VIDEO_FORMAT_BGRA, VK_FORMAT_B8G8R8A8_UNORM, { VK_FORMAT_B8G8R8A8_SRGB, } },
{ GST_VIDEO_FORMAT_BGRx, VK_FORMAT_B8G8R8A8_UNORM, { VK_FORMAT_B8G8R8A8_SRGB, } },
{ GST_VIDEO_FORMAT_ARGB, VK_FORMAT_R8G8B8A8_UNORM, { VK_FORMAT_UNDEFINED, } },
{ GST_VIDEO_FORMAT_xRGB, VK_FORMAT_R8G8B8A8_UNORM, { VK_FORMAT_UNDEFINED, } },
{ GST_VIDEO_FORMAT_ABGR, VK_FORMAT_R8G8B8A8_UNORM, { VK_FORMAT_UNDEFINED, } },
{ GST_VIDEO_FORMAT_xBGR, VK_FORMAT_R8G8B8A8_UNORM, { VK_FORMAT_UNDEFINED, } },
{ GST_VIDEO_FORMAT_RGB, VK_FORMAT_R8G8B8_UNORM, { VK_FORMAT_UNDEFINED, } },
{ GST_VIDEO_FORMAT_BGR, VK_FORMAT_B8G8R8_UNORM, { VK_FORMAT_UNDEFINED, } },
{ GST_VIDEO_FORMAT_RGB16, VK_FORMAT_R5G6B5_UNORM_PACK16, { VK_FORMAT_UNDEFINED, } },
@ -494,12 +494,7 @@ gst_vulkan_format_from_video_info (GstVideoInfo * v_info, guint plane)
continue;
if (GST_VIDEO_INFO_IS_RGB (v_info)) {
if (GST_VIDEO_INFO_COLORIMETRY (v_info).transfer ==
GST_VIDEO_TRANSFER_SRGB) {
return vk_formats_map[i].vkfrmt;
} else {
return vk_formats_map[i].vkfrmts[0];
}
return vk_formats_map[i].vkfrmt;
} else if (GST_VIDEO_INFO_IS_YUV (v_info) &&
GST_VIDEO_INFO_N_PLANES (v_info) > plane) {
return vk_formats_map[i].vkfrmts[plane];
@ -655,33 +650,26 @@ gst_vulkan_format_from_video_info_2 (GstVulkanPhysicalDevice * physical_device,
}
if (GST_VIDEO_INFO_IS_RGB (info)) {
if ((GST_VIDEO_INFO_COLORIMETRY (info).transfer ==
GST_VIDEO_TRANSFER_SRGB
|| GST_VIDEO_INFO_COLORIMETRY (info).transfer ==
GST_VIDEO_TRANSFER_UNKNOWN)) {
usage = _get_usage (feats_primary);
if ((requested_usage & usage) == requested_usage) {
if (fmts)
fmts[0] = vk_formats_map[i].vkfrmt;
if (n_imgs)
*n_imgs = 1;
if (usage_ret)
*usage_ret = usage;
return TRUE;
}
usage = _get_usage (feats_primary);
if ((requested_usage & usage) == requested_usage) {
if (fmts)
fmts[0] = vk_formats_map[i].vkfrmt;
if (n_imgs)
*n_imgs = 1;
if (usage_ret)
*usage_ret = usage;
return TRUE;
}
if (GST_VIDEO_INFO_COLORIMETRY (info).transfer != GST_VIDEO_TRANSFER_SRGB) {
usage = _get_usage (feats_secondary);
if ((requested_usage & usage) == requested_usage) {
if (fmts)
fmts[0] = vk_formats_map[i].vkfrmts[0];
if (n_imgs)
*n_imgs = 1;
if (usage_ret)
*usage_ret = usage;
return TRUE;
}
usage = _get_usage (feats_secondary);
if ((requested_usage & usage) == requested_usage) {
if (fmts)
fmts[0] = vk_formats_map[i].vkfrmts[0];
if (n_imgs)
*n_imgs = 1;
if (usage_ret)
*usage_ret = usage;
return TRUE;
}
return FALSE;
} else {

View file

@ -77,7 +77,7 @@ GST_START_TEST (test_format_from_video_info_2)
fail_unless (gst_vulkan_format_from_video_info_2 (phy_dev, &vinfo,
VK_IMAGE_TILING_LINEAR, TRUE, 0, vk_fmts, &n_imgs, &supported_usage));
fail_unless (n_imgs == 1 && vk_fmts[0] == VK_FORMAT_R8G8B8A8_SRGB);
fail_unless (n_imgs == 1 && vk_fmts[0] == VK_FORMAT_R8G8B8A8_UNORM);
fail_unless (gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_RGBA, 620,
480));