SampleComponentManager.cpp 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883
  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/ImGui/ImGuiUtils.h>
  14. #include <Atom/Feature/ImGui/SystemBus.h>
  15. #include <Atom/Feature/Mesh/MeshFeatureProcessorInterface.h>
  16. #include <Atom/Feature/PostProcessing/PostProcessingConstants.h>
  17. #include <Atom/Feature/SkinnedMesh/SkinnedMeshInputBuffers.h>
  18. #include <Atom/RPI.Public/AuxGeom/AuxGeomFeatureProcessorInterface.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.Public/Shader/ShaderSystemInterface.h>
  28. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  29. #include <Atom/RPI.Reflect/Image/AttachmentImageAsset.h>
  30. #include <Atom/RPI.Reflect/Image/AttachmentImageAssetCreator.h>
  31. #include <Atom/RPI.Reflect/Shader/IShaderVariantFinder.h>
  32. #include <Atom/RHI/Factory.h>
  33. #include <Atom/RHI/RHISystemInterface.h>
  34. #include <Atom/RHI/RHIMemoryStatisticsInterface.h>
  35. #include <Atom/RHI/RHIUtils.h>
  36. #include <Atom/RHI.Reflect/AliasedHeapEnums.h>
  37. #include <Automation/ScriptManager.h>
  38. #include <RHI/AlphaToCoverageExampleComponent.h>
  39. #include <RHI/AsyncComputeExampleComponent.h>
  40. #include <RHI/BasicRHIComponent.h>
  41. #include <RHI/ComputeExampleComponent.h>
  42. #include <RHI/CopyQueueComponent.h>
  43. #include <RHI/DualSourceBlendingComponent.h>
  44. #include <RHI/IndirectRenderingExampleComponent.h>
  45. #include <RHI/InputAssemblyExampleComponent.h>
  46. #include <RHI/SubpassExampleComponent.h>
  47. #include <RHI/MRTExampleComponent.h>
  48. #include <RHI/MSAAExampleComponent.h>
  49. #include <RHI/MultiThreadComponent.h>
  50. #include <RHI/MultiGPUExampleComponent.h>
  51. #include <RHI/MultiViewportSwapchainComponent.h>
  52. #include <RHI/MultipleViewsComponent.h>
  53. #include <RHI/QueryExampleComponent.h>
  54. #include <RHI/StencilExampleComponent.h>
  55. #include <RHI/SwapchainExampleComponent.h>
  56. #include <RHI/SphericalHarmonicsExampleComponent.h>
  57. #include <RHI/Texture3dExampleComponent.h>
  58. #include <RHI/TextureArrayExampleComponent.h>
  59. #include <RHI/TextureExampleComponent.h>
  60. #include <RHI/TextureMapExampleComponent.h>
  61. #include <RHI/TriangleExampleComponent.h>
  62. #include <RHI/XRExampleComponent.h>
  63. #include <RHI/TrianglesConstantBufferExampleComponent.h>
  64. #include <RHI/BindlessPrototypeExampleComponent.h>
  65. #include <RHI/RayTracingExampleComponent.h>
  66. #include <RHI/MatrixAlignmentTestExampleComponent.h>
  67. #include <RHI/VariableRateShadingExampleComponent.h>
  68. #include <Performance/100KDrawable_SingleView_ExampleComponent.h>
  69. #include <Performance/100KDraw_10KDrawable_MultiView_ExampleComponent.h>
  70. #include <AreaLightExampleComponent.h>
  71. #include <AssetLoadTestComponent.h>
  72. #include <AuxGeomExampleComponent.h>
  73. #include <BakedShaderVariantExampleComponent.h>
  74. #include <SponzaBenchmarkComponent.h>
  75. #include <BloomExampleComponent.h>
  76. #include <CheckerboardExampleComponent.h>
  77. #include <CullingAndLodExampleComponent.h>
  78. #include <DecalExampleComponent.h>
  79. #include <DepthOfFieldExampleComponent.h>
  80. #include <DynamicDrawExampleComponent.h>
  81. #include <DynamicMaterialTestComponent.h>
  82. #include <ExposureExampleComponent.h>
  83. #include <EyeMaterialExampleComponent.h>
  84. #include <SceneReloadSoakTestComponent.h>
  85. #include <LightCullingExampleComponent.h>
  86. #include <MeshExampleComponent.h>
  87. #include <MSAA_RPI_ExampleComponent.h>
  88. #include <MultiGPURPIExampleComponent.h>
  89. #include <MultiRenderPipelineExampleComponent.h>
  90. #include <MultiSceneExampleComponent.h>
  91. #include <ParallaxMappingExampleComponent.h>
  92. #include <RayTracingIntersectionShaderExampleComponent.h>
  93. #include <RenderTargetTextureExampleComponent.h>
  94. #include <SceneReloadSoakTestComponent.h>
  95. #include <ShadowExampleComponent.h>
  96. #include <ShadowedSponzaExampleComponent.h>
  97. #include <SkinnedMeshExampleComponent.h>
  98. #include <SsaoExampleComponent.h>
  99. #include <StreamingImageExampleComponent.h>
  100. #include <RootConstantsExampleComponent.h>
  101. #include <MultiViewSingleSceneAuxGeomExampleComponent.h>
  102. #include <TonemappingExampleComponent.h>
  103. #include <TransparencyExampleComponent.h>
  104. #include <DiffuseGIExampleComponent.h>
  105. #include <SSRExampleComponent.h>
  106. #include <XRRPIExampleComponent.h>
  107. #include <ShaderReloadTestComponent.h>
  108. #include <ReadbackExampleComponent.h>
  109. #include <Subpass_RPI_ExampleComponent.h>
  110. #include <Atom/Bootstrap/DefaultWindowBus.h>
  111. #include <AzCore/Component/Entity.h>
  112. #include <AzCore/Debug/Profiler.h>
  113. #include <AzCore/Debug/ProfilerBus.h>
  114. #include <AzCore/IO/IStreamerProfiler.h>
  115. #include <AzCore/Serialization/SerializeContext.h>
  116. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  117. #include <AzCore/std/smart_ptr/make_shared.h>
  118. #include <AzCore/std/algorithm.h>
  119. #include <AzFramework/API/ApplicationAPI.h>
  120. #include <AzFramework/Components/ConsoleBus.h>
  121. #include <AzFramework/Components/TransformComponent.h>
  122. #include <AzFramework/Input/Devices/Keyboard/InputDeviceKeyboard.h>
  123. #include <AzFramework/Entity/GameEntityContextBus.h>
  124. #include <AzFramework/Scene/Scene.h>
  125. #include <AzFramework/Scene/SceneSystemInterface.h>
  126. #include <Passes/RayTracingAmbientOcclusionPass.h>
  127. #include <Utils/Utils.h>
  128. #include <Profiler/ProfilerImGuiBus.h>
  129. #include "ExampleComponentBus.h"
  130. #include <EntityUtilityFunctions.h>
  131. namespace Platform
  132. {
  133. const char* GetPipelineName();
  134. }
  135. namespace AtomSampleViewer
  136. {
  137. namespace
  138. {
  139. constexpr const char* PassTreeToolName = "PassTree";
  140. constexpr const char* CpuProfilerToolName = "CPU Profiler";
  141. constexpr const char* GpuProfilerToolName = "GPU Profiler";
  142. constexpr const char* FileIoProfilerToolName = "File IO Profiler";
  143. constexpr const char* TransientAttachmentProfilerToolName = "Transient Attachment Profiler";
  144. constexpr const char* SampleSetting = "/O3DE/AtomSampleViewer/Sample";
  145. }
  146. bool IsValidNumMSAASamples(int16_t numSamples)
  147. {
  148. return (numSamples == 1) || (numSamples == 2) || (numSamples == 4) || (numSamples == 8);
  149. }
  150. template <typename T>
  151. static SampleEntry NewRHISample(const AZStd::string& name)
  152. {
  153. return NewSample<T>(SamplePipelineType::RHI, "RHI", name);
  154. }
  155. template <typename T>
  156. static SampleEntry NewRHISample(const AZStd::string& name, AZStd::function<bool()> isSupportedFunction)
  157. {
  158. SampleEntry entry = NewSample<T>(SamplePipelineType::RHI, "RHI", name, isSupportedFunction);
  159. entry.m_isSupportedFunc = isSupportedFunction;
  160. return entry;
  161. }
  162. template <typename T>
  163. static SampleEntry NewRPISample(const AZStd::string& name)
  164. {
  165. return NewSample<T>(SamplePipelineType::RPI, "RPI", name);
  166. }
  167. template <typename T>
  168. static SampleEntry NewRPISample(const AZStd::string& name, AZStd::function<bool()> isSupportedFunction)
  169. {
  170. return NewSample<T>(SamplePipelineType::RPI, "RPI", name, isSupportedFunction);
  171. }
  172. template <typename T>
  173. static SampleEntry NewFeaturesSample(const AZStd::string& name)
  174. {
  175. return NewSample<T>(SamplePipelineType::RPI, "Features", name);
  176. }
  177. template <typename T>
  178. static SampleEntry NewFeaturesSample(const AZStd::string& name, AZStd::function<bool()> isSupportedFunction)
  179. {
  180. return NewSample<T>(SamplePipelineType::RPI, "Features", name, isSupportedFunction);
  181. }
  182. template <typename T>
  183. static SampleEntry NewPerfSample(const AZStd::string& name)
  184. {
  185. return NewSample<T>(SamplePipelineType::RPI, "Performance", name);
  186. }
  187. template <typename T>
  188. static SampleEntry NewPerfSample(const AZStd::string& name, AZStd::function<bool()> isSupportedFunction)
  189. {
  190. return NewSample<T>(SamplePipelineType::RPI, "Performance", name, isSupportedFunction);
  191. }
  192. static AZStd::string GetTargetSampleName()
  193. {
  194. //Check command line option
  195. const AzFramework::CommandLine* commandLine = nullptr;
  196. AzFramework::ApplicationRequests::Bus::BroadcastResult(commandLine, &AzFramework::ApplicationRequests::GetCommandLine);
  197. if (commandLine->HasSwitch("sample"))
  198. {
  199. AZStd::string targetSampleName = commandLine->GetSwitchValue("sample", 0);
  200. return targetSampleName;
  201. }
  202. //Check settings registry
  203. if (AZ::SettingsRegistryInterface* settingsRegistry = AZ::SettingsRegistry::Get())
  204. {
  205. if (AZStd::string targetSampleName;
  206. settingsRegistry->Get(targetSampleName, SampleSetting))
  207. {
  208. return targetSampleName;
  209. }
  210. }
  211. return {};
  212. }
  213. void SampleComponentManager::Reflect(AZ::ReflectContext* context)
  214. {
  215. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  216. {
  217. serializeContext->Class<SampleComponentManager, AZ::Component>()
  218. ->Version(0)
  219. ;
  220. // This registration matches ShaderOptionValuesSourceData, which is needed by ImGuiShaderUtils, to support
  221. // generating JSON for shader variants.
  222. serializeContext->RegisterGenericType<AZStd::unordered_map<Name, Name>>();
  223. }
  224. }
  225. void SampleComponentManager::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  226. {
  227. required.push_back(AZ_CRC("PrototypeLmbrCentralService", 0xe35e6de0));
  228. }
  229. void SampleComponentManager::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  230. {
  231. provided.push_back(AZ_CRC_CE("SampleComponentManagerService"));
  232. }
  233. void SampleComponentManager::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  234. {
  235. dependent.push_back(AZ_CRC("AzFrameworkConfigurationSystemComponentService", 0xcc49c96e)); // Ensures a scene is created for the GameEntityContext
  236. }
  237. AZStd::vector<SampleEntry> SampleComponentManager::GetSamples()
  238. {
  239. return {
  240. NewRHISample<AlphaToCoverageExampleComponent>("AlphaToCoverage"),
  241. NewRHISample<AsyncComputeExampleComponent>("AsyncCompute"),
  242. NewRHISample<BindlessPrototypeExampleComponent>("BindlessPrototype"),
  243. NewRHISample<ComputeExampleComponent>("Compute"),
  244. NewRHISample<CopyQueueComponent>("CopyQueue"),
  245. NewRHISample<DualSourceBlendingComponent>("DualSourceBlending", []() {return Utils::GetRHIDevice()->GetFeatures().m_dualSourceBlending; }),
  246. NewRHISample<IndirectRenderingExampleComponent>("IndirectRendering", []() {return Utils::GetRHIDevice()->GetFeatures().m_indirectCommandTier > RHI::IndirectCommandTiers::Tier0; }),
  247. NewRHISample<InputAssemblyExampleComponent>("InputAssembly"),
  248. NewRHISample<MSAAExampleComponent>("MSAA"),
  249. NewRHISample<MultipleViewsComponent>("MultipleViews"),
  250. NewRHISample<MRTExampleComponent>("MultiRenderTarget"),
  251. NewRHISample<MultiThreadComponent>("MultiThread"),
  252. NewRHISample<MultiGPUExampleComponent>("MultiGPU", []() { return AZ::RHI::RHISystemInterface::Get()->GetDeviceCount() >= 2; }),
  253. NewRHISample<MultiViewportSwapchainComponent>("MultiViewportSwapchainComponent", [] { return IsMultiViewportSwapchainSampleSupported(); }),
  254. NewRHISample<QueryExampleComponent>("Queries"),
  255. NewRHISample<RayTracingExampleComponent>("RayTracing", []() {return Utils::GetRHIDevice()->GetFeatures().m_rayTracing; }),
  256. NewRHISample<SphericalHarmonicsExampleComponent>("SphericalHarmonics"),
  257. NewRHISample<StencilExampleComponent>("Stencil"),
  258. NewRHISample<SubpassExampleComponent>("Subpass", []() { return RHI::RHISystemInterface::Get()->CanMergeSubpasses(); }),
  259. NewRHISample<SwapchainExampleComponent>("Swapchain"),
  260. NewRHISample<TextureExampleComponent>("Texture"),
  261. NewRHISample<Texture3dExampleComponent>("Texture3d"),
  262. NewRHISample<TextureArrayExampleComponent>("TextureArray"),
  263. NewRHISample<TextureMapExampleComponent>("TextureMap"),
  264. NewRHISample<TriangleExampleComponent>("Triangle"),
  265. NewRHISample<TrianglesConstantBufferExampleComponent>("TrianglesConstantBuffer"),
  266. NewRHISample<XRExampleComponent>("OpenXr", []() { return AZ::RHI::RHISystemInterface::Get()->GetXRSystem() != nullptr; }),
  267. NewRHISample<MatrixAlignmentTestExampleComponent>("MatrixAlignmentTest"),
  268. NewRHISample<VariableRateShadingExampleComponent>("VariableRateShading", []() { return Utils::GetRHIDevice()->GetFeatures().m_shadingRateTypeMask != RHI::ShadingRateTypeFlags::None; }),
  269. NewRPISample<AssetLoadTestComponent>("AssetLoadTest"),
  270. NewRPISample<AuxGeomExampleComponent>("AuxGeom"),
  271. NewRPISample<BakedShaderVariantExampleComponent>("BakedShaderVariant"),
  272. NewRPISample<SponzaBenchmarkComponent>("SponzaBenchmark"),
  273. NewRPISample<CullingAndLodExampleComponent>("CullingAndLod"),
  274. NewRPISample<DecalExampleComponent>("Decals"),
  275. NewRPISample<DynamicDrawExampleComponent>("DynamicDraw"),
  276. NewRPISample<DynamicMaterialTestComponent>("DynamicMaterialTest"),
  277. NewRPISample<MeshExampleComponent>("Mesh"),
  278. NewRPISample<MSAA_RPI_ExampleComponent>("MSAA"),
  279. NewRPISample<MultiGPURPIExampleComponent>("MultiGPU", []() { return AZ::RHI::RHISystemInterface::Get()->GetDeviceCount() >= 2; }),
  280. NewRPISample<MultiRenderPipelineExampleComponent>("MultiRenderPipeline"),
  281. NewRPISample<MultiSceneExampleComponent>("MultiScene"),
  282. NewRPISample<MultiViewSingleSceneAuxGeomExampleComponent>("MultiViewSingleSceneAuxGeom"),
  283. NewRPISample<ReadbackExampleComponent>("Readback"),
  284. NewRPISample<RenderTargetTextureExampleComponent>("RenderTargetTexture"),
  285. NewRPISample<RootConstantsExampleComponent>("RootConstants"),
  286. NewRPISample<SceneReloadSoakTestComponent>("SceneReloadSoakTest"),
  287. NewRPISample<StreamingImageExampleComponent>("StreamingImage"),
  288. NewRPISample<ShaderReloadTestComponent>("ShaderReloadTest"),
  289. NewRPISample<Subpass_RPI_ExampleComponent>("Subpass", []() { return RHI::RHISystemInterface::Get()->CanMergeSubpasses(); }),
  290. NewFeaturesSample<AreaLightExampleComponent>("AreaLight"),
  291. NewFeaturesSample<BloomExampleComponent>("Bloom"),
  292. NewFeaturesSample<CheckerboardExampleComponent>("Checkerboard", []() {return (Utils::GetRHIDevice()->GetPhysicalDevice().GetDescriptor().m_vendorId != RHI::VendorId::ARM && Utils::GetRHIDevice()->GetPhysicalDevice().GetDescriptor().m_vendorId != RHI::VendorId::Qualcomm); }),
  293. NewFeaturesSample<DepthOfFieldExampleComponent>("DepthOfField"),
  294. NewFeaturesSample<DiffuseGIExampleComponent>("DiffuseGI", []() {return Utils::GetRHIDevice()->GetFeatures().m_rayTracing; }),
  295. NewFeaturesSample<ExposureExampleComponent>("Exposure"),
  296. NewFeaturesSample<EyeMaterialExampleComponent>("EyeMaterial"),
  297. NewFeaturesSample<LightCullingExampleComponent>("LightCulling"),
  298. NewFeaturesSample<ParallaxMappingExampleComponent>("Parallax"),
  299. NewFeaturesSample<RayTracingIntersectionShaderExampleComponent>("RayTracingIntersectionShader", []() { return Utils::GetRHIDevice()->GetFeatures().m_rayTracing; }),
  300. NewFeaturesSample<ShadowExampleComponent>("Shadow"),
  301. NewFeaturesSample<ShadowedSponzaExampleComponent>("ShadowedSponza"),
  302. NewFeaturesSample<SkinnedMeshExampleComponent>("SkinnedMesh"),
  303. NewFeaturesSample<SsaoExampleComponent>("SSAO"),
  304. NewFeaturesSample<SSRExampleComponent>("SSR"),
  305. NewFeaturesSample<XRRPIExampleComponent>("OpenXR", []() { return AZ::RPI::RPISystemInterface::Get()->GetXRSystem() != nullptr; }),
  306. NewFeaturesSample<TonemappingExampleComponent>("Tonemapping"),
  307. NewFeaturesSample<TransparencyExampleComponent>("Transparency"),
  308. NewPerfSample<_100KDrawableExampleComponent>("100KDrawable_SingleView"),
  309. NewPerfSample<_100KDraw10KDrawableExampleComponent>("100KDraw_10KDrawable_MultiView"),
  310. };
  311. }
  312. void SampleComponentManager::RegisterSampleComponent(const SampleEntry& sample)
  313. {
  314. if (AZStd::find(m_availableSamples.begin(), m_availableSamples.end(), sample) == m_availableSamples.end())
  315. {
  316. m_availableSamples.push_back(sample);
  317. m_groupedSamples[sample.m_parentMenuName].push_back(static_cast<int32_t>(m_availableSamples.size() - 1));
  318. }
  319. }
  320. ScriptManager* SampleComponentManager::GetScriptManagerInstance()
  321. {
  322. AZ_Assert(m_scriptManager, "Script Manager is nullptr");
  323. return m_scriptManager.get();
  324. }
  325. ScriptableImGui* SampleComponentManager::GetScriptableImGuiInstance()
  326. {
  327. AZ_Assert(m_scriptableImGui, "Scriptable ImGui is nullptr");
  328. return m_scriptableImGui.get();
  329. }
  330. SampleComponentManager::SampleComponentManager()
  331. : m_imguiFrameCaptureSaver("@user@/frame_capture.xml")
  332. {
  333. m_imGuiFrameTimer = AZStd::make_unique<ImGuiHistogramQueue>(FrameTimeDefaultLogSize, FrameTimeDefaultLogSize, 250.0f);
  334. m_exampleEntity = aznew AZ::Entity();
  335. m_entityContextId = AzFramework::EntityContextId::CreateNull();
  336. }
  337. SampleComponentManager::~SampleComponentManager()
  338. {
  339. m_exampleEntity = nullptr;
  340. m_cameraEntity = nullptr;
  341. m_windowContext = nullptr;
  342. m_availableSamples.clear();
  343. }
  344. void SampleComponentManager::Init()
  345. {
  346. AZStd::vector<SampleEntry> samples = GetSamples();
  347. for (const SampleEntry& sample : samples)
  348. {
  349. RegisterSampleComponent(sample);
  350. }
  351. m_scriptManager = AZStd::make_unique<ScriptManager>();
  352. m_scriptableImGui = AZStd::make_unique<ScriptableImGui>();
  353. }
  354. void SampleComponentManager::Activate()
  355. {
  356. // We can only initialize this component after the asset catalog has been loaded.
  357. AzFramework::AssetCatalogEventBus::Handler::BusConnect();
  358. AZ::Render::ImGuiSystemNotificationBus::Handler::BusConnect();
  359. SampleComponentSingletonRequestBus::Handler::BusConnect();
  360. auto* passSystem = AZ::RPI::PassSystemInterface::Get();
  361. AZ_Assert(passSystem, "Cannot get the pass system.");
  362. passSystem->AddPassCreator(Name("RayTracingAmbientOcclusionPass"), &AZ::Render::RayTracingAmbientOcclusionPass::Create);
  363. }
  364. void SampleComponentManager::ActivateInternal()
  365. {
  366. using namespace AZ;
  367. AZ::ApplicationTypeQuery appType;
  368. ComponentApplicationBus::Broadcast(&AZ::ComponentApplicationBus::Events::QueryApplicationType, appType);
  369. if (!appType.IsValid() || !appType.IsGame())
  370. {
  371. return;
  372. }
  373. // 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
  374. // LoadCatalog call in AtomSampleViewer system component. Although load the same asset catalog twice doesn't seem to cause other issue.
  375. if (m_wasActivated)
  376. {
  377. return;
  378. }
  379. Render::Bootstrap::DefaultWindowBus::BroadcastResult(m_windowContext, &Render::Bootstrap::DefaultWindowBus::Events::GetDefaultWindowContext);
  380. AzFramework::GameEntityContextRequestBus::BroadcastResult(m_entityContextId, &AzFramework::GameEntityContextRequestBus::Events::GetGameEntityContextId);
  381. CreateDefaultCamera();
  382. // Add customized pass classes
  383. auto* passSystem = RPI::PassSystemInterface::Get();
  384. passSystem->AddPassCreator(Name("RHISamplePass"), &AtomSampleViewer::RHISamplePass::Create);
  385. // Use scene and render pipeline for RHI samples as default scene and render pipeline
  386. CreateSceneForRHISample();
  387. m_exampleEntity->Init();
  388. m_exampleEntity->Activate();
  389. m_isSampleSupported.resize(m_availableSamples.size());
  390. for (size_t i = 0; i < m_availableSamples.size(); ++i)
  391. {
  392. // Assume that the sample is supported if no m_isSupportedFunc is provided.
  393. m_isSampleSupported[i] = m_availableSamples[i].m_isSupportedFunc ? m_availableSamples[i].m_isSupportedFunc() : true;
  394. }
  395. AZ_Printf("SampleComponentManager", "Available Samples -------------------------\n");
  396. for (size_t i = 0; i < m_availableSamples.size(); ++i)
  397. {
  398. AZStd::string printStr = "\t[" + m_availableSamples[i].m_fullName + "]";
  399. if (!m_isSampleSupported[i])
  400. {
  401. printStr += " Not Supported ";
  402. }
  403. if (i < 9)
  404. {
  405. printStr += AZStd::string::format("\tctrl+%zu", i + 1);
  406. }
  407. printStr += "\n";
  408. AZ_Printf("SampleComponentManager", printStr.data());
  409. }
  410. AZ_Printf("SampleComponentManager", "-------------------------------------------\n");
  411. AzFramework::InputChannelEventListener::BusConnect();
  412. TickBus::Handler::BusConnect();
  413. [[maybe_unused]] bool targetSampleFound = false;
  414. if (AZStd::string targetSampleName = GetTargetSampleName();
  415. !targetSampleName.empty())
  416. {
  417. AZStd::to_lower(targetSampleName.begin(), targetSampleName.end());
  418. for (int32_t i = 0; i < m_availableSamples.size(); ++i)
  419. {
  420. AZStd::string sampleName = m_availableSamples[i].m_fullName;
  421. AZStd::to_lower(sampleName.begin(), sampleName.end());
  422. if (sampleName == targetSampleName)
  423. {
  424. if (m_isSampleSupported[i])
  425. {
  426. if (m_availableSamples[i].m_contentWarning.empty())
  427. {
  428. m_selectedSampleIndex = i;
  429. m_sampleChangeRequest = true;
  430. }
  431. else
  432. {
  433. m_contentWarningDialog.OpenPopupConfirmation(
  434. m_availableSamples[i].m_contentWarningTitle,
  435. m_availableSamples[i].m_contentWarning,
  436. [this, i]() {
  437. m_selectedSampleIndex = i;
  438. m_sampleChangeRequest = true;
  439. });
  440. }
  441. targetSampleFound = true;
  442. }
  443. break;
  444. }
  445. }
  446. AZ_Warning("SampleComponentManager", targetSampleFound, "Failed find target sample %s", targetSampleName.c_str());
  447. }
  448. const AzFramework::CommandLine* commandLine = nullptr;
  449. AzFramework::ApplicationRequests::Bus::BroadcastResult(commandLine, &AzFramework::ApplicationRequests::GetCommandLine);
  450. if (commandLine->HasSwitch("timingSamples"))
  451. {
  452. AZStd::string timingSamplesStr = commandLine->GetSwitchValue("timingSamples", 0);
  453. int timingSamplesCount = 0;
  454. if (AZ::StringFunc::LooksLikeInt(timingSamplesStr.c_str(), &timingSamplesCount))
  455. {
  456. timingSamplesCount = AZStd::clamp<int>(timingSamplesCount, FrameTimeMinLogSize, FrameTimeMaxLogSize);
  457. m_imGuiFrameTimer = AZStd::make_unique<ImGuiHistogramQueue>(timingSamplesCount, timingSamplesCount, 250.0f);
  458. }
  459. }
  460. // Set default screenshot folder to relative path 'Screenshots'
  461. AZ::IO::Path screenshotFolder = "Screenshots";
  462. // Get folder from command line if it exists
  463. static const char* screenshotFolderFlagName = "screenshotFolder";
  464. if (commandLine->HasSwitch(screenshotFolderFlagName))
  465. {
  466. screenshotFolder = commandLine->GetSwitchValue(screenshotFolderFlagName, 0);
  467. }
  468. // Make the screenshot directory relative to the Writeable Storage Path
  469. // The Path::operator/ smartly knows how to concatenate two absolute paths
  470. // i.e <absolute path1> / <absolute path2> = <absolute path2>
  471. auto settingsRegistry = AZ::SettingsRegistry::Get();
  472. AZ::IO::Path writableStoragePath;
  473. settingsRegistry->Get(writableStoragePath.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_DevWriteStorage);
  474. screenshotFolder = writableStoragePath / screenshotFolder;
  475. m_imguiFrameCaptureSaver.SetDefaultFolder(screenshotFolder.Native());
  476. m_imguiFrameCaptureSaver.SetDefaultFileName("screenshot");
  477. m_imguiFrameCaptureSaver.SetAvailableExtensions({ "png", "ppm", "dds" });
  478. m_imguiFrameCaptureSaver.Activate();
  479. SampleComponentManagerRequestBus::Handler::BusConnect();
  480. m_scriptManager->Activate();
  481. m_wasActivated = true;
  482. SampleComponentManagerNotificationBus::Broadcast(&SampleComponentManagerNotificationBus::Events::OnSampleManagerActivated);
  483. }
  484. void SampleComponentManager::Deactivate()
  485. {
  486. DestroyEntity(m_cameraEntity);
  487. AzFramework::AssetCatalogEventBus::Handler::BusDisconnect();
  488. AZ::Render::ImGuiSystemNotificationBus::Handler::BusDisconnect();
  489. m_scriptManager->Deactivate();
  490. m_imguiFrameCaptureSaver.Deactivate();
  491. SampleComponentSingletonRequestBus::Handler::BusDisconnect();
  492. SampleComponentManagerRequestBus::Handler::BusDisconnect();
  493. AZ::TickBus::Handler::BusDisconnect();
  494. AzFramework::InputChannelEventListener::Disconnect();
  495. Render::ImGuiSystemRequestBus::Broadcast(&Render::ImGuiSystemRequests::PopActiveContext);
  496. m_imguiPassTree.Reset();
  497. m_imguiFrameGraphVisualizer.Reset();
  498. m_windowContext = nullptr;
  499. m_brdfTexture.reset();
  500. ReleaseRHIScene();
  501. ReleaseRPIScene();
  502. }
  503. void SampleComponentManager::OnTick(float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  504. {
  505. if (auto* xrSystem = AZ::RPI::RPISystemInterface::Get()->GetXRSystem())
  506. {
  507. EnableRenderPipeline(xrSystem->GetRHIXRRenderingInterface()->IsDefaultRenderPipelineEnabledOnHost());
  508. //Only enable XR pipelines if the XR drivers indicate we have accurate pose information from the device
  509. EnableXrPipelines(xrSystem->ShouldRender());
  510. }
  511. if (m_imGuiFrameTimer)
  512. {
  513. m_imGuiFrameTimer->PushValue(deltaTime * 1000.0f);
  514. }
  515. bool screenshotRequest = false;
  516. if (m_ctrlModifierLDown || m_ctrlModifierRDown)
  517. {
  518. if (m_alphanumericQDown)
  519. {
  520. RequestExit();
  521. return;
  522. }
  523. if (m_alphanumericTDown && m_canCaptureRADTM)
  524. {
  525. #ifdef AZ_PROFILE_TELEMETRY
  526. Utils::ToggleRadTMCapture();
  527. m_canCaptureRADTM = false;
  528. #endif
  529. }
  530. else if (!m_alphanumericTDown)
  531. {
  532. m_canCaptureRADTM = true;
  533. }
  534. if (m_alphanumericPDown)
  535. {
  536. screenshotRequest = true;
  537. }
  538. }
  539. // Request a frame capture only once per key press, even if the keys are held down for multiple ticks.
  540. if (screenshotRequest)
  541. {
  542. ++m_screenshotKeyDownCount;
  543. if (m_screenshotKeyDownCount == 1)
  544. {
  545. RequestFrameCapture(m_imguiFrameCaptureSaver.GetNextAutoSaveFilePath(), true);
  546. }
  547. }
  548. else
  549. {
  550. m_screenshotKeyDownCount = 0;
  551. }
  552. RenderImGui(deltaTime);
  553. // SampleChange needs to happen after RenderImGui as some of the samples create sidebars on a separate ImGui context
  554. // which can interfere with m_imguiContext in the first frame
  555. if (m_sampleChangeRequest && m_canSwitchSample)
  556. {
  557. SampleChange();
  558. m_canSwitchSample = false;
  559. }
  560. else if (m_escapeDown && m_canSwitchSample)
  561. {
  562. Reset();
  563. m_canSwitchSample = false;
  564. Utils::ReportScriptableAction("OpenSample('')");
  565. }
  566. // 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
  567. if (!m_sampleChangeRequest && !m_escapeDown)
  568. {
  569. m_canSwitchSample = true;
  570. }
  571. // Since the event has been handled, clear the request
  572. m_sampleChangeRequest = false;
  573. m_escapeDown = false;
  574. m_scriptManager->TickScript(deltaTime);
  575. if (m_isFrameCapturePending)
  576. {
  577. if (m_countdownForFrameCapture > 0)
  578. {
  579. m_countdownForFrameCapture--;
  580. }
  581. else if (m_countdownForFrameCapture == 0)
  582. {
  583. AZ::Render::FrameCaptureOutcome capOutcome;
  584. AZ::Render::FrameCaptureRequestBus::BroadcastResult(capOutcome, &AZ::Render::FrameCaptureRequestBus::Events::CaptureScreenshot, m_frameCaptureFilePath);
  585. if (capOutcome.IsSuccess()) // if unsuccessfull leave state to attempt again next tick
  586. {
  587. m_frameCaptureId = capOutcome.GetValue();
  588. AZ::Render::FrameCaptureNotificationBus::Handler::BusConnect(m_frameCaptureId);
  589. m_countdownForFrameCapture = -1; // Don't call CaptureScreenshot again
  590. }
  591. else
  592. {
  593. m_frameCaptureId = AZ::Render::InvalidFrameCaptureId;
  594. }
  595. }
  596. }
  597. }
  598. bool SampleComponentManager::OnInputChannelEventFiltered(const AzFramework::InputChannel& inputChannel)
  599. {
  600. const size_t samplesAvailableCount = m_availableSamples.size();
  601. AZStd::vector<AzFramework::InputChannelId> sampleInputMapping;
  602. sampleInputMapping.reserve(samplesAvailableCount);
  603. for (size_t i = 0; i < samplesAvailableCount; ++i)
  604. {
  605. switch (i)
  606. {
  607. case 0:
  608. sampleInputMapping.push_back(AzFramework::InputDeviceKeyboard::Key::Alphanumeric1);
  609. break;
  610. case 1:
  611. sampleInputMapping.push_back(AzFramework::InputDeviceKeyboard::Key::Alphanumeric2);
  612. break;
  613. case 2:
  614. sampleInputMapping.push_back(AzFramework::InputDeviceKeyboard::Key::Alphanumeric3);
  615. break;
  616. case 3:
  617. sampleInputMapping.push_back(AzFramework::InputDeviceKeyboard::Key::Alphanumeric4);
  618. break;
  619. case 4:
  620. sampleInputMapping.push_back(AzFramework::InputDeviceKeyboard::Key::Alphanumeric5);
  621. break;
  622. case 5:
  623. sampleInputMapping.push_back(AzFramework::InputDeviceKeyboard::Key::Alphanumeric6);
  624. break;
  625. case 6:
  626. sampleInputMapping.push_back(AzFramework::InputDeviceKeyboard::Key::Alphanumeric7);
  627. break;
  628. case 7:
  629. sampleInputMapping.push_back(AzFramework::InputDeviceKeyboard::Key::Alphanumeric8);
  630. break;
  631. case 8:
  632. sampleInputMapping.push_back(AzFramework::InputDeviceKeyboard::Key::Alphanumeric9);
  633. break;
  634. case 9:
  635. sampleInputMapping.push_back(AzFramework::InputDeviceKeyboard::Key::Alphanumeric0);
  636. break;
  637. default:
  638. break;
  639. }
  640. }
  641. const AzFramework::InputChannelId& inputChannelId = inputChannel.GetInputChannelId();
  642. switch (inputChannel.GetState())
  643. {
  644. case AzFramework::InputChannel::State::Began:
  645. case AzFramework::InputChannel::State::Updated:
  646. {
  647. if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::ModifierCtrlL)
  648. {
  649. m_ctrlModifierLDown = true;
  650. }
  651. else if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::ModifierCtrlR)
  652. {
  653. m_ctrlModifierRDown = true;
  654. }
  655. else if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::AlphanumericQ)
  656. {
  657. m_alphanumericQDown = true;
  658. }
  659. else if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::AlphanumericT)
  660. {
  661. m_alphanumericTDown = true;
  662. }
  663. else if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::AlphanumericP)
  664. {
  665. m_alphanumericPDown = true;
  666. }
  667. else if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::Escape)
  668. {
  669. m_escapeDown = true;
  670. }
  671. break;
  672. }
  673. case AzFramework::InputChannel::State::Ended:
  674. {
  675. if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::ModifierCtrlL)
  676. {
  677. m_ctrlModifierLDown = false;
  678. }
  679. else if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::ModifierCtrlR)
  680. {
  681. m_ctrlModifierRDown = false;
  682. }
  683. else if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::AlphanumericQ)
  684. {
  685. m_alphanumericQDown = false;
  686. }
  687. else if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::AlphanumericT)
  688. {
  689. m_alphanumericTDown = false;
  690. }
  691. else if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::AlphanumericP)
  692. {
  693. m_alphanumericPDown = false;
  694. }
  695. else if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::Escape)
  696. {
  697. m_escapeDown = false;
  698. }
  699. break;
  700. }
  701. default:
  702. {
  703. break;
  704. }
  705. }
  706. return false;
  707. }
  708. void SampleComponentManager::RenderImGui(float deltaTime)
  709. {
  710. if (!m_isImGuiAvailable)
  711. {
  712. return;
  713. }
  714. ShowMenuBar();
  715. if (m_exitRequested)
  716. {
  717. return;
  718. }
  719. if (m_showResizeViewportDialog)
  720. {
  721. ShowResizeViewportDialog();
  722. }
  723. if (m_showFramerateHistogram)
  724. {
  725. ShowFramerateHistogram(deltaTime);
  726. }
  727. if (m_showFrameCaptureDialog)
  728. {
  729. ShowFrameCaptureDialog();
  730. }
  731. if (m_showImGuiMetrics)
  732. {
  733. ImGui::ShowMetricsWindow(&m_showImGuiMetrics);
  734. }
  735. if (m_showSampleHelper)
  736. {
  737. ShowSampleHelper();
  738. }
  739. if (m_showAbout)
  740. {
  741. ShowAboutWindow();
  742. }
  743. if (m_showPassTree)
  744. {
  745. ShowPassTreeWindow();
  746. }
  747. if (m_showFrameGraphVisualizer)
  748. {
  749. ShowFrameGraphVisualizerWindow();
  750. }
  751. if (m_showCullingDebugWindow)
  752. {
  753. AZ::RPI::Scene* rpiScene = AZ::RPI::RPISystemInterface::Get()->GetSceneByName(AZ::Name("RPI"));
  754. if (rpiScene)
  755. {
  756. AZ::Render::ImGuiDrawCullingDebug(m_showCullingDebugWindow, rpiScene);
  757. }
  758. }
  759. if (m_showCpuProfiler)
  760. {
  761. ShowCpuProfilerWindow();
  762. }
  763. if (m_showGpuProfiler)
  764. {
  765. ShowGpuProfilerWindow();
  766. }
  767. if (m_showFileIoProfiler)
  768. {
  769. ShowFileIoProfilerWindow();
  770. }
  771. if (m_showTransientAttachmentProfiler)
  772. {
  773. ShowTransientAttachmentProfilerWindow();
  774. }
  775. m_scriptManager->TickImGui();
  776. m_contentWarningDialog.TickPopup();
  777. }
  778. void SampleComponentManager::ShowMenuBar()
  779. {
  780. if (ImGui::BeginMainMenuBar())
  781. {
  782. // If imgui doesn't have enough room to render a menu, it will fall back to the safe area which
  783. // is typically 3 pixels. This causes the menu to overlap the menu bar, and makes it easy to
  784. // accidentally select the first item on that menu bar. By altering the safe area temporarily
  785. // while drawing the menu, this problem can be avoided.
  786. ImVec2 cachedSafeArea = ImGui::GetStyle().DisplaySafeAreaPadding;
  787. ImGui::GetStyle().DisplaySafeAreaPadding = ImVec2(cachedSafeArea.x, cachedSafeArea.y + 16.0f);
  788. if (ImGui::BeginMenu("File"))
  789. {
  790. if (ImGui::MenuItem("Exit", "Ctrl-Q"))
  791. {
  792. RequestExit();
  793. }
  794. if (ImGui::MenuItem("Capture Frame...", "Ctrl-P"))
  795. {
  796. m_showFrameCaptureDialog = true;
  797. }
  798. ImGui::EndMenu();
  799. }
  800. if (ImGui::BeginMenu("View"))
  801. {
  802. if (Utils::SupportsResizeClientArea() && ImGui::MenuItem("Resize Viewport..."))
  803. {
  804. m_showResizeViewportDialog = true;
  805. }
  806. if (Utils::SupportsToggleFullScreenOfDefaultWindow() && ImGui::MenuItem("Toggle Full Screen"))
  807. {
  808. Utils::ToggleFullScreenOfDefaultWindow();
  809. }
  810. if (ImGui::MenuItem("Frame Time Histogram"))
  811. {
  812. m_showFramerateHistogram = !m_showFramerateHistogram;
  813. }
  814. if (ImGui::MenuItem("ImGui Metrics"))
  815. {
  816. m_showImGuiMetrics = !m_showImGuiMetrics;
  817. }
  818. if (ImGui::MenuItem("Sample Helper"))
  819. {
  820. m_showSampleHelper = !m_showSampleHelper;
  821. }
  822. if (ImGui::MenuItem("Frame Graph Visualizer"))
  823. {
  824. m_showFrameGraphVisualizer = !m_showFrameGraphVisualizer;
  825. }
  826. ImGui::EndMenu();
  827. }
  828. if (ImGui::BeginMenu("Samples"))
  829. {
  830. for (auto&& [parentMenuName, samples] : m_groupedSamples)
  831. {
  832. if (ImGui::BeginMenu(parentMenuName.c_str()))
  833. {
  834. for (int32_t index : samples)
  835. {
  836. SampleEntry& sample = m_availableSamples[index];
  837. const char* sampleName = sample.m_sampleName.c_str();
  838. bool enabled = m_isSampleSupported[index];
  839. if (ImGui::MenuItem(sampleName, nullptr, false, enabled))
  840. {
  841. Utils::ReportScriptableAction("OpenSample('%s')", sample.m_sampleName.c_str());
  842. if (sample.m_contentWarning.empty())
  843. {
  844. m_sampleChangeRequest = true;
  845. m_selectedSampleIndex = index;
  846. }
  847. else
  848. {
  849. m_contentWarningDialog.OpenPopupConfirmation(
  850. sample.m_contentWarningTitle,
  851. sample.m_contentWarning,
  852. [this, index]() {
  853. m_sampleChangeRequest = true;
  854. m_selectedSampleIndex = index;
  855. });
  856. }
  857. }
  858. }
  859. ImGui::EndMenu();
  860. }
  861. }
  862. ImGui::EndMenu();
  863. }
  864. #ifdef AZ_PROFILE_TELEMETRY
  865. if (ImGui::BeginMenu("RADTelemetry"))
  866. {
  867. if (ImGui::MenuItem("Toggle Capture", "Ctrl-T"))
  868. {
  869. Utils::ToggleRadTMCapture();
  870. }
  871. ImGui::EndMenu();
  872. }
  873. #endif // AZ_PROFILE_TELEMETRY
  874. if (ImGui::BeginMenu("Automation"))
  875. {
  876. const char* AutomationContentWarningTitle = AtomSampleComponent::CommonPhotosensitiveWarningTitle;
  877. const char* AutomationContentWarning = "Running automated scripts will trigger flashing images that could cause seizures or other adverse effects in photosensitive individuals.";
  878. if (ImGui::MenuItem("Run Script..."))
  879. {
  880. m_contentWarningDialog.OpenPopupConfirmation(
  881. AutomationContentWarningTitle,
  882. AutomationContentWarning,
  883. [this]() {
  884. m_scriptManager->OpenScriptRunnerDialog();
  885. });
  886. }
  887. if (ImGui::MenuItem("Run Precommit Wizard..."))
  888. {
  889. m_contentWarningDialog.OpenPopupConfirmation(
  890. AutomationContentWarningTitle,
  891. AutomationContentWarning,
  892. [this]() {
  893. m_scriptManager->OpenPrecommitWizard();
  894. });
  895. }
  896. ImGui::EndMenu();
  897. }
  898. if (ImGui::BeginMenu("Pass"))
  899. {
  900. if (ImGui::MenuItem(PassTreeToolName))
  901. {
  902. m_showPassTree = !m_showPassTree;
  903. Utils::ReportScriptableAction("ShowTool('%s', %s)", PassTreeToolName, m_showPassTree?"true":"false");
  904. }
  905. ImGui::EndMenu();
  906. }
  907. if (ImGui::BeginMenu("Culling"))
  908. {
  909. if (ImGui::MenuItem("Culling Debug Window"))
  910. {
  911. m_showCullingDebugWindow = !m_showCullingDebugWindow;
  912. }
  913. ImGui::EndMenu();
  914. }
  915. if (ImGui::BeginMenu("Profile"))
  916. {
  917. if (ImGui::MenuItem(CpuProfilerToolName))
  918. {
  919. m_showCpuProfiler = !m_showCpuProfiler;
  920. if (auto profilerSystem = AZ::Debug::ProfilerSystemInterface::Get(); profilerSystem)
  921. {
  922. profilerSystem->SetActive(m_showCpuProfiler);
  923. }
  924. Utils::ReportScriptableAction("ShowTool('%s', %s)", CpuProfilerToolName, m_showCpuProfiler ? "true" : "false");
  925. }
  926. if (AZ::IO::StreamerProfiler::Get() != nullptr)
  927. {
  928. if (ImGui::MenuItem(FileIoProfilerToolName))
  929. {
  930. m_showFileIoProfiler = !m_showFileIoProfiler;
  931. Utils::ReportScriptableAction(
  932. "ShowTool('%s', %s)", FileIoProfilerToolName, m_showFileIoProfiler ? "true" : "false");
  933. }
  934. }
  935. if (ImGui::MenuItem(GpuProfilerToolName))
  936. {
  937. m_showGpuProfiler = !m_showGpuProfiler;
  938. Utils::ReportScriptableAction("ShowTool('%s', %s)", GpuProfilerToolName, m_showGpuProfiler ? "true" : "false");
  939. }
  940. if (ImGui::MenuItem(TransientAttachmentProfilerToolName))
  941. {
  942. m_showTransientAttachmentProfiler = !m_showTransientAttachmentProfiler;
  943. AZ::RHI::RHISystemInterface::Get()->ModifyFrameSchedulerStatisticsFlags(
  944. AZ::RHI::FrameSchedulerStatisticsFlags::GatherTransientAttachmentStatistics,
  945. m_showTransientAttachmentProfiler);
  946. Utils::ReportScriptableAction("ShowTool('%s', %s)", TransientAttachmentProfilerToolName, m_showTransientAttachmentProfiler ? "true" : "false");
  947. }
  948. ImGui::EndMenu();
  949. }
  950. if (ImGui::BeginMenu("Help"))
  951. {
  952. if (ImGui::MenuItem("About"))
  953. {
  954. m_showAbout = !m_showAbout;
  955. }
  956. ImGui::EndMenu();
  957. }
  958. // Restore original safe area.
  959. ImGui::GetStyle().DisplaySafeAreaPadding = cachedSafeArea;
  960. ImGui::EndMainMenuBar();
  961. }
  962. }
  963. void SampleComponentManager::ShowSampleHelper()
  964. {
  965. if (ImGui::Begin("Sample Helper", &m_showSampleHelper, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings))
  966. {
  967. if (ImGui::Button("Reset"))
  968. {
  969. //Removes the existing sample component and
  970. //resets the selection index
  971. Reset();
  972. CameraReset();
  973. m_selectedSampleIndex = -1;
  974. }
  975. ImGui::SameLine();
  976. if (ImGui::Button("Reset Sample"))
  977. {
  978. //Force a sample change event when the selection index
  979. //hasn't changed. This resets the sample component.
  980. SampleChange();
  981. }
  982. ImGui::SameLine();
  983. if (ImGui::Button("Reset Camera"))
  984. {
  985. CameraReset();
  986. }
  987. }
  988. ImGui::End();
  989. }
  990. void SampleComponentManager::ShowAboutWindow()
  991. {
  992. if (ImGui::Begin("About", &m_showAbout, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings))
  993. {
  994. ImGui::Text("RHI API: %s", AZ::RHI::Factory::Get().GetName().GetCStr());
  995. }
  996. ImGui::End();
  997. }
  998. void SampleComponentManager::ShowPassTreeWindow()
  999. {
  1000. m_imguiPassTree.Draw(m_showPassTree, AZ::RPI::PassSystemInterface::Get()->GetRootPass().get());
  1001. }
  1002. void SampleComponentManager::ShowFrameGraphVisualizerWindow()
  1003. {
  1004. AZ::RHI::Device* rhiDevice = Utils::GetRHIDevice().get();
  1005. m_imguiFrameGraphVisualizer.Init(rhiDevice);
  1006. m_imguiFrameGraphVisualizer.Draw(m_showFrameGraphVisualizer);
  1007. }
  1008. void SampleComponentManager::ShowCpuProfilerWindow()
  1009. {
  1010. if (auto profilerImGui = Profiler::ProfilerImGuiInterface::Get(); profilerImGui)
  1011. {
  1012. profilerImGui->ShowCpuProfilerWindow(m_showCpuProfiler);
  1013. }
  1014. }
  1015. void SampleComponentManager::ShowFileIoProfilerWindow()
  1016. {
  1017. if (auto profilerImGui = AZ::IO::StreamerProfiler::Get(); profilerImGui)
  1018. {
  1019. profilerImGui->DrawStatistics(m_showFileIoProfiler);
  1020. }
  1021. }
  1022. void SampleComponentManager::ShowGpuProfilerWindow()
  1023. {
  1024. m_imguiGpuProfiler.Draw(m_showGpuProfiler, AZ::RPI::PassSystemInterface::Get()->GetRootPass());
  1025. }
  1026. void SampleComponentManager::ShowTransientAttachmentProfilerWindow()
  1027. {
  1028. const auto& transientStats = AZ::RHI::RHIMemoryStatisticsInterface::Get()->GetTransientAttachmentStatistics();
  1029. if (!transientStats.empty())
  1030. {
  1031. m_showTransientAttachmentProfiler = m_imguiTransientAttachmentProfiler.Draw(transientStats);
  1032. }
  1033. }
  1034. void SampleComponentManager::ShowResizeViewportDialog()
  1035. {
  1036. static int size[2] = { 0, 0 };
  1037. if (size[0] <= 0)
  1038. {
  1039. size[0] = aznumeric_cast<int>(m_windowContext->GetViewport().m_maxX - m_windowContext->GetViewport().m_minX);
  1040. }
  1041. if (size[1] <= 0)
  1042. {
  1043. size[1] = aznumeric_cast<int>(m_windowContext->GetViewport().m_maxY - m_windowContext->GetViewport().m_minY);
  1044. }
  1045. bool dialogWasOpen = m_showResizeViewportDialog;
  1046. if (ImGui::Begin("Resize Viewport", &m_showResizeViewportDialog, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings))
  1047. {
  1048. ImGui::InputInt2("Size", size);
  1049. if (ImGui::Button("Resize"))
  1050. {
  1051. Utils::ResizeClientArea(size[0], size[1], AzFramework::WindowPosOptions());
  1052. Utils::ReportScriptableAction("ResizeViewport(%d, %d)", size[0], size[1]);
  1053. // Re-initialize the size fields on the next frame so we can see whether the
  1054. // correct size was achieved (should be the same values the user entered)...
  1055. size[0] = 0;
  1056. size[1] = 0;
  1057. }
  1058. }
  1059. ImGui::End();
  1060. if (dialogWasOpen && !m_showResizeViewportDialog)
  1061. {
  1062. // Re-initialize the size fields next time the dialog is shown...
  1063. size[0] = 0;
  1064. size[1] = 0;
  1065. }
  1066. }
  1067. void SampleComponentManager::ShowFramerateHistogram(float deltaTime)
  1068. {
  1069. if (m_imGuiFrameTimer && ImGui::Begin("Frame Time Histogram", &m_showFramerateHistogram, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings))
  1070. {
  1071. ImGuiHistogramQueue::WidgetSettings settings;
  1072. settings.m_reportInverse = false;
  1073. settings.m_units = "ms";
  1074. m_imGuiFrameTimer->Tick(deltaTime * 1000.0f, settings);
  1075. }
  1076. ImGui::End();
  1077. }
  1078. void SampleComponentManager::RequestFrameCapture(const AZStd::string& filePath, bool hideImGui)
  1079. {
  1080. AZ_Assert(false == m_isFrameCapturePending, "Frame capture already in progress");
  1081. AZ_Assert(AZ::Render::InvalidFrameCaptureId == m_frameCaptureId, "Unfinished frame capture detected");
  1082. m_isFrameCapturePending = true;
  1083. m_hideImGuiDuringFrameCapture = hideImGui;
  1084. m_frameCaptureFilePath = filePath;
  1085. // Don't continue the script while a frame capture is pending in case subsequent changes
  1086. // interfere with the pending capture.
  1087. ScriptRunnerRequestBus::Broadcast(&ScriptRunnerRequests::PauseScript);
  1088. if (m_hideImGuiDuringFrameCapture)
  1089. {
  1090. AZ::Render::ImGuiSystemRequestBus::Broadcast(&AZ::Render::ImGuiSystemRequestBus::Events::HideAllImGuiPasses);
  1091. // We also hide Open 3D Engine's debug text
  1092. AzFramework::ConsoleRequestBus::Broadcast(&AzFramework::ConsoleRequests::ExecuteConsoleCommand, "r_DisplayInfo 0");
  1093. // The ExecuteConsoleCommand request is handled in a deferred manner, so we have to delay the screenshot a bit.
  1094. m_countdownForFrameCapture = 1;
  1095. }
  1096. else
  1097. {
  1098. m_countdownForFrameCapture = 0;
  1099. }
  1100. }
  1101. void SampleComponentManager::OnFrameCaptureFinished(AZ::Render::FrameCaptureResult /*result*/, const AZStd::string& /*info*/)
  1102. {
  1103. AZ::Render::FrameCaptureNotificationBus::Handler::BusDisconnect();
  1104. if (m_hideImGuiDuringFrameCapture)
  1105. {
  1106. AZ::Render::ImGuiSystemRequestBus::Broadcast(&AZ::Render::ImGuiSystemRequestBus::Events::ShowAllImGuiPasses);
  1107. // We also show Open 3D Engine's debug text
  1108. AzFramework::ConsoleRequestBus::Broadcast(&AzFramework::ConsoleRequests::ExecuteConsoleCommand, "r_DisplayInfo 1");
  1109. }
  1110. ScriptRunnerRequestBus::Broadcast(&ScriptRunnerRequests::ResumeScript);
  1111. m_isFrameCapturePending = false;
  1112. }
  1113. bool SampleComponentManager::IsFrameCapturePending()
  1114. {
  1115. return m_isFrameCapturePending;
  1116. }
  1117. void SampleComponentManager::RunMainTestSuite(const AZStd::string& suiteFilePath, bool exitOnTestEnd, int randomSeed)
  1118. {
  1119. if (m_scriptManager)
  1120. {
  1121. m_scriptManager->RunMainTestSuite(suiteFilePath, exitOnTestEnd, randomSeed);
  1122. }
  1123. }
  1124. void SampleComponentManager::SetNumMSAASamples(int16_t numMSAASamples)
  1125. {
  1126. AZ_Assert(IsValidNumMSAASamples(numMSAASamples), "Invalid MSAA sample setting");
  1127. m_numMsaaSamples = numMSAASamples;
  1128. }
  1129. int16_t SampleComponentManager::GetNumMSAASamples()
  1130. {
  1131. return m_numMsaaSamples;
  1132. }
  1133. void SampleComponentManager::SetDefaultNumMSAASamples(int16_t defaultNumMsaaSamples)
  1134. {
  1135. m_defaultNumMsaaSamples = defaultNumMsaaSamples;
  1136. }
  1137. int16_t SampleComponentManager::GetDefaultNumMSAASamples()
  1138. {
  1139. return m_defaultNumMsaaSamples;
  1140. }
  1141. void SampleComponentManager::ResetNumMSAASamples()
  1142. {
  1143. m_numMsaaSamples = m_defaultNumMsaaSamples;
  1144. }
  1145. void SampleComponentManager::ResetRPIScene()
  1146. {
  1147. ReleaseRPIScene();
  1148. SwitchSceneForRPISample();
  1149. }
  1150. void SampleComponentManager::ClearRPIScene()
  1151. {
  1152. ReleaseRPIScene();
  1153. }
  1154. void SampleComponentManager::EnableRenderPipeline(bool value)
  1155. {
  1156. if (m_renderPipeline)
  1157. {
  1158. if (value)
  1159. {
  1160. m_renderPipeline->AddToRenderTick();
  1161. }
  1162. else
  1163. {
  1164. m_renderPipeline->RemoveFromRenderTick();
  1165. }
  1166. }
  1167. }
  1168. void SampleComponentManager::EnableXrPipelines(bool value)
  1169. {
  1170. for (RPI::RenderPipelinePtr xrPipeline : m_xrPipelines)
  1171. {
  1172. if (xrPipeline)
  1173. {
  1174. if (value)
  1175. {
  1176. xrPipeline->AddToRenderTick();
  1177. }
  1178. else
  1179. {
  1180. xrPipeline->RemoveFromRenderTick();
  1181. }
  1182. }
  1183. }
  1184. }
  1185. void SampleComponentManager::ShowFrameCaptureDialog()
  1186. {
  1187. static bool requestCaptureOnNextFrame = false;
  1188. static bool hideImGuiFromFrameCapture = true;
  1189. if (requestCaptureOnNextFrame)
  1190. {
  1191. requestCaptureOnNextFrame = false;
  1192. RequestFrameCapture(m_imguiFrameCaptureSaver.GetSaveFilePath(), hideImGuiFromFrameCapture);
  1193. }
  1194. else if (!m_isFrameCapturePending) // Hide this dialog while taking a capture
  1195. {
  1196. if (ImGui::Begin("Frame Capture", &m_showFrameCaptureDialog))
  1197. {
  1198. ImGui::Checkbox("Hide ImGui", &hideImGuiFromFrameCapture);
  1199. ImGuiSaveFilePath::WidgetSettings settings;
  1200. settings.m_labels.m_filePath = "File Path (.png, .ppm, or .dds):";
  1201. m_imguiFrameCaptureSaver.Tick(settings);
  1202. if (ImGui::Button("Capture"))
  1203. {
  1204. // Don't actually do the capture until the next frame, so we can hide this dialog first
  1205. requestCaptureOnNextFrame = true;
  1206. if (hideImGuiFromFrameCapture)
  1207. {
  1208. Utils::ReportScriptableAction("CaptureScreenshot('%s')", m_imguiFrameCaptureSaver.GetSaveFilePath().c_str());
  1209. }
  1210. else
  1211. {
  1212. Utils::ReportScriptableAction("CaptureScreenshotWithImGui('%s')", m_imguiFrameCaptureSaver.GetSaveFilePath().c_str());
  1213. }
  1214. }
  1215. }
  1216. ImGui::End();
  1217. }
  1218. }
  1219. void SampleComponentManager::RequestExit()
  1220. {
  1221. AzFramework::ApplicationRequests::Bus::Broadcast(&AzFramework::ApplicationRequests::ExitMainLoop);
  1222. AZ::TickBus::Handler::BusDisconnect();
  1223. AzFramework::InputChannelEventListener::Disconnect();
  1224. m_exitRequested = true;
  1225. }
  1226. void SampleComponentManager::ShutdownActiveSample()
  1227. {
  1228. m_exampleEntity->Deactivate();
  1229. // Pointer to all the passes within m_rhiSamplePasses must be nullified before all the samples within m_activeSamples are destroyed.
  1230. SetRHISamplePass(nullptr);
  1231. for (AZ::Component* activeComponent : m_activeSamples)
  1232. {
  1233. if (activeComponent != nullptr)
  1234. {
  1235. // Disable the camera controller just in case the active sample enabled it and didn't disable in Deactivate().
  1236. AZ::Debug::CameraControllerRequestBus::Event(m_cameraEntity->GetId(), &AZ::Debug::CameraControllerRequestBus::Events::Disable);
  1237. m_exampleEntity->RemoveComponent(activeComponent);
  1238. delete activeComponent;
  1239. }
  1240. }
  1241. m_activeSamples.clear();
  1242. // Force a reset of the shader variant finder to get more consistent testing of samples every time they are run, rather
  1243. // than the first time for each sample being "special".
  1244. auto variantFinder = AZ::Interface<AZ::RPI::IShaderVariantFinder>::Get();
  1245. variantFinder->Reset();
  1246. }
  1247. void SampleComponentManager::Reset()
  1248. {
  1249. ShutdownActiveSample();
  1250. m_exampleEntity->Activate();
  1251. // Reset to RHI sample pipeline
  1252. SwitchSceneForRHISample();
  1253. SetRHISamplePass(nullptr);
  1254. }
  1255. void SampleComponentManager::CreateDefaultCamera()
  1256. {
  1257. if (m_cameraEntity)
  1258. {
  1259. return;
  1260. }
  1261. // A camera entity will be created by the entity context request bus so that the component for this entity can use a feature processor.
  1262. AzFramework::EntityContextRequestBus::EventResult(m_cameraEntity, m_entityContextId, &AzFramework::EntityContextRequestBus::Events::CreateEntity, "CameraEntity");
  1263. //Add debug camera and controller components
  1264. AZ::Debug::CameraComponentConfig cameraConfig(m_windowContext);
  1265. cameraConfig.m_fovY = AZ::Constants::QuarterPi;
  1266. cameraConfig.m_depthFar = 1000.0f;
  1267. m_cameraEntity->CreateComponent(azrtti_typeid<AZ::Debug::CameraComponent>())
  1268. ->SetConfiguration(cameraConfig);
  1269. m_cameraEntity->CreateComponent(azrtti_typeid<AzFramework::TransformComponent>());
  1270. m_cameraEntity->CreateComponent(azrtti_typeid<AZ::Debug::ArcBallControllerComponent>());
  1271. m_cameraEntity->CreateComponent(azrtti_typeid<AZ::Debug::NoClipControllerComponent>());
  1272. m_cameraEntity->Activate();
  1273. m_scriptManager->SetCameraEntity(m_cameraEntity);
  1274. }
  1275. void SampleComponentManager::SetupImGuiContext()
  1276. {
  1277. AdjustImGuiFontScale();
  1278. // Add imgui context
  1279. Render::ImGuiSystemRequestBus::BroadcastResult(m_isImGuiAvailable, &Render::ImGuiSystemRequests::PushActiveContextFromDefaultPass);
  1280. 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.");
  1281. }
  1282. void SampleComponentManager::ActiveImGuiContextChanged(ImGuiContext* context)
  1283. {
  1284. ImGui::SetCurrentContext(context);
  1285. }
  1286. bool SampleComponentManager::OpenSample(const AZStd::string& sampleName)
  1287. {
  1288. for (int32_t i = 0; i < m_availableSamples.size(); i++)
  1289. {
  1290. if (m_availableSamples[i].m_parentMenuName + '/' + m_availableSamples[i].m_sampleName == sampleName)
  1291. {
  1292. if (!m_availableSamples[i].m_isSupportedFunc || m_availableSamples[i].m_isSupportedFunc())
  1293. {
  1294. m_selectedSampleIndex = i;
  1295. m_sampleChangeRequest = true;
  1296. return true;
  1297. }
  1298. else
  1299. {
  1300. AZ_Error("SampleComponentManager", false, "Sample '%s' is not supported on this platform.", sampleName.c_str());
  1301. }
  1302. }
  1303. }
  1304. return false;
  1305. }
  1306. bool SampleComponentManager::ShowTool(const AZStd::string& toolName, bool enable)
  1307. {
  1308. if (toolName == PassTreeToolName)
  1309. {
  1310. m_showPassTree = enable;
  1311. return true;
  1312. }
  1313. else if (toolName == CpuProfilerToolName)
  1314. {
  1315. m_showCpuProfiler = enable;
  1316. return true;
  1317. }
  1318. else if (toolName == FileIoProfilerToolName)
  1319. {
  1320. m_showFileIoProfiler = enable;
  1321. return true;
  1322. }
  1323. else if (toolName == GpuProfilerToolName)
  1324. {
  1325. m_showGpuProfiler = enable;
  1326. return true;
  1327. }
  1328. else if (toolName == TransientAttachmentProfilerToolName)
  1329. {
  1330. m_showTransientAttachmentProfiler = enable;
  1331. return true;
  1332. }
  1333. return false;
  1334. }
  1335. void SampleComponentManager::SampleChange()
  1336. {
  1337. if (m_selectedSampleIndex == -1)
  1338. {
  1339. return;
  1340. }
  1341. ShutdownActiveSample();
  1342. // Reset the camera *before* activating the sample, because the sample's Activate() function might
  1343. // want to reposition the camera.
  1344. CameraReset();
  1345. const SampleEntry& sampleEntry = m_availableSamples[m_selectedSampleIndex];
  1346. // Create scene and render pipeline before create sample component
  1347. if (sampleEntry.m_pipelineType == SamplePipelineType::RHI)
  1348. {
  1349. SwitchSceneForRHISample();
  1350. }
  1351. else if (sampleEntry.m_pipelineType == SamplePipelineType::RPI)
  1352. {
  1353. SwitchSceneForRPISample();
  1354. }
  1355. SampleComponentConfig config(m_windowContext, m_cameraEntity->GetId(), m_entityContextId);
  1356. // special setup for RHI samples
  1357. if (sampleEntry.m_pipelineType == SamplePipelineType::RHI)
  1358. {
  1359. for (AZ::RPI::Ptr<RHISamplePass> samplePass : m_rhiSamplePasses)
  1360. {
  1361. BasicRHIComponent* rhiSampleComponent = static_cast<BasicRHIComponent*>(m_exampleEntity->CreateComponent(sampleEntry.m_sampleUuid));
  1362. rhiSampleComponent->SetConfiguration(config);
  1363. rhiSampleComponent->SetViewIndex(samplePass->GetViewIndex());
  1364. if (rhiSampleComponent->IsSupportedRHISamplePipeline())
  1365. {
  1366. samplePass->SetRHISample(rhiSampleComponent);
  1367. }
  1368. else
  1369. {
  1370. samplePass->SetRHISample(nullptr);
  1371. }
  1372. m_activeSamples.push_back(rhiSampleComponent);
  1373. }
  1374. }
  1375. else
  1376. {
  1377. AZ::Component* newComponent = m_exampleEntity->CreateComponent(sampleEntry.m_sampleUuid);
  1378. newComponent->SetConfiguration(config);
  1379. m_activeSamples.push_back(newComponent);
  1380. }
  1381. m_exampleEntity->Activate();
  1382. // Even though this is done in CameraReset(), the example component wasn't activated at the time so we have to send this event again.
  1383. ExampleComponentRequestBus::Event(m_exampleEntity->GetId(), &ExampleComponentRequestBus::Events::ResetCamera);
  1384. }
  1385. void SampleComponentManager::CameraReset()
  1386. {
  1387. // Reset the camera transform. Some examples do not use a controller or use a controller that doesn't override the whole transform.
  1388. // Set to a transform that is 5 units away from the origin and looking at the origin along the Y axis.
  1389. const AZ::EntityId cameraEntityId = m_cameraEntity->GetId();
  1390. AZ::TransformBus::Event(cameraEntityId, &AZ::TransformBus::Events::SetWorldTM, AZ::Transform::CreateTranslation(AZ::Vector3(0.0f, -5.0f, 0.0f)));
  1391. AZ::Debug::CameraControllerRequestBus::Event(cameraEntityId, &AZ::Debug::CameraControllerRequestBus::Events::Reset);
  1392. // Tell the current example to reset the camera, any example that controls the camera and preserves controller state should implement this event
  1393. ExampleComponentRequestBus::Event(m_exampleEntity->GetId(), &ExampleComponentRequestBus::Events::ResetCamera);
  1394. }
  1395. void SampleComponentManager::CreateSceneForRHISample()
  1396. {
  1397. // Create and register the rhi scene with only feature processors required for AtomShimRenderer (only for AtomSampleViewerLauncher)
  1398. RPI::SceneDescriptor sceneDesc;
  1399. sceneDesc.m_nameId = AZ::Name("RHI");
  1400. sceneDesc.m_featureProcessorNames.push_back("AZ::Render::AuxGeomFeatureProcessor");
  1401. m_rhiScene = RPI::Scene::CreateScene(sceneDesc);
  1402. m_rhiScene->Activate();
  1403. auto* xrSystem = AZ::RHI::RHISystemInterface::Get()->GetXRSystem();
  1404. const bool createDefaultRenderPipeline = !xrSystem || xrSystem->IsDefaultRenderPipelineNeeded();
  1405. if (createDefaultRenderPipeline)
  1406. {
  1407. RPI::RenderPipelineDescriptor pipelineDesc;
  1408. pipelineDesc.m_name = "RHISamplePipeline";
  1409. pipelineDesc.m_rootPassTemplate = "RHISamplePipelineTemplate";
  1410. // Add view to pipeline since there are few RHI samples are using ViewSrg
  1411. pipelineDesc.m_mainViewTagName = "MainCamera";
  1412. m_renderPipeline = RPI::RenderPipeline::CreateRenderPipelineForWindow(pipelineDesc, *m_windowContext.get());
  1413. m_rhiScene->AddRenderPipeline(m_renderPipeline);
  1414. m_renderPipeline->SetDefaultViewFromEntity(m_cameraEntity->GetId());
  1415. // Get RHISamplePass
  1416. AZ::RPI::PassFilter passFilter = AZ::RPI::PassFilter::CreateWithPassName(AZ::Name("RHISamplePass"), m_renderPipeline.get());
  1417. m_rhiSamplePasses.push_back(azrtti_cast<RHISamplePass*>(AZ::RPI::PassSystemInterface::Get()->FindFirstPass(passFilter)));
  1418. // Enable or disable default pipeline on host while there is an xr system.
  1419. if (xrSystem)
  1420. {
  1421. EnableRenderPipeline(xrSystem->IsDefaultRenderPipelineEnabledOnHost());
  1422. }
  1423. }
  1424. if (xrSystem)
  1425. {
  1426. RPI::RenderPipelineDescriptor xrPipelineDesc;
  1427. xrPipelineDesc.m_mainViewTagName = "MainCamera";
  1428. // Build the pipeline for left eye
  1429. xrPipelineDesc.m_name = "RHISamplePipelineXRLeft";
  1430. xrPipelineDesc.m_rootPassTemplate = "RHISamplePipelineXRLeftTemplate";
  1431. RPI::RenderPipelinePtr renderPipelineLeft = RPI::RenderPipeline::CreateRenderPipelineForWindow(xrPipelineDesc, *m_windowContext.get(), AZ::RPI::ViewType::XrLeft);
  1432. // Build the pipeline for right eye
  1433. xrPipelineDesc.m_name = "RHISamplePipelineXRRight";
  1434. xrPipelineDesc.m_rootPassTemplate = "RHISamplePipelineXRRightTemplate";
  1435. RPI::RenderPipelinePtr renderPipelineRight = RPI::RenderPipeline::CreateRenderPipelineForWindow(xrPipelineDesc, *m_windowContext.get(), AZ::RPI::ViewType::XrRight);
  1436. //Add both the pipelines to the scene
  1437. m_rhiScene->AddRenderPipeline(renderPipelineLeft);
  1438. m_rhiScene->AddRenderPipeline(renderPipelineRight);
  1439. renderPipelineLeft->SetDefaultViewFromEntity(m_cameraEntity->GetId());
  1440. renderPipelineRight->SetDefaultViewFromEntity(m_cameraEntity->GetId());
  1441. // Set the correct view index for the RHI sample passes
  1442. AZ::RPI::PassFilter rhiSamplePassFilterLeft = AZ::RPI::PassFilter::CreateWithPassName(AZ::Name("RHISamplePass"), renderPipelineLeft.get());
  1443. AZ::RPI::Ptr<RHISamplePass> rhiSamplePassLeft = azrtti_cast<RHISamplePass*>(AZ::RPI::PassSystemInterface::Get()->FindFirstPass(rhiSamplePassFilterLeft));
  1444. rhiSamplePassLeft->SetViewIndex(0);
  1445. m_rhiSamplePasses.push_back(rhiSamplePassLeft);
  1446. AZ::RPI::PassFilter rhiSamplePassFilterRight = AZ::RPI::PassFilter::CreateWithPassName(AZ::Name("RHISamplePass"), renderPipelineRight.get());
  1447. AZ::RPI::Ptr<RHISamplePass> rhiSamplePassRight = azrtti_cast<RHISamplePass*>(AZ::RPI::PassSystemInterface::Get()->FindFirstPass(rhiSamplePassFilterRight));
  1448. rhiSamplePassRight->SetViewIndex(1);
  1449. m_rhiSamplePasses.push_back(rhiSamplePassRight);
  1450. //Cache the pipelines in case we want to enable/disable them
  1451. m_xrPipelines.push_back(renderPipelineLeft);
  1452. m_xrPipelines.push_back(renderPipelineRight);
  1453. //Disable XR pipelines by default
  1454. EnableXrPipelines(false);
  1455. }
  1456. // Register the RHi scene
  1457. RPI::RPISystemInterface::Get()->RegisterScene(m_rhiScene);
  1458. // Setup imGui since a new render pipeline with imgui pass was created
  1459. SetupImGuiContext();
  1460. }
  1461. void SampleComponentManager::ReleaseRHIScene()
  1462. {
  1463. if (m_rhiScene)
  1464. {
  1465. m_rhiSamplePasses.clear();
  1466. m_xrPipelines.clear();
  1467. m_renderPipeline = nullptr;
  1468. RPI::RPISystemInterface::Get()->UnregisterScene(m_rhiScene);
  1469. m_rhiScene = nullptr;
  1470. }
  1471. }
  1472. void SampleComponentManager::SwitchSceneForRHISample()
  1473. {
  1474. ReleaseRPIScene();
  1475. ReleaseRHIScene();
  1476. CreateSceneForRHISample();
  1477. }
  1478. void SampleComponentManager::CreateSceneForRPISample()
  1479. {
  1480. // set pipeline MSAA samples
  1481. AZ_Assert(IsValidNumMSAASamples(m_numMsaaSamples), "Invalid MSAA sample setting");
  1482. const bool isNonMsaaPipeline = (m_numMsaaSamples == 1);
  1483. const char* supervariantName = isNonMsaaPipeline ? AZ::RPI::NoMsaaSupervariantName : "";
  1484. AZ::RPI::ShaderSystemInterface::Get()->SetSupervariantName(AZ::Name(supervariantName));
  1485. // Create and register a scene with all available feature processors
  1486. RPI::SceneDescriptor sceneDesc;
  1487. sceneDesc.m_nameId = AZ::Name("RPI");
  1488. m_rpiScene = RPI::Scene::CreateScene(sceneDesc);
  1489. m_rpiScene->EnableAllFeatureProcessors();
  1490. // Bind m_rpiScene to the GameEntityContext's AzFramework::Scene so the RPI Scene can be found by the entity context
  1491. auto sceneSystem = AzFramework::SceneSystemInterface::Get();
  1492. AZ_Assert(sceneSystem, "SampleComponentManager requires an implementation of the scene system.");
  1493. AZStd::shared_ptr<AzFramework::Scene> mainScene = sceneSystem->GetScene(AzFramework::Scene::MainSceneName);
  1494. AZ_Assert(mainScene, "Main scene missing during system component initialization"); // This should never happen unless scene creation has changed.
  1495. // Add RPI::Scene as a sub system for the default AzFramework Scene
  1496. [[maybe_unused]] bool result = mainScene->SetSubsystem(m_rpiScene);
  1497. AZ_Assert(result, "SampleComponentManager failed to register the RPI scene with the general scene.");
  1498. m_rpiScene->Activate();
  1499. // Register scene to RPI system so it will be processed/rendered per tick
  1500. RPI::RPISystemInterface::Get()->RegisterScene(m_rpiScene);
  1501. auto* xrSystem = AZ::RHI::RHISystemInterface::Get()->GetXRSystem();
  1502. const bool createDefaultRenderPipeline = !xrSystem || xrSystem->IsDefaultRenderPipelineNeeded();
  1503. if (createDefaultRenderPipeline)
  1504. {
  1505. // Create MainPipeline as its render pipeline
  1506. RPI::RenderPipelineDescriptor pipelineDesc;
  1507. pipelineDesc.m_name = "RPISamplePipeline";
  1508. pipelineDesc.m_rootPassTemplate = GetRootPassTemplateName();
  1509. pipelineDesc.m_materialPipelineTag = GetMaterialPipelineName();
  1510. pipelineDesc.m_mainViewTagName = "MainCamera";
  1511. pipelineDesc.m_allowModification = true;
  1512. pipelineDesc.m_renderSettings.m_multisampleState.m_samples = static_cast<uint16_t>(m_numMsaaSamples);
  1513. m_renderPipeline = RPI::RenderPipeline::CreateRenderPipelineForWindow(pipelineDesc, *m_windowContext.get());
  1514. m_rpiScene->AddRenderPipeline(m_renderPipeline);
  1515. m_renderPipeline->SetDefaultViewFromEntity(m_cameraEntity->GetId());
  1516. // Enable or disable default pipeline on host while there is an xr system.
  1517. if (xrSystem)
  1518. {
  1519. EnableRenderPipeline(xrSystem->IsDefaultRenderPipelineEnabledOnHost());
  1520. }
  1521. }
  1522. if (xrSystem)
  1523. {
  1524. RPI::RenderPipelineDescriptor xrPipelineDesc;
  1525. xrPipelineDesc.m_mainViewTagName = "MainCamera";
  1526. xrPipelineDesc.m_renderSettings.m_multisampleState.m_samples = static_cast<uint16_t>(m_numMsaaSamples);
  1527. // Build the pipeline for left eye
  1528. xrPipelineDesc.m_name = "RPISamplePipelineXRLeft";
  1529. xrPipelineDesc.m_materialPipelineTag = "MultiViewPipeline";
  1530. xrPipelineDesc.m_rootPassTemplate = "MultiViewPipelineTemplate";
  1531. RPI::RenderPipelinePtr renderPipelineLeft = RPI::RenderPipeline::CreateRenderPipelineForWindow(xrPipelineDesc, *m_windowContext.get(), AZ::RPI::ViewType::XrLeft);
  1532. // Build the pipeline for right eye
  1533. xrPipelineDesc.m_name = "RHISamplePipelineXRRight";
  1534. xrPipelineDesc.m_materialPipelineTag = "MultiViewPipeline";
  1535. xrPipelineDesc.m_rootPassTemplate = "MultiViewPipelineTemplate";
  1536. RPI::RenderPipelinePtr renderPipelineRight = RPI::RenderPipeline::CreateRenderPipelineForWindow(xrPipelineDesc, *m_windowContext.get(), AZ::RPI::ViewType::XrRight);
  1537. //Add both the pipelines to the scene
  1538. m_rpiScene->AddRenderPipeline(renderPipelineLeft);
  1539. m_rpiScene->AddRenderPipeline(renderPipelineRight);
  1540. renderPipelineLeft->SetDefaultStereoscopicViewFromEntity(m_cameraEntity->GetId(), RPI::ViewType::XrLeft); //Left eye
  1541. renderPipelineRight->SetDefaultStereoscopicViewFromEntity(m_cameraEntity->GetId(), RPI::ViewType::XrRight); //Right eye
  1542. //Cache the pipelines in case we want to enable/disable them
  1543. m_xrPipelines.push_back(renderPipelineLeft);
  1544. m_xrPipelines.push_back(renderPipelineRight);
  1545. // Disable XR pipelines by default
  1546. EnableXrPipelines(false);
  1547. }
  1548. // As part of our initialization we need to create the BRDF texture generation pipeline
  1549. AZ::RPI::RenderPipelineDescriptor brdfPipelineDesc;
  1550. brdfPipelineDesc.m_mainViewTagName = "MainCamera";
  1551. brdfPipelineDesc.m_name = "BRDFTexturePipeline";
  1552. brdfPipelineDesc.m_rootPassTemplate = "BRDFTexturePipeline";
  1553. brdfPipelineDesc.m_executeOnce = true;
  1554. RPI::RenderPipelinePtr brdfTexturePipeline = AZ::RPI::RenderPipeline::CreateRenderPipeline(brdfPipelineDesc);
  1555. m_rpiScene->AddRenderPipeline(brdfTexturePipeline);
  1556. // Save a reference to the generated BRDF texture so it doesn't get deleted if all the passes referring to it get deleted and it's ref count goes to zero
  1557. if (!m_brdfTexture)
  1558. {
  1559. const AZStd::shared_ptr<const RPI::PassTemplate> brdfTextureTemplate = RPI::PassSystemInterface::Get()->GetPassTemplate(Name("BRDFTextureTemplate"));
  1560. Data::Asset<RPI::AttachmentImageAsset> brdfImageAsset = RPI::AssetUtils::LoadAssetById<RPI::AttachmentImageAsset>(
  1561. brdfTextureTemplate->m_imageAttachments[0].m_assetRef.m_assetId, RPI::AssetUtils::TraceLevel::Error);
  1562. if (brdfImageAsset.IsReady())
  1563. {
  1564. m_brdfTexture = RPI::AttachmentImage::FindOrCreate(brdfImageAsset);
  1565. }
  1566. }
  1567. // Setup imGui since a new render pipeline with imgui pass was created
  1568. SetupImGuiContext();
  1569. }
  1570. void SampleComponentManager::ReleaseRPIScene()
  1571. {
  1572. if (m_rpiScene)
  1573. {
  1574. m_xrPipelines.clear();
  1575. m_renderPipeline = nullptr;
  1576. RPI::RPISystemInterface::Get()->UnregisterScene(m_rpiScene);
  1577. auto sceneSystem = AzFramework::SceneSystemInterface::Get();
  1578. AZ_Assert(sceneSystem, "Scene system was destroyed before SampleComponentManager was able to unregister the RPI scene.");
  1579. AZStd::shared_ptr<AzFramework::Scene> scene = sceneSystem->GetScene(AzFramework::Scene::MainSceneName);
  1580. AZ_Assert(scene, "The main scene wasn't found in the scene system.");
  1581. [[maybe_unused]] bool result = scene->UnsetSubsystem(m_rpiScene);
  1582. AZ_Assert(result, "SampleComponentManager failed to unregister its RPI scene from the general scene.");
  1583. m_rpiScene = nullptr;
  1584. }
  1585. }
  1586. void SampleComponentManager::SwitchSceneForRPISample()
  1587. {
  1588. ReleaseRHIScene();
  1589. ReleaseRPIScene();
  1590. CreateSceneForRPISample();
  1591. }
  1592. // AzFramework::AssetCatalogEventBus::Handler overrides ...
  1593. void SampleComponentManager::OnCatalogLoaded([[maybe_unused]] const char* catalogFile)
  1594. {
  1595. AZ::TickBus::QueueFunction([&]()
  1596. {
  1597. ActivateInternal();
  1598. });
  1599. }
  1600. void SampleComponentManager::SetRHISamplePass(BasicRHIComponent* sampleComponent)
  1601. {
  1602. for (AZ::RPI::Ptr<RHISamplePass> samplePass : m_rhiSamplePasses)
  1603. {
  1604. samplePass->SetRHISample(sampleComponent);
  1605. }
  1606. }
  1607. } // namespace AtomSampleViewer