StreamingImageExampleComponent.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 <Atom/RHI/IndexBufferView.h>
  11. #include <Atom/RHI/PipelineState.h>
  12. #include <Atom/RHI/DrawList.h>
  13. #include <Atom/RPI.Public/DynamicDraw/DynamicDrawInterface.h>
  14. #include <Atom/RPI.Public/Image/StreamingImage.h>
  15. #include <Atom/RPI.Public/Shader/ShaderResourceGroup.h>
  16. #include <AzFramework/Asset/AssetCatalogBus.h>
  17. #include <AzCore/IO/Path/Path.h>
  18. #include <AzCore/Component/TickBus.h>
  19. #include <Utils/ImGuiSidebar.h>
  20. namespace AtomSampleViewer
  21. {
  22. // This AtomSampleViewer example is to test, profile and visualize StreamingImage streaming process as well as
  23. // testing the StreamingImage hot reloading function.
  24. // It starts loading 36 StreamingImageAssets and create the StreamingImages when each asset is ready.
  25. // After a StreamingImage is created, it will be drawn on the screen with all the its mips.
  26. // The mips which are not streamed in are showing are white blocks.
  27. // When all StreamingImages' mipmaps are streamed in, a profile result would be showing on the screen.
  28. // For StreamingImage hot reloading test, the example will add a new image file in the
  29. // AtomSampleViewer project asset's texture/streaming/ folder.
  30. // The file will be loaded and displayed on the top right side of screen.
  31. // A switch button under it will overwrite the image with another one. When the changed image got processed by AP,
  32. // the new content will be rendered on the screen.
  33. class StreamingImageExampleComponent final
  34. : public CommonSampleComponentBase
  35. , public AZ::Data::AssetBus::MultiHandler
  36. , public AZ::TickBus::Handler
  37. , public AzFramework::AssetCatalogEventBus::Handler
  38. {
  39. public:
  40. AZ_COMPONENT(StreamingImageExampleComponent, "{9AF86786-8B14-4C3E-92CD-3152768E417C}", CommonSampleComponentBase);
  41. static void Reflect(AZ::ReflectContext* context);
  42. StreamingImageExampleComponent() = default;
  43. ~StreamingImageExampleComponent() override = default;
  44. void Activate() override;
  45. void Deactivate() override;
  46. private:
  47. struct ImageToDraw
  48. {
  49. AZ::Data::AssetId m_assetId;
  50. AZ::Data::Asset<AZ::RPI::StreamingImageAsset> m_asset;
  51. AZ::Data::Instance<AZ::RPI::StreamingImage> m_image;
  52. AZ::Data::Instance<AZ::RPI::ShaderResourceGroup> m_srg;
  53. bool m_wasStreamed = false;
  54. void Reset()
  55. {
  56. m_assetId = AZ::Data::AssetId{};
  57. m_asset.Reset();
  58. m_image.reset();
  59. m_srg.reset();
  60. m_wasStreamed = false;
  61. }
  62. };
  63. struct Image3dToDraw
  64. {
  65. AZ::Data::Instance<AZ::RPI::StreamingImage> m_image;
  66. AZ::Data::Instance<AZ::RPI::ShaderResourceGroup> m_srg;
  67. uint32_t m_sliceCount;
  68. };
  69. // AZ::TickBus::Handler...
  70. void OnTick(float deltaTime, AZ::ScriptTimePoint timePoint) override;
  71. // AssetBus::Handler...
  72. /// Used to accept image mip chain asset events.
  73. void OnAssetReady(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  74. void OnAssetReloaded(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  75. // AssetCatalogEventBus::Handler
  76. void OnCatalogAssetAdded(const AZ::Data::AssetId& /*assetId*/) override;
  77. void OnCatalogAssetChanged(const AZ::Data::AssetId& /*assetId*/) override;
  78. // Draw profiling data with Imgui
  79. void DisplayStreamingProfileData();
  80. // Submit draw packages for each streaming image
  81. void DrawImages();
  82. // Submit draw packages for each 3d streaming image
  83. void Draw3dImages();
  84. // Submit draw package for one image
  85. void DrawImage(const ImageToDraw* imageInfo);
  86. // Creates resources, resource views, pipeline state, etc. for rendering
  87. void PrepareRenderData();
  88. void SwitchHotReloadImage();
  89. void DeleteHotReloadImage();
  90. // Create 3d images from Cpu Data and prepare them for draw
  91. void Create3dimages();
  92. // Helper function to get total file size of a streaming image asset and its sub assets
  93. AZ::u64 GetImageAssetSize(AZ::RPI::StreamingImage* image);
  94. // Copy the sourceFile and to destFile. If the destFile already exists, overwrite it.
  95. bool CopyFile(const AZStd::string& destFile, const AZStd::string& sourceFile);
  96. void QueueForLoad(const AZStd::string& filePath);
  97. //[GFX TODO][ATOM-4040] PAL-ify AtomSampleViewer. We disable m_enableHotReloadTest for following platforms as
  98. //they dont have access to the root dev folder, only the asset cache folder and the hot reload
  99. //logic cant work without it. This is because these platforms use AtomSampleViewerLauncher instead of AtomSampleViewerApplication
  100. static bool IsHotReloadTestSupported();
  101. const static uint32_t TestDDSCount = 36; // For image streaming and profile
  102. const static uint32_t TestPNGCount = 4; // For non-power-of-two textures
  103. const AZ::IO::Path TestImageFolder = "Textures/Streaming";
  104. const AZ::IO::Path ReloadTestImageName = "reloadtest.png";
  105. // Constants of display area for showing all streaming images.
  106. // As reference, the window's left bottom is (-1, -1). The window size is (2, 2)
  107. const float AreaWidth = 1.5f;
  108. const float AreaHeight = 1.9f;
  109. const float AreaBottom = -1.0f;
  110. const float AreaLeft = -1.0f;
  111. uint32_t m_numImageAssetQueued = 0; // The number of StreamingImageAsset were queued to load
  112. uint32_t m_numImageCreated = 0; // The number of StreamingImage objects were created
  113. uint32_t m_numImageStreamed = 0; // The number of StreamingImages are fully streamed in.
  114. // images for streaming profiling
  115. AZStd::vector<ImageToDraw> m_images;
  116. // image for hot reloading test
  117. ImageToDraw m_imageHotReload;
  118. int m_curSourceImage = 0;
  119. bool m_enableHotReloadTest = true;
  120. AZ::Data::AssetId m_reloadingAsset;
  121. // images for 3D streaming
  122. AZStd::vector<Image3dToDraw> m_3dImages;
  123. bool m_viewStreamedImages = true;
  124. // profile data
  125. AZ::u64 m_loadImageStart = 0;
  126. AZ::u64 m_loadImageEnd = 0;
  127. AZ::u64 m_createImageTime = 0;
  128. AZ::u64 m_streamingImageEnd = 0;
  129. AZ::u64 m_initialImageMemory = 0;
  130. AZ::u64 m_imageMemory = 0;
  131. ImGuiSidebar m_imguiSidebar;
  132. // Cache the DynamicDraw Interface
  133. AZ::RPI::DynamicDrawInterface* m_dynamicDraw = nullptr;
  134. // render related data
  135. AZ::RHI::ConstPtr<AZ::RHI::PipelineState> m_pipelineState;
  136. AZ::RHI::ConstPtr<AZ::RHI::PipelineState> m_image3dPipelineState;
  137. AZ::RHI::DrawListTag m_drawListTag;
  138. AZ::RHI::DrawListTag m_image3dDrawListTag;
  139. AZ::Data::Asset<AZ::RPI::ShaderAsset> m_shaderAsset;
  140. AZ::RHI::Ptr<AZ::RHI::ShaderResourceGroupLayout> m_srgLayout;
  141. AZ::Data::Asset<AZ::RPI::ShaderAsset> m_image3dShaderAsset;
  142. AZ::RHI::Ptr<AZ::RHI::ShaderResourceGroupLayout> m_image3dSrgLayout;
  143. // shader input indices
  144. AZ::RHI::ShaderInputImageIndex m_imageInputIndex;
  145. AZ::RHI::ShaderInputConstantIndex m_positionInputIndex;
  146. AZ::RHI::ShaderInputConstantIndex m_sizeInputIndex;
  147. AZ::RHI::ShaderInputConstantIndex m_residentMipInputIndex;
  148. AzFramework::WindowSize m_windowSize;
  149. // for pause or resume script automation
  150. bool m_automationPaused = false;
  151. };
  152. } // namespace AtomSampleViewer