3
0

LyShineFeatureProcessor.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <./LyShineFeatureProcessor.h>
  9. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  10. #include <Atom/RPI.Public/Scene.h>
  11. #include <Atom/RPI.Public/Pass/PassFilter.h>
  12. #include <Atom/RPI.Public/Pass/PassSystemInterface.h>
  13. #include <Atom/RPI.Public/Pass/ParentPass.h>
  14. #include <Atom/RPI.Public/RenderPipeline.h>
  15. namespace LyShine
  16. {
  17. void LyShineFeatureProcessor::Reflect(AZ::ReflectContext* context)
  18. {
  19. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  20. {
  21. serialize->Class<LyShineFeatureProcessor, AZ::RPI::FeatureProcessor>()
  22. ->Version(0)
  23. ;
  24. }
  25. }
  26. void LyShineFeatureProcessor::AddRenderPasses(AZ::RPI::RenderPipeline* renderPipeline)
  27. {
  28. // Only add LyShineParentPass if UIPass exists
  29. if (!renderPipeline->FindFirstPass(AZ::Name("UIPass")))
  30. {
  31. return;
  32. }
  33. // Get the pass request if it's not loaded
  34. if (!m_passRequestAsset)
  35. {
  36. const char* passRequestAssetFilePath = "Passes/LyShinePassRequest.azasset";
  37. m_passRequestAsset = AZ::RPI::AssetUtils::LoadAssetByProductPath<AZ::RPI::AnyAsset>(
  38. passRequestAssetFilePath, AZ::RPI::AssetUtils::TraceLevel::Warning);
  39. }
  40. const AZ::RPI::PassRequest *passRequest = nullptr;
  41. if (m_passRequestAsset->IsReady())
  42. {
  43. passRequest = m_passRequestAsset->GetDataAs<AZ::RPI::PassRequest>();
  44. }
  45. if (!passRequest)
  46. {
  47. AZ_Error("LyShine", false, "Failed to add LyShine parent pass. Can't load PassRequest from %s", m_passRequestAsset.GetHint().c_str());
  48. return;
  49. }
  50. AZ::RPI::PassRequest passRequestCopy = *passRequest;
  51. //TODO::This is not the best solution and require a better long term solution.
  52. //Check if DepthPrePass is part of the pipeline. This is possible for any pipelines built for tbdr gpus.
  53. if (!renderPipeline->FindFirstPass(AZ::Name("DepthPrePass")))
  54. {
  55. //Check if ForwardPass is available
  56. if (renderPipeline->FindFirstPass(AZ::Name("ForwardPass")))
  57. {
  58. // Find the depth attachment and hook that up via ForwardPass
  59. const AZ::Name depthAttachment = AZ::Name("DepthInputOutput");
  60. auto findIter = AZStd::find_if(passRequestCopy.m_connections.begin(), passRequestCopy.m_connections.end(), [depthAttachment](const AZ::RPI::PassConnection& entry)
  61. {
  62. return entry.m_localSlot == depthAttachment;
  63. });
  64. if (findIter != passRequestCopy.m_connections.end())
  65. {
  66. (*findIter).m_attachmentRef.m_pass = "ForwardPass";
  67. (*findIter).m_attachmentRef.m_attachment = "DepthStencilOutput";
  68. }
  69. }
  70. passRequest = &passRequestCopy;
  71. }
  72. // Return if the pass to be created already exists
  73. AZ::RPI::PassFilter passFilter = AZ::RPI::PassFilter::CreateWithPassName(passRequest->m_passName, renderPipeline);
  74. AZ::RPI::Pass* pass = AZ::RPI::PassSystemInterface::Get()->FindFirstPass(passFilter);
  75. if (pass)
  76. {
  77. return;
  78. }
  79. // Create the pass
  80. AZ::RPI::Ptr<AZ::RPI::Pass> lyShineParentPass = AZ::RPI::PassSystemInterface::Get()->CreatePassFromRequest(passRequest);
  81. if (!lyShineParentPass)
  82. {
  83. AZ_Error("LyShine", false, "Create LyShine parent pass from pass request failed");
  84. return;
  85. }
  86. // Insert the LyShineParentPass before UIPass
  87. bool success = renderPipeline->AddPassBefore(lyShineParentPass, AZ::Name("UIPass"));
  88. // only create pass resources if it was success
  89. if (!success)
  90. {
  91. AZ_Error("LyShine", false, "Add the LyShine parent pass to render pipeline [%s] failed",
  92. renderPipeline->GetId().GetCStr());
  93. }
  94. }
  95. }