summaryrefslogtreecommitdiff
path: root/main.c
blob: dde113ae75975eaa267fed2c657ccce2d133ced6 (plain)
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/*
Copyright (c) 2019, Jordan Halase <jordan@halase.me>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/

/* Note: Due to the length of many function and variable names in the
 *       Vulkan API, the line width of any Vulkan-related code here may be
 *       extended from 80 columns to 120 colums to improve readability.
 */

/* The goal of this project is to create an entirely self-contained, embeddable
 * Vulkan renderer. While dynamic linking directly against the system Vulkan
 * loader is safe, it will crash if attempting to run on a system without it
 * installed. By loading the Vulkan library dynamically, the application may
 * gracefully close or even fall back to a different rendering API.
 * Furthermore, Vulkan specifies that using functions retrieved via
 * `vkGetDeviceProcAddr` may actually run faster than if the application were
 * using the Vulkan loader directly due to bypassing the loader terminator.
 *
 * https://vulkan.lunarg.com/doc/sdk/1.0.61.1/windows/LoaderAndLayerInterface.html
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#include <vulkan/vulkan.h>

#if defined(_WIN32)
#include <windows.h>
#include <vulkan/vulkan_win32.h>
#else
#include <X11/Xlib.h>
#include <vulkan/vulkan_xlib.h>
#include "pugl/detail/x11.h"
#endif

#include "test/test_utils.h" // for printEvent()

#include "pugl/pugl.h"
#include "pugl/pugl_stub_backend.h"

#define STB_SPRINTF_IMPLEMENTATION
#include "stb_sprintf.h"

#if defined(__GNUC__)
#define APP_THREAD_LOCAL	__thread
#else
#define APP_THREAD_LOCAL	__declspec(thread)
#endif

/** Vulkan allocation callbacks if we ever decide to use them when debugging.
 *
 * This is put in a macro so we don't have to look for every Vulkan function
 * that uses it.
 */
#define ALLOC_VK	NULL

struct VulkanAPI {
	void *handle;
	PFN_vkGetInstanceProcAddr			vkGetInstanceProcAddr;
	PFN_vkCreateInstance				vkCreateInstance;
	PFN_vkDestroyInstance				vkDestroyInstance;
	PFN_vkCreateDebugReportCallbackEXT		vkCreateDebugReportCallbackEXT;
	PFN_vkDestroyDebugReportCallbackEXT		vkDestroyDebugReportCallbackEXT;
	PFN_vkDestroySurfaceKHR				vkDestroySurfaceKHR;
	PFN_vkEnumeratePhysicalDevices			vkEnumeratePhysicalDevices;
	PFN_vkGetPhysicalDeviceProperties		vkGetPhysicalDeviceProperties;
	PFN_vkGetPhysicalDeviceQueueFamilyProperties	vkGetPhysicalDeviceQueueFamilyProperties;
	PFN_vkGetPhysicalDeviceSurfaceSupportKHR	vkGetPhysicalDeviceSurfaceSupportKHR;
	PFN_vkGetPhysicalDeviceMemoryProperties		vkGetPhysicalDeviceMemoryProperties;
	PFN_vkEnumerateDeviceExtensionProperties	vkEnumerateDeviceExtensionProperties;
};

struct RenderVulkan {
	struct VulkanAPI *api;
	char *errMsg;
	VkInstance			instance;
	VkDebugReportCallbackEXT	debugCallback;
	VkSurfaceKHR			surface;
	VkPhysicalDeviceProperties	deviceProperties;
	VkPhysicalDevice		physicalDevice;
	uint32_t			graphicsIndex;
	VkDevice			device;
};

#define RVK_ERRMSG_LEN	4096

void rvkSetErrMsg(struct RenderVulkan *vk, const char *fmt, ...)
{
	vk->errMsg = realloc(vk->errMsg, RVK_ERRMSG_LEN);
	va_list args;
	va_start(args, fmt);
	stbsp_vsnprintf(vk->errMsg, RVK_ERRMSG_LEN, fmt, args);
	va_end(args);
}

void rvkClearErrMsg(struct RenderVulkan *vk)
{
	if (vk->errMsg) {
		free(vk->errMsg);
		vk->errMsg = NULL;
	}
}

const char *rvkGetErrMsg(struct RenderVulkan *vk)
{
	return vk->errMsg;
}

#if defined(_WIN32)
#define VULKAN_SONAME_LATEST	"vulkan-1.dll"
void *appDlopen(const char *soname)
{
	return LoadLibraryA(soname);
}

char *appDlerror()
{
	DWORD errCode = GetLastError();
	static APP_THREAD_LOCAL char errStr[64];
	stbsp_sprintf(errStr, "Dynamic Library Error: %d", errCode);
	return errStr;
}

void *appDlsym(void *handle, const char *symbol)
{
	const uintptr_t ulAddr = (uintptr_t)GetProcAddress(handle, symbol);
	return (void*)ulAddr;
}

int appDlclose(void *handle)
{
	return FreeLibrary(handle);
}

void getRequiredInstanceExtensions(void *windowCtx,
		unsigned *nRequired,
		const char **const extensions)
{
	(void)windowCtx;
	static const char *const required[] = {
		VK_KHR_SURFACE_EXTENSION_NAME,
		VK_KHR_WIN32_SURFACE_EXTENSION_NAME
	};
	static const unsigned num = sizeof(required) / sizeof(required[0]);
	if (extensions) {
		for (int i = 0; i < num; ++i) {
			extensions[i] = required[i];
		}
	} else {
		*nRequired = num;
	}
}

VkResult createVulkanSurface(PuglView *view,
		VkInstance instance,
		PFN_vkGetInstanceProcAddr getInstanceProcAddrFunc,
		const VkAllocationCallbacks *pAllocator,
		VkSurfaceKHR *pSurface)
{
	PuglWorld *world = ((struct PuglViewImpl*)view)->world;
	VkWin32SurfaceCreateInfoKHR createInfo = { 0 };
	createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
	createInfo.hinstance = GetModuleHandle(0);	// FIXME
	createInfo.hwnd = puglGetNativeWindow(view);
	PFN_vkCreateWin32SurfaceKHR vkCreateWin32SurfaceKHR;
	uintptr_t *const ulCreateWin32SurfaceKHR = (uintptr_t*)&vkCreateWin32SurfaceKHR;
	*ulCreateWin32SurfaceKHR = (uintptr_t)getInstanceProcAddrFunc(instance, "vkCreateWin32SurfaceKHR");
	return vkCreateWin32SurfaceKHR(instance, &createInfo, pAllocator, pSurface);
}
#else
#define VULKAN_SONAME_LATEST	"libvulkan.so.1"
#include <dlfcn.h>
// XXX: puglDlopen()
void *appDlopen(const char *soname)
{
	return dlopen(soname, RTLD_NOW);
}

char *appDlerror()
{
	return dlerror();
}

void *appDlsym(void *handle, const char *symbol)
{
	return dlsym(handle, symbol);
}

int appDlclose(void *handle)
{
	return dlclose(handle);
}

/* XXX:  puglGetRequiredInstanceExtensions()
 * TODO: Linux actually has three possible surfaces: Xlib, XCB, and Wayland.
 *       This should be figured out at runtime (without using #ifdefs).
 *       In which case, `windowCtx` will be used to determine which to use.
 *       As of now, Pugl (and LV2!) only supports Xlib.
 */
void getRequiredInstanceExtensions(void *windowCtx,
		unsigned *nRequired,
		const char **const extensions)
{
	(void)windowCtx;
	static const char *const required[] = {
		VK_KHR_SURFACE_EXTENSION_NAME,
		VK_KHR_XLIB_SURFACE_EXTENSION_NAME
	};
	static const unsigned num = sizeof(required) / sizeof(required[0]);
	if (extensions) {
		for (int i = 0; i < num; ++i) {
			extensions[i] = required[i];
		}
	} else {
		*nRequired = num;
	}
}

/* XXX: puglCreateVulkanSurface()
 *      No need to wrap VkFreeSurfaceKHR()
 */
VkResult createVulkanSurface(PuglView *view,
		VkInstance instance,
		PFN_vkGetInstanceProcAddr getInstanceProcAddrFunc,
		const VkAllocationCallbacks *pAllocator,
		VkSurfaceKHR *pSurface)
{
	PuglWorld *world = ((struct PuglViewImpl*)view)->world;
	VkXlibSurfaceCreateInfoKHR createInfo = { 0 };
	createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
	createInfo.dpy = ((struct PuglWorldImpl*)world)->impl->display;
	createInfo.window = puglGetNativeWindow(view);
	PFN_vkCreateXlibSurfaceKHR vkCreateXlibSurfaceKHR;
	uintptr_t *const ulCreateXlibSurfaceKHR = (uintptr_t*)&vkCreateXlibSurfaceKHR;
	*ulCreateXlibSurfaceKHR = (uintptr_t)getInstanceProcAddrFunc(instance, "vkCreateXlibSurfaceKHR");
	return vkCreateXlibSurfaceKHR(instance, &createInfo, pAllocator, pSurface);
}
#endif

void *loadVulkanLibrary(const char *prefix)
{
	(void)prefix;	// TODO
	void *handle = appDlopen(VULKAN_SONAME_LATEST);
	return handle;
}

void loadVulkanGetInstanceProcAddrFunc(void *handle,
		PFN_vkGetInstanceProcAddr *getInstanceProcAddrFunc)
{
	uintptr_t *ulGetInstanceProcAddrFunc = (uintptr_t*)getInstanceProcAddrFunc;
	*ulGetInstanceProcAddrFunc = (uintptr_t)appDlsym(handle, "vkGetInstanceProcAddr");
}

int unloadVulkanLibrary(void *handle)
{
	if (handle) {
		return appDlclose(handle);
	}
	return 0;
}

static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
		VkDebugReportFlagsEXT flags,
		VkDebugReportObjectTypeEXT objType,
		uint64_t obj,
		size_t location,
		int32_t code,
		const char *layerPrefix,
		const char *msg,
		void *userData
		)
{
	fprintf(stderr, "\nValidation layer:\n%s\n\n", msg);
	return VK_FALSE;
}

static VkResult createInstance(struct RenderVulkan *vk,
	const uint32_t nLayers, const char *const *const layers,
	const uint32_t nAdditional, const char *const *const additionalExtensions)
{
	VkApplicationInfo appInfo = { 0 };
	appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
	appInfo.pApplicationName = "Pugl Vulkan Test";
	appInfo.applicationVersion = VK_MAKE_VERSION(0, 1, 0);
	appInfo.pEngineName = "Pugl Vulkan Test Engine";
	appInfo.engineVersion = VK_MAKE_VERSION(0, 1, 0);
	/* MoltenVK for macOS currently only supports Vulkan 1.0 */
	appInfo.apiVersion = VK_MAKE_VERSION(1, 0, 0);

	unsigned i, j, nRequired;
	getRequiredInstanceExtensions(NULL, &nRequired, NULL);

	const uint32_t nExtensions = nRequired + nAdditional;
	const char **const extensions = malloc(sizeof(const char*) * nExtensions);

	getRequiredInstanceExtensions(NULL, NULL, extensions);
	for (i = nRequired, j = 0; i < nExtensions; ++i, ++j) {
		extensions[i] = additionalExtensions[j];
	}

	for (i = 0; i < nExtensions; ++i) {
		printf("Using instance extension:\t%s\n", extensions[i]);
	}

	for (i = 0; i < nLayers; ++i) {
		printf("Using instance layer:\t\t%s\n", layers[i]);
	}

	VkInstanceCreateInfo createInfo = { 0 };
	createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
	createInfo.pApplicationInfo = &appInfo;
	createInfo.enabledLayerCount = nLayers;
	createInfo.ppEnabledLayerNames = layers;
	createInfo.enabledExtensionCount = nExtensions;
	createInfo.ppEnabledExtensionNames = extensions;

	VkResult result = VK_SUCCESS;
	if ((result = vk->api->vkCreateInstance(&createInfo, ALLOC_VK, &vk->instance))) {
		rvkSetErrMsg(vk, "Could not create Vulkan Instance: %d\n", result);
	}
	free(extensions);
	return result;
}

/** Must not be called until all derivative objects are destroyed first */
static void destroyInstance(struct RenderVulkan *vk)
{
	vk->api->vkDestroyInstance(vk->instance, ALLOC_VK);
	vk->instance = VK_NULL_HANDLE;
}

/** This must work no matter the current state of `vk` */
void rvkDestroy(struct RenderVulkan *vk)
{
	if (vk) {
		if (vk->surface) vk->api->vkDestroySurfaceKHR(vk->instance, vk->surface, ALLOC_VK);
		if (vk->debugCallback) {
			/* `vk->debugCallback` implies `vk->api` and instance functions loaded */
			vk->api->vkDestroyDebugReportCallbackEXT(vk->instance, vk->debugCallback, ALLOC_VK);
		}
		if (vk->instance) destroyInstance(vk);
		if (vk->api) {
			if (vk->api->handle) unloadVulkanLibrary(vk->api->handle);
			free(vk->api);
		}
		if (vk->errMsg) free(vk->errMsg);
		if (vk) free(vk);
	}
}

/** Create a self-contained Vulkan instance and set up a debug reporter
 *
 * If errors occurred, the struct will be returned in an unusable state,
 * and MUST be checked via `rvkGetErrMsg`. It MUST then be destroyed via
 * `rvkDestroy`.
 * */
struct RenderVulkan *rvkCreate()
{
	static const char *const instanceLayers[] = {
		"VK_LAYER_LUNARG_standard_validation"
	};
	const uint32_t nInstanceLayers = sizeof(instanceLayers) / sizeof(instanceLayers[0]);
	static const char *const instanceExtensions[] = {
		VK_EXT_DEBUG_REPORT_EXTENSION_NAME
	};
	const uint32_t nInstanceExtensions = sizeof(instanceExtensions) / sizeof(instanceExtensions[0]);

	struct RenderVulkan *vk = calloc(1, sizeof(*vk));
	vk->api = calloc(1, sizeof(*vk->api));

	vk->api->handle = loadVulkanLibrary(NULL);
	if (!vk->api->handle) {
		rvkSetErrMsg(vk, "Error loading Vulkan shared library:\n%s\n", appDlerror());
		return vk;
	}

	loadVulkanGetInstanceProcAddrFunc(vk->api->handle, &vk->api->vkGetInstanceProcAddr);
	if (!vk->api->vkGetInstanceProcAddr) {
		rvkSetErrMsg(vk, "Error loading `vkGetInstanceProcAddr`:\n%s", appDlerror());
		return vk;
	}

	uintptr_t *const ulCreateInstance = (uintptr_t*)&vk->api->vkCreateInstance;
	*ulCreateInstance = (uintptr_t)vk->api->vkGetInstanceProcAddr(NULL, "vkCreateInstance");
	if (!vk->api->vkCreateInstance) {
		rvkSetErrMsg(vk, "Error loading `vkCreateInstance`");
		return vk;
	}

	VkResult result;
	//if ((result = createInstance(vk, 0, NULL, 0, NULL))) {
	if ((result = createInstance(vk, nInstanceLayers, instanceLayers, nInstanceExtensions, instanceExtensions))) {
		return vk;
	}

	/* TODO: This could perhaps be generated */
	static const char *const strErrLd = "Error loading function %s";

	static const char *const strDestroyInstance = "vkDestroyInstance";
	uintptr_t *const ulDestroyInstance = (uintptr_t*)&vk->api->vkDestroyInstance;
	*ulDestroyInstance = (uintptr_t)vk->api->vkGetInstanceProcAddr(vk->instance, strDestroyInstance);
	if (!vk->api->vkDestroyInstance) {
		rvkSetErrMsg(vk, strErrLd, strDestroyInstance);
		return vk;
	}

	/* It is okay if debug reporter functions are not resolved */
	uintptr_t *ulCreateDebugReportCallbackEXT = (uintptr_t*)&vk->api->vkCreateDebugReportCallbackEXT;
	*ulCreateDebugReportCallbackEXT = (uintptr_t)vk->api->vkGetInstanceProcAddr(vk->instance, "vkCreateDebugReportCallbackEXT");

	uintptr_t *ulDestroyDebugReportCallbackEXT = (uintptr_t*)&vk->api->vkDestroyDebugReportCallbackEXT;
	*ulDestroyDebugReportCallbackEXT = (uintptr_t)vk->api->vkGetInstanceProcAddr(vk->instance, "vkDestroyDebugReportCallbackEXT");

	/* But not if we are unable to destroy a created debug reporter */
	if (vk->api->vkCreateDebugReportCallbackEXT && !vk->api->vkDestroyDebugReportCallbackEXT) {
		rvkSetErrMsg(vk, "No debug reporter destroy function loaded for corresponding create function\n");
		return vk;
	}

	static const char *const strDestroySurfaceKHR = "vkDestroySurfaceKHR";
	uintptr_t *ulDestroySurfaceKHR = (uintptr_t*)&vk->api->vkDestroySurfaceKHR;
	*ulDestroySurfaceKHR = (uintptr_t)vk->api->vkGetInstanceProcAddr(vk->instance, strDestroySurfaceKHR);
	if (!vk->api->vkDestroySurfaceKHR) {
		rvkSetErrMsg(vk, strErrLd, strDestroySurfaceKHR);
		return vk;
	}

	static const char *const strEnumeratePhysicalDevices = "vkEnumeratePhysicalDevices";
	uintptr_t *ulEnumeratePhysicalDevices = (uintptr_t*)&vk->api->vkEnumeratePhysicalDevices;
	*ulEnumeratePhysicalDevices = (uintptr_t)vk->api->vkGetInstanceProcAddr(vk->instance, strEnumeratePhysicalDevices);
	if (!vk->api->vkEnumeratePhysicalDevices) {
		rvkSetErrMsg(vk, strErrLd, strEnumeratePhysicalDevices);
		return vk;
	}

	static const char *const strGetPhysicalDeviceProperties = "vkGetPhysicalDeviceProperties";
	uintptr_t *ulGetPhysicalDeviceProperties = (uintptr_t*)&vk->api->vkGetPhysicalDeviceProperties;
	*ulGetPhysicalDeviceProperties = (uintptr_t)vk->api->vkGetInstanceProcAddr(vk->instance, strGetPhysicalDeviceProperties);
	if (!vk->api->vkGetPhysicalDeviceProperties) {
		rvkSetErrMsg(vk, strErrLd, strGetPhysicalDeviceProperties);
		return vk;
	}

	static const char *const strGetPhysicalDeviceQueueFamilyProperties = "vkGetPhysicalDeviceQueueFamilyProperties";
	uintptr_t *ulGetPhysicalDeviceQueueFamilyProperties = (uintptr_t*)&vk->api->vkGetPhysicalDeviceQueueFamilyProperties;
	*ulGetPhysicalDeviceQueueFamilyProperties = (uintptr_t)vk->api->vkGetInstanceProcAddr(vk->instance, strGetPhysicalDeviceQueueFamilyProperties);
	if (!vk->api->vkGetPhysicalDeviceQueueFamilyProperties) {
		rvkSetErrMsg(vk, strErrLd, strGetPhysicalDeviceQueueFamilyProperties);
		return vk;
	}

	static const char *const strGetPhysicalDeviceSurfaceSupportKHR = "vkGetPhysicalDeviceSurfaceSupportKHR";
	uintptr_t *ulGetPhysicalDeviceSurfaceSupportKHR = (uintptr_t*)&vk->api->vkGetPhysicalDeviceSurfaceSupportKHR;
	*ulGetPhysicalDeviceSurfaceSupportKHR = (uintptr_t)vk->api->vkGetInstanceProcAddr(vk->instance, strGetPhysicalDeviceSurfaceSupportKHR);
	if (!vk->api->vkGetPhysicalDeviceSurfaceSupportKHR) {
		rvkSetErrMsg(vk, strErrLd, strGetPhysicalDeviceSurfaceSupportKHR);
		return vk;
	}

	static const char *const strGetPhysicalDeviceMemoryProperties = "vkGetPhysicalDeviceMemoryProperties";
	uintptr_t *ulGetPhysicalDeviceMemoryProperties = (uintptr_t*)&vk->api->vkGetPhysicalDeviceMemoryProperties;
	*ulGetPhysicalDeviceMemoryProperties = (uintptr_t)vk->api->vkGetInstanceProcAddr(vk->instance, strGetPhysicalDeviceMemoryProperties);
	if (!vk->api->vkGetPhysicalDeviceMemoryProperties) {
		rvkSetErrMsg(vk, strErrLd, strGetPhysicalDeviceMemoryProperties);
		return vk;
	}

	static const char *const strEnumerateDeviceExtensionProperties = "vkEnumerateDeviceExtensionProperties";
	uintptr_t *ulEnumerateDeviceExtensionProperties = (uintptr_t*)&vk->api->vkEnumerateDeviceExtensionProperties;
	*ulEnumerateDeviceExtensionProperties = (uintptr_t)vk->api->vkGetInstanceProcAddr(vk->instance, strEnumerateDeviceExtensionProperties);
	if (!vk->api->vkEnumerateDeviceExtensionProperties) {
		rvkSetErrMsg(vk, strErrLd, strEnumerateDeviceExtensionProperties);
		return vk;
	}

	if (vk->api->vkCreateDebugReportCallbackEXT) {
		VkDebugReportCallbackCreateInfoEXT debugInfo = { 0 };
		debugInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT;
		debugInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
		debugInfo.pfnCallback = debugCallback;
		if ((result = vk->api->vkCreateDebugReportCallbackEXT(vk->instance,
						&debugInfo, ALLOC_VK, &vk->debugCallback))) {
			rvkSetErrMsg(vk, "Could not create debug reporter: %d", result);
			return vk;
		}
	}

	return vk;
}

static void rvkCreateSurface(struct RenderVulkan *vk, PuglView *view)
{
	VkResult result;
	if ((result = createVulkanSurface(view, vk->instance, vk->api->vkGetInstanceProcAddr, ALLOC_VK, &vk->surface))) {
		rvkSetErrMsg(vk, "Could not create window surface: %d\n", result);
	}
}

/** Checks if a particular physical device is suitable for this application.
 *
 * Choosing a physical device is an *extremely* application-specific procedure.
 *
 * All rendering in Vulkan is done off-screen by default. To get rendered
 * results on-screen they must be "presented" to a surface.
 * The Vulkan spec allows devices to have presentation done on a separate queue
 * family than GRAPHICS, or no present support at all. However, every graphics
 * card today that is capable of connecting to a display has at least one queue
 * family capable of both GRAPHICS and present.
 *
 * This single-threaded application will use only one queue for all operations.
 * More specifically, it will make use of GRAPHICS and TRANSFER operations on
 * a single queue retrieved from a GRAPHICS queue family that supports present.
 *
 * In my experience, most cards follow this format:
 *
 * INTEGRATED: Typically have one queue family for all operations.
 *             May retrieve only one queue from this single queue family.
 * DEDICATED:  Older cards typically have one queue family for GRAPHICS and one
 *             for TRANSFER. Newer cards typically have a separate queue family
 *             for COMPUTE. Remember that all GRAPHICS queue families
 *             implicitly allow both COMPUTE and TRANSFER operations on it as
 *             well. Programmers making use of separate queue families may allow
 *             devices to work more efficiently.
 *
 *             May typically retrieve up to a few queues from either queue
 *             family. The maximum number of available queues is specific to
 *             that device's queue families. Programmers making use of separate
 *             queues for highly independent workloads may allow devices to work
 *             more efficiently. Queues must be used from a single thread, so
 *             programmers wanting to make use of multithreaded queue submission
 *             must retrieve queues per-thread.
 *
 *             GOTCHA: Some devices may have multiple GRAPHICS queue families
 *             with only one of them able to present. Do not assume a device is
 *             unsuitable until ALL queue families have been checked.
 *
 * Because this application will load all resources before any rendering begins,
 * and the amount of resources are small enough to be loaded almost instantly,
 * it will not make use of a separate TRANSFER queue family. This application
 * will also not attempt to find the "most powerful" device on the system, and
 * will choose the first suitable device it finds.
 *
 * Information for many devices is available online at
 * https://vulkan.gpuinfo.org/
 */
static int isDeviceSuitable(const struct RenderVulkan *const vk,
		const VkPhysicalDevice pd,
		uint32_t *const graphicsIndex)
{
	uint32_t nQueueFamilies;
	vk->api->vkGetPhysicalDeviceQueueFamilyProperties(pd, &nQueueFamilies, NULL);
	VkQueueFamilyProperties *queueProperties = malloc(nQueueFamilies * sizeof(*queueProperties));
	vk->api->vkGetPhysicalDeviceQueueFamilyProperties(pd, &nQueueFamilies, queueProperties);
	for (uint32_t i = 0; i < nQueueFamilies; ++i) {
		printf("Queue Family %d queueCount:\t%d\n", i, queueProperties[i].queueCount);
	}
	uint32_t g;
	for (g = 0; g < nQueueFamilies; ++g) {
		if (queueProperties[g].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
			VkBool32 canSurface;
			vk->api->vkGetPhysicalDeviceSurfaceSupportKHR(pd, g, vk->surface, &canSurface);
			if (canSurface) break;
		}
	}
	free(queueProperties);
	if (g >= nQueueFamilies) {
		/* We only support graphics and present on the same queue family. */
		printf("No graphics+support queue families found on this device\n");
		return 0;
	}
	VkBool32 canSwapchain = 0;
	uint32_t nExtensions;
	vk->api->vkEnumerateDeviceExtensionProperties(pd, NULL, &nExtensions, NULL);
	VkExtensionProperties *availableExtensions = malloc(nExtensions * sizeof(*availableExtensions));
	vk->api->vkEnumerateDeviceExtensionProperties(pd, NULL, &nExtensions, availableExtensions);
	for (uint32_t i = 0; i < nExtensions; ++i) {
		if (!strcmp(availableExtensions[i].extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
			canSwapchain = 1;
			break;
		}
	}
	free(availableExtensions);
	if (!canSwapchain) {
		printf("Cannot use a swapchain on this device\n");
		return 0;
	}
	*graphicsIndex = g;
	return 1;
}

void rvkSelectPhysicalDevice(struct RenderVulkan *vk)
{
	if (!vk->surface) {
		rvkSetErrMsg(vk, "Cannot select a physical device without a surface");
		return;
	}
	static const char *const strErrEnum = "Could not enumerate physical devices: %s";
	static const char *const strWarnEnum = "Warn: Incomplete enumeration of physical devices";
	uint32_t nDevices;
	VkResult result;
	if ((result = vk->api->vkEnumeratePhysicalDevices(vk->instance, &nDevices, NULL))) {
		if (result != VK_INCOMPLETE) {
			rvkSetErrMsg(vk, strErrEnum, result);
			return;
		} else {
			fprintf(stderr, "%s\n", strWarnEnum);
		}
	}
	if (!nDevices) {
		rvkSetErrMsg(vk, "No physical devices found");
		return;
	}
	VkPhysicalDevice *devices = malloc(nDevices * sizeof(*devices));
	VkPhysicalDeviceProperties *deviceProperties = malloc(nDevices * sizeof(*deviceProperties));
	if ((result = vk->api->vkEnumeratePhysicalDevices(vk->instance, &nDevices, devices))) {
		if (result != VK_INCOMPLETE) {
			rvkSetErrMsg(vk, strErrEnum, result);
			goto done;
		} else {
			fprintf(stderr, "%s\n", strWarnEnum);
		}
	}

	uint32_t i;
	for (i = 0; i < nDevices; ++i) {
		vk->api->vkGetPhysicalDeviceProperties(devices[i], &deviceProperties[i]);
		printf("Found physical device:\t\t`%s`\n", deviceProperties[i].deviceName);
	}
	for (i = 0; i < nDevices; ++i) {
		printf("Checking suitability for\t`%s`...\n", deviceProperties[i].deviceName);
		if (isDeviceSuitable(vk, devices[i], &vk->graphicsIndex)) {
			printf("Using physical device:\t\t`%s`\n", deviceProperties[i].deviceName);
			vk->deviceProperties = deviceProperties[i];
			vk->physicalDevice = devices[i];
			printf("Graphics Index:\t\t\t%d\n", vk->graphicsIndex);
			goto done;
		}
		printf("Device `%s` not suitable\n", deviceProperties[i].deviceName);
	}
	if (i >= nDevices) {
		rvkSetErrMsg(vk, "No suitable devices found");
	}
done:
	free(deviceProperties);
	free(devices);
}

void rvkCheckFatal(struct RenderVulkan *vk)
{
	const char *errMsg = NULL;
	if ((errMsg = rvkGetErrMsg(vk))) {
		fprintf(stderr, "%s\n", errMsg);
		rvkDestroy(vk);
		exit(1);
	}
}

int running = 1;

PuglStatus onEvent(PuglView *view, const PuglEvent *e)
{
	printEvent(e, "Event:\t", true);
	switch (e->type) {
		case PUGL_CLOSE:
			running = 0;
			break;
		default:
			break;
	}
	return PUGL_SUCCESS;
}

int main()
{
#if defined(__linux__)
	/* The Mesa Vulkan drivers require this to be called */
	XInitThreads();
#endif
	const char *errMsg = NULL;
	struct RenderVulkan *vk = rvkCreate();
	rvkCheckFatal(vk);

	printf("Created Vulkan Instance Successfully\n");

	PuglWorld *world = puglNewWorld();
	PuglView *view = puglNewView(world);
	const PuglRect frame = { 0, 0, 800, 600 };
	puglSetBackend(view, puglStubBackend());

	PuglStatus status;
	if ((status = puglCreateWindow(view, "Pugl Vulkan Test"))) {
		fprintf(stderr, "Could not create window: %d\n", status);
		rvkDestroy(vk);
		puglFreeWorld(world);
		exit(1);
	}
	puglSetEventFunc(view, onEvent);

	rvkCreateSurface(vk, view);
	rvkCheckFatal(vk);

	rvkSelectPhysicalDevice(vk);
	rvkCheckFatal(vk);

	puglShowWindow(view);
	while (running) {
		puglPollEvents(world, -1);
		puglDispatchEvents(world);
	}

	puglFreeView(view);
	puglFreeWorld(world);
	rvkDestroy(vk);
	return 0;
}

//#if defined(_WIN32)
#if 0
int WINAPI WinMain(
		HINSTANCE hInstance,
		HINSTANCE hPrevInstance,
		LPSTR lpCmdLine,
		int nCmdShow
		)
{
	(void)hInstance;
	(void)hPrevInstance;
	(void)lpCmdLine;
	(void)nCmdShow;
	return main();
}
#endif