Skip to content

Commit

Permalink
sort render lists separately when doing sync rendering, since they're…
Browse files Browse the repository at this point in the history
… not automatically correctly ordered since they're not coming from a tree
  • Loading branch information
douira committed Nov 25, 2024
1 parent 158cd78 commit 9443fbc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private void renderSync(Camera camera, Viewport viewport, boolean spectator) {
this.trees.put(CullType.FRUSTUM, tree);
this.renderTree = tree;

this.renderLists = tree.createRenderLists();
this.renderLists = tree.createRenderLists(viewport);

// remove the other trees, they're very wrong by now
this.trees.remove(CullType.WIDE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* The async visible chunk collector is passed into a section tree to collect visible chunks.
*/
public class VisibleChunkCollectorAsync implements SectionTree.VisibleSectionVisitor {
public class VisibleChunkCollectorAsync implements SectionTree.VisibleSectionVisitor {
private final ObjectArrayList<ChunkRenderList> sortedRenderLists;

private final RenderRegionManager regions;
Expand Down Expand Up @@ -51,44 +51,6 @@ public void visit(int x, int y, int z) {
renderList.add(sectionIndex);
}

private static int[] sortItems = new int[RenderRegion.REGION_SIZE];

public SortedRenderLists createSortedRenderLists(Viewport viewport) {
// sort the regions by distance to fix rare region ordering bugs
var transform = viewport.getTransform();
var cameraX = transform.intX >> (4 + RenderRegion.REGION_WIDTH_SH);
var cameraY = transform.intY >> (4 + RenderRegion.REGION_HEIGHT_SH);
var cameraZ = transform.intZ >> (4 + RenderRegion.REGION_LENGTH_SH);
var size = this.sortedRenderLists.size();

if (sortItems.length < size) {
sortItems = new int[size];
}

for (var i = 0; i < size; i++) {
var region = this.sortedRenderLists.get(i).getRegion();
var x = Math.abs(region.getX() - cameraX);
var y = Math.abs(region.getY() - cameraY);
var z = Math.abs(region.getZ() - cameraZ);
sortItems[i] = (x + y + z) << 16 | i;
}

IntArrays.unstableSort(sortItems, 0, size);

var sorted = new ObjectArrayList<ChunkRenderList>(size);
for (var i = 0; i < size; i++) {
var key = sortItems[i];
var renderList = this.sortedRenderLists.get(key & 0xFFFF);
sorted.add(renderList);
}

for (var list : sorted) {
list.sortSections(transform, sortItems);
}

return new SortedRenderLists(sorted);
}

public SortedRenderLists createRenderLists() {
return new SortedRenderLists(this.sortedRenderLists);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.caffeinemc.mods.sodium.client.render.chunk.lists;

import it.unimi.dsi.fastutil.ints.IntArrays;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.caffeinemc.mods.sodium.client.render.chunk.RenderSection;
import net.caffeinemc.mods.sodium.client.render.chunk.RenderSectionFlags;
Expand Down Expand Up @@ -41,7 +42,41 @@ public void visit(RenderSection section) {
}
}

public SortedRenderLists createRenderLists() {
return new SortedRenderLists(this.sortedRenderLists);
private static int[] sortItems = new int[RenderRegion.REGION_SIZE];

public SortedRenderLists createRenderLists(Viewport viewport) {
// sort the regions by distance to fix rare region ordering bugs
var transform = viewport.getTransform();
var cameraX = transform.intX >> (4 + RenderRegion.REGION_WIDTH_SH);
var cameraY = transform.intY >> (4 + RenderRegion.REGION_HEIGHT_SH);
var cameraZ = transform.intZ >> (4 + RenderRegion.REGION_LENGTH_SH);
var size = this.sortedRenderLists.size();

if (sortItems.length < size) {
sortItems = new int[size];
}

for (var i = 0; i < size; i++) {
var region = this.sortedRenderLists.get(i).getRegion();
var x = Math.abs(region.getX() - cameraX);
var y = Math.abs(region.getY() - cameraY);
var z = Math.abs(region.getZ() - cameraZ);
sortItems[i] = (x + y + z) << 16 | i;
}

IntArrays.unstableSort(sortItems, 0, size);

var sorted = new ObjectArrayList<ChunkRenderList>(size);
for (var i = 0; i < size; i++) {
var key = sortItems[i];
var renderList = this.sortedRenderLists.get(key & 0xFFFF);
sorted.add(renderList);
}

for (var list : sorted) {
list.sortSections(transform, sortItems);
}

return new SortedRenderLists(sorted);
}
}

0 comments on commit 9443fbc

Please sign in to comment.