StreamingImageExampleComponent.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. void Reset()
  54. {
  55. m_assetId = AZ::Data::AssetId{};
  56. m_asset.Reset();
  57. m_image.reset();
  58. m_srg.reset();
  59. }
  60. };
  61. struct Image3dToDraw
  62. {
  63. AZ::Data::Instance<AZ::RPI::StreamingImage> m_image;
  64. AZ::Data::Instance<AZ::RPI::ShaderResourceGroup> m_srg;
  65. uint32_t m_sliceCount;
  66. };
  67. // AZ::TickBus::Handler...
  68. void OnTick(float deltaTime, AZ::ScriptTimePoint timePoint) override;
  69. // AssetBus::Handler...
  70. /// Used to accept image mip chain asset events.
  71. void OnAssetReady(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  72. void OnAssetReloaded(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  73. // AssetCatalogEventBus::Handler
  74. void OnCatalogAssetAdded(const AZ::Data::AssetId& /*assetId*/) override;
  75. void OnCatalogAssetChanged(const AZ::Data::AssetId& /*assetId*/) override;
  76. // Draw profiling data with Imgui
  77. void DisplayStreamingProfileData();
  78. // Submit draw packages for each streaming image
  79. void DrawImages();
  80. // Submit draw packages for each 3d streaming image
  81. void Draw3dImages();
  82. // Submit draw package for one image
  83. void DrawImage(ImageToDraw* imageInfo);
  84. // Creates resources, resource views, pipeline state, etc. for rendering
  85. void PrepareRenderData();
  86. void SwitchHotReloadImage();
  87. void DeleteHotReloadImage();
  88. // Create 3d images from Cpu Data and prepare them for draw
  89. void Create3dimages();
  90. // Helper function to get total file size of a streaming image asset and its sub assets
  91. AZ::u64 GetImageAssetSize(AZ::RPI::StreamingImage* image);
  92. // Copy the sourceFile and to destFile. If the destFile already exists, overwrite it.
  93. bool CopyFile(const AZStd::string& destFile, const AZStd::string& sourceFile);
  94. void QueueForLoad(const AZStd::string& filePath);
  95. //[GFX TODO][ATOM-4040] PAL-ify AtomSampleViewer. We disable m_enableHotReloadTest for following platforms as
  96. //they dont have access to the root dev folder, only the asset cache folder and the hot reload
  97. //logic cant work without it. This is because these platforms use AtomSampleViewerLauncher instead of AtomSampleViewerApplication
  98. static bool IsHotReloadTestSupported();
  99. const static uint32_t TestDDSCount = 36; // For image streaming and profile
  100. const static uint32_t TestPNGCount = 4; // For non-power-of-two textures
  101. const AZ::IO::Path TestImageFolder = "Textures/Streaming";
  102. const AZ::IO::Path ReloadTestImageName = "reloadtest.png";
  103. // Constants of display area for showing all streaming images.
  104. // As reference, the window's left bottom is (-1, -1). The window size is (2, 2)
  105. const float AreaWidth = 1.5f;
  106. const float AreaHeight = 1.9f;
  107. const float AreaBottom = -1.0f;
  108. const float AreaLeft = -1.0f;
  109. uint32_t m_numImageAssetQueued = 0; // The number of StreamingImageAsset were queued to load
  110. uint32_t m_numImageCreated = 0; // The number of StreamingImage objects were created
  111. uint32_t m_numImageStreamed = 0; // The number of StreamingImages are fully streamed in.
  112. // images for streaming profiling
  113. AZStd::vector<ImageToDraw> m_images;
  114. // image for hot reloading test
  115. ImageToDraw m_imageHotReload;
  116. int m_curSourceImage = 0;
  117. bool m_enableHotReloadTest = true;
  118. AZ::Data::AssetId m_reloadingAsset;
  119. // images for 3D streaming
  120. AZStd::vector<Image3dToDraw> m_3dImages;
  121. bool m_viewStreamedImages = true;
  122. // profile data
  123. AZ::u64 m_loadImageStart = 0;
  124. AZ::u64 m_loadImageEnd = 0;
  125. // The total time of create all the images (include upload their tail mipmaps to device)
  126. AZ::u64 m_createImageTime = 0;
  127. // The first time when all image were streamed to their target mip
  128. AZ::u64 m_streamingImageEnd = 0;
  129. // The total size of all the .streamingimage assets
  130. AZ::u64 m_initialImageAssetSize = 0;
  131. // The total size of all the streaming image assets as well as their mipchain assets
  132. AZ::u64 m_imageAssetSize = 0;
  133. ImGuiSidebar m_imguiSidebar;
  134. // Cache the DynamicDraw Interface
  135. AZ::RPI::DynamicDrawInterface* m_dynamicDraw = nullptr;
  136. // render related data
  137. AZ::RHI::ConstPtr<AZ::RHI::PipelineState> m_pipelineState;
  138. AZ::RHI::ConstPtr<AZ::RHI::PipelineState> m_image3dPipelineState;
  139. AZ::RHI::DrawListTag m_drawListTag;
  140. AZ::RHI::DrawListTag m_image3dDrawListTag;
  141. AZ::Data::Asset<AZ::RPI::ShaderAsset> m_shaderAsset;
  142. AZ::RHI::Ptr<AZ::RHI::ShaderResourceGroupLayout> m_srgLayout;
  143. AZ::Data::Asset<AZ::RPI::ShaderAsset> m_image3dShaderAsset;
  144. AZ::RHI::Ptr<AZ::RHI::ShaderResourceGroupLayout> m_image3dSrgLayout;
  145. AZ::RHI::GeometryView m_geometryView;
  146. // shader input indices
  147. AZ::RHI::ShaderInputImageIndex m_imageInputIndex;
  148. AZ::RHI::ShaderInputConstantIndex m_positionInputIndex;
  149. AZ::RHI::ShaderInputConstantIndex m_sizeInputIndex;
  150. AZ::RHI::ShaderInputConstantIndex m_residentMipInputIndex;
  151. AzFramework::WindowSize m_windowSize;
  152. // for pause or resume script automation
  153. bool m_automationPaused = false;
  154. // save previous settings which need to be recovered when exit the sample.
  155. size_t m_cachedPoolBudget = 0;
  156. int16_t m_cachedMipBias = 0;
  157. };
  158. } // namespace AtomSampleViewer