Add threading config presets

This commit is contained in:
James Seibel
2023-06-11 18:02:53 -05:00
parent 79a2e52e30
commit 190a55e98b
6 changed files with 143 additions and 21 deletions
@@ -25,7 +25,7 @@ package com.seibel.lod.api.enums.config.quickOptions;
* MINIMAL_IMPACT, <br>
* LOW_IMPACT, <br>
* BALANCED, <br>
* AGGRESSIVE_LOADING, <br>
* AGGRESSIVE, <br>
* I_PAID_FOR_THE_WHOLE_CPU, <br>
*/
public enum EThreadPreset
@@ -39,7 +39,7 @@ public enum EThreadPreset
MINIMAL_IMPACT,
LOW_IMPACT,
BALANCED,
AGGRESSIVE_LOADING,
AGGRESSIVE,
I_PAID_FOR_THE_WHOLE_CPU;
}
@@ -27,6 +27,7 @@ import com.seibel.lod.api.enums.rendering.*;
import com.seibel.lod.api.enums.worldGeneration.EDhApiDistantGeneratorMode;
import com.seibel.lod.core.config.eventHandlers.RenderQualityPresetConfigEventHandler;
import com.seibel.lod.core.config.eventHandlers.RenderCacheConfigEventHandler;
import com.seibel.lod.core.config.eventHandlers.ThreadPresetConfigEventHandler;
import com.seibel.lod.core.config.types.*;
import com.seibel.lod.core.config.types.enums.*;
import com.seibel.lod.coreapi.ModInfo;
@@ -80,28 +81,31 @@ public class Config
public static ConfigLinkedEntry quickLodChunkRenderDistance = new ConfigLinkedEntry(Advanced.Graphics.Quality.lodChunkRenderDistance);
public static ConfigEntry<EQualityPreset> qualityPresetSetting = new ConfigEntry.Builder<EQualityPreset>()
.set(EQualityPreset.MEDIUM)
.set(EQualityPreset.MEDIUM) // the default value is set via the listener when accessed
.comment(""
+ "Changing this setting will modify a number of different settings that will change the \n"
+ "visual fidelity of the rendered LODs.\n"
+ "\n"
+ "Higher settings will improve the graphical quality while increasing GPU and memory use.\n"
+ "")
.setAppearance(EConfigEntryAppearance.ONLY_IN_GUI) // TODO set when the game boots
.setAppearance(EConfigEntryAppearance.ONLY_IN_GUI)
.addListener(RenderQualityPresetConfigEventHandler.INSTANCE)
.build();
public static ConfigEntry<EThreadPreset> threadPresetSetting = new ConfigEntry.Builder<EThreadPreset>()
.set(EThreadPreset.BALANCED)
.set(EThreadPreset.LOW_IMPACT) // the default value is set via the listener when accessed
.comment(""
+ "Changing this setting will modify a number of different settings that will change \n"
+ "the load that Distant Horizons is allowed to put on your CPU. \n"
+ "\n"
+ "Higher options will improve LOD generation and loading speed, \n"
+ "but will increase CPU load and may introduce stuttering.\n"
+ "\n"
+ "Note: on CPUs with 4 cores or less these settings will be less effective \n"
+ " and some settings will give similar results. \n"
+ "")
.setAppearance(EConfigEntryAppearance.ONLY_IN_GUI) // TODO set when the game boots
//.addListener(null) // TODO add listener
.setAppearance(EConfigEntryAppearance.ONLY_IN_GUI)
.addListener(ThreadPresetConfigEventHandler.INSTANCE)
.build();
public static ConfigLinkedEntry quickEnableWorldGenerator = new ConfigLinkedEntry(Advanced.WorldGenerator.enableDistantGeneration);
@@ -16,10 +16,6 @@ import org.apache.logging.log4j.Logger;
import java.util.*;
/**
* Listens to the config and will automatically
* clear the current render cache if certain settings are changed.
*/
public abstract class AbstractPresetConfigEventHandler<TPresetEnum extends Enum<?>> implements IConfigListener
{
private static final Logger LOGGER = LogManager.getLogger();
@@ -50,7 +46,7 @@ public abstract class AbstractPresetConfigEventHandler<TPresetEnum extends Enum<
}
// if the quick value is custom, nothing needs to be changed
if (qualityPreset == EQualityPreset.CUSTOM)
if (qualityPreset == this.getCustomPresetEnum())
{
return;
}
@@ -16,10 +16,6 @@ import org.apache.logging.log4j.Logger;
import java.util.*;
/**
* Listens to the config and will automatically
* clear the current render cache if certain settings are changed.
*/
public class RenderQualityPresetConfigEventHandler extends AbstractPresetConfigEventHandler<EQualityPreset>
{
public static final RenderQualityPresetConfigEventHandler INSTANCE = new RenderQualityPresetConfigEventHandler();
@@ -98,7 +94,6 @@ public class RenderQualityPresetConfigEventHandler extends AbstractPresetConfigE
@Override
protected List<EQualityPreset> getPresetEnumList() { return Arrays.asList(EQualityPreset.values()); }
@Override
protected EQualityPreset getCustomPresetEnum() { return EQualityPreset.CUSTOM; }
@@ -0,0 +1,127 @@
package com.seibel.lod.core.config.eventHandlers;
import com.seibel.lod.api.enums.config.quickOptions.EThreadPreset;
import com.seibel.lod.core.config.Config;
import com.seibel.lod.core.config.ConfigEntryWithPresetOptions;
import com.seibel.lod.core.config.listeners.ConfigChangeListener;
import com.seibel.lod.coreapi.interfaces.config.IConfigEntry;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
public class ThreadPresetConfigEventHandler extends AbstractPresetConfigEventHandler<EThreadPreset>
{
public static final ThreadPresetConfigEventHandler INSTANCE = new ThreadPresetConfigEventHandler();
private static final Logger LOGGER = LogManager.getLogger();
private final ConfigEntryWithPresetOptions<EThreadPreset, Integer> worldGen = new ConfigEntryWithPresetOptions<>(Config.Client.Advanced.MultiThreading.numberOfWorldGenerationThreads,
new HashMap<EThreadPreset, Integer>()
{{
this.put(EThreadPreset.MINIMAL_IMPACT, 1);
this.put(EThreadPreset.LOW_IMPACT, getThreadCountByPercent(0.1));
this.put(EThreadPreset.BALANCED, getThreadCountByPercent(0.2));
this.put(EThreadPreset.AGGRESSIVE, getThreadCountByPercent(0.4));
this.put(EThreadPreset.I_PAID_FOR_THE_WHOLE_CPU, getThreadCountByPercent(1.0));
}});
private final ConfigEntryWithPresetOptions<EThreadPreset, Integer> bufferBuilders = new ConfigEntryWithPresetOptions<>(Config.Client.Advanced.MultiThreading.numberOfBufferBuilderThreads,
new HashMap<EThreadPreset, Integer>()
{{
this.put(EThreadPreset.MINIMAL_IMPACT, 1);
this.put(EThreadPreset.LOW_IMPACT, getThreadCountByPercent(0.1));
this.put(EThreadPreset.BALANCED, getThreadCountByPercent(0.2));
this.put(EThreadPreset.AGGRESSIVE, getThreadCountByPercent(0.4));
this.put(EThreadPreset.I_PAID_FOR_THE_WHOLE_CPU, getThreadCountByPercent(1.0));
}});
private final ConfigEntryWithPresetOptions<EThreadPreset, Integer> fileHandlers = new ConfigEntryWithPresetOptions<>(Config.Client.Advanced.MultiThreading.numberOfFileHandlerThreads,
new HashMap<EThreadPreset, Integer>()
{{
this.put(EThreadPreset.MINIMAL_IMPACT, 1);
this.put(EThreadPreset.LOW_IMPACT, getThreadCountByPercent(0.1));
this.put(EThreadPreset.BALANCED, getThreadCountByPercent(0.2));
this.put(EThreadPreset.AGGRESSIVE, getThreadCountByPercent(0.2));
this.put(EThreadPreset.I_PAID_FOR_THE_WHOLE_CPU, getThreadCountByPercent(1.0));
}});
private final ConfigEntryWithPresetOptions<EThreadPreset, Integer> dataConverters = new ConfigEntryWithPresetOptions<>(Config.Client.Advanced.MultiThreading.numberOfDataConverterThreads,
new HashMap<EThreadPreset, Integer>()
{{
this.put(EThreadPreset.MINIMAL_IMPACT, 1);
this.put(EThreadPreset.LOW_IMPACT, getThreadCountByPercent(0.1));
this.put(EThreadPreset.BALANCED, getThreadCountByPercent(0.2));
this.put(EThreadPreset.AGGRESSIVE, getThreadCountByPercent(0.2));
this.put(EThreadPreset.I_PAID_FOR_THE_WHOLE_CPU, getThreadCountByPercent(1.0));
}});
//==============//
// constructors //
//==============//
/** private since we only ever need one handler at a time */
private ThreadPresetConfigEventHandler()
{
// add each config used by this preset
this.configList.add(this.worldGen);
this.configList.add(this.bufferBuilders);
this.configList.add(this.fileHandlers);
this.configList.add(this.dataConverters);
for (ConfigEntryWithPresetOptions<EThreadPreset, ?> config : this.configList)
{
// ignore try-using, the listener should only ever be added once and should never be removed
new ConfigChangeListener<>(config.configEntry, (val) -> { this.onConfigValueChanged(); });
}
}
//================//
// helper methods //
//================//
/**
* Pre-computed values for your convenience: <br>
* Format: percent: 4coreCpu-8coreCpu-16coreCpu <br><br>
* <code>
* 0.1: 1-1-2 <br>
* 0.2: 1-2-4 <br>
* 0.4: 2-4-7 <br>
* 0.6: 3-5-10 <br>
* 0.8: 4-7-13 <br>
* 1.0: 4-8-16 <br>
* </code>
*/
private static int getThreadCountByPercent(double percent) throws IllegalArgumentException
{
if (percent <= 0 || percent > 1)
{
throw new IllegalArgumentException("percent must be greater than 0 and less than or equal to 1.");
}
// this is logical processor count, not physical CPU cores
int totalProcessorCount = Runtime.getRuntime().availableProcessors();
int coreCount = (int) Math.ceil(totalProcessorCount * percent);
return Math.max(1, coreCount);
}
//==============//
// enum getters //
//==============//
@Override
protected IConfigEntry<EThreadPreset> getPresetConfigEntry() { return Config.Client.threadPresetSetting; }
@Override
protected List<EThreadPreset> getPresetEnumList() { return Arrays.asList(EThreadPreset.values()); }
@Override
protected EThreadPreset getCustomPresetEnum() { return EThreadPreset.CUSTOM; }
}
@@ -57,11 +57,11 @@
"lod.config.client.qualityPresetSetting":
"Quality Preset",
"lod.config.client.qualityPresetSetting.@tooltip":
"Will modify a number of settings to quickly change Distant Horizons' rendering quality.",
"Modifies a number of graphical settings to quickly change Distant Horizons' rendering quality. \n\nLower this setting if your GPU is at max usage or you are having framerate issues.",
"lod.config.client.threadPresetSetting":
"CPU Load",
"lod.config.client.threadPresetSetting.@tooltip":
"Will modify a number of settings to quickly change how many CPU resources Distant Horizons' is allowed to use. \n\nIncreasing this setting will improve Distant Generator speed and LOD loading speed, \nbut will also increase CPU/memory usage and may introduce stuttering.",
"Modifies how many threads Distant Horizons' will use. \n\nIncreasing this setting will improve the Distant Generator speed and LOD loading speed, \nbut will also increase CPU/memory usage and may introduce stuttering. \n\nNote: on CPUs with 4 cores or less these settings will be less effective \nand some settings will give similar results.",
"lod.config.client.optionsButton":
"Show The Options Button",
@@ -452,8 +452,8 @@
"Low Impact",
"lod.config.enum.EThreadPreset.BALANCED":
"Balanced",
"lod.config.enum.EThreadPreset.AGGRESSIVE_LOADING":
"Aggressive Loading",
"lod.config.enum.EThreadPreset.AGGRESSIVE":
"Aggressive",
"lod.config.enum.EThreadPreset.I_PAID_FOR_THE_WHOLE_CPU":
"I Paid For The Whole CPU",