3
0

RenderPipelineTests.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Atom/RPI.Public/Base.h>
  9. #include <Atom/RPI.Public/RenderPipeline.h>
  10. #include <Atom/RPI.Public/View.h>
  11. #include <AzTest/AzTest.h>
  12. #include <Common/RPITestFixture.h>
  13. #include <Common/SerializeTester.h>
  14. #include <Common/ErrorMessageFinder.h>
  15. namespace UnitTest
  16. {
  17. using namespace AZ;
  18. class RenderPipelineTests
  19. : public RPITestFixture
  20. {
  21. protected:
  22. // A test pass which only include view tag and draw list tag
  23. class TestPass
  24. : public AZ::RPI::ParentPass
  25. {
  26. public:
  27. AZ_RTTI(TestPass, "{2056532E-286F-454F-8659-15A289432A63}", AZ::RPI::ParentPass);
  28. AZ_CLASS_ALLOCATOR(TestPass, AZ::SystemAllocator);
  29. TestPass(const AZ::RPI::PassDescriptor& descriptor)
  30. : AZ::RPI::ParentPass(descriptor)
  31. { }
  32. void Initialize(const Name& drawListTagString, const AZ::RPI::PipelineViewTag& viewTag)
  33. {
  34. RHI::DrawListTagRegistry* drawListTagRegistry = RHI::RHISystemInterface::Get()->GetDrawListTagRegistry();
  35. m_drawListTag = drawListTagRegistry->AcquireTag(drawListTagString);
  36. m_viewTag = viewTag;
  37. m_flags.m_hasDrawListTag = true;
  38. m_flags.m_bindViewSrg = true;
  39. }
  40. AZ::RHI::DrawListTag GetDrawListTag() const override
  41. {
  42. return m_drawListTag;
  43. }
  44. const AZ::RPI::PipelineViewTag& GetPipelineViewTag() const override
  45. {
  46. return m_viewTag;
  47. }
  48. virtual ~TestPass()
  49. {
  50. if (m_drawListTag.IsValid())
  51. {
  52. RHI::DrawListTagRegistry* drawListTagRegistry = RHI::RHISystemInterface::Get()->GetDrawListTagRegistry();
  53. drawListTagRegistry->ReleaseTag(m_drawListTag);
  54. }
  55. }
  56. private:
  57. AZ::RHI::DrawListTag m_drawListTag;
  58. AZ::RPI::PipelineViewTag m_viewTag;
  59. };
  60. };
  61. /**
  62. * Validate view and view tag related functions
  63. */
  64. TEST_F(RenderPipelineTests, ViewFunctionsTest)
  65. {
  66. using namespace AZ;
  67. RHI::DrawListTagRegistry* drawListTagRegistry = RHI::RHISystemInterface::Get()->GetDrawListTagRegistry();
  68. const RPI::PipelineViewTag viewTag1{ "viewTag1" };
  69. const RPI::PipelineViewTag viewTag2{ "viewTag2" };
  70. const RPI::PipelineViewTag viewTag3{ "viewTag3" };
  71. const Name drawListTagString1{ "drawListTag1" };
  72. const Name drawListTagString2{ "drawListTag2" };
  73. const Name drawListTagString3{ "drawListTag3" };
  74. // Create render pipeline
  75. RPI::RenderPipelineDescriptor desc;
  76. desc.m_mainViewTagName = viewTag2.GetStringView();
  77. desc.m_name = "TestPipeline";
  78. RPI::RenderPipelinePtr pipeline = RPI::RenderPipeline::CreateRenderPipeline(desc);
  79. auto& rootPass = pipeline->GetRootPass();
  80. AZ::RPI::PassDescriptor passDescriptor;
  81. passDescriptor.m_passName = "TestPassA";
  82. RPI::Ptr<TestPass> testPassA = aznew TestPass(passDescriptor);
  83. testPassA->Initialize(drawListTagString1, viewTag1);
  84. passDescriptor.m_passName = "TestPassB";
  85. RPI::Ptr<TestPass> testPassB = aznew TestPass(passDescriptor);
  86. testPassB->Initialize(drawListTagString1, viewTag2);
  87. passDescriptor.m_passName = "TestPassC";
  88. RPI::Ptr<TestPass> testPassC = aznew TestPass(passDescriptor);
  89. testPassC->Initialize(drawListTagString2, viewTag2);
  90. passDescriptor.m_passName = "TestPassD";
  91. RPI::Ptr<TestPass> testPassD = aznew TestPass(passDescriptor);
  92. testPassD->Initialize(drawListTagString3, viewTag3);
  93. EXPECT_TRUE(testPassA->HasDrawListTag());
  94. EXPECT_TRUE(testPassB->HasDrawListTag());
  95. EXPECT_TRUE(testPassC->HasDrawListTag());
  96. EXPECT_TRUE(testPassD->HasDrawListTag());
  97. RHI::DrawListTag drawListTag1 = drawListTagRegistry->FindTag(drawListTagString1);
  98. RHI::DrawListTag drawListTag2 = drawListTagRegistry->FindTag(drawListTagString2);
  99. RHI::DrawListTag drawListTag3 = drawListTagRegistry->FindTag(drawListTagString3);
  100. EXPECT_TRUE(testPassA->GetDrawListTag() == drawListTag1);
  101. EXPECT_TRUE(testPassB->GetDrawListTag() == drawListTag1);
  102. EXPECT_TRUE(testPassC->GetDrawListTag() == drawListTag2);
  103. EXPECT_TRUE(testPassD->GetDrawListTag() == drawListTag3);
  104. bool skipStateCheckWhenRunningTests = true;
  105. rootPass->AddChild(testPassA, skipStateCheckWhenRunningTests);
  106. testPassA->AddChild(testPassB, skipStateCheckWhenRunningTests);
  107. testPassA->AddChild(testPassC, skipStateCheckWhenRunningTests);
  108. testPassA->AddChild(testPassD, skipStateCheckWhenRunningTests);
  109. pipeline->UpdatePasses();
  110. EXPECT_TRUE(pipeline->HasViewTag(viewTag1));
  111. auto& views = pipeline->GetViews(viewTag2);
  112. EXPECT_TRUE(views.size() == 0);
  113. RHI::DrawListMask drawListMask;
  114. RPI::PassesByDrawList passesByDrawList;
  115. // viewTag1 only associates with drawListTag1
  116. rootPass->GetViewDrawListInfo(drawListMask, passesByDrawList, viewTag1);
  117. EXPECT_TRUE(drawListMask[drawListTag1.GetIndex()] == true);
  118. EXPECT_TRUE(drawListMask[drawListTag2.GetIndex()] == false);
  119. rootPass->GetViewDrawListInfo(drawListMask, passesByDrawList, viewTag2);
  120. // viewTag2 associates with drawlistTag1 and drawListTag2
  121. EXPECT_TRUE(drawListMask[drawListTag1.GetIndex()] == true);
  122. EXPECT_TRUE(drawListMask[drawListTag2.GetIndex()] == true);
  123. // View functions
  124. RPI::ViewPtr view1 = RPI::View::CreateView(AZ::Name("testViewA"), RPI::View::UsageCamera);
  125. RPI::ViewPtr view2 = RPI::View::CreateView(AZ::Name("testViewB"), RPI::View::UsageCamera);
  126. RPI::ViewPtr view3 = RPI::View::CreateView(AZ::Name("testViewC"), RPI::View::UsageCamera);
  127. // Persistent view
  128. pipeline->SetPersistentView(viewTag1, view1);
  129. EXPECT_TRUE(pipeline->GetViews(viewTag1).size() == 1);
  130. // Replace persistent view
  131. pipeline->SetPersistentView(viewTag1, view2);
  132. auto& viewsFromTag1 = pipeline->GetViews(viewTag1);
  133. EXPECT_TRUE(viewsFromTag1.size() == 1);
  134. EXPECT_TRUE(view2 == viewsFromTag1[0]);
  135. // Try to add a transient view to view tag was associated with persistent view.
  136. AZ_TEST_START_ASSERTTEST;
  137. pipeline->AddTransientView(viewTag1, view1);
  138. AZ_TEST_STOP_ASSERTTEST(1);
  139. EXPECT_TRUE(pipeline->GetViews(viewTag1).size() == 1);
  140. // Try to register a view with multiple viewTags, persistent or transient
  141. AZ_TEST_START_ASSERTTEST;
  142. pipeline->SetPersistentView(viewTag3, view2);
  143. AZ_TEST_STOP_ASSERTTEST(1);
  144. EXPECT_TRUE(pipeline->GetViews(viewTag3).size() == 0);
  145. AZ_TEST_START_ASSERTTEST;
  146. pipeline->AddTransientView(viewTag2, view2);
  147. AZ_TEST_STOP_ASSERTTEST(1);
  148. EXPECT_TRUE(pipeline->GetViews(viewTag2).size() == 0);
  149. // overwrite persistent view 2 with view 3;
  150. pipeline->SetPersistentView(viewTag1, view3);
  151. auto& viewsFromTag1_2 = pipeline->GetViews(viewTag1);
  152. EXPECT_TRUE(viewsFromTag1_2.size() == 1);
  153. EXPECT_TRUE(view3 == viewsFromTag1_2[0]);
  154. // Transient view
  155. pipeline->AddTransientView(viewTag2, view1);
  156. pipeline->AddTransientView(viewTag2, view2);
  157. auto& viewsFromTag2 = pipeline->GetViews(viewTag2);
  158. EXPECT_TRUE(viewsFromTag2.size() == 2);
  159. }
  160. } // namespace UnitTest