VariableRateShadingExampleComponent.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/Math/Color.h>
  12. #include <AzCore/std/containers/array.h>
  13. #include <AzFramework/Input/Events/InputChannelEventListener.h>
  14. #include <AzFramework/Windowing/WindowBus.h>
  15. #include <AzFramework/Windowing/NativeWindow.h>
  16. #include <Atom/RPI.Public/Shader/Shader.h>
  17. #include <Atom/RPI.Public/Shader/ShaderResourceGroup.h>
  18. #include <Atom/RHI/BufferPool.h>
  19. #include <Atom/RHI/Device.h>
  20. #include <Atom/RHI/DeviceCopyItem.h>
  21. #include <Atom/RHI/DrawItem.h>
  22. #include <Atom/RHI/Factory.h>
  23. #include <Atom/RHI/FrameScheduler.h>
  24. #include <Atom/RHI/PipelineState.h>
  25. #include <RHI/BasicRHIComponent.h>
  26. #include <Utils/ImGuiSidebar.h>
  27. #include <Utils/ImGuiProgressList.h>
  28. #include <Atom/Utils/AssetCollectionAsyncLoader.h>
  29. namespace AtomSampleViewer
  30. {
  31. //! This samples demonstrates the use of Variable Rate Shading on the RHI.
  32. //! Shading rates can be specified in 3 different ways: PerDraw, PerPrimtive and PerRegion.
  33. //! This samples only uses the PerDraw and PerRegion modes.
  34. //! The samples render a full screen quad using different shading rates.
  35. //! When the PerRegion mode is used, an image is generated using a compute shader with different
  36. //! rates in a circular pattern from the center (or the pointer position).
  37. //! When a PerDraw mode is used, the rate is applied equally to the whole quad. The rate can be changed
  38. //! using the GUI of the sample.
  39. //! Combinator operations are also exposed when both PerDraw and PerRegion are being used.
  40. class VariableRateShadingExampleComponent final
  41. : public BasicRHIComponent
  42. , public AZ::TickBus::Handler
  43. , public AzFramework::InputChannelEventListener
  44. {
  45. public:
  46. AZ_COMPONENT(VariableRateShadingExampleComponent, "{B98E1C6A-8C23-4AA4-82E6-4B652F6151DD}", AZ::Component);
  47. AZ_DISABLE_COPY(VariableRateShadingExampleComponent);
  48. static void Reflect(AZ::ReflectContext* context);
  49. VariableRateShadingExampleComponent();
  50. ~VariableRateShadingExampleComponent() override = default;
  51. private:
  52. // AZ::Component
  53. void Activate() override;
  54. void Deactivate() override;
  55. // AZ::TickBus::Handler
  56. void OnTick(float deltaTime, AZ::ScriptTimePoint time);
  57. // RHISystemNotificationBus::Handler
  58. void OnFramePrepare(AZ::RHI::FrameGraphBuilder& frameGraphBuilder) override;
  59. // AzFramework::InputChannelEventListener
  60. bool OnInputChannelEventFiltered(const AzFramework::InputChannel& inputChannel) override;
  61. // Draw the ImGUI settings
  62. void DrawSettings();
  63. // Loads the compute and graphics shaders.
  64. void LoadShaders();
  65. // Creates the image pool and images used for shading rate attachments.
  66. void CreateShadingRateImage();
  67. // Creates the shading resource groups used by the compute and graphic scopes.
  68. void CreateShaderResourceGroups();
  69. // Creates the IA resources for the full screen quad.
  70. void CreateInputAssemblyBuffersAndViews();
  71. // Creates the necessary pipelines.
  72. void CreatePipelines();
  73. // Creates the scope used for rendering the full screen quad.
  74. void CreateRenderScope();
  75. // Creates the scope for showing the shading rate image.
  76. void CreatImageDisplayScope();
  77. // Creates the compute used for updating the shading rate image.
  78. void CreateComputeScope();
  79. // ImGUI sidebar that handles the options of the sample.
  80. ImGuiSidebar m_imguiSidebar;
  81. // Whether to use a shading rate image.
  82. bool m_useImageShadingRate = true;
  83. // Whether to show the shading rate image.
  84. bool m_showShadingRateImage = false;
  85. // Whether the center of the shading rate image follows the position of the pointer.
  86. bool m_followPointer = false;
  87. // Whether to use the PerDraw mode.
  88. bool m_useDrawShadingRate = false;
  89. // Combinator operation to applied between the PerDraw and PerRegion shading rate.
  90. AZ::RHI::ShadingRateCombinerOp m_combinerOp = AZ::RHI::ShadingRateCombinerOp::Passthrough;
  91. // Shading rate when using the PerDraw mode.
  92. AZ::RHI::ShadingRate m_shadingRate = AZ::RHI::ShadingRate::Rate1x1;
  93. // Pipelines used for rendering the full screen quad with and without a shading rate attachments.
  94. AZ::RHI::ConstPtr<AZ::RHI::PipelineState> m_modelPipelineState[2];
  95. // Pipeline used for updating the shading rate image.
  96. AZ::RHI::ConstPtr<AZ::RHI::PipelineState> m_computePipelineState;
  97. // Pipeline used for showing the shading rate image.
  98. AZ::RHI::ConstPtr<AZ::RHI::PipelineState> m_imagePipelineState;
  99. // Compute and graphics shaders.
  100. AZStd::vector<AZ::Data::Instance<AZ::RPI::Shader>> m_shaders;
  101. // SRG with information when rendering the full screen quad.
  102. AZ::Data::Instance<AZ::RPI::ShaderResourceGroup> m_modelShaderResourceGroup;
  103. // SRG with information when updating the shading rate iamge.
  104. AZ::Data::Instance<AZ::RPI::ShaderResourceGroup> m_computeShaderResourceGroup;
  105. // SRG with information when displaying the shading rate image.
  106. AZ::Data::Instance<AZ::RPI::ShaderResourceGroup> m_imageShaderResourceGroup;
  107. // Indices into the SRGs for properties that are updated.
  108. AZ::RHI::ShaderInputConstantIndex m_centerIndex;
  109. AZ::RHI::ShaderInputImageIndex m_shadingRateIndex;
  110. AZ::RHI::ShaderInputImageIndex m_shadingRateDisplayIndex;
  111. // Size of the shading rate image tile (in pixels)
  112. AZ::Vector2 m_shadingRateImageSize;
  113. int m_numThreadsX = 8;
  114. int m_numThreadsY = 8;
  115. int m_numThreadsZ = 1;
  116. // Bufferpool for creating the IA buffer
  117. AZ::RHI::Ptr<AZ::RHI::BufferPool> m_bufferPool;
  118. // Buffer for the IA of the full screen quad.
  119. AZ::RHI::Ptr<AZ::RHI::Buffer> m_inputAssemblyBuffer;
  120. // Geometry view for the full screen quad.
  121. AZ::RHI::GeometryView m_geometryView;
  122. // Layout of the full screen quad.
  123. AZ::RHI::InputStreamLayout m_inputStreamLayout;
  124. // Image pool containing the shading rate images.
  125. AZ::RHI::Ptr<AZ::RHI::ImagePool> m_imagePool;
  126. // List of shading rate images used as attachments.
  127. AZStd::fixed_vector<AZ::RHI::Ptr<AZ::RHI::Image>, AZ::RHI::Limits::Device::FrameCountMax> m_shadingRateImages;
  128. // Cursor position (mouse or touch)
  129. AZ::Vector2 m_cursorPos;
  130. // Selected format to be used for the shading rate image.
  131. AZ::RHI::Format m_rateShadingImageFormat = AZ::RHI::Format::Unknown;
  132. // Frame counter for selecting the proper shading rate image.
  133. uint32_t m_frameCount = 0;
  134. // List of supported shading rate values.
  135. AZStd::fixed_vector<AZ::RHI::ShadingRate, static_cast<uint32_t>(AZ::RHI::ShadingRate::Count)> m_supportedModes;
  136. };
  137. } // namespace AtomSampleViewer