Files
o3de/Gems/EMotionFX/Code/Tests/UI/LODSkinnedMeshTests.cpp
T
Chris Burel 8884227fe6 Remove MCore::Array
This translates all usages of MCore::Array to AZStd::vector. It is
designed to be as minimal of a change as possible (no changing to
range-for loops or other C++11 stuff).

We can decide to submit this wholesale, or submit it to a separate
branch that we can then integrate individual files from once we're ready
to do a specific class's transition.

It does not completely solve the `uint32`->`size_t` transition.

One important finding from doing this: `MCore::Array` uses a `memcpy`
when it reallocates. `AZStd::vector` will use the contained type's copy
or move constructor, per element. This is a significant change in
behavior. If you have type, `SomeStruct` that defines a destructor, that
type is copyable and not movable. So if you have a
`MCore::Array<SomeStruct>`, and you call `Add(); Add(); Add()`, that
reallocates 3 times, copying the contents using `memcpy`, and never
invokes `SomeStruct`'s copy constructor or destructor. Translating that
to `AZStd::vector<SomeStruct>` and calling `push_back(); push_back();
push_back();` will still reallocate 3 times, but it sees that
`SomeStruct` is non-movable, and uses the copy constructor to make the
copies, and then the destructor on the previous values. This call to the
destructor wasn't there before, and can cause things to be deleted that
weren't before. The solution to this is to make that struct be a
move-only type. Where possible, this was done by changing that type to
use `AZStd::unique_ptr` instead of a raw pointer, to get the proper move
behavior. Where that is not possible (types that inherit from
`MCore::MemoryObject`), a hand-written move constructor was created.

In general:
GetLength() becomes size()
GetMaxLength() becomes capacity()
GetIsEmpty() becomes empty()
Reserve() becomes reserve()
ReserveExact() becomes reserve()
Resize() becomes resize()
ResizeFast() becomes resize_no_construct()
Add() becomes emplace_back()
AddExact() becomes emplace_back()
AddEmpty() becomes emplace_back()
AddEmptyExact() becomes emplace_back()
GetPtr() becomes data()
GetItem() becomes at()
Shrink() becomes shrink_to_fit()
GetFirst() becomes front()
GetLast() becomes back()
Remove() becomes erase()
RemoveFirst() becomes erase()
RemoveLast() becomes pop_back()
RemoveByValue() becomes if (const auto it = AZStd::find(...); it != end(container)) container.erase(it);
Insert() becomes emplace()
Swap() becomes swap()
Clear(true) becomes clear(); shrink_to_fit()
Clear() becomes clear(); shrink_to_fit()
Clear(false) becomes clear()
Swap() becomes swap()
Find() becomes AZStd::find
MoveElements() becomes AZStd::move
SetMemoryCategory() is removed

Signed-off-by: Chris Burel <burelc@amazon.com>
2021-08-09 08:36:43 -07:00

217 lines
7.8 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 <gtest/gtest.h>
#include <QtTest>
#include <QTreeView>
#include <QTreeWidget>
#include <AzCore/Component/TickBus.h>
#include <AzFramework/Components/TransformComponent.h>
#include <Integration/Components/ActorComponent.h>
#include <Integration/Components/SimpleLODComponent.h>
#include <Integration/Rendering/RenderBackend.h>
#include <Integration/Rendering/RenderBackendManager.h>
#include <AzToolsFramework/UI/PropertyEditor/ReflectedPropertyEditor.hxx>
#include <AzToolsFramework/UI/PropertyEditor/PropertyRowWidget.hxx>
#include <Editor/Plugins/SkeletonOutliner/SkeletonOutlinerPlugin.h>
#include <EMotionFX/Source/Material.h>
#include <EMotionFX/Source/StandardMaterial.h>
#include <EMotionStudio/EMStudioSDK/Source/EMStudioManager.h>
#include <EMotionStudio/EMStudioSDK/Source/NodeHierarchyWidget.h>
#include <EMotionStudio/Plugins/StandardPlugins/Source/NodeWindow/NodeWindowPlugin.h>
#include <Tests/TestAssetCode/ActorFactory.h>
#include <Tests/TestAssetCode/SimpleActors.h>
#include <Tests/TestAssetCode/TestActorAssets.h>
#include <Tests/UI/UIFixture.h>
#include <Editor/ReselectingTreeView.h>
#include <Mocks/IRendererMock.h>
#include <Mocks/ISystemMock.h>
namespace EMotionFX
{
class LODSkinnedMeshFixture
: public ::testing::WithParamInterface <int>
, public UIFixture
{
public:
};
class LODSystemMock : public SystemMock
{
};
class LODSkinnedMeshColorFixture
: public UIFixture
{
public:
void SetUp() override
{
UIFixture::SetUp();
m_app.RegisterComponentDescriptor(Integration::SimpleLODComponent::CreateDescriptor());
m_app.RegisterComponentDescriptor(Integration::ActorComponent::CreateDescriptor());
m_app.RegisterComponentDescriptor(AzFramework::TransformComponent::CreateDescriptor());
m_envPrev = gEnv;
m_env.pSystem = &m_data.m_system;
gEnv = &m_env;
}
void TearDown() override
{
UIFixture::TearDown();
gEnv = m_envPrev;
}
struct DataMembers
{
testing::NiceMock<IRendererMock> m_renderer;
testing::NiceMock<LODSystemMock> m_system;
};
SSystemGlobalEnvironment* m_envPrev = nullptr;
SSystemGlobalEnvironment m_env;
DataMembers m_data;
};
AZStd::unique_ptr<Actor> CreateLODActor(int numLODs)
{
AZStd::unique_ptr<Actor> actor = ActorFactory::CreateAndInit<PlaneActor>("LODSkinnedMeshTestsActor");
// Modify the actor to have numLODs LOD levels.
Mesh* lodMesh = actor->GetMesh(0, 0);
StandardMaterial* dummyMat = StandardMaterial::Create("Dummy Material");
actor->AddMaterial(0, dummyMat); // owns the material
for (int i = 1; i < numLODs; ++i)
{
actor->InsertLODLevel(i);
actor->SetMesh(i, 0, lodMesh->Clone());
dummyMat->SetAmbient(MCore::RGBAColor{ i * 20.f });
actor->AddMaterial(i, dummyMat->Clone());
}
return actor;
}
class LODPropertyRowWidget
: public AzToolsFramework::PropertyRowWidget
{
public:
QLabel* GetDefaultLabel() { return m_defaultLabel; }
};
TEST_P(LODSkinnedMeshFixture, CheckLODLevels)
{
const int numLODs = GetParam();
RecordProperty("test_case_id", "C29202698");
AutoRegisteredActor actor = CreateLODActor(numLODs);
ActorInstance* actorInstance = ActorInstance::Create(actor.get());
// Change the Editor mode to Character
EMStudio::GetMainWindow()->ApplicationModeChanged("Character");
// Find the NodeWindowPlugin
auto nodeWindow = static_cast<EMStudio::NodeWindowPlugin*>(EMStudio::GetPluginManager()->FindActivePlugin(EMStudio::NodeWindowPlugin::CLASS_ID));
EXPECT_TRUE(nodeWindow) << "NodeWidow plugin not found!";
// Select the newly created actor instance
AZStd::string result;
EXPECT_TRUE(CommandSystem::GetCommandManager()->ExecuteCommand(AZStd::string{ "Select -actorInstanceID " } +AZStd::to_string(actorInstance->GetID()), result)) << result.c_str();
QTreeWidget* treeWidget = nodeWindow->GetDockWidget()->findChild<EMStudio::NodeHierarchyWidget*>("EMFX.NodeWindowPlugin.NodeHierarchyWidget.HierarchyWidget")->GetTreeWidget();
EXPECT_TRUE(treeWidget);
// Select the node containing the mesh
const QTreeWidgetItem* actorItem = treeWidget->topLevelItem(0);
ASSERT_TRUE(actorItem);
QTreeWidgetItem* meshNodeItem = actorItem->child(0);
ASSERT_TRUE(meshNodeItem);
EXPECT_STREQ(meshNodeItem->text(0).toStdString().data(), "rootJoint");
treeWidget->setCurrentItem(meshNodeItem, 0, QItemSelectionModel::Select);
// Get the property widget that holds ReflectedPropertyEditor
AzToolsFramework::ReflectedPropertyEditor* propertyWidget = nodeWindow->GetDockWidget()->findChild<AzToolsFramework::ReflectedPropertyEditor*>("EMFX.NodeWindowPlugin.ReflectedPropertyEditor.PropertyWidget");
auto* finalRowWidget = static_cast<LODPropertyRowWidget *>(GetNamedPropertyRowWidgetFromReflectedPropertyEditor(propertyWidget, "Meshes by lod"));
ASSERT_TRUE(finalRowWidget);
// The default label holds the number of LODs found.
const QString defaultString = finalRowWidget->GetDefaultLabel()->text();
const QString testString = QString("%1 elements").arg(numLODs);
EXPECT_TRUE(testString == defaultString);
}
INSTANTIATE_TEST_CASE_P(LODSkinnedMeshFixtureTests, LODSkinnedMeshFixture, ::testing::Range<int>(1, 7));
// TODO: Re-enabled the test when we can access viewport context in the SimpleLODComponent.
TEST_F(LODSkinnedMeshColorFixture, DISABLED_CheckLODDistanceChange)
{
const int numLODs = 6;
RecordProperty("test_case_id", "C29202698");
AZ::EntityId entityId(740216387);
auto gameEntity = AZStd::make_unique<AZ::Entity>();
gameEntity->SetId(entityId);
AZ::Data::AssetId actorAssetId("{85D3EF54-7400-43F8-8A40-F6BCBF534E54}");
AZStd::unique_ptr<Actor> actor = CreateLODActor(numLODs);
AZ::Data::Asset<Integration::ActorAsset> actorAsset = TestActorAssets::GetAssetFromActor(actorAssetId, AZStd::move(actor));
gameEntity->CreateComponent<AzFramework::TransformComponent>();
Integration::ActorComponent::Configuration actorConf;
actorConf.m_actorAsset = actorAsset;
Integration::ActorComponent* actorComponent = gameEntity->CreateComponent<Integration::ActorComponent>(&actorConf);
Integration::SimpleLODComponent::Configuration lodConf;
lodConf.GenerateDefaultValue(numLODs);
gameEntity->CreateComponent<Integration::SimpleLODComponent>(&lodConf);
gameEntity->Init();
gameEntity->Activate();
actorComponent->SetActorAsset(actorAsset);
ActorInstance* actorInstance = actorComponent->GetActorInstance();
EXPECT_TRUE(actorInstance);
// Tick!
AZ::TickBus::Broadcast(&AZ::TickBus::Events::OnTick, 0.0f, AZ::ScriptTimePoint{});
EXPECT_EQ(actorInstance->GetLODLevel(), 0);
Vec3 newVec{ 0,30,0 };
// Tick!
AZ::TickBus::Broadcast(&AZ::TickBus::Events::OnTick, 0.0f, AZ::ScriptTimePoint{});
actorInstance->UpdateTransformations(0.0f);
EXPECT_EQ(actorInstance->GetLODLevel(), 3);
newVec.y = 50;
// Tick!
AZ::TickBus::Broadcast(&AZ::TickBus::Events::OnTick, 0.0f, AZ::ScriptTimePoint{});
actorInstance->UpdateTransformations(0.0f);
EXPECT_EQ(actorInstance->GetLODLevel(), 5);
gameEntity->Deactivate();
}
}