| 1 | package net.minecraft.client.resources.model.sprite;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.util.ArrayList;
|
| 5 | import java.util.HashMap;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Map;
|
| 8 | import java.util.Objects;
|
| 9 | import java.util.Set;
|
| 10 | import java.util.concurrent.CompletableFuture;
|
| 11 | import java.util.concurrent.Executor;
|
| 12 | import java.util.function.BiConsumer;
|
| 13 | import net.fabricmc.api.EnvType;
|
| 14 | import net.fabricmc.api.Environment;
|
| 15 | import net.minecraft.client.renderer.Sheets;
|
| 16 | import net.minecraft.client.renderer.texture.MissingTextureAtlasSprite;
|
| 17 | import net.minecraft.client.renderer.texture.SpriteLoader;
|
| 18 | import net.minecraft.client.renderer.texture.TextureAtlas;
|
| 19 | import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
| 20 | import net.minecraft.client.renderer.texture.TextureManager;
|
| 21 | import net.minecraft.client.resources.metadata.gui.GuiMetadataSection;
|
| 22 | import net.minecraft.data.AtlasIds;
|
| 23 | import net.minecraft.resources.Identifier;
|
| 24 | import net.minecraft.server.packs.metadata.MetadataSectionType;
|
| 25 | import net.minecraft.server.packs.resources.PreparableReloadListener;
|
| 26 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 27 | import net.minecraft.server.packs.resources.PreparableReloadListener.PreparationBarrier;
|
| 28 | import net.minecraft.server.packs.resources.PreparableReloadListener.SharedState;
|
| 29 | import net.minecraft.server.packs.resources.PreparableReloadListener.StateKey;
|
| 30 | import org.slf4j.Logger;
|
| 31 |
|
| 32 | @Environment(EnvType.CLIENT)
|
| 33 | public class AtlasManager implements AutoCloseable, PreparableReloadListener, SpriteGetter {
|
| 34 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 35 | private static final List<AtlasManager.AtlasConfig> KNOWN_ATLASES = List.of(
|
| 36 | new AtlasManager.AtlasConfig(Sheets.BANNER_SHEET, AtlasIds.BANNER_PATTERNS, false),
|
| 37 | new AtlasManager.AtlasConfig(TextureAtlas.LOCATION_BLOCKS, AtlasIds.BLOCKS, true),
|
| 38 | new AtlasManager.AtlasConfig(TextureAtlas.LOCATION_ITEMS, AtlasIds.ITEMS, false),
|
| 39 | new AtlasManager.AtlasConfig(Sheets.CHEST_SHEET, AtlasIds.CHESTS, false),
|
| 40 | new AtlasManager.AtlasConfig(Sheets.DECORATED_POT_SHEET, AtlasIds.DECORATED_POT, false),
|
| 41 | new AtlasManager.AtlasConfig(Sheets.GUI_SHEET, AtlasIds.GUI, false, Set.of(GuiMetadataSection.TYPE)),
|
| 42 | new AtlasManager.AtlasConfig(Sheets.MAP_DECORATIONS_SHEET, AtlasIds.MAP_DECORATIONS, false),
|
| 43 | new AtlasManager.AtlasConfig(Sheets.PAINTINGS_SHEET, AtlasIds.PAINTINGS, false),
|
| 44 | new AtlasManager.AtlasConfig(TextureAtlas.LOCATION_PARTICLES, AtlasIds.PARTICLES, false),
|
| 45 | new AtlasManager.AtlasConfig(Sheets.SHIELD_SHEET, AtlasIds.SHIELD_PATTERNS, false),
|
| 46 | new AtlasManager.AtlasConfig(Sheets.SHULKER_SHEET, AtlasIds.SHULKER_BOXES, false),
|
| 47 | new AtlasManager.AtlasConfig(Sheets.CELESTIAL_SHEET, AtlasIds.CELESTIALS, false)
|
| 48 | );
|
| 49 | public static final StateKey<AtlasManager.PendingStitchResults> PENDING_STITCH = new StateKey<>();
|
| 50 | private final Map<Identifier, AtlasManager.AtlasEntry> atlasByTexture = new HashMap<>();
|
| 51 | private final Map<Identifier, AtlasManager.AtlasEntry> atlasById = new HashMap<>();
|
| 52 | private Map<SpriteId, TextureAtlasSprite> spriteLookup = Map.of();
|
| 53 | private int maxMipmapLevels;
|
| 54 |
|
| 55 | public AtlasManager(final TextureManager textureManager, final int maxMipmapLevels) {
|
| 56 | for (AtlasManager.AtlasConfig info : KNOWN_ATLASES) {
|
| 57 | TextureAtlas atlasTexture = new TextureAtlas(info.textureId);
|
| 58 | textureManager.register(info.textureId, atlasTexture);
|
| 59 | AtlasManager.AtlasEntry atlasEntry = new AtlasManager.AtlasEntry(atlasTexture, info);
|
| 60 | this.atlasByTexture.put(info.textureId, atlasEntry);
|
| 61 | this.atlasById.put(info.definitionLocation, atlasEntry);
|
| 62 | }
|
| 63 |
|
| 64 | this.maxMipmapLevels = maxMipmapLevels;
|
| 65 | }
|
| 66 |
|
| 67 | public TextureAtlas getAtlasOrThrow(final Identifier atlasId) {
|
| 68 | AtlasManager.AtlasEntry atlasEntry = this.atlasById.get(atlasId);
|
| 69 | if (atlasEntry == null) {
|
| 70 | throw new IllegalArgumentException("Invalid atlas id: " + atlasId);
|
| 71 | } else {
|
| 72 | return atlasEntry.atlas();
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | public void forEach(final BiConsumer<Identifier, TextureAtlas> output) {
|
| 77 | this.atlasById.forEach((atlasId, entry) -> output.accept(atlasId, entry.atlas));
|
| 78 | }
|
| 79 |
|
| 80 | public void updateMaxMipLevel(final int maxMipmapLevels) {
|
| 81 | this.maxMipmapLevels = maxMipmapLevels;
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public void close() {
|
| 86 | this.spriteLookup = Map.of();
|
| 87 | this.atlasById.values().forEach(AtlasManager.AtlasEntry::close);
|
| 88 | this.atlasById.clear();
|
| 89 | this.atlasByTexture.clear();
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public TextureAtlasSprite get(final SpriteId sprite) {
|
| 94 | TextureAtlasSprite result = this.spriteLookup.get(sprite);
|
| 95 | if (result != null) {
|
| 96 | return result;
|
| 97 | } else {
|
| 98 | Identifier atlasTextureId = sprite.atlasLocation();
|
| 99 | AtlasManager.AtlasEntry atlasEntry = this.atlasByTexture.get(atlasTextureId);
|
| 100 | if (atlasEntry == null) {
|
| 101 | throw new IllegalArgumentException("Invalid atlas texture id: " + atlasTextureId);
|
| 102 | } else {
|
| 103 | return atlasEntry.atlas().missingSprite();
|
| 104 | }
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | public void prepareSharedState(final SharedState currentReload) {
|
| 110 | int atlasCount = this.atlasById.size();
|
| 111 | List<AtlasManager.PendingStitch> pendingStitches = new ArrayList<>(atlasCount);
|
| 112 | Map<Identifier, CompletableFuture<SpriteLoader.Preparations>> pendingStitchById = new HashMap<>(atlasCount);
|
| 113 | List<CompletableFuture<?>> readyForUploads = new ArrayList<>(atlasCount);
|
| 114 | this.atlasById.forEach((atlasId, atlasEntry) -> {
|
| 115 | CompletableFuture<SpriteLoader.Preparations> stitchingDone = new CompletableFuture<>();
|
| 116 | pendingStitchById.put(atlasId, stitchingDone);
|
| 117 | pendingStitches.add(new AtlasManager.PendingStitch(atlasEntry, stitchingDone));
|
| 118 | readyForUploads.add(stitchingDone.thenCompose(SpriteLoader.Preparations::readyForUpload));
|
| 119 | });
|
| 120 | CompletableFuture<?> allReadyForUploads = CompletableFuture.allOf(readyForUploads.toArray(CompletableFuture[]::new));
|
| 121 | currentReload.set(PENDING_STITCH, new AtlasManager.PendingStitchResults(pendingStitches, pendingStitchById, allReadyForUploads));
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | public CompletableFuture<Void> reload(
|
| 126 | final SharedState currentReload, final Executor taskExecutor, final PreparationBarrier preparationBarrier, final Executor reloadExecutor
|
| 127 | ) {
|
| 128 | AtlasManager.PendingStitchResults pendingStitches = currentReload.get(PENDING_STITCH);
|
| 129 | ResourceManager resourceManager = currentReload.resourceManager();
|
| 130 | pendingStitches.pendingStitches
|
| 131 | .forEach(pending -> pending.entry.scheduleLoad(resourceManager, taskExecutor, this.maxMipmapLevels).whenComplete((value, throwable) -> {
|
| 132 | if (value != null) {
|
| 133 | pending.preparations.complete(value);
|
| 134 | } else {
|
| 135 | pending.preparations.completeExceptionally(throwable);
|
| 136 | }
|
| 137 | }));
|
| 138 | return pendingStitches.allReadyToUpload
|
| 139 | .thenCompose(preparationBarrier::wait)
|
| 140 | .thenAcceptAsync(unused -> this.updateSpriteMaps(pendingStitches), reloadExecutor);
|
| 141 | }
|
| 142 |
|
| 143 | private void updateSpriteMaps(final AtlasManager.PendingStitchResults pendingStitches) {
|
| 144 | this.spriteLookup = pendingStitches.joinAndUpload();
|
| 145 | Map<Identifier, TextureAtlasSprite> globalSpriteLookup = new HashMap<>();
|
| 146 | this.spriteLookup
|
| 147 | .forEach(
|
| 148 | (id, sprite) -> {
|
| 149 | if (!id.texture().equals(MissingTextureAtlasSprite.getLocation())) {
|
| 150 | TextureAtlasSprite previous = globalSpriteLookup.putIfAbsent(id.texture(), sprite);
|
| 151 | if (previous != null) {
|
| 152 | LOGGER.warn(
|
| 153 | "Duplicate sprite {} from atlas {}, already defined in atlas {}. This will be rejected in a future version",
|
| 154 | id.texture(),
|
| 155 | id.atlasLocation(),
|
| 156 | previous.atlasLocation()
|
| 157 | );
|
| 158 | }
|
| 159 | }
|
| 160 | }
|
| 161 | );
|
| 162 | }
|
| 163 |
|
| 164 | @Environment(EnvType.CLIENT)
|
| 165 | public record AtlasConfig(Identifier textureId, Identifier definitionLocation, boolean createMipmaps, Set<MetadataSectionType<?>> additionalMetadata) {
|
| 166 | public AtlasConfig(final Identifier textureId, final Identifier definitionLocation, final boolean createMipmaps) {
|
| 167 | this(textureId, definitionLocation, createMipmaps, Set.of());
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | @Environment(EnvType.CLIENT)
|
| 172 | private record AtlasEntry(TextureAtlas atlas, AtlasManager.AtlasConfig config) implements AutoCloseable {
|
| 173 | @Override
|
| 174 | public void close() {
|
| 175 | this.atlas.close();
|
| 176 | }
|
| 177 |
|
| 178 | private CompletableFuture<SpriteLoader.Preparations> scheduleLoad(final ResourceManager resourceManager, final Executor executor, final int maxMipmapLevels) {
|
| 179 | return SpriteLoader.create(this.atlas)
|
| 180 | .loadAndStitch(resourceManager, this.config.definitionLocation, this.config.createMipmaps ? maxMipmapLevels : 0, executor, this.config.additionalMetadata);
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | @Environment(EnvType.CLIENT)
|
| 185 | private record PendingStitch(AtlasManager.AtlasEntry entry, CompletableFuture<SpriteLoader.Preparations> preparations) {
|
| 186 | public void joinAndUpload(final Map<SpriteId, TextureAtlasSprite> result) {
|
| 187 | SpriteLoader.Preparations preparations = this.preparations.join();
|
| 188 | this.entry.atlas.upload(preparations);
|
| 189 | preparations.regions().forEach((spriteId, spriteContents) -> result.put(new SpriteId(this.entry.config.textureId, spriteId), spriteContents));
|
| 190 | }
|
| 191 | }
|
| 192 |
|
| 193 | @Environment(EnvType.CLIENT)
|
| 194 | public static class PendingStitchResults {
|
| 195 | private final List<AtlasManager.PendingStitch> pendingStitches;
|
| 196 | private final Map<Identifier, CompletableFuture<SpriteLoader.Preparations>> stitchFuturesById;
|
| 197 | private final CompletableFuture<?> allReadyToUpload;
|
| 198 |
|
| 199 | private PendingStitchResults(
|
| 200 | final List<AtlasManager.PendingStitch> pendingStitches,
|
| 201 | final Map<Identifier, CompletableFuture<SpriteLoader.Preparations>> stitchFuturesById,
|
| 202 | final CompletableFuture<?> allReadyToUpload
|
| 203 | ) {
|
| 204 | this.pendingStitches = pendingStitches;
|
| 205 | this.stitchFuturesById = stitchFuturesById;
|
| 206 | this.allReadyToUpload = allReadyToUpload;
|
| 207 | }
|
| 208 |
|
| 209 | public Map<SpriteId, TextureAtlasSprite> joinAndUpload() {
|
| 210 | Map<SpriteId, TextureAtlasSprite> result = new HashMap<>();
|
| 211 | this.pendingStitches.forEach(pendingStitch -> pendingStitch.joinAndUpload(result));
|
| 212 | return result;
|
| 213 | }
|
| 214 |
|
| 215 | public CompletableFuture<SpriteLoader.Preparations> get(final Identifier atlasId) {
|
| 216 | return Objects.requireNonNull(this.stitchFuturesById.get(atlasId));
|
| 217 | }
|
| 218 | }
|
| 219 | }
|
| 220 |
|