SDL_openxr.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. The OpenXR system ID is pulled from the passed GPU context.
  68. *
  69. * \param device a GPU context.
  70. * \param createinfo the create info for the OpenXR session, sans the system ID.
  71. * \param session a pointer filled in with an OpenXR session created for the given device.
  72. * \returns the result of the call.
  73. *
  74. * \sa SDL_CreateGPUDeviceWithProperties
  75. */
  76. extern SDL_DECLSPEC XrResult SDLCALL SDL_CreateGPUXRSession(SDL_GPUDevice *device, const XrSessionCreateInfo *createinfo, XrSession *session);
  77. /**
  78. * Queries the GPU device for supported XR swapchain image formats.
  79. *
  80. * The returned pointer should be allocated with SDL_malloc() and will be
  81. * passed to SDL_free().
  82. *
  83. * \param device a GPU context.
  84. * \param session an OpenXR session created for the given device.
  85. * \param num_formats a pointer filled with the number of supported XR swapchain formats.
  86. * \returns a 0 terminated array of supported formats or NULL on failure;
  87. * call SDL_GetError() for more information. This should be freed
  88. * with SDL_free() when it is no longer needed.
  89. *
  90. * \sa SDL_CreateGPUXRSwapchain
  91. */
  92. extern SDL_DECLSPEC SDL_GPUTextureFormat * SDLCALL SDL_GetGPUXRSwapchainFormats(SDL_GPUDevice *device, XrSession session, int *num_formats);
  93. /**
  94. * Creates an OpenXR swapchain.
  95. *
  96. * The array returned via `textures` is sized according to
  97. *`xrEnumerateSwapchainImages`, and thus should only be accessed via index
  98. * values returned from `xrAcquireSwapchainImage`.
  99. *
  100. * Applications are still allowed to call `xrEnumerateSwapchainImages` on the
  101. * returned XrSwapchain if they need to get the exact size of the array.
  102. *
  103. * \param device a GPU context.
  104. * \param session an OpenXR session created for the given device.
  105. * \param createinfo the create info for the OpenXR swapchain, sans the format.
  106. * \param format a supported format for the OpenXR swapchain.
  107. * \param swapchain a pointer filled in with the created OpenXR swapchain.
  108. * \param textures a pointer filled in with the array of created swapchain images.
  109. * \returns the result of the call.
  110. *
  111. * \sa SDL_CreateGPUDeviceWithProperties
  112. * \sa SDL_CreateGPUXRSession
  113. * \sa SDL_GetGPUXRSwapchainFormats
  114. * \sa SDL_DestroyGPUXRSwapchain
  115. */
  116. extern SDL_DECLSPEC XrResult SDLCALL SDL_CreateGPUXRSwapchain(
  117. SDL_GPUDevice *device,
  118. XrSession session,
  119. const XrSwapchainCreateInfo *createinfo,
  120. SDL_GPUTextureFormat format,
  121. XrSwapchain *swapchain,
  122. SDL_GPUTexture ***textures);
  123. /**
  124. * Destroys and OpenXR swapchain previously returned by SDL_CreateGPUXRSwapchain.
  125. *
  126. * \param device a GPU context.
  127. * \param swapchain a swapchain previously returned by SDL_CreateGPUXRSwapchain.
  128. * \param swapchainImages an array of swapchain images returned by the same call to SDL_CreateGPUXRSwapchain.
  129. * \returns the result of the call.
  130. *
  131. * \sa SDL_CreateGPUDeviceWithProperties
  132. * \sa SDL_CreateGPUXRSession
  133. * \sa SDL_CreateGPUXRSwapchain
  134. */
  135. extern SDL_DECLSPEC XrResult SDLCALL SDL_DestroyGPUXRSwapchain(SDL_GPUDevice *device, XrSwapchain swapchain, SDL_GPUTexture **swapchainImages);
  136. /**
  137. * Dynamically load the OpenXR loader. This can be called at any time.
  138. *
  139. * SDL keeps a reference count of the OpenXR loader, calling this function multiple
  140. * times will increment that count, rather than loading the library multiple times.
  141. *
  142. * If not called, this will be implicitly called when creating a GPU device with OpenXR.
  143. *
  144. * This function will use the platform default OpenXR loader name,
  145. * unless the `SDL_HINT_OPENXR_LIBRARY` hint is set.
  146. *
  147. * \returns true on success or false on failure; call SDL_GetError() for more
  148. * information.
  149. *
  150. * \threadsafety This function is not thread safe.
  151. *
  152. * \sa SDL_HINT_OPENXR_LIBRARY
  153. */
  154. extern SDL_DECLSPEC bool SDLCALL SDL_OpenXR_LoadLibrary(void);
  155. /**
  156. * Unload the OpenXR loader previously loaded by SDL_OpenXR_LoadLibrary.
  157. *
  158. * SDL keeps a reference count of the OpenXR loader, calling this function will decrement that count.
  159. * Once the reference count reaches zero, the library is unloaded.
  160. *
  161. * \threadsafety This function is not thread safe.
  162. */
  163. extern SDL_DECLSPEC void SDLCALL SDL_OpenXR_UnloadLibrary(void);
  164. /**
  165. * Get the address of the `xrGetInstanceProcAddr` function.
  166. *
  167. * This should be called after either calling SDL_OpenXR_LoadLibrary() or
  168. * creating an OpenXR SDL_GPUDevice.
  169. *
  170. * The actual type of the returned function pointer is PFN_xrGetInstanceProcAddr,
  171. * but that isn't always available. You should include the OpenXR headers before this header,
  172. * or cast the return value of this function to the correct type.
  173. *
  174. * \returns the function pointer for `xrGetInstanceProcAddr` or NULL on
  175. * failure; call SDL_GetError() for more information.
  176. */
  177. extern SDL_DECLSPEC PFN_xrGetInstanceProcAddr SDLCALL SDL_OpenXR_GetXrGetInstanceProcAddr(void);
  178. /* Ends C function definitions when using C++ */
  179. #ifdef __cplusplus
  180. }
  181. #endif
  182. #include <SDL3/SDL_close_code.h>
  183. #endif /* SDL_openxr_h_ */