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