3
0

RPITestFixture.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <Common/RPITestFixture.h>
  9. #include <Common/RHI/Factory.h>
  10. #include <AzCore/Asset/AssetManager.h>
  11. #include <AzCore/Asset/AssetManagerComponent.h>
  12. #include <AzCore/Name/NameDictionary.h>
  13. #include <AzCore/AzCore_Traits_Platform.h>
  14. #include <AzCore/Serialization/Json/JsonSystemComponent.h>
  15. #include <AzCore/IO/Path/Path.h>
  16. #include <AzCore/Script/ScriptSystemComponent.h>
  17. #include <AzCore/Utils/Utils.h>
  18. #include <AzFramework/IO/LocalFileIO.h>
  19. #include <AzTest/Utils.h>
  20. #include <Atom/RHI/Device.h>
  21. #include <Atom/RHI.Reflect/ReflectSystemComponent.h>
  22. #include <Atom/RPI.Reflect/ResourcePoolAsset.h>
  23. #include <Atom/RPI.Reflect/Asset/AssetHandler.h>
  24. #include <Atom/RPI.Public/Shader/ShaderResourceGroupPool.h>
  25. namespace UnitTest
  26. {
  27. using namespace AZ;
  28. using namespace RPI;
  29. // Expose AssetManagerComponent::Reflect function for testing
  30. class MyAssetManagerComponent : public AssetManagerComponent
  31. {
  32. public:
  33. static void Reflect(ReflectContext* reflection)
  34. {
  35. AssetManagerComponent::Reflect(reflection);
  36. }
  37. };
  38. RPITestFixture::RPITestFixture() = default;
  39. RPITestFixture::~RPITestFixture() = default;
  40. JsonRegistrationContext* RPITestFixture::GetJsonRegistrationContext()
  41. {
  42. return m_jsonRegistrationContext.get();
  43. }
  44. void RPITestFixture::Reflect(ReflectContext* context)
  45. {
  46. MyAssetManagerComponent::Reflect(context);
  47. RHI::ReflectSystemComponent::Reflect(context);
  48. RPI::RPISystem::Reflect(context);
  49. Name::Reflect(context);
  50. }
  51. void RPITestFixture::SetUp()
  52. {
  53. AssetManagerTestFixture::SetUp();
  54. AZ::RPI::Validation::s_isEnabled = true;
  55. AZ::RHI::Validation::s_isEnabled = true;
  56. m_priorFileIO = AZ::IO::FileIOBase::GetInstance();
  57. m_localFileIO.reset(aznew AZ::IO::LocalFileIO());
  58. AZ::IO::FileIOBase::SetInstance(m_localFileIO.get());
  59. AZ::IO::Path assetPath = AZStd::string_view{ AZ::Utils::GetProjectPath() };
  60. assetPath /= "Cache";
  61. AZ::IO::FileIOBase::GetInstance()->SetAlias("@products@", assetPath.c_str());
  62. m_jsonRegistrationContext = AZStd::make_unique<AZ::JsonRegistrationContext>();
  63. AZ::JsonSystemComponent::Reflect(m_jsonRegistrationContext.get());
  64. m_scriptSystemComponentDescriptor.reset(AZ::ScriptSystemComponent::CreateDescriptor());
  65. // Reflect the ScriptSystemComponent
  66. m_scriptSystemComponentDescriptor->Reflect(GetBehaviorContext());
  67. Reflect(GetSerializeContext());
  68. Reflect(GetBehaviorContext());
  69. Reflect(m_jsonRegistrationContext.get());
  70. NameDictionary::Create();
  71. m_rhiFactory.reset(aznew StubRHI::Factory());
  72. RPI::RPISystemDescriptor rpiSystemDescriptor;
  73. m_rpiSystem = AZStd::make_unique<RPI::RPISystem>();
  74. m_rpiSystem->Initialize(rpiSystemDescriptor);
  75. m_rpiSystem->InitializeSystemAssetsForTests();
  76. // Create the system entity
  77. m_systemEntity = AZStd::make_unique<AZ::Entity>(AZ::SystemEntityId);
  78. // Add the Lua Script System Component to add a Global Script Context
  79. m_systemEntity->CreateComponent<AZ::ScriptSystemComponent>();
  80. // Activate the System Entity
  81. m_systemEntity->Init();
  82. m_systemEntity->Activate();
  83. // Bind the reflected BehaviorContext functions to the ScriptContext
  84. AZ::ScriptContext* scriptContext{};
  85. AZ::ScriptSystemRequestBus::BroadcastResult(scriptContext, &AZ::ScriptSystemRequests::GetContext, AZ::ScriptContextIds::DefaultScriptContextId);
  86. ASSERT_NE(nullptr, scriptContext);
  87. scriptContext->BindTo(GetBehaviorContext());
  88. // Setup job context for job system
  89. JobManagerDesc desc;
  90. JobManagerThreadDesc threadDesc;
  91. #if AZ_TRAIT_SET_JOB_PROCESSOR_ID
  92. threadDesc.m_cpuId = 0; // Don't set processors IDs on windows
  93. #endif
  94. uint32_t numWorkerThreads = AZStd::thread::hardware_concurrency();
  95. for (unsigned int i = 0; i < numWorkerThreads; ++i)
  96. {
  97. desc.m_workerThreads.push_back(threadDesc);
  98. #if AZ_TRAIT_SET_JOB_PROCESSOR_ID
  99. threadDesc.m_cpuId++;
  100. #endif
  101. }
  102. m_jobManager = AZStd::make_unique<JobManager>(desc);
  103. m_jobContext = AZStd::make_unique<JobContext>(*m_jobManager);
  104. JobContext::SetGlobalContext(m_jobContext.get());
  105. m_assetSystemStub.Activate();
  106. }
  107. void RPITestFixture::TearDown()
  108. {
  109. // Flushing the tick bus queue since AZ::RHI::Factory:Register queues a function
  110. AZ::SystemTickBus::ClearQueuedEvents();
  111. m_assetSystemStub.Deactivate();
  112. JobContext::SetGlobalContext(nullptr);
  113. m_jobContext = nullptr;
  114. m_jobManager = nullptr;
  115. // Deactivate and deletes the System Entity
  116. m_systemEntity.reset();
  117. m_rpiSystem->Shutdown();
  118. m_rpiSystem = nullptr;
  119. m_rhiFactory = nullptr;
  120. NameDictionary::Destroy();
  121. m_jsonRegistrationContext->EnableRemoveReflection();
  122. AZ::JsonSystemComponent::Reflect(m_jsonRegistrationContext.get());
  123. Reflect(m_jsonRegistrationContext.get());
  124. m_jsonRegistrationContext->DisableRemoveReflection();
  125. auto serializeContext = GetSerializeContext();
  126. serializeContext->EnableRemoveReflection();
  127. Reflect(serializeContext);
  128. serializeContext->DisableRemoveReflection();
  129. auto behaviorContext = GetBehaviorContext();
  130. behaviorContext->EnableRemoveReflection();
  131. Reflect(behaviorContext);
  132. m_scriptSystemComponentDescriptor->Reflect(behaviorContext);
  133. behaviorContext->DisableRemoveReflection();
  134. m_scriptSystemComponentDescriptor.reset();
  135. m_jsonRegistrationContext.reset();
  136. AZ::IO::FileIOBase::SetInstance(m_priorFileIO);
  137. m_localFileIO.reset();
  138. AssetManagerTestFixture::TearDown();
  139. }
  140. void RPITestFixture::ProcessQueuedSrgCompilations(Data::Asset<ShaderAsset> shaderAsset, const AZ::Name& srgName)
  141. {
  142. Data::Instance<ShaderResourceGroupPool> srgPool = ShaderResourceGroupPool::FindOrCreate(shaderAsset, RPI::DefaultSupervariantIndex, srgName);
  143. srgPool->GetRHIPool()->CompileGroupsBegin();
  144. srgPool->GetRHIPool()->CompileGroupsForInterval(RHI::Interval(0, srgPool->GetRHIPool()->GetGroupsToCompileCount()));
  145. srgPool->GetRHIPool()->CompileGroupsEnd();
  146. }
  147. }
  148. AZ_UNIT_TEST_HOOK(DEFAULT_UNIT_TEST_ENV);