MaterialHotReloadTestComponent.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
  3. * its licensors.
  4. *
  5. * For complete copyright and license terms please see the LICENSE at the root of this
  6. * distribution (the "License"). All use of this software is governed by the License,
  7. * or, if provided, by the license below or the license accompanying this file. Do not
  8. * remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. *
  11. */
  12. #pragma once
  13. #include <CommonSampleComponentBase.h>
  14. #include <Utils/ImGuiSidebar.h>
  15. #include <Utils/FileIOErrorHandler.h>
  16. #include <AzCore/Component/TickBus.h>
  17. #include <Atom/RPI.Reflect/Material/MaterialAsset.h>
  18. #include <Atom/RPI.Reflect/Model/ModelAsset.h>
  19. #include <AzFramework/Asset/AssetSystemTypes.h>
  20. namespace AtomSampleViewer
  21. {
  22. //! This test renders a simple material and exposes controls that can update the source data for that material and its shaders
  23. //! to demonstrate and test hot-reloading. It works by copying entire files from a test data folder into a material source folder
  24. //! and waiting for the Asset Processor to build the updates files.
  25. class MaterialHotReloadTestComponent final
  26. : public CommonSampleComponentBase
  27. , public AZ::TickBus::Handler
  28. , public AZ::Data::AssetBus::Handler
  29. {
  30. public:
  31. AZ_COMPONENT(MaterialHotReloadTestComponent, "{EA684B21-9E39-4210-A640-AFBC28B2E683}", CommonSampleComponentBase);
  32. MaterialHotReloadTestComponent();
  33. static void Reflect(AZ::ReflectContext* context);
  34. // AZ::Component overrides...
  35. void Activate() override;
  36. void Deactivate() override;
  37. private:
  38. AZ_DISABLE_COPY_MOVE(MaterialHotReloadTestComponent);
  39. // AZ::TickBus::Handler overrides...
  40. void OnTick(float deltaTime, AZ::ScriptTimePoint scriptTime) override;
  41. // Finds the paths m_testDataFolder and m_tempSourceFolder
  42. void InitTestDataFolders();
  43. // Deletes a file from m_tempSourceFolder
  44. void DeleteTestFile(const char* tempSourceFile);
  45. // Copies a file from m_testDataFolder to m_tempSourceFolder
  46. void CopyTestFile(const AZStd::string& testDataFile, const AZStd::string& tempSourceFile);
  47. // Returns the AssetStatus of a file in m_tempSourceFolder
  48. AzFramework::AssetSystem::AssetStatus GetTestAssetStatus(const char* tempSourceFile) const;
  49. // Draws ImGui indicating the Asset Processor status of a file in m_tempSourceFolder
  50. void DrawAssetStatus(const char* tempSourceFile, bool includeFileName = false);
  51. AZ::Data::AssetId GetAssetId(const char* productFilePath);
  52. void OnAssetReady(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  53. enum class ShaderVariantStatus
  54. {
  55. None,
  56. Root,
  57. PartiallyBaked,
  58. FullyBaked
  59. };
  60. ShaderVariantStatus GetShaderVariantStatus() const;
  61. static constexpr float LongTimeout = 30.0f;
  62. // Tracks initialization that starts when the component is activated
  63. enum class InitStatus
  64. {
  65. None,
  66. ClearingTestAssets,
  67. CopyingDefaultAzslTestFile,
  68. CopyingDefaultShaderTestFile,
  69. CopyingDefaultMaterialTypeTestFile,
  70. WaitingForDefaultMaterialToRegister,
  71. WaitingForDefaultMaterialToLoad,
  72. Ready
  73. };
  74. InitStatus m_initStatus = InitStatus::None;
  75. AZStd::string m_testDataFolder; //< Stores several txt files with contents to be copied over various source asset files.
  76. AZStd::string m_tempSourceFolder; //< Folder for temp source asset files. These are what the sample edits and reloads.
  77. ImGuiSidebar m_imguiSidebar;
  78. AZ::Render::MeshFeatureProcessorInterface* m_meshFeatureProcessor = nullptr;
  79. AZ::Data::Asset<AZ::RPI::MaterialAsset> m_materialAsset;
  80. AZ::Data::Instance<AZ::RPI::Material> m_material;
  81. AZ::Data::Asset<AZ::RPI::ModelAsset> m_modelAsset;
  82. AZ::Render::MeshFeatureProcessorInterface::MeshHandle m_meshHandle;
  83. AZ::Render::MeshFeatureProcessorInterface::ModelChangedEvent::Handler m_meshChangedHandler;
  84. // These are used to render a secondary mesh that indicates which shader variant is being used to render the primary mesh
  85. AZ::Transform m_shaderVariantIndicatorMeshTransform;
  86. AZ::Render::MeshFeatureProcessorInterface::MeshHandle m_shaderVariantIndicatorMeshHandle;
  87. AZ::Data::Instance<AZ::RPI::Material> m_shaderVariantIndicatorMaterial_root;
  88. AZ::Data::Instance<AZ::RPI::Material> m_shaderVariantIndicatorMaterial_fullyBaked;
  89. AZ::Data::Instance<AZ::RPI::Material> m_shaderVariantIndicatorMaterial_current;
  90. FileIOErrorHandler m_fileIoErrorHandler;
  91. };
  92. } // namespace AtomSampleViewer