Blender  V2.93
wm_xr.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
25 #include "BKE_global.h"
26 #include "BKE_idprop.h"
27 #include "BKE_report.h"
28 
29 #include "DNA_scene_types.h"
31 
32 #include "DEG_depsgraph.h"
33 
34 #include "MEM_guardedalloc.h"
35 
36 #include "GHOST_C-api.h"
37 
38 #include "WM_api.h"
39 
40 #include "wm_surface.h"
41 #include "wm_xr_intern.h"
42 
43 typedef struct {
46 
47 /* -------------------------------------------------------------------- */
48 
49 static void wm_xr_error_handler(const GHOST_XrError *error)
50 {
51  wmXrErrorHandlerData *handler_data = error->customdata;
52  wmWindowManager *wm = handler_data->wm;
53 
55  WM_report(RPT_ERROR, error->user_message);
57 
58  if (wm->xr.runtime) {
59  /* Just play safe and destroy the entire runtime data, including context. */
61  }
62 }
63 
65 {
66  if (wm->xr.runtime && wm->xr.runtime->context) {
67  return true;
68  }
69  static wmXrErrorHandlerData error_customdata;
70 
71  /* Set up error handling */
72  error_customdata.wm = wm;
73  GHOST_XrErrorHandler(wm_xr_error_handler, &error_customdata);
74 
75  {
76  const GHOST_TXrGraphicsBinding gpu_bindings_candidates[] = {
77  GHOST_kXrGraphicsOpenGL,
78 #ifdef WIN32
79  GHOST_kXrGraphicsD3D11,
80 #endif
81  };
82  GHOST_XrContextCreateInfo create_info = {
83  .gpu_binding_candidates = gpu_bindings_candidates,
84  .gpu_binding_candidates_count = ARRAY_SIZE(gpu_bindings_candidates),
85  };
86  GHOST_XrContextHandle context;
87 
88  if (G.debug & G_DEBUG_XR) {
89  create_info.context_flag |= GHOST_kXrContextDebug;
90  }
91  if (G.debug & G_DEBUG_XR_TIME) {
92  create_info.context_flag |= GHOST_kXrContextDebugTime;
93  }
94 
95  if (!(context = GHOST_XrContextCreate(&create_info))) {
96  return false;
97  }
98 
99  /* Set up context callbacks */
100  GHOST_XrGraphicsContextBindFuncs(context,
103  GHOST_XrDrawViewFunc(context, wm_xr_draw_view);
104 
105  if (!wm->xr.runtime) {
107  wm->xr.runtime->context = context;
108  }
109  }
110  BLI_assert(wm->xr.runtime && wm->xr.runtime->context);
111 
112  return true;
113 }
114 
116 {
117  if (wm->xr.runtime != NULL) {
119  }
120  if (wm->xr.session_settings.shading.prop) {
123  }
124 }
125 
127 {
128  if (wm->xr.runtime && wm->xr.runtime->context) {
130 
131  /* wm_window_process_events() uses the return value to determine if it can put the main thread
132  * to sleep for some milliseconds. We never want that to happen while the VR session runs on
133  * the main thread. So always return true. */
134  return true;
135  }
136  return false;
137 }
138 
139 /* -------------------------------------------------------------------- */
144 {
145  wmXrRuntimeData *runtime = MEM_callocN(sizeof(*runtime), __func__);
146  return runtime;
147 }
148 
150 {
151  /* Note that this function may be called twice, because of an indirect recursion: If a session is
152  * running while WM-XR calls this function, calling GHOST_XrContextDestroy() will call this
153  * again, because it's also set as the session exit callback. So NULL-check and NULL everything
154  * that is freed here. */
155 
156  /* We free all runtime XR data here, so if the context is still alive, destroy it. */
157  if ((*runtime)->context != NULL) {
158  GHOST_XrContextHandle context = (*runtime)->context;
159  /* Prevent recursive GHOST_XrContextDestroy() call by NULL'ing the context pointer before the
160  * first call, see comment above. */
161  (*runtime)->context = NULL;
163  }
164  MEM_SAFE_FREE(*runtime);
165 }
166  /* XR Runtime Data */
@ G_DEBUG_XR
Definition: BKE_global.h:154
@ G_DEBUG_XR_TIME
Definition: BKE_global.h:155
void IDP_FreeProperty(struct IDProperty *prop)
Definition: idprop.c:1040
void BKE_reports_clear(ReportList *reports)
Definition: report.c:84
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define ARRAY_SIZE(arr)
GHOST C-API function and type declarations.
GHOST_TSuccess GHOST_XrEventsHandle(GHOST_XrContextHandle xr_contexthandle)
GHOST_XrContextHandle GHOST_XrContextCreate(const GHOST_XrContextCreateInfo *create_info)
Definition: GHOST_Xr.cpp:32
void GHOST_XrContextDestroy(GHOST_XrContextHandle xr_contexthandle)
Definition: GHOST_Xr.cpp:50
void GHOST_XrErrorHandler(GHOST_XrErrorHandlerFn handler_fn, void *customdata)
Definition: GHOST_Xr.cpp:55
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
static void error(const char *str)
Definition: meshlaplacian.c:65
struct SELECTID_Context context
Definition: select_engine.c:47
struct IDProperty * prop
struct View3DShading shading
Definition: DNA_xr_types.h:31
struct ReportList reports
XrSessionSettings session_settings
struct wmXrRuntimeData * runtime
wmWindowManager * wm
Definition: wm_xr.c:44
GHOST_XrContextHandle context
Definition: wm_xr_intern.h:50
#define G(x, y, z)
void WM_report_banner_show(void)
void WM_report(ReportType type, const char *message)
bool wm_xr_init(wmWindowManager *wm)
Definition: wm_xr.c:64
static void wm_xr_error_handler(const GHOST_XrError *error)
Definition: wm_xr.c:49
void wm_xr_runtime_data_free(wmXrRuntimeData **runtime)
Definition: wm_xr.c:149
wmXrRuntimeData * wm_xr_runtime_data_create(void)
Definition: wm_xr.c:143
bool wm_xr_events_handle(wmWindowManager *wm)
Definition: wm_xr.c:126
void wm_xr_exit(wmWindowManager *wm)
Definition: wm_xr.c:115
void wm_xr_draw_view(const GHOST_XrDrawViewInfo *draw_view, void *customdata)
Draw a viewport for a single eye.
Definition: wm_xr_draw.c:105
void wm_xr_session_gpu_binding_context_destroy(GHOST_ContextHandle context)
void * wm_xr_session_gpu_binding_context_create(void)