ReadbackExampleComponent.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 <CommonSampleComponentBase.h>
  10. #include <AzCore/Component/TickBus.h>
  11. #include <Atom/Bootstrap/DefaultWindowBus.h>
  12. #include <Atom/RPI.Public/Pass/AttachmentReadback.h>
  13. #include <Utils/ImGuiSidebar.h>
  14. #include <Atom/Feature/ImGui/ImGuiUtils.h>
  15. namespace AtomSampleViewer
  16. {
  17. //! --- Readback Test ---
  18. //!
  19. //! This test is designed to test the readback capabilities of ATOM
  20. //! It is built around two custom passes working in tandem. The first
  21. //! one generate and fill a texture with a pattern. Using the RPI::Pass
  22. //! readback capabilities (ReadbackAttachment) it then reads that result
  23. //! back to host memory. Once read back the result is uploaded to device
  24. //! memory to be used as a texture input in the second pass that will
  25. //! display it for operator verification.
  26. class ReadbackExampleComponent final
  27. : public CommonSampleComponentBase
  28. , public AZ::Render::Bootstrap::DefaultWindowNotificationBus::Handler
  29. , public AZ::TickBus::Handler
  30. {
  31. public:
  32. AZ_COMPONENT(ReadbackExampleComponent, "{B4221426-D22B-4C06-AAFD-4EA277B44CC8}", CommonSampleComponentBase);
  33. static void Reflect(AZ::ReflectContext* context);
  34. ReadbackExampleComponent();
  35. ~ReadbackExampleComponent() override = default;
  36. void Activate() override;
  37. void Deactivate() override;
  38. private:
  39. AZ_DISABLE_COPY_MOVE(ReadbackExampleComponent);
  40. // AZ::TickBus::Handler overrides...
  41. void OnTick(float deltaTime, AZ::ScriptTimePoint scriptTime) override;
  42. // AZ::Render::Bootstrap::DefaultWindowNotificationBus overrides ...
  43. void DefaultWindowCreated() override;
  44. void CreatePipeline();
  45. void ActivatePipeline();
  46. void DeactivatePipeline();
  47. void CreatePasses();
  48. void DestroyPasses();
  49. void PassesChanged();
  50. void CreateFillerPass();
  51. void CreatePreviewPass();
  52. void CreateResources();
  53. void PerformReadback();
  54. void ReadbackCallback(const AZ::RPI::AttachmentReadback::ReadbackResult& result);
  55. void UploadReadbackResult() const;
  56. void DrawSidebar();
  57. // Pass used to render the pattern and support the readback operation
  58. AZ::RHI::Ptr<AZ::RPI::Pass> m_fillerPass;
  59. // Pass used to display the readback result back on the screen
  60. AZ::RHI::Ptr<AZ::RPI::Pass> m_previewPass;
  61. // Image used as the readback source
  62. AZ::Data::Instance<AZ::RPI::AttachmentImage> m_readbackImage;
  63. // Image used as the readback result destination
  64. AZ::Data::Instance<AZ::RPI::AttachmentImage> m_previewImage;
  65. // Custom pipeline
  66. AZStd::shared_ptr<AZ::RPI::WindowContext> m_windowContext;
  67. AZ::RPI::RenderPipelinePtr m_readbackPipeline;
  68. AZ::RPI::RenderPipelinePtr m_originalPipeline;
  69. AZ::Render::ImGuiActiveContextScope m_imguiScope;
  70. // Readback
  71. AZStd::shared_ptr<AZ::RPI::AttachmentReadback> m_readback;
  72. // Holder for the host available copy of the readback data
  73. AZStd::shared_ptr<AZStd::vector<uint8_t>> m_resultData;
  74. struct {
  75. AZ::Name m_name;
  76. size_t m_bytesRead;
  77. AZ::RHI::ImageDescriptor m_descriptor;
  78. } m_readbackStat;
  79. bool m_textureNeedsUpdate = false;
  80. ImGuiSidebar m_imguiSidebar;
  81. uint32_t m_resourceWidth = 512;
  82. uint32_t m_resourceHeight = 512;
  83. };
  84. } // namespace AtomSampleViewer