summaryrefslogtreecommitdiff
path: root/main.c
blob: cf59f16282156f07c40fe0ee4a76921bc1fdbedc (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
/*
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.
*/

// TODO: Create debug report callback

#include <stdio.h>
#include <stdlib.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

struct RenderVulkan {
	/** Put `device` first in struct to reduce pointer arithmetic on most Vulkan functions */
	VkDevice device;
	VkInstance instance;
};

struct RenderVulkan *vk;

#if defined(_WIN32)
void getRequiredInstanceExtensions(unsigned *nRequired, const char **extensions)
{
	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
void getRequiredInstanceExtensions(unsigned *nRequired, const char **const extensions)
{
	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 createInstance(struct RenderVulkan *vk,
	const uint32_t nLayers, const char *const *layers,
	const uint32_t nAdditional, const char *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);
	appInfo.apiVersion = VK_MAKE_VERSION(1, 0, 0);	// MoltenVK for macOS only supports Vulkan 1.0

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

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

	getRequiredInstanceExtensions(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%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;
	if ((result = vkCreateInstance(&createInfo, NULL, &vk->instance))) {
		exit(result);
	}
	free(extensions);
}

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

int main()
{
	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]);

	vk = malloc(sizeof(*vk));
	createInstance(vk, nInstanceLayers, instanceLayers, nInstanceExtensions, instanceExtensions);
	printf("Created Vulkan Instance Successfully\n");
	destroyInstance(vk);
	free(vk);
	vk = NULL;
	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