More work on the buffers

This commit is contained in:
Speiger 2025-03-23 02:28:58 +01:00
parent d0a462aed8
commit ba4fa73cad
4 changed files with 66 additions and 0 deletions
src/main/java/speiger/src/coreengine/rendering

@ -31,6 +31,8 @@ public interface IUIRenderer {
public void drawCustom(Consumer<Matrix4f> matrix);
public void drawLine(float minX, float minY, float maxX, float maxY, float zLevel, int color);
public default void drawFrame(IGuiBox box, int color) { drawFrame(box.getMinX(), box.getMinY(), box.getMaxX(), box.getMaxY(), 0F, color); }

@ -15,4 +15,6 @@ public interface IVertexBuffer {
public IVertexBuffer normal(float x, float y, float z);
public IVertexBuffer endVertex();
}

@ -1,11 +1,17 @@
package speiger.src.coreengine.rendering.tesselation.buffer;
import java.util.List;
import speiger.src.coreengine.rendering.tesselation.format.VertexElement;
import speiger.src.coreengine.rendering.tesselation.format.VertexElement.Usage;
import speiger.src.coreengine.rendering.utils.values.GLMode;
public interface IVertexBuilder extends IVertexBuffer {
public VertexElement current();
public IVertexBuilder nextElement();
public IVertexBuilder endDrawCall();
public List<BufferDrawCall> getDrawCalls();
public IVertexBuilder put(int offset, byte value);
public IVertexBuilder putShort(int offset, short value);
public IVertexBuilder putInt(int offset, int value);
@ -73,4 +79,9 @@ public interface IVertexBuilder extends IVertexBuffer {
throw new IllegalStateException("Expected State [Usage="+element.usage()+", Size="+element.size()+"], wasn't present. Actual State [Usage="+usage+", Size="+size+"]");
}
}
public record BufferDrawCall(GLMode mode, int startVertex, int endVertex) {
}
}

@ -1,9 +1,12 @@
package speiger.src.coreengine.rendering.tesselation.buffer;
import java.nio.ByteBuffer;
import java.util.List;
import org.lwjgl.system.MemoryUtil;
import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.collections.objects.utils.ObjectLists;
import speiger.src.coreengine.rendering.tesselation.format.VertexElement;
import speiger.src.coreengine.rendering.tesselation.format.VertexFormat;
import speiger.src.coreengine.rendering.utils.values.GLMode;
@ -17,6 +20,8 @@ public class VertexBuilder implements IVertexBuilder {
int totalStoredBytes;
int vertexBytes;
int elementBytes;
int lastVertecies = 0;
List<BufferDrawCall> drawCalls;
public VertexBuilder(int size) {
this(MemoryUtil.memAlloc(size));
@ -37,6 +42,26 @@ public class VertexBuilder implements IVertexBuilder {
return this;
}
private void ensureCapacity(int newValue) {
if(buffer.isDirect() && totalStoredBytes + newValue >= buffer.capacity()) {
int oldSize = buffer.capacity();
int newSize = Math.max(oldSize + (oldSize >> 1), oldSize + newValue);
buffer = MemoryUtil.memRealloc(buffer, newSize); //TODO that if the old buffer gets deallocated
}
}
public VertexBuilder reset() {
index = 0;
vertecies = 0;
elementBytes = 0;
totalStoredBytes = 0;
vertexBytes = 0;
lastVertecies = 0;
buffer.rewind();
drawCalls = null;
return this;
}
@Override
public IVertexBuffer endVertex() {
if(index != 0) throw new IllegalArgumentException("Format is not completed");
@ -46,9 +71,22 @@ public class VertexBuilder implements IVertexBuilder {
vertecies++;
}
vertexBytes = 0;
//TODO ensure capacity too!
return this;
}
public VertexBuilder endDrawCall() {
if(drawCalls == null) drawCalls = new ObjectArrayList<>();
drawCalls.add(new BufferDrawCall(mode, lastVertecies, vertecies));
lastVertecies = vertecies;
return this;
}
@Override
public List<BufferDrawCall> getDrawCalls() {
return drawCalls == null ? ObjectLists.empty() : drawCalls;
}
@Override
public VertexElement current() {
return format.get(index);
@ -94,6 +132,19 @@ public class VertexBuilder implements IVertexBuilder {
return this;
}
public VertexBuilder putBulk(ByteBuffer source) {
ensureCapacity(source.limit() + format.totalOffset());
buffer.position(totalStoredBytes).put(source).position(0);
vertecies += source.limit() / format.totalOffset();
totalStoredBytes += source.limit();
return this;
}
public VertexBuilder copy(ByteBuffer buffer) {
buffer.put(buffer.position(), this.buffer, 0, this.buffer.position());
return this;
}
public byte[] getBytes() {
byte[] data = new byte[totalStoredBytes];
buffer.get(0, data);