MultipleViewsComponent.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 <RHI/BasicRHIComponent.h>
  10. #include <Atom/RHI/Buffer.h>
  11. #include <Atom/RHI/BufferPool.h>
  12. #include <Atom/RHI/PipelineState.h>
  13. #include <Atom/RPI.Public/Image/AttachmentImage.h>
  14. #include <AzCore/Component/EntityBus.h>
  15. #include <AzCore/Math/Vector3.h>
  16. namespace AtomSampleViewer
  17. {
  18. //! MultipleViewsComponent
  19. //! The purpose of this sample is to test multiple views by doing shadow mapping,
  20. //! to have more complex shaders, test resource bindings and comprehensive support of depth/stencil buffer.
  21. //! For this sample, there will be 2 views: the main view (or camera view) and the light view.
  22. //! As for the geometries, there is a plane with a cube sitting on it.
  23. class MultipleViewsComponent final
  24. : public BasicRHIComponent
  25. , public AZ::EntitySystemBus::Handler
  26. {
  27. public:
  28. AZ_COMPONENT(MultipleViewsComponent, "{A8CC6572-7FD4-4708-88B3-E24034C8F6FC}", AZ::Component);
  29. AZ_DISABLE_COPY(MultipleViewsComponent);
  30. static void Reflect(AZ::ReflectContext* context);
  31. MultipleViewsComponent();
  32. ~MultipleViewsComponent() override = default;
  33. // AZ::Component
  34. void Activate() override;
  35. void Deactivate() override;
  36. protected:
  37. static const uint32_t geometryVertexCount = 28;
  38. static const uint32_t geometryIndexCount = 42;
  39. struct BufferData
  40. {
  41. AZStd::array<VertexPosition, geometryVertexCount> m_positions;
  42. AZStd::array<VertexColor, geometryVertexCount> m_colors;
  43. AZStd::array<VertexNormal, geometryVertexCount> m_normals;
  44. AZStd::array<uint16_t, geometryIndexCount> m_indices;
  45. };
  46. struct ScopeData
  47. {
  48. //UserDataParam - Empty for this samples
  49. };
  50. // RHISystemNotificationBus::Handler
  51. void OnFramePrepare(AZ::RHI::FrameGraphBuilder& frameGraphBuilder) override;
  52. // Create Buffers for Input Assembly
  53. BufferData CreateBufferData();
  54. void CreateInputAssemblyBuffersAndViews();
  55. void SetupScene();
  56. void InitRenderTarget();
  57. void CreateDepthScope();
  58. void CreateMainScope();
  59. void ReadDepthShader();
  60. void ReadMainShader();
  61. // shadow map required variable
  62. AZ::Matrix4x4 m_worldMatrix;
  63. AZ::Matrix4x4 m_viewProjectionMatrix;
  64. AZ::Vector3 m_worldPosition;
  65. AZ::Matrix4x4 m_lightViewProjectionMatrix;
  66. AZ::Matrix4x4 m_lightProjectionMatrix;
  67. AZ::Vector4 m_lightPosition;
  68. AZ::Vector4 m_ambientColor;
  69. AZ::Vector4 m_diffuseColor;
  70. static constexpr float m_zNear = 1.0f;
  71. static constexpr float m_zFar = 100.0f;
  72. static const AZ::Vector3 m_up;
  73. static const AZ::Vector3 m_lookAt;
  74. static const int s_shadowMapSize = 1024;
  75. AZ::RHI::Ptr<AZ::RHI::BufferPool> m_bufferPool;
  76. AZ::RHI::Ptr<AZ::RHI::Buffer> m_inputAssemblyBuffer;
  77. AZ::RHI::InputStreamLayout m_inputStreamLayout;
  78. AZStd::array<AZ::RHI::ConstPtr<AZ::RHI::PipelineState>, 2> m_pipelineStates;
  79. AZStd::array<AZ::Data::Instance<AZ::RPI::ShaderResourceGroup>, 2> m_shaderResourceGroups;
  80. AZStd::array<AZ::RHI::ShaderInputConstantIndex, 6> m_shaderInputConstantIndices;
  81. AZ::RHI::ShaderInputImageIndex m_shaderInputImageIndex;
  82. AZ::RHI::GeometryView m_geometryView;
  83. AZ::RHI::AttachmentId m_depthMapID;
  84. AZ::RHI::AttachmentId m_depthStencilID;
  85. AZ::RHI::ClearValue m_depthClearValue;
  86. AZ::RHI::TransientImageDescriptor m_transientImageDescriptor;
  87. };
  88. } // namespace AtomSampleViewer