Skip to content

Commit

Permalink
Add group merging
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeltumn committed Nov 12, 2024
1 parent 8bba210 commit 5af67cb
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,19 @@ public int getTextureId() {
}

/**
* Returns whether this element is always changing. Used to determine
* Returns whether this element is ready to be considered
* for group merging/joining.
*/
public boolean isReady() {
return !needsRedraw && buffer.hasValidPBO();
}

/**
* Returns whether this element is often changing. Used to determine
* when it should be split up this buffer.
*/
public boolean isAlwaysChanging() {
return failedCheckCount >= 50;
public boolean isOftenChanging() {
return failedCheckCount >= 20;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ public boolean[] emptySnapshots() {
return emptyPbos;
}

/**
* Returns whether this buffer has at least one valid PBO.
*/
public boolean hasValidPBO() {
return validPbos > 0;
}

/**
* Marks down that a new PBO should be updated.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,22 @@ public void removeLayers(Collection<LayerWithReference> layers) {
* Returns whether this group should be split up.
*/
public boolean shouldSplit() {
return size() > 1 && dynamic.isAlwaysChanging();
return size() > 1 && dynamic.isReady() && dynamic.isOftenChanging();
}

/**
* Returns whether this group can merge with another.
*/
public boolean canMerge(ElementBufferGroup other) {
// If either needs a redraw we don't edit them as
// things might be inaccurate!
if (!dynamic.isReady() || !other.dynamic.isReady()) return false;

// Don't allow creating groups larger than 6
if (size() + other.size() > 6) return false;

// Don't allow merging when render fps is too different
return Math.abs(dynamic.renderFramerate() - other.dynamic.renderFramerate()) < 10;
}

/**
Expand Down Expand Up @@ -103,7 +118,6 @@ public ElementBufferGroup split() {
*/
public void join(ElementBufferGroup other) {
addLayers(other.layers);
other.close();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

/**
Expand All @@ -15,6 +16,9 @@
public class NoxesiumUiRenderState implements NoxesiumRenderState {

private final List<ElementBufferGroup> groups = new CopyOnWriteArrayList<>();
private final Random random = new Random();
private final double updateFps = 0.5;
private long nextUpdate = -1;
private int lastSize = 0;

/**
Expand Down Expand Up @@ -53,12 +57,36 @@ public void render(GuiGraphics guiGraphics, DeltaTracker deltaTracker, NoxesiumL
group.update();
}

// Try to split up or merge together groups
var index = 0;
while (index < groups.size()) {
var group = groups.get(index++);
if (group.shouldSplit()) {
groups.add(index++, group.split());
// Try to split up or merge together groups, but don't run this too frequently!
if (nextUpdate == -1) {
nextUpdate = nanoTime;
}
if (nanoTime >= nextUpdate) {
// Schedule when we can next update the groups
nextUpdate = nanoTime + (long) Math.floor(((1 / updateFps) * random.nextDouble() * 1000000000));

// Iterate through all groups and make changes
var index = 0;
while (index < groups.size()) {
var group = groups.get(index++);

// Try to merge if there are neighboring groups
if (index > 2 && index < groups.size()) {
if (group.canMerge(groups.get(index))) {
group.join(groups.get(index));
groups.remove(index).close();
index--;
} else if (groups.get(index - 2).canMerge(group)) {
groups.get(index - 2).join(group);
groups.remove(index - 1).close();
index--;
}
}

// Try to split up the group
if (group.shouldSplit()) {
groups.add(index++, group.split());
}
}
}

Expand Down

0 comments on commit 5af67cb

Please sign in to comment.