2
0

openxr_api.cpp 86 KB

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