vulkan.dox 8.1 KB

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