diff --git a/Gems/SurfaceData/Code/CMakeLists.txt b/Gems/SurfaceData/Code/CMakeLists.txt index 860053ca42..6cef8b6e25 100644 --- a/Gems/SurfaceData/Code/CMakeLists.txt +++ b/Gems/SurfaceData/Code/CMakeLists.txt @@ -102,4 +102,8 @@ if(PAL_TRAIT_BUILD_TESTS_SUPPORTED) ly_add_googletest( NAME Gem::SurfaceData.Tests ) + ly_add_googlebenchmark( + NAME Gem::SurfaceData.Benchmarks + TARGET Gem::SurfaceData.Tests + ) endif() diff --git a/Gems/SurfaceData/Code/Include/SurfaceData/SurfaceDataSystemRequestBus.h b/Gems/SurfaceData/Code/Include/SurfaceData/SurfaceDataSystemRequestBus.h index 8dd3e02a53..616f069184 100644 --- a/Gems/SurfaceData/Code/Include/SurfaceData/SurfaceDataSystemRequestBus.h +++ b/Gems/SurfaceData/Code/Include/SurfaceData/SurfaceDataSystemRequestBus.h @@ -12,6 +12,7 @@ #include #include #include +#include #include namespace SurfaceData @@ -41,6 +42,12 @@ namespace SurfaceData virtual void GetSurfacePointsFromRegion(const AZ::Aabb& inRegion, const AZ::Vector2 stepSize, const SurfaceTagVector& desiredTags, SurfacePointLists& surfacePointLists) const = 0; + // Get all surface points for every passed-in input position. Only the XY dimensions of each position are used. + virtual void GetSurfacePointsFromList( + AZStd::span inPositions, + const SurfaceTagVector& desiredTags, + SurfacePointLists& surfacePointLists) const = 0; + virtual SurfaceDataRegistryHandle RegisterSurfaceDataProvider(const SurfaceDataRegistryEntry& entry) = 0; virtual void UnregisterSurfaceDataProvider(const SurfaceDataRegistryHandle& handle) = 0; virtual void UpdateSurfaceDataProvider(const SurfaceDataRegistryHandle& handle, const SurfaceDataRegistryEntry& entry) = 0; diff --git a/Gems/SurfaceData/Code/Include/SurfaceData/Tests/SurfaceDataTestMocks.h b/Gems/SurfaceData/Code/Include/SurfaceData/Tests/SurfaceDataTestMocks.h index 5e8acdc4c5..82a321756d 100644 --- a/Gems/SurfaceData/Code/Include/SurfaceData/Tests/SurfaceDataTestMocks.h +++ b/Gems/SurfaceData/Code/Include/SurfaceData/Tests/SurfaceDataTestMocks.h @@ -8,14 +8,15 @@ #pragma once #include -#include + +#include #include #include #include +#include #include #include -#include namespace UnitTest { @@ -23,23 +24,6 @@ namespace UnitTest : public ::testing::Test { protected: - AZ::ComponentApplication m_app; - AZ::Entity* m_systemEntity = nullptr; - - void SetUp() override - { - AZ::ComponentApplication::Descriptor appDesc; - appDesc.m_memoryBlocksByteSize = 128 * 1024 * 1024; - m_systemEntity = m_app.Create(appDesc); - m_app.AddEntity(m_systemEntity); - } - - void TearDown() override - { - m_app.Destroy(); - m_systemEntity = nullptr; - } - AZStd::unique_ptr CreateEntity() { return AZStd::make_unique(); @@ -57,14 +41,12 @@ namespace UnitTest template AZ::Component* CreateComponent(AZ::Entity* entity, const Configuration& config) { - m_app.RegisterComponentDescriptor(Component::CreateDescriptor()); return entity->CreateComponent(config); } template AZ::Component* CreateComponent(AZ::Entity* entity) { - m_app.RegisterComponentDescriptor(Component::CreateDescriptor()); return entity->CreateComponent(); } }; @@ -129,6 +111,29 @@ namespace UnitTest } }; + // Mock out a generic Physics Collider Component, which is a required dependency for adding a SurfaceDataColliderComponent. + struct MockPhysicsColliderComponent : public AZ::Component + { + public: + AZ_COMPONENT(MockPhysicsColliderComponent, "{4F7C36DE-6475-4E0A-96A7-BFAF21C07C95}", AZ::Component); + + void Activate() override + { + } + void Deactivate() override + { + } + + static void Reflect(AZ::ReflectContext* reflect) + { + AZ_UNUSED(reflect); + } + static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided) + { + provided.push_back(AZ_CRC("PhysXColliderService", 0x4ff43f7c)); + } + }; + struct MockTransformHandler : public AZ::TransformBus::Handler { @@ -204,6 +209,13 @@ namespace UnitTest { } + void GetSurfacePointsFromList( + [[maybe_unused]] AZStd::span inPositions, + [[maybe_unused]] const SurfaceData::SurfaceTagVector& desiredTags, + [[maybe_unused]] SurfaceData::SurfacePointLists& surfacePointLists) const override + { + } + SurfaceData::SurfaceDataRegistryHandle RegisterSurfaceDataProvider(const SurfaceData::SurfaceDataRegistryEntry& entry) override { return RegisterEntry(entry, m_providers); diff --git a/Gems/SurfaceData/Code/Source/Components/SurfaceDataColliderComponent.cpp b/Gems/SurfaceData/Code/Source/Components/SurfaceDataColliderComponent.cpp index 31bf4e7028..d7de299829 100644 --- a/Gems/SurfaceData/Code/Source/Components/SurfaceDataColliderComponent.cpp +++ b/Gems/SurfaceData/Code/Source/Components/SurfaceDataColliderComponent.cpp @@ -240,15 +240,16 @@ namespace SurfaceData point.m_entityId = GetEntityId(); point.m_position = hitPosition; point.m_normal = hitNormal; - AddMaxValueForMasks(point.m_masks, m_configuration.m_providerTags, 1.0f); + for (auto& tag : m_configuration.m_providerTags) + { + point.m_masks[tag] = 1.0f; + } surfacePointList.push_back(point); } } void SurfaceDataColliderComponent::ModifySurfacePoints(SurfacePointList& surfacePointList) const { - AZ_PROFILE_FUNCTION(Entity); - AZStd::lock_guard lock(m_cacheMutex); if (m_colliderBounds.IsValid() && !m_configuration.m_modifierTags.empty()) diff --git a/Gems/SurfaceData/Code/Source/Components/SurfaceDataShapeComponent.cpp b/Gems/SurfaceData/Code/Source/Components/SurfaceDataShapeComponent.cpp index 890f8fba54..a45903498e 100644 --- a/Gems/SurfaceData/Code/Source/Components/SurfaceDataShapeComponent.cpp +++ b/Gems/SurfaceData/Code/Source/Components/SurfaceDataShapeComponent.cpp @@ -143,8 +143,6 @@ namespace SurfaceData void SurfaceDataShapeComponent::GetSurfacePoints(const AZ::Vector3& inPosition, SurfacePointList& surfacePointList) const { - AZ_PROFILE_FUNCTION(Entity); - AZStd::lock_guard lock(m_cacheMutex); if (m_shapeBoundsIsValid) @@ -160,7 +158,10 @@ namespace SurfaceData point.m_entityId = GetEntityId(); point.m_position = rayOrigin + intersectionDistance * rayDirection; point.m_normal = AZ::Vector3::CreateAxisZ(); - AddMaxValueForMasks(point.m_masks, m_configuration.m_providerTags, 1.0f); + for (auto& tag : m_configuration.m_providerTags) + { + point.m_masks[tag] = 1.0f; + } surfacePointList.push_back(point); } } diff --git a/Gems/SurfaceData/Code/Source/SurfaceDataSystemComponent.cpp b/Gems/SurfaceData/Code/Source/SurfaceDataSystemComponent.cpp index 9c53b13fdc..a2a3bc352e 100644 --- a/Gems/SurfaceData/Code/Source/SurfaceDataSystemComponent.cpp +++ b/Gems/SurfaceData/Code/Source/SurfaceDataSystemComponent.cpp @@ -181,8 +181,6 @@ namespace SurfaceData void SurfaceDataSystemComponent::GetSurfacePoints(const AZ::Vector3& inPosition, const SurfaceTagVector& desiredTags, SurfacePointList& surfacePointList) const { - AZ_PROFILE_FUNCTION(Entity); - const bool hasDesiredTags = HasValidTags(desiredTags); const bool hasModifierTags = hasDesiredTags && HasMatchingTags(desiredTags, m_registeredModifierTags); @@ -228,42 +226,47 @@ namespace SurfaceData void SurfaceDataSystemComponent::GetSurfacePointsFromRegion(const AZ::Aabb& inRegion, const AZ::Vector2 stepSize, const SurfaceTagVector& desiredTags, SurfacePointLists& surfacePointLists) const { - AZStd::lock_guard registrationLock(m_registrationMutex); - const size_t totalQueryPositions = aznumeric_cast(ceil(inRegion.GetXExtent() / stepSize.GetX())) * aznumeric_cast(ceil(inRegion.GetYExtent() / stepSize.GetY())); AZStd::vector inPositions; inPositions.reserve(totalQueryPositions); - surfacePointLists.clear(); - surfacePointLists.reserve(totalQueryPositions); - // Initialize our list-per-position list with every input position to query from the region. // This is inclusive on the min sides of inRegion, and exclusive on the max sides. for (float y = inRegion.GetMin().GetY(); y < inRegion.GetMax().GetY(); y += stepSize.GetY()) { for (float x = inRegion.GetMin().GetX(); x < inRegion.GetMax().GetX(); x += stepSize.GetX()) { - inPositions.emplace_back(AZ::Vector3(x, y, AZ::Constants::FloatMax)); - surfacePointLists.emplace_back(SurfaceData::SurfacePointList{}); + inPositions.emplace_back(x, y, AZ::Constants::FloatMax); } } + GetSurfacePointsFromList(inPositions, desiredTags, surfacePointLists); + } + + void SurfaceDataSystemComponent::GetSurfacePointsFromList( + AZStd::span inPositions, const SurfaceTagVector& desiredTags, SurfacePointLists& surfacePointLists) const + { + AZStd::lock_guard registrationLock(m_registrationMutex); + + const size_t totalQueryPositions = inPositions.size(); + + surfacePointLists.clear(); + surfacePointLists.resize(totalQueryPositions); + const bool hasDesiredTags = HasValidTags(desiredTags); const bool hasModifierTags = hasDesiredTags && HasMatchingTags(desiredTags, m_registeredModifierTags); // Loop through each data provider, and query all the points for each one. This allows us to check the tags and the overall - // AABB bounds just once per provider, instead of once per point. It also allows for an eventual optimization in which we could send - // the list of points directly into each SurfaceDataProvider. + // AABB bounds just once per provider, instead of once per point. It also allows for an eventual optimization in which we could + // send the list of points directly into each SurfaceDataProvider. for (const auto& entryPair : m_registeredSurfaceDataProviders) { const SurfaceDataRegistryEntry& entry = entryPair.second; bool alwaysApplies = !entry.m_bounds.IsValid(); - if ((!hasDesiredTags || hasModifierTags || HasMatchingTags(desiredTags, entry.m_tags)) && - ( alwaysApplies || AabbOverlaps2D(entry.m_bounds, inRegion) ) - ) + if (!hasDesiredTags || hasModifierTags || HasMatchingTags(desiredTags, entry.m_tags)) { for (size_t index = 0; index < totalQueryPositions; index++) { @@ -288,18 +291,16 @@ namespace SurfaceData const SurfaceDataRegistryEntry& entry = entryPair.second; bool alwaysApplies = !entry.m_bounds.IsValid(); - if (alwaysApplies || AabbOverlaps2D(entry.m_bounds, inRegion)) + for (size_t index = 0; index < totalQueryPositions; index++) { - for (size_t index = 0; index < totalQueryPositions; index++) + const auto& inPosition = inPositions[index]; + SurfacePointList& surfacePointList = surfacePointLists[index]; + if (!surfacePointList.empty()) { - const auto& inPosition = inPositions[index]; - SurfacePointList& surfacePointList = surfacePointLists[index]; - if (!surfacePointList.empty()) + if (alwaysApplies || AabbContains2D(entry.m_bounds, inPosition)) { - if (alwaysApplies || AabbContains2D(entry.m_bounds, inPosition)) - { - SurfaceDataModifierRequestBus::Event(entryPair.first, &SurfaceDataModifierRequestBus::Events::ModifySurfacePoints, surfacePointList); - } + SurfaceDataModifierRequestBus::Event( + entryPair.first, &SurfaceDataModifierRequestBus::Events::ModifySurfacePoints, surfacePointList); } } } @@ -318,6 +319,8 @@ namespace SurfaceData } } + + void SurfaceDataSystemComponent::CombineSortAndFilterNeighboringPoints(SurfacePointList& sourcePointList, bool hasDesiredTags, const SurfaceTagVector& desiredTags) const { AZ_PROFILE_FUNCTION(Entity); diff --git a/Gems/SurfaceData/Code/Source/SurfaceDataSystemComponent.h b/Gems/SurfaceData/Code/Source/SurfaceDataSystemComponent.h index c329103efb..8914ee7972 100644 --- a/Gems/SurfaceData/Code/Source/SurfaceDataSystemComponent.h +++ b/Gems/SurfaceData/Code/Source/SurfaceDataSystemComponent.h @@ -42,6 +42,10 @@ namespace SurfaceData void GetSurfacePointsFromRegion( const AZ::Aabb& inRegion, const AZ::Vector2 stepSize, const SurfaceTagVector& desiredTags, SurfacePointLists& surfacePointListPerPosition) const override; + void GetSurfacePointsFromList( + AZStd::span inPositions, + const SurfaceTagVector& desiredTags, + SurfacePointLists& surfacePointLists) const override; SurfaceDataRegistryHandle RegisterSurfaceDataProvider(const SurfaceDataRegistryEntry& entry) override; void UnregisterSurfaceDataProvider(const SurfaceDataRegistryHandle& handle) override; diff --git a/Gems/SurfaceData/Code/Tests/SurfaceDataBenchmarks.cpp b/Gems/SurfaceData/Code/Tests/SurfaceDataBenchmarks.cpp new file mode 100644 index 0000000000..65487c155b --- /dev/null +++ b/Gems/SurfaceData/Code/Tests/SurfaceDataBenchmarks.cpp @@ -0,0 +1,276 @@ +/* + * 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 + * + */ + +#ifdef HAVE_BENCHMARK + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace UnitTest +{ + class SurfaceDataBenchmark : public ::benchmark::Fixture + { + public: + void internalSetUp() + { + m_surfaceDataSystemEntity = AZStd::make_unique(); + m_surfaceDataSystemEntity->CreateComponent(); + m_surfaceDataSystemEntity->Init(); + m_surfaceDataSystemEntity->Activate(); + } + + void internalTearDown() + { + m_surfaceDataSystemEntity.reset(); + } + + // Create an entity with a Transform component and a SurfaceDataShape component at the given position with the given tags. + AZStd::unique_ptr CreateBenchmarkEntity( + AZ::Vector3 worldPos, AZStd::span providerTags, AZStd::span modifierTags) + { + AZStd::unique_ptr entity = AZStd::make_unique(); + + auto transform = entity->CreateComponent(); + transform->SetLocalTM(AZ::Transform::CreateTranslation(worldPos)); + transform->SetWorldTM(AZ::Transform::CreateTranslation(worldPos)); + + SurfaceData::SurfaceDataShapeConfig surfaceConfig; + for (auto& providerTag : providerTags) + { + surfaceConfig.m_providerTags.push_back(SurfaceData::SurfaceTag(providerTag)); + } + for (auto& modifierTag : modifierTags) + { + surfaceConfig.m_modifierTags.push_back(SurfaceData::SurfaceTag(modifierTag)); + } + entity->CreateComponent(surfaceConfig); + + return entity; + } + + /* Create a set of shape surfaces in the world that we can use for benchmarking. + Each shape is centered in XY and is the XY size of the world, but with different Z heights and placements. + There are two boxes and one cylinder, layered like this: + + Top: + --- + |O| <- two boxes of equal XY size with a cylinder face-up in the center + --- + + Side: + |-----------| + | |<- entity 3, box that contains the other shapes + | |-------| | <- entity 2, cylinder inside entity 3 and intersecting entity 1 + | | | | + |-----------|<- entity 1, thin box + | |-------| | + | | + |-----------| + + This will give us either 2 or 3 generated surface points at every query point. The entity 1 surface will get the entity 2 and 3 + modifier tags added to it. The entity 2 surface will get the entity 3 modifier tag added to it. The entity 3 surface won't get + modified. + */ + AZStd::vector> CreateBenchmarkEntities(float worldSize) + { + AZStd::vector> testEntities; + float halfWorldSize = worldSize / 2.0f; + + // Create a large flat box with 1 provider tag. + AZStd::unique_ptr surface1 = CreateBenchmarkEntity( + AZ::Vector3(halfWorldSize, halfWorldSize, 10.0f), AZStd::array{ "surface1" }, {}); + { + LmbrCentral::BoxShapeConfig boxConfig(AZ::Vector3(worldSize, worldSize, 1.0f)); + auto shapeComponent = surface1->CreateComponent(LmbrCentral::BoxShapeComponentTypeId); + shapeComponent->SetConfiguration(boxConfig); + + surface1->Init(); + surface1->Activate(); + } + testEntities.push_back(AZStd::move(surface1)); + + // Create a large cylinder with 1 provider tag and 1 modifier tag. + AZStd::unique_ptr surface2 = CreateBenchmarkEntity( + AZ::Vector3(halfWorldSize, halfWorldSize, 20.0f), AZStd::array{ "surface2" }, AZStd::array{ "modifier2" }); + { + LmbrCentral::CylinderShapeConfig cylinderConfig; + cylinderConfig.m_height = 30.0f; + cylinderConfig.m_radius = halfWorldSize; + auto shapeComponent = surface2->CreateComponent(LmbrCentral::CylinderShapeComponentTypeId); + shapeComponent->SetConfiguration(cylinderConfig); + + surface2->Init(); + surface2->Activate(); + } + testEntities.push_back(AZStd::move(surface2)); + + // Create a large box with 1 provider tag and 1 modifier tag. + AZStd::unique_ptr surface3 = CreateBenchmarkEntity( + AZ::Vector3(halfWorldSize, halfWorldSize, 30.0f), AZStd::array{ "surface3" }, AZStd::array{ "modifier3" }); + { + LmbrCentral::BoxShapeConfig boxConfig(AZ::Vector3(worldSize, worldSize, 100.0f)); + auto shapeComponent = surface3->CreateComponent(LmbrCentral::BoxShapeComponentTypeId); + shapeComponent->SetConfiguration(boxConfig); + + surface3->Init(); + surface3->Activate(); + } + testEntities.push_back(AZStd::move(surface3)); + + return testEntities; + } + + SurfaceData::SurfaceTagVector CreateBenchmarkTagFilterList() + { + SurfaceData::SurfaceTagVector tagFilterList; + tagFilterList.emplace_back("surface1"); + tagFilterList.emplace_back("surface2"); + tagFilterList.emplace_back("surface3"); + tagFilterList.emplace_back("modifier2"); + tagFilterList.emplace_back("modifier3"); + return tagFilterList; + } + + protected: + void SetUp([[maybe_unused]] const benchmark::State& state) override + { + internalSetUp(); + } + void SetUp([[maybe_unused]] benchmark::State& state) override + { + internalSetUp(); + } + + void TearDown([[maybe_unused]] const benchmark::State& state) override + { + internalTearDown(); + } + void TearDown([[maybe_unused]] benchmark::State& state) override + { + internalTearDown(); + } + + AZStd::unique_ptr m_surfaceDataSystemEntity; + }; + + BENCHMARK_DEFINE_F(SurfaceDataBenchmark, BM_GetSurfacePoints)(benchmark::State& state) + { + AZ_PROFILE_FUNCTION(Entity); + + // Create our benchmark world + const float worldSize = aznumeric_cast(state.range(0)); + AZStd::vector> benchmarkEntities = CreateBenchmarkEntities(worldSize); + SurfaceData::SurfaceTagVector filterTags = CreateBenchmarkTagFilterList(); + + // Query every point in our world at 1 meter intervals. + for (auto _ : state) + { + // This is declared outside the loop so that the list of points doesn't fully reallocate on every query. + SurfaceData::SurfacePointList points; + + for (float y = 0.0f; y < worldSize; y += 1.0f) + { + for (float x = 0.0f; x < worldSize; x += 1.0f) + { + AZ::Vector3 queryPosition(x, y, 0.0f); + points.clear(); + + SurfaceData::SurfaceDataSystemRequestBus::Broadcast( + &SurfaceData::SurfaceDataSystemRequestBus::Events::GetSurfacePoints, queryPosition, filterTags, points); + benchmark::DoNotOptimize(points); + } + } + } + } + + BENCHMARK_DEFINE_F(SurfaceDataBenchmark, BM_GetSurfacePointsFromRegion)(benchmark::State& state) + { + AZ_PROFILE_FUNCTION(Entity); + + // Create our benchmark world + float worldSize = aznumeric_cast(state.range(0)); + AZStd::vector> benchmarkEntities = CreateBenchmarkEntities(worldSize); + SurfaceData::SurfaceTagVector filterTags = CreateBenchmarkTagFilterList(); + + // Query every point in our world at 1 meter intervals. + for (auto _ : state) + { + SurfaceData::SurfacePointLists points; + + AZ::Aabb inRegion = AZ::Aabb::CreateFromMinMax(AZ::Vector3(0.0f), AZ::Vector3(worldSize)); + AZ::Vector2 stepSize(1.0f); + SurfaceData::SurfaceDataSystemRequestBus::Broadcast( + &SurfaceData::SurfaceDataSystemRequestBus::Events::GetSurfacePointsFromRegion, inRegion, stepSize, filterTags, + points); + benchmark::DoNotOptimize(points); + } + } + + BENCHMARK_DEFINE_F(SurfaceDataBenchmark, BM_GetSurfacePointsFromList)(benchmark::State& state) + { + AZ_PROFILE_FUNCTION(Entity); + + // Create our benchmark world + const float worldSize = aznumeric_cast(state.range(0)); + const int64_t worldSizeInt = state.range(0); + AZStd::vector> benchmarkEntities = CreateBenchmarkEntities(worldSize); + SurfaceData::SurfaceTagVector filterTags = CreateBenchmarkTagFilterList(); + + // Query every point in our world at 1 meter intervals. + for (auto _ : state) + { + AZStd::vector queryPositions; + queryPositions.reserve(worldSizeInt * worldSizeInt); + + for (float y = 0.0f; y < worldSize; y += 1.0f) + { + for (float x = 0.0f; x < worldSize; x += 1.0f) + { + queryPositions.emplace_back(x, y, 0.0f); + } + } + + SurfaceData::SurfacePointLists points; + + SurfaceData::SurfaceDataSystemRequestBus::Broadcast( + &SurfaceData::SurfaceDataSystemRequestBus::Events::GetSurfacePointsFromList, queryPositions, filterTags, points); + benchmark::DoNotOptimize(points); + } + } + + BENCHMARK_REGISTER_F(SurfaceDataBenchmark, BM_GetSurfacePoints) + ->Arg( 1024 ) + ->Arg( 2048 ) + ->Unit(::benchmark::kMillisecond); + + BENCHMARK_REGISTER_F(SurfaceDataBenchmark, BM_GetSurfacePointsFromRegion) + ->Arg( 1024 ) + ->Arg( 2048 ) + ->Unit(::benchmark::kMillisecond); + + BENCHMARK_REGISTER_F(SurfaceDataBenchmark, BM_GetSurfacePointsFromList) + ->Arg( 1024 ) + ->Arg( 2048 ) + ->Unit(::benchmark::kMillisecond); + +#endif +} + + diff --git a/Gems/SurfaceData/Code/Tests/SurfaceDataColliderComponentTest.cpp b/Gems/SurfaceData/Code/Tests/SurfaceDataColliderComponentTest.cpp index 9b31c98759..b0aa5dd38f 100644 --- a/Gems/SurfaceData/Code/Tests/SurfaceDataColliderComponentTest.cpp +++ b/Gems/SurfaceData/Code/Tests/SurfaceDataColliderComponentTest.cpp @@ -12,7 +12,6 @@ #include #include #include -#include #include #include @@ -23,23 +22,6 @@ namespace UnitTest { - // Mock out a generic Physics Collider Component, which is a required dependency for adding a SurfaceDataColliderComponent. - struct MockPhysicsColliderComponent - : public AZ::Component - { - public: - AZ_COMPONENT(MockPhysicsColliderComponent, "{4F7C36DE-6475-4E0A-96A7-BFAF21C07C95}", AZ::Component); - - void Activate() override {} - void Deactivate() override {} - - static void Reflect(AZ::ReflectContext* reflect) { AZ_UNUSED(reflect); } - static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided) - { - provided.push_back(AZ_CRC("PhysXColliderService", 0x4ff43f7c)); - } - }; - class MockPhysicsWorldBusProvider : public AzPhysics::SimulatedBodyComponentRequestsBus::Handler { diff --git a/Gems/SurfaceData/Code/Tests/SurfaceDataTest.cpp b/Gems/SurfaceData/Code/Tests/SurfaceDataTest.cpp index 177abd3824..1d339edc71 100644 --- a/Gems/SurfaceData/Code/Tests/SurfaceDataTest.cpp +++ b/Gems/SurfaceData/Code/Tests/SurfaceDataTest.cpp @@ -8,7 +8,6 @@ #include -#include #include #include #include @@ -22,6 +21,7 @@ #include #include #include +#include // Simple class for mocking out a surface provider, so that we can control exactly what points we expect to query in our tests. // This can be used to either provide a surface or modify a surface. @@ -175,61 +175,33 @@ class MockSurfaceProvider }; - - - TEST(SurfaceDataTest, ComponentsWithComponentApplication) { - AZ::ComponentApplication::Descriptor appDesc; - appDesc.m_memoryBlocksByteSize = 10 * 1024 * 1024; - appDesc.m_recordingMode = AZ::Debug::AllocationRecords::RECORD_FULL; - appDesc.m_stackRecordLevels = 20; - - AZ::ComponentApplication app; - AZ::Entity* systemEntity = app.Create(appDesc); - ASSERT_TRUE(systemEntity != nullptr); - app.RegisterComponentDescriptor(SurfaceData::SurfaceDataSystemComponent::CreateDescriptor()); - systemEntity->CreateComponent(); - - systemEntity->Init(); - systemEntity->Activate(); - - app.Destroy(); - ASSERT_TRUE(true); + AZ::Entity* testSystemEntity = new AZ::Entity(); + testSystemEntity->CreateComponent(); + + testSystemEntity->Init(); + testSystemEntity->Activate(); + EXPECT_EQ(testSystemEntity->GetState(), AZ::Entity::State::Active); + testSystemEntity->Deactivate(); + delete testSystemEntity; } class SurfaceDataTestApp : public ::testing::Test { public: - SurfaceDataTestApp() - : m_application() - , m_systemEntity(nullptr) - { - } - void SetUp() override { - AZ::ComponentApplication::Descriptor appDesc; - appDesc.m_memoryBlocksByteSize = 50 * 1024 * 1024; - appDesc.m_recordingMode = AZ::Debug::AllocationRecords::RECORD_FULL; - appDesc.m_stackRecordLevels = 20; - - AZ::ComponentApplication::StartupParameters appStartup; - appStartup.m_createStaticModulesCallback = - [](AZStd::vector& modules) - { - modules.emplace_back(new SurfaceData::SurfaceDataModule); - }; - - m_systemEntity = m_application.Create(appDesc, appStartup); - m_systemEntity->Init(); - m_systemEntity->Activate(); + m_surfaceDataSystemEntity = AZStd::make_unique(); + m_surfaceDataSystemEntity->CreateComponent(); + m_surfaceDataSystemEntity->Init(); + m_surfaceDataSystemEntity->Activate(); } void TearDown() override { - m_application.Destroy(); + m_surfaceDataSystemEntity.reset(); } bool ValidateRegionListSize(AZ::Aabb bounds, AZ::Vector2 stepSize, const SurfaceData::SurfacePointLists& outputLists) @@ -240,15 +212,43 @@ public: return (outputLists.size() == aznumeric_cast(ceil(bounds.GetXExtent() * stepSize.GetX()) * ceil(bounds.GetYExtent() * stepSize.GetY()))); } + void CompareSurfacePointListWithGetSurfacePoints( + SurfaceData::SurfacePointLists surfacePointLists, const SurfaceData::SurfaceTagVector& testTags) + { + for (auto& pointList : surfacePointLists) + { + AZ::Vector3 queryPosition(pointList[0].m_position.GetX(), pointList[0].m_position.GetY(), 16.0f); + SurfaceData::SurfacePointList singleQueryPointList; + + SurfaceData::SurfaceDataSystemRequestBus::Broadcast( + &SurfaceData::SurfaceDataSystemRequestBus::Events::GetSurfacePoints, queryPosition, testTags, singleQueryPointList); + + // Verify the two point lists are the same size, then verify that each point in each list is equal. + ASSERT_EQ(pointList.size(), singleQueryPointList.size()); + for (size_t index = 0; index < pointList.size(); index++) + { + SurfaceData::SurfacePoint& point1 = pointList[index]; + SurfaceData::SurfacePoint& point2 = singleQueryPointList[index]; + + EXPECT_EQ(point1.m_entityId, point2.m_entityId); + EXPECT_EQ(point1.m_position, point2.m_position); + EXPECT_EQ(point1.m_normal, point2.m_normal); + ASSERT_EQ(point1.m_masks.size(), point2.m_masks.size()); + for (auto& mask : point1.m_masks) + { + EXPECT_EQ(mask.second, point2.m_masks[mask.first]); + } + } + } + } - AZ::ComponentApplication m_application; - AZ::Entity* m_systemEntity; // Test Surface Data tags that we can use for testing query functionality const AZ::Crc32 m_testSurface1Crc = AZ::Crc32("test_surface1"); const AZ::Crc32 m_testSurface2Crc = AZ::Crc32("test_surface2"); const AZ::Crc32 m_testSurfaceNoMatchCrc = AZ::Crc32("test_surface_no_match"); + AZStd::unique_ptr m_surfaceDataSystemEntity; }; TEST_F(SurfaceDataTestApp, SurfaceData_TestRegisteredTags) @@ -706,4 +706,64 @@ TEST_F(SurfaceDataTestApp, SurfaceData_TestSurfacePointsFromRegion_DissimilarPoi } } -AZ_UNIT_TEST_HOOK(DEFAULT_UNIT_TEST_ENV); +TEST_F(SurfaceDataTestApp, SurfaceData_VerifyGetSurfacePointsFromRegionAndGetSurfacePointsMatch) +{ + // This ensures that both GetSurfacePointsFromRegion and GetSurfacePoints produce the same results. + + // Create a mock Surface Provider that covers from (0, 0) - (8, 8) in space. + // It defines points spaced 0.25 apart, with heights of 0 and 4, and with the tags "test_surface1" and "test_surface2". + // (We're creating points spaced more densely than we'll query just to verify that we only get back the queried points) + SurfaceData::SurfaceTagVector providerTags = { SurfaceData::SurfaceTag(m_testSurface1Crc), SurfaceData::SurfaceTag(m_testSurface2Crc) }; + MockSurfaceProvider mockProvider( + MockSurfaceProvider::ProviderType::SURFACE_PROVIDER, providerTags, AZ::Vector3(0.0f), AZ::Vector3(8.0f), + AZ::Vector3(0.25f, 0.25f, 4.0f)); + + // Query for all the surface points from (0, 0, 16) - (4, 4, 16) with a step size of 1. + SurfaceData::SurfacePointLists availablePointsPerPosition; + AZ::Vector2 stepSize(1.0f, 1.0f); + AZ::Aabb regionBounds = AZ::Aabb::CreateFromMinMax(AZ::Vector3(0.0f, 0.0f, 16.0f), AZ::Vector3(4.0f, 4.0f, 16.0f)); + + SurfaceData::SurfaceDataSystemRequestBus::Broadcast( + &SurfaceData::SurfaceDataSystemRequestBus::Events::GetSurfacePointsFromRegion, regionBounds, stepSize, providerTags, + availablePointsPerPosition); + + EXPECT_TRUE(ValidateRegionListSize(regionBounds, stepSize, availablePointsPerPosition)); + + // For each point entry returned from GetSurfacePointsFromRegion, call GetSurfacePoints and verify the results match. + CompareSurfacePointListWithGetSurfacePoints(availablePointsPerPosition, providerTags); +} + +TEST_F(SurfaceDataTestApp, SurfaceData_VerifyGetSurfacePointsFromListAndGetSurfacePointsMatch) +{ + // This ensures that both GetSurfacePointsFromList and GetSurfacePoints produce the same results. + + // Create a mock Surface Provider that covers from (0, 0) - (8, 8) in space. + // It defines points spaced 0.25 apart, with heights of 0 and 4, and with the tags "test_surface1" and "test_surface2". + // (We're creating points spaced more densely than we'll query just to verify that we only get back the queried points) + SurfaceData::SurfaceTagVector providerTags = { SurfaceData::SurfaceTag(m_testSurface1Crc), SurfaceData::SurfaceTag(m_testSurface2Crc) }; + MockSurfaceProvider mockProvider( + MockSurfaceProvider::ProviderType::SURFACE_PROVIDER, providerTags, AZ::Vector3(0.0f), AZ::Vector3(8.0f), + AZ::Vector3(0.25f, 0.25f, 4.0f)); + + // Query for all the surface points from (0, 0, 16) - (4, 4, 16) with a step size of 1. + SurfaceData::SurfacePointLists availablePointsPerPosition; + AZStd::vector queryPositions; + for (float y = 0.0f; y < 4.0f; y += 1.0f) + { + for (float x = 0.0f; x < 4.0f; x += 1.0f) + { + queryPositions.push_back(AZ::Vector3(x, y, 16.0f)); + } + } + + SurfaceData::SurfaceDataSystemRequestBus::Broadcast( + &SurfaceData::SurfaceDataSystemRequestBus::Events::GetSurfacePointsFromList, + queryPositions, providerTags, availablePointsPerPosition); + + EXPECT_EQ(availablePointsPerPosition.size(), 16); + + // For each point entry returned from GetSurfacePointsFromList, call GetSurfacePoints and verify the results match. + CompareSurfacePointListWithGetSurfacePoints(availablePointsPerPosition, providerTags); +} +// This uses custom test / benchmark hooks so that we can load LmbrCentral and use Shape components in our unit tests and benchmarks. +AZ_UNIT_TEST_HOOK(new UnitTest::SurfaceDataTestEnvironment, UnitTest::SurfaceDataBenchmarkEnvironment); diff --git a/Gems/SurfaceData/Code/Tests/SurfaceDataTestFixtures.cpp b/Gems/SurfaceData/Code/Tests/SurfaceDataTestFixtures.cpp new file mode 100644 index 0000000000..2ec531d2e4 --- /dev/null +++ b/Gems/SurfaceData/Code/Tests/SurfaceDataTestFixtures.cpp @@ -0,0 +1,36 @@ +/* + * 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 +#include + +#include +#include +#include +#include + + +namespace UnitTest +{ + void SurfaceDataTestEnvironment::AddGemsAndComponents() + { + AddDynamicModulePaths({ "LmbrCentral" }); + + AddComponentDescriptors({ + AzFramework::TransformComponent::CreateDescriptor(), + + SurfaceData::SurfaceDataSystemComponent::CreateDescriptor(), + SurfaceData::SurfaceDataColliderComponent::CreateDescriptor(), + SurfaceData::SurfaceDataShapeComponent::CreateDescriptor(), + + MockPhysicsColliderComponent::CreateDescriptor() + }); + } +} + diff --git a/Gems/SurfaceData/Code/Tests/SurfaceDataTestFixtures.h b/Gems/SurfaceData/Code/Tests/SurfaceDataTestFixtures.h new file mode 100644 index 0000000000..fc27effd8b --- /dev/null +++ b/Gems/SurfaceData/Code/Tests/SurfaceDataTestFixtures.h @@ -0,0 +1,43 @@ +/* + * 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 + * + */ +#pragma once + +#include + +namespace UnitTest +{ + // SurfaceData needs to use the GemTestEnvironment to load the LmbrCentral Gem so that Shape components can be used + // in the unit tests and benchmarks. + class SurfaceDataTestEnvironment + : public AZ::Test::GemTestEnvironment + { + public: + void AddGemsAndComponents() override; + }; + +#ifdef HAVE_BENCHMARK + //! The Benchmark environment is used for one time setup and tear down of shared resources + class SurfaceDataBenchmarkEnvironment + : public AZ::Test::BenchmarkEnvironmentBase + , public SurfaceDataTestEnvironment + + { + protected: + void SetUpBenchmark() override + { + SetupEnvironment(); + } + + void TearDownBenchmark() override + { + TeardownEnvironment(); + } + }; +#endif + +} diff --git a/Gems/SurfaceData/Code/surfacedata_tests_files.cmake b/Gems/SurfaceData/Code/surfacedata_tests_files.cmake index a65d51c47c..a334f7bf40 100644 --- a/Gems/SurfaceData/Code/surfacedata_tests_files.cmake +++ b/Gems/SurfaceData/Code/surfacedata_tests_files.cmake @@ -8,8 +8,11 @@ set(FILES Include/SurfaceData/Tests/SurfaceDataTestMocks.h + Tests/SurfaceDataBenchmarks.cpp Tests/SurfaceDataColliderComponentTest.cpp Tests/SurfaceDataTest.cpp + Tests/SurfaceDataTestFixtures.cpp + Tests/SurfaceDataTestFixtures.h Source/SurfaceDataModule.cpp Source/SurfaceDataModule.h ) diff --git a/Gems/Vegetation/Code/Tests/VegetationMocks.h b/Gems/Vegetation/Code/Tests/VegetationMocks.h index 8a8bfe6b76..c1d83fb68b 100644 --- a/Gems/Vegetation/Code/Tests/VegetationMocks.h +++ b/Gems/Vegetation/Code/Tests/VegetationMocks.h @@ -345,6 +345,13 @@ namespace UnitTest { } + void GetSurfacePointsFromList( + [[maybe_unused]] AZStd::span inPositions, + [[maybe_unused]] const SurfaceData::SurfaceTagVector& desiredTags, + [[maybe_unused]] SurfaceData::SurfacePointLists& surfacePointLists) const override + { + } + SurfaceData::SurfaceDataRegistryHandle RegisterSurfaceDataProvider([[maybe_unused]] const SurfaceData::SurfaceDataRegistryEntry& entry) override { ++m_count;