Files
o3de/Gems/PhysX/Code/Tests/PhysXSystemTests.cpp
T
Esteban Papp 1f9b284de2 Merge branch 'development' into cmake/SPEC-7179
Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com>

# Conflicts:
#	Code/Editor/Plugins/ComponentEntityEditorPlugin/ComponentEntityEditorPlugin_precompiled.h
#	Code/Editor/Plugins/EditorCommon/EditorCommon_precompiled.h
#	Code/Editor/Plugins/EditorCommon/stdafx.cpp
#	Code/Editor/Plugins/FFMPEGPlugin/FFMPEGPlugin_precompiled.h
#	Code/Editor/Plugins/PerforcePlugin/PerforcePlugin_precompiled.h
#	Code/Editor/Plugins/ProjectSettingsTool/ProjectSettingsTool_precompiled.h
#	Code/Framework/AzToolsFramework/AzToolsFramework/AzToolsFramework_precompiled.h
#	Code/Tools/AssetProcessor/native/precompiled.h
#	Code/Tools/Standalone/StandaloneTools_precompiled.h
#	Gems/AssetMemoryAnalyzer/Code/Source/AssetMemoryAnalyzer_precompiled.h
#	Gems/Atom/Asset/ImageProcessingAtom/Code/Source/ImageProcessing_precompiled.h
#	Gems/Atom/RHI/DX12/Code/Source/RHI/Atom_RHI_DX12_precompiled.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/Mac/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/iOS/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Source/Atom_RHI_Metal_precompiled.h
#	Gems/Atom/RHI/Metal/Code/atom_rhi_metal_common_files.cmake
#	Gems/Atom/RHI/Null/Code/Source/Atom_RHI_Null_precompiled.h
#	Gems/Atom/RHI/Null/Code/atom_rhi_null_common_files.cmake
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Android/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Linux/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Mac/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Windows/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Source/Atom_RHI_Vulkan_precompiled.h
#	Gems/Atom/RHI/Vulkan/Code/Source/RHI/SwapChain.cpp
#	Gems/Atom/RHI/Vulkan/Code/atom_rhi_vulkan_common_files.cmake
#	Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/AtomFont_precompiled.h
#	Gems/Blast/Code/Source/StdAfx.cpp
#	Gems/Camera/Code/Source/Camera_precompiled.h
#	Gems/EMotionFX/Code/Source/EMotionFX_precompiled.h
#	Gems/FastNoise/Code/Source/FastNoise_precompiled.h
#	Gems/Gestures/Code/Source/Gestures_precompiled.h
#	Gems/GradientSignal/Code/Source/GradientSignal_precompiled.h
#	Gems/GraphCanvas/Code/precompiled.h
#	Gems/ImGui/Code/Source/ImGui_precompiled.h
#	Gems/InAppPurchases/Code/Source/InAppPurchases_precompiled.h
#	Gems/LmbrCentral/Code/Source/LmbrCentral_precompiled.h
#	Gems/LmbrCentral/Code/Tests/ShapeGeometryUtilTest.cpp
#	Gems/LyShine/Code/Editor/UiCanvasEditor_precompiled.h
#	Gems/LyShine/Code/Source/Animation/LyShine_precompiled.h
#	Gems/LyShine/Code/Source/LyShine_precompiled.h
#	Gems/LyShineExamples/Code/Source/LyShineExamples_precompiled.h
#	Gems/Maestro/Code/Source/Cinematics/Maestro_precompiled.h
#	Gems/Maestro/Code/Source/Maestro_precompiled.h
#	Gems/MessagePopup/Code/Source/MessagePopup_precompiled.h
#	Gems/Metastream/Code/Source/Metastream_precompiled.h
#	Gems/Microphone/Code/Source/Microphone_precompiled.h
#	Gems/Multiplayer/Code/Source/Multiplayer_precompiled.h
#	Gems/PhysX/Code/NumericalMethods/Source/NumericalMethods_precompiled.h
#	Gems/PhysX/Code/Source/PhysXUnsupported_precompiled.h
#	Gems/PhysX/Code/Source/PhysX_precompiled.h
#	Gems/PhysX/Code/physx_unsupported_files.cmake
#	Gems/PhysXDebug/Code/Source/PhysXDebugUnsupported_precompiled.h
#	Gems/PhysXDebug/Code/Source/PhysXDebug_precompiled.h
#	Gems/ScriptCanvas/Code/Editor/precompiled.h
#	Gems/ScriptCanvas/Code/Source/precompiled.h
#	Gems/ScriptCanvasDeveloper/Code/Source/precompiled.h
#	Gems/ScriptCanvasPhysics/Code/Source/ScriptCanvasPhysics_precompiled.h
#	Gems/ScriptEvents/Code/Source/precompiled.h
#	Gems/ScriptEvents/Code/Tests/Editor/EditorTests.cpp
#	Gems/ScriptedEntityTweener/Code/Source/ScriptedEntityTweener_precompiled.h
#	Gems/SliceFavorites/Code/Source/SliceFavorites_precompiled.h
#	Gems/StartingPointCamera/Code/Source/StartingPointCamera_precompiled.h
#	Gems/StartingPointInput/Code/Source/StartingPointInput_precompiled.h
#	Gems/StartingPointMovement/Code/Source/StartingPointMovement_precompiled.h
#	Gems/SurfaceData/Code/Source/SurfaceData_precompiled.h
#	Gems/TextureAtlas/Code/Source/TextureAtlas_precompiled.h
#	Gems/TickBusOrderViewer/Code/Source/TickBusOrderViewer_precompiled.h
#	Gems/Twitch/Code/Source/Twitch_precompiled.h
#	Gems/VirtualGamepad/Code/Source/VirtualGamepad_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBoxUnsupported_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBox_precompiled.h
2021-07-16 15:42:37 -07:00

321 lines
13 KiB
C++

/*
* 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 <AzTest/AzTest.h>
#include <Tests/PhysXTestCommon.h>
#include <AzFramework/Physics/PhysicsSystem.h>
#include <AzFramework/Physics/PhysicsScene.h>
#include <AzFramework/Physics/Common/PhysicsEvents.h>
#include <PhysX/Configuration/PhysXConfiguration.h>
namespace PhysX
{
namespace Internal
{
static constexpr const char* DefaultSceneNameFormat = "scene-%d";
}
//setup a test fixture with no default created scene
class PhysXSystemFixture
: public testing::Test
{
public:
void SetUp() override
{
//create some configs to use in the tests.
AzPhysics::SceneConfiguration config;
for (int i = 0; i < NumScenes; i++)
{
config.m_sceneName = AZStd::string::format(Internal::DefaultSceneNameFormat, i);
m_sceneConfigs.push_back(config);
}
}
void TearDown() override
{
m_sceneConfigs.clear();
TestUtils::ResetPhysXSystem();
}
static constexpr AZ::u64 NumScenes = 10;
AzPhysics::SceneConfigurationList m_sceneConfigs;
PhysXSystemConfiguration m_systemConfig;
};
TEST_F(PhysXSystemFixture, ChangingSystemConfig_ShouldOnlySendEventIfChanged)
{
auto* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
//setup configs
PhysXSystemConfiguration preTestConfig;
if (const auto* config = azdynamic_cast<const PhysXSystemConfiguration*>(physicsSystem->GetConfiguration()))
{
preTestConfig = *config;
}
PhysXSystemConfiguration modifiedConfig = preTestConfig;
modifiedConfig.m_fixedTimestep = 0.0f;
//create event handler
int triggeredCount = 0;
AzPhysics::SystemEvents::OnConfigurationChangedEvent::Handler eventHandler(
[&triggeredCount]([[maybe_unused]]const AzPhysics::SystemConfiguration* config)
{
triggeredCount++;
}
);
//register handler
physicsSystem->RegisterSystemConfigurationChangedEvent(eventHandler);
//update config
physicsSystem->UpdateConfiguration(&preTestConfig); //this should not signal the event
EXPECT_EQ(triggeredCount, 0);
physicsSystem->UpdateConfiguration(&modifiedConfig); //this should signal the event
EXPECT_EQ(triggeredCount, 1);
//reset state
eventHandler.Disconnect();
physicsSystem->UpdateConfiguration(&preTestConfig);
}
TEST_F(PhysXSystemFixture, AddScenes_createsAllScenesRequested)
{
auto* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
EXPECT_TRUE(physicsSystem->GetAllScenes().size() == 0); //there should be no scenes currently created
//add all scene configs
AzPhysics::SceneHandleList sceneHandles = physicsSystem->AddScenes(m_sceneConfigs);
//there should be the same number of scene handles as configs supplied
EXPECT_EQ(sceneHandles.size(), m_sceneConfigs.size());
//the returned handles should match the order of the supplied configs.
for (int i = 0; i< m_sceneConfigs.size(); i++)
{
AzPhysics::Scene* scene = physicsSystem->GetScene(sceneHandles[i]);
EXPECT_EQ(scene->GetConfiguration().m_sceneName, m_sceneConfigs[i].m_sceneName);
}
}
TEST_F(PhysXSystemFixture, RemovedScene_IsRemoved)
{
auto* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
EXPECT_TRUE(physicsSystem->GetAllScenes().size() == 0); //there should be no scenes currently created
//add all scene configs
AzPhysics::SceneHandleList sceneHandles = physicsSystem->AddScenes(m_sceneConfigs);
//pick a scene handles to remove
AzPhysics::SceneHandle removedSelection = sceneHandles[sceneHandles.size() / 2];
physicsSystem->RemoveScene(removedSelection);
EXPECT_EQ(physicsSystem->GetScene(removedSelection), nullptr);
}
TEST_F(PhysXSystemFixture, RemoveManyScenes_AllRemoved)
{
auto* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
EXPECT_TRUE(physicsSystem->GetAllScenes().size() == 0); //there should be no scenes currently created
//add all scene configs
AzPhysics::SceneHandleList sceneHandles = physicsSystem->AddScenes(m_sceneConfigs);
//select half of the scenes to be removed
AzPhysics::SceneHandleList removedHandles;
removedHandles.reserve(sceneHandles.size() / 2);
for (int i = 0; i < sceneHandles.size(); i += 2)
{
removedHandles.emplace_back(sceneHandles[i]);
}
//remove all selected scene
physicsSystem->RemoveScenes(removedHandles);
//verify all removed handles return nullptr
for (auto handle : removedHandles)
{
EXPECT_EQ(physicsSystem->GetScene(handle), nullptr);
}
}
TEST_F(PhysXSystemFixture, RemovingScene_FreesSceneHandle_ForNextCreatedScene)
{
auto* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
EXPECT_TRUE(physicsSystem->GetAllScenes().size() == 0); //there should be no scenes currently created
//add all scene configs
AzPhysics::SceneHandleList sceneHandles = physicsSystem->AddScenes(m_sceneConfigs);
//pick a scene handles to remove
AzPhysics::SceneHandle removedSelection = sceneHandles[sceneHandles.size() / 2];
physicsSystem->RemoveScene(removedSelection);
AzPhysics::SceneConfiguration newSceneConfig;
newSceneConfig.m_sceneName = "NewScene";
AzPhysics::SceneHandle newSceneHandle = physicsSystem->AddScene(newSceneConfig);
//The old and new scene handle should share an index as the freed slot will be used
EXPECT_EQ(AZStd::get<AzPhysics::HandleTypeIndex::Index>(removedSelection),
AZStd::get<AzPhysics::HandleTypeIndex::Index>(newSceneHandle));
}
TEST_F(PhysXSystemFixture, AddingScenes_PastLimitFails)
{
auto* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
EXPECT_TRUE(physicsSystem->GetAllScenes().size() == 0); //there should be no scenes currently created
//generate the max number of scenes
AzPhysics::SceneConfigurationList sceneConfigs;
AzPhysics::SceneConfiguration config;
for (int i = 0; i < std::numeric_limits<AzPhysics::SceneIndex>::max(); i++)
{
config.m_sceneName = AZStd::string::format(Internal::DefaultSceneNameFormat, i);
sceneConfigs.push_back(config);
}
config.m_sceneName = "boom!";
//add all scene configs
AzPhysics::SceneHandleList sceneHandles = physicsSystem->AddScenes(sceneConfigs);
//Verify we've added the max number and all are valid
EXPECT_EQ(sceneHandles.size(), std::numeric_limits<AzPhysics::SceneIndex>::max());
for (auto& handle : sceneHandles)
{
EXPECT_TRUE(handle != AzPhysics::InvalidSceneHandle);
}
//Add the extra scene that should fail
AzPhysics::SceneHandle failAddSceneHandle = physicsSystem->AddScene(config);
//the handle should be invalid
EXPECT_EQ(failAddSceneHandle, AzPhysics::InvalidSceneHandle);
}
TEST_F(PhysXSystemFixture, PrePostSimulateEvents_TriggerAsExpected)
{
auto* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
const AzPhysics::SystemConfiguration* config = physicsSystem->GetConfiguration();
const float frameDeltaTime = AZStd::clamp(1.0f / 30.0f, 0.0f, config->m_maxTimestep);
float expectedTickTime = AZStd::floorf(frameDeltaTime / config->m_fixedTimestep) * config->m_fixedTimestep;
//init handlers
int preSimEventCount = 0;
int postSimEventCount = 0;
AzPhysics::SystemEvents::OnPresimulateEvent::Handler preSimEvent(
[&expectedTickTime, &preSimEventCount](float deltaTime)
{
EXPECT_NEAR(expectedTickTime, deltaTime, 0.001f);
preSimEventCount++;
});
AzPhysics::SystemEvents::OnPostsimulateEvent::Handler postSimEvent(
[&expectedTickTime, &postSimEventCount](float deltaTime)
{
EXPECT_NEAR(expectedTickTime, deltaTime, 0.001f);
postSimEventCount++;
});
physicsSystem->RegisterPreSimulateEvent(preSimEvent);
physicsSystem->RegisterPostSimulateEvent(postSimEvent);
//run for 1 frame, handlers should be called once.
physicsSystem->Simulate(frameDeltaTime);
EXPECT_TRUE(preSimEventCount == 1);
EXPECT_TRUE(postSimEventCount == 1);
preSimEventCount = 0;
postSimEventCount = 0;
//run for 5 frames, handlers should be called 5 times
constexpr const float numFrames = 5;
float accumulatedTime = frameDeltaTime - config->m_fixedTimestep;
for (int i = 0; i < numFrames; i++)
{
accumulatedTime += frameDeltaTime;
expectedTickTime = AZStd::floorf(accumulatedTime / config->m_fixedTimestep) * config->m_fixedTimestep;
physicsSystem->Simulate(frameDeltaTime);
accumulatedTime -= expectedTickTime;
}
EXPECT_TRUE(preSimEventCount == numFrames);
EXPECT_TRUE(postSimEventCount == numFrames);
}
TEST_F(PhysXSystemFixture, PreSimulateEvent_WithDeltaTime_GreaterThanMaxTimeStep_ShouldSendMaxTimeStep)
{
auto* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
const float fixedTimeStep = physicsSystem->GetConfiguration()->m_fixedTimestep;
const float maxTimeStep = physicsSystem->GetConfiguration()->m_maxTimestep;
const float frameDeltaTime = (maxTimeStep) * 2.0f; //create a delta time greater than the max time step.
//this is the same logic as in PhysXSystem::Simualte
float expectedTimeStep = AZ::GetClamp(frameDeltaTime, 0.0f, maxTimeStep);
expectedTimeStep = AZStd::floorf(expectedTimeStep / fixedTimeStep) * fixedTimeStep;
//init handler
AzPhysics::SystemEvents::OnPresimulateEvent::Handler preSimEvent(
[expectedTimeStep](float deltaTime)
{
EXPECT_NEAR(expectedTimeStep, deltaTime, 0.001f);
});
physicsSystem->RegisterPreSimulateEvent(preSimEvent);
physicsSystem->Simulate(frameDeltaTime);
}
TEST_F(PhysXSystemFixture, GetHandle_ReturnsExpected)
{
auto* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
//add all scene configs
AzPhysics::SceneHandleList sceneHandles = physicsSystem->AddScenes(m_sceneConfigs);
//setup the expected handle to get.
const int testSceneHandleIdx = PhysXSystemFixture::NumScenes / 2; //pick one in range
const AzPhysics::SceneHandle expectedSceneHandle = sceneHandles[testSceneHandleIdx];
//create name as the same way they're created in PhysXSystemFixture::Setup();
const AZStd::string sceneName = AZStd::string::format(Internal::DefaultSceneNameFormat, testSceneHandleIdx);
AzPhysics::SceneHandle sceneHandle = physicsSystem->GetSceneHandle(sceneName);
EXPECT_EQ(sceneHandle, expectedSceneHandle);
//ask for a scene not in the list, returns InvalidSceneHandle
sceneHandle = physicsSystem->GetSceneHandle("ThisSceneIsNotHere");
EXPECT_EQ(sceneHandle, AzPhysics::InvalidSceneHandle);
}
TEST_F(PhysXSystemFixture, AddRemoveScenes_InvokesEvents)
{
auto* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
//setup the add scene handler
int addedCount = 0;
AzPhysics::SystemEvents::OnSceneAddedEvent::Handler onAddedHandler([&addedCount]([[maybe_unused]]AzPhysics::SceneHandle sceneHandle)
{
addedCount++;
});
physicsSystem->RegisterSceneAddedEvent(onAddedHandler);
//add all scene configs
AzPhysics::SceneHandleList sceneHandles = physicsSystem->AddScenes(m_sceneConfigs);
//the handler should be invoked the same number of times of the requested scenes.
EXPECT_EQ(addedCount, m_sceneConfigs.size());
//Setup the removed scene handler
int removedCount = 0;
AzPhysics::SystemEvents::OnSceneRemovedEvent::Handler onRemovedHandler([&removedCount, &sceneHandles, physicsSystem](AzPhysics::SceneHandle sceneHandle)
{
removedCount++;
auto foundItr = AZStd::find(sceneHandles.begin(), sceneHandles.end(), sceneHandle);
EXPECT_NE(foundItr, sceneHandles.end()); //the handle should be in the list.
EXPECT_NE(physicsSystem->GetScene(sceneHandle), nullptr); //should return a valid scene.
});
physicsSystem->RegisterSceneRemovedEvent(onRemovedHandler);
physicsSystem->RemoveScenes(sceneHandles);
EXPECT_EQ(removedCount, m_sceneConfigs.size());
}
}