3
0

Pass.cpp 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  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 <AzCore/RTTI/RTTI.h>
  9. #include <AzCore/std/string/conversions.h>
  10. #include <AtomCore/Instance/InstanceDatabase.h>
  11. #include <AtomCore/std/containers/vector_set.h>
  12. #include <Atom/RHI/FrameGraphAttachmentInterface.h>
  13. #include <Atom/RHI/FrameGraphBuilder.h>
  14. #include <Atom/RHI/RHIUtils.h>
  15. #include <Atom/RHI.Reflect/Base.h>
  16. #include <Atom/RPI.Public/Buffer/Buffer.h>
  17. #include <Atom/RPI.Public/Image/AttachmentImage.h>
  18. #include <Atom/RPI.Reflect/Image/Image.h>
  19. #include <Atom/RPI.Public/Pass/AttachmentReadback.h>
  20. #include <Atom/RPI.Public/Pass/ParentPass.h>
  21. #include <Atom/RPI.Public/Pass/Pass.h>
  22. #include <Atom/RPI.Public/Pass/PassLibrary.h>
  23. #include <Atom/RPI.Public/Pass/PassDefines.h>
  24. #include <Atom/RPI.Public/Pass/PassSystemInterface.h>
  25. #include <Atom/RPI.Public/Pass/PassUtils.h>
  26. #include <Atom/RPI.Public/Pass/Specific/ImageAttachmentPreviewPass.h>
  27. #include <Atom/RPI.Public/RenderPipeline.h>
  28. #include <Atom/RPI.Reflect/Image/AttachmentImageAsset.h>
  29. #include <Atom/RPI.Reflect/Pass/PassRequest.h>
  30. #include <Atom/RPI.Reflect/Pass/PassTemplate.h>
  31. #include <Atom/RPI.Reflect/Pass/PassName.h>
  32. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  33. namespace AZ
  34. {
  35. namespace RPI
  36. {
  37. // --- Constructors ---
  38. Pass::Pass(const PassDescriptor& descriptor)
  39. : m_name(descriptor.m_passName)
  40. , m_path(descriptor.m_passName)
  41. {
  42. AZ_RPI_PASS_ASSERT((descriptor.m_passRequest == nullptr) || (descriptor.m_passTemplate != nullptr),
  43. "Pass::Pass - request is valid but template is nullptr. This is not allowed. Passing a valid passRequest also requires a valid passTemplate.");
  44. m_passData = PassUtils::GetPassDataPtr(descriptor);
  45. if (m_passData)
  46. {
  47. PassUtils::ExtractPipelineGlobalConnections(m_passData, m_pipelineGlobalConnections);
  48. m_viewTag = m_passData->m_pipelineViewTag;
  49. }
  50. m_flags.m_enabled = true;
  51. m_flags.m_timestampQueryEnabled = false;
  52. m_flags.m_pipelineStatisticsQueryEnabled = false;
  53. m_template = descriptor.m_passTemplate;
  54. if (descriptor.m_passRequest != nullptr)
  55. {
  56. // Assert m_template is the same as the one in the pass request
  57. if (PassValidation::IsEnabled())
  58. {
  59. const AZStd::shared_ptr<const PassTemplate> passTemplate = PassSystemInterface::Get()->GetPassTemplate(descriptor.m_passRequest->m_templateName);
  60. AZ_RPI_PASS_ASSERT(m_template == passTemplate, "Error: template in PassDescriptor doesn't match template from PassRequest!");
  61. }
  62. m_request = *descriptor.m_passRequest;
  63. m_flags.m_createdByPassRequest = true;
  64. m_flags.m_enabled = m_request.m_passEnabled;
  65. }
  66. PassSystemInterface::Get()->RegisterPass(this);
  67. QueueForBuildAndInitialization();
  68. // Skip reset since the pass just got created
  69. m_state = PassState::Reset;
  70. }
  71. Pass::~Pass()
  72. {
  73. AZ_RPI_BREAK_ON_TARGET_PASS;
  74. PassSystemInterface::Get()->UnregisterPass(this);
  75. }
  76. PassDescriptor Pass::GetPassDescriptor() const
  77. {
  78. PassDescriptor desc;
  79. desc.m_passName = m_name;
  80. desc.m_passTemplate = m_template ? PassSystemInterface::Get()->GetPassTemplate(m_template->m_name) : nullptr;
  81. if (m_flags.m_createdByPassRequest)
  82. {
  83. desc.m_passRequest = AZStd::make_shared<PassRequest>(m_request);
  84. }
  85. else
  86. {
  87. desc.m_passRequest.reset();
  88. }
  89. desc.m_passData = m_passData;
  90. return desc;
  91. }
  92. void Pass::SetEnabled(bool enabled)
  93. {
  94. m_flags.m_enabled = enabled;
  95. OnHierarchyChange();
  96. }
  97. // --- Error Logging ---
  98. void Pass::LogError(AZStd::string&& message)
  99. {
  100. #if AZ_RPI_ENABLE_PASS_DEBUGGING
  101. AZ::Debug::Trace::Instance().Break();
  102. #endif
  103. if (PassValidation::IsEnabled())
  104. {
  105. ++m_errors;
  106. if (m_errorMessages.size() < MessageLogLimit)
  107. {
  108. m_errorMessages.push_back(AZStd::move(message));
  109. }
  110. }
  111. }
  112. void Pass::LogWarning(AZStd::string&& message)
  113. {
  114. if (PassValidation::IsEnabled())
  115. {
  116. ++m_warnings;
  117. if (m_warningMessages.size() < MessageLogLimit)
  118. {
  119. m_warningMessages.push_back(AZStd::move(message));
  120. }
  121. }
  122. }
  123. // --- Hierarchy functions ---
  124. void Pass::OnHierarchyChange()
  125. {
  126. if (m_parent != nullptr)
  127. {
  128. // Set new tree depth and path
  129. m_flags.m_parentEnabled = m_parent->m_flags.m_enabled && (m_parent->m_flags.m_parentEnabled || m_parent->m_parent == nullptr);
  130. m_treeDepth = m_parent->m_treeDepth + 1;
  131. m_path = ConcatPassName(m_parent->m_path, m_name);
  132. m_flags.m_partOfHierarchy = m_parent->m_flags.m_partOfHierarchy;
  133. if (m_state == PassState::Orphaned)
  134. {
  135. QueueForBuildAndInitialization();
  136. }
  137. }
  138. AZ_RPI_BREAK_ON_TARGET_PASS;
  139. }
  140. void Pass::RemoveFromParent()
  141. {
  142. AZ_RPI_BREAK_ON_TARGET_PASS;
  143. AZ_RPI_PASS_ASSERT(m_parent != nullptr, "Trying to remove pass from parent but pointer to the parent pass is null.");
  144. m_parent->RemoveChild(Ptr<Pass>(this));
  145. m_queueState = PassQueueState::NoQueue;
  146. m_state = PassState::Orphaned;
  147. }
  148. void Pass::OnOrphan()
  149. {
  150. AZ_RPI_BREAK_ON_TARGET_PASS;
  151. if (m_flags.m_containsGlobalReference && m_pipeline != nullptr)
  152. {
  153. m_pipeline->RemovePipelineGlobalConnectionsFromPass(this);
  154. }
  155. m_parent = nullptr;
  156. m_flags.m_partOfHierarchy = false;
  157. m_treeDepth = 0;
  158. m_parentChildIndex = 0;
  159. m_queueState = PassQueueState::NoQueue;
  160. m_state = PassState::Orphaned;
  161. }
  162. ParentPass* Pass::AsParent()
  163. {
  164. return azrtti_cast<ParentPass*>(this);
  165. }
  166. const ParentPass* Pass::AsParent() const
  167. {
  168. return azrtti_cast<const ParentPass*>(this);
  169. }
  170. // --- Bindings ---
  171. PassAttachmentBinding& Pass::GetInputBinding(uint32_t index)
  172. {
  173. uint32_t bindingIndex = m_inputBindingIndices[index];
  174. return m_attachmentBindings[bindingIndex];
  175. }
  176. PassAttachmentBinding& Pass::GetInputOutputBinding(uint32_t index)
  177. {
  178. uint32_t bindingIndex = m_inputOutputBindingIndices[index];
  179. return m_attachmentBindings[bindingIndex];
  180. }
  181. PassAttachmentBinding& Pass::GetOutputBinding(uint32_t index)
  182. {
  183. uint32_t bindingIndex = m_outputBindingIndices[index];
  184. return m_attachmentBindings[bindingIndex];
  185. }
  186. void Pass::AddAttachmentBinding(PassAttachmentBinding attachmentBinding)
  187. {
  188. auto index = static_cast<uint8_t>(m_attachmentBindings.size());
  189. // Add the binding. This will assert if the fixed size array is full.
  190. m_attachmentBindings.push_back(attachmentBinding);
  191. // Add the index of the binding to the input, output or input/output list based on the slot type
  192. switch (attachmentBinding.m_slotType)
  193. {
  194. case PassSlotType::Input:
  195. m_inputBindingIndices.push_back(index);
  196. break;
  197. case PassSlotType::InputOutput:
  198. m_inputOutputBindingIndices.push_back(index);
  199. break;
  200. case PassSlotType::Output:
  201. m_outputBindingIndices.push_back(index);
  202. break;
  203. default:
  204. break;
  205. }
  206. }
  207. // --- Finders ---
  208. Ptr<Pass> Pass::FindAdjacentPass(const Name& passName)
  209. {
  210. // 1. Check This
  211. if (passName == PassNameThis)
  212. {
  213. return Ptr<Pass>(this);
  214. }
  215. // 2. Check Parent
  216. if (m_parent == nullptr)
  217. {
  218. return nullptr;
  219. }
  220. if (passName == PassNameParent || passName == m_parent->GetName())
  221. {
  222. return Ptr<Pass>(m_parent);
  223. }
  224. // 3. Check Siblings
  225. Ptr<Pass> foundPass = m_parent->FindChildPass(passName);
  226. // 4. Check Children
  227. if (!foundPass && AsParent())
  228. {
  229. foundPass = AsParent()->FindChildPass(passName);
  230. }
  231. // Finished search, return
  232. return foundPass;
  233. }
  234. PassAttachmentBinding* Pass::FindAttachmentBinding(const Name& slotName)
  235. {
  236. for (PassAttachmentBinding& binding : m_attachmentBindings)
  237. {
  238. if (slotName == binding.m_name)
  239. {
  240. return &binding;
  241. }
  242. }
  243. return nullptr;
  244. }
  245. const PassAttachmentBinding* Pass::FindAttachmentBinding(const Name& slotName) const
  246. {
  247. for (const PassAttachmentBinding& binding : m_attachmentBindings)
  248. {
  249. if (slotName == binding.m_name)
  250. {
  251. return &binding;
  252. }
  253. }
  254. return nullptr;
  255. }
  256. Ptr<PassAttachment> Pass::FindOwnedAttachment(const Name& attachmentName) const
  257. {
  258. for (const Ptr<PassAttachment>& attachment : m_ownedAttachments)
  259. {
  260. if (attachment->m_name == attachmentName)
  261. {
  262. return attachment;
  263. }
  264. }
  265. return nullptr;
  266. }
  267. Ptr<PassAttachment> Pass::FindAttachment(const Name& slotName) const
  268. {
  269. if (const PassAttachmentBinding* binding = FindAttachmentBinding(slotName))
  270. {
  271. return binding->GetAttachment();
  272. }
  273. return FindOwnedAttachment(slotName);
  274. }
  275. const PassAttachmentBinding* Pass::FindAdjacentBinding(const PassAttachmentRef& attachmentRef, [[maybe_unused]] const char* attachmentSourceTypeDebugName)
  276. {
  277. const PassAttachmentBinding* result = nullptr;
  278. if (attachmentRef.m_pass.IsEmpty() && attachmentRef.m_attachment.IsEmpty())
  279. {
  280. // The data isn't actually referencing anything, so this is not an error, just return null.
  281. return result;
  282. }
  283. if (attachmentRef.m_pass.IsEmpty() != attachmentRef.m_attachment.IsEmpty())
  284. {
  285. AZ_Error("Pass", false,
  286. "Invalid attachment reference (Pass [%s], Attachment [%s]). Both Pass and Attachment must be set.",
  287. attachmentRef.m_pass.GetCStr(), attachmentRef.m_attachment.GetCStr());
  288. return result;
  289. }
  290. // Find pass
  291. if (Ptr<Pass> pass = FindAdjacentPass(attachmentRef.m_pass))
  292. {
  293. // Find attachment within pass
  294. result = pass->FindAttachmentBinding(attachmentRef.m_attachment);
  295. }
  296. AZ_Error("Pass", result, "Pass [%s] could not find %s (Pass [%s], Attachment [%s])",
  297. m_path.GetCStr(), attachmentSourceTypeDebugName, attachmentRef.m_pass.GetCStr(), attachmentRef.m_attachment.GetCStr());
  298. return result;
  299. }
  300. // --- PassTemplate related functions ---
  301. void Pass::CreateBindingsFromTemplate()
  302. {
  303. if (m_template)
  304. {
  305. for (const PassSlot& slot : m_template->m_slots)
  306. {
  307. PassAttachmentBinding binding(slot);
  308. AddAttachmentBinding(binding);
  309. }
  310. }
  311. }
  312. void Pass::AttachBufferToSlot(AZStd::string_view slot, Data::Instance<Buffer> buffer)
  313. {
  314. AttachBufferToSlot(Name(slot), buffer);
  315. }
  316. void Pass::AttachBufferToSlot(const Name& slot, Data::Instance<Buffer> buffer)
  317. {
  318. if (!buffer)
  319. {
  320. return;
  321. }
  322. PassAttachmentBinding* localBinding = FindAttachmentBinding(slot);
  323. if (!localBinding)
  324. {
  325. AZ_RPI_PASS_ERROR(false, "Pass::AttachBufferToSlot - Pass [%s] failed to find slot [%s].",
  326. m_path.GetCStr(), slot.GetCStr());
  327. return;
  328. }
  329. // We can't handle the case that there is already an attachment attached yet.
  330. // We could consider to add it later if there are needs. It may require remove from the owned attachment list and
  331. // handle the connected bindings
  332. if (localBinding->GetAttachment())
  333. {
  334. AZ_RPI_PASS_ERROR(false, "Pass::AttachBufferToSlot - Slot [%s] already has attachment [%s].",
  335. slot.GetCStr(), localBinding->GetAttachment()->m_name.GetCStr());
  336. return;
  337. }
  338. PassBufferAttachmentDesc desc;
  339. desc.m_bufferDescriptor = buffer->GetRHIBuffer()->GetDescriptor();
  340. desc.m_lifetime = RHI::AttachmentLifetimeType::Imported;
  341. desc.m_name = buffer->GetAttachmentId();
  342. Ptr<PassAttachment> attachment = CreateAttachmentFromDesc(desc);
  343. attachment->m_importedResource = buffer;
  344. m_ownedAttachments.push_back(attachment);
  345. localBinding->SetOriginalAttachment(attachment);
  346. }
  347. void Pass::AttachImageToSlot(const Name& slot, Data::Instance<AttachmentImage> image)
  348. {
  349. PassAttachmentBinding* localBinding = FindAttachmentBinding(slot);
  350. if (!localBinding)
  351. {
  352. AZ_RPI_PASS_ERROR(false, "Pass::AttachImageToSlot - Pass [%s] failed to find slot [%s].",
  353. m_path.GetCStr(), slot.GetCStr());
  354. return;
  355. }
  356. // We can't handle the case that there is already an attachment attached yet.
  357. // We could consider to add it later if there are needs. It may require remove from the owned attachment list and
  358. // handle the connected bindings
  359. if (localBinding->GetAttachment())
  360. {
  361. AZ_RPI_PASS_ERROR(false, "Pass::AttachImageToSlot - Slot [%s] already has attachment [%s].",
  362. slot.GetCStr(), localBinding->GetAttachment()->m_name.GetCStr());
  363. return;
  364. }
  365. PassImageAttachmentDesc desc;
  366. desc.m_imageDescriptor = image->GetRHIImage()->GetDescriptor();
  367. desc.m_lifetime = RHI::AttachmentLifetimeType::Imported;
  368. desc.m_name = image->GetAttachmentId();
  369. Ptr<PassAttachment> attachment = CreateAttachmentFromDesc(desc);
  370. attachment->m_importedResource = image;
  371. m_ownedAttachments.push_back(attachment);
  372. localBinding->SetOriginalAttachment(attachment);
  373. }
  374. void Pass::ProcessConnection(const PassConnection& connection, uint32_t slotTypeMask)
  375. {
  376. [[maybe_unused]] auto prefix = [&]() -> AZStd::string
  377. {
  378. return AZStd::string::format(
  379. "Pass::ProcessConnection %s:%s -> %s:%s",
  380. m_path.GetCStr(),
  381. connection.m_localSlot.GetCStr(),
  382. connection.m_attachmentRef.m_pass.GetCStr(),
  383. connection.m_attachmentRef.m_attachment.GetCStr());
  384. };
  385. // -- Find Local Binding --
  386. // Get the input from this pass that forms one end of the connection
  387. PassAttachmentBinding* localBinding = FindAttachmentBinding(connection.m_localSlot);
  388. if (!localBinding)
  389. {
  390. AZ_RPI_PASS_ERROR(false, "%s: failed to find Local Slot.", prefix().c_str());
  391. return;
  392. }
  393. // Slot type mask used to skip connections at various stages of initialization
  394. uint32_t bindingMask = (1 << uint32_t(localBinding->m_slotType));
  395. if (!(bindingMask & slotTypeMask))
  396. {
  397. return;
  398. }
  399. // -- Local Variables --
  400. Name connectedPassName = connection.m_attachmentRef.m_pass;
  401. Name connectedSlotName = connection.m_attachmentRef.m_attachment;
  402. Ptr<PassAttachment> attachment = nullptr;
  403. PassAttachmentBinding* connectedBinding = nullptr;
  404. bool foundPass = false;
  405. bool slotTypeMismatch = false;
  406. // -- Search This Pass --
  407. if (connectedPassName == PassNameThis)
  408. {
  409. foundPass = true;
  410. attachment = FindOwnedAttachment(connectedSlotName);
  411. AZ_RPI_PASS_ERROR(
  412. attachment, "%s: Current Pass doesn't own an attachment named [%s].", prefix().c_str(), connectedSlotName.GetCStr());
  413. }
  414. // -- Search Pipeline --
  415. else if (connectedPassName == PipelineGlobalKeyword)
  416. {
  417. AZ_RPI_PASS_ERROR(m_pipeline != nullptr, "%s: Pass doesn't have a valid pipeline pointer.", prefix().c_str());
  418. foundPass = true; // Using the "Pipeline" keyword, no need to continue searching for passes
  419. if (m_pipeline)
  420. {
  421. const PipelineGlobalBinding* globalBinding = m_pipeline->GetPipelineGlobalConnection(connectedSlotName);
  422. if (globalBinding)
  423. {
  424. connectedBinding = globalBinding->m_binding;
  425. }
  426. AZ_RPI_PASS_ERROR(connectedBinding, "%s: Cannot find pipeline global connection.", prefix().c_str());
  427. }
  428. }
  429. // -- Search Parent & Siblings --
  430. // The (connectedPassName != m_name) avoids edge case where parent pass has child pass of same name.
  431. // In this case, parent pass would ask it's parent pass for a sibling with the given name and get a pointer to itself.
  432. // It would then try to connect to itself, which is obviously not the intention of the user
  433. if (!foundPass && m_parent && connectedPassName != m_name)
  434. {
  435. if (connectedPassName == PassNameParent)
  436. {
  437. foundPass = true;
  438. connectedBinding = m_parent->FindAttachmentBinding(connectedSlotName);
  439. if (!connectedBinding)
  440. {
  441. attachment = m_parent->FindOwnedAttachment(connectedSlotName);
  442. }
  443. else
  444. {
  445. slotTypeMismatch = connectedBinding->m_slotType != localBinding->m_slotType &&
  446. connectedBinding->m_slotType != PassSlotType::InputOutput &&
  447. localBinding->m_slotType != PassSlotType::InputOutput;
  448. }
  449. }
  450. else
  451. {
  452. // Use the connection Name to find a sibling pass
  453. Ptr<Pass> siblingPass = m_parent->FindChildPass(connectedPassName);
  454. if (siblingPass)
  455. {
  456. foundPass = true;
  457. connectedBinding = siblingPass->FindAttachmentBinding(connectedSlotName);
  458. slotTypeMismatch = connectedBinding != nullptr &&
  459. connectedBinding->m_slotType == localBinding->m_slotType &&
  460. connectedBinding->m_slotType != PassSlotType::InputOutput;
  461. }
  462. }
  463. }
  464. // -- Search Children --
  465. ParentPass* asParent = AsParent();
  466. if (!foundPass && asParent)
  467. {
  468. Ptr<Pass> childPass = asParent->FindChildPass(connectedPassName);
  469. if (childPass)
  470. {
  471. foundPass = true;
  472. connectedBinding = childPass->FindAttachmentBinding(connectedSlotName);
  473. slotTypeMismatch = connectedBinding != nullptr &&
  474. connectedBinding->m_slotType != localBinding->m_slotType &&
  475. connectedBinding->m_slotType != PassSlotType::InputOutput &&
  476. localBinding->m_slotType != PassSlotType::InputOutput;
  477. }
  478. }
  479. // -- Finalize & Report Errors --
  480. if (slotTypeMismatch)
  481. {
  482. AZ_RPI_PASS_ERROR(
  483. false,
  484. "%s: Slot Type Mismatch - When connecting to a child slot, both slots must be of the same type, or one must be "
  485. "InputOutput.",
  486. prefix().c_str());
  487. connectedBinding = nullptr;
  488. }
  489. if (connectedBinding)
  490. {
  491. localBinding->m_connectedBinding = connectedBinding;
  492. UpdateConnectedBinding(*localBinding);
  493. }
  494. else if (attachment)
  495. {
  496. localBinding->SetOriginalAttachment(attachment);
  497. }
  498. else
  499. {
  500. if (!m_flags.m_partOfHierarchy)
  501. {
  502. // [GFX TODO][ATOM-13693]: REMOVE POST R1 - passes not in hierarchy should no longer have this function called
  503. // When view is changing, removal of the passes can occur (cascade shadow passes for example)
  504. // resulting in temporary orphan passes that will be removed over the next frame.
  505. AZ_RPI_PASS_WARNING(false, "%s: Pass is no longer part of the hierarchy and about to be removed.", prefix().c_str());
  506. }
  507. else if (foundPass)
  508. {
  509. AZ_RPI_PASS_ERROR(false, "%s: Could not find binding on target.", prefix().c_str());
  510. }
  511. else
  512. {
  513. AZ_RPI_PASS_ERROR(false, "%s: Could not find target pass.", prefix().c_str());
  514. }
  515. }
  516. }
  517. void Pass::ProcessFallbackConnection(const PassFallbackConnection& connection)
  518. {
  519. PassAttachmentBinding* inputBinding = FindAttachmentBinding(connection.m_inputSlotName);
  520. PassAttachmentBinding* outputBinding = FindAttachmentBinding(connection.m_outputSlotName);
  521. [[maybe_unused]] auto prefix = [&]() -> AZStd::string
  522. {
  523. return AZStd::string::format(
  524. "Pass::ProcessFallbackConnection: %s, %s -> %s",
  525. m_path.GetCStr(),
  526. connection.m_inputSlotName.GetCStr(),
  527. connection.m_outputSlotName.GetCStr());
  528. };
  529. if (!outputBinding || !inputBinding)
  530. {
  531. AZ_RPI_PASS_ERROR(inputBinding, "%s: failed to find input slot.", prefix().c_str());
  532. AZ_RPI_PASS_ERROR(outputBinding, "%s: failed to find output slot.", prefix().c_str());
  533. return;
  534. }
  535. bool typesAreValid = inputBinding->m_slotType == PassSlotType::Input && outputBinding->m_slotType == PassSlotType::Output;
  536. if (!typesAreValid)
  537. {
  538. AZ_RPI_PASS_ERROR(
  539. inputBinding->m_slotType == PassSlotType::Input, "%s: Input doesn't have SlotType::Input.", prefix().c_str());
  540. AZ_RPI_PASS_ERROR(
  541. outputBinding->m_slotType == PassSlotType::Output, "%s: Output doesn't have SlotType::Output.", prefix().c_str());
  542. return;
  543. }
  544. outputBinding->m_fallbackBinding = inputBinding;
  545. UpdateConnectedBinding(*outputBinding);
  546. }
  547. template<typename AttachmentDescType>
  548. Ptr<PassAttachment> Pass::CreateAttachmentFromDesc(const AttachmentDescType& desc)
  549. {
  550. Ptr<PassAttachment> attachment = aznew PassAttachment(desc);
  551. // If the attachment is imported, we will create the resource (buffer or image) of this attachment
  552. // from asset referenced in m_assetRef
  553. // The resource instance will be saved in m_importedResource and the attachment id is acquired from resource instance
  554. if (desc.m_lifetime == RHI::AttachmentLifetimeType::Imported)
  555. {
  556. attachment->m_path = desc.m_name;
  557. if (attachment->m_descriptor.m_type == RHI::AttachmentType::Buffer)
  558. {
  559. Data::Asset<BufferAsset> bufferAsset = AssetUtils::LoadAssetById<BufferAsset>(desc.m_assetRef.m_assetId, AssetUtils::TraceLevel::None);
  560. if (bufferAsset.IsReady())
  561. {
  562. Data::Instance<Buffer> buffer = Buffer::FindOrCreate(bufferAsset);
  563. if (buffer)
  564. {
  565. attachment->m_path = buffer->GetAttachmentId();
  566. attachment->m_importedResource = buffer;
  567. attachment->m_descriptor = buffer->GetRHIBuffer()->GetDescriptor();
  568. }
  569. }
  570. }
  571. else if (attachment->m_descriptor.m_type == RHI::AttachmentType::Image)
  572. {
  573. Data::Asset<AttachmentImageAsset> imageAsset = AssetUtils::LoadAssetById<AttachmentImageAsset>(desc.m_assetRef.m_assetId, AssetUtils::TraceLevel::None);
  574. if (imageAsset.IsReady())
  575. {
  576. Data::Instance<AttachmentImage> image = AttachmentImage::FindOrCreate(imageAsset);
  577. if (image)
  578. {
  579. attachment->m_path = image->GetAttachmentId();
  580. attachment->m_importedResource = image;
  581. attachment->m_descriptor = image->GetDescriptor();
  582. }
  583. }
  584. }
  585. else
  586. {
  587. AZ_RPI_PASS_ASSERT(false, "Unsupported imported attachment type");
  588. }
  589. }
  590. else
  591. {
  592. // Only apply path name to transient attachment. Keep the original name for imported attachment
  593. attachment->ComputePathName(m_path);
  594. }
  595. // Setup attachment sources...
  596. if (desc.m_sizeSource.m_source.m_pass == PipelineKeyword) // if source is pipeline
  597. {
  598. attachment->m_renderPipelineSource = m_pipeline;
  599. attachment->m_getSizeFromPipeline = true;
  600. attachment->m_sizeMultipliers = desc.m_sizeSource.m_multipliers;
  601. }
  602. else if (const PassAttachmentBinding* source = FindAdjacentBinding(desc.m_sizeSource.m_source, "SizeSource"))
  603. {
  604. attachment->m_sizeSource = source;
  605. attachment->m_sizeMultipliers = desc.m_sizeSource.m_multipliers;
  606. }
  607. if (desc.m_formatSource.m_pass == PipelineKeyword) // if source is pipeline
  608. {
  609. attachment->m_renderPipelineSource = m_pipeline;
  610. attachment->m_getFormatFromPipeline = true;
  611. }
  612. else if (const PassAttachmentBinding* source = FindAdjacentBinding(desc.m_formatSource, "FormatSource"))
  613. {
  614. attachment->m_formatSource = source;
  615. }
  616. if (desc.m_multisampleSource.m_pass == PipelineKeyword) // if source is pipeline
  617. {
  618. attachment->m_renderPipelineSource = m_pipeline;
  619. attachment->m_getMultisampleStateFromPipeline = true;
  620. }
  621. else if (const PassAttachmentBinding* source = FindAdjacentBinding(desc.m_multisampleSource, "MultisampleSource"))
  622. {
  623. attachment->m_multisampleSource = source;
  624. }
  625. if (const PassAttachmentBinding* source = FindAdjacentBinding(desc.m_arraySizeSource, "ArraySizeSource"))
  626. {
  627. attachment->m_arraySizeSource = source;
  628. }
  629. attachment->m_ownerPass = this;
  630. return attachment;
  631. }
  632. template<typename AttachmentDescType>
  633. void Pass::OverrideOrAddAttachment(const AttachmentDescType& desc)
  634. {
  635. bool overrideAttachment = false;
  636. // Search existing attachments
  637. for (size_t i = 0; i < m_ownedAttachments.size(); ++i)
  638. {
  639. // If we find one with the same name
  640. if (m_ownedAttachments[i]->m_name == desc.m_name)
  641. {
  642. // Override it
  643. m_ownedAttachments[i] = CreateAttachmentFromDesc(desc);
  644. overrideAttachment = true;
  645. break;
  646. }
  647. }
  648. // If we didn't override any attachments
  649. if (!overrideAttachment)
  650. {
  651. // Create a new one
  652. m_ownedAttachments.emplace_back(CreateAttachmentFromDesc(desc));
  653. }
  654. }
  655. void Pass::SetupInputsFromRequest()
  656. {
  657. if (m_flags.m_createdByPassRequest)
  658. {
  659. const uint32_t slotTypeMask = (1 << uint32_t(PassSlotType::Input)) | (1 << uint32_t(PassSlotType::InputOutput));
  660. for (const PassConnection& connection : m_request.m_connections)
  661. {
  662. ProcessConnection(connection, slotTypeMask);
  663. }
  664. }
  665. }
  666. void Pass::SetupOutputsFromRequest()
  667. {
  668. if (m_flags.m_createdByPassRequest)
  669. {
  670. const uint32_t slotTypeMask = (1 << uint32_t(PassSlotType::Output));
  671. for (const PassConnection& connection : m_request.m_connections)
  672. {
  673. ProcessConnection(connection, slotTypeMask);
  674. }
  675. }
  676. }
  677. void Pass::SetupPassDependencies()
  678. {
  679. // Get dependencies declared in the PassRequest
  680. if (m_flags.m_createdByPassRequest)
  681. {
  682. for (const Name& passName : m_request.m_executeAfterPasses)
  683. {
  684. Ptr<Pass> executeAfterPass = FindAdjacentPass(passName);
  685. if (executeAfterPass != nullptr)
  686. {
  687. m_executeAfterPasses.push_back(executeAfterPass.get());
  688. }
  689. }
  690. for (const Name& passName : m_request.m_executeBeforePasses)
  691. {
  692. Ptr<Pass> executeBeforePass = FindAdjacentPass(passName);
  693. if (executeBeforePass != nullptr)
  694. {
  695. m_executeBeforePasses.push_back(executeBeforePass.get());
  696. }
  697. }
  698. }
  699. // Inherit dependencies from ParentPass
  700. if (m_parent)
  701. {
  702. for (Pass* pass : m_parent->m_executeAfterPasses)
  703. {
  704. m_executeAfterPasses.push_back(pass);
  705. }
  706. for (Pass* pass : m_parent->m_executeBeforePasses)
  707. {
  708. m_executeBeforePasses.push_back(pass);
  709. }
  710. }
  711. }
  712. void Pass::SetupInputsFromTemplate()
  713. {
  714. if (m_template)
  715. {
  716. const uint32_t slotTypeMask = (1 << uint32_t(PassSlotType::Input)) | (1 << uint32_t(PassSlotType::InputOutput));
  717. for (const PassConnection& outputConnection : m_template->m_connections)
  718. {
  719. ProcessConnection(outputConnection, slotTypeMask);
  720. }
  721. }
  722. }
  723. void Pass::SetupOutputsFromTemplate()
  724. {
  725. if (m_template)
  726. {
  727. const uint32_t slotTypeMask = (1 << uint32_t(PassSlotType::Output));
  728. for (const PassConnection& outputConnection : m_template->m_connections)
  729. {
  730. ProcessConnection(outputConnection, slotTypeMask);
  731. }
  732. for (const PassFallbackConnection& fallbackConnection : m_template->m_fallbackConnections)
  733. {
  734. ProcessFallbackConnection(fallbackConnection);
  735. }
  736. }
  737. }
  738. void Pass::CreateAttachmentsFromTemplate()
  739. {
  740. if (m_template)
  741. {
  742. // Create image attachments
  743. for (const PassImageAttachmentDesc& desc : m_template->m_imageAttachments)
  744. {
  745. m_ownedAttachments.emplace_back(CreateAttachmentFromDesc(desc));
  746. }
  747. // Create buffer attachments
  748. for (const PassBufferAttachmentDesc& desc : m_template->m_bufferAttachments)
  749. {
  750. m_ownedAttachments.emplace_back(CreateAttachmentFromDesc(desc));
  751. }
  752. }
  753. }
  754. void Pass::CreateAttachmentsFromRequest()
  755. {
  756. if (m_flags.m_createdByPassRequest)
  757. {
  758. // Create image attachments
  759. for (const PassImageAttachmentDesc& desc : m_request.m_imageAttachmentOverrides)
  760. {
  761. OverrideOrAddAttachment(desc);
  762. }
  763. // Create buffer attachments
  764. for (const PassBufferAttachmentDesc& desc : m_request.m_bufferAttachmentOverrides)
  765. {
  766. OverrideOrAddAttachment(desc);
  767. }
  768. }
  769. }
  770. // --- Attachment and Binding related functions ---
  771. void Pass::StoreImportedAttachmentReferences()
  772. {
  773. m_importedAttachmentStore.clear();
  774. for (const Ptr<PassAttachment>& attachment : m_ownedAttachments)
  775. {
  776. if (attachment->m_lifetime == RHI::AttachmentLifetimeType::Imported)
  777. {
  778. m_importedAttachmentStore.push_back(attachment);
  779. }
  780. }
  781. }
  782. void Pass::CreateTransientAttachments(RHI::FrameGraphAttachmentInterface attachmentDatabase)
  783. {
  784. for (const Ptr<PassAttachment>& attachment : m_ownedAttachments)
  785. {
  786. if (attachment->m_lifetime == RHI::AttachmentLifetimeType::Transient)
  787. {
  788. switch (attachment->m_descriptor.m_type)
  789. {
  790. case RHI::AttachmentType::Image:
  791. attachmentDatabase.CreateTransientImage(attachment->GetTransientImageDescriptor());
  792. break;
  793. case RHI::AttachmentType::Buffer:
  794. attachmentDatabase.CreateTransientBuffer(attachment->GetTransientBufferDescriptor());
  795. break;
  796. default:
  797. AZ_RPI_PASS_ASSERT(false, "Error, transient attachment is neither an image nor a buffer!");
  798. break;
  799. }
  800. }
  801. }
  802. }
  803. void Pass::ImportAttachments(RHI::FrameGraphAttachmentInterface attachmentDatabase)
  804. {
  805. for (const Ptr<PassAttachment>& attachment : m_ownedAttachments)
  806. {
  807. if (attachment->m_lifetime == RHI::AttachmentLifetimeType::Imported)
  808. {
  809. // make sure to only import the resource one time
  810. RHI::AttachmentId attachmentId = attachment->GetAttachmentId();
  811. const RHI::FrameAttachment* currentAttachment = attachmentDatabase.FindAttachment(attachmentId);
  812. if (azrtti_istypeof<Image>(attachment->m_importedResource.get()))
  813. {
  814. Image* image = static_cast<Image*>(attachment->m_importedResource.get());
  815. if (currentAttachment == nullptr)
  816. {
  817. attachmentDatabase.ImportImage(attachmentId, image->GetRHIImage());
  818. }
  819. else
  820. {
  821. AZ_Assert(currentAttachment->GetResource() == image->GetRHIImage(),
  822. "Importing image attachment named \"%s\" but a different attachment with the "
  823. "same name already exists in the database.\n", attachmentId.GetCStr());
  824. }
  825. }
  826. else if (azrtti_istypeof<Buffer>(attachment->m_importedResource.get()))
  827. {
  828. Buffer* buffer = static_cast<Buffer*>(attachment->m_importedResource.get());
  829. if (currentAttachment == nullptr)
  830. {
  831. attachmentDatabase.ImportBuffer(attachmentId, buffer->GetRHIBuffer());
  832. }
  833. else
  834. {
  835. AZ_Assert(currentAttachment->GetResource() == buffer->GetRHIBuffer(),
  836. "Importing buffer attachment named \"%s\" but a different attachment with the "
  837. "same name already exists in the database.\n", attachmentId.GetCStr());
  838. }
  839. }
  840. else
  841. {
  842. AZ_RPI_PASS_ERROR(false, "Can't import unknown resource type");
  843. }
  844. }
  845. }
  846. }
  847. void Pass::UpdateAttachmentUsageIndices()
  848. {
  849. // We want to find attachments that are used more than once by the same pass
  850. // An example of this could be reading from and writing to different mips of the same texture
  851. // Loop over all attachments bound to this pass
  852. size_t size = m_attachmentBindings.size();
  853. for (size_t i = 0; i < size; ++i)
  854. {
  855. PassAttachmentBinding& binding01 = m_attachmentBindings[i];
  856. // For the outer loop, only consider bindings which are the
  857. // first occurrence of their given attachment in the pass
  858. if (binding01.m_attachmentUsageIndex != 0)
  859. {
  860. continue;
  861. }
  862. // Loop over all subsequent bindings in the pass
  863. uint8_t duplicateCount = 0;
  864. for (size_t j = i + 1; j < size; ++j)
  865. {
  866. PassAttachmentBinding& binding02 = m_attachmentBindings[j];
  867. // Bindings are considered having the same attachment if they are connected to the same binding...
  868. bool haveSameConnection = binding01.m_connectedBinding == binding02.m_connectedBinding;
  869. haveSameConnection = haveSameConnection && binding01.m_connectedBinding != nullptr;
  870. // ... Or if they point to the same attachment
  871. bool isSameAttachment = binding01.GetAttachment() == binding02.GetAttachment();
  872. isSameAttachment = isSameAttachment && binding01.GetAttachment() != nullptr;
  873. // If binding 01 and binding 02 have the same attachment, update the attachment usage index on binding 02
  874. if (haveSameConnection || isSameAttachment)
  875. {
  876. binding02.m_attachmentUsageIndex = ++duplicateCount;
  877. }
  878. }
  879. }
  880. }
  881. void Pass::UpdateOwnedAttachments()
  882. {
  883. // Update the output attachments to coincide with their source attachments (if specified)
  884. // This involves getting the format and calculating the size from the source attachment
  885. for (Ptr<PassAttachment>& attachment: m_ownedAttachments)
  886. {
  887. attachment->Update();
  888. }
  889. }
  890. void Pass::UpdateConnectedBinding(PassAttachmentBinding& binding)
  891. {
  892. bool useFallback = (m_state != PassState::Building && !IsEnabled());
  893. binding.UpdateConnection(useFallback);
  894. }
  895. void Pass::UpdateConnectedBindings()
  896. {
  897. // Depending on whether a pass is enabled or not, it may switch it's bindings to become a pass-through
  898. // For this reason we update connecting bindings on a per-frame basis
  899. for (PassAttachmentBinding& binding : m_attachmentBindings)
  900. {
  901. UpdateConnectedBinding(binding);
  902. }
  903. }
  904. void Pass::UpdateConnectedInputBindings()
  905. {
  906. for (uint8_t idx : m_inputBindingIndices)
  907. {
  908. UpdateConnectedBinding(m_attachmentBindings[idx]);
  909. }
  910. for (uint8_t idx : m_inputOutputBindingIndices)
  911. {
  912. UpdateConnectedBinding(m_attachmentBindings[idx]);
  913. }
  914. }
  915. void Pass::UpdateConnectedOutputBindings()
  916. {
  917. for (uint8_t idx : m_outputBindingIndices)
  918. {
  919. UpdateConnectedBinding(m_attachmentBindings[idx]);
  920. }
  921. }
  922. void Pass::RegisterPipelineGlobalConnections()
  923. {
  924. if (!m_pipeline)
  925. {
  926. AZ_RPI_PASS_ERROR(m_pipelineGlobalConnections.size() == 0,
  927. "Pass::RegisterPipelineGlobalConnections() - PipelineGlobal connections specified but no pipeline set on pass [%s]",
  928. m_path.GetCStr());
  929. }
  930. for (const PipelineGlobalConnection& connection : m_pipelineGlobalConnections)
  931. {
  932. PassAttachmentBinding* binding = FindAttachmentBinding(connection.m_localBinding);
  933. AZ_RPI_PASS_ERROR(binding != nullptr, "Pass::RegisterPipelineGlobalConnections() - Could not find local binding [%s]",
  934. connection.m_localBinding.GetCStr());
  935. if (binding)
  936. {
  937. m_pipeline->AddPipelineGlobalConnection(connection.m_globalName, binding, this);
  938. }
  939. }
  940. m_flags.m_containsGlobalReference = (m_pipelineGlobalConnections.size() > 0);
  941. }
  942. // --- Queuing functions with PassSystem ---
  943. void Pass::QueueForBuildAndInitialization()
  944. {
  945. // Don't queue if we're currently building. Don't queue if we're already queued for Build or Removal
  946. if (m_state != PassState::Building &&
  947. m_queueState != PassQueueState::QueuedForBuildAndInitialization &&
  948. m_queueState != PassQueueState::QueuedForRemoval)
  949. {
  950. // NOTE: We only queue for Build here, the queue for Initialization happens at the end of Pass::Build
  951. // (doing it this way is an optimization to minimize the number of passes queued for initialization,
  952. // as many passes will be initialized by their parent passes and thus don't need to be queued)
  953. PassSystemInterface::Get()->QueueForBuild(this);
  954. m_queueState = PassQueueState::QueuedForBuildAndInitialization;
  955. // Transition state
  956. // If we are Rendering, the state will transition [Rendering -> Queued] in Pass::FrameEnd
  957. // TODO: the PassState::Reset check is a quick fix until the pass concurrency with multiple scenes issue is fixed
  958. if (m_state != PassState::Rendering && m_state != PassState::Reset)
  959. {
  960. m_state = PassState::Queued;
  961. }
  962. }
  963. }
  964. void Pass::QueueForInitialization()
  965. {
  966. // Only queue if the pass is not in any queue. Don't queue if we're currently initializing.
  967. if (m_queueState == PassQueueState::NoQueue && m_state != PassState::Initializing)
  968. {
  969. PassSystemInterface::Get()->QueueForInitialization(this);
  970. m_queueState = PassQueueState::QueuedForInitialization;
  971. // Transition state
  972. // If we are Rendering, the state will transition [Rendering -> Queued] in Pass::FrameEnd
  973. // If the state is Built, preserve the state since [Built -> Initializing] is a valid transition
  974. // Preserving PassState::Built lets the pass ignore subsequent build calls in the same frame
  975. if (m_state != PassState::Rendering && m_state != PassState::Built)
  976. {
  977. m_state = PassState::Queued;
  978. }
  979. }
  980. }
  981. void Pass::QueueForRemoval()
  982. {
  983. // Skip only if we're already queued for removal, otherwise proceed.
  984. // QueuedForRemoval overrides QueuedForBuildAndInitialization and QueuedForInitialization.
  985. if (m_queueState != PassQueueState::QueuedForRemoval)
  986. {
  987. PassSystemInterface::Get()->QueueForRemoval(this);
  988. m_queueState = PassQueueState::QueuedForRemoval;
  989. // Transition state
  990. // If we are Rendering, the state will transition [Rendering -> Queued] in Pass::FrameEnd
  991. if (m_state != PassState::Rendering)
  992. {
  993. m_state = PassState::Queued;
  994. }
  995. }
  996. }
  997. // --- Pass behavior functions ---
  998. void Pass::Reset()
  999. {
  1000. AZ_RPI_BREAK_ON_TARGET_PASS;
  1001. // Ensure we're in a valid state to reset. This ensures the pass won't be reset multiple times in the same frame.
  1002. bool execute = (m_state == PassState::Idle);
  1003. execute = execute || (m_state == PassState::Queued && m_queueState == PassQueueState::QueuedForBuildAndInitialization);
  1004. execute = execute || (m_state == PassState::Queued && m_queueState == PassQueueState::QueuedForInitialization);
  1005. if (!execute)
  1006. {
  1007. return;
  1008. }
  1009. m_state = PassState::Resetting;
  1010. if (m_flags.m_isPipelineRoot)
  1011. {
  1012. m_pipeline->ClearGlobalBindings();
  1013. }
  1014. // Store references to imported attachments to underlying images and buffers aren't deleted during attachment building
  1015. StoreImportedAttachmentReferences();
  1016. // Clear lists
  1017. m_inputBindingIndices.clear();
  1018. m_inputOutputBindingIndices.clear();
  1019. m_outputBindingIndices.clear();
  1020. m_attachmentBindings.clear();
  1021. m_ownedAttachments.clear();
  1022. m_executeAfterPasses.clear();
  1023. m_executeBeforePasses.clear();
  1024. ResetInternal();
  1025. m_state = PassState::Reset;
  1026. }
  1027. void Pass::Build(bool calledFromPassSystem)
  1028. {
  1029. AZ_RPI_BREAK_ON_TARGET_PASS;
  1030. // Ensure we're in a valid state to build. This ensures the pass won't be built multiple times in the same frame.
  1031. bool execute = (m_state == PassState::Reset);
  1032. if (!execute)
  1033. {
  1034. return;
  1035. }
  1036. m_state = PassState::Building;
  1037. // Bindings, inputs and attachments
  1038. CreateBindingsFromTemplate();
  1039. RegisterPipelineGlobalConnections();
  1040. SetupPassDependencies();
  1041. CreateAttachmentsFromTemplate();
  1042. CreateAttachmentsFromRequest();
  1043. SetupInputsFromTemplate();
  1044. SetupInputsFromRequest();
  1045. // Custom pass behavior
  1046. BuildInternal();
  1047. // Outputs
  1048. SetupOutputsFromTemplate();
  1049. SetupOutputsFromRequest();
  1050. // Update
  1051. UpdateConnectedBindings();
  1052. UpdateOwnedAttachments();
  1053. UpdateAttachmentUsageIndices();
  1054. m_state = PassState::Built;
  1055. m_queueState = PassQueueState::NoQueue;
  1056. // If this pass's Build() wasn't called from the Pass System, then it was called by it's parent pass
  1057. // In which case we don't need to queue for initialization because the parent will already be queued
  1058. if (calledFromPassSystem)
  1059. {
  1060. // Queue for Initialization
  1061. QueueForInitialization();
  1062. }
  1063. }
  1064. void Pass::Initialize()
  1065. {
  1066. AZ_RPI_BREAK_ON_TARGET_PASS;
  1067. // Ensure we're in a valid state to initialize. This ensures the pass won't be initialized multiple times in the same frame.
  1068. bool execute = (m_state == PassState::Idle || m_state == PassState::Built);
  1069. execute = execute || (m_state == PassState::Queued && m_queueState == PassQueueState::QueuedForInitialization);
  1070. if (!execute)
  1071. {
  1072. return;
  1073. }
  1074. m_state = PassState::Initializing;
  1075. m_queueState = PassQueueState::NoQueue;
  1076. InitializeInternal();
  1077. // Need to recreate the dest attachment because the source attachment might be changed
  1078. if (!m_attachmentCopy.expired())
  1079. {
  1080. m_attachmentCopy.lock()->InvalidateDestImage();
  1081. }
  1082. m_state = PassState::Initialized;
  1083. }
  1084. void Pass::OnInitializationFinished()
  1085. {
  1086. m_flags.m_alreadyCreatedChildren = false;
  1087. m_importedAttachmentStore.clear();
  1088. OnInitializationFinishedInternal();
  1089. m_state = PassState::Idle;
  1090. }
  1091. void Pass::Validate(PassValidationResults& validationResults)
  1092. {
  1093. if (PassValidation::IsEnabled())
  1094. {
  1095. // Log passes with missing input
  1096. for (uint8_t idx : m_inputBindingIndices)
  1097. {
  1098. if (!m_attachmentBindings[idx].GetAttachment())
  1099. {
  1100. validationResults.m_passesWithMissingInputs.push_back(this);
  1101. break;
  1102. }
  1103. }
  1104. // Log passes with missing input/output
  1105. for (uint8_t idx : m_inputOutputBindingIndices)
  1106. {
  1107. if (!m_attachmentBindings[idx].GetAttachment())
  1108. {
  1109. validationResults.m_passesWithMissingInputOutputs.push_back(this);
  1110. break;
  1111. }
  1112. }
  1113. // Log passes with missing output (note that missing output connections are not considered an error)
  1114. for (uint8_t idx : m_outputBindingIndices)
  1115. {
  1116. if (!m_attachmentBindings[idx].GetAttachment())
  1117. {
  1118. validationResults.m_passesWithMissingOutputs.push_back(this);
  1119. break;
  1120. }
  1121. }
  1122. if (m_errorMessages.size() > 0)
  1123. {
  1124. validationResults.m_passesWithErrors.push_back(this);
  1125. }
  1126. if (m_warningMessages.size() > 0)
  1127. {
  1128. validationResults.m_passesWithWarnings.push_back(this);
  1129. }
  1130. }
  1131. }
  1132. void Pass::FrameBegin(FramePrepareParams params)
  1133. {
  1134. AZ_RPI_BREAK_ON_TARGET_PASS;
  1135. bool earlyOut = !IsEnabled();
  1136. // Skip if this pass is the root of the pipeline and the pipeline is set to not render
  1137. if (m_flags.m_isPipelineRoot)
  1138. {
  1139. AZ_RPI_PASS_ASSERT(m_pipeline != nullptr, "Pass is flagged as a pipeline root but it's pipeline pointer is invalid while trying to render");
  1140. earlyOut = earlyOut || m_pipeline == nullptr || m_pipeline->GetRenderMode() == RenderPipeline::RenderMode::NoRender;
  1141. }
  1142. if (earlyOut)
  1143. {
  1144. UpdateConnectedBindings();
  1145. return;
  1146. }
  1147. AZ_Error("PassSystem", m_state == PassState::Idle,
  1148. "Pass::FrameBegin - Pass [%s] is attempting to render, and should be in the 'Idle' or 'Queued' state, but is in the '%s' state.",
  1149. m_path.GetCStr(), ToString(m_state).data());
  1150. m_state = PassState::Rendering;
  1151. UpdateConnectedInputBindings();
  1152. UpdateOwnedAttachments();
  1153. CreateTransientAttachments(params.m_frameGraphBuilder->GetAttachmentDatabase());
  1154. ImportAttachments(params.m_frameGraphBuilder->GetAttachmentDatabase());
  1155. // readback attachment with input state
  1156. UpdateReadbackAttachment(params, true);
  1157. // FrameBeginInternal needs to be the last function be called in FrameBegin because its implementation expects
  1158. // all the attachments are imported to database (for example, ImageAttachmentPreview)
  1159. FrameBeginInternal(params);
  1160. // readback attachment with output state
  1161. UpdateReadbackAttachment(params, false);
  1162. // update attachment copy for preview
  1163. UpdateAttachmentCopy(params);
  1164. UpdateConnectedOutputBindings();
  1165. }
  1166. void Pass::FrameEnd()
  1167. {
  1168. if (m_state == PassState::Rendering)
  1169. {
  1170. FrameEndInternal();
  1171. m_state = (m_queueState == PassQueueState::NoQueue) ? PassState::Idle : PassState::Queued;
  1172. }
  1173. }
  1174. // --- RenderPipeline, PipelineViewTag and DrawListTag ---
  1175. RHI::DrawListTag Pass::GetDrawListTag() const
  1176. {
  1177. static RHI::DrawListTag invalidTag;
  1178. return invalidTag;
  1179. }
  1180. const PipelineViewTag& Pass::GetPipelineViewTag() const
  1181. {
  1182. if (m_viewTag.IsEmpty())
  1183. {
  1184. if (m_flags.m_isPipelineRoot && m_pipeline)
  1185. {
  1186. return m_pipeline->GetMainViewTag();
  1187. }
  1188. else if (m_parent)
  1189. {
  1190. return m_parent->GetPipelineViewTag();
  1191. }
  1192. }
  1193. return m_viewTag;
  1194. }
  1195. void Pass::SetRenderPipeline(RenderPipeline* pipeline)
  1196. {
  1197. AZ_Assert(!m_pipeline || !pipeline || m_pipeline == pipeline,
  1198. "Switching passes between pipelines is not supported and may result in undefined behavior");
  1199. if (m_pipeline != pipeline)
  1200. {
  1201. m_pipeline = pipeline;
  1202. // Re-queue for new pipeline.
  1203. if (m_pipeline != nullptr)
  1204. {
  1205. PassState currentState = m_state;
  1206. m_queueState = PassQueueState::NoQueue;
  1207. QueueForBuildAndInitialization();
  1208. if (currentState == PassState::Reset)
  1209. {
  1210. m_state = PassState::Reset;
  1211. }
  1212. }
  1213. }
  1214. }
  1215. void Pass::ManualPipelineBuildAndInitialize()
  1216. {
  1217. Build();
  1218. Initialize();
  1219. OnInitializationFinished();
  1220. }
  1221. Scene* Pass::GetScene() const
  1222. {
  1223. if (m_pipeline)
  1224. {
  1225. return m_pipeline->GetScene();
  1226. }
  1227. return nullptr;
  1228. }
  1229. PassTree* Pass::GetPassTree() const
  1230. {
  1231. return m_pipeline ? &(m_pipeline->m_passTree) : nullptr;
  1232. }
  1233. void Pass::GetViewDrawListInfo(RHI::DrawListMask& outDrawListMask, PassesByDrawList& outPassesByDrawList, const PipelineViewTag& viewTag) const
  1234. {
  1235. // NOTE: we always collect the draw list mask regardless if the pass enabled or not. The reason is we want to keep the view information
  1236. // even when pass is disabled so it can continue work correctly when re-enable it.
  1237. // Only get the DrawListTag if this pass has a DrawListTag and it's PipelineViewId matches
  1238. if (BindViewSrg() && HasDrawListTag() && GetPipelineViewTag() == viewTag)
  1239. {
  1240. RHI::DrawListTag drawListTag = GetDrawListTag();
  1241. if (drawListTag.IsValid() && outPassesByDrawList.find(drawListTag) == outPassesByDrawList.end())
  1242. {
  1243. outPassesByDrawList[drawListTag] = this;
  1244. outDrawListMask.set(drawListTag.GetIndex());
  1245. }
  1246. }
  1247. }
  1248. void Pass::GetPipelineViewTags(PipelineViewTags& outTags) const
  1249. {
  1250. if (BindViewSrg())
  1251. {
  1252. outTags.insert(GetPipelineViewTag());
  1253. }
  1254. }
  1255. void Pass::SortDrawList(RHI::DrawList& drawList) const
  1256. {
  1257. if (!drawList.empty())
  1258. {
  1259. RHI::SortDrawList(drawList, m_drawListSortType);
  1260. }
  1261. }
  1262. // --- Debug & Validation functions ---
  1263. bool PassValidationResults::IsValid()
  1264. {
  1265. if (PassValidation::IsEnabled())
  1266. {
  1267. // Pass validation fail if there are any passes with build errors or missing inputs (or input/outputs)
  1268. return (m_passesWithErrors.size() == 0) && (m_passesWithMissingInputs.size() == 0) && (m_passesWithMissingInputOutputs.size() == 0);
  1269. }
  1270. else
  1271. {
  1272. return true;
  1273. }
  1274. }
  1275. TimestampResult Pass::GetLatestTimestampResult() const
  1276. {
  1277. return GetTimestampResultInternal();
  1278. }
  1279. PipelineStatisticsResult Pass::GetLatestPipelineStatisticsResult() const
  1280. {
  1281. return GetPipelineStatisticsResultInternal();
  1282. }
  1283. bool Pass::ReadbackAttachment(AZStd::shared_ptr<AttachmentReadback> readback, uint32_t readbackIndex, const Name& slotName
  1284. , PassAttachmentReadbackOption option, const RHI::ImageSubresourceRange* mipsRange)
  1285. {
  1286. // Return false if it's already readback
  1287. if (m_attachmentReadback)
  1288. {
  1289. AZ_Warning("Pass", false, "ReadbackAttachment: skip readback pass [%s] slot [%s] because there is an another active readback", m_path.GetCStr(), slotName.GetCStr());
  1290. return false;
  1291. }
  1292. uint32_t bindingIndex = 0;
  1293. for (auto& binding : m_attachmentBindings)
  1294. {
  1295. if (slotName == binding.m_name)
  1296. {
  1297. RHI::AttachmentType type = binding.GetAttachment()->GetAttachmentType();
  1298. if (type == RHI::AttachmentType::Buffer || type == RHI::AttachmentType::Image)
  1299. {
  1300. RHI::AttachmentId attachmentId = binding.GetAttachment()->GetAttachmentId();
  1301. // Append slot index and pass name so the read back's name won't be same as the attachment used in other passes.
  1302. AZStd::string readbackName = AZStd::string::format("%s_%d_%d_%s", attachmentId.GetCStr(),
  1303. readbackIndex, bindingIndex, GetName().GetCStr());
  1304. if (readback->ReadPassAttachment(binding.GetAttachment().get(), AZ::Name(readbackName), mipsRange))
  1305. {
  1306. m_readbackOption = PassAttachmentReadbackOption::Output;
  1307. // The m_readbackOption is only meaningful if the attachment is used for InputOutput.
  1308. if (binding.m_slotType == PassSlotType::InputOutput)
  1309. {
  1310. m_readbackOption = option;
  1311. }
  1312. m_attachmentReadback = readback;
  1313. return true;
  1314. }
  1315. return false;
  1316. }
  1317. }
  1318. bindingIndex++;
  1319. }
  1320. AZ_Warning("Pass", false, "ReadbackAttachment: failed to find slot [%s] from pass [%s]", slotName.GetCStr(), m_path.GetCStr());
  1321. return false;
  1322. }
  1323. void Pass::UpdateReadbackAttachment(FramePrepareParams params, bool beforeAddScopes)
  1324. {
  1325. if (beforeAddScopes == (m_readbackOption == PassAttachmentReadbackOption::Input) && m_attachmentReadback)
  1326. {
  1327. // Read the attachment for one frame. The reference can be released afterwards
  1328. m_attachmentReadback->FrameBegin(params);
  1329. m_attachmentReadback = nullptr;
  1330. }
  1331. }
  1332. void Pass::UpdateAttachmentCopy(FramePrepareParams params)
  1333. {
  1334. if (!m_attachmentCopy.expired())
  1335. {
  1336. m_attachmentCopy.lock()->FrameBegin(params);
  1337. }
  1338. }
  1339. void Pass::PrintIndent(AZStd::string& stringOutput, uint32_t indent) const
  1340. {
  1341. if (PassValidation::IsEnabled())
  1342. {
  1343. for (uint32_t i = 0; i < indent; ++i)
  1344. {
  1345. stringOutput += " ";
  1346. }
  1347. }
  1348. }
  1349. void Pass::PrintPassName(AZStd::string& stringOutput, uint32_t indent) const
  1350. {
  1351. if (PassValidation::IsEnabled())
  1352. {
  1353. stringOutput += "\n";
  1354. PrintIndent(stringOutput, indent);
  1355. stringOutput += "- ";
  1356. //stringOutput += m_name.GetStringView();
  1357. //stringOutput += "- ";
  1358. stringOutput += m_path.GetStringView();
  1359. stringOutput += "\n";
  1360. }
  1361. }
  1362. void Pass::PrintErrors() const
  1363. {
  1364. if (PassValidation::IsEnabled())
  1365. {
  1366. PrintMessages(m_errorMessages);
  1367. }
  1368. }
  1369. void Pass::PrintWarnings() const
  1370. {
  1371. if (PassValidation::IsEnabled())
  1372. {
  1373. PrintMessages(m_warningMessages);
  1374. }
  1375. }
  1376. void Pass::PrintMessages(const AZStd::vector<AZStd::string>& messages) const
  1377. {
  1378. if (PassValidation::IsEnabled())
  1379. {
  1380. AZStd::string stringOutput;
  1381. PrintPassName(stringOutput);
  1382. for (const AZStd::string& message : messages)
  1383. {
  1384. PrintIndent(stringOutput, 1);
  1385. stringOutput += message;
  1386. stringOutput += "\n";
  1387. }
  1388. AZ_Printf("PassSystem", stringOutput.c_str());
  1389. }
  1390. }
  1391. void Pass::PrintBindingsWithoutAttachments(uint32_t slotTypeMask) const
  1392. {
  1393. if (PassValidation::IsEnabled())
  1394. {
  1395. AZStd::string stringOutput;
  1396. PrintPassName(stringOutput);
  1397. for (const PassAttachmentBinding& binding : m_attachmentBindings)
  1398. {
  1399. uint32_t bindingMask = (1 << uint32_t(binding.m_slotType));
  1400. if ((bindingMask & slotTypeMask) && (binding.GetAttachment() == nullptr))
  1401. {
  1402. // Print the name of the slot
  1403. PrintIndent(stringOutput, 1);
  1404. stringOutput += binding.m_name.GetStringView();
  1405. stringOutput += " has no valid attachment\n";
  1406. }
  1407. }
  1408. AZ_Printf("PassSystem", stringOutput.c_str());
  1409. }
  1410. }
  1411. void Pass::DebugPrintBinding(AZStd::string& stringOutput, const PassAttachmentBinding& binding) const
  1412. {
  1413. if (PassValidation::IsEnabled())
  1414. {
  1415. // Print the name of the slot
  1416. stringOutput += binding.m_name.GetStringView();
  1417. // Print the attachment type and size, for example:
  1418. // (Image, 1920, 1080) or (Buffer, 4096 bytes)
  1419. if (binding.GetAttachment() != nullptr)
  1420. {
  1421. stringOutput += " (";
  1422. // Images will have the format: AttachmentName (Image, 1920, 1080)
  1423. if (binding.GetAttachment()->m_descriptor.m_type == RHI::AttachmentType::Image)
  1424. {
  1425. stringOutput += "Image";
  1426. RHI::ImageDescriptor& desc = binding.GetAttachment()->m_descriptor.m_image;
  1427. uint32_t dimensions = static_cast<uint32_t>(desc.m_dimension);
  1428. for (uint32_t i = 0; i < dimensions; ++i)
  1429. {
  1430. stringOutput += ", ";
  1431. stringOutput += AZStd::to_string(desc.m_size[i]);
  1432. }
  1433. if (desc.m_multisampleState.m_samples > 1)
  1434. {
  1435. if (desc.m_multisampleState.m_customPositionsCount > 0)
  1436. {
  1437. stringOutput += ", Custom_MSAA_";
  1438. }
  1439. else
  1440. {
  1441. stringOutput += ", MSAA_";
  1442. }
  1443. stringOutput += AZStd::to_string(desc.m_multisampleState.m_samples);
  1444. stringOutput += "x";
  1445. }
  1446. }
  1447. // Buffers will have the format: AttachmentName (Buffer, 4092 bytes)
  1448. else if (binding.GetAttachment()->m_descriptor.m_type == RHI::AttachmentType::Buffer)
  1449. {
  1450. stringOutput += "Buffer, ";
  1451. stringOutput += AZStd::to_string(binding.GetAttachment()->m_descriptor.m_buffer.m_byteCount);
  1452. stringOutput += " bytes";
  1453. }
  1454. stringOutput += ")";
  1455. }
  1456. }
  1457. }
  1458. void Pass::DebugPrintBindingAndConnection(AZStd::string& stringOutput, uint8_t bindingIndex) const
  1459. {
  1460. if (PassValidation::IsEnabled())
  1461. {
  1462. PrintIndent(stringOutput, m_treeDepth + 2);
  1463. // Print the Attachment
  1464. const PassAttachmentBinding& binding = m_attachmentBindings[bindingIndex];
  1465. DebugPrintBinding(stringOutput, binding);
  1466. // Print the Attachment it's connected to
  1467. if (binding.m_connectedBinding != nullptr)
  1468. {
  1469. stringOutput += " connected to ";
  1470. DebugPrintBinding(stringOutput, *binding.m_connectedBinding);
  1471. }
  1472. stringOutput += "\n";
  1473. }
  1474. }
  1475. void Pass::DebugPrint() const
  1476. {
  1477. if (PassValidation::IsEnabled())
  1478. {
  1479. AZStd::string stringOutput;
  1480. PrintPassName(stringOutput, m_treeDepth);
  1481. // Print inputs
  1482. if (m_inputBindingIndices.size() > 0)
  1483. {
  1484. PrintIndent(stringOutput, m_treeDepth + 1);
  1485. stringOutput += "Inputs:\n";
  1486. for (uint8_t inputIndex : m_inputBindingIndices)
  1487. {
  1488. DebugPrintBindingAndConnection(stringOutput, inputIndex);
  1489. }
  1490. }
  1491. // Print input/outputs
  1492. if (m_inputOutputBindingIndices.size() > 0)
  1493. {
  1494. PrintIndent(stringOutput, m_treeDepth + 1);
  1495. stringOutput += "Input/Outputs:\n";
  1496. for (uint8_t inputIndex : m_inputOutputBindingIndices)
  1497. {
  1498. DebugPrintBindingAndConnection(stringOutput, inputIndex);
  1499. }
  1500. }
  1501. // Print outputs
  1502. if (m_outputBindingIndices.size() > 0)
  1503. {
  1504. PrintIndent(stringOutput, m_treeDepth + 1);
  1505. stringOutput += "Outputs:\n";
  1506. for (uint8_t inputIndex : m_outputBindingIndices)
  1507. {
  1508. DebugPrintBindingAndConnection(stringOutput, inputIndex);
  1509. }
  1510. }
  1511. AZ_Printf("PassSystem", stringOutput.c_str());
  1512. }
  1513. }
  1514. void PassValidationResults::PrintValidationIfError()
  1515. {
  1516. if (PassValidation::IsEnabled())
  1517. {
  1518. if (!IsValid())
  1519. {
  1520. AZ_Printf("PassSystem", "\n--- PASS VALIDATION FAILURE ---"
  1521. "\n--Critical Errors--\n");
  1522. AZ_Printf("PassSystem", "\nThere are %d passes with errors:\n", m_passesWithErrors.size());
  1523. for (Pass* pass : m_passesWithErrors)
  1524. {
  1525. pass->PrintErrors();
  1526. }
  1527. AZ_Printf("PassSystem", "\nThere are %d passes with missing Inputs:\n", m_passesWithMissingInputs.size());
  1528. for (Pass* pass : m_passesWithMissingInputs)
  1529. {
  1530. pass->PrintBindingsWithoutAttachments(uint32_t(PassSlotMask::Input));
  1531. }
  1532. AZ_Printf("PassSystem", "\nThere are %d passes with missing Inputs/Outputs:\n", m_passesWithMissingInputOutputs.size());
  1533. for (Pass* pass : m_passesWithMissingInputOutputs)
  1534. {
  1535. pass->PrintBindingsWithoutAttachments(uint32_t(PassSlotMask::InputOutput));
  1536. }
  1537. AZ_Printf("PassSystem", "\n--Non-Critical Errors/Warnings--\n");
  1538. AZ_Printf("PassSystem", "\nThere are %d passes with warnings:\n", m_passesWithWarnings.size());
  1539. for (Pass* pass : m_passesWithWarnings)
  1540. {
  1541. pass->PrintWarnings();
  1542. }
  1543. }
  1544. }
  1545. }
  1546. } // namespace RPI
  1547. } // namespace AZ