From 6243201f2d3aae87cb0ede23b09ddc31c35471bf Mon Sep 17 00:00:00 2001 From: cola98765 Date: Sun, 24 Oct 2021 10:08:12 +0200 Subject: [PATCH 1/8] removed couple Math.floorDiv and Mod as for positive inputs they are slower and give the same result as standard operands --- .../bufferBuilding/LodBufferBuilder.java | 6 ++--- .../lod/builders/lodBuilding/LodBuilder.java | 24 +++++++++++-------- .../com/seibel/lod/objects/LodDimension.java | 2 +- .../seibel/lod/util/DetailDistanceUtil.java | 5 ++-- .../java/com/seibel/lod/util/LodUtil.java | 6 ++--- .../com/seibel/lod/util/ThreadMapUtil.java | 2 +- 6 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/seibel/lod/builders/bufferBuilding/LodBufferBuilder.java b/src/main/java/com/seibel/lod/builders/bufferBuilding/LodBufferBuilder.java index ab927db81..a49e0cef0 100644 --- a/src/main/java/com/seibel/lod/builders/bufferBuilding/LodBufferBuilder.java +++ b/src/main/java/com/seibel/lod/builders/bufferBuilding/LodBufferBuilder.java @@ -243,8 +243,8 @@ public class LodBufferBuilder if (lodDim.doesRegionNeedBufferRegen(xRegion, zRegion) || fullRegen) { RegionPos regionPos = new RegionPos( - xRegion + lodDim.getCenterRegionPosX() - Math.floorDiv(lodDim.getWidth(), 2), - zRegion + lodDim.getCenterRegionPosZ() - Math.floorDiv(lodDim.getWidth(), 2)); + xRegion + lodDim.getCenterRegionPosX() - lodDim.getWidth() / 2, + zRegion + lodDim.getCenterRegionPosZ() - lodDim.getWidth() / 2); // local position in the vbo and bufferBuilder arrays BufferBuilder[] currentBuffers = buildableBuffers[xRegion][zRegion]; @@ -311,7 +311,7 @@ public class LodBufferBuilder for (int index = 0; index < posToRender.getNumberOfPos(); index++) { - bufferIndex = Math.floorMod(index, currentBuffers.length); + bufferIndex = index % currentBuffers.length; detailLevel = posToRender.getNthDetailLevel(index); posX = posToRender.getNthPosX(index); posZ = posToRender.getNthPosZ(index); diff --git a/src/main/java/com/seibel/lod/builders/lodBuilding/LodBuilder.java b/src/main/java/com/seibel/lod/builders/lodBuilding/LodBuilder.java index 9f9baebe3..b85ae8cbe 100644 --- a/src/main/java/com/seibel/lod/builders/lodBuilding/LodBuilder.java +++ b/src/main/java/com/seibel/lod/builders/lodBuilding/LodBuilder.java @@ -34,6 +34,7 @@ import com.seibel.lod.enums.VerticalQuality; import com.seibel.lod.objects.LodDimension; import com.seibel.lod.objects.LodRegion; import com.seibel.lod.objects.LodWorld; +import com.seibel.lod.proxy.ClientProxy; import com.seibel.lod.util.ColorUtil; import com.seibel.lod.util.DataPointUtil; import com.seibel.lod.util.DetailDistanceUtil; @@ -204,6 +205,7 @@ public class LodBuilder { if (chunk == null) throw new IllegalArgumentException("generateLodFromChunk given a null chunk"); + long startTime = System.currentTimeMillis(); int startX; int startZ; @@ -216,13 +218,12 @@ public class LodBuilder return; // determine how many LODs to generate horizontally - HorizontalResolution detail; byte minDetailLevel = region.getMinDetailLevel(); - detail = DetailDistanceUtil.getLodGenDetail(minDetailLevel); + HorizontalResolution detail = DetailDistanceUtil.getLodGenDetail(minDetailLevel); // determine how many LODs to generate vertically - VerticalQuality verticalQuality = LodConfig.CLIENT.graphics.qualityOption.verticalQuality.get(); + //VerticalQuality verticalQuality = LodConfig.CLIENT.graphics.qualityOption.verticalQuality.get(); byte detailLevel = detail.detailLevel; @@ -236,10 +237,6 @@ public class LodBuilder endX = detail.endX[i]; endZ = detail.endZ[i]; - posX = LevelPosUtil.convert((byte) 0, chunk.getPos().x * 16 + startX, detail.detailLevel); - posZ = LevelPosUtil.convert((byte) 0, chunk.getPos().z * 16 + startZ, detail.detailLevel); - - long[] data; long[] dataToMergeVertical = createVerticalDataToMerge(detail, chunk, config, startX, startZ, endX, endZ); data = DataPointUtil.mergeMultiData(dataToMergeVertical, DataPointUtil.worldHeight, DetailDistanceUtil.getMaxVerticalData(detailLevel)); @@ -247,9 +244,16 @@ public class LodBuilder //lodDim.clear(detailLevel, posX, posZ); if (data != null && data.length != 0) - lodDim.addVerticalData(detailLevel, posX, posZ, data,false); + { + posX = LevelPosUtil.convert((byte) 0, chunk.getPos().x * 16 + startX, detail.detailLevel); + posZ = LevelPosUtil.convert((byte) 0, chunk.getPos().z * 16 + startZ, detail.detailLevel); + lodDim.addVerticalData(detailLevel, posX, posZ, data, false); + } } lodDim.updateData(LodUtil.CHUNK_DETAIL_LEVEL, chunk.getPos().x, chunk.getPos().z); + + startTime = System.currentTimeMillis() - startTime; + ClientProxy.LOGGER.info("gen time: " + startTime + " ms."); } /** creates a vertical DataPoint */ @@ -284,8 +288,8 @@ public class LodBuilder for (index = 0; index < size * size; index++) { - xRel = Math.floorMod(index, size) + startX; - zRel = Math.floorDiv(index, size) + startZ; + xRel = startX + index % size; + zRel = startZ + index / size; xAbs = chunkPos.getMinBlockX() + xRel; zAbs = chunkPos.getMinBlockZ() + zRel; diff --git a/src/main/java/com/seibel/lod/objects/LodDimension.java b/src/main/java/com/seibel/lod/objects/LodDimension.java index 60a98a6c0..f1472dd68 100644 --- a/src/main/java/com/seibel/lod/objects/LodDimension.java +++ b/src/main/java/com/seibel/lod/objects/LodDimension.java @@ -866,7 +866,7 @@ public class LodDimension public void setRegionWidth(int newWidth) { width = newWidth; - halfWidth = Math.floorDiv(width, 2); + halfWidth = width/ 2; regions = new LodRegion[width][width]; isRegionDirty = new boolean[width][width]; diff --git a/src/main/java/com/seibel/lod/util/DetailDistanceUtil.java b/src/main/java/com/seibel/lod/util/DetailDistanceUtil.java index e096dbea3..e8ebdc716 100644 --- a/src/main/java/com/seibel/lod/util/DetailDistanceUtil.java +++ b/src/main/java/com/seibel/lod/util/DetailDistanceUtil.java @@ -97,12 +97,13 @@ public class DetailDistanceUtil return (byte) minDetail; int distanceUnit = LodConfig.CLIENT.graphics.qualityOption.horizontalScale.get().distanceUnit; if (LodConfig.CLIENT.graphics.qualityOption.horizontalQuality.get() == HorizontalQuality.LOWEST) - detail = (byte) Math.floorDiv(distance, distanceUnit); + detail = (byte) distance / distanceUnit; else { double base = LodConfig.CLIENT.graphics.qualityOption.horizontalQuality.get().quadraticBase; double logBase = Math.log(base); - detail = (byte) (Math.log(Math.floorDiv(distance, distanceUnit)) / logBase); + //noinspection IntegerDivisionInFloatingPointContext + detail = (byte) (Math.log(distance / distanceUnit) / logBase); } return (byte) LodUtil.clamp(minDetail, detail, maxDetail - 1); } diff --git a/src/main/java/com/seibel/lod/util/LodUtil.java b/src/main/java/com/seibel/lod/util/LodUtil.java index b5d8472ff..a81b11b45 100644 --- a/src/main/java/com/seibel/lod/util/LodUtil.java +++ b/src/main/java/com/seibel/lod/util/LodUtil.java @@ -201,8 +201,8 @@ public class LodUtil /** Convert a 2D absolute position into a quad tree relative position. */ public static RegionPos convertGenericPosToRegionPos(int x, int z, int detailLevel) { - int relativePosX = Math.floorDiv(x, (int) Math.pow(2, LodUtil.REGION_DETAIL_LEVEL - detailLevel)); - int relativePosZ = Math.floorDiv(z, (int) Math.pow(2, LodUtil.REGION_DETAIL_LEVEL - detailLevel)); + int relativePosX = Math.floorDiv(x, 1 << (LodUtil.REGION_DETAIL_LEVEL - detailLevel)); + int relativePosZ = Math.floorDiv(z, 1 << (LodUtil.REGION_DETAIL_LEVEL - detailLevel)); return new RegionPos(relativePosX, relativePosZ); } @@ -210,7 +210,7 @@ public class LodUtil /** Convert a 2D absolute position into a quad tree relative position. */ public static int convertLevelPos(int pos, int currentDetailLevel, int targetDetailLevel) { - return Math.floorDiv(pos, (int) Math.pow(2, targetDetailLevel - currentDetailLevel)); + return pos / (1 << (targetDetailLevel - currentDetailLevel)); } /** diff --git a/src/main/java/com/seibel/lod/util/ThreadMapUtil.java b/src/main/java/com/seibel/lod/util/ThreadMapUtil.java index d3df265fe..42564aba7 100644 --- a/src/main/java/com/seibel/lod/util/ThreadMapUtil.java +++ b/src/main/java/com/seibel/lod/util/ThreadMapUtil.java @@ -128,7 +128,7 @@ public class ThreadMapUtil int size = 1; for (int i = 0; i < 5; i++) { - array[i] = new long[size * size * DataPointUtil.worldHeight + 1]; + array[i] = new long[size * size * DataPointUtil.worldHeight + 1];//TODO do we need +1? size = size << 1; } threadBuilderVerticalArrayMap.put(Thread.currentThread().getName(), array); From 4064155567664e0f3a636302b5d8574a85be76c6 Mon Sep 17 00:00:00 2001 From: cola98765 Date: Sun, 24 Oct 2021 10:28:35 +0200 Subject: [PATCH 2/8] changed Math.pow usages with simpler (and possibly faster) x*x and 1 << x. reduced repetitions --- .../lod/objects/VerticalLevelContainer.java | 4 +-- .../com/seibel/lod/util/LevelPosUtil.java | 30 ++++++++++++++----- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/seibel/lod/objects/VerticalLevelContainer.java b/src/main/java/com/seibel/lod/objects/VerticalLevelContainer.java index 162d171ac..8233e5aef 100644 --- a/src/main/java/com/seibel/lod/objects/VerticalLevelContainer.java +++ b/src/main/java/com/seibel/lod/objects/VerticalLevelContainer.java @@ -134,7 +134,7 @@ public class VerticalLevelContainer implements LevelContainer index++; maxVerticalData = inputData[index]; index++; - size = (int) Math.pow(2, LodUtil.REGION_DETAIL_LEVEL - detailLevel); + size = 1 << (LodUtil.REGION_DETAIL_LEVEL - detailLevel); int x = size * size * maxVerticalData; this.dataContainer = new long[x]; for (int i = 0; i < x; i++) @@ -217,7 +217,7 @@ public class VerticalLevelContainer implements LevelContainer { /* StringBuilder stringBuilder = new StringBuilder(); - int size = (int) Math.pow(2, LodUtil.REGION_DETAIL_LEVEL - detailLevel); + int size = 1 << (LodUtil.REGION_DETAIL_LEVEL - detailLevel); stringBuilder.append(detailLevel); stringBuilder.append(DATA_DELIMITER); for (int x = 0; x < size; x++) diff --git a/src/main/java/com/seibel/lod/util/LevelPosUtil.java b/src/main/java/com/seibel/lod/util/LevelPosUtil.java index aea601fb2..8415c72af 100644 --- a/src/main/java/com/seibel/lod/util/LevelPosUtil.java +++ b/src/main/java/com/seibel/lod/util/LevelPosUtil.java @@ -149,6 +149,10 @@ public class LevelPosUtil return convert(detailLevel, pos, LodUtil.CHUNK_DETAIL_LEVEL); } + public static int myPow2(int x) + { + return x*x; + } public static int maxDistance(byte detailLevel, int posX, int posZ, int playerPosX, int playerPosZ) { @@ -159,10 +163,15 @@ public class LevelPosUtil int endPosX = startPosX + width; int endPosZ = startPosZ + width; - int maxDistance = (int) Math.sqrt(Math.pow(playerPosX - startPosX, 2) + Math.pow(playerPosZ - startPosZ, 2)); - maxDistance = Math.max(maxDistance, (int) Math.sqrt(Math.pow(playerPosX - startPosX, 2) + Math.pow(playerPosZ - endPosZ, 2))); - maxDistance = Math.max(maxDistance, (int) Math.sqrt(Math.pow(playerPosX - endPosX, 2) + Math.pow(playerPosZ - startPosZ, 2))); - maxDistance = Math.max(maxDistance, (int) Math.sqrt(Math.pow(playerPosX - endPosX, 2) + Math.pow(playerPosZ - endPosZ, 2))); + startPosX = myPow2(playerPosX - startPosX); + startPosZ = myPow2(playerPosZ - startPosZ); + endPosX = myPow2(playerPosX - endPosX); + endPosZ = myPow2(playerPosZ - endPosZ); + + int maxDistance = (int) Math.sqrt(startPosX + startPosZ); + maxDistance = Math.max(maxDistance, (int) Math.sqrt(startPosX + endPosZ)); + maxDistance = Math.max(maxDistance, (int) Math.sqrt(endPosX + startPosZ)); + maxDistance = Math.max(maxDistance, (int) Math.sqrt(endPosX + endPosZ)); return maxDistance; } @@ -205,10 +214,15 @@ public class LevelPosUtil } else { - int minDistance = (int) Math.sqrt(Math.pow(playerPosX - startPosX, 2) + Math.pow(playerPosZ - startPosZ, 2)); - minDistance = Math.min(minDistance, (int) Math.sqrt(Math.pow(playerPosX - startPosX, 2) + Math.pow(playerPosZ - endPosZ, 2))); - minDistance = Math.min(minDistance, (int) Math.sqrt(Math.pow(playerPosX - endPosX, 2) + Math.pow(playerPosZ - startPosZ, 2))); - minDistance = Math.min(minDistance, (int) Math.sqrt(Math.pow(playerPosX - endPosX, 2) + Math.pow(playerPosZ - endPosZ, 2))); + startPosX = myPow2(playerPosX - startPosX); + startPosZ = myPow2(playerPosZ - startPosZ); + endPosX = myPow2(playerPosX - endPosX); + endPosZ = myPow2(playerPosZ - endPosZ); + + int minDistance = (int) Math.sqrt(startPosX + startPosZ); + minDistance = Math.min(minDistance, (int) Math.sqrt(startPosX + endPosZ)); + minDistance = Math.min(minDistance, (int) Math.sqrt(endPosX + startPosZ)); + minDistance = Math.min(minDistance, (int) Math.sqrt(endPosX + endPosZ)); return minDistance; } } From 8b854e3abd0b5c0104315f48beb4b7cfaa2daf13 Mon Sep 17 00:00:00 2001 From: cola98765 Date: Sun, 24 Oct 2021 10:33:02 +0200 Subject: [PATCH 3/8] removed debug message --- .../java/com/seibel/lod/builders/lodBuilding/LodBuilder.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/com/seibel/lod/builders/lodBuilding/LodBuilder.java b/src/main/java/com/seibel/lod/builders/lodBuilding/LodBuilder.java index b85ae8cbe..a0ffb7d0b 100644 --- a/src/main/java/com/seibel/lod/builders/lodBuilding/LodBuilder.java +++ b/src/main/java/com/seibel/lod/builders/lodBuilding/LodBuilder.java @@ -205,7 +205,6 @@ public class LodBuilder { if (chunk == null) throw new IllegalArgumentException("generateLodFromChunk given a null chunk"); - long startTime = System.currentTimeMillis(); int startX; int startZ; @@ -251,9 +250,6 @@ public class LodBuilder } } lodDim.updateData(LodUtil.CHUNK_DETAIL_LEVEL, chunk.getPos().x, chunk.getPos().z); - - startTime = System.currentTimeMillis() - startTime; - ClientProxy.LOGGER.info("gen time: " + startTime + " ms."); } /** creates a vertical DataPoint */ From d4261d4ccf4281393a7f57a1a2d523e4a33b8848 Mon Sep 17 00:00:00 2001 From: cola98765 Date: Sun, 24 Oct 2021 11:01:02 +0200 Subject: [PATCH 4/8] a little improvement to 'pow' changes --- src/main/java/com/seibel/lod/util/LevelPosUtil.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/seibel/lod/util/LevelPosUtil.java b/src/main/java/com/seibel/lod/util/LevelPosUtil.java index 8415c72af..11cbfc4f4 100644 --- a/src/main/java/com/seibel/lod/util/LevelPosUtil.java +++ b/src/main/java/com/seibel/lod/util/LevelPosUtil.java @@ -160,13 +160,10 @@ public class LevelPosUtil int startPosX = posX * width; int startPosZ = posZ * width; - int endPosX = startPosX + width; - int endPosZ = startPosZ + width; - + int endPosX = myPow2(playerPosX - startPosX - width); + int endPosZ = myPow2(playerPosZ - startPosZ - width); startPosX = myPow2(playerPosX - startPosX); startPosZ = myPow2(playerPosZ - startPosZ); - endPosX = myPow2(playerPosX - endPosX); - endPosZ = myPow2(playerPosZ - endPosZ); int maxDistance = (int) Math.sqrt(startPosX + startPosZ); maxDistance = Math.max(maxDistance, (int) Math.sqrt(startPosX + endPosZ)); From 5bba3cb3eb1ef1180b9b1ab567bb7f06b51cacc1 Mon Sep 17 00:00:00 2001 From: cola98765 Date: Sun, 24 Oct 2021 11:35:45 +0200 Subject: [PATCH 5/8] minor cleanup --- .../lod/objects/VerticalLevelContainer.java | 3 --- .../seibel/lod/util/DetailDistanceUtil.java | 13 ------------- .../java/com/seibel/lod/util/LodUtil.java | 19 +++---------------- 3 files changed, 3 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/seibel/lod/objects/VerticalLevelContainer.java b/src/main/java/com/seibel/lod/objects/VerticalLevelContainer.java index 8233e5aef..fa0a3f433 100644 --- a/src/main/java/com/seibel/lod/objects/VerticalLevelContainer.java +++ b/src/main/java/com/seibel/lod/objects/VerticalLevelContainer.java @@ -56,7 +56,6 @@ public class VerticalLevelContainer implements LevelContainer @Override public void clear(int posX, int posZ) { - posX = LevelPosUtil.getRegionModule(detailLevel, posX); posZ = LevelPosUtil.getRegionModule(detailLevel, posZ); for (int verticalIndex = 0; verticalIndex < maxVerticalData; verticalIndex++) @@ -68,7 +67,6 @@ public class VerticalLevelContainer implements LevelContainer @Override public boolean addData(long data, int posX, int posZ, int verticalIndex) { - posX = LevelPosUtil.getRegionModule(detailLevel, posX); posZ = LevelPosUtil.getRegionModule(detailLevel, posZ); dataContainer[posX * size * maxVerticalData + posZ * maxVerticalData + verticalIndex] = data; @@ -78,7 +76,6 @@ public class VerticalLevelContainer implements LevelContainer @Override public boolean addVerticalData(long[] data, int posX, int posZ) { - posX = LevelPosUtil.getRegionModule(detailLevel, posX); posZ = LevelPosUtil.getRegionModule(detailLevel, posZ); for (int verticalIndex = 0; verticalIndex < maxVerticalData; verticalIndex++) diff --git a/src/main/java/com/seibel/lod/util/DetailDistanceUtil.java b/src/main/java/com/seibel/lod/util/DetailDistanceUtil.java index e8ebdc716..f99e2b00c 100644 --- a/src/main/java/com/seibel/lod/util/DetailDistanceUtil.java +++ b/src/main/java/com/seibel/lod/util/DetailDistanceUtil.java @@ -120,14 +120,11 @@ public class DetailDistanceUtil public static byte getTreeCutDetailFromDistance(int distance) { - return baseInverseFunction((int) (distance * treeCutMultiplier), minGenDetail, true); } - public static byte getTreeGenDetailFromDistance(int distance) { - return baseInverseFunction((int) (distance * treeGenMultiplier), minGenDetail, true); } @@ -157,30 +154,20 @@ public class DetailDistanceUtil public static HorizontalResolution getLodGenDetail(int detail) { if (detail < minGenDetail) - { return lodGenDetails[minGenDetail]; - } else - { return lodGenDetails[detail]; - } } public static byte getCutLodDetail(int detail) { if (detail < minGenDetail) - { return lodGenDetails[minGenDetail].detailLevel; - } else if (detail == maxDetail) - { return LodUtil.REGION_DETAIL_LEVEL; - } else - { return lodGenDetails[detail].detailLevel; - } } public static int getMaxVerticalData(int detail) diff --git a/src/main/java/com/seibel/lod/util/LodUtil.java b/src/main/java/com/seibel/lod/util/LodUtil.java index a81b11b45..30e132379 100644 --- a/src/main/java/com/seibel/lod/util/LodUtil.java +++ b/src/main/java/com/seibel/lod/util/LodUtil.java @@ -224,9 +224,7 @@ public class LodUtil for (ChunkSection section : blockStorage) { if (section != null && !section.isEmpty()) - { return true; - } } return false; @@ -492,20 +490,9 @@ public class LodUtil { tempX = x + Box.DIRECTION_NORMAL_MAP.get(direction).getX(); tempZ = z + Box.DIRECTION_NORMAL_MAP.get(direction).getZ(); - if (!(tempX < 0 || tempZ < 0 || tempX >= vanillaRenderedChunks.length || tempZ >= vanillaRenderedChunks[0].length)) - { - if (!vanillaRenderedChunks[tempX][tempZ]) - { - return true; - } - } - else - { - if (vanillaRenderedChunks[x][z]) - { - return true; - } - } + if (vanillaRenderedChunks[x][z] || (!(tempX < 0 || tempZ < 0 || tempX >= vanillaRenderedChunks.length || tempZ >= vanillaRenderedChunks[0].length) + && !vanillaRenderedChunks[tempX][tempZ])) + return true; } return false; } From f80af39e0e1acab1afe545f0fa5494cfc1526064 Mon Sep 17 00:00:00 2001 From: cola98765 Date: Sun, 24 Oct 2021 11:57:08 +0200 Subject: [PATCH 6/8] halving every array related to worldHeight, as you can't get worldHeight lods, as half of that needs to be a gaps --- .../java/com/seibel/lod/builders/lodBuilding/LodBuilder.java | 4 ++-- src/main/java/com/seibel/lod/util/DataPointUtil.java | 2 +- src/main/java/com/seibel/lod/util/ThreadMapUtil.java | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/seibel/lod/builders/lodBuilding/LodBuilder.java b/src/main/java/com/seibel/lod/builders/lodBuilding/LodBuilder.java index a0ffb7d0b..dcd301c81 100644 --- a/src/main/java/com/seibel/lod/builders/lodBuilding/LodBuilder.java +++ b/src/main/java/com/seibel/lod/builders/lodBuilding/LodBuilder.java @@ -238,7 +238,7 @@ public class LodBuilder long[] data; long[] dataToMergeVertical = createVerticalDataToMerge(detail, chunk, config, startX, startZ, endX, endZ); - data = DataPointUtil.mergeMultiData(dataToMergeVertical, DataPointUtil.worldHeight, DetailDistanceUtil.getMaxVerticalData(detailLevel)); + data = DataPointUtil.mergeMultiData(dataToMergeVertical, DataPointUtil.worldHeight / 2 + 1, DetailDistanceUtil.getMaxVerticalData(detailLevel)); //lodDim.clear(detailLevel, posX, posZ); @@ -260,7 +260,7 @@ public class LodBuilder long[] dataToMerge = ThreadMapUtil.getBuilderVerticalArray(detail.detailLevel); - int verticalData = DataPointUtil.worldHeight; + int verticalData = DataPointUtil.worldHeight / 2 + 1; ChunkPos chunkPos = chunk.getPos(); int height; diff --git a/src/main/java/com/seibel/lod/util/DataPointUtil.java b/src/main/java/com/seibel/lod/util/DataPointUtil.java index 25d707e5d..f94a9e7e7 100644 --- a/src/main/java/com/seibel/lod/util/DataPointUtil.java +++ b/src/main/java/com/seibel/lod/util/DataPointUtil.java @@ -282,7 +282,7 @@ public class DataPointUtil int size = dataToMerge.length / inputVerticalData; // We initialize the arrays that are going to be used - short[] heightAndDepth = ThreadMapUtil.getHeightAndDepth((worldHeight + 1) * 2); + short[] heightAndDepth = ThreadMapUtil.getHeightAndDepth((worldHeight / 2 + 1) * 2); long[] dataPoint = ThreadMapUtil.getVerticalDataArray(DetailDistanceUtil.getMaxVerticalData(0)); diff --git a/src/main/java/com/seibel/lod/util/ThreadMapUtil.java b/src/main/java/com/seibel/lod/util/ThreadMapUtil.java index 42564aba7..d70dbc0e8 100644 --- a/src/main/java/com/seibel/lod/util/ThreadMapUtil.java +++ b/src/main/java/com/seibel/lod/util/ThreadMapUtil.java @@ -128,7 +128,7 @@ public class ThreadMapUtil int size = 1; for (int i = 0; i < 5; i++) { - array[i] = new long[size * size * DataPointUtil.worldHeight + 1];//TODO do we need +1? + array[i] = new long[size * size * DataPointUtil.worldHeight / 2 + 1]; size = size << 1; } threadBuilderVerticalArrayMap.put(Thread.currentThread().getName(), array); From e00de99e3168bc83672b0ba51a73645ed79b257d Mon Sep 17 00:00:00 2001 From: cola98765 Date: Sun, 24 Oct 2021 12:11:43 +0200 Subject: [PATCH 7/8] Found place where addData was used for adding whole vertical array. We have addVerticalData for that. --- src/main/java/com/seibel/lod/objects/LodDimension.java | 1 + .../com/seibel/lod/objects/VerticalLevelContainer.java | 8 +------- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/seibel/lod/objects/LodDimension.java b/src/main/java/com/seibel/lod/objects/LodDimension.java index f1472dd68..6043ae051 100644 --- a/src/main/java/com/seibel/lod/objects/LodDimension.java +++ b/src/main/java/com/seibel/lod/objects/LodDimension.java @@ -437,6 +437,7 @@ public class LodDimension } /** + * Use addVerticalData when possible. * Add the given LOD to this dimension at the coordinate * stored in the LOD. If an LOD already exists at the given * coordinate it will be overwritten. diff --git a/src/main/java/com/seibel/lod/objects/VerticalLevelContainer.java b/src/main/java/com/seibel/lod/objects/VerticalLevelContainer.java index fa0a3f433..f692baa1d 100644 --- a/src/main/java/com/seibel/lod/objects/VerticalLevelContainer.java +++ b/src/main/java/com/seibel/lod/objects/VerticalLevelContainer.java @@ -174,13 +174,7 @@ public class VerticalLevelContainer implements LevelContainer } data = DataPointUtil.mergeMultiData(dataToMerge, lowerMaxVertical, getMaxVerticalData()); - for (int verticalIndex = 0; (verticalIndex < data.length) && (verticalIndex < maxVerticalData); verticalIndex++) - { - addData(data[verticalIndex], - posX, - posZ, - verticalIndex); - } + addVerticalData(data, posX, posZ); } @Override From 910f11f68888b6983770f2d2078dc5d25b25fe7a Mon Sep 17 00:00:00 2001 From: jas35484 Date: Sun, 24 Oct 2021 14:57:53 -0500 Subject: [PATCH 8/8] Fix #63 (OpenGL errors) NVFogDistance doesn't work on low end GPUs which causes the OpenGL error I was seeing with Optifine --- .../bufferBuilding/LodBufferBuilder.java | 3 +- .../com/seibel/lod/render/LodRenderer.java | 109 +++++++----------- 2 files changed, 43 insertions(+), 69 deletions(-) diff --git a/src/main/java/com/seibel/lod/builders/bufferBuilding/LodBufferBuilder.java b/src/main/java/com/seibel/lod/builders/bufferBuilding/LodBufferBuilder.java index ab927db81..920165e1b 100644 --- a/src/main/java/com/seibel/lod/builders/bufferBuilding/LodBufferBuilder.java +++ b/src/main/java/com/seibel/lod/builders/bufferBuilding/LodBufferBuilder.java @@ -181,8 +181,7 @@ public class LodBufferBuilder generatingBuffers = true; - Thread thread = new Thread(() -> - generateLodBuffersThread(renderer, lodDim, playerBlockPos, fullRegen)); + Thread thread = new Thread(() -> generateLodBuffersThread(renderer, lodDim, playerBlockPos, fullRegen)); mainGenThread.execute(thread); } diff --git a/src/main/java/com/seibel/lod/render/LodRenderer.java b/src/main/java/com/seibel/lod/render/LodRenderer.java index d238a67b0..a856e4c2a 100644 --- a/src/main/java/com/seibel/lod/render/LodRenderer.java +++ b/src/main/java/com/seibel/lod/render/LodRenderer.java @@ -21,7 +21,6 @@ package com.seibel.lod.render; import java.util.HashSet; -import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL15; import org.lwjgl.opengl.GL15C; import org.lwjgl.opengl.NVFogDistance; @@ -65,7 +64,7 @@ import net.minecraft.util.math.vector.Vector3d; * This is where LODs are draw to the world. * * @author James Seibel - * @version 10-23-2021 + * @version 10-24-2021 */ public class LodRenderer { @@ -73,7 +72,7 @@ public class LodRenderer * this is the light used when rendering the LODs, * it should be something different from what is used by Minecraft */ - private static final int LOD_GL_LIGHT_NUMBER = GL11.GL_LIGHT2; + private static final int LOD_GL_LIGHT_NUMBER = GL15.GL_LIGHT2; /** * If true the LODs colors will be replaced with @@ -218,27 +217,27 @@ public class LodRenderer // set the required open GL settings if (LodConfig.CLIENT.advancedModOptions.debugging.debugMode.get() == DebugMode.SHOW_DETAIL_WIREFRAME) - GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE); + GL15.glPolygonMode(GL15.GL_FRONT_AND_BACK, GL15.GL_LINE); else - GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL); + GL15.glPolygonMode(GL15.GL_FRONT_AND_BACK, GL15.GL_FILL); - GL11.glDisable(GL11.GL_TEXTURE_2D); - GL11.glEnable(GL11.GL_CULL_FACE); - GL11.glEnable(GL11.GL_COLOR_MATERIAL); - GL11.glEnable(GL11.GL_DEPTH_TEST); + GL15.glDisable(GL15.GL_TEXTURE_2D); + GL15.glEnable(GL15.GL_CULL_FACE); + GL15.glEnable(GL15.GL_COLOR_MATERIAL); + GL15.glEnable(GL15.GL_DEPTH_TEST); // enable transparent rendering - GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); - GL11.glEnable(GL11.GL_BLEND); + GL15.glBlendFunc(GL15.GL_SRC_ALPHA, GL15.GL_ONE_MINUS_SRC_ALPHA); + GL15.glEnable(GL15.GL_BLEND); // disable the lights Minecraft uses - GL11.glDisable(GL11.GL_LIGHT0); - GL11.glDisable(GL11.GL_LIGHT1); + GL15.glDisable(GL15.GL_LIGHT0); + GL15.glDisable(GL15.GL_LIGHT1); // get the default projection matrix, so we can // reset it after drawing the LODs float[] mcProjMatrixRaw = new float[16]; - GL11.glGetFloatv(GL11.GL_PROJECTION_MATRIX, mcProjMatrixRaw); + GL15.glGetFloatv(GL15.GL_PROJECTION_MATRIX, mcProjMatrixRaw); Matrix4f mcProjectionMatrix = new Matrix4f(mcProjMatrixRaw); // OpenGl outputs their matrices in col,row form instead of row,col // (or maybe vice versa I have no idea :P) @@ -260,11 +259,10 @@ public class LodRenderer // determine the current fog settings, so they can be // reset after drawing the LODs - float defaultFogStartDist = GL11.glGetFloat(GL11.GL_FOG_START); - float defaultFogEndDist = GL11.glGetFloat(GL11.GL_FOG_END); - int defaultFogMode = GL11.glGetInteger(GL11.GL_FOG_MODE); - int defaultFogDistance = GL11.glGetInteger(NVFogDistance.GL_FOG_DISTANCE_MODE_NV); - + float defaultFogStartDist = GL15.glGetFloat(GL15.GL_FOG_START); + float defaultFogEndDist = GL15.glGetFloat(GL15.GL_FOG_END); + int defaultFogMode = GL15.glGetInteger(GL15.GL_FOG_MODE); + int defaultFogDistance = GlProxy.getInstance().fancyFogAvailable ? GL15.glGetInteger(NVFogDistance.GL_FOG_DISTANCE_MODE_NV) : -1; //===========// // rendering // @@ -323,13 +321,13 @@ public class LodRenderer profiler.popPush("LOD cleanup"); - GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL); - GL11.glEnable(GL11.GL_TEXTURE_2D); - GL11.glDisable(LOD_GL_LIGHT_NUMBER); - GL11.glDisable(GL11.GL_BLEND); + GL15.glPolygonMode(GL15.GL_FRONT_AND_BACK, GL15.GL_FILL); + GL15.glEnable(GL15.GL_TEXTURE_2D); + GL15.glDisable(LOD_GL_LIGHT_NUMBER); + GL15.glDisable(GL15.GL_BLEND); // re-enable the lights Minecraft uses - GL11.glEnable(GL11.GL_LIGHT0); - GL11.glEnable(GL11.GL_LIGHT1); + GL15.glEnable(GL15.GL_LIGHT0); + GL15.glEnable(GL15.GL_LIGHT1); RenderSystem.disableLighting(); // reset the fog settings so the normal chunks @@ -342,7 +340,7 @@ public class LodRenderer // clear the depth buffer so anything drawn is drawn // over the LODs - GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT); + GL15.glClear(GL15.GL_DEPTH_BUFFER_BIT); // end of internal LOD profiling @@ -360,7 +358,7 @@ public class LodRenderer // 0L is the starting pointer LodUtil.LOD_VERTEX_FORMAT.setupBufferState(0L); - vbo.draw(modelViewMatrix, GL11.GL_QUADS); + vbo.draw(modelViewMatrix, GL15.GL_QUADS); GL15C.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); LodUtil.LOD_VERTEX_FORMAT.clearBufferState(); @@ -377,7 +375,7 @@ public class LodRenderer // 0L is the starting pointer LodUtil.LOD_VERTEX_FORMAT.setupBufferState(0L); - vbo.draw(modelViewMatrix, GL11.GL_QUADS); + vbo.draw(modelViewMatrix, GL15.GL_QUADS); GL15C.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); LodUtil.LOD_VERTEX_FORMAT.clearBufferState(); @@ -459,16 +457,16 @@ public class LodRenderer } } - GL11.glEnable(GL11.GL_FOG); + GL15.glEnable(GL15.GL_FOG); RenderSystem.enableFog(); RenderSystem.setupNvFogDistance(); RenderSystem.fogMode(GlStateManager.FogMode.LINEAR); - GL11.glFogi(NVFogDistance.GL_FOG_DISTANCE_MODE_NV, glFogDistanceMode); + + if (GlProxy.getInstance().fancyFogAvailable) + GL15.glFogi(NVFogDistance.GL_FOG_DISTANCE_MODE_NV, glFogDistanceMode); } - /** - * Revert any changes that were made to the fog. - */ + /** Revert any changes that were made to the fog. */ @SuppressWarnings("deprecation") private void cleanupFog(NearFarFogSettings fogSettings, float defaultFogStartDist, float defaultFogEndDist, @@ -477,15 +475,18 @@ public class LodRenderer RenderSystem.fogStart(defaultFogStartDist); RenderSystem.fogEnd(defaultFogEndDist); RenderSystem.fogMode(defaultFogMode); - GL11.glFogi(NVFogDistance.GL_FOG_DISTANCE_MODE_NV, defaultFogDistance); + + // this setting is only valid if the GPU supports fancy fog + if (GlProxy.getInstance().fancyFogAvailable) + GL15.glFogi(NVFogDistance.GL_FOG_DISTANCE_MODE_NV, defaultFogDistance); // disable fog if Minecraft wasn't rendering fog, // but we were if (!fogSettings.vanillaIsRenderingFog && (fogSettings.near.quality != FogQuality.OFF || - fogSettings.far.quality != FogQuality.OFF)) + fogSettings.far.quality != FogQuality.OFF)) { - GL11.glDisable(GL11.GL_FOG); + GL15.glDisable(GL15.GL_FOG); } } @@ -524,29 +525,7 @@ public class LodRenderer return lodModelViewMatrix; } - - - /** - * James added this to test if Vivecraft is not using - * the MC FOV setting or if the problem is deeper - */ - public enum FovTest - { - LOD_USE_FOV(true, false), - MC_USE_FOV(false, true), - NEITHER(false, false), - BOTH(true, true); - final boolean lodProjUseFov; - final boolean defaultMcProjUseFov; - - FovTest(boolean newLodProjUseFov, boolean newDefaultMcProjUseFov) - { - lodProjUseFov = newLodProjUseFov; - defaultMcProjUseFov = newDefaultMcProjUseFov; - } - } - /** * create a new projection matrix and send it over to the GPU * @param currentProjectionMatrix this is Minecraft's current projection matrix @@ -554,7 +533,7 @@ public class LodRenderer */ private void setupProjectionMatrix(Matrix4f currentProjectionMatrix, float partialTicks) { - //Minimum radious of view in 2 render distance + //Minimum radius of view in 2 render distance int minDistance = 1; // create the new projection matrix Matrix4f lodPoj = @@ -625,8 +604,8 @@ public class LodRenderer ByteBuffer temp = ByteBuffer.allocateDirect(16); temp.order(ByteOrder.nativeOrder()); - GL11.glLightfv(LOD_GL_LIGHT_NUMBER, GL11.GL_AMBIENT, (FloatBuffer) temp.asFloatBuffer().put(lightAmbient).flip()); - GL11.glEnable(LOD_GL_LIGHT_NUMBER); // Enable the above lighting + GL15.glLightfv(LOD_GL_LIGHT_NUMBER, GL15.GL_AMBIENT, (FloatBuffer) temp.asFloatBuffer().put(lightAmbient).flip()); + GL15.glEnable(LOD_GL_LIGHT_NUMBER); // Enable the above lighting RenderSystem.enableLighting(); }*/ @@ -669,9 +648,7 @@ public class LodRenderer vbosCenter = result.drawableCenterChunkPos; } - /** - * Calls the BufferBuilder's destroyBuffers method. - */ + /** Calls the BufferBuilder's destroyBuffers method. */ public void destroyBuffers() { lodBufferBuilder.destroyBuffers(); @@ -684,9 +661,7 @@ public class LodRenderer } - /** - * Return what fog settings should be used when rendering. - */ + /** Return what fog settings should be used when rendering. */ private NearFarFogSettings determineFogSettings() { NearFarFogSettings fogSettings = new NearFarFogSettings();