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 "gstvkdescriptorpool.h"
26
27 /**
28 * SECTION:vkdescriptorpool
29 * @title: GstVulkanDescriptorPool
30 * @short_description: Vulkan descriptor pool
31 * @see_also: #GstVulkanDescriptorSet, #GstVulkanDescriptorCache, #GstVulkanDevice
32 */
33
34 #define GET_PRIV(pool) gst_vulkan_descriptor_pool_get_instance_private (pool)
35
36 #define GST_CAT_DEFAULT gst_vulkan_descriptor_pool_debug
37 GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
38
39 struct _GstVulkanDescriptorPoolPrivate
40 {
41 gsize max_sets;
42 gsize outstanding;
43 };
44
45 #define parent_class gst_vulkan_descriptor_pool_parent_class
46 G_DEFINE_TYPE_WITH_CODE (GstVulkanDescriptorPool, gst_vulkan_descriptor_pool,
47 GST_TYPE_OBJECT, G_ADD_PRIVATE (GstVulkanDescriptorPool);
48 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT,
49 "vulkancommandpool", 0, "Vulkan Command Pool"));
50
51 static void gst_vulkan_descriptor_pool_finalize (GObject * object);
52
53 static void
gst_vulkan_descriptor_pool_init(GstVulkanDescriptorPool * pool)54 gst_vulkan_descriptor_pool_init (GstVulkanDescriptorPool * pool)
55 {
56 }
57
58 static void
gst_vulkan_descriptor_pool_class_init(GstVulkanDescriptorPoolClass * device_class)59 gst_vulkan_descriptor_pool_class_init (GstVulkanDescriptorPoolClass *
60 device_class)
61 {
62 GObjectClass *gobject_class = (GObjectClass *) device_class;
63
64 gobject_class->finalize = gst_vulkan_descriptor_pool_finalize;
65 }
66
67 static void
gst_vulkan_descriptor_pool_finalize(GObject * object)68 gst_vulkan_descriptor_pool_finalize (GObject * object)
69 {
70 GstVulkanDescriptorPool *pool = GST_VULKAN_DESCRIPTOR_POOL (object);
71 #if 0
72 GstVulkanDescriptorPoolPrivate *priv = GET_PRIV (pool);
73
74 /* FIXME: track these correctly */
75 if (priv->outstanding > 0)
76 g_critical
77 ("Destroying a Vulkan descriptor pool that has outstanding descriptors!");
78 #endif
79
80 if (pool->pool)
81 vkDestroyDescriptorPool (pool->device->device, pool->pool, NULL);
82 pool->pool = VK_NULL_HANDLE;
83
84 gst_clear_object (&pool->device);
85
86 G_OBJECT_CLASS (parent_class)->finalize (object);
87 }
88
89 /**
90 * gst_vulkan_descriptor_pool_new_wrapped:
91 * @device: a #GstVulkanDevice
92 * @pool: (transfer full): a `VkDescriptorPool`
93 * @max_sets: maximum descriptor sets allocatable wit @pool
94 *
95 * Returns: (transfer full): a new #GstVulkanDescriptorPool
96 *
97 * Since: 1.18
98 */
99 GstVulkanDescriptorPool *
gst_vulkan_descriptor_pool_new_wrapped(GstVulkanDevice * device,VkDescriptorPool pool,gsize max_sets)100 gst_vulkan_descriptor_pool_new_wrapped (GstVulkanDevice * device,
101 VkDescriptorPool pool, gsize max_sets)
102 {
103 GstVulkanDescriptorPool *ret;
104 GstVulkanDescriptorPoolPrivate *priv;
105
106 g_return_val_if_fail (GST_IS_VULKAN_DEVICE (device), NULL);
107 g_return_val_if_fail (pool != VK_NULL_HANDLE, NULL);
108 g_return_val_if_fail (max_sets > 0, NULL);
109
110 ret = g_object_new (GST_TYPE_VULKAN_DESCRIPTOR_POOL, NULL);
111 ret->device = gst_object_ref (device);
112 ret->pool = pool;
113
114 priv = GET_PRIV (ret);
115 priv->max_sets = max_sets;
116
117 gst_object_ref_sink (ret);
118
119 return ret;
120 }
121
122 /**
123 * gst_vulkan_descriptor_pool_get_device
124 * @pool: a #GstVulkanDescriptorPool
125 *
126 * Returns: (transfer full): the parent #GstVulkanDevice for this descriptor pool
127 *
128 * Since: 1.18
129 */
130 GstVulkanDevice *
gst_vulkan_descriptor_pool_get_device(GstVulkanDescriptorPool * pool)131 gst_vulkan_descriptor_pool_get_device (GstVulkanDescriptorPool * pool)
132 {
133 g_return_val_if_fail (GST_IS_VULKAN_DESCRIPTOR_POOL (pool), NULL);
134
135 return pool->device ? gst_object_ref (pool->device) : NULL;
136 }
137
138 /**
139 * gst_vulkan_descriptor_pool_get_max_sets:
140 * @pool: a #GstVulkanDescriptorPool
141 *
142 * Returns: the maximum number of sets allocatable from @pool
143 *
144 * Since: 1.18
145 */
146 gsize
gst_vulkan_descriptor_pool_get_max_sets(GstVulkanDescriptorPool * pool)147 gst_vulkan_descriptor_pool_get_max_sets (GstVulkanDescriptorPool * pool)
148 {
149 GstVulkanDescriptorPoolPrivate *priv;
150
151 g_return_val_if_fail (GST_IS_VULKAN_DESCRIPTOR_POOL (pool), 0);
152
153 priv = GET_PRIV (pool);
154
155 return priv->max_sets;
156 }
157
158 static GstVulkanDescriptorSet *
descriptor_set_alloc(GstVulkanDescriptorPool * pool,guint n_layouts,GstVulkanHandle ** layouts,GError ** error)159 descriptor_set_alloc (GstVulkanDescriptorPool * pool, guint n_layouts,
160 GstVulkanHandle ** layouts, GError ** error)
161 {
162 VkDescriptorSetLayout *vk_layouts;
163 VkDescriptorSetAllocateInfo alloc_info;
164 GstVulkanDescriptorSet *set;
165 VkDescriptorSet descriptor;
166 VkResult err;
167 guint i;
168
169 vk_layouts = g_alloca (n_layouts * sizeof (VkDescriptorSetLayout));
170 for (i = 0; i < n_layouts; i++)
171 vk_layouts[i] = (VkDescriptorSetLayout) layouts[i]->handle;
172
173 /* *INDENT-OFF* */
174 alloc_info = (VkDescriptorSetAllocateInfo) {
175 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
176 .pNext = NULL,
177 .descriptorPool = pool->pool,
178 .descriptorSetCount = n_layouts,
179 .pSetLayouts = vk_layouts
180 };
181 /* *INDENT-ON* */
182
183 err =
184 vkAllocateDescriptorSets (pool->device->device, &alloc_info, &descriptor);
185 if (gst_vulkan_error_to_g_error (err, error, "vkAllocateDescriptorSets") < 0)
186 return NULL;
187
188 set =
189 gst_vulkan_descriptor_set_new_wrapped (pool, descriptor, n_layouts,
190 layouts);
191 GST_LOG_OBJECT (pool, "created descriptor set %p", set);
192
193 return set;
194 }
195
196 /**
197 * gst_vulkan_descriptor_pool_create:
198 * @pool: a #GstVulkanDescriptorPool
199 * @error: a #GError
200 *
201 * Returns: a new #GstVulkanDescriptorSet
202 *
203 * Since: 1.18
204 */
205 GstVulkanDescriptorSet *
gst_vulkan_descriptor_pool_create(GstVulkanDescriptorPool * pool,guint n_layouts,GstVulkanHandle ** layouts,GError ** error)206 gst_vulkan_descriptor_pool_create (GstVulkanDescriptorPool * pool,
207 guint n_layouts, GstVulkanHandle ** layouts, GError ** error)
208 {
209 GstVulkanDescriptorSet *cmd = NULL;
210 GstVulkanDescriptorPoolPrivate *priv;
211
212 g_return_val_if_fail (GST_IS_VULKAN_DESCRIPTOR_POOL (pool), NULL);
213 g_return_val_if_fail (n_layouts > 0, NULL);
214 g_return_val_if_fail (layouts != NULL, NULL);
215
216 priv = GET_PRIV (pool);
217
218 GST_OBJECT_LOCK (pool);
219 priv->outstanding++;
220 if (priv->outstanding >= priv->max_sets) {
221 g_warning ("%s: Attempt was made to allocate more descriptor sets than are "
222 "available", GST_OBJECT_NAME (pool));
223 g_set_error (error, GST_VULKAN_ERROR, VK_ERROR_TOO_MANY_OBJECTS,
224 "Attempt was made to allocate more descriptor sets than are available");
225 priv->outstanding--;
226 GST_OBJECT_UNLOCK (pool);
227 return NULL;
228 }
229 GST_OBJECT_UNLOCK (pool);
230
231 cmd = descriptor_set_alloc (pool, n_layouts, layouts, error);
232 if (!cmd)
233 return NULL;
234
235 return cmd;
236 }
237