Skip to content

Commit d98c30d

Browse files
authored
Use level's configured sea height for chunk mesh estimation heuristics (#3241)
Previously the code assumed the sea height was always y=62.
1 parent 77c3109 commit d98c30d

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public class RenderSectionManager {
7676

7777
private final ConcurrentLinkedDeque<ChunkJobResult<? extends BuilderTaskOutput>> buildResults = new ConcurrentLinkedDeque<>();
7878
private final JobDurationEstimator jobDurationEstimator = new JobDurationEstimator();
79-
private final MeshTaskSizeEstimator meshTaskSizeEstimator = new MeshTaskSizeEstimator();
79+
private final MeshTaskSizeEstimator meshTaskSizeEstimator;
8080
private final UploadDurationEstimator jobUploadDurationEstimator = new UploadDurationEstimator();
8181
private ChunkJobCollector lastBlockingCollector;
8282
private int thisFrameBlockingTasks;
@@ -116,6 +116,8 @@ public class RenderSectionManager {
116116
private final RemovableMultiForest renderableSectionTree;
117117

118118
public RenderSectionManager(ClientLevel level, int renderDistance, SortBehavior sortBehavior, CommandList commandList) {
119+
this.meshTaskSizeEstimator = new MeshTaskSizeEstimator(level);
120+
119121
this.chunkRenderer = new DefaultChunkRenderer(RenderDevice.INSTANCE, ChunkMeshFormats.COMPACT);
120122

121123
this.level = level;
@@ -386,7 +388,7 @@ private boolean processChunkBuildResults(ArrayList<BuilderTaskOutput> results) {
386388
touchedSectionInfo |= this.updateSectionInfo(result.render, chunkBuildOutput.info);
387389

388390
result.render.setLastMeshResultSize(resultSize);
389-
this.meshTaskSizeEstimator.addData(MeshResultSize.forSection(result.render, resultSize));
391+
this.meshTaskSizeEstimator.addData(this.meshTaskSizeEstimator.resultForSection(result.render, resultSize));
390392

391393
if (chunkBuildOutput.translucentData != null) {
392394
this.sortTriggering.integrateTranslucentData(oldData, chunkBuildOutput.translucentData, this.cameraPosition, this::scheduleSort);

common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/compile/estimation/MeshResultSize.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,29 @@ public enum SectionCategory {
1212
SURFACE,
1313
HIGH;
1414

15-
public static SectionCategory forSection(RenderSection section) {
15+
public static SectionCategory forSection(RenderSection section, int seaLevelChunk) {
1616
var sectionY = section.getChunkY();
17-
if (sectionY < 0) {
17+
18+
// Roughly classify type of chunk based on Y level relative to sea level:
19+
// Water level chunks are likely to have different meshes from those below and those above.
20+
// Very low chunks are again different because they aren't going to include the underwater terrain (just caves).
21+
// Very high chunks are (at least locally) different because they are likely to be mostly air with some terrain poking through, or just buildings/jungle tree tops.
22+
if (sectionY == seaLevelChunk) {
23+
return WATER_LEVEL;
24+
}
25+
if (sectionY < seaLevelChunk - 4) {
1826
return LOW;
19-
} else if (sectionY < 3) {
27+
}
28+
if (sectionY < seaLevelChunk) {
2029
return UNDERGROUND;
21-
} else if (sectionY == 3) {
22-
return WATER_LEVEL;
23-
} else if (sectionY < 7) {
30+
}
31+
if (sectionY < seaLevelChunk + 3) {
2432
return SURFACE;
25-
} else {
26-
return HIGH;
2733
}
34+
return HIGH;
2835
}
2936
}
3037

31-
public static MeshResultSize forSection(RenderSection section, long resultSize) {
32-
return new MeshResultSize(SectionCategory.forSection(section), resultSize);
33-
}
34-
3538
@Override
3639
public SectionCategory category() {
3740
return this.category;

common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/compile/estimation/MeshTaskSizeEstimator.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,32 @@
22

33
import net.caffeinemc.mods.sodium.client.render.chunk.RenderSection;
44
import net.caffeinemc.mods.sodium.client.render.chunk.region.RenderRegion;
5+
import net.minecraft.client.Minecraft;
6+
import net.minecraft.client.multiplayer.ClientLevel;
57

68
import java.util.EnumMap;
79
import java.util.Map;
810

911
public class MeshTaskSizeEstimator extends Average1DEstimator<MeshResultSize.SectionCategory> {
1012
public static final float NEW_DATA_RATIO = 0.02f;
13+
14+
private final int seaLevelChunk;
1115

12-
public MeshTaskSizeEstimator() {
16+
public MeshTaskSizeEstimator(ClientLevel level) {
1317
super(NEW_DATA_RATIO, RenderRegion.SECTION_BUFFER_ESTIMATE);
18+
this.seaLevelChunk = level.getSeaLevel() >> 4;
1419
}
1520

1621
public long estimateSize(RenderSection section) {
1722
var lastResultSize = section.getLastMeshResultSize();
1823
if (lastResultSize != MeshResultSize.NO_DATA) {
1924
return lastResultSize;
2025
}
21-
return this.predict(MeshResultSize.SectionCategory.forSection(section));
26+
return this.predict(MeshResultSize.SectionCategory.forSection(section, this.seaLevelChunk));
27+
}
28+
29+
public MeshResultSize resultForSection(RenderSection section, long resultSize) {
30+
return new MeshResultSize(MeshResultSize.SectionCategory.forSection(section, this.seaLevelChunk), resultSize);
2231
}
2332

2433
@Override

0 commit comments

Comments
 (0)