summaryrefslogtreecommitdiff
path: root/main.c
blob: 0d479b8a76cda4d9f92b27e3b0633ea27c79aad6 (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
/*
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>
#endif

#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_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;
	uint32_t graphicsIndex;
	VkDevice device;
};

#define ERRMSG_LEN	4096

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

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

const char *getErrMsg(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;
	}
}
#else
#define VULKAN_SONAME_LATEST	"libvulkan.so.1"
#include <dlfcn.h>
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);
}

/* 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;
	}
}
#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;
}

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))) {
		setErrMsg(vk, "Could not create Vulkan Instance: %d\n", result);
	}
	free(extensions);
	return result;
}

/** Must not be called until all derivative objects are destroyed first */
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 renderVulkanDestroy(struct RenderVulkan *vk)
{
	if (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 `getErrMsg`. It MUST then be destroyed via
 * `renderVulkanDestroy`.
 * */
struct RenderVulkan *renderVulkanCreate()
{
	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) {
		setErrMsg(vk, "Error loading Vulkan shared library:\n%s\n", appDlerror());
		return vk;
	}

	loadVulkanGetInstanceProcAddrFunc(vk->api->handle, &vk->api->vkGetInstanceProcAddr);
	if (!vk->api->vkGetInstanceProcAddr) {
		setErrMsg(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) {
		setErrMsg(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) {
		setErrMsg(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) {
		setErrMsg(vk, "No debug reporter destroy function loaded for corresponding create function\n");
		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) {
		setErrMsg(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) {
		setErrMsg(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) {
		setErrMsg(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) {
		setErrMsg(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) {
		setErrMsg(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) {
		setErrMsg(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))) {
			setErrMsg(vk, "Could not create debug reporter: %d", result);
			return vk;
		}
	}

	return vk;
}

int isDeviceSuitable(struct RenderVulkan *vk, VkPhysicalDevice pd)
{
	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 graphicsIndex;
	for (graphicsIndex = 0; graphicsIndex < nQueueFamilies; ++graphicsIndex) {
		if (queueProperties[graphicsIndex].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
			VkBool32 canSurface;
			vk->api->vkGetPhysicalDeviceSurfaceSupportKHR(pd, graphicsIndex, vk->surface, &canSurface);
			if (canSurface) break;
		}
	}
	if (graphicsIndex >= nQueueFamilies) {
		/* Some esoteric devices may have separate graphics and present queue families, or none at all.
		 * An example would be a device without any video output plugs.
		 * We only support graphics and present on the same queue family.
		 */
		return 0;
	}
	vk->graphicsIndex = graphicsIndex;
	return 1;
}

void selectPhysicalDevice(struct RenderVulkan *vk)
{
	if (!vk->surface) {
		setErrMsg(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) {
			setErrMsg(vk, strErrEnum, result);
			return;
		} else {
			fprintf(stderr, "%s\n", strWarnEnum);
		}
	}
	if (!nDevices) {
		setErrMsg(vk, "No physical devices found");
		return;
	}
	VkPhysicalDevice *devices = malloc(nDevices * sizeof(*devices));
	if ((result = vk->api->vkEnumeratePhysicalDevices(vk->instance, &nDevices, devices))) {
		if (result != VK_INCOMPLETE) {
			setErrMsg(vk, strErrEnum, result);
			goto done;
		} else {
			fprintf(stderr, "%s\n", strWarnEnum);
		}
	}

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

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

int main()
{
	const char *errMsg = NULL;
	struct RenderVulkan *vk = renderVulkanCreate();
	rvkCheckFatal(vk);

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

	selectPhysicalDevice(vk);
	rvkCheckFatal(vk);

	renderVulkanDestroy(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