Class TileLayerCache
Least-recently-used map from packed tile keys to Texture2DArray layer indices, with a pinned set that eviction never takes. The pipeline uses this; most apps never call it.
public sealed class TileLayerCache
- Inheritance
-
objectTileLayerCache
Remarks
Not thread-safe; call from the main thread. Zero allocation per frame after construction; Grow(int) allocates on that call only. TryGetLayer(long, out int) moves the key to most-recent; use Peek(long, out int) when sampling must not change eviction order.
Constructors
TileLayerCache(int)
Builds an empty cache of capacity layers.
public TileLayerCache(int capacity)
Parameters
capacityintLayer count; must be positive.
Properties
Capacity
Layer count this cache was constructed or grown to.
public int Capacity { get; }
Property Value
PinnedKeyCount
Number of keys in the pinned set. May exceed Capacity.
public int PinnedKeyCount { get; }
Property Value
UsedLayers
Highest layer index that has ever been handed out, plus one.
public int UsedLayers { get; }
Property Value
Methods
FittingFloorPinZoom(int, int, int, int)
Deepest full-world floor that fits in capacity
layers after leaving room for the visible footprint and reserve.
public static int FittingFloorPinZoom(int capacity, int reserve, int worstCaseFootprint, int requestedMaxZoom)
Parameters
capacityintTexture array layer count.
reserveintLayers the pin budget always keeps evictable.
worstCaseFootprintintWorst-case visible footprint size, in tiles; used with
capacityandreserveto leave room.requestedMaxZoomintConfigured floor depth ceiling, integer tile zoom.
Returns
- int
Integer tile zoom of the floor to pin, or -1 when not even zoom 0 fits. Never exceeds
requestedMaxZoom.
FloorPinLayerRequirement(int, int, int)
Smallest layer capacity at which FittingFloorPinZoom(int, int, int, int) returns the full requested floor depth.
public static int FloorPinLayerRequirement(int pinMaxZoom, int reserve, int worstCaseFootprint)
Parameters
pinMaxZoomintRequested floor depth, integer tile zoom; negative requests no floor.
reserveintLayers the pin budget always keeps evictable.
worstCaseFootprintintWorst-case visible footprint size, in tiles; used with floor tile count and
reserveto size layer capacity.
Returns
- int
Required layer count, or 0 when no floor is requested.
FloorTileCount(int)
Number of tiles in a full-world floor from zoom 0 through
floorMaxZoom inclusive.
public static long FloorTileCount(int floorMaxZoom)
Parameters
floorMaxZoomintDeepest integer tile zoom in the floor; negative means no floor and returns 0. Values above 30 throw rather than wrap.
Returns
- long
Sum of 4^z for z in 0..floorMaxZoom, or 0 when the depth is negative.
Grow(int)
Extends capacity in place. Existing layer indices, keys, and LRU order do not move.
public void Grow(int newCapacity)
Parameters
Remarks
Shrinking is not supported. New slots are unused until TryInsert(long, out int, out long, out bool) hands them out, before any resident layer is evicted.
Insert(long)
Assigns a layer to key, evicting the least-recent
unpinned layer when full.
public int Insert(long key)
Parameters
keylongPacked tile key.
Returns
- int
Layer index assigned to the key.
Remarks
Throws when every layer is pinned. Prefer TryInsert(long, out int, out long, out bool) on the frame path.
Insert(long, out long, out bool)
Assigns a layer to key, reporting any eviction.
public int Insert(long key, out long evictedKey, out bool evicted)
Parameters
keylongPacked tile key.
evictedKeylongPacked key that left the cache when
evictedis true.evictedboolTrue when an unpinned layer was taken to make room.
Returns
- int
Layer index assigned to the key.
Remarks
Throws when every layer is pinned. Prefer TryInsert(long, out int, out long, out bool) on the frame path.
IsPinned(int)
True when layer is currently pinned against eviction.
public bool IsPinned(int layer)
Parameters
Returns
- bool
True when that layer's resident key is in the pinned set.
Peek(long, out int)
Looks up key without changing eviction order.
public bool Peek(long key, out int layer)
Parameters
Returns
- bool
True when the key is resident.
Remove(long)
Forgets key and returns its layer to the free pool.
public bool Remove(long key)
Parameters
keylongPacked tile key.
Returns
- bool
True when the key was resident and the reservation was rolled back.
Remarks
Use this when TryInsert(long, out int, out long, out bool) reserved a layer and the GPU write then failed, so the cache does not serve uninitialized memory.
SetPinned(long[], int)
Replaces the pinned set. Applies to layers already cached and to keys inserted later while still pinned.
public void SetPinned(long[] newPinnedKeys, int count)
Parameters
newPinnedKeyslong[]Packed tile keys that eviction must not take.
countintNumber of keys at the front of
newPinnedKeysto apply.
TryGetLayer(long, out int)
Looks up key and marks it most-recently used.
public bool TryGetLayer(long key, out int layer)
Parameters
Returns
- bool
True when the key is resident.
TryInsert(long, out int, out long, out bool)
Assigns a layer to key without throwing.
public bool TryInsert(long key, out int layer, out long evictedKey, out bool evicted)
Parameters
keylongPacked tile key.
layerintAssigned layer index on success.
evictedKeylongPacked key that left the cache when
evictedis true.evictedboolTrue when an unpinned layer was taken to make room.
Returns
- bool
False when every layer is pinned, so this upload should be skipped.