vulkan.dox 8.4 KB

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