Adding a temporary Orphan function to the InstanceDatabase (#4297)
* Adding a temporary Orphan function to the InstanceDatabase, which will remove an instance from the database so it will not be found using Find or FindOrCreate. The instance will still persist until its use-count drops to 0, at which point it will be deleted. This is to enable the model asset to remove existing buffer/modellod/model instances and replace them with new instances that have the up to date data. Added unit tests for testing. Signed-off-by: amzn-tommy <waltont@amazon.com> * Fix an incorrect ceil and update ParallelInstance test cases for readability Signed-off-by: amzn-tommy <waltont@amazon.com>
This commit is contained in:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user