More GetValues() overrides (#6896)

* Benchmarks and tests for Image and Constant GetValues

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

* Verify GetValues for Perlin and Random Gradients

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

* Standardized the assert format for GetValues().

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

* More GetValues unit tests and test cleanup

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

* Fixed typos

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

* GetValues() unit tests for surface gradients.

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

* Benchmarks for ShapeAreaFalloff Gradient

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

* Added benchmarks for all remaining gradients and cleaned up the helper methods.

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

* Renamed class for better report formatting.

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

* Added missing Mocks dependencies for the Editor tests.

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

* First batch of specific GetValues() overrides.
Each one is measurably faster than the generic version.  Also, in ShapeAreaFalloffGradient, I optimized and simplified the logic a bit, so GetValue() is marginally faster too.

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

* Change GetValues() to use span and add more overrides.

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

* Convert GetValues() to use AZStd::span and added more overrides.

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

* Add missing include.

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

* PR feedback - switch to fill

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

* Fixed the logic and added unit tests and comments.

Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com>
This commit is contained in:
Mike Balfour
2022-01-14 13:30:45 -06:00
committed by GitHub
parent 7680d1f9d0
commit c403fa3db6
24 changed files with 331 additions and 144 deletions
@@ -59,7 +59,7 @@ namespace AZStd
constexpr span(pointer s, size_type length);
constexpr span(pointer first, const_pointer last);
constexpr span(pointer first, pointer last);
// We explicitly delete this constructor because it's too easy to accidentally
// create a span to just the first element instead of an entire array.
@@ -24,7 +24,7 @@ namespace AZStd
}
template <class Element>
inline constexpr span<Element>::span(pointer first, const_pointer last)
inline constexpr span<Element>::span(pointer first, pointer last)
: m_begin(first)
, m_end(last)
{ }
-5
View File
@@ -19,7 +19,6 @@ ly_add_target(
BUILD_DEPENDENCIES
PUBLIC
AZ::AzCore
AZ::AtomCore
AZ::AzFramework
Gem::SurfaceData
Gem::ImageProcessingAtom.Headers
@@ -41,7 +40,6 @@ ly_add_target(
Gem::LmbrCentral
PUBLIC
AZ::AzCore
AZ::AtomCore
Gem::GradientSignal.Static
Gem::ImageProcessingAtom.Headers # Atom/ImageProcessing/PixelFormats.h is part of a header in Includes
RUNTIME_DEPENDENCIES
@@ -73,7 +71,6 @@ if(PAL_TRAIT_BUILD_HOST_TOOLS)
PUBLIC
3rdParty::Qt::Widgets
AZ::AzCore
AZ::AtomCore
AZ::AzFramework
AZ::AzToolsFramework
AZ::AssetBuilderSDK
@@ -97,8 +94,6 @@ if(PAL_TRAIT_BUILD_HOST_TOOLS)
PRIVATE
Gem::GradientSignal.Editor.Static
Gem::LmbrCentral.Editor
PUBLIC
AZ::AtomCore
RUNTIME_DEPENDENCIES
Gem::LmbrCentral.Editor
)
@@ -62,7 +62,7 @@ namespace GradientSignal
//////////////////////////////////////////////////////////////////////////
// GradientRequestBus
float GetValue(const GradientSampleParams& sampleParams) const override;
void GetValues(AZStd::array_view<AZ::Vector3> positions, AZStd::array_view<float> outValues) const override;
void GetValues(AZStd::span<AZ::Vector3> positions, AZStd::span<float> outValues) const override;
protected:
//////////////////////////////////////////////////////////////////////////
@@ -77,6 +77,7 @@ namespace GradientSignal
//////////////////////////////////////////////////////////////////////////
// GradientRequestBus
float GetValue(const GradientSampleParams& sampleParams) const override;
void GetValues(AZStd::span<AZ::Vector3> positions, AZStd::span<float> outValues) const override;
bool IsEntityInHierarchy(const AZ::EntityId& entityId) const override;
//////////////////////////////////////////////////////////////////////////
@@ -102,6 +103,13 @@ namespace GradientSignal
GradientSampler& GetGradientSampler() override;
private:
static int ScaledPositionToPatternIndex(const AZ::Vector3& scaledPosition, int patternSize);
static float GetDitherValue4x4(const AZ::Vector3& scaledPosition);
static float GetDitherValue8x8(const AZ::Vector3& scaledPosition);
float GetCalculatedPointsPerUnit() const;
float GetDitherValue(const AZ::Vector3& scaledPosition, float value) const;
DitherGradientConfig m_configuration;
LmbrCentral::DependencyMonitor m_dependencyMonitor;
};
@@ -69,7 +69,7 @@ namespace GradientSignal
// GradientRequestBus overrides...
float GetValue(const GradientSampleParams& sampleParams) const override;
void GetValues(AZStd::array_view<AZ::Vector3> positions, AZStd::array_view<float> outValues) const override;
void GetValues(AZStd::span<AZ::Vector3> positions, AZStd::span<float> outValues) const override;
// AZ::Data::AssetBus overrides...
void OnAssetReady(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
@@ -64,6 +64,7 @@ namespace GradientSignal
//////////////////////////////////////////////////////////////////////////
// GradientRequestBus
float GetValue(const GradientSampleParams& sampleParams) const override;
void GetValues(AZStd::span<AZ::Vector3> positions, AZStd::span<float> outValues) const override;
bool IsEntityInHierarchy(const AZ::EntityId& entityId) const override;
protected:
@@ -69,6 +69,7 @@ namespace GradientSignal
//////////////////////////////////////////////////////////////////////////
// GradientRequestBus
float GetValue(const GradientSampleParams& sampleParams) const override;
void GetValues(AZStd::span<AZ::Vector3> positions, AZStd::span<float> outValues) const override;
bool IsEntityInHierarchy(const AZ::EntityId& entityId) const override;
protected:
@@ -70,7 +70,7 @@ namespace GradientSignal
// GradientRequestBus overrides...
float GetValue(const GradientSampleParams& sampleParams) const override;
void GetValues(AZStd::array_view<AZ::Vector3> positions, AZStd::array_view<float> outValues) const override;
void GetValues(AZStd::span<AZ::Vector3> positions, AZStd::span<float> outValues) const override;
private:
PerlinGradientConfig m_configuration;
@@ -61,7 +61,7 @@ namespace GradientSignal
// GradientRequestBus overrides...
float GetValue(const GradientSampleParams& sampleParams) const override;
void GetValues(AZStd::array_view<AZ::Vector3> positions, AZStd::array_view<float> outValues) const override;
void GetValues(AZStd::span<AZ::Vector3> positions, AZStd::span<float> outValues) const override;
private:
RandomGradientConfig m_configuration;
@@ -69,7 +69,7 @@ namespace GradientSignal
//////////////////////////////////////////////////////////////////////////
// GradientRequestBus
float GetValue(const GradientSampleParams& sampleParams) const override;
void GetValues(AZStd::array_view<AZ::Vector3> positions, AZStd::array_view<float> outValues) const override;
void GetValues(AZStd::span<AZ::Vector3> positions, AZStd::span<float> outValues) const override;
protected:
//////////////////////////////////////////////////////////////////////////
@@ -10,8 +10,7 @@
#include <AzCore/EBus/EBus.h>
#include <AzCore/Component/EntityId.h>
#include <AzCore/Math/Vector3.h>
#include <AtomCore/std/containers/array_view.h>
#include <AzCore/std/containers/span.h>
namespace GradientSignal
{
@@ -57,7 +56,7 @@ namespace GradientSignal
* \param positions The input list of positions to query.
* \param outValues The output list of values. This list is expected to be the same size as the positions list.
*/
virtual void GetValues(AZStd::array_view<AZ::Vector3> positions, AZStd::array_view<float> outValues) const
virtual void GetValues(AZStd::span<AZ::Vector3> positions, AZStd::span<float> outValues) const
{
// Reference implementation of GetValues for any gradients that don't have their own optimized implementations.
// This is 10%-60% faster than calling GetValue via EBus many times due to the per-call EBus overhead.
@@ -72,11 +71,7 @@ namespace GradientSignal
for (size_t index = 0; index < positions.size(); index++)
{
sampleParams.m_position = positions[index];
// The const_cast is necessary for now since array_view currently only supports const entries.
// If/when array_view is fixed to support non-const, or AZStd::span gets created, the const_cast can get removed.
auto& outValue = const_cast<float&>(outValues[index]);
outValue = GetValue(sampleParams);
outValues[index] = GetValue(sampleParams);
}
}
@@ -33,7 +33,7 @@ namespace GradientSignal
static void Reflect(AZ::ReflectContext* context);
inline float GetValue(const GradientSampleParams& sampleParams) const;
inline void GetValues(AZStd::array_view<AZ::Vector3> positions, AZStd::array_view<float> outValues) const;
inline void GetValues(AZStd::span<AZ::Vector3> positions, AZStd::span<float> outValues) const;
bool IsEntityInHierarchy(const AZ::EntityId& entityId) const;
@@ -147,18 +147,12 @@ namespace GradientSignal
return output * m_opacity;
}
inline void GradientSampler::GetValues(AZStd::array_view<AZ::Vector3> positions, AZStd::array_view<float> outValues) const
inline void GradientSampler::GetValues(AZStd::span<AZ::Vector3> positions, AZStd::span<float> outValues) const
{
auto ClearOutputValues = [](AZStd::array_view<float> outValues)
auto ClearOutputValues = [](AZStd::span<float> outValues)
{
// If we don't have a valid gradient (or it is fully transparent), clear out all the output values.
for (size_t index = 0; index < outValues.size(); index++)
{
// The const_cast is necessary for now since array_view currently only supports const entries.
// If/when array_view is fixed to support non-const, or AZStd::span gets created, the const_cast can get removed.
auto& outValue = const_cast<float&>(outValues[index]);
outValue = 0.0f;
}
memset(outValues.data(), 0, outValues.size() * sizeof(float));
};
if (m_opacity <= 0.0f || !m_gradientId.IsValid())
@@ -214,12 +208,8 @@ namespace GradientSignal
}
// Perform any post-fetch transformations on the gradient values (invert, levels, opacity).
for (size_t index = 0; index < outValues.size(); index++)
for (auto& outValue : outValues)
{
// The const_cast is necessary for now since array_view currently only supports const entries.
// If/when array_view is fixed to support non-const, or AZStd::span gets created, the const_cast can get removed.
auto& outValue = const_cast<float&>(outValues[index]);
if (m_invertInput)
{
outValue = 1.0f - outValue;
@@ -7,11 +7,12 @@
*/
#pragma once
#include <AzCore/Component/EntityId.h>
#include <AzCore/Math/Aabb.h>
#include <AzCore/Math/Vector3.h>
#include <AzCore/Component/EntityId.h>
#include <AzCore/Math/Matrix3x4.h>
#include <AzCore/Math/Transform.h>
#include <AzCore/std/containers/span.h>
#include <LmbrCentral/Shape/ShapeComponentBus.h>
#include <GradientSignal/GradientTransform.h>
@@ -53,28 +54,61 @@ namespace GradientSignal
inline float GetLevels(float input, float inputMid, float inputMin, float inputMax, float outputMin, float outputMax)
{
input = AZ::GetClamp(input, 0.0f, 1.0f);
inputMid = AZ::GetClamp(inputMid, 0.01f, 10.0f); // Clamp the midpoint to a non-zero value so that it's always safe to divide by it.
inputMid = AZ::GetClamp(inputMid, 0.01f, 10.0f); // Clamp the midpoint to a non-zero value so that it's always safe to divide by it.
inputMin = AZ::GetClamp(inputMin, 0.0f, 1.0f);
inputMax = AZ::GetClamp(inputMax, 0.0f, 1.0f);
outputMin = AZ::GetClamp(outputMin, 0.0f, 1.0f);
outputMax = AZ::GetClamp(outputMax, 0.0f, 1.0f);
float inputCorrected = 0.0f;
if (inputMin == inputMax)
{
inputCorrected = (input <= inputMin) ? 0.0f : 1.0f;
}
else
{
const float inputRemapped = AZ::GetMin(AZ::GetMax(input - inputMin, 0.0f) / (inputMax - inputMin), 1.0f);
// Note: Some paint programs map the midpoint using 1/mid where low values are dark and high values are light,
// others do the reverse and use mid directly, so low values are light and high values are dark. We've chosen to
// align with 1/mid since it appears to be the more prevalent of the two approaches.
inputCorrected = powf(inputRemapped, 1.0f / inputMid);
return (AZ::GetClamp(input, 0.0f, 1.0f) <= inputMin) ? outputMin : outputMax;
}
const float inputMidReciprocal = 1.0f / inputMid;
const float inputExtentsReciprocal = 1.0f / (inputMax - inputMin);
const float inputRemapped =
AZ::GetMin(AZ::GetMax(AZ::GetClamp(input, 0.0f, 1.0f) - inputMin, 0.0f) * inputExtentsReciprocal, 1.0f);
// Note: Some paint programs map the midpoint using 1/mid where low values are dark and high values are light,
// others do the reverse and use mid directly, so low values are light and high values are dark. We've chosen to
// align with 1/mid since it appears to be the more prevalent of the two approaches.
const float inputCorrected = powf(inputRemapped, inputMidReciprocal);
return AZ::Lerp(outputMin, outputMax, inputCorrected);
}
inline void GetLevels(AZStd::span<float> inOutValues, float inputMid, float inputMin, float inputMax, float outputMin, float outputMax)
{
inputMid = AZ::GetClamp(inputMid, 0.01f, 10.0f); // Clamp the midpoint to a non-zero value so that it's always safe to divide by it.
inputMin = AZ::GetClamp(inputMin, 0.0f, 1.0f);
inputMax = AZ::GetClamp(inputMax, 0.0f, 1.0f);
outputMin = AZ::GetClamp(outputMin, 0.0f, 1.0f);
outputMax = AZ::GetClamp(outputMax, 0.0f, 1.0f);
if (inputMin == inputMax)
{
for (auto& inOutValue : inOutValues)
{
inOutValue = (AZ::GetClamp(inOutValue, 0.0f, 1.0f) <= inputMin) ? outputMin : outputMax;
}
}
const float inputMidReciprocal = 1.0f / inputMid;
const float inputExtentsReciprocal = 1.0f / (inputMax - inputMin);
for (auto& inOutValue : inOutValues)
{
const float inputRemapped =
AZ::GetMin(AZ::GetMax(AZ::GetClamp(inOutValue, 0.0f, 1.0f) - inputMin, 0.0f) * inputExtentsReciprocal, 1.0f);
// Note: Some paint programs map the midpoint using 1/mid where low values are dark and high values are light,
// others do the reverse and use mid directly, so low values are light and high values are dark. We've chosen to
// align with 1/mid since it appears to be the more prevalent of the two approaches.
const float inputCorrected = powf(inputRemapped, inputMidReciprocal);
inOutValue = AZ::Lerp(outputMin, outputMax, inputCorrected);
}
}
} // namespace GradientSignal
@@ -135,7 +135,7 @@ namespace GradientSignal
}
void ConstantGradientComponent::GetValues(
[[maybe_unused]] AZStd::array_view<AZ::Vector3> positions, AZStd::array_view<float> outValues) const
[[maybe_unused]] AZStd::span<AZ::Vector3> positions, AZStd::span<float> outValues) const
{
if (positions.size() != outValues.size())
{
@@ -143,11 +143,7 @@ namespace GradientSignal
return;
}
for (auto& outValue : outValues)
{
float& writableOutValue = const_cast<float&>(outValue);
writableOutValue = m_configuration.m_value;
}
AZStd::fill(outValues.begin(), outValues.end(), m_configuration.m_value);
}
float ConstantGradientComponent::GetConstantValue() const
@@ -174,90 +174,122 @@ namespace GradientSignal
return false;
}
int PositionToMatrixIndex(float position, int patternSize)
int DitherGradientComponent::ScaledPositionToPatternIndex(const AZ::Vector3& scaledPosition, int patternSize)
{
int result = static_cast<int>(std::floor(fmod(position, static_cast<float>(patternSize))));
// The input position is expected to be scaled up so that each integer value is a unique point in our dither pattern, and
// the fractional value is just the amount within the point. The output is the specific index into an NxN pattern to use
// for the dither comparison value.
if (result < 0)
{
result += patternSize;
}
// Get the floor before casting to int because we want fractional negative values to go "down" to the next negative value.
AZ::Vector3 flooredScaledPosition = scaledPosition.GetFloor();
return result;
// For a pattern of 4, we want our indices to go 0, 1, 2, 3, 0, 1, 2, 3, etc. However, we want it continuous across
// negative and positive positions so we can't just use mod with abs(). Instead, we use a double-mod which gives us
// a result that's continuous across all coordinate space.
const int x = ((static_cast<int>(flooredScaledPosition.GetX()) % patternSize) + patternSize) % patternSize;
const int y = ((static_cast<int>(flooredScaledPosition.GetY()) % patternSize) + patternSize) % patternSize;
return (patternSize * y + x);
}
float GetDitherValue4x4(const AZ::Vector3& position)
float DitherGradientComponent::GetDitherValue4x4(const AZ::Vector3& scaledPosition)
{
const int patternSize = 4;
const int patternSizeSq = patternSize * patternSize;
const int indexMatrix[patternSizeSq] = {
0, 8, 2, 10,
12, 4, 14, 6,
3, 11, 1, 9,
15, 7, 13, 5 };
constexpr int patternSize = 4;
constexpr float indexMatrix[] = {
0.0f / 16.0f, 8.0f / 16.0f, 2.0f / 16.0f, 10.0f / 16.0f,
12.0f / 16.0f, 4.0f / 16.0f, 14.0f / 16.0f, 6.0f / 16.0f,
3.0f / 16.0f, 11.0f / 16.0f, 1.0f / 16.0f, 9.0f / 16.0f,
15.0f / 16.0f, 7.0f / 16.0f, 13.0f / 16.0f, 5.0f / 16.0f };
const int x = PositionToMatrixIndex(position.GetX(), patternSize);
const int y = PositionToMatrixIndex(position.GetY(), patternSize);
return indexMatrix[patternSize * y + x] / static_cast<float>(patternSizeSq);
return indexMatrix[ScaledPositionToPatternIndex(scaledPosition, patternSize)];
}
float GetDitherValue8x8(const AZ::Vector3& position)
float DitherGradientComponent::GetDitherValue8x8(const AZ::Vector3& scaledPosition)
{
const int patternSize = 8;
const int patternSizeSq = patternSize * patternSize;
const int indexMatrix[patternSizeSq] = {
0, 32, 8, 40, 2, 34, 10, 42,
48, 16, 56, 24, 50, 18, 58, 26,
12, 44, 4, 36, 14, 46, 6, 38,
60, 28, 52, 20, 62, 30, 54, 22,
3, 35, 11, 43, 1, 33, 9, 41,
51, 19, 59, 27, 49, 17, 57, 25,
15, 47, 7, 39, 13, 45, 5, 37,
63, 31, 55, 23, 61, 29, 53, 21 };
constexpr int patternSize = 8;
constexpr float indexMatrix[] = {
0.0f / 64.0f, 32.0f / 64.0f, 8.0f / 64.0f, 40.0f / 64.0f, 2.0f / 64.0f, 34.0f / 64.0f, 10.0f / 64.0f, 42.0f / 64.0f,
48.0f / 64.0f, 16.0f / 64.0f, 56.0f / 64.0f, 24.0f / 64.0f, 50.0f / 64.0f, 18.0f / 64.0f, 58.0f / 64.0f, 26.0f / 64.0f,
12.0f / 64.0f, 44.0f / 64.0f, 4.0f / 64.0f, 36.0f / 64.0f, 14.0f / 64.0f, 46.0f / 64.0f, 6.0f / 64.0f, 38.0f / 64.0f,
60.0f / 64.0f, 28.0f / 64.0f, 52.0f / 64.0f, 20.0f / 64.0f, 62.0f / 64.0f, 30.0f / 64.0f, 54.0f / 64.0f, 22.0f / 64.0f,
3.0f / 64.0f, 35.0f / 64.0f, 11.0f / 64.0f, 43.0f / 64.0f, 1.0f / 64.0f, 33.0f / 64.0f, 9.0f / 64.0f, 41.0f / 64.0f,
51.0f / 64.0f, 19.0f / 64.0f, 59.0f / 64.0f, 27.0f / 64.0f, 49.0f / 64.0f, 17.0f / 64.0f, 57.0f / 64.0f, 25.0f / 64.0f,
15.0f / 64.0f, 47.0f / 64.0f, 7.0f / 64.0f, 39.0f / 64.0f, 13.0f / 64.0f, 45.0f / 64.0f, 5.0f / 64.0f, 37.0f / 64.0f,
63.0f / 64.0f, 31.0f / 64.0f, 55.0f / 64.0f, 23.0f / 64.0f, 61.0f / 64.0f, 29.0f / 64.0f, 53.0f / 64.0f, 21.0f / 64.0f
};
const int x = PositionToMatrixIndex(position.GetX(), patternSize);
const int y = PositionToMatrixIndex(position.GetY(), patternSize);
return indexMatrix[patternSize * y + x] / static_cast<float>(patternSizeSq);
return indexMatrix[ScaledPositionToPatternIndex(scaledPosition, patternSize)];
}
float DitherGradientComponent::GetValue(const GradientSampleParams& sampleParams) const
float DitherGradientComponent::GetCalculatedPointsPerUnit() const
{
AZ_PROFILE_FUNCTION(Entity);
const AZ::Vector3& coordinate = sampleParams.m_position;
float pointsPerUnit = m_configuration.m_pointsPerUnit;
if (m_configuration.m_useSystemPointsPerUnit)
{
SectorDataRequestBus::Broadcast(&SectorDataRequestBus::Events::GetPointsPerMeter, pointsPerUnit);
}
pointsPerUnit = AZ::GetMax(pointsPerUnit, 0.0001f);
auto scaledCoordinate = coordinate * pointsPerUnit;
auto x = std::floor(scaledCoordinate.GetX()) / pointsPerUnit;
auto y = std::floor(scaledCoordinate.GetY()) / pointsPerUnit;
auto z = std::floor(scaledCoordinate.GetZ()) / pointsPerUnit;
AZ::Vector3 flooredCoordinate(x, y, z);
GradientSampleParams adjustedSampleParams = sampleParams;
adjustedSampleParams.m_position = flooredCoordinate;
float value = m_configuration.m_gradientSampler.GetValue(adjustedSampleParams);
return AZ::GetMax(pointsPerUnit, 0.0001f);
}
float DitherGradientComponent::GetDitherValue(const AZ::Vector3& scaledPosition, float value) const
{
float d = 0.0f;
switch (m_configuration.m_patternType)
{
default:
case DitherGradientConfig::BayerPatternType::PATTERN_SIZE_4x4:
d = GetDitherValue4x4((scaledCoordinate) + m_configuration.m_patternOffset);
d = GetDitherValue4x4(scaledPosition + m_configuration.m_patternOffset);
break;
case DitherGradientConfig::BayerPatternType::PATTERN_SIZE_8x8:
d = GetDitherValue8x8((scaledCoordinate) + m_configuration.m_patternOffset);
d = GetDitherValue8x8(scaledPosition + m_configuration.m_patternOffset);
break;
}
return value > d ? 1.0f : 0.0f;
return (value > d) ? 1.0f : 0.0f;
}
float DitherGradientComponent::GetValue(const GradientSampleParams& sampleParams) const
{
const AZ::Vector3& coordinate = sampleParams.m_position;
const float pointsPerUnit = GetCalculatedPointsPerUnit();
AZ::Vector3 scaledCoordinate = coordinate * pointsPerUnit;
AZ::Vector3 flooredCoordinate = scaledCoordinate.GetFloor() / pointsPerUnit;
GradientSampleParams adjustedSampleParams = sampleParams;
adjustedSampleParams.m_position = flooredCoordinate;
float value = m_configuration.m_gradientSampler.GetValue(adjustedSampleParams);
return GetDitherValue(scaledCoordinate, value);
}
void DitherGradientComponent::GetValues(AZStd::span<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;
}
const float pointsPerUnit = GetCalculatedPointsPerUnit();
// Create the entire set of floored coordinates to use in the gradient value lookups.
AZStd::vector<AZ::Vector3> flooredCoordinates(positions.size());
for (size_t index = 0; index < positions.size(); index++)
{
AZ::Vector3 scaledCoordinate = positions[index] * pointsPerUnit;
flooredCoordinates[index] = scaledCoordinate.GetFloor() / pointsPerUnit;
}
m_configuration.m_gradientSampler.GetValues(flooredCoordinates, outValues);
// For each gradient value, turn it into a 0 or 1 based on the location and the dither pattern.
for (size_t index = 0; index < positions.size(); index++)
{
AZ::Vector3 scaledCoordinate = positions[index] * pointsPerUnit;
outValues[index] = GetDitherValue(scaledCoordinate, outValues[index]);
}
}
bool DitherGradientComponent::IsEntityInHierarchy(const AZ::EntityId& entityId) const
@@ -219,7 +219,7 @@ namespace GradientSignal
return 0.0f;
}
void ImageGradientComponent::GetValues(AZStd::array_view<AZ::Vector3> positions, AZStd::array_view<float> outValues) const
void ImageGradientComponent::GetValues(AZStd::span<AZ::Vector3> positions, AZStd::span<float> outValues) const
{
if (positions.size() != outValues.size())
{
@@ -234,20 +234,16 @@ namespace GradientSignal
for (size_t index = 0; index < positions.size(); index++)
{
// The const_cast is necessary for now since array_view currently only supports const entries.
// If/when array_view is fixed to support non-const, or AZStd::span gets created, the const_cast can get removed.
auto& outValue = const_cast<float&>(outValues[index]);
m_gradientTransform.TransformPositionToUVWNormalized(positions[index], uvw, wasPointRejected);
if (!wasPointRejected)
{
outValue = GetValueFromImageAsset(
outValues[index] = GetValueFromImageAsset(
m_configuration.m_imageAsset, uvw, m_configuration.m_tilingX, m_configuration.m_tilingY, 0.0f);
}
else
{
outValue = 0.0f;
outValues[index] = 0.0f;
}
}
}
@@ -137,6 +137,21 @@ namespace GradientSignal
return output;
}
void InvertGradientComponent::GetValues(AZStd::span<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;
}
m_configuration.m_gradientSampler.GetValues(positions, outValues);
for (auto& outValue : outValues)
{
outValue = 1.0f - AZ::GetClamp(outValue, 0.0f, 1.0f);
}
}
bool InvertGradientComponent::IsEntityInHierarchy(const AZ::EntityId& entityId) const
{
return m_configuration.m_gradientSampler.IsEntityInHierarchy(entityId);
@@ -172,8 +172,6 @@ namespace GradientSignal
float LevelsGradientComponent::GetValue(const GradientSampleParams& sampleParams) const
{
AZ_PROFILE_FUNCTION(Entity);
float output = 0.0f;
output = GetLevels(
@@ -187,6 +185,21 @@ namespace GradientSignal
return output;
}
void LevelsGradientComponent::GetValues(AZStd::span<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;
}
m_configuration.m_gradientSampler.GetValues(positions, outValues);
GetLevels(outValues,
m_configuration.m_inputMid, m_configuration.m_inputMin, m_configuration.m_inputMax,
m_configuration.m_outputMin, m_configuration.m_outputMax);
}
bool LevelsGradientComponent::IsEntityInHierarchy(const AZ::EntityId& entityId) const
{
return m_configuration.m_gradientSampler.IsEntityInHierarchy(entityId);
@@ -203,7 +203,7 @@ namespace GradientSignal
return 0.0f;
}
void PerlinGradientComponent::GetValues(AZStd::array_view<AZ::Vector3> positions, AZStd::array_view<float> outValues) const
void PerlinGradientComponent::GetValues(AZStd::span<AZ::Vector3> positions, AZStd::span<float> outValues) const
{
if (positions.size() != outValues.size())
{
@@ -218,21 +218,17 @@ namespace GradientSignal
for (size_t index = 0; index < positions.size(); index++)
{
// The const_cast is necessary for now since array_view currently only supports const entries.
// If/when array_view is fixed to support non-const, or AZStd::span gets created, the const_cast can get removed.
auto& outValue = const_cast<float&>(outValues[index]);
m_gradientTransform.TransformPositionToUVW(positions[index], uvw, wasPointRejected);
if (!wasPointRejected)
{
outValue = m_perlinImprovedNoise->GenerateOctaveNoise(
outValues[index] = m_perlinImprovedNoise->GenerateOctaveNoise(
uvw.GetX(), uvw.GetY(), uvw.GetZ(), m_configuration.m_octave, m_configuration.m_amplitude,
m_configuration.m_frequency);
}
else
{
outValue = 0.0f;
outValues[index] = 0.0f;
}
}
}
@@ -183,7 +183,7 @@ namespace GradientSignal
return 0.0f;
}
void RandomGradientComponent::GetValues(AZStd::array_view<AZ::Vector3> positions, AZStd::array_view<float> outValues) const
void RandomGradientComponent::GetValues(AZStd::span<AZ::Vector3> positions, AZStd::span<float> outValues) const
{
if (positions.size() != outValues.size())
{
@@ -200,19 +200,15 @@ namespace GradientSignal
for (size_t index = 0; index < positions.size(); index++)
{
// The const_cast is necessary for now since array_view currently only supports const entries.
// If/when array_view is fixed to support non-const, or AZStd::span gets created, the const_cast can get removed.
auto& outValue = const_cast<float&>(outValues[index]);
m_gradientTransform.TransformPositionToUVW(positions[index], uvw, wasPointRejected);
if (!wasPointRejected)
{
outValue = GetRandomValue(uvw, seed);
outValues[index] = GetRandomValue(uvw, seed);
}
else
{
outValue = 0.0f;
outValues[index] = 0.0f;
}
}
}
@@ -168,7 +168,7 @@ namespace GradientSignal
return (distance <= 0.0f) ? 1.0f : AZ::GetMax(1.0f - (distance / m_configuration.m_falloffWidth), 0.0f);
}
void ShapeAreaFalloffGradientComponent::GetValues(AZStd::array_view<AZ::Vector3> positions, AZStd::array_view<float> outValues) const
void ShapeAreaFalloffGradientComponent::GetValues(AZStd::span<AZ::Vector3> positions, AZStd::span<float> outValues) const
{
if (positions.size() != outValues.size())
{
@@ -187,10 +187,6 @@ namespace GradientSignal
for (size_t index = 0; index < positions.size(); index++)
{
// The const_cast is necessary for now since array_view currently only supports const entries.
// If/when array_view is fixed to support non-const, or AZStd::span gets created, the const_cast can get removed.
auto& outValue = const_cast<float&>(outValues[index]);
float distance = shapeRequests->DistanceFromPoint(positions[index]);
// Since this is outer falloff, distance should give us values from 1.0 at the minimum distance to 0.0 at the maximum
@@ -198,18 +194,15 @@ namespace GradientSignal
// inside the shape (0 distance) return 1.0, and all points outside the shape return 0. This works because division by 0
// gives infinity, which gets clamped by the GetMax() to 0. However, if distance == 0, it would give us NaN, so we have
// the separate conditional check to handle that case and clamp to 1.0.
outValue = (distance <= 0.0f) ? 1.0f : AZ::GetMax(1.0f - (distance / falloffWidth), 0.0f);
outValues[index] = (distance <= 0.0f) ? 1.0f : AZ::GetMax(1.0f - (distance / falloffWidth), 0.0f);
}
});
// If there's no shape, there's no falloff.
if (!shapeConnected)
{
for (size_t index = 0; index < positions.size(); index++)
for (auto& outValue : outValues)
{
// The const_cast is necessary for now since array_view currently only supports const entries.
// If/when array_view is fixed to support non-const, or AZStd::span gets created, the const_cast can get removed.
auto& outValue = const_cast<float&>(outValues[index]);
outValue = 1.0f;
}
}
@@ -56,9 +56,9 @@ namespace UnitTest
params.m_position = positions[positionIndex];
float value = gradientSampler.GetValue(params);
// We use ASSERT_EQ instead of EXPECT_EQ because if one value doesn't match, they probably all won't, so there's no reason
// to keep running and printing failures for every value.
ASSERT_EQ(value, results[positionIndex]);
// We use ASSERT_NEAR instead of EXPECT_NEAR because if one value doesn't match, they probably all won't, so there's no
// reason to keep running and printing failures for every value.
ASSERT_NEAR(value, results[positionIndex], 0.000001f);
}
}
};
@@ -10,6 +10,8 @@
#include <AzTest/AzTest.h>
#include <AzFramework/Components/TransformComponent.h>
#include <GradientSignal/Components/ConstantGradientComponent.h>
#include <GradientSignal/Components/DitherGradientComponent.h>
#include <GradientSignal/Components/InvertGradientComponent.h>
@@ -81,6 +83,130 @@ namespace UnitTest
TestFixedDataSampler(expectedOutput, dataSize, entity->GetId());
}
TEST_F(GradientSignalServicesTestsFixture, DitherGradientComponent_4x4At50Pct_CrossingZero)
{
// With a 4x4 gradient filled with 8/16 (0.5), verify that the resulting dithered output
// is an expected checkerboard pattern with 8 of 16 pixels filled. The pattern offset is
// shifted -2 in the X direction so that the lookups go from [-2, 2) to verify that the
// pattern remains consistent across negative and positive coordinates.
constexpr int dataSize = 4;
AZStd::vector<float> inputData(dataSize * dataSize, 8.0f / 16.0f);
AZStd::vector<float> expectedOutput = {
1.0f, 0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f, 1.0f,
};
auto entityMock = CreateEntity();
const AZ::EntityId id = entityMock->GetId();
UnitTest::MockGradientArrayRequestsBus mockGradientRequestsBus(id, inputData, dataSize);
GradientSignal::DitherGradientConfig config;
config.m_useSystemPointsPerUnit = false;
config.m_pointsPerUnit = 1.0f;
config.m_patternOffset = AZ::Vector3(-2.0f, 0.0f, 0.0f);
config.m_patternType = GradientSignal::DitherGradientConfig::BayerPatternType::PATTERN_SIZE_4x4;
config.m_gradientSampler.m_gradientId = entityMock->GetId();
auto entity = CreateEntity();
entity->CreateComponent<GradientSignal::DitherGradientComponent>(config);
ActivateEntity(entity.get());
TestFixedDataSampler(expectedOutput, dataSize, entity->GetId());
}
TEST_F(GradientSignalServicesTestsFixture, DitherGradientComponent_4x4At50Pct_MorePointsPerUnit)
{
// With a 4x4 gradient filled with 8/16 (0.5), and 1/2 point per unit, if we query a 4x4 region,
// we should get a checkerboard in 2x2 blocks of the same value because it takes 2 units before the value changes.
constexpr int dataSize = 4;
AZStd::vector<float> inputData(dataSize * dataSize, 8.0f / 16.0f);
AZStd::vector<float> expectedOutput = {
1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
};
auto entityMock = CreateEntity();
const AZ::EntityId id = entityMock->GetId();
UnitTest::MockGradientArrayRequestsBus mockGradientRequestsBus(id, inputData, dataSize);
GradientSignal::DitherGradientConfig config;
config.m_useSystemPointsPerUnit = false;
config.m_pointsPerUnit = 0.5f;
config.m_patternOffset = AZ::Vector3::CreateZero();
config.m_patternType = GradientSignal::DitherGradientConfig::BayerPatternType::PATTERN_SIZE_4x4;
config.m_gradientSampler.m_gradientId = entityMock->GetId();
auto entity = CreateEntity();
entity->CreateComponent<GradientSignal::DitherGradientComponent>(config);
ActivateEntity(entity.get());
TestFixedDataSampler(expectedOutput, dataSize, entity->GetId());
}
TEST_F(GradientSignalServicesTestsFixture, DitherGradientComponent_4x4At50Pct_MorePointsAndCrossingZero)
{
// With a 4x4 gradient filled with 8/16 (0.5), and 2 points per unit, verify that querying
// from -1 to 1 produces a constant checkerboard pattern of results as it crosses the 0 boundary.
// Our expected results are a consistent checkerboard pattern, but with 2x2 blocks of the same value because we're
// querying at 2x the point density (i.e. querying 4 points per unit) to ensure that fractional position lookups work too.
float expectedValues[] = {
1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
};
// Create a 50% constant gradient.
GradientSignal::ConstantGradientConfig constantConfig;
constantConfig.m_value = 8.0f / 16.0f;
auto constantGradientEntity = CreateEntity();
constantGradientEntity->CreateComponent<GradientSignal::ConstantGradientComponent>(constantConfig);
ActivateEntity(constantGradientEntity.get());
GradientSignal::DitherGradientConfig config;
config.m_useSystemPointsPerUnit = false;
config.m_pointsPerUnit = 2.0f;
config.m_patternOffset = AZ::Vector3::CreateZero();
config.m_patternType = GradientSignal::DitherGradientConfig::BayerPatternType::PATTERN_SIZE_4x4;
config.m_gradientSampler.m_gradientId = constantGradientEntity->GetId();
auto entity = CreateEntity();
entity->CreateComponent<GradientSignal::DitherGradientComponent>(config);
ActivateEntity(entity.get());
// Run through [-1, 1) at 1/4 intervals and make sure we get our expected checkerboard. This is testing both that
// we have a consistent pattern across the 0 boundary and that fractional position lookups work correctly
GradientSignal::GradientSampler gradientSampler;
gradientSampler.m_gradientId = entity->GetId();
int expectedValueIndex = 0;
for (float y = -1.0f; y < 1.0f; y += 0.25f)
{
for (float x = -1.0f; x < 1.0f; x += 0.25f)
{
GradientSignal::GradientSampleParams params;
params.m_position = AZ::Vector3(x, y, 0.0f);
float actualValue = gradientSampler.GetValue(params);
float expectedValue = expectedValues[expectedValueIndex++];
EXPECT_NEAR(actualValue, expectedValue, 0.01f);
}
}
}
TEST_F(GradientSignalServicesTestsFixture, DitherGradientComponent_4x4At31Pct)
{
// With a 4x4 gradient filled with 5/16 (0.3125), verify that the resulting dithered output