Platformer in OpenGL
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
7.1 KiB

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