HighInstanceExampleComponent.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 <EntityLatticeTestComponent.h>
  10. #include <Utils/ImGuiSidebar.h>
  11. #include <Utils/ImGuiAssetBrowser.h>
  12. #include <AzCore/Component/TickBus.h>
  13. #include <AzCore/Math/Random.h>
  14. #include <AzCore/Math/Vector3.h>
  15. #include <AzCore/Math/Color.h>
  16. #include <AzCore/std/containers/vector.h>
  17. #include <Atom/Feature/CoreLights/DirectionalLightFeatureProcessorInterface.h>
  18. #include <Atom/Feature/CoreLights/ShadowConstants.h>
  19. #include <Atom/Feature/CoreLights/DiskLightFeatureProcessorInterface.h>
  20. namespace AtomSampleViewer
  21. {
  22. //! This class is used as base of a set of simple cpu performance stress test for Atom.
  23. //! This test loads a X*Y*Z lattice of entities with randomized meshes and materials and creates N shadow casting spotlights plus 0-1 Directional lights
  24. struct HighInstanceTestParameters
  25. {
  26. int m_latticeSize[3] = {22, 22, 22};
  27. float m_latticeSpacing[3] = {5.0f, 5.0f, 5.0f};
  28. float m_entityScale = 1.0f;
  29. AZ::Render::ShadowmapSize m_shadowmapSize = AZ::Render::ShadowmapSize::Size256;
  30. AZ::Render::ShadowFilterMethod m_shadowFilterMethod = AZ::Render::ShadowFilterMethod::None;
  31. int m_numShadowCastingSpotLights = 0;
  32. float m_shadowSpotlightInnerAngleDeg = 10.0f;
  33. float m_shadowSpotlightOuterAngleDeg = 30.0f;
  34. float m_shadowSpotlightMaxDistance = 200.0f;
  35. float m_shadowSpotlightIntensity = 500.f; // Value in Candela
  36. bool m_activateDirectionalLight = false;
  37. uint16_t m_numDirectionalLightShadowCascades = 4;
  38. float m_directionalLightIntensity = 5.0f; // Value in Lux
  39. float m_cameraPosition[3] = {0.0f, 0.0f, 0.0f};
  40. float m_cameraHeadingDeg = -44.7f;
  41. float m_cameraPitchDeg = 25.0f;
  42. float m_iblExposure = 0.0f;
  43. };
  44. class HighInstanceTestComponent
  45. : public EntityLatticeTestComponent
  46. , public AZ::TickBus::Handler
  47. {
  48. using Base = EntityLatticeTestComponent;
  49. public:
  50. AZ_RTTI(HighInstanceTestComponent, "{DAA2B63B-7CC0-4696-A44F-49E53C6390B9}", EntityLatticeTestComponent);
  51. static void Reflect(AZ::ReflectContext* context);
  52. HighInstanceTestComponent();
  53. //! AZ::Component overrides...
  54. void Activate() override;
  55. void Deactivate() override;
  56. private:
  57. AZ_DISABLE_COPY_MOVE(HighInstanceTestComponent);
  58. // CommonSampleComponentBase overrides...
  59. void OnAllAssetsReadyActivate() override;
  60. //! EntityLatticeTestComponent overrides...
  61. void PrepareCreateLatticeInstances(uint32_t instanceCount) override;
  62. void CreateLatticeInstance(const AZ::Transform& transform) override;
  63. void FinalizeLatticeInstances() override;
  64. void DestroyLatticeInstances() override;
  65. void DestroyLights();
  66. void DestroyHandles();
  67. AZ::Data::AssetId GetRandomModelId() const;
  68. AZ::Data::AssetId GetRandomMaterialId() const;
  69. void OnTick(float deltaTime, AZ::ScriptTimePoint scriptTime) override;
  70. void ResetNoClipController();
  71. void SaveCameraConfiguration();
  72. void RestoreCameraConfiguration();
  73. const AZ::Color& GetNextLightColor();
  74. AZ::Vector3 GetRandomDirection();
  75. void BuildDiskLightParameters();
  76. void CreateSpotLights();
  77. void CreateSpotLight(int index);
  78. void DrawDiskLightDebugObjects();
  79. void CreateDirectionalLight();
  80. protected:
  81. HighInstanceTestParameters m_testParameters;
  82. private:
  83. struct ModelInstanceData
  84. {
  85. AZ::Transform m_transform;
  86. AZ::Data::AssetId m_modelAssetId;
  87. AZ::Data::AssetId m_materialAssetId;
  88. AZ::Render::MeshFeatureProcessorInterface::MeshHandle m_meshHandle;
  89. };
  90. ImGuiSidebar m_imguiSidebar;
  91. ImGuiAssetBrowser m_materialBrowser;
  92. ImGuiAssetBrowser m_modelBrowser;
  93. AZStd::vector<ModelInstanceData> m_modelInstanceData;
  94. struct Compare
  95. {
  96. bool operator()(const AZ::Data::Asset<AZ::RPI::MaterialAsset>& lhs, const AZ::Data::Asset<AZ::RPI::MaterialAsset>& rhs) const
  97. {
  98. if (lhs.GetId().m_guid == rhs.GetId().m_guid)
  99. {
  100. return lhs.GetId().m_subId > rhs.GetId().m_subId;
  101. }
  102. return lhs.GetId().m_guid > rhs.GetId().m_guid;
  103. }
  104. };
  105. using MaterialAssetSet = AZStd::set<AZ::Data::Asset<AZ::RPI::MaterialAsset>, Compare>;
  106. MaterialAssetSet m_cachedMaterials;
  107. uint32_t m_pinnedMaterialCount = 0;
  108. uint32_t m_preActivateVSyncInterval = 0;
  109. AZ::SimpleLcgRandom m_random;
  110. AZStd::vector<AZStd::string> m_expandedModelList; // has models that are more expensive on the gpu
  111. AZStd::vector<AZStd::string> m_simpleModelList; // Aims to keep the test cpu bottlenecked by using trivial geometry such as a cube
  112. size_t m_lastPinnedModelCount = 0;
  113. float m_originalFarClipDistance;
  114. bool m_updateTransformEnabled = false;
  115. bool m_useSimpleModels = true;
  116. // light settings
  117. using DirectionalLightHandle = AZ::Render::DirectionalLightFeatureProcessorInterface::LightHandle;
  118. using DiskLightHandle = AZ::Render::DiskLightFeatureProcessorInterface::LightHandle;
  119. class DiskLight
  120. {
  121. public:
  122. DiskLight() = delete;
  123. explicit DiskLight(
  124. const AZ::Color& color,
  125. const AZ::Vector3& relativePosition,
  126. AZ::Render::ShadowmapSize shadowmapSize)
  127. : m_color{ color }
  128. , m_relativePosition{ relativePosition }
  129. , m_shadowmapSize{ shadowmapSize }
  130. {}
  131. ~DiskLight() = default;
  132. const AZ::Color m_color;
  133. const AZ::Vector3 m_relativePosition;
  134. const AZ::Render::ShadowmapSize m_shadowmapSize;
  135. DiskLightHandle m_handle;
  136. };
  137. static constexpr float CutoffIntensity = 0.1f;
  138. AZ::Render::DirectionalLightFeatureProcessorInterface* m_directionalLightFeatureProcessor = nullptr;
  139. AZ::Render::DiskLightFeatureProcessorInterface* m_diskLightFeatureProcessor = nullptr;
  140. DirectionalLightHandle m_directionalLightHandle;
  141. AZStd::vector<DiskLight> m_diskLights;
  142. bool m_drawDiskLightDebug = false;
  143. bool m_diskLightsEnabled = true; // combined with test parameters to determine final state.
  144. bool m_directionalLightEnabled = true; // combined with test parameters to determine final state.
  145. };
  146. } // namespace AtomSampleViewer