From f0823af00f109158efcde7bbe267ae1917f4bb0d Mon Sep 17 00:00:00 2001 From: PepperCode1 <44146161+PepperCode1@users.noreply.github.com> Date: Wed, 10 Aug 2022 20:47:53 -0700 Subject: [PATCH] Wait, it's all unsafe? Always has been. - Create MemoryBlock utility for easier management of memory allocation - Replace most usage of direct ByteBuffer with MemoryBlock - Remove all usage of Mojang's MemoryTracker - Simplifier StructWriter - Rename some classes and packages --- .../flywheel/api/struct/StructType.java | 11 +- .../flywheel/api/struct/StructWriter.java | 14 +- .../flywheel/backend/FlywheelMemory.java | 57 ------- .../backend/gl/versioned/GlCompat.java | 14 +- .../instancing/batching/DrawBuffer.java | 35 +++-- .../instancing/batching/TransformSet.java | 6 +- .../blockentity/BlockEntityInstance.java | 2 +- .../instancing/instancing/GPUInstancer.java | 16 +- .../instancing/instancing/MeshPool.java | 5 +- .../backend/memory/FlwMemoryTracker.java | 141 ++++++++++++++++++ .../flywheel/backend/memory/MemoryBlock.java | 45 ++++++ .../backend/memory/MemoryBlockImpl.java | 76 ++++++++++ .../memory/TrackedMemoryBlockImpl.java | 38 +++++ .../flywheel/backend/struct/BufferWriter.java | 40 ----- .../backend/struct/UnsafeBufferWriter.java | 45 ------ .../flywheel/backend/struct/package-info.java | 6 - .../flywheel/core/hardcoded/ModelPart.java | 20 +-- .../jozufozu/flywheel/core/model/Mesh.java | 6 +- .../flywheel/core/model/ModelUtil.java | 8 +- .../flywheel/core/model/SimpleMesh.java | 25 ++-- .../model/buffering/BakedModelBuilder.java | 6 +- .../model/buffering/BlockModelBuilder.java | 6 +- .../buffering/MultiBlockModelBuilder.java | 6 +- .../core/structs/ColoredLitWriter.java | 17 +++ .../core/structs/ColoredLitWriterUnsafe.java | 26 ---- .../flywheel/core/structs/StructTypes.java | 4 +- .../model/TransformedWriterUnsafe.java | 23 --- .../core/structs/oriented/OrientedType.java | 8 +- ...dWriterUnsafe.java => OrientedWriter.java} | 16 +- .../TransformedPart.java | 2 +- .../TransformedType.java | 10 +- .../transformed/TransformedWriter.java | 17 +++ .../{model => transformed}/package-info.java | 2 +- .../flywheel/core/uniform/FogProvider.java | 2 - .../flywheel/core/uniform/UniformBuffer.java | 4 +- .../jozufozu/flywheel/event/ForgeEvents.java | 7 +- .../jozufozu/flywheel/light/LightVolume.java | 9 +- .../jozufozu/flywheel/util/StringUtil.java | 12 +- .../flywheel/vanilla/ChestInstance.java | 2 +- .../flywheel/vanilla/MinecartInstance.java | 2 +- .../flywheel/vanilla/ShulkerBoxInstance.java | 2 +- .../vanilla/effect/ExampleEffect.java | 2 +- 42 files changed, 445 insertions(+), 350 deletions(-) delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/FlywheelMemory.java create mode 100644 src/main/java/com/jozufozu/flywheel/backend/memory/FlwMemoryTracker.java create mode 100644 src/main/java/com/jozufozu/flywheel/backend/memory/MemoryBlock.java create mode 100644 src/main/java/com/jozufozu/flywheel/backend/memory/MemoryBlockImpl.java create mode 100644 src/main/java/com/jozufozu/flywheel/backend/memory/TrackedMemoryBlockImpl.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/struct/BufferWriter.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/struct/UnsafeBufferWriter.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/struct/package-info.java create mode 100644 src/main/java/com/jozufozu/flywheel/core/structs/ColoredLitWriter.java delete mode 100644 src/main/java/com/jozufozu/flywheel/core/structs/ColoredLitWriterUnsafe.java delete mode 100644 src/main/java/com/jozufozu/flywheel/core/structs/model/TransformedWriterUnsafe.java rename src/main/java/com/jozufozu/flywheel/core/structs/oriented/{OrientedWriterUnsafe.java => OrientedWriter.java} (54%) rename src/main/java/com/jozufozu/flywheel/core/structs/{model => transformed}/TransformedPart.java (97%) rename src/main/java/com/jozufozu/flywheel/core/structs/{model => transformed}/TransformedType.java (86%) create mode 100644 src/main/java/com/jozufozu/flywheel/core/structs/transformed/TransformedWriter.java rename src/main/java/com/jozufozu/flywheel/core/structs/{model => transformed}/package-info.java (75%) diff --git a/src/main/java/com/jozufozu/flywheel/api/struct/StructType.java b/src/main/java/com/jozufozu/flywheel/api/struct/StructType.java index 7841f92de..7bdeab3a0 100644 --- a/src/main/java/com/jozufozu/flywheel/api/struct/StructType.java +++ b/src/main/java/com/jozufozu/flywheel/api/struct/StructType.java @@ -1,7 +1,5 @@ package com.jozufozu.flywheel.api.struct; -import java.nio.ByteBuffer; - import com.jozufozu.flywheel.api.instancer.InstancedPart; import com.jozufozu.flywheel.api.vertex.MutableVertexList; import com.jozufozu.flywheel.core.layout.BufferLayout; @@ -25,16 +23,11 @@ public interface StructType { */ BufferLayout getLayout(); - /** - * Create a {@link StructWriter} that will consume instances of S and write them to the given buffer. - * - * @param backing The buffer that the StructWriter will write to. - */ - StructWriter getWriter(ByteBuffer backing); + StructWriter getWriter(); FileResolution getInstanceShader(); - VertexTransformer getVertexTransformer(); + VertexTransformer getVertexTransformer(); public interface VertexTransformer { void transform(MutableVertexList vertexList, S struct, ClientLevel level); diff --git a/src/main/java/com/jozufozu/flywheel/api/struct/StructWriter.java b/src/main/java/com/jozufozu/flywheel/api/struct/StructWriter.java index abebee807..beb89d025 100644 --- a/src/main/java/com/jozufozu/flywheel/api/struct/StructWriter.java +++ b/src/main/java/com/jozufozu/flywheel/api/struct/StructWriter.java @@ -1,17 +1,13 @@ package com.jozufozu.flywheel.api.struct; +import com.jozufozu.flywheel.api.instancer.InstancedPart; + /** * StructWriters can quickly consume many instances of S and write them to some backing buffer. */ -public interface StructWriter { - +public interface StructWriter { /** - * Write the given struct to the backing array. + * Write the given struct to given memory address. */ - void write(S struct); - - /** - * Seek to the given position. The next write will occur there. - */ - void seek(int pos); + void write(long ptr, S struct); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/FlywheelMemory.java b/src/main/java/com/jozufozu/flywheel/backend/FlywheelMemory.java deleted file mode 100644 index 3d3f8bd60..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/FlywheelMemory.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.jozufozu.flywheel.backend; - -import java.lang.ref.Cleaner; -import java.nio.ByteBuffer; - -import org.lwjgl.system.MemoryUtil; - -public class FlywheelMemory { - - public static final Cleaner CLEANER = Cleaner.create(); - - private static int gpuMemory = 0; - private static int cpuMemory = 0; - - public static void _freeCPUMemory(long size) { - cpuMemory -= size; - } - - public static void _allocCPUMemory(long size) { - cpuMemory += size; - } - - public static void _freeGPUMemory(long size) { - gpuMemory -= size; - } - - public static void _allocGPUMemory(long size) { - gpuMemory += size; - } - - public static int getGPUMemory() { - return gpuMemory; - } - - public static int getCPUMemory() { - return cpuMemory; - } - - public static Cleaner.Cleanable track(Object owner, ByteBuffer buffer) { - return CLEANER.register(owner, new Tracked(buffer)); - } - - public static class Tracked implements Runnable { - - private final ByteBuffer buffer; - - public Tracked(ByteBuffer buffer) { - this.buffer = buffer; - _allocCPUMemory(buffer.capacity()); - } - - public void run() { - _freeCPUMemory(buffer.capacity()); - MemoryUtil.memFree(buffer); - } - } -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/versioned/GlCompat.java b/src/main/java/com/jozufozu/flywheel/backend/gl/versioned/GlCompat.java index 059dd15b1..97a9f6850 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/versioned/GlCompat.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/versioned/GlCompat.java @@ -8,7 +8,6 @@ import org.lwjgl.opengl.GL; import org.lwjgl.opengl.GL20C; import org.lwjgl.opengl.GLCapabilities; import org.lwjgl.system.MemoryStack; -import org.lwjgl.system.MemoryUtil; import net.minecraft.Util; @@ -75,7 +74,7 @@ public class GlCompat { } /** - * Copied from: + * Modified from: *
canvas * *

Identical in function to {@link GL20C#glShaderSource(int, CharSequence)} but @@ -87,18 +86,11 @@ public class GlCompat { *

Hat tip to fewizz for the find and the fix. */ public static void safeShaderSource(int glId, CharSequence source) { - final MemoryStack stack = MemoryStack.stackGet(); - final int stackPointer = stack.getPointer(); - - try { - final ByteBuffer sourceBuffer = MemoryUtil.memUTF8(source, true); + try (MemoryStack stack = MemoryStack.stackPush()) { + final ByteBuffer sourceBuffer = stack.UTF8(source, true); final PointerBuffer pointers = stack.mallocPointer(1); pointers.put(sourceBuffer); - GL20C.nglShaderSource(glId, 1, pointers.address0(), 0); - org.lwjgl.system.APIUtil.apiArrayFree(pointers.address0(), 1); - } finally { - stack.setPointer(stackPointer); } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/DrawBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/DrawBuffer.java index 50a099bae..8559680ea 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/DrawBuffer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/DrawBuffer.java @@ -2,12 +2,10 @@ package com.jozufozu.flywheel.backend.instancing.batching; import java.nio.ByteBuffer; -import org.lwjgl.system.MemoryUtil; - import com.jozufozu.flywheel.api.vertex.ReusableVertexList; import com.jozufozu.flywheel.api.vertex.VertexListProvider; +import com.jozufozu.flywheel.backend.memory.MemoryBlock; import com.jozufozu.flywheel.core.vertex.VertexListProviderRegistry; -import com.mojang.blaze3d.platform.MemoryTracker; import com.mojang.blaze3d.vertex.VertexFormat; import net.minecraft.client.renderer.RenderType; @@ -23,9 +21,10 @@ public class DrawBuffer { private final int stride; private final VertexListProvider provider; - private ByteBuffer backingBuffer; + private MemoryBlock memory; + private ByteBuffer buffer; + private int expectedVertices; - private long ptr; public DrawBuffer(RenderType parent) { this.parent = parent; @@ -41,7 +40,7 @@ public class DrawBuffer { */ public void prepare(int vertexCount) { if (expectedVertices != 0) { - throw new IllegalStateException("Already drawing"); + throw new IllegalStateException("Already drawing!"); } this.expectedVertices = vertexCount; @@ -51,20 +50,20 @@ public class DrawBuffer { // is called and reallocates the buffer if there is not space for one more vertex. int byteSize = stride * (vertexCount + 1); - if (backingBuffer == null) { - backingBuffer = MemoryTracker.create(byteSize); - } else if (byteSize > backingBuffer.capacity()) { - backingBuffer = MemoryTracker.resize(backingBuffer, byteSize); + if (memory == null) { + memory = MemoryBlock.malloc(byteSize); + buffer = memory.asBuffer(); + } else if (byteSize > memory.size()) { + memory = memory.realloc(byteSize); + buffer = memory.asBuffer(); } - backingBuffer.clear(); - ptr = MemoryUtil.memAddress(backingBuffer); - MemoryUtil.memSet(ptr, 0, byteSize); + memory.clear(); } public ReusableVertexList slice(int startVertex, int vertexCount) { ReusableVertexList vertexList = provider.createVertexList(); - vertexList.ptr(ptr + startVertex * stride); + vertexList.ptr(memory.ptr() + startVertex * stride); vertexList.setVertexCount(vertexCount); return vertexList; } @@ -74,7 +73,8 @@ public class DrawBuffer { * @param bufferBuilder The buffer builder to inject into. */ public void inject(BufferBuilderExtension bufferBuilder) { - bufferBuilder.flywheel$injectForRender(backingBuffer, format, expectedVertices); + buffer.clear(); + bufferBuilder.flywheel$injectForRender(buffer, format, expectedVertices); } public int getVertexCount() { @@ -96,4 +96,9 @@ public class DrawBuffer { public void reset() { this.expectedVertices = 0; } + + public void free() { + buffer = null; + memory.free(); + } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/TransformSet.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/TransformSet.java index 4dc4a1bef..8b27d389d 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/TransformSet.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/TransformSet.java @@ -5,7 +5,6 @@ import java.util.List; import com.jozufozu.flywheel.api.instancer.InstancedPart; import com.jozufozu.flywheel.api.material.Material; import com.jozufozu.flywheel.api.struct.StructType; -import com.jozufozu.flywheel.api.struct.StructType.VertexTransformer; import com.jozufozu.flywheel.api.vertex.MutableVertexList; import com.jozufozu.flywheel.api.vertex.ReusableVertexList; import com.jozufozu.flywheel.backend.instancing.TaskEngine; @@ -71,11 +70,10 @@ public class TransformSet { int meshVertexCount = mesh.getVertexCount(); vertexList.setVertexCount(meshVertexCount); - @SuppressWarnings("unchecked") - StructType.VertexTransformer structVertexTransformer = (VertexTransformer) instancer.type.getVertexTransformer(); + StructType.VertexTransformer structVertexTransformer = instancer.type.getVertexTransformer(); for (D d : list) { - mesh.writeInto(vertexList); + mesh.write(vertexList); structVertexTransformer.transform(vertexList, d, level); diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/blockentity/BlockEntityInstance.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/blockentity/BlockEntityInstance.java index 39a53e444..add54b13b 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/blockentity/BlockEntityInstance.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/blockentity/BlockEntityInstance.java @@ -10,8 +10,8 @@ import com.jozufozu.flywheel.api.instancer.InstancerFactory; import com.jozufozu.flywheel.api.instancer.InstancerManager; import com.jozufozu.flywheel.backend.instancing.AbstractInstance; import com.jozufozu.flywheel.core.structs.StructTypes; -import com.jozufozu.flywheel.core.structs.model.TransformedPart; import com.jozufozu.flywheel.core.structs.oriented.OrientedPart; +import com.jozufozu.flywheel.core.structs.transformed.TransformedPart; import com.jozufozu.flywheel.util.box.GridAlignedBB; import com.jozufozu.flywheel.util.box.ImmutableBox; 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 759afd2c0..95015766a 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,6 +3,8 @@ 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; @@ -89,20 +91,14 @@ public class GPUInstancer extends AbstractInstancer buf.clear(clearStart, clearLength); if (size > 0) { + final long ptr = MemoryUtil.memAddress(buf.unwrap()); + final int stride = structType.getLayout().getStride(); + final StructWriter writer = structType.getWriter(); - final StructWriter writer = structType.getWriter(buf.unwrap()); - - boolean sequential = true; for (int i = 0; i < size; i++) { final D element = data.get(i); if (element.checkDirtyAndClear()) { - if (!sequential) { - writer.seek(i); - } - writer.write(element); - sequential = true; - } else { - sequential = false; + writer.write(ptr + i * stride, element); } } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/MeshPool.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/MeshPool.java index 39a0b4a48..a5d7b6051 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/MeshPool.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/MeshPool.java @@ -10,6 +10,7 @@ import java.util.Set; import org.jetbrains.annotations.Nullable; import org.lwjgl.opengl.GL32; +import org.lwjgl.system.MemoryUtil; import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.api.vertex.VertexType; @@ -240,7 +241,9 @@ public class MeshPool { } private void buffer(ByteBuffer buffer) { - this.mesh.writeInto(buffer, this.byteIndex); + buffer.position((int) this.byteIndex); + long ptr = MemoryUtil.memAddress(buffer); + this.mesh.write(ptr); this.boundTo.clear(); this.gpuResident = true; diff --git a/src/main/java/com/jozufozu/flywheel/backend/memory/FlwMemoryTracker.java b/src/main/java/com/jozufozu/flywheel/backend/memory/FlwMemoryTracker.java new file mode 100644 index 000000000..c398eb223 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/memory/FlwMemoryTracker.java @@ -0,0 +1,141 @@ +package com.jozufozu.flywheel.backend.memory; + +import java.lang.ref.Cleaner; +import java.nio.ByteBuffer; + +import org.lwjgl.system.MemoryUtil; + +public class FlwMemoryTracker { + private static final Cleaner CLEANER = Cleaner.create(); + + private static long cpuMemory = 0; + private static long gpuMemory = 0; + + public static long malloc(long size) { + long ptr = MemoryUtil.nmemAlloc(size); + if (ptr == MemoryUtil.NULL) { + throw new OutOfMemoryError("Failed to allocate " + size + " bytes"); + } + return ptr; + } + + public static MemoryBlock mallocBlock(long size) { + MemoryBlock block = new MemoryBlockImpl(malloc(size), size); + cpuMemory += block.size(); + return block; + } + + public static MemoryBlock mallocBlockTracked(long size) { + TrackedMemoryBlockImpl block = new TrackedMemoryBlockImpl(malloc(size), size); + block.cleanable = CLEANER.register(block, () -> { + if (!block.isFreed()) { + block.free(); + } + }); + cpuMemory += block.size(); + return block; + } + + @Deprecated + public static ByteBuffer mallocBuffer(int size) { + ByteBuffer buffer = MemoryUtil.memByteBuffer(malloc(size), size); + cpuMemory += buffer.capacity(); + return buffer; + } + + public static long calloc(long num, long size) { + long ptr = MemoryUtil.nmemCalloc(num, size); + if (ptr == MemoryUtil.NULL) { + throw new OutOfMemoryError("Failed to allocate " + num + " blocks of size " + size + " bytes"); + } + return ptr; + } + + public static MemoryBlock callocBlock(long num, long size) { + MemoryBlock block = new MemoryBlockImpl(calloc(num, size), num * size); + cpuMemory += block.size(); + return block; + } + + public static MemoryBlock callocBlockTracked(long num, long size) { + TrackedMemoryBlockImpl block = new TrackedMemoryBlockImpl(calloc(num, size), num * size); + block.cleanable = CLEANER.register(block, () -> { + if (!block.isFreed()) { + block.free(); + } + }); + cpuMemory += block.size(); + return block; + } + + public static long realloc(long ptr, long size) { + ptr = MemoryUtil.nmemRealloc(ptr, size); + if (ptr == MemoryUtil.NULL) { + throw new OutOfMemoryError("Failed to reallocate " + size + " bytes for address 0x" + Long.toHexString(ptr)); + } + return ptr; + } + + public static MemoryBlock reallocBlock(MemoryBlock block, long size) { + MemoryBlock newBlock = new MemoryBlockImpl(realloc(block.ptr(), size), size); + cpuMemory += -block.size() + newBlock.size(); + return newBlock; + } + + public static MemoryBlock reallocBlockTracked(MemoryBlock block, long size) { + TrackedMemoryBlockImpl newBlock = new TrackedMemoryBlockImpl(realloc(block.ptr(), size), size); + newBlock.cleanable = CLEANER.register(newBlock, () -> { + if (!newBlock.isFreed()) { + newBlock.free(); + } + }); + cpuMemory += -block.size() + newBlock.size(); + return newBlock; + } + + @Deprecated + public static ByteBuffer reallocBuffer(ByteBuffer buffer, int size) { + ByteBuffer newBuffer = MemoryUtil.memByteBuffer(realloc(MemoryUtil.memAddress(buffer), size), size); + cpuMemory += -buffer.capacity() + newBuffer.capacity(); + return newBuffer; + } + + public static void free(long ptr) { + MemoryUtil.nmemFree(ptr); + } + + public static void freeBlock(MemoryBlock block) { + free(block.ptr()); + cpuMemory -= block.size(); + } + + @Deprecated + public static void freeBuffer(ByteBuffer buffer) { + free(MemoryUtil.memAddress(buffer)); + cpuMemory -= buffer.capacity(); + } + + public static void _allocCPUMemory(long size) { + cpuMemory += size; + } + + public static void _freeCPUMemory(long size) { + cpuMemory -= size; + } + + public static void _allocGPUMemory(long size) { + gpuMemory += size; + } + + public static void _freeGPUMemory(long size) { + gpuMemory -= size; + } + + public static long getCPUMemory() { + return cpuMemory; + } + + public static long getGPUMemory() { + return gpuMemory; + } +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/memory/MemoryBlock.java b/src/main/java/com/jozufozu/flywheel/backend/memory/MemoryBlock.java new file mode 100644 index 000000000..c78b8c193 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/memory/MemoryBlock.java @@ -0,0 +1,45 @@ +package com.jozufozu.flywheel.backend.memory; + +import java.nio.ByteBuffer; + +public sealed interface MemoryBlock permits MemoryBlockImpl { + long ptr(); + + long size(); + + boolean isFreed(); + + boolean isTracked(); + + void copyTo(long ptr, long bytes); + + default void copyTo(long ptr) { + copyTo(ptr, size()); + } + + void clear(); + + ByteBuffer asBuffer(); + + MemoryBlock realloc(long size); + + MemoryBlock reallocTracked(long size); + + void free(); + + static MemoryBlock malloc(long size) { + return FlwMemoryTracker.mallocBlock(size); + } + + static MemoryBlock calloc(long num, long size) { + return FlwMemoryTracker.callocBlock(num, size); + } + + static MemoryBlock mallocTracked(long size) { + return FlwMemoryTracker.mallocBlockTracked(size); + } + + static MemoryBlock callocTracked(long num, long size) { + return FlwMemoryTracker.callocBlockTracked(num, size); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/memory/MemoryBlockImpl.java b/src/main/java/com/jozufozu/flywheel/backend/memory/MemoryBlockImpl.java new file mode 100644 index 000000000..1b6d984a6 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/memory/MemoryBlockImpl.java @@ -0,0 +1,76 @@ +package com.jozufozu.flywheel.backend.memory; + +import java.nio.ByteBuffer; + +import org.lwjgl.system.MemoryUtil; + +sealed class MemoryBlockImpl implements MemoryBlock permits TrackedMemoryBlockImpl { + final long ptr; + final long size; + + boolean freed; + + MemoryBlockImpl(long ptr, long size) { + this.ptr = ptr; + this.size = size; + } + + @Override + public long ptr() { + return ptr; + } + + @Override + public long size() { + return size; + } + + @Override + public boolean isFreed() { + return freed; + } + + @Override + public boolean isTracked() { + return false; + } + + @Override + public void copyTo(long ptr, long bytes) { + MemoryUtil.memCopy(this.ptr, ptr, bytes); + } + + @Override + public void clear() { + MemoryUtil.memSet(ptr, 0, size); + } + + @Override + public ByteBuffer asBuffer() { + int intSize = (int) size; + if (intSize != size) { + throw new UnsupportedOperationException("Cannot create buffer with long capacity!"); + } + return MemoryUtil.memByteBuffer(ptr, intSize); + } + + @Override + public MemoryBlock realloc(long size) { + MemoryBlock block = FlwMemoryTracker.reallocBlock(this, size); + freed = true; + return block; + } + + @Override + public MemoryBlock reallocTracked(long size) { + MemoryBlock block = FlwMemoryTracker.reallocBlockTracked(this, size); + freed = true; + return block; + } + + @Override + public void free() { + FlwMemoryTracker.freeBlock(this); + freed = true; + } +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/memory/TrackedMemoryBlockImpl.java b/src/main/java/com/jozufozu/flywheel/backend/memory/TrackedMemoryBlockImpl.java new file mode 100644 index 000000000..40b1a15eb --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/memory/TrackedMemoryBlockImpl.java @@ -0,0 +1,38 @@ +package com.jozufozu.flywheel.backend.memory; + +import java.lang.ref.Cleaner; + +final class TrackedMemoryBlockImpl extends MemoryBlockImpl { + Cleaner.Cleanable cleanable; + + TrackedMemoryBlockImpl(long ptr, long size) { + super(ptr, size); + } + + @Override + public boolean isTracked() { + return true; + } + + @Override + public MemoryBlock realloc(long size) { + MemoryBlock block = FlwMemoryTracker.reallocBlock(this, size); + freed = true; + cleanable.clean(); + return block; + } + + @Override + public MemoryBlock reallocTracked(long size) { + MemoryBlock block = FlwMemoryTracker.reallocBlockTracked(this, size); + freed = true; + cleanable.clean(); + return block; + } + + @Override + public void free() { + cleanable.clean(); + freed = true; + } +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/struct/BufferWriter.java b/src/main/java/com/jozufozu/flywheel/backend/struct/BufferWriter.java deleted file mode 100644 index 49ce4969d..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/struct/BufferWriter.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.jozufozu.flywheel.backend.struct; - -import java.nio.ByteBuffer; - -import com.jozufozu.flywheel.api.instancer.InstancedPart; -import com.jozufozu.flywheel.api.struct.StructType; -import com.jozufozu.flywheel.api.struct.StructWriter; - -public abstract class BufferWriter implements StructWriter { - protected final ByteBuffer backingBuffer; - - protected final int stride; - - protected BufferWriter(StructType structType, ByteBuffer byteBuffer) { - this.backingBuffer = byteBuffer; - - this.stride = structType.getLayout().getStride(); - } - - @Override - public final void write(S struct) { - writeInternal(struct); - advance(); - } - - /** - * Advances the write pointer forward by the stride of one vertex. - * This will always be called after a struct is written, implementors need not call it themselves. - * - * @see #write - */ - protected abstract void advance(); - - protected abstract void writeInternal(S s); - - @Override - public void seek(int pos) { - backingBuffer.position(pos * stride); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/struct/UnsafeBufferWriter.java b/src/main/java/com/jozufozu/flywheel/backend/struct/UnsafeBufferWriter.java deleted file mode 100644 index 7aa852de1..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/struct/UnsafeBufferWriter.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.jozufozu.flywheel.backend.struct; - -import java.nio.ByteBuffer; - -import org.lwjgl.system.MemoryUtil; - -import com.jozufozu.flywheel.api.instancer.InstancedPart; -import com.jozufozu.flywheel.api.struct.StructType; - -/** - * This class copied/adapted from jellysquid's - * - * An unsafe {@link BufferWriter} implementation which uses direct memory operations to enable fast blitting of - * data into memory buffers. Only available on JVMs which support {@link sun.misc.Unsafe}, but generally produces much - * better optimized code than other implementations. The implementation does not check for invalid memory accesses, - * meaning that errors can corrupt process memory. - */ -public abstract class UnsafeBufferWriter extends BufferWriter { - /** - * The write pointer into the buffer storage. This is advanced by the stride every time - * {@link UnsafeBufferWriter#advance()} is called. - */ - protected long writePointer; - - protected UnsafeBufferWriter(StructType structType, ByteBuffer byteBuffer) { - super(structType, byteBuffer); - - acquireWritePointer(); - } - - @Override - public void seek(int pos) { - super.seek(pos); - acquireWritePointer(); - } - - @Override - protected void advance() { - this.writePointer += this.stride; - } - - private void acquireWritePointer() { - this.writePointer = MemoryUtil.memAddress(this.backingBuffer, this.backingBuffer.position()); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/struct/package-info.java b/src/main/java/com/jozufozu/flywheel/backend/struct/package-info.java deleted file mode 100644 index d4cbaadf2..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/struct/package-info.java +++ /dev/null @@ -1,6 +0,0 @@ -@ParametersAreNonnullByDefault @MethodsReturnNonnullByDefault -package com.jozufozu.flywheel.backend.struct; - -import javax.annotation.ParametersAreNonnullByDefault; - -import net.minecraft.MethodsReturnNonnullByDefault; diff --git a/src/main/java/com/jozufozu/flywheel/core/hardcoded/ModelPart.java b/src/main/java/com/jozufozu/flywheel/core/hardcoded/ModelPart.java index 7369df882..4dc301213 100644 --- a/src/main/java/com/jozufozu/flywheel/core/hardcoded/ModelPart.java +++ b/src/main/java/com/jozufozu/flywheel/core/hardcoded/ModelPart.java @@ -1,20 +1,17 @@ package com.jozufozu.flywheel.core.hardcoded; -import java.nio.ByteBuffer; import java.util.List; -import org.lwjgl.system.MemoryUtil; - import com.jozufozu.flywheel.api.vertex.MutableVertexList; import com.jozufozu.flywheel.api.vertex.ReusableVertexList; +import com.jozufozu.flywheel.backend.memory.MemoryBlock; import com.jozufozu.flywheel.core.model.Mesh; import com.jozufozu.flywheel.core.vertex.Formats; import com.jozufozu.flywheel.core.vertex.PosTexNormalVertex; -import com.mojang.blaze3d.platform.MemoryTracker; public class ModelPart implements Mesh { private final int vertexCount; - private final ByteBuffer contents; + private final MemoryBlock contents; private final ReusableVertexList vertexList; private final String name; @@ -29,8 +26,8 @@ public class ModelPart implements Mesh { this.vertexCount = vertices; } - contents = MemoryTracker.create(size()); - long ptr = MemoryUtil.memAddress(contents); + contents = MemoryBlock.malloc(size()); + long ptr = contents.ptr(); VertexWriter writer = new VertexWriterImpl(ptr); for (PartBuilder.CuboidBuilder cuboid : cuboids) { cuboid.write(writer); @@ -56,19 +53,18 @@ public class ModelPart implements Mesh { } @Override - public void writeInto(ByteBuffer buffer, long byteIndex) { - buffer.position((int) byteIndex); - MemoryUtil.memCopy(contents, buffer); + public void write(long ptr) { + contents.copyTo(ptr); } @Override - public void writeInto(MutableVertexList dst) { + public void write(MutableVertexList dst) { vertexList.writeAll(dst); } @Override public void close() { - MemoryUtil.memFree(contents); + contents.free(); } @Override diff --git a/src/main/java/com/jozufozu/flywheel/core/model/Mesh.java b/src/main/java/com/jozufozu/flywheel/core/model/Mesh.java index f09c5ec58..a1a4f10c2 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/Mesh.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/Mesh.java @@ -1,7 +1,5 @@ package com.jozufozu.flywheel.core.model; -import java.nio.ByteBuffer; - import com.jozufozu.flywheel.api.vertex.MutableVertexList; import com.jozufozu.flywheel.api.vertex.VertexType; import com.jozufozu.flywheel.backend.instancing.instancing.ElementBuffer; @@ -51,9 +49,9 @@ public interface Mesh { return getVertexType().byteOffset(getVertexCount()); } - void writeInto(ByteBuffer buffer, long byteIndex); + void write(long ptr); - void writeInto(MutableVertexList vertexList); + void write(MutableVertexList vertexList); /** * Create an element buffer object that indexes the vertices of this mesh. diff --git a/src/main/java/com/jozufozu/flywheel/core/model/ModelUtil.java b/src/main/java/com/jozufozu/flywheel/core/model/ModelUtil.java index dfcd1435a..9a68168ff 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/ModelUtil.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/ModelUtil.java @@ -10,10 +10,10 @@ import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.api.material.Material; import com.jozufozu.flywheel.api.vertex.ReusableVertexList; import com.jozufozu.flywheel.api.vertex.VertexType; +import com.jozufozu.flywheel.backend.memory.MemoryBlock; import com.jozufozu.flywheel.core.Materials; import com.jozufozu.flywheel.core.vertex.Formats; import com.jozufozu.flywheel.core.vertex.VertexListProviderRegistry; -import com.mojang.blaze3d.platform.MemoryTracker; import com.mojang.blaze3d.vertex.BufferBuilder.DrawState; import com.mojang.blaze3d.vertex.VertexFormat; import com.mojang.datafixers.util.Pair; @@ -47,16 +47,16 @@ public class ModelUtil { return dispatcher; } - public static Pair convertBlockBuffer(Pair pair) { + public static Pair convertBlockBuffer(Pair pair) { DrawState drawState = pair.getFirst(); int vertexCount = drawState.vertexCount(); VertexFormat srcFormat = drawState.format(); VertexType dstVertexType = Formats.BLOCK; ByteBuffer src = pair.getSecond(); - ByteBuffer dst = MemoryTracker.create(src.capacity()); + MemoryBlock dst = MemoryBlock.malloc(src.capacity()); long srcPtr = MemoryUtil.memAddress(src); - long dstPtr = MemoryUtil.memAddress(dst); + long dstPtr = dst.ptr(); ReusableVertexList srcList = VertexListProviderRegistry.getOrInfer(srcFormat).createVertexList(); ReusableVertexList dstList = dstVertexType.createVertexList(); diff --git a/src/main/java/com/jozufozu/flywheel/core/model/SimpleMesh.java b/src/main/java/com/jozufozu/flywheel/core/model/SimpleMesh.java index f95423005..d184a05d6 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/SimpleMesh.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/SimpleMesh.java @@ -1,35 +1,31 @@ package com.jozufozu.flywheel.core.model; -import java.nio.ByteBuffer; - -import org.lwjgl.system.MemoryUtil; - import com.jozufozu.flywheel.api.vertex.MutableVertexList; import com.jozufozu.flywheel.api.vertex.ReusableVertexList; import com.jozufozu.flywheel.api.vertex.VertexType; +import com.jozufozu.flywheel.backend.memory.MemoryBlock; public class SimpleMesh implements Mesh { private final VertexType vertexType; private final int vertexCount; - private final ByteBuffer contents; + private final MemoryBlock contents; private final ReusableVertexList vertexList; private final String name; - public SimpleMesh(VertexType vertexType, ByteBuffer contents, String name) { + public SimpleMesh(VertexType vertexType, MemoryBlock contents, String name) { this.vertexType = vertexType; this.contents = contents; this.name = name; - contents.clear(); - int bytes = contents.remaining(); + int bytes = (int) contents.size(); int stride = vertexType.getStride(); if (bytes % stride != 0) { - throw new IllegalArgumentException("Buffer contains non-whole amount of vertices!"); + throw new IllegalArgumentException("MemoryBlock contains non-whole amount of vertices!"); } vertexCount = bytes / stride; vertexList = getVertexType().createVertexList(); - vertexList.ptr(MemoryUtil.memAddress(contents)); + vertexList.ptr(contents.ptr()); vertexList.setVertexCount(vertexCount); } @@ -44,19 +40,18 @@ public class SimpleMesh implements Mesh { } @Override - public void writeInto(ByteBuffer buffer, long byteIndex) { - buffer.position((int) byteIndex); - MemoryUtil.memCopy(contents, buffer); + public void write(long ptr) { + contents.copyTo(ptr); } @Override - public void writeInto(MutableVertexList dst) { + public void write(MutableVertexList dst) { vertexList.writeAll(dst); } @Override public void close() { - MemoryUtil.memFree(contents); + contents.free(); } @Override diff --git a/src/main/java/com/jozufozu/flywheel/core/model/buffering/BakedModelBuilder.java b/src/main/java/com/jozufozu/flywheel/core/model/buffering/BakedModelBuilder.java index d7c138d00..e0ca37b16 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/buffering/BakedModelBuilder.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/buffering/BakedModelBuilder.java @@ -1,11 +1,11 @@ package com.jozufozu.flywheel.core.model.buffering; -import java.nio.ByteBuffer; import java.util.function.BiFunction; import com.google.common.collect.ImmutableMap; import com.jozufozu.flywheel.api.material.Material; import com.jozufozu.flywheel.api.vertex.VertexType; +import com.jozufozu.flywheel.backend.memory.MemoryBlock; import com.jozufozu.flywheel.core.model.Mesh; import com.jozufozu.flywheel.core.model.ModelUtil; import com.jozufozu.flywheel.core.model.SimpleMesh; @@ -104,7 +104,7 @@ public class BakedModelBuilder { buffer.end(); Material material = materialFunc.apply(renderType, shaded); if (material != null) { - Pair pair = ModelUtil.convertBlockBuffer(buffer.popNextBuffer()); + Pair pair = ModelUtil.convertBlockBuffer(buffer.popNextBuffer()); meshMapBuilder.put(material, new SimpleMesh(pair.getFirst(), pair.getSecond(), "bakedModel=" + bakedModel.toString() + ",renderType=" + renderType.toString() + ",shaded=" + shaded)); } }; @@ -119,7 +119,7 @@ public class BakedModelBuilder { buffer.end(); Material material = materialFunc.apply(renderType, false); if (material != null) { - Pair pair = ModelUtil.convertBlockBuffer(buffer.popNextBuffer()); + Pair pair = ModelUtil.convertBlockBuffer(buffer.popNextBuffer()); meshMapBuilder.put(material, new SimpleMesh(pair.getFirst(), pair.getSecond(), "bakedModel=" + bakedModel.toString() + ",renderType=" + renderType.toString())); } }; diff --git a/src/main/java/com/jozufozu/flywheel/core/model/buffering/BlockModelBuilder.java b/src/main/java/com/jozufozu/flywheel/core/model/buffering/BlockModelBuilder.java index f5d0c485e..84686aa74 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/buffering/BlockModelBuilder.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/buffering/BlockModelBuilder.java @@ -1,11 +1,11 @@ package com.jozufozu.flywheel.core.model.buffering; -import java.nio.ByteBuffer; import java.util.function.BiFunction; import com.google.common.collect.ImmutableMap; import com.jozufozu.flywheel.api.material.Material; import com.jozufozu.flywheel.api.vertex.VertexType; +import com.jozufozu.flywheel.backend.memory.MemoryBlock; import com.jozufozu.flywheel.core.model.Mesh; import com.jozufozu.flywheel.core.model.ModelUtil; import com.jozufozu.flywheel.core.model.SimpleMesh; @@ -93,7 +93,7 @@ public class BlockModelBuilder { buffer.end(); Material material = materialFunc.apply(renderType, shaded); if (material != null) { - Pair pair = ModelUtil.convertBlockBuffer(buffer.popNextBuffer()); + Pair pair = ModelUtil.convertBlockBuffer(buffer.popNextBuffer()); meshMapBuilder.put(material, new SimpleMesh(pair.getFirst(), pair.getSecond(), "state=" + state.toString() + ",renderType=" + renderType.toString() + ",shaded=" + shaded)); } }; @@ -108,7 +108,7 @@ public class BlockModelBuilder { buffer.end(); Material material = materialFunc.apply(renderType, false); if (material != null) { - Pair pair = ModelUtil.convertBlockBuffer(buffer.popNextBuffer()); + Pair pair = ModelUtil.convertBlockBuffer(buffer.popNextBuffer()); meshMapBuilder.put(material, new SimpleMesh(pair.getFirst(), pair.getSecond(), "state=" + state.toString() + ",renderType=" + renderType.toString())); } }; diff --git a/src/main/java/com/jozufozu/flywheel/core/model/buffering/MultiBlockModelBuilder.java b/src/main/java/com/jozufozu/flywheel/core/model/buffering/MultiBlockModelBuilder.java index c951acfbd..b329a46c6 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/buffering/MultiBlockModelBuilder.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/buffering/MultiBlockModelBuilder.java @@ -1,6 +1,5 @@ package com.jozufozu.flywheel.core.model.buffering; -import java.nio.ByteBuffer; import java.util.Collection; import java.util.Collections; import java.util.Map; @@ -9,6 +8,7 @@ import java.util.function.BiFunction; import com.google.common.collect.ImmutableMap; import com.jozufozu.flywheel.api.material.Material; import com.jozufozu.flywheel.api.vertex.VertexType; +import com.jozufozu.flywheel.backend.memory.MemoryBlock; import com.jozufozu.flywheel.core.model.Mesh; import com.jozufozu.flywheel.core.model.ModelUtil; import com.jozufozu.flywheel.core.model.SimpleMesh; @@ -100,7 +100,7 @@ public class MultiBlockModelBuilder { buffer.end(); Material material = materialFunc.apply(renderType, shaded); if (material != null) { - Pair pair = ModelUtil.convertBlockBuffer(buffer.popNextBuffer()); + Pair pair = ModelUtil.convertBlockBuffer(buffer.popNextBuffer()); meshMapBuilder.put(material, new SimpleMesh(pair.getFirst(), pair.getSecond(), "renderType=" + renderType.toString() + ",shaded=" + shaded)); } }; @@ -115,7 +115,7 @@ public class MultiBlockModelBuilder { buffer.end(); Material material = materialFunc.apply(renderType, false); if (material != null) { - Pair pair = ModelUtil.convertBlockBuffer(buffer.popNextBuffer()); + Pair pair = ModelUtil.convertBlockBuffer(buffer.popNextBuffer()); meshMapBuilder.put(material, new SimpleMesh(pair.getFirst(), pair.getSecond(), "renderType=" + renderType.toString())); } }; diff --git a/src/main/java/com/jozufozu/flywheel/core/structs/ColoredLitWriter.java b/src/main/java/com/jozufozu/flywheel/core/structs/ColoredLitWriter.java new file mode 100644 index 000000000..dcd23aca0 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/core/structs/ColoredLitWriter.java @@ -0,0 +1,17 @@ +package com.jozufozu.flywheel.core.structs; + +import org.lwjgl.system.MemoryUtil; + +import com.jozufozu.flywheel.api.struct.StructWriter; + +public abstract class ColoredLitWriter implements StructWriter { + @Override + public void write(long ptr, D d) { + MemoryUtil.memPutByte(ptr, d.blockLight); + MemoryUtil.memPutByte(ptr + 1, d.skyLight); + MemoryUtil.memPutByte(ptr + 2, d.r); + MemoryUtil.memPutByte(ptr + 3, d.g); + MemoryUtil.memPutByte(ptr + 4, d.b); + MemoryUtil.memPutByte(ptr + 5, d.a); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/core/structs/ColoredLitWriterUnsafe.java b/src/main/java/com/jozufozu/flywheel/core/structs/ColoredLitWriterUnsafe.java deleted file mode 100644 index 985fc7046..000000000 --- a/src/main/java/com/jozufozu/flywheel/core/structs/ColoredLitWriterUnsafe.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.jozufozu.flywheel.core.structs; - -import java.nio.ByteBuffer; - -import org.lwjgl.system.MemoryUtil; - -import com.jozufozu.flywheel.api.struct.StructType; -import com.jozufozu.flywheel.backend.struct.UnsafeBufferWriter; - -public abstract class ColoredLitWriterUnsafe extends UnsafeBufferWriter { - - public ColoredLitWriterUnsafe(StructType structType, ByteBuffer byteBuffer) { - super(structType, byteBuffer); - } - - @Override - protected void writeInternal(D d) { - long ptr = writePointer; - MemoryUtil.memPutByte(ptr, d.blockLight); - MemoryUtil.memPutByte(ptr + 1, d.skyLight); - MemoryUtil.memPutByte(ptr + 2, d.r); - MemoryUtil.memPutByte(ptr + 3, d.g); - MemoryUtil.memPutByte(ptr + 4, d.b); - MemoryUtil.memPutByte(ptr + 5, d.a); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/core/structs/StructTypes.java b/src/main/java/com/jozufozu/flywheel/core/structs/StructTypes.java index 96015bf27..597a2a43c 100644 --- a/src/main/java/com/jozufozu/flywheel/core/structs/StructTypes.java +++ b/src/main/java/com/jozufozu/flywheel/core/structs/StructTypes.java @@ -2,10 +2,10 @@ package com.jozufozu.flywheel.core.structs; import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.core.ComponentRegistry; -import com.jozufozu.flywheel.core.structs.model.TransformedPart; -import com.jozufozu.flywheel.core.structs.model.TransformedType; import com.jozufozu.flywheel.core.structs.oriented.OrientedPart; import com.jozufozu.flywheel.core.structs.oriented.OrientedType; +import com.jozufozu.flywheel.core.structs.transformed.TransformedPart; +import com.jozufozu.flywheel.core.structs.transformed.TransformedType; public class StructTypes { public static final StructType TRANSFORMED = ComponentRegistry.register(new TransformedType()); diff --git a/src/main/java/com/jozufozu/flywheel/core/structs/model/TransformedWriterUnsafe.java b/src/main/java/com/jozufozu/flywheel/core/structs/model/TransformedWriterUnsafe.java deleted file mode 100644 index 8634e0db7..000000000 --- a/src/main/java/com/jozufozu/flywheel/core/structs/model/TransformedWriterUnsafe.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.jozufozu.flywheel.core.structs.model; - -import java.nio.ByteBuffer; - -import com.jozufozu.flywheel.api.struct.StructType; -import com.jozufozu.flywheel.core.structs.ColoredLitWriterUnsafe; -import com.jozufozu.flywheel.util.MatrixWrite; - -public class TransformedWriterUnsafe extends ColoredLitWriterUnsafe { - - public TransformedWriterUnsafe(StructType structType, ByteBuffer byteBuffer) { - super(structType, byteBuffer); - } - - @Override - protected void writeInternal(TransformedPart d) { - super.writeInternal(d); - long ptr = writePointer + 6; - - ((MatrixWrite) (Object) d.model).flywheel$writeUnsafe(ptr); - ((MatrixWrite) (Object) d.normal).flywheel$writeUnsafe(ptr + 4 * 16); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/core/structs/oriented/OrientedType.java b/src/main/java/com/jozufozu/flywheel/core/structs/oriented/OrientedType.java index 93a0226ef..66f2653f5 100644 --- a/src/main/java/com/jozufozu/flywheel/core/structs/oriented/OrientedType.java +++ b/src/main/java/com/jozufozu/flywheel/core/structs/oriented/OrientedType.java @@ -1,7 +1,5 @@ package com.jozufozu.flywheel.core.structs.oriented; -import java.nio.ByteBuffer; - import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.api.struct.StructWriter; import com.jozufozu.flywheel.core.Components; @@ -32,8 +30,8 @@ public class OrientedType implements StructType { } @Override - public StructWriter getWriter(ByteBuffer backing) { - return new OrientedWriterUnsafe(this, backing); + public StructWriter getWriter() { + return OrientedWriter.INSTANCE; } @Override @@ -42,7 +40,7 @@ public class OrientedType implements StructType { } @Override - public VertexTransformer getVertexTransformer() { + public VertexTransformer getVertexTransformer() { return (vertexList, struct, level) -> { Vector4f pos = new Vector4f(); Vector3f normal = new Vector3f(); diff --git a/src/main/java/com/jozufozu/flywheel/core/structs/oriented/OrientedWriterUnsafe.java b/src/main/java/com/jozufozu/flywheel/core/structs/oriented/OrientedWriter.java similarity index 54% rename from src/main/java/com/jozufozu/flywheel/core/structs/oriented/OrientedWriterUnsafe.java rename to src/main/java/com/jozufozu/flywheel/core/structs/oriented/OrientedWriter.java index fb03c4d1e..8aa99cef4 100644 --- a/src/main/java/com/jozufozu/flywheel/core/structs/oriented/OrientedWriterUnsafe.java +++ b/src/main/java/com/jozufozu/flywheel/core/structs/oriented/OrientedWriter.java @@ -1,21 +1,15 @@ package com.jozufozu.flywheel.core.structs.oriented; -import java.nio.ByteBuffer; - import org.lwjgl.system.MemoryUtil; -import com.jozufozu.flywheel.api.struct.StructType; -import com.jozufozu.flywheel.core.structs.ColoredLitWriterUnsafe; +import com.jozufozu.flywheel.core.structs.ColoredLitWriter; -public class OrientedWriterUnsafe extends ColoredLitWriterUnsafe { - public OrientedWriterUnsafe(StructType structType, ByteBuffer byteBuffer) { - super(structType, byteBuffer); - } +public class OrientedWriter extends ColoredLitWriter { + public static final OrientedWriter INSTANCE = new OrientedWriter(); @Override - protected void writeInternal(OrientedPart d) { - long ptr = writePointer; - super.writeInternal(d); + public void write(long ptr, OrientedPart d) { + super.write(ptr, d); MemoryUtil.memPutFloat(ptr + 6, d.posX); MemoryUtil.memPutFloat(ptr + 10, d.posY); diff --git a/src/main/java/com/jozufozu/flywheel/core/structs/model/TransformedPart.java b/src/main/java/com/jozufozu/flywheel/core/structs/transformed/TransformedPart.java similarity index 97% rename from src/main/java/com/jozufozu/flywheel/core/structs/model/TransformedPart.java rename to src/main/java/com/jozufozu/flywheel/core/structs/transformed/TransformedPart.java index 865f5bccd..215bfd913 100644 --- a/src/main/java/com/jozufozu/flywheel/core/structs/model/TransformedPart.java +++ b/src/main/java/com/jozufozu/flywheel/core/structs/transformed/TransformedPart.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.core.structs.model; +package com.jozufozu.flywheel.core.structs.transformed; import com.jozufozu.flywheel.core.structs.ColoredLitPart; import com.jozufozu.flywheel.core.structs.StructTypes; diff --git a/src/main/java/com/jozufozu/flywheel/core/structs/model/TransformedType.java b/src/main/java/com/jozufozu/flywheel/core/structs/transformed/TransformedType.java similarity index 86% rename from src/main/java/com/jozufozu/flywheel/core/structs/model/TransformedType.java rename to src/main/java/com/jozufozu/flywheel/core/structs/transformed/TransformedType.java index 30d750672..8eb36eeb6 100644 --- a/src/main/java/com/jozufozu/flywheel/core/structs/model/TransformedType.java +++ b/src/main/java/com/jozufozu/flywheel/core/structs/transformed/TransformedType.java @@ -1,6 +1,4 @@ -package com.jozufozu.flywheel.core.structs.model; - -import java.nio.ByteBuffer; +package com.jozufozu.flywheel.core.structs.transformed; import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.api.struct.StructWriter; @@ -29,8 +27,8 @@ public class TransformedType implements StructType { } @Override - public StructWriter getWriter(ByteBuffer backing) { - return new TransformedWriterUnsafe(this, backing); + public StructWriter getWriter() { + return TransformedWriter.INSTANCE; } @Override @@ -39,7 +37,7 @@ public class TransformedType implements StructType { } @Override - public VertexTransformer getVertexTransformer() { + public VertexTransformer getVertexTransformer() { return (vertexList, struct, level) -> { Vector4f pos = new Vector4f(); Vector3f normal = new Vector3f(); diff --git a/src/main/java/com/jozufozu/flywheel/core/structs/transformed/TransformedWriter.java b/src/main/java/com/jozufozu/flywheel/core/structs/transformed/TransformedWriter.java new file mode 100644 index 000000000..eb910d6fb --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/core/structs/transformed/TransformedWriter.java @@ -0,0 +1,17 @@ +package com.jozufozu.flywheel.core.structs.transformed; + +import com.jozufozu.flywheel.core.structs.ColoredLitWriter; +import com.jozufozu.flywheel.util.MatrixWrite; + +public class TransformedWriter extends ColoredLitWriter { + public static final TransformedWriter INSTANCE = new TransformedWriter(); + + @Override + public void write(long ptr, TransformedPart d) { + super.write(ptr, d); + ptr += 6; + + ((MatrixWrite) (Object) d.model).flywheel$writeUnsafe(ptr); + ((MatrixWrite) (Object) d.normal).flywheel$writeUnsafe(ptr + 4 * 16); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/core/structs/model/package-info.java b/src/main/java/com/jozufozu/flywheel/core/structs/transformed/package-info.java similarity index 75% rename from src/main/java/com/jozufozu/flywheel/core/structs/model/package-info.java rename to src/main/java/com/jozufozu/flywheel/core/structs/transformed/package-info.java index 759c3a038..2ca2a5ed4 100644 --- a/src/main/java/com/jozufozu/flywheel/core/structs/model/package-info.java +++ b/src/main/java/com/jozufozu/flywheel/core/structs/transformed/package-info.java @@ -1,5 +1,5 @@ @ParametersAreNonnullByDefault @MethodsReturnNonnullByDefault -package com.jozufozu.flywheel.core.structs.model; +package com.jozufozu.flywheel.core.structs.transformed; import javax.annotation.ParametersAreNonnullByDefault; diff --git a/src/main/java/com/jozufozu/flywheel/core/uniform/FogProvider.java b/src/main/java/com/jozufozu/flywheel/core/uniform/FogProvider.java index 9a7586c41..9bea01220 100644 --- a/src/main/java/com/jozufozu/flywheel/core/uniform/FogProvider.java +++ b/src/main/java/com/jozufozu/flywheel/core/uniform/FogProvider.java @@ -9,8 +9,6 @@ import com.mojang.blaze3d.systems.RenderSystem; public class FogProvider extends UniformProvider { - - @Override public int getSize() { return 16 + 8 + 4; diff --git a/src/main/java/com/jozufozu/flywheel/core/uniform/UniformBuffer.java b/src/main/java/com/jozufozu/flywheel/core/uniform/UniformBuffer.java index 5f3b10bb7..068fbc4c0 100644 --- a/src/main/java/com/jozufozu/flywheel/core/uniform/UniformBuffer.java +++ b/src/main/java/com/jozufozu/flywheel/core/uniform/UniformBuffer.java @@ -12,9 +12,9 @@ import com.google.common.collect.ImmutableList; import com.jozufozu.flywheel.api.uniform.UniformProvider; import com.jozufozu.flywheel.backend.gl.buffer.GlBufferType; import com.jozufozu.flywheel.backend.gl.buffer.MappedGlBuffer; +import com.jozufozu.flywheel.backend.memory.FlwMemoryTracker; import com.jozufozu.flywheel.core.ComponentRegistry; import com.jozufozu.flywheel.util.RenderMath; -import com.mojang.blaze3d.platform.MemoryTracker; public class UniformBuffer { @@ -57,7 +57,7 @@ public class UniformBuffer { allocatedProviders = builder.build(); - data = MemoryTracker.create(totalBytes); + data = FlwMemoryTracker.mallocBuffer(totalBytes); changedBytes = new BitSet(totalBytes); for (Allocated p : allocatedProviders) { diff --git a/src/main/java/com/jozufozu/flywheel/event/ForgeEvents.java b/src/main/java/com/jozufozu/flywheel/event/ForgeEvents.java index 1310235f7..26bc79290 100644 --- a/src/main/java/com/jozufozu/flywheel/event/ForgeEvents.java +++ b/src/main/java/com/jozufozu/flywheel/event/ForgeEvents.java @@ -4,8 +4,8 @@ import java.util.ArrayList; import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.backend.Backend; -import com.jozufozu.flywheel.backend.FlywheelMemory; import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; +import com.jozufozu.flywheel.backend.memory.FlwMemoryTracker; import com.jozufozu.flywheel.light.LightUpdater; import com.jozufozu.flywheel.util.WorldAttached; @@ -24,10 +24,7 @@ public class ForgeEvents { InstancedRenderDispatcher.getDebugString(debug); - // TODO: compress into one line - debug.add("Memory used:"); - debug.add("GPU: " + FlywheelMemory.getGPUMemory()); - debug.add("CPU: " + FlywheelMemory.getCPUMemory()); + debug.add("Memory Usage: CPU: " + FlwMemoryTracker.getCPUMemory() / 1024 + "KiB GPU: " + FlwMemoryTracker.getGPUMemory() / 1024 + "KiB"); } } diff --git a/src/main/java/com/jozufozu/flywheel/light/LightVolume.java b/src/main/java/com/jozufozu/flywheel/light/LightVolume.java index 31245dea9..cf4abacb1 100644 --- a/src/main/java/com/jozufozu/flywheel/light/LightVolume.java +++ b/src/main/java/com/jozufozu/flywheel/light/LightVolume.java @@ -2,8 +2,7 @@ package com.jozufozu.flywheel.light; import java.nio.ByteBuffer; -import org.lwjgl.system.MemoryUtil; - +import com.jozufozu.flywheel.backend.memory.FlwMemoryTracker; import com.jozufozu.flywheel.util.box.GridAlignedBB; import com.jozufozu.flywheel.util.box.ImmutableBox; @@ -21,7 +20,7 @@ public class LightVolume implements ImmutableBox, LightListener { this.level = level; this.setBox(sampleVolume); - this.lightData = MemoryUtil.memAlloc(this.box.volume() * 2); + this.lightData = FlwMemoryTracker.mallocBuffer(this.box.volume() * 2); } protected void setBox(ImmutableBox box) { @@ -66,7 +65,7 @@ public class LightVolume implements ImmutableBox, LightListener { setBox(newSampleVolume); int neededCapacity = box.volume() * 2; if (neededCapacity > lightData.capacity()) { - lightData = MemoryUtil.memRealloc(lightData, neededCapacity); + lightData = FlwMemoryTracker.reallocBuffer(lightData, neededCapacity); } initialize(); } @@ -168,7 +167,7 @@ public class LightVolume implements ImmutableBox, LightListener { } public void delete() { - MemoryUtil.memFree(lightData); + FlwMemoryTracker.freeBuffer(lightData); lightData = null; } diff --git a/src/main/java/com/jozufozu/flywheel/util/StringUtil.java b/src/main/java/com/jozufozu/flywheel/util/StringUtil.java index 054bf0f43..4b7049b74 100644 --- a/src/main/java/com/jozufozu/flywheel/util/StringUtil.java +++ b/src/main/java/com/jozufozu/flywheel/util/StringUtil.java @@ -15,6 +15,8 @@ import java.util.stream.Collectors; import org.lwjgl.system.MemoryUtil; +import com.jozufozu.flywheel.backend.memory.FlwMemoryTracker; + public class StringUtil { private static final NumberFormat timeFormat = new DecimalFormat("#0.000"); @@ -56,11 +58,11 @@ public class StringUtil { ((Buffer) bytebuffer).rewind(); return MemoryUtil.memASCII(bytebuffer, i); } catch (IOException ignored) { + // } finally { if (bytebuffer != null) { - MemoryUtil.memFree(bytebuffer); + FlwMemoryTracker.freeBuffer(bytebuffer); } - } return null; @@ -75,12 +77,12 @@ public class StringUtil { } private static ByteBuffer readInputStream(InputStream is) throws IOException { - ByteBuffer bytebuffer = MemoryUtil.memAlloc(8192); + ByteBuffer bytebuffer = FlwMemoryTracker.mallocBuffer(8192); ReadableByteChannel readablebytechannel = Channels.newChannel(is); while (readablebytechannel.read(bytebuffer) != -1) { if (bytebuffer.remaining() == 0) { - bytebuffer = MemoryUtil.memRealloc(bytebuffer, bytebuffer.capacity() * 2); + bytebuffer = FlwMemoryTracker.reallocBuffer(bytebuffer, bytebuffer.capacity() * 2); } } return bytebuffer; @@ -88,7 +90,7 @@ public class StringUtil { private static ByteBuffer readFileInputStream(FileInputStream fileinputstream) throws IOException { FileChannel filechannel = fileinputstream.getChannel(); - ByteBuffer bytebuffer = MemoryUtil.memAlloc((int) filechannel.size() + 1); + ByteBuffer bytebuffer = FlwMemoryTracker.mallocBuffer((int) filechannel.size() + 1); while (filechannel.read(bytebuffer) != -1) { } diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java index 39e6650fb..0089766d2 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java @@ -15,8 +15,8 @@ import com.jozufozu.flywheel.core.Materials; import com.jozufozu.flywheel.core.hardcoded.ModelPart; import com.jozufozu.flywheel.core.model.SimpleLazyModel; import com.jozufozu.flywheel.core.structs.StructTypes; -import com.jozufozu.flywheel.core.structs.model.TransformedPart; import com.jozufozu.flywheel.core.structs.oriented.OrientedPart; +import com.jozufozu.flywheel.core.structs.transformed.TransformedPart; import com.jozufozu.flywheel.util.AnimationTickHolder; import com.mojang.math.Quaternion; import com.mojang.math.Vector3f; diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/MinecartInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/MinecartInstance.java index e12f6d37e..d1ae67501 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/MinecartInstance.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/MinecartInstance.java @@ -12,7 +12,7 @@ import com.jozufozu.flywheel.core.model.Mesh; import com.jozufozu.flywheel.core.model.Models; import com.jozufozu.flywheel.core.model.SimpleLazyModel; import com.jozufozu.flywheel.core.structs.StructTypes; -import com.jozufozu.flywheel.core.structs.model.TransformedPart; +import com.jozufozu.flywheel.core.structs.transformed.TransformedPart; import com.jozufozu.flywheel.util.AnimationTickHolder; import com.jozufozu.flywheel.util.transform.TransformStack; import com.mojang.blaze3d.vertex.PoseStack; diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxInstance.java index cc9aadb8a..cfe63f41d 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxInstance.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxInstance.java @@ -12,7 +12,7 @@ import com.jozufozu.flywheel.core.Materials; import com.jozufozu.flywheel.core.hardcoded.ModelPart; import com.jozufozu.flywheel.core.model.SimpleLazyModel; import com.jozufozu.flywheel.core.structs.StructTypes; -import com.jozufozu.flywheel.core.structs.model.TransformedPart; +import com.jozufozu.flywheel.core.structs.transformed.TransformedPart; import com.jozufozu.flywheel.util.AnimationTickHolder; import com.jozufozu.flywheel.util.transform.TransformStack; import com.mojang.blaze3d.vertex.PoseStack; diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/effect/ExampleEffect.java b/src/main/java/com/jozufozu/flywheel/vanilla/effect/ExampleEffect.java index 5c583534c..c0adede2e 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/effect/ExampleEffect.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/effect/ExampleEffect.java @@ -13,7 +13,7 @@ import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; import com.jozufozu.flywheel.backend.instancing.effect.Effect; import com.jozufozu.flywheel.core.model.Models; import com.jozufozu.flywheel.core.structs.StructTypes; -import com.jozufozu.flywheel.core.structs.model.TransformedPart; +import com.jozufozu.flywheel.core.structs.transformed.TransformedPart; import com.jozufozu.flywheel.event.ReloadRenderersEvent; import com.jozufozu.flywheel.util.AnimationTickHolder; import com.jozufozu.flywheel.util.box.GridAlignedBB;