Skip to content

Commit

Permalink
Split out experimental patches into two separate optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeltumn committed Dec 18, 2024
1 parent 3cbd3b4 commit a1dfa92
Show file tree
Hide file tree
Showing 23 changed files with 224 additions and 309 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ There are also a few improvements that do not require a server:

### Performance

Noxesium contains a performance patch which reworks UI rendering to have a dynamic fps. Instead of rendering the UI every frame it gets rendered between 20 and 60 times a second. Whenever a UI element changes it starts getting rendered at 60 fps, while UI elements that rarely change render at 20 fps. These are currently experimental and have to manually be enabled in the Noxesium configuration screen.
Noxesium contains an optional performance patch which limits UI rendering to be limited to 60 fps which can improve fps at high values. There is an additional setting available to enable dynamic UI fps lowering, going down to 20 fps.

### Bugfixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@ public class ServerRuleIndices {
*/
public static final int HAND_ITEM_OVERRIDE = 8;

/**
* Disables the UI optimizations provided by Noxesium. There are currently
* no known incompatibilities (text shaders are fully supported), but this
* option is still provided in case it becomes necessary.
*/
public static final int DISABLE_UI_OPTIMIZATIONS = 9;
// Id 9 has been removed.

/**
* Moves the handheld map to be shown in the top left/right corner instead of
Expand Down
43 changes: 14 additions & 29 deletions common/src/main/java/com/noxcrew/noxesium/NoxesiumMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,48 +115,33 @@ public NoxesiumMod(NoxesiumPlatformHook platformHook) {
ignored = CustomCoreShaders.BLIT_SCREEN_MULTIPLE;
ignored = CustomRenderTypes.linesNoDepth();

// Run rebuilds on a separate thread to not destroy fps unnecessarily
var rebuildThread = new Thread("Noxesium Spatial Container Rebuild Thread") {
// Run rebuilds on a separate thread to not destroy fps unnecessarily, also run
// frame comparisons on this thread if they are enabled.
var backgroundTaskThread = new Thread("Noxesium Background Task Thread") {
@Override
public void run() {
while (true) {
try {
Thread.sleep(2500);
SpatialInteractionEntityTree.rebuild();
} catch (InterruptedException ex) {
return;
} catch (Exception ex) {
logger.error("Caught exception from Noxesium Spatial Container Rebuild Thread", ex);
}
}
}
};
rebuildThread.setDaemon(true);
rebuildThread.start();

// Also run frame comparisons on another thread
var frameComparisonThread = new Thread("Noxesium Frame Comparison Thread") {
@Override
public void run() {
while (true) {
try {
forEachRenderStateHolder((it) -> {
var state = it.get();
if (state != null) {
state.tick();
}
});
if (getConfig().enableDynamicUiLimiting) {
forEachRenderStateHolder((it) -> {
var state = it.get();
if (state != null) {
state.tick();
}
});
}
Thread.sleep(20);
} catch (InterruptedException ex) {
return;
} catch (Exception ex) {
logger.error("Caught exception from Noxesium Frame Comparison Thread", ex);
logger.error("Caught exception from Noxesium Background Task Thread", ex);
}
}
}
};
frameComparisonThread.setDaemon(true);
frameComparisonThread.start();
backgroundTaskThread.setDaemon(true);
backgroundTaskThread.start();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,23 @@
*/
public class NoxesiumConfig {

/**
* The current state of the hotkey for toggling the experimental
* patches on/off.
*/
public static Boolean experimentalPatchesHotkey = null;

private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();

public boolean resetToggleKeys = false;
public BooleanOrDefault renderMapsInUi = BooleanOrDefault.DEFAULT;
public boolean showFpsOverlay = false;
public boolean showGameTimeOverlay = false;
public boolean enableQibSystemDebugging = false;
public boolean disableExperimentalPerformancePatches = true; // Still off by default until thoroughly tested!
public boolean showGlowingSettings = false;
public boolean dumpIncomingPackets = false;
public boolean dumpOutgoingPackets = false;
public boolean printPacketExceptions = false;
public double mapUiSize = 0.8;
public MapLocation mapUiLocation = MapLocation.TOP;
public boolean enableUiLimiting = false;
public boolean enableDynamicUiLimiting = false;
public boolean showUiDebugOverlay = false;
public int maxUiFramerate = 60;
public boolean showOptimizationOverlay = false;

/**
* Returns whether experimental performance are enabled in the configuration.
*/
public boolean hasConfiguredPerformancePatches() {
return !disableExperimentalPerformancePatches;
}

/**
* Returns whether to render maps in the UI.
Expand All @@ -56,21 +44,6 @@ public boolean shouldRenderMapsInUi() {
return renderMapsInUi == BooleanOrDefault.TRUE;
}

/**
* Whether the experimental performance patches should be used.
*/
public boolean shouldDisableExperimentalPerformancePatches() {
if (ServerRules.DISABLE_UI_OPTIMIZATIONS.getValue()) return true;

if (hasConfiguredPerformancePatches()) {
if (experimentalPatchesHotkey != null) {
return !experimentalPatchesHotkey;
}
return false;
}
return true;
}

/**
* Loads this configuration file.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@
*/
public class NoxesiumOptions {

private static final OptionInstance<Boolean> experimentalPatches = OptionInstance.createBoolean(
"noxesium.options.experimental_patches.name",
OptionInstance.cachedConstantTooltip(Component.translatable("noxesium.options.experimental_patches.tooltip.v2")),
NoxesiumMod.getInstance().getConfig().hasConfiguredPerformancePatches(),
(newValue) -> {
NoxesiumMod.getInstance().getConfig().disableExperimentalPerformancePatches = !newValue;
NoxesiumMod.getInstance().getConfig().save();
}
);

private static final OptionInstance<Boolean> fpsOverlay = OptionInstance.createBoolean(
"noxesium.options.fps_overlay.name",
OptionInstance.cachedConstantTooltip(Component.translatable("noxesium.options.fps_overlay.tooltip")),
Expand Down Expand Up @@ -93,6 +83,26 @@ public class NoxesiumOptions {
}
);

private static final OptionInstance<Boolean> enableUiLimiting = OptionInstance.createBoolean(
"noxesium.options.ui_limiting.name",
OptionInstance.cachedConstantTooltip(Component.translatable("noxesium.options.ui_limiting.tooltip")),
NoxesiumMod.getInstance().getConfig().enableUiLimiting,
(newValue) -> {
NoxesiumMod.getInstance().getConfig().enableUiLimiting = newValue;
NoxesiumMod.getInstance().getConfig().save();
}
);

private static final OptionInstance<Boolean> enableDynamicUiLimiting = OptionInstance.createBoolean(
"noxesium.options.dynamic_ui_limiting.name",
OptionInstance.cachedConstantTooltip(Component.translatable("noxesium.options.dynamic_ui_limiting.tooltip")),
NoxesiumMod.getInstance().getConfig().enableDynamicUiLimiting,
(newValue) -> {
NoxesiumMod.getInstance().getConfig().enableDynamicUiLimiting = newValue;
NoxesiumMod.getInstance().getConfig().save();
}
);

private static final OptionInstance<Integer> maxUiFramerate = new OptionInstance<>(
"noxesium.options.max_ui_framerate.name",
OptionInstance.cachedConstantTooltip(Component.translatable("noxesium.options.max_ui_framerate.tooltip")),
Expand All @@ -111,17 +121,21 @@ public class NoxesiumOptions {
);

private static final OptionInstance<Boolean> optimizationOverlay = OptionInstance.createBoolean(
"noxesium.options.optimization_overlay.name",
OptionInstance.cachedConstantTooltip(Component.translatable("noxesium.options.optimization_overlay.tooltip")),
NoxesiumMod.getInstance().getConfig().showOptimizationOverlay,
"noxesium.options.ui_debug_overlay.name",
OptionInstance.cachedConstantTooltip(Component.translatable("noxesium.options.ui_debug_overlay.tooltip")),
NoxesiumMod.getInstance().getConfig().showUiDebugOverlay,
(newValue) -> {
NoxesiumMod.getInstance().getConfig().showOptimizationOverlay = newValue;
NoxesiumMod.getInstance().getConfig().showUiDebugOverlay = newValue;
NoxesiumMod.getInstance().getConfig().save();
}
);

public static OptionInstance<Boolean> experimentalPatches() {
return experimentalPatches;
public static OptionInstance<Boolean> enableUiLimiting() {
return enableUiLimiting;
}

public static OptionInstance<Boolean> enableDynamicUiLimiting() {
return enableDynamicUiLimiting;
}

public static OptionInstance<Boolean> fpsOverlay() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ protected void addOptions() {
NoxesiumOptions.playerGlowingKeybinds(),
NoxesiumOptions.qibSystemDebugVisuals()
);
this.list.addBig(NoxesiumOptions.experimentalPatches());
this.list.addBig(NoxesiumOptions.enableUiLimiting());
this.list.addSmall(
NoxesiumOptions.maxUiFramerate(),
NoxesiumOptions.enableDynamicUiLimiting(),
NoxesiumOptions.optimizationOverlay()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ public static String getSpatialTreeState(Entity entity) {
* Rebuilds the model if applicable.
*/
public static void rebuild() {
if (rebuilding.get()) return;
if (!needsRebuilding.get()) return;

rebuilding.set(true);
if (!rebuilding.compareAndSet(false, true)) return;
try {
var world = Minecraft.getInstance().level;
if (world == null) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ public class ServerRules {
*/
public static ClientServerRule<ItemStack> HAND_ITEM_OVERRIDE = register(new ItemStackServerRule(ServerRuleIndices.HAND_ITEM_OVERRIDE));

/**
* Allows server to override whether experimental UI optimizations are on.
*/
public static ClientServerRule<Boolean> DISABLE_UI_OPTIMIZATIONS = register(new BooleanServerRule(ServerRuleIndices.DISABLE_UI_OPTIMIZATIONS, false));

/**
* Moves the handheld map to be shown in the top left/right corner instead of
* in the regular hand slot.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import com.noxcrew.noxesium.feature.ui.layer.NoxesiumLayer;

import java.util.List;

/**
* Holds a layer and a reference to its group.
*/
public record LayerWithReference(
int index,
NoxesiumLayer.Layer layer,
NoxesiumLayer.NestedLayers group
List<NoxesiumLayer.NestedLayers> groups
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,6 @@ public List<NoxesiumLayer> layers() {
return inner.layers();
}

/**
* Returns the condition of this group.
*/
public BooleanSupplier condition() {
return condition;
}

/**
* Returns whether this group's condition has recently changed.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.noxcrew.noxesium.feature.ui.layer;

import com.noxcrew.noxesium.NoxesiumMod;
import com.noxcrew.noxesium.feature.ui.LayerWithReference;
import com.noxcrew.noxesium.feature.ui.render.NoxesiumUiRenderState;
import com.noxcrew.noxesium.feature.ui.render.SharedVertexBuffer;
Expand All @@ -21,7 +20,7 @@
* A custom implementation of layered draw that persists groupings
* of layers to properly be able to distinguish between them.
*/
public class NoxesiumLayeredDraw implements LayeredDraw.Layer, NoxesiumRenderStateHolder<NoxesiumUiRenderState> {
public class NoxesiumLayeredDraw implements NoxesiumRenderStateHolder<NoxesiumUiRenderState> {

private final List<NoxesiumLayer> layers = new ArrayList<>();
private final List<NoxesiumLayer.NestedLayers> subgroups = new ArrayList<>();
Expand Down Expand Up @@ -59,28 +58,24 @@ public void add(NoxesiumLayer layer) {
update();
}

@Override
public void render(GuiGraphics guiGraphics, @NotNull DeltaTracker deltaTracker) {
// If experimental patches are disabled we ignore all custom logic.
if (NoxesiumMod.getInstance().getConfig().shouldDisableExperimentalPerformancePatches()) {
/**
* Resets any lingering data held by this object.
*/
public void reset() {
if (state != null) {
// Destroy the state if it exists
if (state != null) {
state.close();
state = null;
}
state.close();
state = null;

// Ensure everything is disabled!
SharedVertexBuffer.reset();

// Directly draw everything to the screen
guiGraphics.pose().pushPose();
for (var layer : layers) {
renderLayerDirectly(guiGraphics, deltaTracker, layer);
}
guiGraphics.pose().popPose();
return;
}
}

/**
* Renders the layered draw's contents. Returns false if it failed to draw properly.
*/
public boolean render(GuiGraphics guiGraphics, @NotNull DeltaTracker deltaTracker) {
// Defer rendering the object to the current state, this holds all necessary information
// for rendering.
if (state == null) {
Expand All @@ -89,27 +84,7 @@ public void render(GuiGraphics guiGraphics, @NotNull DeltaTracker deltaTracker)
// uses them for sub-groups as well.
state = new NoxesiumUiRenderState();
}
state.render(guiGraphics, deltaTracker, this);
}

/**
* Renders a single layer directly, avoiding all custom UI optimizations.
*/
private void renderLayerDirectly(GuiGraphics guiGraphics, DeltaTracker deltaTracker, NoxesiumLayer layer) {
switch (layer) {
case NoxesiumLayer.Layer single -> {
single.layer().render(guiGraphics, deltaTracker);
guiGraphics.pose().translate(0f, 0f, LayeredDraw.Z_SEPARATION);
}
case NoxesiumLayer.NestedLayers group -> {
if (group.condition().getAsBoolean()) {
for (var subLayer : group.layers()) {
renderLayerDirectly(guiGraphics, deltaTracker, subLayer);
}
guiGraphics.pose().translate(0f, 0f, LayeredDraw.Z_SEPARATION);
}
}
}
return state.render(guiGraphics, deltaTracker, this);
}

/**
Expand All @@ -121,7 +96,7 @@ public List<LayerWithReference> flatten() {
for (var layer : layers) {
switch (layer) {
case NoxesiumLayer.Layer single -> result.add(new LayerWithReference(result.size() + offset.getValue(), single, null));
case NoxesiumLayer.NestedLayers group -> process(group, result, offset);
case NoxesiumLayer.NestedLayers group -> process(group, result, offset, new ArrayList<>());
}
}
return result;
Expand All @@ -130,11 +105,14 @@ public List<LayerWithReference> flatten() {
/**
* Adds the contents of the layer group to the given list.
*/
private void process(NoxesiumLayer.NestedLayers target, List<LayerWithReference> list, MutableInt offset) {
private void process(NoxesiumLayer.NestedLayers target, List<LayerWithReference> list, MutableInt offset, List<NoxesiumLayer.NestedLayers> nested) {
var nestedCopy = new ArrayList<>(nested);
nestedCopy.add(target);

for (var layer : target.layers()) {
switch (layer) {
case NoxesiumLayer.Layer single -> list.add(new LayerWithReference(list.size() + offset.getValue(), single, target));
case NoxesiumLayer.NestedLayers group -> process(group, list, offset);
case NoxesiumLayer.Layer single -> list.add(new LayerWithReference(list.size() + offset.getValue(), single, nestedCopy));
case NoxesiumLayer.NestedLayers group -> process(group, list, offset, nestedCopy);
}
}

Expand Down
Loading

0 comments on commit a1dfa92

Please sign in to comment.