PageTileAllocatorTests.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #include "RHITestFixture.h"
  9. #include <AzCore/UnitTest/UnitTest.h>
  10. #include <Atom/RHI/PageTileAllocator.h>
  11. #include <AzCore/Math/Random.h>
  12. namespace UnitTest
  13. {
  14. using namespace AZ;
  15. class PageTileAllocatorTest
  16. : public RHITestFixture
  17. {
  18. public:
  19. PageTileAllocatorTest()
  20. : RHITestFixture()
  21. {}
  22. // Return true if there is no overlapping between all the input tile groups
  23. bool ValidateTilesNotOverlap(const AZStd::vector<RHI::PageTileSpan>& tilesList)
  24. {
  25. AZStd::set<RHI::PageTileSpan, RHI::PageTileSpan::Compare> sortedTilesList;
  26. for (const RHI::PageTileSpan& tiles : tilesList)
  27. {
  28. sortedTilesList.insert(tiles);
  29. }
  30. uint32_t lastTile = 0;
  31. for (const RHI::PageTileSpan& tiles1 : sortedTilesList)
  32. {
  33. if(lastTile > tiles1.m_offset)
  34. {
  35. return false;
  36. }
  37. uint32_t newLastTile = tiles1.m_offset + tiles1.m_tileCount;
  38. if (lastTile < newLastTile)
  39. {
  40. lastTile = newLastTile;
  41. }
  42. else
  43. {
  44. return false;
  45. }
  46. }
  47. return true;
  48. }
  49. uint32_t GetTileCount(const AZStd::vector<RHI::PageTileSpan>& tilesList)
  50. {
  51. uint32_t tileCount = 0;
  52. for (const auto& tiles : tilesList)
  53. {
  54. tileCount += tiles.m_tileCount;
  55. }
  56. return tileCount;
  57. }
  58. };
  59. TEST_F(PageTileAllocatorTest, SingleAllocation_Success)
  60. {
  61. RHI::PageTileAllocator allocator;
  62. const uint32_t pageTileCount = 256;
  63. allocator.Init(pageTileCount);
  64. ASSERT_TRUE(allocator.GetFreeTileCount() == pageTileCount);
  65. ASSERT_TRUE(allocator.GetTotalTileCount() == pageTileCount);
  66. ASSERT_TRUE(allocator.GetUsedTileCount() == 0);
  67. uint32_t allocated = 0;
  68. uint32_t requested = 24;
  69. AZStd::vector<RHI::PageTileSpan> tilesList = allocator.TryAllocate(requested, allocated);
  70. ASSERT_TRUE(allocated == requested);
  71. ASSERT_TRUE(tilesList.size() == 1);
  72. ASSERT_TRUE(tilesList.front().m_tileCount == requested);
  73. ASSERT_FALSE(allocator.IsPageFree());
  74. ASSERT_TRUE(allocator.GetFreeTileCount() == pageTileCount - requested);
  75. ASSERT_TRUE(allocator.GetUsedTileCount() == requested);
  76. allocator.DeAllocate(tilesList);
  77. ASSERT_TRUE(allocator.GetFreeTileCount() == pageTileCount);
  78. ASSERT_TRUE(allocator.GetUsedTileCount() == 0);
  79. ASSERT_TRUE(allocator.IsPageFree());
  80. }
  81. TEST_F(PageTileAllocatorTest, SingleOutOfRangeAllocation_Failed)
  82. {
  83. RHI::PageTileAllocator allocator;
  84. const uint32_t pageTileCount = 20;
  85. allocator.Init(pageTileCount);
  86. uint32_t allocated = 0;
  87. uint32_t requested = 24;
  88. AZStd::vector<RHI::PageTileSpan> tilesList = allocator.TryAllocate(requested, allocated);
  89. ASSERT_TRUE(allocated == pageTileCount);
  90. ASSERT_TRUE(tilesList.front().m_tileCount == allocated);
  91. ASSERT_FALSE(allocator.IsPageFree());
  92. ASSERT_TRUE(allocator.GetFreeTileCount() == 0);
  93. ASSERT_TRUE(allocator.GetUsedTileCount() == pageTileCount);
  94. allocator.DeAllocate(tilesList);
  95. ASSERT_TRUE(allocator.GetFreeTileCount() == pageTileCount);
  96. ASSERT_TRUE(allocator.GetUsedTileCount() == 0);
  97. ASSERT_TRUE(allocator.IsPageFree());
  98. }
  99. TEST_F(PageTileAllocatorTest, RandomAllocationDeallocation_Success)
  100. {
  101. RHI::PageTileAllocator allocator;
  102. const uint32_t pageTileCount = 30;
  103. allocator.Init(pageTileCount);
  104. uint32_t allocationCount = 100;
  105. // always uses the same seed to get consistent test data
  106. AZ::SimpleLcgRandom random(1234);
  107. AZStd::vector<RHI::PageTileSpan> allocatedTilesList;
  108. while (allocationCount-- != 0)
  109. {
  110. // 51% chance of doing an add. Biased towards adds so we fill up the allocator.
  111. if ((random.GetRandom() % 100) <= 51 || allocatedTilesList.size() == 0)
  112. {
  113. uint32_t requested = random.GetRandom() % (pageTileCount + 10);
  114. uint32_t allocated = 0;
  115. AZStd::vector<RHI::PageTileSpan> tilesList = allocator.TryAllocate(requested, allocated);
  116. allocatedTilesList.insert(AZStd::end(allocatedTilesList), AZStd::begin(tilesList), AZStd::end(tilesList));
  117. ASSERT_TRUE(allocated <= requested);
  118. ASSERT_TRUE(ValidateTilesNotOverlap(allocatedTilesList));
  119. ASSERT_TRUE(GetTileCount(allocatedTilesList) == allocator.GetUsedTileCount());
  120. }
  121. else
  122. {
  123. // select some tile groups from the list
  124. uint32_t count = random.GetRandom() % allocatedTilesList.size() + 1;
  125. AZStd::vector<RHI::PageTileSpan> tilesToBeRemoved;
  126. for (uint32_t i = 0; i < count; i++)
  127. {
  128. uint32_t position = (random.GetRandom() % allocatedTilesList.size());
  129. tilesToBeRemoved.push_back(allocatedTilesList[position]);
  130. allocatedTilesList.erase(allocatedTilesList.begin() + position);
  131. }
  132. allocator.DeAllocate(tilesToBeRemoved);
  133. ASSERT_TRUE(ValidateTilesNotOverlap(allocator.GetFreeList()));
  134. ASSERT_TRUE(GetTileCount(allocatedTilesList) == allocator.GetUsedTileCount());
  135. }
  136. }
  137. allocator.DeAllocate(allocatedTilesList);
  138. ASSERT_TRUE(allocator.GetFreeList().size() == 1);
  139. ASSERT_TRUE(allocator.GetFreeTileCount() == pageTileCount);
  140. ASSERT_TRUE(allocator.GetUsedTileCount() == 0);
  141. }
  142. }