From 975c24c8a9650dcfa9bd817d4c803c108ff1123a Mon Sep 17 00:00:00 2001 From: tom lee Date: Sat, 1 Jan 2022 15:25:37 +0800 Subject: [PATCH] AMD: Fixed AMD render issue due to vertex data alignments --- .../opengl/DefaultLodVertexFormats.java | 16 ++++++++-------- .../core/objects/opengl/LodBufferBuilder.java | 8 ++++---- .../objects/opengl/LodVertexFormatElement.java | 8 +++++++- .../lod/core/render/LodRenderProgram.java | 18 ++++++++++-------- .../core/render/objects/VertexAttribute.java | 4 ++-- .../objects/VertexAttributePostGL43.java | 1 + .../render/objects/VertexAttributePreGL43.java | 5 +++-- .../minecraft/IMinecraftRenderWrapper.java | 9 +++++---- src/main/resources/shaders/standard.vert | 5 +++-- 9 files changed, 43 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/seibel/lod/core/objects/opengl/DefaultLodVertexFormats.java b/src/main/java/com/seibel/lod/core/objects/opengl/DefaultLodVertexFormats.java index 6a0e9cb83..5cb028e95 100644 --- a/src/main/java/com/seibel/lod/core/objects/opengl/DefaultLodVertexFormats.java +++ b/src/main/java/com/seibel/lod/core/objects/opengl/DefaultLodVertexFormats.java @@ -30,14 +30,14 @@ import com.google.common.collect.ImmutableList; */ public class DefaultLodVertexFormats { - public static final LodVertexFormatElement ELEMENT_POSITION = new LodVertexFormatElement(0, LodVertexFormatElement.DataType.FLOAT, 3); - public static final LodVertexFormatElement ELEMENT_COLOR = new LodVertexFormatElement(0, LodVertexFormatElement.DataType.UBYTE, 4); - public static final LodVertexFormatElement ELEMENT_UV = new LodVertexFormatElement(0, LodVertexFormatElement.DataType.FLOAT, 2); - public static final LodVertexFormatElement ELEMENT_LIGHT_MAP_UV = new LodVertexFormatElement(1, LodVertexFormatElement.DataType.SHORT, 2); - public static final LodVertexFormatElement ELEMENT_NORMAL = new LodVertexFormatElement(0, LodVertexFormatElement.DataType.BYTE, 3); - public static final LodVertexFormatElement ELEMENT_PADDING = new LodVertexFormatElement(0, LodVertexFormatElement.DataType.BYTE, 1); + public static final LodVertexFormatElement ELEMENT_POSITION = new LodVertexFormatElement(0, LodVertexFormatElement.DataType.FLOAT, 3, false); + public static final LodVertexFormatElement ELEMENT_COLOR = new LodVertexFormatElement(0, LodVertexFormatElement.DataType.UBYTE, 4, false); + public static final LodVertexFormatElement ELEMENT_UV = new LodVertexFormatElement(0, LodVertexFormatElement.DataType.FLOAT, 2, false); + public static final LodVertexFormatElement ELEMENT_LIGHT_MAP_UV = new LodVertexFormatElement(1, LodVertexFormatElement.DataType.SHORT, 2, false); + public static final LodVertexFormatElement ELEMENT_NORMAL = new LodVertexFormatElement(0, LodVertexFormatElement.DataType.BYTE, 3, false); + public static final LodVertexFormatElement ELEMENT_PADDING = new LodVertexFormatElement(0, LodVertexFormatElement.DataType.BYTE, 1, true); - public static final LodVertexFormatElement ELEMENT_BLOCK_LIGHT = new LodVertexFormatElement(0, LodVertexFormatElement.DataType.UBYTE, 1); + public static final LodVertexFormatElement ELEMENT_BLOCK_LIGHT = new LodVertexFormatElement(0, LodVertexFormatElement.DataType.UBYTE, 1, false); public static final LodVertexFormat POSITION = new LodVertexFormat(ImmutableList.builder().add(ELEMENT_POSITION).build()); @@ -47,5 +47,5 @@ public class DefaultLodVertexFormats public static final LodVertexFormat POSITION_COLOR_TEX = new LodVertexFormat(ImmutableList.builder().add(ELEMENT_POSITION).add(ELEMENT_COLOR).add(ELEMENT_UV).build()); public static final LodVertexFormat POSITION_COLOR_TEX_LIGHTMAP = new LodVertexFormat(ImmutableList.builder().add(ELEMENT_POSITION).add(ELEMENT_COLOR).add(ELEMENT_UV).add(ELEMENT_LIGHT_MAP_UV).build()); - public static final LodVertexFormat POSITION_COLOR_BLOCK_LIGHT_SKY_LIGHT = new LodVertexFormat(ImmutableList.builder().add(ELEMENT_POSITION).add(ELEMENT_COLOR).add(ELEMENT_BLOCK_LIGHT).add(ELEMENT_BLOCK_LIGHT).build()); + public static final LodVertexFormat POSITION_COLOR_BLOCK_LIGHT_SKY_LIGHT = new LodVertexFormat(ImmutableList.builder().add(ELEMENT_POSITION).add(ELEMENT_COLOR).add(ELEMENT_BLOCK_LIGHT).add(ELEMENT_BLOCK_LIGHT).add(ELEMENT_PADDING).add(ELEMENT_PADDING).build()); } diff --git a/src/main/java/com/seibel/lod/core/objects/opengl/LodBufferBuilder.java b/src/main/java/com/seibel/lod/core/objects/opengl/LodBufferBuilder.java index d7ff4c9ef..db77fd4f3 100644 --- a/src/main/java/com/seibel/lod/core/objects/opengl/LodBufferBuilder.java +++ b/src/main/java/com/seibel/lod/core/objects/opengl/LodBufferBuilder.java @@ -323,10 +323,10 @@ public class LodBufferBuilder this.elementIndex = (this.elementIndex + 1) % immutablelist.size(); this.nextElementByte += this.currentElement.getByteSize(); this.currentElement = immutablelist.get(this.elementIndex); -// if (LodVertexFormatelement.getUsage() == LodVertexFormatElement.Usage.PADDING) -// { -// this.nextElement(); -// } + if (currentElement.getIsPadding()) + { + this.nextElement(); + } // if (this.defaultColorSet && this.currentElement.getUsage() == LodVertexFormatElement.Usage.COLOR) // { diff --git a/src/main/java/com/seibel/lod/core/objects/opengl/LodVertexFormatElement.java b/src/main/java/com/seibel/lod/core/objects/opengl/LodVertexFormatElement.java index a85ca82cf..56c79b36d 100644 --- a/src/main/java/com/seibel/lod/core/objects/opengl/LodVertexFormatElement.java +++ b/src/main/java/com/seibel/lod/core/objects/opengl/LodVertexFormatElement.java @@ -39,13 +39,19 @@ public class LodVertexFormatElement private final int index; private final int count; private final int byteSize; + private final boolean isPadding; - public LodVertexFormatElement(int newIndex, LodVertexFormatElement.DataType newType, int newCount) + public LodVertexFormatElement(int newIndex, LodVertexFormatElement.DataType newType, int newCount, boolean isPadding) { this.dataType = newType; this.index = newIndex; this.count = newCount; this.byteSize = newType.getSize() * this.count; + this.isPadding = isPadding; + } + + public final boolean getIsPadding() { + return isPadding; } public final LodVertexFormatElement.DataType getType() diff --git a/src/main/java/com/seibel/lod/core/render/LodRenderProgram.java b/src/main/java/com/seibel/lod/core/render/LodRenderProgram.java index 65b1eac06..ff9bab542 100644 --- a/src/main/java/com/seibel/lod/core/render/LodRenderProgram.java +++ b/src/main/java/com/seibel/lod/core/render/LodRenderProgram.java @@ -40,8 +40,7 @@ public class LodRenderProgram extends ShaderProgram { // Attributes public final int posAttrib; public final int colAttrib; - public final int blockSkyLightAttrib; - public final int blockLightAttrib; + public final int lightAttrib; //Sky light then block light // Uniforms public final int mvmUniform; public final int projUniform; @@ -64,8 +63,7 @@ public class LodRenderProgram extends ShaderProgram { posAttrib = getAttributeLocation("vPosition"); colAttrib = getAttributeLocation("color"); - blockSkyLightAttrib = getAttributeLocation("blockSkyLight"); - blockLightAttrib = getAttributeLocation("blockLight"); + lightAttrib = getAttributeLocation("light"); mvmUniform = getUniformLocation("modelViewMatrix"); projUniform = getUniformLocation("projectionMatrix"); @@ -92,11 +90,15 @@ public class LodRenderProgram extends ShaderProgram { else vao = new VertexAttributePreGL43(); // also binds VertexAttribute //vao.bind(); - vao.setVertexAttribute(0, posAttrib, VertexAttribute.VertexPointer.addVec3Pointer(false)); - vao.setVertexAttribute(0, colAttrib, VertexAttribute.VertexPointer.addUnsignedBytesPointer(4, true)); - vao.setVertexAttribute(0, blockSkyLightAttrib, VertexAttribute.VertexPointer.addUnsignedBytePointer(false)); - vao.setVertexAttribute(0, blockLightAttrib, VertexAttribute.VertexPointer.addUnsignedBytePointer(false)); + vao.setVertexAttribute(0, posAttrib, VertexAttribute.VertexPointer.addVec3Pointer(false)); // 4+4+4 + vao.setVertexAttribute(0, colAttrib, VertexAttribute.VertexPointer.addUnsignedBytesPointer(4, true)); // +4 + vao.setVertexAttribute(0, lightAttrib, VertexAttribute.VertexPointer.addUnsignedBytesPointer(2, false)); // +4 due to how it aligns + try { vao.completeAndCheck(vertexByteCount); + } catch (RuntimeException e) { + System.out.println(LodUtil.LOD_VERTEX_FORMAT); + throw e; + } } // Override ShaderProgram.bind() diff --git a/src/main/java/com/seibel/lod/core/render/objects/VertexAttribute.java b/src/main/java/com/seibel/lod/core/render/objects/VertexAttribute.java index 3c96150d6..87fe3d0fc 100644 --- a/src/main/java/com/seibel/lod/core/render/objects/VertexAttribute.java +++ b/src/main/java/com/seibel/lod/core/render/objects/VertexAttribute.java @@ -47,10 +47,10 @@ public abstract class VertexAttribute { return new VertexPointer(1, GL32.GL_FLOAT, normalized, 16); } public static VertexPointer addUnsignedBytePointer(boolean normalized) { - return new VertexPointer(1, GL32.GL_UNSIGNED_BYTE, normalized, 1); + return new VertexPointer(1, GL32.GL_UNSIGNED_BYTE, normalized, 4); // Always aligned to 4 bytes } public static VertexPointer addUnsignedBytesPointer(int elementCount, boolean normalized) { - return new VertexPointer(elementCount, GL32.GL_UNSIGNED_BYTE, normalized, elementCount); + return new VertexPointer(elementCount, GL32.GL_UNSIGNED_BYTE, normalized, (-Math.floorDiv(-elementCount, 4))*4); // aligned to 4 bytes } public static VertexPointer addIntPointer(boolean normalized) { return new VertexPointer(1, GL32.GL_INT, normalized, 4); diff --git a/src/main/java/com/seibel/lod/core/render/objects/VertexAttributePostGL43.java b/src/main/java/com/seibel/lod/core/render/objects/VertexAttributePostGL43.java index f5948e5ee..93cc94fe1 100644 --- a/src/main/java/com/seibel/lod/core/render/objects/VertexAttributePostGL43.java +++ b/src/main/java/com/seibel/lod/core/render/objects/VertexAttributePostGL43.java @@ -65,6 +65,7 @@ public final class VertexAttributePostGL43 extends VertexAttribute { if (strideSize != expectedStrideSize) { ClientApi.LOGGER.error("Vertex Attribute calculated stride size " + strideSize + " does not match the provided expected stride size " + expectedStrideSize + "!"); + throw new IllegalArgumentException("Vertex Attribute Incorrect Format"); } ClientApi.LOGGER.info("Vertex Attribute (GL43+) completed. It contains "+numberOfBindingPoints +" binding points and a stride size of "+strideSize); diff --git a/src/main/java/com/seibel/lod/core/render/objects/VertexAttributePreGL43.java b/src/main/java/com/seibel/lod/core/render/objects/VertexAttributePreGL43.java index 14e2d049b..65a7e9c89 100644 --- a/src/main/java/com/seibel/lod/core/render/objects/VertexAttributePreGL43.java +++ b/src/main/java/com/seibel/lod/core/render/objects/VertexAttributePreGL43.java @@ -19,7 +19,6 @@ public final class VertexAttributePreGL43 extends VertexAttribute { VertexPointer[] pointers; int[] pointersOffset; - TreeMap> bindingPointsToIndexBuilder; ArrayList pointersBuilder; @@ -124,9 +123,11 @@ public final class VertexAttributePreGL43 extends VertexAttribute { pointersOffset[i] = currentOffset; currentOffset += pointer.byteSize; } - if (currentOffset != expectedStrideSize) + if (currentOffset != expectedStrideSize) { ClientApi.LOGGER.error("Vertex Attribute calculated stride size " + currentOffset + " does not match the provided expected stride size " + expectedStrideSize + "!"); + throw new IllegalArgumentException("Vertex Attribute Incorrect Format"); + } strideSize = currentOffset; ClientApi.LOGGER.info("Vertex Attribute (pre GL43) completed."); diff --git a/src/main/java/com/seibel/lod/core/wrapperInterfaces/minecraft/IMinecraftRenderWrapper.java b/src/main/java/com/seibel/lod/core/wrapperInterfaces/minecraft/IMinecraftRenderWrapper.java index 89eaa3438..886e9603a 100644 --- a/src/main/java/com/seibel/lod/core/wrapperInterfaces/minecraft/IMinecraftRenderWrapper.java +++ b/src/main/java/com/seibel/lod/core/wrapperInterfaces/minecraft/IMinecraftRenderWrapper.java @@ -53,8 +53,12 @@ public interface IMinecraftRenderWrapper Color getFogColor(float partialTicks); - default Color getUnderWaterFogColor(float partialTicks) {return getFogColor(partialTicks);} + public default Color getUnderWaterFogColor(float partialTicks) {return getFogColor(partialTicks);} + public default boolean isFogStateInUnderWater() { + return false; + } + Color getSkyColor(); double getFov(float partialTicks); @@ -134,7 +138,4 @@ public interface IMinecraftRenderWrapper return false; } - public default boolean isFogStateInUnderWater() { - return false; - } } diff --git a/src/main/resources/shaders/standard.vert b/src/main/resources/shaders/standard.vert index 7ab613a3e..44ec8c8d8 100644 --- a/src/main/resources/shaders/standard.vert +++ b/src/main/resources/shaders/standard.vert @@ -2,8 +2,7 @@ in vec3 vPosition; in vec4 color; -in float blockSkyLight; -in float blockLight; +in vec2 light; out vec4 vertexColor; out vec4 vertexWorldPos; @@ -25,6 +24,8 @@ uniform sampler2D lightMap; */ void main() { + float blockSkyLight = light[0]; + float blockLight = light[1]; // just skylight // good for sanity checks; but will cause OpenGL errors since we are binding unused data