From 5d142d2f135a88fffbac072c34cd42c74bb820de Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Sat, 2 Mar 2024 21:30:08 -0800 Subject: [PATCH] Thought I'd need this - Consolidate common logic from transfer list and scatter list into MemoryBuffer --- .../backend/engine/indirect/ScatterList.java | 24 ++------- .../backend/engine/indirect/TransferList.java | 46 +++++------------ .../flywheel/backend/util/MemoryBuffer.java | 50 +++++++++++++++++++ 3 files changed, 67 insertions(+), 53 deletions(-) create mode 100644 src/main/java/com/jozufozu/flywheel/backend/util/MemoryBuffer.java diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/ScatterList.java b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/ScatterList.java index 4955f4cc7..05ce00d03 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/ScatterList.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/ScatterList.java @@ -2,12 +2,12 @@ package com.jozufozu.flywheel.backend.engine.indirect; import org.lwjgl.system.MemoryUtil; -import com.jozufozu.flywheel.lib.memory.MemoryBlock; +import com.jozufozu.flywheel.backend.util.MemoryBuffer; public class ScatterList { public static final long STRIDE = Integer.BYTES * 2; public final long maxBytesPerScatter; - private MemoryBlock block; + private final MemoryBuffer block = new MemoryBuffer(STRIDE); private int length; private long usedBytes; @@ -46,9 +46,9 @@ public class ScatterList { } public void push(long sizeBytes, long srcOffsetBytes, long dstOffsetBytes) { - reallocIfNeeded(length); + block.reallocIfNeeded(length); - long ptr = block.ptr() + length * STRIDE; + long ptr = block.ptrForIndex(length); MemoryUtil.memPutInt(ptr, packSizeAndSrcOffset(sizeBytes, srcOffsetBytes)); MemoryUtil.memPutInt(ptr + Integer.BYTES, (int) (dstOffsetBytes >> 2)); @@ -78,21 +78,7 @@ public class ScatterList { } public void delete() { - if (block != null) { - block.free(); - } - } - - private void reallocIfNeeded(int index) { - if (block == null) { - block = MemoryBlock.malloc(neededCapacityForIndex(index + 8)); - } else if (block.size() < neededCapacityForIndex(index)) { - block = block.realloc(neededCapacityForIndex(index + 8)); - } - } - - private static long neededCapacityForIndex(int index) { - return (index + 1) * STRIDE; + block.delete(); } private static int packSizeAndSrcOffset(long sizeBytes, long srcOffsetBytes) { diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/TransferList.java b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/TransferList.java index bcba94761..178491434 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/TransferList.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/TransferList.java @@ -2,11 +2,11 @@ package com.jozufozu.flywheel.backend.engine.indirect; import org.lwjgl.system.MemoryUtil; -import com.jozufozu.flywheel.lib.memory.MemoryBlock; +import com.jozufozu.flywheel.backend.util.MemoryBuffer; public class TransferList { private static final long STRIDE = Long.BYTES * 4; - private MemoryBlock block; + private final MemoryBuffer block = new MemoryBuffer(STRIDE); private int length; /** @@ -24,7 +24,7 @@ public class TransferList { return; } - reallocIfNeeded(length); + block.reallocIfNeeded(length); vbo(length, vbo); srcOffset(length, srcOffset); @@ -56,25 +56,23 @@ public class TransferList { } public int vbo(int index) { - return MemoryUtil.memGetInt(ptrForIndex(index)); + return MemoryUtil.memGetInt(block.ptrForIndex(index)); } public long srcOffset(int index) { - return MemoryUtil.memGetLong(ptrForIndex(index) + Long.BYTES); + return MemoryUtil.memGetLong(block.ptrForIndex(index) + Long.BYTES); } public long dstOffset(int index) { - return MemoryUtil.memGetLong(ptrForIndex(index) + Long.BYTES * 2); + return MemoryUtil.memGetLong(block.ptrForIndex(index) + Long.BYTES * 2); } public long size(int index) { - return MemoryUtil.memGetLong(ptrForIndex(index) + Long.BYTES * 3); + return MemoryUtil.memGetLong(block.ptrForIndex(index) + Long.BYTES * 3); } public void delete() { - if (block != null) { - block.free(); - } + block.delete(); } private boolean continuesLast(int vbo, long srcOffset, long dstOffset) { @@ -87,38 +85,18 @@ public class TransferList { } private void vbo(int index, int vbo) { - MemoryUtil.memPutInt(ptrForIndex(index), vbo); + MemoryUtil.memPutInt(block.ptrForIndex(index), vbo); } private void srcOffset(int index, long srcOffset) { - MemoryUtil.memPutLong(ptrForIndex(index) + Long.BYTES, srcOffset); + MemoryUtil.memPutLong(block.ptrForIndex(index) + Long.BYTES, srcOffset); } private void dstOffset(int index, long dstOffset) { - MemoryUtil.memPutLong(ptrForIndex(index) + Long.BYTES * 2, dstOffset); + MemoryUtil.memPutLong(block.ptrForIndex(index) + Long.BYTES * 2, dstOffset); } private void size(int index, long size) { - MemoryUtil.memPutLong(ptrForIndex(index) + Long.BYTES * 3, size); - } - - private void reallocIfNeeded(int index) { - if (block == null) { - block = MemoryBlock.malloc(neededCapacityForIndex(index + 8)); - } else if (block.size() < neededCapacityForIndex(index)) { - block = block.realloc(neededCapacityForIndex(index + 8)); - } - } - - private long ptrForIndex(int index) { - return block.ptr() + bytePosForIndex(index); - } - - private static long bytePosForIndex(int index) { - return index * STRIDE; - } - - private static long neededCapacityForIndex(int index) { - return (index + 1) * STRIDE; + MemoryUtil.memPutLong(block.ptrForIndex(index) + Long.BYTES * 3, size); } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/util/MemoryBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/util/MemoryBuffer.java new file mode 100644 index 000000000..47d52f221 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/util/MemoryBuffer.java @@ -0,0 +1,50 @@ +package com.jozufozu.flywheel.backend.util; + +import org.jetbrains.annotations.Nullable; + +import com.jozufozu.flywheel.lib.memory.MemoryBlock; + +public class MemoryBuffer { + private final long stride; + + @Nullable + private MemoryBlock block; + + public MemoryBuffer(long stride) { + this.stride = stride; + } + + public boolean reallocIfNeeded(int index) { + if (block == null) { + block = MemoryBlock.malloc(neededCapacityForIndex(index + 8)); + return true; + } else if (block.size() < neededCapacityForIndex(index)) { + block = block.realloc(neededCapacityForIndex(index + 8)); + return true; + } + + return false; + } + + public long ptr() { + return block.ptr(); + } + + public long ptrForIndex(int index) { + return block.ptr() + bytePosForIndex(index); + } + + public long bytePosForIndex(int index) { + return index * stride; + } + + public long neededCapacityForIndex(int index) { + return (index + 1) * stride; + } + + public void delete() { + if (block != null) { + block.free(); + } + } +}