OpenXRVkReferenceSpacesManager.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #pragma once
  9. #include <openxr/openxr.h>
  10. #include <OpenXRVk/OpenXRVkReferenceSpacesInterface.h>
  11. namespace OpenXRVk
  12. {
  13. //! Concrete implementation of OpenXRReferenceSpacesInterface.
  14. //! The OpenXRVk::Session class should own an instance of this class.
  15. //! Automatically instantiates the default system provided reference spaces
  16. //! as mandated by the OpenXR Spec: View, Local and Stage.
  17. class ReferenceSpacesManager final :
  18. public OpenXRReferenceSpacesInterface::Registrar
  19. {
  20. public:
  21. AZ_CLASS_ALLOCATOR(ReferenceSpacesManager, AZ::SystemAllocator);
  22. AZ_RTTI(ReferenceSpacesManager, "{4BC4D0C0-02D4-4676-8352-8BC51306AF02}", IOpenXRReferenceSpaces)
  23. static constexpr char LogName[] = "OpenXRVkReferenceSpacesManager";
  24. //! Instantiates the OpenXR Guaranteed Reference Spaces: View, Local and Stage.
  25. //! And prepares all the internal data required to service the OpenXRReferenceSpacesInterface interface.
  26. bool Init(XrInstance xrInstance, XrSession xrSession, XrViewConfigurationType xrViewConfigurationType, uint32_t numEyeViews);
  27. //! Called by the Session each tick.
  28. bool SyncViews(XrTime predictedDisplayTime);
  29. //! The Session calls this function when it receives the XR_SESSION_STATE_READY
  30. //! event.
  31. void OnSessionReady();
  32. //! This is useful for the Swapchain system.
  33. const AZStd::vector<XrView>& GetXrViews() const;
  34. //! This is useful for the Swapchain system
  35. XrSpace GetViewSpaceXrSpace() const;
  36. /////////////////////////////////////////////////
  37. /// OpenXRReferenceSpacesInterface overrides
  38. AZStd::vector<AZStd::string> GetReferenceSpaceNames() const override;
  39. AZ::Outcome<bool, AZStd::string> AddReferenceSpace(ReferenceSpaceId referenceSpaceType,
  40. const AZStd::string& spaceName, const AZ::Transform& poseInReferenceSpace) override;
  41. AZ::Outcome<bool, AZStd::string> RemoveReferenceSpace(const AZStd::string& spaceName) override;
  42. const void * GetReferenceSpaceNativeHandle(const AZStd::string& spaceName) const override;
  43. AZ::Outcome<AZ::Transform, AZStd::string> GetReferenceSpacePose(const AZStd::string& spaceName,
  44. const AZStd::string& baseSpaceName) const override;
  45. AZ::Outcome<bool, AZStd::string> SetBaseSpaceForViewSpacePose(const AZStd::string& spaceName) override;
  46. const AZStd::string& GetBaseSpaceForViewSpacePose() const override;
  47. const AZ::Transform& GetViewSpacePose() const override;
  48. uint32_t GetViewCount() const override;
  49. const AZ::Transform& GetViewPose(uint32_t eyeIndex) const override;
  50. const AZ::RPI::FovData& GetViewFovData(uint32_t eyeIndex) const override;
  51. const AZStd::vector<AZ::Transform>& GetViewPoses() const override;
  52. void ForceViewPosesCacheUpdate() override;
  53. /// OpenXRReferenceSpacesInterface overrides
  54. /////////////////////////////////////////////////
  55. private:
  56. XrInstance m_xrInstance = XR_NULL_HANDLE;
  57. XrSession m_xrSession = XR_NULL_HANDLE;
  58. XrViewConfigurationType m_xrViewConfigurationType;
  59. // Updated each time the Session calls UpdateViewSpacePoseAndEyeViewPoses().
  60. XrTime m_predictedDisplaytime;
  61. struct ReferenceSpace
  62. {
  63. AZStd::string m_name;
  64. //! We shave this transform in case we have to reset the spaces.
  65. AZ::Transform m_offsetPose;
  66. // Runtime data
  67. XrSpace m_xrSpace;
  68. };
  69. // At the bare minimum this map will always contain three spaces that can not be removed:
  70. // "View", "Local" and "Stage".
  71. AZStd::unordered_map<AZStd::string, ReferenceSpace> m_spaces;
  72. // We cache here the base space what will be used to "locate" the View Space pose.
  73. const ReferenceSpace* m_baseSpaceForViewSpace;
  74. const ReferenceSpace* m_viewSpace; // Cached for convenience. This pointer is set once during initialization and never changes.
  75. AZ::Transform m_viewSpacePose;
  76. //! The following poses are always relative to @m_viewSpacePose.
  77. AZStd::vector<AZ::Transform> m_eyeViewPoses;
  78. AZStd::vector<AZ::RPI::FovData> m_eyeViewFovDatas;
  79. AZStd::vector<XrView> m_xrViews;
  80. //! Helper function to create an XrSpace.
  81. XrSpace CreateXrSpace(XrReferenceSpaceType referenceSpaceType, const AZ::Transform& relativePose);
  82. };
  83. }