Skip to content

Commit df32670

Browse files
committed
fix sorting by using correct section pos calculation and improve frustum task list update
1 parent b3d4b7c commit df32670

File tree

5 files changed

+31
-33
lines changed

5 files changed

+31
-33
lines changed

common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/RenderSectionManager.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public class RenderSectionManager {
106106
private boolean needsGraphUpdate = true;
107107
private boolean needsRenderListUpdate = true;
108108
private boolean cameraChanged = false;
109+
private boolean needsFrustumTaskListUpdate = true;
109110

110111
private @Nullable BlockPos cameraBlockPos;
111112
private @Nullable Vector3dc cameraPosition;
@@ -154,6 +155,7 @@ public void updateRenderLists(Camera camera, Viewport viewport, boolean spectato
154155

155156
this.frame += 1;
156157
this.needsRenderListUpdate |= this.cameraChanged;
158+
this.needsFrustumTaskListUpdate |= this.needsRenderListUpdate;
157159

158160
// do sync bfs based on update immediately (flawless frames) or if the camera moved too much
159161
var shouldRenderSync = this.cameraTimingControl.getShouldRenderSync(camera);
@@ -186,11 +188,15 @@ public void updateRenderLists(Camera camera, Viewport viewport, boolean spectato
186188

187189
this.scheduleAsyncWork(camera, viewport, spectator);
188190

191+
if (this.needsFrustumTaskListUpdate) {
192+
this.updateFrustumTaskList(viewport);
193+
}
189194
if (this.needsRenderListUpdate) {
190195
processRenderListUpdate(viewport);
191196
}
192197

193198
this.needsRenderListUpdate = false;
199+
this.needsFrustumTaskListUpdate = false;
194200
this.needsGraphUpdate = false;
195201
this.cameraChanged = false;
196202
}
@@ -341,7 +347,7 @@ private CullType[] getScheduleOrder() {
341347

342348
private static final LongArrayList timings = new LongArrayList();
343349

344-
private void processRenderListUpdate(Viewport viewport) {
350+
private void updateFrustumTaskList(Viewport viewport) {
345351
// schedule generating a frustum task list if there's no frustum tree task running
346352
if (this.globalTaskTree != null) {
347353
var frustumTaskListPending = false;
@@ -359,7 +365,9 @@ private void processRenderListUpdate(Viewport viewport) {
359365
this.pendingTasks.add(task);
360366
}
361367
}
368+
}
362369

370+
private void processRenderListUpdate(Viewport viewport) {
363371
// pick the narrowest up-to-date tree, if this tree is insufficiently up to date we would've switched to sync bfs earlier
364372
SectionTree bestTree = null;
365373
boolean bestTreeValid = false;
@@ -907,7 +915,6 @@ public int getVisibleChunkCount() {
907915
// TODO: this fixes very delayed tasks, but it still regresses on same-frame tasks that don't get to run in time because the frustum task collection task takes at least one (and usually only one) frame to run
908916
// maybe intercept tasks that are scheduled in zero- or one-frame defer mode?
909917
// collect and prioritize regardless of visibility if it's an important defer mode?
910-
// TODO: vertical sorting seems to be broken?
911918
private ChunkUpdateType upgradePendingUpdate(RenderSection section, ChunkUpdateType type) {
912919
var current = section.getPendingUpdate();
913920
type = ChunkUpdateType.getPromotionUpdateType(current, type);
@@ -917,6 +924,7 @@ private ChunkUpdateType upgradePendingUpdate(RenderSection section, ChunkUpdateT
917924
// if the section received a new task, mark in the task tree so an update can happen before a global cull task runs
918925
if (this.globalTaskTree != null && type != null && current == null) {
919926
this.globalTaskTree.markSectionTask(section);
927+
this.needsFrustumTaskListUpdate = true;
920928
}
921929

922930
return type;
@@ -955,13 +963,7 @@ public void scheduleRebuild(int x, int y, int z, boolean important) {
955963
pendingUpdate = ChunkUpdateType.REBUILD;
956964
}
957965

958-
pendingUpdate = ChunkUpdateType.getPromotionUpdateType(section.getPendingUpdate(), pendingUpdate);
959-
if (pendingUpdate != null) {
960-
section.setPendingUpdate(pendingUpdate, this.lastFrameAtTime);
961-
962-
// force update to schedule rebuild task on this section
963-
this.markGraphDirty();
964-
}
966+
this.upgradePendingUpdate(section, pendingUpdate);
965967
}
966968
}
967969

common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/lists/PendingTaskCollector.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,22 @@ public PendingTaskCollector(Viewport viewport, float buildDistance, boolean frus
4141
this.isFrustumTested = frustumTested;
4242
var offsetDistance = Mth.ceil(buildDistance / 16.0f) + 1;
4343

44-
var transform = viewport.getTransform();
45-
4644
// the offset applied to section coordinates to encode their position in the octree
47-
var cameraSectionX = transform.intX >> 4;
48-
var cameraSectionY = transform.intY >> 4;
49-
var cameraSectionZ = transform.intZ >> 4;
45+
var sectionPos = viewport.getChunkCoord();
46+
var cameraSectionX = sectionPos.getX();
47+
var cameraSectionY = sectionPos.getY();
48+
var cameraSectionZ = sectionPos.getZ();
5049
this.baseOffsetX = cameraSectionX - offsetDistance;
5150
this.baseOffsetY = cameraSectionY - offsetDistance;
5251
this.baseOffsetZ = cameraSectionZ - offsetDistance;
5352

5453
this.invMaxDistance = PROXIMITY_FACTOR / buildDistance;
5554

5655
if (frustumTested) {
57-
this.cameraX = transform.intX;
58-
this.cameraY = transform.intY;
59-
this.cameraZ = transform.intZ;
56+
var blockPos = viewport.getBlockCoord();
57+
this.cameraX = blockPos.getX();
58+
this.cameraY = blockPos.getY();
59+
this.cameraZ = blockPos.getZ();
6060
} else {
6161
this.cameraX = (cameraSectionX << 4);
6262
this.cameraY = (cameraSectionY << 4);

common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/occlusion/SectionTree.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,10 @@ public int getFrame() {
4242
}
4343

4444
public boolean isValidFor(Viewport viewport, float searchDistance) {
45-
var transform = viewport.getTransform();
46-
var cameraSectionX = transform.intX >> 4;
47-
var cameraSectionY = transform.intY >> 4;
48-
var cameraSectionZ = transform.intZ >> 4;
49-
return this.cameraX >> 4 == cameraSectionX &&
50-
this.cameraY >> 4 == cameraSectionY &&
51-
this.cameraZ >> 4 == cameraSectionZ &&
45+
var cameraPos = viewport.getChunkCoord();
46+
return this.cameraX >> 4 == cameraPos.getX() &&
47+
this.cameraY >> 4 == cameraPos.getY() &&
48+
this.cameraZ >> 4 == cameraPos.getZ() &&
5249
this.buildDistance >= searchDistance;
5350
}
5451

common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/tree/AbstractTraversableMultiForest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ public void calculateReduced() {
2020

2121
@Override
2222
public void traverse(SectionTree.VisibleSectionVisitor visitor, Viewport viewport, float distanceLimit) {
23-
var transform = viewport.getTransform();
24-
var cameraSectionX = transform.intX >> 4;
25-
var cameraSectionY = transform.intY >> 4;
26-
var cameraSectionZ = transform.intZ >> 4;
23+
var cameraPos = viewport.getChunkCoord();
24+
var cameraSectionX = cameraPos.getX();
25+
var cameraSectionY = cameraPos.getY();
26+
var cameraSectionZ = cameraPos.getZ();
2727

2828
// sort the trees by distance from the camera by sorting a packed index array.
2929
var items = new int[this.trees.length];

common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/tree/TraversableTree.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,12 @@ public void traverse(SectionTree.VisibleSectionVisitor visitor, Viewport viewpor
6161
this.viewport = viewport;
6262
this.distanceLimit = distanceLimit;
6363

64-
var transform = viewport.getTransform();
65-
6664
// + 1 to offset section position to compensate for shifted global offset
6765
// adjust camera block position to account for fractional part of camera position
68-
this.cameraOffsetX = ((transform.intX + (int) Math.signum(transform.fracX)) >> 4) - this.offsetX + 1;
69-
this.cameraOffsetY = ((transform.intY + (int) Math.signum(transform.fracY)) >> 4) - this.offsetY + 1;
70-
this.cameraOffsetZ = ((transform.intZ + (int) Math.signum(transform.fracZ)) >> 4) - this.offsetZ + 1;
66+
var sectionPos = viewport.getChunkCoord();
67+
this.cameraOffsetX = sectionPos.getX() - this.offsetX + 1;
68+
this.cameraOffsetY = sectionPos.getY() - this.offsetY + 1;
69+
this.cameraOffsetZ = sectionPos.getZ() - this.offsetZ + 1;
7170

7271
// everything is already inside the distance limit if the build distance is smaller
7372
var initialInside = this.distanceLimit >= buildDistance ? INSIDE_DISTANCE : 0;

0 commit comments

Comments
 (0)