Initialize Vulkan API on demand

This commit is contained in:
Camilla Berglund 2016-07-20 15:11:02 +02:00
parent 2060de5ed0
commit 614debd13f
3 changed files with 31 additions and 30 deletions

View File

@ -130,8 +130,6 @@ GLFWAPI int glfwInit(void)
return GLFW_FALSE; return GLFW_FALSE;
} }
_glfwInitVulkan();
_glfw.monitors = _glfwPlatformGetMonitors(&_glfw.monitorCount); _glfw.monitors = _glfwPlatformGetMonitors(&_glfw.monitorCount);
_glfwInitialized = GLFW_TRUE; _glfwInitialized = GLFW_TRUE;

View File

@ -1034,7 +1034,7 @@ GLFWbool _glfwIsPrintable(int key);
/*! @ingroup utility /*! @ingroup utility
*/ */
void _glfwInitVulkan(void); GLFWbool _glfwInitVulkan(void);
/*! @ingroup utility /*! @ingroup utility
*/ */

View File

@ -36,7 +36,7 @@
////// GLFW internal API ////// ////// GLFW internal API //////
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
void _glfwInitVulkan(void) GLFWbool _glfwInitVulkan(void)
{ {
VkResult err; VkResult err;
VkExtensionProperties* ep; VkExtensionProperties* ep;
@ -47,9 +47,15 @@ void _glfwInitVulkan(void)
const char* name = "libvulkan.so.1"; const char* name = "libvulkan.so.1";
#endif #endif
if (_glfw.vk.available)
return GLFW_TRUE;
_glfw.vk.handle = _glfw_dlopen(name); _glfw.vk.handle = _glfw_dlopen(name);
if (!_glfw.vk.handle) if (!_glfw.vk.handle)
return; {
_glfwInputError(GLFW_API_UNAVAILABLE, "Vulkan: Loader not found");
return GLFW_FALSE;
}
_glfw.vk.GetInstanceProcAddr = (PFN_vkGetInstanceProcAddr) _glfw.vk.GetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)
_glfw_dlsym(_glfw.vk.handle, "vkGetInstanceProcAddr"); _glfw_dlsym(_glfw.vk.handle, "vkGetInstanceProcAddr");
@ -57,7 +63,9 @@ void _glfwInitVulkan(void)
{ {
_glfwInputError(GLFW_API_UNAVAILABLE, _glfwInputError(GLFW_API_UNAVAILABLE,
"Vulkan: Loader does not export vkGetInstanceProcAddr"); "Vulkan: Loader does not export vkGetInstanceProcAddr");
return;
_glfwTerminateVulkan();
return GLFW_FALSE;
} }
_glfw.vk.EnumerateInstanceExtensionProperties = (PFN_vkEnumerateInstanceExtensionProperties) _glfw.vk.EnumerateInstanceExtensionProperties = (PFN_vkEnumerateInstanceExtensionProperties)
@ -66,7 +74,9 @@ void _glfwInitVulkan(void)
{ {
_glfwInputError(GLFW_API_UNAVAILABLE, _glfwInputError(GLFW_API_UNAVAILABLE,
"Vulkan: Failed to retrieve vkEnumerateInstanceExtensionProperties"); "Vulkan: Failed to retrieve vkEnumerateInstanceExtensionProperties");
return;
_glfwTerminateVulkan();
return GLFW_FALSE;
} }
err = vkEnumerateInstanceExtensionProperties(NULL, &count, NULL); err = vkEnumerateInstanceExtensionProperties(NULL, &count, NULL);
@ -75,7 +85,9 @@ void _glfwInitVulkan(void)
_glfwInputError(GLFW_PLATFORM_ERROR, _glfwInputError(GLFW_PLATFORM_ERROR,
"Vulkan: Failed to query instance extension count: %s", "Vulkan: Failed to query instance extension count: %s",
_glfwGetVulkanResultString(err)); _glfwGetVulkanResultString(err));
return;
_glfwTerminateVulkan();
return GLFW_FALSE;
} }
ep = calloc(count, sizeof(VkExtensionProperties)); ep = calloc(count, sizeof(VkExtensionProperties));
@ -88,7 +100,8 @@ void _glfwInitVulkan(void)
_glfwGetVulkanResultString(err)); _glfwGetVulkanResultString(err));
free(ep); free(ep);
return; _glfwTerminateVulkan();
return GLFW_FALSE;
} }
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
@ -111,11 +124,13 @@ void _glfwInitVulkan(void)
_glfw.vk.available = GLFW_TRUE; _glfw.vk.available = GLFW_TRUE;
if (!_glfw.vk.KHR_surface) if (_glfw.vk.KHR_surface)
return; {
_glfw.vk.extensions =
_glfwPlatformGetRequiredInstanceExtensions(&_glfw.vk.extensionCount);
}
_glfw.vk.extensions = return GLFW_TRUE;
_glfwPlatformGetRequiredInstanceExtensions(&_glfw.vk.extensionCount);
} }
void _glfwTerminateVulkan(void) void _glfwTerminateVulkan(void)
@ -193,7 +208,7 @@ const char* _glfwGetVulkanResultString(VkResult result)
GLFWAPI int glfwVulkanSupported(void) GLFWAPI int glfwVulkanSupported(void)
{ {
_GLFW_REQUIRE_INIT_OR_RETURN(GLFW_FALSE); _GLFW_REQUIRE_INIT_OR_RETURN(GLFW_FALSE);
return _glfw.vk.available; return _glfwInitVulkan();
} }
GLFWAPI const char** glfwGetRequiredInstanceExtensions(uint32_t* count) GLFWAPI const char** glfwGetRequiredInstanceExtensions(uint32_t* count)
@ -202,11 +217,8 @@ GLFWAPI const char** glfwGetRequiredInstanceExtensions(uint32_t* count)
_GLFW_REQUIRE_INIT_OR_RETURN(NULL); _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
if (!_glfw.vk.available) if (!_glfwInitVulkan())
{
_glfwInputError(GLFW_API_UNAVAILABLE, "Vulkan: Loader not found");
return NULL; return NULL;
}
*count = _glfw.vk.extensionCount; *count = _glfw.vk.extensionCount;
return (const char**) _glfw.vk.extensions; return (const char**) _glfw.vk.extensions;
@ -219,11 +231,8 @@ GLFWAPI GLFWvkproc glfwGetInstanceProcAddress(VkInstance instance,
_GLFW_REQUIRE_INIT_OR_RETURN(NULL); _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
if (!_glfw.vk.available) if (!_glfwInitVulkan())
{
_glfwInputError(GLFW_API_UNAVAILABLE, "Vulkan: Loader not found");
return NULL; return NULL;
}
proc = (GLFWvkproc) vkGetInstanceProcAddr(instance, procname); proc = (GLFWvkproc) vkGetInstanceProcAddr(instance, procname);
if (!proc) if (!proc)
@ -238,11 +247,8 @@ GLFWAPI int glfwGetPhysicalDevicePresentationSupport(VkInstance instance,
{ {
_GLFW_REQUIRE_INIT_OR_RETURN(GLFW_FALSE); _GLFW_REQUIRE_INIT_OR_RETURN(GLFW_FALSE);
if (!_glfw.vk.available) if (!_glfwInitVulkan())
{
_glfwInputError(GLFW_API_UNAVAILABLE, "Vulkan: Loader not found");
return GLFW_FALSE; return GLFW_FALSE;
}
if (!_glfw.vk.extensions) if (!_glfw.vk.extensions)
{ {
@ -269,11 +275,8 @@ GLFWAPI VkResult glfwCreateWindowSurface(VkInstance instance,
_GLFW_REQUIRE_INIT_OR_RETURN(VK_ERROR_INITIALIZATION_FAILED); _GLFW_REQUIRE_INIT_OR_RETURN(VK_ERROR_INITIALIZATION_FAILED);
if (!_glfw.vk.available) if (!_glfwInitVulkan())
{
_glfwInputError(GLFW_API_UNAVAILABLE, "Vulkan: Loader not found");
return VK_ERROR_INITIALIZATION_FAILED; return VK_ERROR_INITIALIZATION_FAILED;
}
if (!_glfw.vk.extensions) if (!_glfw.vk.extensions)
{ {