| 1 | package net.minecraft.client.renderer.texture;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.util.HashMap;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Locale;
|
| 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.stream.Collectors;
|
| 13 | import net.fabricmc.api.EnvType;
|
| 14 | import net.fabricmc.api.Environment;
|
| 15 | import net.fabricmc.fabric.api.client.renderer.v1.sprite.FabricPreparations;
|
| 16 | import net.minecraft.CrashReport;
|
| 17 | import net.minecraft.CrashReportCategory;
|
| 18 | import net.minecraft.ReportedException;
|
| 19 | import net.minecraft.client.Minecraft;
|
| 20 | import net.minecraft.client.Options;
|
| 21 | import net.minecraft.client.TextureFilteringMethod;
|
| 22 | import net.minecraft.client.renderer.texture.atlas.SpriteResourceLoader;
|
| 23 | import net.minecraft.client.renderer.texture.atlas.SpriteSource;
|
| 24 | import net.minecraft.client.renderer.texture.atlas.SpriteSourceList;
|
| 25 | import net.minecraft.resources.Identifier;
|
| 26 | import net.minecraft.server.packs.metadata.MetadataSectionType;
|
| 27 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 28 | import net.minecraft.util.Mth;
|
| 29 | import net.minecraft.util.Util;
|
| 30 | import net.minecraft.util.profiling.Profiler;
|
| 31 | import net.minecraft.util.profiling.Zone;
|
| 32 | import org.jspecify.annotations.Nullable;
|
| 33 | import org.slf4j.Logger;
|
| 34 |
|
| 35 | @Environment(EnvType.CLIENT)
|
| 36 | public class SpriteLoader {
|
| 37 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 38 | private final Identifier location;
|
| 39 | private final int maxSupportedTextureSize;
|
| 40 |
|
| 41 | public SpriteLoader(final Identifier location, final int maxSupportedTextureSize) {
|
| 42 | this.location = location;
|
| 43 | this.maxSupportedTextureSize = maxSupportedTextureSize;
|
| 44 | }
|
| 45 |
|
| 46 | public static SpriteLoader create(final TextureAtlas atlas) {
|
| 47 | return new SpriteLoader(atlas.location(), atlas.maxSupportedTextureSize());
|
| 48 | }
|
| 49 |
|
| 50 | private SpriteLoader.Preparations stitch(final List<SpriteContents> sprites, final int maxMipmapLevels, final Executor executor) {
|
| 51 | try (Zone ignored = Profiler.get().zone(() -> "stitch " + this.location)) {
|
| 52 | int maxTextureSize = this.maxSupportedTextureSize;
|
| 53 | int minTexelSize = Integer.MAX_VALUE;
|
| 54 | int lowestOneBit = 1 << maxMipmapLevels;
|
| 55 |
|
| 56 | for (SpriteContents spriteInfo : sprites) {
|
| 57 | minTexelSize = Math.min(minTexelSize, Math.min(spriteInfo.width(), spriteInfo.height()));
|
| 58 | int lowestTextureBit = Math.min(Integer.lowestOneBit(spriteInfo.width()), Integer.lowestOneBit(spriteInfo.height()));
|
| 59 | if (lowestTextureBit < lowestOneBit) {
|
| 60 | LOGGER.warn(
|
| 61 | "Texture {} with size {}x{} limits mip level from {} to {}",
|
| 62 | spriteInfo.name(),
|
| 63 | spriteInfo.width(),
|
| 64 | spriteInfo.height(),
|
| 65 | Mth.log2(lowestOneBit),
|
| 66 | Mth.log2(lowestTextureBit)
|
| 67 | );
|
| 68 | lowestOneBit = lowestTextureBit;
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | int minSize = Math.min(minTexelSize, lowestOneBit);
|
| 73 | int minPowerOfTwo = Mth.log2(minSize);
|
| 74 | int mipLevel;
|
| 75 | if (minPowerOfTwo < maxMipmapLevels) {
|
| 76 | LOGGER.warn("{}: dropping miplevel from {} to {}, because of minimum power of two: {}", this.location, maxMipmapLevels, minPowerOfTwo, minSize);
|
| 77 | mipLevel = minPowerOfTwo;
|
| 78 | } else {
|
| 79 | mipLevel = maxMipmapLevels;
|
| 80 | }
|
| 81 |
|
| 82 | Options options = Minecraft.getInstance().options;
|
| 83 | int anisotropyBit = options.textureFiltering().get() != TextureFilteringMethod.ANISOTROPIC ? 0 : options.maxAnisotropyBit().get();
|
| 84 | Stitcher<SpriteContents> stitcher = new Stitcher<>(maxTextureSize, maxTextureSize, mipLevel, anisotropyBit);
|
| 85 |
|
| 86 | for (SpriteContents spriteInfo : sprites) {
|
| 87 | stitcher.registerSprite(spriteInfo);
|
| 88 | }
|
| 89 |
|
| 90 | try {
|
| 91 | stitcher.stitch();
|
| 92 | } catch (StitcherException e) {
|
| 93 | CrashReport report = CrashReport.forThrowable(e, "Stitching");
|
| 94 | CrashReportCategory category = report.addCategory("Stitcher");
|
| 95 | category.setDetail(
|
| 96 | "Sprites", e.getAllSprites().stream().map(s -> String.format(Locale.ROOT, "%s[%dx%d]", s.name(), s.width(), s.height())).collect(Collectors.joining(","))
|
| 97 | );
|
| 98 | category.setDetail("Max Texture Size", maxTextureSize);
|
| 99 | throw new ReportedException(report);
|
| 100 | }
|
| 101 |
|
| 102 | int width = stitcher.getWidth();
|
| 103 | int height = stitcher.getHeight();
|
| 104 | Map<Identifier, TextureAtlasSprite> result = this.getStitchedSprites(stitcher, width, height);
|
| 105 | TextureAtlasSprite missingSprite = result.get(MissingTextureAtlasSprite.getLocation());
|
| 106 | CompletableFuture<Void> readyForUpload = CompletableFuture.runAsync(() -> result.values().forEach(s -> s.contents().increaseMipLevel(mipLevel)), executor);
|
| 107 | return new SpriteLoader.Preparations(width, height, mipLevel, missingSprite, result, readyForUpload);
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | private static CompletableFuture<List<SpriteContents>> runSpriteSuppliers(
|
| 112 | final SpriteResourceLoader resourceLoader, final List<SpriteSource.Loader> sprites, final Executor executor
|
| 113 | ) {
|
| 114 | List<CompletableFuture<SpriteContents>> spriteFutures = sprites.stream()
|
| 115 | .map(supplier -> CompletableFuture.supplyAsync(() -> supplier.get(resourceLoader), executor))
|
| 116 | .toList();
|
| 117 | return Util.sequence(spriteFutures).thenApply(l -> l.stream().filter(Objects::nonNull).toList());
|
| 118 | }
|
| 119 |
|
| 120 | public CompletableFuture<SpriteLoader.Preparations> loadAndStitch(
|
| 121 | final ResourceManager manager,
|
| 122 | final Identifier atlasInfoLocation,
|
| 123 | final int maxMipmapLevels,
|
| 124 | final Executor taskExecutor,
|
| 125 | final Set<MetadataSectionType<?>> additionalMetadata
|
| 126 | ) {
|
| 127 | SpriteResourceLoader spriteResourceLoader = SpriteResourceLoader.create(additionalMetadata);
|
| 128 | return CompletableFuture.<List<SpriteSource.Loader>>supplyAsync(() -> SpriteSourceList.load(manager, atlasInfoLocation).list(manager), taskExecutor)
|
| 129 | .thenCompose(sprites -> runSpriteSuppliers(spriteResourceLoader, (List<SpriteSource.Loader>)sprites, taskExecutor))
|
| 130 | .thenApply(resources -> this.stitch((List<SpriteContents>)resources, maxMipmapLevels, taskExecutor));
|
| 131 | }
|
| 132 |
|
| 133 | private Map<Identifier, TextureAtlasSprite> getStitchedSprites(final Stitcher<SpriteContents> stitcher, final int atlasWidth, final int atlasHeight) {
|
| 134 | Map<Identifier, TextureAtlasSprite> result = new HashMap<>();
|
| 135 | stitcher.gatherSprites(
|
| 136 | (contents, x, y, padding) -> result.put(contents.name(), new TextureAtlasSprite(this.location, contents, atlasWidth, atlasHeight, x, y, padding))
|
| 137 | );
|
| 138 | return result;
|
| 139 | }
|
| 140 |
|
| 141 | @Environment(EnvType.CLIENT)
|
| 142 | public record Preparations(
|
| 143 | int width, int height, int mipLevel, TextureAtlasSprite missing, Map<Identifier, TextureAtlasSprite> regions, CompletableFuture<Void> readyForUpload
|
| 144 | ) implements FabricPreparations {
|
| 145 | public @Nullable TextureAtlasSprite getSprite(final Identifier id) {
|
| 146 | return this.regions.get(id);
|
| 147 | }
|
| 148 | }
|
| 149 | }
|
| 150 |
|