From 68357a8cc9c8cf9eab9e177cb6aea18cfbc3f584 Mon Sep 17 00:00:00 2001 From: FalsePattern Date: Fri, 1 Dec 2023 13:29:09 +0100 Subject: unify stride calculation into NeoRenderer --- .../java/makamys/neodymium/renderer/ChunkMesh.java | 12 ++++++------ .../makamys/neodymium/renderer/GPUMemoryManager.java | 4 ++-- src/main/java/makamys/neodymium/renderer/Mesh.java | 2 -- .../java/makamys/neodymium/renderer/MeshQuad.java | 19 +++++++------------ .../java/makamys/neodymium/renderer/NeoRenderer.java | 10 +++++++++- .../neodymium/renderer/attribs/AttributeSet.java | 7 ++++++- 6 files changed, 30 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/main/java/makamys/neodymium/renderer/ChunkMesh.java b/src/main/java/makamys/neodymium/renderer/ChunkMesh.java index 280772b..663cce5 100644 --- a/src/main/java/makamys/neodymium/renderer/ChunkMesh.java +++ b/src/main/java/makamys/neodymium/renderer/ChunkMesh.java @@ -9,6 +9,9 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List; + +import lombok.val; +import makamys.neodymium.Neodymium; import org.lwjgl.BufferUtils; import org.lwjgl.opengl.GL11; @@ -170,7 +173,8 @@ public class ChunkMesh extends Mesh { } private ByteBuffer createBuffer(List quads, int quadCount) { - ByteBuffer buffer = BufferUtils.createByteBuffer(quadCount * 4 * MeshQuad.getStride()); + val stride = Neodymium.renderer.getStride(); + ByteBuffer buffer = BufferUtils.createByteBuffer(quadCount * 4 * stride); BufferWriter out = new BufferWriter(buffer); boolean sortByNormals = pass == 0; @@ -188,7 +192,7 @@ public class ChunkMesh extends Mesh { if(subMeshStart[subMeshStartIdx] == -1) { subMeshStart[subMeshStartIdx] = i; } - quad.writeToBuffer(out); + quad.writeToBuffer(out, stride); i++; } else if(sortByNormals){ break; @@ -221,10 +225,6 @@ public class ChunkMesh extends Mesh { destroy(); } - public int getStride() { - return MeshQuad.getStride(); - } - static List getChunkMesh(int theX, int theY, int theZ) { WorldRenderer wr = new WorldRenderer(Minecraft.getMinecraft().theWorld, new ArrayList(), theX * 16, theY * 16, theZ * 16, 100000); diff --git a/src/main/java/makamys/neodymium/renderer/GPUMemoryManager.java b/src/main/java/makamys/neodymium/renderer/GPUMemoryManager.java index 16a72d5..6f5e4fe 100644 --- a/src/main/java/makamys/neodymium/renderer/GPUMemoryManager.java +++ b/src/main/java/makamys/neodymium/renderer/GPUMemoryManager.java @@ -70,7 +70,7 @@ public class GPUMemoryManager { glBufferSubData(GL_ARRAY_BUFFER, offset, mesh.buffer); moved++; } - mesh.iFirst = (int)(offset / mesh.getStride()); + mesh.iFirst = (int)(offset / Neodymium.renderer.getStride()); mesh.offset = offset; } else if(mesh.gpuStatus == GPUStatus.PENDING_DELETE) { mesh.iFirst = -1; @@ -154,7 +154,7 @@ public class GPUMemoryManager { glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferSubData(GL_ARRAY_BUFFER, nextBase, mesh.buffer); - mesh.iFirst = (int)(nextBase / mesh.getStride()); + mesh.iFirst = (int)(nextBase / Neodymium.renderer.getStride()); mesh.iCount = mesh.quadCount * 4; mesh.offset = nextBase; diff --git a/src/main/java/makamys/neodymium/renderer/Mesh.java b/src/main/java/makamys/neodymium/renderer/Mesh.java index 6518c46..fbf425d 100644 --- a/src/main/java/makamys/neodymium/renderer/Mesh.java +++ b/src/main/java/makamys/neodymium/renderer/Mesh.java @@ -19,8 +19,6 @@ public abstract class Mesh { int x, y, z; public QuadNormal normal = QuadNormal.NONE; - public abstract int getStride(); - public double distSq(double x2, double y2, double z2) { return Util.distSq(x + 0.5, y + 0.5, z + 0.5, x2, y2, z2); } diff --git a/src/main/java/makamys/neodymium/renderer/MeshQuad.java b/src/main/java/makamys/neodymium/renderer/MeshQuad.java index 9c12472..98fef18 100644 --- a/src/main/java/makamys/neodymium/renderer/MeshQuad.java +++ b/src/main/java/makamys/neodymium/renderer/MeshQuad.java @@ -5,6 +5,7 @@ import java.util.Arrays; import java.util.Comparator; import java.util.Locale; +import makamys.neodymium.Neodymium; import org.lwjgl.opengl.GL11; import org.lwjgl.util.vector.Vector3f; @@ -85,8 +86,11 @@ public class MeshQuad { normal = QuadNormal.fromVector(vectorC); } - - public void writeToBuffer(BufferWriter out) throws IOException { + + /** + * @implSpec This needs to be kept in sync with the attributes in {@link NeoRenderer#init()} + */ + public void writeToBuffer(BufferWriter out, int expectedStride) throws IOException { for(int vertexI = 0; vertexI < 4; vertexI++) { int vi = vertexI; @@ -117,20 +121,11 @@ public class MeshQuad { out.writeInt(c); - assert out.position() % getStride() == 0; + assert out.position() % expectedStride == 0; //System.out.println("[" + vertexI + "] x: " + x + ", y: " + y + " z: " + z + ", u: " + u + ", v: " + v + ", b: " + b + ", c: " + c); } } - - public static int getStride() { - return - 3 * 4 // XYZ (float) - + 2 * (Config.shortUV ? 2 : 4) // UV (float) - + 4 // B (int) - + 4 // C (int) - ; - } @Override public String toString() { diff --git a/src/main/java/makamys/neodymium/renderer/NeoRenderer.java b/src/main/java/makamys/neodymium/renderer/NeoRenderer.java index 61dac28..048fc7a 100644 --- a/src/main/java/makamys/neodymium/renderer/NeoRenderer.java +++ b/src/main/java/makamys/neodymium/renderer/NeoRenderer.java @@ -22,6 +22,7 @@ import java.util.Map.Entry; import java.util.Set; import makamys.neodymium.renderer.attribs.AttributeSet; +import makamys.neodymium.util.BufferWriter; import org.lwjgl.BufferUtils; import org.lwjgl.input.Keyboard; import org.lwjgl.opengl.GL11; @@ -378,7 +379,10 @@ public class NeoRenderer { } private AttributeSet attributes; - + + /** + * @implSpec The attributes here need to be kept in sync with {@link MeshQuad#writeToBuffer(BufferWriter)} + */ public boolean init() { // The average mesh is 60 KB. Let's be safe and assume 8 KB per mesh. // This means 1 MB of index data per 512 MB of VRAM. @@ -417,6 +421,10 @@ public class NeoRenderer { return true; } + + public int getStride() { + return attributes.stride(); + } public void reloadShader(int pass, AttributeSet attributeSet) { for(int hasFog = 0; hasFog <= 1; hasFog++) { diff --git a/src/main/java/makamys/neodymium/renderer/attribs/AttributeSet.java b/src/main/java/makamys/neodymium/renderer/attribs/AttributeSet.java index ebf9c4c..e90bfac 100644 --- a/src/main/java/makamys/neodymium/renderer/attribs/AttributeSet.java +++ b/src/main/java/makamys/neodymium/renderer/attribs/AttributeSet.java @@ -8,14 +8,19 @@ import java.util.Map; public class AttributeSet { private final List attributes = new ArrayList<>(); + private int stride = 0; public void addAttribute(String name, int size, int elementSize, int type) { int index = attributes.size(); attributes.add(new VertexAttribute(name, index, size, elementSize, type)); + stride += elementSize * size; + } + + public int stride() { + return stride; } public void enable() { - int stride = attributes.stream().mapToInt(attrib -> attrib.size * attrib.elementSize).sum(); int offset = 0; for (int i = 0, size = attributes.size(); i < size; i++) { VertexAttribute attribute = attributes.get(i); -- cgit