Blender V5.3
wm_xr.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
12
13#include "BKE_context.hh"
14#include "BKE_global.hh"
15#include "BKE_idprop.hh"
16#include "BKE_main.hh"
17#include "BKE_report.hh"
18
19#include "DNA_scene_types.h"
21
22#include "ED_screen.hh"
23
24#include "GHOST_IXrContext.hh"
25#include "GHOST_Types.hh"
26#include "GHOST_Xr-api.hh"
27
28#include "GPU_context.hh"
29
30#include "MEM_guardedalloc.h"
31
32#include "WM_api.hh"
33
34#include "wm_xr_intern.hh"
35
36namespace blender {
37
41
42/* -------------------------------------------------------------------- */
43
44static void wm_xr_error_handler(const GHOST_XrError *error)
45{
46 wmXrErrorHandlerData *handler_data = static_cast<wmXrErrorHandlerData *>(error->customdata);
47 wmWindowManager *wm = handler_data->wm;
48 wmWindow *xr_root_win = wm->xr.runtime ? CTX_wm_window(wm->xr.runtime->b_context) : nullptr;
49
50 BKE_reports_clear(&wm->runtime->reports);
51 WM_global_report(RPT_ERROR, error->user_message);
52 /* Internally rely on the first WM window as a fallback when `xr_root_win` is nullptr. */
53 WM_report_banner_show(wm, xr_root_win);
54
55 if (wm->xr.runtime) {
56 /* Just play safe and destroy the entire runtime data, including context. */
57 wm_xr_runtime_data_free(&wm->xr.runtime);
58 }
59}
60
62{
64 if (wm->xr.runtime && wm->xr.runtime->ghost_context) {
65 return true;
66 }
67 static wmXrErrorHandlerData error_customdata;
68
69 /* Set up error handling. */
70 error_customdata.wm = wm;
71 GHOST_XrErrorHandler(wm_xr_error_handler, &error_customdata);
72
73 {
74 Vector<GHOST_TXrGraphicsBinding> gpu_bindings_candidates;
75 switch (GPU_backend_get_type()) {
76#ifdef WITH_OPENGL_BACKEND
78 gpu_bindings_candidates.append(GHOST_kXrGraphicsOpenGL);
79# ifdef WIN32
80 gpu_bindings_candidates.append(GHOST_kXrGraphicsOpenGLD3D11);
81# endif
82 break;
83#endif
84
85#ifdef WITH_VULKAN_BACKEND
87 gpu_bindings_candidates.append(GHOST_kXrGraphicsVulkan);
88# ifdef WIN32
89 gpu_bindings_candidates.append(GHOST_kXrGraphicsVulkanD3D11);
90# endif
91 break;
92#endif
93
94#ifdef WITH_METAL_BACKEND
96 gpu_bindings_candidates.append(GHOST_kXrGraphicsMetal);
97 break;
98#endif
99
100 default:
101 break;
102 }
103
104 GHOST_XrContextCreateInfo create_info{
105 /*gpu_binding_candidates*/ gpu_bindings_candidates.data(),
106 /*gpu_binding_candidates_count*/ uint32_t(gpu_bindings_candidates.size()),
107 };
108 if (G.debug & G_DEBUG_XR) {
109 create_info.context_flag |= GHOST_kXrContextDebug;
110 }
111 if (G.debug & G_DEBUG_XR_TIME) {
112 create_info.context_flag |= GHOST_kXrContextDebugTime;
113 }
114#ifdef WIN32
116 create_info.context_flag |= GHOST_kXrContextGpuNVIDIA;
117 }
118#endif
119
120 GHOST_IXrContext *ghost_context;
121 if (!(ghost_context = GHOST_XrContextCreate(&create_info))) {
122 return false;
123 }
124
125 /* Set up context callbacks. */
132
133 if (!wm->xr.runtime) {
134 wm->xr.runtime = wm_xr_runtime_data_create();
135 wm->xr.runtime->ghost_context = ghost_context;
136
137 /* Create a minimal XR-specific context. */
138 wm->xr.runtime->b_context = CTX_create();
139
140 /* Base Main and WM pointers. */
141 CTX_wm_manager_set(wm->xr.runtime->b_context, CTX_wm_manager(C));
142 CTX_data_main_set(wm->xr.runtime->b_context, CTX_data_main(C));
143
144 /* Create the XR offscreen area (independent of any bScreen). */
145 wm->xr.runtime->offscreen_area = ED_area_offscreen_create(CTX_wm_window(C), SPACE_VIEW3D);
147 }
148 }
149 BLI_assert(wm->xr.runtime && wm->xr.runtime->ghost_context && wm->xr.runtime->b_context);
150
151 return true;
152}
153
155{
156 if (wm->xr.runtime != nullptr) {
157 wm_xr_runtime_data_free(&wm->xr.runtime);
158 }
159
160 /* See #wm_xr_data_free for logic that frees window-manager XR data
161 * that may exist even when built without XR. */
162}
163
165{
166 if (wm->xr.runtime && wm->xr.runtime->ghost_context) {
167 GHOST_XrEventsHandle(wm->xr.runtime->ghost_context);
168
169 /* Process OpenXR action events. */
170 if (WM_xr_session_is_ready(&wm->xr)) {
172 }
173
174 /* #wm_window_events_process() uses the return value to determine if it can put the main thread
175 * to sleep for some milliseconds. We never want that to happen while the VR session runs on
176 * the main thread. So always return true. */
177 return true;
178 }
179 return false;
180}
181
182/* -------------------------------------------------------------------- */
185
187{
189 return runtime;
190}
191
193{
194 /* This function may be called recursively via the #GHOST_XrContextDestroy session exit callback.
195 * Guard against double-free by nulling pointers after freeing. */
196
197 /* Destroy context if still alive. */
198 if ((*runtime)->ghost_context != nullptr) {
199 GHOST_IXrContext *ghost_context = (*runtime)->ghost_context;
200 /* Set to nullptr before calling XrContextDestroy to prevent recursive calls. */
201 (*runtime)->ghost_context = nullptr;
202
203 GHOST_XrContextDestroy(ghost_context);
204 }
205
206 /* Free remaining runtime data. */
207 if (*runtime != nullptr) {
208 ScrArea *xr_offscreen_area = (*runtime)->offscreen_area;
209 BLI_assert(xr_offscreen_area);
210
211 wmWindowManager *wm = G_MAIN->wm.first();
213 WM_event_remove_handlers_by_area(&xr_win->runtime->handlers, xr_offscreen_area);
214 ED_area_offscreen_free(wm, xr_win, xr_offscreen_area);
215
216 CTX_free((*runtime)->b_context);
217
218 wm_xr_session_data_free(&(*runtime)->session_state);
219 WM_xr_actionmaps_clear(*runtime);
220
221 MEM_SAFE_DELETE(*runtime);
222 *runtime = nullptr;
223 }
224}
225 /* XR Runtime Data. */
227
228} // namespace blender
#define G_MAIN
Abstraction for XR (VR, AR, MR, ..) access via OpenXR.
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
GHOST_IXrContext * GHOST_XrContextCreate(const GHOST_XrContextCreateInfo *create_info)
Initialize the Ghost XR-context.
Definition GHOST_Xr.cc:40
GHOST_TSuccess GHOST_XrEventsHandle(GHOST_IXrContext *xr_context)
void GHOST_XrDisablePassthroughFunc(GHOST_IXrContext *xr_context, GHOST_XrDisablePassthroughFn disable_passthrough_fn)
Definition GHOST_Xr.cc:102
void GHOST_XrErrorHandler(GHOST_XrErrorHandlerFn handler_fn, void *customdata)
Definition GHOST_Xr.cc:35
void GHOST_XrGraphicsContextBindFuncs(GHOST_IXrContext *xr_context, GHOST_XrGraphicsContextBindFn bind_fn, GHOST_XrGraphicsContextUnbindFn unbind_fn)
Definition GHOST_Xr.cc:84
void GHOST_XrDrawViewFunc(GHOST_IXrContext *xr_context, GHOST_XrDrawViewFn draw_view_fn)
Definition GHOST_Xr.cc:91
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
int64_t size() const
void append(const T &value)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool wm_xr_passthrough_enabled(void *customdata)
Check if XR passthrough is enabled.
void WM_report_banner_show(wmWindowManager *wm, wmWindow *win)
static void wm_xr_error_handler(const GHOST_XrError *error)
Definition wm_xr.cc:44
wmWindow * wm_xr_session_root_window_or_fallback_get(const wmWindowManager *wm, const wmXrRuntimeData *runtime_data)
void wm_xr_draw_view(const GHOST_XrDrawViewInfo *draw_view, void *customdata)
Draw a viewport for a single eye.
void wm_xr_session_actions_update(wmWindowManager *wm)
GPUBackendType GPU_backend_get_type()
GHOST_IContext * wm_xr_session_gpu_binding_context_create()
wmWindow * CTX_wm_window(const bContext *C)
void BKE_reports_clear(ReportList *reports)
Definition report.cc:118
bool wm_xr_init(bContext *C)
Definition wm_xr.cc:61
bContext * CTX_create()
@ G_DEBUG_XR_TIME
Global G
Definition blender.cc:57
unsigned int uint32_t
Definition BKE_text.h:81
bool GPU_type_matches(GPUDeviceType device, GPUOSType os, GPUDriverType driver)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void wm_xr_session_gpu_binding_context_destroy(GHOST_IContext *context)
wmWindowManager * CTX_wm_manager(const bContext *C)
void ED_area_offscreen_free(wmWindowManager *wm, wmWindow *win, ScrArea *area)
Definition area.cc:2522
void CTX_free(bContext *C)
bContext * WM_xr_session_context_ensure(wmXrData *xr, const wmWindowManager *wm)
bool WM_xr_session_is_ready(const wmXrData *xr)
void WM_global_report(eReportType type, const char *message)
ScrArea * ED_area_offscreen_create(wmWindow *win, eSpace_Type space_type)
Definition area.cc:2481
Main * CTX_data_main(const bContext *C)
void wm_xr_exit(wmWindowManager *wm)
Definition wm_xr.cc:154
void wm_xr_runtime_data_free(wmXrRuntimeData **runtime)
Definition wm_xr.cc:192
void CTX_wm_manager_set(bContext *C, wmWindowManager *wm)
void WM_event_remove_handlers_by_area(ListBaseT< wmEventHandler > *handlers, const ScrArea *area)
void CTX_data_main_set(bContext *C, Main *bmain)
void wm_xr_session_data_free(wmXrSessionState *state)
void wm_xr_disable_passthrough(void *customdata)
Disable XR passthrough if not supported.
bool wm_xr_events_handle(wmWindowManager *wm)
Definition wm_xr.cc:164
wmXrRuntimeData * wm_xr_runtime_data_create()
Definition wm_xr.cc:186
void WM_xr_actionmaps_clear(wmXrRuntimeData *runtime)
@ GPU_DEVICE_NVIDIA
ListBaseT< wmEventHandler > handlers
bke::WindowRuntime * runtime
wmWindowManager * wm
Definition wm_xr.cc:39