diff --git a/src/main/java/com/jozufozu/flywheel/api/uniform/UniformProvider.java b/src/main/java/com/jozufozu/flywheel/api/uniform/UniformProvider.java index 2e189f452..62890b521 100644 --- a/src/main/java/com/jozufozu/flywheel/api/uniform/UniformProvider.java +++ b/src/main/java/com/jozufozu/flywheel/api/uniform/UniformProvider.java @@ -6,13 +6,13 @@ import com.jozufozu.flywheel.core.source.FileResolution; public abstract class UniformProvider { - protected ByteBuffer buffer; + protected long ptr; protected Notifier notifier; public abstract int getSize(); - public void updatePtr(ByteBuffer backing, Notifier notifier) { - this.buffer = backing; + public void updatePtr(long ptr, Notifier notifier) { + this.ptr = ptr; this.notifier = notifier; } diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/array/GlVertexArray.java b/src/main/java/com/jozufozu/flywheel/backend/gl/array/GlVertexArray.java index 3abf7a7d0..69efb178f 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/array/GlVertexArray.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/array/GlVertexArray.java @@ -5,8 +5,8 @@ import org.lwjgl.opengl.GL32; import com.jozufozu.flywheel.backend.gl.GlObject; import com.jozufozu.flywheel.backend.gl.GlStateTracker; -import com.jozufozu.flywheel.backend.gl.buffer.GlBuffer; import com.jozufozu.flywheel.backend.gl.buffer.GlBufferType; +import com.jozufozu.flywheel.backend.gl.buffer.GlBuffer; import com.jozufozu.flywheel.backend.gl.versioned.GlCompat; import com.jozufozu.flywheel.core.layout.BufferLayout; import com.mojang.blaze3d.platform.GlStateManager; 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 395c514ec..edfff94d3 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 @@ -1,49 +1,105 @@ package com.jozufozu.flywheel.backend.gl.buffer; -import java.nio.ByteBuffer; +import static org.lwjgl.opengl.GL32.*; -import org.lwjgl.opengl.GL20; +import org.lwjgl.system.MemoryUtil; import com.jozufozu.flywheel.backend.gl.GlObject; -import com.jozufozu.flywheel.backend.gl.versioned.GlCompat; +import com.jozufozu.flywheel.backend.gl.error.GlError; +import com.jozufozu.flywheel.backend.gl.error.GlException; +import com.jozufozu.flywheel.backend.memory.FlwMemoryTracker; +import com.jozufozu.flywheel.backend.memory.MemoryBlock; -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); - } - } +public class GlBuffer extends GlObject { public final GlBufferType type; - + protected final GlBufferUsage usage; /** * The size (in bytes) of the buffer on the GPU. */ protected long size; - /** * How much extra room to give the buffer when we reallocate. */ protected int growthMargin; public GlBuffer(GlBufferType type) { - setHandle(GL20.glGenBuffers()); + this(type, GlBufferUsage.STATIC_DRAW); + } + + public GlBuffer(GlBufferType type, GlBufferUsage usage) { + setHandle(glGenBuffers()); this.type = type; + this.usage = usage; + } + + public boolean ensureCapacity(long size) { + if (size < 0) { + throw new IllegalArgumentException("Size " + size + " < 0"); + } + + if (size == 0) { + return false; + } + + if (this.size == 0) { + this.size = size; + bind(); + glBufferData(type.glEnum, size, usage.glEnum); + FlwMemoryTracker._allocGPUMemory(size); + + return true; + } + + if (size > this.size) { + var oldSize = this.size; + this.size = size + growthMargin; + + realloc(oldSize, this.size); + + return true; + } + + return false; + } + + private void realloc(long oldSize, long newSize) { + FlwMemoryTracker._freeGPUMemory(oldSize); + FlwMemoryTracker._allocGPUMemory(newSize); + var oldHandle = handle(); + var newHandle = glGenBuffers(); + + GlBufferType.COPY_READ_BUFFER.bind(oldHandle); + type.bind(newHandle); + + glBufferData(type.glEnum, newSize, usage.glEnum); + glCopyBufferSubData(GlBufferType.COPY_READ_BUFFER.glEnum, type.glEnum, 0, 0, oldSize); + + glDeleteBuffers(oldHandle); + setHandle(newHandle); + } + + public void upload(MemoryBlock directBuffer) { + bind(); + FlwMemoryTracker._freeGPUMemory(size); + nglBufferData(type.glEnum, directBuffer.size(), directBuffer.ptr(), usage.glEnum); + this.size = directBuffer.size(); + FlwMemoryTracker._allocGPUMemory(size); + } + + public MappedBuffer map() { + bind(); + long ptr = nglMapBufferRange(type.glEnum, 0, size, GL_MAP_WRITE_BIT); + + if (ptr == MemoryUtil.NULL) { + throw new GlException(GlError.poll(), "Could not map buffer"); + } + + return new MappedBuffer(this, ptr, 0, size); + } + + public boolean isPersistent() { + return false; } public void setGrowthMargin(int growthMargin) { @@ -66,24 +122,8 @@ public abstract class GlBuffer extends GlObject { type.unbind(); } - public abstract void upload(ByteBuffer directBuffer); - - public abstract MappedBuffer map(); - - /** - * Ensure that the buffer has at least enough room to store {@code size} bytes. - * - * @return {@code true} if the buffer moved. - */ - public abstract boolean ensureCapacity(long size); - protected void deleteInternal(int handle) { - GL20.glDeleteBuffers(handle); + glDeleteBuffers(handle); + FlwMemoryTracker._freeGPUMemory(size); } - - /** - * Indicates that this buffer need not be #flush()'d for its contents to sync. - * @return true if this buffer is persistently mapped. - */ - public abstract boolean isPersistent(); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedBuffer.java index 4ccdbe721..4b2da8992 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedBuffer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedBuffer.java @@ -1,6 +1,6 @@ package com.jozufozu.flywheel.backend.gl.buffer; -import java.nio.ByteBuffer; +import static org.lwjgl.system.MemoryUtil.NULL; import org.lwjgl.opengl.GL15; import org.lwjgl.system.MemoryUtil; @@ -11,10 +11,10 @@ public class MappedBuffer implements AutoCloseable { private final long length; private final GlBuffer owner; private final boolean persistent; - private ByteBuffer internal; + private long ptr; - public MappedBuffer(GlBuffer owner, ByteBuffer internal, long offset, long length) { - this.internal = internal; + public MappedBuffer(GlBuffer owner, long ptr, long offset, long length) { + this.ptr = ptr; this.owner = owner; this.offset = offset; this.length = length; @@ -27,19 +27,11 @@ public class MappedBuffer implements AutoCloseable { public void flush() { if (persistent) return; - if (internal == null) return; + if (ptr == NULL) return; owner.bind(); GL15.glUnmapBuffer(owner.getType().glEnum); - internal = null; - } - - public MappedBuffer position(int p) { - if (p < offset || p >= offset + length) { - throw new IndexOutOfBoundsException("Index " + p + " is not mapped"); - } - internal.position(p - (int) offset); - return this; + ptr = NULL; } @Override @@ -47,12 +39,8 @@ public class MappedBuffer implements AutoCloseable { flush(); } - public ByteBuffer unwrap() { - return internal; - } - - public long getMemAddress() { - return MemoryUtil.memAddress(internal); + public long getPtr() { + return ptr; } public void clear(long clearStart, long clearLength) { @@ -64,7 +52,7 @@ public class MappedBuffer implements AutoCloseable { throw new IndexOutOfBoundsException("Clear range [" + clearStart + "," + (clearStart + clearLength) + "] is not mapped"); } - long addr = MemoryUtil.memAddress(unwrap()) + clearStart; + long addr = ptr + clearStart; MemoryUtil.memSet(addr, 0, clearLength); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedGlBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedGlBuffer.java deleted file mode 100644 index 6d0e5956b..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedGlBuffer.java +++ /dev/null @@ -1,91 +0,0 @@ -package com.jozufozu.flywheel.backend.gl.buffer; - -import java.nio.ByteBuffer; - -import org.lwjgl.opengl.GL30; -import org.lwjgl.opengl.GL32; - -import com.jozufozu.flywheel.backend.gl.error.GlError; -import com.jozufozu.flywheel.backend.gl.error.GlException; - -public class MappedGlBuffer extends GlBuffer { - - protected final GlBufferUsage usage; - - public MappedGlBuffer(GlBufferType type) { - this(type, GlBufferUsage.STATIC_DRAW); - } - - public MappedGlBuffer(GlBufferType type, GlBufferUsage usage) { - super(type); - this.usage = usage; - } - - @Override - public boolean ensureCapacity(long size) { - if (size < 0) { - throw new IllegalArgumentException("Size " + size + " < 0"); - } - - if (size == 0) { - return false; - } - - if (this.size == 0) { - this.size = size; - bind(); - GL32.glBufferData(type.glEnum, size, usage.glEnum); - - return true; - } - - if (size > this.size) { - var oldSize = this.size; - this.size = size + growthMargin; - - realloc(oldSize, this.size); - - return true; - } - - return false; - } - - private void realloc(long oldSize, long newSize) { - var oldHandle = handle(); - var newHandle = GL32.glGenBuffers(); - - GlBufferType.COPY_READ_BUFFER.bind(oldHandle); - type.bind(newHandle); - - GL32.glBufferData(type.glEnum, newSize, usage.glEnum); - GL32.glCopyBufferSubData(GlBufferType.COPY_READ_BUFFER.glEnum, type.glEnum, 0, 0, oldSize); - - delete(); - setHandle(newHandle); - } - - @Override - public void upload(ByteBuffer directBuffer) { - bind(); - GL32.glBufferData(type.glEnum, directBuffer, usage.glEnum); - this.size = directBuffer.capacity(); - } - - @Override - public MappedBuffer map() { - bind(); - ByteBuffer byteBuffer = GL30.glMapBufferRange(type.glEnum, 0, size, GL30.GL_MAP_WRITE_BIT); - - if (byteBuffer == null) { - throw new GlException(GlError.poll(), "Could not map buffer"); - } - - return new MappedBuffer(this, byteBuffer, 0, size); - } - - @Override - public boolean isPersistent() { - return false; - } -} 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 e0ffa6c26..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/PersistentGlBuffer.java +++ /dev/null @@ -1,124 +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.jetbrains.annotations.Nullable; -import org.lwjgl.opengl.GL32; -import org.lwjgl.system.MemoryUtil; - -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 { - - @Nullable - private MappedBuffer access; - private final int storageFlags; - - public PersistentGlBuffer(GlBufferType type) { - super(type); - - storageFlags = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT; - } - - @Override - public boolean ensureCapacity(long size) { - if (size < 0) { - throw new IllegalArgumentException("Size " + size + " < 0"); - } - - if (size == 0) { - return false; - } - - if (this.size == 0) { - this.size = size; - bind(); - GlCompat.getInstance().bufferStorage.bufferStorage(type, this.size, storageFlags); - return true; - } - - if (size > this.size) { - var oldSize = this.size; - this.size = size + growthMargin; - - realloc(this.size, oldSize); - - access = null; - return true; - } - - return false; - } - - @Override - public void upload(ByteBuffer directBuffer) { - ensureCapacity(directBuffer.capacity()); - - var access = getWriteAccess(); - - ByteBuffer ourBuffer = access.unwrap(); - - ourBuffer.reset(); - - MemoryUtil.memCopy(directBuffer, ourBuffer); - - int uploadSize = directBuffer.remaining(); - int ourSize = ourBuffer.capacity(); - - if (uploadSize < ourSize) { - long clearFrom = access.getMemAddress() + uploadSize; - MemoryUtil.memSet(clearFrom, 0, ourSize - uploadSize); - } - } - - private void mapToClientMemory() { - bind(); - ByteBuffer byteBuffer = GL32.glMapBufferRange(type.glEnum, 0, size, storageFlags); - - if (byteBuffer == null) { - throw new GlException(GlError.poll(), "Could not map buffer"); - } - - access = new MappedBuffer(this, byteBuffer, 0, size); - } - - private void realloc(long newSize, long oldSize) { - int oldHandle = handle(); - int newHandle = GL32.glGenBuffers(); - - GlBufferType.COPY_READ_BUFFER.bind(oldHandle); - type.bind(newHandle); - - GlCompat.getInstance().bufferStorage.bufferStorage(type, newSize, storageFlags); - - GL32.glCopyBufferSubData(GlBufferType.COPY_READ_BUFFER.glEnum, type.glEnum, 0, 0, oldSize); - - delete(); - setHandle(newHandle); - } - - @Override - public MappedBuffer map() { - return getWriteAccess() - .position(0); - } - - private MappedBuffer getWriteAccess() { - if (access == null) { - mapToClientMemory(); - } - - return access; - } - - @Override - public boolean isPersistent() { - return true; - } -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java index 3962d4d1a..1f48ec759 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java @@ -27,7 +27,7 @@ import net.minecraft.world.level.block.entity.BlockEntity; * The instancer manager is shared between the different instance managers. * */ -public class InstanceWorld { +public class InstanceWorld implements AutoCloseable { protected final Engine engine; protected final InstanceManager
*
* Does not clear the backing buffer.
*/
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 95015766a..cfce702fb 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
@@ -3,18 +3,15 @@ package com.jozufozu.flywheel.backend.instancing.instancing;
import java.util.HashSet;
import java.util.Set;
-import org.lwjgl.system.MemoryUtil;
-
import com.jozufozu.flywheel.Flywheel;
import com.jozufozu.flywheel.api.instancer.InstancedPart;
import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.api.struct.StructWriter;
import com.jozufozu.flywheel.backend.gl.array.GlVertexArray;
-import com.jozufozu.flywheel.backend.gl.buffer.GlBuffer;
import com.jozufozu.flywheel.backend.gl.buffer.GlBufferType;
import com.jozufozu.flywheel.backend.gl.buffer.GlBufferUsage;
import com.jozufozu.flywheel.backend.gl.buffer.MappedBuffer;
-import com.jozufozu.flywheel.backend.gl.buffer.MappedGlBuffer;
+import com.jozufozu.flywheel.backend.gl.buffer.GlBuffer;
import com.jozufozu.flywheel.backend.instancing.AbstractInstancer;
import com.jozufozu.flywheel.core.layout.BufferLayout;
@@ -45,7 +42,7 @@ public class GPUInstancer