vulkan.c 10 KB
Newer Older
Camilla Berglund's avatar
Camilla Berglund committed
1
//========================================================================
Camilla Berglund's avatar
Camilla Berglund committed
2
// GLFW 3.3 - www.glfw.org
Camilla Berglund's avatar
Camilla Berglund committed
3
4
//------------------------------------------------------------------------
// Copyright (c) 2002-2006 Marcus Geelnard
Camilla Berglund's avatar
Camilla Berglund committed
5
// Copyright (c) 2006-2016 Camilla Berglund <elmindreda@glfw.org>
Camilla Berglund's avatar
Camilla Berglund committed
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
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
//    claim that you wrote the original software. If you use this software
//    in a product, an acknowledgment in the product documentation would
//    be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
//    be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
//    distribution.
//
//========================================================================

#include "internal.h"

#include <assert.h>
#include <string.h>
#include <stdlib.h>


//////////////////////////////////////////////////////////////////////////
//////                       GLFW internal API                      //////
//////////////////////////////////////////////////////////////////////////

39
GLFWbool _glfwInitVulkan(void)
Camilla Berglund's avatar
Camilla Berglund committed
40
41
42
{
    VkResult err;
    VkExtensionProperties* ep;
43
    uint32_t i, count;
44
45

#if !defined(_GLFW_VULKAN_STATIC)
Camilla Berglund's avatar
Camilla Berglund committed
46
47
48
49
50
51
#if defined(_GLFW_WIN32)
    const char* name = "vulkan-1.dll";
#else
    const char* name = "libvulkan.so.1";
#endif

52
53
54
    if (_glfw.vk.available)
        return GLFW_TRUE;

Camilla Berglund's avatar
Camilla Berglund committed
55
56
    _glfw.vk.handle = _glfw_dlopen(name);
    if (!_glfw.vk.handle)
57
        return GLFW_FALSE;
Camilla Berglund's avatar
Camilla Berglund committed
58
59
60
61
62
63
64

    _glfw.vk.GetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)
        _glfw_dlsym(_glfw.vk.handle, "vkGetInstanceProcAddr");
    if (!_glfw.vk.GetInstanceProcAddr)
    {
        _glfwInputError(GLFW_API_UNAVAILABLE,
                        "Vulkan: Loader does not export vkGetInstanceProcAddr");
65
66
67

        _glfwTerminateVulkan();
        return GLFW_FALSE;
Camilla Berglund's avatar
Camilla Berglund committed
68
69
70
    }

    _glfw.vk.EnumerateInstanceExtensionProperties = (PFN_vkEnumerateInstanceExtensionProperties)
Camilla Berglund's avatar
Camilla Berglund committed
71
        vkGetInstanceProcAddr(NULL, "vkEnumerateInstanceExtensionProperties");
Camilla Berglund's avatar
Camilla Berglund committed
72
73
74
75
    if (!_glfw.vk.EnumerateInstanceExtensionProperties)
    {
        _glfwInputError(GLFW_API_UNAVAILABLE,
                        "Vulkan: Failed to retrieve vkEnumerateInstanceExtensionProperties");
76
77
78

        _glfwTerminateVulkan();
        return GLFW_FALSE;
Camilla Berglund's avatar
Camilla Berglund committed
79
    }
80
81
#endif // _GLFW_VULKAN_STATIC

Camilla Berglund's avatar
Camilla Berglund committed
82
83
84
85
86
87
    err = vkEnumerateInstanceExtensionProperties(NULL, &count, NULL);
    if (err)
    {
        _glfwInputError(GLFW_PLATFORM_ERROR,
                        "Vulkan: Failed to query instance extension count: %s",
                        _glfwGetVulkanResultString(err));
88
89
90

        _glfwTerminateVulkan();
        return GLFW_FALSE;
Camilla Berglund's avatar
Camilla Berglund committed
91
92
93
94
95
96
97
98
99
100
101
102
    }

    ep = calloc(count, sizeof(VkExtensionProperties));

    err = vkEnumerateInstanceExtensionProperties(NULL, &count, ep);
    if (err)
    {
        _glfwInputError(GLFW_PLATFORM_ERROR,
                        "Vulkan: Failed to query instance extensions: %s",
                        _glfwGetVulkanResultString(err));

        free(ep);
103
104
        _glfwTerminateVulkan();
        return GLFW_FALSE;
Camilla Berglund's avatar
Camilla Berglund committed
105
106
107
108
109
110
    }

    for (i = 0;  i < count;  i++)
    {
        if (strcmp(ep[i].extensionName, "VK_KHR_surface") == 0)
            _glfw.vk.KHR_surface = GLFW_TRUE;
111
        else if (strcmp(ep[i].extensionName, "VK_KHR_win32_surface") == 0)
Camilla Berglund's avatar
Camilla Berglund committed
112
            _glfw.vk.KHR_win32_surface = GLFW_TRUE;
113
        else if (strcmp(ep[i].extensionName, "VK_KHR_xlib_surface") == 0)
Camilla Berglund's avatar
Camilla Berglund committed
114
            _glfw.vk.KHR_xlib_surface = GLFW_TRUE;
115
        else if (strcmp(ep[i].extensionName, "VK_KHR_xcb_surface") == 0)
Camilla Berglund's avatar
Camilla Berglund committed
116
            _glfw.vk.KHR_xcb_surface = GLFW_TRUE;
117
        else if (strcmp(ep[i].extensionName, "VK_KHR_wayland_surface") == 0)
Camilla Berglund's avatar
Camilla Berglund committed
118
            _glfw.vk.KHR_wayland_surface = GLFW_TRUE;
119
        else if (strcmp(ep[i].extensionName, "VK_KHR_mir_surface") == 0)
Camilla Berglund's avatar
Camilla Berglund committed
120
121
122
123
124
125
126
            _glfw.vk.KHR_mir_surface = GLFW_TRUE;
    }

    free(ep);

    _glfw.vk.available = GLFW_TRUE;

127
    _glfwPlatformGetRequiredInstanceExtensions(_glfw.vk.extensions);
Camilla Berglund's avatar
Camilla Berglund committed
128

129
    return GLFW_TRUE;
Camilla Berglund's avatar
Camilla Berglund committed
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
}

void _glfwTerminateVulkan(void)
{
    if (_glfw.vk.handle)
        _glfw_dlclose(_glfw.vk.handle);
}

const char* _glfwGetVulkanResultString(VkResult result)
{
    switch (result)
    {
        case VK_SUCCESS:
            return "Success";
        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_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";
        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_SURFACE_LOST_KHR:
            return "A surface is no longer available";
        case VK_SUBOPTIMAL_KHR:
            return "A swapchain no longer matches the surface properties exactly, but can still be used";
        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";
        case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR:
            return "The requested window is already connected to a VkSurfaceKHR, or to some other non-Vulkan API";
        case VK_ERROR_VALIDATION_FAILED_EXT:
            return "A validation layer found an error";
        default:
Camilla Berglund's avatar
Cleanup    
Camilla Berglund committed
189
            return "ERROR: UNKNOWN VULKAN ERROR";
Camilla Berglund's avatar
Camilla Berglund committed
190
191
192
193
194
195
196
197
198
199
200
    }
}


//////////////////////////////////////////////////////////////////////////
//////                        GLFW public API                       //////
//////////////////////////////////////////////////////////////////////////

GLFWAPI int glfwVulkanSupported(void)
{
    _GLFW_REQUIRE_INIT_OR_RETURN(GLFW_FALSE);
201
    return _glfwInitVulkan();
Camilla Berglund's avatar
Camilla Berglund committed
202
203
}

204
GLFWAPI const char** glfwGetRequiredInstanceExtensions(uint32_t* count)
Camilla Berglund's avatar
Camilla Berglund committed
205
206
207
208
209
{
    *count = 0;

    _GLFW_REQUIRE_INIT_OR_RETURN(NULL);

210
    if (!_glfwInitVulkan())
211
212
    {
        _glfwInputError(GLFW_API_UNAVAILABLE, "Vulkan: API not available");
Camilla Berglund's avatar
Camilla Berglund committed
213
        return NULL;
214
    }
Camilla Berglund's avatar
Camilla Berglund committed
215

216
    *count = 2;
Camilla Berglund's avatar
Camilla Berglund committed
217
218
219
220
221
222
223
224
225
226
    return (const char**) _glfw.vk.extensions;
}

GLFWAPI GLFWvkproc glfwGetInstanceProcAddress(VkInstance instance,
                                              const char* procname)
{
    GLFWvkproc proc;

    _GLFW_REQUIRE_INIT_OR_RETURN(NULL);

227
    if (!_glfwInitVulkan())
228
229
    {
        _glfwInputError(GLFW_API_UNAVAILABLE, "Vulkan: API not available");
Camilla Berglund's avatar
Camilla Berglund committed
230
        return NULL;
231
    }
Camilla Berglund's avatar
Camilla Berglund committed
232
233
234
235
236
237
238
239
240
241
242
243
244
245

    proc = (GLFWvkproc) vkGetInstanceProcAddr(instance, procname);
    if (!proc)
        proc = (GLFWvkproc) _glfw_dlsym(_glfw.vk.handle, procname);

    return proc;
}

GLFWAPI int glfwGetPhysicalDevicePresentationSupport(VkInstance instance,
                                                     VkPhysicalDevice device,
                                                     uint32_t queuefamily)
{
    _GLFW_REQUIRE_INIT_OR_RETURN(GLFW_FALSE);

246
    if (!_glfwInitVulkan())
247
248
    {
        _glfwInputError(GLFW_API_UNAVAILABLE, "Vulkan: API not available");
Camilla Berglund's avatar
Camilla Berglund committed
249
        return GLFW_FALSE;
250
    }
Camilla Berglund's avatar
Camilla Berglund committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269

    if (!_glfw.vk.extensions)
    {
        _glfwInputError(GLFW_API_UNAVAILABLE,
                        "Vulkan: Window surface creation extensions not found");
        return GLFW_FALSE;
    }

    return _glfwPlatformGetPhysicalDevicePresentationSupport(instance,
                                                             device,
                                                             queuefamily);
}

GLFWAPI VkResult glfwCreateWindowSurface(VkInstance instance,
                                         GLFWwindow* handle,
                                         const VkAllocationCallbacks* allocator,
                                         VkSurfaceKHR* surface)
{
    _GLFWwindow* window = (_GLFWwindow*) handle;
Camilla Berglund's avatar
Camilla Berglund committed
270
271
    assert(window != NULL);
    assert(surface != NULL);
Camilla Berglund's avatar
Camilla Berglund committed
272

Camilla Berglund's avatar
Camilla Berglund committed
273
274
275
276
    *surface = VK_NULL_HANDLE;

    _GLFW_REQUIRE_INIT_OR_RETURN(VK_ERROR_INITIALIZATION_FAILED);

277
    if (!_glfwInitVulkan())
278
279
    {
        _glfwInputError(GLFW_API_UNAVAILABLE, "Vulkan: API not available");
Camilla Berglund's avatar
Camilla Berglund committed
280
        return VK_ERROR_INITIALIZATION_FAILED;
281
    }
Camilla Berglund's avatar
Camilla Berglund committed
282
283
284
285
286
287
288
289
290
291
292

    if (!_glfw.vk.extensions)
    {
        _glfwInputError(GLFW_API_UNAVAILABLE,
                        "Vulkan: Window surface creation extensions not found");
        return VK_ERROR_EXTENSION_NOT_PRESENT;
    }

    return _glfwPlatformCreateWindowSurface(instance, window, allocator, surface);
}