diff --git a/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java b/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java index 608b6d2ae..586c66fc5 100644 --- a/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java +++ b/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java @@ -3,6 +3,7 @@ package com.gtnewhorizons.angelica.glsm; import com.gtnewhorizon.gtnhlib.client.opengl.GLCaps; import com.gtnewhorizon.gtnhlib.client.opengl.UniversalVAO; import com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFlags; import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormatElement.Usage; import com.gtnewhorizon.gtnhlib.client.renderer.DirectTessellator; import com.gtnewhorizon.gtnhlib.client.renderer.stacks.IStateStack; @@ -257,6 +258,7 @@ public class GLStateManager { } } protected static final IntegerStateStack activeTextureUnit = new IntegerStateStack(0); + private static int clientActiveTextureUnit = 0; protected static final IntegerStateStack shadeModelState = new IntegerStateStack(GL11.GL_SMOOTH); // Client array state (not push/pop tracked, but needed for display list compilation) @@ -390,8 +392,9 @@ public class GLStateManager { @Getter protected static int listBase = 0; @Getter protected static int boundVBO; + @Getter protected static int boundEBO; @Getter protected static int boundVAO; - private static int defaultVAO; // Non-zero on core profile + @Getter private static int defaultVAO; // Non-zero on core profile public static void reset() { runningSplash = true; @@ -929,6 +932,7 @@ public class GLStateManager { case GL11.GL_STENCIL_CLEAR_VALUE -> stencilState.getClearValue(); case GL15.GL_ARRAY_BUFFER_BINDING -> boundVBO; + case GL15.GL_ELEMENT_ARRAY_BUFFER_BINDING -> boundEBO; case GL20.GL_CURRENT_PROGRAM -> activeProgram; @@ -1580,6 +1584,22 @@ public class GLStateManager { } } + public static void glClientActiveTexture(int texture) { + clientActiveTextureUnit = texture - GL13.GL_TEXTURE0; + } + + public static int getClientActiveTextureUnit() { + return clientActiveTextureUnit; + } + + private static int texCoordAttributeLocation() { + return switch (clientActiveTextureUnit) { + case 0 -> Usage.PRIMARY_UV.getAttributeLocation(); + case 1 -> Usage.SECONDARY_UV.getAttributeLocation(); + default -> -1; + }; + } + private static int getBoundTexture() { return getBoundTexture(activeTextureUnit.getValue()); } @@ -1919,7 +1939,11 @@ public class GLStateManager { throw new UnsupportedOperationException("glDrawElements in display lists not yet implemented - if you see this, please report!"); } ShaderManager.getInstance().preDraw(); - GL11.glDrawElements(mode, indices); + if (mode == GL11.GL_QUADS) { + QuadConverter.drawQuadElementsAsTriangles(indices); + } else { + GL11.glDrawElements(mode, indices); + } } public static void glDrawElements(int mode, ShortBuffer indices) { @@ -1927,7 +1951,11 @@ public class GLStateManager { throw new UnsupportedOperationException("glDrawElements in display lists not yet implemented - if you see this, please report!"); } ShaderManager.getInstance().preDraw(); - GL11.glDrawElements(mode, indices); + if (mode == GL11.GL_QUADS) { + QuadConverter.drawQuadElementsAsTriangles(indices); + } else { + GL11.glDrawElements(mode, indices); + } } public static void glDrawElements(int mode, int count, int type, ByteBuffer indices) { @@ -1935,7 +1963,11 @@ public class GLStateManager { throw new UnsupportedOperationException("glDrawElements in display lists not yet implemented - if you see this, please report!"); } ShaderManager.getInstance().preDraw(); - GL11.glDrawElements(mode, count, type, indices); + if (mode == GL11.GL_QUADS) { + QuadConverter.drawQuadElementsAsTriangles(count, type, indices); + } else { + GL11.glDrawElements(mode, count, type, indices); + } } public static void glDrawElements(int mode, int indices_count, int type, long indices_buffer_offset) { @@ -1951,7 +1983,11 @@ public class GLStateManager { } } ShaderManager.getInstance().preDraw(); - GL11.glDrawElements(mode, indices_count, type, indices_buffer_offset); + if (mode == GL11.GL_QUADS) { + QuadConverter.drawQuadElementsAsTriangles(indices_count, type, indices_buffer_offset); + } else { + GL11.glDrawElements(mode, indices_count, type, indices_buffer_offset); + } } public static void glDrawBuffer(int mode) { @@ -2052,39 +2088,53 @@ public class GLStateManager { public static void glTexCoordPointer(int size, int type, int stride, ByteBuffer pointer) { if (!ShaderManager.getInstance().isActive()) return; - GL20.glVertexAttribPointer(Usage.PRIMARY_UV.getAttributeLocation(), size, type, false, stride, pointer); + final int loc = texCoordAttributeLocation(); + if (loc < 0) return; + GL20.glVertexAttribPointer(loc, size, type, false, stride, pointer); } public static void glTexCoordPointer(int size, int type, int stride, FloatBuffer pointer) { if (!ShaderManager.getInstance().isActive()) return; - GL20.glVertexAttribPointer(Usage.PRIMARY_UV.getAttributeLocation(), size, GL11.GL_FLOAT, false, stride, MemoryUtilities.memByteBuffer(pointer)); + final int loc = texCoordAttributeLocation(); + if (loc < 0) return; + GL20.glVertexAttribPointer(loc, size, GL11.GL_FLOAT, false, stride, MemoryUtilities.memByteBuffer(pointer)); } public static void glTexCoordPointer(int size, int type, int stride, ShortBuffer pointer) { if (!ShaderManager.getInstance().isActive()) return; - GL20.glVertexAttribPointer(Usage.PRIMARY_UV.getAttributeLocation(), size, GL11.GL_SHORT, false, stride, MemoryUtilities.memByteBuffer(pointer)); + final int loc = texCoordAttributeLocation(); + if (loc < 0) return; + GL20.glVertexAttribPointer(loc, size, GL11.GL_SHORT, false, stride, MemoryUtilities.memByteBuffer(pointer)); } public static void glTexCoordPointer(int size, int type, int stride, IntBuffer pointer) { if (!ShaderManager.getInstance().isActive()) return; - GL20.glVertexAttribPointer(Usage.PRIMARY_UV.getAttributeLocation(), size, GL11.GL_INT, false, stride, MemoryUtilities.memByteBuffer(pointer)); + final int loc = texCoordAttributeLocation(); + if (loc < 0) return; + GL20.glVertexAttribPointer(loc, size, GL11.GL_INT, false, stride, MemoryUtilities.memByteBuffer(pointer)); } public static void glTexCoordPointer(int size, int type, int stride, long pointer_buffer_offset) { if (!ShaderManager.getInstance().isActive()) return; - GL20.glVertexAttribPointer(Usage.PRIMARY_UV.getAttributeLocation(), size, type, false, stride, pointer_buffer_offset); + final int loc = texCoordAttributeLocation(); + if (loc < 0) return; + GL20.glVertexAttribPointer(loc, size, type, false, stride, pointer_buffer_offset); } public static void glEnableClientState(int cap) { if (!ShaderManager.getInstance().isActive()) return; final int location = clientStateToAttributeLocation(cap); if (location >= 0) GL20.glEnableVertexAttribArray(location); + final int flag = clientStateToVertexFlag(cap); + if (flag != 0) ShaderManager.getInstance().enableClientVertexFlag(flag); } public static void glDisableClientState(int cap) { if (!ShaderManager.getInstance().isActive()) return; final int location = clientStateToAttributeLocation(cap); if (location >= 0) GL20.glDisableVertexAttribArray(location); + final int flag = clientStateToVertexFlag(cap); + if (flag != 0) ShaderManager.getInstance().disableClientVertexFlag(flag); } private static int clientStateToAttributeLocation(int cap) { @@ -2092,11 +2142,155 @@ public class GLStateManager { case GL11.GL_VERTEX_ARRAY -> Usage.POSITION.getAttributeLocation(); case GL11.GL_COLOR_ARRAY -> Usage.COLOR.getAttributeLocation(); case GL11.GL_NORMAL_ARRAY -> Usage.NORMAL.getAttributeLocation(); - case GL11.GL_TEXTURE_COORD_ARRAY -> Usage.PRIMARY_UV.getAttributeLocation(); + case GL11.GL_TEXTURE_COORD_ARRAY -> texCoordAttributeLocation(); default -> -1; }; } + private static int clientStateToVertexFlag(int cap) { + return switch (cap) { + case GL11.GL_COLOR_ARRAY -> VertexFlags.COLOR_BIT; + case GL11.GL_NORMAL_ARRAY -> VertexFlags.NORMAL_BIT; + case GL11.GL_TEXTURE_COORD_ARRAY -> switch (clientActiveTextureUnit) { + case 0 -> VertexFlags.TEXTURE_BIT; + case 1 -> VertexFlags.BRIGHTNESS_BIT; + default -> 0; + }; + default -> 0; // GL_VERTEX_ARRAY — position is implicit + }; + } + + private static final int CLIENT_ATTRIB_STACK_DEPTH = 16; + private static final int[] clientAttribSavedTextureUnit = new int[CLIENT_ATTRIB_STACK_DEPTH]; + private static final int[] clientAttribSavedVertexFlags = new int[CLIENT_ATTRIB_STACK_DEPTH]; + private static int clientAttribStackPointer = 0; + + public static void glPushClientAttrib(int mask) { + if (clientAttribStackPointer < CLIENT_ATTRIB_STACK_DEPTH) { + clientAttribSavedTextureUnit[clientAttribStackPointer] = clientActiveTextureUnit; + clientAttribSavedVertexFlags[clientAttribStackPointer] = ShaderManager.getInstance().getCurrentVertexFlags(); + clientAttribStackPointer++; + } + } + + public static void glPopClientAttrib() { + if (clientAttribStackPointer > 0) { + clientAttribStackPointer--; + clientActiveTextureUnit = clientAttribSavedTextureUnit[clientAttribStackPointer]; + ShaderManager.getInstance().setCurrentVertexFlags(clientAttribSavedVertexFlags[clientAttribStackPointer]); + } + } + + public static void glInterleavedArrays(int format, int stride, long pointer) { + if (!ShaderManager.getInstance().isActive()) { + return; + } + + // Mesa _mesa_get_interleaved_layout decomposition + final int f = 4; // sizeof(float) + final int c = f * ((4 + (f - 1)) / f); // 4 ubytes padded to float alignment = 4 + + boolean tflag = false, cflag = false, nflag = false; + int tcomps = 0, ccomps = 0, vcomps = 0; + int ctype = 0; + final int toffset = 0; + int coffset = 0; + int noffset = 0; + int voffset = 0; + final int defstride; + + switch (format) { + case GL11.GL_V2F: + vcomps = 2; defstride = 2 * f; break; + case GL11.GL_V3F: + vcomps = 3; defstride = 3 * f; break; + case GL11.GL_C4UB_V2F: + cflag = true; ccomps = 4; vcomps = 2; + ctype = GL11.GL_UNSIGNED_BYTE; voffset = c; defstride = c + 2 * f; break; + case GL11.GL_C4UB_V3F: + cflag = true; ccomps = 4; vcomps = 3; + ctype = GL11.GL_UNSIGNED_BYTE; voffset = c; defstride = c + 3 * f; break; + case GL11.GL_C3F_V3F: + cflag = true; ccomps = 3; vcomps = 3; + ctype = GL11.GL_FLOAT; voffset = 3 * f; defstride = 6 * f; break; + case GL11.GL_N3F_V3F: + nflag = true; vcomps = 3; + voffset = 3 * f; defstride = 6 * f; break; + case GL11.GL_C4F_N3F_V3F: + cflag = true; nflag = true; ccomps = 4; vcomps = 3; + ctype = GL11.GL_FLOAT; noffset = 4 * f; voffset = 7 * f; defstride = 10 * f; break; + case GL11.GL_T2F_V3F: + tflag = true; tcomps = 2; vcomps = 3; + voffset = 2 * f; defstride = 5 * f; break; + case GL11.GL_T4F_V4F: + tflag = true; tcomps = 4; vcomps = 4; + voffset = 4 * f; defstride = 8 * f; break; + case GL11.GL_T2F_C4UB_V3F: + tflag = true; cflag = true; tcomps = 2; ccomps = 4; vcomps = 3; + ctype = GL11.GL_UNSIGNED_BYTE; coffset = 2 * f; voffset = c + 2 * f; defstride = c + 5 * f; break; + case GL11.GL_T2F_C3F_V3F: + tflag = true; cflag = true; tcomps = 2; ccomps = 3; vcomps = 3; + ctype = GL11.GL_FLOAT; coffset = 2 * f; voffset = 5 * f; defstride = 8 * f; break; + case GL11.GL_T2F_N3F_V3F: + tflag = true; nflag = true; tcomps = 2; vcomps = 3; + noffset = 2 * f; voffset = 5 * f; defstride = 8 * f; break; + case GL11.GL_T2F_C4F_N3F_V3F: + tflag = true; cflag = true; nflag = true; tcomps = 2; ccomps = 4; vcomps = 3; + ctype = GL11.GL_FLOAT; coffset = 2 * f; noffset = 6 * f; voffset = 9 * f; defstride = 12 * f; break; + case GL11.GL_T4F_C4F_N3F_V4F: + tflag = true; cflag = true; nflag = true; tcomps = 4; ccomps = 4; vcomps = 4; + ctype = GL11.GL_FLOAT; coffset = 4 * f; noffset = 8 * f; voffset = 11 * f; defstride = 15 * f; break; + default: + return; // Invalid format + } + + if (stride == 0) stride = defstride; + + if (tflag) { + glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer(tcomps, GL11.GL_FLOAT, stride, pointer + toffset); + } else { + glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY); + } + + if (cflag) { + glEnableClientState(GL11.GL_COLOR_ARRAY); + glColorPointer(ccomps, ctype, stride, pointer + coffset); + } else { + glDisableClientState(GL11.GL_COLOR_ARRAY); + } + + if (nflag) { + glEnableClientState(GL11.GL_NORMAL_ARRAY); + glNormalPointer(GL11.GL_FLOAT, stride, pointer + noffset); + } else { + glDisableClientState(GL11.GL_NORMAL_ARRAY); + } + + glEnableClientState(GL11.GL_VERTEX_ARRAY); + glVertexPointer(vcomps, GL11.GL_FLOAT, stride, pointer + voffset); + } + + public static void glInterleavedArrays(int format, int stride, ByteBuffer pointer) { + glInterleavedArrays(format, stride, MemoryUtilities.memAddress0(pointer)); + } + + public static void glInterleavedArrays(int format, int stride, FloatBuffer pointer) { + glInterleavedArrays(format, stride, MemoryUtilities.memAddress0(MemoryUtilities.memByteBuffer(pointer))); + } + + public static void glInterleavedArrays(int format, int stride, DoubleBuffer pointer) { + glInterleavedArrays(format, stride, MemoryUtilities.memAddress0(MemoryUtilities.memByteBuffer(pointer))); + } + + public static void glInterleavedArrays(int format, int stride, IntBuffer pointer) { + glInterleavedArrays(format, stride, MemoryUtilities.memAddress0(MemoryUtilities.memByteBuffer(pointer))); + } + + public static void glInterleavedArrays(int format, int stride, ShortBuffer pointer) { + glInterleavedArrays(format, stride, MemoryUtilities.memAddress0(MemoryUtilities.memByteBuffer(pointer))); + } + public static void glLogicOp(int opcode) { final RecordMode mode = DisplayListManager.getRecordMode(); if (mode != RecordMode.NONE) { @@ -2797,6 +2991,8 @@ public class GLStateManager { private static final Vector3f rotation = new Vector3f(); public static void glRotatef(float angle, float x, float y, float z) { + final float lenSq = x * x + y * y + z * z; + if (lenSq == 0.0f) return; final RecordMode mode = DisplayListManager.getRecordMode(); if (mode != RecordMode.NONE) { rotation.set(x, y, z).normalize(); @@ -2811,6 +3007,8 @@ public class GLStateManager { } public static void glRotated(double angle, double x, double y, double z) { + final double lenSq = x * x + y * y + z * z; + if (lenSq == 0.0) return; final RecordMode mode = DisplayListManager.getRecordMode(); if (mode != RecordMode.NONE) { rotation.set((float) x, (float) y, (float) z).normalize(); @@ -4070,6 +4268,8 @@ public class GLStateManager { if (target == GL15.GL_ARRAY_BUFFER) { // if (boundVBO == buffer) return; TODO figure out why this breaks switching async occlusion mode boundVBO = buffer; + } else if (target == GL15.GL_ELEMENT_ARRAY_BUFFER) { + boundEBO = buffer; } GL15.glBindBuffer(target, buffer); } diff --git a/src/main/java/com/gtnewhorizons/angelica/glsm/QuadConverter.java b/src/main/java/com/gtnewhorizons/angelica/glsm/QuadConverter.java index 799e818c8..7d36ce54f 100644 --- a/src/main/java/com/gtnewhorizons/angelica/glsm/QuadConverter.java +++ b/src/main/java/com/gtnewhorizons/angelica/glsm/QuadConverter.java @@ -4,11 +4,17 @@ import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL15; import java.nio.ByteBuffer; +import java.nio.IntBuffer; +import java.nio.ShortBuffer; import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memAddress0; import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memAlloc; import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memFree; +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memGetByte; +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memGetInt; +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memGetShort; import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memPutInt; +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memPutShort; /** * Shared quad-to-triangle EBO for core profile GL_QUADS emulation. @@ -24,6 +30,10 @@ public final class QuadConverter { private static int eboId; private static int maxQuads; + /** Scratch EBO for converted quad-element draws (drawQuadElementsAsTriangles). */ + private static int scratchEboId; + private static int scratchEboCapacity; // in bytes + private QuadConverter() {} private static void ensureCapacity(int quadCount) { @@ -63,11 +73,6 @@ public final class QuadConverter { maxQuads = newMaxQuads; } - /** Get the EBO id. Populated after the first {@link #drawQuadsAsTriangles} call. */ - public static int getEboId() { - return eboId; - } - /** * Convert a GL_QUADS glDrawArrays call to indexed GL_TRIANGLES. * Binds the shared EBO to the current VAO and issues glDrawElements. @@ -86,6 +91,194 @@ public final class QuadConverter { GL11.glDrawElements(GL11.GL_TRIANGLES, quadCount * 6, INDEX_TYPE, indexOffset); } + /** + * Upload converted triangle indices to the scratch EBO and draw. + * Saves/restores the caller's EBO binding so subsequent indexed draws + * (or glGetBufferSubData reads) still see the original EBO. + * + * @param dst off-heap buffer with triangle index data (freed by this method) + * @param triIndexCount number of triangle indices + * @param indexType GL_UNSIGNED_INT or GL_UNSIGNED_SHORT + * @param bytesPerIndex bytes per index element + */ + private static void uploadAndDraw(ByteBuffer dst, int triIndexCount, int indexType, int bytesPerIndex) { + final int needed = triIndexCount * bytesPerIndex; + final int prevEbo = GLStateManager.getBoundEBO(); + + if (scratchEboId == 0) { + scratchEboId = GL15.glGenBuffers(); + } + + GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, scratchEboId); + + if (needed > scratchEboCapacity) { + // Power-of-2 growth — allocate full capacity, upload actual data + int newCap = Math.max(4096, scratchEboCapacity); + while (newCap < needed) newCap *= 2; + GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, newCap, GL15.GL_STREAM_DRAW); + scratchEboCapacity = newCap; + } + GL15.glBufferSubData(GL15.GL_ELEMENT_ARRAY_BUFFER, 0, dst); + + GL11.glDrawElements(GL11.GL_TRIANGLES, triIndexCount, indexType, 0L); + + GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, prevEbo); + memFree(dst); + } + + /** + * Convert an indexed GL_QUADS draw to GL_TRIANGLES by reading the source EBO, + * building a triangle index buffer, and issuing the converted draw. + * + * @param indexCount number of quad indices (must be multiple of 4) + * @param type GL_UNSIGNED_INT, GL_UNSIGNED_SHORT, or GL_UNSIGNED_BYTE + * @param offset byte offset into the currently bound EBO + */ + public static void drawQuadElementsAsTriangles(int indexCount, int type, long offset) { + if (indexCount == 0) return; + assert indexCount % 4 == 0 : "QuadConverter: indexCount must be multiple of 4"; + final int quadCount = indexCount / 4; + final int triIndexCount = quadCount * 6; + + final int bytesPerIndex; + switch (type) { + case GL11.GL_UNSIGNED_INT: bytesPerIndex = 4; break; + case GL11.GL_UNSIGNED_SHORT: bytesPerIndex = 2; break; + case GL11.GL_UNSIGNED_BYTE: bytesPerIndex = 1; break; + default: return; + } + + // Read source indices from caller's EBO + final ByteBuffer src = memAlloc(indexCount * bytesPerIndex); + GL15.glGetBufferSubData(GL15.GL_ELEMENT_ARRAY_BUFFER, offset, src); + final long srcAddr = memAddress0(src); + + // Allocate output as GL_UNSIGNED_INT + final ByteBuffer dst = memAlloc(triIndexCount * 4); + final long dstAddr = memAddress0(dst); + + for (int i = 0; i < quadCount; i++) { + final int a, b, c, d; + switch (type) { + case GL11.GL_UNSIGNED_INT -> { + final long p = srcAddr + (long) i * 16; + a = memGetInt(p); b = memGetInt(p + 4); c = memGetInt(p + 8); d = memGetInt(p + 12); + } + case GL11.GL_UNSIGNED_SHORT -> { + final long p = srcAddr + (long) i * 8; + a = memGetShort(p) & 0xFFFF; b = memGetShort(p + 2) & 0xFFFF; + c = memGetShort(p + 4) & 0xFFFF; d = memGetShort(p + 6) & 0xFFFF; + } + default /* GL_UNSIGNED_BYTE */ -> { + final long p = srcAddr + (long) i * 4; + a = memGetByte(p) & 0xFF; b = memGetByte(p + 1) & 0xFF; + c = memGetByte(p + 2) & 0xFF; d = memGetByte(p + 3) & 0xFF; + } + } + // GL_LAST_VERTEX_CONVENTION: (a,b,d), (b,c,d) + final long o = (long) i * 24; + memPutInt(dstAddr + o, a); + memPutInt(dstAddr + o + 4, b); + memPutInt(dstAddr + o + 8, d); + memPutInt(dstAddr + o + 12, b); + memPutInt(dstAddr + o + 16, c); + memPutInt(dstAddr + o + 20, d); + } + + memFree(src); + uploadAndDraw(dst, triIndexCount, GL11.GL_UNSIGNED_INT, 4); + } + + /** + * Convert a client-side IntBuffer of quad indices to triangles. + */ + public static void drawQuadElementsAsTriangles(IntBuffer indices) { + final int indexCount = indices.remaining(); + if (indexCount == 0) return; + assert indexCount % 4 == 0; + final int quadCount = indexCount / 4; + final int triIndexCount = quadCount * 6; + + final ByteBuffer dst = memAlloc(triIndexCount * 4); + final long dstAddr = memAddress0(dst); + final int basePos = indices.position(); + + for (int i = 0; i < quadCount; i++) { + final int base = basePos + i * 4; + final int a = indices.get(base), b = indices.get(base + 1), c = indices.get(base + 2), d = indices.get(base + 3); + final long o = (long) i * 24; + memPutInt(dstAddr + o, a); memPutInt(dstAddr + o + 4, b); memPutInt(dstAddr + o + 8, d); + memPutInt(dstAddr + o + 12, b); memPutInt(dstAddr + o + 16, c); memPutInt(dstAddr + o + 20, d); + } + + uploadAndDraw(dst, triIndexCount, GL11.GL_UNSIGNED_INT, 4); + } + + /** + * Convert a client-side ShortBuffer of quad indices to triangles. + */ + public static void drawQuadElementsAsTriangles(ShortBuffer indices) { + final int indexCount = indices.remaining(); + if (indexCount == 0) return; + assert indexCount % 4 == 0; + final int quadCount = indexCount / 4; + final int triIndexCount = quadCount * 6; + + final ByteBuffer dst = memAlloc(triIndexCount * 2); + final long dstAddr = memAddress0(dst); + final int basePos = indices.position(); + + for (int i = 0; i < quadCount; i++) { + final int base = basePos + i * 4; + final short a = indices.get(base), b = indices.get(base + 1), c = indices.get(base + 2), d = indices.get(base + 3); + final long o = (long) i * 12; + memPutShort(dstAddr + o, a); memPutShort(dstAddr + o + 2, b); memPutShort(dstAddr + o + 4, d); + memPutShort(dstAddr + o + 6, b); memPutShort(dstAddr + o + 8, c); memPutShort(dstAddr + o + 10, d); + } + + uploadAndDraw(dst, triIndexCount, GL11.GL_UNSIGNED_SHORT, 2); + } + + /** + * Convert a client-side ByteBuffer of quad indices (type-agnostic) to triangles. + */ + public static void drawQuadElementsAsTriangles(int count, int type, ByteBuffer indices) { + if (count == 0) return; + assert count % 4 == 0; + final int quadCount = count / 4; + final int triIndexCount = quadCount * 6; + final long srcAddr = memAddress0(indices) + indices.position(); + + final ByteBuffer dst = memAlloc(triIndexCount * 4); + final long dstAddr = memAddress0(dst); + + for (int i = 0; i < quadCount; i++) { + final int a, b, c, d; + switch (type) { + case GL11.GL_UNSIGNED_INT -> { + final long p = srcAddr + (long) i * 16; + a = memGetInt(p); b = memGetInt(p + 4); c = memGetInt(p + 8); d = memGetInt(p + 12); + } + case GL11.GL_UNSIGNED_SHORT -> { + final long p = srcAddr + (long) i * 8; + a = memGetShort(p) & 0xFFFF; b = memGetShort(p + 2) & 0xFFFF; + c = memGetShort(p + 4) & 0xFFFF; d = memGetShort(p + 6) & 0xFFFF; + } + case GL11.GL_UNSIGNED_BYTE -> { + final long p = srcAddr + (long) i * 4; + a = memGetByte(p) & 0xFF; b = memGetByte(p + 1) & 0xFF; + c = memGetByte(p + 2) & 0xFF; d = memGetByte(p + 3) & 0xFF; + } + default -> { memFree(dst); return; } + } + final long o = (long) i * 24; + memPutInt(dstAddr + o, a); memPutInt(dstAddr + o + 4, b); memPutInt(dstAddr + o + 8, d); + memPutInt(dstAddr + o + 12, b); memPutInt(dstAddr + o + 16, c); memPutInt(dstAddr + o + 20, d); + } + + uploadAndDraw(dst, triIndexCount, GL11.GL_UNSIGNED_INT, 4); + } + /** * Clean up the shared EBO. */ @@ -95,5 +288,10 @@ public final class QuadConverter { eboId = 0; maxQuads = 0; } + if (scratchEboId != 0) { + GL15.glDeleteBuffers(scratchEboId); + scratchEboId = 0; + scratchEboCapacity = 0; + } } } diff --git a/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/ShaderManager.java b/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/ShaderManager.java index 8194de4f3..45acf768e 100644 --- a/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/ShaderManager.java +++ b/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/ShaderManager.java @@ -94,6 +94,8 @@ public class ShaderManager { currentFragmentKeyPacked = Long.MIN_VALUE; } + + public void preDraw(boolean hasColor, boolean hasNormal, boolean hasTexCoord, boolean hasLightmap) { GLStateManager.flushDeferredVertexAttribs(); BlendModeStorage.flushDeferredBlend(); @@ -166,6 +168,11 @@ public class ShaderManager { public static void bumpNormalGeneration() { normalGeneration++; } public static void bumpTexCoordGeneration() { texCoordGeneration++; } + public void enableClientVertexFlag(int flag) { currentVertexFlags |= flag; } + public void disableClientVertexFlag(int flag) { currentVertexFlags &= ~flag; } + public int getCurrentVertexFlags() { return currentVertexFlags; } + public void setCurrentVertexFlags(int flags) { currentVertexFlags = flags; } + public void onBindVertexArray(int vaoId) { final int flags = vaoVertexFlags.get(vaoId); if (flags != -1) { diff --git a/src/main/java/com/gtnewhorizons/angelica/glsm/recording/CommandBufferExecutor.java b/src/main/java/com/gtnewhorizons/angelica/glsm/recording/CommandBufferExecutor.java index 71b5384a0..3a59ea457 100644 --- a/src/main/java/com/gtnewhorizons/angelica/glsm/recording/CommandBufferExecutor.java +++ b/src/main/java/com/gtnewhorizons/angelica/glsm/recording/CommandBufferExecutor.java @@ -519,7 +519,7 @@ public final class CommandBufferExecutor { ptr += 4; } case GLCommand.BIND_VAO -> { - UniversalVAO.bindVertexArray(memGetInt(ptr)); + GLStateManager.glBindVertexArray(memGetInt(ptr)); ptr += 4; } diff --git a/src/main/java/com/gtnewhorizons/angelica/loading/shared/transformers/AngelicaRedirector.java b/src/main/java/com/gtnewhorizons/angelica/loading/shared/transformers/AngelicaRedirector.java index 096d29210..295bd8b67 100644 --- a/src/main/java/com/gtnewhorizons/angelica/loading/shared/transformers/AngelicaRedirector.java +++ b/src/main/java/com/gtnewhorizons/angelica/loading/shared/transformers/AngelicaRedirector.java @@ -232,6 +232,9 @@ public final class AngelicaRedirector { .add("glTexCoordPointer") .add("glEnableClientState") .add("glDisableClientState") + .add("glInterleavedArrays") + .add("glPushClientAttrib") + .add("glPopClientAttrib") .add("glViewport") ); methodRedirects.put(GL12, RedirectMap.newMap() @@ -242,6 +245,7 @@ public final class AngelicaRedirector { methodRedirects.put(GL13, RedirectMap.newMap() .add("glActiveTexture") .add("glSampleCoverage") + .add("glClientActiveTexture") .add("glMultiTexCoord2f") .add("glMultiTexCoord2s") );