123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <AzCore/std/smart_ptr/unique_ptr.h>
- #include <AzCore/Module/Module.h>
- #include <AzCore/Module/DynamicModuleHandle.h>
- #include <AzCore/Serialization/SerializeContext.h>
- #include <AzCore/Component/ComponentApplicationBus.h>
- #include <Configuration/PhysXSettingsRegistryManager.h>
- #include <Source/SystemComponent.h>
- #include <ComponentDescriptors.h>
- #if defined(PHYSX_EDITOR)
- #include <Source/EditorComponentDescriptors.h>
- #include <Editor/Source/Components/EditorSystemComponent.h>
- #include <Editor/Source/Configuration/PhysXEditorSettingsRegistryManager.h>
- #endif // defined(PHYSX_EDITOR)
- #include <System/PhysXSystem.h>
- #include <System/PhysXCookingParams.h>
- namespace PhysX
- {
- class Module
- : public AZ::Module
- {
- public:
- AZ_RTTI(PhysX::Module, "{A2E6D801-1510-4EFD-B40A-8C3B066A0323}", AZ::Module);
- AZ_CLASS_ALLOCATOR(PhysX::Module, AZ::SystemAllocator)
- Module()
- : AZ::Module()
- #if defined(PHYSX_EDITOR)
- , m_physXSystem(AZStd::make_unique<PhysXEditorSettingsRegistryManager>(), PxCooking::GetEditTimeCookingParams())
- #else
- , m_physXSystem(AZStd::make_unique<PhysXSettingsRegistryManager>(), PxCooking::GetRealTimeCookingParams())
- #endif
- {
- // PhysXSystemConfiguration needs to be 16-byte aligned since it contains a SIMD vector4.
- // The vector4 itself is aligned relative to the module class, but if the module class is
- // not also aligned, it will crash. This checks makes sure they will be aligned to 16 bytes.
- static_assert(alignof(PhysX::PhysXSystemConfiguration) == 16);
- static_assert(alignof(PhysX::PhysXSystem) == 16);
-
- LoadModules();
- AZStd::list<AZ::ComponentDescriptor*> descriptorsToAdd = GetDescriptors();
- m_descriptors.insert(m_descriptors.end(), descriptorsToAdd.begin(), descriptorsToAdd.end());
- #if defined(PHYSX_EDITOR)
- AZStd::list<AZ::ComponentDescriptor*> editorDescriptorsToAdd = GetEditorDescriptors();
- m_descriptors.insert(m_descriptors.end(), editorDescriptorsToAdd.begin(), editorDescriptorsToAdd.end());
- #endif // defined(PHYSX_EDITOR)
- }
- virtual ~Module()
- {
- m_physXSystem.Shutdown();
- UnloadModules();
- AZ::GetCurrentSerializeContextModule().Cleanup();
- }
- AZ::ComponentTypeList GetRequiredSystemComponents() const override
- {
- return AZ::ComponentTypeList{
- azrtti_typeid<SystemComponent>()
- #if defined(PHYSX_EDITOR)
- , azrtti_typeid<EditorSystemComponent>()
- #endif
- };
- }
- private:
- void LoadModules()
- {
- #if defined(PHYSX_EDITOR)
- {
- AZStd::unique_ptr<AZ::DynamicModuleHandle> sceneCoreModule = AZ::DynamicModuleHandle::Create("SceneCore");
- [[maybe_unused]] bool ok = sceneCoreModule->Load(AZ::DynamicModuleHandle::LoadFlags::InitFuncRequired);
- AZ_Error("PhysX::Module", ok, "Error loading SceneCore module");
- m_modules.push_back(AZStd::move(sceneCoreModule));
- }
- #endif // defined(PHYSX_EDITOR)
- }
- void UnloadModules()
- {
- // Unload modules in reserve order that were loaded
- for (auto it = m_modules.rbegin(); it != m_modules.rend(); ++it)
- {
- it->reset();
- }
- m_modules.clear();
- }
- /// Required modules to load/unload when PhysX Gem module is created/destroyed
- AZStd::vector<AZStd::unique_ptr<AZ::DynamicModuleHandle>> m_modules;
- PhysXSystem m_physXSystem;
- };
- // The PhysX::Module also needs to be 16-byte aligned
- static_assert(alignof(PhysX::Module) == 16);
- } // namespace PhysX
- #if defined(O3DE_GEM_NAME)
- AZ_DECLARE_MODULE_CLASS(AZ_JOIN(Gem_, O3DE_GEM_NAME), PhysX::Module)
- #else
- AZ_DECLARE_MODULE_CLASS(Gem_PhysX, PhysX::Module)
- #endif
|