123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005 |
- /*
- * 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 <AzCore/std/parallel/semaphore.h>
- #include <AzTest/AzTest.h>
- #include <TerrainSystem/TerrainSystem.h>
- #include <TerrainTestFixtures.h>
- namespace UnitTest::TerrainTest
- {
- /* The TerrainBulkQueryTest suite of tests exist to verify that all of our different query APIs produce the same results.
- * These tests were added after discovering that the async queries could sometimes produce intermittently incorrect results due
- * to a lack of proper thread safety. However, it's also possible that optimizations to the different queries could accidentally
- * produce different results as well, so it's good to have this safety net here.
- */
- class TerrainBulkQueryTest
- : public TerrainTestFixture
- {
- protected:
- // Use the ProcessHeightsFromRegion API as our baseline that we'll compare the other query APIs against.
- void GenerateBaselineHeightData(
- const AZ::Aabb& inputQueryRegion,
- const AZ::Vector2& inputQueryStepSize,
- AzFramework::Terrain::TerrainDataRequests::Sampler sampler,
- AZStd::vector<AZ::Vector3>& queryPositions,
- AZStd::vector<AZ::Vector3>& resultPositions,
- AZStd::vector<bool>& resultExistsFlags)
- {
- queryPositions.clear();
- resultPositions.clear();
- resultExistsFlags.clear();
- auto perPositionCallback = [&queryPositions, &resultPositions, &resultExistsFlags](
- [[maybe_unused]] size_t xIndex, [[maybe_unused]] size_t yIndex,
- const AzFramework::SurfaceData::SurfacePoint& surfacePoint, bool terrainExists)
- {
- queryPositions.emplace_back(surfacePoint.m_position.GetX(), surfacePoint.m_position.GetY(), 0.0f);
- resultPositions.emplace_back(surfacePoint.m_position);
- resultExistsFlags.emplace_back(terrainExists);
- };
- AzFramework::Terrain::TerrainQueryRegion queryRegion =
- AzFramework::Terrain::TerrainQueryRegion::CreateFromAabbAndStepSize(inputQueryRegion, inputQueryStepSize);
- AzFramework::Terrain::TerrainDataRequestBus::Broadcast(
- &AzFramework::Terrain::TerrainDataRequests::QueryRegion, queryRegion,
- AzFramework::Terrain::TerrainDataRequests::TerrainDataMask::Heights, perPositionCallback, sampler);
- EXPECT_EQ(queryPositions.size(), resultPositions.size());
- }
- // Compare two sets of output data and verify that they match.
- void ComparePositionData(const AZStd::vector<AZ::Vector3>& baselineValues, const AZStd::vector<bool>& baselineExistsFlags,
- const AZStd::vector<AZ::Vector3>& comparisonValues, const AZStd::vector<bool>& comparisonExistsFlags)
- {
- // Verify that we have the same quantity of results in both sets.
- ASSERT_EQ(baselineValues.size(), comparisonValues.size());
- // Verify that every value is found exactly once in each set. The two sets might not have the values in the same order though,
- // so we need to search for each value, verify it's found, and verify that it hadn't previously been found.
- AZStd::vector<bool> matchFound(baselineValues.size(), false);
- for (size_t comparisonIndex = 0; comparisonIndex < comparisonValues.size(); comparisonIndex++)
- {
- auto foundValue = AZStd::find(baselineValues.begin(), baselineValues.end(), comparisonValues[comparisonIndex]);
- EXPECT_NE(foundValue, baselineValues.end());
- if (foundValue != baselineValues.end())
- {
- size_t foundIndex = foundValue - baselineValues.begin();
- EXPECT_FALSE(matchFound[foundIndex]);
- EXPECT_EQ(baselineExistsFlags[foundIndex], comparisonExistsFlags[comparisonIndex]);
- matchFound[foundIndex] = true;
- }
- }
- }
- // Use the ProcessNormalsFromRegion API as our baseline that we'll compare the other query APIs against.
- void GenerateBaselineNormalData(
- const AZ::Aabb& inputQueryRegion,
- const AZ::Vector2& inputQueryStepSize,
- AzFramework::Terrain::TerrainDataRequests::Sampler sampler,
- AZStd::vector<AZ::Vector3>& queryPositions,
- AZStd::vector<AZ::Vector3>& resultNormals,
- AZStd::vector<bool>& resultExistsFlags)
- {
- queryPositions.clear();
- resultNormals.clear();
- resultExistsFlags.clear();
- auto perPositionCallback = [&queryPositions, &resultNormals, &resultExistsFlags](
- [[maybe_unused]] size_t xIndex, [[maybe_unused]] size_t yIndex,
- const AzFramework::SurfaceData::SurfacePoint& surfacePoint, bool terrainExists)
- {
- queryPositions.emplace_back(surfacePoint.m_position.GetX(), surfacePoint.m_position.GetY(), 0.0f);
- resultNormals.emplace_back(surfacePoint.m_normal);
- resultExistsFlags.emplace_back(terrainExists);
- };
- AzFramework::Terrain::TerrainQueryRegion queryRegion =
- AzFramework::Terrain::TerrainQueryRegion::CreateFromAabbAndStepSize(inputQueryRegion, inputQueryStepSize);
- AzFramework::Terrain::TerrainDataRequestBus::Broadcast(
- &AzFramework::Terrain::TerrainDataRequests::QueryRegion, queryRegion,
- AzFramework::Terrain::TerrainDataRequests::TerrainDataMask::Normals, perPositionCallback, sampler);
- EXPECT_EQ(queryPositions.size(), resultNormals.size());
- }
- // Compare two sets of output data and verify that they match.
- void CompareNormalData(
- const AZStd::vector<AZ::Vector3>& baselineQueryPositions,
- const AZStd::vector<AZ::Vector3>& baselineValues,
- const AZStd::vector<bool>& baselineExistsFlags,
- const AZStd::vector<AZ::Vector3>& comparisonQueryPositions,
- const AZStd::vector<AZ::Vector3>& comparisonValues,
- const AZStd::vector<bool>& comparisonExistsFlags)
- {
- // Verify that we have the same quantity of results in both sets.
- ASSERT_EQ(baselineValues.size(), comparisonValues.size());
- // Verify that every value is found exactly once in each set. The two sets might not have the values in the same order though,
- // so we need to search for each value, verify it's found, and verify that it hadn't previously been found.
- // Also, since normals are easy to duplicate, we'll search by query positions to find the normals to compare.
- AZStd::vector<bool> matchFound(baselineValues.size(), false);
- for (size_t comparisonIndex = 0; comparisonIndex < comparisonQueryPositions.size(); comparisonIndex++)
- {
- auto& comparisonPosition = comparisonQueryPositions[comparisonIndex];
- auto foundPosition = AZStd::find(baselineQueryPositions.begin(), baselineQueryPositions.end(), comparisonPosition);
- EXPECT_NE(foundPosition, baselineQueryPositions.end());
- if (foundPosition != baselineQueryPositions.end())
- {
- size_t foundIndex = foundPosition - baselineQueryPositions.begin();
- EXPECT_FALSE(matchFound[foundIndex]);
- EXPECT_EQ(baselineValues[foundIndex], comparisonValues[comparisonIndex]);
- EXPECT_EQ(baselineExistsFlags[foundIndex], comparisonExistsFlags[comparisonIndex]);
- matchFound[foundIndex] = true;
- }
- }
- }
- // Use the ProcessSurfaceWeightsFromRegion API as our baseline that we'll compare the other query APIs against.
- void GenerateBaselineSurfaceWeightData(
- const AZ::Aabb& inputQueryRegion,
- const AZ::Vector2& inputQueryStepSize,
- AzFramework::Terrain::TerrainDataRequests::Sampler sampler,
- AZStd::vector<AZ::Vector3>& queryPositions,
- AZStd::vector<AzFramework::SurfaceData::SurfaceTagWeightList>& resultWeights)
- {
- queryPositions.clear();
- resultWeights.clear();
- auto perPositionCallback = [&queryPositions, &resultWeights](
- [[maybe_unused]] size_t xIndex, [[maybe_unused]] size_t yIndex,
- const AzFramework::SurfaceData::SurfacePoint& surfacePoint, bool terrainExists)
- {
- queryPositions.emplace_back(surfacePoint.m_position.GetX(), surfacePoint.m_position.GetY(), 0.0f);
- resultWeights.emplace_back(surfacePoint.m_surfaceTags);
- // For these unit tests, we expect every point queried to have valid terrain data.
- EXPECT_TRUE(terrainExists);
- };
- AzFramework::Terrain::TerrainQueryRegion queryRegion =
- AzFramework::Terrain::TerrainQueryRegion::CreateFromAabbAndStepSize(inputQueryRegion, inputQueryStepSize);
- AzFramework::Terrain::TerrainDataRequestBus::Broadcast(
- &AzFramework::Terrain::TerrainDataRequests::QueryRegion, queryRegion,
- AzFramework::Terrain::TerrainDataRequests::TerrainDataMask::SurfaceData, perPositionCallback, sampler);
- EXPECT_EQ(queryPositions.size(), resultWeights.size());
- }
- // Compare two sets of output data and verify that they match.
- void CompareSurfaceWeightData(
- const AZStd::vector<AZ::Vector3>& baselineQueryPositions,
- const AZStd::vector<AzFramework::SurfaceData::SurfaceTagWeightList>& baselineValues,
- const AZStd::vector<AZ::Vector3>& comparisonQueryPositions,
- const AZStd::vector<AzFramework::SurfaceData::SurfaceTagWeightList>& comparisonValues)
- {
- // Verify that we have the same quantity of results in both sets.
- ASSERT_EQ(baselineValues.size(), comparisonValues.size());
- // Verify that every value is found exactly once in each set. The two sets might not have the values in the same order though,
- // so we need to search for each value, verify it's found, and verify that it hadn't previously been found.
- // Also, since surface weight lists are easy to duplicate, we'll search by query positions to find the weight lists to compare.
- AZStd::vector<bool> matchFound(baselineValues.size(), false);
- for (size_t comparisonIndex = 0; comparisonIndex < comparisonQueryPositions.size(); comparisonIndex++)
- {
- auto& comparisonPosition = comparisonQueryPositions[comparisonIndex];
- auto foundPosition = AZStd::find(baselineQueryPositions.begin(), baselineQueryPositions.end(), comparisonPosition);
- EXPECT_NE(foundPosition, baselineQueryPositions.end());
- if (foundPosition != baselineQueryPositions.end())
- {
- size_t foundIndex = foundPosition - baselineQueryPositions.begin();
- EXPECT_FALSE(matchFound[foundIndex]);
- EXPECT_EQ(baselineValues[foundIndex], comparisonValues[comparisonIndex]);
- matchFound[foundIndex] = true;
- }
- }
- }
- // Use the ProcessSurfacePointsFromRegion API as our baseline that we'll compare the other query APIs against.
- void GenerateBaselineSurfacePointData(
- const AZ::Aabb& inputQueryRegion,
- const AZ::Vector2& inputQueryStepSize,
- AzFramework::Terrain::TerrainDataRequests::Sampler sampler,
- AZStd::vector<AZ::Vector3>& queryPositions,
- AZStd::vector<AzFramework::SurfaceData::SurfacePoint>& resultPoints,
- AZStd::vector<bool>& resultExistsFlags)
- {
- queryPositions.clear();
- resultPoints.clear();
- resultExistsFlags.clear();
- auto perPositionCallback = [&queryPositions, &resultPoints, &resultExistsFlags](
- [[maybe_unused]] size_t xIndex, [[maybe_unused]] size_t yIndex,
- const AzFramework::SurfaceData::SurfacePoint& surfacePoint, bool terrainExists)
- {
- queryPositions.emplace_back(surfacePoint.m_position.GetX(), surfacePoint.m_position.GetY(), 0.0f);
- resultPoints.emplace_back(surfacePoint);
- resultExistsFlags.emplace_back(terrainExists);
- };
- AzFramework::Terrain::TerrainQueryRegion queryRegion =
- AzFramework::Terrain::TerrainQueryRegion::CreateFromAabbAndStepSize(inputQueryRegion, inputQueryStepSize);
- AzFramework::Terrain::TerrainDataRequestBus::Broadcast(
- &AzFramework::Terrain::TerrainDataRequests::QueryRegion, queryRegion,
- AzFramework::Terrain::TerrainDataRequests::TerrainDataMask::All, perPositionCallback, sampler);
- EXPECT_EQ(queryPositions.size(), resultPoints.size());
- }
- // Compare two sets of output data and verify that they match.
- void CompareSurfacePointData(
- const AZStd::vector<AzFramework::SurfaceData::SurfacePoint>& baselineValues,
- const AZStd::vector<bool>& baselineExistsFlags,
- const AZStd::vector<AzFramework::SurfaceData::SurfacePoint>& comparisonValues,
- const AZStd::vector<bool>& comparisonExistsFlags)
- {
- // Verify that we have the same quantity of results in both sets.
- ASSERT_EQ(baselineValues.size(), comparisonValues.size());
- // Verify that every value is found exactly once in each set. The two sets might not have the values in the same order though,
- // so we need to search for each value, verify it's found, and verify that it hadn't previously been found.
- AZStd::vector<bool> matchFound(baselineValues.size(), false);
- for (size_t comparisonIndex = 0; comparisonIndex < comparisonValues.size(); comparisonIndex++)
- {
- const auto& comparisonValue = comparisonValues[comparisonIndex];
- auto foundValue = AZStd::find_if(
- baselineValues.begin(), baselineValues.end(),
- [&comparisonValue](const AzFramework::SurfaceData::SurfacePoint& baselineValue) -> bool
- {
- return (baselineValue.m_position == comparisonValue.m_position)
- && (baselineValue.m_normal == comparisonValue.m_normal)
- && (baselineValue.m_surfaceTags == comparisonValue.m_surfaceTags);
- });
- EXPECT_NE(foundValue, baselineValues.end());
- if (foundValue != baselineValues.end())
- {
- size_t foundIndex = foundValue - baselineValues.begin();
- EXPECT_FALSE(matchFound[foundIndex]);
- EXPECT_EQ(baselineExistsFlags[foundIndex], comparisonExistsFlags[comparisonIndex]);
- matchFound[foundIndex] = true;
- if (baselineExistsFlags[foundIndex] != comparisonExistsFlags[comparisonIndex])
- {
- matchFound[foundIndex] = true;
- }
- }
- }
- }
- AZStd::shared_ptr<AzFramework::Terrain::QueryAsyncParams> CreateTestAsyncParams()
- {
- auto params = AZStd::make_shared<AzFramework::Terrain::QueryAsyncParams>();
- // Set the number of jobs > 1 so that we have parallel queries that execute.
- params->m_desiredNumberOfJobs = 4;
- params->m_completionCallback =
- [this]([[maybe_unused]] AZStd::shared_ptr<AzFramework::Terrain::TerrainJobContext> context)
- {
- // Notify the main test thread that the query has completed.
- m_queryCompletionEvent.release();
- };
- return params;
- }
- // Set up the arbitrary terrain world parameters that we'll use for verifying our queries match.
- const static inline float TerrainSize = 32.0f;
- const static inline float TerrainQueryResolution = 1.0f;
- const static inline uint32_t TerrainNumSurfaces = 3;
- const static inline AZ::Aabb TerrainWorldBounds =
- AZ::Aabb::CreateFromMinMax(AZ::Vector3(-TerrainSize / 2.0f), AZ::Vector3(TerrainSize / 2.0f));
- // Set up the query parameters that we'll use for all our queries
- const static inline AZ::Aabb QueryBounds = TerrainWorldBounds;
- const static inline AZ::Vector2 QueryStepSize = AZ::Vector2(TerrainQueryResolution / 2.0f);
- const static inline uint32_t ExpectedResultCount =
- aznumeric_cast<uint32_t>((TerrainSize / QueryStepSize.GetX()) * (TerrainSize / QueryStepSize.GetY()));
- // Semaphore for use in async tests.
- AZStd::binary_semaphore m_queryCompletionEvent;
- };
- // -----------------------------------------------------------------------------
- // Compare Height Query APIs
- TEST_F(TerrainBulkQueryTest, ProcessHeightsFromRegionAndProcessHeightsFromListProduceSameResults)
- {
- CreateTestTerrainSystem(TerrainWorldBounds, TerrainQueryResolution, TerrainNumSurfaces);
- for (auto sampler : {
- AzFramework::Terrain::TerrainDataRequests::Sampler::BILINEAR,
- AzFramework::Terrain::TerrainDataRequests::Sampler::CLAMP,
- AzFramework::Terrain::TerrainDataRequests::Sampler::EXACT
- })
- {
- // Gather all our initial results from calling Process*FromRegion
- AZStd::vector<AZ::Vector3> queryPositions;
- AZStd::vector<AZ::Vector3> baselineResultPositions;
- AZStd::vector<bool> baselineExistsFlags;
- GenerateBaselineHeightData(QueryBounds, QueryStepSize, sampler, queryPositions, baselineResultPositions, baselineExistsFlags);
- ASSERT_EQ(queryPositions.size(), ExpectedResultCount);
- // Gather results from Process*FromList
- AZStd::vector<AZ::Vector3> comparisonResultPositions;
- AZStd::vector<bool> comparisonExistsFlags;
- auto listPositionCallback = [&comparisonResultPositions, &comparisonExistsFlags]
- (const AzFramework::SurfaceData::SurfacePoint& surfacePoint, bool terrainExists)
- {
- comparisonResultPositions.emplace_back(surfacePoint.m_position);
- comparisonExistsFlags.emplace_back(terrainExists);
- };
- AzFramework::Terrain::TerrainDataRequestBus::Broadcast(
- &AzFramework::Terrain::TerrainDataRequests::QueryList,
- queryPositions, AzFramework::Terrain::TerrainDataRequests::TerrainDataMask::Heights, listPositionCallback, sampler);
- // Compare the results
- ComparePositionData(baselineResultPositions, baselineExistsFlags, comparisonResultPositions, comparisonExistsFlags);
- }
- DestroyTestTerrainSystem();
- }
- TEST_F(TerrainBulkQueryTest, ProcessHeightsFromRegionAndGetHeightProduceSameResults)
- {
- CreateTestTerrainSystem(TerrainWorldBounds, TerrainQueryResolution, TerrainNumSurfaces);
- for (auto sampler :
- { AzFramework::Terrain::TerrainDataRequests::Sampler::BILINEAR, AzFramework::Terrain::TerrainDataRequests::Sampler::CLAMP,
- AzFramework::Terrain::TerrainDataRequests::Sampler::EXACT })
- {
- // Gather all our initial results from calling Process*FromRegion
- AZStd::vector<AZ::Vector3> queryPositions;
- AZStd::vector<AZ::Vector3> baselineResultPositions;
- AZStd::vector<bool> baselineExistsFlags;
- GenerateBaselineHeightData(QueryBounds, QueryStepSize, sampler, queryPositions, baselineResultPositions, baselineExistsFlags);
- ASSERT_EQ(queryPositions.size(), ExpectedResultCount);
- // Gather results from Get*
- AZStd::vector<AZ::Vector3> comparisonResultPositions;
- AZStd::vector<bool> comparisonExistsFlags;
- float worldMinZ = TerrainWorldBounds.GetMin().GetZ();
- for (auto& position : queryPositions)
- {
- float terrainHeight = worldMinZ;
- bool terrainExists = false;
- AzFramework::Terrain::TerrainDataRequestBus::BroadcastResult(
- terrainHeight, &AzFramework::Terrain::TerrainDataRequests::GetHeight, position, sampler, &terrainExists);
- comparisonResultPositions.emplace_back(position.GetX(), position.GetY(), terrainHeight);
- comparisonExistsFlags.emplace_back(terrainExists);
- }
- // Compare the results
- ComparePositionData(baselineResultPositions, baselineExistsFlags, comparisonResultPositions, comparisonExistsFlags);
- }
- DestroyTestTerrainSystem();
- }
- TEST_F(TerrainBulkQueryTest, ProcessHeightsFromRegionAndProcessHeightsFromRegionAsyncProduceSameResults)
- {
- CreateTestTerrainSystem(TerrainWorldBounds, TerrainQueryResolution, TerrainNumSurfaces);
- for (auto sampler :
- { AzFramework::Terrain::TerrainDataRequests::Sampler::BILINEAR, AzFramework::Terrain::TerrainDataRequests::Sampler::CLAMP,
- AzFramework::Terrain::TerrainDataRequests::Sampler::EXACT })
- {
- // Gather all our initial results from calling Process*FromRegion
- AZStd::vector<AZ::Vector3> queryPositions;
- AZStd::vector<AZ::Vector3> baselineResultPositions;
- AZStd::vector<bool> baselineExistsFlags;
- GenerateBaselineHeightData(QueryBounds, QueryStepSize, sampler, queryPositions, baselineResultPositions, baselineExistsFlags);
- ASSERT_EQ(queryPositions.size(), ExpectedResultCount);
- // Gather results from Process*FromRegionAsync
- AZStd::vector<AZ::Vector3> comparisonResultPositions;
- AZStd::vector<bool> comparisonExistsFlags;
- AZStd::mutex outputMutex;
- auto regionPositionCallback = [&comparisonResultPositions, &comparisonExistsFlags, &outputMutex](
- [[maybe_unused]] size_t xIndex, [[maybe_unused]] size_t yIndex,
- const AzFramework::SurfaceData::SurfacePoint& surfacePoint, bool terrainExists)
- {
- // Make sure only one thread can add its result at a time.
- AZStd::scoped_lock lock(outputMutex);
- comparisonResultPositions.emplace_back(surfacePoint.m_position);
- comparisonExistsFlags.emplace_back(terrainExists);
- };
- auto params = CreateTestAsyncParams();
- AZStd::shared_ptr<AzFramework::Terrain::TerrainJobContext> jobContext;
- AzFramework::Terrain::TerrainQueryRegion queryRegion =
- AzFramework::Terrain::TerrainQueryRegion::CreateFromAabbAndStepSize(QueryBounds, QueryStepSize);
- AzFramework::Terrain::TerrainDataRequestBus::BroadcastResult(
- jobContext, &AzFramework::Terrain::TerrainDataRequests::QueryRegionAsync, queryRegion,
- AzFramework::Terrain::TerrainDataRequests::TerrainDataMask::Heights, regionPositionCallback, sampler, params);
- // Wait for the async query to complete
- m_queryCompletionEvent.acquire();
- // Compare the results
- ComparePositionData(baselineResultPositions, baselineExistsFlags, comparisonResultPositions, comparisonExistsFlags);
- }
- DestroyTestTerrainSystem();
- }
- TEST_F(TerrainBulkQueryTest, ProcessHeightsFromRegionAndProcessHeightsFromListAsyncProduceSameResults)
- {
- CreateTestTerrainSystem(TerrainWorldBounds, TerrainQueryResolution, TerrainNumSurfaces);
- for (auto sampler :
- { AzFramework::Terrain::TerrainDataRequests::Sampler::BILINEAR, AzFramework::Terrain::TerrainDataRequests::Sampler::CLAMP,
- AzFramework::Terrain::TerrainDataRequests::Sampler::EXACT })
- {
- // Gather all our initial results from calling Process*FromRegion
- AZStd::vector<AZ::Vector3> queryPositions;
- AZStd::vector<AZ::Vector3> baselineResultPositions;
- AZStd::vector<bool> baselineExistsFlags;
- GenerateBaselineHeightData(QueryBounds, QueryStepSize, sampler, queryPositions, baselineResultPositions, baselineExistsFlags);
- ASSERT_EQ(queryPositions.size(), ExpectedResultCount);
- // Gather results from Process*FromListAsync
- AZStd::vector<AZ::Vector3> comparisonResultPositions;
- AZStd::vector<bool> comparisonExistsFlags;
- AZStd::mutex outputMutex;
- auto listPositionCallback = [&comparisonResultPositions, &comparisonExistsFlags, &outputMutex](
- const AzFramework::SurfaceData::SurfacePoint& surfacePoint, bool terrainExists)
- {
- // Make sure only one thread can add its result at a time.
- AZStd::scoped_lock lock(outputMutex);
- comparisonResultPositions.emplace_back(surfacePoint.m_position);
- comparisonExistsFlags.emplace_back(terrainExists);
- };
- auto params = CreateTestAsyncParams();
- AZStd::shared_ptr<AzFramework::Terrain::TerrainJobContext> jobContext;
- AzFramework::Terrain::TerrainDataRequestBus::BroadcastResult(
- jobContext, &AzFramework::Terrain::TerrainDataRequests::QueryListAsync, queryPositions,
- AzFramework::Terrain::TerrainDataRequests::TerrainDataMask::Heights,
- listPositionCallback, sampler, params);
- // Wait for the async query to complete
- m_queryCompletionEvent.acquire();
- // Compare the results
- ComparePositionData(baselineResultPositions, baselineExistsFlags, comparisonResultPositions, comparisonExistsFlags);
- }
- DestroyTestTerrainSystem();
- }
- // -----------------------------------------------------------------------------
- // Compare Normal Query APIs
- TEST_F(TerrainBulkQueryTest, ProcessNormalsFromRegionAndProcessNormalsFromListProduceSameResults)
- {
- CreateTestTerrainSystem(TerrainWorldBounds, TerrainQueryResolution, TerrainNumSurfaces);
- for (auto sampler :
- { AzFramework::Terrain::TerrainDataRequests::Sampler::BILINEAR, AzFramework::Terrain::TerrainDataRequests::Sampler::CLAMP,
- AzFramework::Terrain::TerrainDataRequests::Sampler::EXACT })
- {
- // Gather all our initial results from calling Process*FromRegion
- AZStd::vector<AZ::Vector3> queryPositions;
- AZStd::vector<AZ::Vector3> baselineResultNormals;
- AZStd::vector<bool> baselineExistsFlags;
- GenerateBaselineNormalData(QueryBounds, QueryStepSize, sampler, queryPositions, baselineResultNormals, baselineExistsFlags);
- ASSERT_EQ(queryPositions.size(), ExpectedResultCount);
- // Gather results from Process*FromList
- AZStd::vector<AZ::Vector3> comparisonResultPositions;
- AZStd::vector<AZ::Vector3> comparisonResultNormals;
- AZStd::vector<bool> comparisonExistsFlags;
- auto listNormalCallback = [&comparisonResultPositions, &comparisonResultNormals, &comparisonExistsFlags](
- const AzFramework::SurfaceData::SurfacePoint& surfacePoint, bool terrainExists)
- {
- comparisonResultPositions.emplace_back(surfacePoint.m_position);
- comparisonResultNormals.emplace_back(surfacePoint.m_normal);
- comparisonExistsFlags.emplace_back(terrainExists);
- };
- AzFramework::Terrain::TerrainDataRequestBus::Broadcast(
- &AzFramework::Terrain::TerrainDataRequests::QueryList, queryPositions,
- AzFramework::Terrain::TerrainDataRequests::TerrainDataMask::Normals, listNormalCallback, sampler);
- // Compare the results
- CompareNormalData(queryPositions, baselineResultNormals, baselineExistsFlags,
- comparisonResultPositions, comparisonResultNormals, comparisonExistsFlags);
- }
- DestroyTestTerrainSystem();
- }
- TEST_F(TerrainBulkQueryTest, ProcessNormalsFromRegionAndGetNormalProduceSameResults)
- {
- CreateTestTerrainSystem(TerrainWorldBounds, TerrainQueryResolution, TerrainNumSurfaces);
- for (auto sampler :
- { AzFramework::Terrain::TerrainDataRequests::Sampler::BILINEAR, AzFramework::Terrain::TerrainDataRequests::Sampler::CLAMP,
- AzFramework::Terrain::TerrainDataRequests::Sampler::EXACT })
- {
- // Gather all our initial results from calling Process*FromRegion
- AZStd::vector<AZ::Vector3> queryPositions;
- AZStd::vector<AZ::Vector3> baselineResultNormals;
- AZStd::vector<bool> baselineExistsFlags;
- GenerateBaselineNormalData(QueryBounds, QueryStepSize, sampler, queryPositions, baselineResultNormals, baselineExistsFlags);
- ASSERT_EQ(queryPositions.size(), ExpectedResultCount);
- // Gather results from Get*
- AZStd::vector<AZ::Vector3> comparisonResultNormals;
- AZStd::vector<bool> comparisonExistsFlags;
- for (auto& position : queryPositions)
- {
- AZ::Vector3 terrainNormal = AZ::Vector3::CreateZero();
- bool terrainExists = false;
- AzFramework::Terrain::TerrainDataRequestBus::BroadcastResult(
- terrainNormal, &AzFramework::Terrain::TerrainDataRequests::GetNormal, position, sampler, &terrainExists);
- comparisonResultNormals.emplace_back(terrainNormal);
- comparisonExistsFlags.emplace_back(terrainExists);
- }
- // Compare the results
- CompareNormalData(queryPositions, baselineResultNormals, baselineExistsFlags,
- queryPositions, comparisonResultNormals, comparisonExistsFlags);
- }
- DestroyTestTerrainSystem();
- }
- TEST_F(TerrainBulkQueryTest, ProcessNormalsFromRegionAndProcessNormalsFromRegionAsyncProduceSameResults)
- {
- CreateTestTerrainSystem(TerrainWorldBounds, TerrainQueryResolution, TerrainNumSurfaces);
- for (auto sampler :
- { AzFramework::Terrain::TerrainDataRequests::Sampler::BILINEAR, AzFramework::Terrain::TerrainDataRequests::Sampler::CLAMP,
- AzFramework::Terrain::TerrainDataRequests::Sampler::EXACT })
- {
- // Gather all our initial results from calling Process*FromRegion
- AZStd::vector<AZ::Vector3> queryPositions;
- AZStd::vector<AZ::Vector3> baselineResultNormals;
- AZStd::vector<bool> baselineExistsFlags;
- GenerateBaselineNormalData(QueryBounds, QueryStepSize, sampler, queryPositions, baselineResultNormals, baselineExistsFlags);
- ASSERT_EQ(queryPositions.size(), ExpectedResultCount);
- // Gather results from Process*FromRegionAsync
- AZStd::vector<AZ::Vector3> comparisonResultPositions;
- AZStd::vector<AZ::Vector3> comparisonResultNormals;
- AZStd::vector<bool> comparisonExistsFlags;
- AZStd::mutex outputMutex;
- auto regionPositionCallback = [&comparisonResultPositions, &comparisonResultNormals, &comparisonExistsFlags, &outputMutex](
- [[maybe_unused]] size_t xIndex, [[maybe_unused]] size_t yIndex,
- const AzFramework::SurfaceData::SurfacePoint& surfacePoint, bool terrainExists)
- {
- // Make sure only one thread can add its result at a time.
- AZStd::scoped_lock lock(outputMutex);
- comparisonResultPositions.emplace_back(surfacePoint.m_position);
- comparisonResultNormals.emplace_back(surfacePoint.m_normal);
- comparisonExistsFlags.emplace_back(terrainExists);
- };
- auto params = CreateTestAsyncParams();
- AZStd::shared_ptr<AzFramework::Terrain::TerrainJobContext> jobContext;
- AzFramework::Terrain::TerrainQueryRegion queryRegion =
- AzFramework::Terrain::TerrainQueryRegion::CreateFromAabbAndStepSize(QueryBounds, QueryStepSize);
- AzFramework::Terrain::TerrainDataRequestBus::BroadcastResult(
- jobContext, &AzFramework::Terrain::TerrainDataRequests::QueryRegionAsync, queryRegion,
- AzFramework::Terrain::TerrainDataRequests::TerrainDataMask::Normals, regionPositionCallback, sampler, params);
- // Wait for the async query to complete
- m_queryCompletionEvent.acquire();
- // Compare the results
- CompareNormalData(queryPositions, baselineResultNormals, baselineExistsFlags,
- comparisonResultPositions, comparisonResultNormals, comparisonExistsFlags);
- }
- DestroyTestTerrainSystem();
- }
- TEST_F(TerrainBulkQueryTest, ProcessNormalsFromRegionAndProcessNormalsFromListAsyncProduceSameResults)
- {
- CreateTestTerrainSystem(TerrainWorldBounds, TerrainQueryResolution, TerrainNumSurfaces);
- for (auto sampler :
- { AzFramework::Terrain::TerrainDataRequests::Sampler::BILINEAR, AzFramework::Terrain::TerrainDataRequests::Sampler::CLAMP,
- AzFramework::Terrain::TerrainDataRequests::Sampler::EXACT })
- {
- // Gather all our initial results from calling Process*FromRegion
- AZStd::vector<AZ::Vector3> queryPositions;
- AZStd::vector<AZ::Vector3> baselineResultNormals;
- AZStd::vector<bool> baselineExistsFlags;
- GenerateBaselineNormalData(QueryBounds, QueryStepSize, sampler, queryPositions, baselineResultNormals, baselineExistsFlags);
- ASSERT_EQ(queryPositions.size(), ExpectedResultCount);
- // Gather results from Process*FromListAsync
- AZStd::vector<AZ::Vector3> comparisonResultPositions;
- AZStd::vector<AZ::Vector3> comparisonResultNormals;
- AZStd::vector<bool> comparisonExistsFlags;
- AZStd::mutex outputMutex;
- auto listPositionCallback = [&comparisonResultPositions, &comparisonResultNormals, &comparisonExistsFlags, &outputMutex](
- const AzFramework::SurfaceData::SurfacePoint& surfacePoint, bool terrainExists)
- {
- // Make sure only one thread can add its result at a time.
- AZStd::scoped_lock lock(outputMutex);
- comparisonResultPositions.emplace_back(surfacePoint.m_position);
- comparisonResultNormals.emplace_back(surfacePoint.m_normal);
- comparisonExistsFlags.emplace_back(terrainExists);
- };
- auto params = CreateTestAsyncParams();
- AZStd::shared_ptr<AzFramework::Terrain::TerrainJobContext> jobContext;
- AzFramework::Terrain::TerrainDataRequestBus::BroadcastResult(
- jobContext, &AzFramework::Terrain::TerrainDataRequests::QueryListAsync, queryPositions,
- AzFramework::Terrain::TerrainDataRequests::TerrainDataMask::Normals, listPositionCallback,
- sampler, params);
- // Wait for the async query to complete
- m_queryCompletionEvent.acquire();
- // Compare the results
- CompareNormalData(queryPositions, baselineResultNormals, baselineExistsFlags,
- comparisonResultPositions, comparisonResultNormals, comparisonExistsFlags);
- }
- DestroyTestTerrainSystem();
- }
- // -----------------------------------------------------------------------------
- // Compare Surface Weight Query APIs
- TEST_F(TerrainBulkQueryTest, ProcessSurfaceWeightsFromRegionAndProcessSurfaceWeightsFromListProduceSameResults)
- {
- CreateTestTerrainSystem(TerrainWorldBounds, TerrainQueryResolution, TerrainNumSurfaces);
- for (auto sampler :
- { AzFramework::Terrain::TerrainDataRequests::Sampler::BILINEAR, AzFramework::Terrain::TerrainDataRequests::Sampler::CLAMP,
- AzFramework::Terrain::TerrainDataRequests::Sampler::EXACT })
- {
- // Gather all our initial results from calling Process*FromRegion
- AZStd::vector<AZ::Vector3> queryPositions;
- AZStd::vector<AzFramework::SurfaceData::SurfaceTagWeightList> baselineResultWeights;
- GenerateBaselineSurfaceWeightData(QueryBounds, QueryStepSize, sampler, queryPositions, baselineResultWeights);
- ASSERT_EQ(queryPositions.size(), ExpectedResultCount);
- // Gather results from Process*FromList
- AZStd::vector<AZ::Vector3> comparisonResultPositions;
- AZStd::vector<AzFramework::SurfaceData::SurfaceTagWeightList> comparisonResultWeights;
- auto listWeightsCallback = [&comparisonResultPositions, &comparisonResultWeights](
- const AzFramework::SurfaceData::SurfacePoint& surfacePoint, bool terrainExists)
- {
- comparisonResultPositions.emplace_back(surfacePoint.m_position);
- comparisonResultWeights.emplace_back(surfacePoint.m_surfaceTags);
- EXPECT_TRUE(terrainExists);
- };
- AzFramework::Terrain::TerrainDataRequestBus::Broadcast(
- &AzFramework::Terrain::TerrainDataRequests::QueryList, queryPositions,
- AzFramework::Terrain::TerrainDataRequests::TerrainDataMask::SurfaceData, listWeightsCallback, sampler);
- // Compare the results
- CompareSurfaceWeightData(queryPositions, baselineResultWeights, comparisonResultPositions, comparisonResultWeights);
- }
- DestroyTestTerrainSystem();
- }
- TEST_F(TerrainBulkQueryTest, ProcessSurfaceWeightsFromRegionAndGetSurfaceWeightsProduceSameResults)
- {
- CreateTestTerrainSystem(TerrainWorldBounds, TerrainQueryResolution, TerrainNumSurfaces);
- for (auto sampler :
- { AzFramework::Terrain::TerrainDataRequests::Sampler::BILINEAR, AzFramework::Terrain::TerrainDataRequests::Sampler::CLAMP,
- AzFramework::Terrain::TerrainDataRequests::Sampler::EXACT })
- {
- // Gather all our initial results from calling Process*FromRegion
- AZStd::vector<AZ::Vector3> queryPositions;
- AZStd::vector<AzFramework::SurfaceData::SurfaceTagWeightList> baselineResultWeights;
- GenerateBaselineSurfaceWeightData(QueryBounds, QueryStepSize, sampler, queryPositions, baselineResultWeights);
- ASSERT_EQ(queryPositions.size(), ExpectedResultCount);
- // Gather results from Get*
- AZStd::vector<AzFramework::SurfaceData::SurfaceTagWeightList> comparisonResultWeights;
- AzFramework::SurfaceData::SurfaceTagWeightList terrainWeights;
- for (auto& position : queryPositions)
- {
- bool terrainExists = false;
- AzFramework::Terrain::TerrainDataRequestBus::Broadcast(
- &AzFramework::Terrain::TerrainDataRequests::GetSurfaceWeights, position, terrainWeights, sampler, &terrainExists);
- comparisonResultWeights.emplace_back(terrainWeights);
- EXPECT_TRUE(terrainExists);
- }
- // Compare the results
- CompareSurfaceWeightData(queryPositions, baselineResultWeights, queryPositions, comparisonResultWeights);
- }
- DestroyTestTerrainSystem();
- }
- TEST_F(TerrainBulkQueryTest, ProcessSurfaceWeightsFromRegionAndProcessSurfaceWeightsFromRegionAsyncProduceSameResults)
- {
- CreateTestTerrainSystem(TerrainWorldBounds, TerrainQueryResolution, TerrainNumSurfaces);
- for (auto sampler :
- { AzFramework::Terrain::TerrainDataRequests::Sampler::BILINEAR, AzFramework::Terrain::TerrainDataRequests::Sampler::CLAMP,
- AzFramework::Terrain::TerrainDataRequests::Sampler::EXACT })
- {
- // Gather all our initial results from calling Process*FromRegion
- AZStd::vector<AZ::Vector3> queryPositions;
- AZStd::vector<AzFramework::SurfaceData::SurfaceTagWeightList> baselineResultWeights;
- GenerateBaselineSurfaceWeightData(QueryBounds, QueryStepSize, sampler, queryPositions, baselineResultWeights);
- ASSERT_EQ(queryPositions.size(), ExpectedResultCount);
- // Gather results from Process*FromRegionAsync
- AZStd::vector<AZ::Vector3> comparisonResultPositions;
- AZStd::vector<AzFramework::SurfaceData::SurfaceTagWeightList> comparisonResultWeights;
- AZStd::mutex outputMutex;
- auto perPositionCallback = [&comparisonResultPositions, &comparisonResultWeights, &outputMutex](
- [[maybe_unused]] size_t xIndex, [[maybe_unused]] size_t yIndex,
- const AzFramework::SurfaceData::SurfacePoint& surfacePoint, bool terrainExists)
- {
- // Make sure only one thread can add its result at a time.
- AZStd::scoped_lock lock(outputMutex);
- comparisonResultPositions.emplace_back(surfacePoint.m_position);
- comparisonResultWeights.emplace_back(surfacePoint.m_surfaceTags);
- EXPECT_TRUE(terrainExists);
- };
- auto params = CreateTestAsyncParams();
- AZStd::shared_ptr<AzFramework::Terrain::TerrainJobContext> jobContext;
- AzFramework::Terrain::TerrainQueryRegion queryRegion =
- AzFramework::Terrain::TerrainQueryRegion::CreateFromAabbAndStepSize(QueryBounds, QueryStepSize);
- AzFramework::Terrain::TerrainDataRequestBus::BroadcastResult(
- jobContext, &AzFramework::Terrain::TerrainDataRequests::QueryRegionAsync, queryRegion,
- AzFramework::Terrain::TerrainDataRequests::TerrainDataMask::SurfaceData, perPositionCallback, sampler, params);
- // Wait for the async query to complete
- m_queryCompletionEvent.acquire();
- // Compare the results
- CompareSurfaceWeightData(queryPositions, baselineResultWeights, comparisonResultPositions, comparisonResultWeights);
- }
- DestroyTestTerrainSystem();
- }
- TEST_F(TerrainBulkQueryTest, ProcessSurfaceWeightsFromRegionAndProcessSurfaceWeightsFromListAsyncProduceSameResults)
- {
- CreateTestTerrainSystem(TerrainWorldBounds, TerrainQueryResolution, TerrainNumSurfaces);
- for (auto sampler :
- { AzFramework::Terrain::TerrainDataRequests::Sampler::BILINEAR, AzFramework::Terrain::TerrainDataRequests::Sampler::CLAMP,
- AzFramework::Terrain::TerrainDataRequests::Sampler::EXACT })
- {
- // Gather all our initial results from calling Process*FromRegion
- AZStd::vector<AZ::Vector3> queryPositions;
- AZStd::vector<AzFramework::SurfaceData::SurfaceTagWeightList> baselineResultWeights;
- GenerateBaselineSurfaceWeightData(QueryBounds, QueryStepSize, sampler, queryPositions, baselineResultWeights);
- ASSERT_EQ(queryPositions.size(), ExpectedResultCount);
- // Gather results from Process*FromListAsync
- AZStd::vector<AZ::Vector3> comparisonResultPositions;
- AZStd::vector<AzFramework::SurfaceData::SurfaceTagWeightList> comparisonResultWeights;
- AZStd::mutex outputMutex;
- auto listPositionCallback = [&comparisonResultPositions, &comparisonResultWeights, &outputMutex](
- const AzFramework::SurfaceData::SurfacePoint& surfacePoint, bool terrainExists)
- {
- // Make sure only one thread can add its result at a time.
- AZStd::scoped_lock lock(outputMutex);
- comparisonResultPositions.emplace_back(surfacePoint.m_position);
- comparisonResultWeights.emplace_back(surfacePoint.m_surfaceTags);
- EXPECT_TRUE(terrainExists);
- };
- auto params = CreateTestAsyncParams();
- AZStd::shared_ptr<AzFramework::Terrain::TerrainJobContext> jobContext;
- AzFramework::Terrain::TerrainDataRequestBus::BroadcastResult(
- jobContext, &AzFramework::Terrain::TerrainDataRequests::QueryListAsync, queryPositions,
- AzFramework::Terrain::TerrainDataRequests::TerrainDataMask::SurfaceData, listPositionCallback, sampler, params);
- // Wait for the async query to complete
- m_queryCompletionEvent.acquire();
- // Compare the results
- CompareSurfaceWeightData(queryPositions, baselineResultWeights, comparisonResultPositions, comparisonResultWeights);
- }
- DestroyTestTerrainSystem();
- }
- // -----------------------------------------------------------------------------
- // Compare Surface Point Query APIs
- TEST_F(TerrainBulkQueryTest, ProcessSurfacePointsFromRegionAndProcessSurfacePointsFromListProduceSameResults)
- {
- CreateTestTerrainSystem(TerrainWorldBounds, TerrainQueryResolution, TerrainNumSurfaces);
- for (auto sampler :
- { AzFramework::Terrain::TerrainDataRequests::Sampler::BILINEAR, AzFramework::Terrain::TerrainDataRequests::Sampler::CLAMP,
- AzFramework::Terrain::TerrainDataRequests::Sampler::EXACT })
- {
- // Gather all our initial results from calling Process*FromRegion
- AZStd::vector<AZ::Vector3> queryPositions;
- AZStd::vector<AzFramework::SurfaceData::SurfacePoint> baselineResultPoints;
- AZStd::vector<bool> baselineExistsFlags;
- GenerateBaselineSurfacePointData(
- QueryBounds, QueryStepSize, sampler, queryPositions, baselineResultPoints, baselineExistsFlags);
- ASSERT_EQ(queryPositions.size(), ExpectedResultCount);
- // Gather results from Process*FromList
- AZStd::vector<AzFramework::SurfaceData::SurfacePoint> comparisonResultPoints;
- AZStd::vector<bool> comparisonExistsFlags;
- auto listPositionCallback = [&comparisonResultPoints, &comparisonExistsFlags](
- const AzFramework::SurfaceData::SurfacePoint& surfacePoint, bool terrainExists)
- {
- comparisonResultPoints.emplace_back(surfacePoint);
- comparisonExistsFlags.emplace_back(terrainExists);
- };
- AzFramework::Terrain::TerrainDataRequestBus::Broadcast(
- &AzFramework::Terrain::TerrainDataRequests::QueryList, queryPositions,
- AzFramework::Terrain::TerrainDataRequests::TerrainDataMask::All, listPositionCallback, sampler);
- // Compare the results
- CompareSurfacePointData(baselineResultPoints, baselineExistsFlags, comparisonResultPoints, comparisonExistsFlags);
- }
- DestroyTestTerrainSystem();
- }
- TEST_F(TerrainBulkQueryTest, ProcessSurfacePointsFromRegionAndGetSurfacePointProduceSameResults)
- {
- CreateTestTerrainSystem(TerrainWorldBounds, TerrainQueryResolution, TerrainNumSurfaces);
- for (auto sampler :
- { AzFramework::Terrain::TerrainDataRequests::Sampler::BILINEAR, AzFramework::Terrain::TerrainDataRequests::Sampler::CLAMP,
- AzFramework::Terrain::TerrainDataRequests::Sampler::EXACT })
- {
- // Gather all our initial results from calling Process*FromRegion
- AZStd::vector<AZ::Vector3> queryPositions;
- AZStd::vector<AzFramework::SurfaceData::SurfacePoint> baselineResultPoints;
- AZStd::vector<bool> baselineExistsFlags;
- GenerateBaselineSurfacePointData(
- QueryBounds, QueryStepSize, sampler, queryPositions, baselineResultPoints, baselineExistsFlags);
- ASSERT_EQ(queryPositions.size(), ExpectedResultCount);
- // Gather results from Get*
- AZStd::vector<AzFramework::SurfaceData::SurfacePoint> comparisonResultPoints;
- AZStd::vector<bool> comparisonExistsFlags;
- AzFramework::SurfaceData::SurfacePoint surfacePoint;
- for (auto& position : queryPositions)
- {
- bool terrainExists = false;
- AzFramework::Terrain::TerrainDataRequestBus::Broadcast(
- &AzFramework::Terrain::TerrainDataRequests::GetSurfacePoint, position, surfacePoint, sampler, &terrainExists);
- comparisonResultPoints.emplace_back(surfacePoint);
- comparisonExistsFlags.emplace_back(terrainExists);
- }
- // Compare the results
- CompareSurfacePointData(baselineResultPoints, baselineExistsFlags, comparisonResultPoints, comparisonExistsFlags);
- }
- DestroyTestTerrainSystem();
- }
- TEST_F(TerrainBulkQueryTest, ProcessSurfacePointsFromRegionAndProcessSurfacePointsFromRegionAsyncProduceSameResults)
- {
- CreateTestTerrainSystem(TerrainWorldBounds, TerrainQueryResolution, TerrainNumSurfaces);
- for (auto sampler :
- { AzFramework::Terrain::TerrainDataRequests::Sampler::BILINEAR, AzFramework::Terrain::TerrainDataRequests::Sampler::CLAMP,
- AzFramework::Terrain::TerrainDataRequests::Sampler::EXACT })
- {
- // Gather all our initial results from calling Process*FromRegion
- AZStd::vector<AZ::Vector3> queryPositions;
- AZStd::vector<AzFramework::SurfaceData::SurfacePoint> baselineResultPoints;
- AZStd::vector<bool> baselineExistsFlags;
- GenerateBaselineSurfacePointData(
- QueryBounds, QueryStepSize, sampler, queryPositions, baselineResultPoints, baselineExistsFlags);
- ASSERT_EQ(queryPositions.size(), ExpectedResultCount);
- // Gather results from Process*FromRegionAsync
- AZStd::vector<AzFramework::SurfaceData::SurfacePoint> comparisonResultPoints;
- AZStd::vector<bool> comparisonExistsFlags;
- AZStd::mutex outputMutex;
- auto regionPositionCallback = [&comparisonResultPoints, &comparisonExistsFlags, &outputMutex](
- [[maybe_unused]] size_t xIndex, [[maybe_unused]] size_t yIndex,
- const AzFramework::SurfaceData::SurfacePoint& surfacePoint, bool terrainExists)
- {
- // Make sure only one thread can add its result at a time.
- AZStd::scoped_lock lock(outputMutex);
- comparisonResultPoints.emplace_back(surfacePoint);
- comparisonExistsFlags.emplace_back(terrainExists);
- };
- auto params = CreateTestAsyncParams();
- AZStd::shared_ptr<AzFramework::Terrain::TerrainJobContext> jobContext;
- AzFramework::Terrain::TerrainQueryRegion queryRegion =
- AzFramework::Terrain::TerrainQueryRegion::CreateFromAabbAndStepSize(QueryBounds, QueryStepSize);
- AzFramework::Terrain::TerrainDataRequestBus::BroadcastResult(
- jobContext, &AzFramework::Terrain::TerrainDataRequests::QueryRegionAsync, queryRegion,
- AzFramework::Terrain::TerrainDataRequests::TerrainDataMask::All, regionPositionCallback, sampler, params);
- // Wait for the async query to complete
- m_queryCompletionEvent.acquire();
- // Compare the results
- CompareSurfacePointData(baselineResultPoints, baselineExistsFlags, comparisonResultPoints, comparisonExistsFlags);
- }
- DestroyTestTerrainSystem();
- }
- TEST_F(TerrainBulkQueryTest, ProcessSurfacePointsFromRegionAndProcessSurfacePointsFromListAsyncProduceSameResults)
- {
- CreateTestTerrainSystem(TerrainWorldBounds, TerrainQueryResolution, TerrainNumSurfaces);
- for (auto sampler :
- { AzFramework::Terrain::TerrainDataRequests::Sampler::BILINEAR, AzFramework::Terrain::TerrainDataRequests::Sampler::CLAMP,
- AzFramework::Terrain::TerrainDataRequests::Sampler::EXACT })
- {
- // Gather all our initial results from calling Process*FromRegion
- AZStd::vector<AZ::Vector3> queryPositions;
- AZStd::vector<AzFramework::SurfaceData::SurfacePoint> baselineResultPoints;
- AZStd::vector<bool> baselineExistsFlags;
- GenerateBaselineSurfacePointData(
- QueryBounds, QueryStepSize, sampler, queryPositions, baselineResultPoints, baselineExistsFlags);
- ASSERT_EQ(queryPositions.size(), ExpectedResultCount);
- // Gather results from Process*FromListAsync
- AZStd::vector<AzFramework::SurfaceData::SurfacePoint> comparisonResultPoints;
- AZStd::vector<bool> comparisonExistsFlags;
- AZStd::mutex outputMutex;
- auto listPositionCallback = [&comparisonResultPoints, &comparisonExistsFlags, &outputMutex](
- const AzFramework::SurfaceData::SurfacePoint& surfacePoint, bool terrainExists)
- {
- // Make sure only one thread can add its result at a time.
- AZStd::scoped_lock lock(outputMutex);
- comparisonResultPoints.emplace_back(surfacePoint);
- comparisonExistsFlags.emplace_back(terrainExists);
- };
- auto params = CreateTestAsyncParams();
- AZStd::shared_ptr<AzFramework::Terrain::TerrainJobContext> jobContext;
- AzFramework::Terrain::TerrainDataRequestBus::BroadcastResult(
- jobContext, &AzFramework::Terrain::TerrainDataRequests::QueryListAsync, queryPositions,
- AzFramework::Terrain::TerrainDataRequests::TerrainDataMask::All, listPositionCallback, sampler, params);
- // Wait for the async query to complete
- m_queryCompletionEvent.acquire();
- // Compare the results
- CompareSurfacePointData(baselineResultPoints, baselineExistsFlags, comparisonResultPoints, comparisonExistsFlags);
- }
- DestroyTestTerrainSystem();
- }
- } // namespace UnitTest
|