TileAllocator.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Renderer/Common.h>
  7. namespace anki
  8. {
  9. /// @addtogroup renderer
  10. /// @{
  11. /// The result of a tile allocation.
  12. enum class TileAllocatorResult : U32
  13. {
  14. CACHED, ///< The tile is cached. No need to re-render it.
  15. ALLOCATION_FAILED, ///< No more available tiles.
  16. ALLOCATION_SUCCEEDED ///< Allocation succeded but the tile needs update.
  17. };
  18. /// Allocates tiles out of a tilemap suitable for shadow mapping.
  19. class TileAllocator : public NonCopyable
  20. {
  21. public:
  22. ~TileAllocator();
  23. /// Initialize the allocator.
  24. void init(HeapAllocator<U8> alloc, U32 tileCountX, U32 tileCountY, U32 lodCount, Bool enableCaching);
  25. /// Allocate some tiles.
  26. ANKI_USE_RESULT TileAllocatorResult allocate(Timestamp crntTimestamp, Timestamp lightTimestamp, U64 lightUuid,
  27. U32 lightFace, U32 drawcallCount, U32 lod,
  28. Array<U32, 4>& tileViewport);
  29. /// Remove an light from the cache.
  30. void invalidateCache(U64 lightUuid, U32 lightFace);
  31. private:
  32. class Tile;
  33. /// A HashMap key.
  34. class HashMapKey;
  35. HeapAllocator<U8> m_alloc;
  36. DynamicArray<Tile> m_allTiles;
  37. DynamicArray<U32> m_lodFirstTileIndex;
  38. HashMap<HashMapKey, U32> m_lightInfoToTileIdx;
  39. U16 m_tileCountX = 0; ///< Tile count for LOD 0
  40. U16 m_tileCountY = 0; ///< Tile count for LOD 0
  41. U8 m_lodCount = 0;
  42. Bool m_cachingEnabled = false;
  43. U32 translateTileIdx(U32 x, U32 y, U32 lod) const
  44. {
  45. const U32 lodWidth = m_tileCountX >> lod;
  46. const U32 idx = y * lodWidth + x + m_lodFirstTileIndex[lod];
  47. ANKI_ASSERT(idx < m_allTiles.getSize());
  48. return idx;
  49. }
  50. void updateSubTiles(const Tile& updateFrom);
  51. void updateSuperTiles(const Tile& updateFrom);
  52. /// Given a tile move the hierarchy up and down to update the hierarchy this tile belongs to.
  53. void updateTileHierarchy(const Tile& updateFrom)
  54. {
  55. updateSubTiles(updateFrom);
  56. updateSuperTiles(updateFrom);
  57. }
  58. /// Search for a tile recursively.
  59. Bool searchTileRecursively(U32 crntTileIdx, U32 crntTileLod, U32 allocationLod, Timestamp crntTimestamp,
  60. U32& emptyTileIdx, U32& toKickTileIdx, Timestamp& tileToKickMinTimestamp) const;
  61. Bool evaluateCandidate(U32 tileIdx, Timestamp crntTimestamp, U32& emptyTileIdx, U32& toKickTileIdx,
  62. Timestamp& tileToKickMinTimestamp) const;
  63. };
  64. /// @}
  65. } // end namespace anki