mirror of
https://github.com/GTNewHorizons/Angelica.git
synced 2026-11-21 04:42:17 +00:00
Speed up boot (#2190)
This commit is contained in:
1 parent
12fa08ab46
commit
38b1d9a0c3
17 files changed
+937
-23
No files matched your search
@@ -6,6 +6,7 @@ import com.gtnewhorizons.angelica.Tags;
|
||||
import com.gtnewhorizons.angelica.config.AngelicaConfig;
|
||||
import com.gtnewhorizons.angelica.glsm.RenderSystem;
|
||||
import com.gtnewhorizons.angelica.glsm.backend.BackendManager;
|
||||
import com.gtnewhorizons.angelica.glsm.hooks.GLSMHooks;
|
||||
import com.gtnewhorizons.angelica.glsm.shader.GlslVulkanPreprocess;
|
||||
import com.gtnewhorizons.angelica.glsm.shader.SpirvCompiler;
|
||||
import com.gtnewhorizons.angelica.glsm.threading.AngelicaWorkers;
|
||||
@@ -97,6 +98,7 @@ public class Iris {
|
||||
private static ShaderpackDirectoryManager shaderpacksDirectoryManager;
|
||||
|
||||
private static ShaderPack currentPack;
|
||||
private static CompletableFuture<Void> programSetWarmup;
|
||||
@Getter
|
||||
private static String currentPackName;
|
||||
@Getter
|
||||
@@ -164,14 +166,13 @@ public class Iris {
|
||||
public static void warmup() {
|
||||
final String vertexShader = "#version 120\nvoid main() { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; }";
|
||||
final String fragmentShader = "#version 120\nvoid main() { gl_FragColor = vec4(1.0); }";
|
||||
try {
|
||||
submitTracked(() -> {
|
||||
TransformPatcher.patchComposite(vertexShader, null, fragmentShader);
|
||||
TransformPatcher.patchAttributes(vertexShader, null, fragmentShader, InputAvailability.of(true, true));
|
||||
}).get();
|
||||
} catch (Exception e) {
|
||||
submitTracked(() -> {
|
||||
TransformPatcher.patchComposite(vertexShader, null, fragmentShader);
|
||||
TransformPatcher.patchAttributes(vertexShader, null, fragmentShader, InputAvailability.of(true, true));
|
||||
}).exceptionally(e -> {
|
||||
logger.warn("Warmup failed", e);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
public static <T> CompletableFuture<T> submitTracked(Supplier<T> supplier) {
|
||||
@@ -394,19 +395,35 @@ public class Iris {
|
||||
}
|
||||
|
||||
public static void warmupShaderTransforms() {
|
||||
if (currentPack == null) return;
|
||||
final ShaderPack pack = currentPack;
|
||||
if (pack == null) return;
|
||||
try {
|
||||
if (BackendManager.RENDER_BACKEND.isSDLGPU()) {
|
||||
IrisGLSMBridge.installPostTransformHook();
|
||||
}
|
||||
final ProgramSet programs = currentPack.getProgramSet(lastDimensionName != null ? lastDimensionName : "Overworld");
|
||||
PerFrameUniformBlockHarvester.harvest(programs);
|
||||
DeferredWorldRenderingPipeline.warmupTransforms(programs);
|
||||
final String dimensionName = lastDimensionName != null ? lastDimensionName : "Overworld";
|
||||
GLSMHooks.uniformBlockBarrier = Iris::awaitProgramSetWarmup;
|
||||
programSetWarmup = ShaderTransformExecutor.submitTracked(() -> {
|
||||
final ProgramSet programs = pack.getProgramSet(dimensionName);
|
||||
PerFrameUniformBlockHarvester.harvest(programs);
|
||||
DeferredWorldRenderingPipeline.warmupTransforms(programs);
|
||||
});
|
||||
} catch (Throwable t) {
|
||||
logger.warn("Early shader transform warmup failed; transforms will run at pipeline creation", t);
|
||||
}
|
||||
}
|
||||
|
||||
private static void awaitProgramSetWarmup() {
|
||||
final CompletableFuture<Void> warmup = programSetWarmup;
|
||||
if (warmup == null) return;
|
||||
programSetWarmup = null;
|
||||
try {
|
||||
warmup.join();
|
||||
} catch (RuntimeException e) {
|
||||
logger.warn("Early shader transform warmup failed; transforms will run at pipeline creation", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean loadExternalShaderpack(String name) {
|
||||
final Path shaderPackRoot;
|
||||
final Path shaderPackConfigTxt;
|
||||
@@ -682,6 +699,7 @@ public class Iris {
|
||||
* Destroys and deallocates all created OpenGL resources. Useful as part of a reload.
|
||||
*/
|
||||
private static void destroyEverything() {
|
||||
awaitProgramSetWarmup();
|
||||
currentPack = null;
|
||||
|
||||
getPipelineManager().destroyPipeline();
|
||||
@@ -746,6 +764,7 @@ public class Iris {
|
||||
* Supports dimension.properties mappings with wildcard fallback.
|
||||
*/
|
||||
private static WorldRenderingPipeline createPipeline(String dimensionName) {
|
||||
awaitProgramSetWarmup();
|
||||
if (currentPack == null) {
|
||||
// Completely disables shader-based rendering
|
||||
PerFrameUniformBlockHarvester.clear();
|
||||
|
||||
@@ -0,0 +1,232 @@
|
||||
package net.coderbot.iris.texture.pbr;
|
||||
|
||||
import com.gtnewhorizons.angelica.glsm.threading.AngelicaWorkers;
|
||||
import cpw.mods.fml.client.FMLFileResourcePack;
|
||||
import cpw.mods.fml.client.FMLFolderResourcePack;
|
||||
import net.minecraft.client.resources.AbstractResourcePack;
|
||||
import net.minecraft.client.resources.FallbackResourceManager;
|
||||
import net.minecraft.client.resources.FileResourcePack;
|
||||
import net.minecraft.client.resources.FolderResourcePack;
|
||||
import net.minecraft.client.resources.IResourceManager;
|
||||
import net.minecraft.client.resources.IResourcePack;
|
||||
import net.minecraft.client.resources.SimpleReloadableResourceManager;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileVisitOption;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.NoSuchFileException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashSet;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
public final class PBRResourceIndex {
|
||||
private static final Logger LOGGER = LogManager.getLogger("Angelica");
|
||||
private static final String ASSETS = "assets";
|
||||
private static final String NORMAL_NAME = PBRType.NORMAL.getSuffix() + ".png";
|
||||
private static final String SPECULAR_NAME = PBRType.SPECULAR.getSuffix() + ".png";
|
||||
|
||||
private static IResourceManager owner;
|
||||
private static CompletableFuture<Map<IResourcePack, PackIndex>> pending;
|
||||
private static Map<IResourcePack, PackIndex> built;
|
||||
private static boolean disabled;
|
||||
private static boolean warned;
|
||||
|
||||
private PBRResourceIndex() {
|
||||
}
|
||||
|
||||
private record PackIndex(Set<String> names, boolean lowerCase) {}
|
||||
|
||||
private record ScanTarget(IResourcePack pack, ZipFile zip, File folder) {}
|
||||
|
||||
public static synchronized void start(IResourceManager resourceManager) {
|
||||
drop();
|
||||
owner = resourceManager;
|
||||
disabled = false;
|
||||
if (!(resourceManager instanceof SimpleReloadableResourceManager manager)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
final List<ScanTarget> targets = collectTargets(manager);
|
||||
pending = AngelicaWorkers.submit(() -> scan(targets));
|
||||
} catch (RuntimeException e) {
|
||||
disable(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static synchronized void drop() {
|
||||
owner = null;
|
||||
pending = null;
|
||||
built = null;
|
||||
}
|
||||
|
||||
public static synchronized boolean mayExist(IResourceManager resourceManager, ResourceLocation location) {
|
||||
if (!(resourceManager instanceof SimpleReloadableResourceManager manager)) {
|
||||
return true;
|
||||
}
|
||||
final String path = location.getResourcePath();
|
||||
if (!path.endsWith(NORMAL_NAME) && !path.endsWith(SPECULAR_NAME)) {
|
||||
return true;
|
||||
}
|
||||
if (owner != resourceManager) {
|
||||
start(resourceManager);
|
||||
}
|
||||
if (disabled) {
|
||||
return true;
|
||||
}
|
||||
if (built == null) {
|
||||
try {
|
||||
built = pending.join();
|
||||
} catch (RuntimeException e) {
|
||||
disable(e);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
final Object domain = manager.domainResourceManagers.get(location.getResourceDomain());
|
||||
if (domain == null) {
|
||||
return false;
|
||||
}
|
||||
if (!(domain instanceof FallbackResourceManager fallback)) {
|
||||
return true;
|
||||
}
|
||||
final String name = ASSETS + "/" + location.getResourceDomain() + "/" + path;
|
||||
String lower = null;
|
||||
final List<IResourcePack> packs = fallback.resourcePacks;
|
||||
for (int i = packs.size() - 1; i >= 0; i--) {
|
||||
final IResourcePack pack = packs.get(i);
|
||||
final PackIndex index = built.get(pack);
|
||||
final boolean present;
|
||||
if (index == null) {
|
||||
present = pack.resourceExists(location);
|
||||
} else if (index.lowerCase()) {
|
||||
if (lower == null) lower = name.toLowerCase(Locale.ROOT);
|
||||
present = index.names().contains(lower);
|
||||
} else {
|
||||
present = index.names().contains(name);
|
||||
}
|
||||
if (present) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void disable(RuntimeException e) {
|
||||
disabled = true;
|
||||
built = null;
|
||||
pending = null;
|
||||
if (!warned) {
|
||||
warned = true;
|
||||
LOGGER.warn("PBR resource index disabled, falling back to direct lookups", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<ScanTarget> collectTargets(SimpleReloadableResourceManager manager) {
|
||||
final Set<IResourcePack> seen = Collections.newSetFromMap(new IdentityHashMap<>());
|
||||
final List<ScanTarget> targets = new ArrayList<>();
|
||||
for (Object domain : manager.domainResourceManagers.values()) {
|
||||
if (domain instanceof FallbackResourceManager fallback) {
|
||||
for (IResourcePack pack : fallback.resourcePacks) {
|
||||
if (seen.add(pack)) {
|
||||
final ScanTarget target = target(pack);
|
||||
if (target != null) {
|
||||
targets.add(target);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return targets;
|
||||
}
|
||||
|
||||
private static ScanTarget target(IResourcePack pack) {
|
||||
final Class<?> type = pack.getClass();
|
||||
if (type == FileResourcePack.class || type == FMLFileResourcePack.class) {
|
||||
final FileResourcePack zipPack = (FileResourcePack) pack;
|
||||
try {
|
||||
return new ScanTarget(pack, zipPack.getResourcePackZipFile(), null);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Unable to index " + zipPack.resourcePackFile, e);
|
||||
}
|
||||
}
|
||||
if (type == FolderResourcePack.class || type == FMLFolderResourcePack.class) {
|
||||
return new ScanTarget(pack, null, ((AbstractResourcePack) pack).resourcePackFile);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Map<IResourcePack, PackIndex> scan(List<ScanTarget> targets) {
|
||||
final Map<IResourcePack, PackIndex> result = new IdentityHashMap<>();
|
||||
for (ScanTarget t : targets) {
|
||||
result.put(t.pack(), t.zip() != null ? scanZip(t.zip()) : scanFolder(t.folder()));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static boolean isPbrName(String name) {
|
||||
return name.endsWith(NORMAL_NAME) || name.endsWith(SPECULAR_NAME);
|
||||
}
|
||||
|
||||
private static PackIndex scanZip(ZipFile zip) {
|
||||
final Set<String> names = new HashSet<>();
|
||||
final Enumeration<? extends ZipEntry> entries = zip.entries();
|
||||
while (entries.hasMoreElements()) {
|
||||
final String name = entries.nextElement().getName();
|
||||
final String key = name.endsWith("/") ? name.substring(0, name.length() - 1) : name;
|
||||
if (isPbrName(key)) {
|
||||
names.add(key);
|
||||
}
|
||||
}
|
||||
return new PackIndex(names, false);
|
||||
}
|
||||
|
||||
private static PackIndex scanFolder(File file) {
|
||||
final Set<String> names = new HashSet<>();
|
||||
final Path root = file.toPath();
|
||||
final Path assets = root.resolve(ASSETS);
|
||||
final boolean caseInsensitive = Files.exists(root.resolve(ASSETS.toUpperCase(Locale.ROOT)));
|
||||
if (Files.isDirectory(assets)) {
|
||||
try {
|
||||
Files.walkFileTree(assets, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<>() {
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) {
|
||||
final String relative = root.relativize(path).toString().replace(File.separatorChar, '/');
|
||||
final String name = caseInsensitive ? relative.toLowerCase(Locale.ROOT) : relative;
|
||||
if (isPbrName(name)) {
|
||||
names.add(name);
|
||||
}
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult visitFileFailed(Path path, IOException e) throws IOException {
|
||||
if (e instanceof NoSuchFileException) {
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Unable to index " + file, e);
|
||||
}
|
||||
}
|
||||
return new PackIndex(names, caseInsensitive);
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import net.coderbot.iris.texture.mipmap.ChannelMipmapGenerator;
|
||||
import net.coderbot.iris.texture.mipmap.CustomMipmapGenerator;
|
||||
import net.coderbot.iris.texture.mipmap.LinearBlendFunction;
|
||||
import net.coderbot.iris.texture.pbr.PBRAtlasTexture;
|
||||
import net.coderbot.iris.texture.pbr.PBRResourceIndex;
|
||||
import net.coderbot.iris.texture.pbr.PBRSpriteHolder;
|
||||
import net.coderbot.iris.texture.pbr.PBRType;
|
||||
import net.coderbot.iris.texture.pbr.TextureAtlasSpriteExtension;
|
||||
@@ -119,6 +120,10 @@ public class AtlasPBRLoader implements PBRTextureLoader<TextureMap> {
|
||||
final ResourceLocation imageLocation = texMap.completeResourceLocation(spriteName, 0);
|
||||
final ResourceLocation pbrImageLocation = pbrType.appendToFileLocation(imageLocation);
|
||||
|
||||
if (!PBRResourceIndex.mayExist(resourceManager, pbrImageLocation)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
TextureAtlasSprite pbrSprite = null;
|
||||
|
||||
try {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package net.coderbot.iris.texture.pbr.loader;
|
||||
|
||||
import net.coderbot.iris.texture.pbr.PBRResourceIndex;
|
||||
import net.coderbot.iris.texture.pbr.PBRType;
|
||||
import net.minecraft.client.renderer.texture.AbstractTexture;
|
||||
import net.minecraft.client.renderer.texture.SimpleTexture;
|
||||
@@ -29,6 +30,10 @@ public class SimplePBRLoader implements PBRTextureLoader<SimpleTexture> {
|
||||
protected AbstractTexture createPBRTexture(ResourceLocation imageLocation, IResourceManager resourceManager, PBRType pbrType) {
|
||||
ResourceLocation pbrImageLocation = pbrType.appendToFileLocation(imageLocation);
|
||||
|
||||
if (!PBRResourceIndex.mayExist(resourceManager, pbrImageLocation)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
SimpleTexture pbrTexture = new SimpleTexture(pbrImageLocation);
|
||||
try {
|
||||
pbrTexture.loadTexture(resourceManager);
|
||||
|
||||
Reference in new issue
Block a user