1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file
* \ingroup wm
*/
#include "BKE_context.h"
#include "BKE_global.h"
#include "BKE_main.h"
#include "BKE_report.h"
#include "BKE_screen.h"
#include "BLI_math_geom.h"
#include "BLI_math_matrix.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"
#include "DNA_view3d_types.h"
#include "DRW_engine.h"
#include "ED_view3d.h"
#include "GHOST_C-api.h"
#include "GPU_context.h"
#include "GPU_draw.h"
#include "GPU_matrix.h"
#include "GPU_viewport.h"
#include "MEM_guardedalloc.h"
#include "UI_interface.h"
#include "WM_types.h"
#include "WM_api.h"
#include "wm.h"
#include "wm_surface.h"
#include "wm_window.h"
#ifdef WIN32 /* Only WIN32 supported now */
//# define USE_FORCE_WINDOWED_SESSION
#endif
#ifdef USE_FORCE_WINDOWED_SESSION
static void xr_session_window_create(bContext *C);
#endif
static wmSurface *g_xr_surface = NULL;
typedef struct {
GHOST_TXrGraphicsBinding gpu_binding_type;
GPUOffScreen *offscreen;
GPUViewport *viewport;
} wmXrSurfaceData;
typedef struct {
wmWindowManager *wm;
bContext *evil_C;
} wmXrErrorHandlerData;
static void wm_xr_error_handler(const GHOST_XrError *error)
{
wmXrErrorHandlerData *handler_data = error->customdata;
wmWindowManager *wm = handler_data->wm;
BKE_reports_clear(&wm->reports);
WM_report(RPT_ERROR, error->user_message);
WM_report_banner_show();
UI_popup_menu_reports(handler_data->evil_C, &wm->reports);
if (wm->xr_context) {
/* Just play safe and destroy the entire context. */
GHOST_XrContextDestroy(wm->xr_context);
wm->xr_context = NULL;
}
}
bool wm_xr_context_ensure(bContext *C, wmWindowManager *wm)
{
if (wm->xr_context) {
return true;
}
static wmXrErrorHandlerData error_customdata;
/* Set up error handling */
error_customdata.wm = wm;
error_customdata.evil_C = C;
GHOST_XrErrorHandler(wm_xr_error_handler, &error_customdata);
{
const GHOST_TXrGraphicsBinding gpu_bindings_candidates[] = {
GHOST_kXrGraphicsOpenGL,
#ifdef WIN32
GHOST_kXrGraphicsD3D11,
#endif
};
GHOST_XrContextCreateInfo create_info = {
.gpu_binding_candidates = gpu_bindings_candidates,
.gpu_binding_candidates_count = ARRAY_SIZE(gpu_bindings_candidates)};
if (G.debug & G_DEBUG_XR) {
create_info.context_flag |= GHOST_kXrContextDebug;
}
if (G.debug & G_DEBUG_XR_TIME) {
create_info.context_flag |= GHOST_kXrContextDebugTime;
}
wm->xr_context = GHOST_XrContextCreate(&create_info);
}
return wm->xr_context != NULL;
}
static void wm_xr_session_surface_draw(bContext *C)
{
wmWindowManager *wm = CTX_wm_manager(C);
if (!GHOST_XrSessionIsRunning(wm->xr_context)) {
return;
}
GHOST_XrSessionDrawViews(wm->xr_context, C);
}
static void wm_xr_session_free_data(wmSurface *surface)
{
wmXrSurfaceData *data = surface->customdata;
if (surface->secondary_ghost_ctx) {
#ifdef WIN32
if (data->gpu_binding_type == GHOST_kXrGraphicsD3D11) {
WM_directx_context_dispose(surface->secondary_ghost_ctx);
}
#endif
}
GPU_context_active_set(surface->gpu_ctx);
DRW_opengl_context_enable_ex(false);
if (data->viewport) {
GPU_viewport_clear_from_offscreen(data->viewport);
GPU_viewport_free(data->viewport);
}
if (data->offscreen) {
GPU_offscreen_free(data->offscreen);
}
DRW_opengl_context_disable_ex(false);
MEM_freeN(surface->customdata);
g_xr_surface = NULL;
}
static wmSurface *wm_xr_session_surface_create(wmWindowManager *wm, unsigned int gpu_binding_type)
{
if (g_xr_surface) {
BLI_assert(false);
return g_xr_surface;
}
wmSurface *surface = MEM_callocN(sizeof(*surface), __func__);
wmXrSurfaceData *data = MEM_callocN(sizeof(*data), "XrSurfaceData");
#ifndef WIN32
BLI_assert(gpu_binding_type == GHOST_kXrGraphicsOpenGL);
#endif
surface->draw = wm_xr_session_surface_draw;
surface->free_data = wm_xr_session_free_data;
data->gpu_binding_type = gpu_binding_type;
surface->customdata = data;
surface->ghost_ctx = DRW_opengl_context_get();
DRW_opengl_context_enable();
switch (gpu_binding_type) {
case GHOST_kXrGraphicsOpenGL:
break;
#ifdef WIN32
case GHOST_kXrGraphicsD3D11:
surface->secondary_ghost_ctx = WM_directx_context_create();
break;
#endif
}
surface->gpu_ctx = DRW_gpu_context_get();
DRW_opengl_context_disable();
g_xr_surface = surface;
return surface;
}
static void wm_xr_draw_matrices_create(const Scene *scene,
const GHOST_XrDrawViewInfo *draw_view,
const float clip_start,
const float clip_end,
float r_view_mat[4][4],
float r_proj_mat[4][4])
{
float scalemat[4][4], quat[4];
float temp[4][4];
perspective_m4_fov(r_proj_mat,
draw_view->fov.angle_left,
draw_view->fov.angle_right,
draw_view->fov.angle_up,
draw_view->fov.angle_down,
clip_start,
clip_end);
scale_m4_fl(scalemat, 1.0f);
invert_qt_qt_normalized(quat, draw_view->pose.orientation_quat);
quat_to_mat4(temp, quat);
translate_m4(temp,
-draw_view->pose.position[0],
-draw_view->pose.position[1],
-draw_view->pose.position[2]);
if (scene->camera) {
invert_m4_m4(scene->camera->imat, scene->camera->obmat);
mul_m4_m4m4(r_view_mat, temp, scene->camera->imat);
}
else {
copy_m4_m4(r_view_mat, temp);
}
}
static bool wm_xr_session_surface_offscreen_ensure(const GHOST_XrDrawViewInfo *draw_view)
{
wmXrSurfaceData *surface_data = g_xr_surface->customdata;
char err_out[256] = "unknown";
bool failure = false;
if (surface_data->offscreen &&
(GPU_offscreen_width(surface_data->offscreen) == draw_view->width) &&
(GPU_offscreen_height(surface_data->offscreen) == draw_view->height)) {
BLI_assert(surface_data->viewport);
return true;
}
DRW_opengl_context_enable();
if (surface_data->offscreen) {
GPU_viewport_clear_from_offscreen(surface_data->viewport);
GPU_viewport_free(surface_data->viewport);
GPU_offscreen_free(surface_data->offscreen);
}
if (!(surface_data->offscreen = GPU_offscreen_create(
draw_view->width, draw_view->height, 0, true, false, err_out))) {
failure = true;
}
if ((failure == false) &&
!(surface_data->viewport = GPU_viewport_create_from_offscreen(surface_data->offscreen))) {
GPU_offscreen_free(surface_data->offscreen);
failure = true;
}
DRW_opengl_context_disable();
if (failure) {
fprintf(stderr, "%s: failed to get buffer, %s\n", __func__, err_out);
return false;
}
return true;
}
static GHOST_ContextHandle wm_xr_draw_view(const GHOST_XrDrawViewInfo *draw_view, void *customdata)
{
bContext *C = customdata;
wmXrSurfaceData *surface_data = g_xr_surface->customdata;
const float clip_start = 0.01, clip_end = 500.0f;
const float lens = 50.0f; /* TODO get from OpenXR */
const rcti rect = {
.xmin = 0, .ymin = 0, .xmax = draw_view->width - 1, .ymax = draw_view->height - 1};
GPUOffScreen *offscreen;
GPUViewport *viewport;
View3DShading shading;
float viewmat[4][4], winmat[4][4];
wm_xr_draw_matrices_create(CTX_data_scene(C), draw_view, clip_start, clip_end, viewmat, winmat);
if (!wm_xr_session_surface_offscreen_ensure(draw_view)) {
return NULL;
}
offscreen = surface_data->offscreen;
viewport = surface_data->viewport;
GPU_viewport_bind(viewport, &rect);
glClear(GL_DEPTH_BUFFER_BIT);
BKE_screen_view3d_shading_init(&shading);
shading.flag |= V3D_SHADING_WORLD_ORIENTATION;
shading.background_type = V3D_SHADING_BACKGROUND_WORLD;
ED_view3d_draw_offscreen_simple(CTX_data_depsgraph(C),
CTX_data_scene(C),
&shading,
OB_SOLID,
draw_view->width,
draw_view->height,
/* Draw floor for better orientation */
V3D_OFSDRAW_OVERRIDE_SCENE_SETTINGS | V3D_OFSDRAW_SHOW_GRIDFLOOR,
viewmat,
winmat,
clip_start,
clip_end,
lens,
true,
true,
NULL,
false,
offscreen,
viewport);
GPU_framebuffer_restore();
GPUTexture *texture = GPU_offscreen_color_texture(offscreen);
wm_draw_offscreen_texture_parameters(offscreen);
GPU_depth_test(false);
wmViewport(&rect);
GPU_viewport_draw_to_screen(viewport, &rect);
if (g_xr_surface->secondary_ghost_ctx &&
GHOST_isUpsideDownContext(g_xr_surface->secondary_ghost_ctx)) {
GPU_texture_bind(texture, 0);
wm_draw_upside_down(draw_view->width, draw_view->height);
GPU_texture_unbind(texture);
}
GPU_viewport_unbind(viewport);
return g_xr_surface->ghost_ctx;
}
static void *wm_xr_session_gpu_binding_context_create(GHOST_TXrGraphicsBinding graphics_binding)
{
#ifndef USE_FORCE_WINDOWED_SESSION
wmSurface *surface = wm_xr_session_surface_create(G_MAIN->wm.first, graphics_binding);
wm_surface_add(surface);
return surface->secondary_ghost_ctx ? surface->secondary_ghost_ctx : surface->ghost_ctx;
#else
# ifdef WIN32
if (graphics_binding == GHOST_kXrGraphicsD3D11) {
wmWindowManager *wm = G_MAIN->wm.first;
for (wmWindow *win = wm->windows.first; win; win = win->next) {
/* TODO better lookup? For now only one D3D window possible, but later? */
if (GHOST_GetDrawingContextType(win->ghostwin) == GHOST_kDrawingContextTypeD3D) {
return GHOST_GetWindowContext(win->ghostwin);
}
}
}
# endif
return NULL;
#endif
}
static void wm_xr_session_gpu_binding_context_destroy(
GHOST_TXrGraphicsBinding UNUSED(graphics_lib), void *UNUSED(context))
{
#ifndef USE_FORCE_WINDOWED_SESSION
if (g_xr_surface) { /* Might have been freed already */
wm_surface_remove(g_xr_surface);
}
#endif
wm_window_reset_drawable();
}
static void wm_xr_session_begin_info_create(const Scene *scene,
GHOST_XrSessionBeginInfo *begin_info)
{
if (scene->camera) {
copy_v3_v3(begin_info->base_pose.position, scene->camera->loc);
/* TODO will only work if rotmode is euler */
eul_to_quat(begin_info->base_pose.orientation_quat, scene->camera->rot);
}
else {
copy_v3_fl(begin_info->base_pose.position, 0.0f);
unit_qt(begin_info->base_pose.orientation_quat);
}
}
void wm_xr_session_toggle(bContext *C, void *xr_context_ptr)
{
GHOST_XrContextHandle xr_context = xr_context_ptr;
if (xr_context && GHOST_XrSessionIsRunning(xr_context)) {
GHOST_XrSessionEnd(xr_context);
}
else {
GHOST_XrSessionBeginInfo begin_info;
#if defined(USE_FORCE_WINDOWED_SESSION)
xr_session_window_create(C);
#endif
wm_xr_session_begin_info_create(CTX_data_scene(C), &begin_info);
GHOST_XrGraphicsContextBindFuncs(xr_context,
wm_xr_session_gpu_binding_context_create,
wm_xr_session_gpu_binding_context_destroy);
GHOST_XrDrawViewFunc(xr_context, wm_xr_draw_view);
GHOST_XrSessionStart(xr_context, &begin_info);
}
}
#if defined(USE_FORCE_WINDOWED_SESSION)
# include "BLI_rect.h"
# include "DNA_workspace_types.h"
# include "BKE_workspace.h"
# include "ED_screen.h"
# include "BLI_string.h"
static void xr_session_window_create(bContext *C)
{
Main *bmain = CTX_data_main(C);
Scene *scene = CTX_data_scene(C);
ViewLayer *view_layer = CTX_data_view_layer(C);
wmWindow *win_prev = CTX_wm_window(C);
rcti rect;
wmWindow *win;
bScreen *screen;
ScrArea *sa;
int screen_size[2];
wm_get_screensize(&screen_size[0], &screen_size[1]);
BLI_rcti_init(&rect, 0, screen_size[0], 0, screen_size[1]);
BLI_rcti_scale(&rect, 0.8f);
wm_window_check_position(&rect);
win = WM_window_open_directx(C, &rect);
if (WM_window_get_active_workspace(win) == NULL) {
WorkSpace *workspace = WM_window_get_active_workspace(win_prev);
BKE_workspace_active_set(win->workspace_hook, workspace);
}
/* add new screen layout */
WorkSpace *workspace = WM_window_get_active_workspace(win);
WorkSpaceLayout *layout = ED_workspace_layout_add(bmain, workspace, win, "VR Session");
screen = BKE_workspace_layout_screen_get(layout);
WM_window_set_active_layout(win, workspace, layout);
/* Set scene and view layer to match original window. */
STRNCPY(win->view_layer_name, view_layer->name);
if (WM_window_get_active_scene(win) != scene) {
ED_screen_scene_change(C, win, scene);
}
WM_check(C);
CTX_wm_window_set(C, win);
sa = screen->areabase.first;
CTX_wm_area_set(C, sa);
ED_area_newspace(C, sa, SPACE_VIEW3D, false);
ED_screen_change(C, screen);
ED_screen_refresh(CTX_wm_manager(C), win); /* test scale */
if (win->ghostwin) {
GHOST_SetTitle(win->ghostwin, "Blender VR Session View");
}
else {
/* very unlikely! but opening a new window can fail */
wm_window_close(C, CTX_wm_manager(C), win);
CTX_wm_window_set(C, win_prev);
}
}
#endif /* USE_FORCE_WINDOWED_SESSION */
|