GradientSignalTestFixtures.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <Tests/GradientSignalTestMocks.h>
  10. #include <LmbrCentral/Shape/MockShapes.h>
  11. #include <Atom/RPI.Reflect/Asset/AssetHandler.h>
  12. #include <AzTest/GemTestEnvironment.h>
  13. namespace UnitTest::StubRHI
  14. {
  15. class Factory;
  16. }// namespace UnitTest::StubRHI
  17. namespace AZ::RPI
  18. {
  19. class RPISystem;
  20. class ImageSystem;
  21. } // namespace AZ::RPI
  22. namespace UnitTest
  23. {
  24. // The GradientSignal unit tests need to use the GemTestEnvironment to load the LmbrCentral Gem so that Shape components can be used
  25. // in the unit tests and benchmarks.
  26. class GradientSignalTestEnvironment
  27. : public AZ::Test::GemTestEnvironment
  28. {
  29. public:
  30. void AddGemsAndComponents() override;
  31. void PostCreateApplication() override;
  32. };
  33. #ifdef HAVE_BENCHMARK
  34. //! The Benchmark environment is used for one time setup and tear down of shared resources
  35. class GradientSignalBenchmarkEnvironment
  36. : public AZ::Test::BenchmarkEnvironmentBase
  37. , public GradientSignalTestEnvironment
  38. {
  39. protected:
  40. void SetUpBenchmark() override
  41. {
  42. SetupEnvironment();
  43. }
  44. void TearDownBenchmark() override
  45. {
  46. TeardownEnvironment();
  47. }
  48. };
  49. #endif
  50. // Base test fixture used for GradientSignal unit tests and benchmark tests
  51. class GradientSignalBaseFixture
  52. {
  53. public:
  54. GradientSignalBaseFixture();
  55. virtual ~GradientSignalBaseFixture();
  56. void SetupCoreSystems();
  57. void TearDownCoreSystems();
  58. AZStd::unique_ptr<AZ::Entity> CreateEntity()
  59. {
  60. return AZStd::make_unique<AZ::Entity>();
  61. }
  62. void ActivateEntity(AZ::Entity* entity)
  63. {
  64. entity->Init();
  65. entity->Activate();
  66. }
  67. // Create an entity with a box shape and a transform. It won't be activated yet though, because we expect a gradient component
  68. // to also get added to it first before activation.
  69. AZStd::unique_ptr<AZ::Entity> CreateTestEntity(float shapeHalfBounds);
  70. // Create an entity with a sphere shape and a transform. It won't be activated yet though, because we expect a gradient component
  71. // to also get added to it first before activation.
  72. AZStd::unique_ptr<AZ::Entity> CreateTestSphereEntity(float shapeRadius);
  73. // Create and activate an entity with a gradient component of the requested type, initialized with test data.
  74. AZStd::unique_ptr<AZ::Entity> BuildTestConstantGradient(float shapeHalfBounds, float value = 0.75f);
  75. AZStd::unique_ptr<AZ::Entity> BuildTestImageGradient(float shapeHalfBounds);
  76. AZStd::unique_ptr<AZ::Entity> BuildTestPerlinGradient(float shapeHalfBounds);
  77. AZStd::unique_ptr<AZ::Entity> BuildTestRandomGradient(float shapeHalfBounds);
  78. AZStd::unique_ptr<AZ::Entity> BuildTestShapeAreaFalloffGradient(float shapeHalfBounds);
  79. AZStd::unique_ptr<AZ::Entity> BuildTestDitherGradient(float shapeHalfBounds, const AZ::EntityId& inputGradientId);
  80. AZStd::unique_ptr<AZ::Entity> BuildTestInvertGradient(float shapeHalfBounds, const AZ::EntityId& inputGradientId);
  81. AZStd::unique_ptr<AZ::Entity> BuildTestLevelsGradient(float shapeHalfBounds, const AZ::EntityId& inputGradientId);
  82. AZStd::unique_ptr<AZ::Entity> BuildTestMixedGradient(
  83. float shapeHalfBounds, const AZ::EntityId& baseGradientId, const AZ::EntityId& mixedGradientId);
  84. AZStd::unique_ptr<AZ::Entity> BuildTestPosterizeGradient(float shapeHalfBounds, const AZ::EntityId& inputGradientId);
  85. AZStd::unique_ptr<AZ::Entity> BuildTestReferenceGradient(float shapeHalfBounds, const AZ::EntityId& inputGradientId);
  86. AZStd::unique_ptr<AZ::Entity> BuildTestSmoothStepGradient(float shapeHalfBounds, const AZ::EntityId& inputGradientId);
  87. AZStd::unique_ptr<AZ::Entity> BuildTestThresholdGradient(float shapeHalfBounds, const AZ::EntityId& inputGradientId);
  88. AZStd::unique_ptr<AZ::Entity> BuildTestSurfaceAltitudeGradient(float shapeHalfBounds);
  89. AZStd::unique_ptr<AZ::Entity> BuildTestSurfaceMaskGradient(float shapeHalfBounds);
  90. AZStd::unique_ptr<AZ::Entity> BuildTestSurfaceSlopeGradient(float shapeHalfBounds);
  91. protected:
  92. AZStd::unique_ptr<UnitTest::StubRHI::Factory> m_rhiFactory;
  93. AZStd::unique_ptr<AZ::RPI::RPISystem> m_rpiSystem;
  94. AZStd::unique_ptr<AZ::RPI::ImageSystem> m_imageSystem;
  95. };
  96. struct GradientSignalTest
  97. : public GradientSignalBaseFixture
  98. , public ::testing::Test
  99. {
  100. protected:
  101. void SetUp() override;
  102. void TearDown() override;
  103. void TestFixedDataSampler(const AZStd::vector<float>& expectedOutput, int size, AZ::EntityId gradientEntityId);
  104. void TestFixedDataSampler(const AZStd::vector<float>& expectedOutput, int size, GradientSignal::GradientSampler& gradientSampler);
  105. };
  106. #ifdef HAVE_BENCHMARK
  107. class GradientSignalBenchmarkFixture
  108. : public GradientSignalBaseFixture
  109. , public ::benchmark::Fixture
  110. {
  111. public:
  112. void internalSetUp()
  113. {
  114. SetupCoreSystems();
  115. }
  116. void internalTearDown()
  117. {
  118. TearDownCoreSystems();
  119. }
  120. protected:
  121. void SetUp([[maybe_unused]] const benchmark::State& state) override
  122. {
  123. internalSetUp();
  124. }
  125. void SetUp([[maybe_unused]] benchmark::State& state) override
  126. {
  127. internalSetUp();
  128. }
  129. void TearDown([[maybe_unused]] const benchmark::State& state) override
  130. {
  131. internalTearDown();
  132. }
  133. void TearDown([[maybe_unused]] benchmark::State& state) override
  134. {
  135. internalTearDown();
  136. }
  137. };
  138. #endif
  139. }