Skip to content

Commit

Permalink
Split up the PBO buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeltumn committed Jan 7, 2025
1 parent d4d929a commit 2fdd846
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class NoxesiumConfig {

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

/** Tests whether at least OpenGL 4.4 is present. */
public static boolean supportsDynamicUiLimiting = false;

public boolean resetToggleKeys = false;
public BooleanOrDefault renderMapsInUi = BooleanOrDefault.DEFAULT;
public boolean showFpsOverlay = false;
Expand All @@ -33,6 +36,13 @@ public class NoxesiumConfig {
public boolean showUiDebugOverlay = false;
public int maxUiFramerate = 60;

/**
* Whether dynamic UI limiting should be used.
*/
public boolean shouldUseDynamicUiLimiting() {
return supportsDynamicUiLimiting && enableUiLimiting;
}

/**
* Returns whether to render maps in the UI.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ public class NoxesiumOptions {
private static final OptionInstance<Boolean> enableDynamicUiLimiting = OptionInstance.createBoolean(
"noxesium.options.dynamic_ui_limiting.name",
OptionInstance.cachedConstantTooltip(
Component.translatable("noxesium.options.dynamic_ui_limiting.tooltip")),
NoxesiumConfig.supportsDynamicUiLimiting
? Component.translatable("noxesium.options.dynamic_ui_limiting.tooltip")
: Component.translatable("noxesium.options.dynamic_ui_limiting.disabled")),
NoxesiumMod.getInstance().getConfig().enableDynamicUiLimiting,
(newValue) -> {
NoxesiumMod.getInstance().getConfig().enableDynamicUiLimiting = newValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,13 @@ protected void addOptions() {
NoxesiumOptions.maxUiFramerate(),
NoxesiumOptions.enableDynamicUiLimiting(),
NoxesiumOptions.optimizationOverlay());

// Disable the dynamic UI limiting button
if (!NoxesiumConfig.supportsDynamicUiLimiting) {
var button = this.list.findOption(NoxesiumOptions.enableDynamicUiLimiting());
if (button != null) {
button.active = false;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.noxcrew.noxesium.feature.ui.render.api.BlendStateHook;
import com.noxcrew.noxesium.feature.ui.render.api.BufferData;
import java.io.Closeable;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.client.gui.GuiGraphics;
Expand All @@ -31,6 +30,7 @@ public class DynamicElement implements Closeable, BlendStateHook {
private boolean canCheck = true;
private boolean lastBlending = true;
private boolean hasChangedLayers = false;
private boolean dynamic = false;

private int renders = 0;
private long nextCheck = System.nanoTime() + 1000000000;
Expand All @@ -51,6 +51,20 @@ public DynamicElement() {
buffers.add(new ElementBuffer());
}

/**
* Returns whether this element should check for dynamic UI.
*/
private boolean shouldCheck() {
if (canCheck) {
if (!NoxesiumMod.getInstance().getConfig().shouldUseDynamicUiLimiting()) {
canCheck = false;
return false;
}
return true;
}
return false;
}

/**
* Resets the display fps back to the maximum.
*/
Expand Down Expand Up @@ -152,7 +166,7 @@ public boolean isReady() {
if (needsRedraw) return false;
if (isNotEmpty()) {
for (var buffer : buffers) {
if (!buffer.hasValidPBO()) {
if (buffer instanceof PBOElementBuffer pboBuffer && !pboBuffer.hasValidPBO()) {
return false;
}
}
Expand Down Expand Up @@ -198,17 +212,25 @@ public boolean isMergeable() {
* Process recently taken snapshots to determine changes.
*/
public void tick() {
// Reset dynamic UI if it's not enabled
if (!NoxesiumMod.getInstance().getConfig().shouldUseDynamicUiLimiting()) {
if (dynamic) {}
return;
}

// Determine if all buffers are the same,
// return the entire method if any buffer is not ready.
var verdict = !hasChangedLayers;
if (isNotEmpty()) {
for (var buffer : buffers) {
// Process the snapshots
var snapshots = buffer.snapshots();
if (snapshots == null) return;
if (!compare(snapshots[0], snapshots[1])) {
verdict = false;
break;
if (buffer instanceof PBOElementBuffer pboBuffer) {
// Process the snapshots
var snapshots = pboBuffer.snapshots();
if (snapshots == null) return;
if (!snapshots[0].equals(snapshots[1])) {
verdict = false;
break;
}
}
}
}
Expand Down Expand Up @@ -245,7 +267,9 @@ public void tick() {
// Request new PBOs from all buffers
if (isNotEmpty()) {
for (var buffer : buffers) {
buffer.requestNewPBO();
if (buffer instanceof PBOElementBuffer pboBuffer) {
pboBuffer.requestNewPBO();
}
}
}
}
Expand All @@ -256,7 +280,9 @@ public void tick() {
public boolean update(long nanoTime, GuiGraphics guiGraphics, Runnable draw) {
// Always start by awaiting the GPU fence
for (var buffer : buffers) {
buffer.awaitFence();
if (buffer instanceof PBOElementBuffer pboBuffer) {
pboBuffer.awaitFence();
}
}

// Determine if we are at the next render threshold yet, otherwise
Expand Down Expand Up @@ -302,7 +328,7 @@ public boolean update(long nanoTime, GuiGraphics guiGraphics, Runnable draw) {
}
bufferZeroInvalid = firstUnusedBufferIndex == 0;

// Unset check
// Unset check once we're done with this frame
if (canCheck) {
canCheck = false;
}
Expand Down Expand Up @@ -412,8 +438,8 @@ public boolean changeFunc(int srcRgb, int dstRgb, int srcAlpha, int dstAlpha) {
private void trySnapshotBuffer() {
if (elementsWereDrawn && canCheck) {
var target = getBuffer(bufferIndex);
if (target.canSnapshot()) {
target.snapshot();
if (target instanceof PBOElementBuffer pboBuffer && pboBuffer.canSnapshot()) {
pboBuffer.snapshot();
}
}
}
Expand All @@ -424,11 +450,4 @@ public void close() {
buffer.close();
}
}

/**
* Compares two frame snapshots.
*/
private boolean compare(ByteBuffer first, ByteBuffer second) {
return first.equals(second);
}
}
Loading

0 comments on commit 2fdd846

Please sign in to comment.