openxr_api.cpp 68 KB

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