diff --git a/src/main/java/com/jozufozu/flywheel/core/vertex/AbstractVertexList.java b/src/main/java/com/jozufozu/flywheel/core/vertex/AbstractVertexList.java new file mode 100644 index 000000000..a68e0c943 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/core/vertex/AbstractVertexList.java @@ -0,0 +1,49 @@ +package com.jozufozu.flywheel.core.vertex; + +import java.nio.Buffer; +import java.nio.ByteBuffer; + +import org.lwjgl.system.MemoryUtil; + +import com.jozufozu.flywheel.api.vertex.VertexList; +import com.mojang.blaze3d.platform.MemoryTracker; +import com.mojang.blaze3d.vertex.BufferBuilder; + +public abstract class AbstractVertexList implements VertexList, AutoCloseable { + + protected final ByteBuffer contents; + protected final long base; + protected final int vertexCount; + + protected AbstractVertexList(ByteBuffer copyFrom, int vertexCount) { + this.contents = MemoryTracker.create(copyFrom.capacity()); + this.vertexCount = vertexCount; + this.base = MemoryUtil.memAddress(this.contents); + init(copyFrom); + } + + public AbstractVertexList(BufferBuilder builder) { + var pair = builder.popNextBuffer(); + ByteBuffer copyFrom = pair.getSecond(); + this.contents = MemoryTracker.create(copyFrom.capacity()); + this.vertexCount = pair.getFirst().vertexCount(); + this.base = MemoryUtil.memAddress(this.contents); + init(copyFrom); + } + + private void init(ByteBuffer copyFrom) { + this.contents.order(copyFrom.order()); + this.contents.put(copyFrom); + ((Buffer) this.contents).flip(); + } + + @Override + public void close() { + MemoryUtil.memFree(contents); + } + + @Override + public int getVertexCount() { + return vertexCount; + } +} diff --git a/src/main/java/com/jozufozu/flywheel/core/vertex/BlockVertexList.java b/src/main/java/com/jozufozu/flywheel/core/vertex/BlockVertexList.java index eaac02cee..f84385f78 100644 --- a/src/main/java/com/jozufozu/flywheel/core/vertex/BlockVertexList.java +++ b/src/main/java/com/jozufozu/flywheel/core/vertex/BlockVertexList.java @@ -1,30 +1,18 @@ package com.jozufozu.flywheel.core.vertex; -import java.nio.ByteBuffer; - import com.jozufozu.flywheel.api.vertex.ShadedVertexList; -import com.jozufozu.flywheel.api.vertex.VertexList; import com.jozufozu.flywheel.core.model.ShadeSeparatedBufferBuilder; import com.jozufozu.flywheel.util.RenderMath; import com.mojang.blaze3d.vertex.BufferBuilder; -import com.mojang.datafixers.util.Pair; -public class BlockVertexList implements VertexList { +public class BlockVertexList extends AbstractVertexList { - private final ByteBuffer buffer; - private final int vertexCount; private final int stride; public BlockVertexList(BufferBuilder builder) { - Pair data = builder.popNextBuffer(); - buffer = data.getSecond(); - - stride = builder.getVertexFormat() + super(builder); + this.stride = builder.getVertexFormat() .getVertexSize(); - - vertexCount = data.getFirst() - .vertexCount(); - } @Override @@ -38,72 +26,67 @@ public class BlockVertexList implements VertexList { @Override public float getX(int index) { - return buffer.getFloat(vertIdx(index)); + return contents.getFloat(vertIdx(index)); } @Override public float getY(int index) { - return buffer.getFloat(vertIdx(index) + 4); + return contents.getFloat(vertIdx(index) + 4); } @Override public float getZ(int index) { - return buffer.getFloat(vertIdx(index) + 8); + return contents.getFloat(vertIdx(index) + 8); } @Override public byte getR(int index) { - return buffer.get(vertIdx(index) + 12); + return contents.get(vertIdx(index) + 12); } @Override public byte getG(int index) { - return buffer.get(vertIdx(index) + 13); + return contents.get(vertIdx(index) + 13); } @Override public byte getB(int index) { - return buffer.get(vertIdx(index) + 14); + return contents.get(vertIdx(index) + 14); } @Override public byte getA(int index) { - return buffer.get(vertIdx(index) + 15); + return contents.get(vertIdx(index) + 15); } @Override public float getU(int index) { - return buffer.getFloat(vertIdx(index) + 16); + return contents.getFloat(vertIdx(index) + 16); } @Override public float getV(int index) { - return buffer.getFloat(vertIdx(index) + 20); + return contents.getFloat(vertIdx(index) + 20); } @Override public int getLight(int index) { - return buffer.getInt(vertIdx(index) + 24); + return contents.getInt(vertIdx(index) + 24); } @Override public float getNX(int index) { - return RenderMath.f(buffer.get(vertIdx(index) + 28)); + return RenderMath.f(contents.get(vertIdx(index) + 28)); } @Override public float getNY(int index) { - return RenderMath.f(buffer.get(vertIdx(index) + 29)); + return RenderMath.f(contents.get(vertIdx(index) + 29)); } @Override public float getNZ(int index) { - return RenderMath.f(buffer.get(vertIdx(index) + 30)); - } - - @Override - public int getVertexCount() { - return vertexCount; + return RenderMath.f(contents.get(vertIdx(index) + 30)); } public static class Shaded extends BlockVertexList implements ShadedVertexList { diff --git a/src/main/java/com/jozufozu/flywheel/core/vertex/BlockVertexListUnsafe.java b/src/main/java/com/jozufozu/flywheel/core/vertex/BlockVertexListUnsafe.java index 839687a7e..959b90ef3 100644 --- a/src/main/java/com/jozufozu/flywheel/core/vertex/BlockVertexListUnsafe.java +++ b/src/main/java/com/jozufozu/flywheel/core/vertex/BlockVertexListUnsafe.java @@ -10,20 +10,10 @@ import com.jozufozu.flywheel.api.vertex.VertexList; import com.jozufozu.flywheel.util.RenderMath; import com.mojang.blaze3d.platform.MemoryTracker; -public class BlockVertexListUnsafe implements VertexList { - - private final ByteBuffer contents; - private final int vertexCount; - private final long base; +public class BlockVertexListUnsafe extends AbstractVertexList { public BlockVertexListUnsafe(ByteBuffer copyFrom, int vertexCount) { - this.contents = MemoryTracker.create(copyFrom.capacity()); - this.contents.order(copyFrom.order()); - this.contents.put(copyFrom); - ((Buffer) this.contents).flip(); - - this.base = MemoryUtil.memAddress(this.contents); - this.vertexCount = vertexCount; + super(copyFrom, vertexCount); } private long ptr(long index) { @@ -100,11 +90,6 @@ public class BlockVertexListUnsafe implements VertexList { return RenderMath.f(MemoryUtil.memGetByte(ptr(index) + 30)); } - @Override - public int getVertexCount() { - return vertexCount; - } - public static class Shaded extends BlockVertexListUnsafe implements ShadedVertexList { private final int unshadedStartVertex; diff --git a/src/main/java/com/jozufozu/flywheel/core/vertex/PosTexNormalVertexListUnsafe.java b/src/main/java/com/jozufozu/flywheel/core/vertex/PosTexNormalVertexListUnsafe.java index e94861b47..79baf8e00 100644 --- a/src/main/java/com/jozufozu/flywheel/core/vertex/PosTexNormalVertexListUnsafe.java +++ b/src/main/java/com/jozufozu/flywheel/core/vertex/PosTexNormalVertexListUnsafe.java @@ -9,20 +9,10 @@ import com.jozufozu.flywheel.api.vertex.VertexList; import com.jozufozu.flywheel.util.RenderMath; import com.mojang.blaze3d.platform.MemoryTracker; -public class PosTexNormalVertexListUnsafe implements VertexList { - - private final ByteBuffer contents; - private final int vertexCount; - private final long base; +public class PosTexNormalVertexListUnsafe extends AbstractVertexList { public PosTexNormalVertexListUnsafe(ByteBuffer copyFrom, int vertexCount) { - this.contents = MemoryTracker.create(copyFrom.capacity()); - this.contents.order(copyFrom.order()); - this.contents.put(copyFrom); - ((Buffer) this.contents).flip(); - - this.base = MemoryUtil.memAddress(this.contents); - this.vertexCount = vertexCount; + super(copyFrom, vertexCount); } private long ptr(long idx) { @@ -93,9 +83,4 @@ public class PosTexNormalVertexListUnsafe implements VertexList { public float getNZ(int index) { return RenderMath.f(MemoryUtil.memGetByte(ptr(index) + 22)); } - - @Override - public int getVertexCount() { - return vertexCount; - } }