ShaderReloadTestComponent.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 <Atom/Bootstrap/DefaultWindowBus.h>
  10. #include <Atom/Feature/ImGui/ImGuiUtils.h>
  11. #include <AzCore/Component/Component.h>
  12. #include <AzCore/Component/EntityBus.h>
  13. #include <AzCore/Component/TickBus.h>
  14. #include <AzFramework/Entity/EntityContextBus.h>
  15. #include <Atom/Utils/AssetCollectionAsyncLoader.h>
  16. #include <Utils/ImGuiSidebar.h>
  17. #include <Utils/Utils.h>
  18. namespace AtomSampleViewer
  19. {
  20. // This example component updates (upon user, or script input) the shader that is being used
  21. // to render a FullscreenTrianglePass, with the purpose on validating that the
  22. // shader reload notification events work properly.
  23. class ShaderReloadTestComponent final
  24. : public AZ::Component
  25. , public AZ::Render::Bootstrap::DefaultWindowNotificationBus::Handler
  26. , public AZ::TickBus::Handler
  27. {
  28. public:
  29. AZ_COMPONENT(ShaderReloadTestComponent, "{47540623-2BB6-4A56-A013-760D5CDAD748}");
  30. static void Reflect(AZ::ReflectContext* context);
  31. ShaderReloadTestComponent();
  32. ~ShaderReloadTestComponent() override = default;
  33. void Activate() override;
  34. void Deactivate() override;
  35. private:
  36. static constexpr char LogName[] = "ShaderReloadTest";
  37. static constexpr char RedShaderFile[] = "Fullscreen_RED.azsl";
  38. static constexpr char GreenShaderFile[] = "Fullscreen_GREEN.azsl";
  39. static constexpr char BlueShaderFile[] = "Fullscreen_BLUE.azsl";
  40. // The following colors are specified in the format R8G8B8A8_UNORM (DX12).
  41. // Vulkan produces pixels in the format B8G8R8A8_UNORM
  42. static constexpr uint32_t RED_COLOR = 0xFF0000FF;
  43. static constexpr uint32_t GREEN_COLOR = 0xFF00FF00;
  44. static constexpr uint32_t BLUE_COLOR = 0xFFFF0000;
  45. void InitTestDataFolders();
  46. void CopyTestFile(const char * originalName, const char * newName, bool replaceIfExists = true);
  47. void DeleteTestFile(const char* tempSourceFile);
  48. // AZ::Component overrides...
  49. bool ReadInConfig(const AZ::ComponentConfig* baseConfig) override;
  50. // DefaultWindowNotificationBus::Handler overrides...
  51. void DefaultWindowCreated() override;
  52. // AZ::TickBus::Handler overrides...
  53. void OnTick(float deltaTime, AZ::ScriptTimePoint timePoint) override;
  54. void PreloadFullscreenShader();
  55. void OnAllAssetsReadyActivate();
  56. void ActivateFullscreenTrianglePipeline();
  57. void DeactivateFullscreenTrianglePipeline();
  58. // draw debug menu
  59. void DrawSidebar();
  60. // Starts the process of capturing the render output attachment.
  61. // Returns true if the request was successfully enqueued.
  62. bool StartRenderOutputCapture();
  63. // Reads an 0xAABBGGRR pixel from the input buffer.
  64. uint32_t ReadPixel(const uint8_t* rawRGBAPixelData, const AZ::RHI::ImageDescriptor& imageDescriptor, uint32_t x, uint32_t y) const;
  65. // Validates if the given color is the expected color, and prepares for another
  66. // render output capture and validation.
  67. void ValidatePixelColor(uint32_t color);
  68. //! Async asset load. Used to guarantee that "Fullscreen.azshader" exists before
  69. //! instantiating the FullscreenTriangle.pass.
  70. AZ::AssetCollectionAsyncLoader m_assetLoadManager;
  71. bool m_initialized = false;
  72. AZStd::string m_relativeTestDataFolder; //< Stores several txt files with contents to be copied over various source asset files.
  73. AZStd::string m_relativeTempSourceFolder; //< Folder for temp source asset files. These are what the sample edits and reloads.
  74. AZStd::string m_absoluteTestDataFolder; //< Stores several txt files with contents to be copied over various source asset files.
  75. AZStd::string m_absoluteTempSourceFolder; //< Folder for temp source asset files. These are what the sample edits and reloads.
  76. bool m_isCapturingRenderOutput = false;
  77. uint32_t m_expectedPixelColor;
  78. AZStd::string m_capturedColorAsString;
  79. AZ::RPI::Scene* m_scene = nullptr;
  80. AZ::RPI::RenderPipelinePtr m_cbPipeline;
  81. AZ::RPI::RenderPipelinePtr m_originalPipeline;
  82. AZStd::shared_ptr<AZ::RPI::WindowContext> m_windowContext;
  83. AZStd::vector<AZStd::string> m_passHierarchy; // Used to capture the CopyToSwapChain pass output.
  84. // debug menu
  85. ImGuiSidebar m_imguiSidebar;
  86. AZ::Render::ImGuiActiveContextScope m_imguiScope;
  87. };
  88. } // namespace AtomSampleViewer