diff --git a/src/main/java/ca/spottedleaf/moonrise/common/util/MoonriseCommon.java b/src/main/java/ca/spottedleaf/moonrise/common/util/MoonriseCommon.java index c125c70a..0d75efbb 100644 --- a/src/main/java/ca/spottedleaf/moonrise/common/util/MoonriseCommon.java +++ b/src/main/java/ca/spottedleaf/moonrise/common/util/MoonriseCommon.java @@ -32,6 +32,7 @@ public void uncaughtException(final Thread thread, final Throwable throwable) { public static final long WORKER_QUEUE_HOLD_TIME = (long)(20.0e6); // 20ms public static final int CLIENT_DIVISION = 0; public static final PrioritisedThreadPool.ExecutorGroup RENDER_EXECUTOR_GROUP = MoonriseCommon.WORKER_POOL.createExecutorGroup(CLIENT_DIVISION, 0); + public static final PrioritisedThreadPool.ExecutorGroup SECTION_OCCLUSION_EXECUTOR_GROUP = MoonriseCommon.WORKER_POOL.createExecutorGroup(CLIENT_DIVISION, 0); public static final int SERVER_DIVISION = 1; public static final PrioritisedThreadPool.ExecutorGroup PARALLEL_GEN_GROUP = MoonriseCommon.WORKER_POOL.createExecutorGroup(SERVER_DIVISION, 0); public static final PrioritisedThreadPool.ExecutorGroup RADIUS_AWARE_GROUP = MoonriseCommon.WORKER_POOL.createExecutorGroup(SERVER_DIVISION, 0); diff --git a/src/main/java/ca/spottedleaf/moonrise/mixin/render/SectionOcclusionGraphMixin.java b/src/main/java/ca/spottedleaf/moonrise/mixin/render/SectionOcclusionGraphMixin.java new file mode 100644 index 00000000..82d3dbab --- /dev/null +++ b/src/main/java/ca/spottedleaf/moonrise/mixin/render/SectionOcclusionGraphMixin.java @@ -0,0 +1,56 @@ +package ca.spottedleaf.moonrise.mixin.render; + +import ca.spottedleaf.concurrentutil.executor.PrioritisedExecutor; +import ca.spottedleaf.concurrentutil.executor.thread.PrioritisedThreadPool; +import ca.spottedleaf.concurrentutil.util.Priority; +import ca.spottedleaf.moonrise.common.util.MoonriseCommon; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; +import net.minecraft.client.renderer.SectionOcclusionGraph; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +@Mixin(SectionOcclusionGraph.class) +abstract class SectionOcclusionGraphMixin { + + @Unique + private static final PrioritisedThreadPool.ExecutorGroup.ThreadPoolExecutor SECTION_OCCLUSION_EXECUTOR = MoonriseCommon.SECTION_OCCLUSION_EXECUTOR_GROUP.createExecutor( + -1, MoonriseCommon.WORKER_QUEUE_HOLD_TIME, 0 + ); + + /** + * @reason Change executor to use our thread pool + * Note: even at normal priority, our worker pool will try to share resources equally rather than having it + * be a free-for-all + * @author jpenilla + */ + @Redirect( + method = "scheduleFullUpdate", + at = @At( + value = "INVOKE", + target = "Ljava/util/concurrent/CompletableFuture;runAsync(Ljava/lang/Runnable;Ljava/util/concurrent/Executor;)Ljava/util/concurrent/CompletableFuture;" + ) + ) + private CompletableFuture changeExecutor(final Runnable runnable, final Executor executor) { + final PrioritisedExecutor.PrioritisedTask[] prioritisedTask = new PrioritisedExecutor.PrioritisedTask[1]; + final CompletableFuture future = new CompletableFuture<>() { + @Override + public Void get() throws InterruptedException, ExecutionException { + prioritisedTask[0].execute(); + return super.get(); + } + }; + prioritisedTask[0] = SECTION_OCCLUSION_EXECUTOR.queueTask(() -> { + try { + runnable.run(); + future.complete(null); + } catch (final Throwable throwable) { + future.completeExceptionally(throwable); + } + }, Priority.HIGH); // Higher than SectionRenderDispatcherMixin#changeExecutor + return future; + } +} diff --git a/src/main/resources/moonrise.mixins.json b/src/main/resources/moonrise.mixins.json index a996f60e..75d09b3e 100644 --- a/src/main/resources/moonrise.mixins.json +++ b/src/main/resources/moonrise.mixins.json @@ -128,6 +128,7 @@ "config.MinecraftMixin", "loading_screen.LevelLoadStatusManagerMixin", "profiler.MinecraftMixin", + "render.SectionOcclusionGraphMixin", "render.SectionRenderDispatcherMixin", "serverlist.ClientConnectionMixin", "serverlist.ServerAddressResolverMixin",