Module.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/std/smart_ptr/unique_ptr.h>
  9. #include <AzCore/Module/Module.h>
  10. #include <AzCore/Module/DynamicModuleHandle.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <AzCore/Component/ComponentApplicationBus.h>
  13. #include <Configuration/PhysXSettingsRegistryManager.h>
  14. #include <Source/SystemComponent.h>
  15. #include <ComponentDescriptors.h>
  16. #if defined(PHYSX_EDITOR)
  17. #include <Source/EditorComponentDescriptors.h>
  18. #include <Editor/Source/Components/EditorSystemComponent.h>
  19. #include <Editor/Source/Configuration/PhysXEditorSettingsRegistryManager.h>
  20. #endif // defined(PHYSX_EDITOR)
  21. #include <System/PhysXSystem.h>
  22. #include <System/PhysXCookingParams.h>
  23. namespace PhysX
  24. {
  25. class Module
  26. : public AZ::Module
  27. {
  28. public:
  29. AZ_RTTI(PhysX::Module, "{A2E6D801-1510-4EFD-B40A-8C3B066A0323}", AZ::Module);
  30. AZ_CLASS_ALLOCATOR(PhysX::Module, AZ::SystemAllocator)
  31. Module()
  32. : AZ::Module()
  33. #if defined(PHYSX_EDITOR)
  34. , m_physXSystem(AZStd::make_unique<PhysXEditorSettingsRegistryManager>(), PxCooking::GetEditTimeCookingParams())
  35. #else
  36. , m_physXSystem(AZStd::make_unique<PhysXSettingsRegistryManager>(), PxCooking::GetRealTimeCookingParams())
  37. #endif
  38. {
  39. // PhysXSystemConfiguration needs to be 16-byte aligned since it contains a SIMD vector4.
  40. // The vector4 itself is aligned relative to the module class, but if the module class is
  41. // not also aligned, it will crash. This checks makes sure they will be aligned to 16 bytes.
  42. static_assert(alignof(PhysX::PhysXSystemConfiguration) == 16);
  43. static_assert(alignof(PhysX::PhysXSystem) == 16);
  44. LoadModules();
  45. AZStd::list<AZ::ComponentDescriptor*> descriptorsToAdd = GetDescriptors();
  46. m_descriptors.insert(m_descriptors.end(), descriptorsToAdd.begin(), descriptorsToAdd.end());
  47. #if defined(PHYSX_EDITOR)
  48. AZStd::list<AZ::ComponentDescriptor*> editorDescriptorsToAdd = GetEditorDescriptors();
  49. m_descriptors.insert(m_descriptors.end(), editorDescriptorsToAdd.begin(), editorDescriptorsToAdd.end());
  50. #endif // defined(PHYSX_EDITOR)
  51. }
  52. virtual ~Module()
  53. {
  54. m_physXSystem.Shutdown();
  55. UnloadModules();
  56. AZ::GetCurrentSerializeContextModule().Cleanup();
  57. }
  58. AZ::ComponentTypeList GetRequiredSystemComponents() const override
  59. {
  60. return AZ::ComponentTypeList{
  61. azrtti_typeid<SystemComponent>()
  62. #if defined(PHYSX_EDITOR)
  63. , azrtti_typeid<EditorSystemComponent>()
  64. #endif
  65. };
  66. }
  67. private:
  68. void LoadModules()
  69. {
  70. #if defined(PHYSX_EDITOR)
  71. {
  72. AZStd::unique_ptr<AZ::DynamicModuleHandle> sceneCoreModule = AZ::DynamicModuleHandle::Create("SceneCore");
  73. [[maybe_unused]] bool ok = sceneCoreModule->Load(AZ::DynamicModuleHandle::LoadFlags::InitFuncRequired);
  74. AZ_Error("PhysX::Module", ok, "Error loading SceneCore module");
  75. m_modules.push_back(AZStd::move(sceneCoreModule));
  76. }
  77. #endif // defined(PHYSX_EDITOR)
  78. }
  79. void UnloadModules()
  80. {
  81. // Unload modules in reserve order that were loaded
  82. for (auto it = m_modules.rbegin(); it != m_modules.rend(); ++it)
  83. {
  84. it->reset();
  85. }
  86. m_modules.clear();
  87. }
  88. /// Required modules to load/unload when PhysX Gem module is created/destroyed
  89. AZStd::vector<AZStd::unique_ptr<AZ::DynamicModuleHandle>> m_modules;
  90. PhysXSystem m_physXSystem;
  91. };
  92. // The PhysX::Module also needs to be 16-byte aligned
  93. static_assert(alignof(PhysX::Module) == 16);
  94. } // namespace PhysX
  95. #if defined(O3DE_GEM_NAME)
  96. AZ_DECLARE_MODULE_CLASS(AZ_JOIN(Gem_, O3DE_GEM_NAME), PhysX::Module)
  97. #else
  98. AZ_DECLARE_MODULE_CLASS(Gem_PhysX, PhysX::Module)
  99. #endif