SDL_openxr.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * # CategoryOpenXR
  20. *
  21. * Functions for creating OpenXR handles for SDL_gpu contexts.
  22. *
  23. * For the most part, OpenXR operates independent of SDL, but
  24. * the graphics initialization depends on direct support from SDL_gpu.
  25. *
  26. */
  27. #ifndef SDL_openxr_h_
  28. #define SDL_openxr_h_
  29. #include <SDL3/SDL_stdinc.h>
  30. #include <SDL3/SDL_gpu.h>
  31. #include <SDL3/SDL_begin_code.h>
  32. /* Set up for C function definitions, even when using C++ */
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. #if defined(OPENXR_H_)
  37. #define NO_SDL_OPENXR_TYPEDEFS 1
  38. #endif /* OPENXR_H_ */
  39. #if !defined(NO_SDL_OPENXR_TYPEDEFS)
  40. #define XR_NULL_HANDLE 0
  41. #if !defined(XR_DEFINE_HANDLE)
  42. #define XR_DEFINE_HANDLE(object) typedef Uint64 object;
  43. #endif /* XR_DEFINE_HANDLE */
  44. typedef enum XrStructureType {
  45. XR_TYPE_SESSION_CREATE_INFO = 8,
  46. XR_TYPE_SWAPCHAIN_CREATE_INFO = 9,
  47. } XrStructureType;
  48. XR_DEFINE_HANDLE(XrInstance)
  49. XR_DEFINE_HANDLE(XrSystemId)
  50. XR_DEFINE_HANDLE(XrSession)
  51. XR_DEFINE_HANDLE(XrSwapchain)
  52. typedef struct {
  53. XrStructureType type;
  54. const void* next;
  55. } XrSessionCreateInfo;
  56. typedef struct {
  57. XrStructureType type;
  58. const void* next;
  59. } XrSwapchainCreateInfo;
  60. typedef enum XrResult {
  61. XR_ERROR_FUNCTION_UNSUPPORTED = -7,
  62. XR_ERROR_HANDLE_INVALID = -12,
  63. } XrResult;
  64. #define PFN_xrGetInstanceProcAddr SDL_FunctionPointer
  65. #endif /* NO_SDL_OPENXR_TYPEDEFS */
  66. /**
  67. * Creates an OpenXR session.
  68. *
  69. * The OpenXR system ID is pulled from the passed GPU context.
  70. *
  71. * \param device a GPU context.
  72. * \param createinfo the create info for the OpenXR session, sans the system
  73. * ID.
  74. * \param session a pointer filled in with an OpenXR session created for the
  75. * given device.
  76. * \returns the result of the call.
  77. *
  78. * \since This function is available since SDL 3.6.0.
  79. *
  80. * \sa SDL_CreateGPUDeviceWithProperties
  81. */
  82. extern SDL_DECLSPEC XrResult SDLCALL SDL_CreateGPUXRSession(SDL_GPUDevice *device, const XrSessionCreateInfo *createinfo, XrSession *session);
  83. /**
  84. * Queries the GPU device for supported XR swapchain image formats.
  85. *
  86. * The returned pointer should be allocated with SDL_malloc() and will be
  87. * passed to SDL_free().
  88. *
  89. * \param device a GPU context.
  90. * \param session an OpenXR session created for the given device.
  91. * \param num_formats a pointer filled with the number of supported XR
  92. * swapchain formats.
  93. * \returns a 0 terminated array of supported formats or NULL on failure; call
  94. * SDL_GetError() for more information. This should be freed with
  95. * SDL_free() when it is no longer needed.
  96. *
  97. * \since This function is available since SDL 3.6.0.
  98. *
  99. * \sa SDL_CreateGPUXRSwapchain
  100. */
  101. extern SDL_DECLSPEC SDL_GPUTextureFormat * SDLCALL SDL_GetGPUXRSwapchainFormats(SDL_GPUDevice *device, XrSession session, int *num_formats);
  102. /**
  103. * Creates an OpenXR swapchain.
  104. *
  105. * The array returned via `textures` is sized according to
  106. * `xrEnumerateSwapchainImages`, and thus should only be accessed via index
  107. * values returned from `xrAcquireSwapchainImage`.
  108. *
  109. * Applications are still allowed to call `xrEnumerateSwapchainImages` on the
  110. * returned XrSwapchain if they need to get the exact size of the array.
  111. *
  112. * \param device a GPU context.
  113. * \param session an OpenXR session created for the given device.
  114. * \param createinfo the create info for the OpenXR swapchain, sans the
  115. * format.
  116. * \param format a supported format for the OpenXR swapchain.
  117. * \param swapchain a pointer filled in with the created OpenXR swapchain.
  118. * \param textures a pointer filled in with the array of created swapchain
  119. * images.
  120. * \returns the result of the call.
  121. *
  122. * \since This function is available since SDL 3.6.0.
  123. *
  124. * \sa SDL_CreateGPUDeviceWithProperties
  125. * \sa SDL_CreateGPUXRSession
  126. * \sa SDL_GetGPUXRSwapchainFormats
  127. * \sa SDL_DestroyGPUXRSwapchain
  128. */
  129. extern SDL_DECLSPEC XrResult SDLCALL SDL_CreateGPUXRSwapchain(
  130. SDL_GPUDevice *device,
  131. XrSession session,
  132. const XrSwapchainCreateInfo *createinfo,
  133. SDL_GPUTextureFormat format,
  134. XrSwapchain *swapchain,
  135. SDL_GPUTexture ***textures);
  136. /**
  137. * Destroys and OpenXR swapchain previously returned by
  138. * SDL_CreateGPUXRSwapchain.
  139. *
  140. * \param device a GPU context.
  141. * \param swapchain a swapchain previously returned by
  142. * SDL_CreateGPUXRSwapchain.
  143. * \param swapchainImages an array of swapchain images returned by the same
  144. * call to SDL_CreateGPUXRSwapchain.
  145. * \returns the result of the call.
  146. *
  147. * \since This function is available since SDL 3.6.0.
  148. *
  149. * \sa SDL_CreateGPUDeviceWithProperties
  150. * \sa SDL_CreateGPUXRSession
  151. * \sa SDL_CreateGPUXRSwapchain
  152. */
  153. extern SDL_DECLSPEC XrResult SDLCALL SDL_DestroyGPUXRSwapchain(SDL_GPUDevice *device, XrSwapchain swapchain, SDL_GPUTexture **swapchainImages);
  154. /**
  155. * Dynamically load the OpenXR loader.
  156. *
  157. * This can be called at any time.
  158. *
  159. * SDL keeps a reference count of the OpenXR loader, calling this function
  160. * multiple times will increment that count, rather than loading the library
  161. * multiple times.
  162. *
  163. * If not called, this will be implicitly called when creating a GPU device
  164. * with OpenXR.
  165. *
  166. * This function will use the platform default OpenXR loader name, unless the
  167. * `SDL_HINT_OPENXR_LIBRARY` hint is set.
  168. *
  169. * \returns true on success or false on failure; call SDL_GetError() for more
  170. * information.
  171. *
  172. * \threadsafety This function is not thread safe.
  173. *
  174. * \since This function is available since SDL 3.6.0.
  175. *
  176. * \sa SDL_HINT_OPENXR_LIBRARY
  177. */
  178. extern SDL_DECLSPEC bool SDLCALL SDL_OpenXR_LoadLibrary(void);
  179. /**
  180. * Unload the OpenXR loader previously loaded by SDL_OpenXR_LoadLibrary.
  181. *
  182. * SDL keeps a reference count of the OpenXR loader, calling this function
  183. * will decrement that count. Once the reference count reaches zero, the
  184. * library is unloaded.
  185. *
  186. * \threadsafety This function is not thread safe.
  187. *
  188. * \since This function is available since SDL 3.6.0.
  189. */
  190. extern SDL_DECLSPEC void SDLCALL SDL_OpenXR_UnloadLibrary(void);
  191. /**
  192. * Get the address of the `xrGetInstanceProcAddr` function.
  193. *
  194. * This should be called after either calling SDL_OpenXR_LoadLibrary() or
  195. * creating an OpenXR SDL_GPUDevice.
  196. *
  197. * The actual type of the returned function pointer is
  198. * PFN_xrGetInstanceProcAddr, but that isn't always available. You should
  199. * include the OpenXR headers before this header, or cast the return value of
  200. * this function to the correct type.
  201. *
  202. * \returns the function pointer for `xrGetInstanceProcAddr` or NULL on
  203. * failure; call SDL_GetError() for more information.
  204. *
  205. * \since This function is available since SDL 3.6.0.
  206. */
  207. extern SDL_DECLSPEC PFN_xrGetInstanceProcAddr SDLCALL SDL_OpenXR_GetXrGetInstanceProcAddr(void);
  208. /* Ends C function definitions when using C++ */
  209. #ifdef __cplusplus
  210. }
  211. #endif
  212. #include <SDL3/SDL_close_code.h>
  213. #endif /* SDL_openxr_h_ */