XRExampleComponent.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <AzCore/Component/Component.h>
  10. #include <Atom/RPI.Public/Shader/ShaderResourceGroup.h>
  11. #include <Atom/RPI.Public/XR/XRSpaceNotificationBus.h>
  12. #include <Atom/RHI/FrameScheduler.h>
  13. #include <Atom/RHI/Device.h>
  14. #include <Atom/RHI/Factory.h>
  15. #include <Atom/RHI/PipelineState.h>
  16. #include <Atom/RHI/BufferPool.h>
  17. #include <AzCore/Math/Matrix4x4.h>
  18. #include <RHI/BasicRHIComponent.h>
  19. #include <AzCore/Component/TickBus.h>
  20. namespace AtomSampleViewer
  21. {
  22. //! The purpose of this sample is to establish a simple XR sample utilizing a simple VR pipeline
  23. //! It will render a mesh per controller plus one for the front view. It will prove out all the
  24. //! code related related to openxr device, instance, swapchain, session, input, space.
  25. //! There will be three instances of this class active.
  26. //! 1- The main pipline.
  27. //! 2- The Left eye pipeline.
  28. //! 3- The Right eye pipeline.
  29. class XRExampleComponent final
  30. : public BasicRHIComponent
  31. , public AZ::TickBus::Handler
  32. , public AZ::RPI::XRSpaceNotificationBus::Handler
  33. {
  34. public:
  35. AZ_COMPONENT(XRExampleComponent, "{A7D9A921-1FF9-4078-92BD-169E258456E7}");
  36. AZ_DISABLE_COPY(XRExampleComponent);
  37. static void Reflect(AZ::ReflectContext* context);
  38. XRExampleComponent();
  39. ~XRExampleComponent() override = default;
  40. protected:
  41. // 1 cube for view + 2 cubes for the controller
  42. static const uint32_t NumberOfCubes = 3;
  43. static const uint32_t GeometryVertexCount = 24;
  44. static const uint32_t GeometryIndexCount = 36;
  45. struct SingleCubeBufferData
  46. {
  47. AZStd::array<VertexPosition, GeometryVertexCount> m_positions;
  48. AZStd::array<VertexColor, GeometryVertexCount> m_colors;
  49. AZStd::array<uint16_t, GeometryIndexCount> m_indices;
  50. };
  51. // AZ::Component
  52. void Activate() override;
  53. void Deactivate() override;
  54. // RHISystemNotificationBus::Handler
  55. void OnFramePrepare(AZ::RHI::FrameGraphBuilder& frameGraphBuilder) override;
  56. // TickBus::Handler
  57. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  58. // AZ::RPI::XRSpaceNotificationBus::Handler overrides
  59. // This notification arrives before this.OnFramePrepare, additionally it is called
  60. // after XR System calls XrSystem::BeginFrame. Why this is important? Because these
  61. // transformations are calculated by the XR Device based on a predicted display time
  62. // for the current frame. If the shaders update the viewSrg with this freash data
  63. // the rendered image appears more stable and less jittery.
  64. void OnXRSpaceLocationsChanged(
  65. const AZ::Transform& baseSpaceToHeadTm,
  66. const AZ::Transform& headToLeftEyeTm,
  67. const AZ::Transform& headToRightEyeTm) override;
  68. // Controller (aka Joysticks) poses/transforms are relative to the head transform.
  69. AZ::Transform m_baseSpaceToHeadTm;
  70. //! Create IA data
  71. void CreateCubeInputAssemblyBuffer();
  72. //! Create Cube data
  73. SingleCubeBufferData CreateSingleCubeBufferData();
  74. //! Create PSO data
  75. void CreateCubePipeline();
  76. //! Create the relevant Scope
  77. void CreateScope();
  78. AZ::RHI::Ptr<AZ::RHI::BufferPool> m_bufferPool;
  79. AZ::RHI::Ptr<AZ::RHI::Buffer> m_inputAssemblyBuffer;
  80. AZ::RHI::InputStreamLayout m_streamLayoutDescriptor;
  81. AZ::RHI::ConstPtr<AZ::RHI::PipelineState> m_pipelineState;
  82. struct BufferData
  83. {
  84. AZStd::array<VertexPosition, 3> m_positions;
  85. AZStd::array<VertexColor, 3> m_colors;
  86. AZStd::array<uint16_t, 3> m_indices;
  87. };
  88. AZ::RHI::GeometryView m_geometryView;
  89. float m_time = 0.0f;
  90. AZStd::array<AZ::Data::Instance<AZ::RPI::ShaderResourceGroup>, NumberOfCubes> m_shaderResourceGroups;
  91. AZStd::array<AZ::Matrix4x4, NumberOfCubes> m_modelMatrices;
  92. AZ::Matrix4x4 m_viewProjMatrix;
  93. AZ::RHI::ShaderInputConstantIndex m_shaderIndexWorldMat;
  94. AZ::RHI::ShaderInputConstantIndex m_shaderIndexViewProj;
  95. AZ::RHI::AttachmentId m_depthStencilID;
  96. };
  97. } // namespace AtomSampleViewer