openxr_interface.cpp 28 KB

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