SubpassExampleComponent.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/Shader.h>
  13. #include <Atom/RPI.Public/Shader/ShaderResourceGroup.h>
  14. #include <Atom/RPI.Public/Model/Model.h>
  15. #include <Atom/RPI.Public/Model/ModelLod.h>
  16. #include <Atom/RHI/BufferPool.h>
  17. #include <Atom/RHI/DrawItem.h>
  18. #include <Atom/RHI/Device.h>
  19. #include <Atom/RHI/Factory.h>
  20. #include <Atom/RHI/FrameScheduler.h>
  21. #include <Atom/RHI/PipelineState.h>
  22. #include <Atom/RHI/RHISystemInterface.h>
  23. #include <AzFramework/Entity/EntityContextBus.h>
  24. #include <RHI/BasicRHIComponent.h>
  25. #include <Utils/ImGuiSidebar.h>
  26. #include <ExampleComponentBus.h>
  27. namespace AtomSampleViewer
  28. {
  29. //! Example that showcases the use of SubpassInputs.
  30. //! The example implements a simple deferred renderer with two passes.
  31. //! The first one is a Gbuffer pass that writes to 3 render targets: position, normal and albedo.
  32. //! The second pass is the composition one that reads from the 3 previous render targets as subpassInputs
  33. //! and then outputs the correct lighting of the scene to the swapchain.
  34. class SubpassExampleComponent final
  35. : public BasicRHIComponent
  36. , public ExampleComponentRequestBus::Handler
  37. {
  38. public:
  39. AZ_COMPONENT(SubpassExampleComponent, "{931B2CA5-5718-4DED-B2BE-262DEA0586B5}", AZ::Component);
  40. AZ_DISABLE_COPY(SubpassExampleComponent);
  41. static void Reflect(AZ::ReflectContext* context);
  42. SubpassExampleComponent();
  43. ~SubpassExampleComponent() override = default;
  44. private:
  45. // Scope types
  46. enum InputAttachmentScopes
  47. {
  48. GBufferScope,
  49. CompositionScope,
  50. NumScopes
  51. };
  52. enum ModelType
  53. {
  54. ModelType_Plane = 0,
  55. ModelType_ShaderBall,
  56. ModelType_Bunny,
  57. ModelType_Suzanne,
  58. ModelType_Count
  59. };
  60. struct ModelData
  61. {
  62. AZ::RHI::StreamBufferIndices m_streamIndices;
  63. AZ::RHI::ConstPtr<AZ::RHI::PipelineState> m_pipelineState;
  64. AZ::Data::Instance<AZ::RPI::ShaderResourceGroup> m_shaderResourceGroup;
  65. ModelType m_modelType = ModelType_ShaderBall;
  66. };
  67. // AZ::Component
  68. void Activate() override;
  69. void Deactivate() override;
  70. // RHISystemNotificationBus::Handler
  71. void OnFramePrepare(AZ::RHI::FrameGraphBuilder& frameGraphBuilder) override;
  72. // ExampleComponentRequestBus::Handler
  73. void ResetCamera() override;
  74. // AZ::Component
  75. bool ReadInConfig(const AZ::ComponentConfig* baseConfig) override;
  76. void LoadModels();
  77. void LoadShaders();
  78. void CreateShaderResourceGroups();
  79. void CreatePipelines();
  80. void CreateGBufferScope();
  81. void CreateCompositionScope();
  82. void SetupScene();
  83. AZStd::array<AZ::Data::Instance<AZ::RPI::Model>, ModelType_Count> m_models;
  84. AZStd::vector<ModelData> m_opaqueModelsData;
  85. uint32_t m_meshCount = 0;
  86. AZStd::vector<AZ::Data::Instance<AZ::RPI::Shader>> m_shaders;
  87. AZ::RHI::AttachmentId m_albedoAttachmentId;
  88. AZ::RHI::AttachmentId m_normalAttachmentId;
  89. AZ::RHI::AttachmentId m_positionAttachmentId;
  90. AZ::RHI::AttachmentId m_depthStencilAttachmentId;
  91. AZ::RHI::GeometryView m_compositeGeometryView;
  92. AZ::EntityId m_cameraEntityId;
  93. AzFramework::EntityContextId m_entityContextId;
  94. // Camera projection matrix
  95. AZ::Matrix4x4 m_projectionMatrix;
  96. AZ::RHI::ShaderInputConstantIndex m_modelMatrixIndex;
  97. AZ::RHI::ShaderInputConstantIndex m_lightsInfoIndex;
  98. AZ::RHI::ShaderInputImageIndex m_subpassInputPosition;
  99. AZ::RHI::ShaderInputImageIndex m_subpassInputNormal;
  100. AZ::RHI::ShaderInputImageIndex m_subpassInputAlbedo;
  101. AZ::Data::Instance<AZ::RPI::ShaderResourceGroup> m_sceneShaderResourceGroup;
  102. AZ::Data::Instance<AZ::RPI::ShaderResourceGroup> m_compositionSubpassInputsSRG;
  103. AZ::Data::Instance<AZ::RPI::ShaderResourceGroup> m_viewShaderResourceGroup;
  104. AZ::RHI::ConstPtr<AZ::RHI::PipelineState> m_compositionPipeline;
  105. };
  106. } // namespace AtomSampleViewer