openxr_api.cpp 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200
  1. /**************************************************************************/
  2. /* openxr_api.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "openxr_api.h"
  31. #include "extensions/openxr_extension_wrapper_extension.h"
  32. #include "openxr_interface.h"
  33. #include "openxr_util.h"
  34. #include "core/config/engine.h"
  35. #include "core/config/project_settings.h"
  36. #include "core/os/memory.h"
  37. #include "core/version.h"
  38. #ifdef TOOLS_ENABLED
  39. #include "editor/editor_settings.h"
  40. #endif
  41. #include "openxr_platform_inc.h"
  42. #ifdef VULKAN_ENABLED
  43. #include "extensions/platform/openxr_vulkan_extension.h"
  44. #endif
  45. #if defined(GLES3_ENABLED) && !defined(MACOS_ENABLED)
  46. #include "extensions/platform/openxr_opengl_extension.h"
  47. #endif
  48. #include "extensions/openxr_composition_layer_depth_extension.h"
  49. #include "extensions/openxr_fb_display_refresh_rate_extension.h"
  50. #include "extensions/openxr_fb_foveation_extension.h"
  51. #include "extensions/openxr_fb_update_swapchain_extension.h"
  52. #include "extensions/openxr_hand_tracking_extension.h"
  53. #ifdef ANDROID_ENABLED
  54. #define OPENXR_LOADER_NAME "libopenxr_loader.so"
  55. #endif
  56. OpenXRAPI *OpenXRAPI::singleton = nullptr;
  57. Vector<OpenXRExtensionWrapper *> OpenXRAPI::registered_extension_wrappers;
  58. bool OpenXRAPI::openxr_is_enabled(bool p_check_run_in_editor) {
  59. // @TODO we need an overrule switch so we can force enable openxr, i.e run "godot --openxr_enabled"
  60. if (Engine::get_singleton()->is_editor_hint() && p_check_run_in_editor) {
  61. // Disabled for now, using XR inside of the editor we'll be working on during the coming months.
  62. return false;
  63. } else {
  64. if (XRServer::get_xr_mode() == XRServer::XRMODE_DEFAULT) {
  65. return GLOBAL_GET("xr/openxr/enabled");
  66. } else {
  67. return XRServer::get_xr_mode() == XRServer::XRMODE_ON;
  68. }
  69. }
  70. }
  71. String OpenXRAPI::get_default_action_map_resource_name() {
  72. String name = GLOBAL_GET("xr/openxr/default_action_map");
  73. return name;
  74. }
  75. String OpenXRAPI::get_error_string(XrResult result) const {
  76. if (XR_SUCCEEDED(result)) {
  77. return String("Succeeded");
  78. }
  79. if (instance == XR_NULL_HANDLE) {
  80. Array args;
  81. args.push_back(Variant(result));
  82. return String("Error code {0}").format(args);
  83. }
  84. char resultString[XR_MAX_RESULT_STRING_SIZE];
  85. xrResultToString(instance, result, resultString);
  86. return String(resultString);
  87. }
  88. String OpenXRAPI::get_swapchain_format_name(int64_t p_swapchain_format) const {
  89. // This is rendering engine dependent...
  90. if (graphics_extension) {
  91. return graphics_extension->get_swapchain_format_name(p_swapchain_format);
  92. }
  93. return String("Swapchain format ") + String::num_int64(int64_t(p_swapchain_format));
  94. }
  95. bool OpenXRAPI::load_layer_properties() {
  96. // This queries additional layers that are available and can be initialized when we create our OpenXR instance
  97. if (layer_properties != nullptr) {
  98. // already retrieved this
  99. return true;
  100. }
  101. // Note, instance is not yet setup so we can't use get_error_string to retrieve our error
  102. XrResult result = xrEnumerateApiLayerProperties(0, &num_layer_properties, nullptr);
  103. ERR_FAIL_COND_V_MSG(XR_FAILED(result), false, "OpenXR: Failed to enumerate number of api layer properties");
  104. layer_properties = (XrApiLayerProperties *)memalloc(sizeof(XrApiLayerProperties) * num_layer_properties);
  105. ERR_FAIL_NULL_V(layer_properties, false);
  106. for (uint32_t i = 0; i < num_layer_properties; i++) {
  107. layer_properties[i].type = XR_TYPE_API_LAYER_PROPERTIES;
  108. layer_properties[i].next = nullptr;
  109. }
  110. result = xrEnumerateApiLayerProperties(num_layer_properties, &num_layer_properties, layer_properties);
  111. ERR_FAIL_COND_V_MSG(XR_FAILED(result), false, "OpenXR: Failed to enumerate api layer properties");
  112. for (uint32_t i = 0; i < num_layer_properties; i++) {
  113. print_verbose(String("OpenXR: Found OpenXR layer ") + layer_properties[i].layerName);
  114. }
  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. for (uint32_t i = 0; i < num_supported_extensions; i++) {
  136. print_verbose(String("OpenXR: Found OpenXR extension ") + supported_extensions[i].extensionName);
  137. }
  138. return true;
  139. }
  140. bool OpenXRAPI::is_extension_supported(const String &p_extension) const {
  141. for (uint32_t i = 0; i < num_supported_extensions; i++) {
  142. if (supported_extensions[i].extensionName == p_extension) {
  143. return true;
  144. }
  145. }
  146. return false;
  147. }
  148. bool OpenXRAPI::is_extension_enabled(const String &p_extension) const {
  149. CharString extension = p_extension.ascii();
  150. for (int i = 0; i < enabled_extensions.size(); i++) {
  151. if (strcmp(enabled_extensions[i].ptr(), extension.ptr()) == 0) {
  152. return true;
  153. }
  154. }
  155. return false;
  156. }
  157. bool OpenXRAPI::is_top_level_path_supported(const String &p_toplevel_path) {
  158. String required_extension = OpenXRInteractionProfileMetadata::get_singleton()->get_top_level_extension(p_toplevel_path);
  159. // If unsupported is returned we likely have a misspelled interaction profile path in our action map. Always output that as an error.
  160. ERR_FAIL_COND_V_MSG(required_extension == XR_PATH_UNSUPPORTED_NAME, false, "OpenXR: Unsupported toplevel path " + p_toplevel_path);
  161. if (required_extension == "") {
  162. // no extension needed, core top level are always "supported", they just won't be used if not really supported
  163. return true;
  164. }
  165. if (!is_extension_enabled(required_extension)) {
  166. // It is very likely we have top level paths for which the extension is not available so don't flood the logs with unnecessary spam.
  167. print_verbose("OpenXR: Top level path " + p_toplevel_path + " requires extension " + required_extension);
  168. return false;
  169. }
  170. return true;
  171. }
  172. bool OpenXRAPI::is_interaction_profile_supported(const String &p_ip_path) {
  173. String required_extension = OpenXRInteractionProfileMetadata::get_singleton()->get_interaction_profile_extension(p_ip_path);
  174. // If unsupported is returned we likely have a misspelled interaction profile path in our action map. Always output that as an error.
  175. ERR_FAIL_COND_V_MSG(required_extension == XR_PATH_UNSUPPORTED_NAME, false, "OpenXR: Unsupported interaction profile " + p_ip_path);
  176. if (required_extension == "") {
  177. // no extension needed, core interaction profiles are always "supported", they just won't be used if not really supported
  178. return true;
  179. }
  180. if (!is_extension_enabled(required_extension)) {
  181. // It is very likely we have interaction profiles for which the extension is not available so don't flood the logs with unnecessary spam.
  182. print_verbose("OpenXR: Interaction profile " + p_ip_path + " requires extension " + required_extension);
  183. return false;
  184. }
  185. return true;
  186. }
  187. bool OpenXRAPI::interaction_profile_supports_io_path(const String &p_ip_path, const String &p_io_path) {
  188. if (!is_interaction_profile_supported(p_ip_path)) {
  189. return false;
  190. }
  191. const OpenXRInteractionProfileMetadata::IOPath *io_path = OpenXRInteractionProfileMetadata::get_singleton()->get_io_path(p_ip_path, p_io_path);
  192. // If the io_path is not part of our metadata we've likely got a misspelled name or a bad action map, report
  193. ERR_FAIL_NULL_V_MSG(io_path, false, "OpenXR: Unsupported io path " + String(p_ip_path) + String(p_io_path));
  194. if (io_path->openxr_extension_name == "") {
  195. // no extension needed, core io paths are always "supported", they just won't be used if not really supported
  196. return true;
  197. }
  198. if (!is_extension_enabled(io_path->openxr_extension_name)) {
  199. // It is very likely we have io paths for which the extension is not available so don't flood the logs with unnecessary spam.
  200. print_verbose("OpenXR: IO path " + String(p_ip_path) + String(p_io_path) + " requires extension " + io_path->openxr_extension_name);
  201. return false;
  202. }
  203. return true;
  204. }
  205. void OpenXRAPI::copy_string_to_char_buffer(const String p_string, char *p_buffer, int p_buffer_len) {
  206. CharString char_string = p_string.utf8();
  207. int len = char_string.length();
  208. if (len < p_buffer_len - 1) {
  209. // was having weird CI issues with strcpy so....
  210. memcpy(p_buffer, char_string.get_data(), len);
  211. p_buffer[len] = '\0';
  212. } else {
  213. memcpy(p_buffer, char_string.get_data(), p_buffer_len - 1);
  214. p_buffer[p_buffer_len - 1] = '\0';
  215. }
  216. }
  217. bool OpenXRAPI::create_instance() {
  218. // Create our OpenXR instance, this will query any registered extension wrappers for extensions we need to enable.
  219. // Append the extensions requested by the registered extension wrappers.
  220. HashMap<String, bool *> requested_extensions;
  221. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  222. const HashMap<String, bool *> &wrapper_request_extensions = wrapper->get_requested_extensions();
  223. for (const KeyValue<String, bool *> &requested_extension : wrapper_request_extensions) {
  224. requested_extensions[requested_extension.key] = requested_extension.value;
  225. }
  226. }
  227. // Check which extensions are supported.
  228. enabled_extensions.clear();
  229. for (KeyValue<String, bool *> &requested_extension : requested_extensions) {
  230. if (!is_extension_supported(requested_extension.key)) {
  231. if (requested_extension.value == nullptr) {
  232. // Null means this is a mandatory extension so we fail.
  233. ERR_FAIL_V_MSG(false, String("OpenXR: OpenXR Runtime does not support ") + requested_extension.key + String(" extension!"));
  234. } else {
  235. // Set this extension as not supported.
  236. *requested_extension.value = false;
  237. }
  238. } else if (requested_extension.value != nullptr) {
  239. // Set this extension as supported.
  240. *requested_extension.value = true;
  241. // And record that we want to enable it.
  242. enabled_extensions.push_back(requested_extension.key.ascii());
  243. } else {
  244. // Record that we want to enable this.
  245. enabled_extensions.push_back(requested_extension.key.ascii());
  246. }
  247. }
  248. Vector<const char *> extension_ptrs;
  249. for (int i = 0; i < enabled_extensions.size(); i++) {
  250. print_verbose(String("OpenXR: Enabling extension ") + String(enabled_extensions[i]));
  251. extension_ptrs.push_back(enabled_extensions[i].get_data());
  252. }
  253. // Get our project name
  254. String project_name = GLOBAL_GET("application/config/name");
  255. // Create our OpenXR instance
  256. XrApplicationInfo application_info{
  257. "", // applicationName, we'll set this down below
  258. 1, // applicationVersion, we don't currently have this
  259. "Godot Game Engine", // engineName
  260. VERSION_MAJOR * 10000 + VERSION_MINOR * 100 + VERSION_PATCH, // engineVersion 4.0 -> 40000, 4.0.1 -> 40001, 4.1 -> 40100, etc.
  261. XR_CURRENT_API_VERSION // apiVersion
  262. };
  263. void *next_pointer = nullptr;
  264. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  265. void *np = wrapper->set_instance_create_info_and_get_next_pointer(next_pointer);
  266. if (np != nullptr) {
  267. next_pointer = np;
  268. }
  269. }
  270. XrInstanceCreateInfo instance_create_info = {
  271. XR_TYPE_INSTANCE_CREATE_INFO, // type
  272. next_pointer, // next
  273. 0, // createFlags
  274. application_info, // applicationInfo
  275. 0, // enabledApiLayerCount, need to find out if we need support for this?
  276. nullptr, // enabledApiLayerNames
  277. uint32_t(extension_ptrs.size()), // enabledExtensionCount
  278. extension_ptrs.ptr() // enabledExtensionNames
  279. };
  280. copy_string_to_char_buffer(project_name, instance_create_info.applicationInfo.applicationName, XR_MAX_APPLICATION_NAME_SIZE);
  281. XrResult result = xrCreateInstance(&instance_create_info, &instance);
  282. ERR_FAIL_COND_V_MSG(XR_FAILED(result), false, "Failed to create XR instance.");
  283. // from this point on we can use get_error_string to get more info about our errors...
  284. XrInstanceProperties instanceProps = {
  285. XR_TYPE_INSTANCE_PROPERTIES, // type;
  286. nullptr, // next
  287. 0, // runtimeVersion, from here will be set by our get call
  288. "" // runtimeName
  289. };
  290. OPENXR_API_INIT_XR_FUNC_V(xrGetInstanceProperties);
  291. result = xrGetInstanceProperties(instance, &instanceProps);
  292. if (XR_FAILED(result)) {
  293. // not fatal probably
  294. print_line("OpenXR: Failed to get XR instance properties [", get_error_string(result), "]");
  295. runtime_name = "";
  296. runtime_version = "";
  297. } else {
  298. runtime_name = instanceProps.runtimeName;
  299. runtime_version = OpenXRUtil::make_xr_version_string(instanceProps.runtimeVersion);
  300. print_line("OpenXR: Running on OpenXR runtime: ", runtime_name, " ", runtime_version);
  301. }
  302. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  303. wrapper->on_instance_created(instance);
  304. }
  305. return true;
  306. }
  307. bool OpenXRAPI::get_system_info() {
  308. // Retrieve basic OpenXR system info based on the form factor we desire
  309. // Retrieve the system for our form factor, fails if form factor is not available
  310. XrSystemGetInfo system_get_info = {
  311. XR_TYPE_SYSTEM_GET_INFO, // type;
  312. nullptr, // next
  313. form_factor // formFactor
  314. };
  315. XrResult result = xrGetSystem(instance, &system_get_info, &system_id);
  316. if (XR_FAILED(result)) {
  317. print_line("OpenXR: Failed to get system for our form factor [", get_error_string(result), "]");
  318. return false;
  319. }
  320. // obtain info about our system, writing this out completely to make CI on Linux happy..
  321. void *next_pointer = nullptr;
  322. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  323. void *np = wrapper->set_system_properties_and_get_next_pointer(next_pointer);
  324. if (np != nullptr) {
  325. next_pointer = np;
  326. }
  327. }
  328. XrSystemProperties system_properties = {
  329. XR_TYPE_SYSTEM_PROPERTIES, // type
  330. next_pointer, // next
  331. 0, // systemId, from here will be set by our get call
  332. 0, // vendorId
  333. "", // systemName
  334. {
  335. 0, // maxSwapchainImageHeight
  336. 0, // maxSwapchainImageWidth
  337. 0, // maxLayerCount
  338. }, // graphicsProperties
  339. {
  340. false, // orientationTracking
  341. false // positionTracking
  342. } // trackingProperties
  343. };
  344. result = xrGetSystemProperties(instance, system_id, &system_properties);
  345. if (XR_FAILED(result)) {
  346. print_line("OpenXR: Failed to get System properties [", get_error_string(result), "]");
  347. return false;
  348. }
  349. // remember this state, we'll use it later
  350. system_name = String(system_properties.systemName);
  351. vendor_id = system_properties.vendorId;
  352. graphics_properties = system_properties.graphicsProperties;
  353. tracking_properties = system_properties.trackingProperties;
  354. return true;
  355. }
  356. bool OpenXRAPI::load_supported_view_configuration_types() {
  357. // This queries the supported configuration types, likely there will only be one choosing between Mono (phone AR) and Stereo (HMDs)
  358. ERR_FAIL_COND_V(instance == XR_NULL_HANDLE, false);
  359. if (supported_view_configuration_types != nullptr) {
  360. // free previous results
  361. memfree(supported_view_configuration_types);
  362. supported_view_configuration_types = nullptr;
  363. }
  364. XrResult result = xrEnumerateViewConfigurations(instance, system_id, 0, &num_view_configuration_types, nullptr);
  365. if (XR_FAILED(result)) {
  366. print_line("OpenXR: Failed to get view configuration count [", get_error_string(result), "]");
  367. return false;
  368. }
  369. supported_view_configuration_types = (XrViewConfigurationType *)memalloc(sizeof(XrViewConfigurationType) * num_view_configuration_types);
  370. ERR_FAIL_NULL_V(supported_view_configuration_types, false);
  371. result = xrEnumerateViewConfigurations(instance, system_id, num_view_configuration_types, &num_view_configuration_types, supported_view_configuration_types);
  372. ERR_FAIL_COND_V_MSG(XR_FAILED(result), false, "OpenXR: Failed to enumerateview configurations");
  373. ERR_FAIL_COND_V_MSG(num_view_configuration_types == 0, false, "OpenXR: Failed to enumerateview configurations"); // JIC there should be at least 1!
  374. for (uint32_t i = 0; i < num_view_configuration_types; i++) {
  375. print_verbose(String("OpenXR: Found supported view configuration ") + OpenXRUtil::get_view_configuration_name(supported_view_configuration_types[i]));
  376. }
  377. // Check value we loaded at startup...
  378. if (!is_view_configuration_supported(view_configuration)) {
  379. print_verbose(String("OpenXR: ") + OpenXRUtil::get_view_configuration_name(view_configuration) + String(" isn't supported, defaulting to ") + OpenXRUtil::get_view_configuration_name(supported_view_configuration_types[0]));
  380. view_configuration = supported_view_configuration_types[0];
  381. }
  382. return true;
  383. }
  384. bool OpenXRAPI::load_supported_environmental_blend_modes() {
  385. // This queries the supported environmental blend modes.
  386. ERR_FAIL_COND_V(instance == XR_NULL_HANDLE, false);
  387. if (supported_environment_blend_modes != nullptr) {
  388. // free previous results
  389. memfree(supported_environment_blend_modes);
  390. supported_environment_blend_modes = nullptr;
  391. num_supported_environment_blend_modes = 0;
  392. }
  393. XrResult result = xrEnumerateEnvironmentBlendModes(instance, system_id, view_configuration, 0, &num_supported_environment_blend_modes, nullptr);
  394. if (XR_FAILED(result)) {
  395. print_line("OpenXR: Failed to get supported environmental blend mode count [", get_error_string(result), "]");
  396. return false;
  397. }
  398. supported_environment_blend_modes = (XrEnvironmentBlendMode *)memalloc(sizeof(XrEnvironmentBlendMode) * num_supported_environment_blend_modes);
  399. ERR_FAIL_NULL_V(supported_environment_blend_modes, false);
  400. result = xrEnumerateEnvironmentBlendModes(instance, system_id, view_configuration, num_supported_environment_blend_modes, &num_supported_environment_blend_modes, supported_environment_blend_modes);
  401. ERR_FAIL_COND_V_MSG(XR_FAILED(result), false, "OpenXR: Failed to enumerate environmental blend modes");
  402. ERR_FAIL_COND_V_MSG(num_supported_environment_blend_modes == 0, false, "OpenXR: Failed to enumerate environmental blend modes"); // JIC there should be at least 1!
  403. for (uint32_t i = 0; i < num_supported_environment_blend_modes; i++) {
  404. print_verbose(String("OpenXR: Found environmental blend mode ") + OpenXRUtil::get_environment_blend_mode_name(supported_environment_blend_modes[i]));
  405. }
  406. // Check value we loaded at startup...
  407. if (!is_environment_blend_mode_supported(environment_blend_mode)) {
  408. print_verbose(String("OpenXR: ") + OpenXRUtil::get_environment_blend_mode_name(environment_blend_mode) + String(" isn't supported, defaulting to ") + OpenXRUtil::get_environment_blend_mode_name(supported_environment_blend_modes[0]));
  409. environment_blend_mode = supported_environment_blend_modes[0];
  410. }
  411. return true;
  412. }
  413. bool OpenXRAPI::is_view_configuration_supported(XrViewConfigurationType p_configuration_type) const {
  414. ERR_FAIL_NULL_V(supported_view_configuration_types, false);
  415. for (uint32_t i = 0; i < num_view_configuration_types; i++) {
  416. if (supported_view_configuration_types[i] == p_configuration_type) {
  417. return true;
  418. }
  419. }
  420. return false;
  421. }
  422. bool OpenXRAPI::load_supported_view_configuration_views(XrViewConfigurationType p_configuration_type) {
  423. // This loads our view configuration for each view so for a stereo HMD, we'll get two entries (that are likely identical)
  424. // The returned data supplies us with the recommended render target size
  425. if (!is_view_configuration_supported(p_configuration_type)) {
  426. print_line("OpenXR: View configuration ", OpenXRUtil::get_view_configuration_name(view_configuration), " is not supported.");
  427. return false;
  428. }
  429. if (view_configuration_views != nullptr) {
  430. // free previous results
  431. memfree(view_configuration_views);
  432. view_configuration_views = nullptr;
  433. }
  434. XrResult result = xrEnumerateViewConfigurationViews(instance, system_id, p_configuration_type, 0, &view_count, nullptr);
  435. if (XR_FAILED(result)) {
  436. print_line("OpenXR: Failed to get view configuration count [", get_error_string(result), "]");
  437. return false;
  438. }
  439. view_configuration_views = (XrViewConfigurationView *)memalloc(sizeof(XrViewConfigurationView) * view_count);
  440. ERR_FAIL_NULL_V(view_configuration_views, false);
  441. for (uint32_t i = 0; i < view_count; i++) {
  442. view_configuration_views[i].type = XR_TYPE_VIEW_CONFIGURATION_VIEW;
  443. view_configuration_views[i].next = nullptr;
  444. }
  445. result = xrEnumerateViewConfigurationViews(instance, system_id, p_configuration_type, view_count, &view_count, view_configuration_views);
  446. ERR_FAIL_COND_V_MSG(XR_FAILED(result), false, "OpenXR: Failed to enumerate view configurations");
  447. for (uint32_t i = 0; i < view_count; i++) {
  448. print_verbose("OpenXR: Found supported view configuration view");
  449. print_verbose(String(" - width: ") + itos(view_configuration_views[i].maxImageRectWidth));
  450. print_verbose(String(" - height: ") + itos(view_configuration_views[i].maxImageRectHeight));
  451. print_verbose(String(" - sample count: ") + itos(view_configuration_views[i].maxSwapchainSampleCount));
  452. print_verbose(String(" - recommended render width: ") + itos(view_configuration_views[i].recommendedImageRectWidth));
  453. print_verbose(String(" - recommended render height: ") + itos(view_configuration_views[i].recommendedImageRectHeight));
  454. print_verbose(String(" - recommended render sample count: ") + itos(view_configuration_views[i].recommendedSwapchainSampleCount));
  455. }
  456. return true;
  457. }
  458. void OpenXRAPI::destroy_instance() {
  459. if (view_configuration_views != nullptr) {
  460. memfree(view_configuration_views);
  461. view_configuration_views = nullptr;
  462. }
  463. if (supported_view_configuration_types != nullptr) {
  464. memfree(supported_view_configuration_types);
  465. supported_view_configuration_types = nullptr;
  466. }
  467. if (supported_environment_blend_modes != nullptr) {
  468. memfree(supported_environment_blend_modes);
  469. supported_environment_blend_modes = nullptr;
  470. num_supported_environment_blend_modes = 0;
  471. }
  472. if (instance != XR_NULL_HANDLE) {
  473. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  474. wrapper->on_instance_destroyed();
  475. }
  476. xrDestroyInstance(instance);
  477. instance = XR_NULL_HANDLE;
  478. }
  479. enabled_extensions.clear();
  480. if (graphics_extension != nullptr) {
  481. unregister_extension_wrapper(graphics_extension);
  482. memdelete(graphics_extension);
  483. graphics_extension = nullptr;
  484. }
  485. }
  486. bool OpenXRAPI::create_session() {
  487. ERR_FAIL_COND_V(instance == XR_NULL_HANDLE, false);
  488. ERR_FAIL_COND_V(session != XR_NULL_HANDLE, false);
  489. void *next_pointer = nullptr;
  490. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  491. void *np = wrapper->set_session_create_and_get_next_pointer(next_pointer);
  492. if (np != nullptr) {
  493. next_pointer = np;
  494. }
  495. }
  496. XrSessionCreateInfo session_create_info = {
  497. XR_TYPE_SESSION_CREATE_INFO, // type
  498. next_pointer, // next
  499. 0, // createFlags
  500. system_id // systemId
  501. };
  502. XrResult result = xrCreateSession(instance, &session_create_info, &session);
  503. if (XR_FAILED(result)) {
  504. print_line("OpenXR: Failed to create session [", get_error_string(result), "]");
  505. return false;
  506. }
  507. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  508. wrapper->on_session_created(session);
  509. }
  510. return true;
  511. }
  512. bool OpenXRAPI::load_supported_reference_spaces() {
  513. // loads the supported reference spaces for our OpenXR session
  514. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, false);
  515. if (supported_reference_spaces != nullptr) {
  516. // free previous results
  517. memfree(supported_reference_spaces);
  518. supported_reference_spaces = nullptr;
  519. }
  520. XrResult result = xrEnumerateReferenceSpaces(session, 0, &num_reference_spaces, nullptr);
  521. if (XR_FAILED(result)) {
  522. print_line("OpenXR: Failed to get reference space count [", get_error_string(result), "]");
  523. return false;
  524. }
  525. supported_reference_spaces = (XrReferenceSpaceType *)memalloc(sizeof(XrReferenceSpaceType) * num_reference_spaces);
  526. ERR_FAIL_NULL_V(supported_reference_spaces, false);
  527. result = xrEnumerateReferenceSpaces(session, num_reference_spaces, &num_reference_spaces, supported_reference_spaces);
  528. ERR_FAIL_COND_V_MSG(XR_FAILED(result), false, "OpenXR: Failed to enumerate reference spaces");
  529. ERR_FAIL_COND_V_MSG(num_reference_spaces == 0, false, "OpenXR: Failed to enumerate reference spaces");
  530. for (uint32_t i = 0; i < num_reference_spaces; i++) {
  531. print_verbose(String("OpenXR: Found supported reference space ") + OpenXRUtil::get_reference_space_name(supported_reference_spaces[i]));
  532. }
  533. return true;
  534. }
  535. bool OpenXRAPI::is_reference_space_supported(XrReferenceSpaceType p_reference_space) {
  536. ERR_FAIL_NULL_V(supported_reference_spaces, false);
  537. for (uint32_t i = 0; i < num_reference_spaces; i++) {
  538. if (supported_reference_spaces[i] == p_reference_space) {
  539. return true;
  540. }
  541. }
  542. return false;
  543. }
  544. bool OpenXRAPI::setup_play_space() {
  545. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, false);
  546. XrPosef identityPose = {
  547. { 0.0, 0.0, 0.0, 1.0 },
  548. { 0.0, 0.0, 0.0 }
  549. };
  550. XrReferenceSpaceType new_reference_space;
  551. XrSpace new_play_space = XR_NULL_HANDLE;
  552. bool will_emulate_local_floor = false;
  553. if (is_reference_space_supported(requested_reference_space)) {
  554. new_reference_space = requested_reference_space;
  555. } else if (requested_reference_space == XR_REFERENCE_SPACE_TYPE_LOCAL_FLOOR_EXT && is_reference_space_supported(XR_REFERENCE_SPACE_TYPE_STAGE)) {
  556. print_verbose("OpenXR: LOCAL_FLOOR space isn't supported, emulating using STAGE and LOCAL spaces.");
  557. new_reference_space = XR_REFERENCE_SPACE_TYPE_LOCAL;
  558. will_emulate_local_floor = true;
  559. } else {
  560. // Fallback on LOCAL, which all OpenXR runtimes are required to support.
  561. print_verbose(String("OpenXR: ") + OpenXRUtil::get_reference_space_name(requested_reference_space) + String(" isn't supported, defaulting to LOCAL space."));
  562. new_reference_space = XR_REFERENCE_SPACE_TYPE_LOCAL;
  563. }
  564. XrReferenceSpaceCreateInfo play_space_create_info = {
  565. XR_TYPE_REFERENCE_SPACE_CREATE_INFO, // type
  566. nullptr, // next
  567. new_reference_space, // referenceSpaceType
  568. identityPose, // poseInReferenceSpace
  569. };
  570. XrResult result = xrCreateReferenceSpace(session, &play_space_create_info, &new_play_space);
  571. if (XR_FAILED(result)) {
  572. print_line("OpenXR: Failed to create play space [", get_error_string(result), "]");
  573. return false;
  574. }
  575. // If we've previously created a play space, clean it up first.
  576. if (play_space != XR_NULL_HANDLE) {
  577. xrDestroySpace(play_space);
  578. }
  579. play_space = new_play_space;
  580. reference_space = new_reference_space;
  581. emulating_local_floor = will_emulate_local_floor;
  582. if (emulating_local_floor) {
  583. // We'll use the STAGE space to get the floor height, but we can't do that until
  584. // after xrWaitFrame(), so just set this flag for now.
  585. should_reset_emulated_floor_height = true;
  586. }
  587. return true;
  588. }
  589. bool OpenXRAPI::setup_view_space() {
  590. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, false);
  591. if (!is_reference_space_supported(XR_REFERENCE_SPACE_TYPE_VIEW)) {
  592. print_line("OpenXR: reference space XR_REFERENCE_SPACE_TYPE_VIEW is not supported.");
  593. return false;
  594. }
  595. XrPosef identityPose = {
  596. { 0.0, 0.0, 0.0, 1.0 },
  597. { 0.0, 0.0, 0.0 }
  598. };
  599. XrReferenceSpaceCreateInfo view_space_create_info = {
  600. XR_TYPE_REFERENCE_SPACE_CREATE_INFO, // type
  601. nullptr, // next
  602. XR_REFERENCE_SPACE_TYPE_VIEW, // referenceSpaceType
  603. identityPose // poseInReferenceSpace
  604. };
  605. XrResult result = xrCreateReferenceSpace(session, &view_space_create_info, &view_space);
  606. if (XR_FAILED(result)) {
  607. print_line("OpenXR: Failed to create view space [", get_error_string(result), "]");
  608. return false;
  609. }
  610. return true;
  611. }
  612. bool OpenXRAPI::reset_emulated_floor_height() {
  613. ERR_FAIL_COND_V(!emulating_local_floor, false);
  614. // This is based on the example code in the OpenXR spec which shows how to
  615. // emulate LOCAL_FLOOR if it's not supported.
  616. // See: https://registry.khronos.org/OpenXR/specs/1.0/html/xrspec.html#XR_EXT_local_floor
  617. XrResult result;
  618. XrPosef identityPose = {
  619. { 0.0, 0.0, 0.0, 1.0 },
  620. { 0.0, 0.0, 0.0 }
  621. };
  622. XrSpace local_space = XR_NULL_HANDLE;
  623. XrSpace stage_space = XR_NULL_HANDLE;
  624. XrReferenceSpaceCreateInfo create_info = {
  625. XR_TYPE_REFERENCE_SPACE_CREATE_INFO, // type
  626. nullptr, // next
  627. XR_REFERENCE_SPACE_TYPE_LOCAL, // referenceSpaceType
  628. identityPose, // poseInReferenceSpace
  629. };
  630. result = xrCreateReferenceSpace(session, &create_info, &local_space);
  631. if (XR_FAILED(result)) {
  632. print_line("OpenXR: Failed to create LOCAL space in order to emulate LOCAL_FLOOR [", get_error_string(result), "]");
  633. return false;
  634. }
  635. create_info.referenceSpaceType = XR_REFERENCE_SPACE_TYPE_STAGE;
  636. result = xrCreateReferenceSpace(session, &create_info, &stage_space);
  637. if (XR_FAILED(result)) {
  638. print_line("OpenXR: Failed to create STAGE space in order to emulate LOCAL_FLOOR [", get_error_string(result), "]");
  639. xrDestroySpace(local_space);
  640. return false;
  641. }
  642. XrSpaceLocation stage_location = {
  643. XR_TYPE_SPACE_LOCATION, // type
  644. nullptr, // next
  645. 0, // locationFlags
  646. identityPose, // pose
  647. };
  648. result = xrLocateSpace(stage_space, local_space, get_next_frame_time(), &stage_location);
  649. xrDestroySpace(local_space);
  650. xrDestroySpace(stage_space);
  651. if (XR_FAILED(result)) {
  652. print_line("OpenXR: Failed to locate STAGE space in LOCAL space, in order to emulate LOCAL_FLOOR [", get_error_string(result), "]");
  653. return false;
  654. }
  655. XrSpace new_play_space;
  656. create_info.referenceSpaceType = XR_REFERENCE_SPACE_TYPE_LOCAL;
  657. create_info.poseInReferenceSpace.position.y = stage_location.pose.position.y;
  658. result = xrCreateReferenceSpace(session, &create_info, &new_play_space);
  659. if (XR_FAILED(result)) {
  660. print_line("OpenXR: Failed to recreate emulated LOCAL_FLOOR play space with latest floor estimate [", get_error_string(result), "]");
  661. return false;
  662. }
  663. xrDestroySpace(play_space);
  664. play_space = new_play_space;
  665. // If we've made it this far, it means we can properly emulate LOCAL_FLOOR, so we'll
  666. // report that as the reference space to the outside world.
  667. reference_space = XR_REFERENCE_SPACE_TYPE_LOCAL_FLOOR_EXT;
  668. return true;
  669. }
  670. bool OpenXRAPI::load_supported_swapchain_formats() {
  671. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, false);
  672. if (supported_swapchain_formats != nullptr) {
  673. // free previous results
  674. memfree(supported_swapchain_formats);
  675. supported_swapchain_formats = nullptr;
  676. }
  677. XrResult result = xrEnumerateSwapchainFormats(session, 0, &num_swapchain_formats, nullptr);
  678. if (XR_FAILED(result)) {
  679. print_line("OpenXR: Failed to get swapchain format count [", get_error_string(result), "]");
  680. return false;
  681. }
  682. supported_swapchain_formats = (int64_t *)memalloc(sizeof(int64_t) * num_swapchain_formats);
  683. ERR_FAIL_NULL_V(supported_swapchain_formats, false);
  684. result = xrEnumerateSwapchainFormats(session, num_swapchain_formats, &num_swapchain_formats, supported_swapchain_formats);
  685. ERR_FAIL_COND_V_MSG(XR_FAILED(result), false, "OpenXR: Failed to enumerate swapchain formats");
  686. for (uint32_t i = 0; i < num_swapchain_formats; i++) {
  687. print_verbose(String("OpenXR: Found supported swapchain format ") + get_swapchain_format_name(supported_swapchain_formats[i]));
  688. }
  689. return true;
  690. }
  691. bool OpenXRAPI::is_swapchain_format_supported(int64_t p_swapchain_format) {
  692. ERR_FAIL_NULL_V(supported_swapchain_formats, false);
  693. for (uint32_t i = 0; i < num_swapchain_formats; i++) {
  694. if (supported_swapchain_formats[i] == p_swapchain_format) {
  695. return true;
  696. }
  697. }
  698. return false;
  699. }
  700. bool OpenXRAPI::create_swapchains() {
  701. ERR_FAIL_NULL_V(graphics_extension, false);
  702. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, false);
  703. /*
  704. TODO: We need to improve on this, for now we're taking our old approach of creating our main swapchains and substituting
  705. those for the ones Godot normally creates.
  706. This however means we can only use swapchains for our main XR view.
  707. It would have been nicer if we could override the swapchain creation in Godot with ours but we have a timing issue here.
  708. We can't create XR swapchains until after our XR session is fully instantiated, yet Godot creates its swapchain much earlier.
  709. Also Godot only creates a swapchain for the main output.
  710. OpenXR will require us to create swapchains as the render target for additional viewports if we want to use the layer system
  711. to optimize text rendering and background rendering as OpenXR may choose to reuse the results for reprojection while we're
  712. already rendering the next frame.
  713. Finally an area we need to expand upon is that Foveated rendering is only enabled for the swap chain we create,
  714. as we render 3D content into internal buffers that are copied into the swapchain, we do now have (basic) VRS support
  715. */
  716. Size2 recommended_size = get_recommended_target_size();
  717. uint32_t sample_count = 1;
  718. // We start with our color swapchain...
  719. {
  720. // Build a vector with swapchain formats we want to use, from best fit to worst
  721. Vector<int64_t> usable_swapchain_formats;
  722. color_swapchain_format = 0;
  723. graphics_extension->get_usable_swapchain_formats(usable_swapchain_formats);
  724. // now find out which one is supported
  725. for (int i = 0; i < usable_swapchain_formats.size() && color_swapchain_format == 0; i++) {
  726. if (is_swapchain_format_supported(usable_swapchain_formats[i])) {
  727. color_swapchain_format = usable_swapchain_formats[i];
  728. }
  729. }
  730. if (color_swapchain_format == 0) {
  731. color_swapchain_format = usable_swapchain_formats[0]; // just use the first one and hope for the best...
  732. print_line("Couldn't find usable color swap chain format, using", get_swapchain_format_name(color_swapchain_format), "instead.");
  733. } else {
  734. print_verbose(String("Using color swap chain format:") + get_swapchain_format_name(color_swapchain_format));
  735. }
  736. if (!create_swapchain(0, XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT | XR_SWAPCHAIN_USAGE_MUTABLE_FORMAT_BIT, color_swapchain_format, recommended_size.width, recommended_size.height, sample_count, view_count, swapchains[OPENXR_SWAPCHAIN_COLOR].swapchain, &swapchains[OPENXR_SWAPCHAIN_COLOR].swapchain_graphics_data)) {
  737. return false;
  738. }
  739. }
  740. views = (XrView *)memalloc(sizeof(XrView) * view_count);
  741. ERR_FAIL_NULL_V_MSG(views, false, "OpenXR Couldn't allocate memory for views");
  742. projection_views = (XrCompositionLayerProjectionView *)memalloc(sizeof(XrCompositionLayerProjectionView) * view_count);
  743. ERR_FAIL_NULL_V_MSG(projection_views, false, "OpenXR Couldn't allocate memory for projection views");
  744. // We create our depth swapchain if:
  745. // - we've enabled submitting depth buffer
  746. // - we support our depth layer extension
  747. // - we have our spacewarp extension (not yet implemented)
  748. if (submit_depth_buffer && OpenXRCompositionLayerDepthExtension::get_singleton()->is_available()) {
  749. // Build a vector with swapchain formats we want to use, from best fit to worst
  750. Vector<int64_t> usable_swapchain_formats;
  751. depth_swapchain_format = 0;
  752. graphics_extension->get_usable_depth_formats(usable_swapchain_formats);
  753. // now find out which one is supported
  754. for (int i = 0; i < usable_swapchain_formats.size() && depth_swapchain_format == 0; i++) {
  755. if (is_swapchain_format_supported(usable_swapchain_formats[i])) {
  756. depth_swapchain_format = usable_swapchain_formats[i];
  757. }
  758. }
  759. if (depth_swapchain_format == 0) {
  760. print_line("Couldn't find usable depth swap chain format, depth buffer will not be submitted.");
  761. } else {
  762. print_verbose(String("Using depth swap chain format:") + get_swapchain_format_name(depth_swapchain_format));
  763. // Note, if VK_FORMAT_D32_SFLOAT is used here but we're using the forward+ renderer, we should probably output a warning.
  764. if (!create_swapchain(0, XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, depth_swapchain_format, recommended_size.width, recommended_size.height, sample_count, view_count, swapchains[OPENXR_SWAPCHAIN_DEPTH].swapchain, &swapchains[OPENXR_SWAPCHAIN_DEPTH].swapchain_graphics_data)) {
  765. return false;
  766. }
  767. depth_views = (XrCompositionLayerDepthInfoKHR *)memalloc(sizeof(XrCompositionLayerDepthInfoKHR) * view_count);
  768. ERR_FAIL_NULL_V_MSG(depth_views, false, "OpenXR Couldn't allocate memory for depth views");
  769. }
  770. }
  771. // We create our velocity swapchain if:
  772. // - we have our spacewarp extension (not yet implemented)
  773. {
  774. // TBD
  775. }
  776. for (uint32_t i = 0; i < view_count; i++) {
  777. views[i].type = XR_TYPE_VIEW;
  778. views[i].next = nullptr;
  779. projection_views[i].type = XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW;
  780. projection_views[i].next = nullptr;
  781. projection_views[i].subImage.swapchain = swapchains[OPENXR_SWAPCHAIN_COLOR].swapchain;
  782. projection_views[i].subImage.imageArrayIndex = i;
  783. projection_views[i].subImage.imageRect.offset.x = 0;
  784. projection_views[i].subImage.imageRect.offset.y = 0;
  785. projection_views[i].subImage.imageRect.extent.width = recommended_size.width;
  786. projection_views[i].subImage.imageRect.extent.height = recommended_size.height;
  787. if (submit_depth_buffer && OpenXRCompositionLayerDepthExtension::get_singleton()->is_available() && depth_views) {
  788. projection_views[i].next = &depth_views[i];
  789. depth_views[i].type = XR_TYPE_COMPOSITION_LAYER_DEPTH_INFO_KHR;
  790. depth_views[i].next = nullptr;
  791. depth_views[i].subImage.swapchain = swapchains[OPENXR_SWAPCHAIN_DEPTH].swapchain;
  792. depth_views[i].subImage.imageArrayIndex = i;
  793. depth_views[i].subImage.imageRect.offset.x = 0;
  794. depth_views[i].subImage.imageRect.offset.y = 0;
  795. depth_views[i].subImage.imageRect.extent.width = recommended_size.width;
  796. depth_views[i].subImage.imageRect.extent.height = recommended_size.height;
  797. depth_views[i].minDepth = 0.0;
  798. depth_views[i].maxDepth = 1.0;
  799. depth_views[i].nearZ = 0.01; // Near and far Z will be set to the correct values in fill_projection_matrix
  800. depth_views[i].farZ = 100.0;
  801. }
  802. };
  803. return true;
  804. };
  805. void OpenXRAPI::destroy_session() {
  806. if (running && session != XR_NULL_HANDLE) {
  807. xrEndSession(session);
  808. }
  809. if (views != nullptr) {
  810. memfree(views);
  811. views = nullptr;
  812. }
  813. if (projection_views != nullptr) {
  814. memfree(projection_views);
  815. projection_views = nullptr;
  816. }
  817. if (depth_views != nullptr) {
  818. memfree(depth_views);
  819. depth_views = nullptr;
  820. }
  821. for (int i = 0; i < OPENXR_SWAPCHAIN_MAX; i++) {
  822. free_swapchain(swapchains[i]);
  823. }
  824. if (supported_swapchain_formats != nullptr) {
  825. memfree(supported_swapchain_formats);
  826. supported_swapchain_formats = nullptr;
  827. }
  828. // destroy our spaces
  829. if (play_space != XR_NULL_HANDLE) {
  830. xrDestroySpace(play_space);
  831. play_space = XR_NULL_HANDLE;
  832. }
  833. if (view_space != XR_NULL_HANDLE) {
  834. xrDestroySpace(view_space);
  835. view_space = XR_NULL_HANDLE;
  836. }
  837. if (supported_reference_spaces != nullptr) {
  838. // free previous results
  839. memfree(supported_reference_spaces);
  840. supported_reference_spaces = nullptr;
  841. }
  842. if (session != XR_NULL_HANDLE) {
  843. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  844. wrapper->on_session_destroyed();
  845. }
  846. xrDestroySession(session);
  847. session = XR_NULL_HANDLE;
  848. }
  849. }
  850. bool OpenXRAPI::create_swapchain(XrSwapchainCreateFlags p_create_flags, 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) {
  851. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, false);
  852. ERR_FAIL_NULL_V(graphics_extension, false);
  853. XrResult result;
  854. void *next_pointer = nullptr;
  855. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  856. void *np = wrapper->set_swapchain_create_info_and_get_next_pointer(next_pointer);
  857. if (np != nullptr) {
  858. next_pointer = np;
  859. }
  860. }
  861. XrSwapchainCreateInfo swapchain_create_info = {
  862. XR_TYPE_SWAPCHAIN_CREATE_INFO, // type
  863. next_pointer, // next
  864. p_create_flags, // createFlags
  865. p_usage_flags, // usageFlags
  866. p_swapchain_format, // format
  867. p_sample_count, // sampleCount
  868. p_width, // width
  869. p_height, // height
  870. 1, // faceCount
  871. p_array_size, // arraySize
  872. 1 // mipCount
  873. };
  874. XrSwapchain new_swapchain;
  875. result = xrCreateSwapchain(session, &swapchain_create_info, &new_swapchain);
  876. if (XR_FAILED(result)) {
  877. print_line("OpenXR: Failed to get swapchain [", get_error_string(result), "]");
  878. return false;
  879. }
  880. 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)) {
  881. xrDestroySwapchain(new_swapchain);
  882. return false;
  883. }
  884. r_swapchain = new_swapchain;
  885. return true;
  886. }
  887. bool OpenXRAPI::on_state_idle() {
  888. print_verbose("On state idle");
  889. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  890. wrapper->on_state_idle();
  891. }
  892. return true;
  893. }
  894. bool OpenXRAPI::on_state_ready() {
  895. print_verbose("On state ready");
  896. // begin session
  897. XrSessionBeginInfo session_begin_info = {
  898. XR_TYPE_SESSION_BEGIN_INFO, // type
  899. nullptr, // next
  900. view_configuration // primaryViewConfigurationType
  901. };
  902. XrResult result = xrBeginSession(session, &session_begin_info);
  903. if (XR_FAILED(result)) {
  904. print_line("OpenXR: Failed to begin session [", get_error_string(result), "]");
  905. return false;
  906. }
  907. // This is when we create our swapchain, this can be a "long" time after Godot finishes, we can deal with this for now
  908. // but once we want to provide Viewports for additional layers where OpenXR requires us to create further swapchains,
  909. // we'll be creating those viewport WAY before we reach this point.
  910. // We may need to implement a wait in our init in main.cpp polling our events until the session is ready.
  911. // That will be very very ugly
  912. // The other possibility is to create a separate OpenXRViewport type specifically for this goal as part of our OpenXR module
  913. if (!create_swapchains()) {
  914. return false;
  915. }
  916. // we're running
  917. running = true;
  918. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  919. wrapper->on_state_ready();
  920. }
  921. if (xr_interface) {
  922. xr_interface->on_state_ready();
  923. }
  924. // TODO Tell android
  925. return true;
  926. }
  927. bool OpenXRAPI::on_state_synchronized() {
  928. print_verbose("On state synchronized");
  929. // Just in case, see if we already have active trackers...
  930. List<RID> trackers;
  931. tracker_owner.get_owned_list(&trackers);
  932. for (int i = 0; i < trackers.size(); i++) {
  933. tracker_check_profile(trackers[i]);
  934. }
  935. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  936. wrapper->on_state_synchronized();
  937. }
  938. return true;
  939. }
  940. bool OpenXRAPI::on_state_visible() {
  941. print_verbose("On state visible");
  942. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  943. wrapper->on_state_visible();
  944. }
  945. if (xr_interface) {
  946. xr_interface->on_state_visible();
  947. }
  948. return true;
  949. }
  950. bool OpenXRAPI::on_state_focused() {
  951. print_verbose("On state focused");
  952. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  953. wrapper->on_state_focused();
  954. }
  955. if (xr_interface) {
  956. xr_interface->on_state_focused();
  957. }
  958. return true;
  959. }
  960. bool OpenXRAPI::on_state_stopping() {
  961. print_verbose("On state stopping");
  962. if (xr_interface) {
  963. xr_interface->on_state_stopping();
  964. }
  965. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  966. wrapper->on_state_stopping();
  967. }
  968. if (running) {
  969. XrResult result = xrEndSession(session);
  970. if (XR_FAILED(result)) {
  971. // we only report this..
  972. print_line("OpenXR: Failed to end session [", get_error_string(result), "]");
  973. }
  974. running = false;
  975. }
  976. // TODO further cleanup
  977. return true;
  978. }
  979. bool OpenXRAPI::on_state_loss_pending() {
  980. print_verbose("On state loss pending");
  981. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  982. wrapper->on_state_loss_pending();
  983. }
  984. // 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)
  985. return true;
  986. }
  987. bool OpenXRAPI::on_state_exiting() {
  988. print_verbose("On state existing");
  989. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  990. wrapper->on_state_exiting();
  991. }
  992. // 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)
  993. return true;
  994. }
  995. void OpenXRAPI::set_form_factor(XrFormFactor p_form_factor) {
  996. ERR_FAIL_COND(is_initialized());
  997. form_factor = p_form_factor;
  998. }
  999. void OpenXRAPI::set_view_configuration(XrViewConfigurationType p_view_configuration) {
  1000. ERR_FAIL_COND(is_initialized());
  1001. view_configuration = p_view_configuration;
  1002. }
  1003. bool OpenXRAPI::set_requested_reference_space(XrReferenceSpaceType p_requested_reference_space) {
  1004. requested_reference_space = p_requested_reference_space;
  1005. if (is_initialized()) {
  1006. return setup_play_space();
  1007. }
  1008. return true;
  1009. }
  1010. void OpenXRAPI::set_submit_depth_buffer(bool p_submit_depth_buffer) {
  1011. ERR_FAIL_COND(is_initialized());
  1012. submit_depth_buffer = p_submit_depth_buffer;
  1013. }
  1014. bool OpenXRAPI::is_initialized() {
  1015. return (instance != XR_NULL_HANDLE);
  1016. }
  1017. bool OpenXRAPI::is_running() {
  1018. if (instance == XR_NULL_HANDLE) {
  1019. return false;
  1020. }
  1021. if (session == XR_NULL_HANDLE) {
  1022. return false;
  1023. }
  1024. return running;
  1025. }
  1026. bool OpenXRAPI::openxr_loader_init() {
  1027. #ifdef ANDROID_ENABLED
  1028. ERR_FAIL_COND_V_MSG(openxr_loader_library_handle != nullptr, false, "OpenXR Loader library is already loaded.");
  1029. {
  1030. Error error_code = OS::get_singleton()->open_dynamic_library(OPENXR_LOADER_NAME, openxr_loader_library_handle);
  1031. ERR_FAIL_COND_V_MSG(error_code != OK, false, "OpenXR loader not found.");
  1032. }
  1033. {
  1034. Error error_code = OS::get_singleton()->get_dynamic_library_symbol_handle(openxr_loader_library_handle, "xrGetInstanceProcAddr", (void *&)xrGetInstanceProcAddr);
  1035. ERR_FAIL_COND_V_MSG(error_code != OK, false, "Symbol xrGetInstanceProcAddr not found in OpenXR Loader library.");
  1036. }
  1037. #endif
  1038. // Resolve the symbols that don't require an instance
  1039. OPENXR_API_INIT_XR_FUNC_V(xrCreateInstance);
  1040. OPENXR_API_INIT_XR_FUNC_V(xrEnumerateApiLayerProperties);
  1041. OPENXR_API_INIT_XR_FUNC_V(xrEnumerateInstanceExtensionProperties);
  1042. return true;
  1043. }
  1044. bool OpenXRAPI::resolve_instance_openxr_symbols() {
  1045. ERR_FAIL_COND_V(instance == XR_NULL_HANDLE, false);
  1046. OPENXR_API_INIT_XR_FUNC_V(xrAcquireSwapchainImage);
  1047. OPENXR_API_INIT_XR_FUNC_V(xrApplyHapticFeedback);
  1048. OPENXR_API_INIT_XR_FUNC_V(xrAttachSessionActionSets);
  1049. OPENXR_API_INIT_XR_FUNC_V(xrBeginFrame);
  1050. OPENXR_API_INIT_XR_FUNC_V(xrBeginSession);
  1051. OPENXR_API_INIT_XR_FUNC_V(xrCreateAction);
  1052. OPENXR_API_INIT_XR_FUNC_V(xrCreateActionSet);
  1053. OPENXR_API_INIT_XR_FUNC_V(xrCreateActionSpace);
  1054. OPENXR_API_INIT_XR_FUNC_V(xrCreateReferenceSpace);
  1055. OPENXR_API_INIT_XR_FUNC_V(xrCreateSession);
  1056. OPENXR_API_INIT_XR_FUNC_V(xrCreateSwapchain);
  1057. OPENXR_API_INIT_XR_FUNC_V(xrDestroyAction);
  1058. OPENXR_API_INIT_XR_FUNC_V(xrDestroyActionSet);
  1059. OPENXR_API_INIT_XR_FUNC_V(xrDestroyInstance);
  1060. OPENXR_API_INIT_XR_FUNC_V(xrDestroySession);
  1061. OPENXR_API_INIT_XR_FUNC_V(xrDestroySpace);
  1062. OPENXR_API_INIT_XR_FUNC_V(xrDestroySwapchain);
  1063. OPENXR_API_INIT_XR_FUNC_V(xrEndFrame);
  1064. OPENXR_API_INIT_XR_FUNC_V(xrEndSession);
  1065. OPENXR_API_INIT_XR_FUNC_V(xrEnumerateEnvironmentBlendModes);
  1066. OPENXR_API_INIT_XR_FUNC_V(xrEnumerateReferenceSpaces);
  1067. OPENXR_API_INIT_XR_FUNC_V(xrEnumerateSwapchainFormats);
  1068. OPENXR_API_INIT_XR_FUNC_V(xrEnumerateViewConfigurations);
  1069. OPENXR_API_INIT_XR_FUNC_V(xrEnumerateViewConfigurationViews);
  1070. OPENXR_API_INIT_XR_FUNC_V(xrGetActionStateBoolean);
  1071. OPENXR_API_INIT_XR_FUNC_V(xrGetActionStateFloat);
  1072. OPENXR_API_INIT_XR_FUNC_V(xrGetActionStateVector2f);
  1073. OPENXR_API_INIT_XR_FUNC_V(xrGetCurrentInteractionProfile);
  1074. OPENXR_API_INIT_XR_FUNC_V(xrGetReferenceSpaceBoundsRect);
  1075. OPENXR_API_INIT_XR_FUNC_V(xrGetSystem);
  1076. OPENXR_API_INIT_XR_FUNC_V(xrGetSystemProperties);
  1077. OPENXR_API_INIT_XR_FUNC_V(xrLocateViews);
  1078. OPENXR_API_INIT_XR_FUNC_V(xrLocateSpace);
  1079. OPENXR_API_INIT_XR_FUNC_V(xrPathToString);
  1080. OPENXR_API_INIT_XR_FUNC_V(xrPollEvent);
  1081. OPENXR_API_INIT_XR_FUNC_V(xrReleaseSwapchainImage);
  1082. OPENXR_API_INIT_XR_FUNC_V(xrResultToString);
  1083. OPENXR_API_INIT_XR_FUNC_V(xrStringToPath);
  1084. OPENXR_API_INIT_XR_FUNC_V(xrSuggestInteractionProfileBindings);
  1085. OPENXR_API_INIT_XR_FUNC_V(xrSyncActions);
  1086. OPENXR_API_INIT_XR_FUNC_V(xrWaitFrame);
  1087. OPENXR_API_INIT_XR_FUNC_V(xrWaitSwapchainImage);
  1088. return true;
  1089. }
  1090. XrResult OpenXRAPI::try_get_instance_proc_addr(const char *p_name, PFN_xrVoidFunction *p_addr) {
  1091. return xrGetInstanceProcAddr(instance, p_name, p_addr);
  1092. }
  1093. XrResult OpenXRAPI::get_instance_proc_addr(const char *p_name, PFN_xrVoidFunction *p_addr) {
  1094. XrResult result = try_get_instance_proc_addr(p_name, p_addr);
  1095. if (result != XR_SUCCESS) {
  1096. String error_message = String("Symbol ") + p_name + " not found in OpenXR instance.";
  1097. ERR_FAIL_V_MSG(result, error_message.utf8().get_data());
  1098. }
  1099. return result;
  1100. }
  1101. bool OpenXRAPI::initialize(const String &p_rendering_driver) {
  1102. ERR_FAIL_COND_V_MSG(instance != XR_NULL_HANDLE, false, "OpenXR instance was already created");
  1103. if (!openxr_loader_init()) {
  1104. return false;
  1105. }
  1106. if (p_rendering_driver == "vulkan") {
  1107. #ifdef VULKAN_ENABLED
  1108. graphics_extension = memnew(OpenXRVulkanExtension);
  1109. register_extension_wrapper(graphics_extension);
  1110. #else
  1111. // shouldn't be possible...
  1112. ERR_FAIL_V(false);
  1113. #endif
  1114. } else if (p_rendering_driver == "opengl3") {
  1115. #if defined(GLES3_ENABLED) && !defined(MACOS_ENABLED)
  1116. graphics_extension = memnew(OpenXROpenGLExtension);
  1117. register_extension_wrapper(graphics_extension);
  1118. #else
  1119. // shouldn't be possible...
  1120. ERR_FAIL_V(false);
  1121. #endif
  1122. } else {
  1123. ERR_FAIL_V_MSG(false, "OpenXR: Unsupported rendering device.");
  1124. }
  1125. // Also register our rendering extensions
  1126. register_extension_wrapper(memnew(OpenXRFBUpdateSwapchainExtension(p_rendering_driver)));
  1127. register_extension_wrapper(memnew(OpenXRFBFoveationExtension(p_rendering_driver)));
  1128. // initialize
  1129. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  1130. wrapper->on_before_instance_created();
  1131. }
  1132. if (!load_layer_properties()) {
  1133. destroy_instance();
  1134. return false;
  1135. }
  1136. if (!load_supported_extensions()) {
  1137. destroy_instance();
  1138. return false;
  1139. }
  1140. if (!create_instance()) {
  1141. destroy_instance();
  1142. return false;
  1143. }
  1144. if (!resolve_instance_openxr_symbols()) {
  1145. destroy_instance();
  1146. return false;
  1147. }
  1148. if (!get_system_info()) {
  1149. destroy_instance();
  1150. return false;
  1151. }
  1152. if (!load_supported_view_configuration_types()) {
  1153. destroy_instance();
  1154. return false;
  1155. }
  1156. if (!load_supported_view_configuration_views(view_configuration)) {
  1157. destroy_instance();
  1158. return false;
  1159. }
  1160. if (!load_supported_environmental_blend_modes()) {
  1161. destroy_instance();
  1162. return false;
  1163. }
  1164. return true;
  1165. }
  1166. bool OpenXRAPI::initialize_session() {
  1167. if (!create_session()) {
  1168. destroy_session();
  1169. return false;
  1170. }
  1171. if (!load_supported_reference_spaces()) {
  1172. destroy_session();
  1173. return false;
  1174. }
  1175. if (!setup_play_space()) {
  1176. destroy_session();
  1177. return false;
  1178. }
  1179. if (!setup_view_space()) {
  1180. destroy_session();
  1181. return false;
  1182. }
  1183. if (!load_supported_swapchain_formats()) {
  1184. destroy_session();
  1185. return false;
  1186. }
  1187. return true;
  1188. }
  1189. void OpenXRAPI::finish() {
  1190. destroy_session();
  1191. destroy_instance();
  1192. }
  1193. void OpenXRAPI::set_xr_interface(OpenXRInterface *p_xr_interface) {
  1194. xr_interface = p_xr_interface;
  1195. }
  1196. void OpenXRAPI::register_extension_wrapper(OpenXRExtensionWrapper *p_extension_wrapper) {
  1197. registered_extension_wrappers.push_back(p_extension_wrapper);
  1198. }
  1199. void OpenXRAPI::unregister_extension_wrapper(OpenXRExtensionWrapper *p_extension_wrapper) {
  1200. registered_extension_wrappers.erase(p_extension_wrapper);
  1201. }
  1202. const Vector<OpenXRExtensionWrapper *> &OpenXRAPI::get_registered_extension_wrappers() {
  1203. return registered_extension_wrappers;
  1204. }
  1205. void OpenXRAPI::register_extension_metadata() {
  1206. for (OpenXRExtensionWrapper *extension_wrapper : registered_extension_wrappers) {
  1207. extension_wrapper->on_register_metadata();
  1208. }
  1209. }
  1210. void OpenXRAPI::cleanup_extension_wrappers() {
  1211. for (OpenXRExtensionWrapper *extension_wrapper : registered_extension_wrappers) {
  1212. // Fix crash when the extension wrapper comes from GDExtension.
  1213. OpenXRExtensionWrapperExtension *gdextension_extension_wrapper = dynamic_cast<OpenXRExtensionWrapperExtension *>(extension_wrapper);
  1214. if (gdextension_extension_wrapper) {
  1215. memdelete(gdextension_extension_wrapper);
  1216. } else {
  1217. memdelete(extension_wrapper);
  1218. }
  1219. }
  1220. registered_extension_wrappers.clear();
  1221. }
  1222. XrHandTrackerEXT OpenXRAPI::get_hand_tracker(int p_hand_index) {
  1223. ERR_FAIL_INDEX_V(p_hand_index, OpenXRHandTrackingExtension::HandTrackedHands::OPENXR_MAX_TRACKED_HANDS, XR_NULL_HANDLE);
  1224. OpenXRHandTrackingExtension::HandTrackedHands hand = static_cast<OpenXRHandTrackingExtension::HandTrackedHands>(p_hand_index);
  1225. return OpenXRHandTrackingExtension::get_singleton()->get_hand_tracker(hand)->hand_tracker;
  1226. }
  1227. Size2 OpenXRAPI::get_recommended_target_size() {
  1228. ERR_FAIL_NULL_V(view_configuration_views, Size2());
  1229. Size2 target_size;
  1230. target_size.width = view_configuration_views[0].recommendedImageRectWidth * render_target_size_multiplier;
  1231. target_size.height = view_configuration_views[0].recommendedImageRectHeight * render_target_size_multiplier;
  1232. return target_size;
  1233. }
  1234. XRPose::TrackingConfidence OpenXRAPI::get_head_center(Transform3D &r_transform, Vector3 &r_linear_velocity, Vector3 &r_angular_velocity) {
  1235. XrResult result;
  1236. if (!running) {
  1237. return XRPose::XR_TRACKING_CONFIDENCE_NONE;
  1238. }
  1239. // xrWaitFrame not run yet
  1240. if (frame_state.predictedDisplayTime == 0) {
  1241. return XRPose::XR_TRACKING_CONFIDENCE_NONE;
  1242. }
  1243. // Get timing for the next frame, as that is the current frame we're processing
  1244. XrTime display_time = get_next_frame_time();
  1245. XrSpaceVelocity velocity = {
  1246. XR_TYPE_SPACE_VELOCITY, // type
  1247. nullptr, // next
  1248. 0, // velocityFlags
  1249. { 0.0, 0.0, 0.0 }, // linearVelocity
  1250. { 0.0, 0.0, 0.0 } // angularVelocity
  1251. };
  1252. XrSpaceLocation location = {
  1253. XR_TYPE_SPACE_LOCATION, // type
  1254. &velocity, // next
  1255. 0, // locationFlags
  1256. {
  1257. { 0.0, 0.0, 0.0, 0.0 }, // orientation
  1258. { 0.0, 0.0, 0.0 } // position
  1259. } // pose
  1260. };
  1261. result = xrLocateSpace(view_space, play_space, display_time, &location);
  1262. if (XR_FAILED(result)) {
  1263. print_line("OpenXR: Failed to locate view space in play space [", get_error_string(result), "]");
  1264. return XRPose::XR_TRACKING_CONFIDENCE_NONE;
  1265. }
  1266. XRPose::TrackingConfidence confidence = transform_from_location(location, r_transform);
  1267. parse_velocities(velocity, r_linear_velocity, r_angular_velocity);
  1268. if (head_pose_confidence != confidence) {
  1269. // prevent error spam
  1270. head_pose_confidence = confidence;
  1271. if (head_pose_confidence == XRPose::XR_TRACKING_CONFIDENCE_NONE) {
  1272. print_line("OpenXR head space location not valid (check tracking?)");
  1273. } else if (head_pose_confidence == XRPose::XR_TRACKING_CONFIDENCE_LOW) {
  1274. print_verbose("OpenVR Head pose now tracking with low confidence");
  1275. } else {
  1276. print_verbose("OpenVR Head pose now tracking with high confidence");
  1277. }
  1278. }
  1279. return confidence;
  1280. }
  1281. bool OpenXRAPI::get_view_transform(uint32_t p_view, Transform3D &r_transform) {
  1282. if (!running) {
  1283. return false;
  1284. }
  1285. // xrWaitFrame not run yet
  1286. if (frame_state.predictedDisplayTime == 0) {
  1287. return false;
  1288. }
  1289. // we don't have valid view info
  1290. if (views == nullptr || !view_pose_valid) {
  1291. return false;
  1292. }
  1293. // Note, the timing of this is set right before rendering, which is what we need here.
  1294. r_transform = transform_from_pose(views[p_view].pose);
  1295. return true;
  1296. }
  1297. bool OpenXRAPI::get_view_projection(uint32_t p_view, double p_z_near, double p_z_far, Projection &p_camera_matrix) {
  1298. ERR_FAIL_NULL_V(graphics_extension, false);
  1299. if (!running) {
  1300. return false;
  1301. }
  1302. // xrWaitFrame not run yet
  1303. if (frame_state.predictedDisplayTime == 0) {
  1304. return false;
  1305. }
  1306. // we don't have valid view info
  1307. if (views == nullptr || !view_pose_valid) {
  1308. return false;
  1309. }
  1310. // if we're using depth views, make sure we update our near and far there...
  1311. if (depth_views != nullptr) {
  1312. for (uint32_t i = 0; i < view_count; i++) {
  1313. depth_views[i].nearZ = p_z_near;
  1314. depth_views[i].farZ = p_z_far;
  1315. }
  1316. }
  1317. // now update our projection
  1318. return graphics_extension->create_projection_fov(views[p_view].fov, p_z_near, p_z_far, p_camera_matrix);
  1319. }
  1320. bool OpenXRAPI::poll_events() {
  1321. ERR_FAIL_COND_V(instance == XR_NULL_HANDLE, false);
  1322. XrEventDataBuffer runtimeEvent;
  1323. runtimeEvent.type = XR_TYPE_EVENT_DATA_BUFFER;
  1324. runtimeEvent.next = nullptr;
  1325. // runtimeEvent.varying = ...
  1326. XrResult pollResult = xrPollEvent(instance, &runtimeEvent);
  1327. while (pollResult == XR_SUCCESS) {
  1328. bool handled = false;
  1329. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  1330. handled |= wrapper->on_event_polled(runtimeEvent);
  1331. }
  1332. switch (runtimeEvent.type) {
  1333. case XR_TYPE_EVENT_DATA_EVENTS_LOST: {
  1334. XrEventDataEventsLost *event = (XrEventDataEventsLost *)&runtimeEvent;
  1335. // We probably didn't poll fast enough, just output warning
  1336. WARN_PRINT("OpenXR EVENT: " + itos(event->lostEventCount) + " event data lost!");
  1337. } break;
  1338. case XR_TYPE_EVENT_DATA_VISIBILITY_MASK_CHANGED_KHR: {
  1339. // XrEventDataVisibilityMaskChangedKHR *event = (XrEventDataVisibilityMaskChangedKHR *)&runtimeEvent;
  1340. // TODO implement this in the future, we should call xrGetVisibilityMaskKHR to obtain a mask,
  1341. // this will allow us to prevent rendering the part of our view which is never displayed giving us
  1342. // a decent performance improvement.
  1343. print_verbose("OpenXR EVENT: STUB: visibility mask changed");
  1344. } break;
  1345. case XR_TYPE_EVENT_DATA_INSTANCE_LOSS_PENDING: {
  1346. XrEventDataInstanceLossPending *event = (XrEventDataInstanceLossPending *)&runtimeEvent;
  1347. // TODO We get this event if we're about to loose our OpenXR instance.
  1348. // We should queue exiting Godot at this point.
  1349. print_verbose(String("OpenXR EVENT: instance loss pending at ") + itos(event->lossTime));
  1350. return false;
  1351. } break;
  1352. case XR_TYPE_EVENT_DATA_SESSION_STATE_CHANGED: {
  1353. XrEventDataSessionStateChanged *event = (XrEventDataSessionStateChanged *)&runtimeEvent;
  1354. session_state = event->state;
  1355. if (session_state >= XR_SESSION_STATE_MAX_ENUM) {
  1356. print_verbose(String("OpenXR EVENT: session state changed to UNKNOWN - ") + itos(session_state));
  1357. } else {
  1358. print_verbose(String("OpenXR EVENT: session state changed to ") + OpenXRUtil::get_session_state_name(session_state));
  1359. switch (session_state) {
  1360. case XR_SESSION_STATE_IDLE:
  1361. on_state_idle();
  1362. break;
  1363. case XR_SESSION_STATE_READY:
  1364. on_state_ready();
  1365. break;
  1366. case XR_SESSION_STATE_SYNCHRONIZED:
  1367. on_state_synchronized();
  1368. break;
  1369. case XR_SESSION_STATE_VISIBLE:
  1370. on_state_visible();
  1371. break;
  1372. case XR_SESSION_STATE_FOCUSED:
  1373. on_state_focused();
  1374. break;
  1375. case XR_SESSION_STATE_STOPPING:
  1376. on_state_stopping();
  1377. break;
  1378. case XR_SESSION_STATE_LOSS_PENDING:
  1379. on_state_loss_pending();
  1380. break;
  1381. case XR_SESSION_STATE_EXITING:
  1382. on_state_exiting();
  1383. break;
  1384. default:
  1385. break;
  1386. }
  1387. }
  1388. } break;
  1389. case XR_TYPE_EVENT_DATA_REFERENCE_SPACE_CHANGE_PENDING: {
  1390. XrEventDataReferenceSpaceChangePending *event = (XrEventDataReferenceSpaceChangePending *)&runtimeEvent;
  1391. print_verbose(String("OpenXR EVENT: reference space type ") + OpenXRUtil::get_reference_space_name(event->referenceSpaceType) + " change pending!");
  1392. if (emulating_local_floor) {
  1393. should_reset_emulated_floor_height = true;
  1394. }
  1395. if (event->poseValid && xr_interface) {
  1396. xr_interface->on_pose_recentered();
  1397. }
  1398. } break;
  1399. case XR_TYPE_EVENT_DATA_INTERACTION_PROFILE_CHANGED: {
  1400. print_verbose("OpenXR EVENT: interaction profile changed!");
  1401. XrEventDataInteractionProfileChanged *event = (XrEventDataInteractionProfileChanged *)&runtimeEvent;
  1402. List<RID> trackers;
  1403. tracker_owner.get_owned_list(&trackers);
  1404. for (int i = 0; i < trackers.size(); i++) {
  1405. tracker_check_profile(trackers[i], event->session);
  1406. }
  1407. } break;
  1408. default:
  1409. if (!handled) {
  1410. print_verbose(String("OpenXR Unhandled event type ") + OpenXRUtil::get_structure_type_name(runtimeEvent.type));
  1411. }
  1412. break;
  1413. }
  1414. runtimeEvent.type = XR_TYPE_EVENT_DATA_BUFFER;
  1415. pollResult = xrPollEvent(instance, &runtimeEvent);
  1416. }
  1417. if (pollResult == XR_EVENT_UNAVAILABLE) {
  1418. // processed all events in the queue
  1419. return true;
  1420. } else {
  1421. ERR_FAIL_V_MSG(false, "OpenXR: Failed to poll events!");
  1422. }
  1423. }
  1424. bool OpenXRAPI::process() {
  1425. ERR_FAIL_COND_V(instance == XR_NULL_HANDLE, false);
  1426. if (!poll_events()) {
  1427. return false;
  1428. }
  1429. if (!running) {
  1430. return false;
  1431. }
  1432. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  1433. wrapper->on_process();
  1434. }
  1435. return true;
  1436. }
  1437. void OpenXRAPI::free_swapchain(OpenXRSwapChainInfo &p_swapchain) {
  1438. if (p_swapchain.image_acquired) {
  1439. release_image(p_swapchain);
  1440. }
  1441. if (graphics_extension && p_swapchain.swapchain_graphics_data != nullptr) {
  1442. graphics_extension->cleanup_swapchain_graphics_data(&p_swapchain.swapchain_graphics_data);
  1443. }
  1444. if (p_swapchain.swapchain != XR_NULL_HANDLE) {
  1445. xrDestroySwapchain(p_swapchain.swapchain);
  1446. p_swapchain.swapchain = XR_NULL_HANDLE;
  1447. }
  1448. }
  1449. bool OpenXRAPI::acquire_image(OpenXRSwapChainInfo &p_swapchain) {
  1450. ERR_FAIL_COND_V(p_swapchain.image_acquired, true); // This was not released when it should be, error out and reuse...
  1451. XrResult result;
  1452. if (!p_swapchain.skip_acquire_swapchain) {
  1453. XrSwapchainImageAcquireInfo swapchain_image_acquire_info = {
  1454. XR_TYPE_SWAPCHAIN_IMAGE_ACQUIRE_INFO, // type
  1455. nullptr // next
  1456. };
  1457. result = xrAcquireSwapchainImage(p_swapchain.swapchain, &swapchain_image_acquire_info, &p_swapchain.image_index);
  1458. if (!XR_UNQUALIFIED_SUCCESS(result)) {
  1459. // Make sure end_frame knows we need to submit an empty frame
  1460. frame_state.shouldRender = false;
  1461. if (XR_FAILED(result)) {
  1462. // Unexpected failure, log this!
  1463. print_line("OpenXR: failed to acquire swapchain image [", get_error_string(result), "]");
  1464. return false;
  1465. } else {
  1466. // In this scenario we silently fail, the XR runtime is simply not ready yet to acquire the swapchain.
  1467. return false;
  1468. }
  1469. }
  1470. }
  1471. XrSwapchainImageWaitInfo swapchain_image_wait_info = {
  1472. XR_TYPE_SWAPCHAIN_IMAGE_WAIT_INFO, // type
  1473. nullptr, // next
  1474. 17000000 // timeout in nanoseconds
  1475. };
  1476. result = xrWaitSwapchainImage(p_swapchain.swapchain, &swapchain_image_wait_info);
  1477. if (!XR_UNQUALIFIED_SUCCESS(result)) {
  1478. // Make sure end_frame knows we need to submit an empty frame
  1479. frame_state.shouldRender = false;
  1480. if (XR_FAILED(result)) {
  1481. // Unexpected failure, log this!
  1482. print_line("OpenXR: failed to wait for swapchain image [", get_error_string(result), "]");
  1483. return false;
  1484. } else {
  1485. // Make sure to skip trying to acquire the swapchain image in the next frame
  1486. p_swapchain.skip_acquire_swapchain = true;
  1487. return false;
  1488. }
  1489. } else {
  1490. p_swapchain.skip_acquire_swapchain = false;
  1491. }
  1492. p_swapchain.image_acquired = true;
  1493. return true;
  1494. }
  1495. RID OpenXRAPI::get_image(OpenXRSwapChainInfo &p_swapchain) {
  1496. if (p_swapchain.image_acquired) {
  1497. return graphics_extension->get_texture(p_swapchain.swapchain_graphics_data, p_swapchain.image_index);
  1498. } else {
  1499. return RID();
  1500. }
  1501. }
  1502. bool OpenXRAPI::release_image(OpenXRSwapChainInfo &p_swapchain) {
  1503. if (!p_swapchain.image_acquired) {
  1504. // Already released or never acquired.
  1505. return true;
  1506. }
  1507. p_swapchain.image_acquired = false; // Regardless if we succeed or not, consider this released.
  1508. XrSwapchainImageReleaseInfo swapchain_image_release_info = {
  1509. XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO, // type
  1510. nullptr // next
  1511. };
  1512. XrResult result = xrReleaseSwapchainImage(p_swapchain.swapchain, &swapchain_image_release_info);
  1513. if (XR_FAILED(result)) {
  1514. print_line("OpenXR: failed to release swapchain image! [", get_error_string(result), "]");
  1515. return false;
  1516. }
  1517. return true;
  1518. }
  1519. void OpenXRAPI::pre_render() {
  1520. ERR_FAIL_COND(instance == XR_NULL_HANDLE);
  1521. if (!running) {
  1522. return;
  1523. }
  1524. // Waitframe does 2 important things in our process:
  1525. // 1) It provides us with predictive timing, telling us when OpenXR expects to display the frame we're about to commit
  1526. // 2) It will use the previous timing to pause our thread so that rendering starts as close to displaying as possible
  1527. // This must thus be called as close to when we start rendering as possible
  1528. XrFrameWaitInfo frame_wait_info = { XR_TYPE_FRAME_WAIT_INFO, nullptr };
  1529. frame_state.predictedDisplayTime = 0;
  1530. frame_state.predictedDisplayPeriod = 0;
  1531. frame_state.shouldRender = false;
  1532. XrResult result = xrWaitFrame(session, &frame_wait_info, &frame_state);
  1533. if (XR_FAILED(result)) {
  1534. print_line("OpenXR: xrWaitFrame() was not successful [", get_error_string(result), "]");
  1535. // reset just in case
  1536. frame_state.predictedDisplayTime = 0;
  1537. frame_state.predictedDisplayPeriod = 0;
  1538. frame_state.shouldRender = false;
  1539. return;
  1540. }
  1541. if (frame_state.predictedDisplayPeriod > 500000000) {
  1542. // display period more then 0.5 seconds? must be wrong data
  1543. print_verbose(String("OpenXR resetting invalid display period ") + rtos(frame_state.predictedDisplayPeriod));
  1544. frame_state.predictedDisplayPeriod = 0;
  1545. }
  1546. if (unlikely(should_reset_emulated_floor_height)) {
  1547. reset_emulated_floor_height();
  1548. should_reset_emulated_floor_height = false;
  1549. }
  1550. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  1551. wrapper->on_pre_render();
  1552. }
  1553. // Get our view info for the frame we're about to render, note from the OpenXR manual:
  1554. // "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"
  1555. // We're calling this "relatively" early, the positioning we're obtaining here will be used to do our frustum culling,
  1556. // occlusion culling, etc. There is however a technique that we can investigate in the future where after our entire
  1557. // Vulkan command buffer is build, but right before vkSubmitQueue is called, we call xrLocateViews one more time and
  1558. // update the view and projection matrix once more with a slightly more accurate predication and then submit the
  1559. // command queues.
  1560. // That is not possible yet but worth investigating in the future.
  1561. XrViewLocateInfo view_locate_info = {
  1562. XR_TYPE_VIEW_LOCATE_INFO, // type
  1563. nullptr, // next
  1564. view_configuration, // viewConfigurationType
  1565. frame_state.predictedDisplayTime, // displayTime
  1566. play_space // space
  1567. };
  1568. XrViewState view_state = {
  1569. XR_TYPE_VIEW_STATE, // type
  1570. nullptr, // next
  1571. 0 // viewStateFlags
  1572. };
  1573. uint32_t view_count_output;
  1574. result = xrLocateViews(session, &view_locate_info, &view_state, view_count, &view_count_output, views);
  1575. if (XR_FAILED(result)) {
  1576. print_line("OpenXR: Couldn't locate views [", get_error_string(result), "]");
  1577. return;
  1578. }
  1579. bool pose_valid = true;
  1580. for (uint64_t i = 0; i < view_count_output; i++) {
  1581. if ((view_state.viewStateFlags & XR_VIEW_STATE_ORIENTATION_VALID_BIT) == 0 ||
  1582. (view_state.viewStateFlags & XR_VIEW_STATE_POSITION_VALID_BIT) == 0) {
  1583. pose_valid = false;
  1584. }
  1585. }
  1586. if (view_pose_valid != pose_valid) {
  1587. view_pose_valid = pose_valid;
  1588. if (!view_pose_valid) {
  1589. print_verbose("OpenXR View pose became invalid");
  1590. } else {
  1591. print_verbose("OpenXR View pose became valid");
  1592. }
  1593. }
  1594. // let's start our frame..
  1595. XrFrameBeginInfo frame_begin_info = {
  1596. XR_TYPE_FRAME_BEGIN_INFO, // type
  1597. nullptr // next
  1598. };
  1599. result = xrBeginFrame(session, &frame_begin_info);
  1600. if (XR_FAILED(result)) {
  1601. print_line("OpenXR: failed to being frame [", get_error_string(result), "]");
  1602. return;
  1603. }
  1604. }
  1605. bool OpenXRAPI::pre_draw_viewport(RID p_render_target) {
  1606. if (!can_render()) {
  1607. return false;
  1608. }
  1609. // TODO: at some point in time we may support multiple viewports in which case we need to handle that...
  1610. // Acquire our images
  1611. for (int i = 0; i < OPENXR_SWAPCHAIN_MAX; i++) {
  1612. if (!swapchains[i].image_acquired && swapchains[i].swapchain != XR_NULL_HANDLE) {
  1613. if (!acquire_image(swapchains[i])) {
  1614. return false;
  1615. }
  1616. }
  1617. }
  1618. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  1619. wrapper->on_pre_draw_viewport(p_render_target);
  1620. }
  1621. return true;
  1622. }
  1623. XrSwapchain OpenXRAPI::get_color_swapchain() {
  1624. return swapchains[OPENXR_SWAPCHAIN_COLOR].swapchain;
  1625. }
  1626. RID OpenXRAPI::get_color_texture() {
  1627. return get_image(swapchains[OPENXR_SWAPCHAIN_COLOR]);
  1628. }
  1629. RID OpenXRAPI::get_depth_texture() {
  1630. // Note, image will not be acquired if we didn't have a suitable swap chain format.
  1631. if (submit_depth_buffer) {
  1632. return get_image(swapchains[OPENXR_SWAPCHAIN_DEPTH]);
  1633. } else {
  1634. return RID();
  1635. }
  1636. }
  1637. void OpenXRAPI::post_draw_viewport(RID p_render_target) {
  1638. if (!can_render()) {
  1639. return;
  1640. }
  1641. for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {
  1642. wrapper->on_post_draw_viewport(p_render_target);
  1643. }
  1644. };
  1645. void OpenXRAPI::end_frame() {
  1646. XrResult result;
  1647. ERR_FAIL_COND(instance == XR_NULL_HANDLE);
  1648. if (!running) {
  1649. return;
  1650. }
  1651. if (frame_state.shouldRender && view_pose_valid && !swapchains[OPENXR_SWAPCHAIN_COLOR].image_acquired) {
  1652. print_line("OpenXR: No viewport was marked with use_xr, there is no rendered output!");
  1653. }
  1654. // must have:
  1655. // - shouldRender set to true
  1656. // - a valid view pose for projection_views[eye].pose to submit layer
  1657. // - an image to render
  1658. if (!frame_state.shouldRender || !view_pose_valid || !swapchains[OPENXR_SWAPCHAIN_COLOR].image_acquired) {
  1659. // submit 0 layers when we shouldn't render
  1660. XrFrameEndInfo frame_end_info = {
  1661. XR_TYPE_FRAME_END_INFO, // type
  1662. nullptr, // next
  1663. frame_state.predictedDisplayTime, // displayTime
  1664. environment_blend_mode, // environmentBlendMode
  1665. 0, // layerCount
  1666. nullptr // layers
  1667. };
  1668. result = xrEndFrame(session, &frame_end_info);
  1669. if (XR_FAILED(result)) {
  1670. print_line("OpenXR: failed to end frame! [", get_error_string(result), "]");
  1671. return;
  1672. }
  1673. // neither eye is rendered
  1674. return;
  1675. }
  1676. // release our swapchain image if we acquired it
  1677. for (int i = 0; i < OPENXR_SWAPCHAIN_MAX; i++) {
  1678. if (swapchains[i].image_acquired) {
  1679. release_image(swapchains[i]);
  1680. }
  1681. }
  1682. for (uint32_t eye = 0; eye < view_count; eye++) {
  1683. projection_views[eye].fov = views[eye].fov;
  1684. projection_views[eye].pose = views[eye].pose;
  1685. }
  1686. Vector<OrderedCompositionLayer> ordered_layers_list;
  1687. bool projection_layer_is_first = true;
  1688. // Add composition layers from providers
  1689. for (OpenXRCompositionLayerProvider *provider : composition_layer_providers) {
  1690. for (int i = 0; i < provider->get_composition_layer_count(); i++) {
  1691. OrderedCompositionLayer layer = {
  1692. provider->get_composition_layer(i),
  1693. provider->get_composition_layer_order(i),
  1694. };
  1695. if (layer.composition_layer) {
  1696. ordered_layers_list.push_back(layer);
  1697. if (layer.sort_order == 0) {
  1698. WARN_PRINT_ONCE_ED("Composition layer returned sort order 0, it may be overwritten by projection layer.");
  1699. } else if (layer.sort_order < 0) {
  1700. projection_layer_is_first = false;
  1701. }
  1702. }
  1703. }
  1704. }
  1705. XrCompositionLayerFlags layer_flags = XR_COMPOSITION_LAYER_CORRECT_CHROMATIC_ABERRATION_BIT;
  1706. if (!projection_layer_is_first || environment_blend_mode != XR_ENVIRONMENT_BLEND_MODE_OPAQUE) {
  1707. layer_flags |= XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT;
  1708. }
  1709. XrCompositionLayerProjection projection_layer = {
  1710. XR_TYPE_COMPOSITION_LAYER_PROJECTION, // type
  1711. nullptr, // next
  1712. layer_flags, // layerFlags
  1713. play_space, // space
  1714. view_count, // viewCount
  1715. projection_views, // views
  1716. };
  1717. ordered_layers_list.push_back({ (const XrCompositionLayerBaseHeader *)&projection_layer, 0 });
  1718. // Sort our layers.
  1719. ordered_layers_list.sort_custom<OrderedCompositionLayer>();
  1720. // Now make a list we can pass on to OpenXR.
  1721. Vector<const XrCompositionLayerBaseHeader *> layers_list;
  1722. for (OrderedCompositionLayer &ordered_layer : ordered_layers_list) {
  1723. layers_list.push_back(ordered_layer.composition_layer);
  1724. }
  1725. XrFrameEndInfo frame_end_info = {
  1726. XR_TYPE_FRAME_END_INFO, // type
  1727. nullptr, // next
  1728. frame_state.predictedDisplayTime, // displayTime
  1729. environment_blend_mode, // environmentBlendMode
  1730. static_cast<uint32_t>(layers_list.size()), // layerCount
  1731. layers_list.ptr() // layers
  1732. };
  1733. result = xrEndFrame(session, &frame_end_info);
  1734. if (XR_FAILED(result)) {
  1735. print_line("OpenXR: failed to end frame! [", get_error_string(result), "]");
  1736. return;
  1737. }
  1738. }
  1739. float OpenXRAPI::get_display_refresh_rate() const {
  1740. OpenXRDisplayRefreshRateExtension *drrext = OpenXRDisplayRefreshRateExtension::get_singleton();
  1741. if (drrext) {
  1742. return drrext->get_refresh_rate();
  1743. }
  1744. return 0.0;
  1745. }
  1746. void OpenXRAPI::set_display_refresh_rate(float p_refresh_rate) {
  1747. OpenXRDisplayRefreshRateExtension *drrext = OpenXRDisplayRefreshRateExtension::get_singleton();
  1748. if (drrext != nullptr) {
  1749. drrext->set_refresh_rate(p_refresh_rate);
  1750. }
  1751. }
  1752. Array OpenXRAPI::get_available_display_refresh_rates() const {
  1753. OpenXRDisplayRefreshRateExtension *drrext = OpenXRDisplayRefreshRateExtension::get_singleton();
  1754. if (drrext != nullptr) {
  1755. return drrext->get_available_refresh_rates();
  1756. }
  1757. return Array();
  1758. }
  1759. double OpenXRAPI::get_render_target_size_multiplier() const {
  1760. return render_target_size_multiplier;
  1761. }
  1762. void OpenXRAPI::set_render_target_size_multiplier(double multiplier) {
  1763. render_target_size_multiplier = multiplier;
  1764. }
  1765. bool OpenXRAPI::is_foveation_supported() const {
  1766. OpenXRFBFoveationExtension *fov_ext = OpenXRFBFoveationExtension::get_singleton();
  1767. return fov_ext != nullptr && fov_ext->is_enabled();
  1768. }
  1769. int OpenXRAPI::get_foveation_level() const {
  1770. OpenXRFBFoveationExtension *fov_ext = OpenXRFBFoveationExtension::get_singleton();
  1771. if (fov_ext != nullptr && fov_ext->is_enabled()) {
  1772. switch (fov_ext->get_foveation_level()) {
  1773. case XR_FOVEATION_LEVEL_NONE_FB:
  1774. return 0;
  1775. case XR_FOVEATION_LEVEL_LOW_FB:
  1776. return 1;
  1777. case XR_FOVEATION_LEVEL_MEDIUM_FB:
  1778. return 2;
  1779. case XR_FOVEATION_LEVEL_HIGH_FB:
  1780. return 3;
  1781. default:
  1782. return 0;
  1783. }
  1784. }
  1785. return 0;
  1786. }
  1787. void OpenXRAPI::set_foveation_level(int p_foveation_level) {
  1788. ERR_FAIL_UNSIGNED_INDEX(p_foveation_level, 4);
  1789. OpenXRFBFoveationExtension *fov_ext = OpenXRFBFoveationExtension::get_singleton();
  1790. if (fov_ext != nullptr && fov_ext->is_enabled()) {
  1791. XrFoveationLevelFB levels[] = { XR_FOVEATION_LEVEL_NONE_FB, XR_FOVEATION_LEVEL_LOW_FB, XR_FOVEATION_LEVEL_MEDIUM_FB, XR_FOVEATION_LEVEL_HIGH_FB };
  1792. fov_ext->set_foveation_level(levels[p_foveation_level]);
  1793. }
  1794. }
  1795. bool OpenXRAPI::get_foveation_dynamic() const {
  1796. OpenXRFBFoveationExtension *fov_ext = OpenXRFBFoveationExtension::get_singleton();
  1797. if (fov_ext != nullptr && fov_ext->is_enabled()) {
  1798. return fov_ext->get_foveation_dynamic() == XR_FOVEATION_DYNAMIC_LEVEL_ENABLED_FB;
  1799. }
  1800. return false;
  1801. }
  1802. void OpenXRAPI::set_foveation_dynamic(bool p_foveation_dynamic) {
  1803. OpenXRFBFoveationExtension *fov_ext = OpenXRFBFoveationExtension::get_singleton();
  1804. if (fov_ext != nullptr && fov_ext->is_enabled()) {
  1805. fov_ext->set_foveation_dynamic(p_foveation_dynamic ? XR_FOVEATION_DYNAMIC_LEVEL_ENABLED_FB : XR_FOVEATION_DYNAMIC_DISABLED_FB);
  1806. }
  1807. }
  1808. Size2 OpenXRAPI::get_play_space_bounds() const {
  1809. Size2 ret;
  1810. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, Size2());
  1811. XrExtent2Df extents;
  1812. XrResult result = xrGetReferenceSpaceBoundsRect(session, reference_space, &extents);
  1813. if (XR_FAILED(result)) {
  1814. print_line("OpenXR: failed to get play space bounds! [", get_error_string(result), "]");
  1815. return ret;
  1816. }
  1817. ret.width = extents.width;
  1818. ret.height = extents.height;
  1819. return ret;
  1820. }
  1821. OpenXRAPI::OpenXRAPI() {
  1822. // OpenXRAPI is only constructed if OpenXR is enabled.
  1823. singleton = this;
  1824. if (Engine::get_singleton()->is_editor_hint()) {
  1825. // Enabled OpenXR in the editor? Adjust our settings for the editor
  1826. } else {
  1827. // Load settings from project settings
  1828. int form_factor_setting = GLOBAL_GET("xr/openxr/form_factor");
  1829. switch (form_factor_setting) {
  1830. case 0: {
  1831. form_factor = XR_FORM_FACTOR_HEAD_MOUNTED_DISPLAY;
  1832. } break;
  1833. case 1: {
  1834. form_factor = XR_FORM_FACTOR_HANDHELD_DISPLAY;
  1835. } break;
  1836. default:
  1837. break;
  1838. }
  1839. int view_configuration_setting = GLOBAL_GET("xr/openxr/view_configuration");
  1840. switch (view_configuration_setting) {
  1841. case 0: {
  1842. view_configuration = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_MONO;
  1843. } break;
  1844. case 1: {
  1845. view_configuration = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO;
  1846. } break;
  1847. /* we don't support quad and observer configurations (yet)
  1848. case 2: {
  1849. view_configuration = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_QUAD_VARJO;
  1850. } break;
  1851. case 3: {
  1852. view_configuration = XR_VIEW_CONFIGURATION_TYPE_SECONDARY_MONO_FIRST_PERSON_OBSERVER_MSFT;
  1853. } break;
  1854. */
  1855. default:
  1856. break;
  1857. }
  1858. int reference_space_setting = GLOBAL_GET("xr/openxr/reference_space");
  1859. switch (reference_space_setting) {
  1860. case 0: {
  1861. requested_reference_space = XR_REFERENCE_SPACE_TYPE_LOCAL;
  1862. } break;
  1863. case 1: {
  1864. requested_reference_space = XR_REFERENCE_SPACE_TYPE_STAGE;
  1865. } break;
  1866. case 2: {
  1867. requested_reference_space = XR_REFERENCE_SPACE_TYPE_LOCAL_FLOOR_EXT;
  1868. } break;
  1869. default:
  1870. break;
  1871. }
  1872. int environment_blend_mode_setting = GLOBAL_GET("xr/openxr/environment_blend_mode");
  1873. switch (environment_blend_mode_setting) {
  1874. case 0: {
  1875. environment_blend_mode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE;
  1876. } break;
  1877. case 1: {
  1878. environment_blend_mode = XR_ENVIRONMENT_BLEND_MODE_ADDITIVE;
  1879. } break;
  1880. case 2: {
  1881. environment_blend_mode = XR_ENVIRONMENT_BLEND_MODE_ALPHA_BLEND;
  1882. } break;
  1883. default:
  1884. break;
  1885. }
  1886. submit_depth_buffer = GLOBAL_GET("xr/openxr/submit_depth_buffer");
  1887. }
  1888. // reset a few things that can't be done in our class definition
  1889. frame_state.predictedDisplayTime = 0;
  1890. frame_state.predictedDisplayPeriod = 0;
  1891. }
  1892. OpenXRAPI::~OpenXRAPI() {
  1893. // cleanup our composition layer providers
  1894. for (OpenXRCompositionLayerProvider *provider : composition_layer_providers) {
  1895. memdelete(provider);
  1896. }
  1897. composition_layer_providers.clear();
  1898. if (supported_extensions != nullptr) {
  1899. memfree(supported_extensions);
  1900. supported_extensions = nullptr;
  1901. }
  1902. if (layer_properties != nullptr) {
  1903. memfree(layer_properties);
  1904. layer_properties = nullptr;
  1905. }
  1906. #ifdef ANDROID_ENABLED
  1907. if (openxr_loader_library_handle) {
  1908. OS::get_singleton()->close_dynamic_library(openxr_loader_library_handle);
  1909. openxr_loader_library_handle = nullptr;
  1910. }
  1911. #endif
  1912. singleton = nullptr;
  1913. }
  1914. Transform3D OpenXRAPI::transform_from_pose(const XrPosef &p_pose) {
  1915. Quaternion q(p_pose.orientation.x, p_pose.orientation.y, p_pose.orientation.z, p_pose.orientation.w);
  1916. Basis basis(q);
  1917. Vector3 origin(p_pose.position.x, p_pose.position.y, p_pose.position.z);
  1918. return Transform3D(basis, origin);
  1919. }
  1920. template <typename T>
  1921. XRPose::TrackingConfidence _transform_from_location(const T &p_location, Transform3D &r_transform) {
  1922. Basis basis;
  1923. Vector3 origin;
  1924. XRPose::TrackingConfidence confidence = XRPose::XR_TRACKING_CONFIDENCE_NONE;
  1925. const XrPosef &pose = p_location.pose;
  1926. // Check orientation
  1927. if (p_location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_VALID_BIT) {
  1928. Quaternion q(pose.orientation.x, pose.orientation.y, pose.orientation.z, pose.orientation.w);
  1929. r_transform.basis = Basis(q);
  1930. if (p_location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_TRACKED_BIT) {
  1931. // Fully valid orientation, so either 3DOF or 6DOF tracking with high confidence so default to HIGH_TRACKING
  1932. confidence = XRPose::XR_TRACKING_CONFIDENCE_HIGH;
  1933. } else {
  1934. // Orientation is being tracked but we're using old/predicted data, so low tracking confidence
  1935. confidence = XRPose::XR_TRACKING_CONFIDENCE_LOW;
  1936. }
  1937. } else {
  1938. r_transform.basis = Basis();
  1939. }
  1940. // Check location
  1941. if (p_location.locationFlags & XR_SPACE_LOCATION_POSITION_VALID_BIT) {
  1942. r_transform.origin = Vector3(pose.position.x, pose.position.y, pose.position.z);
  1943. if (!(p_location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_TRACKED_BIT)) {
  1944. // Location is being tracked but we're using old/predicted data, so low tracking confidence
  1945. confidence = XRPose::XR_TRACKING_CONFIDENCE_LOW;
  1946. } else if (confidence == XRPose::XR_TRACKING_CONFIDENCE_NONE) {
  1947. // Position tracking without orientation tracking?
  1948. confidence = XRPose::XR_TRACKING_CONFIDENCE_HIGH;
  1949. }
  1950. } else {
  1951. // No tracking or 3DOF I guess..
  1952. r_transform.origin = Vector3();
  1953. }
  1954. return confidence;
  1955. }
  1956. XRPose::TrackingConfidence OpenXRAPI::transform_from_location(const XrSpaceLocation &p_location, Transform3D &r_transform) {
  1957. return _transform_from_location(p_location, r_transform);
  1958. }
  1959. XRPose::TrackingConfidence OpenXRAPI::transform_from_location(const XrHandJointLocationEXT &p_location, Transform3D &r_transform) {
  1960. return _transform_from_location(p_location, r_transform);
  1961. }
  1962. void OpenXRAPI::parse_velocities(const XrSpaceVelocity &p_velocity, Vector3 &r_linear_velocity, Vector3 &r_angular_velocity) {
  1963. if (p_velocity.velocityFlags & XR_SPACE_VELOCITY_LINEAR_VALID_BIT) {
  1964. XrVector3f linear_velocity = p_velocity.linearVelocity;
  1965. r_linear_velocity = Vector3(linear_velocity.x, linear_velocity.y, linear_velocity.z);
  1966. } else {
  1967. r_linear_velocity = Vector3();
  1968. }
  1969. if (p_velocity.velocityFlags & XR_SPACE_VELOCITY_ANGULAR_VALID_BIT) {
  1970. XrVector3f angular_velocity = p_velocity.angularVelocity;
  1971. r_angular_velocity = Vector3(angular_velocity.x, angular_velocity.y, angular_velocity.z);
  1972. } else {
  1973. r_angular_velocity = Vector3();
  1974. }
  1975. }
  1976. bool OpenXRAPI::xr_result(XrResult result, const char *format, Array args) const {
  1977. if (XR_SUCCEEDED(result))
  1978. return true;
  1979. char resultString[XR_MAX_RESULT_STRING_SIZE];
  1980. xrResultToString(instance, result, resultString);
  1981. print_error(String("OpenXR ") + String(format).format(args) + String(" [") + String(resultString) + String("]"));
  1982. return false;
  1983. }
  1984. RID OpenXRAPI::get_tracker_rid(XrPath p_path) {
  1985. List<RID> current;
  1986. tracker_owner.get_owned_list(&current);
  1987. for (int i = 0; i < current.size(); i++) {
  1988. Tracker *tracker = tracker_owner.get_or_null(current[i]);
  1989. if (tracker && tracker->toplevel_path == p_path) {
  1990. return current[i];
  1991. }
  1992. }
  1993. return RID();
  1994. }
  1995. RID OpenXRAPI::tracker_create(const String p_name) {
  1996. ERR_FAIL_COND_V(instance == XR_NULL_HANDLE, RID());
  1997. Tracker new_tracker;
  1998. new_tracker.name = p_name;
  1999. new_tracker.toplevel_path = XR_NULL_PATH;
  2000. new_tracker.active_profile_rid = RID();
  2001. XrResult result = xrStringToPath(instance, p_name.utf8().get_data(), &new_tracker.toplevel_path);
  2002. if (XR_FAILED(result)) {
  2003. print_line("OpenXR: failed to get path for ", p_name, "! [", get_error_string(result), "]");
  2004. return RID();
  2005. }
  2006. return tracker_owner.make_rid(new_tracker);
  2007. }
  2008. String OpenXRAPI::tracker_get_name(RID p_tracker) {
  2009. if (p_tracker.is_null()) {
  2010. return String("None");
  2011. }
  2012. Tracker *tracker = tracker_owner.get_or_null(p_tracker);
  2013. ERR_FAIL_NULL_V(tracker, String());
  2014. return tracker->name;
  2015. }
  2016. void OpenXRAPI::tracker_check_profile(RID p_tracker, XrSession p_session) {
  2017. if (p_session == XR_NULL_HANDLE) {
  2018. p_session = session;
  2019. }
  2020. Tracker *tracker = tracker_owner.get_or_null(p_tracker);
  2021. ERR_FAIL_NULL(tracker);
  2022. if (tracker->toplevel_path == XR_NULL_PATH) {
  2023. // no path, how was this even created?
  2024. return;
  2025. }
  2026. XrInteractionProfileState profile_state = {
  2027. XR_TYPE_INTERACTION_PROFILE_STATE, // type
  2028. nullptr, // next
  2029. XR_NULL_PATH // interactionProfile
  2030. };
  2031. XrResult result = xrGetCurrentInteractionProfile(p_session, tracker->toplevel_path, &profile_state);
  2032. if (XR_FAILED(result)) {
  2033. print_line("OpenXR: Failed to get interaction profile for", itos(tracker->toplevel_path), "[", get_error_string(result), "]");
  2034. return;
  2035. }
  2036. XrPath new_profile = profile_state.interactionProfile;
  2037. XrPath was_profile = get_interaction_profile_path(tracker->active_profile_rid);
  2038. if (was_profile != new_profile) {
  2039. tracker->active_profile_rid = get_interaction_profile_rid(new_profile);
  2040. if (xr_interface) {
  2041. xr_interface->tracker_profile_changed(p_tracker, tracker->active_profile_rid);
  2042. }
  2043. }
  2044. }
  2045. void OpenXRAPI::tracker_free(RID p_tracker) {
  2046. Tracker *tracker = tracker_owner.get_or_null(p_tracker);
  2047. ERR_FAIL_NULL(tracker);
  2048. // there is nothing to free here
  2049. tracker_owner.free(p_tracker);
  2050. }
  2051. RID OpenXRAPI::action_set_create(const String p_name, const String p_localized_name, const int p_priority) {
  2052. ERR_FAIL_COND_V(instance == XR_NULL_HANDLE, RID());
  2053. ActionSet action_set;
  2054. action_set.name = p_name;
  2055. action_set.is_attached = false;
  2056. // create our action set...
  2057. XrActionSetCreateInfo action_set_info = {
  2058. XR_TYPE_ACTION_SET_CREATE_INFO, // type
  2059. nullptr, // next
  2060. "", // actionSetName
  2061. "", // localizedActionSetName
  2062. uint32_t(p_priority) // priority
  2063. };
  2064. copy_string_to_char_buffer(p_name, action_set_info.actionSetName, XR_MAX_ACTION_SET_NAME_SIZE);
  2065. copy_string_to_char_buffer(p_localized_name, action_set_info.localizedActionSetName, XR_MAX_LOCALIZED_ACTION_SET_NAME_SIZE);
  2066. XrResult result = xrCreateActionSet(instance, &action_set_info, &action_set.handle);
  2067. if (XR_FAILED(result)) {
  2068. print_line("OpenXR: failed to create action set ", p_name, "! [", get_error_string(result), "]");
  2069. return RID();
  2070. }
  2071. return action_set_owner.make_rid(action_set);
  2072. }
  2073. String OpenXRAPI::action_set_get_name(RID p_action_set) {
  2074. if (p_action_set.is_null()) {
  2075. return String("None");
  2076. }
  2077. ActionSet *action_set = action_set_owner.get_or_null(p_action_set);
  2078. ERR_FAIL_NULL_V(action_set, String());
  2079. return action_set->name;
  2080. }
  2081. bool OpenXRAPI::attach_action_sets(const Vector<RID> &p_action_sets) {
  2082. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, false);
  2083. Vector<XrActionSet> action_handles;
  2084. action_handles.resize(p_action_sets.size());
  2085. for (int i = 0; i < p_action_sets.size(); i++) {
  2086. ActionSet *action_set = action_set_owner.get_or_null(p_action_sets[i]);
  2087. ERR_FAIL_NULL_V(action_set, false);
  2088. if (action_set->is_attached) {
  2089. return false;
  2090. }
  2091. action_handles.set(i, action_set->handle);
  2092. }
  2093. // So according to the docs, once we attach our action set to our session it becomes read only..
  2094. // https://www.khronos.org/registry/OpenXR/specs/1.0/man/html/xrAttachSessionActionSets.html
  2095. XrSessionActionSetsAttachInfo attach_info = {
  2096. XR_TYPE_SESSION_ACTION_SETS_ATTACH_INFO, // type
  2097. nullptr, // next
  2098. (uint32_t)p_action_sets.size(), // countActionSets,
  2099. action_handles.ptr() // actionSets
  2100. };
  2101. XrResult result = xrAttachSessionActionSets(session, &attach_info);
  2102. if (XR_FAILED(result)) {
  2103. print_line("OpenXR: failed to attach action sets! [", get_error_string(result), "]");
  2104. return false;
  2105. }
  2106. for (int i = 0; i < p_action_sets.size(); i++) {
  2107. ActionSet *action_set = action_set_owner.get_or_null(p_action_sets[i]);
  2108. ERR_FAIL_NULL_V(action_set, false);
  2109. action_set->is_attached = true;
  2110. }
  2111. /* For debugging:
  2112. print_verbose("Attached set " + action_set->name);
  2113. List<RID> action_rids;
  2114. action_owner.get_owned_list(&action_rids);
  2115. for (int i = 0; i < action_rids.size(); i++) {
  2116. Action * action = action_owner.get_or_null(action_rids[i]);
  2117. if (action && action->action_set_rid == p_action_set) {
  2118. print_verbose(" - Action " + action->name + ": " + OpenXRUtil::get_action_type_name(action->action_type));
  2119. for (int j = 0; j < action->trackers.size(); j++) {
  2120. Tracker * tracker = tracker_owner.get_or_null(action->trackers[j].tracker_rid);
  2121. if (tracker) {
  2122. print_verbose(" - " + tracker->name);
  2123. }
  2124. }
  2125. }
  2126. }
  2127. */
  2128. return true;
  2129. }
  2130. void OpenXRAPI::action_set_free(RID p_action_set) {
  2131. ActionSet *action_set = action_set_owner.get_or_null(p_action_set);
  2132. ERR_FAIL_NULL(action_set);
  2133. if (action_set->handle != XR_NULL_HANDLE) {
  2134. xrDestroyActionSet(action_set->handle);
  2135. }
  2136. action_set_owner.free(p_action_set);
  2137. }
  2138. RID OpenXRAPI::get_action_rid(XrAction p_action) {
  2139. List<RID> current;
  2140. action_owner.get_owned_list(&current);
  2141. for (int i = 0; i < current.size(); i++) {
  2142. Action *action = action_owner.get_or_null(current[i]);
  2143. if (action && action->handle == p_action) {
  2144. return current[i];
  2145. }
  2146. }
  2147. return RID();
  2148. }
  2149. 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) {
  2150. ERR_FAIL_COND_V(instance == XR_NULL_HANDLE, RID());
  2151. Action action;
  2152. action.name = p_name;
  2153. ActionSet *action_set = action_set_owner.get_or_null(p_action_set);
  2154. ERR_FAIL_NULL_V(action_set, RID());
  2155. ERR_FAIL_COND_V(action_set->handle == XR_NULL_HANDLE, RID());
  2156. action.action_set_rid = p_action_set;
  2157. switch (p_action_type) {
  2158. case OpenXRAction::OPENXR_ACTION_BOOL:
  2159. action.action_type = XR_ACTION_TYPE_BOOLEAN_INPUT;
  2160. break;
  2161. case OpenXRAction::OPENXR_ACTION_FLOAT:
  2162. action.action_type = XR_ACTION_TYPE_FLOAT_INPUT;
  2163. break;
  2164. case OpenXRAction::OPENXR_ACTION_VECTOR2:
  2165. action.action_type = XR_ACTION_TYPE_VECTOR2F_INPUT;
  2166. break;
  2167. case OpenXRAction::OPENXR_ACTION_POSE:
  2168. action.action_type = XR_ACTION_TYPE_POSE_INPUT;
  2169. break;
  2170. case OpenXRAction::OPENXR_ACTION_HAPTIC:
  2171. action.action_type = XR_ACTION_TYPE_VIBRATION_OUTPUT;
  2172. break;
  2173. default:
  2174. ERR_FAIL_V(RID());
  2175. break;
  2176. }
  2177. Vector<XrPath> toplevel_paths;
  2178. for (int i = 0; i < p_trackers.size(); i++) {
  2179. Tracker *tracker = tracker_owner.get_or_null(p_trackers[i]);
  2180. if (tracker != nullptr && tracker->toplevel_path != XR_NULL_PATH) {
  2181. ActionTracker action_tracker = {
  2182. p_trackers[i], // tracker
  2183. XR_NULL_HANDLE, // space
  2184. false // was_location_valid
  2185. };
  2186. action.trackers.push_back(action_tracker);
  2187. toplevel_paths.push_back(tracker->toplevel_path);
  2188. }
  2189. }
  2190. XrActionCreateInfo action_info = {
  2191. XR_TYPE_ACTION_CREATE_INFO, // type
  2192. nullptr, // next
  2193. "", // actionName
  2194. action.action_type, // actionType
  2195. uint32_t(toplevel_paths.size()), // countSubactionPaths
  2196. toplevel_paths.ptr(), // subactionPaths
  2197. "" // localizedActionName
  2198. };
  2199. copy_string_to_char_buffer(p_name, action_info.actionName, XR_MAX_ACTION_NAME_SIZE);
  2200. copy_string_to_char_buffer(p_localized_name, action_info.localizedActionName, XR_MAX_LOCALIZED_ACTION_NAME_SIZE);
  2201. XrResult result = xrCreateAction(action_set->handle, &action_info, &action.handle);
  2202. if (XR_FAILED(result)) {
  2203. print_line("OpenXR: failed to create action ", p_name, "! [", get_error_string(result), "]");
  2204. return RID();
  2205. }
  2206. return action_owner.make_rid(action);
  2207. }
  2208. String OpenXRAPI::action_get_name(RID p_action) {
  2209. if (p_action.is_null()) {
  2210. return String("None");
  2211. }
  2212. Action *action = action_owner.get_or_null(p_action);
  2213. ERR_FAIL_NULL_V(action, String());
  2214. return action->name;
  2215. }
  2216. void OpenXRAPI::action_free(RID p_action) {
  2217. Action *action = action_owner.get_or_null(p_action);
  2218. ERR_FAIL_NULL(action);
  2219. if (action->handle != XR_NULL_HANDLE) {
  2220. xrDestroyAction(action->handle);
  2221. }
  2222. action_owner.free(p_action);
  2223. }
  2224. RID OpenXRAPI::get_interaction_profile_rid(XrPath p_path) {
  2225. List<RID> current;
  2226. interaction_profile_owner.get_owned_list(&current);
  2227. for (int i = 0; i < current.size(); i++) {
  2228. InteractionProfile *ip = interaction_profile_owner.get_or_null(current[i]);
  2229. if (ip && ip->path == p_path) {
  2230. return current[i];
  2231. }
  2232. }
  2233. return RID();
  2234. }
  2235. XrPath OpenXRAPI::get_interaction_profile_path(RID p_interaction_profile) {
  2236. if (p_interaction_profile.is_null()) {
  2237. return XR_NULL_PATH;
  2238. }
  2239. InteractionProfile *ip = interaction_profile_owner.get_or_null(p_interaction_profile);
  2240. ERR_FAIL_NULL_V(ip, XR_NULL_PATH);
  2241. return ip->path;
  2242. }
  2243. RID OpenXRAPI::interaction_profile_create(const String p_name) {
  2244. if (!is_interaction_profile_supported(p_name)) {
  2245. // The extension enabling this path must not be active, we will silently skip this interaction profile
  2246. return RID();
  2247. }
  2248. InteractionProfile new_interaction_profile;
  2249. XrResult result = xrStringToPath(instance, p_name.utf8().get_data(), &new_interaction_profile.path);
  2250. if (XR_FAILED(result)) {
  2251. print_line("OpenXR: failed to get path for ", p_name, "! [", get_error_string(result), "]");
  2252. return RID();
  2253. }
  2254. RID existing_ip = get_interaction_profile_rid(new_interaction_profile.path);
  2255. if (existing_ip.is_valid()) {
  2256. return existing_ip;
  2257. }
  2258. new_interaction_profile.name = p_name;
  2259. return interaction_profile_owner.make_rid(new_interaction_profile);
  2260. }
  2261. String OpenXRAPI::interaction_profile_get_name(RID p_interaction_profile) {
  2262. if (p_interaction_profile.is_null()) {
  2263. return String("None");
  2264. }
  2265. InteractionProfile *ip = interaction_profile_owner.get_or_null(p_interaction_profile);
  2266. ERR_FAIL_NULL_V(ip, String());
  2267. return ip->name;
  2268. }
  2269. void OpenXRAPI::interaction_profile_clear_bindings(RID p_interaction_profile) {
  2270. InteractionProfile *ip = interaction_profile_owner.get_or_null(p_interaction_profile);
  2271. ERR_FAIL_NULL(ip);
  2272. ip->bindings.clear();
  2273. }
  2274. bool OpenXRAPI::interaction_profile_add_binding(RID p_interaction_profile, RID p_action, const String p_path) {
  2275. InteractionProfile *ip = interaction_profile_owner.get_or_null(p_interaction_profile);
  2276. ERR_FAIL_NULL_V(ip, false);
  2277. if (!interaction_profile_supports_io_path(ip->name, p_path)) {
  2278. return false;
  2279. }
  2280. XrActionSuggestedBinding binding;
  2281. Action *action = action_owner.get_or_null(p_action);
  2282. ERR_FAIL_COND_V(action == nullptr || action->handle == XR_NULL_HANDLE, false);
  2283. binding.action = action->handle;
  2284. XrResult result = xrStringToPath(instance, p_path.utf8().get_data(), &binding.binding);
  2285. if (XR_FAILED(result)) {
  2286. print_line("OpenXR: failed to get path for ", p_path, "! [", get_error_string(result), "]");
  2287. return false;
  2288. }
  2289. ip->bindings.push_back(binding);
  2290. return true;
  2291. }
  2292. bool OpenXRAPI::interaction_profile_suggest_bindings(RID p_interaction_profile) {
  2293. ERR_FAIL_COND_V(instance == XR_NULL_HANDLE, false);
  2294. InteractionProfile *ip = interaction_profile_owner.get_or_null(p_interaction_profile);
  2295. ERR_FAIL_NULL_V(ip, false);
  2296. const XrInteractionProfileSuggestedBinding suggested_bindings = {
  2297. XR_TYPE_INTERACTION_PROFILE_SUGGESTED_BINDING, // type
  2298. nullptr, // next
  2299. ip->path, // interactionProfile
  2300. uint32_t(ip->bindings.size()), // countSuggestedBindings
  2301. ip->bindings.ptr() // suggestedBindings
  2302. };
  2303. XrResult result = xrSuggestInteractionProfileBindings(instance, &suggested_bindings);
  2304. if (result == XR_ERROR_PATH_UNSUPPORTED) {
  2305. // this is fine, not all runtimes support all devices.
  2306. print_verbose("OpenXR Interaction profile " + ip->name + " is not supported on this runtime");
  2307. } else if (XR_FAILED(result)) {
  2308. print_line("OpenXR: failed to suggest bindings for ", ip->name, "! [", get_error_string(result), "]");
  2309. // reporting is enough...
  2310. }
  2311. /* For debugging:
  2312. print_verbose("Suggested bindings for " + ip->name);
  2313. for (int i = 0; i < ip->bindings.size(); i++) {
  2314. uint32_t strlen;
  2315. char path[XR_MAX_PATH_LENGTH];
  2316. String action_name = action_get_name(get_action_rid(ip->bindings[i].action));
  2317. XrResult result = xrPathToString(instance, ip->bindings[i].binding, XR_MAX_PATH_LENGTH, &strlen, path);
  2318. if (XR_FAILED(result)) {
  2319. print_line("OpenXR: failed to retrieve bindings for ", action_name, "! [", get_error_string(result), "]");
  2320. }
  2321. print_verbose(" - " + action_name + " => " + String(path));
  2322. }
  2323. */
  2324. return true;
  2325. }
  2326. void OpenXRAPI::interaction_profile_free(RID p_interaction_profile) {
  2327. InteractionProfile *ip = interaction_profile_owner.get_or_null(p_interaction_profile);
  2328. ERR_FAIL_NULL(ip);
  2329. ip->bindings.clear();
  2330. interaction_profile_owner.free(p_interaction_profile);
  2331. }
  2332. bool OpenXRAPI::sync_action_sets(const Vector<RID> p_active_sets) {
  2333. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, false);
  2334. if (!running) {
  2335. return false;
  2336. }
  2337. Vector<XrActiveActionSet> active_sets;
  2338. for (int i = 0; i < p_active_sets.size(); i++) {
  2339. ActionSet *action_set = action_set_owner.get_or_null(p_active_sets[i]);
  2340. if (action_set && action_set->handle != XR_NULL_HANDLE) {
  2341. XrActiveActionSet aset;
  2342. aset.actionSet = action_set->handle;
  2343. aset.subactionPath = XR_NULL_PATH;
  2344. active_sets.push_back(aset);
  2345. }
  2346. }
  2347. ERR_FAIL_COND_V(active_sets.is_empty(), false);
  2348. XrActionsSyncInfo sync_info = {
  2349. XR_TYPE_ACTIONS_SYNC_INFO, // type
  2350. nullptr, // next
  2351. uint32_t(active_sets.size()), // countActiveActionSets
  2352. active_sets.ptr() // activeActionSets
  2353. };
  2354. XrResult result = xrSyncActions(session, &sync_info);
  2355. if (XR_FAILED(result)) {
  2356. print_line("OpenXR: failed to sync active action sets! [", get_error_string(result), "]");
  2357. return false;
  2358. }
  2359. return true;
  2360. }
  2361. bool OpenXRAPI::get_action_bool(RID p_action, RID p_tracker) {
  2362. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, false);
  2363. Action *action = action_owner.get_or_null(p_action);
  2364. ERR_FAIL_NULL_V(action, false);
  2365. Tracker *tracker = tracker_owner.get_or_null(p_tracker);
  2366. ERR_FAIL_NULL_V(tracker, false);
  2367. if (!running) {
  2368. return false;
  2369. }
  2370. ERR_FAIL_COND_V(action->action_type != XR_ACTION_TYPE_BOOLEAN_INPUT, false);
  2371. XrActionStateGetInfo get_info = {
  2372. XR_TYPE_ACTION_STATE_GET_INFO, // type
  2373. nullptr, // next
  2374. action->handle, // action
  2375. tracker->toplevel_path // subactionPath
  2376. };
  2377. XrActionStateBoolean result_state;
  2378. result_state.type = XR_TYPE_ACTION_STATE_BOOLEAN,
  2379. result_state.next = nullptr;
  2380. XrResult result = xrGetActionStateBoolean(session, &get_info, &result_state);
  2381. if (XR_FAILED(result)) {
  2382. print_line("OpenXR: couldn't get action boolean! [", get_error_string(result), "]");
  2383. return false;
  2384. }
  2385. return result_state.isActive && result_state.currentState;
  2386. }
  2387. float OpenXRAPI::get_action_float(RID p_action, RID p_tracker) {
  2388. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, 0.0);
  2389. Action *action = action_owner.get_or_null(p_action);
  2390. ERR_FAIL_NULL_V(action, 0.0);
  2391. Tracker *tracker = tracker_owner.get_or_null(p_tracker);
  2392. ERR_FAIL_NULL_V(tracker, 0.0);
  2393. if (!running) {
  2394. return 0.0;
  2395. }
  2396. ERR_FAIL_COND_V(action->action_type != XR_ACTION_TYPE_FLOAT_INPUT, 0.0);
  2397. XrActionStateGetInfo get_info = {
  2398. XR_TYPE_ACTION_STATE_GET_INFO, // type
  2399. nullptr, // next
  2400. action->handle, // action
  2401. tracker->toplevel_path // subactionPath
  2402. };
  2403. XrActionStateFloat result_state;
  2404. result_state.type = XR_TYPE_ACTION_STATE_FLOAT,
  2405. result_state.next = nullptr;
  2406. XrResult result = xrGetActionStateFloat(session, &get_info, &result_state);
  2407. if (XR_FAILED(result)) {
  2408. print_line("OpenXR: couldn't get action float! [", get_error_string(result), "]");
  2409. return 0.0;
  2410. }
  2411. return result_state.isActive ? result_state.currentState : 0.0;
  2412. }
  2413. Vector2 OpenXRAPI::get_action_vector2(RID p_action, RID p_tracker) {
  2414. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, Vector2());
  2415. Action *action = action_owner.get_or_null(p_action);
  2416. ERR_FAIL_NULL_V(action, Vector2());
  2417. Tracker *tracker = tracker_owner.get_or_null(p_tracker);
  2418. ERR_FAIL_NULL_V(tracker, Vector2());
  2419. if (!running) {
  2420. return Vector2();
  2421. }
  2422. ERR_FAIL_COND_V(action->action_type != XR_ACTION_TYPE_VECTOR2F_INPUT, Vector2());
  2423. XrActionStateGetInfo get_info = {
  2424. XR_TYPE_ACTION_STATE_GET_INFO, // type
  2425. nullptr, // next
  2426. action->handle, // action
  2427. tracker->toplevel_path // subactionPath
  2428. };
  2429. XrActionStateVector2f result_state;
  2430. result_state.type = XR_TYPE_ACTION_STATE_VECTOR2F,
  2431. result_state.next = nullptr;
  2432. XrResult result = xrGetActionStateVector2f(session, &get_info, &result_state);
  2433. if (XR_FAILED(result)) {
  2434. print_line("OpenXR: couldn't get action vector2! [", get_error_string(result), "]");
  2435. return Vector2();
  2436. }
  2437. return result_state.isActive ? Vector2(result_state.currentState.x, result_state.currentState.y) : Vector2();
  2438. }
  2439. XRPose::TrackingConfidence OpenXRAPI::get_action_pose(RID p_action, RID p_tracker, Transform3D &r_transform, Vector3 &r_linear_velocity, Vector3 &r_angular_velocity) {
  2440. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, XRPose::XR_TRACKING_CONFIDENCE_NONE);
  2441. Action *action = action_owner.get_or_null(p_action);
  2442. ERR_FAIL_NULL_V(action, XRPose::XR_TRACKING_CONFIDENCE_NONE);
  2443. Tracker *tracker = tracker_owner.get_or_null(p_tracker);
  2444. ERR_FAIL_NULL_V(tracker, XRPose::XR_TRACKING_CONFIDENCE_NONE);
  2445. if (!running) {
  2446. return XRPose::XR_TRACKING_CONFIDENCE_NONE;
  2447. }
  2448. ERR_FAIL_COND_V(action->action_type != XR_ACTION_TYPE_POSE_INPUT, XRPose::XR_TRACKING_CONFIDENCE_NONE);
  2449. // print_verbose("Checking " + action->name + " => " + tracker->name + " (" + itos(tracker->toplevel_path) + ")");
  2450. uint64_t index = 0xFFFFFFFF;
  2451. uint64_t size = uint64_t(action->trackers.size());
  2452. for (uint64_t i = 0; i < size && index == 0xFFFFFFFF; i++) {
  2453. if (action->trackers[i].tracker_rid == p_tracker) {
  2454. index = i;
  2455. }
  2456. }
  2457. if (index == 0xFFFFFFFF) {
  2458. // couldn't find it?
  2459. return XRPose::XR_TRACKING_CONFIDENCE_NONE;
  2460. }
  2461. XrTime display_time = get_next_frame_time();
  2462. if (display_time == 0) {
  2463. return XRPose::XR_TRACKING_CONFIDENCE_NONE;
  2464. }
  2465. if (action->trackers[index].space == XR_NULL_HANDLE) {
  2466. // if this is a pose we need to define spaces
  2467. XrActionSpaceCreateInfo action_space_info = {
  2468. XR_TYPE_ACTION_SPACE_CREATE_INFO, // type
  2469. nullptr, // next
  2470. action->handle, // action
  2471. tracker->toplevel_path, // subactionPath
  2472. {
  2473. { 0.0, 0.0, 0.0, 1.0 }, // orientation
  2474. { 0.0, 0.0, 0.0 } // position
  2475. } // poseInActionSpace
  2476. };
  2477. XrSpace space;
  2478. XrResult result = xrCreateActionSpace(session, &action_space_info, &space);
  2479. if (XR_FAILED(result)) {
  2480. print_line("OpenXR: couldn't create action space! [", get_error_string(result), "]");
  2481. return XRPose::XR_TRACKING_CONFIDENCE_NONE;
  2482. }
  2483. action->trackers.ptrw()[index].space = space;
  2484. }
  2485. XrSpaceVelocity velocity = {
  2486. XR_TYPE_SPACE_VELOCITY, // type
  2487. nullptr, // next
  2488. 0, // velocityFlags
  2489. { 0.0, 0.0, 0.0 }, // linearVelocity
  2490. { 0.0, 0.0, 0.0 } // angularVelocity
  2491. };
  2492. XrSpaceLocation location = {
  2493. XR_TYPE_SPACE_LOCATION, // type
  2494. &velocity, // next
  2495. 0, // locationFlags
  2496. {
  2497. { 0.0, 0.0, 0.0, 0.0 }, // orientation
  2498. { 0.0, 0.0, 0.0 } // position
  2499. } // pose
  2500. };
  2501. XrResult result = xrLocateSpace(action->trackers[index].space, play_space, display_time, &location);
  2502. if (XR_FAILED(result)) {
  2503. print_line("OpenXR: failed to locate space! [", get_error_string(result), "]");
  2504. return XRPose::XR_TRACKING_CONFIDENCE_NONE;
  2505. }
  2506. XRPose::TrackingConfidence confidence = transform_from_location(location, r_transform);
  2507. parse_velocities(velocity, r_linear_velocity, r_angular_velocity);
  2508. return confidence;
  2509. }
  2510. bool OpenXRAPI::trigger_haptic_pulse(RID p_action, RID p_tracker, float p_frequency, float p_amplitude, XrDuration p_duration_ns) {
  2511. ERR_FAIL_COND_V(session == XR_NULL_HANDLE, false);
  2512. Action *action = action_owner.get_or_null(p_action);
  2513. ERR_FAIL_NULL_V(action, false);
  2514. Tracker *tracker = tracker_owner.get_or_null(p_tracker);
  2515. ERR_FAIL_NULL_V(tracker, false);
  2516. if (!running) {
  2517. return false;
  2518. }
  2519. ERR_FAIL_COND_V(action->action_type != XR_ACTION_TYPE_VIBRATION_OUTPUT, false);
  2520. XrHapticActionInfo action_info = {
  2521. XR_TYPE_HAPTIC_ACTION_INFO, // type
  2522. nullptr, // next
  2523. action->handle, // action
  2524. tracker->toplevel_path // subactionPath
  2525. };
  2526. XrHapticVibration vibration = {
  2527. XR_TYPE_HAPTIC_VIBRATION, // type
  2528. nullptr, // next
  2529. p_duration_ns, // duration
  2530. p_frequency, // frequency
  2531. p_amplitude, // amplitude
  2532. };
  2533. XrResult result = xrApplyHapticFeedback(session, &action_info, (const XrHapticBaseHeader *)&vibration);
  2534. if (XR_FAILED(result)) {
  2535. print_line("OpenXR: failed to apply haptic feedback! [", get_error_string(result), "]");
  2536. return false;
  2537. }
  2538. return true;
  2539. }
  2540. void OpenXRAPI::register_composition_layer_provider(OpenXRCompositionLayerProvider *provider) {
  2541. composition_layer_providers.append(provider);
  2542. }
  2543. void OpenXRAPI::unregister_composition_layer_provider(OpenXRCompositionLayerProvider *provider) {
  2544. composition_layer_providers.erase(provider);
  2545. }
  2546. const XrEnvironmentBlendMode *OpenXRAPI::get_supported_environment_blend_modes(uint32_t &count) {
  2547. count = num_supported_environment_blend_modes;
  2548. return supported_environment_blend_modes;
  2549. }
  2550. bool OpenXRAPI::is_environment_blend_mode_supported(XrEnvironmentBlendMode p_blend_mode) const {
  2551. ERR_FAIL_NULL_V(supported_environment_blend_modes, false);
  2552. for (uint32_t i = 0; i < num_supported_environment_blend_modes; i++) {
  2553. if (supported_environment_blend_modes[i] == p_blend_mode) {
  2554. return true;
  2555. }
  2556. }
  2557. return false;
  2558. }
  2559. bool OpenXRAPI::set_environment_blend_mode(XrEnvironmentBlendMode p_blend_mode) {
  2560. if (emulate_environment_blend_mode_alpha_blend && p_blend_mode == XR_ENVIRONMENT_BLEND_MODE_ALPHA_BLEND) {
  2561. requested_environment_blend_mode = XR_ENVIRONMENT_BLEND_MODE_ALPHA_BLEND;
  2562. environment_blend_mode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE;
  2563. return true;
  2564. }
  2565. // We allow setting this when not initialized and will check if it is supported when initializing.
  2566. // After OpenXR is initialized we verify we're setting a supported blend mode.
  2567. else if (!is_initialized() || is_environment_blend_mode_supported(p_blend_mode)) {
  2568. requested_environment_blend_mode = p_blend_mode;
  2569. environment_blend_mode = p_blend_mode;
  2570. return true;
  2571. }
  2572. return false;
  2573. }
  2574. void OpenXRAPI::set_emulate_environment_blend_mode_alpha_blend(bool p_enabled) {
  2575. emulate_environment_blend_mode_alpha_blend = p_enabled;
  2576. }
  2577. OpenXRAPI::OpenXRAlphaBlendModeSupport OpenXRAPI::is_environment_blend_mode_alpha_blend_supported() {
  2578. if (is_environment_blend_mode_supported(XR_ENVIRONMENT_BLEND_MODE_ALPHA_BLEND)) {
  2579. return OPENXR_ALPHA_BLEND_MODE_SUPPORT_REAL;
  2580. } else if (emulate_environment_blend_mode_alpha_blend) {
  2581. return OPENXR_ALPHA_BLEND_MODE_SUPPORT_EMULATING;
  2582. }
  2583. return OPENXR_ALPHA_BLEND_MODE_SUPPORT_NONE;
  2584. }