3
0

PipelineStateTests.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 "RHITestFixture.h"
  9. #include <Tests/Factory.h>
  10. #include <Tests/Device.h>
  11. #include <Tests/ThreadTester.h>
  12. #include <Atom/RHI/PipelineStateCache.h>
  13. #include <Atom/RHI.Reflect/PipelineLayoutDescriptor.h>
  14. #include <AzCore/Math/Random.h>
  15. namespace UnitTest
  16. {
  17. using namespace AZ;
  18. class PipelineStateTests
  19. : public RHITestFixture
  20. {
  21. static const uint32_t s_heapSizeMB = 64;
  22. protected:
  23. void ScrambleMemory(void* memory, size_t size, uint32_t seed)
  24. {
  25. SimpleLcgRandom random(seed);
  26. uint8_t* bytes = reinterpret_cast<uint8_t*>(memory);
  27. for (size_t i = 0; i < size; ++i)
  28. {
  29. bytes[i] = static_cast<uint8_t>(random.GetRandom());
  30. }
  31. }
  32. // Generates random render state. Everything else is left empty or default as much as possible,
  33. // but we do touch-up the data to make sure we don't end up with something that will fail assertions.
  34. // The point here is to create a unique descriptor that will have a unique hash value.
  35. RHI::PipelineStateDescriptorForDraw CreatePipelineStateDescriptor(uint32_t randomSeed)
  36. {
  37. RHI::PipelineStateDescriptorForDraw desc;
  38. desc.m_inputStreamLayout.SetTopology(RHI::PrimitiveTopology::TriangleList);
  39. desc.m_inputStreamLayout.Finalize();
  40. desc.m_pipelineLayoutDescriptor = m_pipelineLayout;
  41. ScrambleMemory(&desc.m_renderStates, sizeof(RHI::RenderStates), randomSeed++);
  42. desc.m_renderStates.m_depthStencilState.m_depth.m_enable = true;
  43. auto& renderAttachmentLayout = desc.m_renderAttachmentConfiguration.m_renderAttachmentLayout;
  44. renderAttachmentLayout.m_attachmentCount = 2;
  45. renderAttachmentLayout.m_attachmentFormats[0] = RHI::Format::R32_FLOAT;
  46. renderAttachmentLayout.m_attachmentFormats[1] = RHI::Format::R8G8B8A8_SNORM;
  47. renderAttachmentLayout.m_subpassCount = 1;
  48. renderAttachmentLayout.m_subpassLayouts[0].m_rendertargetCount = 1;
  49. renderAttachmentLayout.m_subpassLayouts[0].m_rendertargetDescriptors[0] = RHI::RenderAttachmentDescriptor{ 1, RHI::InvalidRenderAttachmentIndex, RHI::AttachmentLoadStoreAction() };
  50. renderAttachmentLayout.m_subpassLayouts[0].m_depthStencilDescriptor = RHI::RenderAttachmentDescriptor{ 0, RHI::InvalidRenderAttachmentIndex, RHI::AttachmentLoadStoreAction() };
  51. desc.m_renderAttachmentConfiguration.m_subpassIndex = 0;
  52. return desc;
  53. }
  54. void ValidateCacheIntegrity(RHI::Ptr<RHI::PipelineStateCache>& cache) const
  55. {
  56. cache->ValidateCacheIntegrity();
  57. }
  58. private:
  59. void SetUp() override
  60. {
  61. RHITestFixture::SetUp();
  62. m_factory.reset(aznew Factory());
  63. m_pipelineLayout = RHI::PipelineLayoutDescriptor::Create();
  64. m_pipelineLayout->Finalize();
  65. }
  66. void TearDown() override
  67. {
  68. m_pipelineLayout = nullptr;
  69. m_factory.reset();
  70. RHITestFixture::TearDown();
  71. }
  72. RHI::Ptr<RHI::PipelineLayoutDescriptor> m_pipelineLayout;
  73. AZStd::unique_ptr<Factory> m_factory;
  74. };
  75. TEST_F(PipelineStateTests, PipelineState_CreateEmpty_Test)
  76. {
  77. RHI::Ptr<RHI::DevicePipelineState> empty = RHI::Factory::Get().CreatePipelineState();
  78. EXPECT_NE(empty.get(), nullptr);
  79. EXPECT_FALSE(empty->IsInitialized());
  80. }
  81. TEST_F(PipelineStateTests, PipelineState_Init_Test)
  82. {
  83. RHI::Ptr<RHI::Device> device = MakeTestDevice();
  84. RHI::Ptr<RHI::DevicePipelineState> pipelineState = RHI::Factory::Get().CreatePipelineState();
  85. RHI::ResultCode resultCode = pipelineState->Init(*device, CreatePipelineStateDescriptor(0));
  86. EXPECT_EQ(resultCode, RHI::ResultCode::Success);
  87. // Second init should fail and throw validation error.
  88. AZ_TEST_START_ASSERTTEST;
  89. resultCode = pipelineState->Init(*device, CreatePipelineStateDescriptor(0));
  90. AZ_TEST_STOP_ASSERTTEST(1);
  91. EXPECT_EQ(resultCode, RHI::ResultCode::InvalidOperation);
  92. }
  93. TEST_F(PipelineStateTests, PipelineState_Init_Subpass)
  94. {
  95. RHI::Ptr<RHI::Device> device = MakeTestDevice();
  96. RHI::Ptr<RHI::DevicePipelineState> pipelineState = RHI::Factory::Get().CreatePipelineState();
  97. auto descriptor = CreatePipelineStateDescriptor(0);
  98. descriptor.m_renderAttachmentConfiguration.m_subpassIndex = 1337;
  99. AZ_TEST_START_ASSERTTEST;
  100. RHI::ResultCode resultCode = pipelineState->Init(*device, descriptor);
  101. AZ_TEST_STOP_ASSERTTEST(1);
  102. EXPECT_EQ(resultCode, RHI::ResultCode::InvalidOperation);
  103. }
  104. TEST_F(PipelineStateTests, PipelineState_Init_SubpassInput)
  105. {
  106. RHI::Ptr<RHI::Device> device = MakeTestDevice();
  107. RHI::Ptr<RHI::DevicePipelineState> pipelineState = RHI::Factory::Get().CreatePipelineState();
  108. auto descriptor = CreatePipelineStateDescriptor(0);
  109. descriptor.m_renderAttachmentConfiguration.m_renderAttachmentLayout.m_subpassLayouts[0].m_subpassInputCount = 1;
  110. descriptor.m_renderAttachmentConfiguration.m_renderAttachmentLayout.m_subpassLayouts[0].m_subpassInputDescriptors[0].m_attachmentIndex = 1;
  111. RHI::ResultCode resultCode = pipelineState->Init(*device, descriptor);
  112. EXPECT_EQ(resultCode, RHI::ResultCode::Success);
  113. AZ_TEST_START_ASSERTTEST;
  114. pipelineState = RHI::Factory::Get().CreatePipelineState();
  115. descriptor.m_renderAttachmentConfiguration.m_renderAttachmentLayout.m_subpassLayouts[0]
  116. .m_subpassInputDescriptors[0]
  117. .m_attachmentIndex = 3;
  118. resultCode = pipelineState->Init(*device, descriptor);
  119. AZ_TEST_STOP_ASSERTTEST(1);
  120. EXPECT_EQ(resultCode, RHI::ResultCode::InvalidOperation);
  121. }
  122. TEST_F(PipelineStateTests, PipelineState_Init_Resolve)
  123. {
  124. RHI::Ptr<RHI::Device> device = MakeTestDevice();
  125. RHI::Ptr<RHI::DevicePipelineState> pipelineState = RHI::Factory::Get().CreatePipelineState();
  126. auto descriptor = CreatePipelineStateDescriptor(0);
  127. descriptor.m_renderAttachmentConfiguration.m_renderAttachmentLayout.m_subpassLayouts[0].m_rendertargetDescriptors[0].m_resolveAttachmentIndex = 1;
  128. RHI::ResultCode resultCode = pipelineState->Init(*device, descriptor);
  129. EXPECT_EQ(resultCode, RHI::ResultCode::Success);
  130. AZ_TEST_START_ASSERTTEST;
  131. pipelineState = RHI::Factory::Get().CreatePipelineState();
  132. descriptor.m_renderAttachmentConfiguration.m_renderAttachmentLayout.m_subpassLayouts[0].m_rendertargetDescriptors[0].m_resolveAttachmentIndex = 5;
  133. resultCode = pipelineState->Init(*device, descriptor);
  134. AZ_TEST_STOP_ASSERTTEST(1);
  135. EXPECT_EQ(resultCode, RHI::ResultCode::InvalidOperation);
  136. AZ_TEST_START_ASSERTTEST;
  137. pipelineState = RHI::Factory::Get().CreatePipelineState();
  138. descriptor.m_renderAttachmentConfiguration.m_renderAttachmentLayout.m_subpassLayouts[0].m_rendertargetDescriptors[0].m_resolveAttachmentIndex = 0;
  139. resultCode = pipelineState->Init(*device, descriptor);
  140. AZ_TEST_STOP_ASSERTTEST(1);
  141. EXPECT_EQ(resultCode, RHI::ResultCode::InvalidOperation);
  142. }
  143. TEST_F(PipelineStateTests, PipelineLibrary_CreateEmpty_Test)
  144. {
  145. RHI::Ptr<RHI::DevicePipelineLibrary> empty = RHI::Factory::Get().CreatePipelineLibrary();
  146. EXPECT_NE(empty.get(), nullptr);
  147. EXPECT_FALSE(empty->IsInitialized());
  148. AZ_TEST_START_ASSERTTEST;
  149. EXPECT_EQ(empty->MergeInto({}), RHI::ResultCode::InvalidOperation);
  150. EXPECT_EQ(empty->GetSerializedData(), nullptr);
  151. AZ_TEST_STOP_ASSERTTEST(2);
  152. }
  153. TEST_F(PipelineStateTests, PipelineLibrary_Init_Test)
  154. {
  155. RHI::Ptr<RHI::Device> device = MakeTestDevice();
  156. RHI::Ptr<RHI::DevicePipelineLibrary> pipelineLibrary = RHI::Factory::Get().CreatePipelineLibrary();
  157. RHI::ResultCode resultCode = pipelineLibrary->Init(*device, RHI::DevicePipelineLibraryDescriptor{});
  158. EXPECT_EQ(resultCode, RHI::ResultCode::Success);
  159. // Second init should fail and throw validation error.
  160. AZ_TEST_START_ASSERTTEST;
  161. resultCode = pipelineLibrary->Init(*device, RHI::DevicePipelineLibraryDescriptor{});
  162. AZ_TEST_STOP_ASSERTTEST(1);
  163. EXPECT_EQ(resultCode, RHI::ResultCode::InvalidOperation);
  164. }
  165. }