Files
o3de/Gems/GradientSignal/Code/Source/Components/SurfaceMaskGradientComponent.cpp
T
Mike Balfour d9ba0af645 SurfacePoint data structure encapsulations (#7413)
* First pass at encapsulating SurfacePointList.
The biggest challenge in optimizing SurfacePointList(s) usage is the overall memory management associated with it. There are M surface points with N surface mask entries created for every input point, which leads to a lot of container reallocation and memory shuffling when processing multiple input points. By encapsulating the list, it should become easier to preallocate the entries, as well as keep "helper data" around for managing the bookkeeping to associate the input points with the output points.

Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com>

* Small fixes and TODO reminders.

Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com>

* Encapsulate surface point creation and separate EnumeratePoints out from modifications.

Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com>

* Start removing SurfacePoint from the exposed API.

Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com>

* Changed SurfacePointList to split out the surface point storage to allow for span<> usage over time.

Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com>

* Removed entity id

Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com>

* Removed SurfacePoint from SurfaceData, changed all remaining uses to AzFramework::SurfaceData::SurfacePoint.

Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com>

* Encapsulated SurfaceTagWeightMap and renamed to SurfaceTagWeights.

Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com>

* Fixed make file.

Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com>

* Better commenting and parameter naming.

Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com>

* Renamed methods to be more descriptive.

Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com>
2022-02-04 11:27:59 -06:00

238 lines
8.8 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include <GradientSignal/Components/SurfaceMaskGradientComponent.h>
#include <AzCore/RTTI/BehaviorContext.h>
#include <AzCore/Serialization/EditContext.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <LmbrCentral/Shape/ShapeComponentBus.h>
#include <AzCore/Debug/Profiler.h>
namespace GradientSignal
{
void SurfaceMaskGradientConfig::Reflect(AZ::ReflectContext* context)
{
AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
if (serialize)
{
serialize->Class<SurfaceMaskGradientConfig, AZ::ComponentConfig>()
->Version(0)
->Field("SurfaceTagList", &SurfaceMaskGradientConfig::m_surfaceTagList)
;
AZ::EditContext* edit = serialize->GetEditContext();
if (edit)
{
edit->Class<SurfaceMaskGradientConfig>(
"Surface Mask Gradient", "")
->ClassElement(AZ::Edit::ClassElements::EditorData, "")
->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
->Attribute(AZ::Edit::Attributes::AutoExpand, true)
->DataElement(0, &SurfaceMaskGradientConfig::m_surfaceTagList, "Surface Tag List", "Identifiers used to compare against underlying surfaces.")
;
}
}
if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
{
behaviorContext->Class<SurfaceMaskGradientConfig>()
->Attribute(AZ::Script::Attributes::Category, "Vegetation")
->Constructor()
->Method("GetNumTags", &SurfaceMaskGradientConfig::GetNumTags)
->Method("GetTag", &SurfaceMaskGradientConfig::GetTag)
->Method("RemoveTag", &SurfaceMaskGradientConfig::RemoveTag)
->Method("AddTag", &SurfaceMaskGradientConfig::AddTag)
;
}
}
size_t SurfaceMaskGradientConfig::GetNumTags() const
{
return m_surfaceTagList.size();
}
AZ::Crc32 SurfaceMaskGradientConfig::GetTag(int tagIndex) const
{
if (tagIndex < m_surfaceTagList.size())
{
return m_surfaceTagList[tagIndex];
}
return AZ::Crc32();
}
void SurfaceMaskGradientConfig::RemoveTag(int tagIndex)
{
if (tagIndex < m_surfaceTagList.size())
{
m_surfaceTagList.erase(m_surfaceTagList.begin() + tagIndex);
}
}
void SurfaceMaskGradientConfig::AddTag(AZStd::string tag)
{
m_surfaceTagList.push_back(SurfaceData::SurfaceTag(tag));
}
void SurfaceMaskGradientComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
{
services.push_back(AZ_CRC("GradientService", 0x21c18d23));
}
void SurfaceMaskGradientComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services)
{
services.push_back(AZ_CRC("GradientService", 0x21c18d23));
services.push_back(AZ_CRC("GradientTransformService", 0x8c8c5ecc));
}
void SurfaceMaskGradientComponent::Reflect(AZ::ReflectContext* context)
{
SurfaceMaskGradientConfig::Reflect(context);
AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
if (serialize)
{
serialize->Class<SurfaceMaskGradientComponent, AZ::Component>()
->Version(0)
->Field("Configuration", &SurfaceMaskGradientComponent::m_configuration)
;
}
if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
{
behaviorContext->Constant("SurfaceMaskGradientComponentTypeId", BehaviorConstant(SurfaceMaskGradientComponentTypeId));
behaviorContext->Class<SurfaceMaskGradientComponent>()->RequestBus("SurfaceMaskGradientRequestBus");
behaviorContext->EBus<SurfaceMaskGradientRequestBus>("SurfaceMaskGradientRequestBus")
->Attribute(AZ::Script::Attributes::Category, "Vegetation")
->Event("GetNumTags", &SurfaceMaskGradientRequestBus::Events::GetNumTags)
->Event("GetTag", &SurfaceMaskGradientRequestBus::Events::GetTag)
->Event("RemoveTag", &SurfaceMaskGradientRequestBus::Events::RemoveTag)
->Event("AddTag", &SurfaceMaskGradientRequestBus::Events::AddTag)
;
}
}
SurfaceMaskGradientComponent::SurfaceMaskGradientComponent(const SurfaceMaskGradientConfig& configuration)
: m_configuration(configuration)
{
}
void SurfaceMaskGradientComponent::Activate()
{
m_dependencyMonitor.Reset();
m_dependencyMonitor.ConnectOwner(GetEntityId());
GradientRequestBus::Handler::BusConnect(GetEntityId());
SurfaceMaskGradientRequestBus::Handler::BusConnect(GetEntityId());
}
void SurfaceMaskGradientComponent::Deactivate()
{
m_dependencyMonitor.Reset();
GradientRequestBus::Handler::BusDisconnect();
SurfaceMaskGradientRequestBus::Handler::BusDisconnect();
}
bool SurfaceMaskGradientComponent::ReadInConfig(const AZ::ComponentConfig* baseConfig)
{
if (auto config = azrtti_cast<const SurfaceMaskGradientConfig*>(baseConfig))
{
m_configuration = *config;
return true;
}
return false;
}
bool SurfaceMaskGradientComponent::WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const
{
if (auto config = azrtti_cast<SurfaceMaskGradientConfig*>(outBaseConfig))
{
*config = m_configuration;
return true;
}
return false;
}
float SurfaceMaskGradientComponent::GetValue(const GradientSampleParams& params) const
{
float result = 0.0f;
if (!m_configuration.m_surfaceTagList.empty())
{
SurfaceData::SurfacePointList points;
SurfaceData::SurfaceDataSystemRequestBus::Broadcast(&SurfaceData::SurfaceDataSystemRequestBus::Events::GetSurfacePoints,
params.m_position, m_configuration.m_surfaceTagList, points);
result = GetMaxSurfaceWeight(points);
}
return result;
}
void SurfaceMaskGradientComponent::GetValues(AZStd::span<const AZ::Vector3> positions, AZStd::span<float> outValues) const
{
if (positions.size() != outValues.size())
{
AZ_Assert(false, "input and output lists are different sizes (%zu vs %zu).", positions.size(), outValues.size());
return;
}
bool valuesFound = false;
if (!m_configuration.m_surfaceTagList.empty())
{
// Rather than calling GetSurfacePoints on the EBus repeatedly in a loop, we instead pass a lambda into the EBus that contains
// the loop within it so that we can avoid the repeated EBus-calling overhead.
SurfaceData::SurfaceDataSystemRequestBus::Broadcast(
[this, positions, &outValues, &valuesFound](SurfaceData::SurfaceDataSystemRequestBus::Events* surfaceDataRequests)
{
// It's possible that there's nothing connected to the EBus, so keep track of the fact that we have valid results.
valuesFound = true;
SurfaceData::SurfacePointList points;
for (size_t index = 0; index < positions.size(); index++)
{
points.Clear();
surfaceDataRequests->GetSurfacePoints(positions[index], m_configuration.m_surfaceTagList, points);
outValues[index] = GetMaxSurfaceWeight(points);
}
});
}
if (!valuesFound)
{
// No surface tags, so no output values.
AZStd::fill(outValues.begin(), outValues.end(), 0.0f);
}
}
size_t SurfaceMaskGradientComponent::GetNumTags() const
{
return m_configuration.GetNumTags();
}
AZ::Crc32 SurfaceMaskGradientComponent::GetTag(int tagIndex) const
{
return m_configuration.GetTag(tagIndex);
}
void SurfaceMaskGradientComponent::RemoveTag(int tagIndex)
{
m_configuration.RemoveTag(tagIndex);
LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
}
void SurfaceMaskGradientComponent::AddTag(AZStd::string tag)
{
m_configuration.AddTag(tag);
LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
}
}