MSAAExampleComponent.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 <AzFramework/Input/Events/InputChannelEventListener.h>
  10. #include <AzCore/Component/Component.h>
  11. #include <AzCore/std/containers/array.h>
  12. #include <Atom/RPI.Public/Shader/ShaderResourceGroup.h>
  13. #include <Atom/RHI.Reflect/MultisampleState.h>
  14. #include <Atom/RHI.Reflect/ShaderResourceGroupLayoutDescriptor.h>
  15. #include <Atom/RHI.Reflect/ScopeId.h>
  16. #include <Atom/RHI/Buffer.h>
  17. #include <Atom/RHI/BufferPool.h>
  18. #include <Atom/RHI/DrawItem.h>
  19. #include <Atom/RHI/PipelineState.h>
  20. #include <Atom/RHI/StreamBufferView.h>
  21. #include <RHI/BasicRHIComponent.h>
  22. namespace AZ
  23. {
  24. namespace RHI
  25. {
  26. class FrameGraphBuilder;
  27. }
  28. }
  29. namespace AtomSampleViewer
  30. {
  31. // MSAA RHI example.
  32. // Test rendering a triangle to a MSAA texture using different settings and resolving it to non MSAA swapchan image for presentation.
  33. // The example cycles through the following modes when detecting a mouse click:
  34. // 1 - 2xMSAA
  35. // 2 - 4xMSAA
  36. // 3 - 4xMSAA with custom sample positions
  37. // 4 - 4xMSAA with custom resolve using a shader
  38. // 5 - No MSAA
  39. class MSAAExampleComponent final
  40. : public BasicRHIComponent
  41. , public AzFramework::InputChannelEventListener
  42. {
  43. public:
  44. AZ_COMPONENT(MSAAExampleComponent, "{3F35D60E-4EB5-4319-8925-27BE0EC2B7E0}", AZ::Component);
  45. AZ_DISABLE_COPY(MSAAExampleComponent);
  46. static void Reflect(AZ::ReflectContext* context);
  47. MSAAExampleComponent();
  48. ~MSAAExampleComponent() override = default;
  49. protected:
  50. // AZ::Component
  51. void Activate() override;
  52. void Deactivate() override;
  53. // AzFramework::InputChannelEventListener
  54. bool OnInputChannelEventFiltered(const AzFramework::InputChannel& inputChannel) override;
  55. void CreateTriangleResources();
  56. void CreateQuadResources();
  57. struct ScopeData
  58. {
  59. //UserDataParam - Empty for this samples
  60. };
  61. enum class MSAAType : uint32_t
  62. {
  63. MSAA2X = 0,
  64. MSAA4X,
  65. MSAA4X_Custom_Positions,
  66. MSAA4X_Custom_Resolve,
  67. NoAA,
  68. Count
  69. };
  70. static const uint32_t s_numMSAAExamples = static_cast<uint32_t>(MSAAType::Count);
  71. struct SampleProperties
  72. {
  73. uint32_t m_samplesCount;
  74. bool m_resolve;
  75. AZStd::vector<AZ::RHI::SamplePosition> m_customPositions;
  76. AZ::RHI::AttachmentStoreAction m_storeAction;
  77. AZ::RHI::AttachmentId m_attachmentId;
  78. bool m_isTransient;
  79. };
  80. void OnMSAATypeChanged(MSAAType type);
  81. void CreateScopeResources(MSAAType type);
  82. void CreateScopeProducer(MSAAType type);
  83. void CreateCustomMSAAResolveResources();
  84. void CreateCustomMSAAResolveScope();
  85. AZ::RHI::Ptr<AZ::RHI::BufferPool> m_inputAssemblyBufferPool;
  86. AZ::RHI::Ptr<AZ::RHI::Buffer> m_triangleInputAssemblyBuffer;
  87. AZ::RHI::Ptr<AZ::RHI::Buffer> m_quadInputAssemblyBuffer;
  88. AZStd::array<AZ::RHI::ConstPtr<AZ::RHI::PipelineState>, s_numMSAAExamples> m_pipelineStates;
  89. AZ::RHI::ConstPtr<AZ::RHI::PipelineState> m_customResolveMSAAPipelineState;
  90. AZ::Data::Instance<AZ::RPI::ShaderResourceGroup> m_triangleShaderResourceGroup;
  91. AZ::Data::Instance<AZ::RPI::ShaderResourceGroup> m_customMSAAResolveShaderResourceGroup;
  92. AZ::Data::Instance<AZ::RPI::Shader> m_triangleShader;
  93. AZ::Data::Instance<AZ::RPI::Shader> m_customMSAAResolveShader;
  94. AZ::RHI::ShaderInputConstantIndex m_objectMatrixConstantIndex;
  95. AZ::RHI::ShaderInputImageIndex m_customMSAAResolveTextureInputIndex;
  96. struct TriangleBufferData
  97. {
  98. AZStd::array<VertexPosition, 3> m_positions;
  99. AZStd::array<VertexColor, 3> m_colors;
  100. AZStd::array<uint16_t, 3> m_indices;
  101. };
  102. struct QuadBufferData
  103. {
  104. AZStd::array<VertexPosition, 4> m_positions;
  105. AZStd::array<VertexUV, 4> m_uvs;
  106. AZStd::array<uint16_t, 6> m_indices;
  107. };
  108. AZ::RHI::GeometryView m_triangleGeometryView;
  109. AZ::RHI::GeometryView m_quadGeometryView;
  110. AZ::RHI::InputStreamLayout m_triangleInputStreamLayout;
  111. AZ::RHI::InputStreamLayout m_quadInputStreamLayout;
  112. AZStd::array<SampleProperties, s_numMSAAExamples> m_sampleProperties;
  113. AZStd::array<AZStd::vector<AZStd::shared_ptr<AZ::RHI::ScopeProducer>>, s_numMSAAExamples> m_MSAAScopeProducers;
  114. MSAAType m_currentType = MSAAType::MSAA2X;
  115. };
  116. } // namespace AtomSampleViewer