1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
|
/* GDK - The GIMP Drawing Kit
*
* gdkvulkancontext-x11.c: X11 specific Vulkan wrappers
*
* Copyright © 2016 Benjamin Otte
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "gdkvulkancontextprivate.h"
#include <vulkan/vulkan.h>
#include "gdkdisplayprivate.h"
#include "gdkinternals.h"
#include "gdkintl.h"
typedef struct _GdkVulkanContextPrivate GdkVulkanContextPrivate;
struct _GdkVulkanContextPrivate {
VkSurfaceKHR surface;
VkSurfaceFormatKHR image_format;
int swapchain_width, swapchain_height;
VkSwapchainKHR swapchain;
};
G_DEFINE_QUARK (gdk-vulkan-error-quark, gdk_vulkan_error)
static void gdk_vulkan_context_initable_init (GInitableIface *iface);
G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GdkVulkanContext, gdk_vulkan_context, GDK_TYPE_DRAW_CONTEXT,
G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, gdk_vulkan_context_initable_init)
G_ADD_PRIVATE (GdkVulkanContext))
#ifdef GDK_WINDOWING_VULKAN
const char *
gdk_vulkan_strerror (VkResult result)
{
switch (result)
{
case VK_SUCCESS:
return "Command successfully completed.";
case VK_NOT_READY:
return "A fence or query has not yet completed.";
case VK_TIMEOUT:
return "A wait operation has not completed in the specified time.";
case VK_EVENT_SET:
return "An event is signaled.";
case VK_EVENT_RESET:
return "An event is unsignaled.";
case VK_INCOMPLETE:
return "A return array was too small for the result.";
case VK_SUBOPTIMAL_KHR:
return "A swapchain no longer matches the surface properties exactly, but can still be used to present to the surface successfully.";
case VK_ERROR_OUT_OF_HOST_MEMORY:
return "A host memory allocation has failed.";
case VK_ERROR_OUT_OF_DEVICE_MEMORY:
return "A device memory allocation has failed.";
case VK_ERROR_INITIALIZATION_FAILED:
return "Initialization of an object could not be completed for implementation-specific reasons.";
case VK_ERROR_DEVICE_LOST:
return "The logical or physical device has been lost.";
case VK_ERROR_MEMORY_MAP_FAILED:
return "Mapping of a memory object has failed.";
case VK_ERROR_LAYER_NOT_PRESENT:
return "A requested layer is not present or could not be loaded.";
case VK_ERROR_EXTENSION_NOT_PRESENT:
return "A requested extension is not supported.";
case VK_ERROR_FEATURE_NOT_PRESENT:
return "A requested feature is not supported.";
case VK_ERROR_INCOMPATIBLE_DRIVER:
return "The requested version of Vulkan is not supported by the driver or is otherwise incompatible for implementation-specific reasons.";
case VK_ERROR_TOO_MANY_OBJECTS:
return "Too many objects of the type have already been created.";
case VK_ERROR_FORMAT_NOT_SUPPORTED:
return "A requested format is not supported on this device.";
case VK_ERROR_FRAGMENTED_POOL:
return "A requested pool allocation has failed due to fragmentation of the pool’s memory.";
case VK_ERROR_SURFACE_LOST_KHR:
return "A surface is no longer available.";
case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR:
return "The requested window is already in use by Vulkan or another API in a manner which prevents it from being used again.";
case VK_ERROR_OUT_OF_DATE_KHR:
return "A surface has changed in such a way that it is no longer compatible with the swapchain.";
case VK_ERROR_INCOMPATIBLE_DISPLAY_KHR:
return "The display used by a swapchain does not use the same presentable image layout, or is incompatible in a way that prevents sharing an image.";
default:
return "Unknown Vulkan error.";
}
}
static void
gdk_vulkan_context_dispose (GObject *gobject)
{
GdkVulkanContext *context = GDK_VULKAN_CONTEXT (gobject);
GdkVulkanContextPrivate *priv = gdk_vulkan_context_get_instance_private (context);
GdkDisplay *display;
if (priv->swapchain != VK_NULL_HANDLE)
{
vkDestroySwapchainKHR (gdk_vulkan_context_get_device (context),
priv->swapchain,
NULL);
priv->swapchain = VK_NULL_HANDLE;
}
if (priv->surface != VK_NULL_HANDLE)
{
vkDestroySurfaceKHR (gdk_vulkan_context_get_instance (context),
priv->surface,
NULL);
priv->surface = VK_NULL_HANDLE;
}
/* display will be unset in gdk_draw_context_dispose() */
display = gdk_draw_context_get_display (GDK_DRAW_CONTEXT (context));
if (display)
gdk_display_unref_vulkan (display);
G_OBJECT_CLASS (gdk_vulkan_context_parent_class)->dispose (gobject);
}
static void
gdk_vulkan_context_class_init (GdkVulkanContextClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->dispose = gdk_vulkan_context_dispose;
}
static void
gdk_vulkan_context_init (GdkVulkanContext *self)
{
}
static gboolean
gdk_vulkan_context_check_swapchain (GdkVulkanContext *context,
GError **error)
{
GdkVulkanContextPrivate *priv = gdk_vulkan_context_get_instance_private (context);
GdkWindow *window = gdk_draw_context_get_window (GDK_DRAW_CONTEXT (context));
VkSurfaceCapabilitiesKHR capabilities;
VkCompositeAlphaFlagBitsKHR composite_alpha;
VkSwapchainKHR new_swapchain;
VkResult res;
if (gdk_window_get_width (window) == priv->swapchain_width &&
gdk_window_get_height (window) == priv->swapchain_height)
return TRUE;
res = GDK_VK_CHECK (vkGetPhysicalDeviceSurfaceCapabilitiesKHR, gdk_vulkan_context_get_physical_device (context),
priv->surface,
&capabilities);
if (res != VK_SUCCESS)
{
g_set_error (error, GDK_VULKAN_ERROR, GDK_VULKAN_ERROR_NOT_AVAILABLE,
"Could not query surface capabilities: %s", gdk_vulkan_strerror (res));
return FALSE;
}
if (capabilities.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR)
composite_alpha = VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
else if (capabilities.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR)
composite_alpha = VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR;
else if (capabilities.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR)
{
/* let's hope the backend knows what it's doing */
composite_alpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
}
else
{
GDK_NOTE (VULKAN, g_warning ("Vulkan swapchain doesn't do transparency. Using opaque swapchain instead."));
composite_alpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
}
res = GDK_VK_CHECK (vkCreateSwapchainKHR, gdk_vulkan_context_get_device (context),
&(VkSwapchainCreateInfoKHR) {
.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
.pNext = NULL,
.flags = 0,
.surface = priv->surface,
.minImageCount = 2,
.imageFormat = priv->image_format.format,
.imageColorSpace = priv->image_format.colorSpace,
.imageExtent = capabilities.currentExtent,
.imageArrayLayers = 1,
.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
.queueFamilyIndexCount = 1,
.pQueueFamilyIndices = (uint32_t[1]) {
gdk_draw_context_get_display (GDK_DRAW_CONTEXT (context))->vk_queue_family_index
},
.preTransform = capabilities.currentTransform,
.compositeAlpha = composite_alpha,
.presentMode = VK_PRESENT_MODE_FIFO_KHR,
.clipped = VK_FALSE,
.oldSwapchain = priv->swapchain
},
NULL,
&new_swapchain);
if (priv->swapchain != VK_NULL_HANDLE)
vkDestroySwapchainKHR (gdk_vulkan_context_get_device (context),
priv->swapchain,
NULL);
if (res == VK_SUCCESS)
{
priv->swapchain_width = gdk_window_get_width (window);
priv->swapchain_height = gdk_window_get_height (window);
priv->swapchain = new_swapchain;
}
else
{
g_set_error (error, GDK_VULKAN_ERROR, GDK_VULKAN_ERROR_NOT_AVAILABLE,
"Could not create swapchain for this window: %s", gdk_vulkan_strerror (res));
priv->swapchain = VK_NULL_HANDLE;
priv->swapchain_width = 0;
priv->swapchain_height = 0;
return FALSE;
}
return TRUE;
}
static gboolean
gdk_vulkan_context_real_init (GInitable *initable,
GCancellable *cancellable,
GError **error)
{
GdkVulkanContext *context = GDK_VULKAN_CONTEXT (initable);
GdkVulkanContextPrivate *priv = gdk_vulkan_context_get_instance_private (context);
VkResult res;
VkBool32 supported;
uint32_t i;
if (!gdk_display_ref_vulkan (gdk_draw_context_get_display (GDK_DRAW_CONTEXT (context)), error))
return FALSE;
res = GDK_VULKAN_CONTEXT_GET_CLASS (context)->create_surface (context, &priv->surface);
if (res != VK_SUCCESS)
{
g_set_error (error, GDK_VULKAN_ERROR, GDK_VULKAN_ERROR_NOT_AVAILABLE,
"Could not create surface for this window: %s", gdk_vulkan_strerror (res));
return FALSE;
}
res = GDK_VK_CHECK (vkGetPhysicalDeviceSurfaceSupportKHR, gdk_vulkan_context_get_physical_device (context),
gdk_vulkan_context_get_queue_family_index (context),
priv->surface,
&supported);
if (res != VK_SUCCESS)
{
g_set_error (error, GDK_VULKAN_ERROR, GDK_VULKAN_ERROR_NOT_AVAILABLE,
"Could not check if queue family supports this window: %s", gdk_vulkan_strerror (res));
}
else if (!supported)
{
g_set_error (error, GDK_VULKAN_ERROR, GDK_VULKAN_ERROR_NOT_AVAILABLE,
"FIXME: Queue family does not support surface. Write code to try different queue family.");
}
else
{
uint32_t n_formats;
GDK_VK_CHECK (vkGetPhysicalDeviceSurfaceFormatsKHR, gdk_vulkan_context_get_physical_device (context),
priv->surface,
&n_formats, NULL);
VkSurfaceFormatKHR formats[n_formats];
GDK_VK_CHECK (vkGetPhysicalDeviceSurfaceFormatsKHR, gdk_vulkan_context_get_physical_device (context),
priv->surface,
&n_formats, formats);
for (i = 0; i < n_formats; i++)
{
if (formats[i].format == VK_FORMAT_B8G8R8A8_SRGB)
break;
}
if (i == n_formats)
{
g_set_error_literal (error, GDK_VULKAN_ERROR, GDK_VULKAN_ERROR_NOT_AVAILABLE,
"No supported image format found.");
goto out_surface;
}
priv->image_format = formats[i];
if (!gdk_vulkan_context_check_swapchain (context, error))
goto out_surface;
return TRUE;
}
out_surface:
vkDestroySurfaceKHR (gdk_vulkan_context_get_instance (context),
priv->surface,
NULL);
priv->surface = VK_NULL_HANDLE;
return FALSE;
}
static void
gdk_vulkan_context_initable_init (GInitableIface *iface)
{
iface->init = gdk_vulkan_context_real_init;
}
VkInstance
gdk_vulkan_context_get_instance (GdkVulkanContext *context)
{
g_return_val_if_fail (GDK_IS_VULKAN_CONTEXT (context), NULL);
return gdk_draw_context_get_display (GDK_DRAW_CONTEXT (context))->vk_instance;
}
VkPhysicalDevice
gdk_vulkan_context_get_physical_device (GdkVulkanContext *context)
{
g_return_val_if_fail (GDK_IS_VULKAN_CONTEXT (context), NULL);
return gdk_draw_context_get_display (GDK_DRAW_CONTEXT (context))->vk_physical_device;
}
VkDevice
gdk_vulkan_context_get_device (GdkVulkanContext *context)
{
g_return_val_if_fail (GDK_IS_VULKAN_CONTEXT (context), NULL);
return gdk_draw_context_get_display (GDK_DRAW_CONTEXT (context))->vk_device;
}
VkQueue
gdk_vulkan_context_get_queue (GdkVulkanContext *context)
{
g_return_val_if_fail (GDK_IS_VULKAN_CONTEXT (context), NULL);
return gdk_draw_context_get_display (GDK_DRAW_CONTEXT (context))->vk_queue;
}
uint32_t
gdk_vulkan_context_get_queue_family_index (GdkVulkanContext *context)
{
g_return_val_if_fail (GDK_IS_VULKAN_CONTEXT (context), 0);
return gdk_draw_context_get_display (GDK_DRAW_CONTEXT (context))->vk_queue_family_index;
}
VkFormat
gdk_vulkan_context_get_image_format (GdkVulkanContext *context)
{
GdkVulkanContextPrivate *priv = gdk_vulkan_context_get_instance_private (context);
g_return_val_if_fail (GDK_IS_VULKAN_CONTEXT (context), VK_FORMAT_UNDEFINED);
return priv->image_format.format;
}
static gboolean
gdk_display_create_vulkan_device (GdkDisplay *display,
GError **error)
{
uint32_t i, j;
uint32_t n_devices;
GDK_VK_CHECK(vkEnumeratePhysicalDevices, display->vk_instance, &n_devices, NULL);
VkPhysicalDevice devices[n_devices];
GDK_VK_CHECK(vkEnumeratePhysicalDevices, display->vk_instance, &n_devices, devices);
if (n_devices == 0)
{
/* Give a different error for 0 devices so people know their drivers suck. */
g_set_error_literal (error, GDK_VULKAN_ERROR, GDK_VULKAN_ERROR_NOT_AVAILABLE,
"No Vulkan devices available.");
return FALSE;
}
for (i = 0; i < n_devices; i++)
{
VkPhysicalDeviceProperties props;
vkGetPhysicalDeviceProperties (devices[i], &props);
GDK_NOTE (VULKAN, g_print ("Vulkan Device %u:\n", i));
GDK_NOTE (VULKAN, g_print (" %s (%u)\n", props.deviceName, props.deviceType));
GDK_NOTE (VULKAN, g_print (" vendor ID: 0x%Xu\n", props.vendorID));
GDK_NOTE (VULKAN, g_print (" device ID: 0x%Xu\n", props.deviceID));
GDK_NOTE (VULKAN, g_print (" API version %u.%u.%u\n",
VK_VERSION_MAJOR (props.apiVersion),
VK_VERSION_MINOR (props.apiVersion),
VK_VERSION_PATCH (props.apiVersion)));
GDK_NOTE (VULKAN, g_print (" driver version %u.%u.%u\n",
VK_VERSION_MAJOR (props.driverVersion),
VK_VERSION_MINOR (props.driverVersion),
VK_VERSION_PATCH (props.driverVersion)));
uint32_t n_queue_props;
vkGetPhysicalDeviceQueueFamilyProperties (devices[i], &n_queue_props, NULL);
VkQueueFamilyProperties queue_props[n_queue_props];
vkGetPhysicalDeviceQueueFamilyProperties (devices[i], &n_queue_props, queue_props);
for (j = 0; j < n_queue_props; j++)
{
GDK_NOTE (VULKAN, g_print (" queue %u/%u: %s\n", j, n_queue_props, queue_props[j].queueFlags & VK_QUEUE_GRAPHICS_BIT ? "graphics" : "no graphics"));
if (queue_props[j].queueFlags & VK_QUEUE_GRAPHICS_BIT)
{
GDK_NOTE (VULKAN, g_print (" => trying this queue\n"));
if (GDK_VK_CHECK (vkCreateDevice, devices[i],
&(VkDeviceCreateInfo) {
VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
NULL,
0,
1,
&(VkDeviceQueueCreateInfo) {
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
.queueFamilyIndex = j,
.queueCount = 1,
.pQueuePriorities = (float []) { 1.0f },
},
0,
NULL,
1,
(const char * const []) {
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
},
},
NULL,
&display->vk_device) != VK_SUCCESS)
continue;
display->vk_physical_device = devices[i];
vkGetDeviceQueue(display->vk_device, j, 0, &display->vk_queue);
display->vk_queue_family_index = j;
return TRUE;
}
}
}
g_set_error_literal (error, GDK_VULKAN_ERROR, GDK_VULKAN_ERROR_NOT_AVAILABLE,
"Could not find a Vulkan device with the required features.");
return FALSE;
}
static gboolean
gdk_display_create_vulkan_instance (GdkDisplay *display,
GError **error)
{
uint32_t i;
if (GDK_DISPLAY_GET_CLASS (display)->vk_extension_name == NULL)
{
g_set_error (error, GDK_VULKAN_ERROR, GDK_VULKAN_ERROR_UNSUPPORTED,
"The %s backend has no Vulkan support.", G_OBJECT_TYPE_NAME (display));
return FALSE;
}
uint32_t n_extensions;
GDK_VK_CHECK (vkEnumerateInstanceExtensionProperties, NULL, &n_extensions, NULL);
VkExtensionProperties extensions[n_extensions];
GDK_VK_CHECK (vkEnumerateInstanceExtensionProperties, NULL, &n_extensions, extensions);
for (i = 0; i < n_extensions; i++)
{
GDK_NOTE (VULKAN, g_print ("Extension available: %s v%u.%u.%u\n",
extensions[i].extensionName,
VK_VERSION_MAJOR (extensions[i].specVersion),
VK_VERSION_MINOR (extensions[i].specVersion),
VK_VERSION_PATCH (extensions[i].specVersion)));
}
uint32_t n_layers;
GDK_VK_CHECK (vkEnumerateInstanceLayerProperties, &n_layers, NULL);
VkLayerProperties layers[n_layers];
GDK_VK_CHECK (vkEnumerateInstanceLayerProperties, &n_layers, layers);
for (i = 0; i < n_layers; i++)
{
GDK_NOTE (VULKAN, g_print ("Layer available: %s v%u.%u.%u (%s)\n",
layers[i].layerName,
VK_VERSION_MAJOR (layers[i].specVersion),
VK_VERSION_MINOR (layers[i].specVersion),
VK_VERSION_PATCH (layers[i].specVersion),
layers[i].description));
}
if (GDK_VK_CHECK (vkCreateInstance, &(VkInstanceCreateInfo) {
VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
NULL,
0,
&(VkApplicationInfo) {
VK_STRUCTURE_TYPE_APPLICATION_INFO,
NULL,
g_get_application_name (),
0,
"GTK+",
VK_MAKE_VERSION (GDK_MAJOR_VERSION, GDK_MINOR_VERSION, GDK_MICRO_VERSION),
VK_API_VERSION_1_0 },
0,
NULL,
2,
(const char *const *) &(const char *[2]) {
VK_KHR_SURFACE_EXTENSION_NAME,
GDK_DISPLAY_GET_CLASS (display)->vk_extension_name
},
},
NULL,
&display->vk_instance) != VK_SUCCESS)
{
g_set_error_literal (error, GDK_VULKAN_ERROR, GDK_VULKAN_ERROR_UNSUPPORTED,
"Could not create a Vulkan instance.");
return FALSE;
}
return gdk_display_create_vulkan_device (display, error);
}
gboolean
gdk_display_ref_vulkan (GdkDisplay *display,
GError **error)
{
if (display->vulkan_refcount == 0)
{
if (!gdk_display_create_vulkan_instance (display, error))
return FALSE;
}
display->vulkan_refcount++;
return TRUE;
}
void
gdk_display_unref_vulkan (GdkDisplay *display)
{
g_return_if_fail (GDK_IS_DISPLAY (display));
g_return_if_fail (display->vulkan_refcount > 0);
display->vulkan_refcount--;
if (display->vulkan_refcount > 0)
return;
vkDestroyDevice (display->vk_device, NULL);
vkDestroyInstance (display->vk_instance, NULL);
}
#else /* GDK_WINDOWING_VULKAN */
static void
gdk_vulkan_context_class_init (GdkVulkanContextClass *klass)
{
}
static void
gdk_vulkan_context_init (GdkVulkanContext *self)
{
}
static void
gdk_vulkan_context_initable_init (GInitableIface *iface)
{
}
#endif /* GDK_WINDOWING_VULKAN */
|