Improved image gradient GetValue(s) performance by using cached image data
Signed-off-by: Chris Galvan <chgalvan@amazon.com>
This commit is contained in:
@@ -215,6 +215,16 @@ namespace GradientSignal
|
||||
m_dependencyMonitor.ConnectDependency(m_configuration.m_imageAsset.GetId());
|
||||
}
|
||||
|
||||
void ImageGradientComponent::GetSubImageData()
|
||||
{
|
||||
if (!m_configuration.m_imageAsset || !m_configuration.m_imageAsset.IsReady())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
m_imageData = m_configuration.m_imageAsset->GetSubImageData(0, 0);
|
||||
}
|
||||
|
||||
void ImageGradientComponent::Activate()
|
||||
{
|
||||
// This will immediately call OnGradientTransformChanged and initialize m_gradientTransform.
|
||||
@@ -226,8 +236,19 @@ namespace GradientSignal
|
||||
GradientRequestBus::Handler::BusConnect(GetEntityId());
|
||||
AZ::Data::AssetBus::Handler::BusConnect(m_configuration.m_imageAsset.GetId());
|
||||
|
||||
// If the image asset is already ready (e.g. constructed in a unit test),
|
||||
// then go ahead and retrieve the image data now
|
||||
AZStd::unique_lock<decltype(m_imageMutex)> imageLock(m_imageMutex);
|
||||
m_configuration.m_imageAsset.QueueLoad();
|
||||
if (m_configuration.m_imageAsset.IsReady())
|
||||
{
|
||||
GetSubImageData();
|
||||
}
|
||||
// Otherwise for normal use-case, we queue the asset to be loaded now
|
||||
else
|
||||
{
|
||||
m_imageData = AZStd::span<const uint8_t>();
|
||||
m_configuration.m_imageAsset.QueueLoad();
|
||||
}
|
||||
}
|
||||
|
||||
void ImageGradientComponent::Deactivate()
|
||||
@@ -267,18 +288,24 @@ namespace GradientSignal
|
||||
{
|
||||
AZStd::unique_lock<decltype(m_imageMutex)> imageLock(m_imageMutex);
|
||||
m_configuration.m_imageAsset = asset;
|
||||
|
||||
GetSubImageData();
|
||||
}
|
||||
|
||||
void ImageGradientComponent::OnAssetMoved(AZ::Data::Asset<AZ::Data::AssetData> asset, [[maybe_unused]] void* oldDataPointer)
|
||||
{
|
||||
AZStd::unique_lock<decltype(m_imageMutex)> imageLock(m_imageMutex);
|
||||
m_configuration.m_imageAsset = asset;
|
||||
|
||||
GetSubImageData();
|
||||
}
|
||||
|
||||
void ImageGradientComponent::OnAssetReloaded(AZ::Data::Asset<AZ::Data::AssetData> asset)
|
||||
{
|
||||
AZStd::unique_lock<decltype(m_imageMutex)> imageLock(m_imageMutex);
|
||||
m_configuration.m_imageAsset = asset;
|
||||
|
||||
GetSubImageData();
|
||||
}
|
||||
|
||||
void ImageGradientComponent::OnGradientTransformChanged(const GradientTransform& newTransform)
|
||||
@@ -292,6 +319,12 @@ namespace GradientSignal
|
||||
AZ::Vector3 uvw = sampleParams.m_position;
|
||||
bool wasPointRejected = false;
|
||||
|
||||
// Return immediately if our cached image data hasn't been retrieved yet
|
||||
if (m_imageData.empty())
|
||||
{
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
{
|
||||
AZStd::shared_lock<decltype(m_imageMutex)> imageLock(m_imageMutex);
|
||||
|
||||
@@ -300,7 +333,7 @@ namespace GradientSignal
|
||||
if (!wasPointRejected)
|
||||
{
|
||||
return GetValueFromImageAsset(
|
||||
m_configuration.m_imageAsset, uvw, m_configuration.m_tilingX, m_configuration.m_tilingY, 0.0f);
|
||||
m_imageData, m_configuration.m_imageAsset->GetImageDescriptor(), uvw, m_configuration.m_tilingX, m_configuration.m_tilingY, 0.0f);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -315,6 +348,12 @@ namespace GradientSignal
|
||||
return;
|
||||
}
|
||||
|
||||
// Return immediately if our cached image data hasn't been retrieved yet
|
||||
if (m_imageData.empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
AZ::Vector3 uvw;
|
||||
bool wasPointRejected = false;
|
||||
|
||||
@@ -327,7 +366,7 @@ namespace GradientSignal
|
||||
if (!wasPointRejected)
|
||||
{
|
||||
outValues[index] = GetValueFromImageAsset(
|
||||
m_configuration.m_imageAsset, uvw, m_configuration.m_tilingX, m_configuration.m_tilingY, 0.0f);
|
||||
m_imageData, m_configuration.m_imageAsset->GetImageDescriptor(), uvw, m_configuration.m_tilingX, m_configuration.m_tilingY, 0.0f);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -353,6 +392,10 @@ namespace GradientSignal
|
||||
|
||||
{
|
||||
AZStd::unique_lock<decltype(m_imageMutex)> imageLock(m_imageMutex);
|
||||
|
||||
// Clear our cached image data
|
||||
m_imageData = AZStd::span<const uint8_t>();
|
||||
|
||||
m_configuration.m_imageAsset = AZ::Data::AssetManager::Instance().FindOrCreateAsset(assetId, azrtti_typeid<AZ::RPI::StreamingImageAsset>(), m_configuration.m_imageAsset.GetAutoLoadBehavior());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user