aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/makamys/lodmod/ducks/ITessellator.java2
-rw-r--r--src/main/java/makamys/lodmod/mixin/MixinTessellator.java4
-rw-r--r--src/main/java/makamys/lodmod/mixin/MixinWorldRenderer.java5
-rw-r--r--src/main/java/makamys/lodmod/renderer/ChunkMesh.java9
-rw-r--r--src/main/java/makamys/lodmod/renderer/LODChunk.java3
-rw-r--r--src/main/java/makamys/lodmod/renderer/LODRenderer.java118
6 files changed, 57 insertions, 84 deletions
diff --git a/src/main/java/makamys/lodmod/ducks/ITessellator.java b/src/main/java/makamys/lodmod/ducks/ITessellator.java
index 5a57741..6c2cd9d 100644
--- a/src/main/java/makamys/lodmod/ducks/ITessellator.java
+++ b/src/main/java/makamys/lodmod/ducks/ITessellator.java
@@ -6,5 +6,5 @@ import makamys.lodmod.renderer.ChunkMesh;
import net.minecraft.client.renderer.Tessellator;
public interface ITessellator {
- public ChunkMesh toChunkMesh();
+ public ChunkMesh toChunkMesh(int pass);
}
diff --git a/src/main/java/makamys/lodmod/mixin/MixinTessellator.java b/src/main/java/makamys/lodmod/mixin/MixinTessellator.java
index 3624832..eea2a31 100644
--- a/src/main/java/makamys/lodmod/mixin/MixinTessellator.java
+++ b/src/main/java/makamys/lodmod/mixin/MixinTessellator.java
@@ -54,7 +54,7 @@ abstract class MixinTessellator implements ITessellator {
}
}*/
- public ChunkMesh toChunkMesh() {
+ public ChunkMesh toChunkMesh(int pass) {
if(this.vertexCount % 4 != 0) {
System.out.println("Error: Vertex count is not a multiple of 4");
return null;
@@ -122,7 +122,7 @@ abstract class MixinTessellator implements ITessellator {
return new ChunkMesh(
(int)(-xOffset / 16), (int)(-yOffset / 16), (int)(-zOffset / 16),
new ChunkMesh.Flags(hasTexture, hasBrightness, hasColor, hasNormals),
- quadCount, quads);
+ quadCount, quads, pass);
} else {
return null;
}
diff --git a/src/main/java/makamys/lodmod/mixin/MixinWorldRenderer.java b/src/main/java/makamys/lodmod/mixin/MixinWorldRenderer.java
index 8146431..a3a0e60 100644
--- a/src/main/java/makamys/lodmod/mixin/MixinWorldRenderer.java
+++ b/src/main/java/makamys/lodmod/mixin/MixinWorldRenderer.java
@@ -23,6 +23,7 @@ import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.client.renderer.entity.RenderItem;
+import net.minecraft.entity.EntityLivingBase;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.ChunkCache;
import net.minecraft.world.World;
@@ -78,9 +79,9 @@ abstract class MixinWorldRenderer implements IWorldRenderer {
}
@Inject(method = "postRenderBlocks", at = @At(value = "HEAD"))
- private void prePostRenderBlocks(CallbackInfo ci) {
+ private void prePostRenderBlocks(int pass, EntityLivingBase entity, CallbackInfo ci) {
if(LODMod.isActive()) {
- chunkMeshes.add(((ITessellator)Tessellator.instance).toChunkMesh());
+ chunkMeshes.add(((ITessellator)Tessellator.instance).toChunkMesh(pass));
}
}
diff --git a/src/main/java/makamys/lodmod/renderer/ChunkMesh.java b/src/main/java/makamys/lodmod/renderer/ChunkMesh.java
index aacb7ca..cbac60f 100644
--- a/src/main/java/makamys/lodmod/renderer/ChunkMesh.java
+++ b/src/main/java/makamys/lodmod/renderer/ChunkMesh.java
@@ -35,9 +35,6 @@ import net.minecraft.tileentity.TileEntity;
public class ChunkMesh extends Mesh {
- int x;
- int y;
- int z;
Flags flags;
NBTBase nbtData;
@@ -45,12 +42,13 @@ public class ChunkMesh extends Mesh {
public static int usedRAM = 0;
public static int instances = 0;
- public ChunkMesh(int x, int y, int z, Flags flags, int quadCount, byte[] data, List<String> stringTable) {
+ public ChunkMesh(int x, int y, int z, Flags flags, int quadCount, byte[] data, List<String> stringTable, int pass) {
this.x = x;
this.y = y;
this.z = z;
this.flags = flags;
this.quadCount = quadCount;
+ this.pass = pass;
this.buffer = createBuffer(data, stringTable);
this.nbtData = new NBTTagByteArray(data);
@@ -59,12 +57,13 @@ public class ChunkMesh extends Mesh {
instances++;
}
- public ChunkMesh(int x, int y, int z, Flags flags, int quadCount, List<MeshQuad> quads) {
+ public ChunkMesh(int x, int y, int z, Flags flags, int quadCount, List<MeshQuad> quads, int pass) {
this.x = x;
this.y = y;
this.z = z;
this.flags = flags;
this.quadCount = quadCount;
+ this.pass = pass;
this.nbtData = toNBT(quads, quadCount);
// TODO move this somewhere else
diff --git a/src/main/java/makamys/lodmod/renderer/LODChunk.java b/src/main/java/makamys/lodmod/renderer/LODChunk.java
index 557b37e..5ee5f35 100644
--- a/src/main/java/makamys/lodmod/renderer/LODChunk.java
+++ b/src/main/java/makamys/lodmod/renderer/LODChunk.java
@@ -38,8 +38,7 @@ public class LODChunk {
byte[] data = chunkMeshesCompound.getByteArray(key);
- chunkMeshes[keyInt] = new ChunkMesh(x, keyInt / 2, z, new ChunkMesh.Flags(true, true, true, false), data.length / (2 + 4 * (3 + 2 + 2 + 4)), data, spriteList);
- chunkMeshes[keyInt].pass = keyInt % 2;
+ chunkMeshes[keyInt] = new ChunkMesh(x, keyInt / 2, z, new ChunkMesh.Flags(true, true, true, false), data.length / (2 + 4 * (3 + 2 + 2 + 4)), data, spriteList, keyInt % 2);
}
}
diff --git a/src/main/java/makamys/lodmod/renderer/LODRenderer.java b/src/main/java/makamys/lodmod/renderer/LODRenderer.java
index f8dd200..5f307df 100644
--- a/src/main/java/makamys/lodmod/renderer/LODRenderer.java
+++ b/src/main/java/makamys/lodmod/renderer/LODRenderer.java
@@ -63,7 +63,8 @@ public class LODRenderer {
private boolean hasServerInited = false;
private Map<ChunkCoordIntPair, LODRegion> loadedRegionsMap = new HashMap<>();
- private List<Mesh> sentMeshes = new ArrayList<>();
+ private List<Mesh> sentMeshes1 = new ArrayList<>();
+ private List<Mesh> sentMeshes2 = new ArrayList<>();
// TODO make these packets to make this work on dedicated servers
Queue<Chunk> farChunks = new ConcurrentLinkedQueue<>();
@@ -96,11 +97,27 @@ public class LODRenderer {
}
if(renderLOD) {
+ initIndexBuffers();
render();
}
}
}
+ private void initIndexBuffers() {
+ piFirst.limit(sentMeshes1.size() + sentMeshes2.size());
+ piCount.limit(sentMeshes1.size() + sentMeshes2.size());
+ for(List<Mesh> sentMeshes : Arrays.asList(sentMeshes1, sentMeshes2)) {
+ for(Mesh mesh : sentMeshes) {
+ if(mesh.visible) {
+ piFirst.put(mesh.iFirst);
+ piCount.put(mesh.iCount);
+ }
+ }
+ }
+ piFirst.flip();
+ piCount.flip();
+ }
+
private void mainLoop() {
while(!farChunks.isEmpty()) {
LODChunk lodChunk = receiveFarChunk(farChunks.remove());
@@ -162,7 +179,6 @@ public class LODRenderer {
//LODChunk chunk = getLODChunk(9, -18);
//setMeshVisible(chunk.chunkMeshes[7], false, true);
//freezeMeshes = false;
- resort();
//chunk.chunkMeshes[7].quadCount = 256;
//setMeshVisible(chunk.chunkMeshes[7], true, true);
}
@@ -172,22 +188,6 @@ public class LODRenderer {
}
}
- private void resort() {
- List<Mesh> visibleMeshes = new ArrayList<>();
- for(Mesh mesh : sentMeshes) {
- if(mesh.visible) {
- setMeshVisible(mesh, false);
- visibleMeshes.add(mesh);
- }
- }
-
- Entity player = (Entity)Minecraft.getMinecraft().getIntegratedServer().getConfigurationManager().playerEntityList.get(0);
- visibleMeshes.sort(new MeshDistanceComparator(player));
- for(Mesh mesh : visibleMeshes) {
- setMeshVisible(mesh, true);
- }
- }
-
private void runGC() {
nextMeshOffset = 0;
nextTri = 0;
@@ -201,32 +201,34 @@ public class LODRenderer {
int deletedNum = 0;
int deletedRAM = 0;
- for(Iterator<Mesh> it = sentMeshes.iterator(); it.hasNext(); ) {
- Mesh mesh = it.next();
- if(!mesh.pendingGPUDelete) {
- if(mesh.offset != nextMeshOffset) {
- glBufferSubData(GL_ARRAY_BUFFER, nextMeshOffset, mesh.buffer);
+ for(List<Mesh> sentMeshes : Arrays.asList(sentMeshes1, sentMeshes2)) {
+ for(Iterator<Mesh> it = sentMeshes.iterator(); it.hasNext(); ) {
+ Mesh mesh = it.next();
+ if(!mesh.pendingGPUDelete) {
+ if(mesh.offset != nextMeshOffset) {
+ glBufferSubData(GL_ARRAY_BUFFER, nextMeshOffset, mesh.buffer);
+ }
+ mesh.iFirst = nextTri;
+ mesh.offset = nextMeshOffset;
+
+ nextMeshOffset += mesh.buffer.limit();
+ nextTri += mesh.quadCount * 6;
+
+ piFirst.limit(piFirst.limit() + 1);
+ piFirst.put(nextMesh, mesh.iFirst);
+ piCount.limit(piCount.limit() + 1);
+ piCount.put(nextMesh, mesh.iCount);
+ nextMesh++;
+ } else {
+ mesh.iFirst = mesh.offset = -1;
+ mesh.visible = false;
+ mesh.pendingGPUDelete = false;
+ it.remove();
+ deletedNum++;
+ deletedRAM += mesh.buffer.limit();
}
- mesh.iFirst = nextTri;
- mesh.offset = nextMeshOffset;
-
- nextMeshOffset += mesh.buffer.limit();
- nextTri += mesh.quadCount * 6;
-
- piFirst.limit(piFirst.limit() + 1);
- piFirst.put(nextMesh, mesh.iFirst);
- piCount.limit(piCount.limit() + 1);
- piCount.put(nextMesh, mesh.iCount);
- nextMesh++;
- } else {
- mesh.iFirst = mesh.offset = -1;
- mesh.visible = false;
- mesh.pendingGPUDelete = false;
- it.remove();
- deletedNum++;
- deletedRAM += mesh.buffer.limit();
- }
- }
+ }
+ }
System.out.println("Deleted " + deletedNum + " meshes, freeing up " + (deletedRAM / 1024 / 1024) + "MB of VRAM");
@@ -566,37 +568,9 @@ public class LODRenderer {
if(mesh.visible != visible) {
if(!visible) {
- if(mesh.iFirst == -1) {
- System.out.println("uh");
- }
- piFirst.position(0);
- int[] piFirstArr = new int[piFirst.limit()];
- piFirst.get(piFirstArr);
- int index = ArrayUtils.indexOf(piFirstArr, mesh.iFirst);
- piFirstArr = ArrayUtils.remove(piFirstArr, index);
- piFirst.position(0);
- piFirst.put(piFirstArr);
- piFirst.position(0);
- piFirst.limit(piFirst.limit() - 1);
-
- piCount.position(0);
-
deleteMeshFromGPU(mesh);
- int[] piCountArr = new int[piCount.limit()];
- piCount.get(piCountArr);
- piCountArr = ArrayUtils.remove(piCountArr, index);
- piCount.position(0);
- piCount.put(piCountArr);
- piCount.position(0);
- piCount.limit(piCount.limit() - 1);
- nextMesh--;
} else if(visible) {
sendMeshToGPU(mesh);
- piFirst.limit(piFirst.limit() + 1);
- piFirst.put(nextMesh, mesh.iFirst);
- piCount.limit(piCount.limit() + 1);
- piCount.put(nextMesh, mesh.iCount);
- nextMesh++;
}
mesh.visible = visible;
}
@@ -620,7 +594,7 @@ public class LODRenderer {
nextTri += mesh.quadCount * 6;
nextMeshOffset += mesh.buffer.limit();
- sentMeshes.add(mesh);
+ (mesh.pass == 0 ? sentMeshes1 : sentMeshes2).add(mesh);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);