mirror of
https://github.com/MineLittlePony/MineLittlePony.git
synced 2025-02-13 08:14:23 +01:00
Shortcircuit image loading to use an existing NativeImage if one is available
This commit is contained in:
parent
0096dec423
commit
db75aa1600
1 changed files with 58 additions and 36 deletions
|
@ -1,17 +1,19 @@
|
|||
package com.minelittlepony.client.util.render;
|
||||
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.texture.NativeImage;
|
||||
import net.minecraft.client.texture.TextureManager;
|
||||
import net.minecraft.client.texture.*;
|
||||
import net.minecraft.resource.Resource;
|
||||
import net.minecraft.util.Identifier;
|
||||
|
||||
import com.mojang.blaze3d.systems.RenderSystem;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.io.InputStream;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.mojang.blaze3d.platform.GlStateManager._getTexLevelParameter;
|
||||
import static org.lwjgl.opengl.GL11.*;
|
||||
|
@ -64,7 +66,7 @@ public class NativeUtil {
|
|||
private final NativeImage.Format formatClass;
|
||||
private final int glId;
|
||||
|
||||
public static Map<Integer, InternalFormat> LOOKUP = new HashMap<>();
|
||||
public static final Map<Integer, InternalFormat> LOOKUP = Arrays.stream(values()).collect(Collectors.toMap(i -> i.glId, Function.identity()));
|
||||
|
||||
private InternalFormat(NativeImage.Format formatClass) {
|
||||
this(formatClass.toGl(), formatClass);
|
||||
|
@ -80,16 +82,7 @@ public class NativeUtil {
|
|||
}
|
||||
|
||||
public static InternalFormat valueOf(int glId) {
|
||||
if (!LOOKUP.containsKey(glId)) {
|
||||
throw new IllegalStateException("Unsupported image format: " + glId);
|
||||
}
|
||||
return LOOKUP.get(glId);
|
||||
}
|
||||
|
||||
static {
|
||||
for (InternalFormat f : values()) {
|
||||
LOOKUP.put(f.glId, f);
|
||||
}
|
||||
return Objects.requireNonNull(LOOKUP.get(glId), "Unsupported image format: " + glId);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -107,6 +100,34 @@ public class NativeUtil {
|
|||
MinecraftClient mc = MinecraftClient.getInstance();
|
||||
TextureManager textures = mc.getTextureManager();
|
||||
|
||||
AbstractTexture loadedTexture = textures.getTexture(resource);
|
||||
|
||||
if (loadedTexture instanceof NativeImageBackedTexture nibt) {
|
||||
NativeImage image = nibt.getImage();
|
||||
if (image != null) {
|
||||
consumer.accept(image);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Resource res = mc.getResourceManager().getResource(resource).orElse(null);
|
||||
if (res != null) {
|
||||
try (InputStream inputStream = res.getInputStream()){
|
||||
consumer.accept(NativeImage.read(inputStream));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
__reconstructNativeImage(resource, consumer, fail, attempt);
|
||||
} catch (Exception e) {
|
||||
fail.accept(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void __reconstructNativeImage(Identifier resource, Consumer<NativeImage> consumer, Consumer<Exception> fail, int attempt) {
|
||||
MinecraftClient mc = MinecraftClient.getInstance();
|
||||
TextureManager textures = mc.getTextureManager();
|
||||
|
||||
// recreate NativeImage from the GL matrix
|
||||
textures.bindTexture(resource);
|
||||
|
||||
|
@ -120,7 +141,11 @@ public class NativeUtil {
|
|||
if (width * height == 0) {
|
||||
if (attempt < 3) {
|
||||
CompletableFuture.delayedExecutor(500, TimeUnit.MILLISECONDS, mc).execute(() -> {
|
||||
parseImage(resource, consumer, fail, attempt + 1);
|
||||
try {
|
||||
__reconstructNativeImage(resource, consumer, fail, attempt + 1);
|
||||
} catch (Exception e) {
|
||||
fail.accept(e);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
throw new IllegalStateException("GL texture not uploaded yet: " + resource + " Parse failed 3/3 attempts");
|
||||
|
@ -133,8 +158,5 @@ public class NativeUtil {
|
|||
image.loadFromTextureImage(0, false);
|
||||
consumer.accept(image);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
fail.accept(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue