Blender V5.3
GHOST_XrSession.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
8
9#include <algorithm>
10#include <cassert>
11#include <chrono>
12#include <cstdio>
13#include <list>
14#include <sstream>
15
16#include "BLI_span.hh"
17
18#include "GHOST_Context.hh"
20#include "GHOST_XrAction.hh"
21#include "GHOST_XrContext.hh"
23#include "GHOST_XrException.hh"
24#include "GHOST_XrSwapchain.hh"
25#include "GHOST_Xr_intern.hh"
26
27#include "GHOST_XrSession.hh"
28
30 XrSystemId system_id = XR_NULL_SYSTEM_ID;
31 XrSession session = XR_NULL_HANDLE;
32 XrSessionState session_state = XR_SESSION_STATE_UNKNOWN;
33
34 /* Use stereo rendering by default. */
35 XrViewConfigurationType view_type = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO;
36 bool foveation_supported = false;
37
39 XrSpace view_space;
41 std::vector<XrView> views;
42 std::vector<GHOST_XrSwapchain> swapchains;
43
44 std::map<std::string, GHOST_XrActionSet> action_sets;
45 /* Controller models identified by subaction path. */
46 std::map<std::string, GHOST_XrControllerModel> controller_models;
47
48 /* Meta Quest passthrough support. */
50 XrCompositionLayerPassthroughFB passthrough_layer;
51};
52
54 XrFrameState frame_state;
55
57 std::chrono::high_resolution_clock::time_point frame_begin_time;
58 /* Time previous frames took for rendering (in ms). */
59 std::list<double> last_frame_times;
60
61 /* Whether foveation is active for the frame. */
63};
64
65/* -------------------------------------------------------------------- */
68
70 : context_(&xr_context), oxr_(std::make_unique<OpenXRSessionData>())
71{
72}
73
75{
77
78 oxr_->swapchains.clear();
79 oxr_->action_sets.clear();
80
81 if (oxr_->reference_space != XR_NULL_HANDLE) {
82 CHECK_XR_ASSERT(xrDestroySpace(oxr_->reference_space));
83 }
84 if (oxr_->view_space != XR_NULL_HANDLE) {
85 CHECK_XR_ASSERT(xrDestroySpace(oxr_->view_space));
86 }
87 if (oxr_->combined_eye_space != XR_NULL_HANDLE) {
88 CHECK_XR_ASSERT(xrDestroySpace(oxr_->combined_eye_space));
89 }
90 if (oxr_->session != XR_NULL_HANDLE) {
91 CHECK_XR_ASSERT(xrDestroySession(oxr_->session));
92 }
93
94 oxr_->session = XR_NULL_HANDLE;
95 oxr_->session_state = XR_SESSION_STATE_UNKNOWN;
96
97 oxr_->passthrough_supported = false;
98 oxr_->passthrough_layer.layerHandle = XR_NULL_HANDLE;
99
100 context_->getCustomFuncs().session_exit_fn(context_->getCustomFuncs().session_exit_customdata);
101}
102
107void GHOST_XrSession::initSystem()
108{
109 assert(context_->getInstance() != XR_NULL_HANDLE);
110 assert(oxr_->system_id == XR_NULL_SYSTEM_ID);
111
112 XrSystemGetInfo system_info = {};
113 system_info.type = XR_TYPE_SYSTEM_GET_INFO;
114 system_info.formFactor = XR_FORM_FACTOR_HEAD_MOUNTED_DISPLAY;
115
116 CHECK_XR(xrGetSystem(context_->getInstance(), &system_info, &oxr_->system_id),
117 "Failed to get device information. Is a device plugged in?");
118}
119 /* Create, Initialize and Destruct */
121
122/* -------------------------------------------------------------------- */
125
127 const blender::Span<XrReferenceSpaceType> supported_spaces)
128{
129 XrReferenceSpaceCreateInfo create_info = {XR_TYPE_REFERENCE_SPACE_CREATE_INFO};
130 create_info.poseInReferenceSpace.orientation.w = 1.0f;
131
132 /* Use the most suitable space as the main reference space. By order of preference:
133 * - Stage Reference Space
134 * - Local Floor Reference Space
135 * - Local Space
136 * Defaulting to the next one if the prior one is not available.
137 */
138
139 /* Stage Reference Space. */
140 if (supported_spaces.contains(XR_REFERENCE_SPACE_TYPE_STAGE)) {
141 create_info.referenceSpaceType = XR_REFERENCE_SPACE_TYPE_STAGE;
142 CHECK_XR(xrCreateReferenceSpace(oxr.session, &create_info, &oxr.reference_space),
143 "Failed to create stage reference space.");
144
145 /* Check if tracking bounds are valid. Tracking bounds may be invalid if the user did not
146 * define a tracking space via the XR runtime. */
147 XrExtent2Df extents;
148 CHECK_XR(xrGetReferenceSpaceBoundsRect(oxr.session, XR_REFERENCE_SPACE_TYPE_STAGE, &extents),
149 "Failed to get stage reference space bounds.");
150 if (extents.width != 0.0f && extents.height != 0.0f) {
151 /* Stage Reference Space is valid, return. */
152 return;
153 }
154
155 /* Stage Reference Space is invalid, destroy it and try to create the next available space. */
156 if (oxr.reference_space != XR_NULL_HANDLE) {
157 CHECK_XR(xrDestroySpace(oxr.reference_space), "Failed to destroy stage reference space.");
158 }
159
161 "Invalid stage reference space bounds, falling back to local floor reference "
162 "space. To use the stage reference space, please define a tracking space via the "
163 "XR runtime.");
164 }
165
166 /* Local Floor Reference Space. */
167 if (supported_spaces.contains(XR_REFERENCE_SPACE_TYPE_LOCAL_FLOOR_EXT)) {
169 "Stage reference space unavailable, falling back to local floor reference space.");
170 create_info.referenceSpaceType = XR_REFERENCE_SPACE_TYPE_LOCAL_FLOOR_EXT;
171 CHECK_XR(xrCreateReferenceSpace(oxr.session, &create_info, &oxr.reference_space),
172 "Failed to create local floor reference space.");
173 return;
174 }
175
176 /* Local Reference Space. */
178 "Stage and local floor reference space unavailable, falling back to local "
179 "reference space.");
180 create_info.referenceSpaceType = XR_REFERENCE_SPACE_TYPE_LOCAL;
181 CHECK_XR(xrCreateReferenceSpace(oxr.session, &create_info, &oxr.reference_space),
182 "Failed to create local reference space.");
183}
184
186{
187 XrReferenceSpaceCreateInfo create_info = {XR_TYPE_REFERENCE_SPACE_CREATE_INFO};
188 create_info.poseInReferenceSpace.orientation.w = 1.0f;
189
190 /* Enumerate supported reference spaces. */
191 uint32_t space_count = 0;
192 CHECK_XR(xrEnumerateReferenceSpaces(oxr.session, 0, &space_count, nullptr),
193 "Failed to enumerate available reference space count.");
194
195 std::vector<XrReferenceSpaceType> supported_spaces_vec(space_count);
196 CHECK_XR(xrEnumerateReferenceSpaces(
197 oxr.session, space_count, &space_count, supported_spaces_vec.data()),
198 "Failed to enumerate available reference spaces.");
199
200 const blender::Span supported_spaces(supported_spaces_vec);
201
202 create_main_reference_space(oxr, supported_spaces);
203
204 /* View reference space. */
205 create_info.referenceSpaceType = XR_REFERENCE_SPACE_TYPE_VIEW;
206 CHECK_XR(xrCreateReferenceSpace(oxr.session, &create_info, &oxr.view_space),
207 "Failed to create view reference space.");
208
209 /* Foveation reference spaces. */
210 if (oxr.foveation_supported) {
211 create_info.referenceSpaceType = XR_REFERENCE_SPACE_TYPE_COMBINED_EYE_VARJO;
212 CHECK_XR(xrCreateReferenceSpace(oxr.session, &create_info, &oxr.combined_eye_space),
213 "Failed to create combined eye reference space.");
214 }
215}
216
218{
219 assert(context_->getInstance() != XR_NULL_HANDLE);
220 assert(oxr_->session == XR_NULL_HANDLE);
221 if (context_->getCustomFuncs().gpu_ctx_bind_fn == nullptr) {
222 throw GHOST_XrException(
223 "Invalid API usage: No way to bind graphics context to the XR session. Call "
224 "GHOST_XrGraphicsContextBindFuncs() with valid parameters before starting the "
225 "session (through GHOST_XrSessionStart()).");
226 }
227
228 initSystem();
229
230 bindGraphicsContext();
231 if (gpu_ctx_ == nullptr) {
232 throw GHOST_XrException(
233 "Invalid API usage: No graphics context returned through the callback set with "
234 "GHOST_XrGraphicsContextBindFuncs(). This is required for session starting (through "
235 "GHOST_XrSessionStart()).");
236 }
237
238 std::string requirement_str;
239 gpu_binding_ = GHOST_XrGraphicsBindingCreateFromType(context_->getGraphicsBindingType(),
240 *gpu_ctx_);
241 if (!gpu_binding_->loadExtensionFunctions(context_->getInstance())) {
242 throw GHOST_XrException(
243 "Unable to load graphics bindings (could not load the needed extension functions from the "
244 "XrInstance)");
245 }
246 if (!gpu_binding_->checkVersionRequirements(
247 *gpu_ctx_, context_->getInstance(), oxr_->system_id, &requirement_str))
248 {
249 std::ostringstream strstream;
250 strstream << "Available graphics context version does not meet the following requirements: "
251 << requirement_str;
252 throw GHOST_XrException(strstream.str().data());
253 }
254 gpu_binding_->initFromGhostContext(*gpu_ctx_, context_->getInstance(), oxr_->system_id);
255
256 XrSessionCreateInfo create_info = {};
257 create_info.type = XR_TYPE_SESSION_CREATE_INFO;
258 create_info.systemId = oxr_->system_id;
259 create_info.next = &gpu_binding_->oxr_binding;
260
261 CHECK_XR(xrCreateSession(context_->getInstance(), &create_info, &oxr_->session),
262 "Failed to create VR session. The OpenXR runtime may have additional requirements for "
263 "the graphics driver that are not met. Other causes are possible too however.\nTip: "
264 "The --debug-xr command line option for Blender might allow the runtime to output "
265 "detailed error information to the command line.");
266
267 prepareDrawing();
269
270 /* Create and bind actions here. */
271 context_->getCustomFuncs().session_create_fn();
272}
273
275{
276 xrRequestExitSession(oxr_->session);
277}
278
279void GHOST_XrSession::beginSession()
280{
281 XrSessionBeginInfo begin_info = {XR_TYPE_SESSION_BEGIN_INFO};
282 begin_info.primaryViewConfigurationType = oxr_->view_type;
283 CHECK_XR(xrBeginSession(oxr_->session, &begin_info), "Failed to cleanly begin the VR session.");
284}
285
286void GHOST_XrSession::endSession()
287{
288 assert(oxr_->session != XR_NULL_HANDLE);
289 CHECK_XR(xrEndSession(oxr_->session), "Failed to cleanly end the VR session.");
290}
291
293 const XrEventDataSessionStateChanged &lifecycle)
294{
295 oxr_->session_state = lifecycle.state;
296
297 /* Runtime may send events for apparently destroyed session. Our handle should be nullptr then.
298 */
299 assert(oxr_->session == XR_NULL_HANDLE || oxr_->session == lifecycle.session);
300
301 switch (lifecycle.state) {
302 case XR_SESSION_STATE_READY:
303 beginSession();
304 break;
305 case XR_SESSION_STATE_STOPPING:
306 endSession();
307 break;
308 case XR_SESSION_STATE_EXITING:
309 case XR_SESSION_STATE_LOSS_PENDING:
310 return SESSION_DESTROY;
311 default:
312 break;
313 }
314
315 return SESSION_KEEP_ALIVE;
316}
317 /* State Management */
319
320/* -------------------------------------------------------------------- */
323
324void GHOST_XrSession::prepareDrawing()
325{
326 assert(context_->getInstance() != XR_NULL_HANDLE);
327
328 std::vector<XrViewConfigurationView> view_configs;
329 uint32_t view_count;
330
331 /* Attempt to use quad view if supported. */
332 if (context_->isExtensionEnabled(XR_VARJO_QUAD_VIEWS_EXTENSION_NAME)) {
333 oxr_->view_type = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_QUAD_VARJO;
334 }
335
336 oxr_->foveation_supported = context_->isExtensionEnabled(
337 XR_VARJO_FOVEATED_RENDERING_EXTENSION_NAME);
338
339 CHECK_XR(xrEnumerateViewConfigurationViews(
340 context_->getInstance(), oxr_->system_id, oxr_->view_type, 0, &view_count, nullptr),
341 "Failed to get count of view configurations.");
342 view_configs.resize(view_count, {XR_TYPE_VIEW_CONFIGURATION_VIEW});
343 CHECK_XR(xrEnumerateViewConfigurationViews(context_->getInstance(),
344 oxr_->system_id,
345 oxr_->view_type,
346 view_configs.size(),
347 &view_count,
348 view_configs.data()),
349 "Failed to get view configurations.");
350
351 /* If foveated rendering is used, query the foveated views. */
352 if (oxr_->foveation_supported) {
353 std::vector<XrFoveatedViewConfigurationViewVARJO> request_foveated_config{
354 view_count, {XR_TYPE_FOVEATED_VIEW_CONFIGURATION_VIEW_VARJO, nullptr, XR_TRUE}};
355
356 auto foveated_views = std::vector<XrViewConfigurationView>(view_count,
357 {XR_TYPE_VIEW_CONFIGURATION_VIEW});
358
359 for (uint32_t i = 0; i < view_count; i++) {
360 foveated_views[i].next = &request_foveated_config[i];
361 }
362 CHECK_XR(xrEnumerateViewConfigurationViews(context_->getInstance(),
363 oxr_->system_id,
364 oxr_->view_type,
365 view_configs.size(),
366 &view_count,
367 foveated_views.data()),
368 "Failed to get foveated view configurations.");
369
370 /* Ensure swapchains have correct size even when foveation is being used. */
371 for (uint32_t i = 0; i < view_count; i++) {
372 view_configs[i].recommendedImageRectWidth = std::max(
373 view_configs[i].recommendedImageRectWidth, foveated_views[i].recommendedImageRectWidth);
374 view_configs[i].recommendedImageRectHeight = std::max(
375 view_configs[i].recommendedImageRectHeight,
376 foveated_views[i].recommendedImageRectHeight);
377 }
378 }
379
380 for (const XrViewConfigurationView &view_config : view_configs) {
381 oxr_->swapchains.emplace_back(*gpu_binding_, oxr_->session, view_config);
382 }
383
384 oxr_->views.resize(view_count, {XR_TYPE_VIEW});
385
386 draw_info_ = std::make_unique<GHOST_XrDrawInfo>();
387}
388
389void GHOST_XrSession::beginFrameDrawing()
390{
391 XrFrameWaitInfo wait_info = {XR_TYPE_FRAME_WAIT_INFO};
392 XrFrameBeginInfo begin_info = {XR_TYPE_FRAME_BEGIN_INFO};
393 XrFrameState frame_state = {XR_TYPE_FRAME_STATE};
394
395 /* TODO Blocking call. Drawing should run on a separate thread to avoid interferences. */
396 CHECK_XR(xrWaitFrame(oxr_->session, &wait_info, &frame_state),
397 "Failed to synchronize frame rates between Blender and the device.");
398
399 /* Check if we have foveation available for the current frame. */
400 draw_info_->foveation_active = false;
401 if (oxr_->foveation_supported) {
402 XrSpaceLocation render_gaze_location{XR_TYPE_SPACE_LOCATION};
403 CHECK_XR(xrLocateSpace(oxr_->combined_eye_space,
404 oxr_->view_space,
405 frame_state.predictedDisplayTime,
406 &render_gaze_location),
407 "Failed to locate combined eye space.");
408
409 draw_info_->foveation_active = (render_gaze_location.locationFlags &
410 XR_SPACE_LOCATION_ORIENTATION_TRACKED_BIT) != 0;
411 }
412
413 CHECK_XR(xrBeginFrame(oxr_->session, &begin_info),
414 "Failed to submit frame rendering start state.");
415
416 draw_info_->frame_state = frame_state;
417
418 if (context_->isDebugTimeMode()) {
419 draw_info_->frame_begin_time = std::chrono::high_resolution_clock::now();
420 }
421}
422
424{
426 std::chrono::duration<double, std::milli> duration = std::chrono::high_resolution_clock::now() -
427 draw_info.frame_begin_time;
428 const double duration_ms = duration.count();
429 const int avg_frame_count = 8;
430 double avg_ms_tot = 0.0;
431
432 if (draw_info.last_frame_times.size() >= avg_frame_count) {
433 draw_info.last_frame_times.pop_front();
434 assert(draw_info.last_frame_times.size() == avg_frame_count - 1);
435 }
436 draw_info.last_frame_times.push_back(duration_ms);
437 for (double ms_iter : draw_info.last_frame_times) {
438 avg_ms_tot += ms_iter;
439 }
440
442 "VR frame render time: %.0fms - %.2f FPS (%.2f FPS 8 frames average)",
443 duration_ms,
444 1000.0 / duration_ms,
445 1000.0 / (avg_ms_tot / draw_info.last_frame_times.size()));
446}
447
448void GHOST_XrSession::endFrameDrawing(std::vector<XrCompositionLayerBaseHeader *> &layers)
449{
450 XrFrameEndInfo end_info = {XR_TYPE_FRAME_END_INFO};
451
452 end_info.displayTime = draw_info_->frame_state.predictedDisplayTime;
453 end_info.environmentBlendMode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE;
454 end_info.layerCount = layers.size();
455 end_info.layers = layers.data();
456
457 CHECK_XR(xrEndFrame(oxr_->session, &end_info), "Failed to submit rendered frame.");
458
459 if (context_->isDebugTimeMode()) {
460 print_debug_timings(*draw_info_);
461 }
462}
463
464void GHOST_XrSession::draw(void *draw_customdata)
465{
466 std::vector<XrCompositionLayerProjectionView>
467 projection_layer_views; /* Keep alive until #xrEndFrame() call! */
468 XrCompositionLayerProjection proj_layer;
469 std::vector<XrCompositionLayerBaseHeader *> layers;
470
471 beginFrameDrawing();
472
473 if (context_->getCustomFuncs().passthrough_enabled_fn(draw_customdata)) {
474 enablePassthrough();
475 if (oxr_->passthrough_supported) {
476 layers.push_back((XrCompositionLayerBaseHeader *)&oxr_->passthrough_layer);
477 }
478 else {
479 context_->getCustomFuncs().disable_passthrough_fn(draw_customdata);
480 }
481 }
482
483 if (draw_info_->frame_state.shouldRender) {
484 proj_layer = drawLayer(projection_layer_views, draw_customdata);
485 if (layers.size() > 0) {
486 proj_layer.layerFlags = XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT;
487 }
488 layers.push_back(reinterpret_cast<XrCompositionLayerBaseHeader *>(&proj_layer));
489 }
490
491 endFrameDrawing(layers);
492}
493
494static void ghost_xr_draw_view_info_from_view(const XrView &view, GHOST_XrDrawViewInfo &r_info)
495{
496 /* Set and convert to Blender coordinate space. */
497 copy_openxr_pose_to_ghost_pose(view.pose, r_info.eye_pose);
498
499 r_info.fov.angle_left = view.fov.angleLeft;
500 r_info.fov.angle_right = view.fov.angleRight;
501 r_info.fov.angle_up = view.fov.angleUp;
502 r_info.fov.angle_down = view.fov.angleDown;
503}
504
505void GHOST_XrSession::drawView(GHOST_XrSwapchain &swapchain,
506 XrSwapchainImageBaseHeader &swapchain_image,
507 XrCompositionLayerProjectionView &r_proj_layer_view,
508 const XrSpaceLocation &view_location,
509 const XrView &view,
510 uint32_t view_idx,
511 void *draw_customdata)
512{
513 r_proj_layer_view.type = XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW;
514 r_proj_layer_view.pose = view.pose;
515 r_proj_layer_view.fov = view.fov;
516 swapchain.updateCompositionLayerProjectViewSubImage(r_proj_layer_view.subImage);
517
518 assert(view_idx < 256);
519 GHOST_XrDrawViewInfo draw_view_info = {};
520 draw_view_info.view_idx = char(view_idx);
521 draw_view_info.swapchain_format = swapchain.getFormat();
522 draw_view_info.gpu_swapchain_format = swapchain.getGPUFormat();
523 draw_view_info.expects_srgb_buffer = swapchain.isBufferSRGB();
524 draw_view_info.ofsx = r_proj_layer_view.subImage.imageRect.offset.x;
525 draw_view_info.ofsy = r_proj_layer_view.subImage.imageRect.offset.y;
526 draw_view_info.width = r_proj_layer_view.subImage.imageRect.extent.width;
527 draw_view_info.height = r_proj_layer_view.subImage.imageRect.extent.height;
528 copy_openxr_pose_to_ghost_pose(view_location.pose, draw_view_info.local_pose);
529 ghost_xr_draw_view_info_from_view(view, draw_view_info);
530
531 /* Draw! */
532 context_->getCustomFuncs().draw_view_fn(&draw_view_info, draw_customdata);
533 gpu_binding_->submitToSwapchainImage(swapchain_image, draw_view_info);
534}
535
536XrCompositionLayerProjection GHOST_XrSession::drawLayer(
537 std::vector<XrCompositionLayerProjectionView> &r_proj_layer_views, void *draw_customdata)
538{
539 XrViewLocateInfo viewloc_info = {XR_TYPE_VIEW_LOCATE_INFO};
540 XrViewLocateFoveatedRenderingVARJO foveated_info{
541 XR_TYPE_VIEW_LOCATE_FOVEATED_RENDERING_VARJO, nullptr, true};
542 XrViewState view_state = {XR_TYPE_VIEW_STATE};
543 XrCompositionLayerProjection layer = {XR_TYPE_COMPOSITION_LAYER_PROJECTION};
544 XrSpaceLocation view_location{XR_TYPE_SPACE_LOCATION};
545 uint32_t view_count;
546
547 viewloc_info.viewConfigurationType = oxr_->view_type;
548 viewloc_info.displayTime = draw_info_->frame_state.predictedDisplayTime;
549 viewloc_info.space = oxr_->reference_space;
550
551 if (draw_info_->foveation_active) {
552 viewloc_info.next = &foveated_info;
553 }
554
555 CHECK_XR(xrLocateViews(oxr_->session,
556 &viewloc_info,
557 &view_state,
558 oxr_->views.size(),
559 &view_count,
560 oxr_->views.data()),
561 "Failed to query frame view and projection state.");
562
563 assert(oxr_->swapchains.size() == view_count);
564
565 CHECK_XR(xrLocateSpace(
566 oxr_->view_space, oxr_->reference_space, viewloc_info.displayTime, &view_location),
567 "Failed to query frame view space");
568
569 r_proj_layer_views.resize(view_count);
570 std::vector<XrSwapchainImageBaseHeader *> swapchain_images;
571 swapchain_images.resize(view_count);
572
573 for (uint32_t view_idx = 0; view_idx < view_count; view_idx++) {
574 GHOST_XrSwapchain &swapchain = oxr_->swapchains[view_idx];
575 swapchain_images[view_idx] = swapchain.acquireDrawableSwapchainImage();
576 }
577
578 gpu_binding_->submitToSwapchainBegin();
579 for (uint32_t view_idx = 0; view_idx < view_count; view_idx++) {
580 GHOST_XrSwapchain &swapchain = oxr_->swapchains[view_idx];
581 XrSwapchainImageBaseHeader &swapchain_image = *swapchain_images[view_idx];
582 drawView(swapchain,
583 swapchain_image,
584 r_proj_layer_views[view_idx],
585 view_location,
586 oxr_->views[view_idx],
587 view_idx,
588 draw_customdata);
589 }
590 gpu_binding_->submitToSwapchainEnd();
591
592 for (uint32_t view_idx = 0; view_idx < view_count; view_idx++) {
593 GHOST_XrSwapchain &swapchain = oxr_->swapchains[view_idx];
594 swapchain.releaseImage();
595 swapchain_images[view_idx] = nullptr;
596 }
597
598 layer.space = oxr_->reference_space;
599 layer.viewCount = r_proj_layer_views.size();
600 layer.views = r_proj_layer_views.data();
601
602 return layer;
603}
604
606{
607 return gpu_binding_ && gpu_binding_->needsUpsideDownDrawing(*gpu_ctx_);
608}
609 /* Drawing */
611
612/* -------------------------------------------------------------------- */
615
617{
618 if (oxr_->session == XR_NULL_HANDLE) {
619 return false;
620 }
621 switch (oxr_->session_state) {
622 case XR_SESSION_STATE_READY:
623 case XR_SESSION_STATE_SYNCHRONIZED:
624 case XR_SESSION_STATE_VISIBLE:
625 case XR_SESSION_STATE_FOCUSED:
626 return true;
627 default:
628 return false;
629 }
630}
631 /* State Queries */
633
634/* -------------------------------------------------------------------- */
643
644void GHOST_XrSession::bindGraphicsContext()
645{
646 const GHOST_XrCustomFuncs &custom_funcs = context_->getCustomFuncs();
647 assert(custom_funcs.gpu_ctx_bind_fn);
648 gpu_ctx_ = static_cast<GHOST_Context *>(custom_funcs.gpu_ctx_bind_fn());
649}
650
652{
653 const GHOST_XrCustomFuncs &custom_funcs = context_->getCustomFuncs();
654 if (custom_funcs.gpu_ctx_unbind_fn) {
655 custom_funcs.gpu_ctx_unbind_fn(gpu_ctx_);
656 }
657 gpu_ctx_ = nullptr;
658}
659 /* Graphics Context Injection */
661
662/* -------------------------------------------------------------------- */
666
667static GHOST_XrActionSet *find_action_set(OpenXRSessionData *oxr, const char *action_set_name)
668{
669 std::map<std::string, GHOST_XrActionSet>::iterator it = oxr->action_sets.find(action_set_name);
670 if (it == oxr->action_sets.end()) {
671 return nullptr;
672 }
673 return &it->second;
674}
675
676bool GHOST_XrSession::createActionSet(const GHOST_XrActionSetInfo &info)
677{
678 std::map<std::string, GHOST_XrActionSet> &action_sets = oxr_->action_sets;
679 if (action_sets.find(info.name) != action_sets.end()) {
680 return false;
681 }
682
683 XrInstance instance = context_->getInstance();
684
685 action_sets.emplace(
686 std::piecewise_construct, std::make_tuple(info.name), std::make_tuple(instance, info));
687
688 return true;
689}
690
691void GHOST_XrSession::destroyActionSet(const char *action_set_name)
692{
693 std::map<std::string, GHOST_XrActionSet> &action_sets = oxr_->action_sets;
694 /* It's possible nothing is removed. */
695 action_sets.erase(action_set_name);
696}
697
698bool GHOST_XrSession::createActions(const char *action_set_name,
699 uint32_t count,
700 const GHOST_XrActionInfo *infos)
701{
702 GHOST_XrActionSet *action_set = find_action_set(oxr_.get(), action_set_name);
703 if (action_set == nullptr) {
704 return false;
705 }
706
707 XrInstance instance = context_->getInstance();
708
709 for (uint32_t i = 0; i < count; ++i) {
710 if (!action_set->createAction(instance, infos[i])) {
711 return false;
712 }
713 }
714
715 return true;
716}
717
718void GHOST_XrSession::destroyActions(const char *action_set_name,
719 uint32_t count,
720 const char *const *action_names)
721{
722 GHOST_XrActionSet *action_set = find_action_set(oxr_.get(), action_set_name);
723 if (action_set == nullptr) {
724 return;
725 }
726
727 for (uint32_t i = 0; i < count; ++i) {
728 action_set->destroyAction(action_names[i]);
729 }
730}
731
732bool GHOST_XrSession::createActionBindings(const char *action_set_name,
733 uint32_t count,
734 const GHOST_XrActionProfileInfo *infos)
735{
736 GHOST_XrActionSet *action_set = find_action_set(oxr_.get(), action_set_name);
737 if (action_set == nullptr) {
738 return false;
739 }
740
741 XrInstance instance = context_->getInstance();
742 XrSession session = oxr_->session;
743
744 for (uint32_t profile_idx = 0; profile_idx < count; ++profile_idx) {
745 const GHOST_XrActionProfileInfo &info = infos[profile_idx];
746
747 GHOST_XrAction *action = action_set->findAction(info.action_name);
748 if (action == nullptr) {
749 continue;
750 }
751
752 action->createBinding(instance, session, info);
753 }
754
755 return true;
756}
757
758void GHOST_XrSession::destroyActionBindings(const char *action_set_name,
759 uint32_t count,
760 const char *const *action_names,
761 const char *const *profile_paths)
762{
763 GHOST_XrActionSet *action_set = find_action_set(oxr_.get(), action_set_name);
764 if (action_set == nullptr) {
765 return;
766 }
767
768 for (uint32_t i = 0; i < count; ++i) {
769 GHOST_XrAction *action = action_set->findAction(action_names[i]);
770 if (action == nullptr) {
771 continue;
772 }
773
774 action->destroyBinding(profile_paths[i]);
775 }
776}
777
779{
780 /* Suggest action bindings for all action sets. */
781 std::map<XrPath, std::vector<XrActionSuggestedBinding>> profile_bindings;
782 for (auto &[name, action_set] : oxr_->action_sets) {
783 action_set.getBindings(profile_bindings);
784 }
785
786 if (profile_bindings.size() < 1) {
787 return false;
788 }
789
790 XrInteractionProfileSuggestedBinding bindings_info{
791 XR_TYPE_INTERACTION_PROFILE_SUGGESTED_BINDING};
792 XrInstance instance = context_->getInstance();
793
794 for (auto &[profile, bindings] : profile_bindings) {
795 bindings_info.interactionProfile = profile;
796 bindings_info.countSuggestedBindings = uint32_t(bindings.size());
797 bindings_info.suggestedBindings = bindings.data();
798
799 CHECK_XR(xrSuggestInteractionProfileBindings(instance, &bindings_info),
800 "Failed to suggest interaction profile bindings.");
801 }
802
803 /* Attach action sets. */
804 XrSessionActionSetsAttachInfo attach_info{XR_TYPE_SESSION_ACTION_SETS_ATTACH_INFO};
805 attach_info.countActionSets = uint32_t(oxr_->action_sets.size());
806
807 /* Create an aligned copy of the action sets to pass to xrAttachSessionActionSets(). */
808 std::vector<XrActionSet> action_sets(attach_info.countActionSets);
809 uint32_t i = 0;
810 for (auto &[name, action_set] : oxr_->action_sets) {
811 action_sets[i++] = action_set.getActionSet();
812 }
813 attach_info.actionSets = action_sets.data();
814
815 CHECK_XR(xrAttachSessionActionSets(oxr_->session, &attach_info),
816 "Failed to attach XR action sets.");
817
818 return true;
819}
820
821bool GHOST_XrSession::syncActions(const char *action_set_name)
822{
823 std::map<std::string, GHOST_XrActionSet> &action_sets = oxr_->action_sets;
824
825 XrActionsSyncInfo sync_info{XR_TYPE_ACTIONS_SYNC_INFO};
826 sync_info.countActiveActionSets = (action_set_name != nullptr) ? 1 :
827 uint32_t(action_sets.size());
828 if (sync_info.countActiveActionSets < 1) {
829 return false;
830 }
831
832 std::vector<XrActiveActionSet> active_action_sets(sync_info.countActiveActionSets);
833 GHOST_XrActionSet *action_set = nullptr;
834
835 if (action_set_name != nullptr) {
836 action_set = find_action_set(oxr_.get(), action_set_name);
837 if (action_set == nullptr) {
838 return false;
839 }
840
841 XrActiveActionSet &active_action_set = active_action_sets[0];
842 active_action_set.actionSet = action_set->getActionSet();
843 active_action_set.subactionPath = XR_NULL_PATH;
844 }
845 else {
846 uint32_t i = 0;
847 for (auto &[name, action_set] : action_sets) {
848 XrActiveActionSet &active_action_set = active_action_sets[i++];
849 active_action_set.actionSet = action_set.getActionSet();
850 active_action_set.subactionPath = XR_NULL_PATH;
851 }
852 }
853 sync_info.activeActionSets = active_action_sets.data();
854
855 CHECK_XR(xrSyncActions(oxr_->session, &sync_info), "Failed to synchronize XR actions.");
856
857 /* Update action states (i.e. Blender custom data). */
858 XrSession session = oxr_->session;
859 XrSpace reference_space = oxr_->reference_space;
860 const XrTime &predicted_display_time = draw_info_->frame_state.predictedDisplayTime;
861
862 if (action_set != nullptr) {
863 action_set->updateStates(session, reference_space, predicted_display_time);
864 }
865 else {
866 for (auto &[name, action_set] : action_sets) {
867 action_set.updateStates(session, reference_space, predicted_display_time);
868 }
869 }
870
871 return true;
872}
873
874bool GHOST_XrSession::applyHapticAction(const char *action_set_name,
875 const char *action_name,
876 const char *subaction_path,
877 const int64_t &duration,
878 const float &frequency,
879 const float &amplitude)
880{
881 GHOST_XrActionSet *action_set = find_action_set(oxr_.get(), action_set_name);
882 if (action_set == nullptr) {
883 return false;
884 }
885
886 GHOST_XrAction *action = action_set->findAction(action_name);
887 if (action == nullptr) {
888 return false;
889 }
890
891 action->applyHapticFeedback(
892 oxr_->session, action_name, subaction_path, duration, frequency, amplitude);
893
894 return true;
895}
896
897void GHOST_XrSession::stopHapticAction(const char *action_set_name,
898 const char *action_name,
899 const char *subaction_path)
900{
901 GHOST_XrActionSet *action_set = find_action_set(oxr_.get(), action_set_name);
902 if (action_set == nullptr) {
903 return;
904 }
905
906 GHOST_XrAction *action = action_set->findAction(action_name);
907 if (action == nullptr) {
908 return;
909 }
910
911 action->stopHapticFeedback(oxr_->session, action_name, subaction_path);
912}
913
914void *GHOST_XrSession::getActionSetCustomdata(const char *action_set_name)
915{
916 GHOST_XrActionSet *action_set = find_action_set(oxr_.get(), action_set_name);
917 if (action_set == nullptr) {
918 return nullptr;
919 }
920
921 return action_set->getCustomdata();
922}
923
924void *GHOST_XrSession::getActionCustomdata(const char *action_set_name, const char *action_name)
925{
926 GHOST_XrActionSet *action_set = find_action_set(oxr_.get(), action_set_name);
927 if (action_set == nullptr) {
928 return nullptr;
929 }
930
931 GHOST_XrAction *action = action_set->findAction(action_name);
932 if (action == nullptr) {
933 return nullptr;
934 }
935
936 return action->getCustomdata();
937}
938
939uint32_t GHOST_XrSession::getActionCount(const char *action_set_name)
940{
941 const GHOST_XrActionSet *action_set = find_action_set(oxr_.get(), action_set_name);
942 if (action_set == nullptr) {
943 return 0;
944 }
945
946 return action_set->getActionCount();
947}
948
949void GHOST_XrSession::getActionCustomdataArray(const char *action_set_name,
950 void **r_customdata_array)
951{
952 GHOST_XrActionSet *action_set = find_action_set(oxr_.get(), action_set_name);
953 if (action_set == nullptr) {
954 return;
955 }
956
957 action_set->getActionCustomdataArray(r_customdata_array);
958}
959 /* Actions */
961
962/* -------------------------------------------------------------------- */
966
967bool GHOST_XrSession::loadControllerModel(const char *subaction_path)
968{
969 if (!context_->isExtensionEnabled(XR_MSFT_CONTROLLER_MODEL_EXTENSION_NAME)) {
970 return false;
971 }
972
973 XrSession session = oxr_->session;
974 std::map<std::string, GHOST_XrControllerModel> &controller_models = oxr_->controller_models;
975 std::map<std::string, GHOST_XrControllerModel>::iterator it = controller_models.find(
976 subaction_path);
977
978 if (it == controller_models.end()) {
979 XrInstance instance = context_->getInstance();
980 it = controller_models
981 .emplace(std::piecewise_construct,
982 std::make_tuple(subaction_path),
983 std::make_tuple(instance, subaction_path))
984 .first;
985 }
986
987 it->second.load(session);
988
989 return true;
990}
991
992void GHOST_XrSession::unloadControllerModel(const char *subaction_path)
993{
994 std::map<std::string, GHOST_XrControllerModel> &controller_models = oxr_->controller_models;
995 /* It's possible nothing is removed. */
996 controller_models.erase(subaction_path);
997}
998
1000{
1001 XrSession session = oxr_->session;
1002 std::map<std::string, GHOST_XrControllerModel>::iterator it = oxr_->controller_models.find(
1003 subaction_path);
1004 if (it == oxr_->controller_models.end()) {
1005 return false;
1006 }
1007
1008 it->second.updateComponents(session);
1009
1010 return true;
1011}
1012
1013bool GHOST_XrSession::getControllerModelData(const char *subaction_path,
1014 GHOST_XrControllerModelData &r_data)
1015{
1016 std::map<std::string, GHOST_XrControllerModel>::iterator it = oxr_->controller_models.find(
1017 subaction_path);
1018 if (it == oxr_->controller_models.end()) {
1019 return false;
1020 }
1021
1022 it->second.getData(r_data);
1023
1024 return true;
1025}
1026 /* Controller Model */
1028
1029/* -------------------------------------------------------------------- */
1033
1034static PFN_xrCreatePassthroughFB g_xrCreatePassthroughFB = nullptr;
1035static PFN_xrCreatePassthroughLayerFB g_xrCreatePassthroughLayerFB = nullptr;
1036static PFN_xrPassthroughStartFB g_xrPassthroughStartFB = nullptr;
1037static PFN_xrPassthroughLayerResumeFB g_xrPassthroughLayerResumeFB = nullptr;
1038
1039static void init_passthrough_extension_functions(XrInstance instance)
1040{
1041 if (g_xrCreatePassthroughFB == nullptr) {
1042 INIT_EXTENSION_FUNCTION(xrCreatePassthroughFB);
1043 }
1044 if (g_xrCreatePassthroughLayerFB == nullptr) {
1045 INIT_EXTENSION_FUNCTION(xrCreatePassthroughLayerFB);
1046 }
1047 if (g_xrPassthroughStartFB == nullptr) {
1048 INIT_EXTENSION_FUNCTION(xrPassthroughStartFB);
1049 }
1050 if (g_xrPassthroughLayerResumeFB == nullptr) {
1051 INIT_EXTENSION_FUNCTION(xrPassthroughLayerResumeFB);
1052 }
1053}
1054
1055void GHOST_XrSession::enablePassthrough()
1056{
1057 oxr_->passthrough_supported = false;
1058
1059 if (!context_->isExtensionEnabled(XR_FB_PASSTHROUGH_EXTENSION_NAME)) {
1060 return;
1061 }
1062
1063 if (oxr_->passthrough_layer.layerHandle != XR_NULL_HANDLE) {
1064 oxr_->passthrough_supported = true;
1065 return; /* Already initialized. */
1066 }
1067
1068 init_passthrough_extension_functions(context_->getInstance());
1069
1070 XrPassthroughCreateInfoFB passthrough_create_info = {};
1071 passthrough_create_info.type = XR_TYPE_PASSTHROUGH_CREATE_INFO_FB;
1072 passthrough_create_info.next = nullptr;
1073 passthrough_create_info.flags |= XR_PASSTHROUGH_IS_RUNNING_AT_CREATION_BIT_FB;
1074
1075 XrPassthroughFB passthrough_handle;
1076 if (XR_FAILED(
1077 g_xrCreatePassthroughFB(oxr_->session, &passthrough_create_info, &passthrough_handle)))
1078 {
1079 return;
1080 }
1081
1082 XrPassthroughLayerCreateInfoFB passthrough_layer_create_info = {};
1083 passthrough_layer_create_info.type = XR_TYPE_PASSTHROUGH_LAYER_CREATE_INFO_FB;
1084 passthrough_layer_create_info.next = nullptr;
1085 passthrough_layer_create_info.passthrough = passthrough_handle;
1086 passthrough_layer_create_info.flags |= XR_PASSTHROUGH_IS_RUNNING_AT_CREATION_BIT_FB;
1087 passthrough_layer_create_info.purpose = XR_PASSTHROUGH_LAYER_PURPOSE_RECONSTRUCTION_FB;
1088
1089 XrPassthroughLayerFB passthrough_layer_handle;
1090 if (XR_FAILED(g_xrCreatePassthroughLayerFB(
1091 oxr_->session, &passthrough_layer_create_info, &passthrough_layer_handle)))
1092 {
1093 return;
1094 }
1095
1096 g_xrPassthroughStartFB(passthrough_handle);
1097 g_xrPassthroughLayerResumeFB(passthrough_layer_handle);
1098
1099 oxr_->passthrough_layer.type = XR_TYPE_COMPOSITION_LAYER_PASSTHROUGH_FB;
1100 oxr_->passthrough_layer.next = nullptr;
1101 oxr_->passthrough_layer.flags = XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT;
1102 oxr_->passthrough_layer.space = XR_NULL_HANDLE;
1103 oxr_->passthrough_layer.layerHandle = passthrough_layer_handle;
1104
1105 oxr_->passthrough_supported = true;
1106}
1107 /* Meta Quest Passthrough */
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define CLOG_INFO(clg_ref,...)
Definition CLG_log.h:190
std::unique_ptr< GHOST_IXrGraphicsBinding > GHOST_XrGraphicsBindingCreateFromType(GHOST_TXrGraphicsBinding type, GHOST_Context &context)
static void init_passthrough_extension_functions(XrInstance instance)
static void print_debug_timings(GHOST_XrDrawInfo &draw_info)
static void ghost_xr_draw_view_info_from_view(const XrView &view, GHOST_XrDrawViewInfo &r_info)
static PFN_xrCreatePassthroughLayerFB g_xrCreatePassthroughLayerFB
static PFN_xrPassthroughLayerResumeFB g_xrPassthroughLayerResumeFB
static void create_main_reference_space(OpenXRSessionData &oxr, const blender::Span< XrReferenceSpaceType > supported_spaces)
static PFN_xrCreatePassthroughFB g_xrCreatePassthroughFB
static void create_reference_spaces(OpenXRSessionData &oxr)
static PFN_xrPassthroughStartFB g_xrPassthroughStartFB
static GHOST_XrActionSet * find_action_set(OpenXRSessionData *oxr, const char *action_set_name)
CLG_LogRef * LOG_GHOST_XR
void copy_openxr_pose_to_ghost_pose(const XrPosef &oxr_pose, GHOST_XrPose &r_ghost_pose)
#define INIT_EXTENSION_FUNCTION(name)
#define CHECK_XR_ASSERT(call)
#define CHECK_XR(call, error_msg)
long long int int64_t
void updateStates(XrSession session, XrSpace reference_space, const XrTime &predicted_display_time)
GHOST_XrAction * findAction(const char *action_name)
bool createAction(XrInstance instance, const GHOST_XrActionInfo &info)
XrActionSet getActionSet() const
void getActionCustomdataArray(void **r_customdata_array)
uint32_t getActionCount() const
void destroyAction(const char *action_name)
bool createBinding(XrInstance instance, XrSession session, const GHOST_XrActionProfileInfo &info)
void destroyBinding(const char *profile_path)
void applyHapticFeedback(XrSession session, const char *action_name, const char *subaction_path_str, const int64_t &duration, const float &frequency, const float &amplitude)
void stopHapticFeedback(XrSession session, const char *action_name, const char *subaction_path_str)
Main GHOST container to manage OpenXR through.
XrInstance getInstance() const
const GHOST_XrCustomFuncs & getCustomFuncs() const
bool isExtensionEnabled(const char *ext) const
GHOST_XrException(const char *msg, int result=0)
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)
void draw(void *draw_customdata)
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)
GHOST_XrSession(GHOST_XrContext &xr_context)
void stopHapticAction(const char *action_set_name, const char *action_name, const char *subaction_path)
bool isRunning() const
LifeExpectancy handleStateChangeEvent(const XrEventDataSessionStateChanged &lifecycle)
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)
bool needsUpsideDownDrawing() const
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)
int64_t getGPUFormat() const
return the swapchain format selected by the graphic binding.
GHOST_TXrSwapchainFormat getFormat() const
void updateCompositionLayerProjectViewSubImage(XrSwapchainSubImage &r_sub_image)
XrSwapchainImageBaseHeader * acquireDrawableSwapchainImage()
constexpr bool contains(const T &value) const
Definition BLI_span.hh:281
#define assert(assertion)
#define frequency(freq)
#define layer
unsigned int uint32_t
Definition BKE_text.h:81
GHOST_XrGraphicsContextUnbindFn gpu_ctx_unbind_fn
GHOST_XrGraphicsContextBindFn gpu_ctx_bind_fn
std::chrono::high_resolution_clock::time_point frame_begin_time
XrFrameState frame_state
std::list< double > last_frame_times
XrCompositionLayerPassthroughFB passthrough_layer
std::map< std::string, GHOST_XrControllerModel > controller_models
XrViewConfigurationType view_type
std::map< std::string, GHOST_XrActionSet > action_sets
XrSessionState session_state
std::vector< GHOST_XrSwapchain > swapchains
std::vector< XrView > views