openxr_interface.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. /**************************************************************************/
  2. /* openxr_interface.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "openxr_interface.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/io/resource_saver.h"
  33. #include "servers/rendering/rendering_server_globals.h"
  34. void OpenXRInterface::_bind_methods() {
  35. // lifecycle signals
  36. ADD_SIGNAL(MethodInfo("session_begun"));
  37. ADD_SIGNAL(MethodInfo("session_stopping"));
  38. ADD_SIGNAL(MethodInfo("session_focussed"));
  39. ADD_SIGNAL(MethodInfo("session_visible"));
  40. ADD_SIGNAL(MethodInfo("pose_recentered"));
  41. // Display refresh rate
  42. ClassDB::bind_method(D_METHOD("get_display_refresh_rate"), &OpenXRInterface::get_display_refresh_rate);
  43. ClassDB::bind_method(D_METHOD("set_display_refresh_rate", "refresh_rate"), &OpenXRInterface::set_display_refresh_rate);
  44. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "display_refresh_rate"), "set_display_refresh_rate", "get_display_refresh_rate");
  45. // Render Target size multiplier
  46. ClassDB::bind_method(D_METHOD("get_render_target_size_multiplier"), &OpenXRInterface::get_render_target_size_multiplier);
  47. ClassDB::bind_method(D_METHOD("set_render_target_size_multiplier", "multiplier"), &OpenXRInterface::set_render_target_size_multiplier);
  48. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "render_target_size_multiplier"), "set_render_target_size_multiplier", "get_render_target_size_multiplier");
  49. ClassDB::bind_method(D_METHOD("is_action_set_active", "name"), &OpenXRInterface::is_action_set_active);
  50. ClassDB::bind_method(D_METHOD("set_action_set_active", "name", "active"), &OpenXRInterface::set_action_set_active);
  51. ClassDB::bind_method(D_METHOD("get_action_sets"), &OpenXRInterface::get_action_sets);
  52. ClassDB::bind_method(D_METHOD("get_available_display_refresh_rates"), &OpenXRInterface::get_available_display_refresh_rates);
  53. }
  54. StringName OpenXRInterface::get_name() const {
  55. return StringName("OpenXR");
  56. };
  57. uint32_t OpenXRInterface::get_capabilities() const {
  58. return XRInterface::XR_VR + XRInterface::XR_STEREO;
  59. };
  60. PackedStringArray OpenXRInterface::get_suggested_tracker_names() const {
  61. // These are hardcoded in OpenXR, note that they will only be available if added to our action map
  62. PackedStringArray arr = {
  63. "left_hand", // /user/hand/left is mapped to our defaults
  64. "right_hand", // /user/hand/right is mapped to our defaults
  65. "/user/treadmill",
  66. // Even though these are only available if you have the tracker extension,
  67. // we add these as we may be deploying on a different platform than our
  68. // editor is running on.
  69. "/user/vive_tracker_htcx/role/handheld_object",
  70. "/user/vive_tracker_htcx/role/left_foot",
  71. "/user/vive_tracker_htcx/role/right_foot",
  72. "/user/vive_tracker_htcx/role/left_shoulder",
  73. "/user/vive_tracker_htcx/role/right_shoulder",
  74. "/user/vive_tracker_htcx/role/left_elbow",
  75. "/user/vive_tracker_htcx/role/right_elbow",
  76. "/user/vive_tracker_htcx/role/left_knee",
  77. "/user/vive_tracker_htcx/role/right_knee",
  78. "/user/vive_tracker_htcx/role/waist",
  79. "/user/vive_tracker_htcx/role/chest",
  80. "/user/vive_tracker_htcx/role/camera",
  81. "/user/vive_tracker_htcx/role/keyboard"
  82. };
  83. return arr;
  84. }
  85. XRInterface::TrackingStatus OpenXRInterface::get_tracking_status() const {
  86. return tracking_state;
  87. }
  88. void OpenXRInterface::_load_action_map() {
  89. ERR_FAIL_NULL(openxr_api);
  90. // This may seem a bit duplicitous to a little bit of background info here.
  91. // OpenXRActionMap (with all its sub resource classes) is a class that allows us to configure and store an action map in.
  92. // This gives the user the ability to edit the action map in a UI and customize the actions.
  93. // OpenXR however requires us to submit an action map and it takes over from that point and we can no longer change it.
  94. // This system does that push and we store the info needed to then work with this action map going forward.
  95. // Within our openxr device we maintain a number of classes that wrap the relevant OpenXR objects for this.
  96. // Within OpenXRInterface we have a few internal classes that keep track of what we've created.
  97. // This allow us to process the relevant actions each frame.
  98. // just in case clean up
  99. free_trackers();
  100. free_interaction_profiles();
  101. free_action_sets();
  102. Ref<OpenXRActionMap> action_map;
  103. if (Engine::get_singleton()->is_editor_hint()) {
  104. #ifdef TOOLS_ENABLED
  105. action_map.instantiate();
  106. action_map->create_editor_action_sets();
  107. #endif
  108. } else {
  109. String default_tres_name = openxr_api->get_default_action_map_resource_name();
  110. // Check if we can load our default
  111. if (ResourceLoader::exists(default_tres_name)) {
  112. action_map = ResourceLoader::load(default_tres_name);
  113. }
  114. // Check if we need to create default action set
  115. if (action_map.is_null()) {
  116. action_map.instantiate();
  117. action_map->create_default_action_sets();
  118. #ifdef TOOLS_ENABLED
  119. // Save our action sets so our user can
  120. action_map->set_path(default_tres_name, true);
  121. ResourceSaver::save(action_map, default_tres_name);
  122. #endif
  123. }
  124. }
  125. // process our action map
  126. if (action_map.is_valid()) {
  127. HashMap<Ref<OpenXRAction>, Action *> xr_actions;
  128. Array action_set_array = action_map->get_action_sets();
  129. for (int i = 0; i < action_set_array.size(); i++) {
  130. // Create our action set
  131. Ref<OpenXRActionSet> xr_action_set = action_set_array[i];
  132. ActionSet *action_set = create_action_set(xr_action_set->get_name(), xr_action_set->get_localized_name(), xr_action_set->get_priority());
  133. if (!action_set) {
  134. continue;
  135. }
  136. // Now create our actions for these
  137. Array actions = xr_action_set->get_actions();
  138. for (int j = 0; j < actions.size(); j++) {
  139. Ref<OpenXRAction> xr_action = actions[j];
  140. PackedStringArray toplevel_paths = xr_action->get_toplevel_paths();
  141. Vector<Tracker *> trackers_for_action;
  142. for (int k = 0; k < toplevel_paths.size(); k++) {
  143. // Only check for our tracker if our path is supported.
  144. if (openxr_api->is_top_level_path_supported(toplevel_paths[k])) {
  145. Tracker *tracker = find_tracker(toplevel_paths[k], true);
  146. if (tracker) {
  147. trackers_for_action.push_back(tracker);
  148. }
  149. }
  150. }
  151. // Only add our action if we have at least one valid toplevel path
  152. if (trackers_for_action.size() > 0) {
  153. Action *action = create_action(action_set, xr_action->get_name(), xr_action->get_localized_name(), xr_action->get_action_type(), trackers_for_action);
  154. if (action) {
  155. // add this to our map for creating our interaction profiles
  156. xr_actions[xr_action] = action;
  157. }
  158. }
  159. }
  160. }
  161. // now do our suggestions
  162. Array interaction_profile_array = action_map->get_interaction_profiles();
  163. for (int i = 0; i < interaction_profile_array.size(); i++) {
  164. Ref<OpenXRInteractionProfile> xr_interaction_profile = interaction_profile_array[i];
  165. // Note, we can only have one entry per interaction profile so if it already exists we clear it out
  166. RID ip = openxr_api->interaction_profile_create(xr_interaction_profile->get_interaction_profile_path());
  167. if (ip.is_valid()) {
  168. openxr_api->interaction_profile_clear_bindings(ip);
  169. Array xr_bindings = xr_interaction_profile->get_bindings();
  170. for (int j = 0; j < xr_bindings.size(); j++) {
  171. Ref<OpenXRIPBinding> xr_binding = xr_bindings[j];
  172. Ref<OpenXRAction> xr_action = xr_binding->get_action();
  173. Action *action = nullptr;
  174. if (xr_actions.has(xr_action)) {
  175. action = xr_actions[xr_action];
  176. } else {
  177. print_line("Action ", xr_action->get_name(), " isn't part of an action set!");
  178. continue;
  179. }
  180. PackedStringArray paths = xr_binding->get_paths();
  181. for (int k = 0; k < paths.size(); k++) {
  182. openxr_api->interaction_profile_add_binding(ip, action->action_rid, paths[k]);
  183. }
  184. }
  185. // Now submit our suggestions
  186. openxr_api->interaction_profile_suggest_bindings(ip);
  187. // And record it in our array so we can clean it up later on
  188. if (interaction_profile_array.has(ip)) {
  189. interaction_profile_array.push_back(ip);
  190. }
  191. }
  192. }
  193. }
  194. }
  195. OpenXRInterface::ActionSet *OpenXRInterface::create_action_set(const String &p_action_set_name, const String &p_localized_name, const int p_priority) {
  196. ERR_FAIL_NULL_V(openxr_api, nullptr);
  197. // find if it already exists
  198. for (int i = 0; i < action_sets.size(); i++) {
  199. if (action_sets[i]->action_set_name == p_action_set_name) {
  200. // already exists in this set
  201. return nullptr;
  202. }
  203. }
  204. ActionSet *action_set = memnew(ActionSet);
  205. action_set->action_set_name = p_action_set_name;
  206. action_set->is_active = true;
  207. action_set->action_set_rid = openxr_api->action_set_create(p_action_set_name, p_localized_name, p_priority);
  208. action_sets.push_back(action_set);
  209. return action_set;
  210. }
  211. void OpenXRInterface::free_action_sets() {
  212. ERR_FAIL_NULL(openxr_api);
  213. for (int i = 0; i < action_sets.size(); i++) {
  214. ActionSet *action_set = action_sets[i];
  215. free_actions(action_set);
  216. openxr_api->action_set_free(action_set->action_set_rid);
  217. memfree(action_set);
  218. }
  219. action_sets.clear();
  220. }
  221. OpenXRInterface::Action *OpenXRInterface::create_action(ActionSet *p_action_set, const String &p_action_name, const String &p_localized_name, OpenXRAction::ActionType p_action_type, const Vector<Tracker *> p_trackers) {
  222. ERR_FAIL_NULL_V(openxr_api, nullptr);
  223. for (int i = 0; i < p_action_set->actions.size(); i++) {
  224. if (p_action_set->actions[i]->action_name == p_action_name) {
  225. // already exists in this set
  226. return nullptr;
  227. }
  228. }
  229. Vector<RID> tracker_rids;
  230. for (int i = 0; i < p_trackers.size(); i++) {
  231. tracker_rids.push_back(p_trackers[i]->tracker_rid);
  232. }
  233. Action *action = memnew(Action);
  234. if (p_action_type == OpenXRAction::OPENXR_ACTION_POSE) {
  235. // We can't have dual action names in OpenXR hence we added _pose,
  236. // but default, aim and grip and default pose action names in Godot so rename them on the tracker.
  237. // NOTE need to decide on whether we should keep the naming convention or rename it on Godots side
  238. if (p_action_name == "default_pose") {
  239. action->action_name = "default";
  240. } else if (p_action_name == "aim_pose") {
  241. action->action_name = "aim";
  242. } else if (p_action_name == "grip_pose") {
  243. action->action_name = "grip";
  244. } else {
  245. action->action_name = p_action_name;
  246. }
  247. } else {
  248. action->action_name = p_action_name;
  249. }
  250. action->action_type = p_action_type;
  251. action->action_rid = openxr_api->action_create(p_action_set->action_set_rid, p_action_name, p_localized_name, p_action_type, tracker_rids);
  252. p_action_set->actions.push_back(action);
  253. // we link our actions back to our trackers so we know which actions to check when we're processing our trackers
  254. for (int i = 0; i < p_trackers.size(); i++) {
  255. if (p_trackers[i]->actions.find(action) == -1) {
  256. p_trackers[i]->actions.push_back(action);
  257. }
  258. }
  259. return action;
  260. }
  261. OpenXRInterface::Action *OpenXRInterface::find_action(const String &p_action_name) {
  262. // We just find the first action by this name
  263. for (int i = 0; i < action_sets.size(); i++) {
  264. for (int j = 0; j < action_sets[i]->actions.size(); j++) {
  265. if (action_sets[i]->actions[j]->action_name == p_action_name) {
  266. return action_sets[i]->actions[j];
  267. }
  268. }
  269. }
  270. // not found
  271. return nullptr;
  272. }
  273. void OpenXRInterface::free_actions(ActionSet *p_action_set) {
  274. ERR_FAIL_NULL(openxr_api);
  275. for (int i = 0; i < p_action_set->actions.size(); i++) {
  276. Action *action = p_action_set->actions[i];
  277. openxr_api->action_free(action->action_rid);
  278. memdelete(action);
  279. }
  280. p_action_set->actions.clear();
  281. }
  282. OpenXRInterface::Tracker *OpenXRInterface::find_tracker(const String &p_tracker_name, bool p_create) {
  283. XRServer *xr_server = XRServer::get_singleton();
  284. ERR_FAIL_NULL_V(xr_server, nullptr);
  285. ERR_FAIL_NULL_V(openxr_api, nullptr);
  286. Tracker *tracker = nullptr;
  287. for (int i = 0; i < trackers.size(); i++) {
  288. tracker = trackers[i];
  289. if (tracker->tracker_name == p_tracker_name) {
  290. return tracker;
  291. }
  292. }
  293. if (!p_create) {
  294. return nullptr;
  295. }
  296. ERR_FAIL_COND_V(!openxr_api->is_top_level_path_supported(p_tracker_name), nullptr);
  297. // Create our RID
  298. RID tracker_rid = openxr_api->tracker_create(p_tracker_name);
  299. ERR_FAIL_COND_V(tracker_rid.is_null(), nullptr);
  300. // create our positional tracker
  301. Ref<XRPositionalTracker> positional_tracker;
  302. positional_tracker.instantiate();
  303. // We have standardized some names to make things nicer to the user so lets recognize the toplevel paths related to these.
  304. if (p_tracker_name == "/user/hand/left") {
  305. positional_tracker->set_tracker_type(XRServer::TRACKER_CONTROLLER);
  306. positional_tracker->set_tracker_name("left_hand");
  307. positional_tracker->set_tracker_desc("Left hand controller");
  308. positional_tracker->set_tracker_hand(XRPositionalTracker::TRACKER_HAND_LEFT);
  309. } else if (p_tracker_name == "/user/hand/right") {
  310. positional_tracker->set_tracker_type(XRServer::TRACKER_CONTROLLER);
  311. positional_tracker->set_tracker_name("right_hand");
  312. positional_tracker->set_tracker_desc("Right hand controller");
  313. positional_tracker->set_tracker_hand(XRPositionalTracker::TRACKER_HAND_RIGHT);
  314. } else {
  315. positional_tracker->set_tracker_type(XRServer::TRACKER_CONTROLLER);
  316. positional_tracker->set_tracker_name(p_tracker_name);
  317. positional_tracker->set_tracker_desc(p_tracker_name);
  318. }
  319. positional_tracker->set_tracker_profile(INTERACTION_PROFILE_NONE);
  320. xr_server->add_tracker(positional_tracker);
  321. // create a new entry
  322. tracker = memnew(Tracker);
  323. tracker->tracker_name = p_tracker_name;
  324. tracker->tracker_rid = tracker_rid;
  325. tracker->positional_tracker = positional_tracker;
  326. tracker->interaction_profile = RID();
  327. trackers.push_back(tracker);
  328. return tracker;
  329. }
  330. void OpenXRInterface::tracker_profile_changed(RID p_tracker, RID p_interaction_profile) {
  331. Tracker *tracker = nullptr;
  332. for (int i = 0; i < trackers.size() && tracker == nullptr; i++) {
  333. if (trackers[i]->tracker_rid == p_tracker) {
  334. tracker = trackers[i];
  335. }
  336. }
  337. ERR_FAIL_NULL(tracker);
  338. tracker->interaction_profile = p_interaction_profile;
  339. if (p_interaction_profile.is_null()) {
  340. print_verbose("OpenXR: Interaction profile for " + tracker->tracker_name + " changed to " + INTERACTION_PROFILE_NONE);
  341. tracker->positional_tracker->set_tracker_profile(INTERACTION_PROFILE_NONE);
  342. } else {
  343. String name = openxr_api->interaction_profile_get_name(p_interaction_profile);
  344. print_verbose("OpenXR: Interaction profile for " + tracker->tracker_name + " changed to " + name);
  345. tracker->positional_tracker->set_tracker_profile(name);
  346. }
  347. }
  348. void OpenXRInterface::handle_tracker(Tracker *p_tracker) {
  349. ERR_FAIL_NULL(openxr_api);
  350. ERR_FAIL_COND(p_tracker->positional_tracker.is_null());
  351. // Note, which actions are actually bound to inputs are handled by our interaction profiles however interaction
  352. // profiles are suggested bindings for controller types we know about. OpenXR runtimes can stray away from these
  353. // and rebind them or even offer bindings to controllers that are not known to us.
  354. // We don't really have a consistent way to detect whether a controller is active however as long as it is
  355. // unbound it seems to be unavailable, so far unknown controller seem to mimic one of the profiles we've
  356. // supplied.
  357. if (p_tracker->interaction_profile.is_null()) {
  358. return;
  359. }
  360. // We check all actions that are related to our tracker.
  361. for (int i = 0; i < p_tracker->actions.size(); i++) {
  362. Action *action = p_tracker->actions[i];
  363. switch (action->action_type) {
  364. case OpenXRAction::OPENXR_ACTION_BOOL: {
  365. bool pressed = openxr_api->get_action_bool(action->action_rid, p_tracker->tracker_rid);
  366. p_tracker->positional_tracker->set_input(action->action_name, Variant(pressed));
  367. } break;
  368. case OpenXRAction::OPENXR_ACTION_FLOAT: {
  369. real_t value = openxr_api->get_action_float(action->action_rid, p_tracker->tracker_rid);
  370. p_tracker->positional_tracker->set_input(action->action_name, Variant(value));
  371. } break;
  372. case OpenXRAction::OPENXR_ACTION_VECTOR2: {
  373. Vector2 value = openxr_api->get_action_vector2(action->action_rid, p_tracker->tracker_rid);
  374. p_tracker->positional_tracker->set_input(action->action_name, Variant(value));
  375. } break;
  376. case OpenXRAction::OPENXR_ACTION_POSE: {
  377. Transform3D transform;
  378. Vector3 linear, angular;
  379. XRPose::TrackingConfidence confidence = openxr_api->get_action_pose(action->action_rid, p_tracker->tracker_rid, transform, linear, angular);
  380. if (confidence != XRPose::XR_TRACKING_CONFIDENCE_NONE) {
  381. p_tracker->positional_tracker->set_pose(action->action_name, transform, linear, angular, confidence);
  382. } else {
  383. p_tracker->positional_tracker->invalidate_pose(action->action_name);
  384. }
  385. } break;
  386. default: {
  387. // not yet supported
  388. } break;
  389. }
  390. }
  391. }
  392. void OpenXRInterface::trigger_haptic_pulse(const String &p_action_name, const StringName &p_tracker_name, double p_frequency, double p_amplitude, double p_duration_sec, double p_delay_sec) {
  393. ERR_FAIL_NULL(openxr_api);
  394. Action *action = find_action(p_action_name);
  395. ERR_FAIL_NULL(action);
  396. // We need to map our tracker name to our OpenXR name for our inbuild names.
  397. String tracker_name = p_tracker_name;
  398. if (tracker_name == "left_hand") {
  399. tracker_name = "/user/hand/left";
  400. } else if (tracker_name == "right_hand") {
  401. tracker_name = "/user/hand/right";
  402. }
  403. Tracker *tracker = find_tracker(tracker_name);
  404. ERR_FAIL_NULL(tracker);
  405. // TODO OpenXR does not support delay, so we may need to add support for that somehow...
  406. XrDuration duration = XrDuration(p_duration_sec * 1000000000.0); // seconds -> nanoseconds
  407. openxr_api->trigger_haptic_pulse(action->action_rid, tracker->tracker_rid, p_frequency, p_amplitude, duration);
  408. }
  409. void OpenXRInterface::free_trackers() {
  410. XRServer *xr_server = XRServer::get_singleton();
  411. ERR_FAIL_NULL(xr_server);
  412. ERR_FAIL_NULL(openxr_api);
  413. for (int i = 0; i < trackers.size(); i++) {
  414. Tracker *tracker = trackers[i];
  415. openxr_api->tracker_free(tracker->tracker_rid);
  416. xr_server->remove_tracker(tracker->positional_tracker);
  417. tracker->positional_tracker.unref();
  418. memdelete(tracker);
  419. }
  420. trackers.clear();
  421. }
  422. void OpenXRInterface::free_interaction_profiles() {
  423. ERR_FAIL_NULL(openxr_api);
  424. for (int i = 0; i < interaction_profiles.size(); i++) {
  425. openxr_api->interaction_profile_free(interaction_profiles[i]);
  426. }
  427. interaction_profiles.clear();
  428. }
  429. bool OpenXRInterface::initialize_on_startup() const {
  430. if (openxr_api == nullptr) {
  431. return false;
  432. } else if (!openxr_api->is_initialized()) {
  433. return false;
  434. } else {
  435. return true;
  436. }
  437. }
  438. bool OpenXRInterface::is_initialized() const {
  439. return initialized;
  440. };
  441. bool OpenXRInterface::initialize() {
  442. XRServer *xr_server = XRServer::get_singleton();
  443. ERR_FAIL_NULL_V(xr_server, false);
  444. if (openxr_api == nullptr) {
  445. return false;
  446. } else if (!openxr_api->is_initialized()) {
  447. return false;
  448. } else if (initialized) {
  449. return true;
  450. }
  451. // load up our action sets before setting up our session, note that our profiles are suggestions, OpenXR takes ownership of (re)binding
  452. _load_action_map();
  453. if (!openxr_api->initialize_session()) {
  454. return false;
  455. }
  456. // we must create a tracker for our head
  457. head.instantiate();
  458. head->set_tracker_type(XRServer::TRACKER_HEAD);
  459. head->set_tracker_name("head");
  460. head->set_tracker_desc("Players head");
  461. xr_server->add_tracker(head);
  462. // attach action sets
  463. for (int i = 0; i < action_sets.size(); i++) {
  464. openxr_api->action_set_attach(action_sets[i]->action_set_rid);
  465. }
  466. // make this our primary interface
  467. xr_server->set_primary_interface(this);
  468. initialized = true;
  469. return initialized;
  470. }
  471. void OpenXRInterface::uninitialize() {
  472. // Our OpenXR driver will clean itself up properly when Godot exits, so we just do some basic stuff here
  473. // end the session if we need to?
  474. // cleanup stuff
  475. free_trackers();
  476. free_interaction_profiles();
  477. free_action_sets();
  478. XRServer *xr_server = XRServer::get_singleton();
  479. if (xr_server) {
  480. if (head.is_valid()) {
  481. xr_server->remove_tracker(head);
  482. head.unref();
  483. }
  484. }
  485. initialized = false;
  486. }
  487. Dictionary OpenXRInterface::get_system_info() {
  488. Dictionary dict;
  489. if (openxr_api) {
  490. dict[SNAME("XRRuntimeName")] = openxr_api->get_runtime_name();
  491. dict[SNAME("XRRuntimeVersion")] = openxr_api->get_runtime_version();
  492. }
  493. return dict;
  494. }
  495. bool OpenXRInterface::supports_play_area_mode(XRInterface::PlayAreaMode p_mode) {
  496. return false;
  497. }
  498. XRInterface::PlayAreaMode OpenXRInterface::get_play_area_mode() const {
  499. return XRInterface::XR_PLAY_AREA_UNKNOWN;
  500. }
  501. bool OpenXRInterface::set_play_area_mode(XRInterface::PlayAreaMode p_mode) {
  502. return false;
  503. }
  504. float OpenXRInterface::get_display_refresh_rate() const {
  505. if (openxr_api == nullptr) {
  506. return 0.0;
  507. } else if (!openxr_api->is_initialized()) {
  508. return 0.0;
  509. } else {
  510. return openxr_api->get_display_refresh_rate();
  511. }
  512. }
  513. void OpenXRInterface::set_display_refresh_rate(float p_refresh_rate) {
  514. if (openxr_api == nullptr) {
  515. return;
  516. } else if (!openxr_api->is_initialized()) {
  517. return;
  518. } else {
  519. openxr_api->set_display_refresh_rate(p_refresh_rate);
  520. }
  521. }
  522. Array OpenXRInterface::get_available_display_refresh_rates() const {
  523. if (openxr_api == nullptr) {
  524. return Array();
  525. } else if (!openxr_api->is_initialized()) {
  526. return Array();
  527. } else {
  528. return openxr_api->get_available_display_refresh_rates();
  529. }
  530. }
  531. bool OpenXRInterface::is_action_set_active(const String &p_action_set) const {
  532. for (ActionSet *action_set : action_sets) {
  533. if (action_set->action_set_name == p_action_set) {
  534. return action_set->is_active;
  535. }
  536. }
  537. WARN_PRINT("OpenXR: Unknown action set " + p_action_set);
  538. return false;
  539. }
  540. void OpenXRInterface::set_action_set_active(const String &p_action_set, bool p_active) {
  541. for (ActionSet *action_set : action_sets) {
  542. if (action_set->action_set_name == p_action_set) {
  543. action_set->is_active = p_active;
  544. return;
  545. }
  546. }
  547. WARN_PRINT("OpenXR: Unknown action set " + p_action_set);
  548. }
  549. Array OpenXRInterface::get_action_sets() const {
  550. Array arr;
  551. for (ActionSet *action_set : action_sets) {
  552. arr.push_back(action_set->action_set_name);
  553. }
  554. return arr;
  555. }
  556. double OpenXRInterface::get_render_target_size_multiplier() const {
  557. if (openxr_api == nullptr) {
  558. return 1.0;
  559. } else {
  560. return openxr_api->get_render_target_size_multiplier();
  561. }
  562. }
  563. void OpenXRInterface::set_render_target_size_multiplier(double multiplier) {
  564. if (openxr_api == nullptr) {
  565. return;
  566. } else {
  567. openxr_api->set_render_target_size_multiplier(multiplier);
  568. }
  569. }
  570. Size2 OpenXRInterface::get_render_target_size() {
  571. if (openxr_api == nullptr) {
  572. return Size2();
  573. } else {
  574. return openxr_api->get_recommended_target_size();
  575. }
  576. }
  577. uint32_t OpenXRInterface::get_view_count() {
  578. // TODO set this based on our configuration
  579. return 2;
  580. }
  581. void OpenXRInterface::_set_default_pos(Transform3D &p_transform, double p_world_scale, uint64_t p_eye) {
  582. p_transform = Transform3D();
  583. // if we're not tracking, don't put our head on the floor...
  584. p_transform.origin.y = 1.5 * p_world_scale;
  585. // overkill but..
  586. if (p_eye == 1) {
  587. p_transform.origin.x = 0.03 * p_world_scale;
  588. } else if (p_eye == 2) {
  589. p_transform.origin.x = -0.03 * p_world_scale;
  590. }
  591. }
  592. Transform3D OpenXRInterface::get_camera_transform() {
  593. XRServer *xr_server = XRServer::get_singleton();
  594. ERR_FAIL_NULL_V(xr_server, Transform3D());
  595. Transform3D hmd_transform;
  596. double world_scale = xr_server->get_world_scale();
  597. // head_transform should be updated in process
  598. hmd_transform.basis = head_transform.basis;
  599. hmd_transform.origin = head_transform.origin * world_scale;
  600. return hmd_transform;
  601. }
  602. Transform3D OpenXRInterface::get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) {
  603. XRServer *xr_server = XRServer::get_singleton();
  604. ERR_FAIL_NULL_V(xr_server, Transform3D());
  605. ERR_FAIL_UNSIGNED_INDEX_V_MSG(p_view, get_view_count(), Transform3D(), "View index outside bounds.");
  606. Transform3D t;
  607. if (openxr_api && openxr_api->get_view_transform(p_view, t)) {
  608. // update our cached value if we have a valid transform
  609. transform_for_view[p_view] = t;
  610. } else {
  611. // reuse cached value
  612. t = transform_for_view[p_view];
  613. }
  614. // Apply our world scale
  615. double world_scale = xr_server->get_world_scale();
  616. t.origin *= world_scale;
  617. return p_cam_transform * xr_server->get_reference_frame() * t;
  618. }
  619. Projection OpenXRInterface::get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) {
  620. Projection cm;
  621. ERR_FAIL_UNSIGNED_INDEX_V_MSG(p_view, get_view_count(), cm, "View index outside bounds.");
  622. if (openxr_api) {
  623. if (openxr_api->get_view_projection(p_view, p_z_near, p_z_far, cm)) {
  624. return cm;
  625. }
  626. }
  627. // Failed to get from our OpenXR device? Default to some sort of sensible camera matrix..
  628. cm.set_for_hmd(p_view + 1, 1.0, 6.0, 14.5, 4.0, 1.5, p_z_near, p_z_far);
  629. return cm;
  630. }
  631. RID OpenXRInterface::get_color_texture() {
  632. if (openxr_api) {
  633. return openxr_api->get_color_texture();
  634. } else {
  635. return RID();
  636. }
  637. }
  638. RID OpenXRInterface::get_depth_texture() {
  639. if (openxr_api) {
  640. return openxr_api->get_depth_texture();
  641. } else {
  642. return RID();
  643. }
  644. }
  645. void OpenXRInterface::process() {
  646. if (openxr_api) {
  647. // do our normal process
  648. if (openxr_api->process()) {
  649. Transform3D t;
  650. Vector3 linear_velocity;
  651. Vector3 angular_velocity;
  652. XRPose::TrackingConfidence confidence = openxr_api->get_head_center(t, linear_velocity, angular_velocity);
  653. if (confidence != XRPose::XR_TRACKING_CONFIDENCE_NONE) {
  654. // Only update our transform if we have one to update it with
  655. // note that poses are stored without world scale and reference frame applied!
  656. head_transform = t;
  657. head_linear_velocity = linear_velocity;
  658. head_angular_velocity = angular_velocity;
  659. }
  660. }
  661. // handle our action sets....
  662. Vector<RID> active_sets;
  663. for (int i = 0; i < action_sets.size(); i++) {
  664. if (action_sets[i]->is_active) {
  665. active_sets.push_back(action_sets[i]->action_set_rid);
  666. }
  667. }
  668. if (openxr_api->sync_action_sets(active_sets)) {
  669. for (int i = 0; i < trackers.size(); i++) {
  670. handle_tracker(trackers[i]);
  671. }
  672. }
  673. }
  674. if (head.is_valid()) {
  675. // TODO figure out how to get our velocities
  676. head->set_pose("default", head_transform, head_linear_velocity, head_angular_velocity);
  677. // TODO set confidence on pose once we support tracking this..
  678. }
  679. }
  680. void OpenXRInterface::pre_render() {
  681. if (openxr_api) {
  682. openxr_api->pre_render();
  683. }
  684. }
  685. bool OpenXRInterface::pre_draw_viewport(RID p_render_target) {
  686. if (openxr_api) {
  687. return openxr_api->pre_draw_viewport(p_render_target);
  688. } else {
  689. // don't render
  690. return false;
  691. }
  692. }
  693. Vector<BlitToScreen> OpenXRInterface::post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) {
  694. Vector<BlitToScreen> blit_to_screen;
  695. #ifndef ANDROID_ENABLED
  696. // If separate HMD we should output one eye to screen
  697. if (p_screen_rect != Rect2()) {
  698. BlitToScreen blit;
  699. blit.render_target = p_render_target;
  700. blit.multi_view.use_layer = true;
  701. blit.multi_view.layer = 0;
  702. blit.lens_distortion.apply = false;
  703. Size2 render_size = get_render_target_size();
  704. Rect2 dst_rect = p_screen_rect;
  705. float new_height = dst_rect.size.x * (render_size.y / render_size.x);
  706. if (new_height > dst_rect.size.y) {
  707. dst_rect.position.y = (0.5 * dst_rect.size.y) - (0.5 * new_height);
  708. dst_rect.size.y = new_height;
  709. } else {
  710. float new_width = dst_rect.size.y * (render_size.x / render_size.y);
  711. dst_rect.position.x = (0.5 * dst_rect.size.x) - (0.5 * new_width);
  712. dst_rect.size.x = new_width;
  713. }
  714. blit.dst_rect = dst_rect;
  715. blit_to_screen.push_back(blit);
  716. }
  717. #endif
  718. if (openxr_api) {
  719. openxr_api->post_draw_viewport(p_render_target);
  720. }
  721. return blit_to_screen;
  722. }
  723. void OpenXRInterface::end_frame() {
  724. if (openxr_api) {
  725. openxr_api->end_frame();
  726. }
  727. }
  728. bool OpenXRInterface::is_passthrough_supported() {
  729. return passthrough_wrapper != nullptr && passthrough_wrapper->is_passthrough_supported();
  730. }
  731. bool OpenXRInterface::is_passthrough_enabled() {
  732. return passthrough_wrapper != nullptr && passthrough_wrapper->is_passthrough_enabled();
  733. }
  734. bool OpenXRInterface::start_passthrough() {
  735. return passthrough_wrapper != nullptr && passthrough_wrapper->start_passthrough();
  736. }
  737. void OpenXRInterface::stop_passthrough() {
  738. if (passthrough_wrapper) {
  739. passthrough_wrapper->stop_passthrough();
  740. }
  741. }
  742. Array OpenXRInterface::get_supported_environment_blend_modes() {
  743. Array modes;
  744. if (!openxr_api) {
  745. return modes;
  746. }
  747. uint32_t count = 0;
  748. const XrEnvironmentBlendMode *env_blend_modes = openxr_api->get_supported_environment_blend_modes(count);
  749. if (!env_blend_modes) {
  750. return modes;
  751. }
  752. for (uint32_t i = 0; i < count; i++) {
  753. switch (env_blend_modes[i]) {
  754. case XR_ENVIRONMENT_BLEND_MODE_OPAQUE:
  755. modes.push_back(XR_ENV_BLEND_MODE_OPAQUE);
  756. break;
  757. case XR_ENVIRONMENT_BLEND_MODE_ADDITIVE:
  758. modes.push_back(XR_ENV_BLEND_MODE_ADDITIVE);
  759. break;
  760. case XR_ENVIRONMENT_BLEND_MODE_ALPHA_BLEND:
  761. modes.push_back(XR_ENV_BLEND_MODE_ALPHA_BLEND);
  762. break;
  763. default:
  764. WARN_PRINT("Unsupported blend mode found: " + String::num_int64(int64_t(env_blend_modes[i])));
  765. }
  766. }
  767. return modes;
  768. }
  769. bool OpenXRInterface::set_environment_blend_mode(XRInterface::EnvironmentBlendMode mode) {
  770. if (openxr_api) {
  771. XrEnvironmentBlendMode oxr_blend_mode;
  772. switch (mode) {
  773. case XR_ENV_BLEND_MODE_OPAQUE:
  774. oxr_blend_mode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE;
  775. break;
  776. case XR_ENV_BLEND_MODE_ADDITIVE:
  777. oxr_blend_mode = XR_ENVIRONMENT_BLEND_MODE_ADDITIVE;
  778. break;
  779. case XR_ENV_BLEND_MODE_ALPHA_BLEND:
  780. oxr_blend_mode = XR_ENVIRONMENT_BLEND_MODE_ALPHA_BLEND;
  781. break;
  782. default:
  783. WARN_PRINT("Unknown blend mode requested: " + String::num_int64(int64_t(mode)));
  784. oxr_blend_mode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE;
  785. }
  786. return openxr_api->set_environment_blend_mode(oxr_blend_mode);
  787. }
  788. return false;
  789. }
  790. void OpenXRInterface::on_state_ready() {
  791. emit_signal(SNAME("session_begun"));
  792. }
  793. void OpenXRInterface::on_state_visible() {
  794. emit_signal(SNAME("session_visible"));
  795. }
  796. void OpenXRInterface::on_state_focused() {
  797. emit_signal(SNAME("session_focussed"));
  798. }
  799. void OpenXRInterface::on_state_stopping() {
  800. emit_signal(SNAME("session_stopping"));
  801. }
  802. void OpenXRInterface::on_pose_recentered() {
  803. emit_signal(SNAME("pose_recentered"));
  804. }
  805. OpenXRInterface::OpenXRInterface() {
  806. openxr_api = OpenXRAPI::get_singleton();
  807. if (openxr_api) {
  808. openxr_api->set_xr_interface(this);
  809. }
  810. // while we don't have head tracking, don't put the headset on the floor...
  811. _set_default_pos(head_transform, 1.0, 0);
  812. _set_default_pos(transform_for_view[0], 1.0, 1);
  813. _set_default_pos(transform_for_view[1], 1.0, 2);
  814. passthrough_wrapper = OpenXRFbPassthroughExtensionWrapper::get_singleton();
  815. }
  816. OpenXRInterface::~OpenXRInterface() {
  817. if (is_initialized()) {
  818. uninitialize();
  819. }
  820. if (openxr_api) {
  821. openxr_api->set_xr_interface(nullptr);
  822. openxr_api = nullptr;
  823. }
  824. }