fix to memory use

This commit is contained in:
Leonardo
2021-09-22 13:17:56 +02:00
parent 265abb64b3
commit fb480c2695
5 changed files with 49 additions and 26 deletions
@@ -28,7 +28,6 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.locks.ReentrantLock;
import com.seibel.lod.render.RenderUtil;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL15C;
@@ -129,7 +128,7 @@ public class LodBufferBuilder
*/
private volatile ChunkPos drawableCenterChunkPos = new ChunkPos(0, 0);
private volatile ChunkPos buildableCenterChunkPos = new ChunkPos(0, 0);
private volatile boolean firstSetup = true;
public LodBufferBuilder()
{
@@ -175,6 +174,7 @@ public class LodBufferBuilder
ArrayList<Callable<Boolean>> nodeToRenderThreads = new ArrayList<>(lodDim.getWidth() * lodDim.getWidth());
//setupBuffers(lodDim);
startBuffers(fullRegen, lodDim);
// =====================//
@@ -204,7 +204,7 @@ public class LodBufferBuilder
{
for (int zRegion = 0; zRegion < lodDim.getWidth(); zRegion++)
{
if (lodDim.getRegenByArrayIndex(xRegion, zRegion) || fullRegen)
if (lodDim.isRegionToRegen(xRegion, zRegion) || fullRegen)
{
RegionPos regionPos = new RegionPos(
xRegion + lodDim.getCenterX() - Math.floorDiv(lodDim.getWidth(), 2),
@@ -219,7 +219,7 @@ public class LodBufferBuilder
// make sure the buffers weren't
// changed while we were running this method
if (currentBuffer == null || (currentBuffer != null && !currentBuffer.building()))
if (currentBuffer == null || !currentBuffer.building())
return;
byte minDetail = region.getMinDetailLevel();
@@ -403,27 +403,33 @@ public class LodBufferBuilder
* <p>
* May have to wait for the bufferLock to open.
*/
public void setupBuffers(int numbRegionsWide, LodDimension lodDimension)
public void setupBuffers(LodDimension lodDimension)
{
bufferLock.lock();
previousRegionWidth = numbRegionsWide;
int numbRegionsWide = lodDimension.getWidth();
int bufferMaxCapacity;
buildableBuffers = new BufferBuilder[numbRegionsWide][numbRegionsWide];
buildableVbos = new VertexBuffer[numbRegionsWide][numbRegionsWide];
drawableVbos = new VertexBuffer[numbRegionsWide][numbRegionsWide];
//if(previousRegionWidth != numbRegionsWide || firstSetup)
//{
firstSetup = false;
previousRegionWidth = numbRegionsWide;
buildableBuffers = new BufferBuilder[numbRegionsWide][numbRegionsWide];
buildableVbos = new VertexBuffer[numbRegionsWide][numbRegionsWide];
drawableVbos = new VertexBuffer[numbRegionsWide][numbRegionsWide];
//}
for (int x = 0; x < numbRegionsWide; x++)
{
for (int z = 0; z < numbRegionsWide; z++)
{
bufferMaxCapacity = lodDimension.getMemoryRequired(x,z);
buildableBuffers[x][z] = new BufferBuilder(bufferMaxCapacity);
//if(lodDimension.isBufferToSetup(x,z))
//{
bufferMaxCapacity = lodDimension.getMemoryRequired(x, z, LodConfig.CLIENT.graphics.lodTemplate.get());
buildableBuffers[x][z] = new BufferBuilder(bufferMaxCapacity);
buildableVbos[x][z] = new VertexBuffer(LodRenderer.LOD_VERTEX_FORMAT);
drawableVbos[x][z] = new VertexBuffer(LodRenderer.LOD_VERTEX_FORMAT);
buildableVbos[x][z] = new VertexBuffer(LodRenderer.LOD_VERTEX_FORMAT);
drawableVbos[x][z] = new VertexBuffer(LodRenderer.LOD_VERTEX_FORMAT);
//}
}
}
@@ -455,7 +461,7 @@ public class LodBufferBuilder
{
for (int z = 0; z < buildableBuffers.length; z++)
{
if (fullRegen || lodDim.getRegenByArrayIndex(x, z))
if (fullRegen || lodDim.isRegionToRegen(x, z))
{
buildableBuffers[x][z].begin(GL11.GL_QUADS, LodRenderer.LOD_VERTEX_FORMAT);
}
@@ -470,7 +476,7 @@ public class LodBufferBuilder
{
for (int x = 0; x < buildableBuffers.length; x++)
for (int z = 0; z < buildableBuffers.length; z++)
if (buildableBuffers[x][z] != null && buildableBuffers[x][z].building() && (fullRegen || lodDim.getRegenByArrayIndex(x, z)))
if (buildableBuffers[x][z] != null && buildableBuffers[x][z].building() && (fullRegen || lodDim.isRegionToRegen(x, z)))
buildableBuffers[x][z].end();
}
@@ -491,7 +497,7 @@ public class LodBufferBuilder
{
for (int z = 0; z < buildableVbos.length; z++)
{
if (fullRegen || lodDim.getRegenByArrayIndex(x, z))
if (fullRegen || lodDim.isRegionToRegen(x, z))
{
ByteBuffer builderBuffer = buildableBuffers[x][z].popNextBuffer().getSecond();
vboUpload(buildableVbos[x][z], builderBuffer);
@@ -219,7 +219,7 @@ public class LodDimensionFileHandler
{
for (int j = 0; j < loadedDimension.getWidth(); j++)
{
if (loadedDimension.getRegenByArrayIndex(i,j) && loadedDimension.getRegionByArrayIndex(i,j) != null)
if (loadedDimension.isRegionToRegen(i,j) && loadedDimension.getRegionByArrayIndex(i,j) != null)
{
saveRegionToFile(loadedDimension.getRegionByArrayIndex(i,j));
loadedDimension.setRegenByArrayIndex(i, j,false);
@@ -25,6 +25,7 @@ import java.util.concurrent.Executors;
import com.seibel.lod.config.LodConfig;
import com.seibel.lod.enums.DistanceGenerationMode;
import com.seibel.lod.enums.GenerationPriority;
import com.seibel.lod.enums.LodTemplate;
import com.seibel.lod.enums.VerticalQuality;
import com.seibel.lod.handlers.LodDimensionFileHandler;
import com.seibel.lod.util.DataPointUtil;
@@ -68,6 +69,7 @@ public class LodDimension
* stores all the regions in this dimension
*/
public volatile LodRegion[][] regions;
/**
* stores if the region at the given x and z index needs to be saved to disk
*/
@@ -76,6 +78,10 @@ public class LodDimension
* stores if the region at the given x and z index needs to be regenerated
*/
private volatile boolean[][] regionNeedsRegen;
/**
* stores if the buffer size at the given x and z index needs to be changed
*/
private volatile boolean[][] setupBuffer;
/**
* if true that means there are regions in this dimension
@@ -138,6 +144,7 @@ public class LodDimension
regions = new LodRegion[width][width];
isRegionDirty = new boolean[width][width];
regionNeedsRegen = new boolean[width][width];
setupBuffer = new boolean[width][width];
//treeGenerator((int) mc.player.getX(),(int) mc.player.getZ());
@@ -380,6 +387,7 @@ public class LodDimension
if (regions[x][z].getMinDetailLevel() > levelToCut)
{
regions[x][z].cutTree(levelToCut);
setupBuffer[x][z] = true;
}
}
@@ -440,12 +448,14 @@ public class LodDimension
}
regionNeedsRegen[x][z] = true;
regenDimension = true;
setupBuffer[x][z] = true;
} else if (region.getMinDetailLevel() > levelToGen)
{
//Second case, region has been initialized but at a higher level
//We expand the region by introducing the missing layer
region.expand(levelToGen);
setupBuffer[x][z] = true;
}
}
}
@@ -686,10 +696,14 @@ public class LodDimension
region.clear(detailLevel, posX, posZ);
}
public boolean getRegenByArrayIndex(int xIndex, int zIndex)
public boolean isRegionToRegen(int xIndex, int zIndex)
{
return regionNeedsRegen[xIndex][zIndex];
}
public boolean isBufferToSetup(int xIndex, int zIndex)
{
return setupBuffer[xIndex][zIndex];
}
public void setRegenByArrayIndex(int xIndex, int zIndex, boolean newRegen)
{
@@ -804,6 +818,7 @@ public class LodDimension
regions = new LodRegion[width][width];
isRegionDirty = new boolean[width][width];
regionNeedsRegen = new boolean[width][width];
setupBuffer = new boolean[width][width];
// populate isRegionDirty
for (int i = 0; i < width; i++)
@@ -840,11 +855,13 @@ public class LodDimension
return stringBuilder.toString();
}
public int getMemoryRequired(int x, int z)
public int getMemoryRequired(int x, int z, LodTemplate template)
{
/*return regions[x][z].getMinMemoryNeeded(template);*/
int minDistance = LevelPosUtil.minDistance(LodUtil.REGION_DETAIL_LEVEL, x, z, halfWidth, halfWidth);
int detail = DetailDistanceUtil.getGenerationDetailFromDistance(minDistance);
int levelToGen = DetailDistanceUtil.getLodGenDetail(detail).detailLevel;
int detail = DetailDistanceUtil.getTreeCutDetailFromDistance(minDistance);
int levelToGen = DetailDistanceUtil.getCutLodDetail(detail);
int size = 1 << (LodUtil.REGION_DETAIL_LEVEL - levelToGen);
int maxVerticalData = DetailDistanceUtil.getMaxVerticalData(levelToGen);
int numberOfLods = size * size * maxVerticalData;
@@ -339,7 +339,7 @@ public class ClientProxy
// update the dimensions to fit the new width
lodWorld.resizeDimensionRegionWidth(newWidth);
lodBuilder.defaultDimensionWidthInRegions = newWidth;
renderer.setupBuffers(newWidth, lodWorld.getLodDimension(mc.getClientWorld().dimensionType()));
renderer.setupBuffers(lodWorld.getLodDimension(mc.getClientWorld().dimensionType()));
recalculateWidths = false;
//LOGGER.info("new dimension width in regions: " + newWidth + "\t potential: " + newWidth );
@@ -589,7 +589,7 @@ public class LodRenderer
/**
* Create all buffers that will be used.
*/
public void setupBuffers(int numbRegionsWide, LodDimension lodDim)
public void setupBuffers(LodDimension lodDim)
{
// calculate the max amount of memory needed (in bytes)
/*int bufferMemory = RenderUtil.getBufferMemoryForRegion(lodDim);*/
@@ -603,7 +603,7 @@ public class LodRenderer
+ " It tried to allocate \"" + bufferMemory + "\" bytes, when \"" + MAX_ALOCATEABLE_DIRECT_MEMORY + "\" is the max.");
}*/
lodBufferBuilder.setupBuffers(numbRegionsWide, lodDim);
lodBufferBuilder.setupBuffers(lodDim);
}