3
0

Pass.cpp 74 KB

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