vulkan.dox 8.2 KB

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