SampleComponentManager.cpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  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. #include <SampleComponentManager.h>
  9. #include <SampleComponentConfig.h>
  10. #include <Atom/Component/DebugCamera/CameraComponent.h>
  11. #include <Atom/Component/DebugCamera/NoClipControllerComponent.h>
  12. #include <Atom/Component/DebugCamera/ArcBallControllerComponent.h>
  13. #include <Atom/Feature/AuxGeom/AuxGeomFeatureProcessor.h>
  14. #include <Atom/Feature/ImGui/ImGuiUtils.h>
  15. #include <Atom/Feature/ImGui/SystemBus.h>
  16. #include <Atom/Feature/Mesh/MeshFeatureProcessor.h>
  17. #include <Atom/Feature/PostProcessing/PostProcessingConstants.h>
  18. #include <Atom/Feature/SkinnedMesh/SkinnedMeshInputBuffers.h>
  19. #include <Atom/RPI.Public/Pass/Pass.h>
  20. #include <Atom/RPI.Public/Pass/ParentPass.h>
  21. #include <Atom/RPI.Public/Pass/PassSystemInterface.h>
  22. #include <Atom/RPI.Public/Pass/RenderPass.h>
  23. #include <Atom/RPI.Public/Pass/Specific/SwapChainPass.h>
  24. #include <Atom/RPI.Public/RenderPipeline.h>
  25. #include <Atom/RPI.Public/RPISystemInterface.h>
  26. #include <Atom/RPI.Public/Scene.h>
  27. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  28. #include <Atom/RPI.Reflect/Image/AttachmentImageAsset.h>
  29. #include <Atom/RPI.Reflect/Shader/IShaderVariantFinder.h>
  30. #include <Atom/RHI/Factory.h>
  31. #include <Atom/RHI/RHISystemInterface.h>
  32. #include <Atom/RHI.Reflect/AliasedHeapEnums.h>
  33. #include <Automation/ScriptManager.h>
  34. #include <RHI/AlphaToCoverageExampleComponent.h>
  35. #include <RHI/AsyncComputeExampleComponent.h>
  36. #include <RHI/BasicRHIComponent.h>
  37. #include <RHI/ComputeExampleComponent.h>
  38. #include <RHI/CopyQueueComponent.h>
  39. #include <RHI/DualSourceBlendingComponent.h>
  40. #include <RHI/IndirectRenderingExampleComponent.h>
  41. #include <RHI/InputAssemblyExampleComponent.h>
  42. #include <RHI/SubpassExampleComponent.h>
  43. #include <RHI/MRTExampleComponent.h>
  44. #include <RHI/MSAAExampleComponent.h>
  45. #include <RHI/MultiThreadComponent.h>
  46. #include <RHI/MultiViewportSwapchainComponent.h>
  47. #include <RHI/MultipleViewsComponent.h>
  48. #include <RHI/QueryExampleComponent.h>
  49. #include <RHI/StencilExampleComponent.h>
  50. #include <RHI/SwapchainExampleComponent.h>
  51. #include <RHI/SphericalHarmonicsExampleComponent.h>
  52. #include <RHI/Texture3dExampleComponent.h>
  53. #include <RHI/TextureArrayExampleComponent.h>
  54. #include <RHI/TextureExampleComponent.h>
  55. #include <RHI/TextureMapExampleComponent.h>
  56. #include <RHI/TriangleExampleComponent.h>
  57. #include <RHI/TrianglesConstantBufferExampleComponent.h>
  58. #include <RHI/BindlessPrototypeExampleComponent.h>
  59. #include <RHI/RayTracingExampleComponent.h>
  60. #include <RHI/MatrixAlignmentTestExampleComponent.h>
  61. #include <AreaLightExampleComponent.h>
  62. #include <AssetLoadTestComponent.h>
  63. #include <AuxGeomExampleComponent.h>
  64. #include <BakedShaderVariantExampleComponent.h>
  65. #include <SponzaBenchmarkComponent.h>
  66. #include <BloomExampleComponent.h>
  67. #include <CheckerboardExampleComponent.h>
  68. #include <CullingAndLodExampleComponent.h>
  69. #include <DecalExampleComponent.h>
  70. #include <DepthOfFieldExampleComponent.h>
  71. #include <DynamicDrawExampleComponent.h>
  72. #include <DynamicMaterialTestComponent.h>
  73. #include <MaterialHotReloadTestComponent.h>
  74. #include <ExposureExampleComponent.h>
  75. #include <SceneReloadSoakTestComponent.h>
  76. #include <LightCullingExampleComponent.h>
  77. #include <MeshExampleComponent.h>
  78. #include <MSAA_RPI_ExampleComponent.h>
  79. #include <MultiRenderPipelineExampleComponent.h>
  80. #include <MultiSceneExampleComponent.h>
  81. #include <ParallaxMappingExampleComponent.h>
  82. #include <SceneReloadSoakTestComponent.h>
  83. #include <ShadingExampleComponent.h>
  84. #include <ShadowExampleComponent.h>
  85. #include <ShadowedSponzaExampleComponent.h>
  86. #include <SkinnedMeshExampleComponent.h>
  87. #include <SsaoExampleComponent.h>
  88. #include <StreamingImageExampleComponent.h>
  89. #include <RootConstantsExampleComponent.h>
  90. #include <MultiViewSingleSceneAuxGeomExampleComponent.h>
  91. #include <TonemappingExampleComponent.h>
  92. #include <TransparencyExampleComponent.h>
  93. #include <DiffuseGIExampleComponent.h>
  94. #include <SSRExampleComponent.h>
  95. #include <ShaderReloadTestComponent.h>
  96. #include <Atom/Bootstrap/DefaultWindowBus.h>
  97. #include <AzCore/Component/Entity.h>
  98. #include <AzCore/Debug/Profiler.h>
  99. #include <AzCore/Serialization/SerializeContext.h>
  100. #include <AzCore/std/smart_ptr/make_shared.h>
  101. #include <AzCore/std/algorithm.h>
  102. #include <AzFramework/API/ApplicationAPI.h>
  103. #include <AzFramework/Components/ConsoleBus.h>
  104. #include <AzFramework/Components/TransformComponent.h>
  105. #include <AzFramework/Input/Devices/Keyboard/InputDeviceKeyboard.h>
  106. #include <AzFramework/Entity/GameEntityContextBus.h>
  107. #include <AzFramework/Scene/Scene.h>
  108. #include <AzFramework/Scene/SceneSystemInterface.h>
  109. #include <Passes/RayTracingAmbientOcclusionPass.h>
  110. #include <Utils/Utils.h>
  111. #include <Profiler/ProfilerBus.h>
  112. #include <Profiler/ProfilerImGuiBus.h>
  113. #include "ExampleComponentBus.h"
  114. namespace Platform
  115. {
  116. const char* GetPipelineName();
  117. }
  118. namespace AtomSampleViewer
  119. {
  120. namespace
  121. {
  122. const char* PassTreeToolName = "PassTree";
  123. const char* CpuProfilerToolName = "CPU Profiler";
  124. const char* GpuProfilerToolName = "GPU Profiler";
  125. const char* TransientAttachmentProfilerToolName = "Transient Attachment Profiler";
  126. }
  127. bool IsValidNumMSAASamples(int numSamples)
  128. {
  129. return (numSamples == 1) || (numSamples == 2) || (numSamples == 4) || (numSamples == 8);
  130. }
  131. SampleEntry SampleEntry::NewRHISample(const AZStd::string& name, const AZ::Uuid& uuid)
  132. {
  133. SampleEntry entry;
  134. entry.m_sampleName = name;
  135. entry.m_sampleUuid = uuid;
  136. entry.m_pipelineType = SamplePipelineType::RHI;
  137. return entry;
  138. }
  139. SampleEntry SampleEntry::NewRHISample(const AZStd::string& name, const AZ::Uuid& uuid, AZStd::function<bool()> isSupportedFunction)
  140. {
  141. SampleEntry entry;
  142. entry.m_sampleName = name;
  143. entry.m_sampleUuid = uuid;
  144. entry.m_pipelineType = SamplePipelineType::RHI;
  145. entry.m_isSupportedFunc = isSupportedFunction;
  146. return entry;
  147. }
  148. SampleEntry SampleEntry::NewRPISample(const AZStd::string& name, const AZ::Uuid& uuid)
  149. {
  150. SampleEntry entry;
  151. entry.m_sampleName = name;
  152. entry.m_sampleUuid = uuid;
  153. entry.m_pipelineType = SamplePipelineType::RPI;
  154. return entry;
  155. }
  156. SampleEntry SampleEntry::NewRPISample(const AZStd::string& name, const AZ::Uuid& uuid, AZStd::function<bool()> isSupportedFunction)
  157. {
  158. SampleEntry entry;
  159. entry.m_sampleName = name;
  160. entry.m_sampleUuid = uuid;
  161. entry.m_pipelineType = SamplePipelineType::RPI;
  162. entry.m_isSupportedFunc = isSupportedFunction;
  163. return entry;
  164. }
  165. void SampleComponentManager::Reflect(AZ::ReflectContext* context)
  166. {
  167. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  168. {
  169. serializeContext->Class<SampleComponentManager, AZ::Component>()
  170. ->Version(0)
  171. ;
  172. }
  173. }
  174. void SampleComponentManager::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  175. {
  176. required.push_back(AZ_CRC("PrototypeLmbrCentralService", 0xe35e6de0));
  177. }
  178. void SampleComponentManager::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  179. {
  180. dependent.push_back(AZ_CRC("AzFrameworkConfigurationSystemComponentService", 0xcc49c96e)); // Ensures a scene is created for the GameEntityContext
  181. }
  182. void SampleComponentManager::RegisterSampleComponent(const SampleEntry& sample)
  183. {
  184. if (AZStd::find(m_availableSamples.begin(), m_availableSamples.end(), sample) == m_availableSamples.end())
  185. {
  186. m_availableSamples.push_back(sample);
  187. }
  188. }
  189. SampleComponentManager::SampleComponentManager()
  190. : m_imguiFrameCaptureSaver("@user@/frame_capture.xml")
  191. , m_imGuiFrameTimer(FrameTimeLogSize, FrameTimeLogSize)
  192. {
  193. m_exampleEntity = aznew AZ::Entity();
  194. m_entityContextId = AzFramework::EntityContextId::CreateNull();
  195. memset(m_alphanumericNumbersDown, 0, s_alphanumericCount);
  196. }
  197. SampleComponentManager::~SampleComponentManager()
  198. {
  199. m_exampleEntity = nullptr;
  200. m_cameraEntity = nullptr;
  201. m_windowContext = nullptr;
  202. m_availableSamples.clear();
  203. }
  204. void SampleComponentManager::Init()
  205. {
  206. auto isSupportedFunc = []()
  207. {
  208. return SampleComponentManager::IsMultiViewportSwapchainSampleSupported();
  209. };
  210. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/AlphaToCoverage", azrtti_typeid<AlphaToCoverageExampleComponent>() ));
  211. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/AsyncCompute", azrtti_typeid<AsyncComputeExampleComponent>() ) );
  212. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/BindlessPrototype", azrtti_typeid<BindlessPrototypeExampleComponent>(), []() {return Utils::GetRHIDevice()->GetFeatures().m_unboundedArrays; } ));
  213. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/Compute", azrtti_typeid<ComputeExampleComponent>() ));
  214. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/CopyQueue", azrtti_typeid<CopyQueueComponent>() ));
  215. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/DualSourceBlending", azrtti_typeid<DualSourceBlendingComponent>(), []() {return Utils::GetRHIDevice()->GetFeatures().m_dualSourceBlending; } ));
  216. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/IndirectRendering", azrtti_typeid<IndirectRenderingExampleComponent>(), []() {return Utils::GetRHIDevice()->GetFeatures().m_indirectCommandTier > RHI::IndirectCommandTiers::Tier0; } ));
  217. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/InputAssembly", azrtti_typeid<InputAssemblyExampleComponent>()));
  218. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/MSAA", azrtti_typeid<MSAAExampleComponent>() ));
  219. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/MultipleViews", azrtti_typeid<MultipleViewsComponent>() ));
  220. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/MultiRenderTarget", azrtti_typeid<MRTExampleComponent>() ));
  221. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/MultiThread", azrtti_typeid<MultiThreadComponent>() ));
  222. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/MultiViewportSwapchainComponent", azrtti_typeid<MultiViewportSwapchainComponent>(), isSupportedFunc ));
  223. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/Queries", azrtti_typeid<QueryExampleComponent>() ));
  224. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/RayTracing", azrtti_typeid<RayTracingExampleComponent>(), []() {return Utils::GetRHIDevice()->GetFeatures().m_rayTracing; } ));
  225. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/SphericalHarmonics", azrtti_typeid<SphericalHarmonicsExampleComponent>() ));
  226. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/Stencil", azrtti_typeid<StencilExampleComponent>() ));
  227. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/Subpass", azrtti_typeid<SubpassExampleComponent>(), []() {return Utils::GetRHIDevice()->GetFeatures().m_renderTargetSubpassInputSupport != AZ::RHI::SubpassInputSupportType::NotSupported; } ));
  228. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/Swapchain", azrtti_typeid<SwapchainExampleComponent>() ));
  229. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/Texture", azrtti_typeid<TextureExampleComponent>() ));
  230. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/Texture3d", azrtti_typeid<Texture3dExampleComponent>() ));
  231. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/TextureArray", azrtti_typeid<TextureArrayExampleComponent>() ));
  232. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/TextureMap", azrtti_typeid<TextureMapExampleComponent>() ));
  233. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/Triangle", azrtti_typeid<TriangleExampleComponent>() ));
  234. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/TrianglesConstantBuffer", azrtti_typeid<TrianglesConstantBufferExampleComponent>() ));
  235. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRHISample( "RHI/MatrixAlignmentTest", azrtti_typeid<MatrixAlignmentTestExampleComponent>() ));
  236. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "RPI/AssetLoadTest", azrtti_typeid<AssetLoadTestComponent>() ));
  237. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "RPI/AuxGeom", azrtti_typeid<AuxGeomExampleComponent>() ));
  238. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "RPI/BakedShaderVariant", azrtti_typeid<BakedShaderVariantExampleComponent>() ));
  239. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "RPI/SponzaBenchmark", azrtti_typeid<SponzaBenchmarkComponent>() ));
  240. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "RPI/CullingAndLod", azrtti_typeid<CullingAndLodExampleComponent>() ));
  241. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "RPI/Decals", azrtti_typeid<DecalExampleComponent>() ));
  242. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "RPI/DynamicDraw", azrtti_typeid<DynamicDrawExampleComponent>() ));
  243. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "RPI/DynamicMaterialTest", azrtti_typeid<DynamicMaterialTestComponent>() ));
  244. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "RPI/MaterialHotReloadTest", azrtti_typeid<MaterialHotReloadTestComponent>() ));
  245. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "RPI/Mesh", azrtti_typeid<MeshExampleComponent>() ));
  246. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "RPI/MSAA", azrtti_typeid<MSAA_RPI_ExampleComponent>() ));
  247. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "RPI/MultiRenderPipeline", azrtti_typeid<MultiRenderPipelineExampleComponent>() ));
  248. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "RPI/MultiScene", azrtti_typeid<MultiSceneExampleComponent>() ));
  249. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "RPI/MultiViewSingleSceneAuxGeom", azrtti_typeid<MultiViewSingleSceneAuxGeomExampleComponent>() ));
  250. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "RPI/RootConstants", azrtti_typeid<RootConstantsExampleComponent>() ));
  251. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "RPI/SceneReloadSoakTest", azrtti_typeid<SceneReloadSoakTestComponent>() ));
  252. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "RPI/Shading", azrtti_typeid<ShadingExampleComponent>() ));
  253. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "RPI/StreamingImage", azrtti_typeid<StreamingImageExampleComponent>() ));
  254. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "RPI/ShaderReloadTest", azrtti_typeid<ShaderReloadTestComponent>() ));
  255. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "Features/AreaLight", azrtti_typeid<AreaLightExampleComponent>() ));
  256. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "Features/Bloom", azrtti_typeid<BloomExampleComponent>() ));
  257. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "Features/Checkerboard", azrtti_typeid<CheckerboardExampleComponent>(), []() {return (Utils::GetRHIDevice()->GetPhysicalDevice().GetDescriptor().m_vendorId != RHI::VendorId::ARM && Utils::GetRHIDevice()->GetPhysicalDevice().GetDescriptor().m_vendorId != RHI::VendorId::Qualcomm); } ));
  258. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "Features/DepthOfField", azrtti_typeid<DepthOfFieldExampleComponent>() ));
  259. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "Features/DiffuseGI", azrtti_typeid<DiffuseGIExampleComponent>(), []() {return Utils::GetRHIDevice()->GetFeatures().m_rayTracing; }));
  260. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "Features/Exposure", azrtti_typeid<ExposureExampleComponent>() ));
  261. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "Features/LightCulling", azrtti_typeid<LightCullingExampleComponent>() ));
  262. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "Features/Parallax", azrtti_typeid<ParallaxMappingExampleComponent>() ));
  263. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "Features/Shadow", azrtti_typeid<ShadowExampleComponent>() ));
  264. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "Features/ShadowedSponza", azrtti_typeid<ShadowedSponzaExampleComponent>() ));
  265. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "Features/SSAO", azrtti_typeid<SsaoExampleComponent>()));
  266. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "Features/SSR", azrtti_typeid<SSRExampleComponent>()));
  267. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "Features/Tonemapping", azrtti_typeid<TonemappingExampleComponent>() ));
  268. SampleComponentManager::RegisterSampleComponent(SampleEntry::NewRPISample( "Features/Transparency", azrtti_typeid<TransparencyExampleComponent>() ));
  269. m_scriptManager = AZStd::make_unique<ScriptManager>();
  270. }
  271. void SampleComponentManager::Activate()
  272. {
  273. // We can only initialize this component after the asset catalog has been loaded.
  274. AzFramework::AssetCatalogEventBus::Handler::BusConnect();
  275. AZ::Render::ImGuiSystemNotificationBus::Handler::BusConnect();
  276. auto* passSystem = AZ::RPI::PassSystemInterface::Get();
  277. AZ_Assert(passSystem, "Cannot get the pass system.");
  278. passSystem->AddPassCreator(Name("RayTracingAmbientOcclusionPass"), &AZ::Render::RayTracingAmbientOcclusionPass::Create);
  279. m_numMSAASamples = GetDefaultNumMSAASamples();
  280. }
  281. void SampleComponentManager::ActivateInternal()
  282. {
  283. using namespace AZ;
  284. AZ::ApplicationTypeQuery appType;
  285. ComponentApplicationBus::Broadcast(&AZ::ComponentApplicationBus::Events::QueryApplicationType, appType);
  286. if (!appType.IsValid() || !appType.IsGame())
  287. {
  288. return;
  289. }
  290. // ActivateInternal() may get called twice because the OnCatalogLoaded was called twice When run AtomSampleViewer launcher. One of those two events is from ly launcher framework and the other one is from
  291. // LoadCatalog call in AtomSampleViewer system component. Although load the same asset catalog twice doesn't seem to cause other issue.
  292. if (m_wasActivated)
  293. {
  294. return;
  295. }
  296. Render::Bootstrap::DefaultWindowBus::BroadcastResult(m_windowContext, &Render::Bootstrap::DefaultWindowBus::Events::GetDefaultWindowContext);
  297. AzFramework::GameEntityContextRequestBus::BroadcastResult(m_entityContextId, &AzFramework::GameEntityContextRequestBus::Events::GetGameEntityContextId);
  298. CreateDefaultCamera();
  299. // Add customized pass classes
  300. auto* passSystem = RPI::PassSystemInterface::Get();
  301. passSystem->AddPassCreator(Name("RHISamplePass"), &AtomSampleViewer::RHISamplePass::Create);
  302. // Load ASV's own pass templates mapping
  303. // It can be loaded here and it doesn't need be added via OnReadyLoadTemplatesEvent::Handler
  304. // since the first render pipeline is created after this point.
  305. const char* asvPassTemplatesFile = "Passes/ASV/PassTemplates.azasset";
  306. bool loaded = passSystem->LoadPassTemplateMappings(asvPassTemplatesFile);
  307. if (!loaded)
  308. {
  309. AZ_Fatal("SampleComponentManager", "Failed to load AtomSampleViewer's pass templates at %s", asvPassTemplatesFile);
  310. return;
  311. }
  312. // Use scene and render pipeline for RHI samples as default scene and render pipeline
  313. CreateSceneForRHISample();
  314. m_exampleEntity->Init();
  315. m_exampleEntity->Activate();
  316. m_isSampleSupported.resize(m_availableSamples.size());
  317. for (size_t i = 0; i < m_availableSamples.size(); ++i)
  318. {
  319. // Assume that the sample is supported if no m_isSupportedFunc is provided.
  320. m_isSampleSupported[i] = m_availableSamples[i].m_isSupportedFunc ? m_availableSamples[i].m_isSupportedFunc() : true;
  321. }
  322. AZ_Printf("SampleComponentManager", "Available Samples -------------------------\n");
  323. for (size_t i = 0; i < m_availableSamples.size(); ++i)
  324. {
  325. AZStd::string printStr = "\t[" + m_availableSamples[i].m_sampleName + "]";
  326. if (!m_isSampleSupported[i])
  327. {
  328. printStr += " Not Supported ";
  329. }
  330. if (i < 9)
  331. {
  332. printStr += AZStd::string::format("\tctrl+%lu", i + 1);
  333. }
  334. printStr += "\n";
  335. AZ_Printf("SampleComponentManager", printStr.data());
  336. }
  337. AZ_Printf("SampleComponentManager", "-------------------------------------------\n");
  338. AzFramework::InputChannelEventListener::BusConnect();
  339. TickBus::Handler::BusConnect();
  340. bool targetSampleFound = false;
  341. const AzFramework::CommandLine* commandLine = nullptr;
  342. AzFramework::ApplicationRequests::Bus::BroadcastResult(commandLine, &AzFramework::ApplicationRequests::GetCommandLine);
  343. if (commandLine->HasSwitch("sample"))
  344. {
  345. AZStd::string targetSampleName = commandLine->GetSwitchValue("sample", 0);
  346. AZStd::to_lower(targetSampleName.begin(), targetSampleName.end());
  347. for (int32_t i = 0; i < m_availableSamples.size(); ++i)
  348. {
  349. AZStd::string sampleName = m_availableSamples[i].m_sampleName;
  350. AZStd::to_lower(sampleName.begin(), sampleName.end());
  351. if (sampleName == targetSampleName)
  352. {
  353. if (m_isSampleSupported[i])
  354. {
  355. targetSampleFound = true;
  356. m_selectedSampleIndex = i;
  357. m_sampleChangeRequest = true;
  358. }
  359. break;
  360. }
  361. }
  362. AZ_Warning("SampleComponentManager", targetSampleFound, "Failed find target sample %s", targetSampleName.c_str());
  363. }
  364. // Set default screenshot folder to relative path 'Screenshots'
  365. AZStd::string screenshotFolder = "Screenshots";
  366. // Get folder from command line if it exists
  367. static const char* screenshotFolderFlagName = "screenshotFolder";
  368. if (commandLine->HasSwitch(screenshotFolderFlagName))
  369. {
  370. screenshotFolder = commandLine->GetSwitchValue(screenshotFolderFlagName, 0);
  371. AzFramework::StringFunc::Path::Normalize(screenshotFolder);
  372. }
  373. if (AzFramework::StringFunc::Path::IsRelative(screenshotFolder.c_str()))
  374. {
  375. const char* engineRoot = nullptr;
  376. AzFramework::ApplicationRequests::Bus::BroadcastResult(engineRoot, &AzFramework::ApplicationRequests::GetEngineRoot);
  377. AzFramework::StringFunc::Path::Join(engineRoot, screenshotFolder.c_str(), screenshotFolder, true, false);
  378. }
  379. m_imguiFrameCaptureSaver.SetDefaultFolder(screenshotFolder);
  380. m_imguiFrameCaptureSaver.SetDefaultFileName("screenshot");
  381. m_imguiFrameCaptureSaver.SetAvailableExtensions({"png", "ppm", "dds"});
  382. m_imguiFrameCaptureSaver.Activate();
  383. SampleComponentManagerRequestBus::Handler::BusConnect();
  384. m_scriptManager->Activate();
  385. m_wasActivated = true;
  386. SampleComponentManagerNotificationBus::Broadcast(&SampleComponentManagerNotificationBus::Events::OnSampleManagerActivated);
  387. }
  388. void SampleComponentManager::Deactivate()
  389. {
  390. AzFramework::EntityContextRequestBus::Event(
  391. m_entityContextId, &AzFramework::EntityContextRequestBus::Events::DestroyEntity, m_cameraEntity);
  392. m_cameraEntity = nullptr;
  393. AzFramework::AssetCatalogEventBus::Handler::BusDisconnect();
  394. AZ::Render::ImGuiSystemNotificationBus::Handler::BusDisconnect();
  395. m_scriptManager->Deactivate();
  396. m_imguiFrameCaptureSaver.Deactivate();
  397. SampleComponentManagerRequestBus::Handler::BusDisconnect();
  398. AZ::TickBus::Handler::BusDisconnect();
  399. AzFramework::InputChannelEventListener::Disconnect();
  400. Render::ImGuiSystemRequestBus::Broadcast(&Render::ImGuiSystemRequests::PopActiveContext);
  401. m_imguiPassTree.Reset();
  402. m_imguiFrameGraphVisualizer.Reset();
  403. m_windowContext = nullptr;
  404. m_brdfTexture.reset();
  405. ReleaseRHIScene();
  406. ReleaseRPIScene();
  407. }
  408. void SampleComponentManager::OnTick(float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  409. {
  410. m_imGuiFrameTimer.PushValue(deltaTime);
  411. bool screenshotRequest = false;
  412. if (m_ctrlModifierLDown || m_ctrlModifierRDown)
  413. {
  414. if (m_alphanumericQDown)
  415. {
  416. RequestExit();
  417. return;
  418. }
  419. if (m_alphanumericTDown && m_canCaptureRADTM)
  420. {
  421. #ifdef AZ_PROFILE_TELEMETRY
  422. Utils::ToggleRadTMCapture();
  423. m_canCaptureRADTM = false;
  424. #endif
  425. }
  426. else if (!m_alphanumericTDown)
  427. {
  428. m_canCaptureRADTM = true;
  429. }
  430. if (m_alphanumericPDown)
  431. {
  432. screenshotRequest = true;
  433. }
  434. for (size_t i = 0; i < m_availableSamples.size(); ++i)
  435. {
  436. if (m_alphanumericNumbersDown[i] && i < s_alphanumericCount && m_isSampleSupported[i])
  437. {
  438. m_sampleChangeRequest = true;
  439. m_selectedSampleIndex = static_cast<int32_t>(i);
  440. break;
  441. }
  442. }
  443. }
  444. // Request a frame capture only once per key press, even if the keys are held down for multiple ticks.
  445. if (screenshotRequest)
  446. {
  447. ++m_screenshotKeyDownCount;
  448. if (m_screenshotKeyDownCount == 1)
  449. {
  450. RequestFrameCapture(m_imguiFrameCaptureSaver.GetNextAutoSaveFilePath(), true);
  451. }
  452. }
  453. else
  454. {
  455. m_screenshotKeyDownCount = 0;
  456. }
  457. RenderImGui(deltaTime);
  458. // SampleChange needs to happen after RenderImGui as some of the samples create sidebars on a separate ImGui context
  459. // which can interfere with m_imguiContext in the first frame
  460. if (m_sampleChangeRequest && m_canSwitchSample)
  461. {
  462. SampleChange();
  463. m_canSwitchSample = false;
  464. }
  465. else if (m_escapeDown && m_canSwitchSample)
  466. {
  467. Reset();
  468. m_canSwitchSample = false;
  469. Utils::ReportScriptableAction("OpenSample('')");
  470. }
  471. // Once a SampleChange/Reset request has been handled, it will not be handled again until there has been at least one frame where the sample hasn't changed
  472. if (!m_sampleChangeRequest && !m_escapeDown)
  473. {
  474. m_canSwitchSample = true;
  475. }
  476. // Since the event has been handled, clear the request
  477. m_sampleChangeRequest = false;
  478. m_escapeDown = false;
  479. m_scriptManager->TickScript(deltaTime);
  480. if (m_isFrameCapturePending)
  481. {
  482. if (m_countdownForFrameCapture > 0)
  483. {
  484. m_countdownForFrameCapture--;
  485. }
  486. else if (m_countdownForFrameCapture == 0)
  487. {
  488. AZ::Render::FrameCaptureNotificationBus::Handler::BusConnect();
  489. AZ::Render::FrameCaptureRequestBus::Broadcast(&AZ::Render::FrameCaptureRequestBus::Events::CaptureScreenshot, m_frameCaptureFilePath);
  490. m_countdownForFrameCapture = -1; // Don't call CaptureScreenshot again
  491. }
  492. }
  493. }
  494. bool SampleComponentManager::OnInputChannelEventFiltered(const AzFramework::InputChannel& inputChannel)
  495. {
  496. const size_t samplesAvailableCount = m_availableSamples.size();
  497. AZStd::vector<AzFramework::InputChannelId> sampleInputMapping;
  498. sampleInputMapping.reserve(samplesAvailableCount);
  499. for (size_t i = 0; i < samplesAvailableCount; ++i)
  500. {
  501. switch (i)
  502. {
  503. case 0:
  504. sampleInputMapping.push_back(AzFramework::InputDeviceKeyboard::Key::Alphanumeric1);
  505. break;
  506. case 1:
  507. sampleInputMapping.push_back(AzFramework::InputDeviceKeyboard::Key::Alphanumeric2);
  508. break;
  509. case 2:
  510. sampleInputMapping.push_back(AzFramework::InputDeviceKeyboard::Key::Alphanumeric3);
  511. break;
  512. case 3:
  513. sampleInputMapping.push_back(AzFramework::InputDeviceKeyboard::Key::Alphanumeric4);
  514. break;
  515. case 4:
  516. sampleInputMapping.push_back(AzFramework::InputDeviceKeyboard::Key::Alphanumeric5);
  517. break;
  518. case 5:
  519. sampleInputMapping.push_back(AzFramework::InputDeviceKeyboard::Key::Alphanumeric6);
  520. break;
  521. case 6:
  522. sampleInputMapping.push_back(AzFramework::InputDeviceKeyboard::Key::Alphanumeric7);
  523. break;
  524. case 7:
  525. sampleInputMapping.push_back(AzFramework::InputDeviceKeyboard::Key::Alphanumeric8);
  526. break;
  527. case 8:
  528. sampleInputMapping.push_back(AzFramework::InputDeviceKeyboard::Key::Alphanumeric9);
  529. break;
  530. case 9:
  531. sampleInputMapping.push_back(AzFramework::InputDeviceKeyboard::Key::Alphanumeric0);
  532. break;
  533. default:
  534. break;
  535. }
  536. }
  537. const AzFramework::InputChannelId& inputChannelId = inputChannel.GetInputChannelId();
  538. switch (inputChannel.GetState())
  539. {
  540. case AzFramework::InputChannel::State::Began:
  541. case AzFramework::InputChannel::State::Updated:
  542. {
  543. if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::ModifierCtrlL)
  544. {
  545. m_ctrlModifierLDown = true;
  546. }
  547. else if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::ModifierCtrlR)
  548. {
  549. m_ctrlModifierRDown = true;
  550. }
  551. else if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::AlphanumericQ)
  552. {
  553. m_alphanumericQDown = true;
  554. }
  555. else if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::AlphanumericT)
  556. {
  557. m_alphanumericTDown = true;
  558. }
  559. else if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::AlphanumericP)
  560. {
  561. m_alphanumericPDown = true;
  562. }
  563. else if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::Escape)
  564. {
  565. m_escapeDown = true;
  566. }
  567. for (size_t i = 0; i < samplesAvailableCount; ++i)
  568. {
  569. if ((i < s_alphanumericCount) && (inputChannelId == sampleInputMapping[i]))
  570. {
  571. m_alphanumericNumbersDown[i] = true;
  572. }
  573. }
  574. break;
  575. }
  576. case AzFramework::InputChannel::State::Ended:
  577. {
  578. if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::ModifierCtrlL)
  579. {
  580. m_ctrlModifierLDown = false;
  581. }
  582. else if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::ModifierCtrlR)
  583. {
  584. m_ctrlModifierRDown = false;
  585. }
  586. else if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::AlphanumericQ)
  587. {
  588. m_alphanumericQDown = false;
  589. }
  590. else if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::AlphanumericT)
  591. {
  592. m_alphanumericTDown = false;
  593. }
  594. else if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::AlphanumericP)
  595. {
  596. m_alphanumericPDown = false;
  597. }
  598. else if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::Escape)
  599. {
  600. m_escapeDown = false;
  601. }
  602. for (size_t i = 0; i < samplesAvailableCount; ++i)
  603. {
  604. if ((i < s_alphanumericCount) && (inputChannelId == sampleInputMapping[i]))
  605. {
  606. m_alphanumericNumbersDown[i] = false;
  607. }
  608. }
  609. break;
  610. }
  611. default:
  612. {
  613. break;
  614. }
  615. }
  616. return false;
  617. }
  618. void SampleComponentManager::RenderImGui(float deltaTime)
  619. {
  620. if (!m_isImGuiAvailable)
  621. {
  622. return;
  623. }
  624. ShowMenuBar();
  625. if (m_exitRequested)
  626. {
  627. return;
  628. }
  629. if (m_showResizeViewportDialog)
  630. {
  631. ShowResizeViewportDialog();
  632. }
  633. if (m_showFramerateHistogram)
  634. {
  635. ShowFramerateHistogram(deltaTime);
  636. }
  637. if (m_showFrameCaptureDialog)
  638. {
  639. ShowFrameCaptureDialog();
  640. }
  641. if (m_showImGuiMetrics)
  642. {
  643. ImGui::ShowMetricsWindow(&m_showImGuiMetrics);
  644. }
  645. if (m_showSampleHelper)
  646. {
  647. ShowSampleHelper();
  648. }
  649. if (m_showAbout)
  650. {
  651. ShowAboutWindow();
  652. }
  653. if (m_showPassTree)
  654. {
  655. ShowPassTreeWindow();
  656. }
  657. if (m_showFrameGraphVisualizer)
  658. {
  659. ShowFrameGraphVisualizerWindow();
  660. }
  661. if (m_showCullingDebugWindow)
  662. {
  663. AZ::RPI::Scene* defaultScene = AZ::RPI::RPISystemInterface::Get()->GetDefaultScene().get();
  664. AZ::Render::ImGuiDrawCullingDebug(m_showCullingDebugWindow, defaultScene);
  665. }
  666. if (m_showCpuProfiler)
  667. {
  668. ShowCpuProfilerWindow();
  669. }
  670. if (m_showGpuProfiler)
  671. {
  672. ShowGpuProfilerWindow();
  673. }
  674. if (m_showTransientAttachmentProfiler)
  675. {
  676. ShowTransientAttachmentProfilerWindow();
  677. }
  678. if (m_showShaderMetrics)
  679. {
  680. ShowShaderMetricsWindow();
  681. }
  682. m_scriptManager->TickImGui();
  683. }
  684. void SampleComponentManager::ShowMenuBar()
  685. {
  686. if (ImGui::BeginMainMenuBar())
  687. {
  688. // If imgui doesn't have enough room to render a menu, it will fall back to the safe area which
  689. // is typically 3 pixels. This causes the menu to overlap the menu bar, and makes it easy to
  690. // accidentally select the first item on that menu bar. By altering the safe area temporarily
  691. // while drawing the menu, this problem can be avoided.
  692. ImVec2 cachedSafeArea = ImGui::GetStyle().DisplaySafeAreaPadding;
  693. ImGui::GetStyle().DisplaySafeAreaPadding = ImVec2(cachedSafeArea.x, cachedSafeArea.y + 16.0f);
  694. if (ImGui::BeginMenu("File"))
  695. {
  696. if (ImGui::MenuItem("Exit", "Ctrl-Q"))
  697. {
  698. RequestExit();
  699. return;
  700. }
  701. if (ImGui::MenuItem("Capture Frame...", "Ctrl-P"))
  702. {
  703. m_showFrameCaptureDialog = true;
  704. }
  705. ImGui::EndMenu();
  706. }
  707. if (ImGui::BeginMenu("View"))
  708. {
  709. if (Utils::SupportsResizeClientArea() && ImGui::MenuItem("Resize Viewport..."))
  710. {
  711. m_showResizeViewportDialog = true;
  712. }
  713. if (Utils::SupportsToggleFullScreenOfDefaultWindow() && ImGui::MenuItem("Toggle Full Screen"))
  714. {
  715. Utils::ToggleFullScreenOfDefaultWindow();
  716. }
  717. if (ImGui::MenuItem("Framerate Histogram"))
  718. {
  719. m_showFramerateHistogram = !m_showFramerateHistogram;
  720. }
  721. if (ImGui::MenuItem("ImGui Metrics"))
  722. {
  723. m_showImGuiMetrics = !m_showImGuiMetrics;
  724. }
  725. if (ImGui::MenuItem("Sample Helper"))
  726. {
  727. m_showSampleHelper = !m_showSampleHelper;
  728. }
  729. if (ImGui::MenuItem("Frame Graph Visualizer"))
  730. {
  731. m_showFrameGraphVisualizer = !m_showFrameGraphVisualizer;
  732. }
  733. if (ImGui::MenuItem("Shader Metrics"))
  734. {
  735. m_showShaderMetrics = !m_showShaderMetrics;
  736. }
  737. ImGui::EndMenu();
  738. }
  739. if (ImGui::BeginMenu("Samples"))
  740. {
  741. for (int32_t i = 0; i < m_availableSamples.size(); i++)
  742. {
  743. const char* sampleName = m_availableSamples[i].m_sampleName.c_str();
  744. bool enabled = m_isSampleSupported[i];
  745. if (i < s_alphanumericCount)
  746. {
  747. const AZStd::string hotkeyName = AZStd::string::format("Ctrl-%d: ", (i + 1) % 10);
  748. if (ImGui::MenuItem(sampleName, hotkeyName.c_str(), false, enabled))
  749. {
  750. m_selectedSampleIndex = i;
  751. m_sampleChangeRequest = true;
  752. }
  753. }
  754. else
  755. {
  756. if (ImGui::MenuItem(sampleName, nullptr, false, enabled))
  757. {
  758. m_selectedSampleIndex = i;
  759. m_sampleChangeRequest = true;
  760. }
  761. }
  762. }
  763. if (m_sampleChangeRequest)
  764. {
  765. Utils::ReportScriptableAction("OpenSample('%s')", m_availableSamples[m_selectedSampleIndex].m_sampleName.c_str());
  766. }
  767. ImGui::EndMenu();
  768. }
  769. #ifdef AZ_PROFILE_TELEMETRY
  770. if (ImGui::BeginMenu("RADTelemetry"))
  771. {
  772. if (ImGui::MenuItem("Toggle Capture", "Ctrl-T"))
  773. {
  774. Utils::ToggleRadTMCapture();
  775. }
  776. ImGui::EndMenu();
  777. }
  778. #endif // AZ_PROFILE_TELEMETRY
  779. if (ImGui::BeginMenu("Automation"))
  780. {
  781. if (ImGui::MenuItem("Run Script..."))
  782. {
  783. m_scriptManager->OpenScriptRunnerDialog();
  784. }
  785. ImGui::EndMenu();
  786. }
  787. if (ImGui::BeginMenu("Pass"))
  788. {
  789. if (ImGui::MenuItem(PassTreeToolName))
  790. {
  791. m_showPassTree = !m_showPassTree;
  792. Utils::ReportScriptableAction("ShowTool('%s', %s)", PassTreeToolName, m_showPassTree?"true":"false");
  793. }
  794. ImGui::EndMenu();
  795. }
  796. if (ImGui::BeginMenu("Culling"))
  797. {
  798. if (ImGui::MenuItem("Culling Debug Window"))
  799. {
  800. m_showCullingDebugWindow = !m_showCullingDebugWindow;
  801. }
  802. ImGui::EndMenu();
  803. }
  804. if (ImGui::BeginMenu("Profile"))
  805. {
  806. if (ImGui::MenuItem(CpuProfilerToolName))
  807. {
  808. m_showCpuProfiler = !m_showCpuProfiler;
  809. if (auto profiler = Profiler::ProfilerInterface::Get(); profiler)
  810. {
  811. profiler->SetProfilerEnabled(m_showCpuProfiler);
  812. }
  813. Utils::ReportScriptableAction("ShowTool('%s', %s)", CpuProfilerToolName, m_showCpuProfiler ? "true" : "false");
  814. }
  815. if (ImGui::MenuItem(GpuProfilerToolName))
  816. {
  817. m_showGpuProfiler = !m_showGpuProfiler;
  818. Utils::ReportScriptableAction("ShowTool('%s', %s)", GpuProfilerToolName, m_showGpuProfiler ? "true" : "false");
  819. }
  820. if (ImGui::MenuItem(TransientAttachmentProfilerToolName))
  821. {
  822. m_showTransientAttachmentProfiler = !m_showTransientAttachmentProfiler;
  823. AZ::RHI::RHISystemInterface::Get()->ModifyFrameSchedulerStatisticsFlags(
  824. AZ::RHI::FrameSchedulerStatisticsFlags::GatherTransientAttachmentStatistics,
  825. m_showTransientAttachmentProfiler);
  826. Utils::ReportScriptableAction("ShowTool('%s', %s)", TransientAttachmentProfilerToolName, m_showTransientAttachmentProfiler ? "true" : "false");
  827. }
  828. ImGui::EndMenu();
  829. }
  830. if (ImGui::BeginMenu("Help"))
  831. {
  832. if (ImGui::MenuItem("About"))
  833. {
  834. m_showAbout = !m_showAbout;
  835. }
  836. ImGui::EndMenu();
  837. }
  838. // Restore original safe area.
  839. ImGui::GetStyle().DisplaySafeAreaPadding = cachedSafeArea;
  840. ImGui::EndMainMenuBar();
  841. }
  842. }
  843. void SampleComponentManager::ShowSampleHelper()
  844. {
  845. if (ImGui::Begin("Sample Helper", &m_showSampleHelper, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings))
  846. {
  847. if (ImGui::Button("Reset"))
  848. {
  849. //Removes the existing sample component and
  850. //resets the selection index
  851. Reset();
  852. CameraReset();
  853. m_selectedSampleIndex = -1;
  854. }
  855. ImGui::SameLine();
  856. if (ImGui::Button("Reset Sample"))
  857. {
  858. //Force a sample change event when the selection index
  859. //hasn't changed. This resets the sample component.
  860. SampleChange();
  861. }
  862. ImGui::SameLine();
  863. if (ImGui::Button("Reset Camera"))
  864. {
  865. CameraReset();
  866. }
  867. }
  868. ImGui::End();
  869. }
  870. void SampleComponentManager::ShowAboutWindow()
  871. {
  872. if (ImGui::Begin("About", &m_showAbout, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings))
  873. {
  874. ImGui::Text("RHI API: %s", AZ::RHI::Factory::Get().GetName().GetCStr());
  875. }
  876. ImGui::End();
  877. }
  878. void SampleComponentManager::ShowPassTreeWindow()
  879. {
  880. m_imguiPassTree.Draw(m_showPassTree, AZ::RPI::PassSystemInterface::Get()->GetRootPass().get());
  881. }
  882. void SampleComponentManager::ShowFrameGraphVisualizerWindow()
  883. {
  884. AZ::RHI::Device* rhiDevice = Utils::GetRHIDevice().get();
  885. m_imguiFrameGraphVisualizer.Init(rhiDevice);
  886. m_imguiFrameGraphVisualizer.Draw(m_showFrameGraphVisualizer);
  887. }
  888. void SampleComponentManager::ShowCpuProfilerWindow()
  889. {
  890. if (auto profilerImGui = Profiler::ProfilerImGuiInterface::Get(); profilerImGui)
  891. {
  892. profilerImGui->ShowCpuProfilerWindow(m_showCpuProfiler);
  893. }
  894. }
  895. void SampleComponentManager::ShowGpuProfilerWindow()
  896. {
  897. m_imguiGpuProfiler.Draw(m_showGpuProfiler, AZ::RPI::PassSystemInterface::Get()->GetRootPass());
  898. }
  899. void SampleComponentManager::ShowTransientAttachmentProfilerWindow()
  900. {
  901. auto* transientStats = AZ::RHI::RHISystemInterface::Get()->GetTransientAttachmentStatistics();
  902. if (transientStats)
  903. {
  904. m_showTransientAttachmentProfiler = m_imguiTransientAttachmentProfiler.Draw(*transientStats);
  905. }
  906. }
  907. void SampleComponentManager::ShowShaderMetricsWindow()
  908. {
  909. m_imguiShaderMetrics.Draw(m_showShaderMetrics, AZ::RPI::ShaderMetricsSystemInterface::Get()->GetMetrics());
  910. }
  911. void SampleComponentManager::ShowResizeViewportDialog()
  912. {
  913. static int size[2] = { 0, 0 };
  914. if (size[0] <= 0)
  915. {
  916. size[0] = aznumeric_cast<int>(m_windowContext->GetViewport().m_maxX - m_windowContext->GetViewport().m_minX);
  917. }
  918. if (size[1] <= 0)
  919. {
  920. size[1] = aznumeric_cast<int>(m_windowContext->GetViewport().m_maxY - m_windowContext->GetViewport().m_minY);
  921. }
  922. bool dialogWasOpen = m_showResizeViewportDialog;
  923. if (ImGui::Begin("Resize Viewport", &m_showResizeViewportDialog, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings))
  924. {
  925. ImGui::InputInt2("Size", size);
  926. if (ImGui::Button("Resize"))
  927. {
  928. Utils::ResizeClientArea(size[0], size[1]);
  929. Utils::ReportScriptableAction("ResizeViewport(%d, %d)", size[0], size[1]);
  930. // Re-initialize the size fields on the next frame so we can see whether the
  931. // correct size was achieved (should be the same values the user entered)...
  932. size[0] = 0;
  933. size[1] = 0;
  934. }
  935. }
  936. ImGui::End();
  937. if (dialogWasOpen && !m_showResizeViewportDialog)
  938. {
  939. // Re-initialize the size fields next time the dialog is shown...
  940. size[0] = 0;
  941. size[1] = 0;
  942. }
  943. }
  944. void SampleComponentManager::ShowFramerateHistogram(float deltaTime)
  945. {
  946. if (ImGui::Begin("Framerate Histogram", &m_showFramerateHistogram, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings))
  947. {
  948. ImGuiHistogramQueue::WidgetSettings settings;
  949. settings.m_reportInverse = true;
  950. settings.m_units = "fps";
  951. m_imGuiFrameTimer.Tick(deltaTime, settings);
  952. }
  953. ImGui::End();
  954. }
  955. void SampleComponentManager::RequestFrameCapture(const AZStd::string& filePath, bool hideImGui)
  956. {
  957. AZ_Assert(false == m_isFrameCapturePending, "Frame capture already in progress");
  958. m_isFrameCapturePending = true;
  959. m_hideImGuiDuringFrameCapture = hideImGui;
  960. m_frameCaptureFilePath = filePath;
  961. // Don't continue the script while a frame capture is pending in case subsequent changes
  962. // interfere with the pending capture.
  963. ScriptRunnerRequestBus::Broadcast(&ScriptRunnerRequests::PauseScript);
  964. if (m_hideImGuiDuringFrameCapture)
  965. {
  966. AZ::Render::ImGuiSystemRequestBus::Broadcast(&AZ::Render::ImGuiSystemRequestBus::Events::HideAllImGuiPasses);
  967. // We also hide Open 3D Engine's debug text
  968. AzFramework::ConsoleRequestBus::Broadcast(&AzFramework::ConsoleRequests::ExecuteConsoleCommand, "r_DisplayInfo 0");
  969. // The ExecuteConsoleCommand request is handled in a deferred manner, so we have to delay the screenshot a bit.
  970. m_countdownForFrameCapture = 1;
  971. }
  972. else
  973. {
  974. m_countdownForFrameCapture = 0;
  975. }
  976. }
  977. void SampleComponentManager::OnCaptureFinished(AZ::Render::FrameCaptureResult /*result*/, const AZStd::string& /*info*/)
  978. {
  979. AZ::Render::FrameCaptureNotificationBus::Handler::BusDisconnect();
  980. if (m_hideImGuiDuringFrameCapture)
  981. {
  982. AZ::Render::ImGuiSystemRequestBus::Broadcast(&AZ::Render::ImGuiSystemRequestBus::Events::ShowAllImGuiPasses);
  983. // We also show Open 3D Engine's debug text
  984. AzFramework::ConsoleRequestBus::Broadcast(&AzFramework::ConsoleRequests::ExecuteConsoleCommand, "r_DisplayInfo 1");
  985. }
  986. ScriptRunnerRequestBus::Broadcast(&ScriptRunnerRequests::ResumeScript);
  987. m_isFrameCapturePending = false;
  988. }
  989. bool SampleComponentManager::IsFrameCapturePending()
  990. {
  991. return m_isFrameCapturePending;
  992. }
  993. void SampleComponentManager::RunMainTestSuite(const AZStd::string& suiteFilePath, bool exitOnTestEnd, int randomSeed)
  994. {
  995. if (m_scriptManager)
  996. {
  997. m_scriptManager->RunMainTestSuite(suiteFilePath, exitOnTestEnd, randomSeed);
  998. }
  999. }
  1000. void SampleComponentManager::SetNumMSAASamples(int numMSAASamples)
  1001. {
  1002. AZ_Assert(IsValidNumMSAASamples(numMSAASamples), "Invalid MSAA sample setting");
  1003. m_numMSAASamples = numMSAASamples;
  1004. }
  1005. void SampleComponentManager::ResetNumMSAASamples()
  1006. {
  1007. m_numMSAASamples = GetDefaultNumMSAASamples();
  1008. }
  1009. void SampleComponentManager::ResetRPIScene()
  1010. {
  1011. ReleaseRPIScene();
  1012. SwitchSceneForRPISample();
  1013. }
  1014. void SampleComponentManager::ClearRPIScene()
  1015. {
  1016. ReleaseRPIScene();
  1017. }
  1018. void SampleComponentManager::ShowFrameCaptureDialog()
  1019. {
  1020. static bool requestCaptureOnNextFrame = false;
  1021. static bool hideImGuiFromFrameCapture = true;
  1022. if (requestCaptureOnNextFrame)
  1023. {
  1024. requestCaptureOnNextFrame = false;
  1025. RequestFrameCapture(m_imguiFrameCaptureSaver.GetSaveFilePath(), hideImGuiFromFrameCapture);
  1026. }
  1027. else if (!m_isFrameCapturePending) // Hide this dialog while taking a capture
  1028. {
  1029. if (ImGui::Begin("Frame Capture", &m_showFrameCaptureDialog))
  1030. {
  1031. ImGui::Checkbox("Hide ImGui", &hideImGuiFromFrameCapture);
  1032. ImGuiSaveFilePath::WidgetSettings settings;
  1033. settings.m_labels.m_filePath = "File Path (.png, .ppm, or .dds):";
  1034. m_imguiFrameCaptureSaver.Tick(settings);
  1035. if (ImGui::Button("Capture"))
  1036. {
  1037. // Don't actually do the capture until the next frame, so we can hide this dialog first
  1038. requestCaptureOnNextFrame = true;
  1039. if (hideImGuiFromFrameCapture)
  1040. {
  1041. Utils::ReportScriptableAction("CaptureScreenshot('%s')", m_imguiFrameCaptureSaver.GetSaveFilePath().c_str());
  1042. }
  1043. else
  1044. {
  1045. Utils::ReportScriptableAction("CaptureScreenshotWithImGui('%s')", m_imguiFrameCaptureSaver.GetSaveFilePath().c_str());
  1046. }
  1047. }
  1048. }
  1049. ImGui::End();
  1050. }
  1051. }
  1052. void SampleComponentManager::RequestExit()
  1053. {
  1054. AzFramework::ApplicationRequests::Bus::Broadcast(&AzFramework::ApplicationRequests::ExitMainLoop);
  1055. AZ::TickBus::Handler::BusDisconnect();
  1056. AzFramework::InputChannelEventListener::Disconnect();
  1057. m_exitRequested = true;
  1058. }
  1059. void SampleComponentManager::ShutdownActiveSample()
  1060. {
  1061. m_exampleEntity->Deactivate();
  1062. // Pointer to the m_activeSample must be nullified before m_activeSample is destroyed.
  1063. if (m_rhiSamplePass)
  1064. {
  1065. m_rhiSamplePass->SetRHISample(nullptr);
  1066. }
  1067. if (m_activeSample != nullptr)
  1068. {
  1069. // Disable the camera controller just in case the active sample enabled it and didn't disable in Deactivate().
  1070. AZ::Debug::CameraControllerRequestBus::Event(m_cameraEntity->GetId(), &AZ::Debug::CameraControllerRequestBus::Events::Disable);
  1071. m_exampleEntity->RemoveComponent(m_activeSample);
  1072. delete m_activeSample;
  1073. }
  1074. m_activeSample = nullptr;
  1075. // Force a reset of the shader variant finder to get more consistent testing of samples every time they are run, rather
  1076. // than the first time for each sample being "special".
  1077. auto variantFinder = AZ::Interface<AZ::RPI::IShaderVariantFinder>::Get();
  1078. variantFinder->Reset();
  1079. }
  1080. void SampleComponentManager::Reset()
  1081. {
  1082. ShutdownActiveSample();
  1083. m_exampleEntity->Activate();
  1084. // Reset to RHI sample pipeline
  1085. SwitchSceneForRHISample();
  1086. m_rhiSamplePass->SetRHISample(nullptr);
  1087. }
  1088. void SampleComponentManager::CreateDefaultCamera()
  1089. {
  1090. if (m_cameraEntity)
  1091. {
  1092. return;
  1093. }
  1094. // A camera entity will be created by the entity context request bus so that the component for this entity can use a feature processor.
  1095. AzFramework::EntityContextRequestBus::EventResult(m_cameraEntity, m_entityContextId, &AzFramework::EntityContextRequestBus::Events::CreateEntity, "CameraEntity");
  1096. //Add debug camera and controller components
  1097. AZ::Debug::CameraComponentConfig cameraConfig(m_windowContext);
  1098. cameraConfig.m_fovY = AZ::Constants::QuarterPi;
  1099. m_cameraEntity->CreateComponent(azrtti_typeid<AZ::Debug::CameraComponent>())
  1100. ->SetConfiguration(cameraConfig);
  1101. m_cameraEntity->CreateComponent(azrtti_typeid<AzFramework::TransformComponent>());
  1102. m_cameraEntity->CreateComponent(azrtti_typeid<AZ::Debug::ArcBallControllerComponent>());
  1103. m_cameraEntity->CreateComponent(azrtti_typeid<AZ::Debug::NoClipControllerComponent>());
  1104. m_cameraEntity->Activate();
  1105. m_scriptManager->SetCameraEntity(m_cameraEntity);
  1106. }
  1107. void SampleComponentManager::SetupImGuiContext()
  1108. {
  1109. AdjustImGuiFontScale();
  1110. // Add imgui context
  1111. Render::ImGuiSystemRequestBus::BroadcastResult(m_isImGuiAvailable, &Render::ImGuiSystemRequests::PushActiveContextFromDefaultPass);
  1112. AZ_Assert(m_isImGuiAvailable, "Unable set default imgui context to active. Does your pipeline have an ImGui pass marked as default? Your pass assets may need to be rebuilt.");
  1113. }
  1114. void SampleComponentManager::ActiveImGuiContextChanged(ImGuiContext* context)
  1115. {
  1116. ImGui::SetCurrentContext(context);
  1117. }
  1118. bool SampleComponentManager::OpenSample(const AZStd::string& sampleName)
  1119. {
  1120. for (int32_t i = 0; i < m_availableSamples.size(); i++)
  1121. {
  1122. if (m_availableSamples[i].m_sampleName == sampleName)
  1123. {
  1124. if (!m_availableSamples[i].m_isSupportedFunc || m_availableSamples[i].m_isSupportedFunc())
  1125. {
  1126. m_selectedSampleIndex = i;
  1127. m_sampleChangeRequest = true;
  1128. return true;
  1129. }
  1130. else
  1131. {
  1132. AZ_Error("SampleComponentManager", false, "Sample '%s' is not supported on this platform.", sampleName.c_str());
  1133. }
  1134. }
  1135. }
  1136. return false;
  1137. }
  1138. bool SampleComponentManager::ShowTool(const AZStd::string& toolName, bool enable)
  1139. {
  1140. if (toolName == PassTreeToolName)
  1141. {
  1142. m_showPassTree = enable;
  1143. return true;
  1144. }
  1145. else if (toolName == CpuProfilerToolName)
  1146. {
  1147. m_showCpuProfiler = enable;
  1148. return true;
  1149. }
  1150. else if (toolName == GpuProfilerToolName)
  1151. {
  1152. m_showGpuProfiler = enable;
  1153. return true;
  1154. }
  1155. else if (toolName == TransientAttachmentProfilerToolName)
  1156. {
  1157. m_showTransientAttachmentProfiler = enable;
  1158. return true;
  1159. }
  1160. return false;
  1161. }
  1162. void SampleComponentManager::SampleChange()
  1163. {
  1164. if (m_selectedSampleIndex == -1)
  1165. {
  1166. return;
  1167. }
  1168. ShutdownActiveSample();
  1169. // Reset the camera *before* activating the sample, because the sample's Activate() function might
  1170. // want to reposition the camera.
  1171. CameraReset();
  1172. const SampleEntry& sampleEntry = m_availableSamples[m_selectedSampleIndex];
  1173. SampleComponentConfig config(m_windowContext, m_cameraEntity->GetId(), m_entityContextId);
  1174. m_activeSample = m_exampleEntity->CreateComponent(sampleEntry.m_sampleUuid);
  1175. m_activeSample->SetConfiguration(config);
  1176. if (sampleEntry.m_pipelineType == SamplePipelineType::RHI)
  1177. {
  1178. SwitchSceneForRHISample();
  1179. BasicRHIComponent* rhiSampleComponent = static_cast<BasicRHIComponent*>(m_activeSample);
  1180. if (rhiSampleComponent->IsSupportedRHISamplePipeline())
  1181. {
  1182. m_rhiSamplePass->SetRHISample(rhiSampleComponent);
  1183. }
  1184. else
  1185. {
  1186. m_rhiSamplePass->SetRHISample(nullptr);
  1187. }
  1188. }
  1189. else if (sampleEntry.m_pipelineType == SamplePipelineType::RPI)
  1190. {
  1191. SwitchSceneForRPISample();
  1192. }
  1193. m_exampleEntity->Activate();
  1194. // Even though this is done in CameraReset(), the example component wasn't activated at the time so we have to send this event again.
  1195. ExampleComponentRequestBus::Event(m_exampleEntity->GetId(), &ExampleComponentRequestBus::Events::ResetCamera);
  1196. }
  1197. void SampleComponentManager::CameraReset()
  1198. {
  1199. // Reset the camera transform. Some examples do not use a controller or use a controller that doesn't override the whole transform.
  1200. // Set to a transform that is 5 units away from the origin and looking at the origin along the Y axis.
  1201. const AZ::EntityId cameraEntityId = m_cameraEntity->GetId();
  1202. AZ::TransformBus::Event(cameraEntityId, &AZ::TransformBus::Events::SetWorldTM, AZ::Transform::CreateTranslation(AZ::Vector3(0.0f, -5.0f, 0.0f)));
  1203. AZ::Debug::CameraControllerRequestBus::Event(cameraEntityId, &AZ::Debug::CameraControllerRequestBus::Events::Reset);
  1204. // Tell the current example to reset the camera, any example that controls the camera and preserves controller state should implement this event
  1205. ExampleComponentRequestBus::Event(m_exampleEntity->GetId(), &ExampleComponentRequestBus::Events::ResetCamera);
  1206. }
  1207. void SampleComponentManager::CreateSceneForRHISample()
  1208. {
  1209. // Create and register the rhi scene with only feature processors required for AtomShimRenderer (only for AtomSampleViewerLauncher)
  1210. RPI::SceneDescriptor sceneDesc;
  1211. sceneDesc.m_featureProcessorNames.push_back("AuxGeomFeatureProcessor");
  1212. m_rhiScene = RPI::Scene::CreateScene(sceneDesc);
  1213. m_rhiScene->Activate();
  1214. RPI::RenderPipelineDescriptor pipelineDesc;
  1215. pipelineDesc.m_name = "RHISamplePipeline";
  1216. pipelineDesc.m_rootPassTemplate = "RHISamplePipelineTemplate";
  1217. // Add view to pipeline since there are few RHI samples are using ViewSrg
  1218. pipelineDesc.m_mainViewTagName = "MainCamera";
  1219. RPI::RenderPipelinePtr renderPipeline = RPI::RenderPipeline::CreateRenderPipelineForWindow(pipelineDesc, *m_windowContext.get());
  1220. m_rhiScene->AddRenderPipeline(renderPipeline);
  1221. renderPipeline->SetDefaultViewFromEntity(m_cameraEntity->GetId());
  1222. RPI::RPISystemInterface::Get()->RegisterScene(m_rhiScene);
  1223. // Get RHISamplePass
  1224. AZ::RPI::PassFilter passFilter = AZ::RPI::PassFilter::CreateWithPassName(AZ::Name("RHISamplePass"), renderPipeline.get());
  1225. m_rhiSamplePass = azrtti_cast<RHISamplePass*>(AZ::RPI::PassSystemInterface::Get()->FindFirstPass(passFilter));
  1226. // Setup imGui since a new render pipeline with imgui pass was created
  1227. SetupImGuiContext();
  1228. }
  1229. void SampleComponentManager::ReleaseRHIScene()
  1230. {
  1231. if (m_rhiScene)
  1232. {
  1233. m_rhiSamplePass = nullptr;
  1234. RPI::RPISystemInterface::Get()->UnregisterScene(m_rhiScene);
  1235. m_rhiScene = nullptr;
  1236. }
  1237. }
  1238. void SampleComponentManager::SwitchSceneForRHISample()
  1239. {
  1240. ReleaseRPIScene();
  1241. if (!m_rhiScene)
  1242. {
  1243. CreateSceneForRHISample();
  1244. }
  1245. }
  1246. void SampleComponentManager::CreateSceneForRPISample()
  1247. {
  1248. // Create and register a scene with all available feature processors
  1249. RPI::SceneDescriptor sceneDesc;
  1250. m_rpiScene = RPI::Scene::CreateScene(sceneDesc);
  1251. m_rpiScene->EnableAllFeatureProcessors();
  1252. // Bind m_rpiScene to the GameEntityContext's AzFramework::Scene so the RPI Scene can be found by the entity context
  1253. auto sceneSystem = AzFramework::SceneSystemInterface::Get();
  1254. AZ_Assert(sceneSystem, "SampleComponentManager requires an implementation of the scene system.");
  1255. AZStd::shared_ptr<AzFramework::Scene> mainScene = sceneSystem->GetScene(AzFramework::Scene::MainSceneName);
  1256. AZ_Assert(mainScene, "Main scene missing during system component initialization"); // This should never happen unless scene creation has changed.
  1257. // Add RPI::Scene as a sub system for the default AzFramework Scene
  1258. [[maybe_unused]] bool result = mainScene->SetSubsystem(m_rpiScene);
  1259. AZ_Assert(result, "SampleComponentManager failed to register the RPI scene with the general scene.");
  1260. m_rpiScene->Activate();
  1261. // Register scene to RPI system so it will be processed/rendered per tick
  1262. RPI::RPISystemInterface::Get()->RegisterScene(m_rpiScene);
  1263. // Create MainPipeline as its render pipeline
  1264. RPI::RenderPipelineDescriptor pipelineDesc;
  1265. pipelineDesc.m_name = "RPISamplePipeline";
  1266. pipelineDesc.m_rootPassTemplate = GetRootPassTemplateName();
  1267. pipelineDesc.m_mainViewTagName = "MainCamera";
  1268. // set pipeline MSAA samples
  1269. AZ_Assert(IsValidNumMSAASamples(m_numMSAASamples), "Invalid MSAA sample setting");
  1270. pipelineDesc.m_renderSettings.m_multisampleState.m_samples = static_cast<uint16_t>(m_numMSAASamples);
  1271. bool isNonMsaaPipeline = (pipelineDesc.m_renderSettings.m_multisampleState.m_samples == 1);
  1272. const char* supervariantName = isNonMsaaPipeline ? AZ::RPI::NoMsaaSupervariantName : "";
  1273. AZ::RPI::ShaderSystemInterface::Get()->SetSupervariantName(AZ::Name(supervariantName));
  1274. RPI::RenderPipelinePtr renderPipeline = RPI::RenderPipeline::CreateRenderPipelineForWindow(pipelineDesc, *m_windowContext.get());
  1275. m_rpiScene->AddRenderPipeline(renderPipeline);
  1276. renderPipeline->SetDefaultViewFromEntity(m_cameraEntity->GetId());
  1277. // As part of our initialization we need to create the BRDF texture generation pipeline
  1278. AZ::RPI::RenderPipelineDescriptor brdfPipelineDesc;
  1279. brdfPipelineDesc.m_mainViewTagName = "MainCamera";
  1280. brdfPipelineDesc.m_name = "BRDFTexturePipeline";
  1281. brdfPipelineDesc.m_rootPassTemplate = "BRDFTexturePipeline";
  1282. brdfPipelineDesc.m_executeOnce = true;
  1283. RPI::RenderPipelinePtr brdfTexturePipeline = AZ::RPI::RenderPipeline::CreateRenderPipeline(brdfPipelineDesc);
  1284. m_rpiScene->AddRenderPipeline(brdfTexturePipeline);
  1285. // Save a reference to the generated BRDF texture so it doesn't get deleted if all the passes refering to it get deleted and it's ref count goes to zero
  1286. if (!m_brdfTexture)
  1287. {
  1288. const AZStd::shared_ptr<RPI::PassTemplate> brdfTextureTemplate = RPI::PassSystemInterface::Get()->GetPassTemplate(Name("BRDFTextureTemplate"));
  1289. Data::Asset<RPI::AttachmentImageAsset> brdfImageAsset = RPI::AssetUtils::LoadAssetById<RPI::AttachmentImageAsset>(
  1290. brdfTextureTemplate->m_imageAttachments[0].m_assetRef.m_assetId, RPI::AssetUtils::TraceLevel::Error);
  1291. if (brdfImageAsset.IsReady())
  1292. {
  1293. m_brdfTexture = RPI::AttachmentImage::FindOrCreate(brdfImageAsset);
  1294. }
  1295. }
  1296. // Setup imGui since a new render pipeline with imgui pass was created
  1297. SetupImGuiContext();
  1298. }
  1299. void SampleComponentManager::ReleaseRPIScene()
  1300. {
  1301. if (m_rpiScene)
  1302. {
  1303. RPI::RPISystemInterface::Get()->UnregisterScene(m_rpiScene);
  1304. auto sceneSystem = AzFramework::SceneSystemInterface::Get();
  1305. AZ_Assert(sceneSystem, "Scene system was destroyed before SampleComponentManager was able to unregister the RPI scene.");
  1306. AZStd::shared_ptr<AzFramework::Scene> scene = sceneSystem->GetScene(AzFramework::Scene::MainSceneName);
  1307. AZ_Assert(scene, "The main scene wasn't found in the scene system.");
  1308. [[maybe_unused]] bool result = scene->UnsetSubsystem(m_rpiScene);
  1309. AZ_Assert(result, "SampleComponentManager failed to unregister its RPI scene from the general scene.");
  1310. m_rpiScene = nullptr;
  1311. }
  1312. }
  1313. void SampleComponentManager::SwitchSceneForRPISample()
  1314. {
  1315. ReleaseRHIScene();
  1316. if (!m_rpiScene)
  1317. {
  1318. CreateSceneForRPISample();
  1319. }
  1320. }
  1321. // AzFramework::AssetCatalogEventBus::Handler overrides ...
  1322. void SampleComponentManager::OnCatalogLoaded([[maybe_unused]] const char* catalogFile)
  1323. {
  1324. AZ::TickBus::QueueFunction([&]()
  1325. {
  1326. ActivateInternal();
  1327. });
  1328. }
  1329. } // namespace AtomSampleViewer