2
0

XRExampleComponent.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/RHI/FrameScheduler.h>
  12. #include <Atom/RHI/DrawItem.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. class XRExampleComponent final
  26. : public BasicRHIComponent
  27. , public AZ::TickBus::Handler
  28. {
  29. public:
  30. AZ_COMPONENT(XRExampleComponent, "{A7D9A921-1FF9-4078-92BD-169E258456E7}");
  31. AZ_DISABLE_COPY(XRExampleComponent);
  32. static void Reflect(AZ::ReflectContext* context);
  33. XRExampleComponent();
  34. ~XRExampleComponent() override = default;
  35. protected:
  36. // 1 cube for view + 2 cubes for the controller
  37. static const uint32_t NumberOfCubes = 3;
  38. static const uint32_t GeometryVertexCount = 24;
  39. static const uint32_t GeometryIndexCount = 36;
  40. struct SingleCubeBufferData
  41. {
  42. AZStd::array<VertexPosition, GeometryVertexCount> m_positions;
  43. AZStd::array<VertexColor, GeometryVertexCount> m_colors;
  44. AZStd::array<uint16_t, GeometryIndexCount> m_indices;
  45. };
  46. // AZ::Component
  47. void Activate() override;
  48. void Deactivate() override;
  49. // RHISystemNotificationBus::Handler
  50. void OnFramePrepare(AZ::RHI::FrameGraphBuilder& frameGraphBuilder) override;
  51. // TickBus::Handler
  52. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  53. //! Create IA data
  54. void CreateCubeInputAssemblyBuffer();
  55. //! Create Cube data
  56. SingleCubeBufferData CreateSingleCubeBufferData();
  57. //! Create PSO data
  58. void CreateCubePipeline();
  59. //! Create the relevant Scope
  60. void CreateScope();
  61. AZ::RHI::Ptr<AZ::RHI::BufferPool> m_bufferPool;
  62. AZ::RHI::IndexBufferView m_indexBufferView;
  63. AZ::RHI::Ptr<AZ::RHI::Buffer> m_inputAssemblyBuffer;
  64. AZ::RHI::InputStreamLayout m_streamLayoutDescriptor;
  65. AZ::RHI::ConstPtr<AZ::RHI::PipelineState> m_pipelineState;
  66. struct BufferData
  67. {
  68. AZStd::array<VertexPosition, 3> m_positions;
  69. AZStd::array<VertexColor, 3> m_colors;
  70. AZStd::array<uint16_t, 3> m_indices;
  71. };
  72. AZStd::array<AZ::RHI::StreamBufferView, 2> m_streamBufferViews;
  73. AZ::RHI::DrawItem m_drawItem;
  74. float m_time = 0.0f;
  75. AZStd::array<AZ::Data::Instance<AZ::RPI::ShaderResourceGroup>, NumberOfCubes> m_shaderResourceGroups;
  76. AZStd::array<AZ::Matrix4x4, NumberOfCubes> m_modelMatrices;
  77. AZ::Matrix4x4 m_viewProjMatrix;
  78. AZ::RHI::ShaderInputConstantIndex m_shaderIndexWorldMat;
  79. AZ::RHI::ShaderInputConstantIndex m_shaderIndexViewProj;
  80. AZ::RHI::AttachmentId m_depthStencilID;
  81. };
  82. } // namespace AtomSampleViewer