Merge branch 'development' into Atom/guthadam/material_editor_replace_modified_color_with_indicator

Signed-off-by: Guthrie Adams <guthadam@amazon.com>

# Conflicts:
#	Gems/Atom/Tools/MaterialEditor/Code/Source/Window/MaterialInspector/MaterialInspector.cpp
#	Gems/Atom/Tools/MaterialEditor/Code/Source/Window/MaterialInspector/MaterialInspector.h
#	Gems/AtomLyIntegration/CommonFeatures/Code/Source/Material/EditorMaterialComponentInspector.cpp
#	Gems/AtomLyIntegration/CommonFeatures/Code/Source/Material/EditorMaterialComponentInspector.h
This commit is contained in:
Guthrie Adams
2021-10-01 16:39:57 -05:00
732 changed files with 13356 additions and 10290 deletions
@@ -89,6 +89,9 @@ namespace AZ
// Tracks the asset type used to create the instance.
AssetType m_assetType;
// Boolean to indicate if the instance has been orphaned from the instance database
bool m_isOrphaned = false;
};
/// @cond EXCLUDE_DOCS
@@ -203,6 +203,16 @@ namespace AZ
//! Calls FindOrCreate using a random InstanceId
Data::Instance<Type> Create(const Asset<AssetData>& asset, const AZStd::any* param = nullptr);
/**
* Removes the instance data from the database. Does not release it.
* References to existing instances will remain valid, but new calls to Create/FindOrCreate will create a new instance
* This function is temporary, to provide functionality needed for Model hot-reloading, but will be removed
* once the Model class does not need it anymore.
*
* @param id The id of the instance to remove
*/
void TEMPOrphan(const InstanceId& id);
private:
InstanceDatabase(const AssetType& assetType);
~InstanceDatabase();
@@ -356,6 +366,20 @@ namespace AZ
return FindOrCreate(Data::InstanceId::CreateRandom(), asset, param);
}
template<typename Type>
void InstanceDatabase<Type>::TEMPOrphan(const InstanceId& id)
{
AZStd::scoped_lock<AZStd::recursive_mutex> lock(m_databaseMutex);
// Check if the instance is still in the database, in case it was orphaned twice
auto instanceItr = m_database.find(id);
if (instanceItr != m_database.end())
{
// Mark the instance as orphaned, and remove it from the database
instanceItr->second->m_isOrphaned = true;
m_database.erase(instanceItr);
}
}
template<typename Type>
void InstanceDatabase<Type>::ReleaseInstance(InstanceData* instance, const InstanceId& instanceId)
{
@@ -374,6 +398,12 @@ namespace AZ
m_database.erase(instance->GetId());
m_instanceHandler.m_deleteFunction(static_cast<Type*>(instance));
}
else if (instance->m_isOrphaned && instance->m_useCount.compare_exchange_strong(expectedRefCount, -1))
{
// If the instance was orphaned, it has already been removed from the database,
// but still needs to be deleted when the refcount drops to 0
m_instanceHandler.m_deleteFunction(static_cast<Type*>(instance));
}
}
template<typename Type>
@@ -181,7 +181,76 @@ namespace UnitTest
EXPECT_EQ(instance, instance3);
}
void ParallelInstanceCreateHelper(size_t threadCountMax, size_t assetIdCount, size_t durationSeconds)
TEST_F(InstanceDatabaseTest, InstanceOrphan)
{
auto& assetManager = AssetManager::Instance();
auto& instanceDatabase = InstanceDatabase<TestInstanceA>::Instance();
Asset<TestAssetType> someAsset = assetManager.CreateAsset<TestAssetType>(s_assetId0, AZ::Data::AssetLoadBehavior::Default);
Instance<TestInstanceA> orphanedInstance = instanceDatabase.FindOrCreate(s_instanceId0, someAsset);
EXPECT_NE(orphanedInstance, nullptr);
instanceDatabase.TEMPOrphan(s_instanceId0);
// After orphan, the instance should not be found in the database, but it should still be valid
EXPECT_EQ(instanceDatabase.Find(s_instanceId0), nullptr);
EXPECT_NE(orphanedInstance, nullptr);
instanceDatabase.TEMPOrphan(s_instanceId0);
// Orphaning twice should be a no-op
EXPECT_EQ(instanceDatabase.Find(s_instanceId0), nullptr);
EXPECT_NE(orphanedInstance, nullptr);
Instance<TestInstanceA> instance2 = instanceDatabase.FindOrCreate(s_instanceId0, someAsset);
// Creating another instance with the same id should return a different instance than the one that was orphaned
EXPECT_NE(orphanedInstance, instance2);
}
enum class ParallelInstanceTestCases
{
Create,
CreateAndDeferRemoval,
CreateAndOrphan,
CreateDeferRemovalAndOrphan
};
enum class ParralleInstanceCurrentAction
{
Create,
DeferredRemoval,
Orphan
};
ParralleInstanceCurrentAction ParallelInstanceGetCurrentAction(ParallelInstanceTestCases testCase)
{
switch (testCase)
{
case ParallelInstanceTestCases::CreateAndDeferRemoval:
switch (rand() % 2)
{
case 0: return ParralleInstanceCurrentAction::Create;
case 1: return ParralleInstanceCurrentAction::DeferredRemoval;
}
case ParallelInstanceTestCases::CreateAndOrphan:
switch (rand() % 2)
{
case 0: return ParralleInstanceCurrentAction::Create;
case 1: return ParralleInstanceCurrentAction::Orphan;
}
case ParallelInstanceTestCases::CreateDeferRemovalAndOrphan:
switch (rand() % 3)
{
case 0: return ParralleInstanceCurrentAction::Create;
case 1: return ParralleInstanceCurrentAction::DeferredRemoval;
case 2: return ParralleInstanceCurrentAction::Orphan;
}
case ParallelInstanceTestCases::Create:
default:
return ParralleInstanceCurrentAction::Create;
}
}
void ParallelInstanceCreateHelper(size_t threadCountMax, size_t assetIdCount, float durationSeconds, ParallelInstanceTestCases testCase)
{
printf("Testing threads=%zu assetIds=%zu ... ", threadCountMax, assetIdCount);
@@ -192,6 +261,7 @@ namespace UnitTest
auto& instanceManager = InstanceDatabase<TestInstanceA>::Instance();
AZStd::vector<Uuid> guids;
AZStd::vector<Data::Instance<Data::InstanceData>> instances;
AZStd::vector<Asset<TestAssetType>> assets;
for (size_t i = 0; i < assetIdCount; ++i)
@@ -199,6 +269,7 @@ namespace UnitTest
Uuid guid = Uuid::CreateRandom();
guids.emplace_back(guid);
instances.emplace_back(nullptr);
// Pre-create asset so we don't attempt to load it from the catalog.
assets.emplace_back(assetManager.CreateAsset<TestAssetType>(guid, AZ::Data::AssetLoadBehavior::Default));
@@ -206,6 +277,7 @@ namespace UnitTest
AZStd::vector<AZStd::thread> threads;
AZStd::mutex mutex;
AZStd::mutex referenceTableMutex;
AZStd::atomic<int> threadCount((int)threadCountMax);
AZStd::condition_variable cv;
AZStd::atomic_bool keepDispatching(true);
@@ -225,11 +297,15 @@ namespace UnitTest
for (size_t i = 0; i < threadCountMax; ++i)
{
threads.emplace_back(
[&instanceManager, &threadCount, &cv, &guids, &assets, &durationSeconds]()
[&instanceManager, &threadCount, &cv, &guids, &instances, &assets, &durationSeconds, &testCase, &referenceTableMutex]()
{
AZ::Debug::Timer timer;
timer.Stamp();
bool deferRemoval = testCase == ParallelInstanceTestCases::CreateAndDeferRemoval ||
testCase == ParallelInstanceTestCases::CreateDeferRemovalAndOrphan
? true : false;
while (timer.GetDeltaTimeInSeconds() < durationSeconds)
{
const size_t index = rand() % guids.size();
@@ -237,11 +313,36 @@ namespace UnitTest
const InstanceId instanceId{ uuid };
const AssetId assetId{ uuid };
Instance<TestInstanceA> instance =
instanceManager.FindOrCreate(instanceId, Asset<TestAssetType>(assetId, azrtti_typeid<TestAssetType>()));
EXPECT_NE(instance, nullptr);
EXPECT_EQ(instance->GetId(), instanceId);
EXPECT_EQ(instance->m_asset, assets[index]);
ParralleInstanceCurrentAction currentAction = ParallelInstanceGetCurrentAction(testCase);
if (currentAction == ParralleInstanceCurrentAction::Orphan)
{
// Orphan the instance, but don't decrease its refcount
instanceManager.TEMPOrphan(instanceId);
}
else if (currentAction == ParralleInstanceCurrentAction::DeferredRemoval)
{
// Drop the refcount to zero so the instance will be released
referenceTableMutex.lock();
instances[index] = nullptr;
referenceTableMutex.unlock();
}
else
{
// Otherwise, add a new instance
Instance<TestInstanceA> instance = instanceManager.FindOrCreate(instanceId, assets[index]);
EXPECT_NE(instance, nullptr);
EXPECT_EQ(instance->GetId(), instanceId);
EXPECT_EQ(instance->m_asset, assets[index]);
if (deferRemoval)
{
// Keep a reference to the instance alive so it can be removed later
referenceTableMutex.lock();
instances[index] = instance;
referenceTableMutex.unlock();
}
}
}
threadCount--;
@@ -254,10 +355,12 @@ namespace UnitTest
// Used to detect a deadlock. If we wait for more than 10 seconds, it's likely a deadlock has occurred
while (threadCount > 0 && !timedOut)
{
size_t durationSecondsRoundedUp = static_cast<size_t>(std::ceil(durationSeconds));
AZStd::unique_lock<AZStd::mutex> lock(mutex);
timedOut =
(AZStd::cv_status::timeout ==
cv.wait_until(lock, AZStd::chrono::system_clock::now() + AZStd::chrono::seconds(durationSeconds * 2)));
cv.wait_until(lock, AZStd::chrono::system_clock::now() + AZStd::chrono::seconds(durationSecondsRoundedUp * 2)));
}
EXPECT_TRUE(threadCount == 0) << "One or more threads appear to be deadlocked at " << timer.GetDeltaTimeInSeconds() << " seconds";
@@ -273,11 +376,11 @@ namespace UnitTest
printf("Took %f seconds\n", timer.GetDeltaTimeInSeconds());
}
TEST_F(InstanceDatabaseTest, ParallelInstanceCreate)
void ParallelCreateTest(ParallelInstanceTestCases testCase)
{
// This is the original test scenario from when InstanceDatabase was first implemented
// threads, AssetIds, seconds
ParallelInstanceCreateHelper(8, 100, 5);
ParallelInstanceCreateHelper(8, 100, 5, testCase);
// This value is checked in as 1 so this test doesn't take too much time, but can be increased locally to soak the test.
const size_t attempts = 1;
@@ -289,11 +392,11 @@ namespace UnitTest
// The idea behind this series of tests is that there are two threads sharing one Instance, and both threads try to
// create or release that instance at the same time.
// At the time, this set of scenarios has something like a 10% failure rate.
const size_t duration = 2;
const float duration = 2.0f;
// threads, AssetIds, seconds
ParallelInstanceCreateHelper(2, 1, duration);
ParallelInstanceCreateHelper(4, 1, duration);
ParallelInstanceCreateHelper(8, 1, duration);
ParallelInstanceCreateHelper(2, 1, duration, testCase);
ParallelInstanceCreateHelper(4, 1, duration, testCase);
ParallelInstanceCreateHelper(8, 1, duration, testCase);
}
for (size_t i = 0; i < attempts; ++i)
@@ -301,19 +404,39 @@ namespace UnitTest
printf("Attempt %zu of %zu... \n", i, attempts);
// Here we try a bunch of different threadCount:assetCount ratios to be thorough
const size_t duration = 2;
const float duration = 2.0f;
// threads, AssetIds, seconds
ParallelInstanceCreateHelper(2, 1, duration);
ParallelInstanceCreateHelper(4, 1, duration);
ParallelInstanceCreateHelper(4, 2, duration);
ParallelInstanceCreateHelper(4, 4, duration);
ParallelInstanceCreateHelper(8, 1, duration);
ParallelInstanceCreateHelper(8, 2, duration);
ParallelInstanceCreateHelper(8, 3, duration);
ParallelInstanceCreateHelper(8, 4, duration);
ParallelInstanceCreateHelper(2, 1, duration, testCase);
ParallelInstanceCreateHelper(4, 1, duration, testCase);
ParallelInstanceCreateHelper(4, 2, duration, testCase);
ParallelInstanceCreateHelper(4, 4, duration, testCase);
ParallelInstanceCreateHelper(8, 1, duration, testCase);
ParallelInstanceCreateHelper(8, 2, duration, testCase);
ParallelInstanceCreateHelper(8, 3, duration, testCase);
ParallelInstanceCreateHelper(8, 4, duration, testCase);
}
}
TEST_F(InstanceDatabaseTest, ParallelInstanceCreate)
{
ParallelCreateTest(ParallelInstanceTestCases::Create);
}
TEST_F(InstanceDatabaseTest, ParallelInstanceCreateAndDeferRemoval)
{
ParallelCreateTest(ParallelInstanceTestCases::CreateAndDeferRemoval);
}
TEST_F(InstanceDatabaseTest, ParallelInstanceCreateAndOrphan)
{
ParallelCreateTest(ParallelInstanceTestCases::CreateAndOrphan);
}
TEST_F(InstanceDatabaseTest, ParallelInstanceCreateDeferRemovalAndOrphan)
{
ParallelCreateTest(ParallelInstanceTestCases::CreateDeferRemovalAndOrphan);
}
TEST_F(InstanceDatabaseTest, InstanceCreateNoDatabase)
{
bool m_deleted = false;
@@ -340,6 +340,14 @@ namespace AZ
// (Load jobs will attempt to reuse blocked threads before spinning off new job threads)
ProcessLoadJob();
}
// Pump the AssetBus function queue once more after the load has completed in case additional
// functions have been queued between the last call to DispatchEvents and the completion
// of the current load job
if (m_shouldDispatchEvents)
{
AssetManager::Instance().DispatchEvents();
}
}
void Finish()
@@ -543,8 +551,6 @@ namespace AZ
{
PrepareShutDown();
DispatchEvents();
// Acquire the asset lock to make sure nobody else is trying to do anything fancy with assets
AZStd::scoped_lock<AZStd::recursive_mutex> assetLock(m_assetMutex);
@@ -567,7 +573,10 @@ namespace AZ
{
AZ_PROFILE_FUNCTION(AzCore);
AssetManagerNotificationBus::Broadcast(&AssetManagerNotificationBus::Events::OnAssetEventsDispatchBegin);
AssetBus::ExecuteQueuedEvents();
while (AssetBus::QueuedEventCount())
{
AssetBus::ExecuteQueuedEvents();
}
AssetManagerNotificationBus::Broadcast(&AssetManagerNotificationBus::Events::OnAssetEventsDispatchEnd);
}
@@ -13,6 +13,7 @@
#include <AzCore/Serialization/EditContext.h>
#include <AzCore/RTTI/BehaviorContext.h>
#include <AzCore/Asset/AssetManager.h>
#include <AzCore/Asset/AssetSerializer.h>
#include <AzCore/Serialization/Json/RegistrationContext.h>
#include <AzCore/Slice/SliceAssetHandler.h>
#include <AzCore/Slice/SliceComponent.h>
@@ -8,6 +8,7 @@
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/Asset/AssetManager.h>
#include <AzCore/Asset/AssetSerializer.h>
#include <AzCore/IO/SystemFile.h>
namespace AZ {
@@ -22,6 +22,7 @@
#include <AzCore/Time/TimeSystemComponent.h>
#include <AzCore/Console/LoggerSystemComponent.h>
#include <AzCore/EBus/EventSchedulerSystemComponent.h>
#include <AzCore/Task/TaskGraphSystemComponent.h>
namespace AZ
{
@@ -41,6 +42,7 @@ namespace AZ
TimeSystemComponent::CreateDescriptor(),
LoggerSystemComponent::CreateDescriptor(),
EventSchedulerSystemComponent::CreateDescriptor(),
TaskGraphSystemComponent::CreateDescriptor(),
#if !defined(AZCORE_EXCLUDE_LUA)
ScriptSystemComponent::CreateDescriptor(),
@@ -55,6 +57,7 @@ namespace AZ
azrtti_typeid<TimeSystemComponent>(),
azrtti_typeid<LoggerSystemComponent>(),
azrtti_typeid<EventSchedulerSystemComponent>(),
azrtti_typeid<TaskGraphSystemComponent>(),
};
}
}
@@ -22,6 +22,7 @@
#include <AzCore/Memory/Memory.h>
#include <AzCore/Memory/SystemAllocator.h> // Used as the allocator for most components.
#include <AzCore/Outcome/Outcome.h>
#include <AzCore/std/containers/unordered_set.h>
namespace AZ
{
@@ -74,8 +74,6 @@
#include <AzCore/Module/Environment.h>
#include <AzCore/std/string/conversions.h>
AZ_CVAR(float, g_simulation_tick_rate, 0, nullptr, AZ::ConsoleFunctorFlags::Null, "The rate at which the game simulation tick loop runs, or 0 for as fast as possible");
static void PrintEntityName(const AZ::ConsoleCommandContainer& arguments)
{
if (arguments.empty())
@@ -1396,23 +1394,6 @@ namespace AZ
AZ_PROFILE_SCOPE(AzCore, "ComponentApplication::Tick:OnTick");
EBUS_EVENT(TickBus, OnTick, m_deltaTime, ScriptTimePoint(now));
}
// If tick rate limiting is on, ensure (1 / g_simulation_tick_rate) ms has elapsed since the last frame,
// sleeping if there's still time remaining.
if (g_simulation_tick_rate > 0.f)
{
now = AZStd::chrono::system_clock::now();
// Work in microsecond durations here as that's the native measurement time for time_point
constexpr float microsecondsPerSecond = 1000.f * 1000.f;
const AZStd::chrono::microseconds timeBudgetPerTick(static_cast<int>(microsecondsPerSecond / g_simulation_tick_rate));
AZStd::chrono::microseconds timeUntilNextTick = m_currentTime + timeBudgetPerTick - now;
if (timeUntilNextTick.count() > 0)
{
AZStd::this_thread::sleep_for(timeUntilNextTick);
}
}
}
}
@@ -5,8 +5,7 @@
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#ifndef AZCORE_ENTITY_UTILS_H
#define AZCORE_ENTITY_UTILS_H
#pragma once
#include <AzCore/Component/Entity.h>
#include <AzCore/Debug/Profiler.h>
@@ -217,6 +216,3 @@ namespace AZ
} // namespace EntityUtils
} // namespace AZ
#endif // AZCORE_ENTITY_UTILS_H
#pragma once
@@ -46,8 +46,6 @@ namespace AZ
TICK_PRE_RENDER = 750, ///< Suggested tick handler position to update render-related data.
TICK_RENDER = 800, ///< Suggested tick handler position for rendering.
TICK_DEFAULT = 1000, ///< Default tick handler position when the handler is constructed.
TICK_UI = 2000, ///< Suggested tick handler position for UI components.
@@ -8,6 +8,7 @@
#pragma once
#include <AzCore/Memory/OSAllocator.h>
#include <AzCore/std/smart_ptr/unique_ptr.h>
#include <AzCore/EBus/Policies.h>
+10 -10
View File
@@ -262,17 +262,17 @@ namespace AZ
#else // !AZ_ENABLE_TRACING
#define AZ_Assert(...) AZ_UNUSED(__VA_ARGS__);
#define AZ_Error(...) AZ_UNUSED(__VA_ARGS__);
#define AZ_ErrorOnce(...) AZ_UNUSED(__VA_ARGS__);
#define AZ_Warning(...) AZ_UNUSED(__VA_ARGS__);
#define AZ_WarningOnce(...) AZ_UNUSED(__VA_ARGS__);
#define AZ_TracePrintf(...) AZ_UNUSED(__VA_ARGS__);
#define AZ_TracePrintfOnce(...) AZ_UNUSED(__VA_ARGS__);
#define AZ_Assert(...)
#define AZ_Error(...)
#define AZ_ErrorOnce(...)
#define AZ_Warning(...)
#define AZ_WarningOnce(...)
#define AZ_TracePrintf(...)
#define AZ_TracePrintfOnce(...)
#define AZ_Verify(...) AZ_UNUSED(__VA_ARGS__);
#define AZ_VerifyError(...) AZ_UNUSED(__VA_ARGS__);
#define AZ_VerifyWarning(...) AZ_UNUSED(__VA_ARGS__);
#define AZ_Verify(expression, ...) AZ_UNUSED(expression)
#define AZ_VerifyError(window, expression, ...) AZ_UNUSED(expression)
#define AZ_VerifyWarning(window, expression, ...) AZ_UNUSED(expression)
#endif // AZ_ENABLE_TRACING
+7 -10
View File
@@ -19,14 +19,11 @@
#pragma once
#include <AzCore/EBus/BusImpl.h>
#include <AzCore/EBus/Environment.h>
#include <AzCore/EBus/Results.h>
#include <AzCore/EBus/Internal/Debug.h>
// Included for backwards compatibility purposes
#include <AzCore/std/smart_ptr/unique_ptr.h>
#include <AzCore/std/containers/unordered_set.h>
#include <AzCore/std/typetraits/is_same.h>
// End backwards compat
#include <AzCore/std/utils.h>
#include <AzCore/std/parallel/scoped_lock.h>
@@ -90,14 +87,14 @@ namespace AZ
* For available settings, see AZ::EBusHandlerPolicy.
* By default, an EBus supports any number of handlers.
*/
static const EBusHandlerPolicy HandlerPolicy = EBusHandlerPolicy::Multiple;
static constexpr EBusHandlerPolicy HandlerPolicy = EBusHandlerPolicy::Multiple;
/**
* Defines how many addresses exist on the EBus.
* For available settings, see AZ::EBusAddressPolicy.
* By default, an EBus uses a single address.
*/
static const EBusAddressPolicy AddressPolicy = EBusAddressPolicy::Single;
static constexpr EBusAddressPolicy AddressPolicy = EBusAddressPolicy::Single;
/**
* The type of ID that is used to address the EBus.
@@ -152,14 +149,14 @@ namespace AZ
* `<BusName>::ExecuteQueuedEvents()`.
* By default, the event queue is disabled.
*/
static const bool EnableEventQueue = false;
static constexpr bool EnableEventQueue = false;
/**
* Specifies whether the bus should accept queued messages by default or not.
* If set to false, Bus::AllowFunctionQueuing(true) must be called before events are accepted.
* Used only when #EnableEventQueue is true.
*/
static const bool EventQueueingActiveByDefault = true;
static constexpr bool EventQueueingActiveByDefault = true;
/**
* Specifies whether the EBus supports queueing functions which take reference
@@ -168,7 +165,7 @@ namespace AZ
* You should only use this if you know that the data being passed as arguments will
* outlive the dispatch of the queued event.
*/
static const bool EnableQueuedReferences = false;
static constexpr bool EnableQueuedReferences = false;
/**
* Locking primitive that is used when adding and removing
@@ -197,7 +194,7 @@ namespace AZ
* to do.
* By default, the standard policy is used, which locks around all dispatches
*/
static const bool LocklessDispatch = false;
static constexpr bool LocklessDispatch = false;
/**
* Specifies where EBus data is stored.
@@ -13,6 +13,7 @@
#include <AzCore/EBus/EBus.h>
#include <AzCore/RTTI/RTTI.h>
#include <AzCore/RTTI/TypeSafeIntegral.h>
#include <AzCore/std/smart_ptr/unique_ptr.h>
namespace AZ
{
+14 -23
View File
@@ -18,9 +18,8 @@
#include <AzCore/std/function/invoke.h>
#include <AzCore/std/containers/queue.h>
#include <AzCore/std/containers/intrusive_set.h>
#include <AzCore/std/parallel/scoped_lock.h>
#include <AzCore/Module/Environment.h>
#include <AzCore/EBus/Environment.h>
namespace AZ
{
@@ -251,29 +250,21 @@ namespace AZ
void Execute()
{
AZ_Warning("System", m_isActive, "You are calling execute queued functions on a bus which has not activated its function queuing! Call YourBus::AllowFunctionQueuing(true)!");
while (true)
MessageQueueType localMessages;
// Swap the current list of queue functions with a local instance
{
BusMessageCall invoke;
AZStd::scoped_lock lock(m_messagesMutex);
AZStd::swap(localMessages, m_messages);
}
//////////////////////////////////////////////////////////////////////////
// Pop element from the queue.
{
AZStd::lock_guard<MutexType> lock(m_messagesMutex);
size_t numMessages = m_messages.size();
if (numMessages == 0)
{
break;
}
AZStd::swap(invoke, m_messages.front());
m_messages.pop();
if (numMessages == 1)
{
m_messages = {};
}
}
//////////////////////////////////////////////////////////////////////////
invoke();
// Execute the queue functions safely now that are owned by the function
while (!localMessages.empty())
{
const BusMessageCall& localMessage = localMessages.front();
localMessage();
localMessages.pop();
}
}
@@ -43,10 +43,12 @@ namespace AZ::IO
m_mainLoopDesc = threadDesc;
m_mainLoopDesc.m_name = "IO Scheduler";
m_mainLoop = AZStd::thread([this]()
{
Thread_MainLoop();
}, &m_mainLoopDesc);
m_mainLoop = AZStd::thread(
m_mainLoopDesc,
[this]()
{
Thread_MainLoop();
});
}
}
@@ -644,11 +644,11 @@ JobManagerWorkStealing::ThreadList JobManagerWorkStealing::CreateWorkerThreads(c
}
info->m_thread = AZStd::thread(
threadDesc,
[this, info]()
{
this->ProcessJobsWorker(info);
},
&threadDesc
}
);
info->m_threadId = info->m_thread.get_id();
@@ -7,6 +7,7 @@
*/
#include <AzCore/Name/NameSerializer.h>
#include <AzCore/IO/GenericStreams.h>
namespace AZ
{
@@ -14,6 +14,7 @@
#include <AzCore/RTTI/ReflectContext.h>
#include <AzCore/std/containers/array.h>
#include <AzCore/std/containers/unordered_map.h>
#include <AzCore/std/containers/unordered_set.h>
#include <AzCore/std/function/invoke.h>
#include <AzCore/std/string/string.h>
#include <AzCore/std/string/string_view.h>
@@ -5,11 +5,13 @@
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include <AzCore/Asset/AssetSerializer.h>
#include <AzCore/Script/ScriptContext.h>
#include <AzCore/RTTI/BehaviorContext.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/Script/lua/lua.h>
#include <AzCore/Script/ScriptProperty.h>
#include <AzCore/Serialization/SerializeContext.h>
namespace AZ
{
@@ -5,8 +5,7 @@
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#ifndef AZCORE_SCRIPT_SCRIPTPROPERTY_H
#define AZCORE_SCRIPT_SCRIPTPROPERTY_H
#pragma once
#include <AzCore/Component/EntityId.h>
#include <AzCore/Memory/SystemAllocator.h>
@@ -490,5 +489,4 @@ namespace AZ
};
}
#endif
@@ -8,6 +8,7 @@
#include <cinttypes>
#include <AzCore/Asset/AssetSerializer.h>
#include <AzCore/Serialization/DataPatch.h>
#include <AzCore/Serialization/DataPatchBus.h>
#include <AzCore/Serialization/DataPatchUpgradeManager.h>
@@ -9,6 +9,7 @@
#include "AzCore/RTTI/TypeInfo.h"
#include <AzCore/Math/UuidSerializer.h>
#include <AzCore/RTTI/AttributeReader.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/Serialization/Json/CastingHelpers.h>
#include <AzCore/Serialization/Json/JsonDeserializer.h>
#include <AzCore/Serialization/Json/JsonStringConversionUtils.h>
@@ -6,7 +6,9 @@
*
*/
#include <AzCore/Asset/AssetSerializer.h>
#include <AzCore/RTTI/AttributeReader.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/Serialization/Json/JsonSerializer.h>
#include <AzCore/Serialization/Json/BaseJsonSerializer.h>
#include <AzCore/Serialization/Json/JsonSerialization.h>
@@ -7,6 +7,7 @@
*/
#include <algorithm>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/Serialization/Json/BasicContainerSerializer.h>
#include <AzCore/Serialization/Json/JsonSerialization.h>
#include <AzCore/Serialization/Json/MapSerializer.h>
@@ -7,6 +7,7 @@
*/
#include <AzCore/RTTI/AttributeReader.h>
#include <AzCore/Asset/AssetSerializer.h>
#include <AzCore/Serialization/ObjectStream.h>
#include <AzCore/Serialization/DataOverlayInstanceMsgs.h>
#include <AzCore/Serialization/DataOverlayProviderMsgs.h>
@@ -37,7 +37,8 @@ namespace AZ
class GenericStream;
}
namespace ObjectStreamInternal {
namespace ObjectStreamInternal
{
class ObjectStreamImpl;
}
@@ -7,6 +7,8 @@
*/
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/Asset/AssetSerializer.h>
#include <AzCore/Serialization/EditContext.h>
#include <AzCore/Serialization/DataOverlay.h>
#include <AzCore/Serialization/DynamicSerializableField.h>
@@ -5,8 +5,7 @@
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#ifndef AZCORE_SERIALIZE_CONTEXT_H
#define AZCORE_SERIALIZE_CONTEXT_H
#pragma once
#include <limits>
@@ -43,6 +42,12 @@
namespace AZ
{
namespace Data
{
template<typename T>
class Asset;
}
class EditContext;
class ObjectStream;
@@ -2562,11 +2567,13 @@ namespace AZ
#include <AzCore/Serialization/AZStdContainers.inl>
#include <AzCore/Serialization/std/VariantReflection.inl>
/// include asset generics
#include <AzCore/Asset/AssetSerializer.h>
// Forward declare asset serialization helper specialization
namespace AZ
{
template<typename T>
struct SerializeGenericTypeInfo< Data::Asset<T> >;
}
/// include implementation of SerializeContext::EnumBuilder
#include <AzCore/Serialization/SerializeContextEnum.inl>
#endif // AZCORE_SERIALIZE_CONTEXT_H
#pragma once
@@ -717,7 +717,9 @@ namespace AZ::SettingsRegistryMergeUtils
if (registry.Get(cacheRootPath, FilePathKey_CacheRootFolder))
{
mergePath = AZStd::move(cacheRootPath);
mergePath /= SettingsRegistryInterface::RegistryFolder;
AZStd::fixed_string<32> registryFolderLower(SettingsRegistryInterface::RegistryFolder);
AZStd::to_lower(registryFolderLower.begin(), registryFolderLower.end());
mergePath /= registryFolderLower;
registry.MergeSettingsFolder(mergePath.Native(), specializations, platform, "", scratchBuffer);
}
@@ -13,6 +13,7 @@
#include <AzCore/Component/ComponentApplicationBus.h>
#include <AzCore/Debug/Profiler.h>
#include <AzCore/Asset/AssetManager.h>
#include <AzCore/Asset/AssetSerializer.h>
#include <AzCore/Component/TransformBus.h>
#include <AzCore/RTTI/BehaviorContext.h>
#include <AzCore/std/algorithm.h>
@@ -10,6 +10,7 @@
#include <AzCore/EBus/BusImpl.h> //Just to get AZ::NullMutex
#include <AzCore/std/chrono/types.h>
#include <AzCore/Statistics/StatisticsManager.h>
#include <AzCore/std/chrono/chrono.h>
#include <AzCore/std/parallel/scoped_lock.h>
namespace AZ
@@ -243,7 +244,7 @@ namespace AZ
//! This one is needed because running statistics are collected many times across
//! several frames. This value is used to calculate a per frame sample for @m_totalTimePerFrameStat,
//! by subtracting @m_prevAccumulatedSums from the accumulated sum in @m_statisticsManager.
//! by subtracting @m_prevAccumulatedSums from the accumulated sum in @m_statisticsManager.
double m_prevAccumulatedSums;
};
@@ -190,11 +190,13 @@ namespace AZ
class TaskWorker
{
public:
void Spawn(::AZ::TaskExecutor& executor, size_t id, AZStd::semaphore& initSemaphore, bool affinitize)
static thread_local TaskWorker* t_worker;
void Spawn(::AZ::TaskExecutor& executor, uint32_t id, AZStd::semaphore& initSemaphore, bool affinitize)
{
m_executor = &executor;
AZStd::string threadName = AZStd::string::format("TaskWorker %zu", id);
AZStd::string threadName = AZStd::string::format("TaskWorker %u", id);
AZStd::thread_desc desc = {};
desc.m_name = threadName.c_str();
if (affinitize)
@@ -203,12 +205,29 @@ namespace AZ
}
m_active.store(true, AZStd::memory_order_release);
m_thread = AZStd::thread{ [this, &initSemaphore]
m_thread = AZStd::thread{ desc,
[this, &initSemaphore]
{
t_worker = this;
initSemaphore.release();
Run();
},
&desc };
} };
}
// Threads that wait on a graph to complete are disqualified from receiving tasks until the wait finishes
void Disable()
{
m_enabled = false;
}
void Enable()
{
m_enabled = true;
}
bool Enabled() const
{
return m_enabled;
}
void Join()
@@ -222,11 +241,7 @@ namespace AZ
{
m_queue.Enqueue(task);
if (!m_busy.exchange(true))
{
// The worker was idle prior to enqueueing the task, release the semaphore
m_semaphore.release();
}
m_semaphore.release();
}
private:
@@ -234,7 +249,6 @@ namespace AZ
{
while (m_active)
{
m_busy = false;
m_semaphore.acquire();
if (!m_active)
@@ -242,8 +256,6 @@ namespace AZ
return;
}
m_busy = true;
Task* task = m_queue.TryDequeue();
while (task)
{
@@ -271,12 +283,15 @@ namespace AZ
AZStd::thread m_thread;
AZStd::atomic<bool> m_active;
AZStd::atomic<bool> m_busy;
AZStd::atomic<bool> m_enabled = true;
AZStd::binary_semaphore m_semaphore;
::AZ::TaskExecutor* m_executor;
TaskQueue m_queue;
friend class ::AZ::TaskExecutor;
};
thread_local TaskWorker* TaskWorker::t_worker = nullptr;
} // namespace Internal
static EnvironmentVariable<TaskExecutor*> s_executor;
@@ -291,13 +306,16 @@ namespace AZ
return **s_executor;
}
// TODO: Create the default executor as part of a component (as in TaskManagerComponent)
void TaskExecutor::SetInstance(TaskExecutor* executor)
{
AZ_Assert(!s_executor, "Attempting to set the global task executor more than once");
s_executor = AZ::Environment::CreateVariable<TaskExecutor*>("GlobalTaskExecutor");
s_executor.Set(executor);
if (!executor)
{
s_executor.Reset();
}
else if (!s_executor) // ignore any calls to set after the first (this happens in unit tests that create new system entities)
{
s_executor = AZ::Environment::CreateVariable<TaskExecutor*>(s_executorName, executor);
}
}
TaskExecutor::TaskExecutor(uint32_t threadCount)
@@ -307,14 +325,12 @@ namespace AZ
m_workers = reinterpret_cast<Internal::TaskWorker*>(azmalloc(m_threadCount * sizeof(Internal::TaskWorker)));
bool affinitize = m_threadCount == AZStd::thread::hardware_concurrency();
AZStd::semaphore initSemaphore;
for (size_t i = 0; i != m_threadCount; ++i)
for (uint32_t i = 0; i != m_threadCount; ++i)
{
new (m_workers + i) Internal::TaskWorker{};
m_workers[i].Spawn(*this, i, initSemaphore, affinitize);
m_workers[i].Spawn(*this, i, initSemaphore, false);
}
for (size_t i = 0; i != m_threadCount; ++i)
@@ -334,9 +350,21 @@ namespace AZ
azfree(m_workers);
}
void TaskExecutor::Submit(Internal::CompiledTaskGraph& graph)
Internal::TaskWorker* TaskExecutor::GetTaskWorker()
{
if (Internal::TaskWorker::t_worker && Internal::TaskWorker::t_worker->m_executor == this)
{
return Internal::TaskWorker::t_worker;
}
return nullptr;
}
void TaskExecutor::Submit(Internal::CompiledTaskGraph& graph, TaskGraphEvent* event)
{
++m_graphsRemaining;
event->m_executor = this; // Used to validate event is not waited for inside a job
// Submit all tasks that have no inbound edges
for (Internal::Task& task : graph.Tasks())
{
@@ -352,11 +380,24 @@ namespace AZ
// TODO: Something more sophisticated is likely needed here.
// First, we are completely ignoring affinity.
// Second, some heuristics on core availability will help distribute work more effectively
m_workers[++m_lastSubmission % m_threadCount].Enqueue(&task);
uint32_t nextWorker = ++m_lastSubmission % m_threadCount;
while (!m_workers[nextWorker].Enabled())
{
// Graphs that are waiting for the completion of a task graph cannot enqueue tasks onto
// the thread issuing the wait.
nextWorker = ++m_lastSubmission % m_threadCount;
}
m_workers[nextWorker].Enqueue(&task);
}
void TaskExecutor::ReleaseGraph()
{
--m_graphsRemaining;
}
void TaskExecutor::ReactivateTaskWorker()
{
GetTaskWorker()->Enable();
}
} // namespace AZ
@@ -72,14 +72,19 @@ namespace AZ
explicit TaskExecutor(uint32_t threadCount = 0);
~TaskExecutor();
void Submit(Internal::CompiledTaskGraph& graph);
// Submit a task graph for execution. Waitable task graphs cannot enqueue work on the task thread
// that is currently active
void Submit(Internal::CompiledTaskGraph& graph, TaskGraphEvent* event);
void Submit(Internal::Task& task);
private:
friend class Internal::TaskWorker;
friend class TaskGraphEvent;
Internal::TaskWorker* GetTaskWorker();
void ReleaseGraph();
void ReactivateTaskWorker();
Internal::TaskWorker* m_workers;
uint32_t m_threadCount = 0;
@@ -14,6 +14,12 @@ namespace AZ
{
using Internal::CompiledTaskGraph;
void TaskGraphEvent::Wait()
{
AZ_Assert(m_executor->GetTaskWorker() == nullptr, "Waiting in a task is unsupported");
m_semaphore.acquire();
}
void TaskToken::PrecedesInternal(TaskToken& comesAfter)
{
AZ_Assert(!m_parent.m_submitted, "Cannot mutate a TaskGraph that was previously submitted.");
@@ -71,7 +77,7 @@ namespace AZ
m_compiledTaskGraph->m_tasks[i].Init();
}
executor.Submit(*m_compiledTaskGraph);
executor.Submit(*m_compiledTaskGraph, waitEvent);
if (m_retained)
{
@@ -22,10 +22,19 @@ namespace AZ
namespace Internal
{
class CompiledTaskGraph;
class TaskWorker;
}
class TaskExecutor;
class TaskGraph;
class TaskGraphActiveInterface
{
public:
AZ_RTTI(TaskGraphActiveInterface, "{08118074-B139-4EF9-B8FD-29F1D6DC9233}");
virtual bool IsTaskGraphActive() const = 0;
};
// A TaskToken is returned each time a Task is added to the TaskGraph. TaskTokens are used to
// express dependencies between tasks within the graph, and have no purpose after the graph
// is submitted (simply let them go out of scope)
@@ -70,9 +79,12 @@ namespace AZ
private:
friend class ::AZ::Internal::CompiledTaskGraph;
friend class TaskGraph;
friend class TaskExecutor;
void Signal();
AZStd::binary_semaphore m_semaphore;
TaskExecutor* m_executor = nullptr;
};
// The TaskGraph encapsulates a set of tasks and their interdependencies. After adding
@@ -89,6 +101,9 @@ namespace AZ
// Reset the state of the task graph to begin recording tasks and edges again
// NOTE: Graph must be in a "settled" state (cannot be in-flight)
void Reset();
// Returns false if 1 or more tasks have been added to the graph
bool IsEmpty();
// Add a task to the graph, retrieiving a token that can be used to express dependencies
// between tasks. The first argument specifies the TaskKind, used for tracking the task.
@@ -33,11 +33,6 @@ namespace AZ
return m_semaphore.try_acquire_for(AZStd::chrono::milliseconds{ 0 });
}
inline void TaskGraphEvent::Wait()
{
m_semaphore.acquire();
}
inline void TaskGraphEvent::Signal()
{
m_semaphore.release();
@@ -59,6 +54,11 @@ namespace AZ
return { AddTask(descriptor, AZStd::forward<Lambdas>(lambdas))... };
}
inline bool TaskGraph::IsEmpty()
{
return m_tasks.empty();
}
inline void TaskGraph::Detach()
{
m_retained = false;
@@ -0,0 +1,88 @@
/*
* 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/Console/IConsole.h>
#include <AzCore/Interface/Interface.h>
#include <AzCore/Task/TaskGraphSystemComponent.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/Serialization/EditContext.h>
// Create a cvar as a central location for experimentation with switching from the Job system to TaskGraph system.
AZ_CVAR(bool, cl_activateTaskGraph, false, nullptr, AZ::ConsoleFunctorFlags::Null, "Flag clients of TaskGraph to switch between jobs/taskgraph (Note does not disable task graph system)");
static constexpr uint32_t TaskExecutorServiceCrc = AZ_CRC_CE("TaskExecutorService");
namespace AZ
{
void TaskGraphSystemComponent::Activate()
{
AZ_Assert(m_taskExecutor == nullptr, "Error multiple activation of the TaskGraphSystemComponent");
if (Interface<TaskGraphActiveInterface>::Get() == nullptr)
{
Interface<TaskGraphActiveInterface>::Register(this);
m_taskExecutor = aznew TaskExecutor();
TaskExecutor::SetInstance(m_taskExecutor);
}
}
void TaskGraphSystemComponent::Deactivate()
{
if (&TaskExecutor::Instance() == m_taskExecutor) // check that our instance is the global instance (not always true in unit tests)
{
m_taskExecutor->SetInstance(nullptr);
}
if (m_taskExecutor)
{
azdestroy(m_taskExecutor);
m_taskExecutor = nullptr;
}
if (Interface<TaskGraphActiveInterface>::Get() == this)
{
Interface<TaskGraphActiveInterface>::Unregister(this);
}
}
void TaskGraphSystemComponent::GetProvidedServices(ComponentDescriptor::DependencyArrayType& provided)
{
provided.push_back(TaskExecutorServiceCrc);
}
void TaskGraphSystemComponent::GetIncompatibleServices(ComponentDescriptor::DependencyArrayType& incompatible)
{
incompatible.push_back(TaskExecutorServiceCrc);
}
void TaskGraphSystemComponent::GetDependentServices([[maybe_unused]] ComponentDescriptor::DependencyArrayType& dependent)
{
}
void TaskGraphSystemComponent::Reflect(ReflectContext* context)
{
if (SerializeContext* serializeContext = azrtti_cast<SerializeContext*>(context))
{
serializeContext->Class<TaskGraphSystemComponent, AZ::Component>()
->Version(1)
;
if (AZ::EditContext* ec = serializeContext->GetEditContext())
{
ec->Class<TaskGraphSystemComponent>
("TaskGraph", "System component to create the default executor")
->ClassElement(AZ::Edit::ClassElements::EditorData, "")
->Attribute(AZ::Edit::Attributes::Category, "Engine")
->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("System"))
;
}
}
}
bool TaskGraphSystemComponent::IsTaskGraphActive() const
{
return cl_activateTaskGraph;
}
} // namespace AZ
@@ -0,0 +1,47 @@
/*
* 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 <AzCore/Component/Component.h>
#include <AzCore/Math/Crc.h>
#include <AzCore/Task/TaskExecutor.h>
#include <AzCore/Task/TaskGraph.h>
namespace AZ
{
class TaskGraphSystemComponent
: public Component
, public TaskGraphActiveInterface
{
public:
AZ_COMPONENT(AZ::TaskGraphSystemComponent, "{5D56B829-1FEB-43D5-A0BD-E33C0497EFE2}")
TaskGraphSystemComponent() = default;
// Implement TaskGraphActiveInterface
bool IsTaskGraphActive() const override;
private:
//////////////////////////////////////////////////////////////////////////
// Component base
void Activate() override;
void Deactivate() override;
//////////////////////////////////////////////////////////////////////////
/// \ref ComponentDescriptor::GetProvidedServices
static void GetProvidedServices(ComponentDescriptor::DependencyArrayType& provided);
/// \ref ComponentDescriptor::GetIncompatibleServices
static void GetIncompatibleServices(ComponentDescriptor::DependencyArrayType& incompatible);
/// \ref ComponentDescriptor::GetDependentServices
static void GetDependentServices(ComponentDescriptor::DependencyArrayType& dependent);
/// \red ComponentDescriptor::Reflect
static void Reflect(ReflectContext* reflection);
AZ::TaskExecutor* m_taskExecutor = nullptr;
};
}
@@ -633,6 +633,8 @@ set(FILES
Task/TaskGraph.cpp
Task/TaskGraph.h
Task/TaskGraph.inl
Task/TaskGraphSystemComponent.h
Task/TaskGraphSystemComponent.cpp
Threading/ThreadSafeDeque.h
Threading/ThreadSafeDeque.inl
Threading/ThreadSafeObject.h
@@ -87,12 +87,6 @@ namespace AZStd
// construct/copy/destroy:
thread();
/**
* \note thread_desc is AZStd extension.
*/
template <class F>
explicit thread(F&& f, const thread_desc* desc = 0);
~thread();
thread(thread&& rhs)
@@ -108,6 +102,15 @@ namespace AZStd
return *this;
}
template<class F, class... Args, typename = AZStd::enable_if_t<!AZStd::is_convertible_v<AZStd::decay_t<F>, thread_desc>>>
explicit thread(F&& f, Args&&... args);
/**
* \note thread_desc is AZStd extension.
*/
template<class F, class... Args>
thread(const thread_desc& desc, F&& f, Args&&... args);
// Till we fully have RVALUES
template <class F>
explicit thread(Internal::thread_move_t<F> f);
@@ -138,8 +141,8 @@ namespace AZStd
//thread(AZStd::delegate<void ()> d,const thread_desc* desc = 0);
private:
thread(thread&);
thread& operator=(thread&);
thread(const thread&) = delete;
thread& operator=(const thread&) = delete;
native_thread_data_type m_thread;
};
@@ -10,6 +10,8 @@
#include <unistd.h>
#include <sched.h>
#include <AzCore/std/tuple.h>
namespace AZStd
{
namespace Internal
@@ -22,12 +24,20 @@ namespace AZStd
//////////////////////////////////////////////////////////////////////////
// thread
template <class F>
inline thread::thread(F&& f, const thread_desc* desc)
template<class F, class... Args, typename>
thread::thread(F&& f, Args&&... args)
: thread(thread_desc{}, AZStd::forward<F>(f), AZStd::forward<Args>(args)...)
{}
template<class F, class... Args>
thread::thread(const thread_desc& desc, F&& f, Args&&... args)
{
Internal::thread_info* ti = Internal::create_thread_info(AZStd::forward<F>(f));
ti->m_name = desc ? desc->m_name : nullptr;
m_thread = Internal::create_thread(desc, ti);
auto threadfunc = [fn = AZStd::forward<F>(f), argsTuple = AZStd::make_tuple(AZStd::forward<Args>(args)...)]() mutable -> void
{
AZStd::apply(AZStd::move(fn), AZStd::move(argsTuple));
};
Internal::thread_info* ti = Internal::create_thread_info(AZStd::move(threadfunc));
m_thread = Internal::create_thread(&desc, ti);
}
inline bool thread::joinable() const
@@ -18,6 +18,8 @@ extern "C"
AZ_DLL_IMPORT unsigned long __stdcall GetCurrentThreadId(void);
}
#include <AzCore/std/tuple.h>
namespace AZStd
{
namespace Internal
@@ -30,11 +32,20 @@ namespace AZStd
//////////////////////////////////////////////////////////////////////////
// thread
template <class F>
inline thread::thread(F&& f, const thread_desc* desc)
template<class F, class... Args, typename>
thread::thread(F&& f, Args&&... args)
: thread(thread_desc{}, AZStd::forward<F>(f), AZStd::forward<Args>(args)...)
{}
template<class F, class... Args>
thread::thread(const thread_desc& desc, F&& f, Args&&... args)
{
Internal::thread_info* ti = Internal::create_thread_info(AZStd::forward<F>(f));
m_thread.m_handle = Internal::create_thread(desc, ti, &m_thread.m_id);
auto threadfunc = [fn = AZStd::forward<F>(f), argsTuple = AZStd::make_tuple(AZStd::forward<Args>(args)...)]() mutable -> void
{
AZStd::apply(AZStd::move(fn), AZStd::move(argsTuple));
};
Internal::thread_info* ti = Internal::create_thread_info(AZStd::move(threadfunc));
m_thread.m_handle = Internal::create_thread(&desc, ti, &m_thread.m_id);
}
inline bool thread::joinable() const
+77 -77
View File
@@ -195,18 +195,18 @@ namespace UnitTest
void test_thread_id_for_running_thread_is_not_default_constructed_id()
{
const thread_desc* desc = m_numThreadDesc ? &m_desc[0] : nullptr;
AZStd::thread t(AZStd::bind(&Parallel_Thread::do_nothing, this), desc);
const thread_desc desc = m_numThreadDesc ? m_desc[0] : thread_desc{};
AZStd::thread t(desc, AZStd::bind(&Parallel_Thread::do_nothing, this));
AZ_TEST_ASSERT(t.get_id() != AZStd::thread::id());
t.join();
}
void test_different_threads_have_different_ids()
{
const thread_desc* desc1 = m_numThreadDesc ? &m_desc[0] : nullptr;
const thread_desc* desc2 = m_numThreadDesc ? &m_desc[1] : nullptr;
AZStd::thread t(AZStd::bind(&Parallel_Thread::do_nothing, this), desc1);
AZStd::thread t2(AZStd::bind(&Parallel_Thread::do_nothing, this), desc2);
const thread_desc desc1 = m_numThreadDesc ? m_desc[0] : thread_desc{};
const thread_desc desc2 = m_numThreadDesc ? m_desc[1] : thread_desc{};
AZStd::thread t(desc1, AZStd::bind(&Parallel_Thread::do_nothing, this));
AZStd::thread t2(desc2, AZStd::bind(&Parallel_Thread::do_nothing, this));
AZ_TEST_ASSERT(t.get_id() != t2.get_id());
t.join();
t2.join();
@@ -214,13 +214,13 @@ namespace UnitTest
void test_thread_ids_have_a_total_order()
{
const thread_desc* desc1 = m_numThreadDesc ? &m_desc[0] : nullptr;
const thread_desc* desc2 = m_numThreadDesc ? &m_desc[1] : nullptr;
const thread_desc* desc3 = m_numThreadDesc ? &m_desc[2] : nullptr;
const thread_desc desc1 = m_numThreadDesc ? m_desc[0] : thread_desc{};
const thread_desc desc2 = m_numThreadDesc ? m_desc[1] : thread_desc{};
const thread_desc desc3 = m_numThreadDesc ? m_desc[2] : thread_desc{};
AZStd::thread t(AZStd::bind(&Parallel_Thread::do_nothing, this), desc1);
AZStd::thread t2(AZStd::bind(&Parallel_Thread::do_nothing, this), desc2);
AZStd::thread t3(AZStd::bind(&Parallel_Thread::do_nothing, this), desc3);
AZStd::thread t(desc1, AZStd::bind(&Parallel_Thread::do_nothing, this));
AZStd::thread t2(desc2, AZStd::bind(&Parallel_Thread::do_nothing, this));
AZStd::thread t3(desc3, AZStd::bind(&Parallel_Thread::do_nothing, this));
AZ_TEST_ASSERT(t.get_id() != t2.get_id());
AZ_TEST_ASSERT(t.get_id() != t3.get_id());
AZ_TEST_ASSERT(t2.get_id() != t3.get_id());
@@ -313,10 +313,10 @@ namespace UnitTest
void test_thread_id_of_running_thread_returned_by_this_thread_get_id()
{
const thread_desc* desc1 = m_numThreadDesc ? &m_desc[0] : nullptr;
const thread_desc desc1 = m_numThreadDesc ? m_desc[0] : thread_desc{};
AZStd::thread::id id;
AZStd::thread t(AZStd::bind(&Parallel_Thread::get_thread_id, this, &id), desc1);
AZStd::thread t(desc1, AZStd::bind(&Parallel_Thread::get_thread_id, this, &id));
AZStd::thread::id t_id = t.get_id();
t.join();
AZ_TEST_ASSERT(id == t_id);
@@ -366,10 +366,10 @@ namespace UnitTest
void test_move_on_construction()
{
const thread_desc* desc1 = m_numThreadDesc ? &m_desc[0] : nullptr;
const thread_desc desc1 = m_numThreadDesc ? m_desc[0] : thread_desc{};
AZStd::thread::id the_id;
AZStd::thread x;
x = AZStd::thread(AZStd::bind(&Parallel_Thread::do_nothing_id, this, &the_id), desc1);
x = AZStd::thread(desc1, AZStd::bind(&Parallel_Thread::do_nothing_id, this, &the_id));
AZStd::thread::id x_id = x.get_id();
x.join();
AZ_TEST_ASSERT(the_id == x_id);
@@ -377,8 +377,8 @@ namespace UnitTest
AZStd::thread make_thread(AZStd::thread::id* the_id)
{
const thread_desc* desc1 = m_numThreadDesc ? &m_desc[0] : nullptr;
return AZStd::thread(AZStd::bind(&Parallel_Thread::do_nothing_id, this, the_id), desc1);
const thread_desc desc1 = m_numThreadDesc ? m_desc[0] : thread_desc{};
return AZStd::thread(desc1, AZStd::bind(&Parallel_Thread::do_nothing_id, this, the_id));
}
void test_move_from_function_return()
@@ -430,9 +430,9 @@ namespace UnitTest
void do_test_creation()
{
const thread_desc* desc1 = m_numThreadDesc ? &m_desc[0] : nullptr;
const thread_desc desc1 = m_numThreadDesc ? m_desc[0] : thread_desc{};
m_data = 0;
AZStd::thread t(AZStd::bind(&Parallel_Thread::simple_thread, this), desc1);
AZStd::thread t(desc1, AZStd::bind(&Parallel_Thread::simple_thread, this));
t.join();
AZ_TEST_ASSERT(m_data == 999);
}
@@ -445,9 +445,9 @@ namespace UnitTest
void do_test_id_comparison()
{
const thread_desc* desc1 = m_numThreadDesc ? &m_desc[0] : nullptr;
const thread_desc desc1 = m_numThreadDesc ? m_desc[0] : thread_desc{};
AZStd::thread::id self = this_thread::get_id();
AZStd::thread thrd(AZStd::bind(&Parallel_Thread::comparison_thread, this, self), desc1);
AZStd::thread thrd(desc1, AZStd::bind(&Parallel_Thread::comparison_thread, this, self));
thrd.join();
}
@@ -476,10 +476,10 @@ namespace UnitTest
void do_test_creation_through_reference_wrapper()
{
const thread_desc* desc1 = m_numThreadDesc ? &m_desc[0] : nullptr;
const thread_desc desc1 = m_numThreadDesc ? m_desc[0] : thread_desc{};
non_copyable_functor f;
AZStd::thread thrd(AZStd::ref(f), desc1);
AZStd::thread thrd(desc1, AZStd::ref(f));
thrd.join();
AZ_TEST_ASSERT(f.value == 999);
}
@@ -491,10 +491,10 @@ namespace UnitTest
void test_swap()
{
const thread_desc* desc1 = m_numThreadDesc ? &m_desc[0] : nullptr;
const thread_desc* desc2 = m_numThreadDesc ? &m_desc[1] : nullptr;
AZStd::thread t(AZStd::bind(&Parallel_Thread::simple_thread, this), desc1);
AZStd::thread t2(AZStd::bind(&Parallel_Thread::simple_thread, this), desc2);
const thread_desc desc1 = m_numThreadDesc ? m_desc[0] : thread_desc{};
const thread_desc desc2 = m_numThreadDesc ? m_desc[1] : thread_desc{};
AZStd::thread t(desc1, AZStd::bind(&Parallel_Thread::simple_thread, this));
AZStd::thread t2(desc2, AZStd::bind(&Parallel_Thread::simple_thread, this));
AZStd::thread::id id1 = t.get_id();
AZStd::thread::id id2 = t2.get_id();
@@ -512,7 +512,7 @@ namespace UnitTest
void run()
{
const thread_desc* desc1 = m_numThreadDesc ? &m_desc[0] : nullptr;
const thread_desc desc1 = m_numThreadDesc ? m_desc[0] : thread_desc{};
// We need to have at least one processor
AZ_TEST_ASSERT(AZStd::thread::hardware_concurrency() >= 1);
@@ -520,18 +520,18 @@ namespace UnitTest
// Create thread to increment data till we need to
m_data = 0;
m_dataMax = 10;
AZStd::thread tr(AZStd::bind(&Parallel_Thread::increment_data, this), desc1);
AZStd::thread tr(desc1, AZStd::bind(&Parallel_Thread::increment_data, this));
tr.join();
AZ_TEST_ASSERT(m_data == m_dataMax);
m_data = 0;
AZStd::thread trDel(make_delegate(this, &Parallel_Thread::increment_data), desc1);
AZStd::thread trDel(desc1, make_delegate(this, &Parallel_Thread::increment_data));
trDel.join();
AZ_TEST_ASSERT(m_data == m_dataMax);
chrono::system_clock::time_point startTime = chrono::system_clock::now();
{
AZStd::thread tr1(AZStd::bind(&Parallel_Thread::sleep_thread, this, chrono::milliseconds(100)), desc1);
AZStd::thread tr1(desc1, AZStd::bind(&Parallel_Thread::sleep_thread, this, chrono::milliseconds(100)));
tr1.join();
}
auto sleepTime = chrono::system_clock::now() - startTime;
@@ -563,71 +563,71 @@ namespace UnitTest
{
MfTest x;
AZStd::function<void ()> func = AZStd::bind(&MfTest::f0, &x);
AZStd::thread(func, desc1).join();
AZStd::thread(desc1, func).join();
func = AZStd::bind(&MfTest::f0, AZStd::ref(x));
AZStd::thread(func, desc1).join();
AZStd::thread(desc1, func).join();
func = AZStd::bind(&MfTest::g0, &x);
AZStd::thread(func, desc1).join();
AZStd::thread(desc1, func).join();
func = AZStd::bind(&MfTest::g0, x);
AZStd::thread(func, desc1).join();
AZStd::thread(desc1, func).join();
func = AZStd::bind(&MfTest::g0, AZStd::ref(x));
AZStd::thread(func, desc1).join();
AZStd::thread(desc1, func).join();
//// 1
//thread( AZStd::bind(&MfTest::f1, &x, 1) , desc1).join();
//thread( AZStd::bind(&MfTest::f1, AZStd::ref(x), 1) , desc1).join();
//thread( AZStd::bind(&MfTest::g1, &x, 1) , desc1).join();
//thread( AZStd::bind(&MfTest::g1, x, 1) , desc1).join();
//thread( AZStd::bind(&MfTest::g1, AZStd::ref(x), 1) , desc1).join();
//thread( AZStd::bind(desc1, &MfTest::f1, &x, 1)).join();
//thread( AZStd::bind(desc1, &MfTest::f1, AZStd::ref(x), 1)).join();
//thread( AZStd::bind(desc1, &MfTest::g1, &x, 1)).join();
//thread( AZStd::bind(desc1, &MfTest::g1, x, 1)).join();
//thread( AZStd::bind(desc1, &MfTest::g1, AZStd::ref(x), 1)).join();
//// 2
//thread( AZStd::bind(&MfTest::f2, &x, 1, 2) , desc1).join();
//thread( AZStd::bind(&MfTest::f2, AZStd::ref(x), 1, 2) , desc1).join();
//thread( AZStd::bind(&MfTest::g2, &x, 1, 2) , desc1).join();
//thread( AZStd::bind(&MfTest::g2, x, 1, 2) , desc1).join();
//thread( AZStd::bind(&MfTest::g2, AZStd::ref(x), 1, 2) , desc1).join();
//thread( AZStd::bind(desc1, &MfTest::f2, &x, 1, 2)).join();
//thread( AZStd::bind(desc1, &MfTest::f2, AZStd::ref(x), 1, 2)).join();
//thread( AZStd::bind(desc1, &MfTest::g2, &x, 1, 2)).join();
//thread( AZStd::bind(desc1, &MfTest::g2, x, 1, 2)).join();
//thread( AZStd::bind(desc1, &MfTest::g2, AZStd::ref(x), 1, 2)).join();
//// 3
//thread( AZStd::bind(&MfTest::f3, &x, 1, 2, 3) , desc1).join();
//thread( AZStd::bind(&MfTest::f3, AZStd::ref(x), 1, 2, 3) , desc1).join();
//thread( AZStd::bind(&MfTest::g3, &x, 1, 2, 3) , desc1).join();
//thread( AZStd::bind(&MfTest::g3, x, 1, 2, 3) , desc1).join();
//thread( AZStd::bind(&MfTest::g3, AZStd::ref(x), 1, 2, 3) , desc1).join();
//thread( AZStd::bind(desc1, &MfTest::f3, &x, 1, 2, 3)).join();
//thread( AZStd::bind(desc1, &MfTest::f3, AZStd::ref(x), 1, 2, 3)).join();
//thread( AZStd::bind(desc1, &MfTest::g3, &x, 1, 2, 3)).join();
//thread( AZStd::bind(desc1, &MfTest::g3, x, 1, 2, 3)).join();
//thread( AZStd::bind(desc1, &MfTest::g3, AZStd::ref(x), 1, 2, 3)).join();
//// 4
//thread( AZStd::bind(&MfTest::f4, &x, 1, 2, 3, 4) , desc1).join();
//thread( AZStd::bind(&MfTest::f4, AZStd::ref(x), 1, 2, 3, 4) , desc1).join();
//thread( AZStd::bind(&MfTest::g4, &x, 1, 2, 3, 4) , desc1).join();
//thread( AZStd::bind(&MfTest::g4, x, 1, 2, 3, 4) , desc1).join();
//thread( AZStd::bind(&MfTest::g4, AZStd::ref(x), 1, 2, 3, 4) , desc1).join();
//thread( AZStd::bind(desc1, &MfTest::f4, &x, 1, 2, 3, 4)).join();
//thread( AZStd::bind(desc1, &MfTest::f4, AZStd::ref(x), 1, 2, 3, 4)).join();
//thread( AZStd::bind(desc1, &MfTest::g4, &x, 1, 2, 3, 4)).join();
//thread( AZStd::bind(desc1, &MfTest::g4, x, 1, 2, 3, 4)).join();
//thread( AZStd::bind(desc1, &MfTest::g4, AZStd::ref(x), 1, 2, 3, 4)).join();
//// 5
//thread( AZStd::bind(&MfTest::f5, &x, 1, 2, 3, 4, 5) , desc1).join();
//thread( AZStd::bind(&MfTest::f5, AZStd::ref(x), 1, 2, 3, 4, 5) , desc1).join();
//thread( AZStd::bind(&MfTest::g5, &x, 1, 2, 3, 4, 5) , desc1).join();
//thread( AZStd::bind(&MfTest::g5, x, 1, 2, 3, 4, 5) , desc1).join();
//thread( AZStd::bind(&MfTest::g5, AZStd::ref(x), 1, 2, 3, 4, 5) , desc1).join();
//thread( AZStd::bind(desc1, &MfTest::f5, &x, 1, 2, 3, 4, 5)).join();
//thread( AZStd::bind(desc1, &MfTest::f5, AZStd::ref(x), 1, 2, 3, 4, 5)).join();
//thread( AZStd::bind(desc1, &MfTest::g5, &x, 1, 2, 3, 4, 5)).join();
//thread( AZStd::bind(desc1, &MfTest::g5, x, 1, 2, 3, 4, 5)).join();
//thread( AZStd::bind(desc1, &MfTest::g5, AZStd::ref(x), 1, 2, 3, 4, 5)).join();
//// 6
//thread( AZStd::bind(&MfTest::f6, &x, 1, 2, 3, 4, 5, 6) , desc1).join();
//thread( AZStd::bind(&MfTest::f6, AZStd::ref(x), 1, 2, 3, 4, 5, 6) , desc1).join();
//thread( AZStd::bind(&MfTest::g6, &x, 1, 2, 3, 4, 5, 6) , desc1).join();
//thread( AZStd::bind(&MfTest::g6, x, 1, 2, 3, 4, 5, 6) , desc1).join();
//thread( AZStd::bind(&MfTest::g6, AZStd::ref(x), 1, 2, 3, 4, 5, 6) , desc1).join();
//thread( AZStd::bind(desc1, &MfTest::f6, &x, 1, 2, 3, 4, 5, 6)).join();
//thread( AZStd::bind(desc1, &MfTest::f6, AZStd::ref(x), 1, 2, 3, 4, 5, 6)).join();
//thread( AZStd::bind(desc1, &MfTest::g6, &x, 1, 2, 3, 4, 5, 6)).join();
//thread( AZStd::bind(desc1, &MfTest::g6, x, 1, 2, 3, 4, 5, 6)).join();
//thread( AZStd::bind(desc1, &MfTest::g6, AZStd::ref(x), 1, 2, 3, 4, 5, 6)).join();
//// 7
//thread( AZStd::bind(&MfTest::f7, &x, 1, 2, 3, 4, 5, 6, 7), desc1).join();
//thread( AZStd::bind(&MfTest::f7, AZStd::ref(x), 1, 2, 3, 4, 5, 6, 7), desc1).join();
//thread( AZStd::bind(&MfTest::g7, &x, 1, 2, 3, 4, 5, 6, 7), desc1).join();
//thread( AZStd::bind(&MfTest::g7, x, 1, 2, 3, 4, 5, 6, 7), desc1).join();
//thread( AZStd::bind(&MfTest::g7, AZStd::ref(x), 1, 2, 3, 4, 5, 6, 7), desc1).join();
//thread( AZStd::bind(desc1, &MfTest::f7, &x, 1, 2, 3, 4, 5, 6, 7)).join();
//thread( AZStd::bind(desc1, &MfTest::f7, AZStd::ref(x), 1, 2, 3, 4, 5, 6, 7)).join();
//thread( AZStd::bind(desc1, &MfTest::g7, &x, 1, 2, 3, 4, 5, 6, 7)).join();
//thread( AZStd::bind(desc1, &MfTest::g7, x, 1, 2, 3, 4, 5, 6, 7)).join();
//thread( AZStd::bind(desc1, &MfTest::g7, AZStd::ref(x), 1, 2, 3, 4, 5, 6, 7)).join();
//// 8
//thread( AZStd::bind(&MfTest::f8, &x, 1, 2, 3, 4, 5, 6, 7, 8) , desc1).join();
//thread( AZStd::bind(&MfTest::f8, AZStd::ref(x), 1, 2, 3, 4, 5, 6, 7, 8) , desc1).join();
//thread( AZStd::bind(&MfTest::g8, &x, 1, 2, 3, 4, 5, 6, 7, 8) , desc1).join();
//thread( AZStd::bind(&MfTest::g8, x, 1, 2, 3, 4, 5, 6, 7, 8) , desc1).join();
//thread( AZStd::bind(&MfTest::g8, AZStd::ref(x), 1, 2, 3, 4, 5, 6, 7, 8) , desc1).join();
//thread( AZStd::bind(desc1, &MfTest::f8, &x, 1, 2, 3, 4, 5, 6, 7, 8)).join();
//thread( AZStd::bind(desc1, &MfTest::f8, AZStd::ref(x), 1, 2, 3, 4, 5, 6, 7, 8)).join();
//thread( AZStd::bind(desc1, &MfTest::g8, &x, 1, 2, 3, 4, 5, 6, 7, 8)).join();
//thread( AZStd::bind(desc1, &MfTest::g8, x, 1, 2, 3, 4, 5, 6, 7, 8)).join();
//thread( AZStd::bind(desc1, &MfTest::g8, AZStd::ref(x), 1, 2, 3, 4, 5, 6, 7, 8)).join();
AZ_TEST_ASSERT(x.m_hash == 1366);
}
@@ -8,6 +8,7 @@
#include <SerializeContextFixture.h>
#include <AzCore/Asset/AssetSerializer.h>
#include <AzCore/std/any.h>
#include <AzCore/std/containers/variant.h>
#include <AzCore/Component/Entity.h>
@@ -6,6 +6,7 @@
*
*/
#include <AzCore/Asset/AssetManager.h>
#include <AzCore/Asset/AssetSerializer.h>
#include <AzCore/Console/IConsole.h>
#include <AzCore/Console/Console.h>
#include <AzCore/Interface/Interface.h>
@@ -365,6 +366,42 @@ namespace UnitTest
};
static constexpr AZStd::chrono::seconds MaxDispatchTimeoutSeconds = BaseAssetManagerTest::DefaultTimeoutSeconds * 12;
template <typename Pred>
bool DispatchEventsUntilCondition(AZ::Data::AssetManager& assetManager, Pred&& conditionPredicate,
AZStd::chrono::seconds logIntervalSeconds = BaseAssetManagerTest::DefaultTimeoutSeconds,
AZStd::chrono::seconds maxTimeoutSeconds = MaxDispatchTimeoutSeconds)
{
// If the Max Timeout is hit the test will be marked as a failure
AZStd::chrono::time_point dispatchEventTimeStart = AZStd::chrono::system_clock::now();
AZStd::chrono::seconds dispatchEventNextLogTime = logIntervalSeconds;
while (!conditionPredicate())
{
AZStd::chrono::time_point currentTime = AZStd::chrono::system_clock::now();
if (AZStd::chrono::seconds elapsedTime{ currentTime - dispatchEventTimeStart };
elapsedTime >= dispatchEventNextLogTime)
{
const testing::TestInfo* test_info = ::testing::UnitTest::GetInstance()->current_test_info();
AZ_Printf("AssetManagerLoadingTest", "The DispatchEventsUntiTimeout function has been waiting for %llu seconds"
" in test %s.%s", elapsedTime.count(), test_info->test_case_name(), test_info->name());
// Update the next log time to be the next multiple of DefaultTimeout Seconds
// after current elapsed time
dispatchEventNextLogTime = elapsedTime + logIntervalSeconds - ((elapsedTime + logIntervalSeconds) % logIntervalSeconds);
if (elapsedTime >= maxTimeoutSeconds)
{
return false;
}
}
assetManager.DispatchEvents();
AZStd::this_thread::yield();
}
return true;
}
#if AZ_TRAIT_DISABLE_FAILED_ASSET_MANAGER_TESTS || AZ_TRAIT_DISABLE_ASSET_MANAGER_FLOOD_TEST
TEST_F(AssetJobsFloodTest, DISABLED_FloodTest)
#else
@@ -1357,42 +1394,74 @@ namespace UnitTest
m_assetHandlerAndCatalog->m_numCreations = 0;
m_assetHandlerAndCatalog->m_numDestructions = 0;
{
ContainerReadyListener containerLoadingCompleteListener(NoLoadAssetId);
OnAssetReadyListener readyListener(NoLoadAssetId, azrtti_typeid<AssetWithAssetReference>());
OnAssetReadyListener depenencyListener(MyAsset2Id, azrtti_typeid<AssetWithAssetReference>());
OnAssetReadyListener dependencyListener(MyAsset2Id, azrtti_typeid<AssetWithAssetReference>());
SCOPED_TRACE("LoadDependencies_BehaviorObeyed");
auto AssetOnlyReady = [&readyListener]() -> bool
{
return readyListener.m_ready;
};
auto AssetAndDependencyReady = [&readyListener, &dependencyListener]() -> bool
{
return readyListener.m_ready && dependencyListener.m_ready;
};
auto AssetContainerReady = [&containerLoadingCompleteListener]() -> bool
{
return containerLoadingCompleteListener.m_ready;
};
auto noLoadRef = m_testAssetManager->GetAsset(NoLoadAssetId, azrtti_typeid<AssetWithAssetReference>(),
AZ::Data::AssetLoadBehavior::Default);
auto maxTimeout = AZStd::chrono::system_clock::now() + DefaultTimeoutSeconds;
// Dispatch AssetBus events until the NoLoadAssetId has signaled an OnAssetReady
// event or the timeout has been reached
EXPECT_TRUE(DispatchEventsUntilCondition(*m_testAssetManager, AssetOnlyReady))
<< "The DispatchEventsUntiTimeout function has not completed in "
<< MaxDispatchTimeoutSeconds.count() << " seconds. The test will be marked as a failure\n";
// Dispatch AssetBus events until the asset container used to load
// NoLoadAssetId has signaled an OnAssetContainerReady event
// or the timeout has been reached
// Wait until the current asset container has finished loading the NoLoadAssetId
// before trigger another load
// If the wait does not occur here, most likely what would occur is
// the AssetManager::m_ownedAssetContainers object is still loading the NoLoadAssetId
// using the default AssetLoadParameters
// If a call to GetAsset occurs at this point while the Asset is still loading
// it will ignore the new loadParams below and instead just re-use the existing
// AssetContainerReader instance, resulting in the dependent MyAsset2Id not
// being loaded
// The function that can return an existing AssetContainer instance is the
// AssetManager::GetAssetContainer. Since it can be in the middle of a load,
// updating the AssetLoadParams would have an effect on the current in progress
// load
EXPECT_TRUE(DispatchEventsUntilCondition(*m_testAssetManager, AssetContainerReady))
<< "The DispatchEventsUntiTimeout function has not completed in "
<< MaxDispatchTimeoutSeconds.count() << " seconds. The test will be marked as a failure\n";
// Reset the ContainerLoadingComplete ready status back to 0
containerLoadingCompleteListener.m_ready = 0;
while (!readyListener.m_ready)
{
m_testAssetManager->DispatchEvents();
if (AZStd::chrono::system_clock::now() > maxTimeout)
{
break;
}
AZStd::this_thread::yield();
}
EXPECT_EQ(readyListener.m_ready, 1);
EXPECT_EQ(depenencyListener.m_ready, 0);
AZ::Data::AssetLoadParameters loadParams(nullptr, AZ::Data::AssetDependencyLoadRules::LoadAll);
loadParams.m_reloadMissingDependencies = true;
auto loadDependencyRef = m_testAssetManager->GetAsset(NoLoadAssetId, azrtti_typeid<AssetWithAssetReference>(),
AZ::Data::AssetLoadBehavior::Default, loadParams);
while (!depenencyListener.m_ready || !readyListener.m_ready)
{
m_testAssetManager->DispatchEvents();
if (AZStd::chrono::system_clock::now() > maxTimeout)
{
break;
}
AZStd::this_thread::yield();
}
// Dispatch AssetBus events until the NoLoadAssetId and the MyAsset2Id has signaled
// an OnAssetReady event or the timeout has been reached
EXPECT_TRUE(DispatchEventsUntilCondition(*m_testAssetManager, AssetAndDependencyReady))
<< "The DispatchEventsUntiTimeout function has not completed in "
<< MaxDispatchTimeoutSeconds.count() << " seconds. The test will be marked as a failure\n";
EXPECT_EQ(readyListener.m_ready, 1);
EXPECT_EQ(depenencyListener.m_ready, 1);
EXPECT_EQ(dependencyListener.m_ready, 1);
EXPECT_TRUE(DispatchEventsUntilCondition(*m_testAssetManager, AssetContainerReady))
<< "The DispatchEventsUntiTimeout function has not completed in "
<< MaxDispatchTimeoutSeconds.count() << " seconds. The test will be marked as a failure\n";
}
CheckFinishedCreationsAndDestructions();
@@ -6,6 +6,7 @@
*
*/
#include <AzCore/Asset/AssetManager.h>
#include <AzCore/Asset/AssetSerializer.h>
#include <AzCore/Console/IConsole.h>
#include <AzCore/Interface/Interface.h>
#include <AzCore/IO/SystemFile.h>
@@ -7,6 +7,7 @@
*/
#include <Tests/Asset/BaseAssetManagerTest.h>
#include <AzCore/Asset/AssetSerializer.h>
#include <AzCore/Console/IConsole.h>
#include <AzCore/Interface/Interface.h>
#include <AzCore/IO/SystemFile.h>
@@ -10,6 +10,7 @@
#include <AzCore/Asset/AssetJsonSerializer.h>
#include <AzCore/Asset/AssetManager.h>
#include <AzCore/Asset/AssetManagerComponent.h>
#include <AzCore/Asset/AssetSerializer.h>
#include <AzCore/IO/Streamer/StreamerComponent.h>
#include <AzCore/Jobs/JobManagerComponent.h>
#include <AzCore/Jobs/JobManager.h>
@@ -6,6 +6,7 @@
*
*/
#include <AzCore/Asset/AssetManager.h>
#include <AzCore/Asset/AssetSerializer.h>
#include <AzCore/Console/IConsole.h>
#include <AzCore/Interface/Interface.h>
#include <AzCore/IO/SystemFile.h>
+5 -5
View File
@@ -151,7 +151,7 @@ namespace UnitTest
AZStd::thread m_threads[m_maxNumThreads];
for (unsigned int i = 0; i < m_maxNumThreads; ++i)
{
m_threads[i] = AZStd::thread(AZStd::bind(&SystemAllocatorTest::ThreadFunc, this), &m_desc[i]);
m_threads[i] = AZStd::thread(m_desc[i], AZStd::bind(&SystemAllocatorTest::ThreadFunc, this));
// give some time offset to the threads so we can test alloc and dealloc at the same time.
//AZStd::this_thread::sleep_for(AZStd::chrono::microseconds(500));
}
@@ -286,7 +286,7 @@ namespace UnitTest
AZStd::thread m_threads[m_maxNumThreads];
for (unsigned int i = 0; i < m_maxNumThreads; ++i)
{
m_threads[i] = AZStd::thread(AZStd::bind(&SystemAllocatorTest::ThreadFunc, this), &m_desc[i]);
m_threads[i] = AZStd::thread(m_desc[i], AZStd::bind(&SystemAllocatorTest::ThreadFunc, this));
// give some time offset to the threads so we can test alloc and dealloc at the same time.
AZStd::this_thread::sleep_for(AZStd::chrono::microseconds(500));
}
@@ -724,7 +724,7 @@ namespace UnitTest
AZStd::thread m_threads[m_maxNumThreads];
for (unsigned int i = 0; i < m_maxNumThreads; ++i)
{
m_threads[i] = AZStd::thread(AZStd::bind(&ThreadPoolAllocatorTest::AllocDeallocFunc, this), &m_desc[i]);
m_threads[i] = AZStd::thread(m_desc[i], AZStd::bind(&ThreadPoolAllocatorTest::AllocDeallocFunc, this));
}
for (unsigned int i = 0; i < m_maxNumThreads; ++i)
@@ -743,12 +743,12 @@ namespace UnitTest
for (unsigned int i = m_maxNumThreads/2; i <m_maxNumThreads; ++i)
{
m_threads[i] = AZStd::thread(AZStd::bind(&ThreadPoolAllocatorTest::SharedDeAlloc, this), &m_desc[i]);
m_threads[i] = AZStd::thread(m_desc[i], AZStd::bind(&ThreadPoolAllocatorTest::SharedDeAlloc, this));
}
for (unsigned int i = 0; i < m_maxNumThreads/2; ++i)
{
m_threads[i] = AZStd::thread(AZStd::bind(&ThreadPoolAllocatorTest::SharedAlloc, this), &m_desc[i]);
m_threads[i] = AZStd::thread(m_desc[i], AZStd::bind(&ThreadPoolAllocatorTest::SharedAlloc, this));
}
for (unsigned int i = 0; i < m_maxNumThreads/2; ++i)
@@ -9,6 +9,7 @@
#include "FileIOBaseTestTypes.h"
#include <AzCore/Asset/AssetManager.h>
#include <AzCore/Asset/AssetSerializer.h>
#include <AzCore/Component/ComponentApplicationBus.h>
#include <AzCore/Serialization/SerializeContext.h>
+81 -3
View File
@@ -34,7 +34,7 @@ namespace UnitTest
AZ::AllocatorInstance<AZ::PoolAllocator>::Create();
AZ::AllocatorInstance<AZ::ThreadPoolAllocator>::Create();
m_executor = aznew TaskExecutor(4);
m_executor = aznew TaskExecutor();
}
void TearDown() override
@@ -236,6 +236,82 @@ namespace UnitTest
EXPECT_EQ(x, 1);
}
TEST_F(TaskGraphTestFixture, SingleTask)
{
AZStd::atomic_int32_t x = 0;
TaskGraph graph;
graph.AddTask(
defaultTD,
[&x]
{
x = 1;
});
TaskGraphEvent ev;
graph.SubmitOnExecutor(*m_executor, &ev);
ev.Wait();
EXPECT_EQ(1, x);
}
TEST_F(TaskGraphTestFixture, SingleTaskChain)
{
AZStd::atomic_int32_t x = 0;
TaskGraph graph;
auto a = graph.AddTask(
defaultTD,
[&x]
{
x += 1;
});
auto b = graph.AddTask(
defaultTD,
[&x]
{
x += 1;
});
b.Precedes(a);
TaskGraphEvent ev;
graph.SubmitOnExecutor(*m_executor, &ev);
ev.Wait();
EXPECT_EQ(2, x);
}
TEST_F(TaskGraphTestFixture, MultipleIndependentTaskChains)
{
AZStd::atomic_int32_t x = 0;
constexpr int numChains = 5;
TaskGraph graph;
for( int i = 0; i < numChains; ++i)
{
auto a = graph.AddTask(
defaultTD,
[&x]
{
x += 1;
});
auto b = graph.AddTask(
defaultTD,
[&x]
{
x += 1;
});
b.Precedes(a);
}
TaskGraphEvent ev;
graph.SubmitOnExecutor(*m_executor, &ev);
ev.Wait();
EXPECT_EQ(2*numChains, x);
}
TEST_F(TaskGraphTestFixture, VariadicInterface)
{
int x = 0;
@@ -388,6 +464,7 @@ namespace UnitTest
EXPECT_EQ(3, x);
}
// Waiting inside a task is disallowed , test that it fails correctly
TEST_F(TaskGraphTestFixture, SpawnSubgraph)
{
AZStd::atomic<int> x = 0;
@@ -434,7 +511,10 @@ namespace UnitTest
f.Precedes(g);
TaskGraphEvent ev;
subgraph.SubmitOnExecutor(*m_executor, &ev);
// TaskGraphEvent::Wait asserts if called on a worker thread, suppress & validate assert
AZ_TEST_START_TRACE_SUPPRESSION;
ev.Wait();
AZ_TEST_STOP_TRACE_SUPPRESSION(1);
});
auto d = graph.AddTask(
defaultTD,
@@ -464,8 +544,6 @@ namespace UnitTest
TaskGraphEvent ev;
graph.SubmitOnExecutor(*m_executor, &ev);
ev.Wait();
EXPECT_EQ(3 | 0b100000, x);
}
TEST_F(TaskGraphTestFixture, RetainedGraph)
@@ -7,6 +7,7 @@
*/
#include <AzCore/UnitTest/TestTypes.h>
#include <AzCore/Math/Uuid.h>
#include <AzCore/std/containers/unordered_set.h>
using namespace AZ;
@@ -28,6 +28,7 @@
#include <AzCore/NativeUI/NativeUISystemComponent.h>
#include <AzCore/Module/ModuleManagerBus.h>
#include <AzCore/Interface/Interface.h>
#include <AzCore/Task/TaskGraphSystemComponent.h>
#include <AzFramework/Asset/SimpleAsset.h>
#include <AzFramework/Asset/AssetBundleManifest.h>
@@ -295,6 +296,7 @@ namespace AzFramework
azrtti_typeid<AZ::ScriptSystemComponent>(),
azrtti_typeid<AZ::JobManagerComponent>(),
azrtti_typeid<AZ::SliceSystemComponent>(),
azrtti_typeid<AZ::TaskGraphSystemComponent>(),
azrtti_typeid<AzFramework::AssetCatalogComponent>(),
azrtti_typeid<AzFramework::CustomAssetTypeComponent>(),
@@ -477,14 +479,16 @@ namespace AzFramework
newThreadDesc.m_cpuId = AFFINITY_MASK_USERTHREADS;
newThreadDesc.m_name = newThreadName;
AZStd::binary_semaphore binarySemaphore;
AZStd::thread newThread([&workForNewThread, &binarySemaphore, &newThreadName]
{
AZ_PROFILE_SCOPE(AzFramework,
"Application::PumpSystemEventLoopWhileDoingWorkInNewThread:ThreadWorker %s", newThreadName);
AZStd::thread newThread(
newThreadDesc,
[&workForNewThread, &binarySemaphore, &newThreadName]
{
AZ_PROFILE_SCOPE(AzFramework,
"Application::PumpSystemEventLoopWhileDoingWorkInNewThread:ThreadWorker %s", newThreadName);
workForNewThread();
binarySemaphore.release();
}, &newThreadDesc);
workForNewThread();
binarySemaphore.release();
});
while (!binarySemaphore.try_acquire_for(eventPumpFrequency))
{
PumpSystemEventLoopUntilEmpty();
@@ -1631,7 +1631,20 @@ namespace AZ::IO
return nullptr;
}
ZipDir::CacheFactory factory(ZipDir::ZD_INIT_FAST, nFactoryFlags);
ZipDir::InitMethod initType = ZipDir::InitMethod::Default;
if (!ZipDir::IsReleaseConfig)
{
if ((nFlags & INestedArchive::FLAGS_FULL_VALIDATE) != 0)
{
initType = ZipDir::InitMethod::FullValidation;
}
else if ((nFlags & INestedArchive::FLAGS_VALIDATE_HEADERS) != 0)
{
initType = ZipDir::InitMethod::ValidateHeaders;
}
}
ZipDir::CacheFactory factory(initType, nFactoryFlags);
ZipDir::CachePtr cache = factory.New(szFullPath->c_str());
if (cache)
@@ -11,7 +11,9 @@
#include <AzCore/IO/Path/Path_fwd.h>
#include <AzCore/Math/Crc.h>
#include <AzCore/std/containers/vector.h>
#include <AzCore/std/smart_ptr/intrusive_base.h>
#include <AzCore/std/string/string.h>
#include <AzFramework/Archive/Codec.h>
namespace AZ::IO
@@ -71,6 +73,13 @@ namespace AZ::IO
// multiple times
FLAGS_DONT_COMPACT = 1 << 5,
// if this is set, validate header data when opening the archive
FLAGS_VALIDATE_HEADERS = 1 << 9,
// if this is set, validate header data when opening the archive and validate CRCs when decompressing
// & reading files.
FLAGS_FULL_VALIDATE = 1 << 10,
// Disable a pak file without unloading it, this flag is used in combination with patches and multiplayer
// to ensure that specific paks stay in the position(to keep the same priority) but being disabled
// when running multiplayer
@@ -128,6 +137,10 @@ namespace AZ::IO
// Deletes all files and directories in the archive.
virtual int RemoveAll() = 0;
// Summary:
// Lists all the files in the archive.
virtual int ListAllFiles(AZStd::vector<AZ::IO::Path>& outFileEntries) = 0;
// Summary:
// Finds the file; you don't have to close the returned handle.
// Returns:
@@ -89,11 +89,51 @@ namespace AZ::IO
return m_pCache->RemoveDir(fullPath);
}
//////////////////////////////////////////////////////////////////////////
int NestedArchive::RemoveAll()
{
return m_pCache->RemoveAll();
}
//////////////////////////////////////////////////////////////////////////
// Helper for 'ListAllFiles' to recursively traverse the FileEntryTree and gather all the files
void EnumerateFilesRecursive(AZ::IO::Path currentPath, ZipDir::FileEntryTree* currentTree, AZStd::vector<AZ::IO::Path>& fileList)
{
// Drill down directories first...
for (auto dirIter = currentTree->GetDirBegin(); dirIter != currentTree->GetDirEnd(); ++dirIter)
{
if (ZipDir::FileEntryTree* subTree = currentTree->GetDirEntry(dirIter);
subTree != nullptr)
{
EnumerateFilesRecursive(currentPath / currentTree->GetDirName(dirIter), subTree, fileList);
}
}
// Then enumerate the files in current directory...
for (auto fileIter = currentTree->GetFileBegin(); fileIter != currentTree->GetFileEnd(); ++fileIter)
{
fileList.emplace_back(currentPath / currentTree->GetFileName(fileIter));
}
}
//////////////////////////////////////////////////////////////////////////
// lists all files in the archive
int NestedArchive::ListAllFiles(AZStd::vector<AZ::IO::Path>& outFileEntries)
{
AZStd::vector<AZ::IO::Path> filesInArchive;
ZipDir::FileEntryTree* tree = m_pCache->GetRoot();
if (!tree)
{
return ZipDir::ZD_ERROR_UNEXPECTED;
}
EnumerateFilesRecursive(AZ::IO::Path{ AZ::IO::PosixPathSeparator }, tree, filesInArchive);
AZStd::swap(outFileEntries, filesInArchive);
return ZipDir::ZD_ERROR_SUCCESS;
}
//////////////////////////////////////////////////////////////////////////
// Adds a new file to the zip or update an existing one
// adds a directory (creates several nested directories if needed)
@@ -39,7 +39,7 @@ namespace AZ::IO
NestedArchive(IArchive* pArchive, AZStd::string_view strBindRoot, ZipDir::CachePtr pCache, uint32_t nFlags = 0);
~NestedArchive() override;
auto GetRootFolderHandle() -> Handle override;
// Adds a new file to the zip or update an existing one
@@ -68,6 +68,9 @@ namespace AZ::IO
// deletes all files from the archive
int RemoveAll() override;
// lists all the files in the archive
int ListAllFiles(AZStd::vector<AZ::IO::Path>& outFileEntries) override;
// finds the file; you don't have to close the returned handle
Handle FindFile(AZStd::string_view szRelativePath) override;
@@ -79,7 +82,6 @@ namespace AZ::IO
// returns the full path to the archive file
AZ::IO::PathView GetFullPath() const override;
ZipDir::Cache* GetCache();
uint32_t GetFlags() const override;
bool SetFlags(uint32_t nFlagsToSet) override;
@@ -87,12 +89,15 @@ namespace AZ::IO
bool SetPackAccessible(bool bAccessible) override;
ZipDir::Cache* GetCache();
protected:
// returns the pointer to the relative file path to be passed
// to the underlying Cache pointer. Uses the given buffer to construct the path.
// returns nullptr if the file path is invalid
AZ::IO::FixedMaxPathString AdjustPath(AZStd::string_view szRelativePath);
ZipDir::CachePtr m_pCache;
// the binding root may be empty string - in this case, the absolute path binding won't work
AZ::IO::Path m_strBindRoot;
@@ -101,10 +101,11 @@ namespace AZ::IO::ZipDir
FileEntry* operator -> () { return m_pFileEntry; }
FileEntryTransactionAdd(Cache* pCache, AZStd::string_view szRelativePath)
: m_pCache(pCache)
, m_szRelativePath(AZ::IO::PosixPathSeparator)
, m_bCommitted(false)
{
// Update the cache string pool with the relative path to the file
auto pathIt = m_pCache->m_relativePathPool.emplace(AZ::IO::PathView(szRelativePath).LexicallyNormal());
auto pathIt = m_pCache->m_relativePathPool.emplace(AZ::IO::PathView(szRelativePath, AZ::IO::PosixPathSeparator).LexicallyNormal());
m_szRelativePath = *pathIt.first;
// this is the name of the directory - create it or find it
m_pFileEntry = m_pCache->GetRoot()->Add(m_szRelativePath.Native());
@@ -740,6 +741,16 @@ namespace AZ::IO::ZipDir
{
return ZD_ERROR_CORRUPTED_DATA;
}
if (pFileEntry->bCheckCRCNextRead)
{
pFileEntry->bCheckCRCNextRead = false;
uLong uCRC32 = AZ::Crc32((Bytef*)pUncompressed, nSizeUncompressed);
if (uCRC32 != pFileEntry->desc.lCRC32)
{
AZ_Warning("Archive", false, "ZD_ERROR_CRC32_CHECK: Uncompressed stream CRC32 check failed");
return ZD_ERROR_CRC32_CHECK;
}
}
}
}
@@ -18,6 +18,7 @@
#include <AzCore/IO/FileIO.h>
#include <AzCore/IO/Path/Path.h>
#include <AzCore/Memory/PoolAllocator.h>
#include <AzCore/std/containers/unordered_set.h>
#include <AzCore/std/smart_ptr/intrusive_base.h>
#include <AzFramework/Archive/Codec.h>
#include <AzFramework/Archive/ZipDirStructures.h>
@@ -29,7 +29,7 @@ namespace AZ::IO::ZipDir
// this sets the window size of the blocks of data read from the end of the file to find the Central Directory Record
// since normally there are no
static constexpr size_t CDRSearchWindowSize = 0x100;
CacheFactory::CacheFactory(InitMethodEnum nInitMethod, uint32_t nFlags)
CacheFactory::CacheFactory(InitMethod nInitMethod, uint32_t nFlags)
{
m_nCDREndPos = 0;
m_bBuildFileEntryMap = false; // we only need it for validation/debugging
@@ -448,7 +448,6 @@ namespace AZ::IO::ZipDir
// builds up the m_mapFileEntries
bool CacheFactory::BuildFileEntryMap()
{
Seek(m_CDREnd.lCDROffset);
if (m_CDREnd.lCDRSize == 0)
@@ -530,14 +529,6 @@ namespace AZ::IO::ZipDir
{
// Add this file entry.
char* str = reinterpret_cast<char*>(pFileName);
for (int i = 0; i < pFile->nFileNameLength; i++)
{
str[i] = std::tolower(str[i], std::locale());
if (str[i] == AZ_WRONG_FILESYSTEM_SEPARATOR)
{
str[i] = AZ_CORRECT_FILESYSTEM_SEPARATOR;
}
}
str[pFile->nFileNameLength] = 0; // Not standard!, may overwrite signature of the next memory record data in zip.
AddFileEntry(str, pFile, extra);
}
@@ -574,11 +565,7 @@ namespace AZ::IO::ZipDir
FileEntryBase fileEntry(*pFileHeader, extra);
// when using encrypted headers we should always initialize data offsets from CDR
if ((m_encryptedHeaders != ZipFile::HEADERS_NOT_ENCRYPTED || m_nInitMethod >= ZD_INIT_FULL) && pFileHeader->desc.lSizeCompressed)
{
InitDataOffset(fileEntry, pFileHeader);
}
InitDataOffset(fileEntry, pFileHeader);
if (m_bBuildFileEntryMap)
{
@@ -606,142 +593,81 @@ namespace AZ::IO::ZipDir
{
Seek(pFileHeader->lLocalHeaderOffset);
// read the local file header and the name (for validation) into the buffer
AZStd::vector<char>pBuffer;
uint32_t nBufferLength = sizeof(ZipFile::LocalFileHeader) + pFileHeader->nFileNameLength;
pBuffer.resize(nBufferLength);
Read(&pBuffer[0], nBufferLength);
// Read only the LocalFileHeader w/ no additional bytes ('name' or 'extra' fields)
AZStd::vector<char> buffer;
uint32_t bufferLen = sizeof(ZipFile::LocalFileHeader);
buffer.resize_no_construct(bufferLen);
Read(buffer.data(), bufferLen);
// validate the local file header (compare with the CDR file header - they should contain basically the same information)
const auto* pLocalFileHeader = reinterpret_cast<const ZipFile::LocalFileHeader*>(&pBuffer[0]);
if (pFileHeader->desc != pLocalFileHeader->desc
|| pFileHeader->nMethod != pLocalFileHeader->nMethod
|| pFileHeader->nFileNameLength != pLocalFileHeader->nFileNameLength
// for a tough validation, we can compare the timestamps of the local and central directory entries
// but we won't do that for backward compatibility with ZipDir
//|| pFileHeader->nLastModDate != pLocalFileHeader->nLastModDate
//|| pFileHeader->nLastModTime != pLocalFileHeader->nLastModTime
)
const auto* localFileHeader = reinterpret_cast<const ZipFile::LocalFileHeader*>(buffer.data());
// set the correct file data offset...
fileEntry.nFileDataOffset = pFileHeader->lLocalHeaderOffset + sizeof(ZipFile::LocalFileHeader) +
localFileHeader->nFileNameLength + localFileHeader->nExtraFieldLength;
fileEntry.nEOFOffset = fileEntry.nFileDataOffset + fileEntry.desc.lSizeCompressed;
if (m_nInitMethod != ZipDir::InitMethod::Default)
{
AZ_Warning("Archive", false, "ZD_ERROR_VALIDATION_FAILED:"
" The local file header descriptor doesn't match the basic parameters declared in the global file header in the file."
" The archive content is misconsistent and may be damaged. Please try to repair the archive");
return;
if (m_nInitMethod == ZipDir::InitMethod::FullValidation)
{
// Mark the FileEntry to check CRC when the next read occurs
fileEntry.bCheckCRCNextRead = true;
}
// Timestamps
if (pFileHeader->nLastModDate != localFileHeader->nLastModDate
|| pFileHeader->nLastModTime != localFileHeader->nLastModTime)
{
AZ_Warning("Archive", false, "ZD_ERROR_VALIDATION_FAILED: (%s)\n"
" The local file header's modification timestamps don't match that of the global file header in the archive."
" The archive timestamps are inconsistent and may be damaged. Check the archive file.", m_szFilename.c_str());
// don't return here, it may be ok.
}
// Validate data
if (pFileHeader->desc != localFileHeader->desc // this checks CRCs and compressed/uncompressed sizes
|| pFileHeader->nMethod != localFileHeader->nMethod
|| pFileHeader->nFileNameLength != localFileHeader->nFileNameLength)
{
AZ_Warning("Archive", false, "ZD_ERROR_VALIDATION_FAILED: (%s)\n"
" The local file header descriptor doesn't match basic parameters declared in the global file header in the file."
" The archive content is inconsistent and may be damaged. Please try to repair the archive.", m_szFilename.c_str());
// return here because further checks aren't worse than this.
return;
}
// Read extra data
uint32_t extraDataLen = localFileHeader->nFileNameLength + localFileHeader->nExtraFieldLength;
buffer.resize_no_construct(buffer.size() + extraDataLen);
Read(buffer.data() + buffer.size(), extraDataLen);
// Compare local file name with the CDR file name, they should match
AZStd::string_view zipFileName{ buffer.data() + sizeof(ZipFile::LocalFileHeader), localFileHeader->nFileNameLength };
AZStd::string_view cdrFileName{ reinterpret_cast<const char*>(pFileHeader + 1), pFileHeader->nFileNameLength };
if (zipFileName != cdrFileName)
{
AZ_Warning("Archive", false, "ZD_ERROR_VALIDATION_FAILED: (%s)\n"
" The file name in the local file header doesn't match the name in the global file header."
" The archive content is inconsisten with the directory. Please check the archive.", m_szFilename.c_str());
}
// CDR and local "extra field" lengths may be different, should we compare them if they are equal?
// make sure it's the same file and the fileEntry structure is properly initialized
AZ_Assert(fileEntry.nFileHeaderOffset == pFileHeader->lLocalHeaderOffset,
"The file entry header offset doesn't match the file header local offst (%s)", m_szFilename.c_str());
if (fileEntry.nFileDataOffset >= m_nCDREndPos)
{
AZ_Warning("Archive", false, "ZD_ERROR_VALIDATION_FAILED: (%s)\n"
" The global file header declares the file which crosses the boundaries of the archive."
" The archive is either corrupted or truncated, please try to repair it", m_szFilename.c_str());
}
// End Validation
}
// now compare the local file name with the one recorded in CDR: they must match.
auto CompareNoCase = [](const char lhs, const char rhs) { return std::tolower(lhs, std::locale()) == std::tolower(rhs, std::locale()); };
auto zipFileDataBegin = pBuffer.begin() + sizeof(ZipFile::LocalFileHeader);
auto zipFileDataEnd = zipFileDataBegin + pFileHeader->nFileNameLength;
if (!AZStd::equal(zipFileDataBegin, zipFileDataEnd, reinterpret_cast<const char*>(pFileHeader + 1), CompareNoCase))
{
// either file name, or the extra field do not match
AZ_Warning("Archive", false, "ZD_ERROR_VALIDATION_FAILED:"
" The local file header contains file name which does not match the file name of the global file header."
" The archive content is misconsistent with its directory. Please repair the archive");
return;
}
fileEntry.nFileDataOffset = pFileHeader->lLocalHeaderOffset + sizeof(ZipFile::LocalFileHeader) + pLocalFileHeader->nFileNameLength + pLocalFileHeader->nExtraFieldLength;
}
// make sure it's the same file and the fileEntry structure is properly initialized
AZ_Assert(fileEntry.nFileHeaderOffset == pFileHeader->lLocalHeaderOffset, "The file entry header offset doesn't match the file header local offst");
fileEntry.nEOFOffset = fileEntry.nFileDataOffset + fileEntry.desc.lSizeCompressed;
if (fileEntry.nFileDataOffset >= m_nCDREndPos)
{
AZ_Warning("Archive", false, "ZD_ERROR_VALIDATION_FAILED:"
" The global file header declares the file which crosses the boundaries of the archive."
" The archive is either corrupted or truncated, please try to repair it");
return;
}
if (m_nInitMethod >= ZD_INIT_VALIDATE)
{
Validate(fileEntry);
}
}
//////////////////////////////////////////////////////////////////////////
// reads the file pointed by the given header and entry (they must be coherent)
// and decompresses it; then calculates and validates its CRC32
void CacheFactory::Validate(const FileEntryBase& fileEntry)
{
AZStd::vector<char> pBuffer;
// validate the file contents
// allocate memory for both the compressed data and uncompressed data
pBuffer.resize(fileEntry.desc.lSizeCompressed + fileEntry.desc.lSizeUncompressed);
char* pUncompressed = &pBuffer[fileEntry.desc.lSizeCompressed];
char* pCompressed = &pBuffer[0];
AZ_Assert(fileEntry.nFileDataOffset != FileEntry::INVALID_DATA_OFFSET, "File entry has invalid data offset of %" PRIx32, FileEntry::INVALID_DATA_OFFSET);
Seek(fileEntry.nFileDataOffset);
Read(pCompressed, fileEntry.desc.lSizeCompressed);
size_t nDestSize = fileEntry.desc.lSizeUncompressed;
int nError = Z_OK;
if (fileEntry.nMethod)
{
nError = ZipRawUncompress(pUncompressed, &nDestSize, pCompressed, fileEntry.desc.lSizeCompressed);
}
else
{
AZ_Assert(fileEntry.desc.lSizeCompressed == fileEntry.desc.lSizeUncompressed, "Uncompressed file does not have the same commpressed %u and uncompressed file sizes %u",
fileEntry.desc.lSizeCompressed, fileEntry.desc.lSizeUncompressed);
memcpy(pUncompressed, pCompressed, fileEntry.desc.lSizeUncompressed);
}
switch (nError)
{
case Z_OK:
break;
case Z_MEM_ERROR:
AZ_Warning("Archive", false, "ZD_ERROR_ZLIB_NO_MEMORY: ZLib reported out-of-memory error");
return;
case Z_BUF_ERROR:
AZ_Warning("Archive", false, "ZD_ERROR_ZLIB_CORRUPTED_DATA: ZLib reported compressed stream buffer error");
return;
case Z_DATA_ERROR:
AZ_Warning("Archive", false, "ZD_ERROR_ZLIB_CORRUPTED_DATA: ZLib reported compressed stream data error");
return;
default:
AZ_Warning("Archive", false, "ZD_ERROR_ZLIB_FAILED: ZLib reported an unexpected unknown error");
return;
}
if (nDestSize != fileEntry.desc.lSizeUncompressed)
{
AZ_Warning("Archive", false, "ZD_ERROR_CORRUPTED_DATA: Uncompressed stream doesn't match the size of uncompressed file stored in the archive file headers");
return;
}
uLong uCRC32 = AZ::Crc32((Bytef*)pUncompressed, nDestSize);
if (uCRC32 != fileEntry.desc.lCRC32)
{
AZ_Warning("Archive", false, "ZD_ERROR_CRC32_CHECK: Uncompressed stream CRC32 check failed");
return;
}
}
//////////////////////////////////////////////////////////////////////////
// extracts the file path from the file header with subsequent information
// may, or may not, put all letters to lower-case (depending on whether the system is to be case-sensitive or not)
// it's the responsibility of the caller to ensure that the file name is in readable valid memory
char* CacheFactory::GetFilePath(const char* pFileName, uint16_t nFileNameLength)
{
static char strResult[AZ_MAX_PATH_LEN];
AZ_Assert(nFileNameLength < AZ_MAX_PATH_LEN, "Only filenames shorter than %zu can be copied from filename parameter", AZ_MAX_PATH_LEN);
memcpy(strResult, pFileName, nFileNameLength);
strResult[nFileNameLength] = 0;
for (int i = 0; i < nFileNameLength; i++)
{
strResult[i] = std::tolower(strResult[i], std::locale{});
}
return strResult;
}
// seeks in the file relative to the starting position
@@ -39,7 +39,7 @@ namespace AZ::IO::ZipDir
// initializes the internal structures
// nFlags can have FLAGS_READ_ONLY flag, in this case the object will be opened only for reading
CacheFactory(InitMethodEnum nInitMethod, uint32_t nFlags = 0);
CacheFactory(InitMethod nInitMethod, uint32_t nFlags = 0);
~CacheFactory();
// the new function creates a new cache
@@ -66,28 +66,6 @@ namespace AZ::IO::ZipDir
// This function can actually modify strFilePath variable, make sure you use a copy of the real path.
void AddFileEntry(char* strFilePath, const ZipFile::CDRFileHeader* pFileHeader, const SExtraZipFileData& extra);// throw (ErrorEnum);
// extracts the file path from the file header with subsequent information
// may, or may not, put all letters to lower-case (depending on whether the system is to be case-sensitive or not)
// it's the responsibility of the caller to ensure that the file name is in readable valid memory
char* GetFilePath(const ZipFile::CDRFileHeader* pFileHeader)
{
return GetFilePath((const char*)(pFileHeader + 1), pFileHeader->nFileNameLength);
}
// extracts the file path from the file header with subsequent information
// may, or may not, put all letters to lower-case (depending on whether the system is to be case-sensitive or not)
// it's the responsibility of the caller to ensure that the file name is in readable valid memory
char* GetFilePath(const ZipFile::LocalFileHeader* pFileHeader)
{
return GetFilePath((const char*)(pFileHeader + 1), pFileHeader->nFileNameLength);
}
// extracts the file path from the file header with subsequent information
// may, or may not, put all letters to lower-case (depending on whether the system is to be case-sensitive or not)
// it's the responsibility of the caller to ensure that the file name is in readable valid memory
char* GetFilePath(const char* pFileName, uint16_t nFileNameLength);
// validates (if the init method has the corresponding value) the given file/header
void Validate(const FileEntryBase& fileEntry);
// initializes the actual data offset in the file in the fileEntry structure
// searches to the local file header, reads it and calculates the actual offset in the file
void InitDataOffset(FileEntryBase& fileEntry, const ZipFile::CDRFileHeader* pFileHeader);
@@ -104,7 +82,7 @@ namespace AZ::IO::ZipDir
AZStd::string m_szFilename;
CZipFile m_fileExt;
InitMethodEnum m_nInitMethod;
InitMethod m_nInitMethod;
uint32_t m_nFlags;
ZipFile::CDREnd m_CDREnd;
@@ -129,7 +107,7 @@ namespace AZ::IO::ZipDir
ZipFile::CryCustomEncryptionHeader m_headerEncryption;
ZipFile::CrySignedCDRHeader m_headerSignature;
ZipFile::CryCustomExtendedHeader m_headerExtended;
};
}
@@ -68,14 +68,14 @@ namespace AZ::IO::ZipDir
{
for (FileEntryTree::SubdirMap::iterator it = pTree->GetDirBegin(); it != pTree->GetDirEnd(); ++it)
{
AddAllFiles(it->second.get(), (AZ::IO::Path(strRoot) / it->first).Native());
AddAllFiles(it->second.get(), (AZ::IO::Path(strRoot, AZ::IO::PosixPathSeparator) / it->first).Native());
}
for (FileEntryTree::FileMap::iterator it = pTree->GetFileBegin(); it != pTree->GetFileEnd(); ++it)
{
FileRecord rec;
rec.pFileEntryBase = pTree->GetFileEntry(it);
rec.strPath = (AZ::IO::Path(strRoot) / it->first).Native();
rec.strPath = (AZ::IO::Path(strRoot, AZ::IO::PosixPathSeparator) / it->first).Native();
push_back(rec);
}
}
@@ -119,19 +119,28 @@ namespace AZ::IO::ZipDir
const char* m_szDescription;
};
#if defined(_RELEASE)
inline static constexpr bool IsReleaseConfig{ true };
#else
inline static constexpr bool IsReleaseConfig{};
#endif // _RELEASE
// possible initialization methods
enum InitMethodEnum
enum class InitMethod
{
// initialize as fast as possible, with minimal validation
ZD_INIT_FAST,
// after initialization, scan through all file headers, precache the actual file data offset values and validate the headers
ZD_INIT_FULL,
// scan all file headers and try to decompress the data, searching for corrupted files
ZD_INIT_VALIDATE_IN_MEMORY,
// store archive in memory
ZD_INIT_VALIDATE,
// maximum level of validation, checks for integrity of the archive
ZD_INIT_VALIDATE_MAX = ZD_INIT_VALIDATE
// initializes without any sort of extra validation steps
Default,
// initializes with extra validation steps
// not available in RELEASE
// will check CDR and local headers data match
ValidateHeaders,
// initializes with extra validation steps
// not available in RELEASE
// will check CDR and local headers data match
// will check file data CRC matches (when file is read)
FullValidation,
};
// Uncompresses raw (without wrapping) data that is compressed with method 8 (deflated) in the Zip file
@@ -184,7 +193,11 @@ namespace AZ::IO::ZipDir
// the offset to the start of the next file's header - this
// can be used to calculate the available space in zip file
uint32_t nEOFOffset{};
// whether to check the CRC upon the next data read
bool bCheckCRCNextRead{};
};
// this is the record about the file in the Zip file.
struct FileEntry
: FileEntryBase
@@ -8,7 +8,10 @@
#pragma once
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/RTTI/TypeInfo.h>
#include <AzCore/Memory/SystemAllocator.h>
#include <AzCore/std/string/string.h>
#include <AzCore/std/containers/vector.h>
namespace AZ
{
@@ -8,6 +8,7 @@
*/
#include <AzFramework/Asset/AssetSeedList.h>
#include <AzCore/Serialization/SerializeContext.h>
namespace AzFramework
{
@@ -8,7 +8,6 @@
*/
#pragma once
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/std/containers/vector.h>
#include <AzCore/Asset/AssetCommon.h>
#include <AzFramework/Platform/PlatformDefaults.h>
@@ -7,6 +7,7 @@
*/
#include <AzFramework/Asset/Benchmark/BenchmarkAsset.h>
#include <AzCore/Asset/AssetSerializer.h>
namespace AzFramework
{
@@ -7,6 +7,7 @@
*/
#include <AzCore/Serialization/EditContext.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzFramework/Asset/SimpleAsset.h>
namespace AzFramework
@@ -36,4 +37,36 @@ namespace AzFramework
return "";
}
void SimpleAssetReferenceBase::Reflect(AZ::ReflectContext *context)
{
if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
{
serializeContext->Class<SimpleAssetReferenceBase>()
->Version(1)
->Field("AssetPath", &SimpleAssetReferenceBase::m_assetPath);
AZ::EditContext* edit = serializeContext->GetEditContext();
if (edit)
{
edit->Class<SimpleAssetReferenceBase>("Asset path", "Asset reference as a project-relative path")
->ClassElement(AZ::Edit::ClassElements::EditorData, "")
->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::Hide)
;
}
}
if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
{
behaviorContext->Class<SimpleAssetReferenceBase>()
->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
->Attribute(AZ::Script::Attributes::Category, "Asset")
->Attribute(AZ::Script::Attributes::Module, "asset")
->Property("assetPath", &SimpleAssetReferenceBase::GetAssetPath, nullptr)
->Property("assetType", &SimpleAssetReferenceBase::GetAssetType, nullptr)
->Property("fileFilter", &SimpleAssetReferenceBase::GetFileFilter, nullptr)
->Method("SetAssetPath", &SimpleAssetReferenceBase::SetAssetPath)
->Attribute(AZ::Script::Attributes::Alias, "set_asset_path")
;
}
}
} // namespace AzFramework
@@ -42,7 +42,6 @@
*/
#include <AzCore/std/string/string.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/Serialization/EditContext.h>
#include <AzCore/RTTI/BehaviorContext.h>
#include <AzCore/Component/Component.h>
@@ -74,38 +73,7 @@ namespace AzFramework
virtual AZ::Data::AssetType GetAssetType() const = 0;
virtual const char* GetFileFilter() const = 0;
static void Reflect(AZ::ReflectContext* context)
{
if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
{
serializeContext->Class<SimpleAssetReferenceBase>()
->Version(1)
->Field("AssetPath", &SimpleAssetReferenceBase::m_assetPath);
AZ::EditContext* edit = serializeContext->GetEditContext();
if (edit)
{
edit->Class<SimpleAssetReferenceBase>("Asset path", "Asset reference as a project-relative path")
->ClassElement(AZ::Edit::ClassElements::EditorData, "")
->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::Hide)
;
}
}
if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
{
behaviorContext->Class<SimpleAssetReferenceBase>()
->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
->Attribute(AZ::Script::Attributes::Category, "Asset")
->Attribute(AZ::Script::Attributes::Module, "asset")
->Property("assetPath", &SimpleAssetReferenceBase::GetAssetPath, nullptr)
->Property("assetType", &SimpleAssetReferenceBase::GetAssetType, nullptr)
->Property("fileFilter", &SimpleAssetReferenceBase::GetFileFilter, nullptr)
->Method("SetAssetPath", &SimpleAssetReferenceBase::SetAssetPath)
->Attribute(AZ::Script::Attributes::Alias, "set_asset_path")
;
}
}
static void Reflect(AZ::ReflectContext* context);
protected:
@@ -9,6 +9,7 @@
#pragma once
#include <AzCore/std/containers/vector.h>
#include <AzCore/Asset/AssetCommon.h>
#include <AzCore/RTTI/BehaviorContext.h>
#include <AzCore/RTTI/ReflectContext.h>
#include <AzCore/Serialization/EditContext.h>
@@ -10,6 +10,11 @@
#include <AzCore/EBus/EBus.h>
namespace AZ
{
class Entity;
}
namespace AzFramework
{
using EntityContextId = AZ::Uuid;
@@ -8,6 +8,7 @@
#pragma once
#include <AzCore/std/smart_ptr/unique_ptr.h>
#include <AzFramework/Logging/MissingAssetNotificationBus.h>
namespace AzFramework { class LogFile; }
@@ -593,7 +593,7 @@ namespace AzFramework
DebugMessage("StartThread: Starting %s", thread.m_desc.m_name);
thread.m_join = false;
thread.m_thread = AZStd::thread(thread.m_main, &thread.m_desc);
thread.m_thread = AZStd::thread(thread.m_desc, thread.m_main);
}
void AssetProcessorConnection::JoinThread(ThreadState& thread, AZStd::condition_variable* wakeUpCondition /* = nullptr */)
@@ -13,6 +13,7 @@
#include <AzFramework/Physics/Ragdoll.h>
#include <AzFramework/Physics/Shape.h>
#include <AzFramework/Physics/SystemBus.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/std/smart_ptr/make_shared.h>
#include <AzCore/std/utils.h>
@@ -9,6 +9,7 @@
#include <AzFramework/Physics/Collision/CollisionEvents.h>
#include <AzCore/Memory/SystemAllocator.h>
#include <AzCore/RTTI/BehaviorContext.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzFramework/Physics/Common/PhysicsSimulatedBody.h>
@@ -9,6 +9,8 @@
#include <AzFramework/Physics/Collision/CollisionGroups.h>
#include <AzCore/Memory/SystemAllocator.h>
#include <AzCore/RTTI/BehaviorContext.h>
#include <AzCore/Script/ScriptContext.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzFramework/Physics/CollisionBus.h>
@@ -10,6 +10,7 @@
#include <AzCore/Interface/Interface.h>
#include <AzCore/Memory/SystemAllocator.h>
#include <AzCore/RTTI/BehaviorContext.h>
#include <AzCore/Serialization/EditContext.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/std/smart_ptr/make_shared.h>
@@ -8,6 +8,7 @@
#include <AzFramework/Physics/Configuration/SystemConfiguration.h>
#include <AzCore/Asset/AssetSerializer.h>
#include <AzCore/Memory/SystemAllocator.h>
#include <AzCore/Serialization/EditContext.h>
#include <AzCore/Serialization/SerializeContext.h>
@@ -5,6 +5,8 @@
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include <AzCore/Asset/AssetSerializer.h>
#include <AzCore/IO/FileIO.h>
#include <AzCore/Component/ComponentApplicationBus.h>
#include <AzCore/Interface/Interface.h>
@@ -9,6 +9,7 @@
#include <AzFramework/Physics/PhysicsSystem.h>
#include <AzFramework/Physics/Configuration/SceneConfiguration.h>
#include <AzCore/RTTI/BehaviorContext.h>
namespace AzPhysics
@@ -8,6 +8,8 @@
#include <AzFramework/Physics/PhysicsSystem.h>
#include <AzCore/RTTI/BehaviorContext.h>
namespace AzPhysics
{
void SystemInterface::Reflect(AZ::ReflectContext* context)
@@ -9,7 +9,6 @@
#pragma once
#include <AzFramework/Physics/ShapeConfiguration.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzFramework/Physics/Material.h>
#include <AzFramework/Physics/Collision/CollisionGroups.h>
#include <AzFramework/Physics/Collision/CollisionLayers.h>
@@ -7,9 +7,11 @@
*/
#include <AzFramework/Physics/ShapeConfiguration.h>
#include <AzCore/Asset/AssetSerializer.h>
#include <AzCore/Serialization/EditContext.h>
#include <AzFramework/Physics/PropertyTypes.h>
#include <AzFramework/Physics/SystemBus.h>
#include <AzCore/RTTI/BehaviorContext.h>
namespace Physics
{
@@ -10,6 +10,7 @@
#include <AzCore/Math/Vector3.h>
#include <AzCore/Math/Quaternion.h>
#include <AzCore/Asset/AssetCommon.h>
#include <AzCore/Serialization/SerializeContext.h>
namespace Physics
@@ -10,12 +10,14 @@
#include "Utils.h"
#include "Material.h"
#include "Shape.h"
#include <AzCore/RTTI/BehaviorContext.h>
#include <AzCore/Serialization/EditContext.h>
#include <AzFramework/Physics/AnimationConfiguration.h>
#include <AzFramework/Physics/CharacterBus.h>
#include <AzFramework/Physics/Character.h>
#include <AzFramework/Physics/Ragdoll.h>
#include <AzFramework/Physics/ShapeConfiguration.h>
#include <AzCore/Serialization/EditContext.h>
#include <AzFramework/Physics/CollisionBus.h>
#include <AzFramework/Physics/Components/SimulatedBodyComponentBus.h>
#include <AzFramework/Physics/WindBus.h>
@@ -11,6 +11,7 @@
#include <AzCore/Math/Vector2.h>
#include <AzCore/Math/Vector3.h>
#include <AzCore/Component/ComponentBus.h>
#include <AzCore/std/containers/unordered_set.h>
#include <AzCore/std/functional.h>
//! Common structures for Render geometry queries
@@ -14,6 +14,7 @@
#include <AzCore/Asset/AssetManagerBus.h>
#include <AzCore/Asset/AssetManager.h>
#include <AzCore/Asset/AssetSerializer.h>
#include <AzCore/Component/Entity.h>
#include <AzCore/Serialization/SerializeContext.h>
@@ -7,6 +7,7 @@
*/
#include <AzCore/Asset/AssetManager.h>
#include <AzCore/Asset/AssetSerializer.h>
#include <AzCore/Settings/SettingsRegistry.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzFramework/Spawnable/SpawnableMetaData.h>
@@ -7,6 +7,7 @@
*/
#include <AzFramework/StreamingInstall/StreamingInstallNotifications.h>
#include <AzCore/RTTI/BehaviorContext.h>
#include "StreamingInstall.h"
namespace AzFramework
@@ -9,10 +9,8 @@
#pragma once
#include <AzCore/EBus/EBus.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/Serialization/EditContext.h>
#include <AzCore/Serialization/EditContextConstants.inl>
#include <AzCore/RTTI/BehaviorContext.h>
namespace AzFramework
{
@@ -319,7 +319,7 @@ namespace AzFramework
AZStd::thread_desc td;
td.m_name = "TargetManager Thread";
td.m_cpuId = AFFINITY_MASK_USERTHREADS;
m_threadHandle = AZStd::thread(AZStd::bind(&TargetManagementComponent::TickThread, this), &td);
m_threadHandle = AZStd::thread(td, AZStd::bind(&TargetManagementComponent::TickThread, this));
}
void TargetManagementComponent::Deactivate()
@@ -17,15 +17,6 @@
namespace AzFramework
{
AZ_CVAR(
float,
ed_cameraSystemDefaultPlaneHeight,
34.0f,
nullptr,
AZ::ConsoleFunctorFlags::Null,
"The default height of the ground plane to do intersection tests against when orbiting");
AZ_CVAR(float, ed_cameraSystemMinOrbitDistance, 10.0f, nullptr, AZ::ConsoleFunctorFlags::Null, "");
AZ_CVAR(float, ed_cameraSystemMaxOrbitDistance, 50.0f, nullptr, AZ::ConsoleFunctorFlags::Null, "");
AZ_CVAR(
bool,
ed_cameraSystemUseCursor,
@@ -135,8 +126,8 @@ namespace AzFramework
camera.m_pitch = eulerAngles.GetX();
camera.m_yaw = eulerAngles.GetZ();
// note: m_lookDist is negative so we must invert it here
camera.m_lookAt = transform.GetTranslation() + (camera.Rotation().GetBasisY() * -camera.m_lookDist);
camera.m_pivot = transform.GetTranslation();
camera.m_offset = AZ::Vector3::CreateZero();
}
bool CameraSystem::HandleEvents(const InputEvent& event)
@@ -320,14 +311,8 @@ namespace AzFramework
nextCamera.m_pitch -= float(cursorDelta.m_y) * rotateSpeed * Invert(m_invertPitchFn());
nextCamera.m_yaw -= float(cursorDelta.m_x) * rotateSpeed * Invert(m_invertYawFn());
const auto clampRotation = [](const float angle)
{
return AZStd::fmod(angle + AZ::Constants::TwoPi, AZ::Constants::TwoPi);
};
nextCamera.m_yaw = clampRotation(nextCamera.m_yaw);
// clamp pitch to be +/-90 degrees
nextCamera.m_pitch = AZ::GetClamp(nextCamera.m_pitch, -AZ::Constants::HalfPi, AZ::Constants::HalfPi);
nextCamera.m_yaw = WrapYawRotation(nextCamera.m_yaw);
nextCamera.m_pitch = ClampPitchRotation(nextCamera.m_pitch);
return nextCamera;
}
@@ -337,9 +322,10 @@ namespace AzFramework
m_rotateChannelId = rotateChannelId;
}
PanCameraInput::PanCameraInput(const InputChannelId& panChannelId, PanAxesFn panAxesFn)
PanCameraInput::PanCameraInput(const InputChannelId& panChannelId, PanAxesFn panAxesFn, TranslationDeltaFn translationDeltaFn)
: m_panAxesFn(AZStd::move(panAxesFn))
, m_panChannelId(panChannelId)
, m_translationDeltaFn(translationDeltaFn)
{
m_panSpeedFn = []() constexpr
{
@@ -375,11 +361,11 @@ namespace AzFramework
const auto panAxes = m_panAxesFn(nextCamera);
const float panSpeed = m_panSpeedFn();
const auto deltaPanX = float(cursorDelta.m_x) * panAxes.m_horizontalAxis * panSpeed;
const auto deltaPanY = float(cursorDelta.m_y) * panAxes.m_verticalAxis * panSpeed;
const auto deltaPanX = aznumeric_cast<float>(cursorDelta.m_x) * panAxes.m_horizontalAxis * panSpeed;
const auto deltaPanY = aznumeric_cast<float>(cursorDelta.m_y) * panAxes.m_verticalAxis * panSpeed;
nextCamera.m_lookAt += deltaPanX * Invert(m_invertPanXFn());
nextCamera.m_lookAt += deltaPanY * -Invert(m_invertPanYFn());
m_translationDeltaFn(nextCamera, deltaPanX * Invert(m_invertPanXFn()));
m_translationDeltaFn(nextCamera, deltaPanY * -Invert(m_invertPanYFn()));
return nextCamera;
}
@@ -426,8 +412,11 @@ namespace AzFramework
}
TranslateCameraInput::TranslateCameraInput(
TranslationAxesFn translationAxesFn, const TranslateCameraInputChannelIds& translateCameraInputChannelIds)
const TranslateCameraInputChannelIds& translateCameraInputChannelIds,
TranslationAxesFn translationAxesFn,
TranslationDeltaFn translateDeltaFn)
: m_translationAxesFn(AZStd::move(translationAxesFn))
, m_translateDeltaFn(AZStd::move(translateDeltaFn))
, m_translateCameraInputChannelIds(translateCameraInputChannelIds)
{
m_translateSpeedFn = []() constexpr
@@ -497,32 +486,32 @@ namespace AzFramework
if ((m_translation & TranslationType::Forward) == TranslationType::Forward)
{
nextCamera.m_lookAt += axisY * speed * deltaTime;
m_translateDeltaFn(nextCamera, axisY * speed * deltaTime);
}
if ((m_translation & TranslationType::Backward) == TranslationType::Backward)
{
nextCamera.m_lookAt -= axisY * speed * deltaTime;
m_translateDeltaFn(nextCamera, -axisY * speed * deltaTime);
}
if ((m_translation & TranslationType::Left) == TranslationType::Left)
{
nextCamera.m_lookAt -= axisX * speed * deltaTime;
m_translateDeltaFn(nextCamera, -axisX * speed * deltaTime);
}
if ((m_translation & TranslationType::Right) == TranslationType::Right)
{
nextCamera.m_lookAt += axisX * speed * deltaTime;
m_translateDeltaFn(nextCamera, axisX * speed * deltaTime);
}
if ((m_translation & TranslationType::Up) == TranslationType::Up)
{
nextCamera.m_lookAt += axisZ * speed * deltaTime;
m_translateDeltaFn(nextCamera, axisZ * speed * deltaTime);
}
if ((m_translation & TranslationType::Down) == TranslationType::Down)
{
nextCamera.m_lookAt -= axisZ * speed * deltaTime;
m_translateDeltaFn(nextCamera, -axisZ * speed * deltaTime);
}
if (Ending())
@@ -544,16 +533,20 @@ namespace AzFramework
m_translateCameraInputChannelIds = translateCameraInputChannelIds;
}
OrbitCameraInput::OrbitCameraInput(const InputChannelId& orbitChannelId)
: m_orbitChannelId(orbitChannelId)
PivotCameraInput::PivotCameraInput(const InputChannelId& pivotChannelId)
: m_pivotChannelId(pivotChannelId)
{
m_pivotFn = []([[maybe_unused]] const AZ::Vector3& position, [[maybe_unused]] const AZ::Vector3& direction)
{
return AZ::Vector3::CreateZero();
};
}
bool OrbitCameraInput::HandleEvents(const InputEvent& event, const ScreenVector& cursorDelta, const float scrollDelta)
bool PivotCameraInput::HandleEvents(const InputEvent& event, const ScreenVector& cursorDelta, const float scrollDelta)
{
if (const auto* input = AZStd::get_if<DiscreteInputEvent>(&event))
{
if (input->m_channelId == m_orbitChannelId)
if (input->m_channelId == m_pivotChannelId)
{
if (input->m_state == InputChannel::State::Began)
{
@@ -568,85 +561,46 @@ namespace AzFramework
if (Active())
{
return m_orbitCameras.HandleEvents(event, cursorDelta, scrollDelta);
return m_pivotCameras.HandleEvents(event, cursorDelta, scrollDelta);
}
return !Idle();
}
Camera OrbitCameraInput::StepCamera(
Camera PivotCameraInput::StepCamera(
const Camera& targetCamera, const ScreenVector& cursorDelta, const float scrollDelta, const float deltaTime)
{
Camera nextCamera = targetCamera;
if (Beginning())
{
const auto hasLookAt = [&nextCamera, &targetCamera, &lookAtFn = m_lookAtFn]
{
if (lookAtFn)
{
// pass through the camera's position and look vector for use in the lookAt function
if (const auto lookAt = lookAtFn(targetCamera.Translation(), targetCamera.Rotation().GetBasisY()))
{
// default to internal look at behavior if the look at point matches the camera translation
if (targetCamera.m_lookAt.IsClose(*lookAt))
{
return false;
}
auto transform = AZ::Transform::CreateLookAt(targetCamera.m_lookAt, *lookAt);
nextCamera.m_lookDist = -lookAt->GetDistance(targetCamera.m_lookAt);
UpdateCameraFromTransform(nextCamera, transform);
return true;
}
}
return false;
}();
if (!hasLookAt)
{
float hit_distance = 0.0f;
AZ::Plane::CreateFromNormalAndPoint(AZ::Vector3::CreateAxisZ(), AZ::Vector3::CreateAxisZ(ed_cameraSystemDefaultPlaneHeight))
.CastRay(targetCamera.Translation(), targetCamera.Rotation().GetBasisY(), hit_distance);
if (hit_distance > 0.0f)
{
hit_distance = AZStd::min<float>(hit_distance, ed_cameraSystemMaxOrbitDistance);
nextCamera.m_lookDist = -hit_distance;
nextCamera.m_lookAt = targetCamera.Translation() + targetCamera.Rotation().GetBasisY() * hit_distance;
}
else
{
nextCamera.m_lookDist = -ed_cameraSystemMinOrbitDistance;
nextCamera.m_lookAt =
targetCamera.Translation() + targetCamera.Rotation().GetBasisY() * ed_cameraSystemMinOrbitDistance;
}
}
nextCamera.m_pivot = m_pivotFn(targetCamera.Translation(), targetCamera.Rotation().GetBasisY());
nextCamera.m_offset = nextCamera.View().TransformPoint(targetCamera.Translation());
}
if (Active())
{
nextCamera = m_orbitCameras.StepCamera(nextCamera, cursorDelta, scrollDelta, deltaTime);
MovePivotDetached(nextCamera, m_pivotFn(targetCamera.Translation(), targetCamera.Rotation().GetBasisY()));
nextCamera = m_pivotCameras.StepCamera(nextCamera, cursorDelta, scrollDelta, deltaTime);
}
if (Ending())
{
m_orbitCameras.Reset();
m_pivotCameras.Reset();
nextCamera.m_lookAt = nextCamera.Translation();
nextCamera.m_lookDist = 0.0f;
nextCamera.m_pivot = nextCamera.Translation();
nextCamera.m_offset = AZ::Vector3::CreateZero();
}
return nextCamera;
}
void OrbitCameraInput::SetOrbitInputChannelId(const InputChannelId& orbitChanneId)
void PivotCameraInput::SetPivotInputChannelId(const InputChannelId& pivotChanneId)
{
m_orbitChannelId = orbitChanneId;
m_pivotChannelId = pivotChanneId;
}
OrbitDollyScrollCameraInput::OrbitDollyScrollCameraInput()
PivotDollyScrollCameraInput::PivotDollyScrollCameraInput()
{
m_scrollSpeedFn = []() constexpr
{
@@ -654,7 +608,7 @@ namespace AzFramework
};
}
bool OrbitDollyScrollCameraInput::HandleEvents(
bool PivotDollyScrollCameraInput::HandleEvents(
const InputEvent& event, [[maybe_unused]] const ScreenVector& cursorDelta, [[maybe_unused]] const float scrollDelta)
{
if (const auto* scroll = AZStd::get_if<ScrollEvent>(&event))
@@ -665,46 +619,61 @@ namespace AzFramework
return !Idle();
}
Camera OrbitDollyScrollCameraInput::StepCamera(
static Camera PivotDolly(const Camera& targetCamera, const float delta)
{
Camera nextCamera = targetCamera;
const auto pivotDirection = targetCamera.m_offset.GetNormalized();
nextCamera.m_offset -= pivotDirection * delta;
const auto pivotDot = targetCamera.m_offset.Dot(nextCamera.m_offset);
const auto distance = nextCamera.m_offset.GetLength() * AZ::GetSign(pivotDot);
const auto minDistance = 0.01f;
if (distance < minDistance || pivotDot < 0.0f)
{
nextCamera.m_offset = pivotDirection * minDistance;
}
return nextCamera;
}
Camera PivotDollyScrollCameraInput::StepCamera(
const Camera& targetCamera,
[[maybe_unused]] const ScreenVector& cursorDelta,
const float scrollDelta,
[[maybe_unused]] const float deltaTime)
{
Camera nextCamera = targetCamera;
nextCamera.m_lookDist = AZ::GetMin(nextCamera.m_lookDist + scrollDelta * m_scrollSpeedFn(), 0.0f);
const auto nextCamera = PivotDolly(targetCamera, aznumeric_cast<float>(scrollDelta) * m_scrollSpeedFn());
EndActivation();
return nextCamera;
}
OrbitDollyCursorMoveCameraInput::OrbitDollyCursorMoveCameraInput(const InputChannelId& dollyChannelId)
PivotDollyMotionCameraInput::PivotDollyMotionCameraInput(const InputChannelId& dollyChannelId)
: m_dollyChannelId(dollyChannelId)
{
m_cursorSpeedFn = []() constexpr
m_motionSpeedFn = []() constexpr
{
return 0.01f;
};
}
bool OrbitDollyCursorMoveCameraInput::HandleEvents(
bool PivotDollyMotionCameraInput::HandleEvents(
const InputEvent& event, [[maybe_unused]] const ScreenVector& cursorDelta, [[maybe_unused]] const float scrollDelta)
{
HandleActivationEvents(event, m_dollyChannelId, cursorDelta, m_clickDetector, *this);
return CameraInputUpdatingAfterMotion(*this);
}
Camera OrbitDollyCursorMoveCameraInput::StepCamera(
Camera PivotDollyMotionCameraInput::StepCamera(
const Camera& targetCamera,
const ScreenVector& cursorDelta,
[[maybe_unused]] const float scrollDelta,
[[maybe_unused]] const float deltaTime)
{
Camera nextCamera = targetCamera;
nextCamera.m_lookDist = AZ::GetMin(nextCamera.m_lookDist + float(cursorDelta.m_y) * m_cursorSpeedFn(), 0.0f);
return nextCamera;
return PivotDolly(targetCamera, aznumeric_cast<float>(cursorDelta.m_y) * m_motionSpeedFn());
}
void OrbitDollyCursorMoveCameraInput::SetDollyInputChannelId(const InputChannelId& dollyChannelId)
void PivotDollyMotionCameraInput::SetDollyInputChannelId(const InputChannelId& dollyChannelId)
{
m_dollyChannelId = dollyChannelId;
}
@@ -739,7 +708,7 @@ namespace AzFramework
const auto translation_basis = LookTranslation(nextCamera);
const auto axisY = translation_basis.GetBasisY();
nextCamera.m_lookAt += axisY * scrollDelta * m_scrollSpeedFn();
nextCamera.m_pivot += axisY * scrollDelta * m_scrollSpeedFn();
EndActivation();
@@ -790,13 +759,13 @@ namespace AzFramework
{
const float moveRate = AZStd::exp2(cameraProps.m_translateSmoothnessFn());
const float moveTime = AZStd::exp2(-moveRate * deltaTime);
camera.m_lookDist = AZ::Lerp(targetCamera.m_lookDist, currentCamera.m_lookDist, moveTime);
camera.m_lookAt = targetCamera.m_lookAt.Lerp(currentCamera.m_lookAt, moveTime);
camera.m_pivot = targetCamera.m_pivot.Lerp(currentCamera.m_pivot, moveTime);
camera.m_offset = targetCamera.m_offset.Lerp(currentCamera.m_offset, moveTime);
}
else
{
camera.m_lookDist = targetCamera.m_lookDist;
camera.m_lookAt = targetCamera.m_lookAt;
camera.m_pivot = targetCamera.m_pivot;
camera.m_offset = targetCamera.m_offset;
}
return camera;
@@ -29,17 +29,15 @@ namespace AzFramework
//! @note Order of rotation is Z, Y, X.
AZ::Vector3 EulerAngles(const AZ::Matrix3x3& orientation);
//! A simple camera representation using spherical coordinates as input (pitch, yaw and look distance).
//! A simple camera representation using spherical coordinates as input (pitch, yaw, pivot and offset).
//! The cameras transform and view can be obtained through accessor functions that use the internal
//! spherical coordinates to calculate the position and orientation.
struct Camera
{
AZ::Vector3 m_lookAt = AZ::Vector3::CreateZero(); //!< Position of camera when m_lookDist is zero,
//!< or position of m_lookAt when m_lookDist is greater
//!< than zero.
float m_yaw{ 0.0 }; //!< Yaw rotation of camera (stored in radians) usually clamped to 0-360 degrees (0-2Pi radians).
float m_pitch{ 0.0 }; //!< Pitch rotation of the camera (stored in radians) usually clamped to +/-90 degrees (-Pi/2 - Pi/2 radians).
float m_lookDist{ 0.0 }; //!< Zero gives first person free look, otherwise orbit about m_lookAt
AZ::Vector3 m_pivot = AZ::Vector3::CreateZero(); //!< Pivot point to rotate about (modified in world space).
AZ::Vector3 m_offset = AZ::Vector3::CreateZero(); //!< Offset relative to pivot (modified in camera space).
float m_yaw = 0.0f; //!< Yaw rotation of camera (stored in radians) usually clamped to 0-360 degrees (0-2Pi radians).
float m_pitch = 0.0f; //!< Pitch rotation of the camera (stored in radians) usually clamped to +/-90 degrees (-Pi/2 - Pi/2 radians).
//! View camera transform (V in model-view-projection matrix (MVP)).
AZ::Transform View() const;
@@ -51,6 +49,15 @@ namespace AzFramework
AZ::Vector3 Translation() const;
};
//! Helper to allow the pivot to be positioned without altering the camera's position.
inline void MovePivotDetached(Camera& camera, const AZ::Vector3& pivot)
{
const auto& view = camera.View();
const auto delta = view.TransformPoint(pivot) - view.TransformPoint(camera.m_pivot);
camera.m_offset -= delta;
camera.m_pivot = pivot;
}
inline AZ::Transform Camera::View() const
{
return Transform().GetInverse();
@@ -58,8 +65,8 @@ namespace AzFramework
inline AZ::Transform Camera::Transform() const
{
return AZ::Transform::CreateTranslation(m_lookAt) * AZ::Transform::CreateRotationZ(m_yaw) *
AZ::Transform::CreateRotationX(m_pitch) * AZ::Transform::CreateTranslation(AZ::Vector3::CreateAxisY(m_lookDist));
return AZ::Transform::CreateTranslation(m_pivot) * AZ::Transform::CreateRotationZ(m_yaw) * AZ::Transform::CreateRotationX(m_pitch) *
AZ::Transform::CreateTranslation(m_offset);
}
inline AZ::Matrix3x3 Camera::Rotation() const
@@ -279,21 +286,37 @@ namespace AzFramework
public:
bool HandleEvents(const InputEvent& event);
Camera StepCamera(const Camera& targetCamera, float deltaTime);
bool HandlingEvents() const
{
return m_handlingEvents;
}
bool HandlingEvents() const;
Cameras m_cameras; //!< Represents a collection of camera inputs that together provide a camera controller.
private:
ScreenVector m_motionDelta; //!< The delta used for look/orbit/pan (rotation + translation) - two dimensional.
ScreenVector m_motionDelta; //!< The delta used for look/pivot/pan (rotation + translation) - two dimensional.
CursorState m_cursorState; //!< The current and previous position of the cursor (used to calculate movement delta).
float m_scrollDelta = 0.0f; //!< The delta used for dolly/movement (translation) - one dimensional.
bool m_handlingEvents = false; //!< Is the camera system currently handling events (events are consumed and not propagated).
};
//! A camera input to handle motion deltas that can rotate or orbit the camera.
inline bool CameraSystem::HandlingEvents() const
{
return m_handlingEvents;
}
//! Clamps pitch to be +/-90 degrees (-Pi/2, Pi/2).
//! @param pitch Pitch angle in radians.
inline float ClampPitchRotation(const float pitch)
{
return AZ::GetClamp(pitch, -AZ::Constants::HalfPi, AZ::Constants::HalfPi);
}
//! Ensures yaw wraps between 0 and 360 degrees (0, 2Pi).
//! @param yaw Yaw angle in radians.
inline float WrapYawRotation(const float yaw)
{
return AZStd::fmod(yaw + AZ::Constants::TwoPi, AZ::Constants::TwoPi);
}
//! A camera input to handle motion deltas that can rotate or pivot the camera.
class RotateCameraInput : public CameraInput
{
public:
@@ -332,8 +355,8 @@ namespace AzFramework
return { orientation.GetBasisX(), orientation.GetBasisZ() };
}
//! PanAxes to use while in 'orbit' camera behavior.
inline PanAxes OrbitPan(const Camera& camera)
//! PanAxes to use while in 'pivot' camera behavior.
inline PanAxes PivotPan(const Camera& camera)
{
const AZ::Matrix3x3 orientation = camera.Rotation();
@@ -347,11 +370,23 @@ namespace AzFramework
return { basisX, basisY };
}
using TranslationDeltaFn = AZStd::function<void(Camera& camera, const AZ::Vector3& delta)>;
inline void TranslatePivot(Camera& camera, const AZ::Vector3& delta)
{
camera.m_pivot += delta;
}
inline void TranslateOffset(Camera& camera, const AZ::Vector3& delta)
{
camera.m_offset += camera.View().TransformVector(delta);
}
//! A camera input to handle motion deltas that can pan the camera (translate in two axes).
class PanCameraInput : public CameraInput
{
public:
PanCameraInput(const InputChannelId& panChannelId, PanAxesFn panAxesFn);
PanCameraInput(const InputChannelId& panChannelId, PanAxesFn panAxesFn, TranslationDeltaFn translationDeltaFn);
// CameraInput overrides ...
bool HandleEvents(const InputEvent& event, const ScreenVector& cursorDelta, float scrollDelta) override;
@@ -365,6 +400,7 @@ namespace AzFramework
private:
PanAxesFn m_panAxesFn; //!< Builder for the particular pan axes (provided in the constructor).
TranslationDeltaFn m_translationDeltaFn; //!< How to apply the translation delta to the camera offset or pivot.
InputChannelId m_panChannelId; //!< Input channel to begin the pan camera input.
ClickDetector m_clickDetector; //!< Used to determine when a sufficient motion delta has occurred after an initial discrete input
//!< event has started (press and move event).
@@ -385,8 +421,8 @@ namespace AzFramework
return AZ::Matrix3x3::CreateFromColumns(basisX, basisY, basisZ);
}
//! TranslationAxes to use while in 'orbit' camera behavior.
inline AZ::Matrix3x3 OrbitTranslation(const Camera& camera)
//! TranslationAxes to use while in 'pivot' camera behavior.
inline AZ::Matrix3x3 PivotTranslation(const Camera& camera)
{
const AZ::Matrix3x3 orientation = camera.Rotation();
@@ -417,8 +453,10 @@ namespace AzFramework
class TranslateCameraInput : public CameraInput
{
public:
explicit TranslateCameraInput(
TranslationAxesFn translationAxesFn, const TranslateCameraInputChannelIds& translateCameraInputChannelIds);
TranslateCameraInput(
const TranslateCameraInputChannelIds& translateCameraInputChannelIds,
TranslationAxesFn translationAxesFn,
TranslationDeltaFn translateDeltaFn);
// CameraInput overrides ...
bool HandleEvents(const InputEvent& event, const ScreenVector& cursorDelta, float scrollDelta) override;
@@ -492,15 +530,16 @@ namespace AzFramework
TranslationType m_translation = TranslationType::Nil; //!< Types of translation the camera input is under.
TranslationAxesFn m_translationAxesFn; //!< Builder for translation axes.
TranslationDeltaFn m_translateDeltaFn; //!< How to apply the translation delta to the camera offset or pivot.
TranslateCameraInputChannelIds m_translateCameraInputChannelIds; //!< Input channel ids that map to internal translation types.
bool m_boost = false; //!< Is the translation speed currently being multiplied/scaled upwards.
};
//! A camera input to handle discrete scroll events that can modify the camera look distance.
class OrbitDollyScrollCameraInput : public CameraInput
//! A camera input to handle discrete scroll events that can modify the camera pivot distance.
class PivotDollyScrollCameraInput : public CameraInput
{
public:
OrbitDollyScrollCameraInput();
PivotDollyScrollCameraInput();
// CameraInput overrides ...
bool HandleEvents(const InputEvent& event, const ScreenVector& cursorDelta, float scrollDelta) override;
@@ -509,11 +548,11 @@ namespace AzFramework
AZStd::function<float()> m_scrollSpeedFn;
};
//! A camera input to handle motion deltas that can modify the camera look distance.
class OrbitDollyCursorMoveCameraInput : public CameraInput
//! A camera input to handle motion deltas that can modify the camera pivot distance.
class PivotDollyMotionCameraInput : public CameraInput
{
public:
explicit OrbitDollyCursorMoveCameraInput(const InputChannelId& dollyChannelId);
explicit PivotDollyMotionCameraInput(const InputChannelId& dollyChannelId);
// CameraInput overrides ...
bool HandleEvents(const InputEvent& event, const ScreenVector& cursorDelta, float scrollDelta) override;
@@ -521,7 +560,7 @@ namespace AzFramework
void SetDollyInputChannelId(const InputChannelId& dollyChannelId);
AZStd::function<float()> m_cursorSpeedFn;
AZStd::function<float()> m_motionSpeedFn;
private:
InputChannelId m_dollyChannelId; //!< Input channel to begin the dolly cursor camera input.
@@ -544,36 +583,36 @@ namespace AzFramework
//! A camera input that doubles as its own set of camera inputs.
//! It is 'exclusive', so does not overlap with other sibling camera inputs - it runs its own set of camera inputs as 'children'.
class OrbitCameraInput : public CameraInput
class PivotCameraInput : public CameraInput
{
public:
using LookAtFn = AZStd::function<AZStd::optional<AZ::Vector3>(const AZ::Vector3& position, const AZ::Vector3& direction)>;
using PivotFn = AZStd::function<AZ::Vector3(const AZ::Vector3& position, const AZ::Vector3& direction)>;
explicit OrbitCameraInput(const InputChannelId& orbitChannelId);
explicit PivotCameraInput(const InputChannelId& pivotChannelId);
// CameraInput overrides ...
bool HandleEvents(const InputEvent& event, const ScreenVector& cursorDelta, float scrollDelta) override;
Camera StepCamera(const Camera& targetCamera, const ScreenVector& cursorDelta, float scrollDelta, float deltaTime) override;
bool Exclusive() const override;
void SetOrbitInputChannelId(const InputChannelId& orbitChanneId);
void SetPivotInputChannelId(const InputChannelId& pivotChanneId);
Cameras m_orbitCameras; //!< The camera inputs to run when this camera input is active (only these will run as it is exclusive).
Cameras m_pivotCameras; //!< The camera inputs to run when this camera input is active (only these will run as it is exclusive).
//! Override the default behavior for how a look-at point is calculated.
void SetLookAtFn(const LookAtFn& lookAtFn);
//! Override the default behavior for how a pivot point is calculated.
void SetPivotFn(PivotFn pivotFn);
private:
InputChannelId m_orbitChannelId; //!< Input channel to begin the orbit camera input.
LookAtFn m_lookAtFn; //!< The look-at behavior to use for this orbit camera (how is the look-at point calculated/retrieved).
InputChannelId m_pivotChannelId; //!< Input channel to begin the pivot camera input.
PivotFn m_pivotFn; //!< The pivot position to use for this pivot camera (how is the pivot point calculated/retrieved).
};
inline void OrbitCameraInput::SetLookAtFn(const LookAtFn& lookAtFn)
inline void PivotCameraInput::SetPivotFn(PivotFn pivotFn)
{
m_lookAtFn = lookAtFn;
m_pivotFn = AZStd::move(pivotFn);
}
inline bool OrbitCameraInput::Exclusive() const
inline bool PivotCameraInput::Exclusive() const
{
return true;
}
@@ -9,6 +9,7 @@
#pragma once
#include <AzCore/Memory/SystemAllocator.h>
#include <AzCore/std/smart_ptr/unique_ptr.h>
#include <AzFramework/Windowing/WindowBus.h>
@@ -76,6 +76,7 @@ if(PAL_TRAIT_BUILD_TESTS_SUPPORTED)
NAMESPACE AZ
FILES_CMAKE
Tests/frameworktests_files.cmake
${pal_dir}/platform_${PAL_PLATFORM_NAME_LOWERCASE}_files.cmake
INCLUDE_DIRECTORIES
PRIVATE
Tests
@@ -93,6 +94,8 @@ if(PAL_TRAIT_BUILD_TESTS_SUPPORTED)
NAME AZ::AzFramework.Tests
)
include(${pal_dir}/platform_specific_test_targets.cmake)
endif()
endif()
@@ -8,6 +8,7 @@
#include <AzFramework/XcbApplication.h>
#include <AzFramework/XcbEventHandler.h>
#include <AzFramework/XcbInterface.h>
namespace AzFramework
{
@@ -17,8 +18,8 @@ namespace AzFramework
{
public:
XcbConnectionManagerImpl()
: m_xcbConnection(xcb_connect(nullptr, nullptr))
{
m_xcbConnection = xcb_connect(nullptr, nullptr);
AZ_Error("Application", m_xcbConnection != nullptr, "Unable to connect to X11 Server.");
XcbConnectionManagerBus::Handler::BusConnect();
}
@@ -26,16 +27,15 @@ namespace AzFramework
~XcbConnectionManagerImpl() override
{
XcbConnectionManagerBus::Handler::BusDisconnect();
xcb_disconnect(m_xcbConnection);
}
xcb_connection_t* GetXcbConnection() const override
{
return m_xcbConnection;
return m_xcbConnection.get();
}
private:
xcb_connection_t* m_xcbConnection = nullptr;
XcbUniquePtr<xcb_connection_t, xcb_disconnect> m_xcbConnection = nullptr;
};
////////////////////////////////////////////////////////////////////////////////////////////////
@@ -65,10 +65,9 @@ namespace AzFramework
{
if (xcb_connection_t* xcbConnection = m_xcbConnectionManager->GetXcbConnection())
{
if (xcb_generic_event_t* event = xcb_poll_for_event(xcbConnection))
if (auto event = XcbStdFreePtr<xcb_generic_event_t>{xcb_poll_for_event(xcbConnection)})
{
XcbEventHandlerBus::Broadcast(&XcbEventHandlerBus::Events::HandleXcbEvent, event);
free(event);
XcbEventHandlerBus::Broadcast(&XcbEventHandlerBus::Events::HandleXcbEvent, event.get());
}
}
}
@@ -78,10 +77,9 @@ namespace AzFramework
{
if (xcb_connection_t* xcbConnection = m_xcbConnectionManager->GetXcbConnection())
{
while (xcb_generic_event_t* event = xcb_poll_for_event(xcbConnection))
while (auto event = XcbStdFreePtr<xcb_generic_event_t>{xcb_poll_for_event(xcbConnection)})
{
XcbEventHandlerBus::Broadcast(&XcbEventHandlerBus::Events::HandleXcbEvent, event);
free(event);
XcbEventHandlerBus::Broadcast(&XcbEventHandlerBus::Events::HandleXcbEvent, event.get());
}
}
}
@@ -26,7 +26,8 @@ namespace UnitTest
{
constexpr float deltaTime = 0.01666f; // 60fps
const bool consumed = m_cameraSystem->HandleEvents(event);
m_camera = m_cameraSystem->StepCamera(m_targetCamera, deltaTime);
m_targetCamera = m_cameraSystem->StepCamera(m_targetCamera, deltaTime);
m_camera = m_targetCamera; // no smoothing
return consumed;
}
@@ -45,20 +46,38 @@ namespace UnitTest
m_translateCameraInputChannelIds.m_boostChannelId = AzFramework::InputChannelId("keyboard_key_modifier_shift_l");
m_firstPersonRotateCamera = AZStd::make_shared<AzFramework::RotateCameraInput>(AzFramework::InputDeviceMouse::Button::Right);
m_firstPersonTranslateCamera =
AZStd::make_shared<AzFramework::TranslateCameraInput>(AzFramework::LookTranslation, m_translateCameraInputChannelIds);
// set rotate speed to be a value that will scale motion delta (pixels moved) by a thousandth.
m_firstPersonRotateCamera->m_rotateSpeedFn = []()
{
return 0.001f;
};
m_orbitCamera = AZStd::make_shared<AzFramework::OrbitCameraInput>(m_orbitChannelId);
auto orbitRotateCamera = AZStd::make_shared<AzFramework::RotateCameraInput>(AzFramework::InputDeviceMouse::Button::Left);
auto orbitTranslateCamera =
AZStd::make_shared<AzFramework::TranslateCameraInput>(AzFramework::OrbitTranslation, m_translateCameraInputChannelIds);
m_firstPersonTranslateCamera = AZStd::make_shared<AzFramework::TranslateCameraInput>(
m_translateCameraInputChannelIds, AzFramework::LookTranslation, AzFramework::TranslatePivot);
m_orbitCamera->m_orbitCameras.AddCamera(orbitRotateCamera);
m_orbitCamera->m_orbitCameras.AddCamera(orbitTranslateCamera);
m_pivotCamera = AZStd::make_shared<AzFramework::PivotCameraInput>(m_pivotChannelId);
m_pivotCamera->SetPivotFn(
[this](const AZ::Vector3&, const AZ::Vector3&)
{
return m_pivot;
});
auto pivotRotateCamera = AZStd::make_shared<AzFramework::RotateCameraInput>(AzFramework::InputDeviceMouse::Button::Left);
// set rotate speed to be a value that will scale motion delta (pixels moved) by a thousandth.
pivotRotateCamera->m_rotateSpeedFn = []()
{
return 0.001f;
};
auto pivotTranslateCamera = AZStd::make_shared<AzFramework::TranslateCameraInput>(
m_translateCameraInputChannelIds, AzFramework::PivotTranslation, AzFramework::TranslateOffset);
m_pivotCamera->m_pivotCameras.AddCamera(pivotRotateCamera);
m_pivotCamera->m_pivotCameras.AddCamera(pivotTranslateCamera);
m_cameraSystem->m_cameras.AddCamera(m_firstPersonRotateCamera);
m_cameraSystem->m_cameras.AddCamera(m_firstPersonTranslateCamera);
m_cameraSystem->m_cameras.AddCamera(m_orbitCamera);
m_cameraSystem->m_cameras.AddCamera(m_pivotCamera);
// these tests rely on using motion delta, not cursor positions (default is true)
AzFramework::ed_cameraSystemUseCursor = false;
@@ -68,7 +87,7 @@ namespace UnitTest
{
AzFramework::ed_cameraSystemUseCursor = true;
m_orbitCamera.reset();
m_pivotCamera.reset();
m_firstPersonRotateCamera.reset();
m_firstPersonTranslateCamera.reset();
@@ -78,24 +97,29 @@ namespace UnitTest
AllocatorsTestFixture::TearDown();
}
AzFramework::InputChannelId m_orbitChannelId = AzFramework::InputChannelId("keyboard_key_modifier_alt_l");
AzFramework::InputChannelId m_pivotChannelId = AzFramework::InputChannelId("keyboard_key_modifier_alt_l");
AzFramework::TranslateCameraInputChannelIds m_translateCameraInputChannelIds;
AZStd::shared_ptr<AzFramework::RotateCameraInput> m_firstPersonRotateCamera;
AZStd::shared_ptr<AzFramework::TranslateCameraInput> m_firstPersonTranslateCamera;
AZStd::shared_ptr<AzFramework::OrbitCameraInput> m_orbitCamera;
AZStd::shared_ptr<AzFramework::PivotCameraInput> m_pivotCamera;
AZ::Vector3 m_pivot = AZ::Vector3::CreateZero();
//! This is approximately Pi/2 * 1000 - this can be used to rotate the camera 90 degrees (pitch or yaw based
//! on vertical or horizontal motion) as the rotate speed function is set to be 1/1000.
inline static const int PixelMotionDelta = 1570;
};
TEST_F(CameraInputFixture, BeginAndEndOrbitCameraInputConsumesCorrectEvents)
TEST_F(CameraInputFixture, BeginAndEndPivotCameraInputConsumesCorrectEvents)
{
// begin orbit camera
// begin pivot camera
const bool consumed1 = HandleEventAndUpdate(AzFramework::DiscreteInputEvent{ AzFramework::InputDeviceKeyboard::Key::ModifierAltL,
AzFramework::InputChannel::State::Began });
// begin listening for orbit rotate (click detector) - event is not consumed
// begin listening for pivot rotate (click detector) - event is not consumed
const bool consumed2 = HandleEventAndUpdate(
AzFramework::DiscreteInputEvent{ AzFramework::InputDeviceMouse::Button::Left, AzFramework::InputChannel::State::Began });
// begin orbit rotate (mouse has moved sufficient distance to initiate)
// begin pivot rotate (mouse has moved sufficient distance to initiate)
const bool consumed3 = HandleEventAndUpdate(AzFramework::HorizontalMotionEvent{ 5 });
// end orbit (mouse up) - event is not consumed
// end pivot (mouse up) - event is not consumed
const bool consumed4 = HandleEventAndUpdate(
AzFramework::DiscreteInputEvent{ AzFramework::InputDeviceMouse::Button::Left, AzFramework::InputChannel::State::Ended });
@@ -236,29 +260,110 @@ namespace UnitTest
EXPECT_TRUE(activationEnded);
}
TEST_F(CameraInputFixture, OrbitCameraInputHandlesLookAtPointAndSelfAtSamePositionWhenOrbiting)
TEST_F(CameraInputFixture, PivotCameraInputHandlesLookAtPointAndSelfAtSamePositionWhenPivoting)
{
// create pathological lookAtFn that just returns the same position as the camera
m_orbitCamera->SetLookAtFn(
m_pivotCamera->SetPivotFn(
[](const AZ::Vector3& position, [[maybe_unused]] const AZ::Vector3& direction)
{
return position;
});
const auto expectedCameraPosition = AZ::Vector3(10.0f, 10.0f, 10.0f);
AzFramework::UpdateCameraFromTransform(
m_targetCamera,
AZ::Transform::CreateFromQuaternionAndTranslation(
AZ::Quaternion::CreateFromEulerAnglesDegrees(AZ::Vector3(0.0f, 0.0f, 90.0f)), AZ::Vector3(10.0f, 10.0f, 10.0f)));
AZ::Quaternion::CreateFromEulerAnglesDegrees(AZ::Vector3(0.0f, 0.0f, 90.0f)), expectedCameraPosition));
m_camera = m_targetCamera;
HandleEventAndUpdate(AzFramework::DiscreteInputEvent{ m_pivotChannelId, AzFramework::InputChannel::State::Began });
HandleEventAndUpdate(AzFramework::DiscreteInputEvent{ m_orbitChannelId, AzFramework::InputChannel::State::Began });
// verify the camera yaw has not changed and pivot point matches the expected camera position
using ::testing::FloatNear;
EXPECT_THAT(m_camera.m_yaw, FloatNear(AZ::DegToRad(90.0f), 0.001f));
EXPECT_THAT(m_camera.m_pitch, FloatNear(0.0f, 0.001f));
EXPECT_THAT(m_camera.m_offset, IsClose(AZ::Vector3::CreateZero()));
EXPECT_THAT(m_camera.m_pivot, IsClose(expectedCameraPosition));
}
// verify the camera yaw has not changed and the look at point
// does not match that of the camera translation
using ::testing::Eq;
using ::testing::Not;
EXPECT_THAT(m_camera.m_yaw, Eq(AZ::DegToRad(90.0f)));
EXPECT_THAT(m_camera.m_lookAt, Not(IsClose(m_camera.Translation())));
TEST_F(CameraInputFixture, FirstPersonRotateCameraInputRotatesYawByNinetyDegreesWithRequiredPixelDelta)
{
const auto cameraStartingPosition = AZ::Vector3::CreateAxisY(-10.0f);
m_targetCamera.m_pivot = cameraStartingPosition;
HandleEventAndUpdate(
AzFramework::DiscreteInputEvent{ AzFramework::InputDeviceMouse::Button::Right, AzFramework::InputChannel::State::Began });
HandleEventAndUpdate(AzFramework::HorizontalMotionEvent{ PixelMotionDelta });
const float expectedYaw = AzFramework::WrapYawRotation(-AZ::Constants::HalfPi);
using ::testing::FloatNear;
EXPECT_THAT(m_camera.m_yaw, FloatNear(expectedYaw, 0.001f));
EXPECT_THAT(m_camera.m_pitch, FloatNear(0.0f, 0.001f));
EXPECT_THAT(m_camera.m_pivot, IsClose(cameraStartingPosition));
EXPECT_THAT(m_camera.m_offset, IsClose(AZ::Vector3::CreateZero()));
}
TEST_F(CameraInputFixture, FirstPersonRotateCameraInputRotatesPitchByNinetyDegreesWithRequiredPixelDelta)
{
const auto cameraStartingPosition = AZ::Vector3::CreateAxisY(-10.0f);
m_targetCamera.m_pivot = cameraStartingPosition;
HandleEventAndUpdate(
AzFramework::DiscreteInputEvent{ AzFramework::InputDeviceMouse::Button::Right, AzFramework::InputChannel::State::Began });
HandleEventAndUpdate(AzFramework::VerticalMotionEvent{ PixelMotionDelta });
const float expectedPitch = AzFramework::ClampPitchRotation(-AZ::Constants::HalfPi);
using ::testing::FloatNear;
EXPECT_THAT(m_camera.m_yaw, FloatNear(0.0f, 0.001f));
EXPECT_THAT(m_camera.m_pitch, FloatNear(expectedPitch, 0.001f));
EXPECT_THAT(m_camera.m_pivot, IsClose(cameraStartingPosition));
EXPECT_THAT(m_camera.m_offset, IsClose(AZ::Vector3::CreateZero()));
}
TEST_F(CameraInputFixture, PivotRotateCameraInputRotatesPitchOffsetByNinetyDegreesWithRequiredPixelDelta)
{
const auto cameraStartingPosition = AZ::Vector3::CreateAxisY(-20.0f);
m_targetCamera.m_pivot = cameraStartingPosition;
m_pivot = AZ::Vector3::CreateAxisY(-10.0f);
HandleEventAndUpdate(AzFramework::DiscreteInputEvent{ m_pivotChannelId, AzFramework::InputChannel::State::Began });
HandleEventAndUpdate(
AzFramework::DiscreteInputEvent{ AzFramework::InputDeviceMouse::Button::Left, AzFramework::InputChannel::State::Began });
HandleEventAndUpdate(AzFramework::VerticalMotionEvent{ PixelMotionDelta });
const auto expectedCameraEndingPosition = AZ::Vector3(0.0f, -10.0f, 10.0f);
const float expectedPitch = AzFramework::ClampPitchRotation(-AZ::Constants::HalfPi);
using ::testing::FloatNear;
EXPECT_THAT(m_camera.m_yaw, FloatNear(0.0f, 0.001f));
EXPECT_THAT(m_camera.m_pitch, FloatNear(expectedPitch, 0.001f));
EXPECT_THAT(m_camera.m_pivot, IsClose(m_pivot));
EXPECT_THAT(m_camera.m_offset, IsClose(AZ::Vector3::CreateAxisY(-10.0f)));
EXPECT_THAT(m_camera.Translation(), IsCloseTolerance(expectedCameraEndingPosition, 0.01f));
}
TEST_F(CameraInputFixture, PivotRotateCameraInputRotatesYawOffsetByNinetyDegreesWithRequiredPixelDelta)
{
const auto cameraStartingPosition = AZ::Vector3(15.0f, -20.0f, 0.0f);
m_targetCamera.m_pivot = cameraStartingPosition;
m_pivot = AZ::Vector3(10.0f, -10.0f, 0.0f);
HandleEventAndUpdate(AzFramework::DiscreteInputEvent{ m_pivotChannelId, AzFramework::InputChannel::State::Began });
HandleEventAndUpdate(
AzFramework::DiscreteInputEvent{ AzFramework::InputDeviceMouse::Button::Left, AzFramework::InputChannel::State::Began });
HandleEventAndUpdate(AzFramework::HorizontalMotionEvent{ -PixelMotionDelta });
const auto expectedCameraEndingPosition = AZ::Vector3(20.0f, -5.0f, 0.0f);
const float expectedYaw = AzFramework::WrapYawRotation(AZ::Constants::HalfPi);
using ::testing::FloatNear;
EXPECT_THAT(m_camera.m_yaw, FloatNear(expectedYaw, 0.001f));
EXPECT_THAT(m_camera.m_pitch, FloatNear(0.0f, 0.001f));
EXPECT_THAT(m_camera.m_pivot, IsClose(m_pivot));
EXPECT_THAT(m_camera.m_offset, IsClose(AZ::Vector3(5.0f, -10.0f, 0.0f)));
EXPECT_THAT(m_camera.Translation(), IsCloseTolerance(expectedCameraEndingPosition, 0.01f));
}
} // namespace UnitTest

Some files were not shown because too many files have changed in this diff Show More