Please don't take a fence

- Strip out persistent buffer and GlFence code
This commit is contained in:
Jozufozu 2024-08-10 17:22:43 -07:00
parent 752652b3d4
commit e320a7144e
4 changed files with 2 additions and 147 deletions

View file

@ -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;
}
}

View file

@ -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.
*
* <p>
* If Persistent buffers are supported, this will provide one. Otherwise it will fall back to a classic mapped
* buffer.
* </p>
*
* @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;
/**

View file

@ -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;
}
}

View file

@ -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<D extends InstanceData> extends AbstractInstancer<D> {
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);
}