openxr_interface.cpp 30 KB

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