2
0

openxr_interface.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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. Vector<RID> loaded_action_sets;
  464. for (int i = 0; i < action_sets.size(); i++) {
  465. loaded_action_sets.append(action_sets[i]->action_set_rid);
  466. }
  467. openxr_api->attach_action_sets(loaded_action_sets);
  468. // make this our primary interface
  469. xr_server->set_primary_interface(this);
  470. initialized = true;
  471. return initialized;
  472. }
  473. void OpenXRInterface::uninitialize() {
  474. // Our OpenXR driver will clean itself up properly when Godot exits, so we just do some basic stuff here
  475. // end the session if we need to?
  476. // cleanup stuff
  477. free_trackers();
  478. free_interaction_profiles();
  479. free_action_sets();
  480. XRServer *xr_server = XRServer::get_singleton();
  481. if (xr_server) {
  482. if (head.is_valid()) {
  483. xr_server->remove_tracker(head);
  484. head.unref();
  485. }
  486. }
  487. initialized = false;
  488. }
  489. Dictionary OpenXRInterface::get_system_info() {
  490. Dictionary dict;
  491. if (openxr_api) {
  492. dict[SNAME("XRRuntimeName")] = openxr_api->get_runtime_name();
  493. dict[SNAME("XRRuntimeVersion")] = openxr_api->get_runtime_version();
  494. }
  495. return dict;
  496. }
  497. bool OpenXRInterface::supports_play_area_mode(XRInterface::PlayAreaMode p_mode) {
  498. return false;
  499. }
  500. XRInterface::PlayAreaMode OpenXRInterface::get_play_area_mode() const {
  501. return XRInterface::XR_PLAY_AREA_UNKNOWN;
  502. }
  503. bool OpenXRInterface::set_play_area_mode(XRInterface::PlayAreaMode p_mode) {
  504. return false;
  505. }
  506. float OpenXRInterface::get_display_refresh_rate() const {
  507. if (openxr_api == nullptr) {
  508. return 0.0;
  509. } else if (!openxr_api->is_initialized()) {
  510. return 0.0;
  511. } else {
  512. return openxr_api->get_display_refresh_rate();
  513. }
  514. }
  515. void OpenXRInterface::set_display_refresh_rate(float p_refresh_rate) {
  516. if (openxr_api == nullptr) {
  517. return;
  518. } else if (!openxr_api->is_initialized()) {
  519. return;
  520. } else {
  521. openxr_api->set_display_refresh_rate(p_refresh_rate);
  522. }
  523. }
  524. Array OpenXRInterface::get_available_display_refresh_rates() const {
  525. if (openxr_api == nullptr) {
  526. return Array();
  527. } else if (!openxr_api->is_initialized()) {
  528. return Array();
  529. } else {
  530. return openxr_api->get_available_display_refresh_rates();
  531. }
  532. }
  533. bool OpenXRInterface::is_action_set_active(const String &p_action_set) const {
  534. for (ActionSet *action_set : action_sets) {
  535. if (action_set->action_set_name == p_action_set) {
  536. return action_set->is_active;
  537. }
  538. }
  539. WARN_PRINT("OpenXR: Unknown action set " + p_action_set);
  540. return false;
  541. }
  542. void OpenXRInterface::set_action_set_active(const String &p_action_set, bool p_active) {
  543. for (ActionSet *action_set : action_sets) {
  544. if (action_set->action_set_name == p_action_set) {
  545. action_set->is_active = p_active;
  546. return;
  547. }
  548. }
  549. WARN_PRINT("OpenXR: Unknown action set " + p_action_set);
  550. }
  551. Array OpenXRInterface::get_action_sets() const {
  552. Array arr;
  553. for (ActionSet *action_set : action_sets) {
  554. arr.push_back(action_set->action_set_name);
  555. }
  556. return arr;
  557. }
  558. double OpenXRInterface::get_render_target_size_multiplier() const {
  559. if (openxr_api == nullptr) {
  560. return 1.0;
  561. } else {
  562. return openxr_api->get_render_target_size_multiplier();
  563. }
  564. }
  565. void OpenXRInterface::set_render_target_size_multiplier(double multiplier) {
  566. if (openxr_api == nullptr) {
  567. return;
  568. } else {
  569. openxr_api->set_render_target_size_multiplier(multiplier);
  570. }
  571. }
  572. Size2 OpenXRInterface::get_render_target_size() {
  573. if (openxr_api == nullptr) {
  574. return Size2();
  575. } else {
  576. return openxr_api->get_recommended_target_size();
  577. }
  578. }
  579. uint32_t OpenXRInterface::get_view_count() {
  580. // TODO set this based on our configuration
  581. return 2;
  582. }
  583. void OpenXRInterface::_set_default_pos(Transform3D &p_transform, double p_world_scale, uint64_t p_eye) {
  584. p_transform = Transform3D();
  585. // if we're not tracking, don't put our head on the floor...
  586. p_transform.origin.y = 1.5 * p_world_scale;
  587. // overkill but..
  588. if (p_eye == 1) {
  589. p_transform.origin.x = 0.03 * p_world_scale;
  590. } else if (p_eye == 2) {
  591. p_transform.origin.x = -0.03 * p_world_scale;
  592. }
  593. }
  594. Transform3D OpenXRInterface::get_camera_transform() {
  595. XRServer *xr_server = XRServer::get_singleton();
  596. ERR_FAIL_NULL_V(xr_server, Transform3D());
  597. Transform3D hmd_transform;
  598. double world_scale = xr_server->get_world_scale();
  599. // head_transform should be updated in process
  600. hmd_transform.basis = head_transform.basis;
  601. hmd_transform.origin = head_transform.origin * world_scale;
  602. return hmd_transform;
  603. }
  604. Transform3D OpenXRInterface::get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) {
  605. XRServer *xr_server = XRServer::get_singleton();
  606. ERR_FAIL_NULL_V(xr_server, Transform3D());
  607. ERR_FAIL_UNSIGNED_INDEX_V_MSG(p_view, get_view_count(), Transform3D(), "View index outside bounds.");
  608. Transform3D t;
  609. if (openxr_api && openxr_api->get_view_transform(p_view, t)) {
  610. // update our cached value if we have a valid transform
  611. transform_for_view[p_view] = t;
  612. } else {
  613. // reuse cached value
  614. t = transform_for_view[p_view];
  615. }
  616. // Apply our world scale
  617. double world_scale = xr_server->get_world_scale();
  618. t.origin *= world_scale;
  619. return p_cam_transform * xr_server->get_reference_frame() * t;
  620. }
  621. Projection OpenXRInterface::get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) {
  622. Projection cm;
  623. ERR_FAIL_UNSIGNED_INDEX_V_MSG(p_view, get_view_count(), cm, "View index outside bounds.");
  624. if (openxr_api) {
  625. if (openxr_api->get_view_projection(p_view, p_z_near, p_z_far, cm)) {
  626. return cm;
  627. }
  628. }
  629. // Failed to get from our OpenXR device? Default to some sort of sensible camera matrix..
  630. cm.set_for_hmd(p_view + 1, 1.0, 6.0, 14.5, 4.0, 1.5, p_z_near, p_z_far);
  631. return cm;
  632. }
  633. RID OpenXRInterface::get_color_texture() {
  634. if (openxr_api) {
  635. return openxr_api->get_color_texture();
  636. } else {
  637. return RID();
  638. }
  639. }
  640. RID OpenXRInterface::get_depth_texture() {
  641. if (openxr_api) {
  642. return openxr_api->get_depth_texture();
  643. } else {
  644. return RID();
  645. }
  646. }
  647. void OpenXRInterface::process() {
  648. if (openxr_api) {
  649. // do our normal process
  650. if (openxr_api->process()) {
  651. Transform3D t;
  652. Vector3 linear_velocity;
  653. Vector3 angular_velocity;
  654. XRPose::TrackingConfidence confidence = openxr_api->get_head_center(t, linear_velocity, angular_velocity);
  655. if (confidence != XRPose::XR_TRACKING_CONFIDENCE_NONE) {
  656. // Only update our transform if we have one to update it with
  657. // note that poses are stored without world scale and reference frame applied!
  658. head_transform = t;
  659. head_linear_velocity = linear_velocity;
  660. head_angular_velocity = angular_velocity;
  661. }
  662. }
  663. // handle our action sets....
  664. Vector<RID> active_sets;
  665. for (int i = 0; i < action_sets.size(); i++) {
  666. if (action_sets[i]->is_active) {
  667. active_sets.push_back(action_sets[i]->action_set_rid);
  668. }
  669. }
  670. if (openxr_api->sync_action_sets(active_sets)) {
  671. for (int i = 0; i < trackers.size(); i++) {
  672. handle_tracker(trackers[i]);
  673. }
  674. }
  675. }
  676. if (head.is_valid()) {
  677. // TODO figure out how to get our velocities
  678. head->set_pose("default", head_transform, head_linear_velocity, head_angular_velocity);
  679. // TODO set confidence on pose once we support tracking this..
  680. }
  681. }
  682. void OpenXRInterface::pre_render() {
  683. if (openxr_api) {
  684. openxr_api->pre_render();
  685. }
  686. }
  687. bool OpenXRInterface::pre_draw_viewport(RID p_render_target) {
  688. if (openxr_api) {
  689. return openxr_api->pre_draw_viewport(p_render_target);
  690. } else {
  691. // don't render
  692. return false;
  693. }
  694. }
  695. Vector<BlitToScreen> OpenXRInterface::post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) {
  696. Vector<BlitToScreen> blit_to_screen;
  697. #ifndef ANDROID_ENABLED
  698. // If separate HMD we should output one eye to screen
  699. if (p_screen_rect != Rect2()) {
  700. BlitToScreen blit;
  701. blit.render_target = p_render_target;
  702. blit.multi_view.use_layer = true;
  703. blit.multi_view.layer = 0;
  704. blit.lens_distortion.apply = false;
  705. Size2 render_size = get_render_target_size();
  706. Rect2 dst_rect = p_screen_rect;
  707. float new_height = dst_rect.size.x * (render_size.y / render_size.x);
  708. if (new_height > dst_rect.size.y) {
  709. dst_rect.position.y = (0.5 * dst_rect.size.y) - (0.5 * new_height);
  710. dst_rect.size.y = new_height;
  711. } else {
  712. float new_width = dst_rect.size.y * (render_size.x / render_size.y);
  713. dst_rect.position.x = (0.5 * dst_rect.size.x) - (0.5 * new_width);
  714. dst_rect.size.x = new_width;
  715. }
  716. blit.dst_rect = dst_rect;
  717. blit_to_screen.push_back(blit);
  718. }
  719. #endif
  720. if (openxr_api) {
  721. openxr_api->post_draw_viewport(p_render_target);
  722. }
  723. return blit_to_screen;
  724. }
  725. void OpenXRInterface::end_frame() {
  726. if (openxr_api) {
  727. openxr_api->end_frame();
  728. }
  729. }
  730. bool OpenXRInterface::is_passthrough_supported() {
  731. return passthrough_wrapper != nullptr && passthrough_wrapper->is_passthrough_supported();
  732. }
  733. bool OpenXRInterface::is_passthrough_enabled() {
  734. return passthrough_wrapper != nullptr && passthrough_wrapper->is_passthrough_enabled();
  735. }
  736. bool OpenXRInterface::start_passthrough() {
  737. return passthrough_wrapper != nullptr && passthrough_wrapper->start_passthrough();
  738. }
  739. void OpenXRInterface::stop_passthrough() {
  740. if (passthrough_wrapper) {
  741. passthrough_wrapper->stop_passthrough();
  742. }
  743. }
  744. Array OpenXRInterface::get_supported_environment_blend_modes() {
  745. Array modes;
  746. if (!openxr_api) {
  747. return modes;
  748. }
  749. uint32_t count = 0;
  750. const XrEnvironmentBlendMode *env_blend_modes = openxr_api->get_supported_environment_blend_modes(count);
  751. if (!env_blend_modes) {
  752. return modes;
  753. }
  754. for (uint32_t i = 0; i < count; i++) {
  755. switch (env_blend_modes[i]) {
  756. case XR_ENVIRONMENT_BLEND_MODE_OPAQUE:
  757. modes.push_back(XR_ENV_BLEND_MODE_OPAQUE);
  758. break;
  759. case XR_ENVIRONMENT_BLEND_MODE_ADDITIVE:
  760. modes.push_back(XR_ENV_BLEND_MODE_ADDITIVE);
  761. break;
  762. case XR_ENVIRONMENT_BLEND_MODE_ALPHA_BLEND:
  763. modes.push_back(XR_ENV_BLEND_MODE_ALPHA_BLEND);
  764. break;
  765. default:
  766. WARN_PRINT("Unsupported blend mode found: " + String::num_int64(int64_t(env_blend_modes[i])));
  767. }
  768. }
  769. return modes;
  770. }
  771. bool OpenXRInterface::set_environment_blend_mode(XRInterface::EnvironmentBlendMode mode) {
  772. if (openxr_api) {
  773. XrEnvironmentBlendMode oxr_blend_mode;
  774. switch (mode) {
  775. case XR_ENV_BLEND_MODE_OPAQUE:
  776. oxr_blend_mode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE;
  777. break;
  778. case XR_ENV_BLEND_MODE_ADDITIVE:
  779. oxr_blend_mode = XR_ENVIRONMENT_BLEND_MODE_ADDITIVE;
  780. break;
  781. case XR_ENV_BLEND_MODE_ALPHA_BLEND:
  782. oxr_blend_mode = XR_ENVIRONMENT_BLEND_MODE_ALPHA_BLEND;
  783. break;
  784. default:
  785. WARN_PRINT("Unknown blend mode requested: " + String::num_int64(int64_t(mode)));
  786. oxr_blend_mode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE;
  787. }
  788. return openxr_api->set_environment_blend_mode(oxr_blend_mode);
  789. }
  790. return false;
  791. }
  792. void OpenXRInterface::on_state_ready() {
  793. emit_signal(SNAME("session_begun"));
  794. }
  795. void OpenXRInterface::on_state_visible() {
  796. emit_signal(SNAME("session_visible"));
  797. }
  798. void OpenXRInterface::on_state_focused() {
  799. emit_signal(SNAME("session_focussed"));
  800. }
  801. void OpenXRInterface::on_state_stopping() {
  802. emit_signal(SNAME("session_stopping"));
  803. }
  804. void OpenXRInterface::on_pose_recentered() {
  805. emit_signal(SNAME("pose_recentered"));
  806. }
  807. OpenXRInterface::OpenXRInterface() {
  808. openxr_api = OpenXRAPI::get_singleton();
  809. if (openxr_api) {
  810. openxr_api->set_xr_interface(this);
  811. }
  812. // while we don't have head tracking, don't put the headset on the floor...
  813. _set_default_pos(head_transform, 1.0, 0);
  814. _set_default_pos(transform_for_view[0], 1.0, 1);
  815. _set_default_pos(transform_for_view[1], 1.0, 2);
  816. passthrough_wrapper = OpenXRFbPassthroughExtensionWrapper::get_singleton();
  817. }
  818. OpenXRInterface::~OpenXRInterface() {
  819. if (is_initialized()) {
  820. uninitialize();
  821. }
  822. if (openxr_api) {
  823. openxr_api->set_xr_interface(nullptr);
  824. openxr_api = nullptr;
  825. }
  826. }