MultipleViewsComponent.h 4.0 KB

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