glfw/docs/vulkan.md

251 lines
8.9 KiB
Markdown
Raw Normal View History

2024-02-13 19:45:29 +00:00
# Vulkan guide {#vulkan_guide}
2017-11-17 03:34:18 +00:00
2016-02-17 21:03:12 +00:00
@tableofcontents
2020-04-30 19:21:40 +00:00
This guide is intended to fill the gaps between the official [Vulkan
resources](https://www.khronos.org/vulkan/) and the rest of the GLFW
2016-02-17 21:03:12 +00:00
documentation and is not a replacement for either. It assumes some familiarity
with Vulkan concepts like loaders, devices, queues and surfaces and leaves it to
the Vulkan documentation to explain the details of Vulkan functions.
To develop for Vulkan you should download the [LunarG Vulkan
SDK](https://vulkan.lunarg.com/) for your platform. Apart from headers and link
libraries, they also provide the validation layers necessary for development.
2016-02-17 21:03:12 +00:00
2020-04-30 19:21:40 +00:00
The [Vulkan Tutorial](https://vulkan-tutorial.com/) has more information on how
to use GLFW and Vulkan. The [Khronos Vulkan
Samples](https://github.com/KhronosGroup/Vulkan-Samples) also use GLFW, although
with a small framework in between.
For details on a specific Vulkan support function, see the @ref vulkan. There
2016-08-11 17:04:21 +00:00
are also guides for the other areas of the GLFW API.
- @ref intro_guide
- @ref window_guide
- @ref context_guide
- @ref monitor_guide
- @ref input_guide
2016-02-17 21:03:12 +00:00
2024-02-13 19:45:29 +00:00
## Finding the Vulkan loader {#vulkan_loader}
GLFW itself does not ever need to be linked against the Vulkan loader.
By default, GLFW will load the Vulkan loader dynamically at runtime via its standard name:
`vulkan-1.dll` on Windows, `libvulkan.so.1` on Linux and other Unix-like systems and
`libvulkan.1.dylib` on macOS.
@macos GLFW will also look up and search the `Frameworks` subdirectory of your
application bundle.
If your code is using a Vulkan loader with a different name or in a non-standard location
you will need to direct GLFW to it. Pass your version of `vkGetInstanceProcAddr` to @ref
glfwInitVulkanLoader before initializing GLFW and it will use that function for all Vulkan
entry point retrieval. This prevents GLFW from dynamically loading the Vulkan loader.
```c
glfwInitVulkanLoader(vkGetInstanceProcAddr);
```
@macos To make your application be redistributable you will need to set up the application
bundle according to the LunarG SDK documentation. This is explained in more detail in the
[SDK documentation for macOS](https://vulkan.lunarg.com/doc/sdk/latest/mac/getting_started.html).
2024-02-13 19:45:29 +00:00
## Including the Vulkan header file {#vulkan_include}
2016-02-17 21:03:12 +00:00
2021-10-21 21:49:03 +00:00
To have GLFW include the Vulkan header, define @ref GLFW_INCLUDE_VULKAN before including
2016-12-06 00:14:23 +00:00
the GLFW header.
2016-02-17 21:03:12 +00:00
```c
2016-02-17 21:03:12 +00:00
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
```
2016-02-17 21:03:12 +00:00
2016-08-08 23:34:56 +00:00
If you instead want to include the Vulkan header from a custom location or use
your own custom Vulkan header then do this before the GLFW header.
2016-02-17 21:03:12 +00:00
```c
2016-02-17 21:03:12 +00:00
#include <path/to/vulkan.h>
#include <GLFW/glfw3.h>
```
2016-02-17 21:03:12 +00:00
2021-10-21 21:49:03 +00:00
Unless a Vulkan header is included, either by the GLFW header or above it, the following
GLFW functions will not be declared, as depend on Vulkan types.
- @ref glfwInitVulkanLoader
2021-10-21 21:49:03 +00:00
- @ref glfwGetInstanceProcAddress
- @ref glfwGetPhysicalDevicePresentationSupport
- @ref glfwCreateWindowSurface
2016-02-17 21:03:12 +00:00
The `VK_USE_PLATFORM_*_KHR` macros do not need to be defined for the Vulkan part
of GLFW to work. Define them only if you are using these extensions directly.
2024-02-13 19:45:29 +00:00
## Querying for Vulkan support {#vulkan_support}
2016-02-17 21:03:12 +00:00
If you are linking directly against the Vulkan loader then you can skip this
section. The canonical desktop loader library exports all Vulkan core and
Khronos extension functions, allowing them to be called directly.
If you are loading the Vulkan loader dynamically instead of linking directly
2017-02-07 19:56:48 +00:00
against it, you can check for the availability of a loader and ICD with @ref
2016-02-17 21:03:12 +00:00
glfwVulkanSupported.
```c
2016-02-17 21:03:12 +00:00
if (glfwVulkanSupported())
{
// Vulkan is available, at least for compute
}
```
2016-02-17 21:03:12 +00:00
2017-02-07 19:56:48 +00:00
This function returns `GLFW_TRUE` if the Vulkan loader and any minimally
functional ICD was found.
2016-02-17 21:03:12 +00:00
2020-12-14 07:37:20 +00:00
If one or both were not found, calling any other Vulkan related GLFW function
2017-02-07 19:56:48 +00:00
will generate a @ref GLFW_API_UNAVAILABLE error.
2016-02-17 21:03:12 +00:00
2024-02-13 19:45:29 +00:00
### Querying Vulkan function pointers {#vulkan_proc}
2016-02-17 21:03:12 +00:00
To load any Vulkan core or extension function from the found loader, call @ref
glfwGetInstanceProcAddress. To load functions needed for instance creation,
pass `NULL` as the instance.
```c
2016-02-17 21:03:12 +00:00
PFN_vkCreateInstance pfnCreateInstance = (PFN_vkCreateInstance)
glfwGetInstanceProcAddress(NULL, "vkCreateInstance");
```
2016-02-17 21:03:12 +00:00
Once you have created an instance, you can load from it all other Vulkan core
functions and functions from any instance extensions you enabled.
```c
2016-02-17 21:03:12 +00:00
PFN_vkCreateDevice pfnCreateDevice = (PFN_vkCreateDevice)
glfwGetInstanceProcAddress(instance, "vkCreateDevice");
```
2016-02-17 21:03:12 +00:00
This function in turn calls `vkGetInstanceProcAddr`. If that fails, the
function falls back to a platform-specific query of the Vulkan loader (i.e.
`dlsym` or `GetProcAddress`). If that also fails, the function returns `NULL`.
For more information about `vkGetInstanceProcAddr`, see the Vulkan
documentation.
Vulkan also provides `vkGetDeviceProcAddr` for loading device-specific versions
of Vulkan function. This function can be retrieved from an instance with @ref
glfwGetInstanceProcAddress.
```c
2016-02-17 21:03:12 +00:00
PFN_vkGetDeviceProcAddr pfnGetDeviceProcAddr = (PFN_vkGetDeviceProcAddr)
glfwGetInstanceProcAddress(instance, "vkGetDeviceProcAddr");
```
2016-02-17 21:03:12 +00:00
Device-specific functions may execute a little faster, due to not having to
2016-02-17 21:03:12 +00:00
dispatch internally based on the device passed to them. For more information
about `vkGetDeviceProcAddr`, see the Vulkan documentation.
2024-02-13 19:45:29 +00:00
## Querying required Vulkan extensions {#vulkan_ext}
2016-02-17 21:03:12 +00:00
To do anything useful with Vulkan you need to create an instance. If you want
to use Vulkan to render to a window, you must enable the instance extensions
GLFW requires to create Vulkan surfaces.
To query the instance extensions required, call @ref
glfwGetRequiredInstanceExtensions.
```c
uint32_t count;
2016-02-17 21:03:12 +00:00
const char** extensions = glfwGetRequiredInstanceExtensions(&count);
```
2016-02-17 21:03:12 +00:00
These extensions must all be enabled when creating instances that are going to
be passed to @ref glfwGetPhysicalDevicePresentationSupport and @ref
glfwCreateWindowSurface. The set of extensions will vary depending on platform
and may also vary depending on graphics drivers and other factors.
If it fails it will return `NULL` and GLFW will not be able to create Vulkan
window surfaces. You can still use Vulkan for off-screen rendering and compute
work.
If successful the returned array will always include `VK_KHR_surface`, so if
you don't require any additional extensions you can pass this list directly to
the `VkInstanceCreateInfo` struct.
2016-02-17 21:03:12 +00:00
```c
2016-02-17 21:03:12 +00:00
VkInstanceCreateInfo ici;
memset(&ici, 0, sizeof(ici));
ici.enabledExtensionCount = count;
ici.ppEnabledExtensionNames = extensions;
...
```
2016-02-17 21:03:12 +00:00
Additional extensions may be required by future versions of GLFW. You should
check whether any extensions you wish to enable are already in the returned
array, as it is an error to specify an extension more than once in the
2017-11-17 03:34:18 +00:00
`VkInstanceCreateInfo` struct.
2016-02-17 21:03:12 +00:00
@macos MoltenVK is (as of July 2022) not yet a fully conformant implementation
of Vulkan. As of Vulkan SDK 1.3.216.0, this means you must also enable the
`VK_KHR_portability_enumeration` instance extension and set the
`VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR` bit in the instance creation
info flags for MoltenVK to show up in the list of physical devices. For more
information, see the Vulkan and MoltenVK documentation.
2016-02-17 21:03:12 +00:00
2024-02-13 19:45:29 +00:00
## Querying for Vulkan presentation support {#vulkan_present}
2016-02-17 21:03:12 +00:00
Not every queue family of every Vulkan device can present images to surfaces.
To check whether a specific queue family of a physical device supports image
presentation without first having to create a window and surface, call @ref
glfwGetPhysicalDevicePresentationSupport.
```c
2016-02-17 21:03:12 +00:00
if (glfwGetPhysicalDevicePresentationSupport(instance, physical_device, queue_family_index))
{
// Queue family supports image presentation
}
```
2016-02-17 21:03:12 +00:00
2017-11-17 03:34:18 +00:00
The `VK_KHR_surface` extension additionally provides the
2016-02-17 21:03:12 +00:00
`vkGetPhysicalDeviceSurfaceSupportKHR` function, which performs the same test on
an existing Vulkan surface.
2024-02-13 19:45:29 +00:00
## Creating the window {#vulkan_window}
2016-02-17 21:03:12 +00:00
Unless you will be using OpenGL or OpenGL ES with the same window as Vulkan,
there is no need to create a context. You can disable context creation with the
2016-12-06 00:14:23 +00:00
[GLFW_CLIENT_API](@ref GLFW_CLIENT_API_hint) hint.
2016-02-17 21:03:12 +00:00
```c
2016-02-17 21:03:12 +00:00
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
GLFWwindow* window = glfwCreateWindow(640, 480, "Window Title", NULL, NULL);
```
2016-02-17 21:03:12 +00:00
See @ref context_less for more information.
2024-02-13 19:45:29 +00:00
## Creating a Vulkan window surface {#vulkan_surface}
2016-02-17 21:03:12 +00:00
You can create a Vulkan surface (as defined by the `VK_KHR_surface` extension)
for a GLFW window with @ref glfwCreateWindowSurface.
```c
2016-02-17 21:03:12 +00:00
VkSurfaceKHR surface;
VkResult err = glfwCreateWindowSurface(instance, window, NULL, &surface);
if (err)
{
// Window surface creation failed
}
```
2016-02-17 21:03:12 +00:00
If an OpenGL or OpenGL ES context was created on the window, the context has
ownership of the presentation on the window and a Vulkan surface cannot be
created.
2016-02-17 21:03:12 +00:00
It is your responsibility to destroy the surface. GLFW does not destroy it for
you. Call `vkDestroySurfaceKHR` function from the same extension to destroy it.