30-60% mob overlay CPU savings

This commit is contained in:
OrderedSet committed 2026-11-19 20:47:45 -08:00
1 parent 1bb550305c
commit ef310b30ce
3 files changed
+251 -33

No files matched your search

@@ -30,6 +30,8 @@ public class WorldOverlayRenderer implements IKeyStateTracker {
private static final MobOverlayScan MOB_OVERLAY_SCAN = new MobOverlayScan();
private static final MobOverlayFace MARKED_FACE = new MobOverlayFace();
private static final MobOverlayDisplayList MOB_OVERLAY_LIST = new MobOverlayDisplayList();
// the cells of one column, reused by each compile
private static final byte[] COLUMN_CELLS = new byte[2 * MobOverlayScan.MAX_VERTICAL_RADIUS + 1];
public static void reset() {
mobOverlay = 0;
@@ -111,28 +113,58 @@ public class WorldOverlayRenderer implements IKeyStateTracker {
}
/**
* Add a marked face to {@link #MOB_OVERLAY_LIST} for each block of {@link #MOB_OVERLAY_SCAN} where mobs can spawn.
* Add a marked face to {@link #MOB_OVERLAY_LIST} for each block of {@link #MOB_OVERLAY_SCAN} where mobs can spawn,
* in the chunk columns whose cells changed.
*/
private static void compileMobOverlayList(MobOverlayStyle style, long worldTicks) {
MOB_OVERLAY_LIST.beginCompile(MOB_OVERLAY_SCAN, style, worldTicks);
final int minX = MOB_OVERLAY_SCAN.getMinX();
final int minZ = MOB_OVERLAY_SCAN.getMinZ();
final int span = MOB_OVERLAY_SCAN.getSpan();
final int toChunkX = (minX + span - 1) >> MobOverlayScan.CHUNK_SHIFT;
final int toChunkZ = (minZ + span - 1) >> MobOverlayScan.CHUNK_SHIFT;
for (int chunkX = minX >> MobOverlayScan.CHUNK_SHIFT; chunkX <= toChunkX; chunkX++) {
for (int chunkZ = minZ >> MobOverlayScan.CHUNK_SHIFT; chunkZ <= toChunkZ; chunkZ++) {
if (!MOB_OVERLAY_LIST.beginChunk(MOB_OVERLAY_SCAN, chunkX, chunkZ)) continue;
MOB_OVERLAY_LIST.endChunk(compileChunkFaces(style, chunkX, chunkZ));
}
}
MOB_OVERLAY_LIST.endCompile();
}
/** @return whether the chunk column has a marked face */
private static boolean compileChunkFaces(MobOverlayStyle style, int chunkX, int chunkZ) {
final int CHUNK_WIDTH = 1 << MobOverlayScan.CHUNK_SHIFT;
final float alpha = style.computeAlpha();
final Tessellator tess = Tessellator.instance;
tess.startDrawing(style.glMode);
final int cornerX = chunkX << MobOverlayScan.CHUNK_SHIFT;
final int cornerZ = chunkZ << MobOverlayScan.CHUNK_SHIFT;
final int minX = MOB_OVERLAY_SCAN.getMinX();
final int minZ = MOB_OVERLAY_SCAN.getMinZ();
final int span = MOB_OVERLAY_SCAN.getSpan();
for (int i = 0; i < span; i++) {
for (int j = 0; j < span; j++) {
for (int k = 0; k < MOB_OVERLAY_SCAN.getHeight(); k++) {
if (MOB_OVERLAY_SCAN.readSpawnMode(i, k, j) == 0) continue;
final int height = MOB_OVERLAY_SCAN.getHeight();
boolean hasFaces = false;
tess.startDrawing(style.glMode);
for (int i = Math.max(cornerX - minX, 0); i < Math.min(cornerX + CHUNK_WIDTH - minX, span); i++) {
for (int j = Math.max(cornerZ - minZ, 0); j < Math.min(cornerZ + CHUNK_WIDTH - minZ, span); j++) {
MOB_OVERLAY_SCAN.readColumn(i, j, COLUMN_CELLS);
for (int k = 0; k < height; k++) {
if (MobOverlayScan.extractSpawnMode(COLUMN_CELLS[k]) == 0) continue;
MARKED_FACE.moveTo(i, k + MOB_OVERLAY_SCAN.readOverlayYOffset(i, k, j), j);
MARKED_FACE.moveTo(
minX + i - cornerX,
k + MobOverlayScan.computeOverlayYOffset(COLUMN_CELLS[k]),
minZ + j - cornerZ);
loadNeighbourModes(i, k, j);
MARKED_FACE.applyColor(style.colouring, alpha);
style.addVertices(MARKED_FACE);
hasFaces = true;
}
}
}
tess.draw();
MOB_OVERLAY_LIST.endCompile();
return hasFaces;
}
private static void beginMobOverlayDraw(MobOverlayStyle style) {
@@ -1,19 +1,40 @@
package codechicken.nei.moboverlay;
import java.util.Arrays;
import net.minecraft.client.renderer.GLAllocation;
import org.lwjgl.opengl.GL11;
/** The mob spawn overlay quads, compiled into a GL display list. */
/**
* The mob spawn overlay quads, compiled into one GL display list for each chunk column that overlaps the box of a
* {@link MobOverlayScan}. A compile rebuilds only the lists of the chunk columns whose
* {@link MobOverlayScan#readChunkVersion} changed. A parent list calls the chunk-column lists, so a draw is one
* {@code glCallList()}.
*/
public final class MobOverlayDisplayList {
private int listId = -1;
private static final int NO_LIST = -1;
// the first of `chunkSpan * chunkSpan` lists, in the order of the ring of `MobOverlayScan.readChunkVersion()`, then
// the parent list
private int firstListId = NO_LIST;
private int parentListId = NO_LIST;
private boolean hasParentList;
private int anchorX;
private int anchorY;
private int anchorZ;
private int chunkSpan;
private boolean[] isCompiled = new boolean[0];
private boolean[] hasFaces = new boolean[0];
private int[] compiledChunkX = new int[0];
private int[] compiledChunkZ = new int[0];
private int[] compiledMinY = new int[0];
private int[] compiledChunkVersion = new int[0];
private int modesVersion;
private MobOverlayStyle style;
private int minX;
private int minY;
private int minZ;
private long compiledTicks;
private int compilingChunk;
/**
* Compiles after a {@link MobOverlayScan#getModesVersion} change are debounced to one per {@code MIN_COMPILE_TICKS}
@@ -22,7 +43,7 @@ public final class MobOverlayDisplayList {
*/
public boolean isCompileDue(int currentModesVersion, MobOverlayStyle currentStyle, long worldTicks) {
final int MIN_COMPILE_TICKS = 5;
if (listId < 0 || style != currentStyle) return true;
if (firstListId == NO_LIST || style != currentStyle) return true;
// after a dimension change or a server switch, `worldTicks` can be lower than `compiledTicks`
final boolean isWaitOver = worldTicks - compiledTicks >= MIN_COMPILE_TICKS || worldTicks < compiledTicks;
@@ -30,36 +51,110 @@ public final class MobOverlayDisplayList {
}
/**
* Quad positions are relative to block ({@link MobOverlayScan#getMinX}, {@link MobOverlayScan#getMinY},
* {@link MobOverlayScan#getMinZ}) of {@code scan}.
* Start a compile of {@code scan}. After this call, {@link #beginChunk} and {@link #endChunk} compile each chunk
* column of the box, and {@link #endCompile} compiles the parent list. The lists of the chunk columns outside the
* box are not drawn.
*/
public void beginCompile(MobOverlayScan scan, MobOverlayStyle newStyle, long worldTicks) {
if (listId < 0) listId = GLAllocation.generateDisplayLists(1);
if (chunkSpan != scan.getChunkSpan()) resize(scan.getChunkSpan());
if (style != newStyle) Arrays.fill(isCompiled, false);
modesVersion = scan.getModesVersion();
style = newStyle;
minX = scan.getMinX();
minY = scan.getMinY();
minZ = scan.getMinZ();
compiledTicks = worldTicks;
GL11.glNewList(listId, GL11.GL_COMPILE);
anchorX = scan.getMinX();
anchorY = scan.getMinY();
anchorZ = scan.getMinZ();
final int fromChunkX = scan.getMinX() >> MobOverlayScan.CHUNK_SHIFT;
final int toChunkX = (scan.getMinX() + scan.getSpan() - 1) >> MobOverlayScan.CHUNK_SHIFT;
final int fromChunkZ = scan.getMinZ() >> MobOverlayScan.CHUNK_SHIFT;
final int toChunkZ = (scan.getMinZ() + scan.getSpan() - 1) >> MobOverlayScan.CHUNK_SHIFT;
for (int chunk = 0; chunk < isCompiled.length; chunk++) {
final boolean isInBox = compiledChunkX[chunk] >= fromChunkX && compiledChunkX[chunk] <= toChunkX
&& compiledChunkZ[chunk] >= fromChunkZ
&& compiledChunkZ[chunk] <= toChunkZ;
isCompiled[chunk] &= isInBox;
}
}
public void endCompile() {
/**
* Start the list of chunk column ({@code chunkX}, {@code chunkZ}), with quad positions relative to block ({@code
* chunkX * 16}, {@link MobOverlayScan#getMinY}, {@code chunkZ * 16}) of {@code scan}.
*
* @return false, with no list started, when the list is compiled for the chunk column's version
*/
public boolean beginChunk(MobOverlayScan scan, int chunkX, int chunkZ) {
final int chunk = Math.floorMod(chunkX, chunkSpan) * chunkSpan + Math.floorMod(chunkZ, chunkSpan);
final int chunkVersion = scan.readChunkVersion(chunkX, chunkZ);
final boolean isCurrent = isCompiled[chunk] && compiledChunkX[chunk] == chunkX
&& compiledChunkZ[chunk] == chunkZ
&& compiledChunkVersion[chunk] == chunkVersion;
if (isCurrent) return false;
isCompiled[chunk] = true;
compiledChunkX[chunk] = chunkX;
compiledChunkZ[chunk] = chunkZ;
compiledMinY[chunk] = scan.getMinY();
compiledChunkVersion[chunk] = chunkVersion;
compilingChunk = chunk;
GL11.glNewList(firstListId + chunk, GL11.GL_COMPILE);
return true;
}
public void endChunk(boolean hasChunkFaces) {
GL11.glEndList();
hasFaces[compilingChunk] = hasChunkFaces;
}
/** Compile the parent list, with positions relative to the box's minimum corner at {@link #beginCompile}. */
public void endCompile() {
GL11.glNewList(parentListId, GL11.GL_COMPILE);
for (int chunk = 0; chunk < isCompiled.length; chunk++) {
if (!(isCompiled[chunk] && hasFaces[chunk])) continue;
GL11.glPushMatrix();
GL11.glTranslated(
(compiledChunkX[chunk] << MobOverlayScan.CHUNK_SHIFT) - anchorX,
compiledMinY[chunk] - anchorY,
(compiledChunkZ[chunk] << MobOverlayScan.CHUNK_SHIFT) - anchorZ);
GL11.glCallList(firstListId + chunk);
GL11.glPopMatrix();
}
GL11.glEndList();
hasParentList = true;
}
/** The model view matrix is relative to block ({@code cameraX}, {@code cameraY}, {@code cameraZ}). */
public void draw(int cameraX, int cameraY, int cameraZ) {
if (!hasParentList) return;
GL11.glPushMatrix();
GL11.glTranslated(minX - cameraX, minY - cameraY, minZ - cameraZ);
GL11.glCallList(listId);
GL11.glTranslated(anchorX - cameraX, anchorY - cameraY, anchorZ - cameraZ);
GL11.glCallList(parentListId);
GL11.glPopMatrix();
}
public void delete() {
if (listId < 0) return;
if (firstListId == NO_LIST) return;
GLAllocation.deleteDisplayLists(listId);
listId = -1;
GLAllocation.deleteDisplayLists(firstListId);
firstListId = NO_LIST;
parentListId = NO_LIST;
hasParentList = false;
chunkSpan = 0;
Arrays.fill(isCompiled, false);
}
private void resize(int newChunkSpan) {
delete();
chunkSpan = newChunkSpan;
final int chunkCount = newChunkSpan * newChunkSpan;
firstListId = GLAllocation.generateDisplayLists(chunkCount + 1);
parentListId = firstListId + chunkCount;
isCompiled = new boolean[chunkCount];
hasFaces = new boolean[chunkCount];
compiledChunkX = new int[chunkCount];
compiledChunkZ = new int[chunkCount];
compiledMinY = new int[chunkCount];
compiledChunkVersion = new int[chunkCount];
}
}
@@ -1,5 +1,7 @@
package codechicken.nei.moboverlay;
import java.util.Arrays;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EnumCreatureType;
@@ -31,6 +33,7 @@ public final class MobOverlayScan {
public static final int MIN_VERTICAL_RADIUS = 4;
public static final int MAX_VERTICAL_RADIUS = 64;
public static final int DEFAULT_VERTICAL_RADIUS = 16;
public static final int CHUNK_SHIFT = 4;
private static final int RECENTRE_DISTANCE = 4;
private static final int VERTICAL_RECENTRE_DISTANCE = 2;
private static final int RESCAN_TICKS = 40;
@@ -56,6 +59,11 @@ public final class MobOverlayScan {
// the slice rescan of `update()` continues from this column, numbered `(x - minX) * span + (z - minZ)`
private int nextRescanColumn;
private int modesVersion;
// chunk columns per side of the ring `chunkVersions`, enough for every chunk column that overlaps the box
private int chunkSpan;
// indexed by `findChunk()`
private int[] chunkVersions = new int[0];
private int chunkVersionCount;
private boolean hasModes;
private long scannedTick;
// world of `modes`, with `changeListener` added
@@ -71,6 +79,8 @@ public final class MobOverlayScan {
height = 0;
modes = new byte[0];
dirty = new boolean[0];
chunkSpan = 0;
chunkVersions = new int[0];
hasModes = false;
scannedWorld = null;
// `Chunk.worldObj` references the world
@@ -117,6 +127,11 @@ public final class MobOverlayScan {
return modesVersion;
}
/** Chunk columns per side of the ring of {@link #readChunkVersion}. */
public int getChunkSpan() {
return chunkSpan;
}
public int getSpan() {
return span;
}
@@ -137,19 +152,42 @@ public final class MobOverlayScan {
return minZ;
}
/**
* The version changes when a cell of the chunk column or of a column beside the chunk column changes, and when the
* box moves.
*/
public int readChunkVersion(int chunkX, int chunkZ) {
return chunkVersions[findChunk(chunkX, chunkZ)];
}
/** {@code i} and {@code j} are from 0 to {@link #getSpan}, and {@code k} is from 0 to {@link #getHeight}. */
public byte readSpawnMode(int i, int k, int j) {
return (byte) (modes[findCell(minX + i, minY + k, minZ + j)] & SPAWN_MODE_MASK);
return extractSpawnMode(modes[findCell(minX + i, minY + k, minZ + j)]);
}
/**
* Copy the cells of column ({@code i}, {@code j}) to {@code columnCells}, from the bottom of the box to the top.
* {@link #extractSpawnMode} and {@link #computeOverlayYOffset} read a cell. The indices are those of
* {@link #readSpawnMode}.
*/
public void readColumn(int i, int j, byte[] columnCells) {
final int columnStart = findColumn(minX + i, minZ + j) * height;
final int bottomCell = Math.floorMod(minY, height);
System.arraycopy(modes, columnStart + bottomCell, columnCells, 0, height - bottomCell);
System.arraycopy(modes, columnStart, columnCells, height - bottomCell, bottomCell);
}
public static byte extractSpawnMode(byte cell) {
return (byte) (cell & SPAWN_MODE_MASK);
}
/**
* Height of the overlay above the bottom of the block, so that a flat block such as a carpet does not cover the
* overlay. The indices are those of {@link #readSpawnMode}.
* overlay.
*/
public double readOverlayYOffset(int i, int k, int j) {
public static double computeOverlayYOffset(byte cell) {
final double OVERLAY_GAP = 0.02;
final int coverTopSteps = modes[findCell(minX + i, minY + k, minZ + j)] >> COVER_TOP_SHIFT;
return (double) coverTopSteps / COVER_TOP_STEPS + OVERLAY_GAP;
return (double) (cell >> COVER_TOP_SHIFT) / COVER_TOP_STEPS + OVERLAY_GAP;
}
private void resize(int newRadius, int newVerticalRadius) {
@@ -159,6 +197,8 @@ public final class MobOverlayScan {
height = 2 * newVerticalRadius + 1;
modes = new byte[span * span * height];
dirty = new boolean[span * span];
chunkSpan = ((span - 1) >> CHUNK_SHIFT) + 2;
chunkVersions = new int[chunkSpan * chunkSpan];
hasModes = false;
nextRescanColumn = 0;
}
@@ -172,6 +212,7 @@ public final class MobOverlayScan {
scanColumn(x, z);
}
}
markAllChunks();
hasModes = true;
modesVersion++;
scannedTick = scannedWorld.getTotalWorldTime();
@@ -189,6 +230,7 @@ public final class MobOverlayScan {
final int offsetX = centreX - radius - minX;
if (Math.abs(offsetX) > RECENTRE_DISTANCE) {
final int enteringX = offsetX > 0 ? minX + span : minX + offsetX;
markMovedEdgeChunks(minX, offsetX, true);
minX += offsetX;
for (int x = enteringX; x < enteringX + Math.abs(offsetX); x++) {
for (int z = minZ; z < minZ + span; z++) {
@@ -201,6 +243,7 @@ public final class MobOverlayScan {
final int offsetZ = centreZ - radius - minZ;
if (Math.abs(offsetZ) > RECENTRE_DISTANCE) {
final int enteringZ = offsetZ > 0 ? minZ + span : minZ + offsetZ;
markMovedEdgeChunks(minZ, offsetZ, false);
minZ += offsetZ;
for (int x = minX; x < minX + span; x++) {
for (int z = enteringZ; z < enteringZ + Math.abs(offsetZ); z++) {
@@ -220,6 +263,7 @@ public final class MobOverlayScan {
column.scan(enteringY, enteringY + Math.abs(offsetY));
}
}
markAllChunks();
isMoved = true;
}
return isMoved;
@@ -253,7 +297,49 @@ public final class MobOverlayScan {
private boolean scanColumn(int x, int z) {
column.moveTo(x, z);
dirty[findColumn(x, z)] = false;
return column.scan(minY, minY + height);
final boolean isChanged = column.scan(minY, minY + height);
// the connected outline of a cell depends on the spawn modes of the cells beside the cell
if (isChanged) {
for (int offsetX = -1; offsetX <= 1; offsetX++) {
for (int offsetZ = -1; offsetZ <= 1; offsetZ++) {
markChunk((x + offsetX) >> CHUNK_SHIFT, (z + offsetZ) >> CHUNK_SHIFT);
}
}
}
return isChanged;
}
/**
* Mark the chunk columns of the box's edge before and after a move of {@code offset} blocks along x, or along z
* when {@code isAlongX} is false, and of the columns that enter or leave the box. No outline is drawn between an
* edge cell of the box and a cell outside the box, so a move changes the outlines of the edge cells.
*/
private void markMovedEdgeChunks(int oldMin, int offset, boolean isAlongX) {
final int lowFrom = Math.min(oldMin, oldMin + offset);
final int lowTo = Math.max(oldMin, oldMin + offset);
final int highFrom = lowFrom + span - 1;
final int highTo = lowTo + span - 1;
final int acrossFrom = isAlongX ? minZ : minX;
for (int across = acrossFrom >> CHUNK_SHIFT; across <= (acrossFrom + span - 1) >> CHUNK_SHIFT; across++) {
for (int along = lowFrom >> CHUNK_SHIFT; along <= lowTo >> CHUNK_SHIFT; along++) {
markChunk(isAlongX ? along : across, isAlongX ? across : along);
}
for (int along = highFrom >> CHUNK_SHIFT; along <= highTo >> CHUNK_SHIFT; along++) {
markChunk(isAlongX ? along : across, isAlongX ? across : along);
}
}
}
private void markAllChunks() {
Arrays.fill(chunkVersions, ++chunkVersionCount);
}
private void markChunk(int chunkX, int chunkZ) {
chunkVersions[findChunk(chunkX, chunkZ)] = ++chunkVersionCount;
}
private int findChunk(int chunkX, int chunkZ) {
return Math.floorMod(chunkX, chunkSpan) * chunkSpan + Math.floorMod(chunkZ, chunkSpan);
}
private int findColumn(int x, int z) {
@@ -314,6 +400,11 @@ public final class MobOverlayScan {
return 0;
}
// `SpawnerAnimals.canCreatureTypeSpawnAtLocation()` rejects a normal cube in the cell for a monster. Most
// cells below the surface are a normal cube. Test the block here, with no light reads and no reads of the
// blocks above and below the cell
if (chunk.getBlock(x & 15, y, z & 15).isNormalCube()) return 0;
if (chunk.getSavedLightValue(EnumSkyBlock.Block, x & 15, y, z & 15) >= 8 || !SpawnerAnimals
.canCreatureTypeSpawnAtLocation(EnumCreatureType.monster, chunk.worldObj, x, y, z)) {
return 0;