3 * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
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.
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.
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.
25 #include "gstvkdevice.h"
26 #include "gstvkdebug.h"
32 * @title: GstVulkanDevice
33 * @short_description: Vulkan device
34 * @see_also: #GstVulkanPhysicalDevice, #GstVulkanInstance
36 * A #GstVulkanDevice encapsulates a VkDevice
39 #define GST_CAT_DEFAULT gst_vulkan_device_debug
40 GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
41 GST_DEBUG_CATEGORY_STATIC (GST_CAT_CONTEXT);
43 #define GET_PRIV(o) (gst_vulkan_device_get_instance_private (o))
52 static void gst_vulkan_device_dispose (GObject * object);
53 static void gst_vulkan_device_finalize (GObject * object);
55 struct _GstVulkanDevicePrivate
57 GPtrArray *enabled_layers;
58 GPtrArray *enabled_extensions;
61 guint queue_family_id;
64 GstVulkanFenceCache *fence_cache;
72 if (g_once_init_enter (&init)) {
73 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "vulkandevice", 0,
75 GST_DEBUG_CATEGORY_GET (GST_CAT_CONTEXT, "GST_CONTEXT");
76 g_once_init_leave (&init, 1);
80 #define gst_vulkan_device_parent_class parent_class
81 G_DEFINE_TYPE_WITH_CODE (GstVulkanDevice, gst_vulkan_device, GST_TYPE_OBJECT,
82 G_ADD_PRIVATE (GstVulkanDevice);
86 * gst_vulkan_device_new:
87 * @physical_device: the associated #GstVulkanPhysicalDevice
89 * Returns: (transfer full): a new #GstVulkanDevice
94 gst_vulkan_device_new (GstVulkanPhysicalDevice * physical_device)
96 GstVulkanDevice *device;
98 g_return_val_if_fail (GST_IS_VULKAN_PHYSICAL_DEVICE (physical_device), NULL);
100 device = g_object_new (GST_TYPE_VULKAN_DEVICE, "physical-device",
101 physical_device, NULL);
102 gst_object_ref_sink (device);
108 * gst_vulkan_device_new_with_index:
109 * @instance: the associated #GstVulkanInstance
110 * @device_index: the device index to create the new #GstVulkanDevice from
112 * Returns: (transfer full): a new #GstVulkanDevice
117 gst_vulkan_device_new_with_index (GstVulkanInstance * instance,
120 GstVulkanPhysicalDevice *physical;
121 GstVulkanDevice *device;
123 g_return_val_if_fail (GST_IS_VULKAN_INSTANCE (instance), NULL);
125 physical = gst_vulkan_physical_device_new (instance, device_index);
126 device = gst_vulkan_device_new (physical);
127 gst_object_unref (physical);
132 gst_vulkan_device_set_property (GObject * object, guint prop_id,
133 const GValue * value, GParamSpec * pspec)
135 GstVulkanDevice *device = GST_VULKAN_DEVICE (object);
138 case PROP_PHYSICAL_DEVICE:
139 device->physical_device = g_value_dup_object (value);
142 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
148 gst_vulkan_device_get_property (GObject * object, guint prop_id,
149 GValue * value, GParamSpec * pspec)
151 GstVulkanDevice *device = GST_VULKAN_DEVICE (object);
155 g_value_set_object (value, device->instance);
157 case PROP_PHYSICAL_DEVICE:
158 g_value_set_object (value, device->physical_device);
161 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
167 gst_vulkan_device_init (GstVulkanDevice * device)
169 GstVulkanDevicePrivate *priv = GET_PRIV (device);
171 priv->enabled_layers = g_ptr_array_new_with_free_func (g_free);
172 priv->enabled_extensions = g_ptr_array_new_with_free_func (g_free);
176 gst_vulkan_device_constructed (GObject * object)
178 GstVulkanDevice *device = GST_VULKAN_DEVICE (object);
180 g_object_get (device->physical_device, "instance", &device->instance, NULL);
182 /* by default allow vkswapper to work for rendering to an output window.
183 * Ignore the failure if the extension does not exist. */
184 gst_vulkan_device_enable_extension (device, VK_KHR_SWAPCHAIN_EXTENSION_NAME);
186 G_OBJECT_CLASS (parent_class)->constructed (object);
190 gst_vulkan_device_class_init (GstVulkanDeviceClass * device_class)
192 GObjectClass *gobject_class = (GObjectClass *) device_class;
194 gobject_class->set_property = gst_vulkan_device_set_property;
195 gobject_class->get_property = gst_vulkan_device_get_property;
196 gobject_class->finalize = gst_vulkan_device_finalize;
197 gobject_class->dispose = gst_vulkan_device_dispose;
198 gobject_class->constructed = gst_vulkan_device_constructed;
200 g_object_class_install_property (gobject_class, PROP_INSTANCE,
201 g_param_spec_object ("instance", "Instance",
202 "Associated Vulkan Instance",
203 GST_TYPE_VULKAN_INSTANCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
204 g_object_class_install_property (gobject_class, PROP_PHYSICAL_DEVICE,
205 g_param_spec_object ("physical-device", "Physical Device",
206 "Associated Vulkan Physical Device",
207 GST_TYPE_VULKAN_PHYSICAL_DEVICE,
208 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
212 gst_vulkan_device_dispose (GObject * object)
214 GstVulkanDevice *device = GST_VULKAN_DEVICE (object);
215 GstVulkanDevicePrivate *priv = GET_PRIV (device);
217 if (priv->fence_cache) {
218 /* clear any outstanding fences */
219 g_object_run_dispose (G_OBJECT (priv->fence_cache));
221 /* don't double free this device */
222 priv->fence_cache->parent.device = NULL;
224 gst_clear_object (&priv->fence_cache);
226 G_OBJECT_CLASS (parent_class)->dispose (object);
230 gst_vulkan_device_finalize (GObject * object)
232 GstVulkanDevice *device = GST_VULKAN_DEVICE (object);
233 GstVulkanDevicePrivate *priv = GET_PRIV (device);
235 if (device->device) {
236 vkDeviceWaitIdle (device->device);
237 vkDestroyDevice (device->device, NULL);
239 device->device = VK_NULL_HANDLE;
241 gst_clear_object (&device->physical_device);
242 gst_clear_object (&device->instance);
244 g_ptr_array_unref (priv->enabled_layers);
245 priv->enabled_layers = NULL;
247 g_ptr_array_unref (priv->enabled_extensions);
248 priv->enabled_extensions = NULL;
250 G_OBJECT_CLASS (parent_class)->finalize (object);
254 * gst_vulkan_device_open:
255 * @device: a #GstVulkanDevice
258 * Attempts to create the internal `VkDevice` object.
260 * Returns: whether a vulkan device could be created
265 gst_vulkan_device_open (GstVulkanDevice * device, GError ** error)
267 GstVulkanDevicePrivate *priv = GET_PRIV (device);
268 VkPhysicalDevice gpu;
272 g_return_val_if_fail (GST_IS_VULKAN_DEVICE (device), FALSE);
274 GST_OBJECT_LOCK (device);
277 GST_OBJECT_UNLOCK (device);
281 gpu = gst_vulkan_device_get_physical_device (device);
283 /* FIXME: allow overriding/selecting */
284 for (i = 0; i < device->physical_device->n_queue_families; i++) {
285 if (device->physical_device->
286 queue_family_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
289 if (i >= device->physical_device->n_queue_families) {
290 g_set_error (error, GST_VULKAN_ERROR, VK_ERROR_INITIALIZATION_FAILED,
291 "Failed to find a compatible queue family");
294 priv->queue_family_id = i;
297 GST_INFO_OBJECT (device, "Creating a device from physical %" GST_PTR_FORMAT
298 " with %u layers and %u extensions", device->physical_device,
299 priv->enabled_layers->len, priv->enabled_extensions->len);
301 for (i = 0; i < priv->enabled_layers->len; i++)
302 GST_DEBUG_OBJECT (device, "layer %u: %s", i,
303 (gchar *) g_ptr_array_index (priv->enabled_layers, i));
304 for (i = 0; i < priv->enabled_extensions->len; i++)
305 GST_DEBUG_OBJECT (device, "extension %u: %s", i,
306 (gchar *) g_ptr_array_index (priv->enabled_extensions, i));
309 VkDeviceQueueCreateInfo queue_info = { 0, };
310 VkDeviceCreateInfo device_info = { 0, };
311 gfloat queue_priority = 0.5;
313 queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
314 queue_info.pNext = NULL;
315 queue_info.queueFamilyIndex = priv->queue_family_id;
316 queue_info.queueCount = priv->n_queues;
317 queue_info.pQueuePriorities = &queue_priority;
319 device_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
320 device_info.pNext = NULL;
321 device_info.queueCreateInfoCount = 1;
322 device_info.pQueueCreateInfos = &queue_info;
323 device_info.enabledLayerCount = priv->enabled_layers->len;
324 device_info.ppEnabledLayerNames =
325 (const char *const *) priv->enabled_layers->pdata;
326 device_info.enabledExtensionCount = priv->enabled_extensions->len;
327 device_info.ppEnabledExtensionNames =
328 (const char *const *) priv->enabled_extensions->pdata;
329 device_info.pEnabledFeatures = NULL;
331 err = vkCreateDevice (gpu, &device_info, NULL, &device->device);
332 if (gst_vulkan_error_to_g_error (err, error, "vkCreateDevice") < 0) {
337 priv->fence_cache = gst_vulkan_fence_cache_new (device);
338 /* avoid reference loops between us and the fence cache */
339 gst_object_unref (device);
342 GST_OBJECT_UNLOCK (device);
347 GST_OBJECT_UNLOCK (device);
353 * gst_vulkan_device_get_queue:
354 * @device: a #GstVulkanDevice
355 * @queue_family: a queue family to retrieve
356 * @queue_i: index of the family to retrieve
358 * Returns: (transfer full): a new #GstVulkanQueue
363 gst_vulkan_device_get_queue (GstVulkanDevice * device, guint32 queue_family,
366 GstVulkanDevicePrivate *priv = GET_PRIV (device);
369 g_return_val_if_fail (GST_IS_VULKAN_DEVICE (device), NULL);
370 g_return_val_if_fail (device->device != NULL, NULL);
371 g_return_val_if_fail (priv->opened, NULL);
372 g_return_val_if_fail (queue_family < priv->n_queues, NULL);
373 g_return_val_if_fail (queue_i <
374 device->physical_device->queue_family_props[queue_family].queueCount,
377 ret = g_object_new (GST_TYPE_VULKAN_QUEUE, NULL);
378 gst_object_ref_sink (ret);
379 ret->device = gst_object_ref (device);
380 ret->family = queue_family;
381 ret->index = queue_i;
383 vkGetDeviceQueue (device->device, queue_family, queue_i, &ret->queue);
389 * gst_vulkan_device_foreach_queue:
390 * @device: a #GstVulkanDevice
391 * @func: (scope call): a #GstVulkanDeviceForEachQueueFunc to run for each #GstVulkanQueue
392 * @user_data: (closure func): user data to pass to each call of @func
394 * Iterate over each queue family available on #GstVulkanDevice
399 gst_vulkan_device_foreach_queue (GstVulkanDevice * device,
400 GstVulkanDeviceForEachQueueFunc func, gpointer user_data)
402 GstVulkanDevicePrivate *priv = GET_PRIV (device);
403 gboolean done = FALSE;
406 for (i = 0; i < priv->n_queues; i++) {
407 GstVulkanQueue *queue =
408 gst_vulkan_device_get_queue (device, priv->queue_family_id, i);
410 if (!func (device, queue, user_data))
413 gst_object_unref (queue);
421 * gst_vulkan_device_get_proc_address:
422 * @device: a #GstVulkanDevice
423 * @name: name of the function to retrieve
425 * Performs `vkGetDeviceProcAddr()` with @device and @name
427 * Returns: the function pointer for @name or %NULL
432 gst_vulkan_device_get_proc_address (GstVulkanDevice * device,
435 g_return_val_if_fail (GST_IS_VULKAN_DEVICE (device), NULL);
436 g_return_val_if_fail (device->device != NULL, NULL);
437 g_return_val_if_fail (name != NULL, NULL);
439 GST_TRACE_OBJECT (device, "%s", name);
441 return vkGetDeviceProcAddr (device->device, name);
445 * gst_vulkan_device_get_instance:
446 * @device: a #GstVulkanDevice
448 * Returns: (transfer full): the #GstVulkanInstance used to create this @device
453 gst_vulkan_device_get_instance (GstVulkanDevice * device)
455 g_return_val_if_fail (GST_IS_VULKAN_DEVICE (device), NULL);
457 return device->instance ? gst_object_ref (device->instance) : NULL;
461 * gst_vulkan_device_get_physical_device: (skip)
462 * @device: a #GstVulkanDevice
464 * Returns: The VkPhysicalDevice used to create @device
469 gst_vulkan_device_get_physical_device (GstVulkanDevice * device)
471 g_return_val_if_fail (GST_IS_VULKAN_DEVICE (device), NULL);
473 return gst_vulkan_physical_device_get_handle (device->physical_device);
477 * gst_context_set_vulkan_device:
478 * @context: a #GstContext
479 * @device: a #GstVulkanDevice
481 * Sets @device on @context
486 gst_context_set_vulkan_device (GstContext * context, GstVulkanDevice * device)
490 g_return_if_fail (context != NULL);
491 g_return_if_fail (gst_context_is_writable (context));
494 GST_CAT_LOG (GST_CAT_CONTEXT,
495 "setting GstVulkanDevice(%" GST_PTR_FORMAT ") on context(%"
496 GST_PTR_FORMAT ")", device, context);
498 s = gst_context_writable_structure (context);
499 gst_structure_set (s, GST_VULKAN_DEVICE_CONTEXT_TYPE_STR,
500 GST_TYPE_VULKAN_DEVICE, device, NULL);
504 * gst_context_get_vulkan_device:
505 * @context: a #GstContext
506 * @device: resulting #GstVulkanDevice
508 * Returns: Whether @device was in @context
513 gst_context_get_vulkan_device (GstContext * context, GstVulkanDevice ** device)
515 const GstStructure *s;
518 g_return_val_if_fail (device != NULL, FALSE);
519 g_return_val_if_fail (context != NULL, FALSE);
521 s = gst_context_get_structure (context);
522 ret = gst_structure_get (s, GST_VULKAN_DEVICE_CONTEXT_TYPE_STR,
523 GST_TYPE_VULKAN_DEVICE, device, NULL);
525 GST_CAT_LOG (GST_CAT_CONTEXT, "got GstVulkanDevice(%" GST_PTR_FORMAT
526 ") from context(%" GST_PTR_FORMAT ")", *device, context);
532 * gst_vulkan_device_handle_context_query:
533 * @element: a #GstElement
534 * @query: a #GstQuery of type #GST_QUERY_CONTEXT
535 * @device: the #GstVulkanDevice
537 * If a #GstVulkanDevice is requested in @query, sets @device as the reply.
539 * Intended for use with element query handlers to respond to #GST_QUERY_CONTEXT
540 * for a #GstVulkanDevice.
542 * Returns: whether @query was responded to with @device
547 gst_vulkan_device_handle_context_query (GstElement * element, GstQuery * query,
548 GstVulkanDevice * device)
550 gboolean res = FALSE;
551 const gchar *context_type;
552 GstContext *context, *old_context;
554 g_return_val_if_fail (element != NULL, FALSE);
555 g_return_val_if_fail (query != NULL, FALSE);
556 g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONTEXT, FALSE);
561 gst_query_parse_context_type (query, &context_type);
563 if (g_strcmp0 (context_type, GST_VULKAN_DEVICE_CONTEXT_TYPE_STR) == 0) {
564 gst_query_parse_context (query, &old_context);
567 context = gst_context_copy (old_context);
569 context = gst_context_new (GST_VULKAN_DEVICE_CONTEXT_TYPE_STR, TRUE);
571 gst_context_set_vulkan_device (context, device);
572 gst_query_set_context (query, context);
573 gst_context_unref (context);
575 res = device != NULL;
582 * gst_vulkan_device_run_context_query:
583 * @element: a #GstElement
584 * @device: (inout): a #GstVulkanDevice
586 * Attempt to retrieve a #GstVulkanDevice using #GST_QUERY_CONTEXT from the
587 * surrounding elements of @element.
589 * Returns: whether @device contains a valid #GstVulkanDevice
594 gst_vulkan_device_run_context_query (GstElement * element,
595 GstVulkanDevice ** device)
599 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
600 g_return_val_if_fail (device != NULL, FALSE);
604 if (*device && GST_IS_VULKAN_DEVICE (*device))
608 gst_vulkan_local_context_query (element,
609 GST_VULKAN_DEVICE_CONTEXT_TYPE_STR))) {
612 gst_query_parse_context (query, &context);
614 gst_context_get_vulkan_device (context, device);
616 gst_query_unref (query);
619 GST_DEBUG_OBJECT (element, "found device %p", *device);
628 * gst_vulkan_device_create_fence:
629 * @device: a #GstVulkanDevice
630 * @error: a #GError to fill on failure
632 * Returns: a new #GstVulkanFence or %NULL
637 gst_vulkan_device_create_fence (GstVulkanDevice * device, GError ** error)
639 GstVulkanDevicePrivate *priv;
641 g_return_val_if_fail (GST_IS_VULKAN_DEVICE (device), NULL);
642 priv = GET_PRIV (device);
644 return gst_vulkan_fence_cache_acquire (priv->fence_cache, error);
647 /* reimplement a specfic case of g_ptr_array_find_with_equal_func as that
648 * requires Glib 2.54 */
650 ptr_array_find_string (GPtrArray * array, const gchar * str, guint * index)
654 for (i = 0; i < array->len; i++) {
655 gchar *val = (gchar *) g_ptr_array_index (array, i);
656 if (g_strcmp0 (val, str) == 0) {
667 gst_vulkan_device_is_extension_enabled_unlocked (GstVulkanDevice * device,
668 const gchar * name, guint * index)
670 GstVulkanDevicePrivate *priv = GET_PRIV (device);
672 return ptr_array_find_string (priv->enabled_extensions, name, index);
676 * gst_vulkan_device_is_extension_enabled:
677 * @device: a # GstVulkanDevice
678 * @name: extension name
680 * Returns: whether extension @name is enabled
685 gst_vulkan_device_is_extension_enabled (GstVulkanDevice * device,
690 g_return_val_if_fail (GST_IS_VULKAN_DEVICE (device), FALSE);
691 g_return_val_if_fail (name != NULL, FALSE);
693 GST_OBJECT_LOCK (device);
694 ret = gst_vulkan_device_is_extension_enabled_unlocked (device, name, NULL);
695 GST_OBJECT_UNLOCK (device);
701 gst_vulkan_device_enable_extension_unlocked (GstVulkanDevice * device,
704 GstVulkanDevicePrivate *priv = GET_PRIV (device);
706 if (gst_vulkan_device_is_extension_enabled_unlocked (device, name, NULL))
707 /* extension is already enabled */
710 if (!gst_vulkan_physical_device_get_extension_info (device->physical_device,
714 g_ptr_array_add (priv->enabled_extensions, g_strdup (name));
720 * gst_vulkan_device_enable_extension:
721 * @device: a #GstVulkanDevice
722 * @name: extension name to enable
724 * Enable an Vulkan extension by @name. Enabling an extension will
725 * only have an effect before the call to gst_vulkan_device_open().
727 * Returns: whether the Vulkan extension could be enabled.
732 gst_vulkan_device_enable_extension (GstVulkanDevice * device,
737 g_return_val_if_fail (GST_IS_VULKAN_DEVICE (device), FALSE);
738 g_return_val_if_fail (name != NULL, FALSE);
740 GST_OBJECT_LOCK (device);
741 ret = gst_vulkan_device_enable_extension_unlocked (device, name);
742 GST_OBJECT_UNLOCK (device);
748 gst_vulkan_device_disable_extension_unlocked (GstVulkanDevice * device,
751 GstVulkanDevicePrivate *priv = GET_PRIV (device);
754 if (!gst_vulkan_physical_device_get_extension_info (device->physical_device,
758 if (!gst_vulkan_device_is_extension_enabled_unlocked (device, name, &i))
759 /* extension is already disabled */
762 g_ptr_array_remove_index_fast (priv->enabled_extensions, i);
768 * gst_vulkan_device_disable_extension:
769 * @device: a #GstVulkanDevice
770 * @name: extension name to enable
772 * Disable an Vulkan extension by @name. Disabling an extension will only have
773 * an effect before the call to gst_vulkan_device_open().
775 * Returns: whether the Vulkan extension could be disabled.
780 gst_vulkan_device_disable_extension (GstVulkanDevice * device,
785 g_return_val_if_fail (GST_IS_VULKAN_DEVICE (device), FALSE);
786 g_return_val_if_fail (name != NULL, FALSE);
788 GST_OBJECT_LOCK (device);
789 ret = gst_vulkan_device_disable_extension_unlocked (device, name);
790 GST_OBJECT_UNLOCK (device);
796 gst_vulkan_device_is_layer_enabled_unlocked (GstVulkanDevice * device,
799 GstVulkanDevicePrivate *priv = GET_PRIV (device);
801 return ptr_array_find_string (priv->enabled_layers, name, NULL);
805 * gst_vulkan_device_is_layer_enabled:
806 * @device: a # GstVulkanDevice
809 * Returns: whether layer @name is enabled
814 gst_vulkan_device_is_layer_enabled (GstVulkanDevice * device,
819 g_return_val_if_fail (GST_IS_VULKAN_DEVICE (device), FALSE);
820 g_return_val_if_fail (name != NULL, FALSE);
822 GST_OBJECT_LOCK (device);
823 ret = gst_vulkan_device_is_layer_enabled_unlocked (device, name);
824 GST_OBJECT_UNLOCK (device);
830 gst_vulkan_device_enable_layer_unlocked (GstVulkanDevice * device,
833 GstVulkanDevicePrivate *priv = GET_PRIV (device);
835 if (gst_vulkan_device_is_layer_enabled_unlocked (device, name))
836 /* layer is already enabled */
839 if (!gst_vulkan_physical_device_get_layer_info (device->physical_device,
840 name, NULL, NULL, NULL))
843 g_ptr_array_add (priv->enabled_layers, g_strdup (name));
849 * gst_vulkan_device_enable_layer:
850 * @device: a #GstVulkanDevice
851 * @name: layer name to enable
853 * Enable an Vulkan layer by @name. Enabling a layer will
854 * only have an effect before the call to gst_vulkan_device_open().
856 * Returns: whether the Vulkan layer could be enabled.
861 gst_vulkan_device_enable_layer (GstVulkanDevice * device, const gchar * name)
865 g_return_val_if_fail (GST_IS_VULKAN_DEVICE (device), FALSE);
866 g_return_val_if_fail (name != NULL, FALSE);
868 GST_OBJECT_LOCK (device);
869 ret = gst_vulkan_device_enable_layer_unlocked (device, name);
870 GST_OBJECT_UNLOCK (device);