From 56d5bf260fd1a17669e02be609ee73fca8cb8709 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Sat, 10 Aug 2024 17:22:43 -0700 Subject: [PATCH] Please don't take a fence - Strip out persistent buffer and GlFence code --- .../jozufozu/flywheel/backend/gl/GlFence.java | 41 --------- .../flywheel/backend/gl/buffer/GlBuffer.java | 21 ----- .../backend/gl/buffer/PersistentGlBuffer.java | 84 ------------------- .../instancing/instancing/GPUInstancer.java | 3 +- 4 files changed, 2 insertions(+), 147 deletions(-) delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/gl/GlFence.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/gl/buffer/PersistentGlBuffer.java diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/GlFence.java b/src/main/java/com/jozufozu/flywheel/backend/gl/GlFence.java deleted file mode 100644 index 78f6df423..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/GlFence.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.jozufozu.flywheel.backend.gl; - -import static org.lwjgl.opengl.GL32.GL_ALREADY_SIGNALED; -import static org.lwjgl.opengl.GL32.GL_CONDITION_SATISFIED; -import static org.lwjgl.opengl.GL32.GL_SYNC_FLUSH_COMMANDS_BIT; -import static org.lwjgl.opengl.GL32.GL_SYNC_GPU_COMMANDS_COMPLETE; -import static org.lwjgl.opengl.GL32.GL_UNSIGNALED; -import static org.lwjgl.opengl.GL32.glClientWaitSync; -import static org.lwjgl.opengl.GL32.glDeleteSync; -import static org.lwjgl.opengl.GL32.glFenceSync; - -public class GlFence { - - private long fence; - - public void post() { - clear(); - - fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); - } - - public void clear() { - if (fence != 0) { - glDeleteSync(fence); - fence = 0; - } - } - - public void waitSync() { - if (fence != 0) { - int waitReturn = GL_UNSIGNALED; - while (waitReturn != GL_ALREADY_SIGNALED && waitReturn != GL_CONDITION_SATISFIED) { - waitReturn = glClientWaitSync(fence, GL_SYNC_FLUSH_COMMANDS_BIT, 1); - } - - glDeleteSync(fence); - } - - fence = 0; - } -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/GlBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/GlBuffer.java index 55d8d7037..3566b5b04 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/GlBuffer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/GlBuffer.java @@ -5,30 +5,9 @@ import java.nio.ByteBuffer; import org.lwjgl.opengl.GL20; import com.jozufozu.flywheel.backend.gl.GlObject; -import com.jozufozu.flywheel.backend.gl.versioned.GlCompat; public abstract class GlBuffer extends GlObject { - /** - * Request a Persistent mapped buffer. - * - *

- * If Persistent buffers are supported, this will provide one. Otherwise it will fall back to a classic mapped - * buffer. - *

- * - * @param type The type of buffer you want. - * @return A buffer that will be persistent if the driver supports it. - */ - public static GlBuffer requestPersistent(GlBufferType type) { - if (GlCompat.getInstance() - .bufferStorageSupported()) { - return new PersistentGlBuffer(type); - } else { - return new MappedGlBuffer(type); - } - } - protected final GlBufferType type; /** diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/PersistentGlBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/PersistentGlBuffer.java deleted file mode 100644 index ff81679c2..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/PersistentGlBuffer.java +++ /dev/null @@ -1,84 +0,0 @@ -package com.jozufozu.flywheel.backend.gl.buffer; - -import static org.lwjgl.opengl.GL30.GL_MAP_WRITE_BIT; -import static org.lwjgl.opengl.GL44.GL_MAP_COHERENT_BIT; -import static org.lwjgl.opengl.GL44.GL_MAP_PERSISTENT_BIT; - -import java.nio.ByteBuffer; - -import org.lwjgl.opengl.GL30; - -import com.jozufozu.flywheel.backend.gl.GlFence; -import com.jozufozu.flywheel.backend.gl.error.GlError; -import com.jozufozu.flywheel.backend.gl.error.GlException; -import com.jozufozu.flywheel.backend.gl.versioned.GlCompat; - -public class PersistentGlBuffer extends GlBuffer implements Mappable { - - private MappedBuffer buffer; - int flags; - - long size; - GlFence fence; - - public PersistentGlBuffer(GlBufferType type) { - super(type); - - flags = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT; - fence = new GlFence(); - } - - @Override - public void doneForThisFrame() { - fence.post(); - } - - @Override - protected void alloc(long size) { - this.size = size; - - if (buffer != null) { - deleteInternal(handle()); - _create(); - - bind(); - } - - fence.clear(); - - GlCompat.getInstance().bufferStorage.bufferStorage(type, size, flags); - - ByteBuffer byteBuffer = GL30.glMapBufferRange(type.glEnum, 0, size, flags); - - if (byteBuffer == null) { - throw new GlException(GlError.poll(), "Could not map buffer"); - } - - buffer = new MappedBuffer(this, byteBuffer, 0, size); - } - - @Override - public void upload(ByteBuffer directBuffer) { - throw new UnsupportedOperationException("FIXME: Nothing calls #upload on a persistent buffer as of 12/10/2021."); - } - - @Override - public MappedBuffer getBuffer(long offset, long length) { - - fence.waitSync(); - - buffer.position((int) offset); - - return buffer; - } - - @Override - public GlBufferType getType() { - return type; - } - - @Override - public boolean isPersistent() { - return true; - } -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancer.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancer.java index 448e319d4..03ce112f4 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancer.java @@ -10,6 +10,7 @@ import com.jozufozu.flywheel.backend.gl.GlVertexArray; import com.jozufozu.flywheel.backend.gl.buffer.GlBuffer; import com.jozufozu.flywheel.backend.gl.buffer.GlBufferType; import com.jozufozu.flywheel.backend.gl.buffer.MappedBuffer; +import com.jozufozu.flywheel.backend.gl.buffer.MappedGlBuffer; import com.jozufozu.flywheel.backend.gl.versioned.GlCompat; import com.jozufozu.flywheel.backend.instancing.AbstractInstancer; import com.jozufozu.flywheel.backend.model.BufferedModel; @@ -80,7 +81,7 @@ public class GPUInstancer extends AbstractInstancer { vao.bind(); vao.enableArrays(model.getAttributeCount() + instanceFormat.getAttributeCount()); - instanceVBO = GlBuffer.requestPersistent(GlBufferType.ARRAY_BUFFER); + instanceVBO = new MappedGlBuffer(GlBufferType.ARRAY_BUFFER); instanceVBO.setGrowthMargin(instanceFormat.getStride() * 16); }