Scene.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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 <Atom/RPI.Public/Culling.h>
  9. #include <Atom/RPI.Public/DynamicDraw/DynamicDrawSystem.h>
  10. #include <Atom/RPI.Public/FeatureProcessorFactory.h>
  11. #include <Atom/RPI.Public/FeatureProcessor.h>
  12. #include <Atom/RPI.Public/Pass/FullscreenTrianglePass.h>
  13. #include <Atom/RPI.Public/Pass/RasterPass.h>
  14. #include <Atom/RPI.Public/RenderPipeline.h>
  15. #include <Atom/RHI/RHISystemInterface.h>
  16. #include <Atom/RPI.Public/Scene.h>
  17. #include <Atom/RPI.Public/Shader/ShaderResourceGroup.h>
  18. #include <Atom/RPI.Public/View.h>
  19. #include <AzCore/Debug/Profiler.h>
  20. #include <AzCore/Jobs/JobFunction.h>
  21. #include <AzCore/Jobs/JobEmpty.h>
  22. #include <AzCore/Task/TaskGraph.h>
  23. #include <AzFramework/Entity/EntityContext.h>
  24. #include <AzFramework/Visibility/IVisibilitySystem.h>
  25. namespace AZ
  26. {
  27. namespace RPI
  28. {
  29. ScenePtr Scene::CreateScene(const SceneDescriptor& sceneDescriptor)
  30. {
  31. Scene* scene = aznew Scene();
  32. scene->m_name = sceneDescriptor.m_nameId;
  33. AZ::Name visSceneName(AZStd::string::format("RenderCullScene[%s]", scene->m_name.GetCStr()));
  34. scene->m_visibilityScene = AZ::Interface<AzFramework::IVisibilitySystem>::Get()->CreateVisibilityScene(visSceneName);
  35. for (const auto& fpId : sceneDescriptor.m_featureProcessorNames)
  36. {
  37. scene->EnableFeatureProcessor(FeatureProcessorId{ fpId });
  38. }
  39. auto sceneSrgLayout = RPISystemInterface::Get()->GetSceneSrgLayout();
  40. if (sceneSrgLayout)
  41. {
  42. auto shaderAsset = RPISystemInterface::Get()->GetCommonShaderAssetForSrgs();
  43. scene->m_srg = ShaderResourceGroup::Create(shaderAsset, sceneSrgLayout->GetName());
  44. }
  45. return ScenePtr(scene);
  46. }
  47. ScenePtr Scene::CreateSceneFromAsset(Data::Asset<AnyAsset> sceneAsset)
  48. {
  49. const SceneDescriptor* sceneDescriptor = GetDataFromAnyAsset<SceneDescriptor>(sceneAsset);
  50. if (sceneDescriptor == nullptr)
  51. {
  52. return nullptr;
  53. }
  54. ScenePtr scene = Scene::CreateScene(*sceneDescriptor);
  55. if (scene == nullptr)
  56. {
  57. AZ_Error("RPISystem", false, "Failed to create scene from asset %s", sceneAsset.GetHint().c_str());
  58. return nullptr;
  59. }
  60. return scene;
  61. }
  62. Scene* Scene::GetSceneForEntityContextId(AzFramework::EntityContextId entityContextId)
  63. {
  64. // Find the scene for this entity context.
  65. AZStd::shared_ptr<AzFramework::Scene> scene = AzFramework::EntityContext::FindContainingScene(entityContextId);
  66. if (scene)
  67. {
  68. // Get the RPI::Scene subsystem from the AZFramework Scene.
  69. RPI::ScenePtr* scenePtr = scene->FindSubsystem<RPI::ScenePtr>();
  70. if (scenePtr)
  71. {
  72. return scenePtr->get();
  73. }
  74. }
  75. return nullptr;
  76. }
  77. Scene* Scene::GetSceneForEntityId(AZ::EntityId entityId)
  78. {
  79. // Find the entity context for the entity ID.
  80. AzFramework::EntityContextId entityContextId = AzFramework::EntityContextId::CreateNull();
  81. AzFramework::EntityIdContextQueryBus::EventResult(entityContextId, entityId, &AzFramework::EntityIdContextQueryBus::Events::GetOwningContextId);
  82. if (!entityContextId.IsNull())
  83. {
  84. return GetSceneForEntityContextId(entityContextId);
  85. }
  86. return nullptr;
  87. }
  88. Scene::Scene()
  89. {
  90. m_id = AZ::Uuid::CreateRandom();
  91. m_cullingScene = aznew CullingScene();
  92. SceneRequestBus::Handler::BusConnect(m_id);
  93. m_drawFilterTagRegistry = RHI::DrawFilterTagRegistry::Create();
  94. }
  95. Scene::~Scene()
  96. {
  97. if (m_taskGraphActive)
  98. {
  99. WaitAndCleanTGEvent();
  100. }
  101. else
  102. {
  103. WaitAndCleanCompletionJob(m_simulationCompletion);
  104. }
  105. SceneRequestBus::Handler::BusDisconnect();
  106. // Remove all the render pipelines.
  107. for (auto it = m_pipelines.begin(); it != m_pipelines.end(); ++it)
  108. {
  109. RenderPipelinePtr pipelineToRemove = (*it);
  110. pipelineToRemove->OnRemovedFromScene(this);
  111. }
  112. m_pipelines.clear();
  113. Deactivate();
  114. AZ::Interface<AzFramework::IVisibilitySystem>::Get()->DestroyVisibilityScene(m_visibilityScene);
  115. delete m_cullingScene;
  116. }
  117. void Scene::Activate()
  118. {
  119. AZ_Assert(!m_activated, "Already activated");
  120. m_activated = true;
  121. m_cullingScene->Activate(this);
  122. // We have to tick the PassSystem in order for all the pass attachments to get created.
  123. // This has to be done before FeatureProcessors are activated, because they may try to
  124. // create PipelineState objects (PSOs) which would require data from attachments in the
  125. // the pass tree.
  126. AZ::RPI::PassSystemInterface::Get()->ProcessQueuedChanges();
  127. for (auto& fp : m_featureProcessors)
  128. {
  129. fp->Activate();
  130. }
  131. m_dynamicDrawSystem = static_cast<DynamicDrawSystem*>(RPI::DynamicDrawInterface::Get());
  132. }
  133. void Scene::Deactivate()
  134. {
  135. if (!m_activated)
  136. {
  137. return;
  138. }
  139. for (auto& fp : m_featureProcessors)
  140. {
  141. fp->Deactivate();
  142. }
  143. m_cullingScene->Deactivate();
  144. m_activated = false;
  145. m_pipelineStatesLookup.clear();
  146. m_dynamicDrawSystem = nullptr;
  147. }
  148. void Scene::CheckRecreateRenderPipeline()
  149. {
  150. // need to recreate render pipeline passes if the pipeline can be modified by FPs
  151. for (auto& renderPipeline : m_pipelines)
  152. {
  153. if (renderPipeline->m_descriptor.m_allowModification)
  154. {
  155. renderPipeline->MarkPipelinePassChanges(PipelinePassChanges::PipelineChangedByFeatureProcessor);
  156. }
  157. }
  158. }
  159. FeatureProcessor* Scene::EnableFeatureProcessor(const FeatureProcessorId& featureProcessorId)
  160. {
  161. FeatureProcessor* foundFeatureProcessor = GetFeatureProcessor(featureProcessorId);
  162. if (foundFeatureProcessor)
  163. {
  164. AZ_Warning("Scene", false, "FeatureProcessor '%s' is already enabled for this scene. Will not re-enable.", featureProcessorId.GetCStr());
  165. return foundFeatureProcessor;
  166. }
  167. // Check to make sure there aren't multiple different implementations of the same interface enabled for the scene
  168. // otherwise it would be ambiguous which feature processor is returned when GetFeatureProcessor is called with the interface.
  169. AZ::TypeId interfaceTypeId = FeatureProcessorFactory::Get()->GetFeatureProcessorInterfaceTypeId(featureProcessorId);
  170. if (!interfaceTypeId.IsNull())
  171. {
  172. foundFeatureProcessor = GetFeatureProcessor(interfaceTypeId);
  173. if (foundFeatureProcessor)
  174. {
  175. AZ_Error("Scene", false, "FeatureProcessor '%s' is already enabled for this scene, which implements the same interface as %s. You cannot have more than one implementation of the same feature processor interface in a scene.", foundFeatureProcessor->RTTI_GetTypeName(), featureProcessorId.GetCStr());
  176. return foundFeatureProcessor;
  177. }
  178. }
  179. // The feature processor was not found, so create it
  180. FeatureProcessorPtr createdFeatureProcessor = FeatureProcessorFactory::Get()->CreateFeatureProcessor(featureProcessorId);
  181. if (!createdFeatureProcessor)
  182. {
  183. AZ_Error("Scene", false, "FeatureProcessor '%s' could not be created. Check to make sure it has been registered with the FeatureProcessorFactory.", featureProcessorId.GetCStr());
  184. return nullptr;
  185. }
  186. foundFeatureProcessor = createdFeatureProcessor.get();
  187. AddFeatureProcessor(AZStd::move(createdFeatureProcessor));
  188. return foundFeatureProcessor;
  189. }
  190. void Scene::AddFeatureProcessor(FeatureProcessorPtr fp)
  191. {
  192. fp->m_parentScene = this;
  193. // If the Scene is not active then we should not activate the new FeatureProcessor either.
  194. // In this case, the FeatureProcessor will be activated when Scene::Activate() is called.
  195. if (m_activated)
  196. {
  197. fp->Activate();
  198. CheckRecreateRenderPipeline();
  199. }
  200. m_featureProcessors.emplace_back(AZStd::move(fp));
  201. }
  202. void Scene::EnableAllFeatureProcessors()
  203. {
  204. FeatureProcessorFactory::Get()->EnableAllForScene(this);
  205. }
  206. void Scene::DisableFeatureProcessor(const FeatureProcessorId& featureProcessorId)
  207. {
  208. auto foundFeatureProcessor = AZStd::find_if(
  209. AZStd::begin(m_featureProcessors),
  210. AZStd::end(m_featureProcessors),
  211. [featureProcessorId](const FeatureProcessorPtr& fp) { return FeatureProcessorId{ fp->RTTI_GetTypeName() } == featureProcessorId; });
  212. if (foundFeatureProcessor != AZStd::end(m_featureProcessors))
  213. {
  214. // If the Scene is not active then the removed FeatureProcessor is not active either, so no need to deactivate it.
  215. if (m_activated)
  216. {
  217. (*foundFeatureProcessor)->Deactivate();
  218. CheckRecreateRenderPipeline();
  219. }
  220. m_featureProcessors.erase(foundFeatureProcessor);
  221. }
  222. else
  223. {
  224. AZ_Warning("Scene", false, "FeatureProcessor '%s' is already disabled for this scene. Will not re-disable.", featureProcessorId.GetCStr());
  225. }
  226. }
  227. void Scene::DisableAllFeatureProcessors()
  228. {
  229. for (auto& fp : m_featureProcessors)
  230. {
  231. fp->Deactivate();
  232. }
  233. m_featureProcessors.clear();
  234. }
  235. void Scene::VisitFeatureProcessor(FeatureProcessorVisitCallback callback) const
  236. {
  237. auto VisitInterface = [&callback](const FeatureProcessorPtr& featureProcessor)
  238. {
  239. return featureProcessor != nullptr ? callback(*featureProcessor) : true;
  240. };
  241. AZStd::ranges::all_of(m_featureProcessors, VisitInterface);
  242. }
  243. FeatureProcessor* Scene::GetFeatureProcessor(const FeatureProcessorId& featureProcessorId) const
  244. {
  245. FeatureProcessor* foundFp = nullptr;
  246. VisitFeatureProcessor(
  247. [featureProcessorId, &foundFp](FeatureProcessor& fp)
  248. {
  249. if (FeatureProcessorId(fp.RTTI_GetTypeName()) == featureProcessorId)
  250. {
  251. foundFp = &fp;
  252. return false;
  253. }
  254. return true;
  255. }
  256. );
  257. return foundFp;
  258. }
  259. FeatureProcessor* Scene::GetFeatureProcessor(const TypeId& featureProcessorTypeId) const
  260. {
  261. FeatureProcessor* foundFp = nullptr;
  262. VisitFeatureProcessor(
  263. [featureProcessorTypeId, &foundFp](FeatureProcessor& fp)
  264. {
  265. if (fp.RTTI_IsTypeOf(featureProcessorTypeId))
  266. {
  267. foundFp = &fp;
  268. return false;
  269. }
  270. return true;
  271. }
  272. );
  273. return foundFp;
  274. }
  275. void Scene::TryApplyRenderPipelineChanges(RenderPipeline* pipeline)
  276. {
  277. // return directly if the pipeline doesn't allow modification or it was already modified by scene
  278. if (!pipeline->m_descriptor.m_allowModification || pipeline->m_wasModifiedByScene)
  279. {
  280. return;
  281. }
  282. pipeline->m_wasModifiedByScene = true;
  283. for (auto& fp : m_featureProcessors)
  284. {
  285. fp->AddRenderPasses(pipeline);
  286. fp->ApplyRenderPipelineChange(pipeline);
  287. }
  288. pipeline->ProcessQueuedPassChanges();
  289. }
  290. void Scene::AddRenderPipeline(RenderPipelinePtr pipeline)
  291. {
  292. if (pipeline->m_scene != nullptr)
  293. {
  294. AZ_Assert(false, "Pipeline was added to another scene");
  295. return;
  296. }
  297. auto pipelineId = pipeline->GetId();
  298. if (GetRenderPipeline(pipelineId))
  299. {
  300. AZ_Assert(false, "Pipeline with same name id is already added to this scene. Please set the pipeline with a different id");
  301. return;
  302. }
  303. pipeline->SetDrawFilterTags(m_drawFilterTagRegistry.get());
  304. m_pipelines.push_back(pipeline);
  305. // Set this pipeline as default if the default pipeline was empty. This pipeline should be the first pipeline be added to the scene
  306. if (m_defaultPipeline == nullptr)
  307. {
  308. m_defaultPipeline = pipeline;
  309. }
  310. pipeline->OnAddedToScene(this);
  311. pipeline->ProcessQueuedPassChanges();
  312. TryApplyRenderPipelineChanges(pipeline.get());
  313. pipeline->ProcessQueuedPassChanges();
  314. pipeline->BuildPipelineViews();
  315. // Force to update the lookup table since adding render pipeline would effect any pipeline states created before pass system tick
  316. RebuildPipelineStatesLookup();
  317. SceneNotificationBus::Event(m_id, &SceneNotification::OnRenderPipelineAdded, pipeline);
  318. SceneNotificationBus::Event(m_id, &SceneNotification::OnRenderPipelineChanged, pipeline.get(), SceneNotification::RenderPipelineChangeType::Added);
  319. }
  320. void Scene::RemoveRenderPipeline(const RenderPipelineId& pipelineId)
  321. {
  322. [[maybe_unused]] bool removed = false;
  323. for (auto it = m_pipelines.begin(); it != m_pipelines.end(); ++it)
  324. {
  325. if (pipelineId == (*it)->GetId())
  326. {
  327. RenderPipelinePtr pipelineToRemove = (*it);
  328. if (m_defaultPipeline == pipelineToRemove)
  329. {
  330. m_defaultPipeline = nullptr;
  331. }
  332. pipelineToRemove->ReleaseDrawFilterTags(m_drawFilterTagRegistry.get());
  333. pipelineToRemove->OnRemovedFromScene(this);
  334. m_pipelines.erase(it);
  335. SceneNotificationBus::Event(m_id, &SceneNotification::OnRenderPipelineRemoved, pipelineToRemove.get());
  336. SceneNotificationBus::Event(
  337. m_id,
  338. &SceneNotification::OnRenderPipelineChanged,
  339. pipelineToRemove.get(),
  340. SceneNotification::RenderPipelineChangeType::Removed);
  341. // If the default pipeline was removed, set to the first one in the list
  342. if (m_defaultPipeline == nullptr && m_pipelines.size() > 0)
  343. {
  344. m_defaultPipeline = m_pipelines[0];
  345. }
  346. RebuildPipelineStatesLookup();
  347. removed = true;
  348. break;
  349. }
  350. }
  351. AZ_Assert(removed, "Pipeline %s is not added to this Scene", pipelineId.GetCStr());
  352. }
  353. RenderPipelinePtr Scene::GetRenderPipeline(const RenderPipelineId& pipelineId) const
  354. {
  355. for (auto& pipeline : m_pipelines)
  356. {
  357. if (pipeline->GetId() == pipelineId)
  358. {
  359. return pipeline;
  360. }
  361. }
  362. return nullptr;
  363. }
  364. void Scene::SimulateTaskGraph()
  365. {
  366. static const AZ::TaskDescriptor simulationTGDesc{"RPI::Scene::Simulate", "Graphics"};
  367. AZ::TaskGraph simulationTG{ "RPI::Scene::Simulate" };
  368. for (FeatureProcessorPtr& fp : m_featureProcessors)
  369. {
  370. FeatureProcessor* featureProcessor = fp.get();
  371. simulationTG.AddTask(
  372. simulationTGDesc,
  373. [this, featureProcessor]()
  374. {
  375. FeatureProcessor::SimulatePacket jobPacket = m_simulatePacket;
  376. jobPacket.m_parentJob = nullptr;
  377. featureProcessor->Simulate(jobPacket);
  378. });
  379. }
  380. simulationTG.Detach();
  381. m_simulationFinishedTGEvent = AZStd::make_unique<TaskGraphEvent>("RPI::Scene::Simulate Wait");
  382. simulationTG.Submit(m_simulationFinishedTGEvent.get());
  383. }
  384. void Scene::SimulateJobs()
  385. {
  386. // Create a new job to track completion.
  387. m_simulationCompletion = aznew AZ::JobCompletion();
  388. for (FeatureProcessorPtr& fp : m_featureProcessors)
  389. {
  390. FeatureProcessor* featureProcessor = fp.get();
  391. const auto jobLambda = [this, featureProcessor](AZ::Job& owner)
  392. {
  393. FeatureProcessor::SimulatePacket jobPacket = m_simulatePacket;
  394. jobPacket.m_parentJob = &owner;
  395. featureProcessor->Simulate(jobPacket);
  396. };
  397. AZ::Job* simulationJob = AZ::CreateJobFunction(AZStd::move(jobLambda), true, nullptr); //auto-deletes
  398. simulationJob->SetDependent(m_simulationCompletion);
  399. simulationJob->Start();
  400. }
  401. //[GFX TODO]: the completion job should start here
  402. }
  403. void Scene::Simulate(RHI::JobPolicy jobPolicy, float simulationTime)
  404. {
  405. AZ_PROFILE_SCOPE(RPI, "Scene: Simulate");
  406. if (!m_activated)
  407. {
  408. return;
  409. }
  410. m_prevSimulationTime = m_simulationTime;
  411. m_simulationTime = simulationTime;
  412. // If previous simulation job wasn't done, wait for it to finish.
  413. if (m_taskGraphActive)
  414. {
  415. WaitAndCleanTGEvent();
  416. }
  417. else
  418. {
  419. WaitAndCleanCompletionJob(m_simulationCompletion);
  420. }
  421. auto taskGraphActiveInterface = AZ::Interface<AZ::TaskGraphActiveInterface>::Get();
  422. m_taskGraphActive = taskGraphActiveInterface && taskGraphActiveInterface->IsTaskGraphActive();
  423. if (jobPolicy == RHI::JobPolicy::Serial)
  424. {
  425. for (auto& fp : m_featureProcessors)
  426. {
  427. fp->Simulate(m_simulatePacket);
  428. }
  429. }
  430. else
  431. {
  432. if (m_taskGraphActive)
  433. {
  434. SimulateTaskGraph();
  435. }
  436. else
  437. {
  438. SimulateJobs();
  439. }
  440. }
  441. }
  442. void Scene::WaitAndCleanTGEvent()
  443. {
  444. AZ_PROFILE_SCOPE(RPI, "Scene: WaitAndCleanTGEvent");
  445. if (m_simulationFinishedTGEvent)
  446. {
  447. m_simulationFinishedTGEvent->Wait();
  448. }
  449. m_simulationFinishedTGEvent = nullptr;
  450. }
  451. void Scene::WaitAndCleanCompletionJob(AZ::JobCompletion*& completionJob)
  452. {
  453. if (completionJob)
  454. {
  455. AZ_PROFILE_SCOPE(RPI, "Scene: WaitAndCleanCompletionJob");
  456. //[GFX TODO]: the completion job should start earlier and wait for completion here
  457. completionJob->StartAndWaitForCompletion();
  458. delete completionJob;
  459. completionJob = nullptr;
  460. }
  461. }
  462. void Scene::ConnectEvent(PrepareSceneSrgEvent::Handler& handler)
  463. {
  464. handler.Connect(m_prepareSrgEvent);
  465. }
  466. void Scene::PrepareSceneSrg()
  467. {
  468. if (m_srg)
  469. {
  470. m_srg->SetConstant(m_timeInputIndex, m_simulationTime);
  471. m_srg->SetConstant(m_prevTimeInputIndex, m_prevSimulationTime);
  472. // signal any handlers to update values for their partial scene srg
  473. m_prepareSrgEvent.Signal(m_srg.get());
  474. m_srg->Compile();
  475. }
  476. }
  477. void Scene::CollectDrawPacketsTaskGraph()
  478. {
  479. AZ_PROFILE_SCOPE(RPI, "CollectDrawPacketsTaskGraph");
  480. AZ::TaskGraphEvent collectDrawPacketsTGEvent{ "CollectDrawPackets Wait" };
  481. static const AZ::TaskDescriptor collectDrawPacketsTGDesc{"RPI_Scene_PrepareRender_CollectDrawPackets", "Graphics"};
  482. AZ::TaskGraph collectDrawPacketsTG{ "CollectDrawPackets" };
  483. // Launch FeatureProcessor::Render() taskgraphs
  484. for (auto& fp : m_featureProcessors)
  485. {
  486. collectDrawPacketsTG.AddTask(
  487. collectDrawPacketsTGDesc,
  488. [this, &fp]()
  489. {
  490. fp->Render(m_renderPacket);
  491. });
  492. }
  493. collectDrawPacketsTG.Submit(&collectDrawPacketsTGEvent);
  494. // Launch CullingSystem::ProcessCullables() jobs (will run concurrently with FeatureProcessor::Render() jobs if m_parallelOctreeTraversal)
  495. const bool parallelOctreeTraversal = m_cullingScene->GetDebugContext().m_parallelOctreeTraversal;
  496. m_cullingScene->BeginCulling(*this, m_renderPacket.m_views);
  497. static const AZ::TaskDescriptor processCullablesDescriptor{"AZ::RPI::Scene::ProcessCullables", "Graphics"};
  498. AZ::TaskGraphEvent processCullablesTGEvent{ "ProcessCullables Wait" };
  499. AZ::TaskGraph processCullablesTG{ "ProcessCullables" };
  500. if (parallelOctreeTraversal)
  501. {
  502. for (ViewPtr& viewPtr : m_renderPacket.m_views)
  503. {
  504. processCullablesTG.AddTask(processCullablesDescriptor, [this, &viewPtr, &processCullablesTGEvent]()
  505. {
  506. AZ::TaskGraph subTaskGraph{ "ProcessCullables Subgraph" };
  507. m_cullingScene->ProcessCullablesTG(*this, *viewPtr, subTaskGraph, processCullablesTGEvent);
  508. if (!subTaskGraph.IsEmpty())
  509. {
  510. subTaskGraph.Detach();
  511. subTaskGraph.Submit(&processCullablesTGEvent);
  512. }
  513. });
  514. }
  515. }
  516. else
  517. {
  518. for (ViewPtr& viewPtr : m_renderPacket.m_views)
  519. {
  520. m_cullingScene->ProcessCullablesTG(*this, *viewPtr, processCullablesTG, processCullablesTGEvent);
  521. }
  522. }
  523. bool processCullablesHasWork = !processCullablesTG.IsEmpty();
  524. if (processCullablesHasWork)
  525. {
  526. processCullablesTG.Submit(&processCullablesTGEvent);
  527. }
  528. collectDrawPacketsTGEvent.Wait();
  529. if (processCullablesHasWork) // skip the wait if there is no work to do
  530. {
  531. processCullablesTGEvent.Wait();
  532. }
  533. }
  534. void Scene::CollectDrawPacketsJobs()
  535. {
  536. AZ_PROFILE_SCOPE(RPI, "CollectDrawPacketsJobs");
  537. AZ::JobCompletion* collectDrawPacketsCompletion = aznew AZ::JobCompletion();
  538. // Launch FeatureProcessor::Render() jobs
  539. for (auto& fp : m_featureProcessors)
  540. {
  541. const auto renderLambda = [this, &fp]()
  542. {
  543. fp->Render(m_renderPacket);
  544. };
  545. AZ::Job* renderJob = AZ::CreateJobFunction(AZStd::move(renderLambda), true, nullptr); //auto-deletes
  546. renderJob->SetDependent(collectDrawPacketsCompletion);
  547. renderJob->Start();
  548. }
  549. // Launch CullingSystem::ProcessCullables() jobs (will run concurrently with FeatureProcessor::Render() jobs)
  550. const bool parallelOctreeTraversal = m_cullingScene->GetDebugContext().m_parallelOctreeTraversal;
  551. m_cullingScene->BeginCulling(*this, m_renderPacket.m_views);
  552. for (ViewPtr& viewPtr : m_renderPacket.m_views)
  553. {
  554. AZ::Job* processCullablesJob = AZ::CreateJobFunction([this, &viewPtr](AZ::Job& thisJob)
  555. {
  556. m_cullingScene->ProcessCullablesJobs(*this, *viewPtr, thisJob); // can't call directly because ProcessCullables needs a parent job
  557. },
  558. true, nullptr); //auto-deletes
  559. if (parallelOctreeTraversal)
  560. {
  561. processCullablesJob->SetDependent(collectDrawPacketsCompletion);
  562. processCullablesJob->Start();
  563. }
  564. else
  565. {
  566. processCullablesJob->StartAndWaitForCompletion();
  567. }
  568. }
  569. WaitAndCleanCompletionJob(collectDrawPacketsCompletion);
  570. }
  571. void Scene::FinalizeDrawListsTaskGraph()
  572. {
  573. AZ::TaskGraphEvent finalizeDrawListsTGEvent{ "FinalizeDrawLists Wait" };
  574. static const AZ::TaskDescriptor finalizeDrawListsTGDesc{"RPI_Scene_PrepareRender_FinalizeDrawLists", "Graphics"};
  575. AZ::TaskGraph finalizeDrawListsTG{ "FinalizeDrawLists" };
  576. for (auto& view : m_renderPacket.m_views)
  577. {
  578. finalizeDrawListsTG.AddTask(
  579. finalizeDrawListsTGDesc,
  580. [view, &finalizeDrawListsTGEvent]()
  581. {
  582. view->FinalizeDrawListsTG(finalizeDrawListsTGEvent);
  583. });
  584. }
  585. finalizeDrawListsTG.Submit(&finalizeDrawListsTGEvent);
  586. finalizeDrawListsTGEvent.Wait();
  587. }
  588. void Scene::FinalizeDrawListsJobs()
  589. {
  590. AZ::JobCompletion* finalizeDrawListsCompletion = aznew AZ::JobCompletion();
  591. for (auto& view : m_renderPacket.m_views)
  592. {
  593. const auto finalizeDrawListsLambda = [view](AZ::Job& job)
  594. {
  595. view->FinalizeDrawListsJob(&job);
  596. };
  597. AZ::Job* finalizeDrawListsJob = AZ::CreateJobFunction(AZStd::move(finalizeDrawListsLambda), true, nullptr); //auto-deletes
  598. finalizeDrawListsJob->SetDependent(finalizeDrawListsCompletion);
  599. finalizeDrawListsJob->Start();
  600. }
  601. WaitAndCleanCompletionJob(finalizeDrawListsCompletion);
  602. }
  603. void Scene::PrepareRender(RHI::JobPolicy jobPolicy, float simulationTime)
  604. {
  605. AZ_PROFILE_SCOPE(RPI, "Scene: PrepareRender");
  606. if (!m_activated)
  607. {
  608. return;
  609. }
  610. if (m_taskGraphActive)
  611. {
  612. WaitAndCleanTGEvent();
  613. }
  614. else
  615. {
  616. WaitAndCleanCompletionJob(m_simulationCompletion);
  617. }
  618. {
  619. AZ_PROFILE_SCOPE(RPI, "Scene: OnBeginPrepareRender");
  620. SceneNotificationBus::Event(GetId(), &SceneNotification::OnBeginPrepareRender);
  621. }
  622. // Get active pipelines which need to be rendered and notify them frame started
  623. AZStd::vector<RenderPipelinePtr> activePipelines;
  624. {
  625. AZ_PROFILE_SCOPE(RPI, "Scene: OnStartFrame");
  626. for (auto& pipeline : m_pipelines)
  627. {
  628. if (pipeline->NeedsRender())
  629. {
  630. activePipelines.push_back(pipeline);
  631. pipeline->OnStartFrame(simulationTime);
  632. }
  633. }
  634. }
  635. m_numActiveRenderPipelines = aznumeric_cast<uint16_t>(activePipelines.size());
  636. // the pipeline states might have changed during the OnStartFrame, rebuild the lookup
  637. if (m_pipelineStatesLookupNeedsRebuild)
  638. {
  639. RebuildPipelineStatesLookup();
  640. }
  641. // Return if there is no active render pipeline
  642. if (activePipelines.empty())
  643. {
  644. SceneNotificationBus::Event(GetId(), &SceneNotification::OnEndPrepareRender);
  645. return;
  646. }
  647. // Init render packet
  648. m_renderPacket.m_views.clear();
  649. AZ_Assert(m_cullingScene, "m_cullingScene is not initialized");
  650. m_renderPacket.m_cullingScene = m_cullingScene;
  651. m_renderPacket.m_jobPolicy = jobPolicy;
  652. {
  653. AZ_PROFILE_SCOPE(RPI, "Setup Views");
  654. // Collect persistent views from all pipelines to be rendered
  655. AZStd::map<ViewPtr, RHI::DrawListMask> persistentViews;
  656. for (const auto& pipeline : activePipelines)
  657. {
  658. pipeline->CollectPersistentViews(persistentViews);
  659. }
  660. // Set combined draw list mask for each persistent view and get a global draw list mask for late use
  661. for (auto& viewInfo : persistentViews)
  662. {
  663. viewInfo.first->SetDrawListMask(viewInfo.second);
  664. m_renderPacket.m_views.push_back(viewInfo.first);
  665. m_renderPacket.m_drawListMask |= viewInfo.second;
  666. }
  667. // Collect transient views from each feature processor
  668. FeatureProcessor::PrepareViewsPacket prepareViewPacket;
  669. prepareViewPacket.m_persistentViews = persistentViews;
  670. AZStd::vector<AZStd::pair<PipelineViewTag, ViewPtr>> transientViews;
  671. for (auto& fp : m_featureProcessors)
  672. {
  673. fp->PrepareViews(prepareViewPacket, transientViews);
  674. }
  675. // Save transient views to RenderPacket and add them to each pipeline.
  676. for (auto view : transientViews)
  677. {
  678. m_renderPacket.m_views.push_back(view.second);
  679. m_renderPacket.m_drawListMask |= view.second->GetDrawListMask();
  680. for (auto& itr : activePipelines)
  681. {
  682. itr->AddTransientView(view.first, view.second);
  683. }
  684. }
  685. }
  686. {
  687. if (m_taskGraphActive)
  688. {
  689. CollectDrawPacketsTaskGraph();
  690. }
  691. else
  692. {
  693. CollectDrawPacketsJobs();
  694. }
  695. m_cullingScene->EndCulling(*this, m_renderPacket.m_views);
  696. // Add dynamic draw data for all the views
  697. if (m_dynamicDrawSystem)
  698. {
  699. m_dynamicDrawSystem->SubmitDrawData(this, m_renderPacket.m_views);
  700. }
  701. }
  702. {
  703. AZ_PROFILE_SCOPE(RPI, "Scene FinalizeVisibleObjectLists");
  704. for (auto& view : m_renderPacket.m_views)
  705. {
  706. view->FinalizeVisibleObjectList();
  707. }
  708. }
  709. {
  710. AZ_PROFILE_SCOPE(RPI, "Scene OnEndCulling");
  711. for (auto& fp : m_featureProcessors)
  712. {
  713. fp->OnEndCulling(m_renderPacket);
  714. }
  715. }
  716. {
  717. AZ_PROFILE_SCOPE(RPI, "FinalizeDrawLists");
  718. if (jobPolicy == RHI::JobPolicy::Serial ||
  719. m_renderPacket.m_views.size() <= 1) // FinalizeDrawListsX both immediately wait for the job to complete, skip job if only 1 job would be generated
  720. {
  721. for (auto& view : m_renderPacket.m_views)
  722. {
  723. view->FinalizeDrawListsJob(nullptr);
  724. }
  725. }
  726. else
  727. {
  728. if (m_taskGraphActive)
  729. {
  730. FinalizeDrawListsTaskGraph();
  731. }
  732. else
  733. {
  734. FinalizeDrawListsJobs();
  735. }
  736. }
  737. }
  738. {
  739. AZ_PROFILE_SCOPE(RPI, "Scene OnEndPrepareRender");
  740. SceneNotificationBus::Event(GetId(), &SceneNotification::OnEndPrepareRender);
  741. }
  742. }
  743. void Scene::OnFrameEnd()
  744. {
  745. AZ_PROFILE_SCOPE(RPI, "Scene: OnFrameEnd");
  746. for (auto& pipeline : m_pipelines)
  747. {
  748. if (pipeline->NeedsRender())
  749. {
  750. pipeline->OnFrameEnd();
  751. }
  752. }
  753. for (auto& fp : m_featureProcessors)
  754. {
  755. fp->OnRenderEnd();
  756. }
  757. for (auto& pipeline : m_pipelines)
  758. {
  759. for (auto& pipelineView : pipeline->GetPipelineViews())
  760. {
  761. for (auto& view : pipelineView.second.m_views)
  762. {
  763. view->ClearAllFlags();
  764. }
  765. }
  766. }
  767. }
  768. RHI::TagBitRegistry<uint32_t>& Scene::GetViewTagBitRegistry()
  769. {
  770. if (m_viewTagBitRegistry == nullptr)
  771. {
  772. m_viewTagBitRegistry = RHI::TagBitRegistry<uint32_t>::Create();
  773. }
  774. return *m_viewTagBitRegistry;
  775. }
  776. RHI::Ptr<RHI::DrawFilterTagRegistry> Scene::GetDrawFilterTagRegistry() const
  777. {
  778. return m_drawFilterTagRegistry;
  779. }
  780. uint16_t Scene::GetActiveRenderPipelines() const
  781. {
  782. return m_numActiveRenderPipelines;
  783. }
  784. void Scene::UpdateSrgs()
  785. {
  786. if (!m_activated)
  787. {
  788. return;
  789. }
  790. PrepareSceneSrg();
  791. for (auto& view : m_renderPacket.m_views)
  792. {
  793. view->UpdateSrg();
  794. }
  795. }
  796. const RHI::ShaderResourceGroup* Scene::GetRHIShaderResourceGroup() const
  797. {
  798. if (m_srg.get())
  799. {
  800. return m_srg.get()->GetRHIShaderResourceGroup();
  801. }
  802. else
  803. {
  804. return nullptr;
  805. }
  806. }
  807. Data::Instance<ShaderResourceGroup> Scene::GetShaderResourceGroup() const
  808. {
  809. return m_srg;
  810. }
  811. const SceneId& Scene::GetId() const
  812. {
  813. return m_id;
  814. }
  815. AZ::Name Scene::GetName() const
  816. {
  817. return m_name;
  818. }
  819. bool Scene::SetDefaultRenderPipeline(const RenderPipelineId& pipelineId)
  820. {
  821. RenderPipelinePtr newPipeline = GetRenderPipeline(pipelineId);
  822. if (newPipeline)
  823. {
  824. m_defaultPipeline = newPipeline;
  825. return true;
  826. }
  827. return false;
  828. }
  829. RenderPipelinePtr Scene::GetDefaultRenderPipeline() const
  830. {
  831. return m_defaultPipeline;
  832. }
  833. const AZStd::vector<RenderPipelinePtr>& Scene::GetRenderPipelines() const
  834. {
  835. return m_pipelines;
  836. }
  837. void Scene::OnSceneNotificationHandlerConnected(SceneNotification* handler)
  838. {
  839. for (auto renderPipeline : m_pipelines)
  840. {
  841. handler->OnRenderPipelineAdded(renderPipeline);
  842. handler->OnRenderPipelineChanged(renderPipeline.get(), SceneNotification::RenderPipelineChangeType::Added);
  843. const RenderPipeline::PipelineViewMap& viewsInfo = renderPipeline->GetPipelineViews();
  844. for (RenderPipeline::PipelineViewMap::const_iterator itr = viewsInfo.begin(); itr != viewsInfo.end(); itr++)
  845. {
  846. if (itr->second.m_type == PipelineViewType::Persistent && itr->second.m_views.size() == 1)
  847. {
  848. handler->OnRenderPipelinePersistentViewChanged(renderPipeline.get(), itr->first, itr->second.m_views[0], nullptr);
  849. }
  850. }
  851. }
  852. }
  853. void Scene::PipelineStateLookupNeedsRebuild()
  854. {
  855. m_pipelineStatesLookupNeedsRebuild = true;
  856. }
  857. bool Scene::ConfigurePipelineState(RHI::DrawListTag drawListTag, RHI::PipelineStateDescriptorForDraw& outPipelineState) const
  858. {
  859. auto pipelineStatesItr = m_pipelineStatesLookup.find(drawListTag);
  860. if (pipelineStatesItr != m_pipelineStatesLookup.end())
  861. {
  862. const PipelineStateList& pipelineStateList = pipelineStatesItr->second;
  863. AZ_Error("RPI", pipelineStateList.size() == 1, "Scene::ConfigurePipelineState called for drawListTag [%s] which has [%zu] different pipeline states."
  864. "Using first pipeline state by default.",
  865. RHI::RHISystemInterface::Get()->GetDrawListTagRegistry()->GetName(drawListTag).GetCStr(),
  866. pipelineStateList.size()
  867. );
  868. AZ_Assert(pipelineStateList.size() > 0, "Scene::ConfigurePipelineState called for drawListTag [%s] which has [%zu] pipeline states.",
  869. RHI::RHISystemInterface::Get()->GetDrawListTagRegistry()->GetName(drawListTag).GetCStr(),
  870. pipelineStateList.size()
  871. );
  872. outPipelineState.m_renderAttachmentConfiguration = pipelineStateList[0].m_renderAttachmentConfiguration;
  873. outPipelineState.m_renderStates.m_multisampleState = pipelineStateList[0].m_multisampleState;
  874. return true;
  875. }
  876. return false;
  877. }
  878. const Scene::PipelineStateList& Scene::GetPipelineStates(RHI::DrawListTag drawListTag) const
  879. {
  880. static const PipelineStateList emptyList;
  881. auto pipelineStatesItr = m_pipelineStatesLookup.find(drawListTag);
  882. if (pipelineStatesItr != m_pipelineStatesLookup.end())
  883. {
  884. return pipelineStatesItr->second;
  885. }
  886. return emptyList;
  887. }
  888. bool Scene::HasOutputForPipelineState(RHI::DrawListTag drawListTag) const
  889. {
  890. return m_pipelineStatesLookup.find(drawListTag) != m_pipelineStatesLookup.end();
  891. }
  892. AzFramework::IVisibilityScene* Scene::GetVisibilityScene() const
  893. {
  894. return m_visibilityScene;
  895. }
  896. AZ::RPI::CullingScene* Scene::GetCullingScene() const
  897. {
  898. return m_cullingScene;
  899. }
  900. void Scene::RebuildPipelineStatesLookup()
  901. {
  902. AZ_PROFILE_SCOPE(RPI, "Scene: RebuildPipelineStatesLookup");
  903. m_pipelineStatesLookup.clear();
  904. AZStd::queue<ParentPass*> parents;
  905. for (auto renderPipeline : m_pipelines)
  906. {
  907. parents.push(renderPipeline->GetRootPass().get());
  908. // Visit all the passes under this root pass
  909. while (!parents.empty())
  910. {
  911. // Visit the first parent pass in the parents queue then remove it from the queue
  912. ParentPass* parent = parents.front();
  913. parents.pop();
  914. for (const Ptr<Pass>& child : parent->GetChildren())
  915. {
  916. ParentPass* asParent = child->AsParent();
  917. if (asParent)
  918. {
  919. // Add to parent queue for late visiting
  920. parents.push(asParent);
  921. }
  922. else if (child->HasDrawListTag())
  923. {
  924. // only need to process RasterPass since it and its derived classes need to use draw list tag to
  925. // acquire OutputAttachmentLayout and MultisampleState
  926. RasterPass* rasterPass = azrtti_cast<RasterPass*>(child.get());
  927. if (rasterPass == nullptr)
  928. {
  929. continue;
  930. }
  931. RHI::DrawListTag drawListTag = child->GetDrawListTag();
  932. auto pipelineStatesItr = m_pipelineStatesLookup.find(drawListTag);
  933. if (pipelineStatesItr == m_pipelineStatesLookup.end())
  934. {
  935. m_pipelineStatesLookup[drawListTag].emplace_back();
  936. m_pipelineStatesLookup[drawListTag][0].m_multisampleState = rasterPass->GetMultisampleState();
  937. m_pipelineStatesLookup[drawListTag][0].m_renderAttachmentConfiguration = rasterPass->GetRenderAttachmentConfiguration();
  938. rasterPass->SetPipelineStateDataIndex(0);
  939. }
  940. else
  941. {
  942. PipelineStateList& pipelineStateList = pipelineStatesItr->second;
  943. size_t size = pipelineStateList.size();
  944. u32 index = 0;
  945. auto multisampleState = rasterPass->GetMultisampleState();
  946. auto renderAttachmentConfg = rasterPass->GetRenderAttachmentConfiguration();
  947. for (; index < size; ++index)
  948. {
  949. PipelineStateData stateData = pipelineStateList[index];
  950. // don't compare the loadStoreAction of the renderAttachmentConfiguration here for two reasons:
  951. //
  952. // - The FrameGraph takes the actual LoadStoreAction from the Pass (or more exactly from the scope), and
  953. // not from the Renderattachment - config here
  954. //
  955. // - The ShadowMap - Passes are used for both CSM and projected Shadowmaps with the same drawListTag,
  956. // but they use different LoadStoreActions, which would cause two separate entries for the same
  957. // drawListTag here.
  958. if (stateData.m_multisampleState == multisampleState &&
  959. stateData.m_renderAttachmentConfiguration.IsEqual(
  960. renderAttachmentConfg, false /* compareLoadStoreAction */))
  961. {
  962. break;
  963. }
  964. }
  965. if (index < size)
  966. {
  967. // Found matching pipeline state data, set index
  968. rasterPass->SetPipelineStateDataIndex(index);
  969. }
  970. else
  971. {
  972. // No match found, add new pipeline state data
  973. pipelineStateList.emplace_back();
  974. pipelineStateList[size].m_multisampleState = rasterPass->GetMultisampleState();
  975. pipelineStateList[size].m_renderAttachmentConfiguration = rasterPass->GetRenderAttachmentConfiguration();
  976. rasterPass->SetPipelineStateDataIndex(static_cast<AZ::u32>(size));
  977. }
  978. }
  979. }
  980. }
  981. }
  982. }
  983. m_pipelineStatesLookupNeedsRebuild = false;
  984. SceneNotificationBus::Event(m_id, &SceneNotification::OnPipelineStateLookupRebuilt);
  985. }
  986. RenderPipelinePtr Scene::FindRenderPipelineForWindow(AzFramework::NativeWindowHandle windowHandle, ViewType viewType)
  987. {
  988. for (auto renderPipeline : m_pipelines)
  989. {
  990. if (renderPipeline->GetWindowHandle() == windowHandle && renderPipeline->GetViewType() == viewType)
  991. {
  992. return renderPipeline;
  993. }
  994. }
  995. return nullptr;
  996. }
  997. }
  998. }