AlphaToCoverageExampleComponent.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 <AzCore/Component/TickBus.h>
  11. #include <AzCore/std/containers/array.h>
  12. #include <Atom/RPI.Public/Shader/ShaderResourceGroup.h>
  13. #include <Atom/RHI.Reflect/Format.h>
  14. #include <Atom/RHI.Reflect/MultisampleState.h>
  15. #include <Atom/RHI.Reflect/ShaderResourceGroupLayoutDescriptor.h>
  16. #include <Atom/RHI.Reflect/ScopeId.h>
  17. #include <Atom/RHI/Buffer.h>
  18. #include <Atom/RHI/BufferPool.h>
  19. #include <Atom/RHI/DrawItem.h>
  20. #include <Atom/RHI/PipelineState.h>
  21. #include <Atom/RHI/StreamBufferView.h>
  22. #include <RHI/BasicRHIComponent.h>
  23. namespace AtomSampleViewer
  24. {
  25. // Alpha To Coverage example.
  26. // Draw 2 overlapping rectangles with alpha tested texture with the followings 3 ways, from left to right:
  27. // - Src Over
  28. // - Alpha Testing
  29. // - Alpha To Coverage (4xMSAA)
  30. class AlphaToCoverageExampleComponent final
  31. : public BasicRHIComponent
  32. , public AZ::TickBus::Handler
  33. {
  34. public:
  35. AZ_COMPONENT(AlphaToCoverageExampleComponent, "{202755F5-443F-4877-ACE7-0A5D0C8414E6}", AZ::Component);
  36. AZ_DISABLE_COPY(AlphaToCoverageExampleComponent);
  37. static void Reflect(AZ::ReflectContext* context);
  38. AlphaToCoverageExampleComponent();
  39. ~AlphaToCoverageExampleComponent() override = default;
  40. protected:
  41. struct ScopeData
  42. {
  43. };
  44. enum class BlendType : uint32_t
  45. {
  46. SrcOver = 0,
  47. AlphaTest,
  48. A2C_MSAA,
  49. Count
  50. };
  51. static const uint32_t s_numBlendTypes = static_cast<uint32_t>(BlendType::Count);
  52. // AZ::Component
  53. void Activate() override;
  54. void Deactivate() override;
  55. // TickBus::Handler
  56. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  57. // RHISystemNotificationBus::Handler
  58. void OnFramePrepare(AZ::RHI::FrameGraphBuilder& frameGraphBuilder) override;
  59. void CreateResources();
  60. void CreateScopeResources(BlendType type);
  61. void CreateRectangleScopeProducer(BlendType type);
  62. static const uint32_t s_numRectangles = 2; // number of rectangles to overlap for each blend type
  63. static const AZ::RHI::Format s_depthFormat = AZ::RHI::Format::D32_FLOAT;
  64. static const uint32_t s_sampleCount = 4; // sample count for A2C_MSAA
  65. float m_time = 0.f;
  66. static constexpr float m_fieldOfView = AZ::Constants::QuarterPi;
  67. static constexpr float m_targetDepth = 1.f;
  68. // Input Assembly buffer and its Stream/Index Views
  69. struct RectangleBufferData
  70. {
  71. AZStd::array<VertexPosition, 4> m_positions;
  72. AZStd::array<VertexUV, 4> m_uvs;
  73. AZStd::array<uint16_t, 6> m_indices;
  74. };
  75. AZ::RHI::Ptr<AZ::RHI::BufferPool> m_inputAssemblyBufferPool;
  76. AZ::RHI::Ptr<AZ::RHI::Buffer> m_rectangleInputAssemblyBuffer;
  77. AZ::RHI::GeometryView m_geometryView;
  78. AZ::RHI::InputStreamLayout m_rectangleInputStreamLayout;
  79. // Shader Resource
  80. AZ::RHI::ShaderInputNameIndex m_worldMatrixIndex = "m_worldMatrix";
  81. AZ::RHI::ShaderInputNameIndex m_viewProjectionMatrixIndex = "m_viewProjectionMatrix";
  82. AZ::RHI::ShaderInputNameIndex m_alphaTestRefValueIndex = "m_alphaTestRefValue";
  83. AZ::RHI::AttachmentId m_depthImageAttachmentId, m_multisamleDepthImageAttachmentId;
  84. AZ::RHI::AttachmentId m_resolveImageAttachmentId;
  85. AZStd::array<AZStd::array<AZ::Data::Instance<AZ::RPI::ShaderResourceGroup>, s_numRectangles>, s_numBlendTypes> m_shaderResourceGroups;
  86. // Pipeline State and Shader
  87. AZStd::array<AZ::RHI::ConstPtr<AZ::RHI::PipelineState>, s_numBlendTypes> m_pipelineStates;
  88. AZ::Data::Instance<AZ::RPI::Shader> m_shader;
  89. };
  90. } // namespace AtomSampleViewer