vulkan.dox 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*!
  2. @page vulkan_guide Vulkan guide
  3. @tableofcontents
  4. This guide is intended to fill the gaps between the official [Vulkan
  5. resources](https://www.khronos.org/vulkan/) and the rest of the GLFW
  6. documentation and is not a replacement for either. It assumes some familiarity
  7. with Vulkan concepts like loaders, devices, queues and surfaces and leaves it to
  8. the Vulkan documentation to explain the details of Vulkan functions.
  9. To develop for Vulkan you should download the [LunarG Vulkan
  10. SDK](https://vulkan.lunarg.com/) for your platform. Apart from headers and link
  11. libraries, they also provide the validation layers necessary for development.
  12. The [Vulkan Tutorial](https://vulkan-tutorial.com/) has more information on how
  13. to use GLFW and Vulkan. The [Khronos Vulkan
  14. Samples](https://github.com/KhronosGroup/Vulkan-Samples) also use GLFW, although
  15. with a small framework in between.
  16. For details on a specific Vulkan support function, see the @ref vulkan. There
  17. are also guides for the other areas of the GLFW API.
  18. - @ref intro_guide
  19. - @ref window_guide
  20. - @ref context_guide
  21. - @ref monitor_guide
  22. - @ref input_guide
  23. @section vulkan_loader Linking against the Vulkan loader
  24. By default, GLFW will look for the Vulkan loader on demand at runtime via its
  25. standard name (`vulkan-1.dll` on Windows, `libvulkan.so.1` on Linux and other
  26. Unix-like systems and `libvulkan.1.dylib` on macOS). This means that GLFW does
  27. not need to be linked against the loader. However, it also means that if you
  28. are using the static library form of the Vulkan loader GLFW will either fail to
  29. find it or (worse) use the wrong one.
  30. The @ref GLFW_VULKAN_STATIC CMake option makes GLFW call the Vulkan loader
  31. directly instead of dynamically loading it at runtime. Not linking against the
  32. Vulkan loader will then be a compile-time error.
  33. @macos Because the Vulkan loader and ICD are not installed globally on macOS,
  34. you need to set up the application bundle according to the LunarG SDK
  35. documentation. This is explained in more detail in the
  36. [SDK documentation for macOS](https://vulkan.lunarg.com/doc/sdk/latest/mac/getting_started.html).
  37. @section vulkan_include Including the Vulkan and GLFW header files
  38. To include the Vulkan header, define @ref GLFW_INCLUDE_VULKAN before including
  39. the GLFW header.
  40. @code
  41. #define GLFW_INCLUDE_VULKAN
  42. #include <GLFW/glfw3.h>
  43. @endcode
  44. If you instead want to include the Vulkan header from a custom location or use
  45. your own custom Vulkan header then do this before the GLFW header.
  46. @code
  47. #include <path/to/vulkan.h>
  48. #include <GLFW/glfw3.h>
  49. @endcode
  50. Unless a Vulkan header is included, either by the GLFW header or above it, any
  51. GLFW functions that take or return Vulkan types will not be declared.
  52. The `VK_USE_PLATFORM_*_KHR` macros do not need to be defined for the Vulkan part
  53. of GLFW to work. Define them only if you are using these extensions directly.
  54. @section vulkan_support Querying for Vulkan support
  55. If you are linking directly against the Vulkan loader then you can skip this
  56. section. The canonical desktop loader library exports all Vulkan core and
  57. Khronos extension functions, allowing them to be called directly.
  58. If you are loading the Vulkan loader dynamically instead of linking directly
  59. against it, you can check for the availability of a loader and ICD with @ref
  60. glfwVulkanSupported.
  61. @code
  62. if (glfwVulkanSupported())
  63. {
  64. // Vulkan is available, at least for compute
  65. }
  66. @endcode
  67. This function returns `GLFW_TRUE` if the Vulkan loader and any minimally
  68. functional ICD was found.
  69. If one or both were not found, calling any other Vulkan related GLFW function
  70. will generate a @ref GLFW_API_UNAVAILABLE error.
  71. @subsection vulkan_proc Querying Vulkan function pointers
  72. To load any Vulkan core or extension function from the found loader, call @ref
  73. glfwGetInstanceProcAddress. To load functions needed for instance creation,
  74. pass `NULL` as the instance.
  75. @code
  76. PFN_vkCreateInstance pfnCreateInstance = (PFN_vkCreateInstance)
  77. glfwGetInstanceProcAddress(NULL, "vkCreateInstance");
  78. @endcode
  79. Once you have created an instance, you can load from it all other Vulkan core
  80. functions and functions from any instance extensions you enabled.
  81. @code
  82. PFN_vkCreateDevice pfnCreateDevice = (PFN_vkCreateDevice)
  83. glfwGetInstanceProcAddress(instance, "vkCreateDevice");
  84. @endcode
  85. This function in turn calls `vkGetInstanceProcAddr`. If that fails, the
  86. function falls back to a platform-specific query of the Vulkan loader (i.e.
  87. `dlsym` or `GetProcAddress`). If that also fails, the function returns `NULL`.
  88. For more information about `vkGetInstanceProcAddr`, see the Vulkan
  89. documentation.
  90. Vulkan also provides `vkGetDeviceProcAddr` for loading device-specific versions
  91. of Vulkan function. This function can be retrieved from an instance with @ref
  92. glfwGetInstanceProcAddress.
  93. @code
  94. PFN_vkGetDeviceProcAddr pfnGetDeviceProcAddr = (PFN_vkGetDeviceProcAddr)
  95. glfwGetInstanceProcAddress(instance, "vkGetDeviceProcAddr");
  96. @endcode
  97. Device-specific functions may execute a little bit faster, due to not having to
  98. dispatch internally based on the device passed to them. For more information
  99. about `vkGetDeviceProcAddr`, see the Vulkan documentation.
  100. @section vulkan_ext Querying required Vulkan extensions
  101. To do anything useful with Vulkan you need to create an instance. If you want
  102. to use Vulkan to render to a window, you must enable the instance extensions
  103. GLFW requires to create Vulkan surfaces.
  104. To query the instance extensions required, call @ref
  105. glfwGetRequiredInstanceExtensions.
  106. @code
  107. uint32_t count;
  108. const char** extensions = glfwGetRequiredInstanceExtensions(&count);
  109. @endcode
  110. These extensions must all be enabled when creating instances that are going to
  111. be passed to @ref glfwGetPhysicalDevicePresentationSupport and @ref
  112. glfwCreateWindowSurface. The set of extensions will vary depending on platform
  113. and may also vary depending on graphics drivers and other factors.
  114. If it fails it will return `NULL` and GLFW will not be able to create Vulkan
  115. window surfaces. You can still use Vulkan for off-screen rendering and compute
  116. work.
  117. If successful the returned array will always include `VK_KHR_surface`, so if
  118. you don't require any additional extensions you can pass this list directly to
  119. the `VkInstanceCreateInfo` struct.
  120. @code
  121. VkInstanceCreateInfo ici;
  122. memset(&ici, 0, sizeof(ici));
  123. ici.enabledExtensionCount = count;
  124. ici.ppEnabledExtensionNames = extensions;
  125. ...
  126. @endcode
  127. Additional extensions may be required by future versions of GLFW. You should
  128. check whether any extensions you wish to enable are already in the returned
  129. array, as it is an error to specify an extension more than once in the
  130. `VkInstanceCreateInfo` struct.
  131. @section vulkan_present Querying for Vulkan presentation support
  132. Not every queue family of every Vulkan device can present images to surfaces.
  133. To check whether a specific queue family of a physical device supports image
  134. presentation without first having to create a window and surface, call @ref
  135. glfwGetPhysicalDevicePresentationSupport.
  136. @code
  137. if (glfwGetPhysicalDevicePresentationSupport(instance, physical_device, queue_family_index))
  138. {
  139. // Queue family supports image presentation
  140. }
  141. @endcode
  142. The `VK_KHR_surface` extension additionally provides the
  143. `vkGetPhysicalDeviceSurfaceSupportKHR` function, which performs the same test on
  144. an existing Vulkan surface.
  145. @section vulkan_window Creating the window
  146. Unless you will be using OpenGL or OpenGL ES with the same window as Vulkan,
  147. there is no need to create a context. You can disable context creation with the
  148. [GLFW_CLIENT_API](@ref GLFW_CLIENT_API_hint) hint.
  149. @code
  150. glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
  151. GLFWwindow* window = glfwCreateWindow(640, 480, "Window Title", NULL, NULL);
  152. @endcode
  153. See @ref context_less for more information.
  154. @section vulkan_surface Creating a Vulkan window surface
  155. You can create a Vulkan surface (as defined by the `VK_KHR_surface` extension)
  156. for a GLFW window with @ref glfwCreateWindowSurface.
  157. @code
  158. VkSurfaceKHR surface;
  159. VkResult err = glfwCreateWindowSurface(instance, window, NULL, &surface);
  160. if (err)
  161. {
  162. // Window surface creation failed
  163. }
  164. @endcode
  165. If an OpenGL or OpenGL ES context was created on the window, the context has
  166. ownership of the presentation on the window and a Vulkan surface cannot be
  167. created.
  168. It is your responsibility to destroy the surface. GLFW does not destroy it for
  169. you. Call `vkDestroySurfaceKHR` function from the same extension to destroy it.
  170. */