XRSystem.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. AZ::RHI::Format System::GetSwapChainFormat(AZ::u32 viewIndex) const
  111. {
  112. AZ_Assert(m_swapChain, "SwapChain is null");
  113. if (m_swapChain)
  114. {
  115. return m_swapChain->GetSwapChainFormat(viewIndex);
  116. }
  117. return AZ::RHI::Format::Unknown;
  118. }
  119. void System::OnSystemTick()
  120. {
  121. m_session->PollEvents();
  122. if (m_session->IsSessionRunning())
  123. {
  124. m_session->GetInput()->PollActions();
  125. }
  126. }
  127. void System::BeginFrame()
  128. {
  129. if (m_device && m_session && m_session->IsSessionRunning())
  130. {
  131. m_isInFrame = m_device->BeginFrame();
  132. }
  133. }
  134. void System::EndFrame()
  135. {
  136. if (m_isInFrame)
  137. {
  138. m_device->EndFrame(m_swapChain);
  139. m_isInFrame = false;
  140. }
  141. }
  142. void System::AcquireSwapChainImage(AZ::u32 viewIndex)
  143. {
  144. if (m_isInFrame && m_device->ShouldRender())
  145. {
  146. m_device->AcquireSwapChainImage(viewIndex, m_swapChain.get());
  147. }
  148. }
  149. AZ::u32 System::GetNumViews() const
  150. {
  151. return m_swapChain->GetNumViews();
  152. }
  153. AZ::u32 System::GetCurrentImageIndex(AZ::u32 viewIndex) const
  154. {
  155. SwapChain::View* viewSwapchain = m_swapChain->GetView(viewIndex);
  156. return viewSwapchain->m_activeImageIndex;
  157. }
  158. bool System::ShouldRender() const
  159. {
  160. if (m_session->IsSessionRunning())
  161. {
  162. return m_device->ShouldRender();
  163. }
  164. return false;
  165. }
  166. AZ::RPI::FovData System::GetViewFov(AZ::u32 viewIndex) const
  167. {
  168. return m_device->GetViewFov(viewIndex);
  169. }
  170. AZ::RPI::PoseData System::GetViewPose(AZ::u32 viewIndex) const
  171. {
  172. return m_device->GetViewPose(viewIndex);
  173. }
  174. AZ::RPI::PoseData System::GetControllerPose(AZ::u32 handIndex) const
  175. {
  176. if (m_session->IsSessionRunning())
  177. {
  178. return m_session->GetControllerPose(handIndex);
  179. }
  180. return AZ::RPI::PoseData();
  181. }
  182. float System::GetControllerScale(AZ::u32 handIndex) const
  183. {
  184. if (m_session->IsSessionRunning())
  185. {
  186. return m_session->GetControllerScale(handIndex);
  187. }
  188. return 1.0f;
  189. }
  190. AZ::RPI::PoseData System::GetViewFrontPose() const
  191. {
  192. if (m_session->IsSessionRunning())
  193. {
  194. return m_session->GetViewFrontPose();
  195. }
  196. return AZ::RPI::PoseData();
  197. }
  198. AZ::Matrix4x4 System::CreateProjectionOffset(float angleLeft, float angleRight,
  199. float angleBottom, float angleTop,
  200. float nearDist, float farDist)
  201. {
  202. return XR::CreateProjectionOffset(angleLeft, angleRight, angleBottom, angleTop, nearDist, farDist);
  203. }
  204. AZ::RHI::XRRenderingInterface* System::GetRHIXRRenderingInterface()
  205. {
  206. return this;
  207. }
  208. void System::Shutdown()
  209. {
  210. AZ::SystemTickBus::Handler::BusDisconnect();
  211. m_instance = nullptr;
  212. m_device = nullptr;
  213. }
  214. }