openxr_api.cpp 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433
  1. /*************************************************************************/
  2. /* openxr_api.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_api.h"
  31. #include "openxr_util.h"
  32. #include "core/config/engine.h"
  33. #include "core/config/project_settings.h"
  34. #include "core/os/memory.h"
  35. #include "core/version.h"
  36. #ifdef TOOLS_ENABLED
  37. #include "editor/editor_settings.h"
  38. #endif
  39. #ifdef ANDROID_ENABLED
  40. #include "extensions/openxr_android_extension.h"
  41. #endif
  42. #ifdef VULKAN_ENABLED
  43. #include "extensions/openxr_vulkan_extension.h"
  44. #endif
  45. #include "extensions/openxr_htc_vive_tracker_extension.h"
  46. #include "modules/openxr/openxr_interface.h"
  47. OpenXRAPI *OpenXRAPI::singleton = nullptr;
  48. bool OpenXRAPI::openxr_is_enabled(bool p_check_run_in_editor) {
  49. // @TODO we need an overrule switch so we can force enable openxr, i.e run "godot --openxr_enabled"
  50. if (Engine::get_singleton()->is_editor_hint() && p_check_run_in_editor) {
  51. #ifdef TOOLS_ENABLED
  52. // Disabled for now, using XR inside of the editor we'll be working on during the coming months.
  53. return false;
  54. // bool enabled = GLOBAL_GET("xr/openxr/in_editor"); // EDITOR_GET("xr/openxr/in_editor");
  55. // return enabled;
  56. #else
  57. // we should never get here, editor hint won't be true if the editor isn't compiled in.
  58. return false;
  59. #endif
  60. } else {
  61. bool enabled = GLOBAL_GET("xr/openxr/enabled");
  62. return enabled;
  63. }
  64. }
  65. OpenXRAPI *OpenXRAPI::get_singleton() {
  66. return singleton;
  67. }
  68. String OpenXRAPI::get_default_action_map_resource_name() {
  69. String name = GLOBAL_GET("xr/openxr/default_action_map");
  70. return name;
  71. }
  72. String OpenXRAPI::get_error_string(XrResult result) {
  73. if (XR_SUCCEEDED(result)) {
  74. return String("Succeeded");
  75. }
  76. if (instance == XR_NULL_HANDLE) {
  77. Array args;
  78. args.push_back(Variant(result));
  79. return String("Error code {0}").format(args);
  80. }
  81. char resultString[XR_MAX_RESULT_STRING_SIZE];
  82. xrResultToString(instance, result, resultString);
  83. return String(resultString);
  84. }
  85. String OpenXRAPI::get_swapchain_format_name(int64_t p_swapchain_format) const {
  86. // This is rendering engine dependent...
  87. if (graphics_extension) {
  88. return graphics_extension->get_swapchain_format_name(p_swapchain_format);
  89. }
  90. return String("Swapchain format ") + String::num_int64(int64_t(p_swapchain_format));
  91. }
  92. bool OpenXRAPI::load_layer_properties() {
  93. // This queries additional layers that are available and can be initialized when we create our OpenXR instance
  94. if (layer_properties != nullptr) {
  95. // already retrieved this
  96. return true;
  97. }
  98. // Note, instance is not yet setup so we can't use get_error_string to retrieve our error
  99. XrResult result = xrEnumerateApiLayerProperties(0, &num_layer_properties, nullptr);
  100. ERR_FAIL_COND_V_MSG(XR_FAILED(result), false, "OpenXR: Failed to enumerate number of api layer properties");
  101. layer_properties = (XrApiLayerProperties *)memalloc(sizeof(XrApiLayerProperties) * num_layer_properties);
  102. ERR_FAIL_NULL_V(layer_properties, false);
  103. for (uint32_t i = 0; i < num_layer_properties; i++) {
  104. layer_properties[i].type = XR_TYPE_API_LAYER_PROPERTIES;
  105. layer_properties[i].next = nullptr;
  106. }
  107. result = xrEnumerateApiLayerProperties(num_layer_properties, &num_layer_properties, layer_properties);
  108. ERR_FAIL_COND_V_MSG(XR_FAILED(result), false, "OpenXR: Failed to enumerate api layer properties");
  109. #ifdef DEBUG
  110. for (uint32_t i = 0; i < num_layer_properties; i++) {
  111. print_line("OpenXR: Found OpenXR layer ", layer_properties[i].layerName);
  112. }
  113. #endif
  114. return true;
  115. }
  116. bool OpenXRAPI::load_supported_extensions() {
  117. // This queries supported extensions that are available and can be initialized when we create our OpenXR instance
  118. if (supported_extensions != nullptr) {
  119. // already retrieved this
  120. return true;
  121. }
  122. // Note, instance is not yet setup so we can't use get_error_string to retrieve our error
  123. XrResult result = xrEnumerateInstanceExtensionProperties(nullptr, 0, &num_supported_extensions, nullptr);
  124. ERR_FAIL_COND_V_MSG(XR_FAILED(result), false, "OpenXR: Failed to enumerate number of extension properties");
  125. supported_extensions = (XrExtensionProperties *)memalloc(sizeof(XrExtensionProperties) * num_supported_extensions);
  126. ERR_FAIL_NULL_V(supported_extensions, false);
  127. // set our types
  128. for (uint32_t i = 0; i < num_supported_extensions; i++) {
  129. supported_extensions[i].type = XR_TYPE_EXTENSION_PROPERTIES;
  130. supported_extensions[i].next = nullptr;
  131. }
  132. result = xrEnumerateInstanceExtensionProperties(nullptr, num_supported_extensions, &num_supported_extensions, supported_extensions);
  133. ERR_FAIL_COND_V_MSG(XR_FAILED(result), false, "OpenXR: Failed to enumerate extension properties");
  134. #ifdef DEBUG
  135. for (uint32_t i = 0; i < num_supported_extensions; i++) {
  136. print_line("OpenXR: Found OpenXR extension ", supported_extensions[i].extensionName);
  137. }
  138. #endif
  139. return true;
  140. }
  141. bool OpenXRAPI::is_extension_supported(const String &p_extension) const {
  142. for (uint32_t i = 0; i < num_supported_extensions; i++) {
  143. if (supported_extensions[i].extensionName == p_extension) {
  144. #ifdef DEBUG
  145. print_line("OpenXR: requested extension", p_extension, "is supported");
  146. #endif
  147. return true;
  148. }
  149. }
  150. #ifdef DEBUG
  151. print_line("OpenXR: requested extension", p_extension, "is not supported");
  152. #endif
  153. return false;
  154. }
  155. void OpenXRAPI::copy_string_to_char_buffer(const String p_string, char *p_buffer, int p_buffer_len) {
  156. CharString char_string = p_string.utf8();
  157. int len = char_string.length();
  158. if (len < p_buffer_len - 1) {
  159. // was having weird CI issues with strcpy so....
  160. memcpy(p_buffer, char_string.get_data(), len);
  161. p_buffer[len] = '\0';
  162. } else {
  163. memcpy(p_buffer, char_string.get_data(), p_buffer_len - 1);
  164. p_buffer[p_buffer_len - 1] = '\0';
  165. }
  166. }
  167. bool OpenXRAPI::create_instance() {
  168. // Create our OpenXR instance, this will query any registered extension wrappers for extensions we need to enable.
  169. // Append the extensions requested by the registered extension wrappers.
  170. HashMap<String, bool *> requested_extensions;
  171. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  172. const HashMap<String, bool *> &wrapper_request_extensions = wrapper->get_request_extensions();
  173. // requested_extensions.insert(wrapper_request_extensions.begin(), wrapper_request_extensions.end());
  174. for (auto &requested_extension : wrapper_request_extensions) {
  175. requested_extensions[requested_extension.key] = requested_extension.value;
  176. }
  177. }
  178. // Add optional extensions for controllers that may be supported.
  179. // Overkill to create extension classes for this.
  180. requested_extensions[XR_EXT_HP_MIXED_REALITY_CONTROLLER_EXTENSION_NAME] = &ext_hp_mixed_reality_available;
  181. requested_extensions[XR_EXT_SAMSUNG_ODYSSEY_CONTROLLER_EXTENSION_NAME] = &ext_samsung_odyssey_available;
  182. requested_extensions[XR_HTC_VIVE_COSMOS_CONTROLLER_INTERACTION_EXTENSION_NAME] = &ext_vive_cosmos_available;
  183. requested_extensions[XR_HTC_VIVE_FOCUS3_CONTROLLER_INTERACTION_EXTENSION_NAME] = &ext_vive_focus3_available;
  184. requested_extensions[XR_HUAWEI_CONTROLLER_INTERACTION_EXTENSION_NAME] = &ext_huawei_controller_available;
  185. // Check which extensions are supported
  186. enabled_extensions.clear();
  187. for (auto &requested_extension : requested_extensions) {
  188. if (!is_extension_supported(requested_extension.key)) {
  189. if (requested_extension.value == nullptr) {
  190. // nullptr means this is a manditory extension so we fail
  191. ERR_FAIL_V_MSG(false, "OpenXR: OpenXR Runtime does not support OpenGL extension!");
  192. } else {
  193. // set this extension as not supported
  194. *requested_extension.value = false;
  195. }
  196. } else if (requested_extension.value != nullptr) {
  197. // set this extension as supported
  198. *requested_extension.value = true;
  199. // and record that we want to enable it
  200. enabled_extensions.push_back(requested_extension.key.ascii());
  201. } else {
  202. // record that we want to enable this
  203. enabled_extensions.push_back(requested_extension.key.ascii());
  204. }
  205. }
  206. Vector<const char *> extension_ptrs;
  207. for (int i = 0; i < enabled_extensions.size(); i++) {
  208. extension_ptrs.push_back(enabled_extensions[i].get_data());
  209. }
  210. // Get our project name
  211. String project_name = GLOBAL_GET("application/config/name");
  212. // Create our OpenXR instance
  213. XrApplicationInfo application_info{
  214. "", // applicationName, we'll set this down below
  215. 1, // applicationVersion, we don't currently have this
  216. "Godot Game Engine", // engineName
  217. VERSION_MAJOR * 10000 + VERSION_MINOR * 100 + VERSION_PATCH, // engineVersion 4.0 -> 40000, 4.0.1 -> 40001, 4.1 -> 40100, etc.
  218. XR_CURRENT_API_VERSION // apiVersion
  219. };
  220. XrInstanceCreateInfo instance_create_info = {
  221. XR_TYPE_INSTANCE_CREATE_INFO, // type
  222. nullptr, // next
  223. 0, // createFlags
  224. application_info, // applicationInfo
  225. 0, // enabledApiLayerCount, need to find out if we need support for this?
  226. nullptr, // enabledApiLayerNames
  227. uint32_t(extension_ptrs.size()), // enabledExtensionCount
  228. extension_ptrs.ptr() // enabledExtensionNames
  229. };
  230. copy_string_to_char_buffer(project_name, instance_create_info.applicationInfo.applicationName, XR_MAX_APPLICATION_NAME_SIZE);
  231. XrResult result = xrCreateInstance(&instance_create_info, &instance);
  232. ERR_FAIL_COND_V_MSG(XR_FAILED(result), false, "Failed to create XR instance.");
  233. // from this point on we can use get_error_string to get more info about our errors...
  234. XrInstanceProperties instanceProps = {
  235. XR_TYPE_INSTANCE_PROPERTIES, // type;
  236. nullptr, // next
  237. 0, // runtimeVersion, from here will be set by our get call
  238. "" // runtimeName
  239. };
  240. result = xrGetInstanceProperties(instance, &instanceProps);
  241. if (XR_FAILED(result)) {
  242. // not fatal probably
  243. print_line("OpenXR: Failed to get XR instance properties [", get_error_string(result), "]");
  244. } else {
  245. print_line("OpenXR: Running on OpenXR runtime: ", instanceProps.runtimeName, " ", OpenXRUtil::make_xr_version_string(instanceProps.runtimeVersion));
  246. }
  247. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  248. wrapper->on_instance_created(instance);
  249. }
  250. return true;
  251. }
  252. bool OpenXRAPI::get_system_info() {
  253. // Retrieve basic OpenXR system info based on the form factor we desire
  254. // Retrieve the system for our form factor, fails if form factor is not available
  255. XrSystemGetInfo system_get_info = {
  256. XR_TYPE_SYSTEM_GET_INFO, // type;
  257. nullptr, // next
  258. form_factor // formFactor
  259. };
  260. XrResult result = xrGetSystem(instance, &system_get_info, &system_id);
  261. if (XR_FAILED(result)) {
  262. print_line("OpenXR: Failed to get system for our form factor [", get_error_string(result), "]");
  263. return false;
  264. }
  265. // obtain info about our system, writing this out completely to make CI on Linux happy..
  266. void *next_pointer = nullptr;
  267. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  268. void *np = wrapper->set_system_properties_and_get_next_pointer(next_pointer);
  269. if (np != nullptr) {
  270. next_pointer = np;
  271. }
  272. }
  273. XrSystemProperties system_properties = {
  274. XR_TYPE_SYSTEM_PROPERTIES, // type
  275. next_pointer, // next
  276. 0, // systemId, from here will be set by our get call
  277. 0, // vendorId
  278. "", // systemName
  279. {
  280. 0, // maxSwapchainImageHeight
  281. 0, // maxSwapchainImageWidth
  282. 0, // maxLayerCount
  283. }, // graphicsProperties
  284. {
  285. false, // orientationTracking
  286. false // positionTracking
  287. } // trackingProperties
  288. };
  289. result = xrGetSystemProperties(instance, system_id, &system_properties);
  290. if (XR_FAILED(result)) {
  291. print_line("OpenXR: Failed to get System properties [", get_error_string(result), "]");
  292. return false;
  293. }
  294. // remember this state, we'll use it later
  295. system_name = String(system_properties.systemName);
  296. vendor_id = system_properties.vendorId;
  297. graphics_properties = system_properties.graphicsProperties;
  298. tracking_properties = system_properties.trackingProperties;
  299. return true;
  300. }
  301. bool OpenXRAPI::load_supported_view_configuration_types() {
  302. // This queries the supported configuration types, likely there will only be one choosing between Mono (phone AR) and Stereo (HMDs)
  303. ERR_FAIL_COND_V(instance == XR_NULL_HANDLE, false);
  304. if (supported_view_configuration_types != nullptr) {
  305. // free previous results
  306. memfree(supported_view_configuration_types);
  307. supported_view_configuration_types = nullptr;
  308. }
  309. XrResult result = xrEnumerateViewConfigurations(instance, system_id, 0, &num_view_configuration_types, nullptr);
  310. if (XR_FAILED(result)) {
  311. print_line("OpenXR: Failed to get view configuration count [", get_error_string(result), "]");
  312. return false;
  313. }
  314. supported_view_configuration_types = (XrViewConfigurationType *)memalloc(sizeof(XrViewConfigurationType) * num_view_configuration_types);
  315. ERR_FAIL_NULL_V(supported_view_configuration_types, false);
  316. result = xrEnumerateViewConfigurations(instance, system_id, num_view_configuration_types, &num_view_configuration_types, supported_view_configuration_types);
  317. ERR_FAIL_COND_V_MSG(XR_FAILED(result), false, "OpenXR: Failed to enumerateview configurations");
  318. #ifdef DEBUG
  319. for (uint32_t i = 0; i < num_view_configuration_types; i++) {
  320. print_line("OpenXR: Found supported view configuration ", OpenXRUtil::get_view_configuration_name(supported_view_configuration_types[i]));
  321. }
  322. #endif
  323. return true;
  324. }
  325. bool OpenXRAPI::is_view_configuration_supported(XrViewConfigurationType p_configuration_type) const {
  326. ERR_FAIL_NULL_V(supported_view_configuration_types, false);
  327. for (uint32_t i = 0; i < num_view_configuration_types; i++) {
  328. if (supported_view_configuration_types[i] == p_configuration_type) {
  329. return true;
  330. }
  331. }
  332. return false;
  333. }
  334. bool OpenXRAPI::load_supported_view_configuration_views(XrViewConfigurationType p_configuration_type) {
  335. // This loads our view configuration for each view so for a stereo HMD, we'll get two entries (that are likely identical)
  336. // The returned data supplies us with the recommended render target size
  337. if (!is_view_configuration_supported(p_configuration_type)) {
  338. print_line("OpenXR: View configuration ", OpenXRUtil::get_view_configuration_name(view_configuration), " is not supported.");
  339. return false;
  340. }
  341. if (view_configuration_views != nullptr) {
  342. // free previous results
  343. memfree(view_configuration_views);
  344. view_configuration_views = nullptr;
  345. }
  346. XrResult result = xrEnumerateViewConfigurationViews(instance, system_id, p_configuration_type, 0, &view_count, nullptr);
  347. if (XR_FAILED(result)) {
  348. print_line("OpenXR: Failed to get view configuration count [", get_error_string(result), "]");
  349. return false;
  350. }
  351. view_configuration_views = (XrViewConfigurationView *)memalloc(sizeof(XrViewConfigurationView) * view_count);
  352. ERR_FAIL_NULL_V(view_configuration_views, false);
  353. for (uint32_t i = 0; i < view_count; i++) {
  354. view_configuration_views[i].type = XR_TYPE_VIEW_CONFIGURATION_VIEW;
  355. view_configuration_views[i].next = nullptr;
  356. }
  357. result = xrEnumerateViewConfigurationViews(instance, system_id, p_configuration_type, view_count, &view_count, view_configuration_views);
  358. ERR_FAIL_COND_V_MSG(XR_FAILED(result), false, "OpenXR: Failed to enumerate view configurations");
  359. #ifdef DEBUG
  360. for (uint32_t i = 0; i < view_count; i++) {
  361. print_line("OpenXR: Found supported view configuration view");
  362. print_line(" - width: ", view_configuration_views[i].maxImageRectWidth);
  363. print_line(" - height: ", view_configuration_views[i].maxImageRectHeight);
  364. print_line(" - sample count: ", view_configuration_views[i].maxSwapchainSampleCount);
  365. print_line(" - recommended render width: ", view_configuration_views[i].recommendedImageRectWidth);
  366. print_line(" - recommended render height: ", view_configuration_views[i].recommendedImageRectHeight);
  367. print_line(" - recommended render sample count: ", view_configuration_views[i].recommendedSwapchainSampleCount);
  368. }
  369. #endif
  370. return true;
  371. }
  372. void OpenXRAPI::destroy_instance() {
  373. if (view_configuration_views != nullptr) {
  374. memfree(view_configuration_views);
  375. view_configuration_views = nullptr;
  376. }
  377. if (supported_view_configuration_types != nullptr) {
  378. memfree(supported_view_configuration_types);
  379. supported_view_configuration_types = nullptr;
  380. }
  381. if (instance != XR_NULL_HANDLE) {
  382. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  383. wrapper->on_instance_destroyed();
  384. }
  385. xrDestroyInstance(instance);
  386. instance = XR_NULL_HANDLE;
  387. }
  388. enabled_extensions.clear();
  389. }
  390. bool OpenXRAPI::create_session() {
  391. ERR_FAIL_COND_V(instance == XR_NULL_HANDLE, false);
  392. ERR_FAIL_COND_V(session != XR_NULL_HANDLE, false);
  393. void *next_pointer = nullptr;
  394. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  395. void *np = wrapper->set_session_create_and_get_next_pointer(next_pointer);
  396. if (np != nullptr) {
  397. next_pointer = np;
  398. }
  399. }
  400. XrSessionCreateInfo session_create_info = {
  401. XR_TYPE_SESSION_CREATE_INFO, // type
  402. next_pointer, // next
  403. 0, // createFlags
  404. system_id // systemId
  405. };
  406. XrResult result = xrCreateSession(instance, &session_create_info, &session);
  407. if (XR_FAILED(result)) {
  408. print_line("OpenXR: Failed to create session [", get_error_string(result), "]");
  409. return false;
  410. }
  411. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  412. wrapper->on_session_created(session);
  413. }
  414. return true;
  415. }
  416. bool OpenXRAPI::load_supported_reference_spaces() {
  417. // loads the supported reference spaces for our OpenXR session
  418. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, false);
  419. if (supported_reference_spaces != nullptr) {
  420. // free previous results
  421. memfree(supported_reference_spaces);
  422. supported_reference_spaces = nullptr;
  423. }
  424. XrResult result = xrEnumerateReferenceSpaces(session, 0, &num_reference_spaces, nullptr);
  425. if (XR_FAILED(result)) {
  426. print_line("OpenXR: Failed to get reference space count [", get_error_string(result), "]");
  427. return false;
  428. }
  429. supported_reference_spaces = (XrReferenceSpaceType *)memalloc(sizeof(XrReferenceSpaceType) * num_reference_spaces);
  430. ERR_FAIL_NULL_V(supported_reference_spaces, false);
  431. result = xrEnumerateReferenceSpaces(session, num_reference_spaces, &num_reference_spaces, supported_reference_spaces);
  432. ERR_FAIL_COND_V_MSG(XR_FAILED(result), false, "OpenXR: Failed to enumerate reference spaces");
  433. // #ifdef DEBUG
  434. for (uint32_t i = 0; i < num_reference_spaces; i++) {
  435. print_line("OpenXR: Found supported reference space ", OpenXRUtil::get_reference_space_name(supported_reference_spaces[i]));
  436. }
  437. // #endif
  438. return true;
  439. }
  440. bool OpenXRAPI::is_reference_space_supported(XrReferenceSpaceType p_reference_space) {
  441. ERR_FAIL_NULL_V(supported_reference_spaces, false);
  442. for (uint32_t i = 0; i < num_reference_spaces; i++) {
  443. if (supported_reference_spaces[i] == p_reference_space) {
  444. return true;
  445. }
  446. }
  447. return false;
  448. }
  449. bool OpenXRAPI::setup_spaces() {
  450. XrResult result;
  451. XrPosef identityPose = {
  452. { 0.0, 0.0, 0.0, 1.0 },
  453. { 0.0, 0.0, 0.0 }
  454. };
  455. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, false);
  456. // create play space
  457. {
  458. if (!is_reference_space_supported(reference_space)) {
  459. print_line("OpenXR: reference space ", OpenXRUtil::get_reference_space_name(reference_space), " is not supported.");
  460. return false;
  461. }
  462. XrReferenceSpaceCreateInfo play_space_create_info = {
  463. XR_TYPE_REFERENCE_SPACE_CREATE_INFO, // type
  464. nullptr, // next
  465. reference_space, // referenceSpaceType
  466. identityPose // poseInReferenceSpace
  467. };
  468. result = xrCreateReferenceSpace(session, &play_space_create_info, &play_space);
  469. if (XR_FAILED(result)) {
  470. print_line("OpenXR: Failed to create play space [", get_error_string(result), "]");
  471. return false;
  472. }
  473. }
  474. // create view space
  475. {
  476. if (!is_reference_space_supported(XR_REFERENCE_SPACE_TYPE_VIEW)) {
  477. print_line("OpenXR: reference space XR_REFERENCE_SPACE_TYPE_VIEW is not supported.");
  478. return false;
  479. }
  480. XrReferenceSpaceCreateInfo view_space_create_info = {
  481. XR_TYPE_REFERENCE_SPACE_CREATE_INFO, // type
  482. nullptr, // next
  483. XR_REFERENCE_SPACE_TYPE_VIEW, // referenceSpaceType
  484. identityPose // poseInReferenceSpace
  485. };
  486. result = xrCreateReferenceSpace(session, &view_space_create_info, &view_space);
  487. if (XR_FAILED(result)) {
  488. print_line("OpenXR: Failed to create view space [", get_error_string(result), "]");
  489. return false;
  490. }
  491. }
  492. return true;
  493. }
  494. bool OpenXRAPI::load_supported_swapchain_formats() {
  495. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, false);
  496. if (supported_swapchain_formats != nullptr) {
  497. // free previous results
  498. memfree(supported_swapchain_formats);
  499. supported_swapchain_formats = nullptr;
  500. }
  501. XrResult result = xrEnumerateSwapchainFormats(session, 0, &num_swapchain_formats, nullptr);
  502. if (XR_FAILED(result)) {
  503. print_line("OpenXR: Failed to get swapchain format count [", get_error_string(result), "]");
  504. return false;
  505. }
  506. supported_swapchain_formats = (int64_t *)memalloc(sizeof(int64_t) * num_swapchain_formats);
  507. ERR_FAIL_NULL_V(supported_swapchain_formats, false);
  508. result = xrEnumerateSwapchainFormats(session, num_swapchain_formats, &num_swapchain_formats, supported_swapchain_formats);
  509. ERR_FAIL_COND_V_MSG(XR_FAILED(result), false, "OpenXR: Failed to enumerate swapchain formats");
  510. // #ifdef DEBUG
  511. for (uint32_t i = 0; i < num_swapchain_formats; i++) {
  512. print_line("OpenXR: Found supported swapchain format ", get_swapchain_format_name(supported_swapchain_formats[i]));
  513. }
  514. // #endif
  515. return true;
  516. }
  517. bool OpenXRAPI::is_swapchain_format_supported(int64_t p_swapchain_format) {
  518. ERR_FAIL_NULL_V(supported_swapchain_formats, false);
  519. for (uint32_t i = 0; i < num_swapchain_formats; i++) {
  520. if (supported_swapchain_formats[i] == p_swapchain_format) {
  521. return true;
  522. }
  523. }
  524. return false;
  525. }
  526. bool OpenXRAPI::create_main_swapchain() {
  527. ERR_FAIL_NULL_V(graphics_extension, false);
  528. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, false);
  529. /*
  530. TODO: We need to improve on this, for now we're taking our old approach of creating our main swapchains and substituting
  531. those for the ones Godot normally creates.
  532. This however means we can only use swapchains for our main XR view.
  533. It would have been nicer if we could override the swapchain creation in Godot with ours but we have a timing issue here.
  534. We can't create XR swapchains until after our XR session is fully instantiated, yet Godot creates its swapchain much earlier.
  535. Also Godot only creates a swapchain for the main output.
  536. OpenXR will require us to create swapchains as the render target for additional viewports if we want to use the layer system
  537. to optimise text rendering and background rendering as OpenXR may choose to re-use the results for reprojection while we're
  538. already rendering the next frame.
  539. Finally an area we need to expand upon is that Foveated rendering is only enabled for the swap chain we create,
  540. as we render 3D content into internal buffers that are copied into the swapchain, we don't get any of the performance gains
  541. until such time as we implement VRS.
  542. */
  543. // Build a vector with swapchain formats we want to use, from best fit to worst
  544. Vector<int64_t> usable_swapchain_formats;
  545. int64_t swapchain_format_to_use = 0;
  546. graphics_extension->get_usable_swapchain_formats(usable_swapchain_formats);
  547. // now find out which one is supported
  548. for (int i = 0; i < usable_swapchain_formats.size() && swapchain_format_to_use == 0; i++) {
  549. if (is_swapchain_format_supported(usable_swapchain_formats[i])) {
  550. swapchain_format_to_use = usable_swapchain_formats[i];
  551. }
  552. }
  553. if (swapchain_format_to_use == 0) {
  554. swapchain_format_to_use = usable_swapchain_formats[0]; // just use the first one and hope for the best...
  555. print_line("Couldn't find usable swap chain format, using", get_swapchain_format_name(swapchain_format_to_use), "instead.");
  556. } else {
  557. print_line("Using swap chain format:", get_swapchain_format_name(swapchain_format_to_use));
  558. }
  559. Size2 recommended_size = get_recommended_target_size();
  560. if (!create_swapchain(swapchain_format_to_use, recommended_size.width, recommended_size.height, view_configuration_views[0].recommendedSwapchainSampleCount, view_count, swapchain, &swapchain_graphics_data)) {
  561. return false;
  562. }
  563. views = (XrView *)memalloc(sizeof(XrView) * view_count);
  564. ERR_FAIL_NULL_V_MSG(views, false, "OpenXR Couldn't allocate memory for views");
  565. projection_views = (XrCompositionLayerProjectionView *)memalloc(sizeof(XrCompositionLayerProjectionView) * view_count);
  566. ERR_FAIL_NULL_V_MSG(projection_views, false, "OpenXR Couldn't allocate memory for projection views");
  567. for (uint32_t i = 0; i < view_count; i++) {
  568. views[i].type = XR_TYPE_VIEW;
  569. views[i].next = nullptr;
  570. projection_views[i].type = XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW;
  571. projection_views[i].next = nullptr;
  572. projection_views[i].subImage.swapchain = swapchain;
  573. projection_views[i].subImage.imageArrayIndex = i;
  574. projection_views[i].subImage.imageRect.offset.x = 0;
  575. projection_views[i].subImage.imageRect.offset.y = 0;
  576. projection_views[i].subImage.imageRect.extent.width = recommended_size.width;
  577. projection_views[i].subImage.imageRect.extent.height = recommended_size.height;
  578. };
  579. return true;
  580. };
  581. void OpenXRAPI::destroy_session() {
  582. if (running && session != XR_NULL_HANDLE) {
  583. xrEndSession(session);
  584. }
  585. if (graphics_extension) {
  586. graphics_extension->cleanup_swapchain_graphics_data(&swapchain_graphics_data);
  587. }
  588. if (views != nullptr) {
  589. memfree(views);
  590. views = nullptr;
  591. }
  592. if (projection_views != nullptr) {
  593. memfree(projection_views);
  594. projection_views = nullptr;
  595. }
  596. if (swapchain != XR_NULL_HANDLE) {
  597. xrDestroySwapchain(swapchain);
  598. swapchain = XR_NULL_HANDLE;
  599. }
  600. if (supported_swapchain_formats != nullptr) {
  601. memfree(supported_swapchain_formats);
  602. supported_swapchain_formats = nullptr;
  603. }
  604. // destroy our spaces
  605. if (play_space != XR_NULL_HANDLE) {
  606. xrDestroySpace(play_space);
  607. play_space = XR_NULL_HANDLE;
  608. }
  609. if (view_space != XR_NULL_HANDLE) {
  610. xrDestroySpace(view_space);
  611. view_space = XR_NULL_HANDLE;
  612. }
  613. if (supported_reference_spaces != nullptr) {
  614. // free previous results
  615. memfree(supported_reference_spaces);
  616. supported_reference_spaces = nullptr;
  617. }
  618. if (session != XR_NULL_HANDLE) {
  619. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  620. wrapper->on_session_destroyed();
  621. }
  622. xrDestroySession(session);
  623. session = XR_NULL_HANDLE;
  624. }
  625. }
  626. bool OpenXRAPI::create_swapchain(int64_t p_swapchain_format, uint32_t p_width, uint32_t p_height, uint32_t p_sample_count, uint32_t p_array_size, XrSwapchain &r_swapchain, void **r_swapchain_graphics_data) {
  627. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, false);
  628. ERR_FAIL_NULL_V(graphics_extension, false);
  629. XrResult result;
  630. void *next_pointer = nullptr;
  631. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  632. void *np = wrapper->set_swapchain_create_info_and_get_next_pointer(next_pointer);
  633. if (np != nullptr) {
  634. next_pointer = np;
  635. }
  636. }
  637. XrSwapchainCreateInfo swapchain_create_info = {
  638. XR_TYPE_SWAPCHAIN_CREATE_INFO, // type
  639. next_pointer, // next
  640. 0, // createFlags
  641. XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT, // usageFlags
  642. p_swapchain_format, // format
  643. p_sample_count, // sampleCount
  644. p_width, // width
  645. p_height, // height
  646. 1, // faceCount
  647. p_array_size, // arraySize
  648. 1 // mipCount
  649. };
  650. XrSwapchain new_swapchain;
  651. result = xrCreateSwapchain(session, &swapchain_create_info, &new_swapchain);
  652. if (XR_FAILED(result)) {
  653. print_line("OpenXR: Failed to get swapchain [", get_error_string(result), "]");
  654. return false;
  655. }
  656. if (!graphics_extension->get_swapchain_image_data(new_swapchain, p_swapchain_format, p_width, p_height, p_sample_count, p_array_size, r_swapchain_graphics_data)) {
  657. xrDestroySwapchain(new_swapchain);
  658. return false;
  659. }
  660. r_swapchain = new_swapchain;
  661. return true;
  662. }
  663. bool OpenXRAPI::on_state_idle() {
  664. #ifdef DEBUG
  665. print_line("On state idle");
  666. #endif
  667. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  668. wrapper->on_state_idle();
  669. }
  670. return true;
  671. }
  672. bool OpenXRAPI::on_state_ready() {
  673. #ifdef DEBUG
  674. print_line("On state ready");
  675. #endif
  676. // begin session
  677. XrSessionBeginInfo session_begin_info = {
  678. XR_TYPE_SESSION_BEGIN_INFO, // type
  679. nullptr, // next
  680. view_configuration // primaryViewConfigurationType
  681. };
  682. XrResult result = xrBeginSession(session, &session_begin_info);
  683. if (XR_FAILED(result)) {
  684. print_line("OpenXR: Failed to begin session [", get_error_string(result), "]");
  685. return false;
  686. }
  687. // This is when we create our swapchain, this can be a "long" time after Godot finishes, we can deal with this for now
  688. // but once we want to provide Viewports for additional layers where OpenXR requires us to create further swapchains,
  689. // we'll be creating those viewport WAY before we reach this point.
  690. // We may need to implement a wait in our init in main.cpp polling our events until the session is ready.
  691. // That will be very very ugly
  692. // The other possibility is to create a separate OpenXRViewport type specifically for this goal as part of our OpenXR module
  693. if (!create_main_swapchain()) {
  694. return false;
  695. }
  696. // we're running
  697. running = true;
  698. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  699. wrapper->on_state_ready();
  700. }
  701. if (xr_interface) {
  702. xr_interface->on_state_ready();
  703. }
  704. // TODO Tell android
  705. return true;
  706. }
  707. bool OpenXRAPI::on_state_synchronized() {
  708. #ifdef DEBUG
  709. print_line("On state synchronized");
  710. #endif
  711. // Just in case, see if we already have active trackers...
  712. List<RID> trackers;
  713. tracker_owner.get_owned_list(&trackers);
  714. for (int i = 0; i < trackers.size(); i++) {
  715. tracker_check_profile(trackers[i]);
  716. }
  717. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  718. wrapper->on_state_synchronized();
  719. }
  720. return true;
  721. }
  722. bool OpenXRAPI::on_state_visible() {
  723. #ifdef DEBUG
  724. print_line("On state visible");
  725. #endif
  726. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  727. wrapper->on_state_visible();
  728. }
  729. if (xr_interface) {
  730. xr_interface->on_state_visible();
  731. }
  732. return true;
  733. }
  734. bool OpenXRAPI::on_state_focused() {
  735. #ifdef DEBUG
  736. print_line("On state focused");
  737. #endif
  738. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  739. wrapper->on_state_focused();
  740. }
  741. if (xr_interface) {
  742. xr_interface->on_state_focused();
  743. }
  744. return true;
  745. }
  746. bool OpenXRAPI::on_state_stopping() {
  747. #ifdef DEBUG
  748. print_line("On state stopping");
  749. #endif
  750. if (xr_interface) {
  751. xr_interface->on_state_stopping();
  752. }
  753. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  754. wrapper->on_state_stopping();
  755. }
  756. if (running) {
  757. XrResult result = xrEndSession(session);
  758. if (XR_FAILED(result)) {
  759. // we only report this..
  760. print_line("OpenXR: Failed to end session [", get_error_string(result), "]");
  761. }
  762. running = false;
  763. }
  764. // TODO further cleanup
  765. return true;
  766. }
  767. bool OpenXRAPI::on_state_loss_pending() {
  768. #ifdef DEBUG
  769. print_line("On state loss pending");
  770. #endif
  771. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  772. wrapper->on_state_loss_pending();
  773. }
  774. // TODO need to look into the correct action here, read up on the spec but we may need to signal Godot to exit (if it's not already exiting)
  775. return true;
  776. }
  777. bool OpenXRAPI::on_state_exiting() {
  778. #ifdef DEBUG
  779. print_line("On state existing");
  780. #endif
  781. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  782. wrapper->on_state_exiting();
  783. }
  784. // TODO need to look into the correct action here, read up on the spec but we may need to signal Godot to exit (if it's not already exiting)
  785. return true;
  786. }
  787. bool OpenXRAPI::is_initialized() {
  788. return (instance != XR_NULL_HANDLE);
  789. }
  790. bool OpenXRAPI::is_running() {
  791. if (instance == XR_NULL_HANDLE) {
  792. return false;
  793. }
  794. if (session == XR_NULL_HANDLE) {
  795. return false;
  796. }
  797. return running;
  798. }
  799. bool OpenXRAPI::initialize(const String &p_rendering_driver) {
  800. ERR_FAIL_COND_V_MSG(instance != XR_NULL_HANDLE, false, "OpenXR instance was already created");
  801. if (p_rendering_driver == "vulkan") {
  802. #ifdef VULKAN_ENABLED
  803. graphics_extension = memnew(OpenXRVulkanExtension(this));
  804. register_extension_wrapper(graphics_extension);
  805. #else
  806. // shouldn't be possible...
  807. ERR_FAIL_V(false);
  808. #endif
  809. } else if (p_rendering_driver == "opengl3") {
  810. #ifdef GLES3_ENABLED
  811. // graphics_extension = memnew(OpenXROpenGLExtension(this));
  812. // register_extension_wrapper(graphics_extension);
  813. ERR_FAIL_V_MSG(false, "OpenXR: OpenGL is not supported at this time.");
  814. #else
  815. // shouldn't be possible...
  816. ERR_FAIL_V(false);
  817. #endif
  818. } else {
  819. ERR_FAIL_V_MSG(false, "OpenXR: Unsupported rendering device.");
  820. }
  821. // initialize
  822. if (!load_layer_properties()) {
  823. destroy_instance();
  824. return false;
  825. }
  826. if (!load_supported_extensions()) {
  827. destroy_instance();
  828. return false;
  829. }
  830. if (!create_instance()) {
  831. destroy_instance();
  832. return false;
  833. }
  834. if (!get_system_info()) {
  835. destroy_instance();
  836. return false;
  837. }
  838. if (!load_supported_view_configuration_types()) {
  839. destroy_instance();
  840. return false;
  841. }
  842. if (!load_supported_view_configuration_views(view_configuration)) {
  843. destroy_instance();
  844. return false;
  845. }
  846. return true;
  847. }
  848. bool OpenXRAPI::initialize_session() {
  849. if (!create_session()) {
  850. destroy_session();
  851. return false;
  852. }
  853. if (!load_supported_reference_spaces()) {
  854. destroy_session();
  855. return false;
  856. }
  857. if (!setup_spaces()) {
  858. destroy_session();
  859. return false;
  860. }
  861. if (!load_supported_swapchain_formats()) {
  862. destroy_session();
  863. return false;
  864. }
  865. return true;
  866. }
  867. void OpenXRAPI::finish() {
  868. destroy_session();
  869. destroy_instance();
  870. }
  871. void OpenXRAPI::set_xr_interface(OpenXRInterface *p_xr_interface) {
  872. xr_interface = p_xr_interface;
  873. }
  874. void OpenXRAPI::register_extension_wrapper(OpenXRExtensionWrapper *p_extension_wrapper) {
  875. registered_extension_wrappers.push_back(p_extension_wrapper);
  876. }
  877. Size2 OpenXRAPI::get_recommended_target_size() {
  878. ERR_FAIL_NULL_V(view_configuration_views, Size2());
  879. Size2 target_size;
  880. target_size.width = view_configuration_views[0].recommendedImageRectWidth;
  881. target_size.height = view_configuration_views[0].recommendedImageRectHeight;
  882. return target_size;
  883. }
  884. XRPose::TrackingConfidence OpenXRAPI::get_head_center(Transform3D &r_transform, Vector3 &r_linear_velocity, Vector3 &r_angular_velocity) {
  885. XrResult result;
  886. ERR_FAIL_COND_V(!running, XRPose::XR_TRACKING_CONFIDENCE_NONE);
  887. // xrWaitFrame not run yet
  888. if (frame_state.predictedDisplayTime == 0) {
  889. return XRPose::XR_TRACKING_CONFIDENCE_NONE;
  890. }
  891. // Get timing for the next frame, as that is the current frame we're processing
  892. XrTime display_time = get_next_frame_time();
  893. XrSpaceVelocity velocity = {
  894. XR_TYPE_SPACE_VELOCITY, // type
  895. nullptr, // next
  896. 0, // velocityFlags
  897. { 0.0, 0.0, 0.0 }, // linearVelocity
  898. { 0.0, 0.0, 0.0 } // angularVelocity
  899. };
  900. XrSpaceLocation location = {
  901. XR_TYPE_SPACE_LOCATION, // type
  902. &velocity, // next
  903. 0, // locationFlags
  904. {
  905. { 0.0, 0.0, 0.0, 0.0 }, // orientation
  906. { 0.0, 0.0, 0.0 } // position
  907. } // pose
  908. };
  909. result = xrLocateSpace(view_space, play_space, display_time, &location);
  910. if (XR_FAILED(result)) {
  911. print_line("OpenXR: Failed to locate view space in play space [", get_error_string(result), "]");
  912. return XRPose::XR_TRACKING_CONFIDENCE_NONE;
  913. }
  914. XRPose::TrackingConfidence confidence = transform_from_location(location, r_transform);
  915. parse_velocities(velocity, r_linear_velocity, r_angular_velocity);
  916. if (head_pose_confidence != confidence) {
  917. // prevent error spam
  918. head_pose_confidence = confidence;
  919. if (head_pose_confidence == XRPose::XR_TRACKING_CONFIDENCE_NONE) {
  920. print_line("OpenXR head space location not valid (check tracking?)");
  921. #ifdef DEBUG
  922. } else if (head_pose_confidence == XRPose::XR_TRACKING_CONFIDENCE_LOW) {
  923. print_line("OpenVR Head pose now tracking with low confidence");
  924. } else {
  925. print_line("OpenVR Head pose now tracking with high confidence");
  926. #endif
  927. }
  928. }
  929. return confidence;
  930. }
  931. bool OpenXRAPI::get_view_transform(uint32_t p_view, Transform3D &r_transform) {
  932. ERR_FAIL_COND_V(!running, false);
  933. // xrWaitFrame not run yet
  934. if (frame_state.predictedDisplayTime == 0) {
  935. return false;
  936. }
  937. // we don't have valid view info
  938. if (views == nullptr || !view_pose_valid) {
  939. return false;
  940. }
  941. // Note, the timing of this is set right before rendering, which is what we need here.
  942. r_transform = transform_from_pose(views[p_view].pose);
  943. return true;
  944. }
  945. bool OpenXRAPI::get_view_projection(uint32_t p_view, double p_z_near, double p_z_far, CameraMatrix &p_camera_matrix) {
  946. ERR_FAIL_COND_V(!running, false);
  947. ERR_FAIL_NULL_V(graphics_extension, false);
  948. // xrWaitFrame not run yet
  949. if (frame_state.predictedDisplayTime == 0) {
  950. return false;
  951. }
  952. // we don't have valid view info
  953. if (views == nullptr || !view_pose_valid) {
  954. return false;
  955. }
  956. return graphics_extension->create_projection_fov(views[p_view].fov, p_z_near, p_z_far, p_camera_matrix);
  957. }
  958. bool OpenXRAPI::poll_events() {
  959. ERR_FAIL_COND_V(instance == XR_NULL_HANDLE, false);
  960. XrEventDataBuffer runtimeEvent;
  961. runtimeEvent.type = XR_TYPE_EVENT_DATA_BUFFER;
  962. runtimeEvent.next = nullptr;
  963. // runtimeEvent.varying = ...
  964. XrResult pollResult = xrPollEvent(instance, &runtimeEvent);
  965. while (pollResult == XR_SUCCESS) {
  966. bool handled = false;
  967. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  968. handled |= wrapper->on_event_polled(runtimeEvent);
  969. }
  970. switch (runtimeEvent.type) {
  971. case XR_TYPE_EVENT_DATA_EVENTS_LOST: {
  972. XrEventDataEventsLost *event = (XrEventDataEventsLost *)&runtimeEvent;
  973. // We probably didn't poll fast enough, just output warning
  974. WARN_PRINT("OpenXR EVENT: " + itos(event->lostEventCount) + " event data lost!");
  975. } break;
  976. case XR_TYPE_EVENT_DATA_VISIBILITY_MASK_CHANGED_KHR: {
  977. // XrEventDataVisibilityMaskChangedKHR *event = (XrEventDataVisibilityMaskChangedKHR *)&runtimeEvent;
  978. // TODO implement this in the future, we should call xrGetVisibilityMaskKHR to obtain a mask,
  979. // this will allow us to prevent rendering the part of our view which is never displayed giving us
  980. // a decent performance improvement.
  981. print_verbose("OpenXR EVENT: STUB: visibility mask changed");
  982. } break;
  983. case XR_TYPE_EVENT_DATA_INSTANCE_LOSS_PENDING: {
  984. XrEventDataInstanceLossPending *event = (XrEventDataInstanceLossPending *)&runtimeEvent;
  985. // TODO We get this event if we're about to loose our OpenXR instance.
  986. // We should queue exiting Godot at this point.
  987. print_verbose("OpenXR EVENT: instance loss pending at " + itos(event->lossTime));
  988. return false;
  989. } break;
  990. case XR_TYPE_EVENT_DATA_SESSION_STATE_CHANGED: {
  991. XrEventDataSessionStateChanged *event = (XrEventDataSessionStateChanged *)&runtimeEvent;
  992. session_state = event->state;
  993. if (session_state >= XR_SESSION_STATE_MAX_ENUM) {
  994. print_verbose("OpenXR EVENT: session state changed to UNKNOWN - " + itos(session_state));
  995. } else {
  996. print_verbose("OpenXR EVENT: session state changed to " + OpenXRUtil::get_session_state_name(session_state));
  997. switch (session_state) {
  998. case XR_SESSION_STATE_IDLE:
  999. on_state_idle();
  1000. break;
  1001. case XR_SESSION_STATE_READY:
  1002. on_state_ready();
  1003. break;
  1004. case XR_SESSION_STATE_SYNCHRONIZED:
  1005. on_state_synchronized();
  1006. break;
  1007. case XR_SESSION_STATE_VISIBLE:
  1008. on_state_visible();
  1009. break;
  1010. case XR_SESSION_STATE_FOCUSED:
  1011. on_state_focused();
  1012. break;
  1013. case XR_SESSION_STATE_STOPPING:
  1014. on_state_stopping();
  1015. break;
  1016. case XR_SESSION_STATE_LOSS_PENDING:
  1017. on_state_loss_pending();
  1018. break;
  1019. case XR_SESSION_STATE_EXITING:
  1020. on_state_exiting();
  1021. break;
  1022. default:
  1023. break;
  1024. }
  1025. }
  1026. } break;
  1027. case XR_TYPE_EVENT_DATA_REFERENCE_SPACE_CHANGE_PENDING: {
  1028. XrEventDataReferenceSpaceChangePending *event = (XrEventDataReferenceSpaceChangePending *)&runtimeEvent;
  1029. print_verbose("OpenXR EVENT: reference space type " + OpenXRUtil::get_reference_space_name(event->referenceSpaceType) + " change pending!");
  1030. if (event->poseValid && xr_interface) {
  1031. xr_interface->on_pose_recentered();
  1032. }
  1033. } break;
  1034. case XR_TYPE_EVENT_DATA_INTERACTION_PROFILE_CHANGED: {
  1035. print_verbose("OpenXR EVENT: interaction profile changed!");
  1036. XrEventDataInteractionProfileChanged *event = (XrEventDataInteractionProfileChanged *)&runtimeEvent;
  1037. List<RID> trackers;
  1038. tracker_owner.get_owned_list(&trackers);
  1039. for (int i = 0; i < trackers.size(); i++) {
  1040. tracker_check_profile(trackers[i], event->session);
  1041. }
  1042. } break;
  1043. default:
  1044. if (!handled) {
  1045. print_verbose("OpenXR Unhandled event type " + OpenXRUtil::get_structure_type_name(runtimeEvent.type));
  1046. }
  1047. break;
  1048. }
  1049. runtimeEvent.type = XR_TYPE_EVENT_DATA_BUFFER;
  1050. pollResult = xrPollEvent(instance, &runtimeEvent);
  1051. }
  1052. if (pollResult == XR_EVENT_UNAVAILABLE) {
  1053. // processed all events in the queue
  1054. return true;
  1055. } else {
  1056. ERR_FAIL_V_MSG(false, "OpenXR: Failed to poll events!");
  1057. }
  1058. }
  1059. bool OpenXRAPI::process() {
  1060. ERR_FAIL_COND_V(instance == XR_NULL_HANDLE, false);
  1061. if (!poll_events()) {
  1062. return false;
  1063. }
  1064. if (!running) {
  1065. return false;
  1066. }
  1067. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  1068. wrapper->on_process();
  1069. }
  1070. return true;
  1071. }
  1072. bool OpenXRAPI::acquire_image(XrSwapchain p_swapchain, uint32_t &r_image_index) {
  1073. ERR_FAIL_COND_V(image_acquired, true); // this was not released when it should be, error out and re-use...
  1074. XrResult result;
  1075. XrSwapchainImageAcquireInfo swapchain_image_acquire_info = {
  1076. XR_TYPE_SWAPCHAIN_IMAGE_ACQUIRE_INFO, // type
  1077. nullptr // next
  1078. };
  1079. result = xrAcquireSwapchainImage(p_swapchain, &swapchain_image_acquire_info, &r_image_index);
  1080. if (XR_FAILED(result)) {
  1081. print_line("OpenXR: failed to acquire swapchain image [", get_error_string(result), "]");
  1082. return false;
  1083. }
  1084. XrSwapchainImageWaitInfo swapchain_image_wait_info = {
  1085. XR_TYPE_SWAPCHAIN_IMAGE_WAIT_INFO, // type
  1086. nullptr, // next
  1087. 17000000 // timeout in nanoseconds
  1088. };
  1089. result = xrWaitSwapchainImage(p_swapchain, &swapchain_image_wait_info);
  1090. if (XR_FAILED(result)) {
  1091. print_line("OpenXR: failed to wait for swapchain image [", get_error_string(result), "]");
  1092. return false;
  1093. }
  1094. return true;
  1095. }
  1096. bool OpenXRAPI::release_image(XrSwapchain p_swapchain) {
  1097. XrSwapchainImageReleaseInfo swapchain_image_release_info = {
  1098. XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO, // type
  1099. nullptr // next
  1100. };
  1101. XrResult result = xrReleaseSwapchainImage(swapchain, &swapchain_image_release_info);
  1102. if (XR_FAILED(result)) {
  1103. print_line("OpenXR: failed to release swapchain image! [", get_error_string(result), "]");
  1104. return false;
  1105. }
  1106. return true;
  1107. }
  1108. void OpenXRAPI::pre_render() {
  1109. ERR_FAIL_COND(instance == XR_NULL_HANDLE);
  1110. if (!running) {
  1111. return;
  1112. }
  1113. // Waitframe does 2 important things in our process:
  1114. // 1) It provides us with predictive timing, telling us when OpenXR expects to display the frame we're about to commit
  1115. // 2) It will use the previous timing to pause our thread so that rendering starts as close to displaying as possible
  1116. // This must thus be called as close to when we start rendering as possible
  1117. XrFrameWaitInfo frame_wait_info = { XR_TYPE_FRAME_WAIT_INFO, nullptr };
  1118. XrResult result = xrWaitFrame(session, &frame_wait_info, &frame_state);
  1119. if (XR_FAILED(result)) {
  1120. print_line("OpenXR: xrWaitFrame() was not successful [", get_error_string(result), "]");
  1121. // reset just in case
  1122. frame_state.predictedDisplayTime = 0;
  1123. frame_state.predictedDisplayPeriod = 0;
  1124. frame_state.shouldRender = false;
  1125. return;
  1126. }
  1127. if (frame_state.predictedDisplayPeriod > 500000000) {
  1128. // display period more then 0.5 seconds? must be wrong data
  1129. print_verbose("OpenXR resetting invalid display period " + rtos(frame_state.predictedDisplayPeriod));
  1130. frame_state.predictedDisplayPeriod = 0;
  1131. }
  1132. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  1133. wrapper->on_pre_render();
  1134. }
  1135. // Get our view info for the frame we're about to render, note from the OpenXR manual:
  1136. // "Repeatedly calling xrLocateViews with the same time may not necessarily return the same result. Instead the prediction gets increasingly accurate as the function is called closer to the given time for which a prediction is made"
  1137. // We're calling this "relatively" early, the positioning we're obtaining here will be used to do our frustum culling,
  1138. // occlusion culling, etc. There is however a technique that we can investigate in the future where after our entire
  1139. // Vulkan command buffer is build, but right before vkSubmitQueue is called, we call xrLocateViews one more time and
  1140. // update the view and projection matrix once more with a slightly more accurate predication and then submit the
  1141. // command queues.
  1142. // That is not possible yet but worth investigating in the future.
  1143. XrViewLocateInfo view_locate_info = {
  1144. XR_TYPE_VIEW_LOCATE_INFO, // type
  1145. nullptr, // next
  1146. view_configuration, // viewConfigurationType
  1147. frame_state.predictedDisplayTime, // displayTime
  1148. play_space // space
  1149. };
  1150. XrViewState view_state = {
  1151. XR_TYPE_VIEW_STATE, // type
  1152. nullptr, // next
  1153. 0 // viewStateFlags
  1154. };
  1155. uint32_t view_count_output;
  1156. result = xrLocateViews(session, &view_locate_info, &view_state, view_count, &view_count_output, views);
  1157. if (XR_FAILED(result)) {
  1158. print_line("OpenXR: Couldn't locate views [", get_error_string(result), "]");
  1159. return;
  1160. }
  1161. bool pose_valid = true;
  1162. for (uint64_t i = 0; i < view_count_output; i++) {
  1163. if ((view_state.viewStateFlags & XR_VIEW_STATE_ORIENTATION_VALID_BIT) == 0 ||
  1164. (view_state.viewStateFlags & XR_VIEW_STATE_POSITION_VALID_BIT) == 0) {
  1165. pose_valid = false;
  1166. }
  1167. }
  1168. if (view_pose_valid != pose_valid) {
  1169. view_pose_valid = pose_valid;
  1170. #ifdef DEBUG
  1171. if (!view_pose_valid) {
  1172. print_line("OpenXR View pose became invalid");
  1173. } else {
  1174. print_line("OpenXR View pose became valid");
  1175. }
  1176. #endif
  1177. }
  1178. // let's start our frame..
  1179. XrFrameBeginInfo frame_begin_info = {
  1180. XR_TYPE_FRAME_BEGIN_INFO, // type
  1181. nullptr // next
  1182. };
  1183. result = xrBeginFrame(session, &frame_begin_info);
  1184. if (XR_FAILED(result)) {
  1185. print_line("OpenXR: failed to being frame [", get_error_string(result), "]");
  1186. return;
  1187. }
  1188. }
  1189. bool OpenXRAPI::pre_draw_viewport(RID p_render_target) {
  1190. if (!can_render()) {
  1191. return false;
  1192. }
  1193. // TODO: at some point in time we may support multiple viewports in which case we need to handle that...
  1194. return true;
  1195. }
  1196. void OpenXRAPI::post_draw_viewport(RID p_render_target) {
  1197. if (!can_render()) {
  1198. return;
  1199. }
  1200. // TODO: at some point in time we may support multiple viewports in which case we need to handle that...
  1201. // TODO: if we can get PR 51179 to work properly we can change away from this approach and move this into get_external_texture or something
  1202. if (!image_acquired) {
  1203. if (!acquire_image(swapchain, image_index)) {
  1204. return;
  1205. }
  1206. image_acquired = true;
  1207. // print_line("OpenXR: acquired image " + itos(image_index) + ", copying...");
  1208. // Copy our buffer into our swap chain (remove once PR 51179 is done)
  1209. graphics_extension->copy_render_target_to_image(p_render_target, swapchain_graphics_data, image_index);
  1210. }
  1211. };
  1212. void OpenXRAPI::end_frame() {
  1213. XrResult result;
  1214. ERR_FAIL_COND(instance == XR_NULL_HANDLE);
  1215. if (!running) {
  1216. return;
  1217. }
  1218. if (frame_state.shouldRender && view_pose_valid && !image_acquired) {
  1219. print_line("OpenXR: No viewport was marked with use_xr, there is no rendered output!");
  1220. }
  1221. // must have:
  1222. // - shouldRender set to true
  1223. // - a valid view pose for projection_views[eye].pose to submit layer
  1224. // - an image to render
  1225. if (!frame_state.shouldRender || !view_pose_valid || !image_acquired) {
  1226. // submit 0 layers when we shouldn't render
  1227. XrFrameEndInfo frame_end_info = {
  1228. XR_TYPE_FRAME_END_INFO, // type
  1229. nullptr, // next
  1230. frame_state.predictedDisplayTime, // displayTime
  1231. XR_ENVIRONMENT_BLEND_MODE_OPAQUE, // environmentBlendMode
  1232. 0, // layerCount
  1233. nullptr // layers
  1234. };
  1235. result = xrEndFrame(session, &frame_end_info);
  1236. if (XR_FAILED(result)) {
  1237. print_line("OpenXR: failed to end frame! [", get_error_string(result), "]");
  1238. return;
  1239. }
  1240. // neither eye is rendered
  1241. return;
  1242. }
  1243. // release our swapchain image if we acquired it
  1244. if (image_acquired) {
  1245. image_acquired = false; // whether we succeed or not, consider this released.
  1246. release_image(swapchain);
  1247. }
  1248. for (uint32_t eye = 0; eye < view_count; eye++) {
  1249. projection_views[eye].fov = views[eye].fov;
  1250. projection_views[eye].pose = views[eye].pose;
  1251. }
  1252. Vector<const XrCompositionLayerBaseHeader *> layers_list;
  1253. // Add composition layers from providers
  1254. for (OpenXRCompositionLayerProvider *provider : composition_layer_providers) {
  1255. XrCompositionLayerBaseHeader *layer = provider->get_composition_layer();
  1256. if (layer) {
  1257. layers_list.push_back(layer);
  1258. }
  1259. }
  1260. XrCompositionLayerProjection projection_layer = {
  1261. XR_TYPE_COMPOSITION_LAYER_PROJECTION, // type
  1262. nullptr, // next
  1263. layers_list.size() > 1 ? XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT | XR_COMPOSITION_LAYER_CORRECT_CHROMATIC_ABERRATION_BIT : XR_COMPOSITION_LAYER_CORRECT_CHROMATIC_ABERRATION_BIT, // layerFlags
  1264. play_space, // space
  1265. view_count, // viewCount
  1266. projection_views, // views
  1267. };
  1268. layers_list.push_back((const XrCompositionLayerBaseHeader *)&projection_layer);
  1269. XrFrameEndInfo frame_end_info = {
  1270. XR_TYPE_FRAME_END_INFO, // type
  1271. nullptr, // next
  1272. frame_state.predictedDisplayTime, // displayTime
  1273. XR_ENVIRONMENT_BLEND_MODE_OPAQUE, // environmentBlendMode
  1274. static_cast<uint32_t>(layers_list.size()), // layerCount
  1275. layers_list.ptr() // layers
  1276. };
  1277. result = xrEndFrame(session, &frame_end_info);
  1278. if (XR_FAILED(result)) {
  1279. print_line("OpenXR: failed to end frame! [", get_error_string(result), "]");
  1280. return;
  1281. }
  1282. }
  1283. OpenXRAPI::OpenXRAPI() {
  1284. // OpenXRAPI is only constructed if OpenXR is enabled.
  1285. singleton = this;
  1286. if (Engine::get_singleton()->is_editor_hint()) {
  1287. // Enabled OpenXR in the editor? Adjust our settings for the editor
  1288. } else {
  1289. // Load settings from project settings
  1290. int ff = GLOBAL_GET("xr/openxr/form_factor");
  1291. switch (ff) {
  1292. case 0: {
  1293. form_factor = XR_FORM_FACTOR_HEAD_MOUNTED_DISPLAY;
  1294. } break;
  1295. case 1: {
  1296. form_factor = XR_FORM_FACTOR_HANDHELD_DISPLAY;
  1297. } break;
  1298. default:
  1299. break;
  1300. }
  1301. int vc = GLOBAL_GET("xr/openxr/view_configuration");
  1302. switch (vc) {
  1303. case 0: {
  1304. view_configuration = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_MONO;
  1305. } break;
  1306. case 1: {
  1307. view_configuration = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO;
  1308. } break;
  1309. /* we don't support quad and observer configurations (yet)
  1310. case 2: {
  1311. view_configuration = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_QUAD_VARJO;
  1312. } break;
  1313. case 3: {
  1314. view_configuration = XR_VIEW_CONFIGURATION_TYPE_SECONDARY_MONO_FIRST_PERSON_OBSERVER_MSFT;
  1315. } break;
  1316. */
  1317. default:
  1318. break;
  1319. }
  1320. int rs = GLOBAL_GET("xr/openxr/reference_space");
  1321. switch (rs) {
  1322. case 0: {
  1323. reference_space = XR_REFERENCE_SPACE_TYPE_LOCAL;
  1324. } break;
  1325. case 1: {
  1326. reference_space = XR_REFERENCE_SPACE_TYPE_STAGE;
  1327. } break;
  1328. default:
  1329. break;
  1330. }
  1331. }
  1332. // reset a few things that can't be done in our class definition
  1333. frame_state.predictedDisplayTime = 0;
  1334. frame_state.predictedDisplayPeriod = 0;
  1335. #ifdef ANDROID_ENABLED
  1336. // our android wrapper will initialize our android loader at this point
  1337. register_extension_wrapper(memnew(OpenXRAndroidExtension(this)));
  1338. #endif
  1339. // register our other extensions
  1340. register_extension_wrapper(memnew(OpenXRHTCViveTrackerExtension(this)));
  1341. }
  1342. OpenXRAPI::~OpenXRAPI() {
  1343. // cleanup our composition layer providers
  1344. for (OpenXRCompositionLayerProvider *provider : composition_layer_providers) {
  1345. memdelete(provider);
  1346. }
  1347. composition_layer_providers.clear();
  1348. // cleanup our extension wrappers
  1349. for (OpenXRExtensionWrapper *extension_wrapper : registered_extension_wrappers) {
  1350. memdelete(extension_wrapper);
  1351. }
  1352. registered_extension_wrappers.clear();
  1353. if (supported_extensions != nullptr) {
  1354. memfree(supported_extensions);
  1355. supported_extensions = nullptr;
  1356. }
  1357. if (layer_properties != nullptr) {
  1358. memfree(layer_properties);
  1359. layer_properties = nullptr;
  1360. }
  1361. singleton = nullptr;
  1362. }
  1363. Transform3D OpenXRAPI::transform_from_pose(const XrPosef &p_pose) {
  1364. Quaternion q(p_pose.orientation.x, p_pose.orientation.y, p_pose.orientation.z, p_pose.orientation.w);
  1365. Basis basis(q);
  1366. Vector3 origin(p_pose.position.x, p_pose.position.y, p_pose.position.z);
  1367. return Transform3D(basis, origin);
  1368. }
  1369. template <typename T>
  1370. XRPose::TrackingConfidence _transform_from_location(const T &p_location, Transform3D &r_transform) {
  1371. Basis basis;
  1372. Vector3 origin;
  1373. XRPose::TrackingConfidence confidence = XRPose::XR_TRACKING_CONFIDENCE_NONE;
  1374. const auto &pose = p_location.pose;
  1375. // Check orientation
  1376. if (p_location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_VALID_BIT) {
  1377. Quaternion q(pose.orientation.x, pose.orientation.y, pose.orientation.z, pose.orientation.w);
  1378. r_transform.basis = Basis(q);
  1379. if (p_location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_TRACKED_BIT) {
  1380. // Fully valid orientation, so either 3DOF or 6DOF tracking with high confidence so default to HIGH_TRACKING
  1381. confidence = XRPose::XR_TRACKING_CONFIDENCE_HIGH;
  1382. } else {
  1383. // Orientation is being tracked but we're using old/predicted data, so low tracking confidence
  1384. confidence = XRPose::XR_TRACKING_CONFIDENCE_LOW;
  1385. }
  1386. } else {
  1387. r_transform.basis = Basis();
  1388. }
  1389. // Check location
  1390. if (p_location.locationFlags & XR_SPACE_LOCATION_POSITION_VALID_BIT) {
  1391. r_transform.origin = Vector3(pose.position.x, pose.position.y, pose.position.z);
  1392. if (!(p_location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_TRACKED_BIT)) {
  1393. // Location is being tracked but we're using old/predicted data, so low tracking confidence
  1394. confidence = XRPose::XR_TRACKING_CONFIDENCE_LOW;
  1395. } else if (confidence == XRPose::XR_TRACKING_CONFIDENCE_NONE) {
  1396. // Position tracking without orientation tracking?
  1397. confidence = XRPose::XR_TRACKING_CONFIDENCE_HIGH;
  1398. }
  1399. } else {
  1400. // No tracking or 3DOF I guess..
  1401. r_transform.origin = Vector3();
  1402. }
  1403. return confidence;
  1404. }
  1405. XRPose::TrackingConfidence OpenXRAPI::transform_from_location(const XrSpaceLocation &p_location, Transform3D &r_transform) {
  1406. return _transform_from_location(p_location, r_transform);
  1407. }
  1408. XRPose::TrackingConfidence OpenXRAPI::transform_from_location(const XrHandJointLocationEXT &p_location, Transform3D &r_transform) {
  1409. return _transform_from_location(p_location, r_transform);
  1410. }
  1411. void OpenXRAPI::parse_velocities(const XrSpaceVelocity &p_velocity, Vector3 &r_linear_velocity, Vector3 &r_angular_velocity) {
  1412. if (p_velocity.velocityFlags & XR_SPACE_VELOCITY_LINEAR_VALID_BIT) {
  1413. XrVector3f linear_velocity = p_velocity.linearVelocity;
  1414. r_linear_velocity = Vector3(linear_velocity.x, linear_velocity.y, linear_velocity.z);
  1415. } else {
  1416. r_linear_velocity = Vector3();
  1417. }
  1418. if (p_velocity.velocityFlags & XR_SPACE_VELOCITY_ANGULAR_VALID_BIT) {
  1419. XrVector3f angular_velocity = p_velocity.angularVelocity;
  1420. r_angular_velocity = Vector3(angular_velocity.x, angular_velocity.y, angular_velocity.z);
  1421. } else {
  1422. r_angular_velocity = Vector3();
  1423. }
  1424. }
  1425. RID OpenXRAPI::get_tracker_rid(XrPath p_path) {
  1426. List<RID> current;
  1427. tracker_owner.get_owned_list(&current);
  1428. for (int i = 0; i < current.size(); i++) {
  1429. Tracker *tracker = tracker_owner.get_or_null(current[i]);
  1430. if (tracker && tracker->toplevel_path == p_path) {
  1431. return current[i];
  1432. }
  1433. }
  1434. return RID();
  1435. }
  1436. RID OpenXRAPI::tracker_create(const String p_name) {
  1437. ERR_FAIL_COND_V(instance == XR_NULL_HANDLE, RID());
  1438. Tracker new_tracker;
  1439. new_tracker.name = p_name;
  1440. new_tracker.toplevel_path = XR_NULL_PATH;
  1441. new_tracker.active_profile_rid = RID();
  1442. XrResult result = xrStringToPath(instance, p_name.utf8().get_data(), &new_tracker.toplevel_path);
  1443. if (XR_FAILED(result)) {
  1444. print_line("OpenXR: failed to get path for ", p_name, "! [", get_error_string(result), "]");
  1445. return RID();
  1446. }
  1447. return tracker_owner.make_rid(new_tracker);
  1448. }
  1449. String OpenXRAPI::tracker_get_name(RID p_tracker) {
  1450. if (p_tracker.is_null()) {
  1451. return String("None");
  1452. }
  1453. Tracker *tracker = tracker_owner.get_or_null(p_tracker);
  1454. ERR_FAIL_NULL_V(tracker, String());
  1455. return tracker->name;
  1456. }
  1457. void OpenXRAPI::tracker_check_profile(RID p_tracker, XrSession p_session) {
  1458. if (p_session == XR_NULL_HANDLE) {
  1459. p_session = session;
  1460. }
  1461. Tracker *tracker = tracker_owner.get_or_null(p_tracker);
  1462. ERR_FAIL_NULL(tracker);
  1463. if (tracker->toplevel_path == XR_NULL_PATH) {
  1464. // no path, how was this even created?
  1465. return;
  1466. }
  1467. XrInteractionProfileState profile_state = {
  1468. XR_TYPE_INTERACTION_PROFILE_STATE, // type
  1469. nullptr, // next
  1470. XR_NULL_PATH // interactionProfile
  1471. };
  1472. XrResult result = xrGetCurrentInteractionProfile(p_session, tracker->toplevel_path, &profile_state);
  1473. if (XR_FAILED(result)) {
  1474. print_line("OpenXR: Failed to get interaction profile for", itos(tracker->toplevel_path), "[", get_error_string(result), "]");
  1475. return;
  1476. }
  1477. XrPath new_profile = profile_state.interactionProfile;
  1478. XrPath was_profile = get_interaction_profile_path(tracker->active_profile_rid);
  1479. if (was_profile != new_profile) {
  1480. tracker->active_profile_rid = get_interaction_profile_rid(new_profile);
  1481. if (xr_interface) {
  1482. xr_interface->tracker_profile_changed(p_tracker, tracker->active_profile_rid);
  1483. }
  1484. }
  1485. }
  1486. void OpenXRAPI::tracker_free(RID p_tracker) {
  1487. Tracker *tracker = tracker_owner.get_or_null(p_tracker);
  1488. ERR_FAIL_NULL(tracker);
  1489. // there is nothing to free here
  1490. tracker_owner.free(p_tracker);
  1491. }
  1492. RID OpenXRAPI::action_set_create(const String p_name, const String p_localized_name, const int p_priority) {
  1493. ERR_FAIL_COND_V(instance == XR_NULL_HANDLE, RID());
  1494. ActionSet action_set;
  1495. action_set.name = p_name;
  1496. action_set.is_attached = false;
  1497. // create our action set...
  1498. XrActionSetCreateInfo action_set_info = {
  1499. XR_TYPE_ACTION_SET_CREATE_INFO, // type
  1500. nullptr, // next
  1501. "", // actionSetName
  1502. "", // localizedActionSetName
  1503. uint32_t(p_priority) // priority
  1504. };
  1505. copy_string_to_char_buffer(p_name, action_set_info.actionSetName, XR_MAX_ACTION_SET_NAME_SIZE);
  1506. copy_string_to_char_buffer(p_localized_name, action_set_info.localizedActionSetName, XR_MAX_LOCALIZED_ACTION_SET_NAME_SIZE);
  1507. // print_line("Creating action set ", action_set_info.actionSetName, " - ", action_set_info.localizedActionSetName, " (", itos(action_set_info.priority), ")");
  1508. XrResult result = xrCreateActionSet(instance, &action_set_info, &action_set.handle);
  1509. if (XR_FAILED(result)) {
  1510. print_line("OpenXR: failed to create action set ", p_name, "! [", get_error_string(result), "]");
  1511. return RID();
  1512. }
  1513. return action_set_owner.make_rid(action_set);
  1514. }
  1515. String OpenXRAPI::action_set_get_name(RID p_action_set) {
  1516. if (p_action_set.is_null()) {
  1517. return String("None");
  1518. }
  1519. ActionSet *action_set = action_set_owner.get_or_null(p_action_set);
  1520. ERR_FAIL_NULL_V(action_set, String());
  1521. return action_set->name;
  1522. }
  1523. bool OpenXRAPI::action_set_attach(RID p_action_set) {
  1524. ActionSet *action_set = action_set_owner.get_or_null(p_action_set);
  1525. ERR_FAIL_NULL_V(action_set, false);
  1526. if (action_set->is_attached) {
  1527. // already attached
  1528. return true;
  1529. }
  1530. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, false);
  1531. // So according to the docs, once we attach our action set to our session it becomes read only..
  1532. // https://www.khronos.org/registry/OpenXR/specs/1.0/man/html/xrAttachSessionActionSets.html
  1533. XrSessionActionSetsAttachInfo attach_info = {
  1534. XR_TYPE_SESSION_ACTION_SETS_ATTACH_INFO, // type
  1535. nullptr, // next
  1536. 1, // countActionSets,
  1537. &action_set->handle // actionSets
  1538. };
  1539. XrResult result = xrAttachSessionActionSets(session, &attach_info);
  1540. if (XR_FAILED(result)) {
  1541. print_line("OpenXR: failed to attach action set! [", get_error_string(result), "]");
  1542. return false;
  1543. }
  1544. action_set->is_attached = true;
  1545. /* For debugging:
  1546. print_verbose("Attached set " + action_set->name);
  1547. List<RID> action_rids;
  1548. action_owner.get_owned_list(&action_rids);
  1549. for (int i = 0; i < action_rids.size(); i++) {
  1550. Action * action = action_owner.get_or_null(action_rids[i]);
  1551. if (action && action->action_set_rid == p_action_set) {
  1552. print_verbose(" - Action " + action->name + ": " + OpenXRUtil::get_action_type_name(action->action_type));
  1553. for (int j = 0; j < action->trackers.size(); j++) {
  1554. Tracker * tracker = tracker_owner.get_or_null(action->trackers[j].tracker_rid);
  1555. if (tracker) {
  1556. print_verbose(" - " + tracker->name);
  1557. }
  1558. }
  1559. }
  1560. }
  1561. */
  1562. return true;
  1563. }
  1564. void OpenXRAPI::action_set_free(RID p_action_set) {
  1565. ActionSet *action_set = action_set_owner.get_or_null(p_action_set);
  1566. ERR_FAIL_NULL(action_set);
  1567. if (action_set->handle != XR_NULL_HANDLE) {
  1568. xrDestroyActionSet(action_set->handle);
  1569. }
  1570. action_set_owner.free(p_action_set);
  1571. }
  1572. RID OpenXRAPI::get_action_rid(XrAction p_action) {
  1573. List<RID> current;
  1574. action_owner.get_owned_list(&current);
  1575. for (int i = 0; i < current.size(); i++) {
  1576. Action *action = action_owner.get_or_null(current[i]);
  1577. if (action && action->handle == p_action) {
  1578. return current[i];
  1579. }
  1580. }
  1581. return RID();
  1582. }
  1583. RID OpenXRAPI::action_create(RID p_action_set, const String p_name, const String p_localized_name, OpenXRAction::ActionType p_action_type, const Vector<RID> &p_trackers) {
  1584. ERR_FAIL_COND_V(instance == XR_NULL_HANDLE, RID());
  1585. Action action;
  1586. action.name = p_name;
  1587. ActionSet *action_set = action_set_owner.get_or_null(p_action_set);
  1588. ERR_FAIL_NULL_V(action_set, RID());
  1589. ERR_FAIL_COND_V(action_set->handle == XR_NULL_HANDLE, RID());
  1590. action.action_set_rid = p_action_set;
  1591. switch (p_action_type) {
  1592. case OpenXRAction::OPENXR_ACTION_BOOL:
  1593. action.action_type = XR_ACTION_TYPE_BOOLEAN_INPUT;
  1594. break;
  1595. case OpenXRAction::OPENXR_ACTION_FLOAT:
  1596. action.action_type = XR_ACTION_TYPE_FLOAT_INPUT;
  1597. break;
  1598. case OpenXRAction::OPENXR_ACTION_VECTOR2:
  1599. action.action_type = XR_ACTION_TYPE_VECTOR2F_INPUT;
  1600. break;
  1601. case OpenXRAction::OPENXR_ACTION_POSE:
  1602. action.action_type = XR_ACTION_TYPE_POSE_INPUT;
  1603. break;
  1604. case OpenXRAction::OPENXR_ACTION_HAPTIC:
  1605. action.action_type = XR_ACTION_TYPE_VIBRATION_OUTPUT;
  1606. break;
  1607. default:
  1608. ERR_FAIL_V(RID());
  1609. break;
  1610. }
  1611. Vector<XrPath> toplevel_paths;
  1612. for (int i = 0; i < p_trackers.size(); i++) {
  1613. Tracker *tracker = tracker_owner.get_or_null(p_trackers[i]);
  1614. if (tracker != nullptr && tracker->toplevel_path != XR_NULL_PATH) {
  1615. ActionTracker action_tracker = {
  1616. p_trackers[i], // tracker
  1617. XR_NULL_HANDLE, // space
  1618. false // was_location_valid
  1619. };
  1620. action.trackers.push_back(action_tracker);
  1621. toplevel_paths.push_back(tracker->toplevel_path);
  1622. }
  1623. }
  1624. XrActionCreateInfo action_info = {
  1625. XR_TYPE_ACTION_CREATE_INFO, // type
  1626. nullptr, // next
  1627. "", // actionName
  1628. action.action_type, // actionType
  1629. uint32_t(toplevel_paths.size()), // countSubactionPaths
  1630. toplevel_paths.ptr(), // subactionPaths
  1631. "" // localizedActionName
  1632. };
  1633. copy_string_to_char_buffer(p_name, action_info.actionName, XR_MAX_ACTION_NAME_SIZE);
  1634. copy_string_to_char_buffer(p_localized_name, action_info.localizedActionName, XR_MAX_LOCALIZED_ACTION_NAME_SIZE);
  1635. // print_line("Creating action ", action_info.actionName, action_info.localizedActionName, action_info.countSubactionPaths);
  1636. XrResult result = xrCreateAction(action_set->handle, &action_info, &action.handle);
  1637. if (XR_FAILED(result)) {
  1638. print_line("OpenXR: failed to create action ", p_name, "! [", get_error_string(result), "]");
  1639. return RID();
  1640. }
  1641. return action_owner.make_rid(action);
  1642. }
  1643. String OpenXRAPI::action_get_name(RID p_action) {
  1644. if (p_action.is_null()) {
  1645. return String("None");
  1646. }
  1647. Action *action = action_owner.get_or_null(p_action);
  1648. ERR_FAIL_NULL_V(action, String());
  1649. return action->name;
  1650. }
  1651. void OpenXRAPI::action_free(RID p_action) {
  1652. Action *action = action_owner.get_or_null(p_action);
  1653. ERR_FAIL_NULL(action);
  1654. if (action->handle != XR_NULL_HANDLE) {
  1655. xrDestroyAction(action->handle);
  1656. }
  1657. action_owner.free(p_action);
  1658. }
  1659. RID OpenXRAPI::get_interaction_profile_rid(XrPath p_path) {
  1660. List<RID> current;
  1661. interaction_profile_owner.get_owned_list(&current);
  1662. for (int i = 0; i < current.size(); i++) {
  1663. InteractionProfile *ip = interaction_profile_owner.get_or_null(current[i]);
  1664. if (ip && ip->path == p_path) {
  1665. return current[i];
  1666. }
  1667. }
  1668. return RID();
  1669. }
  1670. XrPath OpenXRAPI::get_interaction_profile_path(RID p_interaction_profile) {
  1671. if (p_interaction_profile.is_null()) {
  1672. return XR_NULL_PATH;
  1673. }
  1674. InteractionProfile *ip = interaction_profile_owner.get_or_null(p_interaction_profile);
  1675. ERR_FAIL_NULL_V(ip, XR_NULL_PATH);
  1676. return ip->path;
  1677. }
  1678. RID OpenXRAPI::interaction_profile_create(const String p_name) {
  1679. InteractionProfile new_interaction_profile;
  1680. XrResult result = xrStringToPath(instance, p_name.utf8().get_data(), &new_interaction_profile.path);
  1681. if (XR_FAILED(result)) {
  1682. print_line("OpenXR: failed to get path for ", p_name, "! [", get_error_string(result), "]");
  1683. return RID();
  1684. }
  1685. RID existing_ip = get_interaction_profile_rid(new_interaction_profile.path);
  1686. if (existing_ip.is_valid()) {
  1687. return existing_ip;
  1688. }
  1689. new_interaction_profile.name = p_name;
  1690. return interaction_profile_owner.make_rid(new_interaction_profile);
  1691. }
  1692. String OpenXRAPI::interaction_profile_get_name(RID p_interaction_profile) {
  1693. if (p_interaction_profile.is_null()) {
  1694. return String("None");
  1695. }
  1696. InteractionProfile *ip = interaction_profile_owner.get_or_null(p_interaction_profile);
  1697. ERR_FAIL_NULL_V(ip, String());
  1698. return ip->name;
  1699. }
  1700. void OpenXRAPI::interaction_profile_clear_bindings(RID p_interaction_profile) {
  1701. InteractionProfile *ip = interaction_profile_owner.get_or_null(p_interaction_profile);
  1702. ERR_FAIL_NULL(ip);
  1703. ip->bindings.clear();
  1704. }
  1705. bool OpenXRAPI::interaction_profile_add_binding(RID p_interaction_profile, RID p_action, const String p_path) {
  1706. InteractionProfile *ip = interaction_profile_owner.get_or_null(p_interaction_profile);
  1707. ERR_FAIL_NULL_V(ip, false);
  1708. XrActionSuggestedBinding binding;
  1709. Action *action = action_owner.get_or_null(p_action);
  1710. ERR_FAIL_COND_V(action == nullptr || action->handle == XR_NULL_HANDLE, false);
  1711. binding.action = action->handle;
  1712. XrResult result = xrStringToPath(instance, p_path.utf8().get_data(), &binding.binding);
  1713. if (XR_FAILED(result)) {
  1714. print_line("OpenXR: failed to get path for ", p_path, "! [", get_error_string(result), "]");
  1715. return false;
  1716. }
  1717. ip->bindings.push_back(binding);
  1718. return true;
  1719. }
  1720. bool OpenXRAPI::interaction_profile_suggest_bindings(RID p_interaction_profile) {
  1721. ERR_FAIL_COND_V(instance == XR_NULL_HANDLE, false);
  1722. InteractionProfile *ip = interaction_profile_owner.get_or_null(p_interaction_profile);
  1723. ERR_FAIL_NULL_V(ip, false);
  1724. const XrInteractionProfileSuggestedBinding suggested_bindings = {
  1725. XR_TYPE_INTERACTION_PROFILE_SUGGESTED_BINDING, // type
  1726. nullptr, // next
  1727. ip->path, // interactionProfile
  1728. uint32_t(ip->bindings.size()), // countSuggestedBindings
  1729. ip->bindings.ptr() // suggestedBindings
  1730. };
  1731. XrResult result = xrSuggestInteractionProfileBindings(instance, &suggested_bindings);
  1732. if (result == XR_ERROR_PATH_UNSUPPORTED) {
  1733. // this is fine, not all runtimes support all devices.
  1734. print_verbose("OpenXR Interaction profile " + ip->name + " is not supported on this runtime");
  1735. } else if (XR_FAILED(result)) {
  1736. print_line("OpenXR: failed to suggest bindings for ", ip->name, "! [", get_error_string(result), "]");
  1737. // reporting is enough...
  1738. }
  1739. /* For debugging:
  1740. print_verbose("Suggested bindings for " + ip->name);
  1741. for (int i = 0; i < ip->bindings.size(); i++) {
  1742. uint32_t strlen;
  1743. char path[XR_MAX_PATH_LENGTH];
  1744. String action_name = action_get_name(get_action_rid(ip->bindings[i].action));
  1745. XrResult result = xrPathToString(instance, ip->bindings[i].binding, XR_MAX_PATH_LENGTH, &strlen, path);
  1746. if (XR_FAILED(result)) {
  1747. print_line("OpenXR: failed to retrieve bindings for ", action_name, "! [", get_error_string(result), "]");
  1748. }
  1749. print_verbose(" - " + action_name + " => " + String(path));
  1750. }
  1751. */
  1752. return true;
  1753. }
  1754. void OpenXRAPI::interaction_profile_free(RID p_interaction_profile) {
  1755. InteractionProfile *ip = interaction_profile_owner.get_or_null(p_interaction_profile);
  1756. ERR_FAIL_NULL(ip);
  1757. ip->bindings.clear();
  1758. interaction_profile_owner.free(p_interaction_profile);
  1759. }
  1760. bool OpenXRAPI::sync_action_sets(const Vector<RID> p_active_sets) {
  1761. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, false);
  1762. if (!running) {
  1763. return false;
  1764. }
  1765. Vector<XrActiveActionSet> active_sets;
  1766. for (int i = 0; i < p_active_sets.size(); i++) {
  1767. ActionSet *action_set = action_set_owner.get_or_null(p_active_sets[i]);
  1768. if (action_set && action_set->handle != XR_NULL_HANDLE) {
  1769. XrActiveActionSet aset;
  1770. aset.actionSet = action_set->handle;
  1771. aset.subactionPath = XR_NULL_PATH;
  1772. active_sets.push_back(aset);
  1773. }
  1774. }
  1775. ERR_FAIL_COND_V(active_sets.size() == 0, false);
  1776. XrActionsSyncInfo sync_info = {
  1777. XR_TYPE_ACTIONS_SYNC_INFO, // type
  1778. nullptr, // next
  1779. uint32_t(active_sets.size()), // countActiveActionSets
  1780. active_sets.ptr() // activeActionSets
  1781. };
  1782. XrResult result = xrSyncActions(session, &sync_info);
  1783. if (XR_FAILED(result)) {
  1784. print_line("OpenXR: failed to sync active action sets! [", get_error_string(result), "]");
  1785. return false;
  1786. }
  1787. return true;
  1788. }
  1789. bool OpenXRAPI::get_action_bool(RID p_action, RID p_tracker) {
  1790. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, false);
  1791. Action *action = action_owner.get_or_null(p_action);
  1792. ERR_FAIL_NULL_V(action, false);
  1793. Tracker *tracker = tracker_owner.get_or_null(p_tracker);
  1794. ERR_FAIL_NULL_V(tracker, false);
  1795. if (!running) {
  1796. return false;
  1797. }
  1798. ERR_FAIL_COND_V(action->action_type != XR_ACTION_TYPE_BOOLEAN_INPUT, false);
  1799. XrActionStateGetInfo get_info = {
  1800. XR_TYPE_ACTION_STATE_GET_INFO, // type
  1801. nullptr, // next
  1802. action->handle, // action
  1803. tracker->toplevel_path // subactionPath
  1804. };
  1805. XrActionStateBoolean result_state;
  1806. result_state.type = XR_TYPE_ACTION_STATE_BOOLEAN,
  1807. result_state.next = nullptr;
  1808. XrResult result = xrGetActionStateBoolean(session, &get_info, &result_state);
  1809. if (XR_FAILED(result)) {
  1810. print_line("OpenXR: couldn't get action boolean! [", get_error_string(result), "]");
  1811. return false;
  1812. }
  1813. return result_state.isActive && result_state.currentState;
  1814. }
  1815. float OpenXRAPI::get_action_float(RID p_action, RID p_tracker) {
  1816. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, 0.0);
  1817. Action *action = action_owner.get_or_null(p_action);
  1818. ERR_FAIL_NULL_V(action, 0.0);
  1819. Tracker *tracker = tracker_owner.get_or_null(p_tracker);
  1820. ERR_FAIL_NULL_V(tracker, 0.0);
  1821. if (!running) {
  1822. return 0.0;
  1823. }
  1824. ERR_FAIL_COND_V(action->action_type != XR_ACTION_TYPE_FLOAT_INPUT, 0.0);
  1825. XrActionStateGetInfo get_info = {
  1826. XR_TYPE_ACTION_STATE_GET_INFO, // type
  1827. nullptr, // next
  1828. action->handle, // action
  1829. tracker->toplevel_path // subactionPath
  1830. };
  1831. XrActionStateFloat result_state;
  1832. result_state.type = XR_TYPE_ACTION_STATE_FLOAT,
  1833. result_state.next = nullptr;
  1834. XrResult result = xrGetActionStateFloat(session, &get_info, &result_state);
  1835. if (XR_FAILED(result)) {
  1836. print_line("OpenXR: couldn't get action float! [", get_error_string(result), "]");
  1837. return 0.0;
  1838. }
  1839. return result_state.isActive ? result_state.currentState : 0.0;
  1840. }
  1841. Vector2 OpenXRAPI::get_action_vector2(RID p_action, RID p_tracker) {
  1842. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, Vector2());
  1843. Action *action = action_owner.get_or_null(p_action);
  1844. ERR_FAIL_NULL_V(action, Vector2());
  1845. Tracker *tracker = tracker_owner.get_or_null(p_tracker);
  1846. ERR_FAIL_NULL_V(tracker, Vector2());
  1847. if (!running) {
  1848. return Vector2();
  1849. }
  1850. ERR_FAIL_COND_V(action->action_type != XR_ACTION_TYPE_VECTOR2F_INPUT, Vector2());
  1851. XrActionStateGetInfo get_info = {
  1852. XR_TYPE_ACTION_STATE_GET_INFO, // type
  1853. nullptr, // next
  1854. action->handle, // action
  1855. tracker->toplevel_path // subactionPath
  1856. };
  1857. XrActionStateVector2f result_state;
  1858. result_state.type = XR_TYPE_ACTION_STATE_VECTOR2F,
  1859. result_state.next = nullptr;
  1860. XrResult result = xrGetActionStateVector2f(session, &get_info, &result_state);
  1861. if (XR_FAILED(result)) {
  1862. print_line("OpenXR: couldn't get action vector2! [", get_error_string(result), "]");
  1863. return Vector2();
  1864. }
  1865. return result_state.isActive ? Vector2(result_state.currentState.x, result_state.currentState.y) : Vector2();
  1866. }
  1867. XRPose::TrackingConfidence OpenXRAPI::get_action_pose(RID p_action, RID p_tracker, Transform3D &r_transform, Vector3 &r_linear_velocity, Vector3 &r_angular_velocity) {
  1868. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, XRPose::XR_TRACKING_CONFIDENCE_NONE);
  1869. Action *action = action_owner.get_or_null(p_action);
  1870. ERR_FAIL_NULL_V(action, XRPose::XR_TRACKING_CONFIDENCE_NONE);
  1871. Tracker *tracker = tracker_owner.get_or_null(p_tracker);
  1872. ERR_FAIL_NULL_V(tracker, XRPose::XR_TRACKING_CONFIDENCE_NONE);
  1873. if (!running) {
  1874. return XRPose::XR_TRACKING_CONFIDENCE_NONE;
  1875. }
  1876. ERR_FAIL_COND_V(action->action_type != XR_ACTION_TYPE_POSE_INPUT, XRPose::XR_TRACKING_CONFIDENCE_NONE);
  1877. // print_verbose("Checking " + action->name + " => " + tracker->name + " (" + itos(tracker->toplevel_path) + ")");
  1878. uint64_t index = 0xFFFFFFFF;
  1879. uint64_t size = uint64_t(action->trackers.size());
  1880. for (uint64_t i = 0; i < size && index == 0xFFFFFFFF; i++) {
  1881. if (action->trackers[i].tracker_rid == p_tracker) {
  1882. index = i;
  1883. }
  1884. }
  1885. if (index == 0xFFFFFFFF) {
  1886. // couldn't find it?
  1887. return XRPose::XR_TRACKING_CONFIDENCE_NONE;
  1888. }
  1889. XrTime display_time = get_next_frame_time();
  1890. if (display_time == 0) {
  1891. return XRPose::XR_TRACKING_CONFIDENCE_NONE;
  1892. }
  1893. if (action->trackers[index].space == XR_NULL_HANDLE) {
  1894. // if this is a pose we need to define spaces
  1895. XrActionSpaceCreateInfo action_space_info = {
  1896. XR_TYPE_ACTION_SPACE_CREATE_INFO, // type
  1897. nullptr, // next
  1898. action->handle, // action
  1899. tracker->toplevel_path, // subactionPath
  1900. {
  1901. { 0.0, 0.0, 0.0, 1.0 }, // orientation
  1902. { 0.0, 0.0, 0.0 } // position
  1903. } // poseInActionSpace
  1904. };
  1905. XrSpace space;
  1906. XrResult result = xrCreateActionSpace(session, &action_space_info, &space);
  1907. if (XR_FAILED(result)) {
  1908. print_line("OpenXR: couldn't create action space! [", get_error_string(result), "]");
  1909. return XRPose::XR_TRACKING_CONFIDENCE_NONE;
  1910. }
  1911. action->trackers.ptrw()[index].space = space;
  1912. }
  1913. XrSpaceVelocity velocity = {
  1914. XR_TYPE_SPACE_VELOCITY, // type
  1915. nullptr, // next
  1916. 0, // velocityFlags
  1917. { 0.0, 0.0, 0.0 }, // linearVelocity
  1918. { 0.0, 0.0, 0.0 } // angularVelocity
  1919. };
  1920. XrSpaceLocation location = {
  1921. XR_TYPE_SPACE_LOCATION, // type
  1922. &velocity, // next
  1923. 0, // locationFlags
  1924. {
  1925. { 0.0, 0.0, 0.0, 0.0 }, // orientation
  1926. { 0.0, 0.0, 0.0 } // position
  1927. } // pose
  1928. };
  1929. XrResult result = xrLocateSpace(action->trackers[index].space, play_space, display_time, &location);
  1930. if (XR_FAILED(result)) {
  1931. print_line("OpenXR: failed to locate space! [", get_error_string(result), "]");
  1932. return XRPose::XR_TRACKING_CONFIDENCE_NONE;
  1933. }
  1934. XRPose::TrackingConfidence confidence = transform_from_location(location, r_transform);
  1935. parse_velocities(velocity, r_linear_velocity, r_angular_velocity);
  1936. return confidence;
  1937. }
  1938. bool OpenXRAPI::trigger_haptic_pulse(RID p_action, RID p_tracker, float p_frequency, float p_amplitude, XrDuration p_duration_ns) {
  1939. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, false);
  1940. Action *action = action_owner.get_or_null(p_action);
  1941. ERR_FAIL_NULL_V(action, false);
  1942. Tracker *tracker = tracker_owner.get_or_null(p_tracker);
  1943. ERR_FAIL_NULL_V(tracker, false);
  1944. if (!running) {
  1945. return false;
  1946. }
  1947. ERR_FAIL_COND_V(action->action_type != XR_ACTION_TYPE_VIBRATION_OUTPUT, false);
  1948. XrHapticActionInfo action_info = {
  1949. XR_TYPE_HAPTIC_ACTION_INFO, // type
  1950. nullptr, // next
  1951. action->handle, // action
  1952. tracker->toplevel_path // subactionPath
  1953. };
  1954. XrHapticVibration vibration = {
  1955. XR_TYPE_HAPTIC_VIBRATION, // type
  1956. nullptr, // next
  1957. p_duration_ns, // duration
  1958. p_frequency, // frequency
  1959. p_amplitude, // amplitude
  1960. };
  1961. XrResult result = xrApplyHapticFeedback(session, &action_info, (const XrHapticBaseHeader *)&vibration);
  1962. if (XR_FAILED(result)) {
  1963. print_line("OpenXR: failed to apply haptic feedback! [", get_error_string(result), "]");
  1964. return false;
  1965. }
  1966. return true;
  1967. }