From dd854b5478722db8b05460e8388c4be18701236f Mon Sep 17 00:00:00 2001 From: mcrcortex <18544518+MCRcortex@users.noreply.github.com> Date: Sat, 25 Oct 2025 20:11:35 +1000 Subject: [PATCH] set thread count to max(1,self-sodium) --- .../voxy/client/config/VoxyConfigScreenPages.java | 14 +++++++++++++- .../mixin/sodium/AccessorSodiumWorldRenderer.java | 14 ++++++++++++++ .../mixin/sodium/MixinRenderSectionManager.java | 11 +++++++++++ .../common/thread3/UnifiedServiceThreadPool.java | 7 ++++--- .../me/cortex/voxy/commonImpl/VoxyInstance.java | 5 ++++- src/main/resources/client.voxy.mixins.json | 1 + 6 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 src/main/java/me/cortex/voxy/client/mixin/sodium/AccessorSodiumWorldRenderer.java diff --git a/src/main/java/me/cortex/voxy/client/config/VoxyConfigScreenPages.java b/src/main/java/me/cortex/voxy/client/config/VoxyConfigScreenPages.java index cb706341..9814ac98 100644 --- a/src/main/java/me/cortex/voxy/client/config/VoxyConfigScreenPages.java +++ b/src/main/java/me/cortex/voxy/client/config/VoxyConfigScreenPages.java @@ -4,11 +4,13 @@ import com.google.common.collect.ImmutableList; import me.cortex.voxy.client.RenderStatistics; import me.cortex.voxy.client.VoxyClientInstance; import me.cortex.voxy.client.core.IGetVoxyRenderSystem; +import me.cortex.voxy.client.mixin.sodium.AccessorSodiumWorldRenderer; import me.cortex.voxy.common.util.cpu.CpuLayout; import me.cortex.voxy.commonImpl.VoxyCommon; import net.caffeinemc.mods.sodium.client.gui.options.*; import net.caffeinemc.mods.sodium.client.gui.options.control.SliderControl; import net.caffeinemc.mods.sodium.client.gui.options.control.TickBoxControl; +import net.caffeinemc.mods.sodium.client.render.SodiumWorldRenderer; import net.minecraft.client.MinecraftClient; import net.minecraft.text.Text; @@ -60,7 +62,17 @@ public abstract class VoxyConfigScreenPages { s.serviceThreads = v; var instance = VoxyCommon.getInstance(); if (instance != null) { - instance.setNumThreads(v); + var swr = SodiumWorldRenderer.instanceNullable(); + if (swr != null) { + var rsm = ((AccessorSodiumWorldRenderer)swr).getRenderSectionManager(); + if (rsm!=null) { + instance.setNumThreads(Math.max(1, v-rsm.getBuilder().getTotalThreadCount())); + } else { + instance.setNumThreads(v); + } + } else { + instance.setNumThreads(v); + } } }, s -> s.serviceThreads) .setImpact(OptionImpact.HIGH) diff --git a/src/main/java/me/cortex/voxy/client/mixin/sodium/AccessorSodiumWorldRenderer.java b/src/main/java/me/cortex/voxy/client/mixin/sodium/AccessorSodiumWorldRenderer.java new file mode 100644 index 00000000..9675ca55 --- /dev/null +++ b/src/main/java/me/cortex/voxy/client/mixin/sodium/AccessorSodiumWorldRenderer.java @@ -0,0 +1,14 @@ +package me.cortex.voxy.client.mixin.sodium; + +import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap; +import net.caffeinemc.mods.sodium.client.render.SodiumWorldRenderer; +import net.caffeinemc.mods.sodium.client.render.chunk.RenderSectionManager; +import net.caffeinemc.mods.sodium.client.render.chunk.map.ChunkTracker; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(value = SodiumWorldRenderer.class, remap = false) +public interface AccessorSodiumWorldRenderer { + @Accessor + RenderSectionManager getRenderSectionManager(); +} diff --git a/src/main/java/me/cortex/voxy/client/mixin/sodium/MixinRenderSectionManager.java b/src/main/java/me/cortex/voxy/client/mixin/sodium/MixinRenderSectionManager.java index e681686b..4a7f82d5 100644 --- a/src/main/java/me/cortex/voxy/client/mixin/sodium/MixinRenderSectionManager.java +++ b/src/main/java/me/cortex/voxy/client/mixin/sodium/MixinRenderSectionManager.java @@ -10,6 +10,7 @@ import me.cortex.voxy.commonImpl.WorldIdentifier; import net.caffeinemc.mods.sodium.client.gl.device.CommandList; import net.caffeinemc.mods.sodium.client.render.chunk.RenderSection; import net.caffeinemc.mods.sodium.client.render.chunk.RenderSectionManager; +import net.caffeinemc.mods.sodium.client.render.chunk.compile.executor.ChunkBuilder; import net.caffeinemc.mods.sodium.client.render.chunk.data.BuiltSectionInfo; import net.caffeinemc.mods.sodium.client.render.chunk.map.ChunkTrackerHolder; import net.caffeinemc.mods.sodium.client.render.chunk.translucent_sorting.SortBehavior; @@ -35,6 +36,8 @@ public class MixinRenderSectionManager { @Shadow @Final private ClientWorld level; + @Shadow @Final private ChunkBuilder builder; + @Inject(method = "", at = @At("TAIL")) private void voxy$resetChunkTracker(ClientWorld level, int renderDistance, SortBehavior sortBehavior, CommandList commandList, CallbackInfo ci) { if (level.worldRenderer != null) { @@ -44,6 +47,14 @@ public class MixinRenderSectionManager { } } this.bottomSectionY = this.level.getBottomY()>>4; + + { + //TODO: put in a better position + var instance = VoxyCommon.getInstance(); + if (instance != null) { + instance.setNumThreads(Math.max(1, VoxyConfig.CONFIG.serviceThreads - this.builder.getTotalThreadCount())); + } + } } @Inject(method = "onChunkRemoved", at = @At("HEAD")) diff --git a/src/main/java/me/cortex/voxy/common/thread3/UnifiedServiceThreadPool.java b/src/main/java/me/cortex/voxy/common/thread3/UnifiedServiceThreadPool.java index 80080498..3948847e 100644 --- a/src/main/java/me/cortex/voxy/common/thread3/UnifiedServiceThreadPool.java +++ b/src/main/java/me/cortex/voxy/common/thread3/UnifiedServiceThreadPool.java @@ -25,15 +25,16 @@ public class UnifiedServiceThreadPool { private final void release(int i) {this.groupSemaphore.pooledRelease(i);} - public void setNumThreads(int threads) { + public boolean setNumThreads(int threads) { synchronized (this.threads) { int diff = threads - this.threads.size(); - if (diff==0) return;//Already correct + if (diff==0) return false;//Already correct if (diff<0) {//Remove threads this.selfBlock.release(-diff); } else {//Add threads for (int i = 0; i < diff; i++) { var t = new Thread(this.dedicatedPool, this::workerThread); + t.setPriority(3); t.setDaemon(true); this.threads.add(t); t.start(); @@ -42,7 +43,7 @@ public class UnifiedServiceThreadPool { } while (true) { synchronized (this.threads) { - if (this.threads.size() == threads) return; + if (this.threads.size() == threads) return true; } try { Thread.sleep(50); diff --git a/src/main/java/me/cortex/voxy/commonImpl/VoxyInstance.java b/src/main/java/me/cortex/voxy/commonImpl/VoxyInstance.java index 28606792..8c7ab31e 100644 --- a/src/main/java/me/cortex/voxy/commonImpl/VoxyInstance.java +++ b/src/main/java/me/cortex/voxy/commonImpl/VoxyInstance.java @@ -60,7 +60,10 @@ public abstract class VoxyInstance { } public void setNumThreads(int threads) { - this.threadPool.setNumThreads(threads); + if (threads<0) throw new IllegalArgumentException("Num threads <0"); + if (this.threadPool.setNumThreads(threads)) { + Logger.info("Dedicated voxy thread pool size: " + threads); + } } protected ImportManager createImportManager() { diff --git a/src/main/resources/client.voxy.mixins.json b/src/main/resources/client.voxy.mixins.json index 389263df..c1acd775 100644 --- a/src/main/resources/client.voxy.mixins.json +++ b/src/main/resources/client.voxy.mixins.json @@ -29,6 +29,7 @@ "minecraft.MixinWorldRenderer", "nvidium.MixinRenderPipeline", "sodium.AccessorChunkTracker", + "sodium.AccessorSodiumWorldRenderer", "sodium.MixinChunkJobQueue", "sodium.MixinDefaultChunkRenderer", "sodium.MixinRenderSectionManager",