Blender V5.3
GHOST_Xr.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "GHOST_XrContext.hh"
12#include "GHOST_XrException.hh"
13#include "GHOST_XrSession.hh"
14
15#include "GHOST_Xr-api.hh"
16
18
19#define GHOST_XR_CAPI_CALL(call, ctx) \
20 try { \
21 call; \
22 } \
23 catch (GHOST_XrException & e) { \
24 (ctx)->dispatchErrorMessage(&e); \
25 }
26
27#define GHOST_XR_CAPI_CALL_RET(call, ctx) \
28 try { \
29 return call; \
30 } \
31 catch (GHOST_XrException & e) { \
32 (ctx)->dispatchErrorMessage(&e); \
33 }
34
35void GHOST_XrErrorHandler(GHOST_XrErrorHandlerFn handler_fn, void *customdata)
36{
37 GHOST_XrContext::setErrorHandler(handler_fn, customdata);
38}
39
40GHOST_IXrContext *GHOST_XrContextCreate(const GHOST_XrContextCreateInfo *create_info)
41{
42 auto xr_context = std::make_unique<GHOST_XrContext>(create_info);
43
44 /* TODO GHOST_XrContext's should probably be owned by the GHOST_System, which will handle context
45 * creation and destruction. Try-catch logic can be moved to C-API then. */
46 try {
47 xr_context->initialize(create_info);
48 }
49 catch (GHOST_XrException &e) {
50 xr_context->dispatchErrorMessage(&e);
51 return nullptr;
52 }
53
54 /* Give ownership to the caller. */
55 return xr_context.release();
56}
57
59{
60 delete xr_context;
61}
62
63void GHOST_XrSessionStart(GHOST_IXrContext *xr_context, const GHOST_XrSessionBeginInfo *begin_info)
64{
65 GHOST_XR_CAPI_CALL(xr_context->startSession(begin_info), xr_context);
66}
67
69{
70 GHOST_XR_CAPI_CALL(xr_context->endSession(), xr_context);
71}
72
73void GHOST_XrSessionDrawViews(GHOST_IXrContext *xr_context, void *draw_customdata)
74{
75 GHOST_XR_CAPI_CALL(xr_context->drawSessionViews(draw_customdata), xr_context);
76}
77
79{
80 GHOST_XR_CAPI_CALL_RET(xr_context->isSessionRunning(), xr_context);
81 return 0; /* Only reached if exception is thrown. */
82}
83
85 GHOST_XrGraphicsContextBindFn bind_fn,
86 GHOST_XrGraphicsContextUnbindFn unbind_fn)
87{
88 GHOST_XR_CAPI_CALL(xr_context->setGraphicsContextBindFuncs(bind_fn, unbind_fn), xr_context);
89}
90
91void GHOST_XrDrawViewFunc(GHOST_IXrContext *xr_context, GHOST_XrDrawViewFn draw_view_fn)
92{
93 GHOST_XR_CAPI_CALL(xr_context->setDrawViewFunc(draw_view_fn), xr_context);
94}
95
97 GHOST_XrPassthroughEnabledFn passthrough_enabled_fn)
98{
99 GHOST_XR_CAPI_CALL(xr_context->setPassthroughEnabledFunc(passthrough_enabled_fn), xr_context);
100}
101
103 GHOST_XrDisablePassthroughFn disable_passthrough_fn)
104{
105 GHOST_XR_CAPI_CALL(xr_context->setDisablePassthroughFunc(disable_passthrough_fn), xr_context);
106}
107
109{
110 GHOST_XR_CAPI_CALL_RET(xr_context->needsUpsideDownDrawing(), xr_context);
111 return 0; /* Only reached if exception is thrown. */
112}
113
114int GHOST_XrCreateActionSet(GHOST_IXrContext *xr_context, const GHOST_XrActionSetInfo *info)
115{
116 GHOST_XrSession *xr_session = xr_context->getSession();
117 GHOST_XR_CAPI_CALL_RET(xr_session->createActionSet(*info), xr_context);
118 return 0;
119}
120
121void GHOST_XrDestroyActionSet(GHOST_IXrContext *xr_context, const char *action_set_name)
122{
123 GHOST_XrSession *xr_session = xr_context->getSession();
124 GHOST_XR_CAPI_CALL(xr_session->destroyActionSet(action_set_name), xr_context);
125}
126
128 const char *action_set_name,
129 uint32_t count,
130 const GHOST_XrActionInfo *infos)
131{
132 GHOST_XrSession *xr_session = xr_context->getSession();
133 GHOST_XR_CAPI_CALL_RET(xr_session->createActions(action_set_name, count, infos), xr_context);
134 return 0;
135}
136
138 const char *action_set_name,
139 uint32_t count,
140 const char *const *action_names)
141{
142 GHOST_XrSession *xr_session = xr_context->getSession();
143 GHOST_XR_CAPI_CALL(xr_session->destroyActions(action_set_name, count, action_names), xr_context);
144}
145
147 const char *action_set_name,
148 uint32_t count,
149 const GHOST_XrActionProfileInfo *infos)
150{
151 GHOST_XrSession *xr_session = xr_context->getSession();
152 GHOST_XR_CAPI_CALL_RET(xr_session->createActionBindings(action_set_name, count, infos),
153 xr_context);
154 return 0;
155}
156
158 const char *action_set_name,
159 uint32_t count,
160 const char *const *action_names,
161 const char *const *profile_paths)
162{
163 GHOST_XrSession *xr_session = xr_context->getSession();
165 xr_session->destroyActionBindings(action_set_name, count, action_names, profile_paths),
166 xr_context);
167}
168
170{
171 GHOST_XrSession *xr_session = xr_context->getSession();
172 GHOST_XR_CAPI_CALL_RET(xr_session->attachActionSets(), xr_context);
173 return 0;
174}
175
176int GHOST_XrSyncActions(GHOST_IXrContext *xr_context, const char *action_set_name)
177{
178 GHOST_XrSession *xr_session = xr_context->getSession();
179 GHOST_XR_CAPI_CALL_RET(xr_session->syncActions(action_set_name), xr_context);
180 return 0;
181}
182
184 const char *action_set_name,
185 const char *action_name,
186 const char *subaction_path,
187 const int64_t *duration,
188 const float *frequency,
189 const float *amplitude)
190{
191 GHOST_XrSession *xr_session = xr_context->getSession();
193 xr_session->applyHapticAction(
194 action_set_name, action_name, subaction_path, *duration, *frequency, *amplitude),
195 xr_context);
196 return 0;
197}
198
200 const char *action_set_name,
201 const char *action_name,
202 const char *subaction_path)
203{
204 GHOST_XrSession *xr_session = xr_context->getSession();
205 GHOST_XR_CAPI_CALL(xr_session->stopHapticAction(action_set_name, action_name, subaction_path),
206 xr_context);
207}
208
209void *GHOST_XrGetActionSetCustomdata(GHOST_IXrContext *xr_context, const char *action_set_name)
210{
211 GHOST_XrSession *xr_session = xr_context->getSession();
212 GHOST_XR_CAPI_CALL_RET(xr_session->getActionSetCustomdata(action_set_name), xr_context);
213 return nullptr;
214}
215
217 const char *action_set_name,
218 const char *action_name)
219{
220 GHOST_XrSession *xr_session = xr_context->getSession();
221 GHOST_XR_CAPI_CALL_RET(xr_session->getActionCustomdata(action_set_name, action_name),
222 xr_context);
223 return nullptr;
224}
225
226uint GHOST_XrGetActionCount(GHOST_IXrContext *xr_context, const char *action_set_name)
227{
228 GHOST_XrSession *xr_session = xr_context->getSession();
229 GHOST_XR_CAPI_CALL_RET(xr_session->getActionCount(action_set_name), xr_context);
230 return 0;
231}
232
234 const char *action_set_name,
235 void **r_customdata_array)
236{
237 GHOST_XrSession *xr_session = xr_context->getSession();
238 GHOST_XR_CAPI_CALL(xr_session->getActionCustomdataArray(action_set_name, r_customdata_array),
239 xr_context);
240}
241
242int GHOST_XrLoadControllerModel(GHOST_IXrContext *xr_context, const char *subaction_path)
243{
244 GHOST_XrSession *xr_session = xr_context->getSession();
245 GHOST_XR_CAPI_CALL_RET(xr_session->loadControllerModel(subaction_path), xr_context);
246 return 0;
247}
248
249void GHOST_XrUnloadControllerModel(GHOST_IXrContext *xr_context, const char *subaction_path)
250{
251 GHOST_XrSession *xr_session = xr_context->getSession();
252 GHOST_XR_CAPI_CALL(xr_session->unloadControllerModel(subaction_path), xr_context);
253}
254
256 const char *subaction_path)
257{
258 GHOST_XrSession *xr_session = xr_context->getSession();
259 GHOST_XR_CAPI_CALL_RET(xr_session->updateControllerModelComponents(subaction_path), xr_context);
260 return 0;
261}
262
264 const char *subaction_path,
265 GHOST_XrControllerModelData *r_data)
266{
267 GHOST_XrSession *xr_session = xr_context->getSession();
268 GHOST_XR_CAPI_CALL_RET(xr_session->getControllerModelData(subaction_path, *r_data), xr_context);
269 return 0;
270}
#define CLG_LOGREF_DECLARE_GLOBAL(var, id)
Definition CLG_log.h:139
Abstraction for XR (VR, AR, MR, ..) access via OpenXR.
void GHOST_XrDestroyActionSet(GHOST_IXrContext *xr_context, const char *action_set_name)
Definition GHOST_Xr.cc:121
int GHOST_XrCreateActions(GHOST_IXrContext *xr_context, const char *action_set_name, uint32_t count, const GHOST_XrActionInfo *infos)
Definition GHOST_Xr.cc:127
void GHOST_XrDestroyActions(GHOST_IXrContext *xr_context, const char *action_set_name, uint32_t count, const char *const *action_names)
Definition GHOST_Xr.cc:137
int GHOST_XrLoadControllerModel(GHOST_IXrContext *xr_context, const char *subaction_path)
Definition GHOST_Xr.cc:242
void GHOST_XrContextDestroy(GHOST_IXrContext *xr_context)
Definition GHOST_Xr.cc:58
void GHOST_XrPassthroughEnabledFunc(GHOST_IXrContext *xr_context, GHOST_XrPassthroughEnabledFn passthrough_enabled_fn)
Definition GHOST_Xr.cc:96
unsigned int GHOST_XrGetActionCount(GHOST_IXrContext *xr_context, const char *action_set_name)
Definition GHOST_Xr.cc:226
GHOST_IXrContext * GHOST_XrContextCreate(const GHOST_XrContextCreateInfo *create_info)
Initialize the Ghost XR-context.
Definition GHOST_Xr.cc:40
int GHOST_XrCreateActionSet(GHOST_IXrContext *xr_context, const GHOST_XrActionSetInfo *info)
Definition GHOST_Xr.cc:114
int GHOST_XrGetControllerModelData(GHOST_IXrContext *xr_context, const char *subaction_path, GHOST_XrControllerModelData *r_data)
Definition GHOST_Xr.cc:263
int GHOST_XrSyncActions(GHOST_IXrContext *xr_context, const char *action_set_name)
Definition GHOST_Xr.cc:176
void GHOST_XrSessionEnd(GHOST_IXrContext *xr_context)
Definition GHOST_Xr.cc:68
int GHOST_XrSessionNeedsUpsideDownDrawing(const GHOST_IXrContext *xr_context)
Definition GHOST_Xr.cc:108
void GHOST_XrGetActionCustomdataArray(GHOST_IXrContext *xr_context, const char *action_set_name, void **r_customdata_array)
Definition GHOST_Xr.cc:233
void GHOST_XrSessionStart(GHOST_IXrContext *xr_context, const GHOST_XrSessionBeginInfo *begin_info)
Definition GHOST_Xr.cc:63
void * GHOST_XrGetActionCustomdata(GHOST_IXrContext *xr_context, const char *action_set_name, const char *action_name)
Definition GHOST_Xr.cc:216
int GHOST_XrSessionIsRunning(const GHOST_IXrContext *xr_context)
Definition GHOST_Xr.cc:78
int GHOST_XrCreateActionBindings(GHOST_IXrContext *xr_context, const char *action_set_name, uint32_t count, const GHOST_XrActionProfileInfo *infos)
Definition GHOST_Xr.cc:146
void GHOST_XrDisablePassthroughFunc(GHOST_IXrContext *xr_context, GHOST_XrDisablePassthroughFn disable_passthrough_fn)
Definition GHOST_Xr.cc:102
void GHOST_XrStopHapticAction(GHOST_IXrContext *xr_context_handle, const char *action_set_name, const char *action_name, const char *subaction_path)
Definition GHOST_Xr.cc:199
void * GHOST_XrGetActionSetCustomdata(GHOST_IXrContext *xr_context, const char *action_set_name)
Definition GHOST_Xr.cc:209
int GHOST_XrAttachActionSets(GHOST_IXrContext *xr_context)
Definition GHOST_Xr.cc:169
void GHOST_XrErrorHandler(GHOST_XrErrorHandlerFn handler_fn, void *customdata)
Definition GHOST_Xr.cc:35
int GHOST_XrUpdateControllerModelComponents(GHOST_IXrContext *xr_context, const char *subaction_path)
Definition GHOST_Xr.cc:255
void GHOST_XrGraphicsContextBindFuncs(GHOST_IXrContext *xr_context, GHOST_XrGraphicsContextBindFn bind_fn, GHOST_XrGraphicsContextUnbindFn unbind_fn)
Definition GHOST_Xr.cc:84
void GHOST_XrUnloadControllerModel(GHOST_IXrContext *xr_context, const char *subaction_path)
Definition GHOST_Xr.cc:249
void GHOST_XrDrawViewFunc(GHOST_IXrContext *xr_context, GHOST_XrDrawViewFn draw_view_fn)
Definition GHOST_Xr.cc:91
void GHOST_XrDestroyActionBindings(GHOST_IXrContext *xr_context, const char *action_set_name, uint32_t count, const char *const *action_names, const char *const *profile_paths)
Definition GHOST_Xr.cc:157
void GHOST_XrSessionDrawViews(GHOST_IXrContext *xr_context, void *customdata)
Definition GHOST_Xr.cc:73
int GHOST_XrApplyHapticAction(GHOST_IXrContext *xr_context_handle, const char *action_set_name, const char *action_name, const char *subaction_path, const int64_t *duration, const float *frequency, const float *amplitude)
Definition GHOST_Xr.cc:183
#define GHOST_XR_CAPI_CALL(call, ctx)
Definition GHOST_Xr.cc:19
#define GHOST_XR_CAPI_CALL_RET(call, ctx)
Definition GHOST_Xr.cc:27
CLG_LogRef * LOG_GHOST_XR
long long int int64_t
virtual void endSession()=0
virtual GHOST_XrSession * getSession()=0
virtual void setDisablePassthroughFunc(GHOST_XrDisablePassthroughFn disable_passthrough_fn)=0
virtual void startSession(const GHOST_XrSessionBeginInfo *begin_info)=0
virtual void setPassthroughEnabledFunc(GHOST_XrPassthroughEnabledFn passthrough_enabled_fn)=0
virtual bool needsUpsideDownDrawing() const =0
virtual void setGraphicsContextBindFuncs(GHOST_XrGraphicsContextBindFn bind_fn, GHOST_XrGraphicsContextUnbindFn unbind_fn)=0
virtual void drawSessionViews(void *draw_customdata)=0
virtual bool isSessionRunning() const =0
virtual void setDrawViewFunc(GHOST_XrDrawViewFn draw_view_fn)=0
static void setErrorHandler(GHOST_XrErrorHandlerFn handler_fn, void *customdata)
void destroyActionBindings(const char *action_set_name, uint32_t count, const char *const *action_names, const char *const *profile_paths)
void destroyActionSet(const char *action_set_name)
bool createActionSet(const GHOST_XrActionSetInfo &info)
void * getActionCustomdata(const char *action_set_name, const char *action_name)
void unloadControllerModel(const char *subaction_path)
void * getActionSetCustomdata(const char *action_set_name)
bool updateControllerModelComponents(const char *subaction_path)
bool createActionBindings(const char *action_set_name, uint32_t count, const GHOST_XrActionProfileInfo *infos)
void stopHapticAction(const char *action_set_name, const char *action_name, const char *subaction_path)
void getActionCustomdataArray(const char *action_set_name, void **r_customdata_array)
bool loadControllerModel(const char *subaction_path)
bool syncActions(const char *action_set_name=nullptr)
void destroyActions(const char *action_set_name, uint32_t count, const char *const *action_names)
bool applyHapticAction(const char *action_set_name, const char *action_name, const char *subaction_path, const int64_t &duration, const float &frequency, const float &amplitude)
uint32_t getActionCount(const char *action_set_name)
bool getControllerModelData(const char *subaction_path, GHOST_XrControllerModelData &r_data)
bool createActions(const char *action_set_name, uint32_t count, const GHOST_XrActionInfo *infos)
#define frequency(freq)