Monado OpenXR Runtime
Loading...
Searching...
No Matches
sdl_internal.h
Go to the documentation of this file.
1// Copyright 2020-2023, Collabora, Ltd.
2// Copyright 2026, NVIDIA CORPORATION.
3// SPDX-License-Identifier: BSL-1.0
4/*!
5 * @file
6 * @brief Internal header for SDL XR system.
7 * @author Jakob Bornecrantz <jakob@collabora.com>
8 * @ingroup sdl_test
9 */
10
11#include "xrt/xrt_system.h"
12#include "xrt/xrt_device.h"
13#include "xrt/xrt_instance.h"
14#include "xrt/xrt_tracking.h"
15#include "xrt/xrt_compositor.h"
16
17#include "util/u_pacing.h"
18#include "util/u_logging.h"
19#include "util/comp_base.h"
20#include "util/comp_swapchain.h"
21
22#include "SDL2/SDL.h"
23
24#include "ogl/ogl_api.h"
25
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31struct sdl_program;
32
33/*!
34 * Sub-class of @ref comp_swapchain, used to do OpenGL rendering.
35 *
36 * @ingroup sdl_test
37 */
39{
40 struct comp_swapchain base;
41
42 //! Pointer back to main program.
43 struct sdl_program *sp;
44
45 //! Cached width and height.
46 int w, h;
47
48 //! Number of textures in base.base.base.image_count.
50
51 //! Same number of images as textures.
53};
54
55/*!
56 * Tracking frame state.
57 *
58 * @ingroup sdl_test
59 */
61{
62 int64_t id;
63 uint64_t predicted_display_time_ns;
64 uint64_t desired_present_time_ns;
65 uint64_t present_slop_ns;
66};
67
68/*!
69 * Split out for convenience.
70 *
71 * This ultimately implements @ref xrt_compositor_native but does so by
72 * extending @ref comp_base, similar to how @ref comp_compositor works.
73 *
74 * @extends comp_base
75 * @ingroup sdl_test
76 */
78{
79 //! Base native compositor.
81
82 //! The supported view configurations
83 struct xrt_view_config view_configs[XRT_MAX_COMPOSITOR_VIEW_CONFIGS_COUNT];
84
85 //! Pacing helper to drive us forward.
87
88 struct
89 {
90 //! Frame interval that we are using.
92 } settings;
93
94 // Kept here for convenience.
95 struct xrt_system_compositor_info sys_info;
96
97
98 //! @todo Insert your own required members here
99
100 struct
101 {
102 struct sdl_comp_frame waited;
103 struct sdl_comp_frame rendering;
105};
106
107struct sdl_program_plus;
108
109/*!
110 * C base class for the SDL program.
111 *
112 * @implements xrt_instance
113 * @implements xrt_device
114 * @implements xrt_system_devices
115 * @extends sdl_compositor
116 * @ingroup sdl_test
117 */
119{
120 //! Base class for devices.
122
123 //! Instance base.
125
126 //! System, implemented using base code.
127 struct b_system *bsys;
128
129 //! System devices base.
131
132 //! Space overseer, implemented for now using helper code.
134
135 //! SDL compositor struct.
137
138 //! Created system compositor.
140
141 //! Inputs exposed by the SDL device.
142 struct xrt_input inputs[1];
143
144 //! HMD parts exposed by the SDL device to become a HMD.
146
147 //! Tracking origin that the device is located in.
149
150 //! The current log level.
152
153 struct
154 {
155 struct
156 {
157 //! The pose of the head, only used for view space.
159 } head;
160
161 struct
162 {
163 //! Pose of each individual eye.
164 struct xrt_pose pose;
165
166 //! Fov of each individual eye.
167 struct xrt_fov fov;
168 } left, right;
169 } state;
170
171 //! The main window.
172 SDL_Window *win;
173
174 //! Main OpenGL context.
175 SDL_GLContext ctx;
176
177 //! Protects the OpenGL context.
179
180 //! Pointer back to the C++ part of the program.
182};
183
184
185static inline struct sdl_program *
186from_xinst(struct xrt_instance *xinst)
187{
188 return container_of(xinst, struct sdl_program, xinst_base);
189}
190
191static inline struct sdl_program *
192from_xsysd(struct xrt_system_devices *xsysd)
193{
194 return container_of(xsysd, struct sdl_program, xsysd_base);
195}
196
197static inline struct sdl_program *
198from_xdev(struct xrt_device *xdev)
199{
200 return container_of(xdev, struct sdl_program, xdev_base);
201}
202
203static inline struct sdl_program *
204from_comp(struct xrt_compositor *xc)
205{
206 return container_of(xc, struct sdl_program, c.base.base);
207}
208
209
210/*!
211 * Spew level logging.
212 *
213 * @relates sdl_program
214 * @ingroup sdl_test
215 */
216#define ST_TRACE(sp, ...) U_LOG_IFL_T(sp->log_level, __VA_ARGS__);
217
218/*!
219 * Debug level logging.
220 *
221 * @relates sdl_program
222 */
223#define ST_DEBUG(sp, ...) U_LOG_IFL_D(sp->log_level, __VA_ARGS__);
224
225/*!
226 * Info level logging.
227 *
228 * @relates sdl_program
229 * @ingroup sdl_test
230 */
231#define ST_INFO(sp, ...) U_LOG_IFL_I(sp->log_level, __VA_ARGS__);
232
233/*!
234 * Warn level logging.
235 *
236 * @relates sdl_program
237 * @ingroup sdl_test
238 */
239#define ST_WARN(sp, ...) U_LOG_IFL_W(sp->log_level, __VA_ARGS__);
240
241/*!
242 * Error level logging.
243 *
244 * @relates sdl_program
245 * @ingroup sdl_test
246 */
247#define ST_ERROR(sp, ...) U_LOG_IFL_E(sp->log_level, __VA_ARGS__);
248
249/*!
250 * Check for OpenGL errors, context needs to be current.
251 *
252 * @ingroup sdl_test
253 */
254#define CHECK_GL() \
255 do { \
256 GLint err = glGetError(); \
257 if (err != 0) { \
258 U_LOG_RAW("%s:%u: error: 0x%04x", __func__, __LINE__, err); \
259 } \
260 } while (false)
261
262/*!
263 * Makes the OpenGL context current in this thread, takes lock.
264 *
265 * @ingroup sdl_test
266 */
267static inline void
269{
270 os_mutex_lock(&sp->current_mutex);
271 SDL_GL_MakeCurrent(sp->win, sp->ctx);
272}
273
274/*!
275 * Unmakes the any OpenGL context current in this thread, releases the lock.
276 *
277 * @ingroup sdl_test
278 */
279static inline void
281{
282 SDL_GL_MakeCurrent(NULL, NULL);
283 os_mutex_unlock(&sp->current_mutex);
284}
285
286
287/*
288 *
289 * sdl_device.c
290 *
291 */
292
293/*!
294 * Init the @ref xrt_device sub struct.
295 *
296 * In sdl_device.c
297 *
298 * @ingroup sdl_test
299 */
300void
301sdl_device_init(struct sdl_program *sp);
302
303
304/*
305 *
306 * sdl_swapchain.c
307 *
308 */
309
310/*!
311 * Implementation of @ref xrt_compositor::create_swapchain.
312 *
313 * @ingroup sdl_test
314 */
317 const struct xrt_swapchain_create_info *info,
318 struct xrt_swapchain **out_xsc);
319
320/*!
321 * Implementation of @ref xrt_compositor::import_swapchain.
322 *
323 * @ingroup sdl_test
324 */
327 const struct xrt_swapchain_create_info *info,
328 struct xrt_image_native *native_images,
329 uint32_t native_image_count,
330 struct xrt_swapchain **out_xsc);
331
332
333/*
334 *
335 * sdl_compositor.c
336 *
337 */
338
339/*!
340 * Initializes the compositor part of the SDL program.
341 *
342 * @ingroup sdl_test
343 */
344void
346
347/*!
348 * Creates the system compositor that wraps the native compositor.
349 *
350 * @ingroup sdl_test
351 */
354
355
356/*
357 *
358 * sdl_instance.c
359 *
360 */
361
362/*!
363 * Init the @ref xrt_system (and @ref u_system) struct.
364 *
365 * @ingroup sdl_test
366 */
367void
368sdl_system_init(struct sdl_program *sp);
369
370/*!
371 * Init the @ref xrt_system_devices sub struct.
372 *
373 * @ingroup sdl_test
374 */
375void
377
378/*!
379 * Init the @ref xrt_instance sub struct.
380 *
381 * @ingroup sdl_test
382 */
383void
385
386
387/*
388 *
389 * sdl_program.cpp
390 *
391 */
392
393/*!
394 * Create the SDL program.
395 *
396 * @ingroup sdl_test
397 */
398struct sdl_program *
400
401/*!
402 * Render a frame, called by the compositor when layers have been committed.
403 *
404 * @ingroup sdl_test
405 */
406void
408
409/*!
410 * Destroy the SDL program.
411 *
412 * @ingroup sdl_test
413 */
414void
416
417
418#ifdef __cplusplus
419}
420#endif
Helper implementation for native compositors.
Independent swapchain implementation.
u_logging_level
Logging level enum.
Definition u_logging.h:45
#define XRT_MAX_SWAPCHAIN_IMAGES
Max swapchain images, artificial limit.
Definition xrt_limits.h:53
enum xrt_result xrt_result_t
Result type used across Monado.
#define container_of(ptr, type, field)
Get the holder from a pointer to a field.
Definition xrt_compiler.h:226
OpenGL API wrapper header.
void sdl_compositor_init(struct sdl_program *sp)
Initializes the compositor part of the SDL program.
Definition sdl_compositor.c:557
xrt_result_t sdl_compositor_create_system(struct sdl_program *sp, struct xrt_system_compositor **out_xsysc)
Creates the system compositor that wraps the native compositor.
Definition sdl_compositor.c:607
void sdl_system_init(struct sdl_program *sp)
Init the xrt_system (and u_system) struct.
Definition sdl_instance.c:126
void sdl_program_plus_destroy(struct sdl_program_plus *spp)
Destroy the SDL program.
Definition sdl_program.cpp:175
void sdl_instance_init(struct sdl_program *sp)
Init the xrt_instance sub struct.
Definition sdl_instance.c:167
void sdl_device_init(struct sdl_program *sp)
Init the xrt_device sub struct.
Definition sdl_device.c:71
void sdl_program_plus_render(struct sdl_program_plus *spp)
Render a frame, called by the compositor when layers have been committed.
Definition sdl_program.cpp:103
xrt_result_t sdl_swapchain_create(struct xrt_compositor *xc, const struct xrt_swapchain_create_info *info, struct xrt_swapchain **out_xsc)
Implementation of xrt_compositor::create_swapchain.
Definition sdl_swapchain.c:82
static void sdl_make_current(struct sdl_program *sp)
Makes the OpenGL context current in this thread, takes lock.
Definition sdl_internal.h:268
xrt_result_t sdl_swapchain_import(struct xrt_compositor *xc, const struct xrt_swapchain_create_info *info, struct xrt_image_native *native_images, uint32_t native_image_count, struct xrt_swapchain **out_xsc)
Implementation of xrt_compositor::import_swapchain.
Definition sdl_swapchain.c:120
void sdl_system_devices_init(struct sdl_program *sp)
Init the xrt_system_devices sub struct.
Definition sdl_instance.c:135
static void sdl_make_uncurrent(struct sdl_program *sp)
Unmakes the any OpenGL context current in this thread, releases the lock.
Definition sdl_internal.h:280
struct sdl_program * sdl_program_plus_create(void)
Create the SDL program.
Definition sdl_program.cpp:78
A helper to implement a xrt_system, takes care of multiplexing events to sessions.
Definition b_system.h:46
A simple compositor base that handles a lot of things for you.
Definition comp_base.h:69
struct xrt_compositor_native base
Base native compositor.
Definition comp_base.h:71
A swapchain that is almost a one to one mapping to a OpenXR swapchain.
Definition comp_swapchain.h:92
A wrapper around a native mutex.
Definition os_threading.h:69
Tracking frame state.
Definition sdl_internal.h:61
Split out for convenience.
Definition sdl_internal.h:78
struct sdl_compositor::@312 frame
uint64_t frame_interval_ns
Frame interval that we are using.
Definition sdl_internal.h:91
struct comp_base base
Base native compositor.
Definition sdl_internal.h:80
struct xrt_view_config view_configs[XRT_MAX_COMPOSITOR_VIEW_CONFIGS_COUNT]
The supported view configurations.
Definition sdl_internal.h:83
struct u_pacing_compositor * upc
Pacing helper to drive us forward.
Definition sdl_internal.h:86
C++ version of the sdl_program struct, where you place C++ only things.
Definition sdl_internal.hpp:25
C base class for the SDL program.
Definition sdl_internal.h:119
struct xrt_tracking_origin origin
Tracking origin that the device is located in.
Definition sdl_internal.h:148
struct xrt_system_compositor * xsysc
Created system compositor.
Definition sdl_internal.h:139
SDL_GLContext ctx
Main OpenGL context.
Definition sdl_internal.h:175
struct os_mutex current_mutex
Protects the OpenGL context.
Definition sdl_internal.h:178
struct b_system * bsys
System, implemented using base code.
Definition sdl_internal.h:127
struct xrt_device xdev_base
Base class for devices.
Definition sdl_internal.h:121
SDL_Window * win
The main window.
Definition sdl_internal.h:172
struct xrt_system_devices xsysd_base
System devices base.
Definition sdl_internal.h:130
struct xrt_fov fov
Fov of each individual eye.
Definition sdl_internal.h:167
struct xrt_input inputs[1]
Inputs exposed by the SDL device.
Definition sdl_internal.h:142
enum u_logging_level log_level
The current log level.
Definition sdl_internal.h:151
struct xrt_space_overseer * xso
Space overseer, implemented for now using helper code.
Definition sdl_internal.h:133
struct sdl_program_plus * spp
Pointer back to the C++ part of the program.
Definition sdl_internal.h:181
struct xrt_pose pose
The pose of the head, only used for view space.
Definition sdl_internal.h:158
struct xrt_hmd_parts hmd
HMD parts exposed by the SDL device to become a HMD.
Definition sdl_internal.h:145
struct sdl_compositor c
SDL compositor struct.
Definition sdl_internal.h:136
struct xrt_instance xinst_base
Instance base.
Definition sdl_internal.h:124
Sub-class of comp_swapchain, used to do OpenGL rendering.
Definition sdl_internal.h:39
int w
Cached width and height.
Definition sdl_internal.h:46
GLuint memory[XRT_MAX_SWAPCHAIN_IMAGES]
Same number of images as textures.
Definition sdl_internal.h:52
struct sdl_program * sp
Pointer back to main program.
Definition sdl_internal.h:43
GLuint textures[XRT_MAX_SWAPCHAIN_IMAGES]
Number of textures in base.base.base.image_count.
Definition sdl_internal.h:49
Compositor pacing helper interface.
Definition u_pacing.h:68
Common compositor client interface/base.
Definition xrt_compositor.h:1016
struct xrt_compositor_info info
Capabilities and recommended values information.
Definition xrt_compositor.h:1020
A single HMD or input device.
Definition xrt_device.h:310
Describes a projection matrix fov.
Definition xrt_defines.h:512
All of the device components that deals with interfacing to a users head.
Definition xrt_device.h:117
A single image of a swapchain based on native buffer handles.
Definition xrt_compositor.h:2186
A single named input, that sits on a xrt_device.
Definition xrt_device.h:188
This interface acts as a root object for Monado.
Definition xrt_instance.h:120
A pose composed of a position and orientation.
Definition xrt_defines.h:492
Object that oversees and manages spaces, one created for each XR system.
Definition xrt_space.h:97
Swapchain creation info.
Definition xrt_compositor.h:895
Common swapchain interface/base.
Definition xrt_compositor.h:564
Capabilities and information about the system compositor (and its wrapped native compositor,...
Definition xrt_compositor.h:2363
The system compositor handles composition for a system.
Definition xrt_compositor.h:2488
A collection of xrt_device, and an interface for identifying the roles they have been assigned.
Definition xrt_system.h:214
A tracking system or device origin.
Definition xrt_tracking.h:75
Definition xrt_compositor.h:2340
Basic logging functionality.
Shared pacing code.
Header declaring XRT graphics interfaces.
Header defining an xrt display or controller device.
Header for xrt_instance object.
Header for system objects.
Header defining the tracking system integration in Monado.