mirror of
https://github.com/GTNewHorizons/Angelica.git
synced 2026-11-19 03:49:22 +00:00
Parallel shader transformation and use Transformer instead of Utils (#1191)
This commit is contained in:
1 parent
f0d45b0d62
commit
89132f3ef9
16 files changed
+743
-302
No files matched your search
@@ -16,6 +16,9 @@ import net.coderbot.iris.config.IrisConfig;
|
||||
import net.coderbot.iris.gl.shader.StandardMacros;
|
||||
import net.coderbot.iris.gui.screen.ShaderPackScreen;
|
||||
import net.coderbot.iris.pipeline.DeferredWorldRenderingPipeline;
|
||||
import net.coderbot.iris.pipeline.transform.ShaderTransformer;
|
||||
import net.coderbot.iris.pipeline.transform.TransformPatcher;
|
||||
import net.coderbot.iris.gbuffer_overrides.matching.InputAvailability;
|
||||
import net.coderbot.iris.pipeline.FixedFunctionWorldRenderingPipeline;
|
||||
import net.coderbot.iris.pipeline.PipelineManager;
|
||||
import net.coderbot.iris.pipeline.WorldRenderingPipeline;
|
||||
@@ -54,8 +57,18 @@ import java.nio.file.NoSuchFileException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ForkJoinPool;
|
||||
import java.util.concurrent.ForkJoinWorkerThread;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.zip.ZipError;
|
||||
import java.util.zip.ZipException;
|
||||
@@ -82,6 +95,8 @@ public class Iris {
|
||||
private static String currentPackName;
|
||||
@Getter
|
||||
private static boolean initialized;
|
||||
@Getter
|
||||
private static int shaderPackLoadId = 0;
|
||||
|
||||
private static PipelineManager pipelineManager;
|
||||
@Getter
|
||||
@@ -99,6 +114,175 @@ public class Iris {
|
||||
@Getter
|
||||
private static boolean fallback;
|
||||
|
||||
/**
|
||||
* Lazy executor for parallelizing shader transformations during shader pack loading.
|
||||
* Creates threads on demand and shuts down after a period of inactivity.
|
||||
*/
|
||||
public static final class ShaderTransformExecutor {
|
||||
private static final long IDLE_TIMEOUT_SECONDS = 120;
|
||||
private static final int THREAD_COUNT = Math.max(2, Math.min(12, Runtime.getRuntime().availableProcessors() / 2));
|
||||
|
||||
private static final Object lock = new Object();
|
||||
private static ExecutorService executor;
|
||||
private static ScheduledExecutorService scheduler;
|
||||
private static volatile long lastActivityTime;
|
||||
private static final AtomicInteger inFlight = new AtomicInteger(0);
|
||||
private static boolean idleCheckScheduled;
|
||||
|
||||
private ShaderTransformExecutor() {}
|
||||
|
||||
private static void noteActivity() {
|
||||
lastActivityTime = System.nanoTime();
|
||||
}
|
||||
|
||||
public static ExecutorService get() {
|
||||
synchronized (lock) {
|
||||
noteActivity();
|
||||
|
||||
if (executor != null && !executor.isShutdown()) {
|
||||
return executor;
|
||||
}
|
||||
|
||||
executor = new ForkJoinPool(
|
||||
THREAD_COUNT,
|
||||
pool -> {
|
||||
ForkJoinWorkerThread t = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool);
|
||||
t.setName("Shader-Transform-" + t.getPoolIndex());
|
||||
t.setDaemon(true);
|
||||
return t;
|
||||
},
|
||||
null, // UncaughtExceptionHandler
|
||||
true // asyncMode - FIFO, better for independent tasks
|
||||
);
|
||||
logger.debug("Created shader transform executor with " + THREAD_COUNT + " threads");
|
||||
|
||||
if (scheduler == null || scheduler.isShutdown()) {
|
||||
scheduler = Executors.newSingleThreadScheduledExecutor(r -> {
|
||||
Thread t = new Thread(r, "Shader-Transform-Scheduler");
|
||||
t.setDaemon(true);
|
||||
return t;
|
||||
});
|
||||
}
|
||||
if (!idleCheckScheduled) {
|
||||
idleCheckScheduled = true;
|
||||
scheduleIdleCheck(IDLE_TIMEOUT_SECONDS);
|
||||
}
|
||||
|
||||
return executor;
|
||||
}
|
||||
}
|
||||
|
||||
private static void scheduleIdleCheck(long delaySeconds) {
|
||||
try {
|
||||
scheduler.schedule(ShaderTransformExecutor::checkIdleShutdown, delaySeconds, TimeUnit.SECONDS);
|
||||
} catch (Exception e) {
|
||||
// If scheduling fails, reset the flag so a future get() can try again
|
||||
idleCheckScheduled = false;
|
||||
logger.warn("Failed to schedule idle check", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures the executor exists and begins spinning up worker threads.
|
||||
* Intended for UI entrypoints (eg. shader settings screen open).
|
||||
*/
|
||||
public static void prepare() {
|
||||
// Submit empty tasks to warm up all worker threads without blocking the UI thread.
|
||||
for (int i = 0; i < THREAD_COUNT; i++) {
|
||||
submitTracked(() -> { });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Warm up the threadpool by running a representative shader transformation.
|
||||
*/
|
||||
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, new InputAvailability(true, true));
|
||||
}).get();
|
||||
} catch (Exception e) {
|
||||
logger.warn("Warmup failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> CompletableFuture<T> submitTracked(Supplier<T> supplier) {
|
||||
Objects.requireNonNull(supplier);
|
||||
noteActivity();
|
||||
inFlight.incrementAndGet();
|
||||
try {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
return supplier.get();
|
||||
} finally {
|
||||
noteActivity();
|
||||
inFlight.decrementAndGet();
|
||||
}
|
||||
}, get());
|
||||
} catch (Exception e) {
|
||||
inFlight.decrementAndGet();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public static CompletableFuture<Void> submitTracked(Runnable runnable) {
|
||||
Objects.requireNonNull(runnable);
|
||||
noteActivity();
|
||||
inFlight.incrementAndGet();
|
||||
try {
|
||||
return CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
runnable.run();
|
||||
} finally {
|
||||
noteActivity();
|
||||
inFlight.decrementAndGet();
|
||||
}
|
||||
}, get());
|
||||
} catch (Exception e) {
|
||||
inFlight.decrementAndGet();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkIdleShutdown() {
|
||||
synchronized (lock) {
|
||||
final ExecutorService current = executor;
|
||||
if (current == null || current.isShutdown()) {
|
||||
// Executor already gone, shut down scheduler
|
||||
if (scheduler != null && !scheduler.isShutdown()) {
|
||||
scheduler.shutdown();
|
||||
scheduler = null;
|
||||
}
|
||||
idleCheckScheduled = false;
|
||||
return;
|
||||
}
|
||||
|
||||
final long idleNanos = System.nanoTime() - lastActivityTime;
|
||||
final long idleSeconds = TimeUnit.NANOSECONDS.toSeconds(idleNanos);
|
||||
|
||||
if (idleSeconds >= IDLE_TIMEOUT_SECONDS && inFlight.get() == 0) {
|
||||
logger.debug("Shutting down idle shader transform executor after " + idleSeconds + " seconds");
|
||||
current.shutdown();
|
||||
executor = null;
|
||||
scheduler.shutdown();
|
||||
scheduler = null;
|
||||
idleCheckScheduled = false;
|
||||
|
||||
// Clear transformation caches - no longer needed after loading
|
||||
TransformPatcher.clearCache();
|
||||
ShaderTransformer.clearCache();
|
||||
} else {
|
||||
// Still active (or in-flight), schedule another check.
|
||||
final long remainingSeconds = Math.max(1, IDLE_TIMEOUT_SECONDS - idleSeconds + 1);
|
||||
scheduleIdleCheck(remainingSeconds);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static KeyBinding reloadKeybind;
|
||||
private static KeyBinding toggleShadersKeybind;
|
||||
private static KeyBinding shaderpackScreenKeybind;
|
||||
@@ -194,6 +378,9 @@ public class Iris {
|
||||
return;
|
||||
}
|
||||
|
||||
// Warm up the threadpool so shader transformations are faster when we need them
|
||||
ShaderTransformExecutor.warmup();
|
||||
|
||||
PBRTextureManager.INSTANCE.init();
|
||||
|
||||
// Only load the shader pack when we can access OpenGL
|
||||
@@ -575,7 +762,12 @@ public class Iris {
|
||||
|
||||
|
||||
try {
|
||||
return new DeferredWorldRenderingPipeline(programs);
|
||||
shaderPackLoadId++;
|
||||
long startTime = System.nanoTime();
|
||||
WorldRenderingPipeline pipeline = new DeferredWorldRenderingPipeline(programs);
|
||||
long endTime = System.nanoTime();
|
||||
logger.info("[Load #{}] Total shaderpack load time for '{}': {} ms", shaderPackLoadId, currentPackName, String.format("%.1f", (endTime - startTime) / 1_000_000.0));
|
||||
return pipeline;
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to create shader rendering pipeline, disabling shaders!", e);
|
||||
// TODO: This should be reverted if a dimension change causes shaders to compile again
|
||||
|
||||
Reference in new issue
Block a user