ParentPass.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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 <AtomCore/Instance/InstanceDatabase.h>
  9. #include <AtomCore/std/containers/vector_set.h>
  10. #include <Atom/RPI.Public/Pass/SlowClearPass.h>
  11. #include <Atom/RPI.Public/Pass/ParentPass.h>
  12. #include <Atom/RPI.Public/Pass/PassAttachment.h>
  13. #include <Atom/RPI.Public/Pass/PassDefines.h>
  14. #include <Atom/RPI.Public/Pass/PassSystemInterface.h>
  15. #include <Atom/RPI.Public/RenderPipeline.h>
  16. #include <Atom/RPI.Reflect/Pass/SlowClearPassData.h>
  17. #include <Atom/RPI.Reflect/Pass/PassName.h>
  18. #include <Atom/RPI.Reflect/Pass/PassRequest.h>
  19. namespace AZ
  20. {
  21. namespace RPI
  22. {
  23. Ptr<ParentPass> ParentPass::Create(const PassDescriptor& descriptor)
  24. {
  25. Ptr<ParentPass> pass = aznew ParentPass(descriptor);
  26. return pass;
  27. }
  28. Ptr<ParentPass> ParentPass::Recreate() const
  29. {
  30. PassDescriptor desc = GetPassDescriptor();
  31. Ptr<ParentPass> pass = aznew ParentPass(desc);
  32. return pass;
  33. }
  34. ParentPass::ParentPass(const PassDescriptor& descriptor)
  35. : Pass(descriptor)
  36. {
  37. m_flags.m_createChildren = true;
  38. }
  39. ParentPass::~ParentPass()
  40. {
  41. // Explicitly remove children so we call their OnOrphan function
  42. constexpr bool callingFromDestructor = true;
  43. RemoveChildren(callingFromDestructor);
  44. }
  45. // --- Child Pass Addition ---
  46. void ParentPass::AddChild(const Ptr<Pass>& child, [[maybe_unused]] bool skipStateCheckWhenRunningTests)
  47. {
  48. // Todo: investigate if there's a way for this to not trigger on edge cases such as testing, then turn back into an Error instead of Warning.
  49. if (!(GetPassState() == PassState::Building || IsRootPass() || skipStateCheckWhenRunningTests))
  50. {
  51. AZ_Warning("PassSystem", false, "Do not add child passes outside of build phase");
  52. }
  53. if (child->m_parent != nullptr)
  54. {
  55. AZ_Assert(false, "Can't add Pass that already has a parent. Remove the Pass from it's parent before adding it to another Pass.");
  56. return;
  57. }
  58. child->m_parentChildIndex = static_cast<uint32_t>(m_children.size());
  59. m_children.push_back(child);
  60. OnChildAdded(child);
  61. }
  62. bool ParentPass::InsertChild(const Ptr<Pass>& child, ChildPassIndex position)
  63. {
  64. if (!position.IsValid())
  65. {
  66. AZ_Assert(false, "Can't insert a child pass with invalid position");
  67. return false;
  68. }
  69. return InsertChild(child, position.GetIndex());
  70. }
  71. bool ParentPass::InsertChild(const Ptr<Pass>& child, uint32_t index)
  72. {
  73. if (child->m_parent != nullptr)
  74. {
  75. AZ_Assert(false, "Can't add Pass that already has a parent. Remove the Pass from it's parent before adding it to another Pass.");
  76. return false;
  77. }
  78. if (index > m_children.size())
  79. {
  80. AZ_Assert(false, "Can't insert a child pass with invalid position");
  81. return false;
  82. }
  83. auto insertPos = m_children.cbegin() + index;
  84. m_children.insert(insertPos, child);
  85. OnChildAdded(child);
  86. for (; index < m_children.size(); ++index)
  87. {
  88. m_children[index]->m_parentChildIndex = index;
  89. }
  90. return true;
  91. }
  92. void ParentPass::OnChildAdded(const Ptr<Pass>& child)
  93. {
  94. child->m_parent = this;
  95. child->OnHierarchyChange();
  96. QueueForBuildAndInitialization();
  97. // Notify pipeline
  98. if (m_pipeline)
  99. {
  100. m_pipeline->MarkPipelinePassChanges(PipelinePassChanges::PassesAdded);
  101. // Set child's pipeline if the parent has a owning pipeline
  102. child->SetRenderPipeline(m_pipeline);
  103. }
  104. }
  105. void ParentPass::OnHierarchyChange()
  106. {
  107. Pass::OnHierarchyChange();
  108. // Notify children about hierarchy change
  109. for (const Ptr<Pass>& child : m_children)
  110. {
  111. child->OnHierarchyChange();
  112. }
  113. }
  114. // --- Child Pass Removal ---
  115. // The parameter type cannot be a reference as "const Ptr<Pass>&".
  116. // Otherwise, the content of "pass" will be overwritten by AZStd::remove()
  117. // when "pass" is a reference of a member of m_children.
  118. void ParentPass::RemoveChild(Ptr<Pass> pass)
  119. {
  120. AZ_Assert(pass->m_parent == this, "Trying to remove a pass of which we are not the parent.");
  121. AZ_Error("PassSystem", GetPassState() == PassState::Resetting || GetPassState() == PassState::Building || IsRootPass() ||
  122. (GetPassTree() && GetPassTree()->GetPassTreeState() == PassTreeState::RemovingPasses),
  123. "Do not remove child passes outside of the removal, reset, or build phases.");
  124. // Find child and move it to the end of the list
  125. [[maybe_unused]] auto it = AZStd::remove(m_children.begin(), m_children.end(), pass);
  126. AZ_Assert((it + 1) == m_children.end(), "Pass::RemoveChild found more than one Ptr<Pass> in m_children, which is not allowed.");
  127. // Delete the child that is now at the end of the list
  128. m_children.pop_back();
  129. // Signal child that it was orphaned
  130. pass->OnOrphan();
  131. // Update child indices
  132. for (u32 index = 0; index < m_children.size(); ++index)
  133. {
  134. m_children[index]->m_parentChildIndex = index;
  135. }
  136. // Notify pipeline
  137. if (m_pipeline)
  138. {
  139. m_pipeline->MarkPipelinePassChanges(PipelinePassChanges::PassesRemoved);
  140. }
  141. }
  142. void ParentPass::RemoveChildren([[maybe_unused]] bool calledFromDestructor)
  143. {
  144. AZ_Error("PassSystem", GetPassState() == PassState::Resetting || GetPassState() == PassState::Building || calledFromDestructor ||
  145. (GetPassTree() && GetPassTree()->GetPassTreeState() == PassTreeState::RemovingPasses),
  146. "Do not remove child passes outside of the removal, reset, or build phases.");
  147. for (auto child : m_children)
  148. {
  149. child->OnOrphan();
  150. }
  151. m_children.clear();
  152. // Notify pipeline
  153. if (m_pipeline)
  154. {
  155. m_pipeline->MarkPipelinePassChanges(PipelinePassChanges::PassesRemoved);
  156. }
  157. }
  158. void ParentPass::OnOrphan()
  159. {
  160. Pass::OnOrphan();
  161. for (const Ptr<Pass>& child : m_children)
  162. {
  163. child->OnHierarchyChange();
  164. }
  165. }
  166. // --- Finders ---
  167. Pass::ChildPassIndex ParentPass::FindChildPassIndex(const Name& passName) const
  168. {
  169. for (uint32_t index = 0; index < m_children.size(); ++index)
  170. {
  171. if (passName == m_children[index]->GetName())
  172. {
  173. return ChildPassIndex(index);
  174. }
  175. }
  176. return ChildPassIndex::Null;
  177. }
  178. Ptr<Pass> ParentPass::FindChildPass(const Name& passName) const
  179. {
  180. ChildPassIndex index = FindChildPassIndex(passName);
  181. return index.IsValid() ? m_children[index.GetIndex()] : Ptr<Pass>(nullptr);
  182. }
  183. const Pass* ParentPass::FindPass(RHI::DrawListTag drawListTag) const
  184. {
  185. if (HasDrawListTag() && GetDrawListTag() == drawListTag)
  186. {
  187. return this;
  188. }
  189. for (const Ptr<Pass>& child : m_children)
  190. {
  191. ParentPass* asParent = child->AsParent();
  192. if (asParent)
  193. {
  194. const Pass* pass = asParent->FindPass(drawListTag);
  195. if (pass)
  196. {
  197. return pass;
  198. }
  199. }
  200. else if (child->HasDrawListTag() && child->GetDrawListTag() == drawListTag)
  201. {
  202. return child.get();
  203. }
  204. }
  205. return nullptr;
  206. }
  207. // --- Timestamp functions ---
  208. void ParentPass::SetTimestampQueryEnabled(bool enable)
  209. {
  210. Pass::SetTimestampQueryEnabled(enable);
  211. for (auto& child : m_children)
  212. {
  213. child->SetTimestampQueryEnabled(enable);
  214. }
  215. }
  216. void ParentPass::SetPipelineStatisticsQueryEnabled(bool enable)
  217. {
  218. Pass::SetPipelineStatisticsQueryEnabled(enable);
  219. for (auto& child : m_children)
  220. {
  221. child->SetPipelineStatisticsQueryEnabled(enable);
  222. }
  223. }
  224. // --- Child creation ---
  225. void ParentPass::CreatePassesFromTemplate()
  226. {
  227. PassSystemInterface* passSystem = PassSystemInterface::Get();
  228. if (m_template == nullptr)
  229. {
  230. return;
  231. }
  232. for (const PassRequest& request : m_template->m_passRequests)
  233. {
  234. Ptr<Pass> pass = passSystem->CreatePassFromRequest(&request);
  235. if (pass)
  236. {
  237. AddChild(pass);
  238. }
  239. }
  240. }
  241. void ParentPass::CreateClearPassFromBinding(PassAttachmentBinding& binding, PassRequest& clearRequest)
  242. {
  243. if (binding.m_unifiedScopeDesc.m_loadStoreAction.m_loadAction == RHI::AttachmentLoadAction::Clear ||
  244. binding.m_unifiedScopeDesc.m_loadStoreAction.m_loadActionStencil == RHI::AttachmentLoadAction::Clear)
  245. {
  246. // Set the name of the child clear pass as well as the binding it's connected to
  247. clearRequest.m_passName = ConcatPassName(Name("Clear"), binding.m_name);
  248. clearRequest.m_connections[0].m_attachmentRef.m_attachment = binding.m_name;
  249. // Set the pass clear value to the clear value of the attachment binding
  250. SlowClearPassData* clearData = static_cast<SlowClearPassData*>(clearRequest.m_passData.get());
  251. clearData->m_clearValue = binding.m_unifiedScopeDesc.m_loadStoreAction.m_clearValue;
  252. // Create and add the pass
  253. Ptr<Pass> clearPass = PassSystemInterface::Get()->CreatePassFromRequest(&clearRequest);
  254. if (clearPass)
  255. {
  256. AddChild(clearPass);
  257. }
  258. }
  259. }
  260. void ParentPass::CreateClearPassesFromBindings()
  261. {
  262. PassRequest clearRequest;
  263. clearRequest.m_templateName = Name("SlowClearPassTemplate");
  264. clearRequest.m_passData = AZStd::make_shared<SlowClearPassData>();
  265. clearRequest.m_connections.emplace_back();
  266. clearRequest.m_connections[0].m_localSlot = Name("ClearInputOutput");
  267. clearRequest.m_connections[0].m_attachmentRef.m_pass = Name("Parent");
  268. for (uint32_t idx = 0; idx < GetInputCount(); ++idx)
  269. {
  270. CreateClearPassFromBinding(GetInputBinding(idx), clearRequest);
  271. }
  272. for (uint32_t idx = 0; idx < GetInputOutputCount(); ++idx)
  273. {
  274. CreateClearPassFromBinding(GetInputOutputBinding(idx), clearRequest);
  275. }
  276. }
  277. // --- Pass behavior functions ---
  278. void ParentPass::CreateChildPasses()
  279. {
  280. // The already created flag prevents this function from executing multiple times a frame
  281. if (!m_flags.m_createChildren || m_flags.m_alreadyCreatedChildren)
  282. {
  283. return;
  284. }
  285. m_flags.m_alreadyCreatedChildren = true;
  286. RemoveChildren();
  287. CreateClearPassesFromBindings();
  288. CreatePassesFromTemplate();
  289. CreateChildPassesInternal();
  290. m_flags.m_createChildren = false;
  291. }
  292. void ParentPass::ResetInternal()
  293. {
  294. for (const Ptr<Pass>& child : m_children)
  295. {
  296. child->Reset();
  297. }
  298. }
  299. void ParentPass::BuildInternal()
  300. {
  301. CreateChildPasses();
  302. for (const Ptr<Pass>& child : m_children)
  303. {
  304. child->Build();
  305. }
  306. }
  307. void ParentPass::OnInitializationFinishedInternal()
  308. {
  309. for (const Ptr<Pass>& child : m_children)
  310. {
  311. child->OnInitializationFinished();
  312. }
  313. }
  314. void ParentPass::InitializeInternal()
  315. {
  316. for (const Ptr<Pass>& child : m_children)
  317. {
  318. child->Initialize();
  319. }
  320. }
  321. void ParentPass::Validate(PassValidationResults& validationResults)
  322. {
  323. if (PassValidation::IsEnabled())
  324. {
  325. Pass::Validate(validationResults);
  326. for (const Ptr<Pass>& child : m_children)
  327. {
  328. child->Validate(validationResults);
  329. }
  330. }
  331. }
  332. void ParentPass::UpdateConnectedBindings()
  333. {
  334. Pass::UpdateConnectedBindings();
  335. if (IsEnabled())
  336. {
  337. for (const Ptr<Pass>& child : m_children)
  338. {
  339. child->UpdateConnectedBindings();
  340. }
  341. }
  342. }
  343. void ParentPass::FrameBeginInternal(FramePrepareParams params)
  344. {
  345. for (const Ptr<Pass>& child : m_children)
  346. {
  347. child->FrameBegin(params);
  348. }
  349. }
  350. void ParentPass::FrameEndInternal()
  351. {
  352. for (const Ptr<Pass>& child : m_children)
  353. {
  354. child->FrameEnd();
  355. }
  356. }
  357. // --- Misc ---
  358. void ParentPass::SetRenderPipeline(RenderPipeline* pipeline)
  359. {
  360. if (m_pipeline == pipeline)
  361. {
  362. return;
  363. }
  364. // Call base implementation
  365. Pass::SetRenderPipeline(pipeline);
  366. // Set render pipeline on children
  367. for (const auto& child : m_children)
  368. {
  369. child->SetRenderPipeline(pipeline);
  370. }
  371. }
  372. void ParentPass::GetViewDrawListInfo(RHI::DrawListMask& outDrawListMask, PassesByDrawList& outPassesByDrawList, const PipelineViewTag& viewTag) const
  373. {
  374. // Call base implementation
  375. Pass::GetViewDrawListInfo(outDrawListMask, outPassesByDrawList, viewTag);
  376. // Get mask from children
  377. for (const auto& child : m_children)
  378. {
  379. child->GetViewDrawListInfo(outDrawListMask, outPassesByDrawList, viewTag);
  380. }
  381. }
  382. void ParentPass::GetPipelineViewTags(PipelineViewTags& outTags) const
  383. {
  384. // Call base implementation
  385. Pass::GetPipelineViewTags(outTags);
  386. // Get pipeline view tags from children
  387. for (const auto& child : m_children)
  388. {
  389. child->GetPipelineViewTags(outTags);
  390. }
  391. }
  392. Ptr<PassAttachment> ParentPass::GetOwnedAttachment(const Name& attachmentName) const
  393. {
  394. for (Ptr<PassAttachment> attachment : m_ownedAttachments)
  395. {
  396. if (attachment->m_name == attachmentName)
  397. {
  398. return attachment;
  399. }
  400. }
  401. return nullptr;
  402. }
  403. // --- Debug functions ---
  404. AZStd::span<const Ptr<Pass>> ParentPass::GetChildren() const
  405. {
  406. return m_children;
  407. }
  408. void ParentPass::DebugPrint() const
  409. {
  410. if (PassValidation::IsEnabled())
  411. {
  412. Pass::DebugPrint();
  413. // Print children
  414. for (const Ptr<Pass>& child : m_children)
  415. {
  416. child->DebugPrint();
  417. }
  418. }
  419. }
  420. PipelineStatisticsResult ParentPass::GetPipelineStatisticsResultInternal() const
  421. {
  422. AZStd::vector<PipelineStatisticsResult> pipelineStatisticsResultArray;
  423. pipelineStatisticsResultArray.reserve(m_children.size());
  424. // Calculate the PipelineStatistics result by summing all of its child's PipelineStatistics
  425. for (const Ptr<Pass>& childPass : m_children)
  426. {
  427. pipelineStatisticsResultArray.emplace_back(childPass->GetLatestPipelineStatisticsResult());
  428. }
  429. return PipelineStatisticsResult(pipelineStatisticsResultArray);
  430. }
  431. } // namespace RPI
  432. } // namespace AZ