support instanced vertex attribute bindings #27

Merged
embeddedt merged 2 commits from rdh/celeritas:stonecutter into stonecutter 2026-07-23 17:35:04 -04:00
8 changed files with 111 additions and 16 deletions

No files matched your search

@@ -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<GLSync> 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
@@ -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
@@ -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 =====================
@@ -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;
}
}
@@ -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<T> 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);
@@ -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());
}
}
}
}
@@ -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]);
}
@@ -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())
};
}