DynamicAtlasAllocator .java

net.minecraft.client.gui.render.DynamicAtlasAllocator (class): members: reclaimSpaceFor, hasSpaceForAll, freeSlotCount, getOrAllocate, usedSlotByKey, Slot.

net.minecraft.client.gui.renderclass145 linesclient3.9 KB

API Surface (8)

boolean reclaimSpaceFor(final Set<K> keys)
void endFrame()
boolean hasSpaceForAll(final Set<K> keys)
int freeSlotCount()
Set<K> usedSlotKeys()
int x()
int y()
DynamicAtlasAllocator.SlotState state()

Fields & Constants (4)

width
slots
usedSlotByKey
freeSlots

Full Source

DynamicAtlasAllocator.java145 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.render;
2
3
import com.google.common.annotations.VisibleForTesting;
4
import com.google.common.collect.Sets;
5
import java.util.ArrayList;
6
import java.util.BitSet;
7
import java.util.Collections;
8
import java.util.HashMap;
9
import java.util.List;
10
import java.util.Map;
11
import java.util.Set;
12
import java.util.function.BiPredicate;
13
import net.fabricmc.api.EnvType;
14
import net.fabricmc.api.Environment;
15
import org.apache.commons.lang3.mutable.MutableInt;
16
import org.jspecify.annotations.Nullable;
17
18
@Environment(EnvType.CLIENT)
19
public class DynamicAtlasAllocator<K> {
20
	private final int width;
21
	private final List<DynamicAtlasAllocator.Slot> slots;
22
	private final Map<K, DynamicAtlasAllocator.Slot> usedSlotByKey = new HashMap<>();
23
	private final BitSet freeSlots;
24
25
	public DynamicAtlasAllocator(final int width, final int height) {
26
		this.width = width;
27
		int size = width * height;
28
		this.slots = new ArrayList<>(size);
29
30
		for (int y = 0; y < height; y++) {
31
			for (int x = 0; x < width; x++) {
32
				this.slots.add(new DynamicAtlasAllocator.Slot(x, y));
33
			}
34
		}
35
36
		this.freeSlots = new BitSet(size);
37
		this.freeSlots.set(0, size);
38
	}
39
40
	public boolean reclaimSpaceFor(final Set<K> keys) {
41
		int preexistingKeyCount = Sets.intersection(this.usedSlotByKey.keySet(), keys).size();
42
		if (preexistingKeyCount == keys.size()) {
43
			return true;
44
		}
45
46
		MutableInt needSpaceFor = new MutableInt(keys.size() - preexistingKeyCount);
47
		this.freeSlotIf((key, var3x) -> {
48
			if (needSpaceFor.intValue() != 0 && !keys.contains(key)) {
49
				needSpaceFor.decrement();
50
				return true;
51
			} else {
52
				return false;
53
			}
54
		});
55
		return needSpaceFor.intValue() == 0;
56
	}
57
58
	public void endFrame() {
59
		this.freeSlotIf((var0, slot) -> slot.discardAfterFrame);
60
	}
61
62
	private void freeSlotIf(final BiPredicate<K, DynamicAtlasAllocator.Slot> predicate) {
63
		this.usedSlotByKey.entrySet().removeIf(entry -> {
64
			if (!predicate.test(entry.getKey(), entry.getValue())) {
65
				return false;
66
			}
67
68
			DynamicAtlasAllocator.Slot slot = entry.getValue();
69
			this.freeSlots.set(slot.x + slot.y * this.width);
70
			slot.discardAfterFrame = false;
71
			return true;
72
		});
73
	}
74
75
	public boolean hasSpaceForAll(final Set<K> keys) {
76
		Set<K> predictedUsedSlots = Sets.union(this.usedSlotByKey.keySet(), keys);
77
		return predictedUsedSlots.size() <= this.slots.size();
78
	}
79
80
	public DynamicAtlasAllocator.@Nullable Slot getOrAllocate(final K key, final boolean discardAfterFrame) {
81
		DynamicAtlasAllocator.Slot usedSlot = this.usedSlotByKey.get(key);
82
		if (usedSlot != null) {
83
			usedSlot.discardAfterFrame |= discardAfterFrame;
84
			usedSlot.externalState = DynamicAtlasAllocator.SlotState.READY;
85
			return usedSlot;
86
		}
87
88
		int freeSlotIndex = this.freeSlots.nextSetBit(0);
89
		if (freeSlotIndex == -1) {
90
			return null;
91
		}
92
93
		DynamicAtlasAllocator.Slot freeSlot = this.slots.get(freeSlotIndex);
94
		freeSlot.externalState = freeSlot.fresh ? DynamicAtlasAllocator.SlotState.EMPTY : DynamicAtlasAllocator.SlotState.STALE;
95
		freeSlot.fresh = false;
96
		freeSlot.discardAfterFrame = discardAfterFrame;
97
		this.usedSlotByKey.put(key, freeSlot);
98
		this.freeSlots.clear(freeSlotIndex);
99
		return freeSlot;
100
	}
101
102
	@VisibleForTesting
103
	public int freeSlotCount() {
104
		return this.slots.size() - this.usedSlotByKey.size();
105
	}
106
107
	@VisibleForTesting
108
	public Set<K> usedSlotKeys() {
109
		return Collections.unmodifiableSet(this.usedSlotByKey.keySet());
110
	}
111
112
	@Environment(EnvType.CLIENT)
113
	public static class Slot {
114
		private final int x;
115
		private final int y;
116
		private boolean fresh = true;
117
		private boolean discardAfterFrame;
118
		private DynamicAtlasAllocator.SlotState externalState = DynamicAtlasAllocator.SlotState.EMPTY;
119
120
		private Slot(final int x, final int y) {
121
			this.x = x;
122
			this.y = y;
123
		}
124
125
		public int x() {
126
			return this.x;
127
		}
128
129
		public int y() {
130
			return this.y;
131
		}
132
133
		public DynamicAtlasAllocator.SlotState state() {
134
			return this.externalState;
135
		}
136
	}
137
138
	@Environment(EnvType.CLIENT)
139
	public enum SlotState {
140
		EMPTY,
141
		STALE,
142
		READY;
143
	}
144
}
145

Decompiled Minecraft 26.3 source of DynamicAtlasAllocator (net/minecraft/client/gui/render/DynamicAtlasAllocator.java) with Mojang official mappings, stored verbatim on the site.