TileAllocator.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // Copyright (C) 2009-2020, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <anki/renderer/TileAllocator.h>
  6. namespace anki
  7. {
  8. class TileAllocator::Tile
  9. {
  10. public:
  11. Timestamp m_lightTimestamp = 0; ///< The last timestamp the light got updated
  12. Timestamp m_lastUsedTimestamp = 0; ///< The last timestamp this tile was used
  13. U64 m_lightUuid = 0;
  14. U32 m_lightDrawcallCount = 0;
  15. Array<U32, 4> m_viewport = {};
  16. Array<U32, 4> m_subTiles = {MAX_U32, MAX_U32, MAX_U32, MAX_U32};
  17. U32 m_superTile = MAX_U32;
  18. U8 m_lightLod = 0;
  19. U8 m_lightFace = 0;
  20. };
  21. class TileAllocator::HashMapKey
  22. {
  23. public:
  24. U64 m_lightUuid;
  25. U64 m_face;
  26. U64 computeHash() const
  27. {
  28. return anki::computeHash(this, sizeof(*this), 693);
  29. }
  30. };
  31. TileAllocator::~TileAllocator()
  32. {
  33. m_lightInfoToTileIdx.destroy(m_alloc);
  34. m_allTiles.destroy(m_alloc);
  35. m_lodFirstTileIndex.destroy(m_alloc);
  36. }
  37. void TileAllocator::init(HeapAllocator<U8> alloc, U32 tileCountX, U32 tileCountY, U32 lodCount, Bool enableCaching)
  38. {
  39. // Preconditions
  40. ANKI_ASSERT(tileCountX > 0);
  41. ANKI_ASSERT(tileCountY > 0);
  42. ANKI_ASSERT(lodCount > 0);
  43. // Store some stuff
  44. m_tileCountX = U16(tileCountX);
  45. m_tileCountY = U16(tileCountY);
  46. m_lodCount = U8(lodCount);
  47. m_alloc = alloc;
  48. m_cachingEnabled = enableCaching;
  49. m_lodFirstTileIndex.create(m_alloc, lodCount + 1);
  50. // Create the tile array & index ranges
  51. U32 tileCount = 0;
  52. for(U32 lod = 0; lod < lodCount; ++lod)
  53. {
  54. const U32 lodTileCountX = tileCountX >> lod;
  55. const U32 lodTileCountY = tileCountY >> lod;
  56. ANKI_ASSERT((lodTileCountX << lod) == tileCountX && "Every LOD should be power of 2 of its parent LOD");
  57. ANKI_ASSERT((lodTileCountY << lod) == tileCountY && "Every LOD should be power of 2 of its parent LOD");
  58. m_lodFirstTileIndex[lod] = tileCount;
  59. tileCount += lodTileCountX * lodTileCountY;
  60. }
  61. ANKI_ASSERT(tileCount >= tileCountX * tileCountY);
  62. m_allTiles.create(m_alloc, tileCount);
  63. m_lodFirstTileIndex[lodCount] = tileCount - 1;
  64. // Init the tiles
  65. U32 tileIdx = 0;
  66. for(U32 lod = 0; lod < lodCount; ++lod)
  67. {
  68. const U32 lodTileCountX = tileCountX >> lod;
  69. const U32 lodTileCountY = tileCountY >> lod;
  70. for(U32 y = 0; y < lodTileCountY; ++y)
  71. {
  72. for(U32 x = 0; x < lodTileCountX; ++x)
  73. {
  74. ANKI_ASSERT(tileIdx >= m_lodFirstTileIndex[lod] && tileIdx <= m_lodFirstTileIndex[lod + 1]);
  75. Tile& tile = m_allTiles[tileIdx];
  76. tile.m_viewport[0] = x << lod;
  77. tile.m_viewport[1] = y << lod;
  78. tile.m_viewport[2] = 1 << lod;
  79. tile.m_viewport[3] = 1 << lod;
  80. if(lod > 0)
  81. {
  82. // Has sub tiles
  83. for(U32 j = 0; j < 2; ++j)
  84. {
  85. for(U32 i = 0; i < 2; ++i)
  86. {
  87. const U32 subTileIdx = translateTileIdx((x << 1) + i, (y << 1) + j, lod - 1);
  88. m_allTiles[subTileIdx].m_superTile = tileIdx;
  89. tile.m_subTiles[j * 2 + i] = subTileIdx;
  90. }
  91. }
  92. }
  93. else
  94. {
  95. // No sub-tiles
  96. }
  97. ++tileIdx;
  98. }
  99. }
  100. }
  101. }
  102. void TileAllocator::updateSubTiles(const Tile& updateFrom)
  103. {
  104. if(updateFrom.m_subTiles[0] == MAX_U32)
  105. {
  106. return;
  107. }
  108. for(U32 idx : updateFrom.m_subTiles)
  109. {
  110. m_allTiles[idx].m_lightTimestamp = updateFrom.m_lightTimestamp;
  111. m_allTiles[idx].m_lastUsedTimestamp = updateFrom.m_lastUsedTimestamp;
  112. m_allTiles[idx].m_lightUuid = updateFrom.m_lightUuid;
  113. m_allTiles[idx].m_lightDrawcallCount = updateFrom.m_lightDrawcallCount;
  114. m_allTiles[idx].m_lightLod = updateFrom.m_lightLod;
  115. m_allTiles[idx].m_lightFace = updateFrom.m_lightFace;
  116. updateSubTiles(m_allTiles[idx]);
  117. }
  118. }
  119. void TileAllocator::updateSuperTiles(const Tile& updateFrom)
  120. {
  121. if(updateFrom.m_superTile != MAX_U32)
  122. {
  123. m_allTiles[updateFrom.m_superTile].m_lightUuid = 0;
  124. m_allTiles[updateFrom.m_superTile].m_lastUsedTimestamp = updateFrom.m_lastUsedTimestamp;
  125. updateSuperTiles(m_allTiles[updateFrom.m_superTile]);
  126. }
  127. }
  128. Bool TileAllocator::searchTileRecursively(U32 crntTileIdx, U32 crntTileLod, U32 allocationLod, Timestamp crntTimestamp,
  129. U32& emptyTileIdx, U32& toKickTileIdx,
  130. Timestamp& tileToKickMinTimestamp) const
  131. {
  132. const Tile& tile = m_allTiles[crntTileIdx];
  133. if(crntTileLod == allocationLod)
  134. {
  135. // We may have a candidate
  136. const Bool done =
  137. evaluateCandidate(crntTileIdx, crntTimestamp, emptyTileIdx, toKickTileIdx, tileToKickMinTimestamp);
  138. if(done)
  139. {
  140. return true;
  141. }
  142. }
  143. else if(tile.m_subTiles[0] != MAX_U32)
  144. {
  145. // Move down the hierarchy
  146. ANKI_ASSERT(allocationLod < crntTileLod);
  147. for(const U32 idx : tile.m_subTiles)
  148. {
  149. const Bool done = searchTileRecursively(idx, crntTileLod >> 1, allocationLod, crntTimestamp, emptyTileIdx,
  150. toKickTileIdx, tileToKickMinTimestamp);
  151. if(done)
  152. {
  153. return true;
  154. }
  155. }
  156. }
  157. return false;
  158. }
  159. Bool TileAllocator::evaluateCandidate(U32 tileIdx, Timestamp crntTimestamp, U32& emptyTileIdx, U32& toKickTileIdx,
  160. Timestamp& tileToKickMinTimestamp) const
  161. {
  162. const Tile& tile = m_allTiles[tileIdx];
  163. if(m_cachingEnabled)
  164. {
  165. if(tile.m_lastUsedTimestamp == 0)
  166. {
  167. // Found empty
  168. emptyTileIdx = tileIdx;
  169. return true;
  170. }
  171. else if(tile.m_lastUsedTimestamp != crntTimestamp && tile.m_lastUsedTimestamp < tileToKickMinTimestamp)
  172. {
  173. // Found one with low timestamp
  174. toKickTileIdx = tileIdx;
  175. tileToKickMinTimestamp = tile.m_lightTimestamp;
  176. }
  177. }
  178. else
  179. {
  180. if(tile.m_lastUsedTimestamp != crntTimestamp)
  181. {
  182. emptyTileIdx = tileIdx;
  183. return true;
  184. }
  185. }
  186. return false;
  187. }
  188. TileAllocatorResult TileAllocator::allocate(Timestamp crntTimestamp, Timestamp lightTimestamp, U64 lightUuid,
  189. U32 lightFace, U32 drawcallCount, U32 lod, Array<U32, 4>& tileViewport)
  190. {
  191. // Preconditions
  192. ANKI_ASSERT(crntTimestamp > 0);
  193. ANKI_ASSERT(lightTimestamp > 0);
  194. ANKI_ASSERT(lightTimestamp <= crntTimestamp);
  195. ANKI_ASSERT(lightUuid != 0);
  196. ANKI_ASSERT(lightFace < 6);
  197. ANKI_ASSERT(lod < m_lodCount);
  198. // 1) Search if it's already cached
  199. HashMapKey key;
  200. if(m_cachingEnabled)
  201. {
  202. key.m_lightUuid = lightUuid;
  203. key.m_face = lightFace;
  204. auto it = m_lightInfoToTileIdx.find(key);
  205. if(it != m_lightInfoToTileIdx.getEnd())
  206. {
  207. Tile& tile = m_allTiles[*it];
  208. if(tile.m_lightUuid != lightUuid || tile.m_lightLod != lod || tile.m_lightFace != lightFace)
  209. {
  210. // Cache entry is wrong, remove it
  211. m_lightInfoToTileIdx.erase(m_alloc, it);
  212. }
  213. else
  214. {
  215. // Same light & lod & face, found the cache entry.
  216. ANKI_ASSERT(tile.m_lastUsedTimestamp != crntTimestamp
  217. && "Trying to allocate the same thing twice in this timestamp?");
  218. ANKI_ASSERT(tile.m_lightUuid == lightUuid && tile.m_lightLod == lod && tile.m_lightFace == lightFace);
  219. tileViewport = {tile.m_viewport[0], tile.m_viewport[1], tile.m_viewport[2], tile.m_viewport[3]};
  220. const Bool needsReRendering =
  221. tile.m_lightDrawcallCount != drawcallCount || tile.m_lightTimestamp != lightTimestamp;
  222. tile.m_lightTimestamp = lightTimestamp;
  223. tile.m_lastUsedTimestamp = crntTimestamp;
  224. tile.m_lightDrawcallCount = drawcallCount;
  225. updateTileHierarchy(tile);
  226. return (needsReRendering) ? TileAllocatorResult::ALLOCATION_SUCCEEDED : TileAllocatorResult::CACHED;
  227. }
  228. }
  229. }
  230. // Start searching for a suitable tile. Do a hieratchical search to end up with better locality and not better
  231. // utilization of the atlas' space
  232. U32 emptyTileIdx = MAX_U32;
  233. U32 toKickTileIdx = MAX_U32;
  234. Timestamp tileToKickMinTimestamp = MAX_TIMESTAMP;
  235. const U32 maxLod = m_lodCount - 1;
  236. if(lod == maxLod)
  237. {
  238. // This search is simple, iterate the tiles of the max LOD
  239. for(U32 tileIdx = m_lodFirstTileIndex[maxLod]; tileIdx <= m_lodFirstTileIndex[maxLod + 1]; ++tileIdx)
  240. {
  241. const Bool done =
  242. evaluateCandidate(tileIdx, crntTimestamp, emptyTileIdx, toKickTileIdx, tileToKickMinTimestamp);
  243. if(done)
  244. {
  245. break;
  246. }
  247. }
  248. }
  249. else
  250. {
  251. // Need to do a recursive search
  252. for(U32 tileIdx = m_lodFirstTileIndex[maxLod]; tileIdx <= m_lodFirstTileIndex[maxLod + 1]; ++tileIdx)
  253. {
  254. const Bool done = searchTileRecursively(tileIdx, maxLod, lod, crntTimestamp, emptyTileIdx, toKickTileIdx,
  255. tileToKickMinTimestamp);
  256. if(done)
  257. {
  258. break;
  259. }
  260. }
  261. }
  262. U32 allocatedTileIdx;
  263. if(emptyTileIdx != MAX_U32)
  264. {
  265. allocatedTileIdx = emptyTileIdx;
  266. }
  267. else if(toKickTileIdx != MAX_U32)
  268. {
  269. allocatedTileIdx = toKickTileIdx;
  270. }
  271. else
  272. {
  273. // Out of tiles
  274. return TileAllocatorResult::ALLOCATION_FAILED;
  275. }
  276. // Allocation succedded, need to do some bookkeeping
  277. // Mark the allocated tile
  278. Tile& allocatedTile = m_allTiles[allocatedTileIdx];
  279. allocatedTile.m_lightTimestamp = lightTimestamp;
  280. allocatedTile.m_lastUsedTimestamp = crntTimestamp;
  281. allocatedTile.m_lightUuid = lightUuid;
  282. allocatedTile.m_lightDrawcallCount = drawcallCount;
  283. allocatedTile.m_lightLod = U8(lod);
  284. allocatedTile.m_lightFace = U8(lightFace);
  285. updateTileHierarchy(allocatedTile);
  286. // Update the cache
  287. if(m_cachingEnabled)
  288. {
  289. m_lightInfoToTileIdx.emplace(m_alloc, key, allocatedTileIdx);
  290. }
  291. // Return
  292. tileViewport = {allocatedTile.m_viewport[0], allocatedTile.m_viewport[1], allocatedTile.m_viewport[2],
  293. allocatedTile.m_viewport[3]};
  294. return TileAllocatorResult::ALLOCATION_SUCCEEDED;
  295. }
  296. void TileAllocator::invalidateCache(U64 lightUuid, U32 lightFace)
  297. {
  298. ANKI_ASSERT(m_cachingEnabled);
  299. ANKI_ASSERT(lightUuid > 0);
  300. HashMapKey key;
  301. key.m_lightUuid = lightUuid;
  302. key.m_face = lightFace;
  303. auto it = m_lightInfoToTileIdx.find(key);
  304. if(it != m_lightInfoToTileIdx.getEnd())
  305. {
  306. m_lightInfoToTileIdx.erase(m_alloc, it);
  307. }
  308. }
  309. } // end namespace anki