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 "gstvkinstance.h"
31 * @title: GstVulkanInstance
32 * @short_description: GStreamer Vulkan instance
33 * @see_also: #GstVulkanPhysicalDevice, #GstVulkanDevice, #GstVulkanDisplay
35 * #GstVulkanInstance encapsulates the necessary information for the toplevel
36 * Vulkan instance object.
38 * If GStreamer is built with debugging support, the default Vulkan API chosen
39 * can be selected with the environment variable
40 * `GST_VULKAN_INSTANCE_API_VERSION=1.0`. Any subsequent setting of the
41 * requested Vulkan API version through the available properties will override
42 * the environment variable.
45 #define APP_SHORT_NAME "GStreamer"
47 #define GST_CAT_DEFAULT gst_vulkan_instance_debug
48 GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
49 GST_DEBUG_CATEGORY (GST_VULKAN_DEBUG_CAT);
50 GST_DEBUG_CATEGORY_STATIC (GST_CAT_CONTEXT);
62 PROP_REQUESTED_API_MAJOR_VERSION,
63 PROP_REQUESTED_API_MINOR_VERSION,
66 #define DEFAULT_REQUESTED_API_VERSION_MAJOR 0
67 #define DEFAULT_REQUESTED_API_VERSION_MINOR 0
69 static guint gst_vulkan_instance_signals[LAST_SIGNAL] = { 0 };
71 static void gst_vulkan_instance_finalize (GObject * object);
73 struct _GstVulkanInstancePrivate
75 gboolean info_collected;
77 guint requested_api_major;
78 guint requested_api_minor;
79 uint32_t supported_instance_api;
81 guint n_available_layers;
82 VkLayerProperties *available_layers;
83 guint n_available_extensions;
84 VkExtensionProperties *available_extensions;
85 GPtrArray *enabled_layers;
86 GPtrArray *enabled_extensions;
88 #if !defined (GST_DISABLE_DEBUG)
89 VkDebugReportCallbackEXT msg_callback;
90 PFN_vkCreateDebugReportCallbackEXT dbgCreateDebugReportCallback;
91 PFN_vkDestroyDebugReportCallbackEXT dbgDestroyDebugReportCallback;
92 PFN_vkDebugReportMessageEXT dbgReportMessage;
99 static volatile gsize _init = 0;
101 if (g_once_init_enter (&_init)) {
102 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "vulkaninstance", 0,
104 GST_DEBUG_CATEGORY_INIT (GST_VULKAN_DEBUG_CAT, "vulkandebug", 0,
106 GST_DEBUG_CATEGORY_GET (GST_CAT_CONTEXT, "GST_CONTEXT");
107 g_once_init_leave (&_init, 1);
111 #define GET_PRIV(instance) gst_vulkan_instance_get_instance_private (instance)
113 #define gst_vulkan_instance_parent_class parent_class
114 G_DEFINE_TYPE_WITH_CODE (GstVulkanInstance, gst_vulkan_instance,
115 GST_TYPE_OBJECT, G_ADD_PRIVATE (GstVulkanInstance)
119 * gst_vulkan_instance_new:
121 * Returns: (transfer full): a new uninitialized #GstVulkanInstance
126 gst_vulkan_instance_new (void)
128 GstVulkanInstance *instance;
130 instance = g_object_new (GST_TYPE_VULKAN_INSTANCE, NULL);
131 gst_object_ref_sink (instance);
137 gst_vulkan_instance_set_property (GObject * object, guint prop_id,
138 const GValue * value, GParamSpec * pspec)
140 GstVulkanInstance *instance = GST_VULKAN_INSTANCE (object);
141 GstVulkanInstancePrivate *priv = GET_PRIV (instance);
143 GST_OBJECT_LOCK (instance);
145 case PROP_REQUESTED_API_MAJOR_VERSION:
147 g_warning ("Attempt to set the requested API version after the "
148 "instance has been opened");
149 priv->requested_api_major = g_value_get_uint (value);
151 case PROP_REQUESTED_API_MINOR_VERSION:
153 g_warning ("Attempt to set the requested API version after the "
154 "instance has been opened");
155 priv->requested_api_minor = g_value_get_uint (value);
158 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
161 GST_OBJECT_UNLOCK (instance);
165 gst_vulkan_instance_get_property (GObject * object,
166 guint prop_id, GValue * value, GParamSpec * pspec)
168 GstVulkanInstance *instance = GST_VULKAN_INSTANCE (object);
169 GstVulkanInstancePrivate *priv = GET_PRIV (instance);
171 GST_OBJECT_LOCK (instance);
173 case PROP_REQUESTED_API_MAJOR_VERSION:
174 g_value_set_uint (value, priv->requested_api_major);
176 case PROP_REQUESTED_API_MINOR_VERSION:
177 g_value_set_uint (value, priv->requested_api_minor);
180 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
183 GST_OBJECT_UNLOCK (instance);
187 gst_vulkan_instance_init (GstVulkanInstance * instance)
189 GstVulkanInstancePrivate *priv = GET_PRIV (instance);
191 priv->requested_api_major = DEFAULT_REQUESTED_API_VERSION_MAJOR;
192 priv->requested_api_minor = DEFAULT_REQUESTED_API_VERSION_MINOR;
194 priv->enabled_layers = g_ptr_array_new_with_free_func (g_free);
195 priv->enabled_extensions = g_ptr_array_new_with_free_func (g_free);
197 #if !defined (GST_DISABLE_DEBUG)
199 const gchar *api_override = g_getenv ("GST_VULKAN_INSTANCE_API_VERSION");
204 major = g_ascii_strtoll (api_override, &end, 10);
205 if (end && end[0] == '.') {
206 minor = g_ascii_strtoll (&end[1], NULL, 10);
207 if (major > 0 && major < G_MAXINT64 && minor >= 0 && minor < G_MAXINT64) {
208 priv->requested_api_major = major;
209 priv->requested_api_minor = minor;
218 gst_vulkan_instance_class_init (GstVulkanInstanceClass * klass)
220 GObjectClass *gobject_class = (GObjectClass *) klass;
222 gst_vulkan_memory_init_once ();
223 gst_vulkan_image_memory_init_once ();
224 gst_vulkan_buffer_memory_init_once ();
226 gobject_class->get_property = gst_vulkan_instance_get_property;
227 gobject_class->set_property = gst_vulkan_instance_set_property;
228 gobject_class->finalize = gst_vulkan_instance_finalize;
231 * GstVulkanInstance:requested-api-major:
235 g_object_class_install_property (gobject_class,
236 PROP_REQUESTED_API_MAJOR_VERSION,
237 g_param_spec_uint ("requested-api-major", "Requested API Major",
238 "Major version of the requested Vulkan API (0 = maximum supported)",
239 0, G_MAXUINT, DEFAULT_REQUESTED_API_VERSION_MAJOR,
240 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
243 * GstVulkanInstance:requested-api-minor:
247 g_object_class_install_property (gobject_class,
248 PROP_REQUESTED_API_MINOR_VERSION,
249 g_param_spec_uint ("requested-api-minor", "Requested API Minor",
250 "Minor version of the requested Vulkan API",
251 0, G_MAXUINT, DEFAULT_REQUESTED_API_VERSION_MINOR,
252 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
255 * GstVulkanInstance::create-device:
256 * @object: the #GstVulkanDisplay
258 * Overrides the #GstVulkanDevice creation mechanism.
259 * It can be called from any thread.
261 * Returns: (transfer full): the newly created #GstVulkanDevice.
265 gst_vulkan_instance_signals[SIGNAL_CREATE_DEVICE] =
266 g_signal_new ("create-device", G_TYPE_FROM_CLASS (klass),
267 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_VULKAN_DEVICE, 0);
271 gst_vulkan_instance_finalize (GObject * object)
273 GstVulkanInstance *instance = GST_VULKAN_INSTANCE (object);
274 GstVulkanInstancePrivate *priv = GET_PRIV (instance);
277 if (priv->dbgDestroyDebugReportCallback)
278 priv->dbgDestroyDebugReportCallback (instance->instance,
279 priv->msg_callback, NULL);
281 g_free (instance->physical_devices);
283 priv->opened = FALSE;
285 if (instance->instance)
286 vkDestroyInstance (instance->instance, NULL);
287 instance->instance = NULL;
289 g_free (priv->available_layers);
290 priv->available_layers = NULL;
292 g_free (priv->available_extensions);
293 priv->available_extensions = NULL;
295 g_ptr_array_unref (priv->enabled_layers);
296 priv->enabled_layers = NULL;
298 g_ptr_array_unref (priv->enabled_extensions);
299 priv->enabled_extensions = NULL;
301 G_OBJECT_CLASS (parent_class)->finalize (object);
304 VKAPI_ATTR static VkBool32
305 _gst_vk_debug_callback (VkDebugReportFlagsEXT msgFlags,
306 VkDebugReportObjectTypeEXT objType, uint64_t srcObject, size_t location,
307 int32_t msgCode, const char *pLayerPrefix, const char *pMsg,
310 if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
311 GST_CAT_ERROR (GST_VULKAN_DEBUG_CAT, "[%s] Code %d : %s", pLayerPrefix,
313 g_critical ("[%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
314 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
315 GST_CAT_WARNING (GST_VULKAN_DEBUG_CAT, "[%s] Code %d : %s", pLayerPrefix,
317 g_warning ("[%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
318 } else if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
319 GST_CAT_LOG (GST_VULKAN_DEBUG_CAT, "[%s] Code %d : %s", pLayerPrefix,
321 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
322 GST_CAT_FIXME (GST_VULKAN_DEBUG_CAT, "[%s] Code %d : %s", pLayerPrefix,
324 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
325 GST_CAT_TRACE (GST_VULKAN_DEBUG_CAT, "[%s] Code %d : %s", pLayerPrefix,
332 * false indicates that layer should not bail-out of an
333 * API call that had validation failures. This may mean that the
334 * app dies inside the driver due to invalid parameter(s).
335 * That's what would happen without validation layers, so we'll
336 * keep that behavior here.
342 gst_vulkan_instance_get_layer_info_unlocked (GstVulkanInstance * instance,
343 const gchar * name, gchar ** description, guint32 * spec_version,
344 guint32 * implementation_version)
346 GstVulkanInstancePrivate *priv;
349 priv = GET_PRIV (instance);
351 for (i = 0; i < priv->n_available_layers; i++) {
352 if (g_strcmp0 (name, priv->available_layers[i].layerName) == 0) {
354 *description = g_strdup (priv->available_layers[i].description);
356 *spec_version = priv->available_layers[i].specVersion;
357 if (implementation_version)
358 *spec_version = priv->available_layers[i].implementationVersion;
367 * gst_vulkan_instance_get_layer_info:
368 * @instance: a #GstVulkanInstance
369 * @name: the layer name to look for
370 * @description: (out) (nullable): return value for the layer description or %NULL
371 * @spec_version: (out) (nullable): return value for the layer specification version
372 * @implementation_version: (out) (nullable): return value for the layer implementation version
374 * Retrieves information about a layer.
376 * Will not find any layers before gst_vulkan_instance_fill_info() has been
379 * Returns: whether layer @name is available
384 gst_vulkan_instance_get_layer_info (GstVulkanInstance * instance,
385 const gchar * name, gchar ** description, guint32 * spec_version,
386 guint32 * implementation_version)
390 g_return_val_if_fail (GST_IS_VULKAN_INSTANCE (instance), FALSE);
391 g_return_val_if_fail (name != NULL, FALSE);
393 GST_OBJECT_LOCK (instance);
395 gst_vulkan_instance_get_layer_info_unlocked (instance, name, description,
396 spec_version, implementation_version);
397 GST_OBJECT_UNLOCK (instance);
402 G_GNUC_INTERNAL gboolean
403 gst_vulkan_instance_get_extension_info_unlocked (GstVulkanInstance * instance,
404 const gchar * name, guint32 * spec_version);
406 G_GNUC_INTERNAL gboolean
407 gst_vulkan_instance_get_extension_info_unlocked (GstVulkanInstance * instance,
408 const gchar * name, guint32 * spec_version)
410 GstVulkanInstancePrivate *priv;
413 priv = GET_PRIV (instance);
415 for (i = 0; i < priv->n_available_extensions; i++) {
416 if (g_strcmp0 (name, priv->available_extensions[i].extensionName) == 0) {
418 *spec_version = priv->available_extensions[i].specVersion;
427 * gst_vulkan_instance_get_extension_info:
428 * @instance: a #GstVulkanInstance
429 * @name: the layer name to look for
430 * @spec_version: (out) (nullable): return value for the layer specification version
432 * Retrieves information about an extension.
434 * Will not find any extensions before gst_vulkan_instance_fill_info() has been
437 * Returns: whether extension @name is available
442 gst_vulkan_instance_get_extension_info (GstVulkanInstance * instance,
443 const gchar * name, guint32 * spec_version)
447 g_return_val_if_fail (GST_IS_VULKAN_INSTANCE (instance), FALSE);
448 g_return_val_if_fail (name != NULL, FALSE);
450 GST_OBJECT_LOCK (instance);
452 gst_vulkan_instance_get_extension_info_unlocked (instance, name,
454 GST_OBJECT_UNLOCK (instance);
459 /* reimplement a specfic case of g_ptr_array_find_with_equal_func as that
460 * requires Glib 2.54 */
462 ptr_array_find_string (GPtrArray * array, const gchar * str, guint * index)
466 for (i = 0; i < array->len; i++) {
467 gchar *val = (gchar *) g_ptr_array_index (array, i);
468 if (g_strcmp0 (val, str) == 0) {
479 gst_vulkan_instance_is_extension_enabled_unlocked (GstVulkanInstance * instance,
480 const gchar * name, guint * index)
482 GstVulkanInstancePrivate *priv = GET_PRIV (instance);
484 return ptr_array_find_string (priv->enabled_extensions, name, index);
488 * gst_vulkan_instance_is_extension_enabled:
489 * @instance: a # GstVulkanInstance
490 * @name: extension name
492 * Returns: whether extension @name is enabled
497 gst_vulkan_instance_is_extension_enabled (GstVulkanInstance * instance,
502 g_return_val_if_fail (GST_IS_VULKAN_INSTANCE (instance), FALSE);
503 g_return_val_if_fail (name != NULL, FALSE);
505 GST_OBJECT_LOCK (instance);
507 gst_vulkan_instance_is_extension_enabled_unlocked (instance, name, NULL);
508 GST_OBJECT_UNLOCK (instance);
514 gst_vulkan_instance_enable_extension_unlocked (GstVulkanInstance * instance,
517 GstVulkanInstancePrivate *priv = GET_PRIV (instance);
518 gboolean extension_is_available = FALSE;
521 if (gst_vulkan_instance_is_extension_enabled_unlocked (instance, name, NULL))
522 /* extension is already enabled */
525 for (i = 0; i < priv->n_available_extensions; i++) {
526 if (g_strcmp0 (name, priv->available_extensions[i].extensionName) == 0) {
527 extension_is_available = TRUE;
532 if (!extension_is_available)
535 g_ptr_array_add (priv->enabled_extensions, g_strdup (name));
541 * gst_vulkan_instance_enable_extension:
542 * @instance: a #GstVulkanInstance
543 * @name: extension name to enable
545 * Enable an Vulkan extension by @name. Extensions cannot be enabled until
546 * gst_vulkan_instance_fill_info() has been called. Enabling an extension will
547 * only have an effect before the call to gst_vulkan_instance_open().
549 * Returns: whether the Vulkan extension could be enabled.
554 gst_vulkan_instance_enable_extension (GstVulkanInstance * instance,
559 g_return_val_if_fail (GST_IS_VULKAN_INSTANCE (instance), FALSE);
560 g_return_val_if_fail (name != NULL, FALSE);
562 GST_OBJECT_LOCK (instance);
563 ret = gst_vulkan_instance_enable_extension_unlocked (instance, name);
564 GST_OBJECT_UNLOCK (instance);
570 gst_vulkan_instance_disable_extension_unlocked (GstVulkanInstance * instance,
573 GstVulkanInstancePrivate *priv = GET_PRIV (instance);
574 gboolean extension_is_available = FALSE;
577 for (i = 0; i < priv->n_available_extensions; i++) {
578 if (g_strcmp0 (name, priv->available_extensions[i].extensionName) == 0) {
579 extension_is_available = TRUE;
584 if (!extension_is_available)
587 if (!gst_vulkan_instance_is_extension_enabled_unlocked (instance, name, &i))
588 /* extension is already enabled */
591 g_ptr_array_remove_index_fast (priv->enabled_extensions, i);
597 * gst_vulkan_instance_disable_extension:
598 * @instance: a #GstVulkanInstance
599 * @name: extension name to enable
601 * Disable an Vulkan extension by @name. Disabling an extension will only have
602 * an effect before the call to gst_vulkan_instance_open().
604 * Returns: whether the Vulkan extension could be disabled.
609 gst_vulkan_instance_disable_extension (GstVulkanInstance * instance,
614 g_return_val_if_fail (GST_IS_VULKAN_INSTANCE (instance), FALSE);
615 g_return_val_if_fail (name != NULL, FALSE);
617 GST_OBJECT_LOCK (instance);
618 ret = gst_vulkan_instance_disable_extension_unlocked (instance, name);
619 GST_OBJECT_UNLOCK (instance);
625 gst_vulkan_instance_is_layer_enabled_unlocked (GstVulkanInstance * instance,
628 GstVulkanInstancePrivate *priv = GET_PRIV (instance);
630 return ptr_array_find_string (priv->enabled_layers, name, NULL);
634 * gst_vulkan_instance_is_layer_enabled:
635 * @instance: a # GstVulkanInstance
638 * Returns: whether layer @name is enabled
643 gst_vulkan_instance_is_layer_enabled (GstVulkanInstance * instance,
648 g_return_val_if_fail (GST_IS_VULKAN_INSTANCE (instance), FALSE);
649 g_return_val_if_fail (name != NULL, FALSE);
651 GST_OBJECT_LOCK (instance);
652 ret = gst_vulkan_instance_is_layer_enabled_unlocked (instance, name);
653 GST_OBJECT_UNLOCK (instance);
659 gst_vulkan_instance_enable_layer_unlocked (GstVulkanInstance * instance,
662 GstVulkanInstancePrivate *priv = GET_PRIV (instance);
663 gboolean layer_is_available = FALSE;
666 if (gst_vulkan_instance_is_layer_enabled_unlocked (instance, name))
667 /* layer is already enabled */
670 for (i = 0; i < priv->n_available_layers; i++) {
671 if (g_strcmp0 (name, priv->available_layers[i].layerName) == 0) {
672 layer_is_available = TRUE;
677 if (!layer_is_available)
680 g_ptr_array_add (priv->enabled_layers, g_strdup (name));
686 * gst_vulkan_instance_enable_layer:
687 * @instance: a #GstVulkanInstance
688 * @name: layer name to enable
690 * Enable an Vulkan layer by @name. Layer cannot be enabled until
691 * gst_vulkan_instance_fill_info() has been called. Enabling a layer will
692 * only have an effect before the call to gst_vulkan_instance_open().
694 * Returns: whether the Vulkan layer could be enabled.
699 gst_vulkan_instance_enable_layer (GstVulkanInstance * instance,
704 g_return_val_if_fail (GST_IS_VULKAN_INSTANCE (instance), FALSE);
705 g_return_val_if_fail (name != NULL, FALSE);
707 GST_OBJECT_LOCK (instance);
708 ret = gst_vulkan_instance_enable_layer_unlocked (instance, name);
709 GST_OBJECT_UNLOCK (instance);
715 gst_vulkan_get_supported_api_version_unlocked (GstVulkanInstance * instance)
717 GstVulkanInstancePrivate *priv = GET_PRIV (instance);
718 PFN_vkEnumerateInstanceVersion gst_vkEnumerateInstanceVersion;
720 if (priv->supported_instance_api)
723 gst_vkEnumerateInstanceVersion =
724 (PFN_vkEnumerateInstanceVersion) vkGetInstanceProcAddr (NULL,
725 "vkEnumerateInstanceVersion");
727 if (!gst_vkEnumerateInstanceVersion
729 gst_vkEnumerateInstanceVersion (&priv->supported_instance_api)) {
730 priv->supported_instance_api = VK_MAKE_VERSION (1, 0, 0);
734 G_GNUC_INTERNAL GstVulkanDisplayType
735 gst_vulkan_display_choose_type_unlocked (GstVulkanInstance * instance);
738 gst_vulkan_instance_fill_info_unlocked (GstVulkanInstance * instance,
741 GstVulkanInstancePrivate *priv;
745 priv = GET_PRIV (instance);
747 if (priv->info_collected)
749 priv->info_collected = TRUE;
751 gst_vulkan_get_supported_api_version_unlocked (instance);
753 /* Look for validation layers */
754 err = vkEnumerateInstanceLayerProperties (&priv->n_available_layers, NULL);
755 if (gst_vulkan_error_to_g_error (err, error,
756 "vKEnumerateInstanceLayerProperties") < 0) {
760 priv->available_layers = g_new0 (VkLayerProperties, priv->n_available_layers);
762 vkEnumerateInstanceLayerProperties (&priv->n_available_layers,
763 priv->available_layers);
764 if (gst_vulkan_error_to_g_error (err, error,
765 "vKEnumerateInstanceLayerProperties") < 0) {
770 vkEnumerateInstanceExtensionProperties (NULL,
771 &priv->n_available_extensions, NULL);
772 if (gst_vulkan_error_to_g_error (err, error,
773 "vkEnumerateInstanceExtensionProperties") < 0) {
777 priv->available_extensions =
778 g_new0 (VkExtensionProperties, priv->n_available_extensions);
780 vkEnumerateInstanceExtensionProperties (NULL,
781 &priv->n_available_extensions, priv->available_extensions);
782 if (gst_vulkan_error_to_g_error (err, error,
783 "vkEnumerateInstanceExtensionProperties") < 0) {
787 GST_INFO_OBJECT (instance, "found %u layers and %u extensions",
788 priv->n_available_layers, priv->n_available_extensions);
790 for (i = 0; i < priv->n_available_layers; i++)
791 GST_DEBUG_OBJECT (instance, "available layer %u: %s", i,
792 priv->available_layers[i].layerName);
793 for (i = 0; i < priv->n_available_extensions; i++)
794 GST_DEBUG_OBJECT (instance, "available extension %u: %s", i,
795 priv->available_extensions[i].extensionName);
797 /* configure default extensions */
799 GstVulkanDisplayType display_type;
800 const gchar *winsys_ext_name;
801 GstDebugLevel vulkan_debug_level;
803 display_type = gst_vulkan_display_choose_type_unlocked (instance);
806 gst_vulkan_display_type_to_extension_string (display_type);
807 if (!winsys_ext_name) {
808 GST_WARNING_OBJECT (instance, "No window system extension enabled");
809 } else if (gst_vulkan_instance_get_extension_info_unlocked (instance,
810 VK_KHR_SURFACE_EXTENSION_NAME, NULL)
811 && gst_vulkan_instance_get_extension_info_unlocked (instance,
812 winsys_ext_name, NULL)) {
813 gst_vulkan_instance_enable_extension_unlocked (instance,
814 VK_KHR_SURFACE_EXTENSION_NAME);
815 gst_vulkan_instance_enable_extension_unlocked (instance, winsys_ext_name);
817 #if !defined (GST_DISABLE_DEBUG)
819 gst_debug_category_get_threshold (GST_VULKAN_DEBUG_CAT);
821 if (vulkan_debug_level >= GST_LEVEL_ERROR) {
822 if (gst_vulkan_instance_get_extension_info_unlocked (instance,
823 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, NULL)) {
824 gst_vulkan_instance_enable_extension_unlocked (instance,
825 VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
835 * gst_vulkan_instance_fill_info:
836 * @instance: a #GstVulkanInstance
839 * Retrieve as much information about the available Vulkan instance without
840 * actually creating an Vulkan instance. Will not do anything while @instance
843 * Returns: whether the instance information could be retrieved
848 gst_vulkan_instance_fill_info (GstVulkanInstance * instance, GError ** error)
852 g_return_val_if_fail (GST_IS_VULKAN_INSTANCE (instance), FALSE);
854 GST_OBJECT_LOCK (instance);
855 ret = gst_vulkan_instance_fill_info_unlocked (instance, error);
856 GST_OBJECT_UNLOCK (instance);
862 * gst_vulkan_instance_open:
863 * @instance: a #GstVulkanInstance
866 * Returns: whether the instance could be created
871 gst_vulkan_instance_open (GstVulkanInstance * instance, GError ** error)
873 GstVulkanInstancePrivate *priv;
874 uint32_t requested_instance_api;
875 GstDebugLevel vulkan_debug_level;
878 g_return_val_if_fail (GST_IS_VULKAN_INSTANCE (instance), FALSE);
880 priv = GET_PRIV (instance);
882 GST_OBJECT_LOCK (instance);
884 GST_OBJECT_UNLOCK (instance);
888 if (!gst_vulkan_instance_fill_info_unlocked (instance, error))
891 if (priv->requested_api_major) {
892 requested_instance_api =
893 VK_MAKE_VERSION (priv->requested_api_major, priv->requested_api_minor,
896 requested_instance_api = priv->supported_instance_api;
899 if (requested_instance_api > priv->supported_instance_api) {
900 g_set_error (error, GST_VULKAN_ERROR, VK_ERROR_INITIALIZATION_FAILED,
901 "Requested API version (%u.%u) is larger than the maximum supported "
902 "version (%u.%u)", VK_VERSION_MAJOR (requested_instance_api),
903 VK_VERSION_MINOR (requested_instance_api),
904 VK_VERSION_MAJOR (priv->supported_instance_api),
905 VK_VERSION_MINOR (priv->supported_instance_api));
909 /* list of known vulkan loader environment variables taken from:
910 * https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md#table-of-debug-environment-variables */
911 GST_DEBUG_OBJECT (instance, "VK_ICD_FILENAMES: %s",
912 g_getenv ("VK_ICD_FILENAMES"));
913 GST_DEBUG_OBJECT (instance, "VK_INSTANCE_LAYERS: %s",
914 g_getenv ("VK_INSTANCE_LAYERS"));
915 GST_DEBUG_OBJECT (instance, "VK_LAYER_PATH: %s", g_getenv ("VK_LAYER_PATH"));
916 GST_DEBUG_OBJECT (instance, "VK_LOADER_DISABLE_INST_EXT_FILTER: %s",
917 g_getenv ("VK_LOADER_DISABLE_INST_EXT_FILTER"));
918 GST_DEBUG_OBJECT (instance, "VK_LOADER_DEBUG: %s",
919 g_getenv ("VK_LOADER_DEBUG"));
924 GST_INFO_OBJECT (instance, "attempting to create instance for Vulkan API "
925 "%u.%u, max supported %u.%u with %u layers and %u extensions",
926 VK_VERSION_MAJOR (requested_instance_api),
927 VK_VERSION_MINOR (requested_instance_api),
928 VK_VERSION_MAJOR (priv->supported_instance_api),
929 VK_VERSION_MINOR (priv->supported_instance_api),
930 priv->enabled_layers->len, priv->enabled_extensions->len);
932 for (i = 0; i < priv->enabled_layers->len; i++)
933 GST_DEBUG_OBJECT (instance, "layer %u: %s", i,
934 (gchar *) g_ptr_array_index (priv->enabled_layers, i));
935 for (i = 0; i < priv->enabled_extensions->len; i++)
936 GST_DEBUG_OBJECT (instance, "extension %u: %s", i,
937 (gchar *) g_ptr_array_index (priv->enabled_extensions, i));
941 VkApplicationInfo app = { 0, };
942 VkInstanceCreateInfo inst_info = { 0, };
945 app = (VkApplicationInfo) {
946 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
948 .pApplicationName = APP_SHORT_NAME,
949 .applicationVersion = 0,
950 .pEngineName = APP_SHORT_NAME,
952 .apiVersion = requested_instance_api,
955 inst_info = (VkInstanceCreateInfo) {
956 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
958 .pApplicationInfo = &app,
959 .enabledLayerCount = priv->enabled_layers->len,
960 .ppEnabledLayerNames = (const char *const *) priv->enabled_layers->pdata,
961 .enabledExtensionCount = priv->enabled_extensions->len,
962 .ppEnabledExtensionNames = (const char *const *) priv->enabled_extensions->pdata,
966 err = vkCreateInstance (&inst_info, NULL, &instance->instance);
967 if (gst_vulkan_error_to_g_error (err, error, "vkCreateInstance") < 0) {
973 vkEnumeratePhysicalDevices (instance->instance,
974 &instance->n_physical_devices, NULL);
975 if (gst_vulkan_error_to_g_error (err, error,
976 "vkEnumeratePhysicalDevices") < 0)
978 g_assert (instance->n_physical_devices > 0);
979 instance->physical_devices =
980 g_new0 (VkPhysicalDevice, instance->n_physical_devices);
982 vkEnumeratePhysicalDevices (instance->instance,
983 &instance->n_physical_devices, instance->physical_devices);
984 if (gst_vulkan_error_to_g_error (err, error,
985 "vkEnumeratePhysicalDevices") < 0)
988 #if !defined (GST_DISABLE_DEBUG)
989 vulkan_debug_level = gst_debug_category_get_threshold (GST_VULKAN_DEBUG_CAT);
991 if (vulkan_debug_level >= GST_LEVEL_ERROR
992 && gst_vulkan_instance_is_extension_enabled_unlocked (instance,
993 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, NULL)) {
994 VkDebugReportCallbackCreateInfoEXT info = { 0, };
996 priv->dbgCreateDebugReportCallback = (PFN_vkCreateDebugReportCallbackEXT)
997 gst_vulkan_instance_get_proc_address (instance,
998 "vkCreateDebugReportCallbackEXT");
999 if (!priv->dbgCreateDebugReportCallback) {
1000 g_set_error (error, GST_VULKAN_ERROR, VK_ERROR_INITIALIZATION_FAILED,
1001 "Failed to retrieve vkCreateDebugReportCallback");
1004 priv->dbgDestroyDebugReportCallback = (PFN_vkDestroyDebugReportCallbackEXT)
1005 gst_vulkan_instance_get_proc_address (instance,
1006 "vkDestroyDebugReportCallbackEXT");
1007 if (!priv->dbgDestroyDebugReportCallback) {
1008 g_set_error (error, GST_VULKAN_ERROR, VK_ERROR_INITIALIZATION_FAILED,
1009 "Failed to retrieve vkDestroyDebugReportCallback");
1012 priv->dbgReportMessage = (PFN_vkDebugReportMessageEXT)
1013 gst_vulkan_instance_get_proc_address (instance,
1014 "vkDebugReportMessageEXT");
1015 if (!priv->dbgReportMessage) {
1016 g_set_error (error, GST_VULKAN_ERROR, VK_ERROR_INITIALIZATION_FAILED,
1017 "Failed to retrieve vkDebugReportMessage");
1021 info.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
1024 info.pfnCallback = (PFN_vkDebugReportCallbackEXT) _gst_vk_debug_callback;
1025 info.pUserData = NULL;
1026 /* matches the conditions in _gst_vk_debug_callback() */
1027 if (vulkan_debug_level >= GST_LEVEL_ERROR)
1028 info.flags |= VK_DEBUG_REPORT_ERROR_BIT_EXT;
1029 if (vulkan_debug_level >= GST_LEVEL_WARNING)
1030 info.flags |= VK_DEBUG_REPORT_WARNING_BIT_EXT;
1031 if (vulkan_debug_level >= GST_LEVEL_FIXME)
1032 info.flags |= VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
1033 if (vulkan_debug_level >= GST_LEVEL_LOG)
1034 info.flags |= VK_DEBUG_REPORT_INFORMATION_BIT_EXT;
1035 if (vulkan_debug_level >= GST_LEVEL_TRACE)
1036 info.flags |= VK_DEBUG_REPORT_DEBUG_BIT_EXT;
1039 priv->dbgCreateDebugReportCallback (instance->instance, &info, NULL,
1040 &priv->msg_callback);
1041 if (gst_vulkan_error_to_g_error (err, error,
1042 "vkCreateDebugReportCallback") < 0)
1047 priv->opened = TRUE;
1048 GST_OBJECT_UNLOCK (instance);
1054 GST_OBJECT_UNLOCK (instance);
1060 * gst_vulkan_instance_get_proc_address:
1061 * @instance: a #GstVulkanInstance
1062 * @name: name of the function to retrieve
1064 * Performs `vkGetInstanceProcAddr()` with @instance and @name
1066 * Returns: the function pointer for @name or %NULL
1071 gst_vulkan_instance_get_proc_address (GstVulkanInstance * instance,
1076 g_return_val_if_fail (GST_IS_VULKAN_INSTANCE (instance), NULL);
1077 g_return_val_if_fail (instance->instance != NULL, NULL);
1078 g_return_val_if_fail (name != NULL, NULL);
1080 ret = vkGetInstanceProcAddr (instance->instance, name);
1082 GST_TRACE_OBJECT (instance, "%s = %p", name, ret);
1088 * gst_vulkan_instance_create_device:
1089 * @instance: a #GstVulkanInstance
1091 * Returns: (transfer full): a new #GstVulkanDevice
1096 gst_vulkan_instance_create_device (GstVulkanInstance * instance,
1099 GstVulkanDevice *device;
1101 g_return_val_if_fail (GST_IS_VULKAN_INSTANCE (instance), NULL);
1103 g_signal_emit (instance, gst_vulkan_instance_signals[SIGNAL_CREATE_DEVICE], 0,
1107 device = gst_vulkan_device_new_with_index (instance, 0);
1110 if (!gst_vulkan_device_open (device, error)) {
1111 gst_object_unref (device);
1119 * gst_context_set_vulkan_instance:
1120 * @context: a #GstContext
1121 * @instance: a #GstVulkanInstance
1123 * Sets @instance on @context
1128 gst_context_set_vulkan_instance (GstContext * context,
1129 GstVulkanInstance * instance)
1133 g_return_if_fail (context != NULL);
1134 g_return_if_fail (gst_context_is_writable (context));
1137 GST_CAT_LOG (GST_CAT_CONTEXT,
1138 "setting GstVulkanInstance(%" GST_PTR_FORMAT ") on context(%"
1139 GST_PTR_FORMAT ")", instance, context);
1141 s = gst_context_writable_structure (context);
1142 gst_structure_set (s, GST_VULKAN_INSTANCE_CONTEXT_TYPE_STR,
1143 GST_TYPE_VULKAN_INSTANCE, instance, NULL);
1147 * gst_context_get_vulkan_instance:
1148 * @context: a #GstContext
1149 * @instance: resulting #GstVulkanInstance
1151 * Returns: Whether @instance was in @context
1156 gst_context_get_vulkan_instance (GstContext * context,
1157 GstVulkanInstance ** instance)
1159 const GstStructure *s;
1162 g_return_val_if_fail (instance != NULL, FALSE);
1163 g_return_val_if_fail (context != NULL, FALSE);
1165 s = gst_context_get_structure (context);
1166 ret = gst_structure_get (s, GST_VULKAN_INSTANCE_CONTEXT_TYPE_STR,
1167 GST_TYPE_VULKAN_INSTANCE, instance, NULL);
1169 GST_CAT_LOG (GST_CAT_CONTEXT, "got GstVulkanInstance(%" GST_PTR_FORMAT
1170 ") from context(%" GST_PTR_FORMAT ")", *instance, context);
1176 * gst_vulkan_instance_handle_context_query:
1177 * @element: a #GstElement
1178 * @query: a #GstQuery of type #GST_QUERY_CONTEXT
1179 * @instance: (nullable): the #GstVulkanInstance
1181 * If a #GstVulkanInstance is requested in @query, sets @instance as the reply.
1183 * Intended for use with element query handlers to respond to #GST_QUERY_CONTEXT
1184 * for a #GstVulkanInstance.
1186 * Returns: whether @query was responded to with @instance
1191 gst_vulkan_instance_handle_context_query (GstElement * element,
1192 GstQuery * query, GstVulkanInstance * instance)
1194 gboolean res = FALSE;
1195 const gchar *context_type;
1196 GstContext *context, *old_context;
1198 g_return_val_if_fail (element != NULL, FALSE);
1199 g_return_val_if_fail (query != NULL, FALSE);
1200 g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONTEXT, FALSE);
1205 gst_query_parse_context_type (query, &context_type);
1207 if (g_strcmp0 (context_type, GST_VULKAN_INSTANCE_CONTEXT_TYPE_STR) == 0) {
1208 gst_query_parse_context (query, &old_context);
1211 context = gst_context_copy (old_context);
1213 context = gst_context_new (GST_VULKAN_INSTANCE_CONTEXT_TYPE_STR, TRUE);
1215 gst_context_set_vulkan_instance (context, instance);
1216 gst_query_set_context (query, context);
1217 gst_context_unref (context);
1219 res = instance != NULL;
1226 * gst_vulkan_instance_run_context_query:
1227 * @element: a #GstElement
1228 * @instance: (inout): a #GstVulkanInstance
1230 * Attempt to retrieve a #GstVulkanInstance using #GST_QUERY_CONTEXT from the
1231 * surrounding elements of @element.
1233 * Returns: whether @instance contains a valid #GstVulkanInstance
1238 gst_vulkan_instance_run_context_query (GstElement * element,
1239 GstVulkanInstance ** instance)
1241 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1242 g_return_val_if_fail (instance != NULL, FALSE);
1246 if (*instance && GST_IS_VULKAN_INSTANCE (*instance))
1249 gst_vulkan_global_context_query (element,
1250 GST_VULKAN_INSTANCE_CONTEXT_TYPE_STR);
1252 GST_DEBUG_OBJECT (element, "found instance %p", *instance);
1261 * gst_vulkan_instance_check_version:
1262 * @instance: a #GstVulkanInstance
1263 * @major: major version
1264 * @minor: minor version
1265 * @patch: patch version
1267 * Check if the configured vulkan instance supports the specified version.
1268 * Will not work prior to opening the instance with gst_vulkan_instance_open().
1269 * If a specific version is requested, the @patch level is ignored.
1271 * Returns: whether @instance is at least the requested version.
1276 gst_vulkan_instance_check_version (GstVulkanInstance * instance,
1277 guint major, guint minor, guint patch)
1279 GstVulkanInstancePrivate *priv;
1281 g_return_val_if_fail (GST_IS_VULKAN_INSTANCE (instance), FALSE);
1283 priv = GET_PRIV (instance);
1285 return (priv->requested_api_major == 0
1286 && VK_MAKE_VERSION (major, minor, patch) <= priv->supported_instance_api)
1287 || (priv->requested_api_major >= 0 && (major < priv->requested_api_major
1288 || (major == priv->requested_api_major
1289 && minor <= priv->requested_api_minor)));
1293 * gst_vulkan_instance_get_version:
1294 * @instance: a #GstVulkanInstance
1295 * @major: major version
1296 * @minor: minor version
1297 * @patch: patch version
1299 * Retrieve the vulkan instance configured version. Only returns the supported
1300 * API version by the instance without taking into account the requested API
1301 * version. This means gst_vulkan_instance_check_version() will return
1302 * different values if a specific version has been requested (which is the
1303 * default) than a version check that is performed manually by retrieving the
1304 * version with this function.
1309 gst_vulkan_instance_get_version (GstVulkanInstance * instance,
1310 guint * major, guint * minor, guint * patch)
1312 GstVulkanInstancePrivate *priv;
1314 g_return_if_fail (GST_IS_VULKAN_INSTANCE (instance));
1316 priv = GET_PRIV (instance);
1318 GST_OBJECT_LOCK (instance);
1319 if (!priv->supported_instance_api)
1320 gst_vulkan_get_supported_api_version_unlocked (instance);
1323 *major = VK_VERSION_MAJOR (priv->supported_instance_api);
1325 *minor = VK_VERSION_MINOR (priv->supported_instance_api);
1327 *patch = VK_VERSION_PATCH (priv->supported_instance_api);
1328 GST_OBJECT_UNLOCK (instance);