82f50b5ea8ebdc999b49eececd8962c98bd2b3a4
[platform/upstream/gstreamer.git] / ext / vulkan / vkelementutils.c
1 /*
2  * GStreamer
3  * Copyright (C) 2019 Matthew Waters <matthew@centricular.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "vkelementutils.h"
26
27 static void
28 fill_vulkan_image_view_info (VkImage image, VkFormat format,
29     VkImageViewCreateInfo * info)
30 {
31   /* *INDENT-OFF* */
32   *info = (VkImageViewCreateInfo) {
33       .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
34       .pNext = NULL,
35       .image = image,
36       .format = format,
37       .viewType = VK_IMAGE_VIEW_TYPE_2D,
38       .flags = 0,
39       .components = (VkComponentMapping) {
40           VK_COMPONENT_SWIZZLE_R,
41           VK_COMPONENT_SWIZZLE_G,
42           VK_COMPONENT_SWIZZLE_B,
43           VK_COMPONENT_SWIZZLE_A
44       },
45       .subresourceRange = (VkImageSubresourceRange) {
46           .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
47           .baseMipLevel = 0,
48           .levelCount = 1,
49           .baseArrayLayer = 0,
50           .layerCount = 1,
51       }
52   };
53   /* *INDENT-ON* */
54 }
55
56 static gboolean
57 find_compatible_view (GstVulkanImageView * view, VkImageViewCreateInfo * info)
58 {
59   return view->create_info.image == info->image
60       && view->create_info.format == info->format
61       && view->create_info.viewType == info->viewType
62       && view->create_info.flags == info->flags
63       && view->create_info.components.r == info->components.r
64       && view->create_info.components.g == info->components.g
65       && view->create_info.components.b == info->components.b
66       && view->create_info.components.a == info->components.a
67       && view->create_info.subresourceRange.aspectMask ==
68       info->subresourceRange.aspectMask
69       && view->create_info.subresourceRange.baseMipLevel ==
70       info->subresourceRange.baseMipLevel
71       && view->create_info.subresourceRange.levelCount ==
72       info->subresourceRange.levelCount
73       && view->create_info.subresourceRange.levelCount ==
74       info->subresourceRange.levelCount
75       && view->create_info.subresourceRange.baseArrayLayer ==
76       info->subresourceRange.baseArrayLayer
77       && view->create_info.subresourceRange.layerCount ==
78       info->subresourceRange.layerCount;
79 }
80
81 GstVulkanImageView *
82 get_or_create_image_view (GstVulkanImageMemory * image)
83 {
84   VkImageViewCreateInfo create_info;
85   GstVulkanImageView *ret = NULL;
86
87   fill_vulkan_image_view_info (image->image, image->create_info.format,
88       &create_info);
89
90   ret = gst_vulkan_image_memory_find_view (image,
91       (GstVulkanImageMemoryFindViewFunc) find_compatible_view, &create_info);
92   if (!ret) {
93     ret = gst_vulkan_image_view_new (image, &create_info);
94     gst_vulkan_image_memory_add_view (image, ret);
95   }
96
97   return ret;
98 }