XRSystem.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 <AzCore/Interface/Interface.h>
  9. #include <AzCore/Debug/Profiler.h>
  10. #include <XR/XRFactory.h>
  11. #include <XR/XRSystem.h>
  12. #include <XR/XRUtils.h>
  13. namespace XR
  14. {
  15. void System::Init(const System::Descriptor& descriptor)
  16. {
  17. m_validationMode = descriptor.m_validationMode;
  18. AZ::SystemTickBus::Handler::BusConnect();
  19. }
  20. AZ::RHI::ResultCode System::InitInstance()
  21. {
  22. m_instance = Factory::Get().CreateInstance();
  23. if (m_instance)
  24. {
  25. return m_instance->Init(m_validationMode);
  26. }
  27. return AZ::RHI::ResultCode::Fail;
  28. }
  29. AZ::RHI::ResultCode System::InitNativeInstance(AZ::RHI::XRInstanceDescriptor* instanceDescriptor)
  30. {
  31. return m_instance->InitNativeInstance(instanceDescriptor);
  32. }
  33. AZ::u32 System::GetNumPhysicalDevices() const
  34. {
  35. return m_instance->GetNumPhysicalDevices();
  36. }
  37. AZ::RHI::ResultCode System::GetXRPhysicalDevice(AZ::RHI::XRPhysicalDeviceDescriptor* physicalDeviceDescriptor, int32_t index)
  38. {
  39. AZ_Error("XR", physicalDeviceDescriptor, "The descriptor is null");
  40. if (physicalDeviceDescriptor)
  41. {
  42. return m_instance->GetXRPhysicalDevice(physicalDeviceDescriptor, index);
  43. }
  44. return AZ::RHI::ResultCode::Fail;
  45. }
  46. AZ::RHI::ResultCode System::CreateDevice(AZ::RHI::XRDeviceDescriptor* instanceDescriptor)
  47. {
  48. if (!m_device)
  49. {
  50. m_device = Factory::Get().CreateDevice();
  51. AZ_Assert(m_device, "XR Device not created");
  52. if (m_device->Init(Device::Descriptor{ m_validationMode, m_instance}) == AZ::RHI::ResultCode::Success)
  53. {
  54. return m_device->InitDeviceInternal(instanceDescriptor);
  55. }
  56. }
  57. return AZ::RHI::ResultCode::Fail;
  58. }
  59. AZ::RHI::ResultCode System::CreateSession(AZ::RHI::XRSessionDescriptor* sessionDescriptor)
  60. {
  61. if (!m_session)
  62. {
  63. m_session = Factory::Get().CreateSession();
  64. AZ_Assert(m_session, "Session not created");
  65. AZ::RHI::ResultCode result = m_session->Init(Session::Descriptor{ m_validationMode, m_device, m_instance });
  66. if (result == AZ::RHI::ResultCode::Success)
  67. {
  68. return m_session->InitInternal(sessionDescriptor);
  69. }
  70. }
  71. return AZ::RHI::ResultCode::Fail;
  72. }
  73. AZ::RHI::ResultCode System::CreateSwapChain()
  74. {
  75. if (!m_swapChain)
  76. {
  77. m_swapChain = Factory::Get().CreateSwapChain();
  78. AZ_Assert(m_swapChain, "XR SwapChain not created");
  79. return m_swapChain->Init(SwapChain::Descriptor{ m_validationMode, m_instance, m_session, m_device });
  80. }
  81. return AZ::RHI::ResultCode::Fail;
  82. }
  83. AZ::RHI::ResultCode System::GetSwapChainImage(AZ::RHI::XRSwapChainDescriptor* swapchainDescriptor) const
  84. {
  85. AZ_Assert(m_swapChain, "SwapChain is null");
  86. if (m_swapChain)
  87. {
  88. return m_swapChain->GetSwapChainImage(swapchainDescriptor);
  89. }
  90. return AZ::RHI::ResultCode::Fail;
  91. }
  92. AZ::u32 System::GetSwapChainWidth(AZ::u32 viewIndex) const
  93. {
  94. AZ_Assert(m_swapChain, "SwapChain is null");
  95. if (m_swapChain)
  96. {
  97. return m_swapChain->GetSwapChainWidth(viewIndex);
  98. }
  99. return 0;
  100. }
  101. AZ::u32 System::GetSwapChainHeight(AZ::u32 viewIndex) const
  102. {
  103. AZ_Assert(m_swapChain, "SwapChain is null");
  104. if (m_swapChain)
  105. {
  106. return m_swapChain->GetSwapChainHeight(viewIndex);
  107. }
  108. return 0;
  109. }
  110. void System::OnSystemTick()
  111. {
  112. m_session->PollEvents();
  113. if (m_session->IsSessionRunning())
  114. {
  115. m_session->GetInput()->PollActions();
  116. }
  117. }
  118. void System::BeginFrame()
  119. {
  120. if (m_device && m_session && m_session->IsSessionRunning())
  121. {
  122. m_isInFrame = m_device->BeginFrame();
  123. }
  124. }
  125. void System::EndFrame()
  126. {
  127. if (m_isInFrame)
  128. {
  129. m_device->EndFrame(m_swapChain);
  130. m_isInFrame = false;
  131. }
  132. }
  133. void System::AcquireSwapChainImage(AZ::u32 viewIndex)
  134. {
  135. if (m_isInFrame && m_device->ShouldRender())
  136. {
  137. m_device->AcquireSwapChainImage(viewIndex, m_swapChain.get());
  138. }
  139. }
  140. AZ::u32 System::GetNumViews() const
  141. {
  142. return m_swapChain->GetNumViews();
  143. }
  144. AZ::u32 System::GetCurrentImageIndex(AZ::u32 viewIndex) const
  145. {
  146. SwapChain::View* viewSwapchain = m_swapChain->GetView(viewIndex);
  147. return viewSwapchain->m_activeImageIndex;
  148. }
  149. bool System::ShouldRender() const
  150. {
  151. if (m_session->IsSessionRunning())
  152. {
  153. return m_device->ShouldRender();
  154. }
  155. return false;
  156. }
  157. AZ::RPI::FovData System::GetViewFov(AZ::u32 viewIndex) const
  158. {
  159. return m_device->GetViewFov(viewIndex);
  160. }
  161. AZ::RPI::PoseData System::GetViewPose(AZ::u32 viewIndex) const
  162. {
  163. return m_device->GetViewPose(viewIndex);
  164. }
  165. AZ::RPI::PoseData System::GetControllerPose(AZ::u32 handIndex) const
  166. {
  167. if (m_session->IsSessionRunning())
  168. {
  169. return m_session->GetControllerPose(handIndex);
  170. }
  171. return AZ::RPI::PoseData();
  172. }
  173. float System::GetControllerScale(AZ::u32 handIndex) const
  174. {
  175. if (m_session->IsSessionRunning())
  176. {
  177. return m_session->GetControllerScale(handIndex);
  178. }
  179. return 1.0f;
  180. }
  181. AZ::RPI::PoseData System::GetViewFrontPose() const
  182. {
  183. if (m_session->IsSessionRunning())
  184. {
  185. return m_session->GetViewFrontPose();
  186. }
  187. return AZ::RPI::PoseData();
  188. }
  189. AZ::Matrix4x4 System::CreateProjectionOffset(float angleLeft, float angleRight,
  190. float angleBottom, float angleTop,
  191. float nearDist, float farDist)
  192. {
  193. return XR::CreateProjectionOffset(angleLeft, angleRight, angleBottom, angleTop, nearDist, farDist);
  194. }
  195. void System::Shutdown()
  196. {
  197. AZ::SystemTickBus::Handler::BusDisconnect();
  198. m_instance = nullptr;
  199. m_device = nullptr;
  200. }
  201. }