Skip to content

Commit

Permalink
Limit translucency sorting to 6 chunk radius
Browse files Browse the repository at this point in the history
Prevents lag spikes on weak GPUs
  • Loading branch information
embeddedt committed Oct 15, 2023
1 parent 7e7ed82 commit 4e4bce2
Showing 1 changed file with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import repack.joml.Matrix4f;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -122,6 +123,7 @@ public void render(ChunkRenderMatrices matrices, CommandList commandList,
}

private final Matrix4f cachedModelViewMatrix = new Matrix4f();
private final List<RenderSection> sectionsToCompute = new ArrayList<>();

private void computeTranslucency(ChunkRenderMatrices matrices, CommandList commandList,
ChunkRenderList list, BlockRenderPass pass,
Expand Down Expand Up @@ -154,6 +156,7 @@ private void computeTranslucency(ChunkRenderMatrices matrices, CommandList comma

boolean runCompute = true;
int regionsComputed = 0;

//We want compute to run beginning with the closest chunks
for (Map.Entry<RenderRegion, List<RenderSection>> entry : sortedRegions(list, false)) {
RenderRegion region = entry.getKey();
Expand All @@ -167,7 +170,16 @@ private void computeTranslucency(ChunkRenderMatrices matrices, CommandList comma
}

if (region.getNeedsTranslucencyCompute() && !regionSections.isEmpty()) {
if (!buildDrawBatches(regionSections, pass, camera)) {
sectionsToCompute.clear();

for(RenderSection section : regionSections) {
if(section.getGraphicsState(pass) != null
&& section.getSquaredDistance(camera.blockX, camera.blockY, camera.blockZ) < 96*96) {
sectionsToCompute.add(section);
}
}

if (sectionsToCompute.isEmpty() || !buildDrawBatches(sectionsToCompute, pass, camera)) {
continue;
}
float x = getCameraTranslation(region.getOriginX(), camera.blockX, camera.deltaX);
Expand All @@ -184,12 +196,7 @@ private void computeTranslucency(ChunkRenderMatrices matrices, CommandList comma
runCompute = compute.execute(commandList, batches[0], arenas);
region.setNeedsTranslucencyCompute(false);
if(runCompute) {
// Count the number of sorted sections
//noinspection ForLoopReplaceableByForEach
for(int i = 0; i < regionSections.size(); i++) {
if(regionSections.get(i).getGraphicsState(BlockRenderPass.TRANSLUCENT) != null)
regionsComputed++;
}
regionsComputed += sectionsToCompute.size();
if(regionsComputed >= 15)
runCompute = false; // do not continue sorting for the rest of the frame
}
Expand Down

0 comments on commit 4e4bce2

Please sign in to comment.