diff --git a/common/src/lwjgl2/java/org/taumc/celeritas/lwjgl/lwjgl2/LWJGL2Service.java b/common/src/lwjgl2/java/org/taumc/celeritas/lwjgl/lwjgl2/LWJGL2Service.java index 4a8ff892e..b348e1f42 100644 --- a/common/src/lwjgl2/java/org/taumc/celeritas/lwjgl/lwjgl2/LWJGL2Service.java +++ b/common/src/lwjgl2/java/org/taumc/celeritas/lwjgl/lwjgl2/LWJGL2Service.java @@ -12,6 +12,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.lwjgl.opengl.APPLEVertexArrayObject; import org.lwjgl.opengl.ARBBufferStorage; +import org.lwjgl.opengl.ARBInstancedArrays; import org.lwjgl.opengl.ARBTimerQuery; import org.lwjgl.opengl.ARBVertexArrayObject; import org.lwjgl.opengl.ContextCapabilities; @@ -44,6 +45,7 @@ public record LWJGL2Service( TimerQueryMode timerQueryMode, DebugMode debugMode, VertexAttribIMode vertexAttribIMode, + VertexAttribDivisorMode vertexAttribDivisorMode, Long2ObjectOpenHashMap syncObjects) implements LWJGLService { private static final Logger LOGGER = LogManager.getLogger("Celeritas/LWJGL2Service"); private static final LWJGL2DebugSupport debugSupport = new LWJGL2DebugSupport(); @@ -130,6 +132,26 @@ public record LWJGL2Service( public abstract void vertexAttribIPointer(int index, int size, int type, int stride, long pointer); } + private enum VertexAttribDivisorMode { + CORE { + @Override public void vertexAttribDivisor(int index, int divisor) { + GL33.glVertexAttribDivisor(index, divisor); + } + }, + ARB { + @Override public void vertexAttribDivisor(int index, int divisor) { + ARBInstancedArrays.glVertexAttribDivisorARB(index, divisor); + } + }, + NONE { + @Override public void vertexAttribDivisor(int index, int divisor) { + throw new UnsupportedOperationException("glVertexAttribDivisor not supported"); + } + }; + + public abstract void vertexAttribDivisor(int index, int divisor); + } + public static LWJGL2Service create() { ContextCapabilities caps = GLContext.getCapabilities(); @@ -170,7 +192,17 @@ public record LWJGL2Service( vertexAttribIMode = VertexAttribIMode.NONE; } - return new LWJGL2Service(vaoMode, timerQueryMode, debugMode, vertexAttribIMode, new Long2ObjectOpenHashMap<>()); + VertexAttribDivisorMode vertexAttribDivisorMode; + if (caps.OpenGL33) { + vertexAttribDivisorMode = VertexAttribDivisorMode.CORE; + } else if (caps.GL_ARB_instanced_arrays) { + vertexAttribDivisorMode = VertexAttribDivisorMode.ARB; + } else { + vertexAttribDivisorMode = VertexAttribDivisorMode.NONE; + } + + return new LWJGL2Service(vaoMode, timerQueryMode, debugMode, vertexAttribIMode, vertexAttribDivisorMode, + new Long2ObjectOpenHashMap<>()); } // ===================== CAPABILITIES ===================== @@ -340,6 +372,11 @@ public record LWJGL2Service( GL20.glEnableVertexAttribArray(index); } + @Override + public void glVertexAttribDivisor(int index, int divisor) { + vertexAttribDivisorMode.vertexAttribDivisor(index, divisor); + } + // ===================== SHADER OPERATIONS ===================== @Override diff --git a/common/src/lwjgl3/java/org/taumc/celeritas/lwjgl/lwjgl3/LWJGL3Service.java b/common/src/lwjgl3/java/org/taumc/celeritas/lwjgl/lwjgl3/LWJGL3Service.java index 3a81eb42f..1c22ddfed 100644 --- a/common/src/lwjgl3/java/org/taumc/celeritas/lwjgl/lwjgl3/LWJGL3Service.java +++ b/common/src/lwjgl3/java/org/taumc/celeritas/lwjgl/lwjgl3/LWJGL3Service.java @@ -24,7 +24,8 @@ public record LWJGL3Service( VAOMode vaoMode, TimerQueryMode timerQueryMode, DebugMode debugMode, - VertexAttribIMode vertexAttribIMode) implements LWJGLService { + VertexAttribIMode vertexAttribIMode, + VertexAttribDivisorMode vertexAttribDivisorMode) implements LWJGLService { private static final Logger LOGGER = LogManager.getLogger("Celeritas/LWJGL3Service"); private static final LWJGL3DebugSupport debugSupport = new LWJGL3DebugSupport(); @@ -198,6 +199,7 @@ public record LWJGL3Service( private enum TimerQueryMode { CORE, ARB, NONE } private enum DebugMode { KHR, NONE } private enum VertexAttribIMode { CORE, EXT, NONE } + private enum VertexAttribDivisorMode { CORE, ARB, NONE } // Cached function addresses for APPLE VAO extensions @@ -248,7 +250,16 @@ public record LWJGL3Service( vertexAttribIMode = VertexAttribIMode.NONE; } - return new LWJGL3Service(vaoMode, timerQueryMode, debugMode, vertexAttribIMode); + VertexAttribDivisorMode vertexAttribDivisorMode; + if (caps.OpenGL33) { + vertexAttribDivisorMode = VertexAttribDivisorMode.CORE; + } else if (caps.GL_ARB_instanced_arrays) { + vertexAttribDivisorMode = VertexAttribDivisorMode.ARB; + } else { + vertexAttribDivisorMode = VertexAttribDivisorMode.NONE; + } + + return new LWJGL3Service(vaoMode, timerQueryMode, debugMode, vertexAttribIMode, vertexAttribDivisorMode); } @Override @@ -285,6 +296,15 @@ public record LWJGL3Service( GL20C.glEnableVertexAttribArray(index); } + @Override + public void glVertexAttribDivisor(int index, int divisor) { + switch (vertexAttribDivisorMode) { + case CORE -> GL33C.glVertexAttribDivisor(index, divisor); + case ARB -> ARBInstancedArrays.glVertexAttribDivisorARB(index, divisor); + case NONE -> throw new UnsupportedOperationException("glVertexAttribDivisor not supported"); + } + } + // ===================== SHADER OPERATIONS ===================== @Override diff --git a/common/src/lwjglCommon/java/org/taumc/celeritas/lwjgl/LWJGLService.java b/common/src/lwjglCommon/java/org/taumc/celeritas/lwjgl/LWJGLService.java index b4e58cf39..acbd64db0 100644 --- a/common/src/lwjglCommon/java/org/taumc/celeritas/lwjgl/LWJGLService.java +++ b/common/src/lwjglCommon/java/org/taumc/celeritas/lwjgl/LWJGLService.java @@ -42,6 +42,7 @@ public interface LWJGLService { void glVertexAttribPointer(int index, int size, int type, boolean normalized, int stride, long pointer); void glVertexAttribIPointer(int index, int size, int type, int stride, long pointer); void glEnableVertexAttribArray(int index); + void glVertexAttribDivisor(int index, int divisor); // ===================== SHADER OPERATIONS ===================== diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/gl/attribute/GlVertexAttributeBinding.java b/common/src/main/java/org/embeddedt/embeddium/impl/gl/attribute/GlVertexAttributeBinding.java index bfde9b8c9..8ed0938b1 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/gl/attribute/GlVertexAttributeBinding.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/gl/attribute/GlVertexAttributeBinding.java @@ -2,14 +2,28 @@ package org.embeddedt.embeddium.impl.gl.attribute; public class GlVertexAttributeBinding extends GlVertexAttribute { private final int index; + private final int divisor; public GlVertexAttributeBinding(int index, GlVertexAttribute attribute) { + this(index, attribute, 0); + } + + public GlVertexAttributeBinding(int index, GlVertexAttribute attribute, int divisor) { super(attribute.getFormat(), attribute.getSize(), attribute.getCount(), attribute.getName(), attribute.isNormalized(), attribute.getPointer(), attribute.getStride(), attribute.isIntType()); + if (divisor < 0) { + throw new IllegalArgumentException("Divisor must not be negative"); + } + this.index = index; + this.divisor = divisor; } public int getIndex() { return this.index; } + + public int getDivisor() { + return this.divisor; + } } diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/gl/shader/GlProgram.java b/common/src/main/java/org/embeddedt/embeddium/impl/gl/shader/GlProgram.java index a7bdeef22..fb9095316 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/gl/shader/GlProgram.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/gl/shader/GlProgram.java @@ -7,6 +7,7 @@ import org.taumc.celeritas.lwjgl.GL43; import static org.taumc.celeritas.lwjgl.LWJGLServiceProvider.LWJGL; import org.embeddedt.embeddium.impl.gl.GlObject; +import org.embeddedt.embeddium.impl.gl.attribute.GlVertexFormat; import org.embeddedt.embeddium.impl.gl.debug.GLDebug; import org.embeddedt.embeddium.impl.gl.shader.uniform.GlUniform; import org.embeddedt.embeddium.impl.gl.shader.uniform.GlUniformBlock; @@ -125,6 +126,16 @@ public class GlProgram extends GlObject implements ShaderBindingContext { return this; } + public Builder bindAttributes(GlVertexFormat format, int firstAttributeIndex) { + int index = firstAttributeIndex; + + for (var attribute : format.getAttributes()) { + this.bindAttribute(attribute.getName(), index++); + } + + return this; + } + public Builder bindFragmentData(String name, int index) { LWJGL.glBindFragDataLocation(this.program, index, name); diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/gl/tessellation/GlAbstractTessellation.java b/common/src/main/java/org/embeddedt/embeddium/impl/gl/tessellation/GlAbstractTessellation.java index 41bef44e3..b54704489 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/gl/tessellation/GlAbstractTessellation.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/gl/tessellation/GlAbstractTessellation.java @@ -29,6 +29,10 @@ public abstract class GlAbstractTessellation implements GlTessellation { attrib.getStride(), attrib.getPointer()); } LWJGL.glEnableVertexAttribArray(attrib.getIndex()); + + if (attrib.getDivisor() != 0) { + LWJGL.glVertexAttribDivisor(attrib.getIndex(), attrib.getDivisor()); + } } } } diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/gl/tessellation/TessellationBinding.java b/common/src/main/java/org/embeddedt/embeddium/impl/gl/tessellation/TessellationBinding.java index e2530b853..e410962e1 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/gl/tessellation/TessellationBinding.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/gl/tessellation/TessellationBinding.java @@ -1,6 +1,7 @@ package org.embeddedt.embeddium.impl.gl.tessellation; import org.embeddedt.embeddium.impl.gl.attribute.GlVertexAttributeBinding; +import org.embeddedt.embeddium.impl.gl.attribute.GlVertexFormat; import org.embeddedt.embeddium.impl.gl.buffer.GlBuffer; import org.embeddedt.embeddium.impl.gl.buffer.GlBufferTarget; @@ -14,6 +15,25 @@ public record TessellationBinding(GlBufferTarget target, return new TessellationBinding(GlBufferTarget.ARRAY_BUFFER, buffer, attributes); } + public static TessellationBinding forVertexBuffer(GlBuffer buffer, GlVertexFormat format) { + return forVertexBuffer(buffer, format, 0, 0); + } + + public static TessellationBinding forVertexBuffer(GlBuffer buffer, GlVertexFormat format, int firstAttributeIndex, int divisor) { + Objects.requireNonNull(format); + + var attributes = format.getAttributes(); + var bindings = new GlVertexAttributeBinding[attributes.size()]; + int index = 0; + + for (var attribute : attributes) { + bindings[index] = new GlVertexAttributeBinding(firstAttributeIndex + index, attribute, divisor); + index++; + } + + return forVertexBuffer(buffer, bindings); + } + public static TessellationBinding forElementBuffer(GlBuffer buffer) { return new TessellationBinding(GlBufferTarget.ELEMENT_BUFFER, buffer, new GlVertexAttributeBinding[0]); } diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/DefaultChunkRenderer.java b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/DefaultChunkRenderer.java index 673e52a93..db20d1980 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/DefaultChunkRenderer.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/DefaultChunkRenderer.java @@ -3,7 +3,6 @@ package org.embeddedt.embeddium.impl.render.chunk; import it.unimi.dsi.fastutil.objects.Reference2ReferenceMap; import it.unimi.dsi.fastutil.objects.Reference2ReferenceOpenHashMap; import org.embeddedt.embeddium.impl.gl.array.GlVertexArray; -import org.embeddedt.embeddium.impl.gl.attribute.GlVertexAttributeBinding; import org.embeddedt.embeddium.impl.gl.attribute.GlVertexFormat; import org.embeddedt.embeddium.impl.gl.debug.GLDebug; import org.embeddedt.embeddium.impl.gl.device.CommandList; @@ -270,20 +269,9 @@ public abstract class DefaultChunkRenderer extends ShaderChunkRenderer { return tessellation; } - private GlVertexAttributeBinding[] generateVertexAttributeBindings() { - var attributes = this.currentVertexFormat.getAttributes(); - var bindings = new GlVertexAttributeBinding[attributes.size()]; - int i = 0; - for (var attr : attributes) { - bindings[i] = new GlVertexAttributeBinding(i, attr); - i++; - } - return bindings; - } - protected TessellationBinding[] makeTessellationBindingArray(CommandList commandList, RenderRegion.DeviceResources resources) { return new TessellationBinding[] { - TessellationBinding.forVertexBuffer(resources.getVertexBuffer(), this.generateVertexAttributeBindings()), + TessellationBinding.forVertexBuffer(resources.getVertexBuffer(), this.currentVertexFormat), TessellationBinding.forElementBuffer(this.currentRenderPass.isSorted() ? resources.getIndexBuffer() : this.getSharedIndexBuffer(this.renderPassConfiguration.getPrimitiveTypeForPass(this.currentRenderPass), commandList).getBufferObject()) }; }