OpenXRVkSession.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <OpenXRVk/OpenXRVkSession.h>
  9. #include <OpenXRVk/OpenXRVkDevice.h>
  10. #include <OpenXRVk/OpenXRVkInput.h>
  11. #include <OpenXRVk/OpenXRVkInstance.h>
  12. #include <OpenXRVk/OpenXRVkSpace.h>
  13. #include <OpenXRVk/OpenXRVkUtils.h>
  14. #include <AzCore/Debug/Trace.h>
  15. #include <AzCore/Casting/numeric_cast.h>
  16. #include <Atom/RHI.Reflect/Vulkan/XRVkDescriptors.h>
  17. #include <XR/XRBase.h>
  18. namespace OpenXRVk
  19. {
  20. XR::Ptr<Session> Session::Create()
  21. {
  22. return aznew Session;
  23. }
  24. AZ::RHI::ResultCode Session::InitInternal([[maybe_unused]] AZ::RHI::XRSessionDescriptor* descriptor)
  25. {
  26. Instance* xrVkInstance = static_cast<Instance*>(GetDescriptor().m_instance.get());
  27. Device* xrVkDevice = static_cast<Device*>(GetDescriptor().m_device.get());
  28. auto graphicBinding = xrVkDevice->GetGraphicsBinding(AZ::RHI::HardwareQueueClass::Graphics);
  29. m_xrInstance = xrVkInstance->GetXRInstance();
  30. AZ_Printf("OpenXRVk", "Creating session...\n");
  31. m_graphicsBinding.instance = xrVkInstance->GetNativeInstance();
  32. m_graphicsBinding.physicalDevice = xrVkDevice->GetNativePhysicalDevice();
  33. m_graphicsBinding.device = xrVkDevice->GetNativeDevice();
  34. m_graphicsBinding.queueFamilyIndex = graphicBinding.m_queueFamilyIndex;
  35. m_graphicsBinding.queueIndex = graphicBinding.m_queueIndex;
  36. AZ_Assert(m_xrInstance != XR_NULL_HANDLE, "XR instance is null.");
  37. AZ_Assert(m_session == XR_NULL_HANDLE, "XR session is already initialized.");
  38. XrSessionCreateInfo createInfo{ XR_TYPE_SESSION_CREATE_INFO };
  39. createInfo.next = reinterpret_cast<const XrBaseInStructure*>(&m_graphicsBinding);
  40. createInfo.systemId = xrVkInstance->GetXRSystemId();
  41. XrResult result = xrCreateSession(m_xrInstance, &createInfo, &m_session);
  42. ASSERT_IF_UNSUCCESSFUL(result);
  43. LogReferenceSpaces();
  44. Input* xrVkInput = GetNativeInput();
  45. xrVkInput->InitializeActionSpace(m_session);
  46. xrVkInput->InitializeActionSets(m_session);
  47. Space* xrVkSpace = static_cast<Space*>(GetSpace());
  48. xrVkSpace->CreateVisualizedSpaces(m_session);
  49. return ConvertResult(result);
  50. }
  51. void Session::LogReferenceSpaces()
  52. {
  53. if (GetDescriptor().m_validationMode == AZ::RHI::ValidationMode::Enabled)
  54. {
  55. AZ_Warning("OpenXrVK", m_session != XR_NULL_HANDLE, "Session is not initialized");
  56. if (m_session == XR_NULL_HANDLE)
  57. {
  58. return;
  59. }
  60. uint32_t spaceCount = 0;
  61. XrResult result = xrEnumerateReferenceSpaces(m_session, 0, &spaceCount, nullptr);
  62. WARN_IF_UNSUCCESSFUL(result);
  63. AZStd::vector<XrReferenceSpaceType> spaces(spaceCount);
  64. result = xrEnumerateReferenceSpaces(m_session, spaceCount, &spaceCount, spaces.data());
  65. AZ_Printf("OpenXRVk", "Available reference spaces: %d\n", spaceCount);
  66. for (XrReferenceSpaceType space : spaces)
  67. {
  68. AZ_Printf("OpenXRVk", " Name: %s\n", to_string(space));
  69. }
  70. }
  71. }
  72. void Session::HandleSessionStateChangedEvent(const XrEventDataSessionStateChanged& stateChangedEvent)
  73. {
  74. Instance* xrVkInstance = static_cast<Instance*>(GetDescriptor().m_instance.get());
  75. const XrSessionState oldState = m_sessionState;
  76. m_sessionState = stateChangedEvent.state;
  77. if (GetDescriptor().m_validationMode == AZ::RHI::ValidationMode::Enabled)
  78. {
  79. AZ_Printf(
  80. "OpenXRVk",
  81. "XrEventDataSessionStateChanged: state %s->%s session=%lld time=%lld\n", to_string(oldState), to_string(m_sessionState),
  82. stateChangedEvent.session, stateChangedEvent.time);
  83. }
  84. if ((stateChangedEvent.session != XR_NULL_HANDLE) && (stateChangedEvent.session != m_session))
  85. {
  86. AZ_Printf("OpenXRVk", "XrEventDataSessionStateChanged for unknown session\n");
  87. return;
  88. }
  89. switch (m_sessionState)
  90. {
  91. case XR_SESSION_STATE_READY:
  92. {
  93. AZ_Assert(m_session != XR_NULL_HANDLE, "Session is null");
  94. XrSessionBeginInfo sessionBeginInfo{ XR_TYPE_SESSION_BEGIN_INFO };
  95. sessionBeginInfo.primaryViewConfigurationType = xrVkInstance->GetViewConfigType();
  96. XrResult result = xrBeginSession(m_session, &sessionBeginInfo);
  97. WARN_IF_UNSUCCESSFUL(result);
  98. m_sessionRunning = true;
  99. break;
  100. }
  101. case XR_SESSION_STATE_STOPPING:
  102. {
  103. AZ_Assert(m_session != XR_NULL_HANDLE, "Session is null");
  104. m_sessionRunning = false;
  105. XrResult result = xrEndSession(m_session);
  106. WARN_IF_UNSUCCESSFUL(result);
  107. break;
  108. }
  109. case XR_SESSION_STATE_EXITING:
  110. {
  111. m_exitRenderLoop = true;
  112. // Do not attempt to restart because user closed this session.
  113. m_requestRestart = false;
  114. break;
  115. }
  116. case XR_SESSION_STATE_LOSS_PENDING:
  117. {
  118. m_exitRenderLoop = true;
  119. // Poll for a new instance.
  120. m_requestRestart = true;
  121. break;
  122. }
  123. default:
  124. {
  125. break;
  126. }
  127. }
  128. }
  129. const XrEventDataBaseHeader* Session::TryReadNextEvent()
  130. {
  131. XrEventDataBaseHeader* baseHeader = reinterpret_cast<XrEventDataBaseHeader*>(&m_eventDataBuffer);
  132. *baseHeader = { XR_TYPE_EVENT_DATA_BUFFER };
  133. const XrResult result = xrPollEvent(m_xrInstance, &m_eventDataBuffer);
  134. if (result == XR_SUCCESS)
  135. {
  136. if (baseHeader->type == XR_TYPE_EVENT_DATA_EVENTS_LOST)
  137. {
  138. [[maybe_unused]] const XrEventDataEventsLost* const eventsLost = reinterpret_cast<const XrEventDataEventsLost*>(baseHeader);
  139. if (GetDescriptor().m_validationMode == AZ::RHI::ValidationMode::Enabled)
  140. {
  141. AZ_Printf("OpenXrVK", "%d events lost\n", eventsLost->lostEventCount);
  142. }
  143. }
  144. return baseHeader;
  145. }
  146. if (result == XR_EVENT_UNAVAILABLE)
  147. {
  148. return nullptr;
  149. }
  150. WARN_IF_UNSUCCESSFUL(result);
  151. return nullptr;
  152. }
  153. void Session::PollEvents()
  154. {
  155. m_exitRenderLoop = m_requestRestart = false;
  156. // Process all pending messages.
  157. while (const XrEventDataBaseHeader* event = TryReadNextEvent())
  158. {
  159. switch (event->type)
  160. {
  161. case XR_TYPE_EVENT_DATA_INSTANCE_LOSS_PENDING:
  162. {
  163. if (GetDescriptor().m_validationMode == AZ::RHI::ValidationMode::Enabled)
  164. {
  165. [[maybe_unused]] const auto& instanceLossPending = *reinterpret_cast<const XrEventDataInstanceLossPending*>(event);
  166. AZ_Printf("OpenXRVk", "XrEventDataInstanceLossPending by %lld\n", instanceLossPending.lossTime);
  167. }
  168. m_exitRenderLoop = true;
  169. m_requestRestart = true;
  170. return;
  171. }
  172. case XR_TYPE_EVENT_DATA_SESSION_STATE_CHANGED:
  173. {
  174. auto sessionStateChangedEvent = *reinterpret_cast<const XrEventDataSessionStateChanged*>(event);
  175. HandleSessionStateChangedEvent(sessionStateChangedEvent);
  176. break;
  177. }
  178. case XR_TYPE_EVENT_DATA_INTERACTION_PROFILE_CHANGED:
  179. {
  180. if (GetDescriptor().m_validationMode == AZ::RHI::ValidationMode::Enabled)
  181. {
  182. Input* xrVkInput = GetNativeInput();
  183. LogActionSourceName(xrVkInput->GetSqueezeAction(static_cast<AZ::u32>(XR::Side::Left)), "Squeeze Left");
  184. LogActionSourceName(xrVkInput->GetSqueezeAction(static_cast<AZ::u32>(XR::Side::Right)), "Squeeze Right");
  185. LogActionSourceName(xrVkInput->GetQuitAction(), "Quit");
  186. LogActionSourceName(xrVkInput->GetPoseAction(static_cast<AZ::u32>(XR::Side::Left)), "Pose Left");
  187. LogActionSourceName(xrVkInput->GetPoseAction(static_cast<AZ::u32>(XR::Side::Right)), "Pose Right");
  188. LogActionSourceName(xrVkInput->GetVibrationAction(), "Vibrate");
  189. }
  190. break;
  191. }
  192. case XR_TYPE_EVENT_DATA_REFERENCE_SPACE_CHANGE_PENDING:
  193. [[fallthrough]];
  194. default:
  195. {
  196. if (GetDescriptor().m_validationMode == AZ::RHI::ValidationMode::Enabled)
  197. {
  198. AZ_Printf("OpenXRVk", "Ignoring event type %d\n", event->type);
  199. }
  200. break;
  201. }
  202. }
  203. }
  204. }
  205. void Session::LogActionSourceName(XrAction action, const AZStd::string_view actionName) const
  206. {
  207. XrBoundSourcesForActionEnumerateInfo getInfo = { XR_TYPE_BOUND_SOURCES_FOR_ACTION_ENUMERATE_INFO };
  208. getInfo.action = action;
  209. uint32_t pathCount = 0;
  210. XrResult result = xrEnumerateBoundSourcesForAction(m_session, &getInfo, 0, &pathCount, nullptr);
  211. WARN_IF_UNSUCCESSFUL(result);
  212. AZStd::vector<XrPath> paths(pathCount);
  213. result = xrEnumerateBoundSourcesForAction(m_session, &getInfo, aznumeric_cast<uint32_t>(paths.size()), &pathCount, paths.data());
  214. WARN_IF_UNSUCCESSFUL(result);
  215. AZStd::string sourceName;
  216. for (uint32_t i = 0; i < pathCount; ++i)
  217. {
  218. constexpr XrInputSourceLocalizedNameFlags all = XR_INPUT_SOURCE_LOCALIZED_NAME_USER_PATH_BIT |
  219. XR_INPUT_SOURCE_LOCALIZED_NAME_INTERACTION_PROFILE_BIT | XR_INPUT_SOURCE_LOCALIZED_NAME_COMPONENT_BIT;
  220. XrInputSourceLocalizedNameGetInfo nameInfo = { XR_TYPE_INPUT_SOURCE_LOCALIZED_NAME_GET_INFO };
  221. nameInfo.sourcePath = paths[i];
  222. nameInfo.whichComponents = all;
  223. uint32_t size = 0;
  224. result = xrGetInputSourceLocalizedName(m_session, &nameInfo, 0, &size, nullptr);
  225. WARN_IF_UNSUCCESSFUL(result);
  226. if (size < 1)
  227. {
  228. continue;
  229. }
  230. AZStd::vector<char> grabSource(size);
  231. result = xrGetInputSourceLocalizedName(m_session, &nameInfo, uint32_t(grabSource.size()), &size, grabSource.data());
  232. WARN_IF_UNSUCCESSFUL(result);
  233. if (!sourceName.empty())
  234. {
  235. sourceName += " and ";
  236. }
  237. sourceName += "'";
  238. sourceName += AZStd::string(grabSource.data(), size - 1);
  239. sourceName += "'";
  240. }
  241. AZ_Printf("OpenXrVK",
  242. "%s action is bound to %s\n", actionName.data(), ((!sourceName.empty()) ? sourceName.c_str() : "nothing"));
  243. }
  244. void Session::LocateControllerSpace(AZ::u32 handIndex)
  245. {
  246. Input* xrInput = GetNativeInput();
  247. Device* device = static_cast<Device*>(GetDescriptor().m_device.get());
  248. Space* space = static_cast<Space*>(GetSpace());
  249. xrInput->LocateControllerSpace(device->GetPredictedDisplayTime(), space->GetXrSpace(OpenXRVk::SpaceType::View), handIndex);
  250. }
  251. AZ::RHI::ResultCode Session::GetControllerPose(AZ::u32 handIndex, AZ::RPI::PoseData& outPoseData) const
  252. {
  253. return GetNativeInput()->GetControllerPose(handIndex, outPoseData);
  254. }
  255. AZ::RHI::ResultCode Session::GetControllerStagePose(AZ::u32 handIndex, AZ::RPI::PoseData& outPoseData) const
  256. {
  257. Input* xrInput = GetNativeInput();
  258. return handIndex == 0 ? xrInput->GetVisualizedSpacePose(OpenXRVk::SpaceType::StageLeft, outPoseData) :
  259. xrInput->GetVisualizedSpacePose(OpenXRVk::SpaceType::StageRight, outPoseData);
  260. }
  261. AZ::RHI::ResultCode Session::GetViewFrontPose(AZ::RPI::PoseData& outPoseData) const
  262. {
  263. return GetNativeInput()->GetVisualizedSpacePose(OpenXRVk::SpaceType::ViewFront, outPoseData);
  264. }
  265. AZ::RHI::ResultCode Session::GetViewLocalPose(AZ::RPI::PoseData& outPoseData) const
  266. {
  267. return GetNativeInput()->GetVisualizedSpacePose(OpenXRVk::SpaceType::Local, outPoseData);
  268. }
  269. float Session::GetControllerScale(AZ::u32 handIndex) const
  270. {
  271. return GetNativeInput()->GetControllerScale(handIndex);
  272. }
  273. float Session::GetSqueezeState(AZ::u32 handIndex) const
  274. {
  275. return GetNativeInput()->GetSqueezeState(handIndex);
  276. }
  277. float Session::GetTriggerState(AZ::u32 handIndex) const
  278. {
  279. return GetNativeInput()->GetTriggerState(handIndex);
  280. }
  281. float Session::GetXButtonState() const
  282. {
  283. return (GetNativeInput()->GetXButtonState() ? 1.f : 0.f);
  284. }
  285. float Session::GetYButtonState() const
  286. {
  287. return (GetNativeInput()->GetYButtonState() ? 1.f : 0.f);
  288. }
  289. float Session::GetAButtonState() const
  290. {
  291. return (GetNativeInput()->GetAButtonState() ? 1.f : 0.f);
  292. }
  293. float Session::GetBButtonState() const
  294. {
  295. return (GetNativeInput()->GetBButtonState() ? 1.f : 0.f);
  296. }
  297. float Session::GetXJoyStickState(AZ::u32 handIndex) const
  298. {
  299. return GetNativeInput()->GetXJoyStickState(handIndex);
  300. }
  301. float Session::GetYJoyStickState(AZ::u32 handIndex) const
  302. {
  303. return GetNativeInput()->GetYJoyStickState(handIndex);
  304. }
  305. XrSession Session::GetXrSession() const
  306. {
  307. return m_session;
  308. }
  309. XrSpace Session::GetXrSpace(SpaceType spaceType) const
  310. {
  311. Space* space = static_cast<Space*>(GetSpace());
  312. return space->GetXrSpace(spaceType);
  313. }
  314. bool Session::IsSessionRunning() const
  315. {
  316. return m_sessionRunning;
  317. }
  318. bool Session::IsSessionFocused() const
  319. {
  320. return m_sessionState == XR_SESSION_STATE_FOCUSED;
  321. }
  322. bool Session::IsRestartRequested() const
  323. {
  324. return m_requestRestart;
  325. }
  326. bool Session::IsExitRenderLoopRequested() const
  327. {
  328. return m_exitRenderLoop;
  329. }
  330. void Session::ShutdownInternal()
  331. {
  332. if (m_session != XR_NULL_HANDLE)
  333. {
  334. xrDestroySession(m_session);
  335. }
  336. }
  337. Input* Session::GetNativeInput() const
  338. {
  339. return static_cast<Input*>(GetInput());
  340. }
  341. }